* gcc-interface/trans.c (process_freeze_entity): Be prepared for a
[official-gcc.git] / gcc / dwarf2out.c
blob70409410fde79ee4956a0a541917f9682bc8e0fd
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "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 default:
577 return dw_cfi_oprnd_unused;
581 /* Output one FDE. */
583 static void
584 output_fde (dw_fde_ref fde, bool for_eh, bool second,
585 char *section_start_label, int fde_encoding, char *augmentation,
586 bool any_lsda_needed, int lsda_encoding)
588 const char *begin, *end;
589 static unsigned int j;
590 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
592 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
593 /* empty */ 0);
594 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
595 for_eh + j);
596 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
597 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
598 if (!XCOFF_DEBUGGING_INFO || for_eh)
600 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
601 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
602 " indicating 64-bit DWARF extension");
603 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
604 "FDE Length");
606 ASM_OUTPUT_LABEL (asm_out_file, l1);
608 if (for_eh)
609 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
610 else
611 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
612 debug_frame_section, "FDE CIE offset");
614 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
615 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
617 if (for_eh)
619 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
620 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
621 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
622 "FDE initial location");
623 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
624 end, begin, "FDE address range");
626 else
628 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
629 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
632 if (augmentation[0])
634 if (any_lsda_needed)
636 int size = size_of_encoded_value (lsda_encoding);
638 if (lsda_encoding == DW_EH_PE_aligned)
640 int offset = ( 4 /* Length */
641 + 4 /* CIE offset */
642 + 2 * size_of_encoded_value (fde_encoding)
643 + 1 /* Augmentation size */ );
644 int pad = -offset & (PTR_SIZE - 1);
646 size += pad;
647 gcc_assert (size_of_uleb128 (size) == 1);
650 dw2_asm_output_data_uleb128 (size, "Augmentation size");
652 if (fde->uses_eh_lsda)
654 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
655 fde->funcdef_number);
656 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
657 gen_rtx_SYMBOL_REF (Pmode, l1),
658 false,
659 "Language Specific Data Area");
661 else
663 if (lsda_encoding == DW_EH_PE_aligned)
664 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
665 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
666 "Language Specific Data Area (none)");
669 else
670 dw2_asm_output_data_uleb128 (0, "Augmentation size");
673 /* Loop through the Call Frame Instructions associated with this FDE. */
674 fde->dw_fde_current_label = begin;
676 size_t from, until, i;
678 from = 0;
679 until = vec_safe_length (fde->dw_fde_cfi);
681 if (fde->dw_fde_second_begin == NULL)
683 else if (!second)
684 until = fde->dw_fde_switch_cfi_index;
685 else
686 from = fde->dw_fde_switch_cfi_index;
688 for (i = from; i < until; i++)
689 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
692 /* If we are to emit a ref/link from function bodies to their frame tables,
693 do it now. This is typically performed to make sure that tables
694 associated with functions are dragged with them and not discarded in
695 garbage collecting links. We need to do this on a per function basis to
696 cope with -ffunction-sections. */
698 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
699 /* Switch to the function section, emit the ref to the tables, and
700 switch *back* into the table section. */
701 switch_to_section (function_section (fde->decl));
702 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
703 switch_to_frame_table_section (for_eh, true);
704 #endif
706 /* Pad the FDE out to an address sized boundary. */
707 ASM_OUTPUT_ALIGN (asm_out_file,
708 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
709 ASM_OUTPUT_LABEL (asm_out_file, l2);
711 j += 2;
714 /* Return true if frame description entry FDE is needed for EH. */
716 static bool
717 fde_needed_for_eh_p (dw_fde_ref fde)
719 if (flag_asynchronous_unwind_tables)
720 return true;
722 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
723 return true;
725 if (fde->uses_eh_lsda)
726 return true;
728 /* If exceptions are enabled, we have collected nothrow info. */
729 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
730 return false;
732 return true;
735 /* Output the call frame information used to record information
736 that relates to calculating the frame pointer, and records the
737 location of saved registers. */
739 static void
740 output_call_frame_info (int for_eh)
742 unsigned int i;
743 dw_fde_ref fde;
744 dw_cfi_ref cfi;
745 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
746 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
773 bool any_eh_needed = false;
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
785 if (!any_eh_needed)
786 return;
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (!XCOFF_DEBUGGING_INFO || for_eh)
804 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
805 dw2_asm_output_data (4, 0xffffffff,
806 "Initial length escape value indicating 64-bit DWARF extension");
807 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
808 "Length of Common Information Entry");
810 ASM_OUTPUT_LABEL (asm_out_file, l1);
812 /* Now that the CIE pointer is PC-relative for EH,
813 use 0 to identify the CIE. */
814 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
815 (for_eh ? 0 : DWARF_CIE_ID),
816 "CIE Identifier Tag");
818 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
819 use CIE version 1, unless that would produce incorrect results
820 due to overflowing the return register column. */
821 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
822 dw_cie_version = 1;
823 if (return_reg >= 256 || dwarf_version > 2)
824 dw_cie_version = 3;
825 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
827 augmentation[0] = 0;
828 augmentation_size = 0;
830 personality = current_unit_personality;
831 if (for_eh)
833 char *p;
835 /* Augmentation:
836 z Indicates that a uleb128 is present to size the
837 augmentation section.
838 L Indicates the encoding (and thus presence) of
839 an LSDA pointer in the FDE augmentation.
840 R Indicates a non-default pointer encoding for
841 FDE code pointers.
842 P Indicates the presence of an encoding + language
843 personality routine in the CIE augmentation. */
845 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
846 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
847 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
849 p = augmentation + 1;
850 if (personality)
852 *p++ = 'P';
853 augmentation_size += 1 + size_of_encoded_value (per_encoding);
854 assemble_external_libcall (personality);
856 if (any_lsda_needed)
858 *p++ = 'L';
859 augmentation_size += 1;
861 if (fde_encoding != DW_EH_PE_absptr)
863 *p++ = 'R';
864 augmentation_size += 1;
866 if (p > augmentation + 1)
868 augmentation[0] = 'z';
869 *p = '\0';
872 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
873 if (personality && per_encoding == DW_EH_PE_aligned)
875 int offset = ( 4 /* Length */
876 + 4 /* CIE Id */
877 + 1 /* CIE version */
878 + strlen (augmentation) + 1 /* Augmentation */
879 + size_of_uleb128 (1) /* Code alignment */
880 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
881 + 1 /* RA column */
882 + 1 /* Augmentation size */
883 + 1 /* Personality encoding */ );
884 int pad = -offset & (PTR_SIZE - 1);
886 augmentation_size += pad;
888 /* Augmentations should be small, so there's scarce need to
889 iterate for a solution. Die if we exceed one uleb128 byte. */
890 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
894 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
895 if (dw_cie_version >= 4)
897 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
898 dw2_asm_output_data (1, 0, "CIE Segment Size");
900 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
901 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
902 "CIE Data Alignment Factor");
904 if (dw_cie_version == 1)
905 dw2_asm_output_data (1, return_reg, "CIE RA Column");
906 else
907 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
909 if (augmentation[0])
911 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
912 if (personality)
914 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
915 eh_data_format_name (per_encoding));
916 dw2_asm_output_encoded_addr_rtx (per_encoding,
917 personality,
918 true, NULL);
921 if (any_lsda_needed)
922 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
923 eh_data_format_name (lsda_encoding));
925 if (fde_encoding != DW_EH_PE_absptr)
926 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
927 eh_data_format_name (fde_encoding));
930 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
931 output_cfi (cfi, NULL, for_eh);
933 /* Pad the CIE out to an address sized boundary. */
934 ASM_OUTPUT_ALIGN (asm_out_file,
935 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
936 ASM_OUTPUT_LABEL (asm_out_file, l2);
938 /* Loop through all of the FDE's. */
939 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
941 unsigned int k;
943 /* Don't emit EH unwind info for leaf functions that don't need it. */
944 if (for_eh && !fde_needed_for_eh_p (fde))
945 continue;
947 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
948 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
949 augmentation, any_lsda_needed, lsda_encoding);
952 if (for_eh && targetm.terminate_dw2_eh_frame_info)
953 dw2_asm_output_data (4, 0, "End of Table");
955 /* Turn off app to make assembly quicker. */
956 if (flag_debug_asm)
957 app_disable ();
960 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
962 static void
963 dwarf2out_do_cfi_startproc (bool second)
965 int enc;
966 rtx ref;
968 fprintf (asm_out_file, "\t.cfi_startproc\n");
970 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
971 eh unwinders. */
972 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
973 return;
975 rtx personality = get_personality_function (current_function_decl);
977 if (personality)
979 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
980 ref = personality;
982 /* ??? The GAS support isn't entirely consistent. We have to
983 handle indirect support ourselves, but PC-relative is done
984 in the assembler. Further, the assembler can't handle any
985 of the weirder relocation types. */
986 if (enc & DW_EH_PE_indirect)
987 ref = dw2_force_const_mem (ref, true);
989 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
990 output_addr_const (asm_out_file, ref);
991 fputc ('\n', asm_out_file);
994 if (crtl->uses_eh_lsda)
996 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
998 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
999 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1000 current_function_funcdef_no);
1001 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1002 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1004 if (enc & DW_EH_PE_indirect)
1005 ref = dw2_force_const_mem (ref, true);
1007 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1008 output_addr_const (asm_out_file, ref);
1009 fputc ('\n', asm_out_file);
1013 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1014 this allocation may be done before pass_final. */
1016 dw_fde_ref
1017 dwarf2out_alloc_current_fde (void)
1019 dw_fde_ref fde;
1021 fde = ggc_cleared_alloc<dw_fde_node> ();
1022 fde->decl = current_function_decl;
1023 fde->funcdef_number = current_function_funcdef_no;
1024 fde->fde_index = vec_safe_length (fde_vec);
1025 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1026 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1027 fde->nothrow = crtl->nothrow;
1028 fde->drap_reg = INVALID_REGNUM;
1029 fde->vdrap_reg = INVALID_REGNUM;
1031 /* Record the FDE associated with this function. */
1032 cfun->fde = fde;
1033 vec_safe_push (fde_vec, fde);
1035 return fde;
1038 /* Output a marker (i.e. a label) for the beginning of a function, before
1039 the prologue. */
1041 void
1042 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1043 unsigned int column ATTRIBUTE_UNUSED,
1044 const char *file ATTRIBUTE_UNUSED)
1046 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1047 char * dup_label;
1048 dw_fde_ref fde;
1049 section *fnsec;
1050 bool do_frame;
1052 current_function_func_begin_label = NULL;
1054 do_frame = dwarf2out_do_frame ();
1056 /* ??? current_function_func_begin_label is also used by except.c for
1057 call-site information. We must emit this label if it might be used. */
1058 if (!do_frame
1059 && (!flag_exceptions
1060 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1061 return;
1063 fnsec = function_section (current_function_decl);
1064 switch_to_section (fnsec);
1065 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1066 current_function_funcdef_no);
1067 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1068 current_function_funcdef_no);
1069 dup_label = xstrdup (label);
1070 current_function_func_begin_label = dup_label;
1072 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1073 if (!do_frame)
1074 return;
1076 /* Unlike the debug version, the EH version of frame unwind info is a per-
1077 function setting so we need to record whether we need it for the unit. */
1078 do_eh_frame |= dwarf2out_do_eh_frame ();
1080 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1081 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1082 would include pass_dwarf2_frame. If we've not created the FDE yet,
1083 do so now. */
1084 fde = cfun->fde;
1085 if (fde == NULL)
1086 fde = dwarf2out_alloc_current_fde ();
1088 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1089 fde->dw_fde_begin = dup_label;
1090 fde->dw_fde_current_label = dup_label;
1091 fde->in_std_section = (fnsec == text_section
1092 || (cold_text_section && fnsec == cold_text_section));
1094 /* We only want to output line number information for the genuine dwarf2
1095 prologue case, not the eh frame case. */
1096 #ifdef DWARF2_DEBUGGING_INFO
1097 if (file)
1098 dwarf2out_source_line (line, column, file, 0, true);
1099 #endif
1101 if (dwarf2out_do_cfi_asm ())
1102 dwarf2out_do_cfi_startproc (false);
1103 else
1105 rtx personality = get_personality_function (current_function_decl);
1106 if (!current_unit_personality)
1107 current_unit_personality = personality;
1109 /* We cannot keep a current personality per function as without CFI
1110 asm, at the point where we emit the CFI data, there is no current
1111 function anymore. */
1112 if (personality && current_unit_personality != personality)
1113 sorry ("multiple EH personalities are supported only with assemblers "
1114 "supporting .cfi_personality directive");
1118 /* Output a marker (i.e. a label) for the end of the generated code
1119 for a function prologue. This gets called *after* the prologue code has
1120 been generated. */
1122 void
1123 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1124 const char *file ATTRIBUTE_UNUSED)
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128 /* Output a label to mark the endpoint of the code generated for this
1129 function. */
1130 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1131 current_function_funcdef_no);
1132 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1133 current_function_funcdef_no);
1134 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1137 /* Output a marker (i.e. a label) for the beginning of the generated code
1138 for a function epilogue. This gets called *before* the prologue code has
1139 been generated. */
1141 void
1142 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1143 const char *file ATTRIBUTE_UNUSED)
1145 dw_fde_ref fde = cfun->fde;
1146 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1148 if (fde->dw_fde_vms_begin_epilogue)
1149 return;
1151 /* Output a label to mark the endpoint of the code generated for this
1152 function. */
1153 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1154 current_function_funcdef_no);
1155 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1156 current_function_funcdef_no);
1157 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1160 /* Output a marker (i.e. a label) for the absolute end of the generated code
1161 for a function definition. This gets called *after* the epilogue code has
1162 been generated. */
1164 void
1165 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1166 const char *file ATTRIBUTE_UNUSED)
1168 dw_fde_ref fde;
1169 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1171 last_var_location_insn = NULL;
1172 cached_next_real_insn = NULL;
1174 if (dwarf2out_do_cfi_asm ())
1175 fprintf (asm_out_file, "\t.cfi_endproc\n");
1177 /* Output a label to mark the endpoint of the code generated for this
1178 function. */
1179 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1180 current_function_funcdef_no);
1181 ASM_OUTPUT_LABEL (asm_out_file, label);
1182 fde = cfun->fde;
1183 gcc_assert (fde != NULL);
1184 if (fde->dw_fde_second_begin == NULL)
1185 fde->dw_fde_end = xstrdup (label);
1188 void
1189 dwarf2out_frame_finish (void)
1191 /* Output call frame information. */
1192 if (targetm.debug_unwind_info () == UI_DWARF2)
1193 output_call_frame_info (0);
1195 /* Output another copy for the unwinder. */
1196 if (do_eh_frame)
1197 output_call_frame_info (1);
1200 /* Note that the current function section is being used for code. */
1202 static void
1203 dwarf2out_note_section_used (void)
1205 section *sec = current_function_section ();
1206 if (sec == text_section)
1207 text_section_used = true;
1208 else if (sec == cold_text_section)
1209 cold_text_section_used = true;
1212 static void var_location_switch_text_section (void);
1213 static void set_cur_line_info_table (section *);
1215 void
1216 dwarf2out_switch_text_section (void)
1218 section *sect;
1219 dw_fde_ref fde = cfun->fde;
1221 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1223 if (!in_cold_section_p)
1225 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1226 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1227 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1229 else
1231 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1232 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1233 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1235 have_multiple_function_sections = true;
1237 /* There is no need to mark used sections when not debugging. */
1238 if (cold_text_section != NULL)
1239 dwarf2out_note_section_used ();
1241 if (dwarf2out_do_cfi_asm ())
1242 fprintf (asm_out_file, "\t.cfi_endproc\n");
1244 /* Now do the real section switch. */
1245 sect = current_function_section ();
1246 switch_to_section (sect);
1248 fde->second_in_std_section
1249 = (sect == text_section
1250 || (cold_text_section && sect == cold_text_section));
1252 if (dwarf2out_do_cfi_asm ())
1253 dwarf2out_do_cfi_startproc (true);
1255 var_location_switch_text_section ();
1257 if (cold_text_section != NULL)
1258 set_cur_line_info_table (sect);
1261 /* And now, the subset of the debugging information support code necessary
1262 for emitting location expressions. */
1264 /* Data about a single source file. */
1265 struct GTY((for_user)) dwarf_file_data {
1266 const char * filename;
1267 int emitted_number;
1270 /* Describe an entry into the .debug_addr section. */
1272 enum ate_kind {
1273 ate_kind_rtx,
1274 ate_kind_rtx_dtprel,
1275 ate_kind_label
1278 struct GTY((for_user)) addr_table_entry {
1279 enum ate_kind kind;
1280 unsigned int refcount;
1281 unsigned int index;
1282 union addr_table_entry_struct_union
1284 rtx GTY ((tag ("0"))) rtl;
1285 char * GTY ((tag ("1"))) label;
1287 GTY ((desc ("%1.kind"))) addr;
1290 /* Location lists are ranges + location descriptions for that range,
1291 so you can track variables that are in different places over
1292 their entire life. */
1293 typedef struct GTY(()) dw_loc_list_struct {
1294 dw_loc_list_ref dw_loc_next;
1295 const char *begin; /* Label and addr_entry for start of range */
1296 addr_table_entry *begin_entry;
1297 const char *end; /* Label for end of range */
1298 char *ll_symbol; /* Label for beginning of location list.
1299 Only on head of list */
1300 const char *section; /* Section this loclist is relative to */
1301 dw_loc_descr_ref expr;
1302 hashval_t hash;
1303 /* True if all addresses in this and subsequent lists are known to be
1304 resolved. */
1305 bool resolved_addr;
1306 /* True if this list has been replaced by dw_loc_next. */
1307 bool replaced;
1308 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1309 section. */
1310 unsigned char emitted : 1;
1311 /* True if hash field is index rather than hash value. */
1312 unsigned char num_assigned : 1;
1313 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1314 unsigned char offset_emitted : 1;
1315 /* True if note_variable_value_in_expr has been called on it. */
1316 unsigned char noted_variable_value : 1;
1317 /* True if the range should be emitted even if begin and end
1318 are the same. */
1319 bool force;
1320 } dw_loc_list_node;
1322 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1323 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1325 /* Convert a DWARF stack opcode into its string name. */
1327 static const char *
1328 dwarf_stack_op_name (unsigned int op)
1330 const char *name = get_DW_OP_name (op);
1332 if (name != NULL)
1333 return name;
1335 return "OP_<unknown>";
1338 /* Return a pointer to a newly allocated location description. Location
1339 descriptions are simple expression terms that can be strung
1340 together to form more complicated location (address) descriptions. */
1342 static inline dw_loc_descr_ref
1343 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1344 unsigned HOST_WIDE_INT oprnd2)
1346 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1348 descr->dw_loc_opc = op;
1349 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1350 descr->dw_loc_oprnd1.val_entry = NULL;
1351 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1352 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1353 descr->dw_loc_oprnd2.val_entry = NULL;
1354 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1356 return descr;
1359 /* Return a pointer to a newly allocated location description for
1360 REG and OFFSET. */
1362 static inline dw_loc_descr_ref
1363 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1365 if (reg <= 31)
1366 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1367 offset, 0);
1368 else
1369 return new_loc_descr (DW_OP_bregx, reg, offset);
1372 /* Add a location description term to a location description expression. */
1374 static inline void
1375 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1377 dw_loc_descr_ref *d;
1379 /* Find the end of the chain. */
1380 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1383 *d = descr;
1386 /* Compare two location operands for exact equality. */
1388 static bool
1389 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1391 if (a->val_class != b->val_class)
1392 return false;
1393 switch (a->val_class)
1395 case dw_val_class_none:
1396 return true;
1397 case dw_val_class_addr:
1398 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1400 case dw_val_class_offset:
1401 case dw_val_class_unsigned_const:
1402 case dw_val_class_const:
1403 case dw_val_class_unsigned_const_implicit:
1404 case dw_val_class_const_implicit:
1405 case dw_val_class_range_list:
1406 /* These are all HOST_WIDE_INT, signed or unsigned. */
1407 return a->v.val_unsigned == b->v.val_unsigned;
1409 case dw_val_class_loc:
1410 return a->v.val_loc == b->v.val_loc;
1411 case dw_val_class_loc_list:
1412 return a->v.val_loc_list == b->v.val_loc_list;
1413 case dw_val_class_die_ref:
1414 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1415 case dw_val_class_fde_ref:
1416 return a->v.val_fde_index == b->v.val_fde_index;
1417 case dw_val_class_lbl_id:
1418 case dw_val_class_lineptr:
1419 case dw_val_class_macptr:
1420 case dw_val_class_loclistsptr:
1421 case dw_val_class_high_pc:
1422 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1423 case dw_val_class_str:
1424 return a->v.val_str == b->v.val_str;
1425 case dw_val_class_flag:
1426 return a->v.val_flag == b->v.val_flag;
1427 case dw_val_class_file:
1428 case dw_val_class_file_implicit:
1429 return a->v.val_file == b->v.val_file;
1430 case dw_val_class_decl_ref:
1431 return a->v.val_decl_ref == b->v.val_decl_ref;
1433 case dw_val_class_const_double:
1434 return (a->v.val_double.high == b->v.val_double.high
1435 && a->v.val_double.low == b->v.val_double.low);
1437 case dw_val_class_wide_int:
1438 return *a->v.val_wide == *b->v.val_wide;
1440 case dw_val_class_vec:
1442 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1443 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1445 return (a_len == b_len
1446 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1449 case dw_val_class_data8:
1450 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1452 case dw_val_class_vms_delta:
1453 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1454 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1456 case dw_val_class_discr_value:
1457 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1458 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1459 case dw_val_class_discr_list:
1460 /* It makes no sense comparing two discriminant value lists. */
1461 return false;
1463 gcc_unreachable ();
1466 /* Compare two location atoms for exact equality. */
1468 static bool
1469 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1471 if (a->dw_loc_opc != b->dw_loc_opc)
1472 return false;
1474 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1475 address size, but since we always allocate cleared storage it
1476 should be zero for other types of locations. */
1477 if (a->dtprel != b->dtprel)
1478 return false;
1480 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1481 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1484 /* Compare two complete location expressions for exact equality. */
1486 bool
1487 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1489 while (1)
1491 if (a == b)
1492 return true;
1493 if (a == NULL || b == NULL)
1494 return false;
1495 if (!loc_descr_equal_p_1 (a, b))
1496 return false;
1498 a = a->dw_loc_next;
1499 b = b->dw_loc_next;
1504 /* Add a constant OFFSET to a location expression. */
1506 static void
1507 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1509 dw_loc_descr_ref loc;
1510 HOST_WIDE_INT *p;
1512 gcc_assert (*list_head != NULL);
1514 if (!offset)
1515 return;
1517 /* Find the end of the chain. */
1518 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1521 p = NULL;
1522 if (loc->dw_loc_opc == DW_OP_fbreg
1523 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1524 p = &loc->dw_loc_oprnd1.v.val_int;
1525 else if (loc->dw_loc_opc == DW_OP_bregx)
1526 p = &loc->dw_loc_oprnd2.v.val_int;
1528 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1529 offset. Don't optimize if an signed integer overflow would happen. */
1530 if (p != NULL
1531 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1532 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1533 *p += offset;
1535 else if (offset > 0)
1536 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1538 else
1540 loc->dw_loc_next
1541 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1542 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1546 /* Add a constant OFFSET to a location list. */
1548 static void
1549 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1551 dw_loc_list_ref d;
1552 for (d = list_head; d != NULL; d = d->dw_loc_next)
1553 loc_descr_plus_const (&d->expr, offset);
1556 #define DWARF_REF_SIZE \
1557 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1559 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1560 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1561 DW_FORM_data16 with 128 bits. */
1562 #define DWARF_LARGEST_DATA_FORM_BITS \
1563 (dwarf_version >= 5 ? 128 : 64)
1565 /* Utility inline function for construction of ops that were GNU extension
1566 before DWARF 5. */
1567 static inline enum dwarf_location_atom
1568 dwarf_OP (enum dwarf_location_atom op)
1570 switch (op)
1572 case DW_OP_implicit_pointer:
1573 if (dwarf_version < 5)
1574 return DW_OP_GNU_implicit_pointer;
1575 break;
1577 case DW_OP_entry_value:
1578 if (dwarf_version < 5)
1579 return DW_OP_GNU_entry_value;
1580 break;
1582 case DW_OP_const_type:
1583 if (dwarf_version < 5)
1584 return DW_OP_GNU_const_type;
1585 break;
1587 case DW_OP_regval_type:
1588 if (dwarf_version < 5)
1589 return DW_OP_GNU_regval_type;
1590 break;
1592 case DW_OP_deref_type:
1593 if (dwarf_version < 5)
1594 return DW_OP_GNU_deref_type;
1595 break;
1597 case DW_OP_convert:
1598 if (dwarf_version < 5)
1599 return DW_OP_GNU_convert;
1600 break;
1602 case DW_OP_reinterpret:
1603 if (dwarf_version < 5)
1604 return DW_OP_GNU_reinterpret;
1605 break;
1607 default:
1608 break;
1610 return op;
1613 /* Similarly for attributes. */
1614 static inline enum dwarf_attribute
1615 dwarf_AT (enum dwarf_attribute at)
1617 switch (at)
1619 case DW_AT_call_return_pc:
1620 if (dwarf_version < 5)
1621 return DW_AT_low_pc;
1622 break;
1624 case DW_AT_call_tail_call:
1625 if (dwarf_version < 5)
1626 return DW_AT_GNU_tail_call;
1627 break;
1629 case DW_AT_call_origin:
1630 if (dwarf_version < 5)
1631 return DW_AT_abstract_origin;
1632 break;
1634 case DW_AT_call_target:
1635 if (dwarf_version < 5)
1636 return DW_AT_GNU_call_site_target;
1637 break;
1639 case DW_AT_call_target_clobbered:
1640 if (dwarf_version < 5)
1641 return DW_AT_GNU_call_site_target_clobbered;
1642 break;
1644 case DW_AT_call_parameter:
1645 if (dwarf_version < 5)
1646 return DW_AT_abstract_origin;
1647 break;
1649 case DW_AT_call_value:
1650 if (dwarf_version < 5)
1651 return DW_AT_GNU_call_site_value;
1652 break;
1654 case DW_AT_call_data_value:
1655 if (dwarf_version < 5)
1656 return DW_AT_GNU_call_site_data_value;
1657 break;
1659 case DW_AT_call_all_calls:
1660 if (dwarf_version < 5)
1661 return DW_AT_GNU_all_call_sites;
1662 break;
1664 case DW_AT_call_all_tail_calls:
1665 if (dwarf_version < 5)
1666 return DW_AT_GNU_all_tail_call_sites;
1667 break;
1669 case DW_AT_dwo_name:
1670 if (dwarf_version < 5)
1671 return DW_AT_GNU_dwo_name;
1672 break;
1674 default:
1675 break;
1677 return at;
1680 /* And similarly for tags. */
1681 static inline enum dwarf_tag
1682 dwarf_TAG (enum dwarf_tag tag)
1684 switch (tag)
1686 case DW_TAG_call_site:
1687 if (dwarf_version < 5)
1688 return DW_TAG_GNU_call_site;
1689 break;
1691 case DW_TAG_call_site_parameter:
1692 if (dwarf_version < 5)
1693 return DW_TAG_GNU_call_site_parameter;
1694 break;
1696 default:
1697 break;
1699 return tag;
1702 static unsigned long int get_base_type_offset (dw_die_ref);
1704 /* Return the size of a location descriptor. */
1706 static unsigned long
1707 size_of_loc_descr (dw_loc_descr_ref loc)
1709 unsigned long size = 1;
1711 switch (loc->dw_loc_opc)
1713 case DW_OP_addr:
1714 size += DWARF2_ADDR_SIZE;
1715 break;
1716 case DW_OP_GNU_addr_index:
1717 case DW_OP_GNU_const_index:
1718 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1719 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1720 break;
1721 case DW_OP_const1u:
1722 case DW_OP_const1s:
1723 size += 1;
1724 break;
1725 case DW_OP_const2u:
1726 case DW_OP_const2s:
1727 size += 2;
1728 break;
1729 case DW_OP_const4u:
1730 case DW_OP_const4s:
1731 size += 4;
1732 break;
1733 case DW_OP_const8u:
1734 case DW_OP_const8s:
1735 size += 8;
1736 break;
1737 case DW_OP_constu:
1738 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1739 break;
1740 case DW_OP_consts:
1741 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1742 break;
1743 case DW_OP_pick:
1744 size += 1;
1745 break;
1746 case DW_OP_plus_uconst:
1747 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1748 break;
1749 case DW_OP_skip:
1750 case DW_OP_bra:
1751 size += 2;
1752 break;
1753 case DW_OP_breg0:
1754 case DW_OP_breg1:
1755 case DW_OP_breg2:
1756 case DW_OP_breg3:
1757 case DW_OP_breg4:
1758 case DW_OP_breg5:
1759 case DW_OP_breg6:
1760 case DW_OP_breg7:
1761 case DW_OP_breg8:
1762 case DW_OP_breg9:
1763 case DW_OP_breg10:
1764 case DW_OP_breg11:
1765 case DW_OP_breg12:
1766 case DW_OP_breg13:
1767 case DW_OP_breg14:
1768 case DW_OP_breg15:
1769 case DW_OP_breg16:
1770 case DW_OP_breg17:
1771 case DW_OP_breg18:
1772 case DW_OP_breg19:
1773 case DW_OP_breg20:
1774 case DW_OP_breg21:
1775 case DW_OP_breg22:
1776 case DW_OP_breg23:
1777 case DW_OP_breg24:
1778 case DW_OP_breg25:
1779 case DW_OP_breg26:
1780 case DW_OP_breg27:
1781 case DW_OP_breg28:
1782 case DW_OP_breg29:
1783 case DW_OP_breg30:
1784 case DW_OP_breg31:
1785 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1786 break;
1787 case DW_OP_regx:
1788 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1789 break;
1790 case DW_OP_fbreg:
1791 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1792 break;
1793 case DW_OP_bregx:
1794 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1795 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1796 break;
1797 case DW_OP_piece:
1798 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1799 break;
1800 case DW_OP_bit_piece:
1801 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1802 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1803 break;
1804 case DW_OP_deref_size:
1805 case DW_OP_xderef_size:
1806 size += 1;
1807 break;
1808 case DW_OP_call2:
1809 size += 2;
1810 break;
1811 case DW_OP_call4:
1812 size += 4;
1813 break;
1814 case DW_OP_call_ref:
1815 case DW_OP_GNU_variable_value:
1816 size += DWARF_REF_SIZE;
1817 break;
1818 case DW_OP_implicit_value:
1819 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1820 + loc->dw_loc_oprnd1.v.val_unsigned;
1821 break;
1822 case DW_OP_implicit_pointer:
1823 case DW_OP_GNU_implicit_pointer:
1824 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1825 break;
1826 case DW_OP_entry_value:
1827 case DW_OP_GNU_entry_value:
1829 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1830 size += size_of_uleb128 (op_size) + op_size;
1831 break;
1833 case DW_OP_const_type:
1834 case DW_OP_GNU_const_type:
1836 unsigned long o
1837 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1838 size += size_of_uleb128 (o) + 1;
1839 switch (loc->dw_loc_oprnd2.val_class)
1841 case dw_val_class_vec:
1842 size += loc->dw_loc_oprnd2.v.val_vec.length
1843 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1844 break;
1845 case dw_val_class_const:
1846 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1847 break;
1848 case dw_val_class_const_double:
1849 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1850 break;
1851 case dw_val_class_wide_int:
1852 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1853 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1854 break;
1855 default:
1856 gcc_unreachable ();
1858 break;
1860 case DW_OP_regval_type:
1861 case DW_OP_GNU_regval_type:
1863 unsigned long o
1864 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1865 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1866 + size_of_uleb128 (o);
1868 break;
1869 case DW_OP_deref_type:
1870 case DW_OP_GNU_deref_type:
1872 unsigned long o
1873 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1874 size += 1 + size_of_uleb128 (o);
1876 break;
1877 case DW_OP_convert:
1878 case DW_OP_reinterpret:
1879 case DW_OP_GNU_convert:
1880 case DW_OP_GNU_reinterpret:
1881 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1882 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1883 else
1885 unsigned long o
1886 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1887 size += size_of_uleb128 (o);
1889 break;
1890 case DW_OP_GNU_parameter_ref:
1891 size += 4;
1892 break;
1893 default:
1894 break;
1897 return size;
1900 /* Return the size of a series of location descriptors. */
1902 unsigned long
1903 size_of_locs (dw_loc_descr_ref loc)
1905 dw_loc_descr_ref l;
1906 unsigned long size;
1908 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1909 field, to avoid writing to a PCH file. */
1910 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1912 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1913 break;
1914 size += size_of_loc_descr (l);
1916 if (! l)
1917 return size;
1919 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1921 l->dw_loc_addr = size;
1922 size += size_of_loc_descr (l);
1925 return size;
1928 /* Return the size of the value in a DW_AT_discr_value attribute. */
1930 static int
1931 size_of_discr_value (dw_discr_value *discr_value)
1933 if (discr_value->pos)
1934 return size_of_uleb128 (discr_value->v.uval);
1935 else
1936 return size_of_sleb128 (discr_value->v.sval);
1939 /* Return the size of the value in a DW_AT_discr_list attribute. */
1941 static int
1942 size_of_discr_list (dw_discr_list_ref discr_list)
1944 int size = 0;
1946 for (dw_discr_list_ref list = discr_list;
1947 list != NULL;
1948 list = list->dw_discr_next)
1950 /* One byte for the discriminant value descriptor, and then one or two
1951 LEB128 numbers, depending on whether it's a single case label or a
1952 range label. */
1953 size += 1;
1954 size += size_of_discr_value (&list->dw_discr_lower_bound);
1955 if (list->dw_discr_range != 0)
1956 size += size_of_discr_value (&list->dw_discr_upper_bound);
1958 return size;
1961 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1962 static void get_ref_die_offset_label (char *, dw_die_ref);
1963 static unsigned long int get_ref_die_offset (dw_die_ref);
1965 /* Output location description stack opcode's operands (if any).
1966 The for_eh_or_skip parameter controls whether register numbers are
1967 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1968 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1969 info). This should be suppressed for the cases that have not been converted
1970 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1972 static void
1973 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1975 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1976 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1978 switch (loc->dw_loc_opc)
1980 #ifdef DWARF2_DEBUGGING_INFO
1981 case DW_OP_const2u:
1982 case DW_OP_const2s:
1983 dw2_asm_output_data (2, val1->v.val_int, NULL);
1984 break;
1985 case DW_OP_const4u:
1986 if (loc->dtprel)
1988 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1989 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1990 val1->v.val_addr);
1991 fputc ('\n', asm_out_file);
1992 break;
1994 /* FALLTHRU */
1995 case DW_OP_const4s:
1996 dw2_asm_output_data (4, val1->v.val_int, NULL);
1997 break;
1998 case DW_OP_const8u:
1999 if (loc->dtprel)
2001 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2002 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2003 val1->v.val_addr);
2004 fputc ('\n', asm_out_file);
2005 break;
2007 /* FALLTHRU */
2008 case DW_OP_const8s:
2009 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2010 dw2_asm_output_data (8, val1->v.val_int, NULL);
2011 break;
2012 case DW_OP_skip:
2013 case DW_OP_bra:
2015 int offset;
2017 gcc_assert (val1->val_class == dw_val_class_loc);
2018 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2020 dw2_asm_output_data (2, offset, NULL);
2022 break;
2023 case DW_OP_implicit_value:
2024 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2025 switch (val2->val_class)
2027 case dw_val_class_const:
2028 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2029 break;
2030 case dw_val_class_vec:
2032 unsigned int elt_size = val2->v.val_vec.elt_size;
2033 unsigned int len = val2->v.val_vec.length;
2034 unsigned int i;
2035 unsigned char *p;
2037 if (elt_size > sizeof (HOST_WIDE_INT))
2039 elt_size /= 2;
2040 len *= 2;
2042 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2043 i < len;
2044 i++, p += elt_size)
2045 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2046 "fp or vector constant word %u", i);
2048 break;
2049 case dw_val_class_const_double:
2051 unsigned HOST_WIDE_INT first, second;
2053 if (WORDS_BIG_ENDIAN)
2055 first = val2->v.val_double.high;
2056 second = val2->v.val_double.low;
2058 else
2060 first = val2->v.val_double.low;
2061 second = val2->v.val_double.high;
2063 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2064 first, NULL);
2065 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2066 second, NULL);
2068 break;
2069 case dw_val_class_wide_int:
2071 int i;
2072 int len = get_full_len (*val2->v.val_wide);
2073 if (WORDS_BIG_ENDIAN)
2074 for (i = len - 1; i >= 0; --i)
2075 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2076 val2->v.val_wide->elt (i), NULL);
2077 else
2078 for (i = 0; i < len; ++i)
2079 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2080 val2->v.val_wide->elt (i), NULL);
2082 break;
2083 case dw_val_class_addr:
2084 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2085 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2086 break;
2087 default:
2088 gcc_unreachable ();
2090 break;
2091 #else
2092 case DW_OP_const2u:
2093 case DW_OP_const2s:
2094 case DW_OP_const4u:
2095 case DW_OP_const4s:
2096 case DW_OP_const8u:
2097 case DW_OP_const8s:
2098 case DW_OP_skip:
2099 case DW_OP_bra:
2100 case DW_OP_implicit_value:
2101 /* We currently don't make any attempt to make sure these are
2102 aligned properly like we do for the main unwind info, so
2103 don't support emitting things larger than a byte if we're
2104 only doing unwinding. */
2105 gcc_unreachable ();
2106 #endif
2107 case DW_OP_const1u:
2108 case DW_OP_const1s:
2109 dw2_asm_output_data (1, val1->v.val_int, NULL);
2110 break;
2111 case DW_OP_constu:
2112 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2113 break;
2114 case DW_OP_consts:
2115 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2116 break;
2117 case DW_OP_pick:
2118 dw2_asm_output_data (1, val1->v.val_int, NULL);
2119 break;
2120 case DW_OP_plus_uconst:
2121 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2122 break;
2123 case DW_OP_breg0:
2124 case DW_OP_breg1:
2125 case DW_OP_breg2:
2126 case DW_OP_breg3:
2127 case DW_OP_breg4:
2128 case DW_OP_breg5:
2129 case DW_OP_breg6:
2130 case DW_OP_breg7:
2131 case DW_OP_breg8:
2132 case DW_OP_breg9:
2133 case DW_OP_breg10:
2134 case DW_OP_breg11:
2135 case DW_OP_breg12:
2136 case DW_OP_breg13:
2137 case DW_OP_breg14:
2138 case DW_OP_breg15:
2139 case DW_OP_breg16:
2140 case DW_OP_breg17:
2141 case DW_OP_breg18:
2142 case DW_OP_breg19:
2143 case DW_OP_breg20:
2144 case DW_OP_breg21:
2145 case DW_OP_breg22:
2146 case DW_OP_breg23:
2147 case DW_OP_breg24:
2148 case DW_OP_breg25:
2149 case DW_OP_breg26:
2150 case DW_OP_breg27:
2151 case DW_OP_breg28:
2152 case DW_OP_breg29:
2153 case DW_OP_breg30:
2154 case DW_OP_breg31:
2155 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2156 break;
2157 case DW_OP_regx:
2159 unsigned r = val1->v.val_unsigned;
2160 if (for_eh_or_skip >= 0)
2161 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162 gcc_assert (size_of_uleb128 (r)
2163 == size_of_uleb128 (val1->v.val_unsigned));
2164 dw2_asm_output_data_uleb128 (r, NULL);
2166 break;
2167 case DW_OP_fbreg:
2168 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2169 break;
2170 case DW_OP_bregx:
2172 unsigned r = val1->v.val_unsigned;
2173 if (for_eh_or_skip >= 0)
2174 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2175 gcc_assert (size_of_uleb128 (r)
2176 == size_of_uleb128 (val1->v.val_unsigned));
2177 dw2_asm_output_data_uleb128 (r, NULL);
2178 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2180 break;
2181 case DW_OP_piece:
2182 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2183 break;
2184 case DW_OP_bit_piece:
2185 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2186 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2187 break;
2188 case DW_OP_deref_size:
2189 case DW_OP_xderef_size:
2190 dw2_asm_output_data (1, val1->v.val_int, NULL);
2191 break;
2193 case DW_OP_addr:
2194 if (loc->dtprel)
2196 if (targetm.asm_out.output_dwarf_dtprel)
2198 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2199 DWARF2_ADDR_SIZE,
2200 val1->v.val_addr);
2201 fputc ('\n', asm_out_file);
2203 else
2204 gcc_unreachable ();
2206 else
2208 #ifdef DWARF2_DEBUGGING_INFO
2209 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2210 #else
2211 gcc_unreachable ();
2212 #endif
2214 break;
2216 case DW_OP_GNU_addr_index:
2217 case DW_OP_GNU_const_index:
2218 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2219 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2220 "(index into .debug_addr)");
2221 break;
2223 case DW_OP_call2:
2224 case DW_OP_call4:
2226 unsigned long die_offset
2227 = get_ref_die_offset (val1->v.val_die_ref.die);
2228 /* Make sure the offset has been computed and that we can encode it as
2229 an operand. */
2230 gcc_assert (die_offset > 0
2231 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2232 ? 0xffff
2233 : 0xffffffff));
2234 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2235 die_offset, NULL);
2237 break;
2239 case DW_OP_call_ref:
2240 case DW_OP_GNU_variable_value:
2242 char label[MAX_ARTIFICIAL_LABEL_BYTES
2243 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2244 gcc_assert (val1->val_class == dw_val_class_die_ref);
2245 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2246 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2248 break;
2250 case DW_OP_implicit_pointer:
2251 case DW_OP_GNU_implicit_pointer:
2253 char label[MAX_ARTIFICIAL_LABEL_BYTES
2254 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2255 gcc_assert (val1->val_class == dw_val_class_die_ref);
2256 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2257 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2258 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2260 break;
2262 case DW_OP_entry_value:
2263 case DW_OP_GNU_entry_value:
2264 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2265 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2266 break;
2268 case DW_OP_const_type:
2269 case DW_OP_GNU_const_type:
2271 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2272 gcc_assert (o);
2273 dw2_asm_output_data_uleb128 (o, NULL);
2274 switch (val2->val_class)
2276 case dw_val_class_const:
2277 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2278 dw2_asm_output_data (1, l, NULL);
2279 dw2_asm_output_data (l, val2->v.val_int, NULL);
2280 break;
2281 case dw_val_class_vec:
2283 unsigned int elt_size = val2->v.val_vec.elt_size;
2284 unsigned int len = val2->v.val_vec.length;
2285 unsigned int i;
2286 unsigned char *p;
2288 l = len * elt_size;
2289 dw2_asm_output_data (1, l, NULL);
2290 if (elt_size > sizeof (HOST_WIDE_INT))
2292 elt_size /= 2;
2293 len *= 2;
2295 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2296 i < len;
2297 i++, p += elt_size)
2298 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2299 "fp or vector constant word %u", i);
2301 break;
2302 case dw_val_class_const_double:
2304 unsigned HOST_WIDE_INT first, second;
2305 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2307 dw2_asm_output_data (1, 2 * l, NULL);
2308 if (WORDS_BIG_ENDIAN)
2310 first = val2->v.val_double.high;
2311 second = val2->v.val_double.low;
2313 else
2315 first = val2->v.val_double.low;
2316 second = val2->v.val_double.high;
2318 dw2_asm_output_data (l, first, NULL);
2319 dw2_asm_output_data (l, second, NULL);
2321 break;
2322 case dw_val_class_wide_int:
2324 int i;
2325 int len = get_full_len (*val2->v.val_wide);
2326 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2328 dw2_asm_output_data (1, len * l, NULL);
2329 if (WORDS_BIG_ENDIAN)
2330 for (i = len - 1; i >= 0; --i)
2331 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2332 else
2333 for (i = 0; i < len; ++i)
2334 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2336 break;
2337 default:
2338 gcc_unreachable ();
2341 break;
2342 case DW_OP_regval_type:
2343 case DW_OP_GNU_regval_type:
2345 unsigned r = val1->v.val_unsigned;
2346 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2347 gcc_assert (o);
2348 if (for_eh_or_skip >= 0)
2350 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2351 gcc_assert (size_of_uleb128 (r)
2352 == size_of_uleb128 (val1->v.val_unsigned));
2354 dw2_asm_output_data_uleb128 (r, NULL);
2355 dw2_asm_output_data_uleb128 (o, NULL);
2357 break;
2358 case DW_OP_deref_type:
2359 case DW_OP_GNU_deref_type:
2361 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2362 gcc_assert (o);
2363 dw2_asm_output_data (1, val1->v.val_int, NULL);
2364 dw2_asm_output_data_uleb128 (o, NULL);
2366 break;
2367 case DW_OP_convert:
2368 case DW_OP_reinterpret:
2369 case DW_OP_GNU_convert:
2370 case DW_OP_GNU_reinterpret:
2371 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2372 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2373 else
2375 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2376 gcc_assert (o);
2377 dw2_asm_output_data_uleb128 (o, NULL);
2379 break;
2381 case DW_OP_GNU_parameter_ref:
2383 unsigned long o;
2384 gcc_assert (val1->val_class == dw_val_class_die_ref);
2385 o = get_ref_die_offset (val1->v.val_die_ref.die);
2386 dw2_asm_output_data (4, o, NULL);
2388 break;
2390 default:
2391 /* Other codes have no operands. */
2392 break;
2396 /* Output a sequence of location operations.
2397 The for_eh_or_skip parameter controls whether register numbers are
2398 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2399 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2400 info). This should be suppressed for the cases that have not been converted
2401 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2403 void
2404 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2406 for (; loc != NULL; loc = loc->dw_loc_next)
2408 enum dwarf_location_atom opc = loc->dw_loc_opc;
2409 /* Output the opcode. */
2410 if (for_eh_or_skip >= 0
2411 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2413 unsigned r = (opc - DW_OP_breg0);
2414 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2415 gcc_assert (r <= 31);
2416 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2418 else if (for_eh_or_skip >= 0
2419 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2421 unsigned r = (opc - DW_OP_reg0);
2422 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2423 gcc_assert (r <= 31);
2424 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2427 dw2_asm_output_data (1, opc,
2428 "%s", dwarf_stack_op_name (opc));
2430 /* Output the operand(s) (if any). */
2431 output_loc_operands (loc, for_eh_or_skip);
2435 /* Output location description stack opcode's operands (if any).
2436 The output is single bytes on a line, suitable for .cfi_escape. */
2438 static void
2439 output_loc_operands_raw (dw_loc_descr_ref loc)
2441 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2442 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2444 switch (loc->dw_loc_opc)
2446 case DW_OP_addr:
2447 case DW_OP_GNU_addr_index:
2448 case DW_OP_GNU_const_index:
2449 case DW_OP_implicit_value:
2450 /* We cannot output addresses in .cfi_escape, only bytes. */
2451 gcc_unreachable ();
2453 case DW_OP_const1u:
2454 case DW_OP_const1s:
2455 case DW_OP_pick:
2456 case DW_OP_deref_size:
2457 case DW_OP_xderef_size:
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (1, val1->v.val_int);
2460 break;
2462 case DW_OP_const2u:
2463 case DW_OP_const2s:
2464 fputc (',', asm_out_file);
2465 dw2_asm_output_data_raw (2, val1->v.val_int);
2466 break;
2468 case DW_OP_const4u:
2469 case DW_OP_const4s:
2470 fputc (',', asm_out_file);
2471 dw2_asm_output_data_raw (4, val1->v.val_int);
2472 break;
2474 case DW_OP_const8u:
2475 case DW_OP_const8s:
2476 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2477 fputc (',', asm_out_file);
2478 dw2_asm_output_data_raw (8, val1->v.val_int);
2479 break;
2481 case DW_OP_skip:
2482 case DW_OP_bra:
2484 int offset;
2486 gcc_assert (val1->val_class == dw_val_class_loc);
2487 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2489 fputc (',', asm_out_file);
2490 dw2_asm_output_data_raw (2, offset);
2492 break;
2494 case DW_OP_regx:
2496 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2497 gcc_assert (size_of_uleb128 (r)
2498 == size_of_uleb128 (val1->v.val_unsigned));
2499 fputc (',', asm_out_file);
2500 dw2_asm_output_data_uleb128_raw (r);
2502 break;
2504 case DW_OP_constu:
2505 case DW_OP_plus_uconst:
2506 case DW_OP_piece:
2507 fputc (',', asm_out_file);
2508 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2509 break;
2511 case DW_OP_bit_piece:
2512 fputc (',', asm_out_file);
2513 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2514 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2515 break;
2517 case DW_OP_consts:
2518 case DW_OP_breg0:
2519 case DW_OP_breg1:
2520 case DW_OP_breg2:
2521 case DW_OP_breg3:
2522 case DW_OP_breg4:
2523 case DW_OP_breg5:
2524 case DW_OP_breg6:
2525 case DW_OP_breg7:
2526 case DW_OP_breg8:
2527 case DW_OP_breg9:
2528 case DW_OP_breg10:
2529 case DW_OP_breg11:
2530 case DW_OP_breg12:
2531 case DW_OP_breg13:
2532 case DW_OP_breg14:
2533 case DW_OP_breg15:
2534 case DW_OP_breg16:
2535 case DW_OP_breg17:
2536 case DW_OP_breg18:
2537 case DW_OP_breg19:
2538 case DW_OP_breg20:
2539 case DW_OP_breg21:
2540 case DW_OP_breg22:
2541 case DW_OP_breg23:
2542 case DW_OP_breg24:
2543 case DW_OP_breg25:
2544 case DW_OP_breg26:
2545 case DW_OP_breg27:
2546 case DW_OP_breg28:
2547 case DW_OP_breg29:
2548 case DW_OP_breg30:
2549 case DW_OP_breg31:
2550 case DW_OP_fbreg:
2551 fputc (',', asm_out_file);
2552 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2553 break;
2555 case DW_OP_bregx:
2557 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2558 gcc_assert (size_of_uleb128 (r)
2559 == size_of_uleb128 (val1->v.val_unsigned));
2560 fputc (',', asm_out_file);
2561 dw2_asm_output_data_uleb128_raw (r);
2562 fputc (',', asm_out_file);
2563 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2565 break;
2567 case DW_OP_implicit_pointer:
2568 case DW_OP_entry_value:
2569 case DW_OP_const_type:
2570 case DW_OP_regval_type:
2571 case DW_OP_deref_type:
2572 case DW_OP_convert:
2573 case DW_OP_reinterpret:
2574 case DW_OP_GNU_implicit_pointer:
2575 case DW_OP_GNU_entry_value:
2576 case DW_OP_GNU_const_type:
2577 case DW_OP_GNU_regval_type:
2578 case DW_OP_GNU_deref_type:
2579 case DW_OP_GNU_convert:
2580 case DW_OP_GNU_reinterpret:
2581 case DW_OP_GNU_parameter_ref:
2582 gcc_unreachable ();
2583 break;
2585 default:
2586 /* Other codes have no operands. */
2587 break;
2591 void
2592 output_loc_sequence_raw (dw_loc_descr_ref loc)
2594 while (1)
2596 enum dwarf_location_atom opc = loc->dw_loc_opc;
2597 /* Output the opcode. */
2598 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2600 unsigned r = (opc - DW_OP_breg0);
2601 r = DWARF2_FRAME_REG_OUT (r, 1);
2602 gcc_assert (r <= 31);
2603 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2605 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2607 unsigned r = (opc - DW_OP_reg0);
2608 r = DWARF2_FRAME_REG_OUT (r, 1);
2609 gcc_assert (r <= 31);
2610 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2612 /* Output the opcode. */
2613 fprintf (asm_out_file, "%#x", opc);
2614 output_loc_operands_raw (loc);
2616 if (!loc->dw_loc_next)
2617 break;
2618 loc = loc->dw_loc_next;
2620 fputc (',', asm_out_file);
2624 /* This function builds a dwarf location descriptor sequence from a
2625 dw_cfa_location, adding the given OFFSET to the result of the
2626 expression. */
2628 struct dw_loc_descr_node *
2629 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2631 struct dw_loc_descr_node *head, *tmp;
2633 offset += cfa->offset;
2635 if (cfa->indirect)
2637 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2638 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2639 head->dw_loc_oprnd1.val_entry = NULL;
2640 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2641 add_loc_descr (&head, tmp);
2642 if (offset != 0)
2644 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2645 add_loc_descr (&head, tmp);
2648 else
2649 head = new_reg_loc_descr (cfa->reg, offset);
2651 return head;
2654 /* This function builds a dwarf location descriptor sequence for
2655 the address at OFFSET from the CFA when stack is aligned to
2656 ALIGNMENT byte. */
2658 struct dw_loc_descr_node *
2659 build_cfa_aligned_loc (dw_cfa_location *cfa,
2660 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2662 struct dw_loc_descr_node *head;
2663 unsigned int dwarf_fp
2664 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2666 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2667 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2669 head = new_reg_loc_descr (dwarf_fp, 0);
2670 add_loc_descr (&head, int_loc_descriptor (alignment));
2671 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2672 loc_descr_plus_const (&head, offset);
2674 else
2675 head = new_reg_loc_descr (dwarf_fp, offset);
2676 return head;
2679 /* And now, the support for symbolic debugging information. */
2681 /* .debug_str support. */
2683 static void dwarf2out_init (const char *);
2684 static void dwarf2out_finish (const char *);
2685 static void dwarf2out_early_finish (const char *);
2686 static void dwarf2out_assembly_start (void);
2687 static void dwarf2out_define (unsigned int, const char *);
2688 static void dwarf2out_undef (unsigned int, const char *);
2689 static void dwarf2out_start_source_file (unsigned, const char *);
2690 static void dwarf2out_end_source_file (unsigned);
2691 static void dwarf2out_function_decl (tree);
2692 static void dwarf2out_begin_block (unsigned, unsigned);
2693 static void dwarf2out_end_block (unsigned, unsigned);
2694 static bool dwarf2out_ignore_block (const_tree);
2695 static void dwarf2out_early_global_decl (tree);
2696 static void dwarf2out_late_global_decl (tree);
2697 static void dwarf2out_type_decl (tree, int);
2698 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2699 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2700 dw_die_ref);
2701 static void dwarf2out_abstract_function (tree);
2702 static void dwarf2out_var_location (rtx_insn *);
2703 static void dwarf2out_size_function (tree);
2704 static void dwarf2out_begin_function (tree);
2705 static void dwarf2out_end_function (unsigned int);
2706 static void dwarf2out_register_main_translation_unit (tree unit);
2707 static void dwarf2out_set_name (tree, tree);
2708 static void dwarf2out_register_external_die (tree decl, const char *sym,
2709 unsigned HOST_WIDE_INT off);
2710 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2711 unsigned HOST_WIDE_INT *off);
2713 /* The debug hooks structure. */
2715 const struct gcc_debug_hooks dwarf2_debug_hooks =
2717 dwarf2out_init,
2718 dwarf2out_finish,
2719 dwarf2out_early_finish,
2720 dwarf2out_assembly_start,
2721 dwarf2out_define,
2722 dwarf2out_undef,
2723 dwarf2out_start_source_file,
2724 dwarf2out_end_source_file,
2725 dwarf2out_begin_block,
2726 dwarf2out_end_block,
2727 dwarf2out_ignore_block,
2728 dwarf2out_source_line,
2729 dwarf2out_begin_prologue,
2730 #if VMS_DEBUGGING_INFO
2731 dwarf2out_vms_end_prologue,
2732 dwarf2out_vms_begin_epilogue,
2733 #else
2734 debug_nothing_int_charstar,
2735 debug_nothing_int_charstar,
2736 #endif
2737 dwarf2out_end_epilogue,
2738 dwarf2out_begin_function,
2739 dwarf2out_end_function, /* end_function */
2740 dwarf2out_register_main_translation_unit,
2741 dwarf2out_function_decl, /* function_decl */
2742 dwarf2out_early_global_decl,
2743 dwarf2out_late_global_decl,
2744 dwarf2out_type_decl, /* type_decl */
2745 dwarf2out_imported_module_or_decl,
2746 dwarf2out_die_ref_for_decl,
2747 dwarf2out_register_external_die,
2748 debug_nothing_tree, /* deferred_inline_function */
2749 /* The DWARF 2 backend tries to reduce debugging bloat by not
2750 emitting the abstract description of inline functions until
2751 something tries to reference them. */
2752 dwarf2out_abstract_function, /* outlining_inline_function */
2753 debug_nothing_rtx_code_label, /* label */
2754 debug_nothing_int, /* handle_pch */
2755 dwarf2out_var_location,
2756 debug_nothing_tree, /* inline_entry */
2757 dwarf2out_size_function, /* size_function */
2758 dwarf2out_switch_text_section,
2759 dwarf2out_set_name,
2760 1, /* start_end_main_source_file */
2761 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2764 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2766 dwarf2out_init,
2767 debug_nothing_charstar,
2768 debug_nothing_charstar,
2769 dwarf2out_assembly_start,
2770 debug_nothing_int_charstar,
2771 debug_nothing_int_charstar,
2772 debug_nothing_int_charstar,
2773 debug_nothing_int,
2774 debug_nothing_int_int, /* begin_block */
2775 debug_nothing_int_int, /* end_block */
2776 debug_true_const_tree, /* ignore_block */
2777 dwarf2out_source_line, /* source_line */
2778 debug_nothing_int_int_charstar, /* begin_prologue */
2779 debug_nothing_int_charstar, /* end_prologue */
2780 debug_nothing_int_charstar, /* begin_epilogue */
2781 debug_nothing_int_charstar, /* end_epilogue */
2782 debug_nothing_tree, /* begin_function */
2783 debug_nothing_int, /* end_function */
2784 debug_nothing_tree, /* register_main_translation_unit */
2785 debug_nothing_tree, /* function_decl */
2786 debug_nothing_tree, /* early_global_decl */
2787 debug_nothing_tree, /* late_global_decl */
2788 debug_nothing_tree_int, /* type_decl */
2789 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2790 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2791 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2792 debug_nothing_tree, /* deferred_inline_function */
2793 debug_nothing_tree, /* outlining_inline_function */
2794 debug_nothing_rtx_code_label, /* label */
2795 debug_nothing_int, /* handle_pch */
2796 debug_nothing_rtx_insn, /* var_location */
2797 debug_nothing_tree, /* inline_entry */
2798 debug_nothing_tree, /* size_function */
2799 debug_nothing_void, /* switch_text_section */
2800 debug_nothing_tree_tree, /* set_name */
2801 0, /* start_end_main_source_file */
2802 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2805 /* NOTE: In the comments in this file, many references are made to
2806 "Debugging Information Entries". This term is abbreviated as `DIE'
2807 throughout the remainder of this file. */
2809 /* An internal representation of the DWARF output is built, and then
2810 walked to generate the DWARF debugging info. The walk of the internal
2811 representation is done after the entire program has been compiled.
2812 The types below are used to describe the internal representation. */
2814 /* Whether to put type DIEs into their own section .debug_types instead
2815 of making them part of the .debug_info section. Only supported for
2816 Dwarf V4 or higher and the user didn't disable them through
2817 -fno-debug-types-section. It is more efficient to put them in a
2818 separate comdat sections since the linker will then be able to
2819 remove duplicates. But not all tools support .debug_types sections
2820 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2821 it is DW_UT_type unit type in .debug_info section. */
2823 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2825 /* Various DIE's use offsets relative to the beginning of the
2826 .debug_info section to refer to each other. */
2828 typedef long int dw_offset;
2830 struct comdat_type_node;
2832 /* The entries in the line_info table more-or-less mirror the opcodes
2833 that are used in the real dwarf line table. Arrays of these entries
2834 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2835 supported. */
2837 enum dw_line_info_opcode {
2838 /* Emit DW_LNE_set_address; the operand is the label index. */
2839 LI_set_address,
2841 /* Emit a row to the matrix with the given line. This may be done
2842 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2843 special opcodes. */
2844 LI_set_line,
2846 /* Emit a DW_LNS_set_file. */
2847 LI_set_file,
2849 /* Emit a DW_LNS_set_column. */
2850 LI_set_column,
2852 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2853 LI_negate_stmt,
2855 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2856 LI_set_prologue_end,
2857 LI_set_epilogue_begin,
2859 /* Emit a DW_LNE_set_discriminator. */
2860 LI_set_discriminator
2863 typedef struct GTY(()) dw_line_info_struct {
2864 enum dw_line_info_opcode opcode;
2865 unsigned int val;
2866 } dw_line_info_entry;
2869 struct GTY(()) dw_line_info_table {
2870 /* The label that marks the end of this section. */
2871 const char *end_label;
2873 /* The values for the last row of the matrix, as collected in the table.
2874 These are used to minimize the changes to the next row. */
2875 unsigned int file_num;
2876 unsigned int line_num;
2877 unsigned int column_num;
2878 int discrim_num;
2879 bool is_stmt;
2880 bool in_use;
2882 vec<dw_line_info_entry, va_gc> *entries;
2886 /* Each DIE attribute has a field specifying the attribute kind,
2887 a link to the next attribute in the chain, and an attribute value.
2888 Attributes are typically linked below the DIE they modify. */
2890 typedef struct GTY(()) dw_attr_struct {
2891 enum dwarf_attribute dw_attr;
2892 dw_val_node dw_attr_val;
2894 dw_attr_node;
2897 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2898 The children of each node form a circular list linked by
2899 die_sib. die_child points to the node *before* the "first" child node. */
2901 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2902 union die_symbol_or_type_node
2904 const char * GTY ((tag ("0"))) die_symbol;
2905 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2907 GTY ((desc ("%0.comdat_type_p"))) die_id;
2908 vec<dw_attr_node, va_gc> *die_attr;
2909 dw_die_ref die_parent;
2910 dw_die_ref die_child;
2911 dw_die_ref die_sib;
2912 dw_die_ref die_definition; /* ref from a specification to its definition */
2913 dw_offset die_offset;
2914 unsigned long die_abbrev;
2915 int die_mark;
2916 unsigned int decl_id;
2917 enum dwarf_tag die_tag;
2918 /* Die is used and must not be pruned as unused. */
2919 BOOL_BITFIELD die_perennial_p : 1;
2920 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2921 /* For an external ref to die_symbol if die_offset contains an extra
2922 offset to that symbol. */
2923 BOOL_BITFIELD with_offset : 1;
2924 /* Whether this DIE was removed from the DIE tree, for example via
2925 prune_unused_types. We don't consider those present from the
2926 DIE lookup routines. */
2927 BOOL_BITFIELD removed : 1;
2928 /* Lots of spare bits. */
2930 die_node;
2932 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2933 static bool early_dwarf;
2934 static bool early_dwarf_finished;
2935 struct set_early_dwarf {
2936 bool saved;
2937 set_early_dwarf () : saved(early_dwarf)
2939 gcc_assert (! early_dwarf_finished);
2940 early_dwarf = true;
2942 ~set_early_dwarf () { early_dwarf = saved; }
2945 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2946 #define FOR_EACH_CHILD(die, c, expr) do { \
2947 c = die->die_child; \
2948 if (c) do { \
2949 c = c->die_sib; \
2950 expr; \
2951 } while (c != die->die_child); \
2952 } while (0)
2954 /* The pubname structure */
2956 typedef struct GTY(()) pubname_struct {
2957 dw_die_ref die;
2958 const char *name;
2960 pubname_entry;
2963 struct GTY(()) dw_ranges {
2964 const char *label;
2965 /* If this is positive, it's a block number, otherwise it's a
2966 bitwise-negated index into dw_ranges_by_label. */
2967 int num;
2968 /* Index for the range list for DW_FORM_rnglistx. */
2969 unsigned int idx : 31;
2970 /* True if this range might be possibly in a different section
2971 from previous entry. */
2972 unsigned int maybe_new_sec : 1;
2975 /* A structure to hold a macinfo entry. */
2977 typedef struct GTY(()) macinfo_struct {
2978 unsigned char code;
2979 unsigned HOST_WIDE_INT lineno;
2980 const char *info;
2982 macinfo_entry;
2985 struct GTY(()) dw_ranges_by_label {
2986 const char *begin;
2987 const char *end;
2990 /* The comdat type node structure. */
2991 struct GTY(()) comdat_type_node
2993 dw_die_ref root_die;
2994 dw_die_ref type_die;
2995 dw_die_ref skeleton_die;
2996 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2997 comdat_type_node *next;
3000 /* A list of DIEs for which we can't determine ancestry (parent_die
3001 field) just yet. Later in dwarf2out_finish we will fill in the
3002 missing bits. */
3003 typedef struct GTY(()) limbo_die_struct {
3004 dw_die_ref die;
3005 /* The tree for which this DIE was created. We use this to
3006 determine ancestry later. */
3007 tree created_for;
3008 struct limbo_die_struct *next;
3010 limbo_die_node;
3012 typedef struct skeleton_chain_struct
3014 dw_die_ref old_die;
3015 dw_die_ref new_die;
3016 struct skeleton_chain_struct *parent;
3018 skeleton_chain_node;
3020 /* Define a macro which returns nonzero for a TYPE_DECL which was
3021 implicitly generated for a type.
3023 Note that, unlike the C front-end (which generates a NULL named
3024 TYPE_DECL node for each complete tagged type, each array type,
3025 and each function type node created) the C++ front-end generates
3026 a _named_ TYPE_DECL node for each tagged type node created.
3027 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3028 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3029 front-end, but for each type, tagged or not. */
3031 #define TYPE_DECL_IS_STUB(decl) \
3032 (DECL_NAME (decl) == NULL_TREE \
3033 || (DECL_ARTIFICIAL (decl) \
3034 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3035 /* This is necessary for stub decls that \
3036 appear in nested inline functions. */ \
3037 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3038 && (decl_ultimate_origin (decl) \
3039 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3041 /* Information concerning the compilation unit's programming
3042 language, and compiler version. */
3044 /* Fixed size portion of the DWARF compilation unit header. */
3045 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3046 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3047 + (dwarf_version >= 5 ? 4 : 3))
3049 /* Fixed size portion of the DWARF comdat type unit header. */
3050 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3051 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3052 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3054 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3055 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3056 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3058 /* Fixed size portion of public names info. */
3059 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3061 /* Fixed size portion of the address range info. */
3062 #define DWARF_ARANGES_HEADER_SIZE \
3063 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3064 DWARF2_ADDR_SIZE * 2) \
3065 - DWARF_INITIAL_LENGTH_SIZE)
3067 /* Size of padding portion in the address range info. It must be
3068 aligned to twice the pointer size. */
3069 #define DWARF_ARANGES_PAD_SIZE \
3070 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3071 DWARF2_ADDR_SIZE * 2) \
3072 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3074 /* Use assembler line directives if available. */
3075 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3076 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3077 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3078 #else
3079 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3080 #endif
3081 #endif
3083 /* Minimum line offset in a special line info. opcode.
3084 This value was chosen to give a reasonable range of values. */
3085 #define DWARF_LINE_BASE -10
3087 /* First special line opcode - leave room for the standard opcodes. */
3088 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3090 /* Range of line offsets in a special line info. opcode. */
3091 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3093 /* Flag that indicates the initial value of the is_stmt_start flag.
3094 In the present implementation, we do not mark any lines as
3095 the beginning of a source statement, because that information
3096 is not made available by the GCC front-end. */
3097 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3099 /* Maximum number of operations per instruction bundle. */
3100 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3101 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3102 #endif
3104 /* This location is used by calc_die_sizes() to keep track
3105 the offset of each DIE within the .debug_info section. */
3106 static unsigned long next_die_offset;
3108 /* Record the root of the DIE's built for the current compilation unit. */
3109 static GTY(()) dw_die_ref single_comp_unit_die;
3111 /* A list of type DIEs that have been separated into comdat sections. */
3112 static GTY(()) comdat_type_node *comdat_type_list;
3114 /* A list of CU DIEs that have been separated. */
3115 static GTY(()) limbo_die_node *cu_die_list;
3117 /* A list of DIEs with a NULL parent waiting to be relocated. */
3118 static GTY(()) limbo_die_node *limbo_die_list;
3120 /* A list of DIEs for which we may have to generate
3121 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3122 static GTY(()) limbo_die_node *deferred_asm_name;
3124 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3126 typedef const char *compare_type;
3128 static hashval_t hash (dwarf_file_data *);
3129 static bool equal (dwarf_file_data *, const char *);
3132 /* Filenames referenced by this compilation unit. */
3133 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3135 struct decl_die_hasher : ggc_ptr_hash<die_node>
3137 typedef tree compare_type;
3139 static hashval_t hash (die_node *);
3140 static bool equal (die_node *, tree);
3142 /* A hash table of references to DIE's that describe declarations.
3143 The key is a DECL_UID() which is a unique number identifying each decl. */
3144 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3146 struct GTY ((for_user)) variable_value_struct {
3147 unsigned int decl_id;
3148 vec<dw_die_ref, va_gc> *dies;
3151 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3153 typedef tree compare_type;
3155 static hashval_t hash (variable_value_struct *);
3156 static bool equal (variable_value_struct *, tree);
3158 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3159 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3160 DECL_CONTEXT of the referenced VAR_DECLs. */
3161 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3163 struct block_die_hasher : ggc_ptr_hash<die_struct>
3165 static hashval_t hash (die_struct *);
3166 static bool equal (die_struct *, die_struct *);
3169 /* A hash table of references to DIE's that describe COMMON blocks.
3170 The key is DECL_UID() ^ die_parent. */
3171 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3173 typedef struct GTY(()) die_arg_entry_struct {
3174 dw_die_ref die;
3175 tree arg;
3176 } die_arg_entry;
3179 /* Node of the variable location list. */
3180 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3181 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3182 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3183 in mode of the EXPR_LIST node and first EXPR_LIST operand
3184 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3185 location or NULL for padding. For larger bitsizes,
3186 mode is 0 and first operand is a CONCAT with bitsize
3187 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3188 NULL as second operand. */
3189 rtx GTY (()) loc;
3190 const char * GTY (()) label;
3191 struct var_loc_node * GTY (()) next;
3194 /* Variable location list. */
3195 struct GTY ((for_user)) var_loc_list_def {
3196 struct var_loc_node * GTY (()) first;
3198 /* Pointer to the last but one or last element of the
3199 chained list. If the list is empty, both first and
3200 last are NULL, if the list contains just one node
3201 or the last node certainly is not redundant, it points
3202 to the last node, otherwise points to the last but one.
3203 Do not mark it for GC because it is marked through the chain. */
3204 struct var_loc_node * GTY ((skip ("%h"))) last;
3206 /* Pointer to the last element before section switch,
3207 if NULL, either sections weren't switched or first
3208 is after section switch. */
3209 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3211 /* DECL_UID of the variable decl. */
3212 unsigned int decl_id;
3214 typedef struct var_loc_list_def var_loc_list;
3216 /* Call argument location list. */
3217 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3218 rtx GTY (()) call_arg_loc_note;
3219 const char * GTY (()) label;
3220 tree GTY (()) block;
3221 bool tail_call_p;
3222 rtx GTY (()) symbol_ref;
3223 struct call_arg_loc_node * GTY (()) next;
3227 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3229 typedef const_tree compare_type;
3231 static hashval_t hash (var_loc_list *);
3232 static bool equal (var_loc_list *, const_tree);
3235 /* Table of decl location linked lists. */
3236 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3238 /* Head and tail of call_arg_loc chain. */
3239 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3240 static struct call_arg_loc_node *call_arg_loc_last;
3242 /* Number of call sites in the current function. */
3243 static int call_site_count = -1;
3244 /* Number of tail call sites in the current function. */
3245 static int tail_call_site_count = -1;
3247 /* A cached location list. */
3248 struct GTY ((for_user)) cached_dw_loc_list_def {
3249 /* The DECL_UID of the decl that this entry describes. */
3250 unsigned int decl_id;
3252 /* The cached location list. */
3253 dw_loc_list_ref loc_list;
3255 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3257 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3260 typedef const_tree compare_type;
3262 static hashval_t hash (cached_dw_loc_list *);
3263 static bool equal (cached_dw_loc_list *, const_tree);
3266 /* Table of cached location lists. */
3267 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3269 /* A vector of references to DIE's that are uniquely identified by their tag,
3270 presence/absence of children DIE's, and list of attribute/value pairs. */
3271 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3273 /* A hash map to remember the stack usage for DWARF procedures. The value
3274 stored is the stack size difference between before the DWARF procedure
3275 invokation and after it returned. In other words, for a DWARF procedure
3276 that consumes N stack slots and that pushes M ones, this stores M - N. */
3277 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3279 /* A global counter for generating labels for line number data. */
3280 static unsigned int line_info_label_num;
3282 /* The current table to which we should emit line number information
3283 for the current function. This will be set up at the beginning of
3284 assembly for the function. */
3285 static GTY(()) dw_line_info_table *cur_line_info_table;
3287 /* The two default tables of line number info. */
3288 static GTY(()) dw_line_info_table *text_section_line_info;
3289 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3291 /* The set of all non-default tables of line number info. */
3292 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3294 /* A flag to tell pubnames/types export if there is an info section to
3295 refer to. */
3296 static bool info_section_emitted;
3298 /* A pointer to the base of a table that contains a list of publicly
3299 accessible names. */
3300 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3302 /* A pointer to the base of a table that contains a list of publicly
3303 accessible types. */
3304 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3306 /* A pointer to the base of a table that contains a list of macro
3307 defines/undefines (and file start/end markers). */
3308 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3310 /* True if .debug_macinfo or .debug_macros section is going to be
3311 emitted. */
3312 #define have_macinfo \
3313 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3314 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3315 && !macinfo_table->is_empty ())
3317 /* Vector of dies for which we should generate .debug_ranges info. */
3318 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3320 /* Vector of pairs of labels referenced in ranges_table. */
3321 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3323 /* Whether we have location lists that need outputting */
3324 static GTY(()) bool have_location_lists;
3326 /* Unique label counter. */
3327 static GTY(()) unsigned int loclabel_num;
3329 /* Unique label counter for point-of-call tables. */
3330 static GTY(()) unsigned int poc_label_num;
3332 /* The last file entry emitted by maybe_emit_file(). */
3333 static GTY(()) struct dwarf_file_data * last_emitted_file;
3335 /* Number of internal labels generated by gen_internal_sym(). */
3336 static GTY(()) int label_num;
3338 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3340 /* Instances of generic types for which we need to generate debug
3341 info that describe their generic parameters and arguments. That
3342 generation needs to happen once all types are properly laid out so
3343 we do it at the end of compilation. */
3344 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3346 /* Offset from the "steady-state frame pointer" to the frame base,
3347 within the current function. */
3348 static HOST_WIDE_INT frame_pointer_fb_offset;
3349 static bool frame_pointer_fb_offset_valid;
3351 static vec<dw_die_ref> base_types;
3353 /* Flags to represent a set of attribute classes for attributes that represent
3354 a scalar value (bounds, pointers, ...). */
3355 enum dw_scalar_form
3357 dw_scalar_form_constant = 0x01,
3358 dw_scalar_form_exprloc = 0x02,
3359 dw_scalar_form_reference = 0x04
3362 /* Forward declarations for functions defined in this file. */
3364 static int is_pseudo_reg (const_rtx);
3365 static tree type_main_variant (tree);
3366 static int is_tagged_type (const_tree);
3367 static const char *dwarf_tag_name (unsigned);
3368 static const char *dwarf_attr_name (unsigned);
3369 static const char *dwarf_form_name (unsigned);
3370 static tree decl_ultimate_origin (const_tree);
3371 static tree decl_class_context (tree);
3372 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3373 static inline enum dw_val_class AT_class (dw_attr_node *);
3374 static inline unsigned int AT_index (dw_attr_node *);
3375 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3376 static inline unsigned AT_flag (dw_attr_node *);
3377 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3378 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3379 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3380 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3381 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3382 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3383 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3384 unsigned int, unsigned char *);
3385 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3386 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3387 static inline const char *AT_string (dw_attr_node *);
3388 static enum dwarf_form AT_string_form (dw_attr_node *);
3389 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3390 static void add_AT_specification (dw_die_ref, dw_die_ref);
3391 static inline dw_die_ref AT_ref (dw_attr_node *);
3392 static inline int AT_ref_external (dw_attr_node *);
3393 static inline void set_AT_ref_external (dw_attr_node *, int);
3394 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3395 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3396 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3397 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3398 dw_loc_list_ref);
3399 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3400 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3401 static void remove_addr_table_entry (addr_table_entry *);
3402 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3403 static inline rtx AT_addr (dw_attr_node *);
3404 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3405 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3406 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3407 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3408 const char *);
3409 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3410 unsigned HOST_WIDE_INT);
3411 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3412 unsigned long, bool);
3413 static inline const char *AT_lbl (dw_attr_node *);
3414 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3415 static const char *get_AT_low_pc (dw_die_ref);
3416 static const char *get_AT_hi_pc (dw_die_ref);
3417 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3418 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3419 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3420 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3421 static bool is_cxx (void);
3422 static bool is_cxx (const_tree);
3423 static bool is_fortran (void);
3424 static bool is_ada (void);
3425 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3426 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3427 static void add_child_die (dw_die_ref, dw_die_ref);
3428 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3429 static dw_die_ref lookup_type_die (tree);
3430 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3431 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3432 static void equate_type_number_to_die (tree, dw_die_ref);
3433 static dw_die_ref lookup_decl_die (tree);
3434 static var_loc_list *lookup_decl_loc (const_tree);
3435 static void equate_decl_number_to_die (tree, dw_die_ref);
3436 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3437 static void print_spaces (FILE *);
3438 static void print_die (dw_die_ref, FILE *);
3439 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3440 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3441 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3442 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3443 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3444 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3445 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3446 struct md5_ctx *, int *);
3447 struct checksum_attributes;
3448 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3449 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3450 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3451 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3452 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3453 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3454 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3455 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3456 static int is_type_die (dw_die_ref);
3457 static int is_comdat_die (dw_die_ref);
3458 static inline bool is_template_instantiation (dw_die_ref);
3459 static int is_declaration_die (dw_die_ref);
3460 static int should_move_die_to_comdat (dw_die_ref);
3461 static dw_die_ref clone_as_declaration (dw_die_ref);
3462 static dw_die_ref clone_die (dw_die_ref);
3463 static dw_die_ref clone_tree (dw_die_ref);
3464 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3465 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3466 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3467 static dw_die_ref generate_skeleton (dw_die_ref);
3468 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3469 dw_die_ref,
3470 dw_die_ref);
3471 static void break_out_comdat_types (dw_die_ref);
3472 static void copy_decls_for_unworthy_types (dw_die_ref);
3474 static void add_sibling_attributes (dw_die_ref);
3475 static void output_location_lists (dw_die_ref);
3476 static int constant_size (unsigned HOST_WIDE_INT);
3477 static unsigned long size_of_die (dw_die_ref);
3478 static void calc_die_sizes (dw_die_ref);
3479 static void calc_base_type_die_sizes (void);
3480 static void mark_dies (dw_die_ref);
3481 static void unmark_dies (dw_die_ref);
3482 static void unmark_all_dies (dw_die_ref);
3483 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3484 static unsigned long size_of_aranges (void);
3485 static enum dwarf_form value_format (dw_attr_node *);
3486 static void output_value_format (dw_attr_node *);
3487 static void output_abbrev_section (void);
3488 static void output_die_abbrevs (unsigned long, dw_die_ref);
3489 static void output_die (dw_die_ref);
3490 static void output_compilation_unit_header (enum dwarf_unit_type);
3491 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3492 static void output_comdat_type_unit (comdat_type_node *);
3493 static const char *dwarf2_name (tree, int);
3494 static void add_pubname (tree, dw_die_ref);
3495 static void add_enumerator_pubname (const char *, dw_die_ref);
3496 static void add_pubname_string (const char *, dw_die_ref);
3497 static void add_pubtype (tree, dw_die_ref);
3498 static void output_pubnames (vec<pubname_entry, va_gc> *);
3499 static void output_aranges (void);
3500 static unsigned int add_ranges (const_tree, bool = false);
3501 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3502 bool *, bool);
3503 static void output_ranges (void);
3504 static dw_line_info_table *new_line_info_table (void);
3505 static void output_line_info (bool);
3506 static void output_file_names (void);
3507 static dw_die_ref base_type_die (tree, bool);
3508 static int is_base_type (tree);
3509 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3510 static int decl_quals (const_tree);
3511 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3512 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3513 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3514 static int type_is_enum (const_tree);
3515 static unsigned int dbx_reg_number (const_rtx);
3516 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3517 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3518 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3519 enum var_init_status);
3520 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3521 enum var_init_status);
3522 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3523 enum var_init_status);
3524 static int is_based_loc (const_rtx);
3525 static bool resolve_one_addr (rtx *);
3526 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3527 enum var_init_status);
3528 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3529 enum var_init_status);
3530 struct loc_descr_context;
3531 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3532 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3533 static dw_loc_list_ref loc_list_from_tree (tree, int,
3534 struct loc_descr_context *);
3535 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3536 struct loc_descr_context *);
3537 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3538 static tree field_type (const_tree);
3539 static unsigned int simple_type_align_in_bits (const_tree);
3540 static unsigned int simple_decl_align_in_bits (const_tree);
3541 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3542 struct vlr_context;
3543 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3544 HOST_WIDE_INT *);
3545 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3546 dw_loc_list_ref);
3547 static void add_data_member_location_attribute (dw_die_ref, tree,
3548 struct vlr_context *);
3549 static bool add_const_value_attribute (dw_die_ref, rtx);
3550 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3551 static void insert_wide_int (const wide_int &, unsigned char *, int);
3552 static void insert_float (const_rtx, unsigned char *);
3553 static rtx rtl_for_decl_location (tree);
3554 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3555 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3556 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3557 static void add_name_attribute (dw_die_ref, const char *);
3558 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3559 static void add_comp_dir_attribute (dw_die_ref);
3560 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3561 struct loc_descr_context *);
3562 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3563 struct loc_descr_context *);
3564 static void add_subscript_info (dw_die_ref, tree, bool);
3565 static void add_byte_size_attribute (dw_die_ref, tree);
3566 static void add_alignment_attribute (dw_die_ref, tree);
3567 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3568 struct vlr_context *);
3569 static void add_bit_size_attribute (dw_die_ref, tree);
3570 static void add_prototyped_attribute (dw_die_ref, tree);
3571 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3572 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3573 static void add_src_coords_attributes (dw_die_ref, tree);
3574 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3575 static void add_discr_value (dw_die_ref, dw_discr_value *);
3576 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3577 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3578 static void push_decl_scope (tree);
3579 static void pop_decl_scope (void);
3580 static dw_die_ref scope_die_for (tree, dw_die_ref);
3581 static inline int local_scope_p (dw_die_ref);
3582 static inline int class_scope_p (dw_die_ref);
3583 static inline int class_or_namespace_scope_p (dw_die_ref);
3584 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3585 static void add_calling_convention_attribute (dw_die_ref, tree);
3586 static const char *type_tag (const_tree);
3587 static tree member_declared_type (const_tree);
3588 #if 0
3589 static const char *decl_start_label (tree);
3590 #endif
3591 static void gen_array_type_die (tree, dw_die_ref);
3592 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3593 #if 0
3594 static void gen_entry_point_die (tree, dw_die_ref);
3595 #endif
3596 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3597 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3598 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3599 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3600 static void gen_formal_types_die (tree, dw_die_ref);
3601 static void gen_subprogram_die (tree, dw_die_ref);
3602 static void gen_variable_die (tree, tree, dw_die_ref);
3603 static void gen_const_die (tree, dw_die_ref);
3604 static void gen_label_die (tree, dw_die_ref);
3605 static void gen_lexical_block_die (tree, dw_die_ref);
3606 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3607 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3608 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3609 static dw_die_ref gen_compile_unit_die (const char *);
3610 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3611 static void gen_member_die (tree, dw_die_ref);
3612 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3613 enum debug_info_usage);
3614 static void gen_subroutine_type_die (tree, dw_die_ref);
3615 static void gen_typedef_die (tree, dw_die_ref);
3616 static void gen_type_die (tree, dw_die_ref);
3617 static void gen_block_die (tree, dw_die_ref);
3618 static void decls_for_scope (tree, dw_die_ref);
3619 static bool is_naming_typedef_decl (const_tree);
3620 static inline dw_die_ref get_context_die (tree);
3621 static void gen_namespace_die (tree, dw_die_ref);
3622 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3623 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3624 static dw_die_ref force_decl_die (tree);
3625 static dw_die_ref force_type_die (tree);
3626 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3627 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3628 static struct dwarf_file_data * lookup_filename (const char *);
3629 static void retry_incomplete_types (void);
3630 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3631 static void gen_generic_params_dies (tree);
3632 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3633 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3634 static void splice_child_die (dw_die_ref, dw_die_ref);
3635 static int file_info_cmp (const void *, const void *);
3636 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3637 const char *, const char *);
3638 static void output_loc_list (dw_loc_list_ref);
3639 static char *gen_internal_sym (const char *);
3640 static bool want_pubnames (void);
3642 static void prune_unmark_dies (dw_die_ref);
3643 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3644 static void prune_unused_types_mark (dw_die_ref, int);
3645 static void prune_unused_types_walk (dw_die_ref);
3646 static void prune_unused_types_walk_attribs (dw_die_ref);
3647 static void prune_unused_types_prune (dw_die_ref);
3648 static void prune_unused_types (void);
3649 static int maybe_emit_file (struct dwarf_file_data *fd);
3650 static inline const char *AT_vms_delta1 (dw_attr_node *);
3651 static inline const char *AT_vms_delta2 (dw_attr_node *);
3652 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3653 const char *, const char *);
3654 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3655 static void gen_remaining_tmpl_value_param_die_attribute (void);
3656 static bool generic_type_p (tree);
3657 static void schedule_generic_params_dies_gen (tree t);
3658 static void gen_scheduled_generic_parms_dies (void);
3659 static void resolve_variable_values (void);
3661 static const char *comp_dir_string (void);
3663 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3665 /* enum for tracking thread-local variables whose address is really an offset
3666 relative to the TLS pointer, which will need link-time relocation, but will
3667 not need relocation by the DWARF consumer. */
3669 enum dtprel_bool
3671 dtprel_false = 0,
3672 dtprel_true = 1
3675 /* Return the operator to use for an address of a variable. For dtprel_true, we
3676 use DW_OP_const*. For regular variables, which need both link-time
3677 relocation and consumer-level relocation (e.g., to account for shared objects
3678 loaded at a random address), we use DW_OP_addr*. */
3680 static inline enum dwarf_location_atom
3681 dw_addr_op (enum dtprel_bool dtprel)
3683 if (dtprel == dtprel_true)
3684 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3685 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3686 else
3687 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3690 /* Return a pointer to a newly allocated address location description. If
3691 dwarf_split_debug_info is true, then record the address with the appropriate
3692 relocation. */
3693 static inline dw_loc_descr_ref
3694 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3696 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3698 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3699 ref->dw_loc_oprnd1.v.val_addr = addr;
3700 ref->dtprel = dtprel;
3701 if (dwarf_split_debug_info)
3702 ref->dw_loc_oprnd1.val_entry
3703 = add_addr_table_entry (addr,
3704 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3705 else
3706 ref->dw_loc_oprnd1.val_entry = NULL;
3708 return ref;
3711 /* Section names used to hold DWARF debugging information. */
3713 #ifndef DEBUG_INFO_SECTION
3714 #define DEBUG_INFO_SECTION ".debug_info"
3715 #endif
3716 #ifndef DEBUG_DWO_INFO_SECTION
3717 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3718 #endif
3719 #ifndef DEBUG_LTO_INFO_SECTION
3720 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3721 #endif
3722 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3723 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3724 #endif
3725 #ifndef DEBUG_ABBREV_SECTION
3726 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3727 #endif
3728 #ifndef DEBUG_LTO_ABBREV_SECTION
3729 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3730 #endif
3731 #ifndef DEBUG_DWO_ABBREV_SECTION
3732 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3733 #endif
3734 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3735 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3736 #endif
3737 #ifndef DEBUG_ARANGES_SECTION
3738 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3739 #endif
3740 #ifndef DEBUG_ADDR_SECTION
3741 #define DEBUG_ADDR_SECTION ".debug_addr"
3742 #endif
3743 #ifndef DEBUG_MACINFO_SECTION
3744 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3745 #endif
3746 #ifndef DEBUG_LTO_MACINFO_SECTION
3747 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3748 #endif
3749 #ifndef DEBUG_DWO_MACINFO_SECTION
3750 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3751 #endif
3752 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3753 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3754 #endif
3755 #ifndef DEBUG_MACRO_SECTION
3756 #define DEBUG_MACRO_SECTION ".debug_macro"
3757 #endif
3758 #ifndef DEBUG_LTO_MACRO_SECTION
3759 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3760 #endif
3761 #ifndef DEBUG_DWO_MACRO_SECTION
3762 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3763 #endif
3764 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3765 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3766 #endif
3767 #ifndef DEBUG_LINE_SECTION
3768 #define DEBUG_LINE_SECTION ".debug_line"
3769 #endif
3770 #ifndef DEBUG_LTO_LINE_SECTION
3771 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3772 #endif
3773 #ifndef DEBUG_DWO_LINE_SECTION
3774 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3775 #endif
3776 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3777 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3778 #endif
3779 #ifndef DEBUG_LOC_SECTION
3780 #define DEBUG_LOC_SECTION ".debug_loc"
3781 #endif
3782 #ifndef DEBUG_DWO_LOC_SECTION
3783 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3784 #endif
3785 #ifndef DEBUG_LOCLISTS_SECTION
3786 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3787 #endif
3788 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3789 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3790 #endif
3791 #ifndef DEBUG_PUBNAMES_SECTION
3792 #define DEBUG_PUBNAMES_SECTION \
3793 ((debug_generate_pub_sections == 2) \
3794 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3795 #endif
3796 #ifndef DEBUG_PUBTYPES_SECTION
3797 #define DEBUG_PUBTYPES_SECTION \
3798 ((debug_generate_pub_sections == 2) \
3799 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3800 #endif
3801 #ifndef DEBUG_STR_OFFSETS_SECTION
3802 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3803 #endif
3804 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3805 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3806 #endif
3807 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3808 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3809 #endif
3810 #ifndef DEBUG_STR_SECTION
3811 #define DEBUG_STR_SECTION ".debug_str"
3812 #endif
3813 #ifndef DEBUG_LTO_STR_SECTION
3814 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3815 #endif
3816 #ifndef DEBUG_STR_DWO_SECTION
3817 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3818 #endif
3819 #ifndef DEBUG_LTO_STR_DWO_SECTION
3820 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3821 #endif
3822 #ifndef DEBUG_RANGES_SECTION
3823 #define DEBUG_RANGES_SECTION ".debug_ranges"
3824 #endif
3825 #ifndef DEBUG_RNGLISTS_SECTION
3826 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3827 #endif
3828 #ifndef DEBUG_LINE_STR_SECTION
3829 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3830 #endif
3831 #ifndef DEBUG_LTO_LINE_STR_SECTION
3832 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3833 #endif
3835 /* Standard ELF section names for compiled code and data. */
3836 #ifndef TEXT_SECTION_NAME
3837 #define TEXT_SECTION_NAME ".text"
3838 #endif
3840 /* Section flags for .debug_str section. */
3841 #define DEBUG_STR_SECTION_FLAGS \
3842 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3843 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3844 : SECTION_DEBUG)
3846 /* Section flags for .debug_str.dwo section. */
3847 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3849 /* Attribute used to refer to the macro section. */
3850 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3851 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3853 /* Labels we insert at beginning sections we can reference instead of
3854 the section names themselves. */
3856 #ifndef TEXT_SECTION_LABEL
3857 #define TEXT_SECTION_LABEL "Ltext"
3858 #endif
3859 #ifndef COLD_TEXT_SECTION_LABEL
3860 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3861 #endif
3862 #ifndef DEBUG_LINE_SECTION_LABEL
3863 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3864 #endif
3865 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3866 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3867 #endif
3868 #ifndef DEBUG_INFO_SECTION_LABEL
3869 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3870 #endif
3871 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3872 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3873 #endif
3874 #ifndef DEBUG_ABBREV_SECTION_LABEL
3875 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3876 #endif
3877 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3878 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3879 #endif
3880 #ifndef DEBUG_ADDR_SECTION_LABEL
3881 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3882 #endif
3883 #ifndef DEBUG_LOC_SECTION_LABEL
3884 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3885 #endif
3886 #ifndef DEBUG_RANGES_SECTION_LABEL
3887 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3888 #endif
3889 #ifndef DEBUG_MACINFO_SECTION_LABEL
3890 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3891 #endif
3892 #ifndef DEBUG_MACRO_SECTION_LABEL
3893 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3894 #endif
3895 #define SKELETON_COMP_DIE_ABBREV 1
3896 #define SKELETON_TYPE_DIE_ABBREV 2
3898 /* Definitions of defaults for formats and names of various special
3899 (artificial) labels which may be generated within this file (when the -g
3900 options is used and DWARF2_DEBUGGING_INFO is in effect.
3901 If necessary, these may be overridden from within the tm.h file, but
3902 typically, overriding these defaults is unnecessary. */
3904 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3905 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3906 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3907 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3908 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3909 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3910 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3911 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3912 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3913 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3914 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3915 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3916 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3917 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3918 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3920 #ifndef TEXT_END_LABEL
3921 #define TEXT_END_LABEL "Letext"
3922 #endif
3923 #ifndef COLD_END_LABEL
3924 #define COLD_END_LABEL "Letext_cold"
3925 #endif
3926 #ifndef BLOCK_BEGIN_LABEL
3927 #define BLOCK_BEGIN_LABEL "LBB"
3928 #endif
3929 #ifndef BLOCK_END_LABEL
3930 #define BLOCK_END_LABEL "LBE"
3931 #endif
3932 #ifndef LINE_CODE_LABEL
3933 #define LINE_CODE_LABEL "LM"
3934 #endif
3937 /* Return the root of the DIE's built for the current compilation unit. */
3938 static dw_die_ref
3939 comp_unit_die (void)
3941 if (!single_comp_unit_die)
3942 single_comp_unit_die = gen_compile_unit_die (NULL);
3943 return single_comp_unit_die;
3946 /* We allow a language front-end to designate a function that is to be
3947 called to "demangle" any name before it is put into a DIE. */
3949 static const char *(*demangle_name_func) (const char *);
3951 void
3952 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3954 demangle_name_func = func;
3957 /* Test if rtl node points to a pseudo register. */
3959 static inline int
3960 is_pseudo_reg (const_rtx rtl)
3962 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3963 || (GET_CODE (rtl) == SUBREG
3964 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3967 /* Return a reference to a type, with its const and volatile qualifiers
3968 removed. */
3970 static inline tree
3971 type_main_variant (tree type)
3973 type = TYPE_MAIN_VARIANT (type);
3975 /* ??? There really should be only one main variant among any group of
3976 variants of a given type (and all of the MAIN_VARIANT values for all
3977 members of the group should point to that one type) but sometimes the C
3978 front-end messes this up for array types, so we work around that bug
3979 here. */
3980 if (TREE_CODE (type) == ARRAY_TYPE)
3981 while (type != TYPE_MAIN_VARIANT (type))
3982 type = TYPE_MAIN_VARIANT (type);
3984 return type;
3987 /* Return nonzero if the given type node represents a tagged type. */
3989 static inline int
3990 is_tagged_type (const_tree type)
3992 enum tree_code code = TREE_CODE (type);
3994 return (code == RECORD_TYPE || code == UNION_TYPE
3995 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3998 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4000 static void
4001 get_ref_die_offset_label (char *label, dw_die_ref ref)
4003 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4006 /* Return die_offset of a DIE reference to a base type. */
4008 static unsigned long int
4009 get_base_type_offset (dw_die_ref ref)
4011 if (ref->die_offset)
4012 return ref->die_offset;
4013 if (comp_unit_die ()->die_abbrev)
4015 calc_base_type_die_sizes ();
4016 gcc_assert (ref->die_offset);
4018 return ref->die_offset;
4021 /* Return die_offset of a DIE reference other than base type. */
4023 static unsigned long int
4024 get_ref_die_offset (dw_die_ref ref)
4026 gcc_assert (ref->die_offset);
4027 return ref->die_offset;
4030 /* Convert a DIE tag into its string name. */
4032 static const char *
4033 dwarf_tag_name (unsigned int tag)
4035 const char *name = get_DW_TAG_name (tag);
4037 if (name != NULL)
4038 return name;
4040 return "DW_TAG_<unknown>";
4043 /* Convert a DWARF attribute code into its string name. */
4045 static const char *
4046 dwarf_attr_name (unsigned int attr)
4048 const char *name;
4050 switch (attr)
4052 #if VMS_DEBUGGING_INFO
4053 case DW_AT_HP_prologue:
4054 return "DW_AT_HP_prologue";
4055 #else
4056 case DW_AT_MIPS_loop_unroll_factor:
4057 return "DW_AT_MIPS_loop_unroll_factor";
4058 #endif
4060 #if VMS_DEBUGGING_INFO
4061 case DW_AT_HP_epilogue:
4062 return "DW_AT_HP_epilogue";
4063 #else
4064 case DW_AT_MIPS_stride:
4065 return "DW_AT_MIPS_stride";
4066 #endif
4069 name = get_DW_AT_name (attr);
4071 if (name != NULL)
4072 return name;
4074 return "DW_AT_<unknown>";
4077 /* Convert a DWARF value form code into its string name. */
4079 static const char *
4080 dwarf_form_name (unsigned int form)
4082 const char *name = get_DW_FORM_name (form);
4084 if (name != NULL)
4085 return name;
4087 return "DW_FORM_<unknown>";
4090 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4091 instance of an inlined instance of a decl which is local to an inline
4092 function, so we have to trace all of the way back through the origin chain
4093 to find out what sort of node actually served as the original seed for the
4094 given block. */
4096 static tree
4097 decl_ultimate_origin (const_tree decl)
4099 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4100 return NULL_TREE;
4102 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4103 we're trying to output the abstract instance of this function. */
4104 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4105 return NULL_TREE;
4107 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4108 most distant ancestor, this should never happen. */
4109 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4111 return DECL_ABSTRACT_ORIGIN (decl);
4114 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4115 of a virtual function may refer to a base class, so we check the 'this'
4116 parameter. */
4118 static tree
4119 decl_class_context (tree decl)
4121 tree context = NULL_TREE;
4123 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4124 context = DECL_CONTEXT (decl);
4125 else
4126 context = TYPE_MAIN_VARIANT
4127 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4129 if (context && !TYPE_P (context))
4130 context = NULL_TREE;
4132 return context;
4135 /* Add an attribute/value pair to a DIE. */
4137 static inline void
4138 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4140 /* Maybe this should be an assert? */
4141 if (die == NULL)
4142 return;
4144 if (flag_checking)
4146 /* Check we do not add duplicate attrs. Can't use get_AT here
4147 because that recurses to the specification/abstract origin DIE. */
4148 dw_attr_node *a;
4149 unsigned ix;
4150 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4151 gcc_assert (a->dw_attr != attr->dw_attr);
4154 vec_safe_reserve (die->die_attr, 1);
4155 vec_safe_push (die->die_attr, *attr);
4158 static inline enum dw_val_class
4159 AT_class (dw_attr_node *a)
4161 return a->dw_attr_val.val_class;
4164 /* Return the index for any attribute that will be referenced with a
4165 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4166 are stored in dw_attr_val.v.val_str for reference counting
4167 pruning. */
4169 static inline unsigned int
4170 AT_index (dw_attr_node *a)
4172 if (AT_class (a) == dw_val_class_str)
4173 return a->dw_attr_val.v.val_str->index;
4174 else if (a->dw_attr_val.val_entry != NULL)
4175 return a->dw_attr_val.val_entry->index;
4176 return NOT_INDEXED;
4179 /* Add a flag value attribute to a DIE. */
4181 static inline void
4182 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4184 dw_attr_node attr;
4186 attr.dw_attr = attr_kind;
4187 attr.dw_attr_val.val_class = dw_val_class_flag;
4188 attr.dw_attr_val.val_entry = NULL;
4189 attr.dw_attr_val.v.val_flag = flag;
4190 add_dwarf_attr (die, &attr);
4193 static inline unsigned
4194 AT_flag (dw_attr_node *a)
4196 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4197 return a->dw_attr_val.v.val_flag;
4200 /* Add a signed integer attribute value to a DIE. */
4202 static inline void
4203 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4205 dw_attr_node attr;
4207 attr.dw_attr = attr_kind;
4208 attr.dw_attr_val.val_class = dw_val_class_const;
4209 attr.dw_attr_val.val_entry = NULL;
4210 attr.dw_attr_val.v.val_int = int_val;
4211 add_dwarf_attr (die, &attr);
4214 static inline HOST_WIDE_INT
4215 AT_int (dw_attr_node *a)
4217 gcc_assert (a && (AT_class (a) == dw_val_class_const
4218 || AT_class (a) == dw_val_class_const_implicit));
4219 return a->dw_attr_val.v.val_int;
4222 /* Add an unsigned integer attribute value to a DIE. */
4224 static inline void
4225 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4226 unsigned HOST_WIDE_INT unsigned_val)
4228 dw_attr_node attr;
4230 attr.dw_attr = attr_kind;
4231 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4232 attr.dw_attr_val.val_entry = NULL;
4233 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4234 add_dwarf_attr (die, &attr);
4237 static inline unsigned HOST_WIDE_INT
4238 AT_unsigned (dw_attr_node *a)
4240 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4241 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4242 return a->dw_attr_val.v.val_unsigned;
4245 /* Add an unsigned wide integer attribute value to a DIE. */
4247 static inline void
4248 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4249 const wide_int& w)
4251 dw_attr_node attr;
4253 attr.dw_attr = attr_kind;
4254 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4255 attr.dw_attr_val.val_entry = NULL;
4256 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4257 *attr.dw_attr_val.v.val_wide = w;
4258 add_dwarf_attr (die, &attr);
4261 /* Add an unsigned double integer attribute value to a DIE. */
4263 static inline void
4264 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4265 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4267 dw_attr_node attr;
4269 attr.dw_attr = attr_kind;
4270 attr.dw_attr_val.val_class = dw_val_class_const_double;
4271 attr.dw_attr_val.val_entry = NULL;
4272 attr.dw_attr_val.v.val_double.high = high;
4273 attr.dw_attr_val.v.val_double.low = low;
4274 add_dwarf_attr (die, &attr);
4277 /* Add a floating point attribute value to a DIE and return it. */
4279 static inline void
4280 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4281 unsigned int length, unsigned int elt_size, unsigned char *array)
4283 dw_attr_node attr;
4285 attr.dw_attr = attr_kind;
4286 attr.dw_attr_val.val_class = dw_val_class_vec;
4287 attr.dw_attr_val.val_entry = NULL;
4288 attr.dw_attr_val.v.val_vec.length = length;
4289 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4290 attr.dw_attr_val.v.val_vec.array = array;
4291 add_dwarf_attr (die, &attr);
4294 /* Add an 8-byte data attribute value to a DIE. */
4296 static inline void
4297 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4298 unsigned char data8[8])
4300 dw_attr_node attr;
4302 attr.dw_attr = attr_kind;
4303 attr.dw_attr_val.val_class = dw_val_class_data8;
4304 attr.dw_attr_val.val_entry = NULL;
4305 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4306 add_dwarf_attr (die, &attr);
4309 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4310 dwarf_split_debug_info, address attributes in dies destined for the
4311 final executable have force_direct set to avoid using indexed
4312 references. */
4314 static inline void
4315 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4316 bool force_direct)
4318 dw_attr_node attr;
4319 char * lbl_id;
4321 lbl_id = xstrdup (lbl_low);
4322 attr.dw_attr = DW_AT_low_pc;
4323 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4324 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4325 if (dwarf_split_debug_info && !force_direct)
4326 attr.dw_attr_val.val_entry
4327 = add_addr_table_entry (lbl_id, ate_kind_label);
4328 else
4329 attr.dw_attr_val.val_entry = NULL;
4330 add_dwarf_attr (die, &attr);
4332 attr.dw_attr = DW_AT_high_pc;
4333 if (dwarf_version < 4)
4334 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4335 else
4336 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4337 lbl_id = xstrdup (lbl_high);
4338 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4339 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4340 && dwarf_split_debug_info && !force_direct)
4341 attr.dw_attr_val.val_entry
4342 = add_addr_table_entry (lbl_id, ate_kind_label);
4343 else
4344 attr.dw_attr_val.val_entry = NULL;
4345 add_dwarf_attr (die, &attr);
4348 /* Hash and equality functions for debug_str_hash. */
4350 hashval_t
4351 indirect_string_hasher::hash (indirect_string_node *x)
4353 return htab_hash_string (x->str);
4356 bool
4357 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4359 return strcmp (x1->str, x2) == 0;
4362 /* Add STR to the given string hash table. */
4364 static struct indirect_string_node *
4365 find_AT_string_in_table (const char *str,
4366 hash_table<indirect_string_hasher> *table)
4368 struct indirect_string_node *node;
4370 indirect_string_node **slot
4371 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4372 if (*slot == NULL)
4374 node = ggc_cleared_alloc<indirect_string_node> ();
4375 node->str = ggc_strdup (str);
4376 *slot = node;
4378 else
4379 node = *slot;
4381 node->refcount++;
4382 return node;
4385 /* Add STR to the indirect string hash table. */
4387 static struct indirect_string_node *
4388 find_AT_string (const char *str)
4390 if (! debug_str_hash)
4391 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4393 return find_AT_string_in_table (str, debug_str_hash);
4396 /* Add a string attribute value to a DIE. */
4398 static inline void
4399 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4401 dw_attr_node attr;
4402 struct indirect_string_node *node;
4404 node = find_AT_string (str);
4406 attr.dw_attr = attr_kind;
4407 attr.dw_attr_val.val_class = dw_val_class_str;
4408 attr.dw_attr_val.val_entry = NULL;
4409 attr.dw_attr_val.v.val_str = node;
4410 add_dwarf_attr (die, &attr);
4413 static inline const char *
4414 AT_string (dw_attr_node *a)
4416 gcc_assert (a && AT_class (a) == dw_val_class_str);
4417 return a->dw_attr_val.v.val_str->str;
4420 /* Call this function directly to bypass AT_string_form's logic to put
4421 the string inline in the die. */
4423 static void
4424 set_indirect_string (struct indirect_string_node *node)
4426 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4427 /* Already indirect is a no op. */
4428 if (node->form == DW_FORM_strp
4429 || node->form == DW_FORM_line_strp
4430 || node->form == DW_FORM_GNU_str_index)
4432 gcc_assert (node->label);
4433 return;
4435 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4436 ++dw2_string_counter;
4437 node->label = xstrdup (label);
4439 if (!dwarf_split_debug_info)
4441 node->form = DW_FORM_strp;
4442 node->index = NOT_INDEXED;
4444 else
4446 node->form = DW_FORM_GNU_str_index;
4447 node->index = NO_INDEX_ASSIGNED;
4451 /* A helper function for dwarf2out_finish, called to reset indirect
4452 string decisions done for early LTO dwarf output before fat object
4453 dwarf output. */
4456 reset_indirect_string (indirect_string_node **h, void *)
4458 struct indirect_string_node *node = *h;
4459 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4461 free (node->label);
4462 node->label = NULL;
4463 node->form = (dwarf_form) 0;
4464 node->index = 0;
4466 return 1;
4469 /* Find out whether a string should be output inline in DIE
4470 or out-of-line in .debug_str section. */
4472 static enum dwarf_form
4473 find_string_form (struct indirect_string_node *node)
4475 unsigned int len;
4477 if (node->form)
4478 return node->form;
4480 len = strlen (node->str) + 1;
4482 /* If the string is shorter or equal to the size of the reference, it is
4483 always better to put it inline. */
4484 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4485 return node->form = DW_FORM_string;
4487 /* If we cannot expect the linker to merge strings in .debug_str
4488 section, only put it into .debug_str if it is worth even in this
4489 single module. */
4490 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4491 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4492 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4493 return node->form = DW_FORM_string;
4495 set_indirect_string (node);
4497 return node->form;
4500 /* Find out whether the string referenced from the attribute should be
4501 output inline in DIE or out-of-line in .debug_str section. */
4503 static enum dwarf_form
4504 AT_string_form (dw_attr_node *a)
4506 gcc_assert (a && AT_class (a) == dw_val_class_str);
4507 return find_string_form (a->dw_attr_val.v.val_str);
4510 /* Add a DIE reference attribute value to a DIE. */
4512 static inline void
4513 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4515 dw_attr_node attr;
4516 gcc_checking_assert (targ_die != NULL);
4518 /* With LTO we can end up trying to reference something we didn't create
4519 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4520 if (targ_die == NULL)
4521 return;
4523 attr.dw_attr = attr_kind;
4524 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4525 attr.dw_attr_val.val_entry = NULL;
4526 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4527 attr.dw_attr_val.v.val_die_ref.external = 0;
4528 add_dwarf_attr (die, &attr);
4531 /* Change DIE reference REF to point to NEW_DIE instead. */
4533 static inline void
4534 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4536 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4537 ref->dw_attr_val.v.val_die_ref.die = new_die;
4538 ref->dw_attr_val.v.val_die_ref.external = 0;
4541 /* Add an AT_specification attribute to a DIE, and also make the back
4542 pointer from the specification to the definition. */
4544 static inline void
4545 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4547 add_AT_die_ref (die, DW_AT_specification, targ_die);
4548 gcc_assert (!targ_die->die_definition);
4549 targ_die->die_definition = die;
4552 static inline dw_die_ref
4553 AT_ref (dw_attr_node *a)
4555 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4556 return a->dw_attr_val.v.val_die_ref.die;
4559 static inline int
4560 AT_ref_external (dw_attr_node *a)
4562 if (a && AT_class (a) == dw_val_class_die_ref)
4563 return a->dw_attr_val.v.val_die_ref.external;
4565 return 0;
4568 static inline void
4569 set_AT_ref_external (dw_attr_node *a, int i)
4571 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4572 a->dw_attr_val.v.val_die_ref.external = i;
4575 /* Add an FDE reference attribute value to a DIE. */
4577 static inline void
4578 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_fde_index = targ_fde;
4586 add_dwarf_attr (die, &attr);
4589 /* Add a location description attribute value to a DIE. */
4591 static inline void
4592 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4594 dw_attr_node attr;
4596 attr.dw_attr = attr_kind;
4597 attr.dw_attr_val.val_class = dw_val_class_loc;
4598 attr.dw_attr_val.val_entry = NULL;
4599 attr.dw_attr_val.v.val_loc = loc;
4600 add_dwarf_attr (die, &attr);
4603 static inline dw_loc_descr_ref
4604 AT_loc (dw_attr_node *a)
4606 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4607 return a->dw_attr_val.v.val_loc;
4610 static inline void
4611 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4613 dw_attr_node attr;
4615 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4616 return;
4618 attr.dw_attr = attr_kind;
4619 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4620 attr.dw_attr_val.val_entry = NULL;
4621 attr.dw_attr_val.v.val_loc_list = loc_list;
4622 add_dwarf_attr (die, &attr);
4623 have_location_lists = true;
4626 static inline dw_loc_list_ref
4627 AT_loc_list (dw_attr_node *a)
4629 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4630 return a->dw_attr_val.v.val_loc_list;
4633 static inline dw_loc_list_ref *
4634 AT_loc_list_ptr (dw_attr_node *a)
4636 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4637 return &a->dw_attr_val.v.val_loc_list;
4640 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4642 static hashval_t hash (addr_table_entry *);
4643 static bool equal (addr_table_entry *, addr_table_entry *);
4646 /* Table of entries into the .debug_addr section. */
4648 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4650 /* Hash an address_table_entry. */
4652 hashval_t
4653 addr_hasher::hash (addr_table_entry *a)
4655 inchash::hash hstate;
4656 switch (a->kind)
4658 case ate_kind_rtx:
4659 hstate.add_int (0);
4660 break;
4661 case ate_kind_rtx_dtprel:
4662 hstate.add_int (1);
4663 break;
4664 case ate_kind_label:
4665 return htab_hash_string (a->addr.label);
4666 default:
4667 gcc_unreachable ();
4669 inchash::add_rtx (a->addr.rtl, hstate);
4670 return hstate.end ();
4673 /* Determine equality for two address_table_entries. */
4675 bool
4676 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4678 if (a1->kind != a2->kind)
4679 return 0;
4680 switch (a1->kind)
4682 case ate_kind_rtx:
4683 case ate_kind_rtx_dtprel:
4684 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4685 case ate_kind_label:
4686 return strcmp (a1->addr.label, a2->addr.label) == 0;
4687 default:
4688 gcc_unreachable ();
4692 /* Initialize an addr_table_entry. */
4694 void
4695 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4697 e->kind = kind;
4698 switch (kind)
4700 case ate_kind_rtx:
4701 case ate_kind_rtx_dtprel:
4702 e->addr.rtl = (rtx) addr;
4703 break;
4704 case ate_kind_label:
4705 e->addr.label = (char *) addr;
4706 break;
4708 e->refcount = 0;
4709 e->index = NO_INDEX_ASSIGNED;
4712 /* Add attr to the address table entry to the table. Defer setting an
4713 index until output time. */
4715 static addr_table_entry *
4716 add_addr_table_entry (void *addr, enum ate_kind kind)
4718 addr_table_entry *node;
4719 addr_table_entry finder;
4721 gcc_assert (dwarf_split_debug_info);
4722 if (! addr_index_table)
4723 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4724 init_addr_table_entry (&finder, kind, addr);
4725 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4727 if (*slot == HTAB_EMPTY_ENTRY)
4729 node = ggc_cleared_alloc<addr_table_entry> ();
4730 init_addr_table_entry (node, kind, addr);
4731 *slot = node;
4733 else
4734 node = *slot;
4736 node->refcount++;
4737 return node;
4740 /* Remove an entry from the addr table by decrementing its refcount.
4741 Strictly, decrementing the refcount would be enough, but the
4742 assertion that the entry is actually in the table has found
4743 bugs. */
4745 static void
4746 remove_addr_table_entry (addr_table_entry *entry)
4748 gcc_assert (dwarf_split_debug_info && addr_index_table);
4749 /* After an index is assigned, the table is frozen. */
4750 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4751 entry->refcount--;
4754 /* Given a location list, remove all addresses it refers to from the
4755 address_table. */
4757 static void
4758 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4760 for (; descr; descr = descr->dw_loc_next)
4761 if (descr->dw_loc_oprnd1.val_entry != NULL)
4763 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4764 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4768 /* A helper function for dwarf2out_finish called through
4769 htab_traverse. Assign an addr_table_entry its index. All entries
4770 must be collected into the table when this function is called,
4771 because the indexing code relies on htab_traverse to traverse nodes
4772 in the same order for each run. */
4775 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4777 addr_table_entry *node = *h;
4779 /* Don't index unreferenced nodes. */
4780 if (node->refcount == 0)
4781 return 1;
4783 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4784 node->index = *index;
4785 *index += 1;
4787 return 1;
4790 /* Add an address constant attribute value to a DIE. When using
4791 dwarf_split_debug_info, address attributes in dies destined for the
4792 final executable should be direct references--setting the parameter
4793 force_direct ensures this behavior. */
4795 static inline void
4796 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4797 bool force_direct)
4799 dw_attr_node attr;
4801 attr.dw_attr = attr_kind;
4802 attr.dw_attr_val.val_class = dw_val_class_addr;
4803 attr.dw_attr_val.v.val_addr = addr;
4804 if (dwarf_split_debug_info && !force_direct)
4805 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4806 else
4807 attr.dw_attr_val.val_entry = NULL;
4808 add_dwarf_attr (die, &attr);
4811 /* Get the RTX from to an address DIE attribute. */
4813 static inline rtx
4814 AT_addr (dw_attr_node *a)
4816 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4817 return a->dw_attr_val.v.val_addr;
4820 /* Add a file attribute value to a DIE. */
4822 static inline void
4823 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4824 struct dwarf_file_data *fd)
4826 dw_attr_node attr;
4828 attr.dw_attr = attr_kind;
4829 attr.dw_attr_val.val_class = dw_val_class_file;
4830 attr.dw_attr_val.val_entry = NULL;
4831 attr.dw_attr_val.v.val_file = fd;
4832 add_dwarf_attr (die, &attr);
4835 /* Get the dwarf_file_data from a file DIE attribute. */
4837 static inline struct dwarf_file_data *
4838 AT_file (dw_attr_node *a)
4840 gcc_assert (a && (AT_class (a) == dw_val_class_file
4841 || AT_class (a) == dw_val_class_file_implicit));
4842 return a->dw_attr_val.v.val_file;
4845 /* Add a vms delta attribute value to a DIE. */
4847 static inline void
4848 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4849 const char *lbl1, const char *lbl2)
4851 dw_attr_node attr;
4853 attr.dw_attr = attr_kind;
4854 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4855 attr.dw_attr_val.val_entry = NULL;
4856 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4857 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4858 add_dwarf_attr (die, &attr);
4861 /* Add a label identifier attribute value to a DIE. */
4863 static inline void
4864 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4865 const char *lbl_id)
4867 dw_attr_node attr;
4869 attr.dw_attr = attr_kind;
4870 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4871 attr.dw_attr_val.val_entry = NULL;
4872 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4873 if (dwarf_split_debug_info)
4874 attr.dw_attr_val.val_entry
4875 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4876 ate_kind_label);
4877 add_dwarf_attr (die, &attr);
4880 /* Add a section offset attribute value to a DIE, an offset into the
4881 debug_line section. */
4883 static inline void
4884 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4885 const char *label)
4887 dw_attr_node attr;
4889 attr.dw_attr = attr_kind;
4890 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4891 attr.dw_attr_val.val_entry = NULL;
4892 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4893 add_dwarf_attr (die, &attr);
4896 /* Add a section offset attribute value to a DIE, an offset into the
4897 debug_loclists section. */
4899 static inline void
4900 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4901 const char *label)
4903 dw_attr_node attr;
4905 attr.dw_attr = attr_kind;
4906 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4907 attr.dw_attr_val.val_entry = NULL;
4908 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4909 add_dwarf_attr (die, &attr);
4912 /* Add a section offset attribute value to a DIE, an offset into the
4913 debug_macinfo section. */
4915 static inline void
4916 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4917 const char *label)
4919 dw_attr_node attr;
4921 attr.dw_attr = attr_kind;
4922 attr.dw_attr_val.val_class = dw_val_class_macptr;
4923 attr.dw_attr_val.val_entry = NULL;
4924 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4925 add_dwarf_attr (die, &attr);
4928 /* Add an offset attribute value to a DIE. */
4930 static inline void
4931 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4932 unsigned HOST_WIDE_INT offset)
4934 dw_attr_node attr;
4936 attr.dw_attr = attr_kind;
4937 attr.dw_attr_val.val_class = dw_val_class_offset;
4938 attr.dw_attr_val.val_entry = NULL;
4939 attr.dw_attr_val.v.val_offset = offset;
4940 add_dwarf_attr (die, &attr);
4943 /* Add a range_list attribute value to a DIE. When using
4944 dwarf_split_debug_info, address attributes in dies destined for the
4945 final executable should be direct references--setting the parameter
4946 force_direct ensures this behavior. */
4948 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4949 #define RELOCATED_OFFSET (NULL)
4951 static void
4952 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4953 long unsigned int offset, bool force_direct)
4955 dw_attr_node attr;
4957 attr.dw_attr = attr_kind;
4958 attr.dw_attr_val.val_class = dw_val_class_range_list;
4959 /* For the range_list attribute, use val_entry to store whether the
4960 offset should follow split-debug-info or normal semantics. This
4961 value is read in output_range_list_offset. */
4962 if (dwarf_split_debug_info && !force_direct)
4963 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4964 else
4965 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4966 attr.dw_attr_val.v.val_offset = offset;
4967 add_dwarf_attr (die, &attr);
4970 /* Return the start label of a delta attribute. */
4972 static inline const char *
4973 AT_vms_delta1 (dw_attr_node *a)
4975 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4976 return a->dw_attr_val.v.val_vms_delta.lbl1;
4979 /* Return the end label of a delta attribute. */
4981 static inline const char *
4982 AT_vms_delta2 (dw_attr_node *a)
4984 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4985 return a->dw_attr_val.v.val_vms_delta.lbl2;
4988 static inline const char *
4989 AT_lbl (dw_attr_node *a)
4991 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4992 || AT_class (a) == dw_val_class_lineptr
4993 || AT_class (a) == dw_val_class_macptr
4994 || AT_class (a) == dw_val_class_loclistsptr
4995 || AT_class (a) == dw_val_class_high_pc));
4996 return a->dw_attr_val.v.val_lbl_id;
4999 /* Get the attribute of type attr_kind. */
5001 static dw_attr_node *
5002 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5004 dw_attr_node *a;
5005 unsigned ix;
5006 dw_die_ref spec = NULL;
5008 if (! die)
5009 return NULL;
5011 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5012 if (a->dw_attr == attr_kind)
5013 return a;
5014 else if (a->dw_attr == DW_AT_specification
5015 || a->dw_attr == DW_AT_abstract_origin)
5016 spec = AT_ref (a);
5018 if (spec)
5019 return get_AT (spec, attr_kind);
5021 return NULL;
5024 /* Returns the parent of the declaration of DIE. */
5026 static dw_die_ref
5027 get_die_parent (dw_die_ref die)
5029 dw_die_ref t;
5031 if (!die)
5032 return NULL;
5034 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5035 || (t = get_AT_ref (die, DW_AT_specification)))
5036 die = t;
5038 return die->die_parent;
5041 /* Return the "low pc" attribute value, typically associated with a subprogram
5042 DIE. Return null if the "low pc" attribute is either not present, or if it
5043 cannot be represented as an assembler label identifier. */
5045 static inline const char *
5046 get_AT_low_pc (dw_die_ref die)
5048 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5050 return a ? AT_lbl (a) : NULL;
5053 /* Return the "high pc" attribute value, typically associated with a subprogram
5054 DIE. Return null if the "high pc" attribute is either not present, or if it
5055 cannot be represented as an assembler label identifier. */
5057 static inline const char *
5058 get_AT_hi_pc (dw_die_ref die)
5060 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5062 return a ? AT_lbl (a) : NULL;
5065 /* Return the value of the string attribute designated by ATTR_KIND, or
5066 NULL if it is not present. */
5068 static inline const char *
5069 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5071 dw_attr_node *a = get_AT (die, attr_kind);
5073 return a ? AT_string (a) : NULL;
5076 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5077 if it is not present. */
5079 static inline int
5080 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5082 dw_attr_node *a = get_AT (die, attr_kind);
5084 return a ? AT_flag (a) : 0;
5087 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5088 if it is not present. */
5090 static inline unsigned
5091 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5093 dw_attr_node *a = get_AT (die, attr_kind);
5095 return a ? AT_unsigned (a) : 0;
5098 static inline dw_die_ref
5099 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5101 dw_attr_node *a = get_AT (die, attr_kind);
5103 return a ? AT_ref (a) : NULL;
5106 static inline struct dwarf_file_data *
5107 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5109 dw_attr_node *a = get_AT (die, attr_kind);
5111 return a ? AT_file (a) : NULL;
5114 /* Return TRUE if the language is C++. */
5116 static inline bool
5117 is_cxx (void)
5119 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5121 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5122 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5125 /* Return TRUE if DECL was created by the C++ frontend. */
5127 static bool
5128 is_cxx (const_tree decl)
5130 if (in_lto_p)
5132 const_tree context = get_ultimate_context (decl);
5133 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5134 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5136 return is_cxx ();
5139 /* Return TRUE if the language is Fortran. */
5141 static inline bool
5142 is_fortran (void)
5144 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5146 return (lang == DW_LANG_Fortran77
5147 || lang == DW_LANG_Fortran90
5148 || lang == DW_LANG_Fortran95
5149 || lang == DW_LANG_Fortran03
5150 || lang == DW_LANG_Fortran08);
5153 static inline bool
5154 is_fortran (const_tree decl)
5156 if (in_lto_p)
5158 const_tree context = get_ultimate_context (decl);
5159 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5160 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5161 "GNU Fortran", 11) == 0
5162 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5163 "GNU F77") == 0);
5165 return is_fortran ();
5168 /* Return TRUE if the language is Ada. */
5170 static inline bool
5171 is_ada (void)
5173 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5175 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5178 /* Remove the specified attribute if present. Return TRUE if removal
5179 was successful. */
5181 static bool
5182 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5184 dw_attr_node *a;
5185 unsigned ix;
5187 if (! die)
5188 return false;
5190 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5191 if (a->dw_attr == attr_kind)
5193 if (AT_class (a) == dw_val_class_str)
5194 if (a->dw_attr_val.v.val_str->refcount)
5195 a->dw_attr_val.v.val_str->refcount--;
5197 /* vec::ordered_remove should help reduce the number of abbrevs
5198 that are needed. */
5199 die->die_attr->ordered_remove (ix);
5200 return true;
5202 return false;
5205 /* Remove CHILD from its parent. PREV must have the property that
5206 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5208 static void
5209 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5211 gcc_assert (child->die_parent == prev->die_parent);
5212 gcc_assert (prev->die_sib == child);
5213 if (prev == child)
5215 gcc_assert (child->die_parent->die_child == child);
5216 prev = NULL;
5218 else
5219 prev->die_sib = child->die_sib;
5220 if (child->die_parent->die_child == child)
5221 child->die_parent->die_child = prev;
5222 child->die_sib = NULL;
5225 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5226 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5228 static void
5229 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5231 dw_die_ref parent = old_child->die_parent;
5233 gcc_assert (parent == prev->die_parent);
5234 gcc_assert (prev->die_sib == old_child);
5236 new_child->die_parent = parent;
5237 if (prev == old_child)
5239 gcc_assert (parent->die_child == old_child);
5240 new_child->die_sib = new_child;
5242 else
5244 prev->die_sib = new_child;
5245 new_child->die_sib = old_child->die_sib;
5247 if (old_child->die_parent->die_child == old_child)
5248 old_child->die_parent->die_child = new_child;
5249 old_child->die_sib = NULL;
5252 /* Move all children from OLD_PARENT to NEW_PARENT. */
5254 static void
5255 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5257 dw_die_ref c;
5258 new_parent->die_child = old_parent->die_child;
5259 old_parent->die_child = NULL;
5260 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5263 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5264 matches TAG. */
5266 static void
5267 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5269 dw_die_ref c;
5271 c = die->die_child;
5272 if (c) do {
5273 dw_die_ref prev = c;
5274 c = c->die_sib;
5275 while (c->die_tag == tag)
5277 remove_child_with_prev (c, prev);
5278 c->die_parent = NULL;
5279 /* Might have removed every child. */
5280 if (die->die_child == NULL)
5281 return;
5282 c = prev->die_sib;
5284 } while (c != die->die_child);
5287 /* Add a CHILD_DIE as the last child of DIE. */
5289 static void
5290 add_child_die (dw_die_ref die, dw_die_ref child_die)
5292 /* FIXME this should probably be an assert. */
5293 if (! die || ! child_die)
5294 return;
5295 gcc_assert (die != child_die);
5297 child_die->die_parent = die;
5298 if (die->die_child)
5300 child_die->die_sib = die->die_child->die_sib;
5301 die->die_child->die_sib = child_die;
5303 else
5304 child_die->die_sib = child_die;
5305 die->die_child = child_die;
5308 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5310 static void
5311 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5312 dw_die_ref after_die)
5314 gcc_assert (die
5315 && child_die
5316 && after_die
5317 && die->die_child
5318 && die != child_die);
5320 child_die->die_parent = die;
5321 child_die->die_sib = after_die->die_sib;
5322 after_die->die_sib = child_die;
5323 if (die->die_child == after_die)
5324 die->die_child = child_die;
5327 /* Unassociate CHILD from its parent, and make its parent be
5328 NEW_PARENT. */
5330 static void
5331 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5333 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5334 if (p->die_sib == child)
5336 remove_child_with_prev (child, p);
5337 break;
5339 add_child_die (new_parent, child);
5342 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5343 is the specification, to the end of PARENT's list of children.
5344 This is done by removing and re-adding it. */
5346 static void
5347 splice_child_die (dw_die_ref parent, dw_die_ref child)
5349 /* We want the declaration DIE from inside the class, not the
5350 specification DIE at toplevel. */
5351 if (child->die_parent != parent)
5353 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5355 if (tmp)
5356 child = tmp;
5359 gcc_assert (child->die_parent == parent
5360 || (child->die_parent
5361 == get_AT_ref (parent, DW_AT_specification)));
5363 reparent_child (child, parent);
5366 /* Create and return a new die with TAG_VALUE as tag. */
5368 static inline dw_die_ref
5369 new_die_raw (enum dwarf_tag tag_value)
5371 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5372 die->die_tag = tag_value;
5373 return die;
5376 /* Create and return a new die with a parent of PARENT_DIE. If
5377 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5378 associated tree T must be supplied to determine parenthood
5379 later. */
5381 static inline dw_die_ref
5382 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5384 dw_die_ref die = new_die_raw (tag_value);
5386 if (parent_die != NULL)
5387 add_child_die (parent_die, die);
5388 else
5390 limbo_die_node *limbo_node;
5392 /* No DIEs created after early dwarf should end up in limbo,
5393 because the limbo list should not persist past LTO
5394 streaming. */
5395 if (tag_value != DW_TAG_compile_unit
5396 /* These are allowed because they're generated while
5397 breaking out COMDAT units late. */
5398 && tag_value != DW_TAG_type_unit
5399 && tag_value != DW_TAG_skeleton_unit
5400 && !early_dwarf
5401 /* Allow nested functions to live in limbo because they will
5402 only temporarily live there, as decls_for_scope will fix
5403 them up. */
5404 && (TREE_CODE (t) != FUNCTION_DECL
5405 || !decl_function_context (t))
5406 /* Same as nested functions above but for types. Types that
5407 are local to a function will be fixed in
5408 decls_for_scope. */
5409 && (!RECORD_OR_UNION_TYPE_P (t)
5410 || !TYPE_CONTEXT (t)
5411 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5412 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5413 especially in the ltrans stage, but once we implement LTO
5414 dwarf streaming, we should remove this exception. */
5415 && !in_lto_p)
5417 fprintf (stderr, "symbol ended up in limbo too late:");
5418 debug_generic_stmt (t);
5419 gcc_unreachable ();
5422 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5423 limbo_node->die = die;
5424 limbo_node->created_for = t;
5425 limbo_node->next = limbo_die_list;
5426 limbo_die_list = limbo_node;
5429 return die;
5432 /* Return the DIE associated with the given type specifier. */
5434 static inline dw_die_ref
5435 lookup_type_die (tree type)
5437 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5438 if (die && die->removed)
5440 TYPE_SYMTAB_DIE (type) = NULL;
5441 return NULL;
5443 return die;
5446 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5447 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5448 anonymous type instead the one of the naming typedef. */
5450 static inline dw_die_ref
5451 strip_naming_typedef (tree type, dw_die_ref type_die)
5453 if (type
5454 && TREE_CODE (type) == RECORD_TYPE
5455 && type_die
5456 && type_die->die_tag == DW_TAG_typedef
5457 && is_naming_typedef_decl (TYPE_NAME (type)))
5458 type_die = get_AT_ref (type_die, DW_AT_type);
5459 return type_die;
5462 /* Like lookup_type_die, but if type is an anonymous type named by a
5463 typedef[1], return the DIE of the anonymous type instead the one of
5464 the naming typedef. This is because in gen_typedef_die, we did
5465 equate the anonymous struct named by the typedef with the DIE of
5466 the naming typedef. So by default, lookup_type_die on an anonymous
5467 struct yields the DIE of the naming typedef.
5469 [1]: Read the comment of is_naming_typedef_decl to learn about what
5470 a naming typedef is. */
5472 static inline dw_die_ref
5473 lookup_type_die_strip_naming_typedef (tree type)
5475 dw_die_ref die = lookup_type_die (type);
5476 return strip_naming_typedef (type, die);
5479 /* Equate a DIE to a given type specifier. */
5481 static inline void
5482 equate_type_number_to_die (tree type, dw_die_ref type_die)
5484 TYPE_SYMTAB_DIE (type) = type_die;
5487 /* Returns a hash value for X (which really is a die_struct). */
5489 inline hashval_t
5490 decl_die_hasher::hash (die_node *x)
5492 return (hashval_t) x->decl_id;
5495 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5497 inline bool
5498 decl_die_hasher::equal (die_node *x, tree y)
5500 return (x->decl_id == DECL_UID (y));
5503 /* Return the DIE associated with a given declaration. */
5505 static inline dw_die_ref
5506 lookup_decl_die (tree decl)
5508 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5509 NO_INSERT);
5510 if (!die)
5511 return NULL;
5512 if ((*die)->removed)
5514 decl_die_table->clear_slot (die);
5515 return NULL;
5517 return *die;
5521 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5522 style reference. Return true if we found one refering to a DIE for
5523 DECL, otherwise return false. */
5525 static bool
5526 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5527 unsigned HOST_WIDE_INT *off)
5529 dw_die_ref die;
5531 if (flag_wpa && !decl_die_table)
5532 return false;
5534 if (TREE_CODE (decl) == BLOCK)
5535 die = BLOCK_DIE (decl);
5536 else
5537 die = lookup_decl_die (decl);
5538 if (!die)
5539 return false;
5541 /* During WPA stage we currently use DIEs to store the
5542 decl <-> label + offset map. That's quite inefficient but it
5543 works for now. */
5544 if (flag_wpa)
5546 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5547 if (!ref)
5549 gcc_assert (die == comp_unit_die ());
5550 return false;
5552 *off = ref->die_offset;
5553 *sym = ref->die_id.die_symbol;
5554 return true;
5557 /* Similar to get_ref_die_offset_label, but using the "correct"
5558 label. */
5559 *off = die->die_offset;
5560 while (die->die_parent)
5561 die = die->die_parent;
5562 /* For the containing CU DIE we compute a die_symbol in
5563 compute_comp_unit_symbol. */
5564 gcc_assert (die->die_tag == DW_TAG_compile_unit
5565 && die->die_id.die_symbol != NULL);
5566 *sym = die->die_id.die_symbol;
5567 return true;
5570 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5572 static void
5573 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5574 const char *symbol, HOST_WIDE_INT offset)
5576 /* Create a fake DIE that contains the reference. Don't use
5577 new_die because we don't want to end up in the limbo list. */
5578 dw_die_ref ref = new_die_raw (die->die_tag);
5579 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5580 ref->die_offset = offset;
5581 ref->with_offset = 1;
5582 add_AT_die_ref (die, attr_kind, ref);
5585 /* Create a DIE for DECL if required and add a reference to a DIE
5586 at SYMBOL + OFFSET which contains attributes dumped early. */
5588 static void
5589 dwarf2out_register_external_die (tree decl, const char *sym,
5590 unsigned HOST_WIDE_INT off)
5592 if (debug_info_level == DINFO_LEVEL_NONE)
5593 return;
5595 if (flag_wpa && !decl_die_table)
5596 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5598 dw_die_ref die
5599 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5600 gcc_assert (!die);
5602 tree ctx;
5603 dw_die_ref parent = NULL;
5604 /* Need to lookup a DIE for the decls context - the containing
5605 function or translation unit. */
5606 if (TREE_CODE (decl) == BLOCK)
5608 ctx = BLOCK_SUPERCONTEXT (decl);
5609 /* ??? We do not output DIEs for all scopes thus skip as
5610 many DIEs as needed. */
5611 while (TREE_CODE (ctx) == BLOCK
5612 && !BLOCK_DIE (ctx))
5613 ctx = BLOCK_SUPERCONTEXT (ctx);
5615 else
5616 ctx = DECL_CONTEXT (decl);
5617 while (ctx && TYPE_P (ctx))
5618 ctx = TYPE_CONTEXT (ctx);
5619 if (ctx)
5621 if (TREE_CODE (ctx) == BLOCK)
5622 parent = BLOCK_DIE (ctx);
5623 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5624 /* Keep the 1:1 association during WPA. */
5625 && !flag_wpa)
5626 /* Otherwise all late annotations go to the main CU which
5627 imports the original CUs. */
5628 parent = comp_unit_die ();
5629 else if (TREE_CODE (ctx) == FUNCTION_DECL
5630 && TREE_CODE (decl) != PARM_DECL
5631 && TREE_CODE (decl) != BLOCK)
5632 /* Leave function local entities parent determination to when
5633 we process scope vars. */
5635 else
5636 parent = lookup_decl_die (ctx);
5638 else
5639 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5640 Handle this case gracefully by globalizing stuff. */
5641 parent = comp_unit_die ();
5642 /* Create a DIE "stub". */
5643 switch (TREE_CODE (decl))
5645 case TRANSLATION_UNIT_DECL:
5646 if (! flag_wpa)
5648 die = comp_unit_die ();
5649 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5650 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5651 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5652 to create a DIE for the original CUs. */
5653 return;
5655 /* Keep the 1:1 association during WPA. */
5656 die = new_die (DW_TAG_compile_unit, NULL, decl);
5657 break;
5658 case NAMESPACE_DECL:
5659 if (is_fortran (decl))
5660 die = new_die (DW_TAG_module, parent, decl);
5661 else
5662 die = new_die (DW_TAG_namespace, parent, decl);
5663 break;
5664 case FUNCTION_DECL:
5665 die = new_die (DW_TAG_subprogram, parent, decl);
5666 break;
5667 case VAR_DECL:
5668 die = new_die (DW_TAG_variable, parent, decl);
5669 break;
5670 case RESULT_DECL:
5671 die = new_die (DW_TAG_variable, parent, decl);
5672 break;
5673 case PARM_DECL:
5674 die = new_die (DW_TAG_formal_parameter, parent, decl);
5675 break;
5676 case CONST_DECL:
5677 die = new_die (DW_TAG_constant, parent, decl);
5678 break;
5679 case LABEL_DECL:
5680 die = new_die (DW_TAG_label, parent, decl);
5681 break;
5682 case BLOCK:
5683 die = new_die (DW_TAG_lexical_block, parent, decl);
5684 break;
5685 default:
5686 gcc_unreachable ();
5688 if (TREE_CODE (decl) == BLOCK)
5689 BLOCK_DIE (decl) = die;
5690 else
5691 equate_decl_number_to_die (decl, die);
5693 /* Add a reference to the DIE providing early debug at $sym + off. */
5694 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5697 /* Returns a hash value for X (which really is a var_loc_list). */
5699 inline hashval_t
5700 decl_loc_hasher::hash (var_loc_list *x)
5702 return (hashval_t) x->decl_id;
5705 /* Return nonzero if decl_id of var_loc_list X is the same as
5706 UID of decl *Y. */
5708 inline bool
5709 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5711 return (x->decl_id == DECL_UID (y));
5714 /* Return the var_loc list associated with a given declaration. */
5716 static inline var_loc_list *
5717 lookup_decl_loc (const_tree decl)
5719 if (!decl_loc_table)
5720 return NULL;
5721 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5724 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5726 inline hashval_t
5727 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5729 return (hashval_t) x->decl_id;
5732 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5733 UID of decl *Y. */
5735 inline bool
5736 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5738 return (x->decl_id == DECL_UID (y));
5741 /* Equate a DIE to a particular declaration. */
5743 static void
5744 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5746 unsigned int decl_id = DECL_UID (decl);
5748 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5749 decl_die->decl_id = decl_id;
5752 /* Return how many bits covers PIECE EXPR_LIST. */
5754 static HOST_WIDE_INT
5755 decl_piece_bitsize (rtx piece)
5757 int ret = (int) GET_MODE (piece);
5758 if (ret)
5759 return ret;
5760 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5761 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5762 return INTVAL (XEXP (XEXP (piece, 0), 0));
5765 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5767 static rtx *
5768 decl_piece_varloc_ptr (rtx piece)
5770 if ((int) GET_MODE (piece))
5771 return &XEXP (piece, 0);
5772 else
5773 return &XEXP (XEXP (piece, 0), 1);
5776 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5777 Next is the chain of following piece nodes. */
5779 static rtx_expr_list *
5780 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5782 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5783 return alloc_EXPR_LIST (bitsize, loc_note, next);
5784 else
5785 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5786 GEN_INT (bitsize),
5787 loc_note), next);
5790 /* Return rtx that should be stored into loc field for
5791 LOC_NOTE and BITPOS/BITSIZE. */
5793 static rtx
5794 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5795 HOST_WIDE_INT bitsize)
5797 if (bitsize != -1)
5799 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5800 if (bitpos != 0)
5801 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5803 return loc_note;
5806 /* This function either modifies location piece list *DEST in
5807 place (if SRC and INNER is NULL), or copies location piece list
5808 *SRC to *DEST while modifying it. Location BITPOS is modified
5809 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5810 not copied and if needed some padding around it is added.
5811 When modifying in place, DEST should point to EXPR_LIST where
5812 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5813 to the start of the whole list and INNER points to the EXPR_LIST
5814 where earlier pieces cover PIECE_BITPOS bits. */
5816 static void
5817 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5818 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5819 HOST_WIDE_INT bitsize, rtx loc_note)
5821 HOST_WIDE_INT diff;
5822 bool copy = inner != NULL;
5824 if (copy)
5826 /* First copy all nodes preceding the current bitpos. */
5827 while (src != inner)
5829 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5830 decl_piece_bitsize (*src), NULL_RTX);
5831 dest = &XEXP (*dest, 1);
5832 src = &XEXP (*src, 1);
5835 /* Add padding if needed. */
5836 if (bitpos != piece_bitpos)
5838 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5839 copy ? NULL_RTX : *dest);
5840 dest = &XEXP (*dest, 1);
5842 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5844 gcc_assert (!copy);
5845 /* A piece with correct bitpos and bitsize already exist,
5846 just update the location for it and return. */
5847 *decl_piece_varloc_ptr (*dest) = loc_note;
5848 return;
5850 /* Add the piece that changed. */
5851 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5852 dest = &XEXP (*dest, 1);
5853 /* Skip over pieces that overlap it. */
5854 diff = bitpos - piece_bitpos + bitsize;
5855 if (!copy)
5856 src = dest;
5857 while (diff > 0 && *src)
5859 rtx piece = *src;
5860 diff -= decl_piece_bitsize (piece);
5861 if (copy)
5862 src = &XEXP (piece, 1);
5863 else
5865 *src = XEXP (piece, 1);
5866 free_EXPR_LIST_node (piece);
5869 /* Add padding if needed. */
5870 if (diff < 0 && *src)
5872 if (!copy)
5873 dest = src;
5874 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5875 dest = &XEXP (*dest, 1);
5877 if (!copy)
5878 return;
5879 /* Finally copy all nodes following it. */
5880 while (*src)
5882 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5883 decl_piece_bitsize (*src), NULL_RTX);
5884 dest = &XEXP (*dest, 1);
5885 src = &XEXP (*src, 1);
5889 /* Add a variable location node to the linked list for DECL. */
5891 static struct var_loc_node *
5892 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5894 unsigned int decl_id;
5895 var_loc_list *temp;
5896 struct var_loc_node *loc = NULL;
5897 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5899 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5901 tree realdecl = DECL_DEBUG_EXPR (decl);
5902 if (handled_component_p (realdecl)
5903 || (TREE_CODE (realdecl) == MEM_REF
5904 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5906 HOST_WIDE_INT maxsize;
5907 bool reverse;
5908 tree innerdecl
5909 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5910 &reverse);
5911 if (!DECL_P (innerdecl)
5912 || DECL_IGNORED_P (innerdecl)
5913 || TREE_STATIC (innerdecl)
5914 || bitsize <= 0
5915 || bitpos + bitsize > 256
5916 || bitsize != maxsize)
5917 return NULL;
5918 decl = innerdecl;
5922 decl_id = DECL_UID (decl);
5923 var_loc_list **slot
5924 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5925 if (*slot == NULL)
5927 temp = ggc_cleared_alloc<var_loc_list> ();
5928 temp->decl_id = decl_id;
5929 *slot = temp;
5931 else
5932 temp = *slot;
5934 /* For PARM_DECLs try to keep around the original incoming value,
5935 even if that means we'll emit a zero-range .debug_loc entry. */
5936 if (temp->last
5937 && temp->first == temp->last
5938 && TREE_CODE (decl) == PARM_DECL
5939 && NOTE_P (temp->first->loc)
5940 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5941 && DECL_INCOMING_RTL (decl)
5942 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5943 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5944 == GET_CODE (DECL_INCOMING_RTL (decl))
5945 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5946 && (bitsize != -1
5947 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5948 NOTE_VAR_LOCATION_LOC (loc_note))
5949 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5950 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5952 loc = ggc_cleared_alloc<var_loc_node> ();
5953 temp->first->next = loc;
5954 temp->last = loc;
5955 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5957 else if (temp->last)
5959 struct var_loc_node *last = temp->last, *unused = NULL;
5960 rtx *piece_loc = NULL, last_loc_note;
5961 HOST_WIDE_INT piece_bitpos = 0;
5962 if (last->next)
5964 last = last->next;
5965 gcc_assert (last->next == NULL);
5967 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5969 piece_loc = &last->loc;
5972 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5973 if (piece_bitpos + cur_bitsize > bitpos)
5974 break;
5975 piece_bitpos += cur_bitsize;
5976 piece_loc = &XEXP (*piece_loc, 1);
5978 while (*piece_loc);
5980 /* TEMP->LAST here is either pointer to the last but one or
5981 last element in the chained list, LAST is pointer to the
5982 last element. */
5983 if (label && strcmp (last->label, label) == 0)
5985 /* For SRA optimized variables if there weren't any real
5986 insns since last note, just modify the last node. */
5987 if (piece_loc != NULL)
5989 adjust_piece_list (piece_loc, NULL, NULL,
5990 bitpos, piece_bitpos, bitsize, loc_note);
5991 return NULL;
5993 /* If the last note doesn't cover any instructions, remove it. */
5994 if (temp->last != last)
5996 temp->last->next = NULL;
5997 unused = last;
5998 last = temp->last;
5999 gcc_assert (strcmp (last->label, label) != 0);
6001 else
6003 gcc_assert (temp->first == temp->last
6004 || (temp->first->next == temp->last
6005 && TREE_CODE (decl) == PARM_DECL));
6006 memset (temp->last, '\0', sizeof (*temp->last));
6007 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6008 return temp->last;
6011 if (bitsize == -1 && NOTE_P (last->loc))
6012 last_loc_note = last->loc;
6013 else if (piece_loc != NULL
6014 && *piece_loc != NULL_RTX
6015 && piece_bitpos == bitpos
6016 && decl_piece_bitsize (*piece_loc) == bitsize)
6017 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6018 else
6019 last_loc_note = NULL_RTX;
6020 /* If the current location is the same as the end of the list,
6021 and either both or neither of the locations is uninitialized,
6022 we have nothing to do. */
6023 if (last_loc_note == NULL_RTX
6024 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6025 NOTE_VAR_LOCATION_LOC (loc_note)))
6026 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6027 != NOTE_VAR_LOCATION_STATUS (loc_note))
6028 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6029 == VAR_INIT_STATUS_UNINITIALIZED)
6030 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6031 == VAR_INIT_STATUS_UNINITIALIZED))))
6033 /* Add LOC to the end of list and update LAST. If the last
6034 element of the list has been removed above, reuse its
6035 memory for the new node, otherwise allocate a new one. */
6036 if (unused)
6038 loc = unused;
6039 memset (loc, '\0', sizeof (*loc));
6041 else
6042 loc = ggc_cleared_alloc<var_loc_node> ();
6043 if (bitsize == -1 || piece_loc == NULL)
6044 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6045 else
6046 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6047 bitpos, piece_bitpos, bitsize, loc_note);
6048 last->next = loc;
6049 /* Ensure TEMP->LAST will point either to the new last but one
6050 element of the chain, or to the last element in it. */
6051 if (last != temp->last)
6052 temp->last = last;
6054 else if (unused)
6055 ggc_free (unused);
6057 else
6059 loc = ggc_cleared_alloc<var_loc_node> ();
6060 temp->first = loc;
6061 temp->last = loc;
6062 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6064 return loc;
6067 /* Keep track of the number of spaces used to indent the
6068 output of the debugging routines that print the structure of
6069 the DIE internal representation. */
6070 static int print_indent;
6072 /* Indent the line the number of spaces given by print_indent. */
6074 static inline void
6075 print_spaces (FILE *outfile)
6077 fprintf (outfile, "%*s", print_indent, "");
6080 /* Print a type signature in hex. */
6082 static inline void
6083 print_signature (FILE *outfile, char *sig)
6085 int i;
6087 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6088 fprintf (outfile, "%02x", sig[i] & 0xff);
6091 static inline void
6092 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6094 if (discr_value->pos)
6095 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6096 else
6097 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6100 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6102 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6103 RECURSE, output location descriptor operations. */
6105 static void
6106 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6108 switch (val->val_class)
6110 case dw_val_class_addr:
6111 fprintf (outfile, "address");
6112 break;
6113 case dw_val_class_offset:
6114 fprintf (outfile, "offset");
6115 break;
6116 case dw_val_class_loc:
6117 fprintf (outfile, "location descriptor");
6118 if (val->v.val_loc == NULL)
6119 fprintf (outfile, " -> <null>\n");
6120 else if (recurse)
6122 fprintf (outfile, ":\n");
6123 print_indent += 4;
6124 print_loc_descr (val->v.val_loc, outfile);
6125 print_indent -= 4;
6127 else
6128 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6129 break;
6130 case dw_val_class_loc_list:
6131 fprintf (outfile, "location list -> label:%s",
6132 val->v.val_loc_list->ll_symbol);
6133 break;
6134 case dw_val_class_range_list:
6135 fprintf (outfile, "range list");
6136 break;
6137 case dw_val_class_const:
6138 case dw_val_class_const_implicit:
6139 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6140 break;
6141 case dw_val_class_unsigned_const:
6142 case dw_val_class_unsigned_const_implicit:
6143 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6144 break;
6145 case dw_val_class_const_double:
6146 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6147 HOST_WIDE_INT_PRINT_UNSIGNED")",
6148 val->v.val_double.high,
6149 val->v.val_double.low);
6150 break;
6151 case dw_val_class_wide_int:
6153 int i = val->v.val_wide->get_len ();
6154 fprintf (outfile, "constant (");
6155 gcc_assert (i > 0);
6156 if (val->v.val_wide->elt (i - 1) == 0)
6157 fprintf (outfile, "0x");
6158 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6159 val->v.val_wide->elt (--i));
6160 while (--i >= 0)
6161 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6162 val->v.val_wide->elt (i));
6163 fprintf (outfile, ")");
6164 break;
6166 case dw_val_class_vec:
6167 fprintf (outfile, "floating-point or vector constant");
6168 break;
6169 case dw_val_class_flag:
6170 fprintf (outfile, "%u", val->v.val_flag);
6171 break;
6172 case dw_val_class_die_ref:
6173 if (val->v.val_die_ref.die != NULL)
6175 dw_die_ref die = val->v.val_die_ref.die;
6177 if (die->comdat_type_p)
6179 fprintf (outfile, "die -> signature: ");
6180 print_signature (outfile,
6181 die->die_id.die_type_node->signature);
6183 else if (die->die_id.die_symbol)
6185 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6186 if (die->with_offset)
6187 fprintf (outfile, " + %ld", die->die_offset);
6189 else
6190 fprintf (outfile, "die -> %ld", die->die_offset);
6191 fprintf (outfile, " (%p)", (void *) die);
6193 else
6194 fprintf (outfile, "die -> <null>");
6195 break;
6196 case dw_val_class_vms_delta:
6197 fprintf (outfile, "delta: @slotcount(%s-%s)",
6198 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6199 break;
6200 case dw_val_class_lbl_id:
6201 case dw_val_class_lineptr:
6202 case dw_val_class_macptr:
6203 case dw_val_class_loclistsptr:
6204 case dw_val_class_high_pc:
6205 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6206 break;
6207 case dw_val_class_str:
6208 if (val->v.val_str->str != NULL)
6209 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6210 else
6211 fprintf (outfile, "<null>");
6212 break;
6213 case dw_val_class_file:
6214 case dw_val_class_file_implicit:
6215 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6216 val->v.val_file->emitted_number);
6217 break;
6218 case dw_val_class_data8:
6220 int i;
6222 for (i = 0; i < 8; i++)
6223 fprintf (outfile, "%02x", val->v.val_data8[i]);
6224 break;
6226 case dw_val_class_discr_value:
6227 print_discr_value (outfile, &val->v.val_discr_value);
6228 break;
6229 case dw_val_class_discr_list:
6230 for (dw_discr_list_ref node = val->v.val_discr_list;
6231 node != NULL;
6232 node = node->dw_discr_next)
6234 if (node->dw_discr_range)
6236 fprintf (outfile, " .. ");
6237 print_discr_value (outfile, &node->dw_discr_lower_bound);
6238 print_discr_value (outfile, &node->dw_discr_upper_bound);
6240 else
6241 print_discr_value (outfile, &node->dw_discr_lower_bound);
6243 if (node->dw_discr_next != NULL)
6244 fprintf (outfile, " | ");
6246 default:
6247 break;
6251 /* Likewise, for a DIE attribute. */
6253 static void
6254 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6256 print_dw_val (&a->dw_attr_val, recurse, outfile);
6260 /* Print the list of operands in the LOC location description to OUTFILE. This
6261 routine is a debugging aid only. */
6263 static void
6264 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6266 dw_loc_descr_ref l = loc;
6268 if (loc == NULL)
6270 print_spaces (outfile);
6271 fprintf (outfile, "<null>\n");
6272 return;
6275 for (l = loc; l != NULL; l = l->dw_loc_next)
6277 print_spaces (outfile);
6278 fprintf (outfile, "(%p) %s",
6279 (void *) l,
6280 dwarf_stack_op_name (l->dw_loc_opc));
6281 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6283 fprintf (outfile, " ");
6284 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6286 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6288 fprintf (outfile, ", ");
6289 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6291 fprintf (outfile, "\n");
6295 /* Print the information associated with a given DIE, and its children.
6296 This routine is a debugging aid only. */
6298 static void
6299 print_die (dw_die_ref die, FILE *outfile)
6301 dw_attr_node *a;
6302 dw_die_ref c;
6303 unsigned ix;
6305 print_spaces (outfile);
6306 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6307 die->die_offset, dwarf_tag_name (die->die_tag),
6308 (void*) die);
6309 print_spaces (outfile);
6310 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6311 fprintf (outfile, " offset: %ld", die->die_offset);
6312 fprintf (outfile, " mark: %d\n", die->die_mark);
6314 if (die->comdat_type_p)
6316 print_spaces (outfile);
6317 fprintf (outfile, " signature: ");
6318 print_signature (outfile, die->die_id.die_type_node->signature);
6319 fprintf (outfile, "\n");
6322 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6324 print_spaces (outfile);
6325 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6327 print_attribute (a, true, outfile);
6328 fprintf (outfile, "\n");
6331 if (die->die_child != NULL)
6333 print_indent += 4;
6334 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6335 print_indent -= 4;
6337 if (print_indent == 0)
6338 fprintf (outfile, "\n");
6341 /* Print the list of operations in the LOC location description. */
6343 DEBUG_FUNCTION void
6344 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6346 print_loc_descr (loc, stderr);
6349 /* Print the information collected for a given DIE. */
6351 DEBUG_FUNCTION void
6352 debug_dwarf_die (dw_die_ref die)
6354 print_die (die, stderr);
6357 DEBUG_FUNCTION void
6358 debug (die_struct &ref)
6360 print_die (&ref, stderr);
6363 DEBUG_FUNCTION void
6364 debug (die_struct *ptr)
6366 if (ptr)
6367 debug (*ptr);
6368 else
6369 fprintf (stderr, "<nil>\n");
6373 /* Print all DWARF information collected for the compilation unit.
6374 This routine is a debugging aid only. */
6376 DEBUG_FUNCTION void
6377 debug_dwarf (void)
6379 print_indent = 0;
6380 print_die (comp_unit_die (), stderr);
6383 /* Verify the DIE tree structure. */
6385 DEBUG_FUNCTION void
6386 verify_die (dw_die_ref die)
6388 gcc_assert (!die->die_mark);
6389 if (die->die_parent == NULL
6390 && die->die_sib == NULL)
6391 return;
6392 /* Verify the die_sib list is cyclic. */
6393 dw_die_ref x = die;
6396 x->die_mark = 1;
6397 x = x->die_sib;
6399 while (x && !x->die_mark);
6400 gcc_assert (x == die);
6401 x = die;
6404 /* Verify all dies have the same parent. */
6405 gcc_assert (x->die_parent == die->die_parent);
6406 if (x->die_child)
6408 /* Verify the child has the proper parent and recurse. */
6409 gcc_assert (x->die_child->die_parent == x);
6410 verify_die (x->die_child);
6412 x->die_mark = 0;
6413 x = x->die_sib;
6415 while (x && x->die_mark);
6418 /* Sanity checks on DIEs. */
6420 static void
6421 check_die (dw_die_ref die)
6423 unsigned ix;
6424 dw_attr_node *a;
6425 bool inline_found = false;
6426 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6427 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6428 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6430 switch (a->dw_attr)
6432 case DW_AT_inline:
6433 if (a->dw_attr_val.v.val_unsigned)
6434 inline_found = true;
6435 break;
6436 case DW_AT_location:
6437 ++n_location;
6438 break;
6439 case DW_AT_low_pc:
6440 ++n_low_pc;
6441 break;
6442 case DW_AT_high_pc:
6443 ++n_high_pc;
6444 break;
6445 case DW_AT_artificial:
6446 ++n_artificial;
6447 break;
6448 case DW_AT_decl_column:
6449 ++n_decl_column;
6450 break;
6451 case DW_AT_decl_line:
6452 ++n_decl_line;
6453 break;
6454 case DW_AT_decl_file:
6455 ++n_decl_file;
6456 break;
6457 default:
6458 break;
6461 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6462 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6464 fprintf (stderr, "Duplicate attributes in DIE:\n");
6465 debug_dwarf_die (die);
6466 gcc_unreachable ();
6468 if (inline_found)
6470 /* A debugging information entry that is a member of an abstract
6471 instance tree [that has DW_AT_inline] should not contain any
6472 attributes which describe aspects of the subroutine which vary
6473 between distinct inlined expansions or distinct out-of-line
6474 expansions. */
6475 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6476 gcc_assert (a->dw_attr != DW_AT_low_pc
6477 && a->dw_attr != DW_AT_high_pc
6478 && a->dw_attr != DW_AT_location
6479 && a->dw_attr != DW_AT_frame_base
6480 && a->dw_attr != DW_AT_call_all_calls
6481 && a->dw_attr != DW_AT_GNU_all_call_sites);
6485 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6486 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6487 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6489 /* Calculate the checksum of a location expression. */
6491 static inline void
6492 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6494 int tem;
6495 inchash::hash hstate;
6496 hashval_t hash;
6498 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6499 CHECKSUM (tem);
6500 hash_loc_operands (loc, hstate);
6501 hash = hstate.end();
6502 CHECKSUM (hash);
6505 /* Calculate the checksum of an attribute. */
6507 static void
6508 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6510 dw_loc_descr_ref loc;
6511 rtx r;
6513 CHECKSUM (at->dw_attr);
6515 /* We don't care that this was compiled with a different compiler
6516 snapshot; if the output is the same, that's what matters. */
6517 if (at->dw_attr == DW_AT_producer)
6518 return;
6520 switch (AT_class (at))
6522 case dw_val_class_const:
6523 case dw_val_class_const_implicit:
6524 CHECKSUM (at->dw_attr_val.v.val_int);
6525 break;
6526 case dw_val_class_unsigned_const:
6527 case dw_val_class_unsigned_const_implicit:
6528 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6529 break;
6530 case dw_val_class_const_double:
6531 CHECKSUM (at->dw_attr_val.v.val_double);
6532 break;
6533 case dw_val_class_wide_int:
6534 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6535 get_full_len (*at->dw_attr_val.v.val_wide)
6536 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6537 break;
6538 case dw_val_class_vec:
6539 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6540 (at->dw_attr_val.v.val_vec.length
6541 * at->dw_attr_val.v.val_vec.elt_size));
6542 break;
6543 case dw_val_class_flag:
6544 CHECKSUM (at->dw_attr_val.v.val_flag);
6545 break;
6546 case dw_val_class_str:
6547 CHECKSUM_STRING (AT_string (at));
6548 break;
6550 case dw_val_class_addr:
6551 r = AT_addr (at);
6552 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6553 CHECKSUM_STRING (XSTR (r, 0));
6554 break;
6556 case dw_val_class_offset:
6557 CHECKSUM (at->dw_attr_val.v.val_offset);
6558 break;
6560 case dw_val_class_loc:
6561 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6562 loc_checksum (loc, ctx);
6563 break;
6565 case dw_val_class_die_ref:
6566 die_checksum (AT_ref (at), ctx, mark);
6567 break;
6569 case dw_val_class_fde_ref:
6570 case dw_val_class_vms_delta:
6571 case dw_val_class_lbl_id:
6572 case dw_val_class_lineptr:
6573 case dw_val_class_macptr:
6574 case dw_val_class_loclistsptr:
6575 case dw_val_class_high_pc:
6576 break;
6578 case dw_val_class_file:
6579 case dw_val_class_file_implicit:
6580 CHECKSUM_STRING (AT_file (at)->filename);
6581 break;
6583 case dw_val_class_data8:
6584 CHECKSUM (at->dw_attr_val.v.val_data8);
6585 break;
6587 default:
6588 break;
6592 /* Calculate the checksum of a DIE. */
6594 static void
6595 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6597 dw_die_ref c;
6598 dw_attr_node *a;
6599 unsigned ix;
6601 /* To avoid infinite recursion. */
6602 if (die->die_mark)
6604 CHECKSUM (die->die_mark);
6605 return;
6607 die->die_mark = ++(*mark);
6609 CHECKSUM (die->die_tag);
6611 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6612 attr_checksum (a, ctx, mark);
6614 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6617 #undef CHECKSUM
6618 #undef CHECKSUM_BLOCK
6619 #undef CHECKSUM_STRING
6621 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6622 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6623 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6624 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6625 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6626 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6627 #define CHECKSUM_ATTR(FOO) \
6628 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6630 /* Calculate the checksum of a number in signed LEB128 format. */
6632 static void
6633 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6635 unsigned char byte;
6636 bool more;
6638 while (1)
6640 byte = (value & 0x7f);
6641 value >>= 7;
6642 more = !((value == 0 && (byte & 0x40) == 0)
6643 || (value == -1 && (byte & 0x40) != 0));
6644 if (more)
6645 byte |= 0x80;
6646 CHECKSUM (byte);
6647 if (!more)
6648 break;
6652 /* Calculate the checksum of a number in unsigned LEB128 format. */
6654 static void
6655 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6657 while (1)
6659 unsigned char byte = (value & 0x7f);
6660 value >>= 7;
6661 if (value != 0)
6662 /* More bytes to follow. */
6663 byte |= 0x80;
6664 CHECKSUM (byte);
6665 if (value == 0)
6666 break;
6670 /* Checksum the context of the DIE. This adds the names of any
6671 surrounding namespaces or structures to the checksum. */
6673 static void
6674 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6676 const char *name;
6677 dw_die_ref spec;
6678 int tag = die->die_tag;
6680 if (tag != DW_TAG_namespace
6681 && tag != DW_TAG_structure_type
6682 && tag != DW_TAG_class_type)
6683 return;
6685 name = get_AT_string (die, DW_AT_name);
6687 spec = get_AT_ref (die, DW_AT_specification);
6688 if (spec != NULL)
6689 die = spec;
6691 if (die->die_parent != NULL)
6692 checksum_die_context (die->die_parent, ctx);
6694 CHECKSUM_ULEB128 ('C');
6695 CHECKSUM_ULEB128 (tag);
6696 if (name != NULL)
6697 CHECKSUM_STRING (name);
6700 /* Calculate the checksum of a location expression. */
6702 static inline void
6703 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6705 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6706 were emitted as a DW_FORM_sdata instead of a location expression. */
6707 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6709 CHECKSUM_ULEB128 (DW_FORM_sdata);
6710 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6711 return;
6714 /* Otherwise, just checksum the raw location expression. */
6715 while (loc != NULL)
6717 inchash::hash hstate;
6718 hashval_t hash;
6720 CHECKSUM_ULEB128 (loc->dtprel);
6721 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6722 hash_loc_operands (loc, hstate);
6723 hash = hstate.end ();
6724 CHECKSUM (hash);
6725 loc = loc->dw_loc_next;
6729 /* Calculate the checksum of an attribute. */
6731 static void
6732 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6733 struct md5_ctx *ctx, int *mark)
6735 dw_loc_descr_ref loc;
6736 rtx r;
6738 if (AT_class (at) == dw_val_class_die_ref)
6740 dw_die_ref target_die = AT_ref (at);
6742 /* For pointer and reference types, we checksum only the (qualified)
6743 name of the target type (if there is a name). For friend entries,
6744 we checksum only the (qualified) name of the target type or function.
6745 This allows the checksum to remain the same whether the target type
6746 is complete or not. */
6747 if ((at->dw_attr == DW_AT_type
6748 && (tag == DW_TAG_pointer_type
6749 || tag == DW_TAG_reference_type
6750 || tag == DW_TAG_rvalue_reference_type
6751 || tag == DW_TAG_ptr_to_member_type))
6752 || (at->dw_attr == DW_AT_friend
6753 && tag == DW_TAG_friend))
6755 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6757 if (name_attr != NULL)
6759 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6761 if (decl == NULL)
6762 decl = target_die;
6763 CHECKSUM_ULEB128 ('N');
6764 CHECKSUM_ULEB128 (at->dw_attr);
6765 if (decl->die_parent != NULL)
6766 checksum_die_context (decl->die_parent, ctx);
6767 CHECKSUM_ULEB128 ('E');
6768 CHECKSUM_STRING (AT_string (name_attr));
6769 return;
6773 /* For all other references to another DIE, we check to see if the
6774 target DIE has already been visited. If it has, we emit a
6775 backward reference; if not, we descend recursively. */
6776 if (target_die->die_mark > 0)
6778 CHECKSUM_ULEB128 ('R');
6779 CHECKSUM_ULEB128 (at->dw_attr);
6780 CHECKSUM_ULEB128 (target_die->die_mark);
6782 else
6784 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6786 if (decl == NULL)
6787 decl = target_die;
6788 target_die->die_mark = ++(*mark);
6789 CHECKSUM_ULEB128 ('T');
6790 CHECKSUM_ULEB128 (at->dw_attr);
6791 if (decl->die_parent != NULL)
6792 checksum_die_context (decl->die_parent, ctx);
6793 die_checksum_ordered (target_die, ctx, mark);
6795 return;
6798 CHECKSUM_ULEB128 ('A');
6799 CHECKSUM_ULEB128 (at->dw_attr);
6801 switch (AT_class (at))
6803 case dw_val_class_const:
6804 case dw_val_class_const_implicit:
6805 CHECKSUM_ULEB128 (DW_FORM_sdata);
6806 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6807 break;
6809 case dw_val_class_unsigned_const:
6810 case dw_val_class_unsigned_const_implicit:
6811 CHECKSUM_ULEB128 (DW_FORM_sdata);
6812 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6813 break;
6815 case dw_val_class_const_double:
6816 CHECKSUM_ULEB128 (DW_FORM_block);
6817 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6818 CHECKSUM (at->dw_attr_val.v.val_double);
6819 break;
6821 case dw_val_class_wide_int:
6822 CHECKSUM_ULEB128 (DW_FORM_block);
6823 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6824 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6825 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6826 get_full_len (*at->dw_attr_val.v.val_wide)
6827 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6828 break;
6830 case dw_val_class_vec:
6831 CHECKSUM_ULEB128 (DW_FORM_block);
6832 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6833 * at->dw_attr_val.v.val_vec.elt_size);
6834 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6835 (at->dw_attr_val.v.val_vec.length
6836 * at->dw_attr_val.v.val_vec.elt_size));
6837 break;
6839 case dw_val_class_flag:
6840 CHECKSUM_ULEB128 (DW_FORM_flag);
6841 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6842 break;
6844 case dw_val_class_str:
6845 CHECKSUM_ULEB128 (DW_FORM_string);
6846 CHECKSUM_STRING (AT_string (at));
6847 break;
6849 case dw_val_class_addr:
6850 r = AT_addr (at);
6851 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6852 CHECKSUM_ULEB128 (DW_FORM_string);
6853 CHECKSUM_STRING (XSTR (r, 0));
6854 break;
6856 case dw_val_class_offset:
6857 CHECKSUM_ULEB128 (DW_FORM_sdata);
6858 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6859 break;
6861 case dw_val_class_loc:
6862 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6863 loc_checksum_ordered (loc, ctx);
6864 break;
6866 case dw_val_class_fde_ref:
6867 case dw_val_class_lbl_id:
6868 case dw_val_class_lineptr:
6869 case dw_val_class_macptr:
6870 case dw_val_class_loclistsptr:
6871 case dw_val_class_high_pc:
6872 break;
6874 case dw_val_class_file:
6875 case dw_val_class_file_implicit:
6876 CHECKSUM_ULEB128 (DW_FORM_string);
6877 CHECKSUM_STRING (AT_file (at)->filename);
6878 break;
6880 case dw_val_class_data8:
6881 CHECKSUM (at->dw_attr_val.v.val_data8);
6882 break;
6884 default:
6885 break;
6889 struct checksum_attributes
6891 dw_attr_node *at_name;
6892 dw_attr_node *at_type;
6893 dw_attr_node *at_friend;
6894 dw_attr_node *at_accessibility;
6895 dw_attr_node *at_address_class;
6896 dw_attr_node *at_alignment;
6897 dw_attr_node *at_allocated;
6898 dw_attr_node *at_artificial;
6899 dw_attr_node *at_associated;
6900 dw_attr_node *at_binary_scale;
6901 dw_attr_node *at_bit_offset;
6902 dw_attr_node *at_bit_size;
6903 dw_attr_node *at_bit_stride;
6904 dw_attr_node *at_byte_size;
6905 dw_attr_node *at_byte_stride;
6906 dw_attr_node *at_const_value;
6907 dw_attr_node *at_containing_type;
6908 dw_attr_node *at_count;
6909 dw_attr_node *at_data_location;
6910 dw_attr_node *at_data_member_location;
6911 dw_attr_node *at_decimal_scale;
6912 dw_attr_node *at_decimal_sign;
6913 dw_attr_node *at_default_value;
6914 dw_attr_node *at_digit_count;
6915 dw_attr_node *at_discr;
6916 dw_attr_node *at_discr_list;
6917 dw_attr_node *at_discr_value;
6918 dw_attr_node *at_encoding;
6919 dw_attr_node *at_endianity;
6920 dw_attr_node *at_explicit;
6921 dw_attr_node *at_is_optional;
6922 dw_attr_node *at_location;
6923 dw_attr_node *at_lower_bound;
6924 dw_attr_node *at_mutable;
6925 dw_attr_node *at_ordering;
6926 dw_attr_node *at_picture_string;
6927 dw_attr_node *at_prototyped;
6928 dw_attr_node *at_small;
6929 dw_attr_node *at_segment;
6930 dw_attr_node *at_string_length;
6931 dw_attr_node *at_string_length_bit_size;
6932 dw_attr_node *at_string_length_byte_size;
6933 dw_attr_node *at_threads_scaled;
6934 dw_attr_node *at_upper_bound;
6935 dw_attr_node *at_use_location;
6936 dw_attr_node *at_use_UTF8;
6937 dw_attr_node *at_variable_parameter;
6938 dw_attr_node *at_virtuality;
6939 dw_attr_node *at_visibility;
6940 dw_attr_node *at_vtable_elem_location;
6943 /* Collect the attributes that we will want to use for the checksum. */
6945 static void
6946 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6948 dw_attr_node *a;
6949 unsigned ix;
6951 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6953 switch (a->dw_attr)
6955 case DW_AT_name:
6956 attrs->at_name = a;
6957 break;
6958 case DW_AT_type:
6959 attrs->at_type = a;
6960 break;
6961 case DW_AT_friend:
6962 attrs->at_friend = a;
6963 break;
6964 case DW_AT_accessibility:
6965 attrs->at_accessibility = a;
6966 break;
6967 case DW_AT_address_class:
6968 attrs->at_address_class = a;
6969 break;
6970 case DW_AT_alignment:
6971 attrs->at_alignment = a;
6972 break;
6973 case DW_AT_allocated:
6974 attrs->at_allocated = a;
6975 break;
6976 case DW_AT_artificial:
6977 attrs->at_artificial = a;
6978 break;
6979 case DW_AT_associated:
6980 attrs->at_associated = a;
6981 break;
6982 case DW_AT_binary_scale:
6983 attrs->at_binary_scale = a;
6984 break;
6985 case DW_AT_bit_offset:
6986 attrs->at_bit_offset = a;
6987 break;
6988 case DW_AT_bit_size:
6989 attrs->at_bit_size = a;
6990 break;
6991 case DW_AT_bit_stride:
6992 attrs->at_bit_stride = a;
6993 break;
6994 case DW_AT_byte_size:
6995 attrs->at_byte_size = a;
6996 break;
6997 case DW_AT_byte_stride:
6998 attrs->at_byte_stride = a;
6999 break;
7000 case DW_AT_const_value:
7001 attrs->at_const_value = a;
7002 break;
7003 case DW_AT_containing_type:
7004 attrs->at_containing_type = a;
7005 break;
7006 case DW_AT_count:
7007 attrs->at_count = a;
7008 break;
7009 case DW_AT_data_location:
7010 attrs->at_data_location = a;
7011 break;
7012 case DW_AT_data_member_location:
7013 attrs->at_data_member_location = a;
7014 break;
7015 case DW_AT_decimal_scale:
7016 attrs->at_decimal_scale = a;
7017 break;
7018 case DW_AT_decimal_sign:
7019 attrs->at_decimal_sign = a;
7020 break;
7021 case DW_AT_default_value:
7022 attrs->at_default_value = a;
7023 break;
7024 case DW_AT_digit_count:
7025 attrs->at_digit_count = a;
7026 break;
7027 case DW_AT_discr:
7028 attrs->at_discr = a;
7029 break;
7030 case DW_AT_discr_list:
7031 attrs->at_discr_list = a;
7032 break;
7033 case DW_AT_discr_value:
7034 attrs->at_discr_value = a;
7035 break;
7036 case DW_AT_encoding:
7037 attrs->at_encoding = a;
7038 break;
7039 case DW_AT_endianity:
7040 attrs->at_endianity = a;
7041 break;
7042 case DW_AT_explicit:
7043 attrs->at_explicit = a;
7044 break;
7045 case DW_AT_is_optional:
7046 attrs->at_is_optional = a;
7047 break;
7048 case DW_AT_location:
7049 attrs->at_location = a;
7050 break;
7051 case DW_AT_lower_bound:
7052 attrs->at_lower_bound = a;
7053 break;
7054 case DW_AT_mutable:
7055 attrs->at_mutable = a;
7056 break;
7057 case DW_AT_ordering:
7058 attrs->at_ordering = a;
7059 break;
7060 case DW_AT_picture_string:
7061 attrs->at_picture_string = a;
7062 break;
7063 case DW_AT_prototyped:
7064 attrs->at_prototyped = a;
7065 break;
7066 case DW_AT_small:
7067 attrs->at_small = a;
7068 break;
7069 case DW_AT_segment:
7070 attrs->at_segment = a;
7071 break;
7072 case DW_AT_string_length:
7073 attrs->at_string_length = a;
7074 break;
7075 case DW_AT_string_length_bit_size:
7076 attrs->at_string_length_bit_size = a;
7077 break;
7078 case DW_AT_string_length_byte_size:
7079 attrs->at_string_length_byte_size = a;
7080 break;
7081 case DW_AT_threads_scaled:
7082 attrs->at_threads_scaled = a;
7083 break;
7084 case DW_AT_upper_bound:
7085 attrs->at_upper_bound = a;
7086 break;
7087 case DW_AT_use_location:
7088 attrs->at_use_location = a;
7089 break;
7090 case DW_AT_use_UTF8:
7091 attrs->at_use_UTF8 = a;
7092 break;
7093 case DW_AT_variable_parameter:
7094 attrs->at_variable_parameter = a;
7095 break;
7096 case DW_AT_virtuality:
7097 attrs->at_virtuality = a;
7098 break;
7099 case DW_AT_visibility:
7100 attrs->at_visibility = a;
7101 break;
7102 case DW_AT_vtable_elem_location:
7103 attrs->at_vtable_elem_location = a;
7104 break;
7105 default:
7106 break;
7111 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7113 static void
7114 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7116 dw_die_ref c;
7117 dw_die_ref decl;
7118 struct checksum_attributes attrs;
7120 CHECKSUM_ULEB128 ('D');
7121 CHECKSUM_ULEB128 (die->die_tag);
7123 memset (&attrs, 0, sizeof (attrs));
7125 decl = get_AT_ref (die, DW_AT_specification);
7126 if (decl != NULL)
7127 collect_checksum_attributes (&attrs, decl);
7128 collect_checksum_attributes (&attrs, die);
7130 CHECKSUM_ATTR (attrs.at_name);
7131 CHECKSUM_ATTR (attrs.at_accessibility);
7132 CHECKSUM_ATTR (attrs.at_address_class);
7133 CHECKSUM_ATTR (attrs.at_allocated);
7134 CHECKSUM_ATTR (attrs.at_artificial);
7135 CHECKSUM_ATTR (attrs.at_associated);
7136 CHECKSUM_ATTR (attrs.at_binary_scale);
7137 CHECKSUM_ATTR (attrs.at_bit_offset);
7138 CHECKSUM_ATTR (attrs.at_bit_size);
7139 CHECKSUM_ATTR (attrs.at_bit_stride);
7140 CHECKSUM_ATTR (attrs.at_byte_size);
7141 CHECKSUM_ATTR (attrs.at_byte_stride);
7142 CHECKSUM_ATTR (attrs.at_const_value);
7143 CHECKSUM_ATTR (attrs.at_containing_type);
7144 CHECKSUM_ATTR (attrs.at_count);
7145 CHECKSUM_ATTR (attrs.at_data_location);
7146 CHECKSUM_ATTR (attrs.at_data_member_location);
7147 CHECKSUM_ATTR (attrs.at_decimal_scale);
7148 CHECKSUM_ATTR (attrs.at_decimal_sign);
7149 CHECKSUM_ATTR (attrs.at_default_value);
7150 CHECKSUM_ATTR (attrs.at_digit_count);
7151 CHECKSUM_ATTR (attrs.at_discr);
7152 CHECKSUM_ATTR (attrs.at_discr_list);
7153 CHECKSUM_ATTR (attrs.at_discr_value);
7154 CHECKSUM_ATTR (attrs.at_encoding);
7155 CHECKSUM_ATTR (attrs.at_endianity);
7156 CHECKSUM_ATTR (attrs.at_explicit);
7157 CHECKSUM_ATTR (attrs.at_is_optional);
7158 CHECKSUM_ATTR (attrs.at_location);
7159 CHECKSUM_ATTR (attrs.at_lower_bound);
7160 CHECKSUM_ATTR (attrs.at_mutable);
7161 CHECKSUM_ATTR (attrs.at_ordering);
7162 CHECKSUM_ATTR (attrs.at_picture_string);
7163 CHECKSUM_ATTR (attrs.at_prototyped);
7164 CHECKSUM_ATTR (attrs.at_small);
7165 CHECKSUM_ATTR (attrs.at_segment);
7166 CHECKSUM_ATTR (attrs.at_string_length);
7167 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7168 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7169 CHECKSUM_ATTR (attrs.at_threads_scaled);
7170 CHECKSUM_ATTR (attrs.at_upper_bound);
7171 CHECKSUM_ATTR (attrs.at_use_location);
7172 CHECKSUM_ATTR (attrs.at_use_UTF8);
7173 CHECKSUM_ATTR (attrs.at_variable_parameter);
7174 CHECKSUM_ATTR (attrs.at_virtuality);
7175 CHECKSUM_ATTR (attrs.at_visibility);
7176 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7177 CHECKSUM_ATTR (attrs.at_type);
7178 CHECKSUM_ATTR (attrs.at_friend);
7179 CHECKSUM_ATTR (attrs.at_alignment);
7181 /* Checksum the child DIEs. */
7182 c = die->die_child;
7183 if (c) do {
7184 dw_attr_node *name_attr;
7186 c = c->die_sib;
7187 name_attr = get_AT (c, DW_AT_name);
7188 if (is_template_instantiation (c))
7190 /* Ignore instantiations of member type and function templates. */
7192 else if (name_attr != NULL
7193 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7195 /* Use a shallow checksum for named nested types and member
7196 functions. */
7197 CHECKSUM_ULEB128 ('S');
7198 CHECKSUM_ULEB128 (c->die_tag);
7199 CHECKSUM_STRING (AT_string (name_attr));
7201 else
7203 /* Use a deep checksum for other children. */
7204 /* Mark this DIE so it gets processed when unmarking. */
7205 if (c->die_mark == 0)
7206 c->die_mark = -1;
7207 die_checksum_ordered (c, ctx, mark);
7209 } while (c != die->die_child);
7211 CHECKSUM_ULEB128 (0);
7214 /* Add a type name and tag to a hash. */
7215 static void
7216 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7218 CHECKSUM_ULEB128 (tag);
7219 CHECKSUM_STRING (name);
7222 #undef CHECKSUM
7223 #undef CHECKSUM_STRING
7224 #undef CHECKSUM_ATTR
7225 #undef CHECKSUM_LEB128
7226 #undef CHECKSUM_ULEB128
7228 /* Generate the type signature for DIE. This is computed by generating an
7229 MD5 checksum over the DIE's tag, its relevant attributes, and its
7230 children. Attributes that are references to other DIEs are processed
7231 by recursion, using the MARK field to prevent infinite recursion.
7232 If the DIE is nested inside a namespace or another type, we also
7233 need to include that context in the signature. The lower 64 bits
7234 of the resulting MD5 checksum comprise the signature. */
7236 static void
7237 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7239 int mark;
7240 const char *name;
7241 unsigned char checksum[16];
7242 struct md5_ctx ctx;
7243 dw_die_ref decl;
7244 dw_die_ref parent;
7246 name = get_AT_string (die, DW_AT_name);
7247 decl = get_AT_ref (die, DW_AT_specification);
7248 parent = get_die_parent (die);
7250 /* First, compute a signature for just the type name (and its surrounding
7251 context, if any. This is stored in the type unit DIE for link-time
7252 ODR (one-definition rule) checking. */
7254 if (is_cxx () && name != NULL)
7256 md5_init_ctx (&ctx);
7258 /* Checksum the names of surrounding namespaces and structures. */
7259 if (parent != NULL)
7260 checksum_die_context (parent, &ctx);
7262 /* Checksum the current DIE. */
7263 die_odr_checksum (die->die_tag, name, &ctx);
7264 md5_finish_ctx (&ctx, checksum);
7266 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7269 /* Next, compute the complete type signature. */
7271 md5_init_ctx (&ctx);
7272 mark = 1;
7273 die->die_mark = mark;
7275 /* Checksum the names of surrounding namespaces and structures. */
7276 if (parent != NULL)
7277 checksum_die_context (parent, &ctx);
7279 /* Checksum the DIE and its children. */
7280 die_checksum_ordered (die, &ctx, &mark);
7281 unmark_all_dies (die);
7282 md5_finish_ctx (&ctx, checksum);
7284 /* Store the signature in the type node and link the type DIE and the
7285 type node together. */
7286 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7287 DWARF_TYPE_SIGNATURE_SIZE);
7288 die->comdat_type_p = true;
7289 die->die_id.die_type_node = type_node;
7290 type_node->type_die = die;
7292 /* If the DIE is a specification, link its declaration to the type node
7293 as well. */
7294 if (decl != NULL)
7296 decl->comdat_type_p = true;
7297 decl->die_id.die_type_node = type_node;
7301 /* Do the location expressions look same? */
7302 static inline int
7303 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7305 return loc1->dw_loc_opc == loc2->dw_loc_opc
7306 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7307 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7310 /* Do the values look the same? */
7311 static int
7312 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7314 dw_loc_descr_ref loc1, loc2;
7315 rtx r1, r2;
7317 if (v1->val_class != v2->val_class)
7318 return 0;
7320 switch (v1->val_class)
7322 case dw_val_class_const:
7323 case dw_val_class_const_implicit:
7324 return v1->v.val_int == v2->v.val_int;
7325 case dw_val_class_unsigned_const:
7326 case dw_val_class_unsigned_const_implicit:
7327 return v1->v.val_unsigned == v2->v.val_unsigned;
7328 case dw_val_class_const_double:
7329 return v1->v.val_double.high == v2->v.val_double.high
7330 && v1->v.val_double.low == v2->v.val_double.low;
7331 case dw_val_class_wide_int:
7332 return *v1->v.val_wide == *v2->v.val_wide;
7333 case dw_val_class_vec:
7334 if (v1->v.val_vec.length != v2->v.val_vec.length
7335 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7336 return 0;
7337 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7338 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7339 return 0;
7340 return 1;
7341 case dw_val_class_flag:
7342 return v1->v.val_flag == v2->v.val_flag;
7343 case dw_val_class_str:
7344 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7346 case dw_val_class_addr:
7347 r1 = v1->v.val_addr;
7348 r2 = v2->v.val_addr;
7349 if (GET_CODE (r1) != GET_CODE (r2))
7350 return 0;
7351 return !rtx_equal_p (r1, r2);
7353 case dw_val_class_offset:
7354 return v1->v.val_offset == v2->v.val_offset;
7356 case dw_val_class_loc:
7357 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7358 loc1 && loc2;
7359 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7360 if (!same_loc_p (loc1, loc2, mark))
7361 return 0;
7362 return !loc1 && !loc2;
7364 case dw_val_class_die_ref:
7365 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7367 case dw_val_class_fde_ref:
7368 case dw_val_class_vms_delta:
7369 case dw_val_class_lbl_id:
7370 case dw_val_class_lineptr:
7371 case dw_val_class_macptr:
7372 case dw_val_class_loclistsptr:
7373 case dw_val_class_high_pc:
7374 return 1;
7376 case dw_val_class_file:
7377 case dw_val_class_file_implicit:
7378 return v1->v.val_file == v2->v.val_file;
7380 case dw_val_class_data8:
7381 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7383 default:
7384 return 1;
7388 /* Do the attributes look the same? */
7390 static int
7391 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7393 if (at1->dw_attr != at2->dw_attr)
7394 return 0;
7396 /* We don't care that this was compiled with a different compiler
7397 snapshot; if the output is the same, that's what matters. */
7398 if (at1->dw_attr == DW_AT_producer)
7399 return 1;
7401 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7404 /* Do the dies look the same? */
7406 static int
7407 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7409 dw_die_ref c1, c2;
7410 dw_attr_node *a1;
7411 unsigned ix;
7413 /* To avoid infinite recursion. */
7414 if (die1->die_mark)
7415 return die1->die_mark == die2->die_mark;
7416 die1->die_mark = die2->die_mark = ++(*mark);
7418 if (die1->die_tag != die2->die_tag)
7419 return 0;
7421 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7422 return 0;
7424 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7425 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7426 return 0;
7428 c1 = die1->die_child;
7429 c2 = die2->die_child;
7430 if (! c1)
7432 if (c2)
7433 return 0;
7435 else
7436 for (;;)
7438 if (!same_die_p (c1, c2, mark))
7439 return 0;
7440 c1 = c1->die_sib;
7441 c2 = c2->die_sib;
7442 if (c1 == die1->die_child)
7444 if (c2 == die2->die_child)
7445 break;
7446 else
7447 return 0;
7451 return 1;
7454 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7455 children, and set die_symbol. */
7457 static void
7458 compute_comp_unit_symbol (dw_die_ref unit_die)
7460 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7461 const char *base = die_name ? lbasename (die_name) : "anonymous";
7462 char *name = XALLOCAVEC (char, strlen (base) + 64);
7463 char *p;
7464 int i, mark;
7465 unsigned char checksum[16];
7466 struct md5_ctx ctx;
7468 /* Compute the checksum of the DIE, then append part of it as hex digits to
7469 the name filename of the unit. */
7471 md5_init_ctx (&ctx);
7472 mark = 0;
7473 die_checksum (unit_die, &ctx, &mark);
7474 unmark_all_dies (unit_die);
7475 md5_finish_ctx (&ctx, checksum);
7477 /* When we this for comp_unit_die () we have a DW_AT_name that might
7478 not start with a letter but with anything valid for filenames and
7479 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7480 character is not a letter. */
7481 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7482 clean_symbol_name (name);
7484 p = name + strlen (name);
7485 for (i = 0; i < 4; i++)
7487 sprintf (p, "%.2x", checksum[i]);
7488 p += 2;
7491 unit_die->die_id.die_symbol = xstrdup (name);
7494 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7496 static int
7497 is_type_die (dw_die_ref die)
7499 switch (die->die_tag)
7501 case DW_TAG_array_type:
7502 case DW_TAG_class_type:
7503 case DW_TAG_interface_type:
7504 case DW_TAG_enumeration_type:
7505 case DW_TAG_pointer_type:
7506 case DW_TAG_reference_type:
7507 case DW_TAG_rvalue_reference_type:
7508 case DW_TAG_string_type:
7509 case DW_TAG_structure_type:
7510 case DW_TAG_subroutine_type:
7511 case DW_TAG_union_type:
7512 case DW_TAG_ptr_to_member_type:
7513 case DW_TAG_set_type:
7514 case DW_TAG_subrange_type:
7515 case DW_TAG_base_type:
7516 case DW_TAG_const_type:
7517 case DW_TAG_file_type:
7518 case DW_TAG_packed_type:
7519 case DW_TAG_volatile_type:
7520 case DW_TAG_typedef:
7521 return 1;
7522 default:
7523 return 0;
7527 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7528 Basically, we want to choose the bits that are likely to be shared between
7529 compilations (types) and leave out the bits that are specific to individual
7530 compilations (functions). */
7532 static int
7533 is_comdat_die (dw_die_ref c)
7535 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7536 we do for stabs. The advantage is a greater likelihood of sharing between
7537 objects that don't include headers in the same order (and therefore would
7538 put the base types in a different comdat). jason 8/28/00 */
7540 if (c->die_tag == DW_TAG_base_type)
7541 return 0;
7543 if (c->die_tag == DW_TAG_pointer_type
7544 || c->die_tag == DW_TAG_reference_type
7545 || c->die_tag == DW_TAG_rvalue_reference_type
7546 || c->die_tag == DW_TAG_const_type
7547 || c->die_tag == DW_TAG_volatile_type)
7549 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7551 return t ? is_comdat_die (t) : 0;
7554 return is_type_die (c);
7557 /* Returns true iff C is a compile-unit DIE. */
7559 static inline bool
7560 is_cu_die (dw_die_ref c)
7562 return c && (c->die_tag == DW_TAG_compile_unit
7563 || c->die_tag == DW_TAG_skeleton_unit);
7566 /* Returns true iff C is a unit DIE of some sort. */
7568 static inline bool
7569 is_unit_die (dw_die_ref c)
7571 return c && (c->die_tag == DW_TAG_compile_unit
7572 || c->die_tag == DW_TAG_partial_unit
7573 || c->die_tag == DW_TAG_type_unit
7574 || c->die_tag == DW_TAG_skeleton_unit);
7577 /* Returns true iff C is a namespace DIE. */
7579 static inline bool
7580 is_namespace_die (dw_die_ref c)
7582 return c && c->die_tag == DW_TAG_namespace;
7585 /* Returns true iff C is a class or structure DIE. */
7587 static inline bool
7588 is_class_die (dw_die_ref c)
7590 return c && (c->die_tag == DW_TAG_class_type
7591 || c->die_tag == DW_TAG_structure_type);
7594 /* Return non-zero if this DIE is a template parameter. */
7596 static inline bool
7597 is_template_parameter (dw_die_ref die)
7599 switch (die->die_tag)
7601 case DW_TAG_template_type_param:
7602 case DW_TAG_template_value_param:
7603 case DW_TAG_GNU_template_template_param:
7604 case DW_TAG_GNU_template_parameter_pack:
7605 return true;
7606 default:
7607 return false;
7611 /* Return non-zero if this DIE represents a template instantiation. */
7613 static inline bool
7614 is_template_instantiation (dw_die_ref die)
7616 dw_die_ref c;
7618 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7619 return false;
7620 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7621 return false;
7624 static char *
7625 gen_internal_sym (const char *prefix)
7627 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7629 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7630 return xstrdup (buf);
7633 /* Return non-zero if this DIE is a declaration. */
7635 static int
7636 is_declaration_die (dw_die_ref die)
7638 dw_attr_node *a;
7639 unsigned ix;
7641 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7642 if (a->dw_attr == DW_AT_declaration)
7643 return 1;
7645 return 0;
7648 /* Return non-zero if this DIE is nested inside a subprogram. */
7650 static int
7651 is_nested_in_subprogram (dw_die_ref die)
7653 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7655 if (decl == NULL)
7656 decl = die;
7657 return local_scope_p (decl);
7660 /* Return non-zero if this DIE contains a defining declaration of a
7661 subprogram. */
7663 static int
7664 contains_subprogram_definition (dw_die_ref die)
7666 dw_die_ref c;
7668 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7669 return 1;
7670 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7671 return 0;
7674 /* Return non-zero if this is a type DIE that should be moved to a
7675 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7676 unit type. */
7678 static int
7679 should_move_die_to_comdat (dw_die_ref die)
7681 switch (die->die_tag)
7683 case DW_TAG_class_type:
7684 case DW_TAG_structure_type:
7685 case DW_TAG_enumeration_type:
7686 case DW_TAG_union_type:
7687 /* Don't move declarations, inlined instances, types nested in a
7688 subprogram, or types that contain subprogram definitions. */
7689 if (is_declaration_die (die)
7690 || get_AT (die, DW_AT_abstract_origin)
7691 || is_nested_in_subprogram (die)
7692 || contains_subprogram_definition (die))
7693 return 0;
7694 return 1;
7695 case DW_TAG_array_type:
7696 case DW_TAG_interface_type:
7697 case DW_TAG_pointer_type:
7698 case DW_TAG_reference_type:
7699 case DW_TAG_rvalue_reference_type:
7700 case DW_TAG_string_type:
7701 case DW_TAG_subroutine_type:
7702 case DW_TAG_ptr_to_member_type:
7703 case DW_TAG_set_type:
7704 case DW_TAG_subrange_type:
7705 case DW_TAG_base_type:
7706 case DW_TAG_const_type:
7707 case DW_TAG_file_type:
7708 case DW_TAG_packed_type:
7709 case DW_TAG_volatile_type:
7710 case DW_TAG_typedef:
7711 default:
7712 return 0;
7716 /* Make a clone of DIE. */
7718 static dw_die_ref
7719 clone_die (dw_die_ref die)
7721 dw_die_ref clone = new_die_raw (die->die_tag);
7722 dw_attr_node *a;
7723 unsigned ix;
7725 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7726 add_dwarf_attr (clone, a);
7728 return clone;
7731 /* Make a clone of the tree rooted at DIE. */
7733 static dw_die_ref
7734 clone_tree (dw_die_ref die)
7736 dw_die_ref c;
7737 dw_die_ref clone = clone_die (die);
7739 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7741 return clone;
7744 /* Make a clone of DIE as a declaration. */
7746 static dw_die_ref
7747 clone_as_declaration (dw_die_ref die)
7749 dw_die_ref clone;
7750 dw_die_ref decl;
7751 dw_attr_node *a;
7752 unsigned ix;
7754 /* If the DIE is already a declaration, just clone it. */
7755 if (is_declaration_die (die))
7756 return clone_die (die);
7758 /* If the DIE is a specification, just clone its declaration DIE. */
7759 decl = get_AT_ref (die, DW_AT_specification);
7760 if (decl != NULL)
7762 clone = clone_die (decl);
7763 if (die->comdat_type_p)
7764 add_AT_die_ref (clone, DW_AT_signature, die);
7765 return clone;
7768 clone = new_die_raw (die->die_tag);
7770 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7772 /* We don't want to copy over all attributes.
7773 For example we don't want DW_AT_byte_size because otherwise we will no
7774 longer have a declaration and GDB will treat it as a definition. */
7776 switch (a->dw_attr)
7778 case DW_AT_abstract_origin:
7779 case DW_AT_artificial:
7780 case DW_AT_containing_type:
7781 case DW_AT_external:
7782 case DW_AT_name:
7783 case DW_AT_type:
7784 case DW_AT_virtuality:
7785 case DW_AT_linkage_name:
7786 case DW_AT_MIPS_linkage_name:
7787 add_dwarf_attr (clone, a);
7788 break;
7789 case DW_AT_byte_size:
7790 case DW_AT_alignment:
7791 default:
7792 break;
7796 if (die->comdat_type_p)
7797 add_AT_die_ref (clone, DW_AT_signature, die);
7799 add_AT_flag (clone, DW_AT_declaration, 1);
7800 return clone;
7804 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7806 struct decl_table_entry
7808 dw_die_ref orig;
7809 dw_die_ref copy;
7812 /* Helpers to manipulate hash table of copied declarations. */
7814 /* Hashtable helpers. */
7816 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7818 typedef die_struct *compare_type;
7819 static inline hashval_t hash (const decl_table_entry *);
7820 static inline bool equal (const decl_table_entry *, const die_struct *);
7823 inline hashval_t
7824 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7826 return htab_hash_pointer (entry->orig);
7829 inline bool
7830 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7831 const die_struct *entry2)
7833 return entry1->orig == entry2;
7836 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7838 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7839 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7840 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7841 to check if the ancestor has already been copied into UNIT. */
7843 static dw_die_ref
7844 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7845 decl_hash_type *decl_table)
7847 dw_die_ref parent = die->die_parent;
7848 dw_die_ref new_parent = unit;
7849 dw_die_ref copy;
7850 decl_table_entry **slot = NULL;
7851 struct decl_table_entry *entry = NULL;
7853 if (decl_table)
7855 /* Check if the entry has already been copied to UNIT. */
7856 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7857 INSERT);
7858 if (*slot != HTAB_EMPTY_ENTRY)
7860 entry = *slot;
7861 return entry->copy;
7864 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7865 entry = XCNEW (struct decl_table_entry);
7866 entry->orig = die;
7867 entry->copy = NULL;
7868 *slot = entry;
7871 if (parent != NULL)
7873 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7874 if (spec != NULL)
7875 parent = spec;
7876 if (!is_unit_die (parent))
7877 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7880 copy = clone_as_declaration (die);
7881 add_child_die (new_parent, copy);
7883 if (decl_table)
7885 /* Record the pointer to the copy. */
7886 entry->copy = copy;
7889 return copy;
7891 /* Copy the declaration context to the new type unit DIE. This includes
7892 any surrounding namespace or type declarations. If the DIE has an
7893 AT_specification attribute, it also includes attributes and children
7894 attached to the specification, and returns a pointer to the original
7895 parent of the declaration DIE. Returns NULL otherwise. */
7897 static dw_die_ref
7898 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7900 dw_die_ref decl;
7901 dw_die_ref new_decl;
7902 dw_die_ref orig_parent = NULL;
7904 decl = get_AT_ref (die, DW_AT_specification);
7905 if (decl == NULL)
7906 decl = die;
7907 else
7909 unsigned ix;
7910 dw_die_ref c;
7911 dw_attr_node *a;
7913 /* The original DIE will be changed to a declaration, and must
7914 be moved to be a child of the original declaration DIE. */
7915 orig_parent = decl->die_parent;
7917 /* Copy the type node pointer from the new DIE to the original
7918 declaration DIE so we can forward references later. */
7919 decl->comdat_type_p = true;
7920 decl->die_id.die_type_node = die->die_id.die_type_node;
7922 remove_AT (die, DW_AT_specification);
7924 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7926 if (a->dw_attr != DW_AT_name
7927 && a->dw_attr != DW_AT_declaration
7928 && a->dw_attr != DW_AT_external)
7929 add_dwarf_attr (die, a);
7932 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7935 if (decl->die_parent != NULL
7936 && !is_unit_die (decl->die_parent))
7938 new_decl = copy_ancestor_tree (unit, decl, NULL);
7939 if (new_decl != NULL)
7941 remove_AT (new_decl, DW_AT_signature);
7942 add_AT_specification (die, new_decl);
7946 return orig_parent;
7949 /* Generate the skeleton ancestor tree for the given NODE, then clone
7950 the DIE and add the clone into the tree. */
7952 static void
7953 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7955 if (node->new_die != NULL)
7956 return;
7958 node->new_die = clone_as_declaration (node->old_die);
7960 if (node->parent != NULL)
7962 generate_skeleton_ancestor_tree (node->parent);
7963 add_child_die (node->parent->new_die, node->new_die);
7967 /* Generate a skeleton tree of DIEs containing any declarations that are
7968 found in the original tree. We traverse the tree looking for declaration
7969 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7971 static void
7972 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7974 skeleton_chain_node node;
7975 dw_die_ref c;
7976 dw_die_ref first;
7977 dw_die_ref prev = NULL;
7978 dw_die_ref next = NULL;
7980 node.parent = parent;
7982 first = c = parent->old_die->die_child;
7983 if (c)
7984 next = c->die_sib;
7985 if (c) do {
7986 if (prev == NULL || prev->die_sib == c)
7987 prev = c;
7988 c = next;
7989 next = (c == first ? NULL : c->die_sib);
7990 node.old_die = c;
7991 node.new_die = NULL;
7992 if (is_declaration_die (c))
7994 if (is_template_instantiation (c))
7996 /* Instantiated templates do not need to be cloned into the
7997 type unit. Just move the DIE and its children back to
7998 the skeleton tree (in the main CU). */
7999 remove_child_with_prev (c, prev);
8000 add_child_die (parent->new_die, c);
8001 c = prev;
8003 else if (c->comdat_type_p)
8005 /* This is the skeleton of earlier break_out_comdat_types
8006 type. Clone the existing DIE, but keep the children
8007 under the original (which is in the main CU). */
8008 dw_die_ref clone = clone_die (c);
8010 replace_child (c, clone, prev);
8011 generate_skeleton_ancestor_tree (parent);
8012 add_child_die (parent->new_die, c);
8013 c = clone;
8014 continue;
8016 else
8018 /* Clone the existing DIE, move the original to the skeleton
8019 tree (which is in the main CU), and put the clone, with
8020 all the original's children, where the original came from
8021 (which is about to be moved to the type unit). */
8022 dw_die_ref clone = clone_die (c);
8023 move_all_children (c, clone);
8025 /* If the original has a DW_AT_object_pointer attribute,
8026 it would now point to a child DIE just moved to the
8027 cloned tree, so we need to remove that attribute from
8028 the original. */
8029 remove_AT (c, DW_AT_object_pointer);
8031 replace_child (c, clone, prev);
8032 generate_skeleton_ancestor_tree (parent);
8033 add_child_die (parent->new_die, c);
8034 node.old_die = clone;
8035 node.new_die = c;
8036 c = clone;
8039 generate_skeleton_bottom_up (&node);
8040 } while (next != NULL);
8043 /* Wrapper function for generate_skeleton_bottom_up. */
8045 static dw_die_ref
8046 generate_skeleton (dw_die_ref die)
8048 skeleton_chain_node node;
8050 node.old_die = die;
8051 node.new_die = NULL;
8052 node.parent = NULL;
8054 /* If this type definition is nested inside another type,
8055 and is not an instantiation of a template, always leave
8056 at least a declaration in its place. */
8057 if (die->die_parent != NULL
8058 && is_type_die (die->die_parent)
8059 && !is_template_instantiation (die))
8060 node.new_die = clone_as_declaration (die);
8062 generate_skeleton_bottom_up (&node);
8063 return node.new_die;
8066 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8067 declaration. The original DIE is moved to a new compile unit so that
8068 existing references to it follow it to the new location. If any of the
8069 original DIE's descendants is a declaration, we need to replace the
8070 original DIE with a skeleton tree and move the declarations back into the
8071 skeleton tree. */
8073 static dw_die_ref
8074 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8075 dw_die_ref prev)
8077 dw_die_ref skeleton, orig_parent;
8079 /* Copy the declaration context to the type unit DIE. If the returned
8080 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8081 that DIE. */
8082 orig_parent = copy_declaration_context (unit, child);
8084 skeleton = generate_skeleton (child);
8085 if (skeleton == NULL)
8086 remove_child_with_prev (child, prev);
8087 else
8089 skeleton->comdat_type_p = true;
8090 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8092 /* If the original DIE was a specification, we need to put
8093 the skeleton under the parent DIE of the declaration.
8094 This leaves the original declaration in the tree, but
8095 it will be pruned later since there are no longer any
8096 references to it. */
8097 if (orig_parent != NULL)
8099 remove_child_with_prev (child, prev);
8100 add_child_die (orig_parent, skeleton);
8102 else
8103 replace_child (child, skeleton, prev);
8106 return skeleton;
8109 static void
8110 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8111 comdat_type_node *type_node,
8112 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8114 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8115 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8116 DWARF procedure references in the DW_AT_location attribute. */
8118 static dw_die_ref
8119 copy_dwarf_procedure (dw_die_ref die,
8120 comdat_type_node *type_node,
8121 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8123 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8125 /* DWARF procedures are not supposed to have children... */
8126 gcc_assert (die->die_child == NULL);
8128 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8129 gcc_assert (vec_safe_length (die->die_attr) == 1
8130 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8132 /* Do not copy more than once DWARF procedures. */
8133 bool existed;
8134 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8135 if (existed)
8136 return die_copy;
8138 die_copy = clone_die (die);
8139 add_child_die (type_node->root_die, die_copy);
8140 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8141 return die_copy;
8144 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8145 procedures in DIE's attributes. */
8147 static void
8148 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8149 comdat_type_node *type_node,
8150 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8152 dw_attr_node *a;
8153 unsigned i;
8155 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8157 dw_loc_descr_ref loc;
8159 if (a->dw_attr_val.val_class != dw_val_class_loc)
8160 continue;
8162 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8164 switch (loc->dw_loc_opc)
8166 case DW_OP_call2:
8167 case DW_OP_call4:
8168 case DW_OP_call_ref:
8169 gcc_assert (loc->dw_loc_oprnd1.val_class
8170 == dw_val_class_die_ref);
8171 loc->dw_loc_oprnd1.v.val_die_ref.die
8172 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8173 type_node,
8174 copied_dwarf_procs);
8176 default:
8177 break;
8183 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8184 rewrite references to point to the copies.
8186 References are looked for in DIE's attributes and recursively in all its
8187 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8188 mapping from old DWARF procedures to their copy. It is used not to copy
8189 twice the same DWARF procedure under TYPE_NODE. */
8191 static void
8192 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8193 comdat_type_node *type_node,
8194 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8196 dw_die_ref c;
8198 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8199 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8200 type_node,
8201 copied_dwarf_procs));
8204 /* Traverse the DIE and set up additional .debug_types or .debug_info
8205 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8206 section. */
8208 static void
8209 break_out_comdat_types (dw_die_ref die)
8211 dw_die_ref c;
8212 dw_die_ref first;
8213 dw_die_ref prev = NULL;
8214 dw_die_ref next = NULL;
8215 dw_die_ref unit = NULL;
8217 first = c = die->die_child;
8218 if (c)
8219 next = c->die_sib;
8220 if (c) do {
8221 if (prev == NULL || prev->die_sib == c)
8222 prev = c;
8223 c = next;
8224 next = (c == first ? NULL : c->die_sib);
8225 if (should_move_die_to_comdat (c))
8227 dw_die_ref replacement;
8228 comdat_type_node *type_node;
8230 /* Break out nested types into their own type units. */
8231 break_out_comdat_types (c);
8233 /* Create a new type unit DIE as the root for the new tree, and
8234 add it to the list of comdat types. */
8235 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8236 add_AT_unsigned (unit, DW_AT_language,
8237 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8238 type_node = ggc_cleared_alloc<comdat_type_node> ();
8239 type_node->root_die = unit;
8240 type_node->next = comdat_type_list;
8241 comdat_type_list = type_node;
8243 /* Generate the type signature. */
8244 generate_type_signature (c, type_node);
8246 /* Copy the declaration context, attributes, and children of the
8247 declaration into the new type unit DIE, then remove this DIE
8248 from the main CU (or replace it with a skeleton if necessary). */
8249 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8250 type_node->skeleton_die = replacement;
8252 /* Add the DIE to the new compunit. */
8253 add_child_die (unit, c);
8255 /* Types can reference DWARF procedures for type size or data location
8256 expressions. Calls in DWARF expressions cannot target procedures
8257 that are not in the same section. So we must copy DWARF procedures
8258 along with this type and then rewrite references to them. */
8259 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8260 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8262 if (replacement != NULL)
8263 c = replacement;
8265 else if (c->die_tag == DW_TAG_namespace
8266 || c->die_tag == DW_TAG_class_type
8267 || c->die_tag == DW_TAG_structure_type
8268 || c->die_tag == DW_TAG_union_type)
8270 /* Look for nested types that can be broken out. */
8271 break_out_comdat_types (c);
8273 } while (next != NULL);
8276 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8277 Enter all the cloned children into the hash table decl_table. */
8279 static dw_die_ref
8280 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8282 dw_die_ref c;
8283 dw_die_ref clone;
8284 struct decl_table_entry *entry;
8285 decl_table_entry **slot;
8287 if (die->die_tag == DW_TAG_subprogram)
8288 clone = clone_as_declaration (die);
8289 else
8290 clone = clone_die (die);
8292 slot = decl_table->find_slot_with_hash (die,
8293 htab_hash_pointer (die), INSERT);
8295 /* Assert that DIE isn't in the hash table yet. If it would be there
8296 before, the ancestors would be necessarily there as well, therefore
8297 clone_tree_partial wouldn't be called. */
8298 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8300 entry = XCNEW (struct decl_table_entry);
8301 entry->orig = die;
8302 entry->copy = clone;
8303 *slot = entry;
8305 if (die->die_tag != DW_TAG_subprogram)
8306 FOR_EACH_CHILD (die, c,
8307 add_child_die (clone, clone_tree_partial (c, decl_table)));
8309 return clone;
8312 /* Walk the DIE and its children, looking for references to incomplete
8313 or trivial types that are unmarked (i.e., that are not in the current
8314 type_unit). */
8316 static void
8317 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8319 dw_die_ref c;
8320 dw_attr_node *a;
8321 unsigned ix;
8323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8325 if (AT_class (a) == dw_val_class_die_ref)
8327 dw_die_ref targ = AT_ref (a);
8328 decl_table_entry **slot;
8329 struct decl_table_entry *entry;
8331 if (targ->die_mark != 0 || targ->comdat_type_p)
8332 continue;
8334 slot = decl_table->find_slot_with_hash (targ,
8335 htab_hash_pointer (targ),
8336 INSERT);
8338 if (*slot != HTAB_EMPTY_ENTRY)
8340 /* TARG has already been copied, so we just need to
8341 modify the reference to point to the copy. */
8342 entry = *slot;
8343 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8345 else
8347 dw_die_ref parent = unit;
8348 dw_die_ref copy = clone_die (targ);
8350 /* Record in DECL_TABLE that TARG has been copied.
8351 Need to do this now, before the recursive call,
8352 because DECL_TABLE may be expanded and SLOT
8353 would no longer be a valid pointer. */
8354 entry = XCNEW (struct decl_table_entry);
8355 entry->orig = targ;
8356 entry->copy = copy;
8357 *slot = entry;
8359 /* If TARG is not a declaration DIE, we need to copy its
8360 children. */
8361 if (!is_declaration_die (targ))
8363 FOR_EACH_CHILD (
8364 targ, c,
8365 add_child_die (copy,
8366 clone_tree_partial (c, decl_table)));
8369 /* Make sure the cloned tree is marked as part of the
8370 type unit. */
8371 mark_dies (copy);
8373 /* If TARG has surrounding context, copy its ancestor tree
8374 into the new type unit. */
8375 if (targ->die_parent != NULL
8376 && !is_unit_die (targ->die_parent))
8377 parent = copy_ancestor_tree (unit, targ->die_parent,
8378 decl_table);
8380 add_child_die (parent, copy);
8381 a->dw_attr_val.v.val_die_ref.die = copy;
8383 /* Make sure the newly-copied DIE is walked. If it was
8384 installed in a previously-added context, it won't
8385 get visited otherwise. */
8386 if (parent != unit)
8388 /* Find the highest point of the newly-added tree,
8389 mark each node along the way, and walk from there. */
8390 parent->die_mark = 1;
8391 while (parent->die_parent
8392 && parent->die_parent->die_mark == 0)
8394 parent = parent->die_parent;
8395 parent->die_mark = 1;
8397 copy_decls_walk (unit, parent, decl_table);
8403 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8406 /* Copy declarations for "unworthy" types into the new comdat section.
8407 Incomplete types, modified types, and certain other types aren't broken
8408 out into comdat sections of their own, so they don't have a signature,
8409 and we need to copy the declaration into the same section so that we
8410 don't have an external reference. */
8412 static void
8413 copy_decls_for_unworthy_types (dw_die_ref unit)
8415 mark_dies (unit);
8416 decl_hash_type decl_table (10);
8417 copy_decls_walk (unit, unit, &decl_table);
8418 unmark_dies (unit);
8421 /* Traverse the DIE and add a sibling attribute if it may have the
8422 effect of speeding up access to siblings. To save some space,
8423 avoid generating sibling attributes for DIE's without children. */
8425 static void
8426 add_sibling_attributes (dw_die_ref die)
8428 dw_die_ref c;
8430 if (! die->die_child)
8431 return;
8433 if (die->die_parent && die != die->die_parent->die_child)
8434 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8436 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8439 /* Output all location lists for the DIE and its children. */
8441 static void
8442 output_location_lists (dw_die_ref die)
8444 dw_die_ref c;
8445 dw_attr_node *a;
8446 unsigned ix;
8448 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8449 if (AT_class (a) == dw_val_class_loc_list)
8450 output_loc_list (AT_loc_list (a));
8452 FOR_EACH_CHILD (die, c, output_location_lists (c));
8455 /* During assign_location_list_indexes and output_loclists_offset the
8456 current index, after it the number of assigned indexes (i.e. how
8457 large the .debug_loclists* offset table should be). */
8458 static unsigned int loc_list_idx;
8460 /* Output all location list offsets for the DIE and its children. */
8462 static void
8463 output_loclists_offsets (dw_die_ref die)
8465 dw_die_ref c;
8466 dw_attr_node *a;
8467 unsigned ix;
8469 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8470 if (AT_class (a) == dw_val_class_loc_list)
8472 dw_loc_list_ref l = AT_loc_list (a);
8473 if (l->offset_emitted)
8474 continue;
8475 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8476 loc_section_label, NULL);
8477 gcc_assert (l->hash == loc_list_idx);
8478 loc_list_idx++;
8479 l->offset_emitted = true;
8482 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8485 /* Recursively set indexes of location lists. */
8487 static void
8488 assign_location_list_indexes (dw_die_ref die)
8490 dw_die_ref c;
8491 dw_attr_node *a;
8492 unsigned ix;
8494 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8495 if (AT_class (a) == dw_val_class_loc_list)
8497 dw_loc_list_ref list = AT_loc_list (a);
8498 if (!list->num_assigned)
8500 list->num_assigned = true;
8501 list->hash = loc_list_idx++;
8505 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8508 /* We want to limit the number of external references, because they are
8509 larger than local references: a relocation takes multiple words, and
8510 even a sig8 reference is always eight bytes, whereas a local reference
8511 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8512 So if we encounter multiple external references to the same type DIE, we
8513 make a local typedef stub for it and redirect all references there.
8515 This is the element of the hash table for keeping track of these
8516 references. */
8518 struct external_ref
8520 dw_die_ref type;
8521 dw_die_ref stub;
8522 unsigned n_refs;
8525 /* Hashtable helpers. */
8527 struct external_ref_hasher : free_ptr_hash <external_ref>
8529 static inline hashval_t hash (const external_ref *);
8530 static inline bool equal (const external_ref *, const external_ref *);
8533 inline hashval_t
8534 external_ref_hasher::hash (const external_ref *r)
8536 dw_die_ref die = r->type;
8537 hashval_t h = 0;
8539 /* We can't use the address of the DIE for hashing, because
8540 that will make the order of the stub DIEs non-deterministic. */
8541 if (! die->comdat_type_p)
8542 /* We have a symbol; use it to compute a hash. */
8543 h = htab_hash_string (die->die_id.die_symbol);
8544 else
8546 /* We have a type signature; use a subset of the bits as the hash.
8547 The 8-byte signature is at least as large as hashval_t. */
8548 comdat_type_node *type_node = die->die_id.die_type_node;
8549 memcpy (&h, type_node->signature, sizeof (h));
8551 return h;
8554 inline bool
8555 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8557 return r1->type == r2->type;
8560 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8562 /* Return a pointer to the external_ref for references to DIE. */
8564 static struct external_ref *
8565 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8567 struct external_ref ref, *ref_p;
8568 external_ref **slot;
8570 ref.type = die;
8571 slot = map->find_slot (&ref, INSERT);
8572 if (*slot != HTAB_EMPTY_ENTRY)
8573 return *slot;
8575 ref_p = XCNEW (struct external_ref);
8576 ref_p->type = die;
8577 *slot = ref_p;
8578 return ref_p;
8581 /* Subroutine of optimize_external_refs, below.
8583 If we see a type skeleton, record it as our stub. If we see external
8584 references, remember how many we've seen. */
8586 static void
8587 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8589 dw_die_ref c;
8590 dw_attr_node *a;
8591 unsigned ix;
8592 struct external_ref *ref_p;
8594 if (is_type_die (die)
8595 && (c = get_AT_ref (die, DW_AT_signature)))
8597 /* This is a local skeleton; use it for local references. */
8598 ref_p = lookup_external_ref (map, c);
8599 ref_p->stub = die;
8602 /* Scan the DIE references, and remember any that refer to DIEs from
8603 other CUs (i.e. those which are not marked). */
8604 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8605 if (AT_class (a) == dw_val_class_die_ref
8606 && (c = AT_ref (a))->die_mark == 0
8607 && is_type_die (c))
8609 ref_p = lookup_external_ref (map, c);
8610 ref_p->n_refs++;
8613 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8616 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8617 points to an external_ref, DATA is the CU we're processing. If we don't
8618 already have a local stub, and we have multiple refs, build a stub. */
8621 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8623 struct external_ref *ref_p = *slot;
8625 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8627 /* We have multiple references to this type, so build a small stub.
8628 Both of these forms are a bit dodgy from the perspective of the
8629 DWARF standard, since technically they should have names. */
8630 dw_die_ref cu = data;
8631 dw_die_ref type = ref_p->type;
8632 dw_die_ref stub = NULL;
8634 if (type->comdat_type_p)
8636 /* If we refer to this type via sig8, use AT_signature. */
8637 stub = new_die (type->die_tag, cu, NULL_TREE);
8638 add_AT_die_ref (stub, DW_AT_signature, type);
8640 else
8642 /* Otherwise, use a typedef with no name. */
8643 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8644 add_AT_die_ref (stub, DW_AT_type, type);
8647 stub->die_mark++;
8648 ref_p->stub = stub;
8650 return 1;
8653 /* DIE is a unit; look through all the DIE references to see if there are
8654 any external references to types, and if so, create local stubs for
8655 them which will be applied in build_abbrev_table. This is useful because
8656 references to local DIEs are smaller. */
8658 static external_ref_hash_type *
8659 optimize_external_refs (dw_die_ref die)
8661 external_ref_hash_type *map = new external_ref_hash_type (10);
8662 optimize_external_refs_1 (die, map);
8663 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8664 return map;
8667 /* The following 3 variables are temporaries that are computed only during the
8668 build_abbrev_table call and used and released during the following
8669 optimize_abbrev_table call. */
8671 /* First abbrev_id that can be optimized based on usage. */
8672 static unsigned int abbrev_opt_start;
8674 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8675 abbrev_id smaller than this, because they must be already sized
8676 during build_abbrev_table). */
8677 static unsigned int abbrev_opt_base_type_end;
8679 /* Vector of usage counts during build_abbrev_table. Indexed by
8680 abbrev_id - abbrev_opt_start. */
8681 static vec<unsigned int> abbrev_usage_count;
8683 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8684 static vec<dw_die_ref> sorted_abbrev_dies;
8686 /* The format of each DIE (and its attribute value pairs) is encoded in an
8687 abbreviation table. This routine builds the abbreviation table and assigns
8688 a unique abbreviation id for each abbreviation entry. The children of each
8689 die are visited recursively. */
8691 static void
8692 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8694 unsigned int abbrev_id = 0;
8695 dw_die_ref c;
8696 dw_attr_node *a;
8697 unsigned ix;
8698 dw_die_ref abbrev;
8700 /* Scan the DIE references, and replace any that refer to
8701 DIEs from other CUs (i.e. those which are not marked) with
8702 the local stubs we built in optimize_external_refs. */
8703 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8704 if (AT_class (a) == dw_val_class_die_ref
8705 && (c = AT_ref (a))->die_mark == 0)
8707 struct external_ref *ref_p;
8708 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8710 ref_p = lookup_external_ref (extern_map, c);
8711 if (ref_p->stub && ref_p->stub != die)
8712 change_AT_die_ref (a, ref_p->stub);
8713 else
8714 /* We aren't changing this reference, so mark it external. */
8715 set_AT_ref_external (a, 1);
8718 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8720 dw_attr_node *die_a, *abbrev_a;
8721 unsigned ix;
8722 bool ok = true;
8724 if (abbrev_id == 0)
8725 continue;
8726 if (abbrev->die_tag != die->die_tag)
8727 continue;
8728 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8729 continue;
8731 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8732 continue;
8734 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8736 abbrev_a = &(*abbrev->die_attr)[ix];
8737 if ((abbrev_a->dw_attr != die_a->dw_attr)
8738 || (value_format (abbrev_a) != value_format (die_a)))
8740 ok = false;
8741 break;
8744 if (ok)
8745 break;
8748 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8750 vec_safe_push (abbrev_die_table, die);
8751 if (abbrev_opt_start)
8752 abbrev_usage_count.safe_push (0);
8754 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8756 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8757 sorted_abbrev_dies.safe_push (die);
8760 die->die_abbrev = abbrev_id;
8761 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8764 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8765 by die_abbrev's usage count, from the most commonly used
8766 abbreviation to the least. */
8768 static int
8769 die_abbrev_cmp (const void *p1, const void *p2)
8771 dw_die_ref die1 = *(const dw_die_ref *) p1;
8772 dw_die_ref die2 = *(const dw_die_ref *) p2;
8774 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8775 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8777 if (die1->die_abbrev >= abbrev_opt_base_type_end
8778 && die2->die_abbrev >= abbrev_opt_base_type_end)
8780 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8781 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8782 return -1;
8783 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8784 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8785 return 1;
8788 /* Stabilize the sort. */
8789 if (die1->die_abbrev < die2->die_abbrev)
8790 return -1;
8791 if (die1->die_abbrev > die2->die_abbrev)
8792 return 1;
8794 return 0;
8797 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8798 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8799 into dw_val_class_const_implicit or
8800 dw_val_class_unsigned_const_implicit. */
8802 static void
8803 optimize_implicit_const (unsigned int first_id, unsigned int end,
8804 vec<bool> &implicit_consts)
8806 /* It never makes sense if there is just one DIE using the abbreviation. */
8807 if (end < first_id + 2)
8808 return;
8810 dw_attr_node *a;
8811 unsigned ix, i;
8812 dw_die_ref die = sorted_abbrev_dies[first_id];
8813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8814 if (implicit_consts[ix])
8816 enum dw_val_class new_class = dw_val_class_none;
8817 switch (AT_class (a))
8819 case dw_val_class_unsigned_const:
8820 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8821 continue;
8823 /* The .debug_abbrev section will grow by
8824 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8825 in all the DIEs using that abbreviation. */
8826 if (constant_size (AT_unsigned (a)) * (end - first_id)
8827 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8828 continue;
8830 new_class = dw_val_class_unsigned_const_implicit;
8831 break;
8833 case dw_val_class_const:
8834 new_class = dw_val_class_const_implicit;
8835 break;
8837 case dw_val_class_file:
8838 new_class = dw_val_class_file_implicit;
8839 break;
8841 default:
8842 continue;
8844 for (i = first_id; i < end; i++)
8845 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8846 = new_class;
8850 /* Attempt to optimize abbreviation table from abbrev_opt_start
8851 abbreviation above. */
8853 static void
8854 optimize_abbrev_table (void)
8856 if (abbrev_opt_start
8857 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8858 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8860 auto_vec<bool, 32> implicit_consts;
8861 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8863 unsigned int abbrev_id = abbrev_opt_start - 1;
8864 unsigned int first_id = ~0U;
8865 unsigned int last_abbrev_id = 0;
8866 unsigned int i;
8867 dw_die_ref die;
8868 if (abbrev_opt_base_type_end > abbrev_opt_start)
8869 abbrev_id = abbrev_opt_base_type_end - 1;
8870 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8871 most commonly used abbreviations come first. */
8872 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8874 dw_attr_node *a;
8875 unsigned ix;
8877 /* If calc_base_type_die_sizes has been called, the CU and
8878 base types after it can't be optimized, because we've already
8879 calculated their DIE offsets. We've sorted them first. */
8880 if (die->die_abbrev < abbrev_opt_base_type_end)
8881 continue;
8882 if (die->die_abbrev != last_abbrev_id)
8884 last_abbrev_id = die->die_abbrev;
8885 if (dwarf_version >= 5 && first_id != ~0U)
8886 optimize_implicit_const (first_id, i, implicit_consts);
8887 abbrev_id++;
8888 (*abbrev_die_table)[abbrev_id] = die;
8889 if (dwarf_version >= 5)
8891 first_id = i;
8892 implicit_consts.truncate (0);
8894 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8895 switch (AT_class (a))
8897 case dw_val_class_const:
8898 case dw_val_class_unsigned_const:
8899 case dw_val_class_file:
8900 implicit_consts.safe_push (true);
8901 break;
8902 default:
8903 implicit_consts.safe_push (false);
8904 break;
8908 else if (dwarf_version >= 5)
8910 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8911 if (!implicit_consts[ix])
8912 continue;
8913 else
8915 dw_attr_node *other_a
8916 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8917 if (!dw_val_equal_p (&a->dw_attr_val,
8918 &other_a->dw_attr_val))
8919 implicit_consts[ix] = false;
8922 die->die_abbrev = abbrev_id;
8924 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8925 if (dwarf_version >= 5 && first_id != ~0U)
8926 optimize_implicit_const (first_id, i, implicit_consts);
8929 abbrev_opt_start = 0;
8930 abbrev_opt_base_type_end = 0;
8931 abbrev_usage_count.release ();
8932 sorted_abbrev_dies.release ();
8935 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8937 static int
8938 constant_size (unsigned HOST_WIDE_INT value)
8940 int log;
8942 if (value == 0)
8943 log = 0;
8944 else
8945 log = floor_log2 (value);
8947 log = log / 8;
8948 log = 1 << (floor_log2 (log) + 1);
8950 return log;
8953 /* Return the size of a DIE as it is represented in the
8954 .debug_info section. */
8956 static unsigned long
8957 size_of_die (dw_die_ref die)
8959 unsigned long size = 0;
8960 dw_attr_node *a;
8961 unsigned ix;
8962 enum dwarf_form form;
8964 size += size_of_uleb128 (die->die_abbrev);
8965 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8967 switch (AT_class (a))
8969 case dw_val_class_addr:
8970 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8972 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8973 size += size_of_uleb128 (AT_index (a));
8975 else
8976 size += DWARF2_ADDR_SIZE;
8977 break;
8978 case dw_val_class_offset:
8979 size += DWARF_OFFSET_SIZE;
8980 break;
8981 case dw_val_class_loc:
8983 unsigned long lsize = size_of_locs (AT_loc (a));
8985 /* Block length. */
8986 if (dwarf_version >= 4)
8987 size += size_of_uleb128 (lsize);
8988 else
8989 size += constant_size (lsize);
8990 size += lsize;
8992 break;
8993 case dw_val_class_loc_list:
8994 if (dwarf_split_debug_info && dwarf_version >= 5)
8996 gcc_assert (AT_loc_list (a)->num_assigned);
8997 size += size_of_uleb128 (AT_loc_list (a)->hash);
8999 else
9000 size += DWARF_OFFSET_SIZE;
9001 break;
9002 case dw_val_class_range_list:
9003 if (value_format (a) == DW_FORM_rnglistx)
9005 gcc_assert (rnglist_idx);
9006 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9007 size += size_of_uleb128 (r->idx);
9009 else
9010 size += DWARF_OFFSET_SIZE;
9011 break;
9012 case dw_val_class_const:
9013 size += size_of_sleb128 (AT_int (a));
9014 break;
9015 case dw_val_class_unsigned_const:
9017 int csize = constant_size (AT_unsigned (a));
9018 if (dwarf_version == 3
9019 && a->dw_attr == DW_AT_data_member_location
9020 && csize >= 4)
9021 size += size_of_uleb128 (AT_unsigned (a));
9022 else
9023 size += csize;
9025 break;
9026 case dw_val_class_const_implicit:
9027 case dw_val_class_unsigned_const_implicit:
9028 case dw_val_class_file_implicit:
9029 /* These occupy no size in the DIE, just an extra sleb128 in
9030 .debug_abbrev. */
9031 break;
9032 case dw_val_class_const_double:
9033 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9034 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9035 size++; /* block */
9036 break;
9037 case dw_val_class_wide_int:
9038 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9039 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9040 if (get_full_len (*a->dw_attr_val.v.val_wide)
9041 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9042 size++; /* block */
9043 break;
9044 case dw_val_class_vec:
9045 size += constant_size (a->dw_attr_val.v.val_vec.length
9046 * a->dw_attr_val.v.val_vec.elt_size)
9047 + a->dw_attr_val.v.val_vec.length
9048 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9049 break;
9050 case dw_val_class_flag:
9051 if (dwarf_version >= 4)
9052 /* Currently all add_AT_flag calls pass in 1 as last argument,
9053 so DW_FORM_flag_present can be used. If that ever changes,
9054 we'll need to use DW_FORM_flag and have some optimization
9055 in build_abbrev_table that will change those to
9056 DW_FORM_flag_present if it is set to 1 in all DIEs using
9057 the same abbrev entry. */
9058 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9059 else
9060 size += 1;
9061 break;
9062 case dw_val_class_die_ref:
9063 if (AT_ref_external (a))
9065 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9066 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9067 is sized by target address length, whereas in DWARF3
9068 it's always sized as an offset. */
9069 if (use_debug_types)
9070 size += DWARF_TYPE_SIGNATURE_SIZE;
9071 else if (dwarf_version == 2)
9072 size += DWARF2_ADDR_SIZE;
9073 else
9074 size += DWARF_OFFSET_SIZE;
9076 else
9077 size += DWARF_OFFSET_SIZE;
9078 break;
9079 case dw_val_class_fde_ref:
9080 size += DWARF_OFFSET_SIZE;
9081 break;
9082 case dw_val_class_lbl_id:
9083 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9085 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9086 size += size_of_uleb128 (AT_index (a));
9088 else
9089 size += DWARF2_ADDR_SIZE;
9090 break;
9091 case dw_val_class_lineptr:
9092 case dw_val_class_macptr:
9093 case dw_val_class_loclistsptr:
9094 size += DWARF_OFFSET_SIZE;
9095 break;
9096 case dw_val_class_str:
9097 form = AT_string_form (a);
9098 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9099 size += DWARF_OFFSET_SIZE;
9100 else if (form == DW_FORM_GNU_str_index)
9101 size += size_of_uleb128 (AT_index (a));
9102 else
9103 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9104 break;
9105 case dw_val_class_file:
9106 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9107 break;
9108 case dw_val_class_data8:
9109 size += 8;
9110 break;
9111 case dw_val_class_vms_delta:
9112 size += DWARF_OFFSET_SIZE;
9113 break;
9114 case dw_val_class_high_pc:
9115 size += DWARF2_ADDR_SIZE;
9116 break;
9117 case dw_val_class_discr_value:
9118 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9119 break;
9120 case dw_val_class_discr_list:
9122 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9124 /* This is a block, so we have the block length and then its
9125 data. */
9126 size += constant_size (block_size) + block_size;
9128 break;
9129 default:
9130 gcc_unreachable ();
9134 return size;
9137 /* Size the debugging information associated with a given DIE. Visits the
9138 DIE's children recursively. Updates the global variable next_die_offset, on
9139 each time through. Uses the current value of next_die_offset to update the
9140 die_offset field in each DIE. */
9142 static void
9143 calc_die_sizes (dw_die_ref die)
9145 dw_die_ref c;
9147 gcc_assert (die->die_offset == 0
9148 || (unsigned long int) die->die_offset == next_die_offset);
9149 die->die_offset = next_die_offset;
9150 next_die_offset += size_of_die (die);
9152 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9154 if (die->die_child != NULL)
9155 /* Count the null byte used to terminate sibling lists. */
9156 next_die_offset += 1;
9159 /* Size just the base type children at the start of the CU.
9160 This is needed because build_abbrev needs to size locs
9161 and sizing of type based stack ops needs to know die_offset
9162 values for the base types. */
9164 static void
9165 calc_base_type_die_sizes (void)
9167 unsigned long die_offset = (dwarf_split_debug_info
9168 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9169 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9170 unsigned int i;
9171 dw_die_ref base_type;
9172 #if ENABLE_ASSERT_CHECKING
9173 dw_die_ref prev = comp_unit_die ()->die_child;
9174 #endif
9176 die_offset += size_of_die (comp_unit_die ());
9177 for (i = 0; base_types.iterate (i, &base_type); i++)
9179 #if ENABLE_ASSERT_CHECKING
9180 gcc_assert (base_type->die_offset == 0
9181 && prev->die_sib == base_type
9182 && base_type->die_child == NULL
9183 && base_type->die_abbrev);
9184 prev = base_type;
9185 #endif
9186 if (abbrev_opt_start
9187 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9188 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9189 base_type->die_offset = die_offset;
9190 die_offset += size_of_die (base_type);
9194 /* Set the marks for a die and its children. We do this so
9195 that we know whether or not a reference needs to use FORM_ref_addr; only
9196 DIEs in the same CU will be marked. We used to clear out the offset
9197 and use that as the flag, but ran into ordering problems. */
9199 static void
9200 mark_dies (dw_die_ref die)
9202 dw_die_ref c;
9204 gcc_assert (!die->die_mark);
9206 die->die_mark = 1;
9207 FOR_EACH_CHILD (die, c, mark_dies (c));
9210 /* Clear the marks for a die and its children. */
9212 static void
9213 unmark_dies (dw_die_ref die)
9215 dw_die_ref c;
9217 if (! use_debug_types)
9218 gcc_assert (die->die_mark);
9220 die->die_mark = 0;
9221 FOR_EACH_CHILD (die, c, unmark_dies (c));
9224 /* Clear the marks for a die, its children and referred dies. */
9226 static void
9227 unmark_all_dies (dw_die_ref die)
9229 dw_die_ref c;
9230 dw_attr_node *a;
9231 unsigned ix;
9233 if (!die->die_mark)
9234 return;
9235 die->die_mark = 0;
9237 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9239 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9240 if (AT_class (a) == dw_val_class_die_ref)
9241 unmark_all_dies (AT_ref (a));
9244 /* Calculate if the entry should appear in the final output file. It may be
9245 from a pruned a type. */
9247 static bool
9248 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9250 /* By limiting gnu pubnames to definitions only, gold can generate a
9251 gdb index without entries for declarations, which don't include
9252 enough information to be useful. */
9253 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9254 return false;
9256 if (table == pubname_table)
9258 /* Enumerator names are part of the pubname table, but the
9259 parent DW_TAG_enumeration_type die may have been pruned.
9260 Don't output them if that is the case. */
9261 if (p->die->die_tag == DW_TAG_enumerator &&
9262 (p->die->die_parent == NULL
9263 || !p->die->die_parent->die_perennial_p))
9264 return false;
9266 /* Everything else in the pubname table is included. */
9267 return true;
9270 /* The pubtypes table shouldn't include types that have been
9271 pruned. */
9272 return (p->die->die_offset != 0
9273 || !flag_eliminate_unused_debug_types);
9276 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9277 generated for the compilation unit. */
9279 static unsigned long
9280 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9282 unsigned long size;
9283 unsigned i;
9284 pubname_entry *p;
9285 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9287 size = DWARF_PUBNAMES_HEADER_SIZE;
9288 FOR_EACH_VEC_ELT (*names, i, p)
9289 if (include_pubname_in_output (names, p))
9290 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9292 size += DWARF_OFFSET_SIZE;
9293 return size;
9296 /* Return the size of the information in the .debug_aranges section. */
9298 static unsigned long
9299 size_of_aranges (void)
9301 unsigned long size;
9303 size = DWARF_ARANGES_HEADER_SIZE;
9305 /* Count the address/length pair for this compilation unit. */
9306 if (text_section_used)
9307 size += 2 * DWARF2_ADDR_SIZE;
9308 if (cold_text_section_used)
9309 size += 2 * DWARF2_ADDR_SIZE;
9310 if (have_multiple_function_sections)
9312 unsigned fde_idx;
9313 dw_fde_ref fde;
9315 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9317 if (DECL_IGNORED_P (fde->decl))
9318 continue;
9319 if (!fde->in_std_section)
9320 size += 2 * DWARF2_ADDR_SIZE;
9321 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9322 size += 2 * DWARF2_ADDR_SIZE;
9326 /* Count the two zero words used to terminated the address range table. */
9327 size += 2 * DWARF2_ADDR_SIZE;
9328 return size;
9331 /* Select the encoding of an attribute value. */
9333 static enum dwarf_form
9334 value_format (dw_attr_node *a)
9336 switch (AT_class (a))
9338 case dw_val_class_addr:
9339 /* Only very few attributes allow DW_FORM_addr. */
9340 switch (a->dw_attr)
9342 case DW_AT_low_pc:
9343 case DW_AT_high_pc:
9344 case DW_AT_entry_pc:
9345 case DW_AT_trampoline:
9346 return (AT_index (a) == NOT_INDEXED
9347 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9348 default:
9349 break;
9351 switch (DWARF2_ADDR_SIZE)
9353 case 1:
9354 return DW_FORM_data1;
9355 case 2:
9356 return DW_FORM_data2;
9357 case 4:
9358 return DW_FORM_data4;
9359 case 8:
9360 return DW_FORM_data8;
9361 default:
9362 gcc_unreachable ();
9364 case dw_val_class_loc_list:
9365 if (dwarf_split_debug_info
9366 && dwarf_version >= 5
9367 && AT_loc_list (a)->num_assigned)
9368 return DW_FORM_loclistx;
9369 /* FALLTHRU */
9370 case dw_val_class_range_list:
9371 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9372 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9373 care about sizes of .debug* sections in shared libraries and
9374 executables and don't take into account relocations that affect just
9375 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9376 table in the .debug_rnglists section. */
9377 if (dwarf_split_debug_info
9378 && dwarf_version >= 5
9379 && AT_class (a) == dw_val_class_range_list
9380 && rnglist_idx
9381 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9382 return DW_FORM_rnglistx;
9383 if (dwarf_version >= 4)
9384 return DW_FORM_sec_offset;
9385 /* FALLTHRU */
9386 case dw_val_class_vms_delta:
9387 case dw_val_class_offset:
9388 switch (DWARF_OFFSET_SIZE)
9390 case 4:
9391 return DW_FORM_data4;
9392 case 8:
9393 return DW_FORM_data8;
9394 default:
9395 gcc_unreachable ();
9397 case dw_val_class_loc:
9398 if (dwarf_version >= 4)
9399 return DW_FORM_exprloc;
9400 switch (constant_size (size_of_locs (AT_loc (a))))
9402 case 1:
9403 return DW_FORM_block1;
9404 case 2:
9405 return DW_FORM_block2;
9406 case 4:
9407 return DW_FORM_block4;
9408 default:
9409 gcc_unreachable ();
9411 case dw_val_class_const:
9412 return DW_FORM_sdata;
9413 case dw_val_class_unsigned_const:
9414 switch (constant_size (AT_unsigned (a)))
9416 case 1:
9417 return DW_FORM_data1;
9418 case 2:
9419 return DW_FORM_data2;
9420 case 4:
9421 /* In DWARF3 DW_AT_data_member_location with
9422 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9423 constant, so we need to use DW_FORM_udata if we need
9424 a large constant. */
9425 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9426 return DW_FORM_udata;
9427 return DW_FORM_data4;
9428 case 8:
9429 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9430 return DW_FORM_udata;
9431 return DW_FORM_data8;
9432 default:
9433 gcc_unreachable ();
9435 case dw_val_class_const_implicit:
9436 case dw_val_class_unsigned_const_implicit:
9437 case dw_val_class_file_implicit:
9438 return DW_FORM_implicit_const;
9439 case dw_val_class_const_double:
9440 switch (HOST_BITS_PER_WIDE_INT)
9442 case 8:
9443 return DW_FORM_data2;
9444 case 16:
9445 return DW_FORM_data4;
9446 case 32:
9447 return DW_FORM_data8;
9448 case 64:
9449 if (dwarf_version >= 5)
9450 return DW_FORM_data16;
9451 /* FALLTHRU */
9452 default:
9453 return DW_FORM_block1;
9455 case dw_val_class_wide_int:
9456 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9458 case 8:
9459 return DW_FORM_data1;
9460 case 16:
9461 return DW_FORM_data2;
9462 case 32:
9463 return DW_FORM_data4;
9464 case 64:
9465 return DW_FORM_data8;
9466 case 128:
9467 if (dwarf_version >= 5)
9468 return DW_FORM_data16;
9469 /* FALLTHRU */
9470 default:
9471 return DW_FORM_block1;
9473 case dw_val_class_vec:
9474 switch (constant_size (a->dw_attr_val.v.val_vec.length
9475 * a->dw_attr_val.v.val_vec.elt_size))
9477 case 1:
9478 return DW_FORM_block1;
9479 case 2:
9480 return DW_FORM_block2;
9481 case 4:
9482 return DW_FORM_block4;
9483 default:
9484 gcc_unreachable ();
9486 case dw_val_class_flag:
9487 if (dwarf_version >= 4)
9489 /* Currently all add_AT_flag calls pass in 1 as last argument,
9490 so DW_FORM_flag_present can be used. If that ever changes,
9491 we'll need to use DW_FORM_flag and have some optimization
9492 in build_abbrev_table that will change those to
9493 DW_FORM_flag_present if it is set to 1 in all DIEs using
9494 the same abbrev entry. */
9495 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9496 return DW_FORM_flag_present;
9498 return DW_FORM_flag;
9499 case dw_val_class_die_ref:
9500 if (AT_ref_external (a))
9501 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9502 else
9503 return DW_FORM_ref;
9504 case dw_val_class_fde_ref:
9505 return DW_FORM_data;
9506 case dw_val_class_lbl_id:
9507 return (AT_index (a) == NOT_INDEXED
9508 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9509 case dw_val_class_lineptr:
9510 case dw_val_class_macptr:
9511 case dw_val_class_loclistsptr:
9512 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9513 case dw_val_class_str:
9514 return AT_string_form (a);
9515 case dw_val_class_file:
9516 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9518 case 1:
9519 return DW_FORM_data1;
9520 case 2:
9521 return DW_FORM_data2;
9522 case 4:
9523 return DW_FORM_data4;
9524 default:
9525 gcc_unreachable ();
9528 case dw_val_class_data8:
9529 return DW_FORM_data8;
9531 case dw_val_class_high_pc:
9532 switch (DWARF2_ADDR_SIZE)
9534 case 1:
9535 return DW_FORM_data1;
9536 case 2:
9537 return DW_FORM_data2;
9538 case 4:
9539 return DW_FORM_data4;
9540 case 8:
9541 return DW_FORM_data8;
9542 default:
9543 gcc_unreachable ();
9546 case dw_val_class_discr_value:
9547 return (a->dw_attr_val.v.val_discr_value.pos
9548 ? DW_FORM_udata
9549 : DW_FORM_sdata);
9550 case dw_val_class_discr_list:
9551 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9553 case 1:
9554 return DW_FORM_block1;
9555 case 2:
9556 return DW_FORM_block2;
9557 case 4:
9558 return DW_FORM_block4;
9559 default:
9560 gcc_unreachable ();
9563 default:
9564 gcc_unreachable ();
9568 /* Output the encoding of an attribute value. */
9570 static void
9571 output_value_format (dw_attr_node *a)
9573 enum dwarf_form form = value_format (a);
9575 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9578 /* Given a die and id, produce the appropriate abbreviations. */
9580 static void
9581 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9583 unsigned ix;
9584 dw_attr_node *a_attr;
9586 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9587 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9588 dwarf_tag_name (abbrev->die_tag));
9590 if (abbrev->die_child != NULL)
9591 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9592 else
9593 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9595 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9597 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9598 dwarf_attr_name (a_attr->dw_attr));
9599 output_value_format (a_attr);
9600 if (value_format (a_attr) == DW_FORM_implicit_const)
9602 if (AT_class (a_attr) == dw_val_class_file_implicit)
9604 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9605 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9606 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9608 else
9609 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9613 dw2_asm_output_data (1, 0, NULL);
9614 dw2_asm_output_data (1, 0, NULL);
9618 /* Output the .debug_abbrev section which defines the DIE abbreviation
9619 table. */
9621 static void
9622 output_abbrev_section (void)
9624 unsigned int abbrev_id;
9625 dw_die_ref abbrev;
9627 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9628 if (abbrev_id != 0)
9629 output_die_abbrevs (abbrev_id, abbrev);
9631 /* Terminate the table. */
9632 dw2_asm_output_data (1, 0, NULL);
9635 /* Return a new location list, given the begin and end range, and the
9636 expression. */
9638 static inline dw_loc_list_ref
9639 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9640 const char *section)
9642 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9644 retlist->begin = begin;
9645 retlist->begin_entry = NULL;
9646 retlist->end = end;
9647 retlist->expr = expr;
9648 retlist->section = section;
9650 return retlist;
9653 /* Generate a new internal symbol for this location list node, if it
9654 hasn't got one yet. */
9656 static inline void
9657 gen_llsym (dw_loc_list_ref list)
9659 gcc_assert (!list->ll_symbol);
9660 list->ll_symbol = gen_internal_sym ("LLST");
9663 /* Output the location list given to us. */
9665 static void
9666 output_loc_list (dw_loc_list_ref list_head)
9668 if (list_head->emitted)
9669 return;
9670 list_head->emitted = true;
9672 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9674 dw_loc_list_ref curr = list_head;
9675 const char *last_section = NULL;
9676 const char *base_label = NULL;
9678 /* Walk the location list, and output each range + expression. */
9679 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9681 unsigned long size;
9682 /* Don't output an entry that starts and ends at the same address. */
9683 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9684 continue;
9685 size = size_of_locs (curr->expr);
9686 /* If the expression is too large, drop it on the floor. We could
9687 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9688 in the expression, but >= 64KB expressions for a single value
9689 in a single range are unlikely very useful. */
9690 if (dwarf_version < 5 && size > 0xffff)
9691 continue;
9692 if (dwarf_version >= 5)
9694 if (dwarf_split_debug_info)
9696 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9697 uleb128 index into .debug_addr and uleb128 length. */
9698 dw2_asm_output_data (1, DW_LLE_startx_length,
9699 "DW_LLE_startx_length (%s)",
9700 list_head->ll_symbol);
9701 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9702 "Location list range start index "
9703 "(%s)", curr->begin);
9704 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9705 For that case we probably need to emit DW_LLE_startx_endx,
9706 but we'd need 2 .debug_addr entries rather than just one. */
9707 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9708 "Location list length (%s)",
9709 list_head->ll_symbol);
9711 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9713 /* If all code is in .text section, the base address is
9714 already provided by the CU attributes. Use
9715 DW_LLE_offset_pair where both addresses are uleb128 encoded
9716 offsets against that base. */
9717 dw2_asm_output_data (1, DW_LLE_offset_pair,
9718 "DW_LLE_offset_pair (%s)",
9719 list_head->ll_symbol);
9720 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9721 "Location list begin address (%s)",
9722 list_head->ll_symbol);
9723 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9724 "Location list end address (%s)",
9725 list_head->ll_symbol);
9727 else if (HAVE_AS_LEB128)
9729 /* Otherwise, find out how many consecutive entries could share
9730 the same base entry. If just one, emit DW_LLE_start_length,
9731 otherwise emit DW_LLE_base_address for the base address
9732 followed by a series of DW_LLE_offset_pair. */
9733 if (last_section == NULL || curr->section != last_section)
9735 dw_loc_list_ref curr2;
9736 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9737 curr2 = curr2->dw_loc_next)
9739 if (strcmp (curr2->begin, curr2->end) == 0
9740 && !curr2->force)
9741 continue;
9742 break;
9744 if (curr2 == NULL || curr->section != curr2->section)
9745 last_section = NULL;
9746 else
9748 last_section = curr->section;
9749 base_label = curr->begin;
9750 dw2_asm_output_data (1, DW_LLE_base_address,
9751 "DW_LLE_base_address (%s)",
9752 list_head->ll_symbol);
9753 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9754 "Base address (%s)",
9755 list_head->ll_symbol);
9758 /* Only one entry with the same base address. Use
9759 DW_LLE_start_length with absolute address and uleb128
9760 length. */
9761 if (last_section == NULL)
9763 dw2_asm_output_data (1, DW_LLE_start_length,
9764 "DW_LLE_start_length (%s)",
9765 list_head->ll_symbol);
9766 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9767 "Location list begin address (%s)",
9768 list_head->ll_symbol);
9769 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9770 "Location list length "
9771 "(%s)", list_head->ll_symbol);
9773 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9774 DW_LLE_base_address. */
9775 else
9777 dw2_asm_output_data (1, DW_LLE_offset_pair,
9778 "DW_LLE_offset_pair (%s)",
9779 list_head->ll_symbol);
9780 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9781 "Location list begin address "
9782 "(%s)", list_head->ll_symbol);
9783 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9784 "Location list end address "
9785 "(%s)", list_head->ll_symbol);
9788 /* The assembler does not support .uleb128 directive. Emit
9789 DW_LLE_start_end with a pair of absolute addresses. */
9790 else
9792 dw2_asm_output_data (1, DW_LLE_start_end,
9793 "DW_LLE_start_end (%s)",
9794 list_head->ll_symbol);
9795 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9796 "Location list begin address (%s)",
9797 list_head->ll_symbol);
9798 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9799 "Location list end address (%s)",
9800 list_head->ll_symbol);
9803 else if (dwarf_split_debug_info)
9805 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9806 and 4 byte length. */
9807 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9808 "Location list start/length entry (%s)",
9809 list_head->ll_symbol);
9810 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9811 "Location list range start index (%s)",
9812 curr->begin);
9813 /* The length field is 4 bytes. If we ever need to support
9814 an 8-byte length, we can add a new DW_LLE code or fall back
9815 to DW_LLE_GNU_start_end_entry. */
9816 dw2_asm_output_delta (4, curr->end, curr->begin,
9817 "Location list range length (%s)",
9818 list_head->ll_symbol);
9820 else if (!have_multiple_function_sections)
9822 /* Pair of relative addresses against start of text section. */
9823 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9824 "Location list begin address (%s)",
9825 list_head->ll_symbol);
9826 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9827 "Location list end address (%s)",
9828 list_head->ll_symbol);
9830 else
9832 /* Pair of absolute addresses. */
9833 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9834 "Location list begin address (%s)",
9835 list_head->ll_symbol);
9836 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9837 "Location list end address (%s)",
9838 list_head->ll_symbol);
9841 /* Output the block length for this list of location operations. */
9842 if (dwarf_version >= 5)
9843 dw2_asm_output_data_uleb128 (size, "Location expression size");
9844 else
9846 gcc_assert (size <= 0xffff);
9847 dw2_asm_output_data (2, size, "Location expression size");
9850 output_loc_sequence (curr->expr, -1);
9853 /* And finally list termination. */
9854 if (dwarf_version >= 5)
9855 dw2_asm_output_data (1, DW_LLE_end_of_list,
9856 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9857 else if (dwarf_split_debug_info)
9858 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9859 "Location list terminator (%s)",
9860 list_head->ll_symbol);
9861 else
9863 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9864 "Location list terminator begin (%s)",
9865 list_head->ll_symbol);
9866 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9867 "Location list terminator end (%s)",
9868 list_head->ll_symbol);
9872 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9873 section. Emit a relocated reference if val_entry is NULL, otherwise,
9874 emit an indirect reference. */
9876 static void
9877 output_range_list_offset (dw_attr_node *a)
9879 const char *name = dwarf_attr_name (a->dw_attr);
9881 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9883 if (dwarf_version >= 5)
9885 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9886 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9887 debug_ranges_section, "%s", name);
9889 else
9891 char *p = strchr (ranges_section_label, '\0');
9892 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9893 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9894 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9895 debug_ranges_section, "%s", name);
9896 *p = '\0';
9899 else if (dwarf_version >= 5)
9901 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9902 gcc_assert (rnglist_idx);
9903 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9905 else
9906 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9907 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9908 "%s (offset from %s)", name, ranges_section_label);
9911 /* Output the offset into the debug_loc section. */
9913 static void
9914 output_loc_list_offset (dw_attr_node *a)
9916 char *sym = AT_loc_list (a)->ll_symbol;
9918 gcc_assert (sym);
9919 if (!dwarf_split_debug_info)
9920 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9921 "%s", dwarf_attr_name (a->dw_attr));
9922 else if (dwarf_version >= 5)
9924 gcc_assert (AT_loc_list (a)->num_assigned);
9925 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9926 dwarf_attr_name (a->dw_attr),
9927 sym);
9929 else
9930 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9931 "%s", dwarf_attr_name (a->dw_attr));
9934 /* Output an attribute's index or value appropriately. */
9936 static void
9937 output_attr_index_or_value (dw_attr_node *a)
9939 const char *name = dwarf_attr_name (a->dw_attr);
9941 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9943 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9944 return;
9946 switch (AT_class (a))
9948 case dw_val_class_addr:
9949 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9950 break;
9951 case dw_val_class_high_pc:
9952 case dw_val_class_lbl_id:
9953 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9954 break;
9955 default:
9956 gcc_unreachable ();
9960 /* Output a type signature. */
9962 static inline void
9963 output_signature (const char *sig, const char *name)
9965 int i;
9967 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9968 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9971 /* Output a discriminant value. */
9973 static inline void
9974 output_discr_value (dw_discr_value *discr_value, const char *name)
9976 if (discr_value->pos)
9977 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9978 else
9979 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9982 /* Output the DIE and its attributes. Called recursively to generate
9983 the definitions of each child DIE. */
9985 static void
9986 output_die (dw_die_ref die)
9988 dw_attr_node *a;
9989 dw_die_ref c;
9990 unsigned long size;
9991 unsigned ix;
9993 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9994 (unsigned long)die->die_offset,
9995 dwarf_tag_name (die->die_tag));
9997 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9999 const char *name = dwarf_attr_name (a->dw_attr);
10001 switch (AT_class (a))
10003 case dw_val_class_addr:
10004 output_attr_index_or_value (a);
10005 break;
10007 case dw_val_class_offset:
10008 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10009 "%s", name);
10010 break;
10012 case dw_val_class_range_list:
10013 output_range_list_offset (a);
10014 break;
10016 case dw_val_class_loc:
10017 size = size_of_locs (AT_loc (a));
10019 /* Output the block length for this list of location operations. */
10020 if (dwarf_version >= 4)
10021 dw2_asm_output_data_uleb128 (size, "%s", name);
10022 else
10023 dw2_asm_output_data (constant_size (size), size, "%s", name);
10025 output_loc_sequence (AT_loc (a), -1);
10026 break;
10028 case dw_val_class_const:
10029 /* ??? It would be slightly more efficient to use a scheme like is
10030 used for unsigned constants below, but gdb 4.x does not sign
10031 extend. Gdb 5.x does sign extend. */
10032 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10033 break;
10035 case dw_val_class_unsigned_const:
10037 int csize = constant_size (AT_unsigned (a));
10038 if (dwarf_version == 3
10039 && a->dw_attr == DW_AT_data_member_location
10040 && csize >= 4)
10041 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10042 else
10043 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10045 break;
10047 case dw_val_class_const_implicit:
10048 if (flag_debug_asm)
10049 fprintf (asm_out_file, "\t\t\t%s %s ("
10050 HOST_WIDE_INT_PRINT_DEC ")\n",
10051 ASM_COMMENT_START, name, AT_int (a));
10052 break;
10054 case dw_val_class_unsigned_const_implicit:
10055 if (flag_debug_asm)
10056 fprintf (asm_out_file, "\t\t\t%s %s ("
10057 HOST_WIDE_INT_PRINT_HEX ")\n",
10058 ASM_COMMENT_START, name, AT_unsigned (a));
10059 break;
10061 case dw_val_class_const_double:
10063 unsigned HOST_WIDE_INT first, second;
10065 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10066 dw2_asm_output_data (1,
10067 HOST_BITS_PER_DOUBLE_INT
10068 / HOST_BITS_PER_CHAR,
10069 NULL);
10071 if (WORDS_BIG_ENDIAN)
10073 first = a->dw_attr_val.v.val_double.high;
10074 second = a->dw_attr_val.v.val_double.low;
10076 else
10078 first = a->dw_attr_val.v.val_double.low;
10079 second = a->dw_attr_val.v.val_double.high;
10082 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10083 first, "%s", name);
10084 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10085 second, NULL);
10087 break;
10089 case dw_val_class_wide_int:
10091 int i;
10092 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10093 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10094 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10095 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10096 * l, NULL);
10098 if (WORDS_BIG_ENDIAN)
10099 for (i = len - 1; i >= 0; --i)
10101 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10102 "%s", name);
10103 name = "";
10105 else
10106 for (i = 0; i < len; ++i)
10108 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10109 "%s", name);
10110 name = "";
10113 break;
10115 case dw_val_class_vec:
10117 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10118 unsigned int len = a->dw_attr_val.v.val_vec.length;
10119 unsigned int i;
10120 unsigned char *p;
10122 dw2_asm_output_data (constant_size (len * elt_size),
10123 len * elt_size, "%s", name);
10124 if (elt_size > sizeof (HOST_WIDE_INT))
10126 elt_size /= 2;
10127 len *= 2;
10129 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10130 i < len;
10131 i++, p += elt_size)
10132 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10133 "fp or vector constant word %u", i);
10134 break;
10137 case dw_val_class_flag:
10138 if (dwarf_version >= 4)
10140 /* Currently all add_AT_flag calls pass in 1 as last argument,
10141 so DW_FORM_flag_present can be used. If that ever changes,
10142 we'll need to use DW_FORM_flag and have some optimization
10143 in build_abbrev_table that will change those to
10144 DW_FORM_flag_present if it is set to 1 in all DIEs using
10145 the same abbrev entry. */
10146 gcc_assert (AT_flag (a) == 1);
10147 if (flag_debug_asm)
10148 fprintf (asm_out_file, "\t\t\t%s %s\n",
10149 ASM_COMMENT_START, name);
10150 break;
10152 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10153 break;
10155 case dw_val_class_loc_list:
10156 output_loc_list_offset (a);
10157 break;
10159 case dw_val_class_die_ref:
10160 if (AT_ref_external (a))
10162 if (AT_ref (a)->comdat_type_p)
10164 comdat_type_node *type_node
10165 = AT_ref (a)->die_id.die_type_node;
10167 gcc_assert (type_node);
10168 output_signature (type_node->signature, name);
10170 else
10172 const char *sym = AT_ref (a)->die_id.die_symbol;
10173 int size;
10175 gcc_assert (sym);
10176 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10177 length, whereas in DWARF3 it's always sized as an
10178 offset. */
10179 if (dwarf_version == 2)
10180 size = DWARF2_ADDR_SIZE;
10181 else
10182 size = DWARF_OFFSET_SIZE;
10183 /* ??? We cannot unconditionally output die_offset if
10184 non-zero - others might create references to those
10185 DIEs via symbols.
10186 And we do not clear its DIE offset after outputting it
10187 (and the label refers to the actual DIEs, not the
10188 DWARF CU unit header which is when using label + offset
10189 would be the correct thing to do).
10190 ??? This is the reason for the with_offset flag. */
10191 if (AT_ref (a)->with_offset)
10192 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10193 debug_info_section, "%s", name);
10194 else
10195 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10196 name);
10199 else
10201 gcc_assert (AT_ref (a)->die_offset);
10202 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10203 "%s", name);
10205 break;
10207 case dw_val_class_fde_ref:
10209 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10211 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10212 a->dw_attr_val.v.val_fde_index * 2);
10213 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10214 "%s", name);
10216 break;
10218 case dw_val_class_vms_delta:
10219 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10220 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10221 AT_vms_delta2 (a), AT_vms_delta1 (a),
10222 "%s", name);
10223 #else
10224 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10225 AT_vms_delta2 (a), AT_vms_delta1 (a),
10226 "%s", name);
10227 #endif
10228 break;
10230 case dw_val_class_lbl_id:
10231 output_attr_index_or_value (a);
10232 break;
10234 case dw_val_class_lineptr:
10235 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10236 debug_line_section, "%s", name);
10237 break;
10239 case dw_val_class_macptr:
10240 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10241 debug_macinfo_section, "%s", name);
10242 break;
10244 case dw_val_class_loclistsptr:
10245 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10246 debug_loc_section, "%s", name);
10247 break;
10249 case dw_val_class_str:
10250 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10251 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10252 a->dw_attr_val.v.val_str->label,
10253 debug_str_section,
10254 "%s: \"%s\"", name, AT_string (a));
10255 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10256 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10257 a->dw_attr_val.v.val_str->label,
10258 debug_line_str_section,
10259 "%s: \"%s\"", name, AT_string (a));
10260 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10261 dw2_asm_output_data_uleb128 (AT_index (a),
10262 "%s: \"%s\"", name, AT_string (a));
10263 else
10264 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10265 break;
10267 case dw_val_class_file:
10269 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10271 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10272 a->dw_attr_val.v.val_file->filename);
10273 break;
10276 case dw_val_class_file_implicit:
10277 if (flag_debug_asm)
10278 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10279 ASM_COMMENT_START, name,
10280 maybe_emit_file (a->dw_attr_val.v.val_file),
10281 a->dw_attr_val.v.val_file->filename);
10282 break;
10284 case dw_val_class_data8:
10286 int i;
10288 for (i = 0; i < 8; i++)
10289 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10290 i == 0 ? "%s" : NULL, name);
10291 break;
10294 case dw_val_class_high_pc:
10295 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10296 get_AT_low_pc (die), "DW_AT_high_pc");
10297 break;
10299 case dw_val_class_discr_value:
10300 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10301 break;
10303 case dw_val_class_discr_list:
10305 dw_discr_list_ref list = AT_discr_list (a);
10306 const int size = size_of_discr_list (list);
10308 /* This is a block, so output its length first. */
10309 dw2_asm_output_data (constant_size (size), size,
10310 "%s: block size", name);
10312 for (; list != NULL; list = list->dw_discr_next)
10314 /* One byte for the discriminant value descriptor, and then as
10315 many LEB128 numbers as required. */
10316 if (list->dw_discr_range)
10317 dw2_asm_output_data (1, DW_DSC_range,
10318 "%s: DW_DSC_range", name);
10319 else
10320 dw2_asm_output_data (1, DW_DSC_label,
10321 "%s: DW_DSC_label", name);
10323 output_discr_value (&list->dw_discr_lower_bound, name);
10324 if (list->dw_discr_range)
10325 output_discr_value (&list->dw_discr_upper_bound, name);
10327 break;
10330 default:
10331 gcc_unreachable ();
10335 FOR_EACH_CHILD (die, c, output_die (c));
10337 /* Add null byte to terminate sibling list. */
10338 if (die->die_child != NULL)
10339 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10340 (unsigned long) die->die_offset);
10343 /* Output the compilation unit that appears at the beginning of the
10344 .debug_info section, and precedes the DIE descriptions. */
10346 static void
10347 output_compilation_unit_header (enum dwarf_unit_type ut)
10349 if (!XCOFF_DEBUGGING_INFO)
10351 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10352 dw2_asm_output_data (4, 0xffffffff,
10353 "Initial length escape value indicating 64-bit DWARF extension");
10354 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10355 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10356 "Length of Compilation Unit Info");
10359 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10360 if (dwarf_version >= 5)
10362 const char *name;
10363 switch (ut)
10365 case DW_UT_compile: name = "DW_UT_compile"; break;
10366 case DW_UT_type: name = "DW_UT_type"; break;
10367 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10368 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10369 default: gcc_unreachable ();
10371 dw2_asm_output_data (1, ut, "%s", name);
10372 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10374 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10375 debug_abbrev_section,
10376 "Offset Into Abbrev. Section");
10377 if (dwarf_version < 5)
10378 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10381 /* Output the compilation unit DIE and its children. */
10383 static void
10384 output_comp_unit (dw_die_ref die, int output_if_empty,
10385 const unsigned char *dwo_id)
10387 const char *secname, *oldsym;
10388 char *tmp;
10390 /* Unless we are outputting main CU, we may throw away empty ones. */
10391 if (!output_if_empty && die->die_child == NULL)
10392 return;
10394 /* Even if there are no children of this DIE, we must output the information
10395 about the compilation unit. Otherwise, on an empty translation unit, we
10396 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10397 will then complain when examining the file. First mark all the DIEs in
10398 this CU so we know which get local refs. */
10399 mark_dies (die);
10401 external_ref_hash_type *extern_map = optimize_external_refs (die);
10403 /* For now, optimize only the main CU, in order to optimize the rest
10404 we'd need to see all of them earlier. Leave the rest for post-linking
10405 tools like DWZ. */
10406 if (die == comp_unit_die ())
10407 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10409 build_abbrev_table (die, extern_map);
10411 optimize_abbrev_table ();
10413 delete extern_map;
10415 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10416 next_die_offset = (dwo_id
10417 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10418 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10419 calc_die_sizes (die);
10421 oldsym = die->die_id.die_symbol;
10422 if (oldsym && die->comdat_type_p)
10424 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10426 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10427 secname = tmp;
10428 die->die_id.die_symbol = NULL;
10429 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10431 else
10433 switch_to_section (debug_info_section);
10434 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10435 info_section_emitted = true;
10438 /* For LTO cross unit DIE refs we want a symbol on the start of the
10439 debuginfo section, not on the CU DIE. */
10440 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10442 /* ??? No way to get visibility assembled without a decl. */
10443 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10444 get_identifier (oldsym), char_type_node);
10445 TREE_PUBLIC (decl) = true;
10446 TREE_STATIC (decl) = true;
10447 DECL_ARTIFICIAL (decl) = true;
10448 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10449 DECL_VISIBILITY_SPECIFIED (decl) = true;
10450 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10451 #ifdef ASM_WEAKEN_LABEL
10452 /* We prefer a .weak because that handles duplicates from duplicate
10453 archive members in a graceful way. */
10454 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10455 #else
10456 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10457 #endif
10458 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10461 /* Output debugging information. */
10462 output_compilation_unit_header (dwo_id
10463 ? DW_UT_split_compile : DW_UT_compile);
10464 if (dwarf_version >= 5)
10466 if (dwo_id != NULL)
10467 for (int i = 0; i < 8; i++)
10468 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10470 output_die (die);
10472 /* Leave the marks on the main CU, so we can check them in
10473 output_pubnames. */
10474 if (oldsym)
10476 unmark_dies (die);
10477 die->die_id.die_symbol = oldsym;
10481 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10482 and .debug_pubtypes. This is configured per-target, but can be
10483 overridden by the -gpubnames or -gno-pubnames options. */
10485 static inline bool
10486 want_pubnames (void)
10488 if (debug_info_level <= DINFO_LEVEL_TERSE)
10489 return false;
10490 if (debug_generate_pub_sections != -1)
10491 return debug_generate_pub_sections;
10492 return targetm.want_debug_pub_sections;
10495 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10497 static void
10498 add_AT_pubnames (dw_die_ref die)
10500 if (want_pubnames ())
10501 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10504 /* Add a string attribute value to a skeleton DIE. */
10506 static inline void
10507 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10508 const char *str)
10510 dw_attr_node attr;
10511 struct indirect_string_node *node;
10513 if (! skeleton_debug_str_hash)
10514 skeleton_debug_str_hash
10515 = hash_table<indirect_string_hasher>::create_ggc (10);
10517 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10518 find_string_form (node);
10519 if (node->form == DW_FORM_GNU_str_index)
10520 node->form = DW_FORM_strp;
10522 attr.dw_attr = attr_kind;
10523 attr.dw_attr_val.val_class = dw_val_class_str;
10524 attr.dw_attr_val.val_entry = NULL;
10525 attr.dw_attr_val.v.val_str = node;
10526 add_dwarf_attr (die, &attr);
10529 /* Helper function to generate top-level dies for skeleton debug_info and
10530 debug_types. */
10532 static void
10533 add_top_level_skeleton_die_attrs (dw_die_ref die)
10535 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10536 const char *comp_dir = comp_dir_string ();
10538 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10539 if (comp_dir != NULL)
10540 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10541 add_AT_pubnames (die);
10542 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10545 /* Output skeleton debug sections that point to the dwo file. */
10547 static void
10548 output_skeleton_debug_sections (dw_die_ref comp_unit,
10549 const unsigned char *dwo_id)
10551 /* These attributes will be found in the full debug_info section. */
10552 remove_AT (comp_unit, DW_AT_producer);
10553 remove_AT (comp_unit, DW_AT_language);
10555 switch_to_section (debug_skeleton_info_section);
10556 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10558 /* Produce the skeleton compilation-unit header. This one differs enough from
10559 a normal CU header that it's better not to call output_compilation_unit
10560 header. */
10561 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10562 dw2_asm_output_data (4, 0xffffffff,
10563 "Initial length escape value indicating 64-bit "
10564 "DWARF extension");
10566 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10567 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10568 - DWARF_INITIAL_LENGTH_SIZE
10569 + size_of_die (comp_unit),
10570 "Length of Compilation Unit Info");
10571 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10572 if (dwarf_version >= 5)
10574 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10575 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10577 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10578 debug_skeleton_abbrev_section,
10579 "Offset Into Abbrev. Section");
10580 if (dwarf_version < 5)
10581 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10582 else
10583 for (int i = 0; i < 8; i++)
10584 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10586 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10587 output_die (comp_unit);
10589 /* Build the skeleton debug_abbrev section. */
10590 switch_to_section (debug_skeleton_abbrev_section);
10591 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10593 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10595 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10598 /* Output a comdat type unit DIE and its children. */
10600 static void
10601 output_comdat_type_unit (comdat_type_node *node)
10603 const char *secname;
10604 char *tmp;
10605 int i;
10606 #if defined (OBJECT_FORMAT_ELF)
10607 tree comdat_key;
10608 #endif
10610 /* First mark all the DIEs in this CU so we know which get local refs. */
10611 mark_dies (node->root_die);
10613 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10615 build_abbrev_table (node->root_die, extern_map);
10617 delete extern_map;
10618 extern_map = NULL;
10620 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10621 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10622 calc_die_sizes (node->root_die);
10624 #if defined (OBJECT_FORMAT_ELF)
10625 if (dwarf_version >= 5)
10627 if (!dwarf_split_debug_info)
10628 secname = ".debug_info";
10629 else
10630 secname = ".debug_info.dwo";
10632 else if (!dwarf_split_debug_info)
10633 secname = ".debug_types";
10634 else
10635 secname = ".debug_types.dwo";
10637 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10638 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10639 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10640 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10641 comdat_key = get_identifier (tmp);
10642 targetm.asm_out.named_section (secname,
10643 SECTION_DEBUG | SECTION_LINKONCE,
10644 comdat_key);
10645 #else
10646 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10647 sprintf (tmp, (dwarf_version >= 5
10648 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10649 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10650 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10651 secname = tmp;
10652 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10653 #endif
10655 /* Output debugging information. */
10656 output_compilation_unit_header (dwarf_split_debug_info
10657 ? DW_UT_split_type : DW_UT_type);
10658 output_signature (node->signature, "Type Signature");
10659 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10660 "Offset to Type DIE");
10661 output_die (node->root_die);
10663 unmark_dies (node->root_die);
10666 /* Return the DWARF2/3 pubname associated with a decl. */
10668 static const char *
10669 dwarf2_name (tree decl, int scope)
10671 if (DECL_NAMELESS (decl))
10672 return NULL;
10673 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10676 /* Add a new entry to .debug_pubnames if appropriate. */
10678 static void
10679 add_pubname_string (const char *str, dw_die_ref die)
10681 pubname_entry e;
10683 e.die = die;
10684 e.name = xstrdup (str);
10685 vec_safe_push (pubname_table, e);
10688 static void
10689 add_pubname (tree decl, dw_die_ref die)
10691 if (!want_pubnames ())
10692 return;
10694 /* Don't add items to the table when we expect that the consumer will have
10695 just read the enclosing die. For example, if the consumer is looking at a
10696 class_member, it will either be inside the class already, or will have just
10697 looked up the class to find the member. Either way, searching the class is
10698 faster than searching the index. */
10699 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10700 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10702 const char *name = dwarf2_name (decl, 1);
10704 if (name)
10705 add_pubname_string (name, die);
10709 /* Add an enumerator to the pubnames section. */
10711 static void
10712 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10714 pubname_entry e;
10716 gcc_assert (scope_name);
10717 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10718 e.die = die;
10719 vec_safe_push (pubname_table, e);
10722 /* Add a new entry to .debug_pubtypes if appropriate. */
10724 static void
10725 add_pubtype (tree decl, dw_die_ref die)
10727 pubname_entry e;
10729 if (!want_pubnames ())
10730 return;
10732 if ((TREE_PUBLIC (decl)
10733 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10734 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10736 tree scope = NULL;
10737 const char *scope_name = "";
10738 const char *sep = is_cxx () ? "::" : ".";
10739 const char *name;
10741 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10742 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10744 scope_name = lang_hooks.dwarf_name (scope, 1);
10745 if (scope_name != NULL && scope_name[0] != '\0')
10746 scope_name = concat (scope_name, sep, NULL);
10747 else
10748 scope_name = "";
10751 if (TYPE_P (decl))
10752 name = type_tag (decl);
10753 else
10754 name = lang_hooks.dwarf_name (decl, 1);
10756 /* If we don't have a name for the type, there's no point in adding
10757 it to the table. */
10758 if (name != NULL && name[0] != '\0')
10760 e.die = die;
10761 e.name = concat (scope_name, name, NULL);
10762 vec_safe_push (pubtype_table, e);
10765 /* Although it might be more consistent to add the pubinfo for the
10766 enumerators as their dies are created, they should only be added if the
10767 enum type meets the criteria above. So rather than re-check the parent
10768 enum type whenever an enumerator die is created, just output them all
10769 here. This isn't protected by the name conditional because anonymous
10770 enums don't have names. */
10771 if (die->die_tag == DW_TAG_enumeration_type)
10773 dw_die_ref c;
10775 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10780 /* Output a single entry in the pubnames table. */
10782 static void
10783 output_pubname (dw_offset die_offset, pubname_entry *entry)
10785 dw_die_ref die = entry->die;
10786 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10788 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10790 if (debug_generate_pub_sections == 2)
10792 /* This logic follows gdb's method for determining the value of the flag
10793 byte. */
10794 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10795 switch (die->die_tag)
10797 case DW_TAG_typedef:
10798 case DW_TAG_base_type:
10799 case DW_TAG_subrange_type:
10800 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10801 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10802 break;
10803 case DW_TAG_enumerator:
10804 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10805 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10806 if (!is_cxx ())
10807 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10808 break;
10809 case DW_TAG_subprogram:
10810 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10811 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10812 if (!is_ada ())
10813 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10814 break;
10815 case DW_TAG_constant:
10816 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10817 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10818 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10819 break;
10820 case DW_TAG_variable:
10821 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10822 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10823 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10824 break;
10825 case DW_TAG_namespace:
10826 case DW_TAG_imported_declaration:
10827 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10828 break;
10829 case DW_TAG_class_type:
10830 case DW_TAG_interface_type:
10831 case DW_TAG_structure_type:
10832 case DW_TAG_union_type:
10833 case DW_TAG_enumeration_type:
10834 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10835 if (!is_cxx ())
10836 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10837 break;
10838 default:
10839 /* An unusual tag. Leave the flag-byte empty. */
10840 break;
10842 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10843 "GDB-index flags");
10846 dw2_asm_output_nstring (entry->name, -1, "external name");
10850 /* Output the public names table used to speed up access to externally
10851 visible names; or the public types table used to find type definitions. */
10853 static void
10854 output_pubnames (vec<pubname_entry, va_gc> *names)
10856 unsigned i;
10857 unsigned long pubnames_length = size_of_pubnames (names);
10858 pubname_entry *pub;
10860 if (!XCOFF_DEBUGGING_INFO)
10862 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10863 dw2_asm_output_data (4, 0xffffffff,
10864 "Initial length escape value indicating 64-bit DWARF extension");
10865 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10866 "Pub Info Length");
10869 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10870 dw2_asm_output_data (2, 2, "DWARF Version");
10872 if (dwarf_split_debug_info)
10873 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10874 debug_skeleton_info_section,
10875 "Offset of Compilation Unit Info");
10876 else
10877 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10878 debug_info_section,
10879 "Offset of Compilation Unit Info");
10880 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10881 "Compilation Unit Length");
10883 FOR_EACH_VEC_ELT (*names, i, pub)
10885 if (include_pubname_in_output (names, pub))
10887 dw_offset die_offset = pub->die->die_offset;
10889 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10890 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10891 gcc_assert (pub->die->die_mark);
10893 /* If we're putting types in their own .debug_types sections,
10894 the .debug_pubtypes table will still point to the compile
10895 unit (not the type unit), so we want to use the offset of
10896 the skeleton DIE (if there is one). */
10897 if (pub->die->comdat_type_p && names == pubtype_table)
10899 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10901 if (type_node != NULL)
10902 die_offset = (type_node->skeleton_die != NULL
10903 ? type_node->skeleton_die->die_offset
10904 : comp_unit_die ()->die_offset);
10907 output_pubname (die_offset, pub);
10911 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10914 /* Output public names and types tables if necessary. */
10916 static void
10917 output_pubtables (void)
10919 if (!want_pubnames () || !info_section_emitted)
10920 return;
10922 switch_to_section (debug_pubnames_section);
10923 output_pubnames (pubname_table);
10924 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10925 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10926 simply won't look for the section. */
10927 switch_to_section (debug_pubtypes_section);
10928 output_pubnames (pubtype_table);
10932 /* Output the information that goes into the .debug_aranges table.
10933 Namely, define the beginning and ending address range of the
10934 text section generated for this compilation unit. */
10936 static void
10937 output_aranges (void)
10939 unsigned i;
10940 unsigned long aranges_length = size_of_aranges ();
10942 if (!XCOFF_DEBUGGING_INFO)
10944 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10945 dw2_asm_output_data (4, 0xffffffff,
10946 "Initial length escape value indicating 64-bit DWARF extension");
10947 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10948 "Length of Address Ranges Info");
10951 /* Version number for aranges is still 2, even up to DWARF5. */
10952 dw2_asm_output_data (2, 2, "DWARF Version");
10953 if (dwarf_split_debug_info)
10954 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10955 debug_skeleton_info_section,
10956 "Offset of Compilation Unit Info");
10957 else
10958 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10959 debug_info_section,
10960 "Offset of Compilation Unit Info");
10961 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10962 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10964 /* We need to align to twice the pointer size here. */
10965 if (DWARF_ARANGES_PAD_SIZE)
10967 /* Pad using a 2 byte words so that padding is correct for any
10968 pointer size. */
10969 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10970 2 * DWARF2_ADDR_SIZE);
10971 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10972 dw2_asm_output_data (2, 0, NULL);
10975 /* It is necessary not to output these entries if the sections were
10976 not used; if the sections were not used, the length will be 0 and
10977 the address may end up as 0 if the section is discarded by ld
10978 --gc-sections, leaving an invalid (0, 0) entry that can be
10979 confused with the terminator. */
10980 if (text_section_used)
10982 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10983 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10984 text_section_label, "Length");
10986 if (cold_text_section_used)
10988 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10989 "Address");
10990 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10991 cold_text_section_label, "Length");
10994 if (have_multiple_function_sections)
10996 unsigned fde_idx;
10997 dw_fde_ref fde;
10999 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11001 if (DECL_IGNORED_P (fde->decl))
11002 continue;
11003 if (!fde->in_std_section)
11005 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11006 "Address");
11007 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11008 fde->dw_fde_begin, "Length");
11010 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11012 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11013 "Address");
11014 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11015 fde->dw_fde_second_begin, "Length");
11020 /* Output the terminator words. */
11021 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11022 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11025 /* Add a new entry to .debug_ranges. Return its index into
11026 ranges_table vector. */
11028 static unsigned int
11029 add_ranges_num (int num, bool maybe_new_sec)
11031 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11032 vec_safe_push (ranges_table, r);
11033 return vec_safe_length (ranges_table) - 1;
11036 /* Add a new entry to .debug_ranges corresponding to a block, or a
11037 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11038 this entry might be in a different section from previous range. */
11040 static unsigned int
11041 add_ranges (const_tree block, bool maybe_new_sec)
11043 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11046 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11047 chain, or middle entry of a chain that will be directly referred to. */
11049 static void
11050 note_rnglist_head (unsigned int offset)
11052 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11053 return;
11054 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11057 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11058 When using dwarf_split_debug_info, address attributes in dies destined
11059 for the final executable should be direct references--setting the
11060 parameter force_direct ensures this behavior. */
11062 static void
11063 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11064 bool *added, bool force_direct)
11066 unsigned int in_use = vec_safe_length (ranges_by_label);
11067 unsigned int offset;
11068 dw_ranges_by_label rbl = { begin, end };
11069 vec_safe_push (ranges_by_label, rbl);
11070 offset = add_ranges_num (-(int)in_use - 1, true);
11071 if (!*added)
11073 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11074 *added = true;
11075 note_rnglist_head (offset);
11079 /* Emit .debug_ranges section. */
11081 static void
11082 output_ranges (void)
11084 unsigned i;
11085 static const char *const start_fmt = "Offset %#x";
11086 const char *fmt = start_fmt;
11087 dw_ranges *r;
11089 switch_to_section (debug_ranges_section);
11090 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11091 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11093 int block_num = r->num;
11095 if (block_num > 0)
11097 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11098 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11100 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11101 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11103 /* If all code is in the text section, then the compilation
11104 unit base address defaults to DW_AT_low_pc, which is the
11105 base of the text section. */
11106 if (!have_multiple_function_sections)
11108 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11109 text_section_label,
11110 fmt, i * 2 * DWARF2_ADDR_SIZE);
11111 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11112 text_section_label, NULL);
11115 /* Otherwise, the compilation unit base address is zero,
11116 which allows us to use absolute addresses, and not worry
11117 about whether the target supports cross-section
11118 arithmetic. */
11119 else
11121 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11122 fmt, i * 2 * DWARF2_ADDR_SIZE);
11123 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11126 fmt = NULL;
11129 /* Negative block_num stands for an index into ranges_by_label. */
11130 else if (block_num < 0)
11132 int lab_idx = - block_num - 1;
11134 if (!have_multiple_function_sections)
11136 gcc_unreachable ();
11137 #if 0
11138 /* If we ever use add_ranges_by_labels () for a single
11139 function section, all we have to do is to take out
11140 the #if 0 above. */
11141 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11142 (*ranges_by_label)[lab_idx].begin,
11143 text_section_label,
11144 fmt, i * 2 * DWARF2_ADDR_SIZE);
11145 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11146 (*ranges_by_label)[lab_idx].end,
11147 text_section_label, NULL);
11148 #endif
11150 else
11152 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11153 (*ranges_by_label)[lab_idx].begin,
11154 fmt, i * 2 * DWARF2_ADDR_SIZE);
11155 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11156 (*ranges_by_label)[lab_idx].end,
11157 NULL);
11160 else
11162 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11163 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11164 fmt = start_fmt;
11169 /* Non-zero if .debug_line_str should be used for .debug_line section
11170 strings or strings that are likely shareable with those. */
11171 #define DWARF5_USE_DEBUG_LINE_STR \
11172 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11173 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11174 /* FIXME: there is no .debug_line_str.dwo section, \
11175 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11176 && !dwarf_split_debug_info)
11178 /* Assign .debug_rnglists indexes. */
11180 static void
11181 index_rnglists (void)
11183 unsigned i;
11184 dw_ranges *r;
11186 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11187 if (r->label)
11188 r->idx = rnglist_idx++;
11191 /* Emit .debug_rnglists section. */
11193 static void
11194 output_rnglists (unsigned generation)
11196 unsigned i;
11197 dw_ranges *r;
11198 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11199 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11200 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11202 switch_to_section (debug_ranges_section);
11203 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11204 /* There are up to 4 unique ranges labels per generation.
11205 See also init_sections_and_labels. */
11206 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11207 2 + generation * 4);
11208 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11209 3 + generation * 4);
11210 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11211 dw2_asm_output_data (4, 0xffffffff,
11212 "Initial length escape value indicating "
11213 "64-bit DWARF extension");
11214 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11215 "Length of Range Lists");
11216 ASM_OUTPUT_LABEL (asm_out_file, l1);
11217 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11218 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11219 dw2_asm_output_data (1, 0, "Segment Size");
11220 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11221 about relocation sizes and primarily care about the size of .debug*
11222 sections in linked shared libraries and executables, then
11223 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11224 into it are usually larger than just DW_FORM_sec_offset offsets
11225 into the .debug_rnglists section. */
11226 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11227 "Offset Entry Count");
11228 if (dwarf_split_debug_info)
11230 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11231 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11232 if (r->label)
11233 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11234 ranges_base_label, NULL);
11237 const char *lab = "";
11238 unsigned int len = vec_safe_length (ranges_table);
11239 const char *base = NULL;
11240 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11242 int block_num = r->num;
11244 if (r->label)
11246 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11247 lab = r->label;
11249 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11250 base = NULL;
11251 if (block_num > 0)
11253 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11254 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11256 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11257 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11259 if (HAVE_AS_LEB128)
11261 /* If all code is in the text section, then the compilation
11262 unit base address defaults to DW_AT_low_pc, which is the
11263 base of the text section. */
11264 if (!have_multiple_function_sections)
11266 dw2_asm_output_data (1, DW_RLE_offset_pair,
11267 "DW_RLE_offset_pair (%s)", lab);
11268 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11269 "Range begin address (%s)", lab);
11270 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11271 "Range end address (%s)", lab);
11272 continue;
11274 if (base == NULL)
11276 dw_ranges *r2 = NULL;
11277 if (i < len - 1)
11278 r2 = &(*ranges_table)[i + 1];
11279 if (r2
11280 && r2->num != 0
11281 && r2->label == NULL
11282 && !r2->maybe_new_sec)
11284 dw2_asm_output_data (1, DW_RLE_base_address,
11285 "DW_RLE_base_address (%s)", lab);
11286 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11287 "Base address (%s)", lab);
11288 strcpy (basebuf, blabel);
11289 base = basebuf;
11292 if (base)
11294 dw2_asm_output_data (1, DW_RLE_offset_pair,
11295 "DW_RLE_offset_pair (%s)", lab);
11296 dw2_asm_output_delta_uleb128 (blabel, base,
11297 "Range begin address (%s)", lab);
11298 dw2_asm_output_delta_uleb128 (elabel, base,
11299 "Range end address (%s)", lab);
11300 continue;
11302 dw2_asm_output_data (1, DW_RLE_start_length,
11303 "DW_RLE_start_length (%s)", lab);
11304 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11305 "Range begin address (%s)", lab);
11306 dw2_asm_output_delta_uleb128 (elabel, blabel,
11307 "Range length (%s)", lab);
11309 else
11311 dw2_asm_output_data (1, DW_RLE_start_end,
11312 "DW_RLE_start_end (%s)", lab);
11313 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11314 "Range begin address (%s)", lab);
11315 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11316 "Range end address (%s)", lab);
11320 /* Negative block_num stands for an index into ranges_by_label. */
11321 else if (block_num < 0)
11323 int lab_idx = - block_num - 1;
11324 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11325 const char *elabel = (*ranges_by_label)[lab_idx].end;
11327 if (!have_multiple_function_sections)
11328 gcc_unreachable ();
11329 if (HAVE_AS_LEB128)
11331 dw2_asm_output_data (1, DW_RLE_start_length,
11332 "DW_RLE_start_length (%s)", lab);
11333 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11334 "Range begin address (%s)", lab);
11335 dw2_asm_output_delta_uleb128 (elabel, blabel,
11336 "Range length (%s)", lab);
11338 else
11340 dw2_asm_output_data (1, DW_RLE_start_end,
11341 "DW_RLE_start_end (%s)", lab);
11342 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11343 "Range begin address (%s)", lab);
11344 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11345 "Range end address (%s)", lab);
11348 else
11349 dw2_asm_output_data (1, DW_RLE_end_of_list,
11350 "DW_RLE_end_of_list (%s)", lab);
11352 ASM_OUTPUT_LABEL (asm_out_file, l2);
11355 /* Data structure containing information about input files. */
11356 struct file_info
11358 const char *path; /* Complete file name. */
11359 const char *fname; /* File name part. */
11360 int length; /* Length of entire string. */
11361 struct dwarf_file_data * file_idx; /* Index in input file table. */
11362 int dir_idx; /* Index in directory table. */
11365 /* Data structure containing information about directories with source
11366 files. */
11367 struct dir_info
11369 const char *path; /* Path including directory name. */
11370 int length; /* Path length. */
11371 int prefix; /* Index of directory entry which is a prefix. */
11372 int count; /* Number of files in this directory. */
11373 int dir_idx; /* Index of directory used as base. */
11376 /* Callback function for file_info comparison. We sort by looking at
11377 the directories in the path. */
11379 static int
11380 file_info_cmp (const void *p1, const void *p2)
11382 const struct file_info *const s1 = (const struct file_info *) p1;
11383 const struct file_info *const s2 = (const struct file_info *) p2;
11384 const unsigned char *cp1;
11385 const unsigned char *cp2;
11387 /* Take care of file names without directories. We need to make sure that
11388 we return consistent values to qsort since some will get confused if
11389 we return the same value when identical operands are passed in opposite
11390 orders. So if neither has a directory, return 0 and otherwise return
11391 1 or -1 depending on which one has the directory. */
11392 if ((s1->path == s1->fname || s2->path == s2->fname))
11393 return (s2->path == s2->fname) - (s1->path == s1->fname);
11395 cp1 = (const unsigned char *) s1->path;
11396 cp2 = (const unsigned char *) s2->path;
11398 while (1)
11400 ++cp1;
11401 ++cp2;
11402 /* Reached the end of the first path? If so, handle like above. */
11403 if ((cp1 == (const unsigned char *) s1->fname)
11404 || (cp2 == (const unsigned char *) s2->fname))
11405 return ((cp2 == (const unsigned char *) s2->fname)
11406 - (cp1 == (const unsigned char *) s1->fname));
11408 /* Character of current path component the same? */
11409 else if (*cp1 != *cp2)
11410 return *cp1 - *cp2;
11414 struct file_name_acquire_data
11416 struct file_info *files;
11417 int used_files;
11418 int max_files;
11421 /* Traversal function for the hash table. */
11424 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11426 struct dwarf_file_data *d = *slot;
11427 struct file_info *fi;
11428 const char *f;
11430 gcc_assert (fnad->max_files >= d->emitted_number);
11432 if (! d->emitted_number)
11433 return 1;
11435 gcc_assert (fnad->max_files != fnad->used_files);
11437 fi = fnad->files + fnad->used_files++;
11439 /* Skip all leading "./". */
11440 f = d->filename;
11441 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11442 f += 2;
11444 /* Create a new array entry. */
11445 fi->path = f;
11446 fi->length = strlen (f);
11447 fi->file_idx = d;
11449 /* Search for the file name part. */
11450 f = strrchr (f, DIR_SEPARATOR);
11451 #if defined (DIR_SEPARATOR_2)
11453 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11455 if (g != NULL)
11457 if (f == NULL || f < g)
11458 f = g;
11461 #endif
11463 fi->fname = f == NULL ? fi->path : f + 1;
11464 return 1;
11467 /* Helper function for output_file_names. Emit a FORM encoded
11468 string STR, with assembly comment start ENTRY_KIND and
11469 index IDX */
11471 static void
11472 output_line_string (enum dwarf_form form, const char *str,
11473 const char *entry_kind, unsigned int idx)
11475 switch (form)
11477 case DW_FORM_string:
11478 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11479 break;
11480 case DW_FORM_line_strp:
11481 if (!debug_line_str_hash)
11482 debug_line_str_hash
11483 = hash_table<indirect_string_hasher>::create_ggc (10);
11485 struct indirect_string_node *node;
11486 node = find_AT_string_in_table (str, debug_line_str_hash);
11487 set_indirect_string (node);
11488 node->form = form;
11489 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11490 debug_line_str_section, "%s: %#x: \"%s\"",
11491 entry_kind, 0, node->str);
11492 break;
11493 default:
11494 gcc_unreachable ();
11498 /* Output the directory table and the file name table. We try to minimize
11499 the total amount of memory needed. A heuristic is used to avoid large
11500 slowdowns with many input files. */
11502 static void
11503 output_file_names (void)
11505 struct file_name_acquire_data fnad;
11506 int numfiles;
11507 struct file_info *files;
11508 struct dir_info *dirs;
11509 int *saved;
11510 int *savehere;
11511 int *backmap;
11512 int ndirs;
11513 int idx_offset;
11514 int i;
11516 if (!last_emitted_file)
11518 if (dwarf_version >= 5)
11520 dw2_asm_output_data (1, 0, "Directory entry format count");
11521 dw2_asm_output_data_uleb128 (0, "Directories count");
11522 dw2_asm_output_data (1, 0, "File name entry format count");
11523 dw2_asm_output_data_uleb128 (0, "File names count");
11525 else
11527 dw2_asm_output_data (1, 0, "End directory table");
11528 dw2_asm_output_data (1, 0, "End file name table");
11530 return;
11533 numfiles = last_emitted_file->emitted_number;
11535 /* Allocate the various arrays we need. */
11536 files = XALLOCAVEC (struct file_info, numfiles);
11537 dirs = XALLOCAVEC (struct dir_info, numfiles);
11539 fnad.files = files;
11540 fnad.used_files = 0;
11541 fnad.max_files = numfiles;
11542 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11543 gcc_assert (fnad.used_files == fnad.max_files);
11545 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11547 /* Find all the different directories used. */
11548 dirs[0].path = files[0].path;
11549 dirs[0].length = files[0].fname - files[0].path;
11550 dirs[0].prefix = -1;
11551 dirs[0].count = 1;
11552 dirs[0].dir_idx = 0;
11553 files[0].dir_idx = 0;
11554 ndirs = 1;
11556 for (i = 1; i < numfiles; i++)
11557 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11558 && memcmp (dirs[ndirs - 1].path, files[i].path,
11559 dirs[ndirs - 1].length) == 0)
11561 /* Same directory as last entry. */
11562 files[i].dir_idx = ndirs - 1;
11563 ++dirs[ndirs - 1].count;
11565 else
11567 int j;
11569 /* This is a new directory. */
11570 dirs[ndirs].path = files[i].path;
11571 dirs[ndirs].length = files[i].fname - files[i].path;
11572 dirs[ndirs].count = 1;
11573 dirs[ndirs].dir_idx = ndirs;
11574 files[i].dir_idx = ndirs;
11576 /* Search for a prefix. */
11577 dirs[ndirs].prefix = -1;
11578 for (j = 0; j < ndirs; j++)
11579 if (dirs[j].length < dirs[ndirs].length
11580 && dirs[j].length > 1
11581 && (dirs[ndirs].prefix == -1
11582 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11583 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11584 dirs[ndirs].prefix = j;
11586 ++ndirs;
11589 /* Now to the actual work. We have to find a subset of the directories which
11590 allow expressing the file name using references to the directory table
11591 with the least amount of characters. We do not do an exhaustive search
11592 where we would have to check out every combination of every single
11593 possible prefix. Instead we use a heuristic which provides nearly optimal
11594 results in most cases and never is much off. */
11595 saved = XALLOCAVEC (int, ndirs);
11596 savehere = XALLOCAVEC (int, ndirs);
11598 memset (saved, '\0', ndirs * sizeof (saved[0]));
11599 for (i = 0; i < ndirs; i++)
11601 int j;
11602 int total;
11604 /* We can always save some space for the current directory. But this
11605 does not mean it will be enough to justify adding the directory. */
11606 savehere[i] = dirs[i].length;
11607 total = (savehere[i] - saved[i]) * dirs[i].count;
11609 for (j = i + 1; j < ndirs; j++)
11611 savehere[j] = 0;
11612 if (saved[j] < dirs[i].length)
11614 /* Determine whether the dirs[i] path is a prefix of the
11615 dirs[j] path. */
11616 int k;
11618 k = dirs[j].prefix;
11619 while (k != -1 && k != (int) i)
11620 k = dirs[k].prefix;
11622 if (k == (int) i)
11624 /* Yes it is. We can possibly save some memory by
11625 writing the filenames in dirs[j] relative to
11626 dirs[i]. */
11627 savehere[j] = dirs[i].length;
11628 total += (savehere[j] - saved[j]) * dirs[j].count;
11633 /* Check whether we can save enough to justify adding the dirs[i]
11634 directory. */
11635 if (total > dirs[i].length + 1)
11637 /* It's worthwhile adding. */
11638 for (j = i; j < ndirs; j++)
11639 if (savehere[j] > 0)
11641 /* Remember how much we saved for this directory so far. */
11642 saved[j] = savehere[j];
11644 /* Remember the prefix directory. */
11645 dirs[j].dir_idx = i;
11650 /* Emit the directory name table. */
11651 idx_offset = dirs[0].length > 0 ? 1 : 0;
11652 enum dwarf_form str_form = DW_FORM_string;
11653 enum dwarf_form idx_form = DW_FORM_udata;
11654 if (dwarf_version >= 5)
11656 const char *comp_dir = comp_dir_string ();
11657 if (comp_dir == NULL)
11658 comp_dir = "";
11659 dw2_asm_output_data (1, 1, "Directory entry format count");
11660 if (DWARF5_USE_DEBUG_LINE_STR)
11661 str_form = DW_FORM_line_strp;
11662 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11663 dw2_asm_output_data_uleb128 (str_form, "%s",
11664 get_DW_FORM_name (str_form));
11665 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11666 if (str_form == DW_FORM_string)
11668 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11669 for (i = 1 - idx_offset; i < ndirs; i++)
11670 dw2_asm_output_nstring (dirs[i].path,
11671 dirs[i].length
11672 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11673 "Directory Entry: %#x", i + idx_offset);
11675 else
11677 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11678 for (i = 1 - idx_offset; i < ndirs; i++)
11680 const char *str
11681 = ggc_alloc_string (dirs[i].path,
11682 dirs[i].length
11683 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11684 output_line_string (str_form, str, "Directory Entry",
11685 (unsigned) i + idx_offset);
11689 else
11691 for (i = 1 - idx_offset; i < ndirs; i++)
11692 dw2_asm_output_nstring (dirs[i].path,
11693 dirs[i].length
11694 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11695 "Directory Entry: %#x", i + idx_offset);
11697 dw2_asm_output_data (1, 0, "End directory table");
11700 /* We have to emit them in the order of emitted_number since that's
11701 used in the debug info generation. To do this efficiently we
11702 generate a back-mapping of the indices first. */
11703 backmap = XALLOCAVEC (int, numfiles);
11704 for (i = 0; i < numfiles; i++)
11705 backmap[files[i].file_idx->emitted_number - 1] = i;
11707 if (dwarf_version >= 5)
11709 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11710 if (filename0 == NULL)
11711 filename0 = "";
11712 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11713 DW_FORM_data2. Choose one based on the number of directories
11714 and how much space would they occupy in each encoding.
11715 If we have at most 256 directories, all indexes fit into
11716 a single byte, so DW_FORM_data1 is most compact (if there
11717 are at most 128 directories, DW_FORM_udata would be as
11718 compact as that, but not shorter and slower to decode). */
11719 if (ndirs + idx_offset <= 256)
11720 idx_form = DW_FORM_data1;
11721 /* If there are more than 65536 directories, we have to use
11722 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11723 Otherwise, compute what space would occupy if all the indexes
11724 used DW_FORM_udata - sum - and compare that to how large would
11725 be DW_FORM_data2 encoding, and pick the more efficient one. */
11726 else if (ndirs + idx_offset <= 65536)
11728 unsigned HOST_WIDE_INT sum = 1;
11729 for (i = 0; i < numfiles; i++)
11731 int file_idx = backmap[i];
11732 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11733 sum += size_of_uleb128 (dir_idx);
11735 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11736 idx_form = DW_FORM_data2;
11738 #ifdef VMS_DEBUGGING_INFO
11739 dw2_asm_output_data (1, 4, "File name entry format count");
11740 #else
11741 dw2_asm_output_data (1, 2, "File name entry format count");
11742 #endif
11743 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11744 dw2_asm_output_data_uleb128 (str_form, "%s",
11745 get_DW_FORM_name (str_form));
11746 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11747 "DW_LNCT_directory_index");
11748 dw2_asm_output_data_uleb128 (idx_form, "%s",
11749 get_DW_FORM_name (idx_form));
11750 #ifdef VMS_DEBUGGING_INFO
11751 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11752 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11753 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11754 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11755 #endif
11756 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11758 output_line_string (str_form, filename0, "File Entry", 0);
11760 /* Include directory index. */
11761 if (idx_form != DW_FORM_udata)
11762 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11763 0, NULL);
11764 else
11765 dw2_asm_output_data_uleb128 (0, NULL);
11767 #ifdef VMS_DEBUGGING_INFO
11768 dw2_asm_output_data_uleb128 (0, NULL);
11769 dw2_asm_output_data_uleb128 (0, NULL);
11770 #endif
11773 /* Now write all the file names. */
11774 for (i = 0; i < numfiles; i++)
11776 int file_idx = backmap[i];
11777 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11779 #ifdef VMS_DEBUGGING_INFO
11780 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11782 /* Setting these fields can lead to debugger miscomparisons,
11783 but VMS Debug requires them to be set correctly. */
11785 int ver;
11786 long long cdt;
11787 long siz;
11788 int maxfilelen = (strlen (files[file_idx].path)
11789 + dirs[dir_idx].length
11790 + MAX_VMS_VERSION_LEN + 1);
11791 char *filebuf = XALLOCAVEC (char, maxfilelen);
11793 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11794 snprintf (filebuf, maxfilelen, "%s;%d",
11795 files[file_idx].path + dirs[dir_idx].length, ver);
11797 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11799 /* Include directory index. */
11800 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11801 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11802 dir_idx + idx_offset, NULL);
11803 else
11804 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11806 /* Modification time. */
11807 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11808 &cdt, 0, 0, 0) == 0)
11809 ? cdt : 0, NULL);
11811 /* File length in bytes. */
11812 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11813 0, &siz, 0, 0) == 0)
11814 ? siz : 0, NULL);
11815 #else
11816 output_line_string (str_form,
11817 files[file_idx].path + dirs[dir_idx].length,
11818 "File Entry", (unsigned) i + 1);
11820 /* Include directory index. */
11821 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11822 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11823 dir_idx + idx_offset, NULL);
11824 else
11825 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11827 if (dwarf_version >= 5)
11828 continue;
11830 /* Modification time. */
11831 dw2_asm_output_data_uleb128 (0, NULL);
11833 /* File length in bytes. */
11834 dw2_asm_output_data_uleb128 (0, NULL);
11835 #endif /* VMS_DEBUGGING_INFO */
11838 if (dwarf_version < 5)
11839 dw2_asm_output_data (1, 0, "End file name table");
11843 /* Output one line number table into the .debug_line section. */
11845 static void
11846 output_one_line_info_table (dw_line_info_table *table)
11848 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11849 unsigned int current_line = 1;
11850 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11851 dw_line_info_entry *ent;
11852 size_t i;
11854 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11856 switch (ent->opcode)
11858 case LI_set_address:
11859 /* ??? Unfortunately, we have little choice here currently, and
11860 must always use the most general form. GCC does not know the
11861 address delta itself, so we can't use DW_LNS_advance_pc. Many
11862 ports do have length attributes which will give an upper bound
11863 on the address range. We could perhaps use length attributes
11864 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11865 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11867 /* This can handle any delta. This takes
11868 4+DWARF2_ADDR_SIZE bytes. */
11869 dw2_asm_output_data (1, 0, "set address %s", line_label);
11870 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11871 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11872 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11873 break;
11875 case LI_set_line:
11876 if (ent->val == current_line)
11878 /* We still need to start a new row, so output a copy insn. */
11879 dw2_asm_output_data (1, DW_LNS_copy,
11880 "copy line %u", current_line);
11882 else
11884 int line_offset = ent->val - current_line;
11885 int line_delta = line_offset - DWARF_LINE_BASE;
11887 current_line = ent->val;
11888 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11890 /* This can handle deltas from -10 to 234, using the current
11891 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11892 This takes 1 byte. */
11893 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11894 "line %u", current_line);
11896 else
11898 /* This can handle any delta. This takes at least 4 bytes,
11899 depending on the value being encoded. */
11900 dw2_asm_output_data (1, DW_LNS_advance_line,
11901 "advance to line %u", current_line);
11902 dw2_asm_output_data_sleb128 (line_offset, NULL);
11903 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11906 break;
11908 case LI_set_file:
11909 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11910 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11911 break;
11913 case LI_set_column:
11914 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11915 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11916 break;
11918 case LI_negate_stmt:
11919 current_is_stmt = !current_is_stmt;
11920 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11921 "is_stmt %d", current_is_stmt);
11922 break;
11924 case LI_set_prologue_end:
11925 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11926 "set prologue end");
11927 break;
11929 case LI_set_epilogue_begin:
11930 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11931 "set epilogue begin");
11932 break;
11934 case LI_set_discriminator:
11935 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11936 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11937 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11938 dw2_asm_output_data_uleb128 (ent->val, NULL);
11939 break;
11943 /* Emit debug info for the address of the end of the table. */
11944 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11945 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11946 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11947 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11949 dw2_asm_output_data (1, 0, "end sequence");
11950 dw2_asm_output_data_uleb128 (1, NULL);
11951 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11954 /* Output the source line number correspondence information. This
11955 information goes into the .debug_line section. */
11957 static void
11958 output_line_info (bool prologue_only)
11960 static unsigned int generation;
11961 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11962 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11963 bool saw_one = false;
11964 int opc;
11966 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11967 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11968 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11969 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11971 if (!XCOFF_DEBUGGING_INFO)
11973 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11974 dw2_asm_output_data (4, 0xffffffff,
11975 "Initial length escape value indicating 64-bit DWARF extension");
11976 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11977 "Length of Source Line Info");
11980 ASM_OUTPUT_LABEL (asm_out_file, l1);
11982 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11983 if (dwarf_version >= 5)
11985 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11986 dw2_asm_output_data (1, 0, "Segment Size");
11988 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11989 ASM_OUTPUT_LABEL (asm_out_file, p1);
11991 /* Define the architecture-dependent minimum instruction length (in bytes).
11992 In this implementation of DWARF, this field is used for information
11993 purposes only. Since GCC generates assembly language, we have no
11994 a priori knowledge of how many instruction bytes are generated for each
11995 source line, and therefore can use only the DW_LNE_set_address and
11996 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11997 this as '1', which is "correct enough" for all architectures,
11998 and don't let the target override. */
11999 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12001 if (dwarf_version >= 4)
12002 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12003 "Maximum Operations Per Instruction");
12004 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12005 "Default is_stmt_start flag");
12006 dw2_asm_output_data (1, DWARF_LINE_BASE,
12007 "Line Base Value (Special Opcodes)");
12008 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12009 "Line Range Value (Special Opcodes)");
12010 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12011 "Special Opcode Base");
12013 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12015 int n_op_args;
12016 switch (opc)
12018 case DW_LNS_advance_pc:
12019 case DW_LNS_advance_line:
12020 case DW_LNS_set_file:
12021 case DW_LNS_set_column:
12022 case DW_LNS_fixed_advance_pc:
12023 case DW_LNS_set_isa:
12024 n_op_args = 1;
12025 break;
12026 default:
12027 n_op_args = 0;
12028 break;
12031 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12032 opc, n_op_args);
12035 /* Write out the information about the files we use. */
12036 output_file_names ();
12037 ASM_OUTPUT_LABEL (asm_out_file, p2);
12038 if (prologue_only)
12040 /* Output the marker for the end of the line number info. */
12041 ASM_OUTPUT_LABEL (asm_out_file, l2);
12042 return;
12045 if (separate_line_info)
12047 dw_line_info_table *table;
12048 size_t i;
12050 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12051 if (table->in_use)
12053 output_one_line_info_table (table);
12054 saw_one = true;
12057 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12059 output_one_line_info_table (cold_text_section_line_info);
12060 saw_one = true;
12063 /* ??? Some Darwin linkers crash on a .debug_line section with no
12064 sequences. Further, merely a DW_LNE_end_sequence entry is not
12065 sufficient -- the address column must also be initialized.
12066 Make sure to output at least one set_address/end_sequence pair,
12067 choosing .text since that section is always present. */
12068 if (text_section_line_info->in_use || !saw_one)
12069 output_one_line_info_table (text_section_line_info);
12071 /* Output the marker for the end of the line number info. */
12072 ASM_OUTPUT_LABEL (asm_out_file, l2);
12075 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12077 static inline bool
12078 need_endianity_attribute_p (bool reverse)
12080 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12083 /* Given a pointer to a tree node for some base type, return a pointer to
12084 a DIE that describes the given type. REVERSE is true if the type is
12085 to be interpreted in the reverse storage order wrt the target order.
12087 This routine must only be called for GCC type nodes that correspond to
12088 Dwarf base (fundamental) types. */
12090 static dw_die_ref
12091 base_type_die (tree type, bool reverse)
12093 dw_die_ref base_type_result;
12094 enum dwarf_type encoding;
12095 bool fpt_used = false;
12096 struct fixed_point_type_info fpt_info;
12097 tree type_bias = NULL_TREE;
12099 /* If this is a subtype that should not be emitted as a subrange type,
12100 use the base type. See subrange_type_for_debug_p. */
12101 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12102 type = TREE_TYPE (type);
12104 switch (TREE_CODE (type))
12106 case INTEGER_TYPE:
12107 if ((dwarf_version >= 4 || !dwarf_strict)
12108 && TYPE_NAME (type)
12109 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12110 && DECL_IS_BUILTIN (TYPE_NAME (type))
12111 && DECL_NAME (TYPE_NAME (type)))
12113 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12114 if (strcmp (name, "char16_t") == 0
12115 || strcmp (name, "char32_t") == 0)
12117 encoding = DW_ATE_UTF;
12118 break;
12121 if ((dwarf_version >= 3 || !dwarf_strict)
12122 && lang_hooks.types.get_fixed_point_type_info)
12124 memset (&fpt_info, 0, sizeof (fpt_info));
12125 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12127 fpt_used = true;
12128 encoding = ((TYPE_UNSIGNED (type))
12129 ? DW_ATE_unsigned_fixed
12130 : DW_ATE_signed_fixed);
12131 break;
12134 if (TYPE_STRING_FLAG (type))
12136 if (TYPE_UNSIGNED (type))
12137 encoding = DW_ATE_unsigned_char;
12138 else
12139 encoding = DW_ATE_signed_char;
12141 else if (TYPE_UNSIGNED (type))
12142 encoding = DW_ATE_unsigned;
12143 else
12144 encoding = DW_ATE_signed;
12146 if (!dwarf_strict
12147 && lang_hooks.types.get_type_bias)
12148 type_bias = lang_hooks.types.get_type_bias (type);
12149 break;
12151 case REAL_TYPE:
12152 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12154 if (dwarf_version >= 3 || !dwarf_strict)
12155 encoding = DW_ATE_decimal_float;
12156 else
12157 encoding = DW_ATE_lo_user;
12159 else
12160 encoding = DW_ATE_float;
12161 break;
12163 case FIXED_POINT_TYPE:
12164 if (!(dwarf_version >= 3 || !dwarf_strict))
12165 encoding = DW_ATE_lo_user;
12166 else if (TYPE_UNSIGNED (type))
12167 encoding = DW_ATE_unsigned_fixed;
12168 else
12169 encoding = DW_ATE_signed_fixed;
12170 break;
12172 /* Dwarf2 doesn't know anything about complex ints, so use
12173 a user defined type for it. */
12174 case COMPLEX_TYPE:
12175 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12176 encoding = DW_ATE_complex_float;
12177 else
12178 encoding = DW_ATE_lo_user;
12179 break;
12181 case BOOLEAN_TYPE:
12182 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12183 encoding = DW_ATE_boolean;
12184 break;
12186 default:
12187 /* No other TREE_CODEs are Dwarf fundamental types. */
12188 gcc_unreachable ();
12191 base_type_result = new_die_raw (DW_TAG_base_type);
12193 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12194 int_size_in_bytes (type));
12195 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12197 if (need_endianity_attribute_p (reverse))
12198 add_AT_unsigned (base_type_result, DW_AT_endianity,
12199 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12201 add_alignment_attribute (base_type_result, type);
12203 if (fpt_used)
12205 switch (fpt_info.scale_factor_kind)
12207 case fixed_point_scale_factor_binary:
12208 add_AT_int (base_type_result, DW_AT_binary_scale,
12209 fpt_info.scale_factor.binary);
12210 break;
12212 case fixed_point_scale_factor_decimal:
12213 add_AT_int (base_type_result, DW_AT_decimal_scale,
12214 fpt_info.scale_factor.decimal);
12215 break;
12217 case fixed_point_scale_factor_arbitrary:
12218 /* Arbitrary scale factors cannot be described in standard DWARF,
12219 yet. */
12220 if (!dwarf_strict)
12222 /* Describe the scale factor as a rational constant. */
12223 const dw_die_ref scale_factor
12224 = new_die (DW_TAG_constant, comp_unit_die (), type);
12226 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12227 fpt_info.scale_factor.arbitrary.numerator);
12228 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12229 fpt_info.scale_factor.arbitrary.denominator);
12231 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12233 break;
12235 default:
12236 gcc_unreachable ();
12240 if (type_bias)
12241 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12242 dw_scalar_form_constant
12243 | dw_scalar_form_exprloc
12244 | dw_scalar_form_reference,
12245 NULL);
12247 return base_type_result;
12250 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12251 named 'auto' in its type: return true for it, false otherwise. */
12253 static inline bool
12254 is_cxx_auto (tree type)
12256 if (is_cxx ())
12258 tree name = TYPE_IDENTIFIER (type);
12259 if (name == get_identifier ("auto")
12260 || name == get_identifier ("decltype(auto)"))
12261 return true;
12263 return false;
12266 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12267 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12269 static inline int
12270 is_base_type (tree type)
12272 switch (TREE_CODE (type))
12274 case INTEGER_TYPE:
12275 case REAL_TYPE:
12276 case FIXED_POINT_TYPE:
12277 case COMPLEX_TYPE:
12278 case BOOLEAN_TYPE:
12279 case POINTER_BOUNDS_TYPE:
12280 return 1;
12282 case VOID_TYPE:
12283 case ARRAY_TYPE:
12284 case RECORD_TYPE:
12285 case UNION_TYPE:
12286 case QUAL_UNION_TYPE:
12287 case ENUMERAL_TYPE:
12288 case FUNCTION_TYPE:
12289 case METHOD_TYPE:
12290 case POINTER_TYPE:
12291 case REFERENCE_TYPE:
12292 case NULLPTR_TYPE:
12293 case OFFSET_TYPE:
12294 case LANG_TYPE:
12295 case VECTOR_TYPE:
12296 return 0;
12298 default:
12299 if (is_cxx_auto (type))
12300 return 0;
12301 gcc_unreachable ();
12304 return 0;
12307 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12308 node, return the size in bits for the type if it is a constant, or else
12309 return the alignment for the type if the type's size is not constant, or
12310 else return BITS_PER_WORD if the type actually turns out to be an
12311 ERROR_MARK node. */
12313 static inline unsigned HOST_WIDE_INT
12314 simple_type_size_in_bits (const_tree type)
12316 if (TREE_CODE (type) == ERROR_MARK)
12317 return BITS_PER_WORD;
12318 else if (TYPE_SIZE (type) == NULL_TREE)
12319 return 0;
12320 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12321 return tree_to_uhwi (TYPE_SIZE (type));
12322 else
12323 return TYPE_ALIGN (type);
12326 /* Similarly, but return an offset_int instead of UHWI. */
12328 static inline offset_int
12329 offset_int_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_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12336 return wi::to_offset (TYPE_SIZE (type));
12337 else
12338 return TYPE_ALIGN (type);
12341 /* Given a pointer to a tree node for a subrange type, return a pointer
12342 to a DIE that describes the given type. */
12344 static dw_die_ref
12345 subrange_type_die (tree type, tree low, tree high, tree bias,
12346 dw_die_ref context_die)
12348 dw_die_ref subrange_die;
12349 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12351 if (context_die == NULL)
12352 context_die = comp_unit_die ();
12354 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12356 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12358 /* The size of the subrange type and its base type do not match,
12359 so we need to generate a size attribute for the subrange type. */
12360 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12363 add_alignment_attribute (subrange_die, type);
12365 if (low)
12366 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12367 if (high)
12368 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12369 if (bias && !dwarf_strict)
12370 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12371 dw_scalar_form_constant
12372 | dw_scalar_form_exprloc
12373 | dw_scalar_form_reference,
12374 NULL);
12376 return subrange_die;
12379 /* Returns the (const and/or volatile) cv_qualifiers associated with
12380 the decl node. This will normally be augmented with the
12381 cv_qualifiers of the underlying type in add_type_attribute. */
12383 static int
12384 decl_quals (const_tree decl)
12386 return ((TREE_READONLY (decl)
12387 /* The C++ front-end correctly marks reference-typed
12388 variables as readonly, but from a language (and debug
12389 info) standpoint they are not const-qualified. */
12390 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12391 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12392 | (TREE_THIS_VOLATILE (decl)
12393 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12396 /* Determine the TYPE whose qualifiers match the largest strict subset
12397 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12398 qualifiers outside QUAL_MASK. */
12400 static int
12401 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12403 tree t;
12404 int best_rank = 0, best_qual = 0, max_rank;
12406 type_quals &= qual_mask;
12407 max_rank = popcount_hwi (type_quals) - 1;
12409 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12410 t = TYPE_NEXT_VARIANT (t))
12412 int q = TYPE_QUALS (t) & qual_mask;
12414 if ((q & type_quals) == q && q != type_quals
12415 && check_base_type (t, type))
12417 int rank = popcount_hwi (q);
12419 if (rank > best_rank)
12421 best_rank = rank;
12422 best_qual = q;
12427 return best_qual;
12430 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12431 static const dwarf_qual_info_t dwarf_qual_info[] =
12433 { TYPE_QUAL_CONST, DW_TAG_const_type },
12434 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12435 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12436 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12438 static const unsigned int dwarf_qual_info_size
12439 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12441 /* If DIE is a qualified DIE of some base DIE with the same parent,
12442 return the base DIE, otherwise return NULL. Set MASK to the
12443 qualifiers added compared to the returned DIE. */
12445 static dw_die_ref
12446 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12448 unsigned int i;
12449 for (i = 0; i < dwarf_qual_info_size; i++)
12450 if (die->die_tag == dwarf_qual_info[i].t)
12451 break;
12452 if (i == dwarf_qual_info_size)
12453 return NULL;
12454 if (vec_safe_length (die->die_attr) != 1)
12455 return NULL;
12456 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12457 if (type == NULL || type->die_parent != die->die_parent)
12458 return NULL;
12459 *mask |= dwarf_qual_info[i].q;
12460 if (depth)
12462 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12463 if (ret)
12464 return ret;
12466 return type;
12469 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12470 entry that chains the modifiers specified by CV_QUALS in front of the
12471 given type. REVERSE is true if the type is to be interpreted in the
12472 reverse storage order wrt the target order. */
12474 static dw_die_ref
12475 modified_type_die (tree type, int cv_quals, bool reverse,
12476 dw_die_ref context_die)
12478 enum tree_code code = TREE_CODE (type);
12479 dw_die_ref mod_type_die;
12480 dw_die_ref sub_die = NULL;
12481 tree item_type = NULL;
12482 tree qualified_type;
12483 tree name, low, high;
12484 dw_die_ref mod_scope;
12485 /* Only these cv-qualifiers are currently handled. */
12486 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12487 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12488 ENCODE_QUAL_ADDR_SPACE(~0U));
12489 const bool reverse_base_type
12490 = need_endianity_attribute_p (reverse) && is_base_type (type);
12492 if (code == ERROR_MARK)
12493 return NULL;
12495 if (lang_hooks.types.get_debug_type)
12497 tree debug_type = lang_hooks.types.get_debug_type (type);
12499 if (debug_type != NULL_TREE && debug_type != type)
12500 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12503 cv_quals &= cv_qual_mask;
12505 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12506 tag modifier (and not an attribute) old consumers won't be able
12507 to handle it. */
12508 if (dwarf_version < 3)
12509 cv_quals &= ~TYPE_QUAL_RESTRICT;
12511 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12512 if (dwarf_version < 5)
12513 cv_quals &= ~TYPE_QUAL_ATOMIC;
12515 /* See if we already have the appropriately qualified variant of
12516 this type. */
12517 qualified_type = get_qualified_type (type, cv_quals);
12519 if (qualified_type == sizetype)
12521 /* Try not to expose the internal sizetype type's name. */
12522 if (TYPE_NAME (qualified_type)
12523 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12525 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12527 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12528 && (TYPE_PRECISION (t)
12529 == TYPE_PRECISION (qualified_type))
12530 && (TYPE_UNSIGNED (t)
12531 == TYPE_UNSIGNED (qualified_type)));
12532 qualified_type = t;
12534 else if (qualified_type == sizetype
12535 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12536 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12537 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12538 qualified_type = size_type_node;
12541 /* If we do, then we can just use its DIE, if it exists. */
12542 if (qualified_type)
12544 mod_type_die = lookup_type_die (qualified_type);
12546 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12547 dealt with specially: the DIE with the attribute, if it exists, is
12548 placed immediately after the regular DIE for the same base type. */
12549 if (mod_type_die
12550 && (!reverse_base_type
12551 || ((mod_type_die = mod_type_die->die_sib) != NULL
12552 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12553 return mod_type_die;
12556 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12558 /* Handle C typedef types. */
12559 if (name
12560 && TREE_CODE (name) == TYPE_DECL
12561 && DECL_ORIGINAL_TYPE (name)
12562 && !DECL_ARTIFICIAL (name))
12564 tree dtype = TREE_TYPE (name);
12566 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12567 if (qualified_type == dtype && !reverse_base_type)
12569 tree origin = decl_ultimate_origin (name);
12571 /* Typedef variants that have an abstract origin don't get their own
12572 type DIE (see gen_typedef_die), so fall back on the ultimate
12573 abstract origin instead. */
12574 if (origin != NULL && origin != name)
12575 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12576 context_die);
12578 /* For a named type, use the typedef. */
12579 gen_type_die (qualified_type, context_die);
12580 return lookup_type_die (qualified_type);
12582 else
12584 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12585 dquals &= cv_qual_mask;
12586 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12587 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12588 /* cv-unqualified version of named type. Just use
12589 the unnamed type to which it refers. */
12590 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12591 reverse, context_die);
12592 /* Else cv-qualified version of named type; fall through. */
12596 mod_scope = scope_die_for (type, context_die);
12598 if (cv_quals)
12600 int sub_quals = 0, first_quals = 0;
12601 unsigned i;
12602 dw_die_ref first = NULL, last = NULL;
12604 /* Determine a lesser qualified type that most closely matches
12605 this one. Then generate DW_TAG_* entries for the remaining
12606 qualifiers. */
12607 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12608 cv_qual_mask);
12609 if (sub_quals && use_debug_types)
12611 bool needed = false;
12612 /* If emitting type units, make sure the order of qualifiers
12613 is canonical. Thus, start from unqualified type if
12614 an earlier qualifier is missing in sub_quals, but some later
12615 one is present there. */
12616 for (i = 0; i < dwarf_qual_info_size; i++)
12617 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12618 needed = true;
12619 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12621 sub_quals = 0;
12622 break;
12625 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12626 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12628 /* As not all intermediate qualified DIEs have corresponding
12629 tree types, ensure that qualified DIEs in the same scope
12630 as their DW_AT_type are emitted after their DW_AT_type,
12631 only with other qualified DIEs for the same type possibly
12632 in between them. Determine the range of such qualified
12633 DIEs now (first being the base type, last being corresponding
12634 last qualified DIE for it). */
12635 unsigned int count = 0;
12636 first = qualified_die_p (mod_type_die, &first_quals,
12637 dwarf_qual_info_size);
12638 if (first == NULL)
12639 first = mod_type_die;
12640 gcc_assert ((first_quals & ~sub_quals) == 0);
12641 for (count = 0, last = first;
12642 count < (1U << dwarf_qual_info_size);
12643 count++, last = last->die_sib)
12645 int quals = 0;
12646 if (last == mod_scope->die_child)
12647 break;
12648 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12649 != first)
12650 break;
12654 for (i = 0; i < dwarf_qual_info_size; i++)
12655 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12657 dw_die_ref d;
12658 if (first && first != last)
12660 for (d = first->die_sib; ; d = d->die_sib)
12662 int quals = 0;
12663 qualified_die_p (d, &quals, dwarf_qual_info_size);
12664 if (quals == (first_quals | dwarf_qual_info[i].q))
12665 break;
12666 if (d == last)
12668 d = NULL;
12669 break;
12672 if (d)
12674 mod_type_die = d;
12675 continue;
12678 if (first)
12680 d = new_die_raw (dwarf_qual_info[i].t);
12681 add_child_die_after (mod_scope, d, last);
12682 last = d;
12684 else
12685 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12686 if (mod_type_die)
12687 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12688 mod_type_die = d;
12689 first_quals |= dwarf_qual_info[i].q;
12692 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12694 dwarf_tag tag = DW_TAG_pointer_type;
12695 if (code == REFERENCE_TYPE)
12697 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12698 tag = DW_TAG_rvalue_reference_type;
12699 else
12700 tag = DW_TAG_reference_type;
12702 mod_type_die = new_die (tag, mod_scope, type);
12704 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12705 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12706 add_alignment_attribute (mod_type_die, type);
12707 item_type = TREE_TYPE (type);
12709 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12710 if (!ADDR_SPACE_GENERIC_P (as))
12712 int action = targetm.addr_space.debug (as);
12713 if (action >= 0)
12715 /* Positive values indicate an address_class. */
12716 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12718 else
12720 /* Negative values indicate an (inverted) segment base reg. */
12721 dw_loc_descr_ref d
12722 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12723 add_AT_loc (mod_type_die, DW_AT_segment, d);
12727 else if (code == INTEGER_TYPE
12728 && TREE_TYPE (type) != NULL_TREE
12729 && subrange_type_for_debug_p (type, &low, &high))
12731 tree bias = NULL_TREE;
12732 if (lang_hooks.types.get_type_bias)
12733 bias = lang_hooks.types.get_type_bias (type);
12734 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12735 item_type = TREE_TYPE (type);
12737 else if (is_base_type (type))
12739 mod_type_die = base_type_die (type, reverse);
12741 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12742 if (reverse_base_type)
12744 dw_die_ref after_die
12745 = modified_type_die (type, cv_quals, false, context_die);
12746 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12748 else
12749 add_child_die (comp_unit_die (), mod_type_die);
12751 add_pubtype (type, mod_type_die);
12753 else
12755 gen_type_die (type, context_die);
12757 /* We have to get the type_main_variant here (and pass that to the
12758 `lookup_type_die' routine) because the ..._TYPE node we have
12759 might simply be a *copy* of some original type node (where the
12760 copy was created to help us keep track of typedef names) and
12761 that copy might have a different TYPE_UID from the original
12762 ..._TYPE node. */
12763 if (TREE_CODE (type) == FUNCTION_TYPE
12764 || TREE_CODE (type) == METHOD_TYPE)
12766 /* For function/method types, can't just use type_main_variant here,
12767 because that can have different ref-qualifiers for C++,
12768 but try to canonicalize. */
12769 tree main = TYPE_MAIN_VARIANT (type);
12770 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12771 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12772 && check_base_type (t, main)
12773 && check_lang_type (t, type))
12774 return lookup_type_die (t);
12775 return lookup_type_die (type);
12777 else if (TREE_CODE (type) != VECTOR_TYPE
12778 && TREE_CODE (type) != ARRAY_TYPE)
12779 return lookup_type_die (type_main_variant (type));
12780 else
12781 /* Vectors have the debugging information in the type,
12782 not the main variant. */
12783 return lookup_type_die (type);
12786 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12787 don't output a DW_TAG_typedef, since there isn't one in the
12788 user's program; just attach a DW_AT_name to the type.
12789 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12790 if the base type already has the same name. */
12791 if (name
12792 && ((TREE_CODE (name) != TYPE_DECL
12793 && (qualified_type == TYPE_MAIN_VARIANT (type)
12794 || (cv_quals == TYPE_UNQUALIFIED)))
12795 || (TREE_CODE (name) == TYPE_DECL
12796 && TREE_TYPE (name) == qualified_type
12797 && DECL_NAME (name))))
12799 if (TREE_CODE (name) == TYPE_DECL)
12800 /* Could just call add_name_and_src_coords_attributes here,
12801 but since this is a builtin type it doesn't have any
12802 useful source coordinates anyway. */
12803 name = DECL_NAME (name);
12804 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12806 /* This probably indicates a bug. */
12807 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12809 name = TYPE_IDENTIFIER (type);
12810 add_name_attribute (mod_type_die,
12811 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12814 if (qualified_type && !reverse_base_type)
12815 equate_type_number_to_die (qualified_type, mod_type_die);
12817 if (item_type)
12818 /* We must do this after the equate_type_number_to_die call, in case
12819 this is a recursive type. This ensures that the modified_type_die
12820 recursion will terminate even if the type is recursive. Recursive
12821 types are possible in Ada. */
12822 sub_die = modified_type_die (item_type,
12823 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12824 reverse,
12825 context_die);
12827 if (sub_die != NULL)
12828 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12830 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12831 if (TYPE_ARTIFICIAL (type))
12832 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12834 return mod_type_die;
12837 /* Generate DIEs for the generic parameters of T.
12838 T must be either a generic type or a generic function.
12839 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12841 static void
12842 gen_generic_params_dies (tree t)
12844 tree parms, args;
12845 int parms_num, i;
12846 dw_die_ref die = NULL;
12847 int non_default;
12849 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12850 return;
12852 if (TYPE_P (t))
12853 die = lookup_type_die (t);
12854 else if (DECL_P (t))
12855 die = lookup_decl_die (t);
12857 gcc_assert (die);
12859 parms = lang_hooks.get_innermost_generic_parms (t);
12860 if (!parms)
12861 /* T has no generic parameter. It means T is neither a generic type
12862 or function. End of story. */
12863 return;
12865 parms_num = TREE_VEC_LENGTH (parms);
12866 args = lang_hooks.get_innermost_generic_args (t);
12867 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12868 non_default = int_cst_value (TREE_CHAIN (args));
12869 else
12870 non_default = TREE_VEC_LENGTH (args);
12871 for (i = 0; i < parms_num; i++)
12873 tree parm, arg, arg_pack_elems;
12874 dw_die_ref parm_die;
12876 parm = TREE_VEC_ELT (parms, i);
12877 arg = TREE_VEC_ELT (args, i);
12878 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12879 gcc_assert (parm && TREE_VALUE (parm) && arg);
12881 if (parm && TREE_VALUE (parm) && arg)
12883 /* If PARM represents a template parameter pack,
12884 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12885 by DW_TAG_template_*_parameter DIEs for the argument
12886 pack elements of ARG. Note that ARG would then be
12887 an argument pack. */
12888 if (arg_pack_elems)
12889 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12890 arg_pack_elems,
12891 die);
12892 else
12893 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12894 true /* emit name */, die);
12895 if (i >= non_default)
12896 add_AT_flag (parm_die, DW_AT_default_value, 1);
12901 /* Create and return a DIE for PARM which should be
12902 the representation of a generic type parameter.
12903 For instance, in the C++ front end, PARM would be a template parameter.
12904 ARG is the argument to PARM.
12905 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12906 name of the PARM.
12907 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12908 as a child node. */
12910 static dw_die_ref
12911 generic_parameter_die (tree parm, tree arg,
12912 bool emit_name_p,
12913 dw_die_ref parent_die)
12915 dw_die_ref tmpl_die = NULL;
12916 const char *name = NULL;
12918 if (!parm || !DECL_NAME (parm) || !arg)
12919 return NULL;
12921 /* We support non-type generic parameters and arguments,
12922 type generic parameters and arguments, as well as
12923 generic generic parameters (a.k.a. template template parameters in C++)
12924 and arguments. */
12925 if (TREE_CODE (parm) == PARM_DECL)
12926 /* PARM is a nontype generic parameter */
12927 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12928 else if (TREE_CODE (parm) == TYPE_DECL)
12929 /* PARM is a type generic parameter. */
12930 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12931 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12932 /* PARM is a generic generic parameter.
12933 Its DIE is a GNU extension. It shall have a
12934 DW_AT_name attribute to represent the name of the template template
12935 parameter, and a DW_AT_GNU_template_name attribute to represent the
12936 name of the template template argument. */
12937 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12938 parent_die, parm);
12939 else
12940 gcc_unreachable ();
12942 if (tmpl_die)
12944 tree tmpl_type;
12946 /* If PARM is a generic parameter pack, it means we are
12947 emitting debug info for a template argument pack element.
12948 In other terms, ARG is a template argument pack element.
12949 In that case, we don't emit any DW_AT_name attribute for
12950 the die. */
12951 if (emit_name_p)
12953 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12954 gcc_assert (name);
12955 add_AT_string (tmpl_die, DW_AT_name, name);
12958 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12960 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12961 TMPL_DIE should have a child DW_AT_type attribute that is set
12962 to the type of the argument to PARM, which is ARG.
12963 If PARM is a type generic parameter, TMPL_DIE should have a
12964 child DW_AT_type that is set to ARG. */
12965 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12966 add_type_attribute (tmpl_die, tmpl_type,
12967 (TREE_THIS_VOLATILE (tmpl_type)
12968 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12969 false, parent_die);
12971 else
12973 /* So TMPL_DIE is a DIE representing a
12974 a generic generic template parameter, a.k.a template template
12975 parameter in C++ and arg is a template. */
12977 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12978 to the name of the argument. */
12979 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12980 if (name)
12981 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12984 if (TREE_CODE (parm) == PARM_DECL)
12985 /* So PARM is a non-type generic parameter.
12986 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12987 attribute of TMPL_DIE which value represents the value
12988 of ARG.
12989 We must be careful here:
12990 The value of ARG might reference some function decls.
12991 We might currently be emitting debug info for a generic
12992 type and types are emitted before function decls, we don't
12993 know if the function decls referenced by ARG will actually be
12994 emitted after cgraph computations.
12995 So must defer the generation of the DW_AT_const_value to
12996 after cgraph is ready. */
12997 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13000 return tmpl_die;
13003 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13004 PARM_PACK must be a template parameter pack. The returned DIE
13005 will be child DIE of PARENT_DIE. */
13007 static dw_die_ref
13008 template_parameter_pack_die (tree parm_pack,
13009 tree parm_pack_args,
13010 dw_die_ref parent_die)
13012 dw_die_ref die;
13013 int j;
13015 gcc_assert (parent_die && parm_pack);
13017 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13018 add_name_and_src_coords_attributes (die, parm_pack);
13019 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13020 generic_parameter_die (parm_pack,
13021 TREE_VEC_ELT (parm_pack_args, j),
13022 false /* Don't emit DW_AT_name */,
13023 die);
13024 return die;
13027 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13028 an enumerated type. */
13030 static inline int
13031 type_is_enum (const_tree type)
13033 return TREE_CODE (type) == ENUMERAL_TYPE;
13036 /* Return the DBX register number described by a given RTL node. */
13038 static unsigned int
13039 dbx_reg_number (const_rtx rtl)
13041 unsigned regno = REGNO (rtl);
13043 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13045 #ifdef LEAF_REG_REMAP
13046 if (crtl->uses_only_leaf_regs)
13048 int leaf_reg = LEAF_REG_REMAP (regno);
13049 if (leaf_reg != -1)
13050 regno = (unsigned) leaf_reg;
13052 #endif
13054 regno = DBX_REGISTER_NUMBER (regno);
13055 gcc_assert (regno != INVALID_REGNUM);
13056 return regno;
13059 /* Optionally add a DW_OP_piece term to a location description expression.
13060 DW_OP_piece is only added if the location description expression already
13061 doesn't end with DW_OP_piece. */
13063 static void
13064 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13066 dw_loc_descr_ref loc;
13068 if (*list_head != NULL)
13070 /* Find the end of the chain. */
13071 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13074 if (loc->dw_loc_opc != DW_OP_piece)
13075 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13079 /* Return a location descriptor that designates a machine register or
13080 zero if there is none. */
13082 static dw_loc_descr_ref
13083 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13085 rtx regs;
13087 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13088 return 0;
13090 /* We only use "frame base" when we're sure we're talking about the
13091 post-prologue local stack frame. We do this by *not* running
13092 register elimination until this point, and recognizing the special
13093 argument pointer and soft frame pointer rtx's.
13094 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13095 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13096 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13098 dw_loc_descr_ref result = NULL;
13100 if (dwarf_version >= 4 || !dwarf_strict)
13102 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13103 initialized);
13104 if (result)
13105 add_loc_descr (&result,
13106 new_loc_descr (DW_OP_stack_value, 0, 0));
13108 return result;
13111 regs = targetm.dwarf_register_span (rtl);
13113 if (REG_NREGS (rtl) > 1 || regs)
13114 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13115 else
13117 unsigned int dbx_regnum = dbx_reg_number (rtl);
13118 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13119 return 0;
13120 return one_reg_loc_descriptor (dbx_regnum, initialized);
13124 /* Return a location descriptor that designates a machine register for
13125 a given hard register number. */
13127 static dw_loc_descr_ref
13128 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13130 dw_loc_descr_ref reg_loc_descr;
13132 if (regno <= 31)
13133 reg_loc_descr
13134 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13135 else
13136 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13138 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13139 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13141 return reg_loc_descr;
13144 /* Given an RTL of a register, return a location descriptor that
13145 designates a value that spans more than one register. */
13147 static dw_loc_descr_ref
13148 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13149 enum var_init_status initialized)
13151 int size, i;
13152 dw_loc_descr_ref loc_result = NULL;
13154 /* Simple, contiguous registers. */
13155 if (regs == NULL_RTX)
13157 unsigned reg = REGNO (rtl);
13158 int nregs;
13160 #ifdef LEAF_REG_REMAP
13161 if (crtl->uses_only_leaf_regs)
13163 int leaf_reg = LEAF_REG_REMAP (reg);
13164 if (leaf_reg != -1)
13165 reg = (unsigned) leaf_reg;
13167 #endif
13169 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13170 nregs = REG_NREGS (rtl);
13172 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13174 loc_result = NULL;
13175 while (nregs--)
13177 dw_loc_descr_ref t;
13179 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13180 VAR_INIT_STATUS_INITIALIZED);
13181 add_loc_descr (&loc_result, t);
13182 add_loc_descr_op_piece (&loc_result, size);
13183 ++reg;
13185 return loc_result;
13188 /* Now onto stupid register sets in non contiguous locations. */
13190 gcc_assert (GET_CODE (regs) == PARALLEL);
13192 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13193 loc_result = NULL;
13195 for (i = 0; i < XVECLEN (regs, 0); ++i)
13197 dw_loc_descr_ref t;
13199 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13200 VAR_INIT_STATUS_INITIALIZED);
13201 add_loc_descr (&loc_result, t);
13202 add_loc_descr_op_piece (&loc_result, size);
13205 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13206 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13207 return loc_result;
13210 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13212 /* Return a location descriptor that designates a constant i,
13213 as a compound operation from constant (i >> shift), constant shift
13214 and DW_OP_shl. */
13216 static dw_loc_descr_ref
13217 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13219 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13220 add_loc_descr (&ret, int_loc_descriptor (shift));
13221 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13222 return ret;
13225 /* Return a location descriptor that designates a constant. */
13227 static dw_loc_descr_ref
13228 int_loc_descriptor (HOST_WIDE_INT i)
13230 enum dwarf_location_atom op;
13232 /* Pick the smallest representation of a constant, rather than just
13233 defaulting to the LEB encoding. */
13234 if (i >= 0)
13236 int clz = clz_hwi (i);
13237 int ctz = ctz_hwi (i);
13238 if (i <= 31)
13239 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13240 else if (i <= 0xff)
13241 op = DW_OP_const1u;
13242 else if (i <= 0xffff)
13243 op = DW_OP_const2u;
13244 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13245 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13246 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13247 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13248 while DW_OP_const4u is 5 bytes. */
13249 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13250 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13251 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13252 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13253 while DW_OP_const4u is 5 bytes. */
13254 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13256 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13257 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13258 <= 4)
13260 /* As i >= 2**31, the double cast above will yield a negative number.
13261 Since wrapping is defined in DWARF expressions we can output big
13262 positive integers as small negative ones, regardless of the size
13263 of host wide ints.
13265 Here, since the evaluator will handle 32-bit values and since i >=
13266 2**31, we know it's going to be interpreted as a negative literal:
13267 store it this way if we can do better than 5 bytes this way. */
13268 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13270 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13271 op = DW_OP_const4u;
13273 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13274 least 6 bytes: see if we can do better before falling back to it. */
13275 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13276 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13277 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13278 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13279 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13280 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13281 >= HOST_BITS_PER_WIDE_INT)
13282 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13283 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13284 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13285 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13286 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13287 && size_of_uleb128 (i) > 6)
13288 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13289 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13290 else
13291 op = DW_OP_constu;
13293 else
13295 if (i >= -0x80)
13296 op = DW_OP_const1s;
13297 else if (i >= -0x8000)
13298 op = DW_OP_const2s;
13299 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13301 if (size_of_int_loc_descriptor (i) < 5)
13303 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13304 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13305 return ret;
13307 op = DW_OP_const4s;
13309 else
13311 if (size_of_int_loc_descriptor (i)
13312 < (unsigned long) 1 + size_of_sleb128 (i))
13314 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13315 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13316 return ret;
13318 op = DW_OP_consts;
13322 return new_loc_descr (op, i, 0);
13325 /* Likewise, for unsigned constants. */
13327 static dw_loc_descr_ref
13328 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13330 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13331 const unsigned HOST_WIDE_INT max_uint
13332 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13334 /* If possible, use the clever signed constants handling. */
13335 if (i <= max_int)
13336 return int_loc_descriptor ((HOST_WIDE_INT) i);
13338 /* Here, we are left with positive numbers that cannot be represented as
13339 HOST_WIDE_INT, i.e.:
13340 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13342 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13343 whereas may be better to output a negative integer: thanks to integer
13344 wrapping, we know that:
13345 x = x - 2 ** DWARF2_ADDR_SIZE
13346 = x - 2 * (max (HOST_WIDE_INT) + 1)
13347 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13348 small negative integers. Let's try that in cases it will clearly improve
13349 the encoding: there is no gain turning DW_OP_const4u into
13350 DW_OP_const4s. */
13351 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13352 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13353 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13355 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13357 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13358 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13359 const HOST_WIDE_INT second_shift
13360 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13362 /* So we finally have:
13363 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13364 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13365 return int_loc_descriptor (second_shift);
13368 /* Last chance: fallback to a simple constant operation. */
13369 return new_loc_descr
13370 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13371 ? DW_OP_const4u
13372 : DW_OP_const8u,
13373 i, 0);
13376 /* Generate and return a location description that computes the unsigned
13377 comparison of the two stack top entries (a OP b where b is the top-most
13378 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13379 LE_EXPR, GT_EXPR or GE_EXPR. */
13381 static dw_loc_descr_ref
13382 uint_comparison_loc_list (enum tree_code kind)
13384 enum dwarf_location_atom op, flip_op;
13385 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13387 switch (kind)
13389 case LT_EXPR:
13390 op = DW_OP_lt;
13391 break;
13392 case LE_EXPR:
13393 op = DW_OP_le;
13394 break;
13395 case GT_EXPR:
13396 op = DW_OP_gt;
13397 break;
13398 case GE_EXPR:
13399 op = DW_OP_ge;
13400 break;
13401 default:
13402 gcc_unreachable ();
13405 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13406 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13408 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13409 possible to perform unsigned comparisons: we just have to distinguish
13410 three cases:
13412 1. when a and b have the same sign (as signed integers); then we should
13413 return: a OP(signed) b;
13415 2. when a is a negative signed integer while b is a positive one, then a
13416 is a greater unsigned integer than b; likewise when a and b's roles
13417 are flipped.
13419 So first, compare the sign of the two operands. */
13420 ret = new_loc_descr (DW_OP_over, 0, 0);
13421 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13422 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13423 /* If they have different signs (i.e. they have different sign bits), then
13424 the stack top value has now the sign bit set and thus it's smaller than
13425 zero. */
13426 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13427 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13428 add_loc_descr (&ret, bra_node);
13430 /* We are in case 1. At this point, we know both operands have the same
13431 sign, to it's safe to use the built-in signed comparison. */
13432 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13433 add_loc_descr (&ret, jmp_node);
13435 /* We are in case 2. Here, we know both operands do not have the same sign,
13436 so we have to flip the signed comparison. */
13437 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13438 tmp = new_loc_descr (flip_op, 0, 0);
13439 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13440 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13441 add_loc_descr (&ret, tmp);
13443 /* This dummy operation is necessary to make the two branches join. */
13444 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13445 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13446 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13447 add_loc_descr (&ret, tmp);
13449 return ret;
13452 /* Likewise, but takes the location description lists (might be destructive on
13453 them). Return NULL if either is NULL or if concatenation fails. */
13455 static dw_loc_list_ref
13456 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13457 enum tree_code kind)
13459 if (left == NULL || right == NULL)
13460 return NULL;
13462 add_loc_list (&left, right);
13463 if (left == NULL)
13464 return NULL;
13466 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13467 return left;
13470 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13471 without actually allocating it. */
13473 static unsigned long
13474 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13476 return size_of_int_loc_descriptor (i >> shift)
13477 + size_of_int_loc_descriptor (shift)
13478 + 1;
13481 /* Return size_of_locs (int_loc_descriptor (i)) without
13482 actually allocating it. */
13484 static unsigned long
13485 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13487 unsigned long s;
13489 if (i >= 0)
13491 int clz, ctz;
13492 if (i <= 31)
13493 return 1;
13494 else if (i <= 0xff)
13495 return 2;
13496 else if (i <= 0xffff)
13497 return 3;
13498 clz = clz_hwi (i);
13499 ctz = ctz_hwi (i);
13500 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13501 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13502 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13503 - clz - 5);
13504 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13505 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13506 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13507 - clz - 8);
13508 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13509 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13510 <= 4)
13511 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13512 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13513 return 5;
13514 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13515 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13516 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13517 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13518 - clz - 8);
13519 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13520 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13521 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13522 - clz - 16);
13523 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13524 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13525 && s > 6)
13526 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13527 - clz - 32);
13528 else
13529 return 1 + s;
13531 else
13533 if (i >= -0x80)
13534 return 2;
13535 else if (i >= -0x8000)
13536 return 3;
13537 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13539 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13541 s = size_of_int_loc_descriptor (-i) + 1;
13542 if (s < 5)
13543 return s;
13545 return 5;
13547 else
13549 unsigned long r = 1 + size_of_sleb128 (i);
13550 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13552 s = size_of_int_loc_descriptor (-i) + 1;
13553 if (s < r)
13554 return s;
13556 return r;
13561 /* Return loc description representing "address" of integer value.
13562 This can appear only as toplevel expression. */
13564 static dw_loc_descr_ref
13565 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13567 int litsize;
13568 dw_loc_descr_ref loc_result = NULL;
13570 if (!(dwarf_version >= 4 || !dwarf_strict))
13571 return NULL;
13573 litsize = size_of_int_loc_descriptor (i);
13574 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13575 is more compact. For DW_OP_stack_value we need:
13576 litsize + 1 (DW_OP_stack_value)
13577 and for DW_OP_implicit_value:
13578 1 (DW_OP_implicit_value) + 1 (length) + size. */
13579 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13581 loc_result = int_loc_descriptor (i);
13582 add_loc_descr (&loc_result,
13583 new_loc_descr (DW_OP_stack_value, 0, 0));
13584 return loc_result;
13587 loc_result = new_loc_descr (DW_OP_implicit_value,
13588 size, 0);
13589 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13590 loc_result->dw_loc_oprnd2.v.val_int = i;
13591 return loc_result;
13594 /* Return a location descriptor that designates a base+offset location. */
13596 static dw_loc_descr_ref
13597 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13598 enum var_init_status initialized)
13600 unsigned int regno;
13601 dw_loc_descr_ref result;
13602 dw_fde_ref fde = cfun->fde;
13604 /* We only use "frame base" when we're sure we're talking about the
13605 post-prologue local stack frame. We do this by *not* running
13606 register elimination until this point, and recognizing the special
13607 argument pointer and soft frame pointer rtx's. */
13608 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13610 rtx elim = (ira_use_lra_p
13611 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13612 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13614 if (elim != reg)
13616 if (GET_CODE (elim) == PLUS)
13618 offset += INTVAL (XEXP (elim, 1));
13619 elim = XEXP (elim, 0);
13621 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13622 && (elim == hard_frame_pointer_rtx
13623 || elim == stack_pointer_rtx))
13624 || elim == (frame_pointer_needed
13625 ? hard_frame_pointer_rtx
13626 : stack_pointer_rtx));
13628 /* If drap register is used to align stack, use frame
13629 pointer + offset to access stack variables. If stack
13630 is aligned without drap, use stack pointer + offset to
13631 access stack variables. */
13632 if (crtl->stack_realign_tried
13633 && reg == frame_pointer_rtx)
13635 int base_reg
13636 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13637 ? HARD_FRAME_POINTER_REGNUM
13638 : REGNO (elim));
13639 return new_reg_loc_descr (base_reg, offset);
13642 gcc_assert (frame_pointer_fb_offset_valid);
13643 offset += frame_pointer_fb_offset;
13644 return new_loc_descr (DW_OP_fbreg, offset, 0);
13648 regno = REGNO (reg);
13649 #ifdef LEAF_REG_REMAP
13650 if (crtl->uses_only_leaf_regs)
13652 int leaf_reg = LEAF_REG_REMAP (regno);
13653 if (leaf_reg != -1)
13654 regno = (unsigned) leaf_reg;
13656 #endif
13657 regno = DWARF_FRAME_REGNUM (regno);
13659 if (!optimize && fde
13660 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13662 /* Use cfa+offset to represent the location of arguments passed
13663 on the stack when drap is used to align stack.
13664 Only do this when not optimizing, for optimized code var-tracking
13665 is supposed to track where the arguments live and the register
13666 used as vdrap or drap in some spot might be used for something
13667 else in other part of the routine. */
13668 return new_loc_descr (DW_OP_fbreg, offset, 0);
13671 if (regno <= 31)
13672 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13673 offset, 0);
13674 else
13675 result = new_loc_descr (DW_OP_bregx, regno, offset);
13677 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13678 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13680 return result;
13683 /* Return true if this RTL expression describes a base+offset calculation. */
13685 static inline int
13686 is_based_loc (const_rtx rtl)
13688 return (GET_CODE (rtl) == PLUS
13689 && ((REG_P (XEXP (rtl, 0))
13690 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13691 && CONST_INT_P (XEXP (rtl, 1)))));
13694 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13695 failed. */
13697 static dw_loc_descr_ref
13698 tls_mem_loc_descriptor (rtx mem)
13700 tree base;
13701 dw_loc_descr_ref loc_result;
13703 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13704 return NULL;
13706 base = get_base_address (MEM_EXPR (mem));
13707 if (base == NULL
13708 || !VAR_P (base)
13709 || !DECL_THREAD_LOCAL_P (base))
13710 return NULL;
13712 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13713 if (loc_result == NULL)
13714 return NULL;
13716 if (MEM_OFFSET (mem))
13717 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13719 return loc_result;
13722 /* Output debug info about reason why we failed to expand expression as dwarf
13723 expression. */
13725 static void
13726 expansion_failed (tree expr, rtx rtl, char const *reason)
13728 if (dump_file && (dump_flags & TDF_DETAILS))
13730 fprintf (dump_file, "Failed to expand as dwarf: ");
13731 if (expr)
13732 print_generic_expr (dump_file, expr, dump_flags);
13733 if (rtl)
13735 fprintf (dump_file, "\n");
13736 print_rtl (dump_file, rtl);
13738 fprintf (dump_file, "\nReason: %s\n", reason);
13742 /* Helper function for const_ok_for_output. */
13744 static bool
13745 const_ok_for_output_1 (rtx rtl)
13747 if (targetm.const_not_ok_for_debug_p (rtl))
13749 if (GET_CODE (rtl) != UNSPEC)
13751 expansion_failed (NULL_TREE, rtl,
13752 "Expression rejected for debug by the backend.\n");
13753 return false;
13756 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13757 the target hook doesn't explicitly allow it in debug info, assume
13758 we can't express it in the debug info. */
13759 /* Don't complain about TLS UNSPECs, those are just too hard to
13760 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13761 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13762 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13763 if (flag_checking
13764 && (XVECLEN (rtl, 0) == 0
13765 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13766 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13767 inform (current_function_decl
13768 ? DECL_SOURCE_LOCATION (current_function_decl)
13769 : UNKNOWN_LOCATION,
13770 #if NUM_UNSPEC_VALUES > 0
13771 "non-delegitimized UNSPEC %s (%d) found in variable location",
13772 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13773 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13774 XINT (rtl, 1));
13775 #else
13776 "non-delegitimized UNSPEC %d found in variable location",
13777 XINT (rtl, 1));
13778 #endif
13779 expansion_failed (NULL_TREE, rtl,
13780 "UNSPEC hasn't been delegitimized.\n");
13781 return false;
13784 /* FIXME: Refer to PR60655. It is possible for simplification
13785 of rtl expressions in var tracking to produce such expressions.
13786 We should really identify / validate expressions
13787 enclosed in CONST that can be handled by assemblers on various
13788 targets and only handle legitimate cases here. */
13789 switch (GET_CODE (rtl))
13791 case SYMBOL_REF:
13792 break;
13793 case NOT:
13794 case NEG:
13795 return false;
13796 default:
13797 return true;
13800 if (CONSTANT_POOL_ADDRESS_P (rtl))
13802 bool marked;
13803 get_pool_constant_mark (rtl, &marked);
13804 /* If all references to this pool constant were optimized away,
13805 it was not output and thus we can't represent it. */
13806 if (!marked)
13808 expansion_failed (NULL_TREE, rtl,
13809 "Constant was removed from constant pool.\n");
13810 return false;
13814 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13815 return false;
13817 /* Avoid references to external symbols in debug info, on several targets
13818 the linker might even refuse to link when linking a shared library,
13819 and in many other cases the relocations for .debug_info/.debug_loc are
13820 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13821 to be defined within the same shared library or executable are fine. */
13822 if (SYMBOL_REF_EXTERNAL_P (rtl))
13824 tree decl = SYMBOL_REF_DECL (rtl);
13826 if (decl == NULL || !targetm.binds_local_p (decl))
13828 expansion_failed (NULL_TREE, rtl,
13829 "Symbol not defined in current TU.\n");
13830 return false;
13834 return true;
13837 /* Return true if constant RTL can be emitted in DW_OP_addr or
13838 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13839 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13841 static bool
13842 const_ok_for_output (rtx rtl)
13844 if (GET_CODE (rtl) == SYMBOL_REF)
13845 return const_ok_for_output_1 (rtl);
13847 if (GET_CODE (rtl) == CONST)
13849 subrtx_var_iterator::array_type array;
13850 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13851 if (!const_ok_for_output_1 (*iter))
13852 return false;
13853 return true;
13856 return true;
13859 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13860 if possible, NULL otherwise. */
13862 static dw_die_ref
13863 base_type_for_mode (machine_mode mode, bool unsignedp)
13865 dw_die_ref type_die;
13866 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13868 if (type == NULL)
13869 return NULL;
13870 switch (TREE_CODE (type))
13872 case INTEGER_TYPE:
13873 case REAL_TYPE:
13874 break;
13875 default:
13876 return NULL;
13878 type_die = lookup_type_die (type);
13879 if (!type_die)
13880 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13881 comp_unit_die ());
13882 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13883 return NULL;
13884 return type_die;
13887 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13888 type matching MODE, or, if MODE is narrower than or as wide as
13889 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13890 possible. */
13892 static dw_loc_descr_ref
13893 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13895 machine_mode outer_mode = mode;
13896 dw_die_ref type_die;
13897 dw_loc_descr_ref cvt;
13899 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13901 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13902 return op;
13904 type_die = base_type_for_mode (outer_mode, 1);
13905 if (type_die == NULL)
13906 return NULL;
13907 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13908 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13909 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13910 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13911 add_loc_descr (&op, cvt);
13912 return op;
13915 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13917 static dw_loc_descr_ref
13918 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13919 dw_loc_descr_ref op1)
13921 dw_loc_descr_ref ret = op0;
13922 add_loc_descr (&ret, op1);
13923 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13924 if (STORE_FLAG_VALUE != 1)
13926 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13927 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13929 return ret;
13932 /* Subroutine of scompare_loc_descriptor for the case in which we're
13933 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13934 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13936 static dw_loc_descr_ref
13937 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13938 scalar_int_mode op_mode,
13939 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13941 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13942 dw_loc_descr_ref cvt;
13944 if (type_die == NULL)
13945 return NULL;
13946 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13947 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13948 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13949 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13950 add_loc_descr (&op0, cvt);
13951 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13952 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13953 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13954 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13955 add_loc_descr (&op1, cvt);
13956 return compare_loc_descriptor (op, op0, op1);
13959 /* Subroutine of scompare_loc_descriptor for the case in which we're
13960 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13961 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13963 static dw_loc_descr_ref
13964 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13965 scalar_int_mode op_mode,
13966 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13968 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13969 /* For eq/ne, if the operands are known to be zero-extended,
13970 there is no need to do the fancy shifting up. */
13971 if (op == DW_OP_eq || op == DW_OP_ne)
13973 dw_loc_descr_ref last0, last1;
13974 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13976 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13978 /* deref_size zero extends, and for constants we can check
13979 whether they are zero extended or not. */
13980 if (((last0->dw_loc_opc == DW_OP_deref_size
13981 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13982 || (CONST_INT_P (XEXP (rtl, 0))
13983 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13984 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13985 && ((last1->dw_loc_opc == DW_OP_deref_size
13986 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13987 || (CONST_INT_P (XEXP (rtl, 1))
13988 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13989 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13990 return compare_loc_descriptor (op, op0, op1);
13992 /* EQ/NE comparison against constant in narrower type than
13993 DWARF2_ADDR_SIZE can be performed either as
13994 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13995 DW_OP_{eq,ne}
13997 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13998 DW_OP_{eq,ne}. Pick whatever is shorter. */
13999 if (CONST_INT_P (XEXP (rtl, 1))
14000 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14001 && (size_of_int_loc_descriptor (shift) + 1
14002 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14003 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14004 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14005 & GET_MODE_MASK (op_mode))))
14007 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14008 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14009 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14010 & GET_MODE_MASK (op_mode));
14011 return compare_loc_descriptor (op, op0, op1);
14014 add_loc_descr (&op0, int_loc_descriptor (shift));
14015 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14016 if (CONST_INT_P (XEXP (rtl, 1)))
14017 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14018 else
14020 add_loc_descr (&op1, int_loc_descriptor (shift));
14021 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14023 return compare_loc_descriptor (op, op0, op1);
14026 /* Return location descriptor for unsigned comparison OP RTL. */
14028 static dw_loc_descr_ref
14029 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14030 machine_mode mem_mode)
14032 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14033 dw_loc_descr_ref op0, op1;
14035 if (op_mode == VOIDmode)
14036 op_mode = GET_MODE (XEXP (rtl, 1));
14037 if (op_mode == VOIDmode)
14038 return NULL;
14040 scalar_int_mode int_op_mode;
14041 if (dwarf_strict
14042 && dwarf_version < 5
14043 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14044 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14045 return NULL;
14047 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14048 VAR_INIT_STATUS_INITIALIZED);
14049 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14050 VAR_INIT_STATUS_INITIALIZED);
14052 if (op0 == NULL || op1 == NULL)
14053 return NULL;
14055 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14057 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14058 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14060 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14061 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14063 return compare_loc_descriptor (op, op0, op1);
14066 /* Return location descriptor for unsigned comparison OP RTL. */
14068 static dw_loc_descr_ref
14069 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14070 machine_mode mem_mode)
14072 dw_loc_descr_ref op0, op1;
14074 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14075 if (test_op_mode == VOIDmode)
14076 test_op_mode = GET_MODE (XEXP (rtl, 1));
14078 scalar_int_mode op_mode;
14079 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14080 return NULL;
14082 if (dwarf_strict
14083 && dwarf_version < 5
14084 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14085 return NULL;
14087 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14088 VAR_INIT_STATUS_INITIALIZED);
14089 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14090 VAR_INIT_STATUS_INITIALIZED);
14092 if (op0 == NULL || op1 == NULL)
14093 return NULL;
14095 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14097 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14098 dw_loc_descr_ref last0, last1;
14099 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14101 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14103 if (CONST_INT_P (XEXP (rtl, 0)))
14104 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14105 /* deref_size zero extends, so no need to mask it again. */
14106 else if (last0->dw_loc_opc != DW_OP_deref_size
14107 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14109 add_loc_descr (&op0, int_loc_descriptor (mask));
14110 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14112 if (CONST_INT_P (XEXP (rtl, 1)))
14113 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14114 /* deref_size zero extends, so no need to mask it again. */
14115 else if (last1->dw_loc_opc != DW_OP_deref_size
14116 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14118 add_loc_descr (&op1, int_loc_descriptor (mask));
14119 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14122 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14124 HOST_WIDE_INT bias = 1;
14125 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14126 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14127 if (CONST_INT_P (XEXP (rtl, 1)))
14128 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14129 + INTVAL (XEXP (rtl, 1)));
14130 else
14131 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14132 bias, 0));
14134 return compare_loc_descriptor (op, op0, op1);
14137 /* Return location descriptor for {U,S}{MIN,MAX}. */
14139 static dw_loc_descr_ref
14140 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14141 machine_mode mem_mode)
14143 enum dwarf_location_atom op;
14144 dw_loc_descr_ref op0, op1, ret;
14145 dw_loc_descr_ref bra_node, drop_node;
14147 scalar_int_mode int_mode;
14148 if (dwarf_strict
14149 && dwarf_version < 5
14150 && (!is_a <scalar_int_mode> (mode, &int_mode)
14151 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14152 return NULL;
14154 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14155 VAR_INIT_STATUS_INITIALIZED);
14156 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14157 VAR_INIT_STATUS_INITIALIZED);
14159 if (op0 == NULL || op1 == NULL)
14160 return NULL;
14162 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14163 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14164 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14165 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14167 /* Checked by the caller. */
14168 int_mode = as_a <scalar_int_mode> (mode);
14169 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14171 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14172 add_loc_descr (&op0, int_loc_descriptor (mask));
14173 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14174 add_loc_descr (&op1, int_loc_descriptor (mask));
14175 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14177 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14179 HOST_WIDE_INT bias = 1;
14180 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14181 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14182 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14185 else if (is_a <scalar_int_mode> (mode, &int_mode)
14186 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14188 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14189 add_loc_descr (&op0, int_loc_descriptor (shift));
14190 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14191 add_loc_descr (&op1, int_loc_descriptor (shift));
14192 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14194 else if (is_a <scalar_int_mode> (mode, &int_mode)
14195 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14197 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14198 dw_loc_descr_ref cvt;
14199 if (type_die == NULL)
14200 return NULL;
14201 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14202 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14203 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14204 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14205 add_loc_descr (&op0, cvt);
14206 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14207 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14208 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14209 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14210 add_loc_descr (&op1, cvt);
14213 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14214 op = DW_OP_lt;
14215 else
14216 op = DW_OP_gt;
14217 ret = op0;
14218 add_loc_descr (&ret, op1);
14219 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14220 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14221 add_loc_descr (&ret, bra_node);
14222 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14223 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14224 add_loc_descr (&ret, drop_node);
14225 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14226 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14227 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14228 && is_a <scalar_int_mode> (mode, &int_mode)
14229 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14230 ret = convert_descriptor_to_mode (int_mode, ret);
14231 return ret;
14234 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14235 but after converting arguments to type_die, afterwards
14236 convert back to unsigned. */
14238 static dw_loc_descr_ref
14239 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14240 scalar_int_mode mode, machine_mode mem_mode)
14242 dw_loc_descr_ref cvt, op0, op1;
14244 if (type_die == NULL)
14245 return NULL;
14246 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14247 VAR_INIT_STATUS_INITIALIZED);
14248 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14249 VAR_INIT_STATUS_INITIALIZED);
14250 if (op0 == NULL || op1 == NULL)
14251 return NULL;
14252 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14253 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14254 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14255 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14256 add_loc_descr (&op0, cvt);
14257 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14258 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14259 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14260 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14261 add_loc_descr (&op1, cvt);
14262 add_loc_descr (&op0, op1);
14263 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14264 return convert_descriptor_to_mode (mode, op0);
14267 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14268 const0 is DW_OP_lit0 or corresponding typed constant,
14269 const1 is DW_OP_lit1 or corresponding typed constant
14270 and constMSB is constant with just the MSB bit set
14271 for the mode):
14272 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14273 L1: const0 DW_OP_swap
14274 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14275 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14276 L3: DW_OP_drop
14277 L4: DW_OP_nop
14279 CTZ is similar:
14280 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14281 L1: const0 DW_OP_swap
14282 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14283 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14284 L3: DW_OP_drop
14285 L4: DW_OP_nop
14287 FFS is similar:
14288 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14289 L1: const1 DW_OP_swap
14290 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14291 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14292 L3: DW_OP_drop
14293 L4: DW_OP_nop */
14295 static dw_loc_descr_ref
14296 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14297 machine_mode mem_mode)
14299 dw_loc_descr_ref op0, ret, tmp;
14300 HOST_WIDE_INT valv;
14301 dw_loc_descr_ref l1jump, l1label;
14302 dw_loc_descr_ref l2jump, l2label;
14303 dw_loc_descr_ref l3jump, l3label;
14304 dw_loc_descr_ref l4jump, l4label;
14305 rtx msb;
14307 if (GET_MODE (XEXP (rtl, 0)) != mode)
14308 return NULL;
14310 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14311 VAR_INIT_STATUS_INITIALIZED);
14312 if (op0 == NULL)
14313 return NULL;
14314 ret = op0;
14315 if (GET_CODE (rtl) == CLZ)
14317 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14318 valv = GET_MODE_BITSIZE (mode);
14320 else if (GET_CODE (rtl) == FFS)
14321 valv = 0;
14322 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14323 valv = GET_MODE_BITSIZE (mode);
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14325 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14326 add_loc_descr (&ret, l1jump);
14327 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14328 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14329 VAR_INIT_STATUS_INITIALIZED);
14330 if (tmp == NULL)
14331 return NULL;
14332 add_loc_descr (&ret, tmp);
14333 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14334 add_loc_descr (&ret, l4jump);
14335 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14336 ? const1_rtx : const0_rtx,
14337 mode, mem_mode,
14338 VAR_INIT_STATUS_INITIALIZED);
14339 if (l1label == NULL)
14340 return NULL;
14341 add_loc_descr (&ret, l1label);
14342 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14343 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14344 add_loc_descr (&ret, l2label);
14345 if (GET_CODE (rtl) != CLZ)
14346 msb = const1_rtx;
14347 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14348 msb = GEN_INT (HOST_WIDE_INT_1U
14349 << (GET_MODE_BITSIZE (mode) - 1));
14350 else
14351 msb = immed_wide_int_const
14352 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14353 GET_MODE_PRECISION (mode)), mode);
14354 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14355 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14356 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14357 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14358 else
14359 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14360 VAR_INIT_STATUS_INITIALIZED);
14361 if (tmp == NULL)
14362 return NULL;
14363 add_loc_descr (&ret, tmp);
14364 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14365 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14366 add_loc_descr (&ret, l3jump);
14367 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14368 VAR_INIT_STATUS_INITIALIZED);
14369 if (tmp == NULL)
14370 return NULL;
14371 add_loc_descr (&ret, tmp);
14372 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14373 ? DW_OP_shl : DW_OP_shr, 0, 0));
14374 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14375 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14376 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14377 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14378 add_loc_descr (&ret, l2jump);
14379 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14380 add_loc_descr (&ret, l3label);
14381 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14382 add_loc_descr (&ret, l4label);
14383 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14384 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14385 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14386 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14387 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14388 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14389 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14390 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14391 return ret;
14394 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14395 const1 is DW_OP_lit1 or corresponding typed constant):
14396 const0 DW_OP_swap
14397 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14398 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14399 L2: DW_OP_drop
14401 PARITY is similar:
14402 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14403 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14404 L2: DW_OP_drop */
14406 static dw_loc_descr_ref
14407 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14408 machine_mode mem_mode)
14410 dw_loc_descr_ref op0, ret, tmp;
14411 dw_loc_descr_ref l1jump, l1label;
14412 dw_loc_descr_ref l2jump, l2label;
14414 if (GET_MODE (XEXP (rtl, 0)) != mode)
14415 return NULL;
14417 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14418 VAR_INIT_STATUS_INITIALIZED);
14419 if (op0 == NULL)
14420 return NULL;
14421 ret = op0;
14422 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14423 VAR_INIT_STATUS_INITIALIZED);
14424 if (tmp == NULL)
14425 return NULL;
14426 add_loc_descr (&ret, tmp);
14427 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14428 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14429 add_loc_descr (&ret, l1label);
14430 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14431 add_loc_descr (&ret, l2jump);
14432 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14433 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14434 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14435 VAR_INIT_STATUS_INITIALIZED);
14436 if (tmp == NULL)
14437 return NULL;
14438 add_loc_descr (&ret, tmp);
14439 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14440 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14441 ? DW_OP_plus : DW_OP_xor, 0, 0));
14442 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14443 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14444 VAR_INIT_STATUS_INITIALIZED);
14445 add_loc_descr (&ret, tmp);
14446 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14447 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14448 add_loc_descr (&ret, l1jump);
14449 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14450 add_loc_descr (&ret, l2label);
14451 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14452 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14453 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14454 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14455 return ret;
14458 /* BSWAP (constS is initial shift count, either 56 or 24):
14459 constS const0
14460 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14461 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14462 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14463 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14464 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14466 static dw_loc_descr_ref
14467 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14468 machine_mode mem_mode)
14470 dw_loc_descr_ref op0, ret, tmp;
14471 dw_loc_descr_ref l1jump, l1label;
14472 dw_loc_descr_ref l2jump, l2label;
14474 if (BITS_PER_UNIT != 8
14475 || (GET_MODE_BITSIZE (mode) != 32
14476 && GET_MODE_BITSIZE (mode) != 64))
14477 return NULL;
14479 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14480 VAR_INIT_STATUS_INITIALIZED);
14481 if (op0 == NULL)
14482 return NULL;
14484 ret = op0;
14485 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14486 mode, mem_mode,
14487 VAR_INIT_STATUS_INITIALIZED);
14488 if (tmp == NULL)
14489 return NULL;
14490 add_loc_descr (&ret, tmp);
14491 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14492 VAR_INIT_STATUS_INITIALIZED);
14493 if (tmp == NULL)
14494 return NULL;
14495 add_loc_descr (&ret, tmp);
14496 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14497 add_loc_descr (&ret, l1label);
14498 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14499 mode, mem_mode,
14500 VAR_INIT_STATUS_INITIALIZED);
14501 add_loc_descr (&ret, tmp);
14502 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14503 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14505 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14506 VAR_INIT_STATUS_INITIALIZED);
14507 if (tmp == NULL)
14508 return NULL;
14509 add_loc_descr (&ret, tmp);
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14511 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14512 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14514 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14515 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14516 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14517 VAR_INIT_STATUS_INITIALIZED);
14518 add_loc_descr (&ret, tmp);
14519 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14520 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14521 add_loc_descr (&ret, l2jump);
14522 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14523 VAR_INIT_STATUS_INITIALIZED);
14524 add_loc_descr (&ret, tmp);
14525 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14526 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14527 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14528 add_loc_descr (&ret, l1jump);
14529 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14530 add_loc_descr (&ret, l2label);
14531 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14532 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14533 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14534 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14535 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14536 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14537 return ret;
14540 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14541 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14542 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14543 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14545 ROTATERT is similar:
14546 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14547 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14548 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14550 static dw_loc_descr_ref
14551 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14552 machine_mode mem_mode)
14554 rtx rtlop1 = XEXP (rtl, 1);
14555 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14556 int i;
14558 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14559 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14560 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14561 VAR_INIT_STATUS_INITIALIZED);
14562 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14563 VAR_INIT_STATUS_INITIALIZED);
14564 if (op0 == NULL || op1 == NULL)
14565 return NULL;
14566 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14567 for (i = 0; i < 2; i++)
14569 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14570 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14571 mode, mem_mode,
14572 VAR_INIT_STATUS_INITIALIZED);
14573 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14574 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14575 ? DW_OP_const4u
14576 : HOST_BITS_PER_WIDE_INT == 64
14577 ? DW_OP_const8u : DW_OP_constu,
14578 GET_MODE_MASK (mode), 0);
14579 else
14580 mask[i] = NULL;
14581 if (mask[i] == NULL)
14582 return NULL;
14583 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14585 ret = op0;
14586 add_loc_descr (&ret, op1);
14587 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14588 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14589 if (GET_CODE (rtl) == ROTATERT)
14591 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14592 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14593 GET_MODE_BITSIZE (mode), 0));
14595 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14596 if (mask[0] != NULL)
14597 add_loc_descr (&ret, mask[0]);
14598 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14599 if (mask[1] != NULL)
14601 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14602 add_loc_descr (&ret, mask[1]);
14603 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14605 if (GET_CODE (rtl) == ROTATE)
14607 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14608 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14609 GET_MODE_BITSIZE (mode), 0));
14611 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14612 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14613 return ret;
14616 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14617 for DEBUG_PARAMETER_REF RTL. */
14619 static dw_loc_descr_ref
14620 parameter_ref_descriptor (rtx rtl)
14622 dw_loc_descr_ref ret;
14623 dw_die_ref ref;
14625 if (dwarf_strict)
14626 return NULL;
14627 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14628 /* With LTO during LTRANS we get the late DIE that refers to the early
14629 DIE, thus we add another indirection here. This seems to confuse
14630 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14631 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14632 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14633 if (ref)
14635 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14636 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14637 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14639 else
14641 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14642 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14644 return ret;
14647 /* The following routine converts the RTL for a variable or parameter
14648 (resident in memory) into an equivalent Dwarf representation of a
14649 mechanism for getting the address of that same variable onto the top of a
14650 hypothetical "address evaluation" stack.
14652 When creating memory location descriptors, we are effectively transforming
14653 the RTL for a memory-resident object into its Dwarf postfix expression
14654 equivalent. This routine recursively descends an RTL tree, turning
14655 it into Dwarf postfix code as it goes.
14657 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14659 MEM_MODE is the mode of the memory reference, needed to handle some
14660 autoincrement addressing modes.
14662 Return 0 if we can't represent the location. */
14664 dw_loc_descr_ref
14665 mem_loc_descriptor (rtx rtl, machine_mode mode,
14666 machine_mode mem_mode,
14667 enum var_init_status initialized)
14669 dw_loc_descr_ref mem_loc_result = NULL;
14670 enum dwarf_location_atom op;
14671 dw_loc_descr_ref op0, op1;
14672 rtx inner = NULL_RTX;
14674 if (mode == VOIDmode)
14675 mode = GET_MODE (rtl);
14677 /* Note that for a dynamically sized array, the location we will generate a
14678 description of here will be the lowest numbered location which is
14679 actually within the array. That's *not* necessarily the same as the
14680 zeroth element of the array. */
14682 rtl = targetm.delegitimize_address (rtl);
14684 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14685 return NULL;
14687 scalar_int_mode int_mode, inner_mode, op1_mode;
14688 switch (GET_CODE (rtl))
14690 case POST_INC:
14691 case POST_DEC:
14692 case POST_MODIFY:
14693 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14695 case SUBREG:
14696 /* The case of a subreg may arise when we have a local (register)
14697 variable or a formal (register) parameter which doesn't quite fill
14698 up an entire register. For now, just assume that it is
14699 legitimate to make the Dwarf info refer to the whole register which
14700 contains the given subreg. */
14701 if (!subreg_lowpart_p (rtl))
14702 break;
14703 inner = SUBREG_REG (rtl);
14704 /* FALLTHRU */
14705 case TRUNCATE:
14706 if (inner == NULL_RTX)
14707 inner = XEXP (rtl, 0);
14708 if (is_a <scalar_int_mode> (mode, &int_mode)
14709 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14710 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14711 #ifdef POINTERS_EXTEND_UNSIGNED
14712 || (int_mode == Pmode && mem_mode != VOIDmode)
14713 #endif
14715 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14717 mem_loc_result = mem_loc_descriptor (inner,
14718 inner_mode,
14719 mem_mode, initialized);
14720 break;
14722 if (dwarf_strict && dwarf_version < 5)
14723 break;
14724 if (is_a <scalar_int_mode> (mode, &int_mode)
14725 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14726 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14727 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14729 dw_die_ref type_die;
14730 dw_loc_descr_ref cvt;
14732 mem_loc_result = mem_loc_descriptor (inner,
14733 GET_MODE (inner),
14734 mem_mode, initialized);
14735 if (mem_loc_result == NULL)
14736 break;
14737 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14738 if (type_die == NULL)
14740 mem_loc_result = NULL;
14741 break;
14743 if (GET_MODE_SIZE (mode)
14744 != GET_MODE_SIZE (GET_MODE (inner)))
14745 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14746 else
14747 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14748 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14749 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14750 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14751 add_loc_descr (&mem_loc_result, cvt);
14752 if (is_a <scalar_int_mode> (mode, &int_mode)
14753 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14755 /* Convert it to untyped afterwards. */
14756 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14757 add_loc_descr (&mem_loc_result, cvt);
14760 break;
14762 case REG:
14763 if (!is_a <scalar_int_mode> (mode, &int_mode)
14764 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14765 && rtl != arg_pointer_rtx
14766 && rtl != frame_pointer_rtx
14767 #ifdef POINTERS_EXTEND_UNSIGNED
14768 && (int_mode != Pmode || mem_mode == VOIDmode)
14769 #endif
14772 dw_die_ref type_die;
14773 unsigned int dbx_regnum;
14775 if (dwarf_strict && dwarf_version < 5)
14776 break;
14777 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14778 break;
14779 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14780 if (type_die == NULL)
14781 break;
14783 dbx_regnum = dbx_reg_number (rtl);
14784 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14785 break;
14786 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14787 dbx_regnum, 0);
14788 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14789 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14790 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14791 break;
14793 /* Whenever a register number forms a part of the description of the
14794 method for calculating the (dynamic) address of a memory resident
14795 object, DWARF rules require the register number be referred to as
14796 a "base register". This distinction is not based in any way upon
14797 what category of register the hardware believes the given register
14798 belongs to. This is strictly DWARF terminology we're dealing with
14799 here. Note that in cases where the location of a memory-resident
14800 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14801 OP_CONST (0)) the actual DWARF location descriptor that we generate
14802 may just be OP_BASEREG (basereg). This may look deceptively like
14803 the object in question was allocated to a register (rather than in
14804 memory) so DWARF consumers need to be aware of the subtle
14805 distinction between OP_REG and OP_BASEREG. */
14806 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14807 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14808 else if (stack_realign_drap
14809 && crtl->drap_reg
14810 && crtl->args.internal_arg_pointer == rtl
14811 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14813 /* If RTL is internal_arg_pointer, which has been optimized
14814 out, use DRAP instead. */
14815 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14816 VAR_INIT_STATUS_INITIALIZED);
14818 break;
14820 case SIGN_EXTEND:
14821 case ZERO_EXTEND:
14822 if (!is_a <scalar_int_mode> (mode, &int_mode)
14823 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14824 break;
14825 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14826 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14827 if (op0 == 0)
14828 break;
14829 else if (GET_CODE (rtl) == ZERO_EXTEND
14830 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14831 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14832 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14833 to expand zero extend as two shifts instead of
14834 masking. */
14835 && GET_MODE_SIZE (inner_mode) <= 4)
14837 mem_loc_result = op0;
14838 add_loc_descr (&mem_loc_result,
14839 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14840 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14842 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14844 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14845 shift *= BITS_PER_UNIT;
14846 if (GET_CODE (rtl) == SIGN_EXTEND)
14847 op = DW_OP_shra;
14848 else
14849 op = DW_OP_shr;
14850 mem_loc_result = op0;
14851 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14852 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14853 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14854 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14856 else if (!dwarf_strict || dwarf_version >= 5)
14858 dw_die_ref type_die1, type_die2;
14859 dw_loc_descr_ref cvt;
14861 type_die1 = base_type_for_mode (inner_mode,
14862 GET_CODE (rtl) == ZERO_EXTEND);
14863 if (type_die1 == NULL)
14864 break;
14865 type_die2 = base_type_for_mode (int_mode, 1);
14866 if (type_die2 == NULL)
14867 break;
14868 mem_loc_result = op0;
14869 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14870 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14871 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14872 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14873 add_loc_descr (&mem_loc_result, cvt);
14874 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14875 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14876 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14877 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14878 add_loc_descr (&mem_loc_result, cvt);
14880 break;
14882 case MEM:
14884 rtx new_rtl = avoid_constant_pool_reference (rtl);
14885 if (new_rtl != rtl)
14887 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14888 initialized);
14889 if (mem_loc_result != NULL)
14890 return mem_loc_result;
14893 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14894 get_address_mode (rtl), mode,
14895 VAR_INIT_STATUS_INITIALIZED);
14896 if (mem_loc_result == NULL)
14897 mem_loc_result = tls_mem_loc_descriptor (rtl);
14898 if (mem_loc_result != NULL)
14900 if (!is_a <scalar_int_mode> (mode, &int_mode)
14901 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14903 dw_die_ref type_die;
14904 dw_loc_descr_ref deref;
14906 if (dwarf_strict && dwarf_version < 5)
14907 return NULL;
14908 type_die
14909 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14910 if (type_die == NULL)
14911 return NULL;
14912 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14913 GET_MODE_SIZE (mode), 0);
14914 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14915 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14916 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14917 add_loc_descr (&mem_loc_result, deref);
14919 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14920 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14921 else
14922 add_loc_descr (&mem_loc_result,
14923 new_loc_descr (DW_OP_deref_size,
14924 GET_MODE_SIZE (int_mode), 0));
14926 break;
14928 case LO_SUM:
14929 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14931 case LABEL_REF:
14932 /* Some ports can transform a symbol ref into a label ref, because
14933 the symbol ref is too far away and has to be dumped into a constant
14934 pool. */
14935 case CONST:
14936 case SYMBOL_REF:
14937 if (!is_a <scalar_int_mode> (mode, &int_mode)
14938 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14939 #ifdef POINTERS_EXTEND_UNSIGNED
14940 && (int_mode != Pmode || mem_mode == VOIDmode)
14941 #endif
14943 break;
14944 if (GET_CODE (rtl) == SYMBOL_REF
14945 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14947 dw_loc_descr_ref temp;
14949 /* If this is not defined, we have no way to emit the data. */
14950 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14951 break;
14953 temp = new_addr_loc_descr (rtl, dtprel_true);
14955 /* We check for DWARF 5 here because gdb did not implement
14956 DW_OP_form_tls_address until after 7.12. */
14957 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14958 ? DW_OP_form_tls_address
14959 : DW_OP_GNU_push_tls_address),
14960 0, 0);
14961 add_loc_descr (&mem_loc_result, temp);
14963 break;
14966 if (!const_ok_for_output (rtl))
14968 if (GET_CODE (rtl) == CONST)
14969 switch (GET_CODE (XEXP (rtl, 0)))
14971 case NOT:
14972 op = DW_OP_not;
14973 goto try_const_unop;
14974 case NEG:
14975 op = DW_OP_neg;
14976 goto try_const_unop;
14977 try_const_unop:
14978 rtx arg;
14979 arg = XEXP (XEXP (rtl, 0), 0);
14980 if (!CONSTANT_P (arg))
14981 arg = gen_rtx_CONST (int_mode, arg);
14982 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
14983 initialized);
14984 if (op0)
14986 mem_loc_result = op0;
14987 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14989 break;
14990 default:
14991 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14992 mem_mode, initialized);
14993 break;
14995 break;
14998 symref:
14999 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15000 vec_safe_push (used_rtx_array, rtl);
15001 break;
15003 case CONCAT:
15004 case CONCATN:
15005 case VAR_LOCATION:
15006 case DEBUG_IMPLICIT_PTR:
15007 expansion_failed (NULL_TREE, rtl,
15008 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15009 return 0;
15011 case ENTRY_VALUE:
15012 if (dwarf_strict && dwarf_version < 5)
15013 return NULL;
15014 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15016 if (!is_a <scalar_int_mode> (mode, &int_mode)
15017 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15018 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15019 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15020 else
15022 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15023 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15024 return NULL;
15025 op0 = one_reg_loc_descriptor (dbx_regnum,
15026 VAR_INIT_STATUS_INITIALIZED);
15029 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15030 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15032 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15033 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15034 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15035 return NULL;
15037 else
15038 gcc_unreachable ();
15039 if (op0 == NULL)
15040 return NULL;
15041 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15042 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15043 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15044 break;
15046 case DEBUG_PARAMETER_REF:
15047 mem_loc_result = parameter_ref_descriptor (rtl);
15048 break;
15050 case PRE_MODIFY:
15051 /* Extract the PLUS expression nested inside and fall into
15052 PLUS code below. */
15053 rtl = XEXP (rtl, 1);
15054 goto plus;
15056 case PRE_INC:
15057 case PRE_DEC:
15058 /* Turn these into a PLUS expression and fall into the PLUS code
15059 below. */
15060 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15061 gen_int_mode (GET_CODE (rtl) == PRE_INC
15062 ? GET_MODE_UNIT_SIZE (mem_mode)
15063 : -GET_MODE_UNIT_SIZE (mem_mode),
15064 mode));
15066 /* fall through */
15068 case PLUS:
15069 plus:
15070 if (is_based_loc (rtl)
15071 && is_a <scalar_int_mode> (mode, &int_mode)
15072 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15073 || XEXP (rtl, 0) == arg_pointer_rtx
15074 || XEXP (rtl, 0) == frame_pointer_rtx))
15075 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15076 INTVAL (XEXP (rtl, 1)),
15077 VAR_INIT_STATUS_INITIALIZED);
15078 else
15080 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15081 VAR_INIT_STATUS_INITIALIZED);
15082 if (mem_loc_result == 0)
15083 break;
15085 if (CONST_INT_P (XEXP (rtl, 1))
15086 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15087 <= DWARF2_ADDR_SIZE))
15088 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15089 else
15091 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15092 VAR_INIT_STATUS_INITIALIZED);
15093 if (op1 == 0)
15094 return NULL;
15095 add_loc_descr (&mem_loc_result, op1);
15096 add_loc_descr (&mem_loc_result,
15097 new_loc_descr (DW_OP_plus, 0, 0));
15100 break;
15102 /* If a pseudo-reg is optimized away, it is possible for it to
15103 be replaced with a MEM containing a multiply or shift. */
15104 case MINUS:
15105 op = DW_OP_minus;
15106 goto do_binop;
15108 case MULT:
15109 op = DW_OP_mul;
15110 goto do_binop;
15112 case DIV:
15113 if ((!dwarf_strict || dwarf_version >= 5)
15114 && is_a <scalar_int_mode> (mode, &int_mode)
15115 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15117 mem_loc_result = typed_binop (DW_OP_div, rtl,
15118 base_type_for_mode (mode, 0),
15119 int_mode, mem_mode);
15120 break;
15122 op = DW_OP_div;
15123 goto do_binop;
15125 case UMOD:
15126 op = DW_OP_mod;
15127 goto do_binop;
15129 case ASHIFT:
15130 op = DW_OP_shl;
15131 goto do_shift;
15133 case ASHIFTRT:
15134 op = DW_OP_shra;
15135 goto do_shift;
15137 case LSHIFTRT:
15138 op = DW_OP_shr;
15139 goto do_shift;
15141 do_shift:
15142 if (!is_a <scalar_int_mode> (mode, &int_mode))
15143 break;
15144 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15145 VAR_INIT_STATUS_INITIALIZED);
15147 rtx rtlop1 = XEXP (rtl, 1);
15148 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15149 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15150 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15151 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15152 VAR_INIT_STATUS_INITIALIZED);
15155 if (op0 == 0 || op1 == 0)
15156 break;
15158 mem_loc_result = op0;
15159 add_loc_descr (&mem_loc_result, op1);
15160 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15161 break;
15163 case AND:
15164 op = DW_OP_and;
15165 goto do_binop;
15167 case IOR:
15168 op = DW_OP_or;
15169 goto do_binop;
15171 case XOR:
15172 op = DW_OP_xor;
15173 goto do_binop;
15175 do_binop:
15176 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15177 VAR_INIT_STATUS_INITIALIZED);
15178 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15179 VAR_INIT_STATUS_INITIALIZED);
15181 if (op0 == 0 || op1 == 0)
15182 break;
15184 mem_loc_result = op0;
15185 add_loc_descr (&mem_loc_result, op1);
15186 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15187 break;
15189 case MOD:
15190 if ((!dwarf_strict || dwarf_version >= 5)
15191 && is_a <scalar_int_mode> (mode, &int_mode)
15192 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15194 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15195 base_type_for_mode (mode, 0),
15196 int_mode, mem_mode);
15197 break;
15200 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15201 VAR_INIT_STATUS_INITIALIZED);
15202 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15203 VAR_INIT_STATUS_INITIALIZED);
15205 if (op0 == 0 || op1 == 0)
15206 break;
15208 mem_loc_result = op0;
15209 add_loc_descr (&mem_loc_result, op1);
15210 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15211 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15212 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15213 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15214 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15215 break;
15217 case UDIV:
15218 if ((!dwarf_strict || dwarf_version >= 5)
15219 && is_a <scalar_int_mode> (mode, &int_mode))
15221 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15223 op = DW_OP_div;
15224 goto do_binop;
15226 mem_loc_result = typed_binop (DW_OP_div, rtl,
15227 base_type_for_mode (int_mode, 1),
15228 int_mode, mem_mode);
15230 break;
15232 case NOT:
15233 op = DW_OP_not;
15234 goto do_unop;
15236 case ABS:
15237 op = DW_OP_abs;
15238 goto do_unop;
15240 case NEG:
15241 op = DW_OP_neg;
15242 goto do_unop;
15244 do_unop:
15245 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15246 VAR_INIT_STATUS_INITIALIZED);
15248 if (op0 == 0)
15249 break;
15251 mem_loc_result = op0;
15252 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15253 break;
15255 case CONST_INT:
15256 if (!is_a <scalar_int_mode> (mode, &int_mode)
15257 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15258 #ifdef POINTERS_EXTEND_UNSIGNED
15259 || (int_mode == Pmode
15260 && mem_mode != VOIDmode
15261 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15262 #endif
15265 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15266 break;
15268 if ((!dwarf_strict || dwarf_version >= 5)
15269 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15270 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15272 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15273 scalar_int_mode amode;
15274 if (type_die == NULL)
15275 return NULL;
15276 if (INTVAL (rtl) >= 0
15277 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15278 .exists (&amode))
15279 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15280 /* const DW_OP_convert <XXX> vs.
15281 DW_OP_const_type <XXX, 1, const>. */
15282 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15283 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15285 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15286 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15287 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15288 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15289 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15290 add_loc_descr (&mem_loc_result, op0);
15291 return mem_loc_result;
15293 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15294 INTVAL (rtl));
15295 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15296 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15297 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15298 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15299 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15300 else
15302 mem_loc_result->dw_loc_oprnd2.val_class
15303 = dw_val_class_const_double;
15304 mem_loc_result->dw_loc_oprnd2.v.val_double
15305 = double_int::from_shwi (INTVAL (rtl));
15308 break;
15310 case CONST_DOUBLE:
15311 if (!dwarf_strict || dwarf_version >= 5)
15313 dw_die_ref type_die;
15315 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15316 CONST_DOUBLE rtx could represent either a large integer
15317 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15318 the value is always a floating point constant.
15320 When it is an integer, a CONST_DOUBLE is used whenever
15321 the constant requires 2 HWIs to be adequately represented.
15322 We output CONST_DOUBLEs as blocks. */
15323 if (mode == VOIDmode
15324 || (GET_MODE (rtl) == VOIDmode
15325 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15326 break;
15327 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15328 if (type_die == NULL)
15329 return NULL;
15330 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15331 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15332 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15333 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15334 #if TARGET_SUPPORTS_WIDE_INT == 0
15335 if (!SCALAR_FLOAT_MODE_P (mode))
15337 mem_loc_result->dw_loc_oprnd2.val_class
15338 = dw_val_class_const_double;
15339 mem_loc_result->dw_loc_oprnd2.v.val_double
15340 = rtx_to_double_int (rtl);
15342 else
15343 #endif
15345 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15346 unsigned int length = GET_MODE_SIZE (float_mode);
15347 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15349 insert_float (rtl, array);
15350 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15351 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15352 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15353 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15356 break;
15358 case CONST_WIDE_INT:
15359 if (!dwarf_strict || dwarf_version >= 5)
15361 dw_die_ref type_die;
15363 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15364 if (type_die == NULL)
15365 return NULL;
15366 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15367 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15368 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15369 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15370 mem_loc_result->dw_loc_oprnd2.val_class
15371 = dw_val_class_wide_int;
15372 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15373 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15375 break;
15377 case EQ:
15378 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15379 break;
15381 case GE:
15382 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15383 break;
15385 case GT:
15386 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15387 break;
15389 case LE:
15390 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15391 break;
15393 case LT:
15394 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15395 break;
15397 case NE:
15398 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15399 break;
15401 case GEU:
15402 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15403 break;
15405 case GTU:
15406 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15407 break;
15409 case LEU:
15410 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15411 break;
15413 case LTU:
15414 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15415 break;
15417 case UMIN:
15418 case UMAX:
15419 if (!SCALAR_INT_MODE_P (mode))
15420 break;
15421 /* FALLTHRU */
15422 case SMIN:
15423 case SMAX:
15424 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15425 break;
15427 case ZERO_EXTRACT:
15428 case SIGN_EXTRACT:
15429 if (CONST_INT_P (XEXP (rtl, 1))
15430 && CONST_INT_P (XEXP (rtl, 2))
15431 && is_a <scalar_int_mode> (mode, &int_mode)
15432 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15433 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15434 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15435 && ((unsigned) INTVAL (XEXP (rtl, 1))
15436 + (unsigned) INTVAL (XEXP (rtl, 2))
15437 <= GET_MODE_BITSIZE (int_mode)))
15439 int shift, size;
15440 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15441 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15442 if (op0 == 0)
15443 break;
15444 if (GET_CODE (rtl) == SIGN_EXTRACT)
15445 op = DW_OP_shra;
15446 else
15447 op = DW_OP_shr;
15448 mem_loc_result = op0;
15449 size = INTVAL (XEXP (rtl, 1));
15450 shift = INTVAL (XEXP (rtl, 2));
15451 if (BITS_BIG_ENDIAN)
15452 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15453 if (shift + size != (int) DWARF2_ADDR_SIZE)
15455 add_loc_descr (&mem_loc_result,
15456 int_loc_descriptor (DWARF2_ADDR_SIZE
15457 - shift - size));
15458 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15460 if (size != (int) DWARF2_ADDR_SIZE)
15462 add_loc_descr (&mem_loc_result,
15463 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15464 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15467 break;
15469 case IF_THEN_ELSE:
15471 dw_loc_descr_ref op2, bra_node, drop_node;
15472 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15473 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15474 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15475 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15476 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15477 VAR_INIT_STATUS_INITIALIZED);
15478 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15479 VAR_INIT_STATUS_INITIALIZED);
15480 if (op0 == NULL || op1 == NULL || op2 == NULL)
15481 break;
15483 mem_loc_result = op1;
15484 add_loc_descr (&mem_loc_result, op2);
15485 add_loc_descr (&mem_loc_result, op0);
15486 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15487 add_loc_descr (&mem_loc_result, bra_node);
15488 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15489 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15490 add_loc_descr (&mem_loc_result, drop_node);
15491 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15492 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15494 break;
15496 case FLOAT_EXTEND:
15497 case FLOAT_TRUNCATE:
15498 case FLOAT:
15499 case UNSIGNED_FLOAT:
15500 case FIX:
15501 case UNSIGNED_FIX:
15502 if (!dwarf_strict || dwarf_version >= 5)
15504 dw_die_ref type_die;
15505 dw_loc_descr_ref cvt;
15507 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15508 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15509 if (op0 == NULL)
15510 break;
15511 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15512 && (GET_CODE (rtl) == FLOAT
15513 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15515 type_die = base_type_for_mode (int_mode,
15516 GET_CODE (rtl) == UNSIGNED_FLOAT);
15517 if (type_die == NULL)
15518 break;
15519 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15520 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15521 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15522 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15523 add_loc_descr (&op0, cvt);
15525 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15526 if (type_die == NULL)
15527 break;
15528 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15529 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15530 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15531 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15532 add_loc_descr (&op0, cvt);
15533 if (is_a <scalar_int_mode> (mode, &int_mode)
15534 && (GET_CODE (rtl) == FIX
15535 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15537 op0 = convert_descriptor_to_mode (int_mode, op0);
15538 if (op0 == NULL)
15539 break;
15541 mem_loc_result = op0;
15543 break;
15545 case CLZ:
15546 case CTZ:
15547 case FFS:
15548 if (is_a <scalar_int_mode> (mode, &int_mode))
15549 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15550 break;
15552 case POPCOUNT:
15553 case PARITY:
15554 if (is_a <scalar_int_mode> (mode, &int_mode))
15555 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15556 break;
15558 case BSWAP:
15559 if (is_a <scalar_int_mode> (mode, &int_mode))
15560 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15561 break;
15563 case ROTATE:
15564 case ROTATERT:
15565 if (is_a <scalar_int_mode> (mode, &int_mode))
15566 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15567 break;
15569 case COMPARE:
15570 /* In theory, we could implement the above. */
15571 /* DWARF cannot represent the unsigned compare operations
15572 natively. */
15573 case SS_MULT:
15574 case US_MULT:
15575 case SS_DIV:
15576 case US_DIV:
15577 case SS_PLUS:
15578 case US_PLUS:
15579 case SS_MINUS:
15580 case US_MINUS:
15581 case SS_NEG:
15582 case US_NEG:
15583 case SS_ABS:
15584 case SS_ASHIFT:
15585 case US_ASHIFT:
15586 case SS_TRUNCATE:
15587 case US_TRUNCATE:
15588 case UNORDERED:
15589 case ORDERED:
15590 case UNEQ:
15591 case UNGE:
15592 case UNGT:
15593 case UNLE:
15594 case UNLT:
15595 case LTGT:
15596 case FRACT_CONVERT:
15597 case UNSIGNED_FRACT_CONVERT:
15598 case SAT_FRACT:
15599 case UNSIGNED_SAT_FRACT:
15600 case SQRT:
15601 case ASM_OPERANDS:
15602 case VEC_MERGE:
15603 case VEC_SELECT:
15604 case VEC_CONCAT:
15605 case VEC_DUPLICATE:
15606 case VEC_SERIES:
15607 case UNSPEC:
15608 case HIGH:
15609 case FMA:
15610 case STRICT_LOW_PART:
15611 case CONST_VECTOR:
15612 case CONST_FIXED:
15613 case CLRSB:
15614 case CLOBBER:
15615 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15616 can't express it in the debug info. This can happen e.g. with some
15617 TLS UNSPECs. */
15618 break;
15620 case CONST_STRING:
15621 resolve_one_addr (&rtl);
15622 goto symref;
15624 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15625 the expression. An UNSPEC rtx represents a raw DWARF operation,
15626 new_loc_descr is called for it to build the operation directly.
15627 Otherwise mem_loc_descriptor is called recursively. */
15628 case PARALLEL:
15630 int index = 0;
15631 dw_loc_descr_ref exp_result = NULL;
15633 for (; index < XVECLEN (rtl, 0); index++)
15635 rtx elem = XVECEXP (rtl, 0, index);
15636 if (GET_CODE (elem) == UNSPEC)
15638 /* Each DWARF operation UNSPEC contain two operands, if
15639 one operand is not used for the operation, const0_rtx is
15640 passed. */
15641 gcc_assert (XVECLEN (elem, 0) == 2);
15643 HOST_WIDE_INT dw_op = XINT (elem, 1);
15644 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15645 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15646 exp_result
15647 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15648 oprnd2);
15650 else
15651 exp_result
15652 = mem_loc_descriptor (elem, mode, mem_mode,
15653 VAR_INIT_STATUS_INITIALIZED);
15655 if (!mem_loc_result)
15656 mem_loc_result = exp_result;
15657 else
15658 add_loc_descr (&mem_loc_result, exp_result);
15661 break;
15664 default:
15665 if (flag_checking)
15667 print_rtl (stderr, rtl);
15668 gcc_unreachable ();
15670 break;
15673 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15674 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15676 return mem_loc_result;
15679 /* Return a descriptor that describes the concatenation of two locations.
15680 This is typically a complex variable. */
15682 static dw_loc_descr_ref
15683 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15685 dw_loc_descr_ref cc_loc_result = NULL;
15686 dw_loc_descr_ref x0_ref
15687 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15688 dw_loc_descr_ref x1_ref
15689 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15691 if (x0_ref == 0 || x1_ref == 0)
15692 return 0;
15694 cc_loc_result = x0_ref;
15695 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15697 add_loc_descr (&cc_loc_result, x1_ref);
15698 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15700 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15701 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15703 return cc_loc_result;
15706 /* Return a descriptor that describes the concatenation of N
15707 locations. */
15709 static dw_loc_descr_ref
15710 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15712 unsigned int i;
15713 dw_loc_descr_ref cc_loc_result = NULL;
15714 unsigned int n = XVECLEN (concatn, 0);
15716 for (i = 0; i < n; ++i)
15718 dw_loc_descr_ref ref;
15719 rtx x = XVECEXP (concatn, 0, i);
15721 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15722 if (ref == NULL)
15723 return NULL;
15725 add_loc_descr (&cc_loc_result, ref);
15726 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15729 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15730 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15732 return cc_loc_result;
15735 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15736 for DEBUG_IMPLICIT_PTR RTL. */
15738 static dw_loc_descr_ref
15739 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15741 dw_loc_descr_ref ret;
15742 dw_die_ref ref;
15744 if (dwarf_strict && dwarf_version < 5)
15745 return NULL;
15746 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15747 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15748 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15749 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15750 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15751 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15752 if (ref)
15754 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15755 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15756 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15758 else
15760 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15761 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15763 return ret;
15766 /* Output a proper Dwarf location descriptor for a variable or parameter
15767 which is either allocated in a register or in a memory location. For a
15768 register, we just generate an OP_REG and the register number. For a
15769 memory location we provide a Dwarf postfix expression describing how to
15770 generate the (dynamic) address of the object onto the address stack.
15772 MODE is mode of the decl if this loc_descriptor is going to be used in
15773 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15774 allowed, VOIDmode otherwise.
15776 If we don't know how to describe it, return 0. */
15778 static dw_loc_descr_ref
15779 loc_descriptor (rtx rtl, machine_mode mode,
15780 enum var_init_status initialized)
15782 dw_loc_descr_ref loc_result = NULL;
15783 scalar_int_mode int_mode;
15785 switch (GET_CODE (rtl))
15787 case SUBREG:
15788 /* The case of a subreg may arise when we have a local (register)
15789 variable or a formal (register) parameter which doesn't quite fill
15790 up an entire register. For now, just assume that it is
15791 legitimate to make the Dwarf info refer to the whole register which
15792 contains the given subreg. */
15793 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15794 loc_result = loc_descriptor (SUBREG_REG (rtl),
15795 GET_MODE (SUBREG_REG (rtl)), initialized);
15796 else
15797 goto do_default;
15798 break;
15800 case REG:
15801 loc_result = reg_loc_descriptor (rtl, initialized);
15802 break;
15804 case MEM:
15805 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15806 GET_MODE (rtl), initialized);
15807 if (loc_result == NULL)
15808 loc_result = tls_mem_loc_descriptor (rtl);
15809 if (loc_result == NULL)
15811 rtx new_rtl = avoid_constant_pool_reference (rtl);
15812 if (new_rtl != rtl)
15813 loc_result = loc_descriptor (new_rtl, mode, initialized);
15815 break;
15817 case CONCAT:
15818 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15819 initialized);
15820 break;
15822 case CONCATN:
15823 loc_result = concatn_loc_descriptor (rtl, initialized);
15824 break;
15826 case VAR_LOCATION:
15827 /* Single part. */
15828 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15830 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15831 if (GET_CODE (loc) == EXPR_LIST)
15832 loc = XEXP (loc, 0);
15833 loc_result = loc_descriptor (loc, mode, initialized);
15834 break;
15837 rtl = XEXP (rtl, 1);
15838 /* FALLTHRU */
15840 case PARALLEL:
15842 rtvec par_elems = XVEC (rtl, 0);
15843 int num_elem = GET_NUM_ELEM (par_elems);
15844 machine_mode mode;
15845 int i;
15847 /* Create the first one, so we have something to add to. */
15848 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15849 VOIDmode, initialized);
15850 if (loc_result == NULL)
15851 return NULL;
15852 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15853 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15854 for (i = 1; i < num_elem; i++)
15856 dw_loc_descr_ref temp;
15858 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15859 VOIDmode, initialized);
15860 if (temp == NULL)
15861 return NULL;
15862 add_loc_descr (&loc_result, temp);
15863 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15864 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15867 break;
15869 case CONST_INT:
15870 if (mode != VOIDmode && mode != BLKmode)
15872 int_mode = as_a <scalar_int_mode> (mode);
15873 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15874 INTVAL (rtl));
15876 break;
15878 case CONST_DOUBLE:
15879 if (mode == VOIDmode)
15880 mode = GET_MODE (rtl);
15882 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15884 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15886 /* Note that a CONST_DOUBLE rtx could represent either an integer
15887 or a floating-point constant. A CONST_DOUBLE is used whenever
15888 the constant requires more than one word in order to be
15889 adequately represented. We output CONST_DOUBLEs as blocks. */
15890 scalar_mode smode = as_a <scalar_mode> (mode);
15891 loc_result = new_loc_descr (DW_OP_implicit_value,
15892 GET_MODE_SIZE (smode), 0);
15893 #if TARGET_SUPPORTS_WIDE_INT == 0
15894 if (!SCALAR_FLOAT_MODE_P (smode))
15896 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15897 loc_result->dw_loc_oprnd2.v.val_double
15898 = rtx_to_double_int (rtl);
15900 else
15901 #endif
15903 unsigned int length = GET_MODE_SIZE (smode);
15904 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15906 insert_float (rtl, array);
15907 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15908 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15909 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15910 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15913 break;
15915 case CONST_WIDE_INT:
15916 if (mode == VOIDmode)
15917 mode = GET_MODE (rtl);
15919 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15921 int_mode = as_a <scalar_int_mode> (mode);
15922 loc_result = new_loc_descr (DW_OP_implicit_value,
15923 GET_MODE_SIZE (int_mode), 0);
15924 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15925 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15926 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15928 break;
15930 case CONST_VECTOR:
15931 if (mode == VOIDmode)
15932 mode = GET_MODE (rtl);
15934 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15936 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15937 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15938 unsigned char *array
15939 = ggc_vec_alloc<unsigned char> (length * elt_size);
15940 unsigned int i;
15941 unsigned char *p;
15942 machine_mode imode = GET_MODE_INNER (mode);
15944 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15945 switch (GET_MODE_CLASS (mode))
15947 case MODE_VECTOR_INT:
15948 for (i = 0, p = array; i < length; i++, p += elt_size)
15950 rtx elt = CONST_VECTOR_ELT (rtl, i);
15951 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15953 break;
15955 case MODE_VECTOR_FLOAT:
15956 for (i = 0, p = array; i < length; i++, p += elt_size)
15958 rtx elt = CONST_VECTOR_ELT (rtl, i);
15959 insert_float (elt, p);
15961 break;
15963 default:
15964 gcc_unreachable ();
15967 loc_result = new_loc_descr (DW_OP_implicit_value,
15968 length * elt_size, 0);
15969 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15970 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15971 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15972 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15974 break;
15976 case CONST:
15977 if (mode == VOIDmode
15978 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15979 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15980 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15982 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15983 break;
15985 /* FALLTHROUGH */
15986 case SYMBOL_REF:
15987 if (!const_ok_for_output (rtl))
15988 break;
15989 /* FALLTHROUGH */
15990 case LABEL_REF:
15991 if (is_a <scalar_int_mode> (mode, &int_mode)
15992 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15993 && (dwarf_version >= 4 || !dwarf_strict))
15995 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15996 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15997 vec_safe_push (used_rtx_array, rtl);
15999 break;
16001 case DEBUG_IMPLICIT_PTR:
16002 loc_result = implicit_ptr_descriptor (rtl, 0);
16003 break;
16005 case PLUS:
16006 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16007 && CONST_INT_P (XEXP (rtl, 1)))
16009 loc_result
16010 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16011 break;
16013 /* FALLTHRU */
16014 do_default:
16015 default:
16016 if ((is_a <scalar_int_mode> (mode, &int_mode)
16017 && GET_MODE (rtl) == int_mode
16018 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16019 && dwarf_version >= 4)
16020 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16022 /* Value expression. */
16023 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16024 if (loc_result)
16025 add_loc_descr (&loc_result,
16026 new_loc_descr (DW_OP_stack_value, 0, 0));
16028 break;
16031 return loc_result;
16034 /* We need to figure out what section we should use as the base for the
16035 address ranges where a given location is valid.
16036 1. If this particular DECL has a section associated with it, use that.
16037 2. If this function has a section associated with it, use that.
16038 3. Otherwise, use the text section.
16039 XXX: If you split a variable across multiple sections, we won't notice. */
16041 static const char *
16042 secname_for_decl (const_tree decl)
16044 const char *secname;
16046 if (VAR_OR_FUNCTION_DECL_P (decl)
16047 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16048 && DECL_SECTION_NAME (decl))
16049 secname = DECL_SECTION_NAME (decl);
16050 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16051 secname = DECL_SECTION_NAME (current_function_decl);
16052 else if (cfun && in_cold_section_p)
16053 secname = crtl->subsections.cold_section_label;
16054 else
16055 secname = text_section_label;
16057 return secname;
16060 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16062 static bool
16063 decl_by_reference_p (tree decl)
16065 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16066 || VAR_P (decl))
16067 && DECL_BY_REFERENCE (decl));
16070 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16071 for VARLOC. */
16073 static dw_loc_descr_ref
16074 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16075 enum var_init_status initialized)
16077 int have_address = 0;
16078 dw_loc_descr_ref descr;
16079 machine_mode mode;
16081 if (want_address != 2)
16083 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16084 /* Single part. */
16085 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16087 varloc = PAT_VAR_LOCATION_LOC (varloc);
16088 if (GET_CODE (varloc) == EXPR_LIST)
16089 varloc = XEXP (varloc, 0);
16090 mode = GET_MODE (varloc);
16091 if (MEM_P (varloc))
16093 rtx addr = XEXP (varloc, 0);
16094 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16095 mode, initialized);
16096 if (descr)
16097 have_address = 1;
16098 else
16100 rtx x = avoid_constant_pool_reference (varloc);
16101 if (x != varloc)
16102 descr = mem_loc_descriptor (x, mode, VOIDmode,
16103 initialized);
16106 else
16107 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16109 else
16110 return 0;
16112 else
16114 if (GET_CODE (varloc) == VAR_LOCATION)
16115 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16116 else
16117 mode = DECL_MODE (loc);
16118 descr = loc_descriptor (varloc, mode, initialized);
16119 have_address = 1;
16122 if (!descr)
16123 return 0;
16125 if (want_address == 2 && !have_address
16126 && (dwarf_version >= 4 || !dwarf_strict))
16128 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16130 expansion_failed (loc, NULL_RTX,
16131 "DWARF address size mismatch");
16132 return 0;
16134 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16135 have_address = 1;
16137 /* Show if we can't fill the request for an address. */
16138 if (want_address && !have_address)
16140 expansion_failed (loc, NULL_RTX,
16141 "Want address and only have value");
16142 return 0;
16145 /* If we've got an address and don't want one, dereference. */
16146 if (!want_address && have_address)
16148 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16149 enum dwarf_location_atom op;
16151 if (size > DWARF2_ADDR_SIZE || size == -1)
16153 expansion_failed (loc, NULL_RTX,
16154 "DWARF address size mismatch");
16155 return 0;
16157 else if (size == DWARF2_ADDR_SIZE)
16158 op = DW_OP_deref;
16159 else
16160 op = DW_OP_deref_size;
16162 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16165 return descr;
16168 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16169 if it is not possible. */
16171 static dw_loc_descr_ref
16172 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16174 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16175 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16176 else if (dwarf_version >= 3 || !dwarf_strict)
16177 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16178 else
16179 return NULL;
16182 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16183 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16185 static dw_loc_descr_ref
16186 dw_sra_loc_expr (tree decl, rtx loc)
16188 rtx p;
16189 unsigned HOST_WIDE_INT padsize = 0;
16190 dw_loc_descr_ref descr, *descr_tail;
16191 unsigned HOST_WIDE_INT decl_size;
16192 rtx varloc;
16193 enum var_init_status initialized;
16195 if (DECL_SIZE (decl) == NULL
16196 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16197 return NULL;
16199 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16200 descr = NULL;
16201 descr_tail = &descr;
16203 for (p = loc; p; p = XEXP (p, 1))
16205 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16206 rtx loc_note = *decl_piece_varloc_ptr (p);
16207 dw_loc_descr_ref cur_descr;
16208 dw_loc_descr_ref *tail, last = NULL;
16209 unsigned HOST_WIDE_INT opsize = 0;
16211 if (loc_note == NULL_RTX
16212 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16214 padsize += bitsize;
16215 continue;
16217 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16218 varloc = NOTE_VAR_LOCATION (loc_note);
16219 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16220 if (cur_descr == NULL)
16222 padsize += bitsize;
16223 continue;
16226 /* Check that cur_descr either doesn't use
16227 DW_OP_*piece operations, or their sum is equal
16228 to bitsize. Otherwise we can't embed it. */
16229 for (tail = &cur_descr; *tail != NULL;
16230 tail = &(*tail)->dw_loc_next)
16231 if ((*tail)->dw_loc_opc == DW_OP_piece)
16233 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16234 * BITS_PER_UNIT;
16235 last = *tail;
16237 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16239 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16240 last = *tail;
16243 if (last != NULL && opsize != bitsize)
16245 padsize += bitsize;
16246 /* Discard the current piece of the descriptor and release any
16247 addr_table entries it uses. */
16248 remove_loc_list_addr_table_entries (cur_descr);
16249 continue;
16252 /* If there is a hole, add DW_OP_*piece after empty DWARF
16253 expression, which means that those bits are optimized out. */
16254 if (padsize)
16256 if (padsize > decl_size)
16258 remove_loc_list_addr_table_entries (cur_descr);
16259 goto discard_descr;
16261 decl_size -= padsize;
16262 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16263 if (*descr_tail == NULL)
16265 remove_loc_list_addr_table_entries (cur_descr);
16266 goto discard_descr;
16268 descr_tail = &(*descr_tail)->dw_loc_next;
16269 padsize = 0;
16271 *descr_tail = cur_descr;
16272 descr_tail = tail;
16273 if (bitsize > decl_size)
16274 goto discard_descr;
16275 decl_size -= bitsize;
16276 if (last == NULL)
16278 HOST_WIDE_INT offset = 0;
16279 if (GET_CODE (varloc) == VAR_LOCATION
16280 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16282 varloc = PAT_VAR_LOCATION_LOC (varloc);
16283 if (GET_CODE (varloc) == EXPR_LIST)
16284 varloc = XEXP (varloc, 0);
16288 if (GET_CODE (varloc) == CONST
16289 || GET_CODE (varloc) == SIGN_EXTEND
16290 || GET_CODE (varloc) == ZERO_EXTEND)
16291 varloc = XEXP (varloc, 0);
16292 else if (GET_CODE (varloc) == SUBREG)
16293 varloc = SUBREG_REG (varloc);
16294 else
16295 break;
16297 while (1);
16298 /* DW_OP_bit_size offset should be zero for register
16299 or implicit location descriptions and empty location
16300 descriptions, but for memory addresses needs big endian
16301 adjustment. */
16302 if (MEM_P (varloc))
16304 unsigned HOST_WIDE_INT memsize
16305 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16306 if (memsize != bitsize)
16308 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16309 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16310 goto discard_descr;
16311 if (memsize < bitsize)
16312 goto discard_descr;
16313 if (BITS_BIG_ENDIAN)
16314 offset = memsize - bitsize;
16318 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16319 if (*descr_tail == NULL)
16320 goto discard_descr;
16321 descr_tail = &(*descr_tail)->dw_loc_next;
16325 /* If there were any non-empty expressions, add padding till the end of
16326 the decl. */
16327 if (descr != NULL && decl_size != 0)
16329 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16330 if (*descr_tail == NULL)
16331 goto discard_descr;
16333 return descr;
16335 discard_descr:
16336 /* Discard the descriptor and release any addr_table entries it uses. */
16337 remove_loc_list_addr_table_entries (descr);
16338 return NULL;
16341 /* Return the dwarf representation of the location list LOC_LIST of
16342 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16343 function. */
16345 static dw_loc_list_ref
16346 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16348 const char *endname, *secname;
16349 rtx varloc;
16350 enum var_init_status initialized;
16351 struct var_loc_node *node;
16352 dw_loc_descr_ref descr;
16353 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16354 dw_loc_list_ref list = NULL;
16355 dw_loc_list_ref *listp = &list;
16357 /* Now that we know what section we are using for a base,
16358 actually construct the list of locations.
16359 The first location information is what is passed to the
16360 function that creates the location list, and the remaining
16361 locations just get added on to that list.
16362 Note that we only know the start address for a location
16363 (IE location changes), so to build the range, we use
16364 the range [current location start, next location start].
16365 This means we have to special case the last node, and generate
16366 a range of [last location start, end of function label]. */
16368 if (cfun && crtl->has_bb_partition)
16370 bool save_in_cold_section_p = in_cold_section_p;
16371 in_cold_section_p = first_function_block_is_cold;
16372 if (loc_list->last_before_switch == NULL)
16373 in_cold_section_p = !in_cold_section_p;
16374 secname = secname_for_decl (decl);
16375 in_cold_section_p = save_in_cold_section_p;
16377 else
16378 secname = secname_for_decl (decl);
16380 for (node = loc_list->first; node; node = node->next)
16382 bool range_across_switch = false;
16383 if (GET_CODE (node->loc) == EXPR_LIST
16384 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16386 if (GET_CODE (node->loc) == EXPR_LIST)
16388 descr = NULL;
16389 /* This requires DW_OP_{,bit_}piece, which is not usable
16390 inside DWARF expressions. */
16391 if (want_address == 2)
16392 descr = dw_sra_loc_expr (decl, node->loc);
16394 else
16396 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16397 varloc = NOTE_VAR_LOCATION (node->loc);
16398 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16400 if (descr)
16402 /* If section switch happens in between node->label
16403 and node->next->label (or end of function) and
16404 we can't emit it as a single entry list,
16405 emit two ranges, first one ending at the end
16406 of first partition and second one starting at the
16407 beginning of second partition. */
16408 if (node == loc_list->last_before_switch
16409 && (node != loc_list->first || loc_list->first->next)
16410 && current_function_decl)
16412 endname = cfun->fde->dw_fde_end;
16413 range_across_switch = true;
16415 /* The variable has a location between NODE->LABEL and
16416 NODE->NEXT->LABEL. */
16417 else if (node->next)
16418 endname = node->next->label;
16419 /* If the variable has a location at the last label
16420 it keeps its location until the end of function. */
16421 else if (!current_function_decl)
16422 endname = text_end_label;
16423 else
16425 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16426 current_function_funcdef_no);
16427 endname = ggc_strdup (label_id);
16430 *listp = new_loc_list (descr, node->label, endname, secname);
16431 if (TREE_CODE (decl) == PARM_DECL
16432 && node == loc_list->first
16433 && NOTE_P (node->loc)
16434 && strcmp (node->label, endname) == 0)
16435 (*listp)->force = true;
16436 listp = &(*listp)->dw_loc_next;
16440 if (cfun
16441 && crtl->has_bb_partition
16442 && node == loc_list->last_before_switch)
16444 bool save_in_cold_section_p = in_cold_section_p;
16445 in_cold_section_p = !first_function_block_is_cold;
16446 secname = secname_for_decl (decl);
16447 in_cold_section_p = save_in_cold_section_p;
16450 if (range_across_switch)
16452 if (GET_CODE (node->loc) == EXPR_LIST)
16453 descr = dw_sra_loc_expr (decl, node->loc);
16454 else
16456 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16457 varloc = NOTE_VAR_LOCATION (node->loc);
16458 descr = dw_loc_list_1 (decl, varloc, want_address,
16459 initialized);
16461 gcc_assert (descr);
16462 /* The variable has a location between NODE->LABEL and
16463 NODE->NEXT->LABEL. */
16464 if (node->next)
16465 endname = node->next->label;
16466 else
16467 endname = cfun->fde->dw_fde_second_end;
16468 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin,
16469 endname, secname);
16470 listp = &(*listp)->dw_loc_next;
16474 /* Try to avoid the overhead of a location list emitting a location
16475 expression instead, but only if we didn't have more than one
16476 location entry in the first place. If some entries were not
16477 representable, we don't want to pretend a single entry that was
16478 applies to the entire scope in which the variable is
16479 available. */
16480 if (list && loc_list->first->next)
16481 gen_llsym (list);
16483 return list;
16486 /* Return if the loc_list has only single element and thus can be represented
16487 as location description. */
16489 static bool
16490 single_element_loc_list_p (dw_loc_list_ref list)
16492 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16493 return !list->ll_symbol;
16496 /* Duplicate a single element of location list. */
16498 static inline dw_loc_descr_ref
16499 copy_loc_descr (dw_loc_descr_ref ref)
16501 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16502 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16503 return copy;
16506 /* To each location in list LIST append loc descr REF. */
16508 static void
16509 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16511 dw_loc_descr_ref copy;
16512 add_loc_descr (&list->expr, ref);
16513 list = list->dw_loc_next;
16514 while (list)
16516 copy = copy_loc_descr (ref);
16517 add_loc_descr (&list->expr, copy);
16518 while (copy->dw_loc_next)
16519 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16520 list = list->dw_loc_next;
16524 /* To each location in list LIST prepend loc descr REF. */
16526 static void
16527 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16529 dw_loc_descr_ref copy;
16530 dw_loc_descr_ref ref_end = list->expr;
16531 add_loc_descr (&ref, list->expr);
16532 list->expr = ref;
16533 list = list->dw_loc_next;
16534 while (list)
16536 dw_loc_descr_ref end = list->expr;
16537 list->expr = copy = copy_loc_descr (ref);
16538 while (copy->dw_loc_next != ref_end)
16539 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16540 copy->dw_loc_next = end;
16541 list = list->dw_loc_next;
16545 /* Given two lists RET and LIST
16546 produce location list that is result of adding expression in LIST
16547 to expression in RET on each position in program.
16548 Might be destructive on both RET and LIST.
16550 TODO: We handle only simple cases of RET or LIST having at most one
16551 element. General case would involve sorting the lists in program order
16552 and merging them that will need some additional work.
16553 Adding that will improve quality of debug info especially for SRA-ed
16554 structures. */
16556 static void
16557 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16559 if (!list)
16560 return;
16561 if (!*ret)
16563 *ret = list;
16564 return;
16566 if (!list->dw_loc_next)
16568 add_loc_descr_to_each (*ret, list->expr);
16569 return;
16571 if (!(*ret)->dw_loc_next)
16573 prepend_loc_descr_to_each (list, (*ret)->expr);
16574 *ret = list;
16575 return;
16577 expansion_failed (NULL_TREE, NULL_RTX,
16578 "Don't know how to merge two non-trivial"
16579 " location lists.\n");
16580 *ret = NULL;
16581 return;
16584 /* LOC is constant expression. Try a luck, look it up in constant
16585 pool and return its loc_descr of its address. */
16587 static dw_loc_descr_ref
16588 cst_pool_loc_descr (tree loc)
16590 /* Get an RTL for this, if something has been emitted. */
16591 rtx rtl = lookup_constant_def (loc);
16593 if (!rtl || !MEM_P (rtl))
16595 gcc_assert (!rtl);
16596 return 0;
16598 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16600 /* TODO: We might get more coverage if we was actually delaying expansion
16601 of all expressions till end of compilation when constant pools are fully
16602 populated. */
16603 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16605 expansion_failed (loc, NULL_RTX,
16606 "CST value in contant pool but not marked.");
16607 return 0;
16609 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16610 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16613 /* Return dw_loc_list representing address of addr_expr LOC
16614 by looking for inner INDIRECT_REF expression and turning
16615 it into simple arithmetics.
16617 See loc_list_from_tree for the meaning of CONTEXT. */
16619 static dw_loc_list_ref
16620 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16621 loc_descr_context *context)
16623 tree obj, offset;
16624 HOST_WIDE_INT bitsize, bitpos, bytepos;
16625 machine_mode mode;
16626 int unsignedp, reversep, volatilep = 0;
16627 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16629 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16630 &bitsize, &bitpos, &offset, &mode,
16631 &unsignedp, &reversep, &volatilep);
16632 STRIP_NOPS (obj);
16633 if (bitpos % BITS_PER_UNIT)
16635 expansion_failed (loc, NULL_RTX, "bitfield access");
16636 return 0;
16638 if (!INDIRECT_REF_P (obj))
16640 expansion_failed (obj,
16641 NULL_RTX, "no indirect ref in inner refrence");
16642 return 0;
16644 if (!offset && !bitpos)
16645 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16646 context);
16647 else if (toplev
16648 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16649 && (dwarf_version >= 4 || !dwarf_strict))
16651 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16652 if (!list_ret)
16653 return 0;
16654 if (offset)
16656 /* Variable offset. */
16657 list_ret1 = loc_list_from_tree (offset, 0, context);
16658 if (list_ret1 == 0)
16659 return 0;
16660 add_loc_list (&list_ret, list_ret1);
16661 if (!list_ret)
16662 return 0;
16663 add_loc_descr_to_each (list_ret,
16664 new_loc_descr (DW_OP_plus, 0, 0));
16666 bytepos = bitpos / BITS_PER_UNIT;
16667 if (bytepos > 0)
16668 add_loc_descr_to_each (list_ret,
16669 new_loc_descr (DW_OP_plus_uconst,
16670 bytepos, 0));
16671 else if (bytepos < 0)
16672 loc_list_plus_const (list_ret, bytepos);
16673 add_loc_descr_to_each (list_ret,
16674 new_loc_descr (DW_OP_stack_value, 0, 0));
16676 return list_ret;
16679 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16680 all operations from LOC are nops, move to the last one. Insert in NOPS all
16681 operations that are skipped. */
16683 static void
16684 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16685 hash_set<dw_loc_descr_ref> &nops)
16687 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16689 nops.add (loc);
16690 loc = loc->dw_loc_next;
16694 /* Helper for loc_descr_without_nops: free the location description operation
16695 P. */
16697 bool
16698 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16700 ggc_free (loc);
16701 return true;
16704 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16705 finishes LOC. */
16707 static void
16708 loc_descr_without_nops (dw_loc_descr_ref &loc)
16710 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16711 return;
16713 /* Set of all DW_OP_nop operations we remove. */
16714 hash_set<dw_loc_descr_ref> nops;
16716 /* First, strip all prefix NOP operations in order to keep the head of the
16717 operations list. */
16718 loc_descr_to_next_no_nop (loc, nops);
16720 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16722 /* For control flow operations: strip "prefix" nops in destination
16723 labels. */
16724 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16725 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16726 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16727 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16729 /* Do the same for the operations that follow, then move to the next
16730 iteration. */
16731 if (cur->dw_loc_next != NULL)
16732 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16733 cur = cur->dw_loc_next;
16736 nops.traverse<void *, free_loc_descr> (NULL);
16740 struct dwarf_procedure_info;
16742 /* Helper structure for location descriptions generation. */
16743 struct loc_descr_context
16745 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16746 NULL_TREE if DW_OP_push_object_address in invalid for this location
16747 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16748 tree context_type;
16749 /* The ..._DECL node that should be translated as a
16750 DW_OP_push_object_address operation. */
16751 tree base_decl;
16752 /* Information about the DWARF procedure we are currently generating. NULL if
16753 we are not generating a DWARF procedure. */
16754 struct dwarf_procedure_info *dpi;
16755 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16756 by consumer. Used for DW_TAG_generic_subrange attributes. */
16757 bool placeholder_arg;
16758 /* True if PLACEHOLDER_EXPR has been seen. */
16759 bool placeholder_seen;
16762 /* DWARF procedures generation
16764 DWARF expressions (aka. location descriptions) are used to encode variable
16765 things such as sizes or offsets. Such computations can have redundant parts
16766 that can be factorized in order to reduce the size of the output debug
16767 information. This is the whole point of DWARF procedures.
16769 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16770 already factorized into functions ("size functions") in order to handle very
16771 big and complex types. Such functions are quite simple: they have integral
16772 arguments, they return an integral result and their body contains only a
16773 return statement with arithmetic expressions. This is the only kind of
16774 function we are interested in translating into DWARF procedures, here.
16776 DWARF expressions and DWARF procedure are executed using a stack, so we have
16777 to define some calling convention for them to interact. Let's say that:
16779 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16780 all arguments in reverse order (right-to-left) so that when the DWARF
16781 procedure execution starts, the first argument is the top of the stack.
16783 - Then, when returning, the DWARF procedure must have consumed all arguments
16784 on the stack, must have pushed the result and touched nothing else.
16786 - Each integral argument and the result are integral types can be hold in a
16787 single stack slot.
16789 - We call "frame offset" the number of stack slots that are "under DWARF
16790 procedure control": it includes the arguments slots, the temporaries and
16791 the result slot. Thus, it is equal to the number of arguments when the
16792 procedure execution starts and must be equal to one (the result) when it
16793 returns. */
16795 /* Helper structure used when generating operations for a DWARF procedure. */
16796 struct dwarf_procedure_info
16798 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16799 currently translated. */
16800 tree fndecl;
16801 /* The number of arguments FNDECL takes. */
16802 unsigned args_count;
16805 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16806 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16807 equate it to this DIE. */
16809 static dw_die_ref
16810 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16811 dw_die_ref parent_die)
16813 dw_die_ref dwarf_proc_die;
16815 if ((dwarf_version < 3 && dwarf_strict)
16816 || location == NULL)
16817 return NULL;
16819 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16820 if (fndecl)
16821 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16822 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16823 return dwarf_proc_die;
16826 /* Return whether TYPE is a supported type as a DWARF procedure argument
16827 type or return type (we handle only scalar types and pointer types that
16828 aren't wider than the DWARF expression evaluation stack. */
16830 static bool
16831 is_handled_procedure_type (tree type)
16833 return ((INTEGRAL_TYPE_P (type)
16834 || TREE_CODE (type) == OFFSET_TYPE
16835 || TREE_CODE (type) == POINTER_TYPE)
16836 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16839 /* Helper for resolve_args_picking: do the same but stop when coming across
16840 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16841 offset *before* evaluating the corresponding operation. */
16843 static bool
16844 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16845 struct dwarf_procedure_info *dpi,
16846 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16848 /* The "frame_offset" identifier is already used to name a macro... */
16849 unsigned frame_offset_ = initial_frame_offset;
16850 dw_loc_descr_ref l;
16852 for (l = loc; l != NULL;)
16854 bool existed;
16855 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16857 /* If we already met this node, there is nothing to compute anymore. */
16858 if (existed)
16860 /* Make sure that the stack size is consistent wherever the execution
16861 flow comes from. */
16862 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16863 break;
16865 l_frame_offset = frame_offset_;
16867 /* If needed, relocate the picking offset with respect to the frame
16868 offset. */
16869 if (l->frame_offset_rel)
16871 unsigned HOST_WIDE_INT off;
16872 switch (l->dw_loc_opc)
16874 case DW_OP_pick:
16875 off = l->dw_loc_oprnd1.v.val_unsigned;
16876 break;
16877 case DW_OP_dup:
16878 off = 0;
16879 break;
16880 case DW_OP_over:
16881 off = 1;
16882 break;
16883 default:
16884 gcc_unreachable ();
16886 /* frame_offset_ is the size of the current stack frame, including
16887 incoming arguments. Besides, the arguments are pushed
16888 right-to-left. Thus, in order to access the Nth argument from
16889 this operation node, the picking has to skip temporaries *plus*
16890 one stack slot per argument (0 for the first one, 1 for the second
16891 one, etc.).
16893 The targetted argument number (N) is already set as the operand,
16894 and the number of temporaries can be computed with:
16895 frame_offsets_ - dpi->args_count */
16896 off += frame_offset_ - dpi->args_count;
16898 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16899 if (off > 255)
16900 return false;
16902 if (off == 0)
16904 l->dw_loc_opc = DW_OP_dup;
16905 l->dw_loc_oprnd1.v.val_unsigned = 0;
16907 else if (off == 1)
16909 l->dw_loc_opc = DW_OP_over;
16910 l->dw_loc_oprnd1.v.val_unsigned = 0;
16912 else
16914 l->dw_loc_opc = DW_OP_pick;
16915 l->dw_loc_oprnd1.v.val_unsigned = off;
16919 /* Update frame_offset according to the effect the current operation has
16920 on the stack. */
16921 switch (l->dw_loc_opc)
16923 case DW_OP_deref:
16924 case DW_OP_swap:
16925 case DW_OP_rot:
16926 case DW_OP_abs:
16927 case DW_OP_neg:
16928 case DW_OP_not:
16929 case DW_OP_plus_uconst:
16930 case DW_OP_skip:
16931 case DW_OP_reg0:
16932 case DW_OP_reg1:
16933 case DW_OP_reg2:
16934 case DW_OP_reg3:
16935 case DW_OP_reg4:
16936 case DW_OP_reg5:
16937 case DW_OP_reg6:
16938 case DW_OP_reg7:
16939 case DW_OP_reg8:
16940 case DW_OP_reg9:
16941 case DW_OP_reg10:
16942 case DW_OP_reg11:
16943 case DW_OP_reg12:
16944 case DW_OP_reg13:
16945 case DW_OP_reg14:
16946 case DW_OP_reg15:
16947 case DW_OP_reg16:
16948 case DW_OP_reg17:
16949 case DW_OP_reg18:
16950 case DW_OP_reg19:
16951 case DW_OP_reg20:
16952 case DW_OP_reg21:
16953 case DW_OP_reg22:
16954 case DW_OP_reg23:
16955 case DW_OP_reg24:
16956 case DW_OP_reg25:
16957 case DW_OP_reg26:
16958 case DW_OP_reg27:
16959 case DW_OP_reg28:
16960 case DW_OP_reg29:
16961 case DW_OP_reg30:
16962 case DW_OP_reg31:
16963 case DW_OP_bregx:
16964 case DW_OP_piece:
16965 case DW_OP_deref_size:
16966 case DW_OP_nop:
16967 case DW_OP_bit_piece:
16968 case DW_OP_implicit_value:
16969 case DW_OP_stack_value:
16970 break;
16972 case DW_OP_addr:
16973 case DW_OP_const1u:
16974 case DW_OP_const1s:
16975 case DW_OP_const2u:
16976 case DW_OP_const2s:
16977 case DW_OP_const4u:
16978 case DW_OP_const4s:
16979 case DW_OP_const8u:
16980 case DW_OP_const8s:
16981 case DW_OP_constu:
16982 case DW_OP_consts:
16983 case DW_OP_dup:
16984 case DW_OP_over:
16985 case DW_OP_pick:
16986 case DW_OP_lit0:
16987 case DW_OP_lit1:
16988 case DW_OP_lit2:
16989 case DW_OP_lit3:
16990 case DW_OP_lit4:
16991 case DW_OP_lit5:
16992 case DW_OP_lit6:
16993 case DW_OP_lit7:
16994 case DW_OP_lit8:
16995 case DW_OP_lit9:
16996 case DW_OP_lit10:
16997 case DW_OP_lit11:
16998 case DW_OP_lit12:
16999 case DW_OP_lit13:
17000 case DW_OP_lit14:
17001 case DW_OP_lit15:
17002 case DW_OP_lit16:
17003 case DW_OP_lit17:
17004 case DW_OP_lit18:
17005 case DW_OP_lit19:
17006 case DW_OP_lit20:
17007 case DW_OP_lit21:
17008 case DW_OP_lit22:
17009 case DW_OP_lit23:
17010 case DW_OP_lit24:
17011 case DW_OP_lit25:
17012 case DW_OP_lit26:
17013 case DW_OP_lit27:
17014 case DW_OP_lit28:
17015 case DW_OP_lit29:
17016 case DW_OP_lit30:
17017 case DW_OP_lit31:
17018 case DW_OP_breg0:
17019 case DW_OP_breg1:
17020 case DW_OP_breg2:
17021 case DW_OP_breg3:
17022 case DW_OP_breg4:
17023 case DW_OP_breg5:
17024 case DW_OP_breg6:
17025 case DW_OP_breg7:
17026 case DW_OP_breg8:
17027 case DW_OP_breg9:
17028 case DW_OP_breg10:
17029 case DW_OP_breg11:
17030 case DW_OP_breg12:
17031 case DW_OP_breg13:
17032 case DW_OP_breg14:
17033 case DW_OP_breg15:
17034 case DW_OP_breg16:
17035 case DW_OP_breg17:
17036 case DW_OP_breg18:
17037 case DW_OP_breg19:
17038 case DW_OP_breg20:
17039 case DW_OP_breg21:
17040 case DW_OP_breg22:
17041 case DW_OP_breg23:
17042 case DW_OP_breg24:
17043 case DW_OP_breg25:
17044 case DW_OP_breg26:
17045 case DW_OP_breg27:
17046 case DW_OP_breg28:
17047 case DW_OP_breg29:
17048 case DW_OP_breg30:
17049 case DW_OP_breg31:
17050 case DW_OP_fbreg:
17051 case DW_OP_push_object_address:
17052 case DW_OP_call_frame_cfa:
17053 case DW_OP_GNU_variable_value:
17054 ++frame_offset_;
17055 break;
17057 case DW_OP_drop:
17058 case DW_OP_xderef:
17059 case DW_OP_and:
17060 case DW_OP_div:
17061 case DW_OP_minus:
17062 case DW_OP_mod:
17063 case DW_OP_mul:
17064 case DW_OP_or:
17065 case DW_OP_plus:
17066 case DW_OP_shl:
17067 case DW_OP_shr:
17068 case DW_OP_shra:
17069 case DW_OP_xor:
17070 case DW_OP_bra:
17071 case DW_OP_eq:
17072 case DW_OP_ge:
17073 case DW_OP_gt:
17074 case DW_OP_le:
17075 case DW_OP_lt:
17076 case DW_OP_ne:
17077 case DW_OP_regx:
17078 case DW_OP_xderef_size:
17079 --frame_offset_;
17080 break;
17082 case DW_OP_call2:
17083 case DW_OP_call4:
17084 case DW_OP_call_ref:
17086 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17087 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17089 if (stack_usage == NULL)
17090 return false;
17091 frame_offset_ += *stack_usage;
17092 break;
17095 case DW_OP_implicit_pointer:
17096 case DW_OP_entry_value:
17097 case DW_OP_const_type:
17098 case DW_OP_regval_type:
17099 case DW_OP_deref_type:
17100 case DW_OP_convert:
17101 case DW_OP_reinterpret:
17102 case DW_OP_form_tls_address:
17103 case DW_OP_GNU_push_tls_address:
17104 case DW_OP_GNU_uninit:
17105 case DW_OP_GNU_encoded_addr:
17106 case DW_OP_GNU_implicit_pointer:
17107 case DW_OP_GNU_entry_value:
17108 case DW_OP_GNU_const_type:
17109 case DW_OP_GNU_regval_type:
17110 case DW_OP_GNU_deref_type:
17111 case DW_OP_GNU_convert:
17112 case DW_OP_GNU_reinterpret:
17113 case DW_OP_GNU_parameter_ref:
17114 /* loc_list_from_tree will probably not output these operations for
17115 size functions, so assume they will not appear here. */
17116 /* Fall through... */
17118 default:
17119 gcc_unreachable ();
17122 /* Now, follow the control flow (except subroutine calls). */
17123 switch (l->dw_loc_opc)
17125 case DW_OP_bra:
17126 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17127 frame_offsets))
17128 return false;
17129 /* Fall through. */
17131 case DW_OP_skip:
17132 l = l->dw_loc_oprnd1.v.val_loc;
17133 break;
17135 case DW_OP_stack_value:
17136 return true;
17138 default:
17139 l = l->dw_loc_next;
17140 break;
17144 return true;
17147 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17148 operations) in order to resolve the operand of DW_OP_pick operations that
17149 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17150 offset *before* LOC is executed. Return if all relocations were
17151 successful. */
17153 static bool
17154 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17155 struct dwarf_procedure_info *dpi)
17157 /* Associate to all visited operations the frame offset *before* evaluating
17158 this operation. */
17159 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17161 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17162 frame_offsets);
17165 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17166 Return NULL if it is not possible. */
17168 static dw_die_ref
17169 function_to_dwarf_procedure (tree fndecl)
17171 struct loc_descr_context ctx;
17172 struct dwarf_procedure_info dpi;
17173 dw_die_ref dwarf_proc_die;
17174 tree tree_body = DECL_SAVED_TREE (fndecl);
17175 dw_loc_descr_ref loc_body, epilogue;
17177 tree cursor;
17178 unsigned i;
17180 /* Do not generate multiple DWARF procedures for the same function
17181 declaration. */
17182 dwarf_proc_die = lookup_decl_die (fndecl);
17183 if (dwarf_proc_die != NULL)
17184 return dwarf_proc_die;
17186 /* DWARF procedures are available starting with the DWARFv3 standard. */
17187 if (dwarf_version < 3 && dwarf_strict)
17188 return NULL;
17190 /* We handle only functions for which we still have a body, that return a
17191 supported type and that takes arguments with supported types. Note that
17192 there is no point translating functions that return nothing. */
17193 if (tree_body == NULL_TREE
17194 || DECL_RESULT (fndecl) == NULL_TREE
17195 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17196 return NULL;
17198 for (cursor = DECL_ARGUMENTS (fndecl);
17199 cursor != NULL_TREE;
17200 cursor = TREE_CHAIN (cursor))
17201 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17202 return NULL;
17204 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17205 if (TREE_CODE (tree_body) != RETURN_EXPR)
17206 return NULL;
17207 tree_body = TREE_OPERAND (tree_body, 0);
17208 if (TREE_CODE (tree_body) != MODIFY_EXPR
17209 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17210 return NULL;
17211 tree_body = TREE_OPERAND (tree_body, 1);
17213 /* Try to translate the body expression itself. Note that this will probably
17214 cause an infinite recursion if its call graph has a cycle. This is very
17215 unlikely for size functions, however, so don't bother with such things at
17216 the moment. */
17217 ctx.context_type = NULL_TREE;
17218 ctx.base_decl = NULL_TREE;
17219 ctx.dpi = &dpi;
17220 ctx.placeholder_arg = false;
17221 ctx.placeholder_seen = false;
17222 dpi.fndecl = fndecl;
17223 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17224 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17225 if (!loc_body)
17226 return NULL;
17228 /* After evaluating all operands in "loc_body", we should still have on the
17229 stack all arguments plus the desired function result (top of the stack).
17230 Generate code in order to keep only the result in our stack frame. */
17231 epilogue = NULL;
17232 for (i = 0; i < dpi.args_count; ++i)
17234 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17235 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17236 op_couple->dw_loc_next->dw_loc_next = epilogue;
17237 epilogue = op_couple;
17239 add_loc_descr (&loc_body, epilogue);
17240 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17241 return NULL;
17243 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17244 because they are considered useful. Now there is an epilogue, they are
17245 not anymore, so give it another try. */
17246 loc_descr_without_nops (loc_body);
17248 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17249 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17250 though, given that size functions do not come from source, so they should
17251 not have a dedicated DW_TAG_subprogram DIE. */
17252 dwarf_proc_die
17253 = new_dwarf_proc_die (loc_body, fndecl,
17254 get_context_die (DECL_CONTEXT (fndecl)));
17256 /* The called DWARF procedure consumes one stack slot per argument and
17257 returns one stack slot. */
17258 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17260 return dwarf_proc_die;
17264 /* Generate Dwarf location list representing LOC.
17265 If WANT_ADDRESS is false, expression computing LOC will be computed
17266 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17267 if WANT_ADDRESS is 2, expression computing address useable in location
17268 will be returned (i.e. DW_OP_reg can be used
17269 to refer to register values).
17271 CONTEXT provides information to customize the location descriptions
17272 generation. Its context_type field specifies what type is implicitly
17273 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17274 will not be generated.
17276 Its DPI field determines whether we are generating a DWARF expression for a
17277 DWARF procedure, so PARM_DECL references are processed specifically.
17279 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17280 and dpi fields were null. */
17282 static dw_loc_list_ref
17283 loc_list_from_tree_1 (tree loc, int want_address,
17284 struct loc_descr_context *context)
17286 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17287 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17288 int have_address = 0;
17289 enum dwarf_location_atom op;
17291 /* ??? Most of the time we do not take proper care for sign/zero
17292 extending the values properly. Hopefully this won't be a real
17293 problem... */
17295 if (context != NULL
17296 && context->base_decl == loc
17297 && want_address == 0)
17299 if (dwarf_version >= 3 || !dwarf_strict)
17300 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17301 NULL, NULL, NULL);
17302 else
17303 return NULL;
17306 switch (TREE_CODE (loc))
17308 case ERROR_MARK:
17309 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17310 return 0;
17312 case PLACEHOLDER_EXPR:
17313 /* This case involves extracting fields from an object to determine the
17314 position of other fields. It is supposed to appear only as the first
17315 operand of COMPONENT_REF nodes and to reference precisely the type
17316 that the context allows. */
17317 if (context != NULL
17318 && TREE_TYPE (loc) == context->context_type
17319 && want_address >= 1)
17321 if (dwarf_version >= 3 || !dwarf_strict)
17323 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17324 have_address = 1;
17325 break;
17327 else
17328 return NULL;
17330 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17331 the single argument passed by consumer. */
17332 else if (context != NULL
17333 && context->placeholder_arg
17334 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17335 && want_address == 0)
17337 ret = new_loc_descr (DW_OP_pick, 0, 0);
17338 ret->frame_offset_rel = 1;
17339 context->placeholder_seen = true;
17340 break;
17342 else
17343 expansion_failed (loc, NULL_RTX,
17344 "PLACEHOLDER_EXPR for an unexpected type");
17345 break;
17347 case CALL_EXPR:
17349 const int nargs = call_expr_nargs (loc);
17350 tree callee = get_callee_fndecl (loc);
17351 int i;
17352 dw_die_ref dwarf_proc;
17354 if (callee == NULL_TREE)
17355 goto call_expansion_failed;
17357 /* We handle only functions that return an integer. */
17358 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17359 goto call_expansion_failed;
17361 dwarf_proc = function_to_dwarf_procedure (callee);
17362 if (dwarf_proc == NULL)
17363 goto call_expansion_failed;
17365 /* Evaluate arguments right-to-left so that the first argument will
17366 be the top-most one on the stack. */
17367 for (i = nargs - 1; i >= 0; --i)
17369 dw_loc_descr_ref loc_descr
17370 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17371 context);
17373 if (loc_descr == NULL)
17374 goto call_expansion_failed;
17376 add_loc_descr (&ret, loc_descr);
17379 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17380 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17381 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17382 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17383 add_loc_descr (&ret, ret1);
17384 break;
17386 call_expansion_failed:
17387 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17388 /* There are no opcodes for these operations. */
17389 return 0;
17392 case PREINCREMENT_EXPR:
17393 case PREDECREMENT_EXPR:
17394 case POSTINCREMENT_EXPR:
17395 case POSTDECREMENT_EXPR:
17396 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17397 /* There are no opcodes for these operations. */
17398 return 0;
17400 case ADDR_EXPR:
17401 /* If we already want an address, see if there is INDIRECT_REF inside
17402 e.g. for &this->field. */
17403 if (want_address)
17405 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17406 (loc, want_address == 2, context);
17407 if (list_ret)
17408 have_address = 1;
17409 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17410 && (ret = cst_pool_loc_descr (loc)))
17411 have_address = 1;
17413 /* Otherwise, process the argument and look for the address. */
17414 if (!list_ret && !ret)
17415 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17416 else
17418 if (want_address)
17419 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17420 return NULL;
17422 break;
17424 case VAR_DECL:
17425 if (DECL_THREAD_LOCAL_P (loc))
17427 rtx rtl;
17428 enum dwarf_location_atom tls_op;
17429 enum dtprel_bool dtprel = dtprel_false;
17431 if (targetm.have_tls)
17433 /* If this is not defined, we have no way to emit the
17434 data. */
17435 if (!targetm.asm_out.output_dwarf_dtprel)
17436 return 0;
17438 /* The way DW_OP_GNU_push_tls_address is specified, we
17439 can only look up addresses of objects in the current
17440 module. We used DW_OP_addr as first op, but that's
17441 wrong, because DW_OP_addr is relocated by the debug
17442 info consumer, while DW_OP_GNU_push_tls_address
17443 operand shouldn't be. */
17444 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17445 return 0;
17446 dtprel = dtprel_true;
17447 /* We check for DWARF 5 here because gdb did not implement
17448 DW_OP_form_tls_address until after 7.12. */
17449 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17450 : DW_OP_GNU_push_tls_address);
17452 else
17454 if (!targetm.emutls.debug_form_tls_address
17455 || !(dwarf_version >= 3 || !dwarf_strict))
17456 return 0;
17457 /* We stuffed the control variable into the DECL_VALUE_EXPR
17458 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17459 no longer appear in gimple code. We used the control
17460 variable in specific so that we could pick it up here. */
17461 loc = DECL_VALUE_EXPR (loc);
17462 tls_op = DW_OP_form_tls_address;
17465 rtl = rtl_for_decl_location (loc);
17466 if (rtl == NULL_RTX)
17467 return 0;
17469 if (!MEM_P (rtl))
17470 return 0;
17471 rtl = XEXP (rtl, 0);
17472 if (! CONSTANT_P (rtl))
17473 return 0;
17475 ret = new_addr_loc_descr (rtl, dtprel);
17476 ret1 = new_loc_descr (tls_op, 0, 0);
17477 add_loc_descr (&ret, ret1);
17479 have_address = 1;
17480 break;
17482 /* FALLTHRU */
17484 case PARM_DECL:
17485 if (context != NULL && context->dpi != NULL
17486 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17488 /* We are generating code for a DWARF procedure and we want to access
17489 one of its arguments: find the appropriate argument offset and let
17490 the resolve_args_picking pass compute the offset that complies
17491 with the stack frame size. */
17492 unsigned i = 0;
17493 tree cursor;
17495 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17496 cursor != NULL_TREE && cursor != loc;
17497 cursor = TREE_CHAIN (cursor), ++i)
17499 /* If we are translating a DWARF procedure, all referenced parameters
17500 must belong to the current function. */
17501 gcc_assert (cursor != NULL_TREE);
17503 ret = new_loc_descr (DW_OP_pick, i, 0);
17504 ret->frame_offset_rel = 1;
17505 break;
17507 /* FALLTHRU */
17509 case RESULT_DECL:
17510 if (DECL_HAS_VALUE_EXPR_P (loc))
17511 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17512 want_address, context);
17513 /* FALLTHRU */
17515 case FUNCTION_DECL:
17517 rtx rtl;
17518 var_loc_list *loc_list = lookup_decl_loc (loc);
17520 if (loc_list && loc_list->first)
17522 list_ret = dw_loc_list (loc_list, loc, want_address);
17523 have_address = want_address != 0;
17524 break;
17526 rtl = rtl_for_decl_location (loc);
17527 if (rtl == NULL_RTX)
17529 if (TREE_CODE (loc) != FUNCTION_DECL
17530 && early_dwarf
17531 && current_function_decl
17532 && want_address != 1
17533 && ! DECL_IGNORED_P (loc)
17534 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17535 || POINTER_TYPE_P (TREE_TYPE (loc)))
17536 && DECL_CONTEXT (loc) == current_function_decl
17537 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17538 <= DWARF2_ADDR_SIZE))
17540 dw_die_ref ref = lookup_decl_die (loc);
17541 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17542 if (ref)
17544 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17545 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17546 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17548 else
17550 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17551 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17553 break;
17555 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17556 return 0;
17558 else if (CONST_INT_P (rtl))
17560 HOST_WIDE_INT val = INTVAL (rtl);
17561 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17562 val &= GET_MODE_MASK (DECL_MODE (loc));
17563 ret = int_loc_descriptor (val);
17565 else if (GET_CODE (rtl) == CONST_STRING)
17567 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17568 return 0;
17570 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17571 ret = new_addr_loc_descr (rtl, dtprel_false);
17572 else
17574 machine_mode mode, mem_mode;
17576 /* Certain constructs can only be represented at top-level. */
17577 if (want_address == 2)
17579 ret = loc_descriptor (rtl, VOIDmode,
17580 VAR_INIT_STATUS_INITIALIZED);
17581 have_address = 1;
17583 else
17585 mode = GET_MODE (rtl);
17586 mem_mode = VOIDmode;
17587 if (MEM_P (rtl))
17589 mem_mode = mode;
17590 mode = get_address_mode (rtl);
17591 rtl = XEXP (rtl, 0);
17592 have_address = 1;
17594 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17595 VAR_INIT_STATUS_INITIALIZED);
17597 if (!ret)
17598 expansion_failed (loc, rtl,
17599 "failed to produce loc descriptor for rtl");
17602 break;
17604 case MEM_REF:
17605 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17607 have_address = 1;
17608 goto do_plus;
17610 /* Fallthru. */
17611 case INDIRECT_REF:
17612 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17613 have_address = 1;
17614 break;
17616 case TARGET_MEM_REF:
17617 case SSA_NAME:
17618 case DEBUG_EXPR_DECL:
17619 return NULL;
17621 case COMPOUND_EXPR:
17622 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17623 context);
17625 CASE_CONVERT:
17626 case VIEW_CONVERT_EXPR:
17627 case SAVE_EXPR:
17628 case MODIFY_EXPR:
17629 case NON_LVALUE_EXPR:
17630 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17631 context);
17633 case COMPONENT_REF:
17634 case BIT_FIELD_REF:
17635 case ARRAY_REF:
17636 case ARRAY_RANGE_REF:
17637 case REALPART_EXPR:
17638 case IMAGPART_EXPR:
17640 tree obj, offset;
17641 HOST_WIDE_INT bitsize, bitpos, bytepos;
17642 machine_mode mode;
17643 int unsignedp, reversep, volatilep = 0;
17645 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17646 &unsignedp, &reversep, &volatilep);
17648 gcc_assert (obj != loc);
17650 list_ret = loc_list_from_tree_1 (obj,
17651 want_address == 2
17652 && !bitpos && !offset ? 2 : 1,
17653 context);
17654 /* TODO: We can extract value of the small expression via shifting even
17655 for nonzero bitpos. */
17656 if (list_ret == 0)
17657 return 0;
17658 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17660 expansion_failed (loc, NULL_RTX,
17661 "bitfield access");
17662 return 0;
17665 if (offset != NULL_TREE)
17667 /* Variable offset. */
17668 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17669 if (list_ret1 == 0)
17670 return 0;
17671 add_loc_list (&list_ret, list_ret1);
17672 if (!list_ret)
17673 return 0;
17674 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17677 bytepos = bitpos / BITS_PER_UNIT;
17678 if (bytepos > 0)
17679 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17680 else if (bytepos < 0)
17681 loc_list_plus_const (list_ret, bytepos);
17683 have_address = 1;
17684 break;
17687 case INTEGER_CST:
17688 if ((want_address || !tree_fits_shwi_p (loc))
17689 && (ret = cst_pool_loc_descr (loc)))
17690 have_address = 1;
17691 else if (want_address == 2
17692 && tree_fits_shwi_p (loc)
17693 && (ret = address_of_int_loc_descriptor
17694 (int_size_in_bytes (TREE_TYPE (loc)),
17695 tree_to_shwi (loc))))
17696 have_address = 1;
17697 else if (tree_fits_shwi_p (loc))
17698 ret = int_loc_descriptor (tree_to_shwi (loc));
17699 else if (tree_fits_uhwi_p (loc))
17700 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17701 else
17703 expansion_failed (loc, NULL_RTX,
17704 "Integer operand is not host integer");
17705 return 0;
17707 break;
17709 case CONSTRUCTOR:
17710 case REAL_CST:
17711 case STRING_CST:
17712 case COMPLEX_CST:
17713 if ((ret = cst_pool_loc_descr (loc)))
17714 have_address = 1;
17715 else if (TREE_CODE (loc) == CONSTRUCTOR)
17717 tree type = TREE_TYPE (loc);
17718 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17719 unsigned HOST_WIDE_INT offset = 0;
17720 unsigned HOST_WIDE_INT cnt;
17721 constructor_elt *ce;
17723 if (TREE_CODE (type) == RECORD_TYPE)
17725 /* This is very limited, but it's enough to output
17726 pointers to member functions, as long as the
17727 referenced function is defined in the current
17728 translation unit. */
17729 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17731 tree val = ce->value;
17733 tree field = ce->index;
17735 if (val)
17736 STRIP_NOPS (val);
17738 if (!field || DECL_BIT_FIELD (field))
17740 expansion_failed (loc, NULL_RTX,
17741 "bitfield in record type constructor");
17742 size = offset = (unsigned HOST_WIDE_INT)-1;
17743 ret = NULL;
17744 break;
17747 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17748 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17749 gcc_assert (pos + fieldsize <= size);
17750 if (pos < offset)
17752 expansion_failed (loc, NULL_RTX,
17753 "out-of-order fields in record constructor");
17754 size = offset = (unsigned HOST_WIDE_INT)-1;
17755 ret = NULL;
17756 break;
17758 if (pos > offset)
17760 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17761 add_loc_descr (&ret, ret1);
17762 offset = pos;
17764 if (val && fieldsize != 0)
17766 ret1 = loc_descriptor_from_tree (val, want_address, context);
17767 if (!ret1)
17769 expansion_failed (loc, NULL_RTX,
17770 "unsupported expression in field");
17771 size = offset = (unsigned HOST_WIDE_INT)-1;
17772 ret = NULL;
17773 break;
17775 add_loc_descr (&ret, ret1);
17777 if (fieldsize)
17779 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17780 add_loc_descr (&ret, ret1);
17781 offset = pos + fieldsize;
17785 if (offset != size)
17787 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17788 add_loc_descr (&ret, ret1);
17789 offset = size;
17792 have_address = !!want_address;
17794 else
17795 expansion_failed (loc, NULL_RTX,
17796 "constructor of non-record type");
17798 else
17799 /* We can construct small constants here using int_loc_descriptor. */
17800 expansion_failed (loc, NULL_RTX,
17801 "constructor or constant not in constant pool");
17802 break;
17804 case TRUTH_AND_EXPR:
17805 case TRUTH_ANDIF_EXPR:
17806 case BIT_AND_EXPR:
17807 op = DW_OP_and;
17808 goto do_binop;
17810 case TRUTH_XOR_EXPR:
17811 case BIT_XOR_EXPR:
17812 op = DW_OP_xor;
17813 goto do_binop;
17815 case TRUTH_OR_EXPR:
17816 case TRUTH_ORIF_EXPR:
17817 case BIT_IOR_EXPR:
17818 op = DW_OP_or;
17819 goto do_binop;
17821 case FLOOR_DIV_EXPR:
17822 case CEIL_DIV_EXPR:
17823 case ROUND_DIV_EXPR:
17824 case TRUNC_DIV_EXPR:
17825 case EXACT_DIV_EXPR:
17826 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17827 return 0;
17828 op = DW_OP_div;
17829 goto do_binop;
17831 case MINUS_EXPR:
17832 op = DW_OP_minus;
17833 goto do_binop;
17835 case FLOOR_MOD_EXPR:
17836 case CEIL_MOD_EXPR:
17837 case ROUND_MOD_EXPR:
17838 case TRUNC_MOD_EXPR:
17839 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17841 op = DW_OP_mod;
17842 goto do_binop;
17844 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17845 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17846 if (list_ret == 0 || list_ret1 == 0)
17847 return 0;
17849 add_loc_list (&list_ret, list_ret1);
17850 if (list_ret == 0)
17851 return 0;
17852 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17853 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17854 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17855 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17856 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17857 break;
17859 case MULT_EXPR:
17860 op = DW_OP_mul;
17861 goto do_binop;
17863 case LSHIFT_EXPR:
17864 op = DW_OP_shl;
17865 goto do_binop;
17867 case RSHIFT_EXPR:
17868 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17869 goto do_binop;
17871 case POINTER_PLUS_EXPR:
17872 case PLUS_EXPR:
17873 do_plus:
17874 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17876 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17877 smarter to encode their opposite. The DW_OP_plus_uconst operation
17878 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17879 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17880 bytes, Y being the size of the operation that pushes the opposite
17881 of the addend. So let's choose the smallest representation. */
17882 const tree tree_addend = TREE_OPERAND (loc, 1);
17883 offset_int wi_addend;
17884 HOST_WIDE_INT shwi_addend;
17885 dw_loc_descr_ref loc_naddend;
17887 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17888 if (list_ret == 0)
17889 return 0;
17891 /* Try to get the literal to push. It is the opposite of the addend,
17892 so as we rely on wrapping during DWARF evaluation, first decode
17893 the literal as a "DWARF-sized" signed number. */
17894 wi_addend = wi::to_offset (tree_addend);
17895 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17896 shwi_addend = wi_addend.to_shwi ();
17897 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17898 ? int_loc_descriptor (-shwi_addend)
17899 : NULL;
17901 if (loc_naddend != NULL
17902 && ((unsigned) size_of_uleb128 (shwi_addend)
17903 > size_of_loc_descr (loc_naddend)))
17905 add_loc_descr_to_each (list_ret, loc_naddend);
17906 add_loc_descr_to_each (list_ret,
17907 new_loc_descr (DW_OP_minus, 0, 0));
17909 else
17911 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17913 loc_naddend = loc_cur;
17914 loc_cur = loc_cur->dw_loc_next;
17915 ggc_free (loc_naddend);
17917 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17919 break;
17922 op = DW_OP_plus;
17923 goto do_binop;
17925 case LE_EXPR:
17926 op = DW_OP_le;
17927 goto do_comp_binop;
17929 case GE_EXPR:
17930 op = DW_OP_ge;
17931 goto do_comp_binop;
17933 case LT_EXPR:
17934 op = DW_OP_lt;
17935 goto do_comp_binop;
17937 case GT_EXPR:
17938 op = DW_OP_gt;
17939 goto do_comp_binop;
17941 do_comp_binop:
17942 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17944 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17945 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17946 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17947 TREE_CODE (loc));
17948 break;
17950 else
17951 goto do_binop;
17953 case EQ_EXPR:
17954 op = DW_OP_eq;
17955 goto do_binop;
17957 case NE_EXPR:
17958 op = DW_OP_ne;
17959 goto do_binop;
17961 do_binop:
17962 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17963 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17964 if (list_ret == 0 || list_ret1 == 0)
17965 return 0;
17967 add_loc_list (&list_ret, list_ret1);
17968 if (list_ret == 0)
17969 return 0;
17970 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17971 break;
17973 case TRUTH_NOT_EXPR:
17974 case BIT_NOT_EXPR:
17975 op = DW_OP_not;
17976 goto do_unop;
17978 case ABS_EXPR:
17979 op = DW_OP_abs;
17980 goto do_unop;
17982 case NEGATE_EXPR:
17983 op = DW_OP_neg;
17984 goto do_unop;
17986 do_unop:
17987 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17988 if (list_ret == 0)
17989 return 0;
17991 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17992 break;
17994 case MIN_EXPR:
17995 case MAX_EXPR:
17997 const enum tree_code code =
17998 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18000 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18001 build2 (code, integer_type_node,
18002 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18003 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18006 /* fall through */
18008 case COND_EXPR:
18010 dw_loc_descr_ref lhs
18011 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18012 dw_loc_list_ref rhs
18013 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18014 dw_loc_descr_ref bra_node, jump_node, tmp;
18016 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18017 if (list_ret == 0 || lhs == 0 || rhs == 0)
18018 return 0;
18020 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18021 add_loc_descr_to_each (list_ret, bra_node);
18023 add_loc_list (&list_ret, rhs);
18024 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18025 add_loc_descr_to_each (list_ret, jump_node);
18027 add_loc_descr_to_each (list_ret, lhs);
18028 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18029 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18031 /* ??? Need a node to point the skip at. Use a nop. */
18032 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18033 add_loc_descr_to_each (list_ret, tmp);
18034 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18035 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18037 break;
18039 case FIX_TRUNC_EXPR:
18040 return 0;
18042 default:
18043 /* Leave front-end specific codes as simply unknown. This comes
18044 up, for instance, with the C STMT_EXPR. */
18045 if ((unsigned int) TREE_CODE (loc)
18046 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18048 expansion_failed (loc, NULL_RTX,
18049 "language specific tree node");
18050 return 0;
18053 /* Otherwise this is a generic code; we should just lists all of
18054 these explicitly. We forgot one. */
18055 if (flag_checking)
18056 gcc_unreachable ();
18058 /* In a release build, we want to degrade gracefully: better to
18059 generate incomplete debugging information than to crash. */
18060 return NULL;
18063 if (!ret && !list_ret)
18064 return 0;
18066 if (want_address == 2 && !have_address
18067 && (dwarf_version >= 4 || !dwarf_strict))
18069 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18071 expansion_failed (loc, NULL_RTX,
18072 "DWARF address size mismatch");
18073 return 0;
18075 if (ret)
18076 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18077 else
18078 add_loc_descr_to_each (list_ret,
18079 new_loc_descr (DW_OP_stack_value, 0, 0));
18080 have_address = 1;
18082 /* Show if we can't fill the request for an address. */
18083 if (want_address && !have_address)
18085 expansion_failed (loc, NULL_RTX,
18086 "Want address and only have value");
18087 return 0;
18090 gcc_assert (!ret || !list_ret);
18092 /* If we've got an address and don't want one, dereference. */
18093 if (!want_address && have_address)
18095 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18097 if (size > DWARF2_ADDR_SIZE || size == -1)
18099 expansion_failed (loc, NULL_RTX,
18100 "DWARF address size mismatch");
18101 return 0;
18103 else if (size == DWARF2_ADDR_SIZE)
18104 op = DW_OP_deref;
18105 else
18106 op = DW_OP_deref_size;
18108 if (ret)
18109 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18110 else
18111 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18113 if (ret)
18114 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18116 return list_ret;
18119 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18120 expressions. */
18122 static dw_loc_list_ref
18123 loc_list_from_tree (tree loc, int want_address,
18124 struct loc_descr_context *context)
18126 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18128 for (dw_loc_list_ref loc_cur = result;
18129 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18130 loc_descr_without_nops (loc_cur->expr);
18131 return result;
18134 /* Same as above but return only single location expression. */
18135 static dw_loc_descr_ref
18136 loc_descriptor_from_tree (tree loc, int want_address,
18137 struct loc_descr_context *context)
18139 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18140 if (!ret)
18141 return NULL;
18142 if (ret->dw_loc_next)
18144 expansion_failed (loc, NULL_RTX,
18145 "Location list where only loc descriptor needed");
18146 return NULL;
18148 return ret->expr;
18151 /* Given a value, round it up to the lowest multiple of `boundary'
18152 which is not less than the value itself. */
18154 static inline HOST_WIDE_INT
18155 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18157 return (((value + boundary - 1) / boundary) * boundary);
18160 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18161 pointer to the declared type for the relevant field variable, or return
18162 `integer_type_node' if the given node turns out to be an
18163 ERROR_MARK node. */
18165 static inline tree
18166 field_type (const_tree decl)
18168 tree type;
18170 if (TREE_CODE (decl) == ERROR_MARK)
18171 return integer_type_node;
18173 type = DECL_BIT_FIELD_TYPE (decl);
18174 if (type == NULL_TREE)
18175 type = TREE_TYPE (decl);
18177 return type;
18180 /* Given a pointer to a tree node, return the alignment in bits for
18181 it, or else return BITS_PER_WORD if the node actually turns out to
18182 be an ERROR_MARK node. */
18184 static inline unsigned
18185 simple_type_align_in_bits (const_tree type)
18187 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18190 static inline unsigned
18191 simple_decl_align_in_bits (const_tree decl)
18193 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18196 /* Return the result of rounding T up to ALIGN. */
18198 static inline offset_int
18199 round_up_to_align (const offset_int &t, unsigned int align)
18201 return wi::udiv_trunc (t + align - 1, align) * align;
18204 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18205 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18206 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18207 if we fail to return the size in one of these two forms. */
18209 static dw_loc_descr_ref
18210 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18212 tree tree_size;
18213 struct loc_descr_context ctx;
18215 /* Return a constant integer in priority, if possible. */
18216 *cst_size = int_size_in_bytes (type);
18217 if (*cst_size != -1)
18218 return NULL;
18220 ctx.context_type = const_cast<tree> (type);
18221 ctx.base_decl = NULL_TREE;
18222 ctx.dpi = NULL;
18223 ctx.placeholder_arg = false;
18224 ctx.placeholder_seen = false;
18226 type = TYPE_MAIN_VARIANT (type);
18227 tree_size = TYPE_SIZE_UNIT (type);
18228 return ((tree_size != NULL_TREE)
18229 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18230 : NULL);
18233 /* Helper structure for RECORD_TYPE processing. */
18234 struct vlr_context
18236 /* Root RECORD_TYPE. It is needed to generate data member location
18237 descriptions in variable-length records (VLR), but also to cope with
18238 variants, which are composed of nested structures multiplexed with
18239 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18240 function processing a FIELD_DECL, it is required to be non null. */
18241 tree struct_type;
18242 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18243 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18244 this variant part as part of the root record (in storage units). For
18245 regular records, it must be NULL_TREE. */
18246 tree variant_part_offset;
18249 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18250 addressed byte of the "containing object" for the given FIELD_DECL. If
18251 possible, return a native constant through CST_OFFSET (in which case NULL is
18252 returned); otherwise return a DWARF expression that computes the offset.
18254 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18255 that offset is, either because the argument turns out to be a pointer to an
18256 ERROR_MARK node, or because the offset expression is too complex for us.
18258 CTX is required: see the comment for VLR_CONTEXT. */
18260 static dw_loc_descr_ref
18261 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18262 HOST_WIDE_INT *cst_offset)
18264 tree tree_result;
18265 dw_loc_list_ref loc_result;
18267 *cst_offset = 0;
18269 if (TREE_CODE (decl) == ERROR_MARK)
18270 return NULL;
18271 else
18272 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18274 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18275 case. */
18276 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18277 return NULL;
18279 #ifdef PCC_BITFIELD_TYPE_MATTERS
18280 /* We used to handle only constant offsets in all cases. Now, we handle
18281 properly dynamic byte offsets only when PCC bitfield type doesn't
18282 matter. */
18283 if (PCC_BITFIELD_TYPE_MATTERS
18284 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18286 offset_int object_offset_in_bits;
18287 offset_int object_offset_in_bytes;
18288 offset_int bitpos_int;
18289 tree type;
18290 tree field_size_tree;
18291 offset_int deepest_bitpos;
18292 offset_int field_size_in_bits;
18293 unsigned int type_align_in_bits;
18294 unsigned int decl_align_in_bits;
18295 offset_int type_size_in_bits;
18297 bitpos_int = wi::to_offset (bit_position (decl));
18298 type = field_type (decl);
18299 type_size_in_bits = offset_int_type_size_in_bits (type);
18300 type_align_in_bits = simple_type_align_in_bits (type);
18302 field_size_tree = DECL_SIZE (decl);
18304 /* The size could be unspecified if there was an error, or for
18305 a flexible array member. */
18306 if (!field_size_tree)
18307 field_size_tree = bitsize_zero_node;
18309 /* If the size of the field is not constant, use the type size. */
18310 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18311 field_size_in_bits = wi::to_offset (field_size_tree);
18312 else
18313 field_size_in_bits = type_size_in_bits;
18315 decl_align_in_bits = simple_decl_align_in_bits (decl);
18317 /* The GCC front-end doesn't make any attempt to keep track of the
18318 starting bit offset (relative to the start of the containing
18319 structure type) of the hypothetical "containing object" for a
18320 bit-field. Thus, when computing the byte offset value for the
18321 start of the "containing object" of a bit-field, we must deduce
18322 this information on our own. This can be rather tricky to do in
18323 some cases. For example, handling the following structure type
18324 definition when compiling for an i386/i486 target (which only
18325 aligns long long's to 32-bit boundaries) can be very tricky:
18327 struct S { int field1; long long field2:31; };
18329 Fortunately, there is a simple rule-of-thumb which can be used
18330 in such cases. When compiling for an i386/i486, GCC will
18331 allocate 8 bytes for the structure shown above. It decides to
18332 do this based upon one simple rule for bit-field allocation.
18333 GCC allocates each "containing object" for each bit-field at
18334 the first (i.e. lowest addressed) legitimate alignment boundary
18335 (based upon the required minimum alignment for the declared
18336 type of the field) which it can possibly use, subject to the
18337 condition that there is still enough available space remaining
18338 in the containing object (when allocated at the selected point)
18339 to fully accommodate all of the bits of the bit-field itself.
18341 This simple rule makes it obvious why GCC allocates 8 bytes for
18342 each object of the structure type shown above. When looking
18343 for a place to allocate the "containing object" for `field2',
18344 the compiler simply tries to allocate a 64-bit "containing
18345 object" at each successive 32-bit boundary (starting at zero)
18346 until it finds a place to allocate that 64- bit field such that
18347 at least 31 contiguous (and previously unallocated) bits remain
18348 within that selected 64 bit field. (As it turns out, for the
18349 example above, the compiler finds it is OK to allocate the
18350 "containing object" 64-bit field at bit-offset zero within the
18351 structure type.)
18353 Here we attempt to work backwards from the limited set of facts
18354 we're given, and we try to deduce from those facts, where GCC
18355 must have believed that the containing object started (within
18356 the structure type). The value we deduce is then used (by the
18357 callers of this routine) to generate DW_AT_location and
18358 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18359 the case of DW_AT_location, regular fields as well). */
18361 /* Figure out the bit-distance from the start of the structure to
18362 the "deepest" bit of the bit-field. */
18363 deepest_bitpos = bitpos_int + field_size_in_bits;
18365 /* This is the tricky part. Use some fancy footwork to deduce
18366 where the lowest addressed bit of the containing object must
18367 be. */
18368 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18370 /* Round up to type_align by default. This works best for
18371 bitfields. */
18372 object_offset_in_bits
18373 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18375 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18377 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18379 /* Round up to decl_align instead. */
18380 object_offset_in_bits
18381 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18384 object_offset_in_bytes
18385 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18386 if (ctx->variant_part_offset == NULL_TREE)
18388 *cst_offset = object_offset_in_bytes.to_shwi ();
18389 return NULL;
18391 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18393 else
18394 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18395 tree_result = byte_position (decl);
18397 if (ctx->variant_part_offset != NULL_TREE)
18398 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18399 ctx->variant_part_offset, tree_result);
18401 /* If the byte offset is a constant, it's simplier to handle a native
18402 constant rather than a DWARF expression. */
18403 if (TREE_CODE (tree_result) == INTEGER_CST)
18405 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18406 return NULL;
18408 struct loc_descr_context loc_ctx = {
18409 ctx->struct_type, /* context_type */
18410 NULL_TREE, /* base_decl */
18411 NULL, /* dpi */
18412 false, /* placeholder_arg */
18413 false /* placeholder_seen */
18415 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18417 /* We want a DWARF expression: abort if we only have a location list with
18418 multiple elements. */
18419 if (!loc_result || !single_element_loc_list_p (loc_result))
18420 return NULL;
18421 else
18422 return loc_result->expr;
18425 /* The following routines define various Dwarf attributes and any data
18426 associated with them. */
18428 /* Add a location description attribute value to a DIE.
18430 This emits location attributes suitable for whole variables and
18431 whole parameters. Note that the location attributes for struct fields are
18432 generated by the routine `data_member_location_attribute' below. */
18434 static inline void
18435 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18436 dw_loc_list_ref descr)
18438 if (descr == 0)
18439 return;
18440 if (single_element_loc_list_p (descr))
18441 add_AT_loc (die, attr_kind, descr->expr);
18442 else
18443 add_AT_loc_list (die, attr_kind, descr);
18446 /* Add DW_AT_accessibility attribute to DIE if needed. */
18448 static void
18449 add_accessibility_attribute (dw_die_ref die, tree decl)
18451 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18452 children, otherwise the default is DW_ACCESS_public. In DWARF2
18453 the default has always been DW_ACCESS_public. */
18454 if (TREE_PROTECTED (decl))
18455 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18456 else if (TREE_PRIVATE (decl))
18458 if (dwarf_version == 2
18459 || die->die_parent == NULL
18460 || die->die_parent->die_tag != DW_TAG_class_type)
18461 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18463 else if (dwarf_version > 2
18464 && die->die_parent
18465 && die->die_parent->die_tag == DW_TAG_class_type)
18466 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18469 /* Attach the specialized form of location attribute used for data members of
18470 struct and union types. In the special case of a FIELD_DECL node which
18471 represents a bit-field, the "offset" part of this special location
18472 descriptor must indicate the distance in bytes from the lowest-addressed
18473 byte of the containing struct or union type to the lowest-addressed byte of
18474 the "containing object" for the bit-field. (See the `field_byte_offset'
18475 function above).
18477 For any given bit-field, the "containing object" is a hypothetical object
18478 (of some integral or enum type) within which the given bit-field lives. The
18479 type of this hypothetical "containing object" is always the same as the
18480 declared type of the individual bit-field itself (for GCC anyway... the
18481 DWARF spec doesn't actually mandate this). Note that it is the size (in
18482 bytes) of the hypothetical "containing object" which will be given in the
18483 DW_AT_byte_size attribute for this bit-field. (See the
18484 `byte_size_attribute' function below.) It is also used when calculating the
18485 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18486 function below.)
18488 CTX is required: see the comment for VLR_CONTEXT. */
18490 static void
18491 add_data_member_location_attribute (dw_die_ref die,
18492 tree decl,
18493 struct vlr_context *ctx)
18495 HOST_WIDE_INT offset;
18496 dw_loc_descr_ref loc_descr = 0;
18498 if (TREE_CODE (decl) == TREE_BINFO)
18500 /* We're working on the TAG_inheritance for a base class. */
18501 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18503 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18504 aren't at a fixed offset from all (sub)objects of the same
18505 type. We need to extract the appropriate offset from our
18506 vtable. The following dwarf expression means
18508 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18510 This is specific to the V3 ABI, of course. */
18512 dw_loc_descr_ref tmp;
18514 /* Make a copy of the object address. */
18515 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18516 add_loc_descr (&loc_descr, tmp);
18518 /* Extract the vtable address. */
18519 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18520 add_loc_descr (&loc_descr, tmp);
18522 /* Calculate the address of the offset. */
18523 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18524 gcc_assert (offset < 0);
18526 tmp = int_loc_descriptor (-offset);
18527 add_loc_descr (&loc_descr, tmp);
18528 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18529 add_loc_descr (&loc_descr, tmp);
18531 /* Extract the offset. */
18532 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18533 add_loc_descr (&loc_descr, tmp);
18535 /* Add it to the object address. */
18536 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18537 add_loc_descr (&loc_descr, tmp);
18539 else
18540 offset = tree_to_shwi (BINFO_OFFSET (decl));
18542 else
18544 loc_descr = field_byte_offset (decl, ctx, &offset);
18546 /* If loc_descr is available then we know the field offset is dynamic.
18547 However, GDB does not handle dynamic field offsets very well at the
18548 moment. */
18549 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18551 loc_descr = NULL;
18552 offset = 0;
18555 /* Data member location evalutation starts with the base address on the
18556 stack. Compute the field offset and add it to this base address. */
18557 else if (loc_descr != NULL)
18558 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18561 if (! loc_descr)
18563 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18564 e.g. GDB only added support to it in November 2016. For DWARF5
18565 we need newer debug info consumers anyway. We might change this
18566 to dwarf_version >= 4 once most consumers catched up. */
18567 if (dwarf_version >= 5
18568 && TREE_CODE (decl) == FIELD_DECL
18569 && DECL_BIT_FIELD_TYPE (decl))
18571 tree off = bit_position (decl);
18572 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18574 remove_AT (die, DW_AT_byte_size);
18575 remove_AT (die, DW_AT_bit_offset);
18576 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18577 return;
18580 if (dwarf_version > 2)
18582 /* Don't need to output a location expression, just the constant. */
18583 if (offset < 0)
18584 add_AT_int (die, DW_AT_data_member_location, offset);
18585 else
18586 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18587 return;
18589 else
18591 enum dwarf_location_atom op;
18593 /* The DWARF2 standard says that we should assume that the structure
18594 address is already on the stack, so we can specify a structure
18595 field address by using DW_OP_plus_uconst. */
18596 op = DW_OP_plus_uconst;
18597 loc_descr = new_loc_descr (op, offset, 0);
18601 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18604 /* Writes integer values to dw_vec_const array. */
18606 static void
18607 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18609 while (size != 0)
18611 *dest++ = val & 0xff;
18612 val >>= 8;
18613 --size;
18617 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18619 static HOST_WIDE_INT
18620 extract_int (const unsigned char *src, unsigned int size)
18622 HOST_WIDE_INT val = 0;
18624 src += size;
18625 while (size != 0)
18627 val <<= 8;
18628 val |= *--src & 0xff;
18629 --size;
18631 return val;
18634 /* Writes wide_int values to dw_vec_const array. */
18636 static void
18637 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18639 int i;
18641 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18643 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18644 return;
18647 /* We'd have to extend this code to support odd sizes. */
18648 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18650 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18652 if (WORDS_BIG_ENDIAN)
18653 for (i = n - 1; i >= 0; i--)
18655 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18656 dest += sizeof (HOST_WIDE_INT);
18658 else
18659 for (i = 0; i < n; i++)
18661 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18662 dest += sizeof (HOST_WIDE_INT);
18666 /* Writes floating point values to dw_vec_const array. */
18668 static void
18669 insert_float (const_rtx rtl, unsigned char *array)
18671 long val[4];
18672 int i;
18673 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18675 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18677 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18678 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18680 insert_int (val[i], 4, array);
18681 array += 4;
18685 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18686 does not have a "location" either in memory or in a register. These
18687 things can arise in GNU C when a constant is passed as an actual parameter
18688 to an inlined function. They can also arise in C++ where declared
18689 constants do not necessarily get memory "homes". */
18691 static bool
18692 add_const_value_attribute (dw_die_ref die, rtx rtl)
18694 switch (GET_CODE (rtl))
18696 case CONST_INT:
18698 HOST_WIDE_INT val = INTVAL (rtl);
18700 if (val < 0)
18701 add_AT_int (die, DW_AT_const_value, val);
18702 else
18703 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18705 return true;
18707 case CONST_WIDE_INT:
18709 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18710 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18711 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18712 wide_int w = wi::zext (w1, prec);
18713 add_AT_wide (die, DW_AT_const_value, w);
18715 return true;
18717 case CONST_DOUBLE:
18718 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18719 floating-point constant. A CONST_DOUBLE is used whenever the
18720 constant requires more than one word in order to be adequately
18721 represented. */
18722 if (TARGET_SUPPORTS_WIDE_INT == 0
18723 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18724 add_AT_double (die, DW_AT_const_value,
18725 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18726 else
18728 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18729 unsigned int length = GET_MODE_SIZE (mode);
18730 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18732 insert_float (rtl, array);
18733 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18735 return true;
18737 case CONST_VECTOR:
18739 machine_mode mode = GET_MODE (rtl);
18740 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18741 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18742 unsigned char *array
18743 = ggc_vec_alloc<unsigned char> (length * elt_size);
18744 unsigned int i;
18745 unsigned char *p;
18746 machine_mode imode = GET_MODE_INNER (mode);
18748 switch (GET_MODE_CLASS (mode))
18750 case MODE_VECTOR_INT:
18751 for (i = 0, p = array; i < length; i++, p += elt_size)
18753 rtx elt = CONST_VECTOR_ELT (rtl, i);
18754 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18756 break;
18758 case MODE_VECTOR_FLOAT:
18759 for (i = 0, p = array; i < length; i++, p += elt_size)
18761 rtx elt = CONST_VECTOR_ELT (rtl, i);
18762 insert_float (elt, p);
18764 break;
18766 default:
18767 gcc_unreachable ();
18770 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18772 return true;
18774 case CONST_STRING:
18775 if (dwarf_version >= 4 || !dwarf_strict)
18777 dw_loc_descr_ref loc_result;
18778 resolve_one_addr (&rtl);
18779 rtl_addr:
18780 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18781 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18782 add_AT_loc (die, DW_AT_location, loc_result);
18783 vec_safe_push (used_rtx_array, rtl);
18784 return true;
18786 return false;
18788 case CONST:
18789 if (CONSTANT_P (XEXP (rtl, 0)))
18790 return add_const_value_attribute (die, XEXP (rtl, 0));
18791 /* FALLTHROUGH */
18792 case SYMBOL_REF:
18793 if (!const_ok_for_output (rtl))
18794 return false;
18795 /* FALLTHROUGH */
18796 case LABEL_REF:
18797 if (dwarf_version >= 4 || !dwarf_strict)
18798 goto rtl_addr;
18799 return false;
18801 case PLUS:
18802 /* In cases where an inlined instance of an inline function is passed
18803 the address of an `auto' variable (which is local to the caller) we
18804 can get a situation where the DECL_RTL of the artificial local
18805 variable (for the inlining) which acts as a stand-in for the
18806 corresponding formal parameter (of the inline function) will look
18807 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18808 exactly a compile-time constant expression, but it isn't the address
18809 of the (artificial) local variable either. Rather, it represents the
18810 *value* which the artificial local variable always has during its
18811 lifetime. We currently have no way to represent such quasi-constant
18812 values in Dwarf, so for now we just punt and generate nothing. */
18813 return false;
18815 case HIGH:
18816 case CONST_FIXED:
18817 return false;
18819 case MEM:
18820 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18821 && MEM_READONLY_P (rtl)
18822 && GET_MODE (rtl) == BLKmode)
18824 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18825 return true;
18827 return false;
18829 default:
18830 /* No other kinds of rtx should be possible here. */
18831 gcc_unreachable ();
18833 return false;
18836 /* Determine whether the evaluation of EXPR references any variables
18837 or functions which aren't otherwise used (and therefore may not be
18838 output). */
18839 static tree
18840 reference_to_unused (tree * tp, int * walk_subtrees,
18841 void * data ATTRIBUTE_UNUSED)
18843 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18844 *walk_subtrees = 0;
18846 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18847 && ! TREE_ASM_WRITTEN (*tp))
18848 return *tp;
18849 /* ??? The C++ FE emits debug information for using decls, so
18850 putting gcc_unreachable here falls over. See PR31899. For now
18851 be conservative. */
18852 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18853 return *tp;
18854 else if (VAR_P (*tp))
18856 varpool_node *node = varpool_node::get (*tp);
18857 if (!node || !node->definition)
18858 return *tp;
18860 else if (TREE_CODE (*tp) == FUNCTION_DECL
18861 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18863 /* The call graph machinery must have finished analyzing,
18864 optimizing and gimplifying the CU by now.
18865 So if *TP has no call graph node associated
18866 to it, it means *TP will not be emitted. */
18867 if (!cgraph_node::get (*tp))
18868 return *tp;
18870 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18871 return *tp;
18873 return NULL_TREE;
18876 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18877 for use in a later add_const_value_attribute call. */
18879 static rtx
18880 rtl_for_decl_init (tree init, tree type)
18882 rtx rtl = NULL_RTX;
18884 STRIP_NOPS (init);
18886 /* If a variable is initialized with a string constant without embedded
18887 zeros, build CONST_STRING. */
18888 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18890 tree enttype = TREE_TYPE (type);
18891 tree domain = TYPE_DOMAIN (type);
18892 scalar_int_mode mode;
18894 if (is_int_mode (TYPE_MODE (enttype), &mode)
18895 && GET_MODE_SIZE (mode) == 1
18896 && domain
18897 && integer_zerop (TYPE_MIN_VALUE (domain))
18898 && compare_tree_int (TYPE_MAX_VALUE (domain),
18899 TREE_STRING_LENGTH (init) - 1) == 0
18900 && ((size_t) TREE_STRING_LENGTH (init)
18901 == strlen (TREE_STRING_POINTER (init)) + 1))
18903 rtl = gen_rtx_CONST_STRING (VOIDmode,
18904 ggc_strdup (TREE_STRING_POINTER (init)));
18905 rtl = gen_rtx_MEM (BLKmode, rtl);
18906 MEM_READONLY_P (rtl) = 1;
18909 /* Other aggregates, and complex values, could be represented using
18910 CONCAT: FIXME! */
18911 else if (AGGREGATE_TYPE_P (type)
18912 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18913 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18914 || TREE_CODE (type) == COMPLEX_TYPE)
18916 /* Vectors only work if their mode is supported by the target.
18917 FIXME: generic vectors ought to work too. */
18918 else if (TREE_CODE (type) == VECTOR_TYPE
18919 && !VECTOR_MODE_P (TYPE_MODE (type)))
18921 /* If the initializer is something that we know will expand into an
18922 immediate RTL constant, expand it now. We must be careful not to
18923 reference variables which won't be output. */
18924 else if (initializer_constant_valid_p (init, type)
18925 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18927 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18928 possible. */
18929 if (TREE_CODE (type) == VECTOR_TYPE)
18930 switch (TREE_CODE (init))
18932 case VECTOR_CST:
18933 break;
18934 case CONSTRUCTOR:
18935 if (TREE_CONSTANT (init))
18937 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18938 bool constant_p = true;
18939 tree value;
18940 unsigned HOST_WIDE_INT ix;
18942 /* Even when ctor is constant, it might contain non-*_CST
18943 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18944 belong into VECTOR_CST nodes. */
18945 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18946 if (!CONSTANT_CLASS_P (value))
18948 constant_p = false;
18949 break;
18952 if (constant_p)
18954 init = build_vector_from_ctor (type, elts);
18955 break;
18958 /* FALLTHRU */
18960 default:
18961 return NULL;
18964 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18966 /* If expand_expr returns a MEM, it wasn't immediate. */
18967 gcc_assert (!rtl || !MEM_P (rtl));
18970 return rtl;
18973 /* Generate RTL for the variable DECL to represent its location. */
18975 static rtx
18976 rtl_for_decl_location (tree decl)
18978 rtx rtl;
18980 /* Here we have to decide where we are going to say the parameter "lives"
18981 (as far as the debugger is concerned). We only have a couple of
18982 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18984 DECL_RTL normally indicates where the parameter lives during most of the
18985 activation of the function. If optimization is enabled however, this
18986 could be either NULL or else a pseudo-reg. Both of those cases indicate
18987 that the parameter doesn't really live anywhere (as far as the code
18988 generation parts of GCC are concerned) during most of the function's
18989 activation. That will happen (for example) if the parameter is never
18990 referenced within the function.
18992 We could just generate a location descriptor here for all non-NULL
18993 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18994 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18995 where DECL_RTL is NULL or is a pseudo-reg.
18997 Note however that we can only get away with using DECL_INCOMING_RTL as
18998 a backup substitute for DECL_RTL in certain limited cases. In cases
18999 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19000 we can be sure that the parameter was passed using the same type as it is
19001 declared to have within the function, and that its DECL_INCOMING_RTL
19002 points us to a place where a value of that type is passed.
19004 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19005 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19006 because in these cases DECL_INCOMING_RTL points us to a value of some
19007 type which is *different* from the type of the parameter itself. Thus,
19008 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19009 such cases, the debugger would end up (for example) trying to fetch a
19010 `float' from a place which actually contains the first part of a
19011 `double'. That would lead to really incorrect and confusing
19012 output at debug-time.
19014 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19015 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19016 are a couple of exceptions however. On little-endian machines we can
19017 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19018 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19019 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19020 when (on a little-endian machine) a non-prototyped function has a
19021 parameter declared to be of type `short' or `char'. In such cases,
19022 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19023 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19024 passed `int' value. If the debugger then uses that address to fetch
19025 a `short' or a `char' (on a little-endian machine) the result will be
19026 the correct data, so we allow for such exceptional cases below.
19028 Note that our goal here is to describe the place where the given formal
19029 parameter lives during most of the function's activation (i.e. between the
19030 end of the prologue and the start of the epilogue). We'll do that as best
19031 as we can. Note however that if the given formal parameter is modified
19032 sometime during the execution of the function, then a stack backtrace (at
19033 debug-time) will show the function as having been called with the *new*
19034 value rather than the value which was originally passed in. This happens
19035 rarely enough that it is not a major problem, but it *is* a problem, and
19036 I'd like to fix it.
19038 A future version of dwarf2out.c may generate two additional attributes for
19039 any given DW_TAG_formal_parameter DIE which will describe the "passed
19040 type" and the "passed location" for the given formal parameter in addition
19041 to the attributes we now generate to indicate the "declared type" and the
19042 "active location" for each parameter. This additional set of attributes
19043 could be used by debuggers for stack backtraces. Separately, note that
19044 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19045 This happens (for example) for inlined-instances of inline function formal
19046 parameters which are never referenced. This really shouldn't be
19047 happening. All PARM_DECL nodes should get valid non-NULL
19048 DECL_INCOMING_RTL values. FIXME. */
19050 /* Use DECL_RTL as the "location" unless we find something better. */
19051 rtl = DECL_RTL_IF_SET (decl);
19053 /* When generating abstract instances, ignore everything except
19054 constants, symbols living in memory, and symbols living in
19055 fixed registers. */
19056 if (! reload_completed)
19058 if (rtl
19059 && (CONSTANT_P (rtl)
19060 || (MEM_P (rtl)
19061 && CONSTANT_P (XEXP (rtl, 0)))
19062 || (REG_P (rtl)
19063 && VAR_P (decl)
19064 && TREE_STATIC (decl))))
19066 rtl = targetm.delegitimize_address (rtl);
19067 return rtl;
19069 rtl = NULL_RTX;
19071 else if (TREE_CODE (decl) == PARM_DECL)
19073 if (rtl == NULL_RTX
19074 || is_pseudo_reg (rtl)
19075 || (MEM_P (rtl)
19076 && is_pseudo_reg (XEXP (rtl, 0))
19077 && DECL_INCOMING_RTL (decl)
19078 && MEM_P (DECL_INCOMING_RTL (decl))
19079 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19081 tree declared_type = TREE_TYPE (decl);
19082 tree passed_type = DECL_ARG_TYPE (decl);
19083 machine_mode dmode = TYPE_MODE (declared_type);
19084 machine_mode pmode = TYPE_MODE (passed_type);
19086 /* This decl represents a formal parameter which was optimized out.
19087 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19088 all cases where (rtl == NULL_RTX) just below. */
19089 if (dmode == pmode)
19090 rtl = DECL_INCOMING_RTL (decl);
19091 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19092 && SCALAR_INT_MODE_P (dmode)
19093 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19094 && DECL_INCOMING_RTL (decl))
19096 rtx inc = DECL_INCOMING_RTL (decl);
19097 if (REG_P (inc))
19098 rtl = inc;
19099 else if (MEM_P (inc))
19101 if (BYTES_BIG_ENDIAN)
19102 rtl = adjust_address_nv (inc, dmode,
19103 GET_MODE_SIZE (pmode)
19104 - GET_MODE_SIZE (dmode));
19105 else
19106 rtl = inc;
19111 /* If the parm was passed in registers, but lives on the stack, then
19112 make a big endian correction if the mode of the type of the
19113 parameter is not the same as the mode of the rtl. */
19114 /* ??? This is the same series of checks that are made in dbxout.c before
19115 we reach the big endian correction code there. It isn't clear if all
19116 of these checks are necessary here, but keeping them all is the safe
19117 thing to do. */
19118 else if (MEM_P (rtl)
19119 && XEXP (rtl, 0) != const0_rtx
19120 && ! CONSTANT_P (XEXP (rtl, 0))
19121 /* Not passed in memory. */
19122 && !MEM_P (DECL_INCOMING_RTL (decl))
19123 /* Not passed by invisible reference. */
19124 && (!REG_P (XEXP (rtl, 0))
19125 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19126 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19127 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19128 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19129 #endif
19131 /* Big endian correction check. */
19132 && BYTES_BIG_ENDIAN
19133 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19134 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19135 < UNITS_PER_WORD))
19137 machine_mode addr_mode = get_address_mode (rtl);
19138 int offset = (UNITS_PER_WORD
19139 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19141 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19142 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19145 else if (VAR_P (decl)
19146 && rtl
19147 && MEM_P (rtl)
19148 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19150 machine_mode addr_mode = get_address_mode (rtl);
19151 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19152 GET_MODE (rtl));
19154 /* If a variable is declared "register" yet is smaller than
19155 a register, then if we store the variable to memory, it
19156 looks like we're storing a register-sized value, when in
19157 fact we are not. We need to adjust the offset of the
19158 storage location to reflect the actual value's bytes,
19159 else gdb will not be able to display it. */
19160 if (offset != 0)
19161 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19162 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19165 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19166 and will have been substituted directly into all expressions that use it.
19167 C does not have such a concept, but C++ and other languages do. */
19168 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19169 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19171 if (rtl)
19172 rtl = targetm.delegitimize_address (rtl);
19174 /* If we don't look past the constant pool, we risk emitting a
19175 reference to a constant pool entry that isn't referenced from
19176 code, and thus is not emitted. */
19177 if (rtl)
19178 rtl = avoid_constant_pool_reference (rtl);
19180 /* Try harder to get a rtl. If this symbol ends up not being emitted
19181 in the current CU, resolve_addr will remove the expression referencing
19182 it. */
19183 if (rtl == NULL_RTX
19184 && VAR_P (decl)
19185 && !DECL_EXTERNAL (decl)
19186 && TREE_STATIC (decl)
19187 && DECL_NAME (decl)
19188 && !DECL_HARD_REGISTER (decl)
19189 && DECL_MODE (decl) != VOIDmode)
19191 rtl = make_decl_rtl_for_debug (decl);
19192 if (!MEM_P (rtl)
19193 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19194 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19195 rtl = NULL_RTX;
19198 return rtl;
19201 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19202 returned. If so, the decl for the COMMON block is returned, and the
19203 value is the offset into the common block for the symbol. */
19205 static tree
19206 fortran_common (tree decl, HOST_WIDE_INT *value)
19208 tree val_expr, cvar;
19209 machine_mode mode;
19210 HOST_WIDE_INT bitsize, bitpos;
19211 tree offset;
19212 int unsignedp, reversep, volatilep = 0;
19214 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19215 it does not have a value (the offset into the common area), or if it
19216 is thread local (as opposed to global) then it isn't common, and shouldn't
19217 be handled as such. */
19218 if (!VAR_P (decl)
19219 || !TREE_STATIC (decl)
19220 || !DECL_HAS_VALUE_EXPR_P (decl)
19221 || !is_fortran ())
19222 return NULL_TREE;
19224 val_expr = DECL_VALUE_EXPR (decl);
19225 if (TREE_CODE (val_expr) != COMPONENT_REF)
19226 return NULL_TREE;
19228 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19229 &unsignedp, &reversep, &volatilep);
19231 if (cvar == NULL_TREE
19232 || !VAR_P (cvar)
19233 || DECL_ARTIFICIAL (cvar)
19234 || !TREE_PUBLIC (cvar))
19235 return NULL_TREE;
19237 *value = 0;
19238 if (offset != NULL)
19240 if (!tree_fits_shwi_p (offset))
19241 return NULL_TREE;
19242 *value = tree_to_shwi (offset);
19244 if (bitpos != 0)
19245 *value += bitpos / BITS_PER_UNIT;
19247 return cvar;
19250 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19251 data attribute for a variable or a parameter. We generate the
19252 DW_AT_const_value attribute only in those cases where the given variable
19253 or parameter does not have a true "location" either in memory or in a
19254 register. This can happen (for example) when a constant is passed as an
19255 actual argument in a call to an inline function. (It's possible that
19256 these things can crop up in other ways also.) Note that one type of
19257 constant value which can be passed into an inlined function is a constant
19258 pointer. This can happen for example if an actual argument in an inlined
19259 function call evaluates to a compile-time constant address.
19261 CACHE_P is true if it is worth caching the location list for DECL,
19262 so that future calls can reuse it rather than regenerate it from scratch.
19263 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19264 since we will need to refer to them each time the function is inlined. */
19266 static bool
19267 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19269 rtx rtl;
19270 dw_loc_list_ref list;
19271 var_loc_list *loc_list;
19272 cached_dw_loc_list *cache;
19274 if (early_dwarf)
19275 return false;
19277 if (TREE_CODE (decl) == ERROR_MARK)
19278 return false;
19280 if (get_AT (die, DW_AT_location)
19281 || get_AT (die, DW_AT_const_value))
19282 return true;
19284 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19285 || TREE_CODE (decl) == RESULT_DECL);
19287 /* Try to get some constant RTL for this decl, and use that as the value of
19288 the location. */
19290 rtl = rtl_for_decl_location (decl);
19291 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19292 && add_const_value_attribute (die, rtl))
19293 return true;
19295 /* See if we have single element location list that is equivalent to
19296 a constant value. That way we are better to use add_const_value_attribute
19297 rather than expanding constant value equivalent. */
19298 loc_list = lookup_decl_loc (decl);
19299 if (loc_list
19300 && loc_list->first
19301 && loc_list->first->next == NULL
19302 && NOTE_P (loc_list->first->loc)
19303 && NOTE_VAR_LOCATION (loc_list->first->loc)
19304 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19306 struct var_loc_node *node;
19308 node = loc_list->first;
19309 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19310 if (GET_CODE (rtl) == EXPR_LIST)
19311 rtl = XEXP (rtl, 0);
19312 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19313 && add_const_value_attribute (die, rtl))
19314 return true;
19316 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19317 list several times. See if we've already cached the contents. */
19318 list = NULL;
19319 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19320 cache_p = false;
19321 if (cache_p)
19323 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19324 if (cache)
19325 list = cache->loc_list;
19327 if (list == NULL)
19329 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19330 NULL);
19331 /* It is usually worth caching this result if the decl is from
19332 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19333 if (cache_p && list && list->dw_loc_next)
19335 cached_dw_loc_list **slot
19336 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19337 DECL_UID (decl),
19338 INSERT);
19339 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19340 cache->decl_id = DECL_UID (decl);
19341 cache->loc_list = list;
19342 *slot = cache;
19345 if (list)
19347 add_AT_location_description (die, DW_AT_location, list);
19348 return true;
19350 /* None of that worked, so it must not really have a location;
19351 try adding a constant value attribute from the DECL_INITIAL. */
19352 return tree_add_const_value_attribute_for_decl (die, decl);
19355 /* Helper function for tree_add_const_value_attribute. Natively encode
19356 initializer INIT into an array. Return true if successful. */
19358 static bool
19359 native_encode_initializer (tree init, unsigned char *array, int size)
19361 tree type;
19363 if (init == NULL_TREE)
19364 return false;
19366 STRIP_NOPS (init);
19367 switch (TREE_CODE (init))
19369 case STRING_CST:
19370 type = TREE_TYPE (init);
19371 if (TREE_CODE (type) == ARRAY_TYPE)
19373 tree enttype = TREE_TYPE (type);
19374 scalar_int_mode mode;
19376 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19377 || GET_MODE_SIZE (mode) != 1)
19378 return false;
19379 if (int_size_in_bytes (type) != size)
19380 return false;
19381 if (size > TREE_STRING_LENGTH (init))
19383 memcpy (array, TREE_STRING_POINTER (init),
19384 TREE_STRING_LENGTH (init));
19385 memset (array + TREE_STRING_LENGTH (init),
19386 '\0', size - TREE_STRING_LENGTH (init));
19388 else
19389 memcpy (array, TREE_STRING_POINTER (init), size);
19390 return true;
19392 return false;
19393 case CONSTRUCTOR:
19394 type = TREE_TYPE (init);
19395 if (int_size_in_bytes (type) != size)
19396 return false;
19397 if (TREE_CODE (type) == ARRAY_TYPE)
19399 HOST_WIDE_INT min_index;
19400 unsigned HOST_WIDE_INT cnt;
19401 int curpos = 0, fieldsize;
19402 constructor_elt *ce;
19404 if (TYPE_DOMAIN (type) == NULL_TREE
19405 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19406 return false;
19408 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19409 if (fieldsize <= 0)
19410 return false;
19412 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19413 memset (array, '\0', size);
19414 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19416 tree val = ce->value;
19417 tree index = ce->index;
19418 int pos = curpos;
19419 if (index && TREE_CODE (index) == RANGE_EXPR)
19420 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19421 * fieldsize;
19422 else if (index)
19423 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19425 if (val)
19427 STRIP_NOPS (val);
19428 if (!native_encode_initializer (val, array + pos, fieldsize))
19429 return false;
19431 curpos = pos + fieldsize;
19432 if (index && TREE_CODE (index) == RANGE_EXPR)
19434 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19435 - tree_to_shwi (TREE_OPERAND (index, 0));
19436 while (count-- > 0)
19438 if (val)
19439 memcpy (array + curpos, array + pos, fieldsize);
19440 curpos += fieldsize;
19443 gcc_assert (curpos <= size);
19445 return true;
19447 else if (TREE_CODE (type) == RECORD_TYPE
19448 || TREE_CODE (type) == UNION_TYPE)
19450 tree field = NULL_TREE;
19451 unsigned HOST_WIDE_INT cnt;
19452 constructor_elt *ce;
19454 if (int_size_in_bytes (type) != size)
19455 return false;
19457 if (TREE_CODE (type) == RECORD_TYPE)
19458 field = TYPE_FIELDS (type);
19460 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19462 tree val = ce->value;
19463 int pos, fieldsize;
19465 if (ce->index != 0)
19466 field = ce->index;
19468 if (val)
19469 STRIP_NOPS (val);
19471 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19472 return false;
19474 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19475 && TYPE_DOMAIN (TREE_TYPE (field))
19476 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19477 return false;
19478 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19479 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19480 return false;
19481 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19482 pos = int_byte_position (field);
19483 gcc_assert (pos + fieldsize <= size);
19484 if (val && fieldsize != 0
19485 && !native_encode_initializer (val, array + pos, fieldsize))
19486 return false;
19488 return true;
19490 return false;
19491 case VIEW_CONVERT_EXPR:
19492 case NON_LVALUE_EXPR:
19493 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19494 default:
19495 return native_encode_expr (init, array, size) == size;
19499 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19500 attribute is the const value T. */
19502 static bool
19503 tree_add_const_value_attribute (dw_die_ref die, tree t)
19505 tree init;
19506 tree type = TREE_TYPE (t);
19507 rtx rtl;
19509 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19510 return false;
19512 init = t;
19513 gcc_assert (!DECL_P (init));
19515 if (TREE_CODE (init) == INTEGER_CST)
19517 if (tree_fits_uhwi_p (init))
19519 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19520 return true;
19522 if (tree_fits_shwi_p (init))
19524 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19525 return true;
19528 if (! early_dwarf)
19530 rtl = rtl_for_decl_init (init, type);
19531 if (rtl)
19532 return add_const_value_attribute (die, rtl);
19534 /* If the host and target are sane, try harder. */
19535 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19536 && initializer_constant_valid_p (init, type))
19538 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19539 if (size > 0 && (int) size == size)
19541 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19543 if (native_encode_initializer (init, array, size))
19545 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19546 return true;
19548 ggc_free (array);
19551 return false;
19554 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19555 attribute is the const value of T, where T is an integral constant
19556 variable with static storage duration
19557 (so it can't be a PARM_DECL or a RESULT_DECL). */
19559 static bool
19560 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19563 if (!decl
19564 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19565 || (VAR_P (decl) && !TREE_STATIC (decl)))
19566 return false;
19568 if (TREE_READONLY (decl)
19569 && ! TREE_THIS_VOLATILE (decl)
19570 && DECL_INITIAL (decl))
19571 /* OK */;
19572 else
19573 return false;
19575 /* Don't add DW_AT_const_value if abstract origin already has one. */
19576 if (get_AT (var_die, DW_AT_const_value))
19577 return false;
19579 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19582 /* Convert the CFI instructions for the current function into a
19583 location list. This is used for DW_AT_frame_base when we targeting
19584 a dwarf2 consumer that does not support the dwarf3
19585 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19586 expressions. */
19588 static dw_loc_list_ref
19589 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19591 int ix;
19592 dw_fde_ref fde;
19593 dw_loc_list_ref list, *list_tail;
19594 dw_cfi_ref cfi;
19595 dw_cfa_location last_cfa, next_cfa;
19596 const char *start_label, *last_label, *section;
19597 dw_cfa_location remember;
19599 fde = cfun->fde;
19600 gcc_assert (fde != NULL);
19602 section = secname_for_decl (current_function_decl);
19603 list_tail = &list;
19604 list = NULL;
19606 memset (&next_cfa, 0, sizeof (next_cfa));
19607 next_cfa.reg = INVALID_REGNUM;
19608 remember = next_cfa;
19610 start_label = fde->dw_fde_begin;
19612 /* ??? Bald assumption that the CIE opcode list does not contain
19613 advance opcodes. */
19614 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19615 lookup_cfa_1 (cfi, &next_cfa, &remember);
19617 last_cfa = next_cfa;
19618 last_label = start_label;
19620 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19622 /* If the first partition contained no CFI adjustments, the
19623 CIE opcodes apply to the whole first partition. */
19624 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19625 fde->dw_fde_begin, fde->dw_fde_end, section);
19626 list_tail =&(*list_tail)->dw_loc_next;
19627 start_label = last_label = fde->dw_fde_second_begin;
19630 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19632 switch (cfi->dw_cfi_opc)
19634 case DW_CFA_set_loc:
19635 case DW_CFA_advance_loc1:
19636 case DW_CFA_advance_loc2:
19637 case DW_CFA_advance_loc4:
19638 if (!cfa_equal_p (&last_cfa, &next_cfa))
19640 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19641 start_label, last_label, section);
19643 list_tail = &(*list_tail)->dw_loc_next;
19644 last_cfa = next_cfa;
19645 start_label = last_label;
19647 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19648 break;
19650 case DW_CFA_advance_loc:
19651 /* The encoding is complex enough that we should never emit this. */
19652 gcc_unreachable ();
19654 default:
19655 lookup_cfa_1 (cfi, &next_cfa, &remember);
19656 break;
19658 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19660 if (!cfa_equal_p (&last_cfa, &next_cfa))
19662 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19663 start_label, last_label, section);
19665 list_tail = &(*list_tail)->dw_loc_next;
19666 last_cfa = next_cfa;
19667 start_label = last_label;
19669 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19670 start_label, fde->dw_fde_end, section);
19671 list_tail = &(*list_tail)->dw_loc_next;
19672 start_label = last_label = fde->dw_fde_second_begin;
19676 if (!cfa_equal_p (&last_cfa, &next_cfa))
19678 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19679 start_label, last_label, section);
19680 list_tail = &(*list_tail)->dw_loc_next;
19681 start_label = last_label;
19684 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19685 start_label,
19686 fde->dw_fde_second_begin
19687 ? fde->dw_fde_second_end : fde->dw_fde_end,
19688 section);
19690 if (list && list->dw_loc_next)
19691 gen_llsym (list);
19693 return list;
19696 /* Compute a displacement from the "steady-state frame pointer" to the
19697 frame base (often the same as the CFA), and store it in
19698 frame_pointer_fb_offset. OFFSET is added to the displacement
19699 before the latter is negated. */
19701 static void
19702 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19704 rtx reg, elim;
19706 #ifdef FRAME_POINTER_CFA_OFFSET
19707 reg = frame_pointer_rtx;
19708 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19709 #else
19710 reg = arg_pointer_rtx;
19711 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19712 #endif
19714 elim = (ira_use_lra_p
19715 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19716 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19717 if (GET_CODE (elim) == PLUS)
19719 offset += INTVAL (XEXP (elim, 1));
19720 elim = XEXP (elim, 0);
19723 frame_pointer_fb_offset = -offset;
19725 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19726 in which to eliminate. This is because it's stack pointer isn't
19727 directly accessible as a register within the ISA. To work around
19728 this, assume that while we cannot provide a proper value for
19729 frame_pointer_fb_offset, we won't need one either. */
19730 frame_pointer_fb_offset_valid
19731 = ((SUPPORTS_STACK_ALIGNMENT
19732 && (elim == hard_frame_pointer_rtx
19733 || elim == stack_pointer_rtx))
19734 || elim == (frame_pointer_needed
19735 ? hard_frame_pointer_rtx
19736 : stack_pointer_rtx));
19739 /* Generate a DW_AT_name attribute given some string value to be included as
19740 the value of the attribute. */
19742 static void
19743 add_name_attribute (dw_die_ref die, const char *name_string)
19745 if (name_string != NULL && *name_string != 0)
19747 if (demangle_name_func)
19748 name_string = (*demangle_name_func) (name_string);
19750 add_AT_string (die, DW_AT_name, name_string);
19754 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19755 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19756 of TYPE accordingly.
19758 ??? This is a temporary measure until after we're able to generate
19759 regular DWARF for the complex Ada type system. */
19761 static void
19762 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19763 dw_die_ref context_die)
19765 tree dtype;
19766 dw_die_ref dtype_die;
19768 if (!lang_hooks.types.descriptive_type)
19769 return;
19771 dtype = lang_hooks.types.descriptive_type (type);
19772 if (!dtype)
19773 return;
19775 dtype_die = lookup_type_die (dtype);
19776 if (!dtype_die)
19778 gen_type_die (dtype, context_die);
19779 dtype_die = lookup_type_die (dtype);
19780 gcc_assert (dtype_die);
19783 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19786 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19788 static const char *
19789 comp_dir_string (void)
19791 const char *wd;
19792 char *wd1;
19793 static const char *cached_wd = NULL;
19795 if (cached_wd != NULL)
19796 return cached_wd;
19798 wd = get_src_pwd ();
19799 if (wd == NULL)
19800 return NULL;
19802 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19804 int wdlen;
19806 wdlen = strlen (wd);
19807 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19808 strcpy (wd1, wd);
19809 wd1 [wdlen] = DIR_SEPARATOR;
19810 wd1 [wdlen + 1] = 0;
19811 wd = wd1;
19814 cached_wd = remap_debug_filename (wd);
19815 return cached_wd;
19818 /* Generate a DW_AT_comp_dir attribute for DIE. */
19820 static void
19821 add_comp_dir_attribute (dw_die_ref die)
19823 const char * wd = comp_dir_string ();
19824 if (wd != NULL)
19825 add_AT_string (die, DW_AT_comp_dir, wd);
19828 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19829 pointer computation, ...), output a representation for that bound according
19830 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19831 loc_list_from_tree for the meaning of CONTEXT. */
19833 static void
19834 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19835 int forms, struct loc_descr_context *context)
19837 dw_die_ref context_die, decl_die;
19838 dw_loc_list_ref list;
19839 bool strip_conversions = true;
19840 bool placeholder_seen = false;
19842 while (strip_conversions)
19843 switch (TREE_CODE (value))
19845 case ERROR_MARK:
19846 case SAVE_EXPR:
19847 return;
19849 CASE_CONVERT:
19850 case VIEW_CONVERT_EXPR:
19851 value = TREE_OPERAND (value, 0);
19852 break;
19854 default:
19855 strip_conversions = false;
19856 break;
19859 /* If possible and permitted, output the attribute as a constant. */
19860 if ((forms & dw_scalar_form_constant) != 0
19861 && TREE_CODE (value) == INTEGER_CST)
19863 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19865 /* If HOST_WIDE_INT is big enough then represent the bound as
19866 a constant value. We need to choose a form based on
19867 whether the type is signed or unsigned. We cannot just
19868 call add_AT_unsigned if the value itself is positive
19869 (add_AT_unsigned might add the unsigned value encoded as
19870 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19871 bounds type and then sign extend any unsigned values found
19872 for signed types. This is needed only for
19873 DW_AT_{lower,upper}_bound, since for most other attributes,
19874 consumers will treat DW_FORM_data[1248] as unsigned values,
19875 regardless of the underlying type. */
19876 if (prec <= HOST_BITS_PER_WIDE_INT
19877 || tree_fits_uhwi_p (value))
19879 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19880 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19881 else
19882 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19884 else
19885 /* Otherwise represent the bound as an unsigned value with
19886 the precision of its type. The precision and signedness
19887 of the type will be necessary to re-interpret it
19888 unambiguously. */
19889 add_AT_wide (die, attr, wi::to_wide (value));
19890 return;
19893 /* Otherwise, if it's possible and permitted too, output a reference to
19894 another DIE. */
19895 if ((forms & dw_scalar_form_reference) != 0)
19897 tree decl = NULL_TREE;
19899 /* Some type attributes reference an outer type. For instance, the upper
19900 bound of an array may reference an embedding record (this happens in
19901 Ada). */
19902 if (TREE_CODE (value) == COMPONENT_REF
19903 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19904 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19905 decl = TREE_OPERAND (value, 1);
19907 else if (VAR_P (value)
19908 || TREE_CODE (value) == PARM_DECL
19909 || TREE_CODE (value) == RESULT_DECL)
19910 decl = value;
19912 if (decl != NULL_TREE)
19914 dw_die_ref decl_die = lookup_decl_die (decl);
19916 /* ??? Can this happen, or should the variable have been bound
19917 first? Probably it can, since I imagine that we try to create
19918 the types of parameters in the order in which they exist in
19919 the list, and won't have created a forward reference to a
19920 later parameter. */
19921 if (decl_die != NULL)
19923 add_AT_die_ref (die, attr, decl_die);
19924 return;
19929 /* Last chance: try to create a stack operation procedure to evaluate the
19930 value. Do nothing if even that is not possible or permitted. */
19931 if ((forms & dw_scalar_form_exprloc) == 0)
19932 return;
19934 list = loc_list_from_tree (value, 2, context);
19935 if (context && context->placeholder_arg)
19937 placeholder_seen = context->placeholder_seen;
19938 context->placeholder_seen = false;
19940 if (list == NULL || single_element_loc_list_p (list))
19942 /* If this attribute is not a reference nor constant, it is
19943 a DWARF expression rather than location description. For that
19944 loc_list_from_tree (value, 0, &context) is needed. */
19945 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19946 if (list2 && single_element_loc_list_p (list2))
19948 if (placeholder_seen)
19950 struct dwarf_procedure_info dpi;
19951 dpi.fndecl = NULL_TREE;
19952 dpi.args_count = 1;
19953 if (!resolve_args_picking (list2->expr, 1, &dpi))
19954 return;
19956 add_AT_loc (die, attr, list2->expr);
19957 return;
19961 /* If that failed to give a single element location list, fall back to
19962 outputting this as a reference... still if permitted. */
19963 if (list == NULL
19964 || (forms & dw_scalar_form_reference) == 0
19965 || placeholder_seen)
19966 return;
19968 if (current_function_decl == 0)
19969 context_die = comp_unit_die ();
19970 else
19971 context_die = lookup_decl_die (current_function_decl);
19973 decl_die = new_die (DW_TAG_variable, context_die, value);
19974 add_AT_flag (decl_die, DW_AT_artificial, 1);
19975 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19976 context_die);
19977 add_AT_location_description (decl_die, DW_AT_location, list);
19978 add_AT_die_ref (die, attr, decl_die);
19981 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19982 default. */
19984 static int
19985 lower_bound_default (void)
19987 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19989 case DW_LANG_C:
19990 case DW_LANG_C89:
19991 case DW_LANG_C99:
19992 case DW_LANG_C11:
19993 case DW_LANG_C_plus_plus:
19994 case DW_LANG_C_plus_plus_11:
19995 case DW_LANG_C_plus_plus_14:
19996 case DW_LANG_ObjC:
19997 case DW_LANG_ObjC_plus_plus:
19998 return 0;
19999 case DW_LANG_Fortran77:
20000 case DW_LANG_Fortran90:
20001 case DW_LANG_Fortran95:
20002 case DW_LANG_Fortran03:
20003 case DW_LANG_Fortran08:
20004 return 1;
20005 case DW_LANG_UPC:
20006 case DW_LANG_D:
20007 case DW_LANG_Python:
20008 return dwarf_version >= 4 ? 0 : -1;
20009 case DW_LANG_Ada95:
20010 case DW_LANG_Ada83:
20011 case DW_LANG_Cobol74:
20012 case DW_LANG_Cobol85:
20013 case DW_LANG_Modula2:
20014 case DW_LANG_PLI:
20015 return dwarf_version >= 4 ? 1 : -1;
20016 default:
20017 return -1;
20021 /* Given a tree node describing an array bound (either lower or upper) output
20022 a representation for that bound. */
20024 static void
20025 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20026 tree bound, struct loc_descr_context *context)
20028 int dflt;
20030 while (1)
20031 switch (TREE_CODE (bound))
20033 /* Strip all conversions. */
20034 CASE_CONVERT:
20035 case VIEW_CONVERT_EXPR:
20036 bound = TREE_OPERAND (bound, 0);
20037 break;
20039 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20040 are even omitted when they are the default. */
20041 case INTEGER_CST:
20042 /* If the value for this bound is the default one, we can even omit the
20043 attribute. */
20044 if (bound_attr == DW_AT_lower_bound
20045 && tree_fits_shwi_p (bound)
20046 && (dflt = lower_bound_default ()) != -1
20047 && tree_to_shwi (bound) == dflt)
20048 return;
20050 /* FALLTHRU */
20052 default:
20053 /* Because of the complex interaction there can be with other GNAT
20054 encodings, GDB isn't ready yet to handle proper DWARF description
20055 for self-referencial subrange bounds: let GNAT encodings do the
20056 magic in such a case. */
20057 if (is_ada ()
20058 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20059 && contains_placeholder_p (bound))
20060 return;
20062 add_scalar_info (subrange_die, bound_attr, bound,
20063 dw_scalar_form_constant
20064 | dw_scalar_form_exprloc
20065 | dw_scalar_form_reference,
20066 context);
20067 return;
20071 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20072 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20073 Note that the block of subscript information for an array type also
20074 includes information about the element type of the given array type.
20076 This function reuses previously set type and bound information if
20077 available. */
20079 static void
20080 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20082 unsigned dimension_number;
20083 tree lower, upper;
20084 dw_die_ref child = type_die->die_child;
20086 for (dimension_number = 0;
20087 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20088 type = TREE_TYPE (type), dimension_number++)
20090 tree domain = TYPE_DOMAIN (type);
20092 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20093 break;
20095 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20096 and (in GNU C only) variable bounds. Handle all three forms
20097 here. */
20099 /* Find and reuse a previously generated DW_TAG_subrange_type if
20100 available.
20102 For multi-dimensional arrays, as we iterate through the
20103 various dimensions in the enclosing for loop above, we also
20104 iterate through the DIE children and pick at each
20105 DW_TAG_subrange_type previously generated (if available).
20106 Each child DW_TAG_subrange_type DIE describes the range of
20107 the current dimension. At this point we should have as many
20108 DW_TAG_subrange_type's as we have dimensions in the
20109 array. */
20110 dw_die_ref subrange_die = NULL;
20111 if (child)
20112 while (1)
20114 child = child->die_sib;
20115 if (child->die_tag == DW_TAG_subrange_type)
20116 subrange_die = child;
20117 if (child == type_die->die_child)
20119 /* If we wrapped around, stop looking next time. */
20120 child = NULL;
20121 break;
20123 if (child->die_tag == DW_TAG_subrange_type)
20124 break;
20126 if (!subrange_die)
20127 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20129 if (domain)
20131 /* We have an array type with specified bounds. */
20132 lower = TYPE_MIN_VALUE (domain);
20133 upper = TYPE_MAX_VALUE (domain);
20135 /* Define the index type. */
20136 if (TREE_TYPE (domain)
20137 && !get_AT (subrange_die, DW_AT_type))
20139 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20140 TREE_TYPE field. We can't emit debug info for this
20141 because it is an unnamed integral type. */
20142 if (TREE_CODE (domain) == INTEGER_TYPE
20143 && TYPE_NAME (domain) == NULL_TREE
20144 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20145 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20147 else
20148 add_type_attribute (subrange_die, TREE_TYPE (domain),
20149 TYPE_UNQUALIFIED, false, type_die);
20152 /* ??? If upper is NULL, the array has unspecified length,
20153 but it does have a lower bound. This happens with Fortran
20154 dimension arr(N:*)
20155 Since the debugger is definitely going to need to know N
20156 to produce useful results, go ahead and output the lower
20157 bound solo, and hope the debugger can cope. */
20159 if (!get_AT (subrange_die, DW_AT_lower_bound))
20160 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20161 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20162 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20165 /* Otherwise we have an array type with an unspecified length. The
20166 DWARF-2 spec does not say how to handle this; let's just leave out the
20167 bounds. */
20171 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20173 static void
20174 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20176 dw_die_ref decl_die;
20177 HOST_WIDE_INT size;
20178 dw_loc_descr_ref size_expr = NULL;
20180 switch (TREE_CODE (tree_node))
20182 case ERROR_MARK:
20183 size = 0;
20184 break;
20185 case ENUMERAL_TYPE:
20186 case RECORD_TYPE:
20187 case UNION_TYPE:
20188 case QUAL_UNION_TYPE:
20189 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20190 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20192 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20193 return;
20195 size_expr = type_byte_size (tree_node, &size);
20196 break;
20197 case FIELD_DECL:
20198 /* For a data member of a struct or union, the DW_AT_byte_size is
20199 generally given as the number of bytes normally allocated for an
20200 object of the *declared* type of the member itself. This is true
20201 even for bit-fields. */
20202 size = int_size_in_bytes (field_type (tree_node));
20203 break;
20204 default:
20205 gcc_unreachable ();
20208 /* Support for dynamically-sized objects was introduced by DWARFv3.
20209 At the moment, GDB does not handle variable byte sizes very well,
20210 though. */
20211 if ((dwarf_version >= 3 || !dwarf_strict)
20212 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20213 && size_expr != NULL)
20214 add_AT_loc (die, DW_AT_byte_size, size_expr);
20216 /* Note that `size' might be -1 when we get to this point. If it is, that
20217 indicates that the byte size of the entity in question is variable and
20218 that we could not generate a DWARF expression that computes it. */
20219 if (size >= 0)
20220 add_AT_unsigned (die, DW_AT_byte_size, size);
20223 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20224 alignment. */
20226 static void
20227 add_alignment_attribute (dw_die_ref die, tree tree_node)
20229 if (dwarf_version < 5 && dwarf_strict)
20230 return;
20232 unsigned align;
20234 if (DECL_P (tree_node))
20236 if (!DECL_USER_ALIGN (tree_node))
20237 return;
20239 align = DECL_ALIGN_UNIT (tree_node);
20241 else if (TYPE_P (tree_node))
20243 if (!TYPE_USER_ALIGN (tree_node))
20244 return;
20246 align = TYPE_ALIGN_UNIT (tree_node);
20248 else
20249 gcc_unreachable ();
20251 add_AT_unsigned (die, DW_AT_alignment, align);
20254 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20255 which specifies the distance in bits from the highest order bit of the
20256 "containing object" for the bit-field to the highest order bit of the
20257 bit-field itself.
20259 For any given bit-field, the "containing object" is a hypothetical object
20260 (of some integral or enum type) within which the given bit-field lives. The
20261 type of this hypothetical "containing object" is always the same as the
20262 declared type of the individual bit-field itself. The determination of the
20263 exact location of the "containing object" for a bit-field is rather
20264 complicated. It's handled by the `field_byte_offset' function (above).
20266 CTX is required: see the comment for VLR_CONTEXT.
20268 Note that it is the size (in bytes) of the hypothetical "containing object"
20269 which will be given in the DW_AT_byte_size attribute for this bit-field.
20270 (See `byte_size_attribute' above). */
20272 static inline void
20273 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20275 HOST_WIDE_INT object_offset_in_bytes;
20276 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20277 HOST_WIDE_INT bitpos_int;
20278 HOST_WIDE_INT highest_order_object_bit_offset;
20279 HOST_WIDE_INT highest_order_field_bit_offset;
20280 HOST_WIDE_INT bit_offset;
20282 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20284 /* Must be a field and a bit field. */
20285 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20287 /* We can't yet handle bit-fields whose offsets are variable, so if we
20288 encounter such things, just return without generating any attribute
20289 whatsoever. Likewise for variable or too large size. */
20290 if (! tree_fits_shwi_p (bit_position (decl))
20291 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20292 return;
20294 bitpos_int = int_bit_position (decl);
20296 /* Note that the bit offset is always the distance (in bits) from the
20297 highest-order bit of the "containing object" to the highest-order bit of
20298 the bit-field itself. Since the "high-order end" of any object or field
20299 is different on big-endian and little-endian machines, the computation
20300 below must take account of these differences. */
20301 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20302 highest_order_field_bit_offset = bitpos_int;
20304 if (! BYTES_BIG_ENDIAN)
20306 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20307 highest_order_object_bit_offset +=
20308 simple_type_size_in_bits (original_type);
20311 bit_offset
20312 = (! BYTES_BIG_ENDIAN
20313 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20314 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20316 if (bit_offset < 0)
20317 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20318 else
20319 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20322 /* For a FIELD_DECL node which represents a bit field, output an attribute
20323 which specifies the length in bits of the given field. */
20325 static inline void
20326 add_bit_size_attribute (dw_die_ref die, tree decl)
20328 /* Must be a field and a bit field. */
20329 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20330 && DECL_BIT_FIELD_TYPE (decl));
20332 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20333 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20336 /* If the compiled language is ANSI C, then add a 'prototyped'
20337 attribute, if arg types are given for the parameters of a function. */
20339 static inline void
20340 add_prototyped_attribute (dw_die_ref die, tree func_type)
20342 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20344 case DW_LANG_C:
20345 case DW_LANG_C89:
20346 case DW_LANG_C99:
20347 case DW_LANG_C11:
20348 case DW_LANG_ObjC:
20349 if (prototype_p (func_type))
20350 add_AT_flag (die, DW_AT_prototyped, 1);
20351 break;
20352 default:
20353 break;
20357 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20358 by looking in the type declaration, the object declaration equate table or
20359 the block mapping. */
20361 static inline dw_die_ref
20362 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20364 dw_die_ref origin_die = NULL;
20366 if (DECL_P (origin))
20368 dw_die_ref c;
20369 origin_die = lookup_decl_die (origin);
20370 /* "Unwrap" the decls DIE which we put in the imported unit context.
20371 We are looking for the abstract copy here. */
20372 if (in_lto_p
20373 && origin_die
20374 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20375 /* ??? Identify this better. */
20376 && c->with_offset)
20377 origin_die = c;
20379 else if (TYPE_P (origin))
20380 origin_die = lookup_type_die (origin);
20381 else if (TREE_CODE (origin) == BLOCK)
20382 origin_die = BLOCK_DIE (origin);
20384 /* XXX: Functions that are never lowered don't always have correct block
20385 trees (in the case of java, they simply have no block tree, in some other
20386 languages). For these functions, there is nothing we can really do to
20387 output correct debug info for inlined functions in all cases. Rather
20388 than die, we'll just produce deficient debug info now, in that we will
20389 have variables without a proper abstract origin. In the future, when all
20390 functions are lowered, we should re-add a gcc_assert (origin_die)
20391 here. */
20393 if (origin_die)
20394 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20395 return origin_die;
20398 /* We do not currently support the pure_virtual attribute. */
20400 static inline void
20401 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20403 if (DECL_VINDEX (func_decl))
20405 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20407 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20408 add_AT_loc (die, DW_AT_vtable_elem_location,
20409 new_loc_descr (DW_OP_constu,
20410 tree_to_shwi (DECL_VINDEX (func_decl)),
20411 0));
20413 /* GNU extension: Record what type this method came from originally. */
20414 if (debug_info_level > DINFO_LEVEL_TERSE
20415 && DECL_CONTEXT (func_decl))
20416 add_AT_die_ref (die, DW_AT_containing_type,
20417 lookup_type_die (DECL_CONTEXT (func_decl)));
20421 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20422 given decl. This used to be a vendor extension until after DWARF 4
20423 standardized it. */
20425 static void
20426 add_linkage_attr (dw_die_ref die, tree decl)
20428 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20430 /* Mimic what assemble_name_raw does with a leading '*'. */
20431 if (name[0] == '*')
20432 name = &name[1];
20434 if (dwarf_version >= 4)
20435 add_AT_string (die, DW_AT_linkage_name, name);
20436 else
20437 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20440 /* Add source coordinate attributes for the given decl. */
20442 static void
20443 add_src_coords_attributes (dw_die_ref die, tree decl)
20445 expanded_location s;
20447 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20448 return;
20449 s = expand_location (DECL_SOURCE_LOCATION (decl));
20450 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20451 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20452 if (debug_column_info && s.column)
20453 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20456 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20458 static void
20459 add_linkage_name_raw (dw_die_ref die, tree decl)
20461 /* Defer until we have an assembler name set. */
20462 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20464 limbo_die_node *asm_name;
20466 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20467 asm_name->die = die;
20468 asm_name->created_for = decl;
20469 asm_name->next = deferred_asm_name;
20470 deferred_asm_name = asm_name;
20472 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20473 add_linkage_attr (die, decl);
20476 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20478 static void
20479 add_linkage_name (dw_die_ref die, tree decl)
20481 if (debug_info_level > DINFO_LEVEL_NONE
20482 && VAR_OR_FUNCTION_DECL_P (decl)
20483 && TREE_PUBLIC (decl)
20484 && !(VAR_P (decl) && DECL_REGISTER (decl))
20485 && die->die_tag != DW_TAG_member)
20486 add_linkage_name_raw (die, decl);
20489 /* Add a DW_AT_name attribute and source coordinate attribute for the
20490 given decl, but only if it actually has a name. */
20492 static void
20493 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20494 bool no_linkage_name)
20496 tree decl_name;
20498 decl_name = DECL_NAME (decl);
20499 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20501 const char *name = dwarf2_name (decl, 0);
20502 if (name)
20503 add_name_attribute (die, name);
20504 if (! DECL_ARTIFICIAL (decl))
20505 add_src_coords_attributes (die, decl);
20507 if (!no_linkage_name)
20508 add_linkage_name (die, decl);
20511 #ifdef VMS_DEBUGGING_INFO
20512 /* Get the function's name, as described by its RTL. This may be different
20513 from the DECL_NAME name used in the source file. */
20514 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20516 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20517 XEXP (DECL_RTL (decl), 0), false);
20518 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20520 #endif /* VMS_DEBUGGING_INFO */
20523 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20525 static void
20526 add_discr_value (dw_die_ref die, dw_discr_value *value)
20528 dw_attr_node attr;
20530 attr.dw_attr = DW_AT_discr_value;
20531 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20532 attr.dw_attr_val.val_entry = NULL;
20533 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20534 if (value->pos)
20535 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20536 else
20537 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20538 add_dwarf_attr (die, &attr);
20541 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20543 static void
20544 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20546 dw_attr_node attr;
20548 attr.dw_attr = DW_AT_discr_list;
20549 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20550 attr.dw_attr_val.val_entry = NULL;
20551 attr.dw_attr_val.v.val_discr_list = discr_list;
20552 add_dwarf_attr (die, &attr);
20555 static inline dw_discr_list_ref
20556 AT_discr_list (dw_attr_node *attr)
20558 return attr->dw_attr_val.v.val_discr_list;
20561 #ifdef VMS_DEBUGGING_INFO
20562 /* Output the debug main pointer die for VMS */
20564 void
20565 dwarf2out_vms_debug_main_pointer (void)
20567 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20568 dw_die_ref die;
20570 /* Allocate the VMS debug main subprogram die. */
20571 die = new_die_raw (DW_TAG_subprogram);
20572 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20573 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20574 current_function_funcdef_no);
20575 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20577 /* Make it the first child of comp_unit_die (). */
20578 die->die_parent = comp_unit_die ();
20579 if (comp_unit_die ()->die_child)
20581 die->die_sib = comp_unit_die ()->die_child->die_sib;
20582 comp_unit_die ()->die_child->die_sib = die;
20584 else
20586 die->die_sib = die;
20587 comp_unit_die ()->die_child = die;
20590 #endif /* VMS_DEBUGGING_INFO */
20592 /* Push a new declaration scope. */
20594 static void
20595 push_decl_scope (tree scope)
20597 vec_safe_push (decl_scope_table, scope);
20600 /* Pop a declaration scope. */
20602 static inline void
20603 pop_decl_scope (void)
20605 decl_scope_table->pop ();
20608 /* walk_tree helper function for uses_local_type, below. */
20610 static tree
20611 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20613 if (!TYPE_P (*tp))
20614 *walk_subtrees = 0;
20615 else
20617 tree name = TYPE_NAME (*tp);
20618 if (name && DECL_P (name) && decl_function_context (name))
20619 return *tp;
20621 return NULL_TREE;
20624 /* If TYPE involves a function-local type (including a local typedef to a
20625 non-local type), returns that type; otherwise returns NULL_TREE. */
20627 static tree
20628 uses_local_type (tree type)
20630 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20631 return used;
20634 /* Return the DIE for the scope that immediately contains this type.
20635 Non-named types that do not involve a function-local type get global
20636 scope. Named types nested in namespaces or other types get their
20637 containing scope. All other types (i.e. function-local named types) get
20638 the current active scope. */
20640 static dw_die_ref
20641 scope_die_for (tree t, dw_die_ref context_die)
20643 dw_die_ref scope_die = NULL;
20644 tree containing_scope;
20646 /* Non-types always go in the current scope. */
20647 gcc_assert (TYPE_P (t));
20649 /* Use the scope of the typedef, rather than the scope of the type
20650 it refers to. */
20651 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20652 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20653 else
20654 containing_scope = TYPE_CONTEXT (t);
20656 /* Use the containing namespace if there is one. */
20657 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20659 if (context_die == lookup_decl_die (containing_scope))
20660 /* OK */;
20661 else if (debug_info_level > DINFO_LEVEL_TERSE)
20662 context_die = get_context_die (containing_scope);
20663 else
20664 containing_scope = NULL_TREE;
20667 /* Ignore function type "scopes" from the C frontend. They mean that
20668 a tagged type is local to a parmlist of a function declarator, but
20669 that isn't useful to DWARF. */
20670 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20671 containing_scope = NULL_TREE;
20673 if (SCOPE_FILE_SCOPE_P (containing_scope))
20675 /* If T uses a local type keep it local as well, to avoid references
20676 to function-local DIEs from outside the function. */
20677 if (current_function_decl && uses_local_type (t))
20678 scope_die = context_die;
20679 else
20680 scope_die = comp_unit_die ();
20682 else if (TYPE_P (containing_scope))
20684 /* For types, we can just look up the appropriate DIE. */
20685 if (debug_info_level > DINFO_LEVEL_TERSE)
20686 scope_die = get_context_die (containing_scope);
20687 else
20689 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20690 if (scope_die == NULL)
20691 scope_die = comp_unit_die ();
20694 else
20695 scope_die = context_die;
20697 return scope_die;
20700 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20702 static inline int
20703 local_scope_p (dw_die_ref context_die)
20705 for (; context_die; context_die = context_die->die_parent)
20706 if (context_die->die_tag == DW_TAG_inlined_subroutine
20707 || context_die->die_tag == DW_TAG_subprogram)
20708 return 1;
20710 return 0;
20713 /* Returns nonzero if CONTEXT_DIE is a class. */
20715 static inline int
20716 class_scope_p (dw_die_ref context_die)
20718 return (context_die
20719 && (context_die->die_tag == DW_TAG_structure_type
20720 || context_die->die_tag == DW_TAG_class_type
20721 || context_die->die_tag == DW_TAG_interface_type
20722 || context_die->die_tag == DW_TAG_union_type));
20725 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20726 whether or not to treat a DIE in this context as a declaration. */
20728 static inline int
20729 class_or_namespace_scope_p (dw_die_ref context_die)
20731 return (class_scope_p (context_die)
20732 || (context_die && context_die->die_tag == DW_TAG_namespace));
20735 /* Many forms of DIEs require a "type description" attribute. This
20736 routine locates the proper "type descriptor" die for the type given
20737 by 'type' plus any additional qualifiers given by 'cv_quals', and
20738 adds a DW_AT_type attribute below the given die. */
20740 static void
20741 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20742 bool reverse, dw_die_ref context_die)
20744 enum tree_code code = TREE_CODE (type);
20745 dw_die_ref type_die = NULL;
20747 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20748 or fixed-point type, use the inner type. This is because we have no
20749 support for unnamed types in base_type_die. This can happen if this is
20750 an Ada subrange type. Correct solution is emit a subrange type die. */
20751 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20752 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20753 type = TREE_TYPE (type), code = TREE_CODE (type);
20755 if (code == ERROR_MARK
20756 /* Handle a special case. For functions whose return type is void, we
20757 generate *no* type attribute. (Note that no object may have type
20758 `void', so this only applies to function return types). */
20759 || code == VOID_TYPE)
20760 return;
20762 type_die = modified_type_die (type,
20763 cv_quals | TYPE_QUALS (type),
20764 reverse,
20765 context_die);
20767 if (type_die != NULL)
20768 add_AT_die_ref (object_die, DW_AT_type, type_die);
20771 /* Given an object die, add the calling convention attribute for the
20772 function call type. */
20773 static void
20774 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20776 enum dwarf_calling_convention value = DW_CC_normal;
20778 value = ((enum dwarf_calling_convention)
20779 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20781 if (is_fortran ()
20782 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20784 /* DWARF 2 doesn't provide a way to identify a program's source-level
20785 entry point. DW_AT_calling_convention attributes are only meant
20786 to describe functions' calling conventions. However, lacking a
20787 better way to signal the Fortran main program, we used this for
20788 a long time, following existing custom. Now, DWARF 4 has
20789 DW_AT_main_subprogram, which we add below, but some tools still
20790 rely on the old way, which we thus keep. */
20791 value = DW_CC_program;
20793 if (dwarf_version >= 4 || !dwarf_strict)
20794 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20797 /* Only add the attribute if the backend requests it, and
20798 is not DW_CC_normal. */
20799 if (value && (value != DW_CC_normal))
20800 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20803 /* Given a tree pointer to a struct, class, union, or enum type node, return
20804 a pointer to the (string) tag name for the given type, or zero if the type
20805 was declared without a tag. */
20807 static const char *
20808 type_tag (const_tree type)
20810 const char *name = 0;
20812 if (TYPE_NAME (type) != 0)
20814 tree t = 0;
20816 /* Find the IDENTIFIER_NODE for the type name. */
20817 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20818 && !TYPE_NAMELESS (type))
20819 t = TYPE_NAME (type);
20821 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20822 a TYPE_DECL node, regardless of whether or not a `typedef' was
20823 involved. */
20824 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20825 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20827 /* We want to be extra verbose. Don't call dwarf_name if
20828 DECL_NAME isn't set. The default hook for decl_printable_name
20829 doesn't like that, and in this context it's correct to return
20830 0, instead of "<anonymous>" or the like. */
20831 if (DECL_NAME (TYPE_NAME (type))
20832 && !DECL_NAMELESS (TYPE_NAME (type)))
20833 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20836 /* Now get the name as a string, or invent one. */
20837 if (!name && t != 0)
20838 name = IDENTIFIER_POINTER (t);
20841 return (name == 0 || *name == '\0') ? 0 : name;
20844 /* Return the type associated with a data member, make a special check
20845 for bit field types. */
20847 static inline tree
20848 member_declared_type (const_tree member)
20850 return (DECL_BIT_FIELD_TYPE (member)
20851 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20854 /* Get the decl's label, as described by its RTL. This may be different
20855 from the DECL_NAME name used in the source file. */
20857 #if 0
20858 static const char *
20859 decl_start_label (tree decl)
20861 rtx x;
20862 const char *fnname;
20864 x = DECL_RTL (decl);
20865 gcc_assert (MEM_P (x));
20867 x = XEXP (x, 0);
20868 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20870 fnname = XSTR (x, 0);
20871 return fnname;
20873 #endif
20875 /* For variable-length arrays that have been previously generated, but
20876 may be incomplete due to missing subscript info, fill the subscript
20877 info. Return TRUE if this is one of those cases. */
20878 static bool
20879 fill_variable_array_bounds (tree type)
20881 if (TREE_ASM_WRITTEN (type)
20882 && TREE_CODE (type) == ARRAY_TYPE
20883 && variably_modified_type_p (type, NULL))
20885 dw_die_ref array_die = lookup_type_die (type);
20886 if (!array_die)
20887 return false;
20888 add_subscript_info (array_die, type, !is_ada ());
20889 return true;
20891 return false;
20894 /* These routines generate the internal representation of the DIE's for
20895 the compilation unit. Debugging information is collected by walking
20896 the declaration trees passed in from dwarf2out_decl(). */
20898 static void
20899 gen_array_type_die (tree type, dw_die_ref context_die)
20901 dw_die_ref array_die;
20903 /* GNU compilers represent multidimensional array types as sequences of one
20904 dimensional array types whose element types are themselves array types.
20905 We sometimes squish that down to a single array_type DIE with multiple
20906 subscripts in the Dwarf debugging info. The draft Dwarf specification
20907 say that we are allowed to do this kind of compression in C, because
20908 there is no difference between an array of arrays and a multidimensional
20909 array. We don't do this for Ada to remain as close as possible to the
20910 actual representation, which is especially important against the language
20911 flexibilty wrt arrays of variable size. */
20913 bool collapse_nested_arrays = !is_ada ();
20915 if (fill_variable_array_bounds (type))
20916 return;
20918 dw_die_ref scope_die = scope_die_for (type, context_die);
20919 tree element_type;
20921 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20922 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20923 if (TYPE_STRING_FLAG (type)
20924 && TREE_CODE (type) == ARRAY_TYPE
20925 && is_fortran ()
20926 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20928 HOST_WIDE_INT size;
20930 array_die = new_die (DW_TAG_string_type, scope_die, type);
20931 add_name_attribute (array_die, type_tag (type));
20932 equate_type_number_to_die (type, array_die);
20933 size = int_size_in_bytes (type);
20934 if (size >= 0)
20935 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20936 /* ??? We can't annotate types late, but for LTO we may not
20937 generate a location early either (gfortran.dg/save_6.f90). */
20938 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20939 && TYPE_DOMAIN (type) != NULL_TREE
20940 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20942 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20943 tree rszdecl = szdecl;
20945 size = int_size_in_bytes (TREE_TYPE (szdecl));
20946 if (!DECL_P (szdecl))
20948 if (TREE_CODE (szdecl) == INDIRECT_REF
20949 && DECL_P (TREE_OPERAND (szdecl, 0)))
20951 rszdecl = TREE_OPERAND (szdecl, 0);
20952 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20953 != DWARF2_ADDR_SIZE)
20954 size = 0;
20956 else
20957 size = 0;
20959 if (size > 0)
20961 dw_loc_list_ref loc
20962 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20963 NULL);
20964 if (loc)
20966 add_AT_location_description (array_die, DW_AT_string_length,
20967 loc);
20968 if (size != DWARF2_ADDR_SIZE)
20969 add_AT_unsigned (array_die, dwarf_version >= 5
20970 ? DW_AT_string_length_byte_size
20971 : DW_AT_byte_size, size);
20975 return;
20978 array_die = new_die (DW_TAG_array_type, scope_die, type);
20979 add_name_attribute (array_die, type_tag (type));
20980 equate_type_number_to_die (type, array_die);
20982 if (TREE_CODE (type) == VECTOR_TYPE)
20983 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20985 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20986 if (is_fortran ()
20987 && TREE_CODE (type) == ARRAY_TYPE
20988 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20989 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20990 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20992 #if 0
20993 /* We default the array ordering. Debuggers will probably do the right
20994 things even if DW_AT_ordering is not present. It's not even an issue
20995 until we start to get into multidimensional arrays anyway. If a debugger
20996 is ever caught doing the Wrong Thing for multi-dimensional arrays,
20997 then we'll have to put the DW_AT_ordering attribute back in. (But if
20998 and when we find out that we need to put these in, we will only do so
20999 for multidimensional arrays. */
21000 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21001 #endif
21003 if (TREE_CODE (type) == VECTOR_TYPE)
21005 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21006 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21007 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21008 add_bound_info (subrange_die, DW_AT_upper_bound,
21009 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21011 else
21012 add_subscript_info (array_die, type, collapse_nested_arrays);
21014 /* Add representation of the type of the elements of this array type and
21015 emit the corresponding DIE if we haven't done it already. */
21016 element_type = TREE_TYPE (type);
21017 if (collapse_nested_arrays)
21018 while (TREE_CODE (element_type) == ARRAY_TYPE)
21020 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21021 break;
21022 element_type = TREE_TYPE (element_type);
21025 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21026 TREE_CODE (type) == ARRAY_TYPE
21027 && TYPE_REVERSE_STORAGE_ORDER (type),
21028 context_die);
21030 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21031 if (TYPE_ARTIFICIAL (type))
21032 add_AT_flag (array_die, DW_AT_artificial, 1);
21034 if (get_AT (array_die, DW_AT_name))
21035 add_pubtype (type, array_die);
21037 add_alignment_attribute (array_die, type);
21040 /* This routine generates DIE for array with hidden descriptor, details
21041 are filled into *info by a langhook. */
21043 static void
21044 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21045 dw_die_ref context_die)
21047 const dw_die_ref scope_die = scope_die_for (type, context_die);
21048 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21049 struct loc_descr_context context = { type, info->base_decl, NULL,
21050 false, false };
21051 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21052 int dim;
21054 add_name_attribute (array_die, type_tag (type));
21055 equate_type_number_to_die (type, array_die);
21057 if (info->ndimensions > 1)
21058 switch (info->ordering)
21060 case array_descr_ordering_row_major:
21061 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21062 break;
21063 case array_descr_ordering_column_major:
21064 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21065 break;
21066 default:
21067 break;
21070 if (dwarf_version >= 3 || !dwarf_strict)
21072 if (info->data_location)
21073 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21074 dw_scalar_form_exprloc, &context);
21075 if (info->associated)
21076 add_scalar_info (array_die, DW_AT_associated, info->associated,
21077 dw_scalar_form_constant
21078 | dw_scalar_form_exprloc
21079 | dw_scalar_form_reference, &context);
21080 if (info->allocated)
21081 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21082 dw_scalar_form_constant
21083 | dw_scalar_form_exprloc
21084 | dw_scalar_form_reference, &context);
21085 if (info->stride)
21087 const enum dwarf_attribute attr
21088 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21089 const int forms
21090 = (info->stride_in_bits)
21091 ? dw_scalar_form_constant
21092 : (dw_scalar_form_constant
21093 | dw_scalar_form_exprloc
21094 | dw_scalar_form_reference);
21096 add_scalar_info (array_die, attr, info->stride, forms, &context);
21099 if (dwarf_version >= 5)
21101 if (info->rank)
21103 add_scalar_info (array_die, DW_AT_rank, info->rank,
21104 dw_scalar_form_constant
21105 | dw_scalar_form_exprloc, &context);
21106 subrange_tag = DW_TAG_generic_subrange;
21107 context.placeholder_arg = true;
21111 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21113 for (dim = 0; dim < info->ndimensions; dim++)
21115 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21117 if (info->dimen[dim].bounds_type)
21118 add_type_attribute (subrange_die,
21119 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21120 false, context_die);
21121 if (info->dimen[dim].lower_bound)
21122 add_bound_info (subrange_die, DW_AT_lower_bound,
21123 info->dimen[dim].lower_bound, &context);
21124 if (info->dimen[dim].upper_bound)
21125 add_bound_info (subrange_die, DW_AT_upper_bound,
21126 info->dimen[dim].upper_bound, &context);
21127 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21128 add_scalar_info (subrange_die, DW_AT_byte_stride,
21129 info->dimen[dim].stride,
21130 dw_scalar_form_constant
21131 | dw_scalar_form_exprloc
21132 | dw_scalar_form_reference,
21133 &context);
21136 gen_type_die (info->element_type, context_die);
21137 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21138 TREE_CODE (type) == ARRAY_TYPE
21139 && TYPE_REVERSE_STORAGE_ORDER (type),
21140 context_die);
21142 if (get_AT (array_die, DW_AT_name))
21143 add_pubtype (type, array_die);
21145 add_alignment_attribute (array_die, type);
21148 #if 0
21149 static void
21150 gen_entry_point_die (tree decl, dw_die_ref context_die)
21152 tree origin = decl_ultimate_origin (decl);
21153 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21155 if (origin != NULL)
21156 add_abstract_origin_attribute (decl_die, origin);
21157 else
21159 add_name_and_src_coords_attributes (decl_die, decl);
21160 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21161 TYPE_UNQUALIFIED, false, context_die);
21164 if (DECL_ABSTRACT_P (decl))
21165 equate_decl_number_to_die (decl, decl_die);
21166 else
21167 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21169 #endif
21171 /* Walk through the list of incomplete types again, trying once more to
21172 emit full debugging info for them. */
21174 static void
21175 retry_incomplete_types (void)
21177 set_early_dwarf s;
21178 int i;
21180 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21181 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21182 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21183 vec_safe_truncate (incomplete_types, 0);
21186 /* Determine what tag to use for a record type. */
21188 static enum dwarf_tag
21189 record_type_tag (tree type)
21191 if (! lang_hooks.types.classify_record)
21192 return DW_TAG_structure_type;
21194 switch (lang_hooks.types.classify_record (type))
21196 case RECORD_IS_STRUCT:
21197 return DW_TAG_structure_type;
21199 case RECORD_IS_CLASS:
21200 return DW_TAG_class_type;
21202 case RECORD_IS_INTERFACE:
21203 if (dwarf_version >= 3 || !dwarf_strict)
21204 return DW_TAG_interface_type;
21205 return DW_TAG_structure_type;
21207 default:
21208 gcc_unreachable ();
21212 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21213 include all of the information about the enumeration values also. Each
21214 enumerated type name/value is listed as a child of the enumerated type
21215 DIE. */
21217 static dw_die_ref
21218 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21220 dw_die_ref type_die = lookup_type_die (type);
21222 if (type_die == NULL)
21224 type_die = new_die (DW_TAG_enumeration_type,
21225 scope_die_for (type, context_die), type);
21226 equate_type_number_to_die (type, type_die);
21227 add_name_attribute (type_die, type_tag (type));
21228 if (dwarf_version >= 4 || !dwarf_strict)
21230 if (ENUM_IS_SCOPED (type))
21231 add_AT_flag (type_die, DW_AT_enum_class, 1);
21232 if (ENUM_IS_OPAQUE (type))
21233 add_AT_flag (type_die, DW_AT_declaration, 1);
21235 if (!dwarf_strict)
21236 add_AT_unsigned (type_die, DW_AT_encoding,
21237 TYPE_UNSIGNED (type)
21238 ? DW_ATE_unsigned
21239 : DW_ATE_signed);
21241 else if (! TYPE_SIZE (type))
21242 return type_die;
21243 else
21244 remove_AT (type_die, DW_AT_declaration);
21246 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21247 given enum type is incomplete, do not generate the DW_AT_byte_size
21248 attribute or the DW_AT_element_list attribute. */
21249 if (TYPE_SIZE (type))
21251 tree link;
21253 TREE_ASM_WRITTEN (type) = 1;
21254 add_byte_size_attribute (type_die, type);
21255 add_alignment_attribute (type_die, type);
21256 if (dwarf_version >= 3 || !dwarf_strict)
21258 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21259 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21260 context_die);
21262 if (TYPE_STUB_DECL (type) != NULL_TREE)
21264 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21265 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21268 /* If the first reference to this type was as the return type of an
21269 inline function, then it may not have a parent. Fix this now. */
21270 if (type_die->die_parent == NULL)
21271 add_child_die (scope_die_for (type, context_die), type_die);
21273 for (link = TYPE_VALUES (type);
21274 link != NULL; link = TREE_CHAIN (link))
21276 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21277 tree value = TREE_VALUE (link);
21279 add_name_attribute (enum_die,
21280 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21282 if (TREE_CODE (value) == CONST_DECL)
21283 value = DECL_INITIAL (value);
21285 if (simple_type_size_in_bits (TREE_TYPE (value))
21286 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21288 /* For constant forms created by add_AT_unsigned DWARF
21289 consumers (GDB, elfutils, etc.) always zero extend
21290 the value. Only when the actual value is negative
21291 do we need to use add_AT_int to generate a constant
21292 form that can represent negative values. */
21293 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21294 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21295 add_AT_unsigned (enum_die, DW_AT_const_value,
21296 (unsigned HOST_WIDE_INT) val);
21297 else
21298 add_AT_int (enum_die, DW_AT_const_value, val);
21300 else
21301 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21302 that here. TODO: This should be re-worked to use correct
21303 signed/unsigned double tags for all cases. */
21304 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21307 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21308 if (TYPE_ARTIFICIAL (type))
21309 add_AT_flag (type_die, DW_AT_artificial, 1);
21311 else
21312 add_AT_flag (type_die, DW_AT_declaration, 1);
21314 add_pubtype (type, type_die);
21316 return type_die;
21319 /* Generate a DIE to represent either a real live formal parameter decl or to
21320 represent just the type of some formal parameter position in some function
21321 type.
21323 Note that this routine is a bit unusual because its argument may be a
21324 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21325 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21326 node. If it's the former then this function is being called to output a
21327 DIE to represent a formal parameter object (or some inlining thereof). If
21328 it's the latter, then this function is only being called to output a
21329 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21330 argument type of some subprogram type.
21331 If EMIT_NAME_P is true, name and source coordinate attributes
21332 are emitted. */
21334 static dw_die_ref
21335 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21336 dw_die_ref context_die)
21338 tree node_or_origin = node ? node : origin;
21339 tree ultimate_origin;
21340 dw_die_ref parm_die = NULL;
21342 if (DECL_P (node_or_origin))
21344 parm_die = lookup_decl_die (node);
21346 /* If the contexts differ, we may not be talking about the same
21347 thing.
21348 ??? When in LTO the DIE parent is the "abstract" copy and the
21349 context_die is the specification "copy". But this whole block
21350 should eventually be no longer needed. */
21351 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21353 if (!DECL_ABSTRACT_P (node))
21355 /* This can happen when creating an inlined instance, in
21356 which case we need to create a new DIE that will get
21357 annotated with DW_AT_abstract_origin. */
21358 parm_die = NULL;
21360 else
21361 gcc_unreachable ();
21364 if (parm_die && parm_die->die_parent == NULL)
21366 /* Check that parm_die already has the right attributes that
21367 we would have added below. If any attributes are
21368 missing, fall through to add them. */
21369 if (! DECL_ABSTRACT_P (node_or_origin)
21370 && !get_AT (parm_die, DW_AT_location)
21371 && !get_AT (parm_die, DW_AT_const_value))
21372 /* We are missing location info, and are about to add it. */
21374 else
21376 add_child_die (context_die, parm_die);
21377 return parm_die;
21382 /* If we have a previously generated DIE, use it, unless this is an
21383 concrete instance (origin != NULL), in which case we need a new
21384 DIE with a corresponding DW_AT_abstract_origin. */
21385 bool reusing_die;
21386 if (parm_die && origin == NULL)
21387 reusing_die = true;
21388 else
21390 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21391 reusing_die = false;
21394 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21396 case tcc_declaration:
21397 ultimate_origin = decl_ultimate_origin (node_or_origin);
21398 if (node || ultimate_origin)
21399 origin = ultimate_origin;
21401 if (reusing_die)
21402 goto add_location;
21404 if (origin != NULL)
21405 add_abstract_origin_attribute (parm_die, origin);
21406 else if (emit_name_p)
21407 add_name_and_src_coords_attributes (parm_die, node);
21408 if (origin == NULL
21409 || (! DECL_ABSTRACT_P (node_or_origin)
21410 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21411 decl_function_context
21412 (node_or_origin))))
21414 tree type = TREE_TYPE (node_or_origin);
21415 if (decl_by_reference_p (node_or_origin))
21416 add_type_attribute (parm_die, TREE_TYPE (type),
21417 TYPE_UNQUALIFIED,
21418 false, context_die);
21419 else
21420 add_type_attribute (parm_die, type,
21421 decl_quals (node_or_origin),
21422 false, context_die);
21424 if (origin == NULL && DECL_ARTIFICIAL (node))
21425 add_AT_flag (parm_die, DW_AT_artificial, 1);
21426 add_location:
21427 if (node && node != origin)
21428 equate_decl_number_to_die (node, parm_die);
21429 if (! DECL_ABSTRACT_P (node_or_origin))
21430 add_location_or_const_value_attribute (parm_die, node_or_origin,
21431 node == NULL);
21433 break;
21435 case tcc_type:
21436 /* We were called with some kind of a ..._TYPE node. */
21437 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21438 context_die);
21439 break;
21441 default:
21442 gcc_unreachable ();
21445 return parm_die;
21448 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21449 children DW_TAG_formal_parameter DIEs representing the arguments of the
21450 parameter pack.
21452 PARM_PACK must be a function parameter pack.
21453 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21454 must point to the subsequent arguments of the function PACK_ARG belongs to.
21455 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21456 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21457 following the last one for which a DIE was generated. */
21459 static dw_die_ref
21460 gen_formal_parameter_pack_die (tree parm_pack,
21461 tree pack_arg,
21462 dw_die_ref subr_die,
21463 tree *next_arg)
21465 tree arg;
21466 dw_die_ref parm_pack_die;
21468 gcc_assert (parm_pack
21469 && lang_hooks.function_parameter_pack_p (parm_pack)
21470 && subr_die);
21472 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21473 add_src_coords_attributes (parm_pack_die, parm_pack);
21475 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21477 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21478 parm_pack))
21479 break;
21480 gen_formal_parameter_die (arg, NULL,
21481 false /* Don't emit name attribute. */,
21482 parm_pack_die);
21484 if (next_arg)
21485 *next_arg = arg;
21486 return parm_pack_die;
21489 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21490 at the end of an (ANSI prototyped) formal parameters list. */
21492 static void
21493 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21495 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21498 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21499 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21500 parameters as specified in some function type specification (except for
21501 those which appear as part of a function *definition*). */
21503 static void
21504 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21506 tree link;
21507 tree formal_type = NULL;
21508 tree first_parm_type;
21509 tree arg;
21511 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21513 arg = DECL_ARGUMENTS (function_or_method_type);
21514 function_or_method_type = TREE_TYPE (function_or_method_type);
21516 else
21517 arg = NULL_TREE;
21519 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21521 /* Make our first pass over the list of formal parameter types and output a
21522 DW_TAG_formal_parameter DIE for each one. */
21523 for (link = first_parm_type; link; )
21525 dw_die_ref parm_die;
21527 formal_type = TREE_VALUE (link);
21528 if (formal_type == void_type_node)
21529 break;
21531 /* Output a (nameless) DIE to represent the formal parameter itself. */
21532 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21534 parm_die = gen_formal_parameter_die (formal_type, NULL,
21535 true /* Emit name attribute. */,
21536 context_die);
21537 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21538 && link == first_parm_type)
21540 add_AT_flag (parm_die, DW_AT_artificial, 1);
21541 if (dwarf_version >= 3 || !dwarf_strict)
21542 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21544 else if (arg && DECL_ARTIFICIAL (arg))
21545 add_AT_flag (parm_die, DW_AT_artificial, 1);
21548 link = TREE_CHAIN (link);
21549 if (arg)
21550 arg = DECL_CHAIN (arg);
21553 /* If this function type has an ellipsis, add a
21554 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21555 if (formal_type != void_type_node)
21556 gen_unspecified_parameters_die (function_or_method_type, context_die);
21558 /* Make our second (and final) pass over the list of formal parameter types
21559 and output DIEs to represent those types (as necessary). */
21560 for (link = TYPE_ARG_TYPES (function_or_method_type);
21561 link && TREE_VALUE (link);
21562 link = TREE_CHAIN (link))
21563 gen_type_die (TREE_VALUE (link), context_die);
21566 /* We want to generate the DIE for TYPE so that we can generate the
21567 die for MEMBER, which has been defined; we will need to refer back
21568 to the member declaration nested within TYPE. If we're trying to
21569 generate minimal debug info for TYPE, processing TYPE won't do the
21570 trick; we need to attach the member declaration by hand. */
21572 static void
21573 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21575 gen_type_die (type, context_die);
21577 /* If we're trying to avoid duplicate debug info, we may not have
21578 emitted the member decl for this function. Emit it now. */
21579 if (TYPE_STUB_DECL (type)
21580 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21581 && ! lookup_decl_die (member))
21583 dw_die_ref type_die;
21584 gcc_assert (!decl_ultimate_origin (member));
21586 push_decl_scope (type);
21587 type_die = lookup_type_die_strip_naming_typedef (type);
21588 if (TREE_CODE (member) == FUNCTION_DECL)
21589 gen_subprogram_die (member, type_die);
21590 else if (TREE_CODE (member) == FIELD_DECL)
21592 /* Ignore the nameless fields that are used to skip bits but handle
21593 C++ anonymous unions and structs. */
21594 if (DECL_NAME (member) != NULL_TREE
21595 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21596 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21598 struct vlr_context vlr_ctx = {
21599 DECL_CONTEXT (member), /* struct_type */
21600 NULL_TREE /* variant_part_offset */
21602 gen_type_die (member_declared_type (member), type_die);
21603 gen_field_die (member, &vlr_ctx, type_die);
21606 else
21607 gen_variable_die (member, NULL_TREE, type_die);
21609 pop_decl_scope ();
21613 /* Forward declare these functions, because they are mutually recursive
21614 with their set_block_* pairing functions. */
21615 static void set_decl_origin_self (tree);
21617 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21618 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21619 that it points to the node itself, thus indicating that the node is its
21620 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21621 the given node is NULL, recursively descend the decl/block tree which
21622 it is the root of, and for each other ..._DECL or BLOCK node contained
21623 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21624 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21625 values to point to themselves. */
21627 static void
21628 set_block_origin_self (tree stmt)
21630 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21632 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21635 tree local_decl;
21637 for (local_decl = BLOCK_VARS (stmt);
21638 local_decl != NULL_TREE;
21639 local_decl = DECL_CHAIN (local_decl))
21640 /* Do not recurse on nested functions since the inlining status
21641 of parent and child can be different as per the DWARF spec. */
21642 if (TREE_CODE (local_decl) != FUNCTION_DECL
21643 && !DECL_EXTERNAL (local_decl))
21644 set_decl_origin_self (local_decl);
21648 tree subblock;
21650 for (subblock = BLOCK_SUBBLOCKS (stmt);
21651 subblock != NULL_TREE;
21652 subblock = BLOCK_CHAIN (subblock))
21653 set_block_origin_self (subblock); /* Recurse. */
21658 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21659 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21660 node to so that it points to the node itself, thus indicating that the
21661 node represents its own (abstract) origin. Additionally, if the
21662 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21663 the decl/block tree of which the given node is the root of, and for
21664 each other ..._DECL or BLOCK node contained therein whose
21665 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21666 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21667 point to themselves. */
21669 static void
21670 set_decl_origin_self (tree decl)
21672 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21674 DECL_ABSTRACT_ORIGIN (decl) = decl;
21675 if (TREE_CODE (decl) == FUNCTION_DECL)
21677 tree arg;
21679 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21680 DECL_ABSTRACT_ORIGIN (arg) = arg;
21681 if (DECL_INITIAL (decl) != NULL_TREE
21682 && DECL_INITIAL (decl) != error_mark_node)
21683 set_block_origin_self (DECL_INITIAL (decl));
21688 /* Mark the early DIE for DECL as the abstract instance. */
21690 static void
21691 dwarf2out_abstract_function (tree decl)
21693 dw_die_ref old_die;
21695 /* Make sure we have the actual abstract inline, not a clone. */
21696 decl = DECL_ORIGIN (decl);
21698 if (DECL_IGNORED_P (decl))
21699 return;
21701 old_die = lookup_decl_die (decl);
21702 /* With early debug we always have an old DIE unless we are in LTO
21703 and the user did not compile but only link with debug. */
21704 if (in_lto_p && ! old_die)
21705 return;
21706 gcc_assert (old_die != NULL);
21707 if (get_AT (old_die, DW_AT_inline)
21708 || get_AT (old_die, DW_AT_abstract_origin))
21709 /* We've already generated the abstract instance. */
21710 return;
21712 /* Go ahead and put DW_AT_inline on the DIE. */
21713 if (DECL_DECLARED_INLINE_P (decl))
21715 if (cgraph_function_possibly_inlined_p (decl))
21716 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21717 else
21718 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21720 else
21722 if (cgraph_function_possibly_inlined_p (decl))
21723 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21724 else
21725 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21728 if (DECL_DECLARED_INLINE_P (decl)
21729 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21730 add_AT_flag (old_die, DW_AT_artificial, 1);
21732 set_decl_origin_self (decl);
21735 /* Helper function of premark_used_types() which gets called through
21736 htab_traverse.
21738 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21739 marked as unused by prune_unused_types. */
21741 bool
21742 premark_used_types_helper (tree const &type, void *)
21744 dw_die_ref die;
21746 die = lookup_type_die (type);
21747 if (die != NULL)
21748 die->die_perennial_p = 1;
21749 return true;
21752 /* Helper function of premark_types_used_by_global_vars which gets called
21753 through htab_traverse.
21755 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21756 marked as unused by prune_unused_types. The DIE of the type is marked
21757 only if the global variable using the type will actually be emitted. */
21760 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21761 void *)
21763 struct types_used_by_vars_entry *entry;
21764 dw_die_ref die;
21766 entry = (struct types_used_by_vars_entry *) *slot;
21767 gcc_assert (entry->type != NULL
21768 && entry->var_decl != NULL);
21769 die = lookup_type_die (entry->type);
21770 if (die)
21772 /* Ask cgraph if the global variable really is to be emitted.
21773 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21774 varpool_node *node = varpool_node::get (entry->var_decl);
21775 if (node && node->definition)
21777 die->die_perennial_p = 1;
21778 /* Keep the parent DIEs as well. */
21779 while ((die = die->die_parent) && die->die_perennial_p == 0)
21780 die->die_perennial_p = 1;
21783 return 1;
21786 /* Mark all members of used_types_hash as perennial. */
21788 static void
21789 premark_used_types (struct function *fun)
21791 if (fun && fun->used_types_hash)
21792 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21795 /* Mark all members of types_used_by_vars_entry as perennial. */
21797 static void
21798 premark_types_used_by_global_vars (void)
21800 if (types_used_by_vars_hash)
21801 types_used_by_vars_hash
21802 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21805 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21806 for CA_LOC call arg loc node. */
21808 static dw_die_ref
21809 gen_call_site_die (tree decl, dw_die_ref subr_die,
21810 struct call_arg_loc_node *ca_loc)
21812 dw_die_ref stmt_die = NULL, die;
21813 tree block = ca_loc->block;
21815 while (block
21816 && block != DECL_INITIAL (decl)
21817 && TREE_CODE (block) == BLOCK)
21819 stmt_die = BLOCK_DIE (block);
21820 if (stmt_die)
21821 break;
21822 block = BLOCK_SUPERCONTEXT (block);
21824 if (stmt_die == NULL)
21825 stmt_die = subr_die;
21826 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21827 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21828 if (ca_loc->tail_call_p)
21829 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21830 if (ca_loc->symbol_ref)
21832 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21833 if (tdie)
21834 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21835 else
21836 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21837 false);
21839 return die;
21842 /* Generate a DIE to represent a declared function (either file-scope or
21843 block-local). */
21845 static void
21846 gen_subprogram_die (tree decl, dw_die_ref context_die)
21848 tree origin = decl_ultimate_origin (decl);
21849 dw_die_ref subr_die;
21850 dw_die_ref old_die = lookup_decl_die (decl);
21852 /* This function gets called multiple times for different stages of
21853 the debug process. For example, for func() in this code:
21855 namespace S
21857 void func() { ... }
21860 ...we get called 4 times. Twice in early debug and twice in
21861 late debug:
21863 Early debug
21864 -----------
21866 1. Once while generating func() within the namespace. This is
21867 the declaration. The declaration bit below is set, as the
21868 context is the namespace.
21870 A new DIE will be generated with DW_AT_declaration set.
21872 2. Once for func() itself. This is the specification. The
21873 declaration bit below is clear as the context is the CU.
21875 We will use the cached DIE from (1) to create a new DIE with
21876 DW_AT_specification pointing to the declaration in (1).
21878 Late debug via rest_of_handle_final()
21879 -------------------------------------
21881 3. Once generating func() within the namespace. This is also the
21882 declaration, as in (1), but this time we will early exit below
21883 as we have a cached DIE and a declaration needs no additional
21884 annotations (no locations), as the source declaration line
21885 info is enough.
21887 4. Once for func() itself. As in (2), this is the specification,
21888 but this time we will re-use the cached DIE, and just annotate
21889 it with the location information that should now be available.
21891 For something without namespaces, but with abstract instances, we
21892 are also called a multiple times:
21894 class Base
21896 public:
21897 Base (); // constructor declaration (1)
21900 Base::Base () { } // constructor specification (2)
21902 Early debug
21903 -----------
21905 1. Once for the Base() constructor by virtue of it being a
21906 member of the Base class. This is done via
21907 rest_of_type_compilation.
21909 This is a declaration, so a new DIE will be created with
21910 DW_AT_declaration.
21912 2. Once for the Base() constructor definition, but this time
21913 while generating the abstract instance of the base
21914 constructor (__base_ctor) which is being generated via early
21915 debug of reachable functions.
21917 Even though we have a cached version of the declaration (1),
21918 we will create a DW_AT_specification of the declaration DIE
21919 in (1).
21921 3. Once for the __base_ctor itself, but this time, we generate
21922 an DW_AT_abstract_origin version of the DW_AT_specification in
21923 (2).
21925 Late debug via rest_of_handle_final
21926 -----------------------------------
21928 4. One final time for the __base_ctor (which will have a cached
21929 DIE with DW_AT_abstract_origin created in (3). This time,
21930 we will just annotate the location information now
21931 available.
21933 int declaration = (current_function_decl != decl
21934 || class_or_namespace_scope_p (context_die));
21936 /* Now that the C++ front end lazily declares artificial member fns, we
21937 might need to retrofit the declaration into its class. */
21938 if (!declaration && !origin && !old_die
21939 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21940 && !class_or_namespace_scope_p (context_die)
21941 && debug_info_level > DINFO_LEVEL_TERSE)
21942 old_die = force_decl_die (decl);
21944 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21945 if (origin != NULL)
21947 gcc_assert (!declaration || local_scope_p (context_die));
21949 /* Fixup die_parent for the abstract instance of a nested
21950 inline function. */
21951 if (old_die && old_die->die_parent == NULL)
21952 add_child_die (context_die, old_die);
21954 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21956 /* If we have a DW_AT_abstract_origin we have a working
21957 cached version. */
21958 subr_die = old_die;
21960 else
21962 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21963 add_abstract_origin_attribute (subr_die, origin);
21964 /* This is where the actual code for a cloned function is.
21965 Let's emit linkage name attribute for it. This helps
21966 debuggers to e.g, set breakpoints into
21967 constructors/destructors when the user asks "break
21968 K::K". */
21969 add_linkage_name (subr_die, decl);
21972 /* A cached copy, possibly from early dwarf generation. Reuse as
21973 much as possible. */
21974 else if (old_die)
21976 /* A declaration that has been previously dumped needs no
21977 additional information. */
21978 if (declaration)
21979 return;
21981 if (!get_AT_flag (old_die, DW_AT_declaration)
21982 /* We can have a normal definition following an inline one in the
21983 case of redefinition of GNU C extern inlines.
21984 It seems reasonable to use AT_specification in this case. */
21985 && !get_AT (old_die, DW_AT_inline))
21987 /* Detect and ignore this case, where we are trying to output
21988 something we have already output. */
21989 if (get_AT (old_die, DW_AT_low_pc)
21990 || get_AT (old_die, DW_AT_ranges))
21991 return;
21993 /* If we have no location information, this must be a
21994 partially generated DIE from early dwarf generation.
21995 Fall through and generate it. */
21998 /* If the definition comes from the same place as the declaration,
21999 maybe use the old DIE. We always want the DIE for this function
22000 that has the *_pc attributes to be under comp_unit_die so the
22001 debugger can find it. We also need to do this for abstract
22002 instances of inlines, since the spec requires the out-of-line copy
22003 to have the same parent. For local class methods, this doesn't
22004 apply; we just use the old DIE. */
22005 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22006 struct dwarf_file_data * file_index = lookup_filename (s.file);
22007 if ((is_cu_die (old_die->die_parent)
22008 /* This condition fixes the inconsistency/ICE with the
22009 following Fortran test (or some derivative thereof) while
22010 building libgfortran:
22012 module some_m
22013 contains
22014 logical function funky (FLAG)
22015 funky = .true.
22016 end function
22017 end module
22019 || (old_die->die_parent
22020 && old_die->die_parent->die_tag == DW_TAG_module)
22021 || context_die == NULL)
22022 && (DECL_ARTIFICIAL (decl)
22023 /* The location attributes may be in the abstract origin
22024 which in the case of LTO might be not available to
22025 look at. */
22026 || get_AT (old_die, DW_AT_abstract_origin)
22027 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22028 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22029 == (unsigned) s.line)
22030 && (!debug_column_info
22031 || s.column == 0
22032 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22033 == (unsigned) s.column)))))
22035 subr_die = old_die;
22037 /* Clear out the declaration attribute, but leave the
22038 parameters so they can be augmented with location
22039 information later. Unless this was a declaration, in
22040 which case, wipe out the nameless parameters and recreate
22041 them further down. */
22042 if (remove_AT (subr_die, DW_AT_declaration))
22045 remove_AT (subr_die, DW_AT_object_pointer);
22046 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22049 /* Make a specification pointing to the previously built
22050 declaration. */
22051 else
22053 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22054 add_AT_specification (subr_die, old_die);
22055 add_pubname (decl, subr_die);
22056 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22057 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22058 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22059 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22060 if (debug_column_info
22061 && s.column
22062 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22063 != (unsigned) s.column))
22064 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22066 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22067 emit the real type on the definition die. */
22068 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22070 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22071 if (die == auto_die || die == decltype_auto_die)
22072 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22073 TYPE_UNQUALIFIED, false, context_die);
22076 /* When we process the method declaration, we haven't seen
22077 the out-of-class defaulted definition yet, so we have to
22078 recheck now. */
22079 if ((dwarf_version >= 5 || ! dwarf_strict)
22080 && !get_AT (subr_die, DW_AT_defaulted))
22082 int defaulted
22083 = lang_hooks.decls.decl_dwarf_attribute (decl,
22084 DW_AT_defaulted);
22085 if (defaulted != -1)
22087 /* Other values must have been handled before. */
22088 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22089 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22094 /* Create a fresh DIE for anything else. */
22095 else
22097 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22099 if (TREE_PUBLIC (decl))
22100 add_AT_flag (subr_die, DW_AT_external, 1);
22102 add_name_and_src_coords_attributes (subr_die, decl);
22103 add_pubname (decl, subr_die);
22104 if (debug_info_level > DINFO_LEVEL_TERSE)
22106 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22107 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22108 TYPE_UNQUALIFIED, false, context_die);
22111 add_pure_or_virtual_attribute (subr_die, decl);
22112 if (DECL_ARTIFICIAL (decl))
22113 add_AT_flag (subr_die, DW_AT_artificial, 1);
22115 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22116 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22118 add_alignment_attribute (subr_die, decl);
22120 add_accessibility_attribute (subr_die, decl);
22123 /* Unless we have an existing non-declaration DIE, equate the new
22124 DIE. */
22125 if (!old_die || is_declaration_die (old_die))
22126 equate_decl_number_to_die (decl, subr_die);
22128 if (declaration)
22130 if (!old_die || !get_AT (old_die, DW_AT_inline))
22132 add_AT_flag (subr_die, DW_AT_declaration, 1);
22134 /* If this is an explicit function declaration then generate
22135 a DW_AT_explicit attribute. */
22136 if ((dwarf_version >= 3 || !dwarf_strict)
22137 && lang_hooks.decls.decl_dwarf_attribute (decl,
22138 DW_AT_explicit) == 1)
22139 add_AT_flag (subr_die, DW_AT_explicit, 1);
22141 /* If this is a C++11 deleted special function member then generate
22142 a DW_AT_deleted attribute. */
22143 if ((dwarf_version >= 5 || !dwarf_strict)
22144 && lang_hooks.decls.decl_dwarf_attribute (decl,
22145 DW_AT_deleted) == 1)
22146 add_AT_flag (subr_die, DW_AT_deleted, 1);
22148 /* If this is a C++11 defaulted special function member then
22149 generate a DW_AT_defaulted attribute. */
22150 if (dwarf_version >= 5 || !dwarf_strict)
22152 int defaulted
22153 = lang_hooks.decls.decl_dwarf_attribute (decl,
22154 DW_AT_defaulted);
22155 if (defaulted != -1)
22156 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22159 /* If this is a C++11 non-static member function with & ref-qualifier
22160 then generate a DW_AT_reference attribute. */
22161 if ((dwarf_version >= 5 || !dwarf_strict)
22162 && lang_hooks.decls.decl_dwarf_attribute (decl,
22163 DW_AT_reference) == 1)
22164 add_AT_flag (subr_die, DW_AT_reference, 1);
22166 /* If this is a C++11 non-static member function with &&
22167 ref-qualifier then generate a DW_AT_reference attribute. */
22168 if ((dwarf_version >= 5 || !dwarf_strict)
22169 && lang_hooks.decls.decl_dwarf_attribute (decl,
22170 DW_AT_rvalue_reference)
22171 == 1)
22172 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22175 /* For non DECL_EXTERNALs, if range information is available, fill
22176 the DIE with it. */
22177 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22179 HOST_WIDE_INT cfa_fb_offset;
22181 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22183 if (!crtl->has_bb_partition)
22185 dw_fde_ref fde = fun->fde;
22186 if (fde->dw_fde_begin)
22188 /* We have already generated the labels. */
22189 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22190 fde->dw_fde_end, false);
22192 else
22194 /* Create start/end labels and add the range. */
22195 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22196 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22197 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22198 current_function_funcdef_no);
22199 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22200 current_function_funcdef_no);
22201 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22202 false);
22205 #if VMS_DEBUGGING_INFO
22206 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22207 Section 2.3 Prologue and Epilogue Attributes:
22208 When a breakpoint is set on entry to a function, it is generally
22209 desirable for execution to be suspended, not on the very first
22210 instruction of the function, but rather at a point after the
22211 function's frame has been set up, after any language defined local
22212 declaration processing has been completed, and before execution of
22213 the first statement of the function begins. Debuggers generally
22214 cannot properly determine where this point is. Similarly for a
22215 breakpoint set on exit from a function. The prologue and epilogue
22216 attributes allow a compiler to communicate the location(s) to use. */
22219 if (fde->dw_fde_vms_end_prologue)
22220 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22221 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22223 if (fde->dw_fde_vms_begin_epilogue)
22224 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22225 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22227 #endif
22230 else
22232 /* Generate pubnames entries for the split function code ranges. */
22233 dw_fde_ref fde = fun->fde;
22235 if (fde->dw_fde_second_begin)
22237 if (dwarf_version >= 3 || !dwarf_strict)
22239 /* We should use ranges for non-contiguous code section
22240 addresses. Use the actual code range for the initial
22241 section, since the HOT/COLD labels might precede an
22242 alignment offset. */
22243 bool range_list_added = false;
22244 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22245 fde->dw_fde_end, &range_list_added,
22246 false);
22247 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22248 fde->dw_fde_second_end,
22249 &range_list_added, false);
22250 if (range_list_added)
22251 add_ranges (NULL);
22253 else
22255 /* There is no real support in DW2 for this .. so we make
22256 a work-around. First, emit the pub name for the segment
22257 containing the function label. Then make and emit a
22258 simplified subprogram DIE for the second segment with the
22259 name pre-fixed by __hot/cold_sect_of_. We use the same
22260 linkage name for the second die so that gdb will find both
22261 sections when given "b foo". */
22262 const char *name = NULL;
22263 tree decl_name = DECL_NAME (decl);
22264 dw_die_ref seg_die;
22266 /* Do the 'primary' section. */
22267 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22268 fde->dw_fde_end, false);
22270 /* Build a minimal DIE for the secondary section. */
22271 seg_die = new_die (DW_TAG_subprogram,
22272 subr_die->die_parent, decl);
22274 if (TREE_PUBLIC (decl))
22275 add_AT_flag (seg_die, DW_AT_external, 1);
22277 if (decl_name != NULL
22278 && IDENTIFIER_POINTER (decl_name) != NULL)
22280 name = dwarf2_name (decl, 1);
22281 if (! DECL_ARTIFICIAL (decl))
22282 add_src_coords_attributes (seg_die, decl);
22284 add_linkage_name (seg_die, decl);
22286 gcc_assert (name != NULL);
22287 add_pure_or_virtual_attribute (seg_die, decl);
22288 if (DECL_ARTIFICIAL (decl))
22289 add_AT_flag (seg_die, DW_AT_artificial, 1);
22291 name = concat ("__second_sect_of_", name, NULL);
22292 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22293 fde->dw_fde_second_end, false);
22294 add_name_attribute (seg_die, name);
22295 if (want_pubnames ())
22296 add_pubname_string (name, seg_die);
22299 else
22300 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22301 false);
22304 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22306 /* We define the "frame base" as the function's CFA. This is more
22307 convenient for several reasons: (1) It's stable across the prologue
22308 and epilogue, which makes it better than just a frame pointer,
22309 (2) With dwarf3, there exists a one-byte encoding that allows us
22310 to reference the .debug_frame data by proxy, but failing that,
22311 (3) We can at least reuse the code inspection and interpretation
22312 code that determines the CFA position at various points in the
22313 function. */
22314 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22316 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22317 add_AT_loc (subr_die, DW_AT_frame_base, op);
22319 else
22321 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22322 if (list->dw_loc_next)
22323 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22324 else
22325 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22328 /* Compute a displacement from the "steady-state frame pointer" to
22329 the CFA. The former is what all stack slots and argument slots
22330 will reference in the rtl; the latter is what we've told the
22331 debugger about. We'll need to adjust all frame_base references
22332 by this displacement. */
22333 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22335 if (fun->static_chain_decl)
22337 /* DWARF requires here a location expression that computes the
22338 address of the enclosing subprogram's frame base. The machinery
22339 in tree-nested.c is supposed to store this specific address in the
22340 last field of the FRAME record. */
22341 const tree frame_type
22342 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22343 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22345 tree fb_expr
22346 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22347 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22348 fb_expr, fb_decl, NULL_TREE);
22350 add_AT_location_description (subr_die, DW_AT_static_link,
22351 loc_list_from_tree (fb_expr, 0, NULL));
22354 resolve_variable_values ();
22357 /* Generate child dies for template paramaters. */
22358 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22359 gen_generic_params_dies (decl);
22361 /* Now output descriptions of the arguments for this function. This gets
22362 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22363 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22364 `...' at the end of the formal parameter list. In order to find out if
22365 there was a trailing ellipsis or not, we must instead look at the type
22366 associated with the FUNCTION_DECL. This will be a node of type
22367 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22368 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22369 an ellipsis at the end. */
22371 /* In the case where we are describing a mere function declaration, all we
22372 need to do here (and all we *can* do here) is to describe the *types* of
22373 its formal parameters. */
22374 if (debug_info_level <= DINFO_LEVEL_TERSE)
22376 else if (declaration)
22377 gen_formal_types_die (decl, subr_die);
22378 else
22380 /* Generate DIEs to represent all known formal parameters. */
22381 tree parm = DECL_ARGUMENTS (decl);
22382 tree generic_decl = early_dwarf
22383 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22384 tree generic_decl_parm = generic_decl
22385 ? DECL_ARGUMENTS (generic_decl)
22386 : NULL;
22388 /* Now we want to walk the list of parameters of the function and
22389 emit their relevant DIEs.
22391 We consider the case of DECL being an instance of a generic function
22392 as well as it being a normal function.
22394 If DECL is an instance of a generic function we walk the
22395 parameters of the generic function declaration _and_ the parameters of
22396 DECL itself. This is useful because we want to emit specific DIEs for
22397 function parameter packs and those are declared as part of the
22398 generic function declaration. In that particular case,
22399 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22400 That DIE has children DIEs representing the set of arguments
22401 of the pack. Note that the set of pack arguments can be empty.
22402 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22403 children DIE.
22405 Otherwise, we just consider the parameters of DECL. */
22406 while (generic_decl_parm || parm)
22408 if (generic_decl_parm
22409 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22410 gen_formal_parameter_pack_die (generic_decl_parm,
22411 parm, subr_die,
22412 &parm);
22413 else if (parm && !POINTER_BOUNDS_P (parm))
22415 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22417 if (early_dwarf
22418 && parm == DECL_ARGUMENTS (decl)
22419 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22420 && parm_die
22421 && (dwarf_version >= 3 || !dwarf_strict))
22422 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22424 parm = DECL_CHAIN (parm);
22426 else if (parm)
22427 parm = DECL_CHAIN (parm);
22429 if (generic_decl_parm)
22430 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22433 /* Decide whether we need an unspecified_parameters DIE at the end.
22434 There are 2 more cases to do this for: 1) the ansi ... declaration -
22435 this is detectable when the end of the arg list is not a
22436 void_type_node 2) an unprototyped function declaration (not a
22437 definition). This just means that we have no info about the
22438 parameters at all. */
22439 if (early_dwarf)
22441 if (prototype_p (TREE_TYPE (decl)))
22443 /* This is the prototyped case, check for.... */
22444 if (stdarg_p (TREE_TYPE (decl)))
22445 gen_unspecified_parameters_die (decl, subr_die);
22447 else if (DECL_INITIAL (decl) == NULL_TREE)
22448 gen_unspecified_parameters_die (decl, subr_die);
22452 if (subr_die != old_die)
22453 /* Add the calling convention attribute if requested. */
22454 add_calling_convention_attribute (subr_die, decl);
22456 /* Output Dwarf info for all of the stuff within the body of the function
22457 (if it has one - it may be just a declaration).
22459 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22460 a function. This BLOCK actually represents the outermost binding contour
22461 for the function, i.e. the contour in which the function's formal
22462 parameters and labels get declared. Curiously, it appears that the front
22463 end doesn't actually put the PARM_DECL nodes for the current function onto
22464 the BLOCK_VARS list for this outer scope, but are strung off of the
22465 DECL_ARGUMENTS list for the function instead.
22467 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22468 the LABEL_DECL nodes for the function however, and we output DWARF info
22469 for those in decls_for_scope. Just within the `outer_scope' there will be
22470 a BLOCK node representing the function's outermost pair of curly braces,
22471 and any blocks used for the base and member initializers of a C++
22472 constructor function. */
22473 tree outer_scope = DECL_INITIAL (decl);
22474 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22476 int call_site_note_count = 0;
22477 int tail_call_site_note_count = 0;
22479 /* Emit a DW_TAG_variable DIE for a named return value. */
22480 if (DECL_NAME (DECL_RESULT (decl)))
22481 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22483 /* The first time through decls_for_scope we will generate the
22484 DIEs for the locals. The second time, we fill in the
22485 location info. */
22486 decls_for_scope (outer_scope, subr_die);
22488 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22490 struct call_arg_loc_node *ca_loc;
22491 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22493 dw_die_ref die = NULL;
22494 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22495 rtx arg, next_arg;
22497 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22498 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22499 : NULL_RTX);
22500 arg; arg = next_arg)
22502 dw_loc_descr_ref reg, val;
22503 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22504 dw_die_ref cdie, tdie = NULL;
22506 next_arg = XEXP (arg, 1);
22507 if (REG_P (XEXP (XEXP (arg, 0), 0))
22508 && next_arg
22509 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22510 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22511 && REGNO (XEXP (XEXP (arg, 0), 0))
22512 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22513 next_arg = XEXP (next_arg, 1);
22514 if (mode == VOIDmode)
22516 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22517 if (mode == VOIDmode)
22518 mode = GET_MODE (XEXP (arg, 0));
22520 if (mode == VOIDmode || mode == BLKmode)
22521 continue;
22522 /* Get dynamic information about call target only if we
22523 have no static information: we cannot generate both
22524 DW_AT_call_origin and DW_AT_call_target
22525 attributes. */
22526 if (ca_loc->symbol_ref == NULL_RTX)
22528 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22530 tloc = XEXP (XEXP (arg, 0), 1);
22531 continue;
22533 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22534 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22536 tlocc = XEXP (XEXP (arg, 0), 1);
22537 continue;
22540 reg = NULL;
22541 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22542 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22543 VAR_INIT_STATUS_INITIALIZED);
22544 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22546 rtx mem = XEXP (XEXP (arg, 0), 0);
22547 reg = mem_loc_descriptor (XEXP (mem, 0),
22548 get_address_mode (mem),
22549 GET_MODE (mem),
22550 VAR_INIT_STATUS_INITIALIZED);
22552 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22553 == DEBUG_PARAMETER_REF)
22555 tree tdecl
22556 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22557 tdie = lookup_decl_die (tdecl);
22558 if (tdie == NULL)
22559 continue;
22561 else
22562 continue;
22563 if (reg == NULL
22564 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22565 != DEBUG_PARAMETER_REF)
22566 continue;
22567 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22568 VOIDmode,
22569 VAR_INIT_STATUS_INITIALIZED);
22570 if (val == NULL)
22571 continue;
22572 if (die == NULL)
22573 die = gen_call_site_die (decl, subr_die, ca_loc);
22574 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22575 NULL_TREE);
22576 if (reg != NULL)
22577 add_AT_loc (cdie, DW_AT_location, reg);
22578 else if (tdie != NULL)
22579 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22580 tdie);
22581 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22582 if (next_arg != XEXP (arg, 1))
22584 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22585 if (mode == VOIDmode)
22586 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22587 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22588 0), 1),
22589 mode, VOIDmode,
22590 VAR_INIT_STATUS_INITIALIZED);
22591 if (val != NULL)
22592 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22593 val);
22596 if (die == NULL
22597 && (ca_loc->symbol_ref || tloc))
22598 die = gen_call_site_die (decl, subr_die, ca_loc);
22599 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22601 dw_loc_descr_ref tval = NULL;
22603 if (tloc != NULL_RTX)
22604 tval = mem_loc_descriptor (tloc,
22605 GET_MODE (tloc) == VOIDmode
22606 ? Pmode : GET_MODE (tloc),
22607 VOIDmode,
22608 VAR_INIT_STATUS_INITIALIZED);
22609 if (tval)
22610 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22611 else if (tlocc != NULL_RTX)
22613 tval = mem_loc_descriptor (tlocc,
22614 GET_MODE (tlocc) == VOIDmode
22615 ? Pmode : GET_MODE (tlocc),
22616 VOIDmode,
22617 VAR_INIT_STATUS_INITIALIZED);
22618 if (tval)
22619 add_AT_loc (die,
22620 dwarf_AT (DW_AT_call_target_clobbered),
22621 tval);
22624 if (die != NULL)
22626 call_site_note_count++;
22627 if (ca_loc->tail_call_p)
22628 tail_call_site_note_count++;
22632 call_arg_locations = NULL;
22633 call_arg_loc_last = NULL;
22634 if (tail_call_site_count >= 0
22635 && tail_call_site_count == tail_call_site_note_count
22636 && (!dwarf_strict || dwarf_version >= 5))
22638 if (call_site_count >= 0
22639 && call_site_count == call_site_note_count)
22640 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22641 else
22642 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22644 call_site_count = -1;
22645 tail_call_site_count = -1;
22648 /* Mark used types after we have created DIEs for the functions scopes. */
22649 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22652 /* Returns a hash value for X (which really is a die_struct). */
22654 hashval_t
22655 block_die_hasher::hash (die_struct *d)
22657 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22660 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22661 as decl_id and die_parent of die_struct Y. */
22663 bool
22664 block_die_hasher::equal (die_struct *x, die_struct *y)
22666 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22669 /* Return TRUE if DECL, which may have been previously generated as
22670 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22671 true if decl (or its origin) is either an extern declaration or a
22672 class/namespace scoped declaration.
22674 The declare_in_namespace support causes us to get two DIEs for one
22675 variable, both of which are declarations. We want to avoid
22676 considering one to be a specification, so we must test for
22677 DECLARATION and DW_AT_declaration. */
22678 static inline bool
22679 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22681 return (old_die && TREE_STATIC (decl) && !declaration
22682 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22685 /* Return true if DECL is a local static. */
22687 static inline bool
22688 local_function_static (tree decl)
22690 gcc_assert (VAR_P (decl));
22691 return TREE_STATIC (decl)
22692 && DECL_CONTEXT (decl)
22693 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22696 /* Generate a DIE to represent a declared data object.
22697 Either DECL or ORIGIN must be non-null. */
22699 static void
22700 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22702 HOST_WIDE_INT off = 0;
22703 tree com_decl;
22704 tree decl_or_origin = decl ? decl : origin;
22705 tree ultimate_origin;
22706 dw_die_ref var_die;
22707 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22708 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22709 || class_or_namespace_scope_p (context_die));
22710 bool specialization_p = false;
22711 bool no_linkage_name = false;
22713 /* While C++ inline static data members have definitions inside of the
22714 class, force the first DIE to be a declaration, then let gen_member_die
22715 reparent it to the class context and call gen_variable_die again
22716 to create the outside of the class DIE for the definition. */
22717 if (!declaration
22718 && old_die == NULL
22719 && decl
22720 && DECL_CONTEXT (decl)
22721 && TYPE_P (DECL_CONTEXT (decl))
22722 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22724 declaration = true;
22725 if (dwarf_version < 5)
22726 no_linkage_name = true;
22729 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22730 if (decl || ultimate_origin)
22731 origin = ultimate_origin;
22732 com_decl = fortran_common (decl_or_origin, &off);
22734 /* Symbol in common gets emitted as a child of the common block, in the form
22735 of a data member. */
22736 if (com_decl)
22738 dw_die_ref com_die;
22739 dw_loc_list_ref loc = NULL;
22740 die_node com_die_arg;
22742 var_die = lookup_decl_die (decl_or_origin);
22743 if (var_die)
22745 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22747 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22748 if (loc)
22750 if (off)
22752 /* Optimize the common case. */
22753 if (single_element_loc_list_p (loc)
22754 && loc->expr->dw_loc_opc == DW_OP_addr
22755 && loc->expr->dw_loc_next == NULL
22756 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22757 == SYMBOL_REF)
22759 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22760 loc->expr->dw_loc_oprnd1.v.val_addr
22761 = plus_constant (GET_MODE (x), x , off);
22763 else
22764 loc_list_plus_const (loc, off);
22766 add_AT_location_description (var_die, DW_AT_location, loc);
22767 remove_AT (var_die, DW_AT_declaration);
22770 return;
22773 if (common_block_die_table == NULL)
22774 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22776 com_die_arg.decl_id = DECL_UID (com_decl);
22777 com_die_arg.die_parent = context_die;
22778 com_die = common_block_die_table->find (&com_die_arg);
22779 if (! early_dwarf)
22780 loc = loc_list_from_tree (com_decl, 2, NULL);
22781 if (com_die == NULL)
22783 const char *cnam
22784 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22785 die_node **slot;
22787 com_die = new_die (DW_TAG_common_block, context_die, decl);
22788 add_name_and_src_coords_attributes (com_die, com_decl);
22789 if (loc)
22791 add_AT_location_description (com_die, DW_AT_location, loc);
22792 /* Avoid sharing the same loc descriptor between
22793 DW_TAG_common_block and DW_TAG_variable. */
22794 loc = loc_list_from_tree (com_decl, 2, NULL);
22796 else if (DECL_EXTERNAL (decl_or_origin))
22797 add_AT_flag (com_die, DW_AT_declaration, 1);
22798 if (want_pubnames ())
22799 add_pubname_string (cnam, com_die); /* ??? needed? */
22800 com_die->decl_id = DECL_UID (com_decl);
22801 slot = common_block_die_table->find_slot (com_die, INSERT);
22802 *slot = com_die;
22804 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22806 add_AT_location_description (com_die, DW_AT_location, loc);
22807 loc = loc_list_from_tree (com_decl, 2, NULL);
22808 remove_AT (com_die, DW_AT_declaration);
22810 var_die = new_die (DW_TAG_variable, com_die, decl);
22811 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22812 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22813 decl_quals (decl_or_origin), false,
22814 context_die);
22815 add_alignment_attribute (var_die, decl);
22816 add_AT_flag (var_die, DW_AT_external, 1);
22817 if (loc)
22819 if (off)
22821 /* Optimize the common case. */
22822 if (single_element_loc_list_p (loc)
22823 && loc->expr->dw_loc_opc == DW_OP_addr
22824 && loc->expr->dw_loc_next == NULL
22825 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22827 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22828 loc->expr->dw_loc_oprnd1.v.val_addr
22829 = plus_constant (GET_MODE (x), x, off);
22831 else
22832 loc_list_plus_const (loc, off);
22834 add_AT_location_description (var_die, DW_AT_location, loc);
22836 else if (DECL_EXTERNAL (decl_or_origin))
22837 add_AT_flag (var_die, DW_AT_declaration, 1);
22838 if (decl)
22839 equate_decl_number_to_die (decl, var_die);
22840 return;
22843 if (old_die)
22845 if (declaration)
22847 /* A declaration that has been previously dumped, needs no
22848 further annotations, since it doesn't need location on
22849 the second pass. */
22850 return;
22852 else if (decl_will_get_specification_p (old_die, decl, declaration)
22853 && !get_AT (old_die, DW_AT_specification))
22855 /* Fall-thru so we can make a new variable die along with a
22856 DW_AT_specification. */
22858 else if (origin && old_die->die_parent != context_die)
22860 /* If we will be creating an inlined instance, we need a
22861 new DIE that will get annotated with
22862 DW_AT_abstract_origin. Clear things so we can get a
22863 new DIE. */
22864 gcc_assert (!DECL_ABSTRACT_P (decl));
22865 old_die = NULL;
22867 else
22869 /* If a DIE was dumped early, it still needs location info.
22870 Skip to where we fill the location bits. */
22871 var_die = old_die;
22873 /* ??? In LTRANS we cannot annotate early created variably
22874 modified type DIEs without copying them and adjusting all
22875 references to them. Thus we dumped them again, also add a
22876 reference to them. */
22877 tree type = TREE_TYPE (decl_or_origin);
22878 if (in_lto_p
22879 && variably_modified_type_p
22880 (type, decl_function_context (decl_or_origin)))
22882 if (decl_by_reference_p (decl_or_origin))
22883 add_type_attribute (var_die, TREE_TYPE (type),
22884 TYPE_UNQUALIFIED, false, context_die);
22885 else
22886 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22887 false, context_die);
22890 goto gen_variable_die_location;
22894 /* For static data members, the declaration in the class is supposed
22895 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22896 also in DWARF2; the specification should still be DW_TAG_variable
22897 referencing the DW_TAG_member DIE. */
22898 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22899 var_die = new_die (DW_TAG_member, context_die, decl);
22900 else
22901 var_die = new_die (DW_TAG_variable, context_die, decl);
22903 if (origin != NULL)
22904 add_abstract_origin_attribute (var_die, origin);
22906 /* Loop unrolling can create multiple blocks that refer to the same
22907 static variable, so we must test for the DW_AT_declaration flag.
22909 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22910 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22911 sharing them.
22913 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22914 else if (decl_will_get_specification_p (old_die, decl, declaration))
22916 /* This is a definition of a C++ class level static. */
22917 add_AT_specification (var_die, old_die);
22918 specialization_p = true;
22919 if (DECL_NAME (decl))
22921 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22922 struct dwarf_file_data * file_index = lookup_filename (s.file);
22924 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22925 add_AT_file (var_die, DW_AT_decl_file, file_index);
22927 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22928 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22930 if (debug_column_info
22931 && s.column
22932 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22933 != (unsigned) s.column))
22934 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22936 if (old_die->die_tag == DW_TAG_member)
22937 add_linkage_name (var_die, decl);
22940 else
22941 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22943 if ((origin == NULL && !specialization_p)
22944 || (origin != NULL
22945 && !DECL_ABSTRACT_P (decl_or_origin)
22946 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22947 decl_function_context
22948 (decl_or_origin))))
22950 tree type = TREE_TYPE (decl_or_origin);
22952 if (decl_by_reference_p (decl_or_origin))
22953 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22954 context_die);
22955 else
22956 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22957 context_die);
22960 if (origin == NULL && !specialization_p)
22962 if (TREE_PUBLIC (decl))
22963 add_AT_flag (var_die, DW_AT_external, 1);
22965 if (DECL_ARTIFICIAL (decl))
22966 add_AT_flag (var_die, DW_AT_artificial, 1);
22968 add_alignment_attribute (var_die, decl);
22970 add_accessibility_attribute (var_die, decl);
22973 if (declaration)
22974 add_AT_flag (var_die, DW_AT_declaration, 1);
22976 if (decl && (DECL_ABSTRACT_P (decl)
22977 || !old_die || is_declaration_die (old_die)))
22978 equate_decl_number_to_die (decl, var_die);
22980 gen_variable_die_location:
22981 if (! declaration
22982 && (! DECL_ABSTRACT_P (decl_or_origin)
22983 /* Local static vars are shared between all clones/inlines,
22984 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22985 already set. */
22986 || (VAR_P (decl_or_origin)
22987 && TREE_STATIC (decl_or_origin)
22988 && DECL_RTL_SET_P (decl_or_origin))))
22990 if (early_dwarf)
22991 add_pubname (decl_or_origin, var_die);
22992 else
22993 add_location_or_const_value_attribute (var_die, decl_or_origin,
22994 decl == NULL);
22996 else
22997 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22999 if ((dwarf_version >= 4 || !dwarf_strict)
23000 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23001 DW_AT_const_expr) == 1
23002 && !get_AT (var_die, DW_AT_const_expr)
23003 && !specialization_p)
23004 add_AT_flag (var_die, DW_AT_const_expr, 1);
23006 if (!dwarf_strict)
23008 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23009 DW_AT_inline);
23010 if (inl != -1
23011 && !get_AT (var_die, DW_AT_inline)
23012 && !specialization_p)
23013 add_AT_unsigned (var_die, DW_AT_inline, inl);
23017 /* Generate a DIE to represent a named constant. */
23019 static void
23020 gen_const_die (tree decl, dw_die_ref context_die)
23022 dw_die_ref const_die;
23023 tree type = TREE_TYPE (decl);
23025 const_die = lookup_decl_die (decl);
23026 if (const_die)
23027 return;
23029 const_die = new_die (DW_TAG_constant, context_die, decl);
23030 equate_decl_number_to_die (decl, const_die);
23031 add_name_and_src_coords_attributes (const_die, decl);
23032 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23033 if (TREE_PUBLIC (decl))
23034 add_AT_flag (const_die, DW_AT_external, 1);
23035 if (DECL_ARTIFICIAL (decl))
23036 add_AT_flag (const_die, DW_AT_artificial, 1);
23037 tree_add_const_value_attribute_for_decl (const_die, decl);
23040 /* Generate a DIE to represent a label identifier. */
23042 static void
23043 gen_label_die (tree decl, dw_die_ref context_die)
23045 tree origin = decl_ultimate_origin (decl);
23046 dw_die_ref lbl_die = lookup_decl_die (decl);
23047 rtx insn;
23048 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23050 if (!lbl_die)
23052 lbl_die = new_die (DW_TAG_label, context_die, decl);
23053 equate_decl_number_to_die (decl, lbl_die);
23055 if (origin != NULL)
23056 add_abstract_origin_attribute (lbl_die, origin);
23057 else
23058 add_name_and_src_coords_attributes (lbl_die, decl);
23061 if (DECL_ABSTRACT_P (decl))
23062 equate_decl_number_to_die (decl, lbl_die);
23063 else if (! early_dwarf)
23065 insn = DECL_RTL_IF_SET (decl);
23067 /* Deleted labels are programmer specified labels which have been
23068 eliminated because of various optimizations. We still emit them
23069 here so that it is possible to put breakpoints on them. */
23070 if (insn
23071 && (LABEL_P (insn)
23072 || ((NOTE_P (insn)
23073 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23075 /* When optimization is enabled (via -O) some parts of the compiler
23076 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23077 represent source-level labels which were explicitly declared by
23078 the user. This really shouldn't be happening though, so catch
23079 it if it ever does happen. */
23080 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23082 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23083 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23085 else if (insn
23086 && NOTE_P (insn)
23087 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23088 && CODE_LABEL_NUMBER (insn) != -1)
23090 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23091 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23096 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23097 attributes to the DIE for a block STMT, to describe where the inlined
23098 function was called from. This is similar to add_src_coords_attributes. */
23100 static inline void
23101 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23103 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23105 if (dwarf_version >= 3 || !dwarf_strict)
23107 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23108 add_AT_unsigned (die, DW_AT_call_line, s.line);
23109 if (debug_column_info && s.column)
23110 add_AT_unsigned (die, DW_AT_call_column, s.column);
23115 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23116 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23118 static inline void
23119 add_high_low_attributes (tree stmt, dw_die_ref die)
23121 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23123 if (BLOCK_FRAGMENT_CHAIN (stmt)
23124 && (dwarf_version >= 3 || !dwarf_strict))
23126 tree chain, superblock = NULL_TREE;
23127 dw_die_ref pdie;
23128 dw_attr_node *attr = NULL;
23130 if (inlined_function_outer_scope_p (stmt))
23132 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23133 BLOCK_NUMBER (stmt));
23134 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23137 /* Optimize duplicate .debug_ranges lists or even tails of
23138 lists. If this BLOCK has same ranges as its supercontext,
23139 lookup DW_AT_ranges attribute in the supercontext (and
23140 recursively so), verify that the ranges_table contains the
23141 right values and use it instead of adding a new .debug_range. */
23142 for (chain = stmt, pdie = die;
23143 BLOCK_SAME_RANGE (chain);
23144 chain = BLOCK_SUPERCONTEXT (chain))
23146 dw_attr_node *new_attr;
23148 pdie = pdie->die_parent;
23149 if (pdie == NULL)
23150 break;
23151 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23152 break;
23153 new_attr = get_AT (pdie, DW_AT_ranges);
23154 if (new_attr == NULL
23155 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23156 break;
23157 attr = new_attr;
23158 superblock = BLOCK_SUPERCONTEXT (chain);
23160 if (attr != NULL
23161 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23162 == BLOCK_NUMBER (superblock))
23163 && BLOCK_FRAGMENT_CHAIN (superblock))
23165 unsigned long off = attr->dw_attr_val.v.val_offset;
23166 unsigned long supercnt = 0, thiscnt = 0;
23167 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23168 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23170 ++supercnt;
23171 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23172 == BLOCK_NUMBER (chain));
23174 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23175 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23176 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23177 ++thiscnt;
23178 gcc_assert (supercnt >= thiscnt);
23179 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23180 false);
23181 note_rnglist_head (off + supercnt - thiscnt);
23182 return;
23185 unsigned int offset = add_ranges (stmt, true);
23186 add_AT_range_list (die, DW_AT_ranges, offset, false);
23187 note_rnglist_head (offset);
23189 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23190 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23193 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23194 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23195 chain = BLOCK_FRAGMENT_CHAIN (chain);
23197 while (chain);
23198 add_ranges (NULL);
23200 else
23202 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23203 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23204 BLOCK_NUMBER (stmt));
23205 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23206 BLOCK_NUMBER (stmt));
23207 add_AT_low_high_pc (die, label, label_high, false);
23211 /* Generate a DIE for a lexical block. */
23213 static void
23214 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23216 dw_die_ref old_die = BLOCK_DIE (stmt);
23217 dw_die_ref stmt_die = NULL;
23218 if (!old_die)
23220 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23221 BLOCK_DIE (stmt) = stmt_die;
23224 if (BLOCK_ABSTRACT (stmt))
23226 if (old_die)
23228 /* This must have been generated early and it won't even
23229 need location information since it's a DW_AT_inline
23230 function. */
23231 if (flag_checking)
23232 for (dw_die_ref c = context_die; c; c = c->die_parent)
23233 if (c->die_tag == DW_TAG_inlined_subroutine
23234 || c->die_tag == DW_TAG_subprogram)
23236 gcc_assert (get_AT (c, DW_AT_inline));
23237 break;
23239 return;
23242 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23244 /* If this is an inlined instance, create a new lexical die for
23245 anything below to attach DW_AT_abstract_origin to. */
23246 if (old_die)
23248 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23249 BLOCK_DIE (stmt) = stmt_die;
23250 old_die = NULL;
23253 tree origin = block_ultimate_origin (stmt);
23254 if (origin != NULL_TREE && origin != stmt)
23255 add_abstract_origin_attribute (stmt_die, origin);
23258 if (old_die)
23259 stmt_die = old_die;
23261 /* A non abstract block whose blocks have already been reordered
23262 should have the instruction range for this block. If so, set the
23263 high/low attributes. */
23264 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23266 gcc_assert (stmt_die);
23267 add_high_low_attributes (stmt, stmt_die);
23270 decls_for_scope (stmt, stmt_die);
23273 /* Generate a DIE for an inlined subprogram. */
23275 static void
23276 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23278 tree decl;
23280 /* The instance of function that is effectively being inlined shall not
23281 be abstract. */
23282 gcc_assert (! BLOCK_ABSTRACT (stmt));
23284 decl = block_ultimate_origin (stmt);
23286 /* Make sure any inlined functions are known to be inlineable. */
23287 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23288 || cgraph_function_possibly_inlined_p (decl));
23290 if (! BLOCK_ABSTRACT (stmt))
23292 dw_die_ref subr_die
23293 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23295 if (call_arg_locations)
23296 BLOCK_DIE (stmt) = subr_die;
23297 add_abstract_origin_attribute (subr_die, decl);
23298 if (TREE_ASM_WRITTEN (stmt))
23299 add_high_low_attributes (stmt, subr_die);
23300 add_call_src_coords_attributes (stmt, subr_die);
23302 decls_for_scope (stmt, subr_die);
23306 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23307 the comment for VLR_CONTEXT. */
23309 static void
23310 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23312 dw_die_ref decl_die;
23314 if (TREE_TYPE (decl) == error_mark_node)
23315 return;
23317 decl_die = new_die (DW_TAG_member, context_die, decl);
23318 add_name_and_src_coords_attributes (decl_die, decl);
23319 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23320 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23321 context_die);
23323 if (DECL_BIT_FIELD_TYPE (decl))
23325 add_byte_size_attribute (decl_die, decl);
23326 add_bit_size_attribute (decl_die, decl);
23327 add_bit_offset_attribute (decl_die, decl, ctx);
23330 add_alignment_attribute (decl_die, decl);
23332 /* If we have a variant part offset, then we are supposed to process a member
23333 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23334 trees. */
23335 gcc_assert (ctx->variant_part_offset == NULL_TREE
23336 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23337 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23338 add_data_member_location_attribute (decl_die, decl, ctx);
23340 if (DECL_ARTIFICIAL (decl))
23341 add_AT_flag (decl_die, DW_AT_artificial, 1);
23343 add_accessibility_attribute (decl_die, decl);
23345 /* Equate decl number to die, so that we can look up this decl later on. */
23346 equate_decl_number_to_die (decl, decl_die);
23349 /* Generate a DIE for a pointer to a member type. TYPE can be an
23350 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23351 pointer to member function. */
23353 static void
23354 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23356 if (lookup_type_die (type))
23357 return;
23359 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23360 scope_die_for (type, context_die), type);
23362 equate_type_number_to_die (type, ptr_die);
23363 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23364 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23365 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23366 context_die);
23367 add_alignment_attribute (ptr_die, type);
23369 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23370 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23372 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23373 add_AT_loc (ptr_die, DW_AT_use_location, op);
23377 static char *producer_string;
23379 /* Return a heap allocated producer string including command line options
23380 if -grecord-gcc-switches. */
23382 static char *
23383 gen_producer_string (void)
23385 size_t j;
23386 auto_vec<const char *> switches;
23387 const char *language_string = lang_hooks.name;
23388 char *producer, *tail;
23389 const char *p;
23390 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23391 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23393 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23394 switch (save_decoded_options[j].opt_index)
23396 case OPT_o:
23397 case OPT_d:
23398 case OPT_dumpbase:
23399 case OPT_dumpdir:
23400 case OPT_auxbase:
23401 case OPT_auxbase_strip:
23402 case OPT_quiet:
23403 case OPT_version:
23404 case OPT_v:
23405 case OPT_w:
23406 case OPT_L:
23407 case OPT_D:
23408 case OPT_I:
23409 case OPT_U:
23410 case OPT_SPECIAL_unknown:
23411 case OPT_SPECIAL_ignore:
23412 case OPT_SPECIAL_program_name:
23413 case OPT_SPECIAL_input_file:
23414 case OPT_grecord_gcc_switches:
23415 case OPT__output_pch_:
23416 case OPT_fdiagnostics_show_location_:
23417 case OPT_fdiagnostics_show_option:
23418 case OPT_fdiagnostics_show_caret:
23419 case OPT_fdiagnostics_color_:
23420 case OPT_fverbose_asm:
23421 case OPT____:
23422 case OPT__sysroot_:
23423 case OPT_nostdinc:
23424 case OPT_nostdinc__:
23425 case OPT_fpreprocessed:
23426 case OPT_fltrans_output_list_:
23427 case OPT_fresolution_:
23428 case OPT_fdebug_prefix_map_:
23429 case OPT_fcompare_debug:
23430 /* Ignore these. */
23431 continue;
23432 default:
23433 if (cl_options[save_decoded_options[j].opt_index].flags
23434 & CL_NO_DWARF_RECORD)
23435 continue;
23436 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23437 == '-');
23438 switch (save_decoded_options[j].canonical_option[0][1])
23440 case 'M':
23441 case 'i':
23442 case 'W':
23443 continue;
23444 case 'f':
23445 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23446 "dump", 4) == 0)
23447 continue;
23448 break;
23449 default:
23450 break;
23452 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23453 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23454 break;
23457 producer = XNEWVEC (char, plen + 1 + len + 1);
23458 tail = producer;
23459 sprintf (tail, "%s %s", language_string, version_string);
23460 tail += plen;
23462 FOR_EACH_VEC_ELT (switches, j, p)
23464 len = strlen (p);
23465 *tail = ' ';
23466 memcpy (tail + 1, p, len);
23467 tail += len + 1;
23470 *tail = '\0';
23471 return producer;
23474 /* Given a C and/or C++ language/version string return the "highest".
23475 C++ is assumed to be "higher" than C in this case. Used for merging
23476 LTO translation unit languages. */
23477 static const char *
23478 highest_c_language (const char *lang1, const char *lang2)
23480 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23481 return "GNU C++17";
23482 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23483 return "GNU C++14";
23484 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23485 return "GNU C++11";
23486 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23487 return "GNU C++98";
23489 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23490 return "GNU C17";
23491 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23492 return "GNU C11";
23493 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23494 return "GNU C99";
23495 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23496 return "GNU C89";
23498 gcc_unreachable ();
23502 /* Generate the DIE for the compilation unit. */
23504 static dw_die_ref
23505 gen_compile_unit_die (const char *filename)
23507 dw_die_ref die;
23508 const char *language_string = lang_hooks.name;
23509 int language;
23511 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23513 if (filename)
23515 add_name_attribute (die, filename);
23516 /* Don't add cwd for <built-in>. */
23517 if (filename[0] != '<')
23518 add_comp_dir_attribute (die);
23521 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23523 /* If our producer is LTO try to figure out a common language to use
23524 from the global list of translation units. */
23525 if (strcmp (language_string, "GNU GIMPLE") == 0)
23527 unsigned i;
23528 tree t;
23529 const char *common_lang = NULL;
23531 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23533 if (!TRANSLATION_UNIT_LANGUAGE (t))
23534 continue;
23535 if (!common_lang)
23536 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23537 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23539 else if (strncmp (common_lang, "GNU C", 5) == 0
23540 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23541 /* Mixing C and C++ is ok, use C++ in that case. */
23542 common_lang = highest_c_language (common_lang,
23543 TRANSLATION_UNIT_LANGUAGE (t));
23544 else
23546 /* Fall back to C. */
23547 common_lang = NULL;
23548 break;
23552 if (common_lang)
23553 language_string = common_lang;
23556 language = DW_LANG_C;
23557 if (strncmp (language_string, "GNU C", 5) == 0
23558 && ISDIGIT (language_string[5]))
23560 language = DW_LANG_C89;
23561 if (dwarf_version >= 3 || !dwarf_strict)
23563 if (strcmp (language_string, "GNU C89") != 0)
23564 language = DW_LANG_C99;
23566 if (dwarf_version >= 5 /* || !dwarf_strict */)
23567 if (strcmp (language_string, "GNU C11") == 0
23568 || strcmp (language_string, "GNU C17") == 0)
23569 language = DW_LANG_C11;
23572 else if (strncmp (language_string, "GNU C++", 7) == 0)
23574 language = DW_LANG_C_plus_plus;
23575 if (dwarf_version >= 5 /* || !dwarf_strict */)
23577 if (strcmp (language_string, "GNU C++11") == 0)
23578 language = DW_LANG_C_plus_plus_11;
23579 else if (strcmp (language_string, "GNU C++14") == 0)
23580 language = DW_LANG_C_plus_plus_14;
23581 else if (strcmp (language_string, "GNU C++17") == 0)
23582 /* For now. */
23583 language = DW_LANG_C_plus_plus_14;
23586 else if (strcmp (language_string, "GNU F77") == 0)
23587 language = DW_LANG_Fortran77;
23588 else if (dwarf_version >= 3 || !dwarf_strict)
23590 if (strcmp (language_string, "GNU Ada") == 0)
23591 language = DW_LANG_Ada95;
23592 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23594 language = DW_LANG_Fortran95;
23595 if (dwarf_version >= 5 /* || !dwarf_strict */)
23597 if (strcmp (language_string, "GNU Fortran2003") == 0)
23598 language = DW_LANG_Fortran03;
23599 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23600 language = DW_LANG_Fortran08;
23603 else if (strcmp (language_string, "GNU Objective-C") == 0)
23604 language = DW_LANG_ObjC;
23605 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23606 language = DW_LANG_ObjC_plus_plus;
23607 else if (dwarf_version >= 5 || !dwarf_strict)
23609 if (strcmp (language_string, "GNU Go") == 0)
23610 language = DW_LANG_Go;
23613 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23614 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23615 language = DW_LANG_Fortran90;
23617 add_AT_unsigned (die, DW_AT_language, language);
23619 switch (language)
23621 case DW_LANG_Fortran77:
23622 case DW_LANG_Fortran90:
23623 case DW_LANG_Fortran95:
23624 case DW_LANG_Fortran03:
23625 case DW_LANG_Fortran08:
23626 /* Fortran has case insensitive identifiers and the front-end
23627 lowercases everything. */
23628 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23629 break;
23630 default:
23631 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23632 break;
23634 return die;
23637 /* Generate the DIE for a base class. */
23639 static void
23640 gen_inheritance_die (tree binfo, tree access, tree type,
23641 dw_die_ref context_die)
23643 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23644 struct vlr_context ctx = { type, NULL };
23646 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23647 context_die);
23648 add_data_member_location_attribute (die, binfo, &ctx);
23650 if (BINFO_VIRTUAL_P (binfo))
23651 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23653 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23654 children, otherwise the default is DW_ACCESS_public. In DWARF2
23655 the default has always been DW_ACCESS_private. */
23656 if (access == access_public_node)
23658 if (dwarf_version == 2
23659 || context_die->die_tag == DW_TAG_class_type)
23660 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23662 else if (access == access_protected_node)
23663 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23664 else if (dwarf_version > 2
23665 && context_die->die_tag != DW_TAG_class_type)
23666 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23669 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23670 structure. */
23671 static bool
23672 is_variant_part (tree decl)
23674 return (TREE_CODE (decl) == FIELD_DECL
23675 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23678 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23679 return the FIELD_DECL. Return NULL_TREE otherwise. */
23681 static tree
23682 analyze_discr_in_predicate (tree operand, tree struct_type)
23684 bool continue_stripping = true;
23685 while (continue_stripping)
23686 switch (TREE_CODE (operand))
23688 CASE_CONVERT:
23689 operand = TREE_OPERAND (operand, 0);
23690 break;
23691 default:
23692 continue_stripping = false;
23693 break;
23696 /* Match field access to members of struct_type only. */
23697 if (TREE_CODE (operand) == COMPONENT_REF
23698 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23699 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23700 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23701 return TREE_OPERAND (operand, 1);
23702 else
23703 return NULL_TREE;
23706 /* Check that SRC is a constant integer that can be represented as a native
23707 integer constant (either signed or unsigned). If so, store it into DEST and
23708 return true. Return false otherwise. */
23710 static bool
23711 get_discr_value (tree src, dw_discr_value *dest)
23713 tree discr_type = TREE_TYPE (src);
23715 if (lang_hooks.types.get_debug_type)
23717 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23718 if (debug_type != NULL)
23719 discr_type = debug_type;
23722 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23723 return false;
23725 /* Signedness can vary between the original type and the debug type. This
23726 can happen for character types in Ada for instance: the character type
23727 used for code generation can be signed, to be compatible with the C one,
23728 but from a debugger point of view, it must be unsigned. */
23729 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23730 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23732 if (is_orig_unsigned != is_debug_unsigned)
23733 src = fold_convert (discr_type, src);
23735 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23736 return false;
23738 dest->pos = is_debug_unsigned;
23739 if (is_debug_unsigned)
23740 dest->v.uval = tree_to_uhwi (src);
23741 else
23742 dest->v.sval = tree_to_shwi (src);
23744 return true;
23747 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23748 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23749 store NULL_TREE in DISCR_DECL. Otherwise:
23751 - store the discriminant field in STRUCT_TYPE that controls the variant
23752 part to *DISCR_DECL
23754 - put in *DISCR_LISTS_P an array where for each variant, the item
23755 represents the corresponding matching list of discriminant values.
23757 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23758 the above array.
23760 Note that when the array is allocated (i.e. when the analysis is
23761 successful), it is up to the caller to free the array. */
23763 static void
23764 analyze_variants_discr (tree variant_part_decl,
23765 tree struct_type,
23766 tree *discr_decl,
23767 dw_discr_list_ref **discr_lists_p,
23768 unsigned *discr_lists_length)
23770 tree variant_part_type = TREE_TYPE (variant_part_decl);
23771 tree variant;
23772 dw_discr_list_ref *discr_lists;
23773 unsigned i;
23775 /* Compute how many variants there are in this variant part. */
23776 *discr_lists_length = 0;
23777 for (variant = TYPE_FIELDS (variant_part_type);
23778 variant != NULL_TREE;
23779 variant = DECL_CHAIN (variant))
23780 ++*discr_lists_length;
23782 *discr_decl = NULL_TREE;
23783 *discr_lists_p
23784 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23785 sizeof (**discr_lists_p));
23786 discr_lists = *discr_lists_p;
23788 /* And then analyze all variants to extract discriminant information for all
23789 of them. This analysis is conservative: as soon as we detect something we
23790 do not support, abort everything and pretend we found nothing. */
23791 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23792 variant != NULL_TREE;
23793 variant = DECL_CHAIN (variant), ++i)
23795 tree match_expr = DECL_QUALIFIER (variant);
23797 /* Now, try to analyze the predicate and deduce a discriminant for
23798 it. */
23799 if (match_expr == boolean_true_node)
23800 /* Typically happens for the default variant: it matches all cases that
23801 previous variants rejected. Don't output any matching value for
23802 this one. */
23803 continue;
23805 /* The following loop tries to iterate over each discriminant
23806 possibility: single values or ranges. */
23807 while (match_expr != NULL_TREE)
23809 tree next_round_match_expr;
23810 tree candidate_discr = NULL_TREE;
23811 dw_discr_list_ref new_node = NULL;
23813 /* Possibilities are matched one after the other by nested
23814 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23815 continue with the rest at next iteration. */
23816 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23818 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23819 match_expr = TREE_OPERAND (match_expr, 1);
23821 else
23822 next_round_match_expr = NULL_TREE;
23824 if (match_expr == boolean_false_node)
23825 /* This sub-expression matches nothing: just wait for the next
23826 one. */
23829 else if (TREE_CODE (match_expr) == EQ_EXPR)
23831 /* We are matching: <discr_field> == <integer_cst>
23832 This sub-expression matches a single value. */
23833 tree integer_cst = TREE_OPERAND (match_expr, 1);
23835 candidate_discr
23836 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23837 struct_type);
23839 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23840 if (!get_discr_value (integer_cst,
23841 &new_node->dw_discr_lower_bound))
23842 goto abort;
23843 new_node->dw_discr_range = false;
23846 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23848 /* We are matching:
23849 <discr_field> > <integer_cst>
23850 && <discr_field> < <integer_cst>.
23851 This sub-expression matches the range of values between the
23852 two matched integer constants. Note that comparisons can be
23853 inclusive or exclusive. */
23854 tree candidate_discr_1, candidate_discr_2;
23855 tree lower_cst, upper_cst;
23856 bool lower_cst_included, upper_cst_included;
23857 tree lower_op = TREE_OPERAND (match_expr, 0);
23858 tree upper_op = TREE_OPERAND (match_expr, 1);
23860 /* When the comparison is exclusive, the integer constant is not
23861 the discriminant range bound we are looking for: we will have
23862 to increment or decrement it. */
23863 if (TREE_CODE (lower_op) == GE_EXPR)
23864 lower_cst_included = true;
23865 else if (TREE_CODE (lower_op) == GT_EXPR)
23866 lower_cst_included = false;
23867 else
23868 goto abort;
23870 if (TREE_CODE (upper_op) == LE_EXPR)
23871 upper_cst_included = true;
23872 else if (TREE_CODE (upper_op) == LT_EXPR)
23873 upper_cst_included = false;
23874 else
23875 goto abort;
23877 /* Extract the discriminant from the first operand and check it
23878 is consistant with the same analysis in the second
23879 operand. */
23880 candidate_discr_1
23881 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23882 struct_type);
23883 candidate_discr_2
23884 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23885 struct_type);
23886 if (candidate_discr_1 == candidate_discr_2)
23887 candidate_discr = candidate_discr_1;
23888 else
23889 goto abort;
23891 /* Extract bounds from both. */
23892 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23893 lower_cst = TREE_OPERAND (lower_op, 1);
23894 upper_cst = TREE_OPERAND (upper_op, 1);
23896 if (!lower_cst_included)
23897 lower_cst
23898 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23899 build_int_cst (TREE_TYPE (lower_cst), 1));
23900 if (!upper_cst_included)
23901 upper_cst
23902 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23903 build_int_cst (TREE_TYPE (upper_cst), 1));
23905 if (!get_discr_value (lower_cst,
23906 &new_node->dw_discr_lower_bound)
23907 || !get_discr_value (upper_cst,
23908 &new_node->dw_discr_upper_bound))
23909 goto abort;
23911 new_node->dw_discr_range = true;
23914 else
23915 /* Unsupported sub-expression: we cannot determine the set of
23916 matching discriminant values. Abort everything. */
23917 goto abort;
23919 /* If the discriminant info is not consistant with what we saw so
23920 far, consider the analysis failed and abort everything. */
23921 if (candidate_discr == NULL_TREE
23922 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23923 goto abort;
23924 else
23925 *discr_decl = candidate_discr;
23927 if (new_node != NULL)
23929 new_node->dw_discr_next = discr_lists[i];
23930 discr_lists[i] = new_node;
23932 match_expr = next_round_match_expr;
23936 /* If we reach this point, we could match everything we were interested
23937 in. */
23938 return;
23940 abort:
23941 /* Clean all data structure and return no result. */
23942 free (*discr_lists_p);
23943 *discr_lists_p = NULL;
23944 *discr_decl = NULL_TREE;
23947 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23948 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23949 under CONTEXT_DIE.
23951 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23952 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23953 this type, which are record types, represent the available variants and each
23954 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23955 values are inferred from these attributes.
23957 In trees, the offsets for the fields inside these sub-records are relative
23958 to the variant part itself, whereas the corresponding DIEs should have
23959 offset attributes that are relative to the embedding record base address.
23960 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23961 must be an expression that computes the offset of the variant part to
23962 describe in DWARF. */
23964 static void
23965 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23966 dw_die_ref context_die)
23968 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23969 tree variant_part_offset = vlr_ctx->variant_part_offset;
23970 struct loc_descr_context ctx = {
23971 vlr_ctx->struct_type, /* context_type */
23972 NULL_TREE, /* base_decl */
23973 NULL, /* dpi */
23974 false, /* placeholder_arg */
23975 false /* placeholder_seen */
23978 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23979 NULL_TREE if there is no such field. */
23980 tree discr_decl = NULL_TREE;
23981 dw_discr_list_ref *discr_lists;
23982 unsigned discr_lists_length = 0;
23983 unsigned i;
23985 dw_die_ref dwarf_proc_die = NULL;
23986 dw_die_ref variant_part_die
23987 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23989 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23991 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23992 &discr_decl, &discr_lists, &discr_lists_length);
23994 if (discr_decl != NULL_TREE)
23996 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23998 if (discr_die)
23999 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24000 else
24001 /* We have no DIE for the discriminant, so just discard all
24002 discrimimant information in the output. */
24003 discr_decl = NULL_TREE;
24006 /* If the offset for this variant part is more complex than a constant,
24007 create a DWARF procedure for it so that we will not have to generate DWARF
24008 expressions for it for each member. */
24009 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24010 && (dwarf_version >= 3 || !dwarf_strict))
24012 const tree dwarf_proc_fndecl
24013 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24014 build_function_type (TREE_TYPE (variant_part_offset),
24015 NULL_TREE));
24016 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24017 const dw_loc_descr_ref dwarf_proc_body
24018 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24020 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24021 dwarf_proc_fndecl, context_die);
24022 if (dwarf_proc_die != NULL)
24023 variant_part_offset = dwarf_proc_call;
24026 /* Output DIEs for all variants. */
24027 i = 0;
24028 for (tree variant = TYPE_FIELDS (variant_part_type);
24029 variant != NULL_TREE;
24030 variant = DECL_CHAIN (variant), ++i)
24032 tree variant_type = TREE_TYPE (variant);
24033 dw_die_ref variant_die;
24035 /* All variants (i.e. members of a variant part) are supposed to be
24036 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24037 under these records. */
24038 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24040 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24041 equate_decl_number_to_die (variant, variant_die);
24043 /* Output discriminant values this variant matches, if any. */
24044 if (discr_decl == NULL || discr_lists[i] == NULL)
24045 /* In the case we have discriminant information at all, this is
24046 probably the default variant: as the standard says, don't
24047 output any discriminant value/list attribute. */
24049 else if (discr_lists[i]->dw_discr_next == NULL
24050 && !discr_lists[i]->dw_discr_range)
24051 /* If there is only one accepted value, don't bother outputting a
24052 list. */
24053 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24054 else
24055 add_discr_list (variant_die, discr_lists[i]);
24057 for (tree member = TYPE_FIELDS (variant_type);
24058 member != NULL_TREE;
24059 member = DECL_CHAIN (member))
24061 struct vlr_context vlr_sub_ctx = {
24062 vlr_ctx->struct_type, /* struct_type */
24063 NULL /* variant_part_offset */
24065 if (is_variant_part (member))
24067 /* All offsets for fields inside variant parts are relative to
24068 the top-level embedding RECORD_TYPE's base address. On the
24069 other hand, offsets in GCC's types are relative to the
24070 nested-most variant part. So we have to sum offsets each time
24071 we recurse. */
24073 vlr_sub_ctx.variant_part_offset
24074 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24075 variant_part_offset, byte_position (member));
24076 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24078 else
24080 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24081 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24086 free (discr_lists);
24089 /* Generate a DIE for a class member. */
24091 static void
24092 gen_member_die (tree type, dw_die_ref context_die)
24094 tree member;
24095 tree binfo = TYPE_BINFO (type);
24097 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24099 /* If this is not an incomplete type, output descriptions of each of its
24100 members. Note that as we output the DIEs necessary to represent the
24101 members of this record or union type, we will also be trying to output
24102 DIEs to represent the *types* of those members. However the `type'
24103 function (above) will specifically avoid generating type DIEs for member
24104 types *within* the list of member DIEs for this (containing) type except
24105 for those types (of members) which are explicitly marked as also being
24106 members of this (containing) type themselves. The g++ front- end can
24107 force any given type to be treated as a member of some other (containing)
24108 type by setting the TYPE_CONTEXT of the given (member) type to point to
24109 the TREE node representing the appropriate (containing) type. */
24111 /* First output info about the base classes. */
24112 if (binfo)
24114 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24115 int i;
24116 tree base;
24118 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24119 gen_inheritance_die (base,
24120 (accesses ? (*accesses)[i] : access_public_node),
24121 type,
24122 context_die);
24125 /* Now output info about the data members and type members. */
24126 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24128 struct vlr_context vlr_ctx = { type, NULL_TREE };
24129 bool static_inline_p
24130 = (TREE_STATIC (member)
24131 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24132 != -1));
24134 /* Ignore clones. */
24135 if (DECL_ABSTRACT_ORIGIN (member))
24136 continue;
24138 /* If we thought we were generating minimal debug info for TYPE
24139 and then changed our minds, some of the member declarations
24140 may have already been defined. Don't define them again, but
24141 do put them in the right order. */
24143 if (dw_die_ref child = lookup_decl_die (member))
24145 /* Handle inline static data members, which only have in-class
24146 declarations. */
24147 dw_die_ref ref = NULL;
24148 if (child->die_tag == DW_TAG_variable
24149 && child->die_parent == comp_unit_die ())
24151 ref = get_AT_ref (child, DW_AT_specification);
24152 /* For C++17 inline static data members followed by redundant
24153 out of class redeclaration, we might get here with
24154 child being the DIE created for the out of class
24155 redeclaration and with its DW_AT_specification being
24156 the DIE created for in-class definition. We want to
24157 reparent the latter, and don't want to create another
24158 DIE with DW_AT_specification in that case, because
24159 we already have one. */
24160 if (ref
24161 && static_inline_p
24162 && ref->die_tag == DW_TAG_variable
24163 && ref->die_parent == comp_unit_die ()
24164 && get_AT (ref, DW_AT_specification) == NULL)
24166 child = ref;
24167 ref = NULL;
24168 static_inline_p = false;
24172 if (child->die_tag == DW_TAG_variable
24173 && child->die_parent == comp_unit_die ()
24174 && ref == NULL)
24176 reparent_child (child, context_die);
24177 if (dwarf_version < 5)
24178 child->die_tag = DW_TAG_member;
24180 else
24181 splice_child_die (context_die, child);
24184 /* Do not generate standard DWARF for variant parts if we are generating
24185 the corresponding GNAT encodings: DIEs generated for both would
24186 conflict in our mappings. */
24187 else if (is_variant_part (member)
24188 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24190 vlr_ctx.variant_part_offset = byte_position (member);
24191 gen_variant_part (member, &vlr_ctx, context_die);
24193 else
24195 vlr_ctx.variant_part_offset = NULL_TREE;
24196 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24199 /* For C++ inline static data members emit immediately a DW_TAG_variable
24200 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24201 DW_AT_specification. */
24202 if (static_inline_p)
24204 int old_extern = DECL_EXTERNAL (member);
24205 DECL_EXTERNAL (member) = 0;
24206 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24207 DECL_EXTERNAL (member) = old_extern;
24212 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24213 is set, we pretend that the type was never defined, so we only get the
24214 member DIEs needed by later specification DIEs. */
24216 static void
24217 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24218 enum debug_info_usage usage)
24220 if (TREE_ASM_WRITTEN (type))
24222 /* Fill in the bound of variable-length fields in late dwarf if
24223 still incomplete. */
24224 if (!early_dwarf && variably_modified_type_p (type, NULL))
24225 for (tree member = TYPE_FIELDS (type);
24226 member;
24227 member = DECL_CHAIN (member))
24228 fill_variable_array_bounds (TREE_TYPE (member));
24229 return;
24232 dw_die_ref type_die = lookup_type_die (type);
24233 dw_die_ref scope_die = 0;
24234 int nested = 0;
24235 int complete = (TYPE_SIZE (type)
24236 && (! TYPE_STUB_DECL (type)
24237 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24238 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24239 complete = complete && should_emit_struct_debug (type, usage);
24241 if (type_die && ! complete)
24242 return;
24244 if (TYPE_CONTEXT (type) != NULL_TREE
24245 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24246 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24247 nested = 1;
24249 scope_die = scope_die_for (type, context_die);
24251 /* Generate child dies for template paramaters. */
24252 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24253 schedule_generic_params_dies_gen (type);
24255 if (! type_die || (nested && is_cu_die (scope_die)))
24256 /* First occurrence of type or toplevel definition of nested class. */
24258 dw_die_ref old_die = type_die;
24260 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24261 ? record_type_tag (type) : DW_TAG_union_type,
24262 scope_die, type);
24263 equate_type_number_to_die (type, type_die);
24264 if (old_die)
24265 add_AT_specification (type_die, old_die);
24266 else
24267 add_name_attribute (type_die, type_tag (type));
24269 else
24270 remove_AT (type_die, DW_AT_declaration);
24272 /* If this type has been completed, then give it a byte_size attribute and
24273 then give a list of members. */
24274 if (complete && !ns_decl)
24276 /* Prevent infinite recursion in cases where the type of some member of
24277 this type is expressed in terms of this type itself. */
24278 TREE_ASM_WRITTEN (type) = 1;
24279 add_byte_size_attribute (type_die, type);
24280 add_alignment_attribute (type_die, type);
24281 if (TYPE_STUB_DECL (type) != NULL_TREE)
24283 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24284 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24287 /* If the first reference to this type was as the return type of an
24288 inline function, then it may not have a parent. Fix this now. */
24289 if (type_die->die_parent == NULL)
24290 add_child_die (scope_die, type_die);
24292 push_decl_scope (type);
24293 gen_member_die (type, type_die);
24294 pop_decl_scope ();
24296 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24297 if (TYPE_ARTIFICIAL (type))
24298 add_AT_flag (type_die, DW_AT_artificial, 1);
24300 /* GNU extension: Record what type our vtable lives in. */
24301 if (TYPE_VFIELD (type))
24303 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24305 gen_type_die (vtype, context_die);
24306 add_AT_die_ref (type_die, DW_AT_containing_type,
24307 lookup_type_die (vtype));
24310 else
24312 add_AT_flag (type_die, DW_AT_declaration, 1);
24314 /* We don't need to do this for function-local types. */
24315 if (TYPE_STUB_DECL (type)
24316 && ! decl_function_context (TYPE_STUB_DECL (type)))
24317 vec_safe_push (incomplete_types, type);
24320 if (get_AT (type_die, DW_AT_name))
24321 add_pubtype (type, type_die);
24324 /* Generate a DIE for a subroutine _type_. */
24326 static void
24327 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24329 tree return_type = TREE_TYPE (type);
24330 dw_die_ref subr_die
24331 = new_die (DW_TAG_subroutine_type,
24332 scope_die_for (type, context_die), type);
24334 equate_type_number_to_die (type, subr_die);
24335 add_prototyped_attribute (subr_die, type);
24336 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24337 context_die);
24338 add_alignment_attribute (subr_die, type);
24339 gen_formal_types_die (type, subr_die);
24341 if (get_AT (subr_die, DW_AT_name))
24342 add_pubtype (type, subr_die);
24343 if ((dwarf_version >= 5 || !dwarf_strict)
24344 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24345 add_AT_flag (subr_die, DW_AT_reference, 1);
24346 if ((dwarf_version >= 5 || !dwarf_strict)
24347 && lang_hooks.types.type_dwarf_attribute (type,
24348 DW_AT_rvalue_reference) != -1)
24349 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24352 /* Generate a DIE for a type definition. */
24354 static void
24355 gen_typedef_die (tree decl, dw_die_ref context_die)
24357 dw_die_ref type_die;
24358 tree type;
24360 if (TREE_ASM_WRITTEN (decl))
24362 if (DECL_ORIGINAL_TYPE (decl))
24363 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24364 return;
24367 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24368 checks in process_scope_var and modified_type_die), this should be called
24369 only for original types. */
24370 gcc_assert (decl_ultimate_origin (decl) == NULL
24371 || decl_ultimate_origin (decl) == decl);
24373 TREE_ASM_WRITTEN (decl) = 1;
24374 type_die = new_die (DW_TAG_typedef, context_die, decl);
24376 add_name_and_src_coords_attributes (type_die, decl);
24377 if (DECL_ORIGINAL_TYPE (decl))
24379 type = DECL_ORIGINAL_TYPE (decl);
24380 if (type == error_mark_node)
24381 return;
24383 gcc_assert (type != TREE_TYPE (decl));
24384 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24386 else
24388 type = TREE_TYPE (decl);
24389 if (type == error_mark_node)
24390 return;
24392 if (is_naming_typedef_decl (TYPE_NAME (type)))
24394 /* Here, we are in the case of decl being a typedef naming
24395 an anonymous type, e.g:
24396 typedef struct {...} foo;
24397 In that case TREE_TYPE (decl) is not a typedef variant
24398 type and TYPE_NAME of the anonymous type is set to the
24399 TYPE_DECL of the typedef. This construct is emitted by
24400 the C++ FE.
24402 TYPE is the anonymous struct named by the typedef
24403 DECL. As we need the DW_AT_type attribute of the
24404 DW_TAG_typedef to point to the DIE of TYPE, let's
24405 generate that DIE right away. add_type_attribute
24406 called below will then pick (via lookup_type_die) that
24407 anonymous struct DIE. */
24408 if (!TREE_ASM_WRITTEN (type))
24409 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24411 /* This is a GNU Extension. We are adding a
24412 DW_AT_linkage_name attribute to the DIE of the
24413 anonymous struct TYPE. The value of that attribute
24414 is the name of the typedef decl naming the anonymous
24415 struct. This greatly eases the work of consumers of
24416 this debug info. */
24417 add_linkage_name_raw (lookup_type_die (type), decl);
24421 add_type_attribute (type_die, type, decl_quals (decl), false,
24422 context_die);
24424 if (is_naming_typedef_decl (decl))
24425 /* We want that all subsequent calls to lookup_type_die with
24426 TYPE in argument yield the DW_TAG_typedef we have just
24427 created. */
24428 equate_type_number_to_die (type, type_die);
24430 add_alignment_attribute (type_die, TREE_TYPE (decl));
24432 add_accessibility_attribute (type_die, decl);
24434 if (DECL_ABSTRACT_P (decl))
24435 equate_decl_number_to_die (decl, type_die);
24437 if (get_AT (type_die, DW_AT_name))
24438 add_pubtype (decl, type_die);
24441 /* Generate a DIE for a struct, class, enum or union type. */
24443 static void
24444 gen_tagged_type_die (tree type,
24445 dw_die_ref context_die,
24446 enum debug_info_usage usage)
24448 int need_pop;
24450 if (type == NULL_TREE
24451 || !is_tagged_type (type))
24452 return;
24454 if (TREE_ASM_WRITTEN (type))
24455 need_pop = 0;
24456 /* If this is a nested type whose containing class hasn't been written
24457 out yet, writing it out will cover this one, too. This does not apply
24458 to instantiations of member class templates; they need to be added to
24459 the containing class as they are generated. FIXME: This hurts the
24460 idea of combining type decls from multiple TUs, since we can't predict
24461 what set of template instantiations we'll get. */
24462 else if (TYPE_CONTEXT (type)
24463 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24464 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24466 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24468 if (TREE_ASM_WRITTEN (type))
24469 return;
24471 /* If that failed, attach ourselves to the stub. */
24472 push_decl_scope (TYPE_CONTEXT (type));
24473 context_die = lookup_type_die (TYPE_CONTEXT (type));
24474 need_pop = 1;
24476 else if (TYPE_CONTEXT (type) != NULL_TREE
24477 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24479 /* If this type is local to a function that hasn't been written
24480 out yet, use a NULL context for now; it will be fixed up in
24481 decls_for_scope. */
24482 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24483 /* A declaration DIE doesn't count; nested types need to go in the
24484 specification. */
24485 if (context_die && is_declaration_die (context_die))
24486 context_die = NULL;
24487 need_pop = 0;
24489 else
24491 context_die = declare_in_namespace (type, context_die);
24492 need_pop = 0;
24495 if (TREE_CODE (type) == ENUMERAL_TYPE)
24497 /* This might have been written out by the call to
24498 declare_in_namespace. */
24499 if (!TREE_ASM_WRITTEN (type))
24500 gen_enumeration_type_die (type, context_die);
24502 else
24503 gen_struct_or_union_type_die (type, context_die, usage);
24505 if (need_pop)
24506 pop_decl_scope ();
24508 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24509 it up if it is ever completed. gen_*_type_die will set it for us
24510 when appropriate. */
24513 /* Generate a type description DIE. */
24515 static void
24516 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24517 enum debug_info_usage usage)
24519 struct array_descr_info info;
24521 if (type == NULL_TREE || type == error_mark_node)
24522 return;
24524 if (flag_checking && type)
24525 verify_type (type);
24527 if (TYPE_NAME (type) != NULL_TREE
24528 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24529 && is_redundant_typedef (TYPE_NAME (type))
24530 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24531 /* The DECL of this type is a typedef we don't want to emit debug
24532 info for but we want debug info for its underlying typedef.
24533 This can happen for e.g, the injected-class-name of a C++
24534 type. */
24535 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24537 /* If TYPE is a typedef type variant, let's generate debug info
24538 for the parent typedef which TYPE is a type of. */
24539 if (typedef_variant_p (type))
24541 if (TREE_ASM_WRITTEN (type))
24542 return;
24544 tree name = TYPE_NAME (type);
24545 tree origin = decl_ultimate_origin (name);
24546 if (origin != NULL && origin != name)
24548 gen_decl_die (origin, NULL, NULL, context_die);
24549 return;
24552 /* Prevent broken recursion; we can't hand off to the same type. */
24553 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24555 /* Give typedefs the right scope. */
24556 context_die = scope_die_for (type, context_die);
24558 TREE_ASM_WRITTEN (type) = 1;
24560 gen_decl_die (name, NULL, NULL, context_die);
24561 return;
24564 /* If type is an anonymous tagged type named by a typedef, let's
24565 generate debug info for the typedef. */
24566 if (is_naming_typedef_decl (TYPE_NAME (type)))
24568 /* Use the DIE of the containing namespace as the parent DIE of
24569 the type description DIE we want to generate. */
24570 if (DECL_CONTEXT (TYPE_NAME (type))
24571 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24572 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24574 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24575 return;
24578 if (lang_hooks.types.get_debug_type)
24580 tree debug_type = lang_hooks.types.get_debug_type (type);
24582 if (debug_type != NULL_TREE && debug_type != type)
24584 gen_type_die_with_usage (debug_type, context_die, usage);
24585 return;
24589 /* We are going to output a DIE to represent the unqualified version
24590 of this type (i.e. without any const or volatile qualifiers) so
24591 get the main variant (i.e. the unqualified version) of this type
24592 now. (Vectors and arrays are special because the debugging info is in the
24593 cloned type itself. Similarly function/method types can contain extra
24594 ref-qualification). */
24595 if (TREE_CODE (type) == FUNCTION_TYPE
24596 || TREE_CODE (type) == METHOD_TYPE)
24598 /* For function/method types, can't use type_main_variant here,
24599 because that can have different ref-qualifiers for C++,
24600 but try to canonicalize. */
24601 tree main = TYPE_MAIN_VARIANT (type);
24602 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24603 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24604 && check_base_type (t, main)
24605 && check_lang_type (t, type))
24607 type = t;
24608 break;
24611 else if (TREE_CODE (type) != VECTOR_TYPE
24612 && TREE_CODE (type) != ARRAY_TYPE)
24613 type = type_main_variant (type);
24615 /* If this is an array type with hidden descriptor, handle it first. */
24616 if (!TREE_ASM_WRITTEN (type)
24617 && lang_hooks.types.get_array_descr_info)
24619 memset (&info, 0, sizeof (info));
24620 if (lang_hooks.types.get_array_descr_info (type, &info))
24622 /* Fortran sometimes emits array types with no dimension. */
24623 gcc_assert (info.ndimensions >= 0
24624 && (info.ndimensions
24625 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24626 gen_descr_array_type_die (type, &info, context_die);
24627 TREE_ASM_WRITTEN (type) = 1;
24628 return;
24632 if (TREE_ASM_WRITTEN (type))
24634 /* Variable-length types may be incomplete even if
24635 TREE_ASM_WRITTEN. For such types, fall through to
24636 gen_array_type_die() and possibly fill in
24637 DW_AT_{upper,lower}_bound attributes. */
24638 if ((TREE_CODE (type) != ARRAY_TYPE
24639 && TREE_CODE (type) != RECORD_TYPE
24640 && TREE_CODE (type) != UNION_TYPE
24641 && TREE_CODE (type) != QUAL_UNION_TYPE)
24642 || !variably_modified_type_p (type, NULL))
24643 return;
24646 switch (TREE_CODE (type))
24648 case ERROR_MARK:
24649 break;
24651 case POINTER_TYPE:
24652 case REFERENCE_TYPE:
24653 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24654 ensures that the gen_type_die recursion will terminate even if the
24655 type is recursive. Recursive types are possible in Ada. */
24656 /* ??? We could perhaps do this for all types before the switch
24657 statement. */
24658 TREE_ASM_WRITTEN (type) = 1;
24660 /* For these types, all that is required is that we output a DIE (or a
24661 set of DIEs) to represent the "basis" type. */
24662 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24663 DINFO_USAGE_IND_USE);
24664 break;
24666 case OFFSET_TYPE:
24667 /* This code is used for C++ pointer-to-data-member types.
24668 Output a description of the relevant class type. */
24669 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24670 DINFO_USAGE_IND_USE);
24672 /* Output a description of the type of the object pointed to. */
24673 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24674 DINFO_USAGE_IND_USE);
24676 /* Now output a DIE to represent this pointer-to-data-member type
24677 itself. */
24678 gen_ptr_to_mbr_type_die (type, context_die);
24679 break;
24681 case FUNCTION_TYPE:
24682 /* Force out return type (in case it wasn't forced out already). */
24683 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24684 DINFO_USAGE_DIR_USE);
24685 gen_subroutine_type_die (type, context_die);
24686 break;
24688 case METHOD_TYPE:
24689 /* Force out return type (in case it wasn't forced out already). */
24690 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24691 DINFO_USAGE_DIR_USE);
24692 gen_subroutine_type_die (type, context_die);
24693 break;
24695 case ARRAY_TYPE:
24696 case VECTOR_TYPE:
24697 gen_array_type_die (type, context_die);
24698 break;
24700 case ENUMERAL_TYPE:
24701 case RECORD_TYPE:
24702 case UNION_TYPE:
24703 case QUAL_UNION_TYPE:
24704 gen_tagged_type_die (type, context_die, usage);
24705 return;
24707 case VOID_TYPE:
24708 case INTEGER_TYPE:
24709 case REAL_TYPE:
24710 case FIXED_POINT_TYPE:
24711 case COMPLEX_TYPE:
24712 case BOOLEAN_TYPE:
24713 case POINTER_BOUNDS_TYPE:
24714 /* No DIEs needed for fundamental types. */
24715 break;
24717 case NULLPTR_TYPE:
24718 case LANG_TYPE:
24719 /* Just use DW_TAG_unspecified_type. */
24721 dw_die_ref type_die = lookup_type_die (type);
24722 if (type_die == NULL)
24724 tree name = TYPE_IDENTIFIER (type);
24725 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24726 type);
24727 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24728 equate_type_number_to_die (type, type_die);
24731 break;
24733 default:
24734 if (is_cxx_auto (type))
24736 tree name = TYPE_IDENTIFIER (type);
24737 dw_die_ref *die = (name == get_identifier ("auto")
24738 ? &auto_die : &decltype_auto_die);
24739 if (!*die)
24741 *die = new_die (DW_TAG_unspecified_type,
24742 comp_unit_die (), NULL_TREE);
24743 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24745 equate_type_number_to_die (type, *die);
24746 break;
24748 gcc_unreachable ();
24751 TREE_ASM_WRITTEN (type) = 1;
24754 static void
24755 gen_type_die (tree type, dw_die_ref context_die)
24757 if (type != error_mark_node)
24759 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24760 if (flag_checking)
24762 dw_die_ref die = lookup_type_die (type);
24763 if (die)
24764 check_die (die);
24769 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24770 things which are local to the given block. */
24772 static void
24773 gen_block_die (tree stmt, dw_die_ref context_die)
24775 int must_output_die = 0;
24776 bool inlined_func;
24778 /* Ignore blocks that are NULL. */
24779 if (stmt == NULL_TREE)
24780 return;
24782 inlined_func = inlined_function_outer_scope_p (stmt);
24784 /* If the block is one fragment of a non-contiguous block, do not
24785 process the variables, since they will have been done by the
24786 origin block. Do process subblocks. */
24787 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24789 tree sub;
24791 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24792 gen_block_die (sub, context_die);
24794 return;
24797 /* Determine if we need to output any Dwarf DIEs at all to represent this
24798 block. */
24799 if (inlined_func)
24800 /* The outer scopes for inlinings *must* always be represented. We
24801 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24802 must_output_die = 1;
24803 else
24805 /* Determine if this block directly contains any "significant"
24806 local declarations which we will need to output DIEs for. */
24807 if (debug_info_level > DINFO_LEVEL_TERSE)
24808 /* We are not in terse mode so *any* local declaration counts
24809 as being a "significant" one. */
24810 must_output_die = ((BLOCK_VARS (stmt) != NULL
24811 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24812 && (TREE_USED (stmt)
24813 || TREE_ASM_WRITTEN (stmt)
24814 || BLOCK_ABSTRACT (stmt)));
24815 else if ((TREE_USED (stmt)
24816 || TREE_ASM_WRITTEN (stmt)
24817 || BLOCK_ABSTRACT (stmt))
24818 && !dwarf2out_ignore_block (stmt))
24819 must_output_die = 1;
24822 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24823 DIE for any block which contains no significant local declarations at
24824 all. Rather, in such cases we just call `decls_for_scope' so that any
24825 needed Dwarf info for any sub-blocks will get properly generated. Note
24826 that in terse mode, our definition of what constitutes a "significant"
24827 local declaration gets restricted to include only inlined function
24828 instances and local (nested) function definitions. */
24829 if (must_output_die)
24831 if (inlined_func)
24833 /* If STMT block is abstract, that means we have been called
24834 indirectly from dwarf2out_abstract_function.
24835 That function rightfully marks the descendent blocks (of
24836 the abstract function it is dealing with) as being abstract,
24837 precisely to prevent us from emitting any
24838 DW_TAG_inlined_subroutine DIE as a descendent
24839 of an abstract function instance. So in that case, we should
24840 not call gen_inlined_subroutine_die.
24842 Later though, when cgraph asks dwarf2out to emit info
24843 for the concrete instance of the function decl into which
24844 the concrete instance of STMT got inlined, the later will lead
24845 to the generation of a DW_TAG_inlined_subroutine DIE. */
24846 if (! BLOCK_ABSTRACT (stmt))
24847 gen_inlined_subroutine_die (stmt, context_die);
24849 else
24850 gen_lexical_block_die (stmt, context_die);
24852 else
24853 decls_for_scope (stmt, context_die);
24856 /* Process variable DECL (or variable with origin ORIGIN) within
24857 block STMT and add it to CONTEXT_DIE. */
24858 static void
24859 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24861 dw_die_ref die;
24862 tree decl_or_origin = decl ? decl : origin;
24864 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24865 die = lookup_decl_die (decl_or_origin);
24866 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24868 if (TYPE_DECL_IS_STUB (decl_or_origin))
24869 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24870 else
24871 die = lookup_decl_die (decl_or_origin);
24872 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24873 if (! die && ! early_dwarf)
24874 return;
24876 else
24877 die = NULL;
24879 /* Avoid creating DIEs for local typedefs and concrete static variables that
24880 will only be pruned later. */
24881 if ((origin || decl_ultimate_origin (decl))
24882 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24883 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24885 origin = decl_ultimate_origin (decl_or_origin);
24886 if (decl && VAR_P (decl) && die != NULL)
24888 die = lookup_decl_die (origin);
24889 if (die != NULL)
24890 equate_decl_number_to_die (decl, die);
24892 return;
24895 if (die != NULL && die->die_parent == NULL)
24896 add_child_die (context_die, die);
24897 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24899 if (early_dwarf)
24900 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24901 stmt, context_die);
24903 else
24905 if (decl && DECL_P (decl))
24907 die = lookup_decl_die (decl);
24909 /* Early created DIEs do not have a parent as the decls refer
24910 to the function as DECL_CONTEXT rather than the BLOCK. */
24911 if (die && die->die_parent == NULL)
24913 gcc_assert (in_lto_p);
24914 add_child_die (context_die, die);
24918 gen_decl_die (decl, origin, NULL, context_die);
24922 /* Generate all of the decls declared within a given scope and (recursively)
24923 all of its sub-blocks. */
24925 static void
24926 decls_for_scope (tree stmt, dw_die_ref context_die)
24928 tree decl;
24929 unsigned int i;
24930 tree subblocks;
24932 /* Ignore NULL blocks. */
24933 if (stmt == NULL_TREE)
24934 return;
24936 /* Output the DIEs to represent all of the data objects and typedefs
24937 declared directly within this block but not within any nested
24938 sub-blocks. Also, nested function and tag DIEs have been
24939 generated with a parent of NULL; fix that up now. We don't
24940 have to do this if we're at -g1. */
24941 if (debug_info_level > DINFO_LEVEL_TERSE)
24943 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24944 process_scope_var (stmt, decl, NULL_TREE, context_die);
24945 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24946 origin - avoid doing this twice as we have no good way to see
24947 if we've done it once already. */
24948 if (! early_dwarf)
24949 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24951 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24952 if (decl == current_function_decl)
24953 /* Ignore declarations of the current function, while they
24954 are declarations, gen_subprogram_die would treat them
24955 as definitions again, because they are equal to
24956 current_function_decl and endlessly recurse. */;
24957 else if (TREE_CODE (decl) == FUNCTION_DECL)
24958 process_scope_var (stmt, decl, NULL_TREE, context_die);
24959 else
24960 process_scope_var (stmt, NULL_TREE, decl, context_die);
24964 /* Even if we're at -g1, we need to process the subblocks in order to get
24965 inlined call information. */
24967 /* Output the DIEs to represent all sub-blocks (and the items declared
24968 therein) of this block. */
24969 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24970 subblocks != NULL;
24971 subblocks = BLOCK_CHAIN (subblocks))
24972 gen_block_die (subblocks, context_die);
24975 /* Is this a typedef we can avoid emitting? */
24977 bool
24978 is_redundant_typedef (const_tree decl)
24980 if (TYPE_DECL_IS_STUB (decl))
24981 return true;
24983 if (DECL_ARTIFICIAL (decl)
24984 && DECL_CONTEXT (decl)
24985 && is_tagged_type (DECL_CONTEXT (decl))
24986 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24987 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24988 /* Also ignore the artificial member typedef for the class name. */
24989 return true;
24991 return false;
24994 /* Return TRUE if TYPE is a typedef that names a type for linkage
24995 purposes. This kind of typedefs is produced by the C++ FE for
24996 constructs like:
24998 typedef struct {...} foo;
25000 In that case, there is no typedef variant type produced for foo.
25001 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25002 struct type. */
25004 static bool
25005 is_naming_typedef_decl (const_tree decl)
25007 if (decl == NULL_TREE
25008 || TREE_CODE (decl) != TYPE_DECL
25009 || DECL_NAMELESS (decl)
25010 || !is_tagged_type (TREE_TYPE (decl))
25011 || DECL_IS_BUILTIN (decl)
25012 || is_redundant_typedef (decl)
25013 /* It looks like Ada produces TYPE_DECLs that are very similar
25014 to C++ naming typedefs but that have different
25015 semantics. Let's be specific to c++ for now. */
25016 || !is_cxx (decl))
25017 return FALSE;
25019 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25020 && TYPE_NAME (TREE_TYPE (decl)) == decl
25021 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25022 != TYPE_NAME (TREE_TYPE (decl))));
25025 /* Looks up the DIE for a context. */
25027 static inline dw_die_ref
25028 lookup_context_die (tree context)
25030 if (context)
25032 /* Find die that represents this context. */
25033 if (TYPE_P (context))
25035 context = TYPE_MAIN_VARIANT (context);
25036 dw_die_ref ctx = lookup_type_die (context);
25037 if (!ctx)
25038 return NULL;
25039 return strip_naming_typedef (context, ctx);
25041 else
25042 return lookup_decl_die (context);
25044 return comp_unit_die ();
25047 /* Returns the DIE for a context. */
25049 static inline dw_die_ref
25050 get_context_die (tree context)
25052 if (context)
25054 /* Find die that represents this context. */
25055 if (TYPE_P (context))
25057 context = TYPE_MAIN_VARIANT (context);
25058 return strip_naming_typedef (context, force_type_die (context));
25060 else
25061 return force_decl_die (context);
25063 return comp_unit_die ();
25066 /* Returns the DIE for decl. A DIE will always be returned. */
25068 static dw_die_ref
25069 force_decl_die (tree decl)
25071 dw_die_ref decl_die;
25072 unsigned saved_external_flag;
25073 tree save_fn = NULL_TREE;
25074 decl_die = lookup_decl_die (decl);
25075 if (!decl_die)
25077 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25079 decl_die = lookup_decl_die (decl);
25080 if (decl_die)
25081 return decl_die;
25083 switch (TREE_CODE (decl))
25085 case FUNCTION_DECL:
25086 /* Clear current_function_decl, so that gen_subprogram_die thinks
25087 that this is a declaration. At this point, we just want to force
25088 declaration die. */
25089 save_fn = current_function_decl;
25090 current_function_decl = NULL_TREE;
25091 gen_subprogram_die (decl, context_die);
25092 current_function_decl = save_fn;
25093 break;
25095 case VAR_DECL:
25096 /* Set external flag to force declaration die. Restore it after
25097 gen_decl_die() call. */
25098 saved_external_flag = DECL_EXTERNAL (decl);
25099 DECL_EXTERNAL (decl) = 1;
25100 gen_decl_die (decl, NULL, NULL, context_die);
25101 DECL_EXTERNAL (decl) = saved_external_flag;
25102 break;
25104 case NAMESPACE_DECL:
25105 if (dwarf_version >= 3 || !dwarf_strict)
25106 dwarf2out_decl (decl);
25107 else
25108 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25109 decl_die = comp_unit_die ();
25110 break;
25112 case TRANSLATION_UNIT_DECL:
25113 decl_die = comp_unit_die ();
25114 break;
25116 default:
25117 gcc_unreachable ();
25120 /* We should be able to find the DIE now. */
25121 if (!decl_die)
25122 decl_die = lookup_decl_die (decl);
25123 gcc_assert (decl_die);
25126 return decl_die;
25129 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25130 always returned. */
25132 static dw_die_ref
25133 force_type_die (tree type)
25135 dw_die_ref type_die;
25137 type_die = lookup_type_die (type);
25138 if (!type_die)
25140 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25142 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25143 false, context_die);
25144 gcc_assert (type_die);
25146 return type_die;
25149 /* Force out any required namespaces to be able to output DECL,
25150 and return the new context_die for it, if it's changed. */
25152 static dw_die_ref
25153 setup_namespace_context (tree thing, dw_die_ref context_die)
25155 tree context = (DECL_P (thing)
25156 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25157 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25158 /* Force out the namespace. */
25159 context_die = force_decl_die (context);
25161 return context_die;
25164 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25165 type) within its namespace, if appropriate.
25167 For compatibility with older debuggers, namespace DIEs only contain
25168 declarations; all definitions are emitted at CU scope, with
25169 DW_AT_specification pointing to the declaration (like with class
25170 members). */
25172 static dw_die_ref
25173 declare_in_namespace (tree thing, dw_die_ref context_die)
25175 dw_die_ref ns_context;
25177 if (debug_info_level <= DINFO_LEVEL_TERSE)
25178 return context_die;
25180 /* External declarations in the local scope only need to be emitted
25181 once, not once in the namespace and once in the scope.
25183 This avoids declaring the `extern' below in the
25184 namespace DIE as well as in the innermost scope:
25186 namespace S
25188 int i=5;
25189 int foo()
25191 int i=8;
25192 extern int i;
25193 return i;
25197 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25198 return context_die;
25200 /* If this decl is from an inlined function, then don't try to emit it in its
25201 namespace, as we will get confused. It would have already been emitted
25202 when the abstract instance of the inline function was emitted anyways. */
25203 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25204 return context_die;
25206 ns_context = setup_namespace_context (thing, context_die);
25208 if (ns_context != context_die)
25210 if (is_fortran ())
25211 return ns_context;
25212 if (DECL_P (thing))
25213 gen_decl_die (thing, NULL, NULL, ns_context);
25214 else
25215 gen_type_die (thing, ns_context);
25217 return context_die;
25220 /* Generate a DIE for a namespace or namespace alias. */
25222 static void
25223 gen_namespace_die (tree decl, dw_die_ref context_die)
25225 dw_die_ref namespace_die;
25227 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25228 they are an alias of. */
25229 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25231 /* Output a real namespace or module. */
25232 context_die = setup_namespace_context (decl, comp_unit_die ());
25233 namespace_die = new_die (is_fortran ()
25234 ? DW_TAG_module : DW_TAG_namespace,
25235 context_die, decl);
25236 /* For Fortran modules defined in different CU don't add src coords. */
25237 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25239 const char *name = dwarf2_name (decl, 0);
25240 if (name)
25241 add_name_attribute (namespace_die, name);
25243 else
25244 add_name_and_src_coords_attributes (namespace_die, decl);
25245 if (DECL_EXTERNAL (decl))
25246 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25247 equate_decl_number_to_die (decl, namespace_die);
25249 else
25251 /* Output a namespace alias. */
25253 /* Force out the namespace we are an alias of, if necessary. */
25254 dw_die_ref origin_die
25255 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25257 if (DECL_FILE_SCOPE_P (decl)
25258 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25259 context_die = setup_namespace_context (decl, comp_unit_die ());
25260 /* Now create the namespace alias DIE. */
25261 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25262 add_name_and_src_coords_attributes (namespace_die, decl);
25263 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25264 equate_decl_number_to_die (decl, namespace_die);
25266 if ((dwarf_version >= 5 || !dwarf_strict)
25267 && lang_hooks.decls.decl_dwarf_attribute (decl,
25268 DW_AT_export_symbols) == 1)
25269 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25271 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25272 if (want_pubnames ())
25273 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25276 /* Generate Dwarf debug information for a decl described by DECL.
25277 The return value is currently only meaningful for PARM_DECLs,
25278 for all other decls it returns NULL.
25280 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25281 It can be NULL otherwise. */
25283 static dw_die_ref
25284 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25285 dw_die_ref context_die)
25287 tree decl_or_origin = decl ? decl : origin;
25288 tree class_origin = NULL, ultimate_origin;
25290 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25291 return NULL;
25293 /* Ignore pointer bounds decls. */
25294 if (DECL_P (decl_or_origin)
25295 && TREE_TYPE (decl_or_origin)
25296 && POINTER_BOUNDS_P (decl_or_origin))
25297 return NULL;
25299 switch (TREE_CODE (decl_or_origin))
25301 case ERROR_MARK:
25302 break;
25304 case CONST_DECL:
25305 if (!is_fortran () && !is_ada ())
25307 /* The individual enumerators of an enum type get output when we output
25308 the Dwarf representation of the relevant enum type itself. */
25309 break;
25312 /* Emit its type. */
25313 gen_type_die (TREE_TYPE (decl), context_die);
25315 /* And its containing namespace. */
25316 context_die = declare_in_namespace (decl, context_die);
25318 gen_const_die (decl, context_die);
25319 break;
25321 case FUNCTION_DECL:
25322 #if 0
25323 /* FIXME */
25324 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25325 on local redeclarations of global functions. That seems broken. */
25326 if (current_function_decl != decl)
25327 /* This is only a declaration. */;
25328 #endif
25330 /* We should have abstract copies already and should not generate
25331 stray type DIEs in late LTO dumping. */
25332 if (! early_dwarf)
25335 /* If we're emitting a clone, emit info for the abstract instance. */
25336 else if (origin || DECL_ORIGIN (decl) != decl)
25337 dwarf2out_abstract_function (origin
25338 ? DECL_ORIGIN (origin)
25339 : DECL_ABSTRACT_ORIGIN (decl));
25341 /* If we're emitting a possibly inlined function emit it as
25342 abstract instance. */
25343 else if (cgraph_function_possibly_inlined_p (decl)
25344 && ! DECL_ABSTRACT_P (decl)
25345 && ! class_or_namespace_scope_p (context_die)
25346 /* dwarf2out_abstract_function won't emit a die if this is just
25347 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25348 that case, because that works only if we have a die. */
25349 && DECL_INITIAL (decl) != NULL_TREE)
25350 dwarf2out_abstract_function (decl);
25352 /* Otherwise we're emitting the primary DIE for this decl. */
25353 else if (debug_info_level > DINFO_LEVEL_TERSE)
25355 /* Before we describe the FUNCTION_DECL itself, make sure that we
25356 have its containing type. */
25357 if (!origin)
25358 origin = decl_class_context (decl);
25359 if (origin != NULL_TREE)
25360 gen_type_die (origin, context_die);
25362 /* And its return type. */
25363 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25365 /* And its virtual context. */
25366 if (DECL_VINDEX (decl) != NULL_TREE)
25367 gen_type_die (DECL_CONTEXT (decl), context_die);
25369 /* Make sure we have a member DIE for decl. */
25370 if (origin != NULL_TREE)
25371 gen_type_die_for_member (origin, decl, context_die);
25373 /* And its containing namespace. */
25374 context_die = declare_in_namespace (decl, context_die);
25377 /* Now output a DIE to represent the function itself. */
25378 if (decl)
25379 gen_subprogram_die (decl, context_die);
25380 break;
25382 case TYPE_DECL:
25383 /* If we are in terse mode, don't generate any DIEs to represent any
25384 actual typedefs. */
25385 if (debug_info_level <= DINFO_LEVEL_TERSE)
25386 break;
25388 /* In the special case of a TYPE_DECL node representing the declaration
25389 of some type tag, if the given TYPE_DECL is marked as having been
25390 instantiated from some other (original) TYPE_DECL node (e.g. one which
25391 was generated within the original definition of an inline function) we
25392 used to generate a special (abbreviated) DW_TAG_structure_type,
25393 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25394 should be actually referencing those DIEs, as variable DIEs with that
25395 type would be emitted already in the abstract origin, so it was always
25396 removed during unused type prunning. Don't add anything in this
25397 case. */
25398 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25399 break;
25401 if (is_redundant_typedef (decl))
25402 gen_type_die (TREE_TYPE (decl), context_die);
25403 else
25404 /* Output a DIE to represent the typedef itself. */
25405 gen_typedef_die (decl, context_die);
25406 break;
25408 case LABEL_DECL:
25409 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25410 gen_label_die (decl, context_die);
25411 break;
25413 case VAR_DECL:
25414 case RESULT_DECL:
25415 /* If we are in terse mode, don't generate any DIEs to represent any
25416 variable declarations or definitions. */
25417 if (debug_info_level <= DINFO_LEVEL_TERSE)
25418 break;
25420 /* Avoid generating stray type DIEs during late dwarf dumping.
25421 All types have been dumped early. */
25422 if (early_dwarf
25423 /* ??? But in LTRANS we cannot annotate early created variably
25424 modified type DIEs without copying them and adjusting all
25425 references to them. Dump them again as happens for inlining
25426 which copies both the decl and the types. */
25427 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25428 in VLA bound information for example. */
25429 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25430 current_function_decl)))
25432 /* Output any DIEs that are needed to specify the type of this data
25433 object. */
25434 if (decl_by_reference_p (decl_or_origin))
25435 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25436 else
25437 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25440 if (early_dwarf)
25442 /* And its containing type. */
25443 class_origin = decl_class_context (decl_or_origin);
25444 if (class_origin != NULL_TREE)
25445 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25447 /* And its containing namespace. */
25448 context_die = declare_in_namespace (decl_or_origin, context_die);
25451 /* Now output the DIE to represent the data object itself. This gets
25452 complicated because of the possibility that the VAR_DECL really
25453 represents an inlined instance of a formal parameter for an inline
25454 function. */
25455 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25456 if (ultimate_origin != NULL_TREE
25457 && TREE_CODE (ultimate_origin) == PARM_DECL)
25458 gen_formal_parameter_die (decl, origin,
25459 true /* Emit name attribute. */,
25460 context_die);
25461 else
25462 gen_variable_die (decl, origin, context_die);
25463 break;
25465 case FIELD_DECL:
25466 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25467 /* Ignore the nameless fields that are used to skip bits but handle C++
25468 anonymous unions and structs. */
25469 if (DECL_NAME (decl) != NULL_TREE
25470 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25471 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25473 gen_type_die (member_declared_type (decl), context_die);
25474 gen_field_die (decl, ctx, context_die);
25476 break;
25478 case PARM_DECL:
25479 /* Avoid generating stray type DIEs during late dwarf dumping.
25480 All types have been dumped early. */
25481 if (early_dwarf
25482 /* ??? But in LTRANS we cannot annotate early created variably
25483 modified type DIEs without copying them and adjusting all
25484 references to them. Dump them again as happens for inlining
25485 which copies both the decl and the types. */
25486 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25487 in VLA bound information for example. */
25488 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25489 current_function_decl)))
25491 if (DECL_BY_REFERENCE (decl_or_origin))
25492 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25493 else
25494 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25496 return gen_formal_parameter_die (decl, origin,
25497 true /* Emit name attribute. */,
25498 context_die);
25500 case NAMESPACE_DECL:
25501 if (dwarf_version >= 3 || !dwarf_strict)
25502 gen_namespace_die (decl, context_die);
25503 break;
25505 case IMPORTED_DECL:
25506 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25507 DECL_CONTEXT (decl), context_die);
25508 break;
25510 case NAMELIST_DECL:
25511 gen_namelist_decl (DECL_NAME (decl), context_die,
25512 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25513 break;
25515 default:
25516 /* Probably some frontend-internal decl. Assume we don't care. */
25517 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25518 break;
25521 return NULL;
25524 /* Output initial debug information for global DECL. Called at the
25525 end of the parsing process.
25527 This is the initial debug generation process. As such, the DIEs
25528 generated may be incomplete. A later debug generation pass
25529 (dwarf2out_late_global_decl) will augment the information generated
25530 in this pass (e.g., with complete location info). */
25532 static void
25533 dwarf2out_early_global_decl (tree decl)
25535 set_early_dwarf s;
25537 /* gen_decl_die() will set DECL_ABSTRACT because
25538 cgraph_function_possibly_inlined_p() returns true. This is in
25539 turn will cause DW_AT_inline attributes to be set.
25541 This happens because at early dwarf generation, there is no
25542 cgraph information, causing cgraph_function_possibly_inlined_p()
25543 to return true. Trick cgraph_function_possibly_inlined_p()
25544 while we generate dwarf early. */
25545 bool save = symtab->global_info_ready;
25546 symtab->global_info_ready = true;
25548 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25549 other DECLs and they can point to template types or other things
25550 that dwarf2out can't handle when done via dwarf2out_decl. */
25551 if (TREE_CODE (decl) != TYPE_DECL
25552 && TREE_CODE (decl) != PARM_DECL)
25554 if (TREE_CODE (decl) == FUNCTION_DECL)
25556 tree save_fndecl = current_function_decl;
25558 /* For nested functions, make sure we have DIEs for the parents first
25559 so that all nested DIEs are generated at the proper scope in the
25560 first shot. */
25561 tree context = decl_function_context (decl);
25562 if (context != NULL)
25564 dw_die_ref context_die = lookup_decl_die (context);
25565 current_function_decl = context;
25567 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25568 enough so that it lands in its own context. This avoids type
25569 pruning issues later on. */
25570 if (context_die == NULL || is_declaration_die (context_die))
25571 dwarf2out_decl (context);
25574 /* Emit an abstract origin of a function first. This happens
25575 with C++ constructor clones for example and makes
25576 dwarf2out_abstract_function happy which requires the early
25577 DIE of the abstract instance to be present. */
25578 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25579 dw_die_ref origin_die;
25580 if (origin != NULL
25581 /* Do not emit the DIE multiple times but make sure to
25582 process it fully here in case we just saw a declaration. */
25583 && ((origin_die = lookup_decl_die (origin)) == NULL
25584 || is_declaration_die (origin_die)))
25586 current_function_decl = origin;
25587 dwarf2out_decl (origin);
25590 /* Emit the DIE for decl but avoid doing that multiple times. */
25591 dw_die_ref old_die;
25592 if ((old_die = lookup_decl_die (decl)) == NULL
25593 || is_declaration_die (old_die))
25595 current_function_decl = decl;
25596 dwarf2out_decl (decl);
25599 current_function_decl = save_fndecl;
25601 else
25602 dwarf2out_decl (decl);
25604 symtab->global_info_ready = save;
25607 /* Output debug information for global decl DECL. Called from
25608 toplev.c after compilation proper has finished. */
25610 static void
25611 dwarf2out_late_global_decl (tree decl)
25613 /* Fill-in any location information we were unable to determine
25614 on the first pass. */
25615 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25617 dw_die_ref die = lookup_decl_die (decl);
25619 /* We may have to generate early debug late for LTO in case debug
25620 was not enabled at compile-time or the target doesn't support
25621 the LTO early debug scheme. */
25622 if (! die && in_lto_p)
25624 dwarf2out_decl (decl);
25625 die = lookup_decl_die (decl);
25628 if (die)
25630 /* We get called via the symtab code invoking late_global_decl
25631 for symbols that are optimized out. Do not add locations
25632 for those, except if they have a DECL_VALUE_EXPR, in which case
25633 they are relevant for debuggers. */
25634 varpool_node *node = varpool_node::get (decl);
25635 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25636 tree_add_const_value_attribute_for_decl (die, decl);
25637 else
25638 add_location_or_const_value_attribute (die, decl, false);
25643 /* Output debug information for type decl DECL. Called from toplev.c
25644 and from language front ends (to record built-in types). */
25645 static void
25646 dwarf2out_type_decl (tree decl, int local)
25648 if (!local)
25650 set_early_dwarf s;
25651 dwarf2out_decl (decl);
25655 /* Output debug information for imported module or decl DECL.
25656 NAME is non-NULL name in the lexical block if the decl has been renamed.
25657 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25658 that DECL belongs to.
25659 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25660 static void
25661 dwarf2out_imported_module_or_decl_1 (tree decl,
25662 tree name,
25663 tree lexical_block,
25664 dw_die_ref lexical_block_die)
25666 expanded_location xloc;
25667 dw_die_ref imported_die = NULL;
25668 dw_die_ref at_import_die;
25670 if (TREE_CODE (decl) == IMPORTED_DECL)
25672 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25673 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25674 gcc_assert (decl);
25676 else
25677 xloc = expand_location (input_location);
25679 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25681 at_import_die = force_type_die (TREE_TYPE (decl));
25682 /* For namespace N { typedef void T; } using N::T; base_type_die
25683 returns NULL, but DW_TAG_imported_declaration requires
25684 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25685 if (!at_import_die)
25687 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25688 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25689 at_import_die = lookup_type_die (TREE_TYPE (decl));
25690 gcc_assert (at_import_die);
25693 else
25695 at_import_die = lookup_decl_die (decl);
25696 if (!at_import_die)
25698 /* If we're trying to avoid duplicate debug info, we may not have
25699 emitted the member decl for this field. Emit it now. */
25700 if (TREE_CODE (decl) == FIELD_DECL)
25702 tree type = DECL_CONTEXT (decl);
25704 if (TYPE_CONTEXT (type)
25705 && TYPE_P (TYPE_CONTEXT (type))
25706 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25707 DINFO_USAGE_DIR_USE))
25708 return;
25709 gen_type_die_for_member (type, decl,
25710 get_context_die (TYPE_CONTEXT (type)));
25712 if (TREE_CODE (decl) == NAMELIST_DECL)
25713 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25714 get_context_die (DECL_CONTEXT (decl)),
25715 NULL_TREE);
25716 else
25717 at_import_die = force_decl_die (decl);
25721 if (TREE_CODE (decl) == NAMESPACE_DECL)
25723 if (dwarf_version >= 3 || !dwarf_strict)
25724 imported_die = new_die (DW_TAG_imported_module,
25725 lexical_block_die,
25726 lexical_block);
25727 else
25728 return;
25730 else
25731 imported_die = new_die (DW_TAG_imported_declaration,
25732 lexical_block_die,
25733 lexical_block);
25735 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25736 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25737 if (debug_column_info && xloc.column)
25738 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25739 if (name)
25740 add_AT_string (imported_die, DW_AT_name,
25741 IDENTIFIER_POINTER (name));
25742 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25745 /* Output debug information for imported module or decl DECL.
25746 NAME is non-NULL name in context if the decl has been renamed.
25747 CHILD is true if decl is one of the renamed decls as part of
25748 importing whole module.
25749 IMPLICIT is set if this hook is called for an implicit import
25750 such as inline namespace. */
25752 static void
25753 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25754 bool child, bool implicit)
25756 /* dw_die_ref at_import_die; */
25757 dw_die_ref scope_die;
25759 if (debug_info_level <= DINFO_LEVEL_TERSE)
25760 return;
25762 gcc_assert (decl);
25764 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25765 should be enough, for DWARF4 and older even if we emit as extension
25766 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25767 for the benefit of consumers unaware of DW_AT_export_symbols. */
25768 if (implicit
25769 && dwarf_version >= 5
25770 && lang_hooks.decls.decl_dwarf_attribute (decl,
25771 DW_AT_export_symbols) == 1)
25772 return;
25774 set_early_dwarf s;
25776 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25777 We need decl DIE for reference and scope die. First, get DIE for the decl
25778 itself. */
25780 /* Get the scope die for decl context. Use comp_unit_die for global module
25781 or decl. If die is not found for non globals, force new die. */
25782 if (context
25783 && TYPE_P (context)
25784 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25785 return;
25787 scope_die = get_context_die (context);
25789 if (child)
25791 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25792 there is nothing we can do, here. */
25793 if (dwarf_version < 3 && dwarf_strict)
25794 return;
25796 gcc_assert (scope_die->die_child);
25797 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25798 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25799 scope_die = scope_die->die_child;
25802 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25803 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25806 /* Output debug information for namelists. */
25808 static dw_die_ref
25809 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25811 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25812 tree value;
25813 unsigned i;
25815 if (debug_info_level <= DINFO_LEVEL_TERSE)
25816 return NULL;
25818 gcc_assert (scope_die != NULL);
25819 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25820 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25822 /* If there are no item_decls, we have a nondefining namelist, e.g.
25823 with USE association; hence, set DW_AT_declaration. */
25824 if (item_decls == NULL_TREE)
25826 add_AT_flag (nml_die, DW_AT_declaration, 1);
25827 return nml_die;
25830 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25832 nml_item_ref_die = lookup_decl_die (value);
25833 if (!nml_item_ref_die)
25834 nml_item_ref_die = force_decl_die (value);
25836 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25837 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25839 return nml_die;
25843 /* Write the debugging output for DECL and return the DIE. */
25845 static void
25846 dwarf2out_decl (tree decl)
25848 dw_die_ref context_die = comp_unit_die ();
25850 switch (TREE_CODE (decl))
25852 case ERROR_MARK:
25853 return;
25855 case FUNCTION_DECL:
25856 /* If we're a nested function, initially use a parent of NULL; if we're
25857 a plain function, this will be fixed up in decls_for_scope. If
25858 we're a method, it will be ignored, since we already have a DIE. */
25859 if (decl_function_context (decl)
25860 /* But if we're in terse mode, we don't care about scope. */
25861 && debug_info_level > DINFO_LEVEL_TERSE)
25862 context_die = NULL;
25863 break;
25865 case VAR_DECL:
25866 /* For local statics lookup proper context die. */
25867 if (local_function_static (decl))
25868 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25870 /* If we are in terse mode, don't generate any DIEs to represent any
25871 variable declarations or definitions. */
25872 if (debug_info_level <= DINFO_LEVEL_TERSE)
25873 return;
25874 break;
25876 case CONST_DECL:
25877 if (debug_info_level <= DINFO_LEVEL_TERSE)
25878 return;
25879 if (!is_fortran () && !is_ada ())
25880 return;
25881 if (TREE_STATIC (decl) && decl_function_context (decl))
25882 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25883 break;
25885 case NAMESPACE_DECL:
25886 case IMPORTED_DECL:
25887 if (debug_info_level <= DINFO_LEVEL_TERSE)
25888 return;
25889 if (lookup_decl_die (decl) != NULL)
25890 return;
25891 break;
25893 case TYPE_DECL:
25894 /* Don't emit stubs for types unless they are needed by other DIEs. */
25895 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25896 return;
25898 /* Don't bother trying to generate any DIEs to represent any of the
25899 normal built-in types for the language we are compiling. */
25900 if (DECL_IS_BUILTIN (decl))
25901 return;
25903 /* If we are in terse mode, don't generate any DIEs for types. */
25904 if (debug_info_level <= DINFO_LEVEL_TERSE)
25905 return;
25907 /* If we're a function-scope tag, initially use a parent of NULL;
25908 this will be fixed up in decls_for_scope. */
25909 if (decl_function_context (decl))
25910 context_die = NULL;
25912 break;
25914 case NAMELIST_DECL:
25915 break;
25917 default:
25918 return;
25921 gen_decl_die (decl, NULL, NULL, context_die);
25923 if (flag_checking)
25925 dw_die_ref die = lookup_decl_die (decl);
25926 if (die)
25927 check_die (die);
25931 /* Write the debugging output for DECL. */
25933 static void
25934 dwarf2out_function_decl (tree decl)
25936 dwarf2out_decl (decl);
25937 call_arg_locations = NULL;
25938 call_arg_loc_last = NULL;
25939 call_site_count = -1;
25940 tail_call_site_count = -1;
25941 decl_loc_table->empty ();
25942 cached_dw_loc_list_table->empty ();
25945 /* Output a marker (i.e. a label) for the beginning of the generated code for
25946 a lexical block. */
25948 static void
25949 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25950 unsigned int blocknum)
25952 switch_to_section (current_function_section ());
25953 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25956 /* Output a marker (i.e. a label) for the end of the generated code for a
25957 lexical block. */
25959 static void
25960 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25962 switch_to_section (current_function_section ());
25963 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25966 /* Returns nonzero if it is appropriate not to emit any debugging
25967 information for BLOCK, because it doesn't contain any instructions.
25969 Don't allow this for blocks with nested functions or local classes
25970 as we would end up with orphans, and in the presence of scheduling
25971 we may end up calling them anyway. */
25973 static bool
25974 dwarf2out_ignore_block (const_tree block)
25976 tree decl;
25977 unsigned int i;
25979 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25980 if (TREE_CODE (decl) == FUNCTION_DECL
25981 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25982 return 0;
25983 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25985 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25986 if (TREE_CODE (decl) == FUNCTION_DECL
25987 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25988 return 0;
25991 return 1;
25994 /* Hash table routines for file_hash. */
25996 bool
25997 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25999 return filename_cmp (p1->filename, p2) == 0;
26002 hashval_t
26003 dwarf_file_hasher::hash (dwarf_file_data *p)
26005 return htab_hash_string (p->filename);
26008 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26009 dwarf2out.c) and return its "index". The index of each (known) filename is
26010 just a unique number which is associated with only that one filename. We
26011 need such numbers for the sake of generating labels (in the .debug_sfnames
26012 section) and references to those files numbers (in the .debug_srcinfo
26013 and .debug_macinfo sections). If the filename given as an argument is not
26014 found in our current list, add it to the list and assign it the next
26015 available unique index number. */
26017 static struct dwarf_file_data *
26018 lookup_filename (const char *file_name)
26020 struct dwarf_file_data * created;
26022 if (!file_name)
26023 return NULL;
26025 dwarf_file_data **slot
26026 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26027 INSERT);
26028 if (*slot)
26029 return *slot;
26031 created = ggc_alloc<dwarf_file_data> ();
26032 created->filename = file_name;
26033 created->emitted_number = 0;
26034 *slot = created;
26035 return created;
26038 /* If the assembler will construct the file table, then translate the compiler
26039 internal file table number into the assembler file table number, and emit
26040 a .file directive if we haven't already emitted one yet. The file table
26041 numbers are different because we prune debug info for unused variables and
26042 types, which may include filenames. */
26044 static int
26045 maybe_emit_file (struct dwarf_file_data * fd)
26047 if (! fd->emitted_number)
26049 if (last_emitted_file)
26050 fd->emitted_number = last_emitted_file->emitted_number + 1;
26051 else
26052 fd->emitted_number = 1;
26053 last_emitted_file = fd;
26055 if (DWARF2_ASM_LINE_DEBUG_INFO)
26057 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26058 output_quoted_string (asm_out_file,
26059 remap_debug_filename (fd->filename));
26060 fputc ('\n', asm_out_file);
26064 return fd->emitted_number;
26067 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26068 That generation should happen after function debug info has been
26069 generated. The value of the attribute is the constant value of ARG. */
26071 static void
26072 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26074 die_arg_entry entry;
26076 if (!die || !arg)
26077 return;
26079 gcc_assert (early_dwarf);
26081 if (!tmpl_value_parm_die_table)
26082 vec_alloc (tmpl_value_parm_die_table, 32);
26084 entry.die = die;
26085 entry.arg = arg;
26086 vec_safe_push (tmpl_value_parm_die_table, entry);
26089 /* Return TRUE if T is an instance of generic type, FALSE
26090 otherwise. */
26092 static bool
26093 generic_type_p (tree t)
26095 if (t == NULL_TREE || !TYPE_P (t))
26096 return false;
26097 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26100 /* Schedule the generation of the generic parameter dies for the
26101 instance of generic type T. The proper generation itself is later
26102 done by gen_scheduled_generic_parms_dies. */
26104 static void
26105 schedule_generic_params_dies_gen (tree t)
26107 if (!generic_type_p (t))
26108 return;
26110 gcc_assert (early_dwarf);
26112 if (!generic_type_instances)
26113 vec_alloc (generic_type_instances, 256);
26115 vec_safe_push (generic_type_instances, t);
26118 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26119 by append_entry_to_tmpl_value_parm_die_table. This function must
26120 be called after function DIEs have been generated. */
26122 static void
26123 gen_remaining_tmpl_value_param_die_attribute (void)
26125 if (tmpl_value_parm_die_table)
26127 unsigned i, j;
26128 die_arg_entry *e;
26130 /* We do this in two phases - first get the cases we can
26131 handle during early-finish, preserving those we cannot
26132 (containing symbolic constants where we don't yet know
26133 whether we are going to output the referenced symbols).
26134 For those we try again at late-finish. */
26135 j = 0;
26136 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26138 if (!e->die->removed
26139 && !tree_add_const_value_attribute (e->die, e->arg))
26141 dw_loc_descr_ref loc = NULL;
26142 if (! early_dwarf
26143 && (dwarf_version >= 5 || !dwarf_strict))
26144 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26145 if (loc)
26146 add_AT_loc (e->die, DW_AT_location, loc);
26147 else
26148 (*tmpl_value_parm_die_table)[j++] = *e;
26151 tmpl_value_parm_die_table->truncate (j);
26155 /* Generate generic parameters DIEs for instances of generic types
26156 that have been previously scheduled by
26157 schedule_generic_params_dies_gen. This function must be called
26158 after all the types of the CU have been laid out. */
26160 static void
26161 gen_scheduled_generic_parms_dies (void)
26163 unsigned i;
26164 tree t;
26166 if (!generic_type_instances)
26167 return;
26169 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26170 if (COMPLETE_TYPE_P (t))
26171 gen_generic_params_dies (t);
26173 generic_type_instances = NULL;
26177 /* Replace DW_AT_name for the decl with name. */
26179 static void
26180 dwarf2out_set_name (tree decl, tree name)
26182 dw_die_ref die;
26183 dw_attr_node *attr;
26184 const char *dname;
26186 die = TYPE_SYMTAB_DIE (decl);
26187 if (!die)
26188 return;
26190 dname = dwarf2_name (name, 0);
26191 if (!dname)
26192 return;
26194 attr = get_AT (die, DW_AT_name);
26195 if (attr)
26197 struct indirect_string_node *node;
26199 node = find_AT_string (dname);
26200 /* replace the string. */
26201 attr->dw_attr_val.v.val_str = node;
26204 else
26205 add_name_attribute (die, dname);
26208 /* True if before or during processing of the first function being emitted. */
26209 static bool in_first_function_p = true;
26210 /* True if loc_note during dwarf2out_var_location call might still be
26211 before first real instruction at address equal to .Ltext0. */
26212 static bool maybe_at_text_label_p = true;
26213 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26214 static unsigned int first_loclabel_num_not_at_text_label;
26216 /* Look ahead for a real insn, or for a begin stmt marker. */
26218 static rtx_insn *
26219 dwarf2out_next_real_insn (rtx_insn *loc_note)
26221 rtx_insn *next_real = NEXT_INSN (loc_note);
26223 while (next_real)
26224 if (INSN_P (next_real))
26225 break;
26226 else
26227 next_real = NEXT_INSN (next_real);
26229 return next_real;
26232 /* Called by the final INSN scan whenever we see a var location. We
26233 use it to drop labels in the right places, and throw the location in
26234 our lookup table. */
26236 static void
26237 dwarf2out_var_location (rtx_insn *loc_note)
26239 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26240 struct var_loc_node *newloc;
26241 rtx_insn *next_real, *next_note;
26242 rtx_insn *call_insn = NULL;
26243 static const char *last_label;
26244 static const char *last_postcall_label;
26245 static bool last_in_cold_section_p;
26246 static rtx_insn *expected_next_loc_note;
26247 tree decl;
26248 bool var_loc_p;
26250 if (!NOTE_P (loc_note))
26252 if (CALL_P (loc_note))
26254 call_site_count++;
26255 if (SIBLING_CALL_P (loc_note))
26256 tail_call_site_count++;
26257 if (optimize == 0 && !flag_var_tracking)
26259 /* When the var-tracking pass is not running, there is no note
26260 for indirect calls whose target is compile-time known. In this
26261 case, process such calls specifically so that we generate call
26262 sites for them anyway. */
26263 rtx x = PATTERN (loc_note);
26264 if (GET_CODE (x) == PARALLEL)
26265 x = XVECEXP (x, 0, 0);
26266 if (GET_CODE (x) == SET)
26267 x = SET_SRC (x);
26268 if (GET_CODE (x) == CALL)
26269 x = XEXP (x, 0);
26270 if (!MEM_P (x)
26271 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26272 || !SYMBOL_REF_DECL (XEXP (x, 0))
26273 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26274 != FUNCTION_DECL))
26276 call_insn = loc_note;
26277 loc_note = NULL;
26278 var_loc_p = false;
26280 next_real = dwarf2out_next_real_insn (call_insn);
26281 next_note = NULL;
26282 cached_next_real_insn = NULL;
26283 goto create_label;
26287 return;
26290 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26291 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26292 return;
26294 /* Optimize processing a large consecutive sequence of location
26295 notes so we don't spend too much time in next_real_insn. If the
26296 next insn is another location note, remember the next_real_insn
26297 calculation for next time. */
26298 next_real = cached_next_real_insn;
26299 if (next_real)
26301 if (expected_next_loc_note != loc_note)
26302 next_real = NULL;
26305 next_note = NEXT_INSN (loc_note);
26306 if (! next_note
26307 || next_note->deleted ()
26308 || ! NOTE_P (next_note)
26309 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26310 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
26311 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26312 next_note = NULL;
26314 if (! next_real)
26315 next_real = dwarf2out_next_real_insn (loc_note);
26317 if (next_note)
26319 expected_next_loc_note = next_note;
26320 cached_next_real_insn = next_real;
26322 else
26323 cached_next_real_insn = NULL;
26325 /* If there are no instructions which would be affected by this note,
26326 don't do anything. */
26327 if (var_loc_p
26328 && next_real == NULL_RTX
26329 && !NOTE_DURING_CALL_P (loc_note))
26330 return;
26332 create_label:
26334 if (next_real == NULL_RTX)
26335 next_real = get_last_insn ();
26337 /* If there were any real insns between note we processed last time
26338 and this note (or if it is the first note), clear
26339 last_{,postcall_}label so that they are not reused this time. */
26340 if (last_var_location_insn == NULL_RTX
26341 || last_var_location_insn != next_real
26342 || last_in_cold_section_p != in_cold_section_p)
26344 last_label = NULL;
26345 last_postcall_label = NULL;
26348 if (var_loc_p)
26350 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26351 newloc = add_var_loc_to_decl (decl, loc_note,
26352 NOTE_DURING_CALL_P (loc_note)
26353 ? last_postcall_label : last_label);
26354 if (newloc == NULL)
26355 return;
26357 else
26359 decl = NULL_TREE;
26360 newloc = NULL;
26363 /* If there were no real insns between note we processed last time
26364 and this note, use the label we emitted last time. Otherwise
26365 create a new label and emit it. */
26366 if (last_label == NULL)
26368 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26369 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26370 loclabel_num++;
26371 last_label = ggc_strdup (loclabel);
26372 /* See if loclabel might be equal to .Ltext0. If yes,
26373 bump first_loclabel_num_not_at_text_label. */
26374 if (!have_multiple_function_sections
26375 && in_first_function_p
26376 && maybe_at_text_label_p)
26378 static rtx_insn *last_start;
26379 rtx_insn *insn;
26380 for (insn = loc_note; insn; insn = previous_insn (insn))
26381 if (insn == last_start)
26382 break;
26383 else if (!NONDEBUG_INSN_P (insn))
26384 continue;
26385 else
26387 rtx body = PATTERN (insn);
26388 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26389 continue;
26390 /* Inline asm could occupy zero bytes. */
26391 else if (GET_CODE (body) == ASM_INPUT
26392 || asm_noperands (body) >= 0)
26393 continue;
26394 #ifdef HAVE_attr_length
26395 else if (get_attr_min_length (insn) == 0)
26396 continue;
26397 #endif
26398 else
26400 /* Assume insn has non-zero length. */
26401 maybe_at_text_label_p = false;
26402 break;
26405 if (maybe_at_text_label_p)
26407 last_start = loc_note;
26408 first_loclabel_num_not_at_text_label = loclabel_num;
26413 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26414 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26416 if (!var_loc_p)
26418 struct call_arg_loc_node *ca_loc
26419 = ggc_cleared_alloc<call_arg_loc_node> ();
26420 rtx_insn *prev
26421 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26423 ca_loc->call_arg_loc_note = loc_note;
26424 ca_loc->next = NULL;
26425 ca_loc->label = last_label;
26426 gcc_assert (prev
26427 && (CALL_P (prev)
26428 || (NONJUMP_INSN_P (prev)
26429 && GET_CODE (PATTERN (prev)) == SEQUENCE
26430 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26431 if (!CALL_P (prev))
26432 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26433 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26435 /* Look for a SYMBOL_REF in the "prev" instruction. */
26436 rtx x = get_call_rtx_from (PATTERN (prev));
26437 if (x)
26439 /* Try to get the call symbol, if any. */
26440 if (MEM_P (XEXP (x, 0)))
26441 x = XEXP (x, 0);
26442 /* First, look for a memory access to a symbol_ref. */
26443 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26444 && SYMBOL_REF_DECL (XEXP (x, 0))
26445 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26446 ca_loc->symbol_ref = XEXP (x, 0);
26447 /* Otherwise, look at a compile-time known user-level function
26448 declaration. */
26449 else if (MEM_P (x)
26450 && MEM_EXPR (x)
26451 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26452 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26455 ca_loc->block = insn_scope (prev);
26456 if (call_arg_locations)
26457 call_arg_loc_last->next = ca_loc;
26458 else
26459 call_arg_locations = ca_loc;
26460 call_arg_loc_last = ca_loc;
26462 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26463 newloc->label = last_label;
26464 else
26466 if (!last_postcall_label)
26468 sprintf (loclabel, "%s-1", last_label);
26469 last_postcall_label = ggc_strdup (loclabel);
26471 newloc->label = last_postcall_label;
26474 if (var_loc_p && flag_debug_asm)
26476 const char *name = NULL, *sep = " => ", *patstr = NULL;
26477 if (decl && DECL_NAME (decl))
26478 name = IDENTIFIER_POINTER (DECL_NAME (decl));
26479 if (NOTE_VAR_LOCATION_LOC (loc_note))
26480 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
26481 else
26483 sep = " ";
26484 patstr = "RESET";
26486 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
26487 name, sep, patstr);
26490 last_var_location_insn = next_real;
26491 last_in_cold_section_p = in_cold_section_p;
26494 /* Called from finalize_size_functions for size functions so that their body
26495 can be encoded in the debug info to describe the layout of variable-length
26496 structures. */
26498 static void
26499 dwarf2out_size_function (tree decl)
26501 function_to_dwarf_procedure (decl);
26504 /* Note in one location list that text section has changed. */
26507 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26509 var_loc_list *list = *slot;
26510 if (list->first)
26511 list->last_before_switch
26512 = list->last->next ? list->last->next : list->last;
26513 return 1;
26516 /* Note in all location lists that text section has changed. */
26518 static void
26519 var_location_switch_text_section (void)
26521 if (decl_loc_table == NULL)
26522 return;
26524 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26527 /* Create a new line number table. */
26529 static dw_line_info_table *
26530 new_line_info_table (void)
26532 dw_line_info_table *table;
26534 table = ggc_cleared_alloc<dw_line_info_table> ();
26535 table->file_num = 1;
26536 table->line_num = 1;
26537 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26539 return table;
26542 /* Lookup the "current" table into which we emit line info, so
26543 that we don't have to do it for every source line. */
26545 static void
26546 set_cur_line_info_table (section *sec)
26548 dw_line_info_table *table;
26550 if (sec == text_section)
26551 table = text_section_line_info;
26552 else if (sec == cold_text_section)
26554 table = cold_text_section_line_info;
26555 if (!table)
26557 cold_text_section_line_info = table = new_line_info_table ();
26558 table->end_label = cold_end_label;
26561 else
26563 const char *end_label;
26565 if (crtl->has_bb_partition)
26567 if (in_cold_section_p)
26568 end_label = crtl->subsections.cold_section_end_label;
26569 else
26570 end_label = crtl->subsections.hot_section_end_label;
26572 else
26574 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26575 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26576 current_function_funcdef_no);
26577 end_label = ggc_strdup (label);
26580 table = new_line_info_table ();
26581 table->end_label = end_label;
26583 vec_safe_push (separate_line_info, table);
26586 if (DWARF2_ASM_LINE_DEBUG_INFO)
26587 table->is_stmt = (cur_line_info_table
26588 ? cur_line_info_table->is_stmt
26589 : DWARF_LINE_DEFAULT_IS_STMT_START);
26590 cur_line_info_table = table;
26594 /* We need to reset the locations at the beginning of each
26595 function. We can't do this in the end_function hook, because the
26596 declarations that use the locations won't have been output when
26597 that hook is called. Also compute have_multiple_function_sections here. */
26599 static void
26600 dwarf2out_begin_function (tree fun)
26602 section *sec = function_section (fun);
26604 if (sec != text_section)
26605 have_multiple_function_sections = true;
26607 if (crtl->has_bb_partition && !cold_text_section)
26609 gcc_assert (current_function_decl == fun);
26610 cold_text_section = unlikely_text_section ();
26611 switch_to_section (cold_text_section);
26612 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26613 switch_to_section (sec);
26616 dwarf2out_note_section_used ();
26617 call_site_count = 0;
26618 tail_call_site_count = 0;
26620 set_cur_line_info_table (sec);
26623 /* Helper function of dwarf2out_end_function, called only after emitting
26624 the very first function into assembly. Check if some .debug_loc range
26625 might end with a .LVL* label that could be equal to .Ltext0.
26626 In that case we must force using absolute addresses in .debug_loc ranges,
26627 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26628 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26629 list terminator.
26630 Set have_multiple_function_sections to true in that case and
26631 terminate htab traversal. */
26634 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26636 var_loc_list *entry = *slot;
26637 struct var_loc_node *node;
26639 node = entry->first;
26640 if (node && node->next && node->next->label)
26642 unsigned int i;
26643 const char *label = node->next->label;
26644 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26646 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26648 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26649 if (strcmp (label, loclabel) == 0)
26651 have_multiple_function_sections = true;
26652 return 0;
26656 return 1;
26659 /* Hook called after emitting a function into assembly.
26660 This does something only for the very first function emitted. */
26662 static void
26663 dwarf2out_end_function (unsigned int)
26665 if (in_first_function_p
26666 && !have_multiple_function_sections
26667 && first_loclabel_num_not_at_text_label
26668 && decl_loc_table)
26669 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26670 in_first_function_p = false;
26671 maybe_at_text_label_p = false;
26674 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26675 front-ends register a translation unit even before dwarf2out_init is
26676 called. */
26677 static tree main_translation_unit = NULL_TREE;
26679 /* Hook called by front-ends after they built their main translation unit.
26680 Associate comp_unit_die to UNIT. */
26682 static void
26683 dwarf2out_register_main_translation_unit (tree unit)
26685 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26686 && main_translation_unit == NULL_TREE);
26687 main_translation_unit = unit;
26688 /* If dwarf2out_init has not been called yet, it will perform the association
26689 itself looking at main_translation_unit. */
26690 if (decl_die_table != NULL)
26691 equate_decl_number_to_die (unit, comp_unit_die ());
26694 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26696 static void
26697 push_dw_line_info_entry (dw_line_info_table *table,
26698 enum dw_line_info_opcode opcode, unsigned int val)
26700 dw_line_info_entry e;
26701 e.opcode = opcode;
26702 e.val = val;
26703 vec_safe_push (table->entries, e);
26706 /* Output a label to mark the beginning of a source code line entry
26707 and record information relating to this source line, in
26708 'line_info_table' for later output of the .debug_line section. */
26709 /* ??? The discriminator parameter ought to be unsigned. */
26711 static void
26712 dwarf2out_source_line (unsigned int line, unsigned int column,
26713 const char *filename,
26714 int discriminator, bool is_stmt)
26716 unsigned int file_num;
26717 dw_line_info_table *table;
26719 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26720 return;
26722 /* The discriminator column was added in dwarf4. Simplify the below
26723 by simply removing it if we're not supposed to output it. */
26724 if (dwarf_version < 4 && dwarf_strict)
26725 discriminator = 0;
26727 if (!debug_column_info)
26728 column = 0;
26730 table = cur_line_info_table;
26731 file_num = maybe_emit_file (lookup_filename (filename));
26733 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26734 the debugger has used the second (possibly duplicate) line number
26735 at the beginning of the function to mark the end of the prologue.
26736 We could eliminate any other duplicates within the function. For
26737 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26738 that second line number entry. */
26739 /* Recall that this end-of-prologue indication is *not* the same thing
26740 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26741 to which the hook corresponds, follows the last insn that was
26742 emitted by gen_prologue. What we need is to precede the first insn
26743 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26744 insn that corresponds to something the user wrote. These may be
26745 very different locations once scheduling is enabled. */
26747 if (0 && file_num == table->file_num
26748 && line == table->line_num
26749 && column == table->column_num
26750 && discriminator == table->discrim_num
26751 && is_stmt == table->is_stmt)
26752 return;
26754 switch_to_section (current_function_section ());
26756 /* If requested, emit something human-readable. */
26757 if (flag_debug_asm)
26759 if (debug_column_info)
26760 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26761 filename, line, column);
26762 else
26763 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26764 filename, line);
26767 if (DWARF2_ASM_LINE_DEBUG_INFO)
26769 /* Emit the .loc directive understood by GNU as. */
26770 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26771 file_num, line, is_stmt, discriminator */
26772 fputs ("\t.loc ", asm_out_file);
26773 fprint_ul (asm_out_file, file_num);
26774 putc (' ', asm_out_file);
26775 fprint_ul (asm_out_file, line);
26776 putc (' ', asm_out_file);
26777 fprint_ul (asm_out_file, column);
26779 if (is_stmt != table->is_stmt)
26781 fputs (" is_stmt ", asm_out_file);
26782 putc (is_stmt ? '1' : '0', asm_out_file);
26784 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26786 gcc_assert (discriminator > 0);
26787 fputs (" discriminator ", asm_out_file);
26788 fprint_ul (asm_out_file, (unsigned long) discriminator);
26790 putc ('\n', asm_out_file);
26792 else
26794 unsigned int label_num = ++line_info_label_num;
26796 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26798 push_dw_line_info_entry (table, LI_set_address, label_num);
26799 if (file_num != table->file_num)
26800 push_dw_line_info_entry (table, LI_set_file, file_num);
26801 if (discriminator != table->discrim_num)
26802 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26803 if (is_stmt != table->is_stmt)
26804 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26805 push_dw_line_info_entry (table, LI_set_line, line);
26806 if (debug_column_info)
26807 push_dw_line_info_entry (table, LI_set_column, column);
26810 table->file_num = file_num;
26811 table->line_num = line;
26812 table->column_num = column;
26813 table->discrim_num = discriminator;
26814 table->is_stmt = is_stmt;
26815 table->in_use = true;
26818 /* Record the beginning of a new source file. */
26820 static void
26821 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26823 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26825 macinfo_entry e;
26826 e.code = DW_MACINFO_start_file;
26827 e.lineno = lineno;
26828 e.info = ggc_strdup (filename);
26829 vec_safe_push (macinfo_table, e);
26833 /* Record the end of a source file. */
26835 static void
26836 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26838 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26840 macinfo_entry e;
26841 e.code = DW_MACINFO_end_file;
26842 e.lineno = lineno;
26843 e.info = NULL;
26844 vec_safe_push (macinfo_table, e);
26848 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26849 the tail part of the directive line, i.e. the part which is past the
26850 initial whitespace, #, whitespace, directive-name, whitespace part. */
26852 static void
26853 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26854 const char *buffer ATTRIBUTE_UNUSED)
26856 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26858 macinfo_entry e;
26859 /* Insert a dummy first entry to be able to optimize the whole
26860 predefined macro block using DW_MACRO_import. */
26861 if (macinfo_table->is_empty () && lineno <= 1)
26863 e.code = 0;
26864 e.lineno = 0;
26865 e.info = NULL;
26866 vec_safe_push (macinfo_table, e);
26868 e.code = DW_MACINFO_define;
26869 e.lineno = lineno;
26870 e.info = ggc_strdup (buffer);
26871 vec_safe_push (macinfo_table, e);
26875 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26876 the tail part of the directive line, i.e. the part which is past the
26877 initial whitespace, #, whitespace, directive-name, whitespace part. */
26879 static void
26880 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26881 const char *buffer ATTRIBUTE_UNUSED)
26883 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26885 macinfo_entry e;
26886 /* Insert a dummy first entry to be able to optimize the whole
26887 predefined macro block using DW_MACRO_import. */
26888 if (macinfo_table->is_empty () && lineno <= 1)
26890 e.code = 0;
26891 e.lineno = 0;
26892 e.info = NULL;
26893 vec_safe_push (macinfo_table, e);
26895 e.code = DW_MACINFO_undef;
26896 e.lineno = lineno;
26897 e.info = ggc_strdup (buffer);
26898 vec_safe_push (macinfo_table, e);
26902 /* Helpers to manipulate hash table of CUs. */
26904 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26906 static inline hashval_t hash (const macinfo_entry *);
26907 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26910 inline hashval_t
26911 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26913 return htab_hash_string (entry->info);
26916 inline bool
26917 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26918 const macinfo_entry *entry2)
26920 return !strcmp (entry1->info, entry2->info);
26923 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26925 /* Output a single .debug_macinfo entry. */
26927 static void
26928 output_macinfo_op (macinfo_entry *ref)
26930 int file_num;
26931 size_t len;
26932 struct indirect_string_node *node;
26933 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26934 struct dwarf_file_data *fd;
26936 switch (ref->code)
26938 case DW_MACINFO_start_file:
26939 fd = lookup_filename (ref->info);
26940 file_num = maybe_emit_file (fd);
26941 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26942 dw2_asm_output_data_uleb128 (ref->lineno,
26943 "Included from line number %lu",
26944 (unsigned long) ref->lineno);
26945 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26946 break;
26947 case DW_MACINFO_end_file:
26948 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26949 break;
26950 case DW_MACINFO_define:
26951 case DW_MACINFO_undef:
26952 len = strlen (ref->info) + 1;
26953 if (!dwarf_strict
26954 && len > DWARF_OFFSET_SIZE
26955 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26956 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26958 ref->code = ref->code == DW_MACINFO_define
26959 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26960 output_macinfo_op (ref);
26961 return;
26963 dw2_asm_output_data (1, ref->code,
26964 ref->code == DW_MACINFO_define
26965 ? "Define macro" : "Undefine macro");
26966 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26967 (unsigned long) ref->lineno);
26968 dw2_asm_output_nstring (ref->info, -1, "The macro");
26969 break;
26970 case DW_MACRO_define_strp:
26971 case DW_MACRO_undef_strp:
26972 node = find_AT_string (ref->info);
26973 gcc_assert (node
26974 && (node->form == DW_FORM_strp
26975 || node->form == DW_FORM_GNU_str_index));
26976 dw2_asm_output_data (1, ref->code,
26977 ref->code == DW_MACRO_define_strp
26978 ? "Define macro strp"
26979 : "Undefine macro strp");
26980 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26981 (unsigned long) ref->lineno);
26982 if (node->form == DW_FORM_strp)
26983 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26984 debug_str_section, "The macro: \"%s\"",
26985 ref->info);
26986 else
26987 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26988 ref->info);
26989 break;
26990 case DW_MACRO_import:
26991 dw2_asm_output_data (1, ref->code, "Import");
26992 ASM_GENERATE_INTERNAL_LABEL (label,
26993 DEBUG_MACRO_SECTION_LABEL,
26994 ref->lineno + macinfo_label_base);
26995 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26996 break;
26997 default:
26998 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26999 ASM_COMMENT_START, (unsigned long) ref->code);
27000 break;
27004 /* Attempt to make a sequence of define/undef macinfo ops shareable with
27005 other compilation unit .debug_macinfo sections. IDX is the first
27006 index of a define/undef, return the number of ops that should be
27007 emitted in a comdat .debug_macinfo section and emit
27008 a DW_MACRO_import entry referencing it.
27009 If the define/undef entry should be emitted normally, return 0. */
27011 static unsigned
27012 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
27013 macinfo_hash_type **macinfo_htab)
27015 macinfo_entry *first, *second, *cur, *inc;
27016 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
27017 unsigned char checksum[16];
27018 struct md5_ctx ctx;
27019 char *grp_name, *tail;
27020 const char *base;
27021 unsigned int i, count, encoded_filename_len, linebuf_len;
27022 macinfo_entry **slot;
27024 first = &(*macinfo_table)[idx];
27025 second = &(*macinfo_table)[idx + 1];
27027 /* Optimize only if there are at least two consecutive define/undef ops,
27028 and either all of them are before first DW_MACINFO_start_file
27029 with lineno {0,1} (i.e. predefined macro block), or all of them are
27030 in some included header file. */
27031 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
27032 return 0;
27033 if (vec_safe_is_empty (files))
27035 if (first->lineno > 1 || second->lineno > 1)
27036 return 0;
27038 else if (first->lineno == 0)
27039 return 0;
27041 /* Find the last define/undef entry that can be grouped together
27042 with first and at the same time compute md5 checksum of their
27043 codes, linenumbers and strings. */
27044 md5_init_ctx (&ctx);
27045 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27046 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27047 break;
27048 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27049 break;
27050 else
27052 unsigned char code = cur->code;
27053 md5_process_bytes (&code, 1, &ctx);
27054 checksum_uleb128 (cur->lineno, &ctx);
27055 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27057 md5_finish_ctx (&ctx, checksum);
27058 count = i - idx;
27060 /* From the containing include filename (if any) pick up just
27061 usable characters from its basename. */
27062 if (vec_safe_is_empty (files))
27063 base = "";
27064 else
27065 base = lbasename (files->last ().info);
27066 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27067 if (ISIDNUM (base[i]) || base[i] == '.')
27068 encoded_filename_len++;
27069 /* Count . at the end. */
27070 if (encoded_filename_len)
27071 encoded_filename_len++;
27073 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27074 linebuf_len = strlen (linebuf);
27076 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27077 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27078 + 16 * 2 + 1);
27079 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27080 tail = grp_name + 4;
27081 if (encoded_filename_len)
27083 for (i = 0; base[i]; i++)
27084 if (ISIDNUM (base[i]) || base[i] == '.')
27085 *tail++ = base[i];
27086 *tail++ = '.';
27088 memcpy (tail, linebuf, linebuf_len);
27089 tail += linebuf_len;
27090 *tail++ = '.';
27091 for (i = 0; i < 16; i++)
27092 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27094 /* Construct a macinfo_entry for DW_MACRO_import
27095 in the empty vector entry before the first define/undef. */
27096 inc = &(*macinfo_table)[idx - 1];
27097 inc->code = DW_MACRO_import;
27098 inc->lineno = 0;
27099 inc->info = ggc_strdup (grp_name);
27100 if (!*macinfo_htab)
27101 *macinfo_htab = new macinfo_hash_type (10);
27102 /* Avoid emitting duplicates. */
27103 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27104 if (*slot != NULL)
27106 inc->code = 0;
27107 inc->info = NULL;
27108 /* If such an entry has been used before, just emit
27109 a DW_MACRO_import op. */
27110 inc = *slot;
27111 output_macinfo_op (inc);
27112 /* And clear all macinfo_entry in the range to avoid emitting them
27113 in the second pass. */
27114 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27116 cur->code = 0;
27117 cur->info = NULL;
27120 else
27122 *slot = inc;
27123 inc->lineno = (*macinfo_htab)->elements ();
27124 output_macinfo_op (inc);
27126 return count;
27129 /* Save any strings needed by the macinfo table in the debug str
27130 table. All strings must be collected into the table by the time
27131 index_string is called. */
27133 static void
27134 save_macinfo_strings (void)
27136 unsigned len;
27137 unsigned i;
27138 macinfo_entry *ref;
27140 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27142 switch (ref->code)
27144 /* Match the logic in output_macinfo_op to decide on
27145 indirect strings. */
27146 case DW_MACINFO_define:
27147 case DW_MACINFO_undef:
27148 len = strlen (ref->info) + 1;
27149 if (!dwarf_strict
27150 && len > DWARF_OFFSET_SIZE
27151 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27152 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27153 set_indirect_string (find_AT_string (ref->info));
27154 break;
27155 case DW_MACRO_define_strp:
27156 case DW_MACRO_undef_strp:
27157 set_indirect_string (find_AT_string (ref->info));
27158 break;
27159 default:
27160 break;
27165 /* Output macinfo section(s). */
27167 static void
27168 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27170 unsigned i;
27171 unsigned long length = vec_safe_length (macinfo_table);
27172 macinfo_entry *ref;
27173 vec<macinfo_entry, va_gc> *files = NULL;
27174 macinfo_hash_type *macinfo_htab = NULL;
27175 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27177 if (! length)
27178 return;
27180 /* output_macinfo* uses these interchangeably. */
27181 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27182 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27183 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27184 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27186 /* AIX Assembler inserts the length, so adjust the reference to match the
27187 offset expected by debuggers. */
27188 strcpy (dl_section_ref, debug_line_label);
27189 if (XCOFF_DEBUGGING_INFO)
27190 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27192 /* For .debug_macro emit the section header. */
27193 if (!dwarf_strict || dwarf_version >= 5)
27195 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27196 "DWARF macro version number");
27197 if (DWARF_OFFSET_SIZE == 8)
27198 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27199 else
27200 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27201 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27202 debug_line_section, NULL);
27205 /* In the first loop, it emits the primary .debug_macinfo section
27206 and after each emitted op the macinfo_entry is cleared.
27207 If a longer range of define/undef ops can be optimized using
27208 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27209 the vector before the first define/undef in the range and the
27210 whole range of define/undef ops is not emitted and kept. */
27211 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27213 switch (ref->code)
27215 case DW_MACINFO_start_file:
27216 vec_safe_push (files, *ref);
27217 break;
27218 case DW_MACINFO_end_file:
27219 if (!vec_safe_is_empty (files))
27220 files->pop ();
27221 break;
27222 case DW_MACINFO_define:
27223 case DW_MACINFO_undef:
27224 if ((!dwarf_strict || dwarf_version >= 5)
27225 && HAVE_COMDAT_GROUP
27226 && vec_safe_length (files) != 1
27227 && i > 0
27228 && i + 1 < length
27229 && (*macinfo_table)[i - 1].code == 0)
27231 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27232 if (count)
27234 i += count - 1;
27235 continue;
27238 break;
27239 case 0:
27240 /* A dummy entry may be inserted at the beginning to be able
27241 to optimize the whole block of predefined macros. */
27242 if (i == 0)
27243 continue;
27244 default:
27245 break;
27247 output_macinfo_op (ref);
27248 ref->info = NULL;
27249 ref->code = 0;
27252 if (!macinfo_htab)
27253 return;
27255 /* Save the number of transparent includes so we can adjust the
27256 label number for the fat LTO object DWARF. */
27257 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27259 delete macinfo_htab;
27260 macinfo_htab = NULL;
27262 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27263 terminate the current chain and switch to a new comdat .debug_macinfo
27264 section and emit the define/undef entries within it. */
27265 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27266 switch (ref->code)
27268 case 0:
27269 continue;
27270 case DW_MACRO_import:
27272 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27273 tree comdat_key = get_identifier (ref->info);
27274 /* Terminate the previous .debug_macinfo section. */
27275 dw2_asm_output_data (1, 0, "End compilation unit");
27276 targetm.asm_out.named_section (debug_macinfo_section_name,
27277 SECTION_DEBUG
27278 | SECTION_LINKONCE
27279 | (early_lto_debug
27280 ? SECTION_EXCLUDE : 0),
27281 comdat_key);
27282 ASM_GENERATE_INTERNAL_LABEL (label,
27283 DEBUG_MACRO_SECTION_LABEL,
27284 ref->lineno + macinfo_label_base);
27285 ASM_OUTPUT_LABEL (asm_out_file, label);
27286 ref->code = 0;
27287 ref->info = NULL;
27288 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27289 "DWARF macro version number");
27290 if (DWARF_OFFSET_SIZE == 8)
27291 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27292 else
27293 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27295 break;
27296 case DW_MACINFO_define:
27297 case DW_MACINFO_undef:
27298 output_macinfo_op (ref);
27299 ref->code = 0;
27300 ref->info = NULL;
27301 break;
27302 default:
27303 gcc_unreachable ();
27306 macinfo_label_base += macinfo_label_base_adj;
27309 /* Initialize the various sections and labels for dwarf output and prefix
27310 them with PREFIX if non-NULL. Returns the generation (zero based
27311 number of times function was called). */
27313 static unsigned
27314 init_sections_and_labels (bool early_lto_debug)
27316 /* As we may get called multiple times have a generation count for
27317 labels. */
27318 static unsigned generation = 0;
27320 if (early_lto_debug)
27322 if (!dwarf_split_debug_info)
27324 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27325 SECTION_DEBUG | SECTION_EXCLUDE,
27326 NULL);
27327 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27328 SECTION_DEBUG | SECTION_EXCLUDE,
27329 NULL);
27330 debug_macinfo_section_name
27331 = ((dwarf_strict && dwarf_version < 5)
27332 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27333 debug_macinfo_section = get_section (debug_macinfo_section_name,
27334 SECTION_DEBUG
27335 | SECTION_EXCLUDE, NULL);
27336 /* For macro info we have to refer to a debug_line section, so
27337 similar to split-dwarf emit a skeleton one for early debug. */
27338 debug_skeleton_line_section
27339 = get_section (DEBUG_LTO_LINE_SECTION,
27340 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27341 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27342 DEBUG_SKELETON_LINE_SECTION_LABEL,
27343 generation);
27345 else
27347 /* ??? Which of the following do we need early? */
27348 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27349 SECTION_DEBUG | SECTION_EXCLUDE,
27350 NULL);
27351 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27352 SECTION_DEBUG | SECTION_EXCLUDE,
27353 NULL);
27354 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27355 SECTION_DEBUG
27356 | SECTION_EXCLUDE, NULL);
27357 debug_skeleton_abbrev_section
27358 = get_section (DEBUG_LTO_ABBREV_SECTION,
27359 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27360 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27361 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27362 generation);
27364 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27365 stay in the main .o, but the skeleton_line goes into the split
27366 off dwo. */
27367 debug_skeleton_line_section
27368 = get_section (DEBUG_LTO_LINE_SECTION,
27369 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27370 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27371 DEBUG_SKELETON_LINE_SECTION_LABEL,
27372 generation);
27373 debug_str_offsets_section
27374 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27375 SECTION_DEBUG | SECTION_EXCLUDE,
27376 NULL);
27377 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27378 DEBUG_SKELETON_INFO_SECTION_LABEL,
27379 generation);
27380 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27381 DEBUG_STR_DWO_SECTION_FLAGS,
27382 NULL);
27383 debug_macinfo_section_name
27384 = ((dwarf_strict && dwarf_version < 5)
27385 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27386 debug_macinfo_section = get_section (debug_macinfo_section_name,
27387 SECTION_DEBUG | SECTION_EXCLUDE,
27388 NULL);
27390 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27391 DEBUG_STR_SECTION_FLAGS
27392 | SECTION_EXCLUDE, NULL);
27393 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27394 debug_line_str_section
27395 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27396 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27398 else
27400 if (!dwarf_split_debug_info)
27402 debug_info_section = get_section (DEBUG_INFO_SECTION,
27403 SECTION_DEBUG, NULL);
27404 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27405 SECTION_DEBUG, NULL);
27406 debug_loc_section = get_section (dwarf_version >= 5
27407 ? DEBUG_LOCLISTS_SECTION
27408 : DEBUG_LOC_SECTION,
27409 SECTION_DEBUG, NULL);
27410 debug_macinfo_section_name
27411 = ((dwarf_strict && dwarf_version < 5)
27412 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27413 debug_macinfo_section = get_section (debug_macinfo_section_name,
27414 SECTION_DEBUG, NULL);
27416 else
27418 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27419 SECTION_DEBUG | SECTION_EXCLUDE,
27420 NULL);
27421 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27422 SECTION_DEBUG | SECTION_EXCLUDE,
27423 NULL);
27424 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27425 SECTION_DEBUG, NULL);
27426 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27427 SECTION_DEBUG, NULL);
27428 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27429 SECTION_DEBUG, NULL);
27430 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27431 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27432 generation);
27434 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27435 stay in the main .o, but the skeleton_line goes into the
27436 split off dwo. */
27437 debug_skeleton_line_section
27438 = get_section (DEBUG_DWO_LINE_SECTION,
27439 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27440 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27441 DEBUG_SKELETON_LINE_SECTION_LABEL,
27442 generation);
27443 debug_str_offsets_section
27444 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27445 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27446 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27447 DEBUG_SKELETON_INFO_SECTION_LABEL,
27448 generation);
27449 debug_loc_section = get_section (dwarf_version >= 5
27450 ? DEBUG_DWO_LOCLISTS_SECTION
27451 : DEBUG_DWO_LOC_SECTION,
27452 SECTION_DEBUG | SECTION_EXCLUDE,
27453 NULL);
27454 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27455 DEBUG_STR_DWO_SECTION_FLAGS,
27456 NULL);
27457 debug_macinfo_section_name
27458 = ((dwarf_strict && dwarf_version < 5)
27459 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27460 debug_macinfo_section = get_section (debug_macinfo_section_name,
27461 SECTION_DEBUG | SECTION_EXCLUDE,
27462 NULL);
27464 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27465 SECTION_DEBUG, NULL);
27466 debug_line_section = get_section (DEBUG_LINE_SECTION,
27467 SECTION_DEBUG, NULL);
27468 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27469 SECTION_DEBUG, NULL);
27470 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27471 SECTION_DEBUG, NULL);
27472 debug_str_section = get_section (DEBUG_STR_SECTION,
27473 DEBUG_STR_SECTION_FLAGS, NULL);
27474 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27475 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27476 DEBUG_STR_SECTION_FLAGS, NULL);
27477 debug_ranges_section = get_section (dwarf_version >= 5
27478 ? DEBUG_RNGLISTS_SECTION
27479 : DEBUG_RANGES_SECTION,
27480 SECTION_DEBUG, NULL);
27481 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27482 SECTION_DEBUG, NULL);
27485 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27486 DEBUG_ABBREV_SECTION_LABEL, generation);
27487 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27488 DEBUG_INFO_SECTION_LABEL, generation);
27489 info_section_emitted = false;
27490 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27491 DEBUG_LINE_SECTION_LABEL, generation);
27492 /* There are up to 4 unique ranges labels per generation.
27493 See also output_rnglists. */
27494 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27495 DEBUG_RANGES_SECTION_LABEL, generation * 4);
27496 if (dwarf_version >= 5 && dwarf_split_debug_info)
27497 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27498 DEBUG_RANGES_SECTION_LABEL,
27499 1 + generation * 4);
27500 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27501 DEBUG_ADDR_SECTION_LABEL, generation);
27502 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27503 (dwarf_strict && dwarf_version < 5)
27504 ? DEBUG_MACINFO_SECTION_LABEL
27505 : DEBUG_MACRO_SECTION_LABEL, generation);
27506 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27507 generation);
27509 ++generation;
27510 return generation - 1;
27513 /* Set up for Dwarf output at the start of compilation. */
27515 static void
27516 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27518 /* Allocate the file_table. */
27519 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27521 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27522 /* Allocate the decl_die_table. */
27523 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27525 /* Allocate the decl_loc_table. */
27526 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27528 /* Allocate the cached_dw_loc_list_table. */
27529 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27531 /* Allocate the initial hunk of the decl_scope_table. */
27532 vec_alloc (decl_scope_table, 256);
27534 /* Allocate the initial hunk of the abbrev_die_table. */
27535 vec_alloc (abbrev_die_table, 256);
27536 /* Zero-th entry is allocated, but unused. */
27537 abbrev_die_table->quick_push (NULL);
27539 /* Allocate the dwarf_proc_stack_usage_map. */
27540 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27542 /* Allocate the pubtypes and pubnames vectors. */
27543 vec_alloc (pubname_table, 32);
27544 vec_alloc (pubtype_table, 32);
27546 vec_alloc (incomplete_types, 64);
27548 vec_alloc (used_rtx_array, 32);
27550 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27551 vec_alloc (macinfo_table, 64);
27552 #endif
27554 /* If front-ends already registered a main translation unit but we were not
27555 ready to perform the association, do this now. */
27556 if (main_translation_unit != NULL_TREE)
27557 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27560 /* Called before compile () starts outputtting functions, variables
27561 and toplevel asms into assembly. */
27563 static void
27564 dwarf2out_assembly_start (void)
27566 if (text_section_line_info)
27567 return;
27569 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27570 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27571 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27572 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27573 COLD_TEXT_SECTION_LABEL, 0);
27574 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27576 switch_to_section (text_section);
27577 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27578 #endif
27580 /* Make sure the line number table for .text always exists. */
27581 text_section_line_info = new_line_info_table ();
27582 text_section_line_info->end_label = text_end_label;
27584 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27585 cur_line_info_table = text_section_line_info;
27586 #endif
27588 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27589 && dwarf2out_do_cfi_asm ()
27590 && !dwarf2out_do_eh_frame ())
27591 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27594 /* A helper function for dwarf2out_finish called through
27595 htab_traverse. Assign a string its index. All strings must be
27596 collected into the table by the time index_string is called,
27597 because the indexing code relies on htab_traverse to traverse nodes
27598 in the same order for each run. */
27601 index_string (indirect_string_node **h, unsigned int *index)
27603 indirect_string_node *node = *h;
27605 find_string_form (node);
27606 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27608 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27609 node->index = *index;
27610 *index += 1;
27612 return 1;
27615 /* A helper function for output_indirect_strings called through
27616 htab_traverse. Output the offset to a string and update the
27617 current offset. */
27620 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27622 indirect_string_node *node = *h;
27624 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27626 /* Assert that this node has been assigned an index. */
27627 gcc_assert (node->index != NO_INDEX_ASSIGNED
27628 && node->index != NOT_INDEXED);
27629 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27630 "indexed string 0x%x: %s", node->index, node->str);
27631 *offset += strlen (node->str) + 1;
27633 return 1;
27636 /* A helper function for dwarf2out_finish called through
27637 htab_traverse. Output the indexed string. */
27640 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27642 struct indirect_string_node *node = *h;
27644 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27646 /* Assert that the strings are output in the same order as their
27647 indexes were assigned. */
27648 gcc_assert (*cur_idx == node->index);
27649 assemble_string (node->str, strlen (node->str) + 1);
27650 *cur_idx += 1;
27652 return 1;
27655 /* A helper function for dwarf2out_finish called through
27656 htab_traverse. Emit one queued .debug_str string. */
27659 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27661 struct indirect_string_node *node = *h;
27663 node->form = find_string_form (node);
27664 if (node->form == form && node->refcount > 0)
27666 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27667 assemble_string (node->str, strlen (node->str) + 1);
27670 return 1;
27673 /* Output the indexed string table. */
27675 static void
27676 output_indirect_strings (void)
27678 switch_to_section (debug_str_section);
27679 if (!dwarf_split_debug_info)
27680 debug_str_hash->traverse<enum dwarf_form,
27681 output_indirect_string> (DW_FORM_strp);
27682 else
27684 unsigned int offset = 0;
27685 unsigned int cur_idx = 0;
27687 skeleton_debug_str_hash->traverse<enum dwarf_form,
27688 output_indirect_string> (DW_FORM_strp);
27690 switch_to_section (debug_str_offsets_section);
27691 debug_str_hash->traverse_noresize
27692 <unsigned int *, output_index_string_offset> (&offset);
27693 switch_to_section (debug_str_dwo_section);
27694 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27695 (&cur_idx);
27699 /* Callback for htab_traverse to assign an index to an entry in the
27700 table, and to write that entry to the .debug_addr section. */
27703 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27705 addr_table_entry *entry = *slot;
27707 if (entry->refcount == 0)
27709 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27710 || entry->index == NOT_INDEXED);
27711 return 1;
27714 gcc_assert (entry->index == *cur_index);
27715 (*cur_index)++;
27717 switch (entry->kind)
27719 case ate_kind_rtx:
27720 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27721 "0x%x", entry->index);
27722 break;
27723 case ate_kind_rtx_dtprel:
27724 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27725 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27726 DWARF2_ADDR_SIZE,
27727 entry->addr.rtl);
27728 fputc ('\n', asm_out_file);
27729 break;
27730 case ate_kind_label:
27731 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27732 "0x%x", entry->index);
27733 break;
27734 default:
27735 gcc_unreachable ();
27737 return 1;
27740 /* Produce the .debug_addr section. */
27742 static void
27743 output_addr_table (void)
27745 unsigned int index = 0;
27746 if (addr_index_table == NULL || addr_index_table->size () == 0)
27747 return;
27749 switch_to_section (debug_addr_section);
27750 addr_index_table
27751 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27754 #if ENABLE_ASSERT_CHECKING
27755 /* Verify that all marks are clear. */
27757 static void
27758 verify_marks_clear (dw_die_ref die)
27760 dw_die_ref c;
27762 gcc_assert (! die->die_mark);
27763 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27765 #endif /* ENABLE_ASSERT_CHECKING */
27767 /* Clear the marks for a die and its children.
27768 Be cool if the mark isn't set. */
27770 static void
27771 prune_unmark_dies (dw_die_ref die)
27773 dw_die_ref c;
27775 if (die->die_mark)
27776 die->die_mark = 0;
27777 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27780 /* Given LOC that is referenced by a DIE we're marking as used, find all
27781 referenced DWARF procedures it references and mark them as used. */
27783 static void
27784 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27786 for (; loc != NULL; loc = loc->dw_loc_next)
27787 switch (loc->dw_loc_opc)
27789 case DW_OP_implicit_pointer:
27790 case DW_OP_convert:
27791 case DW_OP_reinterpret:
27792 case DW_OP_GNU_implicit_pointer:
27793 case DW_OP_GNU_convert:
27794 case DW_OP_GNU_reinterpret:
27795 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27796 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27797 break;
27798 case DW_OP_GNU_variable_value:
27799 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27801 dw_die_ref ref
27802 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27803 if (ref == NULL)
27804 break;
27805 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27806 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27807 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27809 /* FALLTHRU */
27810 case DW_OP_call2:
27811 case DW_OP_call4:
27812 case DW_OP_call_ref:
27813 case DW_OP_const_type:
27814 case DW_OP_GNU_const_type:
27815 case DW_OP_GNU_parameter_ref:
27816 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27817 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27818 break;
27819 case DW_OP_regval_type:
27820 case DW_OP_deref_type:
27821 case DW_OP_GNU_regval_type:
27822 case DW_OP_GNU_deref_type:
27823 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27824 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27825 break;
27826 case DW_OP_entry_value:
27827 case DW_OP_GNU_entry_value:
27828 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27829 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27830 break;
27831 default:
27832 break;
27836 /* Given DIE that we're marking as used, find any other dies
27837 it references as attributes and mark them as used. */
27839 static void
27840 prune_unused_types_walk_attribs (dw_die_ref die)
27842 dw_attr_node *a;
27843 unsigned ix;
27845 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27847 switch (AT_class (a))
27849 /* Make sure DWARF procedures referenced by location descriptions will
27850 get emitted. */
27851 case dw_val_class_loc:
27852 prune_unused_types_walk_loc_descr (AT_loc (a));
27853 break;
27854 case dw_val_class_loc_list:
27855 for (dw_loc_list_ref list = AT_loc_list (a);
27856 list != NULL;
27857 list = list->dw_loc_next)
27858 prune_unused_types_walk_loc_descr (list->expr);
27859 break;
27861 case dw_val_class_die_ref:
27862 /* A reference to another DIE.
27863 Make sure that it will get emitted.
27864 If it was broken out into a comdat group, don't follow it. */
27865 if (! AT_ref (a)->comdat_type_p
27866 || a->dw_attr == DW_AT_specification)
27867 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27868 break;
27870 case dw_val_class_str:
27871 /* Set the string's refcount to 0 so that prune_unused_types_mark
27872 accounts properly for it. */
27873 a->dw_attr_val.v.val_str->refcount = 0;
27874 break;
27876 default:
27877 break;
27882 /* Mark the generic parameters and arguments children DIEs of DIE. */
27884 static void
27885 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27887 dw_die_ref c;
27889 if (die == NULL || die->die_child == NULL)
27890 return;
27891 c = die->die_child;
27894 if (is_template_parameter (c))
27895 prune_unused_types_mark (c, 1);
27896 c = c->die_sib;
27897 } while (c && c != die->die_child);
27900 /* Mark DIE as being used. If DOKIDS is true, then walk down
27901 to DIE's children. */
27903 static void
27904 prune_unused_types_mark (dw_die_ref die, int dokids)
27906 dw_die_ref c;
27908 if (die->die_mark == 0)
27910 /* We haven't done this node yet. Mark it as used. */
27911 die->die_mark = 1;
27912 /* If this is the DIE of a generic type instantiation,
27913 mark the children DIEs that describe its generic parms and
27914 args. */
27915 prune_unused_types_mark_generic_parms_dies (die);
27917 /* We also have to mark its parents as used.
27918 (But we don't want to mark our parent's kids due to this,
27919 unless it is a class.) */
27920 if (die->die_parent)
27921 prune_unused_types_mark (die->die_parent,
27922 class_scope_p (die->die_parent));
27924 /* Mark any referenced nodes. */
27925 prune_unused_types_walk_attribs (die);
27927 /* If this node is a specification,
27928 also mark the definition, if it exists. */
27929 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27930 prune_unused_types_mark (die->die_definition, 1);
27933 if (dokids && die->die_mark != 2)
27935 /* We need to walk the children, but haven't done so yet.
27936 Remember that we've walked the kids. */
27937 die->die_mark = 2;
27939 /* If this is an array type, we need to make sure our
27940 kids get marked, even if they're types. If we're
27941 breaking out types into comdat sections, do this
27942 for all type definitions. */
27943 if (die->die_tag == DW_TAG_array_type
27944 || (use_debug_types
27945 && is_type_die (die) && ! is_declaration_die (die)))
27946 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27947 else
27948 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27952 /* For local classes, look if any static member functions were emitted
27953 and if so, mark them. */
27955 static void
27956 prune_unused_types_walk_local_classes (dw_die_ref die)
27958 dw_die_ref c;
27960 if (die->die_mark == 2)
27961 return;
27963 switch (die->die_tag)
27965 case DW_TAG_structure_type:
27966 case DW_TAG_union_type:
27967 case DW_TAG_class_type:
27968 break;
27970 case DW_TAG_subprogram:
27971 if (!get_AT_flag (die, DW_AT_declaration)
27972 || die->die_definition != NULL)
27973 prune_unused_types_mark (die, 1);
27974 return;
27976 default:
27977 return;
27980 /* Mark children. */
27981 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27984 /* Walk the tree DIE and mark types that we actually use. */
27986 static void
27987 prune_unused_types_walk (dw_die_ref die)
27989 dw_die_ref c;
27991 /* Don't do anything if this node is already marked and
27992 children have been marked as well. */
27993 if (die->die_mark == 2)
27994 return;
27996 switch (die->die_tag)
27998 case DW_TAG_structure_type:
27999 case DW_TAG_union_type:
28000 case DW_TAG_class_type:
28001 if (die->die_perennial_p)
28002 break;
28004 for (c = die->die_parent; c; c = c->die_parent)
28005 if (c->die_tag == DW_TAG_subprogram)
28006 break;
28008 /* Finding used static member functions inside of classes
28009 is needed just for local classes, because for other classes
28010 static member function DIEs with DW_AT_specification
28011 are emitted outside of the DW_TAG_*_type. If we ever change
28012 it, we'd need to call this even for non-local classes. */
28013 if (c)
28014 prune_unused_types_walk_local_classes (die);
28016 /* It's a type node --- don't mark it. */
28017 return;
28019 case DW_TAG_const_type:
28020 case DW_TAG_packed_type:
28021 case DW_TAG_pointer_type:
28022 case DW_TAG_reference_type:
28023 case DW_TAG_rvalue_reference_type:
28024 case DW_TAG_volatile_type:
28025 case DW_TAG_typedef:
28026 case DW_TAG_array_type:
28027 case DW_TAG_interface_type:
28028 case DW_TAG_friend:
28029 case DW_TAG_enumeration_type:
28030 case DW_TAG_subroutine_type:
28031 case DW_TAG_string_type:
28032 case DW_TAG_set_type:
28033 case DW_TAG_subrange_type:
28034 case DW_TAG_ptr_to_member_type:
28035 case DW_TAG_file_type:
28036 /* Type nodes are useful only when other DIEs reference them --- don't
28037 mark them. */
28038 /* FALLTHROUGH */
28040 case DW_TAG_dwarf_procedure:
28041 /* Likewise for DWARF procedures. */
28043 if (die->die_perennial_p)
28044 break;
28046 return;
28048 default:
28049 /* Mark everything else. */
28050 break;
28053 if (die->die_mark == 0)
28055 die->die_mark = 1;
28057 /* Now, mark any dies referenced from here. */
28058 prune_unused_types_walk_attribs (die);
28061 die->die_mark = 2;
28063 /* Mark children. */
28064 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28067 /* Increment the string counts on strings referred to from DIE's
28068 attributes. */
28070 static void
28071 prune_unused_types_update_strings (dw_die_ref die)
28073 dw_attr_node *a;
28074 unsigned ix;
28076 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28077 if (AT_class (a) == dw_val_class_str)
28079 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28080 s->refcount++;
28081 /* Avoid unnecessarily putting strings that are used less than
28082 twice in the hash table. */
28083 if (s->refcount
28084 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28086 indirect_string_node **slot
28087 = debug_str_hash->find_slot_with_hash (s->str,
28088 htab_hash_string (s->str),
28089 INSERT);
28090 gcc_assert (*slot == NULL);
28091 *slot = s;
28096 /* Mark DIE and its children as removed. */
28098 static void
28099 mark_removed (dw_die_ref die)
28101 dw_die_ref c;
28102 die->removed = true;
28103 FOR_EACH_CHILD (die, c, mark_removed (c));
28106 /* Remove from the tree DIE any dies that aren't marked. */
28108 static void
28109 prune_unused_types_prune (dw_die_ref die)
28111 dw_die_ref c;
28113 gcc_assert (die->die_mark);
28114 prune_unused_types_update_strings (die);
28116 if (! die->die_child)
28117 return;
28119 c = die->die_child;
28120 do {
28121 dw_die_ref prev = c, next;
28122 for (c = c->die_sib; ! c->die_mark; c = next)
28123 if (c == die->die_child)
28125 /* No marked children between 'prev' and the end of the list. */
28126 if (prev == c)
28127 /* No marked children at all. */
28128 die->die_child = NULL;
28129 else
28131 prev->die_sib = c->die_sib;
28132 die->die_child = prev;
28134 c->die_sib = NULL;
28135 mark_removed (c);
28136 return;
28138 else
28140 next = c->die_sib;
28141 c->die_sib = NULL;
28142 mark_removed (c);
28145 if (c != prev->die_sib)
28146 prev->die_sib = c;
28147 prune_unused_types_prune (c);
28148 } while (c != die->die_child);
28151 /* Remove dies representing declarations that we never use. */
28153 static void
28154 prune_unused_types (void)
28156 unsigned int i;
28157 limbo_die_node *node;
28158 comdat_type_node *ctnode;
28159 pubname_entry *pub;
28160 dw_die_ref base_type;
28162 #if ENABLE_ASSERT_CHECKING
28163 /* All the marks should already be clear. */
28164 verify_marks_clear (comp_unit_die ());
28165 for (node = limbo_die_list; node; node = node->next)
28166 verify_marks_clear (node->die);
28167 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28168 verify_marks_clear (ctnode->root_die);
28169 #endif /* ENABLE_ASSERT_CHECKING */
28171 /* Mark types that are used in global variables. */
28172 premark_types_used_by_global_vars ();
28174 /* Set the mark on nodes that are actually used. */
28175 prune_unused_types_walk (comp_unit_die ());
28176 for (node = limbo_die_list; node; node = node->next)
28177 prune_unused_types_walk (node->die);
28178 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28180 prune_unused_types_walk (ctnode->root_die);
28181 prune_unused_types_mark (ctnode->type_die, 1);
28184 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28185 are unusual in that they are pubnames that are the children of pubtypes.
28186 They should only be marked via their parent DW_TAG_enumeration_type die,
28187 not as roots in themselves. */
28188 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28189 if (pub->die->die_tag != DW_TAG_enumerator)
28190 prune_unused_types_mark (pub->die, 1);
28191 for (i = 0; base_types.iterate (i, &base_type); i++)
28192 prune_unused_types_mark (base_type, 1);
28194 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28195 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28196 callees). */
28197 cgraph_node *cnode;
28198 FOR_EACH_FUNCTION (cnode)
28199 if (cnode->referred_to_p (false))
28201 dw_die_ref die = lookup_decl_die (cnode->decl);
28202 if (die == NULL || die->die_mark)
28203 continue;
28204 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28205 if (e->caller != cnode
28206 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28208 prune_unused_types_mark (die, 1);
28209 break;
28213 if (debug_str_hash)
28214 debug_str_hash->empty ();
28215 if (skeleton_debug_str_hash)
28216 skeleton_debug_str_hash->empty ();
28217 prune_unused_types_prune (comp_unit_die ());
28218 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28220 node = *pnode;
28221 if (!node->die->die_mark)
28222 *pnode = node->next;
28223 else
28225 prune_unused_types_prune (node->die);
28226 pnode = &node->next;
28229 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28230 prune_unused_types_prune (ctnode->root_die);
28232 /* Leave the marks clear. */
28233 prune_unmark_dies (comp_unit_die ());
28234 for (node = limbo_die_list; node; node = node->next)
28235 prune_unmark_dies (node->die);
28236 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28237 prune_unmark_dies (ctnode->root_die);
28240 /* Helpers to manipulate hash table of comdat type units. */
28242 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28244 static inline hashval_t hash (const comdat_type_node *);
28245 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28248 inline hashval_t
28249 comdat_type_hasher::hash (const comdat_type_node *type_node)
28251 hashval_t h;
28252 memcpy (&h, type_node->signature, sizeof (h));
28253 return h;
28256 inline bool
28257 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28258 const comdat_type_node *type_node_2)
28260 return (! memcmp (type_node_1->signature, type_node_2->signature,
28261 DWARF_TYPE_SIGNATURE_SIZE));
28264 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28265 to the location it would have been added, should we know its
28266 DECL_ASSEMBLER_NAME when we added other attributes. This will
28267 probably improve compactness of debug info, removing equivalent
28268 abbrevs, and hide any differences caused by deferring the
28269 computation of the assembler name, triggered by e.g. PCH. */
28271 static inline void
28272 move_linkage_attr (dw_die_ref die)
28274 unsigned ix = vec_safe_length (die->die_attr);
28275 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28277 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28278 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28280 while (--ix > 0)
28282 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28284 if (prev->dw_attr == DW_AT_decl_line
28285 || prev->dw_attr == DW_AT_decl_column
28286 || prev->dw_attr == DW_AT_name)
28287 break;
28290 if (ix != vec_safe_length (die->die_attr) - 1)
28292 die->die_attr->pop ();
28293 die->die_attr->quick_insert (ix, linkage);
28297 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28298 referenced from typed stack ops and count how often they are used. */
28300 static void
28301 mark_base_types (dw_loc_descr_ref loc)
28303 dw_die_ref base_type = NULL;
28305 for (; loc; loc = loc->dw_loc_next)
28307 switch (loc->dw_loc_opc)
28309 case DW_OP_regval_type:
28310 case DW_OP_deref_type:
28311 case DW_OP_GNU_regval_type:
28312 case DW_OP_GNU_deref_type:
28313 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28314 break;
28315 case DW_OP_convert:
28316 case DW_OP_reinterpret:
28317 case DW_OP_GNU_convert:
28318 case DW_OP_GNU_reinterpret:
28319 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28320 continue;
28321 /* FALLTHRU */
28322 case DW_OP_const_type:
28323 case DW_OP_GNU_const_type:
28324 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28325 break;
28326 case DW_OP_entry_value:
28327 case DW_OP_GNU_entry_value:
28328 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28329 continue;
28330 default:
28331 continue;
28333 gcc_assert (base_type->die_parent == comp_unit_die ());
28334 if (base_type->die_mark)
28335 base_type->die_mark++;
28336 else
28338 base_types.safe_push (base_type);
28339 base_type->die_mark = 1;
28344 /* Comparison function for sorting marked base types. */
28346 static int
28347 base_type_cmp (const void *x, const void *y)
28349 dw_die_ref dx = *(const dw_die_ref *) x;
28350 dw_die_ref dy = *(const dw_die_ref *) y;
28351 unsigned int byte_size1, byte_size2;
28352 unsigned int encoding1, encoding2;
28353 unsigned int align1, align2;
28354 if (dx->die_mark > dy->die_mark)
28355 return -1;
28356 if (dx->die_mark < dy->die_mark)
28357 return 1;
28358 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28359 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28360 if (byte_size1 < byte_size2)
28361 return 1;
28362 if (byte_size1 > byte_size2)
28363 return -1;
28364 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28365 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28366 if (encoding1 < encoding2)
28367 return 1;
28368 if (encoding1 > encoding2)
28369 return -1;
28370 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28371 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28372 if (align1 < align2)
28373 return 1;
28374 if (align1 > align2)
28375 return -1;
28376 return 0;
28379 /* Move base types marked by mark_base_types as early as possible
28380 in the CU, sorted by decreasing usage count both to make the
28381 uleb128 references as small as possible and to make sure they
28382 will have die_offset already computed by calc_die_sizes when
28383 sizes of typed stack loc ops is computed. */
28385 static void
28386 move_marked_base_types (void)
28388 unsigned int i;
28389 dw_die_ref base_type, die, c;
28391 if (base_types.is_empty ())
28392 return;
28394 /* Sort by decreasing usage count, they will be added again in that
28395 order later on. */
28396 base_types.qsort (base_type_cmp);
28397 die = comp_unit_die ();
28398 c = die->die_child;
28401 dw_die_ref prev = c;
28402 c = c->die_sib;
28403 while (c->die_mark)
28405 remove_child_with_prev (c, prev);
28406 /* As base types got marked, there must be at least
28407 one node other than DW_TAG_base_type. */
28408 gcc_assert (die->die_child != NULL);
28409 c = prev->die_sib;
28412 while (c != die->die_child);
28413 gcc_assert (die->die_child);
28414 c = die->die_child;
28415 for (i = 0; base_types.iterate (i, &base_type); i++)
28417 base_type->die_mark = 0;
28418 base_type->die_sib = c->die_sib;
28419 c->die_sib = base_type;
28420 c = base_type;
28424 /* Helper function for resolve_addr, attempt to resolve
28425 one CONST_STRING, return true if successful. Similarly verify that
28426 SYMBOL_REFs refer to variables emitted in the current CU. */
28428 static bool
28429 resolve_one_addr (rtx *addr)
28431 rtx rtl = *addr;
28433 if (GET_CODE (rtl) == CONST_STRING)
28435 size_t len = strlen (XSTR (rtl, 0)) + 1;
28436 tree t = build_string (len, XSTR (rtl, 0));
28437 tree tlen = size_int (len - 1);
28438 TREE_TYPE (t)
28439 = build_array_type (char_type_node, build_index_type (tlen));
28440 rtl = lookup_constant_def (t);
28441 if (!rtl || !MEM_P (rtl))
28442 return false;
28443 rtl = XEXP (rtl, 0);
28444 if (GET_CODE (rtl) == SYMBOL_REF
28445 && SYMBOL_REF_DECL (rtl)
28446 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28447 return false;
28448 vec_safe_push (used_rtx_array, rtl);
28449 *addr = rtl;
28450 return true;
28453 if (GET_CODE (rtl) == SYMBOL_REF
28454 && SYMBOL_REF_DECL (rtl))
28456 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28458 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28459 return false;
28461 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28462 return false;
28465 if (GET_CODE (rtl) == CONST)
28467 subrtx_ptr_iterator::array_type array;
28468 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28469 if (!resolve_one_addr (*iter))
28470 return false;
28473 return true;
28476 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28477 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28478 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28480 static rtx
28481 string_cst_pool_decl (tree t)
28483 rtx rtl = output_constant_def (t, 1);
28484 unsigned char *array;
28485 dw_loc_descr_ref l;
28486 tree decl;
28487 size_t len;
28488 dw_die_ref ref;
28490 if (!rtl || !MEM_P (rtl))
28491 return NULL_RTX;
28492 rtl = XEXP (rtl, 0);
28493 if (GET_CODE (rtl) != SYMBOL_REF
28494 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28495 return NULL_RTX;
28497 decl = SYMBOL_REF_DECL (rtl);
28498 if (!lookup_decl_die (decl))
28500 len = TREE_STRING_LENGTH (t);
28501 vec_safe_push (used_rtx_array, rtl);
28502 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28503 array = ggc_vec_alloc<unsigned char> (len);
28504 memcpy (array, TREE_STRING_POINTER (t), len);
28505 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28506 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28507 l->dw_loc_oprnd2.v.val_vec.length = len;
28508 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28509 l->dw_loc_oprnd2.v.val_vec.array = array;
28510 add_AT_loc (ref, DW_AT_location, l);
28511 equate_decl_number_to_die (decl, ref);
28513 return rtl;
28516 /* Helper function of resolve_addr_in_expr. LOC is
28517 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28518 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28519 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28520 with DW_OP_implicit_pointer if possible
28521 and return true, if unsuccessful, return false. */
28523 static bool
28524 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28526 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28527 HOST_WIDE_INT offset = 0;
28528 dw_die_ref ref = NULL;
28529 tree decl;
28531 if (GET_CODE (rtl) == CONST
28532 && GET_CODE (XEXP (rtl, 0)) == PLUS
28533 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28535 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28536 rtl = XEXP (XEXP (rtl, 0), 0);
28538 if (GET_CODE (rtl) == CONST_STRING)
28540 size_t len = strlen (XSTR (rtl, 0)) + 1;
28541 tree t = build_string (len, XSTR (rtl, 0));
28542 tree tlen = size_int (len - 1);
28544 TREE_TYPE (t)
28545 = build_array_type (char_type_node, build_index_type (tlen));
28546 rtl = string_cst_pool_decl (t);
28547 if (!rtl)
28548 return false;
28550 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28552 decl = SYMBOL_REF_DECL (rtl);
28553 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28555 ref = lookup_decl_die (decl);
28556 if (ref && (get_AT (ref, DW_AT_location)
28557 || get_AT (ref, DW_AT_const_value)))
28559 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28560 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28561 loc->dw_loc_oprnd1.val_entry = NULL;
28562 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28563 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28564 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28565 loc->dw_loc_oprnd2.v.val_int = offset;
28566 return true;
28570 return false;
28573 /* Helper function for resolve_addr, handle one location
28574 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28575 the location list couldn't be resolved. */
28577 static bool
28578 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28580 dw_loc_descr_ref keep = NULL;
28581 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28582 switch (loc->dw_loc_opc)
28584 case DW_OP_addr:
28585 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28587 if ((prev == NULL
28588 || prev->dw_loc_opc == DW_OP_piece
28589 || prev->dw_loc_opc == DW_OP_bit_piece)
28590 && loc->dw_loc_next
28591 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28592 && (!dwarf_strict || dwarf_version >= 5)
28593 && optimize_one_addr_into_implicit_ptr (loc))
28594 break;
28595 return false;
28597 break;
28598 case DW_OP_GNU_addr_index:
28599 case DW_OP_GNU_const_index:
28600 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28601 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28603 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28604 if (!resolve_one_addr (&rtl))
28605 return false;
28606 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28607 loc->dw_loc_oprnd1.val_entry
28608 = add_addr_table_entry (rtl, ate_kind_rtx);
28610 break;
28611 case DW_OP_const4u:
28612 case DW_OP_const8u:
28613 if (loc->dtprel
28614 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28615 return false;
28616 break;
28617 case DW_OP_plus_uconst:
28618 if (size_of_loc_descr (loc)
28619 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28621 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28623 dw_loc_descr_ref repl
28624 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28625 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28626 add_loc_descr (&repl, loc->dw_loc_next);
28627 *loc = *repl;
28629 break;
28630 case DW_OP_implicit_value:
28631 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28632 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28633 return false;
28634 break;
28635 case DW_OP_implicit_pointer:
28636 case DW_OP_GNU_implicit_pointer:
28637 case DW_OP_GNU_parameter_ref:
28638 case DW_OP_GNU_variable_value:
28639 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28641 dw_die_ref ref
28642 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28643 if (ref == NULL)
28644 return false;
28645 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28646 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28647 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28649 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28651 if (prev == NULL
28652 && loc->dw_loc_next == NULL
28653 && AT_class (a) == dw_val_class_loc)
28654 switch (a->dw_attr)
28656 /* Following attributes allow both exprloc and reference,
28657 so if the whole expression is DW_OP_GNU_variable_value
28658 alone we could transform it into reference. */
28659 case DW_AT_byte_size:
28660 case DW_AT_bit_size:
28661 case DW_AT_lower_bound:
28662 case DW_AT_upper_bound:
28663 case DW_AT_bit_stride:
28664 case DW_AT_count:
28665 case DW_AT_allocated:
28666 case DW_AT_associated:
28667 case DW_AT_byte_stride:
28668 a->dw_attr_val.val_class = dw_val_class_die_ref;
28669 a->dw_attr_val.val_entry = NULL;
28670 a->dw_attr_val.v.val_die_ref.die
28671 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28672 a->dw_attr_val.v.val_die_ref.external = 0;
28673 return true;
28674 default:
28675 break;
28677 if (dwarf_strict)
28678 return false;
28680 break;
28681 case DW_OP_const_type:
28682 case DW_OP_regval_type:
28683 case DW_OP_deref_type:
28684 case DW_OP_convert:
28685 case DW_OP_reinterpret:
28686 case DW_OP_GNU_const_type:
28687 case DW_OP_GNU_regval_type:
28688 case DW_OP_GNU_deref_type:
28689 case DW_OP_GNU_convert:
28690 case DW_OP_GNU_reinterpret:
28691 while (loc->dw_loc_next
28692 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28693 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28695 dw_die_ref base1, base2;
28696 unsigned enc1, enc2, size1, size2;
28697 if (loc->dw_loc_opc == DW_OP_regval_type
28698 || loc->dw_loc_opc == DW_OP_deref_type
28699 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28700 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28701 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28702 else if (loc->dw_loc_oprnd1.val_class
28703 == dw_val_class_unsigned_const)
28704 break;
28705 else
28706 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28707 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28708 == dw_val_class_unsigned_const)
28709 break;
28710 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28711 gcc_assert (base1->die_tag == DW_TAG_base_type
28712 && base2->die_tag == DW_TAG_base_type);
28713 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28714 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28715 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28716 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28717 if (size1 == size2
28718 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28719 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28720 && loc != keep)
28721 || enc1 == enc2))
28723 /* Optimize away next DW_OP_convert after
28724 adjusting LOC's base type die reference. */
28725 if (loc->dw_loc_opc == DW_OP_regval_type
28726 || loc->dw_loc_opc == DW_OP_deref_type
28727 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28728 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28729 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28730 else
28731 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28732 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28733 continue;
28735 /* Don't change integer DW_OP_convert after e.g. floating
28736 point typed stack entry. */
28737 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28738 keep = loc->dw_loc_next;
28739 break;
28741 break;
28742 default:
28743 break;
28745 return true;
28748 /* Helper function of resolve_addr. DIE had DW_AT_location of
28749 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28750 and DW_OP_addr couldn't be resolved. resolve_addr has already
28751 removed the DW_AT_location attribute. This function attempts to
28752 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28753 to it or DW_AT_const_value attribute, if possible. */
28755 static void
28756 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28758 if (!VAR_P (decl)
28759 || lookup_decl_die (decl) != die
28760 || DECL_EXTERNAL (decl)
28761 || !TREE_STATIC (decl)
28762 || DECL_INITIAL (decl) == NULL_TREE
28763 || DECL_P (DECL_INITIAL (decl))
28764 || get_AT (die, DW_AT_const_value))
28765 return;
28767 tree init = DECL_INITIAL (decl);
28768 HOST_WIDE_INT offset = 0;
28769 /* For variables that have been optimized away and thus
28770 don't have a memory location, see if we can emit
28771 DW_AT_const_value instead. */
28772 if (tree_add_const_value_attribute (die, init))
28773 return;
28774 if (dwarf_strict && dwarf_version < 5)
28775 return;
28776 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28777 and ADDR_EXPR refers to a decl that has DW_AT_location or
28778 DW_AT_const_value (but isn't addressable, otherwise
28779 resolving the original DW_OP_addr wouldn't fail), see if
28780 we can add DW_OP_implicit_pointer. */
28781 STRIP_NOPS (init);
28782 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28783 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28785 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28786 init = TREE_OPERAND (init, 0);
28787 STRIP_NOPS (init);
28789 if (TREE_CODE (init) != ADDR_EXPR)
28790 return;
28791 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28792 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28793 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28794 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28795 && TREE_OPERAND (init, 0) != decl))
28797 dw_die_ref ref;
28798 dw_loc_descr_ref l;
28800 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28802 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28803 if (!rtl)
28804 return;
28805 decl = SYMBOL_REF_DECL (rtl);
28807 else
28808 decl = TREE_OPERAND (init, 0);
28809 ref = lookup_decl_die (decl);
28810 if (ref == NULL
28811 || (!get_AT (ref, DW_AT_location)
28812 && !get_AT (ref, DW_AT_const_value)))
28813 return;
28814 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28815 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28816 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28817 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28818 add_AT_loc (die, DW_AT_location, l);
28822 /* Return NULL if l is a DWARF expression, or first op that is not
28823 valid DWARF expression. */
28825 static dw_loc_descr_ref
28826 non_dwarf_expression (dw_loc_descr_ref l)
28828 while (l)
28830 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28831 return l;
28832 switch (l->dw_loc_opc)
28834 case DW_OP_regx:
28835 case DW_OP_implicit_value:
28836 case DW_OP_stack_value:
28837 case DW_OP_implicit_pointer:
28838 case DW_OP_GNU_implicit_pointer:
28839 case DW_OP_GNU_parameter_ref:
28840 case DW_OP_piece:
28841 case DW_OP_bit_piece:
28842 return l;
28843 default:
28844 break;
28846 l = l->dw_loc_next;
28848 return NULL;
28851 /* Return adjusted copy of EXPR:
28852 If it is empty DWARF expression, return it.
28853 If it is valid non-empty DWARF expression,
28854 return copy of EXPR with DW_OP_deref appended to it.
28855 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28856 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28857 If it is DWARF expression followed by DW_OP_stack_value, return
28858 copy of the DWARF expression without anything appended.
28859 Otherwise, return NULL. */
28861 static dw_loc_descr_ref
28862 copy_deref_exprloc (dw_loc_descr_ref expr)
28864 dw_loc_descr_ref tail = NULL;
28866 if (expr == NULL)
28867 return NULL;
28869 dw_loc_descr_ref l = non_dwarf_expression (expr);
28870 if (l && l->dw_loc_next)
28871 return NULL;
28873 if (l)
28875 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28876 tail = new_loc_descr ((enum dwarf_location_atom)
28877 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28878 0, 0);
28879 else
28880 switch (l->dw_loc_opc)
28882 case DW_OP_regx:
28883 tail = new_loc_descr (DW_OP_bregx,
28884 l->dw_loc_oprnd1.v.val_unsigned, 0);
28885 break;
28886 case DW_OP_stack_value:
28887 break;
28888 default:
28889 return NULL;
28892 else
28893 tail = new_loc_descr (DW_OP_deref, 0, 0);
28895 dw_loc_descr_ref ret = NULL, *p = &ret;
28896 while (expr != l)
28898 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28899 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28900 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28901 p = &(*p)->dw_loc_next;
28902 expr = expr->dw_loc_next;
28904 *p = tail;
28905 return ret;
28908 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28909 reference to a variable or argument, adjust it if needed and return:
28910 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28911 attribute if present should be removed
28912 0 keep the attribute perhaps with minor modifications, no need to rescan
28913 1 if the attribute has been successfully adjusted. */
28915 static int
28916 optimize_string_length (dw_attr_node *a)
28918 dw_loc_descr_ref l = AT_loc (a), lv;
28919 dw_die_ref die;
28920 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28922 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28923 die = lookup_decl_die (decl);
28924 if (die)
28926 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28927 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28928 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28930 else
28931 return -1;
28933 else
28934 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28936 /* DWARF5 allows reference class, so we can then reference the DIE.
28937 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28938 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28940 a->dw_attr_val.val_class = dw_val_class_die_ref;
28941 a->dw_attr_val.val_entry = NULL;
28942 a->dw_attr_val.v.val_die_ref.die = die;
28943 a->dw_attr_val.v.val_die_ref.external = 0;
28944 return 0;
28947 dw_attr_node *av = get_AT (die, DW_AT_location);
28948 dw_loc_list_ref d;
28949 bool non_dwarf_expr = false;
28951 if (av == NULL)
28952 return dwarf_strict ? -1 : 0;
28953 switch (AT_class (av))
28955 case dw_val_class_loc_list:
28956 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28957 if (d->expr && non_dwarf_expression (d->expr))
28958 non_dwarf_expr = true;
28959 break;
28960 case dw_val_class_loc:
28961 lv = AT_loc (av);
28962 if (lv == NULL)
28963 return dwarf_strict ? -1 : 0;
28964 if (non_dwarf_expression (lv))
28965 non_dwarf_expr = true;
28966 break;
28967 default:
28968 return dwarf_strict ? -1 : 0;
28971 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28972 into DW_OP_call4 or DW_OP_GNU_variable_value into
28973 DW_OP_call4 DW_OP_deref, do so. */
28974 if (!non_dwarf_expr
28975 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28977 l->dw_loc_opc = DW_OP_call4;
28978 if (l->dw_loc_next)
28979 l->dw_loc_next = NULL;
28980 else
28981 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28982 return 0;
28985 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28986 copy over the DW_AT_location attribute from die to a. */
28987 if (l->dw_loc_next != NULL)
28989 a->dw_attr_val = av->dw_attr_val;
28990 return 1;
28993 dw_loc_list_ref list, *p;
28994 switch (AT_class (av))
28996 case dw_val_class_loc_list:
28997 p = &list;
28998 list = NULL;
28999 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29001 lv = copy_deref_exprloc (d->expr);
29002 if (lv)
29004 *p = new_loc_list (lv, d->begin, d->end, d->section);
29005 p = &(*p)->dw_loc_next;
29007 else if (!dwarf_strict && d->expr)
29008 return 0;
29010 if (list == NULL)
29011 return dwarf_strict ? -1 : 0;
29012 a->dw_attr_val.val_class = dw_val_class_loc_list;
29013 gen_llsym (list);
29014 *AT_loc_list_ptr (a) = list;
29015 return 1;
29016 case dw_val_class_loc:
29017 lv = copy_deref_exprloc (AT_loc (av));
29018 if (lv == NULL)
29019 return dwarf_strict ? -1 : 0;
29020 a->dw_attr_val.v.val_loc = lv;
29021 return 1;
29022 default:
29023 gcc_unreachable ();
29027 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
29028 an address in .rodata section if the string literal is emitted there,
29029 or remove the containing location list or replace DW_AT_const_value
29030 with DW_AT_location and empty location expression, if it isn't found
29031 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
29032 to something that has been emitted in the current CU. */
29034 static void
29035 resolve_addr (dw_die_ref die)
29037 dw_die_ref c;
29038 dw_attr_node *a;
29039 dw_loc_list_ref *curr, *start, loc;
29040 unsigned ix;
29041 bool remove_AT_byte_size = false;
29043 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29044 switch (AT_class (a))
29046 case dw_val_class_loc_list:
29047 start = curr = AT_loc_list_ptr (a);
29048 loc = *curr;
29049 gcc_assert (loc);
29050 /* The same list can be referenced more than once. See if we have
29051 already recorded the result from a previous pass. */
29052 if (loc->replaced)
29053 *curr = loc->dw_loc_next;
29054 else if (!loc->resolved_addr)
29056 /* As things stand, we do not expect or allow one die to
29057 reference a suffix of another die's location list chain.
29058 References must be identical or completely separate.
29059 There is therefore no need to cache the result of this
29060 pass on any list other than the first; doing so
29061 would lead to unnecessary writes. */
29062 while (*curr)
29064 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29065 if (!resolve_addr_in_expr (a, (*curr)->expr))
29067 dw_loc_list_ref next = (*curr)->dw_loc_next;
29068 dw_loc_descr_ref l = (*curr)->expr;
29070 if (next && (*curr)->ll_symbol)
29072 gcc_assert (!next->ll_symbol);
29073 next->ll_symbol = (*curr)->ll_symbol;
29075 if (dwarf_split_debug_info)
29076 remove_loc_list_addr_table_entries (l);
29077 *curr = next;
29079 else
29081 mark_base_types ((*curr)->expr);
29082 curr = &(*curr)->dw_loc_next;
29085 if (loc == *start)
29086 loc->resolved_addr = 1;
29087 else
29089 loc->replaced = 1;
29090 loc->dw_loc_next = *start;
29093 if (!*start)
29095 remove_AT (die, a->dw_attr);
29096 ix--;
29098 break;
29099 case dw_val_class_loc:
29101 dw_loc_descr_ref l = AT_loc (a);
29102 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29103 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29104 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29105 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29106 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29107 with DW_FORM_ref referencing the same DIE as
29108 DW_OP_GNU_variable_value used to reference. */
29109 if (a->dw_attr == DW_AT_string_length
29110 && l
29111 && l->dw_loc_opc == DW_OP_GNU_variable_value
29112 && (l->dw_loc_next == NULL
29113 || (l->dw_loc_next->dw_loc_next == NULL
29114 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29116 switch (optimize_string_length (a))
29118 case -1:
29119 remove_AT (die, a->dw_attr);
29120 ix--;
29121 /* If we drop DW_AT_string_length, we need to drop also
29122 DW_AT_{string_length_,}byte_size. */
29123 remove_AT_byte_size = true;
29124 continue;
29125 default:
29126 break;
29127 case 1:
29128 /* Even if we keep the optimized DW_AT_string_length,
29129 it might have changed AT_class, so process it again. */
29130 ix--;
29131 continue;
29134 /* For -gdwarf-2 don't attempt to optimize
29135 DW_AT_data_member_location containing
29136 DW_OP_plus_uconst - older consumers might
29137 rely on it being that op instead of a more complex,
29138 but shorter, location description. */
29139 if ((dwarf_version > 2
29140 || a->dw_attr != DW_AT_data_member_location
29141 || l == NULL
29142 || l->dw_loc_opc != DW_OP_plus_uconst
29143 || l->dw_loc_next != NULL)
29144 && !resolve_addr_in_expr (a, l))
29146 if (dwarf_split_debug_info)
29147 remove_loc_list_addr_table_entries (l);
29148 if (l != NULL
29149 && l->dw_loc_next == NULL
29150 && l->dw_loc_opc == DW_OP_addr
29151 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29152 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29153 && a->dw_attr == DW_AT_location)
29155 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29156 remove_AT (die, a->dw_attr);
29157 ix--;
29158 optimize_location_into_implicit_ptr (die, decl);
29159 break;
29161 if (a->dw_attr == DW_AT_string_length)
29162 /* If we drop DW_AT_string_length, we need to drop also
29163 DW_AT_{string_length_,}byte_size. */
29164 remove_AT_byte_size = true;
29165 remove_AT (die, a->dw_attr);
29166 ix--;
29168 else
29169 mark_base_types (l);
29171 break;
29172 case dw_val_class_addr:
29173 if (a->dw_attr == DW_AT_const_value
29174 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29176 if (AT_index (a) != NOT_INDEXED)
29177 remove_addr_table_entry (a->dw_attr_val.val_entry);
29178 remove_AT (die, a->dw_attr);
29179 ix--;
29181 if ((die->die_tag == DW_TAG_call_site
29182 && a->dw_attr == DW_AT_call_origin)
29183 || (die->die_tag == DW_TAG_GNU_call_site
29184 && a->dw_attr == DW_AT_abstract_origin))
29186 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29187 dw_die_ref tdie = lookup_decl_die (tdecl);
29188 dw_die_ref cdie;
29189 if (tdie == NULL
29190 && DECL_EXTERNAL (tdecl)
29191 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29192 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29194 dw_die_ref pdie = cdie;
29195 /* Make sure we don't add these DIEs into type units.
29196 We could emit skeleton DIEs for context (namespaces,
29197 outer structs/classes) and a skeleton DIE for the
29198 innermost context with DW_AT_signature pointing to the
29199 type unit. See PR78835. */
29200 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29201 pdie = pdie->die_parent;
29202 if (pdie == NULL)
29204 /* Creating a full DIE for tdecl is overly expensive and
29205 at this point even wrong when in the LTO phase
29206 as it can end up generating new type DIEs we didn't
29207 output and thus optimize_external_refs will crash. */
29208 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29209 add_AT_flag (tdie, DW_AT_external, 1);
29210 add_AT_flag (tdie, DW_AT_declaration, 1);
29211 add_linkage_attr (tdie, tdecl);
29212 add_name_and_src_coords_attributes (tdie, tdecl, true);
29213 equate_decl_number_to_die (tdecl, tdie);
29216 if (tdie)
29218 a->dw_attr_val.val_class = dw_val_class_die_ref;
29219 a->dw_attr_val.v.val_die_ref.die = tdie;
29220 a->dw_attr_val.v.val_die_ref.external = 0;
29222 else
29224 if (AT_index (a) != NOT_INDEXED)
29225 remove_addr_table_entry (a->dw_attr_val.val_entry);
29226 remove_AT (die, a->dw_attr);
29227 ix--;
29230 break;
29231 default:
29232 break;
29235 if (remove_AT_byte_size)
29236 remove_AT (die, dwarf_version >= 5
29237 ? DW_AT_string_length_byte_size
29238 : DW_AT_byte_size);
29240 FOR_EACH_CHILD (die, c, resolve_addr (c));
29243 /* Helper routines for optimize_location_lists.
29244 This pass tries to share identical local lists in .debug_loc
29245 section. */
29247 /* Iteratively hash operands of LOC opcode into HSTATE. */
29249 static void
29250 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29252 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29253 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29255 switch (loc->dw_loc_opc)
29257 case DW_OP_const4u:
29258 case DW_OP_const8u:
29259 if (loc->dtprel)
29260 goto hash_addr;
29261 /* FALLTHRU */
29262 case DW_OP_const1u:
29263 case DW_OP_const1s:
29264 case DW_OP_const2u:
29265 case DW_OP_const2s:
29266 case DW_OP_const4s:
29267 case DW_OP_const8s:
29268 case DW_OP_constu:
29269 case DW_OP_consts:
29270 case DW_OP_pick:
29271 case DW_OP_plus_uconst:
29272 case DW_OP_breg0:
29273 case DW_OP_breg1:
29274 case DW_OP_breg2:
29275 case DW_OP_breg3:
29276 case DW_OP_breg4:
29277 case DW_OP_breg5:
29278 case DW_OP_breg6:
29279 case DW_OP_breg7:
29280 case DW_OP_breg8:
29281 case DW_OP_breg9:
29282 case DW_OP_breg10:
29283 case DW_OP_breg11:
29284 case DW_OP_breg12:
29285 case DW_OP_breg13:
29286 case DW_OP_breg14:
29287 case DW_OP_breg15:
29288 case DW_OP_breg16:
29289 case DW_OP_breg17:
29290 case DW_OP_breg18:
29291 case DW_OP_breg19:
29292 case DW_OP_breg20:
29293 case DW_OP_breg21:
29294 case DW_OP_breg22:
29295 case DW_OP_breg23:
29296 case DW_OP_breg24:
29297 case DW_OP_breg25:
29298 case DW_OP_breg26:
29299 case DW_OP_breg27:
29300 case DW_OP_breg28:
29301 case DW_OP_breg29:
29302 case DW_OP_breg30:
29303 case DW_OP_breg31:
29304 case DW_OP_regx:
29305 case DW_OP_fbreg:
29306 case DW_OP_piece:
29307 case DW_OP_deref_size:
29308 case DW_OP_xderef_size:
29309 hstate.add_object (val1->v.val_int);
29310 break;
29311 case DW_OP_skip:
29312 case DW_OP_bra:
29314 int offset;
29316 gcc_assert (val1->val_class == dw_val_class_loc);
29317 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29318 hstate.add_object (offset);
29320 break;
29321 case DW_OP_implicit_value:
29322 hstate.add_object (val1->v.val_unsigned);
29323 switch (val2->val_class)
29325 case dw_val_class_const:
29326 hstate.add_object (val2->v.val_int);
29327 break;
29328 case dw_val_class_vec:
29330 unsigned int elt_size = val2->v.val_vec.elt_size;
29331 unsigned int len = val2->v.val_vec.length;
29333 hstate.add_int (elt_size);
29334 hstate.add_int (len);
29335 hstate.add (val2->v.val_vec.array, len * elt_size);
29337 break;
29338 case dw_val_class_const_double:
29339 hstate.add_object (val2->v.val_double.low);
29340 hstate.add_object (val2->v.val_double.high);
29341 break;
29342 case dw_val_class_wide_int:
29343 hstate.add (val2->v.val_wide->get_val (),
29344 get_full_len (*val2->v.val_wide)
29345 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29346 break;
29347 case dw_val_class_addr:
29348 inchash::add_rtx (val2->v.val_addr, hstate);
29349 break;
29350 default:
29351 gcc_unreachable ();
29353 break;
29354 case DW_OP_bregx:
29355 case DW_OP_bit_piece:
29356 hstate.add_object (val1->v.val_int);
29357 hstate.add_object (val2->v.val_int);
29358 break;
29359 case DW_OP_addr:
29360 hash_addr:
29361 if (loc->dtprel)
29363 unsigned char dtprel = 0xd1;
29364 hstate.add_object (dtprel);
29366 inchash::add_rtx (val1->v.val_addr, hstate);
29367 break;
29368 case DW_OP_GNU_addr_index:
29369 case DW_OP_GNU_const_index:
29371 if (loc->dtprel)
29373 unsigned char dtprel = 0xd1;
29374 hstate.add_object (dtprel);
29376 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29378 break;
29379 case DW_OP_implicit_pointer:
29380 case DW_OP_GNU_implicit_pointer:
29381 hstate.add_int (val2->v.val_int);
29382 break;
29383 case DW_OP_entry_value:
29384 case DW_OP_GNU_entry_value:
29385 hstate.add_object (val1->v.val_loc);
29386 break;
29387 case DW_OP_regval_type:
29388 case DW_OP_deref_type:
29389 case DW_OP_GNU_regval_type:
29390 case DW_OP_GNU_deref_type:
29392 unsigned int byte_size
29393 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29394 unsigned int encoding
29395 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29396 hstate.add_object (val1->v.val_int);
29397 hstate.add_object (byte_size);
29398 hstate.add_object (encoding);
29400 break;
29401 case DW_OP_convert:
29402 case DW_OP_reinterpret:
29403 case DW_OP_GNU_convert:
29404 case DW_OP_GNU_reinterpret:
29405 if (val1->val_class == dw_val_class_unsigned_const)
29407 hstate.add_object (val1->v.val_unsigned);
29408 break;
29410 /* FALLTHRU */
29411 case DW_OP_const_type:
29412 case DW_OP_GNU_const_type:
29414 unsigned int byte_size
29415 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29416 unsigned int encoding
29417 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29418 hstate.add_object (byte_size);
29419 hstate.add_object (encoding);
29420 if (loc->dw_loc_opc != DW_OP_const_type
29421 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29422 break;
29423 hstate.add_object (val2->val_class);
29424 switch (val2->val_class)
29426 case dw_val_class_const:
29427 hstate.add_object (val2->v.val_int);
29428 break;
29429 case dw_val_class_vec:
29431 unsigned int elt_size = val2->v.val_vec.elt_size;
29432 unsigned int len = val2->v.val_vec.length;
29434 hstate.add_object (elt_size);
29435 hstate.add_object (len);
29436 hstate.add (val2->v.val_vec.array, len * elt_size);
29438 break;
29439 case dw_val_class_const_double:
29440 hstate.add_object (val2->v.val_double.low);
29441 hstate.add_object (val2->v.val_double.high);
29442 break;
29443 case dw_val_class_wide_int:
29444 hstate.add (val2->v.val_wide->get_val (),
29445 get_full_len (*val2->v.val_wide)
29446 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29447 break;
29448 default:
29449 gcc_unreachable ();
29452 break;
29454 default:
29455 /* Other codes have no operands. */
29456 break;
29460 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29462 static inline void
29463 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29465 dw_loc_descr_ref l;
29466 bool sizes_computed = false;
29467 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29468 size_of_locs (loc);
29470 for (l = loc; l != NULL; l = l->dw_loc_next)
29472 enum dwarf_location_atom opc = l->dw_loc_opc;
29473 hstate.add_object (opc);
29474 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29476 size_of_locs (loc);
29477 sizes_computed = true;
29479 hash_loc_operands (l, hstate);
29483 /* Compute hash of the whole location list LIST_HEAD. */
29485 static inline void
29486 hash_loc_list (dw_loc_list_ref list_head)
29488 dw_loc_list_ref curr = list_head;
29489 inchash::hash hstate;
29491 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29493 hstate.add (curr->begin, strlen (curr->begin) + 1);
29494 hstate.add (curr->end, strlen (curr->end) + 1);
29495 if (curr->section)
29496 hstate.add (curr->section, strlen (curr->section) + 1);
29497 hash_locs (curr->expr, hstate);
29499 list_head->hash = hstate.end ();
29502 /* Return true if X and Y opcodes have the same operands. */
29504 static inline bool
29505 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29507 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29508 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29509 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29510 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29512 switch (x->dw_loc_opc)
29514 case DW_OP_const4u:
29515 case DW_OP_const8u:
29516 if (x->dtprel)
29517 goto hash_addr;
29518 /* FALLTHRU */
29519 case DW_OP_const1u:
29520 case DW_OP_const1s:
29521 case DW_OP_const2u:
29522 case DW_OP_const2s:
29523 case DW_OP_const4s:
29524 case DW_OP_const8s:
29525 case DW_OP_constu:
29526 case DW_OP_consts:
29527 case DW_OP_pick:
29528 case DW_OP_plus_uconst:
29529 case DW_OP_breg0:
29530 case DW_OP_breg1:
29531 case DW_OP_breg2:
29532 case DW_OP_breg3:
29533 case DW_OP_breg4:
29534 case DW_OP_breg5:
29535 case DW_OP_breg6:
29536 case DW_OP_breg7:
29537 case DW_OP_breg8:
29538 case DW_OP_breg9:
29539 case DW_OP_breg10:
29540 case DW_OP_breg11:
29541 case DW_OP_breg12:
29542 case DW_OP_breg13:
29543 case DW_OP_breg14:
29544 case DW_OP_breg15:
29545 case DW_OP_breg16:
29546 case DW_OP_breg17:
29547 case DW_OP_breg18:
29548 case DW_OP_breg19:
29549 case DW_OP_breg20:
29550 case DW_OP_breg21:
29551 case DW_OP_breg22:
29552 case DW_OP_breg23:
29553 case DW_OP_breg24:
29554 case DW_OP_breg25:
29555 case DW_OP_breg26:
29556 case DW_OP_breg27:
29557 case DW_OP_breg28:
29558 case DW_OP_breg29:
29559 case DW_OP_breg30:
29560 case DW_OP_breg31:
29561 case DW_OP_regx:
29562 case DW_OP_fbreg:
29563 case DW_OP_piece:
29564 case DW_OP_deref_size:
29565 case DW_OP_xderef_size:
29566 return valx1->v.val_int == valy1->v.val_int;
29567 case DW_OP_skip:
29568 case DW_OP_bra:
29569 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29570 can cause irrelevant differences in dw_loc_addr. */
29571 gcc_assert (valx1->val_class == dw_val_class_loc
29572 && valy1->val_class == dw_val_class_loc
29573 && (dwarf_split_debug_info
29574 || x->dw_loc_addr == y->dw_loc_addr));
29575 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29576 case DW_OP_implicit_value:
29577 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29578 || valx2->val_class != valy2->val_class)
29579 return false;
29580 switch (valx2->val_class)
29582 case dw_val_class_const:
29583 return valx2->v.val_int == valy2->v.val_int;
29584 case dw_val_class_vec:
29585 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29586 && valx2->v.val_vec.length == valy2->v.val_vec.length
29587 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29588 valx2->v.val_vec.elt_size
29589 * valx2->v.val_vec.length) == 0;
29590 case dw_val_class_const_double:
29591 return valx2->v.val_double.low == valy2->v.val_double.low
29592 && valx2->v.val_double.high == valy2->v.val_double.high;
29593 case dw_val_class_wide_int:
29594 return *valx2->v.val_wide == *valy2->v.val_wide;
29595 case dw_val_class_addr:
29596 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29597 default:
29598 gcc_unreachable ();
29600 case DW_OP_bregx:
29601 case DW_OP_bit_piece:
29602 return valx1->v.val_int == valy1->v.val_int
29603 && valx2->v.val_int == valy2->v.val_int;
29604 case DW_OP_addr:
29605 hash_addr:
29606 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29607 case DW_OP_GNU_addr_index:
29608 case DW_OP_GNU_const_index:
29610 rtx ax1 = valx1->val_entry->addr.rtl;
29611 rtx ay1 = valy1->val_entry->addr.rtl;
29612 return rtx_equal_p (ax1, ay1);
29614 case DW_OP_implicit_pointer:
29615 case DW_OP_GNU_implicit_pointer:
29616 return valx1->val_class == dw_val_class_die_ref
29617 && valx1->val_class == valy1->val_class
29618 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29619 && valx2->v.val_int == valy2->v.val_int;
29620 case DW_OP_entry_value:
29621 case DW_OP_GNU_entry_value:
29622 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29623 case DW_OP_const_type:
29624 case DW_OP_GNU_const_type:
29625 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29626 || valx2->val_class != valy2->val_class)
29627 return false;
29628 switch (valx2->val_class)
29630 case dw_val_class_const:
29631 return valx2->v.val_int == valy2->v.val_int;
29632 case dw_val_class_vec:
29633 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29634 && valx2->v.val_vec.length == valy2->v.val_vec.length
29635 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29636 valx2->v.val_vec.elt_size
29637 * valx2->v.val_vec.length) == 0;
29638 case dw_val_class_const_double:
29639 return valx2->v.val_double.low == valy2->v.val_double.low
29640 && valx2->v.val_double.high == valy2->v.val_double.high;
29641 case dw_val_class_wide_int:
29642 return *valx2->v.val_wide == *valy2->v.val_wide;
29643 default:
29644 gcc_unreachable ();
29646 case DW_OP_regval_type:
29647 case DW_OP_deref_type:
29648 case DW_OP_GNU_regval_type:
29649 case DW_OP_GNU_deref_type:
29650 return valx1->v.val_int == valy1->v.val_int
29651 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29652 case DW_OP_convert:
29653 case DW_OP_reinterpret:
29654 case DW_OP_GNU_convert:
29655 case DW_OP_GNU_reinterpret:
29656 if (valx1->val_class != valy1->val_class)
29657 return false;
29658 if (valx1->val_class == dw_val_class_unsigned_const)
29659 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29660 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29661 case DW_OP_GNU_parameter_ref:
29662 return valx1->val_class == dw_val_class_die_ref
29663 && valx1->val_class == valy1->val_class
29664 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29665 default:
29666 /* Other codes have no operands. */
29667 return true;
29671 /* Return true if DWARF location expressions X and Y are the same. */
29673 static inline bool
29674 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29676 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29677 if (x->dw_loc_opc != y->dw_loc_opc
29678 || x->dtprel != y->dtprel
29679 || !compare_loc_operands (x, y))
29680 break;
29681 return x == NULL && y == NULL;
29684 /* Hashtable helpers. */
29686 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29688 static inline hashval_t hash (const dw_loc_list_struct *);
29689 static inline bool equal (const dw_loc_list_struct *,
29690 const dw_loc_list_struct *);
29693 /* Return precomputed hash of location list X. */
29695 inline hashval_t
29696 loc_list_hasher::hash (const dw_loc_list_struct *x)
29698 return x->hash;
29701 /* Return true if location lists A and B are the same. */
29703 inline bool
29704 loc_list_hasher::equal (const dw_loc_list_struct *a,
29705 const dw_loc_list_struct *b)
29707 if (a == b)
29708 return 1;
29709 if (a->hash != b->hash)
29710 return 0;
29711 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29712 if (strcmp (a->begin, b->begin) != 0
29713 || strcmp (a->end, b->end) != 0
29714 || (a->section == NULL) != (b->section == NULL)
29715 || (a->section && strcmp (a->section, b->section) != 0)
29716 || !compare_locs (a->expr, b->expr))
29717 break;
29718 return a == NULL && b == NULL;
29721 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29724 /* Recursively optimize location lists referenced from DIE
29725 children and share them whenever possible. */
29727 static void
29728 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29730 dw_die_ref c;
29731 dw_attr_node *a;
29732 unsigned ix;
29733 dw_loc_list_struct **slot;
29735 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29736 if (AT_class (a) == dw_val_class_loc_list)
29738 dw_loc_list_ref list = AT_loc_list (a);
29739 /* TODO: perform some optimizations here, before hashing
29740 it and storing into the hash table. */
29741 hash_loc_list (list);
29742 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29743 if (*slot == NULL)
29744 *slot = list;
29745 else
29746 a->dw_attr_val.v.val_loc_list = *slot;
29749 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29753 /* Recursively assign each location list a unique index into the debug_addr
29754 section. */
29756 static void
29757 index_location_lists (dw_die_ref die)
29759 dw_die_ref c;
29760 dw_attr_node *a;
29761 unsigned ix;
29763 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29764 if (AT_class (a) == dw_val_class_loc_list)
29766 dw_loc_list_ref list = AT_loc_list (a);
29767 dw_loc_list_ref curr;
29768 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29770 /* Don't index an entry that has already been indexed
29771 or won't be output. */
29772 if (curr->begin_entry != NULL
29773 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29774 continue;
29776 curr->begin_entry
29777 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29781 FOR_EACH_CHILD (die, c, index_location_lists (c));
29784 /* Optimize location lists referenced from DIE
29785 children and share them whenever possible. */
29787 static void
29788 optimize_location_lists (dw_die_ref die)
29790 loc_list_hash_type htab (500);
29791 optimize_location_lists_1 (die, &htab);
29794 /* Traverse the limbo die list, and add parent/child links. The only
29795 dies without parents that should be here are concrete instances of
29796 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29797 For concrete instances, we can get the parent die from the abstract
29798 instance. */
29800 static void
29801 flush_limbo_die_list (void)
29803 limbo_die_node *node;
29805 /* get_context_die calls force_decl_die, which can put new DIEs on the
29806 limbo list in LTO mode when nested functions are put in a different
29807 partition than that of their parent function. */
29808 while ((node = limbo_die_list))
29810 dw_die_ref die = node->die;
29811 limbo_die_list = node->next;
29813 if (die->die_parent == NULL)
29815 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29817 if (origin && origin->die_parent)
29818 add_child_die (origin->die_parent, die);
29819 else if (is_cu_die (die))
29821 else if (seen_error ())
29822 /* It's OK to be confused by errors in the input. */
29823 add_child_die (comp_unit_die (), die);
29824 else
29826 /* In certain situations, the lexical block containing a
29827 nested function can be optimized away, which results
29828 in the nested function die being orphaned. Likewise
29829 with the return type of that nested function. Force
29830 this to be a child of the containing function.
29832 It may happen that even the containing function got fully
29833 inlined and optimized out. In that case we are lost and
29834 assign the empty child. This should not be big issue as
29835 the function is likely unreachable too. */
29836 gcc_assert (node->created_for);
29838 if (DECL_P (node->created_for))
29839 origin = get_context_die (DECL_CONTEXT (node->created_for));
29840 else if (TYPE_P (node->created_for))
29841 origin = scope_die_for (node->created_for, comp_unit_die ());
29842 else
29843 origin = comp_unit_die ();
29845 add_child_die (origin, die);
29851 /* Reset DIEs so we can output them again. */
29853 static void
29854 reset_dies (dw_die_ref die)
29856 dw_die_ref c;
29858 /* Remove stuff we re-generate. */
29859 die->die_mark = 0;
29860 die->die_offset = 0;
29861 die->die_abbrev = 0;
29862 remove_AT (die, DW_AT_sibling);
29864 FOR_EACH_CHILD (die, c, reset_dies (c));
29867 /* Output stuff that dwarf requires at the end of every file,
29868 and generate the DWARF-2 debugging info. */
29870 static void
29871 dwarf2out_finish (const char *)
29873 comdat_type_node *ctnode;
29874 dw_die_ref main_comp_unit_die;
29875 unsigned char checksum[16];
29876 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29878 /* Flush out any latecomers to the limbo party. */
29879 flush_limbo_die_list ();
29881 if (flag_checking)
29883 verify_die (comp_unit_die ());
29884 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29885 verify_die (node->die);
29888 /* We shouldn't have any symbols with delayed asm names for
29889 DIEs generated after early finish. */
29890 gcc_assert (deferred_asm_name == NULL);
29892 gen_remaining_tmpl_value_param_die_attribute ();
29894 if (flag_generate_lto || flag_generate_offload)
29896 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29898 /* Prune stuff so that dwarf2out_finish runs successfully
29899 for the fat part of the object. */
29900 reset_dies (comp_unit_die ());
29901 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29902 reset_dies (node->die);
29904 hash_table<comdat_type_hasher> comdat_type_table (100);
29905 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29907 comdat_type_node **slot
29908 = comdat_type_table.find_slot (ctnode, INSERT);
29910 /* Don't reset types twice. */
29911 if (*slot != HTAB_EMPTY_ENTRY)
29912 continue;
29914 /* Add a pointer to the line table for the main compilation unit
29915 so that the debugger can make sense of DW_AT_decl_file
29916 attributes. */
29917 if (debug_info_level >= DINFO_LEVEL_TERSE)
29918 reset_dies (ctnode->root_die);
29920 *slot = ctnode;
29923 /* Reset die CU symbol so we don't output it twice. */
29924 comp_unit_die ()->die_id.die_symbol = NULL;
29926 /* Remove DW_AT_macro from the early output. */
29927 if (have_macinfo)
29928 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29930 /* Remove indirect string decisions. */
29931 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29934 #if ENABLE_ASSERT_CHECKING
29936 dw_die_ref die = comp_unit_die (), c;
29937 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29939 #endif
29940 resolve_addr (comp_unit_die ());
29941 move_marked_base_types ();
29943 /* Initialize sections and labels used for actual assembler output. */
29944 unsigned generation = init_sections_and_labels (false);
29946 /* Traverse the DIE's and add sibling attributes to those DIE's that
29947 have children. */
29948 add_sibling_attributes (comp_unit_die ());
29949 limbo_die_node *node;
29950 for (node = cu_die_list; node; node = node->next)
29951 add_sibling_attributes (node->die);
29952 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29953 add_sibling_attributes (ctnode->root_die);
29955 /* When splitting DWARF info, we put some attributes in the
29956 skeleton compile_unit DIE that remains in the .o, while
29957 most attributes go in the DWO compile_unit_die. */
29958 if (dwarf_split_debug_info)
29960 limbo_die_node *cu;
29961 main_comp_unit_die = gen_compile_unit_die (NULL);
29962 if (dwarf_version >= 5)
29963 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29964 cu = limbo_die_list;
29965 gcc_assert (cu->die == main_comp_unit_die);
29966 limbo_die_list = limbo_die_list->next;
29967 cu->next = cu_die_list;
29968 cu_die_list = cu;
29970 else
29971 main_comp_unit_die = comp_unit_die ();
29973 /* Output a terminator label for the .text section. */
29974 switch_to_section (text_section);
29975 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29976 if (cold_text_section)
29978 switch_to_section (cold_text_section);
29979 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29982 /* We can only use the low/high_pc attributes if all of the code was
29983 in .text. */
29984 if (!have_multiple_function_sections
29985 || (dwarf_version < 3 && dwarf_strict))
29987 /* Don't add if the CU has no associated code. */
29988 if (text_section_used)
29989 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29990 text_end_label, true);
29992 else
29994 unsigned fde_idx;
29995 dw_fde_ref fde;
29996 bool range_list_added = false;
29998 if (text_section_used)
29999 add_ranges_by_labels (main_comp_unit_die, text_section_label,
30000 text_end_label, &range_list_added, true);
30001 if (cold_text_section_used)
30002 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
30003 cold_end_label, &range_list_added, true);
30005 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
30007 if (DECL_IGNORED_P (fde->decl))
30008 continue;
30009 if (!fde->in_std_section)
30010 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
30011 fde->dw_fde_end, &range_list_added,
30012 true);
30013 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
30014 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
30015 fde->dw_fde_second_end, &range_list_added,
30016 true);
30019 if (range_list_added)
30021 /* We need to give .debug_loc and .debug_ranges an appropriate
30022 "base address". Use zero so that these addresses become
30023 absolute. Historically, we've emitted the unexpected
30024 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
30025 Emit both to give time for other tools to adapt. */
30026 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
30027 if (! dwarf_strict && dwarf_version < 4)
30028 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
30030 add_ranges (NULL);
30034 /* AIX Assembler inserts the length, so adjust the reference to match the
30035 offset expected by debuggers. */
30036 strcpy (dl_section_ref, debug_line_section_label);
30037 if (XCOFF_DEBUGGING_INFO)
30038 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
30040 if (debug_info_level >= DINFO_LEVEL_TERSE)
30041 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
30042 dl_section_ref);
30044 if (have_macinfo)
30045 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30046 macinfo_section_label);
30048 if (dwarf_split_debug_info)
30050 if (have_location_lists)
30052 if (dwarf_version >= 5)
30053 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30054 loc_section_label);
30055 /* optimize_location_lists calculates the size of the lists,
30056 so index them first, and assign indices to the entries.
30057 Although optimize_location_lists will remove entries from
30058 the table, it only does so for duplicates, and therefore
30059 only reduces ref_counts to 1. */
30060 index_location_lists (comp_unit_die ());
30063 if (addr_index_table != NULL)
30065 unsigned int index = 0;
30066 addr_index_table
30067 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30068 (&index);
30072 loc_list_idx = 0;
30073 if (have_location_lists)
30075 optimize_location_lists (comp_unit_die ());
30076 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30077 if (dwarf_version >= 5 && dwarf_split_debug_info)
30078 assign_location_list_indexes (comp_unit_die ());
30081 save_macinfo_strings ();
30083 if (dwarf_split_debug_info)
30085 unsigned int index = 0;
30087 /* Add attributes common to skeleton compile_units and
30088 type_units. Because these attributes include strings, it
30089 must be done before freezing the string table. Top-level
30090 skeleton die attrs are added when the skeleton type unit is
30091 created, so ensure it is created by this point. */
30092 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30093 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30096 /* Output all of the compilation units. We put the main one last so that
30097 the offsets are available to output_pubnames. */
30098 for (node = cu_die_list; node; node = node->next)
30099 output_comp_unit (node->die, 0, NULL);
30101 hash_table<comdat_type_hasher> comdat_type_table (100);
30102 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30104 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30106 /* Don't output duplicate types. */
30107 if (*slot != HTAB_EMPTY_ENTRY)
30108 continue;
30110 /* Add a pointer to the line table for the main compilation unit
30111 so that the debugger can make sense of DW_AT_decl_file
30112 attributes. */
30113 if (debug_info_level >= DINFO_LEVEL_TERSE)
30114 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30115 (!dwarf_split_debug_info
30116 ? dl_section_ref
30117 : debug_skeleton_line_section_label));
30119 output_comdat_type_unit (ctnode);
30120 *slot = ctnode;
30123 if (dwarf_split_debug_info)
30125 int mark;
30126 struct md5_ctx ctx;
30128 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30129 index_rnglists ();
30131 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30132 md5_init_ctx (&ctx);
30133 mark = 0;
30134 die_checksum (comp_unit_die (), &ctx, &mark);
30135 unmark_all_dies (comp_unit_die ());
30136 md5_finish_ctx (&ctx, checksum);
30138 if (dwarf_version < 5)
30140 /* Use the first 8 bytes of the checksum as the dwo_id,
30141 and add it to both comp-unit DIEs. */
30142 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30143 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30146 /* Add the base offset of the ranges table to the skeleton
30147 comp-unit DIE. */
30148 if (!vec_safe_is_empty (ranges_table))
30150 if (dwarf_version >= 5)
30151 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30152 ranges_base_label);
30153 else
30154 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30155 ranges_section_label);
30158 switch_to_section (debug_addr_section);
30159 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30160 output_addr_table ();
30163 /* Output the main compilation unit if non-empty or if .debug_macinfo
30164 or .debug_macro will be emitted. */
30165 output_comp_unit (comp_unit_die (), have_macinfo,
30166 dwarf_split_debug_info ? checksum : NULL);
30168 if (dwarf_split_debug_info && info_section_emitted)
30169 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30171 /* Output the abbreviation table. */
30172 if (vec_safe_length (abbrev_die_table) != 1)
30174 switch_to_section (debug_abbrev_section);
30175 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30176 output_abbrev_section ();
30179 /* Output location list section if necessary. */
30180 if (have_location_lists)
30182 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30183 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30184 /* Output the location lists info. */
30185 switch_to_section (debug_loc_section);
30186 if (dwarf_version >= 5)
30188 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30189 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30190 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30191 dw2_asm_output_data (4, 0xffffffff,
30192 "Initial length escape value indicating "
30193 "64-bit DWARF extension");
30194 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30195 "Length of Location Lists");
30196 ASM_OUTPUT_LABEL (asm_out_file, l1);
30197 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30198 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30199 dw2_asm_output_data (1, 0, "Segment Size");
30200 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30201 "Offset Entry Count");
30203 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30204 if (dwarf_version >= 5 && dwarf_split_debug_info)
30206 unsigned int save_loc_list_idx = loc_list_idx;
30207 loc_list_idx = 0;
30208 output_loclists_offsets (comp_unit_die ());
30209 gcc_assert (save_loc_list_idx == loc_list_idx);
30211 output_location_lists (comp_unit_die ());
30212 if (dwarf_version >= 5)
30213 ASM_OUTPUT_LABEL (asm_out_file, l2);
30216 output_pubtables ();
30218 /* Output the address range information if a CU (.debug_info section)
30219 was emitted. We output an empty table even if we had no functions
30220 to put in it. This because the consumer has no way to tell the
30221 difference between an empty table that we omitted and failure to
30222 generate a table that would have contained data. */
30223 if (info_section_emitted)
30225 switch_to_section (debug_aranges_section);
30226 output_aranges ();
30229 /* Output ranges section if necessary. */
30230 if (!vec_safe_is_empty (ranges_table))
30232 if (dwarf_version >= 5)
30233 output_rnglists (generation);
30234 else
30235 output_ranges ();
30238 /* Have to end the macro section. */
30239 if (have_macinfo)
30241 switch_to_section (debug_macinfo_section);
30242 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30243 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30244 : debug_skeleton_line_section_label, false);
30245 dw2_asm_output_data (1, 0, "End compilation unit");
30248 /* Output the source line correspondence table. We must do this
30249 even if there is no line information. Otherwise, on an empty
30250 translation unit, we will generate a present, but empty,
30251 .debug_info section. IRIX 6.5 `nm' will then complain when
30252 examining the file. This is done late so that any filenames
30253 used by the debug_info section are marked as 'used'. */
30254 switch_to_section (debug_line_section);
30255 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30256 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30257 output_line_info (false);
30259 if (dwarf_split_debug_info && info_section_emitted)
30261 switch_to_section (debug_skeleton_line_section);
30262 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30263 output_line_info (true);
30266 /* If we emitted any indirect strings, output the string table too. */
30267 if (debug_str_hash || skeleton_debug_str_hash)
30268 output_indirect_strings ();
30269 if (debug_line_str_hash)
30271 switch_to_section (debug_line_str_section);
30272 const enum dwarf_form form = DW_FORM_line_strp;
30273 debug_line_str_hash->traverse<enum dwarf_form,
30274 output_indirect_string> (form);
30278 /* Returns a hash value for X (which really is a variable_value_struct). */
30280 inline hashval_t
30281 variable_value_hasher::hash (variable_value_struct *x)
30283 return (hashval_t) x->decl_id;
30286 /* Return nonzero if decl_id of variable_value_struct X is the same as
30287 UID of decl Y. */
30289 inline bool
30290 variable_value_hasher::equal (variable_value_struct *x, tree y)
30292 return x->decl_id == DECL_UID (y);
30295 /* Helper function for resolve_variable_value, handle
30296 DW_OP_GNU_variable_value in one location expression.
30297 Return true if exprloc has been changed into loclist. */
30299 static bool
30300 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30302 dw_loc_descr_ref next;
30303 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30305 next = loc->dw_loc_next;
30306 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30307 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30308 continue;
30310 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30311 if (DECL_CONTEXT (decl) != current_function_decl)
30312 continue;
30314 dw_die_ref ref = lookup_decl_die (decl);
30315 if (ref)
30317 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30318 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30319 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30320 continue;
30322 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30323 if (l == NULL)
30324 continue;
30325 if (l->dw_loc_next)
30327 if (AT_class (a) != dw_val_class_loc)
30328 continue;
30329 switch (a->dw_attr)
30331 /* Following attributes allow both exprloc and loclist
30332 classes, so we can change them into a loclist. */
30333 case DW_AT_location:
30334 case DW_AT_string_length:
30335 case DW_AT_return_addr:
30336 case DW_AT_data_member_location:
30337 case DW_AT_frame_base:
30338 case DW_AT_segment:
30339 case DW_AT_static_link:
30340 case DW_AT_use_location:
30341 case DW_AT_vtable_elem_location:
30342 if (prev)
30344 prev->dw_loc_next = NULL;
30345 prepend_loc_descr_to_each (l, AT_loc (a));
30347 if (next)
30348 add_loc_descr_to_each (l, next);
30349 a->dw_attr_val.val_class = dw_val_class_loc_list;
30350 a->dw_attr_val.val_entry = NULL;
30351 a->dw_attr_val.v.val_loc_list = l;
30352 have_location_lists = true;
30353 return true;
30354 /* Following attributes allow both exprloc and reference,
30355 so if the whole expression is DW_OP_GNU_variable_value alone
30356 we could transform it into reference. */
30357 case DW_AT_byte_size:
30358 case DW_AT_bit_size:
30359 case DW_AT_lower_bound:
30360 case DW_AT_upper_bound:
30361 case DW_AT_bit_stride:
30362 case DW_AT_count:
30363 case DW_AT_allocated:
30364 case DW_AT_associated:
30365 case DW_AT_byte_stride:
30366 if (prev == NULL && next == NULL)
30367 break;
30368 /* FALLTHRU */
30369 default:
30370 if (dwarf_strict)
30371 continue;
30372 break;
30374 /* Create DW_TAG_variable that we can refer to. */
30375 gen_decl_die (decl, NULL_TREE, NULL,
30376 lookup_decl_die (current_function_decl));
30377 ref = lookup_decl_die (decl);
30378 if (ref)
30380 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30381 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30382 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30384 continue;
30386 if (prev)
30388 prev->dw_loc_next = l->expr;
30389 add_loc_descr (&prev->dw_loc_next, next);
30390 free_loc_descr (loc, NULL);
30391 next = prev->dw_loc_next;
30393 else
30395 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30396 add_loc_descr (&loc, next);
30397 next = loc;
30399 loc = prev;
30401 return false;
30404 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30406 static void
30407 resolve_variable_value (dw_die_ref die)
30409 dw_attr_node *a;
30410 dw_loc_list_ref loc;
30411 unsigned ix;
30413 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30414 switch (AT_class (a))
30416 case dw_val_class_loc:
30417 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30418 break;
30419 /* FALLTHRU */
30420 case dw_val_class_loc_list:
30421 loc = AT_loc_list (a);
30422 gcc_assert (loc);
30423 for (; loc; loc = loc->dw_loc_next)
30424 resolve_variable_value_in_expr (a, loc->expr);
30425 break;
30426 default:
30427 break;
30431 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30432 temporaries in the current function. */
30434 static void
30435 resolve_variable_values (void)
30437 if (!variable_value_hash || !current_function_decl)
30438 return;
30440 struct variable_value_struct *node
30441 = variable_value_hash->find_with_hash (current_function_decl,
30442 DECL_UID (current_function_decl));
30444 if (node == NULL)
30445 return;
30447 unsigned int i;
30448 dw_die_ref die;
30449 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30450 resolve_variable_value (die);
30453 /* Helper function for note_variable_value, handle one location
30454 expression. */
30456 static void
30457 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30459 for (; loc; loc = loc->dw_loc_next)
30460 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30461 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30463 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30464 dw_die_ref ref = lookup_decl_die (decl);
30465 if (! ref && (flag_generate_lto || flag_generate_offload))
30467 /* ??? This is somewhat a hack because we do not create DIEs
30468 for variables not in BLOCK trees early but when generating
30469 early LTO output we need the dw_val_class_decl_ref to be
30470 fully resolved. For fat LTO objects we'd also like to
30471 undo this after LTO dwarf output. */
30472 gcc_assert (DECL_CONTEXT (decl));
30473 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30474 gcc_assert (ctx != NULL);
30475 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30476 ref = lookup_decl_die (decl);
30477 gcc_assert (ref != NULL);
30479 if (ref)
30481 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30482 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30483 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30484 continue;
30486 if (VAR_P (decl)
30487 && DECL_CONTEXT (decl)
30488 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30489 && lookup_decl_die (DECL_CONTEXT (decl)))
30491 if (!variable_value_hash)
30492 variable_value_hash
30493 = hash_table<variable_value_hasher>::create_ggc (10);
30495 tree fndecl = DECL_CONTEXT (decl);
30496 struct variable_value_struct *node;
30497 struct variable_value_struct **slot
30498 = variable_value_hash->find_slot_with_hash (fndecl,
30499 DECL_UID (fndecl),
30500 INSERT);
30501 if (*slot == NULL)
30503 node = ggc_cleared_alloc<variable_value_struct> ();
30504 node->decl_id = DECL_UID (fndecl);
30505 *slot = node;
30507 else
30508 node = *slot;
30510 vec_safe_push (node->dies, die);
30515 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30516 with dw_val_class_decl_ref operand. */
30518 static void
30519 note_variable_value (dw_die_ref die)
30521 dw_die_ref c;
30522 dw_attr_node *a;
30523 dw_loc_list_ref loc;
30524 unsigned ix;
30526 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30527 switch (AT_class (a))
30529 case dw_val_class_loc_list:
30530 loc = AT_loc_list (a);
30531 gcc_assert (loc);
30532 if (!loc->noted_variable_value)
30534 loc->noted_variable_value = 1;
30535 for (; loc; loc = loc->dw_loc_next)
30536 note_variable_value_in_expr (die, loc->expr);
30538 break;
30539 case dw_val_class_loc:
30540 note_variable_value_in_expr (die, AT_loc (a));
30541 break;
30542 default:
30543 break;
30546 /* Mark children. */
30547 FOR_EACH_CHILD (die, c, note_variable_value (c));
30550 /* Perform any cleanups needed after the early debug generation pass
30551 has run. */
30553 static void
30554 dwarf2out_early_finish (const char *filename)
30556 set_early_dwarf s;
30558 /* PCH might result in DW_AT_producer string being restored from the
30559 header compilation, so always fill it with empty string initially
30560 and overwrite only here. */
30561 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30562 producer_string = gen_producer_string ();
30563 producer->dw_attr_val.v.val_str->refcount--;
30564 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30566 /* Add the name for the main input file now. We delayed this from
30567 dwarf2out_init to avoid complications with PCH. */
30568 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30569 add_comp_dir_attribute (comp_unit_die ());
30571 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30572 DW_AT_comp_dir into .debug_line_str section. */
30573 if (!DWARF2_ASM_LINE_DEBUG_INFO
30574 && dwarf_version >= 5
30575 && DWARF5_USE_DEBUG_LINE_STR)
30577 for (int i = 0; i < 2; i++)
30579 dw_attr_node *a = get_AT (comp_unit_die (),
30580 i ? DW_AT_comp_dir : DW_AT_name);
30581 if (a == NULL
30582 || AT_class (a) != dw_val_class_str
30583 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30584 continue;
30586 if (! debug_line_str_hash)
30587 debug_line_str_hash
30588 = hash_table<indirect_string_hasher>::create_ggc (10);
30590 struct indirect_string_node *node
30591 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30592 set_indirect_string (node);
30593 node->form = DW_FORM_line_strp;
30594 a->dw_attr_val.v.val_str->refcount--;
30595 a->dw_attr_val.v.val_str = node;
30599 /* With LTO early dwarf was really finished at compile-time, so make
30600 sure to adjust the phase after annotating the LTRANS CU DIE. */
30601 if (in_lto_p)
30603 early_dwarf_finished = true;
30604 return;
30607 /* Walk through the list of incomplete types again, trying once more to
30608 emit full debugging info for them. */
30609 retry_incomplete_types ();
30611 /* The point here is to flush out the limbo list so that it is empty
30612 and we don't need to stream it for LTO. */
30613 flush_limbo_die_list ();
30615 gen_scheduled_generic_parms_dies ();
30616 gen_remaining_tmpl_value_param_die_attribute ();
30618 /* Add DW_AT_linkage_name for all deferred DIEs. */
30619 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30621 tree decl = node->created_for;
30622 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30623 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30624 ended up in deferred_asm_name before we knew it was
30625 constant and never written to disk. */
30626 && DECL_ASSEMBLER_NAME (decl))
30628 add_linkage_attr (node->die, decl);
30629 move_linkage_attr (node->die);
30632 deferred_asm_name = NULL;
30634 if (flag_eliminate_unused_debug_types)
30635 prune_unused_types ();
30637 /* Generate separate COMDAT sections for type DIEs. */
30638 if (use_debug_types)
30640 break_out_comdat_types (comp_unit_die ());
30642 /* Each new type_unit DIE was added to the limbo die list when created.
30643 Since these have all been added to comdat_type_list, clear the
30644 limbo die list. */
30645 limbo_die_list = NULL;
30647 /* For each new comdat type unit, copy declarations for incomplete
30648 types to make the new unit self-contained (i.e., no direct
30649 references to the main compile unit). */
30650 for (comdat_type_node *ctnode = comdat_type_list;
30651 ctnode != NULL; ctnode = ctnode->next)
30652 copy_decls_for_unworthy_types (ctnode->root_die);
30653 copy_decls_for_unworthy_types (comp_unit_die ());
30655 /* In the process of copying declarations from one unit to another,
30656 we may have left some declarations behind that are no longer
30657 referenced. Prune them. */
30658 prune_unused_types ();
30661 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30662 with dw_val_class_decl_ref operand. */
30663 note_variable_value (comp_unit_die ());
30664 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30665 note_variable_value (node->die);
30666 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30667 ctnode = ctnode->next)
30668 note_variable_value (ctnode->root_die);
30669 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30670 note_variable_value (node->die);
30672 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30673 both the main_cu and all skeleton TUs. Making this call unconditional
30674 would end up either adding a second copy of the AT_pubnames attribute, or
30675 requiring a special case in add_top_level_skeleton_die_attrs. */
30676 if (!dwarf_split_debug_info)
30677 add_AT_pubnames (comp_unit_die ());
30679 /* The early debug phase is now finished. */
30680 early_dwarf_finished = true;
30682 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30683 if (!flag_generate_lto && !flag_generate_offload)
30684 return;
30686 /* Now as we are going to output for LTO initialize sections and labels
30687 to the LTO variants. We don't need a random-seed postfix as other
30688 LTO sections as linking the LTO debug sections into one in a partial
30689 link is fine. */
30690 init_sections_and_labels (true);
30692 /* The output below is modeled after dwarf2out_finish with all
30693 location related output removed and some LTO specific changes.
30694 Some refactoring might make both smaller and easier to match up. */
30696 /* Traverse the DIE's and add add sibling attributes to those DIE's
30697 that have children. */
30698 add_sibling_attributes (comp_unit_die ());
30699 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30700 add_sibling_attributes (node->die);
30701 for (comdat_type_node *ctnode = comdat_type_list;
30702 ctnode != NULL; ctnode = ctnode->next)
30703 add_sibling_attributes (ctnode->root_die);
30705 if (have_macinfo)
30706 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30707 macinfo_section_label);
30709 save_macinfo_strings ();
30711 /* Output all of the compilation units. We put the main one last so that
30712 the offsets are available to output_pubnames. */
30713 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30714 output_comp_unit (node->die, 0, NULL);
30716 hash_table<comdat_type_hasher> comdat_type_table (100);
30717 for (comdat_type_node *ctnode = comdat_type_list;
30718 ctnode != NULL; ctnode = ctnode->next)
30720 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30722 /* Don't output duplicate types. */
30723 if (*slot != HTAB_EMPTY_ENTRY)
30724 continue;
30726 /* Add a pointer to the line table for the main compilation unit
30727 so that the debugger can make sense of DW_AT_decl_file
30728 attributes. */
30729 if (debug_info_level >= DINFO_LEVEL_TERSE)
30730 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30731 (!dwarf_split_debug_info
30732 ? debug_line_section_label
30733 : debug_skeleton_line_section_label));
30735 output_comdat_type_unit (ctnode);
30736 *slot = ctnode;
30739 /* Stick a unique symbol to the main debuginfo section. */
30740 compute_comp_unit_symbol (comp_unit_die ());
30742 /* Output the main compilation unit. We always need it if only for
30743 the CU symbol. */
30744 output_comp_unit (comp_unit_die (), true, NULL);
30746 /* Output the abbreviation table. */
30747 if (vec_safe_length (abbrev_die_table) != 1)
30749 switch_to_section (debug_abbrev_section);
30750 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30751 output_abbrev_section ();
30754 /* Have to end the macro section. */
30755 if (have_macinfo)
30757 /* We have to save macinfo state if we need to output it again
30758 for the FAT part of the object. */
30759 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30760 if (flag_fat_lto_objects)
30761 macinfo_table = macinfo_table->copy ();
30763 switch_to_section (debug_macinfo_section);
30764 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30765 output_macinfo (debug_skeleton_line_section_label, true);
30766 dw2_asm_output_data (1, 0, "End compilation unit");
30768 /* Emit a skeleton debug_line section. */
30769 switch_to_section (debug_skeleton_line_section);
30770 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30771 output_line_info (true);
30773 if (flag_fat_lto_objects)
30775 vec_free (macinfo_table);
30776 macinfo_table = saved_macinfo_table;
30781 /* If we emitted any indirect strings, output the string table too. */
30782 if (debug_str_hash || skeleton_debug_str_hash)
30783 output_indirect_strings ();
30785 /* Switch back to the text section. */
30786 switch_to_section (text_section);
30789 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30790 within the same process. For use by toplev::finalize. */
30792 void
30793 dwarf2out_c_finalize (void)
30795 last_var_location_insn = NULL;
30796 cached_next_real_insn = NULL;
30797 used_rtx_array = NULL;
30798 incomplete_types = NULL;
30799 decl_scope_table = NULL;
30800 debug_info_section = NULL;
30801 debug_skeleton_info_section = NULL;
30802 debug_abbrev_section = NULL;
30803 debug_skeleton_abbrev_section = NULL;
30804 debug_aranges_section = NULL;
30805 debug_addr_section = NULL;
30806 debug_macinfo_section = NULL;
30807 debug_line_section = NULL;
30808 debug_skeleton_line_section = NULL;
30809 debug_loc_section = NULL;
30810 debug_pubnames_section = NULL;
30811 debug_pubtypes_section = NULL;
30812 debug_str_section = NULL;
30813 debug_line_str_section = NULL;
30814 debug_str_dwo_section = NULL;
30815 debug_str_offsets_section = NULL;
30816 debug_ranges_section = NULL;
30817 debug_frame_section = NULL;
30818 fde_vec = NULL;
30819 debug_str_hash = NULL;
30820 debug_line_str_hash = NULL;
30821 skeleton_debug_str_hash = NULL;
30822 dw2_string_counter = 0;
30823 have_multiple_function_sections = false;
30824 text_section_used = false;
30825 cold_text_section_used = false;
30826 cold_text_section = NULL;
30827 current_unit_personality = NULL;
30829 early_dwarf = false;
30830 early_dwarf_finished = false;
30832 next_die_offset = 0;
30833 single_comp_unit_die = NULL;
30834 comdat_type_list = NULL;
30835 limbo_die_list = NULL;
30836 file_table = NULL;
30837 decl_die_table = NULL;
30838 common_block_die_table = NULL;
30839 decl_loc_table = NULL;
30840 call_arg_locations = NULL;
30841 call_arg_loc_last = NULL;
30842 call_site_count = -1;
30843 tail_call_site_count = -1;
30844 cached_dw_loc_list_table = NULL;
30845 abbrev_die_table = NULL;
30846 delete dwarf_proc_stack_usage_map;
30847 dwarf_proc_stack_usage_map = NULL;
30848 line_info_label_num = 0;
30849 cur_line_info_table = NULL;
30850 text_section_line_info = NULL;
30851 cold_text_section_line_info = NULL;
30852 separate_line_info = NULL;
30853 info_section_emitted = false;
30854 pubname_table = NULL;
30855 pubtype_table = NULL;
30856 macinfo_table = NULL;
30857 ranges_table = NULL;
30858 ranges_by_label = NULL;
30859 rnglist_idx = 0;
30860 have_location_lists = false;
30861 loclabel_num = 0;
30862 poc_label_num = 0;
30863 last_emitted_file = NULL;
30864 label_num = 0;
30865 tmpl_value_parm_die_table = NULL;
30866 generic_type_instances = NULL;
30867 frame_pointer_fb_offset = 0;
30868 frame_pointer_fb_offset_valid = false;
30869 base_types.release ();
30870 XDELETEVEC (producer_string);
30871 producer_string = NULL;
30874 #include "gt-dwarf2out.h"