2017-08-21 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dwarf2out.c
blob764fd36bc8260e47e2f8e755dfaf69da667a9ec7
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static unsigned macinfo_label_base = 1;
168 static GTY(()) section *debug_line_section;
169 static GTY(()) section *debug_skeleton_line_section;
170 static GTY(()) section *debug_loc_section;
171 static GTY(()) section *debug_pubnames_section;
172 static GTY(()) section *debug_pubtypes_section;
173 static GTY(()) section *debug_str_section;
174 static GTY(()) section *debug_line_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
196 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
197 #endif
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
229 typedef const char *compare_type;
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
237 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
239 /* With split_debug_info, both the comp_dir and dwo_name go in the
240 main object file, rather than the dwo, similar to the force_direct
241 parameter elsewhere but with additional complications:
243 1) The string is needed in both the main object file and the dwo.
244 That is, the comp_dir and dwo_name will appear in both places.
246 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
247 DW_FORM_line_strp or DW_FORM_GNU_str_index.
249 3) GCC chooses the form to use late, depending on the size and
250 reference count.
252 Rather than forcing the all debug string handling functions and
253 callers to deal with these complications, simply use a separate,
254 special-cased string table for any attribute that should go in the
255 main object file. This limits the complexity to just the places
256 that need it. */
258 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
260 static GTY(()) int dw2_string_counter;
262 /* True if the compilation unit places functions in more than one section. */
263 static GTY(()) bool have_multiple_function_sections = false;
265 /* Whether the default text and cold text sections have been used at all. */
267 static GTY(()) bool text_section_used = false;
268 static GTY(()) bool cold_text_section_used = false;
270 /* The default cold text section. */
271 static GTY(()) section *cold_text_section;
273 /* The DIE for C++14 'auto' in a function return type. */
274 static GTY(()) dw_die_ref auto_die;
276 /* The DIE for C++14 'decltype(auto)' in a function return type. */
277 static GTY(()) dw_die_ref decltype_auto_die;
279 /* Forward declarations for functions defined in this file. */
281 static void output_call_frame_info (int);
282 static void dwarf2out_note_section_used (void);
284 /* Personality decl of current unit. Used only when assembler does not support
285 personality CFI. */
286 static GTY(()) rtx current_unit_personality;
288 /* .debug_rnglists next index. */
289 static unsigned int rnglist_idx;
291 /* Data and reference forms for relocatable data. */
292 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
293 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
295 #ifndef DEBUG_FRAME_SECTION
296 #define DEBUG_FRAME_SECTION ".debug_frame"
297 #endif
299 #ifndef FUNC_BEGIN_LABEL
300 #define FUNC_BEGIN_LABEL "LFB"
301 #endif
303 #ifndef FUNC_END_LABEL
304 #define FUNC_END_LABEL "LFE"
305 #endif
307 #ifndef PROLOGUE_END_LABEL
308 #define PROLOGUE_END_LABEL "LPE"
309 #endif
311 #ifndef EPILOGUE_BEGIN_LABEL
312 #define EPILOGUE_BEGIN_LABEL "LEB"
313 #endif
315 #ifndef FRAME_BEGIN_LABEL
316 #define FRAME_BEGIN_LABEL "Lframe"
317 #endif
318 #define CIE_AFTER_SIZE_LABEL "LSCIE"
319 #define CIE_END_LABEL "LECIE"
320 #define FDE_LABEL "LSFDE"
321 #define FDE_AFTER_SIZE_LABEL "LASFDE"
322 #define FDE_END_LABEL "LEFDE"
323 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
324 #define LINE_NUMBER_END_LABEL "LELT"
325 #define LN_PROLOG_AS_LABEL "LASLTP"
326 #define LN_PROLOG_END_LABEL "LELTP"
327 #define DIE_LABEL_PREFIX "DW"
329 /* Match the base name of a file to the base name of a compilation unit. */
331 static int
332 matches_main_base (const char *path)
334 /* Cache the last query. */
335 static const char *last_path = NULL;
336 static int last_match = 0;
337 if (path != last_path)
339 const char *base;
340 int length = base_of_path (path, &base);
341 last_path = path;
342 last_match = (length == main_input_baselength
343 && memcmp (base, main_input_basename, length) == 0);
345 return last_match;
348 #ifdef DEBUG_DEBUG_STRUCT
350 static int
351 dump_struct_debug (tree type, enum debug_info_usage usage,
352 enum debug_struct_file criterion, int generic,
353 int matches, int result)
355 /* Find the type name. */
356 tree type_decl = TYPE_STUB_DECL (type);
357 tree t = type_decl;
358 const char *name = 0;
359 if (TREE_CODE (t) == TYPE_DECL)
360 t = DECL_NAME (t);
361 if (t)
362 name = IDENTIFIER_POINTER (t);
364 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
365 criterion,
366 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
367 matches ? "bas" : "hdr",
368 generic ? "gen" : "ord",
369 usage == DINFO_USAGE_DFN ? ";" :
370 usage == DINFO_USAGE_DIR_USE ? "." : "*",
371 result,
372 (void*) type_decl, name);
373 return result;
375 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
376 dump_struct_debug (type, usage, criterion, generic, matches, result)
378 #else
380 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
381 (result)
383 #endif
385 /* Get the number of HOST_WIDE_INTs needed to represent the precision
386 of the number. Some constants have a large uniform precision, so
387 we get the precision needed for the actual value of the number. */
389 static unsigned int
390 get_full_len (const wide_int &op)
392 int prec = wi::min_precision (op, UNSIGNED);
393 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
394 / HOST_BITS_PER_WIDE_INT);
397 static bool
398 should_emit_struct_debug (tree type, enum debug_info_usage usage)
400 enum debug_struct_file criterion;
401 tree type_decl;
402 bool generic = lang_hooks.types.generic_p (type);
404 if (generic)
405 criterion = debug_struct_generic[usage];
406 else
407 criterion = debug_struct_ordinary[usage];
409 if (criterion == DINFO_STRUCT_FILE_NONE)
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 if (criterion == DINFO_STRUCT_FILE_ANY)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
416 if (type_decl != NULL)
418 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
419 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
421 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
422 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
428 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
429 switch to the data section instead, and write out a synthetic start label
430 for collect2 the first time around. */
432 static void
433 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
435 if (eh_frame_section == 0)
437 int flags;
439 if (EH_TABLES_CAN_BE_READ_ONLY)
441 int fde_encoding;
442 int per_encoding;
443 int lsda_encoding;
445 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
446 /*global=*/0);
447 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
448 /*global=*/1);
449 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
450 /*global=*/0);
451 flags = ((! flag_pic
452 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
453 && (fde_encoding & 0x70) != DW_EH_PE_aligned
454 && (per_encoding & 0x70) != DW_EH_PE_absptr
455 && (per_encoding & 0x70) != DW_EH_PE_aligned
456 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
457 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
458 ? 0 : SECTION_WRITE);
460 else
461 flags = SECTION_WRITE;
463 #ifdef EH_FRAME_SECTION_NAME
464 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
465 #else
466 eh_frame_section = ((flags == SECTION_WRITE)
467 ? data_section : readonly_data_section);
468 #endif /* EH_FRAME_SECTION_NAME */
471 switch_to_section (eh_frame_section);
473 #ifdef EH_FRAME_THROUGH_COLLECT2
474 /* We have no special eh_frame section. Emit special labels to guide
475 collect2. */
476 if (!back)
478 tree label = get_file_function_name ("F");
479 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
480 targetm.asm_out.globalize_label (asm_out_file,
481 IDENTIFIER_POINTER (label));
482 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
484 #endif
487 /* Switch [BACK] to the eh or debug frame table section, depending on
488 FOR_EH. */
490 static void
491 switch_to_frame_table_section (int for_eh, bool back)
493 if (for_eh)
494 switch_to_eh_frame_section (back);
495 else
497 if (!debug_frame_section)
498 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
499 SECTION_DEBUG, NULL);
500 switch_to_section (debug_frame_section);
504 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
506 enum dw_cfi_oprnd_type
507 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
509 switch (cfi)
511 case DW_CFA_nop:
512 case DW_CFA_GNU_window_save:
513 case DW_CFA_remember_state:
514 case DW_CFA_restore_state:
515 return dw_cfi_oprnd_unused;
517 case DW_CFA_set_loc:
518 case DW_CFA_advance_loc1:
519 case DW_CFA_advance_loc2:
520 case DW_CFA_advance_loc4:
521 case DW_CFA_MIPS_advance_loc8:
522 return dw_cfi_oprnd_addr;
524 case DW_CFA_offset:
525 case DW_CFA_offset_extended:
526 case DW_CFA_def_cfa:
527 case DW_CFA_offset_extended_sf:
528 case DW_CFA_def_cfa_sf:
529 case DW_CFA_restore:
530 case DW_CFA_restore_extended:
531 case DW_CFA_undefined:
532 case DW_CFA_same_value:
533 case DW_CFA_def_cfa_register:
534 case DW_CFA_register:
535 case DW_CFA_expression:
536 case DW_CFA_val_expression:
537 return dw_cfi_oprnd_reg_num;
539 case DW_CFA_def_cfa_offset:
540 case DW_CFA_GNU_args_size:
541 case DW_CFA_def_cfa_offset_sf:
542 return dw_cfi_oprnd_offset;
544 case DW_CFA_def_cfa_expression:
545 return dw_cfi_oprnd_loc;
547 default:
548 gcc_unreachable ();
552 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
554 enum dw_cfi_oprnd_type
555 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
557 switch (cfi)
559 case DW_CFA_def_cfa:
560 case DW_CFA_def_cfa_sf:
561 case DW_CFA_offset:
562 case DW_CFA_offset_extended_sf:
563 case DW_CFA_offset_extended:
564 return dw_cfi_oprnd_offset;
566 case DW_CFA_register:
567 return dw_cfi_oprnd_reg_num;
569 case DW_CFA_expression:
570 case DW_CFA_val_expression:
571 return dw_cfi_oprnd_loc;
573 default:
574 return dw_cfi_oprnd_unused;
578 /* Output one FDE. */
580 static void
581 output_fde (dw_fde_ref fde, bool for_eh, bool second,
582 char *section_start_label, int fde_encoding, char *augmentation,
583 bool any_lsda_needed, int lsda_encoding)
585 const char *begin, *end;
586 static unsigned int j;
587 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
589 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
590 /* empty */ 0);
591 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
592 for_eh + j);
593 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
595 if (!XCOFF_DEBUGGING_INFO || for_eh)
597 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
598 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
599 " indicating 64-bit DWARF extension");
600 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
601 "FDE Length");
603 ASM_OUTPUT_LABEL (asm_out_file, l1);
605 if (for_eh)
606 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
607 else
608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
609 debug_frame_section, "FDE CIE offset");
611 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
612 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614 if (for_eh)
616 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
617 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
618 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
619 "FDE initial location");
620 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
621 end, begin, "FDE address range");
623 else
625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 if (augmentation[0])
631 if (any_lsda_needed)
633 int size = size_of_encoded_value (lsda_encoding);
635 if (lsda_encoding == DW_EH_PE_aligned)
637 int offset = ( 4 /* Length */
638 + 4 /* CIE offset */
639 + 2 * size_of_encoded_value (fde_encoding)
640 + 1 /* Augmentation size */ );
641 int pad = -offset & (PTR_SIZE - 1);
643 size += pad;
644 gcc_assert (size_of_uleb128 (size) == 1);
647 dw2_asm_output_data_uleb128 (size, "Augmentation size");
649 if (fde->uses_eh_lsda)
651 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
652 fde->funcdef_number);
653 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
654 gen_rtx_SYMBOL_REF (Pmode, l1),
655 false,
656 "Language Specific Data Area");
658 else
660 if (lsda_encoding == DW_EH_PE_aligned)
661 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
662 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
663 "Language Specific Data Area (none)");
666 else
667 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 /* Loop through the Call Frame Instructions associated with this FDE. */
671 fde->dw_fde_current_label = begin;
673 size_t from, until, i;
675 from = 0;
676 until = vec_safe_length (fde->dw_fde_cfi);
678 if (fde->dw_fde_second_begin == NULL)
680 else if (!second)
681 until = fde->dw_fde_switch_cfi_index;
682 else
683 from = fde->dw_fde_switch_cfi_index;
685 for (i = from; i < until; i++)
686 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 /* If we are to emit a ref/link from function bodies to their frame tables,
690 do it now. This is typically performed to make sure that tables
691 associated with functions are dragged with them and not discarded in
692 garbage collecting links. We need to do this on a per function basis to
693 cope with -ffunction-sections. */
695 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
696 /* Switch to the function section, emit the ref to the tables, and
697 switch *back* into the table section. */
698 switch_to_section (function_section (fde->decl));
699 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
700 switch_to_frame_table_section (for_eh, true);
701 #endif
703 /* Pad the FDE out to an address sized boundary. */
704 ASM_OUTPUT_ALIGN (asm_out_file,
705 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
706 ASM_OUTPUT_LABEL (asm_out_file, l2);
708 j += 2;
711 /* Return true if frame description entry FDE is needed for EH. */
713 static bool
714 fde_needed_for_eh_p (dw_fde_ref fde)
716 if (flag_asynchronous_unwind_tables)
717 return true;
719 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
720 return true;
722 if (fde->uses_eh_lsda)
723 return true;
725 /* If exceptions are enabled, we have collected nothrow info. */
726 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
727 return false;
729 return true;
732 /* Output the call frame information used to record information
733 that relates to calculating the frame pointer, and records the
734 location of saved registers. */
736 static void
737 output_call_frame_info (int for_eh)
739 unsigned int i;
740 dw_fde_ref fde;
741 dw_cfi_ref cfi;
742 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
743 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
744 bool any_lsda_needed = false;
745 char augmentation[6];
746 int augmentation_size;
747 int fde_encoding = DW_EH_PE_absptr;
748 int per_encoding = DW_EH_PE_absptr;
749 int lsda_encoding = DW_EH_PE_absptr;
750 int return_reg;
751 rtx personality = NULL;
752 int dw_cie_version;
754 /* Don't emit a CIE if there won't be any FDEs. */
755 if (!fde_vec)
756 return;
758 /* Nothing to do if the assembler's doing it all. */
759 if (dwarf2out_do_cfi_asm ())
760 return;
762 /* If we don't have any functions we'll want to unwind out of, don't emit
763 any EH unwind information. If we make FDEs linkonce, we may have to
764 emit an empty label for an FDE that wouldn't otherwise be emitted. We
765 want to avoid having an FDE kept around when the function it refers to
766 is discarded. Example where this matters: a primary function template
767 in C++ requires EH information, an explicit specialization doesn't. */
768 if (for_eh)
770 bool any_eh_needed = false;
772 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 if (fde->uses_eh_lsda)
775 any_eh_needed = any_lsda_needed = true;
776 else if (fde_needed_for_eh_p (fde))
777 any_eh_needed = true;
778 else if (TARGET_USES_WEAK_UNWIND_INFO)
779 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 if (!any_eh_needed)
783 return;
786 /* We're going to be generating comments, so turn on app. */
787 if (flag_debug_asm)
788 app_enable ();
790 /* Switch to the proper frame section, first time. */
791 switch_to_frame_table_section (for_eh, false);
793 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796 /* Output the CIE. */
797 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802 dw2_asm_output_data (4, 0xffffffff,
803 "Initial length escape value indicating 64-bit DWARF extension");
804 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 unsigned int column ATTRIBUTE_UNUSED,
1035 const char *file ATTRIBUTE_UNUSED)
1037 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1038 char * dup_label;
1039 dw_fde_ref fde;
1040 section *fnsec;
1041 bool do_frame;
1043 current_function_func_begin_label = NULL;
1045 do_frame = dwarf2out_do_frame ();
1047 /* ??? current_function_func_begin_label is also used by except.c for
1048 call-site information. We must emit this label if it might be used. */
1049 if (!do_frame
1050 && (!flag_exceptions
1051 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1052 return;
1054 fnsec = function_section (current_function_decl);
1055 switch_to_section (fnsec);
1056 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1057 current_function_funcdef_no);
1058 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1059 current_function_funcdef_no);
1060 dup_label = xstrdup (label);
1061 current_function_func_begin_label = dup_label;
1063 /* We can elide the fde allocation if we're not emitting debug info. */
1064 if (!do_frame)
1065 return;
1067 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1068 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1069 would include pass_dwarf2_frame. If we've not created the FDE yet,
1070 do so now. */
1071 fde = cfun->fde;
1072 if (fde == NULL)
1073 fde = dwarf2out_alloc_current_fde ();
1075 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1076 fde->dw_fde_begin = dup_label;
1077 fde->dw_fde_current_label = dup_label;
1078 fde->in_std_section = (fnsec == text_section
1079 || (cold_text_section && fnsec == cold_text_section));
1081 /* We only want to output line number information for the genuine dwarf2
1082 prologue case, not the eh frame case. */
1083 #ifdef DWARF2_DEBUGGING_INFO
1084 if (file)
1085 dwarf2out_source_line (line, column, file, 0, true);
1086 #endif
1088 if (dwarf2out_do_cfi_asm ())
1089 dwarf2out_do_cfi_startproc (false);
1090 else
1092 rtx personality = get_personality_function (current_function_decl);
1093 if (!current_unit_personality)
1094 current_unit_personality = personality;
1096 /* We cannot keep a current personality per function as without CFI
1097 asm, at the point where we emit the CFI data, there is no current
1098 function anymore. */
1099 if (personality && current_unit_personality != personality)
1100 sorry ("multiple EH personalities are supported only with assemblers "
1101 "supporting .cfi_personality directive");
1105 /* Output a marker (i.e. a label) for the end of the generated code
1106 for a function prologue. This gets called *after* the prologue code has
1107 been generated. */
1109 void
1110 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1111 const char *file ATTRIBUTE_UNUSED)
1113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115 /* Output a label to mark the endpoint of the code generated for this
1116 function. */
1117 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1118 current_function_funcdef_no);
1119 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1120 current_function_funcdef_no);
1121 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1124 /* Output a marker (i.e. a label) for the beginning of the generated code
1125 for a function epilogue. This gets called *before* the prologue code has
1126 been generated. */
1128 void
1129 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1130 const char *file ATTRIBUTE_UNUSED)
1132 dw_fde_ref fde = cfun->fde;
1133 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135 if (fde->dw_fde_vms_begin_epilogue)
1136 return;
1138 /* Output a label to mark the endpoint of the code generated for this
1139 function. */
1140 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1141 current_function_funcdef_no);
1142 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1143 current_function_funcdef_no);
1144 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1147 /* Output a marker (i.e. a label) for the absolute end of the generated code
1148 for a function definition. This gets called *after* the epilogue code has
1149 been generated. */
1151 void
1152 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1153 const char *file ATTRIBUTE_UNUSED)
1155 dw_fde_ref fde;
1156 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158 last_var_location_insn = NULL;
1159 cached_next_real_insn = NULL;
1161 if (dwarf2out_do_cfi_asm ())
1162 fprintf (asm_out_file, "\t.cfi_endproc\n");
1164 /* Output a label to mark the endpoint of the code generated for this
1165 function. */
1166 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1167 current_function_funcdef_no);
1168 ASM_OUTPUT_LABEL (asm_out_file, label);
1169 fde = cfun->fde;
1170 gcc_assert (fde != NULL);
1171 if (fde->dw_fde_second_begin == NULL)
1172 fde->dw_fde_end = xstrdup (label);
1175 void
1176 dwarf2out_frame_finish (void)
1178 /* Output call frame information. */
1179 if (targetm.debug_unwind_info () == UI_DWARF2)
1180 output_call_frame_info (0);
1182 /* Output another copy for the unwinder. */
1183 if ((flag_unwind_tables || flag_exceptions)
1184 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1185 output_call_frame_info (1);
1188 /* Note that the current function section is being used for code. */
1190 static void
1191 dwarf2out_note_section_used (void)
1193 section *sec = current_function_section ();
1194 if (sec == text_section)
1195 text_section_used = true;
1196 else if (sec == cold_text_section)
1197 cold_text_section_used = true;
1200 static void var_location_switch_text_section (void);
1201 static void set_cur_line_info_table (section *);
1203 void
1204 dwarf2out_switch_text_section (void)
1206 section *sect;
1207 dw_fde_ref fde = cfun->fde;
1209 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1211 if (!in_cold_section_p)
1213 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1214 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1215 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1217 else
1219 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1220 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1221 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1223 have_multiple_function_sections = true;
1225 /* There is no need to mark used sections when not debugging. */
1226 if (cold_text_section != NULL)
1227 dwarf2out_note_section_used ();
1229 if (dwarf2out_do_cfi_asm ())
1230 fprintf (asm_out_file, "\t.cfi_endproc\n");
1232 /* Now do the real section switch. */
1233 sect = current_function_section ();
1234 switch_to_section (sect);
1236 fde->second_in_std_section
1237 = (sect == text_section
1238 || (cold_text_section && sect == cold_text_section));
1240 if (dwarf2out_do_cfi_asm ())
1241 dwarf2out_do_cfi_startproc (true);
1243 var_location_switch_text_section ();
1245 if (cold_text_section != NULL)
1246 set_cur_line_info_table (sect);
1249 /* And now, the subset of the debugging information support code necessary
1250 for emitting location expressions. */
1252 /* Data about a single source file. */
1253 struct GTY((for_user)) dwarf_file_data {
1254 const char * filename;
1255 int emitted_number;
1258 /* Describe an entry into the .debug_addr section. */
1260 enum ate_kind {
1261 ate_kind_rtx,
1262 ate_kind_rtx_dtprel,
1263 ate_kind_label
1266 struct GTY((for_user)) addr_table_entry {
1267 enum ate_kind kind;
1268 unsigned int refcount;
1269 unsigned int index;
1270 union addr_table_entry_struct_union
1272 rtx GTY ((tag ("0"))) rtl;
1273 char * GTY ((tag ("1"))) label;
1275 GTY ((desc ("%1.kind"))) addr;
1278 /* Location lists are ranges + location descriptions for that range,
1279 so you can track variables that are in different places over
1280 their entire life. */
1281 typedef struct GTY(()) dw_loc_list_struct {
1282 dw_loc_list_ref dw_loc_next;
1283 const char *begin; /* Label and addr_entry for start of range */
1284 addr_table_entry *begin_entry;
1285 const char *end; /* Label for end of range */
1286 char *ll_symbol; /* Label for beginning of location list.
1287 Only on head of list */
1288 const char *section; /* Section this loclist is relative to */
1289 dw_loc_descr_ref expr;
1290 hashval_t hash;
1291 /* True if all addresses in this and subsequent lists are known to be
1292 resolved. */
1293 bool resolved_addr;
1294 /* True if this list has been replaced by dw_loc_next. */
1295 bool replaced;
1296 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1297 section. */
1298 unsigned char emitted : 1;
1299 /* True if hash field is index rather than hash value. */
1300 unsigned char num_assigned : 1;
1301 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1302 unsigned char offset_emitted : 1;
1303 /* True if note_variable_value_in_expr has been called on it. */
1304 unsigned char noted_variable_value : 1;
1305 /* True if the range should be emitted even if begin and end
1306 are the same. */
1307 bool force;
1308 } dw_loc_list_node;
1310 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1311 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1313 /* Convert a DWARF stack opcode into its string name. */
1315 static const char *
1316 dwarf_stack_op_name (unsigned int op)
1318 const char *name = get_DW_OP_name (op);
1320 if (name != NULL)
1321 return name;
1323 return "OP_<unknown>";
1326 /* Return a pointer to a newly allocated location description. Location
1327 descriptions are simple expression terms that can be strung
1328 together to form more complicated location (address) descriptions. */
1330 static inline dw_loc_descr_ref
1331 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1332 unsigned HOST_WIDE_INT oprnd2)
1334 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1336 descr->dw_loc_opc = op;
1337 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd1.val_entry = NULL;
1339 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1340 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1341 descr->dw_loc_oprnd2.val_entry = NULL;
1342 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1344 return descr;
1347 /* Return a pointer to a newly allocated location description for
1348 REG and OFFSET. */
1350 static inline dw_loc_descr_ref
1351 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1353 if (reg <= 31)
1354 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1355 offset, 0);
1356 else
1357 return new_loc_descr (DW_OP_bregx, reg, offset);
1360 /* Add a location description term to a location description expression. */
1362 static inline void
1363 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1365 dw_loc_descr_ref *d;
1367 /* Find the end of the chain. */
1368 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1371 *d = descr;
1374 /* Compare two location operands for exact equality. */
1376 static bool
1377 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1379 if (a->val_class != b->val_class)
1380 return false;
1381 switch (a->val_class)
1383 case dw_val_class_none:
1384 return true;
1385 case dw_val_class_addr:
1386 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1388 case dw_val_class_offset:
1389 case dw_val_class_unsigned_const:
1390 case dw_val_class_const:
1391 case dw_val_class_unsigned_const_implicit:
1392 case dw_val_class_const_implicit:
1393 case dw_val_class_range_list:
1394 /* These are all HOST_WIDE_INT, signed or unsigned. */
1395 return a->v.val_unsigned == b->v.val_unsigned;
1397 case dw_val_class_loc:
1398 return a->v.val_loc == b->v.val_loc;
1399 case dw_val_class_loc_list:
1400 return a->v.val_loc_list == b->v.val_loc_list;
1401 case dw_val_class_die_ref:
1402 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1403 case dw_val_class_fde_ref:
1404 return a->v.val_fde_index == b->v.val_fde_index;
1405 case dw_val_class_lbl_id:
1406 case dw_val_class_lineptr:
1407 case dw_val_class_macptr:
1408 case dw_val_class_loclistsptr:
1409 case dw_val_class_high_pc:
1410 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1411 case dw_val_class_str:
1412 return a->v.val_str == b->v.val_str;
1413 case dw_val_class_flag:
1414 return a->v.val_flag == b->v.val_flag;
1415 case dw_val_class_file:
1416 case dw_val_class_file_implicit:
1417 return a->v.val_file == b->v.val_file;
1418 case dw_val_class_decl_ref:
1419 return a->v.val_decl_ref == b->v.val_decl_ref;
1421 case dw_val_class_const_double:
1422 return (a->v.val_double.high == b->v.val_double.high
1423 && a->v.val_double.low == b->v.val_double.low);
1425 case dw_val_class_wide_int:
1426 return *a->v.val_wide == *b->v.val_wide;
1428 case dw_val_class_vec:
1430 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1431 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1433 return (a_len == b_len
1434 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1437 case dw_val_class_data8:
1438 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1440 case dw_val_class_vms_delta:
1441 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1442 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1444 case dw_val_class_discr_value:
1445 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1446 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1447 case dw_val_class_discr_list:
1448 /* It makes no sense comparing two discriminant value lists. */
1449 return false;
1451 gcc_unreachable ();
1454 /* Compare two location atoms for exact equality. */
1456 static bool
1457 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1459 if (a->dw_loc_opc != b->dw_loc_opc)
1460 return false;
1462 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1463 address size, but since we always allocate cleared storage it
1464 should be zero for other types of locations. */
1465 if (a->dtprel != b->dtprel)
1466 return false;
1468 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1469 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1472 /* Compare two complete location expressions for exact equality. */
1474 bool
1475 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1477 while (1)
1479 if (a == b)
1480 return true;
1481 if (a == NULL || b == NULL)
1482 return false;
1483 if (!loc_descr_equal_p_1 (a, b))
1484 return false;
1486 a = a->dw_loc_next;
1487 b = b->dw_loc_next;
1492 /* Add a constant OFFSET to a location expression. */
1494 static void
1495 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1497 dw_loc_descr_ref loc;
1498 HOST_WIDE_INT *p;
1500 gcc_assert (*list_head != NULL);
1502 if (!offset)
1503 return;
1505 /* Find the end of the chain. */
1506 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1509 p = NULL;
1510 if (loc->dw_loc_opc == DW_OP_fbreg
1511 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1512 p = &loc->dw_loc_oprnd1.v.val_int;
1513 else if (loc->dw_loc_opc == DW_OP_bregx)
1514 p = &loc->dw_loc_oprnd2.v.val_int;
1516 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1517 offset. Don't optimize if an signed integer overflow would happen. */
1518 if (p != NULL
1519 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1520 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1521 *p += offset;
1523 else if (offset > 0)
1524 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1526 else
1528 loc->dw_loc_next
1529 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1530 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534 /* Add a constant OFFSET to a location list. */
1536 static void
1537 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1539 dw_loc_list_ref d;
1540 for (d = list_head; d != NULL; d = d->dw_loc_next)
1541 loc_descr_plus_const (&d->expr, offset);
1544 #define DWARF_REF_SIZE \
1545 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1547 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1548 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1549 DW_FORM_data16 with 128 bits. */
1550 #define DWARF_LARGEST_DATA_FORM_BITS \
1551 (dwarf_version >= 5 ? 128 : 64)
1553 /* Utility inline function for construction of ops that were GNU extension
1554 before DWARF 5. */
1555 static inline enum dwarf_location_atom
1556 dwarf_OP (enum dwarf_location_atom op)
1558 switch (op)
1560 case DW_OP_implicit_pointer:
1561 if (dwarf_version < 5)
1562 return DW_OP_GNU_implicit_pointer;
1563 break;
1565 case DW_OP_entry_value:
1566 if (dwarf_version < 5)
1567 return DW_OP_GNU_entry_value;
1568 break;
1570 case DW_OP_const_type:
1571 if (dwarf_version < 5)
1572 return DW_OP_GNU_const_type;
1573 break;
1575 case DW_OP_regval_type:
1576 if (dwarf_version < 5)
1577 return DW_OP_GNU_regval_type;
1578 break;
1580 case DW_OP_deref_type:
1581 if (dwarf_version < 5)
1582 return DW_OP_GNU_deref_type;
1583 break;
1585 case DW_OP_convert:
1586 if (dwarf_version < 5)
1587 return DW_OP_GNU_convert;
1588 break;
1590 case DW_OP_reinterpret:
1591 if (dwarf_version < 5)
1592 return DW_OP_GNU_reinterpret;
1593 break;
1595 default:
1596 break;
1598 return op;
1601 /* Similarly for attributes. */
1602 static inline enum dwarf_attribute
1603 dwarf_AT (enum dwarf_attribute at)
1605 switch (at)
1607 case DW_AT_call_return_pc:
1608 if (dwarf_version < 5)
1609 return DW_AT_low_pc;
1610 break;
1612 case DW_AT_call_tail_call:
1613 if (dwarf_version < 5)
1614 return DW_AT_GNU_tail_call;
1615 break;
1617 case DW_AT_call_origin:
1618 if (dwarf_version < 5)
1619 return DW_AT_abstract_origin;
1620 break;
1622 case DW_AT_call_target:
1623 if (dwarf_version < 5)
1624 return DW_AT_GNU_call_site_target;
1625 break;
1627 case DW_AT_call_target_clobbered:
1628 if (dwarf_version < 5)
1629 return DW_AT_GNU_call_site_target_clobbered;
1630 break;
1632 case DW_AT_call_parameter:
1633 if (dwarf_version < 5)
1634 return DW_AT_abstract_origin;
1635 break;
1637 case DW_AT_call_value:
1638 if (dwarf_version < 5)
1639 return DW_AT_GNU_call_site_value;
1640 break;
1642 case DW_AT_call_data_value:
1643 if (dwarf_version < 5)
1644 return DW_AT_GNU_call_site_data_value;
1645 break;
1647 case DW_AT_call_all_calls:
1648 if (dwarf_version < 5)
1649 return DW_AT_GNU_all_call_sites;
1650 break;
1652 case DW_AT_call_all_tail_calls:
1653 if (dwarf_version < 5)
1654 return DW_AT_GNU_all_tail_call_sites;
1655 break;
1657 case DW_AT_dwo_name:
1658 if (dwarf_version < 5)
1659 return DW_AT_GNU_dwo_name;
1660 break;
1662 default:
1663 break;
1665 return at;
1668 /* And similarly for tags. */
1669 static inline enum dwarf_tag
1670 dwarf_TAG (enum dwarf_tag tag)
1672 switch (tag)
1674 case DW_TAG_call_site:
1675 if (dwarf_version < 5)
1676 return DW_TAG_GNU_call_site;
1677 break;
1679 case DW_TAG_call_site_parameter:
1680 if (dwarf_version < 5)
1681 return DW_TAG_GNU_call_site_parameter;
1682 break;
1684 default:
1685 break;
1687 return tag;
1690 static unsigned long int get_base_type_offset (dw_die_ref);
1692 /* Return the size of a location descriptor. */
1694 static unsigned long
1695 size_of_loc_descr (dw_loc_descr_ref loc)
1697 unsigned long size = 1;
1699 switch (loc->dw_loc_opc)
1701 case DW_OP_addr:
1702 size += DWARF2_ADDR_SIZE;
1703 break;
1704 case DW_OP_GNU_addr_index:
1705 case DW_OP_GNU_const_index:
1706 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1707 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1708 break;
1709 case DW_OP_const1u:
1710 case DW_OP_const1s:
1711 size += 1;
1712 break;
1713 case DW_OP_const2u:
1714 case DW_OP_const2s:
1715 size += 2;
1716 break;
1717 case DW_OP_const4u:
1718 case DW_OP_const4s:
1719 size += 4;
1720 break;
1721 case DW_OP_const8u:
1722 case DW_OP_const8s:
1723 size += 8;
1724 break;
1725 case DW_OP_constu:
1726 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1727 break;
1728 case DW_OP_consts:
1729 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1730 break;
1731 case DW_OP_pick:
1732 size += 1;
1733 break;
1734 case DW_OP_plus_uconst:
1735 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1736 break;
1737 case DW_OP_skip:
1738 case DW_OP_bra:
1739 size += 2;
1740 break;
1741 case DW_OP_breg0:
1742 case DW_OP_breg1:
1743 case DW_OP_breg2:
1744 case DW_OP_breg3:
1745 case DW_OP_breg4:
1746 case DW_OP_breg5:
1747 case DW_OP_breg6:
1748 case DW_OP_breg7:
1749 case DW_OP_breg8:
1750 case DW_OP_breg9:
1751 case DW_OP_breg10:
1752 case DW_OP_breg11:
1753 case DW_OP_breg12:
1754 case DW_OP_breg13:
1755 case DW_OP_breg14:
1756 case DW_OP_breg15:
1757 case DW_OP_breg16:
1758 case DW_OP_breg17:
1759 case DW_OP_breg18:
1760 case DW_OP_breg19:
1761 case DW_OP_breg20:
1762 case DW_OP_breg21:
1763 case DW_OP_breg22:
1764 case DW_OP_breg23:
1765 case DW_OP_breg24:
1766 case DW_OP_breg25:
1767 case DW_OP_breg26:
1768 case DW_OP_breg27:
1769 case DW_OP_breg28:
1770 case DW_OP_breg29:
1771 case DW_OP_breg30:
1772 case DW_OP_breg31:
1773 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1774 break;
1775 case DW_OP_regx:
1776 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1777 break;
1778 case DW_OP_fbreg:
1779 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1780 break;
1781 case DW_OP_bregx:
1782 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1784 break;
1785 case DW_OP_piece:
1786 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787 break;
1788 case DW_OP_bit_piece:
1789 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1791 break;
1792 case DW_OP_deref_size:
1793 case DW_OP_xderef_size:
1794 size += 1;
1795 break;
1796 case DW_OP_call2:
1797 size += 2;
1798 break;
1799 case DW_OP_call4:
1800 size += 4;
1801 break;
1802 case DW_OP_call_ref:
1803 case DW_OP_GNU_variable_value:
1804 size += DWARF_REF_SIZE;
1805 break;
1806 case DW_OP_implicit_value:
1807 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1808 + loc->dw_loc_oprnd1.v.val_unsigned;
1809 break;
1810 case DW_OP_implicit_pointer:
1811 case DW_OP_GNU_implicit_pointer:
1812 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1813 break;
1814 case DW_OP_entry_value:
1815 case DW_OP_GNU_entry_value:
1817 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1818 size += size_of_uleb128 (op_size) + op_size;
1819 break;
1821 case DW_OP_const_type:
1822 case DW_OP_GNU_const_type:
1824 unsigned long o
1825 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1826 size += size_of_uleb128 (o) + 1;
1827 switch (loc->dw_loc_oprnd2.val_class)
1829 case dw_val_class_vec:
1830 size += loc->dw_loc_oprnd2.v.val_vec.length
1831 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1832 break;
1833 case dw_val_class_const:
1834 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1835 break;
1836 case dw_val_class_const_double:
1837 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1838 break;
1839 case dw_val_class_wide_int:
1840 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1841 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1842 break;
1843 default:
1844 gcc_unreachable ();
1846 break;
1848 case DW_OP_regval_type:
1849 case DW_OP_GNU_regval_type:
1851 unsigned long o
1852 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1853 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1854 + size_of_uleb128 (o);
1856 break;
1857 case DW_OP_deref_type:
1858 case DW_OP_GNU_deref_type:
1860 unsigned long o
1861 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1862 size += 1 + size_of_uleb128 (o);
1864 break;
1865 case DW_OP_convert:
1866 case DW_OP_reinterpret:
1867 case DW_OP_GNU_convert:
1868 case DW_OP_GNU_reinterpret:
1869 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871 else
1873 unsigned long o
1874 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1875 size += size_of_uleb128 (o);
1877 break;
1878 case DW_OP_GNU_parameter_ref:
1879 size += 4;
1880 break;
1881 default:
1882 break;
1885 return size;
1888 /* Return the size of a series of location descriptors. */
1890 unsigned long
1891 size_of_locs (dw_loc_descr_ref loc)
1893 dw_loc_descr_ref l;
1894 unsigned long size;
1896 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1897 field, to avoid writing to a PCH file. */
1898 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1900 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1901 break;
1902 size += size_of_loc_descr (l);
1904 if (! l)
1905 return size;
1907 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1909 l->dw_loc_addr = size;
1910 size += size_of_loc_descr (l);
1913 return size;
1916 /* Return the size of the value in a DW_AT_discr_value attribute. */
1918 static int
1919 size_of_discr_value (dw_discr_value *discr_value)
1921 if (discr_value->pos)
1922 return size_of_uleb128 (discr_value->v.uval);
1923 else
1924 return size_of_sleb128 (discr_value->v.sval);
1927 /* Return the size of the value in a DW_AT_discr_list attribute. */
1929 static int
1930 size_of_discr_list (dw_discr_list_ref discr_list)
1932 int size = 0;
1934 for (dw_discr_list_ref list = discr_list;
1935 list != NULL;
1936 list = list->dw_discr_next)
1938 /* One byte for the discriminant value descriptor, and then one or two
1939 LEB128 numbers, depending on whether it's a single case label or a
1940 range label. */
1941 size += 1;
1942 size += size_of_discr_value (&list->dw_discr_lower_bound);
1943 if (list->dw_discr_range != 0)
1944 size += size_of_discr_value (&list->dw_discr_upper_bound);
1946 return size;
1949 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1950 static void get_ref_die_offset_label (char *, dw_die_ref);
1951 static unsigned long int get_ref_die_offset (dw_die_ref);
1953 /* Output location description stack opcode's operands (if any).
1954 The for_eh_or_skip parameter controls whether register numbers are
1955 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1956 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1957 info). This should be suppressed for the cases that have not been converted
1958 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1960 static void
1961 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1963 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1964 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1966 switch (loc->dw_loc_opc)
1968 #ifdef DWARF2_DEBUGGING_INFO
1969 case DW_OP_const2u:
1970 case DW_OP_const2s:
1971 dw2_asm_output_data (2, val1->v.val_int, NULL);
1972 break;
1973 case DW_OP_const4u:
1974 if (loc->dtprel)
1976 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1980 break;
1982 /* FALLTHRU */
1983 case DW_OP_const4s:
1984 dw2_asm_output_data (4, val1->v.val_int, NULL);
1985 break;
1986 case DW_OP_const8u:
1987 if (loc->dtprel)
1989 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1990 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1991 val1->v.val_addr);
1992 fputc ('\n', asm_out_file);
1993 break;
1995 /* FALLTHRU */
1996 case DW_OP_const8s:
1997 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1998 dw2_asm_output_data (8, val1->v.val_int, NULL);
1999 break;
2000 case DW_OP_skip:
2001 case DW_OP_bra:
2003 int offset;
2005 gcc_assert (val1->val_class == dw_val_class_loc);
2006 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2008 dw2_asm_output_data (2, offset, NULL);
2010 break;
2011 case DW_OP_implicit_value:
2012 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2013 switch (val2->val_class)
2015 case dw_val_class_const:
2016 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2017 break;
2018 case dw_val_class_vec:
2020 unsigned int elt_size = val2->v.val_vec.elt_size;
2021 unsigned int len = val2->v.val_vec.length;
2022 unsigned int i;
2023 unsigned char *p;
2025 if (elt_size > sizeof (HOST_WIDE_INT))
2027 elt_size /= 2;
2028 len *= 2;
2030 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2031 i < len;
2032 i++, p += elt_size)
2033 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2034 "fp or vector constant word %u", i);
2036 break;
2037 case dw_val_class_const_double:
2039 unsigned HOST_WIDE_INT first, second;
2041 if (WORDS_BIG_ENDIAN)
2043 first = val2->v.val_double.high;
2044 second = val2->v.val_double.low;
2046 else
2048 first = val2->v.val_double.low;
2049 second = val2->v.val_double.high;
2051 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2052 first, NULL);
2053 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2054 second, NULL);
2056 break;
2057 case dw_val_class_wide_int:
2059 int i;
2060 int len = get_full_len (*val2->v.val_wide);
2061 if (WORDS_BIG_ENDIAN)
2062 for (i = len - 1; i >= 0; --i)
2063 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2064 val2->v.val_wide->elt (i), NULL);
2065 else
2066 for (i = 0; i < len; ++i)
2067 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2068 val2->v.val_wide->elt (i), NULL);
2070 break;
2071 case dw_val_class_addr:
2072 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2073 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2074 break;
2075 default:
2076 gcc_unreachable ();
2078 break;
2079 #else
2080 case DW_OP_const2u:
2081 case DW_OP_const2s:
2082 case DW_OP_const4u:
2083 case DW_OP_const4s:
2084 case DW_OP_const8u:
2085 case DW_OP_const8s:
2086 case DW_OP_skip:
2087 case DW_OP_bra:
2088 case DW_OP_implicit_value:
2089 /* We currently don't make any attempt to make sure these are
2090 aligned properly like we do for the main unwind info, so
2091 don't support emitting things larger than a byte if we're
2092 only doing unwinding. */
2093 gcc_unreachable ();
2094 #endif
2095 case DW_OP_const1u:
2096 case DW_OP_const1s:
2097 dw2_asm_output_data (1, val1->v.val_int, NULL);
2098 break;
2099 case DW_OP_constu:
2100 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2101 break;
2102 case DW_OP_consts:
2103 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2104 break;
2105 case DW_OP_pick:
2106 dw2_asm_output_data (1, val1->v.val_int, NULL);
2107 break;
2108 case DW_OP_plus_uconst:
2109 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2110 break;
2111 case DW_OP_breg0:
2112 case DW_OP_breg1:
2113 case DW_OP_breg2:
2114 case DW_OP_breg3:
2115 case DW_OP_breg4:
2116 case DW_OP_breg5:
2117 case DW_OP_breg6:
2118 case DW_OP_breg7:
2119 case DW_OP_breg8:
2120 case DW_OP_breg9:
2121 case DW_OP_breg10:
2122 case DW_OP_breg11:
2123 case DW_OP_breg12:
2124 case DW_OP_breg13:
2125 case DW_OP_breg14:
2126 case DW_OP_breg15:
2127 case DW_OP_breg16:
2128 case DW_OP_breg17:
2129 case DW_OP_breg18:
2130 case DW_OP_breg19:
2131 case DW_OP_breg20:
2132 case DW_OP_breg21:
2133 case DW_OP_breg22:
2134 case DW_OP_breg23:
2135 case DW_OP_breg24:
2136 case DW_OP_breg25:
2137 case DW_OP_breg26:
2138 case DW_OP_breg27:
2139 case DW_OP_breg28:
2140 case DW_OP_breg29:
2141 case DW_OP_breg30:
2142 case DW_OP_breg31:
2143 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2144 break;
2145 case DW_OP_regx:
2147 unsigned r = val1->v.val_unsigned;
2148 if (for_eh_or_skip >= 0)
2149 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2150 gcc_assert (size_of_uleb128 (r)
2151 == size_of_uleb128 (val1->v.val_unsigned));
2152 dw2_asm_output_data_uleb128 (r, NULL);
2154 break;
2155 case DW_OP_fbreg:
2156 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2157 break;
2158 case DW_OP_bregx:
2160 unsigned r = val1->v.val_unsigned;
2161 if (for_eh_or_skip >= 0)
2162 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2163 gcc_assert (size_of_uleb128 (r)
2164 == size_of_uleb128 (val1->v.val_unsigned));
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2168 break;
2169 case DW_OP_piece:
2170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2171 break;
2172 case DW_OP_bit_piece:
2173 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2174 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2175 break;
2176 case DW_OP_deref_size:
2177 case DW_OP_xderef_size:
2178 dw2_asm_output_data (1, val1->v.val_int, NULL);
2179 break;
2181 case DW_OP_addr:
2182 if (loc->dtprel)
2184 if (targetm.asm_out.output_dwarf_dtprel)
2186 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2187 DWARF2_ADDR_SIZE,
2188 val1->v.val_addr);
2189 fputc ('\n', asm_out_file);
2191 else
2192 gcc_unreachable ();
2194 else
2196 #ifdef DWARF2_DEBUGGING_INFO
2197 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2198 #else
2199 gcc_unreachable ();
2200 #endif
2202 break;
2204 case DW_OP_GNU_addr_index:
2205 case DW_OP_GNU_const_index:
2206 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2207 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2208 "(index into .debug_addr)");
2209 break;
2211 case DW_OP_call2:
2212 case DW_OP_call4:
2214 unsigned long die_offset
2215 = get_ref_die_offset (val1->v.val_die_ref.die);
2216 /* Make sure the offset has been computed and that we can encode it as
2217 an operand. */
2218 gcc_assert (die_offset > 0
2219 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2220 ? 0xffff
2221 : 0xffffffff));
2222 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2223 die_offset, NULL);
2225 break;
2227 case DW_OP_call_ref:
2228 case DW_OP_GNU_variable_value:
2230 char label[MAX_ARTIFICIAL_LABEL_BYTES
2231 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2232 gcc_assert (val1->val_class == dw_val_class_die_ref);
2233 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2234 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2236 break;
2238 case DW_OP_implicit_pointer:
2239 case DW_OP_GNU_implicit_pointer:
2241 char label[MAX_ARTIFICIAL_LABEL_BYTES
2242 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2243 gcc_assert (val1->val_class == dw_val_class_die_ref);
2244 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2245 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2246 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2248 break;
2250 case DW_OP_entry_value:
2251 case DW_OP_GNU_entry_value:
2252 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2253 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2254 break;
2256 case DW_OP_const_type:
2257 case DW_OP_GNU_const_type:
2259 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2260 gcc_assert (o);
2261 dw2_asm_output_data_uleb128 (o, NULL);
2262 switch (val2->val_class)
2264 case dw_val_class_const:
2265 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2266 dw2_asm_output_data (1, l, NULL);
2267 dw2_asm_output_data (l, val2->v.val_int, NULL);
2268 break;
2269 case dw_val_class_vec:
2271 unsigned int elt_size = val2->v.val_vec.elt_size;
2272 unsigned int len = val2->v.val_vec.length;
2273 unsigned int i;
2274 unsigned char *p;
2276 l = len * elt_size;
2277 dw2_asm_output_data (1, l, NULL);
2278 if (elt_size > sizeof (HOST_WIDE_INT))
2280 elt_size /= 2;
2281 len *= 2;
2283 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2284 i < len;
2285 i++, p += elt_size)
2286 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2287 "fp or vector constant word %u", i);
2289 break;
2290 case dw_val_class_const_double:
2292 unsigned HOST_WIDE_INT first, second;
2293 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2295 dw2_asm_output_data (1, 2 * l, NULL);
2296 if (WORDS_BIG_ENDIAN)
2298 first = val2->v.val_double.high;
2299 second = val2->v.val_double.low;
2301 else
2303 first = val2->v.val_double.low;
2304 second = val2->v.val_double.high;
2306 dw2_asm_output_data (l, first, NULL);
2307 dw2_asm_output_data (l, second, NULL);
2309 break;
2310 case dw_val_class_wide_int:
2312 int i;
2313 int len = get_full_len (*val2->v.val_wide);
2314 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2316 dw2_asm_output_data (1, len * l, NULL);
2317 if (WORDS_BIG_ENDIAN)
2318 for (i = len - 1; i >= 0; --i)
2319 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2320 else
2321 for (i = 0; i < len; ++i)
2322 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2324 break;
2325 default:
2326 gcc_unreachable ();
2329 break;
2330 case DW_OP_regval_type:
2331 case DW_OP_GNU_regval_type:
2333 unsigned r = val1->v.val_unsigned;
2334 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2335 gcc_assert (o);
2336 if (for_eh_or_skip >= 0)
2338 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2339 gcc_assert (size_of_uleb128 (r)
2340 == size_of_uleb128 (val1->v.val_unsigned));
2342 dw2_asm_output_data_uleb128 (r, NULL);
2343 dw2_asm_output_data_uleb128 (o, NULL);
2345 break;
2346 case DW_OP_deref_type:
2347 case DW_OP_GNU_deref_type:
2349 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2350 gcc_assert (o);
2351 dw2_asm_output_data (1, val1->v.val_int, NULL);
2352 dw2_asm_output_data_uleb128 (o, NULL);
2354 break;
2355 case DW_OP_convert:
2356 case DW_OP_reinterpret:
2357 case DW_OP_GNU_convert:
2358 case DW_OP_GNU_reinterpret:
2359 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2360 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2361 else
2363 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2364 gcc_assert (o);
2365 dw2_asm_output_data_uleb128 (o, NULL);
2367 break;
2369 case DW_OP_GNU_parameter_ref:
2371 unsigned long o;
2372 gcc_assert (val1->val_class == dw_val_class_die_ref);
2373 o = get_ref_die_offset (val1->v.val_die_ref.die);
2374 dw2_asm_output_data (4, o, NULL);
2376 break;
2378 default:
2379 /* Other codes have no operands. */
2380 break;
2384 /* Output a sequence of location operations.
2385 The for_eh_or_skip parameter controls whether register numbers are
2386 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2387 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2388 info). This should be suppressed for the cases that have not been converted
2389 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2391 void
2392 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2394 for (; loc != NULL; loc = loc->dw_loc_next)
2396 enum dwarf_location_atom opc = loc->dw_loc_opc;
2397 /* Output the opcode. */
2398 if (for_eh_or_skip >= 0
2399 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2401 unsigned r = (opc - DW_OP_breg0);
2402 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2403 gcc_assert (r <= 31);
2404 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2406 else if (for_eh_or_skip >= 0
2407 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2409 unsigned r = (opc - DW_OP_reg0);
2410 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2411 gcc_assert (r <= 31);
2412 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2415 dw2_asm_output_data (1, opc,
2416 "%s", dwarf_stack_op_name (opc));
2418 /* Output the operand(s) (if any). */
2419 output_loc_operands (loc, for_eh_or_skip);
2423 /* Output location description stack opcode's operands (if any).
2424 The output is single bytes on a line, suitable for .cfi_escape. */
2426 static void
2427 output_loc_operands_raw (dw_loc_descr_ref loc)
2429 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2430 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2432 switch (loc->dw_loc_opc)
2434 case DW_OP_addr:
2435 case DW_OP_GNU_addr_index:
2436 case DW_OP_GNU_const_index:
2437 case DW_OP_implicit_value:
2438 /* We cannot output addresses in .cfi_escape, only bytes. */
2439 gcc_unreachable ();
2441 case DW_OP_const1u:
2442 case DW_OP_const1s:
2443 case DW_OP_pick:
2444 case DW_OP_deref_size:
2445 case DW_OP_xderef_size:
2446 fputc (',', asm_out_file);
2447 dw2_asm_output_data_raw (1, val1->v.val_int);
2448 break;
2450 case DW_OP_const2u:
2451 case DW_OP_const2s:
2452 fputc (',', asm_out_file);
2453 dw2_asm_output_data_raw (2, val1->v.val_int);
2454 break;
2456 case DW_OP_const4u:
2457 case DW_OP_const4s:
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (4, val1->v.val_int);
2460 break;
2462 case DW_OP_const8u:
2463 case DW_OP_const8s:
2464 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2465 fputc (',', asm_out_file);
2466 dw2_asm_output_data_raw (8, val1->v.val_int);
2467 break;
2469 case DW_OP_skip:
2470 case DW_OP_bra:
2472 int offset;
2474 gcc_assert (val1->val_class == dw_val_class_loc);
2475 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2477 fputc (',', asm_out_file);
2478 dw2_asm_output_data_raw (2, offset);
2480 break;
2482 case DW_OP_regx:
2484 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2485 gcc_assert (size_of_uleb128 (r)
2486 == size_of_uleb128 (val1->v.val_unsigned));
2487 fputc (',', asm_out_file);
2488 dw2_asm_output_data_uleb128_raw (r);
2490 break;
2492 case DW_OP_constu:
2493 case DW_OP_plus_uconst:
2494 case DW_OP_piece:
2495 fputc (',', asm_out_file);
2496 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2497 break;
2499 case DW_OP_bit_piece:
2500 fputc (',', asm_out_file);
2501 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2502 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2503 break;
2505 case DW_OP_consts:
2506 case DW_OP_breg0:
2507 case DW_OP_breg1:
2508 case DW_OP_breg2:
2509 case DW_OP_breg3:
2510 case DW_OP_breg4:
2511 case DW_OP_breg5:
2512 case DW_OP_breg6:
2513 case DW_OP_breg7:
2514 case DW_OP_breg8:
2515 case DW_OP_breg9:
2516 case DW_OP_breg10:
2517 case DW_OP_breg11:
2518 case DW_OP_breg12:
2519 case DW_OP_breg13:
2520 case DW_OP_breg14:
2521 case DW_OP_breg15:
2522 case DW_OP_breg16:
2523 case DW_OP_breg17:
2524 case DW_OP_breg18:
2525 case DW_OP_breg19:
2526 case DW_OP_breg20:
2527 case DW_OP_breg21:
2528 case DW_OP_breg22:
2529 case DW_OP_breg23:
2530 case DW_OP_breg24:
2531 case DW_OP_breg25:
2532 case DW_OP_breg26:
2533 case DW_OP_breg27:
2534 case DW_OP_breg28:
2535 case DW_OP_breg29:
2536 case DW_OP_breg30:
2537 case DW_OP_breg31:
2538 case DW_OP_fbreg:
2539 fputc (',', asm_out_file);
2540 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2541 break;
2543 case DW_OP_bregx:
2545 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2546 gcc_assert (size_of_uleb128 (r)
2547 == size_of_uleb128 (val1->v.val_unsigned));
2548 fputc (',', asm_out_file);
2549 dw2_asm_output_data_uleb128_raw (r);
2550 fputc (',', asm_out_file);
2551 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2553 break;
2555 case DW_OP_implicit_pointer:
2556 case DW_OP_entry_value:
2557 case DW_OP_const_type:
2558 case DW_OP_regval_type:
2559 case DW_OP_deref_type:
2560 case DW_OP_convert:
2561 case DW_OP_reinterpret:
2562 case DW_OP_GNU_implicit_pointer:
2563 case DW_OP_GNU_entry_value:
2564 case DW_OP_GNU_const_type:
2565 case DW_OP_GNU_regval_type:
2566 case DW_OP_GNU_deref_type:
2567 case DW_OP_GNU_convert:
2568 case DW_OP_GNU_reinterpret:
2569 case DW_OP_GNU_parameter_ref:
2570 gcc_unreachable ();
2571 break;
2573 default:
2574 /* Other codes have no operands. */
2575 break;
2579 void
2580 output_loc_sequence_raw (dw_loc_descr_ref loc)
2582 while (1)
2584 enum dwarf_location_atom opc = loc->dw_loc_opc;
2585 /* Output the opcode. */
2586 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2588 unsigned r = (opc - DW_OP_breg0);
2589 r = DWARF2_FRAME_REG_OUT (r, 1);
2590 gcc_assert (r <= 31);
2591 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2593 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2595 unsigned r = (opc - DW_OP_reg0);
2596 r = DWARF2_FRAME_REG_OUT (r, 1);
2597 gcc_assert (r <= 31);
2598 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2600 /* Output the opcode. */
2601 fprintf (asm_out_file, "%#x", opc);
2602 output_loc_operands_raw (loc);
2604 if (!loc->dw_loc_next)
2605 break;
2606 loc = loc->dw_loc_next;
2608 fputc (',', asm_out_file);
2612 /* This function builds a dwarf location descriptor sequence from a
2613 dw_cfa_location, adding the given OFFSET to the result of the
2614 expression. */
2616 struct dw_loc_descr_node *
2617 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2619 struct dw_loc_descr_node *head, *tmp;
2621 offset += cfa->offset;
2623 if (cfa->indirect)
2625 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2626 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2627 head->dw_loc_oprnd1.val_entry = NULL;
2628 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2629 add_loc_descr (&head, tmp);
2630 if (offset != 0)
2632 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2633 add_loc_descr (&head, tmp);
2636 else
2637 head = new_reg_loc_descr (cfa->reg, offset);
2639 return head;
2642 /* This function builds a dwarf location descriptor sequence for
2643 the address at OFFSET from the CFA when stack is aligned to
2644 ALIGNMENT byte. */
2646 struct dw_loc_descr_node *
2647 build_cfa_aligned_loc (dw_cfa_location *cfa,
2648 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2650 struct dw_loc_descr_node *head;
2651 unsigned int dwarf_fp
2652 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2654 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2655 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2657 head = new_reg_loc_descr (dwarf_fp, 0);
2658 add_loc_descr (&head, int_loc_descriptor (alignment));
2659 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2660 loc_descr_plus_const (&head, offset);
2662 else
2663 head = new_reg_loc_descr (dwarf_fp, offset);
2664 return head;
2667 /* And now, the support for symbolic debugging information. */
2669 /* .debug_str support. */
2671 static void dwarf2out_init (const char *);
2672 static void dwarf2out_finish (const char *);
2673 static void dwarf2out_early_finish (const char *);
2674 static void dwarf2out_assembly_start (void);
2675 static void dwarf2out_define (unsigned int, const char *);
2676 static void dwarf2out_undef (unsigned int, const char *);
2677 static void dwarf2out_start_source_file (unsigned, const char *);
2678 static void dwarf2out_end_source_file (unsigned);
2679 static void dwarf2out_function_decl (tree);
2680 static void dwarf2out_begin_block (unsigned, unsigned);
2681 static void dwarf2out_end_block (unsigned, unsigned);
2682 static bool dwarf2out_ignore_block (const_tree);
2683 static void dwarf2out_early_global_decl (tree);
2684 static void dwarf2out_late_global_decl (tree);
2685 static void dwarf2out_type_decl (tree, int);
2686 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2687 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2688 dw_die_ref);
2689 static void dwarf2out_abstract_function (tree);
2690 static void dwarf2out_var_location (rtx_insn *);
2691 static void dwarf2out_size_function (tree);
2692 static void dwarf2out_begin_function (tree);
2693 static void dwarf2out_end_function (unsigned int);
2694 static void dwarf2out_register_main_translation_unit (tree unit);
2695 static void dwarf2out_set_name (tree, tree);
2696 static void dwarf2out_register_external_die (tree decl, const char *sym,
2697 unsigned HOST_WIDE_INT off);
2698 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2699 unsigned HOST_WIDE_INT *off);
2701 /* The debug hooks structure. */
2703 const struct gcc_debug_hooks dwarf2_debug_hooks =
2705 dwarf2out_init,
2706 dwarf2out_finish,
2707 dwarf2out_early_finish,
2708 dwarf2out_assembly_start,
2709 dwarf2out_define,
2710 dwarf2out_undef,
2711 dwarf2out_start_source_file,
2712 dwarf2out_end_source_file,
2713 dwarf2out_begin_block,
2714 dwarf2out_end_block,
2715 dwarf2out_ignore_block,
2716 dwarf2out_source_line,
2717 dwarf2out_begin_prologue,
2718 #if VMS_DEBUGGING_INFO
2719 dwarf2out_vms_end_prologue,
2720 dwarf2out_vms_begin_epilogue,
2721 #else
2722 debug_nothing_int_charstar,
2723 debug_nothing_int_charstar,
2724 #endif
2725 dwarf2out_end_epilogue,
2726 dwarf2out_begin_function,
2727 dwarf2out_end_function, /* end_function */
2728 dwarf2out_register_main_translation_unit,
2729 dwarf2out_function_decl, /* function_decl */
2730 dwarf2out_early_global_decl,
2731 dwarf2out_late_global_decl,
2732 dwarf2out_type_decl, /* type_decl */
2733 dwarf2out_imported_module_or_decl,
2734 dwarf2out_die_ref_for_decl,
2735 dwarf2out_register_external_die,
2736 debug_nothing_tree, /* deferred_inline_function */
2737 /* The DWARF 2 backend tries to reduce debugging bloat by not
2738 emitting the abstract description of inline functions until
2739 something tries to reference them. */
2740 dwarf2out_abstract_function, /* outlining_inline_function */
2741 debug_nothing_rtx_code_label, /* label */
2742 debug_nothing_int, /* handle_pch */
2743 dwarf2out_var_location,
2744 dwarf2out_size_function, /* size_function */
2745 dwarf2out_switch_text_section,
2746 dwarf2out_set_name,
2747 1, /* start_end_main_source_file */
2748 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2751 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2753 dwarf2out_init,
2754 debug_nothing_charstar,
2755 debug_nothing_charstar,
2756 dwarf2out_assembly_start,
2757 debug_nothing_int_charstar,
2758 debug_nothing_int_charstar,
2759 debug_nothing_int_charstar,
2760 debug_nothing_int,
2761 debug_nothing_int_int, /* begin_block */
2762 debug_nothing_int_int, /* end_block */
2763 debug_true_const_tree, /* ignore_block */
2764 dwarf2out_source_line, /* source_line */
2765 debug_nothing_int_int_charstar, /* begin_prologue */
2766 debug_nothing_int_charstar, /* end_prologue */
2767 debug_nothing_int_charstar, /* begin_epilogue */
2768 debug_nothing_int_charstar, /* end_epilogue */
2769 debug_nothing_tree, /* begin_function */
2770 debug_nothing_int, /* end_function */
2771 debug_nothing_tree, /* register_main_translation_unit */
2772 debug_nothing_tree, /* function_decl */
2773 debug_nothing_tree, /* early_global_decl */
2774 debug_nothing_tree, /* late_global_decl */
2775 debug_nothing_tree_int, /* type_decl */
2776 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2777 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2778 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2779 debug_nothing_tree, /* deferred_inline_function */
2780 debug_nothing_tree, /* outlining_inline_function */
2781 debug_nothing_rtx_code_label, /* label */
2782 debug_nothing_int, /* handle_pch */
2783 debug_nothing_rtx_insn, /* var_location */
2784 debug_nothing_tree, /* size_function */
2785 debug_nothing_void, /* switch_text_section */
2786 debug_nothing_tree_tree, /* set_name */
2787 0, /* start_end_main_source_file */
2788 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2791 /* NOTE: In the comments in this file, many references are made to
2792 "Debugging Information Entries". This term is abbreviated as `DIE'
2793 throughout the remainder of this file. */
2795 /* An internal representation of the DWARF output is built, and then
2796 walked to generate the DWARF debugging info. The walk of the internal
2797 representation is done after the entire program has been compiled.
2798 The types below are used to describe the internal representation. */
2800 /* Whether to put type DIEs into their own section .debug_types instead
2801 of making them part of the .debug_info section. Only supported for
2802 Dwarf V4 or higher and the user didn't disable them through
2803 -fno-debug-types-section. It is more efficient to put them in a
2804 separate comdat sections since the linker will then be able to
2805 remove duplicates. But not all tools support .debug_types sections
2806 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2807 it is DW_UT_type unit type in .debug_info section. */
2809 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2811 /* Various DIE's use offsets relative to the beginning of the
2812 .debug_info section to refer to each other. */
2814 typedef long int dw_offset;
2816 struct comdat_type_node;
2818 /* The entries in the line_info table more-or-less mirror the opcodes
2819 that are used in the real dwarf line table. Arrays of these entries
2820 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2821 supported. */
2823 enum dw_line_info_opcode {
2824 /* Emit DW_LNE_set_address; the operand is the label index. */
2825 LI_set_address,
2827 /* Emit a row to the matrix with the given line. This may be done
2828 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2829 special opcodes. */
2830 LI_set_line,
2832 /* Emit a DW_LNS_set_file. */
2833 LI_set_file,
2835 /* Emit a DW_LNS_set_column. */
2836 LI_set_column,
2838 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2839 LI_negate_stmt,
2841 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2842 LI_set_prologue_end,
2843 LI_set_epilogue_begin,
2845 /* Emit a DW_LNE_set_discriminator. */
2846 LI_set_discriminator
2849 typedef struct GTY(()) dw_line_info_struct {
2850 enum dw_line_info_opcode opcode;
2851 unsigned int val;
2852 } dw_line_info_entry;
2855 struct GTY(()) dw_line_info_table {
2856 /* The label that marks the end of this section. */
2857 const char *end_label;
2859 /* The values for the last row of the matrix, as collected in the table.
2860 These are used to minimize the changes to the next row. */
2861 unsigned int file_num;
2862 unsigned int line_num;
2863 unsigned int column_num;
2864 int discrim_num;
2865 bool is_stmt;
2866 bool in_use;
2868 vec<dw_line_info_entry, va_gc> *entries;
2872 /* Each DIE attribute has a field specifying the attribute kind,
2873 a link to the next attribute in the chain, and an attribute value.
2874 Attributes are typically linked below the DIE they modify. */
2876 typedef struct GTY(()) dw_attr_struct {
2877 enum dwarf_attribute dw_attr;
2878 dw_val_node dw_attr_val;
2880 dw_attr_node;
2883 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2884 The children of each node form a circular list linked by
2885 die_sib. die_child points to the node *before* the "first" child node. */
2887 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2888 union die_symbol_or_type_node
2890 const char * GTY ((tag ("0"))) die_symbol;
2891 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2893 GTY ((desc ("%0.comdat_type_p"))) die_id;
2894 vec<dw_attr_node, va_gc> *die_attr;
2895 dw_die_ref die_parent;
2896 dw_die_ref die_child;
2897 dw_die_ref die_sib;
2898 dw_die_ref die_definition; /* ref from a specification to its definition */
2899 dw_offset die_offset;
2900 unsigned long die_abbrev;
2901 int die_mark;
2902 unsigned int decl_id;
2903 enum dwarf_tag die_tag;
2904 /* Die is used and must not be pruned as unused. */
2905 BOOL_BITFIELD die_perennial_p : 1;
2906 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2907 /* For an external ref to die_symbol if die_offset contains an extra
2908 offset to that symbol. */
2909 BOOL_BITFIELD with_offset : 1;
2910 /* Whether this DIE was removed from the DIE tree, for example via
2911 prune_unused_types. We don't consider those present from the
2912 DIE lookup routines. */
2913 BOOL_BITFIELD removed : 1;
2914 /* Lots of spare bits. */
2916 die_node;
2918 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2919 static bool early_dwarf;
2920 static bool early_dwarf_finished;
2921 struct set_early_dwarf {
2922 bool saved;
2923 set_early_dwarf () : saved(early_dwarf)
2925 gcc_assert (! early_dwarf_finished);
2926 early_dwarf = true;
2928 ~set_early_dwarf () { early_dwarf = saved; }
2931 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2932 #define FOR_EACH_CHILD(die, c, expr) do { \
2933 c = die->die_child; \
2934 if (c) do { \
2935 c = c->die_sib; \
2936 expr; \
2937 } while (c != die->die_child); \
2938 } while (0)
2940 /* The pubname structure */
2942 typedef struct GTY(()) pubname_struct {
2943 dw_die_ref die;
2944 const char *name;
2946 pubname_entry;
2949 struct GTY(()) dw_ranges {
2950 const char *label;
2951 /* If this is positive, it's a block number, otherwise it's a
2952 bitwise-negated index into dw_ranges_by_label. */
2953 int num;
2954 /* Index for the range list for DW_FORM_rnglistx. */
2955 unsigned int idx : 31;
2956 /* True if this range might be possibly in a different section
2957 from previous entry. */
2958 unsigned int maybe_new_sec : 1;
2961 /* A structure to hold a macinfo entry. */
2963 typedef struct GTY(()) macinfo_struct {
2964 unsigned char code;
2965 unsigned HOST_WIDE_INT lineno;
2966 const char *info;
2968 macinfo_entry;
2971 struct GTY(()) dw_ranges_by_label {
2972 const char *begin;
2973 const char *end;
2976 /* The comdat type node structure. */
2977 struct GTY(()) comdat_type_node
2979 dw_die_ref root_die;
2980 dw_die_ref type_die;
2981 dw_die_ref skeleton_die;
2982 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2983 comdat_type_node *next;
2986 /* A list of DIEs for which we can't determine ancestry (parent_die
2987 field) just yet. Later in dwarf2out_finish we will fill in the
2988 missing bits. */
2989 typedef struct GTY(()) limbo_die_struct {
2990 dw_die_ref die;
2991 /* The tree for which this DIE was created. We use this to
2992 determine ancestry later. */
2993 tree created_for;
2994 struct limbo_die_struct *next;
2996 limbo_die_node;
2998 typedef struct skeleton_chain_struct
3000 dw_die_ref old_die;
3001 dw_die_ref new_die;
3002 struct skeleton_chain_struct *parent;
3004 skeleton_chain_node;
3006 /* Define a macro which returns nonzero for a TYPE_DECL which was
3007 implicitly generated for a type.
3009 Note that, unlike the C front-end (which generates a NULL named
3010 TYPE_DECL node for each complete tagged type, each array type,
3011 and each function type node created) the C++ front-end generates
3012 a _named_ TYPE_DECL node for each tagged type node created.
3013 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3014 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3015 front-end, but for each type, tagged or not. */
3017 #define TYPE_DECL_IS_STUB(decl) \
3018 (DECL_NAME (decl) == NULL_TREE \
3019 || (DECL_ARTIFICIAL (decl) \
3020 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3021 /* This is necessary for stub decls that \
3022 appear in nested inline functions. */ \
3023 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3024 && (decl_ultimate_origin (decl) \
3025 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3027 /* Information concerning the compilation unit's programming
3028 language, and compiler version. */
3030 /* Fixed size portion of the DWARF compilation unit header. */
3031 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3032 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3033 + (dwarf_version >= 5 ? 4 : 3))
3035 /* Fixed size portion of the DWARF comdat type unit header. */
3036 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3037 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3038 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3040 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3041 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3042 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3044 /* Fixed size portion of public names info. */
3045 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3047 /* Fixed size portion of the address range info. */
3048 #define DWARF_ARANGES_HEADER_SIZE \
3049 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3050 DWARF2_ADDR_SIZE * 2) \
3051 - DWARF_INITIAL_LENGTH_SIZE)
3053 /* Size of padding portion in the address range info. It must be
3054 aligned to twice the pointer size. */
3055 #define DWARF_ARANGES_PAD_SIZE \
3056 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3057 DWARF2_ADDR_SIZE * 2) \
3058 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3060 /* Use assembler line directives if available. */
3061 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3062 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3063 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3064 #else
3065 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3066 #endif
3067 #endif
3069 /* Minimum line offset in a special line info. opcode.
3070 This value was chosen to give a reasonable range of values. */
3071 #define DWARF_LINE_BASE -10
3073 /* First special line opcode - leave room for the standard opcodes. */
3074 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3076 /* Range of line offsets in a special line info. opcode. */
3077 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3079 /* Flag that indicates the initial value of the is_stmt_start flag.
3080 In the present implementation, we do not mark any lines as
3081 the beginning of a source statement, because that information
3082 is not made available by the GCC front-end. */
3083 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3085 /* Maximum number of operations per instruction bundle. */
3086 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3087 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3088 #endif
3090 /* This location is used by calc_die_sizes() to keep track
3091 the offset of each DIE within the .debug_info section. */
3092 static unsigned long next_die_offset;
3094 /* Record the root of the DIE's built for the current compilation unit. */
3095 static GTY(()) dw_die_ref single_comp_unit_die;
3097 /* A list of type DIEs that have been separated into comdat sections. */
3098 static GTY(()) comdat_type_node *comdat_type_list;
3100 /* A list of CU DIEs that have been separated. */
3101 static GTY(()) limbo_die_node *cu_die_list;
3103 /* A list of DIEs with a NULL parent waiting to be relocated. */
3104 static GTY(()) limbo_die_node *limbo_die_list;
3106 /* A list of DIEs for which we may have to generate
3107 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3108 static GTY(()) limbo_die_node *deferred_asm_name;
3110 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3112 typedef const char *compare_type;
3114 static hashval_t hash (dwarf_file_data *);
3115 static bool equal (dwarf_file_data *, const char *);
3118 /* Filenames referenced by this compilation unit. */
3119 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3121 struct decl_die_hasher : ggc_ptr_hash<die_node>
3123 typedef tree compare_type;
3125 static hashval_t hash (die_node *);
3126 static bool equal (die_node *, tree);
3128 /* A hash table of references to DIE's that describe declarations.
3129 The key is a DECL_UID() which is a unique number identifying each decl. */
3130 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3132 struct GTY ((for_user)) variable_value_struct {
3133 unsigned int decl_id;
3134 vec<dw_die_ref, va_gc> *dies;
3137 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3139 typedef tree compare_type;
3141 static hashval_t hash (variable_value_struct *);
3142 static bool equal (variable_value_struct *, tree);
3144 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3145 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3146 DECL_CONTEXT of the referenced VAR_DECLs. */
3147 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3149 struct block_die_hasher : ggc_ptr_hash<die_struct>
3151 static hashval_t hash (die_struct *);
3152 static bool equal (die_struct *, die_struct *);
3155 /* A hash table of references to DIE's that describe COMMON blocks.
3156 The key is DECL_UID() ^ die_parent. */
3157 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3159 typedef struct GTY(()) die_arg_entry_struct {
3160 dw_die_ref die;
3161 tree arg;
3162 } die_arg_entry;
3165 /* Node of the variable location list. */
3166 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3167 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3168 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3169 in mode of the EXPR_LIST node and first EXPR_LIST operand
3170 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3171 location or NULL for padding. For larger bitsizes,
3172 mode is 0 and first operand is a CONCAT with bitsize
3173 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3174 NULL as second operand. */
3175 rtx GTY (()) loc;
3176 const char * GTY (()) label;
3177 struct var_loc_node * GTY (()) next;
3180 /* Variable location list. */
3181 struct GTY ((for_user)) var_loc_list_def {
3182 struct var_loc_node * GTY (()) first;
3184 /* Pointer to the last but one or last element of the
3185 chained list. If the list is empty, both first and
3186 last are NULL, if the list contains just one node
3187 or the last node certainly is not redundant, it points
3188 to the last node, otherwise points to the last but one.
3189 Do not mark it for GC because it is marked through the chain. */
3190 struct var_loc_node * GTY ((skip ("%h"))) last;
3192 /* Pointer to the last element before section switch,
3193 if NULL, either sections weren't switched or first
3194 is after section switch. */
3195 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3197 /* DECL_UID of the variable decl. */
3198 unsigned int decl_id;
3200 typedef struct var_loc_list_def var_loc_list;
3202 /* Call argument location list. */
3203 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3204 rtx GTY (()) call_arg_loc_note;
3205 const char * GTY (()) label;
3206 tree GTY (()) block;
3207 bool tail_call_p;
3208 rtx GTY (()) symbol_ref;
3209 struct call_arg_loc_node * GTY (()) next;
3213 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3215 typedef const_tree compare_type;
3217 static hashval_t hash (var_loc_list *);
3218 static bool equal (var_loc_list *, const_tree);
3221 /* Table of decl location linked lists. */
3222 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3224 /* Head and tail of call_arg_loc chain. */
3225 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3226 static struct call_arg_loc_node *call_arg_loc_last;
3228 /* Number of call sites in the current function. */
3229 static int call_site_count = -1;
3230 /* Number of tail call sites in the current function. */
3231 static int tail_call_site_count = -1;
3233 /* A cached location list. */
3234 struct GTY ((for_user)) cached_dw_loc_list_def {
3235 /* The DECL_UID of the decl that this entry describes. */
3236 unsigned int decl_id;
3238 /* The cached location list. */
3239 dw_loc_list_ref loc_list;
3241 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3243 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3246 typedef const_tree compare_type;
3248 static hashval_t hash (cached_dw_loc_list *);
3249 static bool equal (cached_dw_loc_list *, const_tree);
3252 /* Table of cached location lists. */
3253 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3255 /* A vector of references to DIE's that are uniquely identified by their tag,
3256 presence/absence of children DIE's, and list of attribute/value pairs. */
3257 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3259 /* A hash map to remember the stack usage for DWARF procedures. The value
3260 stored is the stack size difference between before the DWARF procedure
3261 invokation and after it returned. In other words, for a DWARF procedure
3262 that consumes N stack slots and that pushes M ones, this stores M - N. */
3263 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3265 /* A global counter for generating labels for line number data. */
3266 static unsigned int line_info_label_num;
3268 /* The current table to which we should emit line number information
3269 for the current function. This will be set up at the beginning of
3270 assembly for the function. */
3271 static GTY(()) dw_line_info_table *cur_line_info_table;
3273 /* The two default tables of line number info. */
3274 static GTY(()) dw_line_info_table *text_section_line_info;
3275 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3277 /* The set of all non-default tables of line number info. */
3278 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3280 /* A flag to tell pubnames/types export if there is an info section to
3281 refer to. */
3282 static bool info_section_emitted;
3284 /* A pointer to the base of a table that contains a list of publicly
3285 accessible names. */
3286 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3288 /* A pointer to the base of a table that contains a list of publicly
3289 accessible types. */
3290 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3292 /* A pointer to the base of a table that contains a list of macro
3293 defines/undefines (and file start/end markers). */
3294 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3296 /* True if .debug_macinfo or .debug_macros section is going to be
3297 emitted. */
3298 #define have_macinfo \
3299 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3300 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3301 && !macinfo_table->is_empty ())
3303 /* Vector of dies for which we should generate .debug_ranges info. */
3304 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3306 /* Vector of pairs of labels referenced in ranges_table. */
3307 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3309 /* Whether we have location lists that need outputting */
3310 static GTY(()) bool have_location_lists;
3312 /* Unique label counter. */
3313 static GTY(()) unsigned int loclabel_num;
3315 /* Unique label counter for point-of-call tables. */
3316 static GTY(()) unsigned int poc_label_num;
3318 /* The last file entry emitted by maybe_emit_file(). */
3319 static GTY(()) struct dwarf_file_data * last_emitted_file;
3321 /* Number of internal labels generated by gen_internal_sym(). */
3322 static GTY(()) int label_num;
3324 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3326 /* Instances of generic types for which we need to generate debug
3327 info that describe their generic parameters and arguments. That
3328 generation needs to happen once all types are properly laid out so
3329 we do it at the end of compilation. */
3330 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3332 /* Offset from the "steady-state frame pointer" to the frame base,
3333 within the current function. */
3334 static HOST_WIDE_INT frame_pointer_fb_offset;
3335 static bool frame_pointer_fb_offset_valid;
3337 static vec<dw_die_ref> base_types;
3339 /* Flags to represent a set of attribute classes for attributes that represent
3340 a scalar value (bounds, pointers, ...). */
3341 enum dw_scalar_form
3343 dw_scalar_form_constant = 0x01,
3344 dw_scalar_form_exprloc = 0x02,
3345 dw_scalar_form_reference = 0x04
3348 /* Forward declarations for functions defined in this file. */
3350 static int is_pseudo_reg (const_rtx);
3351 static tree type_main_variant (tree);
3352 static int is_tagged_type (const_tree);
3353 static const char *dwarf_tag_name (unsigned);
3354 static const char *dwarf_attr_name (unsigned);
3355 static const char *dwarf_form_name (unsigned);
3356 static tree decl_ultimate_origin (const_tree);
3357 static tree decl_class_context (tree);
3358 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3359 static inline enum dw_val_class AT_class (dw_attr_node *);
3360 static inline unsigned int AT_index (dw_attr_node *);
3361 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3362 static inline unsigned AT_flag (dw_attr_node *);
3363 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3364 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3365 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3366 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3367 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3368 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3369 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3370 unsigned int, unsigned char *);
3371 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3372 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3373 static inline const char *AT_string (dw_attr_node *);
3374 static enum dwarf_form AT_string_form (dw_attr_node *);
3375 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3376 static void add_AT_specification (dw_die_ref, dw_die_ref);
3377 static inline dw_die_ref AT_ref (dw_attr_node *);
3378 static inline int AT_ref_external (dw_attr_node *);
3379 static inline void set_AT_ref_external (dw_attr_node *, int);
3380 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3381 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3382 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3383 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3384 dw_loc_list_ref);
3385 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3386 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3387 static void remove_addr_table_entry (addr_table_entry *);
3388 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3389 static inline rtx AT_addr (dw_attr_node *);
3390 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3391 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3392 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3393 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3394 const char *);
3395 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3396 unsigned HOST_WIDE_INT);
3397 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3398 unsigned long, bool);
3399 static inline const char *AT_lbl (dw_attr_node *);
3400 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3401 static const char *get_AT_low_pc (dw_die_ref);
3402 static const char *get_AT_hi_pc (dw_die_ref);
3403 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3404 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3405 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3406 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3407 static bool is_cxx (void);
3408 static bool is_cxx (const_tree);
3409 static bool is_fortran (void);
3410 static bool is_ada (void);
3411 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3412 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3413 static void add_child_die (dw_die_ref, dw_die_ref);
3414 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3415 static dw_die_ref lookup_type_die (tree);
3416 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3417 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3418 static void equate_type_number_to_die (tree, dw_die_ref);
3419 static dw_die_ref lookup_decl_die (tree);
3420 static var_loc_list *lookup_decl_loc (const_tree);
3421 static void equate_decl_number_to_die (tree, dw_die_ref);
3422 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3423 static void print_spaces (FILE *);
3424 static void print_die (dw_die_ref, FILE *);
3425 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3426 static dw_die_ref pop_compile_unit (dw_die_ref);
3427 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3428 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3429 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3430 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3431 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3432 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3433 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3434 struct md5_ctx *, int *);
3435 struct checksum_attributes;
3436 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3437 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3438 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3439 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3440 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3441 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3442 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3443 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3444 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3445 static void compute_section_prefix (dw_die_ref);
3446 static int is_type_die (dw_die_ref);
3447 static int is_comdat_die (dw_die_ref);
3448 static int is_symbol_die (dw_die_ref);
3449 static inline bool is_template_instantiation (dw_die_ref);
3450 static void assign_symbol_names (dw_die_ref);
3451 static void break_out_includes (dw_die_ref);
3452 static int is_declaration_die (dw_die_ref);
3453 static int should_move_die_to_comdat (dw_die_ref);
3454 static dw_die_ref clone_as_declaration (dw_die_ref);
3455 static dw_die_ref clone_die (dw_die_ref);
3456 static dw_die_ref clone_tree (dw_die_ref);
3457 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3458 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3459 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3460 static dw_die_ref generate_skeleton (dw_die_ref);
3461 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3462 dw_die_ref,
3463 dw_die_ref);
3464 static void break_out_comdat_types (dw_die_ref);
3465 static void copy_decls_for_unworthy_types (dw_die_ref);
3467 static void add_sibling_attributes (dw_die_ref);
3468 static void output_location_lists (dw_die_ref);
3469 static int constant_size (unsigned HOST_WIDE_INT);
3470 static unsigned long size_of_die (dw_die_ref);
3471 static void calc_die_sizes (dw_die_ref);
3472 static void calc_base_type_die_sizes (void);
3473 static void mark_dies (dw_die_ref);
3474 static void unmark_dies (dw_die_ref);
3475 static void unmark_all_dies (dw_die_ref);
3476 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3477 static unsigned long size_of_aranges (void);
3478 static enum dwarf_form value_format (dw_attr_node *);
3479 static void output_value_format (dw_attr_node *);
3480 static void output_abbrev_section (void);
3481 static void output_die_abbrevs (unsigned long, dw_die_ref);
3482 static void output_die_symbol (dw_die_ref);
3483 static void output_die (dw_die_ref);
3484 static void output_compilation_unit_header (enum dwarf_unit_type);
3485 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3486 static void output_comdat_type_unit (comdat_type_node *);
3487 static const char *dwarf2_name (tree, int);
3488 static void add_pubname (tree, dw_die_ref);
3489 static void add_enumerator_pubname (const char *, dw_die_ref);
3490 static void add_pubname_string (const char *, dw_die_ref);
3491 static void add_pubtype (tree, dw_die_ref);
3492 static void output_pubnames (vec<pubname_entry, va_gc> *);
3493 static void output_aranges (void);
3494 static unsigned int add_ranges (const_tree, bool = false);
3495 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3496 bool *, bool);
3497 static void output_ranges (void);
3498 static dw_line_info_table *new_line_info_table (void);
3499 static void output_line_info (bool);
3500 static void output_file_names (void);
3501 static dw_die_ref base_type_die (tree, bool);
3502 static int is_base_type (tree);
3503 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3504 static int decl_quals (const_tree);
3505 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3506 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3507 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3508 static int type_is_enum (const_tree);
3509 static unsigned int dbx_reg_number (const_rtx);
3510 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3511 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3512 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3513 enum var_init_status);
3514 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3515 enum var_init_status);
3516 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3517 enum var_init_status);
3518 static int is_based_loc (const_rtx);
3519 static bool resolve_one_addr (rtx *);
3520 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3521 enum var_init_status);
3522 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3523 enum var_init_status);
3524 struct loc_descr_context;
3525 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3526 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3527 static dw_loc_list_ref loc_list_from_tree (tree, int,
3528 struct loc_descr_context *);
3529 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3530 struct loc_descr_context *);
3531 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3532 static tree field_type (const_tree);
3533 static unsigned int simple_type_align_in_bits (const_tree);
3534 static unsigned int simple_decl_align_in_bits (const_tree);
3535 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3536 struct vlr_context;
3537 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3538 HOST_WIDE_INT *);
3539 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3540 dw_loc_list_ref);
3541 static void add_data_member_location_attribute (dw_die_ref, tree,
3542 struct vlr_context *);
3543 static bool add_const_value_attribute (dw_die_ref, rtx);
3544 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3545 static void insert_wide_int (const wide_int &, unsigned char *, int);
3546 static void insert_float (const_rtx, unsigned char *);
3547 static rtx rtl_for_decl_location (tree);
3548 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3549 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3550 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3551 static void add_name_attribute (dw_die_ref, const char *);
3552 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3553 static void add_comp_dir_attribute (dw_die_ref);
3554 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3555 struct loc_descr_context *);
3556 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3557 struct loc_descr_context *);
3558 static void add_subscript_info (dw_die_ref, tree, bool);
3559 static void add_byte_size_attribute (dw_die_ref, tree);
3560 static void add_alignment_attribute (dw_die_ref, tree);
3561 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3562 struct vlr_context *);
3563 static void add_bit_size_attribute (dw_die_ref, tree);
3564 static void add_prototyped_attribute (dw_die_ref, tree);
3565 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3566 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3567 static void add_src_coords_attributes (dw_die_ref, tree);
3568 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3569 static void add_discr_value (dw_die_ref, dw_discr_value *);
3570 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3571 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3572 static void push_decl_scope (tree);
3573 static void pop_decl_scope (void);
3574 static dw_die_ref scope_die_for (tree, dw_die_ref);
3575 static inline int local_scope_p (dw_die_ref);
3576 static inline int class_scope_p (dw_die_ref);
3577 static inline int class_or_namespace_scope_p (dw_die_ref);
3578 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3579 static void add_calling_convention_attribute (dw_die_ref, tree);
3580 static const char *type_tag (const_tree);
3581 static tree member_declared_type (const_tree);
3582 #if 0
3583 static const char *decl_start_label (tree);
3584 #endif
3585 static void gen_array_type_die (tree, dw_die_ref);
3586 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3587 #if 0
3588 static void gen_entry_point_die (tree, dw_die_ref);
3589 #endif
3590 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3591 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3592 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3593 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3594 static void gen_formal_types_die (tree, dw_die_ref);
3595 static void gen_subprogram_die (tree, dw_die_ref);
3596 static void gen_variable_die (tree, tree, dw_die_ref);
3597 static void gen_const_die (tree, dw_die_ref);
3598 static void gen_label_die (tree, dw_die_ref);
3599 static void gen_lexical_block_die (tree, dw_die_ref);
3600 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3601 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3602 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3603 static dw_die_ref gen_compile_unit_die (const char *);
3604 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3605 static void gen_member_die (tree, dw_die_ref);
3606 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3607 enum debug_info_usage);
3608 static void gen_subroutine_type_die (tree, dw_die_ref);
3609 static void gen_typedef_die (tree, dw_die_ref);
3610 static void gen_type_die (tree, dw_die_ref);
3611 static void gen_block_die (tree, dw_die_ref);
3612 static void decls_for_scope (tree, dw_die_ref);
3613 static bool is_naming_typedef_decl (const_tree);
3614 static inline dw_die_ref get_context_die (tree);
3615 static void gen_namespace_die (tree, dw_die_ref);
3616 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3617 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3618 static dw_die_ref force_decl_die (tree);
3619 static dw_die_ref force_type_die (tree);
3620 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3621 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3622 static struct dwarf_file_data * lookup_filename (const char *);
3623 static void retry_incomplete_types (void);
3624 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3625 static void gen_generic_params_dies (tree);
3626 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3627 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3628 static void splice_child_die (dw_die_ref, dw_die_ref);
3629 static int file_info_cmp (const void *, const void *);
3630 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3631 const char *, const char *);
3632 static void output_loc_list (dw_loc_list_ref);
3633 static char *gen_internal_sym (const char *);
3634 static bool want_pubnames (void);
3636 static void prune_unmark_dies (dw_die_ref);
3637 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3638 static void prune_unused_types_mark (dw_die_ref, int);
3639 static void prune_unused_types_walk (dw_die_ref);
3640 static void prune_unused_types_walk_attribs (dw_die_ref);
3641 static void prune_unused_types_prune (dw_die_ref);
3642 static void prune_unused_types (void);
3643 static int maybe_emit_file (struct dwarf_file_data *fd);
3644 static inline const char *AT_vms_delta1 (dw_attr_node *);
3645 static inline const char *AT_vms_delta2 (dw_attr_node *);
3646 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3647 const char *, const char *);
3648 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3649 static void gen_remaining_tmpl_value_param_die_attribute (void);
3650 static bool generic_type_p (tree);
3651 static void schedule_generic_params_dies_gen (tree t);
3652 static void gen_scheduled_generic_parms_dies (void);
3653 static void resolve_variable_values (void);
3655 static const char *comp_dir_string (void);
3657 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3659 /* enum for tracking thread-local variables whose address is really an offset
3660 relative to the TLS pointer, which will need link-time relocation, but will
3661 not need relocation by the DWARF consumer. */
3663 enum dtprel_bool
3665 dtprel_false = 0,
3666 dtprel_true = 1
3669 /* Return the operator to use for an address of a variable. For dtprel_true, we
3670 use DW_OP_const*. For regular variables, which need both link-time
3671 relocation and consumer-level relocation (e.g., to account for shared objects
3672 loaded at a random address), we use DW_OP_addr*. */
3674 static inline enum dwarf_location_atom
3675 dw_addr_op (enum dtprel_bool dtprel)
3677 if (dtprel == dtprel_true)
3678 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3679 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3680 else
3681 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3684 /* Return a pointer to a newly allocated address location description. If
3685 dwarf_split_debug_info is true, then record the address with the appropriate
3686 relocation. */
3687 static inline dw_loc_descr_ref
3688 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3690 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3692 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3693 ref->dw_loc_oprnd1.v.val_addr = addr;
3694 ref->dtprel = dtprel;
3695 if (dwarf_split_debug_info)
3696 ref->dw_loc_oprnd1.val_entry
3697 = add_addr_table_entry (addr,
3698 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3699 else
3700 ref->dw_loc_oprnd1.val_entry = NULL;
3702 return ref;
3705 /* Section names used to hold DWARF debugging information. */
3707 #ifndef DEBUG_INFO_SECTION
3708 #define DEBUG_INFO_SECTION ".debug_info"
3709 #endif
3710 #ifndef DEBUG_DWO_INFO_SECTION
3711 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3712 #endif
3713 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3714 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3715 #endif
3716 #ifndef DEBUG_LTO_INFO_SECTION
3717 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3718 #endif
3719 #ifndef DEBUG_ABBREV_SECTION
3720 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3721 #endif
3722 #ifndef DEBUG_DWO_ABBREV_SECTION
3723 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3724 #endif
3725 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3726 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3727 #endif
3728 #ifndef DEBUG_LTO_ABBREV_SECTION
3729 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3730 #endif
3731 #ifndef DEBUG_ARANGES_SECTION
3732 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3733 #endif
3734 #ifndef DEBUG_ADDR_SECTION
3735 #define DEBUG_ADDR_SECTION ".debug_addr"
3736 #endif
3737 #ifndef DEBUG_MACINFO_SECTION
3738 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3739 #endif
3740 #ifndef DEBUG_DWO_MACINFO_SECTION
3741 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3742 #endif
3743 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3744 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3745 #endif
3746 #ifndef DEBUG_LTO_MACINFO_SECTION
3747 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3748 #endif
3749 #ifndef DEBUG_DWO_MACRO_SECTION
3750 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3751 #endif
3752 #ifndef DEBUG_MACRO_SECTION
3753 #define DEBUG_MACRO_SECTION ".debug_macro"
3754 #endif
3755 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3756 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3757 #endif
3758 #ifndef DEBUG_LTO_MACRO_SECTION
3759 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3760 #endif
3761 #ifndef DEBUG_LINE_SECTION
3762 #define DEBUG_LINE_SECTION ".debug_line"
3763 #endif
3764 #ifndef DEBUG_DWO_LINE_SECTION
3765 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3766 #endif
3767 #ifndef DEBUG_LTO_LINE_SECTION
3768 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3769 #endif
3770 #ifndef DEBUG_LOC_SECTION
3771 #define DEBUG_LOC_SECTION ".debug_loc"
3772 #endif
3773 #ifndef DEBUG_DWO_LOC_SECTION
3774 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3775 #endif
3776 #ifndef DEBUG_LOCLISTS_SECTION
3777 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3778 #endif
3779 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3780 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3781 #endif
3782 #ifndef DEBUG_PUBNAMES_SECTION
3783 #define DEBUG_PUBNAMES_SECTION \
3784 ((debug_generate_pub_sections == 2) \
3785 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3786 #endif
3787 #ifndef DEBUG_PUBTYPES_SECTION
3788 #define DEBUG_PUBTYPES_SECTION \
3789 ((debug_generate_pub_sections == 2) \
3790 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3791 #endif
3792 #ifndef DEBUG_STR_OFFSETS_SECTION
3793 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3794 #endif
3795 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3796 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3797 #endif
3798 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3799 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3800 #endif
3801 #ifndef DEBUG_STR_DWO_SECTION
3802 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3803 #endif
3804 #ifndef DEBUG_LTO_STR_DWO_SECTION
3805 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3806 #endif
3807 #ifndef DEBUG_STR_SECTION
3808 #define DEBUG_STR_SECTION ".debug_str"
3809 #endif
3810 #ifndef DEBUG_LTO_STR_SECTION
3811 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3812 #endif
3813 #ifndef DEBUG_RANGES_SECTION
3814 #define DEBUG_RANGES_SECTION ".debug_ranges"
3815 #endif
3816 #ifndef DEBUG_RNGLISTS_SECTION
3817 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3818 #endif
3819 #ifndef DEBUG_LINE_STR_SECTION
3820 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3821 #endif
3823 /* Standard ELF section names for compiled code and data. */
3824 #ifndef TEXT_SECTION_NAME
3825 #define TEXT_SECTION_NAME ".text"
3826 #endif
3828 /* Section flags for .debug_str section. */
3829 #define DEBUG_STR_SECTION_FLAGS \
3830 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3831 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3832 : SECTION_DEBUG)
3834 /* Section flags for .debug_str.dwo section. */
3835 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3837 /* Attribute used to refer to the macro section. */
3838 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3839 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3841 /* Labels we insert at beginning sections we can reference instead of
3842 the section names themselves. */
3844 #ifndef TEXT_SECTION_LABEL
3845 #define TEXT_SECTION_LABEL "Ltext"
3846 #endif
3847 #ifndef COLD_TEXT_SECTION_LABEL
3848 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3849 #endif
3850 #ifndef DEBUG_LINE_SECTION_LABEL
3851 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3852 #endif
3853 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3854 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3855 #endif
3856 #ifndef DEBUG_INFO_SECTION_LABEL
3857 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3858 #endif
3859 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3860 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3861 #endif
3862 #ifndef DEBUG_ABBREV_SECTION_LABEL
3863 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3864 #endif
3865 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3866 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3867 #endif
3868 #ifndef DEBUG_ADDR_SECTION_LABEL
3869 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3870 #endif
3871 #ifndef DEBUG_LOC_SECTION_LABEL
3872 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3873 #endif
3874 #ifndef DEBUG_RANGES_SECTION_LABEL
3875 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3876 #endif
3877 #ifndef DEBUG_MACINFO_SECTION_LABEL
3878 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3879 #endif
3880 #ifndef DEBUG_MACRO_SECTION_LABEL
3881 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3882 #endif
3883 #define SKELETON_COMP_DIE_ABBREV 1
3884 #define SKELETON_TYPE_DIE_ABBREV 2
3886 /* Definitions of defaults for formats and names of various special
3887 (artificial) labels which may be generated within this file (when the -g
3888 options is used and DWARF2_DEBUGGING_INFO is in effect.
3889 If necessary, these may be overridden from within the tm.h file, but
3890 typically, overriding these defaults is unnecessary. */
3892 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3893 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3894 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3895 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3899 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3900 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3901 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3902 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3903 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3904 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3905 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3906 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3908 #ifndef TEXT_END_LABEL
3909 #define TEXT_END_LABEL "Letext"
3910 #endif
3911 #ifndef COLD_END_LABEL
3912 #define COLD_END_LABEL "Letext_cold"
3913 #endif
3914 #ifndef BLOCK_BEGIN_LABEL
3915 #define BLOCK_BEGIN_LABEL "LBB"
3916 #endif
3917 #ifndef BLOCK_END_LABEL
3918 #define BLOCK_END_LABEL "LBE"
3919 #endif
3920 #ifndef LINE_CODE_LABEL
3921 #define LINE_CODE_LABEL "LM"
3922 #endif
3925 /* Return the root of the DIE's built for the current compilation unit. */
3926 static dw_die_ref
3927 comp_unit_die (void)
3929 if (!single_comp_unit_die)
3930 single_comp_unit_die = gen_compile_unit_die (NULL);
3931 return single_comp_unit_die;
3934 /* We allow a language front-end to designate a function that is to be
3935 called to "demangle" any name before it is put into a DIE. */
3937 static const char *(*demangle_name_func) (const char *);
3939 void
3940 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3942 demangle_name_func = func;
3945 /* Test if rtl node points to a pseudo register. */
3947 static inline int
3948 is_pseudo_reg (const_rtx rtl)
3950 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3951 || (GET_CODE (rtl) == SUBREG
3952 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3955 /* Return a reference to a type, with its const and volatile qualifiers
3956 removed. */
3958 static inline tree
3959 type_main_variant (tree type)
3961 type = TYPE_MAIN_VARIANT (type);
3963 /* ??? There really should be only one main variant among any group of
3964 variants of a given type (and all of the MAIN_VARIANT values for all
3965 members of the group should point to that one type) but sometimes the C
3966 front-end messes this up for array types, so we work around that bug
3967 here. */
3968 if (TREE_CODE (type) == ARRAY_TYPE)
3969 while (type != TYPE_MAIN_VARIANT (type))
3970 type = TYPE_MAIN_VARIANT (type);
3972 return type;
3975 /* Return nonzero if the given type node represents a tagged type. */
3977 static inline int
3978 is_tagged_type (const_tree type)
3980 enum tree_code code = TREE_CODE (type);
3982 return (code == RECORD_TYPE || code == UNION_TYPE
3983 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3986 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3988 static void
3989 get_ref_die_offset_label (char *label, dw_die_ref ref)
3991 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3994 /* Return die_offset of a DIE reference to a base type. */
3996 static unsigned long int
3997 get_base_type_offset (dw_die_ref ref)
3999 if (ref->die_offset)
4000 return ref->die_offset;
4001 if (comp_unit_die ()->die_abbrev)
4003 calc_base_type_die_sizes ();
4004 gcc_assert (ref->die_offset);
4006 return ref->die_offset;
4009 /* Return die_offset of a DIE reference other than base type. */
4011 static unsigned long int
4012 get_ref_die_offset (dw_die_ref ref)
4014 gcc_assert (ref->die_offset);
4015 return ref->die_offset;
4018 /* Convert a DIE tag into its string name. */
4020 static const char *
4021 dwarf_tag_name (unsigned int tag)
4023 const char *name = get_DW_TAG_name (tag);
4025 if (name != NULL)
4026 return name;
4028 return "DW_TAG_<unknown>";
4031 /* Convert a DWARF attribute code into its string name. */
4033 static const char *
4034 dwarf_attr_name (unsigned int attr)
4036 const char *name;
4038 switch (attr)
4040 #if VMS_DEBUGGING_INFO
4041 case DW_AT_HP_prologue:
4042 return "DW_AT_HP_prologue";
4043 #else
4044 case DW_AT_MIPS_loop_unroll_factor:
4045 return "DW_AT_MIPS_loop_unroll_factor";
4046 #endif
4048 #if VMS_DEBUGGING_INFO
4049 case DW_AT_HP_epilogue:
4050 return "DW_AT_HP_epilogue";
4051 #else
4052 case DW_AT_MIPS_stride:
4053 return "DW_AT_MIPS_stride";
4054 #endif
4057 name = get_DW_AT_name (attr);
4059 if (name != NULL)
4060 return name;
4062 return "DW_AT_<unknown>";
4065 /* Convert a DWARF value form code into its string name. */
4067 static const char *
4068 dwarf_form_name (unsigned int form)
4070 const char *name = get_DW_FORM_name (form);
4072 if (name != NULL)
4073 return name;
4075 return "DW_FORM_<unknown>";
4078 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4079 instance of an inlined instance of a decl which is local to an inline
4080 function, so we have to trace all of the way back through the origin chain
4081 to find out what sort of node actually served as the original seed for the
4082 given block. */
4084 static tree
4085 decl_ultimate_origin (const_tree decl)
4087 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4088 return NULL_TREE;
4090 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4091 we're trying to output the abstract instance of this function. */
4092 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4093 return NULL_TREE;
4095 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4096 most distant ancestor, this should never happen. */
4097 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4099 return DECL_ABSTRACT_ORIGIN (decl);
4102 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4103 of a virtual function may refer to a base class, so we check the 'this'
4104 parameter. */
4106 static tree
4107 decl_class_context (tree decl)
4109 tree context = NULL_TREE;
4111 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4112 context = DECL_CONTEXT (decl);
4113 else
4114 context = TYPE_MAIN_VARIANT
4115 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4117 if (context && !TYPE_P (context))
4118 context = NULL_TREE;
4120 return context;
4123 /* Add an attribute/value pair to a DIE. */
4125 static inline void
4126 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4128 /* Maybe this should be an assert? */
4129 if (die == NULL)
4130 return;
4132 vec_safe_reserve (die->die_attr, 1);
4133 vec_safe_push (die->die_attr, *attr);
4136 static inline enum dw_val_class
4137 AT_class (dw_attr_node *a)
4139 return a->dw_attr_val.val_class;
4142 /* Return the index for any attribute that will be referenced with a
4143 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4144 are stored in dw_attr_val.v.val_str for reference counting
4145 pruning. */
4147 static inline unsigned int
4148 AT_index (dw_attr_node *a)
4150 if (AT_class (a) == dw_val_class_str)
4151 return a->dw_attr_val.v.val_str->index;
4152 else if (a->dw_attr_val.val_entry != NULL)
4153 return a->dw_attr_val.val_entry->index;
4154 return NOT_INDEXED;
4157 /* Add a flag value attribute to a DIE. */
4159 static inline void
4160 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4162 dw_attr_node attr;
4164 attr.dw_attr = attr_kind;
4165 attr.dw_attr_val.val_class = dw_val_class_flag;
4166 attr.dw_attr_val.val_entry = NULL;
4167 attr.dw_attr_val.v.val_flag = flag;
4168 add_dwarf_attr (die, &attr);
4171 static inline unsigned
4172 AT_flag (dw_attr_node *a)
4174 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4175 return a->dw_attr_val.v.val_flag;
4178 /* Add a signed integer attribute value to a DIE. */
4180 static inline void
4181 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4183 dw_attr_node attr;
4185 attr.dw_attr = attr_kind;
4186 attr.dw_attr_val.val_class = dw_val_class_const;
4187 attr.dw_attr_val.val_entry = NULL;
4188 attr.dw_attr_val.v.val_int = int_val;
4189 add_dwarf_attr (die, &attr);
4192 static inline HOST_WIDE_INT
4193 AT_int (dw_attr_node *a)
4195 gcc_assert (a && (AT_class (a) == dw_val_class_const
4196 || AT_class (a) == dw_val_class_const_implicit));
4197 return a->dw_attr_val.v.val_int;
4200 /* Add an unsigned integer attribute value to a DIE. */
4202 static inline void
4203 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4204 unsigned HOST_WIDE_INT unsigned_val)
4206 dw_attr_node attr;
4208 attr.dw_attr = attr_kind;
4209 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4210 attr.dw_attr_val.val_entry = NULL;
4211 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4212 add_dwarf_attr (die, &attr);
4215 static inline unsigned HOST_WIDE_INT
4216 AT_unsigned (dw_attr_node *a)
4218 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4219 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4220 return a->dw_attr_val.v.val_unsigned;
4223 /* Add an unsigned wide integer attribute value to a DIE. */
4225 static inline void
4226 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4227 const wide_int& w)
4229 dw_attr_node attr;
4231 attr.dw_attr = attr_kind;
4232 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4233 attr.dw_attr_val.val_entry = NULL;
4234 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4235 *attr.dw_attr_val.v.val_wide = w;
4236 add_dwarf_attr (die, &attr);
4239 /* Add an unsigned double integer attribute value to a DIE. */
4241 static inline void
4242 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4243 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4245 dw_attr_node attr;
4247 attr.dw_attr = attr_kind;
4248 attr.dw_attr_val.val_class = dw_val_class_const_double;
4249 attr.dw_attr_val.val_entry = NULL;
4250 attr.dw_attr_val.v.val_double.high = high;
4251 attr.dw_attr_val.v.val_double.low = low;
4252 add_dwarf_attr (die, &attr);
4255 /* Add a floating point attribute value to a DIE and return it. */
4257 static inline void
4258 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4259 unsigned int length, unsigned int elt_size, unsigned char *array)
4261 dw_attr_node attr;
4263 attr.dw_attr = attr_kind;
4264 attr.dw_attr_val.val_class = dw_val_class_vec;
4265 attr.dw_attr_val.val_entry = NULL;
4266 attr.dw_attr_val.v.val_vec.length = length;
4267 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4268 attr.dw_attr_val.v.val_vec.array = array;
4269 add_dwarf_attr (die, &attr);
4272 /* Add an 8-byte data attribute value to a DIE. */
4274 static inline void
4275 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4276 unsigned char data8[8])
4278 dw_attr_node attr;
4280 attr.dw_attr = attr_kind;
4281 attr.dw_attr_val.val_class = dw_val_class_data8;
4282 attr.dw_attr_val.val_entry = NULL;
4283 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4284 add_dwarf_attr (die, &attr);
4287 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4288 dwarf_split_debug_info, address attributes in dies destined for the
4289 final executable have force_direct set to avoid using indexed
4290 references. */
4292 static inline void
4293 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4294 bool force_direct)
4296 dw_attr_node attr;
4297 char * lbl_id;
4299 lbl_id = xstrdup (lbl_low);
4300 attr.dw_attr = DW_AT_low_pc;
4301 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4302 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4303 if (dwarf_split_debug_info && !force_direct)
4304 attr.dw_attr_val.val_entry
4305 = add_addr_table_entry (lbl_id, ate_kind_label);
4306 else
4307 attr.dw_attr_val.val_entry = NULL;
4308 add_dwarf_attr (die, &attr);
4310 attr.dw_attr = DW_AT_high_pc;
4311 if (dwarf_version < 4)
4312 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4313 else
4314 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4315 lbl_id = xstrdup (lbl_high);
4316 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4317 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4318 && dwarf_split_debug_info && !force_direct)
4319 attr.dw_attr_val.val_entry
4320 = add_addr_table_entry (lbl_id, ate_kind_label);
4321 else
4322 attr.dw_attr_val.val_entry = NULL;
4323 add_dwarf_attr (die, &attr);
4326 /* Hash and equality functions for debug_str_hash. */
4328 hashval_t
4329 indirect_string_hasher::hash (indirect_string_node *x)
4331 return htab_hash_string (x->str);
4334 bool
4335 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4337 return strcmp (x1->str, x2) == 0;
4340 /* Add STR to the given string hash table. */
4342 static struct indirect_string_node *
4343 find_AT_string_in_table (const char *str,
4344 hash_table<indirect_string_hasher> *table)
4346 struct indirect_string_node *node;
4348 indirect_string_node **slot
4349 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4350 if (*slot == NULL)
4352 node = ggc_cleared_alloc<indirect_string_node> ();
4353 node->str = ggc_strdup (str);
4354 *slot = node;
4356 else
4357 node = *slot;
4359 node->refcount++;
4360 return node;
4363 /* Add STR to the indirect string hash table. */
4365 static struct indirect_string_node *
4366 find_AT_string (const char *str)
4368 if (! debug_str_hash)
4369 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4371 return find_AT_string_in_table (str, debug_str_hash);
4374 /* Add a string attribute value to a DIE. */
4376 static inline void
4377 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4379 dw_attr_node attr;
4380 struct indirect_string_node *node;
4382 node = find_AT_string (str);
4384 attr.dw_attr = attr_kind;
4385 attr.dw_attr_val.val_class = dw_val_class_str;
4386 attr.dw_attr_val.val_entry = NULL;
4387 attr.dw_attr_val.v.val_str = node;
4388 add_dwarf_attr (die, &attr);
4391 static inline const char *
4392 AT_string (dw_attr_node *a)
4394 gcc_assert (a && AT_class (a) == dw_val_class_str);
4395 return a->dw_attr_val.v.val_str->str;
4398 /* Call this function directly to bypass AT_string_form's logic to put
4399 the string inline in the die. */
4401 static void
4402 set_indirect_string (struct indirect_string_node *node)
4404 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4405 /* Already indirect is a no op. */
4406 if (node->form == DW_FORM_strp
4407 || node->form == DW_FORM_line_strp
4408 || node->form == DW_FORM_GNU_str_index)
4410 gcc_assert (node->label);
4411 return;
4413 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4414 ++dw2_string_counter;
4415 node->label = xstrdup (label);
4417 if (!dwarf_split_debug_info)
4419 node->form = DW_FORM_strp;
4420 node->index = NOT_INDEXED;
4422 else
4424 node->form = DW_FORM_GNU_str_index;
4425 node->index = NO_INDEX_ASSIGNED;
4429 /* A helper function for dwarf2out_finish, called to reset indirect
4430 string decisions done for early LTO dwarf output before fat object
4431 dwarf output. */
4434 reset_indirect_string (indirect_string_node **h, void *)
4436 struct indirect_string_node *node = *h;
4437 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4439 free (node->label);
4440 node->label = NULL;
4441 node->form = (dwarf_form) 0;
4442 node->index = 0;
4444 return 1;
4447 /* Find out whether a string should be output inline in DIE
4448 or out-of-line in .debug_str section. */
4450 static enum dwarf_form
4451 find_string_form (struct indirect_string_node *node)
4453 unsigned int len;
4455 if (node->form)
4456 return node->form;
4458 len = strlen (node->str) + 1;
4460 /* If the string is shorter or equal to the size of the reference, it is
4461 always better to put it inline. */
4462 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4463 return node->form = DW_FORM_string;
4465 /* If we cannot expect the linker to merge strings in .debug_str
4466 section, only put it into .debug_str if it is worth even in this
4467 single module. */
4468 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4469 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4470 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4471 return node->form = DW_FORM_string;
4473 set_indirect_string (node);
4475 return node->form;
4478 /* Find out whether the string referenced from the attribute should be
4479 output inline in DIE or out-of-line in .debug_str section. */
4481 static enum dwarf_form
4482 AT_string_form (dw_attr_node *a)
4484 gcc_assert (a && AT_class (a) == dw_val_class_str);
4485 return find_string_form (a->dw_attr_val.v.val_str);
4488 /* Add a DIE reference attribute value to a DIE. */
4490 static inline void
4491 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4493 dw_attr_node attr;
4494 gcc_checking_assert (targ_die != NULL);
4496 /* With LTO we can end up trying to reference something we didn't create
4497 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4498 if (targ_die == NULL)
4499 return;
4501 attr.dw_attr = attr_kind;
4502 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4503 attr.dw_attr_val.val_entry = NULL;
4504 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4505 attr.dw_attr_val.v.val_die_ref.external = 0;
4506 add_dwarf_attr (die, &attr);
4509 /* Change DIE reference REF to point to NEW_DIE instead. */
4511 static inline void
4512 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4514 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4515 ref->dw_attr_val.v.val_die_ref.die = new_die;
4516 ref->dw_attr_val.v.val_die_ref.external = 0;
4519 /* Add an AT_specification attribute to a DIE, and also make the back
4520 pointer from the specification to the definition. */
4522 static inline void
4523 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4525 add_AT_die_ref (die, DW_AT_specification, targ_die);
4526 gcc_assert (!targ_die->die_definition);
4527 targ_die->die_definition = die;
4530 static inline dw_die_ref
4531 AT_ref (dw_attr_node *a)
4533 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4534 return a->dw_attr_val.v.val_die_ref.die;
4537 static inline int
4538 AT_ref_external (dw_attr_node *a)
4540 if (a && AT_class (a) == dw_val_class_die_ref)
4541 return a->dw_attr_val.v.val_die_ref.external;
4543 return 0;
4546 static inline void
4547 set_AT_ref_external (dw_attr_node *a, int i)
4549 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4550 a->dw_attr_val.v.val_die_ref.external = i;
4553 /* Add an FDE reference attribute value to a DIE. */
4555 static inline void
4556 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4558 dw_attr_node attr;
4560 attr.dw_attr = attr_kind;
4561 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4562 attr.dw_attr_val.val_entry = NULL;
4563 attr.dw_attr_val.v.val_fde_index = targ_fde;
4564 add_dwarf_attr (die, &attr);
4567 /* Add a location description attribute value to a DIE. */
4569 static inline void
4570 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4572 dw_attr_node attr;
4574 attr.dw_attr = attr_kind;
4575 attr.dw_attr_val.val_class = dw_val_class_loc;
4576 attr.dw_attr_val.val_entry = NULL;
4577 attr.dw_attr_val.v.val_loc = loc;
4578 add_dwarf_attr (die, &attr);
4581 static inline dw_loc_descr_ref
4582 AT_loc (dw_attr_node *a)
4584 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4585 return a->dw_attr_val.v.val_loc;
4588 static inline void
4589 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4591 dw_attr_node attr;
4593 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4594 return;
4596 attr.dw_attr = attr_kind;
4597 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4598 attr.dw_attr_val.val_entry = NULL;
4599 attr.dw_attr_val.v.val_loc_list = loc_list;
4600 add_dwarf_attr (die, &attr);
4601 have_location_lists = true;
4604 static inline dw_loc_list_ref
4605 AT_loc_list (dw_attr_node *a)
4607 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4608 return a->dw_attr_val.v.val_loc_list;
4611 static inline dw_loc_list_ref *
4612 AT_loc_list_ptr (dw_attr_node *a)
4614 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4615 return &a->dw_attr_val.v.val_loc_list;
4618 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4620 static hashval_t hash (addr_table_entry *);
4621 static bool equal (addr_table_entry *, addr_table_entry *);
4624 /* Table of entries into the .debug_addr section. */
4626 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4628 /* Hash an address_table_entry. */
4630 hashval_t
4631 addr_hasher::hash (addr_table_entry *a)
4633 inchash::hash hstate;
4634 switch (a->kind)
4636 case ate_kind_rtx:
4637 hstate.add_int (0);
4638 break;
4639 case ate_kind_rtx_dtprel:
4640 hstate.add_int (1);
4641 break;
4642 case ate_kind_label:
4643 return htab_hash_string (a->addr.label);
4644 default:
4645 gcc_unreachable ();
4647 inchash::add_rtx (a->addr.rtl, hstate);
4648 return hstate.end ();
4651 /* Determine equality for two address_table_entries. */
4653 bool
4654 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4656 if (a1->kind != a2->kind)
4657 return 0;
4658 switch (a1->kind)
4660 case ate_kind_rtx:
4661 case ate_kind_rtx_dtprel:
4662 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4663 case ate_kind_label:
4664 return strcmp (a1->addr.label, a2->addr.label) == 0;
4665 default:
4666 gcc_unreachable ();
4670 /* Initialize an addr_table_entry. */
4672 void
4673 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4675 e->kind = kind;
4676 switch (kind)
4678 case ate_kind_rtx:
4679 case ate_kind_rtx_dtprel:
4680 e->addr.rtl = (rtx) addr;
4681 break;
4682 case ate_kind_label:
4683 e->addr.label = (char *) addr;
4684 break;
4686 e->refcount = 0;
4687 e->index = NO_INDEX_ASSIGNED;
4690 /* Add attr to the address table entry to the table. Defer setting an
4691 index until output time. */
4693 static addr_table_entry *
4694 add_addr_table_entry (void *addr, enum ate_kind kind)
4696 addr_table_entry *node;
4697 addr_table_entry finder;
4699 gcc_assert (dwarf_split_debug_info);
4700 if (! addr_index_table)
4701 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4702 init_addr_table_entry (&finder, kind, addr);
4703 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4705 if (*slot == HTAB_EMPTY_ENTRY)
4707 node = ggc_cleared_alloc<addr_table_entry> ();
4708 init_addr_table_entry (node, kind, addr);
4709 *slot = node;
4711 else
4712 node = *slot;
4714 node->refcount++;
4715 return node;
4718 /* Remove an entry from the addr table by decrementing its refcount.
4719 Strictly, decrementing the refcount would be enough, but the
4720 assertion that the entry is actually in the table has found
4721 bugs. */
4723 static void
4724 remove_addr_table_entry (addr_table_entry *entry)
4726 gcc_assert (dwarf_split_debug_info && addr_index_table);
4727 /* After an index is assigned, the table is frozen. */
4728 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4729 entry->refcount--;
4732 /* Given a location list, remove all addresses it refers to from the
4733 address_table. */
4735 static void
4736 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4738 for (; descr; descr = descr->dw_loc_next)
4739 if (descr->dw_loc_oprnd1.val_entry != NULL)
4741 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4742 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4746 /* A helper function for dwarf2out_finish called through
4747 htab_traverse. Assign an addr_table_entry its index. All entries
4748 must be collected into the table when this function is called,
4749 because the indexing code relies on htab_traverse to traverse nodes
4750 in the same order for each run. */
4753 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4755 addr_table_entry *node = *h;
4757 /* Don't index unreferenced nodes. */
4758 if (node->refcount == 0)
4759 return 1;
4761 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4762 node->index = *index;
4763 *index += 1;
4765 return 1;
4768 /* Add an address constant attribute value to a DIE. When using
4769 dwarf_split_debug_info, address attributes in dies destined for the
4770 final executable should be direct references--setting the parameter
4771 force_direct ensures this behavior. */
4773 static inline void
4774 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4775 bool force_direct)
4777 dw_attr_node attr;
4779 attr.dw_attr = attr_kind;
4780 attr.dw_attr_val.val_class = dw_val_class_addr;
4781 attr.dw_attr_val.v.val_addr = addr;
4782 if (dwarf_split_debug_info && !force_direct)
4783 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4784 else
4785 attr.dw_attr_val.val_entry = NULL;
4786 add_dwarf_attr (die, &attr);
4789 /* Get the RTX from to an address DIE attribute. */
4791 static inline rtx
4792 AT_addr (dw_attr_node *a)
4794 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4795 return a->dw_attr_val.v.val_addr;
4798 /* Add a file attribute value to a DIE. */
4800 static inline void
4801 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4802 struct dwarf_file_data *fd)
4804 dw_attr_node attr;
4806 attr.dw_attr = attr_kind;
4807 attr.dw_attr_val.val_class = dw_val_class_file;
4808 attr.dw_attr_val.val_entry = NULL;
4809 attr.dw_attr_val.v.val_file = fd;
4810 add_dwarf_attr (die, &attr);
4813 /* Get the dwarf_file_data from a file DIE attribute. */
4815 static inline struct dwarf_file_data *
4816 AT_file (dw_attr_node *a)
4818 gcc_assert (a && (AT_class (a) == dw_val_class_file
4819 || AT_class (a) == dw_val_class_file_implicit));
4820 return a->dw_attr_val.v.val_file;
4823 /* Add a vms delta attribute value to a DIE. */
4825 static inline void
4826 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4827 const char *lbl1, const char *lbl2)
4829 dw_attr_node attr;
4831 attr.dw_attr = attr_kind;
4832 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4833 attr.dw_attr_val.val_entry = NULL;
4834 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4835 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4836 add_dwarf_attr (die, &attr);
4839 /* Add a label identifier attribute value to a DIE. */
4841 static inline void
4842 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4843 const char *lbl_id)
4845 dw_attr_node attr;
4847 attr.dw_attr = attr_kind;
4848 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4849 attr.dw_attr_val.val_entry = NULL;
4850 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4851 if (dwarf_split_debug_info)
4852 attr.dw_attr_val.val_entry
4853 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4854 ate_kind_label);
4855 add_dwarf_attr (die, &attr);
4858 /* Add a section offset attribute value to a DIE, an offset into the
4859 debug_line section. */
4861 static inline void
4862 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4863 const char *label)
4865 dw_attr_node attr;
4867 attr.dw_attr = attr_kind;
4868 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4869 attr.dw_attr_val.val_entry = NULL;
4870 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4871 add_dwarf_attr (die, &attr);
4874 /* Add a section offset attribute value to a DIE, an offset into the
4875 debug_loclists section. */
4877 static inline void
4878 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4879 const char *label)
4881 dw_attr_node attr;
4883 attr.dw_attr = attr_kind;
4884 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4885 attr.dw_attr_val.val_entry = NULL;
4886 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4887 add_dwarf_attr (die, &attr);
4890 /* Add a section offset attribute value to a DIE, an offset into the
4891 debug_macinfo section. */
4893 static inline void
4894 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4895 const char *label)
4897 dw_attr_node attr;
4899 attr.dw_attr = attr_kind;
4900 attr.dw_attr_val.val_class = dw_val_class_macptr;
4901 attr.dw_attr_val.val_entry = NULL;
4902 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4903 add_dwarf_attr (die, &attr);
4906 /* Add an offset attribute value to a DIE. */
4908 static inline void
4909 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4910 unsigned HOST_WIDE_INT offset)
4912 dw_attr_node attr;
4914 attr.dw_attr = attr_kind;
4915 attr.dw_attr_val.val_class = dw_val_class_offset;
4916 attr.dw_attr_val.val_entry = NULL;
4917 attr.dw_attr_val.v.val_offset = offset;
4918 add_dwarf_attr (die, &attr);
4921 /* Add a range_list attribute value to a DIE. When using
4922 dwarf_split_debug_info, address attributes in dies destined for the
4923 final executable should be direct references--setting the parameter
4924 force_direct ensures this behavior. */
4926 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4927 #define RELOCATED_OFFSET (NULL)
4929 static void
4930 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4931 long unsigned int offset, bool force_direct)
4933 dw_attr_node attr;
4935 attr.dw_attr = attr_kind;
4936 attr.dw_attr_val.val_class = dw_val_class_range_list;
4937 /* For the range_list attribute, use val_entry to store whether the
4938 offset should follow split-debug-info or normal semantics. This
4939 value is read in output_range_list_offset. */
4940 if (dwarf_split_debug_info && !force_direct)
4941 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4942 else
4943 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4944 attr.dw_attr_val.v.val_offset = offset;
4945 add_dwarf_attr (die, &attr);
4948 /* Return the start label of a delta attribute. */
4950 static inline const char *
4951 AT_vms_delta1 (dw_attr_node *a)
4953 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4954 return a->dw_attr_val.v.val_vms_delta.lbl1;
4957 /* Return the end label of a delta attribute. */
4959 static inline const char *
4960 AT_vms_delta2 (dw_attr_node *a)
4962 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4963 return a->dw_attr_val.v.val_vms_delta.lbl2;
4966 static inline const char *
4967 AT_lbl (dw_attr_node *a)
4969 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4970 || AT_class (a) == dw_val_class_lineptr
4971 || AT_class (a) == dw_val_class_macptr
4972 || AT_class (a) == dw_val_class_loclistsptr
4973 || AT_class (a) == dw_val_class_high_pc));
4974 return a->dw_attr_val.v.val_lbl_id;
4977 /* Get the attribute of type attr_kind. */
4979 static dw_attr_node *
4980 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4982 dw_attr_node *a;
4983 unsigned ix;
4984 dw_die_ref spec = NULL;
4986 if (! die)
4987 return NULL;
4989 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4990 if (a->dw_attr == attr_kind)
4991 return a;
4992 else if (a->dw_attr == DW_AT_specification
4993 || a->dw_attr == DW_AT_abstract_origin)
4994 spec = AT_ref (a);
4996 if (spec)
4997 return get_AT (spec, attr_kind);
4999 return NULL;
5002 /* Returns the parent of the declaration of DIE. */
5004 static dw_die_ref
5005 get_die_parent (dw_die_ref die)
5007 dw_die_ref t;
5009 if (!die)
5010 return NULL;
5012 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5013 || (t = get_AT_ref (die, DW_AT_specification)))
5014 die = t;
5016 return die->die_parent;
5019 /* Return the "low pc" attribute value, typically associated with a subprogram
5020 DIE. Return null if the "low pc" attribute is either not present, or if it
5021 cannot be represented as an assembler label identifier. */
5023 static inline const char *
5024 get_AT_low_pc (dw_die_ref die)
5026 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5028 return a ? AT_lbl (a) : NULL;
5031 /* Return the "high pc" attribute value, typically associated with a subprogram
5032 DIE. Return null if the "high pc" attribute is either not present, or if it
5033 cannot be represented as an assembler label identifier. */
5035 static inline const char *
5036 get_AT_hi_pc (dw_die_ref die)
5038 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5040 return a ? AT_lbl (a) : NULL;
5043 /* Return the value of the string attribute designated by ATTR_KIND, or
5044 NULL if it is not present. */
5046 static inline const char *
5047 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5049 dw_attr_node *a = get_AT (die, attr_kind);
5051 return a ? AT_string (a) : NULL;
5054 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5055 if it is not present. */
5057 static inline int
5058 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5060 dw_attr_node *a = get_AT (die, attr_kind);
5062 return a ? AT_flag (a) : 0;
5065 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5066 if it is not present. */
5068 static inline unsigned
5069 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5071 dw_attr_node *a = get_AT (die, attr_kind);
5073 return a ? AT_unsigned (a) : 0;
5076 static inline dw_die_ref
5077 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5079 dw_attr_node *a = get_AT (die, attr_kind);
5081 return a ? AT_ref (a) : NULL;
5084 static inline struct dwarf_file_data *
5085 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5087 dw_attr_node *a = get_AT (die, attr_kind);
5089 return a ? AT_file (a) : NULL;
5092 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
5094 static const_tree
5095 get_ultimate_context (const_tree decl)
5097 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
5099 if (TREE_CODE (decl) == BLOCK)
5100 decl = BLOCK_SUPERCONTEXT (decl);
5101 else
5102 decl = get_containing_scope (decl);
5104 return decl;
5107 /* Return TRUE if the language is C++. */
5109 static inline bool
5110 is_cxx (void)
5112 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5114 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5115 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5118 /* Return TRUE if DECL was created by the C++ frontend. */
5120 static bool
5121 is_cxx (const_tree decl)
5123 if (in_lto_p)
5125 const_tree context = get_ultimate_context (decl);
5126 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5127 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5129 return is_cxx ();
5132 /* Return TRUE if the language is Fortran. */
5134 static inline bool
5135 is_fortran (void)
5137 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5139 return (lang == DW_LANG_Fortran77
5140 || lang == DW_LANG_Fortran90
5141 || lang == DW_LANG_Fortran95
5142 || lang == DW_LANG_Fortran03
5143 || lang == DW_LANG_Fortran08);
5146 static inline bool
5147 is_fortran (const_tree decl)
5149 if (in_lto_p)
5151 const_tree context = get_ultimate_context (decl);
5152 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5153 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5154 "GNU Fortran", 11) == 0
5155 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5156 "GNU F77") == 0);
5158 return is_fortran ();
5161 /* Return TRUE if the language is Ada. */
5163 static inline bool
5164 is_ada (void)
5166 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5168 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5171 /* Remove the specified attribute if present. Return TRUE if removal
5172 was successful. */
5174 static bool
5175 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5177 dw_attr_node *a;
5178 unsigned ix;
5180 if (! die)
5181 return false;
5183 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5184 if (a->dw_attr == attr_kind)
5186 if (AT_class (a) == dw_val_class_str)
5187 if (a->dw_attr_val.v.val_str->refcount)
5188 a->dw_attr_val.v.val_str->refcount--;
5190 /* vec::ordered_remove should help reduce the number of abbrevs
5191 that are needed. */
5192 die->die_attr->ordered_remove (ix);
5193 return true;
5195 return false;
5198 /* Remove CHILD from its parent. PREV must have the property that
5199 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5201 static void
5202 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5204 gcc_assert (child->die_parent == prev->die_parent);
5205 gcc_assert (prev->die_sib == child);
5206 if (prev == child)
5208 gcc_assert (child->die_parent->die_child == child);
5209 prev = NULL;
5211 else
5212 prev->die_sib = child->die_sib;
5213 if (child->die_parent->die_child == child)
5214 child->die_parent->die_child = prev;
5215 child->die_sib = NULL;
5218 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5219 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5221 static void
5222 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5224 dw_die_ref parent = old_child->die_parent;
5226 gcc_assert (parent == prev->die_parent);
5227 gcc_assert (prev->die_sib == old_child);
5229 new_child->die_parent = parent;
5230 if (prev == old_child)
5232 gcc_assert (parent->die_child == old_child);
5233 new_child->die_sib = new_child;
5235 else
5237 prev->die_sib = new_child;
5238 new_child->die_sib = old_child->die_sib;
5240 if (old_child->die_parent->die_child == old_child)
5241 old_child->die_parent->die_child = new_child;
5242 old_child->die_sib = NULL;
5245 /* Move all children from OLD_PARENT to NEW_PARENT. */
5247 static void
5248 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5250 dw_die_ref c;
5251 new_parent->die_child = old_parent->die_child;
5252 old_parent->die_child = NULL;
5253 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5256 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5257 matches TAG. */
5259 static void
5260 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5262 dw_die_ref c;
5264 c = die->die_child;
5265 if (c) do {
5266 dw_die_ref prev = c;
5267 c = c->die_sib;
5268 while (c->die_tag == tag)
5270 remove_child_with_prev (c, prev);
5271 c->die_parent = NULL;
5272 /* Might have removed every child. */
5273 if (die->die_child == NULL)
5274 return;
5275 c = prev->die_sib;
5277 } while (c != die->die_child);
5280 /* Add a CHILD_DIE as the last child of DIE. */
5282 static void
5283 add_child_die (dw_die_ref die, dw_die_ref child_die)
5285 /* FIXME this should probably be an assert. */
5286 if (! die || ! child_die)
5287 return;
5288 gcc_assert (die != child_die);
5290 child_die->die_parent = die;
5291 if (die->die_child)
5293 child_die->die_sib = die->die_child->die_sib;
5294 die->die_child->die_sib = child_die;
5296 else
5297 child_die->die_sib = child_die;
5298 die->die_child = child_die;
5301 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5303 static void
5304 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5305 dw_die_ref after_die)
5307 gcc_assert (die
5308 && child_die
5309 && after_die
5310 && die->die_child
5311 && die != child_die);
5313 child_die->die_parent = die;
5314 child_die->die_sib = after_die->die_sib;
5315 after_die->die_sib = child_die;
5316 if (die->die_child == after_die)
5317 die->die_child = child_die;
5320 /* Unassociate CHILD from its parent, and make its parent be
5321 NEW_PARENT. */
5323 static void
5324 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5326 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5327 if (p->die_sib == child)
5329 remove_child_with_prev (child, p);
5330 break;
5332 add_child_die (new_parent, child);
5335 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5336 is the specification, to the end of PARENT's list of children.
5337 This is done by removing and re-adding it. */
5339 static void
5340 splice_child_die (dw_die_ref parent, dw_die_ref child)
5342 /* We want the declaration DIE from inside the class, not the
5343 specification DIE at toplevel. */
5344 if (child->die_parent != parent)
5346 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5348 if (tmp)
5349 child = tmp;
5352 gcc_assert (child->die_parent == parent
5353 || (child->die_parent
5354 == get_AT_ref (parent, DW_AT_specification)));
5356 reparent_child (child, parent);
5359 /* Create and return a new die with a parent of PARENT_DIE. If
5360 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5361 associated tree T must be supplied to determine parenthood
5362 later. */
5364 static inline dw_die_ref
5365 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5367 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5369 die->die_tag = tag_value;
5371 if (parent_die != NULL)
5372 add_child_die (parent_die, die);
5373 else
5375 limbo_die_node *limbo_node;
5377 /* No DIEs created after early dwarf should end up in limbo,
5378 because the limbo list should not persist past LTO
5379 streaming. */
5380 if (tag_value != DW_TAG_compile_unit
5381 /* These are allowed because they're generated while
5382 breaking out COMDAT units late. */
5383 && tag_value != DW_TAG_type_unit
5384 && tag_value != DW_TAG_skeleton_unit
5385 && !early_dwarf
5386 /* Allow nested functions to live in limbo because they will
5387 only temporarily live there, as decls_for_scope will fix
5388 them up. */
5389 && (TREE_CODE (t) != FUNCTION_DECL
5390 || !decl_function_context (t))
5391 /* Same as nested functions above but for types. Types that
5392 are local to a function will be fixed in
5393 decls_for_scope. */
5394 && (!RECORD_OR_UNION_TYPE_P (t)
5395 || !TYPE_CONTEXT (t)
5396 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5397 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5398 especially in the ltrans stage, but once we implement LTO
5399 dwarf streaming, we should remove this exception. */
5400 && !in_lto_p)
5402 fprintf (stderr, "symbol ended up in limbo too late:");
5403 debug_generic_stmt (t);
5404 gcc_unreachable ();
5407 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5408 limbo_node->die = die;
5409 limbo_node->created_for = t;
5410 limbo_node->next = limbo_die_list;
5411 limbo_die_list = limbo_node;
5414 return die;
5417 /* Return the DIE associated with the given type specifier. */
5419 static inline dw_die_ref
5420 lookup_type_die (tree type)
5422 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5423 if (die && die->removed)
5425 TYPE_SYMTAB_DIE (type) = NULL;
5426 return NULL;
5428 return die;
5431 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5432 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5433 anonymous type instead the one of the naming typedef. */
5435 static inline dw_die_ref
5436 strip_naming_typedef (tree type, dw_die_ref type_die)
5438 if (type
5439 && TREE_CODE (type) == RECORD_TYPE
5440 && type_die
5441 && type_die->die_tag == DW_TAG_typedef
5442 && is_naming_typedef_decl (TYPE_NAME (type)))
5443 type_die = get_AT_ref (type_die, DW_AT_type);
5444 return type_die;
5447 /* Like lookup_type_die, but if type is an anonymous type named by a
5448 typedef[1], return the DIE of the anonymous type instead the one of
5449 the naming typedef. This is because in gen_typedef_die, we did
5450 equate the anonymous struct named by the typedef with the DIE of
5451 the naming typedef. So by default, lookup_type_die on an anonymous
5452 struct yields the DIE of the naming typedef.
5454 [1]: Read the comment of is_naming_typedef_decl to learn about what
5455 a naming typedef is. */
5457 static inline dw_die_ref
5458 lookup_type_die_strip_naming_typedef (tree type)
5460 dw_die_ref die = lookup_type_die (type);
5461 return strip_naming_typedef (type, die);
5464 /* Equate a DIE to a given type specifier. */
5466 static inline void
5467 equate_type_number_to_die (tree type, dw_die_ref type_die)
5469 TYPE_SYMTAB_DIE (type) = type_die;
5472 /* Returns a hash value for X (which really is a die_struct). */
5474 inline hashval_t
5475 decl_die_hasher::hash (die_node *x)
5477 return (hashval_t) x->decl_id;
5480 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5482 inline bool
5483 decl_die_hasher::equal (die_node *x, tree y)
5485 return (x->decl_id == DECL_UID (y));
5488 /* Return the DIE associated with a given declaration. */
5490 static inline dw_die_ref
5491 lookup_decl_die (tree decl)
5493 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5494 NO_INSERT);
5495 if (!die)
5496 return NULL;
5497 if ((*die)->removed)
5499 decl_die_table->clear_slot (die);
5500 return NULL;
5502 return *die;
5506 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5507 style reference. Return true if we found one refering to a DIE for
5508 DECL, otherwise return false. */
5510 static bool
5511 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5512 unsigned HOST_WIDE_INT *off)
5514 dw_die_ref die;
5516 if (flag_wpa && !decl_die_table)
5517 return false;
5519 if (TREE_CODE (decl) == BLOCK)
5520 die = BLOCK_DIE (decl);
5521 else
5522 die = lookup_decl_die (decl);
5523 if (!die)
5524 return false;
5526 /* During WPA stage we currently use DIEs to store the
5527 decl <-> label + offset map. That's quite inefficient but it
5528 works for now. */
5529 if (flag_wpa)
5531 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5532 if (!ref)
5534 gcc_assert (die == comp_unit_die ());
5535 return false;
5537 *off = ref->die_offset;
5538 *sym = ref->die_id.die_symbol;
5539 return true;
5542 /* Similar to get_ref_die_offset_label, but using the "correct"
5543 label. */
5544 *off = die->die_offset;
5545 while (die->die_parent)
5546 die = die->die_parent;
5547 /* For the containing CU DIE we compute a die_symbol in
5548 compute_section_prefix. */
5549 gcc_assert (die->die_tag == DW_TAG_compile_unit
5550 && die->die_id.die_symbol != NULL);
5551 *sym = die->die_id.die_symbol;
5552 return true;
5555 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5557 static void
5558 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5559 const char *symbol, HOST_WIDE_INT offset)
5561 /* Create a fake DIE that contains the reference. Don't use
5562 new_die because we don't want to end up in the limbo list. */
5563 dw_die_ref ref = ggc_cleared_alloc<die_node> ();
5564 ref->die_tag = die->die_tag;
5565 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5566 ref->die_offset = offset;
5567 ref->with_offset = 1;
5568 add_AT_die_ref (die, attr_kind, ref);
5571 /* Create a DIE for DECL if required and add a reference to a DIE
5572 at SYMBOL + OFFSET which contains attributes dumped early. */
5574 static void
5575 dwarf2out_register_external_die (tree decl, const char *sym,
5576 unsigned HOST_WIDE_INT off)
5578 if (debug_info_level == DINFO_LEVEL_NONE)
5579 return;
5581 if (flag_wpa && !decl_die_table)
5582 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5584 dw_die_ref die
5585 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5586 gcc_assert (!die);
5588 tree ctx;
5589 dw_die_ref parent = NULL;
5590 /* Need to lookup a DIE for the decls context - the containing
5591 function or translation unit. */
5592 if (TREE_CODE (decl) == BLOCK)
5594 ctx = BLOCK_SUPERCONTEXT (decl);
5595 /* ??? We do not output DIEs for all scopes thus skip as
5596 many DIEs as needed. */
5597 while (TREE_CODE (ctx) == BLOCK
5598 && !BLOCK_DIE (ctx))
5599 ctx = BLOCK_SUPERCONTEXT (ctx);
5601 else
5602 ctx = DECL_CONTEXT (decl);
5603 while (ctx && TYPE_P (ctx))
5604 ctx = TYPE_CONTEXT (ctx);
5605 if (ctx)
5607 if (TREE_CODE (ctx) == BLOCK)
5608 parent = BLOCK_DIE (ctx);
5609 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5610 /* Keep the 1:1 association during WPA. */
5611 && !flag_wpa)
5612 /* Otherwise all late annotations go to the main CU which
5613 imports the original CUs. */
5614 parent = comp_unit_die ();
5615 else if (TREE_CODE (ctx) == FUNCTION_DECL
5616 && TREE_CODE (decl) != PARM_DECL
5617 && TREE_CODE (decl) != BLOCK)
5618 /* Leave function local entities parent determination to when
5619 we process scope vars. */
5621 else
5622 parent = lookup_decl_die (ctx);
5624 else
5625 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5626 Handle this case gracefully by globalizing stuff. */
5627 parent = comp_unit_die ();
5628 /* Create a DIE "stub". */
5629 switch (TREE_CODE (decl))
5631 case TRANSLATION_UNIT_DECL:
5632 if (! flag_wpa)
5634 die = comp_unit_die ();
5635 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5636 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5637 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5638 to create a DIE for the original CUs. */
5639 return;
5641 /* Keep the 1:1 association during WPA. */
5642 die = new_die (DW_TAG_compile_unit, NULL, decl);
5643 break;
5644 case NAMESPACE_DECL:
5645 if (is_fortran (decl))
5646 die = new_die (DW_TAG_module, parent, decl);
5647 else
5648 die = new_die (DW_TAG_namespace, parent, decl);
5649 break;
5650 case FUNCTION_DECL:
5651 die = new_die (DW_TAG_subprogram, parent, decl);
5652 break;
5653 case VAR_DECL:
5654 die = new_die (DW_TAG_variable, parent, decl);
5655 break;
5656 case RESULT_DECL:
5657 die = new_die (DW_TAG_variable, parent, decl);
5658 break;
5659 case PARM_DECL:
5660 die = new_die (DW_TAG_formal_parameter, parent, decl);
5661 break;
5662 case CONST_DECL:
5663 die = new_die (DW_TAG_constant, parent, decl);
5664 break;
5665 case LABEL_DECL:
5666 die = new_die (DW_TAG_label, parent, decl);
5667 break;
5668 case BLOCK:
5669 die = new_die (DW_TAG_lexical_block, parent, decl);
5670 break;
5671 default:
5672 gcc_unreachable ();
5674 if (TREE_CODE (decl) == BLOCK)
5675 BLOCK_DIE (decl) = die;
5676 else
5677 equate_decl_number_to_die (decl, die);
5679 /* Add a reference to the DIE providing early debug at $sym + off. */
5680 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5683 /* Returns a hash value for X (which really is a var_loc_list). */
5685 inline hashval_t
5686 decl_loc_hasher::hash (var_loc_list *x)
5688 return (hashval_t) x->decl_id;
5691 /* Return nonzero if decl_id of var_loc_list X is the same as
5692 UID of decl *Y. */
5694 inline bool
5695 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5697 return (x->decl_id == DECL_UID (y));
5700 /* Return the var_loc list associated with a given declaration. */
5702 static inline var_loc_list *
5703 lookup_decl_loc (const_tree decl)
5705 if (!decl_loc_table)
5706 return NULL;
5707 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5710 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5712 inline hashval_t
5713 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5715 return (hashval_t) x->decl_id;
5718 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5719 UID of decl *Y. */
5721 inline bool
5722 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5724 return (x->decl_id == DECL_UID (y));
5727 /* Equate a DIE to a particular declaration. */
5729 static void
5730 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5732 unsigned int decl_id = DECL_UID (decl);
5734 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5735 decl_die->decl_id = decl_id;
5738 /* Return how many bits covers PIECE EXPR_LIST. */
5740 static HOST_WIDE_INT
5741 decl_piece_bitsize (rtx piece)
5743 int ret = (int) GET_MODE (piece);
5744 if (ret)
5745 return ret;
5746 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5747 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5748 return INTVAL (XEXP (XEXP (piece, 0), 0));
5751 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5753 static rtx *
5754 decl_piece_varloc_ptr (rtx piece)
5756 if ((int) GET_MODE (piece))
5757 return &XEXP (piece, 0);
5758 else
5759 return &XEXP (XEXP (piece, 0), 1);
5762 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5763 Next is the chain of following piece nodes. */
5765 static rtx_expr_list *
5766 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5768 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5769 return alloc_EXPR_LIST (bitsize, loc_note, next);
5770 else
5771 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5772 GEN_INT (bitsize),
5773 loc_note), next);
5776 /* Return rtx that should be stored into loc field for
5777 LOC_NOTE and BITPOS/BITSIZE. */
5779 static rtx
5780 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5781 HOST_WIDE_INT bitsize)
5783 if (bitsize != -1)
5785 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5786 if (bitpos != 0)
5787 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5789 return loc_note;
5792 /* This function either modifies location piece list *DEST in
5793 place (if SRC and INNER is NULL), or copies location piece list
5794 *SRC to *DEST while modifying it. Location BITPOS is modified
5795 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5796 not copied and if needed some padding around it is added.
5797 When modifying in place, DEST should point to EXPR_LIST where
5798 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5799 to the start of the whole list and INNER points to the EXPR_LIST
5800 where earlier pieces cover PIECE_BITPOS bits. */
5802 static void
5803 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5804 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5805 HOST_WIDE_INT bitsize, rtx loc_note)
5807 HOST_WIDE_INT diff;
5808 bool copy = inner != NULL;
5810 if (copy)
5812 /* First copy all nodes preceding the current bitpos. */
5813 while (src != inner)
5815 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5816 decl_piece_bitsize (*src), NULL_RTX);
5817 dest = &XEXP (*dest, 1);
5818 src = &XEXP (*src, 1);
5821 /* Add padding if needed. */
5822 if (bitpos != piece_bitpos)
5824 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5825 copy ? NULL_RTX : *dest);
5826 dest = &XEXP (*dest, 1);
5828 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5830 gcc_assert (!copy);
5831 /* A piece with correct bitpos and bitsize already exist,
5832 just update the location for it and return. */
5833 *decl_piece_varloc_ptr (*dest) = loc_note;
5834 return;
5836 /* Add the piece that changed. */
5837 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5838 dest = &XEXP (*dest, 1);
5839 /* Skip over pieces that overlap it. */
5840 diff = bitpos - piece_bitpos + bitsize;
5841 if (!copy)
5842 src = dest;
5843 while (diff > 0 && *src)
5845 rtx piece = *src;
5846 diff -= decl_piece_bitsize (piece);
5847 if (copy)
5848 src = &XEXP (piece, 1);
5849 else
5851 *src = XEXP (piece, 1);
5852 free_EXPR_LIST_node (piece);
5855 /* Add padding if needed. */
5856 if (diff < 0 && *src)
5858 if (!copy)
5859 dest = src;
5860 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5861 dest = &XEXP (*dest, 1);
5863 if (!copy)
5864 return;
5865 /* Finally copy all nodes following it. */
5866 while (*src)
5868 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5869 decl_piece_bitsize (*src), NULL_RTX);
5870 dest = &XEXP (*dest, 1);
5871 src = &XEXP (*src, 1);
5875 /* Add a variable location node to the linked list for DECL. */
5877 static struct var_loc_node *
5878 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5880 unsigned int decl_id;
5881 var_loc_list *temp;
5882 struct var_loc_node *loc = NULL;
5883 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5885 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5887 tree realdecl = DECL_DEBUG_EXPR (decl);
5888 if (handled_component_p (realdecl)
5889 || (TREE_CODE (realdecl) == MEM_REF
5890 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5892 HOST_WIDE_INT maxsize;
5893 bool reverse;
5894 tree innerdecl
5895 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5896 &reverse);
5897 if (!DECL_P (innerdecl)
5898 || DECL_IGNORED_P (innerdecl)
5899 || TREE_STATIC (innerdecl)
5900 || bitsize <= 0
5901 || bitpos + bitsize > 256
5902 || bitsize != maxsize)
5903 return NULL;
5904 decl = innerdecl;
5908 decl_id = DECL_UID (decl);
5909 var_loc_list **slot
5910 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5911 if (*slot == NULL)
5913 temp = ggc_cleared_alloc<var_loc_list> ();
5914 temp->decl_id = decl_id;
5915 *slot = temp;
5917 else
5918 temp = *slot;
5920 /* For PARM_DECLs try to keep around the original incoming value,
5921 even if that means we'll emit a zero-range .debug_loc entry. */
5922 if (temp->last
5923 && temp->first == temp->last
5924 && TREE_CODE (decl) == PARM_DECL
5925 && NOTE_P (temp->first->loc)
5926 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5927 && DECL_INCOMING_RTL (decl)
5928 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5929 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5930 == GET_CODE (DECL_INCOMING_RTL (decl))
5931 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5932 && (bitsize != -1
5933 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5934 NOTE_VAR_LOCATION_LOC (loc_note))
5935 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5936 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5938 loc = ggc_cleared_alloc<var_loc_node> ();
5939 temp->first->next = loc;
5940 temp->last = loc;
5941 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5943 else if (temp->last)
5945 struct var_loc_node *last = temp->last, *unused = NULL;
5946 rtx *piece_loc = NULL, last_loc_note;
5947 HOST_WIDE_INT piece_bitpos = 0;
5948 if (last->next)
5950 last = last->next;
5951 gcc_assert (last->next == NULL);
5953 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5955 piece_loc = &last->loc;
5958 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5959 if (piece_bitpos + cur_bitsize > bitpos)
5960 break;
5961 piece_bitpos += cur_bitsize;
5962 piece_loc = &XEXP (*piece_loc, 1);
5964 while (*piece_loc);
5966 /* TEMP->LAST here is either pointer to the last but one or
5967 last element in the chained list, LAST is pointer to the
5968 last element. */
5969 if (label && strcmp (last->label, label) == 0)
5971 /* For SRA optimized variables if there weren't any real
5972 insns since last note, just modify the last node. */
5973 if (piece_loc != NULL)
5975 adjust_piece_list (piece_loc, NULL, NULL,
5976 bitpos, piece_bitpos, bitsize, loc_note);
5977 return NULL;
5979 /* If the last note doesn't cover any instructions, remove it. */
5980 if (temp->last != last)
5982 temp->last->next = NULL;
5983 unused = last;
5984 last = temp->last;
5985 gcc_assert (strcmp (last->label, label) != 0);
5987 else
5989 gcc_assert (temp->first == temp->last
5990 || (temp->first->next == temp->last
5991 && TREE_CODE (decl) == PARM_DECL));
5992 memset (temp->last, '\0', sizeof (*temp->last));
5993 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5994 return temp->last;
5997 if (bitsize == -1 && NOTE_P (last->loc))
5998 last_loc_note = last->loc;
5999 else if (piece_loc != NULL
6000 && *piece_loc != NULL_RTX
6001 && piece_bitpos == bitpos
6002 && decl_piece_bitsize (*piece_loc) == bitsize)
6003 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6004 else
6005 last_loc_note = NULL_RTX;
6006 /* If the current location is the same as the end of the list,
6007 and either both or neither of the locations is uninitialized,
6008 we have nothing to do. */
6009 if (last_loc_note == NULL_RTX
6010 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6011 NOTE_VAR_LOCATION_LOC (loc_note)))
6012 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6013 != NOTE_VAR_LOCATION_STATUS (loc_note))
6014 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6015 == VAR_INIT_STATUS_UNINITIALIZED)
6016 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6017 == VAR_INIT_STATUS_UNINITIALIZED))))
6019 /* Add LOC to the end of list and update LAST. If the last
6020 element of the list has been removed above, reuse its
6021 memory for the new node, otherwise allocate a new one. */
6022 if (unused)
6024 loc = unused;
6025 memset (loc, '\0', sizeof (*loc));
6027 else
6028 loc = ggc_cleared_alloc<var_loc_node> ();
6029 if (bitsize == -1 || piece_loc == NULL)
6030 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6031 else
6032 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6033 bitpos, piece_bitpos, bitsize, loc_note);
6034 last->next = loc;
6035 /* Ensure TEMP->LAST will point either to the new last but one
6036 element of the chain, or to the last element in it. */
6037 if (last != temp->last)
6038 temp->last = last;
6040 else if (unused)
6041 ggc_free (unused);
6043 else
6045 loc = ggc_cleared_alloc<var_loc_node> ();
6046 temp->first = loc;
6047 temp->last = loc;
6048 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6050 return loc;
6053 /* Keep track of the number of spaces used to indent the
6054 output of the debugging routines that print the structure of
6055 the DIE internal representation. */
6056 static int print_indent;
6058 /* Indent the line the number of spaces given by print_indent. */
6060 static inline void
6061 print_spaces (FILE *outfile)
6063 fprintf (outfile, "%*s", print_indent, "");
6066 /* Print a type signature in hex. */
6068 static inline void
6069 print_signature (FILE *outfile, char *sig)
6071 int i;
6073 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6074 fprintf (outfile, "%02x", sig[i] & 0xff);
6077 static inline void
6078 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6080 if (discr_value->pos)
6081 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6082 else
6083 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6086 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6088 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6089 RECURSE, output location descriptor operations. */
6091 static void
6092 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6094 switch (val->val_class)
6096 case dw_val_class_addr:
6097 fprintf (outfile, "address");
6098 break;
6099 case dw_val_class_offset:
6100 fprintf (outfile, "offset");
6101 break;
6102 case dw_val_class_loc:
6103 fprintf (outfile, "location descriptor");
6104 if (val->v.val_loc == NULL)
6105 fprintf (outfile, " -> <null>\n");
6106 else if (recurse)
6108 fprintf (outfile, ":\n");
6109 print_indent += 4;
6110 print_loc_descr (val->v.val_loc, outfile);
6111 print_indent -= 4;
6113 else
6114 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6115 break;
6116 case dw_val_class_loc_list:
6117 fprintf (outfile, "location list -> label:%s",
6118 val->v.val_loc_list->ll_symbol);
6119 break;
6120 case dw_val_class_range_list:
6121 fprintf (outfile, "range list");
6122 break;
6123 case dw_val_class_const:
6124 case dw_val_class_const_implicit:
6125 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6126 break;
6127 case dw_val_class_unsigned_const:
6128 case dw_val_class_unsigned_const_implicit:
6129 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6130 break;
6131 case dw_val_class_const_double:
6132 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6133 HOST_WIDE_INT_PRINT_UNSIGNED")",
6134 val->v.val_double.high,
6135 val->v.val_double.low);
6136 break;
6137 case dw_val_class_wide_int:
6139 int i = val->v.val_wide->get_len ();
6140 fprintf (outfile, "constant (");
6141 gcc_assert (i > 0);
6142 if (val->v.val_wide->elt (i - 1) == 0)
6143 fprintf (outfile, "0x");
6144 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6145 val->v.val_wide->elt (--i));
6146 while (--i >= 0)
6147 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6148 val->v.val_wide->elt (i));
6149 fprintf (outfile, ")");
6150 break;
6152 case dw_val_class_vec:
6153 fprintf (outfile, "floating-point or vector constant");
6154 break;
6155 case dw_val_class_flag:
6156 fprintf (outfile, "%u", val->v.val_flag);
6157 break;
6158 case dw_val_class_die_ref:
6159 if (val->v.val_die_ref.die != NULL)
6161 dw_die_ref die = val->v.val_die_ref.die;
6163 if (die->comdat_type_p)
6165 fprintf (outfile, "die -> signature: ");
6166 print_signature (outfile,
6167 die->die_id.die_type_node->signature);
6169 else if (die->die_id.die_symbol)
6171 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6172 if (die->with_offset)
6173 fprintf (outfile, " + %ld", die->die_offset);
6175 else
6176 fprintf (outfile, "die -> %ld", die->die_offset);
6177 fprintf (outfile, " (%p)", (void *) die);
6179 else
6180 fprintf (outfile, "die -> <null>");
6181 break;
6182 case dw_val_class_vms_delta:
6183 fprintf (outfile, "delta: @slotcount(%s-%s)",
6184 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6185 break;
6186 case dw_val_class_lbl_id:
6187 case dw_val_class_lineptr:
6188 case dw_val_class_macptr:
6189 case dw_val_class_loclistsptr:
6190 case dw_val_class_high_pc:
6191 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6192 break;
6193 case dw_val_class_str:
6194 if (val->v.val_str->str != NULL)
6195 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6196 else
6197 fprintf (outfile, "<null>");
6198 break;
6199 case dw_val_class_file:
6200 case dw_val_class_file_implicit:
6201 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6202 val->v.val_file->emitted_number);
6203 break;
6204 case dw_val_class_data8:
6206 int i;
6208 for (i = 0; i < 8; i++)
6209 fprintf (outfile, "%02x", val->v.val_data8[i]);
6210 break;
6212 case dw_val_class_discr_value:
6213 print_discr_value (outfile, &val->v.val_discr_value);
6214 break;
6215 case dw_val_class_discr_list:
6216 for (dw_discr_list_ref node = val->v.val_discr_list;
6217 node != NULL;
6218 node = node->dw_discr_next)
6220 if (node->dw_discr_range)
6222 fprintf (outfile, " .. ");
6223 print_discr_value (outfile, &node->dw_discr_lower_bound);
6224 print_discr_value (outfile, &node->dw_discr_upper_bound);
6226 else
6227 print_discr_value (outfile, &node->dw_discr_lower_bound);
6229 if (node->dw_discr_next != NULL)
6230 fprintf (outfile, " | ");
6232 default:
6233 break;
6237 /* Likewise, for a DIE attribute. */
6239 static void
6240 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6242 print_dw_val (&a->dw_attr_val, recurse, outfile);
6246 /* Print the list of operands in the LOC location description to OUTFILE. This
6247 routine is a debugging aid only. */
6249 static void
6250 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6252 dw_loc_descr_ref l = loc;
6254 if (loc == NULL)
6256 print_spaces (outfile);
6257 fprintf (outfile, "<null>\n");
6258 return;
6261 for (l = loc; l != NULL; l = l->dw_loc_next)
6263 print_spaces (outfile);
6264 fprintf (outfile, "(%p) %s",
6265 (void *) l,
6266 dwarf_stack_op_name (l->dw_loc_opc));
6267 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6269 fprintf (outfile, " ");
6270 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6272 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6274 fprintf (outfile, ", ");
6275 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6277 fprintf (outfile, "\n");
6281 /* Print the information associated with a given DIE, and its children.
6282 This routine is a debugging aid only. */
6284 static void
6285 print_die (dw_die_ref die, FILE *outfile)
6287 dw_attr_node *a;
6288 dw_die_ref c;
6289 unsigned ix;
6291 print_spaces (outfile);
6292 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6293 die->die_offset, dwarf_tag_name (die->die_tag),
6294 (void*) die);
6295 print_spaces (outfile);
6296 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6297 fprintf (outfile, " offset: %ld", die->die_offset);
6298 fprintf (outfile, " mark: %d\n", die->die_mark);
6300 if (die->comdat_type_p)
6302 print_spaces (outfile);
6303 fprintf (outfile, " signature: ");
6304 print_signature (outfile, die->die_id.die_type_node->signature);
6305 fprintf (outfile, "\n");
6308 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6310 print_spaces (outfile);
6311 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6313 print_attribute (a, true, outfile);
6314 fprintf (outfile, "\n");
6317 if (die->die_child != NULL)
6319 print_indent += 4;
6320 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6321 print_indent -= 4;
6323 if (print_indent == 0)
6324 fprintf (outfile, "\n");
6327 /* Print the list of operations in the LOC location description. */
6329 DEBUG_FUNCTION void
6330 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6332 print_loc_descr (loc, stderr);
6335 /* Print the information collected for a given DIE. */
6337 DEBUG_FUNCTION void
6338 debug_dwarf_die (dw_die_ref die)
6340 print_die (die, stderr);
6343 DEBUG_FUNCTION void
6344 debug (die_struct &ref)
6346 print_die (&ref, stderr);
6349 DEBUG_FUNCTION void
6350 debug (die_struct *ptr)
6352 if (ptr)
6353 debug (*ptr);
6354 else
6355 fprintf (stderr, "<nil>\n");
6359 /* Print all DWARF information collected for the compilation unit.
6360 This routine is a debugging aid only. */
6362 DEBUG_FUNCTION void
6363 debug_dwarf (void)
6365 print_indent = 0;
6366 print_die (comp_unit_die (), stderr);
6369 /* Verify the DIE tree structure. */
6371 DEBUG_FUNCTION void
6372 verify_die (dw_die_ref die)
6374 gcc_assert (!die->die_mark);
6375 if (die->die_parent == NULL
6376 && die->die_sib == NULL)
6377 return;
6378 /* Verify the die_sib list is cyclic. */
6379 dw_die_ref x = die;
6382 x->die_mark = 1;
6383 x = x->die_sib;
6385 while (x && !x->die_mark);
6386 gcc_assert (x == die);
6387 x = die;
6390 /* Verify all dies have the same parent. */
6391 gcc_assert (x->die_parent == die->die_parent);
6392 if (x->die_child)
6394 /* Verify the child has the proper parent and recurse. */
6395 gcc_assert (x->die_child->die_parent == x);
6396 verify_die (x->die_child);
6398 x->die_mark = 0;
6399 x = x->die_sib;
6401 while (x && x->die_mark);
6404 /* Sanity checks on DIEs. */
6406 static void
6407 check_die (dw_die_ref die)
6409 unsigned ix;
6410 dw_attr_node *a;
6411 bool inline_found = false;
6412 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6413 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6414 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6416 switch (a->dw_attr)
6418 case DW_AT_inline:
6419 if (a->dw_attr_val.v.val_unsigned)
6420 inline_found = true;
6421 break;
6422 case DW_AT_location:
6423 ++n_location;
6424 break;
6425 case DW_AT_low_pc:
6426 ++n_low_pc;
6427 break;
6428 case DW_AT_high_pc:
6429 ++n_high_pc;
6430 break;
6431 case DW_AT_artificial:
6432 ++n_artificial;
6433 break;
6434 case DW_AT_decl_column:
6435 ++n_decl_column;
6436 break;
6437 case DW_AT_decl_line:
6438 ++n_decl_line;
6439 break;
6440 case DW_AT_decl_file:
6441 ++n_decl_file;
6442 break;
6443 default:
6444 break;
6447 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6448 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6450 fprintf (stderr, "Duplicate attributes in DIE:\n");
6451 debug_dwarf_die (die);
6452 gcc_unreachable ();
6454 if (inline_found)
6456 /* A debugging information entry that is a member of an abstract
6457 instance tree [that has DW_AT_inline] should not contain any
6458 attributes which describe aspects of the subroutine which vary
6459 between distinct inlined expansions or distinct out-of-line
6460 expansions. */
6461 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6462 gcc_assert (a->dw_attr != DW_AT_low_pc
6463 && a->dw_attr != DW_AT_high_pc
6464 && a->dw_attr != DW_AT_location
6465 && a->dw_attr != DW_AT_frame_base
6466 && a->dw_attr != DW_AT_call_all_calls
6467 && a->dw_attr != DW_AT_GNU_all_call_sites);
6471 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6472 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6473 DIE that marks the start of the DIEs for this include file. */
6475 static dw_die_ref
6476 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6478 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6479 dw_die_ref new_unit = gen_compile_unit_die (filename);
6481 new_unit->die_sib = old_unit;
6482 return new_unit;
6485 /* Close an include-file CU and reopen the enclosing one. */
6487 static dw_die_ref
6488 pop_compile_unit (dw_die_ref old_unit)
6490 dw_die_ref new_unit = old_unit->die_sib;
6492 old_unit->die_sib = NULL;
6493 return new_unit;
6496 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6497 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6498 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6500 /* Calculate the checksum of a location expression. */
6502 static inline void
6503 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6505 int tem;
6506 inchash::hash hstate;
6507 hashval_t hash;
6509 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6510 CHECKSUM (tem);
6511 hash_loc_operands (loc, hstate);
6512 hash = hstate.end();
6513 CHECKSUM (hash);
6516 /* Calculate the checksum of an attribute. */
6518 static void
6519 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6521 dw_loc_descr_ref loc;
6522 rtx r;
6524 CHECKSUM (at->dw_attr);
6526 /* We don't care that this was compiled with a different compiler
6527 snapshot; if the output is the same, that's what matters. */
6528 if (at->dw_attr == DW_AT_producer)
6529 return;
6531 switch (AT_class (at))
6533 case dw_val_class_const:
6534 case dw_val_class_const_implicit:
6535 CHECKSUM (at->dw_attr_val.v.val_int);
6536 break;
6537 case dw_val_class_unsigned_const:
6538 case dw_val_class_unsigned_const_implicit:
6539 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6540 break;
6541 case dw_val_class_const_double:
6542 CHECKSUM (at->dw_attr_val.v.val_double);
6543 break;
6544 case dw_val_class_wide_int:
6545 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6546 get_full_len (*at->dw_attr_val.v.val_wide)
6547 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6548 break;
6549 case dw_val_class_vec:
6550 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6551 (at->dw_attr_val.v.val_vec.length
6552 * at->dw_attr_val.v.val_vec.elt_size));
6553 break;
6554 case dw_val_class_flag:
6555 CHECKSUM (at->dw_attr_val.v.val_flag);
6556 break;
6557 case dw_val_class_str:
6558 CHECKSUM_STRING (AT_string (at));
6559 break;
6561 case dw_val_class_addr:
6562 r = AT_addr (at);
6563 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6564 CHECKSUM_STRING (XSTR (r, 0));
6565 break;
6567 case dw_val_class_offset:
6568 CHECKSUM (at->dw_attr_val.v.val_offset);
6569 break;
6571 case dw_val_class_loc:
6572 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6573 loc_checksum (loc, ctx);
6574 break;
6576 case dw_val_class_die_ref:
6577 die_checksum (AT_ref (at), ctx, mark);
6578 break;
6580 case dw_val_class_fde_ref:
6581 case dw_val_class_vms_delta:
6582 case dw_val_class_lbl_id:
6583 case dw_val_class_lineptr:
6584 case dw_val_class_macptr:
6585 case dw_val_class_loclistsptr:
6586 case dw_val_class_high_pc:
6587 break;
6589 case dw_val_class_file:
6590 case dw_val_class_file_implicit:
6591 CHECKSUM_STRING (AT_file (at)->filename);
6592 break;
6594 case dw_val_class_data8:
6595 CHECKSUM (at->dw_attr_val.v.val_data8);
6596 break;
6598 default:
6599 break;
6603 /* Calculate the checksum of a DIE. */
6605 static void
6606 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6608 dw_die_ref c;
6609 dw_attr_node *a;
6610 unsigned ix;
6612 /* To avoid infinite recursion. */
6613 if (die->die_mark)
6615 CHECKSUM (die->die_mark);
6616 return;
6618 die->die_mark = ++(*mark);
6620 CHECKSUM (die->die_tag);
6622 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6623 attr_checksum (a, ctx, mark);
6625 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6628 #undef CHECKSUM
6629 #undef CHECKSUM_BLOCK
6630 #undef CHECKSUM_STRING
6632 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6633 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6634 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6635 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6636 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6637 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6638 #define CHECKSUM_ATTR(FOO) \
6639 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6641 /* Calculate the checksum of a number in signed LEB128 format. */
6643 static void
6644 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6646 unsigned char byte;
6647 bool more;
6649 while (1)
6651 byte = (value & 0x7f);
6652 value >>= 7;
6653 more = !((value == 0 && (byte & 0x40) == 0)
6654 || (value == -1 && (byte & 0x40) != 0));
6655 if (more)
6656 byte |= 0x80;
6657 CHECKSUM (byte);
6658 if (!more)
6659 break;
6663 /* Calculate the checksum of a number in unsigned LEB128 format. */
6665 static void
6666 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6668 while (1)
6670 unsigned char byte = (value & 0x7f);
6671 value >>= 7;
6672 if (value != 0)
6673 /* More bytes to follow. */
6674 byte |= 0x80;
6675 CHECKSUM (byte);
6676 if (value == 0)
6677 break;
6681 /* Checksum the context of the DIE. This adds the names of any
6682 surrounding namespaces or structures to the checksum. */
6684 static void
6685 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6687 const char *name;
6688 dw_die_ref spec;
6689 int tag = die->die_tag;
6691 if (tag != DW_TAG_namespace
6692 && tag != DW_TAG_structure_type
6693 && tag != DW_TAG_class_type)
6694 return;
6696 name = get_AT_string (die, DW_AT_name);
6698 spec = get_AT_ref (die, DW_AT_specification);
6699 if (spec != NULL)
6700 die = spec;
6702 if (die->die_parent != NULL)
6703 checksum_die_context (die->die_parent, ctx);
6705 CHECKSUM_ULEB128 ('C');
6706 CHECKSUM_ULEB128 (tag);
6707 if (name != NULL)
6708 CHECKSUM_STRING (name);
6711 /* Calculate the checksum of a location expression. */
6713 static inline void
6714 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6716 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6717 were emitted as a DW_FORM_sdata instead of a location expression. */
6718 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6720 CHECKSUM_ULEB128 (DW_FORM_sdata);
6721 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6722 return;
6725 /* Otherwise, just checksum the raw location expression. */
6726 while (loc != NULL)
6728 inchash::hash hstate;
6729 hashval_t hash;
6731 CHECKSUM_ULEB128 (loc->dtprel);
6732 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6733 hash_loc_operands (loc, hstate);
6734 hash = hstate.end ();
6735 CHECKSUM (hash);
6736 loc = loc->dw_loc_next;
6740 /* Calculate the checksum of an attribute. */
6742 static void
6743 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6744 struct md5_ctx *ctx, int *mark)
6746 dw_loc_descr_ref loc;
6747 rtx r;
6749 if (AT_class (at) == dw_val_class_die_ref)
6751 dw_die_ref target_die = AT_ref (at);
6753 /* For pointer and reference types, we checksum only the (qualified)
6754 name of the target type (if there is a name). For friend entries,
6755 we checksum only the (qualified) name of the target type or function.
6756 This allows the checksum to remain the same whether the target type
6757 is complete or not. */
6758 if ((at->dw_attr == DW_AT_type
6759 && (tag == DW_TAG_pointer_type
6760 || tag == DW_TAG_reference_type
6761 || tag == DW_TAG_rvalue_reference_type
6762 || tag == DW_TAG_ptr_to_member_type))
6763 || (at->dw_attr == DW_AT_friend
6764 && tag == DW_TAG_friend))
6766 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6768 if (name_attr != NULL)
6770 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6772 if (decl == NULL)
6773 decl = target_die;
6774 CHECKSUM_ULEB128 ('N');
6775 CHECKSUM_ULEB128 (at->dw_attr);
6776 if (decl->die_parent != NULL)
6777 checksum_die_context (decl->die_parent, ctx);
6778 CHECKSUM_ULEB128 ('E');
6779 CHECKSUM_STRING (AT_string (name_attr));
6780 return;
6784 /* For all other references to another DIE, we check to see if the
6785 target DIE has already been visited. If it has, we emit a
6786 backward reference; if not, we descend recursively. */
6787 if (target_die->die_mark > 0)
6789 CHECKSUM_ULEB128 ('R');
6790 CHECKSUM_ULEB128 (at->dw_attr);
6791 CHECKSUM_ULEB128 (target_die->die_mark);
6793 else
6795 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6797 if (decl == NULL)
6798 decl = target_die;
6799 target_die->die_mark = ++(*mark);
6800 CHECKSUM_ULEB128 ('T');
6801 CHECKSUM_ULEB128 (at->dw_attr);
6802 if (decl->die_parent != NULL)
6803 checksum_die_context (decl->die_parent, ctx);
6804 die_checksum_ordered (target_die, ctx, mark);
6806 return;
6809 CHECKSUM_ULEB128 ('A');
6810 CHECKSUM_ULEB128 (at->dw_attr);
6812 switch (AT_class (at))
6814 case dw_val_class_const:
6815 case dw_val_class_const_implicit:
6816 CHECKSUM_ULEB128 (DW_FORM_sdata);
6817 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6818 break;
6820 case dw_val_class_unsigned_const:
6821 case dw_val_class_unsigned_const_implicit:
6822 CHECKSUM_ULEB128 (DW_FORM_sdata);
6823 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6824 break;
6826 case dw_val_class_const_double:
6827 CHECKSUM_ULEB128 (DW_FORM_block);
6828 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6829 CHECKSUM (at->dw_attr_val.v.val_double);
6830 break;
6832 case dw_val_class_wide_int:
6833 CHECKSUM_ULEB128 (DW_FORM_block);
6834 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6835 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6836 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6837 get_full_len (*at->dw_attr_val.v.val_wide)
6838 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6839 break;
6841 case dw_val_class_vec:
6842 CHECKSUM_ULEB128 (DW_FORM_block);
6843 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6844 * at->dw_attr_val.v.val_vec.elt_size);
6845 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6846 (at->dw_attr_val.v.val_vec.length
6847 * at->dw_attr_val.v.val_vec.elt_size));
6848 break;
6850 case dw_val_class_flag:
6851 CHECKSUM_ULEB128 (DW_FORM_flag);
6852 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6853 break;
6855 case dw_val_class_str:
6856 CHECKSUM_ULEB128 (DW_FORM_string);
6857 CHECKSUM_STRING (AT_string (at));
6858 break;
6860 case dw_val_class_addr:
6861 r = AT_addr (at);
6862 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6863 CHECKSUM_ULEB128 (DW_FORM_string);
6864 CHECKSUM_STRING (XSTR (r, 0));
6865 break;
6867 case dw_val_class_offset:
6868 CHECKSUM_ULEB128 (DW_FORM_sdata);
6869 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6870 break;
6872 case dw_val_class_loc:
6873 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6874 loc_checksum_ordered (loc, ctx);
6875 break;
6877 case dw_val_class_fde_ref:
6878 case dw_val_class_lbl_id:
6879 case dw_val_class_lineptr:
6880 case dw_val_class_macptr:
6881 case dw_val_class_loclistsptr:
6882 case dw_val_class_high_pc:
6883 break;
6885 case dw_val_class_file:
6886 case dw_val_class_file_implicit:
6887 CHECKSUM_ULEB128 (DW_FORM_string);
6888 CHECKSUM_STRING (AT_file (at)->filename);
6889 break;
6891 case dw_val_class_data8:
6892 CHECKSUM (at->dw_attr_val.v.val_data8);
6893 break;
6895 default:
6896 break;
6900 struct checksum_attributes
6902 dw_attr_node *at_name;
6903 dw_attr_node *at_type;
6904 dw_attr_node *at_friend;
6905 dw_attr_node *at_accessibility;
6906 dw_attr_node *at_address_class;
6907 dw_attr_node *at_alignment;
6908 dw_attr_node *at_allocated;
6909 dw_attr_node *at_artificial;
6910 dw_attr_node *at_associated;
6911 dw_attr_node *at_binary_scale;
6912 dw_attr_node *at_bit_offset;
6913 dw_attr_node *at_bit_size;
6914 dw_attr_node *at_bit_stride;
6915 dw_attr_node *at_byte_size;
6916 dw_attr_node *at_byte_stride;
6917 dw_attr_node *at_const_value;
6918 dw_attr_node *at_containing_type;
6919 dw_attr_node *at_count;
6920 dw_attr_node *at_data_location;
6921 dw_attr_node *at_data_member_location;
6922 dw_attr_node *at_decimal_scale;
6923 dw_attr_node *at_decimal_sign;
6924 dw_attr_node *at_default_value;
6925 dw_attr_node *at_digit_count;
6926 dw_attr_node *at_discr;
6927 dw_attr_node *at_discr_list;
6928 dw_attr_node *at_discr_value;
6929 dw_attr_node *at_encoding;
6930 dw_attr_node *at_endianity;
6931 dw_attr_node *at_explicit;
6932 dw_attr_node *at_is_optional;
6933 dw_attr_node *at_location;
6934 dw_attr_node *at_lower_bound;
6935 dw_attr_node *at_mutable;
6936 dw_attr_node *at_ordering;
6937 dw_attr_node *at_picture_string;
6938 dw_attr_node *at_prototyped;
6939 dw_attr_node *at_small;
6940 dw_attr_node *at_segment;
6941 dw_attr_node *at_string_length;
6942 dw_attr_node *at_string_length_bit_size;
6943 dw_attr_node *at_string_length_byte_size;
6944 dw_attr_node *at_threads_scaled;
6945 dw_attr_node *at_upper_bound;
6946 dw_attr_node *at_use_location;
6947 dw_attr_node *at_use_UTF8;
6948 dw_attr_node *at_variable_parameter;
6949 dw_attr_node *at_virtuality;
6950 dw_attr_node *at_visibility;
6951 dw_attr_node *at_vtable_elem_location;
6954 /* Collect the attributes that we will want to use for the checksum. */
6956 static void
6957 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6959 dw_attr_node *a;
6960 unsigned ix;
6962 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6964 switch (a->dw_attr)
6966 case DW_AT_name:
6967 attrs->at_name = a;
6968 break;
6969 case DW_AT_type:
6970 attrs->at_type = a;
6971 break;
6972 case DW_AT_friend:
6973 attrs->at_friend = a;
6974 break;
6975 case DW_AT_accessibility:
6976 attrs->at_accessibility = a;
6977 break;
6978 case DW_AT_address_class:
6979 attrs->at_address_class = a;
6980 break;
6981 case DW_AT_alignment:
6982 attrs->at_alignment = a;
6983 break;
6984 case DW_AT_allocated:
6985 attrs->at_allocated = a;
6986 break;
6987 case DW_AT_artificial:
6988 attrs->at_artificial = a;
6989 break;
6990 case DW_AT_associated:
6991 attrs->at_associated = a;
6992 break;
6993 case DW_AT_binary_scale:
6994 attrs->at_binary_scale = a;
6995 break;
6996 case DW_AT_bit_offset:
6997 attrs->at_bit_offset = a;
6998 break;
6999 case DW_AT_bit_size:
7000 attrs->at_bit_size = a;
7001 break;
7002 case DW_AT_bit_stride:
7003 attrs->at_bit_stride = a;
7004 break;
7005 case DW_AT_byte_size:
7006 attrs->at_byte_size = a;
7007 break;
7008 case DW_AT_byte_stride:
7009 attrs->at_byte_stride = a;
7010 break;
7011 case DW_AT_const_value:
7012 attrs->at_const_value = a;
7013 break;
7014 case DW_AT_containing_type:
7015 attrs->at_containing_type = a;
7016 break;
7017 case DW_AT_count:
7018 attrs->at_count = a;
7019 break;
7020 case DW_AT_data_location:
7021 attrs->at_data_location = a;
7022 break;
7023 case DW_AT_data_member_location:
7024 attrs->at_data_member_location = a;
7025 break;
7026 case DW_AT_decimal_scale:
7027 attrs->at_decimal_scale = a;
7028 break;
7029 case DW_AT_decimal_sign:
7030 attrs->at_decimal_sign = a;
7031 break;
7032 case DW_AT_default_value:
7033 attrs->at_default_value = a;
7034 break;
7035 case DW_AT_digit_count:
7036 attrs->at_digit_count = a;
7037 break;
7038 case DW_AT_discr:
7039 attrs->at_discr = a;
7040 break;
7041 case DW_AT_discr_list:
7042 attrs->at_discr_list = a;
7043 break;
7044 case DW_AT_discr_value:
7045 attrs->at_discr_value = a;
7046 break;
7047 case DW_AT_encoding:
7048 attrs->at_encoding = a;
7049 break;
7050 case DW_AT_endianity:
7051 attrs->at_endianity = a;
7052 break;
7053 case DW_AT_explicit:
7054 attrs->at_explicit = a;
7055 break;
7056 case DW_AT_is_optional:
7057 attrs->at_is_optional = a;
7058 break;
7059 case DW_AT_location:
7060 attrs->at_location = a;
7061 break;
7062 case DW_AT_lower_bound:
7063 attrs->at_lower_bound = a;
7064 break;
7065 case DW_AT_mutable:
7066 attrs->at_mutable = a;
7067 break;
7068 case DW_AT_ordering:
7069 attrs->at_ordering = a;
7070 break;
7071 case DW_AT_picture_string:
7072 attrs->at_picture_string = a;
7073 break;
7074 case DW_AT_prototyped:
7075 attrs->at_prototyped = a;
7076 break;
7077 case DW_AT_small:
7078 attrs->at_small = a;
7079 break;
7080 case DW_AT_segment:
7081 attrs->at_segment = a;
7082 break;
7083 case DW_AT_string_length:
7084 attrs->at_string_length = a;
7085 break;
7086 case DW_AT_string_length_bit_size:
7087 attrs->at_string_length_bit_size = a;
7088 break;
7089 case DW_AT_string_length_byte_size:
7090 attrs->at_string_length_byte_size = a;
7091 break;
7092 case DW_AT_threads_scaled:
7093 attrs->at_threads_scaled = a;
7094 break;
7095 case DW_AT_upper_bound:
7096 attrs->at_upper_bound = a;
7097 break;
7098 case DW_AT_use_location:
7099 attrs->at_use_location = a;
7100 break;
7101 case DW_AT_use_UTF8:
7102 attrs->at_use_UTF8 = a;
7103 break;
7104 case DW_AT_variable_parameter:
7105 attrs->at_variable_parameter = a;
7106 break;
7107 case DW_AT_virtuality:
7108 attrs->at_virtuality = a;
7109 break;
7110 case DW_AT_visibility:
7111 attrs->at_visibility = a;
7112 break;
7113 case DW_AT_vtable_elem_location:
7114 attrs->at_vtable_elem_location = a;
7115 break;
7116 default:
7117 break;
7122 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7124 static void
7125 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7127 dw_die_ref c;
7128 dw_die_ref decl;
7129 struct checksum_attributes attrs;
7131 CHECKSUM_ULEB128 ('D');
7132 CHECKSUM_ULEB128 (die->die_tag);
7134 memset (&attrs, 0, sizeof (attrs));
7136 decl = get_AT_ref (die, DW_AT_specification);
7137 if (decl != NULL)
7138 collect_checksum_attributes (&attrs, decl);
7139 collect_checksum_attributes (&attrs, die);
7141 CHECKSUM_ATTR (attrs.at_name);
7142 CHECKSUM_ATTR (attrs.at_accessibility);
7143 CHECKSUM_ATTR (attrs.at_address_class);
7144 CHECKSUM_ATTR (attrs.at_allocated);
7145 CHECKSUM_ATTR (attrs.at_artificial);
7146 CHECKSUM_ATTR (attrs.at_associated);
7147 CHECKSUM_ATTR (attrs.at_binary_scale);
7148 CHECKSUM_ATTR (attrs.at_bit_offset);
7149 CHECKSUM_ATTR (attrs.at_bit_size);
7150 CHECKSUM_ATTR (attrs.at_bit_stride);
7151 CHECKSUM_ATTR (attrs.at_byte_size);
7152 CHECKSUM_ATTR (attrs.at_byte_stride);
7153 CHECKSUM_ATTR (attrs.at_const_value);
7154 CHECKSUM_ATTR (attrs.at_containing_type);
7155 CHECKSUM_ATTR (attrs.at_count);
7156 CHECKSUM_ATTR (attrs.at_data_location);
7157 CHECKSUM_ATTR (attrs.at_data_member_location);
7158 CHECKSUM_ATTR (attrs.at_decimal_scale);
7159 CHECKSUM_ATTR (attrs.at_decimal_sign);
7160 CHECKSUM_ATTR (attrs.at_default_value);
7161 CHECKSUM_ATTR (attrs.at_digit_count);
7162 CHECKSUM_ATTR (attrs.at_discr);
7163 CHECKSUM_ATTR (attrs.at_discr_list);
7164 CHECKSUM_ATTR (attrs.at_discr_value);
7165 CHECKSUM_ATTR (attrs.at_encoding);
7166 CHECKSUM_ATTR (attrs.at_endianity);
7167 CHECKSUM_ATTR (attrs.at_explicit);
7168 CHECKSUM_ATTR (attrs.at_is_optional);
7169 CHECKSUM_ATTR (attrs.at_location);
7170 CHECKSUM_ATTR (attrs.at_lower_bound);
7171 CHECKSUM_ATTR (attrs.at_mutable);
7172 CHECKSUM_ATTR (attrs.at_ordering);
7173 CHECKSUM_ATTR (attrs.at_picture_string);
7174 CHECKSUM_ATTR (attrs.at_prototyped);
7175 CHECKSUM_ATTR (attrs.at_small);
7176 CHECKSUM_ATTR (attrs.at_segment);
7177 CHECKSUM_ATTR (attrs.at_string_length);
7178 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7179 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7180 CHECKSUM_ATTR (attrs.at_threads_scaled);
7181 CHECKSUM_ATTR (attrs.at_upper_bound);
7182 CHECKSUM_ATTR (attrs.at_use_location);
7183 CHECKSUM_ATTR (attrs.at_use_UTF8);
7184 CHECKSUM_ATTR (attrs.at_variable_parameter);
7185 CHECKSUM_ATTR (attrs.at_virtuality);
7186 CHECKSUM_ATTR (attrs.at_visibility);
7187 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7188 CHECKSUM_ATTR (attrs.at_type);
7189 CHECKSUM_ATTR (attrs.at_friend);
7190 CHECKSUM_ATTR (attrs.at_alignment);
7192 /* Checksum the child DIEs. */
7193 c = die->die_child;
7194 if (c) do {
7195 dw_attr_node *name_attr;
7197 c = c->die_sib;
7198 name_attr = get_AT (c, DW_AT_name);
7199 if (is_template_instantiation (c))
7201 /* Ignore instantiations of member type and function templates. */
7203 else if (name_attr != NULL
7204 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7206 /* Use a shallow checksum for named nested types and member
7207 functions. */
7208 CHECKSUM_ULEB128 ('S');
7209 CHECKSUM_ULEB128 (c->die_tag);
7210 CHECKSUM_STRING (AT_string (name_attr));
7212 else
7214 /* Use a deep checksum for other children. */
7215 /* Mark this DIE so it gets processed when unmarking. */
7216 if (c->die_mark == 0)
7217 c->die_mark = -1;
7218 die_checksum_ordered (c, ctx, mark);
7220 } while (c != die->die_child);
7222 CHECKSUM_ULEB128 (0);
7225 /* Add a type name and tag to a hash. */
7226 static void
7227 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7229 CHECKSUM_ULEB128 (tag);
7230 CHECKSUM_STRING (name);
7233 #undef CHECKSUM
7234 #undef CHECKSUM_STRING
7235 #undef CHECKSUM_ATTR
7236 #undef CHECKSUM_LEB128
7237 #undef CHECKSUM_ULEB128
7239 /* Generate the type signature for DIE. This is computed by generating an
7240 MD5 checksum over the DIE's tag, its relevant attributes, and its
7241 children. Attributes that are references to other DIEs are processed
7242 by recursion, using the MARK field to prevent infinite recursion.
7243 If the DIE is nested inside a namespace or another type, we also
7244 need to include that context in the signature. The lower 64 bits
7245 of the resulting MD5 checksum comprise the signature. */
7247 static void
7248 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7250 int mark;
7251 const char *name;
7252 unsigned char checksum[16];
7253 struct md5_ctx ctx;
7254 dw_die_ref decl;
7255 dw_die_ref parent;
7257 name = get_AT_string (die, DW_AT_name);
7258 decl = get_AT_ref (die, DW_AT_specification);
7259 parent = get_die_parent (die);
7261 /* First, compute a signature for just the type name (and its surrounding
7262 context, if any. This is stored in the type unit DIE for link-time
7263 ODR (one-definition rule) checking. */
7265 if (is_cxx () && name != NULL)
7267 md5_init_ctx (&ctx);
7269 /* Checksum the names of surrounding namespaces and structures. */
7270 if (parent != NULL)
7271 checksum_die_context (parent, &ctx);
7273 /* Checksum the current DIE. */
7274 die_odr_checksum (die->die_tag, name, &ctx);
7275 md5_finish_ctx (&ctx, checksum);
7277 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7280 /* Next, compute the complete type signature. */
7282 md5_init_ctx (&ctx);
7283 mark = 1;
7284 die->die_mark = mark;
7286 /* Checksum the names of surrounding namespaces and structures. */
7287 if (parent != NULL)
7288 checksum_die_context (parent, &ctx);
7290 /* Checksum the DIE and its children. */
7291 die_checksum_ordered (die, &ctx, &mark);
7292 unmark_all_dies (die);
7293 md5_finish_ctx (&ctx, checksum);
7295 /* Store the signature in the type node and link the type DIE and the
7296 type node together. */
7297 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7298 DWARF_TYPE_SIGNATURE_SIZE);
7299 die->comdat_type_p = true;
7300 die->die_id.die_type_node = type_node;
7301 type_node->type_die = die;
7303 /* If the DIE is a specification, link its declaration to the type node
7304 as well. */
7305 if (decl != NULL)
7307 decl->comdat_type_p = true;
7308 decl->die_id.die_type_node = type_node;
7312 /* Do the location expressions look same? */
7313 static inline int
7314 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7316 return loc1->dw_loc_opc == loc2->dw_loc_opc
7317 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7318 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7321 /* Do the values look the same? */
7322 static int
7323 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7325 dw_loc_descr_ref loc1, loc2;
7326 rtx r1, r2;
7328 if (v1->val_class != v2->val_class)
7329 return 0;
7331 switch (v1->val_class)
7333 case dw_val_class_const:
7334 case dw_val_class_const_implicit:
7335 return v1->v.val_int == v2->v.val_int;
7336 case dw_val_class_unsigned_const:
7337 case dw_val_class_unsigned_const_implicit:
7338 return v1->v.val_unsigned == v2->v.val_unsigned;
7339 case dw_val_class_const_double:
7340 return v1->v.val_double.high == v2->v.val_double.high
7341 && v1->v.val_double.low == v2->v.val_double.low;
7342 case dw_val_class_wide_int:
7343 return *v1->v.val_wide == *v2->v.val_wide;
7344 case dw_val_class_vec:
7345 if (v1->v.val_vec.length != v2->v.val_vec.length
7346 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7347 return 0;
7348 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7349 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7350 return 0;
7351 return 1;
7352 case dw_val_class_flag:
7353 return v1->v.val_flag == v2->v.val_flag;
7354 case dw_val_class_str:
7355 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7357 case dw_val_class_addr:
7358 r1 = v1->v.val_addr;
7359 r2 = v2->v.val_addr;
7360 if (GET_CODE (r1) != GET_CODE (r2))
7361 return 0;
7362 return !rtx_equal_p (r1, r2);
7364 case dw_val_class_offset:
7365 return v1->v.val_offset == v2->v.val_offset;
7367 case dw_val_class_loc:
7368 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7369 loc1 && loc2;
7370 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7371 if (!same_loc_p (loc1, loc2, mark))
7372 return 0;
7373 return !loc1 && !loc2;
7375 case dw_val_class_die_ref:
7376 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7378 case dw_val_class_fde_ref:
7379 case dw_val_class_vms_delta:
7380 case dw_val_class_lbl_id:
7381 case dw_val_class_lineptr:
7382 case dw_val_class_macptr:
7383 case dw_val_class_loclistsptr:
7384 case dw_val_class_high_pc:
7385 return 1;
7387 case dw_val_class_file:
7388 case dw_val_class_file_implicit:
7389 return v1->v.val_file == v2->v.val_file;
7391 case dw_val_class_data8:
7392 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7394 default:
7395 return 1;
7399 /* Do the attributes look the same? */
7401 static int
7402 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7404 if (at1->dw_attr != at2->dw_attr)
7405 return 0;
7407 /* We don't care that this was compiled with a different compiler
7408 snapshot; if the output is the same, that's what matters. */
7409 if (at1->dw_attr == DW_AT_producer)
7410 return 1;
7412 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7415 /* Do the dies look the same? */
7417 static int
7418 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7420 dw_die_ref c1, c2;
7421 dw_attr_node *a1;
7422 unsigned ix;
7424 /* To avoid infinite recursion. */
7425 if (die1->die_mark)
7426 return die1->die_mark == die2->die_mark;
7427 die1->die_mark = die2->die_mark = ++(*mark);
7429 if (die1->die_tag != die2->die_tag)
7430 return 0;
7432 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7433 return 0;
7435 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7436 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7437 return 0;
7439 c1 = die1->die_child;
7440 c2 = die2->die_child;
7441 if (! c1)
7443 if (c2)
7444 return 0;
7446 else
7447 for (;;)
7449 if (!same_die_p (c1, c2, mark))
7450 return 0;
7451 c1 = c1->die_sib;
7452 c2 = c2->die_sib;
7453 if (c1 == die1->die_child)
7455 if (c2 == die2->die_child)
7456 break;
7457 else
7458 return 0;
7462 return 1;
7465 /* Do the dies look the same? Wrapper around same_die_p. */
7467 static int
7468 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7470 int mark = 0;
7471 int ret = same_die_p (die1, die2, &mark);
7473 unmark_all_dies (die1);
7474 unmark_all_dies (die2);
7476 return ret;
7479 /* The prefix to attach to symbols on DIEs in the current comdat debug
7480 info section. */
7481 static const char *comdat_symbol_id;
7483 /* The index of the current symbol within the current comdat CU. */
7484 static unsigned int comdat_symbol_number;
7486 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7487 children, and set die_symbol. */
7489 static void
7490 compute_comp_unit_symbol (dw_die_ref unit_die)
7492 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7493 const char *base = die_name ? lbasename (die_name) : "anonymous";
7494 char *name = XALLOCAVEC (char, strlen (base) + 64);
7495 char *p;
7496 int i, mark;
7497 unsigned char checksum[16];
7498 struct md5_ctx ctx;
7500 /* Compute the checksum of the DIE, then append part of it as hex digits to
7501 the name filename of the unit. */
7503 md5_init_ctx (&ctx);
7504 mark = 0;
7505 die_checksum (unit_die, &ctx, &mark);
7506 unmark_all_dies (unit_die);
7507 md5_finish_ctx (&ctx, checksum);
7509 /* When we this for comp_unit_die () we have a DW_AT_name that might
7510 not start with a letter but with anything valid for filenames and
7511 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7512 character is not a letter. */
7513 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7514 clean_symbol_name (name);
7516 p = name + strlen (name);
7517 for (i = 0; i < 4; i++)
7519 sprintf (p, "%.2x", checksum[i]);
7520 p += 2;
7523 unit_die->die_id.die_symbol = xstrdup (name);
7526 static void
7527 compute_section_prefix (dw_die_ref unit_die)
7529 compute_comp_unit_symbol (unit_die);
7530 unit_die->comdat_type_p = true;
7531 comdat_symbol_id = unit_die->die_id.die_symbol;
7532 comdat_symbol_number = 0;
7535 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7537 static int
7538 is_type_die (dw_die_ref die)
7540 switch (die->die_tag)
7542 case DW_TAG_array_type:
7543 case DW_TAG_class_type:
7544 case DW_TAG_interface_type:
7545 case DW_TAG_enumeration_type:
7546 case DW_TAG_pointer_type:
7547 case DW_TAG_reference_type:
7548 case DW_TAG_rvalue_reference_type:
7549 case DW_TAG_string_type:
7550 case DW_TAG_structure_type:
7551 case DW_TAG_subroutine_type:
7552 case DW_TAG_union_type:
7553 case DW_TAG_ptr_to_member_type:
7554 case DW_TAG_set_type:
7555 case DW_TAG_subrange_type:
7556 case DW_TAG_base_type:
7557 case DW_TAG_const_type:
7558 case DW_TAG_file_type:
7559 case DW_TAG_packed_type:
7560 case DW_TAG_volatile_type:
7561 case DW_TAG_typedef:
7562 return 1;
7563 default:
7564 return 0;
7568 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7569 Basically, we want to choose the bits that are likely to be shared between
7570 compilations (types) and leave out the bits that are specific to individual
7571 compilations (functions). */
7573 static int
7574 is_comdat_die (dw_die_ref c)
7576 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7577 we do for stabs. The advantage is a greater likelihood of sharing between
7578 objects that don't include headers in the same order (and therefore would
7579 put the base types in a different comdat). jason 8/28/00 */
7581 if (c->die_tag == DW_TAG_base_type)
7582 return 0;
7584 if (c->die_tag == DW_TAG_pointer_type
7585 || c->die_tag == DW_TAG_reference_type
7586 || c->die_tag == DW_TAG_rvalue_reference_type
7587 || c->die_tag == DW_TAG_const_type
7588 || c->die_tag == DW_TAG_volatile_type)
7590 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7592 return t ? is_comdat_die (t) : 0;
7595 return is_type_die (c);
7598 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7599 compilation unit. */
7601 static int
7602 is_symbol_die (dw_die_ref c)
7604 return (is_type_die (c)
7605 || is_declaration_die (c)
7606 || c->die_tag == DW_TAG_namespace
7607 || c->die_tag == DW_TAG_module);
7610 /* Returns true iff C is a compile-unit DIE. */
7612 static inline bool
7613 is_cu_die (dw_die_ref c)
7615 return c && (c->die_tag == DW_TAG_compile_unit
7616 || c->die_tag == DW_TAG_skeleton_unit);
7619 /* Returns true iff C is a unit DIE of some sort. */
7621 static inline bool
7622 is_unit_die (dw_die_ref c)
7624 return c && (c->die_tag == DW_TAG_compile_unit
7625 || c->die_tag == DW_TAG_partial_unit
7626 || c->die_tag == DW_TAG_type_unit
7627 || c->die_tag == DW_TAG_skeleton_unit);
7630 /* Returns true iff C is a namespace DIE. */
7632 static inline bool
7633 is_namespace_die (dw_die_ref c)
7635 return c && c->die_tag == DW_TAG_namespace;
7638 /* Returns true iff C is a class or structure DIE. */
7640 static inline bool
7641 is_class_die (dw_die_ref c)
7643 return c && (c->die_tag == DW_TAG_class_type
7644 || c->die_tag == DW_TAG_structure_type);
7647 /* Return non-zero if this DIE is a template parameter. */
7649 static inline bool
7650 is_template_parameter (dw_die_ref die)
7652 switch (die->die_tag)
7654 case DW_TAG_template_type_param:
7655 case DW_TAG_template_value_param:
7656 case DW_TAG_GNU_template_template_param:
7657 case DW_TAG_GNU_template_parameter_pack:
7658 return true;
7659 default:
7660 return false;
7664 /* Return non-zero if this DIE represents a template instantiation. */
7666 static inline bool
7667 is_template_instantiation (dw_die_ref die)
7669 dw_die_ref c;
7671 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7672 return false;
7673 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7674 return false;
7677 static char *
7678 gen_internal_sym (const char *prefix)
7680 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7682 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7683 return xstrdup (buf);
7686 /* Assign symbols to all worthy DIEs under DIE. */
7688 static void
7689 assign_symbol_names (dw_die_ref die)
7691 dw_die_ref c;
7693 if (is_symbol_die (die) && !die->comdat_type_p)
7695 if (comdat_symbol_id)
7697 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7699 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7700 comdat_symbol_id, comdat_symbol_number++);
7701 die->die_id.die_symbol = xstrdup (p);
7703 else
7704 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7707 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7710 struct cu_hash_table_entry
7712 dw_die_ref cu;
7713 unsigned min_comdat_num, max_comdat_num;
7714 struct cu_hash_table_entry *next;
7717 /* Helpers to manipulate hash table of CUs. */
7719 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7721 typedef die_struct *compare_type;
7722 static inline hashval_t hash (const cu_hash_table_entry *);
7723 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7724 static inline void remove (cu_hash_table_entry *);
7727 inline hashval_t
7728 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7730 return htab_hash_string (entry->cu->die_id.die_symbol);
7733 inline bool
7734 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7735 const die_struct *entry2)
7737 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7740 inline void
7741 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7743 struct cu_hash_table_entry *next;
7745 while (entry)
7747 next = entry->next;
7748 free (entry);
7749 entry = next;
7753 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7755 /* Check whether we have already seen this CU and set up SYM_NUM
7756 accordingly. */
7757 static int
7758 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7760 struct cu_hash_table_entry dummy;
7761 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7763 dummy.max_comdat_num = 0;
7765 slot = htable->find_slot_with_hash (cu,
7766 htab_hash_string (cu->die_id.die_symbol),
7767 INSERT);
7768 entry = *slot;
7770 for (; entry; last = entry, entry = entry->next)
7772 if (same_die_p_wrap (cu, entry->cu))
7773 break;
7776 if (entry)
7778 *sym_num = entry->min_comdat_num;
7779 return 1;
7782 entry = XCNEW (struct cu_hash_table_entry);
7783 entry->cu = cu;
7784 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7785 entry->next = *slot;
7786 *slot = entry;
7788 return 0;
7791 /* Record SYM_NUM to record of CU in HTABLE. */
7792 static void
7793 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7794 unsigned int sym_num)
7796 struct cu_hash_table_entry **slot, *entry;
7798 slot = htable->find_slot_with_hash (cu,
7799 htab_hash_string (cu->die_id.die_symbol),
7800 NO_INSERT);
7801 entry = *slot;
7803 entry->max_comdat_num = sym_num;
7806 /* Traverse the DIE (which is always comp_unit_die), and set up
7807 additional compilation units for each of the include files we see
7808 bracketed by BINCL/EINCL. */
7810 static void
7811 break_out_includes (dw_die_ref die)
7813 dw_die_ref c;
7814 dw_die_ref unit = NULL;
7815 limbo_die_node *node, **pnode;
7817 c = die->die_child;
7818 if (c) do {
7819 dw_die_ref prev = c;
7820 c = c->die_sib;
7821 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7822 || (unit && is_comdat_die (c)))
7824 dw_die_ref next = c->die_sib;
7826 /* This DIE is for a secondary CU; remove it from the main one. */
7827 remove_child_with_prev (c, prev);
7829 if (c->die_tag == DW_TAG_GNU_BINCL)
7830 unit = push_new_compile_unit (unit, c);
7831 else if (c->die_tag == DW_TAG_GNU_EINCL)
7832 unit = pop_compile_unit (unit);
7833 else
7834 add_child_die (unit, c);
7835 c = next;
7836 if (c == die->die_child)
7837 break;
7839 } while (c != die->die_child);
7841 #if 0
7842 /* We can only use this in debugging, since the frontend doesn't check
7843 to make sure that we leave every include file we enter. */
7844 gcc_assert (!unit);
7845 #endif
7847 assign_symbol_names (die);
7848 cu_hash_type cu_hash_table (10);
7849 for (node = limbo_die_list, pnode = &limbo_die_list;
7850 node;
7851 node = node->next)
7853 int is_dupl;
7855 compute_section_prefix (node->die);
7856 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7857 &comdat_symbol_number);
7858 assign_symbol_names (node->die);
7859 if (is_dupl)
7860 *pnode = node->next;
7861 else
7863 pnode = &node->next;
7864 record_comdat_symbol_number (node->die, &cu_hash_table,
7865 comdat_symbol_number);
7870 /* Return non-zero if this DIE is a declaration. */
7872 static int
7873 is_declaration_die (dw_die_ref die)
7875 dw_attr_node *a;
7876 unsigned ix;
7878 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7879 if (a->dw_attr == DW_AT_declaration)
7880 return 1;
7882 return 0;
7885 /* Return non-zero if this DIE is nested inside a subprogram. */
7887 static int
7888 is_nested_in_subprogram (dw_die_ref die)
7890 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7892 if (decl == NULL)
7893 decl = die;
7894 return local_scope_p (decl);
7897 /* Return non-zero if this DIE contains a defining declaration of a
7898 subprogram. */
7900 static int
7901 contains_subprogram_definition (dw_die_ref die)
7903 dw_die_ref c;
7905 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7906 return 1;
7907 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7908 return 0;
7911 /* Return non-zero if this is a type DIE that should be moved to a
7912 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7913 unit type. */
7915 static int
7916 should_move_die_to_comdat (dw_die_ref die)
7918 switch (die->die_tag)
7920 case DW_TAG_class_type:
7921 case DW_TAG_structure_type:
7922 case DW_TAG_enumeration_type:
7923 case DW_TAG_union_type:
7924 /* Don't move declarations, inlined instances, types nested in a
7925 subprogram, or types that contain subprogram definitions. */
7926 if (is_declaration_die (die)
7927 || get_AT (die, DW_AT_abstract_origin)
7928 || is_nested_in_subprogram (die)
7929 || contains_subprogram_definition (die))
7930 return 0;
7931 return 1;
7932 case DW_TAG_array_type:
7933 case DW_TAG_interface_type:
7934 case DW_TAG_pointer_type:
7935 case DW_TAG_reference_type:
7936 case DW_TAG_rvalue_reference_type:
7937 case DW_TAG_string_type:
7938 case DW_TAG_subroutine_type:
7939 case DW_TAG_ptr_to_member_type:
7940 case DW_TAG_set_type:
7941 case DW_TAG_subrange_type:
7942 case DW_TAG_base_type:
7943 case DW_TAG_const_type:
7944 case DW_TAG_file_type:
7945 case DW_TAG_packed_type:
7946 case DW_TAG_volatile_type:
7947 case DW_TAG_typedef:
7948 default:
7949 return 0;
7953 /* Make a clone of DIE. */
7955 static dw_die_ref
7956 clone_die (dw_die_ref die)
7958 dw_die_ref clone;
7959 dw_attr_node *a;
7960 unsigned ix;
7962 clone = ggc_cleared_alloc<die_node> ();
7963 clone->die_tag = die->die_tag;
7965 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7966 add_dwarf_attr (clone, a);
7968 return clone;
7971 /* Make a clone of the tree rooted at DIE. */
7973 static dw_die_ref
7974 clone_tree (dw_die_ref die)
7976 dw_die_ref c;
7977 dw_die_ref clone = clone_die (die);
7979 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7981 return clone;
7984 /* Make a clone of DIE as a declaration. */
7986 static dw_die_ref
7987 clone_as_declaration (dw_die_ref die)
7989 dw_die_ref clone;
7990 dw_die_ref decl;
7991 dw_attr_node *a;
7992 unsigned ix;
7994 /* If the DIE is already a declaration, just clone it. */
7995 if (is_declaration_die (die))
7996 return clone_die (die);
7998 /* If the DIE is a specification, just clone its declaration DIE. */
7999 decl = get_AT_ref (die, DW_AT_specification);
8000 if (decl != NULL)
8002 clone = clone_die (decl);
8003 if (die->comdat_type_p)
8004 add_AT_die_ref (clone, DW_AT_signature, die);
8005 return clone;
8008 clone = ggc_cleared_alloc<die_node> ();
8009 clone->die_tag = die->die_tag;
8011 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8013 /* We don't want to copy over all attributes.
8014 For example we don't want DW_AT_byte_size because otherwise we will no
8015 longer have a declaration and GDB will treat it as a definition. */
8017 switch (a->dw_attr)
8019 case DW_AT_abstract_origin:
8020 case DW_AT_artificial:
8021 case DW_AT_containing_type:
8022 case DW_AT_external:
8023 case DW_AT_name:
8024 case DW_AT_type:
8025 case DW_AT_virtuality:
8026 case DW_AT_linkage_name:
8027 case DW_AT_MIPS_linkage_name:
8028 add_dwarf_attr (clone, a);
8029 break;
8030 case DW_AT_byte_size:
8031 case DW_AT_alignment:
8032 default:
8033 break;
8037 if (die->comdat_type_p)
8038 add_AT_die_ref (clone, DW_AT_signature, die);
8040 add_AT_flag (clone, DW_AT_declaration, 1);
8041 return clone;
8045 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8047 struct decl_table_entry
8049 dw_die_ref orig;
8050 dw_die_ref copy;
8053 /* Helpers to manipulate hash table of copied declarations. */
8055 /* Hashtable helpers. */
8057 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8059 typedef die_struct *compare_type;
8060 static inline hashval_t hash (const decl_table_entry *);
8061 static inline bool equal (const decl_table_entry *, const die_struct *);
8064 inline hashval_t
8065 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8067 return htab_hash_pointer (entry->orig);
8070 inline bool
8071 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8072 const die_struct *entry2)
8074 return entry1->orig == entry2;
8077 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8079 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8080 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8081 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8082 to check if the ancestor has already been copied into UNIT. */
8084 static dw_die_ref
8085 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8086 decl_hash_type *decl_table)
8088 dw_die_ref parent = die->die_parent;
8089 dw_die_ref new_parent = unit;
8090 dw_die_ref copy;
8091 decl_table_entry **slot = NULL;
8092 struct decl_table_entry *entry = NULL;
8094 if (decl_table)
8096 /* Check if the entry has already been copied to UNIT. */
8097 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8098 INSERT);
8099 if (*slot != HTAB_EMPTY_ENTRY)
8101 entry = *slot;
8102 return entry->copy;
8105 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8106 entry = XCNEW (struct decl_table_entry);
8107 entry->orig = die;
8108 entry->copy = NULL;
8109 *slot = entry;
8112 if (parent != NULL)
8114 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8115 if (spec != NULL)
8116 parent = spec;
8117 if (!is_unit_die (parent))
8118 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8121 copy = clone_as_declaration (die);
8122 add_child_die (new_parent, copy);
8124 if (decl_table)
8126 /* Record the pointer to the copy. */
8127 entry->copy = copy;
8130 return copy;
8132 /* Copy the declaration context to the new type unit DIE. This includes
8133 any surrounding namespace or type declarations. If the DIE has an
8134 AT_specification attribute, it also includes attributes and children
8135 attached to the specification, and returns a pointer to the original
8136 parent of the declaration DIE. Returns NULL otherwise. */
8138 static dw_die_ref
8139 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8141 dw_die_ref decl;
8142 dw_die_ref new_decl;
8143 dw_die_ref orig_parent = NULL;
8145 decl = get_AT_ref (die, DW_AT_specification);
8146 if (decl == NULL)
8147 decl = die;
8148 else
8150 unsigned ix;
8151 dw_die_ref c;
8152 dw_attr_node *a;
8154 /* The original DIE will be changed to a declaration, and must
8155 be moved to be a child of the original declaration DIE. */
8156 orig_parent = decl->die_parent;
8158 /* Copy the type node pointer from the new DIE to the original
8159 declaration DIE so we can forward references later. */
8160 decl->comdat_type_p = true;
8161 decl->die_id.die_type_node = die->die_id.die_type_node;
8163 remove_AT (die, DW_AT_specification);
8165 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8167 if (a->dw_attr != DW_AT_name
8168 && a->dw_attr != DW_AT_declaration
8169 && a->dw_attr != DW_AT_external)
8170 add_dwarf_attr (die, a);
8173 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8176 if (decl->die_parent != NULL
8177 && !is_unit_die (decl->die_parent))
8179 new_decl = copy_ancestor_tree (unit, decl, NULL);
8180 if (new_decl != NULL)
8182 remove_AT (new_decl, DW_AT_signature);
8183 add_AT_specification (die, new_decl);
8187 return orig_parent;
8190 /* Generate the skeleton ancestor tree for the given NODE, then clone
8191 the DIE and add the clone into the tree. */
8193 static void
8194 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8196 if (node->new_die != NULL)
8197 return;
8199 node->new_die = clone_as_declaration (node->old_die);
8201 if (node->parent != NULL)
8203 generate_skeleton_ancestor_tree (node->parent);
8204 add_child_die (node->parent->new_die, node->new_die);
8208 /* Generate a skeleton tree of DIEs containing any declarations that are
8209 found in the original tree. We traverse the tree looking for declaration
8210 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8212 static void
8213 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8215 skeleton_chain_node node;
8216 dw_die_ref c;
8217 dw_die_ref first;
8218 dw_die_ref prev = NULL;
8219 dw_die_ref next = NULL;
8221 node.parent = parent;
8223 first = c = parent->old_die->die_child;
8224 if (c)
8225 next = c->die_sib;
8226 if (c) do {
8227 if (prev == NULL || prev->die_sib == c)
8228 prev = c;
8229 c = next;
8230 next = (c == first ? NULL : c->die_sib);
8231 node.old_die = c;
8232 node.new_die = NULL;
8233 if (is_declaration_die (c))
8235 if (is_template_instantiation (c))
8237 /* Instantiated templates do not need to be cloned into the
8238 type unit. Just move the DIE and its children back to
8239 the skeleton tree (in the main CU). */
8240 remove_child_with_prev (c, prev);
8241 add_child_die (parent->new_die, c);
8242 c = prev;
8244 else if (c->comdat_type_p)
8246 /* This is the skeleton of earlier break_out_comdat_types
8247 type. Clone the existing DIE, but keep the children
8248 under the original (which is in the main CU). */
8249 dw_die_ref clone = clone_die (c);
8251 replace_child (c, clone, prev);
8252 generate_skeleton_ancestor_tree (parent);
8253 add_child_die (parent->new_die, c);
8254 c = clone;
8255 continue;
8257 else
8259 /* Clone the existing DIE, move the original to the skeleton
8260 tree (which is in the main CU), and put the clone, with
8261 all the original's children, where the original came from
8262 (which is about to be moved to the type unit). */
8263 dw_die_ref clone = clone_die (c);
8264 move_all_children (c, clone);
8266 /* If the original has a DW_AT_object_pointer attribute,
8267 it would now point to a child DIE just moved to the
8268 cloned tree, so we need to remove that attribute from
8269 the original. */
8270 remove_AT (c, DW_AT_object_pointer);
8272 replace_child (c, clone, prev);
8273 generate_skeleton_ancestor_tree (parent);
8274 add_child_die (parent->new_die, c);
8275 node.old_die = clone;
8276 node.new_die = c;
8277 c = clone;
8280 generate_skeleton_bottom_up (&node);
8281 } while (next != NULL);
8284 /* Wrapper function for generate_skeleton_bottom_up. */
8286 static dw_die_ref
8287 generate_skeleton (dw_die_ref die)
8289 skeleton_chain_node node;
8291 node.old_die = die;
8292 node.new_die = NULL;
8293 node.parent = NULL;
8295 /* If this type definition is nested inside another type,
8296 and is not an instantiation of a template, always leave
8297 at least a declaration in its place. */
8298 if (die->die_parent != NULL
8299 && is_type_die (die->die_parent)
8300 && !is_template_instantiation (die))
8301 node.new_die = clone_as_declaration (die);
8303 generate_skeleton_bottom_up (&node);
8304 return node.new_die;
8307 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8308 declaration. The original DIE is moved to a new compile unit so that
8309 existing references to it follow it to the new location. If any of the
8310 original DIE's descendants is a declaration, we need to replace the
8311 original DIE with a skeleton tree and move the declarations back into the
8312 skeleton tree. */
8314 static dw_die_ref
8315 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8316 dw_die_ref prev)
8318 dw_die_ref skeleton, orig_parent;
8320 /* Copy the declaration context to the type unit DIE. If the returned
8321 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8322 that DIE. */
8323 orig_parent = copy_declaration_context (unit, child);
8325 skeleton = generate_skeleton (child);
8326 if (skeleton == NULL)
8327 remove_child_with_prev (child, prev);
8328 else
8330 skeleton->comdat_type_p = true;
8331 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8333 /* If the original DIE was a specification, we need to put
8334 the skeleton under the parent DIE of the declaration.
8335 This leaves the original declaration in the tree, but
8336 it will be pruned later since there are no longer any
8337 references to it. */
8338 if (orig_parent != NULL)
8340 remove_child_with_prev (child, prev);
8341 add_child_die (orig_parent, skeleton);
8343 else
8344 replace_child (child, skeleton, prev);
8347 return skeleton;
8350 static void
8351 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8352 comdat_type_node *type_node,
8353 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8355 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8356 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8357 DWARF procedure references in the DW_AT_location attribute. */
8359 static dw_die_ref
8360 copy_dwarf_procedure (dw_die_ref die,
8361 comdat_type_node *type_node,
8362 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8364 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8366 /* DWARF procedures are not supposed to have children... */
8367 gcc_assert (die->die_child == NULL);
8369 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8370 gcc_assert (vec_safe_length (die->die_attr) == 1
8371 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8373 /* Do not copy more than once DWARF procedures. */
8374 bool existed;
8375 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8376 if (existed)
8377 return die_copy;
8379 die_copy = clone_die (die);
8380 add_child_die (type_node->root_die, die_copy);
8381 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8382 return die_copy;
8385 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8386 procedures in DIE's attributes. */
8388 static void
8389 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8390 comdat_type_node *type_node,
8391 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8393 dw_attr_node *a;
8394 unsigned i;
8396 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8398 dw_loc_descr_ref loc;
8400 if (a->dw_attr_val.val_class != dw_val_class_loc)
8401 continue;
8403 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8405 switch (loc->dw_loc_opc)
8407 case DW_OP_call2:
8408 case DW_OP_call4:
8409 case DW_OP_call_ref:
8410 gcc_assert (loc->dw_loc_oprnd1.val_class
8411 == dw_val_class_die_ref);
8412 loc->dw_loc_oprnd1.v.val_die_ref.die
8413 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8414 type_node,
8415 copied_dwarf_procs);
8417 default:
8418 break;
8424 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8425 rewrite references to point to the copies.
8427 References are looked for in DIE's attributes and recursively in all its
8428 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8429 mapping from old DWARF procedures to their copy. It is used not to copy
8430 twice the same DWARF procedure under TYPE_NODE. */
8432 static void
8433 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8434 comdat_type_node *type_node,
8435 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8437 dw_die_ref c;
8439 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8440 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8441 type_node,
8442 copied_dwarf_procs));
8445 /* Traverse the DIE and set up additional .debug_types or .debug_info
8446 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8447 section. */
8449 static void
8450 break_out_comdat_types (dw_die_ref die)
8452 dw_die_ref c;
8453 dw_die_ref first;
8454 dw_die_ref prev = NULL;
8455 dw_die_ref next = NULL;
8456 dw_die_ref unit = NULL;
8458 first = c = die->die_child;
8459 if (c)
8460 next = c->die_sib;
8461 if (c) do {
8462 if (prev == NULL || prev->die_sib == c)
8463 prev = c;
8464 c = next;
8465 next = (c == first ? NULL : c->die_sib);
8466 if (should_move_die_to_comdat (c))
8468 dw_die_ref replacement;
8469 comdat_type_node *type_node;
8471 /* Break out nested types into their own type units. */
8472 break_out_comdat_types (c);
8474 /* Create a new type unit DIE as the root for the new tree, and
8475 add it to the list of comdat types. */
8476 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8477 add_AT_unsigned (unit, DW_AT_language,
8478 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8479 type_node = ggc_cleared_alloc<comdat_type_node> ();
8480 type_node->root_die = unit;
8481 type_node->next = comdat_type_list;
8482 comdat_type_list = type_node;
8484 /* Generate the type signature. */
8485 generate_type_signature (c, type_node);
8487 /* Copy the declaration context, attributes, and children of the
8488 declaration into the new type unit DIE, then remove this DIE
8489 from the main CU (or replace it with a skeleton if necessary). */
8490 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8491 type_node->skeleton_die = replacement;
8493 /* Add the DIE to the new compunit. */
8494 add_child_die (unit, c);
8496 /* Types can reference DWARF procedures for type size or data location
8497 expressions. Calls in DWARF expressions cannot target procedures
8498 that are not in the same section. So we must copy DWARF procedures
8499 along with this type and then rewrite references to them. */
8500 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8501 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8503 if (replacement != NULL)
8504 c = replacement;
8506 else if (c->die_tag == DW_TAG_namespace
8507 || c->die_tag == DW_TAG_class_type
8508 || c->die_tag == DW_TAG_structure_type
8509 || c->die_tag == DW_TAG_union_type)
8511 /* Look for nested types that can be broken out. */
8512 break_out_comdat_types (c);
8514 } while (next != NULL);
8517 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8518 Enter all the cloned children into the hash table decl_table. */
8520 static dw_die_ref
8521 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8523 dw_die_ref c;
8524 dw_die_ref clone;
8525 struct decl_table_entry *entry;
8526 decl_table_entry **slot;
8528 if (die->die_tag == DW_TAG_subprogram)
8529 clone = clone_as_declaration (die);
8530 else
8531 clone = clone_die (die);
8533 slot = decl_table->find_slot_with_hash (die,
8534 htab_hash_pointer (die), INSERT);
8536 /* Assert that DIE isn't in the hash table yet. If it would be there
8537 before, the ancestors would be necessarily there as well, therefore
8538 clone_tree_partial wouldn't be called. */
8539 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8541 entry = XCNEW (struct decl_table_entry);
8542 entry->orig = die;
8543 entry->copy = clone;
8544 *slot = entry;
8546 if (die->die_tag != DW_TAG_subprogram)
8547 FOR_EACH_CHILD (die, c,
8548 add_child_die (clone, clone_tree_partial (c, decl_table)));
8550 return clone;
8553 /* Walk the DIE and its children, looking for references to incomplete
8554 or trivial types that are unmarked (i.e., that are not in the current
8555 type_unit). */
8557 static void
8558 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8560 dw_die_ref c;
8561 dw_attr_node *a;
8562 unsigned ix;
8564 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8566 if (AT_class (a) == dw_val_class_die_ref)
8568 dw_die_ref targ = AT_ref (a);
8569 decl_table_entry **slot;
8570 struct decl_table_entry *entry;
8572 if (targ->die_mark != 0 || targ->comdat_type_p)
8573 continue;
8575 slot = decl_table->find_slot_with_hash (targ,
8576 htab_hash_pointer (targ),
8577 INSERT);
8579 if (*slot != HTAB_EMPTY_ENTRY)
8581 /* TARG has already been copied, so we just need to
8582 modify the reference to point to the copy. */
8583 entry = *slot;
8584 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8586 else
8588 dw_die_ref parent = unit;
8589 dw_die_ref copy = clone_die (targ);
8591 /* Record in DECL_TABLE that TARG has been copied.
8592 Need to do this now, before the recursive call,
8593 because DECL_TABLE may be expanded and SLOT
8594 would no longer be a valid pointer. */
8595 entry = XCNEW (struct decl_table_entry);
8596 entry->orig = targ;
8597 entry->copy = copy;
8598 *slot = entry;
8600 /* If TARG is not a declaration DIE, we need to copy its
8601 children. */
8602 if (!is_declaration_die (targ))
8604 FOR_EACH_CHILD (
8605 targ, c,
8606 add_child_die (copy,
8607 clone_tree_partial (c, decl_table)));
8610 /* Make sure the cloned tree is marked as part of the
8611 type unit. */
8612 mark_dies (copy);
8614 /* If TARG has surrounding context, copy its ancestor tree
8615 into the new type unit. */
8616 if (targ->die_parent != NULL
8617 && !is_unit_die (targ->die_parent))
8618 parent = copy_ancestor_tree (unit, targ->die_parent,
8619 decl_table);
8621 add_child_die (parent, copy);
8622 a->dw_attr_val.v.val_die_ref.die = copy;
8624 /* Make sure the newly-copied DIE is walked. If it was
8625 installed in a previously-added context, it won't
8626 get visited otherwise. */
8627 if (parent != unit)
8629 /* Find the highest point of the newly-added tree,
8630 mark each node along the way, and walk from there. */
8631 parent->die_mark = 1;
8632 while (parent->die_parent
8633 && parent->die_parent->die_mark == 0)
8635 parent = parent->die_parent;
8636 parent->die_mark = 1;
8638 copy_decls_walk (unit, parent, decl_table);
8644 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8647 /* Copy declarations for "unworthy" types into the new comdat section.
8648 Incomplete types, modified types, and certain other types aren't broken
8649 out into comdat sections of their own, so they don't have a signature,
8650 and we need to copy the declaration into the same section so that we
8651 don't have an external reference. */
8653 static void
8654 copy_decls_for_unworthy_types (dw_die_ref unit)
8656 mark_dies (unit);
8657 decl_hash_type decl_table (10);
8658 copy_decls_walk (unit, unit, &decl_table);
8659 unmark_dies (unit);
8662 /* Traverse the DIE and add a sibling attribute if it may have the
8663 effect of speeding up access to siblings. To save some space,
8664 avoid generating sibling attributes for DIE's without children. */
8666 static void
8667 add_sibling_attributes (dw_die_ref die)
8669 dw_die_ref c;
8671 if (! die->die_child)
8672 return;
8674 if (die->die_parent && die != die->die_parent->die_child)
8675 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8677 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8680 /* Output all location lists for the DIE and its children. */
8682 static void
8683 output_location_lists (dw_die_ref die)
8685 dw_die_ref c;
8686 dw_attr_node *a;
8687 unsigned ix;
8689 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8690 if (AT_class (a) == dw_val_class_loc_list)
8691 output_loc_list (AT_loc_list (a));
8693 FOR_EACH_CHILD (die, c, output_location_lists (c));
8696 /* During assign_location_list_indexes and output_loclists_offset the
8697 current index, after it the number of assigned indexes (i.e. how
8698 large the .debug_loclists* offset table should be). */
8699 static unsigned int loc_list_idx;
8701 /* Output all location list offsets for the DIE and its children. */
8703 static void
8704 output_loclists_offsets (dw_die_ref die)
8706 dw_die_ref c;
8707 dw_attr_node *a;
8708 unsigned ix;
8710 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8711 if (AT_class (a) == dw_val_class_loc_list)
8713 dw_loc_list_ref l = AT_loc_list (a);
8714 if (l->offset_emitted)
8715 continue;
8716 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8717 loc_section_label, NULL);
8718 gcc_assert (l->hash == loc_list_idx);
8719 loc_list_idx++;
8720 l->offset_emitted = true;
8723 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8726 /* Recursively set indexes of location lists. */
8728 static void
8729 assign_location_list_indexes (dw_die_ref die)
8731 dw_die_ref c;
8732 dw_attr_node *a;
8733 unsigned ix;
8735 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8736 if (AT_class (a) == dw_val_class_loc_list)
8738 dw_loc_list_ref list = AT_loc_list (a);
8739 if (!list->num_assigned)
8741 list->num_assigned = true;
8742 list->hash = loc_list_idx++;
8746 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8749 /* We want to limit the number of external references, because they are
8750 larger than local references: a relocation takes multiple words, and
8751 even a sig8 reference is always eight bytes, whereas a local reference
8752 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8753 So if we encounter multiple external references to the same type DIE, we
8754 make a local typedef stub for it and redirect all references there.
8756 This is the element of the hash table for keeping track of these
8757 references. */
8759 struct external_ref
8761 dw_die_ref type;
8762 dw_die_ref stub;
8763 unsigned n_refs;
8766 /* Hashtable helpers. */
8768 struct external_ref_hasher : free_ptr_hash <external_ref>
8770 static inline hashval_t hash (const external_ref *);
8771 static inline bool equal (const external_ref *, const external_ref *);
8774 inline hashval_t
8775 external_ref_hasher::hash (const external_ref *r)
8777 dw_die_ref die = r->type;
8778 hashval_t h = 0;
8780 /* We can't use the address of the DIE for hashing, because
8781 that will make the order of the stub DIEs non-deterministic. */
8782 if (! die->comdat_type_p)
8783 /* We have a symbol; use it to compute a hash. */
8784 h = htab_hash_string (die->die_id.die_symbol);
8785 else
8787 /* We have a type signature; use a subset of the bits as the hash.
8788 The 8-byte signature is at least as large as hashval_t. */
8789 comdat_type_node *type_node = die->die_id.die_type_node;
8790 memcpy (&h, type_node->signature, sizeof (h));
8792 return h;
8795 inline bool
8796 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8798 return r1->type == r2->type;
8801 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8803 /* Return a pointer to the external_ref for references to DIE. */
8805 static struct external_ref *
8806 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8808 struct external_ref ref, *ref_p;
8809 external_ref **slot;
8811 ref.type = die;
8812 slot = map->find_slot (&ref, INSERT);
8813 if (*slot != HTAB_EMPTY_ENTRY)
8814 return *slot;
8816 ref_p = XCNEW (struct external_ref);
8817 ref_p->type = die;
8818 *slot = ref_p;
8819 return ref_p;
8822 /* Subroutine of optimize_external_refs, below.
8824 If we see a type skeleton, record it as our stub. If we see external
8825 references, remember how many we've seen. */
8827 static void
8828 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8830 dw_die_ref c;
8831 dw_attr_node *a;
8832 unsigned ix;
8833 struct external_ref *ref_p;
8835 if (is_type_die (die)
8836 && (c = get_AT_ref (die, DW_AT_signature)))
8838 /* This is a local skeleton; use it for local references. */
8839 ref_p = lookup_external_ref (map, c);
8840 ref_p->stub = die;
8843 /* Scan the DIE references, and remember any that refer to DIEs from
8844 other CUs (i.e. those which are not marked). */
8845 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8846 if (AT_class (a) == dw_val_class_die_ref
8847 && (c = AT_ref (a))->die_mark == 0
8848 && is_type_die (c))
8850 ref_p = lookup_external_ref (map, c);
8851 ref_p->n_refs++;
8854 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8857 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8858 points to an external_ref, DATA is the CU we're processing. If we don't
8859 already have a local stub, and we have multiple refs, build a stub. */
8862 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8864 struct external_ref *ref_p = *slot;
8866 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8868 /* We have multiple references to this type, so build a small stub.
8869 Both of these forms are a bit dodgy from the perspective of the
8870 DWARF standard, since technically they should have names. */
8871 dw_die_ref cu = data;
8872 dw_die_ref type = ref_p->type;
8873 dw_die_ref stub = NULL;
8875 if (type->comdat_type_p)
8877 /* If we refer to this type via sig8, use AT_signature. */
8878 stub = new_die (type->die_tag, cu, NULL_TREE);
8879 add_AT_die_ref (stub, DW_AT_signature, type);
8881 else
8883 /* Otherwise, use a typedef with no name. */
8884 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8885 add_AT_die_ref (stub, DW_AT_type, type);
8888 stub->die_mark++;
8889 ref_p->stub = stub;
8891 return 1;
8894 /* DIE is a unit; look through all the DIE references to see if there are
8895 any external references to types, and if so, create local stubs for
8896 them which will be applied in build_abbrev_table. This is useful because
8897 references to local DIEs are smaller. */
8899 static external_ref_hash_type *
8900 optimize_external_refs (dw_die_ref die)
8902 external_ref_hash_type *map = new external_ref_hash_type (10);
8903 optimize_external_refs_1 (die, map);
8904 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8905 return map;
8908 /* The following 3 variables are temporaries that are computed only during the
8909 build_abbrev_table call and used and released during the following
8910 optimize_abbrev_table call. */
8912 /* First abbrev_id that can be optimized based on usage. */
8913 static unsigned int abbrev_opt_start;
8915 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8916 abbrev_id smaller than this, because they must be already sized
8917 during build_abbrev_table). */
8918 static unsigned int abbrev_opt_base_type_end;
8920 /* Vector of usage counts during build_abbrev_table. Indexed by
8921 abbrev_id - abbrev_opt_start. */
8922 static vec<unsigned int> abbrev_usage_count;
8924 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8925 static vec<dw_die_ref> sorted_abbrev_dies;
8927 /* The format of each DIE (and its attribute value pairs) is encoded in an
8928 abbreviation table. This routine builds the abbreviation table and assigns
8929 a unique abbreviation id for each abbreviation entry. The children of each
8930 die are visited recursively. */
8932 static void
8933 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8935 unsigned int abbrev_id = 0;
8936 dw_die_ref c;
8937 dw_attr_node *a;
8938 unsigned ix;
8939 dw_die_ref abbrev;
8941 /* Scan the DIE references, and replace any that refer to
8942 DIEs from other CUs (i.e. those which are not marked) with
8943 the local stubs we built in optimize_external_refs. */
8944 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8945 if (AT_class (a) == dw_val_class_die_ref
8946 && (c = AT_ref (a))->die_mark == 0)
8948 struct external_ref *ref_p;
8949 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8951 ref_p = lookup_external_ref (extern_map, c);
8952 if (ref_p->stub && ref_p->stub != die)
8953 change_AT_die_ref (a, ref_p->stub);
8954 else
8955 /* We aren't changing this reference, so mark it external. */
8956 set_AT_ref_external (a, 1);
8959 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8961 dw_attr_node *die_a, *abbrev_a;
8962 unsigned ix;
8963 bool ok = true;
8965 if (abbrev_id == 0)
8966 continue;
8967 if (abbrev->die_tag != die->die_tag)
8968 continue;
8969 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8970 continue;
8972 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8973 continue;
8975 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8977 abbrev_a = &(*abbrev->die_attr)[ix];
8978 if ((abbrev_a->dw_attr != die_a->dw_attr)
8979 || (value_format (abbrev_a) != value_format (die_a)))
8981 ok = false;
8982 break;
8985 if (ok)
8986 break;
8989 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8991 vec_safe_push (abbrev_die_table, die);
8992 if (abbrev_opt_start)
8993 abbrev_usage_count.safe_push (0);
8995 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8997 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8998 sorted_abbrev_dies.safe_push (die);
9001 die->die_abbrev = abbrev_id;
9002 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9005 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9006 by die_abbrev's usage count, from the most commonly used
9007 abbreviation to the least. */
9009 static int
9010 die_abbrev_cmp (const void *p1, const void *p2)
9012 dw_die_ref die1 = *(const dw_die_ref *) p1;
9013 dw_die_ref die2 = *(const dw_die_ref *) p2;
9015 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9016 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9018 if (die1->die_abbrev >= abbrev_opt_base_type_end
9019 && die2->die_abbrev >= abbrev_opt_base_type_end)
9021 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9022 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9023 return -1;
9024 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9025 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9026 return 1;
9029 /* Stabilize the sort. */
9030 if (die1->die_abbrev < die2->die_abbrev)
9031 return -1;
9032 if (die1->die_abbrev > die2->die_abbrev)
9033 return 1;
9035 return 0;
9038 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9039 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9040 into dw_val_class_const_implicit or
9041 dw_val_class_unsigned_const_implicit. */
9043 static void
9044 optimize_implicit_const (unsigned int first_id, unsigned int end,
9045 vec<bool> &implicit_consts)
9047 /* It never makes sense if there is just one DIE using the abbreviation. */
9048 if (end < first_id + 2)
9049 return;
9051 dw_attr_node *a;
9052 unsigned ix, i;
9053 dw_die_ref die = sorted_abbrev_dies[first_id];
9054 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9055 if (implicit_consts[ix])
9057 enum dw_val_class new_class = dw_val_class_none;
9058 switch (AT_class (a))
9060 case dw_val_class_unsigned_const:
9061 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9062 continue;
9064 /* The .debug_abbrev section will grow by
9065 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9066 in all the DIEs using that abbreviation. */
9067 if (constant_size (AT_unsigned (a)) * (end - first_id)
9068 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9069 continue;
9071 new_class = dw_val_class_unsigned_const_implicit;
9072 break;
9074 case dw_val_class_const:
9075 new_class = dw_val_class_const_implicit;
9076 break;
9078 case dw_val_class_file:
9079 new_class = dw_val_class_file_implicit;
9080 break;
9082 default:
9083 continue;
9085 for (i = first_id; i < end; i++)
9086 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9087 = new_class;
9091 /* Attempt to optimize abbreviation table from abbrev_opt_start
9092 abbreviation above. */
9094 static void
9095 optimize_abbrev_table (void)
9097 if (abbrev_opt_start
9098 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9099 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9101 auto_vec<bool, 32> implicit_consts;
9102 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9104 unsigned int abbrev_id = abbrev_opt_start - 1;
9105 unsigned int first_id = ~0U;
9106 unsigned int last_abbrev_id = 0;
9107 unsigned int i;
9108 dw_die_ref die;
9109 if (abbrev_opt_base_type_end > abbrev_opt_start)
9110 abbrev_id = abbrev_opt_base_type_end - 1;
9111 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9112 most commonly used abbreviations come first. */
9113 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9115 dw_attr_node *a;
9116 unsigned ix;
9118 /* If calc_base_type_die_sizes has been called, the CU and
9119 base types after it can't be optimized, because we've already
9120 calculated their DIE offsets. We've sorted them first. */
9121 if (die->die_abbrev < abbrev_opt_base_type_end)
9122 continue;
9123 if (die->die_abbrev != last_abbrev_id)
9125 last_abbrev_id = die->die_abbrev;
9126 if (dwarf_version >= 5 && first_id != ~0U)
9127 optimize_implicit_const (first_id, i, implicit_consts);
9128 abbrev_id++;
9129 (*abbrev_die_table)[abbrev_id] = die;
9130 if (dwarf_version >= 5)
9132 first_id = i;
9133 implicit_consts.truncate (0);
9135 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9136 switch (AT_class (a))
9138 case dw_val_class_const:
9139 case dw_val_class_unsigned_const:
9140 case dw_val_class_file:
9141 implicit_consts.safe_push (true);
9142 break;
9143 default:
9144 implicit_consts.safe_push (false);
9145 break;
9149 else if (dwarf_version >= 5)
9151 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9152 if (!implicit_consts[ix])
9153 continue;
9154 else
9156 dw_attr_node *other_a
9157 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9158 if (!dw_val_equal_p (&a->dw_attr_val,
9159 &other_a->dw_attr_val))
9160 implicit_consts[ix] = false;
9163 die->die_abbrev = abbrev_id;
9165 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9166 if (dwarf_version >= 5 && first_id != ~0U)
9167 optimize_implicit_const (first_id, i, implicit_consts);
9170 abbrev_opt_start = 0;
9171 abbrev_opt_base_type_end = 0;
9172 abbrev_usage_count.release ();
9173 sorted_abbrev_dies.release ();
9176 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9178 static int
9179 constant_size (unsigned HOST_WIDE_INT value)
9181 int log;
9183 if (value == 0)
9184 log = 0;
9185 else
9186 log = floor_log2 (value);
9188 log = log / 8;
9189 log = 1 << (floor_log2 (log) + 1);
9191 return log;
9194 /* Return the size of a DIE as it is represented in the
9195 .debug_info section. */
9197 static unsigned long
9198 size_of_die (dw_die_ref die)
9200 unsigned long size = 0;
9201 dw_attr_node *a;
9202 unsigned ix;
9203 enum dwarf_form form;
9205 size += size_of_uleb128 (die->die_abbrev);
9206 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9208 switch (AT_class (a))
9210 case dw_val_class_addr:
9211 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9213 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9214 size += size_of_uleb128 (AT_index (a));
9216 else
9217 size += DWARF2_ADDR_SIZE;
9218 break;
9219 case dw_val_class_offset:
9220 size += DWARF_OFFSET_SIZE;
9221 break;
9222 case dw_val_class_loc:
9224 unsigned long lsize = size_of_locs (AT_loc (a));
9226 /* Block length. */
9227 if (dwarf_version >= 4)
9228 size += size_of_uleb128 (lsize);
9229 else
9230 size += constant_size (lsize);
9231 size += lsize;
9233 break;
9234 case dw_val_class_loc_list:
9235 if (dwarf_split_debug_info && dwarf_version >= 5)
9237 gcc_assert (AT_loc_list (a)->num_assigned);
9238 size += size_of_uleb128 (AT_loc_list (a)->hash);
9240 else
9241 size += DWARF_OFFSET_SIZE;
9242 break;
9243 case dw_val_class_range_list:
9244 if (value_format (a) == DW_FORM_rnglistx)
9246 gcc_assert (rnglist_idx);
9247 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9248 size += size_of_uleb128 (r->idx);
9250 else
9251 size += DWARF_OFFSET_SIZE;
9252 break;
9253 case dw_val_class_const:
9254 size += size_of_sleb128 (AT_int (a));
9255 break;
9256 case dw_val_class_unsigned_const:
9258 int csize = constant_size (AT_unsigned (a));
9259 if (dwarf_version == 3
9260 && a->dw_attr == DW_AT_data_member_location
9261 && csize >= 4)
9262 size += size_of_uleb128 (AT_unsigned (a));
9263 else
9264 size += csize;
9266 break;
9267 case dw_val_class_const_implicit:
9268 case dw_val_class_unsigned_const_implicit:
9269 case dw_val_class_file_implicit:
9270 /* These occupy no size in the DIE, just an extra sleb128 in
9271 .debug_abbrev. */
9272 break;
9273 case dw_val_class_const_double:
9274 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9275 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9276 size++; /* block */
9277 break;
9278 case dw_val_class_wide_int:
9279 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9280 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9281 if (get_full_len (*a->dw_attr_val.v.val_wide)
9282 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9283 size++; /* block */
9284 break;
9285 case dw_val_class_vec:
9286 size += constant_size (a->dw_attr_val.v.val_vec.length
9287 * a->dw_attr_val.v.val_vec.elt_size)
9288 + a->dw_attr_val.v.val_vec.length
9289 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9290 break;
9291 case dw_val_class_flag:
9292 if (dwarf_version >= 4)
9293 /* Currently all add_AT_flag calls pass in 1 as last argument,
9294 so DW_FORM_flag_present can be used. If that ever changes,
9295 we'll need to use DW_FORM_flag and have some optimization
9296 in build_abbrev_table that will change those to
9297 DW_FORM_flag_present if it is set to 1 in all DIEs using
9298 the same abbrev entry. */
9299 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9300 else
9301 size += 1;
9302 break;
9303 case dw_val_class_die_ref:
9304 if (AT_ref_external (a))
9306 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9307 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9308 is sized by target address length, whereas in DWARF3
9309 it's always sized as an offset. */
9310 if (use_debug_types)
9311 size += DWARF_TYPE_SIGNATURE_SIZE;
9312 else if (dwarf_version == 2)
9313 size += DWARF2_ADDR_SIZE;
9314 else
9315 size += DWARF_OFFSET_SIZE;
9317 else
9318 size += DWARF_OFFSET_SIZE;
9319 break;
9320 case dw_val_class_fde_ref:
9321 size += DWARF_OFFSET_SIZE;
9322 break;
9323 case dw_val_class_lbl_id:
9324 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9326 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9327 size += size_of_uleb128 (AT_index (a));
9329 else
9330 size += DWARF2_ADDR_SIZE;
9331 break;
9332 case dw_val_class_lineptr:
9333 case dw_val_class_macptr:
9334 case dw_val_class_loclistsptr:
9335 size += DWARF_OFFSET_SIZE;
9336 break;
9337 case dw_val_class_str:
9338 form = AT_string_form (a);
9339 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9340 size += DWARF_OFFSET_SIZE;
9341 else if (form == DW_FORM_GNU_str_index)
9342 size += size_of_uleb128 (AT_index (a));
9343 else
9344 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9345 break;
9346 case dw_val_class_file:
9347 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9348 break;
9349 case dw_val_class_data8:
9350 size += 8;
9351 break;
9352 case dw_val_class_vms_delta:
9353 size += DWARF_OFFSET_SIZE;
9354 break;
9355 case dw_val_class_high_pc:
9356 size += DWARF2_ADDR_SIZE;
9357 break;
9358 case dw_val_class_discr_value:
9359 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9360 break;
9361 case dw_val_class_discr_list:
9363 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9365 /* This is a block, so we have the block length and then its
9366 data. */
9367 size += constant_size (block_size) + block_size;
9369 break;
9370 default:
9371 gcc_unreachable ();
9375 return size;
9378 /* Size the debugging information associated with a given DIE. Visits the
9379 DIE's children recursively. Updates the global variable next_die_offset, on
9380 each time through. Uses the current value of next_die_offset to update the
9381 die_offset field in each DIE. */
9383 static void
9384 calc_die_sizes (dw_die_ref die)
9386 dw_die_ref c;
9388 gcc_assert (die->die_offset == 0
9389 || (unsigned long int) die->die_offset == next_die_offset);
9390 die->die_offset = next_die_offset;
9391 next_die_offset += size_of_die (die);
9393 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9395 if (die->die_child != NULL)
9396 /* Count the null byte used to terminate sibling lists. */
9397 next_die_offset += 1;
9400 /* Size just the base type children at the start of the CU.
9401 This is needed because build_abbrev needs to size locs
9402 and sizing of type based stack ops needs to know die_offset
9403 values for the base types. */
9405 static void
9406 calc_base_type_die_sizes (void)
9408 unsigned long die_offset = (dwarf_split_debug_info
9409 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9410 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9411 unsigned int i;
9412 dw_die_ref base_type;
9413 #if ENABLE_ASSERT_CHECKING
9414 dw_die_ref prev = comp_unit_die ()->die_child;
9415 #endif
9417 die_offset += size_of_die (comp_unit_die ());
9418 for (i = 0; base_types.iterate (i, &base_type); i++)
9420 #if ENABLE_ASSERT_CHECKING
9421 gcc_assert (base_type->die_offset == 0
9422 && prev->die_sib == base_type
9423 && base_type->die_child == NULL
9424 && base_type->die_abbrev);
9425 prev = base_type;
9426 #endif
9427 if (abbrev_opt_start
9428 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9429 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9430 base_type->die_offset = die_offset;
9431 die_offset += size_of_die (base_type);
9435 /* Set the marks for a die and its children. We do this so
9436 that we know whether or not a reference needs to use FORM_ref_addr; only
9437 DIEs in the same CU will be marked. We used to clear out the offset
9438 and use that as the flag, but ran into ordering problems. */
9440 static void
9441 mark_dies (dw_die_ref die)
9443 dw_die_ref c;
9445 gcc_assert (!die->die_mark);
9447 die->die_mark = 1;
9448 FOR_EACH_CHILD (die, c, mark_dies (c));
9451 /* Clear the marks for a die and its children. */
9453 static void
9454 unmark_dies (dw_die_ref die)
9456 dw_die_ref c;
9458 if (! use_debug_types)
9459 gcc_assert (die->die_mark);
9461 die->die_mark = 0;
9462 FOR_EACH_CHILD (die, c, unmark_dies (c));
9465 /* Clear the marks for a die, its children and referred dies. */
9467 static void
9468 unmark_all_dies (dw_die_ref die)
9470 dw_die_ref c;
9471 dw_attr_node *a;
9472 unsigned ix;
9474 if (!die->die_mark)
9475 return;
9476 die->die_mark = 0;
9478 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9480 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9481 if (AT_class (a) == dw_val_class_die_ref)
9482 unmark_all_dies (AT_ref (a));
9485 /* Calculate if the entry should appear in the final output file. It may be
9486 from a pruned a type. */
9488 static bool
9489 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9491 /* By limiting gnu pubnames to definitions only, gold can generate a
9492 gdb index without entries for declarations, which don't include
9493 enough information to be useful. */
9494 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9495 return false;
9497 if (table == pubname_table)
9499 /* Enumerator names are part of the pubname table, but the
9500 parent DW_TAG_enumeration_type die may have been pruned.
9501 Don't output them if that is the case. */
9502 if (p->die->die_tag == DW_TAG_enumerator &&
9503 (p->die->die_parent == NULL
9504 || !p->die->die_parent->die_perennial_p))
9505 return false;
9507 /* Everything else in the pubname table is included. */
9508 return true;
9511 /* The pubtypes table shouldn't include types that have been
9512 pruned. */
9513 return (p->die->die_offset != 0
9514 || !flag_eliminate_unused_debug_types);
9517 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9518 generated for the compilation unit. */
9520 static unsigned long
9521 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9523 unsigned long size;
9524 unsigned i;
9525 pubname_entry *p;
9526 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9528 size = DWARF_PUBNAMES_HEADER_SIZE;
9529 FOR_EACH_VEC_ELT (*names, i, p)
9530 if (include_pubname_in_output (names, p))
9531 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9533 size += DWARF_OFFSET_SIZE;
9534 return size;
9537 /* Return the size of the information in the .debug_aranges section. */
9539 static unsigned long
9540 size_of_aranges (void)
9542 unsigned long size;
9544 size = DWARF_ARANGES_HEADER_SIZE;
9546 /* Count the address/length pair for this compilation unit. */
9547 if (text_section_used)
9548 size += 2 * DWARF2_ADDR_SIZE;
9549 if (cold_text_section_used)
9550 size += 2 * DWARF2_ADDR_SIZE;
9551 if (have_multiple_function_sections)
9553 unsigned fde_idx;
9554 dw_fde_ref fde;
9556 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9558 if (DECL_IGNORED_P (fde->decl))
9559 continue;
9560 if (!fde->in_std_section)
9561 size += 2 * DWARF2_ADDR_SIZE;
9562 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9563 size += 2 * DWARF2_ADDR_SIZE;
9567 /* Count the two zero words used to terminated the address range table. */
9568 size += 2 * DWARF2_ADDR_SIZE;
9569 return size;
9572 /* Select the encoding of an attribute value. */
9574 static enum dwarf_form
9575 value_format (dw_attr_node *a)
9577 switch (AT_class (a))
9579 case dw_val_class_addr:
9580 /* Only very few attributes allow DW_FORM_addr. */
9581 switch (a->dw_attr)
9583 case DW_AT_low_pc:
9584 case DW_AT_high_pc:
9585 case DW_AT_entry_pc:
9586 case DW_AT_trampoline:
9587 return (AT_index (a) == NOT_INDEXED
9588 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9589 default:
9590 break;
9592 switch (DWARF2_ADDR_SIZE)
9594 case 1:
9595 return DW_FORM_data1;
9596 case 2:
9597 return DW_FORM_data2;
9598 case 4:
9599 return DW_FORM_data4;
9600 case 8:
9601 return DW_FORM_data8;
9602 default:
9603 gcc_unreachable ();
9605 case dw_val_class_loc_list:
9606 if (dwarf_split_debug_info
9607 && dwarf_version >= 5
9608 && AT_loc_list (a)->num_assigned)
9609 return DW_FORM_loclistx;
9610 /* FALLTHRU */
9611 case dw_val_class_range_list:
9612 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9613 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9614 care about sizes of .debug* sections in shared libraries and
9615 executables and don't take into account relocations that affect just
9616 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9617 table in the .debug_rnglists section. */
9618 if (dwarf_split_debug_info
9619 && dwarf_version >= 5
9620 && AT_class (a) == dw_val_class_range_list
9621 && rnglist_idx
9622 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9623 return DW_FORM_rnglistx;
9624 if (dwarf_version >= 4)
9625 return DW_FORM_sec_offset;
9626 /* FALLTHRU */
9627 case dw_val_class_vms_delta:
9628 case dw_val_class_offset:
9629 switch (DWARF_OFFSET_SIZE)
9631 case 4:
9632 return DW_FORM_data4;
9633 case 8:
9634 return DW_FORM_data8;
9635 default:
9636 gcc_unreachable ();
9638 case dw_val_class_loc:
9639 if (dwarf_version >= 4)
9640 return DW_FORM_exprloc;
9641 switch (constant_size (size_of_locs (AT_loc (a))))
9643 case 1:
9644 return DW_FORM_block1;
9645 case 2:
9646 return DW_FORM_block2;
9647 case 4:
9648 return DW_FORM_block4;
9649 default:
9650 gcc_unreachable ();
9652 case dw_val_class_const:
9653 return DW_FORM_sdata;
9654 case dw_val_class_unsigned_const:
9655 switch (constant_size (AT_unsigned (a)))
9657 case 1:
9658 return DW_FORM_data1;
9659 case 2:
9660 return DW_FORM_data2;
9661 case 4:
9662 /* In DWARF3 DW_AT_data_member_location with
9663 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9664 constant, so we need to use DW_FORM_udata if we need
9665 a large constant. */
9666 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9667 return DW_FORM_udata;
9668 return DW_FORM_data4;
9669 case 8:
9670 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9671 return DW_FORM_udata;
9672 return DW_FORM_data8;
9673 default:
9674 gcc_unreachable ();
9676 case dw_val_class_const_implicit:
9677 case dw_val_class_unsigned_const_implicit:
9678 case dw_val_class_file_implicit:
9679 return DW_FORM_implicit_const;
9680 case dw_val_class_const_double:
9681 switch (HOST_BITS_PER_WIDE_INT)
9683 case 8:
9684 return DW_FORM_data2;
9685 case 16:
9686 return DW_FORM_data4;
9687 case 32:
9688 return DW_FORM_data8;
9689 case 64:
9690 if (dwarf_version >= 5)
9691 return DW_FORM_data16;
9692 /* FALLTHRU */
9693 default:
9694 return DW_FORM_block1;
9696 case dw_val_class_wide_int:
9697 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9699 case 8:
9700 return DW_FORM_data1;
9701 case 16:
9702 return DW_FORM_data2;
9703 case 32:
9704 return DW_FORM_data4;
9705 case 64:
9706 return DW_FORM_data8;
9707 case 128:
9708 if (dwarf_version >= 5)
9709 return DW_FORM_data16;
9710 /* FALLTHRU */
9711 default:
9712 return DW_FORM_block1;
9714 case dw_val_class_vec:
9715 switch (constant_size (a->dw_attr_val.v.val_vec.length
9716 * a->dw_attr_val.v.val_vec.elt_size))
9718 case 1:
9719 return DW_FORM_block1;
9720 case 2:
9721 return DW_FORM_block2;
9722 case 4:
9723 return DW_FORM_block4;
9724 default:
9725 gcc_unreachable ();
9727 case dw_val_class_flag:
9728 if (dwarf_version >= 4)
9730 /* Currently all add_AT_flag calls pass in 1 as last argument,
9731 so DW_FORM_flag_present can be used. If that ever changes,
9732 we'll need to use DW_FORM_flag and have some optimization
9733 in build_abbrev_table that will change those to
9734 DW_FORM_flag_present if it is set to 1 in all DIEs using
9735 the same abbrev entry. */
9736 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9737 return DW_FORM_flag_present;
9739 return DW_FORM_flag;
9740 case dw_val_class_die_ref:
9741 if (AT_ref_external (a))
9742 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9743 else
9744 return DW_FORM_ref;
9745 case dw_val_class_fde_ref:
9746 return DW_FORM_data;
9747 case dw_val_class_lbl_id:
9748 return (AT_index (a) == NOT_INDEXED
9749 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9750 case dw_val_class_lineptr:
9751 case dw_val_class_macptr:
9752 case dw_val_class_loclistsptr:
9753 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9754 case dw_val_class_str:
9755 return AT_string_form (a);
9756 case dw_val_class_file:
9757 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9759 case 1:
9760 return DW_FORM_data1;
9761 case 2:
9762 return DW_FORM_data2;
9763 case 4:
9764 return DW_FORM_data4;
9765 default:
9766 gcc_unreachable ();
9769 case dw_val_class_data8:
9770 return DW_FORM_data8;
9772 case dw_val_class_high_pc:
9773 switch (DWARF2_ADDR_SIZE)
9775 case 1:
9776 return DW_FORM_data1;
9777 case 2:
9778 return DW_FORM_data2;
9779 case 4:
9780 return DW_FORM_data4;
9781 case 8:
9782 return DW_FORM_data8;
9783 default:
9784 gcc_unreachable ();
9787 case dw_val_class_discr_value:
9788 return (a->dw_attr_val.v.val_discr_value.pos
9789 ? DW_FORM_udata
9790 : DW_FORM_sdata);
9791 case dw_val_class_discr_list:
9792 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9794 case 1:
9795 return DW_FORM_block1;
9796 case 2:
9797 return DW_FORM_block2;
9798 case 4:
9799 return DW_FORM_block4;
9800 default:
9801 gcc_unreachable ();
9804 default:
9805 gcc_unreachable ();
9809 /* Output the encoding of an attribute value. */
9811 static void
9812 output_value_format (dw_attr_node *a)
9814 enum dwarf_form form = value_format (a);
9816 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9819 /* Given a die and id, produce the appropriate abbreviations. */
9821 static void
9822 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9824 unsigned ix;
9825 dw_attr_node *a_attr;
9827 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9828 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9829 dwarf_tag_name (abbrev->die_tag));
9831 if (abbrev->die_child != NULL)
9832 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9833 else
9834 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9836 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9838 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9839 dwarf_attr_name (a_attr->dw_attr));
9840 output_value_format (a_attr);
9841 if (value_format (a_attr) == DW_FORM_implicit_const)
9843 if (AT_class (a_attr) == dw_val_class_file_implicit)
9845 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9846 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9847 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9849 else
9850 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9854 dw2_asm_output_data (1, 0, NULL);
9855 dw2_asm_output_data (1, 0, NULL);
9859 /* Output the .debug_abbrev section which defines the DIE abbreviation
9860 table. */
9862 static void
9863 output_abbrev_section (void)
9865 unsigned int abbrev_id;
9866 dw_die_ref abbrev;
9868 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9869 if (abbrev_id != 0)
9870 output_die_abbrevs (abbrev_id, abbrev);
9872 /* Terminate the table. */
9873 dw2_asm_output_data (1, 0, NULL);
9876 /* Output a symbol we can use to refer to this DIE from another CU. */
9878 static inline void
9879 output_die_symbol (dw_die_ref die)
9881 const char *sym = die->die_id.die_symbol;
9883 gcc_assert (!die->comdat_type_p);
9885 if (sym == 0)
9886 return;
9888 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9889 /* We make these global, not weak; if the target doesn't support
9890 .linkonce, it doesn't support combining the sections, so debugging
9891 will break. */
9892 targetm.asm_out.globalize_label (asm_out_file, sym);
9894 ASM_OUTPUT_LABEL (asm_out_file, sym);
9897 /* Return a new location list, given the begin and end range, and the
9898 expression. */
9900 static inline dw_loc_list_ref
9901 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9902 const char *section)
9904 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9906 retlist->begin = begin;
9907 retlist->begin_entry = NULL;
9908 retlist->end = end;
9909 retlist->expr = expr;
9910 retlist->section = section;
9912 return retlist;
9915 /* Generate a new internal symbol for this location list node, if it
9916 hasn't got one yet. */
9918 static inline void
9919 gen_llsym (dw_loc_list_ref list)
9921 gcc_assert (!list->ll_symbol);
9922 list->ll_symbol = gen_internal_sym ("LLST");
9925 /* Output the location list given to us. */
9927 static void
9928 output_loc_list (dw_loc_list_ref list_head)
9930 if (list_head->emitted)
9931 return;
9932 list_head->emitted = true;
9934 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9936 dw_loc_list_ref curr = list_head;
9937 const char *last_section = NULL;
9938 const char *base_label = NULL;
9940 /* Walk the location list, and output each range + expression. */
9941 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9943 unsigned long size;
9944 /* Don't output an entry that starts and ends at the same address. */
9945 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9946 continue;
9947 size = size_of_locs (curr->expr);
9948 /* If the expression is too large, drop it on the floor. We could
9949 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9950 in the expression, but >= 64KB expressions for a single value
9951 in a single range are unlikely very useful. */
9952 if (dwarf_version < 5 && size > 0xffff)
9953 continue;
9954 if (dwarf_version >= 5)
9956 if (dwarf_split_debug_info)
9958 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9959 uleb128 index into .debug_addr and uleb128 length. */
9960 dw2_asm_output_data (1, DW_LLE_startx_length,
9961 "DW_LLE_startx_length (%s)",
9962 list_head->ll_symbol);
9963 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9964 "Location list range start index "
9965 "(%s)", curr->begin);
9966 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9967 For that case we probably need to emit DW_LLE_startx_endx,
9968 but we'd need 2 .debug_addr entries rather than just one. */
9969 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9970 "Location list length (%s)",
9971 list_head->ll_symbol);
9973 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9975 /* If all code is in .text section, the base address is
9976 already provided by the CU attributes. Use
9977 DW_LLE_offset_pair where both addresses are uleb128 encoded
9978 offsets against that base. */
9979 dw2_asm_output_data (1, DW_LLE_offset_pair,
9980 "DW_LLE_offset_pair (%s)",
9981 list_head->ll_symbol);
9982 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9983 "Location list begin address (%s)",
9984 list_head->ll_symbol);
9985 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9986 "Location list end address (%s)",
9987 list_head->ll_symbol);
9989 else if (HAVE_AS_LEB128)
9991 /* Otherwise, find out how many consecutive entries could share
9992 the same base entry. If just one, emit DW_LLE_start_length,
9993 otherwise emit DW_LLE_base_address for the base address
9994 followed by a series of DW_LLE_offset_pair. */
9995 if (last_section == NULL || curr->section != last_section)
9997 dw_loc_list_ref curr2;
9998 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9999 curr2 = curr2->dw_loc_next)
10001 if (strcmp (curr2->begin, curr2->end) == 0
10002 && !curr2->force)
10003 continue;
10004 break;
10006 if (curr2 == NULL || curr->section != curr2->section)
10007 last_section = NULL;
10008 else
10010 last_section = curr->section;
10011 base_label = curr->begin;
10012 dw2_asm_output_data (1, DW_LLE_base_address,
10013 "DW_LLE_base_address (%s)",
10014 list_head->ll_symbol);
10015 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10016 "Base address (%s)",
10017 list_head->ll_symbol);
10020 /* Only one entry with the same base address. Use
10021 DW_LLE_start_length with absolute address and uleb128
10022 length. */
10023 if (last_section == NULL)
10025 dw2_asm_output_data (1, DW_LLE_start_length,
10026 "DW_LLE_start_length (%s)",
10027 list_head->ll_symbol);
10028 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10029 "Location list begin address (%s)",
10030 list_head->ll_symbol);
10031 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10032 "Location list length "
10033 "(%s)", list_head->ll_symbol);
10035 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10036 DW_LLE_base_address. */
10037 else
10039 dw2_asm_output_data (1, DW_LLE_offset_pair,
10040 "DW_LLE_offset_pair (%s)",
10041 list_head->ll_symbol);
10042 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10043 "Location list begin address "
10044 "(%s)", list_head->ll_symbol);
10045 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10046 "Location list end address "
10047 "(%s)", list_head->ll_symbol);
10050 /* The assembler does not support .uleb128 directive. Emit
10051 DW_LLE_start_end with a pair of absolute addresses. */
10052 else
10054 dw2_asm_output_data (1, DW_LLE_start_end,
10055 "DW_LLE_start_end (%s)",
10056 list_head->ll_symbol);
10057 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10058 "Location list begin address (%s)",
10059 list_head->ll_symbol);
10060 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10061 "Location list end address (%s)",
10062 list_head->ll_symbol);
10065 else if (dwarf_split_debug_info)
10067 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10068 and 4 byte length. */
10069 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10070 "Location list start/length entry (%s)",
10071 list_head->ll_symbol);
10072 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10073 "Location list range start index (%s)",
10074 curr->begin);
10075 /* The length field is 4 bytes. If we ever need to support
10076 an 8-byte length, we can add a new DW_LLE code or fall back
10077 to DW_LLE_GNU_start_end_entry. */
10078 dw2_asm_output_delta (4, curr->end, curr->begin,
10079 "Location list range length (%s)",
10080 list_head->ll_symbol);
10082 else if (!have_multiple_function_sections)
10084 /* Pair of relative addresses against start of text section. */
10085 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10086 "Location list begin address (%s)",
10087 list_head->ll_symbol);
10088 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10089 "Location list end address (%s)",
10090 list_head->ll_symbol);
10092 else
10094 /* Pair of absolute addresses. */
10095 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10096 "Location list begin address (%s)",
10097 list_head->ll_symbol);
10098 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10099 "Location list end address (%s)",
10100 list_head->ll_symbol);
10103 /* Output the block length for this list of location operations. */
10104 if (dwarf_version >= 5)
10105 dw2_asm_output_data_uleb128 (size, "Location expression size");
10106 else
10108 gcc_assert (size <= 0xffff);
10109 dw2_asm_output_data (2, size, "Location expression size");
10112 output_loc_sequence (curr->expr, -1);
10115 /* And finally list termination. */
10116 if (dwarf_version >= 5)
10117 dw2_asm_output_data (1, DW_LLE_end_of_list,
10118 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10119 else if (dwarf_split_debug_info)
10120 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10121 "Location list terminator (%s)",
10122 list_head->ll_symbol);
10123 else
10125 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10126 "Location list terminator begin (%s)",
10127 list_head->ll_symbol);
10128 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10129 "Location list terminator end (%s)",
10130 list_head->ll_symbol);
10134 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10135 section. Emit a relocated reference if val_entry is NULL, otherwise,
10136 emit an indirect reference. */
10138 static void
10139 output_range_list_offset (dw_attr_node *a)
10141 const char *name = dwarf_attr_name (a->dw_attr);
10143 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10145 if (dwarf_version >= 5)
10147 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10148 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10149 debug_ranges_section, "%s", name);
10151 else
10153 char *p = strchr (ranges_section_label, '\0');
10154 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10155 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10156 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10157 debug_ranges_section, "%s", name);
10158 *p = '\0';
10161 else if (dwarf_version >= 5)
10163 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10164 gcc_assert (rnglist_idx);
10165 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10167 else
10168 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10169 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10170 "%s (offset from %s)", name, ranges_section_label);
10173 /* Output the offset into the debug_loc section. */
10175 static void
10176 output_loc_list_offset (dw_attr_node *a)
10178 char *sym = AT_loc_list (a)->ll_symbol;
10180 gcc_assert (sym);
10181 if (!dwarf_split_debug_info)
10182 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10183 "%s", dwarf_attr_name (a->dw_attr));
10184 else if (dwarf_version >= 5)
10186 gcc_assert (AT_loc_list (a)->num_assigned);
10187 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10188 dwarf_attr_name (a->dw_attr),
10189 sym);
10191 else
10192 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10193 "%s", dwarf_attr_name (a->dw_attr));
10196 /* Output an attribute's index or value appropriately. */
10198 static void
10199 output_attr_index_or_value (dw_attr_node *a)
10201 const char *name = dwarf_attr_name (a->dw_attr);
10203 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10205 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10206 return;
10208 switch (AT_class (a))
10210 case dw_val_class_addr:
10211 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10212 break;
10213 case dw_val_class_high_pc:
10214 case dw_val_class_lbl_id:
10215 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10216 break;
10217 default:
10218 gcc_unreachable ();
10222 /* Output a type signature. */
10224 static inline void
10225 output_signature (const char *sig, const char *name)
10227 int i;
10229 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10230 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10233 /* Output a discriminant value. */
10235 static inline void
10236 output_discr_value (dw_discr_value *discr_value, const char *name)
10238 if (discr_value->pos)
10239 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10240 else
10241 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10244 /* Output the DIE and its attributes. Called recursively to generate
10245 the definitions of each child DIE. */
10247 static void
10248 output_die (dw_die_ref die)
10250 dw_attr_node *a;
10251 dw_die_ref c;
10252 unsigned long size;
10253 unsigned ix;
10255 /* If someone in another CU might refer to us, set up a symbol for
10256 them to point to. */
10257 if (! die->comdat_type_p && die->die_id.die_symbol
10258 /* Don't output the symbol twice. For LTO we want the label
10259 on the section beginning, not on the actual DIE. */
10260 && (!flag_generate_lto
10261 || die->die_tag != DW_TAG_compile_unit))
10262 output_die_symbol (die);
10264 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10265 (unsigned long)die->die_offset,
10266 dwarf_tag_name (die->die_tag));
10268 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10270 const char *name = dwarf_attr_name (a->dw_attr);
10272 switch (AT_class (a))
10274 case dw_val_class_addr:
10275 output_attr_index_or_value (a);
10276 break;
10278 case dw_val_class_offset:
10279 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10280 "%s", name);
10281 break;
10283 case dw_val_class_range_list:
10284 output_range_list_offset (a);
10285 break;
10287 case dw_val_class_loc:
10288 size = size_of_locs (AT_loc (a));
10290 /* Output the block length for this list of location operations. */
10291 if (dwarf_version >= 4)
10292 dw2_asm_output_data_uleb128 (size, "%s", name);
10293 else
10294 dw2_asm_output_data (constant_size (size), size, "%s", name);
10296 output_loc_sequence (AT_loc (a), -1);
10297 break;
10299 case dw_val_class_const:
10300 /* ??? It would be slightly more efficient to use a scheme like is
10301 used for unsigned constants below, but gdb 4.x does not sign
10302 extend. Gdb 5.x does sign extend. */
10303 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10304 break;
10306 case dw_val_class_unsigned_const:
10308 int csize = constant_size (AT_unsigned (a));
10309 if (dwarf_version == 3
10310 && a->dw_attr == DW_AT_data_member_location
10311 && csize >= 4)
10312 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10313 else
10314 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10316 break;
10318 case dw_val_class_const_implicit:
10319 if (flag_debug_asm)
10320 fprintf (asm_out_file, "\t\t\t%s %s ("
10321 HOST_WIDE_INT_PRINT_DEC ")\n",
10322 ASM_COMMENT_START, name, AT_int (a));
10323 break;
10325 case dw_val_class_unsigned_const_implicit:
10326 if (flag_debug_asm)
10327 fprintf (asm_out_file, "\t\t\t%s %s ("
10328 HOST_WIDE_INT_PRINT_HEX ")\n",
10329 ASM_COMMENT_START, name, AT_unsigned (a));
10330 break;
10332 case dw_val_class_const_double:
10334 unsigned HOST_WIDE_INT first, second;
10336 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10337 dw2_asm_output_data (1,
10338 HOST_BITS_PER_DOUBLE_INT
10339 / HOST_BITS_PER_CHAR,
10340 NULL);
10342 if (WORDS_BIG_ENDIAN)
10344 first = a->dw_attr_val.v.val_double.high;
10345 second = a->dw_attr_val.v.val_double.low;
10347 else
10349 first = a->dw_attr_val.v.val_double.low;
10350 second = a->dw_attr_val.v.val_double.high;
10353 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10354 first, "%s", name);
10355 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10356 second, NULL);
10358 break;
10360 case dw_val_class_wide_int:
10362 int i;
10363 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10364 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10365 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10366 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10367 * l, NULL);
10369 if (WORDS_BIG_ENDIAN)
10370 for (i = len - 1; i >= 0; --i)
10372 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10373 "%s", name);
10374 name = "";
10376 else
10377 for (i = 0; i < len; ++i)
10379 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10380 "%s", name);
10381 name = "";
10384 break;
10386 case dw_val_class_vec:
10388 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10389 unsigned int len = a->dw_attr_val.v.val_vec.length;
10390 unsigned int i;
10391 unsigned char *p;
10393 dw2_asm_output_data (constant_size (len * elt_size),
10394 len * elt_size, "%s", name);
10395 if (elt_size > sizeof (HOST_WIDE_INT))
10397 elt_size /= 2;
10398 len *= 2;
10400 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10401 i < len;
10402 i++, p += elt_size)
10403 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10404 "fp or vector constant word %u", i);
10405 break;
10408 case dw_val_class_flag:
10409 if (dwarf_version >= 4)
10411 /* Currently all add_AT_flag calls pass in 1 as last argument,
10412 so DW_FORM_flag_present can be used. If that ever changes,
10413 we'll need to use DW_FORM_flag and have some optimization
10414 in build_abbrev_table that will change those to
10415 DW_FORM_flag_present if it is set to 1 in all DIEs using
10416 the same abbrev entry. */
10417 gcc_assert (AT_flag (a) == 1);
10418 if (flag_debug_asm)
10419 fprintf (asm_out_file, "\t\t\t%s %s\n",
10420 ASM_COMMENT_START, name);
10421 break;
10423 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10424 break;
10426 case dw_val_class_loc_list:
10427 output_loc_list_offset (a);
10428 break;
10430 case dw_val_class_die_ref:
10431 if (AT_ref_external (a))
10433 if (AT_ref (a)->comdat_type_p)
10435 comdat_type_node *type_node
10436 = AT_ref (a)->die_id.die_type_node;
10438 gcc_assert (type_node);
10439 output_signature (type_node->signature, name);
10441 else
10443 const char *sym = AT_ref (a)->die_id.die_symbol;
10444 int size;
10446 gcc_assert (sym);
10447 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10448 length, whereas in DWARF3 it's always sized as an
10449 offset. */
10450 if (dwarf_version == 2)
10451 size = DWARF2_ADDR_SIZE;
10452 else
10453 size = DWARF_OFFSET_SIZE;
10454 /* ??? We cannot unconditionally output die_offset if
10455 non-zero - at least -feliminate-dwarf2-dups will
10456 create references to those DIEs via symbols. And we
10457 do not clear its DIE offset after outputting it
10458 (and the label refers to the actual DIEs, not the
10459 DWARF CU unit header which is when using label + offset
10460 would be the correct thing to do).
10461 ??? This is the reason for the with_offset flag. */
10462 if (AT_ref (a)->with_offset)
10463 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10464 debug_info_section, "%s", name);
10465 else
10466 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10467 name);
10470 else
10472 gcc_assert (AT_ref (a)->die_offset);
10473 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10474 "%s", name);
10476 break;
10478 case dw_val_class_fde_ref:
10480 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10482 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10483 a->dw_attr_val.v.val_fde_index * 2);
10484 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10485 "%s", name);
10487 break;
10489 case dw_val_class_vms_delta:
10490 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10491 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10492 AT_vms_delta2 (a), AT_vms_delta1 (a),
10493 "%s", name);
10494 #else
10495 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10496 AT_vms_delta2 (a), AT_vms_delta1 (a),
10497 "%s", name);
10498 #endif
10499 break;
10501 case dw_val_class_lbl_id:
10502 output_attr_index_or_value (a);
10503 break;
10505 case dw_val_class_lineptr:
10506 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10507 debug_line_section, "%s", name);
10508 break;
10510 case dw_val_class_macptr:
10511 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10512 debug_macinfo_section, "%s", name);
10513 break;
10515 case dw_val_class_loclistsptr:
10516 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10517 debug_loc_section, "%s", name);
10518 break;
10520 case dw_val_class_str:
10521 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10522 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10523 a->dw_attr_val.v.val_str->label,
10524 debug_str_section,
10525 "%s: \"%s\"", name, AT_string (a));
10526 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10527 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10528 a->dw_attr_val.v.val_str->label,
10529 debug_line_str_section,
10530 "%s: \"%s\"", name, AT_string (a));
10531 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10532 dw2_asm_output_data_uleb128 (AT_index (a),
10533 "%s: \"%s\"", name, AT_string (a));
10534 else
10535 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10536 break;
10538 case dw_val_class_file:
10540 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10542 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10543 a->dw_attr_val.v.val_file->filename);
10544 break;
10547 case dw_val_class_file_implicit:
10548 if (flag_debug_asm)
10549 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10550 ASM_COMMENT_START, name,
10551 maybe_emit_file (a->dw_attr_val.v.val_file),
10552 a->dw_attr_val.v.val_file->filename);
10553 break;
10555 case dw_val_class_data8:
10557 int i;
10559 for (i = 0; i < 8; i++)
10560 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10561 i == 0 ? "%s" : NULL, name);
10562 break;
10565 case dw_val_class_high_pc:
10566 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10567 get_AT_low_pc (die), "DW_AT_high_pc");
10568 break;
10570 case dw_val_class_discr_value:
10571 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10572 break;
10574 case dw_val_class_discr_list:
10576 dw_discr_list_ref list = AT_discr_list (a);
10577 const int size = size_of_discr_list (list);
10579 /* This is a block, so output its length first. */
10580 dw2_asm_output_data (constant_size (size), size,
10581 "%s: block size", name);
10583 for (; list != NULL; list = list->dw_discr_next)
10585 /* One byte for the discriminant value descriptor, and then as
10586 many LEB128 numbers as required. */
10587 if (list->dw_discr_range)
10588 dw2_asm_output_data (1, DW_DSC_range,
10589 "%s: DW_DSC_range", name);
10590 else
10591 dw2_asm_output_data (1, DW_DSC_label,
10592 "%s: DW_DSC_label", name);
10594 output_discr_value (&list->dw_discr_lower_bound, name);
10595 if (list->dw_discr_range)
10596 output_discr_value (&list->dw_discr_upper_bound, name);
10598 break;
10601 default:
10602 gcc_unreachable ();
10606 FOR_EACH_CHILD (die, c, output_die (c));
10608 /* Add null byte to terminate sibling list. */
10609 if (die->die_child != NULL)
10610 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10611 (unsigned long) die->die_offset);
10614 /* Output the compilation unit that appears at the beginning of the
10615 .debug_info section, and precedes the DIE descriptions. */
10617 static void
10618 output_compilation_unit_header (enum dwarf_unit_type ut)
10620 if (!XCOFF_DEBUGGING_INFO)
10622 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10623 dw2_asm_output_data (4, 0xffffffff,
10624 "Initial length escape value indicating 64-bit DWARF extension");
10625 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10626 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10627 "Length of Compilation Unit Info");
10630 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10631 if (dwarf_version >= 5)
10633 const char *name;
10634 switch (ut)
10636 case DW_UT_compile: name = "DW_UT_compile"; break;
10637 case DW_UT_type: name = "DW_UT_type"; break;
10638 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10639 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10640 default: gcc_unreachable ();
10642 dw2_asm_output_data (1, ut, "%s", name);
10643 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10645 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10646 debug_abbrev_section,
10647 "Offset Into Abbrev. Section");
10648 if (dwarf_version < 5)
10649 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10652 /* Output the compilation unit DIE and its children. */
10654 static void
10655 output_comp_unit (dw_die_ref die, int output_if_empty,
10656 const unsigned char *dwo_id)
10658 const char *secname, *oldsym;
10659 char *tmp;
10661 /* Unless we are outputting main CU, we may throw away empty ones. */
10662 if (!output_if_empty && die->die_child == NULL)
10663 return;
10665 /* Even if there are no children of this DIE, we must output the information
10666 about the compilation unit. Otherwise, on an empty translation unit, we
10667 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10668 will then complain when examining the file. First mark all the DIEs in
10669 this CU so we know which get local refs. */
10670 mark_dies (die);
10672 external_ref_hash_type *extern_map = optimize_external_refs (die);
10674 /* For now, optimize only the main CU, in order to optimize the rest
10675 we'd need to see all of them earlier. Leave the rest for post-linking
10676 tools like DWZ. */
10677 if (die == comp_unit_die ())
10678 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10680 build_abbrev_table (die, extern_map);
10682 optimize_abbrev_table ();
10684 delete extern_map;
10686 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10687 next_die_offset = (dwo_id
10688 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10689 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10690 calc_die_sizes (die);
10692 oldsym = die->die_id.die_symbol;
10693 if (oldsym && die->comdat_type_p)
10695 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10697 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10698 secname = tmp;
10699 die->die_id.die_symbol = NULL;
10700 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10702 else
10704 switch_to_section (debug_info_section);
10705 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10706 info_section_emitted = true;
10709 /* For LTO cross unit DIE refs we want a symbol on the start of the
10710 debuginfo section, not on the CU DIE. */
10711 if (flag_generate_lto && oldsym)
10713 /* ??? No way to get visibility assembled without a decl. */
10714 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10715 get_identifier (oldsym), char_type_node);
10716 TREE_PUBLIC (decl) = true;
10717 TREE_STATIC (decl) = true;
10718 DECL_ARTIFICIAL (decl) = true;
10719 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10720 DECL_VISIBILITY_SPECIFIED (decl) = true;
10721 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10722 #ifdef ASM_WEAKEN_LABEL
10723 /* We prefer a .weak because that handles duplicates from duplicate
10724 archive members in a graceful way. */
10725 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10726 #else
10727 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10728 #endif
10729 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10732 /* Output debugging information. */
10733 output_compilation_unit_header (dwo_id
10734 ? DW_UT_split_compile : DW_UT_compile);
10735 if (dwarf_version >= 5)
10737 if (dwo_id != NULL)
10738 for (int i = 0; i < 8; i++)
10739 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10741 output_die (die);
10743 /* Leave the marks on the main CU, so we can check them in
10744 output_pubnames. */
10745 if (oldsym)
10747 unmark_dies (die);
10748 die->die_id.die_symbol = oldsym;
10752 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10753 and .debug_pubtypes. This is configured per-target, but can be
10754 overridden by the -gpubnames or -gno-pubnames options. */
10756 static inline bool
10757 want_pubnames (void)
10759 if (debug_info_level <= DINFO_LEVEL_TERSE)
10760 return false;
10761 if (debug_generate_pub_sections != -1)
10762 return debug_generate_pub_sections;
10763 return targetm.want_debug_pub_sections;
10766 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10768 static void
10769 add_AT_pubnames (dw_die_ref die)
10771 if (want_pubnames ())
10772 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10775 /* Add a string attribute value to a skeleton DIE. */
10777 static inline void
10778 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10779 const char *str)
10781 dw_attr_node attr;
10782 struct indirect_string_node *node;
10784 if (! skeleton_debug_str_hash)
10785 skeleton_debug_str_hash
10786 = hash_table<indirect_string_hasher>::create_ggc (10);
10788 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10789 find_string_form (node);
10790 if (node->form == DW_FORM_GNU_str_index)
10791 node->form = DW_FORM_strp;
10793 attr.dw_attr = attr_kind;
10794 attr.dw_attr_val.val_class = dw_val_class_str;
10795 attr.dw_attr_val.val_entry = NULL;
10796 attr.dw_attr_val.v.val_str = node;
10797 add_dwarf_attr (die, &attr);
10800 /* Helper function to generate top-level dies for skeleton debug_info and
10801 debug_types. */
10803 static void
10804 add_top_level_skeleton_die_attrs (dw_die_ref die)
10806 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10807 const char *comp_dir = comp_dir_string ();
10809 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10810 if (comp_dir != NULL)
10811 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10812 add_AT_pubnames (die);
10813 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10816 /* Output skeleton debug sections that point to the dwo file. */
10818 static void
10819 output_skeleton_debug_sections (dw_die_ref comp_unit,
10820 const unsigned char *dwo_id)
10822 /* These attributes will be found in the full debug_info section. */
10823 remove_AT (comp_unit, DW_AT_producer);
10824 remove_AT (comp_unit, DW_AT_language);
10826 switch_to_section (debug_skeleton_info_section);
10827 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10829 /* Produce the skeleton compilation-unit header. This one differs enough from
10830 a normal CU header that it's better not to call output_compilation_unit
10831 header. */
10832 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10833 dw2_asm_output_data (4, 0xffffffff,
10834 "Initial length escape value indicating 64-bit "
10835 "DWARF extension");
10837 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10838 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10839 - DWARF_INITIAL_LENGTH_SIZE
10840 + size_of_die (comp_unit),
10841 "Length of Compilation Unit Info");
10842 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10843 if (dwarf_version >= 5)
10845 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10846 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10848 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10849 debug_skeleton_abbrev_section,
10850 "Offset Into Abbrev. Section");
10851 if (dwarf_version < 5)
10852 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10853 else
10854 for (int i = 0; i < 8; i++)
10855 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10857 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10858 output_die (comp_unit);
10860 /* Build the skeleton debug_abbrev section. */
10861 switch_to_section (debug_skeleton_abbrev_section);
10862 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10864 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10866 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10869 /* Output a comdat type unit DIE and its children. */
10871 static void
10872 output_comdat_type_unit (comdat_type_node *node)
10874 const char *secname;
10875 char *tmp;
10876 int i;
10877 #if defined (OBJECT_FORMAT_ELF)
10878 tree comdat_key;
10879 #endif
10881 /* First mark all the DIEs in this CU so we know which get local refs. */
10882 mark_dies (node->root_die);
10884 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10886 build_abbrev_table (node->root_die, extern_map);
10888 delete extern_map;
10889 extern_map = NULL;
10891 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10892 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10893 calc_die_sizes (node->root_die);
10895 #if defined (OBJECT_FORMAT_ELF)
10896 if (dwarf_version >= 5)
10898 if (!dwarf_split_debug_info)
10899 secname = ".debug_info";
10900 else
10901 secname = ".debug_info.dwo";
10903 else if (!dwarf_split_debug_info)
10904 secname = ".debug_types";
10905 else
10906 secname = ".debug_types.dwo";
10908 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10909 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10910 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10911 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10912 comdat_key = get_identifier (tmp);
10913 targetm.asm_out.named_section (secname,
10914 SECTION_DEBUG | SECTION_LINKONCE,
10915 comdat_key);
10916 #else
10917 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10918 sprintf (tmp, (dwarf_version >= 5
10919 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10920 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10921 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10922 secname = tmp;
10923 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10924 #endif
10926 /* Output debugging information. */
10927 output_compilation_unit_header (dwarf_split_debug_info
10928 ? DW_UT_split_type : DW_UT_type);
10929 output_signature (node->signature, "Type Signature");
10930 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10931 "Offset to Type DIE");
10932 output_die (node->root_die);
10934 unmark_dies (node->root_die);
10937 /* Return the DWARF2/3 pubname associated with a decl. */
10939 static const char *
10940 dwarf2_name (tree decl, int scope)
10942 if (DECL_NAMELESS (decl))
10943 return NULL;
10944 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10947 /* Add a new entry to .debug_pubnames if appropriate. */
10949 static void
10950 add_pubname_string (const char *str, dw_die_ref die)
10952 pubname_entry e;
10954 e.die = die;
10955 e.name = xstrdup (str);
10956 vec_safe_push (pubname_table, e);
10959 static void
10960 add_pubname (tree decl, dw_die_ref die)
10962 if (!want_pubnames ())
10963 return;
10965 /* Don't add items to the table when we expect that the consumer will have
10966 just read the enclosing die. For example, if the consumer is looking at a
10967 class_member, it will either be inside the class already, or will have just
10968 looked up the class to find the member. Either way, searching the class is
10969 faster than searching the index. */
10970 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10971 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10973 const char *name = dwarf2_name (decl, 1);
10975 if (name)
10976 add_pubname_string (name, die);
10980 /* Add an enumerator to the pubnames section. */
10982 static void
10983 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10985 pubname_entry e;
10987 gcc_assert (scope_name);
10988 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10989 e.die = die;
10990 vec_safe_push (pubname_table, e);
10993 /* Add a new entry to .debug_pubtypes if appropriate. */
10995 static void
10996 add_pubtype (tree decl, dw_die_ref die)
10998 pubname_entry e;
11000 if (!want_pubnames ())
11001 return;
11003 if ((TREE_PUBLIC (decl)
11004 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11005 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11007 tree scope = NULL;
11008 const char *scope_name = "";
11009 const char *sep = is_cxx () ? "::" : ".";
11010 const char *name;
11012 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11013 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11015 scope_name = lang_hooks.dwarf_name (scope, 1);
11016 if (scope_name != NULL && scope_name[0] != '\0')
11017 scope_name = concat (scope_name, sep, NULL);
11018 else
11019 scope_name = "";
11022 if (TYPE_P (decl))
11023 name = type_tag (decl);
11024 else
11025 name = lang_hooks.dwarf_name (decl, 1);
11027 /* If we don't have a name for the type, there's no point in adding
11028 it to the table. */
11029 if (name != NULL && name[0] != '\0')
11031 e.die = die;
11032 e.name = concat (scope_name, name, NULL);
11033 vec_safe_push (pubtype_table, e);
11036 /* Although it might be more consistent to add the pubinfo for the
11037 enumerators as their dies are created, they should only be added if the
11038 enum type meets the criteria above. So rather than re-check the parent
11039 enum type whenever an enumerator die is created, just output them all
11040 here. This isn't protected by the name conditional because anonymous
11041 enums don't have names. */
11042 if (die->die_tag == DW_TAG_enumeration_type)
11044 dw_die_ref c;
11046 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11051 /* Output a single entry in the pubnames table. */
11053 static void
11054 output_pubname (dw_offset die_offset, pubname_entry *entry)
11056 dw_die_ref die = entry->die;
11057 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11059 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11061 if (debug_generate_pub_sections == 2)
11063 /* This logic follows gdb's method for determining the value of the flag
11064 byte. */
11065 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11066 switch (die->die_tag)
11068 case DW_TAG_typedef:
11069 case DW_TAG_base_type:
11070 case DW_TAG_subrange_type:
11071 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11072 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11073 break;
11074 case DW_TAG_enumerator:
11075 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11076 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11077 if (!is_cxx ())
11078 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11079 break;
11080 case DW_TAG_subprogram:
11081 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11082 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11083 if (!is_ada ())
11084 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11085 break;
11086 case DW_TAG_constant:
11087 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11088 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11089 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11090 break;
11091 case DW_TAG_variable:
11092 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11093 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11094 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11095 break;
11096 case DW_TAG_namespace:
11097 case DW_TAG_imported_declaration:
11098 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11099 break;
11100 case DW_TAG_class_type:
11101 case DW_TAG_interface_type:
11102 case DW_TAG_structure_type:
11103 case DW_TAG_union_type:
11104 case DW_TAG_enumeration_type:
11105 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11106 if (!is_cxx ())
11107 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11108 break;
11109 default:
11110 /* An unusual tag. Leave the flag-byte empty. */
11111 break;
11113 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11114 "GDB-index flags");
11117 dw2_asm_output_nstring (entry->name, -1, "external name");
11121 /* Output the public names table used to speed up access to externally
11122 visible names; or the public types table used to find type definitions. */
11124 static void
11125 output_pubnames (vec<pubname_entry, va_gc> *names)
11127 unsigned i;
11128 unsigned long pubnames_length = size_of_pubnames (names);
11129 pubname_entry *pub;
11131 if (!XCOFF_DEBUGGING_INFO)
11133 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11134 dw2_asm_output_data (4, 0xffffffff,
11135 "Initial length escape value indicating 64-bit DWARF extension");
11136 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11137 "Pub Info Length");
11140 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11141 dw2_asm_output_data (2, 2, "DWARF Version");
11143 if (dwarf_split_debug_info)
11144 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11145 debug_skeleton_info_section,
11146 "Offset of Compilation Unit Info");
11147 else
11148 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11149 debug_info_section,
11150 "Offset of Compilation Unit Info");
11151 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11152 "Compilation Unit Length");
11154 FOR_EACH_VEC_ELT (*names, i, pub)
11156 if (include_pubname_in_output (names, pub))
11158 dw_offset die_offset = pub->die->die_offset;
11160 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11161 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11162 gcc_assert (pub->die->die_mark);
11164 /* If we're putting types in their own .debug_types sections,
11165 the .debug_pubtypes table will still point to the compile
11166 unit (not the type unit), so we want to use the offset of
11167 the skeleton DIE (if there is one). */
11168 if (pub->die->comdat_type_p && names == pubtype_table)
11170 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11172 if (type_node != NULL)
11173 die_offset = (type_node->skeleton_die != NULL
11174 ? type_node->skeleton_die->die_offset
11175 : comp_unit_die ()->die_offset);
11178 output_pubname (die_offset, pub);
11182 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11185 /* Output public names and types tables if necessary. */
11187 static void
11188 output_pubtables (void)
11190 if (!want_pubnames () || !info_section_emitted)
11191 return;
11193 switch_to_section (debug_pubnames_section);
11194 output_pubnames (pubname_table);
11195 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11196 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11197 simply won't look for the section. */
11198 switch_to_section (debug_pubtypes_section);
11199 output_pubnames (pubtype_table);
11203 /* Output the information that goes into the .debug_aranges table.
11204 Namely, define the beginning and ending address range of the
11205 text section generated for this compilation unit. */
11207 static void
11208 output_aranges (void)
11210 unsigned i;
11211 unsigned long aranges_length = size_of_aranges ();
11213 if (!XCOFF_DEBUGGING_INFO)
11215 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11216 dw2_asm_output_data (4, 0xffffffff,
11217 "Initial length escape value indicating 64-bit DWARF extension");
11218 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11219 "Length of Address Ranges Info");
11222 /* Version number for aranges is still 2, even up to DWARF5. */
11223 dw2_asm_output_data (2, 2, "DWARF Version");
11224 if (dwarf_split_debug_info)
11225 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11226 debug_skeleton_info_section,
11227 "Offset of Compilation Unit Info");
11228 else
11229 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11230 debug_info_section,
11231 "Offset of Compilation Unit Info");
11232 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11233 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11235 /* We need to align to twice the pointer size here. */
11236 if (DWARF_ARANGES_PAD_SIZE)
11238 /* Pad using a 2 byte words so that padding is correct for any
11239 pointer size. */
11240 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11241 2 * DWARF2_ADDR_SIZE);
11242 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11243 dw2_asm_output_data (2, 0, NULL);
11246 /* It is necessary not to output these entries if the sections were
11247 not used; if the sections were not used, the length will be 0 and
11248 the address may end up as 0 if the section is discarded by ld
11249 --gc-sections, leaving an invalid (0, 0) entry that can be
11250 confused with the terminator. */
11251 if (text_section_used)
11253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11254 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11255 text_section_label, "Length");
11257 if (cold_text_section_used)
11259 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11260 "Address");
11261 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11262 cold_text_section_label, "Length");
11265 if (have_multiple_function_sections)
11267 unsigned fde_idx;
11268 dw_fde_ref fde;
11270 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11272 if (DECL_IGNORED_P (fde->decl))
11273 continue;
11274 if (!fde->in_std_section)
11276 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11277 "Address");
11278 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11279 fde->dw_fde_begin, "Length");
11281 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11284 "Address");
11285 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11286 fde->dw_fde_second_begin, "Length");
11291 /* Output the terminator words. */
11292 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11293 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11296 /* Add a new entry to .debug_ranges. Return its index into
11297 ranges_table vector. */
11299 static unsigned int
11300 add_ranges_num (int num, bool maybe_new_sec)
11302 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11303 vec_safe_push (ranges_table, r);
11304 return vec_safe_length (ranges_table) - 1;
11307 /* Add a new entry to .debug_ranges corresponding to a block, or a
11308 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11309 this entry might be in a different section from previous range. */
11311 static unsigned int
11312 add_ranges (const_tree block, bool maybe_new_sec)
11314 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11317 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11318 chain, or middle entry of a chain that will be directly referred to. */
11320 static void
11321 note_rnglist_head (unsigned int offset)
11323 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11324 return;
11325 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11328 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11329 When using dwarf_split_debug_info, address attributes in dies destined
11330 for the final executable should be direct references--setting the
11331 parameter force_direct ensures this behavior. */
11333 static void
11334 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11335 bool *added, bool force_direct)
11337 unsigned int in_use = vec_safe_length (ranges_by_label);
11338 unsigned int offset;
11339 dw_ranges_by_label rbl = { begin, end };
11340 vec_safe_push (ranges_by_label, rbl);
11341 offset = add_ranges_num (-(int)in_use - 1, true);
11342 if (!*added)
11344 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11345 *added = true;
11346 note_rnglist_head (offset);
11350 /* Emit .debug_ranges section. */
11352 static void
11353 output_ranges (void)
11355 unsigned i;
11356 static const char *const start_fmt = "Offset %#x";
11357 const char *fmt = start_fmt;
11358 dw_ranges *r;
11360 switch_to_section (debug_ranges_section);
11361 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11362 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11364 int block_num = r->num;
11366 if (block_num > 0)
11368 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11369 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11371 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11372 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11374 /* If all code is in the text section, then the compilation
11375 unit base address defaults to DW_AT_low_pc, which is the
11376 base of the text section. */
11377 if (!have_multiple_function_sections)
11379 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11380 text_section_label,
11381 fmt, i * 2 * DWARF2_ADDR_SIZE);
11382 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11383 text_section_label, NULL);
11386 /* Otherwise, the compilation unit base address is zero,
11387 which allows us to use absolute addresses, and not worry
11388 about whether the target supports cross-section
11389 arithmetic. */
11390 else
11392 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11393 fmt, i * 2 * DWARF2_ADDR_SIZE);
11394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11397 fmt = NULL;
11400 /* Negative block_num stands for an index into ranges_by_label. */
11401 else if (block_num < 0)
11403 int lab_idx = - block_num - 1;
11405 if (!have_multiple_function_sections)
11407 gcc_unreachable ();
11408 #if 0
11409 /* If we ever use add_ranges_by_labels () for a single
11410 function section, all we have to do is to take out
11411 the #if 0 above. */
11412 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11413 (*ranges_by_label)[lab_idx].begin,
11414 text_section_label,
11415 fmt, i * 2 * DWARF2_ADDR_SIZE);
11416 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11417 (*ranges_by_label)[lab_idx].end,
11418 text_section_label, NULL);
11419 #endif
11421 else
11423 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11424 (*ranges_by_label)[lab_idx].begin,
11425 fmt, i * 2 * DWARF2_ADDR_SIZE);
11426 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11427 (*ranges_by_label)[lab_idx].end,
11428 NULL);
11431 else
11433 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11434 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11435 fmt = start_fmt;
11440 /* Non-zero if .debug_line_str should be used for .debug_line section
11441 strings or strings that are likely shareable with those. */
11442 #define DWARF5_USE_DEBUG_LINE_STR \
11443 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11444 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11445 /* FIXME: there is no .debug_line_str.dwo section, \
11446 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11447 && !dwarf_split_debug_info)
11449 /* Assign .debug_rnglists indexes. */
11451 static void
11452 index_rnglists (void)
11454 unsigned i;
11455 dw_ranges *r;
11457 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11458 if (r->label)
11459 r->idx = rnglist_idx++;
11462 /* Emit .debug_rnglists section. */
11464 static void
11465 output_rnglists (void)
11467 unsigned i;
11468 dw_ranges *r;
11469 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11470 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11471 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11473 switch_to_section (debug_ranges_section);
11474 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11475 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11476 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11477 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11478 dw2_asm_output_data (4, 0xffffffff,
11479 "Initial length escape value indicating "
11480 "64-bit DWARF extension");
11481 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11482 "Length of Range Lists");
11483 ASM_OUTPUT_LABEL (asm_out_file, l1);
11484 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11485 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11486 dw2_asm_output_data (1, 0, "Segment Size");
11487 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11488 about relocation sizes and primarily care about the size of .debug*
11489 sections in linked shared libraries and executables, then
11490 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11491 into it are usually larger than just DW_FORM_sec_offset offsets
11492 into the .debug_rnglists section. */
11493 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11494 "Offset Entry Count");
11495 if (dwarf_split_debug_info)
11497 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11498 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11499 if (r->label)
11500 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11501 ranges_base_label, NULL);
11504 const char *lab = "";
11505 unsigned int len = vec_safe_length (ranges_table);
11506 const char *base = NULL;
11507 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11509 int block_num = r->num;
11511 if (r->label)
11513 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11514 lab = r->label;
11516 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11517 base = NULL;
11518 if (block_num > 0)
11520 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11521 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11523 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11524 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11526 if (HAVE_AS_LEB128)
11528 /* If all code is in the text section, then the compilation
11529 unit base address defaults to DW_AT_low_pc, which is the
11530 base of the text section. */
11531 if (!have_multiple_function_sections)
11533 dw2_asm_output_data (1, DW_RLE_offset_pair,
11534 "DW_RLE_offset_pair (%s)", lab);
11535 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11536 "Range begin address (%s)", lab);
11537 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11538 "Range end address (%s)", lab);
11539 continue;
11541 if (base == NULL)
11543 dw_ranges *r2 = NULL;
11544 if (i < len - 1)
11545 r2 = &(*ranges_table)[i + 1];
11546 if (r2
11547 && r2->num != 0
11548 && r2->label == NULL
11549 && !r2->maybe_new_sec)
11551 dw2_asm_output_data (1, DW_RLE_base_address,
11552 "DW_RLE_base_address (%s)", lab);
11553 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11554 "Base address (%s)", lab);
11555 strcpy (basebuf, blabel);
11556 base = basebuf;
11559 if (base)
11561 dw2_asm_output_data (1, DW_RLE_offset_pair,
11562 "DW_RLE_offset_pair (%s)", lab);
11563 dw2_asm_output_delta_uleb128 (blabel, base,
11564 "Range begin address (%s)", lab);
11565 dw2_asm_output_delta_uleb128 (elabel, base,
11566 "Range end address (%s)", lab);
11567 continue;
11569 dw2_asm_output_data (1, DW_RLE_start_length,
11570 "DW_RLE_start_length (%s)", lab);
11571 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11572 "Range begin address (%s)", lab);
11573 dw2_asm_output_delta_uleb128 (elabel, blabel,
11574 "Range length (%s)", lab);
11576 else
11578 dw2_asm_output_data (1, DW_RLE_start_end,
11579 "DW_RLE_start_end (%s)", lab);
11580 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11581 "Range begin address (%s)", lab);
11582 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11583 "Range end address (%s)", lab);
11587 /* Negative block_num stands for an index into ranges_by_label. */
11588 else if (block_num < 0)
11590 int lab_idx = - block_num - 1;
11591 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11592 const char *elabel = (*ranges_by_label)[lab_idx].end;
11594 if (!have_multiple_function_sections)
11595 gcc_unreachable ();
11596 if (HAVE_AS_LEB128)
11598 dw2_asm_output_data (1, DW_RLE_start_length,
11599 "DW_RLE_start_length (%s)", lab);
11600 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11601 "Range begin address (%s)", lab);
11602 dw2_asm_output_delta_uleb128 (elabel, blabel,
11603 "Range length (%s)", lab);
11605 else
11607 dw2_asm_output_data (1, DW_RLE_start_end,
11608 "DW_RLE_start_end (%s)", lab);
11609 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11610 "Range begin address (%s)", lab);
11611 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11612 "Range end address (%s)", lab);
11615 else
11616 dw2_asm_output_data (1, DW_RLE_end_of_list,
11617 "DW_RLE_end_of_list (%s)", lab);
11619 ASM_OUTPUT_LABEL (asm_out_file, l2);
11622 /* Data structure containing information about input files. */
11623 struct file_info
11625 const char *path; /* Complete file name. */
11626 const char *fname; /* File name part. */
11627 int length; /* Length of entire string. */
11628 struct dwarf_file_data * file_idx; /* Index in input file table. */
11629 int dir_idx; /* Index in directory table. */
11632 /* Data structure containing information about directories with source
11633 files. */
11634 struct dir_info
11636 const char *path; /* Path including directory name. */
11637 int length; /* Path length. */
11638 int prefix; /* Index of directory entry which is a prefix. */
11639 int count; /* Number of files in this directory. */
11640 int dir_idx; /* Index of directory used as base. */
11643 /* Callback function for file_info comparison. We sort by looking at
11644 the directories in the path. */
11646 static int
11647 file_info_cmp (const void *p1, const void *p2)
11649 const struct file_info *const s1 = (const struct file_info *) p1;
11650 const struct file_info *const s2 = (const struct file_info *) p2;
11651 const unsigned char *cp1;
11652 const unsigned char *cp2;
11654 /* Take care of file names without directories. We need to make sure that
11655 we return consistent values to qsort since some will get confused if
11656 we return the same value when identical operands are passed in opposite
11657 orders. So if neither has a directory, return 0 and otherwise return
11658 1 or -1 depending on which one has the directory. */
11659 if ((s1->path == s1->fname || s2->path == s2->fname))
11660 return (s2->path == s2->fname) - (s1->path == s1->fname);
11662 cp1 = (const unsigned char *) s1->path;
11663 cp2 = (const unsigned char *) s2->path;
11665 while (1)
11667 ++cp1;
11668 ++cp2;
11669 /* Reached the end of the first path? If so, handle like above. */
11670 if ((cp1 == (const unsigned char *) s1->fname)
11671 || (cp2 == (const unsigned char *) s2->fname))
11672 return ((cp2 == (const unsigned char *) s2->fname)
11673 - (cp1 == (const unsigned char *) s1->fname));
11675 /* Character of current path component the same? */
11676 else if (*cp1 != *cp2)
11677 return *cp1 - *cp2;
11681 struct file_name_acquire_data
11683 struct file_info *files;
11684 int used_files;
11685 int max_files;
11688 /* Traversal function for the hash table. */
11691 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11693 struct dwarf_file_data *d = *slot;
11694 struct file_info *fi;
11695 const char *f;
11697 gcc_assert (fnad->max_files >= d->emitted_number);
11699 if (! d->emitted_number)
11700 return 1;
11702 gcc_assert (fnad->max_files != fnad->used_files);
11704 fi = fnad->files + fnad->used_files++;
11706 /* Skip all leading "./". */
11707 f = d->filename;
11708 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11709 f += 2;
11711 /* Create a new array entry. */
11712 fi->path = f;
11713 fi->length = strlen (f);
11714 fi->file_idx = d;
11716 /* Search for the file name part. */
11717 f = strrchr (f, DIR_SEPARATOR);
11718 #if defined (DIR_SEPARATOR_2)
11720 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11722 if (g != NULL)
11724 if (f == NULL || f < g)
11725 f = g;
11728 #endif
11730 fi->fname = f == NULL ? fi->path : f + 1;
11731 return 1;
11734 /* Helper function for output_file_names. Emit a FORM encoded
11735 string STR, with assembly comment start ENTRY_KIND and
11736 index IDX */
11738 static void
11739 output_line_string (enum dwarf_form form, const char *str,
11740 const char *entry_kind, unsigned int idx)
11742 switch (form)
11744 case DW_FORM_string:
11745 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11746 break;
11747 case DW_FORM_line_strp:
11748 if (!debug_line_str_hash)
11749 debug_line_str_hash
11750 = hash_table<indirect_string_hasher>::create_ggc (10);
11752 struct indirect_string_node *node;
11753 node = find_AT_string_in_table (str, debug_line_str_hash);
11754 set_indirect_string (node);
11755 node->form = form;
11756 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11757 debug_line_str_section, "%s: %#x: \"%s\"",
11758 entry_kind, 0, node->str);
11759 break;
11760 default:
11761 gcc_unreachable ();
11765 /* Output the directory table and the file name table. We try to minimize
11766 the total amount of memory needed. A heuristic is used to avoid large
11767 slowdowns with many input files. */
11769 static void
11770 output_file_names (void)
11772 struct file_name_acquire_data fnad;
11773 int numfiles;
11774 struct file_info *files;
11775 struct dir_info *dirs;
11776 int *saved;
11777 int *savehere;
11778 int *backmap;
11779 int ndirs;
11780 int idx_offset;
11781 int i;
11783 if (!last_emitted_file)
11785 if (dwarf_version >= 5)
11787 dw2_asm_output_data (1, 0, "Directory entry format count");
11788 dw2_asm_output_data_uleb128 (0, "Directories count");
11789 dw2_asm_output_data (1, 0, "File name entry format count");
11790 dw2_asm_output_data_uleb128 (0, "File names count");
11792 else
11794 dw2_asm_output_data (1, 0, "End directory table");
11795 dw2_asm_output_data (1, 0, "End file name table");
11797 return;
11800 numfiles = last_emitted_file->emitted_number;
11802 /* Allocate the various arrays we need. */
11803 files = XALLOCAVEC (struct file_info, numfiles);
11804 dirs = XALLOCAVEC (struct dir_info, numfiles);
11806 fnad.files = files;
11807 fnad.used_files = 0;
11808 fnad.max_files = numfiles;
11809 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11810 gcc_assert (fnad.used_files == fnad.max_files);
11812 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11814 /* Find all the different directories used. */
11815 dirs[0].path = files[0].path;
11816 dirs[0].length = files[0].fname - files[0].path;
11817 dirs[0].prefix = -1;
11818 dirs[0].count = 1;
11819 dirs[0].dir_idx = 0;
11820 files[0].dir_idx = 0;
11821 ndirs = 1;
11823 for (i = 1; i < numfiles; i++)
11824 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11825 && memcmp (dirs[ndirs - 1].path, files[i].path,
11826 dirs[ndirs - 1].length) == 0)
11828 /* Same directory as last entry. */
11829 files[i].dir_idx = ndirs - 1;
11830 ++dirs[ndirs - 1].count;
11832 else
11834 int j;
11836 /* This is a new directory. */
11837 dirs[ndirs].path = files[i].path;
11838 dirs[ndirs].length = files[i].fname - files[i].path;
11839 dirs[ndirs].count = 1;
11840 dirs[ndirs].dir_idx = ndirs;
11841 files[i].dir_idx = ndirs;
11843 /* Search for a prefix. */
11844 dirs[ndirs].prefix = -1;
11845 for (j = 0; j < ndirs; j++)
11846 if (dirs[j].length < dirs[ndirs].length
11847 && dirs[j].length > 1
11848 && (dirs[ndirs].prefix == -1
11849 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11850 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11851 dirs[ndirs].prefix = j;
11853 ++ndirs;
11856 /* Now to the actual work. We have to find a subset of the directories which
11857 allow expressing the file name using references to the directory table
11858 with the least amount of characters. We do not do an exhaustive search
11859 where we would have to check out every combination of every single
11860 possible prefix. Instead we use a heuristic which provides nearly optimal
11861 results in most cases and never is much off. */
11862 saved = XALLOCAVEC (int, ndirs);
11863 savehere = XALLOCAVEC (int, ndirs);
11865 memset (saved, '\0', ndirs * sizeof (saved[0]));
11866 for (i = 0; i < ndirs; i++)
11868 int j;
11869 int total;
11871 /* We can always save some space for the current directory. But this
11872 does not mean it will be enough to justify adding the directory. */
11873 savehere[i] = dirs[i].length;
11874 total = (savehere[i] - saved[i]) * dirs[i].count;
11876 for (j = i + 1; j < ndirs; j++)
11878 savehere[j] = 0;
11879 if (saved[j] < dirs[i].length)
11881 /* Determine whether the dirs[i] path is a prefix of the
11882 dirs[j] path. */
11883 int k;
11885 k = dirs[j].prefix;
11886 while (k != -1 && k != (int) i)
11887 k = dirs[k].prefix;
11889 if (k == (int) i)
11891 /* Yes it is. We can possibly save some memory by
11892 writing the filenames in dirs[j] relative to
11893 dirs[i]. */
11894 savehere[j] = dirs[i].length;
11895 total += (savehere[j] - saved[j]) * dirs[j].count;
11900 /* Check whether we can save enough to justify adding the dirs[i]
11901 directory. */
11902 if (total > dirs[i].length + 1)
11904 /* It's worthwhile adding. */
11905 for (j = i; j < ndirs; j++)
11906 if (savehere[j] > 0)
11908 /* Remember how much we saved for this directory so far. */
11909 saved[j] = savehere[j];
11911 /* Remember the prefix directory. */
11912 dirs[j].dir_idx = i;
11917 /* Emit the directory name table. */
11918 idx_offset = dirs[0].length > 0 ? 1 : 0;
11919 enum dwarf_form str_form = DW_FORM_string;
11920 enum dwarf_form idx_form = DW_FORM_udata;
11921 if (dwarf_version >= 5)
11923 const char *comp_dir = comp_dir_string ();
11924 if (comp_dir == NULL)
11925 comp_dir = "";
11926 dw2_asm_output_data (1, 1, "Directory entry format count");
11927 if (DWARF5_USE_DEBUG_LINE_STR)
11928 str_form = DW_FORM_line_strp;
11929 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11930 dw2_asm_output_data_uleb128 (str_form, "%s",
11931 get_DW_FORM_name (str_form));
11932 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11933 if (str_form == DW_FORM_string)
11935 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11936 for (i = 1 - idx_offset; i < ndirs; i++)
11937 dw2_asm_output_nstring (dirs[i].path,
11938 dirs[i].length
11939 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11940 "Directory Entry: %#x", i + idx_offset);
11942 else
11944 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11945 for (i = 1 - idx_offset; i < ndirs; i++)
11947 const char *str
11948 = ggc_alloc_string (dirs[i].path,
11949 dirs[i].length
11950 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11951 output_line_string (str_form, str, "Directory Entry",
11952 (unsigned) i + idx_offset);
11956 else
11958 for (i = 1 - idx_offset; i < ndirs; i++)
11959 dw2_asm_output_nstring (dirs[i].path,
11960 dirs[i].length
11961 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11962 "Directory Entry: %#x", i + idx_offset);
11964 dw2_asm_output_data (1, 0, "End directory table");
11967 /* We have to emit them in the order of emitted_number since that's
11968 used in the debug info generation. To do this efficiently we
11969 generate a back-mapping of the indices first. */
11970 backmap = XALLOCAVEC (int, numfiles);
11971 for (i = 0; i < numfiles; i++)
11972 backmap[files[i].file_idx->emitted_number - 1] = i;
11974 if (dwarf_version >= 5)
11976 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11977 if (filename0 == NULL)
11978 filename0 = "";
11979 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11980 DW_FORM_data2. Choose one based on the number of directories
11981 and how much space would they occupy in each encoding.
11982 If we have at most 256 directories, all indexes fit into
11983 a single byte, so DW_FORM_data1 is most compact (if there
11984 are at most 128 directories, DW_FORM_udata would be as
11985 compact as that, but not shorter and slower to decode). */
11986 if (ndirs + idx_offset <= 256)
11987 idx_form = DW_FORM_data1;
11988 /* If there are more than 65536 directories, we have to use
11989 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11990 Otherwise, compute what space would occupy if all the indexes
11991 used DW_FORM_udata - sum - and compare that to how large would
11992 be DW_FORM_data2 encoding, and pick the more efficient one. */
11993 else if (ndirs + idx_offset <= 65536)
11995 unsigned HOST_WIDE_INT sum = 1;
11996 for (i = 0; i < numfiles; i++)
11998 int file_idx = backmap[i];
11999 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12000 sum += size_of_uleb128 (dir_idx);
12002 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12003 idx_form = DW_FORM_data2;
12005 #ifdef VMS_DEBUGGING_INFO
12006 dw2_asm_output_data (1, 4, "File name entry format count");
12007 #else
12008 dw2_asm_output_data (1, 2, "File name entry format count");
12009 #endif
12010 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12011 dw2_asm_output_data_uleb128 (str_form, "%s",
12012 get_DW_FORM_name (str_form));
12013 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12014 "DW_LNCT_directory_index");
12015 dw2_asm_output_data_uleb128 (idx_form, "%s",
12016 get_DW_FORM_name (idx_form));
12017 #ifdef VMS_DEBUGGING_INFO
12018 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12019 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12020 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12021 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12022 #endif
12023 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12025 output_line_string (str_form, filename0, "File Entry", 0);
12027 /* Include directory index. */
12028 if (idx_form != DW_FORM_udata)
12029 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12030 0, NULL);
12031 else
12032 dw2_asm_output_data_uleb128 (0, NULL);
12034 #ifdef VMS_DEBUGGING_INFO
12035 dw2_asm_output_data_uleb128 (0, NULL);
12036 dw2_asm_output_data_uleb128 (0, NULL);
12037 #endif
12040 /* Now write all the file names. */
12041 for (i = 0; i < numfiles; i++)
12043 int file_idx = backmap[i];
12044 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12046 #ifdef VMS_DEBUGGING_INFO
12047 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12049 /* Setting these fields can lead to debugger miscomparisons,
12050 but VMS Debug requires them to be set correctly. */
12052 int ver;
12053 long long cdt;
12054 long siz;
12055 int maxfilelen = (strlen (files[file_idx].path)
12056 + dirs[dir_idx].length
12057 + MAX_VMS_VERSION_LEN + 1);
12058 char *filebuf = XALLOCAVEC (char, maxfilelen);
12060 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12061 snprintf (filebuf, maxfilelen, "%s;%d",
12062 files[file_idx].path + dirs[dir_idx].length, ver);
12064 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12066 /* Include directory index. */
12067 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12068 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12069 dir_idx + idx_offset, NULL);
12070 else
12071 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12073 /* Modification time. */
12074 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12075 &cdt, 0, 0, 0) == 0)
12076 ? cdt : 0, NULL);
12078 /* File length in bytes. */
12079 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12080 0, &siz, 0, 0) == 0)
12081 ? siz : 0, NULL);
12082 #else
12083 output_line_string (str_form,
12084 files[file_idx].path + dirs[dir_idx].length,
12085 "File Entry", (unsigned) i + 1);
12087 /* Include directory index. */
12088 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12089 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12090 dir_idx + idx_offset, NULL);
12091 else
12092 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12094 if (dwarf_version >= 5)
12095 continue;
12097 /* Modification time. */
12098 dw2_asm_output_data_uleb128 (0, NULL);
12100 /* File length in bytes. */
12101 dw2_asm_output_data_uleb128 (0, NULL);
12102 #endif /* VMS_DEBUGGING_INFO */
12105 if (dwarf_version < 5)
12106 dw2_asm_output_data (1, 0, "End file name table");
12110 /* Output one line number table into the .debug_line section. */
12112 static void
12113 output_one_line_info_table (dw_line_info_table *table)
12115 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12116 unsigned int current_line = 1;
12117 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12118 dw_line_info_entry *ent;
12119 size_t i;
12121 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12123 switch (ent->opcode)
12125 case LI_set_address:
12126 /* ??? Unfortunately, we have little choice here currently, and
12127 must always use the most general form. GCC does not know the
12128 address delta itself, so we can't use DW_LNS_advance_pc. Many
12129 ports do have length attributes which will give an upper bound
12130 on the address range. We could perhaps use length attributes
12131 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12132 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12134 /* This can handle any delta. This takes
12135 4+DWARF2_ADDR_SIZE bytes. */
12136 dw2_asm_output_data (1, 0, "set address %s", line_label);
12137 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12138 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12139 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12140 break;
12142 case LI_set_line:
12143 if (ent->val == current_line)
12145 /* We still need to start a new row, so output a copy insn. */
12146 dw2_asm_output_data (1, DW_LNS_copy,
12147 "copy line %u", current_line);
12149 else
12151 int line_offset = ent->val - current_line;
12152 int line_delta = line_offset - DWARF_LINE_BASE;
12154 current_line = ent->val;
12155 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12157 /* This can handle deltas from -10 to 234, using the current
12158 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12159 This takes 1 byte. */
12160 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12161 "line %u", current_line);
12163 else
12165 /* This can handle any delta. This takes at least 4 bytes,
12166 depending on the value being encoded. */
12167 dw2_asm_output_data (1, DW_LNS_advance_line,
12168 "advance to line %u", current_line);
12169 dw2_asm_output_data_sleb128 (line_offset, NULL);
12170 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12173 break;
12175 case LI_set_file:
12176 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12177 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12178 break;
12180 case LI_set_column:
12181 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12182 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12183 break;
12185 case LI_negate_stmt:
12186 current_is_stmt = !current_is_stmt;
12187 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12188 "is_stmt %d", current_is_stmt);
12189 break;
12191 case LI_set_prologue_end:
12192 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12193 "set prologue end");
12194 break;
12196 case LI_set_epilogue_begin:
12197 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12198 "set epilogue begin");
12199 break;
12201 case LI_set_discriminator:
12202 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12203 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12204 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12205 dw2_asm_output_data_uleb128 (ent->val, NULL);
12206 break;
12210 /* Emit debug info for the address of the end of the table. */
12211 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12212 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12213 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12214 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12216 dw2_asm_output_data (1, 0, "end sequence");
12217 dw2_asm_output_data_uleb128 (1, NULL);
12218 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12221 /* Output the source line number correspondence information. This
12222 information goes into the .debug_line section. */
12224 static void
12225 output_line_info (bool prologue_only)
12227 static unsigned int generation;
12228 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12229 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12230 bool saw_one = false;
12231 int opc;
12233 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12234 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12235 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12236 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12238 if (!XCOFF_DEBUGGING_INFO)
12240 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12241 dw2_asm_output_data (4, 0xffffffff,
12242 "Initial length escape value indicating 64-bit DWARF extension");
12243 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12244 "Length of Source Line Info");
12247 ASM_OUTPUT_LABEL (asm_out_file, l1);
12249 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
12250 if (dwarf_version >= 5)
12252 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12253 dw2_asm_output_data (1, 0, "Segment Size");
12255 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12256 ASM_OUTPUT_LABEL (asm_out_file, p1);
12258 /* Define the architecture-dependent minimum instruction length (in bytes).
12259 In this implementation of DWARF, this field is used for information
12260 purposes only. Since GCC generates assembly language, we have no
12261 a priori knowledge of how many instruction bytes are generated for each
12262 source line, and therefore can use only the DW_LNE_set_address and
12263 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12264 this as '1', which is "correct enough" for all architectures,
12265 and don't let the target override. */
12266 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12268 if (dwarf_version >= 4)
12269 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12270 "Maximum Operations Per Instruction");
12271 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12272 "Default is_stmt_start flag");
12273 dw2_asm_output_data (1, DWARF_LINE_BASE,
12274 "Line Base Value (Special Opcodes)");
12275 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12276 "Line Range Value (Special Opcodes)");
12277 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12278 "Special Opcode Base");
12280 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12282 int n_op_args;
12283 switch (opc)
12285 case DW_LNS_advance_pc:
12286 case DW_LNS_advance_line:
12287 case DW_LNS_set_file:
12288 case DW_LNS_set_column:
12289 case DW_LNS_fixed_advance_pc:
12290 case DW_LNS_set_isa:
12291 n_op_args = 1;
12292 break;
12293 default:
12294 n_op_args = 0;
12295 break;
12298 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12299 opc, n_op_args);
12302 /* Write out the information about the files we use. */
12303 output_file_names ();
12304 ASM_OUTPUT_LABEL (asm_out_file, p2);
12305 if (prologue_only)
12307 /* Output the marker for the end of the line number info. */
12308 ASM_OUTPUT_LABEL (asm_out_file, l2);
12309 return;
12312 if (separate_line_info)
12314 dw_line_info_table *table;
12315 size_t i;
12317 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12318 if (table->in_use)
12320 output_one_line_info_table (table);
12321 saw_one = true;
12324 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12326 output_one_line_info_table (cold_text_section_line_info);
12327 saw_one = true;
12330 /* ??? Some Darwin linkers crash on a .debug_line section with no
12331 sequences. Further, merely a DW_LNE_end_sequence entry is not
12332 sufficient -- the address column must also be initialized.
12333 Make sure to output at least one set_address/end_sequence pair,
12334 choosing .text since that section is always present. */
12335 if (text_section_line_info->in_use || !saw_one)
12336 output_one_line_info_table (text_section_line_info);
12338 /* Output the marker for the end of the line number info. */
12339 ASM_OUTPUT_LABEL (asm_out_file, l2);
12342 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12344 static inline bool
12345 need_endianity_attribute_p (bool reverse)
12347 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12350 /* Given a pointer to a tree node for some base type, return a pointer to
12351 a DIE that describes the given type. REVERSE is true if the type is
12352 to be interpreted in the reverse storage order wrt the target order.
12354 This routine must only be called for GCC type nodes that correspond to
12355 Dwarf base (fundamental) types. */
12357 static dw_die_ref
12358 base_type_die (tree type, bool reverse)
12360 dw_die_ref base_type_result;
12361 enum dwarf_type encoding;
12362 bool fpt_used = false;
12363 struct fixed_point_type_info fpt_info;
12364 tree type_bias = NULL_TREE;
12366 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12367 return 0;
12369 /* If this is a subtype that should not be emitted as a subrange type,
12370 use the base type. See subrange_type_for_debug_p. */
12371 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12372 type = TREE_TYPE (type);
12374 switch (TREE_CODE (type))
12376 case INTEGER_TYPE:
12377 if ((dwarf_version >= 4 || !dwarf_strict)
12378 && TYPE_NAME (type)
12379 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12380 && DECL_IS_BUILTIN (TYPE_NAME (type))
12381 && DECL_NAME (TYPE_NAME (type)))
12383 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12384 if (strcmp (name, "char16_t") == 0
12385 || strcmp (name, "char32_t") == 0)
12387 encoding = DW_ATE_UTF;
12388 break;
12391 if ((dwarf_version >= 3 || !dwarf_strict)
12392 && lang_hooks.types.get_fixed_point_type_info)
12394 memset (&fpt_info, 0, sizeof (fpt_info));
12395 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12397 fpt_used = true;
12398 encoding = ((TYPE_UNSIGNED (type))
12399 ? DW_ATE_unsigned_fixed
12400 : DW_ATE_signed_fixed);
12401 break;
12404 if (TYPE_STRING_FLAG (type))
12406 if (TYPE_UNSIGNED (type))
12407 encoding = DW_ATE_unsigned_char;
12408 else
12409 encoding = DW_ATE_signed_char;
12411 else if (TYPE_UNSIGNED (type))
12412 encoding = DW_ATE_unsigned;
12413 else
12414 encoding = DW_ATE_signed;
12416 if (!dwarf_strict
12417 && lang_hooks.types.get_type_bias)
12418 type_bias = lang_hooks.types.get_type_bias (type);
12419 break;
12421 case REAL_TYPE:
12422 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12424 if (dwarf_version >= 3 || !dwarf_strict)
12425 encoding = DW_ATE_decimal_float;
12426 else
12427 encoding = DW_ATE_lo_user;
12429 else
12430 encoding = DW_ATE_float;
12431 break;
12433 case FIXED_POINT_TYPE:
12434 if (!(dwarf_version >= 3 || !dwarf_strict))
12435 encoding = DW_ATE_lo_user;
12436 else if (TYPE_UNSIGNED (type))
12437 encoding = DW_ATE_unsigned_fixed;
12438 else
12439 encoding = DW_ATE_signed_fixed;
12440 break;
12442 /* Dwarf2 doesn't know anything about complex ints, so use
12443 a user defined type for it. */
12444 case COMPLEX_TYPE:
12445 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12446 encoding = DW_ATE_complex_float;
12447 else
12448 encoding = DW_ATE_lo_user;
12449 break;
12451 case BOOLEAN_TYPE:
12452 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12453 encoding = DW_ATE_boolean;
12454 break;
12456 default:
12457 /* No other TREE_CODEs are Dwarf fundamental types. */
12458 gcc_unreachable ();
12461 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12463 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12464 int_size_in_bytes (type));
12465 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12467 if (need_endianity_attribute_p (reverse))
12468 add_AT_unsigned (base_type_result, DW_AT_endianity,
12469 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12471 add_alignment_attribute (base_type_result, type);
12473 if (fpt_used)
12475 switch (fpt_info.scale_factor_kind)
12477 case fixed_point_scale_factor_binary:
12478 add_AT_int (base_type_result, DW_AT_binary_scale,
12479 fpt_info.scale_factor.binary);
12480 break;
12482 case fixed_point_scale_factor_decimal:
12483 add_AT_int (base_type_result, DW_AT_decimal_scale,
12484 fpt_info.scale_factor.decimal);
12485 break;
12487 case fixed_point_scale_factor_arbitrary:
12488 /* Arbitrary scale factors cannot be described in standard DWARF,
12489 yet. */
12490 if (!dwarf_strict)
12492 /* Describe the scale factor as a rational constant. */
12493 const dw_die_ref scale_factor
12494 = new_die (DW_TAG_constant, comp_unit_die (), type);
12496 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12497 fpt_info.scale_factor.arbitrary.numerator);
12498 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12499 fpt_info.scale_factor.arbitrary.denominator);
12501 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12503 break;
12505 default:
12506 gcc_unreachable ();
12510 if (type_bias)
12511 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12512 dw_scalar_form_constant
12513 | dw_scalar_form_exprloc
12514 | dw_scalar_form_reference,
12515 NULL);
12517 add_pubtype (type, base_type_result);
12519 return base_type_result;
12522 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12523 named 'auto' in its type: return true for it, false otherwise. */
12525 static inline bool
12526 is_cxx_auto (tree type)
12528 if (is_cxx ())
12530 tree name = TYPE_IDENTIFIER (type);
12531 if (name == get_identifier ("auto")
12532 || name == get_identifier ("decltype(auto)"))
12533 return true;
12535 return false;
12538 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12539 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12541 static inline int
12542 is_base_type (tree type)
12544 switch (TREE_CODE (type))
12546 case ERROR_MARK:
12547 case VOID_TYPE:
12548 case INTEGER_TYPE:
12549 case REAL_TYPE:
12550 case FIXED_POINT_TYPE:
12551 case COMPLEX_TYPE:
12552 case BOOLEAN_TYPE:
12553 case POINTER_BOUNDS_TYPE:
12554 return 1;
12556 case ARRAY_TYPE:
12557 case RECORD_TYPE:
12558 case UNION_TYPE:
12559 case QUAL_UNION_TYPE:
12560 case ENUMERAL_TYPE:
12561 case FUNCTION_TYPE:
12562 case METHOD_TYPE:
12563 case POINTER_TYPE:
12564 case REFERENCE_TYPE:
12565 case NULLPTR_TYPE:
12566 case OFFSET_TYPE:
12567 case LANG_TYPE:
12568 case VECTOR_TYPE:
12569 return 0;
12571 default:
12572 if (is_cxx_auto (type))
12573 return 0;
12574 gcc_unreachable ();
12577 return 0;
12580 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12581 node, return the size in bits for the type if it is a constant, or else
12582 return the alignment for the type if the type's size is not constant, or
12583 else return BITS_PER_WORD if the type actually turns out to be an
12584 ERROR_MARK node. */
12586 static inline unsigned HOST_WIDE_INT
12587 simple_type_size_in_bits (const_tree type)
12589 if (TREE_CODE (type) == ERROR_MARK)
12590 return BITS_PER_WORD;
12591 else if (TYPE_SIZE (type) == NULL_TREE)
12592 return 0;
12593 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12594 return tree_to_uhwi (TYPE_SIZE (type));
12595 else
12596 return TYPE_ALIGN (type);
12599 /* Similarly, but return an offset_int instead of UHWI. */
12601 static inline offset_int
12602 offset_int_type_size_in_bits (const_tree type)
12604 if (TREE_CODE (type) == ERROR_MARK)
12605 return BITS_PER_WORD;
12606 else if (TYPE_SIZE (type) == NULL_TREE)
12607 return 0;
12608 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12609 return wi::to_offset (TYPE_SIZE (type));
12610 else
12611 return TYPE_ALIGN (type);
12614 /* Given a pointer to a tree node for a subrange type, return a pointer
12615 to a DIE that describes the given type. */
12617 static dw_die_ref
12618 subrange_type_die (tree type, tree low, tree high, tree bias,
12619 dw_die_ref context_die)
12621 dw_die_ref subrange_die;
12622 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12624 if (context_die == NULL)
12625 context_die = comp_unit_die ();
12627 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12629 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12631 /* The size of the subrange type and its base type do not match,
12632 so we need to generate a size attribute for the subrange type. */
12633 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12636 add_alignment_attribute (subrange_die, type);
12638 if (low)
12639 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12640 if (high)
12641 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12642 if (bias && !dwarf_strict)
12643 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12644 dw_scalar_form_constant
12645 | dw_scalar_form_exprloc
12646 | dw_scalar_form_reference,
12647 NULL);
12649 return subrange_die;
12652 /* Returns the (const and/or volatile) cv_qualifiers associated with
12653 the decl node. This will normally be augmented with the
12654 cv_qualifiers of the underlying type in add_type_attribute. */
12656 static int
12657 decl_quals (const_tree decl)
12659 return ((TREE_READONLY (decl)
12660 /* The C++ front-end correctly marks reference-typed
12661 variables as readonly, but from a language (and debug
12662 info) standpoint they are not const-qualified. */
12663 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12664 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12665 | (TREE_THIS_VOLATILE (decl)
12666 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12669 /* Determine the TYPE whose qualifiers match the largest strict subset
12670 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12671 qualifiers outside QUAL_MASK. */
12673 static int
12674 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12676 tree t;
12677 int best_rank = 0, best_qual = 0, max_rank;
12679 type_quals &= qual_mask;
12680 max_rank = popcount_hwi (type_quals) - 1;
12682 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12683 t = TYPE_NEXT_VARIANT (t))
12685 int q = TYPE_QUALS (t) & qual_mask;
12687 if ((q & type_quals) == q && q != type_quals
12688 && check_base_type (t, type))
12690 int rank = popcount_hwi (q);
12692 if (rank > best_rank)
12694 best_rank = rank;
12695 best_qual = q;
12700 return best_qual;
12703 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12704 static const dwarf_qual_info_t dwarf_qual_info[] =
12706 { TYPE_QUAL_CONST, DW_TAG_const_type },
12707 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12708 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12709 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12711 static const unsigned int dwarf_qual_info_size
12712 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12714 /* If DIE is a qualified DIE of some base DIE with the same parent,
12715 return the base DIE, otherwise return NULL. Set MASK to the
12716 qualifiers added compared to the returned DIE. */
12718 static dw_die_ref
12719 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12721 unsigned int i;
12722 for (i = 0; i < dwarf_qual_info_size; i++)
12723 if (die->die_tag == dwarf_qual_info[i].t)
12724 break;
12725 if (i == dwarf_qual_info_size)
12726 return NULL;
12727 if (vec_safe_length (die->die_attr) != 1)
12728 return NULL;
12729 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12730 if (type == NULL || type->die_parent != die->die_parent)
12731 return NULL;
12732 *mask |= dwarf_qual_info[i].q;
12733 if (depth)
12735 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12736 if (ret)
12737 return ret;
12739 return type;
12742 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12743 entry that chains the modifiers specified by CV_QUALS in front of the
12744 given type. REVERSE is true if the type is to be interpreted in the
12745 reverse storage order wrt the target order. */
12747 static dw_die_ref
12748 modified_type_die (tree type, int cv_quals, bool reverse,
12749 dw_die_ref context_die)
12751 enum tree_code code = TREE_CODE (type);
12752 dw_die_ref mod_type_die;
12753 dw_die_ref sub_die = NULL;
12754 tree item_type = NULL;
12755 tree qualified_type;
12756 tree name, low, high;
12757 dw_die_ref mod_scope;
12758 /* Only these cv-qualifiers are currently handled. */
12759 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12760 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12762 if (code == ERROR_MARK)
12763 return NULL;
12765 if (lang_hooks.types.get_debug_type)
12767 tree debug_type = lang_hooks.types.get_debug_type (type);
12769 if (debug_type != NULL_TREE && debug_type != type)
12770 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12773 cv_quals &= cv_qual_mask;
12775 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12776 tag modifier (and not an attribute) old consumers won't be able
12777 to handle it. */
12778 if (dwarf_version < 3)
12779 cv_quals &= ~TYPE_QUAL_RESTRICT;
12781 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12782 if (dwarf_version < 5)
12783 cv_quals &= ~TYPE_QUAL_ATOMIC;
12785 /* See if we already have the appropriately qualified variant of
12786 this type. */
12787 qualified_type = get_qualified_type (type, cv_quals);
12789 if (qualified_type == sizetype)
12791 /* Try not to expose the internal sizetype type's name. */
12792 if (TYPE_NAME (qualified_type)
12793 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12795 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12797 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12798 && (TYPE_PRECISION (t)
12799 == TYPE_PRECISION (qualified_type))
12800 && (TYPE_UNSIGNED (t)
12801 == TYPE_UNSIGNED (qualified_type)));
12802 qualified_type = t;
12804 else if (qualified_type == sizetype
12805 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12806 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12807 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12808 qualified_type = size_type_node;
12812 /* If we do, then we can just use its DIE, if it exists. */
12813 if (qualified_type)
12815 mod_type_die = lookup_type_die (qualified_type);
12817 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12818 if (mod_type_die
12819 && (!need_endianity_attribute_p (reverse)
12820 || !is_base_type (type)
12821 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12822 return mod_type_die;
12825 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12827 /* Handle C typedef types. */
12828 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12829 && !DECL_ARTIFICIAL (name))
12831 tree dtype = TREE_TYPE (name);
12833 if (qualified_type == dtype)
12835 tree origin = decl_ultimate_origin (name);
12837 /* Typedef variants that have an abstract origin don't get their own
12838 type DIE (see gen_typedef_die), so fall back on the ultimate
12839 abstract origin instead. */
12840 if (origin != NULL && origin != name)
12841 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12842 context_die);
12844 /* For a named type, use the typedef. */
12845 gen_type_die (qualified_type, context_die);
12846 return lookup_type_die (qualified_type);
12848 else
12850 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12851 dquals &= cv_qual_mask;
12852 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12853 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12854 /* cv-unqualified version of named type. Just use
12855 the unnamed type to which it refers. */
12856 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12857 reverse, context_die);
12858 /* Else cv-qualified version of named type; fall through. */
12862 mod_scope = scope_die_for (type, context_die);
12864 if (cv_quals)
12866 int sub_quals = 0, first_quals = 0;
12867 unsigned i;
12868 dw_die_ref first = NULL, last = NULL;
12870 /* Determine a lesser qualified type that most closely matches
12871 this one. Then generate DW_TAG_* entries for the remaining
12872 qualifiers. */
12873 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12874 cv_qual_mask);
12875 if (sub_quals && use_debug_types)
12877 bool needed = false;
12878 /* If emitting type units, make sure the order of qualifiers
12879 is canonical. Thus, start from unqualified type if
12880 an earlier qualifier is missing in sub_quals, but some later
12881 one is present there. */
12882 for (i = 0; i < dwarf_qual_info_size; i++)
12883 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12884 needed = true;
12885 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12887 sub_quals = 0;
12888 break;
12891 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12892 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12894 /* As not all intermediate qualified DIEs have corresponding
12895 tree types, ensure that qualified DIEs in the same scope
12896 as their DW_AT_type are emitted after their DW_AT_type,
12897 only with other qualified DIEs for the same type possibly
12898 in between them. Determine the range of such qualified
12899 DIEs now (first being the base type, last being corresponding
12900 last qualified DIE for it). */
12901 unsigned int count = 0;
12902 first = qualified_die_p (mod_type_die, &first_quals,
12903 dwarf_qual_info_size);
12904 if (first == NULL)
12905 first = mod_type_die;
12906 gcc_assert ((first_quals & ~sub_quals) == 0);
12907 for (count = 0, last = first;
12908 count < (1U << dwarf_qual_info_size);
12909 count++, last = last->die_sib)
12911 int quals = 0;
12912 if (last == mod_scope->die_child)
12913 break;
12914 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12915 != first)
12916 break;
12920 for (i = 0; i < dwarf_qual_info_size; i++)
12921 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12923 dw_die_ref d;
12924 if (first && first != last)
12926 for (d = first->die_sib; ; d = d->die_sib)
12928 int quals = 0;
12929 qualified_die_p (d, &quals, dwarf_qual_info_size);
12930 if (quals == (first_quals | dwarf_qual_info[i].q))
12931 break;
12932 if (d == last)
12934 d = NULL;
12935 break;
12938 if (d)
12940 mod_type_die = d;
12941 continue;
12944 if (first)
12946 d = ggc_cleared_alloc<die_node> ();
12947 d->die_tag = dwarf_qual_info[i].t;
12948 add_child_die_after (mod_scope, d, last);
12949 last = d;
12951 else
12952 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12953 if (mod_type_die)
12954 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12955 mod_type_die = d;
12956 first_quals |= dwarf_qual_info[i].q;
12959 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12961 dwarf_tag tag = DW_TAG_pointer_type;
12962 if (code == REFERENCE_TYPE)
12964 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12965 tag = DW_TAG_rvalue_reference_type;
12966 else
12967 tag = DW_TAG_reference_type;
12969 mod_type_die = new_die (tag, mod_scope, type);
12971 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12972 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12973 add_alignment_attribute (mod_type_die, type);
12974 item_type = TREE_TYPE (type);
12976 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12977 if (!ADDR_SPACE_GENERIC_P (as))
12979 int action = targetm.addr_space.debug (as);
12980 if (action >= 0)
12982 /* Positive values indicate an address_class. */
12983 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12985 else
12987 /* Negative values indicate an (inverted) segment base reg. */
12988 dw_loc_descr_ref d
12989 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12990 add_AT_loc (mod_type_die, DW_AT_segment, d);
12994 else if (code == INTEGER_TYPE
12995 && TREE_TYPE (type) != NULL_TREE
12996 && subrange_type_for_debug_p (type, &low, &high))
12998 tree bias = NULL_TREE;
12999 if (lang_hooks.types.get_type_bias)
13000 bias = lang_hooks.types.get_type_bias (type);
13001 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13002 item_type = TREE_TYPE (type);
13004 else if (is_base_type (type))
13005 mod_type_die = base_type_die (type, reverse);
13006 else
13008 gen_type_die (type, context_die);
13010 /* We have to get the type_main_variant here (and pass that to the
13011 `lookup_type_die' routine) because the ..._TYPE node we have
13012 might simply be a *copy* of some original type node (where the
13013 copy was created to help us keep track of typedef names) and
13014 that copy might have a different TYPE_UID from the original
13015 ..._TYPE node. */
13016 if (TREE_CODE (type) == FUNCTION_TYPE
13017 || TREE_CODE (type) == METHOD_TYPE)
13019 /* For function/method types, can't just use type_main_variant here,
13020 because that can have different ref-qualifiers for C++,
13021 but try to canonicalize. */
13022 tree main = TYPE_MAIN_VARIANT (type);
13023 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13024 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13025 && check_base_type (t, main)
13026 && check_lang_type (t, type))
13027 return lookup_type_die (t);
13028 return lookup_type_die (type);
13030 else if (TREE_CODE (type) != VECTOR_TYPE
13031 && TREE_CODE (type) != ARRAY_TYPE)
13032 return lookup_type_die (type_main_variant (type));
13033 else
13034 /* Vectors have the debugging information in the type,
13035 not the main variant. */
13036 return lookup_type_die (type);
13039 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13040 don't output a DW_TAG_typedef, since there isn't one in the
13041 user's program; just attach a DW_AT_name to the type.
13042 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13043 if the base type already has the same name. */
13044 if (name
13045 && ((TREE_CODE (name) != TYPE_DECL
13046 && (qualified_type == TYPE_MAIN_VARIANT (type)
13047 || (cv_quals == TYPE_UNQUALIFIED)))
13048 || (TREE_CODE (name) == TYPE_DECL
13049 && TREE_TYPE (name) == qualified_type
13050 && DECL_NAME (name))))
13052 if (TREE_CODE (name) == TYPE_DECL)
13053 /* Could just call add_name_and_src_coords_attributes here,
13054 but since this is a builtin type it doesn't have any
13055 useful source coordinates anyway. */
13056 name = DECL_NAME (name);
13057 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13059 /* This probably indicates a bug. */
13060 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13062 name = TYPE_IDENTIFIER (type);
13063 add_name_attribute (mod_type_die,
13064 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13067 if (qualified_type)
13068 equate_type_number_to_die (qualified_type, mod_type_die);
13070 if (item_type)
13071 /* We must do this after the equate_type_number_to_die call, in case
13072 this is a recursive type. This ensures that the modified_type_die
13073 recursion will terminate even if the type is recursive. Recursive
13074 types are possible in Ada. */
13075 sub_die = modified_type_die (item_type,
13076 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13077 reverse,
13078 context_die);
13080 if (sub_die != NULL)
13081 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13083 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13084 if (TYPE_ARTIFICIAL (type))
13085 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13087 return mod_type_die;
13090 /* Generate DIEs for the generic parameters of T.
13091 T must be either a generic type or a generic function.
13092 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13094 static void
13095 gen_generic_params_dies (tree t)
13097 tree parms, args;
13098 int parms_num, i;
13099 dw_die_ref die = NULL;
13100 int non_default;
13102 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13103 return;
13105 if (TYPE_P (t))
13106 die = lookup_type_die (t);
13107 else if (DECL_P (t))
13108 die = lookup_decl_die (t);
13110 gcc_assert (die);
13112 parms = lang_hooks.get_innermost_generic_parms (t);
13113 if (!parms)
13114 /* T has no generic parameter. It means T is neither a generic type
13115 or function. End of story. */
13116 return;
13118 parms_num = TREE_VEC_LENGTH (parms);
13119 args = lang_hooks.get_innermost_generic_args (t);
13120 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13121 non_default = int_cst_value (TREE_CHAIN (args));
13122 else
13123 non_default = TREE_VEC_LENGTH (args);
13124 for (i = 0; i < parms_num; i++)
13126 tree parm, arg, arg_pack_elems;
13127 dw_die_ref parm_die;
13129 parm = TREE_VEC_ELT (parms, i);
13130 arg = TREE_VEC_ELT (args, i);
13131 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13132 gcc_assert (parm && TREE_VALUE (parm) && arg);
13134 if (parm && TREE_VALUE (parm) && arg)
13136 /* If PARM represents a template parameter pack,
13137 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13138 by DW_TAG_template_*_parameter DIEs for the argument
13139 pack elements of ARG. Note that ARG would then be
13140 an argument pack. */
13141 if (arg_pack_elems)
13142 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13143 arg_pack_elems,
13144 die);
13145 else
13146 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13147 true /* emit name */, die);
13148 if (i >= non_default)
13149 add_AT_flag (parm_die, DW_AT_default_value, 1);
13154 /* Create and return a DIE for PARM which should be
13155 the representation of a generic type parameter.
13156 For instance, in the C++ front end, PARM would be a template parameter.
13157 ARG is the argument to PARM.
13158 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13159 name of the PARM.
13160 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13161 as a child node. */
13163 static dw_die_ref
13164 generic_parameter_die (tree parm, tree arg,
13165 bool emit_name_p,
13166 dw_die_ref parent_die)
13168 dw_die_ref tmpl_die = NULL;
13169 const char *name = NULL;
13171 if (!parm || !DECL_NAME (parm) || !arg)
13172 return NULL;
13174 /* We support non-type generic parameters and arguments,
13175 type generic parameters and arguments, as well as
13176 generic generic parameters (a.k.a. template template parameters in C++)
13177 and arguments. */
13178 if (TREE_CODE (parm) == PARM_DECL)
13179 /* PARM is a nontype generic parameter */
13180 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13181 else if (TREE_CODE (parm) == TYPE_DECL)
13182 /* PARM is a type generic parameter. */
13183 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13184 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13185 /* PARM is a generic generic parameter.
13186 Its DIE is a GNU extension. It shall have a
13187 DW_AT_name attribute to represent the name of the template template
13188 parameter, and a DW_AT_GNU_template_name attribute to represent the
13189 name of the template template argument. */
13190 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13191 parent_die, parm);
13192 else
13193 gcc_unreachable ();
13195 if (tmpl_die)
13197 tree tmpl_type;
13199 /* If PARM is a generic parameter pack, it means we are
13200 emitting debug info for a template argument pack element.
13201 In other terms, ARG is a template argument pack element.
13202 In that case, we don't emit any DW_AT_name attribute for
13203 the die. */
13204 if (emit_name_p)
13206 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13207 gcc_assert (name);
13208 add_AT_string (tmpl_die, DW_AT_name, name);
13211 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13213 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13214 TMPL_DIE should have a child DW_AT_type attribute that is set
13215 to the type of the argument to PARM, which is ARG.
13216 If PARM is a type generic parameter, TMPL_DIE should have a
13217 child DW_AT_type that is set to ARG. */
13218 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13219 add_type_attribute (tmpl_die, tmpl_type,
13220 (TREE_THIS_VOLATILE (tmpl_type)
13221 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13222 false, parent_die);
13224 else
13226 /* So TMPL_DIE is a DIE representing a
13227 a generic generic template parameter, a.k.a template template
13228 parameter in C++ and arg is a template. */
13230 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13231 to the name of the argument. */
13232 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13233 if (name)
13234 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13237 if (TREE_CODE (parm) == PARM_DECL)
13238 /* So PARM is a non-type generic parameter.
13239 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13240 attribute of TMPL_DIE which value represents the value
13241 of ARG.
13242 We must be careful here:
13243 The value of ARG might reference some function decls.
13244 We might currently be emitting debug info for a generic
13245 type and types are emitted before function decls, we don't
13246 know if the function decls referenced by ARG will actually be
13247 emitted after cgraph computations.
13248 So must defer the generation of the DW_AT_const_value to
13249 after cgraph is ready. */
13250 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13253 return tmpl_die;
13256 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13257 PARM_PACK must be a template parameter pack. The returned DIE
13258 will be child DIE of PARENT_DIE. */
13260 static dw_die_ref
13261 template_parameter_pack_die (tree parm_pack,
13262 tree parm_pack_args,
13263 dw_die_ref parent_die)
13265 dw_die_ref die;
13266 int j;
13268 gcc_assert (parent_die && parm_pack);
13270 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13271 add_name_and_src_coords_attributes (die, parm_pack);
13272 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13273 generic_parameter_die (parm_pack,
13274 TREE_VEC_ELT (parm_pack_args, j),
13275 false /* Don't emit DW_AT_name */,
13276 die);
13277 return die;
13280 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13281 an enumerated type. */
13283 static inline int
13284 type_is_enum (const_tree type)
13286 return TREE_CODE (type) == ENUMERAL_TYPE;
13289 /* Return the DBX register number described by a given RTL node. */
13291 static unsigned int
13292 dbx_reg_number (const_rtx rtl)
13294 unsigned regno = REGNO (rtl);
13296 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13298 #ifdef LEAF_REG_REMAP
13299 if (crtl->uses_only_leaf_regs)
13301 int leaf_reg = LEAF_REG_REMAP (regno);
13302 if (leaf_reg != -1)
13303 regno = (unsigned) leaf_reg;
13305 #endif
13307 regno = DBX_REGISTER_NUMBER (regno);
13308 gcc_assert (regno != INVALID_REGNUM);
13309 return regno;
13312 /* Optionally add a DW_OP_piece term to a location description expression.
13313 DW_OP_piece is only added if the location description expression already
13314 doesn't end with DW_OP_piece. */
13316 static void
13317 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13319 dw_loc_descr_ref loc;
13321 if (*list_head != NULL)
13323 /* Find the end of the chain. */
13324 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13327 if (loc->dw_loc_opc != DW_OP_piece)
13328 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13332 /* Return a location descriptor that designates a machine register or
13333 zero if there is none. */
13335 static dw_loc_descr_ref
13336 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13338 rtx regs;
13340 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13341 return 0;
13343 /* We only use "frame base" when we're sure we're talking about the
13344 post-prologue local stack frame. We do this by *not* running
13345 register elimination until this point, and recognizing the special
13346 argument pointer and soft frame pointer rtx's.
13347 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13348 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13349 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13351 dw_loc_descr_ref result = NULL;
13353 if (dwarf_version >= 4 || !dwarf_strict)
13355 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13356 initialized);
13357 if (result)
13358 add_loc_descr (&result,
13359 new_loc_descr (DW_OP_stack_value, 0, 0));
13361 return result;
13364 regs = targetm.dwarf_register_span (rtl);
13366 if (REG_NREGS (rtl) > 1 || regs)
13367 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13368 else
13370 unsigned int dbx_regnum = dbx_reg_number (rtl);
13371 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13372 return 0;
13373 return one_reg_loc_descriptor (dbx_regnum, initialized);
13377 /* Return a location descriptor that designates a machine register for
13378 a given hard register number. */
13380 static dw_loc_descr_ref
13381 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13383 dw_loc_descr_ref reg_loc_descr;
13385 if (regno <= 31)
13386 reg_loc_descr
13387 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13388 else
13389 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13391 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13392 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13394 return reg_loc_descr;
13397 /* Given an RTL of a register, return a location descriptor that
13398 designates a value that spans more than one register. */
13400 static dw_loc_descr_ref
13401 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13402 enum var_init_status initialized)
13404 int size, i;
13405 dw_loc_descr_ref loc_result = NULL;
13407 /* Simple, contiguous registers. */
13408 if (regs == NULL_RTX)
13410 unsigned reg = REGNO (rtl);
13411 int nregs;
13413 #ifdef LEAF_REG_REMAP
13414 if (crtl->uses_only_leaf_regs)
13416 int leaf_reg = LEAF_REG_REMAP (reg);
13417 if (leaf_reg != -1)
13418 reg = (unsigned) leaf_reg;
13420 #endif
13422 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13423 nregs = REG_NREGS (rtl);
13425 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13427 loc_result = NULL;
13428 while (nregs--)
13430 dw_loc_descr_ref t;
13432 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13433 VAR_INIT_STATUS_INITIALIZED);
13434 add_loc_descr (&loc_result, t);
13435 add_loc_descr_op_piece (&loc_result, size);
13436 ++reg;
13438 return loc_result;
13441 /* Now onto stupid register sets in non contiguous locations. */
13443 gcc_assert (GET_CODE (regs) == PARALLEL);
13445 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13446 loc_result = NULL;
13448 for (i = 0; i < XVECLEN (regs, 0); ++i)
13450 dw_loc_descr_ref t;
13452 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13453 VAR_INIT_STATUS_INITIALIZED);
13454 add_loc_descr (&loc_result, t);
13455 add_loc_descr_op_piece (&loc_result, size);
13458 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13459 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13460 return loc_result;
13463 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13465 /* Return a location descriptor that designates a constant i,
13466 as a compound operation from constant (i >> shift), constant shift
13467 and DW_OP_shl. */
13469 static dw_loc_descr_ref
13470 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13472 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13473 add_loc_descr (&ret, int_loc_descriptor (shift));
13474 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13475 return ret;
13478 /* Return a location descriptor that designates a constant. */
13480 static dw_loc_descr_ref
13481 int_loc_descriptor (HOST_WIDE_INT i)
13483 enum dwarf_location_atom op;
13485 /* Pick the smallest representation of a constant, rather than just
13486 defaulting to the LEB encoding. */
13487 if (i >= 0)
13489 int clz = clz_hwi (i);
13490 int ctz = ctz_hwi (i);
13491 if (i <= 31)
13492 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13493 else if (i <= 0xff)
13494 op = DW_OP_const1u;
13495 else if (i <= 0xffff)
13496 op = DW_OP_const2u;
13497 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13498 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13499 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13500 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13501 while DW_OP_const4u is 5 bytes. */
13502 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13503 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13504 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13505 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13506 while DW_OP_const4u is 5 bytes. */
13507 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13509 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13510 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13511 <= 4)
13513 /* As i >= 2**31, the double cast above will yield a negative number.
13514 Since wrapping is defined in DWARF expressions we can output big
13515 positive integers as small negative ones, regardless of the size
13516 of host wide ints.
13518 Here, since the evaluator will handle 32-bit values and since i >=
13519 2**31, we know it's going to be interpreted as a negative literal:
13520 store it this way if we can do better than 5 bytes this way. */
13521 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13523 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13524 op = DW_OP_const4u;
13526 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13527 least 6 bytes: see if we can do better before falling back to it. */
13528 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13529 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13530 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13531 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13532 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13533 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13534 >= HOST_BITS_PER_WIDE_INT)
13535 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13536 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13537 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13538 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13539 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13540 && size_of_uleb128 (i) > 6)
13541 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13542 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13543 else
13544 op = DW_OP_constu;
13546 else
13548 if (i >= -0x80)
13549 op = DW_OP_const1s;
13550 else if (i >= -0x8000)
13551 op = DW_OP_const2s;
13552 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13554 if (size_of_int_loc_descriptor (i) < 5)
13556 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13557 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13558 return ret;
13560 op = DW_OP_const4s;
13562 else
13564 if (size_of_int_loc_descriptor (i)
13565 < (unsigned long) 1 + size_of_sleb128 (i))
13567 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13568 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13569 return ret;
13571 op = DW_OP_consts;
13575 return new_loc_descr (op, i, 0);
13578 /* Likewise, for unsigned constants. */
13580 static dw_loc_descr_ref
13581 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13583 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13584 const unsigned HOST_WIDE_INT max_uint
13585 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13587 /* If possible, use the clever signed constants handling. */
13588 if (i <= max_int)
13589 return int_loc_descriptor ((HOST_WIDE_INT) i);
13591 /* Here, we are left with positive numbers that cannot be represented as
13592 HOST_WIDE_INT, i.e.:
13593 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13595 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13596 whereas may be better to output a negative integer: thanks to integer
13597 wrapping, we know that:
13598 x = x - 2 ** DWARF2_ADDR_SIZE
13599 = x - 2 * (max (HOST_WIDE_INT) + 1)
13600 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13601 small negative integers. Let's try that in cases it will clearly improve
13602 the encoding: there is no gain turning DW_OP_const4u into
13603 DW_OP_const4s. */
13604 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13605 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13606 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13608 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13610 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13611 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13612 const HOST_WIDE_INT second_shift
13613 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13615 /* So we finally have:
13616 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13617 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13618 return int_loc_descriptor (second_shift);
13621 /* Last chance: fallback to a simple constant operation. */
13622 return new_loc_descr
13623 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13624 ? DW_OP_const4u
13625 : DW_OP_const8u,
13626 i, 0);
13629 /* Generate and return a location description that computes the unsigned
13630 comparison of the two stack top entries (a OP b where b is the top-most
13631 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13632 LE_EXPR, GT_EXPR or GE_EXPR. */
13634 static dw_loc_descr_ref
13635 uint_comparison_loc_list (enum tree_code kind)
13637 enum dwarf_location_atom op, flip_op;
13638 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13640 switch (kind)
13642 case LT_EXPR:
13643 op = DW_OP_lt;
13644 break;
13645 case LE_EXPR:
13646 op = DW_OP_le;
13647 break;
13648 case GT_EXPR:
13649 op = DW_OP_gt;
13650 break;
13651 case GE_EXPR:
13652 op = DW_OP_ge;
13653 break;
13654 default:
13655 gcc_unreachable ();
13658 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13659 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13661 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13662 possible to perform unsigned comparisons: we just have to distinguish
13663 three cases:
13665 1. when a and b have the same sign (as signed integers); then we should
13666 return: a OP(signed) b;
13668 2. when a is a negative signed integer while b is a positive one, then a
13669 is a greater unsigned integer than b; likewise when a and b's roles
13670 are flipped.
13672 So first, compare the sign of the two operands. */
13673 ret = new_loc_descr (DW_OP_over, 0, 0);
13674 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13675 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13676 /* If they have different signs (i.e. they have different sign bits), then
13677 the stack top value has now the sign bit set and thus it's smaller than
13678 zero. */
13679 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13680 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13681 add_loc_descr (&ret, bra_node);
13683 /* We are in case 1. At this point, we know both operands have the same
13684 sign, to it's safe to use the built-in signed comparison. */
13685 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13686 add_loc_descr (&ret, jmp_node);
13688 /* We are in case 2. Here, we know both operands do not have the same sign,
13689 so we have to flip the signed comparison. */
13690 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13691 tmp = new_loc_descr (flip_op, 0, 0);
13692 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13693 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13694 add_loc_descr (&ret, tmp);
13696 /* This dummy operation is necessary to make the two branches join. */
13697 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13698 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13699 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13700 add_loc_descr (&ret, tmp);
13702 return ret;
13705 /* Likewise, but takes the location description lists (might be destructive on
13706 them). Return NULL if either is NULL or if concatenation fails. */
13708 static dw_loc_list_ref
13709 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13710 enum tree_code kind)
13712 if (left == NULL || right == NULL)
13713 return NULL;
13715 add_loc_list (&left, right);
13716 if (left == NULL)
13717 return NULL;
13719 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13720 return left;
13723 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13724 without actually allocating it. */
13726 static unsigned long
13727 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13729 return size_of_int_loc_descriptor (i >> shift)
13730 + size_of_int_loc_descriptor (shift)
13731 + 1;
13734 /* Return size_of_locs (int_loc_descriptor (i)) without
13735 actually allocating it. */
13737 static unsigned long
13738 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13740 unsigned long s;
13742 if (i >= 0)
13744 int clz, ctz;
13745 if (i <= 31)
13746 return 1;
13747 else if (i <= 0xff)
13748 return 2;
13749 else if (i <= 0xffff)
13750 return 3;
13751 clz = clz_hwi (i);
13752 ctz = ctz_hwi (i);
13753 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13754 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13755 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13756 - clz - 5);
13757 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13758 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13759 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13760 - clz - 8);
13761 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13762 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13763 <= 4)
13764 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13765 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13766 return 5;
13767 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13768 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13769 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13770 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13771 - clz - 8);
13772 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13773 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13774 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13775 - clz - 16);
13776 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13777 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13778 && s > 6)
13779 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13780 - clz - 32);
13781 else
13782 return 1 + s;
13784 else
13786 if (i >= -0x80)
13787 return 2;
13788 else if (i >= -0x8000)
13789 return 3;
13790 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13792 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13794 s = size_of_int_loc_descriptor (-i) + 1;
13795 if (s < 5)
13796 return s;
13798 return 5;
13800 else
13802 unsigned long r = 1 + size_of_sleb128 (i);
13803 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13805 s = size_of_int_loc_descriptor (-i) + 1;
13806 if (s < r)
13807 return s;
13809 return r;
13814 /* Return loc description representing "address" of integer value.
13815 This can appear only as toplevel expression. */
13817 static dw_loc_descr_ref
13818 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13820 int litsize;
13821 dw_loc_descr_ref loc_result = NULL;
13823 if (!(dwarf_version >= 4 || !dwarf_strict))
13824 return NULL;
13826 litsize = size_of_int_loc_descriptor (i);
13827 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13828 is more compact. For DW_OP_stack_value we need:
13829 litsize + 1 (DW_OP_stack_value)
13830 and for DW_OP_implicit_value:
13831 1 (DW_OP_implicit_value) + 1 (length) + size. */
13832 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13834 loc_result = int_loc_descriptor (i);
13835 add_loc_descr (&loc_result,
13836 new_loc_descr (DW_OP_stack_value, 0, 0));
13837 return loc_result;
13840 loc_result = new_loc_descr (DW_OP_implicit_value,
13841 size, 0);
13842 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13843 loc_result->dw_loc_oprnd2.v.val_int = i;
13844 return loc_result;
13847 /* Return a location descriptor that designates a base+offset location. */
13849 static dw_loc_descr_ref
13850 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13851 enum var_init_status initialized)
13853 unsigned int regno;
13854 dw_loc_descr_ref result;
13855 dw_fde_ref fde = cfun->fde;
13857 /* We only use "frame base" when we're sure we're talking about the
13858 post-prologue local stack frame. We do this by *not* running
13859 register elimination until this point, and recognizing the special
13860 argument pointer and soft frame pointer rtx's. */
13861 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13863 rtx elim = (ira_use_lra_p
13864 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13865 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13867 if (elim != reg)
13869 if (GET_CODE (elim) == PLUS)
13871 offset += INTVAL (XEXP (elim, 1));
13872 elim = XEXP (elim, 0);
13874 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13875 && (elim == hard_frame_pointer_rtx
13876 || elim == stack_pointer_rtx))
13877 || elim == (frame_pointer_needed
13878 ? hard_frame_pointer_rtx
13879 : stack_pointer_rtx));
13881 /* If drap register is used to align stack, use frame
13882 pointer + offset to access stack variables. If stack
13883 is aligned without drap, use stack pointer + offset to
13884 access stack variables. */
13885 if (crtl->stack_realign_tried
13886 && reg == frame_pointer_rtx)
13888 int base_reg
13889 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13890 ? HARD_FRAME_POINTER_REGNUM
13891 : REGNO (elim));
13892 return new_reg_loc_descr (base_reg, offset);
13895 gcc_assert (frame_pointer_fb_offset_valid);
13896 offset += frame_pointer_fb_offset;
13897 return new_loc_descr (DW_OP_fbreg, offset, 0);
13901 regno = REGNO (reg);
13902 #ifdef LEAF_REG_REMAP
13903 if (crtl->uses_only_leaf_regs)
13905 int leaf_reg = LEAF_REG_REMAP (regno);
13906 if (leaf_reg != -1)
13907 regno = (unsigned) leaf_reg;
13909 #endif
13910 regno = DWARF_FRAME_REGNUM (regno);
13912 if (!optimize && fde
13913 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13915 /* Use cfa+offset to represent the location of arguments passed
13916 on the stack when drap is used to align stack.
13917 Only do this when not optimizing, for optimized code var-tracking
13918 is supposed to track where the arguments live and the register
13919 used as vdrap or drap in some spot might be used for something
13920 else in other part of the routine. */
13921 return new_loc_descr (DW_OP_fbreg, offset, 0);
13924 if (regno <= 31)
13925 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13926 offset, 0);
13927 else
13928 result = new_loc_descr (DW_OP_bregx, regno, offset);
13930 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13931 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13933 return result;
13936 /* Return true if this RTL expression describes a base+offset calculation. */
13938 static inline int
13939 is_based_loc (const_rtx rtl)
13941 return (GET_CODE (rtl) == PLUS
13942 && ((REG_P (XEXP (rtl, 0))
13943 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13944 && CONST_INT_P (XEXP (rtl, 1)))));
13947 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13948 failed. */
13950 static dw_loc_descr_ref
13951 tls_mem_loc_descriptor (rtx mem)
13953 tree base;
13954 dw_loc_descr_ref loc_result;
13956 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13957 return NULL;
13959 base = get_base_address (MEM_EXPR (mem));
13960 if (base == NULL
13961 || !VAR_P (base)
13962 || !DECL_THREAD_LOCAL_P (base))
13963 return NULL;
13965 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13966 if (loc_result == NULL)
13967 return NULL;
13969 if (MEM_OFFSET (mem))
13970 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13972 return loc_result;
13975 /* Output debug info about reason why we failed to expand expression as dwarf
13976 expression. */
13978 static void
13979 expansion_failed (tree expr, rtx rtl, char const *reason)
13981 if (dump_file && (dump_flags & TDF_DETAILS))
13983 fprintf (dump_file, "Failed to expand as dwarf: ");
13984 if (expr)
13985 print_generic_expr (dump_file, expr, dump_flags);
13986 if (rtl)
13988 fprintf (dump_file, "\n");
13989 print_rtl (dump_file, rtl);
13991 fprintf (dump_file, "\nReason: %s\n", reason);
13995 /* Helper function for const_ok_for_output. */
13997 static bool
13998 const_ok_for_output_1 (rtx rtl)
14000 if (GET_CODE (rtl) == UNSPEC)
14002 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
14003 we can't express it in the debug info. */
14004 /* Don't complain about TLS UNSPECs, those are just too hard to
14005 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14006 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14007 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14008 if (flag_checking
14009 && (XVECLEN (rtl, 0) == 0
14010 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14011 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14012 inform (current_function_decl
14013 ? DECL_SOURCE_LOCATION (current_function_decl)
14014 : UNKNOWN_LOCATION,
14015 #if NUM_UNSPEC_VALUES > 0
14016 "non-delegitimized UNSPEC %s (%d) found in variable location",
14017 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14018 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14019 XINT (rtl, 1));
14020 #else
14021 "non-delegitimized UNSPEC %d found in variable location",
14022 XINT (rtl, 1));
14023 #endif
14024 expansion_failed (NULL_TREE, rtl,
14025 "UNSPEC hasn't been delegitimized.\n");
14026 return false;
14029 if (targetm.const_not_ok_for_debug_p (rtl))
14031 expansion_failed (NULL_TREE, rtl,
14032 "Expression rejected for debug by the backend.\n");
14033 return false;
14036 /* FIXME: Refer to PR60655. It is possible for simplification
14037 of rtl expressions in var tracking to produce such expressions.
14038 We should really identify / validate expressions
14039 enclosed in CONST that can be handled by assemblers on various
14040 targets and only handle legitimate cases here. */
14041 if (GET_CODE (rtl) != SYMBOL_REF)
14043 if (GET_CODE (rtl) == NOT)
14044 return false;
14045 return true;
14048 if (CONSTANT_POOL_ADDRESS_P (rtl))
14050 bool marked;
14051 get_pool_constant_mark (rtl, &marked);
14052 /* If all references to this pool constant were optimized away,
14053 it was not output and thus we can't represent it. */
14054 if (!marked)
14056 expansion_failed (NULL_TREE, rtl,
14057 "Constant was removed from constant pool.\n");
14058 return false;
14062 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14063 return false;
14065 /* Avoid references to external symbols in debug info, on several targets
14066 the linker might even refuse to link when linking a shared library,
14067 and in many other cases the relocations for .debug_info/.debug_loc are
14068 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14069 to be defined within the same shared library or executable are fine. */
14070 if (SYMBOL_REF_EXTERNAL_P (rtl))
14072 tree decl = SYMBOL_REF_DECL (rtl);
14074 if (decl == NULL || !targetm.binds_local_p (decl))
14076 expansion_failed (NULL_TREE, rtl,
14077 "Symbol not defined in current TU.\n");
14078 return false;
14082 return true;
14085 /* Return true if constant RTL can be emitted in DW_OP_addr or
14086 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14087 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14089 static bool
14090 const_ok_for_output (rtx rtl)
14092 if (GET_CODE (rtl) == SYMBOL_REF)
14093 return const_ok_for_output_1 (rtl);
14095 if (GET_CODE (rtl) == CONST)
14097 subrtx_var_iterator::array_type array;
14098 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14099 if (!const_ok_for_output_1 (*iter))
14100 return false;
14101 return true;
14104 return true;
14107 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14108 if possible, NULL otherwise. */
14110 static dw_die_ref
14111 base_type_for_mode (machine_mode mode, bool unsignedp)
14113 dw_die_ref type_die;
14114 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14116 if (type == NULL)
14117 return NULL;
14118 switch (TREE_CODE (type))
14120 case INTEGER_TYPE:
14121 case REAL_TYPE:
14122 break;
14123 default:
14124 return NULL;
14126 type_die = lookup_type_die (type);
14127 if (!type_die)
14128 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14129 comp_unit_die ());
14130 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14131 return NULL;
14132 return type_die;
14135 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14136 type matching MODE, or, if MODE is narrower than or as wide as
14137 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14138 possible. */
14140 static dw_loc_descr_ref
14141 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
14143 machine_mode outer_mode = mode;
14144 dw_die_ref type_die;
14145 dw_loc_descr_ref cvt;
14147 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14149 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14150 return op;
14152 type_die = base_type_for_mode (outer_mode, 1);
14153 if (type_die == NULL)
14154 return NULL;
14155 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14156 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14157 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14158 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14159 add_loc_descr (&op, cvt);
14160 return op;
14163 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14165 static dw_loc_descr_ref
14166 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14167 dw_loc_descr_ref op1)
14169 dw_loc_descr_ref ret = op0;
14170 add_loc_descr (&ret, op1);
14171 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14172 if (STORE_FLAG_VALUE != 1)
14174 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14175 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14177 return ret;
14180 /* Return location descriptor for signed comparison OP RTL. */
14182 static dw_loc_descr_ref
14183 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14184 machine_mode mem_mode)
14186 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14187 dw_loc_descr_ref op0, op1;
14188 int shift;
14190 if (op_mode == VOIDmode)
14191 op_mode = GET_MODE (XEXP (rtl, 1));
14192 if (op_mode == VOIDmode)
14193 return NULL;
14195 if (dwarf_strict
14196 && dwarf_version < 5
14197 && (!SCALAR_INT_MODE_P (op_mode)
14198 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
14199 return NULL;
14201 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14202 VAR_INIT_STATUS_INITIALIZED);
14203 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14204 VAR_INIT_STATUS_INITIALIZED);
14206 if (op0 == NULL || op1 == NULL)
14207 return NULL;
14209 if (!SCALAR_INT_MODE_P (op_mode)
14210 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14211 return compare_loc_descriptor (op, op0, op1);
14213 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14215 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14216 dw_loc_descr_ref cvt;
14218 if (type_die == NULL)
14219 return NULL;
14220 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14221 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14222 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14223 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14224 add_loc_descr (&op0, cvt);
14225 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14226 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14227 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14228 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14229 add_loc_descr (&op1, cvt);
14230 return compare_loc_descriptor (op, op0, op1);
14233 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14234 /* For eq/ne, if the operands are known to be zero-extended,
14235 there is no need to do the fancy shifting up. */
14236 if (op == DW_OP_eq || op == DW_OP_ne)
14238 dw_loc_descr_ref last0, last1;
14239 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14241 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14243 /* deref_size zero extends, and for constants we can check
14244 whether they are zero extended or not. */
14245 if (((last0->dw_loc_opc == DW_OP_deref_size
14246 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14247 || (CONST_INT_P (XEXP (rtl, 0))
14248 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14249 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14250 && ((last1->dw_loc_opc == DW_OP_deref_size
14251 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14252 || (CONST_INT_P (XEXP (rtl, 1))
14253 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14254 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14255 return compare_loc_descriptor (op, op0, op1);
14257 /* EQ/NE comparison against constant in narrower type than
14258 DWARF2_ADDR_SIZE can be performed either as
14259 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14260 DW_OP_{eq,ne}
14262 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14263 DW_OP_{eq,ne}. Pick whatever is shorter. */
14264 if (CONST_INT_P (XEXP (rtl, 1))
14265 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14266 && (size_of_int_loc_descriptor (shift) + 1
14267 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14268 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14269 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14270 & GET_MODE_MASK (op_mode))))
14272 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14273 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14274 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14275 & GET_MODE_MASK (op_mode));
14276 return compare_loc_descriptor (op, op0, op1);
14279 add_loc_descr (&op0, int_loc_descriptor (shift));
14280 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14281 if (CONST_INT_P (XEXP (rtl, 1)))
14282 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14283 else
14285 add_loc_descr (&op1, int_loc_descriptor (shift));
14286 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14288 return compare_loc_descriptor (op, op0, op1);
14291 /* Return location descriptor for unsigned comparison OP RTL. */
14293 static dw_loc_descr_ref
14294 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14295 machine_mode mem_mode)
14297 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14298 dw_loc_descr_ref op0, op1;
14300 if (op_mode == VOIDmode)
14301 op_mode = GET_MODE (XEXP (rtl, 1));
14302 if (op_mode == VOIDmode)
14303 return NULL;
14304 if (!SCALAR_INT_MODE_P (op_mode))
14305 return NULL;
14307 if (dwarf_strict
14308 && dwarf_version < 5
14309 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14310 return NULL;
14312 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14313 VAR_INIT_STATUS_INITIALIZED);
14314 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14315 VAR_INIT_STATUS_INITIALIZED);
14317 if (op0 == NULL || op1 == NULL)
14318 return NULL;
14320 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14322 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14323 dw_loc_descr_ref last0, last1;
14324 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14326 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14328 if (CONST_INT_P (XEXP (rtl, 0)))
14329 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14330 /* deref_size zero extends, so no need to mask it again. */
14331 else if (last0->dw_loc_opc != DW_OP_deref_size
14332 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14334 add_loc_descr (&op0, int_loc_descriptor (mask));
14335 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14337 if (CONST_INT_P (XEXP (rtl, 1)))
14338 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14339 /* deref_size zero extends, so no need to mask it again. */
14340 else if (last1->dw_loc_opc != DW_OP_deref_size
14341 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14343 add_loc_descr (&op1, int_loc_descriptor (mask));
14344 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14347 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14349 HOST_WIDE_INT bias = 1;
14350 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14351 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14352 if (CONST_INT_P (XEXP (rtl, 1)))
14353 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14354 + INTVAL (XEXP (rtl, 1)));
14355 else
14356 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14357 bias, 0));
14359 return compare_loc_descriptor (op, op0, op1);
14362 /* Return location descriptor for {U,S}{MIN,MAX}. */
14364 static dw_loc_descr_ref
14365 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14366 machine_mode mem_mode)
14368 enum dwarf_location_atom op;
14369 dw_loc_descr_ref op0, op1, ret;
14370 dw_loc_descr_ref bra_node, drop_node;
14372 if (dwarf_strict
14373 && dwarf_version < 5
14374 && (!SCALAR_INT_MODE_P (mode)
14375 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14376 return NULL;
14378 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14379 VAR_INIT_STATUS_INITIALIZED);
14380 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14381 VAR_INIT_STATUS_INITIALIZED);
14383 if (op0 == NULL || op1 == NULL)
14384 return NULL;
14386 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14387 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14388 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14389 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14391 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14393 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14394 add_loc_descr (&op0, int_loc_descriptor (mask));
14395 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14396 add_loc_descr (&op1, int_loc_descriptor (mask));
14397 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14399 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14401 HOST_WIDE_INT bias = 1;
14402 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14403 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14404 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14407 else if (!SCALAR_INT_MODE_P (mode)
14408 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14410 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14411 add_loc_descr (&op0, int_loc_descriptor (shift));
14412 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14413 add_loc_descr (&op1, int_loc_descriptor (shift));
14414 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14416 else if (SCALAR_INT_MODE_P (mode)
14417 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14419 dw_die_ref type_die = base_type_for_mode (mode, 0);
14420 dw_loc_descr_ref cvt;
14421 if (type_die == NULL)
14422 return NULL;
14423 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14424 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14425 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14426 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14427 add_loc_descr (&op0, cvt);
14428 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14429 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14430 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14431 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14432 add_loc_descr (&op1, cvt);
14435 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14436 op = DW_OP_lt;
14437 else
14438 op = DW_OP_gt;
14439 ret = op0;
14440 add_loc_descr (&ret, op1);
14441 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14442 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14443 add_loc_descr (&ret, bra_node);
14444 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14445 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14446 add_loc_descr (&ret, drop_node);
14447 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14448 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14449 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14450 && SCALAR_INT_MODE_P (mode)
14451 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14452 ret = convert_descriptor_to_mode (mode, ret);
14453 return ret;
14456 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14457 but after converting arguments to type_die, afterwards
14458 convert back to unsigned. */
14460 static dw_loc_descr_ref
14461 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14462 machine_mode mode, machine_mode mem_mode)
14464 dw_loc_descr_ref cvt, op0, op1;
14466 if (type_die == NULL)
14467 return NULL;
14468 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14469 VAR_INIT_STATUS_INITIALIZED);
14470 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14471 VAR_INIT_STATUS_INITIALIZED);
14472 if (op0 == NULL || op1 == NULL)
14473 return NULL;
14474 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14475 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14476 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14477 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14478 add_loc_descr (&op0, cvt);
14479 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14480 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14481 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14482 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14483 add_loc_descr (&op1, cvt);
14484 add_loc_descr (&op0, op1);
14485 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14486 return convert_descriptor_to_mode (mode, op0);
14489 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14490 const0 is DW_OP_lit0 or corresponding typed constant,
14491 const1 is DW_OP_lit1 or corresponding typed constant
14492 and constMSB is constant with just the MSB bit set
14493 for the mode):
14494 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14495 L1: const0 DW_OP_swap
14496 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14497 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14498 L3: DW_OP_drop
14499 L4: DW_OP_nop
14501 CTZ is similar:
14502 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14503 L1: const0 DW_OP_swap
14504 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14505 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14506 L3: DW_OP_drop
14507 L4: DW_OP_nop
14509 FFS is similar:
14510 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14511 L1: const1 DW_OP_swap
14512 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14513 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14514 L3: DW_OP_drop
14515 L4: DW_OP_nop */
14517 static dw_loc_descr_ref
14518 clz_loc_descriptor (rtx rtl, machine_mode mode,
14519 machine_mode mem_mode)
14521 dw_loc_descr_ref op0, ret, tmp;
14522 HOST_WIDE_INT valv;
14523 dw_loc_descr_ref l1jump, l1label;
14524 dw_loc_descr_ref l2jump, l2label;
14525 dw_loc_descr_ref l3jump, l3label;
14526 dw_loc_descr_ref l4jump, l4label;
14527 rtx msb;
14529 if (!SCALAR_INT_MODE_P (mode)
14530 || GET_MODE (XEXP (rtl, 0)) != mode)
14531 return NULL;
14533 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14534 VAR_INIT_STATUS_INITIALIZED);
14535 if (op0 == NULL)
14536 return NULL;
14537 ret = op0;
14538 if (GET_CODE (rtl) == CLZ)
14540 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14541 valv = GET_MODE_BITSIZE (mode);
14543 else if (GET_CODE (rtl) == FFS)
14544 valv = 0;
14545 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14546 valv = GET_MODE_BITSIZE (mode);
14547 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14548 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14549 add_loc_descr (&ret, l1jump);
14550 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14551 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14552 VAR_INIT_STATUS_INITIALIZED);
14553 if (tmp == NULL)
14554 return NULL;
14555 add_loc_descr (&ret, tmp);
14556 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14557 add_loc_descr (&ret, l4jump);
14558 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14559 ? const1_rtx : const0_rtx,
14560 mode, mem_mode,
14561 VAR_INIT_STATUS_INITIALIZED);
14562 if (l1label == NULL)
14563 return NULL;
14564 add_loc_descr (&ret, l1label);
14565 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14566 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14567 add_loc_descr (&ret, l2label);
14568 if (GET_CODE (rtl) != CLZ)
14569 msb = const1_rtx;
14570 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14571 msb = GEN_INT (HOST_WIDE_INT_1U
14572 << (GET_MODE_BITSIZE (mode) - 1));
14573 else
14574 msb = immed_wide_int_const
14575 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14576 GET_MODE_PRECISION (mode)), mode);
14577 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14578 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14579 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14580 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14581 else
14582 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14583 VAR_INIT_STATUS_INITIALIZED);
14584 if (tmp == NULL)
14585 return NULL;
14586 add_loc_descr (&ret, tmp);
14587 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14588 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14589 add_loc_descr (&ret, l3jump);
14590 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14591 VAR_INIT_STATUS_INITIALIZED);
14592 if (tmp == NULL)
14593 return NULL;
14594 add_loc_descr (&ret, tmp);
14595 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14596 ? DW_OP_shl : DW_OP_shr, 0, 0));
14597 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14598 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14599 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14600 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14601 add_loc_descr (&ret, l2jump);
14602 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14603 add_loc_descr (&ret, l3label);
14604 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14605 add_loc_descr (&ret, l4label);
14606 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14607 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14608 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14609 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14610 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14611 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14612 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14613 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14614 return ret;
14617 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14618 const1 is DW_OP_lit1 or corresponding typed constant):
14619 const0 DW_OP_swap
14620 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14621 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14622 L2: DW_OP_drop
14624 PARITY is similar:
14625 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14626 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14627 L2: DW_OP_drop */
14629 static dw_loc_descr_ref
14630 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14631 machine_mode mem_mode)
14633 dw_loc_descr_ref op0, ret, tmp;
14634 dw_loc_descr_ref l1jump, l1label;
14635 dw_loc_descr_ref l2jump, l2label;
14637 if (!SCALAR_INT_MODE_P (mode)
14638 || GET_MODE (XEXP (rtl, 0)) != mode)
14639 return NULL;
14641 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14642 VAR_INIT_STATUS_INITIALIZED);
14643 if (op0 == NULL)
14644 return NULL;
14645 ret = op0;
14646 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14647 VAR_INIT_STATUS_INITIALIZED);
14648 if (tmp == NULL)
14649 return NULL;
14650 add_loc_descr (&ret, tmp);
14651 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14652 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14653 add_loc_descr (&ret, l1label);
14654 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14655 add_loc_descr (&ret, l2jump);
14656 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14657 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14658 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14659 VAR_INIT_STATUS_INITIALIZED);
14660 if (tmp == NULL)
14661 return NULL;
14662 add_loc_descr (&ret, tmp);
14663 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14664 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14665 ? DW_OP_plus : DW_OP_xor, 0, 0));
14666 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14667 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14668 VAR_INIT_STATUS_INITIALIZED);
14669 add_loc_descr (&ret, tmp);
14670 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14671 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14672 add_loc_descr (&ret, l1jump);
14673 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14674 add_loc_descr (&ret, l2label);
14675 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14676 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14677 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14678 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14679 return ret;
14682 /* BSWAP (constS is initial shift count, either 56 or 24):
14683 constS const0
14684 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14685 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14686 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14687 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14688 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14690 static dw_loc_descr_ref
14691 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14692 machine_mode mem_mode)
14694 dw_loc_descr_ref op0, ret, tmp;
14695 dw_loc_descr_ref l1jump, l1label;
14696 dw_loc_descr_ref l2jump, l2label;
14698 if (!SCALAR_INT_MODE_P (mode)
14699 || BITS_PER_UNIT != 8
14700 || (GET_MODE_BITSIZE (mode) != 32
14701 && GET_MODE_BITSIZE (mode) != 64))
14702 return NULL;
14704 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14705 VAR_INIT_STATUS_INITIALIZED);
14706 if (op0 == NULL)
14707 return NULL;
14709 ret = op0;
14710 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14711 mode, mem_mode,
14712 VAR_INIT_STATUS_INITIALIZED);
14713 if (tmp == NULL)
14714 return NULL;
14715 add_loc_descr (&ret, tmp);
14716 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14717 VAR_INIT_STATUS_INITIALIZED);
14718 if (tmp == NULL)
14719 return NULL;
14720 add_loc_descr (&ret, tmp);
14721 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14722 add_loc_descr (&ret, l1label);
14723 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14724 mode, mem_mode,
14725 VAR_INIT_STATUS_INITIALIZED);
14726 add_loc_descr (&ret, tmp);
14727 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14728 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14729 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14730 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14731 VAR_INIT_STATUS_INITIALIZED);
14732 if (tmp == NULL)
14733 return NULL;
14734 add_loc_descr (&ret, tmp);
14735 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14736 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14737 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14738 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14739 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14740 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14741 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14742 VAR_INIT_STATUS_INITIALIZED);
14743 add_loc_descr (&ret, tmp);
14744 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14745 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14746 add_loc_descr (&ret, l2jump);
14747 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14748 VAR_INIT_STATUS_INITIALIZED);
14749 add_loc_descr (&ret, tmp);
14750 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14751 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14752 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14753 add_loc_descr (&ret, l1jump);
14754 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14755 add_loc_descr (&ret, l2label);
14756 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14757 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14758 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14759 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14760 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14761 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14762 return ret;
14765 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14766 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14767 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14768 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14770 ROTATERT is similar:
14771 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14772 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14773 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14775 static dw_loc_descr_ref
14776 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14777 machine_mode mem_mode)
14779 rtx rtlop1 = XEXP (rtl, 1);
14780 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14781 int i;
14783 if (!SCALAR_INT_MODE_P (mode))
14784 return NULL;
14786 if (GET_MODE (rtlop1) != VOIDmode
14787 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14788 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14789 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14790 VAR_INIT_STATUS_INITIALIZED);
14791 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14792 VAR_INIT_STATUS_INITIALIZED);
14793 if (op0 == NULL || op1 == NULL)
14794 return NULL;
14795 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14796 for (i = 0; i < 2; i++)
14798 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14799 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14800 mode, mem_mode,
14801 VAR_INIT_STATUS_INITIALIZED);
14802 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14803 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14804 ? DW_OP_const4u
14805 : HOST_BITS_PER_WIDE_INT == 64
14806 ? DW_OP_const8u : DW_OP_constu,
14807 GET_MODE_MASK (mode), 0);
14808 else
14809 mask[i] = NULL;
14810 if (mask[i] == NULL)
14811 return NULL;
14812 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14814 ret = op0;
14815 add_loc_descr (&ret, op1);
14816 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14817 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14818 if (GET_CODE (rtl) == ROTATERT)
14820 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14821 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14822 GET_MODE_BITSIZE (mode), 0));
14824 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14825 if (mask[0] != NULL)
14826 add_loc_descr (&ret, mask[0]);
14827 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14828 if (mask[1] != NULL)
14830 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14831 add_loc_descr (&ret, mask[1]);
14832 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14834 if (GET_CODE (rtl) == ROTATE)
14836 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14837 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14838 GET_MODE_BITSIZE (mode), 0));
14840 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14841 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14842 return ret;
14845 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14846 for DEBUG_PARAMETER_REF RTL. */
14848 static dw_loc_descr_ref
14849 parameter_ref_descriptor (rtx rtl)
14851 dw_loc_descr_ref ret;
14852 dw_die_ref ref;
14854 if (dwarf_strict)
14855 return NULL;
14856 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14857 /* With LTO during LTRANS we get the late DIE that refers to the early
14858 DIE, thus we add another indirection here. This seems to confuse
14859 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14860 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14861 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14862 if (ref)
14864 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14865 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14866 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14868 else
14870 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14871 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14873 return ret;
14876 /* The following routine converts the RTL for a variable or parameter
14877 (resident in memory) into an equivalent Dwarf representation of a
14878 mechanism for getting the address of that same variable onto the top of a
14879 hypothetical "address evaluation" stack.
14881 When creating memory location descriptors, we are effectively transforming
14882 the RTL for a memory-resident object into its Dwarf postfix expression
14883 equivalent. This routine recursively descends an RTL tree, turning
14884 it into Dwarf postfix code as it goes.
14886 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14888 MEM_MODE is the mode of the memory reference, needed to handle some
14889 autoincrement addressing modes.
14891 Return 0 if we can't represent the location. */
14893 dw_loc_descr_ref
14894 mem_loc_descriptor (rtx rtl, machine_mode mode,
14895 machine_mode mem_mode,
14896 enum var_init_status initialized)
14898 dw_loc_descr_ref mem_loc_result = NULL;
14899 enum dwarf_location_atom op;
14900 dw_loc_descr_ref op0, op1;
14901 rtx inner = NULL_RTX;
14903 if (mode == VOIDmode)
14904 mode = GET_MODE (rtl);
14906 /* Note that for a dynamically sized array, the location we will generate a
14907 description of here will be the lowest numbered location which is
14908 actually within the array. That's *not* necessarily the same as the
14909 zeroth element of the array. */
14911 rtl = targetm.delegitimize_address (rtl);
14913 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14914 return NULL;
14916 switch (GET_CODE (rtl))
14918 case POST_INC:
14919 case POST_DEC:
14920 case POST_MODIFY:
14921 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14923 case SUBREG:
14924 /* The case of a subreg may arise when we have a local (register)
14925 variable or a formal (register) parameter which doesn't quite fill
14926 up an entire register. For now, just assume that it is
14927 legitimate to make the Dwarf info refer to the whole register which
14928 contains the given subreg. */
14929 if (!subreg_lowpart_p (rtl))
14930 break;
14931 inner = SUBREG_REG (rtl);
14932 /* FALLTHRU */
14933 case TRUNCATE:
14934 if (inner == NULL_RTX)
14935 inner = XEXP (rtl, 0);
14936 if (SCALAR_INT_MODE_P (mode)
14937 && SCALAR_INT_MODE_P (GET_MODE (inner))
14938 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14939 #ifdef POINTERS_EXTEND_UNSIGNED
14940 || (mode == Pmode && mem_mode != VOIDmode)
14941 #endif
14943 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14945 mem_loc_result = mem_loc_descriptor (inner,
14946 GET_MODE (inner),
14947 mem_mode, initialized);
14948 break;
14950 if (dwarf_strict && dwarf_version < 5)
14951 break;
14952 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14953 break;
14954 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14955 && (!SCALAR_INT_MODE_P (mode)
14956 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14957 break;
14958 else
14960 dw_die_ref type_die;
14961 dw_loc_descr_ref cvt;
14963 mem_loc_result = mem_loc_descriptor (inner,
14964 GET_MODE (inner),
14965 mem_mode, initialized);
14966 if (mem_loc_result == NULL)
14967 break;
14968 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14969 if (type_die == NULL)
14971 mem_loc_result = NULL;
14972 break;
14974 if (GET_MODE_SIZE (mode)
14975 != GET_MODE_SIZE (GET_MODE (inner)))
14976 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14977 else
14978 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14979 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14980 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14981 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14982 add_loc_descr (&mem_loc_result, cvt);
14983 if (SCALAR_INT_MODE_P (mode)
14984 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14986 /* Convert it to untyped afterwards. */
14987 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14988 add_loc_descr (&mem_loc_result, cvt);
14991 break;
14993 case REG:
14994 if (! SCALAR_INT_MODE_P (mode)
14995 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14996 && rtl != arg_pointer_rtx
14997 && rtl != frame_pointer_rtx
14998 #ifdef POINTERS_EXTEND_UNSIGNED
14999 && (mode != Pmode || mem_mode == VOIDmode)
15000 #endif
15003 dw_die_ref type_die;
15004 unsigned int dbx_regnum;
15006 if (dwarf_strict && dwarf_version < 5)
15007 break;
15008 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
15009 break;
15010 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15011 if (type_die == NULL)
15012 break;
15014 dbx_regnum = dbx_reg_number (rtl);
15015 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15016 break;
15017 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15018 dbx_regnum, 0);
15019 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15020 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15021 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15022 break;
15024 /* Whenever a register number forms a part of the description of the
15025 method for calculating the (dynamic) address of a memory resident
15026 object, DWARF rules require the register number be referred to as
15027 a "base register". This distinction is not based in any way upon
15028 what category of register the hardware believes the given register
15029 belongs to. This is strictly DWARF terminology we're dealing with
15030 here. Note that in cases where the location of a memory-resident
15031 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15032 OP_CONST (0)) the actual DWARF location descriptor that we generate
15033 may just be OP_BASEREG (basereg). This may look deceptively like
15034 the object in question was allocated to a register (rather than in
15035 memory) so DWARF consumers need to be aware of the subtle
15036 distinction between OP_REG and OP_BASEREG. */
15037 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15038 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15039 else if (stack_realign_drap
15040 && crtl->drap_reg
15041 && crtl->args.internal_arg_pointer == rtl
15042 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15044 /* If RTL is internal_arg_pointer, which has been optimized
15045 out, use DRAP instead. */
15046 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15047 VAR_INIT_STATUS_INITIALIZED);
15049 break;
15051 case SIGN_EXTEND:
15052 case ZERO_EXTEND:
15053 if (!SCALAR_INT_MODE_P (mode))
15054 break;
15055 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15056 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15057 if (op0 == 0)
15058 break;
15059 else if (GET_CODE (rtl) == ZERO_EXTEND
15060 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15061 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15062 < HOST_BITS_PER_WIDE_INT
15063 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15064 to expand zero extend as two shifts instead of
15065 masking. */
15066 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
15068 machine_mode imode = GET_MODE (XEXP (rtl, 0));
15069 mem_loc_result = op0;
15070 add_loc_descr (&mem_loc_result,
15071 int_loc_descriptor (GET_MODE_MASK (imode)));
15072 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15074 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15076 int shift = DWARF2_ADDR_SIZE
15077 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
15078 shift *= BITS_PER_UNIT;
15079 if (GET_CODE (rtl) == SIGN_EXTEND)
15080 op = DW_OP_shra;
15081 else
15082 op = DW_OP_shr;
15083 mem_loc_result = op0;
15084 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15085 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15086 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15087 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15089 else if (!dwarf_strict || dwarf_version >= 5)
15091 dw_die_ref type_die1, type_die2;
15092 dw_loc_descr_ref cvt;
15094 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15095 GET_CODE (rtl) == ZERO_EXTEND);
15096 if (type_die1 == NULL)
15097 break;
15098 type_die2 = base_type_for_mode (mode, 1);
15099 if (type_die2 == NULL)
15100 break;
15101 mem_loc_result = op0;
15102 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15103 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15104 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15105 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15106 add_loc_descr (&mem_loc_result, cvt);
15107 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15108 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15109 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15110 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15111 add_loc_descr (&mem_loc_result, cvt);
15113 break;
15115 case MEM:
15117 rtx new_rtl = avoid_constant_pool_reference (rtl);
15118 if (new_rtl != rtl)
15120 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15121 initialized);
15122 if (mem_loc_result != NULL)
15123 return mem_loc_result;
15126 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15127 get_address_mode (rtl), mode,
15128 VAR_INIT_STATUS_INITIALIZED);
15129 if (mem_loc_result == NULL)
15130 mem_loc_result = tls_mem_loc_descriptor (rtl);
15131 if (mem_loc_result != NULL)
15133 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15134 || !SCALAR_INT_MODE_P(mode))
15136 dw_die_ref type_die;
15137 dw_loc_descr_ref deref;
15139 if (dwarf_strict && dwarf_version < 5)
15140 return NULL;
15141 type_die
15142 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15143 if (type_die == NULL)
15144 return NULL;
15145 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
15146 GET_MODE_SIZE (mode), 0);
15147 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15148 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15149 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15150 add_loc_descr (&mem_loc_result, deref);
15152 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
15153 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15154 else
15155 add_loc_descr (&mem_loc_result,
15156 new_loc_descr (DW_OP_deref_size,
15157 GET_MODE_SIZE (mode), 0));
15159 break;
15161 case LO_SUM:
15162 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15164 case LABEL_REF:
15165 /* Some ports can transform a symbol ref into a label ref, because
15166 the symbol ref is too far away and has to be dumped into a constant
15167 pool. */
15168 case CONST:
15169 case SYMBOL_REF:
15170 if (!SCALAR_INT_MODE_P (mode)
15171 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15172 #ifdef POINTERS_EXTEND_UNSIGNED
15173 && (mode != Pmode || mem_mode == VOIDmode)
15174 #endif
15176 break;
15177 if (GET_CODE (rtl) == SYMBOL_REF
15178 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15180 dw_loc_descr_ref temp;
15182 /* If this is not defined, we have no way to emit the data. */
15183 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15184 break;
15186 temp = new_addr_loc_descr (rtl, dtprel_true);
15188 /* We check for DWARF 5 here because gdb did not implement
15189 DW_OP_form_tls_address until after 7.12. */
15190 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15191 ? DW_OP_form_tls_address
15192 : DW_OP_GNU_push_tls_address),
15193 0, 0);
15194 add_loc_descr (&mem_loc_result, temp);
15196 break;
15199 if (!const_ok_for_output (rtl))
15201 if (GET_CODE (rtl) == CONST)
15202 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15203 initialized);
15204 break;
15207 symref:
15208 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15209 vec_safe_push (used_rtx_array, rtl);
15210 break;
15212 case CONCAT:
15213 case CONCATN:
15214 case VAR_LOCATION:
15215 case DEBUG_IMPLICIT_PTR:
15216 expansion_failed (NULL_TREE, rtl,
15217 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15218 return 0;
15220 case ENTRY_VALUE:
15221 if (dwarf_strict && dwarf_version < 5)
15222 return NULL;
15223 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15225 if (!SCALAR_INT_MODE_P (mode)
15226 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15227 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15228 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15229 else
15231 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15232 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15233 return NULL;
15234 op0 = one_reg_loc_descriptor (dbx_regnum,
15235 VAR_INIT_STATUS_INITIALIZED);
15238 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15239 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15241 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15242 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15243 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15244 return NULL;
15246 else
15247 gcc_unreachable ();
15248 if (op0 == NULL)
15249 return NULL;
15250 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15251 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15252 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15253 break;
15255 case DEBUG_PARAMETER_REF:
15256 mem_loc_result = parameter_ref_descriptor (rtl);
15257 break;
15259 case PRE_MODIFY:
15260 /* Extract the PLUS expression nested inside and fall into
15261 PLUS code below. */
15262 rtl = XEXP (rtl, 1);
15263 goto plus;
15265 case PRE_INC:
15266 case PRE_DEC:
15267 /* Turn these into a PLUS expression and fall into the PLUS code
15268 below. */
15269 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15270 gen_int_mode (GET_CODE (rtl) == PRE_INC
15271 ? GET_MODE_UNIT_SIZE (mem_mode)
15272 : -GET_MODE_UNIT_SIZE (mem_mode),
15273 mode));
15275 /* fall through */
15277 case PLUS:
15278 plus:
15279 if (is_based_loc (rtl)
15280 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15281 || XEXP (rtl, 0) == arg_pointer_rtx
15282 || XEXP (rtl, 0) == frame_pointer_rtx)
15283 && SCALAR_INT_MODE_P (mode))
15284 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15285 INTVAL (XEXP (rtl, 1)),
15286 VAR_INIT_STATUS_INITIALIZED);
15287 else
15289 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15290 VAR_INIT_STATUS_INITIALIZED);
15291 if (mem_loc_result == 0)
15292 break;
15294 if (CONST_INT_P (XEXP (rtl, 1))
15295 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15296 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15297 else
15299 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15300 VAR_INIT_STATUS_INITIALIZED);
15301 if (op1 == 0)
15302 return NULL;
15303 add_loc_descr (&mem_loc_result, op1);
15304 add_loc_descr (&mem_loc_result,
15305 new_loc_descr (DW_OP_plus, 0, 0));
15308 break;
15310 /* If a pseudo-reg is optimized away, it is possible for it to
15311 be replaced with a MEM containing a multiply or shift. */
15312 case MINUS:
15313 op = DW_OP_minus;
15314 goto do_binop;
15316 case MULT:
15317 op = DW_OP_mul;
15318 goto do_binop;
15320 case DIV:
15321 if ((!dwarf_strict || dwarf_version >= 5)
15322 && SCALAR_INT_MODE_P (mode)
15323 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15325 mem_loc_result = typed_binop (DW_OP_div, rtl,
15326 base_type_for_mode (mode, 0),
15327 mode, mem_mode);
15328 break;
15330 op = DW_OP_div;
15331 goto do_binop;
15333 case UMOD:
15334 op = DW_OP_mod;
15335 goto do_binop;
15337 case ASHIFT:
15338 op = DW_OP_shl;
15339 goto do_shift;
15341 case ASHIFTRT:
15342 op = DW_OP_shra;
15343 goto do_shift;
15345 case LSHIFTRT:
15346 op = DW_OP_shr;
15347 goto do_shift;
15349 do_shift:
15350 if (!SCALAR_INT_MODE_P (mode))
15351 break;
15352 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15353 VAR_INIT_STATUS_INITIALIZED);
15355 rtx rtlop1 = XEXP (rtl, 1);
15356 if (GET_MODE (rtlop1) != VOIDmode
15357 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15358 < GET_MODE_BITSIZE (mode))
15359 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15360 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15361 VAR_INIT_STATUS_INITIALIZED);
15364 if (op0 == 0 || op1 == 0)
15365 break;
15367 mem_loc_result = op0;
15368 add_loc_descr (&mem_loc_result, op1);
15369 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15370 break;
15372 case AND:
15373 op = DW_OP_and;
15374 goto do_binop;
15376 case IOR:
15377 op = DW_OP_or;
15378 goto do_binop;
15380 case XOR:
15381 op = DW_OP_xor;
15382 goto do_binop;
15384 do_binop:
15385 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15386 VAR_INIT_STATUS_INITIALIZED);
15387 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15388 VAR_INIT_STATUS_INITIALIZED);
15390 if (op0 == 0 || op1 == 0)
15391 break;
15393 mem_loc_result = op0;
15394 add_loc_descr (&mem_loc_result, op1);
15395 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15396 break;
15398 case MOD:
15399 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15400 && (!dwarf_strict || dwarf_version >= 5))
15402 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15403 base_type_for_mode (mode, 0),
15404 mode, mem_mode);
15405 break;
15408 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15409 VAR_INIT_STATUS_INITIALIZED);
15410 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15411 VAR_INIT_STATUS_INITIALIZED);
15413 if (op0 == 0 || op1 == 0)
15414 break;
15416 mem_loc_result = op0;
15417 add_loc_descr (&mem_loc_result, op1);
15418 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15419 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15420 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15421 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15422 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15423 break;
15425 case UDIV:
15426 if ((!dwarf_strict || dwarf_version >= 5)
15427 && SCALAR_INT_MODE_P (mode))
15429 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15431 op = DW_OP_div;
15432 goto do_binop;
15434 mem_loc_result = typed_binop (DW_OP_div, rtl,
15435 base_type_for_mode (mode, 1),
15436 mode, mem_mode);
15438 break;
15440 case NOT:
15441 op = DW_OP_not;
15442 goto do_unop;
15444 case ABS:
15445 op = DW_OP_abs;
15446 goto do_unop;
15448 case NEG:
15449 op = DW_OP_neg;
15450 goto do_unop;
15452 do_unop:
15453 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15454 VAR_INIT_STATUS_INITIALIZED);
15456 if (op0 == 0)
15457 break;
15459 mem_loc_result = op0;
15460 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15461 break;
15463 case CONST_INT:
15464 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15465 #ifdef POINTERS_EXTEND_UNSIGNED
15466 || (mode == Pmode
15467 && mem_mode != VOIDmode
15468 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15469 #endif
15472 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15473 break;
15475 if ((!dwarf_strict || dwarf_version >= 5)
15476 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15477 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15479 dw_die_ref type_die = base_type_for_mode (mode, 1);
15480 machine_mode amode;
15481 if (type_die == NULL)
15482 return NULL;
15483 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15484 MODE_INT, 0);
15485 if (INTVAL (rtl) >= 0
15486 && amode != BLKmode
15487 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15488 /* const DW_OP_convert <XXX> vs.
15489 DW_OP_const_type <XXX, 1, const>. */
15490 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15491 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15493 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15494 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15495 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15496 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15497 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15498 add_loc_descr (&mem_loc_result, op0);
15499 return mem_loc_result;
15501 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15502 INTVAL (rtl));
15503 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15504 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15505 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15506 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15507 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15508 else
15510 mem_loc_result->dw_loc_oprnd2.val_class
15511 = dw_val_class_const_double;
15512 mem_loc_result->dw_loc_oprnd2.v.val_double
15513 = double_int::from_shwi (INTVAL (rtl));
15516 break;
15518 case CONST_DOUBLE:
15519 if (!dwarf_strict || dwarf_version >= 5)
15521 dw_die_ref type_die;
15523 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15524 CONST_DOUBLE rtx could represent either a large integer
15525 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15526 the value is always a floating point constant.
15528 When it is an integer, a CONST_DOUBLE is used whenever
15529 the constant requires 2 HWIs to be adequately represented.
15530 We output CONST_DOUBLEs as blocks. */
15531 if (mode == VOIDmode
15532 || (GET_MODE (rtl) == VOIDmode
15533 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15534 break;
15535 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15536 if (type_die == NULL)
15537 return NULL;
15538 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15539 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15540 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15541 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15542 #if TARGET_SUPPORTS_WIDE_INT == 0
15543 if (!SCALAR_FLOAT_MODE_P (mode))
15545 mem_loc_result->dw_loc_oprnd2.val_class
15546 = dw_val_class_const_double;
15547 mem_loc_result->dw_loc_oprnd2.v.val_double
15548 = rtx_to_double_int (rtl);
15550 else
15551 #endif
15553 unsigned int length = GET_MODE_SIZE (mode);
15554 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15556 insert_float (rtl, array);
15557 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15558 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15559 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15560 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15563 break;
15565 case CONST_WIDE_INT:
15566 if (!dwarf_strict || dwarf_version >= 5)
15568 dw_die_ref type_die;
15570 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15571 if (type_die == NULL)
15572 return NULL;
15573 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15574 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15575 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15576 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15577 mem_loc_result->dw_loc_oprnd2.val_class
15578 = dw_val_class_wide_int;
15579 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15580 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15582 break;
15584 case EQ:
15585 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15586 break;
15588 case GE:
15589 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15590 break;
15592 case GT:
15593 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15594 break;
15596 case LE:
15597 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15598 break;
15600 case LT:
15601 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15602 break;
15604 case NE:
15605 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15606 break;
15608 case GEU:
15609 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15610 break;
15612 case GTU:
15613 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15614 break;
15616 case LEU:
15617 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15618 break;
15620 case LTU:
15621 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15622 break;
15624 case UMIN:
15625 case UMAX:
15626 if (!SCALAR_INT_MODE_P (mode))
15627 break;
15628 /* FALLTHRU */
15629 case SMIN:
15630 case SMAX:
15631 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15632 break;
15634 case ZERO_EXTRACT:
15635 case SIGN_EXTRACT:
15636 if (CONST_INT_P (XEXP (rtl, 1))
15637 && CONST_INT_P (XEXP (rtl, 2))
15638 && ((unsigned) INTVAL (XEXP (rtl, 1))
15639 + (unsigned) INTVAL (XEXP (rtl, 2))
15640 <= GET_MODE_BITSIZE (mode))
15641 && SCALAR_INT_MODE_P (mode)
15642 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15643 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15645 int shift, size;
15646 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15647 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15648 if (op0 == 0)
15649 break;
15650 if (GET_CODE (rtl) == SIGN_EXTRACT)
15651 op = DW_OP_shra;
15652 else
15653 op = DW_OP_shr;
15654 mem_loc_result = op0;
15655 size = INTVAL (XEXP (rtl, 1));
15656 shift = INTVAL (XEXP (rtl, 2));
15657 if (BITS_BIG_ENDIAN)
15658 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15659 - shift - size;
15660 if (shift + size != (int) DWARF2_ADDR_SIZE)
15662 add_loc_descr (&mem_loc_result,
15663 int_loc_descriptor (DWARF2_ADDR_SIZE
15664 - shift - size));
15665 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15667 if (size != (int) DWARF2_ADDR_SIZE)
15669 add_loc_descr (&mem_loc_result,
15670 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15671 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15674 break;
15676 case IF_THEN_ELSE:
15678 dw_loc_descr_ref op2, bra_node, drop_node;
15679 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15680 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15681 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15682 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15683 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15684 VAR_INIT_STATUS_INITIALIZED);
15685 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15686 VAR_INIT_STATUS_INITIALIZED);
15687 if (op0 == NULL || op1 == NULL || op2 == NULL)
15688 break;
15690 mem_loc_result = op1;
15691 add_loc_descr (&mem_loc_result, op2);
15692 add_loc_descr (&mem_loc_result, op0);
15693 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15694 add_loc_descr (&mem_loc_result, bra_node);
15695 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15696 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15697 add_loc_descr (&mem_loc_result, drop_node);
15698 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15699 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15701 break;
15703 case FLOAT_EXTEND:
15704 case FLOAT_TRUNCATE:
15705 case FLOAT:
15706 case UNSIGNED_FLOAT:
15707 case FIX:
15708 case UNSIGNED_FIX:
15709 if (!dwarf_strict || dwarf_version >= 5)
15711 dw_die_ref type_die;
15712 dw_loc_descr_ref cvt;
15714 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15715 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15716 if (op0 == NULL)
15717 break;
15718 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15719 && (GET_CODE (rtl) == FLOAT
15720 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15721 <= DWARF2_ADDR_SIZE))
15723 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15724 GET_CODE (rtl) == UNSIGNED_FLOAT);
15725 if (type_die == NULL)
15726 break;
15727 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15728 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15729 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15730 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15731 add_loc_descr (&op0, cvt);
15733 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15734 if (type_die == NULL)
15735 break;
15736 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15737 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15738 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15739 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15740 add_loc_descr (&op0, cvt);
15741 if (SCALAR_INT_MODE_P (mode)
15742 && (GET_CODE (rtl) == FIX
15743 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15745 op0 = convert_descriptor_to_mode (mode, op0);
15746 if (op0 == NULL)
15747 break;
15749 mem_loc_result = op0;
15751 break;
15753 case CLZ:
15754 case CTZ:
15755 case FFS:
15756 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15757 break;
15759 case POPCOUNT:
15760 case PARITY:
15761 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15762 break;
15764 case BSWAP:
15765 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15766 break;
15768 case ROTATE:
15769 case ROTATERT:
15770 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15771 break;
15773 case COMPARE:
15774 /* In theory, we could implement the above. */
15775 /* DWARF cannot represent the unsigned compare operations
15776 natively. */
15777 case SS_MULT:
15778 case US_MULT:
15779 case SS_DIV:
15780 case US_DIV:
15781 case SS_PLUS:
15782 case US_PLUS:
15783 case SS_MINUS:
15784 case US_MINUS:
15785 case SS_NEG:
15786 case US_NEG:
15787 case SS_ABS:
15788 case SS_ASHIFT:
15789 case US_ASHIFT:
15790 case SS_TRUNCATE:
15791 case US_TRUNCATE:
15792 case UNORDERED:
15793 case ORDERED:
15794 case UNEQ:
15795 case UNGE:
15796 case UNGT:
15797 case UNLE:
15798 case UNLT:
15799 case LTGT:
15800 case FRACT_CONVERT:
15801 case UNSIGNED_FRACT_CONVERT:
15802 case SAT_FRACT:
15803 case UNSIGNED_SAT_FRACT:
15804 case SQRT:
15805 case ASM_OPERANDS:
15806 case VEC_MERGE:
15807 case VEC_SELECT:
15808 case VEC_CONCAT:
15809 case VEC_DUPLICATE:
15810 case UNSPEC:
15811 case HIGH:
15812 case FMA:
15813 case STRICT_LOW_PART:
15814 case CONST_VECTOR:
15815 case CONST_FIXED:
15816 case CLRSB:
15817 case CLOBBER:
15818 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15819 can't express it in the debug info. This can happen e.g. with some
15820 TLS UNSPECs. */
15821 break;
15823 case CONST_STRING:
15824 resolve_one_addr (&rtl);
15825 goto symref;
15827 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15828 the expression. An UNSPEC rtx represents a raw DWARF operation,
15829 new_loc_descr is called for it to build the operation directly.
15830 Otherwise mem_loc_descriptor is called recursively. */
15831 case PARALLEL:
15833 int index = 0;
15834 dw_loc_descr_ref exp_result = NULL;
15836 for (; index < XVECLEN (rtl, 0); index++)
15838 rtx elem = XVECEXP (rtl, 0, index);
15839 if (GET_CODE (elem) == UNSPEC)
15841 /* Each DWARF operation UNSPEC contain two operands, if
15842 one operand is not used for the operation, const0_rtx is
15843 passed. */
15844 gcc_assert (XVECLEN (elem, 0) == 2);
15846 HOST_WIDE_INT dw_op = XINT (elem, 1);
15847 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15848 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15849 exp_result
15850 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15851 oprnd2);
15853 else
15854 exp_result
15855 = mem_loc_descriptor (elem, mode, mem_mode,
15856 VAR_INIT_STATUS_INITIALIZED);
15858 if (!mem_loc_result)
15859 mem_loc_result = exp_result;
15860 else
15861 add_loc_descr (&mem_loc_result, exp_result);
15864 break;
15867 default:
15868 if (flag_checking)
15870 print_rtl (stderr, rtl);
15871 gcc_unreachable ();
15873 break;
15876 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15877 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15879 return mem_loc_result;
15882 /* Return a descriptor that describes the concatenation of two locations.
15883 This is typically a complex variable. */
15885 static dw_loc_descr_ref
15886 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15888 dw_loc_descr_ref cc_loc_result = NULL;
15889 dw_loc_descr_ref x0_ref
15890 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15891 dw_loc_descr_ref x1_ref
15892 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15894 if (x0_ref == 0 || x1_ref == 0)
15895 return 0;
15897 cc_loc_result = x0_ref;
15898 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15900 add_loc_descr (&cc_loc_result, x1_ref);
15901 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15903 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15904 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15906 return cc_loc_result;
15909 /* Return a descriptor that describes the concatenation of N
15910 locations. */
15912 static dw_loc_descr_ref
15913 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15915 unsigned int i;
15916 dw_loc_descr_ref cc_loc_result = NULL;
15917 unsigned int n = XVECLEN (concatn, 0);
15919 for (i = 0; i < n; ++i)
15921 dw_loc_descr_ref ref;
15922 rtx x = XVECEXP (concatn, 0, i);
15924 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15925 if (ref == NULL)
15926 return NULL;
15928 add_loc_descr (&cc_loc_result, ref);
15929 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15932 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15933 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15935 return cc_loc_result;
15938 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15939 for DEBUG_IMPLICIT_PTR RTL. */
15941 static dw_loc_descr_ref
15942 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15944 dw_loc_descr_ref ret;
15945 dw_die_ref ref;
15947 if (dwarf_strict && dwarf_version < 5)
15948 return NULL;
15949 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15950 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15951 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15952 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15953 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15954 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15955 if (ref)
15957 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15958 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15959 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15961 else
15963 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15964 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15966 return ret;
15969 /* Output a proper Dwarf location descriptor for a variable or parameter
15970 which is either allocated in a register or in a memory location. For a
15971 register, we just generate an OP_REG and the register number. For a
15972 memory location we provide a Dwarf postfix expression describing how to
15973 generate the (dynamic) address of the object onto the address stack.
15975 MODE is mode of the decl if this loc_descriptor is going to be used in
15976 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15977 allowed, VOIDmode otherwise.
15979 If we don't know how to describe it, return 0. */
15981 static dw_loc_descr_ref
15982 loc_descriptor (rtx rtl, machine_mode mode,
15983 enum var_init_status initialized)
15985 dw_loc_descr_ref loc_result = NULL;
15987 switch (GET_CODE (rtl))
15989 case SUBREG:
15990 /* The case of a subreg may arise when we have a local (register)
15991 variable or a formal (register) parameter which doesn't quite fill
15992 up an entire register. For now, just assume that it is
15993 legitimate to make the Dwarf info refer to the whole register which
15994 contains the given subreg. */
15995 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15996 loc_result = loc_descriptor (SUBREG_REG (rtl),
15997 GET_MODE (SUBREG_REG (rtl)), initialized);
15998 else
15999 goto do_default;
16000 break;
16002 case REG:
16003 loc_result = reg_loc_descriptor (rtl, initialized);
16004 break;
16006 case MEM:
16007 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16008 GET_MODE (rtl), initialized);
16009 if (loc_result == NULL)
16010 loc_result = tls_mem_loc_descriptor (rtl);
16011 if (loc_result == NULL)
16013 rtx new_rtl = avoid_constant_pool_reference (rtl);
16014 if (new_rtl != rtl)
16015 loc_result = loc_descriptor (new_rtl, mode, initialized);
16017 break;
16019 case CONCAT:
16020 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16021 initialized);
16022 break;
16024 case CONCATN:
16025 loc_result = concatn_loc_descriptor (rtl, initialized);
16026 break;
16028 case VAR_LOCATION:
16029 /* Single part. */
16030 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16032 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16033 if (GET_CODE (loc) == EXPR_LIST)
16034 loc = XEXP (loc, 0);
16035 loc_result = loc_descriptor (loc, mode, initialized);
16036 break;
16039 rtl = XEXP (rtl, 1);
16040 /* FALLTHRU */
16042 case PARALLEL:
16044 rtvec par_elems = XVEC (rtl, 0);
16045 int num_elem = GET_NUM_ELEM (par_elems);
16046 machine_mode mode;
16047 int i;
16049 /* Create the first one, so we have something to add to. */
16050 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16051 VOIDmode, initialized);
16052 if (loc_result == NULL)
16053 return NULL;
16054 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16055 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
16056 for (i = 1; i < num_elem; i++)
16058 dw_loc_descr_ref temp;
16060 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16061 VOIDmode, initialized);
16062 if (temp == NULL)
16063 return NULL;
16064 add_loc_descr (&loc_result, temp);
16065 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16066 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
16069 break;
16071 case CONST_INT:
16072 if (mode != VOIDmode && mode != BLKmode)
16073 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
16074 INTVAL (rtl));
16075 break;
16077 case CONST_DOUBLE:
16078 if (mode == VOIDmode)
16079 mode = GET_MODE (rtl);
16081 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16083 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16085 /* Note that a CONST_DOUBLE rtx could represent either an integer
16086 or a floating-point constant. A CONST_DOUBLE is used whenever
16087 the constant requires more than one word in order to be
16088 adequately represented. We output CONST_DOUBLEs as blocks. */
16089 loc_result = new_loc_descr (DW_OP_implicit_value,
16090 GET_MODE_SIZE (mode), 0);
16091 #if TARGET_SUPPORTS_WIDE_INT == 0
16092 if (!SCALAR_FLOAT_MODE_P (mode))
16094 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16095 loc_result->dw_loc_oprnd2.v.val_double
16096 = rtx_to_double_int (rtl);
16098 else
16099 #endif
16101 unsigned int length = GET_MODE_SIZE (mode);
16102 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16104 insert_float (rtl, array);
16105 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16106 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16107 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16108 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16111 break;
16113 case CONST_WIDE_INT:
16114 if (mode == VOIDmode)
16115 mode = GET_MODE (rtl);
16117 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16119 loc_result = new_loc_descr (DW_OP_implicit_value,
16120 GET_MODE_SIZE (mode), 0);
16121 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16122 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16123 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16125 break;
16127 case CONST_VECTOR:
16128 if (mode == VOIDmode)
16129 mode = GET_MODE (rtl);
16131 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16133 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16134 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16135 unsigned char *array
16136 = ggc_vec_alloc<unsigned char> (length * elt_size);
16137 unsigned int i;
16138 unsigned char *p;
16139 machine_mode imode = GET_MODE_INNER (mode);
16141 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16142 switch (GET_MODE_CLASS (mode))
16144 case MODE_VECTOR_INT:
16145 for (i = 0, p = array; i < length; i++, p += elt_size)
16147 rtx elt = CONST_VECTOR_ELT (rtl, i);
16148 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16150 break;
16152 case MODE_VECTOR_FLOAT:
16153 for (i = 0, p = array; i < length; i++, p += elt_size)
16155 rtx elt = CONST_VECTOR_ELT (rtl, i);
16156 insert_float (elt, p);
16158 break;
16160 default:
16161 gcc_unreachable ();
16164 loc_result = new_loc_descr (DW_OP_implicit_value,
16165 length * elt_size, 0);
16166 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16167 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16168 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16169 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16171 break;
16173 case CONST:
16174 if (mode == VOIDmode
16175 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16176 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16177 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16179 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16180 break;
16182 /* FALLTHROUGH */
16183 case SYMBOL_REF:
16184 if (!const_ok_for_output (rtl))
16185 break;
16186 /* FALLTHROUGH */
16187 case LABEL_REF:
16188 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
16189 && (dwarf_version >= 4 || !dwarf_strict))
16191 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16192 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16193 vec_safe_push (used_rtx_array, rtl);
16195 break;
16197 case DEBUG_IMPLICIT_PTR:
16198 loc_result = implicit_ptr_descriptor (rtl, 0);
16199 break;
16201 case PLUS:
16202 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16203 && CONST_INT_P (XEXP (rtl, 1)))
16205 loc_result
16206 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16207 break;
16209 /* FALLTHRU */
16210 do_default:
16211 default:
16212 if ((SCALAR_INT_MODE_P (mode)
16213 && GET_MODE (rtl) == mode
16214 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
16215 && dwarf_version >= 4)
16216 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16218 /* Value expression. */
16219 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16220 if (loc_result)
16221 add_loc_descr (&loc_result,
16222 new_loc_descr (DW_OP_stack_value, 0, 0));
16224 break;
16227 return loc_result;
16230 /* We need to figure out what section we should use as the base for the
16231 address ranges where a given location is valid.
16232 1. If this particular DECL has a section associated with it, use that.
16233 2. If this function has a section associated with it, use that.
16234 3. Otherwise, use the text section.
16235 XXX: If you split a variable across multiple sections, we won't notice. */
16237 static const char *
16238 secname_for_decl (const_tree decl)
16240 const char *secname;
16242 if (VAR_OR_FUNCTION_DECL_P (decl)
16243 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16244 && DECL_SECTION_NAME (decl))
16245 secname = DECL_SECTION_NAME (decl);
16246 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16247 secname = DECL_SECTION_NAME (current_function_decl);
16248 else if (cfun && in_cold_section_p)
16249 secname = crtl->subsections.cold_section_label;
16250 else
16251 secname = text_section_label;
16253 return secname;
16256 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16258 static bool
16259 decl_by_reference_p (tree decl)
16261 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16262 || VAR_P (decl))
16263 && DECL_BY_REFERENCE (decl));
16266 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16267 for VARLOC. */
16269 static dw_loc_descr_ref
16270 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16271 enum var_init_status initialized)
16273 int have_address = 0;
16274 dw_loc_descr_ref descr;
16275 machine_mode mode;
16277 if (want_address != 2)
16279 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16280 /* Single part. */
16281 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16283 varloc = PAT_VAR_LOCATION_LOC (varloc);
16284 if (GET_CODE (varloc) == EXPR_LIST)
16285 varloc = XEXP (varloc, 0);
16286 mode = GET_MODE (varloc);
16287 if (MEM_P (varloc))
16289 rtx addr = XEXP (varloc, 0);
16290 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16291 mode, initialized);
16292 if (descr)
16293 have_address = 1;
16294 else
16296 rtx x = avoid_constant_pool_reference (varloc);
16297 if (x != varloc)
16298 descr = mem_loc_descriptor (x, mode, VOIDmode,
16299 initialized);
16302 else
16303 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16305 else
16306 return 0;
16308 else
16310 if (GET_CODE (varloc) == VAR_LOCATION)
16311 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16312 else
16313 mode = DECL_MODE (loc);
16314 descr = loc_descriptor (varloc, mode, initialized);
16315 have_address = 1;
16318 if (!descr)
16319 return 0;
16321 if (want_address == 2 && !have_address
16322 && (dwarf_version >= 4 || !dwarf_strict))
16324 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16326 expansion_failed (loc, NULL_RTX,
16327 "DWARF address size mismatch");
16328 return 0;
16330 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16331 have_address = 1;
16333 /* Show if we can't fill the request for an address. */
16334 if (want_address && !have_address)
16336 expansion_failed (loc, NULL_RTX,
16337 "Want address and only have value");
16338 return 0;
16341 /* If we've got an address and don't want one, dereference. */
16342 if (!want_address && have_address)
16344 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16345 enum dwarf_location_atom op;
16347 if (size > DWARF2_ADDR_SIZE || size == -1)
16349 expansion_failed (loc, NULL_RTX,
16350 "DWARF address size mismatch");
16351 return 0;
16353 else if (size == DWARF2_ADDR_SIZE)
16354 op = DW_OP_deref;
16355 else
16356 op = DW_OP_deref_size;
16358 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16361 return descr;
16364 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16365 if it is not possible. */
16367 static dw_loc_descr_ref
16368 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16370 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16371 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16372 else if (dwarf_version >= 3 || !dwarf_strict)
16373 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16374 else
16375 return NULL;
16378 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16379 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16381 static dw_loc_descr_ref
16382 dw_sra_loc_expr (tree decl, rtx loc)
16384 rtx p;
16385 unsigned HOST_WIDE_INT padsize = 0;
16386 dw_loc_descr_ref descr, *descr_tail;
16387 unsigned HOST_WIDE_INT decl_size;
16388 rtx varloc;
16389 enum var_init_status initialized;
16391 if (DECL_SIZE (decl) == NULL
16392 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16393 return NULL;
16395 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16396 descr = NULL;
16397 descr_tail = &descr;
16399 for (p = loc; p; p = XEXP (p, 1))
16401 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16402 rtx loc_note = *decl_piece_varloc_ptr (p);
16403 dw_loc_descr_ref cur_descr;
16404 dw_loc_descr_ref *tail, last = NULL;
16405 unsigned HOST_WIDE_INT opsize = 0;
16407 if (loc_note == NULL_RTX
16408 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16410 padsize += bitsize;
16411 continue;
16413 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16414 varloc = NOTE_VAR_LOCATION (loc_note);
16415 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16416 if (cur_descr == NULL)
16418 padsize += bitsize;
16419 continue;
16422 /* Check that cur_descr either doesn't use
16423 DW_OP_*piece operations, or their sum is equal
16424 to bitsize. Otherwise we can't embed it. */
16425 for (tail = &cur_descr; *tail != NULL;
16426 tail = &(*tail)->dw_loc_next)
16427 if ((*tail)->dw_loc_opc == DW_OP_piece)
16429 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16430 * BITS_PER_UNIT;
16431 last = *tail;
16433 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16435 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16436 last = *tail;
16439 if (last != NULL && opsize != bitsize)
16441 padsize += bitsize;
16442 /* Discard the current piece of the descriptor and release any
16443 addr_table entries it uses. */
16444 remove_loc_list_addr_table_entries (cur_descr);
16445 continue;
16448 /* If there is a hole, add DW_OP_*piece after empty DWARF
16449 expression, which means that those bits are optimized out. */
16450 if (padsize)
16452 if (padsize > decl_size)
16454 remove_loc_list_addr_table_entries (cur_descr);
16455 goto discard_descr;
16457 decl_size -= padsize;
16458 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16459 if (*descr_tail == NULL)
16461 remove_loc_list_addr_table_entries (cur_descr);
16462 goto discard_descr;
16464 descr_tail = &(*descr_tail)->dw_loc_next;
16465 padsize = 0;
16467 *descr_tail = cur_descr;
16468 descr_tail = tail;
16469 if (bitsize > decl_size)
16470 goto discard_descr;
16471 decl_size -= bitsize;
16472 if (last == NULL)
16474 HOST_WIDE_INT offset = 0;
16475 if (GET_CODE (varloc) == VAR_LOCATION
16476 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16478 varloc = PAT_VAR_LOCATION_LOC (varloc);
16479 if (GET_CODE (varloc) == EXPR_LIST)
16480 varloc = XEXP (varloc, 0);
16484 if (GET_CODE (varloc) == CONST
16485 || GET_CODE (varloc) == SIGN_EXTEND
16486 || GET_CODE (varloc) == ZERO_EXTEND)
16487 varloc = XEXP (varloc, 0);
16488 else if (GET_CODE (varloc) == SUBREG)
16489 varloc = SUBREG_REG (varloc);
16490 else
16491 break;
16493 while (1);
16494 /* DW_OP_bit_size offset should be zero for register
16495 or implicit location descriptions and empty location
16496 descriptions, but for memory addresses needs big endian
16497 adjustment. */
16498 if (MEM_P (varloc))
16500 unsigned HOST_WIDE_INT memsize
16501 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16502 if (memsize != bitsize)
16504 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16505 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16506 goto discard_descr;
16507 if (memsize < bitsize)
16508 goto discard_descr;
16509 if (BITS_BIG_ENDIAN)
16510 offset = memsize - bitsize;
16514 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16515 if (*descr_tail == NULL)
16516 goto discard_descr;
16517 descr_tail = &(*descr_tail)->dw_loc_next;
16521 /* If there were any non-empty expressions, add padding till the end of
16522 the decl. */
16523 if (descr != NULL && decl_size != 0)
16525 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16526 if (*descr_tail == NULL)
16527 goto discard_descr;
16529 return descr;
16531 discard_descr:
16532 /* Discard the descriptor and release any addr_table entries it uses. */
16533 remove_loc_list_addr_table_entries (descr);
16534 return NULL;
16537 /* Return the dwarf representation of the location list LOC_LIST of
16538 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16539 function. */
16541 static dw_loc_list_ref
16542 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16544 const char *endname, *secname;
16545 rtx varloc;
16546 enum var_init_status initialized;
16547 struct var_loc_node *node;
16548 dw_loc_descr_ref descr;
16549 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16550 dw_loc_list_ref list = NULL;
16551 dw_loc_list_ref *listp = &list;
16553 /* Now that we know what section we are using for a base,
16554 actually construct the list of locations.
16555 The first location information is what is passed to the
16556 function that creates the location list, and the remaining
16557 locations just get added on to that list.
16558 Note that we only know the start address for a location
16559 (IE location changes), so to build the range, we use
16560 the range [current location start, next location start].
16561 This means we have to special case the last node, and generate
16562 a range of [last location start, end of function label]. */
16564 secname = secname_for_decl (decl);
16566 for (node = loc_list->first; node; node = node->next)
16567 if (GET_CODE (node->loc) == EXPR_LIST
16568 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16570 if (GET_CODE (node->loc) == EXPR_LIST)
16572 /* This requires DW_OP_{,bit_}piece, which is not usable
16573 inside DWARF expressions. */
16574 if (want_address != 2)
16575 continue;
16576 descr = dw_sra_loc_expr (decl, node->loc);
16577 if (descr == NULL)
16578 continue;
16580 else
16582 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16583 varloc = NOTE_VAR_LOCATION (node->loc);
16584 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16586 if (descr)
16588 bool range_across_switch = false;
16589 /* If section switch happens in between node->label
16590 and node->next->label (or end of function) and
16591 we can't emit it as a single entry list,
16592 emit two ranges, first one ending at the end
16593 of first partition and second one starting at the
16594 beginning of second partition. */
16595 if (node == loc_list->last_before_switch
16596 && (node != loc_list->first || loc_list->first->next)
16597 && current_function_decl)
16599 endname = cfun->fde->dw_fde_end;
16600 range_across_switch = true;
16602 /* The variable has a location between NODE->LABEL and
16603 NODE->NEXT->LABEL. */
16604 else if (node->next)
16605 endname = node->next->label;
16606 /* If the variable has a location at the last label
16607 it keeps its location until the end of function. */
16608 else if (!current_function_decl)
16609 endname = text_end_label;
16610 else
16612 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16613 current_function_funcdef_no);
16614 endname = ggc_strdup (label_id);
16617 *listp = new_loc_list (descr, node->label, endname, secname);
16618 if (TREE_CODE (decl) == PARM_DECL
16619 && node == loc_list->first
16620 && NOTE_P (node->loc)
16621 && strcmp (node->label, endname) == 0)
16622 (*listp)->force = true;
16623 listp = &(*listp)->dw_loc_next;
16625 if (range_across_switch)
16627 if (GET_CODE (node->loc) == EXPR_LIST)
16628 descr = dw_sra_loc_expr (decl, node->loc);
16629 else
16631 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16632 varloc = NOTE_VAR_LOCATION (node->loc);
16633 descr = dw_loc_list_1 (decl, varloc, want_address,
16634 initialized);
16636 gcc_assert (descr);
16637 /* The variable has a location between NODE->LABEL and
16638 NODE->NEXT->LABEL. */
16639 if (node->next)
16640 endname = node->next->label;
16641 else
16642 endname = cfun->fde->dw_fde_second_end;
16643 *listp = new_loc_list (descr,
16644 cfun->fde->dw_fde_second_begin,
16645 endname, secname);
16646 listp = &(*listp)->dw_loc_next;
16651 /* Try to avoid the overhead of a location list emitting a location
16652 expression instead, but only if we didn't have more than one
16653 location entry in the first place. If some entries were not
16654 representable, we don't want to pretend a single entry that was
16655 applies to the entire scope in which the variable is
16656 available. */
16657 if (list && loc_list->first->next)
16658 gen_llsym (list);
16660 return list;
16663 /* Return if the loc_list has only single element and thus can be represented
16664 as location description. */
16666 static bool
16667 single_element_loc_list_p (dw_loc_list_ref list)
16669 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16670 return !list->ll_symbol;
16673 /* Duplicate a single element of location list. */
16675 static inline dw_loc_descr_ref
16676 copy_loc_descr (dw_loc_descr_ref ref)
16678 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16679 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16680 return copy;
16683 /* To each location in list LIST append loc descr REF. */
16685 static void
16686 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16688 dw_loc_descr_ref copy;
16689 add_loc_descr (&list->expr, ref);
16690 list = list->dw_loc_next;
16691 while (list)
16693 copy = copy_loc_descr (ref);
16694 add_loc_descr (&list->expr, copy);
16695 while (copy->dw_loc_next)
16696 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16697 list = list->dw_loc_next;
16701 /* To each location in list LIST prepend loc descr REF. */
16703 static void
16704 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16706 dw_loc_descr_ref copy;
16707 dw_loc_descr_ref ref_end = list->expr;
16708 add_loc_descr (&ref, list->expr);
16709 list->expr = ref;
16710 list = list->dw_loc_next;
16711 while (list)
16713 dw_loc_descr_ref end = list->expr;
16714 list->expr = copy = copy_loc_descr (ref);
16715 while (copy->dw_loc_next != ref_end)
16716 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16717 copy->dw_loc_next = end;
16718 list = list->dw_loc_next;
16722 /* Given two lists RET and LIST
16723 produce location list that is result of adding expression in LIST
16724 to expression in RET on each position in program.
16725 Might be destructive on both RET and LIST.
16727 TODO: We handle only simple cases of RET or LIST having at most one
16728 element. General case would involve sorting the lists in program order
16729 and merging them that will need some additional work.
16730 Adding that will improve quality of debug info especially for SRA-ed
16731 structures. */
16733 static void
16734 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16736 if (!list)
16737 return;
16738 if (!*ret)
16740 *ret = list;
16741 return;
16743 if (!list->dw_loc_next)
16745 add_loc_descr_to_each (*ret, list->expr);
16746 return;
16748 if (!(*ret)->dw_loc_next)
16750 prepend_loc_descr_to_each (list, (*ret)->expr);
16751 *ret = list;
16752 return;
16754 expansion_failed (NULL_TREE, NULL_RTX,
16755 "Don't know how to merge two non-trivial"
16756 " location lists.\n");
16757 *ret = NULL;
16758 return;
16761 /* LOC is constant expression. Try a luck, look it up in constant
16762 pool and return its loc_descr of its address. */
16764 static dw_loc_descr_ref
16765 cst_pool_loc_descr (tree loc)
16767 /* Get an RTL for this, if something has been emitted. */
16768 rtx rtl = lookup_constant_def (loc);
16770 if (!rtl || !MEM_P (rtl))
16772 gcc_assert (!rtl);
16773 return 0;
16775 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16777 /* TODO: We might get more coverage if we was actually delaying expansion
16778 of all expressions till end of compilation when constant pools are fully
16779 populated. */
16780 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16782 expansion_failed (loc, NULL_RTX,
16783 "CST value in contant pool but not marked.");
16784 return 0;
16786 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16787 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16790 /* Return dw_loc_list representing address of addr_expr LOC
16791 by looking for inner INDIRECT_REF expression and turning
16792 it into simple arithmetics.
16794 See loc_list_from_tree for the meaning of CONTEXT. */
16796 static dw_loc_list_ref
16797 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16798 loc_descr_context *context)
16800 tree obj, offset;
16801 HOST_WIDE_INT bitsize, bitpos, bytepos;
16802 machine_mode mode;
16803 int unsignedp, reversep, volatilep = 0;
16804 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16806 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16807 &bitsize, &bitpos, &offset, &mode,
16808 &unsignedp, &reversep, &volatilep);
16809 STRIP_NOPS (obj);
16810 if (bitpos % BITS_PER_UNIT)
16812 expansion_failed (loc, NULL_RTX, "bitfield access");
16813 return 0;
16815 if (!INDIRECT_REF_P (obj))
16817 expansion_failed (obj,
16818 NULL_RTX, "no indirect ref in inner refrence");
16819 return 0;
16821 if (!offset && !bitpos)
16822 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16823 context);
16824 else if (toplev
16825 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16826 && (dwarf_version >= 4 || !dwarf_strict))
16828 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16829 if (!list_ret)
16830 return 0;
16831 if (offset)
16833 /* Variable offset. */
16834 list_ret1 = loc_list_from_tree (offset, 0, context);
16835 if (list_ret1 == 0)
16836 return 0;
16837 add_loc_list (&list_ret, list_ret1);
16838 if (!list_ret)
16839 return 0;
16840 add_loc_descr_to_each (list_ret,
16841 new_loc_descr (DW_OP_plus, 0, 0));
16843 bytepos = bitpos / BITS_PER_UNIT;
16844 if (bytepos > 0)
16845 add_loc_descr_to_each (list_ret,
16846 new_loc_descr (DW_OP_plus_uconst,
16847 bytepos, 0));
16848 else if (bytepos < 0)
16849 loc_list_plus_const (list_ret, bytepos);
16850 add_loc_descr_to_each (list_ret,
16851 new_loc_descr (DW_OP_stack_value, 0, 0));
16853 return list_ret;
16856 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16857 all operations from LOC are nops, move to the last one. Insert in NOPS all
16858 operations that are skipped. */
16860 static void
16861 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16862 hash_set<dw_loc_descr_ref> &nops)
16864 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16866 nops.add (loc);
16867 loc = loc->dw_loc_next;
16871 /* Helper for loc_descr_without_nops: free the location description operation
16872 P. */
16874 bool
16875 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16877 ggc_free (loc);
16878 return true;
16881 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16882 finishes LOC. */
16884 static void
16885 loc_descr_without_nops (dw_loc_descr_ref &loc)
16887 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16888 return;
16890 /* Set of all DW_OP_nop operations we remove. */
16891 hash_set<dw_loc_descr_ref> nops;
16893 /* First, strip all prefix NOP operations in order to keep the head of the
16894 operations list. */
16895 loc_descr_to_next_no_nop (loc, nops);
16897 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16899 /* For control flow operations: strip "prefix" nops in destination
16900 labels. */
16901 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16902 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16903 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16904 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16906 /* Do the same for the operations that follow, then move to the next
16907 iteration. */
16908 if (cur->dw_loc_next != NULL)
16909 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16910 cur = cur->dw_loc_next;
16913 nops.traverse<void *, free_loc_descr> (NULL);
16917 struct dwarf_procedure_info;
16919 /* Helper structure for location descriptions generation. */
16920 struct loc_descr_context
16922 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16923 NULL_TREE if DW_OP_push_object_address in invalid for this location
16924 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16925 tree context_type;
16926 /* The ..._DECL node that should be translated as a
16927 DW_OP_push_object_address operation. */
16928 tree base_decl;
16929 /* Information about the DWARF procedure we are currently generating. NULL if
16930 we are not generating a DWARF procedure. */
16931 struct dwarf_procedure_info *dpi;
16932 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16933 by consumer. Used for DW_TAG_generic_subrange attributes. */
16934 bool placeholder_arg;
16935 /* True if PLACEHOLDER_EXPR has been seen. */
16936 bool placeholder_seen;
16939 /* DWARF procedures generation
16941 DWARF expressions (aka. location descriptions) are used to encode variable
16942 things such as sizes or offsets. Such computations can have redundant parts
16943 that can be factorized in order to reduce the size of the output debug
16944 information. This is the whole point of DWARF procedures.
16946 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16947 already factorized into functions ("size functions") in order to handle very
16948 big and complex types. Such functions are quite simple: they have integral
16949 arguments, they return an integral result and their body contains only a
16950 return statement with arithmetic expressions. This is the only kind of
16951 function we are interested in translating into DWARF procedures, here.
16953 DWARF expressions and DWARF procedure are executed using a stack, so we have
16954 to define some calling convention for them to interact. Let's say that:
16956 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16957 all arguments in reverse order (right-to-left) so that when the DWARF
16958 procedure execution starts, the first argument is the top of the stack.
16960 - Then, when returning, the DWARF procedure must have consumed all arguments
16961 on the stack, must have pushed the result and touched nothing else.
16963 - Each integral argument and the result are integral types can be hold in a
16964 single stack slot.
16966 - We call "frame offset" the number of stack slots that are "under DWARF
16967 procedure control": it includes the arguments slots, the temporaries and
16968 the result slot. Thus, it is equal to the number of arguments when the
16969 procedure execution starts and must be equal to one (the result) when it
16970 returns. */
16972 /* Helper structure used when generating operations for a DWARF procedure. */
16973 struct dwarf_procedure_info
16975 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16976 currently translated. */
16977 tree fndecl;
16978 /* The number of arguments FNDECL takes. */
16979 unsigned args_count;
16982 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16983 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16984 equate it to this DIE. */
16986 static dw_die_ref
16987 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16988 dw_die_ref parent_die)
16990 dw_die_ref dwarf_proc_die;
16992 if ((dwarf_version < 3 && dwarf_strict)
16993 || location == NULL)
16994 return NULL;
16996 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16997 if (fndecl)
16998 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16999 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17000 return dwarf_proc_die;
17003 /* Return whether TYPE is a supported type as a DWARF procedure argument
17004 type or return type (we handle only scalar types and pointer types that
17005 aren't wider than the DWARF expression evaluation stack. */
17007 static bool
17008 is_handled_procedure_type (tree type)
17010 return ((INTEGRAL_TYPE_P (type)
17011 || TREE_CODE (type) == OFFSET_TYPE
17012 || TREE_CODE (type) == POINTER_TYPE)
17013 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17016 /* Helper for resolve_args_picking: do the same but stop when coming across
17017 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17018 offset *before* evaluating the corresponding operation. */
17020 static bool
17021 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17022 struct dwarf_procedure_info *dpi,
17023 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17025 /* The "frame_offset" identifier is already used to name a macro... */
17026 unsigned frame_offset_ = initial_frame_offset;
17027 dw_loc_descr_ref l;
17029 for (l = loc; l != NULL;)
17031 bool existed;
17032 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17034 /* If we already met this node, there is nothing to compute anymore. */
17035 if (existed)
17037 /* Make sure that the stack size is consistent wherever the execution
17038 flow comes from. */
17039 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17040 break;
17042 l_frame_offset = frame_offset_;
17044 /* If needed, relocate the picking offset with respect to the frame
17045 offset. */
17046 if (l->frame_offset_rel)
17048 unsigned HOST_WIDE_INT off;
17049 switch (l->dw_loc_opc)
17051 case DW_OP_pick:
17052 off = l->dw_loc_oprnd1.v.val_unsigned;
17053 break;
17054 case DW_OP_dup:
17055 off = 0;
17056 break;
17057 case DW_OP_over:
17058 off = 1;
17059 break;
17060 default:
17061 gcc_unreachable ();
17063 /* frame_offset_ is the size of the current stack frame, including
17064 incoming arguments. Besides, the arguments are pushed
17065 right-to-left. Thus, in order to access the Nth argument from
17066 this operation node, the picking has to skip temporaries *plus*
17067 one stack slot per argument (0 for the first one, 1 for the second
17068 one, etc.).
17070 The targetted argument number (N) is already set as the operand,
17071 and the number of temporaries can be computed with:
17072 frame_offsets_ - dpi->args_count */
17073 off += frame_offset_ - dpi->args_count;
17075 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17076 if (off > 255)
17077 return false;
17079 if (off == 0)
17081 l->dw_loc_opc = DW_OP_dup;
17082 l->dw_loc_oprnd1.v.val_unsigned = 0;
17084 else if (off == 1)
17086 l->dw_loc_opc = DW_OP_over;
17087 l->dw_loc_oprnd1.v.val_unsigned = 0;
17089 else
17091 l->dw_loc_opc = DW_OP_pick;
17092 l->dw_loc_oprnd1.v.val_unsigned = off;
17096 /* Update frame_offset according to the effect the current operation has
17097 on the stack. */
17098 switch (l->dw_loc_opc)
17100 case DW_OP_deref:
17101 case DW_OP_swap:
17102 case DW_OP_rot:
17103 case DW_OP_abs:
17104 case DW_OP_neg:
17105 case DW_OP_not:
17106 case DW_OP_plus_uconst:
17107 case DW_OP_skip:
17108 case DW_OP_reg0:
17109 case DW_OP_reg1:
17110 case DW_OP_reg2:
17111 case DW_OP_reg3:
17112 case DW_OP_reg4:
17113 case DW_OP_reg5:
17114 case DW_OP_reg6:
17115 case DW_OP_reg7:
17116 case DW_OP_reg8:
17117 case DW_OP_reg9:
17118 case DW_OP_reg10:
17119 case DW_OP_reg11:
17120 case DW_OP_reg12:
17121 case DW_OP_reg13:
17122 case DW_OP_reg14:
17123 case DW_OP_reg15:
17124 case DW_OP_reg16:
17125 case DW_OP_reg17:
17126 case DW_OP_reg18:
17127 case DW_OP_reg19:
17128 case DW_OP_reg20:
17129 case DW_OP_reg21:
17130 case DW_OP_reg22:
17131 case DW_OP_reg23:
17132 case DW_OP_reg24:
17133 case DW_OP_reg25:
17134 case DW_OP_reg26:
17135 case DW_OP_reg27:
17136 case DW_OP_reg28:
17137 case DW_OP_reg29:
17138 case DW_OP_reg30:
17139 case DW_OP_reg31:
17140 case DW_OP_bregx:
17141 case DW_OP_piece:
17142 case DW_OP_deref_size:
17143 case DW_OP_nop:
17144 case DW_OP_bit_piece:
17145 case DW_OP_implicit_value:
17146 case DW_OP_stack_value:
17147 break;
17149 case DW_OP_addr:
17150 case DW_OP_const1u:
17151 case DW_OP_const1s:
17152 case DW_OP_const2u:
17153 case DW_OP_const2s:
17154 case DW_OP_const4u:
17155 case DW_OP_const4s:
17156 case DW_OP_const8u:
17157 case DW_OP_const8s:
17158 case DW_OP_constu:
17159 case DW_OP_consts:
17160 case DW_OP_dup:
17161 case DW_OP_over:
17162 case DW_OP_pick:
17163 case DW_OP_lit0:
17164 case DW_OP_lit1:
17165 case DW_OP_lit2:
17166 case DW_OP_lit3:
17167 case DW_OP_lit4:
17168 case DW_OP_lit5:
17169 case DW_OP_lit6:
17170 case DW_OP_lit7:
17171 case DW_OP_lit8:
17172 case DW_OP_lit9:
17173 case DW_OP_lit10:
17174 case DW_OP_lit11:
17175 case DW_OP_lit12:
17176 case DW_OP_lit13:
17177 case DW_OP_lit14:
17178 case DW_OP_lit15:
17179 case DW_OP_lit16:
17180 case DW_OP_lit17:
17181 case DW_OP_lit18:
17182 case DW_OP_lit19:
17183 case DW_OP_lit20:
17184 case DW_OP_lit21:
17185 case DW_OP_lit22:
17186 case DW_OP_lit23:
17187 case DW_OP_lit24:
17188 case DW_OP_lit25:
17189 case DW_OP_lit26:
17190 case DW_OP_lit27:
17191 case DW_OP_lit28:
17192 case DW_OP_lit29:
17193 case DW_OP_lit30:
17194 case DW_OP_lit31:
17195 case DW_OP_breg0:
17196 case DW_OP_breg1:
17197 case DW_OP_breg2:
17198 case DW_OP_breg3:
17199 case DW_OP_breg4:
17200 case DW_OP_breg5:
17201 case DW_OP_breg6:
17202 case DW_OP_breg7:
17203 case DW_OP_breg8:
17204 case DW_OP_breg9:
17205 case DW_OP_breg10:
17206 case DW_OP_breg11:
17207 case DW_OP_breg12:
17208 case DW_OP_breg13:
17209 case DW_OP_breg14:
17210 case DW_OP_breg15:
17211 case DW_OP_breg16:
17212 case DW_OP_breg17:
17213 case DW_OP_breg18:
17214 case DW_OP_breg19:
17215 case DW_OP_breg20:
17216 case DW_OP_breg21:
17217 case DW_OP_breg22:
17218 case DW_OP_breg23:
17219 case DW_OP_breg24:
17220 case DW_OP_breg25:
17221 case DW_OP_breg26:
17222 case DW_OP_breg27:
17223 case DW_OP_breg28:
17224 case DW_OP_breg29:
17225 case DW_OP_breg30:
17226 case DW_OP_breg31:
17227 case DW_OP_fbreg:
17228 case DW_OP_push_object_address:
17229 case DW_OP_call_frame_cfa:
17230 case DW_OP_GNU_variable_value:
17231 ++frame_offset_;
17232 break;
17234 case DW_OP_drop:
17235 case DW_OP_xderef:
17236 case DW_OP_and:
17237 case DW_OP_div:
17238 case DW_OP_minus:
17239 case DW_OP_mod:
17240 case DW_OP_mul:
17241 case DW_OP_or:
17242 case DW_OP_plus:
17243 case DW_OP_shl:
17244 case DW_OP_shr:
17245 case DW_OP_shra:
17246 case DW_OP_xor:
17247 case DW_OP_bra:
17248 case DW_OP_eq:
17249 case DW_OP_ge:
17250 case DW_OP_gt:
17251 case DW_OP_le:
17252 case DW_OP_lt:
17253 case DW_OP_ne:
17254 case DW_OP_regx:
17255 case DW_OP_xderef_size:
17256 --frame_offset_;
17257 break;
17259 case DW_OP_call2:
17260 case DW_OP_call4:
17261 case DW_OP_call_ref:
17263 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17264 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17266 if (stack_usage == NULL)
17267 return false;
17268 frame_offset_ += *stack_usage;
17269 break;
17272 case DW_OP_implicit_pointer:
17273 case DW_OP_entry_value:
17274 case DW_OP_const_type:
17275 case DW_OP_regval_type:
17276 case DW_OP_deref_type:
17277 case DW_OP_convert:
17278 case DW_OP_reinterpret:
17279 case DW_OP_form_tls_address:
17280 case DW_OP_GNU_push_tls_address:
17281 case DW_OP_GNU_uninit:
17282 case DW_OP_GNU_encoded_addr:
17283 case DW_OP_GNU_implicit_pointer:
17284 case DW_OP_GNU_entry_value:
17285 case DW_OP_GNU_const_type:
17286 case DW_OP_GNU_regval_type:
17287 case DW_OP_GNU_deref_type:
17288 case DW_OP_GNU_convert:
17289 case DW_OP_GNU_reinterpret:
17290 case DW_OP_GNU_parameter_ref:
17291 /* loc_list_from_tree will probably not output these operations for
17292 size functions, so assume they will not appear here. */
17293 /* Fall through... */
17295 default:
17296 gcc_unreachable ();
17299 /* Now, follow the control flow (except subroutine calls). */
17300 switch (l->dw_loc_opc)
17302 case DW_OP_bra:
17303 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17304 frame_offsets))
17305 return false;
17306 /* Fall through. */
17308 case DW_OP_skip:
17309 l = l->dw_loc_oprnd1.v.val_loc;
17310 break;
17312 case DW_OP_stack_value:
17313 return true;
17315 default:
17316 l = l->dw_loc_next;
17317 break;
17321 return true;
17324 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17325 operations) in order to resolve the operand of DW_OP_pick operations that
17326 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17327 offset *before* LOC is executed. Return if all relocations were
17328 successful. */
17330 static bool
17331 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17332 struct dwarf_procedure_info *dpi)
17334 /* Associate to all visited operations the frame offset *before* evaluating
17335 this operation. */
17336 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17338 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17339 frame_offsets);
17342 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17343 Return NULL if it is not possible. */
17345 static dw_die_ref
17346 function_to_dwarf_procedure (tree fndecl)
17348 struct loc_descr_context ctx;
17349 struct dwarf_procedure_info dpi;
17350 dw_die_ref dwarf_proc_die;
17351 tree tree_body = DECL_SAVED_TREE (fndecl);
17352 dw_loc_descr_ref loc_body, epilogue;
17354 tree cursor;
17355 unsigned i;
17357 /* Do not generate multiple DWARF procedures for the same function
17358 declaration. */
17359 dwarf_proc_die = lookup_decl_die (fndecl);
17360 if (dwarf_proc_die != NULL)
17361 return dwarf_proc_die;
17363 /* DWARF procedures are available starting with the DWARFv3 standard. */
17364 if (dwarf_version < 3 && dwarf_strict)
17365 return NULL;
17367 /* We handle only functions for which we still have a body, that return a
17368 supported type and that takes arguments with supported types. Note that
17369 there is no point translating functions that return nothing. */
17370 if (tree_body == NULL_TREE
17371 || DECL_RESULT (fndecl) == NULL_TREE
17372 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17373 return NULL;
17375 for (cursor = DECL_ARGUMENTS (fndecl);
17376 cursor != NULL_TREE;
17377 cursor = TREE_CHAIN (cursor))
17378 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17379 return NULL;
17381 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17382 if (TREE_CODE (tree_body) != RETURN_EXPR)
17383 return NULL;
17384 tree_body = TREE_OPERAND (tree_body, 0);
17385 if (TREE_CODE (tree_body) != MODIFY_EXPR
17386 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17387 return NULL;
17388 tree_body = TREE_OPERAND (tree_body, 1);
17390 /* Try to translate the body expression itself. Note that this will probably
17391 cause an infinite recursion if its call graph has a cycle. This is very
17392 unlikely for size functions, however, so don't bother with such things at
17393 the moment. */
17394 ctx.context_type = NULL_TREE;
17395 ctx.base_decl = NULL_TREE;
17396 ctx.dpi = &dpi;
17397 ctx.placeholder_arg = false;
17398 ctx.placeholder_seen = false;
17399 dpi.fndecl = fndecl;
17400 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17401 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17402 if (!loc_body)
17403 return NULL;
17405 /* After evaluating all operands in "loc_body", we should still have on the
17406 stack all arguments plus the desired function result (top of the stack).
17407 Generate code in order to keep only the result in our stack frame. */
17408 epilogue = NULL;
17409 for (i = 0; i < dpi.args_count; ++i)
17411 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17412 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17413 op_couple->dw_loc_next->dw_loc_next = epilogue;
17414 epilogue = op_couple;
17416 add_loc_descr (&loc_body, epilogue);
17417 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17418 return NULL;
17420 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17421 because they are considered useful. Now there is an epilogue, they are
17422 not anymore, so give it another try. */
17423 loc_descr_without_nops (loc_body);
17425 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17426 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17427 though, given that size functions do not come from source, so they should
17428 not have a dedicated DW_TAG_subprogram DIE. */
17429 dwarf_proc_die
17430 = new_dwarf_proc_die (loc_body, fndecl,
17431 get_context_die (DECL_CONTEXT (fndecl)));
17433 /* The called DWARF procedure consumes one stack slot per argument and
17434 returns one stack slot. */
17435 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17437 return dwarf_proc_die;
17441 /* Generate Dwarf location list representing LOC.
17442 If WANT_ADDRESS is false, expression computing LOC will be computed
17443 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17444 if WANT_ADDRESS is 2, expression computing address useable in location
17445 will be returned (i.e. DW_OP_reg can be used
17446 to refer to register values).
17448 CONTEXT provides information to customize the location descriptions
17449 generation. Its context_type field specifies what type is implicitly
17450 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17451 will not be generated.
17453 Its DPI field determines whether we are generating a DWARF expression for a
17454 DWARF procedure, so PARM_DECL references are processed specifically.
17456 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17457 and dpi fields were null. */
17459 static dw_loc_list_ref
17460 loc_list_from_tree_1 (tree loc, int want_address,
17461 struct loc_descr_context *context)
17463 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17464 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17465 int have_address = 0;
17466 enum dwarf_location_atom op;
17468 /* ??? Most of the time we do not take proper care for sign/zero
17469 extending the values properly. Hopefully this won't be a real
17470 problem... */
17472 if (context != NULL
17473 && context->base_decl == loc
17474 && want_address == 0)
17476 if (dwarf_version >= 3 || !dwarf_strict)
17477 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17478 NULL, NULL, NULL);
17479 else
17480 return NULL;
17483 switch (TREE_CODE (loc))
17485 case ERROR_MARK:
17486 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17487 return 0;
17489 case PLACEHOLDER_EXPR:
17490 /* This case involves extracting fields from an object to determine the
17491 position of other fields. It is supposed to appear only as the first
17492 operand of COMPONENT_REF nodes and to reference precisely the type
17493 that the context allows. */
17494 if (context != NULL
17495 && TREE_TYPE (loc) == context->context_type
17496 && want_address >= 1)
17498 if (dwarf_version >= 3 || !dwarf_strict)
17500 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17501 have_address = 1;
17502 break;
17504 else
17505 return NULL;
17507 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17508 the single argument passed by consumer. */
17509 else if (context != NULL
17510 && context->placeholder_arg
17511 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17512 && want_address == 0)
17514 ret = new_loc_descr (DW_OP_pick, 0, 0);
17515 ret->frame_offset_rel = 1;
17516 context->placeholder_seen = true;
17517 break;
17519 else
17520 expansion_failed (loc, NULL_RTX,
17521 "PLACEHOLDER_EXPR for an unexpected type");
17522 break;
17524 case CALL_EXPR:
17526 const int nargs = call_expr_nargs (loc);
17527 tree callee = get_callee_fndecl (loc);
17528 int i;
17529 dw_die_ref dwarf_proc;
17531 if (callee == NULL_TREE)
17532 goto call_expansion_failed;
17534 /* We handle only functions that return an integer. */
17535 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17536 goto call_expansion_failed;
17538 dwarf_proc = function_to_dwarf_procedure (callee);
17539 if (dwarf_proc == NULL)
17540 goto call_expansion_failed;
17542 /* Evaluate arguments right-to-left so that the first argument will
17543 be the top-most one on the stack. */
17544 for (i = nargs - 1; i >= 0; --i)
17546 dw_loc_descr_ref loc_descr
17547 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17548 context);
17550 if (loc_descr == NULL)
17551 goto call_expansion_failed;
17553 add_loc_descr (&ret, loc_descr);
17556 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17557 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17558 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17559 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17560 add_loc_descr (&ret, ret1);
17561 break;
17563 call_expansion_failed:
17564 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17565 /* There are no opcodes for these operations. */
17566 return 0;
17569 case PREINCREMENT_EXPR:
17570 case PREDECREMENT_EXPR:
17571 case POSTINCREMENT_EXPR:
17572 case POSTDECREMENT_EXPR:
17573 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17574 /* There are no opcodes for these operations. */
17575 return 0;
17577 case ADDR_EXPR:
17578 /* If we already want an address, see if there is INDIRECT_REF inside
17579 e.g. for &this->field. */
17580 if (want_address)
17582 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17583 (loc, want_address == 2, context);
17584 if (list_ret)
17585 have_address = 1;
17586 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17587 && (ret = cst_pool_loc_descr (loc)))
17588 have_address = 1;
17590 /* Otherwise, process the argument and look for the address. */
17591 if (!list_ret && !ret)
17592 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17593 else
17595 if (want_address)
17596 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17597 return NULL;
17599 break;
17601 case VAR_DECL:
17602 if (DECL_THREAD_LOCAL_P (loc))
17604 rtx rtl;
17605 enum dwarf_location_atom tls_op;
17606 enum dtprel_bool dtprel = dtprel_false;
17608 if (targetm.have_tls)
17610 /* If this is not defined, we have no way to emit the
17611 data. */
17612 if (!targetm.asm_out.output_dwarf_dtprel)
17613 return 0;
17615 /* The way DW_OP_GNU_push_tls_address is specified, we
17616 can only look up addresses of objects in the current
17617 module. We used DW_OP_addr as first op, but that's
17618 wrong, because DW_OP_addr is relocated by the debug
17619 info consumer, while DW_OP_GNU_push_tls_address
17620 operand shouldn't be. */
17621 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17622 return 0;
17623 dtprel = dtprel_true;
17624 /* We check for DWARF 5 here because gdb did not implement
17625 DW_OP_form_tls_address until after 7.12. */
17626 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17627 : DW_OP_GNU_push_tls_address);
17629 else
17631 if (!targetm.emutls.debug_form_tls_address
17632 || !(dwarf_version >= 3 || !dwarf_strict))
17633 return 0;
17634 /* We stuffed the control variable into the DECL_VALUE_EXPR
17635 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17636 no longer appear in gimple code. We used the control
17637 variable in specific so that we could pick it up here. */
17638 loc = DECL_VALUE_EXPR (loc);
17639 tls_op = DW_OP_form_tls_address;
17642 rtl = rtl_for_decl_location (loc);
17643 if (rtl == NULL_RTX)
17644 return 0;
17646 if (!MEM_P (rtl))
17647 return 0;
17648 rtl = XEXP (rtl, 0);
17649 if (! CONSTANT_P (rtl))
17650 return 0;
17652 ret = new_addr_loc_descr (rtl, dtprel);
17653 ret1 = new_loc_descr (tls_op, 0, 0);
17654 add_loc_descr (&ret, ret1);
17656 have_address = 1;
17657 break;
17659 /* FALLTHRU */
17661 case PARM_DECL:
17662 if (context != NULL && context->dpi != NULL
17663 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17665 /* We are generating code for a DWARF procedure and we want to access
17666 one of its arguments: find the appropriate argument offset and let
17667 the resolve_args_picking pass compute the offset that complies
17668 with the stack frame size. */
17669 unsigned i = 0;
17670 tree cursor;
17672 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17673 cursor != NULL_TREE && cursor != loc;
17674 cursor = TREE_CHAIN (cursor), ++i)
17676 /* If we are translating a DWARF procedure, all referenced parameters
17677 must belong to the current function. */
17678 gcc_assert (cursor != NULL_TREE);
17680 ret = new_loc_descr (DW_OP_pick, i, 0);
17681 ret->frame_offset_rel = 1;
17682 break;
17684 /* FALLTHRU */
17686 case RESULT_DECL:
17687 if (DECL_HAS_VALUE_EXPR_P (loc))
17688 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17689 want_address, context);
17690 /* FALLTHRU */
17692 case FUNCTION_DECL:
17694 rtx rtl;
17695 var_loc_list *loc_list = lookup_decl_loc (loc);
17697 if (loc_list && loc_list->first)
17699 list_ret = dw_loc_list (loc_list, loc, want_address);
17700 have_address = want_address != 0;
17701 break;
17703 rtl = rtl_for_decl_location (loc);
17704 if (rtl == NULL_RTX)
17706 if (TREE_CODE (loc) != FUNCTION_DECL
17707 && early_dwarf
17708 && current_function_decl
17709 && want_address != 1
17710 && ! DECL_IGNORED_P (loc)
17711 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17712 || POINTER_TYPE_P (TREE_TYPE (loc)))
17713 && DECL_CONTEXT (loc) == current_function_decl
17714 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17715 <= DWARF2_ADDR_SIZE))
17717 dw_die_ref ref = lookup_decl_die (loc);
17718 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17719 if (ref)
17721 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17722 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17723 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17725 else
17727 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17728 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17730 break;
17732 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17733 return 0;
17735 else if (CONST_INT_P (rtl))
17737 HOST_WIDE_INT val = INTVAL (rtl);
17738 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17739 val &= GET_MODE_MASK (DECL_MODE (loc));
17740 ret = int_loc_descriptor (val);
17742 else if (GET_CODE (rtl) == CONST_STRING)
17744 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17745 return 0;
17747 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17748 ret = new_addr_loc_descr (rtl, dtprel_false);
17749 else
17751 machine_mode mode, mem_mode;
17753 /* Certain constructs can only be represented at top-level. */
17754 if (want_address == 2)
17756 ret = loc_descriptor (rtl, VOIDmode,
17757 VAR_INIT_STATUS_INITIALIZED);
17758 have_address = 1;
17760 else
17762 mode = GET_MODE (rtl);
17763 mem_mode = VOIDmode;
17764 if (MEM_P (rtl))
17766 mem_mode = mode;
17767 mode = get_address_mode (rtl);
17768 rtl = XEXP (rtl, 0);
17769 have_address = 1;
17771 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17772 VAR_INIT_STATUS_INITIALIZED);
17774 if (!ret)
17775 expansion_failed (loc, rtl,
17776 "failed to produce loc descriptor for rtl");
17779 break;
17781 case MEM_REF:
17782 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17784 have_address = 1;
17785 goto do_plus;
17787 /* Fallthru. */
17788 case INDIRECT_REF:
17789 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17790 have_address = 1;
17791 break;
17793 case TARGET_MEM_REF:
17794 case SSA_NAME:
17795 case DEBUG_EXPR_DECL:
17796 return NULL;
17798 case COMPOUND_EXPR:
17799 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17800 context);
17802 CASE_CONVERT:
17803 case VIEW_CONVERT_EXPR:
17804 case SAVE_EXPR:
17805 case MODIFY_EXPR:
17806 case NON_LVALUE_EXPR:
17807 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17808 context);
17810 case COMPONENT_REF:
17811 case BIT_FIELD_REF:
17812 case ARRAY_REF:
17813 case ARRAY_RANGE_REF:
17814 case REALPART_EXPR:
17815 case IMAGPART_EXPR:
17817 tree obj, offset;
17818 HOST_WIDE_INT bitsize, bitpos, bytepos;
17819 machine_mode mode;
17820 int unsignedp, reversep, volatilep = 0;
17822 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17823 &unsignedp, &reversep, &volatilep);
17825 gcc_assert (obj != loc);
17827 list_ret = loc_list_from_tree_1 (obj,
17828 want_address == 2
17829 && !bitpos && !offset ? 2 : 1,
17830 context);
17831 /* TODO: We can extract value of the small expression via shifting even
17832 for nonzero bitpos. */
17833 if (list_ret == 0)
17834 return 0;
17835 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17837 expansion_failed (loc, NULL_RTX,
17838 "bitfield access");
17839 return 0;
17842 if (offset != NULL_TREE)
17844 /* Variable offset. */
17845 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17846 if (list_ret1 == 0)
17847 return 0;
17848 add_loc_list (&list_ret, list_ret1);
17849 if (!list_ret)
17850 return 0;
17851 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17854 bytepos = bitpos / BITS_PER_UNIT;
17855 if (bytepos > 0)
17856 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17857 else if (bytepos < 0)
17858 loc_list_plus_const (list_ret, bytepos);
17860 have_address = 1;
17861 break;
17864 case INTEGER_CST:
17865 if ((want_address || !tree_fits_shwi_p (loc))
17866 && (ret = cst_pool_loc_descr (loc)))
17867 have_address = 1;
17868 else if (want_address == 2
17869 && tree_fits_shwi_p (loc)
17870 && (ret = address_of_int_loc_descriptor
17871 (int_size_in_bytes (TREE_TYPE (loc)),
17872 tree_to_shwi (loc))))
17873 have_address = 1;
17874 else if (tree_fits_shwi_p (loc))
17875 ret = int_loc_descriptor (tree_to_shwi (loc));
17876 else if (tree_fits_uhwi_p (loc))
17877 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17878 else
17880 expansion_failed (loc, NULL_RTX,
17881 "Integer operand is not host integer");
17882 return 0;
17884 break;
17886 case CONSTRUCTOR:
17887 case REAL_CST:
17888 case STRING_CST:
17889 case COMPLEX_CST:
17890 if ((ret = cst_pool_loc_descr (loc)))
17891 have_address = 1;
17892 else if (TREE_CODE (loc) == CONSTRUCTOR)
17894 tree type = TREE_TYPE (loc);
17895 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17896 unsigned HOST_WIDE_INT offset = 0;
17897 unsigned HOST_WIDE_INT cnt;
17898 constructor_elt *ce;
17900 if (TREE_CODE (type) == RECORD_TYPE)
17902 /* This is very limited, but it's enough to output
17903 pointers to member functions, as long as the
17904 referenced function is defined in the current
17905 translation unit. */
17906 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17908 tree val = ce->value;
17910 tree field = ce->index;
17912 if (val)
17913 STRIP_NOPS (val);
17915 if (!field || DECL_BIT_FIELD (field))
17917 expansion_failed (loc, NULL_RTX,
17918 "bitfield in record type constructor");
17919 size = offset = (unsigned HOST_WIDE_INT)-1;
17920 ret = NULL;
17921 break;
17924 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17925 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17926 gcc_assert (pos + fieldsize <= size);
17927 if (pos < offset)
17929 expansion_failed (loc, NULL_RTX,
17930 "out-of-order fields in record constructor");
17931 size = offset = (unsigned HOST_WIDE_INT)-1;
17932 ret = NULL;
17933 break;
17935 if (pos > offset)
17937 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17938 add_loc_descr (&ret, ret1);
17939 offset = pos;
17941 if (val && fieldsize != 0)
17943 ret1 = loc_descriptor_from_tree (val, want_address, context);
17944 if (!ret1)
17946 expansion_failed (loc, NULL_RTX,
17947 "unsupported expression in field");
17948 size = offset = (unsigned HOST_WIDE_INT)-1;
17949 ret = NULL;
17950 break;
17952 add_loc_descr (&ret, ret1);
17954 if (fieldsize)
17956 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17957 add_loc_descr (&ret, ret1);
17958 offset = pos + fieldsize;
17962 if (offset != size)
17964 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17965 add_loc_descr (&ret, ret1);
17966 offset = size;
17969 have_address = !!want_address;
17971 else
17972 expansion_failed (loc, NULL_RTX,
17973 "constructor of non-record type");
17975 else
17976 /* We can construct small constants here using int_loc_descriptor. */
17977 expansion_failed (loc, NULL_RTX,
17978 "constructor or constant not in constant pool");
17979 break;
17981 case TRUTH_AND_EXPR:
17982 case TRUTH_ANDIF_EXPR:
17983 case BIT_AND_EXPR:
17984 op = DW_OP_and;
17985 goto do_binop;
17987 case TRUTH_XOR_EXPR:
17988 case BIT_XOR_EXPR:
17989 op = DW_OP_xor;
17990 goto do_binop;
17992 case TRUTH_OR_EXPR:
17993 case TRUTH_ORIF_EXPR:
17994 case BIT_IOR_EXPR:
17995 op = DW_OP_or;
17996 goto do_binop;
17998 case FLOOR_DIV_EXPR:
17999 case CEIL_DIV_EXPR:
18000 case ROUND_DIV_EXPR:
18001 case TRUNC_DIV_EXPR:
18002 case EXACT_DIV_EXPR:
18003 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18004 return 0;
18005 op = DW_OP_div;
18006 goto do_binop;
18008 case MINUS_EXPR:
18009 op = DW_OP_minus;
18010 goto do_binop;
18012 case FLOOR_MOD_EXPR:
18013 case CEIL_MOD_EXPR:
18014 case ROUND_MOD_EXPR:
18015 case TRUNC_MOD_EXPR:
18016 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18018 op = DW_OP_mod;
18019 goto do_binop;
18021 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18022 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18023 if (list_ret == 0 || list_ret1 == 0)
18024 return 0;
18026 add_loc_list (&list_ret, list_ret1);
18027 if (list_ret == 0)
18028 return 0;
18029 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18030 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18031 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18032 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18033 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18034 break;
18036 case MULT_EXPR:
18037 op = DW_OP_mul;
18038 goto do_binop;
18040 case LSHIFT_EXPR:
18041 op = DW_OP_shl;
18042 goto do_binop;
18044 case RSHIFT_EXPR:
18045 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18046 goto do_binop;
18048 case POINTER_PLUS_EXPR:
18049 case PLUS_EXPR:
18050 do_plus:
18051 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18053 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18054 smarter to encode their opposite. The DW_OP_plus_uconst operation
18055 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18056 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18057 bytes, Y being the size of the operation that pushes the opposite
18058 of the addend. So let's choose the smallest representation. */
18059 const tree tree_addend = TREE_OPERAND (loc, 1);
18060 offset_int wi_addend;
18061 HOST_WIDE_INT shwi_addend;
18062 dw_loc_descr_ref loc_naddend;
18064 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18065 if (list_ret == 0)
18066 return 0;
18068 /* Try to get the literal to push. It is the opposite of the addend,
18069 so as we rely on wrapping during DWARF evaluation, first decode
18070 the literal as a "DWARF-sized" signed number. */
18071 wi_addend = wi::to_offset (tree_addend);
18072 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18073 shwi_addend = wi_addend.to_shwi ();
18074 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18075 ? int_loc_descriptor (-shwi_addend)
18076 : NULL;
18078 if (loc_naddend != NULL
18079 && ((unsigned) size_of_uleb128 (shwi_addend)
18080 > size_of_loc_descr (loc_naddend)))
18082 add_loc_descr_to_each (list_ret, loc_naddend);
18083 add_loc_descr_to_each (list_ret,
18084 new_loc_descr (DW_OP_minus, 0, 0));
18086 else
18088 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18090 loc_naddend = loc_cur;
18091 loc_cur = loc_cur->dw_loc_next;
18092 ggc_free (loc_naddend);
18094 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18096 break;
18099 op = DW_OP_plus;
18100 goto do_binop;
18102 case LE_EXPR:
18103 op = DW_OP_le;
18104 goto do_comp_binop;
18106 case GE_EXPR:
18107 op = DW_OP_ge;
18108 goto do_comp_binop;
18110 case LT_EXPR:
18111 op = DW_OP_lt;
18112 goto do_comp_binop;
18114 case GT_EXPR:
18115 op = DW_OP_gt;
18116 goto do_comp_binop;
18118 do_comp_binop:
18119 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18121 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18122 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18123 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18124 TREE_CODE (loc));
18125 break;
18127 else
18128 goto do_binop;
18130 case EQ_EXPR:
18131 op = DW_OP_eq;
18132 goto do_binop;
18134 case NE_EXPR:
18135 op = DW_OP_ne;
18136 goto do_binop;
18138 do_binop:
18139 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18140 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18141 if (list_ret == 0 || list_ret1 == 0)
18142 return 0;
18144 add_loc_list (&list_ret, list_ret1);
18145 if (list_ret == 0)
18146 return 0;
18147 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18148 break;
18150 case TRUTH_NOT_EXPR:
18151 case BIT_NOT_EXPR:
18152 op = DW_OP_not;
18153 goto do_unop;
18155 case ABS_EXPR:
18156 op = DW_OP_abs;
18157 goto do_unop;
18159 case NEGATE_EXPR:
18160 op = DW_OP_neg;
18161 goto do_unop;
18163 do_unop:
18164 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18165 if (list_ret == 0)
18166 return 0;
18168 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18169 break;
18171 case MIN_EXPR:
18172 case MAX_EXPR:
18174 const enum tree_code code =
18175 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18177 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18178 build2 (code, integer_type_node,
18179 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18180 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18183 /* fall through */
18185 case COND_EXPR:
18187 dw_loc_descr_ref lhs
18188 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18189 dw_loc_list_ref rhs
18190 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18191 dw_loc_descr_ref bra_node, jump_node, tmp;
18193 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18194 if (list_ret == 0 || lhs == 0 || rhs == 0)
18195 return 0;
18197 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18198 add_loc_descr_to_each (list_ret, bra_node);
18200 add_loc_list (&list_ret, rhs);
18201 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18202 add_loc_descr_to_each (list_ret, jump_node);
18204 add_loc_descr_to_each (list_ret, lhs);
18205 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18206 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18208 /* ??? Need a node to point the skip at. Use a nop. */
18209 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18210 add_loc_descr_to_each (list_ret, tmp);
18211 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18212 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18214 break;
18216 case FIX_TRUNC_EXPR:
18217 return 0;
18219 default:
18220 /* Leave front-end specific codes as simply unknown. This comes
18221 up, for instance, with the C STMT_EXPR. */
18222 if ((unsigned int) TREE_CODE (loc)
18223 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18225 expansion_failed (loc, NULL_RTX,
18226 "language specific tree node");
18227 return 0;
18230 /* Otherwise this is a generic code; we should just lists all of
18231 these explicitly. We forgot one. */
18232 if (flag_checking)
18233 gcc_unreachable ();
18235 /* In a release build, we want to degrade gracefully: better to
18236 generate incomplete debugging information than to crash. */
18237 return NULL;
18240 if (!ret && !list_ret)
18241 return 0;
18243 if (want_address == 2 && !have_address
18244 && (dwarf_version >= 4 || !dwarf_strict))
18246 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18248 expansion_failed (loc, NULL_RTX,
18249 "DWARF address size mismatch");
18250 return 0;
18252 if (ret)
18253 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18254 else
18255 add_loc_descr_to_each (list_ret,
18256 new_loc_descr (DW_OP_stack_value, 0, 0));
18257 have_address = 1;
18259 /* Show if we can't fill the request for an address. */
18260 if (want_address && !have_address)
18262 expansion_failed (loc, NULL_RTX,
18263 "Want address and only have value");
18264 return 0;
18267 gcc_assert (!ret || !list_ret);
18269 /* If we've got an address and don't want one, dereference. */
18270 if (!want_address && have_address)
18272 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18274 if (size > DWARF2_ADDR_SIZE || size == -1)
18276 expansion_failed (loc, NULL_RTX,
18277 "DWARF address size mismatch");
18278 return 0;
18280 else if (size == DWARF2_ADDR_SIZE)
18281 op = DW_OP_deref;
18282 else
18283 op = DW_OP_deref_size;
18285 if (ret)
18286 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18287 else
18288 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18290 if (ret)
18291 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18293 return list_ret;
18296 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18297 expressions. */
18299 static dw_loc_list_ref
18300 loc_list_from_tree (tree loc, int want_address,
18301 struct loc_descr_context *context)
18303 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18305 for (dw_loc_list_ref loc_cur = result;
18306 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18307 loc_descr_without_nops (loc_cur->expr);
18308 return result;
18311 /* Same as above but return only single location expression. */
18312 static dw_loc_descr_ref
18313 loc_descriptor_from_tree (tree loc, int want_address,
18314 struct loc_descr_context *context)
18316 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18317 if (!ret)
18318 return NULL;
18319 if (ret->dw_loc_next)
18321 expansion_failed (loc, NULL_RTX,
18322 "Location list where only loc descriptor needed");
18323 return NULL;
18325 return ret->expr;
18328 /* Given a value, round it up to the lowest multiple of `boundary'
18329 which is not less than the value itself. */
18331 static inline HOST_WIDE_INT
18332 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18334 return (((value + boundary - 1) / boundary) * boundary);
18337 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18338 pointer to the declared type for the relevant field variable, or return
18339 `integer_type_node' if the given node turns out to be an
18340 ERROR_MARK node. */
18342 static inline tree
18343 field_type (const_tree decl)
18345 tree type;
18347 if (TREE_CODE (decl) == ERROR_MARK)
18348 return integer_type_node;
18350 type = DECL_BIT_FIELD_TYPE (decl);
18351 if (type == NULL_TREE)
18352 type = TREE_TYPE (decl);
18354 return type;
18357 /* Given a pointer to a tree node, return the alignment in bits for
18358 it, or else return BITS_PER_WORD if the node actually turns out to
18359 be an ERROR_MARK node. */
18361 static inline unsigned
18362 simple_type_align_in_bits (const_tree type)
18364 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18367 static inline unsigned
18368 simple_decl_align_in_bits (const_tree decl)
18370 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18373 /* Return the result of rounding T up to ALIGN. */
18375 static inline offset_int
18376 round_up_to_align (const offset_int &t, unsigned int align)
18378 return wi::udiv_trunc (t + align - 1, align) * align;
18381 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18382 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18383 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18384 if we fail to return the size in one of these two forms. */
18386 static dw_loc_descr_ref
18387 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18389 tree tree_size;
18390 struct loc_descr_context ctx;
18392 /* Return a constant integer in priority, if possible. */
18393 *cst_size = int_size_in_bytes (type);
18394 if (*cst_size != -1)
18395 return NULL;
18397 ctx.context_type = const_cast<tree> (type);
18398 ctx.base_decl = NULL_TREE;
18399 ctx.dpi = NULL;
18400 ctx.placeholder_arg = false;
18401 ctx.placeholder_seen = false;
18403 type = TYPE_MAIN_VARIANT (type);
18404 tree_size = TYPE_SIZE_UNIT (type);
18405 return ((tree_size != NULL_TREE)
18406 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18407 : NULL);
18410 /* Helper structure for RECORD_TYPE processing. */
18411 struct vlr_context
18413 /* Root RECORD_TYPE. It is needed to generate data member location
18414 descriptions in variable-length records (VLR), but also to cope with
18415 variants, which are composed of nested structures multiplexed with
18416 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18417 function processing a FIELD_DECL, it is required to be non null. */
18418 tree struct_type;
18419 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18420 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18421 this variant part as part of the root record (in storage units). For
18422 regular records, it must be NULL_TREE. */
18423 tree variant_part_offset;
18426 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18427 addressed byte of the "containing object" for the given FIELD_DECL. If
18428 possible, return a native constant through CST_OFFSET (in which case NULL is
18429 returned); otherwise return a DWARF expression that computes the offset.
18431 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18432 that offset is, either because the argument turns out to be a pointer to an
18433 ERROR_MARK node, or because the offset expression is too complex for us.
18435 CTX is required: see the comment for VLR_CONTEXT. */
18437 static dw_loc_descr_ref
18438 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18439 HOST_WIDE_INT *cst_offset)
18441 tree tree_result;
18442 dw_loc_list_ref loc_result;
18444 *cst_offset = 0;
18446 if (TREE_CODE (decl) == ERROR_MARK)
18447 return NULL;
18448 else
18449 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18451 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18452 case. */
18453 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18454 return NULL;
18456 #ifdef PCC_BITFIELD_TYPE_MATTERS
18457 /* We used to handle only constant offsets in all cases. Now, we handle
18458 properly dynamic byte offsets only when PCC bitfield type doesn't
18459 matter. */
18460 if (PCC_BITFIELD_TYPE_MATTERS
18461 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18463 offset_int object_offset_in_bits;
18464 offset_int object_offset_in_bytes;
18465 offset_int bitpos_int;
18466 tree type;
18467 tree field_size_tree;
18468 offset_int deepest_bitpos;
18469 offset_int field_size_in_bits;
18470 unsigned int type_align_in_bits;
18471 unsigned int decl_align_in_bits;
18472 offset_int type_size_in_bits;
18474 bitpos_int = wi::to_offset (bit_position (decl));
18475 type = field_type (decl);
18476 type_size_in_bits = offset_int_type_size_in_bits (type);
18477 type_align_in_bits = simple_type_align_in_bits (type);
18479 field_size_tree = DECL_SIZE (decl);
18481 /* The size could be unspecified if there was an error, or for
18482 a flexible array member. */
18483 if (!field_size_tree)
18484 field_size_tree = bitsize_zero_node;
18486 /* If the size of the field is not constant, use the type size. */
18487 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18488 field_size_in_bits = wi::to_offset (field_size_tree);
18489 else
18490 field_size_in_bits = type_size_in_bits;
18492 decl_align_in_bits = simple_decl_align_in_bits (decl);
18494 /* The GCC front-end doesn't make any attempt to keep track of the
18495 starting bit offset (relative to the start of the containing
18496 structure type) of the hypothetical "containing object" for a
18497 bit-field. Thus, when computing the byte offset value for the
18498 start of the "containing object" of a bit-field, we must deduce
18499 this information on our own. This can be rather tricky to do in
18500 some cases. For example, handling the following structure type
18501 definition when compiling for an i386/i486 target (which only
18502 aligns long long's to 32-bit boundaries) can be very tricky:
18504 struct S { int field1; long long field2:31; };
18506 Fortunately, there is a simple rule-of-thumb which can be used
18507 in such cases. When compiling for an i386/i486, GCC will
18508 allocate 8 bytes for the structure shown above. It decides to
18509 do this based upon one simple rule for bit-field allocation.
18510 GCC allocates each "containing object" for each bit-field at
18511 the first (i.e. lowest addressed) legitimate alignment boundary
18512 (based upon the required minimum alignment for the declared
18513 type of the field) which it can possibly use, subject to the
18514 condition that there is still enough available space remaining
18515 in the containing object (when allocated at the selected point)
18516 to fully accommodate all of the bits of the bit-field itself.
18518 This simple rule makes it obvious why GCC allocates 8 bytes for
18519 each object of the structure type shown above. When looking
18520 for a place to allocate the "containing object" for `field2',
18521 the compiler simply tries to allocate a 64-bit "containing
18522 object" at each successive 32-bit boundary (starting at zero)
18523 until it finds a place to allocate that 64- bit field such that
18524 at least 31 contiguous (and previously unallocated) bits remain
18525 within that selected 64 bit field. (As it turns out, for the
18526 example above, the compiler finds it is OK to allocate the
18527 "containing object" 64-bit field at bit-offset zero within the
18528 structure type.)
18530 Here we attempt to work backwards from the limited set of facts
18531 we're given, and we try to deduce from those facts, where GCC
18532 must have believed that the containing object started (within
18533 the structure type). The value we deduce is then used (by the
18534 callers of this routine) to generate DW_AT_location and
18535 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18536 the case of DW_AT_location, regular fields as well). */
18538 /* Figure out the bit-distance from the start of the structure to
18539 the "deepest" bit of the bit-field. */
18540 deepest_bitpos = bitpos_int + field_size_in_bits;
18542 /* This is the tricky part. Use some fancy footwork to deduce
18543 where the lowest addressed bit of the containing object must
18544 be. */
18545 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18547 /* Round up to type_align by default. This works best for
18548 bitfields. */
18549 object_offset_in_bits
18550 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18552 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18554 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18556 /* Round up to decl_align instead. */
18557 object_offset_in_bits
18558 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18561 object_offset_in_bytes
18562 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18563 if (ctx->variant_part_offset == NULL_TREE)
18565 *cst_offset = object_offset_in_bytes.to_shwi ();
18566 return NULL;
18568 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18570 else
18571 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18572 tree_result = byte_position (decl);
18574 if (ctx->variant_part_offset != NULL_TREE)
18575 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18576 ctx->variant_part_offset, tree_result);
18578 /* If the byte offset is a constant, it's simplier to handle a native
18579 constant rather than a DWARF expression. */
18580 if (TREE_CODE (tree_result) == INTEGER_CST)
18582 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18583 return NULL;
18585 struct loc_descr_context loc_ctx = {
18586 ctx->struct_type, /* context_type */
18587 NULL_TREE, /* base_decl */
18588 NULL, /* dpi */
18589 false, /* placeholder_arg */
18590 false /* placeholder_seen */
18592 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18594 /* We want a DWARF expression: abort if we only have a location list with
18595 multiple elements. */
18596 if (!loc_result || !single_element_loc_list_p (loc_result))
18597 return NULL;
18598 else
18599 return loc_result->expr;
18602 /* The following routines define various Dwarf attributes and any data
18603 associated with them. */
18605 /* Add a location description attribute value to a DIE.
18607 This emits location attributes suitable for whole variables and
18608 whole parameters. Note that the location attributes for struct fields are
18609 generated by the routine `data_member_location_attribute' below. */
18611 static inline void
18612 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18613 dw_loc_list_ref descr)
18615 if (descr == 0)
18616 return;
18617 if (single_element_loc_list_p (descr))
18618 add_AT_loc (die, attr_kind, descr->expr);
18619 else
18620 add_AT_loc_list (die, attr_kind, descr);
18623 /* Add DW_AT_accessibility attribute to DIE if needed. */
18625 static void
18626 add_accessibility_attribute (dw_die_ref die, tree decl)
18628 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18629 children, otherwise the default is DW_ACCESS_public. In DWARF2
18630 the default has always been DW_ACCESS_public. */
18631 if (TREE_PROTECTED (decl))
18632 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18633 else if (TREE_PRIVATE (decl))
18635 if (dwarf_version == 2
18636 || die->die_parent == NULL
18637 || die->die_parent->die_tag != DW_TAG_class_type)
18638 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18640 else if (dwarf_version > 2
18641 && die->die_parent
18642 && die->die_parent->die_tag == DW_TAG_class_type)
18643 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18646 /* Attach the specialized form of location attribute used for data members of
18647 struct and union types. In the special case of a FIELD_DECL node which
18648 represents a bit-field, the "offset" part of this special location
18649 descriptor must indicate the distance in bytes from the lowest-addressed
18650 byte of the containing struct or union type to the lowest-addressed byte of
18651 the "containing object" for the bit-field. (See the `field_byte_offset'
18652 function above).
18654 For any given bit-field, the "containing object" is a hypothetical object
18655 (of some integral or enum type) within which the given bit-field lives. The
18656 type of this hypothetical "containing object" is always the same as the
18657 declared type of the individual bit-field itself (for GCC anyway... the
18658 DWARF spec doesn't actually mandate this). Note that it is the size (in
18659 bytes) of the hypothetical "containing object" which will be given in the
18660 DW_AT_byte_size attribute for this bit-field. (See the
18661 `byte_size_attribute' function below.) It is also used when calculating the
18662 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18663 function below.)
18665 CTX is required: see the comment for VLR_CONTEXT. */
18667 static void
18668 add_data_member_location_attribute (dw_die_ref die,
18669 tree decl,
18670 struct vlr_context *ctx)
18672 HOST_WIDE_INT offset;
18673 dw_loc_descr_ref loc_descr = 0;
18675 if (TREE_CODE (decl) == TREE_BINFO)
18677 /* We're working on the TAG_inheritance for a base class. */
18678 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18680 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18681 aren't at a fixed offset from all (sub)objects of the same
18682 type. We need to extract the appropriate offset from our
18683 vtable. The following dwarf expression means
18685 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18687 This is specific to the V3 ABI, of course. */
18689 dw_loc_descr_ref tmp;
18691 /* Make a copy of the object address. */
18692 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18693 add_loc_descr (&loc_descr, tmp);
18695 /* Extract the vtable address. */
18696 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18697 add_loc_descr (&loc_descr, tmp);
18699 /* Calculate the address of the offset. */
18700 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18701 gcc_assert (offset < 0);
18703 tmp = int_loc_descriptor (-offset);
18704 add_loc_descr (&loc_descr, tmp);
18705 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18706 add_loc_descr (&loc_descr, tmp);
18708 /* Extract the offset. */
18709 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18710 add_loc_descr (&loc_descr, tmp);
18712 /* Add it to the object address. */
18713 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18714 add_loc_descr (&loc_descr, tmp);
18716 else
18717 offset = tree_to_shwi (BINFO_OFFSET (decl));
18719 else
18721 loc_descr = field_byte_offset (decl, ctx, &offset);
18723 /* If loc_descr is available then we know the field offset is dynamic.
18724 However, GDB does not handle dynamic field offsets very well at the
18725 moment. */
18726 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18728 loc_descr = NULL;
18729 offset = 0;
18732 /* Data member location evalutation starts with the base address on the
18733 stack. Compute the field offset and add it to this base address. */
18734 else if (loc_descr != NULL)
18735 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18738 if (! loc_descr)
18740 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18741 e.g. GDB only added support to it in November 2016. For DWARF5
18742 we need newer debug info consumers anyway. We might change this
18743 to dwarf_version >= 4 once most consumers catched up. */
18744 if (dwarf_version >= 5
18745 && TREE_CODE (decl) == FIELD_DECL
18746 && DECL_BIT_FIELD_TYPE (decl))
18748 tree off = bit_position (decl);
18749 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18751 remove_AT (die, DW_AT_byte_size);
18752 remove_AT (die, DW_AT_bit_offset);
18753 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18754 return;
18757 if (dwarf_version > 2)
18759 /* Don't need to output a location expression, just the constant. */
18760 if (offset < 0)
18761 add_AT_int (die, DW_AT_data_member_location, offset);
18762 else
18763 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18764 return;
18766 else
18768 enum dwarf_location_atom op;
18770 /* The DWARF2 standard says that we should assume that the structure
18771 address is already on the stack, so we can specify a structure
18772 field address by using DW_OP_plus_uconst. */
18773 op = DW_OP_plus_uconst;
18774 loc_descr = new_loc_descr (op, offset, 0);
18778 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18781 /* Writes integer values to dw_vec_const array. */
18783 static void
18784 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18786 while (size != 0)
18788 *dest++ = val & 0xff;
18789 val >>= 8;
18790 --size;
18794 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18796 static HOST_WIDE_INT
18797 extract_int (const unsigned char *src, unsigned int size)
18799 HOST_WIDE_INT val = 0;
18801 src += size;
18802 while (size != 0)
18804 val <<= 8;
18805 val |= *--src & 0xff;
18806 --size;
18808 return val;
18811 /* Writes wide_int values to dw_vec_const array. */
18813 static void
18814 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18816 int i;
18818 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18820 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18821 return;
18824 /* We'd have to extend this code to support odd sizes. */
18825 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18827 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18829 if (WORDS_BIG_ENDIAN)
18830 for (i = n - 1; i >= 0; i--)
18832 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18833 dest += sizeof (HOST_WIDE_INT);
18835 else
18836 for (i = 0; i < n; i++)
18838 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18839 dest += sizeof (HOST_WIDE_INT);
18843 /* Writes floating point values to dw_vec_const array. */
18845 static void
18846 insert_float (const_rtx rtl, unsigned char *array)
18848 long val[4];
18849 int i;
18851 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18853 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18854 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18856 insert_int (val[i], 4, array);
18857 array += 4;
18861 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18862 does not have a "location" either in memory or in a register. These
18863 things can arise in GNU C when a constant is passed as an actual parameter
18864 to an inlined function. They can also arise in C++ where declared
18865 constants do not necessarily get memory "homes". */
18867 static bool
18868 add_const_value_attribute (dw_die_ref die, rtx rtl)
18870 switch (GET_CODE (rtl))
18872 case CONST_INT:
18874 HOST_WIDE_INT val = INTVAL (rtl);
18876 if (val < 0)
18877 add_AT_int (die, DW_AT_const_value, val);
18878 else
18879 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18881 return true;
18883 case CONST_WIDE_INT:
18885 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18886 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18887 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18888 wide_int w = wi::zext (w1, prec);
18889 add_AT_wide (die, DW_AT_const_value, w);
18891 return true;
18893 case CONST_DOUBLE:
18894 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18895 floating-point constant. A CONST_DOUBLE is used whenever the
18896 constant requires more than one word in order to be adequately
18897 represented. */
18899 machine_mode mode = GET_MODE (rtl);
18901 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18902 add_AT_double (die, DW_AT_const_value,
18903 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18904 else
18906 unsigned int length = GET_MODE_SIZE (mode);
18907 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18909 insert_float (rtl, array);
18910 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18913 return true;
18915 case CONST_VECTOR:
18917 machine_mode mode = GET_MODE (rtl);
18918 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18919 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18920 unsigned char *array
18921 = ggc_vec_alloc<unsigned char> (length * elt_size);
18922 unsigned int i;
18923 unsigned char *p;
18924 machine_mode imode = GET_MODE_INNER (mode);
18926 switch (GET_MODE_CLASS (mode))
18928 case MODE_VECTOR_INT:
18929 for (i = 0, p = array; i < length; i++, p += elt_size)
18931 rtx elt = CONST_VECTOR_ELT (rtl, i);
18932 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18934 break;
18936 case MODE_VECTOR_FLOAT:
18937 for (i = 0, p = array; i < length; i++, p += elt_size)
18939 rtx elt = CONST_VECTOR_ELT (rtl, i);
18940 insert_float (elt, p);
18942 break;
18944 default:
18945 gcc_unreachable ();
18948 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18950 return true;
18952 case CONST_STRING:
18953 if (dwarf_version >= 4 || !dwarf_strict)
18955 dw_loc_descr_ref loc_result;
18956 resolve_one_addr (&rtl);
18957 rtl_addr:
18958 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18959 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18960 add_AT_loc (die, DW_AT_location, loc_result);
18961 vec_safe_push (used_rtx_array, rtl);
18962 return true;
18964 return false;
18966 case CONST:
18967 if (CONSTANT_P (XEXP (rtl, 0)))
18968 return add_const_value_attribute (die, XEXP (rtl, 0));
18969 /* FALLTHROUGH */
18970 case SYMBOL_REF:
18971 if (!const_ok_for_output (rtl))
18972 return false;
18973 /* FALLTHROUGH */
18974 case LABEL_REF:
18975 if (dwarf_version >= 4 || !dwarf_strict)
18976 goto rtl_addr;
18977 return false;
18979 case PLUS:
18980 /* In cases where an inlined instance of an inline function is passed
18981 the address of an `auto' variable (which is local to the caller) we
18982 can get a situation where the DECL_RTL of the artificial local
18983 variable (for the inlining) which acts as a stand-in for the
18984 corresponding formal parameter (of the inline function) will look
18985 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18986 exactly a compile-time constant expression, but it isn't the address
18987 of the (artificial) local variable either. Rather, it represents the
18988 *value* which the artificial local variable always has during its
18989 lifetime. We currently have no way to represent such quasi-constant
18990 values in Dwarf, so for now we just punt and generate nothing. */
18991 return false;
18993 case HIGH:
18994 case CONST_FIXED:
18995 return false;
18997 case MEM:
18998 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18999 && MEM_READONLY_P (rtl)
19000 && GET_MODE (rtl) == BLKmode)
19002 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19003 return true;
19005 return false;
19007 default:
19008 /* No other kinds of rtx should be possible here. */
19009 gcc_unreachable ();
19011 return false;
19014 /* Determine whether the evaluation of EXPR references any variables
19015 or functions which aren't otherwise used (and therefore may not be
19016 output). */
19017 static tree
19018 reference_to_unused (tree * tp, int * walk_subtrees,
19019 void * data ATTRIBUTE_UNUSED)
19021 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19022 *walk_subtrees = 0;
19024 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19025 && ! TREE_ASM_WRITTEN (*tp))
19026 return *tp;
19027 /* ??? The C++ FE emits debug information for using decls, so
19028 putting gcc_unreachable here falls over. See PR31899. For now
19029 be conservative. */
19030 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19031 return *tp;
19032 else if (VAR_P (*tp))
19034 varpool_node *node = varpool_node::get (*tp);
19035 if (!node || !node->definition)
19036 return *tp;
19038 else if (TREE_CODE (*tp) == FUNCTION_DECL
19039 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19041 /* The call graph machinery must have finished analyzing,
19042 optimizing and gimplifying the CU by now.
19043 So if *TP has no call graph node associated
19044 to it, it means *TP will not be emitted. */
19045 if (!cgraph_node::get (*tp))
19046 return *tp;
19048 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19049 return *tp;
19051 return NULL_TREE;
19054 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19055 for use in a later add_const_value_attribute call. */
19057 static rtx
19058 rtl_for_decl_init (tree init, tree type)
19060 rtx rtl = NULL_RTX;
19062 STRIP_NOPS (init);
19064 /* If a variable is initialized with a string constant without embedded
19065 zeros, build CONST_STRING. */
19066 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19068 tree enttype = TREE_TYPE (type);
19069 tree domain = TYPE_DOMAIN (type);
19070 machine_mode mode = TYPE_MODE (enttype);
19072 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
19073 && domain
19074 && integer_zerop (TYPE_MIN_VALUE (domain))
19075 && compare_tree_int (TYPE_MAX_VALUE (domain),
19076 TREE_STRING_LENGTH (init) - 1) == 0
19077 && ((size_t) TREE_STRING_LENGTH (init)
19078 == strlen (TREE_STRING_POINTER (init)) + 1))
19080 rtl = gen_rtx_CONST_STRING (VOIDmode,
19081 ggc_strdup (TREE_STRING_POINTER (init)));
19082 rtl = gen_rtx_MEM (BLKmode, rtl);
19083 MEM_READONLY_P (rtl) = 1;
19086 /* Other aggregates, and complex values, could be represented using
19087 CONCAT: FIXME! */
19088 else if (AGGREGATE_TYPE_P (type)
19089 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19090 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19091 || TREE_CODE (type) == COMPLEX_TYPE)
19093 /* Vectors only work if their mode is supported by the target.
19094 FIXME: generic vectors ought to work too. */
19095 else if (TREE_CODE (type) == VECTOR_TYPE
19096 && !VECTOR_MODE_P (TYPE_MODE (type)))
19098 /* If the initializer is something that we know will expand into an
19099 immediate RTL constant, expand it now. We must be careful not to
19100 reference variables which won't be output. */
19101 else if (initializer_constant_valid_p (init, type)
19102 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19104 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19105 possible. */
19106 if (TREE_CODE (type) == VECTOR_TYPE)
19107 switch (TREE_CODE (init))
19109 case VECTOR_CST:
19110 break;
19111 case CONSTRUCTOR:
19112 if (TREE_CONSTANT (init))
19114 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19115 bool constant_p = true;
19116 tree value;
19117 unsigned HOST_WIDE_INT ix;
19119 /* Even when ctor is constant, it might contain non-*_CST
19120 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19121 belong into VECTOR_CST nodes. */
19122 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19123 if (!CONSTANT_CLASS_P (value))
19125 constant_p = false;
19126 break;
19129 if (constant_p)
19131 init = build_vector_from_ctor (type, elts);
19132 break;
19135 /* FALLTHRU */
19137 default:
19138 return NULL;
19141 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19143 /* If expand_expr returns a MEM, it wasn't immediate. */
19144 gcc_assert (!rtl || !MEM_P (rtl));
19147 return rtl;
19150 /* Generate RTL for the variable DECL to represent its location. */
19152 static rtx
19153 rtl_for_decl_location (tree decl)
19155 rtx rtl;
19157 /* Here we have to decide where we are going to say the parameter "lives"
19158 (as far as the debugger is concerned). We only have a couple of
19159 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19161 DECL_RTL normally indicates where the parameter lives during most of the
19162 activation of the function. If optimization is enabled however, this
19163 could be either NULL or else a pseudo-reg. Both of those cases indicate
19164 that the parameter doesn't really live anywhere (as far as the code
19165 generation parts of GCC are concerned) during most of the function's
19166 activation. That will happen (for example) if the parameter is never
19167 referenced within the function.
19169 We could just generate a location descriptor here for all non-NULL
19170 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19171 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19172 where DECL_RTL is NULL or is a pseudo-reg.
19174 Note however that we can only get away with using DECL_INCOMING_RTL as
19175 a backup substitute for DECL_RTL in certain limited cases. In cases
19176 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19177 we can be sure that the parameter was passed using the same type as it is
19178 declared to have within the function, and that its DECL_INCOMING_RTL
19179 points us to a place where a value of that type is passed.
19181 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19182 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19183 because in these cases DECL_INCOMING_RTL points us to a value of some
19184 type which is *different* from the type of the parameter itself. Thus,
19185 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19186 such cases, the debugger would end up (for example) trying to fetch a
19187 `float' from a place which actually contains the first part of a
19188 `double'. That would lead to really incorrect and confusing
19189 output at debug-time.
19191 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19192 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19193 are a couple of exceptions however. On little-endian machines we can
19194 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19195 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19196 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19197 when (on a little-endian machine) a non-prototyped function has a
19198 parameter declared to be of type `short' or `char'. In such cases,
19199 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19200 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19201 passed `int' value. If the debugger then uses that address to fetch
19202 a `short' or a `char' (on a little-endian machine) the result will be
19203 the correct data, so we allow for such exceptional cases below.
19205 Note that our goal here is to describe the place where the given formal
19206 parameter lives during most of the function's activation (i.e. between the
19207 end of the prologue and the start of the epilogue). We'll do that as best
19208 as we can. Note however that if the given formal parameter is modified
19209 sometime during the execution of the function, then a stack backtrace (at
19210 debug-time) will show the function as having been called with the *new*
19211 value rather than the value which was originally passed in. This happens
19212 rarely enough that it is not a major problem, but it *is* a problem, and
19213 I'd like to fix it.
19215 A future version of dwarf2out.c may generate two additional attributes for
19216 any given DW_TAG_formal_parameter DIE which will describe the "passed
19217 type" and the "passed location" for the given formal parameter in addition
19218 to the attributes we now generate to indicate the "declared type" and the
19219 "active location" for each parameter. This additional set of attributes
19220 could be used by debuggers for stack backtraces. Separately, note that
19221 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19222 This happens (for example) for inlined-instances of inline function formal
19223 parameters which are never referenced. This really shouldn't be
19224 happening. All PARM_DECL nodes should get valid non-NULL
19225 DECL_INCOMING_RTL values. FIXME. */
19227 /* Use DECL_RTL as the "location" unless we find something better. */
19228 rtl = DECL_RTL_IF_SET (decl);
19230 /* When generating abstract instances, ignore everything except
19231 constants, symbols living in memory, and symbols living in
19232 fixed registers. */
19233 if (! reload_completed)
19235 if (rtl
19236 && (CONSTANT_P (rtl)
19237 || (MEM_P (rtl)
19238 && CONSTANT_P (XEXP (rtl, 0)))
19239 || (REG_P (rtl)
19240 && VAR_P (decl)
19241 && TREE_STATIC (decl))))
19243 rtl = targetm.delegitimize_address (rtl);
19244 return rtl;
19246 rtl = NULL_RTX;
19248 else if (TREE_CODE (decl) == PARM_DECL)
19250 if (rtl == NULL_RTX
19251 || is_pseudo_reg (rtl)
19252 || (MEM_P (rtl)
19253 && is_pseudo_reg (XEXP (rtl, 0))
19254 && DECL_INCOMING_RTL (decl)
19255 && MEM_P (DECL_INCOMING_RTL (decl))
19256 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19258 tree declared_type = TREE_TYPE (decl);
19259 tree passed_type = DECL_ARG_TYPE (decl);
19260 machine_mode dmode = TYPE_MODE (declared_type);
19261 machine_mode pmode = TYPE_MODE (passed_type);
19263 /* This decl represents a formal parameter which was optimized out.
19264 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19265 all cases where (rtl == NULL_RTX) just below. */
19266 if (dmode == pmode)
19267 rtl = DECL_INCOMING_RTL (decl);
19268 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19269 && SCALAR_INT_MODE_P (dmode)
19270 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19271 && DECL_INCOMING_RTL (decl))
19273 rtx inc = DECL_INCOMING_RTL (decl);
19274 if (REG_P (inc))
19275 rtl = inc;
19276 else if (MEM_P (inc))
19278 if (BYTES_BIG_ENDIAN)
19279 rtl = adjust_address_nv (inc, dmode,
19280 GET_MODE_SIZE (pmode)
19281 - GET_MODE_SIZE (dmode));
19282 else
19283 rtl = inc;
19288 /* If the parm was passed in registers, but lives on the stack, then
19289 make a big endian correction if the mode of the type of the
19290 parameter is not the same as the mode of the rtl. */
19291 /* ??? This is the same series of checks that are made in dbxout.c before
19292 we reach the big endian correction code there. It isn't clear if all
19293 of these checks are necessary here, but keeping them all is the safe
19294 thing to do. */
19295 else if (MEM_P (rtl)
19296 && XEXP (rtl, 0) != const0_rtx
19297 && ! CONSTANT_P (XEXP (rtl, 0))
19298 /* Not passed in memory. */
19299 && !MEM_P (DECL_INCOMING_RTL (decl))
19300 /* Not passed by invisible reference. */
19301 && (!REG_P (XEXP (rtl, 0))
19302 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19303 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19304 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19305 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19306 #endif
19308 /* Big endian correction check. */
19309 && BYTES_BIG_ENDIAN
19310 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19311 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19312 < UNITS_PER_WORD))
19314 machine_mode addr_mode = get_address_mode (rtl);
19315 int offset = (UNITS_PER_WORD
19316 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19318 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19319 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19322 else if (VAR_P (decl)
19323 && rtl
19324 && MEM_P (rtl)
19325 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
19326 && BYTES_BIG_ENDIAN)
19328 machine_mode addr_mode = get_address_mode (rtl);
19329 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
19330 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
19332 /* If a variable is declared "register" yet is smaller than
19333 a register, then if we store the variable to memory, it
19334 looks like we're storing a register-sized value, when in
19335 fact we are not. We need to adjust the offset of the
19336 storage location to reflect the actual value's bytes,
19337 else gdb will not be able to display it. */
19338 if (rsize > dsize)
19339 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19340 plus_constant (addr_mode, XEXP (rtl, 0),
19341 rsize - dsize));
19344 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19345 and will have been substituted directly into all expressions that use it.
19346 C does not have such a concept, but C++ and other languages do. */
19347 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19348 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19350 if (rtl)
19351 rtl = targetm.delegitimize_address (rtl);
19353 /* If we don't look past the constant pool, we risk emitting a
19354 reference to a constant pool entry that isn't referenced from
19355 code, and thus is not emitted. */
19356 if (rtl)
19357 rtl = avoid_constant_pool_reference (rtl);
19359 /* Try harder to get a rtl. If this symbol ends up not being emitted
19360 in the current CU, resolve_addr will remove the expression referencing
19361 it. */
19362 if (rtl == NULL_RTX
19363 && VAR_P (decl)
19364 && !DECL_EXTERNAL (decl)
19365 && TREE_STATIC (decl)
19366 && DECL_NAME (decl)
19367 && !DECL_HARD_REGISTER (decl)
19368 && DECL_MODE (decl) != VOIDmode)
19370 rtl = make_decl_rtl_for_debug (decl);
19371 if (!MEM_P (rtl)
19372 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19373 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19374 rtl = NULL_RTX;
19377 return rtl;
19380 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19381 returned. If so, the decl for the COMMON block is returned, and the
19382 value is the offset into the common block for the symbol. */
19384 static tree
19385 fortran_common (tree decl, HOST_WIDE_INT *value)
19387 tree val_expr, cvar;
19388 machine_mode mode;
19389 HOST_WIDE_INT bitsize, bitpos;
19390 tree offset;
19391 int unsignedp, reversep, volatilep = 0;
19393 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19394 it does not have a value (the offset into the common area), or if it
19395 is thread local (as opposed to global) then it isn't common, and shouldn't
19396 be handled as such. */
19397 if (!VAR_P (decl)
19398 || !TREE_STATIC (decl)
19399 || !DECL_HAS_VALUE_EXPR_P (decl)
19400 || !is_fortran ())
19401 return NULL_TREE;
19403 val_expr = DECL_VALUE_EXPR (decl);
19404 if (TREE_CODE (val_expr) != COMPONENT_REF)
19405 return NULL_TREE;
19407 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19408 &unsignedp, &reversep, &volatilep);
19410 if (cvar == NULL_TREE
19411 || !VAR_P (cvar)
19412 || DECL_ARTIFICIAL (cvar)
19413 || !TREE_PUBLIC (cvar))
19414 return NULL_TREE;
19416 *value = 0;
19417 if (offset != NULL)
19419 if (!tree_fits_shwi_p (offset))
19420 return NULL_TREE;
19421 *value = tree_to_shwi (offset);
19423 if (bitpos != 0)
19424 *value += bitpos / BITS_PER_UNIT;
19426 return cvar;
19429 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19430 data attribute for a variable or a parameter. We generate the
19431 DW_AT_const_value attribute only in those cases where the given variable
19432 or parameter does not have a true "location" either in memory or in a
19433 register. This can happen (for example) when a constant is passed as an
19434 actual argument in a call to an inline function. (It's possible that
19435 these things can crop up in other ways also.) Note that one type of
19436 constant value which can be passed into an inlined function is a constant
19437 pointer. This can happen for example if an actual argument in an inlined
19438 function call evaluates to a compile-time constant address.
19440 CACHE_P is true if it is worth caching the location list for DECL,
19441 so that future calls can reuse it rather than regenerate it from scratch.
19442 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19443 since we will need to refer to them each time the function is inlined. */
19445 static bool
19446 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19448 rtx rtl;
19449 dw_loc_list_ref list;
19450 var_loc_list *loc_list;
19451 cached_dw_loc_list *cache;
19453 if (early_dwarf)
19454 return false;
19456 if (TREE_CODE (decl) == ERROR_MARK)
19457 return false;
19459 if (get_AT (die, DW_AT_location)
19460 || get_AT (die, DW_AT_const_value))
19461 return true;
19463 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19464 || TREE_CODE (decl) == RESULT_DECL);
19466 /* Try to get some constant RTL for this decl, and use that as the value of
19467 the location. */
19469 rtl = rtl_for_decl_location (decl);
19470 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19471 && add_const_value_attribute (die, rtl))
19472 return true;
19474 /* See if we have single element location list that is equivalent to
19475 a constant value. That way we are better to use add_const_value_attribute
19476 rather than expanding constant value equivalent. */
19477 loc_list = lookup_decl_loc (decl);
19478 if (loc_list
19479 && loc_list->first
19480 && loc_list->first->next == NULL
19481 && NOTE_P (loc_list->first->loc)
19482 && NOTE_VAR_LOCATION (loc_list->first->loc)
19483 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19485 struct var_loc_node *node;
19487 node = loc_list->first;
19488 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19489 if (GET_CODE (rtl) == EXPR_LIST)
19490 rtl = XEXP (rtl, 0);
19491 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19492 && add_const_value_attribute (die, rtl))
19493 return true;
19495 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19496 list several times. See if we've already cached the contents. */
19497 list = NULL;
19498 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19499 cache_p = false;
19500 if (cache_p)
19502 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19503 if (cache)
19504 list = cache->loc_list;
19506 if (list == NULL)
19508 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19509 NULL);
19510 /* It is usually worth caching this result if the decl is from
19511 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19512 if (cache_p && list && list->dw_loc_next)
19514 cached_dw_loc_list **slot
19515 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19516 DECL_UID (decl),
19517 INSERT);
19518 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19519 cache->decl_id = DECL_UID (decl);
19520 cache->loc_list = list;
19521 *slot = cache;
19524 if (list)
19526 add_AT_location_description (die, DW_AT_location, list);
19527 return true;
19529 /* None of that worked, so it must not really have a location;
19530 try adding a constant value attribute from the DECL_INITIAL. */
19531 return tree_add_const_value_attribute_for_decl (die, decl);
19534 /* Helper function for tree_add_const_value_attribute. Natively encode
19535 initializer INIT into an array. Return true if successful. */
19537 static bool
19538 native_encode_initializer (tree init, unsigned char *array, int size)
19540 tree type;
19542 if (init == NULL_TREE)
19543 return false;
19545 STRIP_NOPS (init);
19546 switch (TREE_CODE (init))
19548 case STRING_CST:
19549 type = TREE_TYPE (init);
19550 if (TREE_CODE (type) == ARRAY_TYPE)
19552 tree enttype = TREE_TYPE (type);
19553 machine_mode mode = TYPE_MODE (enttype);
19555 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19556 return false;
19557 if (int_size_in_bytes (type) != size)
19558 return false;
19559 if (size > TREE_STRING_LENGTH (init))
19561 memcpy (array, TREE_STRING_POINTER (init),
19562 TREE_STRING_LENGTH (init));
19563 memset (array + TREE_STRING_LENGTH (init),
19564 '\0', size - TREE_STRING_LENGTH (init));
19566 else
19567 memcpy (array, TREE_STRING_POINTER (init), size);
19568 return true;
19570 return false;
19571 case CONSTRUCTOR:
19572 type = TREE_TYPE (init);
19573 if (int_size_in_bytes (type) != size)
19574 return false;
19575 if (TREE_CODE (type) == ARRAY_TYPE)
19577 HOST_WIDE_INT min_index;
19578 unsigned HOST_WIDE_INT cnt;
19579 int curpos = 0, fieldsize;
19580 constructor_elt *ce;
19582 if (TYPE_DOMAIN (type) == NULL_TREE
19583 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19584 return false;
19586 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19587 if (fieldsize <= 0)
19588 return false;
19590 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19591 memset (array, '\0', size);
19592 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19594 tree val = ce->value;
19595 tree index = ce->index;
19596 int pos = curpos;
19597 if (index && TREE_CODE (index) == RANGE_EXPR)
19598 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19599 * fieldsize;
19600 else if (index)
19601 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19603 if (val)
19605 STRIP_NOPS (val);
19606 if (!native_encode_initializer (val, array + pos, fieldsize))
19607 return false;
19609 curpos = pos + fieldsize;
19610 if (index && TREE_CODE (index) == RANGE_EXPR)
19612 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19613 - tree_to_shwi (TREE_OPERAND (index, 0));
19614 while (count-- > 0)
19616 if (val)
19617 memcpy (array + curpos, array + pos, fieldsize);
19618 curpos += fieldsize;
19621 gcc_assert (curpos <= size);
19623 return true;
19625 else if (TREE_CODE (type) == RECORD_TYPE
19626 || TREE_CODE (type) == UNION_TYPE)
19628 tree field = NULL_TREE;
19629 unsigned HOST_WIDE_INT cnt;
19630 constructor_elt *ce;
19632 if (int_size_in_bytes (type) != size)
19633 return false;
19635 if (TREE_CODE (type) == RECORD_TYPE)
19636 field = TYPE_FIELDS (type);
19638 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19640 tree val = ce->value;
19641 int pos, fieldsize;
19643 if (ce->index != 0)
19644 field = ce->index;
19646 if (val)
19647 STRIP_NOPS (val);
19649 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19650 return false;
19652 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19653 && TYPE_DOMAIN (TREE_TYPE (field))
19654 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19655 return false;
19656 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19657 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19658 return false;
19659 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19660 pos = int_byte_position (field);
19661 gcc_assert (pos + fieldsize <= size);
19662 if (val && fieldsize != 0
19663 && !native_encode_initializer (val, array + pos, fieldsize))
19664 return false;
19666 return true;
19668 return false;
19669 case VIEW_CONVERT_EXPR:
19670 case NON_LVALUE_EXPR:
19671 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19672 default:
19673 return native_encode_expr (init, array, size) == size;
19677 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19678 attribute is the const value T. */
19680 static bool
19681 tree_add_const_value_attribute (dw_die_ref die, tree t)
19683 tree init;
19684 tree type = TREE_TYPE (t);
19685 rtx rtl;
19687 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19688 return false;
19690 init = t;
19691 gcc_assert (!DECL_P (init));
19693 if (! early_dwarf)
19695 rtl = rtl_for_decl_init (init, type);
19696 if (rtl)
19697 return add_const_value_attribute (die, rtl);
19699 /* If the host and target are sane, try harder. */
19700 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19701 && initializer_constant_valid_p (init, type))
19703 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19704 if (size > 0 && (int) size == size)
19706 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19708 if (native_encode_initializer (init, array, size))
19710 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19711 return true;
19713 ggc_free (array);
19716 return false;
19719 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19720 attribute is the const value of T, where T is an integral constant
19721 variable with static storage duration
19722 (so it can't be a PARM_DECL or a RESULT_DECL). */
19724 static bool
19725 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19728 if (!decl
19729 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19730 || (VAR_P (decl) && !TREE_STATIC (decl)))
19731 return false;
19733 if (TREE_READONLY (decl)
19734 && ! TREE_THIS_VOLATILE (decl)
19735 && DECL_INITIAL (decl))
19736 /* OK */;
19737 else
19738 return false;
19740 /* Don't add DW_AT_const_value if abstract origin already has one. */
19741 if (get_AT (var_die, DW_AT_const_value))
19742 return false;
19744 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19747 /* Convert the CFI instructions for the current function into a
19748 location list. This is used for DW_AT_frame_base when we targeting
19749 a dwarf2 consumer that does not support the dwarf3
19750 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19751 expressions. */
19753 static dw_loc_list_ref
19754 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19756 int ix;
19757 dw_fde_ref fde;
19758 dw_loc_list_ref list, *list_tail;
19759 dw_cfi_ref cfi;
19760 dw_cfa_location last_cfa, next_cfa;
19761 const char *start_label, *last_label, *section;
19762 dw_cfa_location remember;
19764 fde = cfun->fde;
19765 gcc_assert (fde != NULL);
19767 section = secname_for_decl (current_function_decl);
19768 list_tail = &list;
19769 list = NULL;
19771 memset (&next_cfa, 0, sizeof (next_cfa));
19772 next_cfa.reg = INVALID_REGNUM;
19773 remember = next_cfa;
19775 start_label = fde->dw_fde_begin;
19777 /* ??? Bald assumption that the CIE opcode list does not contain
19778 advance opcodes. */
19779 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19780 lookup_cfa_1 (cfi, &next_cfa, &remember);
19782 last_cfa = next_cfa;
19783 last_label = start_label;
19785 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19787 /* If the first partition contained no CFI adjustments, the
19788 CIE opcodes apply to the whole first partition. */
19789 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19790 fde->dw_fde_begin, fde->dw_fde_end, section);
19791 list_tail =&(*list_tail)->dw_loc_next;
19792 start_label = last_label = fde->dw_fde_second_begin;
19795 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19797 switch (cfi->dw_cfi_opc)
19799 case DW_CFA_set_loc:
19800 case DW_CFA_advance_loc1:
19801 case DW_CFA_advance_loc2:
19802 case DW_CFA_advance_loc4:
19803 if (!cfa_equal_p (&last_cfa, &next_cfa))
19805 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19806 start_label, last_label, section);
19808 list_tail = &(*list_tail)->dw_loc_next;
19809 last_cfa = next_cfa;
19810 start_label = last_label;
19812 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19813 break;
19815 case DW_CFA_advance_loc:
19816 /* The encoding is complex enough that we should never emit this. */
19817 gcc_unreachable ();
19819 default:
19820 lookup_cfa_1 (cfi, &next_cfa, &remember);
19821 break;
19823 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19825 if (!cfa_equal_p (&last_cfa, &next_cfa))
19827 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19828 start_label, last_label, section);
19830 list_tail = &(*list_tail)->dw_loc_next;
19831 last_cfa = next_cfa;
19832 start_label = last_label;
19834 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19835 start_label, fde->dw_fde_end, section);
19836 list_tail = &(*list_tail)->dw_loc_next;
19837 start_label = last_label = fde->dw_fde_second_begin;
19841 if (!cfa_equal_p (&last_cfa, &next_cfa))
19843 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19844 start_label, last_label, section);
19845 list_tail = &(*list_tail)->dw_loc_next;
19846 start_label = last_label;
19849 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19850 start_label,
19851 fde->dw_fde_second_begin
19852 ? fde->dw_fde_second_end : fde->dw_fde_end,
19853 section);
19855 if (list && list->dw_loc_next)
19856 gen_llsym (list);
19858 return list;
19861 /* Compute a displacement from the "steady-state frame pointer" to the
19862 frame base (often the same as the CFA), and store it in
19863 frame_pointer_fb_offset. OFFSET is added to the displacement
19864 before the latter is negated. */
19866 static void
19867 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19869 rtx reg, elim;
19871 #ifdef FRAME_POINTER_CFA_OFFSET
19872 reg = frame_pointer_rtx;
19873 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19874 #else
19875 reg = arg_pointer_rtx;
19876 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19877 #endif
19879 elim = (ira_use_lra_p
19880 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19881 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19882 if (GET_CODE (elim) == PLUS)
19884 offset += INTVAL (XEXP (elim, 1));
19885 elim = XEXP (elim, 0);
19888 frame_pointer_fb_offset = -offset;
19890 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19891 in which to eliminate. This is because it's stack pointer isn't
19892 directly accessible as a register within the ISA. To work around
19893 this, assume that while we cannot provide a proper value for
19894 frame_pointer_fb_offset, we won't need one either. */
19895 frame_pointer_fb_offset_valid
19896 = ((SUPPORTS_STACK_ALIGNMENT
19897 && (elim == hard_frame_pointer_rtx
19898 || elim == stack_pointer_rtx))
19899 || elim == (frame_pointer_needed
19900 ? hard_frame_pointer_rtx
19901 : stack_pointer_rtx));
19904 /* Generate a DW_AT_name attribute given some string value to be included as
19905 the value of the attribute. */
19907 static void
19908 add_name_attribute (dw_die_ref die, const char *name_string)
19910 if (name_string != NULL && *name_string != 0)
19912 if (demangle_name_func)
19913 name_string = (*demangle_name_func) (name_string);
19915 add_AT_string (die, DW_AT_name, name_string);
19919 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19920 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19921 of TYPE accordingly.
19923 ??? This is a temporary measure until after we're able to generate
19924 regular DWARF for the complex Ada type system. */
19926 static void
19927 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19928 dw_die_ref context_die)
19930 tree dtype;
19931 dw_die_ref dtype_die;
19933 if (!lang_hooks.types.descriptive_type)
19934 return;
19936 dtype = lang_hooks.types.descriptive_type (type);
19937 if (!dtype)
19938 return;
19940 dtype_die = lookup_type_die (dtype);
19941 if (!dtype_die)
19943 gen_type_die (dtype, context_die);
19944 dtype_die = lookup_type_die (dtype);
19945 gcc_assert (dtype_die);
19948 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19951 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19953 static const char *
19954 comp_dir_string (void)
19956 const char *wd;
19957 char *wd1;
19958 static const char *cached_wd = NULL;
19960 if (cached_wd != NULL)
19961 return cached_wd;
19963 wd = get_src_pwd ();
19964 if (wd == NULL)
19965 return NULL;
19967 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19969 int wdlen;
19971 wdlen = strlen (wd);
19972 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19973 strcpy (wd1, wd);
19974 wd1 [wdlen] = DIR_SEPARATOR;
19975 wd1 [wdlen + 1] = 0;
19976 wd = wd1;
19979 cached_wd = remap_debug_filename (wd);
19980 return cached_wd;
19983 /* Generate a DW_AT_comp_dir attribute for DIE. */
19985 static void
19986 add_comp_dir_attribute (dw_die_ref die)
19988 const char * wd = comp_dir_string ();
19989 if (wd != NULL)
19990 add_AT_string (die, DW_AT_comp_dir, wd);
19993 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19994 pointer computation, ...), output a representation for that bound according
19995 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19996 loc_list_from_tree for the meaning of CONTEXT. */
19998 static void
19999 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20000 int forms, struct loc_descr_context *context)
20002 dw_die_ref context_die, decl_die;
20003 dw_loc_list_ref list;
20004 bool strip_conversions = true;
20005 bool placeholder_seen = false;
20007 while (strip_conversions)
20008 switch (TREE_CODE (value))
20010 case ERROR_MARK:
20011 case SAVE_EXPR:
20012 return;
20014 CASE_CONVERT:
20015 case VIEW_CONVERT_EXPR:
20016 value = TREE_OPERAND (value, 0);
20017 break;
20019 default:
20020 strip_conversions = false;
20021 break;
20024 /* If possible and permitted, output the attribute as a constant. */
20025 if ((forms & dw_scalar_form_constant) != 0
20026 && TREE_CODE (value) == INTEGER_CST)
20028 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20030 /* If HOST_WIDE_INT is big enough then represent the bound as
20031 a constant value. We need to choose a form based on
20032 whether the type is signed or unsigned. We cannot just
20033 call add_AT_unsigned if the value itself is positive
20034 (add_AT_unsigned might add the unsigned value encoded as
20035 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20036 bounds type and then sign extend any unsigned values found
20037 for signed types. This is needed only for
20038 DW_AT_{lower,upper}_bound, since for most other attributes,
20039 consumers will treat DW_FORM_data[1248] as unsigned values,
20040 regardless of the underlying type. */
20041 if (prec <= HOST_BITS_PER_WIDE_INT
20042 || tree_fits_uhwi_p (value))
20044 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20045 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20046 else
20047 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20049 else
20050 /* Otherwise represent the bound as an unsigned value with
20051 the precision of its type. The precision and signedness
20052 of the type will be necessary to re-interpret it
20053 unambiguously. */
20054 add_AT_wide (die, attr, value);
20055 return;
20058 /* Otherwise, if it's possible and permitted too, output a reference to
20059 another DIE. */
20060 if ((forms & dw_scalar_form_reference) != 0)
20062 tree decl = NULL_TREE;
20064 /* Some type attributes reference an outer type. For instance, the upper
20065 bound of an array may reference an embedding record (this happens in
20066 Ada). */
20067 if (TREE_CODE (value) == COMPONENT_REF
20068 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20069 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20070 decl = TREE_OPERAND (value, 1);
20072 else if (VAR_P (value)
20073 || TREE_CODE (value) == PARM_DECL
20074 || TREE_CODE (value) == RESULT_DECL)
20075 decl = value;
20077 if (decl != NULL_TREE)
20079 dw_die_ref decl_die = lookup_decl_die (decl);
20081 /* ??? Can this happen, or should the variable have been bound
20082 first? Probably it can, since I imagine that we try to create
20083 the types of parameters in the order in which they exist in
20084 the list, and won't have created a forward reference to a
20085 later parameter. */
20086 if (decl_die != NULL)
20088 add_AT_die_ref (die, attr, decl_die);
20089 return;
20094 /* Last chance: try to create a stack operation procedure to evaluate the
20095 value. Do nothing if even that is not possible or permitted. */
20096 if ((forms & dw_scalar_form_exprloc) == 0)
20097 return;
20099 list = loc_list_from_tree (value, 2, context);
20100 if (context && context->placeholder_arg)
20102 placeholder_seen = context->placeholder_seen;
20103 context->placeholder_seen = false;
20105 if (list == NULL || single_element_loc_list_p (list))
20107 /* If this attribute is not a reference nor constant, it is
20108 a DWARF expression rather than location description. For that
20109 loc_list_from_tree (value, 0, &context) is needed. */
20110 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20111 if (list2 && single_element_loc_list_p (list2))
20113 if (placeholder_seen)
20115 struct dwarf_procedure_info dpi;
20116 dpi.fndecl = NULL_TREE;
20117 dpi.args_count = 1;
20118 if (!resolve_args_picking (list2->expr, 1, &dpi))
20119 return;
20121 add_AT_loc (die, attr, list2->expr);
20122 return;
20126 /* If that failed to give a single element location list, fall back to
20127 outputting this as a reference... still if permitted. */
20128 if (list == NULL
20129 || (forms & dw_scalar_form_reference) == 0
20130 || placeholder_seen)
20131 return;
20133 if (current_function_decl == 0)
20134 context_die = comp_unit_die ();
20135 else
20136 context_die = lookup_decl_die (current_function_decl);
20138 decl_die = new_die (DW_TAG_variable, context_die, value);
20139 add_AT_flag (decl_die, DW_AT_artificial, 1);
20140 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20141 context_die);
20142 add_AT_location_description (decl_die, DW_AT_location, list);
20143 add_AT_die_ref (die, attr, decl_die);
20146 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20147 default. */
20149 static int
20150 lower_bound_default (void)
20152 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20154 case DW_LANG_C:
20155 case DW_LANG_C89:
20156 case DW_LANG_C99:
20157 case DW_LANG_C11:
20158 case DW_LANG_C_plus_plus:
20159 case DW_LANG_C_plus_plus_11:
20160 case DW_LANG_C_plus_plus_14:
20161 case DW_LANG_ObjC:
20162 case DW_LANG_ObjC_plus_plus:
20163 return 0;
20164 case DW_LANG_Fortran77:
20165 case DW_LANG_Fortran90:
20166 case DW_LANG_Fortran95:
20167 case DW_LANG_Fortran03:
20168 case DW_LANG_Fortran08:
20169 return 1;
20170 case DW_LANG_UPC:
20171 case DW_LANG_D:
20172 case DW_LANG_Python:
20173 return dwarf_version >= 4 ? 0 : -1;
20174 case DW_LANG_Ada95:
20175 case DW_LANG_Ada83:
20176 case DW_LANG_Cobol74:
20177 case DW_LANG_Cobol85:
20178 case DW_LANG_Modula2:
20179 case DW_LANG_PLI:
20180 return dwarf_version >= 4 ? 1 : -1;
20181 default:
20182 return -1;
20186 /* Given a tree node describing an array bound (either lower or upper) output
20187 a representation for that bound. */
20189 static void
20190 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20191 tree bound, struct loc_descr_context *context)
20193 int dflt;
20195 while (1)
20196 switch (TREE_CODE (bound))
20198 /* Strip all conversions. */
20199 CASE_CONVERT:
20200 case VIEW_CONVERT_EXPR:
20201 bound = TREE_OPERAND (bound, 0);
20202 break;
20204 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20205 are even omitted when they are the default. */
20206 case INTEGER_CST:
20207 /* If the value for this bound is the default one, we can even omit the
20208 attribute. */
20209 if (bound_attr == DW_AT_lower_bound
20210 && tree_fits_shwi_p (bound)
20211 && (dflt = lower_bound_default ()) != -1
20212 && tree_to_shwi (bound) == dflt)
20213 return;
20215 /* FALLTHRU */
20217 default:
20218 /* Because of the complex interaction there can be with other GNAT
20219 encodings, GDB isn't ready yet to handle proper DWARF description
20220 for self-referencial subrange bounds: let GNAT encodings do the
20221 magic in such a case. */
20222 if (is_ada ()
20223 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20224 && contains_placeholder_p (bound))
20225 return;
20227 add_scalar_info (subrange_die, bound_attr, bound,
20228 dw_scalar_form_constant
20229 | dw_scalar_form_exprloc
20230 | dw_scalar_form_reference,
20231 context);
20232 return;
20236 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20237 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20238 Note that the block of subscript information for an array type also
20239 includes information about the element type of the given array type.
20241 This function reuses previously set type and bound information if
20242 available. */
20244 static void
20245 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20247 unsigned dimension_number;
20248 tree lower, upper;
20249 dw_die_ref child = type_die->die_child;
20251 for (dimension_number = 0;
20252 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20253 type = TREE_TYPE (type), dimension_number++)
20255 tree domain = TYPE_DOMAIN (type);
20257 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20258 break;
20260 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20261 and (in GNU C only) variable bounds. Handle all three forms
20262 here. */
20264 /* Find and reuse a previously generated DW_TAG_subrange_type if
20265 available.
20267 For multi-dimensional arrays, as we iterate through the
20268 various dimensions in the enclosing for loop above, we also
20269 iterate through the DIE children and pick at each
20270 DW_TAG_subrange_type previously generated (if available).
20271 Each child DW_TAG_subrange_type DIE describes the range of
20272 the current dimension. At this point we should have as many
20273 DW_TAG_subrange_type's as we have dimensions in the
20274 array. */
20275 dw_die_ref subrange_die = NULL;
20276 if (child)
20277 while (1)
20279 child = child->die_sib;
20280 if (child->die_tag == DW_TAG_subrange_type)
20281 subrange_die = child;
20282 if (child == type_die->die_child)
20284 /* If we wrapped around, stop looking next time. */
20285 child = NULL;
20286 break;
20288 if (child->die_tag == DW_TAG_subrange_type)
20289 break;
20291 if (!subrange_die)
20292 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20294 if (domain)
20296 /* We have an array type with specified bounds. */
20297 lower = TYPE_MIN_VALUE (domain);
20298 upper = TYPE_MAX_VALUE (domain);
20300 /* Define the index type. */
20301 if (TREE_TYPE (domain)
20302 && !get_AT (subrange_die, DW_AT_type))
20304 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20305 TREE_TYPE field. We can't emit debug info for this
20306 because it is an unnamed integral type. */
20307 if (TREE_CODE (domain) == INTEGER_TYPE
20308 && TYPE_NAME (domain) == NULL_TREE
20309 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20310 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20312 else
20313 add_type_attribute (subrange_die, TREE_TYPE (domain),
20314 TYPE_UNQUALIFIED, false, type_die);
20317 /* ??? If upper is NULL, the array has unspecified length,
20318 but it does have a lower bound. This happens with Fortran
20319 dimension arr(N:*)
20320 Since the debugger is definitely going to need to know N
20321 to produce useful results, go ahead and output the lower
20322 bound solo, and hope the debugger can cope. */
20324 if (!get_AT (subrange_die, DW_AT_lower_bound))
20325 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20326 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20327 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20330 /* Otherwise we have an array type with an unspecified length. The
20331 DWARF-2 spec does not say how to handle this; let's just leave out the
20332 bounds. */
20336 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20338 static void
20339 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20341 dw_die_ref decl_die;
20342 HOST_WIDE_INT size;
20343 dw_loc_descr_ref size_expr = NULL;
20345 switch (TREE_CODE (tree_node))
20347 case ERROR_MARK:
20348 size = 0;
20349 break;
20350 case ENUMERAL_TYPE:
20351 case RECORD_TYPE:
20352 case UNION_TYPE:
20353 case QUAL_UNION_TYPE:
20354 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20355 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20357 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20358 return;
20360 size_expr = type_byte_size (tree_node, &size);
20361 break;
20362 case FIELD_DECL:
20363 /* For a data member of a struct or union, the DW_AT_byte_size is
20364 generally given as the number of bytes normally allocated for an
20365 object of the *declared* type of the member itself. This is true
20366 even for bit-fields. */
20367 size = int_size_in_bytes (field_type (tree_node));
20368 break;
20369 default:
20370 gcc_unreachable ();
20373 /* Support for dynamically-sized objects was introduced by DWARFv3.
20374 At the moment, GDB does not handle variable byte sizes very well,
20375 though. */
20376 if ((dwarf_version >= 3 || !dwarf_strict)
20377 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20378 && size_expr != NULL)
20379 add_AT_loc (die, DW_AT_byte_size, size_expr);
20381 /* Note that `size' might be -1 when we get to this point. If it is, that
20382 indicates that the byte size of the entity in question is variable and
20383 that we could not generate a DWARF expression that computes it. */
20384 if (size >= 0)
20385 add_AT_unsigned (die, DW_AT_byte_size, size);
20388 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20389 alignment. */
20391 static void
20392 add_alignment_attribute (dw_die_ref die, tree tree_node)
20394 if (dwarf_version < 5 && dwarf_strict)
20395 return;
20397 unsigned align;
20399 if (DECL_P (tree_node))
20401 if (!DECL_USER_ALIGN (tree_node))
20402 return;
20404 align = DECL_ALIGN_UNIT (tree_node);
20406 else if (TYPE_P (tree_node))
20408 if (!TYPE_USER_ALIGN (tree_node))
20409 return;
20411 align = TYPE_ALIGN_UNIT (tree_node);
20413 else
20414 gcc_unreachable ();
20416 add_AT_unsigned (die, DW_AT_alignment, align);
20419 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20420 which specifies the distance in bits from the highest order bit of the
20421 "containing object" for the bit-field to the highest order bit of the
20422 bit-field itself.
20424 For any given bit-field, the "containing object" is a hypothetical object
20425 (of some integral or enum type) within which the given bit-field lives. The
20426 type of this hypothetical "containing object" is always the same as the
20427 declared type of the individual bit-field itself. The determination of the
20428 exact location of the "containing object" for a bit-field is rather
20429 complicated. It's handled by the `field_byte_offset' function (above).
20431 CTX is required: see the comment for VLR_CONTEXT.
20433 Note that it is the size (in bytes) of the hypothetical "containing object"
20434 which will be given in the DW_AT_byte_size attribute for this bit-field.
20435 (See `byte_size_attribute' above). */
20437 static inline void
20438 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20440 HOST_WIDE_INT object_offset_in_bytes;
20441 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20442 HOST_WIDE_INT bitpos_int;
20443 HOST_WIDE_INT highest_order_object_bit_offset;
20444 HOST_WIDE_INT highest_order_field_bit_offset;
20445 HOST_WIDE_INT bit_offset;
20447 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20449 /* Must be a field and a bit field. */
20450 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20452 /* We can't yet handle bit-fields whose offsets are variable, so if we
20453 encounter such things, just return without generating any attribute
20454 whatsoever. Likewise for variable or too large size. */
20455 if (! tree_fits_shwi_p (bit_position (decl))
20456 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20457 return;
20459 bitpos_int = int_bit_position (decl);
20461 /* Note that the bit offset is always the distance (in bits) from the
20462 highest-order bit of the "containing object" to the highest-order bit of
20463 the bit-field itself. Since the "high-order end" of any object or field
20464 is different on big-endian and little-endian machines, the computation
20465 below must take account of these differences. */
20466 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20467 highest_order_field_bit_offset = bitpos_int;
20469 if (! BYTES_BIG_ENDIAN)
20471 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20472 highest_order_object_bit_offset +=
20473 simple_type_size_in_bits (original_type);
20476 bit_offset
20477 = (! BYTES_BIG_ENDIAN
20478 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20479 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20481 if (bit_offset < 0)
20482 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20483 else
20484 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20487 /* For a FIELD_DECL node which represents a bit field, output an attribute
20488 which specifies the length in bits of the given field. */
20490 static inline void
20491 add_bit_size_attribute (dw_die_ref die, tree decl)
20493 /* Must be a field and a bit field. */
20494 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20495 && DECL_BIT_FIELD_TYPE (decl));
20497 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20498 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20501 /* If the compiled language is ANSI C, then add a 'prototyped'
20502 attribute, if arg types are given for the parameters of a function. */
20504 static inline void
20505 add_prototyped_attribute (dw_die_ref die, tree func_type)
20507 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20509 case DW_LANG_C:
20510 case DW_LANG_C89:
20511 case DW_LANG_C99:
20512 case DW_LANG_C11:
20513 case DW_LANG_ObjC:
20514 if (prototype_p (func_type))
20515 add_AT_flag (die, DW_AT_prototyped, 1);
20516 break;
20517 default:
20518 break;
20522 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20523 by looking in the type declaration, the object declaration equate table or
20524 the block mapping. */
20526 static inline dw_die_ref
20527 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20529 dw_die_ref origin_die = NULL;
20531 if (DECL_P (origin))
20533 dw_die_ref c;
20534 origin_die = lookup_decl_die (origin);
20535 /* "Unwrap" the decls DIE which we put in the imported unit context.
20536 We are looking for the abstract copy here. */
20537 if (in_lto_p
20538 && origin_die
20539 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20540 /* ??? Identify this better. */
20541 && c->with_offset)
20542 origin_die = c;
20544 else if (TYPE_P (origin))
20545 origin_die = lookup_type_die (origin);
20546 else if (TREE_CODE (origin) == BLOCK)
20547 origin_die = BLOCK_DIE (origin);
20549 /* XXX: Functions that are never lowered don't always have correct block
20550 trees (in the case of java, they simply have no block tree, in some other
20551 languages). For these functions, there is nothing we can really do to
20552 output correct debug info for inlined functions in all cases. Rather
20553 than die, we'll just produce deficient debug info now, in that we will
20554 have variables without a proper abstract origin. In the future, when all
20555 functions are lowered, we should re-add a gcc_assert (origin_die)
20556 here. */
20558 if (origin_die)
20559 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20560 return origin_die;
20563 /* We do not currently support the pure_virtual attribute. */
20565 static inline void
20566 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20568 if (DECL_VINDEX (func_decl))
20570 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20572 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20573 add_AT_loc (die, DW_AT_vtable_elem_location,
20574 new_loc_descr (DW_OP_constu,
20575 tree_to_shwi (DECL_VINDEX (func_decl)),
20576 0));
20578 /* GNU extension: Record what type this method came from originally. */
20579 if (debug_info_level > DINFO_LEVEL_TERSE
20580 && DECL_CONTEXT (func_decl))
20581 add_AT_die_ref (die, DW_AT_containing_type,
20582 lookup_type_die (DECL_CONTEXT (func_decl)));
20586 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20587 given decl. This used to be a vendor extension until after DWARF 4
20588 standardized it. */
20590 static void
20591 add_linkage_attr (dw_die_ref die, tree decl)
20593 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20595 /* Mimic what assemble_name_raw does with a leading '*'. */
20596 if (name[0] == '*')
20597 name = &name[1];
20599 if (dwarf_version >= 4)
20600 add_AT_string (die, DW_AT_linkage_name, name);
20601 else
20602 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20605 /* Add source coordinate attributes for the given decl. */
20607 static void
20608 add_src_coords_attributes (dw_die_ref die, tree decl)
20610 expanded_location s;
20612 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20613 return;
20614 s = expand_location (DECL_SOURCE_LOCATION (decl));
20615 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20616 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20617 if (debug_column_info && s.column)
20618 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20621 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20623 static void
20624 add_linkage_name_raw (dw_die_ref die, tree decl)
20626 /* Defer until we have an assembler name set. */
20627 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20629 limbo_die_node *asm_name;
20631 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20632 asm_name->die = die;
20633 asm_name->created_for = decl;
20634 asm_name->next = deferred_asm_name;
20635 deferred_asm_name = asm_name;
20637 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20638 add_linkage_attr (die, decl);
20641 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20643 static void
20644 add_linkage_name (dw_die_ref die, tree decl)
20646 if (debug_info_level > DINFO_LEVEL_NONE
20647 && VAR_OR_FUNCTION_DECL_P (decl)
20648 && TREE_PUBLIC (decl)
20649 && !(VAR_P (decl) && DECL_REGISTER (decl))
20650 && die->die_tag != DW_TAG_member)
20651 add_linkage_name_raw (die, decl);
20654 /* Add a DW_AT_name attribute and source coordinate attribute for the
20655 given decl, but only if it actually has a name. */
20657 static void
20658 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20659 bool no_linkage_name)
20661 tree decl_name;
20663 decl_name = DECL_NAME (decl);
20664 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20666 const char *name = dwarf2_name (decl, 0);
20667 if (name)
20668 add_name_attribute (die, name);
20669 if (! DECL_ARTIFICIAL (decl))
20670 add_src_coords_attributes (die, decl);
20672 if (!no_linkage_name)
20673 add_linkage_name (die, decl);
20676 #ifdef VMS_DEBUGGING_INFO
20677 /* Get the function's name, as described by its RTL. This may be different
20678 from the DECL_NAME name used in the source file. */
20679 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20681 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20682 XEXP (DECL_RTL (decl), 0), false);
20683 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20685 #endif /* VMS_DEBUGGING_INFO */
20688 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20690 static void
20691 add_discr_value (dw_die_ref die, dw_discr_value *value)
20693 dw_attr_node attr;
20695 attr.dw_attr = DW_AT_discr_value;
20696 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20697 attr.dw_attr_val.val_entry = NULL;
20698 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20699 if (value->pos)
20700 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20701 else
20702 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20703 add_dwarf_attr (die, &attr);
20706 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20708 static void
20709 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20711 dw_attr_node attr;
20713 attr.dw_attr = DW_AT_discr_list;
20714 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20715 attr.dw_attr_val.val_entry = NULL;
20716 attr.dw_attr_val.v.val_discr_list = discr_list;
20717 add_dwarf_attr (die, &attr);
20720 static inline dw_discr_list_ref
20721 AT_discr_list (dw_attr_node *attr)
20723 return attr->dw_attr_val.v.val_discr_list;
20726 #ifdef VMS_DEBUGGING_INFO
20727 /* Output the debug main pointer die for VMS */
20729 void
20730 dwarf2out_vms_debug_main_pointer (void)
20732 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20733 dw_die_ref die;
20735 /* Allocate the VMS debug main subprogram die. */
20736 die = ggc_cleared_alloc<die_node> ();
20737 die->die_tag = DW_TAG_subprogram;
20738 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20739 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20740 current_function_funcdef_no);
20741 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20743 /* Make it the first child of comp_unit_die (). */
20744 die->die_parent = comp_unit_die ();
20745 if (comp_unit_die ()->die_child)
20747 die->die_sib = comp_unit_die ()->die_child->die_sib;
20748 comp_unit_die ()->die_child->die_sib = die;
20750 else
20752 die->die_sib = die;
20753 comp_unit_die ()->die_child = die;
20756 #endif /* VMS_DEBUGGING_INFO */
20758 /* Push a new declaration scope. */
20760 static void
20761 push_decl_scope (tree scope)
20763 vec_safe_push (decl_scope_table, scope);
20766 /* Pop a declaration scope. */
20768 static inline void
20769 pop_decl_scope (void)
20771 decl_scope_table->pop ();
20774 /* walk_tree helper function for uses_local_type, below. */
20776 static tree
20777 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20779 if (!TYPE_P (*tp))
20780 *walk_subtrees = 0;
20781 else
20783 tree name = TYPE_NAME (*tp);
20784 if (name && DECL_P (name) && decl_function_context (name))
20785 return *tp;
20787 return NULL_TREE;
20790 /* If TYPE involves a function-local type (including a local typedef to a
20791 non-local type), returns that type; otherwise returns NULL_TREE. */
20793 static tree
20794 uses_local_type (tree type)
20796 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20797 return used;
20800 /* Return the DIE for the scope that immediately contains this type.
20801 Non-named types that do not involve a function-local type get global
20802 scope. Named types nested in namespaces or other types get their
20803 containing scope. All other types (i.e. function-local named types) get
20804 the current active scope. */
20806 static dw_die_ref
20807 scope_die_for (tree t, dw_die_ref context_die)
20809 dw_die_ref scope_die = NULL;
20810 tree containing_scope;
20812 /* Non-types always go in the current scope. */
20813 gcc_assert (TYPE_P (t));
20815 /* Use the scope of the typedef, rather than the scope of the type
20816 it refers to. */
20817 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20818 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20819 else
20820 containing_scope = TYPE_CONTEXT (t);
20822 /* Use the containing namespace if there is one. */
20823 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20825 if (context_die == lookup_decl_die (containing_scope))
20826 /* OK */;
20827 else if (debug_info_level > DINFO_LEVEL_TERSE)
20828 context_die = get_context_die (containing_scope);
20829 else
20830 containing_scope = NULL_TREE;
20833 /* Ignore function type "scopes" from the C frontend. They mean that
20834 a tagged type is local to a parmlist of a function declarator, but
20835 that isn't useful to DWARF. */
20836 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20837 containing_scope = NULL_TREE;
20839 if (SCOPE_FILE_SCOPE_P (containing_scope))
20841 /* If T uses a local type keep it local as well, to avoid references
20842 to function-local DIEs from outside the function. */
20843 if (current_function_decl && uses_local_type (t))
20844 scope_die = context_die;
20845 else
20846 scope_die = comp_unit_die ();
20848 else if (TYPE_P (containing_scope))
20850 /* For types, we can just look up the appropriate DIE. */
20851 if (debug_info_level > DINFO_LEVEL_TERSE)
20852 scope_die = get_context_die (containing_scope);
20853 else
20855 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20856 if (scope_die == NULL)
20857 scope_die = comp_unit_die ();
20860 else
20861 scope_die = context_die;
20863 return scope_die;
20866 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20868 static inline int
20869 local_scope_p (dw_die_ref context_die)
20871 for (; context_die; context_die = context_die->die_parent)
20872 if (context_die->die_tag == DW_TAG_inlined_subroutine
20873 || context_die->die_tag == DW_TAG_subprogram)
20874 return 1;
20876 return 0;
20879 /* Returns nonzero if CONTEXT_DIE is a class. */
20881 static inline int
20882 class_scope_p (dw_die_ref context_die)
20884 return (context_die
20885 && (context_die->die_tag == DW_TAG_structure_type
20886 || context_die->die_tag == DW_TAG_class_type
20887 || context_die->die_tag == DW_TAG_interface_type
20888 || context_die->die_tag == DW_TAG_union_type));
20891 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20892 whether or not to treat a DIE in this context as a declaration. */
20894 static inline int
20895 class_or_namespace_scope_p (dw_die_ref context_die)
20897 return (class_scope_p (context_die)
20898 || (context_die && context_die->die_tag == DW_TAG_namespace));
20901 /* Many forms of DIEs require a "type description" attribute. This
20902 routine locates the proper "type descriptor" die for the type given
20903 by 'type' plus any additional qualifiers given by 'cv_quals', and
20904 adds a DW_AT_type attribute below the given die. */
20906 static void
20907 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20908 bool reverse, dw_die_ref context_die)
20910 enum tree_code code = TREE_CODE (type);
20911 dw_die_ref type_die = NULL;
20913 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20914 or fixed-point type, use the inner type. This is because we have no
20915 support for unnamed types in base_type_die. This can happen if this is
20916 an Ada subrange type. Correct solution is emit a subrange type die. */
20917 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20918 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20919 type = TREE_TYPE (type), code = TREE_CODE (type);
20921 if (code == ERROR_MARK
20922 /* Handle a special case. For functions whose return type is void, we
20923 generate *no* type attribute. (Note that no object may have type
20924 `void', so this only applies to function return types). */
20925 || code == VOID_TYPE)
20926 return;
20928 type_die = modified_type_die (type,
20929 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20930 reverse,
20931 context_die);
20933 if (type_die != NULL)
20934 add_AT_die_ref (object_die, DW_AT_type, type_die);
20937 /* Given an object die, add the calling convention attribute for the
20938 function call type. */
20939 static void
20940 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20942 enum dwarf_calling_convention value = DW_CC_normal;
20944 value = ((enum dwarf_calling_convention)
20945 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20947 if (is_fortran ()
20948 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20950 /* DWARF 2 doesn't provide a way to identify a program's source-level
20951 entry point. DW_AT_calling_convention attributes are only meant
20952 to describe functions' calling conventions. However, lacking a
20953 better way to signal the Fortran main program, we used this for
20954 a long time, following existing custom. Now, DWARF 4 has
20955 DW_AT_main_subprogram, which we add below, but some tools still
20956 rely on the old way, which we thus keep. */
20957 value = DW_CC_program;
20959 if (dwarf_version >= 4 || !dwarf_strict)
20960 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20963 /* Only add the attribute if the backend requests it, and
20964 is not DW_CC_normal. */
20965 if (value && (value != DW_CC_normal))
20966 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20969 /* Given a tree pointer to a struct, class, union, or enum type node, return
20970 a pointer to the (string) tag name for the given type, or zero if the type
20971 was declared without a tag. */
20973 static const char *
20974 type_tag (const_tree type)
20976 const char *name = 0;
20978 if (TYPE_NAME (type) != 0)
20980 tree t = 0;
20982 /* Find the IDENTIFIER_NODE for the type name. */
20983 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20984 && !TYPE_NAMELESS (type))
20985 t = TYPE_NAME (type);
20987 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20988 a TYPE_DECL node, regardless of whether or not a `typedef' was
20989 involved. */
20990 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20991 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20993 /* We want to be extra verbose. Don't call dwarf_name if
20994 DECL_NAME isn't set. The default hook for decl_printable_name
20995 doesn't like that, and in this context it's correct to return
20996 0, instead of "<anonymous>" or the like. */
20997 if (DECL_NAME (TYPE_NAME (type))
20998 && !DECL_NAMELESS (TYPE_NAME (type)))
20999 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21002 /* Now get the name as a string, or invent one. */
21003 if (!name && t != 0)
21004 name = IDENTIFIER_POINTER (t);
21007 return (name == 0 || *name == '\0') ? 0 : name;
21010 /* Return the type associated with a data member, make a special check
21011 for bit field types. */
21013 static inline tree
21014 member_declared_type (const_tree member)
21016 return (DECL_BIT_FIELD_TYPE (member)
21017 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21020 /* Get the decl's label, as described by its RTL. This may be different
21021 from the DECL_NAME name used in the source file. */
21023 #if 0
21024 static const char *
21025 decl_start_label (tree decl)
21027 rtx x;
21028 const char *fnname;
21030 x = DECL_RTL (decl);
21031 gcc_assert (MEM_P (x));
21033 x = XEXP (x, 0);
21034 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21036 fnname = XSTR (x, 0);
21037 return fnname;
21039 #endif
21041 /* For variable-length arrays that have been previously generated, but
21042 may be incomplete due to missing subscript info, fill the subscript
21043 info. Return TRUE if this is one of those cases. */
21044 static bool
21045 fill_variable_array_bounds (tree type)
21047 if (TREE_ASM_WRITTEN (type)
21048 && TREE_CODE (type) == ARRAY_TYPE
21049 && variably_modified_type_p (type, NULL))
21051 dw_die_ref array_die = lookup_type_die (type);
21052 if (!array_die)
21053 return false;
21054 add_subscript_info (array_die, type, !is_ada ());
21055 return true;
21057 return false;
21060 /* These routines generate the internal representation of the DIE's for
21061 the compilation unit. Debugging information is collected by walking
21062 the declaration trees passed in from dwarf2out_decl(). */
21064 static void
21065 gen_array_type_die (tree type, dw_die_ref context_die)
21067 dw_die_ref array_die;
21069 /* GNU compilers represent multidimensional array types as sequences of one
21070 dimensional array types whose element types are themselves array types.
21071 We sometimes squish that down to a single array_type DIE with multiple
21072 subscripts in the Dwarf debugging info. The draft Dwarf specification
21073 say that we are allowed to do this kind of compression in C, because
21074 there is no difference between an array of arrays and a multidimensional
21075 array. We don't do this for Ada to remain as close as possible to the
21076 actual representation, which is especially important against the language
21077 flexibilty wrt arrays of variable size. */
21079 bool collapse_nested_arrays = !is_ada ();
21081 if (fill_variable_array_bounds (type))
21082 return;
21084 dw_die_ref scope_die = scope_die_for (type, context_die);
21085 tree element_type;
21087 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21088 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21089 if (TYPE_STRING_FLAG (type)
21090 && TREE_CODE (type) == ARRAY_TYPE
21091 && is_fortran ()
21092 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21094 HOST_WIDE_INT size;
21096 array_die = new_die (DW_TAG_string_type, scope_die, type);
21097 add_name_attribute (array_die, type_tag (type));
21098 equate_type_number_to_die (type, array_die);
21099 size = int_size_in_bytes (type);
21100 if (size >= 0)
21101 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21102 /* ??? We can't annotate types late, but for LTO we may not
21103 generate a location early either (gfortran.dg/save_6.f90). */
21104 else if (! (early_dwarf && flag_generate_lto)
21105 && TYPE_DOMAIN (type) != NULL_TREE
21106 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21108 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21109 tree rszdecl = szdecl;
21111 size = int_size_in_bytes (TREE_TYPE (szdecl));
21112 if (!DECL_P (szdecl))
21114 if (TREE_CODE (szdecl) == INDIRECT_REF
21115 && DECL_P (TREE_OPERAND (szdecl, 0)))
21117 rszdecl = TREE_OPERAND (szdecl, 0);
21118 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21119 != DWARF2_ADDR_SIZE)
21120 size = 0;
21122 else
21123 size = 0;
21125 if (size > 0)
21127 dw_loc_list_ref loc
21128 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21129 NULL);
21130 if (loc)
21132 add_AT_location_description (array_die, DW_AT_string_length,
21133 loc);
21134 if (size != DWARF2_ADDR_SIZE)
21135 add_AT_unsigned (array_die, dwarf_version >= 5
21136 ? DW_AT_string_length_byte_size
21137 : DW_AT_byte_size, size);
21141 return;
21144 array_die = new_die (DW_TAG_array_type, scope_die, type);
21145 add_name_attribute (array_die, type_tag (type));
21146 equate_type_number_to_die (type, array_die);
21148 if (TREE_CODE (type) == VECTOR_TYPE)
21149 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21151 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21152 if (is_fortran ()
21153 && TREE_CODE (type) == ARRAY_TYPE
21154 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21155 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21156 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21158 #if 0
21159 /* We default the array ordering. SDB will probably do
21160 the right things even if DW_AT_ordering is not present. It's not even
21161 an issue until we start to get into multidimensional arrays anyway. If
21162 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
21163 then we'll have to put the DW_AT_ordering attribute back in. (But if
21164 and when we find out that we need to put these in, we will only do so
21165 for multidimensional arrays. */
21166 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21167 #endif
21169 if (TREE_CODE (type) == VECTOR_TYPE)
21171 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21172 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21173 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21174 add_bound_info (subrange_die, DW_AT_upper_bound,
21175 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21177 else
21178 add_subscript_info (array_die, type, collapse_nested_arrays);
21180 /* Add representation of the type of the elements of this array type and
21181 emit the corresponding DIE if we haven't done it already. */
21182 element_type = TREE_TYPE (type);
21183 if (collapse_nested_arrays)
21184 while (TREE_CODE (element_type) == ARRAY_TYPE)
21186 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21187 break;
21188 element_type = TREE_TYPE (element_type);
21191 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21192 TREE_CODE (type) == ARRAY_TYPE
21193 && TYPE_REVERSE_STORAGE_ORDER (type),
21194 context_die);
21196 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21197 if (TYPE_ARTIFICIAL (type))
21198 add_AT_flag (array_die, DW_AT_artificial, 1);
21200 if (get_AT (array_die, DW_AT_name))
21201 add_pubtype (type, array_die);
21203 add_alignment_attribute (array_die, type);
21206 /* This routine generates DIE for array with hidden descriptor, details
21207 are filled into *info by a langhook. */
21209 static void
21210 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21211 dw_die_ref context_die)
21213 const dw_die_ref scope_die = scope_die_for (type, context_die);
21214 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21215 struct loc_descr_context context = { type, info->base_decl, NULL,
21216 false, false };
21217 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21218 int dim;
21220 add_name_attribute (array_die, type_tag (type));
21221 equate_type_number_to_die (type, array_die);
21223 if (info->ndimensions > 1)
21224 switch (info->ordering)
21226 case array_descr_ordering_row_major:
21227 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21228 break;
21229 case array_descr_ordering_column_major:
21230 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21231 break;
21232 default:
21233 break;
21236 if (dwarf_version >= 3 || !dwarf_strict)
21238 if (info->data_location)
21239 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21240 dw_scalar_form_exprloc, &context);
21241 if (info->associated)
21242 add_scalar_info (array_die, DW_AT_associated, info->associated,
21243 dw_scalar_form_constant
21244 | dw_scalar_form_exprloc
21245 | dw_scalar_form_reference, &context);
21246 if (info->allocated)
21247 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21248 dw_scalar_form_constant
21249 | dw_scalar_form_exprloc
21250 | dw_scalar_form_reference, &context);
21251 if (info->stride)
21253 const enum dwarf_attribute attr
21254 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21255 const int forms
21256 = (info->stride_in_bits)
21257 ? dw_scalar_form_constant
21258 : (dw_scalar_form_constant
21259 | dw_scalar_form_exprloc
21260 | dw_scalar_form_reference);
21262 add_scalar_info (array_die, attr, info->stride, forms, &context);
21265 if (dwarf_version >= 5)
21267 if (info->rank)
21269 add_scalar_info (array_die, DW_AT_rank, info->rank,
21270 dw_scalar_form_constant
21271 | dw_scalar_form_exprloc, &context);
21272 subrange_tag = DW_TAG_generic_subrange;
21273 context.placeholder_arg = true;
21277 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21279 for (dim = 0; dim < info->ndimensions; dim++)
21281 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21283 if (info->dimen[dim].bounds_type)
21284 add_type_attribute (subrange_die,
21285 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21286 false, context_die);
21287 if (info->dimen[dim].lower_bound)
21288 add_bound_info (subrange_die, DW_AT_lower_bound,
21289 info->dimen[dim].lower_bound, &context);
21290 if (info->dimen[dim].upper_bound)
21291 add_bound_info (subrange_die, DW_AT_upper_bound,
21292 info->dimen[dim].upper_bound, &context);
21293 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21294 add_scalar_info (subrange_die, DW_AT_byte_stride,
21295 info->dimen[dim].stride,
21296 dw_scalar_form_constant
21297 | dw_scalar_form_exprloc
21298 | dw_scalar_form_reference,
21299 &context);
21302 gen_type_die (info->element_type, context_die);
21303 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21304 TREE_CODE (type) == ARRAY_TYPE
21305 && TYPE_REVERSE_STORAGE_ORDER (type),
21306 context_die);
21308 if (get_AT (array_die, DW_AT_name))
21309 add_pubtype (type, array_die);
21311 add_alignment_attribute (array_die, type);
21314 #if 0
21315 static void
21316 gen_entry_point_die (tree decl, dw_die_ref context_die)
21318 tree origin = decl_ultimate_origin (decl);
21319 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21321 if (origin != NULL)
21322 add_abstract_origin_attribute (decl_die, origin);
21323 else
21325 add_name_and_src_coords_attributes (decl_die, decl);
21326 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21327 TYPE_UNQUALIFIED, false, context_die);
21330 if (DECL_ABSTRACT_P (decl))
21331 equate_decl_number_to_die (decl, decl_die);
21332 else
21333 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21335 #endif
21337 /* Walk through the list of incomplete types again, trying once more to
21338 emit full debugging info for them. */
21340 static void
21341 retry_incomplete_types (void)
21343 set_early_dwarf s;
21344 int i;
21346 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21347 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21348 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21349 vec_safe_truncate (incomplete_types, 0);
21352 /* Determine what tag to use for a record type. */
21354 static enum dwarf_tag
21355 record_type_tag (tree type)
21357 if (! lang_hooks.types.classify_record)
21358 return DW_TAG_structure_type;
21360 switch (lang_hooks.types.classify_record (type))
21362 case RECORD_IS_STRUCT:
21363 return DW_TAG_structure_type;
21365 case RECORD_IS_CLASS:
21366 return DW_TAG_class_type;
21368 case RECORD_IS_INTERFACE:
21369 if (dwarf_version >= 3 || !dwarf_strict)
21370 return DW_TAG_interface_type;
21371 return DW_TAG_structure_type;
21373 default:
21374 gcc_unreachable ();
21378 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21379 include all of the information about the enumeration values also. Each
21380 enumerated type name/value is listed as a child of the enumerated type
21381 DIE. */
21383 static dw_die_ref
21384 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21386 dw_die_ref type_die = lookup_type_die (type);
21388 if (type_die == NULL)
21390 type_die = new_die (DW_TAG_enumeration_type,
21391 scope_die_for (type, context_die), type);
21392 equate_type_number_to_die (type, type_die);
21393 add_name_attribute (type_die, type_tag (type));
21394 if (dwarf_version >= 4 || !dwarf_strict)
21396 if (ENUM_IS_SCOPED (type))
21397 add_AT_flag (type_die, DW_AT_enum_class, 1);
21398 if (ENUM_IS_OPAQUE (type))
21399 add_AT_flag (type_die, DW_AT_declaration, 1);
21401 if (!dwarf_strict)
21402 add_AT_unsigned (type_die, DW_AT_encoding,
21403 TYPE_UNSIGNED (type)
21404 ? DW_ATE_unsigned
21405 : DW_ATE_signed);
21407 else if (! TYPE_SIZE (type))
21408 return type_die;
21409 else
21410 remove_AT (type_die, DW_AT_declaration);
21412 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21413 given enum type is incomplete, do not generate the DW_AT_byte_size
21414 attribute or the DW_AT_element_list attribute. */
21415 if (TYPE_SIZE (type))
21417 tree link;
21419 TREE_ASM_WRITTEN (type) = 1;
21420 add_byte_size_attribute (type_die, type);
21421 add_alignment_attribute (type_die, type);
21422 if (dwarf_version >= 3 || !dwarf_strict)
21424 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21425 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21426 context_die);
21428 if (TYPE_STUB_DECL (type) != NULL_TREE)
21430 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21431 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21434 /* If the first reference to this type was as the return type of an
21435 inline function, then it may not have a parent. Fix this now. */
21436 if (type_die->die_parent == NULL)
21437 add_child_die (scope_die_for (type, context_die), type_die);
21439 for (link = TYPE_VALUES (type);
21440 link != NULL; link = TREE_CHAIN (link))
21442 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21443 tree value = TREE_VALUE (link);
21445 add_name_attribute (enum_die,
21446 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21448 if (TREE_CODE (value) == CONST_DECL)
21449 value = DECL_INITIAL (value);
21451 if (simple_type_size_in_bits (TREE_TYPE (value))
21452 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21454 /* For constant forms created by add_AT_unsigned DWARF
21455 consumers (GDB, elfutils, etc.) always zero extend
21456 the value. Only when the actual value is negative
21457 do we need to use add_AT_int to generate a constant
21458 form that can represent negative values. */
21459 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21460 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21461 add_AT_unsigned (enum_die, DW_AT_const_value,
21462 (unsigned HOST_WIDE_INT) val);
21463 else
21464 add_AT_int (enum_die, DW_AT_const_value, val);
21466 else
21467 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21468 that here. TODO: This should be re-worked to use correct
21469 signed/unsigned double tags for all cases. */
21470 add_AT_wide (enum_die, DW_AT_const_value, value);
21473 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21474 if (TYPE_ARTIFICIAL (type))
21475 add_AT_flag (type_die, DW_AT_artificial, 1);
21477 else
21478 add_AT_flag (type_die, DW_AT_declaration, 1);
21480 add_alignment_attribute (type_die, type);
21482 add_pubtype (type, type_die);
21484 return type_die;
21487 /* Generate a DIE to represent either a real live formal parameter decl or to
21488 represent just the type of some formal parameter position in some function
21489 type.
21491 Note that this routine is a bit unusual because its argument may be a
21492 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21493 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21494 node. If it's the former then this function is being called to output a
21495 DIE to represent a formal parameter object (or some inlining thereof). If
21496 it's the latter, then this function is only being called to output a
21497 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21498 argument type of some subprogram type.
21499 If EMIT_NAME_P is true, name and source coordinate attributes
21500 are emitted. */
21502 static dw_die_ref
21503 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21504 dw_die_ref context_die)
21506 tree node_or_origin = node ? node : origin;
21507 tree ultimate_origin;
21508 dw_die_ref parm_die = NULL;
21510 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21512 parm_die = lookup_decl_die (node);
21514 /* If the contexts differ, we may not be talking about the same
21515 thing.
21516 ??? When in LTO the DIE parent is the "abstract" copy and the
21517 context_die is the specification "copy". But this whole block
21518 should eventually be no longer needed. */
21519 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21521 if (!DECL_ABSTRACT_P (node))
21523 /* This can happen when creating an inlined instance, in
21524 which case we need to create a new DIE that will get
21525 annotated with DW_AT_abstract_origin. */
21526 parm_die = NULL;
21528 else
21529 gcc_unreachable ();
21532 if (parm_die && parm_die->die_parent == NULL)
21534 /* Check that parm_die already has the right attributes that
21535 we would have added below. If any attributes are
21536 missing, fall through to add them. */
21537 if (! DECL_ABSTRACT_P (node_or_origin)
21538 && !get_AT (parm_die, DW_AT_location)
21539 && !get_AT (parm_die, DW_AT_const_value))
21540 /* We are missing location info, and are about to add it. */
21542 else
21544 add_child_die (context_die, parm_die);
21545 return parm_die;
21550 /* If we have a previously generated DIE, use it, unless this is an
21551 concrete instance (origin != NULL), in which case we need a new
21552 DIE with a corresponding DW_AT_abstract_origin. */
21553 bool reusing_die;
21554 if (parm_die && origin == NULL)
21555 reusing_die = true;
21556 else
21558 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21559 reusing_die = false;
21562 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21564 case tcc_declaration:
21565 ultimate_origin = decl_ultimate_origin (node_or_origin);
21566 if (node || ultimate_origin)
21567 origin = ultimate_origin;
21569 if (reusing_die)
21570 goto add_location;
21572 if (origin != NULL)
21573 add_abstract_origin_attribute (parm_die, origin);
21574 else if (emit_name_p)
21575 add_name_and_src_coords_attributes (parm_die, node);
21576 if (origin == NULL
21577 || (! DECL_ABSTRACT_P (node_or_origin)
21578 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21579 decl_function_context
21580 (node_or_origin))))
21582 tree type = TREE_TYPE (node_or_origin);
21583 if (decl_by_reference_p (node_or_origin))
21584 add_type_attribute (parm_die, TREE_TYPE (type),
21585 TYPE_UNQUALIFIED,
21586 false, context_die);
21587 else
21588 add_type_attribute (parm_die, type,
21589 decl_quals (node_or_origin),
21590 false, context_die);
21592 if (origin == NULL && DECL_ARTIFICIAL (node))
21593 add_AT_flag (parm_die, DW_AT_artificial, 1);
21594 add_location:
21595 if (node && node != origin)
21596 equate_decl_number_to_die (node, parm_die);
21597 if (! DECL_ABSTRACT_P (node_or_origin))
21598 add_location_or_const_value_attribute (parm_die, node_or_origin,
21599 node == NULL);
21601 break;
21603 case tcc_type:
21604 /* We were called with some kind of a ..._TYPE node. */
21605 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21606 context_die);
21607 break;
21609 default:
21610 gcc_unreachable ();
21613 return parm_die;
21616 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21617 children DW_TAG_formal_parameter DIEs representing the arguments of the
21618 parameter pack.
21620 PARM_PACK must be a function parameter pack.
21621 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21622 must point to the subsequent arguments of the function PACK_ARG belongs to.
21623 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21624 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21625 following the last one for which a DIE was generated. */
21627 static dw_die_ref
21628 gen_formal_parameter_pack_die (tree parm_pack,
21629 tree pack_arg,
21630 dw_die_ref subr_die,
21631 tree *next_arg)
21633 tree arg;
21634 dw_die_ref parm_pack_die;
21636 gcc_assert (parm_pack
21637 && lang_hooks.function_parameter_pack_p (parm_pack)
21638 && subr_die);
21640 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21641 add_src_coords_attributes (parm_pack_die, parm_pack);
21643 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21645 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21646 parm_pack))
21647 break;
21648 gen_formal_parameter_die (arg, NULL,
21649 false /* Don't emit name attribute. */,
21650 parm_pack_die);
21652 if (next_arg)
21653 *next_arg = arg;
21654 return parm_pack_die;
21657 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21658 at the end of an (ANSI prototyped) formal parameters list. */
21660 static void
21661 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21663 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21666 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21667 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21668 parameters as specified in some function type specification (except for
21669 those which appear as part of a function *definition*). */
21671 static void
21672 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21674 tree link;
21675 tree formal_type = NULL;
21676 tree first_parm_type;
21677 tree arg;
21679 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21681 arg = DECL_ARGUMENTS (function_or_method_type);
21682 function_or_method_type = TREE_TYPE (function_or_method_type);
21684 else
21685 arg = NULL_TREE;
21687 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21689 /* Make our first pass over the list of formal parameter types and output a
21690 DW_TAG_formal_parameter DIE for each one. */
21691 for (link = first_parm_type; link; )
21693 dw_die_ref parm_die;
21695 formal_type = TREE_VALUE (link);
21696 if (formal_type == void_type_node)
21697 break;
21699 /* Output a (nameless) DIE to represent the formal parameter itself. */
21700 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21702 parm_die = gen_formal_parameter_die (formal_type, NULL,
21703 true /* Emit name attribute. */,
21704 context_die);
21705 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21706 && link == first_parm_type)
21708 add_AT_flag (parm_die, DW_AT_artificial, 1);
21709 if (dwarf_version >= 3 || !dwarf_strict)
21710 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21712 else if (arg && DECL_ARTIFICIAL (arg))
21713 add_AT_flag (parm_die, DW_AT_artificial, 1);
21716 link = TREE_CHAIN (link);
21717 if (arg)
21718 arg = DECL_CHAIN (arg);
21721 /* If this function type has an ellipsis, add a
21722 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21723 if (formal_type != void_type_node)
21724 gen_unspecified_parameters_die (function_or_method_type, context_die);
21726 /* Make our second (and final) pass over the list of formal parameter types
21727 and output DIEs to represent those types (as necessary). */
21728 for (link = TYPE_ARG_TYPES (function_or_method_type);
21729 link && TREE_VALUE (link);
21730 link = TREE_CHAIN (link))
21731 gen_type_die (TREE_VALUE (link), context_die);
21734 /* We want to generate the DIE for TYPE so that we can generate the
21735 die for MEMBER, which has been defined; we will need to refer back
21736 to the member declaration nested within TYPE. If we're trying to
21737 generate minimal debug info for TYPE, processing TYPE won't do the
21738 trick; we need to attach the member declaration by hand. */
21740 static void
21741 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21743 gen_type_die (type, context_die);
21745 /* If we're trying to avoid duplicate debug info, we may not have
21746 emitted the member decl for this function. Emit it now. */
21747 if (TYPE_STUB_DECL (type)
21748 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21749 && ! lookup_decl_die (member))
21751 dw_die_ref type_die;
21752 gcc_assert (!decl_ultimate_origin (member));
21754 push_decl_scope (type);
21755 type_die = lookup_type_die_strip_naming_typedef (type);
21756 if (TREE_CODE (member) == FUNCTION_DECL)
21757 gen_subprogram_die (member, type_die);
21758 else if (TREE_CODE (member) == FIELD_DECL)
21760 /* Ignore the nameless fields that are used to skip bits but handle
21761 C++ anonymous unions and structs. */
21762 if (DECL_NAME (member) != NULL_TREE
21763 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21764 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21766 struct vlr_context vlr_ctx = {
21767 DECL_CONTEXT (member), /* struct_type */
21768 NULL_TREE /* variant_part_offset */
21770 gen_type_die (member_declared_type (member), type_die);
21771 gen_field_die (member, &vlr_ctx, type_die);
21774 else
21775 gen_variable_die (member, NULL_TREE, type_die);
21777 pop_decl_scope ();
21781 /* Forward declare these functions, because they are mutually recursive
21782 with their set_block_* pairing functions. */
21783 static void set_decl_origin_self (tree);
21785 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21786 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21787 that it points to the node itself, thus indicating that the node is its
21788 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21789 the given node is NULL, recursively descend the decl/block tree which
21790 it is the root of, and for each other ..._DECL or BLOCK node contained
21791 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21792 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21793 values to point to themselves. */
21795 static void
21796 set_block_origin_self (tree stmt)
21798 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21800 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21803 tree local_decl;
21805 for (local_decl = BLOCK_VARS (stmt);
21806 local_decl != NULL_TREE;
21807 local_decl = DECL_CHAIN (local_decl))
21808 /* Do not recurse on nested functions since the inlining status
21809 of parent and child can be different as per the DWARF spec. */
21810 if (TREE_CODE (local_decl) != FUNCTION_DECL
21811 && !DECL_EXTERNAL (local_decl))
21812 set_decl_origin_self (local_decl);
21816 tree subblock;
21818 for (subblock = BLOCK_SUBBLOCKS (stmt);
21819 subblock != NULL_TREE;
21820 subblock = BLOCK_CHAIN (subblock))
21821 set_block_origin_self (subblock); /* Recurse. */
21826 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21827 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21828 node to so that it points to the node itself, thus indicating that the
21829 node represents its own (abstract) origin. Additionally, if the
21830 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21831 the decl/block tree of which the given node is the root of, and for
21832 each other ..._DECL or BLOCK node contained therein whose
21833 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21834 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21835 point to themselves. */
21837 static void
21838 set_decl_origin_self (tree decl)
21840 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21842 DECL_ABSTRACT_ORIGIN (decl) = decl;
21843 if (TREE_CODE (decl) == FUNCTION_DECL)
21845 tree arg;
21847 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21848 DECL_ABSTRACT_ORIGIN (arg) = arg;
21849 if (DECL_INITIAL (decl) != NULL_TREE
21850 && DECL_INITIAL (decl) != error_mark_node)
21851 set_block_origin_self (DECL_INITIAL (decl));
21856 /* Mark the early DIE for DECL as the abstract instance. */
21858 static void
21859 dwarf2out_abstract_function (tree decl)
21861 dw_die_ref old_die;
21863 /* Make sure we have the actual abstract inline, not a clone. */
21864 decl = DECL_ORIGIN (decl);
21866 if (DECL_IGNORED_P (decl))
21867 return;
21869 old_die = lookup_decl_die (decl);
21870 /* With early debug we always have an old DIE. */
21871 gcc_assert (old_die != NULL);
21872 if (get_AT (old_die, DW_AT_inline)
21873 || get_AT (old_die, DW_AT_abstract_origin))
21874 /* We've already generated the abstract instance. */
21875 return;
21877 /* Go ahead and put DW_AT_inline on the DIE. */
21878 if (DECL_DECLARED_INLINE_P (decl))
21880 if (cgraph_function_possibly_inlined_p (decl))
21881 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21882 else
21883 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21885 else
21887 if (cgraph_function_possibly_inlined_p (decl))
21888 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21889 else
21890 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21893 if (DECL_DECLARED_INLINE_P (decl)
21894 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21895 add_AT_flag (old_die, DW_AT_artificial, 1);
21897 set_decl_origin_self (decl);
21900 /* Helper function of premark_used_types() which gets called through
21901 htab_traverse.
21903 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21904 marked as unused by prune_unused_types. */
21906 bool
21907 premark_used_types_helper (tree const &type, void *)
21909 dw_die_ref die;
21911 die = lookup_type_die (type);
21912 if (die != NULL)
21913 die->die_perennial_p = 1;
21914 return true;
21917 /* Helper function of premark_types_used_by_global_vars which gets called
21918 through htab_traverse.
21920 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21921 marked as unused by prune_unused_types. The DIE of the type is marked
21922 only if the global variable using the type will actually be emitted. */
21925 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21926 void *)
21928 struct types_used_by_vars_entry *entry;
21929 dw_die_ref die;
21931 entry = (struct types_used_by_vars_entry *) *slot;
21932 gcc_assert (entry->type != NULL
21933 && entry->var_decl != NULL);
21934 die = lookup_type_die (entry->type);
21935 if (die)
21937 /* Ask cgraph if the global variable really is to be emitted.
21938 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21939 varpool_node *node = varpool_node::get (entry->var_decl);
21940 if (node && node->definition)
21942 die->die_perennial_p = 1;
21943 /* Keep the parent DIEs as well. */
21944 while ((die = die->die_parent) && die->die_perennial_p == 0)
21945 die->die_perennial_p = 1;
21948 return 1;
21951 /* Mark all members of used_types_hash as perennial. */
21953 static void
21954 premark_used_types (struct function *fun)
21956 if (fun && fun->used_types_hash)
21957 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21960 /* Mark all members of types_used_by_vars_entry as perennial. */
21962 static void
21963 premark_types_used_by_global_vars (void)
21965 if (types_used_by_vars_hash)
21966 types_used_by_vars_hash
21967 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21970 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21971 for CA_LOC call arg loc node. */
21973 static dw_die_ref
21974 gen_call_site_die (tree decl, dw_die_ref subr_die,
21975 struct call_arg_loc_node *ca_loc)
21977 dw_die_ref stmt_die = NULL, die;
21978 tree block = ca_loc->block;
21980 while (block
21981 && block != DECL_INITIAL (decl)
21982 && TREE_CODE (block) == BLOCK)
21984 stmt_die = BLOCK_DIE (block);
21985 if (stmt_die)
21986 break;
21987 block = BLOCK_SUPERCONTEXT (block);
21989 if (stmt_die == NULL)
21990 stmt_die = subr_die;
21991 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21992 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21993 if (ca_loc->tail_call_p)
21994 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21995 if (ca_loc->symbol_ref)
21997 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21998 if (tdie)
21999 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22000 else
22001 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22002 false);
22004 return die;
22007 /* Generate a DIE to represent a declared function (either file-scope or
22008 block-local). */
22010 static void
22011 gen_subprogram_die (tree decl, dw_die_ref context_die)
22013 tree origin = decl_ultimate_origin (decl);
22014 dw_die_ref subr_die;
22015 dw_die_ref old_die = lookup_decl_die (decl);
22017 /* This function gets called multiple times for different stages of
22018 the debug process. For example, for func() in this code:
22020 namespace S
22022 void func() { ... }
22025 ...we get called 4 times. Twice in early debug and twice in
22026 late debug:
22028 Early debug
22029 -----------
22031 1. Once while generating func() within the namespace. This is
22032 the declaration. The declaration bit below is set, as the
22033 context is the namespace.
22035 A new DIE will be generated with DW_AT_declaration set.
22037 2. Once for func() itself. This is the specification. The
22038 declaration bit below is clear as the context is the CU.
22040 We will use the cached DIE from (1) to create a new DIE with
22041 DW_AT_specification pointing to the declaration in (1).
22043 Late debug via rest_of_handle_final()
22044 -------------------------------------
22046 3. Once generating func() within the namespace. This is also the
22047 declaration, as in (1), but this time we will early exit below
22048 as we have a cached DIE and a declaration needs no additional
22049 annotations (no locations), as the source declaration line
22050 info is enough.
22052 4. Once for func() itself. As in (2), this is the specification,
22053 but this time we will re-use the cached DIE, and just annotate
22054 it with the location information that should now be available.
22056 For something without namespaces, but with abstract instances, we
22057 are also called a multiple times:
22059 class Base
22061 public:
22062 Base (); // constructor declaration (1)
22065 Base::Base () { } // constructor specification (2)
22067 Early debug
22068 -----------
22070 1. Once for the Base() constructor by virtue of it being a
22071 member of the Base class. This is done via
22072 rest_of_type_compilation.
22074 This is a declaration, so a new DIE will be created with
22075 DW_AT_declaration.
22077 2. Once for the Base() constructor definition, but this time
22078 while generating the abstract instance of the base
22079 constructor (__base_ctor) which is being generated via early
22080 debug of reachable functions.
22082 Even though we have a cached version of the declaration (1),
22083 we will create a DW_AT_specification of the declaration DIE
22084 in (1).
22086 3. Once for the __base_ctor itself, but this time, we generate
22087 an DW_AT_abstract_origin version of the DW_AT_specification in
22088 (2).
22090 Late debug via rest_of_handle_final
22091 -----------------------------------
22093 4. One final time for the __base_ctor (which will have a cached
22094 DIE with DW_AT_abstract_origin created in (3). This time,
22095 we will just annotate the location information now
22096 available.
22098 int declaration = (current_function_decl != decl
22099 || class_or_namespace_scope_p (context_die));
22101 /* Now that the C++ front end lazily declares artificial member fns, we
22102 might need to retrofit the declaration into its class. */
22103 if (!declaration && !origin && !old_die
22104 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22105 && !class_or_namespace_scope_p (context_die)
22106 && debug_info_level > DINFO_LEVEL_TERSE)
22107 old_die = force_decl_die (decl);
22109 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22110 if (origin != NULL)
22112 gcc_assert (!declaration || local_scope_p (context_die));
22114 /* Fixup die_parent for the abstract instance of a nested
22115 inline function. */
22116 if (old_die && old_die->die_parent == NULL)
22117 add_child_die (context_die, old_die);
22119 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22121 /* If we have a DW_AT_abstract_origin we have a working
22122 cached version. */
22123 subr_die = old_die;
22125 else
22127 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22128 add_abstract_origin_attribute (subr_die, origin);
22129 /* This is where the actual code for a cloned function is.
22130 Let's emit linkage name attribute for it. This helps
22131 debuggers to e.g, set breakpoints into
22132 constructors/destructors when the user asks "break
22133 K::K". */
22134 add_linkage_name (subr_die, decl);
22137 /* A cached copy, possibly from early dwarf generation. Reuse as
22138 much as possible. */
22139 else if (old_die)
22141 /* A declaration that has been previously dumped needs no
22142 additional information. */
22143 if (declaration)
22144 return;
22146 if (!get_AT_flag (old_die, DW_AT_declaration)
22147 /* We can have a normal definition following an inline one in the
22148 case of redefinition of GNU C extern inlines.
22149 It seems reasonable to use AT_specification in this case. */
22150 && !get_AT (old_die, DW_AT_inline))
22152 /* Detect and ignore this case, where we are trying to output
22153 something we have already output. */
22154 if (get_AT (old_die, DW_AT_low_pc)
22155 || get_AT (old_die, DW_AT_ranges))
22156 return;
22158 /* If we have no location information, this must be a
22159 partially generated DIE from early dwarf generation.
22160 Fall through and generate it. */
22163 /* If the definition comes from the same place as the declaration,
22164 maybe use the old DIE. We always want the DIE for this function
22165 that has the *_pc attributes to be under comp_unit_die so the
22166 debugger can find it. We also need to do this for abstract
22167 instances of inlines, since the spec requires the out-of-line copy
22168 to have the same parent. For local class methods, this doesn't
22169 apply; we just use the old DIE. */
22170 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22171 struct dwarf_file_data * file_index = lookup_filename (s.file);
22172 if ((is_cu_die (old_die->die_parent)
22173 /* This condition fixes the inconsistency/ICE with the
22174 following Fortran test (or some derivative thereof) while
22175 building libgfortran:
22177 module some_m
22178 contains
22179 logical function funky (FLAG)
22180 funky = .true.
22181 end function
22182 end module
22184 || (old_die->die_parent
22185 && old_die->die_parent->die_tag == DW_TAG_module)
22186 || context_die == NULL)
22187 && (DECL_ARTIFICIAL (decl)
22188 /* The location attributes may be in the abstract origin
22189 which in the case of LTO might be not available to
22190 look at. */
22191 || get_AT (old_die, DW_AT_abstract_origin)
22192 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22193 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22194 == (unsigned) s.line)
22195 && (!debug_column_info
22196 || s.column == 0
22197 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22198 == (unsigned) s.column)))))
22200 subr_die = old_die;
22202 /* Clear out the declaration attribute, but leave the
22203 parameters so they can be augmented with location
22204 information later. Unless this was a declaration, in
22205 which case, wipe out the nameless parameters and recreate
22206 them further down. */
22207 if (remove_AT (subr_die, DW_AT_declaration))
22210 remove_AT (subr_die, DW_AT_object_pointer);
22211 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22214 /* Make a specification pointing to the previously built
22215 declaration. */
22216 else
22218 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22219 add_AT_specification (subr_die, old_die);
22220 add_pubname (decl, subr_die);
22221 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22222 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22223 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22224 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22225 if (debug_column_info
22226 && s.column
22227 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22228 != (unsigned) s.column))
22229 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22231 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22232 emit the real type on the definition die. */
22233 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22235 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22236 if (die == auto_die || die == decltype_auto_die)
22237 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22238 TYPE_UNQUALIFIED, false, context_die);
22241 /* When we process the method declaration, we haven't seen
22242 the out-of-class defaulted definition yet, so we have to
22243 recheck now. */
22244 if ((dwarf_version >= 5 || ! dwarf_strict)
22245 && !get_AT (subr_die, DW_AT_defaulted))
22247 int defaulted
22248 = lang_hooks.decls.decl_dwarf_attribute (decl,
22249 DW_AT_defaulted);
22250 if (defaulted != -1)
22252 /* Other values must have been handled before. */
22253 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22254 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22259 /* Create a fresh DIE for anything else. */
22260 else
22262 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22264 if (TREE_PUBLIC (decl))
22265 add_AT_flag (subr_die, DW_AT_external, 1);
22267 add_name_and_src_coords_attributes (subr_die, decl);
22268 add_pubname (decl, subr_die);
22269 if (debug_info_level > DINFO_LEVEL_TERSE)
22271 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22272 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22273 TYPE_UNQUALIFIED, false, context_die);
22276 add_pure_or_virtual_attribute (subr_die, decl);
22277 if (DECL_ARTIFICIAL (decl))
22278 add_AT_flag (subr_die, DW_AT_artificial, 1);
22280 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22281 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22283 add_alignment_attribute (subr_die, decl);
22285 add_accessibility_attribute (subr_die, decl);
22288 /* Unless we have an existing non-declaration DIE, equate the new
22289 DIE. */
22290 if (!old_die || is_declaration_die (old_die))
22291 equate_decl_number_to_die (decl, subr_die);
22293 if (declaration)
22295 if (!old_die || !get_AT (old_die, DW_AT_inline))
22297 add_AT_flag (subr_die, DW_AT_declaration, 1);
22299 /* If this is an explicit function declaration then generate
22300 a DW_AT_explicit attribute. */
22301 if ((dwarf_version >= 3 || !dwarf_strict)
22302 && lang_hooks.decls.decl_dwarf_attribute (decl,
22303 DW_AT_explicit) == 1)
22304 add_AT_flag (subr_die, DW_AT_explicit, 1);
22306 /* If this is a C++11 deleted special function member then generate
22307 a DW_AT_deleted attribute. */
22308 if ((dwarf_version >= 5 || !dwarf_strict)
22309 && lang_hooks.decls.decl_dwarf_attribute (decl,
22310 DW_AT_deleted) == 1)
22311 add_AT_flag (subr_die, DW_AT_deleted, 1);
22313 /* If this is a C++11 defaulted special function member then
22314 generate a DW_AT_defaulted attribute. */
22315 if (dwarf_version >= 5 || !dwarf_strict)
22317 int defaulted
22318 = lang_hooks.decls.decl_dwarf_attribute (decl,
22319 DW_AT_defaulted);
22320 if (defaulted != -1)
22321 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22324 /* If this is a C++11 non-static member function with & ref-qualifier
22325 then generate a DW_AT_reference attribute. */
22326 if ((dwarf_version >= 5 || !dwarf_strict)
22327 && lang_hooks.decls.decl_dwarf_attribute (decl,
22328 DW_AT_reference) == 1)
22329 add_AT_flag (subr_die, DW_AT_reference, 1);
22331 /* If this is a C++11 non-static member function with &&
22332 ref-qualifier then generate a DW_AT_reference attribute. */
22333 if ((dwarf_version >= 5 || !dwarf_strict)
22334 && lang_hooks.decls.decl_dwarf_attribute (decl,
22335 DW_AT_rvalue_reference)
22336 == 1)
22337 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22340 /* Tag abstract instances with DW_AT_inline. */
22341 else if (DECL_ABSTRACT_P (decl))
22343 if (DECL_DECLARED_INLINE_P (decl))
22345 if (cgraph_function_possibly_inlined_p (decl))
22346 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22347 else
22348 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22350 else
22352 if (cgraph_function_possibly_inlined_p (decl))
22353 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22354 else
22355 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22358 if (DECL_DECLARED_INLINE_P (decl)
22359 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22360 add_AT_flag (subr_die, DW_AT_artificial, 1);
22362 /* For non DECL_EXTERNALs, if range information is available, fill
22363 the DIE with it. */
22364 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22366 HOST_WIDE_INT cfa_fb_offset;
22368 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22370 if (!crtl->has_bb_partition)
22372 dw_fde_ref fde = fun->fde;
22373 if (fde->dw_fde_begin)
22375 /* We have already generated the labels. */
22376 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22377 fde->dw_fde_end, false);
22379 else
22381 /* Create start/end labels and add the range. */
22382 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22383 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22384 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22385 current_function_funcdef_no);
22386 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22387 current_function_funcdef_no);
22388 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22389 false);
22392 #if VMS_DEBUGGING_INFO
22393 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22394 Section 2.3 Prologue and Epilogue Attributes:
22395 When a breakpoint is set on entry to a function, it is generally
22396 desirable for execution to be suspended, not on the very first
22397 instruction of the function, but rather at a point after the
22398 function's frame has been set up, after any language defined local
22399 declaration processing has been completed, and before execution of
22400 the first statement of the function begins. Debuggers generally
22401 cannot properly determine where this point is. Similarly for a
22402 breakpoint set on exit from a function. The prologue and epilogue
22403 attributes allow a compiler to communicate the location(s) to use. */
22406 if (fde->dw_fde_vms_end_prologue)
22407 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22408 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22410 if (fde->dw_fde_vms_begin_epilogue)
22411 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22412 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22414 #endif
22417 else
22419 /* Generate pubnames entries for the split function code ranges. */
22420 dw_fde_ref fde = fun->fde;
22422 if (fde->dw_fde_second_begin)
22424 if (dwarf_version >= 3 || !dwarf_strict)
22426 /* We should use ranges for non-contiguous code section
22427 addresses. Use the actual code range for the initial
22428 section, since the HOT/COLD labels might precede an
22429 alignment offset. */
22430 bool range_list_added = false;
22431 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22432 fde->dw_fde_end, &range_list_added,
22433 false);
22434 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22435 fde->dw_fde_second_end,
22436 &range_list_added, false);
22437 if (range_list_added)
22438 add_ranges (NULL);
22440 else
22442 /* There is no real support in DW2 for this .. so we make
22443 a work-around. First, emit the pub name for the segment
22444 containing the function label. Then make and emit a
22445 simplified subprogram DIE for the second segment with the
22446 name pre-fixed by __hot/cold_sect_of_. We use the same
22447 linkage name for the second die so that gdb will find both
22448 sections when given "b foo". */
22449 const char *name = NULL;
22450 tree decl_name = DECL_NAME (decl);
22451 dw_die_ref seg_die;
22453 /* Do the 'primary' section. */
22454 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22455 fde->dw_fde_end, false);
22457 /* Build a minimal DIE for the secondary section. */
22458 seg_die = new_die (DW_TAG_subprogram,
22459 subr_die->die_parent, decl);
22461 if (TREE_PUBLIC (decl))
22462 add_AT_flag (seg_die, DW_AT_external, 1);
22464 if (decl_name != NULL
22465 && IDENTIFIER_POINTER (decl_name) != NULL)
22467 name = dwarf2_name (decl, 1);
22468 if (! DECL_ARTIFICIAL (decl))
22469 add_src_coords_attributes (seg_die, decl);
22471 add_linkage_name (seg_die, decl);
22473 gcc_assert (name != NULL);
22474 add_pure_or_virtual_attribute (seg_die, decl);
22475 if (DECL_ARTIFICIAL (decl))
22476 add_AT_flag (seg_die, DW_AT_artificial, 1);
22478 name = concat ("__second_sect_of_", name, NULL);
22479 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22480 fde->dw_fde_second_end, false);
22481 add_name_attribute (seg_die, name);
22482 if (want_pubnames ())
22483 add_pubname_string (name, seg_die);
22486 else
22487 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22488 false);
22491 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22493 /* We define the "frame base" as the function's CFA. This is more
22494 convenient for several reasons: (1) It's stable across the prologue
22495 and epilogue, which makes it better than just a frame pointer,
22496 (2) With dwarf3, there exists a one-byte encoding that allows us
22497 to reference the .debug_frame data by proxy, but failing that,
22498 (3) We can at least reuse the code inspection and interpretation
22499 code that determines the CFA position at various points in the
22500 function. */
22501 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22503 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22504 add_AT_loc (subr_die, DW_AT_frame_base, op);
22506 else
22508 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22509 if (list->dw_loc_next)
22510 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22511 else
22512 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22515 /* Compute a displacement from the "steady-state frame pointer" to
22516 the CFA. The former is what all stack slots and argument slots
22517 will reference in the rtl; the latter is what we've told the
22518 debugger about. We'll need to adjust all frame_base references
22519 by this displacement. */
22520 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22522 if (fun->static_chain_decl)
22524 /* DWARF requires here a location expression that computes the
22525 address of the enclosing subprogram's frame base. The machinery
22526 in tree-nested.c is supposed to store this specific address in the
22527 last field of the FRAME record. */
22528 const tree frame_type
22529 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22530 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22532 tree fb_expr
22533 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22534 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22535 fb_expr, fb_decl, NULL_TREE);
22537 add_AT_location_description (subr_die, DW_AT_static_link,
22538 loc_list_from_tree (fb_expr, 0, NULL));
22541 resolve_variable_values ();
22544 /* Generate child dies for template paramaters. */
22545 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22546 gen_generic_params_dies (decl);
22548 /* Now output descriptions of the arguments for this function. This gets
22549 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22550 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22551 `...' at the end of the formal parameter list. In order to find out if
22552 there was a trailing ellipsis or not, we must instead look at the type
22553 associated with the FUNCTION_DECL. This will be a node of type
22554 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22555 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22556 an ellipsis at the end. */
22558 /* In the case where we are describing a mere function declaration, all we
22559 need to do here (and all we *can* do here) is to describe the *types* of
22560 its formal parameters. */
22561 if (debug_info_level <= DINFO_LEVEL_TERSE)
22563 else if (declaration)
22564 gen_formal_types_die (decl, subr_die);
22565 else
22567 /* Generate DIEs to represent all known formal parameters. */
22568 tree parm = DECL_ARGUMENTS (decl);
22569 tree generic_decl = early_dwarf
22570 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22571 tree generic_decl_parm = generic_decl
22572 ? DECL_ARGUMENTS (generic_decl)
22573 : NULL;
22575 /* Now we want to walk the list of parameters of the function and
22576 emit their relevant DIEs.
22578 We consider the case of DECL being an instance of a generic function
22579 as well as it being a normal function.
22581 If DECL is an instance of a generic function we walk the
22582 parameters of the generic function declaration _and_ the parameters of
22583 DECL itself. This is useful because we want to emit specific DIEs for
22584 function parameter packs and those are declared as part of the
22585 generic function declaration. In that particular case,
22586 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22587 That DIE has children DIEs representing the set of arguments
22588 of the pack. Note that the set of pack arguments can be empty.
22589 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22590 children DIE.
22592 Otherwise, we just consider the parameters of DECL. */
22593 while (generic_decl_parm || parm)
22595 if (generic_decl_parm
22596 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22597 gen_formal_parameter_pack_die (generic_decl_parm,
22598 parm, subr_die,
22599 &parm);
22600 else if (parm && !POINTER_BOUNDS_P (parm))
22602 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22604 if (parm == DECL_ARGUMENTS (decl)
22605 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22606 && parm_die
22607 && (dwarf_version >= 3 || !dwarf_strict))
22608 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22610 parm = DECL_CHAIN (parm);
22612 else if (parm)
22613 parm = DECL_CHAIN (parm);
22615 if (generic_decl_parm)
22616 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22619 /* Decide whether we need an unspecified_parameters DIE at the end.
22620 There are 2 more cases to do this for: 1) the ansi ... declaration -
22621 this is detectable when the end of the arg list is not a
22622 void_type_node 2) an unprototyped function declaration (not a
22623 definition). This just means that we have no info about the
22624 parameters at all. */
22625 if (early_dwarf)
22627 if (prototype_p (TREE_TYPE (decl)))
22629 /* This is the prototyped case, check for.... */
22630 if (stdarg_p (TREE_TYPE (decl)))
22631 gen_unspecified_parameters_die (decl, subr_die);
22633 else if (DECL_INITIAL (decl) == NULL_TREE)
22634 gen_unspecified_parameters_die (decl, subr_die);
22638 if (subr_die != old_die)
22639 /* Add the calling convention attribute if requested. */
22640 add_calling_convention_attribute (subr_die, decl);
22642 /* Output Dwarf info for all of the stuff within the body of the function
22643 (if it has one - it may be just a declaration).
22645 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22646 a function. This BLOCK actually represents the outermost binding contour
22647 for the function, i.e. the contour in which the function's formal
22648 parameters and labels get declared. Curiously, it appears that the front
22649 end doesn't actually put the PARM_DECL nodes for the current function onto
22650 the BLOCK_VARS list for this outer scope, but are strung off of the
22651 DECL_ARGUMENTS list for the function instead.
22653 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22654 the LABEL_DECL nodes for the function however, and we output DWARF info
22655 for those in decls_for_scope. Just within the `outer_scope' there will be
22656 a BLOCK node representing the function's outermost pair of curly braces,
22657 and any blocks used for the base and member initializers of a C++
22658 constructor function. */
22659 tree outer_scope = DECL_INITIAL (decl);
22660 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22662 int call_site_note_count = 0;
22663 int tail_call_site_note_count = 0;
22665 /* Emit a DW_TAG_variable DIE for a named return value. */
22666 if (DECL_NAME (DECL_RESULT (decl)))
22667 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22669 /* The first time through decls_for_scope we will generate the
22670 DIEs for the locals. The second time, we fill in the
22671 location info. */
22672 decls_for_scope (outer_scope, subr_die);
22674 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22676 struct call_arg_loc_node *ca_loc;
22677 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22679 dw_die_ref die = NULL;
22680 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22681 rtx arg, next_arg;
22683 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22684 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22685 : NULL_RTX);
22686 arg; arg = next_arg)
22688 dw_loc_descr_ref reg, val;
22689 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22690 dw_die_ref cdie, tdie = NULL;
22692 next_arg = XEXP (arg, 1);
22693 if (REG_P (XEXP (XEXP (arg, 0), 0))
22694 && next_arg
22695 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22696 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22697 && REGNO (XEXP (XEXP (arg, 0), 0))
22698 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22699 next_arg = XEXP (next_arg, 1);
22700 if (mode == VOIDmode)
22702 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22703 if (mode == VOIDmode)
22704 mode = GET_MODE (XEXP (arg, 0));
22706 if (mode == VOIDmode || mode == BLKmode)
22707 continue;
22708 /* Get dynamic information about call target only if we
22709 have no static information: we cannot generate both
22710 DW_AT_call_origin and DW_AT_call_target
22711 attributes. */
22712 if (ca_loc->symbol_ref == NULL_RTX)
22714 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22716 tloc = XEXP (XEXP (arg, 0), 1);
22717 continue;
22719 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22720 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22722 tlocc = XEXP (XEXP (arg, 0), 1);
22723 continue;
22726 reg = NULL;
22727 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22728 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22729 VAR_INIT_STATUS_INITIALIZED);
22730 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22732 rtx mem = XEXP (XEXP (arg, 0), 0);
22733 reg = mem_loc_descriptor (XEXP (mem, 0),
22734 get_address_mode (mem),
22735 GET_MODE (mem),
22736 VAR_INIT_STATUS_INITIALIZED);
22738 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22739 == DEBUG_PARAMETER_REF)
22741 tree tdecl
22742 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22743 tdie = lookup_decl_die (tdecl);
22744 if (tdie == NULL)
22745 continue;
22747 else
22748 continue;
22749 if (reg == NULL
22750 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22751 != DEBUG_PARAMETER_REF)
22752 continue;
22753 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22754 VOIDmode,
22755 VAR_INIT_STATUS_INITIALIZED);
22756 if (val == NULL)
22757 continue;
22758 if (die == NULL)
22759 die = gen_call_site_die (decl, subr_die, ca_loc);
22760 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22761 NULL_TREE);
22762 if (reg != NULL)
22763 add_AT_loc (cdie, DW_AT_location, reg);
22764 else if (tdie != NULL)
22765 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22766 tdie);
22767 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22768 if (next_arg != XEXP (arg, 1))
22770 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22771 if (mode == VOIDmode)
22772 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22773 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22774 0), 1),
22775 mode, VOIDmode,
22776 VAR_INIT_STATUS_INITIALIZED);
22777 if (val != NULL)
22778 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22779 val);
22782 if (die == NULL
22783 && (ca_loc->symbol_ref || tloc))
22784 die = gen_call_site_die (decl, subr_die, ca_loc);
22785 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22787 dw_loc_descr_ref tval = NULL;
22789 if (tloc != NULL_RTX)
22790 tval = mem_loc_descriptor (tloc,
22791 GET_MODE (tloc) == VOIDmode
22792 ? Pmode : GET_MODE (tloc),
22793 VOIDmode,
22794 VAR_INIT_STATUS_INITIALIZED);
22795 if (tval)
22796 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22797 else if (tlocc != NULL_RTX)
22799 tval = mem_loc_descriptor (tlocc,
22800 GET_MODE (tlocc) == VOIDmode
22801 ? Pmode : GET_MODE (tlocc),
22802 VOIDmode,
22803 VAR_INIT_STATUS_INITIALIZED);
22804 if (tval)
22805 add_AT_loc (die,
22806 dwarf_AT (DW_AT_call_target_clobbered),
22807 tval);
22810 if (die != NULL)
22812 call_site_note_count++;
22813 if (ca_loc->tail_call_p)
22814 tail_call_site_note_count++;
22818 call_arg_locations = NULL;
22819 call_arg_loc_last = NULL;
22820 if (tail_call_site_count >= 0
22821 && tail_call_site_count == tail_call_site_note_count
22822 && (!dwarf_strict || dwarf_version >= 5))
22824 if (call_site_count >= 0
22825 && call_site_count == call_site_note_count)
22826 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22827 else
22828 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22830 call_site_count = -1;
22831 tail_call_site_count = -1;
22834 /* Mark used types after we have created DIEs for the functions scopes. */
22835 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22838 /* Returns a hash value for X (which really is a die_struct). */
22840 hashval_t
22841 block_die_hasher::hash (die_struct *d)
22843 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22846 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22847 as decl_id and die_parent of die_struct Y. */
22849 bool
22850 block_die_hasher::equal (die_struct *x, die_struct *y)
22852 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22855 /* Return TRUE if DECL, which may have been previously generated as
22856 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22857 true if decl (or its origin) is either an extern declaration or a
22858 class/namespace scoped declaration.
22860 The declare_in_namespace support causes us to get two DIEs for one
22861 variable, both of which are declarations. We want to avoid
22862 considering one to be a specification, so we must test for
22863 DECLARATION and DW_AT_declaration. */
22864 static inline bool
22865 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22867 return (old_die && TREE_STATIC (decl) && !declaration
22868 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22871 /* Return true if DECL is a local static. */
22873 static inline bool
22874 local_function_static (tree decl)
22876 gcc_assert (VAR_P (decl));
22877 return TREE_STATIC (decl)
22878 && DECL_CONTEXT (decl)
22879 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22882 /* Generate a DIE to represent a declared data object.
22883 Either DECL or ORIGIN must be non-null. */
22885 static void
22886 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22888 HOST_WIDE_INT off = 0;
22889 tree com_decl;
22890 tree decl_or_origin = decl ? decl : origin;
22891 tree ultimate_origin;
22892 dw_die_ref var_die;
22893 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22894 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22895 || class_or_namespace_scope_p (context_die));
22896 bool specialization_p = false;
22897 bool no_linkage_name = false;
22899 /* While C++ inline static data members have definitions inside of the
22900 class, force the first DIE to be a declaration, then let gen_member_die
22901 reparent it to the class context and call gen_variable_die again
22902 to create the outside of the class DIE for the definition. */
22903 if (!declaration
22904 && old_die == NULL
22905 && decl
22906 && DECL_CONTEXT (decl)
22907 && TYPE_P (DECL_CONTEXT (decl))
22908 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22910 declaration = true;
22911 if (dwarf_version < 5)
22912 no_linkage_name = true;
22915 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22916 if (decl || ultimate_origin)
22917 origin = ultimate_origin;
22918 com_decl = fortran_common (decl_or_origin, &off);
22920 /* Symbol in common gets emitted as a child of the common block, in the form
22921 of a data member. */
22922 if (com_decl)
22924 dw_die_ref com_die;
22925 dw_loc_list_ref loc = NULL;
22926 die_node com_die_arg;
22928 var_die = lookup_decl_die (decl_or_origin);
22929 if (var_die)
22931 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22933 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22934 if (loc)
22936 if (off)
22938 /* Optimize the common case. */
22939 if (single_element_loc_list_p (loc)
22940 && loc->expr->dw_loc_opc == DW_OP_addr
22941 && loc->expr->dw_loc_next == NULL
22942 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22943 == SYMBOL_REF)
22945 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22946 loc->expr->dw_loc_oprnd1.v.val_addr
22947 = plus_constant (GET_MODE (x), x , off);
22949 else
22950 loc_list_plus_const (loc, off);
22952 add_AT_location_description (var_die, DW_AT_location, loc);
22953 remove_AT (var_die, DW_AT_declaration);
22956 return;
22959 if (common_block_die_table == NULL)
22960 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22962 com_die_arg.decl_id = DECL_UID (com_decl);
22963 com_die_arg.die_parent = context_die;
22964 com_die = common_block_die_table->find (&com_die_arg);
22965 if (! early_dwarf)
22966 loc = loc_list_from_tree (com_decl, 2, NULL);
22967 if (com_die == NULL)
22969 const char *cnam
22970 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22971 die_node **slot;
22973 com_die = new_die (DW_TAG_common_block, context_die, decl);
22974 add_name_and_src_coords_attributes (com_die, com_decl);
22975 if (loc)
22977 add_AT_location_description (com_die, DW_AT_location, loc);
22978 /* Avoid sharing the same loc descriptor between
22979 DW_TAG_common_block and DW_TAG_variable. */
22980 loc = loc_list_from_tree (com_decl, 2, NULL);
22982 else if (DECL_EXTERNAL (decl_or_origin))
22983 add_AT_flag (com_die, DW_AT_declaration, 1);
22984 if (want_pubnames ())
22985 add_pubname_string (cnam, com_die); /* ??? needed? */
22986 com_die->decl_id = DECL_UID (com_decl);
22987 slot = common_block_die_table->find_slot (com_die, INSERT);
22988 *slot = com_die;
22990 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22992 add_AT_location_description (com_die, DW_AT_location, loc);
22993 loc = loc_list_from_tree (com_decl, 2, NULL);
22994 remove_AT (com_die, DW_AT_declaration);
22996 var_die = new_die (DW_TAG_variable, com_die, decl);
22997 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22998 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22999 decl_quals (decl_or_origin), false,
23000 context_die);
23001 add_alignment_attribute (var_die, decl);
23002 add_AT_flag (var_die, DW_AT_external, 1);
23003 if (loc)
23005 if (off)
23007 /* Optimize the common case. */
23008 if (single_element_loc_list_p (loc)
23009 && loc->expr->dw_loc_opc == DW_OP_addr
23010 && loc->expr->dw_loc_next == NULL
23011 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23013 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23014 loc->expr->dw_loc_oprnd1.v.val_addr
23015 = plus_constant (GET_MODE (x), x, off);
23017 else
23018 loc_list_plus_const (loc, off);
23020 add_AT_location_description (var_die, DW_AT_location, loc);
23022 else if (DECL_EXTERNAL (decl_or_origin))
23023 add_AT_flag (var_die, DW_AT_declaration, 1);
23024 if (decl)
23025 equate_decl_number_to_die (decl, var_die);
23026 return;
23029 if (old_die)
23031 if (declaration)
23033 /* A declaration that has been previously dumped, needs no
23034 further annotations, since it doesn't need location on
23035 the second pass. */
23036 return;
23038 else if (decl_will_get_specification_p (old_die, decl, declaration)
23039 && !get_AT (old_die, DW_AT_specification))
23041 /* Fall-thru so we can make a new variable die along with a
23042 DW_AT_specification. */
23044 else if (origin && old_die->die_parent != context_die)
23046 /* If we will be creating an inlined instance, we need a
23047 new DIE that will get annotated with
23048 DW_AT_abstract_origin. Clear things so we can get a
23049 new DIE. */
23050 gcc_assert (!DECL_ABSTRACT_P (decl));
23051 old_die = NULL;
23053 else
23055 /* If a DIE was dumped early, it still needs location info.
23056 Skip to where we fill the location bits. */
23057 var_die = old_die;
23059 /* ??? In LTRANS we cannot annotate early created variably
23060 modified type DIEs without copying them and adjusting all
23061 references to them. Thus we dumped them again, also add a
23062 reference to them. */
23063 tree type = TREE_TYPE (decl_or_origin);
23064 if (in_lto_p
23065 && variably_modified_type_p
23066 (type, decl_function_context (decl_or_origin)))
23068 if (decl_by_reference_p (decl_or_origin))
23069 add_type_attribute (var_die, TREE_TYPE (type),
23070 TYPE_UNQUALIFIED, false, context_die);
23071 else
23072 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23073 false, context_die);
23076 goto gen_variable_die_location;
23080 /* For static data members, the declaration in the class is supposed
23081 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23082 also in DWARF2; the specification should still be DW_TAG_variable
23083 referencing the DW_TAG_member DIE. */
23084 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23085 var_die = new_die (DW_TAG_member, context_die, decl);
23086 else
23087 var_die = new_die (DW_TAG_variable, context_die, decl);
23089 if (origin != NULL)
23090 add_abstract_origin_attribute (var_die, origin);
23092 /* Loop unrolling can create multiple blocks that refer to the same
23093 static variable, so we must test for the DW_AT_declaration flag.
23095 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23096 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23097 sharing them.
23099 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23100 else if (decl_will_get_specification_p (old_die, decl, declaration))
23102 /* This is a definition of a C++ class level static. */
23103 add_AT_specification (var_die, old_die);
23104 specialization_p = true;
23105 if (DECL_NAME (decl))
23107 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23108 struct dwarf_file_data * file_index = lookup_filename (s.file);
23110 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23111 add_AT_file (var_die, DW_AT_decl_file, file_index);
23113 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23114 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23116 if (debug_column_info
23117 && s.column
23118 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23119 != (unsigned) s.column))
23120 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23122 if (old_die->die_tag == DW_TAG_member)
23123 add_linkage_name (var_die, decl);
23126 else
23127 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23129 if ((origin == NULL && !specialization_p)
23130 || (origin != NULL
23131 && !DECL_ABSTRACT_P (decl_or_origin)
23132 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23133 decl_function_context
23134 (decl_or_origin))))
23136 tree type = TREE_TYPE (decl_or_origin);
23138 if (decl_by_reference_p (decl_or_origin))
23139 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23140 context_die);
23141 else
23142 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23143 context_die);
23146 if (origin == NULL && !specialization_p)
23148 if (TREE_PUBLIC (decl))
23149 add_AT_flag (var_die, DW_AT_external, 1);
23151 if (DECL_ARTIFICIAL (decl))
23152 add_AT_flag (var_die, DW_AT_artificial, 1);
23154 add_alignment_attribute (var_die, decl);
23156 add_accessibility_attribute (var_die, decl);
23159 if (declaration)
23160 add_AT_flag (var_die, DW_AT_declaration, 1);
23162 if (decl && (DECL_ABSTRACT_P (decl)
23163 || !old_die || is_declaration_die (old_die)))
23164 equate_decl_number_to_die (decl, var_die);
23166 gen_variable_die_location:
23167 if (! declaration
23168 && (! DECL_ABSTRACT_P (decl_or_origin)
23169 /* Local static vars are shared between all clones/inlines,
23170 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23171 already set. */
23172 || (VAR_P (decl_or_origin)
23173 && TREE_STATIC (decl_or_origin)
23174 && DECL_RTL_SET_P (decl_or_origin))))
23176 if (early_dwarf)
23177 add_pubname (decl_or_origin, var_die);
23178 else
23179 add_location_or_const_value_attribute (var_die, decl_or_origin,
23180 decl == NULL);
23182 else
23183 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23185 if ((dwarf_version >= 4 || !dwarf_strict)
23186 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23187 DW_AT_const_expr) == 1
23188 && !get_AT (var_die, DW_AT_const_expr)
23189 && !specialization_p)
23190 add_AT_flag (var_die, DW_AT_const_expr, 1);
23192 if (!dwarf_strict)
23194 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23195 DW_AT_inline);
23196 if (inl != -1
23197 && !get_AT (var_die, DW_AT_inline)
23198 && !specialization_p)
23199 add_AT_unsigned (var_die, DW_AT_inline, inl);
23203 /* Generate a DIE to represent a named constant. */
23205 static void
23206 gen_const_die (tree decl, dw_die_ref context_die)
23208 dw_die_ref const_die;
23209 tree type = TREE_TYPE (decl);
23211 const_die = lookup_decl_die (decl);
23212 if (const_die)
23213 return;
23215 const_die = new_die (DW_TAG_constant, context_die, decl);
23216 equate_decl_number_to_die (decl, const_die);
23217 add_name_and_src_coords_attributes (const_die, decl);
23218 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23219 if (TREE_PUBLIC (decl))
23220 add_AT_flag (const_die, DW_AT_external, 1);
23221 if (DECL_ARTIFICIAL (decl))
23222 add_AT_flag (const_die, DW_AT_artificial, 1);
23223 tree_add_const_value_attribute_for_decl (const_die, decl);
23226 /* Generate a DIE to represent a label identifier. */
23228 static void
23229 gen_label_die (tree decl, dw_die_ref context_die)
23231 tree origin = decl_ultimate_origin (decl);
23232 dw_die_ref lbl_die = lookup_decl_die (decl);
23233 rtx insn;
23234 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23236 if (!lbl_die)
23238 lbl_die = new_die (DW_TAG_label, context_die, decl);
23239 equate_decl_number_to_die (decl, lbl_die);
23241 if (origin != NULL)
23242 add_abstract_origin_attribute (lbl_die, origin);
23243 else
23244 add_name_and_src_coords_attributes (lbl_die, decl);
23247 if (DECL_ABSTRACT_P (decl))
23248 equate_decl_number_to_die (decl, lbl_die);
23249 else if (! early_dwarf)
23251 insn = DECL_RTL_IF_SET (decl);
23253 /* Deleted labels are programmer specified labels which have been
23254 eliminated because of various optimizations. We still emit them
23255 here so that it is possible to put breakpoints on them. */
23256 if (insn
23257 && (LABEL_P (insn)
23258 || ((NOTE_P (insn)
23259 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23261 /* When optimization is enabled (via -O) some parts of the compiler
23262 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23263 represent source-level labels which were explicitly declared by
23264 the user. This really shouldn't be happening though, so catch
23265 it if it ever does happen. */
23266 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23268 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23269 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23271 else if (insn
23272 && NOTE_P (insn)
23273 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23274 && CODE_LABEL_NUMBER (insn) != -1)
23276 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23277 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23282 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23283 attributes to the DIE for a block STMT, to describe where the inlined
23284 function was called from. This is similar to add_src_coords_attributes. */
23286 static inline void
23287 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23289 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23291 if (dwarf_version >= 3 || !dwarf_strict)
23293 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23294 add_AT_unsigned (die, DW_AT_call_line, s.line);
23295 if (debug_column_info && s.column)
23296 add_AT_unsigned (die, DW_AT_call_column, s.column);
23301 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23302 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23304 static inline void
23305 add_high_low_attributes (tree stmt, dw_die_ref die)
23307 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23309 if (BLOCK_FRAGMENT_CHAIN (stmt)
23310 && (dwarf_version >= 3 || !dwarf_strict))
23312 tree chain, superblock = NULL_TREE;
23313 dw_die_ref pdie;
23314 dw_attr_node *attr = NULL;
23316 if (inlined_function_outer_scope_p (stmt))
23318 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23319 BLOCK_NUMBER (stmt));
23320 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23323 /* Optimize duplicate .debug_ranges lists or even tails of
23324 lists. If this BLOCK has same ranges as its supercontext,
23325 lookup DW_AT_ranges attribute in the supercontext (and
23326 recursively so), verify that the ranges_table contains the
23327 right values and use it instead of adding a new .debug_range. */
23328 for (chain = stmt, pdie = die;
23329 BLOCK_SAME_RANGE (chain);
23330 chain = BLOCK_SUPERCONTEXT (chain))
23332 dw_attr_node *new_attr;
23334 pdie = pdie->die_parent;
23335 if (pdie == NULL)
23336 break;
23337 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23338 break;
23339 new_attr = get_AT (pdie, DW_AT_ranges);
23340 if (new_attr == NULL
23341 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23342 break;
23343 attr = new_attr;
23344 superblock = BLOCK_SUPERCONTEXT (chain);
23346 if (attr != NULL
23347 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23348 == BLOCK_NUMBER (superblock))
23349 && BLOCK_FRAGMENT_CHAIN (superblock))
23351 unsigned long off = attr->dw_attr_val.v.val_offset;
23352 unsigned long supercnt = 0, thiscnt = 0;
23353 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23354 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23356 ++supercnt;
23357 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23358 == BLOCK_NUMBER (chain));
23360 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23361 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23362 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23363 ++thiscnt;
23364 gcc_assert (supercnt >= thiscnt);
23365 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23366 false);
23367 note_rnglist_head (off + supercnt - thiscnt);
23368 return;
23371 unsigned int offset = add_ranges (stmt, true);
23372 add_AT_range_list (die, DW_AT_ranges, offset, false);
23373 note_rnglist_head (offset);
23375 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23376 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23379 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23380 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23381 chain = BLOCK_FRAGMENT_CHAIN (chain);
23383 while (chain);
23384 add_ranges (NULL);
23386 else
23388 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23389 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23390 BLOCK_NUMBER (stmt));
23391 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23392 BLOCK_NUMBER (stmt));
23393 add_AT_low_high_pc (die, label, label_high, false);
23397 /* Generate a DIE for a lexical block. */
23399 static void
23400 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23402 dw_die_ref old_die = BLOCK_DIE (stmt);
23403 dw_die_ref stmt_die = NULL;
23404 if (!old_die)
23406 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23407 BLOCK_DIE (stmt) = stmt_die;
23410 if (BLOCK_ABSTRACT (stmt))
23412 if (old_die)
23414 /* This must have been generated early and it won't even
23415 need location information since it's a DW_AT_inline
23416 function. */
23417 if (flag_checking)
23418 for (dw_die_ref c = context_die; c; c = c->die_parent)
23419 if (c->die_tag == DW_TAG_inlined_subroutine
23420 || c->die_tag == DW_TAG_subprogram)
23422 gcc_assert (get_AT (c, DW_AT_inline));
23423 break;
23425 return;
23428 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23430 /* If this is an inlined instance, create a new lexical die for
23431 anything below to attach DW_AT_abstract_origin to. */
23432 if (old_die)
23434 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23435 BLOCK_DIE (stmt) = stmt_die;
23436 old_die = NULL;
23439 tree origin = block_ultimate_origin (stmt);
23440 if (origin != NULL_TREE && origin != stmt)
23441 add_abstract_origin_attribute (stmt_die, origin);
23444 if (old_die)
23445 stmt_die = old_die;
23447 /* A non abstract block whose blocks have already been reordered
23448 should have the instruction range for this block. If so, set the
23449 high/low attributes. */
23450 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23452 gcc_assert (stmt_die);
23453 add_high_low_attributes (stmt, stmt_die);
23456 decls_for_scope (stmt, stmt_die);
23459 /* Generate a DIE for an inlined subprogram. */
23461 static void
23462 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23464 tree decl;
23466 /* The instance of function that is effectively being inlined shall not
23467 be abstract. */
23468 gcc_assert (! BLOCK_ABSTRACT (stmt));
23470 decl = block_ultimate_origin (stmt);
23472 /* Make sure any inlined functions are known to be inlineable. */
23473 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23474 || cgraph_function_possibly_inlined_p (decl));
23476 if (! BLOCK_ABSTRACT (stmt))
23478 dw_die_ref subr_die
23479 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23481 if (call_arg_locations)
23482 BLOCK_DIE (stmt) = subr_die;
23483 add_abstract_origin_attribute (subr_die, decl);
23484 if (TREE_ASM_WRITTEN (stmt))
23485 add_high_low_attributes (stmt, subr_die);
23486 add_call_src_coords_attributes (stmt, subr_die);
23488 decls_for_scope (stmt, subr_die);
23492 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23493 the comment for VLR_CONTEXT. */
23495 static void
23496 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23498 dw_die_ref decl_die;
23500 if (TREE_TYPE (decl) == error_mark_node)
23501 return;
23503 decl_die = new_die (DW_TAG_member, context_die, decl);
23504 add_name_and_src_coords_attributes (decl_die, decl);
23505 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23506 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23507 context_die);
23509 if (DECL_BIT_FIELD_TYPE (decl))
23511 add_byte_size_attribute (decl_die, decl);
23512 add_bit_size_attribute (decl_die, decl);
23513 add_bit_offset_attribute (decl_die, decl, ctx);
23516 add_alignment_attribute (decl_die, decl);
23518 /* If we have a variant part offset, then we are supposed to process a member
23519 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23520 trees. */
23521 gcc_assert (ctx->variant_part_offset == NULL_TREE
23522 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23523 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23524 add_data_member_location_attribute (decl_die, decl, ctx);
23526 if (DECL_ARTIFICIAL (decl))
23527 add_AT_flag (decl_die, DW_AT_artificial, 1);
23529 add_accessibility_attribute (decl_die, decl);
23531 /* Equate decl number to die, so that we can look up this decl later on. */
23532 equate_decl_number_to_die (decl, decl_die);
23535 /* Generate a DIE for a pointer to a member type. TYPE can be an
23536 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23537 pointer to member function. */
23539 static void
23540 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23542 if (lookup_type_die (type))
23543 return;
23545 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23546 scope_die_for (type, context_die), type);
23548 equate_type_number_to_die (type, ptr_die);
23549 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23550 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23551 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23552 context_die);
23553 add_alignment_attribute (ptr_die, type);
23555 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23556 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23558 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23559 add_AT_loc (ptr_die, DW_AT_use_location, op);
23563 static char *producer_string;
23565 /* Return a heap allocated producer string including command line options
23566 if -grecord-gcc-switches. */
23568 static char *
23569 gen_producer_string (void)
23571 size_t j;
23572 auto_vec<const char *> switches;
23573 const char *language_string = lang_hooks.name;
23574 char *producer, *tail;
23575 const char *p;
23576 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23577 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23579 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23580 switch (save_decoded_options[j].opt_index)
23582 case OPT_o:
23583 case OPT_d:
23584 case OPT_dumpbase:
23585 case OPT_dumpdir:
23586 case OPT_auxbase:
23587 case OPT_auxbase_strip:
23588 case OPT_quiet:
23589 case OPT_version:
23590 case OPT_v:
23591 case OPT_w:
23592 case OPT_L:
23593 case OPT_D:
23594 case OPT_I:
23595 case OPT_U:
23596 case OPT_SPECIAL_unknown:
23597 case OPT_SPECIAL_ignore:
23598 case OPT_SPECIAL_program_name:
23599 case OPT_SPECIAL_input_file:
23600 case OPT_grecord_gcc_switches:
23601 case OPT_gno_record_gcc_switches:
23602 case OPT__output_pch_:
23603 case OPT_fdiagnostics_show_location_:
23604 case OPT_fdiagnostics_show_option:
23605 case OPT_fdiagnostics_show_caret:
23606 case OPT_fdiagnostics_color_:
23607 case OPT_fverbose_asm:
23608 case OPT____:
23609 case OPT__sysroot_:
23610 case OPT_nostdinc:
23611 case OPT_nostdinc__:
23612 case OPT_fpreprocessed:
23613 case OPT_fltrans_output_list_:
23614 case OPT_fresolution_:
23615 case OPT_fdebug_prefix_map_:
23616 /* Ignore these. */
23617 continue;
23618 default:
23619 if (cl_options[save_decoded_options[j].opt_index].flags
23620 & CL_NO_DWARF_RECORD)
23621 continue;
23622 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23623 == '-');
23624 switch (save_decoded_options[j].canonical_option[0][1])
23626 case 'M':
23627 case 'i':
23628 case 'W':
23629 continue;
23630 case 'f':
23631 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23632 "dump", 4) == 0)
23633 continue;
23634 break;
23635 default:
23636 break;
23638 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23639 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23640 break;
23643 producer = XNEWVEC (char, plen + 1 + len + 1);
23644 tail = producer;
23645 sprintf (tail, "%s %s", language_string, version_string);
23646 tail += plen;
23648 FOR_EACH_VEC_ELT (switches, j, p)
23650 len = strlen (p);
23651 *tail = ' ';
23652 memcpy (tail + 1, p, len);
23653 tail += len + 1;
23656 *tail = '\0';
23657 return producer;
23660 /* Given a C and/or C++ language/version string return the "highest".
23661 C++ is assumed to be "higher" than C in this case. Used for merging
23662 LTO translation unit languages. */
23663 static const char *
23664 highest_c_language (const char *lang1, const char *lang2)
23666 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23667 return "GNU C++14";
23668 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23669 return "GNU C++11";
23670 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23671 return "GNU C++98";
23673 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23674 return "GNU C11";
23675 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23676 return "GNU C99";
23677 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23678 return "GNU C89";
23680 gcc_unreachable ();
23684 /* Generate the DIE for the compilation unit. */
23686 static dw_die_ref
23687 gen_compile_unit_die (const char *filename)
23689 dw_die_ref die;
23690 const char *language_string = lang_hooks.name;
23691 int language;
23693 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23695 if (filename)
23697 add_name_attribute (die, filename);
23698 /* Don't add cwd for <built-in>. */
23699 if (filename[0] != '<')
23700 add_comp_dir_attribute (die);
23703 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23705 /* If our producer is LTO try to figure out a common language to use
23706 from the global list of translation units. */
23707 if (strcmp (language_string, "GNU GIMPLE") == 0)
23709 unsigned i;
23710 tree t;
23711 const char *common_lang = NULL;
23713 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23715 if (!TRANSLATION_UNIT_LANGUAGE (t))
23716 continue;
23717 if (!common_lang)
23718 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23719 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23721 else if (strncmp (common_lang, "GNU C", 5) == 0
23722 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23723 /* Mixing C and C++ is ok, use C++ in that case. */
23724 common_lang = highest_c_language (common_lang,
23725 TRANSLATION_UNIT_LANGUAGE (t));
23726 else
23728 /* Fall back to C. */
23729 common_lang = NULL;
23730 break;
23734 if (common_lang)
23735 language_string = common_lang;
23738 language = DW_LANG_C;
23739 if (strncmp (language_string, "GNU C", 5) == 0
23740 && ISDIGIT (language_string[5]))
23742 language = DW_LANG_C89;
23743 if (dwarf_version >= 3 || !dwarf_strict)
23745 if (strcmp (language_string, "GNU C89") != 0)
23746 language = DW_LANG_C99;
23748 if (dwarf_version >= 5 /* || !dwarf_strict */)
23749 if (strcmp (language_string, "GNU C11") == 0)
23750 language = DW_LANG_C11;
23753 else if (strncmp (language_string, "GNU C++", 7) == 0)
23755 language = DW_LANG_C_plus_plus;
23756 if (dwarf_version >= 5 /* || !dwarf_strict */)
23758 if (strcmp (language_string, "GNU C++11") == 0)
23759 language = DW_LANG_C_plus_plus_11;
23760 else if (strcmp (language_string, "GNU C++14") == 0)
23761 language = DW_LANG_C_plus_plus_14;
23764 else if (strcmp (language_string, "GNU F77") == 0)
23765 language = DW_LANG_Fortran77;
23766 else if (dwarf_version >= 3 || !dwarf_strict)
23768 if (strcmp (language_string, "GNU Ada") == 0)
23769 language = DW_LANG_Ada95;
23770 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23772 language = DW_LANG_Fortran95;
23773 if (dwarf_version >= 5 /* || !dwarf_strict */)
23775 if (strcmp (language_string, "GNU Fortran2003") == 0)
23776 language = DW_LANG_Fortran03;
23777 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23778 language = DW_LANG_Fortran08;
23781 else if (strcmp (language_string, "GNU Objective-C") == 0)
23782 language = DW_LANG_ObjC;
23783 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23784 language = DW_LANG_ObjC_plus_plus;
23785 else if (dwarf_version >= 5 || !dwarf_strict)
23787 if (strcmp (language_string, "GNU Go") == 0)
23788 language = DW_LANG_Go;
23791 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23792 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23793 language = DW_LANG_Fortran90;
23795 add_AT_unsigned (die, DW_AT_language, language);
23797 switch (language)
23799 case DW_LANG_Fortran77:
23800 case DW_LANG_Fortran90:
23801 case DW_LANG_Fortran95:
23802 case DW_LANG_Fortran03:
23803 case DW_LANG_Fortran08:
23804 /* Fortran has case insensitive identifiers and the front-end
23805 lowercases everything. */
23806 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23807 break;
23808 default:
23809 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23810 break;
23812 return die;
23815 /* Generate the DIE for a base class. */
23817 static void
23818 gen_inheritance_die (tree binfo, tree access, tree type,
23819 dw_die_ref context_die)
23821 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23822 struct vlr_context ctx = { type, NULL };
23824 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23825 context_die);
23826 add_data_member_location_attribute (die, binfo, &ctx);
23828 if (BINFO_VIRTUAL_P (binfo))
23829 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23831 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23832 children, otherwise the default is DW_ACCESS_public. In DWARF2
23833 the default has always been DW_ACCESS_private. */
23834 if (access == access_public_node)
23836 if (dwarf_version == 2
23837 || context_die->die_tag == DW_TAG_class_type)
23838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23840 else if (access == access_protected_node)
23841 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23842 else if (dwarf_version > 2
23843 && context_die->die_tag != DW_TAG_class_type)
23844 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23847 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23848 structure. */
23849 static bool
23850 is_variant_part (tree decl)
23852 return (TREE_CODE (decl) == FIELD_DECL
23853 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23856 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23857 return the FIELD_DECL. Return NULL_TREE otherwise. */
23859 static tree
23860 analyze_discr_in_predicate (tree operand, tree struct_type)
23862 bool continue_stripping = true;
23863 while (continue_stripping)
23864 switch (TREE_CODE (operand))
23866 CASE_CONVERT:
23867 operand = TREE_OPERAND (operand, 0);
23868 break;
23869 default:
23870 continue_stripping = false;
23871 break;
23874 /* Match field access to members of struct_type only. */
23875 if (TREE_CODE (operand) == COMPONENT_REF
23876 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23877 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23878 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23879 return TREE_OPERAND (operand, 1);
23880 else
23881 return NULL_TREE;
23884 /* Check that SRC is a constant integer that can be represented as a native
23885 integer constant (either signed or unsigned). If so, store it into DEST and
23886 return true. Return false otherwise. */
23888 static bool
23889 get_discr_value (tree src, dw_discr_value *dest)
23891 tree discr_type = TREE_TYPE (src);
23893 if (lang_hooks.types.get_debug_type)
23895 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23896 if (debug_type != NULL)
23897 discr_type = debug_type;
23900 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23901 return false;
23903 /* Signedness can vary between the original type and the debug type. This
23904 can happen for character types in Ada for instance: the character type
23905 used for code generation can be signed, to be compatible with the C one,
23906 but from a debugger point of view, it must be unsigned. */
23907 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23908 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23910 if (is_orig_unsigned != is_debug_unsigned)
23911 src = fold_convert (discr_type, src);
23913 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23914 return false;
23916 dest->pos = is_debug_unsigned;
23917 if (is_debug_unsigned)
23918 dest->v.uval = tree_to_uhwi (src);
23919 else
23920 dest->v.sval = tree_to_shwi (src);
23922 return true;
23925 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23926 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23927 store NULL_TREE in DISCR_DECL. Otherwise:
23929 - store the discriminant field in STRUCT_TYPE that controls the variant
23930 part to *DISCR_DECL
23932 - put in *DISCR_LISTS_P an array where for each variant, the item
23933 represents the corresponding matching list of discriminant values.
23935 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23936 the above array.
23938 Note that when the array is allocated (i.e. when the analysis is
23939 successful), it is up to the caller to free the array. */
23941 static void
23942 analyze_variants_discr (tree variant_part_decl,
23943 tree struct_type,
23944 tree *discr_decl,
23945 dw_discr_list_ref **discr_lists_p,
23946 unsigned *discr_lists_length)
23948 tree variant_part_type = TREE_TYPE (variant_part_decl);
23949 tree variant;
23950 dw_discr_list_ref *discr_lists;
23951 unsigned i;
23953 /* Compute how many variants there are in this variant part. */
23954 *discr_lists_length = 0;
23955 for (variant = TYPE_FIELDS (variant_part_type);
23956 variant != NULL_TREE;
23957 variant = DECL_CHAIN (variant))
23958 ++*discr_lists_length;
23960 *discr_decl = NULL_TREE;
23961 *discr_lists_p
23962 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23963 sizeof (**discr_lists_p));
23964 discr_lists = *discr_lists_p;
23966 /* And then analyze all variants to extract discriminant information for all
23967 of them. This analysis is conservative: as soon as we detect something we
23968 do not support, abort everything and pretend we found nothing. */
23969 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23970 variant != NULL_TREE;
23971 variant = DECL_CHAIN (variant), ++i)
23973 tree match_expr = DECL_QUALIFIER (variant);
23975 /* Now, try to analyze the predicate and deduce a discriminant for
23976 it. */
23977 if (match_expr == boolean_true_node)
23978 /* Typically happens for the default variant: it matches all cases that
23979 previous variants rejected. Don't output any matching value for
23980 this one. */
23981 continue;
23983 /* The following loop tries to iterate over each discriminant
23984 possibility: single values or ranges. */
23985 while (match_expr != NULL_TREE)
23987 tree next_round_match_expr;
23988 tree candidate_discr = NULL_TREE;
23989 dw_discr_list_ref new_node = NULL;
23991 /* Possibilities are matched one after the other by nested
23992 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23993 continue with the rest at next iteration. */
23994 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23996 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23997 match_expr = TREE_OPERAND (match_expr, 1);
23999 else
24000 next_round_match_expr = NULL_TREE;
24002 if (match_expr == boolean_false_node)
24003 /* This sub-expression matches nothing: just wait for the next
24004 one. */
24007 else if (TREE_CODE (match_expr) == EQ_EXPR)
24009 /* We are matching: <discr_field> == <integer_cst>
24010 This sub-expression matches a single value. */
24011 tree integer_cst = TREE_OPERAND (match_expr, 1);
24013 candidate_discr
24014 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24015 struct_type);
24017 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24018 if (!get_discr_value (integer_cst,
24019 &new_node->dw_discr_lower_bound))
24020 goto abort;
24021 new_node->dw_discr_range = false;
24024 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24026 /* We are matching:
24027 <discr_field> > <integer_cst>
24028 && <discr_field> < <integer_cst>.
24029 This sub-expression matches the range of values between the
24030 two matched integer constants. Note that comparisons can be
24031 inclusive or exclusive. */
24032 tree candidate_discr_1, candidate_discr_2;
24033 tree lower_cst, upper_cst;
24034 bool lower_cst_included, upper_cst_included;
24035 tree lower_op = TREE_OPERAND (match_expr, 0);
24036 tree upper_op = TREE_OPERAND (match_expr, 1);
24038 /* When the comparison is exclusive, the integer constant is not
24039 the discriminant range bound we are looking for: we will have
24040 to increment or decrement it. */
24041 if (TREE_CODE (lower_op) == GE_EXPR)
24042 lower_cst_included = true;
24043 else if (TREE_CODE (lower_op) == GT_EXPR)
24044 lower_cst_included = false;
24045 else
24046 goto abort;
24048 if (TREE_CODE (upper_op) == LE_EXPR)
24049 upper_cst_included = true;
24050 else if (TREE_CODE (upper_op) == LT_EXPR)
24051 upper_cst_included = false;
24052 else
24053 goto abort;
24055 /* Extract the discriminant from the first operand and check it
24056 is consistant with the same analysis in the second
24057 operand. */
24058 candidate_discr_1
24059 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24060 struct_type);
24061 candidate_discr_2
24062 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24063 struct_type);
24064 if (candidate_discr_1 == candidate_discr_2)
24065 candidate_discr = candidate_discr_1;
24066 else
24067 goto abort;
24069 /* Extract bounds from both. */
24070 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24071 lower_cst = TREE_OPERAND (lower_op, 1);
24072 upper_cst = TREE_OPERAND (upper_op, 1);
24074 if (!lower_cst_included)
24075 lower_cst
24076 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24077 build_int_cst (TREE_TYPE (lower_cst), 1));
24078 if (!upper_cst_included)
24079 upper_cst
24080 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24081 build_int_cst (TREE_TYPE (upper_cst), 1));
24083 if (!get_discr_value (lower_cst,
24084 &new_node->dw_discr_lower_bound)
24085 || !get_discr_value (upper_cst,
24086 &new_node->dw_discr_upper_bound))
24087 goto abort;
24089 new_node->dw_discr_range = true;
24092 else
24093 /* Unsupported sub-expression: we cannot determine the set of
24094 matching discriminant values. Abort everything. */
24095 goto abort;
24097 /* If the discriminant info is not consistant with what we saw so
24098 far, consider the analysis failed and abort everything. */
24099 if (candidate_discr == NULL_TREE
24100 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24101 goto abort;
24102 else
24103 *discr_decl = candidate_discr;
24105 if (new_node != NULL)
24107 new_node->dw_discr_next = discr_lists[i];
24108 discr_lists[i] = new_node;
24110 match_expr = next_round_match_expr;
24114 /* If we reach this point, we could match everything we were interested
24115 in. */
24116 return;
24118 abort:
24119 /* Clean all data structure and return no result. */
24120 free (*discr_lists_p);
24121 *discr_lists_p = NULL;
24122 *discr_decl = NULL_TREE;
24125 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24126 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24127 under CONTEXT_DIE.
24129 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24130 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24131 this type, which are record types, represent the available variants and each
24132 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24133 values are inferred from these attributes.
24135 In trees, the offsets for the fields inside these sub-records are relative
24136 to the variant part itself, whereas the corresponding DIEs should have
24137 offset attributes that are relative to the embedding record base address.
24138 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24139 must be an expression that computes the offset of the variant part to
24140 describe in DWARF. */
24142 static void
24143 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24144 dw_die_ref context_die)
24146 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24147 tree variant_part_offset = vlr_ctx->variant_part_offset;
24148 struct loc_descr_context ctx = {
24149 vlr_ctx->struct_type, /* context_type */
24150 NULL_TREE, /* base_decl */
24151 NULL, /* dpi */
24152 false, /* placeholder_arg */
24153 false /* placeholder_seen */
24156 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24157 NULL_TREE if there is no such field. */
24158 tree discr_decl = NULL_TREE;
24159 dw_discr_list_ref *discr_lists;
24160 unsigned discr_lists_length = 0;
24161 unsigned i;
24163 dw_die_ref dwarf_proc_die = NULL;
24164 dw_die_ref variant_part_die
24165 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24167 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24169 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24170 &discr_decl, &discr_lists, &discr_lists_length);
24172 if (discr_decl != NULL_TREE)
24174 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24176 if (discr_die)
24177 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24178 else
24179 /* We have no DIE for the discriminant, so just discard all
24180 discrimimant information in the output. */
24181 discr_decl = NULL_TREE;
24184 /* If the offset for this variant part is more complex than a constant,
24185 create a DWARF procedure for it so that we will not have to generate DWARF
24186 expressions for it for each member. */
24187 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24188 && (dwarf_version >= 3 || !dwarf_strict))
24190 const tree dwarf_proc_fndecl
24191 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24192 build_function_type (TREE_TYPE (variant_part_offset),
24193 NULL_TREE));
24194 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24195 const dw_loc_descr_ref dwarf_proc_body
24196 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24198 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24199 dwarf_proc_fndecl, context_die);
24200 if (dwarf_proc_die != NULL)
24201 variant_part_offset = dwarf_proc_call;
24204 /* Output DIEs for all variants. */
24205 i = 0;
24206 for (tree variant = TYPE_FIELDS (variant_part_type);
24207 variant != NULL_TREE;
24208 variant = DECL_CHAIN (variant), ++i)
24210 tree variant_type = TREE_TYPE (variant);
24211 dw_die_ref variant_die;
24213 /* All variants (i.e. members of a variant part) are supposed to be
24214 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24215 under these records. */
24216 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24218 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24219 equate_decl_number_to_die (variant, variant_die);
24221 /* Output discriminant values this variant matches, if any. */
24222 if (discr_decl == NULL || discr_lists[i] == NULL)
24223 /* In the case we have discriminant information at all, this is
24224 probably the default variant: as the standard says, don't
24225 output any discriminant value/list attribute. */
24227 else if (discr_lists[i]->dw_discr_next == NULL
24228 && !discr_lists[i]->dw_discr_range)
24229 /* If there is only one accepted value, don't bother outputting a
24230 list. */
24231 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24232 else
24233 add_discr_list (variant_die, discr_lists[i]);
24235 for (tree member = TYPE_FIELDS (variant_type);
24236 member != NULL_TREE;
24237 member = DECL_CHAIN (member))
24239 struct vlr_context vlr_sub_ctx = {
24240 vlr_ctx->struct_type, /* struct_type */
24241 NULL /* variant_part_offset */
24243 if (is_variant_part (member))
24245 /* All offsets for fields inside variant parts are relative to
24246 the top-level embedding RECORD_TYPE's base address. On the
24247 other hand, offsets in GCC's types are relative to the
24248 nested-most variant part. So we have to sum offsets each time
24249 we recurse. */
24251 vlr_sub_ctx.variant_part_offset
24252 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24253 variant_part_offset, byte_position (member));
24254 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24256 else
24258 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24259 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24264 free (discr_lists);
24267 /* Generate a DIE for a class member. */
24269 static void
24270 gen_member_die (tree type, dw_die_ref context_die)
24272 tree member;
24273 tree binfo = TYPE_BINFO (type);
24275 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24277 /* If this is not an incomplete type, output descriptions of each of its
24278 members. Note that as we output the DIEs necessary to represent the
24279 members of this record or union type, we will also be trying to output
24280 DIEs to represent the *types* of those members. However the `type'
24281 function (above) will specifically avoid generating type DIEs for member
24282 types *within* the list of member DIEs for this (containing) type except
24283 for those types (of members) which are explicitly marked as also being
24284 members of this (containing) type themselves. The g++ front- end can
24285 force any given type to be treated as a member of some other (containing)
24286 type by setting the TYPE_CONTEXT of the given (member) type to point to
24287 the TREE node representing the appropriate (containing) type. */
24289 /* First output info about the base classes. */
24290 if (binfo)
24292 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24293 int i;
24294 tree base;
24296 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24297 gen_inheritance_die (base,
24298 (accesses ? (*accesses)[i] : access_public_node),
24299 type,
24300 context_die);
24303 /* Now output info about the data members and type members. */
24304 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24306 struct vlr_context vlr_ctx = { type, NULL_TREE };
24307 bool static_inline_p
24308 = (TREE_STATIC (member)
24309 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24310 != -1));
24312 /* Ignore clones. */
24313 if (DECL_ABSTRACT_ORIGIN (member))
24314 continue;
24316 /* If we thought we were generating minimal debug info for TYPE
24317 and then changed our minds, some of the member declarations
24318 may have already been defined. Don't define them again, but
24319 do put them in the right order. */
24321 if (dw_die_ref child = lookup_decl_die (member))
24323 /* Handle inline static data members, which only have in-class
24324 declarations. */
24325 dw_die_ref ref = NULL;
24326 if (child->die_tag == DW_TAG_variable
24327 && child->die_parent == comp_unit_die ())
24329 ref = get_AT_ref (child, DW_AT_specification);
24330 /* For C++17 inline static data members followed by redundant
24331 out of class redeclaration, we might get here with
24332 child being the DIE created for the out of class
24333 redeclaration and with its DW_AT_specification being
24334 the DIE created for in-class definition. We want to
24335 reparent the latter, and don't want to create another
24336 DIE with DW_AT_specification in that case, because
24337 we already have one. */
24338 if (ref
24339 && static_inline_p
24340 && ref->die_tag == DW_TAG_variable
24341 && ref->die_parent == comp_unit_die ()
24342 && get_AT (ref, DW_AT_specification) == NULL)
24344 child = ref;
24345 ref = NULL;
24346 static_inline_p = false;
24350 if (child->die_tag == DW_TAG_variable
24351 && child->die_parent == comp_unit_die ()
24352 && ref == NULL)
24354 reparent_child (child, context_die);
24355 if (dwarf_version < 5)
24356 child->die_tag = DW_TAG_member;
24358 else
24359 splice_child_die (context_die, child);
24362 /* Do not generate standard DWARF for variant parts if we are generating
24363 the corresponding GNAT encodings: DIEs generated for both would
24364 conflict in our mappings. */
24365 else if (is_variant_part (member)
24366 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24368 vlr_ctx.variant_part_offset = byte_position (member);
24369 gen_variant_part (member, &vlr_ctx, context_die);
24371 else
24373 vlr_ctx.variant_part_offset = NULL_TREE;
24374 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24377 /* For C++ inline static data members emit immediately a DW_TAG_variable
24378 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24379 DW_AT_specification. */
24380 if (static_inline_p)
24382 int old_extern = DECL_EXTERNAL (member);
24383 DECL_EXTERNAL (member) = 0;
24384 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24385 DECL_EXTERNAL (member) = old_extern;
24390 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24391 is set, we pretend that the type was never defined, so we only get the
24392 member DIEs needed by later specification DIEs. */
24394 static void
24395 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24396 enum debug_info_usage usage)
24398 if (TREE_ASM_WRITTEN (type))
24400 /* Fill in the bound of variable-length fields in late dwarf if
24401 still incomplete. */
24402 if (!early_dwarf && variably_modified_type_p (type, NULL))
24403 for (tree member = TYPE_FIELDS (type);
24404 member;
24405 member = DECL_CHAIN (member))
24406 fill_variable_array_bounds (TREE_TYPE (member));
24407 return;
24410 dw_die_ref type_die = lookup_type_die (type);
24411 dw_die_ref scope_die = 0;
24412 int nested = 0;
24413 int complete = (TYPE_SIZE (type)
24414 && (! TYPE_STUB_DECL (type)
24415 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24416 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24417 complete = complete && should_emit_struct_debug (type, usage);
24419 if (type_die && ! complete)
24420 return;
24422 if (TYPE_CONTEXT (type) != NULL_TREE
24423 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24424 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24425 nested = 1;
24427 scope_die = scope_die_for (type, context_die);
24429 /* Generate child dies for template paramaters. */
24430 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24431 schedule_generic_params_dies_gen (type);
24433 if (! type_die || (nested && is_cu_die (scope_die)))
24434 /* First occurrence of type or toplevel definition of nested class. */
24436 dw_die_ref old_die = type_die;
24438 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24439 ? record_type_tag (type) : DW_TAG_union_type,
24440 scope_die, type);
24441 equate_type_number_to_die (type, type_die);
24442 if (old_die)
24443 add_AT_specification (type_die, old_die);
24444 else
24445 add_name_attribute (type_die, type_tag (type));
24447 else
24448 remove_AT (type_die, DW_AT_declaration);
24450 /* If this type has been completed, then give it a byte_size attribute and
24451 then give a list of members. */
24452 if (complete && !ns_decl)
24454 /* Prevent infinite recursion in cases where the type of some member of
24455 this type is expressed in terms of this type itself. */
24456 TREE_ASM_WRITTEN (type) = 1;
24457 add_byte_size_attribute (type_die, type);
24458 add_alignment_attribute (type_die, type);
24459 if (TYPE_STUB_DECL (type) != NULL_TREE)
24461 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24462 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24465 /* If the first reference to this type was as the return type of an
24466 inline function, then it may not have a parent. Fix this now. */
24467 if (type_die->die_parent == NULL)
24468 add_child_die (scope_die, type_die);
24470 push_decl_scope (type);
24471 gen_member_die (type, type_die);
24472 pop_decl_scope ();
24474 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24475 if (TYPE_ARTIFICIAL (type))
24476 add_AT_flag (type_die, DW_AT_artificial, 1);
24478 /* GNU extension: Record what type our vtable lives in. */
24479 if (TYPE_VFIELD (type))
24481 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24483 gen_type_die (vtype, context_die);
24484 add_AT_die_ref (type_die, DW_AT_containing_type,
24485 lookup_type_die (vtype));
24488 else
24490 add_AT_flag (type_die, DW_AT_declaration, 1);
24492 /* We don't need to do this for function-local types. */
24493 if (TYPE_STUB_DECL (type)
24494 && ! decl_function_context (TYPE_STUB_DECL (type)))
24495 vec_safe_push (incomplete_types, type);
24498 if (get_AT (type_die, DW_AT_name))
24499 add_pubtype (type, type_die);
24502 /* Generate a DIE for a subroutine _type_. */
24504 static void
24505 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24507 tree return_type = TREE_TYPE (type);
24508 dw_die_ref subr_die
24509 = new_die (DW_TAG_subroutine_type,
24510 scope_die_for (type, context_die), type);
24512 equate_type_number_to_die (type, subr_die);
24513 add_prototyped_attribute (subr_die, type);
24514 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24515 context_die);
24516 add_alignment_attribute (subr_die, type);
24517 gen_formal_types_die (type, subr_die);
24519 if (get_AT (subr_die, DW_AT_name))
24520 add_pubtype (type, subr_die);
24521 if ((dwarf_version >= 5 || !dwarf_strict)
24522 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24523 add_AT_flag (subr_die, DW_AT_reference, 1);
24524 if ((dwarf_version >= 5 || !dwarf_strict)
24525 && lang_hooks.types.type_dwarf_attribute (type,
24526 DW_AT_rvalue_reference) != -1)
24527 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24530 /* Generate a DIE for a type definition. */
24532 static void
24533 gen_typedef_die (tree decl, dw_die_ref context_die)
24535 dw_die_ref type_die;
24536 tree type;
24538 if (TREE_ASM_WRITTEN (decl))
24540 if (DECL_ORIGINAL_TYPE (decl))
24541 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24542 return;
24545 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24546 checks in process_scope_var and modified_type_die), this should be called
24547 only for original types. */
24548 gcc_assert (decl_ultimate_origin (decl) == NULL
24549 || decl_ultimate_origin (decl) == decl);
24551 TREE_ASM_WRITTEN (decl) = 1;
24552 type_die = new_die (DW_TAG_typedef, context_die, decl);
24554 add_name_and_src_coords_attributes (type_die, decl);
24555 if (DECL_ORIGINAL_TYPE (decl))
24557 type = DECL_ORIGINAL_TYPE (decl);
24558 if (type == error_mark_node)
24559 return;
24561 gcc_assert (type != TREE_TYPE (decl));
24562 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24564 else
24566 type = TREE_TYPE (decl);
24567 if (type == error_mark_node)
24568 return;
24570 if (is_naming_typedef_decl (TYPE_NAME (type)))
24572 /* Here, we are in the case of decl being a typedef naming
24573 an anonymous type, e.g:
24574 typedef struct {...} foo;
24575 In that case TREE_TYPE (decl) is not a typedef variant
24576 type and TYPE_NAME of the anonymous type is set to the
24577 TYPE_DECL of the typedef. This construct is emitted by
24578 the C++ FE.
24580 TYPE is the anonymous struct named by the typedef
24581 DECL. As we need the DW_AT_type attribute of the
24582 DW_TAG_typedef to point to the DIE of TYPE, let's
24583 generate that DIE right away. add_type_attribute
24584 called below will then pick (via lookup_type_die) that
24585 anonymous struct DIE. */
24586 if (!TREE_ASM_WRITTEN (type))
24587 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24589 /* This is a GNU Extension. We are adding a
24590 DW_AT_linkage_name attribute to the DIE of the
24591 anonymous struct TYPE. The value of that attribute
24592 is the name of the typedef decl naming the anonymous
24593 struct. This greatly eases the work of consumers of
24594 this debug info. */
24595 add_linkage_name_raw (lookup_type_die (type), decl);
24599 add_type_attribute (type_die, type, decl_quals (decl), false,
24600 context_die);
24602 if (is_naming_typedef_decl (decl))
24603 /* We want that all subsequent calls to lookup_type_die with
24604 TYPE in argument yield the DW_TAG_typedef we have just
24605 created. */
24606 equate_type_number_to_die (type, type_die);
24608 add_alignment_attribute (type_die, TREE_TYPE (decl));
24610 add_accessibility_attribute (type_die, decl);
24612 if (DECL_ABSTRACT_P (decl))
24613 equate_decl_number_to_die (decl, type_die);
24615 if (get_AT (type_die, DW_AT_name))
24616 add_pubtype (decl, type_die);
24619 /* Generate a DIE for a struct, class, enum or union type. */
24621 static void
24622 gen_tagged_type_die (tree type,
24623 dw_die_ref context_die,
24624 enum debug_info_usage usage)
24626 int need_pop;
24628 if (type == NULL_TREE
24629 || !is_tagged_type (type))
24630 return;
24632 if (TREE_ASM_WRITTEN (type))
24633 need_pop = 0;
24634 /* If this is a nested type whose containing class hasn't been written
24635 out yet, writing it out will cover this one, too. This does not apply
24636 to instantiations of member class templates; they need to be added to
24637 the containing class as they are generated. FIXME: This hurts the
24638 idea of combining type decls from multiple TUs, since we can't predict
24639 what set of template instantiations we'll get. */
24640 else if (TYPE_CONTEXT (type)
24641 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24642 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24644 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24646 if (TREE_ASM_WRITTEN (type))
24647 return;
24649 /* If that failed, attach ourselves to the stub. */
24650 push_decl_scope (TYPE_CONTEXT (type));
24651 context_die = lookup_type_die (TYPE_CONTEXT (type));
24652 need_pop = 1;
24654 else if (TYPE_CONTEXT (type) != NULL_TREE
24655 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24657 /* If this type is local to a function that hasn't been written
24658 out yet, use a NULL context for now; it will be fixed up in
24659 decls_for_scope. */
24660 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24661 /* A declaration DIE doesn't count; nested types need to go in the
24662 specification. */
24663 if (context_die && is_declaration_die (context_die))
24664 context_die = NULL;
24665 need_pop = 0;
24667 else
24669 context_die = declare_in_namespace (type, context_die);
24670 need_pop = 0;
24673 if (TREE_CODE (type) == ENUMERAL_TYPE)
24675 /* This might have been written out by the call to
24676 declare_in_namespace. */
24677 if (!TREE_ASM_WRITTEN (type))
24678 gen_enumeration_type_die (type, context_die);
24680 else
24681 gen_struct_or_union_type_die (type, context_die, usage);
24683 if (need_pop)
24684 pop_decl_scope ();
24686 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24687 it up if it is ever completed. gen_*_type_die will set it for us
24688 when appropriate. */
24691 /* Generate a type description DIE. */
24693 static void
24694 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24695 enum debug_info_usage usage)
24697 struct array_descr_info info;
24699 if (type == NULL_TREE || type == error_mark_node)
24700 return;
24702 if (flag_checking && type)
24703 verify_type (type);
24705 if (TYPE_NAME (type) != NULL_TREE
24706 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24707 && is_redundant_typedef (TYPE_NAME (type))
24708 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24709 /* The DECL of this type is a typedef we don't want to emit debug
24710 info for but we want debug info for its underlying typedef.
24711 This can happen for e.g, the injected-class-name of a C++
24712 type. */
24713 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24715 /* If TYPE is a typedef type variant, let's generate debug info
24716 for the parent typedef which TYPE is a type of. */
24717 if (typedef_variant_p (type))
24719 if (TREE_ASM_WRITTEN (type))
24720 return;
24722 tree name = TYPE_NAME (type);
24723 tree origin = decl_ultimate_origin (name);
24724 if (origin != NULL && origin != name)
24726 gen_decl_die (origin, NULL, NULL, context_die);
24727 return;
24730 /* Prevent broken recursion; we can't hand off to the same type. */
24731 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24733 /* Give typedefs the right scope. */
24734 context_die = scope_die_for (type, context_die);
24736 TREE_ASM_WRITTEN (type) = 1;
24738 gen_decl_die (name, NULL, NULL, context_die);
24739 return;
24742 /* If type is an anonymous tagged type named by a typedef, let's
24743 generate debug info for the typedef. */
24744 if (is_naming_typedef_decl (TYPE_NAME (type)))
24746 /* Use the DIE of the containing namespace as the parent DIE of
24747 the type description DIE we want to generate. */
24748 if (DECL_CONTEXT (TYPE_NAME (type))
24749 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24750 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24752 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24753 return;
24756 if (lang_hooks.types.get_debug_type)
24758 tree debug_type = lang_hooks.types.get_debug_type (type);
24760 if (debug_type != NULL_TREE && debug_type != type)
24762 gen_type_die_with_usage (debug_type, context_die, usage);
24763 return;
24767 /* We are going to output a DIE to represent the unqualified version
24768 of this type (i.e. without any const or volatile qualifiers) so
24769 get the main variant (i.e. the unqualified version) of this type
24770 now. (Vectors and arrays are special because the debugging info is in the
24771 cloned type itself. Similarly function/method types can contain extra
24772 ref-qualification). */
24773 if (TREE_CODE (type) == FUNCTION_TYPE
24774 || TREE_CODE (type) == METHOD_TYPE)
24776 /* For function/method types, can't use type_main_variant here,
24777 because that can have different ref-qualifiers for C++,
24778 but try to canonicalize. */
24779 tree main = TYPE_MAIN_VARIANT (type);
24780 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24781 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24782 && check_base_type (t, main)
24783 && check_lang_type (t, type))
24785 type = t;
24786 break;
24789 else if (TREE_CODE (type) != VECTOR_TYPE
24790 && TREE_CODE (type) != ARRAY_TYPE)
24791 type = type_main_variant (type);
24793 /* If this is an array type with hidden descriptor, handle it first. */
24794 if (!TREE_ASM_WRITTEN (type)
24795 && lang_hooks.types.get_array_descr_info)
24797 memset (&info, 0, sizeof (info));
24798 if (lang_hooks.types.get_array_descr_info (type, &info))
24800 /* Fortran sometimes emits array types with no dimension. */
24801 gcc_assert (info.ndimensions >= 0
24802 && (info.ndimensions
24803 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24804 gen_descr_array_type_die (type, &info, context_die);
24805 TREE_ASM_WRITTEN (type) = 1;
24806 return;
24810 if (TREE_ASM_WRITTEN (type))
24812 /* Variable-length types may be incomplete even if
24813 TREE_ASM_WRITTEN. For such types, fall through to
24814 gen_array_type_die() and possibly fill in
24815 DW_AT_{upper,lower}_bound attributes. */
24816 if ((TREE_CODE (type) != ARRAY_TYPE
24817 && TREE_CODE (type) != RECORD_TYPE
24818 && TREE_CODE (type) != UNION_TYPE
24819 && TREE_CODE (type) != QUAL_UNION_TYPE)
24820 || !variably_modified_type_p (type, NULL))
24821 return;
24824 switch (TREE_CODE (type))
24826 case ERROR_MARK:
24827 break;
24829 case POINTER_TYPE:
24830 case REFERENCE_TYPE:
24831 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24832 ensures that the gen_type_die recursion will terminate even if the
24833 type is recursive. Recursive types are possible in Ada. */
24834 /* ??? We could perhaps do this for all types before the switch
24835 statement. */
24836 TREE_ASM_WRITTEN (type) = 1;
24838 /* For these types, all that is required is that we output a DIE (or a
24839 set of DIEs) to represent the "basis" type. */
24840 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24841 DINFO_USAGE_IND_USE);
24842 break;
24844 case OFFSET_TYPE:
24845 /* This code is used for C++ pointer-to-data-member types.
24846 Output a description of the relevant class type. */
24847 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24848 DINFO_USAGE_IND_USE);
24850 /* Output a description of the type of the object pointed to. */
24851 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24852 DINFO_USAGE_IND_USE);
24854 /* Now output a DIE to represent this pointer-to-data-member type
24855 itself. */
24856 gen_ptr_to_mbr_type_die (type, context_die);
24857 break;
24859 case FUNCTION_TYPE:
24860 /* Force out return type (in case it wasn't forced out already). */
24861 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24862 DINFO_USAGE_DIR_USE);
24863 gen_subroutine_type_die (type, context_die);
24864 break;
24866 case METHOD_TYPE:
24867 /* Force out return type (in case it wasn't forced out already). */
24868 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24869 DINFO_USAGE_DIR_USE);
24870 gen_subroutine_type_die (type, context_die);
24871 break;
24873 case ARRAY_TYPE:
24874 case VECTOR_TYPE:
24875 gen_array_type_die (type, context_die);
24876 break;
24878 case ENUMERAL_TYPE:
24879 case RECORD_TYPE:
24880 case UNION_TYPE:
24881 case QUAL_UNION_TYPE:
24882 gen_tagged_type_die (type, context_die, usage);
24883 return;
24885 case VOID_TYPE:
24886 case INTEGER_TYPE:
24887 case REAL_TYPE:
24888 case FIXED_POINT_TYPE:
24889 case COMPLEX_TYPE:
24890 case BOOLEAN_TYPE:
24891 case POINTER_BOUNDS_TYPE:
24892 /* No DIEs needed for fundamental types. */
24893 break;
24895 case NULLPTR_TYPE:
24896 case LANG_TYPE:
24897 /* Just use DW_TAG_unspecified_type. */
24899 dw_die_ref type_die = lookup_type_die (type);
24900 if (type_die == NULL)
24902 tree name = TYPE_IDENTIFIER (type);
24903 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24904 type);
24905 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24906 equate_type_number_to_die (type, type_die);
24909 break;
24911 default:
24912 if (is_cxx_auto (type))
24914 tree name = TYPE_IDENTIFIER (type);
24915 dw_die_ref *die = (name == get_identifier ("auto")
24916 ? &auto_die : &decltype_auto_die);
24917 if (!*die)
24919 *die = new_die (DW_TAG_unspecified_type,
24920 comp_unit_die (), NULL_TREE);
24921 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24923 equate_type_number_to_die (type, *die);
24924 break;
24926 gcc_unreachable ();
24929 TREE_ASM_WRITTEN (type) = 1;
24932 static void
24933 gen_type_die (tree type, dw_die_ref context_die)
24935 if (type != error_mark_node)
24937 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24938 if (flag_checking)
24940 dw_die_ref die = lookup_type_die (type);
24941 if (die)
24942 check_die (die);
24947 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24948 things which are local to the given block. */
24950 static void
24951 gen_block_die (tree stmt, dw_die_ref context_die)
24953 int must_output_die = 0;
24954 bool inlined_func;
24956 /* Ignore blocks that are NULL. */
24957 if (stmt == NULL_TREE)
24958 return;
24960 inlined_func = inlined_function_outer_scope_p (stmt);
24962 /* If the block is one fragment of a non-contiguous block, do not
24963 process the variables, since they will have been done by the
24964 origin block. Do process subblocks. */
24965 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24967 tree sub;
24969 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24970 gen_block_die (sub, context_die);
24972 return;
24975 /* Determine if we need to output any Dwarf DIEs at all to represent this
24976 block. */
24977 if (inlined_func)
24978 /* The outer scopes for inlinings *must* always be represented. We
24979 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24980 must_output_die = 1;
24981 else
24983 /* Determine if this block directly contains any "significant"
24984 local declarations which we will need to output DIEs for. */
24985 if (debug_info_level > DINFO_LEVEL_TERSE)
24986 /* We are not in terse mode so *any* local declaration counts
24987 as being a "significant" one. */
24988 must_output_die = ((BLOCK_VARS (stmt) != NULL
24989 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24990 && (TREE_USED (stmt)
24991 || TREE_ASM_WRITTEN (stmt)
24992 || BLOCK_ABSTRACT (stmt)));
24993 else if ((TREE_USED (stmt)
24994 || TREE_ASM_WRITTEN (stmt)
24995 || BLOCK_ABSTRACT (stmt))
24996 && !dwarf2out_ignore_block (stmt))
24997 must_output_die = 1;
25000 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25001 DIE for any block which contains no significant local declarations at
25002 all. Rather, in such cases we just call `decls_for_scope' so that any
25003 needed Dwarf info for any sub-blocks will get properly generated. Note
25004 that in terse mode, our definition of what constitutes a "significant"
25005 local declaration gets restricted to include only inlined function
25006 instances and local (nested) function definitions. */
25007 if (must_output_die)
25009 if (inlined_func)
25011 /* If STMT block is abstract, that means we have been called
25012 indirectly from dwarf2out_abstract_function.
25013 That function rightfully marks the descendent blocks (of
25014 the abstract function it is dealing with) as being abstract,
25015 precisely to prevent us from emitting any
25016 DW_TAG_inlined_subroutine DIE as a descendent
25017 of an abstract function instance. So in that case, we should
25018 not call gen_inlined_subroutine_die.
25020 Later though, when cgraph asks dwarf2out to emit info
25021 for the concrete instance of the function decl into which
25022 the concrete instance of STMT got inlined, the later will lead
25023 to the generation of a DW_TAG_inlined_subroutine DIE. */
25024 if (! BLOCK_ABSTRACT (stmt))
25025 gen_inlined_subroutine_die (stmt, context_die);
25027 else
25028 gen_lexical_block_die (stmt, context_die);
25030 else
25031 decls_for_scope (stmt, context_die);
25034 /* Process variable DECL (or variable with origin ORIGIN) within
25035 block STMT and add it to CONTEXT_DIE. */
25036 static void
25037 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25039 dw_die_ref die;
25040 tree decl_or_origin = decl ? decl : origin;
25042 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25043 die = lookup_decl_die (decl_or_origin);
25044 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25046 if (TYPE_DECL_IS_STUB (decl_or_origin))
25047 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25048 else
25049 die = lookup_decl_die (decl_or_origin);
25050 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25051 if (! die && ! early_dwarf)
25052 return;
25054 else
25055 die = NULL;
25057 /* Avoid creating DIEs for local typedefs and concrete static variables that
25058 will only be pruned later. */
25059 if ((origin || decl_ultimate_origin (decl))
25060 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25061 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25063 origin = decl_ultimate_origin (decl_or_origin);
25064 if (decl && VAR_P (decl) && die != NULL)
25066 die = lookup_decl_die (origin);
25067 if (die != NULL)
25068 equate_decl_number_to_die (decl, die);
25070 return;
25073 if (die != NULL && die->die_parent == NULL)
25074 add_child_die (context_die, die);
25075 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25077 if (early_dwarf)
25078 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25079 stmt, context_die);
25081 else
25083 if (decl && DECL_P (decl))
25085 die = lookup_decl_die (decl);
25087 /* Early created DIEs do not have a parent as the decls refer
25088 to the function as DECL_CONTEXT rather than the BLOCK. */
25089 if (die && die->die_parent == NULL)
25091 gcc_assert (in_lto_p);
25092 add_child_die (context_die, die);
25096 gen_decl_die (decl, origin, NULL, context_die);
25100 /* Generate all of the decls declared within a given scope and (recursively)
25101 all of its sub-blocks. */
25103 static void
25104 decls_for_scope (tree stmt, dw_die_ref context_die)
25106 tree decl;
25107 unsigned int i;
25108 tree subblocks;
25110 /* Ignore NULL blocks. */
25111 if (stmt == NULL_TREE)
25112 return;
25114 /* Output the DIEs to represent all of the data objects and typedefs
25115 declared directly within this block but not within any nested
25116 sub-blocks. Also, nested function and tag DIEs have been
25117 generated with a parent of NULL; fix that up now. We don't
25118 have to do this if we're at -g1. */
25119 if (debug_info_level > DINFO_LEVEL_TERSE)
25121 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25122 process_scope_var (stmt, decl, NULL_TREE, context_die);
25123 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25124 origin - avoid doing this twice as we have no good way to see
25125 if we've done it once already. */
25126 if (! early_dwarf)
25127 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25129 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25130 if (decl == current_function_decl)
25131 /* Ignore declarations of the current function, while they
25132 are declarations, gen_subprogram_die would treat them
25133 as definitions again, because they are equal to
25134 current_function_decl and endlessly recurse. */;
25135 else if (TREE_CODE (decl) == FUNCTION_DECL)
25136 process_scope_var (stmt, decl, NULL_TREE, context_die);
25137 else
25138 process_scope_var (stmt, NULL_TREE, decl, context_die);
25142 /* Even if we're at -g1, we need to process the subblocks in order to get
25143 inlined call information. */
25145 /* Output the DIEs to represent all sub-blocks (and the items declared
25146 therein) of this block. */
25147 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25148 subblocks != NULL;
25149 subblocks = BLOCK_CHAIN (subblocks))
25150 gen_block_die (subblocks, context_die);
25153 /* Is this a typedef we can avoid emitting? */
25155 bool
25156 is_redundant_typedef (const_tree decl)
25158 if (TYPE_DECL_IS_STUB (decl))
25159 return true;
25161 if (DECL_ARTIFICIAL (decl)
25162 && DECL_CONTEXT (decl)
25163 && is_tagged_type (DECL_CONTEXT (decl))
25164 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25165 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25166 /* Also ignore the artificial member typedef for the class name. */
25167 return true;
25169 return false;
25172 /* Return TRUE if TYPE is a typedef that names a type for linkage
25173 purposes. This kind of typedefs is produced by the C++ FE for
25174 constructs like:
25176 typedef struct {...} foo;
25178 In that case, there is no typedef variant type produced for foo.
25179 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25180 struct type. */
25182 static bool
25183 is_naming_typedef_decl (const_tree decl)
25185 if (decl == NULL_TREE
25186 || TREE_CODE (decl) != TYPE_DECL
25187 || DECL_NAMELESS (decl)
25188 || !is_tagged_type (TREE_TYPE (decl))
25189 || DECL_IS_BUILTIN (decl)
25190 || is_redundant_typedef (decl)
25191 /* It looks like Ada produces TYPE_DECLs that are very similar
25192 to C++ naming typedefs but that have different
25193 semantics. Let's be specific to c++ for now. */
25194 || !is_cxx (decl))
25195 return FALSE;
25197 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25198 && TYPE_NAME (TREE_TYPE (decl)) == decl
25199 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25200 != TYPE_NAME (TREE_TYPE (decl))));
25203 /* Looks up the DIE for a context. */
25205 static inline dw_die_ref
25206 lookup_context_die (tree context)
25208 if (context)
25210 /* Find die that represents this context. */
25211 if (TYPE_P (context))
25213 context = TYPE_MAIN_VARIANT (context);
25214 dw_die_ref ctx = lookup_type_die (context);
25215 if (!ctx)
25216 return NULL;
25217 return strip_naming_typedef (context, ctx);
25219 else
25220 return lookup_decl_die (context);
25222 return comp_unit_die ();
25225 /* Returns the DIE for a context. */
25227 static inline dw_die_ref
25228 get_context_die (tree context)
25230 if (context)
25232 /* Find die that represents this context. */
25233 if (TYPE_P (context))
25235 context = TYPE_MAIN_VARIANT (context);
25236 return strip_naming_typedef (context, force_type_die (context));
25238 else
25239 return force_decl_die (context);
25241 return comp_unit_die ();
25244 /* Returns the DIE for decl. A DIE will always be returned. */
25246 static dw_die_ref
25247 force_decl_die (tree decl)
25249 dw_die_ref decl_die;
25250 unsigned saved_external_flag;
25251 tree save_fn = NULL_TREE;
25252 decl_die = lookup_decl_die (decl);
25253 if (!decl_die)
25255 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25257 decl_die = lookup_decl_die (decl);
25258 if (decl_die)
25259 return decl_die;
25261 switch (TREE_CODE (decl))
25263 case FUNCTION_DECL:
25264 /* Clear current_function_decl, so that gen_subprogram_die thinks
25265 that this is a declaration. At this point, we just want to force
25266 declaration die. */
25267 save_fn = current_function_decl;
25268 current_function_decl = NULL_TREE;
25269 gen_subprogram_die (decl, context_die);
25270 current_function_decl = save_fn;
25271 break;
25273 case VAR_DECL:
25274 /* Set external flag to force declaration die. Restore it after
25275 gen_decl_die() call. */
25276 saved_external_flag = DECL_EXTERNAL (decl);
25277 DECL_EXTERNAL (decl) = 1;
25278 gen_decl_die (decl, NULL, NULL, context_die);
25279 DECL_EXTERNAL (decl) = saved_external_flag;
25280 break;
25282 case NAMESPACE_DECL:
25283 if (dwarf_version >= 3 || !dwarf_strict)
25284 dwarf2out_decl (decl);
25285 else
25286 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25287 decl_die = comp_unit_die ();
25288 break;
25290 case TRANSLATION_UNIT_DECL:
25291 decl_die = comp_unit_die ();
25292 break;
25294 default:
25295 gcc_unreachable ();
25298 /* We should be able to find the DIE now. */
25299 if (!decl_die)
25300 decl_die = lookup_decl_die (decl);
25301 gcc_assert (decl_die);
25304 return decl_die;
25307 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25308 always returned. */
25310 static dw_die_ref
25311 force_type_die (tree type)
25313 dw_die_ref type_die;
25315 type_die = lookup_type_die (type);
25316 if (!type_die)
25318 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25320 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25321 false, context_die);
25322 gcc_assert (type_die);
25324 return type_die;
25327 /* Force out any required namespaces to be able to output DECL,
25328 and return the new context_die for it, if it's changed. */
25330 static dw_die_ref
25331 setup_namespace_context (tree thing, dw_die_ref context_die)
25333 tree context = (DECL_P (thing)
25334 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25335 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25336 /* Force out the namespace. */
25337 context_die = force_decl_die (context);
25339 return context_die;
25342 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25343 type) within its namespace, if appropriate.
25345 For compatibility with older debuggers, namespace DIEs only contain
25346 declarations; all definitions are emitted at CU scope, with
25347 DW_AT_specification pointing to the declaration (like with class
25348 members). */
25350 static dw_die_ref
25351 declare_in_namespace (tree thing, dw_die_ref context_die)
25353 dw_die_ref ns_context;
25355 if (debug_info_level <= DINFO_LEVEL_TERSE)
25356 return context_die;
25358 /* External declarations in the local scope only need to be emitted
25359 once, not once in the namespace and once in the scope.
25361 This avoids declaring the `extern' below in the
25362 namespace DIE as well as in the innermost scope:
25364 namespace S
25366 int i=5;
25367 int foo()
25369 int i=8;
25370 extern int i;
25371 return i;
25375 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25376 return context_die;
25378 /* If this decl is from an inlined function, then don't try to emit it in its
25379 namespace, as we will get confused. It would have already been emitted
25380 when the abstract instance of the inline function was emitted anyways. */
25381 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25382 return context_die;
25384 ns_context = setup_namespace_context (thing, context_die);
25386 if (ns_context != context_die)
25388 if (is_fortran ())
25389 return ns_context;
25390 if (DECL_P (thing))
25391 gen_decl_die (thing, NULL, NULL, ns_context);
25392 else
25393 gen_type_die (thing, ns_context);
25395 return context_die;
25398 /* Generate a DIE for a namespace or namespace alias. */
25400 static void
25401 gen_namespace_die (tree decl, dw_die_ref context_die)
25403 dw_die_ref namespace_die;
25405 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25406 they are an alias of. */
25407 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25409 /* Output a real namespace or module. */
25410 context_die = setup_namespace_context (decl, comp_unit_die ());
25411 namespace_die = new_die (is_fortran ()
25412 ? DW_TAG_module : DW_TAG_namespace,
25413 context_die, decl);
25414 /* For Fortran modules defined in different CU don't add src coords. */
25415 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25417 const char *name = dwarf2_name (decl, 0);
25418 if (name)
25419 add_name_attribute (namespace_die, name);
25421 else
25422 add_name_and_src_coords_attributes (namespace_die, decl);
25423 if (DECL_EXTERNAL (decl))
25424 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25425 equate_decl_number_to_die (decl, namespace_die);
25427 else
25429 /* Output a namespace alias. */
25431 /* Force out the namespace we are an alias of, if necessary. */
25432 dw_die_ref origin_die
25433 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25435 if (DECL_FILE_SCOPE_P (decl)
25436 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25437 context_die = setup_namespace_context (decl, comp_unit_die ());
25438 /* Now create the namespace alias DIE. */
25439 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25440 add_name_and_src_coords_attributes (namespace_die, decl);
25441 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25442 equate_decl_number_to_die (decl, namespace_die);
25444 if ((dwarf_version >= 5 || !dwarf_strict)
25445 && lang_hooks.decls.decl_dwarf_attribute (decl,
25446 DW_AT_export_symbols) == 1)
25447 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25449 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25450 if (want_pubnames ())
25451 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25454 /* Generate Dwarf debug information for a decl described by DECL.
25455 The return value is currently only meaningful for PARM_DECLs,
25456 for all other decls it returns NULL.
25458 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25459 It can be NULL otherwise. */
25461 static dw_die_ref
25462 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25463 dw_die_ref context_die)
25465 tree decl_or_origin = decl ? decl : origin;
25466 tree class_origin = NULL, ultimate_origin;
25468 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25469 return NULL;
25471 /* Ignore pointer bounds decls. */
25472 if (DECL_P (decl_or_origin)
25473 && TREE_TYPE (decl_or_origin)
25474 && POINTER_BOUNDS_P (decl_or_origin))
25475 return NULL;
25477 switch (TREE_CODE (decl_or_origin))
25479 case ERROR_MARK:
25480 break;
25482 case CONST_DECL:
25483 if (!is_fortran () && !is_ada ())
25485 /* The individual enumerators of an enum type get output when we output
25486 the Dwarf representation of the relevant enum type itself. */
25487 break;
25490 /* Emit its type. */
25491 gen_type_die (TREE_TYPE (decl), context_die);
25493 /* And its containing namespace. */
25494 context_die = declare_in_namespace (decl, context_die);
25496 gen_const_die (decl, context_die);
25497 break;
25499 case FUNCTION_DECL:
25500 #if 0
25501 /* FIXME */
25502 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25503 on local redeclarations of global functions. That seems broken. */
25504 if (current_function_decl != decl)
25505 /* This is only a declaration. */;
25506 #endif
25508 /* We should have abstract copies already and should not generate
25509 stray type DIEs in late LTO dumping. */
25510 if (! early_dwarf)
25513 /* If we're emitting a clone, emit info for the abstract instance. */
25514 else if (origin || DECL_ORIGIN (decl) != decl)
25515 dwarf2out_abstract_function (origin
25516 ? DECL_ORIGIN (origin)
25517 : DECL_ABSTRACT_ORIGIN (decl));
25519 /* If we're emitting a possibly inlined function emit it as
25520 abstract instance. */
25521 else if (cgraph_function_possibly_inlined_p (decl)
25522 && ! DECL_ABSTRACT_P (decl)
25523 && ! class_or_namespace_scope_p (context_die)
25524 /* dwarf2out_abstract_function won't emit a die if this is just
25525 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25526 that case, because that works only if we have a die. */
25527 && DECL_INITIAL (decl) != NULL_TREE)
25528 dwarf2out_abstract_function (decl);
25530 /* Otherwise we're emitting the primary DIE for this decl. */
25531 else if (debug_info_level > DINFO_LEVEL_TERSE)
25533 /* Before we describe the FUNCTION_DECL itself, make sure that we
25534 have its containing type. */
25535 if (!origin)
25536 origin = decl_class_context (decl);
25537 if (origin != NULL_TREE)
25538 gen_type_die (origin, context_die);
25540 /* And its return type. */
25541 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25543 /* And its virtual context. */
25544 if (DECL_VINDEX (decl) != NULL_TREE)
25545 gen_type_die (DECL_CONTEXT (decl), context_die);
25547 /* Make sure we have a member DIE for decl. */
25548 if (origin != NULL_TREE)
25549 gen_type_die_for_member (origin, decl, context_die);
25551 /* And its containing namespace. */
25552 context_die = declare_in_namespace (decl, context_die);
25555 /* Now output a DIE to represent the function itself. */
25556 if (decl)
25557 gen_subprogram_die (decl, context_die);
25558 break;
25560 case TYPE_DECL:
25561 /* If we are in terse mode, don't generate any DIEs to represent any
25562 actual typedefs. */
25563 if (debug_info_level <= DINFO_LEVEL_TERSE)
25564 break;
25566 /* In the special case of a TYPE_DECL node representing the declaration
25567 of some type tag, if the given TYPE_DECL is marked as having been
25568 instantiated from some other (original) TYPE_DECL node (e.g. one which
25569 was generated within the original definition of an inline function) we
25570 used to generate a special (abbreviated) DW_TAG_structure_type,
25571 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25572 should be actually referencing those DIEs, as variable DIEs with that
25573 type would be emitted already in the abstract origin, so it was always
25574 removed during unused type prunning. Don't add anything in this
25575 case. */
25576 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25577 break;
25579 if (is_redundant_typedef (decl))
25580 gen_type_die (TREE_TYPE (decl), context_die);
25581 else
25582 /* Output a DIE to represent the typedef itself. */
25583 gen_typedef_die (decl, context_die);
25584 break;
25586 case LABEL_DECL:
25587 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25588 gen_label_die (decl, context_die);
25589 break;
25591 case VAR_DECL:
25592 case RESULT_DECL:
25593 /* If we are in terse mode, don't generate any DIEs to represent any
25594 variable declarations or definitions. */
25595 if (debug_info_level <= DINFO_LEVEL_TERSE)
25596 break;
25598 /* Avoid generating stray type DIEs during late dwarf dumping.
25599 All types have been dumped early. */
25600 if (early_dwarf
25601 /* ??? But in LTRANS we cannot annotate early created variably
25602 modified type DIEs without copying them and adjusting all
25603 references to them. Dump them again as happens for inlining
25604 which copies both the decl and the types. */
25605 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25606 in VLA bound information for example. */
25607 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25608 current_function_decl)))
25610 /* Output any DIEs that are needed to specify the type of this data
25611 object. */
25612 if (decl_by_reference_p (decl_or_origin))
25613 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25614 else
25615 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25618 if (early_dwarf)
25620 /* And its containing type. */
25621 class_origin = decl_class_context (decl_or_origin);
25622 if (class_origin != NULL_TREE)
25623 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25625 /* And its containing namespace. */
25626 context_die = declare_in_namespace (decl_or_origin, context_die);
25629 /* Now output the DIE to represent the data object itself. This gets
25630 complicated because of the possibility that the VAR_DECL really
25631 represents an inlined instance of a formal parameter for an inline
25632 function. */
25633 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25634 if (ultimate_origin != NULL_TREE
25635 && TREE_CODE (ultimate_origin) == PARM_DECL)
25636 gen_formal_parameter_die (decl, origin,
25637 true /* Emit name attribute. */,
25638 context_die);
25639 else
25640 gen_variable_die (decl, origin, context_die);
25641 break;
25643 case FIELD_DECL:
25644 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25645 /* Ignore the nameless fields that are used to skip bits but handle C++
25646 anonymous unions and structs. */
25647 if (DECL_NAME (decl) != NULL_TREE
25648 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25649 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25651 gen_type_die (member_declared_type (decl), context_die);
25652 gen_field_die (decl, ctx, context_die);
25654 break;
25656 case PARM_DECL:
25657 /* Avoid generating stray type DIEs during late dwarf dumping.
25658 All types have been dumped early. */
25659 if (early_dwarf
25660 /* ??? But in LTRANS we cannot annotate early created variably
25661 modified type DIEs without copying them and adjusting all
25662 references to them. Dump them again as happens for inlining
25663 which copies both the decl and the types. */
25664 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25665 in VLA bound information for example. */
25666 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25667 current_function_decl)))
25669 if (DECL_BY_REFERENCE (decl_or_origin))
25670 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25671 else
25672 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25674 return gen_formal_parameter_die (decl, origin,
25675 true /* Emit name attribute. */,
25676 context_die);
25678 case NAMESPACE_DECL:
25679 if (dwarf_version >= 3 || !dwarf_strict)
25680 gen_namespace_die (decl, context_die);
25681 break;
25683 case IMPORTED_DECL:
25684 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25685 DECL_CONTEXT (decl), context_die);
25686 break;
25688 case NAMELIST_DECL:
25689 gen_namelist_decl (DECL_NAME (decl), context_die,
25690 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25691 break;
25693 default:
25694 /* Probably some frontend-internal decl. Assume we don't care. */
25695 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25696 break;
25699 return NULL;
25702 /* Output initial debug information for global DECL. Called at the
25703 end of the parsing process.
25705 This is the initial debug generation process. As such, the DIEs
25706 generated may be incomplete. A later debug generation pass
25707 (dwarf2out_late_global_decl) will augment the information generated
25708 in this pass (e.g., with complete location info). */
25710 static void
25711 dwarf2out_early_global_decl (tree decl)
25713 set_early_dwarf s;
25715 /* gen_decl_die() will set DECL_ABSTRACT because
25716 cgraph_function_possibly_inlined_p() returns true. This is in
25717 turn will cause DW_AT_inline attributes to be set.
25719 This happens because at early dwarf generation, there is no
25720 cgraph information, causing cgraph_function_possibly_inlined_p()
25721 to return true. Trick cgraph_function_possibly_inlined_p()
25722 while we generate dwarf early. */
25723 bool save = symtab->global_info_ready;
25724 symtab->global_info_ready = true;
25726 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25727 other DECLs and they can point to template types or other things
25728 that dwarf2out can't handle when done via dwarf2out_decl. */
25729 if (TREE_CODE (decl) != TYPE_DECL
25730 && TREE_CODE (decl) != PARM_DECL)
25732 tree save_fndecl = current_function_decl;
25733 if (TREE_CODE (decl) == FUNCTION_DECL)
25735 /* For nested functions, make sure we have DIEs for the parents first
25736 so that all nested DIEs are generated at the proper scope in the
25737 first shot. */
25738 tree context = decl_function_context (decl);
25739 if (context != NULL && lookup_decl_die (context) == NULL)
25741 current_function_decl = context;
25742 dwarf2out_decl (context);
25745 /* Emit an abstract origin of a function first. This happens
25746 with C++ constructor clones for example and makes
25747 dwarf2out_abstract_function happy which requires the early
25748 DIE of the abstract instance to be present. */
25749 if (DECL_ABSTRACT_ORIGIN (decl))
25751 current_function_decl = DECL_ABSTRACT_ORIGIN (decl);
25752 dwarf2out_decl (DECL_ABSTRACT_ORIGIN (decl));
25755 current_function_decl = decl;
25757 dwarf2out_decl (decl);
25758 if (TREE_CODE (decl) == FUNCTION_DECL)
25759 current_function_decl = save_fndecl;
25761 symtab->global_info_ready = save;
25764 /* Output debug information for global decl DECL. Called from
25765 toplev.c after compilation proper has finished. */
25767 static void
25768 dwarf2out_late_global_decl (tree decl)
25770 /* Fill-in any location information we were unable to determine
25771 on the first pass. */
25772 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25774 dw_die_ref die = lookup_decl_die (decl);
25776 /* We may have to generate early debug late for LTO in case debug
25777 was not enabled at compile-time or the target doesn't support
25778 the LTO early debug scheme. */
25779 if (! die && in_lto_p)
25781 dwarf2out_decl (decl);
25782 die = lookup_decl_die (decl);
25785 if (die)
25787 /* We get called via the symtab code invoking late_global_decl
25788 for symbols that are optimized out. Do not add locations
25789 for those, except if they have a DECL_VALUE_EXPR, in which case
25790 they are relevant for debuggers. */
25791 varpool_node *node = varpool_node::get (decl);
25792 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25793 tree_add_const_value_attribute_for_decl (die, decl);
25794 else
25795 add_location_or_const_value_attribute (die, decl, false);
25800 /* Output debug information for type decl DECL. Called from toplev.c
25801 and from language front ends (to record built-in types). */
25802 static void
25803 dwarf2out_type_decl (tree decl, int local)
25805 if (!local)
25807 set_early_dwarf s;
25808 dwarf2out_decl (decl);
25812 /* Output debug information for imported module or decl DECL.
25813 NAME is non-NULL name in the lexical block if the decl has been renamed.
25814 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25815 that DECL belongs to.
25816 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25817 static void
25818 dwarf2out_imported_module_or_decl_1 (tree decl,
25819 tree name,
25820 tree lexical_block,
25821 dw_die_ref lexical_block_die)
25823 expanded_location xloc;
25824 dw_die_ref imported_die = NULL;
25825 dw_die_ref at_import_die;
25827 if (TREE_CODE (decl) == IMPORTED_DECL)
25829 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25830 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25831 gcc_assert (decl);
25833 else
25834 xloc = expand_location (input_location);
25836 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25838 at_import_die = force_type_die (TREE_TYPE (decl));
25839 /* For namespace N { typedef void T; } using N::T; base_type_die
25840 returns NULL, but DW_TAG_imported_declaration requires
25841 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25842 if (!at_import_die)
25844 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25845 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25846 at_import_die = lookup_type_die (TREE_TYPE (decl));
25847 gcc_assert (at_import_die);
25850 else
25852 at_import_die = lookup_decl_die (decl);
25853 if (!at_import_die)
25855 /* If we're trying to avoid duplicate debug info, we may not have
25856 emitted the member decl for this field. Emit it now. */
25857 if (TREE_CODE (decl) == FIELD_DECL)
25859 tree type = DECL_CONTEXT (decl);
25861 if (TYPE_CONTEXT (type)
25862 && TYPE_P (TYPE_CONTEXT (type))
25863 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25864 DINFO_USAGE_DIR_USE))
25865 return;
25866 gen_type_die_for_member (type, decl,
25867 get_context_die (TYPE_CONTEXT (type)));
25869 if (TREE_CODE (decl) == NAMELIST_DECL)
25870 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25871 get_context_die (DECL_CONTEXT (decl)),
25872 NULL_TREE);
25873 else
25874 at_import_die = force_decl_die (decl);
25878 if (TREE_CODE (decl) == NAMESPACE_DECL)
25880 if (dwarf_version >= 3 || !dwarf_strict)
25881 imported_die = new_die (DW_TAG_imported_module,
25882 lexical_block_die,
25883 lexical_block);
25884 else
25885 return;
25887 else
25888 imported_die = new_die (DW_TAG_imported_declaration,
25889 lexical_block_die,
25890 lexical_block);
25892 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25893 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25894 if (debug_column_info && xloc.column)
25895 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25896 if (name)
25897 add_AT_string (imported_die, DW_AT_name,
25898 IDENTIFIER_POINTER (name));
25899 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25902 /* Output debug information for imported module or decl DECL.
25903 NAME is non-NULL name in context if the decl has been renamed.
25904 CHILD is true if decl is one of the renamed decls as part of
25905 importing whole module.
25906 IMPLICIT is set if this hook is called for an implicit import
25907 such as inline namespace. */
25909 static void
25910 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25911 bool child, bool implicit)
25913 /* dw_die_ref at_import_die; */
25914 dw_die_ref scope_die;
25916 if (debug_info_level <= DINFO_LEVEL_TERSE)
25917 return;
25919 gcc_assert (decl);
25921 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25922 should be enough, for DWARF4 and older even if we emit as extension
25923 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25924 for the benefit of consumers unaware of DW_AT_export_symbols. */
25925 if (implicit
25926 && dwarf_version >= 5
25927 && lang_hooks.decls.decl_dwarf_attribute (decl,
25928 DW_AT_export_symbols) == 1)
25929 return;
25931 set_early_dwarf s;
25933 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25934 We need decl DIE for reference and scope die. First, get DIE for the decl
25935 itself. */
25937 /* Get the scope die for decl context. Use comp_unit_die for global module
25938 or decl. If die is not found for non globals, force new die. */
25939 if (context
25940 && TYPE_P (context)
25941 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25942 return;
25944 scope_die = get_context_die (context);
25946 if (child)
25948 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25949 there is nothing we can do, here. */
25950 if (dwarf_version < 3 && dwarf_strict)
25951 return;
25953 gcc_assert (scope_die->die_child);
25954 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25955 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25956 scope_die = scope_die->die_child;
25959 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25960 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25963 /* Output debug information for namelists. */
25965 static dw_die_ref
25966 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25968 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25969 tree value;
25970 unsigned i;
25972 if (debug_info_level <= DINFO_LEVEL_TERSE)
25973 return NULL;
25975 gcc_assert (scope_die != NULL);
25976 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25977 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25979 /* If there are no item_decls, we have a nondefining namelist, e.g.
25980 with USE association; hence, set DW_AT_declaration. */
25981 if (item_decls == NULL_TREE)
25983 add_AT_flag (nml_die, DW_AT_declaration, 1);
25984 return nml_die;
25987 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25989 nml_item_ref_die = lookup_decl_die (value);
25990 if (!nml_item_ref_die)
25991 nml_item_ref_die = force_decl_die (value);
25993 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25994 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25996 return nml_die;
26000 /* Write the debugging output for DECL and return the DIE. */
26002 static void
26003 dwarf2out_decl (tree decl)
26005 dw_die_ref context_die = comp_unit_die ();
26007 switch (TREE_CODE (decl))
26009 case ERROR_MARK:
26010 return;
26012 case FUNCTION_DECL:
26013 /* If we're a nested function, initially use a parent of NULL; if we're
26014 a plain function, this will be fixed up in decls_for_scope. If
26015 we're a method, it will be ignored, since we already have a DIE. */
26016 if (decl_function_context (decl)
26017 /* But if we're in terse mode, we don't care about scope. */
26018 && debug_info_level > DINFO_LEVEL_TERSE)
26019 context_die = NULL;
26020 break;
26022 case VAR_DECL:
26023 /* For local statics lookup proper context die. */
26024 if (local_function_static (decl))
26025 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26027 /* If we are in terse mode, don't generate any DIEs to represent any
26028 variable declarations or definitions. */
26029 if (debug_info_level <= DINFO_LEVEL_TERSE)
26030 return;
26031 break;
26033 case CONST_DECL:
26034 if (debug_info_level <= DINFO_LEVEL_TERSE)
26035 return;
26036 if (!is_fortran () && !is_ada ())
26037 return;
26038 if (TREE_STATIC (decl) && decl_function_context (decl))
26039 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26040 break;
26042 case NAMESPACE_DECL:
26043 case IMPORTED_DECL:
26044 if (debug_info_level <= DINFO_LEVEL_TERSE)
26045 return;
26046 if (lookup_decl_die (decl) != NULL)
26047 return;
26048 break;
26050 case TYPE_DECL:
26051 /* Don't emit stubs for types unless they are needed by other DIEs. */
26052 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26053 return;
26055 /* Don't bother trying to generate any DIEs to represent any of the
26056 normal built-in types for the language we are compiling. */
26057 if (DECL_IS_BUILTIN (decl))
26058 return;
26060 /* If we are in terse mode, don't generate any DIEs for types. */
26061 if (debug_info_level <= DINFO_LEVEL_TERSE)
26062 return;
26064 /* If we're a function-scope tag, initially use a parent of NULL;
26065 this will be fixed up in decls_for_scope. */
26066 if (decl_function_context (decl))
26067 context_die = NULL;
26069 break;
26071 case NAMELIST_DECL:
26072 break;
26074 default:
26075 return;
26078 gen_decl_die (decl, NULL, NULL, context_die);
26080 if (flag_checking)
26082 dw_die_ref die = lookup_decl_die (decl);
26083 if (die)
26084 check_die (die);
26088 /* Write the debugging output for DECL. */
26090 static void
26091 dwarf2out_function_decl (tree decl)
26093 dwarf2out_decl (decl);
26094 call_arg_locations = NULL;
26095 call_arg_loc_last = NULL;
26096 call_site_count = -1;
26097 tail_call_site_count = -1;
26098 decl_loc_table->empty ();
26099 cached_dw_loc_list_table->empty ();
26102 /* Output a marker (i.e. a label) for the beginning of the generated code for
26103 a lexical block. */
26105 static void
26106 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26107 unsigned int blocknum)
26109 switch_to_section (current_function_section ());
26110 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26113 /* Output a marker (i.e. a label) for the end of the generated code for a
26114 lexical block. */
26116 static void
26117 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26119 switch_to_section (current_function_section ());
26120 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26123 /* Returns nonzero if it is appropriate not to emit any debugging
26124 information for BLOCK, because it doesn't contain any instructions.
26126 Don't allow this for blocks with nested functions or local classes
26127 as we would end up with orphans, and in the presence of scheduling
26128 we may end up calling them anyway. */
26130 static bool
26131 dwarf2out_ignore_block (const_tree block)
26133 tree decl;
26134 unsigned int i;
26136 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26137 if (TREE_CODE (decl) == FUNCTION_DECL
26138 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26139 return 0;
26140 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26142 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26143 if (TREE_CODE (decl) == FUNCTION_DECL
26144 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26145 return 0;
26148 return 1;
26151 /* Hash table routines for file_hash. */
26153 bool
26154 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26156 return filename_cmp (p1->filename, p2) == 0;
26159 hashval_t
26160 dwarf_file_hasher::hash (dwarf_file_data *p)
26162 return htab_hash_string (p->filename);
26165 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26166 dwarf2out.c) and return its "index". The index of each (known) filename is
26167 just a unique number which is associated with only that one filename. We
26168 need such numbers for the sake of generating labels (in the .debug_sfnames
26169 section) and references to those files numbers (in the .debug_srcinfo
26170 and .debug_macinfo sections). If the filename given as an argument is not
26171 found in our current list, add it to the list and assign it the next
26172 available unique index number. */
26174 static struct dwarf_file_data *
26175 lookup_filename (const char *file_name)
26177 struct dwarf_file_data * created;
26179 if (!file_name)
26180 return NULL;
26182 dwarf_file_data **slot
26183 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26184 INSERT);
26185 if (*slot)
26186 return *slot;
26188 created = ggc_alloc<dwarf_file_data> ();
26189 created->filename = file_name;
26190 created->emitted_number = 0;
26191 *slot = created;
26192 return created;
26195 /* If the assembler will construct the file table, then translate the compiler
26196 internal file table number into the assembler file table number, and emit
26197 a .file directive if we haven't already emitted one yet. The file table
26198 numbers are different because we prune debug info for unused variables and
26199 types, which may include filenames. */
26201 static int
26202 maybe_emit_file (struct dwarf_file_data * fd)
26204 if (! fd->emitted_number)
26206 if (last_emitted_file)
26207 fd->emitted_number = last_emitted_file->emitted_number + 1;
26208 else
26209 fd->emitted_number = 1;
26210 last_emitted_file = fd;
26212 if (DWARF2_ASM_LINE_DEBUG_INFO)
26214 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26215 output_quoted_string (asm_out_file,
26216 remap_debug_filename (fd->filename));
26217 fputc ('\n', asm_out_file);
26221 return fd->emitted_number;
26224 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26225 That generation should happen after function debug info has been
26226 generated. The value of the attribute is the constant value of ARG. */
26228 static void
26229 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26231 die_arg_entry entry;
26233 if (!die || !arg)
26234 return;
26236 gcc_assert (early_dwarf);
26238 if (!tmpl_value_parm_die_table)
26239 vec_alloc (tmpl_value_parm_die_table, 32);
26241 entry.die = die;
26242 entry.arg = arg;
26243 vec_safe_push (tmpl_value_parm_die_table, entry);
26246 /* Return TRUE if T is an instance of generic type, FALSE
26247 otherwise. */
26249 static bool
26250 generic_type_p (tree t)
26252 if (t == NULL_TREE || !TYPE_P (t))
26253 return false;
26254 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26257 /* Schedule the generation of the generic parameter dies for the
26258 instance of generic type T. The proper generation itself is later
26259 done by gen_scheduled_generic_parms_dies. */
26261 static void
26262 schedule_generic_params_dies_gen (tree t)
26264 if (!generic_type_p (t))
26265 return;
26267 gcc_assert (early_dwarf);
26269 if (!generic_type_instances)
26270 vec_alloc (generic_type_instances, 256);
26272 vec_safe_push (generic_type_instances, t);
26275 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26276 by append_entry_to_tmpl_value_parm_die_table. This function must
26277 be called after function DIEs have been generated. */
26279 static void
26280 gen_remaining_tmpl_value_param_die_attribute (void)
26282 if (tmpl_value_parm_die_table)
26284 unsigned i, j;
26285 die_arg_entry *e;
26287 /* We do this in two phases - first get the cases we can
26288 handle during early-finish, preserving those we cannot
26289 (containing symbolic constants where we don't yet know
26290 whether we are going to output the referenced symbols).
26291 For those we try again at late-finish. */
26292 j = 0;
26293 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26295 if (!tree_add_const_value_attribute (e->die, e->arg))
26297 dw_loc_descr_ref loc = NULL;
26298 if (! early_dwarf
26299 && (dwarf_version >= 5 || !dwarf_strict))
26300 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26301 if (loc)
26302 add_AT_loc (e->die, DW_AT_location, loc);
26303 else
26304 (*tmpl_value_parm_die_table)[j++] = *e;
26307 tmpl_value_parm_die_table->truncate (j);
26311 /* Generate generic parameters DIEs for instances of generic types
26312 that have been previously scheduled by
26313 schedule_generic_params_dies_gen. This function must be called
26314 after all the types of the CU have been laid out. */
26316 static void
26317 gen_scheduled_generic_parms_dies (void)
26319 unsigned i;
26320 tree t;
26322 if (!generic_type_instances)
26323 return;
26325 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26326 if (COMPLETE_TYPE_P (t))
26327 gen_generic_params_dies (t);
26329 generic_type_instances = NULL;
26333 /* Replace DW_AT_name for the decl with name. */
26335 static void
26336 dwarf2out_set_name (tree decl, tree name)
26338 dw_die_ref die;
26339 dw_attr_node *attr;
26340 const char *dname;
26342 die = TYPE_SYMTAB_DIE (decl);
26343 if (!die)
26344 return;
26346 dname = dwarf2_name (name, 0);
26347 if (!dname)
26348 return;
26350 attr = get_AT (die, DW_AT_name);
26351 if (attr)
26353 struct indirect_string_node *node;
26355 node = find_AT_string (dname);
26356 /* replace the string. */
26357 attr->dw_attr_val.v.val_str = node;
26360 else
26361 add_name_attribute (die, dname);
26364 /* True if before or during processing of the first function being emitted. */
26365 static bool in_first_function_p = true;
26366 /* True if loc_note during dwarf2out_var_location call might still be
26367 before first real instruction at address equal to .Ltext0. */
26368 static bool maybe_at_text_label_p = true;
26369 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26370 static unsigned int first_loclabel_num_not_at_text_label;
26372 /* Called by the final INSN scan whenever we see a var location. We
26373 use it to drop labels in the right places, and throw the location in
26374 our lookup table. */
26376 static void
26377 dwarf2out_var_location (rtx_insn *loc_note)
26379 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26380 struct var_loc_node *newloc;
26381 rtx_insn *next_real, *next_note;
26382 rtx_insn *call_insn = NULL;
26383 static const char *last_label;
26384 static const char *last_postcall_label;
26385 static bool last_in_cold_section_p;
26386 static rtx_insn *expected_next_loc_note;
26387 tree decl;
26388 bool var_loc_p;
26390 if (!NOTE_P (loc_note))
26392 if (CALL_P (loc_note))
26394 call_site_count++;
26395 if (SIBLING_CALL_P (loc_note))
26396 tail_call_site_count++;
26397 if (optimize == 0 && !flag_var_tracking)
26399 /* When the var-tracking pass is not running, there is no note
26400 for indirect calls whose target is compile-time known. In this
26401 case, process such calls specifically so that we generate call
26402 sites for them anyway. */
26403 rtx x = PATTERN (loc_note);
26404 if (GET_CODE (x) == PARALLEL)
26405 x = XVECEXP (x, 0, 0);
26406 if (GET_CODE (x) == SET)
26407 x = SET_SRC (x);
26408 if (GET_CODE (x) == CALL)
26409 x = XEXP (x, 0);
26410 if (!MEM_P (x)
26411 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26412 || !SYMBOL_REF_DECL (XEXP (x, 0))
26413 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26414 != FUNCTION_DECL))
26416 call_insn = loc_note;
26417 loc_note = NULL;
26418 var_loc_p = false;
26420 next_real = next_real_insn (call_insn);
26421 next_note = NULL;
26422 cached_next_real_insn = NULL;
26423 goto create_label;
26427 return;
26430 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26431 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26432 return;
26434 /* Optimize processing a large consecutive sequence of location
26435 notes so we don't spend too much time in next_real_insn. If the
26436 next insn is another location note, remember the next_real_insn
26437 calculation for next time. */
26438 next_real = cached_next_real_insn;
26439 if (next_real)
26441 if (expected_next_loc_note != loc_note)
26442 next_real = NULL;
26445 next_note = NEXT_INSN (loc_note);
26446 if (! next_note
26447 || next_note->deleted ()
26448 || ! NOTE_P (next_note)
26449 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26450 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26451 next_note = NULL;
26453 if (! next_real)
26454 next_real = next_real_insn (loc_note);
26456 if (next_note)
26458 expected_next_loc_note = next_note;
26459 cached_next_real_insn = next_real;
26461 else
26462 cached_next_real_insn = NULL;
26464 /* If there are no instructions which would be affected by this note,
26465 don't do anything. */
26466 if (var_loc_p
26467 && next_real == NULL_RTX
26468 && !NOTE_DURING_CALL_P (loc_note))
26469 return;
26471 create_label:
26473 if (next_real == NULL_RTX)
26474 next_real = get_last_insn ();
26476 /* If there were any real insns between note we processed last time
26477 and this note (or if it is the first note), clear
26478 last_{,postcall_}label so that they are not reused this time. */
26479 if (last_var_location_insn == NULL_RTX
26480 || last_var_location_insn != next_real
26481 || last_in_cold_section_p != in_cold_section_p)
26483 last_label = NULL;
26484 last_postcall_label = NULL;
26487 if (var_loc_p)
26489 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26490 newloc = add_var_loc_to_decl (decl, loc_note,
26491 NOTE_DURING_CALL_P (loc_note)
26492 ? last_postcall_label : last_label);
26493 if (newloc == NULL)
26494 return;
26496 else
26498 decl = NULL_TREE;
26499 newloc = NULL;
26502 /* If there were no real insns between note we processed last time
26503 and this note, use the label we emitted last time. Otherwise
26504 create a new label and emit it. */
26505 if (last_label == NULL)
26507 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26508 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26509 loclabel_num++;
26510 last_label = ggc_strdup (loclabel);
26511 /* See if loclabel might be equal to .Ltext0. If yes,
26512 bump first_loclabel_num_not_at_text_label. */
26513 if (!have_multiple_function_sections
26514 && in_first_function_p
26515 && maybe_at_text_label_p)
26517 static rtx_insn *last_start;
26518 rtx_insn *insn;
26519 for (insn = loc_note; insn; insn = previous_insn (insn))
26520 if (insn == last_start)
26521 break;
26522 else if (!NONDEBUG_INSN_P (insn))
26523 continue;
26524 else
26526 rtx body = PATTERN (insn);
26527 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26528 continue;
26529 /* Inline asm could occupy zero bytes. */
26530 else if (GET_CODE (body) == ASM_INPUT
26531 || asm_noperands (body) >= 0)
26532 continue;
26533 #ifdef HAVE_attr_length
26534 else if (get_attr_min_length (insn) == 0)
26535 continue;
26536 #endif
26537 else
26539 /* Assume insn has non-zero length. */
26540 maybe_at_text_label_p = false;
26541 break;
26544 if (maybe_at_text_label_p)
26546 last_start = loc_note;
26547 first_loclabel_num_not_at_text_label = loclabel_num;
26552 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26553 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26555 if (!var_loc_p)
26557 struct call_arg_loc_node *ca_loc
26558 = ggc_cleared_alloc<call_arg_loc_node> ();
26559 rtx_insn *prev
26560 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26562 ca_loc->call_arg_loc_note = loc_note;
26563 ca_loc->next = NULL;
26564 ca_loc->label = last_label;
26565 gcc_assert (prev
26566 && (CALL_P (prev)
26567 || (NONJUMP_INSN_P (prev)
26568 && GET_CODE (PATTERN (prev)) == SEQUENCE
26569 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26570 if (!CALL_P (prev))
26571 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26572 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26574 /* Look for a SYMBOL_REF in the "prev" instruction. */
26575 rtx x = get_call_rtx_from (PATTERN (prev));
26576 if (x)
26578 /* Try to get the call symbol, if any. */
26579 if (MEM_P (XEXP (x, 0)))
26580 x = XEXP (x, 0);
26581 /* First, look for a memory access to a symbol_ref. */
26582 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26583 && SYMBOL_REF_DECL (XEXP (x, 0))
26584 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26585 ca_loc->symbol_ref = XEXP (x, 0);
26586 /* Otherwise, look at a compile-time known user-level function
26587 declaration. */
26588 else if (MEM_P (x)
26589 && MEM_EXPR (x)
26590 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26591 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26594 ca_loc->block = insn_scope (prev);
26595 if (call_arg_locations)
26596 call_arg_loc_last->next = ca_loc;
26597 else
26598 call_arg_locations = ca_loc;
26599 call_arg_loc_last = ca_loc;
26601 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26602 newloc->label = last_label;
26603 else
26605 if (!last_postcall_label)
26607 sprintf (loclabel, "%s-1", last_label);
26608 last_postcall_label = ggc_strdup (loclabel);
26610 newloc->label = last_postcall_label;
26613 last_var_location_insn = next_real;
26614 last_in_cold_section_p = in_cold_section_p;
26617 /* Called from finalize_size_functions for size functions so that their body
26618 can be encoded in the debug info to describe the layout of variable-length
26619 structures. */
26621 static void
26622 dwarf2out_size_function (tree decl)
26624 function_to_dwarf_procedure (decl);
26627 /* Note in one location list that text section has changed. */
26630 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26632 var_loc_list *list = *slot;
26633 if (list->first)
26634 list->last_before_switch
26635 = list->last->next ? list->last->next : list->last;
26636 return 1;
26639 /* Note in all location lists that text section has changed. */
26641 static void
26642 var_location_switch_text_section (void)
26644 if (decl_loc_table == NULL)
26645 return;
26647 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26650 /* Create a new line number table. */
26652 static dw_line_info_table *
26653 new_line_info_table (void)
26655 dw_line_info_table *table;
26657 table = ggc_cleared_alloc<dw_line_info_table> ();
26658 table->file_num = 1;
26659 table->line_num = 1;
26660 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26662 return table;
26665 /* Lookup the "current" table into which we emit line info, so
26666 that we don't have to do it for every source line. */
26668 static void
26669 set_cur_line_info_table (section *sec)
26671 dw_line_info_table *table;
26673 if (sec == text_section)
26674 table = text_section_line_info;
26675 else if (sec == cold_text_section)
26677 table = cold_text_section_line_info;
26678 if (!table)
26680 cold_text_section_line_info = table = new_line_info_table ();
26681 table->end_label = cold_end_label;
26684 else
26686 const char *end_label;
26688 if (crtl->has_bb_partition)
26690 if (in_cold_section_p)
26691 end_label = crtl->subsections.cold_section_end_label;
26692 else
26693 end_label = crtl->subsections.hot_section_end_label;
26695 else
26697 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26698 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26699 current_function_funcdef_no);
26700 end_label = ggc_strdup (label);
26703 table = new_line_info_table ();
26704 table->end_label = end_label;
26706 vec_safe_push (separate_line_info, table);
26709 if (DWARF2_ASM_LINE_DEBUG_INFO)
26710 table->is_stmt = (cur_line_info_table
26711 ? cur_line_info_table->is_stmt
26712 : DWARF_LINE_DEFAULT_IS_STMT_START);
26713 cur_line_info_table = table;
26717 /* We need to reset the locations at the beginning of each
26718 function. We can't do this in the end_function hook, because the
26719 declarations that use the locations won't have been output when
26720 that hook is called. Also compute have_multiple_function_sections here. */
26722 static void
26723 dwarf2out_begin_function (tree fun)
26725 section *sec = function_section (fun);
26727 if (sec != text_section)
26728 have_multiple_function_sections = true;
26730 if (crtl->has_bb_partition && !cold_text_section)
26732 gcc_assert (current_function_decl == fun);
26733 cold_text_section = unlikely_text_section ();
26734 switch_to_section (cold_text_section);
26735 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26736 switch_to_section (sec);
26739 dwarf2out_note_section_used ();
26740 call_site_count = 0;
26741 tail_call_site_count = 0;
26743 set_cur_line_info_table (sec);
26746 /* Helper function of dwarf2out_end_function, called only after emitting
26747 the very first function into assembly. Check if some .debug_loc range
26748 might end with a .LVL* label that could be equal to .Ltext0.
26749 In that case we must force using absolute addresses in .debug_loc ranges,
26750 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26751 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26752 list terminator.
26753 Set have_multiple_function_sections to true in that case and
26754 terminate htab traversal. */
26757 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26759 var_loc_list *entry = *slot;
26760 struct var_loc_node *node;
26762 node = entry->first;
26763 if (node && node->next && node->next->label)
26765 unsigned int i;
26766 const char *label = node->next->label;
26767 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26769 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26771 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26772 if (strcmp (label, loclabel) == 0)
26774 have_multiple_function_sections = true;
26775 return 0;
26779 return 1;
26782 /* Hook called after emitting a function into assembly.
26783 This does something only for the very first function emitted. */
26785 static void
26786 dwarf2out_end_function (unsigned int)
26788 if (in_first_function_p
26789 && !have_multiple_function_sections
26790 && first_loclabel_num_not_at_text_label
26791 && decl_loc_table)
26792 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26793 in_first_function_p = false;
26794 maybe_at_text_label_p = false;
26797 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26798 front-ends register a translation unit even before dwarf2out_init is
26799 called. */
26800 static tree main_translation_unit = NULL_TREE;
26802 /* Hook called by front-ends after they built their main translation unit.
26803 Associate comp_unit_die to UNIT. */
26805 static void
26806 dwarf2out_register_main_translation_unit (tree unit)
26808 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26809 && main_translation_unit == NULL_TREE);
26810 main_translation_unit = unit;
26811 /* If dwarf2out_init has not been called yet, it will perform the association
26812 itself looking at main_translation_unit. */
26813 if (decl_die_table != NULL)
26814 equate_decl_number_to_die (unit, comp_unit_die ());
26817 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26819 static void
26820 push_dw_line_info_entry (dw_line_info_table *table,
26821 enum dw_line_info_opcode opcode, unsigned int val)
26823 dw_line_info_entry e;
26824 e.opcode = opcode;
26825 e.val = val;
26826 vec_safe_push (table->entries, e);
26829 /* Output a label to mark the beginning of a source code line entry
26830 and record information relating to this source line, in
26831 'line_info_table' for later output of the .debug_line section. */
26832 /* ??? The discriminator parameter ought to be unsigned. */
26834 static void
26835 dwarf2out_source_line (unsigned int line, unsigned int column,
26836 const char *filename,
26837 int discriminator, bool is_stmt)
26839 unsigned int file_num;
26840 dw_line_info_table *table;
26842 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26843 return;
26845 /* The discriminator column was added in dwarf4. Simplify the below
26846 by simply removing it if we're not supposed to output it. */
26847 if (dwarf_version < 4 && dwarf_strict)
26848 discriminator = 0;
26850 if (!debug_column_info)
26851 column = 0;
26853 table = cur_line_info_table;
26854 file_num = maybe_emit_file (lookup_filename (filename));
26856 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26857 the debugger has used the second (possibly duplicate) line number
26858 at the beginning of the function to mark the end of the prologue.
26859 We could eliminate any other duplicates within the function. For
26860 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26861 that second line number entry. */
26862 /* Recall that this end-of-prologue indication is *not* the same thing
26863 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26864 to which the hook corresponds, follows the last insn that was
26865 emitted by gen_prologue. What we need is to precede the first insn
26866 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26867 insn that corresponds to something the user wrote. These may be
26868 very different locations once scheduling is enabled. */
26870 if (0 && file_num == table->file_num
26871 && line == table->line_num
26872 && column == table->column_num
26873 && discriminator == table->discrim_num
26874 && is_stmt == table->is_stmt)
26875 return;
26877 switch_to_section (current_function_section ());
26879 /* If requested, emit something human-readable. */
26880 if (flag_debug_asm)
26882 if (debug_column_info)
26883 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26884 filename, line, column);
26885 else
26886 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26887 filename, line);
26890 if (DWARF2_ASM_LINE_DEBUG_INFO)
26892 /* Emit the .loc directive understood by GNU as. */
26893 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26894 file_num, line, is_stmt, discriminator */
26895 fputs ("\t.loc ", asm_out_file);
26896 fprint_ul (asm_out_file, file_num);
26897 putc (' ', asm_out_file);
26898 fprint_ul (asm_out_file, line);
26899 putc (' ', asm_out_file);
26900 if (debug_column_info)
26901 fprint_ul (asm_out_file, column);
26902 else
26903 putc ('0', asm_out_file);
26905 if (is_stmt != table->is_stmt)
26907 fputs (" is_stmt ", asm_out_file);
26908 putc (is_stmt ? '1' : '0', asm_out_file);
26910 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26912 gcc_assert (discriminator > 0);
26913 fputs (" discriminator ", asm_out_file);
26914 fprint_ul (asm_out_file, (unsigned long) discriminator);
26916 putc ('\n', asm_out_file);
26918 else
26920 unsigned int label_num = ++line_info_label_num;
26922 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26924 push_dw_line_info_entry (table, LI_set_address, label_num);
26925 if (file_num != table->file_num)
26926 push_dw_line_info_entry (table, LI_set_file, file_num);
26927 if (discriminator != table->discrim_num)
26928 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26929 if (is_stmt != table->is_stmt)
26930 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26931 push_dw_line_info_entry (table, LI_set_line, line);
26932 if (debug_column_info)
26933 push_dw_line_info_entry (table, LI_set_column, column);
26936 table->file_num = file_num;
26937 table->line_num = line;
26938 table->column_num = column;
26939 table->discrim_num = discriminator;
26940 table->is_stmt = is_stmt;
26941 table->in_use = true;
26944 /* Record the beginning of a new source file. */
26946 static void
26947 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26949 if (flag_eliminate_dwarf2_dups)
26951 /* Record the beginning of the file for break_out_includes. */
26952 dw_die_ref bincl_die;
26954 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26955 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26958 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26960 macinfo_entry e;
26961 e.code = DW_MACINFO_start_file;
26962 e.lineno = lineno;
26963 e.info = ggc_strdup (filename);
26964 vec_safe_push (macinfo_table, e);
26968 /* Record the end of a source file. */
26970 static void
26971 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26973 if (flag_eliminate_dwarf2_dups)
26974 /* Record the end of the file for break_out_includes. */
26975 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26977 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26979 macinfo_entry e;
26980 e.code = DW_MACINFO_end_file;
26981 e.lineno = lineno;
26982 e.info = NULL;
26983 vec_safe_push (macinfo_table, e);
26987 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26988 the tail part of the directive line, i.e. the part which is past the
26989 initial whitespace, #, whitespace, directive-name, whitespace part. */
26991 static void
26992 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26993 const char *buffer ATTRIBUTE_UNUSED)
26995 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26997 macinfo_entry e;
26998 /* Insert a dummy first entry to be able to optimize the whole
26999 predefined macro block using DW_MACRO_import. */
27000 if (macinfo_table->is_empty () && lineno <= 1)
27002 e.code = 0;
27003 e.lineno = 0;
27004 e.info = NULL;
27005 vec_safe_push (macinfo_table, e);
27007 e.code = DW_MACINFO_define;
27008 e.lineno = lineno;
27009 e.info = ggc_strdup (buffer);
27010 vec_safe_push (macinfo_table, e);
27014 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
27015 the tail part of the directive line, i.e. the part which is past the
27016 initial whitespace, #, whitespace, directive-name, whitespace part. */
27018 static void
27019 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
27020 const char *buffer ATTRIBUTE_UNUSED)
27022 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27024 macinfo_entry e;
27025 /* Insert a dummy first entry to be able to optimize the whole
27026 predefined macro block using DW_MACRO_import. */
27027 if (macinfo_table->is_empty () && lineno <= 1)
27029 e.code = 0;
27030 e.lineno = 0;
27031 e.info = NULL;
27032 vec_safe_push (macinfo_table, e);
27034 e.code = DW_MACINFO_undef;
27035 e.lineno = lineno;
27036 e.info = ggc_strdup (buffer);
27037 vec_safe_push (macinfo_table, e);
27041 /* Helpers to manipulate hash table of CUs. */
27043 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
27045 static inline hashval_t hash (const macinfo_entry *);
27046 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
27049 inline hashval_t
27050 macinfo_entry_hasher::hash (const macinfo_entry *entry)
27052 return htab_hash_string (entry->info);
27055 inline bool
27056 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
27057 const macinfo_entry *entry2)
27059 return !strcmp (entry1->info, entry2->info);
27062 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
27064 /* Output a single .debug_macinfo entry. */
27066 static void
27067 output_macinfo_op (macinfo_entry *ref)
27069 int file_num;
27070 size_t len;
27071 struct indirect_string_node *node;
27072 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27073 struct dwarf_file_data *fd;
27075 switch (ref->code)
27077 case DW_MACINFO_start_file:
27078 fd = lookup_filename (ref->info);
27079 file_num = maybe_emit_file (fd);
27080 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
27081 dw2_asm_output_data_uleb128 (ref->lineno,
27082 "Included from line number %lu",
27083 (unsigned long) ref->lineno);
27084 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
27085 break;
27086 case DW_MACINFO_end_file:
27087 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
27088 break;
27089 case DW_MACINFO_define:
27090 case DW_MACINFO_undef:
27091 len = strlen (ref->info) + 1;
27092 if (!dwarf_strict
27093 && len > DWARF_OFFSET_SIZE
27094 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27095 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27097 ref->code = ref->code == DW_MACINFO_define
27098 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
27099 output_macinfo_op (ref);
27100 return;
27102 dw2_asm_output_data (1, ref->code,
27103 ref->code == DW_MACINFO_define
27104 ? "Define macro" : "Undefine macro");
27105 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27106 (unsigned long) ref->lineno);
27107 dw2_asm_output_nstring (ref->info, -1, "The macro");
27108 break;
27109 case DW_MACRO_define_strp:
27110 case DW_MACRO_undef_strp:
27111 node = find_AT_string (ref->info);
27112 gcc_assert (node
27113 && (node->form == DW_FORM_strp
27114 || node->form == DW_FORM_GNU_str_index));
27115 dw2_asm_output_data (1, ref->code,
27116 ref->code == DW_MACRO_define_strp
27117 ? "Define macro strp"
27118 : "Undefine macro strp");
27119 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27120 (unsigned long) ref->lineno);
27121 if (node->form == DW_FORM_strp)
27122 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
27123 debug_str_section, "The macro: \"%s\"",
27124 ref->info);
27125 else
27126 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
27127 ref->info);
27128 break;
27129 case DW_MACRO_import:
27130 dw2_asm_output_data (1, ref->code, "Import");
27131 ASM_GENERATE_INTERNAL_LABEL (label,
27132 DEBUG_MACRO_SECTION_LABEL,
27133 ref->lineno + macinfo_label_base);
27134 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
27135 break;
27136 default:
27137 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
27138 ASM_COMMENT_START, (unsigned long) ref->code);
27139 break;
27143 /* Attempt to make a sequence of define/undef macinfo ops shareable with
27144 other compilation unit .debug_macinfo sections. IDX is the first
27145 index of a define/undef, return the number of ops that should be
27146 emitted in a comdat .debug_macinfo section and emit
27147 a DW_MACRO_import entry referencing it.
27148 If the define/undef entry should be emitted normally, return 0. */
27150 static unsigned
27151 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
27152 macinfo_hash_type **macinfo_htab)
27154 macinfo_entry *first, *second, *cur, *inc;
27155 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
27156 unsigned char checksum[16];
27157 struct md5_ctx ctx;
27158 char *grp_name, *tail;
27159 const char *base;
27160 unsigned int i, count, encoded_filename_len, linebuf_len;
27161 macinfo_entry **slot;
27163 first = &(*macinfo_table)[idx];
27164 second = &(*macinfo_table)[idx + 1];
27166 /* Optimize only if there are at least two consecutive define/undef ops,
27167 and either all of them are before first DW_MACINFO_start_file
27168 with lineno {0,1} (i.e. predefined macro block), or all of them are
27169 in some included header file. */
27170 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
27171 return 0;
27172 if (vec_safe_is_empty (files))
27174 if (first->lineno > 1 || second->lineno > 1)
27175 return 0;
27177 else if (first->lineno == 0)
27178 return 0;
27180 /* Find the last define/undef entry that can be grouped together
27181 with first and at the same time compute md5 checksum of their
27182 codes, linenumbers and strings. */
27183 md5_init_ctx (&ctx);
27184 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27185 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27186 break;
27187 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27188 break;
27189 else
27191 unsigned char code = cur->code;
27192 md5_process_bytes (&code, 1, &ctx);
27193 checksum_uleb128 (cur->lineno, &ctx);
27194 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27196 md5_finish_ctx (&ctx, checksum);
27197 count = i - idx;
27199 /* From the containing include filename (if any) pick up just
27200 usable characters from its basename. */
27201 if (vec_safe_is_empty (files))
27202 base = "";
27203 else
27204 base = lbasename (files->last ().info);
27205 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27206 if (ISIDNUM (base[i]) || base[i] == '.')
27207 encoded_filename_len++;
27208 /* Count . at the end. */
27209 if (encoded_filename_len)
27210 encoded_filename_len++;
27212 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27213 linebuf_len = strlen (linebuf);
27215 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27216 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27217 + 16 * 2 + 1);
27218 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27219 tail = grp_name + 4;
27220 if (encoded_filename_len)
27222 for (i = 0; base[i]; i++)
27223 if (ISIDNUM (base[i]) || base[i] == '.')
27224 *tail++ = base[i];
27225 *tail++ = '.';
27227 memcpy (tail, linebuf, linebuf_len);
27228 tail += linebuf_len;
27229 *tail++ = '.';
27230 for (i = 0; i < 16; i++)
27231 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27233 /* Construct a macinfo_entry for DW_MACRO_import
27234 in the empty vector entry before the first define/undef. */
27235 inc = &(*macinfo_table)[idx - 1];
27236 inc->code = DW_MACRO_import;
27237 inc->lineno = 0;
27238 inc->info = ggc_strdup (grp_name);
27239 if (!*macinfo_htab)
27240 *macinfo_htab = new macinfo_hash_type (10);
27241 /* Avoid emitting duplicates. */
27242 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27243 if (*slot != NULL)
27245 inc->code = 0;
27246 inc->info = NULL;
27247 /* If such an entry has been used before, just emit
27248 a DW_MACRO_import op. */
27249 inc = *slot;
27250 output_macinfo_op (inc);
27251 /* And clear all macinfo_entry in the range to avoid emitting them
27252 in the second pass. */
27253 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27255 cur->code = 0;
27256 cur->info = NULL;
27259 else
27261 *slot = inc;
27262 inc->lineno = (*macinfo_htab)->elements ();
27263 output_macinfo_op (inc);
27265 return count;
27268 /* Save any strings needed by the macinfo table in the debug str
27269 table. All strings must be collected into the table by the time
27270 index_string is called. */
27272 static void
27273 save_macinfo_strings (void)
27275 unsigned len;
27276 unsigned i;
27277 macinfo_entry *ref;
27279 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27281 switch (ref->code)
27283 /* Match the logic in output_macinfo_op to decide on
27284 indirect strings. */
27285 case DW_MACINFO_define:
27286 case DW_MACINFO_undef:
27287 len = strlen (ref->info) + 1;
27288 if (!dwarf_strict
27289 && len > DWARF_OFFSET_SIZE
27290 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27291 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27292 set_indirect_string (find_AT_string (ref->info));
27293 break;
27294 case DW_MACRO_define_strp:
27295 case DW_MACRO_undef_strp:
27296 set_indirect_string (find_AT_string (ref->info));
27297 break;
27298 default:
27299 break;
27304 /* Output macinfo section(s). */
27306 static void
27307 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27309 unsigned i;
27310 unsigned long length = vec_safe_length (macinfo_table);
27311 macinfo_entry *ref;
27312 vec<macinfo_entry, va_gc> *files = NULL;
27313 macinfo_hash_type *macinfo_htab = NULL;
27314 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27316 if (! length)
27317 return;
27319 /* output_macinfo* uses these interchangeably. */
27320 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27321 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27322 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27323 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27325 /* AIX Assembler inserts the length, so adjust the reference to match the
27326 offset expected by debuggers. */
27327 strcpy (dl_section_ref, debug_line_label);
27328 if (XCOFF_DEBUGGING_INFO)
27329 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27331 /* For .debug_macro emit the section header. */
27332 if (!dwarf_strict || dwarf_version >= 5)
27334 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27335 "DWARF macro version number");
27336 if (DWARF_OFFSET_SIZE == 8)
27337 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27338 else
27339 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27340 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27341 debug_line_section, NULL);
27344 /* In the first loop, it emits the primary .debug_macinfo section
27345 and after each emitted op the macinfo_entry is cleared.
27346 If a longer range of define/undef ops can be optimized using
27347 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27348 the vector before the first define/undef in the range and the
27349 whole range of define/undef ops is not emitted and kept. */
27350 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27352 switch (ref->code)
27354 case DW_MACINFO_start_file:
27355 vec_safe_push (files, *ref);
27356 break;
27357 case DW_MACINFO_end_file:
27358 if (!vec_safe_is_empty (files))
27359 files->pop ();
27360 break;
27361 case DW_MACINFO_define:
27362 case DW_MACINFO_undef:
27363 if ((!dwarf_strict || dwarf_version >= 5)
27364 && HAVE_COMDAT_GROUP
27365 && vec_safe_length (files) != 1
27366 && i > 0
27367 && i + 1 < length
27368 && (*macinfo_table)[i - 1].code == 0)
27370 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27371 if (count)
27373 i += count - 1;
27374 continue;
27377 break;
27378 case 0:
27379 /* A dummy entry may be inserted at the beginning to be able
27380 to optimize the whole block of predefined macros. */
27381 if (i == 0)
27382 continue;
27383 default:
27384 break;
27386 output_macinfo_op (ref);
27387 ref->info = NULL;
27388 ref->code = 0;
27391 if (!macinfo_htab)
27392 return;
27394 /* Save the number of transparent includes so we can adjust the
27395 label number for the fat LTO object DWARF. */
27396 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27398 delete macinfo_htab;
27399 macinfo_htab = NULL;
27401 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27402 terminate the current chain and switch to a new comdat .debug_macinfo
27403 section and emit the define/undef entries within it. */
27404 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27405 switch (ref->code)
27407 case 0:
27408 continue;
27409 case DW_MACRO_import:
27411 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27412 tree comdat_key = get_identifier (ref->info);
27413 /* Terminate the previous .debug_macinfo section. */
27414 dw2_asm_output_data (1, 0, "End compilation unit");
27415 targetm.asm_out.named_section (debug_macinfo_section_name,
27416 SECTION_DEBUG
27417 | SECTION_LINKONCE
27418 | (early_lto_debug
27419 ? SECTION_EXCLUDE : 0),
27420 comdat_key);
27421 ASM_GENERATE_INTERNAL_LABEL (label,
27422 DEBUG_MACRO_SECTION_LABEL,
27423 ref->lineno + macinfo_label_base);
27424 ASM_OUTPUT_LABEL (asm_out_file, label);
27425 ref->code = 0;
27426 ref->info = NULL;
27427 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27428 "DWARF macro version number");
27429 if (DWARF_OFFSET_SIZE == 8)
27430 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27431 else
27432 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27434 break;
27435 case DW_MACINFO_define:
27436 case DW_MACINFO_undef:
27437 output_macinfo_op (ref);
27438 ref->code = 0;
27439 ref->info = NULL;
27440 break;
27441 default:
27442 gcc_unreachable ();
27445 macinfo_label_base += macinfo_label_base_adj;
27448 /* Initialize the various sections and labels for dwarf output and prefix
27449 them with PREFIX if non-NULL. */
27451 static void
27452 init_sections_and_labels (bool early_lto_debug)
27454 /* As we may get called multiple times have a generation count for labels. */
27455 static unsigned generation = 0;
27457 if (early_lto_debug)
27459 if (!dwarf_split_debug_info)
27461 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27462 SECTION_DEBUG | SECTION_EXCLUDE,
27463 NULL);
27464 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27465 SECTION_DEBUG | SECTION_EXCLUDE,
27466 NULL);
27467 debug_macinfo_section_name = ((dwarf_strict && dwarf_version < 5)
27468 ? DEBUG_LTO_MACINFO_SECTION
27469 : DEBUG_LTO_MACRO_SECTION);
27470 debug_macinfo_section = get_section (debug_macinfo_section_name,
27471 SECTION_DEBUG
27472 | SECTION_EXCLUDE, NULL);
27473 /* For macro info we have to refer to a debug_line section, so similar
27474 to split-dwarf emit a skeleton one for early debug. */
27475 debug_skeleton_line_section
27476 = get_section (DEBUG_LTO_LINE_SECTION,
27477 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27478 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27479 DEBUG_SKELETON_LINE_SECTION_LABEL,
27480 generation);
27482 else
27484 /* ??? Which of the following do we need early? */
27485 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27486 SECTION_DEBUG | SECTION_EXCLUDE,
27487 NULL);
27488 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27489 SECTION_DEBUG | SECTION_EXCLUDE,
27490 NULL);
27491 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27492 SECTION_DEBUG
27493 | SECTION_EXCLUDE, NULL);
27494 debug_skeleton_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27495 SECTION_DEBUG
27496 | SECTION_EXCLUDE, NULL);
27497 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27498 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27499 generation);
27501 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27502 the main .o, but the skeleton_line goes into the split off dwo. */
27503 debug_skeleton_line_section
27504 = get_section (DEBUG_LTO_LINE_SECTION,
27505 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27506 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27507 DEBUG_SKELETON_LINE_SECTION_LABEL,
27508 generation);
27509 debug_str_offsets_section
27510 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27511 SECTION_DEBUG | SECTION_EXCLUDE,
27512 NULL);
27513 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27514 DEBUG_SKELETON_INFO_SECTION_LABEL,
27515 generation);
27516 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27517 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27518 debug_macinfo_section_name
27519 = (dwarf_strict
27520 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27521 debug_macinfo_section = get_section (debug_macinfo_section_name,
27522 SECTION_DEBUG | SECTION_EXCLUDE,
27523 NULL);
27525 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27526 DEBUG_STR_SECTION_FLAGS
27527 | SECTION_EXCLUDE, NULL);
27529 else
27531 if (!dwarf_split_debug_info)
27533 debug_info_section = get_section (DEBUG_INFO_SECTION,
27534 SECTION_DEBUG, NULL);
27535 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27536 SECTION_DEBUG, NULL);
27537 debug_loc_section = get_section (DEBUG_LOC_SECTION,
27538 SECTION_DEBUG, NULL);
27539 debug_macinfo_section_name
27540 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27541 debug_macinfo_section = get_section (debug_macinfo_section_name,
27542 SECTION_DEBUG, NULL);
27544 else
27546 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27547 SECTION_DEBUG | SECTION_EXCLUDE,
27548 NULL);
27549 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27550 SECTION_DEBUG | SECTION_EXCLUDE,
27551 NULL);
27552 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27553 SECTION_DEBUG, NULL);
27554 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27555 SECTION_DEBUG, NULL);
27556 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27557 SECTION_DEBUG, NULL);
27558 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27559 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27560 generation);
27562 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27563 stay in the main .o, but the skeleton_line goes into the
27564 split off dwo. */
27565 debug_skeleton_line_section
27566 = get_section (DEBUG_DWO_LINE_SECTION,
27567 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27568 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27569 DEBUG_SKELETON_LINE_SECTION_LABEL,
27570 generation);
27571 debug_str_offsets_section
27572 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27573 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27574 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27575 DEBUG_SKELETON_INFO_SECTION_LABEL,
27576 generation);
27577 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
27578 SECTION_DEBUG | SECTION_EXCLUDE,
27579 NULL);
27580 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27581 DEBUG_STR_DWO_SECTION_FLAGS,
27582 NULL);
27583 debug_macinfo_section_name
27584 = (dwarf_strict && dwarf_version < 5)
27585 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27586 debug_macinfo_section = get_section (debug_macinfo_section_name,
27587 SECTION_DEBUG | SECTION_EXCLUDE,
27588 NULL);
27590 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27591 SECTION_DEBUG, NULL);
27592 debug_line_section = get_section (DEBUG_LINE_SECTION,
27593 SECTION_DEBUG, NULL);
27594 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27595 SECTION_DEBUG, NULL);
27596 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27597 SECTION_DEBUG, NULL);
27598 debug_str_section = get_section (DEBUG_STR_SECTION,
27599 DEBUG_STR_SECTION_FLAGS, NULL);
27600 debug_ranges_section = get_section (dwarf_version >= 5
27601 ? DEBUG_RNGLISTS_SECTION
27602 : DEBUG_RANGES_SECTION,
27603 SECTION_DEBUG, NULL);
27604 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27605 SECTION_DEBUG, NULL);
27608 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27609 DEBUG_ABBREV_SECTION_LABEL, generation);
27610 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27611 DEBUG_INFO_SECTION_LABEL, generation);
27612 info_section_emitted = false;
27613 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27614 DEBUG_LINE_SECTION_LABEL, generation);
27615 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27616 DEBUG_RANGES_SECTION_LABEL, generation);
27617 if (dwarf_version >= 5 && dwarf_split_debug_info)
27618 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27619 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27620 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27621 DEBUG_ADDR_SECTION_LABEL, generation);
27622 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27623 (dwarf_strict && dwarf_version < 5)
27624 ? DEBUG_MACINFO_SECTION_LABEL
27625 : DEBUG_MACRO_SECTION_LABEL, generation);
27626 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27627 generation);
27629 ++generation;
27632 /* Set up for Dwarf output at the start of compilation. */
27634 static void
27635 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27637 /* This option is currently broken, see (PR53118 and PR46102). */
27638 if (flag_eliminate_dwarf2_dups
27639 && strstr (lang_hooks.name, "C++"))
27641 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27642 flag_eliminate_dwarf2_dups = 0;
27645 /* Allocate the file_table. */
27646 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27648 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27649 /* Allocate the decl_die_table. */
27650 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27652 /* Allocate the decl_loc_table. */
27653 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27655 /* Allocate the cached_dw_loc_list_table. */
27656 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27658 /* Allocate the initial hunk of the decl_scope_table. */
27659 vec_alloc (decl_scope_table, 256);
27661 /* Allocate the initial hunk of the abbrev_die_table. */
27662 vec_alloc (abbrev_die_table, 256);
27663 /* Zero-th entry is allocated, but unused. */
27664 abbrev_die_table->quick_push (NULL);
27666 /* Allocate the dwarf_proc_stack_usage_map. */
27667 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27669 /* Allocate the pubtypes and pubnames vectors. */
27670 vec_alloc (pubname_table, 32);
27671 vec_alloc (pubtype_table, 32);
27673 vec_alloc (incomplete_types, 64);
27675 vec_alloc (used_rtx_array, 32);
27677 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27678 vec_alloc (macinfo_table, 64);
27679 #endif
27681 /* If front-ends already registered a main translation unit but we were not
27682 ready to perform the association, do this now. */
27683 if (main_translation_unit != NULL_TREE)
27684 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27687 /* Called before compile () starts outputtting functions, variables
27688 and toplevel asms into assembly. */
27690 static void
27691 dwarf2out_assembly_start (void)
27693 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27694 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27695 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27696 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27697 COLD_TEXT_SECTION_LABEL, 0);
27698 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27700 switch_to_section (text_section);
27701 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27702 #endif
27704 /* Make sure the line number table for .text always exists. */
27705 text_section_line_info = new_line_info_table ();
27706 text_section_line_info->end_label = text_end_label;
27708 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27709 cur_line_info_table = text_section_line_info;
27710 #endif
27712 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27713 && dwarf2out_do_cfi_asm ()
27714 && (!(flag_unwind_tables || flag_exceptions)
27715 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27716 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27719 /* A helper function for dwarf2out_finish called through
27720 htab_traverse. Assign a string its index. All strings must be
27721 collected into the table by the time index_string is called,
27722 because the indexing code relies on htab_traverse to traverse nodes
27723 in the same order for each run. */
27726 index_string (indirect_string_node **h, unsigned int *index)
27728 indirect_string_node *node = *h;
27730 find_string_form (node);
27731 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27733 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27734 node->index = *index;
27735 *index += 1;
27737 return 1;
27740 /* A helper function for output_indirect_strings called through
27741 htab_traverse. Output the offset to a string and update the
27742 current offset. */
27745 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27747 indirect_string_node *node = *h;
27749 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27751 /* Assert that this node has been assigned an index. */
27752 gcc_assert (node->index != NO_INDEX_ASSIGNED
27753 && node->index != NOT_INDEXED);
27754 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27755 "indexed string 0x%x: %s", node->index, node->str);
27756 *offset += strlen (node->str) + 1;
27758 return 1;
27761 /* A helper function for dwarf2out_finish called through
27762 htab_traverse. Output the indexed string. */
27765 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27767 struct indirect_string_node *node = *h;
27769 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27771 /* Assert that the strings are output in the same order as their
27772 indexes were assigned. */
27773 gcc_assert (*cur_idx == node->index);
27774 assemble_string (node->str, strlen (node->str) + 1);
27775 *cur_idx += 1;
27777 return 1;
27780 /* A helper function for dwarf2out_finish called through
27781 htab_traverse. Emit one queued .debug_str string. */
27784 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27786 struct indirect_string_node *node = *h;
27788 node->form = find_string_form (node);
27789 if (node->form == form && node->refcount > 0)
27791 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27792 assemble_string (node->str, strlen (node->str) + 1);
27795 return 1;
27798 /* Output the indexed string table. */
27800 static void
27801 output_indirect_strings (void)
27803 switch_to_section (debug_str_section);
27804 if (!dwarf_split_debug_info)
27805 debug_str_hash->traverse<enum dwarf_form,
27806 output_indirect_string> (DW_FORM_strp);
27807 else
27809 unsigned int offset = 0;
27810 unsigned int cur_idx = 0;
27812 skeleton_debug_str_hash->traverse<enum dwarf_form,
27813 output_indirect_string> (DW_FORM_strp);
27815 switch_to_section (debug_str_offsets_section);
27816 debug_str_hash->traverse_noresize
27817 <unsigned int *, output_index_string_offset> (&offset);
27818 switch_to_section (debug_str_dwo_section);
27819 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27820 (&cur_idx);
27824 /* Callback for htab_traverse to assign an index to an entry in the
27825 table, and to write that entry to the .debug_addr section. */
27828 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27830 addr_table_entry *entry = *slot;
27832 if (entry->refcount == 0)
27834 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27835 || entry->index == NOT_INDEXED);
27836 return 1;
27839 gcc_assert (entry->index == *cur_index);
27840 (*cur_index)++;
27842 switch (entry->kind)
27844 case ate_kind_rtx:
27845 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27846 "0x%x", entry->index);
27847 break;
27848 case ate_kind_rtx_dtprel:
27849 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27850 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27851 DWARF2_ADDR_SIZE,
27852 entry->addr.rtl);
27853 fputc ('\n', asm_out_file);
27854 break;
27855 case ate_kind_label:
27856 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27857 "0x%x", entry->index);
27858 break;
27859 default:
27860 gcc_unreachable ();
27862 return 1;
27865 /* Produce the .debug_addr section. */
27867 static void
27868 output_addr_table (void)
27870 unsigned int index = 0;
27871 if (addr_index_table == NULL || addr_index_table->size () == 0)
27872 return;
27874 switch_to_section (debug_addr_section);
27875 addr_index_table
27876 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27879 #if ENABLE_ASSERT_CHECKING
27880 /* Verify that all marks are clear. */
27882 static void
27883 verify_marks_clear (dw_die_ref die)
27885 dw_die_ref c;
27887 gcc_assert (! die->die_mark);
27888 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27890 #endif /* ENABLE_ASSERT_CHECKING */
27892 /* Clear the marks for a die and its children.
27893 Be cool if the mark isn't set. */
27895 static void
27896 prune_unmark_dies (dw_die_ref die)
27898 dw_die_ref c;
27900 if (die->die_mark)
27901 die->die_mark = 0;
27902 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27905 /* Given LOC that is referenced by a DIE we're marking as used, find all
27906 referenced DWARF procedures it references and mark them as used. */
27908 static void
27909 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27911 for (; loc != NULL; loc = loc->dw_loc_next)
27912 switch (loc->dw_loc_opc)
27914 case DW_OP_implicit_pointer:
27915 case DW_OP_convert:
27916 case DW_OP_reinterpret:
27917 case DW_OP_GNU_implicit_pointer:
27918 case DW_OP_GNU_convert:
27919 case DW_OP_GNU_reinterpret:
27920 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27921 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27922 break;
27923 case DW_OP_GNU_variable_value:
27924 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27926 dw_die_ref ref
27927 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27928 if (ref == NULL)
27929 break;
27930 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27931 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27932 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27934 /* FALLTHRU */
27935 case DW_OP_call2:
27936 case DW_OP_call4:
27937 case DW_OP_call_ref:
27938 case DW_OP_const_type:
27939 case DW_OP_GNU_const_type:
27940 case DW_OP_GNU_parameter_ref:
27941 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27942 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27943 break;
27944 case DW_OP_regval_type:
27945 case DW_OP_deref_type:
27946 case DW_OP_GNU_regval_type:
27947 case DW_OP_GNU_deref_type:
27948 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27949 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27950 break;
27951 case DW_OP_entry_value:
27952 case DW_OP_GNU_entry_value:
27953 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27954 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27955 break;
27956 default:
27957 break;
27961 /* Given DIE that we're marking as used, find any other dies
27962 it references as attributes and mark them as used. */
27964 static void
27965 prune_unused_types_walk_attribs (dw_die_ref die)
27967 dw_attr_node *a;
27968 unsigned ix;
27970 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27972 switch (AT_class (a))
27974 /* Make sure DWARF procedures referenced by location descriptions will
27975 get emitted. */
27976 case dw_val_class_loc:
27977 prune_unused_types_walk_loc_descr (AT_loc (a));
27978 break;
27979 case dw_val_class_loc_list:
27980 for (dw_loc_list_ref list = AT_loc_list (a);
27981 list != NULL;
27982 list = list->dw_loc_next)
27983 prune_unused_types_walk_loc_descr (list->expr);
27984 break;
27986 case dw_val_class_die_ref:
27987 /* A reference to another DIE.
27988 Make sure that it will get emitted.
27989 If it was broken out into a comdat group, don't follow it. */
27990 if (! AT_ref (a)->comdat_type_p
27991 || a->dw_attr == DW_AT_specification)
27992 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27993 break;
27995 case dw_val_class_str:
27996 /* Set the string's refcount to 0 so that prune_unused_types_mark
27997 accounts properly for it. */
27998 a->dw_attr_val.v.val_str->refcount = 0;
27999 break;
28001 default:
28002 break;
28007 /* Mark the generic parameters and arguments children DIEs of DIE. */
28009 static void
28010 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
28012 dw_die_ref c;
28014 if (die == NULL || die->die_child == NULL)
28015 return;
28016 c = die->die_child;
28019 if (is_template_parameter (c))
28020 prune_unused_types_mark (c, 1);
28021 c = c->die_sib;
28022 } while (c && c != die->die_child);
28025 /* Mark DIE as being used. If DOKIDS is true, then walk down
28026 to DIE's children. */
28028 static void
28029 prune_unused_types_mark (dw_die_ref die, int dokids)
28031 dw_die_ref c;
28033 if (die->die_mark == 0)
28035 /* We haven't done this node yet. Mark it as used. */
28036 die->die_mark = 1;
28037 /* If this is the DIE of a generic type instantiation,
28038 mark the children DIEs that describe its generic parms and
28039 args. */
28040 prune_unused_types_mark_generic_parms_dies (die);
28042 /* We also have to mark its parents as used.
28043 (But we don't want to mark our parent's kids due to this,
28044 unless it is a class.) */
28045 if (die->die_parent)
28046 prune_unused_types_mark (die->die_parent,
28047 class_scope_p (die->die_parent));
28049 /* Mark any referenced nodes. */
28050 prune_unused_types_walk_attribs (die);
28052 /* If this node is a specification,
28053 also mark the definition, if it exists. */
28054 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
28055 prune_unused_types_mark (die->die_definition, 1);
28058 if (dokids && die->die_mark != 2)
28060 /* We need to walk the children, but haven't done so yet.
28061 Remember that we've walked the kids. */
28062 die->die_mark = 2;
28064 /* If this is an array type, we need to make sure our
28065 kids get marked, even if they're types. If we're
28066 breaking out types into comdat sections, do this
28067 for all type definitions. */
28068 if (die->die_tag == DW_TAG_array_type
28069 || (use_debug_types
28070 && is_type_die (die) && ! is_declaration_die (die)))
28071 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
28072 else
28073 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28077 /* For local classes, look if any static member functions were emitted
28078 and if so, mark them. */
28080 static void
28081 prune_unused_types_walk_local_classes (dw_die_ref die)
28083 dw_die_ref c;
28085 if (die->die_mark == 2)
28086 return;
28088 switch (die->die_tag)
28090 case DW_TAG_structure_type:
28091 case DW_TAG_union_type:
28092 case DW_TAG_class_type:
28093 break;
28095 case DW_TAG_subprogram:
28096 if (!get_AT_flag (die, DW_AT_declaration)
28097 || die->die_definition != NULL)
28098 prune_unused_types_mark (die, 1);
28099 return;
28101 default:
28102 return;
28105 /* Mark children. */
28106 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
28109 /* Walk the tree DIE and mark types that we actually use. */
28111 static void
28112 prune_unused_types_walk (dw_die_ref die)
28114 dw_die_ref c;
28116 /* Don't do anything if this node is already marked and
28117 children have been marked as well. */
28118 if (die->die_mark == 2)
28119 return;
28121 switch (die->die_tag)
28123 case DW_TAG_structure_type:
28124 case DW_TAG_union_type:
28125 case DW_TAG_class_type:
28126 if (die->die_perennial_p)
28127 break;
28129 for (c = die->die_parent; c; c = c->die_parent)
28130 if (c->die_tag == DW_TAG_subprogram)
28131 break;
28133 /* Finding used static member functions inside of classes
28134 is needed just for local classes, because for other classes
28135 static member function DIEs with DW_AT_specification
28136 are emitted outside of the DW_TAG_*_type. If we ever change
28137 it, we'd need to call this even for non-local classes. */
28138 if (c)
28139 prune_unused_types_walk_local_classes (die);
28141 /* It's a type node --- don't mark it. */
28142 return;
28144 case DW_TAG_const_type:
28145 case DW_TAG_packed_type:
28146 case DW_TAG_pointer_type:
28147 case DW_TAG_reference_type:
28148 case DW_TAG_rvalue_reference_type:
28149 case DW_TAG_volatile_type:
28150 case DW_TAG_typedef:
28151 case DW_TAG_array_type:
28152 case DW_TAG_interface_type:
28153 case DW_TAG_friend:
28154 case DW_TAG_enumeration_type:
28155 case DW_TAG_subroutine_type:
28156 case DW_TAG_string_type:
28157 case DW_TAG_set_type:
28158 case DW_TAG_subrange_type:
28159 case DW_TAG_ptr_to_member_type:
28160 case DW_TAG_file_type:
28161 /* Type nodes are useful only when other DIEs reference them --- don't
28162 mark them. */
28163 /* FALLTHROUGH */
28165 case DW_TAG_dwarf_procedure:
28166 /* Likewise for DWARF procedures. */
28168 if (die->die_perennial_p)
28169 break;
28171 return;
28173 default:
28174 /* Mark everything else. */
28175 break;
28178 if (die->die_mark == 0)
28180 die->die_mark = 1;
28182 /* Now, mark any dies referenced from here. */
28183 prune_unused_types_walk_attribs (die);
28186 die->die_mark = 2;
28188 /* Mark children. */
28189 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28192 /* Increment the string counts on strings referred to from DIE's
28193 attributes. */
28195 static void
28196 prune_unused_types_update_strings (dw_die_ref die)
28198 dw_attr_node *a;
28199 unsigned ix;
28201 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28202 if (AT_class (a) == dw_val_class_str)
28204 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28205 s->refcount++;
28206 /* Avoid unnecessarily putting strings that are used less than
28207 twice in the hash table. */
28208 if (s->refcount
28209 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28211 indirect_string_node **slot
28212 = debug_str_hash->find_slot_with_hash (s->str,
28213 htab_hash_string (s->str),
28214 INSERT);
28215 gcc_assert (*slot == NULL);
28216 *slot = s;
28221 /* Mark DIE and its children as removed. */
28223 static void
28224 mark_removed (dw_die_ref die)
28226 dw_die_ref c;
28227 die->removed = true;
28228 FOR_EACH_CHILD (die, c, mark_removed (c));
28231 /* Remove from the tree DIE any dies that aren't marked. */
28233 static void
28234 prune_unused_types_prune (dw_die_ref die)
28236 dw_die_ref c;
28238 gcc_assert (die->die_mark);
28239 prune_unused_types_update_strings (die);
28241 if (! die->die_child)
28242 return;
28244 c = die->die_child;
28245 do {
28246 dw_die_ref prev = c, next;
28247 for (c = c->die_sib; ! c->die_mark; c = next)
28248 if (c == die->die_child)
28250 /* No marked children between 'prev' and the end of the list. */
28251 if (prev == c)
28252 /* No marked children at all. */
28253 die->die_child = NULL;
28254 else
28256 prev->die_sib = c->die_sib;
28257 die->die_child = prev;
28259 c->die_sib = NULL;
28260 mark_removed (c);
28261 return;
28263 else
28265 next = c->die_sib;
28266 c->die_sib = NULL;
28267 mark_removed (c);
28270 if (c != prev->die_sib)
28271 prev->die_sib = c;
28272 prune_unused_types_prune (c);
28273 } while (c != die->die_child);
28276 /* Remove dies representing declarations that we never use. */
28278 static void
28279 prune_unused_types (void)
28281 unsigned int i;
28282 limbo_die_node *node;
28283 comdat_type_node *ctnode;
28284 pubname_entry *pub;
28285 dw_die_ref base_type;
28287 #if ENABLE_ASSERT_CHECKING
28288 /* All the marks should already be clear. */
28289 verify_marks_clear (comp_unit_die ());
28290 for (node = limbo_die_list; node; node = node->next)
28291 verify_marks_clear (node->die);
28292 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28293 verify_marks_clear (ctnode->root_die);
28294 #endif /* ENABLE_ASSERT_CHECKING */
28296 /* Mark types that are used in global variables. */
28297 premark_types_used_by_global_vars ();
28299 /* Set the mark on nodes that are actually used. */
28300 prune_unused_types_walk (comp_unit_die ());
28301 for (node = limbo_die_list; node; node = node->next)
28302 prune_unused_types_walk (node->die);
28303 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28305 prune_unused_types_walk (ctnode->root_die);
28306 prune_unused_types_mark (ctnode->type_die, 1);
28309 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28310 are unusual in that they are pubnames that are the children of pubtypes.
28311 They should only be marked via their parent DW_TAG_enumeration_type die,
28312 not as roots in themselves. */
28313 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28314 if (pub->die->die_tag != DW_TAG_enumerator)
28315 prune_unused_types_mark (pub->die, 1);
28316 for (i = 0; base_types.iterate (i, &base_type); i++)
28317 prune_unused_types_mark (base_type, 1);
28319 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28320 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28321 callees). */
28322 cgraph_node *cnode;
28323 FOR_EACH_FUNCTION (cnode)
28324 if (cnode->referred_to_p (false))
28326 dw_die_ref die = lookup_decl_die (cnode->decl);
28327 if (die == NULL || die->die_mark)
28328 continue;
28329 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28330 if (e->caller != cnode
28331 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28333 prune_unused_types_mark (die, 1);
28334 break;
28338 if (debug_str_hash)
28339 debug_str_hash->empty ();
28340 if (skeleton_debug_str_hash)
28341 skeleton_debug_str_hash->empty ();
28342 prune_unused_types_prune (comp_unit_die ());
28343 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28345 node = *pnode;
28346 if (!node->die->die_mark)
28347 *pnode = node->next;
28348 else
28350 prune_unused_types_prune (node->die);
28351 pnode = &node->next;
28354 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28355 prune_unused_types_prune (ctnode->root_die);
28357 /* Leave the marks clear. */
28358 prune_unmark_dies (comp_unit_die ());
28359 for (node = limbo_die_list; node; node = node->next)
28360 prune_unmark_dies (node->die);
28361 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28362 prune_unmark_dies (ctnode->root_die);
28365 /* Helpers to manipulate hash table of comdat type units. */
28367 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28369 static inline hashval_t hash (const comdat_type_node *);
28370 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28373 inline hashval_t
28374 comdat_type_hasher::hash (const comdat_type_node *type_node)
28376 hashval_t h;
28377 memcpy (&h, type_node->signature, sizeof (h));
28378 return h;
28381 inline bool
28382 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28383 const comdat_type_node *type_node_2)
28385 return (! memcmp (type_node_1->signature, type_node_2->signature,
28386 DWARF_TYPE_SIGNATURE_SIZE));
28389 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28390 to the location it would have been added, should we know its
28391 DECL_ASSEMBLER_NAME when we added other attributes. This will
28392 probably improve compactness of debug info, removing equivalent
28393 abbrevs, and hide any differences caused by deferring the
28394 computation of the assembler name, triggered by e.g. PCH. */
28396 static inline void
28397 move_linkage_attr (dw_die_ref die)
28399 unsigned ix = vec_safe_length (die->die_attr);
28400 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28402 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28403 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28405 while (--ix > 0)
28407 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28409 if (prev->dw_attr == DW_AT_decl_line
28410 || prev->dw_attr == DW_AT_decl_column
28411 || prev->dw_attr == DW_AT_name)
28412 break;
28415 if (ix != vec_safe_length (die->die_attr) - 1)
28417 die->die_attr->pop ();
28418 die->die_attr->quick_insert (ix, linkage);
28422 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28423 referenced from typed stack ops and count how often they are used. */
28425 static void
28426 mark_base_types (dw_loc_descr_ref loc)
28428 dw_die_ref base_type = NULL;
28430 for (; loc; loc = loc->dw_loc_next)
28432 switch (loc->dw_loc_opc)
28434 case DW_OP_regval_type:
28435 case DW_OP_deref_type:
28436 case DW_OP_GNU_regval_type:
28437 case DW_OP_GNU_deref_type:
28438 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28439 break;
28440 case DW_OP_convert:
28441 case DW_OP_reinterpret:
28442 case DW_OP_GNU_convert:
28443 case DW_OP_GNU_reinterpret:
28444 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28445 continue;
28446 /* FALLTHRU */
28447 case DW_OP_const_type:
28448 case DW_OP_GNU_const_type:
28449 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28450 break;
28451 case DW_OP_entry_value:
28452 case DW_OP_GNU_entry_value:
28453 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28454 continue;
28455 default:
28456 continue;
28458 gcc_assert (base_type->die_parent == comp_unit_die ());
28459 if (base_type->die_mark)
28460 base_type->die_mark++;
28461 else
28463 base_types.safe_push (base_type);
28464 base_type->die_mark = 1;
28469 /* Comparison function for sorting marked base types. */
28471 static int
28472 base_type_cmp (const void *x, const void *y)
28474 dw_die_ref dx = *(const dw_die_ref *) x;
28475 dw_die_ref dy = *(const dw_die_ref *) y;
28476 unsigned int byte_size1, byte_size2;
28477 unsigned int encoding1, encoding2;
28478 unsigned int align1, align2;
28479 if (dx->die_mark > dy->die_mark)
28480 return -1;
28481 if (dx->die_mark < dy->die_mark)
28482 return 1;
28483 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28484 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28485 if (byte_size1 < byte_size2)
28486 return 1;
28487 if (byte_size1 > byte_size2)
28488 return -1;
28489 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28490 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28491 if (encoding1 < encoding2)
28492 return 1;
28493 if (encoding1 > encoding2)
28494 return -1;
28495 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28496 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28497 if (align1 < align2)
28498 return 1;
28499 if (align1 > align2)
28500 return -1;
28501 return 0;
28504 /* Move base types marked by mark_base_types as early as possible
28505 in the CU, sorted by decreasing usage count both to make the
28506 uleb128 references as small as possible and to make sure they
28507 will have die_offset already computed by calc_die_sizes when
28508 sizes of typed stack loc ops is computed. */
28510 static void
28511 move_marked_base_types (void)
28513 unsigned int i;
28514 dw_die_ref base_type, die, c;
28516 if (base_types.is_empty ())
28517 return;
28519 /* Sort by decreasing usage count, they will be added again in that
28520 order later on. */
28521 base_types.qsort (base_type_cmp);
28522 die = comp_unit_die ();
28523 c = die->die_child;
28526 dw_die_ref prev = c;
28527 c = c->die_sib;
28528 while (c->die_mark)
28530 remove_child_with_prev (c, prev);
28531 /* As base types got marked, there must be at least
28532 one node other than DW_TAG_base_type. */
28533 gcc_assert (die->die_child != NULL);
28534 c = prev->die_sib;
28537 while (c != die->die_child);
28538 gcc_assert (die->die_child);
28539 c = die->die_child;
28540 for (i = 0; base_types.iterate (i, &base_type); i++)
28542 base_type->die_mark = 0;
28543 base_type->die_sib = c->die_sib;
28544 c->die_sib = base_type;
28545 c = base_type;
28549 /* Helper function for resolve_addr, attempt to resolve
28550 one CONST_STRING, return true if successful. Similarly verify that
28551 SYMBOL_REFs refer to variables emitted in the current CU. */
28553 static bool
28554 resolve_one_addr (rtx *addr)
28556 rtx rtl = *addr;
28558 if (GET_CODE (rtl) == CONST_STRING)
28560 size_t len = strlen (XSTR (rtl, 0)) + 1;
28561 tree t = build_string (len, XSTR (rtl, 0));
28562 tree tlen = size_int (len - 1);
28563 TREE_TYPE (t)
28564 = build_array_type (char_type_node, build_index_type (tlen));
28565 rtl = lookup_constant_def (t);
28566 if (!rtl || !MEM_P (rtl))
28567 return false;
28568 rtl = XEXP (rtl, 0);
28569 if (GET_CODE (rtl) == SYMBOL_REF
28570 && SYMBOL_REF_DECL (rtl)
28571 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28572 return false;
28573 vec_safe_push (used_rtx_array, rtl);
28574 *addr = rtl;
28575 return true;
28578 if (GET_CODE (rtl) == SYMBOL_REF
28579 && SYMBOL_REF_DECL (rtl))
28581 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28583 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28584 return false;
28586 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28587 return false;
28590 if (GET_CODE (rtl) == CONST)
28592 subrtx_ptr_iterator::array_type array;
28593 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28594 if (!resolve_one_addr (*iter))
28595 return false;
28598 return true;
28601 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28602 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28603 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28605 static rtx
28606 string_cst_pool_decl (tree t)
28608 rtx rtl = output_constant_def (t, 1);
28609 unsigned char *array;
28610 dw_loc_descr_ref l;
28611 tree decl;
28612 size_t len;
28613 dw_die_ref ref;
28615 if (!rtl || !MEM_P (rtl))
28616 return NULL_RTX;
28617 rtl = XEXP (rtl, 0);
28618 if (GET_CODE (rtl) != SYMBOL_REF
28619 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28620 return NULL_RTX;
28622 decl = SYMBOL_REF_DECL (rtl);
28623 if (!lookup_decl_die (decl))
28625 len = TREE_STRING_LENGTH (t);
28626 vec_safe_push (used_rtx_array, rtl);
28627 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28628 array = ggc_vec_alloc<unsigned char> (len);
28629 memcpy (array, TREE_STRING_POINTER (t), len);
28630 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28631 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28632 l->dw_loc_oprnd2.v.val_vec.length = len;
28633 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28634 l->dw_loc_oprnd2.v.val_vec.array = array;
28635 add_AT_loc (ref, DW_AT_location, l);
28636 equate_decl_number_to_die (decl, ref);
28638 return rtl;
28641 /* Helper function of resolve_addr_in_expr. LOC is
28642 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28643 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28644 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28645 with DW_OP_implicit_pointer if possible
28646 and return true, if unsuccessful, return false. */
28648 static bool
28649 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28651 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28652 HOST_WIDE_INT offset = 0;
28653 dw_die_ref ref = NULL;
28654 tree decl;
28656 if (GET_CODE (rtl) == CONST
28657 && GET_CODE (XEXP (rtl, 0)) == PLUS
28658 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28660 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28661 rtl = XEXP (XEXP (rtl, 0), 0);
28663 if (GET_CODE (rtl) == CONST_STRING)
28665 size_t len = strlen (XSTR (rtl, 0)) + 1;
28666 tree t = build_string (len, XSTR (rtl, 0));
28667 tree tlen = size_int (len - 1);
28669 TREE_TYPE (t)
28670 = build_array_type (char_type_node, build_index_type (tlen));
28671 rtl = string_cst_pool_decl (t);
28672 if (!rtl)
28673 return false;
28675 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28677 decl = SYMBOL_REF_DECL (rtl);
28678 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28680 ref = lookup_decl_die (decl);
28681 if (ref && (get_AT (ref, DW_AT_location)
28682 || get_AT (ref, DW_AT_const_value)))
28684 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28685 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28686 loc->dw_loc_oprnd1.val_entry = NULL;
28687 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28688 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28689 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28690 loc->dw_loc_oprnd2.v.val_int = offset;
28691 return true;
28695 return false;
28698 /* Helper function for resolve_addr, handle one location
28699 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28700 the location list couldn't be resolved. */
28702 static bool
28703 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28705 dw_loc_descr_ref keep = NULL;
28706 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28707 switch (loc->dw_loc_opc)
28709 case DW_OP_addr:
28710 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28712 if ((prev == NULL
28713 || prev->dw_loc_opc == DW_OP_piece
28714 || prev->dw_loc_opc == DW_OP_bit_piece)
28715 && loc->dw_loc_next
28716 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28717 && (!dwarf_strict || dwarf_version >= 5)
28718 && optimize_one_addr_into_implicit_ptr (loc))
28719 break;
28720 return false;
28722 break;
28723 case DW_OP_GNU_addr_index:
28724 case DW_OP_GNU_const_index:
28725 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28726 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28728 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28729 if (!resolve_one_addr (&rtl))
28730 return false;
28731 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28732 loc->dw_loc_oprnd1.val_entry
28733 = add_addr_table_entry (rtl, ate_kind_rtx);
28735 break;
28736 case DW_OP_const4u:
28737 case DW_OP_const8u:
28738 if (loc->dtprel
28739 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28740 return false;
28741 break;
28742 case DW_OP_plus_uconst:
28743 if (size_of_loc_descr (loc)
28744 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28746 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28748 dw_loc_descr_ref repl
28749 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28750 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28751 add_loc_descr (&repl, loc->dw_loc_next);
28752 *loc = *repl;
28754 break;
28755 case DW_OP_implicit_value:
28756 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28757 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28758 return false;
28759 break;
28760 case DW_OP_implicit_pointer:
28761 case DW_OP_GNU_implicit_pointer:
28762 case DW_OP_GNU_parameter_ref:
28763 case DW_OP_GNU_variable_value:
28764 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28766 dw_die_ref ref
28767 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28768 if (ref == NULL)
28769 return false;
28770 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28771 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28772 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28774 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28776 if (prev == NULL
28777 && loc->dw_loc_next == NULL
28778 && AT_class (a) == dw_val_class_loc)
28779 switch (a->dw_attr)
28781 /* Following attributes allow both exprloc and reference,
28782 so if the whole expression is DW_OP_GNU_variable_value
28783 alone we could transform it into reference. */
28784 case DW_AT_byte_size:
28785 case DW_AT_bit_size:
28786 case DW_AT_lower_bound:
28787 case DW_AT_upper_bound:
28788 case DW_AT_bit_stride:
28789 case DW_AT_count:
28790 case DW_AT_allocated:
28791 case DW_AT_associated:
28792 case DW_AT_byte_stride:
28793 a->dw_attr_val.val_class = dw_val_class_die_ref;
28794 a->dw_attr_val.val_entry = NULL;
28795 a->dw_attr_val.v.val_die_ref.die
28796 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28797 a->dw_attr_val.v.val_die_ref.external = 0;
28798 return true;
28799 default:
28800 break;
28802 if (dwarf_strict)
28803 return false;
28805 break;
28806 case DW_OP_const_type:
28807 case DW_OP_regval_type:
28808 case DW_OP_deref_type:
28809 case DW_OP_convert:
28810 case DW_OP_reinterpret:
28811 case DW_OP_GNU_const_type:
28812 case DW_OP_GNU_regval_type:
28813 case DW_OP_GNU_deref_type:
28814 case DW_OP_GNU_convert:
28815 case DW_OP_GNU_reinterpret:
28816 while (loc->dw_loc_next
28817 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28818 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28820 dw_die_ref base1, base2;
28821 unsigned enc1, enc2, size1, size2;
28822 if (loc->dw_loc_opc == DW_OP_regval_type
28823 || loc->dw_loc_opc == DW_OP_deref_type
28824 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28825 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28826 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28827 else if (loc->dw_loc_oprnd1.val_class
28828 == dw_val_class_unsigned_const)
28829 break;
28830 else
28831 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28832 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28833 == dw_val_class_unsigned_const)
28834 break;
28835 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28836 gcc_assert (base1->die_tag == DW_TAG_base_type
28837 && base2->die_tag == DW_TAG_base_type);
28838 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28839 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28840 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28841 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28842 if (size1 == size2
28843 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28844 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28845 && loc != keep)
28846 || enc1 == enc2))
28848 /* Optimize away next DW_OP_convert after
28849 adjusting LOC's base type die reference. */
28850 if (loc->dw_loc_opc == DW_OP_regval_type
28851 || loc->dw_loc_opc == DW_OP_deref_type
28852 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28853 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28854 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28855 else
28856 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28857 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28858 continue;
28860 /* Don't change integer DW_OP_convert after e.g. floating
28861 point typed stack entry. */
28862 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28863 keep = loc->dw_loc_next;
28864 break;
28866 break;
28867 default:
28868 break;
28870 return true;
28873 /* Helper function of resolve_addr. DIE had DW_AT_location of
28874 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28875 and DW_OP_addr couldn't be resolved. resolve_addr has already
28876 removed the DW_AT_location attribute. This function attempts to
28877 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28878 to it or DW_AT_const_value attribute, if possible. */
28880 static void
28881 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28883 if (!VAR_P (decl)
28884 || lookup_decl_die (decl) != die
28885 || DECL_EXTERNAL (decl)
28886 || !TREE_STATIC (decl)
28887 || DECL_INITIAL (decl) == NULL_TREE
28888 || DECL_P (DECL_INITIAL (decl))
28889 || get_AT (die, DW_AT_const_value))
28890 return;
28892 tree init = DECL_INITIAL (decl);
28893 HOST_WIDE_INT offset = 0;
28894 /* For variables that have been optimized away and thus
28895 don't have a memory location, see if we can emit
28896 DW_AT_const_value instead. */
28897 if (tree_add_const_value_attribute (die, init))
28898 return;
28899 if (dwarf_strict && dwarf_version < 5)
28900 return;
28901 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28902 and ADDR_EXPR refers to a decl that has DW_AT_location or
28903 DW_AT_const_value (but isn't addressable, otherwise
28904 resolving the original DW_OP_addr wouldn't fail), see if
28905 we can add DW_OP_implicit_pointer. */
28906 STRIP_NOPS (init);
28907 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28908 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28910 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28911 init = TREE_OPERAND (init, 0);
28912 STRIP_NOPS (init);
28914 if (TREE_CODE (init) != ADDR_EXPR)
28915 return;
28916 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28917 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28918 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28919 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28920 && TREE_OPERAND (init, 0) != decl))
28922 dw_die_ref ref;
28923 dw_loc_descr_ref l;
28925 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28927 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28928 if (!rtl)
28929 return;
28930 decl = SYMBOL_REF_DECL (rtl);
28932 else
28933 decl = TREE_OPERAND (init, 0);
28934 ref = lookup_decl_die (decl);
28935 if (ref == NULL
28936 || (!get_AT (ref, DW_AT_location)
28937 && !get_AT (ref, DW_AT_const_value)))
28938 return;
28939 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28940 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28941 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28942 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28943 add_AT_loc (die, DW_AT_location, l);
28947 /* Return NULL if l is a DWARF expression, or first op that is not
28948 valid DWARF expression. */
28950 static dw_loc_descr_ref
28951 non_dwarf_expression (dw_loc_descr_ref l)
28953 while (l)
28955 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28956 return l;
28957 switch (l->dw_loc_opc)
28959 case DW_OP_regx:
28960 case DW_OP_implicit_value:
28961 case DW_OP_stack_value:
28962 case DW_OP_implicit_pointer:
28963 case DW_OP_GNU_implicit_pointer:
28964 case DW_OP_GNU_parameter_ref:
28965 case DW_OP_piece:
28966 case DW_OP_bit_piece:
28967 return l;
28968 default:
28969 break;
28971 l = l->dw_loc_next;
28973 return NULL;
28976 /* Return adjusted copy of EXPR:
28977 If it is empty DWARF expression, return it.
28978 If it is valid non-empty DWARF expression,
28979 return copy of EXPR with DW_OP_deref appended to it.
28980 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28981 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28982 If it is DWARF expression followed by DW_OP_stack_value, return
28983 copy of the DWARF expression without anything appended.
28984 Otherwise, return NULL. */
28986 static dw_loc_descr_ref
28987 copy_deref_exprloc (dw_loc_descr_ref expr)
28989 dw_loc_descr_ref tail = NULL;
28991 if (expr == NULL)
28992 return NULL;
28994 dw_loc_descr_ref l = non_dwarf_expression (expr);
28995 if (l && l->dw_loc_next)
28996 return NULL;
28998 if (l)
29000 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29001 tail = new_loc_descr ((enum dwarf_location_atom)
29002 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
29003 0, 0);
29004 else
29005 switch (l->dw_loc_opc)
29007 case DW_OP_regx:
29008 tail = new_loc_descr (DW_OP_bregx,
29009 l->dw_loc_oprnd1.v.val_unsigned, 0);
29010 break;
29011 case DW_OP_stack_value:
29012 break;
29013 default:
29014 return NULL;
29017 else
29018 tail = new_loc_descr (DW_OP_deref, 0, 0);
29020 dw_loc_descr_ref ret = NULL, *p = &ret;
29021 while (expr != l)
29023 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
29024 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
29025 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
29026 p = &(*p)->dw_loc_next;
29027 expr = expr->dw_loc_next;
29029 *p = tail;
29030 return ret;
29033 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
29034 reference to a variable or argument, adjust it if needed and return:
29035 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
29036 attribute if present should be removed
29037 0 keep the attribute perhaps with minor modifications, no need to rescan
29038 1 if the attribute has been successfully adjusted. */
29040 static int
29041 optimize_string_length (dw_attr_node *a)
29043 dw_loc_descr_ref l = AT_loc (a), lv;
29044 dw_die_ref die;
29045 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29047 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
29048 die = lookup_decl_die (decl);
29049 if (die)
29051 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29052 l->dw_loc_oprnd1.v.val_die_ref.die = die;
29053 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29055 else
29056 return -1;
29058 else
29059 die = l->dw_loc_oprnd1.v.val_die_ref.die;
29061 /* DWARF5 allows reference class, so we can then reference the DIE.
29062 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
29063 if (l->dw_loc_next != NULL && dwarf_version >= 5)
29065 a->dw_attr_val.val_class = dw_val_class_die_ref;
29066 a->dw_attr_val.val_entry = NULL;
29067 a->dw_attr_val.v.val_die_ref.die = die;
29068 a->dw_attr_val.v.val_die_ref.external = 0;
29069 return 0;
29072 dw_attr_node *av = get_AT (die, DW_AT_location);
29073 dw_loc_list_ref d;
29074 bool non_dwarf_expr = false;
29076 if (av == NULL)
29077 return dwarf_strict ? -1 : 0;
29078 switch (AT_class (av))
29080 case dw_val_class_loc_list:
29081 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29082 if (d->expr && non_dwarf_expression (d->expr))
29083 non_dwarf_expr = true;
29084 break;
29085 case dw_val_class_loc:
29086 lv = AT_loc (av);
29087 if (lv == NULL)
29088 return dwarf_strict ? -1 : 0;
29089 if (non_dwarf_expression (lv))
29090 non_dwarf_expr = true;
29091 break;
29092 default:
29093 return dwarf_strict ? -1 : 0;
29096 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
29097 into DW_OP_call4 or DW_OP_GNU_variable_value into
29098 DW_OP_call4 DW_OP_deref, do so. */
29099 if (!non_dwarf_expr
29100 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
29102 l->dw_loc_opc = DW_OP_call4;
29103 if (l->dw_loc_next)
29104 l->dw_loc_next = NULL;
29105 else
29106 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
29107 return 0;
29110 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
29111 copy over the DW_AT_location attribute from die to a. */
29112 if (l->dw_loc_next != NULL)
29114 a->dw_attr_val = av->dw_attr_val;
29115 return 1;
29118 dw_loc_list_ref list, *p;
29119 switch (AT_class (av))
29121 case dw_val_class_loc_list:
29122 p = &list;
29123 list = NULL;
29124 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29126 lv = copy_deref_exprloc (d->expr);
29127 if (lv)
29129 *p = new_loc_list (lv, d->begin, d->end, d->section);
29130 p = &(*p)->dw_loc_next;
29132 else if (!dwarf_strict && d->expr)
29133 return 0;
29135 if (list == NULL)
29136 return dwarf_strict ? -1 : 0;
29137 a->dw_attr_val.val_class = dw_val_class_loc_list;
29138 gen_llsym (list);
29139 *AT_loc_list_ptr (a) = list;
29140 return 1;
29141 case dw_val_class_loc:
29142 lv = copy_deref_exprloc (AT_loc (av));
29143 if (lv == NULL)
29144 return dwarf_strict ? -1 : 0;
29145 a->dw_attr_val.v.val_loc = lv;
29146 return 1;
29147 default:
29148 gcc_unreachable ();
29152 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
29153 an address in .rodata section if the string literal is emitted there,
29154 or remove the containing location list or replace DW_AT_const_value
29155 with DW_AT_location and empty location expression, if it isn't found
29156 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
29157 to something that has been emitted in the current CU. */
29159 static void
29160 resolve_addr (dw_die_ref die)
29162 dw_die_ref c;
29163 dw_attr_node *a;
29164 dw_loc_list_ref *curr, *start, loc;
29165 unsigned ix;
29166 bool remove_AT_byte_size = false;
29168 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29169 switch (AT_class (a))
29171 case dw_val_class_loc_list:
29172 start = curr = AT_loc_list_ptr (a);
29173 loc = *curr;
29174 gcc_assert (loc);
29175 /* The same list can be referenced more than once. See if we have
29176 already recorded the result from a previous pass. */
29177 if (loc->replaced)
29178 *curr = loc->dw_loc_next;
29179 else if (!loc->resolved_addr)
29181 /* As things stand, we do not expect or allow one die to
29182 reference a suffix of another die's location list chain.
29183 References must be identical or completely separate.
29184 There is therefore no need to cache the result of this
29185 pass on any list other than the first; doing so
29186 would lead to unnecessary writes. */
29187 while (*curr)
29189 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29190 if (!resolve_addr_in_expr (a, (*curr)->expr))
29192 dw_loc_list_ref next = (*curr)->dw_loc_next;
29193 dw_loc_descr_ref l = (*curr)->expr;
29195 if (next && (*curr)->ll_symbol)
29197 gcc_assert (!next->ll_symbol);
29198 next->ll_symbol = (*curr)->ll_symbol;
29200 if (dwarf_split_debug_info)
29201 remove_loc_list_addr_table_entries (l);
29202 *curr = next;
29204 else
29206 mark_base_types ((*curr)->expr);
29207 curr = &(*curr)->dw_loc_next;
29210 if (loc == *start)
29211 loc->resolved_addr = 1;
29212 else
29214 loc->replaced = 1;
29215 loc->dw_loc_next = *start;
29218 if (!*start)
29220 remove_AT (die, a->dw_attr);
29221 ix--;
29223 break;
29224 case dw_val_class_loc:
29226 dw_loc_descr_ref l = AT_loc (a);
29227 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29228 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29229 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29230 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29231 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29232 with DW_FORM_ref referencing the same DIE as
29233 DW_OP_GNU_variable_value used to reference. */
29234 if (a->dw_attr == DW_AT_string_length
29235 && l
29236 && l->dw_loc_opc == DW_OP_GNU_variable_value
29237 && (l->dw_loc_next == NULL
29238 || (l->dw_loc_next->dw_loc_next == NULL
29239 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29241 switch (optimize_string_length (a))
29243 case -1:
29244 remove_AT (die, a->dw_attr);
29245 ix--;
29246 /* If we drop DW_AT_string_length, we need to drop also
29247 DW_AT_{string_length_,}byte_size. */
29248 remove_AT_byte_size = true;
29249 continue;
29250 default:
29251 break;
29252 case 1:
29253 /* Even if we keep the optimized DW_AT_string_length,
29254 it might have changed AT_class, so process it again. */
29255 ix--;
29256 continue;
29259 /* For -gdwarf-2 don't attempt to optimize
29260 DW_AT_data_member_location containing
29261 DW_OP_plus_uconst - older consumers might
29262 rely on it being that op instead of a more complex,
29263 but shorter, location description. */
29264 if ((dwarf_version > 2
29265 || a->dw_attr != DW_AT_data_member_location
29266 || l == NULL
29267 || l->dw_loc_opc != DW_OP_plus_uconst
29268 || l->dw_loc_next != NULL)
29269 && !resolve_addr_in_expr (a, l))
29271 if (dwarf_split_debug_info)
29272 remove_loc_list_addr_table_entries (l);
29273 if (l != NULL
29274 && l->dw_loc_next == NULL
29275 && l->dw_loc_opc == DW_OP_addr
29276 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29277 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29278 && a->dw_attr == DW_AT_location)
29280 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29281 remove_AT (die, a->dw_attr);
29282 ix--;
29283 optimize_location_into_implicit_ptr (die, decl);
29284 break;
29286 if (a->dw_attr == DW_AT_string_length)
29287 /* If we drop DW_AT_string_length, we need to drop also
29288 DW_AT_{string_length_,}byte_size. */
29289 remove_AT_byte_size = true;
29290 remove_AT (die, a->dw_attr);
29291 ix--;
29293 else
29294 mark_base_types (l);
29296 break;
29297 case dw_val_class_addr:
29298 if (a->dw_attr == DW_AT_const_value
29299 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29301 if (AT_index (a) != NOT_INDEXED)
29302 remove_addr_table_entry (a->dw_attr_val.val_entry);
29303 remove_AT (die, a->dw_attr);
29304 ix--;
29306 if ((die->die_tag == DW_TAG_call_site
29307 && a->dw_attr == DW_AT_call_origin)
29308 || (die->die_tag == DW_TAG_GNU_call_site
29309 && a->dw_attr == DW_AT_abstract_origin))
29311 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29312 dw_die_ref tdie = lookup_decl_die (tdecl);
29313 dw_die_ref cdie;
29314 if (tdie == NULL
29315 && DECL_EXTERNAL (tdecl)
29316 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29317 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29319 dw_die_ref pdie = cdie;
29320 /* Make sure we don't add these DIEs into type units.
29321 We could emit skeleton DIEs for context (namespaces,
29322 outer structs/classes) and a skeleton DIE for the
29323 innermost context with DW_AT_signature pointing to the
29324 type unit. See PR78835. */
29325 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29326 pdie = pdie->die_parent;
29327 if (pdie == NULL)
29329 /* Creating a full DIE for tdecl is overly expensive and
29330 at this point even wrong when in the LTO phase
29331 as it can end up generating new type DIEs we didn't
29332 output and thus optimize_external_refs will crash. */
29333 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29334 add_AT_flag (tdie, DW_AT_external, 1);
29335 add_AT_flag (tdie, DW_AT_declaration, 1);
29336 add_linkage_attr (tdie, tdecl);
29337 add_name_and_src_coords_attributes (tdie, tdecl);
29338 equate_decl_number_to_die (tdecl, tdie);
29341 if (tdie)
29343 a->dw_attr_val.val_class = dw_val_class_die_ref;
29344 a->dw_attr_val.v.val_die_ref.die = tdie;
29345 a->dw_attr_val.v.val_die_ref.external = 0;
29347 else
29349 if (AT_index (a) != NOT_INDEXED)
29350 remove_addr_table_entry (a->dw_attr_val.val_entry);
29351 remove_AT (die, a->dw_attr);
29352 ix--;
29355 break;
29356 default:
29357 break;
29360 if (remove_AT_byte_size)
29361 remove_AT (die, dwarf_version >= 5
29362 ? DW_AT_string_length_byte_size
29363 : DW_AT_byte_size);
29365 FOR_EACH_CHILD (die, c, resolve_addr (c));
29368 /* Helper routines for optimize_location_lists.
29369 This pass tries to share identical local lists in .debug_loc
29370 section. */
29372 /* Iteratively hash operands of LOC opcode into HSTATE. */
29374 static void
29375 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29377 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29378 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29380 switch (loc->dw_loc_opc)
29382 case DW_OP_const4u:
29383 case DW_OP_const8u:
29384 if (loc->dtprel)
29385 goto hash_addr;
29386 /* FALLTHRU */
29387 case DW_OP_const1u:
29388 case DW_OP_const1s:
29389 case DW_OP_const2u:
29390 case DW_OP_const2s:
29391 case DW_OP_const4s:
29392 case DW_OP_const8s:
29393 case DW_OP_constu:
29394 case DW_OP_consts:
29395 case DW_OP_pick:
29396 case DW_OP_plus_uconst:
29397 case DW_OP_breg0:
29398 case DW_OP_breg1:
29399 case DW_OP_breg2:
29400 case DW_OP_breg3:
29401 case DW_OP_breg4:
29402 case DW_OP_breg5:
29403 case DW_OP_breg6:
29404 case DW_OP_breg7:
29405 case DW_OP_breg8:
29406 case DW_OP_breg9:
29407 case DW_OP_breg10:
29408 case DW_OP_breg11:
29409 case DW_OP_breg12:
29410 case DW_OP_breg13:
29411 case DW_OP_breg14:
29412 case DW_OP_breg15:
29413 case DW_OP_breg16:
29414 case DW_OP_breg17:
29415 case DW_OP_breg18:
29416 case DW_OP_breg19:
29417 case DW_OP_breg20:
29418 case DW_OP_breg21:
29419 case DW_OP_breg22:
29420 case DW_OP_breg23:
29421 case DW_OP_breg24:
29422 case DW_OP_breg25:
29423 case DW_OP_breg26:
29424 case DW_OP_breg27:
29425 case DW_OP_breg28:
29426 case DW_OP_breg29:
29427 case DW_OP_breg30:
29428 case DW_OP_breg31:
29429 case DW_OP_regx:
29430 case DW_OP_fbreg:
29431 case DW_OP_piece:
29432 case DW_OP_deref_size:
29433 case DW_OP_xderef_size:
29434 hstate.add_object (val1->v.val_int);
29435 break;
29436 case DW_OP_skip:
29437 case DW_OP_bra:
29439 int offset;
29441 gcc_assert (val1->val_class == dw_val_class_loc);
29442 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29443 hstate.add_object (offset);
29445 break;
29446 case DW_OP_implicit_value:
29447 hstate.add_object (val1->v.val_unsigned);
29448 switch (val2->val_class)
29450 case dw_val_class_const:
29451 hstate.add_object (val2->v.val_int);
29452 break;
29453 case dw_val_class_vec:
29455 unsigned int elt_size = val2->v.val_vec.elt_size;
29456 unsigned int len = val2->v.val_vec.length;
29458 hstate.add_int (elt_size);
29459 hstate.add_int (len);
29460 hstate.add (val2->v.val_vec.array, len * elt_size);
29462 break;
29463 case dw_val_class_const_double:
29464 hstate.add_object (val2->v.val_double.low);
29465 hstate.add_object (val2->v.val_double.high);
29466 break;
29467 case dw_val_class_wide_int:
29468 hstate.add (val2->v.val_wide->get_val (),
29469 get_full_len (*val2->v.val_wide)
29470 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29471 break;
29472 case dw_val_class_addr:
29473 inchash::add_rtx (val2->v.val_addr, hstate);
29474 break;
29475 default:
29476 gcc_unreachable ();
29478 break;
29479 case DW_OP_bregx:
29480 case DW_OP_bit_piece:
29481 hstate.add_object (val1->v.val_int);
29482 hstate.add_object (val2->v.val_int);
29483 break;
29484 case DW_OP_addr:
29485 hash_addr:
29486 if (loc->dtprel)
29488 unsigned char dtprel = 0xd1;
29489 hstate.add_object (dtprel);
29491 inchash::add_rtx (val1->v.val_addr, hstate);
29492 break;
29493 case DW_OP_GNU_addr_index:
29494 case DW_OP_GNU_const_index:
29496 if (loc->dtprel)
29498 unsigned char dtprel = 0xd1;
29499 hstate.add_object (dtprel);
29501 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29503 break;
29504 case DW_OP_implicit_pointer:
29505 case DW_OP_GNU_implicit_pointer:
29506 hstate.add_int (val2->v.val_int);
29507 break;
29508 case DW_OP_entry_value:
29509 case DW_OP_GNU_entry_value:
29510 hstate.add_object (val1->v.val_loc);
29511 break;
29512 case DW_OP_regval_type:
29513 case DW_OP_deref_type:
29514 case DW_OP_GNU_regval_type:
29515 case DW_OP_GNU_deref_type:
29517 unsigned int byte_size
29518 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29519 unsigned int encoding
29520 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29521 hstate.add_object (val1->v.val_int);
29522 hstate.add_object (byte_size);
29523 hstate.add_object (encoding);
29525 break;
29526 case DW_OP_convert:
29527 case DW_OP_reinterpret:
29528 case DW_OP_GNU_convert:
29529 case DW_OP_GNU_reinterpret:
29530 if (val1->val_class == dw_val_class_unsigned_const)
29532 hstate.add_object (val1->v.val_unsigned);
29533 break;
29535 /* FALLTHRU */
29536 case DW_OP_const_type:
29537 case DW_OP_GNU_const_type:
29539 unsigned int byte_size
29540 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29541 unsigned int encoding
29542 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29543 hstate.add_object (byte_size);
29544 hstate.add_object (encoding);
29545 if (loc->dw_loc_opc != DW_OP_const_type
29546 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29547 break;
29548 hstate.add_object (val2->val_class);
29549 switch (val2->val_class)
29551 case dw_val_class_const:
29552 hstate.add_object (val2->v.val_int);
29553 break;
29554 case dw_val_class_vec:
29556 unsigned int elt_size = val2->v.val_vec.elt_size;
29557 unsigned int len = val2->v.val_vec.length;
29559 hstate.add_object (elt_size);
29560 hstate.add_object (len);
29561 hstate.add (val2->v.val_vec.array, len * elt_size);
29563 break;
29564 case dw_val_class_const_double:
29565 hstate.add_object (val2->v.val_double.low);
29566 hstate.add_object (val2->v.val_double.high);
29567 break;
29568 case dw_val_class_wide_int:
29569 hstate.add (val2->v.val_wide->get_val (),
29570 get_full_len (*val2->v.val_wide)
29571 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29572 break;
29573 default:
29574 gcc_unreachable ();
29577 break;
29579 default:
29580 /* Other codes have no operands. */
29581 break;
29585 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29587 static inline void
29588 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29590 dw_loc_descr_ref l;
29591 bool sizes_computed = false;
29592 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29593 size_of_locs (loc);
29595 for (l = loc; l != NULL; l = l->dw_loc_next)
29597 enum dwarf_location_atom opc = l->dw_loc_opc;
29598 hstate.add_object (opc);
29599 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29601 size_of_locs (loc);
29602 sizes_computed = true;
29604 hash_loc_operands (l, hstate);
29608 /* Compute hash of the whole location list LIST_HEAD. */
29610 static inline void
29611 hash_loc_list (dw_loc_list_ref list_head)
29613 dw_loc_list_ref curr = list_head;
29614 inchash::hash hstate;
29616 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29618 hstate.add (curr->begin, strlen (curr->begin) + 1);
29619 hstate.add (curr->end, strlen (curr->end) + 1);
29620 if (curr->section)
29621 hstate.add (curr->section, strlen (curr->section) + 1);
29622 hash_locs (curr->expr, hstate);
29624 list_head->hash = hstate.end ();
29627 /* Return true if X and Y opcodes have the same operands. */
29629 static inline bool
29630 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29632 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29633 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29634 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29635 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29637 switch (x->dw_loc_opc)
29639 case DW_OP_const4u:
29640 case DW_OP_const8u:
29641 if (x->dtprel)
29642 goto hash_addr;
29643 /* FALLTHRU */
29644 case DW_OP_const1u:
29645 case DW_OP_const1s:
29646 case DW_OP_const2u:
29647 case DW_OP_const2s:
29648 case DW_OP_const4s:
29649 case DW_OP_const8s:
29650 case DW_OP_constu:
29651 case DW_OP_consts:
29652 case DW_OP_pick:
29653 case DW_OP_plus_uconst:
29654 case DW_OP_breg0:
29655 case DW_OP_breg1:
29656 case DW_OP_breg2:
29657 case DW_OP_breg3:
29658 case DW_OP_breg4:
29659 case DW_OP_breg5:
29660 case DW_OP_breg6:
29661 case DW_OP_breg7:
29662 case DW_OP_breg8:
29663 case DW_OP_breg9:
29664 case DW_OP_breg10:
29665 case DW_OP_breg11:
29666 case DW_OP_breg12:
29667 case DW_OP_breg13:
29668 case DW_OP_breg14:
29669 case DW_OP_breg15:
29670 case DW_OP_breg16:
29671 case DW_OP_breg17:
29672 case DW_OP_breg18:
29673 case DW_OP_breg19:
29674 case DW_OP_breg20:
29675 case DW_OP_breg21:
29676 case DW_OP_breg22:
29677 case DW_OP_breg23:
29678 case DW_OP_breg24:
29679 case DW_OP_breg25:
29680 case DW_OP_breg26:
29681 case DW_OP_breg27:
29682 case DW_OP_breg28:
29683 case DW_OP_breg29:
29684 case DW_OP_breg30:
29685 case DW_OP_breg31:
29686 case DW_OP_regx:
29687 case DW_OP_fbreg:
29688 case DW_OP_piece:
29689 case DW_OP_deref_size:
29690 case DW_OP_xderef_size:
29691 return valx1->v.val_int == valy1->v.val_int;
29692 case DW_OP_skip:
29693 case DW_OP_bra:
29694 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29695 can cause irrelevant differences in dw_loc_addr. */
29696 gcc_assert (valx1->val_class == dw_val_class_loc
29697 && valy1->val_class == dw_val_class_loc
29698 && (dwarf_split_debug_info
29699 || x->dw_loc_addr == y->dw_loc_addr));
29700 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29701 case DW_OP_implicit_value:
29702 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29703 || valx2->val_class != valy2->val_class)
29704 return false;
29705 switch (valx2->val_class)
29707 case dw_val_class_const:
29708 return valx2->v.val_int == valy2->v.val_int;
29709 case dw_val_class_vec:
29710 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29711 && valx2->v.val_vec.length == valy2->v.val_vec.length
29712 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29713 valx2->v.val_vec.elt_size
29714 * valx2->v.val_vec.length) == 0;
29715 case dw_val_class_const_double:
29716 return valx2->v.val_double.low == valy2->v.val_double.low
29717 && valx2->v.val_double.high == valy2->v.val_double.high;
29718 case dw_val_class_wide_int:
29719 return *valx2->v.val_wide == *valy2->v.val_wide;
29720 case dw_val_class_addr:
29721 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29722 default:
29723 gcc_unreachable ();
29725 case DW_OP_bregx:
29726 case DW_OP_bit_piece:
29727 return valx1->v.val_int == valy1->v.val_int
29728 && valx2->v.val_int == valy2->v.val_int;
29729 case DW_OP_addr:
29730 hash_addr:
29731 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29732 case DW_OP_GNU_addr_index:
29733 case DW_OP_GNU_const_index:
29735 rtx ax1 = valx1->val_entry->addr.rtl;
29736 rtx ay1 = valy1->val_entry->addr.rtl;
29737 return rtx_equal_p (ax1, ay1);
29739 case DW_OP_implicit_pointer:
29740 case DW_OP_GNU_implicit_pointer:
29741 return valx1->val_class == dw_val_class_die_ref
29742 && valx1->val_class == valy1->val_class
29743 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29744 && valx2->v.val_int == valy2->v.val_int;
29745 case DW_OP_entry_value:
29746 case DW_OP_GNU_entry_value:
29747 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29748 case DW_OP_const_type:
29749 case DW_OP_GNU_const_type:
29750 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29751 || valx2->val_class != valy2->val_class)
29752 return false;
29753 switch (valx2->val_class)
29755 case dw_val_class_const:
29756 return valx2->v.val_int == valy2->v.val_int;
29757 case dw_val_class_vec:
29758 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29759 && valx2->v.val_vec.length == valy2->v.val_vec.length
29760 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29761 valx2->v.val_vec.elt_size
29762 * valx2->v.val_vec.length) == 0;
29763 case dw_val_class_const_double:
29764 return valx2->v.val_double.low == valy2->v.val_double.low
29765 && valx2->v.val_double.high == valy2->v.val_double.high;
29766 case dw_val_class_wide_int:
29767 return *valx2->v.val_wide == *valy2->v.val_wide;
29768 default:
29769 gcc_unreachable ();
29771 case DW_OP_regval_type:
29772 case DW_OP_deref_type:
29773 case DW_OP_GNU_regval_type:
29774 case DW_OP_GNU_deref_type:
29775 return valx1->v.val_int == valy1->v.val_int
29776 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29777 case DW_OP_convert:
29778 case DW_OP_reinterpret:
29779 case DW_OP_GNU_convert:
29780 case DW_OP_GNU_reinterpret:
29781 if (valx1->val_class != valy1->val_class)
29782 return false;
29783 if (valx1->val_class == dw_val_class_unsigned_const)
29784 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29785 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29786 case DW_OP_GNU_parameter_ref:
29787 return valx1->val_class == dw_val_class_die_ref
29788 && valx1->val_class == valy1->val_class
29789 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29790 default:
29791 /* Other codes have no operands. */
29792 return true;
29796 /* Return true if DWARF location expressions X and Y are the same. */
29798 static inline bool
29799 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29801 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29802 if (x->dw_loc_opc != y->dw_loc_opc
29803 || x->dtprel != y->dtprel
29804 || !compare_loc_operands (x, y))
29805 break;
29806 return x == NULL && y == NULL;
29809 /* Hashtable helpers. */
29811 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29813 static inline hashval_t hash (const dw_loc_list_struct *);
29814 static inline bool equal (const dw_loc_list_struct *,
29815 const dw_loc_list_struct *);
29818 /* Return precomputed hash of location list X. */
29820 inline hashval_t
29821 loc_list_hasher::hash (const dw_loc_list_struct *x)
29823 return x->hash;
29826 /* Return true if location lists A and B are the same. */
29828 inline bool
29829 loc_list_hasher::equal (const dw_loc_list_struct *a,
29830 const dw_loc_list_struct *b)
29832 if (a == b)
29833 return 1;
29834 if (a->hash != b->hash)
29835 return 0;
29836 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29837 if (strcmp (a->begin, b->begin) != 0
29838 || strcmp (a->end, b->end) != 0
29839 || (a->section == NULL) != (b->section == NULL)
29840 || (a->section && strcmp (a->section, b->section) != 0)
29841 || !compare_locs (a->expr, b->expr))
29842 break;
29843 return a == NULL && b == NULL;
29846 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29849 /* Recursively optimize location lists referenced from DIE
29850 children and share them whenever possible. */
29852 static void
29853 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29855 dw_die_ref c;
29856 dw_attr_node *a;
29857 unsigned ix;
29858 dw_loc_list_struct **slot;
29860 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29861 if (AT_class (a) == dw_val_class_loc_list)
29863 dw_loc_list_ref list = AT_loc_list (a);
29864 /* TODO: perform some optimizations here, before hashing
29865 it and storing into the hash table. */
29866 hash_loc_list (list);
29867 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29868 if (*slot == NULL)
29869 *slot = list;
29870 else
29871 a->dw_attr_val.v.val_loc_list = *slot;
29874 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29878 /* Recursively assign each location list a unique index into the debug_addr
29879 section. */
29881 static void
29882 index_location_lists (dw_die_ref die)
29884 dw_die_ref c;
29885 dw_attr_node *a;
29886 unsigned ix;
29888 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29889 if (AT_class (a) == dw_val_class_loc_list)
29891 dw_loc_list_ref list = AT_loc_list (a);
29892 dw_loc_list_ref curr;
29893 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29895 /* Don't index an entry that has already been indexed
29896 or won't be output. */
29897 if (curr->begin_entry != NULL
29898 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29899 continue;
29901 curr->begin_entry
29902 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29906 FOR_EACH_CHILD (die, c, index_location_lists (c));
29909 /* Optimize location lists referenced from DIE
29910 children and share them whenever possible. */
29912 static void
29913 optimize_location_lists (dw_die_ref die)
29915 loc_list_hash_type htab (500);
29916 optimize_location_lists_1 (die, &htab);
29919 /* Traverse the limbo die list, and add parent/child links. The only
29920 dies without parents that should be here are concrete instances of
29921 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29922 For concrete instances, we can get the parent die from the abstract
29923 instance. */
29925 static void
29926 flush_limbo_die_list (void)
29928 limbo_die_node *node;
29930 /* get_context_die calls force_decl_die, which can put new DIEs on the
29931 limbo list in LTO mode when nested functions are put in a different
29932 partition than that of their parent function. */
29933 while ((node = limbo_die_list))
29935 dw_die_ref die = node->die;
29936 limbo_die_list = node->next;
29938 if (die->die_parent == NULL)
29940 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29942 if (origin && origin->die_parent)
29943 add_child_die (origin->die_parent, die);
29944 else if (is_cu_die (die))
29946 else if (seen_error ())
29947 /* It's OK to be confused by errors in the input. */
29948 add_child_die (comp_unit_die (), die);
29949 else
29951 /* In certain situations, the lexical block containing a
29952 nested function can be optimized away, which results
29953 in the nested function die being orphaned. Likewise
29954 with the return type of that nested function. Force
29955 this to be a child of the containing function.
29957 It may happen that even the containing function got fully
29958 inlined and optimized out. In that case we are lost and
29959 assign the empty child. This should not be big issue as
29960 the function is likely unreachable too. */
29961 gcc_assert (node->created_for);
29963 if (DECL_P (node->created_for))
29964 origin = get_context_die (DECL_CONTEXT (node->created_for));
29965 else if (TYPE_P (node->created_for))
29966 origin = scope_die_for (node->created_for, comp_unit_die ());
29967 else
29968 origin = comp_unit_die ();
29970 add_child_die (origin, die);
29976 /* Reset DIEs so we can output them again. */
29978 static void
29979 reset_dies (dw_die_ref die)
29981 dw_die_ref c;
29983 /* Remove stuff we re-generate. */
29984 die->die_mark = 0;
29985 die->die_offset = 0;
29986 die->die_abbrev = 0;
29987 remove_AT (die, DW_AT_sibling);
29989 FOR_EACH_CHILD (die, c, reset_dies (c));
29992 /* Output stuff that dwarf requires at the end of every file,
29993 and generate the DWARF-2 debugging info. */
29995 static void
29996 dwarf2out_finish (const char *)
29998 comdat_type_node *ctnode;
29999 dw_die_ref main_comp_unit_die;
30000 unsigned char checksum[16];
30001 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
30003 /* Flush out any latecomers to the limbo party. */
30004 flush_limbo_die_list ();
30006 if (flag_checking)
30008 verify_die (comp_unit_die ());
30009 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30010 verify_die (node->die);
30013 /* We shouldn't have any symbols with delayed asm names for
30014 DIEs generated after early finish. */
30015 gcc_assert (deferred_asm_name == NULL);
30017 gen_remaining_tmpl_value_param_die_attribute ();
30019 if (flag_generate_lto)
30021 gcc_assert (flag_fat_lto_objects);
30023 /* Prune stuff so that dwarf2out_finish runs successfully
30024 for the fat part of the object. */
30025 reset_dies (comp_unit_die ());
30026 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30027 reset_dies (node->die);
30029 hash_table<comdat_type_hasher> comdat_type_table (100);
30030 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30032 comdat_type_node **slot
30033 = comdat_type_table.find_slot (ctnode, INSERT);
30035 /* Don't reset types twice. */
30036 if (*slot != HTAB_EMPTY_ENTRY)
30037 continue;
30039 /* Add a pointer to the line table for the main compilation unit
30040 so that the debugger can make sense of DW_AT_decl_file
30041 attributes. */
30042 if (debug_info_level >= DINFO_LEVEL_TERSE)
30043 reset_dies (ctnode->root_die);
30045 *slot = ctnode;
30048 /* Reset die CU symbol so we don't output it twice. */
30049 comp_unit_die ()->die_id.die_symbol = NULL;
30051 /* Remove DW_AT_macro from the early output. */
30052 if (have_macinfo)
30053 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
30055 /* Remove indirect string decisions. */
30056 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
30059 #if ENABLE_ASSERT_CHECKING
30061 dw_die_ref die = comp_unit_die (), c;
30062 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
30064 #endif
30065 resolve_addr (comp_unit_die ());
30066 move_marked_base_types ();
30068 /* Initialize sections and labels used for actual assembler output. */
30069 init_sections_and_labels (false);
30071 /* Traverse the DIE's and add sibling attributes to those DIE's that
30072 have children. */
30073 add_sibling_attributes (comp_unit_die ());
30074 limbo_die_node *node;
30075 for (node = cu_die_list; node; node = node->next)
30076 add_sibling_attributes (node->die);
30077 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30078 add_sibling_attributes (ctnode->root_die);
30080 /* When splitting DWARF info, we put some attributes in the
30081 skeleton compile_unit DIE that remains in the .o, while
30082 most attributes go in the DWO compile_unit_die. */
30083 if (dwarf_split_debug_info)
30085 limbo_die_node *cu;
30086 main_comp_unit_die = gen_compile_unit_die (NULL);
30087 if (dwarf_version >= 5)
30088 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
30089 cu = limbo_die_list;
30090 gcc_assert (cu->die == main_comp_unit_die);
30091 limbo_die_list = limbo_die_list->next;
30092 cu->next = cu_die_list;
30093 cu_die_list = cu;
30095 else
30096 main_comp_unit_die = comp_unit_die ();
30098 /* Output a terminator label for the .text section. */
30099 switch_to_section (text_section);
30100 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
30101 if (cold_text_section)
30103 switch_to_section (cold_text_section);
30104 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
30107 /* We can only use the low/high_pc attributes if all of the code was
30108 in .text. */
30109 if (!have_multiple_function_sections
30110 || (dwarf_version < 3 && dwarf_strict))
30112 /* Don't add if the CU has no associated code. */
30113 if (text_section_used)
30114 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
30115 text_end_label, true);
30117 else
30119 unsigned fde_idx;
30120 dw_fde_ref fde;
30121 bool range_list_added = false;
30123 if (text_section_used)
30124 add_ranges_by_labels (main_comp_unit_die, text_section_label,
30125 text_end_label, &range_list_added, true);
30126 if (cold_text_section_used)
30127 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
30128 cold_end_label, &range_list_added, true);
30130 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
30132 if (DECL_IGNORED_P (fde->decl))
30133 continue;
30134 if (!fde->in_std_section)
30135 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
30136 fde->dw_fde_end, &range_list_added,
30137 true);
30138 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
30139 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
30140 fde->dw_fde_second_end, &range_list_added,
30141 true);
30144 if (range_list_added)
30146 /* We need to give .debug_loc and .debug_ranges an appropriate
30147 "base address". Use zero so that these addresses become
30148 absolute. Historically, we've emitted the unexpected
30149 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
30150 Emit both to give time for other tools to adapt. */
30151 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
30152 if (! dwarf_strict && dwarf_version < 4)
30153 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
30155 add_ranges (NULL);
30159 /* AIX Assembler inserts the length, so adjust the reference to match the
30160 offset expected by debuggers. */
30161 strcpy (dl_section_ref, debug_line_section_label);
30162 if (XCOFF_DEBUGGING_INFO)
30163 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
30165 if (debug_info_level >= DINFO_LEVEL_TERSE)
30166 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
30167 dl_section_ref);
30169 if (have_macinfo)
30170 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30171 macinfo_section_label);
30173 if (dwarf_split_debug_info)
30175 if (have_location_lists)
30177 if (dwarf_version >= 5)
30178 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30179 loc_section_label);
30180 /* optimize_location_lists calculates the size of the lists,
30181 so index them first, and assign indices to the entries.
30182 Although optimize_location_lists will remove entries from
30183 the table, it only does so for duplicates, and therefore
30184 only reduces ref_counts to 1. */
30185 index_location_lists (comp_unit_die ());
30188 if (addr_index_table != NULL)
30190 unsigned int index = 0;
30191 addr_index_table
30192 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30193 (&index);
30197 loc_list_idx = 0;
30198 if (have_location_lists)
30200 optimize_location_lists (comp_unit_die ());
30201 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30202 if (dwarf_version >= 5 && dwarf_split_debug_info)
30203 assign_location_list_indexes (comp_unit_die ());
30206 save_macinfo_strings ();
30208 if (dwarf_split_debug_info)
30210 unsigned int index = 0;
30212 /* Add attributes common to skeleton compile_units and
30213 type_units. Because these attributes include strings, it
30214 must be done before freezing the string table. Top-level
30215 skeleton die attrs are added when the skeleton type unit is
30216 created, so ensure it is created by this point. */
30217 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30218 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30221 /* Output all of the compilation units. We put the main one last so that
30222 the offsets are available to output_pubnames. */
30223 for (node = cu_die_list; node; node = node->next)
30224 output_comp_unit (node->die, 0, NULL);
30226 hash_table<comdat_type_hasher> comdat_type_table (100);
30227 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30229 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30231 /* Don't output duplicate types. */
30232 if (*slot != HTAB_EMPTY_ENTRY)
30233 continue;
30235 /* Add a pointer to the line table for the main compilation unit
30236 so that the debugger can make sense of DW_AT_decl_file
30237 attributes. */
30238 if (debug_info_level >= DINFO_LEVEL_TERSE)
30239 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30240 (!dwarf_split_debug_info
30241 ? dl_section_ref
30242 : debug_skeleton_line_section_label));
30244 output_comdat_type_unit (ctnode);
30245 *slot = ctnode;
30248 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30249 both the main_cu and all skeleton TUs. Making this call unconditional
30250 would end up either adding a second copy of the AT_pubnames attribute, or
30251 requiring a special case in add_top_level_skeleton_die_attrs. */
30252 if (!dwarf_split_debug_info)
30253 add_AT_pubnames (comp_unit_die ());
30255 if (dwarf_split_debug_info)
30257 int mark;
30258 struct md5_ctx ctx;
30260 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30261 index_rnglists ();
30263 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30264 md5_init_ctx (&ctx);
30265 mark = 0;
30266 die_checksum (comp_unit_die (), &ctx, &mark);
30267 unmark_all_dies (comp_unit_die ());
30268 md5_finish_ctx (&ctx, checksum);
30270 if (dwarf_version < 5)
30272 /* Use the first 8 bytes of the checksum as the dwo_id,
30273 and add it to both comp-unit DIEs. */
30274 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30275 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30278 /* Add the base offset of the ranges table to the skeleton
30279 comp-unit DIE. */
30280 if (!vec_safe_is_empty (ranges_table))
30282 if (dwarf_version >= 5)
30283 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30284 ranges_base_label);
30285 else
30286 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30287 ranges_section_label);
30290 switch_to_section (debug_addr_section);
30291 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30292 output_addr_table ();
30295 /* Output the main compilation unit if non-empty or if .debug_macinfo
30296 or .debug_macro will be emitted. */
30297 output_comp_unit (comp_unit_die (), have_macinfo,
30298 dwarf_split_debug_info ? checksum : NULL);
30300 if (dwarf_split_debug_info && info_section_emitted)
30301 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30303 /* Output the abbreviation table. */
30304 if (vec_safe_length (abbrev_die_table) != 1)
30306 switch_to_section (debug_abbrev_section);
30307 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30308 output_abbrev_section ();
30311 /* Output location list section if necessary. */
30312 if (have_location_lists)
30314 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30315 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30316 /* Output the location lists info. */
30317 switch_to_section (debug_loc_section);
30318 if (dwarf_version >= 5)
30320 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30321 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30322 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30323 dw2_asm_output_data (4, 0xffffffff,
30324 "Initial length escape value indicating "
30325 "64-bit DWARF extension");
30326 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30327 "Length of Location Lists");
30328 ASM_OUTPUT_LABEL (asm_out_file, l1);
30329 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30330 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30331 dw2_asm_output_data (1, 0, "Segment Size");
30332 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30333 "Offset Entry Count");
30335 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30336 if (dwarf_version >= 5 && dwarf_split_debug_info)
30338 unsigned int save_loc_list_idx = loc_list_idx;
30339 loc_list_idx = 0;
30340 output_loclists_offsets (comp_unit_die ());
30341 gcc_assert (save_loc_list_idx == loc_list_idx);
30343 output_location_lists (comp_unit_die ());
30344 if (dwarf_version >= 5)
30345 ASM_OUTPUT_LABEL (asm_out_file, l2);
30348 output_pubtables ();
30350 /* Output the address range information if a CU (.debug_info section)
30351 was emitted. We output an empty table even if we had no functions
30352 to put in it. This because the consumer has no way to tell the
30353 difference between an empty table that we omitted and failure to
30354 generate a table that would have contained data. */
30355 if (info_section_emitted)
30357 switch_to_section (debug_aranges_section);
30358 output_aranges ();
30361 /* Output ranges section if necessary. */
30362 if (!vec_safe_is_empty (ranges_table))
30364 if (dwarf_version >= 5)
30365 output_rnglists ();
30366 else
30367 output_ranges ();
30370 /* Have to end the macro section. */
30371 if (have_macinfo)
30373 switch_to_section (debug_macinfo_section);
30374 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30375 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30376 : debug_skeleton_line_section_label, false);
30377 dw2_asm_output_data (1, 0, "End compilation unit");
30380 /* Output the source line correspondence table. We must do this
30381 even if there is no line information. Otherwise, on an empty
30382 translation unit, we will generate a present, but empty,
30383 .debug_info section. IRIX 6.5 `nm' will then complain when
30384 examining the file. This is done late so that any filenames
30385 used by the debug_info section are marked as 'used'. */
30386 switch_to_section (debug_line_section);
30387 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30388 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30389 output_line_info (false);
30391 if (dwarf_split_debug_info && info_section_emitted)
30393 switch_to_section (debug_skeleton_line_section);
30394 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30395 output_line_info (true);
30398 /* If we emitted any indirect strings, output the string table too. */
30399 if (debug_str_hash || skeleton_debug_str_hash)
30400 output_indirect_strings ();
30401 if (debug_line_str_hash)
30403 switch_to_section (debug_line_str_section);
30404 const enum dwarf_form form = DW_FORM_line_strp;
30405 debug_line_str_hash->traverse<enum dwarf_form,
30406 output_indirect_string> (form);
30410 /* Returns a hash value for X (which really is a variable_value_struct). */
30412 inline hashval_t
30413 variable_value_hasher::hash (variable_value_struct *x)
30415 return (hashval_t) x->decl_id;
30418 /* Return nonzero if decl_id of variable_value_struct X is the same as
30419 UID of decl Y. */
30421 inline bool
30422 variable_value_hasher::equal (variable_value_struct *x, tree y)
30424 return x->decl_id == DECL_UID (y);
30427 /* Helper function for resolve_variable_value, handle
30428 DW_OP_GNU_variable_value in one location expression.
30429 Return true if exprloc has been changed into loclist. */
30431 static bool
30432 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30434 dw_loc_descr_ref next;
30435 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30437 next = loc->dw_loc_next;
30438 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30439 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30440 continue;
30442 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30443 if (DECL_CONTEXT (decl) != current_function_decl)
30444 continue;
30446 dw_die_ref ref = lookup_decl_die (decl);
30447 if (ref)
30449 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30450 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30451 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30452 continue;
30454 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30455 if (l == NULL)
30456 continue;
30457 if (l->dw_loc_next)
30459 if (AT_class (a) != dw_val_class_loc)
30460 continue;
30461 switch (a->dw_attr)
30463 /* Following attributes allow both exprloc and loclist
30464 classes, so we can change them into a loclist. */
30465 case DW_AT_location:
30466 case DW_AT_string_length:
30467 case DW_AT_return_addr:
30468 case DW_AT_data_member_location:
30469 case DW_AT_frame_base:
30470 case DW_AT_segment:
30471 case DW_AT_static_link:
30472 case DW_AT_use_location:
30473 case DW_AT_vtable_elem_location:
30474 if (prev)
30476 prev->dw_loc_next = NULL;
30477 prepend_loc_descr_to_each (l, AT_loc (a));
30479 if (next)
30480 add_loc_descr_to_each (l, next);
30481 a->dw_attr_val.val_class = dw_val_class_loc_list;
30482 a->dw_attr_val.val_entry = NULL;
30483 a->dw_attr_val.v.val_loc_list = l;
30484 have_location_lists = true;
30485 return true;
30486 /* Following attributes allow both exprloc and reference,
30487 so if the whole expression is DW_OP_GNU_variable_value alone
30488 we could transform it into reference. */
30489 case DW_AT_byte_size:
30490 case DW_AT_bit_size:
30491 case DW_AT_lower_bound:
30492 case DW_AT_upper_bound:
30493 case DW_AT_bit_stride:
30494 case DW_AT_count:
30495 case DW_AT_allocated:
30496 case DW_AT_associated:
30497 case DW_AT_byte_stride:
30498 if (prev == NULL && next == NULL)
30499 break;
30500 /* FALLTHRU */
30501 default:
30502 if (dwarf_strict)
30503 continue;
30504 break;
30506 /* Create DW_TAG_variable that we can refer to. */
30507 gen_decl_die (decl, NULL_TREE, NULL,
30508 lookup_decl_die (current_function_decl));
30509 ref = lookup_decl_die (decl);
30510 if (ref)
30512 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30513 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30514 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30516 continue;
30518 if (prev)
30520 prev->dw_loc_next = l->expr;
30521 add_loc_descr (&prev->dw_loc_next, next);
30522 free_loc_descr (loc, NULL);
30523 next = prev->dw_loc_next;
30525 else
30527 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30528 add_loc_descr (&loc, next);
30529 next = loc;
30531 loc = prev;
30533 return false;
30536 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30538 static void
30539 resolve_variable_value (dw_die_ref die)
30541 dw_attr_node *a;
30542 dw_loc_list_ref loc;
30543 unsigned ix;
30545 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30546 switch (AT_class (a))
30548 case dw_val_class_loc:
30549 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30550 break;
30551 /* FALLTHRU */
30552 case dw_val_class_loc_list:
30553 loc = AT_loc_list (a);
30554 gcc_assert (loc);
30555 for (; loc; loc = loc->dw_loc_next)
30556 resolve_variable_value_in_expr (a, loc->expr);
30557 break;
30558 default:
30559 break;
30563 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30564 temporaries in the current function. */
30566 static void
30567 resolve_variable_values (void)
30569 if (!variable_value_hash || !current_function_decl)
30570 return;
30572 struct variable_value_struct *node
30573 = variable_value_hash->find_with_hash (current_function_decl,
30574 DECL_UID (current_function_decl));
30576 if (node == NULL)
30577 return;
30579 unsigned int i;
30580 dw_die_ref die;
30581 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30582 resolve_variable_value (die);
30585 /* Helper function for note_variable_value, handle one location
30586 expression. */
30588 static void
30589 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30591 for (; loc; loc = loc->dw_loc_next)
30592 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30593 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30595 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30596 dw_die_ref ref = lookup_decl_die (decl);
30597 if (! ref && flag_generate_lto)
30599 /* ??? This is somewhat a hack because we do not create DIEs
30600 for variables not in BLOCK trees early but when generating
30601 early LTO output we need the dw_val_class_decl_ref to be
30602 fully resolved. For fat LTO objects we'd also like to
30603 undo this after LTO dwarf output. */
30604 gcc_assert (DECL_CONTEXT (decl));
30605 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30606 gcc_assert (ctx != NULL);
30607 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30608 ref = lookup_decl_die (decl);
30609 gcc_assert (ref != NULL);
30611 if (ref)
30613 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30614 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30615 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30616 continue;
30618 if (VAR_P (decl)
30619 && DECL_CONTEXT (decl)
30620 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30621 && lookup_decl_die (DECL_CONTEXT (decl)))
30623 if (!variable_value_hash)
30624 variable_value_hash
30625 = hash_table<variable_value_hasher>::create_ggc (10);
30627 tree fndecl = DECL_CONTEXT (decl);
30628 struct variable_value_struct *node;
30629 struct variable_value_struct **slot
30630 = variable_value_hash->find_slot_with_hash (fndecl,
30631 DECL_UID (fndecl),
30632 INSERT);
30633 if (*slot == NULL)
30635 node = ggc_cleared_alloc<variable_value_struct> ();
30636 node->decl_id = DECL_UID (fndecl);
30637 *slot = node;
30639 else
30640 node = *slot;
30642 vec_safe_push (node->dies, die);
30647 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30648 with dw_val_class_decl_ref operand. */
30650 static void
30651 note_variable_value (dw_die_ref die)
30653 dw_die_ref c;
30654 dw_attr_node *a;
30655 dw_loc_list_ref loc;
30656 unsigned ix;
30658 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30659 switch (AT_class (a))
30661 case dw_val_class_loc_list:
30662 loc = AT_loc_list (a);
30663 gcc_assert (loc);
30664 if (!loc->noted_variable_value)
30666 loc->noted_variable_value = 1;
30667 for (; loc; loc = loc->dw_loc_next)
30668 note_variable_value_in_expr (die, loc->expr);
30670 break;
30671 case dw_val_class_loc:
30672 note_variable_value_in_expr (die, AT_loc (a));
30673 break;
30674 default:
30675 break;
30678 /* Mark children. */
30679 FOR_EACH_CHILD (die, c, note_variable_value (c));
30682 /* Perform any cleanups needed after the early debug generation pass
30683 has run. */
30685 static void
30686 dwarf2out_early_finish (const char *filename)
30688 set_early_dwarf s;
30690 /* PCH might result in DW_AT_producer string being restored from the
30691 header compilation, so always fill it with empty string initially
30692 and overwrite only here. */
30693 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30694 producer_string = gen_producer_string ();
30695 producer->dw_attr_val.v.val_str->refcount--;
30696 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30698 /* Add the name for the main input file now. We delayed this from
30699 dwarf2out_init to avoid complications with PCH. */
30700 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30701 add_comp_dir_attribute (comp_unit_die ());
30703 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30704 DW_AT_comp_dir into .debug_line_str section. */
30705 if (!DWARF2_ASM_LINE_DEBUG_INFO
30706 && dwarf_version >= 5
30707 && DWARF5_USE_DEBUG_LINE_STR)
30709 for (int i = 0; i < 2; i++)
30711 dw_attr_node *a = get_AT (comp_unit_die (),
30712 i ? DW_AT_comp_dir : DW_AT_name);
30713 if (a == NULL
30714 || AT_class (a) != dw_val_class_str
30715 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30716 continue;
30718 if (! debug_line_str_hash)
30719 debug_line_str_hash
30720 = hash_table<indirect_string_hasher>::create_ggc (10);
30722 struct indirect_string_node *node
30723 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30724 set_indirect_string (node);
30725 node->form = DW_FORM_line_strp;
30726 a->dw_attr_val.v.val_str->refcount--;
30727 a->dw_attr_val.v.val_str = node;
30731 /* With LTO early dwarf was really finished at compile-time, so make
30732 sure to adjust the phase after annotating the LTRANS CU DIE. */
30733 if (in_lto_p)
30735 early_dwarf_finished = true;
30736 return;
30739 /* Walk through the list of incomplete types again, trying once more to
30740 emit full debugging info for them. */
30741 retry_incomplete_types ();
30743 /* The point here is to flush out the limbo list so that it is empty
30744 and we don't need to stream it for LTO. */
30745 flush_limbo_die_list ();
30747 gen_scheduled_generic_parms_dies ();
30748 gen_remaining_tmpl_value_param_die_attribute ();
30750 /* Add DW_AT_linkage_name for all deferred DIEs. */
30751 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30753 tree decl = node->created_for;
30754 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30755 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30756 ended up in deferred_asm_name before we knew it was
30757 constant and never written to disk. */
30758 && DECL_ASSEMBLER_NAME (decl))
30760 add_linkage_attr (node->die, decl);
30761 move_linkage_attr (node->die);
30764 deferred_asm_name = NULL;
30766 if (flag_eliminate_unused_debug_types)
30767 prune_unused_types ();
30769 /* Generate separate COMDAT sections for type DIEs. */
30770 if (use_debug_types)
30772 break_out_comdat_types (comp_unit_die ());
30774 /* Each new type_unit DIE was added to the limbo die list when created.
30775 Since these have all been added to comdat_type_list, clear the
30776 limbo die list. */
30777 limbo_die_list = NULL;
30779 /* For each new comdat type unit, copy declarations for incomplete
30780 types to make the new unit self-contained (i.e., no direct
30781 references to the main compile unit). */
30782 for (comdat_type_node *ctnode = comdat_type_list;
30783 ctnode != NULL; ctnode = ctnode->next)
30784 copy_decls_for_unworthy_types (ctnode->root_die);
30785 copy_decls_for_unworthy_types (comp_unit_die ());
30787 /* In the process of copying declarations from one unit to another,
30788 we may have left some declarations behind that are no longer
30789 referenced. Prune them. */
30790 prune_unused_types ();
30793 /* Generate separate CUs for each of the include files we've seen.
30794 They will go into limbo_die_list and from there to cu_die_list. */
30795 if (flag_eliminate_dwarf2_dups)
30797 gcc_assert (limbo_die_list == NULL);
30798 break_out_includes (comp_unit_die ());
30799 limbo_die_node *cu;
30800 while ((cu = limbo_die_list))
30802 limbo_die_list = cu->next;
30803 cu->next = cu_die_list;
30804 cu_die_list = cu;
30808 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30809 with dw_val_class_decl_ref operand. */
30810 note_variable_value (comp_unit_die ());
30811 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30812 note_variable_value (node->die);
30813 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30814 ctnode = ctnode->next)
30815 note_variable_value (ctnode->root_die);
30816 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30817 note_variable_value (node->die);
30819 /* The early debug phase is now finished. */
30820 early_dwarf_finished = true;
30822 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30823 if (!flag_generate_lto)
30824 return;
30826 /* Now as we are going to output for LTO initialize sections and labels
30827 to the LTO variants. We don't need a random-seed postfix as other
30828 LTO sections as linking the LTO debug sections into one in a partial
30829 link is fine. */
30830 init_sections_and_labels (true);
30832 /* The output below is modeled after dwarf2out_finish with all
30833 location related output removed and some LTO specific changes.
30834 Some refactoring might make both smaller and easier to match up. */
30836 /* Traverse the DIE's and add add sibling attributes to those DIE's
30837 that have children. */
30838 add_sibling_attributes (comp_unit_die ());
30839 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30840 add_sibling_attributes (node->die);
30841 for (comdat_type_node *ctnode = comdat_type_list;
30842 ctnode != NULL; ctnode = ctnode->next)
30843 add_sibling_attributes (ctnode->root_die);
30845 if (have_macinfo)
30846 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30847 macinfo_section_label);
30849 save_macinfo_strings ();
30851 /* Output all of the compilation units. We put the main one last so that
30852 the offsets are available to output_pubnames. */
30853 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30854 output_comp_unit (node->die, 0, NULL);
30856 hash_table<comdat_type_hasher> comdat_type_table (100);
30857 for (comdat_type_node *ctnode = comdat_type_list;
30858 ctnode != NULL; ctnode = ctnode->next)
30860 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30862 /* Don't output duplicate types. */
30863 if (*slot != HTAB_EMPTY_ENTRY)
30864 continue;
30866 /* Add a pointer to the line table for the main compilation unit
30867 so that the debugger can make sense of DW_AT_decl_file
30868 attributes. */
30869 if (debug_info_level >= DINFO_LEVEL_TERSE)
30870 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30871 (!dwarf_split_debug_info
30872 ? debug_line_section_label
30873 : debug_skeleton_line_section_label));
30875 output_comdat_type_unit (ctnode);
30876 *slot = ctnode;
30879 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30880 both the main_cu and all skeleton TUs. Making this call unconditional
30881 would end up either adding a second copy of the AT_pubnames attribute, or
30882 requiring a special case in add_top_level_skeleton_die_attrs. */
30883 if (!dwarf_split_debug_info)
30884 add_AT_pubnames (comp_unit_die ());
30886 /* Stick a unique symbol to the main debuginfo section. */
30887 compute_comp_unit_symbol (comp_unit_die ());
30889 /* Output the main compilation unit. We always need it if only for
30890 the CU symbol. */
30891 output_comp_unit (comp_unit_die (), true, NULL);
30893 /* Output the abbreviation table. */
30894 if (vec_safe_length (abbrev_die_table) != 1)
30896 switch_to_section (debug_abbrev_section);
30897 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30898 output_abbrev_section ();
30901 /* Have to end the macro section. */
30902 if (have_macinfo)
30904 /* We have to save macinfo state if we need to output it again
30905 for the FAT part of the object. */
30906 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30907 if (flag_fat_lto_objects)
30908 macinfo_table = macinfo_table->copy ();
30910 switch_to_section (debug_macinfo_section);
30911 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30912 output_macinfo (debug_skeleton_line_section_label, true);
30913 dw2_asm_output_data (1, 0, "End compilation unit");
30915 /* Emit a skeleton debug_line section. */
30916 switch_to_section (debug_skeleton_line_section);
30917 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30918 output_line_info (true);
30920 if (flag_fat_lto_objects)
30922 vec_free (macinfo_table);
30923 macinfo_table = saved_macinfo_table;
30928 /* If we emitted any indirect strings, output the string table too. */
30929 if (debug_str_hash || skeleton_debug_str_hash)
30930 output_indirect_strings ();
30932 /* Switch back to the text section. */
30933 switch_to_section (text_section);
30936 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30937 within the same process. For use by toplev::finalize. */
30939 void
30940 dwarf2out_c_finalize (void)
30942 last_var_location_insn = NULL;
30943 cached_next_real_insn = NULL;
30944 used_rtx_array = NULL;
30945 incomplete_types = NULL;
30946 decl_scope_table = NULL;
30947 debug_info_section = NULL;
30948 debug_skeleton_info_section = NULL;
30949 debug_abbrev_section = NULL;
30950 debug_skeleton_abbrev_section = NULL;
30951 debug_aranges_section = NULL;
30952 debug_addr_section = NULL;
30953 debug_macinfo_section = NULL;
30954 debug_line_section = NULL;
30955 debug_skeleton_line_section = NULL;
30956 debug_loc_section = NULL;
30957 debug_pubnames_section = NULL;
30958 debug_pubtypes_section = NULL;
30959 debug_str_section = NULL;
30960 debug_line_str_section = NULL;
30961 debug_str_dwo_section = NULL;
30962 debug_str_offsets_section = NULL;
30963 debug_ranges_section = NULL;
30964 debug_frame_section = NULL;
30965 fde_vec = NULL;
30966 debug_str_hash = NULL;
30967 debug_line_str_hash = NULL;
30968 skeleton_debug_str_hash = NULL;
30969 dw2_string_counter = 0;
30970 have_multiple_function_sections = false;
30971 text_section_used = false;
30972 cold_text_section_used = false;
30973 cold_text_section = NULL;
30974 current_unit_personality = NULL;
30976 early_dwarf = false;
30977 early_dwarf_finished = false;
30979 next_die_offset = 0;
30980 single_comp_unit_die = NULL;
30981 comdat_type_list = NULL;
30982 limbo_die_list = NULL;
30983 file_table = NULL;
30984 decl_die_table = NULL;
30985 common_block_die_table = NULL;
30986 decl_loc_table = NULL;
30987 call_arg_locations = NULL;
30988 call_arg_loc_last = NULL;
30989 call_site_count = -1;
30990 tail_call_site_count = -1;
30991 cached_dw_loc_list_table = NULL;
30992 abbrev_die_table = NULL;
30993 delete dwarf_proc_stack_usage_map;
30994 dwarf_proc_stack_usage_map = NULL;
30995 line_info_label_num = 0;
30996 cur_line_info_table = NULL;
30997 text_section_line_info = NULL;
30998 cold_text_section_line_info = NULL;
30999 separate_line_info = NULL;
31000 info_section_emitted = false;
31001 pubname_table = NULL;
31002 pubtype_table = NULL;
31003 macinfo_table = NULL;
31004 ranges_table = NULL;
31005 ranges_by_label = NULL;
31006 rnglist_idx = 0;
31007 have_location_lists = false;
31008 loclabel_num = 0;
31009 poc_label_num = 0;
31010 last_emitted_file = NULL;
31011 label_num = 0;
31012 tmpl_value_parm_die_table = NULL;
31013 generic_type_instances = NULL;
31014 frame_pointer_fb_offset = 0;
31015 frame_pointer_fb_offset_valid = false;
31016 base_types.release ();
31017 XDELETEVEC (producer_string);
31018 producer_string = NULL;
31021 #include "gt-dwarf2out.h"