* function.c (dump_stack_clash_frame_info): New function.
[official-gcc.git] / gcc / dwarf2out.c
blob1a4f1a2b12be24427befa32dccb4c8fd103171b4
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static unsigned macinfo_label_base = 1;
168 static GTY(()) section *debug_line_section;
169 static GTY(()) section *debug_skeleton_line_section;
170 static GTY(()) section *debug_loc_section;
171 static GTY(()) section *debug_pubnames_section;
172 static GTY(()) section *debug_pubtypes_section;
173 static GTY(()) section *debug_str_section;
174 static GTY(()) section *debug_line_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 40
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
196 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
197 #endif
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
229 typedef const char *compare_type;
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
237 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
239 /* With split_debug_info, both the comp_dir and dwo_name go in the
240 main object file, rather than the dwo, similar to the force_direct
241 parameter elsewhere but with additional complications:
243 1) The string is needed in both the main object file and the dwo.
244 That is, the comp_dir and dwo_name will appear in both places.
246 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
247 DW_FORM_line_strp or DW_FORM_GNU_str_index.
249 3) GCC chooses the form to use late, depending on the size and
250 reference count.
252 Rather than forcing the all debug string handling functions and
253 callers to deal with these complications, simply use a separate,
254 special-cased string table for any attribute that should go in the
255 main object file. This limits the complexity to just the places
256 that need it. */
258 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
260 static GTY(()) int dw2_string_counter;
262 /* True if the compilation unit places functions in more than one section. */
263 static GTY(()) bool have_multiple_function_sections = false;
265 /* Whether the default text and cold text sections have been used at all. */
267 static GTY(()) bool text_section_used = false;
268 static GTY(()) bool cold_text_section_used = false;
270 /* The default cold text section. */
271 static GTY(()) section *cold_text_section;
273 /* The DIE for C++14 'auto' in a function return type. */
274 static GTY(()) dw_die_ref auto_die;
276 /* The DIE for C++14 'decltype(auto)' in a function return type. */
277 static GTY(()) dw_die_ref decltype_auto_die;
279 /* Forward declarations for functions defined in this file. */
281 static void output_call_frame_info (int);
282 static void dwarf2out_note_section_used (void);
284 /* Personality decl of current unit. Used only when assembler does not support
285 personality CFI. */
286 static GTY(()) rtx current_unit_personality;
288 /* .debug_rnglists next index. */
289 static unsigned int rnglist_idx;
291 /* Data and reference forms for relocatable data. */
292 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
293 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
295 #ifndef DEBUG_FRAME_SECTION
296 #define DEBUG_FRAME_SECTION ".debug_frame"
297 #endif
299 #ifndef FUNC_BEGIN_LABEL
300 #define FUNC_BEGIN_LABEL "LFB"
301 #endif
303 #ifndef FUNC_END_LABEL
304 #define FUNC_END_LABEL "LFE"
305 #endif
307 #ifndef PROLOGUE_END_LABEL
308 #define PROLOGUE_END_LABEL "LPE"
309 #endif
311 #ifndef EPILOGUE_BEGIN_LABEL
312 #define EPILOGUE_BEGIN_LABEL "LEB"
313 #endif
315 #ifndef FRAME_BEGIN_LABEL
316 #define FRAME_BEGIN_LABEL "Lframe"
317 #endif
318 #define CIE_AFTER_SIZE_LABEL "LSCIE"
319 #define CIE_END_LABEL "LECIE"
320 #define FDE_LABEL "LSFDE"
321 #define FDE_AFTER_SIZE_LABEL "LASFDE"
322 #define FDE_END_LABEL "LEFDE"
323 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
324 #define LINE_NUMBER_END_LABEL "LELT"
325 #define LN_PROLOG_AS_LABEL "LASLTP"
326 #define LN_PROLOG_END_LABEL "LELTP"
327 #define DIE_LABEL_PREFIX "DW"
329 /* Match the base name of a file to the base name of a compilation unit. */
331 static int
332 matches_main_base (const char *path)
334 /* Cache the last query. */
335 static const char *last_path = NULL;
336 static int last_match = 0;
337 if (path != last_path)
339 const char *base;
340 int length = base_of_path (path, &base);
341 last_path = path;
342 last_match = (length == main_input_baselength
343 && memcmp (base, main_input_basename, length) == 0);
345 return last_match;
348 #ifdef DEBUG_DEBUG_STRUCT
350 static int
351 dump_struct_debug (tree type, enum debug_info_usage usage,
352 enum debug_struct_file criterion, int generic,
353 int matches, int result)
355 /* Find the type name. */
356 tree type_decl = TYPE_STUB_DECL (type);
357 tree t = type_decl;
358 const char *name = 0;
359 if (TREE_CODE (t) == TYPE_DECL)
360 t = DECL_NAME (t);
361 if (t)
362 name = IDENTIFIER_POINTER (t);
364 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
365 criterion,
366 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
367 matches ? "bas" : "hdr",
368 generic ? "gen" : "ord",
369 usage == DINFO_USAGE_DFN ? ";" :
370 usage == DINFO_USAGE_DIR_USE ? "." : "*",
371 result,
372 (void*) type_decl, name);
373 return result;
375 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
376 dump_struct_debug (type, usage, criterion, generic, matches, result)
378 #else
380 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
381 (result)
383 #endif
385 /* Get the number of HOST_WIDE_INTs needed to represent the precision
386 of the number. Some constants have a large uniform precision, so
387 we get the precision needed for the actual value of the number. */
389 static unsigned int
390 get_full_len (const wide_int &op)
392 int prec = wi::min_precision (op, UNSIGNED);
393 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
394 / HOST_BITS_PER_WIDE_INT);
397 static bool
398 should_emit_struct_debug (tree type, enum debug_info_usage usage)
400 enum debug_struct_file criterion;
401 tree type_decl;
402 bool generic = lang_hooks.types.generic_p (type);
404 if (generic)
405 criterion = debug_struct_generic[usage];
406 else
407 criterion = debug_struct_ordinary[usage];
409 if (criterion == DINFO_STRUCT_FILE_NONE)
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 if (criterion == DINFO_STRUCT_FILE_ANY)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
416 if (type_decl != NULL)
418 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
419 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
421 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
422 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
428 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
429 switch to the data section instead, and write out a synthetic start label
430 for collect2 the first time around. */
432 static void
433 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
435 if (eh_frame_section == 0)
437 int flags;
439 if (EH_TABLES_CAN_BE_READ_ONLY)
441 int fde_encoding;
442 int per_encoding;
443 int lsda_encoding;
445 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
446 /*global=*/0);
447 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
448 /*global=*/1);
449 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
450 /*global=*/0);
451 flags = ((! flag_pic
452 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
453 && (fde_encoding & 0x70) != DW_EH_PE_aligned
454 && (per_encoding & 0x70) != DW_EH_PE_absptr
455 && (per_encoding & 0x70) != DW_EH_PE_aligned
456 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
457 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
458 ? 0 : SECTION_WRITE);
460 else
461 flags = SECTION_WRITE;
463 #ifdef EH_FRAME_SECTION_NAME
464 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
465 #else
466 eh_frame_section = ((flags == SECTION_WRITE)
467 ? data_section : readonly_data_section);
468 #endif /* EH_FRAME_SECTION_NAME */
471 switch_to_section (eh_frame_section);
473 #ifdef EH_FRAME_THROUGH_COLLECT2
474 /* We have no special eh_frame section. Emit special labels to guide
475 collect2. */
476 if (!back)
478 tree label = get_file_function_name ("F");
479 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
480 targetm.asm_out.globalize_label (asm_out_file,
481 IDENTIFIER_POINTER (label));
482 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
484 #endif
487 /* Switch [BACK] to the eh or debug frame table section, depending on
488 FOR_EH. */
490 static void
491 switch_to_frame_table_section (int for_eh, bool back)
493 if (for_eh)
494 switch_to_eh_frame_section (back);
495 else
497 if (!debug_frame_section)
498 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
499 SECTION_DEBUG, NULL);
500 switch_to_section (debug_frame_section);
504 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
506 enum dw_cfi_oprnd_type
507 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
509 switch (cfi)
511 case DW_CFA_nop:
512 case DW_CFA_GNU_window_save:
513 case DW_CFA_remember_state:
514 case DW_CFA_restore_state:
515 return dw_cfi_oprnd_unused;
517 case DW_CFA_set_loc:
518 case DW_CFA_advance_loc1:
519 case DW_CFA_advance_loc2:
520 case DW_CFA_advance_loc4:
521 case DW_CFA_MIPS_advance_loc8:
522 return dw_cfi_oprnd_addr;
524 case DW_CFA_offset:
525 case DW_CFA_offset_extended:
526 case DW_CFA_def_cfa:
527 case DW_CFA_offset_extended_sf:
528 case DW_CFA_def_cfa_sf:
529 case DW_CFA_restore:
530 case DW_CFA_restore_extended:
531 case DW_CFA_undefined:
532 case DW_CFA_same_value:
533 case DW_CFA_def_cfa_register:
534 case DW_CFA_register:
535 case DW_CFA_expression:
536 case DW_CFA_val_expression:
537 return dw_cfi_oprnd_reg_num;
539 case DW_CFA_def_cfa_offset:
540 case DW_CFA_GNU_args_size:
541 case DW_CFA_def_cfa_offset_sf:
542 return dw_cfi_oprnd_offset;
544 case DW_CFA_def_cfa_expression:
545 return dw_cfi_oprnd_loc;
547 default:
548 gcc_unreachable ();
552 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
554 enum dw_cfi_oprnd_type
555 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
557 switch (cfi)
559 case DW_CFA_def_cfa:
560 case DW_CFA_def_cfa_sf:
561 case DW_CFA_offset:
562 case DW_CFA_offset_extended_sf:
563 case DW_CFA_offset_extended:
564 return dw_cfi_oprnd_offset;
566 case DW_CFA_register:
567 return dw_cfi_oprnd_reg_num;
569 case DW_CFA_expression:
570 case DW_CFA_val_expression:
571 return dw_cfi_oprnd_loc;
573 default:
574 return dw_cfi_oprnd_unused;
578 /* Output one FDE. */
580 static void
581 output_fde (dw_fde_ref fde, bool for_eh, bool second,
582 char *section_start_label, int fde_encoding, char *augmentation,
583 bool any_lsda_needed, int lsda_encoding)
585 const char *begin, *end;
586 static unsigned int j;
587 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
589 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
590 /* empty */ 0);
591 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
592 for_eh + j);
593 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
595 if (!XCOFF_DEBUGGING_INFO || for_eh)
597 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
598 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
599 " indicating 64-bit DWARF extension");
600 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
601 "FDE Length");
603 ASM_OUTPUT_LABEL (asm_out_file, l1);
605 if (for_eh)
606 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
607 else
608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
609 debug_frame_section, "FDE CIE offset");
611 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
612 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614 if (for_eh)
616 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
617 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
618 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
619 "FDE initial location");
620 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
621 end, begin, "FDE address range");
623 else
625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 if (augmentation[0])
631 if (any_lsda_needed)
633 int size = size_of_encoded_value (lsda_encoding);
635 if (lsda_encoding == DW_EH_PE_aligned)
637 int offset = ( 4 /* Length */
638 + 4 /* CIE offset */
639 + 2 * size_of_encoded_value (fde_encoding)
640 + 1 /* Augmentation size */ );
641 int pad = -offset & (PTR_SIZE - 1);
643 size += pad;
644 gcc_assert (size_of_uleb128 (size) == 1);
647 dw2_asm_output_data_uleb128 (size, "Augmentation size");
649 if (fde->uses_eh_lsda)
651 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
652 fde->funcdef_number);
653 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
654 gen_rtx_SYMBOL_REF (Pmode, l1),
655 false,
656 "Language Specific Data Area");
658 else
660 if (lsda_encoding == DW_EH_PE_aligned)
661 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
662 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
663 "Language Specific Data Area (none)");
666 else
667 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 /* Loop through the Call Frame Instructions associated with this FDE. */
671 fde->dw_fde_current_label = begin;
673 size_t from, until, i;
675 from = 0;
676 until = vec_safe_length (fde->dw_fde_cfi);
678 if (fde->dw_fde_second_begin == NULL)
680 else if (!second)
681 until = fde->dw_fde_switch_cfi_index;
682 else
683 from = fde->dw_fde_switch_cfi_index;
685 for (i = from; i < until; i++)
686 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 /* If we are to emit a ref/link from function bodies to their frame tables,
690 do it now. This is typically performed to make sure that tables
691 associated with functions are dragged with them and not discarded in
692 garbage collecting links. We need to do this on a per function basis to
693 cope with -ffunction-sections. */
695 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
696 /* Switch to the function section, emit the ref to the tables, and
697 switch *back* into the table section. */
698 switch_to_section (function_section (fde->decl));
699 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
700 switch_to_frame_table_section (for_eh, true);
701 #endif
703 /* Pad the FDE out to an address sized boundary. */
704 ASM_OUTPUT_ALIGN (asm_out_file,
705 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
706 ASM_OUTPUT_LABEL (asm_out_file, l2);
708 j += 2;
711 /* Return true if frame description entry FDE is needed for EH. */
713 static bool
714 fde_needed_for_eh_p (dw_fde_ref fde)
716 if (flag_asynchronous_unwind_tables)
717 return true;
719 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
720 return true;
722 if (fde->uses_eh_lsda)
723 return true;
725 /* If exceptions are enabled, we have collected nothrow info. */
726 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
727 return false;
729 return true;
732 /* Output the call frame information used to record information
733 that relates to calculating the frame pointer, and records the
734 location of saved registers. */
736 static void
737 output_call_frame_info (int for_eh)
739 unsigned int i;
740 dw_fde_ref fde;
741 dw_cfi_ref cfi;
742 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
743 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
744 bool any_lsda_needed = false;
745 char augmentation[6];
746 int augmentation_size;
747 int fde_encoding = DW_EH_PE_absptr;
748 int per_encoding = DW_EH_PE_absptr;
749 int lsda_encoding = DW_EH_PE_absptr;
750 int return_reg;
751 rtx personality = NULL;
752 int dw_cie_version;
754 /* Don't emit a CIE if there won't be any FDEs. */
755 if (!fde_vec)
756 return;
758 /* Nothing to do if the assembler's doing it all. */
759 if (dwarf2out_do_cfi_asm ())
760 return;
762 /* If we don't have any functions we'll want to unwind out of, don't emit
763 any EH unwind information. If we make FDEs linkonce, we may have to
764 emit an empty label for an FDE that wouldn't otherwise be emitted. We
765 want to avoid having an FDE kept around when the function it refers to
766 is discarded. Example where this matters: a primary function template
767 in C++ requires EH information, an explicit specialization doesn't. */
768 if (for_eh)
770 bool any_eh_needed = false;
772 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 if (fde->uses_eh_lsda)
775 any_eh_needed = any_lsda_needed = true;
776 else if (fde_needed_for_eh_p (fde))
777 any_eh_needed = true;
778 else if (TARGET_USES_WEAK_UNWIND_INFO)
779 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 if (!any_eh_needed)
783 return;
786 /* We're going to be generating comments, so turn on app. */
787 if (flag_debug_asm)
788 app_enable ();
790 /* Switch to the proper frame section, first time. */
791 switch_to_frame_table_section (for_eh, false);
793 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796 /* Output the CIE. */
797 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802 dw2_asm_output_data (4, 0xffffffff,
803 "Initial length escape value indicating 64-bit DWARF extension");
804 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 unsigned int column ATTRIBUTE_UNUSED,
1035 const char *file ATTRIBUTE_UNUSED)
1037 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1038 char * dup_label;
1039 dw_fde_ref fde;
1040 section *fnsec;
1041 bool do_frame;
1043 current_function_func_begin_label = NULL;
1045 do_frame = dwarf2out_do_frame ();
1047 /* ??? current_function_func_begin_label is also used by except.c for
1048 call-site information. We must emit this label if it might be used. */
1049 if (!do_frame
1050 && (!flag_exceptions
1051 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1052 return;
1054 fnsec = function_section (current_function_decl);
1055 switch_to_section (fnsec);
1056 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1057 current_function_funcdef_no);
1058 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1059 current_function_funcdef_no);
1060 dup_label = xstrdup (label);
1061 current_function_func_begin_label = dup_label;
1063 /* We can elide the fde allocation if we're not emitting debug info. */
1064 if (!do_frame)
1065 return;
1067 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1068 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1069 would include pass_dwarf2_frame. If we've not created the FDE yet,
1070 do so now. */
1071 fde = cfun->fde;
1072 if (fde == NULL)
1073 fde = dwarf2out_alloc_current_fde ();
1075 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1076 fde->dw_fde_begin = dup_label;
1077 fde->dw_fde_current_label = dup_label;
1078 fde->in_std_section = (fnsec == text_section
1079 || (cold_text_section && fnsec == cold_text_section));
1081 /* We only want to output line number information for the genuine dwarf2
1082 prologue case, not the eh frame case. */
1083 #ifdef DWARF2_DEBUGGING_INFO
1084 if (file)
1085 dwarf2out_source_line (line, column, file, 0, true);
1086 #endif
1088 if (dwarf2out_do_cfi_asm ())
1089 dwarf2out_do_cfi_startproc (false);
1090 else
1092 rtx personality = get_personality_function (current_function_decl);
1093 if (!current_unit_personality)
1094 current_unit_personality = personality;
1096 /* We cannot keep a current personality per function as without CFI
1097 asm, at the point where we emit the CFI data, there is no current
1098 function anymore. */
1099 if (personality && current_unit_personality != personality)
1100 sorry ("multiple EH personalities are supported only with assemblers "
1101 "supporting .cfi_personality directive");
1105 /* Output a marker (i.e. a label) for the end of the generated code
1106 for a function prologue. This gets called *after* the prologue code has
1107 been generated. */
1109 void
1110 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1111 const char *file ATTRIBUTE_UNUSED)
1113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115 /* Output a label to mark the endpoint of the code generated for this
1116 function. */
1117 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1118 current_function_funcdef_no);
1119 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1120 current_function_funcdef_no);
1121 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1124 /* Output a marker (i.e. a label) for the beginning of the generated code
1125 for a function epilogue. This gets called *before* the prologue code has
1126 been generated. */
1128 void
1129 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1130 const char *file ATTRIBUTE_UNUSED)
1132 dw_fde_ref fde = cfun->fde;
1133 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135 if (fde->dw_fde_vms_begin_epilogue)
1136 return;
1138 /* Output a label to mark the endpoint of the code generated for this
1139 function. */
1140 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1141 current_function_funcdef_no);
1142 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1143 current_function_funcdef_no);
1144 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1147 /* Output a marker (i.e. a label) for the absolute end of the generated code
1148 for a function definition. This gets called *after* the epilogue code has
1149 been generated. */
1151 void
1152 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1153 const char *file ATTRIBUTE_UNUSED)
1155 dw_fde_ref fde;
1156 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158 last_var_location_insn = NULL;
1159 cached_next_real_insn = NULL;
1161 if (dwarf2out_do_cfi_asm ())
1162 fprintf (asm_out_file, "\t.cfi_endproc\n");
1164 /* Output a label to mark the endpoint of the code generated for this
1165 function. */
1166 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1167 current_function_funcdef_no);
1168 ASM_OUTPUT_LABEL (asm_out_file, label);
1169 fde = cfun->fde;
1170 gcc_assert (fde != NULL);
1171 if (fde->dw_fde_second_begin == NULL)
1172 fde->dw_fde_end = xstrdup (label);
1175 void
1176 dwarf2out_frame_finish (void)
1178 /* Output call frame information. */
1179 if (targetm.debug_unwind_info () == UI_DWARF2)
1180 output_call_frame_info (0);
1182 /* Output another copy for the unwinder. */
1183 if ((flag_unwind_tables || flag_exceptions)
1184 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1185 output_call_frame_info (1);
1188 /* Note that the current function section is being used for code. */
1190 static void
1191 dwarf2out_note_section_used (void)
1193 section *sec = current_function_section ();
1194 if (sec == text_section)
1195 text_section_used = true;
1196 else if (sec == cold_text_section)
1197 cold_text_section_used = true;
1200 static void var_location_switch_text_section (void);
1201 static void set_cur_line_info_table (section *);
1203 void
1204 dwarf2out_switch_text_section (void)
1206 section *sect;
1207 dw_fde_ref fde = cfun->fde;
1209 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1211 if (!in_cold_section_p)
1213 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1214 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1215 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1217 else
1219 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1220 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1221 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1223 have_multiple_function_sections = true;
1225 /* There is no need to mark used sections when not debugging. */
1226 if (cold_text_section != NULL)
1227 dwarf2out_note_section_used ();
1229 if (dwarf2out_do_cfi_asm ())
1230 fprintf (asm_out_file, "\t.cfi_endproc\n");
1232 /* Now do the real section switch. */
1233 sect = current_function_section ();
1234 switch_to_section (sect);
1236 fde->second_in_std_section
1237 = (sect == text_section
1238 || (cold_text_section && sect == cold_text_section));
1240 if (dwarf2out_do_cfi_asm ())
1241 dwarf2out_do_cfi_startproc (true);
1243 var_location_switch_text_section ();
1245 if (cold_text_section != NULL)
1246 set_cur_line_info_table (sect);
1249 /* And now, the subset of the debugging information support code necessary
1250 for emitting location expressions. */
1252 /* Data about a single source file. */
1253 struct GTY((for_user)) dwarf_file_data {
1254 const char * filename;
1255 int emitted_number;
1258 /* Describe an entry into the .debug_addr section. */
1260 enum ate_kind {
1261 ate_kind_rtx,
1262 ate_kind_rtx_dtprel,
1263 ate_kind_label
1266 struct GTY((for_user)) addr_table_entry {
1267 enum ate_kind kind;
1268 unsigned int refcount;
1269 unsigned int index;
1270 union addr_table_entry_struct_union
1272 rtx GTY ((tag ("0"))) rtl;
1273 char * GTY ((tag ("1"))) label;
1275 GTY ((desc ("%1.kind"))) addr;
1278 /* Location lists are ranges + location descriptions for that range,
1279 so you can track variables that are in different places over
1280 their entire life. */
1281 typedef struct GTY(()) dw_loc_list_struct {
1282 dw_loc_list_ref dw_loc_next;
1283 const char *begin; /* Label and addr_entry for start of range */
1284 addr_table_entry *begin_entry;
1285 const char *end; /* Label for end of range */
1286 char *ll_symbol; /* Label for beginning of location list.
1287 Only on head of list */
1288 const char *section; /* Section this loclist is relative to */
1289 dw_loc_descr_ref expr;
1290 hashval_t hash;
1291 /* True if all addresses in this and subsequent lists are known to be
1292 resolved. */
1293 bool resolved_addr;
1294 /* True if this list has been replaced by dw_loc_next. */
1295 bool replaced;
1296 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1297 section. */
1298 unsigned char emitted : 1;
1299 /* True if hash field is index rather than hash value. */
1300 unsigned char num_assigned : 1;
1301 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1302 unsigned char offset_emitted : 1;
1303 /* True if note_variable_value_in_expr has been called on it. */
1304 unsigned char noted_variable_value : 1;
1305 /* True if the range should be emitted even if begin and end
1306 are the same. */
1307 bool force;
1308 } dw_loc_list_node;
1310 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1311 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1313 /* Convert a DWARF stack opcode into its string name. */
1315 static const char *
1316 dwarf_stack_op_name (unsigned int op)
1318 const char *name = get_DW_OP_name (op);
1320 if (name != NULL)
1321 return name;
1323 return "OP_<unknown>";
1326 /* Return a pointer to a newly allocated location description. Location
1327 descriptions are simple expression terms that can be strung
1328 together to form more complicated location (address) descriptions. */
1330 static inline dw_loc_descr_ref
1331 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1332 unsigned HOST_WIDE_INT oprnd2)
1334 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1336 descr->dw_loc_opc = op;
1337 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd1.val_entry = NULL;
1339 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1340 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1341 descr->dw_loc_oprnd2.val_entry = NULL;
1342 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1344 return descr;
1347 /* Return a pointer to a newly allocated location description for
1348 REG and OFFSET. */
1350 static inline dw_loc_descr_ref
1351 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1353 if (reg <= 31)
1354 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1355 offset, 0);
1356 else
1357 return new_loc_descr (DW_OP_bregx, reg, offset);
1360 /* Add a location description term to a location description expression. */
1362 static inline void
1363 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1365 dw_loc_descr_ref *d;
1367 /* Find the end of the chain. */
1368 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1371 *d = descr;
1374 /* Compare two location operands for exact equality. */
1376 static bool
1377 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1379 if (a->val_class != b->val_class)
1380 return false;
1381 switch (a->val_class)
1383 case dw_val_class_none:
1384 return true;
1385 case dw_val_class_addr:
1386 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1388 case dw_val_class_offset:
1389 case dw_val_class_unsigned_const:
1390 case dw_val_class_const:
1391 case dw_val_class_unsigned_const_implicit:
1392 case dw_val_class_const_implicit:
1393 case dw_val_class_range_list:
1394 /* These are all HOST_WIDE_INT, signed or unsigned. */
1395 return a->v.val_unsigned == b->v.val_unsigned;
1397 case dw_val_class_loc:
1398 return a->v.val_loc == b->v.val_loc;
1399 case dw_val_class_loc_list:
1400 return a->v.val_loc_list == b->v.val_loc_list;
1401 case dw_val_class_die_ref:
1402 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1403 case dw_val_class_fde_ref:
1404 return a->v.val_fde_index == b->v.val_fde_index;
1405 case dw_val_class_lbl_id:
1406 case dw_val_class_lineptr:
1407 case dw_val_class_macptr:
1408 case dw_val_class_loclistsptr:
1409 case dw_val_class_high_pc:
1410 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1411 case dw_val_class_str:
1412 return a->v.val_str == b->v.val_str;
1413 case dw_val_class_flag:
1414 return a->v.val_flag == b->v.val_flag;
1415 case dw_val_class_file:
1416 case dw_val_class_file_implicit:
1417 return a->v.val_file == b->v.val_file;
1418 case dw_val_class_decl_ref:
1419 return a->v.val_decl_ref == b->v.val_decl_ref;
1421 case dw_val_class_const_double:
1422 return (a->v.val_double.high == b->v.val_double.high
1423 && a->v.val_double.low == b->v.val_double.low);
1425 case dw_val_class_wide_int:
1426 return *a->v.val_wide == *b->v.val_wide;
1428 case dw_val_class_vec:
1430 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1431 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1433 return (a_len == b_len
1434 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1437 case dw_val_class_data8:
1438 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1440 case dw_val_class_vms_delta:
1441 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1442 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1444 case dw_val_class_discr_value:
1445 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1446 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1447 case dw_val_class_discr_list:
1448 /* It makes no sense comparing two discriminant value lists. */
1449 return false;
1451 gcc_unreachable ();
1454 /* Compare two location atoms for exact equality. */
1456 static bool
1457 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1459 if (a->dw_loc_opc != b->dw_loc_opc)
1460 return false;
1462 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1463 address size, but since we always allocate cleared storage it
1464 should be zero for other types of locations. */
1465 if (a->dtprel != b->dtprel)
1466 return false;
1468 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1469 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1472 /* Compare two complete location expressions for exact equality. */
1474 bool
1475 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1477 while (1)
1479 if (a == b)
1480 return true;
1481 if (a == NULL || b == NULL)
1482 return false;
1483 if (!loc_descr_equal_p_1 (a, b))
1484 return false;
1486 a = a->dw_loc_next;
1487 b = b->dw_loc_next;
1492 /* Add a constant OFFSET to a location expression. */
1494 static void
1495 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1497 dw_loc_descr_ref loc;
1498 HOST_WIDE_INT *p;
1500 gcc_assert (*list_head != NULL);
1502 if (!offset)
1503 return;
1505 /* Find the end of the chain. */
1506 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1509 p = NULL;
1510 if (loc->dw_loc_opc == DW_OP_fbreg
1511 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1512 p = &loc->dw_loc_oprnd1.v.val_int;
1513 else if (loc->dw_loc_opc == DW_OP_bregx)
1514 p = &loc->dw_loc_oprnd2.v.val_int;
1516 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1517 offset. Don't optimize if an signed integer overflow would happen. */
1518 if (p != NULL
1519 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1520 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1521 *p += offset;
1523 else if (offset > 0)
1524 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1526 else
1528 loc->dw_loc_next
1529 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1530 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534 /* Add a constant OFFSET to a location list. */
1536 static void
1537 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1539 dw_loc_list_ref d;
1540 for (d = list_head; d != NULL; d = d->dw_loc_next)
1541 loc_descr_plus_const (&d->expr, offset);
1544 #define DWARF_REF_SIZE \
1545 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1547 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1548 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1549 DW_FORM_data16 with 128 bits. */
1550 #define DWARF_LARGEST_DATA_FORM_BITS \
1551 (dwarf_version >= 5 ? 128 : 64)
1553 /* Utility inline function for construction of ops that were GNU extension
1554 before DWARF 5. */
1555 static inline enum dwarf_location_atom
1556 dwarf_OP (enum dwarf_location_atom op)
1558 switch (op)
1560 case DW_OP_implicit_pointer:
1561 if (dwarf_version < 5)
1562 return DW_OP_GNU_implicit_pointer;
1563 break;
1565 case DW_OP_entry_value:
1566 if (dwarf_version < 5)
1567 return DW_OP_GNU_entry_value;
1568 break;
1570 case DW_OP_const_type:
1571 if (dwarf_version < 5)
1572 return DW_OP_GNU_const_type;
1573 break;
1575 case DW_OP_regval_type:
1576 if (dwarf_version < 5)
1577 return DW_OP_GNU_regval_type;
1578 break;
1580 case DW_OP_deref_type:
1581 if (dwarf_version < 5)
1582 return DW_OP_GNU_deref_type;
1583 break;
1585 case DW_OP_convert:
1586 if (dwarf_version < 5)
1587 return DW_OP_GNU_convert;
1588 break;
1590 case DW_OP_reinterpret:
1591 if (dwarf_version < 5)
1592 return DW_OP_GNU_reinterpret;
1593 break;
1595 default:
1596 break;
1598 return op;
1601 /* Similarly for attributes. */
1602 static inline enum dwarf_attribute
1603 dwarf_AT (enum dwarf_attribute at)
1605 switch (at)
1607 case DW_AT_call_return_pc:
1608 if (dwarf_version < 5)
1609 return DW_AT_low_pc;
1610 break;
1612 case DW_AT_call_tail_call:
1613 if (dwarf_version < 5)
1614 return DW_AT_GNU_tail_call;
1615 break;
1617 case DW_AT_call_origin:
1618 if (dwarf_version < 5)
1619 return DW_AT_abstract_origin;
1620 break;
1622 case DW_AT_call_target:
1623 if (dwarf_version < 5)
1624 return DW_AT_GNU_call_site_target;
1625 break;
1627 case DW_AT_call_target_clobbered:
1628 if (dwarf_version < 5)
1629 return DW_AT_GNU_call_site_target_clobbered;
1630 break;
1632 case DW_AT_call_parameter:
1633 if (dwarf_version < 5)
1634 return DW_AT_abstract_origin;
1635 break;
1637 case DW_AT_call_value:
1638 if (dwarf_version < 5)
1639 return DW_AT_GNU_call_site_value;
1640 break;
1642 case DW_AT_call_data_value:
1643 if (dwarf_version < 5)
1644 return DW_AT_GNU_call_site_data_value;
1645 break;
1647 case DW_AT_call_all_calls:
1648 if (dwarf_version < 5)
1649 return DW_AT_GNU_all_call_sites;
1650 break;
1652 case DW_AT_call_all_tail_calls:
1653 if (dwarf_version < 5)
1654 return DW_AT_GNU_all_tail_call_sites;
1655 break;
1657 case DW_AT_dwo_name:
1658 if (dwarf_version < 5)
1659 return DW_AT_GNU_dwo_name;
1660 break;
1662 default:
1663 break;
1665 return at;
1668 /* And similarly for tags. */
1669 static inline enum dwarf_tag
1670 dwarf_TAG (enum dwarf_tag tag)
1672 switch (tag)
1674 case DW_TAG_call_site:
1675 if (dwarf_version < 5)
1676 return DW_TAG_GNU_call_site;
1677 break;
1679 case DW_TAG_call_site_parameter:
1680 if (dwarf_version < 5)
1681 return DW_TAG_GNU_call_site_parameter;
1682 break;
1684 default:
1685 break;
1687 return tag;
1690 static unsigned long int get_base_type_offset (dw_die_ref);
1692 /* Return the size of a location descriptor. */
1694 static unsigned long
1695 size_of_loc_descr (dw_loc_descr_ref loc)
1697 unsigned long size = 1;
1699 switch (loc->dw_loc_opc)
1701 case DW_OP_addr:
1702 size += DWARF2_ADDR_SIZE;
1703 break;
1704 case DW_OP_GNU_addr_index:
1705 case DW_OP_GNU_const_index:
1706 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1707 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1708 break;
1709 case DW_OP_const1u:
1710 case DW_OP_const1s:
1711 size += 1;
1712 break;
1713 case DW_OP_const2u:
1714 case DW_OP_const2s:
1715 size += 2;
1716 break;
1717 case DW_OP_const4u:
1718 case DW_OP_const4s:
1719 size += 4;
1720 break;
1721 case DW_OP_const8u:
1722 case DW_OP_const8s:
1723 size += 8;
1724 break;
1725 case DW_OP_constu:
1726 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1727 break;
1728 case DW_OP_consts:
1729 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1730 break;
1731 case DW_OP_pick:
1732 size += 1;
1733 break;
1734 case DW_OP_plus_uconst:
1735 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1736 break;
1737 case DW_OP_skip:
1738 case DW_OP_bra:
1739 size += 2;
1740 break;
1741 case DW_OP_breg0:
1742 case DW_OP_breg1:
1743 case DW_OP_breg2:
1744 case DW_OP_breg3:
1745 case DW_OP_breg4:
1746 case DW_OP_breg5:
1747 case DW_OP_breg6:
1748 case DW_OP_breg7:
1749 case DW_OP_breg8:
1750 case DW_OP_breg9:
1751 case DW_OP_breg10:
1752 case DW_OP_breg11:
1753 case DW_OP_breg12:
1754 case DW_OP_breg13:
1755 case DW_OP_breg14:
1756 case DW_OP_breg15:
1757 case DW_OP_breg16:
1758 case DW_OP_breg17:
1759 case DW_OP_breg18:
1760 case DW_OP_breg19:
1761 case DW_OP_breg20:
1762 case DW_OP_breg21:
1763 case DW_OP_breg22:
1764 case DW_OP_breg23:
1765 case DW_OP_breg24:
1766 case DW_OP_breg25:
1767 case DW_OP_breg26:
1768 case DW_OP_breg27:
1769 case DW_OP_breg28:
1770 case DW_OP_breg29:
1771 case DW_OP_breg30:
1772 case DW_OP_breg31:
1773 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1774 break;
1775 case DW_OP_regx:
1776 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1777 break;
1778 case DW_OP_fbreg:
1779 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1780 break;
1781 case DW_OP_bregx:
1782 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1784 break;
1785 case DW_OP_piece:
1786 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787 break;
1788 case DW_OP_bit_piece:
1789 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1791 break;
1792 case DW_OP_deref_size:
1793 case DW_OP_xderef_size:
1794 size += 1;
1795 break;
1796 case DW_OP_call2:
1797 size += 2;
1798 break;
1799 case DW_OP_call4:
1800 size += 4;
1801 break;
1802 case DW_OP_call_ref:
1803 case DW_OP_GNU_variable_value:
1804 size += DWARF_REF_SIZE;
1805 break;
1806 case DW_OP_implicit_value:
1807 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1808 + loc->dw_loc_oprnd1.v.val_unsigned;
1809 break;
1810 case DW_OP_implicit_pointer:
1811 case DW_OP_GNU_implicit_pointer:
1812 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1813 break;
1814 case DW_OP_entry_value:
1815 case DW_OP_GNU_entry_value:
1817 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1818 size += size_of_uleb128 (op_size) + op_size;
1819 break;
1821 case DW_OP_const_type:
1822 case DW_OP_GNU_const_type:
1824 unsigned long o
1825 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1826 size += size_of_uleb128 (o) + 1;
1827 switch (loc->dw_loc_oprnd2.val_class)
1829 case dw_val_class_vec:
1830 size += loc->dw_loc_oprnd2.v.val_vec.length
1831 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1832 break;
1833 case dw_val_class_const:
1834 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1835 break;
1836 case dw_val_class_const_double:
1837 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1838 break;
1839 case dw_val_class_wide_int:
1840 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1841 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1842 break;
1843 default:
1844 gcc_unreachable ();
1846 break;
1848 case DW_OP_regval_type:
1849 case DW_OP_GNU_regval_type:
1851 unsigned long o
1852 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1853 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1854 + size_of_uleb128 (o);
1856 break;
1857 case DW_OP_deref_type:
1858 case DW_OP_GNU_deref_type:
1860 unsigned long o
1861 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1862 size += 1 + size_of_uleb128 (o);
1864 break;
1865 case DW_OP_convert:
1866 case DW_OP_reinterpret:
1867 case DW_OP_GNU_convert:
1868 case DW_OP_GNU_reinterpret:
1869 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871 else
1873 unsigned long o
1874 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1875 size += size_of_uleb128 (o);
1877 break;
1878 case DW_OP_GNU_parameter_ref:
1879 size += 4;
1880 break;
1881 default:
1882 break;
1885 return size;
1888 /* Return the size of a series of location descriptors. */
1890 unsigned long
1891 size_of_locs (dw_loc_descr_ref loc)
1893 dw_loc_descr_ref l;
1894 unsigned long size;
1896 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1897 field, to avoid writing to a PCH file. */
1898 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1900 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1901 break;
1902 size += size_of_loc_descr (l);
1904 if (! l)
1905 return size;
1907 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1909 l->dw_loc_addr = size;
1910 size += size_of_loc_descr (l);
1913 return size;
1916 /* Return the size of the value in a DW_AT_discr_value attribute. */
1918 static int
1919 size_of_discr_value (dw_discr_value *discr_value)
1921 if (discr_value->pos)
1922 return size_of_uleb128 (discr_value->v.uval);
1923 else
1924 return size_of_sleb128 (discr_value->v.sval);
1927 /* Return the size of the value in a DW_AT_discr_list attribute. */
1929 static int
1930 size_of_discr_list (dw_discr_list_ref discr_list)
1932 int size = 0;
1934 for (dw_discr_list_ref list = discr_list;
1935 list != NULL;
1936 list = list->dw_discr_next)
1938 /* One byte for the discriminant value descriptor, and then one or two
1939 LEB128 numbers, depending on whether it's a single case label or a
1940 range label. */
1941 size += 1;
1942 size += size_of_discr_value (&list->dw_discr_lower_bound);
1943 if (list->dw_discr_range != 0)
1944 size += size_of_discr_value (&list->dw_discr_upper_bound);
1946 return size;
1949 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1950 static void get_ref_die_offset_label (char *, dw_die_ref);
1951 static unsigned long int get_ref_die_offset (dw_die_ref);
1953 /* Output location description stack opcode's operands (if any).
1954 The for_eh_or_skip parameter controls whether register numbers are
1955 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1956 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1957 info). This should be suppressed for the cases that have not been converted
1958 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1960 static void
1961 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1963 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1964 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1966 switch (loc->dw_loc_opc)
1968 #ifdef DWARF2_DEBUGGING_INFO
1969 case DW_OP_const2u:
1970 case DW_OP_const2s:
1971 dw2_asm_output_data (2, val1->v.val_int, NULL);
1972 break;
1973 case DW_OP_const4u:
1974 if (loc->dtprel)
1976 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1980 break;
1982 /* FALLTHRU */
1983 case DW_OP_const4s:
1984 dw2_asm_output_data (4, val1->v.val_int, NULL);
1985 break;
1986 case DW_OP_const8u:
1987 if (loc->dtprel)
1989 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1990 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1991 val1->v.val_addr);
1992 fputc ('\n', asm_out_file);
1993 break;
1995 /* FALLTHRU */
1996 case DW_OP_const8s:
1997 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1998 dw2_asm_output_data (8, val1->v.val_int, NULL);
1999 break;
2000 case DW_OP_skip:
2001 case DW_OP_bra:
2003 int offset;
2005 gcc_assert (val1->val_class == dw_val_class_loc);
2006 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2008 dw2_asm_output_data (2, offset, NULL);
2010 break;
2011 case DW_OP_implicit_value:
2012 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2013 switch (val2->val_class)
2015 case dw_val_class_const:
2016 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2017 break;
2018 case dw_val_class_vec:
2020 unsigned int elt_size = val2->v.val_vec.elt_size;
2021 unsigned int len = val2->v.val_vec.length;
2022 unsigned int i;
2023 unsigned char *p;
2025 if (elt_size > sizeof (HOST_WIDE_INT))
2027 elt_size /= 2;
2028 len *= 2;
2030 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2031 i < len;
2032 i++, p += elt_size)
2033 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2034 "fp or vector constant word %u", i);
2036 break;
2037 case dw_val_class_const_double:
2039 unsigned HOST_WIDE_INT first, second;
2041 if (WORDS_BIG_ENDIAN)
2043 first = val2->v.val_double.high;
2044 second = val2->v.val_double.low;
2046 else
2048 first = val2->v.val_double.low;
2049 second = val2->v.val_double.high;
2051 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2052 first, NULL);
2053 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2054 second, NULL);
2056 break;
2057 case dw_val_class_wide_int:
2059 int i;
2060 int len = get_full_len (*val2->v.val_wide);
2061 if (WORDS_BIG_ENDIAN)
2062 for (i = len - 1; i >= 0; --i)
2063 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2064 val2->v.val_wide->elt (i), NULL);
2065 else
2066 for (i = 0; i < len; ++i)
2067 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2068 val2->v.val_wide->elt (i), NULL);
2070 break;
2071 case dw_val_class_addr:
2072 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2073 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2074 break;
2075 default:
2076 gcc_unreachable ();
2078 break;
2079 #else
2080 case DW_OP_const2u:
2081 case DW_OP_const2s:
2082 case DW_OP_const4u:
2083 case DW_OP_const4s:
2084 case DW_OP_const8u:
2085 case DW_OP_const8s:
2086 case DW_OP_skip:
2087 case DW_OP_bra:
2088 case DW_OP_implicit_value:
2089 /* We currently don't make any attempt to make sure these are
2090 aligned properly like we do for the main unwind info, so
2091 don't support emitting things larger than a byte if we're
2092 only doing unwinding. */
2093 gcc_unreachable ();
2094 #endif
2095 case DW_OP_const1u:
2096 case DW_OP_const1s:
2097 dw2_asm_output_data (1, val1->v.val_int, NULL);
2098 break;
2099 case DW_OP_constu:
2100 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2101 break;
2102 case DW_OP_consts:
2103 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2104 break;
2105 case DW_OP_pick:
2106 dw2_asm_output_data (1, val1->v.val_int, NULL);
2107 break;
2108 case DW_OP_plus_uconst:
2109 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2110 break;
2111 case DW_OP_breg0:
2112 case DW_OP_breg1:
2113 case DW_OP_breg2:
2114 case DW_OP_breg3:
2115 case DW_OP_breg4:
2116 case DW_OP_breg5:
2117 case DW_OP_breg6:
2118 case DW_OP_breg7:
2119 case DW_OP_breg8:
2120 case DW_OP_breg9:
2121 case DW_OP_breg10:
2122 case DW_OP_breg11:
2123 case DW_OP_breg12:
2124 case DW_OP_breg13:
2125 case DW_OP_breg14:
2126 case DW_OP_breg15:
2127 case DW_OP_breg16:
2128 case DW_OP_breg17:
2129 case DW_OP_breg18:
2130 case DW_OP_breg19:
2131 case DW_OP_breg20:
2132 case DW_OP_breg21:
2133 case DW_OP_breg22:
2134 case DW_OP_breg23:
2135 case DW_OP_breg24:
2136 case DW_OP_breg25:
2137 case DW_OP_breg26:
2138 case DW_OP_breg27:
2139 case DW_OP_breg28:
2140 case DW_OP_breg29:
2141 case DW_OP_breg30:
2142 case DW_OP_breg31:
2143 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2144 break;
2145 case DW_OP_regx:
2147 unsigned r = val1->v.val_unsigned;
2148 if (for_eh_or_skip >= 0)
2149 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2150 gcc_assert (size_of_uleb128 (r)
2151 == size_of_uleb128 (val1->v.val_unsigned));
2152 dw2_asm_output_data_uleb128 (r, NULL);
2154 break;
2155 case DW_OP_fbreg:
2156 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2157 break;
2158 case DW_OP_bregx:
2160 unsigned r = val1->v.val_unsigned;
2161 if (for_eh_or_skip >= 0)
2162 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2163 gcc_assert (size_of_uleb128 (r)
2164 == size_of_uleb128 (val1->v.val_unsigned));
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2168 break;
2169 case DW_OP_piece:
2170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2171 break;
2172 case DW_OP_bit_piece:
2173 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2174 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2175 break;
2176 case DW_OP_deref_size:
2177 case DW_OP_xderef_size:
2178 dw2_asm_output_data (1, val1->v.val_int, NULL);
2179 break;
2181 case DW_OP_addr:
2182 if (loc->dtprel)
2184 if (targetm.asm_out.output_dwarf_dtprel)
2186 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2187 DWARF2_ADDR_SIZE,
2188 val1->v.val_addr);
2189 fputc ('\n', asm_out_file);
2191 else
2192 gcc_unreachable ();
2194 else
2196 #ifdef DWARF2_DEBUGGING_INFO
2197 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2198 #else
2199 gcc_unreachable ();
2200 #endif
2202 break;
2204 case DW_OP_GNU_addr_index:
2205 case DW_OP_GNU_const_index:
2206 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2207 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2208 "(index into .debug_addr)");
2209 break;
2211 case DW_OP_call2:
2212 case DW_OP_call4:
2214 unsigned long die_offset
2215 = get_ref_die_offset (val1->v.val_die_ref.die);
2216 /* Make sure the offset has been computed and that we can encode it as
2217 an operand. */
2218 gcc_assert (die_offset > 0
2219 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2220 ? 0xffff
2221 : 0xffffffff));
2222 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2223 die_offset, NULL);
2225 break;
2227 case DW_OP_call_ref:
2228 case DW_OP_GNU_variable_value:
2230 char label[MAX_ARTIFICIAL_LABEL_BYTES
2231 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2232 gcc_assert (val1->val_class == dw_val_class_die_ref);
2233 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2234 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2236 break;
2238 case DW_OP_implicit_pointer:
2239 case DW_OP_GNU_implicit_pointer:
2241 char label[MAX_ARTIFICIAL_LABEL_BYTES
2242 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2243 gcc_assert (val1->val_class == dw_val_class_die_ref);
2244 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2245 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2246 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2248 break;
2250 case DW_OP_entry_value:
2251 case DW_OP_GNU_entry_value:
2252 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2253 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2254 break;
2256 case DW_OP_const_type:
2257 case DW_OP_GNU_const_type:
2259 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2260 gcc_assert (o);
2261 dw2_asm_output_data_uleb128 (o, NULL);
2262 switch (val2->val_class)
2264 case dw_val_class_const:
2265 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2266 dw2_asm_output_data (1, l, NULL);
2267 dw2_asm_output_data (l, val2->v.val_int, NULL);
2268 break;
2269 case dw_val_class_vec:
2271 unsigned int elt_size = val2->v.val_vec.elt_size;
2272 unsigned int len = val2->v.val_vec.length;
2273 unsigned int i;
2274 unsigned char *p;
2276 l = len * elt_size;
2277 dw2_asm_output_data (1, l, NULL);
2278 if (elt_size > sizeof (HOST_WIDE_INT))
2280 elt_size /= 2;
2281 len *= 2;
2283 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2284 i < len;
2285 i++, p += elt_size)
2286 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2287 "fp or vector constant word %u", i);
2289 break;
2290 case dw_val_class_const_double:
2292 unsigned HOST_WIDE_INT first, second;
2293 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2295 dw2_asm_output_data (1, 2 * l, NULL);
2296 if (WORDS_BIG_ENDIAN)
2298 first = val2->v.val_double.high;
2299 second = val2->v.val_double.low;
2301 else
2303 first = val2->v.val_double.low;
2304 second = val2->v.val_double.high;
2306 dw2_asm_output_data (l, first, NULL);
2307 dw2_asm_output_data (l, second, NULL);
2309 break;
2310 case dw_val_class_wide_int:
2312 int i;
2313 int len = get_full_len (*val2->v.val_wide);
2314 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2316 dw2_asm_output_data (1, len * l, NULL);
2317 if (WORDS_BIG_ENDIAN)
2318 for (i = len - 1; i >= 0; --i)
2319 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2320 else
2321 for (i = 0; i < len; ++i)
2322 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2324 break;
2325 default:
2326 gcc_unreachable ();
2329 break;
2330 case DW_OP_regval_type:
2331 case DW_OP_GNU_regval_type:
2333 unsigned r = val1->v.val_unsigned;
2334 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2335 gcc_assert (o);
2336 if (for_eh_or_skip >= 0)
2338 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2339 gcc_assert (size_of_uleb128 (r)
2340 == size_of_uleb128 (val1->v.val_unsigned));
2342 dw2_asm_output_data_uleb128 (r, NULL);
2343 dw2_asm_output_data_uleb128 (o, NULL);
2345 break;
2346 case DW_OP_deref_type:
2347 case DW_OP_GNU_deref_type:
2349 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2350 gcc_assert (o);
2351 dw2_asm_output_data (1, val1->v.val_int, NULL);
2352 dw2_asm_output_data_uleb128 (o, NULL);
2354 break;
2355 case DW_OP_convert:
2356 case DW_OP_reinterpret:
2357 case DW_OP_GNU_convert:
2358 case DW_OP_GNU_reinterpret:
2359 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2360 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2361 else
2363 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2364 gcc_assert (o);
2365 dw2_asm_output_data_uleb128 (o, NULL);
2367 break;
2369 case DW_OP_GNU_parameter_ref:
2371 unsigned long o;
2372 gcc_assert (val1->val_class == dw_val_class_die_ref);
2373 o = get_ref_die_offset (val1->v.val_die_ref.die);
2374 dw2_asm_output_data (4, o, NULL);
2376 break;
2378 default:
2379 /* Other codes have no operands. */
2380 break;
2384 /* Output a sequence of location operations.
2385 The for_eh_or_skip parameter controls whether register numbers are
2386 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2387 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2388 info). This should be suppressed for the cases that have not been converted
2389 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2391 void
2392 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2394 for (; loc != NULL; loc = loc->dw_loc_next)
2396 enum dwarf_location_atom opc = loc->dw_loc_opc;
2397 /* Output the opcode. */
2398 if (for_eh_or_skip >= 0
2399 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2401 unsigned r = (opc - DW_OP_breg0);
2402 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2403 gcc_assert (r <= 31);
2404 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2406 else if (for_eh_or_skip >= 0
2407 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2409 unsigned r = (opc - DW_OP_reg0);
2410 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2411 gcc_assert (r <= 31);
2412 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2415 dw2_asm_output_data (1, opc,
2416 "%s", dwarf_stack_op_name (opc));
2418 /* Output the operand(s) (if any). */
2419 output_loc_operands (loc, for_eh_or_skip);
2423 /* Output location description stack opcode's operands (if any).
2424 The output is single bytes on a line, suitable for .cfi_escape. */
2426 static void
2427 output_loc_operands_raw (dw_loc_descr_ref loc)
2429 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2430 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2432 switch (loc->dw_loc_opc)
2434 case DW_OP_addr:
2435 case DW_OP_GNU_addr_index:
2436 case DW_OP_GNU_const_index:
2437 case DW_OP_implicit_value:
2438 /* We cannot output addresses in .cfi_escape, only bytes. */
2439 gcc_unreachable ();
2441 case DW_OP_const1u:
2442 case DW_OP_const1s:
2443 case DW_OP_pick:
2444 case DW_OP_deref_size:
2445 case DW_OP_xderef_size:
2446 fputc (',', asm_out_file);
2447 dw2_asm_output_data_raw (1, val1->v.val_int);
2448 break;
2450 case DW_OP_const2u:
2451 case DW_OP_const2s:
2452 fputc (',', asm_out_file);
2453 dw2_asm_output_data_raw (2, val1->v.val_int);
2454 break;
2456 case DW_OP_const4u:
2457 case DW_OP_const4s:
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (4, val1->v.val_int);
2460 break;
2462 case DW_OP_const8u:
2463 case DW_OP_const8s:
2464 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2465 fputc (',', asm_out_file);
2466 dw2_asm_output_data_raw (8, val1->v.val_int);
2467 break;
2469 case DW_OP_skip:
2470 case DW_OP_bra:
2472 int offset;
2474 gcc_assert (val1->val_class == dw_val_class_loc);
2475 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2477 fputc (',', asm_out_file);
2478 dw2_asm_output_data_raw (2, offset);
2480 break;
2482 case DW_OP_regx:
2484 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2485 gcc_assert (size_of_uleb128 (r)
2486 == size_of_uleb128 (val1->v.val_unsigned));
2487 fputc (',', asm_out_file);
2488 dw2_asm_output_data_uleb128_raw (r);
2490 break;
2492 case DW_OP_constu:
2493 case DW_OP_plus_uconst:
2494 case DW_OP_piece:
2495 fputc (',', asm_out_file);
2496 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2497 break;
2499 case DW_OP_bit_piece:
2500 fputc (',', asm_out_file);
2501 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2502 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2503 break;
2505 case DW_OP_consts:
2506 case DW_OP_breg0:
2507 case DW_OP_breg1:
2508 case DW_OP_breg2:
2509 case DW_OP_breg3:
2510 case DW_OP_breg4:
2511 case DW_OP_breg5:
2512 case DW_OP_breg6:
2513 case DW_OP_breg7:
2514 case DW_OP_breg8:
2515 case DW_OP_breg9:
2516 case DW_OP_breg10:
2517 case DW_OP_breg11:
2518 case DW_OP_breg12:
2519 case DW_OP_breg13:
2520 case DW_OP_breg14:
2521 case DW_OP_breg15:
2522 case DW_OP_breg16:
2523 case DW_OP_breg17:
2524 case DW_OP_breg18:
2525 case DW_OP_breg19:
2526 case DW_OP_breg20:
2527 case DW_OP_breg21:
2528 case DW_OP_breg22:
2529 case DW_OP_breg23:
2530 case DW_OP_breg24:
2531 case DW_OP_breg25:
2532 case DW_OP_breg26:
2533 case DW_OP_breg27:
2534 case DW_OP_breg28:
2535 case DW_OP_breg29:
2536 case DW_OP_breg30:
2537 case DW_OP_breg31:
2538 case DW_OP_fbreg:
2539 fputc (',', asm_out_file);
2540 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2541 break;
2543 case DW_OP_bregx:
2545 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2546 gcc_assert (size_of_uleb128 (r)
2547 == size_of_uleb128 (val1->v.val_unsigned));
2548 fputc (',', asm_out_file);
2549 dw2_asm_output_data_uleb128_raw (r);
2550 fputc (',', asm_out_file);
2551 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2553 break;
2555 case DW_OP_implicit_pointer:
2556 case DW_OP_entry_value:
2557 case DW_OP_const_type:
2558 case DW_OP_regval_type:
2559 case DW_OP_deref_type:
2560 case DW_OP_convert:
2561 case DW_OP_reinterpret:
2562 case DW_OP_GNU_implicit_pointer:
2563 case DW_OP_GNU_entry_value:
2564 case DW_OP_GNU_const_type:
2565 case DW_OP_GNU_regval_type:
2566 case DW_OP_GNU_deref_type:
2567 case DW_OP_GNU_convert:
2568 case DW_OP_GNU_reinterpret:
2569 case DW_OP_GNU_parameter_ref:
2570 gcc_unreachable ();
2571 break;
2573 default:
2574 /* Other codes have no operands. */
2575 break;
2579 void
2580 output_loc_sequence_raw (dw_loc_descr_ref loc)
2582 while (1)
2584 enum dwarf_location_atom opc = loc->dw_loc_opc;
2585 /* Output the opcode. */
2586 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2588 unsigned r = (opc - DW_OP_breg0);
2589 r = DWARF2_FRAME_REG_OUT (r, 1);
2590 gcc_assert (r <= 31);
2591 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2593 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2595 unsigned r = (opc - DW_OP_reg0);
2596 r = DWARF2_FRAME_REG_OUT (r, 1);
2597 gcc_assert (r <= 31);
2598 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2600 /* Output the opcode. */
2601 fprintf (asm_out_file, "%#x", opc);
2602 output_loc_operands_raw (loc);
2604 if (!loc->dw_loc_next)
2605 break;
2606 loc = loc->dw_loc_next;
2608 fputc (',', asm_out_file);
2612 /* This function builds a dwarf location descriptor sequence from a
2613 dw_cfa_location, adding the given OFFSET to the result of the
2614 expression. */
2616 struct dw_loc_descr_node *
2617 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2619 struct dw_loc_descr_node *head, *tmp;
2621 offset += cfa->offset;
2623 if (cfa->indirect)
2625 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2626 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2627 head->dw_loc_oprnd1.val_entry = NULL;
2628 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2629 add_loc_descr (&head, tmp);
2630 if (offset != 0)
2632 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2633 add_loc_descr (&head, tmp);
2636 else
2637 head = new_reg_loc_descr (cfa->reg, offset);
2639 return head;
2642 /* This function builds a dwarf location descriptor sequence for
2643 the address at OFFSET from the CFA when stack is aligned to
2644 ALIGNMENT byte. */
2646 struct dw_loc_descr_node *
2647 build_cfa_aligned_loc (dw_cfa_location *cfa,
2648 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2650 struct dw_loc_descr_node *head;
2651 unsigned int dwarf_fp
2652 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2654 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2655 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2657 head = new_reg_loc_descr (dwarf_fp, 0);
2658 add_loc_descr (&head, int_loc_descriptor (alignment));
2659 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2660 loc_descr_plus_const (&head, offset);
2662 else
2663 head = new_reg_loc_descr (dwarf_fp, offset);
2664 return head;
2667 /* And now, the support for symbolic debugging information. */
2669 /* .debug_str support. */
2671 static void dwarf2out_init (const char *);
2672 static void dwarf2out_finish (const char *);
2673 static void dwarf2out_early_finish (const char *);
2674 static void dwarf2out_assembly_start (void);
2675 static void dwarf2out_define (unsigned int, const char *);
2676 static void dwarf2out_undef (unsigned int, const char *);
2677 static void dwarf2out_start_source_file (unsigned, const char *);
2678 static void dwarf2out_end_source_file (unsigned);
2679 static void dwarf2out_function_decl (tree);
2680 static void dwarf2out_begin_block (unsigned, unsigned);
2681 static void dwarf2out_end_block (unsigned, unsigned);
2682 static bool dwarf2out_ignore_block (const_tree);
2683 static void dwarf2out_early_global_decl (tree);
2684 static void dwarf2out_late_global_decl (tree);
2685 static void dwarf2out_type_decl (tree, int);
2686 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2687 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2688 dw_die_ref);
2689 static void dwarf2out_abstract_function (tree);
2690 static void dwarf2out_var_location (rtx_insn *);
2691 static void dwarf2out_size_function (tree);
2692 static void dwarf2out_begin_function (tree);
2693 static void dwarf2out_end_function (unsigned int);
2694 static void dwarf2out_register_main_translation_unit (tree unit);
2695 static void dwarf2out_set_name (tree, tree);
2696 static void dwarf2out_register_external_die (tree decl, const char *sym,
2697 unsigned HOST_WIDE_INT off);
2698 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2699 unsigned HOST_WIDE_INT *off);
2701 /* The debug hooks structure. */
2703 const struct gcc_debug_hooks dwarf2_debug_hooks =
2705 dwarf2out_init,
2706 dwarf2out_finish,
2707 dwarf2out_early_finish,
2708 dwarf2out_assembly_start,
2709 dwarf2out_define,
2710 dwarf2out_undef,
2711 dwarf2out_start_source_file,
2712 dwarf2out_end_source_file,
2713 dwarf2out_begin_block,
2714 dwarf2out_end_block,
2715 dwarf2out_ignore_block,
2716 dwarf2out_source_line,
2717 dwarf2out_begin_prologue,
2718 #if VMS_DEBUGGING_INFO
2719 dwarf2out_vms_end_prologue,
2720 dwarf2out_vms_begin_epilogue,
2721 #else
2722 debug_nothing_int_charstar,
2723 debug_nothing_int_charstar,
2724 #endif
2725 dwarf2out_end_epilogue,
2726 dwarf2out_begin_function,
2727 dwarf2out_end_function, /* end_function */
2728 dwarf2out_register_main_translation_unit,
2729 dwarf2out_function_decl, /* function_decl */
2730 dwarf2out_early_global_decl,
2731 dwarf2out_late_global_decl,
2732 dwarf2out_type_decl, /* type_decl */
2733 dwarf2out_imported_module_or_decl,
2734 dwarf2out_die_ref_for_decl,
2735 dwarf2out_register_external_die,
2736 debug_nothing_tree, /* deferred_inline_function */
2737 /* The DWARF 2 backend tries to reduce debugging bloat by not
2738 emitting the abstract description of inline functions until
2739 something tries to reference them. */
2740 dwarf2out_abstract_function, /* outlining_inline_function */
2741 debug_nothing_rtx_code_label, /* label */
2742 debug_nothing_int, /* handle_pch */
2743 dwarf2out_var_location,
2744 dwarf2out_size_function, /* size_function */
2745 dwarf2out_switch_text_section,
2746 dwarf2out_set_name,
2747 1, /* start_end_main_source_file */
2748 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2751 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2753 dwarf2out_init,
2754 debug_nothing_charstar,
2755 debug_nothing_charstar,
2756 dwarf2out_assembly_start,
2757 debug_nothing_int_charstar,
2758 debug_nothing_int_charstar,
2759 debug_nothing_int_charstar,
2760 debug_nothing_int,
2761 debug_nothing_int_int, /* begin_block */
2762 debug_nothing_int_int, /* end_block */
2763 debug_true_const_tree, /* ignore_block */
2764 dwarf2out_source_line, /* source_line */
2765 debug_nothing_int_int_charstar, /* begin_prologue */
2766 debug_nothing_int_charstar, /* end_prologue */
2767 debug_nothing_int_charstar, /* begin_epilogue */
2768 debug_nothing_int_charstar, /* end_epilogue */
2769 debug_nothing_tree, /* begin_function */
2770 debug_nothing_int, /* end_function */
2771 debug_nothing_tree, /* register_main_translation_unit */
2772 debug_nothing_tree, /* function_decl */
2773 debug_nothing_tree, /* early_global_decl */
2774 debug_nothing_tree, /* late_global_decl */
2775 debug_nothing_tree_int, /* type_decl */
2776 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2777 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2778 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2779 debug_nothing_tree, /* deferred_inline_function */
2780 debug_nothing_tree, /* outlining_inline_function */
2781 debug_nothing_rtx_code_label, /* label */
2782 debug_nothing_int, /* handle_pch */
2783 debug_nothing_rtx_insn, /* var_location */
2784 debug_nothing_tree, /* size_function */
2785 debug_nothing_void, /* switch_text_section */
2786 debug_nothing_tree_tree, /* set_name */
2787 0, /* start_end_main_source_file */
2788 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2791 /* NOTE: In the comments in this file, many references are made to
2792 "Debugging Information Entries". This term is abbreviated as `DIE'
2793 throughout the remainder of this file. */
2795 /* An internal representation of the DWARF output is built, and then
2796 walked to generate the DWARF debugging info. The walk of the internal
2797 representation is done after the entire program has been compiled.
2798 The types below are used to describe the internal representation. */
2800 /* Whether to put type DIEs into their own section .debug_types instead
2801 of making them part of the .debug_info section. Only supported for
2802 Dwarf V4 or higher and the user didn't disable them through
2803 -fno-debug-types-section. It is more efficient to put them in a
2804 separate comdat sections since the linker will then be able to
2805 remove duplicates. But not all tools support .debug_types sections
2806 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2807 it is DW_UT_type unit type in .debug_info section. */
2809 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2811 /* Various DIE's use offsets relative to the beginning of the
2812 .debug_info section to refer to each other. */
2814 typedef long int dw_offset;
2816 struct comdat_type_node;
2818 /* The entries in the line_info table more-or-less mirror the opcodes
2819 that are used in the real dwarf line table. Arrays of these entries
2820 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2821 supported. */
2823 enum dw_line_info_opcode {
2824 /* Emit DW_LNE_set_address; the operand is the label index. */
2825 LI_set_address,
2827 /* Emit a row to the matrix with the given line. This may be done
2828 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2829 special opcodes. */
2830 LI_set_line,
2832 /* Emit a DW_LNS_set_file. */
2833 LI_set_file,
2835 /* Emit a DW_LNS_set_column. */
2836 LI_set_column,
2838 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2839 LI_negate_stmt,
2841 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2842 LI_set_prologue_end,
2843 LI_set_epilogue_begin,
2845 /* Emit a DW_LNE_set_discriminator. */
2846 LI_set_discriminator
2849 typedef struct GTY(()) dw_line_info_struct {
2850 enum dw_line_info_opcode opcode;
2851 unsigned int val;
2852 } dw_line_info_entry;
2855 struct GTY(()) dw_line_info_table {
2856 /* The label that marks the end of this section. */
2857 const char *end_label;
2859 /* The values for the last row of the matrix, as collected in the table.
2860 These are used to minimize the changes to the next row. */
2861 unsigned int file_num;
2862 unsigned int line_num;
2863 unsigned int column_num;
2864 int discrim_num;
2865 bool is_stmt;
2866 bool in_use;
2868 vec<dw_line_info_entry, va_gc> *entries;
2872 /* Each DIE attribute has a field specifying the attribute kind,
2873 a link to the next attribute in the chain, and an attribute value.
2874 Attributes are typically linked below the DIE they modify. */
2876 typedef struct GTY(()) dw_attr_struct {
2877 enum dwarf_attribute dw_attr;
2878 dw_val_node dw_attr_val;
2880 dw_attr_node;
2883 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2884 The children of each node form a circular list linked by
2885 die_sib. die_child points to the node *before* the "first" child node. */
2887 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2888 union die_symbol_or_type_node
2890 const char * GTY ((tag ("0"))) die_symbol;
2891 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2893 GTY ((desc ("%0.comdat_type_p"))) die_id;
2894 vec<dw_attr_node, va_gc> *die_attr;
2895 dw_die_ref die_parent;
2896 dw_die_ref die_child;
2897 dw_die_ref die_sib;
2898 dw_die_ref die_definition; /* ref from a specification to its definition */
2899 dw_offset die_offset;
2900 unsigned long die_abbrev;
2901 int die_mark;
2902 unsigned int decl_id;
2903 enum dwarf_tag die_tag;
2904 /* Die is used and must not be pruned as unused. */
2905 BOOL_BITFIELD die_perennial_p : 1;
2906 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2907 /* For an external ref to die_symbol if die_offset contains an extra
2908 offset to that symbol. */
2909 BOOL_BITFIELD with_offset : 1;
2910 /* Whether this DIE was removed from the DIE tree, for example via
2911 prune_unused_types. We don't consider those present from the
2912 DIE lookup routines. */
2913 BOOL_BITFIELD removed : 1;
2914 /* Lots of spare bits. */
2916 die_node;
2918 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2919 static bool early_dwarf;
2920 static bool early_dwarf_finished;
2921 struct set_early_dwarf {
2922 bool saved;
2923 set_early_dwarf () : saved(early_dwarf)
2925 gcc_assert (! early_dwarf_finished);
2926 early_dwarf = true;
2928 ~set_early_dwarf () { early_dwarf = saved; }
2931 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2932 #define FOR_EACH_CHILD(die, c, expr) do { \
2933 c = die->die_child; \
2934 if (c) do { \
2935 c = c->die_sib; \
2936 expr; \
2937 } while (c != die->die_child); \
2938 } while (0)
2940 /* The pubname structure */
2942 typedef struct GTY(()) pubname_struct {
2943 dw_die_ref die;
2944 const char *name;
2946 pubname_entry;
2949 struct GTY(()) dw_ranges {
2950 const char *label;
2951 /* If this is positive, it's a block number, otherwise it's a
2952 bitwise-negated index into dw_ranges_by_label. */
2953 int num;
2954 /* Index for the range list for DW_FORM_rnglistx. */
2955 unsigned int idx : 31;
2956 /* True if this range might be possibly in a different section
2957 from previous entry. */
2958 unsigned int maybe_new_sec : 1;
2961 /* A structure to hold a macinfo entry. */
2963 typedef struct GTY(()) macinfo_struct {
2964 unsigned char code;
2965 unsigned HOST_WIDE_INT lineno;
2966 const char *info;
2968 macinfo_entry;
2971 struct GTY(()) dw_ranges_by_label {
2972 const char *begin;
2973 const char *end;
2976 /* The comdat type node structure. */
2977 struct GTY(()) comdat_type_node
2979 dw_die_ref root_die;
2980 dw_die_ref type_die;
2981 dw_die_ref skeleton_die;
2982 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2983 comdat_type_node *next;
2986 /* A list of DIEs for which we can't determine ancestry (parent_die
2987 field) just yet. Later in dwarf2out_finish we will fill in the
2988 missing bits. */
2989 typedef struct GTY(()) limbo_die_struct {
2990 dw_die_ref die;
2991 /* The tree for which this DIE was created. We use this to
2992 determine ancestry later. */
2993 tree created_for;
2994 struct limbo_die_struct *next;
2996 limbo_die_node;
2998 typedef struct skeleton_chain_struct
3000 dw_die_ref old_die;
3001 dw_die_ref new_die;
3002 struct skeleton_chain_struct *parent;
3004 skeleton_chain_node;
3006 /* Define a macro which returns nonzero for a TYPE_DECL which was
3007 implicitly generated for a type.
3009 Note that, unlike the C front-end (which generates a NULL named
3010 TYPE_DECL node for each complete tagged type, each array type,
3011 and each function type node created) the C++ front-end generates
3012 a _named_ TYPE_DECL node for each tagged type node created.
3013 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3014 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3015 front-end, but for each type, tagged or not. */
3017 #define TYPE_DECL_IS_STUB(decl) \
3018 (DECL_NAME (decl) == NULL_TREE \
3019 || (DECL_ARTIFICIAL (decl) \
3020 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3021 /* This is necessary for stub decls that \
3022 appear in nested inline functions. */ \
3023 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3024 && (decl_ultimate_origin (decl) \
3025 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3027 /* Information concerning the compilation unit's programming
3028 language, and compiler version. */
3030 /* Fixed size portion of the DWARF compilation unit header. */
3031 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3032 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3033 + (dwarf_version >= 5 ? 4 : 3))
3035 /* Fixed size portion of the DWARF comdat type unit header. */
3036 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3037 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3038 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3040 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3041 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3042 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3044 /* Fixed size portion of public names info. */
3045 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3047 /* Fixed size portion of the address range info. */
3048 #define DWARF_ARANGES_HEADER_SIZE \
3049 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3050 DWARF2_ADDR_SIZE * 2) \
3051 - DWARF_INITIAL_LENGTH_SIZE)
3053 /* Size of padding portion in the address range info. It must be
3054 aligned to twice the pointer size. */
3055 #define DWARF_ARANGES_PAD_SIZE \
3056 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3057 DWARF2_ADDR_SIZE * 2) \
3058 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3060 /* Use assembler line directives if available. */
3061 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3062 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3063 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3064 #else
3065 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3066 #endif
3067 #endif
3069 /* Minimum line offset in a special line info. opcode.
3070 This value was chosen to give a reasonable range of values. */
3071 #define DWARF_LINE_BASE -10
3073 /* First special line opcode - leave room for the standard opcodes. */
3074 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3076 /* Range of line offsets in a special line info. opcode. */
3077 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3079 /* Flag that indicates the initial value of the is_stmt_start flag.
3080 In the present implementation, we do not mark any lines as
3081 the beginning of a source statement, because that information
3082 is not made available by the GCC front-end. */
3083 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3085 /* Maximum number of operations per instruction bundle. */
3086 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3087 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3088 #endif
3090 /* This location is used by calc_die_sizes() to keep track
3091 the offset of each DIE within the .debug_info section. */
3092 static unsigned long next_die_offset;
3094 /* Record the root of the DIE's built for the current compilation unit. */
3095 static GTY(()) dw_die_ref single_comp_unit_die;
3097 /* A list of type DIEs that have been separated into comdat sections. */
3098 static GTY(()) comdat_type_node *comdat_type_list;
3100 /* A list of CU DIEs that have been separated. */
3101 static GTY(()) limbo_die_node *cu_die_list;
3103 /* A list of DIEs with a NULL parent waiting to be relocated. */
3104 static GTY(()) limbo_die_node *limbo_die_list;
3106 /* A list of DIEs for which we may have to generate
3107 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3108 static GTY(()) limbo_die_node *deferred_asm_name;
3110 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3112 typedef const char *compare_type;
3114 static hashval_t hash (dwarf_file_data *);
3115 static bool equal (dwarf_file_data *, const char *);
3118 /* Filenames referenced by this compilation unit. */
3119 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3121 struct decl_die_hasher : ggc_ptr_hash<die_node>
3123 typedef tree compare_type;
3125 static hashval_t hash (die_node *);
3126 static bool equal (die_node *, tree);
3128 /* A hash table of references to DIE's that describe declarations.
3129 The key is a DECL_UID() which is a unique number identifying each decl. */
3130 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3132 struct GTY ((for_user)) variable_value_struct {
3133 unsigned int decl_id;
3134 vec<dw_die_ref, va_gc> *dies;
3137 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3139 typedef tree compare_type;
3141 static hashval_t hash (variable_value_struct *);
3142 static bool equal (variable_value_struct *, tree);
3144 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3145 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3146 DECL_CONTEXT of the referenced VAR_DECLs. */
3147 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3149 struct block_die_hasher : ggc_ptr_hash<die_struct>
3151 static hashval_t hash (die_struct *);
3152 static bool equal (die_struct *, die_struct *);
3155 /* A hash table of references to DIE's that describe COMMON blocks.
3156 The key is DECL_UID() ^ die_parent. */
3157 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3159 typedef struct GTY(()) die_arg_entry_struct {
3160 dw_die_ref die;
3161 tree arg;
3162 } die_arg_entry;
3165 /* Node of the variable location list. */
3166 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3167 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3168 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3169 in mode of the EXPR_LIST node and first EXPR_LIST operand
3170 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3171 location or NULL for padding. For larger bitsizes,
3172 mode is 0 and first operand is a CONCAT with bitsize
3173 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3174 NULL as second operand. */
3175 rtx GTY (()) loc;
3176 const char * GTY (()) label;
3177 struct var_loc_node * GTY (()) next;
3180 /* Variable location list. */
3181 struct GTY ((for_user)) var_loc_list_def {
3182 struct var_loc_node * GTY (()) first;
3184 /* Pointer to the last but one or last element of the
3185 chained list. If the list is empty, both first and
3186 last are NULL, if the list contains just one node
3187 or the last node certainly is not redundant, it points
3188 to the last node, otherwise points to the last but one.
3189 Do not mark it for GC because it is marked through the chain. */
3190 struct var_loc_node * GTY ((skip ("%h"))) last;
3192 /* Pointer to the last element before section switch,
3193 if NULL, either sections weren't switched or first
3194 is after section switch. */
3195 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3197 /* DECL_UID of the variable decl. */
3198 unsigned int decl_id;
3200 typedef struct var_loc_list_def var_loc_list;
3202 /* Call argument location list. */
3203 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3204 rtx GTY (()) call_arg_loc_note;
3205 const char * GTY (()) label;
3206 tree GTY (()) block;
3207 bool tail_call_p;
3208 rtx GTY (()) symbol_ref;
3209 struct call_arg_loc_node * GTY (()) next;
3213 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3215 typedef const_tree compare_type;
3217 static hashval_t hash (var_loc_list *);
3218 static bool equal (var_loc_list *, const_tree);
3221 /* Table of decl location linked lists. */
3222 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3224 /* Head and tail of call_arg_loc chain. */
3225 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3226 static struct call_arg_loc_node *call_arg_loc_last;
3228 /* Number of call sites in the current function. */
3229 static int call_site_count = -1;
3230 /* Number of tail call sites in the current function. */
3231 static int tail_call_site_count = -1;
3233 /* A cached location list. */
3234 struct GTY ((for_user)) cached_dw_loc_list_def {
3235 /* The DECL_UID of the decl that this entry describes. */
3236 unsigned int decl_id;
3238 /* The cached location list. */
3239 dw_loc_list_ref loc_list;
3241 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3243 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3246 typedef const_tree compare_type;
3248 static hashval_t hash (cached_dw_loc_list *);
3249 static bool equal (cached_dw_loc_list *, const_tree);
3252 /* Table of cached location lists. */
3253 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3255 /* A vector of references to DIE's that are uniquely identified by their tag,
3256 presence/absence of children DIE's, and list of attribute/value pairs. */
3257 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3259 /* A hash map to remember the stack usage for DWARF procedures. The value
3260 stored is the stack size difference between before the DWARF procedure
3261 invokation and after it returned. In other words, for a DWARF procedure
3262 that consumes N stack slots and that pushes M ones, this stores M - N. */
3263 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3265 /* A global counter for generating labels for line number data. */
3266 static unsigned int line_info_label_num;
3268 /* The current table to which we should emit line number information
3269 for the current function. This will be set up at the beginning of
3270 assembly for the function. */
3271 static GTY(()) dw_line_info_table *cur_line_info_table;
3273 /* The two default tables of line number info. */
3274 static GTY(()) dw_line_info_table *text_section_line_info;
3275 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3277 /* The set of all non-default tables of line number info. */
3278 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3280 /* A flag to tell pubnames/types export if there is an info section to
3281 refer to. */
3282 static bool info_section_emitted;
3284 /* A pointer to the base of a table that contains a list of publicly
3285 accessible names. */
3286 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3288 /* A pointer to the base of a table that contains a list of publicly
3289 accessible types. */
3290 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3292 /* A pointer to the base of a table that contains a list of macro
3293 defines/undefines (and file start/end markers). */
3294 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3296 /* True if .debug_macinfo or .debug_macros section is going to be
3297 emitted. */
3298 #define have_macinfo \
3299 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3300 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3301 && !macinfo_table->is_empty ())
3303 /* Vector of dies for which we should generate .debug_ranges info. */
3304 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3306 /* Vector of pairs of labels referenced in ranges_table. */
3307 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3309 /* Whether we have location lists that need outputting */
3310 static GTY(()) bool have_location_lists;
3312 /* Unique label counter. */
3313 static GTY(()) unsigned int loclabel_num;
3315 /* Unique label counter for point-of-call tables. */
3316 static GTY(()) unsigned int poc_label_num;
3318 /* The last file entry emitted by maybe_emit_file(). */
3319 static GTY(()) struct dwarf_file_data * last_emitted_file;
3321 /* Number of internal labels generated by gen_internal_sym(). */
3322 static GTY(()) int label_num;
3324 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3326 /* Instances of generic types for which we need to generate debug
3327 info that describe their generic parameters and arguments. That
3328 generation needs to happen once all types are properly laid out so
3329 we do it at the end of compilation. */
3330 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3332 /* Offset from the "steady-state frame pointer" to the frame base,
3333 within the current function. */
3334 static HOST_WIDE_INT frame_pointer_fb_offset;
3335 static bool frame_pointer_fb_offset_valid;
3337 static vec<dw_die_ref> base_types;
3339 /* Flags to represent a set of attribute classes for attributes that represent
3340 a scalar value (bounds, pointers, ...). */
3341 enum dw_scalar_form
3343 dw_scalar_form_constant = 0x01,
3344 dw_scalar_form_exprloc = 0x02,
3345 dw_scalar_form_reference = 0x04
3348 /* Forward declarations for functions defined in this file. */
3350 static int is_pseudo_reg (const_rtx);
3351 static tree type_main_variant (tree);
3352 static int is_tagged_type (const_tree);
3353 static const char *dwarf_tag_name (unsigned);
3354 static const char *dwarf_attr_name (unsigned);
3355 static const char *dwarf_form_name (unsigned);
3356 static tree decl_ultimate_origin (const_tree);
3357 static tree decl_class_context (tree);
3358 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3359 static inline enum dw_val_class AT_class (dw_attr_node *);
3360 static inline unsigned int AT_index (dw_attr_node *);
3361 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3362 static inline unsigned AT_flag (dw_attr_node *);
3363 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3364 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3365 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3366 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3367 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3368 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3369 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3370 unsigned int, unsigned char *);
3371 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3372 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3373 static inline const char *AT_string (dw_attr_node *);
3374 static enum dwarf_form AT_string_form (dw_attr_node *);
3375 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3376 static void add_AT_specification (dw_die_ref, dw_die_ref);
3377 static inline dw_die_ref AT_ref (dw_attr_node *);
3378 static inline int AT_ref_external (dw_attr_node *);
3379 static inline void set_AT_ref_external (dw_attr_node *, int);
3380 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3381 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3382 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3383 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3384 dw_loc_list_ref);
3385 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3386 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3387 static void remove_addr_table_entry (addr_table_entry *);
3388 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3389 static inline rtx AT_addr (dw_attr_node *);
3390 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3391 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3392 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3393 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3394 const char *);
3395 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3396 unsigned HOST_WIDE_INT);
3397 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3398 unsigned long, bool);
3399 static inline const char *AT_lbl (dw_attr_node *);
3400 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3401 static const char *get_AT_low_pc (dw_die_ref);
3402 static const char *get_AT_hi_pc (dw_die_ref);
3403 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3404 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3405 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3406 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3407 static bool is_cxx (void);
3408 static bool is_cxx (const_tree);
3409 static bool is_fortran (void);
3410 static bool is_ada (void);
3411 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3412 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3413 static void add_child_die (dw_die_ref, dw_die_ref);
3414 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3415 static dw_die_ref lookup_type_die (tree);
3416 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3417 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3418 static void equate_type_number_to_die (tree, dw_die_ref);
3419 static dw_die_ref lookup_decl_die (tree);
3420 static var_loc_list *lookup_decl_loc (const_tree);
3421 static void equate_decl_number_to_die (tree, dw_die_ref);
3422 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3423 static void print_spaces (FILE *);
3424 static void print_die (dw_die_ref, FILE *);
3425 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3426 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3427 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3428 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3429 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3430 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3431 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3432 struct md5_ctx *, int *);
3433 struct checksum_attributes;
3434 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3435 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3436 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3437 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3438 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3439 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3440 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3441 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3442 static int is_type_die (dw_die_ref);
3443 static int is_comdat_die (dw_die_ref);
3444 static inline bool is_template_instantiation (dw_die_ref);
3445 static int is_declaration_die (dw_die_ref);
3446 static int should_move_die_to_comdat (dw_die_ref);
3447 static dw_die_ref clone_as_declaration (dw_die_ref);
3448 static dw_die_ref clone_die (dw_die_ref);
3449 static dw_die_ref clone_tree (dw_die_ref);
3450 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3451 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3452 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3453 static dw_die_ref generate_skeleton (dw_die_ref);
3454 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3455 dw_die_ref,
3456 dw_die_ref);
3457 static void break_out_comdat_types (dw_die_ref);
3458 static void copy_decls_for_unworthy_types (dw_die_ref);
3460 static void add_sibling_attributes (dw_die_ref);
3461 static void output_location_lists (dw_die_ref);
3462 static int constant_size (unsigned HOST_WIDE_INT);
3463 static unsigned long size_of_die (dw_die_ref);
3464 static void calc_die_sizes (dw_die_ref);
3465 static void calc_base_type_die_sizes (void);
3466 static void mark_dies (dw_die_ref);
3467 static void unmark_dies (dw_die_ref);
3468 static void unmark_all_dies (dw_die_ref);
3469 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3470 static unsigned long size_of_aranges (void);
3471 static enum dwarf_form value_format (dw_attr_node *);
3472 static void output_value_format (dw_attr_node *);
3473 static void output_abbrev_section (void);
3474 static void output_die_abbrevs (unsigned long, dw_die_ref);
3475 static void output_die (dw_die_ref);
3476 static void output_compilation_unit_header (enum dwarf_unit_type);
3477 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3478 static void output_comdat_type_unit (comdat_type_node *);
3479 static const char *dwarf2_name (tree, int);
3480 static void add_pubname (tree, dw_die_ref);
3481 static void add_enumerator_pubname (const char *, dw_die_ref);
3482 static void add_pubname_string (const char *, dw_die_ref);
3483 static void add_pubtype (tree, dw_die_ref);
3484 static void output_pubnames (vec<pubname_entry, va_gc> *);
3485 static void output_aranges (void);
3486 static unsigned int add_ranges (const_tree, bool = false);
3487 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3488 bool *, bool);
3489 static void output_ranges (void);
3490 static dw_line_info_table *new_line_info_table (void);
3491 static void output_line_info (bool);
3492 static void output_file_names (void);
3493 static dw_die_ref base_type_die (tree, bool);
3494 static int is_base_type (tree);
3495 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3496 static int decl_quals (const_tree);
3497 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3498 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3499 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3500 static int type_is_enum (const_tree);
3501 static unsigned int dbx_reg_number (const_rtx);
3502 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3503 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3504 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3505 enum var_init_status);
3506 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3507 enum var_init_status);
3508 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3509 enum var_init_status);
3510 static int is_based_loc (const_rtx);
3511 static bool resolve_one_addr (rtx *);
3512 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3513 enum var_init_status);
3514 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3515 enum var_init_status);
3516 struct loc_descr_context;
3517 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3518 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3519 static dw_loc_list_ref loc_list_from_tree (tree, int,
3520 struct loc_descr_context *);
3521 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3522 struct loc_descr_context *);
3523 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3524 static tree field_type (const_tree);
3525 static unsigned int simple_type_align_in_bits (const_tree);
3526 static unsigned int simple_decl_align_in_bits (const_tree);
3527 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3528 struct vlr_context;
3529 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3530 HOST_WIDE_INT *);
3531 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3532 dw_loc_list_ref);
3533 static void add_data_member_location_attribute (dw_die_ref, tree,
3534 struct vlr_context *);
3535 static bool add_const_value_attribute (dw_die_ref, rtx);
3536 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3537 static void insert_wide_int (const wide_int &, unsigned char *, int);
3538 static void insert_float (const_rtx, unsigned char *);
3539 static rtx rtl_for_decl_location (tree);
3540 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3541 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3542 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3543 static void add_name_attribute (dw_die_ref, const char *);
3544 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3545 static void add_comp_dir_attribute (dw_die_ref);
3546 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3547 struct loc_descr_context *);
3548 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3549 struct loc_descr_context *);
3550 static void add_subscript_info (dw_die_ref, tree, bool);
3551 static void add_byte_size_attribute (dw_die_ref, tree);
3552 static void add_alignment_attribute (dw_die_ref, tree);
3553 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3554 struct vlr_context *);
3555 static void add_bit_size_attribute (dw_die_ref, tree);
3556 static void add_prototyped_attribute (dw_die_ref, tree);
3557 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3558 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3559 static void add_src_coords_attributes (dw_die_ref, tree);
3560 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3561 static void add_discr_value (dw_die_ref, dw_discr_value *);
3562 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3563 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3564 static void push_decl_scope (tree);
3565 static void pop_decl_scope (void);
3566 static dw_die_ref scope_die_for (tree, dw_die_ref);
3567 static inline int local_scope_p (dw_die_ref);
3568 static inline int class_scope_p (dw_die_ref);
3569 static inline int class_or_namespace_scope_p (dw_die_ref);
3570 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3571 static void add_calling_convention_attribute (dw_die_ref, tree);
3572 static const char *type_tag (const_tree);
3573 static tree member_declared_type (const_tree);
3574 #if 0
3575 static const char *decl_start_label (tree);
3576 #endif
3577 static void gen_array_type_die (tree, dw_die_ref);
3578 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3579 #if 0
3580 static void gen_entry_point_die (tree, dw_die_ref);
3581 #endif
3582 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3583 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3584 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3585 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3586 static void gen_formal_types_die (tree, dw_die_ref);
3587 static void gen_subprogram_die (tree, dw_die_ref);
3588 static void gen_variable_die (tree, tree, dw_die_ref);
3589 static void gen_const_die (tree, dw_die_ref);
3590 static void gen_label_die (tree, dw_die_ref);
3591 static void gen_lexical_block_die (tree, dw_die_ref);
3592 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3593 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3594 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3595 static dw_die_ref gen_compile_unit_die (const char *);
3596 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3597 static void gen_member_die (tree, dw_die_ref);
3598 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3599 enum debug_info_usage);
3600 static void gen_subroutine_type_die (tree, dw_die_ref);
3601 static void gen_typedef_die (tree, dw_die_ref);
3602 static void gen_type_die (tree, dw_die_ref);
3603 static void gen_block_die (tree, dw_die_ref);
3604 static void decls_for_scope (tree, dw_die_ref);
3605 static bool is_naming_typedef_decl (const_tree);
3606 static inline dw_die_ref get_context_die (tree);
3607 static void gen_namespace_die (tree, dw_die_ref);
3608 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3609 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3610 static dw_die_ref force_decl_die (tree);
3611 static dw_die_ref force_type_die (tree);
3612 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3613 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3614 static struct dwarf_file_data * lookup_filename (const char *);
3615 static void retry_incomplete_types (void);
3616 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3617 static void gen_generic_params_dies (tree);
3618 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3619 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3620 static void splice_child_die (dw_die_ref, dw_die_ref);
3621 static int file_info_cmp (const void *, const void *);
3622 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3623 const char *, const char *);
3624 static void output_loc_list (dw_loc_list_ref);
3625 static char *gen_internal_sym (const char *);
3626 static bool want_pubnames (void);
3628 static void prune_unmark_dies (dw_die_ref);
3629 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3630 static void prune_unused_types_mark (dw_die_ref, int);
3631 static void prune_unused_types_walk (dw_die_ref);
3632 static void prune_unused_types_walk_attribs (dw_die_ref);
3633 static void prune_unused_types_prune (dw_die_ref);
3634 static void prune_unused_types (void);
3635 static int maybe_emit_file (struct dwarf_file_data *fd);
3636 static inline const char *AT_vms_delta1 (dw_attr_node *);
3637 static inline const char *AT_vms_delta2 (dw_attr_node *);
3638 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3639 const char *, const char *);
3640 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3641 static void gen_remaining_tmpl_value_param_die_attribute (void);
3642 static bool generic_type_p (tree);
3643 static void schedule_generic_params_dies_gen (tree t);
3644 static void gen_scheduled_generic_parms_dies (void);
3645 static void resolve_variable_values (void);
3647 static const char *comp_dir_string (void);
3649 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3651 /* enum for tracking thread-local variables whose address is really an offset
3652 relative to the TLS pointer, which will need link-time relocation, but will
3653 not need relocation by the DWARF consumer. */
3655 enum dtprel_bool
3657 dtprel_false = 0,
3658 dtprel_true = 1
3661 /* Return the operator to use for an address of a variable. For dtprel_true, we
3662 use DW_OP_const*. For regular variables, which need both link-time
3663 relocation and consumer-level relocation (e.g., to account for shared objects
3664 loaded at a random address), we use DW_OP_addr*. */
3666 static inline enum dwarf_location_atom
3667 dw_addr_op (enum dtprel_bool dtprel)
3669 if (dtprel == dtprel_true)
3670 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3671 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3672 else
3673 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3676 /* Return a pointer to a newly allocated address location description. If
3677 dwarf_split_debug_info is true, then record the address with the appropriate
3678 relocation. */
3679 static inline dw_loc_descr_ref
3680 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3682 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3684 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3685 ref->dw_loc_oprnd1.v.val_addr = addr;
3686 ref->dtprel = dtprel;
3687 if (dwarf_split_debug_info)
3688 ref->dw_loc_oprnd1.val_entry
3689 = add_addr_table_entry (addr,
3690 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3691 else
3692 ref->dw_loc_oprnd1.val_entry = NULL;
3694 return ref;
3697 /* Section names used to hold DWARF debugging information. */
3699 #ifndef DEBUG_INFO_SECTION
3700 #define DEBUG_INFO_SECTION ".debug_info"
3701 #endif
3702 #ifndef DEBUG_DWO_INFO_SECTION
3703 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3704 #endif
3705 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3706 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3707 #endif
3708 #ifndef DEBUG_LTO_INFO_SECTION
3709 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3710 #endif
3711 #ifndef DEBUG_ABBREV_SECTION
3712 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3713 #endif
3714 #ifndef DEBUG_DWO_ABBREV_SECTION
3715 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3716 #endif
3717 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3718 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3719 #endif
3720 #ifndef DEBUG_LTO_ABBREV_SECTION
3721 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3722 #endif
3723 #ifndef DEBUG_ARANGES_SECTION
3724 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3725 #endif
3726 #ifndef DEBUG_ADDR_SECTION
3727 #define DEBUG_ADDR_SECTION ".debug_addr"
3728 #endif
3729 #ifndef DEBUG_MACINFO_SECTION
3730 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3731 #endif
3732 #ifndef DEBUG_DWO_MACINFO_SECTION
3733 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3734 #endif
3735 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3736 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3737 #endif
3738 #ifndef DEBUG_LTO_MACINFO_SECTION
3739 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3740 #endif
3741 #ifndef DEBUG_DWO_MACRO_SECTION
3742 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3743 #endif
3744 #ifndef DEBUG_MACRO_SECTION
3745 #define DEBUG_MACRO_SECTION ".debug_macro"
3746 #endif
3747 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3748 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3749 #endif
3750 #ifndef DEBUG_LTO_MACRO_SECTION
3751 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3752 #endif
3753 #ifndef DEBUG_LINE_SECTION
3754 #define DEBUG_LINE_SECTION ".debug_line"
3755 #endif
3756 #ifndef DEBUG_DWO_LINE_SECTION
3757 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3758 #endif
3759 #ifndef DEBUG_LTO_LINE_SECTION
3760 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3761 #endif
3762 #ifndef DEBUG_LOC_SECTION
3763 #define DEBUG_LOC_SECTION ".debug_loc"
3764 #endif
3765 #ifndef DEBUG_DWO_LOC_SECTION
3766 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3767 #endif
3768 #ifndef DEBUG_LOCLISTS_SECTION
3769 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3770 #endif
3771 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3772 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3773 #endif
3774 #ifndef DEBUG_PUBNAMES_SECTION
3775 #define DEBUG_PUBNAMES_SECTION \
3776 ((debug_generate_pub_sections == 2) \
3777 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3778 #endif
3779 #ifndef DEBUG_PUBTYPES_SECTION
3780 #define DEBUG_PUBTYPES_SECTION \
3781 ((debug_generate_pub_sections == 2) \
3782 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3783 #endif
3784 #ifndef DEBUG_STR_OFFSETS_SECTION
3785 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3786 #endif
3787 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3788 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3789 #endif
3790 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3791 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3792 #endif
3793 #ifndef DEBUG_STR_DWO_SECTION
3794 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3795 #endif
3796 #ifndef DEBUG_LTO_STR_DWO_SECTION
3797 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3798 #endif
3799 #ifndef DEBUG_STR_SECTION
3800 #define DEBUG_STR_SECTION ".debug_str"
3801 #endif
3802 #ifndef DEBUG_LTO_STR_SECTION
3803 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3804 #endif
3805 #ifndef DEBUG_RANGES_SECTION
3806 #define DEBUG_RANGES_SECTION ".debug_ranges"
3807 #endif
3808 #ifndef DEBUG_RNGLISTS_SECTION
3809 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3810 #endif
3811 #ifndef DEBUG_LINE_STR_SECTION
3812 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3813 #endif
3815 /* Standard ELF section names for compiled code and data. */
3816 #ifndef TEXT_SECTION_NAME
3817 #define TEXT_SECTION_NAME ".text"
3818 #endif
3820 /* Section flags for .debug_str section. */
3821 #define DEBUG_STR_SECTION_FLAGS \
3822 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3823 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3824 : SECTION_DEBUG)
3826 /* Section flags for .debug_str.dwo section. */
3827 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3829 /* Attribute used to refer to the macro section. */
3830 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3831 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3833 /* Labels we insert at beginning sections we can reference instead of
3834 the section names themselves. */
3836 #ifndef TEXT_SECTION_LABEL
3837 #define TEXT_SECTION_LABEL "Ltext"
3838 #endif
3839 #ifndef COLD_TEXT_SECTION_LABEL
3840 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3841 #endif
3842 #ifndef DEBUG_LINE_SECTION_LABEL
3843 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3844 #endif
3845 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3846 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3847 #endif
3848 #ifndef DEBUG_INFO_SECTION_LABEL
3849 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3850 #endif
3851 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3852 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3853 #endif
3854 #ifndef DEBUG_ABBREV_SECTION_LABEL
3855 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3856 #endif
3857 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3858 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3859 #endif
3860 #ifndef DEBUG_ADDR_SECTION_LABEL
3861 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3862 #endif
3863 #ifndef DEBUG_LOC_SECTION_LABEL
3864 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3865 #endif
3866 #ifndef DEBUG_RANGES_SECTION_LABEL
3867 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3868 #endif
3869 #ifndef DEBUG_MACINFO_SECTION_LABEL
3870 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3871 #endif
3872 #ifndef DEBUG_MACRO_SECTION_LABEL
3873 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3874 #endif
3875 #define SKELETON_COMP_DIE_ABBREV 1
3876 #define SKELETON_TYPE_DIE_ABBREV 2
3878 /* Definitions of defaults for formats and names of various special
3879 (artificial) labels which may be generated within this file (when the -g
3880 options is used and DWARF2_DEBUGGING_INFO is in effect.
3881 If necessary, these may be overridden from within the tm.h file, but
3882 typically, overriding these defaults is unnecessary. */
3884 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3885 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3886 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3887 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3888 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3889 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3890 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3891 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3892 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3893 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3894 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3895 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3900 #ifndef TEXT_END_LABEL
3901 #define TEXT_END_LABEL "Letext"
3902 #endif
3903 #ifndef COLD_END_LABEL
3904 #define COLD_END_LABEL "Letext_cold"
3905 #endif
3906 #ifndef BLOCK_BEGIN_LABEL
3907 #define BLOCK_BEGIN_LABEL "LBB"
3908 #endif
3909 #ifndef BLOCK_END_LABEL
3910 #define BLOCK_END_LABEL "LBE"
3911 #endif
3912 #ifndef LINE_CODE_LABEL
3913 #define LINE_CODE_LABEL "LM"
3914 #endif
3917 /* Return the root of the DIE's built for the current compilation unit. */
3918 static dw_die_ref
3919 comp_unit_die (void)
3921 if (!single_comp_unit_die)
3922 single_comp_unit_die = gen_compile_unit_die (NULL);
3923 return single_comp_unit_die;
3926 /* We allow a language front-end to designate a function that is to be
3927 called to "demangle" any name before it is put into a DIE. */
3929 static const char *(*demangle_name_func) (const char *);
3931 void
3932 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3934 demangle_name_func = func;
3937 /* Test if rtl node points to a pseudo register. */
3939 static inline int
3940 is_pseudo_reg (const_rtx rtl)
3942 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3943 || (GET_CODE (rtl) == SUBREG
3944 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3947 /* Return a reference to a type, with its const and volatile qualifiers
3948 removed. */
3950 static inline tree
3951 type_main_variant (tree type)
3953 type = TYPE_MAIN_VARIANT (type);
3955 /* ??? There really should be only one main variant among any group of
3956 variants of a given type (and all of the MAIN_VARIANT values for all
3957 members of the group should point to that one type) but sometimes the C
3958 front-end messes this up for array types, so we work around that bug
3959 here. */
3960 if (TREE_CODE (type) == ARRAY_TYPE)
3961 while (type != TYPE_MAIN_VARIANT (type))
3962 type = TYPE_MAIN_VARIANT (type);
3964 return type;
3967 /* Return nonzero if the given type node represents a tagged type. */
3969 static inline int
3970 is_tagged_type (const_tree type)
3972 enum tree_code code = TREE_CODE (type);
3974 return (code == RECORD_TYPE || code == UNION_TYPE
3975 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3978 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3980 static void
3981 get_ref_die_offset_label (char *label, dw_die_ref ref)
3983 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3986 /* Return die_offset of a DIE reference to a base type. */
3988 static unsigned long int
3989 get_base_type_offset (dw_die_ref ref)
3991 if (ref->die_offset)
3992 return ref->die_offset;
3993 if (comp_unit_die ()->die_abbrev)
3995 calc_base_type_die_sizes ();
3996 gcc_assert (ref->die_offset);
3998 return ref->die_offset;
4001 /* Return die_offset of a DIE reference other than base type. */
4003 static unsigned long int
4004 get_ref_die_offset (dw_die_ref ref)
4006 gcc_assert (ref->die_offset);
4007 return ref->die_offset;
4010 /* Convert a DIE tag into its string name. */
4012 static const char *
4013 dwarf_tag_name (unsigned int tag)
4015 const char *name = get_DW_TAG_name (tag);
4017 if (name != NULL)
4018 return name;
4020 return "DW_TAG_<unknown>";
4023 /* Convert a DWARF attribute code into its string name. */
4025 static const char *
4026 dwarf_attr_name (unsigned int attr)
4028 const char *name;
4030 switch (attr)
4032 #if VMS_DEBUGGING_INFO
4033 case DW_AT_HP_prologue:
4034 return "DW_AT_HP_prologue";
4035 #else
4036 case DW_AT_MIPS_loop_unroll_factor:
4037 return "DW_AT_MIPS_loop_unroll_factor";
4038 #endif
4040 #if VMS_DEBUGGING_INFO
4041 case DW_AT_HP_epilogue:
4042 return "DW_AT_HP_epilogue";
4043 #else
4044 case DW_AT_MIPS_stride:
4045 return "DW_AT_MIPS_stride";
4046 #endif
4049 name = get_DW_AT_name (attr);
4051 if (name != NULL)
4052 return name;
4054 return "DW_AT_<unknown>";
4057 /* Convert a DWARF value form code into its string name. */
4059 static const char *
4060 dwarf_form_name (unsigned int form)
4062 const char *name = get_DW_FORM_name (form);
4064 if (name != NULL)
4065 return name;
4067 return "DW_FORM_<unknown>";
4070 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4071 instance of an inlined instance of a decl which is local to an inline
4072 function, so we have to trace all of the way back through the origin chain
4073 to find out what sort of node actually served as the original seed for the
4074 given block. */
4076 static tree
4077 decl_ultimate_origin (const_tree decl)
4079 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4080 return NULL_TREE;
4082 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4083 we're trying to output the abstract instance of this function. */
4084 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4085 return NULL_TREE;
4087 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4088 most distant ancestor, this should never happen. */
4089 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4091 return DECL_ABSTRACT_ORIGIN (decl);
4094 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4095 of a virtual function may refer to a base class, so we check the 'this'
4096 parameter. */
4098 static tree
4099 decl_class_context (tree decl)
4101 tree context = NULL_TREE;
4103 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4104 context = DECL_CONTEXT (decl);
4105 else
4106 context = TYPE_MAIN_VARIANT
4107 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4109 if (context && !TYPE_P (context))
4110 context = NULL_TREE;
4112 return context;
4115 /* Add an attribute/value pair to a DIE. */
4117 static inline void
4118 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4120 /* Maybe this should be an assert? */
4121 if (die == NULL)
4122 return;
4124 if (flag_checking)
4126 /* Check we do not add duplicate attrs. Can't use get_AT here
4127 because that recurses to the specification/abstract origin DIE. */
4128 dw_attr_node *a;
4129 unsigned ix;
4130 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4131 gcc_assert (a->dw_attr != attr->dw_attr);
4134 vec_safe_reserve (die->die_attr, 1);
4135 vec_safe_push (die->die_attr, *attr);
4138 static inline enum dw_val_class
4139 AT_class (dw_attr_node *a)
4141 return a->dw_attr_val.val_class;
4144 /* Return the index for any attribute that will be referenced with a
4145 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4146 are stored in dw_attr_val.v.val_str for reference counting
4147 pruning. */
4149 static inline unsigned int
4150 AT_index (dw_attr_node *a)
4152 if (AT_class (a) == dw_val_class_str)
4153 return a->dw_attr_val.v.val_str->index;
4154 else if (a->dw_attr_val.val_entry != NULL)
4155 return a->dw_attr_val.val_entry->index;
4156 return NOT_INDEXED;
4159 /* Add a flag value attribute to a DIE. */
4161 static inline void
4162 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4164 dw_attr_node attr;
4166 attr.dw_attr = attr_kind;
4167 attr.dw_attr_val.val_class = dw_val_class_flag;
4168 attr.dw_attr_val.val_entry = NULL;
4169 attr.dw_attr_val.v.val_flag = flag;
4170 add_dwarf_attr (die, &attr);
4173 static inline unsigned
4174 AT_flag (dw_attr_node *a)
4176 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4177 return a->dw_attr_val.v.val_flag;
4180 /* Add a signed integer attribute value to a DIE. */
4182 static inline void
4183 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4185 dw_attr_node attr;
4187 attr.dw_attr = attr_kind;
4188 attr.dw_attr_val.val_class = dw_val_class_const;
4189 attr.dw_attr_val.val_entry = NULL;
4190 attr.dw_attr_val.v.val_int = int_val;
4191 add_dwarf_attr (die, &attr);
4194 static inline HOST_WIDE_INT
4195 AT_int (dw_attr_node *a)
4197 gcc_assert (a && (AT_class (a) == dw_val_class_const
4198 || AT_class (a) == dw_val_class_const_implicit));
4199 return a->dw_attr_val.v.val_int;
4202 /* Add an unsigned integer attribute value to a DIE. */
4204 static inline void
4205 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4206 unsigned HOST_WIDE_INT unsigned_val)
4208 dw_attr_node attr;
4210 attr.dw_attr = attr_kind;
4211 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4212 attr.dw_attr_val.val_entry = NULL;
4213 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4214 add_dwarf_attr (die, &attr);
4217 static inline unsigned HOST_WIDE_INT
4218 AT_unsigned (dw_attr_node *a)
4220 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4221 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4222 return a->dw_attr_val.v.val_unsigned;
4225 /* Add an unsigned wide integer attribute value to a DIE. */
4227 static inline void
4228 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4229 const wide_int& w)
4231 dw_attr_node attr;
4233 attr.dw_attr = attr_kind;
4234 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4235 attr.dw_attr_val.val_entry = NULL;
4236 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4237 *attr.dw_attr_val.v.val_wide = w;
4238 add_dwarf_attr (die, &attr);
4241 /* Add an unsigned double integer attribute value to a DIE. */
4243 static inline void
4244 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4245 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4247 dw_attr_node attr;
4249 attr.dw_attr = attr_kind;
4250 attr.dw_attr_val.val_class = dw_val_class_const_double;
4251 attr.dw_attr_val.val_entry = NULL;
4252 attr.dw_attr_val.v.val_double.high = high;
4253 attr.dw_attr_val.v.val_double.low = low;
4254 add_dwarf_attr (die, &attr);
4257 /* Add a floating point attribute value to a DIE and return it. */
4259 static inline void
4260 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4261 unsigned int length, unsigned int elt_size, unsigned char *array)
4263 dw_attr_node attr;
4265 attr.dw_attr = attr_kind;
4266 attr.dw_attr_val.val_class = dw_val_class_vec;
4267 attr.dw_attr_val.val_entry = NULL;
4268 attr.dw_attr_val.v.val_vec.length = length;
4269 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4270 attr.dw_attr_val.v.val_vec.array = array;
4271 add_dwarf_attr (die, &attr);
4274 /* Add an 8-byte data attribute value to a DIE. */
4276 static inline void
4277 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4278 unsigned char data8[8])
4280 dw_attr_node attr;
4282 attr.dw_attr = attr_kind;
4283 attr.dw_attr_val.val_class = dw_val_class_data8;
4284 attr.dw_attr_val.val_entry = NULL;
4285 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4286 add_dwarf_attr (die, &attr);
4289 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4290 dwarf_split_debug_info, address attributes in dies destined for the
4291 final executable have force_direct set to avoid using indexed
4292 references. */
4294 static inline void
4295 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4296 bool force_direct)
4298 dw_attr_node attr;
4299 char * lbl_id;
4301 lbl_id = xstrdup (lbl_low);
4302 attr.dw_attr = DW_AT_low_pc;
4303 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4304 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4305 if (dwarf_split_debug_info && !force_direct)
4306 attr.dw_attr_val.val_entry
4307 = add_addr_table_entry (lbl_id, ate_kind_label);
4308 else
4309 attr.dw_attr_val.val_entry = NULL;
4310 add_dwarf_attr (die, &attr);
4312 attr.dw_attr = DW_AT_high_pc;
4313 if (dwarf_version < 4)
4314 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4315 else
4316 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4317 lbl_id = xstrdup (lbl_high);
4318 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4319 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4320 && dwarf_split_debug_info && !force_direct)
4321 attr.dw_attr_val.val_entry
4322 = add_addr_table_entry (lbl_id, ate_kind_label);
4323 else
4324 attr.dw_attr_val.val_entry = NULL;
4325 add_dwarf_attr (die, &attr);
4328 /* Hash and equality functions for debug_str_hash. */
4330 hashval_t
4331 indirect_string_hasher::hash (indirect_string_node *x)
4333 return htab_hash_string (x->str);
4336 bool
4337 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4339 return strcmp (x1->str, x2) == 0;
4342 /* Add STR to the given string hash table. */
4344 static struct indirect_string_node *
4345 find_AT_string_in_table (const char *str,
4346 hash_table<indirect_string_hasher> *table)
4348 struct indirect_string_node *node;
4350 indirect_string_node **slot
4351 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4352 if (*slot == NULL)
4354 node = ggc_cleared_alloc<indirect_string_node> ();
4355 node->str = ggc_strdup (str);
4356 *slot = node;
4358 else
4359 node = *slot;
4361 node->refcount++;
4362 return node;
4365 /* Add STR to the indirect string hash table. */
4367 static struct indirect_string_node *
4368 find_AT_string (const char *str)
4370 if (! debug_str_hash)
4371 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4373 return find_AT_string_in_table (str, debug_str_hash);
4376 /* Add a string attribute value to a DIE. */
4378 static inline void
4379 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4381 dw_attr_node attr;
4382 struct indirect_string_node *node;
4384 node = find_AT_string (str);
4386 attr.dw_attr = attr_kind;
4387 attr.dw_attr_val.val_class = dw_val_class_str;
4388 attr.dw_attr_val.val_entry = NULL;
4389 attr.dw_attr_val.v.val_str = node;
4390 add_dwarf_attr (die, &attr);
4393 static inline const char *
4394 AT_string (dw_attr_node *a)
4396 gcc_assert (a && AT_class (a) == dw_val_class_str);
4397 return a->dw_attr_val.v.val_str->str;
4400 /* Call this function directly to bypass AT_string_form's logic to put
4401 the string inline in the die. */
4403 static void
4404 set_indirect_string (struct indirect_string_node *node)
4406 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4407 /* Already indirect is a no op. */
4408 if (node->form == DW_FORM_strp
4409 || node->form == DW_FORM_line_strp
4410 || node->form == DW_FORM_GNU_str_index)
4412 gcc_assert (node->label);
4413 return;
4415 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4416 ++dw2_string_counter;
4417 node->label = xstrdup (label);
4419 if (!dwarf_split_debug_info)
4421 node->form = DW_FORM_strp;
4422 node->index = NOT_INDEXED;
4424 else
4426 node->form = DW_FORM_GNU_str_index;
4427 node->index = NO_INDEX_ASSIGNED;
4431 /* A helper function for dwarf2out_finish, called to reset indirect
4432 string decisions done for early LTO dwarf output before fat object
4433 dwarf output. */
4436 reset_indirect_string (indirect_string_node **h, void *)
4438 struct indirect_string_node *node = *h;
4439 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4441 free (node->label);
4442 node->label = NULL;
4443 node->form = (dwarf_form) 0;
4444 node->index = 0;
4446 return 1;
4449 /* Find out whether a string should be output inline in DIE
4450 or out-of-line in .debug_str section. */
4452 static enum dwarf_form
4453 find_string_form (struct indirect_string_node *node)
4455 unsigned int len;
4457 if (node->form)
4458 return node->form;
4460 len = strlen (node->str) + 1;
4462 /* If the string is shorter or equal to the size of the reference, it is
4463 always better to put it inline. */
4464 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4465 return node->form = DW_FORM_string;
4467 /* If we cannot expect the linker to merge strings in .debug_str
4468 section, only put it into .debug_str if it is worth even in this
4469 single module. */
4470 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4471 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4472 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4473 return node->form = DW_FORM_string;
4475 set_indirect_string (node);
4477 return node->form;
4480 /* Find out whether the string referenced from the attribute should be
4481 output inline in DIE or out-of-line in .debug_str section. */
4483 static enum dwarf_form
4484 AT_string_form (dw_attr_node *a)
4486 gcc_assert (a && AT_class (a) == dw_val_class_str);
4487 return find_string_form (a->dw_attr_val.v.val_str);
4490 /* Add a DIE reference attribute value to a DIE. */
4492 static inline void
4493 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4495 dw_attr_node attr;
4496 gcc_checking_assert (targ_die != NULL);
4498 /* With LTO we can end up trying to reference something we didn't create
4499 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4500 if (targ_die == NULL)
4501 return;
4503 attr.dw_attr = attr_kind;
4504 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4505 attr.dw_attr_val.val_entry = NULL;
4506 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4507 attr.dw_attr_val.v.val_die_ref.external = 0;
4508 add_dwarf_attr (die, &attr);
4511 /* Change DIE reference REF to point to NEW_DIE instead. */
4513 static inline void
4514 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4516 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4517 ref->dw_attr_val.v.val_die_ref.die = new_die;
4518 ref->dw_attr_val.v.val_die_ref.external = 0;
4521 /* Add an AT_specification attribute to a DIE, and also make the back
4522 pointer from the specification to the definition. */
4524 static inline void
4525 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4527 add_AT_die_ref (die, DW_AT_specification, targ_die);
4528 gcc_assert (!targ_die->die_definition);
4529 targ_die->die_definition = die;
4532 static inline dw_die_ref
4533 AT_ref (dw_attr_node *a)
4535 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4536 return a->dw_attr_val.v.val_die_ref.die;
4539 static inline int
4540 AT_ref_external (dw_attr_node *a)
4542 if (a && AT_class (a) == dw_val_class_die_ref)
4543 return a->dw_attr_val.v.val_die_ref.external;
4545 return 0;
4548 static inline void
4549 set_AT_ref_external (dw_attr_node *a, int i)
4551 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4552 a->dw_attr_val.v.val_die_ref.external = i;
4555 /* Add an FDE reference attribute value to a DIE. */
4557 static inline void
4558 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4560 dw_attr_node attr;
4562 attr.dw_attr = attr_kind;
4563 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4564 attr.dw_attr_val.val_entry = NULL;
4565 attr.dw_attr_val.v.val_fde_index = targ_fde;
4566 add_dwarf_attr (die, &attr);
4569 /* Add a location description attribute value to a DIE. */
4571 static inline void
4572 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4574 dw_attr_node attr;
4576 attr.dw_attr = attr_kind;
4577 attr.dw_attr_val.val_class = dw_val_class_loc;
4578 attr.dw_attr_val.val_entry = NULL;
4579 attr.dw_attr_val.v.val_loc = loc;
4580 add_dwarf_attr (die, &attr);
4583 static inline dw_loc_descr_ref
4584 AT_loc (dw_attr_node *a)
4586 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4587 return a->dw_attr_val.v.val_loc;
4590 static inline void
4591 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4593 dw_attr_node attr;
4595 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4596 return;
4598 attr.dw_attr = attr_kind;
4599 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4600 attr.dw_attr_val.val_entry = NULL;
4601 attr.dw_attr_val.v.val_loc_list = loc_list;
4602 add_dwarf_attr (die, &attr);
4603 have_location_lists = true;
4606 static inline dw_loc_list_ref
4607 AT_loc_list (dw_attr_node *a)
4609 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4610 return a->dw_attr_val.v.val_loc_list;
4613 static inline dw_loc_list_ref *
4614 AT_loc_list_ptr (dw_attr_node *a)
4616 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4617 return &a->dw_attr_val.v.val_loc_list;
4620 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4622 static hashval_t hash (addr_table_entry *);
4623 static bool equal (addr_table_entry *, addr_table_entry *);
4626 /* Table of entries into the .debug_addr section. */
4628 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4630 /* Hash an address_table_entry. */
4632 hashval_t
4633 addr_hasher::hash (addr_table_entry *a)
4635 inchash::hash hstate;
4636 switch (a->kind)
4638 case ate_kind_rtx:
4639 hstate.add_int (0);
4640 break;
4641 case ate_kind_rtx_dtprel:
4642 hstate.add_int (1);
4643 break;
4644 case ate_kind_label:
4645 return htab_hash_string (a->addr.label);
4646 default:
4647 gcc_unreachable ();
4649 inchash::add_rtx (a->addr.rtl, hstate);
4650 return hstate.end ();
4653 /* Determine equality for two address_table_entries. */
4655 bool
4656 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4658 if (a1->kind != a2->kind)
4659 return 0;
4660 switch (a1->kind)
4662 case ate_kind_rtx:
4663 case ate_kind_rtx_dtprel:
4664 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4665 case ate_kind_label:
4666 return strcmp (a1->addr.label, a2->addr.label) == 0;
4667 default:
4668 gcc_unreachable ();
4672 /* Initialize an addr_table_entry. */
4674 void
4675 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4677 e->kind = kind;
4678 switch (kind)
4680 case ate_kind_rtx:
4681 case ate_kind_rtx_dtprel:
4682 e->addr.rtl = (rtx) addr;
4683 break;
4684 case ate_kind_label:
4685 e->addr.label = (char *) addr;
4686 break;
4688 e->refcount = 0;
4689 e->index = NO_INDEX_ASSIGNED;
4692 /* Add attr to the address table entry to the table. Defer setting an
4693 index until output time. */
4695 static addr_table_entry *
4696 add_addr_table_entry (void *addr, enum ate_kind kind)
4698 addr_table_entry *node;
4699 addr_table_entry finder;
4701 gcc_assert (dwarf_split_debug_info);
4702 if (! addr_index_table)
4703 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4704 init_addr_table_entry (&finder, kind, addr);
4705 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4707 if (*slot == HTAB_EMPTY_ENTRY)
4709 node = ggc_cleared_alloc<addr_table_entry> ();
4710 init_addr_table_entry (node, kind, addr);
4711 *slot = node;
4713 else
4714 node = *slot;
4716 node->refcount++;
4717 return node;
4720 /* Remove an entry from the addr table by decrementing its refcount.
4721 Strictly, decrementing the refcount would be enough, but the
4722 assertion that the entry is actually in the table has found
4723 bugs. */
4725 static void
4726 remove_addr_table_entry (addr_table_entry *entry)
4728 gcc_assert (dwarf_split_debug_info && addr_index_table);
4729 /* After an index is assigned, the table is frozen. */
4730 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4731 entry->refcount--;
4734 /* Given a location list, remove all addresses it refers to from the
4735 address_table. */
4737 static void
4738 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4740 for (; descr; descr = descr->dw_loc_next)
4741 if (descr->dw_loc_oprnd1.val_entry != NULL)
4743 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4744 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4748 /* A helper function for dwarf2out_finish called through
4749 htab_traverse. Assign an addr_table_entry its index. All entries
4750 must be collected into the table when this function is called,
4751 because the indexing code relies on htab_traverse to traverse nodes
4752 in the same order for each run. */
4755 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4757 addr_table_entry *node = *h;
4759 /* Don't index unreferenced nodes. */
4760 if (node->refcount == 0)
4761 return 1;
4763 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4764 node->index = *index;
4765 *index += 1;
4767 return 1;
4770 /* Add an address constant attribute value to a DIE. When using
4771 dwarf_split_debug_info, address attributes in dies destined for the
4772 final executable should be direct references--setting the parameter
4773 force_direct ensures this behavior. */
4775 static inline void
4776 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4777 bool force_direct)
4779 dw_attr_node attr;
4781 attr.dw_attr = attr_kind;
4782 attr.dw_attr_val.val_class = dw_val_class_addr;
4783 attr.dw_attr_val.v.val_addr = addr;
4784 if (dwarf_split_debug_info && !force_direct)
4785 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4786 else
4787 attr.dw_attr_val.val_entry = NULL;
4788 add_dwarf_attr (die, &attr);
4791 /* Get the RTX from to an address DIE attribute. */
4793 static inline rtx
4794 AT_addr (dw_attr_node *a)
4796 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4797 return a->dw_attr_val.v.val_addr;
4800 /* Add a file attribute value to a DIE. */
4802 static inline void
4803 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4804 struct dwarf_file_data *fd)
4806 dw_attr_node attr;
4808 attr.dw_attr = attr_kind;
4809 attr.dw_attr_val.val_class = dw_val_class_file;
4810 attr.dw_attr_val.val_entry = NULL;
4811 attr.dw_attr_val.v.val_file = fd;
4812 add_dwarf_attr (die, &attr);
4815 /* Get the dwarf_file_data from a file DIE attribute. */
4817 static inline struct dwarf_file_data *
4818 AT_file (dw_attr_node *a)
4820 gcc_assert (a && (AT_class (a) == dw_val_class_file
4821 || AT_class (a) == dw_val_class_file_implicit));
4822 return a->dw_attr_val.v.val_file;
4825 /* Add a vms delta attribute value to a DIE. */
4827 static inline void
4828 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4829 const char *lbl1, const char *lbl2)
4831 dw_attr_node attr;
4833 attr.dw_attr = attr_kind;
4834 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4835 attr.dw_attr_val.val_entry = NULL;
4836 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4837 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4838 add_dwarf_attr (die, &attr);
4841 /* Add a label identifier attribute value to a DIE. */
4843 static inline void
4844 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4845 const char *lbl_id)
4847 dw_attr_node attr;
4849 attr.dw_attr = attr_kind;
4850 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4851 attr.dw_attr_val.val_entry = NULL;
4852 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4853 if (dwarf_split_debug_info)
4854 attr.dw_attr_val.val_entry
4855 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4856 ate_kind_label);
4857 add_dwarf_attr (die, &attr);
4860 /* Add a section offset attribute value to a DIE, an offset into the
4861 debug_line section. */
4863 static inline void
4864 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4865 const char *label)
4867 dw_attr_node attr;
4869 attr.dw_attr = attr_kind;
4870 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4871 attr.dw_attr_val.val_entry = NULL;
4872 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4873 add_dwarf_attr (die, &attr);
4876 /* Add a section offset attribute value to a DIE, an offset into the
4877 debug_loclists section. */
4879 static inline void
4880 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4881 const char *label)
4883 dw_attr_node attr;
4885 attr.dw_attr = attr_kind;
4886 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4887 attr.dw_attr_val.val_entry = NULL;
4888 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4889 add_dwarf_attr (die, &attr);
4892 /* Add a section offset attribute value to a DIE, an offset into the
4893 debug_macinfo section. */
4895 static inline void
4896 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4897 const char *label)
4899 dw_attr_node attr;
4901 attr.dw_attr = attr_kind;
4902 attr.dw_attr_val.val_class = dw_val_class_macptr;
4903 attr.dw_attr_val.val_entry = NULL;
4904 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4905 add_dwarf_attr (die, &attr);
4908 /* Add an offset attribute value to a DIE. */
4910 static inline void
4911 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4912 unsigned HOST_WIDE_INT offset)
4914 dw_attr_node attr;
4916 attr.dw_attr = attr_kind;
4917 attr.dw_attr_val.val_class = dw_val_class_offset;
4918 attr.dw_attr_val.val_entry = NULL;
4919 attr.dw_attr_val.v.val_offset = offset;
4920 add_dwarf_attr (die, &attr);
4923 /* Add a range_list attribute value to a DIE. When using
4924 dwarf_split_debug_info, address attributes in dies destined for the
4925 final executable should be direct references--setting the parameter
4926 force_direct ensures this behavior. */
4928 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4929 #define RELOCATED_OFFSET (NULL)
4931 static void
4932 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4933 long unsigned int offset, bool force_direct)
4935 dw_attr_node attr;
4937 attr.dw_attr = attr_kind;
4938 attr.dw_attr_val.val_class = dw_val_class_range_list;
4939 /* For the range_list attribute, use val_entry to store whether the
4940 offset should follow split-debug-info or normal semantics. This
4941 value is read in output_range_list_offset. */
4942 if (dwarf_split_debug_info && !force_direct)
4943 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4944 else
4945 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4946 attr.dw_attr_val.v.val_offset = offset;
4947 add_dwarf_attr (die, &attr);
4950 /* Return the start label of a delta attribute. */
4952 static inline const char *
4953 AT_vms_delta1 (dw_attr_node *a)
4955 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4956 return a->dw_attr_val.v.val_vms_delta.lbl1;
4959 /* Return the end label of a delta attribute. */
4961 static inline const char *
4962 AT_vms_delta2 (dw_attr_node *a)
4964 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4965 return a->dw_attr_val.v.val_vms_delta.lbl2;
4968 static inline const char *
4969 AT_lbl (dw_attr_node *a)
4971 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4972 || AT_class (a) == dw_val_class_lineptr
4973 || AT_class (a) == dw_val_class_macptr
4974 || AT_class (a) == dw_val_class_loclistsptr
4975 || AT_class (a) == dw_val_class_high_pc));
4976 return a->dw_attr_val.v.val_lbl_id;
4979 /* Get the attribute of type attr_kind. */
4981 static dw_attr_node *
4982 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4984 dw_attr_node *a;
4985 unsigned ix;
4986 dw_die_ref spec = NULL;
4988 if (! die)
4989 return NULL;
4991 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4992 if (a->dw_attr == attr_kind)
4993 return a;
4994 else if (a->dw_attr == DW_AT_specification
4995 || a->dw_attr == DW_AT_abstract_origin)
4996 spec = AT_ref (a);
4998 if (spec)
4999 return get_AT (spec, attr_kind);
5001 return NULL;
5004 /* Returns the parent of the declaration of DIE. */
5006 static dw_die_ref
5007 get_die_parent (dw_die_ref die)
5009 dw_die_ref t;
5011 if (!die)
5012 return NULL;
5014 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5015 || (t = get_AT_ref (die, DW_AT_specification)))
5016 die = t;
5018 return die->die_parent;
5021 /* Return the "low pc" attribute value, typically associated with a subprogram
5022 DIE. Return null if the "low pc" attribute is either not present, or if it
5023 cannot be represented as an assembler label identifier. */
5025 static inline const char *
5026 get_AT_low_pc (dw_die_ref die)
5028 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5030 return a ? AT_lbl (a) : NULL;
5033 /* Return the "high pc" attribute value, typically associated with a subprogram
5034 DIE. Return null if the "high pc" attribute is either not present, or if it
5035 cannot be represented as an assembler label identifier. */
5037 static inline const char *
5038 get_AT_hi_pc (dw_die_ref die)
5040 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5042 return a ? AT_lbl (a) : NULL;
5045 /* Return the value of the string attribute designated by ATTR_KIND, or
5046 NULL if it is not present. */
5048 static inline const char *
5049 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5051 dw_attr_node *a = get_AT (die, attr_kind);
5053 return a ? AT_string (a) : NULL;
5056 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5057 if it is not present. */
5059 static inline int
5060 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5062 dw_attr_node *a = get_AT (die, attr_kind);
5064 return a ? AT_flag (a) : 0;
5067 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5068 if it is not present. */
5070 static inline unsigned
5071 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5073 dw_attr_node *a = get_AT (die, attr_kind);
5075 return a ? AT_unsigned (a) : 0;
5078 static inline dw_die_ref
5079 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5081 dw_attr_node *a = get_AT (die, attr_kind);
5083 return a ? AT_ref (a) : NULL;
5086 static inline struct dwarf_file_data *
5087 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5089 dw_attr_node *a = get_AT (die, attr_kind);
5091 return a ? AT_file (a) : NULL;
5094 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
5096 static const_tree
5097 get_ultimate_context (const_tree decl)
5099 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
5101 if (TREE_CODE (decl) == BLOCK)
5102 decl = BLOCK_SUPERCONTEXT (decl);
5103 else
5104 decl = get_containing_scope (decl);
5106 return decl;
5109 /* Return TRUE if the language is C++. */
5111 static inline bool
5112 is_cxx (void)
5114 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5116 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5117 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5120 /* Return TRUE if DECL was created by the C++ frontend. */
5122 static bool
5123 is_cxx (const_tree decl)
5125 if (in_lto_p)
5127 const_tree context = get_ultimate_context (decl);
5128 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5129 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5131 return is_cxx ();
5134 /* Return TRUE if the language is Fortran. */
5136 static inline bool
5137 is_fortran (void)
5139 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5141 return (lang == DW_LANG_Fortran77
5142 || lang == DW_LANG_Fortran90
5143 || lang == DW_LANG_Fortran95
5144 || lang == DW_LANG_Fortran03
5145 || lang == DW_LANG_Fortran08);
5148 static inline bool
5149 is_fortran (const_tree decl)
5151 if (in_lto_p)
5153 const_tree context = get_ultimate_context (decl);
5154 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5155 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5156 "GNU Fortran", 11) == 0
5157 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5158 "GNU F77") == 0);
5160 return is_fortran ();
5163 /* Return TRUE if the language is Ada. */
5165 static inline bool
5166 is_ada (void)
5168 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5170 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5173 /* Remove the specified attribute if present. Return TRUE if removal
5174 was successful. */
5176 static bool
5177 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5179 dw_attr_node *a;
5180 unsigned ix;
5182 if (! die)
5183 return false;
5185 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5186 if (a->dw_attr == attr_kind)
5188 if (AT_class (a) == dw_val_class_str)
5189 if (a->dw_attr_val.v.val_str->refcount)
5190 a->dw_attr_val.v.val_str->refcount--;
5192 /* vec::ordered_remove should help reduce the number of abbrevs
5193 that are needed. */
5194 die->die_attr->ordered_remove (ix);
5195 return true;
5197 return false;
5200 /* Remove CHILD from its parent. PREV must have the property that
5201 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5203 static void
5204 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5206 gcc_assert (child->die_parent == prev->die_parent);
5207 gcc_assert (prev->die_sib == child);
5208 if (prev == child)
5210 gcc_assert (child->die_parent->die_child == child);
5211 prev = NULL;
5213 else
5214 prev->die_sib = child->die_sib;
5215 if (child->die_parent->die_child == child)
5216 child->die_parent->die_child = prev;
5217 child->die_sib = NULL;
5220 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5221 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5223 static void
5224 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5226 dw_die_ref parent = old_child->die_parent;
5228 gcc_assert (parent == prev->die_parent);
5229 gcc_assert (prev->die_sib == old_child);
5231 new_child->die_parent = parent;
5232 if (prev == old_child)
5234 gcc_assert (parent->die_child == old_child);
5235 new_child->die_sib = new_child;
5237 else
5239 prev->die_sib = new_child;
5240 new_child->die_sib = old_child->die_sib;
5242 if (old_child->die_parent->die_child == old_child)
5243 old_child->die_parent->die_child = new_child;
5244 old_child->die_sib = NULL;
5247 /* Move all children from OLD_PARENT to NEW_PARENT. */
5249 static void
5250 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5252 dw_die_ref c;
5253 new_parent->die_child = old_parent->die_child;
5254 old_parent->die_child = NULL;
5255 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5258 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5259 matches TAG. */
5261 static void
5262 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5264 dw_die_ref c;
5266 c = die->die_child;
5267 if (c) do {
5268 dw_die_ref prev = c;
5269 c = c->die_sib;
5270 while (c->die_tag == tag)
5272 remove_child_with_prev (c, prev);
5273 c->die_parent = NULL;
5274 /* Might have removed every child. */
5275 if (die->die_child == NULL)
5276 return;
5277 c = prev->die_sib;
5279 } while (c != die->die_child);
5282 /* Add a CHILD_DIE as the last child of DIE. */
5284 static void
5285 add_child_die (dw_die_ref die, dw_die_ref child_die)
5287 /* FIXME this should probably be an assert. */
5288 if (! die || ! child_die)
5289 return;
5290 gcc_assert (die != child_die);
5292 child_die->die_parent = die;
5293 if (die->die_child)
5295 child_die->die_sib = die->die_child->die_sib;
5296 die->die_child->die_sib = child_die;
5298 else
5299 child_die->die_sib = child_die;
5300 die->die_child = child_die;
5303 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5305 static void
5306 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5307 dw_die_ref after_die)
5309 gcc_assert (die
5310 && child_die
5311 && after_die
5312 && die->die_child
5313 && die != child_die);
5315 child_die->die_parent = die;
5316 child_die->die_sib = after_die->die_sib;
5317 after_die->die_sib = child_die;
5318 if (die->die_child == after_die)
5319 die->die_child = child_die;
5322 /* Unassociate CHILD from its parent, and make its parent be
5323 NEW_PARENT. */
5325 static void
5326 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5328 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5329 if (p->die_sib == child)
5331 remove_child_with_prev (child, p);
5332 break;
5334 add_child_die (new_parent, child);
5337 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5338 is the specification, to the end of PARENT's list of children.
5339 This is done by removing and re-adding it. */
5341 static void
5342 splice_child_die (dw_die_ref parent, dw_die_ref child)
5344 /* We want the declaration DIE from inside the class, not the
5345 specification DIE at toplevel. */
5346 if (child->die_parent != parent)
5348 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5350 if (tmp)
5351 child = tmp;
5354 gcc_assert (child->die_parent == parent
5355 || (child->die_parent
5356 == get_AT_ref (parent, DW_AT_specification)));
5358 reparent_child (child, parent);
5361 /* Create and return a new die with a parent of PARENT_DIE. If
5362 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5363 associated tree T must be supplied to determine parenthood
5364 later. */
5366 static inline dw_die_ref
5367 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5369 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5371 die->die_tag = tag_value;
5373 if (parent_die != NULL)
5374 add_child_die (parent_die, die);
5375 else
5377 limbo_die_node *limbo_node;
5379 /* No DIEs created after early dwarf should end up in limbo,
5380 because the limbo list should not persist past LTO
5381 streaming. */
5382 if (tag_value != DW_TAG_compile_unit
5383 /* These are allowed because they're generated while
5384 breaking out COMDAT units late. */
5385 && tag_value != DW_TAG_type_unit
5386 && tag_value != DW_TAG_skeleton_unit
5387 && !early_dwarf
5388 /* Allow nested functions to live in limbo because they will
5389 only temporarily live there, as decls_for_scope will fix
5390 them up. */
5391 && (TREE_CODE (t) != FUNCTION_DECL
5392 || !decl_function_context (t))
5393 /* Same as nested functions above but for types. Types that
5394 are local to a function will be fixed in
5395 decls_for_scope. */
5396 && (!RECORD_OR_UNION_TYPE_P (t)
5397 || !TYPE_CONTEXT (t)
5398 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5399 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5400 especially in the ltrans stage, but once we implement LTO
5401 dwarf streaming, we should remove this exception. */
5402 && !in_lto_p)
5404 fprintf (stderr, "symbol ended up in limbo too late:");
5405 debug_generic_stmt (t);
5406 gcc_unreachable ();
5409 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5410 limbo_node->die = die;
5411 limbo_node->created_for = t;
5412 limbo_node->next = limbo_die_list;
5413 limbo_die_list = limbo_node;
5416 return die;
5419 /* Return the DIE associated with the given type specifier. */
5421 static inline dw_die_ref
5422 lookup_type_die (tree type)
5424 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5425 if (die && die->removed)
5427 TYPE_SYMTAB_DIE (type) = NULL;
5428 return NULL;
5430 return die;
5433 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5434 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5435 anonymous type instead the one of the naming typedef. */
5437 static inline dw_die_ref
5438 strip_naming_typedef (tree type, dw_die_ref type_die)
5440 if (type
5441 && TREE_CODE (type) == RECORD_TYPE
5442 && type_die
5443 && type_die->die_tag == DW_TAG_typedef
5444 && is_naming_typedef_decl (TYPE_NAME (type)))
5445 type_die = get_AT_ref (type_die, DW_AT_type);
5446 return type_die;
5449 /* Like lookup_type_die, but if type is an anonymous type named by a
5450 typedef[1], return the DIE of the anonymous type instead the one of
5451 the naming typedef. This is because in gen_typedef_die, we did
5452 equate the anonymous struct named by the typedef with the DIE of
5453 the naming typedef. So by default, lookup_type_die on an anonymous
5454 struct yields the DIE of the naming typedef.
5456 [1]: Read the comment of is_naming_typedef_decl to learn about what
5457 a naming typedef is. */
5459 static inline dw_die_ref
5460 lookup_type_die_strip_naming_typedef (tree type)
5462 dw_die_ref die = lookup_type_die (type);
5463 return strip_naming_typedef (type, die);
5466 /* Equate a DIE to a given type specifier. */
5468 static inline void
5469 equate_type_number_to_die (tree type, dw_die_ref type_die)
5471 TYPE_SYMTAB_DIE (type) = type_die;
5474 /* Returns a hash value for X (which really is a die_struct). */
5476 inline hashval_t
5477 decl_die_hasher::hash (die_node *x)
5479 return (hashval_t) x->decl_id;
5482 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5484 inline bool
5485 decl_die_hasher::equal (die_node *x, tree y)
5487 return (x->decl_id == DECL_UID (y));
5490 /* Return the DIE associated with a given declaration. */
5492 static inline dw_die_ref
5493 lookup_decl_die (tree decl)
5495 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5496 NO_INSERT);
5497 if (!die)
5498 return NULL;
5499 if ((*die)->removed)
5501 decl_die_table->clear_slot (die);
5502 return NULL;
5504 return *die;
5508 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5509 style reference. Return true if we found one refering to a DIE for
5510 DECL, otherwise return false. */
5512 static bool
5513 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5514 unsigned HOST_WIDE_INT *off)
5516 dw_die_ref die;
5518 if (flag_wpa && !decl_die_table)
5519 return false;
5521 if (TREE_CODE (decl) == BLOCK)
5522 die = BLOCK_DIE (decl);
5523 else
5524 die = lookup_decl_die (decl);
5525 if (!die)
5526 return false;
5528 /* During WPA stage we currently use DIEs to store the
5529 decl <-> label + offset map. That's quite inefficient but it
5530 works for now. */
5531 if (flag_wpa)
5533 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5534 if (!ref)
5536 gcc_assert (die == comp_unit_die ());
5537 return false;
5539 *off = ref->die_offset;
5540 *sym = ref->die_id.die_symbol;
5541 return true;
5544 /* Similar to get_ref_die_offset_label, but using the "correct"
5545 label. */
5546 *off = die->die_offset;
5547 while (die->die_parent)
5548 die = die->die_parent;
5549 /* For the containing CU DIE we compute a die_symbol in
5550 compute_comp_unit_symbol. */
5551 gcc_assert (die->die_tag == DW_TAG_compile_unit
5552 && die->die_id.die_symbol != NULL);
5553 *sym = die->die_id.die_symbol;
5554 return true;
5557 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5559 static void
5560 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5561 const char *symbol, HOST_WIDE_INT offset)
5563 /* Create a fake DIE that contains the reference. Don't use
5564 new_die because we don't want to end up in the limbo list. */
5565 dw_die_ref ref = ggc_cleared_alloc<die_node> ();
5566 ref->die_tag = die->die_tag;
5567 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5568 ref->die_offset = offset;
5569 ref->with_offset = 1;
5570 add_AT_die_ref (die, attr_kind, ref);
5573 /* Create a DIE for DECL if required and add a reference to a DIE
5574 at SYMBOL + OFFSET which contains attributes dumped early. */
5576 static void
5577 dwarf2out_register_external_die (tree decl, const char *sym,
5578 unsigned HOST_WIDE_INT off)
5580 if (debug_info_level == DINFO_LEVEL_NONE)
5581 return;
5583 if (flag_wpa && !decl_die_table)
5584 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5586 dw_die_ref die
5587 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5588 gcc_assert (!die);
5590 tree ctx;
5591 dw_die_ref parent = NULL;
5592 /* Need to lookup a DIE for the decls context - the containing
5593 function or translation unit. */
5594 if (TREE_CODE (decl) == BLOCK)
5596 ctx = BLOCK_SUPERCONTEXT (decl);
5597 /* ??? We do not output DIEs for all scopes thus skip as
5598 many DIEs as needed. */
5599 while (TREE_CODE (ctx) == BLOCK
5600 && !BLOCK_DIE (ctx))
5601 ctx = BLOCK_SUPERCONTEXT (ctx);
5603 else
5604 ctx = DECL_CONTEXT (decl);
5605 while (ctx && TYPE_P (ctx))
5606 ctx = TYPE_CONTEXT (ctx);
5607 if (ctx)
5609 if (TREE_CODE (ctx) == BLOCK)
5610 parent = BLOCK_DIE (ctx);
5611 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5612 /* Keep the 1:1 association during WPA. */
5613 && !flag_wpa)
5614 /* Otherwise all late annotations go to the main CU which
5615 imports the original CUs. */
5616 parent = comp_unit_die ();
5617 else if (TREE_CODE (ctx) == FUNCTION_DECL
5618 && TREE_CODE (decl) != PARM_DECL
5619 && TREE_CODE (decl) != BLOCK)
5620 /* Leave function local entities parent determination to when
5621 we process scope vars. */
5623 else
5624 parent = lookup_decl_die (ctx);
5626 else
5627 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5628 Handle this case gracefully by globalizing stuff. */
5629 parent = comp_unit_die ();
5630 /* Create a DIE "stub". */
5631 switch (TREE_CODE (decl))
5633 case TRANSLATION_UNIT_DECL:
5634 if (! flag_wpa)
5636 die = comp_unit_die ();
5637 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5638 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5639 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5640 to create a DIE for the original CUs. */
5641 return;
5643 /* Keep the 1:1 association during WPA. */
5644 die = new_die (DW_TAG_compile_unit, NULL, decl);
5645 break;
5646 case NAMESPACE_DECL:
5647 if (is_fortran (decl))
5648 die = new_die (DW_TAG_module, parent, decl);
5649 else
5650 die = new_die (DW_TAG_namespace, parent, decl);
5651 break;
5652 case FUNCTION_DECL:
5653 die = new_die (DW_TAG_subprogram, parent, decl);
5654 break;
5655 case VAR_DECL:
5656 die = new_die (DW_TAG_variable, parent, decl);
5657 break;
5658 case RESULT_DECL:
5659 die = new_die (DW_TAG_variable, parent, decl);
5660 break;
5661 case PARM_DECL:
5662 die = new_die (DW_TAG_formal_parameter, parent, decl);
5663 break;
5664 case CONST_DECL:
5665 die = new_die (DW_TAG_constant, parent, decl);
5666 break;
5667 case LABEL_DECL:
5668 die = new_die (DW_TAG_label, parent, decl);
5669 break;
5670 case BLOCK:
5671 die = new_die (DW_TAG_lexical_block, parent, decl);
5672 break;
5673 default:
5674 gcc_unreachable ();
5676 if (TREE_CODE (decl) == BLOCK)
5677 BLOCK_DIE (decl) = die;
5678 else
5679 equate_decl_number_to_die (decl, die);
5681 /* Add a reference to the DIE providing early debug at $sym + off. */
5682 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5685 /* Returns a hash value for X (which really is a var_loc_list). */
5687 inline hashval_t
5688 decl_loc_hasher::hash (var_loc_list *x)
5690 return (hashval_t) x->decl_id;
5693 /* Return nonzero if decl_id of var_loc_list X is the same as
5694 UID of decl *Y. */
5696 inline bool
5697 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5699 return (x->decl_id == DECL_UID (y));
5702 /* Return the var_loc list associated with a given declaration. */
5704 static inline var_loc_list *
5705 lookup_decl_loc (const_tree decl)
5707 if (!decl_loc_table)
5708 return NULL;
5709 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5712 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5714 inline hashval_t
5715 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5717 return (hashval_t) x->decl_id;
5720 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5721 UID of decl *Y. */
5723 inline bool
5724 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5726 return (x->decl_id == DECL_UID (y));
5729 /* Equate a DIE to a particular declaration. */
5731 static void
5732 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5734 unsigned int decl_id = DECL_UID (decl);
5736 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5737 decl_die->decl_id = decl_id;
5740 /* Return how many bits covers PIECE EXPR_LIST. */
5742 static HOST_WIDE_INT
5743 decl_piece_bitsize (rtx piece)
5745 int ret = (int) GET_MODE (piece);
5746 if (ret)
5747 return ret;
5748 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5749 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5750 return INTVAL (XEXP (XEXP (piece, 0), 0));
5753 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5755 static rtx *
5756 decl_piece_varloc_ptr (rtx piece)
5758 if ((int) GET_MODE (piece))
5759 return &XEXP (piece, 0);
5760 else
5761 return &XEXP (XEXP (piece, 0), 1);
5764 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5765 Next is the chain of following piece nodes. */
5767 static rtx_expr_list *
5768 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5770 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5771 return alloc_EXPR_LIST (bitsize, loc_note, next);
5772 else
5773 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5774 GEN_INT (bitsize),
5775 loc_note), next);
5778 /* Return rtx that should be stored into loc field for
5779 LOC_NOTE and BITPOS/BITSIZE. */
5781 static rtx
5782 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5783 HOST_WIDE_INT bitsize)
5785 if (bitsize != -1)
5787 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5788 if (bitpos != 0)
5789 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5791 return loc_note;
5794 /* This function either modifies location piece list *DEST in
5795 place (if SRC and INNER is NULL), or copies location piece list
5796 *SRC to *DEST while modifying it. Location BITPOS is modified
5797 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5798 not copied and if needed some padding around it is added.
5799 When modifying in place, DEST should point to EXPR_LIST where
5800 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5801 to the start of the whole list and INNER points to the EXPR_LIST
5802 where earlier pieces cover PIECE_BITPOS bits. */
5804 static void
5805 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5806 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5807 HOST_WIDE_INT bitsize, rtx loc_note)
5809 HOST_WIDE_INT diff;
5810 bool copy = inner != NULL;
5812 if (copy)
5814 /* First copy all nodes preceding the current bitpos. */
5815 while (src != inner)
5817 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5818 decl_piece_bitsize (*src), NULL_RTX);
5819 dest = &XEXP (*dest, 1);
5820 src = &XEXP (*src, 1);
5823 /* Add padding if needed. */
5824 if (bitpos != piece_bitpos)
5826 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5827 copy ? NULL_RTX : *dest);
5828 dest = &XEXP (*dest, 1);
5830 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5832 gcc_assert (!copy);
5833 /* A piece with correct bitpos and bitsize already exist,
5834 just update the location for it and return. */
5835 *decl_piece_varloc_ptr (*dest) = loc_note;
5836 return;
5838 /* Add the piece that changed. */
5839 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5840 dest = &XEXP (*dest, 1);
5841 /* Skip over pieces that overlap it. */
5842 diff = bitpos - piece_bitpos + bitsize;
5843 if (!copy)
5844 src = dest;
5845 while (diff > 0 && *src)
5847 rtx piece = *src;
5848 diff -= decl_piece_bitsize (piece);
5849 if (copy)
5850 src = &XEXP (piece, 1);
5851 else
5853 *src = XEXP (piece, 1);
5854 free_EXPR_LIST_node (piece);
5857 /* Add padding if needed. */
5858 if (diff < 0 && *src)
5860 if (!copy)
5861 dest = src;
5862 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5863 dest = &XEXP (*dest, 1);
5865 if (!copy)
5866 return;
5867 /* Finally copy all nodes following it. */
5868 while (*src)
5870 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5871 decl_piece_bitsize (*src), NULL_RTX);
5872 dest = &XEXP (*dest, 1);
5873 src = &XEXP (*src, 1);
5877 /* Add a variable location node to the linked list for DECL. */
5879 static struct var_loc_node *
5880 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5882 unsigned int decl_id;
5883 var_loc_list *temp;
5884 struct var_loc_node *loc = NULL;
5885 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5887 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5889 tree realdecl = DECL_DEBUG_EXPR (decl);
5890 if (handled_component_p (realdecl)
5891 || (TREE_CODE (realdecl) == MEM_REF
5892 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5894 HOST_WIDE_INT maxsize;
5895 bool reverse;
5896 tree innerdecl
5897 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5898 &reverse);
5899 if (!DECL_P (innerdecl)
5900 || DECL_IGNORED_P (innerdecl)
5901 || TREE_STATIC (innerdecl)
5902 || bitsize <= 0
5903 || bitpos + bitsize > 256
5904 || bitsize != maxsize)
5905 return NULL;
5906 decl = innerdecl;
5910 decl_id = DECL_UID (decl);
5911 var_loc_list **slot
5912 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5913 if (*slot == NULL)
5915 temp = ggc_cleared_alloc<var_loc_list> ();
5916 temp->decl_id = decl_id;
5917 *slot = temp;
5919 else
5920 temp = *slot;
5922 /* For PARM_DECLs try to keep around the original incoming value,
5923 even if that means we'll emit a zero-range .debug_loc entry. */
5924 if (temp->last
5925 && temp->first == temp->last
5926 && TREE_CODE (decl) == PARM_DECL
5927 && NOTE_P (temp->first->loc)
5928 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5929 && DECL_INCOMING_RTL (decl)
5930 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5931 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5932 == GET_CODE (DECL_INCOMING_RTL (decl))
5933 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5934 && (bitsize != -1
5935 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5936 NOTE_VAR_LOCATION_LOC (loc_note))
5937 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5938 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5940 loc = ggc_cleared_alloc<var_loc_node> ();
5941 temp->first->next = loc;
5942 temp->last = loc;
5943 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5945 else if (temp->last)
5947 struct var_loc_node *last = temp->last, *unused = NULL;
5948 rtx *piece_loc = NULL, last_loc_note;
5949 HOST_WIDE_INT piece_bitpos = 0;
5950 if (last->next)
5952 last = last->next;
5953 gcc_assert (last->next == NULL);
5955 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5957 piece_loc = &last->loc;
5960 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5961 if (piece_bitpos + cur_bitsize > bitpos)
5962 break;
5963 piece_bitpos += cur_bitsize;
5964 piece_loc = &XEXP (*piece_loc, 1);
5966 while (*piece_loc);
5968 /* TEMP->LAST here is either pointer to the last but one or
5969 last element in the chained list, LAST is pointer to the
5970 last element. */
5971 if (label && strcmp (last->label, label) == 0)
5973 /* For SRA optimized variables if there weren't any real
5974 insns since last note, just modify the last node. */
5975 if (piece_loc != NULL)
5977 adjust_piece_list (piece_loc, NULL, NULL,
5978 bitpos, piece_bitpos, bitsize, loc_note);
5979 return NULL;
5981 /* If the last note doesn't cover any instructions, remove it. */
5982 if (temp->last != last)
5984 temp->last->next = NULL;
5985 unused = last;
5986 last = temp->last;
5987 gcc_assert (strcmp (last->label, label) != 0);
5989 else
5991 gcc_assert (temp->first == temp->last
5992 || (temp->first->next == temp->last
5993 && TREE_CODE (decl) == PARM_DECL));
5994 memset (temp->last, '\0', sizeof (*temp->last));
5995 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5996 return temp->last;
5999 if (bitsize == -1 && NOTE_P (last->loc))
6000 last_loc_note = last->loc;
6001 else if (piece_loc != NULL
6002 && *piece_loc != NULL_RTX
6003 && piece_bitpos == bitpos
6004 && decl_piece_bitsize (*piece_loc) == bitsize)
6005 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6006 else
6007 last_loc_note = NULL_RTX;
6008 /* If the current location is the same as the end of the list,
6009 and either both or neither of the locations is uninitialized,
6010 we have nothing to do. */
6011 if (last_loc_note == NULL_RTX
6012 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6013 NOTE_VAR_LOCATION_LOC (loc_note)))
6014 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6015 != NOTE_VAR_LOCATION_STATUS (loc_note))
6016 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6017 == VAR_INIT_STATUS_UNINITIALIZED)
6018 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6019 == VAR_INIT_STATUS_UNINITIALIZED))))
6021 /* Add LOC to the end of list and update LAST. If the last
6022 element of the list has been removed above, reuse its
6023 memory for the new node, otherwise allocate a new one. */
6024 if (unused)
6026 loc = unused;
6027 memset (loc, '\0', sizeof (*loc));
6029 else
6030 loc = ggc_cleared_alloc<var_loc_node> ();
6031 if (bitsize == -1 || piece_loc == NULL)
6032 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6033 else
6034 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6035 bitpos, piece_bitpos, bitsize, loc_note);
6036 last->next = loc;
6037 /* Ensure TEMP->LAST will point either to the new last but one
6038 element of the chain, or to the last element in it. */
6039 if (last != temp->last)
6040 temp->last = last;
6042 else if (unused)
6043 ggc_free (unused);
6045 else
6047 loc = ggc_cleared_alloc<var_loc_node> ();
6048 temp->first = loc;
6049 temp->last = loc;
6050 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6052 return loc;
6055 /* Keep track of the number of spaces used to indent the
6056 output of the debugging routines that print the structure of
6057 the DIE internal representation. */
6058 static int print_indent;
6060 /* Indent the line the number of spaces given by print_indent. */
6062 static inline void
6063 print_spaces (FILE *outfile)
6065 fprintf (outfile, "%*s", print_indent, "");
6068 /* Print a type signature in hex. */
6070 static inline void
6071 print_signature (FILE *outfile, char *sig)
6073 int i;
6075 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6076 fprintf (outfile, "%02x", sig[i] & 0xff);
6079 static inline void
6080 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6082 if (discr_value->pos)
6083 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6084 else
6085 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6088 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6090 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6091 RECURSE, output location descriptor operations. */
6093 static void
6094 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6096 switch (val->val_class)
6098 case dw_val_class_addr:
6099 fprintf (outfile, "address");
6100 break;
6101 case dw_val_class_offset:
6102 fprintf (outfile, "offset");
6103 break;
6104 case dw_val_class_loc:
6105 fprintf (outfile, "location descriptor");
6106 if (val->v.val_loc == NULL)
6107 fprintf (outfile, " -> <null>\n");
6108 else if (recurse)
6110 fprintf (outfile, ":\n");
6111 print_indent += 4;
6112 print_loc_descr (val->v.val_loc, outfile);
6113 print_indent -= 4;
6115 else
6116 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6117 break;
6118 case dw_val_class_loc_list:
6119 fprintf (outfile, "location list -> label:%s",
6120 val->v.val_loc_list->ll_symbol);
6121 break;
6122 case dw_val_class_range_list:
6123 fprintf (outfile, "range list");
6124 break;
6125 case dw_val_class_const:
6126 case dw_val_class_const_implicit:
6127 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6128 break;
6129 case dw_val_class_unsigned_const:
6130 case dw_val_class_unsigned_const_implicit:
6131 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6132 break;
6133 case dw_val_class_const_double:
6134 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6135 HOST_WIDE_INT_PRINT_UNSIGNED")",
6136 val->v.val_double.high,
6137 val->v.val_double.low);
6138 break;
6139 case dw_val_class_wide_int:
6141 int i = val->v.val_wide->get_len ();
6142 fprintf (outfile, "constant (");
6143 gcc_assert (i > 0);
6144 if (val->v.val_wide->elt (i - 1) == 0)
6145 fprintf (outfile, "0x");
6146 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6147 val->v.val_wide->elt (--i));
6148 while (--i >= 0)
6149 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6150 val->v.val_wide->elt (i));
6151 fprintf (outfile, ")");
6152 break;
6154 case dw_val_class_vec:
6155 fprintf (outfile, "floating-point or vector constant");
6156 break;
6157 case dw_val_class_flag:
6158 fprintf (outfile, "%u", val->v.val_flag);
6159 break;
6160 case dw_val_class_die_ref:
6161 if (val->v.val_die_ref.die != NULL)
6163 dw_die_ref die = val->v.val_die_ref.die;
6165 if (die->comdat_type_p)
6167 fprintf (outfile, "die -> signature: ");
6168 print_signature (outfile,
6169 die->die_id.die_type_node->signature);
6171 else if (die->die_id.die_symbol)
6173 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6174 if (die->with_offset)
6175 fprintf (outfile, " + %ld", die->die_offset);
6177 else
6178 fprintf (outfile, "die -> %ld", die->die_offset);
6179 fprintf (outfile, " (%p)", (void *) die);
6181 else
6182 fprintf (outfile, "die -> <null>");
6183 break;
6184 case dw_val_class_vms_delta:
6185 fprintf (outfile, "delta: @slotcount(%s-%s)",
6186 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6187 break;
6188 case dw_val_class_lbl_id:
6189 case dw_val_class_lineptr:
6190 case dw_val_class_macptr:
6191 case dw_val_class_loclistsptr:
6192 case dw_val_class_high_pc:
6193 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6194 break;
6195 case dw_val_class_str:
6196 if (val->v.val_str->str != NULL)
6197 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6198 else
6199 fprintf (outfile, "<null>");
6200 break;
6201 case dw_val_class_file:
6202 case dw_val_class_file_implicit:
6203 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6204 val->v.val_file->emitted_number);
6205 break;
6206 case dw_val_class_data8:
6208 int i;
6210 for (i = 0; i < 8; i++)
6211 fprintf (outfile, "%02x", val->v.val_data8[i]);
6212 break;
6214 case dw_val_class_discr_value:
6215 print_discr_value (outfile, &val->v.val_discr_value);
6216 break;
6217 case dw_val_class_discr_list:
6218 for (dw_discr_list_ref node = val->v.val_discr_list;
6219 node != NULL;
6220 node = node->dw_discr_next)
6222 if (node->dw_discr_range)
6224 fprintf (outfile, " .. ");
6225 print_discr_value (outfile, &node->dw_discr_lower_bound);
6226 print_discr_value (outfile, &node->dw_discr_upper_bound);
6228 else
6229 print_discr_value (outfile, &node->dw_discr_lower_bound);
6231 if (node->dw_discr_next != NULL)
6232 fprintf (outfile, " | ");
6234 default:
6235 break;
6239 /* Likewise, for a DIE attribute. */
6241 static void
6242 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6244 print_dw_val (&a->dw_attr_val, recurse, outfile);
6248 /* Print the list of operands in the LOC location description to OUTFILE. This
6249 routine is a debugging aid only. */
6251 static void
6252 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6254 dw_loc_descr_ref l = loc;
6256 if (loc == NULL)
6258 print_spaces (outfile);
6259 fprintf (outfile, "<null>\n");
6260 return;
6263 for (l = loc; l != NULL; l = l->dw_loc_next)
6265 print_spaces (outfile);
6266 fprintf (outfile, "(%p) %s",
6267 (void *) l,
6268 dwarf_stack_op_name (l->dw_loc_opc));
6269 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6271 fprintf (outfile, " ");
6272 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6274 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6276 fprintf (outfile, ", ");
6277 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6279 fprintf (outfile, "\n");
6283 /* Print the information associated with a given DIE, and its children.
6284 This routine is a debugging aid only. */
6286 static void
6287 print_die (dw_die_ref die, FILE *outfile)
6289 dw_attr_node *a;
6290 dw_die_ref c;
6291 unsigned ix;
6293 print_spaces (outfile);
6294 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6295 die->die_offset, dwarf_tag_name (die->die_tag),
6296 (void*) die);
6297 print_spaces (outfile);
6298 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6299 fprintf (outfile, " offset: %ld", die->die_offset);
6300 fprintf (outfile, " mark: %d\n", die->die_mark);
6302 if (die->comdat_type_p)
6304 print_spaces (outfile);
6305 fprintf (outfile, " signature: ");
6306 print_signature (outfile, die->die_id.die_type_node->signature);
6307 fprintf (outfile, "\n");
6310 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6312 print_spaces (outfile);
6313 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6315 print_attribute (a, true, outfile);
6316 fprintf (outfile, "\n");
6319 if (die->die_child != NULL)
6321 print_indent += 4;
6322 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6323 print_indent -= 4;
6325 if (print_indent == 0)
6326 fprintf (outfile, "\n");
6329 /* Print the list of operations in the LOC location description. */
6331 DEBUG_FUNCTION void
6332 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6334 print_loc_descr (loc, stderr);
6337 /* Print the information collected for a given DIE. */
6339 DEBUG_FUNCTION void
6340 debug_dwarf_die (dw_die_ref die)
6342 print_die (die, stderr);
6345 DEBUG_FUNCTION void
6346 debug (die_struct &ref)
6348 print_die (&ref, stderr);
6351 DEBUG_FUNCTION void
6352 debug (die_struct *ptr)
6354 if (ptr)
6355 debug (*ptr);
6356 else
6357 fprintf (stderr, "<nil>\n");
6361 /* Print all DWARF information collected for the compilation unit.
6362 This routine is a debugging aid only. */
6364 DEBUG_FUNCTION void
6365 debug_dwarf (void)
6367 print_indent = 0;
6368 print_die (comp_unit_die (), stderr);
6371 /* Verify the DIE tree structure. */
6373 DEBUG_FUNCTION void
6374 verify_die (dw_die_ref die)
6376 gcc_assert (!die->die_mark);
6377 if (die->die_parent == NULL
6378 && die->die_sib == NULL)
6379 return;
6380 /* Verify the die_sib list is cyclic. */
6381 dw_die_ref x = die;
6384 x->die_mark = 1;
6385 x = x->die_sib;
6387 while (x && !x->die_mark);
6388 gcc_assert (x == die);
6389 x = die;
6392 /* Verify all dies have the same parent. */
6393 gcc_assert (x->die_parent == die->die_parent);
6394 if (x->die_child)
6396 /* Verify the child has the proper parent and recurse. */
6397 gcc_assert (x->die_child->die_parent == x);
6398 verify_die (x->die_child);
6400 x->die_mark = 0;
6401 x = x->die_sib;
6403 while (x && x->die_mark);
6406 /* Sanity checks on DIEs. */
6408 static void
6409 check_die (dw_die_ref die)
6411 unsigned ix;
6412 dw_attr_node *a;
6413 bool inline_found = false;
6414 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6415 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6416 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6418 switch (a->dw_attr)
6420 case DW_AT_inline:
6421 if (a->dw_attr_val.v.val_unsigned)
6422 inline_found = true;
6423 break;
6424 case DW_AT_location:
6425 ++n_location;
6426 break;
6427 case DW_AT_low_pc:
6428 ++n_low_pc;
6429 break;
6430 case DW_AT_high_pc:
6431 ++n_high_pc;
6432 break;
6433 case DW_AT_artificial:
6434 ++n_artificial;
6435 break;
6436 case DW_AT_decl_column:
6437 ++n_decl_column;
6438 break;
6439 case DW_AT_decl_line:
6440 ++n_decl_line;
6441 break;
6442 case DW_AT_decl_file:
6443 ++n_decl_file;
6444 break;
6445 default:
6446 break;
6449 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6450 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6452 fprintf (stderr, "Duplicate attributes in DIE:\n");
6453 debug_dwarf_die (die);
6454 gcc_unreachable ();
6456 if (inline_found)
6458 /* A debugging information entry that is a member of an abstract
6459 instance tree [that has DW_AT_inline] should not contain any
6460 attributes which describe aspects of the subroutine which vary
6461 between distinct inlined expansions or distinct out-of-line
6462 expansions. */
6463 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6464 gcc_assert (a->dw_attr != DW_AT_low_pc
6465 && a->dw_attr != DW_AT_high_pc
6466 && a->dw_attr != DW_AT_location
6467 && a->dw_attr != DW_AT_frame_base
6468 && a->dw_attr != DW_AT_call_all_calls
6469 && a->dw_attr != DW_AT_GNU_all_call_sites);
6473 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6474 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6475 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6477 /* Calculate the checksum of a location expression. */
6479 static inline void
6480 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6482 int tem;
6483 inchash::hash hstate;
6484 hashval_t hash;
6486 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6487 CHECKSUM (tem);
6488 hash_loc_operands (loc, hstate);
6489 hash = hstate.end();
6490 CHECKSUM (hash);
6493 /* Calculate the checksum of an attribute. */
6495 static void
6496 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6498 dw_loc_descr_ref loc;
6499 rtx r;
6501 CHECKSUM (at->dw_attr);
6503 /* We don't care that this was compiled with a different compiler
6504 snapshot; if the output is the same, that's what matters. */
6505 if (at->dw_attr == DW_AT_producer)
6506 return;
6508 switch (AT_class (at))
6510 case dw_val_class_const:
6511 case dw_val_class_const_implicit:
6512 CHECKSUM (at->dw_attr_val.v.val_int);
6513 break;
6514 case dw_val_class_unsigned_const:
6515 case dw_val_class_unsigned_const_implicit:
6516 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6517 break;
6518 case dw_val_class_const_double:
6519 CHECKSUM (at->dw_attr_val.v.val_double);
6520 break;
6521 case dw_val_class_wide_int:
6522 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6523 get_full_len (*at->dw_attr_val.v.val_wide)
6524 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6525 break;
6526 case dw_val_class_vec:
6527 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6528 (at->dw_attr_val.v.val_vec.length
6529 * at->dw_attr_val.v.val_vec.elt_size));
6530 break;
6531 case dw_val_class_flag:
6532 CHECKSUM (at->dw_attr_val.v.val_flag);
6533 break;
6534 case dw_val_class_str:
6535 CHECKSUM_STRING (AT_string (at));
6536 break;
6538 case dw_val_class_addr:
6539 r = AT_addr (at);
6540 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6541 CHECKSUM_STRING (XSTR (r, 0));
6542 break;
6544 case dw_val_class_offset:
6545 CHECKSUM (at->dw_attr_val.v.val_offset);
6546 break;
6548 case dw_val_class_loc:
6549 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6550 loc_checksum (loc, ctx);
6551 break;
6553 case dw_val_class_die_ref:
6554 die_checksum (AT_ref (at), ctx, mark);
6555 break;
6557 case dw_val_class_fde_ref:
6558 case dw_val_class_vms_delta:
6559 case dw_val_class_lbl_id:
6560 case dw_val_class_lineptr:
6561 case dw_val_class_macptr:
6562 case dw_val_class_loclistsptr:
6563 case dw_val_class_high_pc:
6564 break;
6566 case dw_val_class_file:
6567 case dw_val_class_file_implicit:
6568 CHECKSUM_STRING (AT_file (at)->filename);
6569 break;
6571 case dw_val_class_data8:
6572 CHECKSUM (at->dw_attr_val.v.val_data8);
6573 break;
6575 default:
6576 break;
6580 /* Calculate the checksum of a DIE. */
6582 static void
6583 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6585 dw_die_ref c;
6586 dw_attr_node *a;
6587 unsigned ix;
6589 /* To avoid infinite recursion. */
6590 if (die->die_mark)
6592 CHECKSUM (die->die_mark);
6593 return;
6595 die->die_mark = ++(*mark);
6597 CHECKSUM (die->die_tag);
6599 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6600 attr_checksum (a, ctx, mark);
6602 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6605 #undef CHECKSUM
6606 #undef CHECKSUM_BLOCK
6607 #undef CHECKSUM_STRING
6609 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6610 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6611 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6612 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6613 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6614 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6615 #define CHECKSUM_ATTR(FOO) \
6616 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6618 /* Calculate the checksum of a number in signed LEB128 format. */
6620 static void
6621 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6623 unsigned char byte;
6624 bool more;
6626 while (1)
6628 byte = (value & 0x7f);
6629 value >>= 7;
6630 more = !((value == 0 && (byte & 0x40) == 0)
6631 || (value == -1 && (byte & 0x40) != 0));
6632 if (more)
6633 byte |= 0x80;
6634 CHECKSUM (byte);
6635 if (!more)
6636 break;
6640 /* Calculate the checksum of a number in unsigned LEB128 format. */
6642 static void
6643 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6645 while (1)
6647 unsigned char byte = (value & 0x7f);
6648 value >>= 7;
6649 if (value != 0)
6650 /* More bytes to follow. */
6651 byte |= 0x80;
6652 CHECKSUM (byte);
6653 if (value == 0)
6654 break;
6658 /* Checksum the context of the DIE. This adds the names of any
6659 surrounding namespaces or structures to the checksum. */
6661 static void
6662 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6664 const char *name;
6665 dw_die_ref spec;
6666 int tag = die->die_tag;
6668 if (tag != DW_TAG_namespace
6669 && tag != DW_TAG_structure_type
6670 && tag != DW_TAG_class_type)
6671 return;
6673 name = get_AT_string (die, DW_AT_name);
6675 spec = get_AT_ref (die, DW_AT_specification);
6676 if (spec != NULL)
6677 die = spec;
6679 if (die->die_parent != NULL)
6680 checksum_die_context (die->die_parent, ctx);
6682 CHECKSUM_ULEB128 ('C');
6683 CHECKSUM_ULEB128 (tag);
6684 if (name != NULL)
6685 CHECKSUM_STRING (name);
6688 /* Calculate the checksum of a location expression. */
6690 static inline void
6691 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6693 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6694 were emitted as a DW_FORM_sdata instead of a location expression. */
6695 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6697 CHECKSUM_ULEB128 (DW_FORM_sdata);
6698 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6699 return;
6702 /* Otherwise, just checksum the raw location expression. */
6703 while (loc != NULL)
6705 inchash::hash hstate;
6706 hashval_t hash;
6708 CHECKSUM_ULEB128 (loc->dtprel);
6709 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6710 hash_loc_operands (loc, hstate);
6711 hash = hstate.end ();
6712 CHECKSUM (hash);
6713 loc = loc->dw_loc_next;
6717 /* Calculate the checksum of an attribute. */
6719 static void
6720 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6721 struct md5_ctx *ctx, int *mark)
6723 dw_loc_descr_ref loc;
6724 rtx r;
6726 if (AT_class (at) == dw_val_class_die_ref)
6728 dw_die_ref target_die = AT_ref (at);
6730 /* For pointer and reference types, we checksum only the (qualified)
6731 name of the target type (if there is a name). For friend entries,
6732 we checksum only the (qualified) name of the target type or function.
6733 This allows the checksum to remain the same whether the target type
6734 is complete or not. */
6735 if ((at->dw_attr == DW_AT_type
6736 && (tag == DW_TAG_pointer_type
6737 || tag == DW_TAG_reference_type
6738 || tag == DW_TAG_rvalue_reference_type
6739 || tag == DW_TAG_ptr_to_member_type))
6740 || (at->dw_attr == DW_AT_friend
6741 && tag == DW_TAG_friend))
6743 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6745 if (name_attr != NULL)
6747 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6749 if (decl == NULL)
6750 decl = target_die;
6751 CHECKSUM_ULEB128 ('N');
6752 CHECKSUM_ULEB128 (at->dw_attr);
6753 if (decl->die_parent != NULL)
6754 checksum_die_context (decl->die_parent, ctx);
6755 CHECKSUM_ULEB128 ('E');
6756 CHECKSUM_STRING (AT_string (name_attr));
6757 return;
6761 /* For all other references to another DIE, we check to see if the
6762 target DIE has already been visited. If it has, we emit a
6763 backward reference; if not, we descend recursively. */
6764 if (target_die->die_mark > 0)
6766 CHECKSUM_ULEB128 ('R');
6767 CHECKSUM_ULEB128 (at->dw_attr);
6768 CHECKSUM_ULEB128 (target_die->die_mark);
6770 else
6772 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6774 if (decl == NULL)
6775 decl = target_die;
6776 target_die->die_mark = ++(*mark);
6777 CHECKSUM_ULEB128 ('T');
6778 CHECKSUM_ULEB128 (at->dw_attr);
6779 if (decl->die_parent != NULL)
6780 checksum_die_context (decl->die_parent, ctx);
6781 die_checksum_ordered (target_die, ctx, mark);
6783 return;
6786 CHECKSUM_ULEB128 ('A');
6787 CHECKSUM_ULEB128 (at->dw_attr);
6789 switch (AT_class (at))
6791 case dw_val_class_const:
6792 case dw_val_class_const_implicit:
6793 CHECKSUM_ULEB128 (DW_FORM_sdata);
6794 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6795 break;
6797 case dw_val_class_unsigned_const:
6798 case dw_val_class_unsigned_const_implicit:
6799 CHECKSUM_ULEB128 (DW_FORM_sdata);
6800 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6801 break;
6803 case dw_val_class_const_double:
6804 CHECKSUM_ULEB128 (DW_FORM_block);
6805 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6806 CHECKSUM (at->dw_attr_val.v.val_double);
6807 break;
6809 case dw_val_class_wide_int:
6810 CHECKSUM_ULEB128 (DW_FORM_block);
6811 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6812 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6813 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6814 get_full_len (*at->dw_attr_val.v.val_wide)
6815 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6816 break;
6818 case dw_val_class_vec:
6819 CHECKSUM_ULEB128 (DW_FORM_block);
6820 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6821 * at->dw_attr_val.v.val_vec.elt_size);
6822 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6823 (at->dw_attr_val.v.val_vec.length
6824 * at->dw_attr_val.v.val_vec.elt_size));
6825 break;
6827 case dw_val_class_flag:
6828 CHECKSUM_ULEB128 (DW_FORM_flag);
6829 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6830 break;
6832 case dw_val_class_str:
6833 CHECKSUM_ULEB128 (DW_FORM_string);
6834 CHECKSUM_STRING (AT_string (at));
6835 break;
6837 case dw_val_class_addr:
6838 r = AT_addr (at);
6839 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6840 CHECKSUM_ULEB128 (DW_FORM_string);
6841 CHECKSUM_STRING (XSTR (r, 0));
6842 break;
6844 case dw_val_class_offset:
6845 CHECKSUM_ULEB128 (DW_FORM_sdata);
6846 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6847 break;
6849 case dw_val_class_loc:
6850 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6851 loc_checksum_ordered (loc, ctx);
6852 break;
6854 case dw_val_class_fde_ref:
6855 case dw_val_class_lbl_id:
6856 case dw_val_class_lineptr:
6857 case dw_val_class_macptr:
6858 case dw_val_class_loclistsptr:
6859 case dw_val_class_high_pc:
6860 break;
6862 case dw_val_class_file:
6863 case dw_val_class_file_implicit:
6864 CHECKSUM_ULEB128 (DW_FORM_string);
6865 CHECKSUM_STRING (AT_file (at)->filename);
6866 break;
6868 case dw_val_class_data8:
6869 CHECKSUM (at->dw_attr_val.v.val_data8);
6870 break;
6872 default:
6873 break;
6877 struct checksum_attributes
6879 dw_attr_node *at_name;
6880 dw_attr_node *at_type;
6881 dw_attr_node *at_friend;
6882 dw_attr_node *at_accessibility;
6883 dw_attr_node *at_address_class;
6884 dw_attr_node *at_alignment;
6885 dw_attr_node *at_allocated;
6886 dw_attr_node *at_artificial;
6887 dw_attr_node *at_associated;
6888 dw_attr_node *at_binary_scale;
6889 dw_attr_node *at_bit_offset;
6890 dw_attr_node *at_bit_size;
6891 dw_attr_node *at_bit_stride;
6892 dw_attr_node *at_byte_size;
6893 dw_attr_node *at_byte_stride;
6894 dw_attr_node *at_const_value;
6895 dw_attr_node *at_containing_type;
6896 dw_attr_node *at_count;
6897 dw_attr_node *at_data_location;
6898 dw_attr_node *at_data_member_location;
6899 dw_attr_node *at_decimal_scale;
6900 dw_attr_node *at_decimal_sign;
6901 dw_attr_node *at_default_value;
6902 dw_attr_node *at_digit_count;
6903 dw_attr_node *at_discr;
6904 dw_attr_node *at_discr_list;
6905 dw_attr_node *at_discr_value;
6906 dw_attr_node *at_encoding;
6907 dw_attr_node *at_endianity;
6908 dw_attr_node *at_explicit;
6909 dw_attr_node *at_is_optional;
6910 dw_attr_node *at_location;
6911 dw_attr_node *at_lower_bound;
6912 dw_attr_node *at_mutable;
6913 dw_attr_node *at_ordering;
6914 dw_attr_node *at_picture_string;
6915 dw_attr_node *at_prototyped;
6916 dw_attr_node *at_small;
6917 dw_attr_node *at_segment;
6918 dw_attr_node *at_string_length;
6919 dw_attr_node *at_string_length_bit_size;
6920 dw_attr_node *at_string_length_byte_size;
6921 dw_attr_node *at_threads_scaled;
6922 dw_attr_node *at_upper_bound;
6923 dw_attr_node *at_use_location;
6924 dw_attr_node *at_use_UTF8;
6925 dw_attr_node *at_variable_parameter;
6926 dw_attr_node *at_virtuality;
6927 dw_attr_node *at_visibility;
6928 dw_attr_node *at_vtable_elem_location;
6931 /* Collect the attributes that we will want to use for the checksum. */
6933 static void
6934 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6936 dw_attr_node *a;
6937 unsigned ix;
6939 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6941 switch (a->dw_attr)
6943 case DW_AT_name:
6944 attrs->at_name = a;
6945 break;
6946 case DW_AT_type:
6947 attrs->at_type = a;
6948 break;
6949 case DW_AT_friend:
6950 attrs->at_friend = a;
6951 break;
6952 case DW_AT_accessibility:
6953 attrs->at_accessibility = a;
6954 break;
6955 case DW_AT_address_class:
6956 attrs->at_address_class = a;
6957 break;
6958 case DW_AT_alignment:
6959 attrs->at_alignment = a;
6960 break;
6961 case DW_AT_allocated:
6962 attrs->at_allocated = a;
6963 break;
6964 case DW_AT_artificial:
6965 attrs->at_artificial = a;
6966 break;
6967 case DW_AT_associated:
6968 attrs->at_associated = a;
6969 break;
6970 case DW_AT_binary_scale:
6971 attrs->at_binary_scale = a;
6972 break;
6973 case DW_AT_bit_offset:
6974 attrs->at_bit_offset = a;
6975 break;
6976 case DW_AT_bit_size:
6977 attrs->at_bit_size = a;
6978 break;
6979 case DW_AT_bit_stride:
6980 attrs->at_bit_stride = a;
6981 break;
6982 case DW_AT_byte_size:
6983 attrs->at_byte_size = a;
6984 break;
6985 case DW_AT_byte_stride:
6986 attrs->at_byte_stride = a;
6987 break;
6988 case DW_AT_const_value:
6989 attrs->at_const_value = a;
6990 break;
6991 case DW_AT_containing_type:
6992 attrs->at_containing_type = a;
6993 break;
6994 case DW_AT_count:
6995 attrs->at_count = a;
6996 break;
6997 case DW_AT_data_location:
6998 attrs->at_data_location = a;
6999 break;
7000 case DW_AT_data_member_location:
7001 attrs->at_data_member_location = a;
7002 break;
7003 case DW_AT_decimal_scale:
7004 attrs->at_decimal_scale = a;
7005 break;
7006 case DW_AT_decimal_sign:
7007 attrs->at_decimal_sign = a;
7008 break;
7009 case DW_AT_default_value:
7010 attrs->at_default_value = a;
7011 break;
7012 case DW_AT_digit_count:
7013 attrs->at_digit_count = a;
7014 break;
7015 case DW_AT_discr:
7016 attrs->at_discr = a;
7017 break;
7018 case DW_AT_discr_list:
7019 attrs->at_discr_list = a;
7020 break;
7021 case DW_AT_discr_value:
7022 attrs->at_discr_value = a;
7023 break;
7024 case DW_AT_encoding:
7025 attrs->at_encoding = a;
7026 break;
7027 case DW_AT_endianity:
7028 attrs->at_endianity = a;
7029 break;
7030 case DW_AT_explicit:
7031 attrs->at_explicit = a;
7032 break;
7033 case DW_AT_is_optional:
7034 attrs->at_is_optional = a;
7035 break;
7036 case DW_AT_location:
7037 attrs->at_location = a;
7038 break;
7039 case DW_AT_lower_bound:
7040 attrs->at_lower_bound = a;
7041 break;
7042 case DW_AT_mutable:
7043 attrs->at_mutable = a;
7044 break;
7045 case DW_AT_ordering:
7046 attrs->at_ordering = a;
7047 break;
7048 case DW_AT_picture_string:
7049 attrs->at_picture_string = a;
7050 break;
7051 case DW_AT_prototyped:
7052 attrs->at_prototyped = a;
7053 break;
7054 case DW_AT_small:
7055 attrs->at_small = a;
7056 break;
7057 case DW_AT_segment:
7058 attrs->at_segment = a;
7059 break;
7060 case DW_AT_string_length:
7061 attrs->at_string_length = a;
7062 break;
7063 case DW_AT_string_length_bit_size:
7064 attrs->at_string_length_bit_size = a;
7065 break;
7066 case DW_AT_string_length_byte_size:
7067 attrs->at_string_length_byte_size = a;
7068 break;
7069 case DW_AT_threads_scaled:
7070 attrs->at_threads_scaled = a;
7071 break;
7072 case DW_AT_upper_bound:
7073 attrs->at_upper_bound = a;
7074 break;
7075 case DW_AT_use_location:
7076 attrs->at_use_location = a;
7077 break;
7078 case DW_AT_use_UTF8:
7079 attrs->at_use_UTF8 = a;
7080 break;
7081 case DW_AT_variable_parameter:
7082 attrs->at_variable_parameter = a;
7083 break;
7084 case DW_AT_virtuality:
7085 attrs->at_virtuality = a;
7086 break;
7087 case DW_AT_visibility:
7088 attrs->at_visibility = a;
7089 break;
7090 case DW_AT_vtable_elem_location:
7091 attrs->at_vtable_elem_location = a;
7092 break;
7093 default:
7094 break;
7099 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7101 static void
7102 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7104 dw_die_ref c;
7105 dw_die_ref decl;
7106 struct checksum_attributes attrs;
7108 CHECKSUM_ULEB128 ('D');
7109 CHECKSUM_ULEB128 (die->die_tag);
7111 memset (&attrs, 0, sizeof (attrs));
7113 decl = get_AT_ref (die, DW_AT_specification);
7114 if (decl != NULL)
7115 collect_checksum_attributes (&attrs, decl);
7116 collect_checksum_attributes (&attrs, die);
7118 CHECKSUM_ATTR (attrs.at_name);
7119 CHECKSUM_ATTR (attrs.at_accessibility);
7120 CHECKSUM_ATTR (attrs.at_address_class);
7121 CHECKSUM_ATTR (attrs.at_allocated);
7122 CHECKSUM_ATTR (attrs.at_artificial);
7123 CHECKSUM_ATTR (attrs.at_associated);
7124 CHECKSUM_ATTR (attrs.at_binary_scale);
7125 CHECKSUM_ATTR (attrs.at_bit_offset);
7126 CHECKSUM_ATTR (attrs.at_bit_size);
7127 CHECKSUM_ATTR (attrs.at_bit_stride);
7128 CHECKSUM_ATTR (attrs.at_byte_size);
7129 CHECKSUM_ATTR (attrs.at_byte_stride);
7130 CHECKSUM_ATTR (attrs.at_const_value);
7131 CHECKSUM_ATTR (attrs.at_containing_type);
7132 CHECKSUM_ATTR (attrs.at_count);
7133 CHECKSUM_ATTR (attrs.at_data_location);
7134 CHECKSUM_ATTR (attrs.at_data_member_location);
7135 CHECKSUM_ATTR (attrs.at_decimal_scale);
7136 CHECKSUM_ATTR (attrs.at_decimal_sign);
7137 CHECKSUM_ATTR (attrs.at_default_value);
7138 CHECKSUM_ATTR (attrs.at_digit_count);
7139 CHECKSUM_ATTR (attrs.at_discr);
7140 CHECKSUM_ATTR (attrs.at_discr_list);
7141 CHECKSUM_ATTR (attrs.at_discr_value);
7142 CHECKSUM_ATTR (attrs.at_encoding);
7143 CHECKSUM_ATTR (attrs.at_endianity);
7144 CHECKSUM_ATTR (attrs.at_explicit);
7145 CHECKSUM_ATTR (attrs.at_is_optional);
7146 CHECKSUM_ATTR (attrs.at_location);
7147 CHECKSUM_ATTR (attrs.at_lower_bound);
7148 CHECKSUM_ATTR (attrs.at_mutable);
7149 CHECKSUM_ATTR (attrs.at_ordering);
7150 CHECKSUM_ATTR (attrs.at_picture_string);
7151 CHECKSUM_ATTR (attrs.at_prototyped);
7152 CHECKSUM_ATTR (attrs.at_small);
7153 CHECKSUM_ATTR (attrs.at_segment);
7154 CHECKSUM_ATTR (attrs.at_string_length);
7155 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7156 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7157 CHECKSUM_ATTR (attrs.at_threads_scaled);
7158 CHECKSUM_ATTR (attrs.at_upper_bound);
7159 CHECKSUM_ATTR (attrs.at_use_location);
7160 CHECKSUM_ATTR (attrs.at_use_UTF8);
7161 CHECKSUM_ATTR (attrs.at_variable_parameter);
7162 CHECKSUM_ATTR (attrs.at_virtuality);
7163 CHECKSUM_ATTR (attrs.at_visibility);
7164 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7165 CHECKSUM_ATTR (attrs.at_type);
7166 CHECKSUM_ATTR (attrs.at_friend);
7167 CHECKSUM_ATTR (attrs.at_alignment);
7169 /* Checksum the child DIEs. */
7170 c = die->die_child;
7171 if (c) do {
7172 dw_attr_node *name_attr;
7174 c = c->die_sib;
7175 name_attr = get_AT (c, DW_AT_name);
7176 if (is_template_instantiation (c))
7178 /* Ignore instantiations of member type and function templates. */
7180 else if (name_attr != NULL
7181 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7183 /* Use a shallow checksum for named nested types and member
7184 functions. */
7185 CHECKSUM_ULEB128 ('S');
7186 CHECKSUM_ULEB128 (c->die_tag);
7187 CHECKSUM_STRING (AT_string (name_attr));
7189 else
7191 /* Use a deep checksum for other children. */
7192 /* Mark this DIE so it gets processed when unmarking. */
7193 if (c->die_mark == 0)
7194 c->die_mark = -1;
7195 die_checksum_ordered (c, ctx, mark);
7197 } while (c != die->die_child);
7199 CHECKSUM_ULEB128 (0);
7202 /* Add a type name and tag to a hash. */
7203 static void
7204 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7206 CHECKSUM_ULEB128 (tag);
7207 CHECKSUM_STRING (name);
7210 #undef CHECKSUM
7211 #undef CHECKSUM_STRING
7212 #undef CHECKSUM_ATTR
7213 #undef CHECKSUM_LEB128
7214 #undef CHECKSUM_ULEB128
7216 /* Generate the type signature for DIE. This is computed by generating an
7217 MD5 checksum over the DIE's tag, its relevant attributes, and its
7218 children. Attributes that are references to other DIEs are processed
7219 by recursion, using the MARK field to prevent infinite recursion.
7220 If the DIE is nested inside a namespace or another type, we also
7221 need to include that context in the signature. The lower 64 bits
7222 of the resulting MD5 checksum comprise the signature. */
7224 static void
7225 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7227 int mark;
7228 const char *name;
7229 unsigned char checksum[16];
7230 struct md5_ctx ctx;
7231 dw_die_ref decl;
7232 dw_die_ref parent;
7234 name = get_AT_string (die, DW_AT_name);
7235 decl = get_AT_ref (die, DW_AT_specification);
7236 parent = get_die_parent (die);
7238 /* First, compute a signature for just the type name (and its surrounding
7239 context, if any. This is stored in the type unit DIE for link-time
7240 ODR (one-definition rule) checking. */
7242 if (is_cxx () && name != NULL)
7244 md5_init_ctx (&ctx);
7246 /* Checksum the names of surrounding namespaces and structures. */
7247 if (parent != NULL)
7248 checksum_die_context (parent, &ctx);
7250 /* Checksum the current DIE. */
7251 die_odr_checksum (die->die_tag, name, &ctx);
7252 md5_finish_ctx (&ctx, checksum);
7254 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7257 /* Next, compute the complete type signature. */
7259 md5_init_ctx (&ctx);
7260 mark = 1;
7261 die->die_mark = mark;
7263 /* Checksum the names of surrounding namespaces and structures. */
7264 if (parent != NULL)
7265 checksum_die_context (parent, &ctx);
7267 /* Checksum the DIE and its children. */
7268 die_checksum_ordered (die, &ctx, &mark);
7269 unmark_all_dies (die);
7270 md5_finish_ctx (&ctx, checksum);
7272 /* Store the signature in the type node and link the type DIE and the
7273 type node together. */
7274 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7275 DWARF_TYPE_SIGNATURE_SIZE);
7276 die->comdat_type_p = true;
7277 die->die_id.die_type_node = type_node;
7278 type_node->type_die = die;
7280 /* If the DIE is a specification, link its declaration to the type node
7281 as well. */
7282 if (decl != NULL)
7284 decl->comdat_type_p = true;
7285 decl->die_id.die_type_node = type_node;
7289 /* Do the location expressions look same? */
7290 static inline int
7291 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7293 return loc1->dw_loc_opc == loc2->dw_loc_opc
7294 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7295 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7298 /* Do the values look the same? */
7299 static int
7300 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7302 dw_loc_descr_ref loc1, loc2;
7303 rtx r1, r2;
7305 if (v1->val_class != v2->val_class)
7306 return 0;
7308 switch (v1->val_class)
7310 case dw_val_class_const:
7311 case dw_val_class_const_implicit:
7312 return v1->v.val_int == v2->v.val_int;
7313 case dw_val_class_unsigned_const:
7314 case dw_val_class_unsigned_const_implicit:
7315 return v1->v.val_unsigned == v2->v.val_unsigned;
7316 case dw_val_class_const_double:
7317 return v1->v.val_double.high == v2->v.val_double.high
7318 && v1->v.val_double.low == v2->v.val_double.low;
7319 case dw_val_class_wide_int:
7320 return *v1->v.val_wide == *v2->v.val_wide;
7321 case dw_val_class_vec:
7322 if (v1->v.val_vec.length != v2->v.val_vec.length
7323 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7324 return 0;
7325 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7326 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7327 return 0;
7328 return 1;
7329 case dw_val_class_flag:
7330 return v1->v.val_flag == v2->v.val_flag;
7331 case dw_val_class_str:
7332 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7334 case dw_val_class_addr:
7335 r1 = v1->v.val_addr;
7336 r2 = v2->v.val_addr;
7337 if (GET_CODE (r1) != GET_CODE (r2))
7338 return 0;
7339 return !rtx_equal_p (r1, r2);
7341 case dw_val_class_offset:
7342 return v1->v.val_offset == v2->v.val_offset;
7344 case dw_val_class_loc:
7345 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7346 loc1 && loc2;
7347 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7348 if (!same_loc_p (loc1, loc2, mark))
7349 return 0;
7350 return !loc1 && !loc2;
7352 case dw_val_class_die_ref:
7353 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7355 case dw_val_class_fde_ref:
7356 case dw_val_class_vms_delta:
7357 case dw_val_class_lbl_id:
7358 case dw_val_class_lineptr:
7359 case dw_val_class_macptr:
7360 case dw_val_class_loclistsptr:
7361 case dw_val_class_high_pc:
7362 return 1;
7364 case dw_val_class_file:
7365 case dw_val_class_file_implicit:
7366 return v1->v.val_file == v2->v.val_file;
7368 case dw_val_class_data8:
7369 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7371 default:
7372 return 1;
7376 /* Do the attributes look the same? */
7378 static int
7379 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7381 if (at1->dw_attr != at2->dw_attr)
7382 return 0;
7384 /* We don't care that this was compiled with a different compiler
7385 snapshot; if the output is the same, that's what matters. */
7386 if (at1->dw_attr == DW_AT_producer)
7387 return 1;
7389 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7392 /* Do the dies look the same? */
7394 static int
7395 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7397 dw_die_ref c1, c2;
7398 dw_attr_node *a1;
7399 unsigned ix;
7401 /* To avoid infinite recursion. */
7402 if (die1->die_mark)
7403 return die1->die_mark == die2->die_mark;
7404 die1->die_mark = die2->die_mark = ++(*mark);
7406 if (die1->die_tag != die2->die_tag)
7407 return 0;
7409 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7410 return 0;
7412 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7413 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7414 return 0;
7416 c1 = die1->die_child;
7417 c2 = die2->die_child;
7418 if (! c1)
7420 if (c2)
7421 return 0;
7423 else
7424 for (;;)
7426 if (!same_die_p (c1, c2, mark))
7427 return 0;
7428 c1 = c1->die_sib;
7429 c2 = c2->die_sib;
7430 if (c1 == die1->die_child)
7432 if (c2 == die2->die_child)
7433 break;
7434 else
7435 return 0;
7439 return 1;
7442 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7443 children, and set die_symbol. */
7445 static void
7446 compute_comp_unit_symbol (dw_die_ref unit_die)
7448 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7449 const char *base = die_name ? lbasename (die_name) : "anonymous";
7450 char *name = XALLOCAVEC (char, strlen (base) + 64);
7451 char *p;
7452 int i, mark;
7453 unsigned char checksum[16];
7454 struct md5_ctx ctx;
7456 /* Compute the checksum of the DIE, then append part of it as hex digits to
7457 the name filename of the unit. */
7459 md5_init_ctx (&ctx);
7460 mark = 0;
7461 die_checksum (unit_die, &ctx, &mark);
7462 unmark_all_dies (unit_die);
7463 md5_finish_ctx (&ctx, checksum);
7465 /* When we this for comp_unit_die () we have a DW_AT_name that might
7466 not start with a letter but with anything valid for filenames and
7467 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7468 character is not a letter. */
7469 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7470 clean_symbol_name (name);
7472 p = name + strlen (name);
7473 for (i = 0; i < 4; i++)
7475 sprintf (p, "%.2x", checksum[i]);
7476 p += 2;
7479 unit_die->die_id.die_symbol = xstrdup (name);
7482 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7484 static int
7485 is_type_die (dw_die_ref die)
7487 switch (die->die_tag)
7489 case DW_TAG_array_type:
7490 case DW_TAG_class_type:
7491 case DW_TAG_interface_type:
7492 case DW_TAG_enumeration_type:
7493 case DW_TAG_pointer_type:
7494 case DW_TAG_reference_type:
7495 case DW_TAG_rvalue_reference_type:
7496 case DW_TAG_string_type:
7497 case DW_TAG_structure_type:
7498 case DW_TAG_subroutine_type:
7499 case DW_TAG_union_type:
7500 case DW_TAG_ptr_to_member_type:
7501 case DW_TAG_set_type:
7502 case DW_TAG_subrange_type:
7503 case DW_TAG_base_type:
7504 case DW_TAG_const_type:
7505 case DW_TAG_file_type:
7506 case DW_TAG_packed_type:
7507 case DW_TAG_volatile_type:
7508 case DW_TAG_typedef:
7509 return 1;
7510 default:
7511 return 0;
7515 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7516 Basically, we want to choose the bits that are likely to be shared between
7517 compilations (types) and leave out the bits that are specific to individual
7518 compilations (functions). */
7520 static int
7521 is_comdat_die (dw_die_ref c)
7523 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7524 we do for stabs. The advantage is a greater likelihood of sharing between
7525 objects that don't include headers in the same order (and therefore would
7526 put the base types in a different comdat). jason 8/28/00 */
7528 if (c->die_tag == DW_TAG_base_type)
7529 return 0;
7531 if (c->die_tag == DW_TAG_pointer_type
7532 || c->die_tag == DW_TAG_reference_type
7533 || c->die_tag == DW_TAG_rvalue_reference_type
7534 || c->die_tag == DW_TAG_const_type
7535 || c->die_tag == DW_TAG_volatile_type)
7537 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7539 return t ? is_comdat_die (t) : 0;
7542 return is_type_die (c);
7545 /* Returns true iff C is a compile-unit DIE. */
7547 static inline bool
7548 is_cu_die (dw_die_ref c)
7550 return c && (c->die_tag == DW_TAG_compile_unit
7551 || c->die_tag == DW_TAG_skeleton_unit);
7554 /* Returns true iff C is a unit DIE of some sort. */
7556 static inline bool
7557 is_unit_die (dw_die_ref c)
7559 return c && (c->die_tag == DW_TAG_compile_unit
7560 || c->die_tag == DW_TAG_partial_unit
7561 || c->die_tag == DW_TAG_type_unit
7562 || c->die_tag == DW_TAG_skeleton_unit);
7565 /* Returns true iff C is a namespace DIE. */
7567 static inline bool
7568 is_namespace_die (dw_die_ref c)
7570 return c && c->die_tag == DW_TAG_namespace;
7573 /* Returns true iff C is a class or structure DIE. */
7575 static inline bool
7576 is_class_die (dw_die_ref c)
7578 return c && (c->die_tag == DW_TAG_class_type
7579 || c->die_tag == DW_TAG_structure_type);
7582 /* Return non-zero if this DIE is a template parameter. */
7584 static inline bool
7585 is_template_parameter (dw_die_ref die)
7587 switch (die->die_tag)
7589 case DW_TAG_template_type_param:
7590 case DW_TAG_template_value_param:
7591 case DW_TAG_GNU_template_template_param:
7592 case DW_TAG_GNU_template_parameter_pack:
7593 return true;
7594 default:
7595 return false;
7599 /* Return non-zero if this DIE represents a template instantiation. */
7601 static inline bool
7602 is_template_instantiation (dw_die_ref die)
7604 dw_die_ref c;
7606 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7607 return false;
7608 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7609 return false;
7612 static char *
7613 gen_internal_sym (const char *prefix)
7615 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7617 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7618 return xstrdup (buf);
7621 /* Return non-zero if this DIE is a declaration. */
7623 static int
7624 is_declaration_die (dw_die_ref die)
7626 dw_attr_node *a;
7627 unsigned ix;
7629 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7630 if (a->dw_attr == DW_AT_declaration)
7631 return 1;
7633 return 0;
7636 /* Return non-zero if this DIE is nested inside a subprogram. */
7638 static int
7639 is_nested_in_subprogram (dw_die_ref die)
7641 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7643 if (decl == NULL)
7644 decl = die;
7645 return local_scope_p (decl);
7648 /* Return non-zero if this DIE contains a defining declaration of a
7649 subprogram. */
7651 static int
7652 contains_subprogram_definition (dw_die_ref die)
7654 dw_die_ref c;
7656 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7657 return 1;
7658 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7659 return 0;
7662 /* Return non-zero if this is a type DIE that should be moved to a
7663 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7664 unit type. */
7666 static int
7667 should_move_die_to_comdat (dw_die_ref die)
7669 switch (die->die_tag)
7671 case DW_TAG_class_type:
7672 case DW_TAG_structure_type:
7673 case DW_TAG_enumeration_type:
7674 case DW_TAG_union_type:
7675 /* Don't move declarations, inlined instances, types nested in a
7676 subprogram, or types that contain subprogram definitions. */
7677 if (is_declaration_die (die)
7678 || get_AT (die, DW_AT_abstract_origin)
7679 || is_nested_in_subprogram (die)
7680 || contains_subprogram_definition (die))
7681 return 0;
7682 return 1;
7683 case DW_TAG_array_type:
7684 case DW_TAG_interface_type:
7685 case DW_TAG_pointer_type:
7686 case DW_TAG_reference_type:
7687 case DW_TAG_rvalue_reference_type:
7688 case DW_TAG_string_type:
7689 case DW_TAG_subroutine_type:
7690 case DW_TAG_ptr_to_member_type:
7691 case DW_TAG_set_type:
7692 case DW_TAG_subrange_type:
7693 case DW_TAG_base_type:
7694 case DW_TAG_const_type:
7695 case DW_TAG_file_type:
7696 case DW_TAG_packed_type:
7697 case DW_TAG_volatile_type:
7698 case DW_TAG_typedef:
7699 default:
7700 return 0;
7704 /* Make a clone of DIE. */
7706 static dw_die_ref
7707 clone_die (dw_die_ref die)
7709 dw_die_ref clone;
7710 dw_attr_node *a;
7711 unsigned ix;
7713 clone = ggc_cleared_alloc<die_node> ();
7714 clone->die_tag = die->die_tag;
7716 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7717 add_dwarf_attr (clone, a);
7719 return clone;
7722 /* Make a clone of the tree rooted at DIE. */
7724 static dw_die_ref
7725 clone_tree (dw_die_ref die)
7727 dw_die_ref c;
7728 dw_die_ref clone = clone_die (die);
7730 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7732 return clone;
7735 /* Make a clone of DIE as a declaration. */
7737 static dw_die_ref
7738 clone_as_declaration (dw_die_ref die)
7740 dw_die_ref clone;
7741 dw_die_ref decl;
7742 dw_attr_node *a;
7743 unsigned ix;
7745 /* If the DIE is already a declaration, just clone it. */
7746 if (is_declaration_die (die))
7747 return clone_die (die);
7749 /* If the DIE is a specification, just clone its declaration DIE. */
7750 decl = get_AT_ref (die, DW_AT_specification);
7751 if (decl != NULL)
7753 clone = clone_die (decl);
7754 if (die->comdat_type_p)
7755 add_AT_die_ref (clone, DW_AT_signature, die);
7756 return clone;
7759 clone = ggc_cleared_alloc<die_node> ();
7760 clone->die_tag = die->die_tag;
7762 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7764 /* We don't want to copy over all attributes.
7765 For example we don't want DW_AT_byte_size because otherwise we will no
7766 longer have a declaration and GDB will treat it as a definition. */
7768 switch (a->dw_attr)
7770 case DW_AT_abstract_origin:
7771 case DW_AT_artificial:
7772 case DW_AT_containing_type:
7773 case DW_AT_external:
7774 case DW_AT_name:
7775 case DW_AT_type:
7776 case DW_AT_virtuality:
7777 case DW_AT_linkage_name:
7778 case DW_AT_MIPS_linkage_name:
7779 add_dwarf_attr (clone, a);
7780 break;
7781 case DW_AT_byte_size:
7782 case DW_AT_alignment:
7783 default:
7784 break;
7788 if (die->comdat_type_p)
7789 add_AT_die_ref (clone, DW_AT_signature, die);
7791 add_AT_flag (clone, DW_AT_declaration, 1);
7792 return clone;
7796 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7798 struct decl_table_entry
7800 dw_die_ref orig;
7801 dw_die_ref copy;
7804 /* Helpers to manipulate hash table of copied declarations. */
7806 /* Hashtable helpers. */
7808 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7810 typedef die_struct *compare_type;
7811 static inline hashval_t hash (const decl_table_entry *);
7812 static inline bool equal (const decl_table_entry *, const die_struct *);
7815 inline hashval_t
7816 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7818 return htab_hash_pointer (entry->orig);
7821 inline bool
7822 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7823 const die_struct *entry2)
7825 return entry1->orig == entry2;
7828 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7830 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7831 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7832 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7833 to check if the ancestor has already been copied into UNIT. */
7835 static dw_die_ref
7836 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7837 decl_hash_type *decl_table)
7839 dw_die_ref parent = die->die_parent;
7840 dw_die_ref new_parent = unit;
7841 dw_die_ref copy;
7842 decl_table_entry **slot = NULL;
7843 struct decl_table_entry *entry = NULL;
7845 if (decl_table)
7847 /* Check if the entry has already been copied to UNIT. */
7848 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7849 INSERT);
7850 if (*slot != HTAB_EMPTY_ENTRY)
7852 entry = *slot;
7853 return entry->copy;
7856 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7857 entry = XCNEW (struct decl_table_entry);
7858 entry->orig = die;
7859 entry->copy = NULL;
7860 *slot = entry;
7863 if (parent != NULL)
7865 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7866 if (spec != NULL)
7867 parent = spec;
7868 if (!is_unit_die (parent))
7869 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7872 copy = clone_as_declaration (die);
7873 add_child_die (new_parent, copy);
7875 if (decl_table)
7877 /* Record the pointer to the copy. */
7878 entry->copy = copy;
7881 return copy;
7883 /* Copy the declaration context to the new type unit DIE. This includes
7884 any surrounding namespace or type declarations. If the DIE has an
7885 AT_specification attribute, it also includes attributes and children
7886 attached to the specification, and returns a pointer to the original
7887 parent of the declaration DIE. Returns NULL otherwise. */
7889 static dw_die_ref
7890 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7892 dw_die_ref decl;
7893 dw_die_ref new_decl;
7894 dw_die_ref orig_parent = NULL;
7896 decl = get_AT_ref (die, DW_AT_specification);
7897 if (decl == NULL)
7898 decl = die;
7899 else
7901 unsigned ix;
7902 dw_die_ref c;
7903 dw_attr_node *a;
7905 /* The original DIE will be changed to a declaration, and must
7906 be moved to be a child of the original declaration DIE. */
7907 orig_parent = decl->die_parent;
7909 /* Copy the type node pointer from the new DIE to the original
7910 declaration DIE so we can forward references later. */
7911 decl->comdat_type_p = true;
7912 decl->die_id.die_type_node = die->die_id.die_type_node;
7914 remove_AT (die, DW_AT_specification);
7916 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7918 if (a->dw_attr != DW_AT_name
7919 && a->dw_attr != DW_AT_declaration
7920 && a->dw_attr != DW_AT_external)
7921 add_dwarf_attr (die, a);
7924 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7927 if (decl->die_parent != NULL
7928 && !is_unit_die (decl->die_parent))
7930 new_decl = copy_ancestor_tree (unit, decl, NULL);
7931 if (new_decl != NULL)
7933 remove_AT (new_decl, DW_AT_signature);
7934 add_AT_specification (die, new_decl);
7938 return orig_parent;
7941 /* Generate the skeleton ancestor tree for the given NODE, then clone
7942 the DIE and add the clone into the tree. */
7944 static void
7945 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7947 if (node->new_die != NULL)
7948 return;
7950 node->new_die = clone_as_declaration (node->old_die);
7952 if (node->parent != NULL)
7954 generate_skeleton_ancestor_tree (node->parent);
7955 add_child_die (node->parent->new_die, node->new_die);
7959 /* Generate a skeleton tree of DIEs containing any declarations that are
7960 found in the original tree. We traverse the tree looking for declaration
7961 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7963 static void
7964 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7966 skeleton_chain_node node;
7967 dw_die_ref c;
7968 dw_die_ref first;
7969 dw_die_ref prev = NULL;
7970 dw_die_ref next = NULL;
7972 node.parent = parent;
7974 first = c = parent->old_die->die_child;
7975 if (c)
7976 next = c->die_sib;
7977 if (c) do {
7978 if (prev == NULL || prev->die_sib == c)
7979 prev = c;
7980 c = next;
7981 next = (c == first ? NULL : c->die_sib);
7982 node.old_die = c;
7983 node.new_die = NULL;
7984 if (is_declaration_die (c))
7986 if (is_template_instantiation (c))
7988 /* Instantiated templates do not need to be cloned into the
7989 type unit. Just move the DIE and its children back to
7990 the skeleton tree (in the main CU). */
7991 remove_child_with_prev (c, prev);
7992 add_child_die (parent->new_die, c);
7993 c = prev;
7995 else if (c->comdat_type_p)
7997 /* This is the skeleton of earlier break_out_comdat_types
7998 type. Clone the existing DIE, but keep the children
7999 under the original (which is in the main CU). */
8000 dw_die_ref clone = clone_die (c);
8002 replace_child (c, clone, prev);
8003 generate_skeleton_ancestor_tree (parent);
8004 add_child_die (parent->new_die, c);
8005 c = clone;
8006 continue;
8008 else
8010 /* Clone the existing DIE, move the original to the skeleton
8011 tree (which is in the main CU), and put the clone, with
8012 all the original's children, where the original came from
8013 (which is about to be moved to the type unit). */
8014 dw_die_ref clone = clone_die (c);
8015 move_all_children (c, clone);
8017 /* If the original has a DW_AT_object_pointer attribute,
8018 it would now point to a child DIE just moved to the
8019 cloned tree, so we need to remove that attribute from
8020 the original. */
8021 remove_AT (c, DW_AT_object_pointer);
8023 replace_child (c, clone, prev);
8024 generate_skeleton_ancestor_tree (parent);
8025 add_child_die (parent->new_die, c);
8026 node.old_die = clone;
8027 node.new_die = c;
8028 c = clone;
8031 generate_skeleton_bottom_up (&node);
8032 } while (next != NULL);
8035 /* Wrapper function for generate_skeleton_bottom_up. */
8037 static dw_die_ref
8038 generate_skeleton (dw_die_ref die)
8040 skeleton_chain_node node;
8042 node.old_die = die;
8043 node.new_die = NULL;
8044 node.parent = NULL;
8046 /* If this type definition is nested inside another type,
8047 and is not an instantiation of a template, always leave
8048 at least a declaration in its place. */
8049 if (die->die_parent != NULL
8050 && is_type_die (die->die_parent)
8051 && !is_template_instantiation (die))
8052 node.new_die = clone_as_declaration (die);
8054 generate_skeleton_bottom_up (&node);
8055 return node.new_die;
8058 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8059 declaration. The original DIE is moved to a new compile unit so that
8060 existing references to it follow it to the new location. If any of the
8061 original DIE's descendants is a declaration, we need to replace the
8062 original DIE with a skeleton tree and move the declarations back into the
8063 skeleton tree. */
8065 static dw_die_ref
8066 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8067 dw_die_ref prev)
8069 dw_die_ref skeleton, orig_parent;
8071 /* Copy the declaration context to the type unit DIE. If the returned
8072 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8073 that DIE. */
8074 orig_parent = copy_declaration_context (unit, child);
8076 skeleton = generate_skeleton (child);
8077 if (skeleton == NULL)
8078 remove_child_with_prev (child, prev);
8079 else
8081 skeleton->comdat_type_p = true;
8082 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8084 /* If the original DIE was a specification, we need to put
8085 the skeleton under the parent DIE of the declaration.
8086 This leaves the original declaration in the tree, but
8087 it will be pruned later since there are no longer any
8088 references to it. */
8089 if (orig_parent != NULL)
8091 remove_child_with_prev (child, prev);
8092 add_child_die (orig_parent, skeleton);
8094 else
8095 replace_child (child, skeleton, prev);
8098 return skeleton;
8101 static void
8102 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8103 comdat_type_node *type_node,
8104 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8106 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8107 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8108 DWARF procedure references in the DW_AT_location attribute. */
8110 static dw_die_ref
8111 copy_dwarf_procedure (dw_die_ref die,
8112 comdat_type_node *type_node,
8113 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8115 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8117 /* DWARF procedures are not supposed to have children... */
8118 gcc_assert (die->die_child == NULL);
8120 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8121 gcc_assert (vec_safe_length (die->die_attr) == 1
8122 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8124 /* Do not copy more than once DWARF procedures. */
8125 bool existed;
8126 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8127 if (existed)
8128 return die_copy;
8130 die_copy = clone_die (die);
8131 add_child_die (type_node->root_die, die_copy);
8132 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8133 return die_copy;
8136 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8137 procedures in DIE's attributes. */
8139 static void
8140 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8141 comdat_type_node *type_node,
8142 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8144 dw_attr_node *a;
8145 unsigned i;
8147 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8149 dw_loc_descr_ref loc;
8151 if (a->dw_attr_val.val_class != dw_val_class_loc)
8152 continue;
8154 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8156 switch (loc->dw_loc_opc)
8158 case DW_OP_call2:
8159 case DW_OP_call4:
8160 case DW_OP_call_ref:
8161 gcc_assert (loc->dw_loc_oprnd1.val_class
8162 == dw_val_class_die_ref);
8163 loc->dw_loc_oprnd1.v.val_die_ref.die
8164 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8165 type_node,
8166 copied_dwarf_procs);
8168 default:
8169 break;
8175 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8176 rewrite references to point to the copies.
8178 References are looked for in DIE's attributes and recursively in all its
8179 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8180 mapping from old DWARF procedures to their copy. It is used not to copy
8181 twice the same DWARF procedure under TYPE_NODE. */
8183 static void
8184 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8185 comdat_type_node *type_node,
8186 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8188 dw_die_ref c;
8190 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8191 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8192 type_node,
8193 copied_dwarf_procs));
8196 /* Traverse the DIE and set up additional .debug_types or .debug_info
8197 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8198 section. */
8200 static void
8201 break_out_comdat_types (dw_die_ref die)
8203 dw_die_ref c;
8204 dw_die_ref first;
8205 dw_die_ref prev = NULL;
8206 dw_die_ref next = NULL;
8207 dw_die_ref unit = NULL;
8209 first = c = die->die_child;
8210 if (c)
8211 next = c->die_sib;
8212 if (c) do {
8213 if (prev == NULL || prev->die_sib == c)
8214 prev = c;
8215 c = next;
8216 next = (c == first ? NULL : c->die_sib);
8217 if (should_move_die_to_comdat (c))
8219 dw_die_ref replacement;
8220 comdat_type_node *type_node;
8222 /* Break out nested types into their own type units. */
8223 break_out_comdat_types (c);
8225 /* Create a new type unit DIE as the root for the new tree, and
8226 add it to the list of comdat types. */
8227 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8228 add_AT_unsigned (unit, DW_AT_language,
8229 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8230 type_node = ggc_cleared_alloc<comdat_type_node> ();
8231 type_node->root_die = unit;
8232 type_node->next = comdat_type_list;
8233 comdat_type_list = type_node;
8235 /* Generate the type signature. */
8236 generate_type_signature (c, type_node);
8238 /* Copy the declaration context, attributes, and children of the
8239 declaration into the new type unit DIE, then remove this DIE
8240 from the main CU (or replace it with a skeleton if necessary). */
8241 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8242 type_node->skeleton_die = replacement;
8244 /* Add the DIE to the new compunit. */
8245 add_child_die (unit, c);
8247 /* Types can reference DWARF procedures for type size or data location
8248 expressions. Calls in DWARF expressions cannot target procedures
8249 that are not in the same section. So we must copy DWARF procedures
8250 along with this type and then rewrite references to them. */
8251 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8252 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8254 if (replacement != NULL)
8255 c = replacement;
8257 else if (c->die_tag == DW_TAG_namespace
8258 || c->die_tag == DW_TAG_class_type
8259 || c->die_tag == DW_TAG_structure_type
8260 || c->die_tag == DW_TAG_union_type)
8262 /* Look for nested types that can be broken out. */
8263 break_out_comdat_types (c);
8265 } while (next != NULL);
8268 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8269 Enter all the cloned children into the hash table decl_table. */
8271 static dw_die_ref
8272 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8274 dw_die_ref c;
8275 dw_die_ref clone;
8276 struct decl_table_entry *entry;
8277 decl_table_entry **slot;
8279 if (die->die_tag == DW_TAG_subprogram)
8280 clone = clone_as_declaration (die);
8281 else
8282 clone = clone_die (die);
8284 slot = decl_table->find_slot_with_hash (die,
8285 htab_hash_pointer (die), INSERT);
8287 /* Assert that DIE isn't in the hash table yet. If it would be there
8288 before, the ancestors would be necessarily there as well, therefore
8289 clone_tree_partial wouldn't be called. */
8290 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8292 entry = XCNEW (struct decl_table_entry);
8293 entry->orig = die;
8294 entry->copy = clone;
8295 *slot = entry;
8297 if (die->die_tag != DW_TAG_subprogram)
8298 FOR_EACH_CHILD (die, c,
8299 add_child_die (clone, clone_tree_partial (c, decl_table)));
8301 return clone;
8304 /* Walk the DIE and its children, looking for references to incomplete
8305 or trivial types that are unmarked (i.e., that are not in the current
8306 type_unit). */
8308 static void
8309 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8311 dw_die_ref c;
8312 dw_attr_node *a;
8313 unsigned ix;
8315 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8317 if (AT_class (a) == dw_val_class_die_ref)
8319 dw_die_ref targ = AT_ref (a);
8320 decl_table_entry **slot;
8321 struct decl_table_entry *entry;
8323 if (targ->die_mark != 0 || targ->comdat_type_p)
8324 continue;
8326 slot = decl_table->find_slot_with_hash (targ,
8327 htab_hash_pointer (targ),
8328 INSERT);
8330 if (*slot != HTAB_EMPTY_ENTRY)
8332 /* TARG has already been copied, so we just need to
8333 modify the reference to point to the copy. */
8334 entry = *slot;
8335 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8337 else
8339 dw_die_ref parent = unit;
8340 dw_die_ref copy = clone_die (targ);
8342 /* Record in DECL_TABLE that TARG has been copied.
8343 Need to do this now, before the recursive call,
8344 because DECL_TABLE may be expanded and SLOT
8345 would no longer be a valid pointer. */
8346 entry = XCNEW (struct decl_table_entry);
8347 entry->orig = targ;
8348 entry->copy = copy;
8349 *slot = entry;
8351 /* If TARG is not a declaration DIE, we need to copy its
8352 children. */
8353 if (!is_declaration_die (targ))
8355 FOR_EACH_CHILD (
8356 targ, c,
8357 add_child_die (copy,
8358 clone_tree_partial (c, decl_table)));
8361 /* Make sure the cloned tree is marked as part of the
8362 type unit. */
8363 mark_dies (copy);
8365 /* If TARG has surrounding context, copy its ancestor tree
8366 into the new type unit. */
8367 if (targ->die_parent != NULL
8368 && !is_unit_die (targ->die_parent))
8369 parent = copy_ancestor_tree (unit, targ->die_parent,
8370 decl_table);
8372 add_child_die (parent, copy);
8373 a->dw_attr_val.v.val_die_ref.die = copy;
8375 /* Make sure the newly-copied DIE is walked. If it was
8376 installed in a previously-added context, it won't
8377 get visited otherwise. */
8378 if (parent != unit)
8380 /* Find the highest point of the newly-added tree,
8381 mark each node along the way, and walk from there. */
8382 parent->die_mark = 1;
8383 while (parent->die_parent
8384 && parent->die_parent->die_mark == 0)
8386 parent = parent->die_parent;
8387 parent->die_mark = 1;
8389 copy_decls_walk (unit, parent, decl_table);
8395 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8398 /* Copy declarations for "unworthy" types into the new comdat section.
8399 Incomplete types, modified types, and certain other types aren't broken
8400 out into comdat sections of their own, so they don't have a signature,
8401 and we need to copy the declaration into the same section so that we
8402 don't have an external reference. */
8404 static void
8405 copy_decls_for_unworthy_types (dw_die_ref unit)
8407 mark_dies (unit);
8408 decl_hash_type decl_table (10);
8409 copy_decls_walk (unit, unit, &decl_table);
8410 unmark_dies (unit);
8413 /* Traverse the DIE and add a sibling attribute if it may have the
8414 effect of speeding up access to siblings. To save some space,
8415 avoid generating sibling attributes for DIE's without children. */
8417 static void
8418 add_sibling_attributes (dw_die_ref die)
8420 dw_die_ref c;
8422 if (! die->die_child)
8423 return;
8425 if (die->die_parent && die != die->die_parent->die_child)
8426 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8428 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8431 /* Output all location lists for the DIE and its children. */
8433 static void
8434 output_location_lists (dw_die_ref die)
8436 dw_die_ref c;
8437 dw_attr_node *a;
8438 unsigned ix;
8440 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8441 if (AT_class (a) == dw_val_class_loc_list)
8442 output_loc_list (AT_loc_list (a));
8444 FOR_EACH_CHILD (die, c, output_location_lists (c));
8447 /* During assign_location_list_indexes and output_loclists_offset the
8448 current index, after it the number of assigned indexes (i.e. how
8449 large the .debug_loclists* offset table should be). */
8450 static unsigned int loc_list_idx;
8452 /* Output all location list offsets for the DIE and its children. */
8454 static void
8455 output_loclists_offsets (dw_die_ref die)
8457 dw_die_ref c;
8458 dw_attr_node *a;
8459 unsigned ix;
8461 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8462 if (AT_class (a) == dw_val_class_loc_list)
8464 dw_loc_list_ref l = AT_loc_list (a);
8465 if (l->offset_emitted)
8466 continue;
8467 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8468 loc_section_label, NULL);
8469 gcc_assert (l->hash == loc_list_idx);
8470 loc_list_idx++;
8471 l->offset_emitted = true;
8474 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8477 /* Recursively set indexes of location lists. */
8479 static void
8480 assign_location_list_indexes (dw_die_ref die)
8482 dw_die_ref c;
8483 dw_attr_node *a;
8484 unsigned ix;
8486 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8487 if (AT_class (a) == dw_val_class_loc_list)
8489 dw_loc_list_ref list = AT_loc_list (a);
8490 if (!list->num_assigned)
8492 list->num_assigned = true;
8493 list->hash = loc_list_idx++;
8497 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8500 /* We want to limit the number of external references, because they are
8501 larger than local references: a relocation takes multiple words, and
8502 even a sig8 reference is always eight bytes, whereas a local reference
8503 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8504 So if we encounter multiple external references to the same type DIE, we
8505 make a local typedef stub for it and redirect all references there.
8507 This is the element of the hash table for keeping track of these
8508 references. */
8510 struct external_ref
8512 dw_die_ref type;
8513 dw_die_ref stub;
8514 unsigned n_refs;
8517 /* Hashtable helpers. */
8519 struct external_ref_hasher : free_ptr_hash <external_ref>
8521 static inline hashval_t hash (const external_ref *);
8522 static inline bool equal (const external_ref *, const external_ref *);
8525 inline hashval_t
8526 external_ref_hasher::hash (const external_ref *r)
8528 dw_die_ref die = r->type;
8529 hashval_t h = 0;
8531 /* We can't use the address of the DIE for hashing, because
8532 that will make the order of the stub DIEs non-deterministic. */
8533 if (! die->comdat_type_p)
8534 /* We have a symbol; use it to compute a hash. */
8535 h = htab_hash_string (die->die_id.die_symbol);
8536 else
8538 /* We have a type signature; use a subset of the bits as the hash.
8539 The 8-byte signature is at least as large as hashval_t. */
8540 comdat_type_node *type_node = die->die_id.die_type_node;
8541 memcpy (&h, type_node->signature, sizeof (h));
8543 return h;
8546 inline bool
8547 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8549 return r1->type == r2->type;
8552 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8554 /* Return a pointer to the external_ref for references to DIE. */
8556 static struct external_ref *
8557 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8559 struct external_ref ref, *ref_p;
8560 external_ref **slot;
8562 ref.type = die;
8563 slot = map->find_slot (&ref, INSERT);
8564 if (*slot != HTAB_EMPTY_ENTRY)
8565 return *slot;
8567 ref_p = XCNEW (struct external_ref);
8568 ref_p->type = die;
8569 *slot = ref_p;
8570 return ref_p;
8573 /* Subroutine of optimize_external_refs, below.
8575 If we see a type skeleton, record it as our stub. If we see external
8576 references, remember how many we've seen. */
8578 static void
8579 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8581 dw_die_ref c;
8582 dw_attr_node *a;
8583 unsigned ix;
8584 struct external_ref *ref_p;
8586 if (is_type_die (die)
8587 && (c = get_AT_ref (die, DW_AT_signature)))
8589 /* This is a local skeleton; use it for local references. */
8590 ref_p = lookup_external_ref (map, c);
8591 ref_p->stub = die;
8594 /* Scan the DIE references, and remember any that refer to DIEs from
8595 other CUs (i.e. those which are not marked). */
8596 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8597 if (AT_class (a) == dw_val_class_die_ref
8598 && (c = AT_ref (a))->die_mark == 0
8599 && is_type_die (c))
8601 ref_p = lookup_external_ref (map, c);
8602 ref_p->n_refs++;
8605 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8608 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8609 points to an external_ref, DATA is the CU we're processing. If we don't
8610 already have a local stub, and we have multiple refs, build a stub. */
8613 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8615 struct external_ref *ref_p = *slot;
8617 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8619 /* We have multiple references to this type, so build a small stub.
8620 Both of these forms are a bit dodgy from the perspective of the
8621 DWARF standard, since technically they should have names. */
8622 dw_die_ref cu = data;
8623 dw_die_ref type = ref_p->type;
8624 dw_die_ref stub = NULL;
8626 if (type->comdat_type_p)
8628 /* If we refer to this type via sig8, use AT_signature. */
8629 stub = new_die (type->die_tag, cu, NULL_TREE);
8630 add_AT_die_ref (stub, DW_AT_signature, type);
8632 else
8634 /* Otherwise, use a typedef with no name. */
8635 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8636 add_AT_die_ref (stub, DW_AT_type, type);
8639 stub->die_mark++;
8640 ref_p->stub = stub;
8642 return 1;
8645 /* DIE is a unit; look through all the DIE references to see if there are
8646 any external references to types, and if so, create local stubs for
8647 them which will be applied in build_abbrev_table. This is useful because
8648 references to local DIEs are smaller. */
8650 static external_ref_hash_type *
8651 optimize_external_refs (dw_die_ref die)
8653 external_ref_hash_type *map = new external_ref_hash_type (10);
8654 optimize_external_refs_1 (die, map);
8655 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8656 return map;
8659 /* The following 3 variables are temporaries that are computed only during the
8660 build_abbrev_table call and used and released during the following
8661 optimize_abbrev_table call. */
8663 /* First abbrev_id that can be optimized based on usage. */
8664 static unsigned int abbrev_opt_start;
8666 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8667 abbrev_id smaller than this, because they must be already sized
8668 during build_abbrev_table). */
8669 static unsigned int abbrev_opt_base_type_end;
8671 /* Vector of usage counts during build_abbrev_table. Indexed by
8672 abbrev_id - abbrev_opt_start. */
8673 static vec<unsigned int> abbrev_usage_count;
8675 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8676 static vec<dw_die_ref> sorted_abbrev_dies;
8678 /* The format of each DIE (and its attribute value pairs) is encoded in an
8679 abbreviation table. This routine builds the abbreviation table and assigns
8680 a unique abbreviation id for each abbreviation entry. The children of each
8681 die are visited recursively. */
8683 static void
8684 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8686 unsigned int abbrev_id = 0;
8687 dw_die_ref c;
8688 dw_attr_node *a;
8689 unsigned ix;
8690 dw_die_ref abbrev;
8692 /* Scan the DIE references, and replace any that refer to
8693 DIEs from other CUs (i.e. those which are not marked) with
8694 the local stubs we built in optimize_external_refs. */
8695 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8696 if (AT_class (a) == dw_val_class_die_ref
8697 && (c = AT_ref (a))->die_mark == 0)
8699 struct external_ref *ref_p;
8700 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8702 ref_p = lookup_external_ref (extern_map, c);
8703 if (ref_p->stub && ref_p->stub != die)
8704 change_AT_die_ref (a, ref_p->stub);
8705 else
8706 /* We aren't changing this reference, so mark it external. */
8707 set_AT_ref_external (a, 1);
8710 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8712 dw_attr_node *die_a, *abbrev_a;
8713 unsigned ix;
8714 bool ok = true;
8716 if (abbrev_id == 0)
8717 continue;
8718 if (abbrev->die_tag != die->die_tag)
8719 continue;
8720 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8721 continue;
8723 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8724 continue;
8726 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8728 abbrev_a = &(*abbrev->die_attr)[ix];
8729 if ((abbrev_a->dw_attr != die_a->dw_attr)
8730 || (value_format (abbrev_a) != value_format (die_a)))
8732 ok = false;
8733 break;
8736 if (ok)
8737 break;
8740 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8742 vec_safe_push (abbrev_die_table, die);
8743 if (abbrev_opt_start)
8744 abbrev_usage_count.safe_push (0);
8746 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8748 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8749 sorted_abbrev_dies.safe_push (die);
8752 die->die_abbrev = abbrev_id;
8753 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8756 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8757 by die_abbrev's usage count, from the most commonly used
8758 abbreviation to the least. */
8760 static int
8761 die_abbrev_cmp (const void *p1, const void *p2)
8763 dw_die_ref die1 = *(const dw_die_ref *) p1;
8764 dw_die_ref die2 = *(const dw_die_ref *) p2;
8766 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8767 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8769 if (die1->die_abbrev >= abbrev_opt_base_type_end
8770 && die2->die_abbrev >= abbrev_opt_base_type_end)
8772 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8773 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8774 return -1;
8775 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8776 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8777 return 1;
8780 /* Stabilize the sort. */
8781 if (die1->die_abbrev < die2->die_abbrev)
8782 return -1;
8783 if (die1->die_abbrev > die2->die_abbrev)
8784 return 1;
8786 return 0;
8789 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8790 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8791 into dw_val_class_const_implicit or
8792 dw_val_class_unsigned_const_implicit. */
8794 static void
8795 optimize_implicit_const (unsigned int first_id, unsigned int end,
8796 vec<bool> &implicit_consts)
8798 /* It never makes sense if there is just one DIE using the abbreviation. */
8799 if (end < first_id + 2)
8800 return;
8802 dw_attr_node *a;
8803 unsigned ix, i;
8804 dw_die_ref die = sorted_abbrev_dies[first_id];
8805 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8806 if (implicit_consts[ix])
8808 enum dw_val_class new_class = dw_val_class_none;
8809 switch (AT_class (a))
8811 case dw_val_class_unsigned_const:
8812 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8813 continue;
8815 /* The .debug_abbrev section will grow by
8816 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8817 in all the DIEs using that abbreviation. */
8818 if (constant_size (AT_unsigned (a)) * (end - first_id)
8819 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8820 continue;
8822 new_class = dw_val_class_unsigned_const_implicit;
8823 break;
8825 case dw_val_class_const:
8826 new_class = dw_val_class_const_implicit;
8827 break;
8829 case dw_val_class_file:
8830 new_class = dw_val_class_file_implicit;
8831 break;
8833 default:
8834 continue;
8836 for (i = first_id; i < end; i++)
8837 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8838 = new_class;
8842 /* Attempt to optimize abbreviation table from abbrev_opt_start
8843 abbreviation above. */
8845 static void
8846 optimize_abbrev_table (void)
8848 if (abbrev_opt_start
8849 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8850 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8852 auto_vec<bool, 32> implicit_consts;
8853 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8855 unsigned int abbrev_id = abbrev_opt_start - 1;
8856 unsigned int first_id = ~0U;
8857 unsigned int last_abbrev_id = 0;
8858 unsigned int i;
8859 dw_die_ref die;
8860 if (abbrev_opt_base_type_end > abbrev_opt_start)
8861 abbrev_id = abbrev_opt_base_type_end - 1;
8862 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8863 most commonly used abbreviations come first. */
8864 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8866 dw_attr_node *a;
8867 unsigned ix;
8869 /* If calc_base_type_die_sizes has been called, the CU and
8870 base types after it can't be optimized, because we've already
8871 calculated their DIE offsets. We've sorted them first. */
8872 if (die->die_abbrev < abbrev_opt_base_type_end)
8873 continue;
8874 if (die->die_abbrev != last_abbrev_id)
8876 last_abbrev_id = die->die_abbrev;
8877 if (dwarf_version >= 5 && first_id != ~0U)
8878 optimize_implicit_const (first_id, i, implicit_consts);
8879 abbrev_id++;
8880 (*abbrev_die_table)[abbrev_id] = die;
8881 if (dwarf_version >= 5)
8883 first_id = i;
8884 implicit_consts.truncate (0);
8886 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8887 switch (AT_class (a))
8889 case dw_val_class_const:
8890 case dw_val_class_unsigned_const:
8891 case dw_val_class_file:
8892 implicit_consts.safe_push (true);
8893 break;
8894 default:
8895 implicit_consts.safe_push (false);
8896 break;
8900 else if (dwarf_version >= 5)
8902 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8903 if (!implicit_consts[ix])
8904 continue;
8905 else
8907 dw_attr_node *other_a
8908 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8909 if (!dw_val_equal_p (&a->dw_attr_val,
8910 &other_a->dw_attr_val))
8911 implicit_consts[ix] = false;
8914 die->die_abbrev = abbrev_id;
8916 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8917 if (dwarf_version >= 5 && first_id != ~0U)
8918 optimize_implicit_const (first_id, i, implicit_consts);
8921 abbrev_opt_start = 0;
8922 abbrev_opt_base_type_end = 0;
8923 abbrev_usage_count.release ();
8924 sorted_abbrev_dies.release ();
8927 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8929 static int
8930 constant_size (unsigned HOST_WIDE_INT value)
8932 int log;
8934 if (value == 0)
8935 log = 0;
8936 else
8937 log = floor_log2 (value);
8939 log = log / 8;
8940 log = 1 << (floor_log2 (log) + 1);
8942 return log;
8945 /* Return the size of a DIE as it is represented in the
8946 .debug_info section. */
8948 static unsigned long
8949 size_of_die (dw_die_ref die)
8951 unsigned long size = 0;
8952 dw_attr_node *a;
8953 unsigned ix;
8954 enum dwarf_form form;
8956 size += size_of_uleb128 (die->die_abbrev);
8957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8959 switch (AT_class (a))
8961 case dw_val_class_addr:
8962 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8964 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8965 size += size_of_uleb128 (AT_index (a));
8967 else
8968 size += DWARF2_ADDR_SIZE;
8969 break;
8970 case dw_val_class_offset:
8971 size += DWARF_OFFSET_SIZE;
8972 break;
8973 case dw_val_class_loc:
8975 unsigned long lsize = size_of_locs (AT_loc (a));
8977 /* Block length. */
8978 if (dwarf_version >= 4)
8979 size += size_of_uleb128 (lsize);
8980 else
8981 size += constant_size (lsize);
8982 size += lsize;
8984 break;
8985 case dw_val_class_loc_list:
8986 if (dwarf_split_debug_info && dwarf_version >= 5)
8988 gcc_assert (AT_loc_list (a)->num_assigned);
8989 size += size_of_uleb128 (AT_loc_list (a)->hash);
8991 else
8992 size += DWARF_OFFSET_SIZE;
8993 break;
8994 case dw_val_class_range_list:
8995 if (value_format (a) == DW_FORM_rnglistx)
8997 gcc_assert (rnglist_idx);
8998 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8999 size += size_of_uleb128 (r->idx);
9001 else
9002 size += DWARF_OFFSET_SIZE;
9003 break;
9004 case dw_val_class_const:
9005 size += size_of_sleb128 (AT_int (a));
9006 break;
9007 case dw_val_class_unsigned_const:
9009 int csize = constant_size (AT_unsigned (a));
9010 if (dwarf_version == 3
9011 && a->dw_attr == DW_AT_data_member_location
9012 && csize >= 4)
9013 size += size_of_uleb128 (AT_unsigned (a));
9014 else
9015 size += csize;
9017 break;
9018 case dw_val_class_const_implicit:
9019 case dw_val_class_unsigned_const_implicit:
9020 case dw_val_class_file_implicit:
9021 /* These occupy no size in the DIE, just an extra sleb128 in
9022 .debug_abbrev. */
9023 break;
9024 case dw_val_class_const_double:
9025 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9026 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9027 size++; /* block */
9028 break;
9029 case dw_val_class_wide_int:
9030 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9031 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9032 if (get_full_len (*a->dw_attr_val.v.val_wide)
9033 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9034 size++; /* block */
9035 break;
9036 case dw_val_class_vec:
9037 size += constant_size (a->dw_attr_val.v.val_vec.length
9038 * a->dw_attr_val.v.val_vec.elt_size)
9039 + a->dw_attr_val.v.val_vec.length
9040 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9041 break;
9042 case dw_val_class_flag:
9043 if (dwarf_version >= 4)
9044 /* Currently all add_AT_flag calls pass in 1 as last argument,
9045 so DW_FORM_flag_present can be used. If that ever changes,
9046 we'll need to use DW_FORM_flag and have some optimization
9047 in build_abbrev_table that will change those to
9048 DW_FORM_flag_present if it is set to 1 in all DIEs using
9049 the same abbrev entry. */
9050 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9051 else
9052 size += 1;
9053 break;
9054 case dw_val_class_die_ref:
9055 if (AT_ref_external (a))
9057 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9058 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9059 is sized by target address length, whereas in DWARF3
9060 it's always sized as an offset. */
9061 if (use_debug_types)
9062 size += DWARF_TYPE_SIGNATURE_SIZE;
9063 else if (dwarf_version == 2)
9064 size += DWARF2_ADDR_SIZE;
9065 else
9066 size += DWARF_OFFSET_SIZE;
9068 else
9069 size += DWARF_OFFSET_SIZE;
9070 break;
9071 case dw_val_class_fde_ref:
9072 size += DWARF_OFFSET_SIZE;
9073 break;
9074 case dw_val_class_lbl_id:
9075 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9077 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9078 size += size_of_uleb128 (AT_index (a));
9080 else
9081 size += DWARF2_ADDR_SIZE;
9082 break;
9083 case dw_val_class_lineptr:
9084 case dw_val_class_macptr:
9085 case dw_val_class_loclistsptr:
9086 size += DWARF_OFFSET_SIZE;
9087 break;
9088 case dw_val_class_str:
9089 form = AT_string_form (a);
9090 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9091 size += DWARF_OFFSET_SIZE;
9092 else if (form == DW_FORM_GNU_str_index)
9093 size += size_of_uleb128 (AT_index (a));
9094 else
9095 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9096 break;
9097 case dw_val_class_file:
9098 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9099 break;
9100 case dw_val_class_data8:
9101 size += 8;
9102 break;
9103 case dw_val_class_vms_delta:
9104 size += DWARF_OFFSET_SIZE;
9105 break;
9106 case dw_val_class_high_pc:
9107 size += DWARF2_ADDR_SIZE;
9108 break;
9109 case dw_val_class_discr_value:
9110 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9111 break;
9112 case dw_val_class_discr_list:
9114 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9116 /* This is a block, so we have the block length and then its
9117 data. */
9118 size += constant_size (block_size) + block_size;
9120 break;
9121 default:
9122 gcc_unreachable ();
9126 return size;
9129 /* Size the debugging information associated with a given DIE. Visits the
9130 DIE's children recursively. Updates the global variable next_die_offset, on
9131 each time through. Uses the current value of next_die_offset to update the
9132 die_offset field in each DIE. */
9134 static void
9135 calc_die_sizes (dw_die_ref die)
9137 dw_die_ref c;
9139 gcc_assert (die->die_offset == 0
9140 || (unsigned long int) die->die_offset == next_die_offset);
9141 die->die_offset = next_die_offset;
9142 next_die_offset += size_of_die (die);
9144 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9146 if (die->die_child != NULL)
9147 /* Count the null byte used to terminate sibling lists. */
9148 next_die_offset += 1;
9151 /* Size just the base type children at the start of the CU.
9152 This is needed because build_abbrev needs to size locs
9153 and sizing of type based stack ops needs to know die_offset
9154 values for the base types. */
9156 static void
9157 calc_base_type_die_sizes (void)
9159 unsigned long die_offset = (dwarf_split_debug_info
9160 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9161 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9162 unsigned int i;
9163 dw_die_ref base_type;
9164 #if ENABLE_ASSERT_CHECKING
9165 dw_die_ref prev = comp_unit_die ()->die_child;
9166 #endif
9168 die_offset += size_of_die (comp_unit_die ());
9169 for (i = 0; base_types.iterate (i, &base_type); i++)
9171 #if ENABLE_ASSERT_CHECKING
9172 gcc_assert (base_type->die_offset == 0
9173 && prev->die_sib == base_type
9174 && base_type->die_child == NULL
9175 && base_type->die_abbrev);
9176 prev = base_type;
9177 #endif
9178 if (abbrev_opt_start
9179 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9180 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9181 base_type->die_offset = die_offset;
9182 die_offset += size_of_die (base_type);
9186 /* Set the marks for a die and its children. We do this so
9187 that we know whether or not a reference needs to use FORM_ref_addr; only
9188 DIEs in the same CU will be marked. We used to clear out the offset
9189 and use that as the flag, but ran into ordering problems. */
9191 static void
9192 mark_dies (dw_die_ref die)
9194 dw_die_ref c;
9196 gcc_assert (!die->die_mark);
9198 die->die_mark = 1;
9199 FOR_EACH_CHILD (die, c, mark_dies (c));
9202 /* Clear the marks for a die and its children. */
9204 static void
9205 unmark_dies (dw_die_ref die)
9207 dw_die_ref c;
9209 if (! use_debug_types)
9210 gcc_assert (die->die_mark);
9212 die->die_mark = 0;
9213 FOR_EACH_CHILD (die, c, unmark_dies (c));
9216 /* Clear the marks for a die, its children and referred dies. */
9218 static void
9219 unmark_all_dies (dw_die_ref die)
9221 dw_die_ref c;
9222 dw_attr_node *a;
9223 unsigned ix;
9225 if (!die->die_mark)
9226 return;
9227 die->die_mark = 0;
9229 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9231 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9232 if (AT_class (a) == dw_val_class_die_ref)
9233 unmark_all_dies (AT_ref (a));
9236 /* Calculate if the entry should appear in the final output file. It may be
9237 from a pruned a type. */
9239 static bool
9240 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9242 /* By limiting gnu pubnames to definitions only, gold can generate a
9243 gdb index without entries for declarations, which don't include
9244 enough information to be useful. */
9245 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9246 return false;
9248 if (table == pubname_table)
9250 /* Enumerator names are part of the pubname table, but the
9251 parent DW_TAG_enumeration_type die may have been pruned.
9252 Don't output them if that is the case. */
9253 if (p->die->die_tag == DW_TAG_enumerator &&
9254 (p->die->die_parent == NULL
9255 || !p->die->die_parent->die_perennial_p))
9256 return false;
9258 /* Everything else in the pubname table is included. */
9259 return true;
9262 /* The pubtypes table shouldn't include types that have been
9263 pruned. */
9264 return (p->die->die_offset != 0
9265 || !flag_eliminate_unused_debug_types);
9268 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9269 generated for the compilation unit. */
9271 static unsigned long
9272 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9274 unsigned long size;
9275 unsigned i;
9276 pubname_entry *p;
9277 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9279 size = DWARF_PUBNAMES_HEADER_SIZE;
9280 FOR_EACH_VEC_ELT (*names, i, p)
9281 if (include_pubname_in_output (names, p))
9282 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9284 size += DWARF_OFFSET_SIZE;
9285 return size;
9288 /* Return the size of the information in the .debug_aranges section. */
9290 static unsigned long
9291 size_of_aranges (void)
9293 unsigned long size;
9295 size = DWARF_ARANGES_HEADER_SIZE;
9297 /* Count the address/length pair for this compilation unit. */
9298 if (text_section_used)
9299 size += 2 * DWARF2_ADDR_SIZE;
9300 if (cold_text_section_used)
9301 size += 2 * DWARF2_ADDR_SIZE;
9302 if (have_multiple_function_sections)
9304 unsigned fde_idx;
9305 dw_fde_ref fde;
9307 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9309 if (DECL_IGNORED_P (fde->decl))
9310 continue;
9311 if (!fde->in_std_section)
9312 size += 2 * DWARF2_ADDR_SIZE;
9313 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9314 size += 2 * DWARF2_ADDR_SIZE;
9318 /* Count the two zero words used to terminated the address range table. */
9319 size += 2 * DWARF2_ADDR_SIZE;
9320 return size;
9323 /* Select the encoding of an attribute value. */
9325 static enum dwarf_form
9326 value_format (dw_attr_node *a)
9328 switch (AT_class (a))
9330 case dw_val_class_addr:
9331 /* Only very few attributes allow DW_FORM_addr. */
9332 switch (a->dw_attr)
9334 case DW_AT_low_pc:
9335 case DW_AT_high_pc:
9336 case DW_AT_entry_pc:
9337 case DW_AT_trampoline:
9338 return (AT_index (a) == NOT_INDEXED
9339 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9340 default:
9341 break;
9343 switch (DWARF2_ADDR_SIZE)
9345 case 1:
9346 return DW_FORM_data1;
9347 case 2:
9348 return DW_FORM_data2;
9349 case 4:
9350 return DW_FORM_data4;
9351 case 8:
9352 return DW_FORM_data8;
9353 default:
9354 gcc_unreachable ();
9356 case dw_val_class_loc_list:
9357 if (dwarf_split_debug_info
9358 && dwarf_version >= 5
9359 && AT_loc_list (a)->num_assigned)
9360 return DW_FORM_loclistx;
9361 /* FALLTHRU */
9362 case dw_val_class_range_list:
9363 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9364 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9365 care about sizes of .debug* sections in shared libraries and
9366 executables and don't take into account relocations that affect just
9367 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9368 table in the .debug_rnglists section. */
9369 if (dwarf_split_debug_info
9370 && dwarf_version >= 5
9371 && AT_class (a) == dw_val_class_range_list
9372 && rnglist_idx
9373 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9374 return DW_FORM_rnglistx;
9375 if (dwarf_version >= 4)
9376 return DW_FORM_sec_offset;
9377 /* FALLTHRU */
9378 case dw_val_class_vms_delta:
9379 case dw_val_class_offset:
9380 switch (DWARF_OFFSET_SIZE)
9382 case 4:
9383 return DW_FORM_data4;
9384 case 8:
9385 return DW_FORM_data8;
9386 default:
9387 gcc_unreachable ();
9389 case dw_val_class_loc:
9390 if (dwarf_version >= 4)
9391 return DW_FORM_exprloc;
9392 switch (constant_size (size_of_locs (AT_loc (a))))
9394 case 1:
9395 return DW_FORM_block1;
9396 case 2:
9397 return DW_FORM_block2;
9398 case 4:
9399 return DW_FORM_block4;
9400 default:
9401 gcc_unreachable ();
9403 case dw_val_class_const:
9404 return DW_FORM_sdata;
9405 case dw_val_class_unsigned_const:
9406 switch (constant_size (AT_unsigned (a)))
9408 case 1:
9409 return DW_FORM_data1;
9410 case 2:
9411 return DW_FORM_data2;
9412 case 4:
9413 /* In DWARF3 DW_AT_data_member_location with
9414 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9415 constant, so we need to use DW_FORM_udata if we need
9416 a large constant. */
9417 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9418 return DW_FORM_udata;
9419 return DW_FORM_data4;
9420 case 8:
9421 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9422 return DW_FORM_udata;
9423 return DW_FORM_data8;
9424 default:
9425 gcc_unreachable ();
9427 case dw_val_class_const_implicit:
9428 case dw_val_class_unsigned_const_implicit:
9429 case dw_val_class_file_implicit:
9430 return DW_FORM_implicit_const;
9431 case dw_val_class_const_double:
9432 switch (HOST_BITS_PER_WIDE_INT)
9434 case 8:
9435 return DW_FORM_data2;
9436 case 16:
9437 return DW_FORM_data4;
9438 case 32:
9439 return DW_FORM_data8;
9440 case 64:
9441 if (dwarf_version >= 5)
9442 return DW_FORM_data16;
9443 /* FALLTHRU */
9444 default:
9445 return DW_FORM_block1;
9447 case dw_val_class_wide_int:
9448 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9450 case 8:
9451 return DW_FORM_data1;
9452 case 16:
9453 return DW_FORM_data2;
9454 case 32:
9455 return DW_FORM_data4;
9456 case 64:
9457 return DW_FORM_data8;
9458 case 128:
9459 if (dwarf_version >= 5)
9460 return DW_FORM_data16;
9461 /* FALLTHRU */
9462 default:
9463 return DW_FORM_block1;
9465 case dw_val_class_vec:
9466 switch (constant_size (a->dw_attr_val.v.val_vec.length
9467 * a->dw_attr_val.v.val_vec.elt_size))
9469 case 1:
9470 return DW_FORM_block1;
9471 case 2:
9472 return DW_FORM_block2;
9473 case 4:
9474 return DW_FORM_block4;
9475 default:
9476 gcc_unreachable ();
9478 case dw_val_class_flag:
9479 if (dwarf_version >= 4)
9481 /* Currently all add_AT_flag calls pass in 1 as last argument,
9482 so DW_FORM_flag_present can be used. If that ever changes,
9483 we'll need to use DW_FORM_flag and have some optimization
9484 in build_abbrev_table that will change those to
9485 DW_FORM_flag_present if it is set to 1 in all DIEs using
9486 the same abbrev entry. */
9487 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9488 return DW_FORM_flag_present;
9490 return DW_FORM_flag;
9491 case dw_val_class_die_ref:
9492 if (AT_ref_external (a))
9493 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9494 else
9495 return DW_FORM_ref;
9496 case dw_val_class_fde_ref:
9497 return DW_FORM_data;
9498 case dw_val_class_lbl_id:
9499 return (AT_index (a) == NOT_INDEXED
9500 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9501 case dw_val_class_lineptr:
9502 case dw_val_class_macptr:
9503 case dw_val_class_loclistsptr:
9504 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9505 case dw_val_class_str:
9506 return AT_string_form (a);
9507 case dw_val_class_file:
9508 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9510 case 1:
9511 return DW_FORM_data1;
9512 case 2:
9513 return DW_FORM_data2;
9514 case 4:
9515 return DW_FORM_data4;
9516 default:
9517 gcc_unreachable ();
9520 case dw_val_class_data8:
9521 return DW_FORM_data8;
9523 case dw_val_class_high_pc:
9524 switch (DWARF2_ADDR_SIZE)
9526 case 1:
9527 return DW_FORM_data1;
9528 case 2:
9529 return DW_FORM_data2;
9530 case 4:
9531 return DW_FORM_data4;
9532 case 8:
9533 return DW_FORM_data8;
9534 default:
9535 gcc_unreachable ();
9538 case dw_val_class_discr_value:
9539 return (a->dw_attr_val.v.val_discr_value.pos
9540 ? DW_FORM_udata
9541 : DW_FORM_sdata);
9542 case dw_val_class_discr_list:
9543 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9545 case 1:
9546 return DW_FORM_block1;
9547 case 2:
9548 return DW_FORM_block2;
9549 case 4:
9550 return DW_FORM_block4;
9551 default:
9552 gcc_unreachable ();
9555 default:
9556 gcc_unreachable ();
9560 /* Output the encoding of an attribute value. */
9562 static void
9563 output_value_format (dw_attr_node *a)
9565 enum dwarf_form form = value_format (a);
9567 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9570 /* Given a die and id, produce the appropriate abbreviations. */
9572 static void
9573 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9575 unsigned ix;
9576 dw_attr_node *a_attr;
9578 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9579 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9580 dwarf_tag_name (abbrev->die_tag));
9582 if (abbrev->die_child != NULL)
9583 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9584 else
9585 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9587 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9589 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9590 dwarf_attr_name (a_attr->dw_attr));
9591 output_value_format (a_attr);
9592 if (value_format (a_attr) == DW_FORM_implicit_const)
9594 if (AT_class (a_attr) == dw_val_class_file_implicit)
9596 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9597 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9598 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9600 else
9601 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9605 dw2_asm_output_data (1, 0, NULL);
9606 dw2_asm_output_data (1, 0, NULL);
9610 /* Output the .debug_abbrev section which defines the DIE abbreviation
9611 table. */
9613 static void
9614 output_abbrev_section (void)
9616 unsigned int abbrev_id;
9617 dw_die_ref abbrev;
9619 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9620 if (abbrev_id != 0)
9621 output_die_abbrevs (abbrev_id, abbrev);
9623 /* Terminate the table. */
9624 dw2_asm_output_data (1, 0, NULL);
9627 /* Return a new location list, given the begin and end range, and the
9628 expression. */
9630 static inline dw_loc_list_ref
9631 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9632 const char *section)
9634 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9636 retlist->begin = begin;
9637 retlist->begin_entry = NULL;
9638 retlist->end = end;
9639 retlist->expr = expr;
9640 retlist->section = section;
9642 return retlist;
9645 /* Generate a new internal symbol for this location list node, if it
9646 hasn't got one yet. */
9648 static inline void
9649 gen_llsym (dw_loc_list_ref list)
9651 gcc_assert (!list->ll_symbol);
9652 list->ll_symbol = gen_internal_sym ("LLST");
9655 /* Output the location list given to us. */
9657 static void
9658 output_loc_list (dw_loc_list_ref list_head)
9660 if (list_head->emitted)
9661 return;
9662 list_head->emitted = true;
9664 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9666 dw_loc_list_ref curr = list_head;
9667 const char *last_section = NULL;
9668 const char *base_label = NULL;
9670 /* Walk the location list, and output each range + expression. */
9671 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9673 unsigned long size;
9674 /* Don't output an entry that starts and ends at the same address. */
9675 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9676 continue;
9677 size = size_of_locs (curr->expr);
9678 /* If the expression is too large, drop it on the floor. We could
9679 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9680 in the expression, but >= 64KB expressions for a single value
9681 in a single range are unlikely very useful. */
9682 if (dwarf_version < 5 && size > 0xffff)
9683 continue;
9684 if (dwarf_version >= 5)
9686 if (dwarf_split_debug_info)
9688 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9689 uleb128 index into .debug_addr and uleb128 length. */
9690 dw2_asm_output_data (1, DW_LLE_startx_length,
9691 "DW_LLE_startx_length (%s)",
9692 list_head->ll_symbol);
9693 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9694 "Location list range start index "
9695 "(%s)", curr->begin);
9696 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9697 For that case we probably need to emit DW_LLE_startx_endx,
9698 but we'd need 2 .debug_addr entries rather than just one. */
9699 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9700 "Location list length (%s)",
9701 list_head->ll_symbol);
9703 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9705 /* If all code is in .text section, the base address is
9706 already provided by the CU attributes. Use
9707 DW_LLE_offset_pair where both addresses are uleb128 encoded
9708 offsets against that base. */
9709 dw2_asm_output_data (1, DW_LLE_offset_pair,
9710 "DW_LLE_offset_pair (%s)",
9711 list_head->ll_symbol);
9712 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9713 "Location list begin address (%s)",
9714 list_head->ll_symbol);
9715 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9716 "Location list end address (%s)",
9717 list_head->ll_symbol);
9719 else if (HAVE_AS_LEB128)
9721 /* Otherwise, find out how many consecutive entries could share
9722 the same base entry. If just one, emit DW_LLE_start_length,
9723 otherwise emit DW_LLE_base_address for the base address
9724 followed by a series of DW_LLE_offset_pair. */
9725 if (last_section == NULL || curr->section != last_section)
9727 dw_loc_list_ref curr2;
9728 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9729 curr2 = curr2->dw_loc_next)
9731 if (strcmp (curr2->begin, curr2->end) == 0
9732 && !curr2->force)
9733 continue;
9734 break;
9736 if (curr2 == NULL || curr->section != curr2->section)
9737 last_section = NULL;
9738 else
9740 last_section = curr->section;
9741 base_label = curr->begin;
9742 dw2_asm_output_data (1, DW_LLE_base_address,
9743 "DW_LLE_base_address (%s)",
9744 list_head->ll_symbol);
9745 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9746 "Base address (%s)",
9747 list_head->ll_symbol);
9750 /* Only one entry with the same base address. Use
9751 DW_LLE_start_length with absolute address and uleb128
9752 length. */
9753 if (last_section == NULL)
9755 dw2_asm_output_data (1, DW_LLE_start_length,
9756 "DW_LLE_start_length (%s)",
9757 list_head->ll_symbol);
9758 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9759 "Location list begin address (%s)",
9760 list_head->ll_symbol);
9761 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9762 "Location list length "
9763 "(%s)", list_head->ll_symbol);
9765 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9766 DW_LLE_base_address. */
9767 else
9769 dw2_asm_output_data (1, DW_LLE_offset_pair,
9770 "DW_LLE_offset_pair (%s)",
9771 list_head->ll_symbol);
9772 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9773 "Location list begin address "
9774 "(%s)", list_head->ll_symbol);
9775 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9776 "Location list end address "
9777 "(%s)", list_head->ll_symbol);
9780 /* The assembler does not support .uleb128 directive. Emit
9781 DW_LLE_start_end with a pair of absolute addresses. */
9782 else
9784 dw2_asm_output_data (1, DW_LLE_start_end,
9785 "DW_LLE_start_end (%s)",
9786 list_head->ll_symbol);
9787 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9788 "Location list begin address (%s)",
9789 list_head->ll_symbol);
9790 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9791 "Location list end address (%s)",
9792 list_head->ll_symbol);
9795 else if (dwarf_split_debug_info)
9797 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9798 and 4 byte length. */
9799 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9800 "Location list start/length entry (%s)",
9801 list_head->ll_symbol);
9802 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9803 "Location list range start index (%s)",
9804 curr->begin);
9805 /* The length field is 4 bytes. If we ever need to support
9806 an 8-byte length, we can add a new DW_LLE code or fall back
9807 to DW_LLE_GNU_start_end_entry. */
9808 dw2_asm_output_delta (4, curr->end, curr->begin,
9809 "Location list range length (%s)",
9810 list_head->ll_symbol);
9812 else if (!have_multiple_function_sections)
9814 /* Pair of relative addresses against start of text section. */
9815 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9816 "Location list begin address (%s)",
9817 list_head->ll_symbol);
9818 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9819 "Location list end address (%s)",
9820 list_head->ll_symbol);
9822 else
9824 /* Pair of absolute addresses. */
9825 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9826 "Location list begin address (%s)",
9827 list_head->ll_symbol);
9828 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9829 "Location list end address (%s)",
9830 list_head->ll_symbol);
9833 /* Output the block length for this list of location operations. */
9834 if (dwarf_version >= 5)
9835 dw2_asm_output_data_uleb128 (size, "Location expression size");
9836 else
9838 gcc_assert (size <= 0xffff);
9839 dw2_asm_output_data (2, size, "Location expression size");
9842 output_loc_sequence (curr->expr, -1);
9845 /* And finally list termination. */
9846 if (dwarf_version >= 5)
9847 dw2_asm_output_data (1, DW_LLE_end_of_list,
9848 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9849 else if (dwarf_split_debug_info)
9850 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9851 "Location list terminator (%s)",
9852 list_head->ll_symbol);
9853 else
9855 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9856 "Location list terminator begin (%s)",
9857 list_head->ll_symbol);
9858 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9859 "Location list terminator end (%s)",
9860 list_head->ll_symbol);
9864 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9865 section. Emit a relocated reference if val_entry is NULL, otherwise,
9866 emit an indirect reference. */
9868 static void
9869 output_range_list_offset (dw_attr_node *a)
9871 const char *name = dwarf_attr_name (a->dw_attr);
9873 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9875 if (dwarf_version >= 5)
9877 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9878 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9879 debug_ranges_section, "%s", name);
9881 else
9883 char *p = strchr (ranges_section_label, '\0');
9884 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9885 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9886 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9887 debug_ranges_section, "%s", name);
9888 *p = '\0';
9891 else if (dwarf_version >= 5)
9893 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9894 gcc_assert (rnglist_idx);
9895 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9897 else
9898 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9899 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9900 "%s (offset from %s)", name, ranges_section_label);
9903 /* Output the offset into the debug_loc section. */
9905 static void
9906 output_loc_list_offset (dw_attr_node *a)
9908 char *sym = AT_loc_list (a)->ll_symbol;
9910 gcc_assert (sym);
9911 if (!dwarf_split_debug_info)
9912 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9913 "%s", dwarf_attr_name (a->dw_attr));
9914 else if (dwarf_version >= 5)
9916 gcc_assert (AT_loc_list (a)->num_assigned);
9917 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9918 dwarf_attr_name (a->dw_attr),
9919 sym);
9921 else
9922 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9923 "%s", dwarf_attr_name (a->dw_attr));
9926 /* Output an attribute's index or value appropriately. */
9928 static void
9929 output_attr_index_or_value (dw_attr_node *a)
9931 const char *name = dwarf_attr_name (a->dw_attr);
9933 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9935 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9936 return;
9938 switch (AT_class (a))
9940 case dw_val_class_addr:
9941 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9942 break;
9943 case dw_val_class_high_pc:
9944 case dw_val_class_lbl_id:
9945 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9946 break;
9947 default:
9948 gcc_unreachable ();
9952 /* Output a type signature. */
9954 static inline void
9955 output_signature (const char *sig, const char *name)
9957 int i;
9959 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9960 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9963 /* Output a discriminant value. */
9965 static inline void
9966 output_discr_value (dw_discr_value *discr_value, const char *name)
9968 if (discr_value->pos)
9969 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9970 else
9971 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9974 /* Output the DIE and its attributes. Called recursively to generate
9975 the definitions of each child DIE. */
9977 static void
9978 output_die (dw_die_ref die)
9980 dw_attr_node *a;
9981 dw_die_ref c;
9982 unsigned long size;
9983 unsigned ix;
9985 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9986 (unsigned long)die->die_offset,
9987 dwarf_tag_name (die->die_tag));
9989 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9991 const char *name = dwarf_attr_name (a->dw_attr);
9993 switch (AT_class (a))
9995 case dw_val_class_addr:
9996 output_attr_index_or_value (a);
9997 break;
9999 case dw_val_class_offset:
10000 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10001 "%s", name);
10002 break;
10004 case dw_val_class_range_list:
10005 output_range_list_offset (a);
10006 break;
10008 case dw_val_class_loc:
10009 size = size_of_locs (AT_loc (a));
10011 /* Output the block length for this list of location operations. */
10012 if (dwarf_version >= 4)
10013 dw2_asm_output_data_uleb128 (size, "%s", name);
10014 else
10015 dw2_asm_output_data (constant_size (size), size, "%s", name);
10017 output_loc_sequence (AT_loc (a), -1);
10018 break;
10020 case dw_val_class_const:
10021 /* ??? It would be slightly more efficient to use a scheme like is
10022 used for unsigned constants below, but gdb 4.x does not sign
10023 extend. Gdb 5.x does sign extend. */
10024 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10025 break;
10027 case dw_val_class_unsigned_const:
10029 int csize = constant_size (AT_unsigned (a));
10030 if (dwarf_version == 3
10031 && a->dw_attr == DW_AT_data_member_location
10032 && csize >= 4)
10033 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10034 else
10035 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10037 break;
10039 case dw_val_class_const_implicit:
10040 if (flag_debug_asm)
10041 fprintf (asm_out_file, "\t\t\t%s %s ("
10042 HOST_WIDE_INT_PRINT_DEC ")\n",
10043 ASM_COMMENT_START, name, AT_int (a));
10044 break;
10046 case dw_val_class_unsigned_const_implicit:
10047 if (flag_debug_asm)
10048 fprintf (asm_out_file, "\t\t\t%s %s ("
10049 HOST_WIDE_INT_PRINT_HEX ")\n",
10050 ASM_COMMENT_START, name, AT_unsigned (a));
10051 break;
10053 case dw_val_class_const_double:
10055 unsigned HOST_WIDE_INT first, second;
10057 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10058 dw2_asm_output_data (1,
10059 HOST_BITS_PER_DOUBLE_INT
10060 / HOST_BITS_PER_CHAR,
10061 NULL);
10063 if (WORDS_BIG_ENDIAN)
10065 first = a->dw_attr_val.v.val_double.high;
10066 second = a->dw_attr_val.v.val_double.low;
10068 else
10070 first = a->dw_attr_val.v.val_double.low;
10071 second = a->dw_attr_val.v.val_double.high;
10074 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10075 first, "%s", name);
10076 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10077 second, NULL);
10079 break;
10081 case dw_val_class_wide_int:
10083 int i;
10084 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10085 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10086 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10087 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10088 * l, NULL);
10090 if (WORDS_BIG_ENDIAN)
10091 for (i = len - 1; i >= 0; --i)
10093 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10094 "%s", name);
10095 name = "";
10097 else
10098 for (i = 0; i < len; ++i)
10100 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10101 "%s", name);
10102 name = "";
10105 break;
10107 case dw_val_class_vec:
10109 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10110 unsigned int len = a->dw_attr_val.v.val_vec.length;
10111 unsigned int i;
10112 unsigned char *p;
10114 dw2_asm_output_data (constant_size (len * elt_size),
10115 len * elt_size, "%s", name);
10116 if (elt_size > sizeof (HOST_WIDE_INT))
10118 elt_size /= 2;
10119 len *= 2;
10121 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10122 i < len;
10123 i++, p += elt_size)
10124 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10125 "fp or vector constant word %u", i);
10126 break;
10129 case dw_val_class_flag:
10130 if (dwarf_version >= 4)
10132 /* Currently all add_AT_flag calls pass in 1 as last argument,
10133 so DW_FORM_flag_present can be used. If that ever changes,
10134 we'll need to use DW_FORM_flag and have some optimization
10135 in build_abbrev_table that will change those to
10136 DW_FORM_flag_present if it is set to 1 in all DIEs using
10137 the same abbrev entry. */
10138 gcc_assert (AT_flag (a) == 1);
10139 if (flag_debug_asm)
10140 fprintf (asm_out_file, "\t\t\t%s %s\n",
10141 ASM_COMMENT_START, name);
10142 break;
10144 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10145 break;
10147 case dw_val_class_loc_list:
10148 output_loc_list_offset (a);
10149 break;
10151 case dw_val_class_die_ref:
10152 if (AT_ref_external (a))
10154 if (AT_ref (a)->comdat_type_p)
10156 comdat_type_node *type_node
10157 = AT_ref (a)->die_id.die_type_node;
10159 gcc_assert (type_node);
10160 output_signature (type_node->signature, name);
10162 else
10164 const char *sym = AT_ref (a)->die_id.die_symbol;
10165 int size;
10167 gcc_assert (sym);
10168 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10169 length, whereas in DWARF3 it's always sized as an
10170 offset. */
10171 if (dwarf_version == 2)
10172 size = DWARF2_ADDR_SIZE;
10173 else
10174 size = DWARF_OFFSET_SIZE;
10175 /* ??? We cannot unconditionally output die_offset if
10176 non-zero - others might create references to those
10177 DIEs via symbols.
10178 And we do not clear its DIE offset after outputting it
10179 (and the label refers to the actual DIEs, not the
10180 DWARF CU unit header which is when using label + offset
10181 would be the correct thing to do).
10182 ??? This is the reason for the with_offset flag. */
10183 if (AT_ref (a)->with_offset)
10184 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10185 debug_info_section, "%s", name);
10186 else
10187 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10188 name);
10191 else
10193 gcc_assert (AT_ref (a)->die_offset);
10194 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10195 "%s", name);
10197 break;
10199 case dw_val_class_fde_ref:
10201 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10203 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10204 a->dw_attr_val.v.val_fde_index * 2);
10205 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10206 "%s", name);
10208 break;
10210 case dw_val_class_vms_delta:
10211 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10212 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10213 AT_vms_delta2 (a), AT_vms_delta1 (a),
10214 "%s", name);
10215 #else
10216 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10217 AT_vms_delta2 (a), AT_vms_delta1 (a),
10218 "%s", name);
10219 #endif
10220 break;
10222 case dw_val_class_lbl_id:
10223 output_attr_index_or_value (a);
10224 break;
10226 case dw_val_class_lineptr:
10227 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10228 debug_line_section, "%s", name);
10229 break;
10231 case dw_val_class_macptr:
10232 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10233 debug_macinfo_section, "%s", name);
10234 break;
10236 case dw_val_class_loclistsptr:
10237 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10238 debug_loc_section, "%s", name);
10239 break;
10241 case dw_val_class_str:
10242 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10243 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10244 a->dw_attr_val.v.val_str->label,
10245 debug_str_section,
10246 "%s: \"%s\"", name, AT_string (a));
10247 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10248 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10249 a->dw_attr_val.v.val_str->label,
10250 debug_line_str_section,
10251 "%s: \"%s\"", name, AT_string (a));
10252 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10253 dw2_asm_output_data_uleb128 (AT_index (a),
10254 "%s: \"%s\"", name, AT_string (a));
10255 else
10256 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10257 break;
10259 case dw_val_class_file:
10261 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10263 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10264 a->dw_attr_val.v.val_file->filename);
10265 break;
10268 case dw_val_class_file_implicit:
10269 if (flag_debug_asm)
10270 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10271 ASM_COMMENT_START, name,
10272 maybe_emit_file (a->dw_attr_val.v.val_file),
10273 a->dw_attr_val.v.val_file->filename);
10274 break;
10276 case dw_val_class_data8:
10278 int i;
10280 for (i = 0; i < 8; i++)
10281 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10282 i == 0 ? "%s" : NULL, name);
10283 break;
10286 case dw_val_class_high_pc:
10287 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10288 get_AT_low_pc (die), "DW_AT_high_pc");
10289 break;
10291 case dw_val_class_discr_value:
10292 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10293 break;
10295 case dw_val_class_discr_list:
10297 dw_discr_list_ref list = AT_discr_list (a);
10298 const int size = size_of_discr_list (list);
10300 /* This is a block, so output its length first. */
10301 dw2_asm_output_data (constant_size (size), size,
10302 "%s: block size", name);
10304 for (; list != NULL; list = list->dw_discr_next)
10306 /* One byte for the discriminant value descriptor, and then as
10307 many LEB128 numbers as required. */
10308 if (list->dw_discr_range)
10309 dw2_asm_output_data (1, DW_DSC_range,
10310 "%s: DW_DSC_range", name);
10311 else
10312 dw2_asm_output_data (1, DW_DSC_label,
10313 "%s: DW_DSC_label", name);
10315 output_discr_value (&list->dw_discr_lower_bound, name);
10316 if (list->dw_discr_range)
10317 output_discr_value (&list->dw_discr_upper_bound, name);
10319 break;
10322 default:
10323 gcc_unreachable ();
10327 FOR_EACH_CHILD (die, c, output_die (c));
10329 /* Add null byte to terminate sibling list. */
10330 if (die->die_child != NULL)
10331 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10332 (unsigned long) die->die_offset);
10335 /* Output the compilation unit that appears at the beginning of the
10336 .debug_info section, and precedes the DIE descriptions. */
10338 static void
10339 output_compilation_unit_header (enum dwarf_unit_type ut)
10341 if (!XCOFF_DEBUGGING_INFO)
10343 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10344 dw2_asm_output_data (4, 0xffffffff,
10345 "Initial length escape value indicating 64-bit DWARF extension");
10346 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10347 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10348 "Length of Compilation Unit Info");
10351 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10352 if (dwarf_version >= 5)
10354 const char *name;
10355 switch (ut)
10357 case DW_UT_compile: name = "DW_UT_compile"; break;
10358 case DW_UT_type: name = "DW_UT_type"; break;
10359 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10360 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10361 default: gcc_unreachable ();
10363 dw2_asm_output_data (1, ut, "%s", name);
10364 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10366 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10367 debug_abbrev_section,
10368 "Offset Into Abbrev. Section");
10369 if (dwarf_version < 5)
10370 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10373 /* Output the compilation unit DIE and its children. */
10375 static void
10376 output_comp_unit (dw_die_ref die, int output_if_empty,
10377 const unsigned char *dwo_id)
10379 const char *secname, *oldsym;
10380 char *tmp;
10382 /* Unless we are outputting main CU, we may throw away empty ones. */
10383 if (!output_if_empty && die->die_child == NULL)
10384 return;
10386 /* Even if there are no children of this DIE, we must output the information
10387 about the compilation unit. Otherwise, on an empty translation unit, we
10388 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10389 will then complain when examining the file. First mark all the DIEs in
10390 this CU so we know which get local refs. */
10391 mark_dies (die);
10393 external_ref_hash_type *extern_map = optimize_external_refs (die);
10395 /* For now, optimize only the main CU, in order to optimize the rest
10396 we'd need to see all of them earlier. Leave the rest for post-linking
10397 tools like DWZ. */
10398 if (die == comp_unit_die ())
10399 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10401 build_abbrev_table (die, extern_map);
10403 optimize_abbrev_table ();
10405 delete extern_map;
10407 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10408 next_die_offset = (dwo_id
10409 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10410 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10411 calc_die_sizes (die);
10413 oldsym = die->die_id.die_symbol;
10414 if (oldsym && die->comdat_type_p)
10416 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10418 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10419 secname = tmp;
10420 die->die_id.die_symbol = NULL;
10421 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10423 else
10425 switch_to_section (debug_info_section);
10426 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10427 info_section_emitted = true;
10430 /* For LTO cross unit DIE refs we want a symbol on the start of the
10431 debuginfo section, not on the CU DIE. */
10432 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10434 /* ??? No way to get visibility assembled without a decl. */
10435 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10436 get_identifier (oldsym), char_type_node);
10437 TREE_PUBLIC (decl) = true;
10438 TREE_STATIC (decl) = true;
10439 DECL_ARTIFICIAL (decl) = true;
10440 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10441 DECL_VISIBILITY_SPECIFIED (decl) = true;
10442 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10443 #ifdef ASM_WEAKEN_LABEL
10444 /* We prefer a .weak because that handles duplicates from duplicate
10445 archive members in a graceful way. */
10446 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10447 #else
10448 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10449 #endif
10450 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10453 /* Output debugging information. */
10454 output_compilation_unit_header (dwo_id
10455 ? DW_UT_split_compile : DW_UT_compile);
10456 if (dwarf_version >= 5)
10458 if (dwo_id != NULL)
10459 for (int i = 0; i < 8; i++)
10460 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10462 output_die (die);
10464 /* Leave the marks on the main CU, so we can check them in
10465 output_pubnames. */
10466 if (oldsym)
10468 unmark_dies (die);
10469 die->die_id.die_symbol = oldsym;
10473 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10474 and .debug_pubtypes. This is configured per-target, but can be
10475 overridden by the -gpubnames or -gno-pubnames options. */
10477 static inline bool
10478 want_pubnames (void)
10480 if (debug_info_level <= DINFO_LEVEL_TERSE)
10481 return false;
10482 if (debug_generate_pub_sections != -1)
10483 return debug_generate_pub_sections;
10484 return targetm.want_debug_pub_sections;
10487 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10489 static void
10490 add_AT_pubnames (dw_die_ref die)
10492 if (want_pubnames ())
10493 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10496 /* Add a string attribute value to a skeleton DIE. */
10498 static inline void
10499 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10500 const char *str)
10502 dw_attr_node attr;
10503 struct indirect_string_node *node;
10505 if (! skeleton_debug_str_hash)
10506 skeleton_debug_str_hash
10507 = hash_table<indirect_string_hasher>::create_ggc (10);
10509 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10510 find_string_form (node);
10511 if (node->form == DW_FORM_GNU_str_index)
10512 node->form = DW_FORM_strp;
10514 attr.dw_attr = attr_kind;
10515 attr.dw_attr_val.val_class = dw_val_class_str;
10516 attr.dw_attr_val.val_entry = NULL;
10517 attr.dw_attr_val.v.val_str = node;
10518 add_dwarf_attr (die, &attr);
10521 /* Helper function to generate top-level dies for skeleton debug_info and
10522 debug_types. */
10524 static void
10525 add_top_level_skeleton_die_attrs (dw_die_ref die)
10527 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10528 const char *comp_dir = comp_dir_string ();
10530 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10531 if (comp_dir != NULL)
10532 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10533 add_AT_pubnames (die);
10534 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10537 /* Output skeleton debug sections that point to the dwo file. */
10539 static void
10540 output_skeleton_debug_sections (dw_die_ref comp_unit,
10541 const unsigned char *dwo_id)
10543 /* These attributes will be found in the full debug_info section. */
10544 remove_AT (comp_unit, DW_AT_producer);
10545 remove_AT (comp_unit, DW_AT_language);
10547 switch_to_section (debug_skeleton_info_section);
10548 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10550 /* Produce the skeleton compilation-unit header. This one differs enough from
10551 a normal CU header that it's better not to call output_compilation_unit
10552 header. */
10553 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10554 dw2_asm_output_data (4, 0xffffffff,
10555 "Initial length escape value indicating 64-bit "
10556 "DWARF extension");
10558 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10559 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10560 - DWARF_INITIAL_LENGTH_SIZE
10561 + size_of_die (comp_unit),
10562 "Length of Compilation Unit Info");
10563 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10564 if (dwarf_version >= 5)
10566 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10567 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10569 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10570 debug_skeleton_abbrev_section,
10571 "Offset Into Abbrev. Section");
10572 if (dwarf_version < 5)
10573 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10574 else
10575 for (int i = 0; i < 8; i++)
10576 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10578 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10579 output_die (comp_unit);
10581 /* Build the skeleton debug_abbrev section. */
10582 switch_to_section (debug_skeleton_abbrev_section);
10583 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10585 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10587 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10590 /* Output a comdat type unit DIE and its children. */
10592 static void
10593 output_comdat_type_unit (comdat_type_node *node)
10595 const char *secname;
10596 char *tmp;
10597 int i;
10598 #if defined (OBJECT_FORMAT_ELF)
10599 tree comdat_key;
10600 #endif
10602 /* First mark all the DIEs in this CU so we know which get local refs. */
10603 mark_dies (node->root_die);
10605 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10607 build_abbrev_table (node->root_die, extern_map);
10609 delete extern_map;
10610 extern_map = NULL;
10612 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10613 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10614 calc_die_sizes (node->root_die);
10616 #if defined (OBJECT_FORMAT_ELF)
10617 if (dwarf_version >= 5)
10619 if (!dwarf_split_debug_info)
10620 secname = ".debug_info";
10621 else
10622 secname = ".debug_info.dwo";
10624 else if (!dwarf_split_debug_info)
10625 secname = ".debug_types";
10626 else
10627 secname = ".debug_types.dwo";
10629 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10630 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10631 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10632 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10633 comdat_key = get_identifier (tmp);
10634 targetm.asm_out.named_section (secname,
10635 SECTION_DEBUG | SECTION_LINKONCE,
10636 comdat_key);
10637 #else
10638 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10639 sprintf (tmp, (dwarf_version >= 5
10640 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10641 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10642 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10643 secname = tmp;
10644 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10645 #endif
10647 /* Output debugging information. */
10648 output_compilation_unit_header (dwarf_split_debug_info
10649 ? DW_UT_split_type : DW_UT_type);
10650 output_signature (node->signature, "Type Signature");
10651 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10652 "Offset to Type DIE");
10653 output_die (node->root_die);
10655 unmark_dies (node->root_die);
10658 /* Return the DWARF2/3 pubname associated with a decl. */
10660 static const char *
10661 dwarf2_name (tree decl, int scope)
10663 if (DECL_NAMELESS (decl))
10664 return NULL;
10665 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10668 /* Add a new entry to .debug_pubnames if appropriate. */
10670 static void
10671 add_pubname_string (const char *str, dw_die_ref die)
10673 pubname_entry e;
10675 e.die = die;
10676 e.name = xstrdup (str);
10677 vec_safe_push (pubname_table, e);
10680 static void
10681 add_pubname (tree decl, dw_die_ref die)
10683 if (!want_pubnames ())
10684 return;
10686 /* Don't add items to the table when we expect that the consumer will have
10687 just read the enclosing die. For example, if the consumer is looking at a
10688 class_member, it will either be inside the class already, or will have just
10689 looked up the class to find the member. Either way, searching the class is
10690 faster than searching the index. */
10691 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10692 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10694 const char *name = dwarf2_name (decl, 1);
10696 if (name)
10697 add_pubname_string (name, die);
10701 /* Add an enumerator to the pubnames section. */
10703 static void
10704 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10706 pubname_entry e;
10708 gcc_assert (scope_name);
10709 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10710 e.die = die;
10711 vec_safe_push (pubname_table, e);
10714 /* Add a new entry to .debug_pubtypes if appropriate. */
10716 static void
10717 add_pubtype (tree decl, dw_die_ref die)
10719 pubname_entry e;
10721 if (!want_pubnames ())
10722 return;
10724 if ((TREE_PUBLIC (decl)
10725 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10726 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10728 tree scope = NULL;
10729 const char *scope_name = "";
10730 const char *sep = is_cxx () ? "::" : ".";
10731 const char *name;
10733 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10734 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10736 scope_name = lang_hooks.dwarf_name (scope, 1);
10737 if (scope_name != NULL && scope_name[0] != '\0')
10738 scope_name = concat (scope_name, sep, NULL);
10739 else
10740 scope_name = "";
10743 if (TYPE_P (decl))
10744 name = type_tag (decl);
10745 else
10746 name = lang_hooks.dwarf_name (decl, 1);
10748 /* If we don't have a name for the type, there's no point in adding
10749 it to the table. */
10750 if (name != NULL && name[0] != '\0')
10752 e.die = die;
10753 e.name = concat (scope_name, name, NULL);
10754 vec_safe_push (pubtype_table, e);
10757 /* Although it might be more consistent to add the pubinfo for the
10758 enumerators as their dies are created, they should only be added if the
10759 enum type meets the criteria above. So rather than re-check the parent
10760 enum type whenever an enumerator die is created, just output them all
10761 here. This isn't protected by the name conditional because anonymous
10762 enums don't have names. */
10763 if (die->die_tag == DW_TAG_enumeration_type)
10765 dw_die_ref c;
10767 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10772 /* Output a single entry in the pubnames table. */
10774 static void
10775 output_pubname (dw_offset die_offset, pubname_entry *entry)
10777 dw_die_ref die = entry->die;
10778 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10780 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10782 if (debug_generate_pub_sections == 2)
10784 /* This logic follows gdb's method for determining the value of the flag
10785 byte. */
10786 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10787 switch (die->die_tag)
10789 case DW_TAG_typedef:
10790 case DW_TAG_base_type:
10791 case DW_TAG_subrange_type:
10792 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10793 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10794 break;
10795 case DW_TAG_enumerator:
10796 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10797 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10798 if (!is_cxx ())
10799 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10800 break;
10801 case DW_TAG_subprogram:
10802 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10803 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10804 if (!is_ada ())
10805 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10806 break;
10807 case DW_TAG_constant:
10808 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10809 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10810 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10811 break;
10812 case DW_TAG_variable:
10813 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10814 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10815 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10816 break;
10817 case DW_TAG_namespace:
10818 case DW_TAG_imported_declaration:
10819 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10820 break;
10821 case DW_TAG_class_type:
10822 case DW_TAG_interface_type:
10823 case DW_TAG_structure_type:
10824 case DW_TAG_union_type:
10825 case DW_TAG_enumeration_type:
10826 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10827 if (!is_cxx ())
10828 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10829 break;
10830 default:
10831 /* An unusual tag. Leave the flag-byte empty. */
10832 break;
10834 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10835 "GDB-index flags");
10838 dw2_asm_output_nstring (entry->name, -1, "external name");
10842 /* Output the public names table used to speed up access to externally
10843 visible names; or the public types table used to find type definitions. */
10845 static void
10846 output_pubnames (vec<pubname_entry, va_gc> *names)
10848 unsigned i;
10849 unsigned long pubnames_length = size_of_pubnames (names);
10850 pubname_entry *pub;
10852 if (!XCOFF_DEBUGGING_INFO)
10854 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10855 dw2_asm_output_data (4, 0xffffffff,
10856 "Initial length escape value indicating 64-bit DWARF extension");
10857 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10858 "Pub Info Length");
10861 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10862 dw2_asm_output_data (2, 2, "DWARF Version");
10864 if (dwarf_split_debug_info)
10865 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10866 debug_skeleton_info_section,
10867 "Offset of Compilation Unit Info");
10868 else
10869 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10870 debug_info_section,
10871 "Offset of Compilation Unit Info");
10872 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10873 "Compilation Unit Length");
10875 FOR_EACH_VEC_ELT (*names, i, pub)
10877 if (include_pubname_in_output (names, pub))
10879 dw_offset die_offset = pub->die->die_offset;
10881 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10882 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10883 gcc_assert (pub->die->die_mark);
10885 /* If we're putting types in their own .debug_types sections,
10886 the .debug_pubtypes table will still point to the compile
10887 unit (not the type unit), so we want to use the offset of
10888 the skeleton DIE (if there is one). */
10889 if (pub->die->comdat_type_p && names == pubtype_table)
10891 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10893 if (type_node != NULL)
10894 die_offset = (type_node->skeleton_die != NULL
10895 ? type_node->skeleton_die->die_offset
10896 : comp_unit_die ()->die_offset);
10899 output_pubname (die_offset, pub);
10903 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10906 /* Output public names and types tables if necessary. */
10908 static void
10909 output_pubtables (void)
10911 if (!want_pubnames () || !info_section_emitted)
10912 return;
10914 switch_to_section (debug_pubnames_section);
10915 output_pubnames (pubname_table);
10916 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10917 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10918 simply won't look for the section. */
10919 switch_to_section (debug_pubtypes_section);
10920 output_pubnames (pubtype_table);
10924 /* Output the information that goes into the .debug_aranges table.
10925 Namely, define the beginning and ending address range of the
10926 text section generated for this compilation unit. */
10928 static void
10929 output_aranges (void)
10931 unsigned i;
10932 unsigned long aranges_length = size_of_aranges ();
10934 if (!XCOFF_DEBUGGING_INFO)
10936 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10937 dw2_asm_output_data (4, 0xffffffff,
10938 "Initial length escape value indicating 64-bit DWARF extension");
10939 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10940 "Length of Address Ranges Info");
10943 /* Version number for aranges is still 2, even up to DWARF5. */
10944 dw2_asm_output_data (2, 2, "DWARF Version");
10945 if (dwarf_split_debug_info)
10946 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10947 debug_skeleton_info_section,
10948 "Offset of Compilation Unit Info");
10949 else
10950 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10951 debug_info_section,
10952 "Offset of Compilation Unit Info");
10953 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10954 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10956 /* We need to align to twice the pointer size here. */
10957 if (DWARF_ARANGES_PAD_SIZE)
10959 /* Pad using a 2 byte words so that padding is correct for any
10960 pointer size. */
10961 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10962 2 * DWARF2_ADDR_SIZE);
10963 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10964 dw2_asm_output_data (2, 0, NULL);
10967 /* It is necessary not to output these entries if the sections were
10968 not used; if the sections were not used, the length will be 0 and
10969 the address may end up as 0 if the section is discarded by ld
10970 --gc-sections, leaving an invalid (0, 0) entry that can be
10971 confused with the terminator. */
10972 if (text_section_used)
10974 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10975 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10976 text_section_label, "Length");
10978 if (cold_text_section_used)
10980 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10981 "Address");
10982 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10983 cold_text_section_label, "Length");
10986 if (have_multiple_function_sections)
10988 unsigned fde_idx;
10989 dw_fde_ref fde;
10991 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10993 if (DECL_IGNORED_P (fde->decl))
10994 continue;
10995 if (!fde->in_std_section)
10997 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10998 "Address");
10999 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11000 fde->dw_fde_begin, "Length");
11002 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11004 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11005 "Address");
11006 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11007 fde->dw_fde_second_begin, "Length");
11012 /* Output the terminator words. */
11013 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11014 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11017 /* Add a new entry to .debug_ranges. Return its index into
11018 ranges_table vector. */
11020 static unsigned int
11021 add_ranges_num (int num, bool maybe_new_sec)
11023 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11024 vec_safe_push (ranges_table, r);
11025 return vec_safe_length (ranges_table) - 1;
11028 /* Add a new entry to .debug_ranges corresponding to a block, or a
11029 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11030 this entry might be in a different section from previous range. */
11032 static unsigned int
11033 add_ranges (const_tree block, bool maybe_new_sec)
11035 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11038 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11039 chain, or middle entry of a chain that will be directly referred to. */
11041 static void
11042 note_rnglist_head (unsigned int offset)
11044 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11045 return;
11046 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11049 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11050 When using dwarf_split_debug_info, address attributes in dies destined
11051 for the final executable should be direct references--setting the
11052 parameter force_direct ensures this behavior. */
11054 static void
11055 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11056 bool *added, bool force_direct)
11058 unsigned int in_use = vec_safe_length (ranges_by_label);
11059 unsigned int offset;
11060 dw_ranges_by_label rbl = { begin, end };
11061 vec_safe_push (ranges_by_label, rbl);
11062 offset = add_ranges_num (-(int)in_use - 1, true);
11063 if (!*added)
11065 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11066 *added = true;
11067 note_rnglist_head (offset);
11071 /* Emit .debug_ranges section. */
11073 static void
11074 output_ranges (void)
11076 unsigned i;
11077 static const char *const start_fmt = "Offset %#x";
11078 const char *fmt = start_fmt;
11079 dw_ranges *r;
11081 switch_to_section (debug_ranges_section);
11082 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11083 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11085 int block_num = r->num;
11087 if (block_num > 0)
11089 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11090 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11092 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11093 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11095 /* If all code is in the text section, then the compilation
11096 unit base address defaults to DW_AT_low_pc, which is the
11097 base of the text section. */
11098 if (!have_multiple_function_sections)
11100 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11101 text_section_label,
11102 fmt, i * 2 * DWARF2_ADDR_SIZE);
11103 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11104 text_section_label, NULL);
11107 /* Otherwise, the compilation unit base address is zero,
11108 which allows us to use absolute addresses, and not worry
11109 about whether the target supports cross-section
11110 arithmetic. */
11111 else
11113 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11114 fmt, i * 2 * DWARF2_ADDR_SIZE);
11115 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11118 fmt = NULL;
11121 /* Negative block_num stands for an index into ranges_by_label. */
11122 else if (block_num < 0)
11124 int lab_idx = - block_num - 1;
11126 if (!have_multiple_function_sections)
11128 gcc_unreachable ();
11129 #if 0
11130 /* If we ever use add_ranges_by_labels () for a single
11131 function section, all we have to do is to take out
11132 the #if 0 above. */
11133 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11134 (*ranges_by_label)[lab_idx].begin,
11135 text_section_label,
11136 fmt, i * 2 * DWARF2_ADDR_SIZE);
11137 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11138 (*ranges_by_label)[lab_idx].end,
11139 text_section_label, NULL);
11140 #endif
11142 else
11144 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11145 (*ranges_by_label)[lab_idx].begin,
11146 fmt, i * 2 * DWARF2_ADDR_SIZE);
11147 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11148 (*ranges_by_label)[lab_idx].end,
11149 NULL);
11152 else
11154 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11155 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11156 fmt = start_fmt;
11161 /* Non-zero if .debug_line_str should be used for .debug_line section
11162 strings or strings that are likely shareable with those. */
11163 #define DWARF5_USE_DEBUG_LINE_STR \
11164 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11165 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11166 /* FIXME: there is no .debug_line_str.dwo section, \
11167 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11168 && !dwarf_split_debug_info)
11170 /* Assign .debug_rnglists indexes. */
11172 static void
11173 index_rnglists (void)
11175 unsigned i;
11176 dw_ranges *r;
11178 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11179 if (r->label)
11180 r->idx = rnglist_idx++;
11183 /* Emit .debug_rnglists section. */
11185 static void
11186 output_rnglists (void)
11188 unsigned i;
11189 dw_ranges *r;
11190 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11191 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11192 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11194 switch_to_section (debug_ranges_section);
11195 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11196 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11197 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11198 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11199 dw2_asm_output_data (4, 0xffffffff,
11200 "Initial length escape value indicating "
11201 "64-bit DWARF extension");
11202 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11203 "Length of Range Lists");
11204 ASM_OUTPUT_LABEL (asm_out_file, l1);
11205 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11206 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11207 dw2_asm_output_data (1, 0, "Segment Size");
11208 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11209 about relocation sizes and primarily care about the size of .debug*
11210 sections in linked shared libraries and executables, then
11211 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11212 into it are usually larger than just DW_FORM_sec_offset offsets
11213 into the .debug_rnglists section. */
11214 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11215 "Offset Entry Count");
11216 if (dwarf_split_debug_info)
11218 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11219 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11220 if (r->label)
11221 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11222 ranges_base_label, NULL);
11225 const char *lab = "";
11226 unsigned int len = vec_safe_length (ranges_table);
11227 const char *base = NULL;
11228 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11230 int block_num = r->num;
11232 if (r->label)
11234 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11235 lab = r->label;
11237 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11238 base = NULL;
11239 if (block_num > 0)
11241 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11242 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11244 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11245 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11247 if (HAVE_AS_LEB128)
11249 /* If all code is in the text section, then the compilation
11250 unit base address defaults to DW_AT_low_pc, which is the
11251 base of the text section. */
11252 if (!have_multiple_function_sections)
11254 dw2_asm_output_data (1, DW_RLE_offset_pair,
11255 "DW_RLE_offset_pair (%s)", lab);
11256 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11257 "Range begin address (%s)", lab);
11258 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11259 "Range end address (%s)", lab);
11260 continue;
11262 if (base == NULL)
11264 dw_ranges *r2 = NULL;
11265 if (i < len - 1)
11266 r2 = &(*ranges_table)[i + 1];
11267 if (r2
11268 && r2->num != 0
11269 && r2->label == NULL
11270 && !r2->maybe_new_sec)
11272 dw2_asm_output_data (1, DW_RLE_base_address,
11273 "DW_RLE_base_address (%s)", lab);
11274 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11275 "Base address (%s)", lab);
11276 strcpy (basebuf, blabel);
11277 base = basebuf;
11280 if (base)
11282 dw2_asm_output_data (1, DW_RLE_offset_pair,
11283 "DW_RLE_offset_pair (%s)", lab);
11284 dw2_asm_output_delta_uleb128 (blabel, base,
11285 "Range begin address (%s)", lab);
11286 dw2_asm_output_delta_uleb128 (elabel, base,
11287 "Range end address (%s)", lab);
11288 continue;
11290 dw2_asm_output_data (1, DW_RLE_start_length,
11291 "DW_RLE_start_length (%s)", lab);
11292 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11293 "Range begin address (%s)", lab);
11294 dw2_asm_output_delta_uleb128 (elabel, blabel,
11295 "Range length (%s)", lab);
11297 else
11299 dw2_asm_output_data (1, DW_RLE_start_end,
11300 "DW_RLE_start_end (%s)", lab);
11301 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11302 "Range begin address (%s)", lab);
11303 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11304 "Range end address (%s)", lab);
11308 /* Negative block_num stands for an index into ranges_by_label. */
11309 else if (block_num < 0)
11311 int lab_idx = - block_num - 1;
11312 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11313 const char *elabel = (*ranges_by_label)[lab_idx].end;
11315 if (!have_multiple_function_sections)
11316 gcc_unreachable ();
11317 if (HAVE_AS_LEB128)
11319 dw2_asm_output_data (1, DW_RLE_start_length,
11320 "DW_RLE_start_length (%s)", lab);
11321 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11322 "Range begin address (%s)", lab);
11323 dw2_asm_output_delta_uleb128 (elabel, blabel,
11324 "Range length (%s)", lab);
11326 else
11328 dw2_asm_output_data (1, DW_RLE_start_end,
11329 "DW_RLE_start_end (%s)", lab);
11330 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11331 "Range begin address (%s)", lab);
11332 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11333 "Range end address (%s)", lab);
11336 else
11337 dw2_asm_output_data (1, DW_RLE_end_of_list,
11338 "DW_RLE_end_of_list (%s)", lab);
11340 ASM_OUTPUT_LABEL (asm_out_file, l2);
11343 /* Data structure containing information about input files. */
11344 struct file_info
11346 const char *path; /* Complete file name. */
11347 const char *fname; /* File name part. */
11348 int length; /* Length of entire string. */
11349 struct dwarf_file_data * file_idx; /* Index in input file table. */
11350 int dir_idx; /* Index in directory table. */
11353 /* Data structure containing information about directories with source
11354 files. */
11355 struct dir_info
11357 const char *path; /* Path including directory name. */
11358 int length; /* Path length. */
11359 int prefix; /* Index of directory entry which is a prefix. */
11360 int count; /* Number of files in this directory. */
11361 int dir_idx; /* Index of directory used as base. */
11364 /* Callback function for file_info comparison. We sort by looking at
11365 the directories in the path. */
11367 static int
11368 file_info_cmp (const void *p1, const void *p2)
11370 const struct file_info *const s1 = (const struct file_info *) p1;
11371 const struct file_info *const s2 = (const struct file_info *) p2;
11372 const unsigned char *cp1;
11373 const unsigned char *cp2;
11375 /* Take care of file names without directories. We need to make sure that
11376 we return consistent values to qsort since some will get confused if
11377 we return the same value when identical operands are passed in opposite
11378 orders. So if neither has a directory, return 0 and otherwise return
11379 1 or -1 depending on which one has the directory. */
11380 if ((s1->path == s1->fname || s2->path == s2->fname))
11381 return (s2->path == s2->fname) - (s1->path == s1->fname);
11383 cp1 = (const unsigned char *) s1->path;
11384 cp2 = (const unsigned char *) s2->path;
11386 while (1)
11388 ++cp1;
11389 ++cp2;
11390 /* Reached the end of the first path? If so, handle like above. */
11391 if ((cp1 == (const unsigned char *) s1->fname)
11392 || (cp2 == (const unsigned char *) s2->fname))
11393 return ((cp2 == (const unsigned char *) s2->fname)
11394 - (cp1 == (const unsigned char *) s1->fname));
11396 /* Character of current path component the same? */
11397 else if (*cp1 != *cp2)
11398 return *cp1 - *cp2;
11402 struct file_name_acquire_data
11404 struct file_info *files;
11405 int used_files;
11406 int max_files;
11409 /* Traversal function for the hash table. */
11412 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11414 struct dwarf_file_data *d = *slot;
11415 struct file_info *fi;
11416 const char *f;
11418 gcc_assert (fnad->max_files >= d->emitted_number);
11420 if (! d->emitted_number)
11421 return 1;
11423 gcc_assert (fnad->max_files != fnad->used_files);
11425 fi = fnad->files + fnad->used_files++;
11427 /* Skip all leading "./". */
11428 f = d->filename;
11429 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11430 f += 2;
11432 /* Create a new array entry. */
11433 fi->path = f;
11434 fi->length = strlen (f);
11435 fi->file_idx = d;
11437 /* Search for the file name part. */
11438 f = strrchr (f, DIR_SEPARATOR);
11439 #if defined (DIR_SEPARATOR_2)
11441 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11443 if (g != NULL)
11445 if (f == NULL || f < g)
11446 f = g;
11449 #endif
11451 fi->fname = f == NULL ? fi->path : f + 1;
11452 return 1;
11455 /* Helper function for output_file_names. Emit a FORM encoded
11456 string STR, with assembly comment start ENTRY_KIND and
11457 index IDX */
11459 static void
11460 output_line_string (enum dwarf_form form, const char *str,
11461 const char *entry_kind, unsigned int idx)
11463 switch (form)
11465 case DW_FORM_string:
11466 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11467 break;
11468 case DW_FORM_line_strp:
11469 if (!debug_line_str_hash)
11470 debug_line_str_hash
11471 = hash_table<indirect_string_hasher>::create_ggc (10);
11473 struct indirect_string_node *node;
11474 node = find_AT_string_in_table (str, debug_line_str_hash);
11475 set_indirect_string (node);
11476 node->form = form;
11477 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11478 debug_line_str_section, "%s: %#x: \"%s\"",
11479 entry_kind, 0, node->str);
11480 break;
11481 default:
11482 gcc_unreachable ();
11486 /* Output the directory table and the file name table. We try to minimize
11487 the total amount of memory needed. A heuristic is used to avoid large
11488 slowdowns with many input files. */
11490 static void
11491 output_file_names (void)
11493 struct file_name_acquire_data fnad;
11494 int numfiles;
11495 struct file_info *files;
11496 struct dir_info *dirs;
11497 int *saved;
11498 int *savehere;
11499 int *backmap;
11500 int ndirs;
11501 int idx_offset;
11502 int i;
11504 if (!last_emitted_file)
11506 if (dwarf_version >= 5)
11508 dw2_asm_output_data (1, 0, "Directory entry format count");
11509 dw2_asm_output_data_uleb128 (0, "Directories count");
11510 dw2_asm_output_data (1, 0, "File name entry format count");
11511 dw2_asm_output_data_uleb128 (0, "File names count");
11513 else
11515 dw2_asm_output_data (1, 0, "End directory table");
11516 dw2_asm_output_data (1, 0, "End file name table");
11518 return;
11521 numfiles = last_emitted_file->emitted_number;
11523 /* Allocate the various arrays we need. */
11524 files = XALLOCAVEC (struct file_info, numfiles);
11525 dirs = XALLOCAVEC (struct dir_info, numfiles);
11527 fnad.files = files;
11528 fnad.used_files = 0;
11529 fnad.max_files = numfiles;
11530 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11531 gcc_assert (fnad.used_files == fnad.max_files);
11533 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11535 /* Find all the different directories used. */
11536 dirs[0].path = files[0].path;
11537 dirs[0].length = files[0].fname - files[0].path;
11538 dirs[0].prefix = -1;
11539 dirs[0].count = 1;
11540 dirs[0].dir_idx = 0;
11541 files[0].dir_idx = 0;
11542 ndirs = 1;
11544 for (i = 1; i < numfiles; i++)
11545 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11546 && memcmp (dirs[ndirs - 1].path, files[i].path,
11547 dirs[ndirs - 1].length) == 0)
11549 /* Same directory as last entry. */
11550 files[i].dir_idx = ndirs - 1;
11551 ++dirs[ndirs - 1].count;
11553 else
11555 int j;
11557 /* This is a new directory. */
11558 dirs[ndirs].path = files[i].path;
11559 dirs[ndirs].length = files[i].fname - files[i].path;
11560 dirs[ndirs].count = 1;
11561 dirs[ndirs].dir_idx = ndirs;
11562 files[i].dir_idx = ndirs;
11564 /* Search for a prefix. */
11565 dirs[ndirs].prefix = -1;
11566 for (j = 0; j < ndirs; j++)
11567 if (dirs[j].length < dirs[ndirs].length
11568 && dirs[j].length > 1
11569 && (dirs[ndirs].prefix == -1
11570 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11571 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11572 dirs[ndirs].prefix = j;
11574 ++ndirs;
11577 /* Now to the actual work. We have to find a subset of the directories which
11578 allow expressing the file name using references to the directory table
11579 with the least amount of characters. We do not do an exhaustive search
11580 where we would have to check out every combination of every single
11581 possible prefix. Instead we use a heuristic which provides nearly optimal
11582 results in most cases and never is much off. */
11583 saved = XALLOCAVEC (int, ndirs);
11584 savehere = XALLOCAVEC (int, ndirs);
11586 memset (saved, '\0', ndirs * sizeof (saved[0]));
11587 for (i = 0; i < ndirs; i++)
11589 int j;
11590 int total;
11592 /* We can always save some space for the current directory. But this
11593 does not mean it will be enough to justify adding the directory. */
11594 savehere[i] = dirs[i].length;
11595 total = (savehere[i] - saved[i]) * dirs[i].count;
11597 for (j = i + 1; j < ndirs; j++)
11599 savehere[j] = 0;
11600 if (saved[j] < dirs[i].length)
11602 /* Determine whether the dirs[i] path is a prefix of the
11603 dirs[j] path. */
11604 int k;
11606 k = dirs[j].prefix;
11607 while (k != -1 && k != (int) i)
11608 k = dirs[k].prefix;
11610 if (k == (int) i)
11612 /* Yes it is. We can possibly save some memory by
11613 writing the filenames in dirs[j] relative to
11614 dirs[i]. */
11615 savehere[j] = dirs[i].length;
11616 total += (savehere[j] - saved[j]) * dirs[j].count;
11621 /* Check whether we can save enough to justify adding the dirs[i]
11622 directory. */
11623 if (total > dirs[i].length + 1)
11625 /* It's worthwhile adding. */
11626 for (j = i; j < ndirs; j++)
11627 if (savehere[j] > 0)
11629 /* Remember how much we saved for this directory so far. */
11630 saved[j] = savehere[j];
11632 /* Remember the prefix directory. */
11633 dirs[j].dir_idx = i;
11638 /* Emit the directory name table. */
11639 idx_offset = dirs[0].length > 0 ? 1 : 0;
11640 enum dwarf_form str_form = DW_FORM_string;
11641 enum dwarf_form idx_form = DW_FORM_udata;
11642 if (dwarf_version >= 5)
11644 const char *comp_dir = comp_dir_string ();
11645 if (comp_dir == NULL)
11646 comp_dir = "";
11647 dw2_asm_output_data (1, 1, "Directory entry format count");
11648 if (DWARF5_USE_DEBUG_LINE_STR)
11649 str_form = DW_FORM_line_strp;
11650 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11651 dw2_asm_output_data_uleb128 (str_form, "%s",
11652 get_DW_FORM_name (str_form));
11653 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11654 if (str_form == DW_FORM_string)
11656 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11657 for (i = 1 - idx_offset; i < ndirs; i++)
11658 dw2_asm_output_nstring (dirs[i].path,
11659 dirs[i].length
11660 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11661 "Directory Entry: %#x", i + idx_offset);
11663 else
11665 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11666 for (i = 1 - idx_offset; i < ndirs; i++)
11668 const char *str
11669 = ggc_alloc_string (dirs[i].path,
11670 dirs[i].length
11671 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11672 output_line_string (str_form, str, "Directory Entry",
11673 (unsigned) i + idx_offset);
11677 else
11679 for (i = 1 - idx_offset; i < ndirs; i++)
11680 dw2_asm_output_nstring (dirs[i].path,
11681 dirs[i].length
11682 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11683 "Directory Entry: %#x", i + idx_offset);
11685 dw2_asm_output_data (1, 0, "End directory table");
11688 /* We have to emit them in the order of emitted_number since that's
11689 used in the debug info generation. To do this efficiently we
11690 generate a back-mapping of the indices first. */
11691 backmap = XALLOCAVEC (int, numfiles);
11692 for (i = 0; i < numfiles; i++)
11693 backmap[files[i].file_idx->emitted_number - 1] = i;
11695 if (dwarf_version >= 5)
11697 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11698 if (filename0 == NULL)
11699 filename0 = "";
11700 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11701 DW_FORM_data2. Choose one based on the number of directories
11702 and how much space would they occupy in each encoding.
11703 If we have at most 256 directories, all indexes fit into
11704 a single byte, so DW_FORM_data1 is most compact (if there
11705 are at most 128 directories, DW_FORM_udata would be as
11706 compact as that, but not shorter and slower to decode). */
11707 if (ndirs + idx_offset <= 256)
11708 idx_form = DW_FORM_data1;
11709 /* If there are more than 65536 directories, we have to use
11710 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11711 Otherwise, compute what space would occupy if all the indexes
11712 used DW_FORM_udata - sum - and compare that to how large would
11713 be DW_FORM_data2 encoding, and pick the more efficient one. */
11714 else if (ndirs + idx_offset <= 65536)
11716 unsigned HOST_WIDE_INT sum = 1;
11717 for (i = 0; i < numfiles; i++)
11719 int file_idx = backmap[i];
11720 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11721 sum += size_of_uleb128 (dir_idx);
11723 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11724 idx_form = DW_FORM_data2;
11726 #ifdef VMS_DEBUGGING_INFO
11727 dw2_asm_output_data (1, 4, "File name entry format count");
11728 #else
11729 dw2_asm_output_data (1, 2, "File name entry format count");
11730 #endif
11731 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11732 dw2_asm_output_data_uleb128 (str_form, "%s",
11733 get_DW_FORM_name (str_form));
11734 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11735 "DW_LNCT_directory_index");
11736 dw2_asm_output_data_uleb128 (idx_form, "%s",
11737 get_DW_FORM_name (idx_form));
11738 #ifdef VMS_DEBUGGING_INFO
11739 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11740 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11741 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11742 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11743 #endif
11744 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11746 output_line_string (str_form, filename0, "File Entry", 0);
11748 /* Include directory index. */
11749 if (idx_form != DW_FORM_udata)
11750 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11751 0, NULL);
11752 else
11753 dw2_asm_output_data_uleb128 (0, NULL);
11755 #ifdef VMS_DEBUGGING_INFO
11756 dw2_asm_output_data_uleb128 (0, NULL);
11757 dw2_asm_output_data_uleb128 (0, NULL);
11758 #endif
11761 /* Now write all the file names. */
11762 for (i = 0; i < numfiles; i++)
11764 int file_idx = backmap[i];
11765 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11767 #ifdef VMS_DEBUGGING_INFO
11768 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11770 /* Setting these fields can lead to debugger miscomparisons,
11771 but VMS Debug requires them to be set correctly. */
11773 int ver;
11774 long long cdt;
11775 long siz;
11776 int maxfilelen = (strlen (files[file_idx].path)
11777 + dirs[dir_idx].length
11778 + MAX_VMS_VERSION_LEN + 1);
11779 char *filebuf = XALLOCAVEC (char, maxfilelen);
11781 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11782 snprintf (filebuf, maxfilelen, "%s;%d",
11783 files[file_idx].path + dirs[dir_idx].length, ver);
11785 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11787 /* Include directory index. */
11788 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11789 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11790 dir_idx + idx_offset, NULL);
11791 else
11792 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11794 /* Modification time. */
11795 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11796 &cdt, 0, 0, 0) == 0)
11797 ? cdt : 0, NULL);
11799 /* File length in bytes. */
11800 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11801 0, &siz, 0, 0) == 0)
11802 ? siz : 0, NULL);
11803 #else
11804 output_line_string (str_form,
11805 files[file_idx].path + dirs[dir_idx].length,
11806 "File Entry", (unsigned) i + 1);
11808 /* Include directory index. */
11809 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11810 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11811 dir_idx + idx_offset, NULL);
11812 else
11813 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11815 if (dwarf_version >= 5)
11816 continue;
11818 /* Modification time. */
11819 dw2_asm_output_data_uleb128 (0, NULL);
11821 /* File length in bytes. */
11822 dw2_asm_output_data_uleb128 (0, NULL);
11823 #endif /* VMS_DEBUGGING_INFO */
11826 if (dwarf_version < 5)
11827 dw2_asm_output_data (1, 0, "End file name table");
11831 /* Output one line number table into the .debug_line section. */
11833 static void
11834 output_one_line_info_table (dw_line_info_table *table)
11836 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11837 unsigned int current_line = 1;
11838 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11839 dw_line_info_entry *ent;
11840 size_t i;
11842 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11844 switch (ent->opcode)
11846 case LI_set_address:
11847 /* ??? Unfortunately, we have little choice here currently, and
11848 must always use the most general form. GCC does not know the
11849 address delta itself, so we can't use DW_LNS_advance_pc. Many
11850 ports do have length attributes which will give an upper bound
11851 on the address range. We could perhaps use length attributes
11852 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11853 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11855 /* This can handle any delta. This takes
11856 4+DWARF2_ADDR_SIZE bytes. */
11857 dw2_asm_output_data (1, 0, "set address %s", line_label);
11858 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11859 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11860 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11861 break;
11863 case LI_set_line:
11864 if (ent->val == current_line)
11866 /* We still need to start a new row, so output a copy insn. */
11867 dw2_asm_output_data (1, DW_LNS_copy,
11868 "copy line %u", current_line);
11870 else
11872 int line_offset = ent->val - current_line;
11873 int line_delta = line_offset - DWARF_LINE_BASE;
11875 current_line = ent->val;
11876 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11878 /* This can handle deltas from -10 to 234, using the current
11879 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11880 This takes 1 byte. */
11881 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11882 "line %u", current_line);
11884 else
11886 /* This can handle any delta. This takes at least 4 bytes,
11887 depending on the value being encoded. */
11888 dw2_asm_output_data (1, DW_LNS_advance_line,
11889 "advance to line %u", current_line);
11890 dw2_asm_output_data_sleb128 (line_offset, NULL);
11891 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11894 break;
11896 case LI_set_file:
11897 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11898 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11899 break;
11901 case LI_set_column:
11902 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11903 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11904 break;
11906 case LI_negate_stmt:
11907 current_is_stmt = !current_is_stmt;
11908 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11909 "is_stmt %d", current_is_stmt);
11910 break;
11912 case LI_set_prologue_end:
11913 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11914 "set prologue end");
11915 break;
11917 case LI_set_epilogue_begin:
11918 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11919 "set epilogue begin");
11920 break;
11922 case LI_set_discriminator:
11923 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11924 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11925 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11926 dw2_asm_output_data_uleb128 (ent->val, NULL);
11927 break;
11931 /* Emit debug info for the address of the end of the table. */
11932 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11933 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11934 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11935 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11937 dw2_asm_output_data (1, 0, "end sequence");
11938 dw2_asm_output_data_uleb128 (1, NULL);
11939 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11942 /* Output the source line number correspondence information. This
11943 information goes into the .debug_line section. */
11945 static void
11946 output_line_info (bool prologue_only)
11948 static unsigned int generation;
11949 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11950 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11951 bool saw_one = false;
11952 int opc;
11954 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11955 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11956 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11957 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11959 if (!XCOFF_DEBUGGING_INFO)
11961 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11962 dw2_asm_output_data (4, 0xffffffff,
11963 "Initial length escape value indicating 64-bit DWARF extension");
11964 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11965 "Length of Source Line Info");
11968 ASM_OUTPUT_LABEL (asm_out_file, l1);
11970 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11971 if (dwarf_version >= 5)
11973 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11974 dw2_asm_output_data (1, 0, "Segment Size");
11976 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11977 ASM_OUTPUT_LABEL (asm_out_file, p1);
11979 /* Define the architecture-dependent minimum instruction length (in bytes).
11980 In this implementation of DWARF, this field is used for information
11981 purposes only. Since GCC generates assembly language, we have no
11982 a priori knowledge of how many instruction bytes are generated for each
11983 source line, and therefore can use only the DW_LNE_set_address and
11984 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11985 this as '1', which is "correct enough" for all architectures,
11986 and don't let the target override. */
11987 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11989 if (dwarf_version >= 4)
11990 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11991 "Maximum Operations Per Instruction");
11992 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11993 "Default is_stmt_start flag");
11994 dw2_asm_output_data (1, DWARF_LINE_BASE,
11995 "Line Base Value (Special Opcodes)");
11996 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11997 "Line Range Value (Special Opcodes)");
11998 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11999 "Special Opcode Base");
12001 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12003 int n_op_args;
12004 switch (opc)
12006 case DW_LNS_advance_pc:
12007 case DW_LNS_advance_line:
12008 case DW_LNS_set_file:
12009 case DW_LNS_set_column:
12010 case DW_LNS_fixed_advance_pc:
12011 case DW_LNS_set_isa:
12012 n_op_args = 1;
12013 break;
12014 default:
12015 n_op_args = 0;
12016 break;
12019 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12020 opc, n_op_args);
12023 /* Write out the information about the files we use. */
12024 output_file_names ();
12025 ASM_OUTPUT_LABEL (asm_out_file, p2);
12026 if (prologue_only)
12028 /* Output the marker for the end of the line number info. */
12029 ASM_OUTPUT_LABEL (asm_out_file, l2);
12030 return;
12033 if (separate_line_info)
12035 dw_line_info_table *table;
12036 size_t i;
12038 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12039 if (table->in_use)
12041 output_one_line_info_table (table);
12042 saw_one = true;
12045 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12047 output_one_line_info_table (cold_text_section_line_info);
12048 saw_one = true;
12051 /* ??? Some Darwin linkers crash on a .debug_line section with no
12052 sequences. Further, merely a DW_LNE_end_sequence entry is not
12053 sufficient -- the address column must also be initialized.
12054 Make sure to output at least one set_address/end_sequence pair,
12055 choosing .text since that section is always present. */
12056 if (text_section_line_info->in_use || !saw_one)
12057 output_one_line_info_table (text_section_line_info);
12059 /* Output the marker for the end of the line number info. */
12060 ASM_OUTPUT_LABEL (asm_out_file, l2);
12063 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12065 static inline bool
12066 need_endianity_attribute_p (bool reverse)
12068 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12071 /* Given a pointer to a tree node for some base type, return a pointer to
12072 a DIE that describes the given type. REVERSE is true if the type is
12073 to be interpreted in the reverse storage order wrt the target order.
12075 This routine must only be called for GCC type nodes that correspond to
12076 Dwarf base (fundamental) types. */
12078 static dw_die_ref
12079 base_type_die (tree type, bool reverse)
12081 dw_die_ref base_type_result;
12082 enum dwarf_type encoding;
12083 bool fpt_used = false;
12084 struct fixed_point_type_info fpt_info;
12085 tree type_bias = NULL_TREE;
12087 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12088 return 0;
12090 /* If this is a subtype that should not be emitted as a subrange type,
12091 use the base type. See subrange_type_for_debug_p. */
12092 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12093 type = TREE_TYPE (type);
12095 switch (TREE_CODE (type))
12097 case INTEGER_TYPE:
12098 if ((dwarf_version >= 4 || !dwarf_strict)
12099 && TYPE_NAME (type)
12100 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12101 && DECL_IS_BUILTIN (TYPE_NAME (type))
12102 && DECL_NAME (TYPE_NAME (type)))
12104 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12105 if (strcmp (name, "char16_t") == 0
12106 || strcmp (name, "char32_t") == 0)
12108 encoding = DW_ATE_UTF;
12109 break;
12112 if ((dwarf_version >= 3 || !dwarf_strict)
12113 && lang_hooks.types.get_fixed_point_type_info)
12115 memset (&fpt_info, 0, sizeof (fpt_info));
12116 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12118 fpt_used = true;
12119 encoding = ((TYPE_UNSIGNED (type))
12120 ? DW_ATE_unsigned_fixed
12121 : DW_ATE_signed_fixed);
12122 break;
12125 if (TYPE_STRING_FLAG (type))
12127 if (TYPE_UNSIGNED (type))
12128 encoding = DW_ATE_unsigned_char;
12129 else
12130 encoding = DW_ATE_signed_char;
12132 else if (TYPE_UNSIGNED (type))
12133 encoding = DW_ATE_unsigned;
12134 else
12135 encoding = DW_ATE_signed;
12137 if (!dwarf_strict
12138 && lang_hooks.types.get_type_bias)
12139 type_bias = lang_hooks.types.get_type_bias (type);
12140 break;
12142 case REAL_TYPE:
12143 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12145 if (dwarf_version >= 3 || !dwarf_strict)
12146 encoding = DW_ATE_decimal_float;
12147 else
12148 encoding = DW_ATE_lo_user;
12150 else
12151 encoding = DW_ATE_float;
12152 break;
12154 case FIXED_POINT_TYPE:
12155 if (!(dwarf_version >= 3 || !dwarf_strict))
12156 encoding = DW_ATE_lo_user;
12157 else if (TYPE_UNSIGNED (type))
12158 encoding = DW_ATE_unsigned_fixed;
12159 else
12160 encoding = DW_ATE_signed_fixed;
12161 break;
12163 /* Dwarf2 doesn't know anything about complex ints, so use
12164 a user defined type for it. */
12165 case COMPLEX_TYPE:
12166 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12167 encoding = DW_ATE_complex_float;
12168 else
12169 encoding = DW_ATE_lo_user;
12170 break;
12172 case BOOLEAN_TYPE:
12173 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12174 encoding = DW_ATE_boolean;
12175 break;
12177 default:
12178 /* No other TREE_CODEs are Dwarf fundamental types. */
12179 gcc_unreachable ();
12182 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12184 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12185 int_size_in_bytes (type));
12186 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12188 if (need_endianity_attribute_p (reverse))
12189 add_AT_unsigned (base_type_result, DW_AT_endianity,
12190 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12192 add_alignment_attribute (base_type_result, type);
12194 if (fpt_used)
12196 switch (fpt_info.scale_factor_kind)
12198 case fixed_point_scale_factor_binary:
12199 add_AT_int (base_type_result, DW_AT_binary_scale,
12200 fpt_info.scale_factor.binary);
12201 break;
12203 case fixed_point_scale_factor_decimal:
12204 add_AT_int (base_type_result, DW_AT_decimal_scale,
12205 fpt_info.scale_factor.decimal);
12206 break;
12208 case fixed_point_scale_factor_arbitrary:
12209 /* Arbitrary scale factors cannot be described in standard DWARF,
12210 yet. */
12211 if (!dwarf_strict)
12213 /* Describe the scale factor as a rational constant. */
12214 const dw_die_ref scale_factor
12215 = new_die (DW_TAG_constant, comp_unit_die (), type);
12217 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12218 fpt_info.scale_factor.arbitrary.numerator);
12219 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12220 fpt_info.scale_factor.arbitrary.denominator);
12222 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12224 break;
12226 default:
12227 gcc_unreachable ();
12231 if (type_bias)
12232 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12233 dw_scalar_form_constant
12234 | dw_scalar_form_exprloc
12235 | dw_scalar_form_reference,
12236 NULL);
12238 add_pubtype (type, base_type_result);
12240 return base_type_result;
12243 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12244 named 'auto' in its type: return true for it, false otherwise. */
12246 static inline bool
12247 is_cxx_auto (tree type)
12249 if (is_cxx ())
12251 tree name = TYPE_IDENTIFIER (type);
12252 if (name == get_identifier ("auto")
12253 || name == get_identifier ("decltype(auto)"))
12254 return true;
12256 return false;
12259 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12260 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12262 static inline int
12263 is_base_type (tree type)
12265 switch (TREE_CODE (type))
12267 case ERROR_MARK:
12268 case VOID_TYPE:
12269 case INTEGER_TYPE:
12270 case REAL_TYPE:
12271 case FIXED_POINT_TYPE:
12272 case COMPLEX_TYPE:
12273 case BOOLEAN_TYPE:
12274 case POINTER_BOUNDS_TYPE:
12275 return 1;
12277 case ARRAY_TYPE:
12278 case RECORD_TYPE:
12279 case UNION_TYPE:
12280 case QUAL_UNION_TYPE:
12281 case ENUMERAL_TYPE:
12282 case FUNCTION_TYPE:
12283 case METHOD_TYPE:
12284 case POINTER_TYPE:
12285 case REFERENCE_TYPE:
12286 case NULLPTR_TYPE:
12287 case OFFSET_TYPE:
12288 case LANG_TYPE:
12289 case VECTOR_TYPE:
12290 return 0;
12292 default:
12293 if (is_cxx_auto (type))
12294 return 0;
12295 gcc_unreachable ();
12298 return 0;
12301 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12302 node, return the size in bits for the type if it is a constant, or else
12303 return the alignment for the type if the type's size is not constant, or
12304 else return BITS_PER_WORD if the type actually turns out to be an
12305 ERROR_MARK node. */
12307 static inline unsigned HOST_WIDE_INT
12308 simple_type_size_in_bits (const_tree type)
12310 if (TREE_CODE (type) == ERROR_MARK)
12311 return BITS_PER_WORD;
12312 else if (TYPE_SIZE (type) == NULL_TREE)
12313 return 0;
12314 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12315 return tree_to_uhwi (TYPE_SIZE (type));
12316 else
12317 return TYPE_ALIGN (type);
12320 /* Similarly, but return an offset_int instead of UHWI. */
12322 static inline offset_int
12323 offset_int_type_size_in_bits (const_tree type)
12325 if (TREE_CODE (type) == ERROR_MARK)
12326 return BITS_PER_WORD;
12327 else if (TYPE_SIZE (type) == NULL_TREE)
12328 return 0;
12329 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12330 return wi::to_offset (TYPE_SIZE (type));
12331 else
12332 return TYPE_ALIGN (type);
12335 /* Given a pointer to a tree node for a subrange type, return a pointer
12336 to a DIE that describes the given type. */
12338 static dw_die_ref
12339 subrange_type_die (tree type, tree low, tree high, tree bias,
12340 dw_die_ref context_die)
12342 dw_die_ref subrange_die;
12343 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12345 if (context_die == NULL)
12346 context_die = comp_unit_die ();
12348 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12350 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12352 /* The size of the subrange type and its base type do not match,
12353 so we need to generate a size attribute for the subrange type. */
12354 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12357 add_alignment_attribute (subrange_die, type);
12359 if (low)
12360 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12361 if (high)
12362 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12363 if (bias && !dwarf_strict)
12364 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12365 dw_scalar_form_constant
12366 | dw_scalar_form_exprloc
12367 | dw_scalar_form_reference,
12368 NULL);
12370 return subrange_die;
12373 /* Returns the (const and/or volatile) cv_qualifiers associated with
12374 the decl node. This will normally be augmented with the
12375 cv_qualifiers of the underlying type in add_type_attribute. */
12377 static int
12378 decl_quals (const_tree decl)
12380 return ((TREE_READONLY (decl)
12381 /* The C++ front-end correctly marks reference-typed
12382 variables as readonly, but from a language (and debug
12383 info) standpoint they are not const-qualified. */
12384 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12385 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12386 | (TREE_THIS_VOLATILE (decl)
12387 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12390 /* Determine the TYPE whose qualifiers match the largest strict subset
12391 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12392 qualifiers outside QUAL_MASK. */
12394 static int
12395 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12397 tree t;
12398 int best_rank = 0, best_qual = 0, max_rank;
12400 type_quals &= qual_mask;
12401 max_rank = popcount_hwi (type_quals) - 1;
12403 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12404 t = TYPE_NEXT_VARIANT (t))
12406 int q = TYPE_QUALS (t) & qual_mask;
12408 if ((q & type_quals) == q && q != type_quals
12409 && check_base_type (t, type))
12411 int rank = popcount_hwi (q);
12413 if (rank > best_rank)
12415 best_rank = rank;
12416 best_qual = q;
12421 return best_qual;
12424 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12425 static const dwarf_qual_info_t dwarf_qual_info[] =
12427 { TYPE_QUAL_CONST, DW_TAG_const_type },
12428 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12429 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12430 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12432 static const unsigned int dwarf_qual_info_size
12433 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12435 /* If DIE is a qualified DIE of some base DIE with the same parent,
12436 return the base DIE, otherwise return NULL. Set MASK to the
12437 qualifiers added compared to the returned DIE. */
12439 static dw_die_ref
12440 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12442 unsigned int i;
12443 for (i = 0; i < dwarf_qual_info_size; i++)
12444 if (die->die_tag == dwarf_qual_info[i].t)
12445 break;
12446 if (i == dwarf_qual_info_size)
12447 return NULL;
12448 if (vec_safe_length (die->die_attr) != 1)
12449 return NULL;
12450 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12451 if (type == NULL || type->die_parent != die->die_parent)
12452 return NULL;
12453 *mask |= dwarf_qual_info[i].q;
12454 if (depth)
12456 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12457 if (ret)
12458 return ret;
12460 return type;
12463 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12464 entry that chains the modifiers specified by CV_QUALS in front of the
12465 given type. REVERSE is true if the type is to be interpreted in the
12466 reverse storage order wrt the target order. */
12468 static dw_die_ref
12469 modified_type_die (tree type, int cv_quals, bool reverse,
12470 dw_die_ref context_die)
12472 enum tree_code code = TREE_CODE (type);
12473 dw_die_ref mod_type_die;
12474 dw_die_ref sub_die = NULL;
12475 tree item_type = NULL;
12476 tree qualified_type;
12477 tree name, low, high;
12478 dw_die_ref mod_scope;
12479 /* Only these cv-qualifiers are currently handled. */
12480 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12481 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12483 if (code == ERROR_MARK)
12484 return NULL;
12486 if (lang_hooks.types.get_debug_type)
12488 tree debug_type = lang_hooks.types.get_debug_type (type);
12490 if (debug_type != NULL_TREE && debug_type != type)
12491 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12494 cv_quals &= cv_qual_mask;
12496 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12497 tag modifier (and not an attribute) old consumers won't be able
12498 to handle it. */
12499 if (dwarf_version < 3)
12500 cv_quals &= ~TYPE_QUAL_RESTRICT;
12502 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12503 if (dwarf_version < 5)
12504 cv_quals &= ~TYPE_QUAL_ATOMIC;
12506 /* See if we already have the appropriately qualified variant of
12507 this type. */
12508 qualified_type = get_qualified_type (type, cv_quals);
12510 if (qualified_type == sizetype)
12512 /* Try not to expose the internal sizetype type's name. */
12513 if (TYPE_NAME (qualified_type)
12514 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12516 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12518 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12519 && (TYPE_PRECISION (t)
12520 == TYPE_PRECISION (qualified_type))
12521 && (TYPE_UNSIGNED (t)
12522 == TYPE_UNSIGNED (qualified_type)));
12523 qualified_type = t;
12525 else if (qualified_type == sizetype
12526 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12527 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12528 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12529 qualified_type = size_type_node;
12533 /* If we do, then we can just use its DIE, if it exists. */
12534 if (qualified_type)
12536 mod_type_die = lookup_type_die (qualified_type);
12538 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12539 if (mod_type_die
12540 && (!need_endianity_attribute_p (reverse)
12541 || !is_base_type (type)
12542 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12543 return mod_type_die;
12546 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12548 /* Handle C typedef types. */
12549 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12550 && !DECL_ARTIFICIAL (name))
12552 tree dtype = TREE_TYPE (name);
12554 if (qualified_type == dtype)
12556 tree origin = decl_ultimate_origin (name);
12558 /* Typedef variants that have an abstract origin don't get their own
12559 type DIE (see gen_typedef_die), so fall back on the ultimate
12560 abstract origin instead. */
12561 if (origin != NULL && origin != name)
12562 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12563 context_die);
12565 /* For a named type, use the typedef. */
12566 gen_type_die (qualified_type, context_die);
12567 return lookup_type_die (qualified_type);
12569 else
12571 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12572 dquals &= cv_qual_mask;
12573 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12574 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12575 /* cv-unqualified version of named type. Just use
12576 the unnamed type to which it refers. */
12577 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12578 reverse, context_die);
12579 /* Else cv-qualified version of named type; fall through. */
12583 mod_scope = scope_die_for (type, context_die);
12585 if (cv_quals)
12587 int sub_quals = 0, first_quals = 0;
12588 unsigned i;
12589 dw_die_ref first = NULL, last = NULL;
12591 /* Determine a lesser qualified type that most closely matches
12592 this one. Then generate DW_TAG_* entries for the remaining
12593 qualifiers. */
12594 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12595 cv_qual_mask);
12596 if (sub_quals && use_debug_types)
12598 bool needed = false;
12599 /* If emitting type units, make sure the order of qualifiers
12600 is canonical. Thus, start from unqualified type if
12601 an earlier qualifier is missing in sub_quals, but some later
12602 one is present there. */
12603 for (i = 0; i < dwarf_qual_info_size; i++)
12604 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12605 needed = true;
12606 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12608 sub_quals = 0;
12609 break;
12612 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12613 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12615 /* As not all intermediate qualified DIEs have corresponding
12616 tree types, ensure that qualified DIEs in the same scope
12617 as their DW_AT_type are emitted after their DW_AT_type,
12618 only with other qualified DIEs for the same type possibly
12619 in between them. Determine the range of such qualified
12620 DIEs now (first being the base type, last being corresponding
12621 last qualified DIE for it). */
12622 unsigned int count = 0;
12623 first = qualified_die_p (mod_type_die, &first_quals,
12624 dwarf_qual_info_size);
12625 if (first == NULL)
12626 first = mod_type_die;
12627 gcc_assert ((first_quals & ~sub_quals) == 0);
12628 for (count = 0, last = first;
12629 count < (1U << dwarf_qual_info_size);
12630 count++, last = last->die_sib)
12632 int quals = 0;
12633 if (last == mod_scope->die_child)
12634 break;
12635 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12636 != first)
12637 break;
12641 for (i = 0; i < dwarf_qual_info_size; i++)
12642 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12644 dw_die_ref d;
12645 if (first && first != last)
12647 for (d = first->die_sib; ; d = d->die_sib)
12649 int quals = 0;
12650 qualified_die_p (d, &quals, dwarf_qual_info_size);
12651 if (quals == (first_quals | dwarf_qual_info[i].q))
12652 break;
12653 if (d == last)
12655 d = NULL;
12656 break;
12659 if (d)
12661 mod_type_die = d;
12662 continue;
12665 if (first)
12667 d = ggc_cleared_alloc<die_node> ();
12668 d->die_tag = dwarf_qual_info[i].t;
12669 add_child_die_after (mod_scope, d, last);
12670 last = d;
12672 else
12673 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12674 if (mod_type_die)
12675 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12676 mod_type_die = d;
12677 first_quals |= dwarf_qual_info[i].q;
12680 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12682 dwarf_tag tag = DW_TAG_pointer_type;
12683 if (code == REFERENCE_TYPE)
12685 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12686 tag = DW_TAG_rvalue_reference_type;
12687 else
12688 tag = DW_TAG_reference_type;
12690 mod_type_die = new_die (tag, mod_scope, type);
12692 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12693 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12694 add_alignment_attribute (mod_type_die, type);
12695 item_type = TREE_TYPE (type);
12697 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12698 if (!ADDR_SPACE_GENERIC_P (as))
12700 int action = targetm.addr_space.debug (as);
12701 if (action >= 0)
12703 /* Positive values indicate an address_class. */
12704 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12706 else
12708 /* Negative values indicate an (inverted) segment base reg. */
12709 dw_loc_descr_ref d
12710 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12711 add_AT_loc (mod_type_die, DW_AT_segment, d);
12715 else if (code == INTEGER_TYPE
12716 && TREE_TYPE (type) != NULL_TREE
12717 && subrange_type_for_debug_p (type, &low, &high))
12719 tree bias = NULL_TREE;
12720 if (lang_hooks.types.get_type_bias)
12721 bias = lang_hooks.types.get_type_bias (type);
12722 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12723 item_type = TREE_TYPE (type);
12725 else if (is_base_type (type))
12726 mod_type_die = base_type_die (type, reverse);
12727 else
12729 gen_type_die (type, context_die);
12731 /* We have to get the type_main_variant here (and pass that to the
12732 `lookup_type_die' routine) because the ..._TYPE node we have
12733 might simply be a *copy* of some original type node (where the
12734 copy was created to help us keep track of typedef names) and
12735 that copy might have a different TYPE_UID from the original
12736 ..._TYPE node. */
12737 if (TREE_CODE (type) == FUNCTION_TYPE
12738 || TREE_CODE (type) == METHOD_TYPE)
12740 /* For function/method types, can't just use type_main_variant here,
12741 because that can have different ref-qualifiers for C++,
12742 but try to canonicalize. */
12743 tree main = TYPE_MAIN_VARIANT (type);
12744 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12745 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12746 && check_base_type (t, main)
12747 && check_lang_type (t, type))
12748 return lookup_type_die (t);
12749 return lookup_type_die (type);
12751 else if (TREE_CODE (type) != VECTOR_TYPE
12752 && TREE_CODE (type) != ARRAY_TYPE)
12753 return lookup_type_die (type_main_variant (type));
12754 else
12755 /* Vectors have the debugging information in the type,
12756 not the main variant. */
12757 return lookup_type_die (type);
12760 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12761 don't output a DW_TAG_typedef, since there isn't one in the
12762 user's program; just attach a DW_AT_name to the type.
12763 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12764 if the base type already has the same name. */
12765 if (name
12766 && ((TREE_CODE (name) != TYPE_DECL
12767 && (qualified_type == TYPE_MAIN_VARIANT (type)
12768 || (cv_quals == TYPE_UNQUALIFIED)))
12769 || (TREE_CODE (name) == TYPE_DECL
12770 && TREE_TYPE (name) == qualified_type
12771 && DECL_NAME (name))))
12773 if (TREE_CODE (name) == TYPE_DECL)
12774 /* Could just call add_name_and_src_coords_attributes here,
12775 but since this is a builtin type it doesn't have any
12776 useful source coordinates anyway. */
12777 name = DECL_NAME (name);
12778 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12780 /* This probably indicates a bug. */
12781 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12783 name = TYPE_IDENTIFIER (type);
12784 add_name_attribute (mod_type_die,
12785 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12788 if (qualified_type)
12789 equate_type_number_to_die (qualified_type, mod_type_die);
12791 if (item_type)
12792 /* We must do this after the equate_type_number_to_die call, in case
12793 this is a recursive type. This ensures that the modified_type_die
12794 recursion will terminate even if the type is recursive. Recursive
12795 types are possible in Ada. */
12796 sub_die = modified_type_die (item_type,
12797 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12798 reverse,
12799 context_die);
12801 if (sub_die != NULL)
12802 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12804 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12805 if (TYPE_ARTIFICIAL (type))
12806 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12808 return mod_type_die;
12811 /* Generate DIEs for the generic parameters of T.
12812 T must be either a generic type or a generic function.
12813 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12815 static void
12816 gen_generic_params_dies (tree t)
12818 tree parms, args;
12819 int parms_num, i;
12820 dw_die_ref die = NULL;
12821 int non_default;
12823 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12824 return;
12826 if (TYPE_P (t))
12827 die = lookup_type_die (t);
12828 else if (DECL_P (t))
12829 die = lookup_decl_die (t);
12831 gcc_assert (die);
12833 parms = lang_hooks.get_innermost_generic_parms (t);
12834 if (!parms)
12835 /* T has no generic parameter. It means T is neither a generic type
12836 or function. End of story. */
12837 return;
12839 parms_num = TREE_VEC_LENGTH (parms);
12840 args = lang_hooks.get_innermost_generic_args (t);
12841 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12842 non_default = int_cst_value (TREE_CHAIN (args));
12843 else
12844 non_default = TREE_VEC_LENGTH (args);
12845 for (i = 0; i < parms_num; i++)
12847 tree parm, arg, arg_pack_elems;
12848 dw_die_ref parm_die;
12850 parm = TREE_VEC_ELT (parms, i);
12851 arg = TREE_VEC_ELT (args, i);
12852 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12853 gcc_assert (parm && TREE_VALUE (parm) && arg);
12855 if (parm && TREE_VALUE (parm) && arg)
12857 /* If PARM represents a template parameter pack,
12858 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12859 by DW_TAG_template_*_parameter DIEs for the argument
12860 pack elements of ARG. Note that ARG would then be
12861 an argument pack. */
12862 if (arg_pack_elems)
12863 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12864 arg_pack_elems,
12865 die);
12866 else
12867 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12868 true /* emit name */, die);
12869 if (i >= non_default)
12870 add_AT_flag (parm_die, DW_AT_default_value, 1);
12875 /* Create and return a DIE for PARM which should be
12876 the representation of a generic type parameter.
12877 For instance, in the C++ front end, PARM would be a template parameter.
12878 ARG is the argument to PARM.
12879 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12880 name of the PARM.
12881 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12882 as a child node. */
12884 static dw_die_ref
12885 generic_parameter_die (tree parm, tree arg,
12886 bool emit_name_p,
12887 dw_die_ref parent_die)
12889 dw_die_ref tmpl_die = NULL;
12890 const char *name = NULL;
12892 if (!parm || !DECL_NAME (parm) || !arg)
12893 return NULL;
12895 /* We support non-type generic parameters and arguments,
12896 type generic parameters and arguments, as well as
12897 generic generic parameters (a.k.a. template template parameters in C++)
12898 and arguments. */
12899 if (TREE_CODE (parm) == PARM_DECL)
12900 /* PARM is a nontype generic parameter */
12901 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12902 else if (TREE_CODE (parm) == TYPE_DECL)
12903 /* PARM is a type generic parameter. */
12904 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12905 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12906 /* PARM is a generic generic parameter.
12907 Its DIE is a GNU extension. It shall have a
12908 DW_AT_name attribute to represent the name of the template template
12909 parameter, and a DW_AT_GNU_template_name attribute to represent the
12910 name of the template template argument. */
12911 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12912 parent_die, parm);
12913 else
12914 gcc_unreachable ();
12916 if (tmpl_die)
12918 tree tmpl_type;
12920 /* If PARM is a generic parameter pack, it means we are
12921 emitting debug info for a template argument pack element.
12922 In other terms, ARG is a template argument pack element.
12923 In that case, we don't emit any DW_AT_name attribute for
12924 the die. */
12925 if (emit_name_p)
12927 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12928 gcc_assert (name);
12929 add_AT_string (tmpl_die, DW_AT_name, name);
12932 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12934 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12935 TMPL_DIE should have a child DW_AT_type attribute that is set
12936 to the type of the argument to PARM, which is ARG.
12937 If PARM is a type generic parameter, TMPL_DIE should have a
12938 child DW_AT_type that is set to ARG. */
12939 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12940 add_type_attribute (tmpl_die, tmpl_type,
12941 (TREE_THIS_VOLATILE (tmpl_type)
12942 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12943 false, parent_die);
12945 else
12947 /* So TMPL_DIE is a DIE representing a
12948 a generic generic template parameter, a.k.a template template
12949 parameter in C++ and arg is a template. */
12951 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12952 to the name of the argument. */
12953 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12954 if (name)
12955 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12958 if (TREE_CODE (parm) == PARM_DECL)
12959 /* So PARM is a non-type generic parameter.
12960 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12961 attribute of TMPL_DIE which value represents the value
12962 of ARG.
12963 We must be careful here:
12964 The value of ARG might reference some function decls.
12965 We might currently be emitting debug info for a generic
12966 type and types are emitted before function decls, we don't
12967 know if the function decls referenced by ARG will actually be
12968 emitted after cgraph computations.
12969 So must defer the generation of the DW_AT_const_value to
12970 after cgraph is ready. */
12971 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12974 return tmpl_die;
12977 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12978 PARM_PACK must be a template parameter pack. The returned DIE
12979 will be child DIE of PARENT_DIE. */
12981 static dw_die_ref
12982 template_parameter_pack_die (tree parm_pack,
12983 tree parm_pack_args,
12984 dw_die_ref parent_die)
12986 dw_die_ref die;
12987 int j;
12989 gcc_assert (parent_die && parm_pack);
12991 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12992 add_name_and_src_coords_attributes (die, parm_pack);
12993 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12994 generic_parameter_die (parm_pack,
12995 TREE_VEC_ELT (parm_pack_args, j),
12996 false /* Don't emit DW_AT_name */,
12997 die);
12998 return die;
13001 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13002 an enumerated type. */
13004 static inline int
13005 type_is_enum (const_tree type)
13007 return TREE_CODE (type) == ENUMERAL_TYPE;
13010 /* Return the DBX register number described by a given RTL node. */
13012 static unsigned int
13013 dbx_reg_number (const_rtx rtl)
13015 unsigned regno = REGNO (rtl);
13017 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13019 #ifdef LEAF_REG_REMAP
13020 if (crtl->uses_only_leaf_regs)
13022 int leaf_reg = LEAF_REG_REMAP (regno);
13023 if (leaf_reg != -1)
13024 regno = (unsigned) leaf_reg;
13026 #endif
13028 regno = DBX_REGISTER_NUMBER (regno);
13029 gcc_assert (regno != INVALID_REGNUM);
13030 return regno;
13033 /* Optionally add a DW_OP_piece term to a location description expression.
13034 DW_OP_piece is only added if the location description expression already
13035 doesn't end with DW_OP_piece. */
13037 static void
13038 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13040 dw_loc_descr_ref loc;
13042 if (*list_head != NULL)
13044 /* Find the end of the chain. */
13045 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13048 if (loc->dw_loc_opc != DW_OP_piece)
13049 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13053 /* Return a location descriptor that designates a machine register or
13054 zero if there is none. */
13056 static dw_loc_descr_ref
13057 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13059 rtx regs;
13061 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13062 return 0;
13064 /* We only use "frame base" when we're sure we're talking about the
13065 post-prologue local stack frame. We do this by *not* running
13066 register elimination until this point, and recognizing the special
13067 argument pointer and soft frame pointer rtx's.
13068 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13069 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13070 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13072 dw_loc_descr_ref result = NULL;
13074 if (dwarf_version >= 4 || !dwarf_strict)
13076 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13077 initialized);
13078 if (result)
13079 add_loc_descr (&result,
13080 new_loc_descr (DW_OP_stack_value, 0, 0));
13082 return result;
13085 regs = targetm.dwarf_register_span (rtl);
13087 if (REG_NREGS (rtl) > 1 || regs)
13088 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13089 else
13091 unsigned int dbx_regnum = dbx_reg_number (rtl);
13092 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13093 return 0;
13094 return one_reg_loc_descriptor (dbx_regnum, initialized);
13098 /* Return a location descriptor that designates a machine register for
13099 a given hard register number. */
13101 static dw_loc_descr_ref
13102 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13104 dw_loc_descr_ref reg_loc_descr;
13106 if (regno <= 31)
13107 reg_loc_descr
13108 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13109 else
13110 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13112 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13113 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13115 return reg_loc_descr;
13118 /* Given an RTL of a register, return a location descriptor that
13119 designates a value that spans more than one register. */
13121 static dw_loc_descr_ref
13122 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13123 enum var_init_status initialized)
13125 int size, i;
13126 dw_loc_descr_ref loc_result = NULL;
13128 /* Simple, contiguous registers. */
13129 if (regs == NULL_RTX)
13131 unsigned reg = REGNO (rtl);
13132 int nregs;
13134 #ifdef LEAF_REG_REMAP
13135 if (crtl->uses_only_leaf_regs)
13137 int leaf_reg = LEAF_REG_REMAP (reg);
13138 if (leaf_reg != -1)
13139 reg = (unsigned) leaf_reg;
13141 #endif
13143 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13144 nregs = REG_NREGS (rtl);
13146 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13148 loc_result = NULL;
13149 while (nregs--)
13151 dw_loc_descr_ref t;
13153 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13154 VAR_INIT_STATUS_INITIALIZED);
13155 add_loc_descr (&loc_result, t);
13156 add_loc_descr_op_piece (&loc_result, size);
13157 ++reg;
13159 return loc_result;
13162 /* Now onto stupid register sets in non contiguous locations. */
13164 gcc_assert (GET_CODE (regs) == PARALLEL);
13166 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13167 loc_result = NULL;
13169 for (i = 0; i < XVECLEN (regs, 0); ++i)
13171 dw_loc_descr_ref t;
13173 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13174 VAR_INIT_STATUS_INITIALIZED);
13175 add_loc_descr (&loc_result, t);
13176 add_loc_descr_op_piece (&loc_result, size);
13179 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13180 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13181 return loc_result;
13184 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13186 /* Return a location descriptor that designates a constant i,
13187 as a compound operation from constant (i >> shift), constant shift
13188 and DW_OP_shl. */
13190 static dw_loc_descr_ref
13191 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13193 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13194 add_loc_descr (&ret, int_loc_descriptor (shift));
13195 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13196 return ret;
13199 /* Return a location descriptor that designates a constant. */
13201 static dw_loc_descr_ref
13202 int_loc_descriptor (HOST_WIDE_INT i)
13204 enum dwarf_location_atom op;
13206 /* Pick the smallest representation of a constant, rather than just
13207 defaulting to the LEB encoding. */
13208 if (i >= 0)
13210 int clz = clz_hwi (i);
13211 int ctz = ctz_hwi (i);
13212 if (i <= 31)
13213 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13214 else if (i <= 0xff)
13215 op = DW_OP_const1u;
13216 else if (i <= 0xffff)
13217 op = DW_OP_const2u;
13218 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13219 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13220 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13221 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13222 while DW_OP_const4u is 5 bytes. */
13223 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13224 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13225 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13226 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13227 while DW_OP_const4u is 5 bytes. */
13228 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13230 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13231 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13232 <= 4)
13234 /* As i >= 2**31, the double cast above will yield a negative number.
13235 Since wrapping is defined in DWARF expressions we can output big
13236 positive integers as small negative ones, regardless of the size
13237 of host wide ints.
13239 Here, since the evaluator will handle 32-bit values and since i >=
13240 2**31, we know it's going to be interpreted as a negative literal:
13241 store it this way if we can do better than 5 bytes this way. */
13242 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13244 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13245 op = DW_OP_const4u;
13247 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13248 least 6 bytes: see if we can do better before falling back to it. */
13249 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13250 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13251 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13252 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13253 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13254 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13255 >= HOST_BITS_PER_WIDE_INT)
13256 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13257 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13258 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13259 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13260 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13261 && size_of_uleb128 (i) > 6)
13262 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13263 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13264 else
13265 op = DW_OP_constu;
13267 else
13269 if (i >= -0x80)
13270 op = DW_OP_const1s;
13271 else if (i >= -0x8000)
13272 op = DW_OP_const2s;
13273 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13275 if (size_of_int_loc_descriptor (i) < 5)
13277 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13278 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13279 return ret;
13281 op = DW_OP_const4s;
13283 else
13285 if (size_of_int_loc_descriptor (i)
13286 < (unsigned long) 1 + size_of_sleb128 (i))
13288 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13289 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13290 return ret;
13292 op = DW_OP_consts;
13296 return new_loc_descr (op, i, 0);
13299 /* Likewise, for unsigned constants. */
13301 static dw_loc_descr_ref
13302 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13304 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13305 const unsigned HOST_WIDE_INT max_uint
13306 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13308 /* If possible, use the clever signed constants handling. */
13309 if (i <= max_int)
13310 return int_loc_descriptor ((HOST_WIDE_INT) i);
13312 /* Here, we are left with positive numbers that cannot be represented as
13313 HOST_WIDE_INT, i.e.:
13314 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13316 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13317 whereas may be better to output a negative integer: thanks to integer
13318 wrapping, we know that:
13319 x = x - 2 ** DWARF2_ADDR_SIZE
13320 = x - 2 * (max (HOST_WIDE_INT) + 1)
13321 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13322 small negative integers. Let's try that in cases it will clearly improve
13323 the encoding: there is no gain turning DW_OP_const4u into
13324 DW_OP_const4s. */
13325 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13326 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13327 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13329 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13331 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13332 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13333 const HOST_WIDE_INT second_shift
13334 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13336 /* So we finally have:
13337 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13338 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13339 return int_loc_descriptor (second_shift);
13342 /* Last chance: fallback to a simple constant operation. */
13343 return new_loc_descr
13344 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13345 ? DW_OP_const4u
13346 : DW_OP_const8u,
13347 i, 0);
13350 /* Generate and return a location description that computes the unsigned
13351 comparison of the two stack top entries (a OP b where b is the top-most
13352 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13353 LE_EXPR, GT_EXPR or GE_EXPR. */
13355 static dw_loc_descr_ref
13356 uint_comparison_loc_list (enum tree_code kind)
13358 enum dwarf_location_atom op, flip_op;
13359 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13361 switch (kind)
13363 case LT_EXPR:
13364 op = DW_OP_lt;
13365 break;
13366 case LE_EXPR:
13367 op = DW_OP_le;
13368 break;
13369 case GT_EXPR:
13370 op = DW_OP_gt;
13371 break;
13372 case GE_EXPR:
13373 op = DW_OP_ge;
13374 break;
13375 default:
13376 gcc_unreachable ();
13379 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13380 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13382 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13383 possible to perform unsigned comparisons: we just have to distinguish
13384 three cases:
13386 1. when a and b have the same sign (as signed integers); then we should
13387 return: a OP(signed) b;
13389 2. when a is a negative signed integer while b is a positive one, then a
13390 is a greater unsigned integer than b; likewise when a and b's roles
13391 are flipped.
13393 So first, compare the sign of the two operands. */
13394 ret = new_loc_descr (DW_OP_over, 0, 0);
13395 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13396 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13397 /* If they have different signs (i.e. they have different sign bits), then
13398 the stack top value has now the sign bit set and thus it's smaller than
13399 zero. */
13400 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13401 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13402 add_loc_descr (&ret, bra_node);
13404 /* We are in case 1. At this point, we know both operands have the same
13405 sign, to it's safe to use the built-in signed comparison. */
13406 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13407 add_loc_descr (&ret, jmp_node);
13409 /* We are in case 2. Here, we know both operands do not have the same sign,
13410 so we have to flip the signed comparison. */
13411 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13412 tmp = new_loc_descr (flip_op, 0, 0);
13413 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13414 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13415 add_loc_descr (&ret, tmp);
13417 /* This dummy operation is necessary to make the two branches join. */
13418 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13419 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13420 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13421 add_loc_descr (&ret, tmp);
13423 return ret;
13426 /* Likewise, but takes the location description lists (might be destructive on
13427 them). Return NULL if either is NULL or if concatenation fails. */
13429 static dw_loc_list_ref
13430 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13431 enum tree_code kind)
13433 if (left == NULL || right == NULL)
13434 return NULL;
13436 add_loc_list (&left, right);
13437 if (left == NULL)
13438 return NULL;
13440 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13441 return left;
13444 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13445 without actually allocating it. */
13447 static unsigned long
13448 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13450 return size_of_int_loc_descriptor (i >> shift)
13451 + size_of_int_loc_descriptor (shift)
13452 + 1;
13455 /* Return size_of_locs (int_loc_descriptor (i)) without
13456 actually allocating it. */
13458 static unsigned long
13459 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13461 unsigned long s;
13463 if (i >= 0)
13465 int clz, ctz;
13466 if (i <= 31)
13467 return 1;
13468 else if (i <= 0xff)
13469 return 2;
13470 else if (i <= 0xffff)
13471 return 3;
13472 clz = clz_hwi (i);
13473 ctz = ctz_hwi (i);
13474 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13475 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13476 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13477 - clz - 5);
13478 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13479 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13480 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13481 - clz - 8);
13482 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13483 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13484 <= 4)
13485 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13486 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13487 return 5;
13488 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13489 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13490 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13491 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13492 - clz - 8);
13493 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13494 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13495 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13496 - clz - 16);
13497 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13498 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13499 && s > 6)
13500 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13501 - clz - 32);
13502 else
13503 return 1 + s;
13505 else
13507 if (i >= -0x80)
13508 return 2;
13509 else if (i >= -0x8000)
13510 return 3;
13511 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13513 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13515 s = size_of_int_loc_descriptor (-i) + 1;
13516 if (s < 5)
13517 return s;
13519 return 5;
13521 else
13523 unsigned long r = 1 + size_of_sleb128 (i);
13524 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13526 s = size_of_int_loc_descriptor (-i) + 1;
13527 if (s < r)
13528 return s;
13530 return r;
13535 /* Return loc description representing "address" of integer value.
13536 This can appear only as toplevel expression. */
13538 static dw_loc_descr_ref
13539 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13541 int litsize;
13542 dw_loc_descr_ref loc_result = NULL;
13544 if (!(dwarf_version >= 4 || !dwarf_strict))
13545 return NULL;
13547 litsize = size_of_int_loc_descriptor (i);
13548 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13549 is more compact. For DW_OP_stack_value we need:
13550 litsize + 1 (DW_OP_stack_value)
13551 and for DW_OP_implicit_value:
13552 1 (DW_OP_implicit_value) + 1 (length) + size. */
13553 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13555 loc_result = int_loc_descriptor (i);
13556 add_loc_descr (&loc_result,
13557 new_loc_descr (DW_OP_stack_value, 0, 0));
13558 return loc_result;
13561 loc_result = new_loc_descr (DW_OP_implicit_value,
13562 size, 0);
13563 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13564 loc_result->dw_loc_oprnd2.v.val_int = i;
13565 return loc_result;
13568 /* Return a location descriptor that designates a base+offset location. */
13570 static dw_loc_descr_ref
13571 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13572 enum var_init_status initialized)
13574 unsigned int regno;
13575 dw_loc_descr_ref result;
13576 dw_fde_ref fde = cfun->fde;
13578 /* We only use "frame base" when we're sure we're talking about the
13579 post-prologue local stack frame. We do this by *not* running
13580 register elimination until this point, and recognizing the special
13581 argument pointer and soft frame pointer rtx's. */
13582 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13584 rtx elim = (ira_use_lra_p
13585 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13586 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13588 if (elim != reg)
13590 if (GET_CODE (elim) == PLUS)
13592 offset += INTVAL (XEXP (elim, 1));
13593 elim = XEXP (elim, 0);
13595 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13596 && (elim == hard_frame_pointer_rtx
13597 || elim == stack_pointer_rtx))
13598 || elim == (frame_pointer_needed
13599 ? hard_frame_pointer_rtx
13600 : stack_pointer_rtx));
13602 /* If drap register is used to align stack, use frame
13603 pointer + offset to access stack variables. If stack
13604 is aligned without drap, use stack pointer + offset to
13605 access stack variables. */
13606 if (crtl->stack_realign_tried
13607 && reg == frame_pointer_rtx)
13609 int base_reg
13610 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13611 ? HARD_FRAME_POINTER_REGNUM
13612 : REGNO (elim));
13613 return new_reg_loc_descr (base_reg, offset);
13616 gcc_assert (frame_pointer_fb_offset_valid);
13617 offset += frame_pointer_fb_offset;
13618 return new_loc_descr (DW_OP_fbreg, offset, 0);
13622 regno = REGNO (reg);
13623 #ifdef LEAF_REG_REMAP
13624 if (crtl->uses_only_leaf_regs)
13626 int leaf_reg = LEAF_REG_REMAP (regno);
13627 if (leaf_reg != -1)
13628 regno = (unsigned) leaf_reg;
13630 #endif
13631 regno = DWARF_FRAME_REGNUM (regno);
13633 if (!optimize && fde
13634 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13636 /* Use cfa+offset to represent the location of arguments passed
13637 on the stack when drap is used to align stack.
13638 Only do this when not optimizing, for optimized code var-tracking
13639 is supposed to track where the arguments live and the register
13640 used as vdrap or drap in some spot might be used for something
13641 else in other part of the routine. */
13642 return new_loc_descr (DW_OP_fbreg, offset, 0);
13645 if (regno <= 31)
13646 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13647 offset, 0);
13648 else
13649 result = new_loc_descr (DW_OP_bregx, regno, offset);
13651 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13652 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13654 return result;
13657 /* Return true if this RTL expression describes a base+offset calculation. */
13659 static inline int
13660 is_based_loc (const_rtx rtl)
13662 return (GET_CODE (rtl) == PLUS
13663 && ((REG_P (XEXP (rtl, 0))
13664 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13665 && CONST_INT_P (XEXP (rtl, 1)))));
13668 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13669 failed. */
13671 static dw_loc_descr_ref
13672 tls_mem_loc_descriptor (rtx mem)
13674 tree base;
13675 dw_loc_descr_ref loc_result;
13677 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13678 return NULL;
13680 base = get_base_address (MEM_EXPR (mem));
13681 if (base == NULL
13682 || !VAR_P (base)
13683 || !DECL_THREAD_LOCAL_P (base))
13684 return NULL;
13686 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13687 if (loc_result == NULL)
13688 return NULL;
13690 if (MEM_OFFSET (mem))
13691 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13693 return loc_result;
13696 /* Output debug info about reason why we failed to expand expression as dwarf
13697 expression. */
13699 static void
13700 expansion_failed (tree expr, rtx rtl, char const *reason)
13702 if (dump_file && (dump_flags & TDF_DETAILS))
13704 fprintf (dump_file, "Failed to expand as dwarf: ");
13705 if (expr)
13706 print_generic_expr (dump_file, expr, dump_flags);
13707 if (rtl)
13709 fprintf (dump_file, "\n");
13710 print_rtl (dump_file, rtl);
13712 fprintf (dump_file, "\nReason: %s\n", reason);
13716 /* Helper function for const_ok_for_output. */
13718 static bool
13719 const_ok_for_output_1 (rtx rtl)
13721 if (GET_CODE (rtl) == UNSPEC)
13723 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13724 we can't express it in the debug info. */
13725 /* Don't complain about TLS UNSPECs, those are just too hard to
13726 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13727 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13728 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13729 if (flag_checking
13730 && (XVECLEN (rtl, 0) == 0
13731 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13732 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13733 inform (current_function_decl
13734 ? DECL_SOURCE_LOCATION (current_function_decl)
13735 : UNKNOWN_LOCATION,
13736 #if NUM_UNSPEC_VALUES > 0
13737 "non-delegitimized UNSPEC %s (%d) found in variable location",
13738 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13739 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13740 XINT (rtl, 1));
13741 #else
13742 "non-delegitimized UNSPEC %d found in variable location",
13743 XINT (rtl, 1));
13744 #endif
13745 expansion_failed (NULL_TREE, rtl,
13746 "UNSPEC hasn't been delegitimized.\n");
13747 return false;
13750 if (targetm.const_not_ok_for_debug_p (rtl))
13752 expansion_failed (NULL_TREE, rtl,
13753 "Expression rejected for debug by the backend.\n");
13754 return false;
13757 /* FIXME: Refer to PR60655. It is possible for simplification
13758 of rtl expressions in var tracking to produce such expressions.
13759 We should really identify / validate expressions
13760 enclosed in CONST that can be handled by assemblers on various
13761 targets and only handle legitimate cases here. */
13762 if (GET_CODE (rtl) != SYMBOL_REF)
13764 if (GET_CODE (rtl) == NOT)
13765 return false;
13766 return true;
13769 if (CONSTANT_POOL_ADDRESS_P (rtl))
13771 bool marked;
13772 get_pool_constant_mark (rtl, &marked);
13773 /* If all references to this pool constant were optimized away,
13774 it was not output and thus we can't represent it. */
13775 if (!marked)
13777 expansion_failed (NULL_TREE, rtl,
13778 "Constant was removed from constant pool.\n");
13779 return false;
13783 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13784 return false;
13786 /* Avoid references to external symbols in debug info, on several targets
13787 the linker might even refuse to link when linking a shared library,
13788 and in many other cases the relocations for .debug_info/.debug_loc are
13789 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13790 to be defined within the same shared library or executable are fine. */
13791 if (SYMBOL_REF_EXTERNAL_P (rtl))
13793 tree decl = SYMBOL_REF_DECL (rtl);
13795 if (decl == NULL || !targetm.binds_local_p (decl))
13797 expansion_failed (NULL_TREE, rtl,
13798 "Symbol not defined in current TU.\n");
13799 return false;
13803 return true;
13806 /* Return true if constant RTL can be emitted in DW_OP_addr or
13807 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13808 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13810 static bool
13811 const_ok_for_output (rtx rtl)
13813 if (GET_CODE (rtl) == SYMBOL_REF)
13814 return const_ok_for_output_1 (rtl);
13816 if (GET_CODE (rtl) == CONST)
13818 subrtx_var_iterator::array_type array;
13819 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13820 if (!const_ok_for_output_1 (*iter))
13821 return false;
13822 return true;
13825 return true;
13828 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13829 if possible, NULL otherwise. */
13831 static dw_die_ref
13832 base_type_for_mode (machine_mode mode, bool unsignedp)
13834 dw_die_ref type_die;
13835 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13837 if (type == NULL)
13838 return NULL;
13839 switch (TREE_CODE (type))
13841 case INTEGER_TYPE:
13842 case REAL_TYPE:
13843 break;
13844 default:
13845 return NULL;
13847 type_die = lookup_type_die (type);
13848 if (!type_die)
13849 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13850 comp_unit_die ());
13851 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13852 return NULL;
13853 return type_die;
13856 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13857 type matching MODE, or, if MODE is narrower than or as wide as
13858 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13859 possible. */
13861 static dw_loc_descr_ref
13862 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13864 machine_mode outer_mode = mode;
13865 dw_die_ref type_die;
13866 dw_loc_descr_ref cvt;
13868 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13870 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13871 return op;
13873 type_die = base_type_for_mode (outer_mode, 1);
13874 if (type_die == NULL)
13875 return NULL;
13876 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13877 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13878 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13879 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13880 add_loc_descr (&op, cvt);
13881 return op;
13884 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13886 static dw_loc_descr_ref
13887 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13888 dw_loc_descr_ref op1)
13890 dw_loc_descr_ref ret = op0;
13891 add_loc_descr (&ret, op1);
13892 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13893 if (STORE_FLAG_VALUE != 1)
13895 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13896 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13898 return ret;
13901 /* Subroutine of scompare_loc_descriptor for the case in which we're
13902 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13903 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13905 static dw_loc_descr_ref
13906 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13907 scalar_int_mode op_mode,
13908 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13910 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13911 dw_loc_descr_ref cvt;
13913 if (type_die == NULL)
13914 return NULL;
13915 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13916 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13917 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13918 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13919 add_loc_descr (&op0, cvt);
13920 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13921 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13922 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13923 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13924 add_loc_descr (&op1, cvt);
13925 return compare_loc_descriptor (op, op0, op1);
13928 /* Subroutine of scompare_loc_descriptor for the case in which we're
13929 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13930 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13932 static dw_loc_descr_ref
13933 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13934 scalar_int_mode op_mode,
13935 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13937 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13938 /* For eq/ne, if the operands are known to be zero-extended,
13939 there is no need to do the fancy shifting up. */
13940 if (op == DW_OP_eq || op == DW_OP_ne)
13942 dw_loc_descr_ref last0, last1;
13943 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13945 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13947 /* deref_size zero extends, and for constants we can check
13948 whether they are zero extended or not. */
13949 if (((last0->dw_loc_opc == DW_OP_deref_size
13950 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13951 || (CONST_INT_P (XEXP (rtl, 0))
13952 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13953 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13954 && ((last1->dw_loc_opc == DW_OP_deref_size
13955 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13956 || (CONST_INT_P (XEXP (rtl, 1))
13957 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13958 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13959 return compare_loc_descriptor (op, op0, op1);
13961 /* EQ/NE comparison against constant in narrower type than
13962 DWARF2_ADDR_SIZE can be performed either as
13963 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13964 DW_OP_{eq,ne}
13966 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13967 DW_OP_{eq,ne}. Pick whatever is shorter. */
13968 if (CONST_INT_P (XEXP (rtl, 1))
13969 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13970 && (size_of_int_loc_descriptor (shift) + 1
13971 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13972 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13973 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13974 & GET_MODE_MASK (op_mode))))
13976 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13977 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13978 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13979 & GET_MODE_MASK (op_mode));
13980 return compare_loc_descriptor (op, op0, op1);
13983 add_loc_descr (&op0, int_loc_descriptor (shift));
13984 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13985 if (CONST_INT_P (XEXP (rtl, 1)))
13986 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13987 else
13989 add_loc_descr (&op1, int_loc_descriptor (shift));
13990 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13992 return compare_loc_descriptor (op, op0, op1);
13995 /* Return location descriptor for unsigned comparison OP RTL. */
13997 static dw_loc_descr_ref
13998 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13999 machine_mode mem_mode)
14001 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14002 dw_loc_descr_ref op0, op1;
14004 if (op_mode == VOIDmode)
14005 op_mode = GET_MODE (XEXP (rtl, 1));
14006 if (op_mode == VOIDmode)
14007 return NULL;
14009 scalar_int_mode int_op_mode;
14010 if (dwarf_strict
14011 && dwarf_version < 5
14012 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14013 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14014 return NULL;
14016 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14017 VAR_INIT_STATUS_INITIALIZED);
14018 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14019 VAR_INIT_STATUS_INITIALIZED);
14021 if (op0 == NULL || op1 == NULL)
14022 return NULL;
14024 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14026 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14027 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14029 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14030 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14032 return compare_loc_descriptor (op, op0, op1);
14035 /* Return location descriptor for unsigned comparison OP RTL. */
14037 static dw_loc_descr_ref
14038 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14039 machine_mode mem_mode)
14041 dw_loc_descr_ref op0, op1;
14043 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14044 if (test_op_mode == VOIDmode)
14045 test_op_mode = GET_MODE (XEXP (rtl, 1));
14047 scalar_int_mode op_mode;
14048 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14049 return NULL;
14051 if (dwarf_strict
14052 && dwarf_version < 5
14053 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14054 return NULL;
14056 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14057 VAR_INIT_STATUS_INITIALIZED);
14058 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14059 VAR_INIT_STATUS_INITIALIZED);
14061 if (op0 == NULL || op1 == NULL)
14062 return NULL;
14064 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14066 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14067 dw_loc_descr_ref last0, last1;
14068 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14070 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14072 if (CONST_INT_P (XEXP (rtl, 0)))
14073 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14074 /* deref_size zero extends, so no need to mask it again. */
14075 else if (last0->dw_loc_opc != DW_OP_deref_size
14076 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14078 add_loc_descr (&op0, int_loc_descriptor (mask));
14079 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14081 if (CONST_INT_P (XEXP (rtl, 1)))
14082 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14083 /* deref_size zero extends, so no need to mask it again. */
14084 else if (last1->dw_loc_opc != DW_OP_deref_size
14085 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14087 add_loc_descr (&op1, int_loc_descriptor (mask));
14088 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14091 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14093 HOST_WIDE_INT bias = 1;
14094 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14095 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14096 if (CONST_INT_P (XEXP (rtl, 1)))
14097 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14098 + INTVAL (XEXP (rtl, 1)));
14099 else
14100 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14101 bias, 0));
14103 return compare_loc_descriptor (op, op0, op1);
14106 /* Return location descriptor for {U,S}{MIN,MAX}. */
14108 static dw_loc_descr_ref
14109 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14110 machine_mode mem_mode)
14112 enum dwarf_location_atom op;
14113 dw_loc_descr_ref op0, op1, ret;
14114 dw_loc_descr_ref bra_node, drop_node;
14116 scalar_int_mode int_mode;
14117 if (dwarf_strict
14118 && dwarf_version < 5
14119 && (!is_a <scalar_int_mode> (mode, &int_mode)
14120 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14121 return NULL;
14123 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14124 VAR_INIT_STATUS_INITIALIZED);
14125 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14126 VAR_INIT_STATUS_INITIALIZED);
14128 if (op0 == NULL || op1 == NULL)
14129 return NULL;
14131 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14132 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14133 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14134 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14136 /* Checked by the caller. */
14137 int_mode = as_a <scalar_int_mode> (mode);
14138 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14140 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14141 add_loc_descr (&op0, int_loc_descriptor (mask));
14142 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14143 add_loc_descr (&op1, int_loc_descriptor (mask));
14144 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14146 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14148 HOST_WIDE_INT bias = 1;
14149 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14150 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14151 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14154 else if (is_a <scalar_int_mode> (mode, &int_mode)
14155 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14157 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14158 add_loc_descr (&op0, int_loc_descriptor (shift));
14159 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14160 add_loc_descr (&op1, int_loc_descriptor (shift));
14161 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14163 else if (is_a <scalar_int_mode> (mode, &int_mode)
14164 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14166 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14167 dw_loc_descr_ref cvt;
14168 if (type_die == NULL)
14169 return NULL;
14170 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14171 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14172 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14173 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14174 add_loc_descr (&op0, cvt);
14175 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14176 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14177 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14178 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14179 add_loc_descr (&op1, cvt);
14182 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14183 op = DW_OP_lt;
14184 else
14185 op = DW_OP_gt;
14186 ret = op0;
14187 add_loc_descr (&ret, op1);
14188 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14189 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14190 add_loc_descr (&ret, bra_node);
14191 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14192 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14193 add_loc_descr (&ret, drop_node);
14194 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14195 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14196 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14197 && is_a <scalar_int_mode> (mode, &int_mode)
14198 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14199 ret = convert_descriptor_to_mode (int_mode, ret);
14200 return ret;
14203 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14204 but after converting arguments to type_die, afterwards
14205 convert back to unsigned. */
14207 static dw_loc_descr_ref
14208 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14209 scalar_int_mode mode, machine_mode mem_mode)
14211 dw_loc_descr_ref cvt, op0, op1;
14213 if (type_die == NULL)
14214 return NULL;
14215 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14216 VAR_INIT_STATUS_INITIALIZED);
14217 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14218 VAR_INIT_STATUS_INITIALIZED);
14219 if (op0 == NULL || op1 == NULL)
14220 return NULL;
14221 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14222 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14223 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14224 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14225 add_loc_descr (&op0, cvt);
14226 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14227 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14228 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14229 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14230 add_loc_descr (&op1, cvt);
14231 add_loc_descr (&op0, op1);
14232 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14233 return convert_descriptor_to_mode (mode, op0);
14236 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14237 const0 is DW_OP_lit0 or corresponding typed constant,
14238 const1 is DW_OP_lit1 or corresponding typed constant
14239 and constMSB is constant with just the MSB bit set
14240 for the mode):
14241 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14242 L1: const0 DW_OP_swap
14243 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14244 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14245 L3: DW_OP_drop
14246 L4: DW_OP_nop
14248 CTZ is similar:
14249 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14250 L1: const0 DW_OP_swap
14251 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14252 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14253 L3: DW_OP_drop
14254 L4: DW_OP_nop
14256 FFS is similar:
14257 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14258 L1: const1 DW_OP_swap
14259 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14260 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14261 L3: DW_OP_drop
14262 L4: DW_OP_nop */
14264 static dw_loc_descr_ref
14265 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14266 machine_mode mem_mode)
14268 dw_loc_descr_ref op0, ret, tmp;
14269 HOST_WIDE_INT valv;
14270 dw_loc_descr_ref l1jump, l1label;
14271 dw_loc_descr_ref l2jump, l2label;
14272 dw_loc_descr_ref l3jump, l3label;
14273 dw_loc_descr_ref l4jump, l4label;
14274 rtx msb;
14276 if (GET_MODE (XEXP (rtl, 0)) != mode)
14277 return NULL;
14279 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14280 VAR_INIT_STATUS_INITIALIZED);
14281 if (op0 == NULL)
14282 return NULL;
14283 ret = op0;
14284 if (GET_CODE (rtl) == CLZ)
14286 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14287 valv = GET_MODE_BITSIZE (mode);
14289 else if (GET_CODE (rtl) == FFS)
14290 valv = 0;
14291 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14292 valv = GET_MODE_BITSIZE (mode);
14293 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14294 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14295 add_loc_descr (&ret, l1jump);
14296 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14297 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14298 VAR_INIT_STATUS_INITIALIZED);
14299 if (tmp == NULL)
14300 return NULL;
14301 add_loc_descr (&ret, tmp);
14302 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14303 add_loc_descr (&ret, l4jump);
14304 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14305 ? const1_rtx : const0_rtx,
14306 mode, mem_mode,
14307 VAR_INIT_STATUS_INITIALIZED);
14308 if (l1label == NULL)
14309 return NULL;
14310 add_loc_descr (&ret, l1label);
14311 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14312 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14313 add_loc_descr (&ret, l2label);
14314 if (GET_CODE (rtl) != CLZ)
14315 msb = const1_rtx;
14316 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14317 msb = GEN_INT (HOST_WIDE_INT_1U
14318 << (GET_MODE_BITSIZE (mode) - 1));
14319 else
14320 msb = immed_wide_int_const
14321 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14322 GET_MODE_PRECISION (mode)), mode);
14323 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14324 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14325 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14326 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14327 else
14328 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14329 VAR_INIT_STATUS_INITIALIZED);
14330 if (tmp == NULL)
14331 return NULL;
14332 add_loc_descr (&ret, tmp);
14333 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14334 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14335 add_loc_descr (&ret, l3jump);
14336 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14337 VAR_INIT_STATUS_INITIALIZED);
14338 if (tmp == NULL)
14339 return NULL;
14340 add_loc_descr (&ret, tmp);
14341 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14342 ? DW_OP_shl : DW_OP_shr, 0, 0));
14343 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14344 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14345 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14346 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14347 add_loc_descr (&ret, l2jump);
14348 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14349 add_loc_descr (&ret, l3label);
14350 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14351 add_loc_descr (&ret, l4label);
14352 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14353 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14354 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14355 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14356 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14357 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14358 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14359 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14360 return ret;
14363 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14364 const1 is DW_OP_lit1 or corresponding typed constant):
14365 const0 DW_OP_swap
14366 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14367 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14368 L2: DW_OP_drop
14370 PARITY is similar:
14371 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14372 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14373 L2: DW_OP_drop */
14375 static dw_loc_descr_ref
14376 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14377 machine_mode mem_mode)
14379 dw_loc_descr_ref op0, ret, tmp;
14380 dw_loc_descr_ref l1jump, l1label;
14381 dw_loc_descr_ref l2jump, l2label;
14383 if (GET_MODE (XEXP (rtl, 0)) != mode)
14384 return NULL;
14386 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14387 VAR_INIT_STATUS_INITIALIZED);
14388 if (op0 == NULL)
14389 return NULL;
14390 ret = op0;
14391 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14392 VAR_INIT_STATUS_INITIALIZED);
14393 if (tmp == NULL)
14394 return NULL;
14395 add_loc_descr (&ret, tmp);
14396 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14397 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14398 add_loc_descr (&ret, l1label);
14399 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14400 add_loc_descr (&ret, l2jump);
14401 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14403 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14404 VAR_INIT_STATUS_INITIALIZED);
14405 if (tmp == NULL)
14406 return NULL;
14407 add_loc_descr (&ret, tmp);
14408 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14409 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14410 ? DW_OP_plus : DW_OP_xor, 0, 0));
14411 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14412 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14413 VAR_INIT_STATUS_INITIALIZED);
14414 add_loc_descr (&ret, tmp);
14415 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14416 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14417 add_loc_descr (&ret, l1jump);
14418 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14419 add_loc_descr (&ret, l2label);
14420 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14421 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14422 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14423 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14424 return ret;
14427 /* BSWAP (constS is initial shift count, either 56 or 24):
14428 constS const0
14429 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14430 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14431 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14432 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14433 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14435 static dw_loc_descr_ref
14436 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14437 machine_mode mem_mode)
14439 dw_loc_descr_ref op0, ret, tmp;
14440 dw_loc_descr_ref l1jump, l1label;
14441 dw_loc_descr_ref l2jump, l2label;
14443 if (BITS_PER_UNIT != 8
14444 || (GET_MODE_BITSIZE (mode) != 32
14445 && GET_MODE_BITSIZE (mode) != 64))
14446 return NULL;
14448 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14449 VAR_INIT_STATUS_INITIALIZED);
14450 if (op0 == NULL)
14451 return NULL;
14453 ret = op0;
14454 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14455 mode, mem_mode,
14456 VAR_INIT_STATUS_INITIALIZED);
14457 if (tmp == NULL)
14458 return NULL;
14459 add_loc_descr (&ret, tmp);
14460 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14461 VAR_INIT_STATUS_INITIALIZED);
14462 if (tmp == NULL)
14463 return NULL;
14464 add_loc_descr (&ret, tmp);
14465 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14466 add_loc_descr (&ret, l1label);
14467 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14468 mode, mem_mode,
14469 VAR_INIT_STATUS_INITIALIZED);
14470 add_loc_descr (&ret, tmp);
14471 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14472 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14473 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14474 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14475 VAR_INIT_STATUS_INITIALIZED);
14476 if (tmp == NULL)
14477 return NULL;
14478 add_loc_descr (&ret, tmp);
14479 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14480 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14481 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14482 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14483 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14484 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14485 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14486 VAR_INIT_STATUS_INITIALIZED);
14487 add_loc_descr (&ret, tmp);
14488 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14489 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14490 add_loc_descr (&ret, l2jump);
14491 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14492 VAR_INIT_STATUS_INITIALIZED);
14493 add_loc_descr (&ret, tmp);
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14495 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14496 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14497 add_loc_descr (&ret, l1jump);
14498 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14499 add_loc_descr (&ret, l2label);
14500 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14502 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14503 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14504 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14505 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14506 return ret;
14509 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14510 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14511 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14512 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14514 ROTATERT is similar:
14515 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14516 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14517 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14519 static dw_loc_descr_ref
14520 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14521 machine_mode mem_mode)
14523 rtx rtlop1 = XEXP (rtl, 1);
14524 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14525 int i;
14527 if (GET_MODE (rtlop1) != VOIDmode
14528 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14529 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14530 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14531 VAR_INIT_STATUS_INITIALIZED);
14532 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14533 VAR_INIT_STATUS_INITIALIZED);
14534 if (op0 == NULL || op1 == NULL)
14535 return NULL;
14536 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14537 for (i = 0; i < 2; i++)
14539 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14540 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14541 mode, mem_mode,
14542 VAR_INIT_STATUS_INITIALIZED);
14543 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14544 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14545 ? DW_OP_const4u
14546 : HOST_BITS_PER_WIDE_INT == 64
14547 ? DW_OP_const8u : DW_OP_constu,
14548 GET_MODE_MASK (mode), 0);
14549 else
14550 mask[i] = NULL;
14551 if (mask[i] == NULL)
14552 return NULL;
14553 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14555 ret = op0;
14556 add_loc_descr (&ret, op1);
14557 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14558 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14559 if (GET_CODE (rtl) == ROTATERT)
14561 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14562 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14563 GET_MODE_BITSIZE (mode), 0));
14565 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14566 if (mask[0] != NULL)
14567 add_loc_descr (&ret, mask[0]);
14568 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14569 if (mask[1] != NULL)
14571 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14572 add_loc_descr (&ret, mask[1]);
14573 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14575 if (GET_CODE (rtl) == ROTATE)
14577 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14578 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14579 GET_MODE_BITSIZE (mode), 0));
14581 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14582 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14583 return ret;
14586 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14587 for DEBUG_PARAMETER_REF RTL. */
14589 static dw_loc_descr_ref
14590 parameter_ref_descriptor (rtx rtl)
14592 dw_loc_descr_ref ret;
14593 dw_die_ref ref;
14595 if (dwarf_strict)
14596 return NULL;
14597 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14598 /* With LTO during LTRANS we get the late DIE that refers to the early
14599 DIE, thus we add another indirection here. This seems to confuse
14600 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14601 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14602 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14603 if (ref)
14605 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14606 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14607 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14609 else
14611 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14612 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14614 return ret;
14617 /* The following routine converts the RTL for a variable or parameter
14618 (resident in memory) into an equivalent Dwarf representation of a
14619 mechanism for getting the address of that same variable onto the top of a
14620 hypothetical "address evaluation" stack.
14622 When creating memory location descriptors, we are effectively transforming
14623 the RTL for a memory-resident object into its Dwarf postfix expression
14624 equivalent. This routine recursively descends an RTL tree, turning
14625 it into Dwarf postfix code as it goes.
14627 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14629 MEM_MODE is the mode of the memory reference, needed to handle some
14630 autoincrement addressing modes.
14632 Return 0 if we can't represent the location. */
14634 dw_loc_descr_ref
14635 mem_loc_descriptor (rtx rtl, machine_mode mode,
14636 machine_mode mem_mode,
14637 enum var_init_status initialized)
14639 dw_loc_descr_ref mem_loc_result = NULL;
14640 enum dwarf_location_atom op;
14641 dw_loc_descr_ref op0, op1;
14642 rtx inner = NULL_RTX;
14644 if (mode == VOIDmode)
14645 mode = GET_MODE (rtl);
14647 /* Note that for a dynamically sized array, the location we will generate a
14648 description of here will be the lowest numbered location which is
14649 actually within the array. That's *not* necessarily the same as the
14650 zeroth element of the array. */
14652 rtl = targetm.delegitimize_address (rtl);
14654 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14655 return NULL;
14657 scalar_int_mode int_mode, inner_mode, op1_mode;
14658 switch (GET_CODE (rtl))
14660 case POST_INC:
14661 case POST_DEC:
14662 case POST_MODIFY:
14663 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14665 case SUBREG:
14666 /* The case of a subreg may arise when we have a local (register)
14667 variable or a formal (register) parameter which doesn't quite fill
14668 up an entire register. For now, just assume that it is
14669 legitimate to make the Dwarf info refer to the whole register which
14670 contains the given subreg. */
14671 if (!subreg_lowpart_p (rtl))
14672 break;
14673 inner = SUBREG_REG (rtl);
14674 /* FALLTHRU */
14675 case TRUNCATE:
14676 if (inner == NULL_RTX)
14677 inner = XEXP (rtl, 0);
14678 if (is_a <scalar_int_mode> (mode, &int_mode)
14679 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14680 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14681 #ifdef POINTERS_EXTEND_UNSIGNED
14682 || (int_mode == Pmode && mem_mode != VOIDmode)
14683 #endif
14685 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14687 mem_loc_result = mem_loc_descriptor (inner,
14688 inner_mode,
14689 mem_mode, initialized);
14690 break;
14692 if (dwarf_strict && dwarf_version < 5)
14693 break;
14694 if (is_a <scalar_int_mode> (mode, &int_mode)
14695 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14696 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14697 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14699 dw_die_ref type_die;
14700 dw_loc_descr_ref cvt;
14702 mem_loc_result = mem_loc_descriptor (inner,
14703 GET_MODE (inner),
14704 mem_mode, initialized);
14705 if (mem_loc_result == NULL)
14706 break;
14707 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14708 if (type_die == NULL)
14710 mem_loc_result = NULL;
14711 break;
14713 if (GET_MODE_SIZE (mode)
14714 != GET_MODE_SIZE (GET_MODE (inner)))
14715 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14716 else
14717 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14718 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14719 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14720 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14721 add_loc_descr (&mem_loc_result, cvt);
14722 if (is_a <scalar_int_mode> (mode, &int_mode)
14723 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14725 /* Convert it to untyped afterwards. */
14726 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14727 add_loc_descr (&mem_loc_result, cvt);
14730 break;
14732 case REG:
14733 if (!is_a <scalar_int_mode> (mode, &int_mode)
14734 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14735 && rtl != arg_pointer_rtx
14736 && rtl != frame_pointer_rtx
14737 #ifdef POINTERS_EXTEND_UNSIGNED
14738 && (int_mode != Pmode || mem_mode == VOIDmode)
14739 #endif
14742 dw_die_ref type_die;
14743 unsigned int dbx_regnum;
14745 if (dwarf_strict && dwarf_version < 5)
14746 break;
14747 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14748 break;
14749 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14750 if (type_die == NULL)
14751 break;
14753 dbx_regnum = dbx_reg_number (rtl);
14754 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14755 break;
14756 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14757 dbx_regnum, 0);
14758 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14759 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14760 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14761 break;
14763 /* Whenever a register number forms a part of the description of the
14764 method for calculating the (dynamic) address of a memory resident
14765 object, DWARF rules require the register number be referred to as
14766 a "base register". This distinction is not based in any way upon
14767 what category of register the hardware believes the given register
14768 belongs to. This is strictly DWARF terminology we're dealing with
14769 here. Note that in cases where the location of a memory-resident
14770 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14771 OP_CONST (0)) the actual DWARF location descriptor that we generate
14772 may just be OP_BASEREG (basereg). This may look deceptively like
14773 the object in question was allocated to a register (rather than in
14774 memory) so DWARF consumers need to be aware of the subtle
14775 distinction between OP_REG and OP_BASEREG. */
14776 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14777 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14778 else if (stack_realign_drap
14779 && crtl->drap_reg
14780 && crtl->args.internal_arg_pointer == rtl
14781 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14783 /* If RTL is internal_arg_pointer, which has been optimized
14784 out, use DRAP instead. */
14785 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14786 VAR_INIT_STATUS_INITIALIZED);
14788 break;
14790 case SIGN_EXTEND:
14791 case ZERO_EXTEND:
14792 if (!is_a <scalar_int_mode> (mode, &int_mode)
14793 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14794 break;
14795 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14796 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14797 if (op0 == 0)
14798 break;
14799 else if (GET_CODE (rtl) == ZERO_EXTEND
14800 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14801 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14802 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14803 to expand zero extend as two shifts instead of
14804 masking. */
14805 && GET_MODE_SIZE (inner_mode) <= 4)
14807 mem_loc_result = op0;
14808 add_loc_descr (&mem_loc_result,
14809 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14810 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14812 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14814 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14815 shift *= BITS_PER_UNIT;
14816 if (GET_CODE (rtl) == SIGN_EXTEND)
14817 op = DW_OP_shra;
14818 else
14819 op = DW_OP_shr;
14820 mem_loc_result = op0;
14821 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14822 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14823 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14824 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14826 else if (!dwarf_strict || dwarf_version >= 5)
14828 dw_die_ref type_die1, type_die2;
14829 dw_loc_descr_ref cvt;
14831 type_die1 = base_type_for_mode (inner_mode,
14832 GET_CODE (rtl) == ZERO_EXTEND);
14833 if (type_die1 == NULL)
14834 break;
14835 type_die2 = base_type_for_mode (int_mode, 1);
14836 if (type_die2 == NULL)
14837 break;
14838 mem_loc_result = op0;
14839 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14840 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14841 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14842 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14843 add_loc_descr (&mem_loc_result, cvt);
14844 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14845 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14846 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14847 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14848 add_loc_descr (&mem_loc_result, cvt);
14850 break;
14852 case MEM:
14854 rtx new_rtl = avoid_constant_pool_reference (rtl);
14855 if (new_rtl != rtl)
14857 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14858 initialized);
14859 if (mem_loc_result != NULL)
14860 return mem_loc_result;
14863 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14864 get_address_mode (rtl), mode,
14865 VAR_INIT_STATUS_INITIALIZED);
14866 if (mem_loc_result == NULL)
14867 mem_loc_result = tls_mem_loc_descriptor (rtl);
14868 if (mem_loc_result != NULL)
14870 if (!is_a <scalar_int_mode> (mode, &int_mode)
14871 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14873 dw_die_ref type_die;
14874 dw_loc_descr_ref deref;
14876 if (dwarf_strict && dwarf_version < 5)
14877 return NULL;
14878 type_die
14879 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14880 if (type_die == NULL)
14881 return NULL;
14882 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14883 GET_MODE_SIZE (mode), 0);
14884 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14885 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14886 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14887 add_loc_descr (&mem_loc_result, deref);
14889 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14890 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14891 else
14892 add_loc_descr (&mem_loc_result,
14893 new_loc_descr (DW_OP_deref_size,
14894 GET_MODE_SIZE (int_mode), 0));
14896 break;
14898 case LO_SUM:
14899 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14901 case LABEL_REF:
14902 /* Some ports can transform a symbol ref into a label ref, because
14903 the symbol ref is too far away and has to be dumped into a constant
14904 pool. */
14905 case CONST:
14906 case SYMBOL_REF:
14907 if (!is_a <scalar_int_mode> (mode, &int_mode)
14908 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14909 #ifdef POINTERS_EXTEND_UNSIGNED
14910 && (int_mode != Pmode || mem_mode == VOIDmode)
14911 #endif
14913 break;
14914 if (GET_CODE (rtl) == SYMBOL_REF
14915 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14917 dw_loc_descr_ref temp;
14919 /* If this is not defined, we have no way to emit the data. */
14920 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14921 break;
14923 temp = new_addr_loc_descr (rtl, dtprel_true);
14925 /* We check for DWARF 5 here because gdb did not implement
14926 DW_OP_form_tls_address until after 7.12. */
14927 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14928 ? DW_OP_form_tls_address
14929 : DW_OP_GNU_push_tls_address),
14930 0, 0);
14931 add_loc_descr (&mem_loc_result, temp);
14933 break;
14936 if (!const_ok_for_output (rtl))
14938 if (GET_CODE (rtl) == CONST)
14939 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14940 mem_mode, initialized);
14941 break;
14944 symref:
14945 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14946 vec_safe_push (used_rtx_array, rtl);
14947 break;
14949 case CONCAT:
14950 case CONCATN:
14951 case VAR_LOCATION:
14952 case DEBUG_IMPLICIT_PTR:
14953 expansion_failed (NULL_TREE, rtl,
14954 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14955 return 0;
14957 case ENTRY_VALUE:
14958 if (dwarf_strict && dwarf_version < 5)
14959 return NULL;
14960 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14962 if (!is_a <scalar_int_mode> (mode, &int_mode)
14963 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14964 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14965 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14966 else
14968 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14969 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14970 return NULL;
14971 op0 = one_reg_loc_descriptor (dbx_regnum,
14972 VAR_INIT_STATUS_INITIALIZED);
14975 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14976 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14978 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14979 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14980 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14981 return NULL;
14983 else
14984 gcc_unreachable ();
14985 if (op0 == NULL)
14986 return NULL;
14987 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14988 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14989 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14990 break;
14992 case DEBUG_PARAMETER_REF:
14993 mem_loc_result = parameter_ref_descriptor (rtl);
14994 break;
14996 case PRE_MODIFY:
14997 /* Extract the PLUS expression nested inside and fall into
14998 PLUS code below. */
14999 rtl = XEXP (rtl, 1);
15000 goto plus;
15002 case PRE_INC:
15003 case PRE_DEC:
15004 /* Turn these into a PLUS expression and fall into the PLUS code
15005 below. */
15006 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15007 gen_int_mode (GET_CODE (rtl) == PRE_INC
15008 ? GET_MODE_UNIT_SIZE (mem_mode)
15009 : -GET_MODE_UNIT_SIZE (mem_mode),
15010 mode));
15012 /* fall through */
15014 case PLUS:
15015 plus:
15016 if (is_based_loc (rtl)
15017 && is_a <scalar_int_mode> (mode, &int_mode)
15018 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15019 || XEXP (rtl, 0) == arg_pointer_rtx
15020 || XEXP (rtl, 0) == frame_pointer_rtx))
15021 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15022 INTVAL (XEXP (rtl, 1)),
15023 VAR_INIT_STATUS_INITIALIZED);
15024 else
15026 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15027 VAR_INIT_STATUS_INITIALIZED);
15028 if (mem_loc_result == 0)
15029 break;
15031 if (CONST_INT_P (XEXP (rtl, 1))
15032 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15033 <= DWARF2_ADDR_SIZE))
15034 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15035 else
15037 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15038 VAR_INIT_STATUS_INITIALIZED);
15039 if (op1 == 0)
15040 return NULL;
15041 add_loc_descr (&mem_loc_result, op1);
15042 add_loc_descr (&mem_loc_result,
15043 new_loc_descr (DW_OP_plus, 0, 0));
15046 break;
15048 /* If a pseudo-reg is optimized away, it is possible for it to
15049 be replaced with a MEM containing a multiply or shift. */
15050 case MINUS:
15051 op = DW_OP_minus;
15052 goto do_binop;
15054 case MULT:
15055 op = DW_OP_mul;
15056 goto do_binop;
15058 case DIV:
15059 if ((!dwarf_strict || dwarf_version >= 5)
15060 && is_a <scalar_int_mode> (mode, &int_mode)
15061 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15063 mem_loc_result = typed_binop (DW_OP_div, rtl,
15064 base_type_for_mode (mode, 0),
15065 int_mode, mem_mode);
15066 break;
15068 op = DW_OP_div;
15069 goto do_binop;
15071 case UMOD:
15072 op = DW_OP_mod;
15073 goto do_binop;
15075 case ASHIFT:
15076 op = DW_OP_shl;
15077 goto do_shift;
15079 case ASHIFTRT:
15080 op = DW_OP_shra;
15081 goto do_shift;
15083 case LSHIFTRT:
15084 op = DW_OP_shr;
15085 goto do_shift;
15087 do_shift:
15088 if (!is_a <scalar_int_mode> (mode, &int_mode))
15089 break;
15090 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15091 VAR_INIT_STATUS_INITIALIZED);
15093 rtx rtlop1 = XEXP (rtl, 1);
15094 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15095 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15096 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15097 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15098 VAR_INIT_STATUS_INITIALIZED);
15101 if (op0 == 0 || op1 == 0)
15102 break;
15104 mem_loc_result = op0;
15105 add_loc_descr (&mem_loc_result, op1);
15106 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15107 break;
15109 case AND:
15110 op = DW_OP_and;
15111 goto do_binop;
15113 case IOR:
15114 op = DW_OP_or;
15115 goto do_binop;
15117 case XOR:
15118 op = DW_OP_xor;
15119 goto do_binop;
15121 do_binop:
15122 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15123 VAR_INIT_STATUS_INITIALIZED);
15124 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15125 VAR_INIT_STATUS_INITIALIZED);
15127 if (op0 == 0 || op1 == 0)
15128 break;
15130 mem_loc_result = op0;
15131 add_loc_descr (&mem_loc_result, op1);
15132 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15133 break;
15135 case MOD:
15136 if ((!dwarf_strict || dwarf_version >= 5)
15137 && is_a <scalar_int_mode> (mode, &int_mode)
15138 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15140 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15141 base_type_for_mode (mode, 0),
15142 int_mode, mem_mode);
15143 break;
15146 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15147 VAR_INIT_STATUS_INITIALIZED);
15148 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15149 VAR_INIT_STATUS_INITIALIZED);
15151 if (op0 == 0 || op1 == 0)
15152 break;
15154 mem_loc_result = op0;
15155 add_loc_descr (&mem_loc_result, op1);
15156 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15157 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15158 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15159 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15160 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15161 break;
15163 case UDIV:
15164 if ((!dwarf_strict || dwarf_version >= 5)
15165 && is_a <scalar_int_mode> (mode, &int_mode))
15167 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15169 op = DW_OP_div;
15170 goto do_binop;
15172 mem_loc_result = typed_binop (DW_OP_div, rtl,
15173 base_type_for_mode (int_mode, 1),
15174 int_mode, mem_mode);
15176 break;
15178 case NOT:
15179 op = DW_OP_not;
15180 goto do_unop;
15182 case ABS:
15183 op = DW_OP_abs;
15184 goto do_unop;
15186 case NEG:
15187 op = DW_OP_neg;
15188 goto do_unop;
15190 do_unop:
15191 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15192 VAR_INIT_STATUS_INITIALIZED);
15194 if (op0 == 0)
15195 break;
15197 mem_loc_result = op0;
15198 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15199 break;
15201 case CONST_INT:
15202 if (!is_a <scalar_int_mode> (mode, &int_mode)
15203 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15204 #ifdef POINTERS_EXTEND_UNSIGNED
15205 || (int_mode == Pmode
15206 && mem_mode != VOIDmode
15207 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15208 #endif
15211 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15212 break;
15214 if ((!dwarf_strict || dwarf_version >= 5)
15215 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15216 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15218 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15219 scalar_int_mode amode;
15220 if (type_die == NULL)
15221 return NULL;
15222 if (INTVAL (rtl) >= 0
15223 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15224 .exists (&amode))
15225 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15226 /* const DW_OP_convert <XXX> vs.
15227 DW_OP_const_type <XXX, 1, const>. */
15228 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15229 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15231 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15232 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15233 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15234 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15235 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15236 add_loc_descr (&mem_loc_result, op0);
15237 return mem_loc_result;
15239 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15240 INTVAL (rtl));
15241 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15242 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15243 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15244 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15245 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15246 else
15248 mem_loc_result->dw_loc_oprnd2.val_class
15249 = dw_val_class_const_double;
15250 mem_loc_result->dw_loc_oprnd2.v.val_double
15251 = double_int::from_shwi (INTVAL (rtl));
15254 break;
15256 case CONST_DOUBLE:
15257 if (!dwarf_strict || dwarf_version >= 5)
15259 dw_die_ref type_die;
15261 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15262 CONST_DOUBLE rtx could represent either a large integer
15263 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15264 the value is always a floating point constant.
15266 When it is an integer, a CONST_DOUBLE is used whenever
15267 the constant requires 2 HWIs to be adequately represented.
15268 We output CONST_DOUBLEs as blocks. */
15269 if (mode == VOIDmode
15270 || (GET_MODE (rtl) == VOIDmode
15271 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15272 break;
15273 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15274 if (type_die == NULL)
15275 return NULL;
15276 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15277 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15278 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15279 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15280 #if TARGET_SUPPORTS_WIDE_INT == 0
15281 if (!SCALAR_FLOAT_MODE_P (mode))
15283 mem_loc_result->dw_loc_oprnd2.val_class
15284 = dw_val_class_const_double;
15285 mem_loc_result->dw_loc_oprnd2.v.val_double
15286 = rtx_to_double_int (rtl);
15288 else
15289 #endif
15291 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15292 unsigned int length = GET_MODE_SIZE (float_mode);
15293 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15295 insert_float (rtl, array);
15296 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15297 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15298 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15299 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15302 break;
15304 case CONST_WIDE_INT:
15305 if (!dwarf_strict || dwarf_version >= 5)
15307 dw_die_ref type_die;
15309 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15310 if (type_die == NULL)
15311 return NULL;
15312 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15313 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15314 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15315 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15316 mem_loc_result->dw_loc_oprnd2.val_class
15317 = dw_val_class_wide_int;
15318 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15319 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15321 break;
15323 case EQ:
15324 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15325 break;
15327 case GE:
15328 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15329 break;
15331 case GT:
15332 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15333 break;
15335 case LE:
15336 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15337 break;
15339 case LT:
15340 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15341 break;
15343 case NE:
15344 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15345 break;
15347 case GEU:
15348 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15349 break;
15351 case GTU:
15352 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15353 break;
15355 case LEU:
15356 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15357 break;
15359 case LTU:
15360 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15361 break;
15363 case UMIN:
15364 case UMAX:
15365 if (!SCALAR_INT_MODE_P (mode))
15366 break;
15367 /* FALLTHRU */
15368 case SMIN:
15369 case SMAX:
15370 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15371 break;
15373 case ZERO_EXTRACT:
15374 case SIGN_EXTRACT:
15375 if (CONST_INT_P (XEXP (rtl, 1))
15376 && CONST_INT_P (XEXP (rtl, 2))
15377 && is_a <scalar_int_mode> (mode, &int_mode)
15378 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15379 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15380 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15381 && ((unsigned) INTVAL (XEXP (rtl, 1))
15382 + (unsigned) INTVAL (XEXP (rtl, 2))
15383 <= GET_MODE_BITSIZE (int_mode)))
15385 int shift, size;
15386 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15387 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15388 if (op0 == 0)
15389 break;
15390 if (GET_CODE (rtl) == SIGN_EXTRACT)
15391 op = DW_OP_shra;
15392 else
15393 op = DW_OP_shr;
15394 mem_loc_result = op0;
15395 size = INTVAL (XEXP (rtl, 1));
15396 shift = INTVAL (XEXP (rtl, 2));
15397 if (BITS_BIG_ENDIAN)
15398 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15399 if (shift + size != (int) DWARF2_ADDR_SIZE)
15401 add_loc_descr (&mem_loc_result,
15402 int_loc_descriptor (DWARF2_ADDR_SIZE
15403 - shift - size));
15404 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15406 if (size != (int) DWARF2_ADDR_SIZE)
15408 add_loc_descr (&mem_loc_result,
15409 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15410 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15413 break;
15415 case IF_THEN_ELSE:
15417 dw_loc_descr_ref op2, bra_node, drop_node;
15418 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15419 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15420 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15421 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15422 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15423 VAR_INIT_STATUS_INITIALIZED);
15424 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15425 VAR_INIT_STATUS_INITIALIZED);
15426 if (op0 == NULL || op1 == NULL || op2 == NULL)
15427 break;
15429 mem_loc_result = op1;
15430 add_loc_descr (&mem_loc_result, op2);
15431 add_loc_descr (&mem_loc_result, op0);
15432 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15433 add_loc_descr (&mem_loc_result, bra_node);
15434 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15435 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15436 add_loc_descr (&mem_loc_result, drop_node);
15437 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15438 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15440 break;
15442 case FLOAT_EXTEND:
15443 case FLOAT_TRUNCATE:
15444 case FLOAT:
15445 case UNSIGNED_FLOAT:
15446 case FIX:
15447 case UNSIGNED_FIX:
15448 if (!dwarf_strict || dwarf_version >= 5)
15450 dw_die_ref type_die;
15451 dw_loc_descr_ref cvt;
15453 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15454 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15455 if (op0 == NULL)
15456 break;
15457 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15458 && (GET_CODE (rtl) == FLOAT
15459 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15461 type_die = base_type_for_mode (int_mode,
15462 GET_CODE (rtl) == UNSIGNED_FLOAT);
15463 if (type_die == NULL)
15464 break;
15465 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15466 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15467 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15468 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15469 add_loc_descr (&op0, cvt);
15471 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15472 if (type_die == NULL)
15473 break;
15474 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15475 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15476 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15477 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15478 add_loc_descr (&op0, cvt);
15479 if (is_a <scalar_int_mode> (mode, &int_mode)
15480 && (GET_CODE (rtl) == FIX
15481 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15483 op0 = convert_descriptor_to_mode (int_mode, op0);
15484 if (op0 == NULL)
15485 break;
15487 mem_loc_result = op0;
15489 break;
15491 case CLZ:
15492 case CTZ:
15493 case FFS:
15494 if (is_a <scalar_int_mode> (mode, &int_mode))
15495 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15496 break;
15498 case POPCOUNT:
15499 case PARITY:
15500 if (is_a <scalar_int_mode> (mode, &int_mode))
15501 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15502 break;
15504 case BSWAP:
15505 if (is_a <scalar_int_mode> (mode, &int_mode))
15506 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15507 break;
15509 case ROTATE:
15510 case ROTATERT:
15511 if (is_a <scalar_int_mode> (mode, &int_mode))
15512 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15513 break;
15515 case COMPARE:
15516 /* In theory, we could implement the above. */
15517 /* DWARF cannot represent the unsigned compare operations
15518 natively. */
15519 case SS_MULT:
15520 case US_MULT:
15521 case SS_DIV:
15522 case US_DIV:
15523 case SS_PLUS:
15524 case US_PLUS:
15525 case SS_MINUS:
15526 case US_MINUS:
15527 case SS_NEG:
15528 case US_NEG:
15529 case SS_ABS:
15530 case SS_ASHIFT:
15531 case US_ASHIFT:
15532 case SS_TRUNCATE:
15533 case US_TRUNCATE:
15534 case UNORDERED:
15535 case ORDERED:
15536 case UNEQ:
15537 case UNGE:
15538 case UNGT:
15539 case UNLE:
15540 case UNLT:
15541 case LTGT:
15542 case FRACT_CONVERT:
15543 case UNSIGNED_FRACT_CONVERT:
15544 case SAT_FRACT:
15545 case UNSIGNED_SAT_FRACT:
15546 case SQRT:
15547 case ASM_OPERANDS:
15548 case VEC_MERGE:
15549 case VEC_SELECT:
15550 case VEC_CONCAT:
15551 case VEC_DUPLICATE:
15552 case UNSPEC:
15553 case HIGH:
15554 case FMA:
15555 case STRICT_LOW_PART:
15556 case CONST_VECTOR:
15557 case CONST_FIXED:
15558 case CLRSB:
15559 case CLOBBER:
15560 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15561 can't express it in the debug info. This can happen e.g. with some
15562 TLS UNSPECs. */
15563 break;
15565 case CONST_STRING:
15566 resolve_one_addr (&rtl);
15567 goto symref;
15569 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15570 the expression. An UNSPEC rtx represents a raw DWARF operation,
15571 new_loc_descr is called for it to build the operation directly.
15572 Otherwise mem_loc_descriptor is called recursively. */
15573 case PARALLEL:
15575 int index = 0;
15576 dw_loc_descr_ref exp_result = NULL;
15578 for (; index < XVECLEN (rtl, 0); index++)
15580 rtx elem = XVECEXP (rtl, 0, index);
15581 if (GET_CODE (elem) == UNSPEC)
15583 /* Each DWARF operation UNSPEC contain two operands, if
15584 one operand is not used for the operation, const0_rtx is
15585 passed. */
15586 gcc_assert (XVECLEN (elem, 0) == 2);
15588 HOST_WIDE_INT dw_op = XINT (elem, 1);
15589 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15590 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15591 exp_result
15592 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15593 oprnd2);
15595 else
15596 exp_result
15597 = mem_loc_descriptor (elem, mode, mem_mode,
15598 VAR_INIT_STATUS_INITIALIZED);
15600 if (!mem_loc_result)
15601 mem_loc_result = exp_result;
15602 else
15603 add_loc_descr (&mem_loc_result, exp_result);
15606 break;
15609 default:
15610 if (flag_checking)
15612 print_rtl (stderr, rtl);
15613 gcc_unreachable ();
15615 break;
15618 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15619 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15621 return mem_loc_result;
15624 /* Return a descriptor that describes the concatenation of two locations.
15625 This is typically a complex variable. */
15627 static dw_loc_descr_ref
15628 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15630 dw_loc_descr_ref cc_loc_result = NULL;
15631 dw_loc_descr_ref x0_ref
15632 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15633 dw_loc_descr_ref x1_ref
15634 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15636 if (x0_ref == 0 || x1_ref == 0)
15637 return 0;
15639 cc_loc_result = x0_ref;
15640 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15642 add_loc_descr (&cc_loc_result, x1_ref);
15643 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15645 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15646 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15648 return cc_loc_result;
15651 /* Return a descriptor that describes the concatenation of N
15652 locations. */
15654 static dw_loc_descr_ref
15655 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15657 unsigned int i;
15658 dw_loc_descr_ref cc_loc_result = NULL;
15659 unsigned int n = XVECLEN (concatn, 0);
15661 for (i = 0; i < n; ++i)
15663 dw_loc_descr_ref ref;
15664 rtx x = XVECEXP (concatn, 0, i);
15666 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15667 if (ref == NULL)
15668 return NULL;
15670 add_loc_descr (&cc_loc_result, ref);
15671 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15674 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15675 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15677 return cc_loc_result;
15680 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15681 for DEBUG_IMPLICIT_PTR RTL. */
15683 static dw_loc_descr_ref
15684 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15686 dw_loc_descr_ref ret;
15687 dw_die_ref ref;
15689 if (dwarf_strict && dwarf_version < 5)
15690 return NULL;
15691 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15692 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15693 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15694 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15695 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15696 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15697 if (ref)
15699 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15700 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15701 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15703 else
15705 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15706 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15708 return ret;
15711 /* Output a proper Dwarf location descriptor for a variable or parameter
15712 which is either allocated in a register or in a memory location. For a
15713 register, we just generate an OP_REG and the register number. For a
15714 memory location we provide a Dwarf postfix expression describing how to
15715 generate the (dynamic) address of the object onto the address stack.
15717 MODE is mode of the decl if this loc_descriptor is going to be used in
15718 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15719 allowed, VOIDmode otherwise.
15721 If we don't know how to describe it, return 0. */
15723 static dw_loc_descr_ref
15724 loc_descriptor (rtx rtl, machine_mode mode,
15725 enum var_init_status initialized)
15727 dw_loc_descr_ref loc_result = NULL;
15728 scalar_int_mode int_mode;
15730 switch (GET_CODE (rtl))
15732 case SUBREG:
15733 /* The case of a subreg may arise when we have a local (register)
15734 variable or a formal (register) parameter which doesn't quite fill
15735 up an entire register. For now, just assume that it is
15736 legitimate to make the Dwarf info refer to the whole register which
15737 contains the given subreg. */
15738 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15739 loc_result = loc_descriptor (SUBREG_REG (rtl),
15740 GET_MODE (SUBREG_REG (rtl)), initialized);
15741 else
15742 goto do_default;
15743 break;
15745 case REG:
15746 loc_result = reg_loc_descriptor (rtl, initialized);
15747 break;
15749 case MEM:
15750 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15751 GET_MODE (rtl), initialized);
15752 if (loc_result == NULL)
15753 loc_result = tls_mem_loc_descriptor (rtl);
15754 if (loc_result == NULL)
15756 rtx new_rtl = avoid_constant_pool_reference (rtl);
15757 if (new_rtl != rtl)
15758 loc_result = loc_descriptor (new_rtl, mode, initialized);
15760 break;
15762 case CONCAT:
15763 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15764 initialized);
15765 break;
15767 case CONCATN:
15768 loc_result = concatn_loc_descriptor (rtl, initialized);
15769 break;
15771 case VAR_LOCATION:
15772 /* Single part. */
15773 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15775 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15776 if (GET_CODE (loc) == EXPR_LIST)
15777 loc = XEXP (loc, 0);
15778 loc_result = loc_descriptor (loc, mode, initialized);
15779 break;
15782 rtl = XEXP (rtl, 1);
15783 /* FALLTHRU */
15785 case PARALLEL:
15787 rtvec par_elems = XVEC (rtl, 0);
15788 int num_elem = GET_NUM_ELEM (par_elems);
15789 machine_mode mode;
15790 int i;
15792 /* Create the first one, so we have something to add to. */
15793 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15794 VOIDmode, initialized);
15795 if (loc_result == NULL)
15796 return NULL;
15797 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15798 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15799 for (i = 1; i < num_elem; i++)
15801 dw_loc_descr_ref temp;
15803 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15804 VOIDmode, initialized);
15805 if (temp == NULL)
15806 return NULL;
15807 add_loc_descr (&loc_result, temp);
15808 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15809 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15812 break;
15814 case CONST_INT:
15815 if (mode != VOIDmode && mode != BLKmode)
15817 int_mode = as_a <scalar_int_mode> (mode);
15818 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15819 INTVAL (rtl));
15821 break;
15823 case CONST_DOUBLE:
15824 if (mode == VOIDmode)
15825 mode = GET_MODE (rtl);
15827 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15829 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15831 /* Note that a CONST_DOUBLE rtx could represent either an integer
15832 or a floating-point constant. A CONST_DOUBLE is used whenever
15833 the constant requires more than one word in order to be
15834 adequately represented. We output CONST_DOUBLEs as blocks. */
15835 scalar_mode smode = as_a <scalar_mode> (mode);
15836 loc_result = new_loc_descr (DW_OP_implicit_value,
15837 GET_MODE_SIZE (smode), 0);
15838 #if TARGET_SUPPORTS_WIDE_INT == 0
15839 if (!SCALAR_FLOAT_MODE_P (smode))
15841 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15842 loc_result->dw_loc_oprnd2.v.val_double
15843 = rtx_to_double_int (rtl);
15845 else
15846 #endif
15848 unsigned int length = GET_MODE_SIZE (smode);
15849 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15851 insert_float (rtl, array);
15852 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15853 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15854 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15855 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15858 break;
15860 case CONST_WIDE_INT:
15861 if (mode == VOIDmode)
15862 mode = GET_MODE (rtl);
15864 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15866 int_mode = as_a <scalar_int_mode> (mode);
15867 loc_result = new_loc_descr (DW_OP_implicit_value,
15868 GET_MODE_SIZE (int_mode), 0);
15869 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15870 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15871 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15873 break;
15875 case CONST_VECTOR:
15876 if (mode == VOIDmode)
15877 mode = GET_MODE (rtl);
15879 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15881 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15882 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15883 unsigned char *array
15884 = ggc_vec_alloc<unsigned char> (length * elt_size);
15885 unsigned int i;
15886 unsigned char *p;
15887 machine_mode imode = GET_MODE_INNER (mode);
15889 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15890 switch (GET_MODE_CLASS (mode))
15892 case MODE_VECTOR_INT:
15893 for (i = 0, p = array; i < length; i++, p += elt_size)
15895 rtx elt = CONST_VECTOR_ELT (rtl, i);
15896 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15898 break;
15900 case MODE_VECTOR_FLOAT:
15901 for (i = 0, p = array; i < length; i++, p += elt_size)
15903 rtx elt = CONST_VECTOR_ELT (rtl, i);
15904 insert_float (elt, p);
15906 break;
15908 default:
15909 gcc_unreachable ();
15912 loc_result = new_loc_descr (DW_OP_implicit_value,
15913 length * elt_size, 0);
15914 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15915 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15916 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15917 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15919 break;
15921 case CONST:
15922 if (mode == VOIDmode
15923 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15924 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15925 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15927 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15928 break;
15930 /* FALLTHROUGH */
15931 case SYMBOL_REF:
15932 if (!const_ok_for_output (rtl))
15933 break;
15934 /* FALLTHROUGH */
15935 case LABEL_REF:
15936 if (is_a <scalar_int_mode> (mode, &int_mode)
15937 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15938 && (dwarf_version >= 4 || !dwarf_strict))
15940 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15941 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15942 vec_safe_push (used_rtx_array, rtl);
15944 break;
15946 case DEBUG_IMPLICIT_PTR:
15947 loc_result = implicit_ptr_descriptor (rtl, 0);
15948 break;
15950 case PLUS:
15951 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15952 && CONST_INT_P (XEXP (rtl, 1)))
15954 loc_result
15955 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15956 break;
15958 /* FALLTHRU */
15959 do_default:
15960 default:
15961 if ((is_a <scalar_int_mode> (mode, &int_mode)
15962 && GET_MODE (rtl) == int_mode
15963 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15964 && dwarf_version >= 4)
15965 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15967 /* Value expression. */
15968 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15969 if (loc_result)
15970 add_loc_descr (&loc_result,
15971 new_loc_descr (DW_OP_stack_value, 0, 0));
15973 break;
15976 return loc_result;
15979 /* We need to figure out what section we should use as the base for the
15980 address ranges where a given location is valid.
15981 1. If this particular DECL has a section associated with it, use that.
15982 2. If this function has a section associated with it, use that.
15983 3. Otherwise, use the text section.
15984 XXX: If you split a variable across multiple sections, we won't notice. */
15986 static const char *
15987 secname_for_decl (const_tree decl)
15989 const char *secname;
15991 if (VAR_OR_FUNCTION_DECL_P (decl)
15992 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15993 && DECL_SECTION_NAME (decl))
15994 secname = DECL_SECTION_NAME (decl);
15995 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15996 secname = DECL_SECTION_NAME (current_function_decl);
15997 else if (cfun && in_cold_section_p)
15998 secname = crtl->subsections.cold_section_label;
15999 else
16000 secname = text_section_label;
16002 return secname;
16005 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16007 static bool
16008 decl_by_reference_p (tree decl)
16010 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16011 || VAR_P (decl))
16012 && DECL_BY_REFERENCE (decl));
16015 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16016 for VARLOC. */
16018 static dw_loc_descr_ref
16019 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16020 enum var_init_status initialized)
16022 int have_address = 0;
16023 dw_loc_descr_ref descr;
16024 machine_mode mode;
16026 if (want_address != 2)
16028 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16029 /* Single part. */
16030 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16032 varloc = PAT_VAR_LOCATION_LOC (varloc);
16033 if (GET_CODE (varloc) == EXPR_LIST)
16034 varloc = XEXP (varloc, 0);
16035 mode = GET_MODE (varloc);
16036 if (MEM_P (varloc))
16038 rtx addr = XEXP (varloc, 0);
16039 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16040 mode, initialized);
16041 if (descr)
16042 have_address = 1;
16043 else
16045 rtx x = avoid_constant_pool_reference (varloc);
16046 if (x != varloc)
16047 descr = mem_loc_descriptor (x, mode, VOIDmode,
16048 initialized);
16051 else
16052 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16054 else
16055 return 0;
16057 else
16059 if (GET_CODE (varloc) == VAR_LOCATION)
16060 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16061 else
16062 mode = DECL_MODE (loc);
16063 descr = loc_descriptor (varloc, mode, initialized);
16064 have_address = 1;
16067 if (!descr)
16068 return 0;
16070 if (want_address == 2 && !have_address
16071 && (dwarf_version >= 4 || !dwarf_strict))
16073 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16075 expansion_failed (loc, NULL_RTX,
16076 "DWARF address size mismatch");
16077 return 0;
16079 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16080 have_address = 1;
16082 /* Show if we can't fill the request for an address. */
16083 if (want_address && !have_address)
16085 expansion_failed (loc, NULL_RTX,
16086 "Want address and only have value");
16087 return 0;
16090 /* If we've got an address and don't want one, dereference. */
16091 if (!want_address && have_address)
16093 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16094 enum dwarf_location_atom op;
16096 if (size > DWARF2_ADDR_SIZE || size == -1)
16098 expansion_failed (loc, NULL_RTX,
16099 "DWARF address size mismatch");
16100 return 0;
16102 else if (size == DWARF2_ADDR_SIZE)
16103 op = DW_OP_deref;
16104 else
16105 op = DW_OP_deref_size;
16107 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16110 return descr;
16113 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16114 if it is not possible. */
16116 static dw_loc_descr_ref
16117 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16119 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16120 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16121 else if (dwarf_version >= 3 || !dwarf_strict)
16122 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16123 else
16124 return NULL;
16127 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16128 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16130 static dw_loc_descr_ref
16131 dw_sra_loc_expr (tree decl, rtx loc)
16133 rtx p;
16134 unsigned HOST_WIDE_INT padsize = 0;
16135 dw_loc_descr_ref descr, *descr_tail;
16136 unsigned HOST_WIDE_INT decl_size;
16137 rtx varloc;
16138 enum var_init_status initialized;
16140 if (DECL_SIZE (decl) == NULL
16141 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16142 return NULL;
16144 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16145 descr = NULL;
16146 descr_tail = &descr;
16148 for (p = loc; p; p = XEXP (p, 1))
16150 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16151 rtx loc_note = *decl_piece_varloc_ptr (p);
16152 dw_loc_descr_ref cur_descr;
16153 dw_loc_descr_ref *tail, last = NULL;
16154 unsigned HOST_WIDE_INT opsize = 0;
16156 if (loc_note == NULL_RTX
16157 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16159 padsize += bitsize;
16160 continue;
16162 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16163 varloc = NOTE_VAR_LOCATION (loc_note);
16164 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16165 if (cur_descr == NULL)
16167 padsize += bitsize;
16168 continue;
16171 /* Check that cur_descr either doesn't use
16172 DW_OP_*piece operations, or their sum is equal
16173 to bitsize. Otherwise we can't embed it. */
16174 for (tail = &cur_descr; *tail != NULL;
16175 tail = &(*tail)->dw_loc_next)
16176 if ((*tail)->dw_loc_opc == DW_OP_piece)
16178 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16179 * BITS_PER_UNIT;
16180 last = *tail;
16182 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16184 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16185 last = *tail;
16188 if (last != NULL && opsize != bitsize)
16190 padsize += bitsize;
16191 /* Discard the current piece of the descriptor and release any
16192 addr_table entries it uses. */
16193 remove_loc_list_addr_table_entries (cur_descr);
16194 continue;
16197 /* If there is a hole, add DW_OP_*piece after empty DWARF
16198 expression, which means that those bits are optimized out. */
16199 if (padsize)
16201 if (padsize > decl_size)
16203 remove_loc_list_addr_table_entries (cur_descr);
16204 goto discard_descr;
16206 decl_size -= padsize;
16207 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16208 if (*descr_tail == NULL)
16210 remove_loc_list_addr_table_entries (cur_descr);
16211 goto discard_descr;
16213 descr_tail = &(*descr_tail)->dw_loc_next;
16214 padsize = 0;
16216 *descr_tail = cur_descr;
16217 descr_tail = tail;
16218 if (bitsize > decl_size)
16219 goto discard_descr;
16220 decl_size -= bitsize;
16221 if (last == NULL)
16223 HOST_WIDE_INT offset = 0;
16224 if (GET_CODE (varloc) == VAR_LOCATION
16225 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16227 varloc = PAT_VAR_LOCATION_LOC (varloc);
16228 if (GET_CODE (varloc) == EXPR_LIST)
16229 varloc = XEXP (varloc, 0);
16233 if (GET_CODE (varloc) == CONST
16234 || GET_CODE (varloc) == SIGN_EXTEND
16235 || GET_CODE (varloc) == ZERO_EXTEND)
16236 varloc = XEXP (varloc, 0);
16237 else if (GET_CODE (varloc) == SUBREG)
16238 varloc = SUBREG_REG (varloc);
16239 else
16240 break;
16242 while (1);
16243 /* DW_OP_bit_size offset should be zero for register
16244 or implicit location descriptions and empty location
16245 descriptions, but for memory addresses needs big endian
16246 adjustment. */
16247 if (MEM_P (varloc))
16249 unsigned HOST_WIDE_INT memsize
16250 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16251 if (memsize != bitsize)
16253 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16254 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16255 goto discard_descr;
16256 if (memsize < bitsize)
16257 goto discard_descr;
16258 if (BITS_BIG_ENDIAN)
16259 offset = memsize - bitsize;
16263 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16264 if (*descr_tail == NULL)
16265 goto discard_descr;
16266 descr_tail = &(*descr_tail)->dw_loc_next;
16270 /* If there were any non-empty expressions, add padding till the end of
16271 the decl. */
16272 if (descr != NULL && decl_size != 0)
16274 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16275 if (*descr_tail == NULL)
16276 goto discard_descr;
16278 return descr;
16280 discard_descr:
16281 /* Discard the descriptor and release any addr_table entries it uses. */
16282 remove_loc_list_addr_table_entries (descr);
16283 return NULL;
16286 /* Return the dwarf representation of the location list LOC_LIST of
16287 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16288 function. */
16290 static dw_loc_list_ref
16291 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16293 const char *endname, *secname;
16294 rtx varloc;
16295 enum var_init_status initialized;
16296 struct var_loc_node *node;
16297 dw_loc_descr_ref descr;
16298 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16299 dw_loc_list_ref list = NULL;
16300 dw_loc_list_ref *listp = &list;
16302 /* Now that we know what section we are using for a base,
16303 actually construct the list of locations.
16304 The first location information is what is passed to the
16305 function that creates the location list, and the remaining
16306 locations just get added on to that list.
16307 Note that we only know the start address for a location
16308 (IE location changes), so to build the range, we use
16309 the range [current location start, next location start].
16310 This means we have to special case the last node, and generate
16311 a range of [last location start, end of function label]. */
16313 secname = secname_for_decl (decl);
16315 for (node = loc_list->first; node; node = node->next)
16316 if (GET_CODE (node->loc) == EXPR_LIST
16317 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16319 if (GET_CODE (node->loc) == EXPR_LIST)
16321 /* This requires DW_OP_{,bit_}piece, which is not usable
16322 inside DWARF expressions. */
16323 if (want_address != 2)
16324 continue;
16325 descr = dw_sra_loc_expr (decl, node->loc);
16326 if (descr == NULL)
16327 continue;
16329 else
16331 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16332 varloc = NOTE_VAR_LOCATION (node->loc);
16333 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16335 if (descr)
16337 bool range_across_switch = false;
16338 /* If section switch happens in between node->label
16339 and node->next->label (or end of function) and
16340 we can't emit it as a single entry list,
16341 emit two ranges, first one ending at the end
16342 of first partition and second one starting at the
16343 beginning of second partition. */
16344 if (node == loc_list->last_before_switch
16345 && (node != loc_list->first || loc_list->first->next)
16346 && current_function_decl)
16348 endname = cfun->fde->dw_fde_end;
16349 range_across_switch = true;
16351 /* The variable has a location between NODE->LABEL and
16352 NODE->NEXT->LABEL. */
16353 else if (node->next)
16354 endname = node->next->label;
16355 /* If the variable has a location at the last label
16356 it keeps its location until the end of function. */
16357 else if (!current_function_decl)
16358 endname = text_end_label;
16359 else
16361 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16362 current_function_funcdef_no);
16363 endname = ggc_strdup (label_id);
16366 *listp = new_loc_list (descr, node->label, endname, secname);
16367 if (TREE_CODE (decl) == PARM_DECL
16368 && node == loc_list->first
16369 && NOTE_P (node->loc)
16370 && strcmp (node->label, endname) == 0)
16371 (*listp)->force = true;
16372 listp = &(*listp)->dw_loc_next;
16374 if (range_across_switch)
16376 if (GET_CODE (node->loc) == EXPR_LIST)
16377 descr = dw_sra_loc_expr (decl, node->loc);
16378 else
16380 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16381 varloc = NOTE_VAR_LOCATION (node->loc);
16382 descr = dw_loc_list_1 (decl, varloc, want_address,
16383 initialized);
16385 gcc_assert (descr);
16386 /* The variable has a location between NODE->LABEL and
16387 NODE->NEXT->LABEL. */
16388 if (node->next)
16389 endname = node->next->label;
16390 else
16391 endname = cfun->fde->dw_fde_second_end;
16392 *listp = new_loc_list (descr,
16393 cfun->fde->dw_fde_second_begin,
16394 endname, secname);
16395 listp = &(*listp)->dw_loc_next;
16400 /* Try to avoid the overhead of a location list emitting a location
16401 expression instead, but only if we didn't have more than one
16402 location entry in the first place. If some entries were not
16403 representable, we don't want to pretend a single entry that was
16404 applies to the entire scope in which the variable is
16405 available. */
16406 if (list && loc_list->first->next)
16407 gen_llsym (list);
16409 return list;
16412 /* Return if the loc_list has only single element and thus can be represented
16413 as location description. */
16415 static bool
16416 single_element_loc_list_p (dw_loc_list_ref list)
16418 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16419 return !list->ll_symbol;
16422 /* Duplicate a single element of location list. */
16424 static inline dw_loc_descr_ref
16425 copy_loc_descr (dw_loc_descr_ref ref)
16427 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16428 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16429 return copy;
16432 /* To each location in list LIST append loc descr REF. */
16434 static void
16435 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16437 dw_loc_descr_ref copy;
16438 add_loc_descr (&list->expr, ref);
16439 list = list->dw_loc_next;
16440 while (list)
16442 copy = copy_loc_descr (ref);
16443 add_loc_descr (&list->expr, copy);
16444 while (copy->dw_loc_next)
16445 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16446 list = list->dw_loc_next;
16450 /* To each location in list LIST prepend loc descr REF. */
16452 static void
16453 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16455 dw_loc_descr_ref copy;
16456 dw_loc_descr_ref ref_end = list->expr;
16457 add_loc_descr (&ref, list->expr);
16458 list->expr = ref;
16459 list = list->dw_loc_next;
16460 while (list)
16462 dw_loc_descr_ref end = list->expr;
16463 list->expr = copy = copy_loc_descr (ref);
16464 while (copy->dw_loc_next != ref_end)
16465 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16466 copy->dw_loc_next = end;
16467 list = list->dw_loc_next;
16471 /* Given two lists RET and LIST
16472 produce location list that is result of adding expression in LIST
16473 to expression in RET on each position in program.
16474 Might be destructive on both RET and LIST.
16476 TODO: We handle only simple cases of RET or LIST having at most one
16477 element. General case would involve sorting the lists in program order
16478 and merging them that will need some additional work.
16479 Adding that will improve quality of debug info especially for SRA-ed
16480 structures. */
16482 static void
16483 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16485 if (!list)
16486 return;
16487 if (!*ret)
16489 *ret = list;
16490 return;
16492 if (!list->dw_loc_next)
16494 add_loc_descr_to_each (*ret, list->expr);
16495 return;
16497 if (!(*ret)->dw_loc_next)
16499 prepend_loc_descr_to_each (list, (*ret)->expr);
16500 *ret = list;
16501 return;
16503 expansion_failed (NULL_TREE, NULL_RTX,
16504 "Don't know how to merge two non-trivial"
16505 " location lists.\n");
16506 *ret = NULL;
16507 return;
16510 /* LOC is constant expression. Try a luck, look it up in constant
16511 pool and return its loc_descr of its address. */
16513 static dw_loc_descr_ref
16514 cst_pool_loc_descr (tree loc)
16516 /* Get an RTL for this, if something has been emitted. */
16517 rtx rtl = lookup_constant_def (loc);
16519 if (!rtl || !MEM_P (rtl))
16521 gcc_assert (!rtl);
16522 return 0;
16524 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16526 /* TODO: We might get more coverage if we was actually delaying expansion
16527 of all expressions till end of compilation when constant pools are fully
16528 populated. */
16529 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16531 expansion_failed (loc, NULL_RTX,
16532 "CST value in contant pool but not marked.");
16533 return 0;
16535 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16536 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16539 /* Return dw_loc_list representing address of addr_expr LOC
16540 by looking for inner INDIRECT_REF expression and turning
16541 it into simple arithmetics.
16543 See loc_list_from_tree for the meaning of CONTEXT. */
16545 static dw_loc_list_ref
16546 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16547 loc_descr_context *context)
16549 tree obj, offset;
16550 HOST_WIDE_INT bitsize, bitpos, bytepos;
16551 machine_mode mode;
16552 int unsignedp, reversep, volatilep = 0;
16553 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16555 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16556 &bitsize, &bitpos, &offset, &mode,
16557 &unsignedp, &reversep, &volatilep);
16558 STRIP_NOPS (obj);
16559 if (bitpos % BITS_PER_UNIT)
16561 expansion_failed (loc, NULL_RTX, "bitfield access");
16562 return 0;
16564 if (!INDIRECT_REF_P (obj))
16566 expansion_failed (obj,
16567 NULL_RTX, "no indirect ref in inner refrence");
16568 return 0;
16570 if (!offset && !bitpos)
16571 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16572 context);
16573 else if (toplev
16574 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16575 && (dwarf_version >= 4 || !dwarf_strict))
16577 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16578 if (!list_ret)
16579 return 0;
16580 if (offset)
16582 /* Variable offset. */
16583 list_ret1 = loc_list_from_tree (offset, 0, context);
16584 if (list_ret1 == 0)
16585 return 0;
16586 add_loc_list (&list_ret, list_ret1);
16587 if (!list_ret)
16588 return 0;
16589 add_loc_descr_to_each (list_ret,
16590 new_loc_descr (DW_OP_plus, 0, 0));
16592 bytepos = bitpos / BITS_PER_UNIT;
16593 if (bytepos > 0)
16594 add_loc_descr_to_each (list_ret,
16595 new_loc_descr (DW_OP_plus_uconst,
16596 bytepos, 0));
16597 else if (bytepos < 0)
16598 loc_list_plus_const (list_ret, bytepos);
16599 add_loc_descr_to_each (list_ret,
16600 new_loc_descr (DW_OP_stack_value, 0, 0));
16602 return list_ret;
16605 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16606 all operations from LOC are nops, move to the last one. Insert in NOPS all
16607 operations that are skipped. */
16609 static void
16610 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16611 hash_set<dw_loc_descr_ref> &nops)
16613 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16615 nops.add (loc);
16616 loc = loc->dw_loc_next;
16620 /* Helper for loc_descr_without_nops: free the location description operation
16621 P. */
16623 bool
16624 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16626 ggc_free (loc);
16627 return true;
16630 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16631 finishes LOC. */
16633 static void
16634 loc_descr_without_nops (dw_loc_descr_ref &loc)
16636 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16637 return;
16639 /* Set of all DW_OP_nop operations we remove. */
16640 hash_set<dw_loc_descr_ref> nops;
16642 /* First, strip all prefix NOP operations in order to keep the head of the
16643 operations list. */
16644 loc_descr_to_next_no_nop (loc, nops);
16646 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16648 /* For control flow operations: strip "prefix" nops in destination
16649 labels. */
16650 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16651 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16652 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16653 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16655 /* Do the same for the operations that follow, then move to the next
16656 iteration. */
16657 if (cur->dw_loc_next != NULL)
16658 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16659 cur = cur->dw_loc_next;
16662 nops.traverse<void *, free_loc_descr> (NULL);
16666 struct dwarf_procedure_info;
16668 /* Helper structure for location descriptions generation. */
16669 struct loc_descr_context
16671 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16672 NULL_TREE if DW_OP_push_object_address in invalid for this location
16673 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16674 tree context_type;
16675 /* The ..._DECL node that should be translated as a
16676 DW_OP_push_object_address operation. */
16677 tree base_decl;
16678 /* Information about the DWARF procedure we are currently generating. NULL if
16679 we are not generating a DWARF procedure. */
16680 struct dwarf_procedure_info *dpi;
16681 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16682 by consumer. Used for DW_TAG_generic_subrange attributes. */
16683 bool placeholder_arg;
16684 /* True if PLACEHOLDER_EXPR has been seen. */
16685 bool placeholder_seen;
16688 /* DWARF procedures generation
16690 DWARF expressions (aka. location descriptions) are used to encode variable
16691 things such as sizes or offsets. Such computations can have redundant parts
16692 that can be factorized in order to reduce the size of the output debug
16693 information. This is the whole point of DWARF procedures.
16695 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16696 already factorized into functions ("size functions") in order to handle very
16697 big and complex types. Such functions are quite simple: they have integral
16698 arguments, they return an integral result and their body contains only a
16699 return statement with arithmetic expressions. This is the only kind of
16700 function we are interested in translating into DWARF procedures, here.
16702 DWARF expressions and DWARF procedure are executed using a stack, so we have
16703 to define some calling convention for them to interact. Let's say that:
16705 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16706 all arguments in reverse order (right-to-left) so that when the DWARF
16707 procedure execution starts, the first argument is the top of the stack.
16709 - Then, when returning, the DWARF procedure must have consumed all arguments
16710 on the stack, must have pushed the result and touched nothing else.
16712 - Each integral argument and the result are integral types can be hold in a
16713 single stack slot.
16715 - We call "frame offset" the number of stack slots that are "under DWARF
16716 procedure control": it includes the arguments slots, the temporaries and
16717 the result slot. Thus, it is equal to the number of arguments when the
16718 procedure execution starts and must be equal to one (the result) when it
16719 returns. */
16721 /* Helper structure used when generating operations for a DWARF procedure. */
16722 struct dwarf_procedure_info
16724 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16725 currently translated. */
16726 tree fndecl;
16727 /* The number of arguments FNDECL takes. */
16728 unsigned args_count;
16731 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16732 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16733 equate it to this DIE. */
16735 static dw_die_ref
16736 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16737 dw_die_ref parent_die)
16739 dw_die_ref dwarf_proc_die;
16741 if ((dwarf_version < 3 && dwarf_strict)
16742 || location == NULL)
16743 return NULL;
16745 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16746 if (fndecl)
16747 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16748 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16749 return dwarf_proc_die;
16752 /* Return whether TYPE is a supported type as a DWARF procedure argument
16753 type or return type (we handle only scalar types and pointer types that
16754 aren't wider than the DWARF expression evaluation stack. */
16756 static bool
16757 is_handled_procedure_type (tree type)
16759 return ((INTEGRAL_TYPE_P (type)
16760 || TREE_CODE (type) == OFFSET_TYPE
16761 || TREE_CODE (type) == POINTER_TYPE)
16762 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16765 /* Helper for resolve_args_picking: do the same but stop when coming across
16766 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16767 offset *before* evaluating the corresponding operation. */
16769 static bool
16770 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16771 struct dwarf_procedure_info *dpi,
16772 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16774 /* The "frame_offset" identifier is already used to name a macro... */
16775 unsigned frame_offset_ = initial_frame_offset;
16776 dw_loc_descr_ref l;
16778 for (l = loc; l != NULL;)
16780 bool existed;
16781 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16783 /* If we already met this node, there is nothing to compute anymore. */
16784 if (existed)
16786 /* Make sure that the stack size is consistent wherever the execution
16787 flow comes from. */
16788 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16789 break;
16791 l_frame_offset = frame_offset_;
16793 /* If needed, relocate the picking offset with respect to the frame
16794 offset. */
16795 if (l->frame_offset_rel)
16797 unsigned HOST_WIDE_INT off;
16798 switch (l->dw_loc_opc)
16800 case DW_OP_pick:
16801 off = l->dw_loc_oprnd1.v.val_unsigned;
16802 break;
16803 case DW_OP_dup:
16804 off = 0;
16805 break;
16806 case DW_OP_over:
16807 off = 1;
16808 break;
16809 default:
16810 gcc_unreachable ();
16812 /* frame_offset_ is the size of the current stack frame, including
16813 incoming arguments. Besides, the arguments are pushed
16814 right-to-left. Thus, in order to access the Nth argument from
16815 this operation node, the picking has to skip temporaries *plus*
16816 one stack slot per argument (0 for the first one, 1 for the second
16817 one, etc.).
16819 The targetted argument number (N) is already set as the operand,
16820 and the number of temporaries can be computed with:
16821 frame_offsets_ - dpi->args_count */
16822 off += frame_offset_ - dpi->args_count;
16824 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16825 if (off > 255)
16826 return false;
16828 if (off == 0)
16830 l->dw_loc_opc = DW_OP_dup;
16831 l->dw_loc_oprnd1.v.val_unsigned = 0;
16833 else if (off == 1)
16835 l->dw_loc_opc = DW_OP_over;
16836 l->dw_loc_oprnd1.v.val_unsigned = 0;
16838 else
16840 l->dw_loc_opc = DW_OP_pick;
16841 l->dw_loc_oprnd1.v.val_unsigned = off;
16845 /* Update frame_offset according to the effect the current operation has
16846 on the stack. */
16847 switch (l->dw_loc_opc)
16849 case DW_OP_deref:
16850 case DW_OP_swap:
16851 case DW_OP_rot:
16852 case DW_OP_abs:
16853 case DW_OP_neg:
16854 case DW_OP_not:
16855 case DW_OP_plus_uconst:
16856 case DW_OP_skip:
16857 case DW_OP_reg0:
16858 case DW_OP_reg1:
16859 case DW_OP_reg2:
16860 case DW_OP_reg3:
16861 case DW_OP_reg4:
16862 case DW_OP_reg5:
16863 case DW_OP_reg6:
16864 case DW_OP_reg7:
16865 case DW_OP_reg8:
16866 case DW_OP_reg9:
16867 case DW_OP_reg10:
16868 case DW_OP_reg11:
16869 case DW_OP_reg12:
16870 case DW_OP_reg13:
16871 case DW_OP_reg14:
16872 case DW_OP_reg15:
16873 case DW_OP_reg16:
16874 case DW_OP_reg17:
16875 case DW_OP_reg18:
16876 case DW_OP_reg19:
16877 case DW_OP_reg20:
16878 case DW_OP_reg21:
16879 case DW_OP_reg22:
16880 case DW_OP_reg23:
16881 case DW_OP_reg24:
16882 case DW_OP_reg25:
16883 case DW_OP_reg26:
16884 case DW_OP_reg27:
16885 case DW_OP_reg28:
16886 case DW_OP_reg29:
16887 case DW_OP_reg30:
16888 case DW_OP_reg31:
16889 case DW_OP_bregx:
16890 case DW_OP_piece:
16891 case DW_OP_deref_size:
16892 case DW_OP_nop:
16893 case DW_OP_bit_piece:
16894 case DW_OP_implicit_value:
16895 case DW_OP_stack_value:
16896 break;
16898 case DW_OP_addr:
16899 case DW_OP_const1u:
16900 case DW_OP_const1s:
16901 case DW_OP_const2u:
16902 case DW_OP_const2s:
16903 case DW_OP_const4u:
16904 case DW_OP_const4s:
16905 case DW_OP_const8u:
16906 case DW_OP_const8s:
16907 case DW_OP_constu:
16908 case DW_OP_consts:
16909 case DW_OP_dup:
16910 case DW_OP_over:
16911 case DW_OP_pick:
16912 case DW_OP_lit0:
16913 case DW_OP_lit1:
16914 case DW_OP_lit2:
16915 case DW_OP_lit3:
16916 case DW_OP_lit4:
16917 case DW_OP_lit5:
16918 case DW_OP_lit6:
16919 case DW_OP_lit7:
16920 case DW_OP_lit8:
16921 case DW_OP_lit9:
16922 case DW_OP_lit10:
16923 case DW_OP_lit11:
16924 case DW_OP_lit12:
16925 case DW_OP_lit13:
16926 case DW_OP_lit14:
16927 case DW_OP_lit15:
16928 case DW_OP_lit16:
16929 case DW_OP_lit17:
16930 case DW_OP_lit18:
16931 case DW_OP_lit19:
16932 case DW_OP_lit20:
16933 case DW_OP_lit21:
16934 case DW_OP_lit22:
16935 case DW_OP_lit23:
16936 case DW_OP_lit24:
16937 case DW_OP_lit25:
16938 case DW_OP_lit26:
16939 case DW_OP_lit27:
16940 case DW_OP_lit28:
16941 case DW_OP_lit29:
16942 case DW_OP_lit30:
16943 case DW_OP_lit31:
16944 case DW_OP_breg0:
16945 case DW_OP_breg1:
16946 case DW_OP_breg2:
16947 case DW_OP_breg3:
16948 case DW_OP_breg4:
16949 case DW_OP_breg5:
16950 case DW_OP_breg6:
16951 case DW_OP_breg7:
16952 case DW_OP_breg8:
16953 case DW_OP_breg9:
16954 case DW_OP_breg10:
16955 case DW_OP_breg11:
16956 case DW_OP_breg12:
16957 case DW_OP_breg13:
16958 case DW_OP_breg14:
16959 case DW_OP_breg15:
16960 case DW_OP_breg16:
16961 case DW_OP_breg17:
16962 case DW_OP_breg18:
16963 case DW_OP_breg19:
16964 case DW_OP_breg20:
16965 case DW_OP_breg21:
16966 case DW_OP_breg22:
16967 case DW_OP_breg23:
16968 case DW_OP_breg24:
16969 case DW_OP_breg25:
16970 case DW_OP_breg26:
16971 case DW_OP_breg27:
16972 case DW_OP_breg28:
16973 case DW_OP_breg29:
16974 case DW_OP_breg30:
16975 case DW_OP_breg31:
16976 case DW_OP_fbreg:
16977 case DW_OP_push_object_address:
16978 case DW_OP_call_frame_cfa:
16979 case DW_OP_GNU_variable_value:
16980 ++frame_offset_;
16981 break;
16983 case DW_OP_drop:
16984 case DW_OP_xderef:
16985 case DW_OP_and:
16986 case DW_OP_div:
16987 case DW_OP_minus:
16988 case DW_OP_mod:
16989 case DW_OP_mul:
16990 case DW_OP_or:
16991 case DW_OP_plus:
16992 case DW_OP_shl:
16993 case DW_OP_shr:
16994 case DW_OP_shra:
16995 case DW_OP_xor:
16996 case DW_OP_bra:
16997 case DW_OP_eq:
16998 case DW_OP_ge:
16999 case DW_OP_gt:
17000 case DW_OP_le:
17001 case DW_OP_lt:
17002 case DW_OP_ne:
17003 case DW_OP_regx:
17004 case DW_OP_xderef_size:
17005 --frame_offset_;
17006 break;
17008 case DW_OP_call2:
17009 case DW_OP_call4:
17010 case DW_OP_call_ref:
17012 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17013 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17015 if (stack_usage == NULL)
17016 return false;
17017 frame_offset_ += *stack_usage;
17018 break;
17021 case DW_OP_implicit_pointer:
17022 case DW_OP_entry_value:
17023 case DW_OP_const_type:
17024 case DW_OP_regval_type:
17025 case DW_OP_deref_type:
17026 case DW_OP_convert:
17027 case DW_OP_reinterpret:
17028 case DW_OP_form_tls_address:
17029 case DW_OP_GNU_push_tls_address:
17030 case DW_OP_GNU_uninit:
17031 case DW_OP_GNU_encoded_addr:
17032 case DW_OP_GNU_implicit_pointer:
17033 case DW_OP_GNU_entry_value:
17034 case DW_OP_GNU_const_type:
17035 case DW_OP_GNU_regval_type:
17036 case DW_OP_GNU_deref_type:
17037 case DW_OP_GNU_convert:
17038 case DW_OP_GNU_reinterpret:
17039 case DW_OP_GNU_parameter_ref:
17040 /* loc_list_from_tree will probably not output these operations for
17041 size functions, so assume they will not appear here. */
17042 /* Fall through... */
17044 default:
17045 gcc_unreachable ();
17048 /* Now, follow the control flow (except subroutine calls). */
17049 switch (l->dw_loc_opc)
17051 case DW_OP_bra:
17052 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17053 frame_offsets))
17054 return false;
17055 /* Fall through. */
17057 case DW_OP_skip:
17058 l = l->dw_loc_oprnd1.v.val_loc;
17059 break;
17061 case DW_OP_stack_value:
17062 return true;
17064 default:
17065 l = l->dw_loc_next;
17066 break;
17070 return true;
17073 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17074 operations) in order to resolve the operand of DW_OP_pick operations that
17075 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17076 offset *before* LOC is executed. Return if all relocations were
17077 successful. */
17079 static bool
17080 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17081 struct dwarf_procedure_info *dpi)
17083 /* Associate to all visited operations the frame offset *before* evaluating
17084 this operation. */
17085 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17087 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17088 frame_offsets);
17091 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17092 Return NULL if it is not possible. */
17094 static dw_die_ref
17095 function_to_dwarf_procedure (tree fndecl)
17097 struct loc_descr_context ctx;
17098 struct dwarf_procedure_info dpi;
17099 dw_die_ref dwarf_proc_die;
17100 tree tree_body = DECL_SAVED_TREE (fndecl);
17101 dw_loc_descr_ref loc_body, epilogue;
17103 tree cursor;
17104 unsigned i;
17106 /* Do not generate multiple DWARF procedures for the same function
17107 declaration. */
17108 dwarf_proc_die = lookup_decl_die (fndecl);
17109 if (dwarf_proc_die != NULL)
17110 return dwarf_proc_die;
17112 /* DWARF procedures are available starting with the DWARFv3 standard. */
17113 if (dwarf_version < 3 && dwarf_strict)
17114 return NULL;
17116 /* We handle only functions for which we still have a body, that return a
17117 supported type and that takes arguments with supported types. Note that
17118 there is no point translating functions that return nothing. */
17119 if (tree_body == NULL_TREE
17120 || DECL_RESULT (fndecl) == NULL_TREE
17121 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17122 return NULL;
17124 for (cursor = DECL_ARGUMENTS (fndecl);
17125 cursor != NULL_TREE;
17126 cursor = TREE_CHAIN (cursor))
17127 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17128 return NULL;
17130 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17131 if (TREE_CODE (tree_body) != RETURN_EXPR)
17132 return NULL;
17133 tree_body = TREE_OPERAND (tree_body, 0);
17134 if (TREE_CODE (tree_body) != MODIFY_EXPR
17135 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17136 return NULL;
17137 tree_body = TREE_OPERAND (tree_body, 1);
17139 /* Try to translate the body expression itself. Note that this will probably
17140 cause an infinite recursion if its call graph has a cycle. This is very
17141 unlikely for size functions, however, so don't bother with such things at
17142 the moment. */
17143 ctx.context_type = NULL_TREE;
17144 ctx.base_decl = NULL_TREE;
17145 ctx.dpi = &dpi;
17146 ctx.placeholder_arg = false;
17147 ctx.placeholder_seen = false;
17148 dpi.fndecl = fndecl;
17149 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17150 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17151 if (!loc_body)
17152 return NULL;
17154 /* After evaluating all operands in "loc_body", we should still have on the
17155 stack all arguments plus the desired function result (top of the stack).
17156 Generate code in order to keep only the result in our stack frame. */
17157 epilogue = NULL;
17158 for (i = 0; i < dpi.args_count; ++i)
17160 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17161 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17162 op_couple->dw_loc_next->dw_loc_next = epilogue;
17163 epilogue = op_couple;
17165 add_loc_descr (&loc_body, epilogue);
17166 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17167 return NULL;
17169 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17170 because they are considered useful. Now there is an epilogue, they are
17171 not anymore, so give it another try. */
17172 loc_descr_without_nops (loc_body);
17174 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17175 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17176 though, given that size functions do not come from source, so they should
17177 not have a dedicated DW_TAG_subprogram DIE. */
17178 dwarf_proc_die
17179 = new_dwarf_proc_die (loc_body, fndecl,
17180 get_context_die (DECL_CONTEXT (fndecl)));
17182 /* The called DWARF procedure consumes one stack slot per argument and
17183 returns one stack slot. */
17184 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17186 return dwarf_proc_die;
17190 /* Generate Dwarf location list representing LOC.
17191 If WANT_ADDRESS is false, expression computing LOC will be computed
17192 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17193 if WANT_ADDRESS is 2, expression computing address useable in location
17194 will be returned (i.e. DW_OP_reg can be used
17195 to refer to register values).
17197 CONTEXT provides information to customize the location descriptions
17198 generation. Its context_type field specifies what type is implicitly
17199 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17200 will not be generated.
17202 Its DPI field determines whether we are generating a DWARF expression for a
17203 DWARF procedure, so PARM_DECL references are processed specifically.
17205 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17206 and dpi fields were null. */
17208 static dw_loc_list_ref
17209 loc_list_from_tree_1 (tree loc, int want_address,
17210 struct loc_descr_context *context)
17212 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17213 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17214 int have_address = 0;
17215 enum dwarf_location_atom op;
17217 /* ??? Most of the time we do not take proper care for sign/zero
17218 extending the values properly. Hopefully this won't be a real
17219 problem... */
17221 if (context != NULL
17222 && context->base_decl == loc
17223 && want_address == 0)
17225 if (dwarf_version >= 3 || !dwarf_strict)
17226 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17227 NULL, NULL, NULL);
17228 else
17229 return NULL;
17232 switch (TREE_CODE (loc))
17234 case ERROR_MARK:
17235 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17236 return 0;
17238 case PLACEHOLDER_EXPR:
17239 /* This case involves extracting fields from an object to determine the
17240 position of other fields. It is supposed to appear only as the first
17241 operand of COMPONENT_REF nodes and to reference precisely the type
17242 that the context allows. */
17243 if (context != NULL
17244 && TREE_TYPE (loc) == context->context_type
17245 && want_address >= 1)
17247 if (dwarf_version >= 3 || !dwarf_strict)
17249 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17250 have_address = 1;
17251 break;
17253 else
17254 return NULL;
17256 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17257 the single argument passed by consumer. */
17258 else if (context != NULL
17259 && context->placeholder_arg
17260 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17261 && want_address == 0)
17263 ret = new_loc_descr (DW_OP_pick, 0, 0);
17264 ret->frame_offset_rel = 1;
17265 context->placeholder_seen = true;
17266 break;
17268 else
17269 expansion_failed (loc, NULL_RTX,
17270 "PLACEHOLDER_EXPR for an unexpected type");
17271 break;
17273 case CALL_EXPR:
17275 const int nargs = call_expr_nargs (loc);
17276 tree callee = get_callee_fndecl (loc);
17277 int i;
17278 dw_die_ref dwarf_proc;
17280 if (callee == NULL_TREE)
17281 goto call_expansion_failed;
17283 /* We handle only functions that return an integer. */
17284 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17285 goto call_expansion_failed;
17287 dwarf_proc = function_to_dwarf_procedure (callee);
17288 if (dwarf_proc == NULL)
17289 goto call_expansion_failed;
17291 /* Evaluate arguments right-to-left so that the first argument will
17292 be the top-most one on the stack. */
17293 for (i = nargs - 1; i >= 0; --i)
17295 dw_loc_descr_ref loc_descr
17296 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17297 context);
17299 if (loc_descr == NULL)
17300 goto call_expansion_failed;
17302 add_loc_descr (&ret, loc_descr);
17305 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17306 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17307 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17308 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17309 add_loc_descr (&ret, ret1);
17310 break;
17312 call_expansion_failed:
17313 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17314 /* There are no opcodes for these operations. */
17315 return 0;
17318 case PREINCREMENT_EXPR:
17319 case PREDECREMENT_EXPR:
17320 case POSTINCREMENT_EXPR:
17321 case POSTDECREMENT_EXPR:
17322 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17323 /* There are no opcodes for these operations. */
17324 return 0;
17326 case ADDR_EXPR:
17327 /* If we already want an address, see if there is INDIRECT_REF inside
17328 e.g. for &this->field. */
17329 if (want_address)
17331 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17332 (loc, want_address == 2, context);
17333 if (list_ret)
17334 have_address = 1;
17335 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17336 && (ret = cst_pool_loc_descr (loc)))
17337 have_address = 1;
17339 /* Otherwise, process the argument and look for the address. */
17340 if (!list_ret && !ret)
17341 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17342 else
17344 if (want_address)
17345 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17346 return NULL;
17348 break;
17350 case VAR_DECL:
17351 if (DECL_THREAD_LOCAL_P (loc))
17353 rtx rtl;
17354 enum dwarf_location_atom tls_op;
17355 enum dtprel_bool dtprel = dtprel_false;
17357 if (targetm.have_tls)
17359 /* If this is not defined, we have no way to emit the
17360 data. */
17361 if (!targetm.asm_out.output_dwarf_dtprel)
17362 return 0;
17364 /* The way DW_OP_GNU_push_tls_address is specified, we
17365 can only look up addresses of objects in the current
17366 module. We used DW_OP_addr as first op, but that's
17367 wrong, because DW_OP_addr is relocated by the debug
17368 info consumer, while DW_OP_GNU_push_tls_address
17369 operand shouldn't be. */
17370 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17371 return 0;
17372 dtprel = dtprel_true;
17373 /* We check for DWARF 5 here because gdb did not implement
17374 DW_OP_form_tls_address until after 7.12. */
17375 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17376 : DW_OP_GNU_push_tls_address);
17378 else
17380 if (!targetm.emutls.debug_form_tls_address
17381 || !(dwarf_version >= 3 || !dwarf_strict))
17382 return 0;
17383 /* We stuffed the control variable into the DECL_VALUE_EXPR
17384 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17385 no longer appear in gimple code. We used the control
17386 variable in specific so that we could pick it up here. */
17387 loc = DECL_VALUE_EXPR (loc);
17388 tls_op = DW_OP_form_tls_address;
17391 rtl = rtl_for_decl_location (loc);
17392 if (rtl == NULL_RTX)
17393 return 0;
17395 if (!MEM_P (rtl))
17396 return 0;
17397 rtl = XEXP (rtl, 0);
17398 if (! CONSTANT_P (rtl))
17399 return 0;
17401 ret = new_addr_loc_descr (rtl, dtprel);
17402 ret1 = new_loc_descr (tls_op, 0, 0);
17403 add_loc_descr (&ret, ret1);
17405 have_address = 1;
17406 break;
17408 /* FALLTHRU */
17410 case PARM_DECL:
17411 if (context != NULL && context->dpi != NULL
17412 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17414 /* We are generating code for a DWARF procedure and we want to access
17415 one of its arguments: find the appropriate argument offset and let
17416 the resolve_args_picking pass compute the offset that complies
17417 with the stack frame size. */
17418 unsigned i = 0;
17419 tree cursor;
17421 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17422 cursor != NULL_TREE && cursor != loc;
17423 cursor = TREE_CHAIN (cursor), ++i)
17425 /* If we are translating a DWARF procedure, all referenced parameters
17426 must belong to the current function. */
17427 gcc_assert (cursor != NULL_TREE);
17429 ret = new_loc_descr (DW_OP_pick, i, 0);
17430 ret->frame_offset_rel = 1;
17431 break;
17433 /* FALLTHRU */
17435 case RESULT_DECL:
17436 if (DECL_HAS_VALUE_EXPR_P (loc))
17437 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17438 want_address, context);
17439 /* FALLTHRU */
17441 case FUNCTION_DECL:
17443 rtx rtl;
17444 var_loc_list *loc_list = lookup_decl_loc (loc);
17446 if (loc_list && loc_list->first)
17448 list_ret = dw_loc_list (loc_list, loc, want_address);
17449 have_address = want_address != 0;
17450 break;
17452 rtl = rtl_for_decl_location (loc);
17453 if (rtl == NULL_RTX)
17455 if (TREE_CODE (loc) != FUNCTION_DECL
17456 && early_dwarf
17457 && current_function_decl
17458 && want_address != 1
17459 && ! DECL_IGNORED_P (loc)
17460 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17461 || POINTER_TYPE_P (TREE_TYPE (loc)))
17462 && DECL_CONTEXT (loc) == current_function_decl
17463 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17464 <= DWARF2_ADDR_SIZE))
17466 dw_die_ref ref = lookup_decl_die (loc);
17467 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17468 if (ref)
17470 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17471 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17472 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17474 else
17476 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17477 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17479 break;
17481 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17482 return 0;
17484 else if (CONST_INT_P (rtl))
17486 HOST_WIDE_INT val = INTVAL (rtl);
17487 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17488 val &= GET_MODE_MASK (DECL_MODE (loc));
17489 ret = int_loc_descriptor (val);
17491 else if (GET_CODE (rtl) == CONST_STRING)
17493 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17494 return 0;
17496 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17497 ret = new_addr_loc_descr (rtl, dtprel_false);
17498 else
17500 machine_mode mode, mem_mode;
17502 /* Certain constructs can only be represented at top-level. */
17503 if (want_address == 2)
17505 ret = loc_descriptor (rtl, VOIDmode,
17506 VAR_INIT_STATUS_INITIALIZED);
17507 have_address = 1;
17509 else
17511 mode = GET_MODE (rtl);
17512 mem_mode = VOIDmode;
17513 if (MEM_P (rtl))
17515 mem_mode = mode;
17516 mode = get_address_mode (rtl);
17517 rtl = XEXP (rtl, 0);
17518 have_address = 1;
17520 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17521 VAR_INIT_STATUS_INITIALIZED);
17523 if (!ret)
17524 expansion_failed (loc, rtl,
17525 "failed to produce loc descriptor for rtl");
17528 break;
17530 case MEM_REF:
17531 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17533 have_address = 1;
17534 goto do_plus;
17536 /* Fallthru. */
17537 case INDIRECT_REF:
17538 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17539 have_address = 1;
17540 break;
17542 case TARGET_MEM_REF:
17543 case SSA_NAME:
17544 case DEBUG_EXPR_DECL:
17545 return NULL;
17547 case COMPOUND_EXPR:
17548 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17549 context);
17551 CASE_CONVERT:
17552 case VIEW_CONVERT_EXPR:
17553 case SAVE_EXPR:
17554 case MODIFY_EXPR:
17555 case NON_LVALUE_EXPR:
17556 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17557 context);
17559 case COMPONENT_REF:
17560 case BIT_FIELD_REF:
17561 case ARRAY_REF:
17562 case ARRAY_RANGE_REF:
17563 case REALPART_EXPR:
17564 case IMAGPART_EXPR:
17566 tree obj, offset;
17567 HOST_WIDE_INT bitsize, bitpos, bytepos;
17568 machine_mode mode;
17569 int unsignedp, reversep, volatilep = 0;
17571 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17572 &unsignedp, &reversep, &volatilep);
17574 gcc_assert (obj != loc);
17576 list_ret = loc_list_from_tree_1 (obj,
17577 want_address == 2
17578 && !bitpos && !offset ? 2 : 1,
17579 context);
17580 /* TODO: We can extract value of the small expression via shifting even
17581 for nonzero bitpos. */
17582 if (list_ret == 0)
17583 return 0;
17584 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17586 expansion_failed (loc, NULL_RTX,
17587 "bitfield access");
17588 return 0;
17591 if (offset != NULL_TREE)
17593 /* Variable offset. */
17594 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17595 if (list_ret1 == 0)
17596 return 0;
17597 add_loc_list (&list_ret, list_ret1);
17598 if (!list_ret)
17599 return 0;
17600 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17603 bytepos = bitpos / BITS_PER_UNIT;
17604 if (bytepos > 0)
17605 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17606 else if (bytepos < 0)
17607 loc_list_plus_const (list_ret, bytepos);
17609 have_address = 1;
17610 break;
17613 case INTEGER_CST:
17614 if ((want_address || !tree_fits_shwi_p (loc))
17615 && (ret = cst_pool_loc_descr (loc)))
17616 have_address = 1;
17617 else if (want_address == 2
17618 && tree_fits_shwi_p (loc)
17619 && (ret = address_of_int_loc_descriptor
17620 (int_size_in_bytes (TREE_TYPE (loc)),
17621 tree_to_shwi (loc))))
17622 have_address = 1;
17623 else if (tree_fits_shwi_p (loc))
17624 ret = int_loc_descriptor (tree_to_shwi (loc));
17625 else if (tree_fits_uhwi_p (loc))
17626 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17627 else
17629 expansion_failed (loc, NULL_RTX,
17630 "Integer operand is not host integer");
17631 return 0;
17633 break;
17635 case CONSTRUCTOR:
17636 case REAL_CST:
17637 case STRING_CST:
17638 case COMPLEX_CST:
17639 if ((ret = cst_pool_loc_descr (loc)))
17640 have_address = 1;
17641 else if (TREE_CODE (loc) == CONSTRUCTOR)
17643 tree type = TREE_TYPE (loc);
17644 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17645 unsigned HOST_WIDE_INT offset = 0;
17646 unsigned HOST_WIDE_INT cnt;
17647 constructor_elt *ce;
17649 if (TREE_CODE (type) == RECORD_TYPE)
17651 /* This is very limited, but it's enough to output
17652 pointers to member functions, as long as the
17653 referenced function is defined in the current
17654 translation unit. */
17655 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17657 tree val = ce->value;
17659 tree field = ce->index;
17661 if (val)
17662 STRIP_NOPS (val);
17664 if (!field || DECL_BIT_FIELD (field))
17666 expansion_failed (loc, NULL_RTX,
17667 "bitfield in record type constructor");
17668 size = offset = (unsigned HOST_WIDE_INT)-1;
17669 ret = NULL;
17670 break;
17673 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17674 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17675 gcc_assert (pos + fieldsize <= size);
17676 if (pos < offset)
17678 expansion_failed (loc, NULL_RTX,
17679 "out-of-order fields in record constructor");
17680 size = offset = (unsigned HOST_WIDE_INT)-1;
17681 ret = NULL;
17682 break;
17684 if (pos > offset)
17686 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17687 add_loc_descr (&ret, ret1);
17688 offset = pos;
17690 if (val && fieldsize != 0)
17692 ret1 = loc_descriptor_from_tree (val, want_address, context);
17693 if (!ret1)
17695 expansion_failed (loc, NULL_RTX,
17696 "unsupported expression in field");
17697 size = offset = (unsigned HOST_WIDE_INT)-1;
17698 ret = NULL;
17699 break;
17701 add_loc_descr (&ret, ret1);
17703 if (fieldsize)
17705 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17706 add_loc_descr (&ret, ret1);
17707 offset = pos + fieldsize;
17711 if (offset != size)
17713 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17714 add_loc_descr (&ret, ret1);
17715 offset = size;
17718 have_address = !!want_address;
17720 else
17721 expansion_failed (loc, NULL_RTX,
17722 "constructor of non-record type");
17724 else
17725 /* We can construct small constants here using int_loc_descriptor. */
17726 expansion_failed (loc, NULL_RTX,
17727 "constructor or constant not in constant pool");
17728 break;
17730 case TRUTH_AND_EXPR:
17731 case TRUTH_ANDIF_EXPR:
17732 case BIT_AND_EXPR:
17733 op = DW_OP_and;
17734 goto do_binop;
17736 case TRUTH_XOR_EXPR:
17737 case BIT_XOR_EXPR:
17738 op = DW_OP_xor;
17739 goto do_binop;
17741 case TRUTH_OR_EXPR:
17742 case TRUTH_ORIF_EXPR:
17743 case BIT_IOR_EXPR:
17744 op = DW_OP_or;
17745 goto do_binop;
17747 case FLOOR_DIV_EXPR:
17748 case CEIL_DIV_EXPR:
17749 case ROUND_DIV_EXPR:
17750 case TRUNC_DIV_EXPR:
17751 case EXACT_DIV_EXPR:
17752 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17753 return 0;
17754 op = DW_OP_div;
17755 goto do_binop;
17757 case MINUS_EXPR:
17758 op = DW_OP_minus;
17759 goto do_binop;
17761 case FLOOR_MOD_EXPR:
17762 case CEIL_MOD_EXPR:
17763 case ROUND_MOD_EXPR:
17764 case TRUNC_MOD_EXPR:
17765 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17767 op = DW_OP_mod;
17768 goto do_binop;
17770 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17771 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17772 if (list_ret == 0 || list_ret1 == 0)
17773 return 0;
17775 add_loc_list (&list_ret, list_ret1);
17776 if (list_ret == 0)
17777 return 0;
17778 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17779 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17780 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17781 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17782 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17783 break;
17785 case MULT_EXPR:
17786 op = DW_OP_mul;
17787 goto do_binop;
17789 case LSHIFT_EXPR:
17790 op = DW_OP_shl;
17791 goto do_binop;
17793 case RSHIFT_EXPR:
17794 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17795 goto do_binop;
17797 case POINTER_PLUS_EXPR:
17798 case PLUS_EXPR:
17799 do_plus:
17800 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17802 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17803 smarter to encode their opposite. The DW_OP_plus_uconst operation
17804 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17805 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17806 bytes, Y being the size of the operation that pushes the opposite
17807 of the addend. So let's choose the smallest representation. */
17808 const tree tree_addend = TREE_OPERAND (loc, 1);
17809 offset_int wi_addend;
17810 HOST_WIDE_INT shwi_addend;
17811 dw_loc_descr_ref loc_naddend;
17813 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17814 if (list_ret == 0)
17815 return 0;
17817 /* Try to get the literal to push. It is the opposite of the addend,
17818 so as we rely on wrapping during DWARF evaluation, first decode
17819 the literal as a "DWARF-sized" signed number. */
17820 wi_addend = wi::to_offset (tree_addend);
17821 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17822 shwi_addend = wi_addend.to_shwi ();
17823 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17824 ? int_loc_descriptor (-shwi_addend)
17825 : NULL;
17827 if (loc_naddend != NULL
17828 && ((unsigned) size_of_uleb128 (shwi_addend)
17829 > size_of_loc_descr (loc_naddend)))
17831 add_loc_descr_to_each (list_ret, loc_naddend);
17832 add_loc_descr_to_each (list_ret,
17833 new_loc_descr (DW_OP_minus, 0, 0));
17835 else
17837 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17839 loc_naddend = loc_cur;
17840 loc_cur = loc_cur->dw_loc_next;
17841 ggc_free (loc_naddend);
17843 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17845 break;
17848 op = DW_OP_plus;
17849 goto do_binop;
17851 case LE_EXPR:
17852 op = DW_OP_le;
17853 goto do_comp_binop;
17855 case GE_EXPR:
17856 op = DW_OP_ge;
17857 goto do_comp_binop;
17859 case LT_EXPR:
17860 op = DW_OP_lt;
17861 goto do_comp_binop;
17863 case GT_EXPR:
17864 op = DW_OP_gt;
17865 goto do_comp_binop;
17867 do_comp_binop:
17868 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17870 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17871 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17872 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17873 TREE_CODE (loc));
17874 break;
17876 else
17877 goto do_binop;
17879 case EQ_EXPR:
17880 op = DW_OP_eq;
17881 goto do_binop;
17883 case NE_EXPR:
17884 op = DW_OP_ne;
17885 goto do_binop;
17887 do_binop:
17888 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17889 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17890 if (list_ret == 0 || list_ret1 == 0)
17891 return 0;
17893 add_loc_list (&list_ret, list_ret1);
17894 if (list_ret == 0)
17895 return 0;
17896 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17897 break;
17899 case TRUTH_NOT_EXPR:
17900 case BIT_NOT_EXPR:
17901 op = DW_OP_not;
17902 goto do_unop;
17904 case ABS_EXPR:
17905 op = DW_OP_abs;
17906 goto do_unop;
17908 case NEGATE_EXPR:
17909 op = DW_OP_neg;
17910 goto do_unop;
17912 do_unop:
17913 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17914 if (list_ret == 0)
17915 return 0;
17917 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17918 break;
17920 case MIN_EXPR:
17921 case MAX_EXPR:
17923 const enum tree_code code =
17924 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17926 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17927 build2 (code, integer_type_node,
17928 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17929 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17932 /* fall through */
17934 case COND_EXPR:
17936 dw_loc_descr_ref lhs
17937 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17938 dw_loc_list_ref rhs
17939 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17940 dw_loc_descr_ref bra_node, jump_node, tmp;
17942 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17943 if (list_ret == 0 || lhs == 0 || rhs == 0)
17944 return 0;
17946 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17947 add_loc_descr_to_each (list_ret, bra_node);
17949 add_loc_list (&list_ret, rhs);
17950 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17951 add_loc_descr_to_each (list_ret, jump_node);
17953 add_loc_descr_to_each (list_ret, lhs);
17954 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17955 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17957 /* ??? Need a node to point the skip at. Use a nop. */
17958 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17959 add_loc_descr_to_each (list_ret, tmp);
17960 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17961 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17963 break;
17965 case FIX_TRUNC_EXPR:
17966 return 0;
17968 default:
17969 /* Leave front-end specific codes as simply unknown. This comes
17970 up, for instance, with the C STMT_EXPR. */
17971 if ((unsigned int) TREE_CODE (loc)
17972 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17974 expansion_failed (loc, NULL_RTX,
17975 "language specific tree node");
17976 return 0;
17979 /* Otherwise this is a generic code; we should just lists all of
17980 these explicitly. We forgot one. */
17981 if (flag_checking)
17982 gcc_unreachable ();
17984 /* In a release build, we want to degrade gracefully: better to
17985 generate incomplete debugging information than to crash. */
17986 return NULL;
17989 if (!ret && !list_ret)
17990 return 0;
17992 if (want_address == 2 && !have_address
17993 && (dwarf_version >= 4 || !dwarf_strict))
17995 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17997 expansion_failed (loc, NULL_RTX,
17998 "DWARF address size mismatch");
17999 return 0;
18001 if (ret)
18002 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18003 else
18004 add_loc_descr_to_each (list_ret,
18005 new_loc_descr (DW_OP_stack_value, 0, 0));
18006 have_address = 1;
18008 /* Show if we can't fill the request for an address. */
18009 if (want_address && !have_address)
18011 expansion_failed (loc, NULL_RTX,
18012 "Want address and only have value");
18013 return 0;
18016 gcc_assert (!ret || !list_ret);
18018 /* If we've got an address and don't want one, dereference. */
18019 if (!want_address && have_address)
18021 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18023 if (size > DWARF2_ADDR_SIZE || size == -1)
18025 expansion_failed (loc, NULL_RTX,
18026 "DWARF address size mismatch");
18027 return 0;
18029 else if (size == DWARF2_ADDR_SIZE)
18030 op = DW_OP_deref;
18031 else
18032 op = DW_OP_deref_size;
18034 if (ret)
18035 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18036 else
18037 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18039 if (ret)
18040 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18042 return list_ret;
18045 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18046 expressions. */
18048 static dw_loc_list_ref
18049 loc_list_from_tree (tree loc, int want_address,
18050 struct loc_descr_context *context)
18052 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18054 for (dw_loc_list_ref loc_cur = result;
18055 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18056 loc_descr_without_nops (loc_cur->expr);
18057 return result;
18060 /* Same as above but return only single location expression. */
18061 static dw_loc_descr_ref
18062 loc_descriptor_from_tree (tree loc, int want_address,
18063 struct loc_descr_context *context)
18065 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18066 if (!ret)
18067 return NULL;
18068 if (ret->dw_loc_next)
18070 expansion_failed (loc, NULL_RTX,
18071 "Location list where only loc descriptor needed");
18072 return NULL;
18074 return ret->expr;
18077 /* Given a value, round it up to the lowest multiple of `boundary'
18078 which is not less than the value itself. */
18080 static inline HOST_WIDE_INT
18081 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18083 return (((value + boundary - 1) / boundary) * boundary);
18086 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18087 pointer to the declared type for the relevant field variable, or return
18088 `integer_type_node' if the given node turns out to be an
18089 ERROR_MARK node. */
18091 static inline tree
18092 field_type (const_tree decl)
18094 tree type;
18096 if (TREE_CODE (decl) == ERROR_MARK)
18097 return integer_type_node;
18099 type = DECL_BIT_FIELD_TYPE (decl);
18100 if (type == NULL_TREE)
18101 type = TREE_TYPE (decl);
18103 return type;
18106 /* Given a pointer to a tree node, return the alignment in bits for
18107 it, or else return BITS_PER_WORD if the node actually turns out to
18108 be an ERROR_MARK node. */
18110 static inline unsigned
18111 simple_type_align_in_bits (const_tree type)
18113 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18116 static inline unsigned
18117 simple_decl_align_in_bits (const_tree decl)
18119 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18122 /* Return the result of rounding T up to ALIGN. */
18124 static inline offset_int
18125 round_up_to_align (const offset_int &t, unsigned int align)
18127 return wi::udiv_trunc (t + align - 1, align) * align;
18130 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18131 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18132 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18133 if we fail to return the size in one of these two forms. */
18135 static dw_loc_descr_ref
18136 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18138 tree tree_size;
18139 struct loc_descr_context ctx;
18141 /* Return a constant integer in priority, if possible. */
18142 *cst_size = int_size_in_bytes (type);
18143 if (*cst_size != -1)
18144 return NULL;
18146 ctx.context_type = const_cast<tree> (type);
18147 ctx.base_decl = NULL_TREE;
18148 ctx.dpi = NULL;
18149 ctx.placeholder_arg = false;
18150 ctx.placeholder_seen = false;
18152 type = TYPE_MAIN_VARIANT (type);
18153 tree_size = TYPE_SIZE_UNIT (type);
18154 return ((tree_size != NULL_TREE)
18155 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18156 : NULL);
18159 /* Helper structure for RECORD_TYPE processing. */
18160 struct vlr_context
18162 /* Root RECORD_TYPE. It is needed to generate data member location
18163 descriptions in variable-length records (VLR), but also to cope with
18164 variants, which are composed of nested structures multiplexed with
18165 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18166 function processing a FIELD_DECL, it is required to be non null. */
18167 tree struct_type;
18168 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18169 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18170 this variant part as part of the root record (in storage units). For
18171 regular records, it must be NULL_TREE. */
18172 tree variant_part_offset;
18175 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18176 addressed byte of the "containing object" for the given FIELD_DECL. If
18177 possible, return a native constant through CST_OFFSET (in which case NULL is
18178 returned); otherwise return a DWARF expression that computes the offset.
18180 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18181 that offset is, either because the argument turns out to be a pointer to an
18182 ERROR_MARK node, or because the offset expression is too complex for us.
18184 CTX is required: see the comment for VLR_CONTEXT. */
18186 static dw_loc_descr_ref
18187 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18188 HOST_WIDE_INT *cst_offset)
18190 tree tree_result;
18191 dw_loc_list_ref loc_result;
18193 *cst_offset = 0;
18195 if (TREE_CODE (decl) == ERROR_MARK)
18196 return NULL;
18197 else
18198 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18200 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18201 case. */
18202 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18203 return NULL;
18205 #ifdef PCC_BITFIELD_TYPE_MATTERS
18206 /* We used to handle only constant offsets in all cases. Now, we handle
18207 properly dynamic byte offsets only when PCC bitfield type doesn't
18208 matter. */
18209 if (PCC_BITFIELD_TYPE_MATTERS
18210 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18212 offset_int object_offset_in_bits;
18213 offset_int object_offset_in_bytes;
18214 offset_int bitpos_int;
18215 tree type;
18216 tree field_size_tree;
18217 offset_int deepest_bitpos;
18218 offset_int field_size_in_bits;
18219 unsigned int type_align_in_bits;
18220 unsigned int decl_align_in_bits;
18221 offset_int type_size_in_bits;
18223 bitpos_int = wi::to_offset (bit_position (decl));
18224 type = field_type (decl);
18225 type_size_in_bits = offset_int_type_size_in_bits (type);
18226 type_align_in_bits = simple_type_align_in_bits (type);
18228 field_size_tree = DECL_SIZE (decl);
18230 /* The size could be unspecified if there was an error, or for
18231 a flexible array member. */
18232 if (!field_size_tree)
18233 field_size_tree = bitsize_zero_node;
18235 /* If the size of the field is not constant, use the type size. */
18236 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18237 field_size_in_bits = wi::to_offset (field_size_tree);
18238 else
18239 field_size_in_bits = type_size_in_bits;
18241 decl_align_in_bits = simple_decl_align_in_bits (decl);
18243 /* The GCC front-end doesn't make any attempt to keep track of the
18244 starting bit offset (relative to the start of the containing
18245 structure type) of the hypothetical "containing object" for a
18246 bit-field. Thus, when computing the byte offset value for the
18247 start of the "containing object" of a bit-field, we must deduce
18248 this information on our own. This can be rather tricky to do in
18249 some cases. For example, handling the following structure type
18250 definition when compiling for an i386/i486 target (which only
18251 aligns long long's to 32-bit boundaries) can be very tricky:
18253 struct S { int field1; long long field2:31; };
18255 Fortunately, there is a simple rule-of-thumb which can be used
18256 in such cases. When compiling for an i386/i486, GCC will
18257 allocate 8 bytes for the structure shown above. It decides to
18258 do this based upon one simple rule for bit-field allocation.
18259 GCC allocates each "containing object" for each bit-field at
18260 the first (i.e. lowest addressed) legitimate alignment boundary
18261 (based upon the required minimum alignment for the declared
18262 type of the field) which it can possibly use, subject to the
18263 condition that there is still enough available space remaining
18264 in the containing object (when allocated at the selected point)
18265 to fully accommodate all of the bits of the bit-field itself.
18267 This simple rule makes it obvious why GCC allocates 8 bytes for
18268 each object of the structure type shown above. When looking
18269 for a place to allocate the "containing object" for `field2',
18270 the compiler simply tries to allocate a 64-bit "containing
18271 object" at each successive 32-bit boundary (starting at zero)
18272 until it finds a place to allocate that 64- bit field such that
18273 at least 31 contiguous (and previously unallocated) bits remain
18274 within that selected 64 bit field. (As it turns out, for the
18275 example above, the compiler finds it is OK to allocate the
18276 "containing object" 64-bit field at bit-offset zero within the
18277 structure type.)
18279 Here we attempt to work backwards from the limited set of facts
18280 we're given, and we try to deduce from those facts, where GCC
18281 must have believed that the containing object started (within
18282 the structure type). The value we deduce is then used (by the
18283 callers of this routine) to generate DW_AT_location and
18284 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18285 the case of DW_AT_location, regular fields as well). */
18287 /* Figure out the bit-distance from the start of the structure to
18288 the "deepest" bit of the bit-field. */
18289 deepest_bitpos = bitpos_int + field_size_in_bits;
18291 /* This is the tricky part. Use some fancy footwork to deduce
18292 where the lowest addressed bit of the containing object must
18293 be. */
18294 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18296 /* Round up to type_align by default. This works best for
18297 bitfields. */
18298 object_offset_in_bits
18299 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18301 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18303 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18305 /* Round up to decl_align instead. */
18306 object_offset_in_bits
18307 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18310 object_offset_in_bytes
18311 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18312 if (ctx->variant_part_offset == NULL_TREE)
18314 *cst_offset = object_offset_in_bytes.to_shwi ();
18315 return NULL;
18317 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18319 else
18320 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18321 tree_result = byte_position (decl);
18323 if (ctx->variant_part_offset != NULL_TREE)
18324 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18325 ctx->variant_part_offset, tree_result);
18327 /* If the byte offset is a constant, it's simplier to handle a native
18328 constant rather than a DWARF expression. */
18329 if (TREE_CODE (tree_result) == INTEGER_CST)
18331 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18332 return NULL;
18334 struct loc_descr_context loc_ctx = {
18335 ctx->struct_type, /* context_type */
18336 NULL_TREE, /* base_decl */
18337 NULL, /* dpi */
18338 false, /* placeholder_arg */
18339 false /* placeholder_seen */
18341 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18343 /* We want a DWARF expression: abort if we only have a location list with
18344 multiple elements. */
18345 if (!loc_result || !single_element_loc_list_p (loc_result))
18346 return NULL;
18347 else
18348 return loc_result->expr;
18351 /* The following routines define various Dwarf attributes and any data
18352 associated with them. */
18354 /* Add a location description attribute value to a DIE.
18356 This emits location attributes suitable for whole variables and
18357 whole parameters. Note that the location attributes for struct fields are
18358 generated by the routine `data_member_location_attribute' below. */
18360 static inline void
18361 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18362 dw_loc_list_ref descr)
18364 if (descr == 0)
18365 return;
18366 if (single_element_loc_list_p (descr))
18367 add_AT_loc (die, attr_kind, descr->expr);
18368 else
18369 add_AT_loc_list (die, attr_kind, descr);
18372 /* Add DW_AT_accessibility attribute to DIE if needed. */
18374 static void
18375 add_accessibility_attribute (dw_die_ref die, tree decl)
18377 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18378 children, otherwise the default is DW_ACCESS_public. In DWARF2
18379 the default has always been DW_ACCESS_public. */
18380 if (TREE_PROTECTED (decl))
18381 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18382 else if (TREE_PRIVATE (decl))
18384 if (dwarf_version == 2
18385 || die->die_parent == NULL
18386 || die->die_parent->die_tag != DW_TAG_class_type)
18387 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18389 else if (dwarf_version > 2
18390 && die->die_parent
18391 && die->die_parent->die_tag == DW_TAG_class_type)
18392 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18395 /* Attach the specialized form of location attribute used for data members of
18396 struct and union types. In the special case of a FIELD_DECL node which
18397 represents a bit-field, the "offset" part of this special location
18398 descriptor must indicate the distance in bytes from the lowest-addressed
18399 byte of the containing struct or union type to the lowest-addressed byte of
18400 the "containing object" for the bit-field. (See the `field_byte_offset'
18401 function above).
18403 For any given bit-field, the "containing object" is a hypothetical object
18404 (of some integral or enum type) within which the given bit-field lives. The
18405 type of this hypothetical "containing object" is always the same as the
18406 declared type of the individual bit-field itself (for GCC anyway... the
18407 DWARF spec doesn't actually mandate this). Note that it is the size (in
18408 bytes) of the hypothetical "containing object" which will be given in the
18409 DW_AT_byte_size attribute for this bit-field. (See the
18410 `byte_size_attribute' function below.) It is also used when calculating the
18411 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18412 function below.)
18414 CTX is required: see the comment for VLR_CONTEXT. */
18416 static void
18417 add_data_member_location_attribute (dw_die_ref die,
18418 tree decl,
18419 struct vlr_context *ctx)
18421 HOST_WIDE_INT offset;
18422 dw_loc_descr_ref loc_descr = 0;
18424 if (TREE_CODE (decl) == TREE_BINFO)
18426 /* We're working on the TAG_inheritance for a base class. */
18427 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18429 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18430 aren't at a fixed offset from all (sub)objects of the same
18431 type. We need to extract the appropriate offset from our
18432 vtable. The following dwarf expression means
18434 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18436 This is specific to the V3 ABI, of course. */
18438 dw_loc_descr_ref tmp;
18440 /* Make a copy of the object address. */
18441 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18442 add_loc_descr (&loc_descr, tmp);
18444 /* Extract the vtable address. */
18445 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18446 add_loc_descr (&loc_descr, tmp);
18448 /* Calculate the address of the offset. */
18449 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18450 gcc_assert (offset < 0);
18452 tmp = int_loc_descriptor (-offset);
18453 add_loc_descr (&loc_descr, tmp);
18454 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18455 add_loc_descr (&loc_descr, tmp);
18457 /* Extract the offset. */
18458 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18459 add_loc_descr (&loc_descr, tmp);
18461 /* Add it to the object address. */
18462 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18463 add_loc_descr (&loc_descr, tmp);
18465 else
18466 offset = tree_to_shwi (BINFO_OFFSET (decl));
18468 else
18470 loc_descr = field_byte_offset (decl, ctx, &offset);
18472 /* If loc_descr is available then we know the field offset is dynamic.
18473 However, GDB does not handle dynamic field offsets very well at the
18474 moment. */
18475 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18477 loc_descr = NULL;
18478 offset = 0;
18481 /* Data member location evalutation starts with the base address on the
18482 stack. Compute the field offset and add it to this base address. */
18483 else if (loc_descr != NULL)
18484 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18487 if (! loc_descr)
18489 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18490 e.g. GDB only added support to it in November 2016. For DWARF5
18491 we need newer debug info consumers anyway. We might change this
18492 to dwarf_version >= 4 once most consumers catched up. */
18493 if (dwarf_version >= 5
18494 && TREE_CODE (decl) == FIELD_DECL
18495 && DECL_BIT_FIELD_TYPE (decl))
18497 tree off = bit_position (decl);
18498 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18500 remove_AT (die, DW_AT_byte_size);
18501 remove_AT (die, DW_AT_bit_offset);
18502 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18503 return;
18506 if (dwarf_version > 2)
18508 /* Don't need to output a location expression, just the constant. */
18509 if (offset < 0)
18510 add_AT_int (die, DW_AT_data_member_location, offset);
18511 else
18512 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18513 return;
18515 else
18517 enum dwarf_location_atom op;
18519 /* The DWARF2 standard says that we should assume that the structure
18520 address is already on the stack, so we can specify a structure
18521 field address by using DW_OP_plus_uconst. */
18522 op = DW_OP_plus_uconst;
18523 loc_descr = new_loc_descr (op, offset, 0);
18527 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18530 /* Writes integer values to dw_vec_const array. */
18532 static void
18533 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18535 while (size != 0)
18537 *dest++ = val & 0xff;
18538 val >>= 8;
18539 --size;
18543 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18545 static HOST_WIDE_INT
18546 extract_int (const unsigned char *src, unsigned int size)
18548 HOST_WIDE_INT val = 0;
18550 src += size;
18551 while (size != 0)
18553 val <<= 8;
18554 val |= *--src & 0xff;
18555 --size;
18557 return val;
18560 /* Writes wide_int values to dw_vec_const array. */
18562 static void
18563 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18565 int i;
18567 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18569 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18570 return;
18573 /* We'd have to extend this code to support odd sizes. */
18574 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18576 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18578 if (WORDS_BIG_ENDIAN)
18579 for (i = n - 1; i >= 0; i--)
18581 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18582 dest += sizeof (HOST_WIDE_INT);
18584 else
18585 for (i = 0; i < n; i++)
18587 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18588 dest += sizeof (HOST_WIDE_INT);
18592 /* Writes floating point values to dw_vec_const array. */
18594 static void
18595 insert_float (const_rtx rtl, unsigned char *array)
18597 long val[4];
18598 int i;
18599 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18601 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18603 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18604 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18606 insert_int (val[i], 4, array);
18607 array += 4;
18611 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18612 does not have a "location" either in memory or in a register. These
18613 things can arise in GNU C when a constant is passed as an actual parameter
18614 to an inlined function. They can also arise in C++ where declared
18615 constants do not necessarily get memory "homes". */
18617 static bool
18618 add_const_value_attribute (dw_die_ref die, rtx rtl)
18620 switch (GET_CODE (rtl))
18622 case CONST_INT:
18624 HOST_WIDE_INT val = INTVAL (rtl);
18626 if (val < 0)
18627 add_AT_int (die, DW_AT_const_value, val);
18628 else
18629 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18631 return true;
18633 case CONST_WIDE_INT:
18635 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18636 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18637 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18638 wide_int w = wi::zext (w1, prec);
18639 add_AT_wide (die, DW_AT_const_value, w);
18641 return true;
18643 case CONST_DOUBLE:
18644 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18645 floating-point constant. A CONST_DOUBLE is used whenever the
18646 constant requires more than one word in order to be adequately
18647 represented. */
18648 if (TARGET_SUPPORTS_WIDE_INT == 0
18649 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18650 add_AT_double (die, DW_AT_const_value,
18651 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18652 else
18654 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18655 unsigned int length = GET_MODE_SIZE (mode);
18656 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18658 insert_float (rtl, array);
18659 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18661 return true;
18663 case CONST_VECTOR:
18665 machine_mode mode = GET_MODE (rtl);
18666 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18667 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18668 unsigned char *array
18669 = ggc_vec_alloc<unsigned char> (length * elt_size);
18670 unsigned int i;
18671 unsigned char *p;
18672 machine_mode imode = GET_MODE_INNER (mode);
18674 switch (GET_MODE_CLASS (mode))
18676 case MODE_VECTOR_INT:
18677 for (i = 0, p = array; i < length; i++, p += elt_size)
18679 rtx elt = CONST_VECTOR_ELT (rtl, i);
18680 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18682 break;
18684 case MODE_VECTOR_FLOAT:
18685 for (i = 0, p = array; i < length; i++, p += elt_size)
18687 rtx elt = CONST_VECTOR_ELT (rtl, i);
18688 insert_float (elt, p);
18690 break;
18692 default:
18693 gcc_unreachable ();
18696 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18698 return true;
18700 case CONST_STRING:
18701 if (dwarf_version >= 4 || !dwarf_strict)
18703 dw_loc_descr_ref loc_result;
18704 resolve_one_addr (&rtl);
18705 rtl_addr:
18706 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18707 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18708 add_AT_loc (die, DW_AT_location, loc_result);
18709 vec_safe_push (used_rtx_array, rtl);
18710 return true;
18712 return false;
18714 case CONST:
18715 if (CONSTANT_P (XEXP (rtl, 0)))
18716 return add_const_value_attribute (die, XEXP (rtl, 0));
18717 /* FALLTHROUGH */
18718 case SYMBOL_REF:
18719 if (!const_ok_for_output (rtl))
18720 return false;
18721 /* FALLTHROUGH */
18722 case LABEL_REF:
18723 if (dwarf_version >= 4 || !dwarf_strict)
18724 goto rtl_addr;
18725 return false;
18727 case PLUS:
18728 /* In cases where an inlined instance of an inline function is passed
18729 the address of an `auto' variable (which is local to the caller) we
18730 can get a situation where the DECL_RTL of the artificial local
18731 variable (for the inlining) which acts as a stand-in for the
18732 corresponding formal parameter (of the inline function) will look
18733 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18734 exactly a compile-time constant expression, but it isn't the address
18735 of the (artificial) local variable either. Rather, it represents the
18736 *value* which the artificial local variable always has during its
18737 lifetime. We currently have no way to represent such quasi-constant
18738 values in Dwarf, so for now we just punt and generate nothing. */
18739 return false;
18741 case HIGH:
18742 case CONST_FIXED:
18743 return false;
18745 case MEM:
18746 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18747 && MEM_READONLY_P (rtl)
18748 && GET_MODE (rtl) == BLKmode)
18750 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18751 return true;
18753 return false;
18755 default:
18756 /* No other kinds of rtx should be possible here. */
18757 gcc_unreachable ();
18759 return false;
18762 /* Determine whether the evaluation of EXPR references any variables
18763 or functions which aren't otherwise used (and therefore may not be
18764 output). */
18765 static tree
18766 reference_to_unused (tree * tp, int * walk_subtrees,
18767 void * data ATTRIBUTE_UNUSED)
18769 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18770 *walk_subtrees = 0;
18772 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18773 && ! TREE_ASM_WRITTEN (*tp))
18774 return *tp;
18775 /* ??? The C++ FE emits debug information for using decls, so
18776 putting gcc_unreachable here falls over. See PR31899. For now
18777 be conservative. */
18778 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18779 return *tp;
18780 else if (VAR_P (*tp))
18782 varpool_node *node = varpool_node::get (*tp);
18783 if (!node || !node->definition)
18784 return *tp;
18786 else if (TREE_CODE (*tp) == FUNCTION_DECL
18787 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18789 /* The call graph machinery must have finished analyzing,
18790 optimizing and gimplifying the CU by now.
18791 So if *TP has no call graph node associated
18792 to it, it means *TP will not be emitted. */
18793 if (!cgraph_node::get (*tp))
18794 return *tp;
18796 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18797 return *tp;
18799 return NULL_TREE;
18802 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18803 for use in a later add_const_value_attribute call. */
18805 static rtx
18806 rtl_for_decl_init (tree init, tree type)
18808 rtx rtl = NULL_RTX;
18810 STRIP_NOPS (init);
18812 /* If a variable is initialized with a string constant without embedded
18813 zeros, build CONST_STRING. */
18814 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18816 tree enttype = TREE_TYPE (type);
18817 tree domain = TYPE_DOMAIN (type);
18818 scalar_int_mode mode;
18820 if (is_int_mode (TYPE_MODE (enttype), &mode)
18821 && GET_MODE_SIZE (mode) == 1
18822 && domain
18823 && integer_zerop (TYPE_MIN_VALUE (domain))
18824 && compare_tree_int (TYPE_MAX_VALUE (domain),
18825 TREE_STRING_LENGTH (init) - 1) == 0
18826 && ((size_t) TREE_STRING_LENGTH (init)
18827 == strlen (TREE_STRING_POINTER (init)) + 1))
18829 rtl = gen_rtx_CONST_STRING (VOIDmode,
18830 ggc_strdup (TREE_STRING_POINTER (init)));
18831 rtl = gen_rtx_MEM (BLKmode, rtl);
18832 MEM_READONLY_P (rtl) = 1;
18835 /* Other aggregates, and complex values, could be represented using
18836 CONCAT: FIXME! */
18837 else if (AGGREGATE_TYPE_P (type)
18838 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18839 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18840 || TREE_CODE (type) == COMPLEX_TYPE)
18842 /* Vectors only work if their mode is supported by the target.
18843 FIXME: generic vectors ought to work too. */
18844 else if (TREE_CODE (type) == VECTOR_TYPE
18845 && !VECTOR_MODE_P (TYPE_MODE (type)))
18847 /* If the initializer is something that we know will expand into an
18848 immediate RTL constant, expand it now. We must be careful not to
18849 reference variables which won't be output. */
18850 else if (initializer_constant_valid_p (init, type)
18851 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18853 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18854 possible. */
18855 if (TREE_CODE (type) == VECTOR_TYPE)
18856 switch (TREE_CODE (init))
18858 case VECTOR_CST:
18859 break;
18860 case CONSTRUCTOR:
18861 if (TREE_CONSTANT (init))
18863 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18864 bool constant_p = true;
18865 tree value;
18866 unsigned HOST_WIDE_INT ix;
18868 /* Even when ctor is constant, it might contain non-*_CST
18869 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18870 belong into VECTOR_CST nodes. */
18871 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18872 if (!CONSTANT_CLASS_P (value))
18874 constant_p = false;
18875 break;
18878 if (constant_p)
18880 init = build_vector_from_ctor (type, elts);
18881 break;
18884 /* FALLTHRU */
18886 default:
18887 return NULL;
18890 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18892 /* If expand_expr returns a MEM, it wasn't immediate. */
18893 gcc_assert (!rtl || !MEM_P (rtl));
18896 return rtl;
18899 /* Generate RTL for the variable DECL to represent its location. */
18901 static rtx
18902 rtl_for_decl_location (tree decl)
18904 rtx rtl;
18906 /* Here we have to decide where we are going to say the parameter "lives"
18907 (as far as the debugger is concerned). We only have a couple of
18908 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18910 DECL_RTL normally indicates where the parameter lives during most of the
18911 activation of the function. If optimization is enabled however, this
18912 could be either NULL or else a pseudo-reg. Both of those cases indicate
18913 that the parameter doesn't really live anywhere (as far as the code
18914 generation parts of GCC are concerned) during most of the function's
18915 activation. That will happen (for example) if the parameter is never
18916 referenced within the function.
18918 We could just generate a location descriptor here for all non-NULL
18919 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18920 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18921 where DECL_RTL is NULL or is a pseudo-reg.
18923 Note however that we can only get away with using DECL_INCOMING_RTL as
18924 a backup substitute for DECL_RTL in certain limited cases. In cases
18925 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18926 we can be sure that the parameter was passed using the same type as it is
18927 declared to have within the function, and that its DECL_INCOMING_RTL
18928 points us to a place where a value of that type is passed.
18930 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18931 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18932 because in these cases DECL_INCOMING_RTL points us to a value of some
18933 type which is *different* from the type of the parameter itself. Thus,
18934 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18935 such cases, the debugger would end up (for example) trying to fetch a
18936 `float' from a place which actually contains the first part of a
18937 `double'. That would lead to really incorrect and confusing
18938 output at debug-time.
18940 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18941 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18942 are a couple of exceptions however. On little-endian machines we can
18943 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18944 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18945 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18946 when (on a little-endian machine) a non-prototyped function has a
18947 parameter declared to be of type `short' or `char'. In such cases,
18948 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18949 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18950 passed `int' value. If the debugger then uses that address to fetch
18951 a `short' or a `char' (on a little-endian machine) the result will be
18952 the correct data, so we allow for such exceptional cases below.
18954 Note that our goal here is to describe the place where the given formal
18955 parameter lives during most of the function's activation (i.e. between the
18956 end of the prologue and the start of the epilogue). We'll do that as best
18957 as we can. Note however that if the given formal parameter is modified
18958 sometime during the execution of the function, then a stack backtrace (at
18959 debug-time) will show the function as having been called with the *new*
18960 value rather than the value which was originally passed in. This happens
18961 rarely enough that it is not a major problem, but it *is* a problem, and
18962 I'd like to fix it.
18964 A future version of dwarf2out.c may generate two additional attributes for
18965 any given DW_TAG_formal_parameter DIE which will describe the "passed
18966 type" and the "passed location" for the given formal parameter in addition
18967 to the attributes we now generate to indicate the "declared type" and the
18968 "active location" for each parameter. This additional set of attributes
18969 could be used by debuggers for stack backtraces. Separately, note that
18970 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18971 This happens (for example) for inlined-instances of inline function formal
18972 parameters which are never referenced. This really shouldn't be
18973 happening. All PARM_DECL nodes should get valid non-NULL
18974 DECL_INCOMING_RTL values. FIXME. */
18976 /* Use DECL_RTL as the "location" unless we find something better. */
18977 rtl = DECL_RTL_IF_SET (decl);
18979 /* When generating abstract instances, ignore everything except
18980 constants, symbols living in memory, and symbols living in
18981 fixed registers. */
18982 if (! reload_completed)
18984 if (rtl
18985 && (CONSTANT_P (rtl)
18986 || (MEM_P (rtl)
18987 && CONSTANT_P (XEXP (rtl, 0)))
18988 || (REG_P (rtl)
18989 && VAR_P (decl)
18990 && TREE_STATIC (decl))))
18992 rtl = targetm.delegitimize_address (rtl);
18993 return rtl;
18995 rtl = NULL_RTX;
18997 else if (TREE_CODE (decl) == PARM_DECL)
18999 if (rtl == NULL_RTX
19000 || is_pseudo_reg (rtl)
19001 || (MEM_P (rtl)
19002 && is_pseudo_reg (XEXP (rtl, 0))
19003 && DECL_INCOMING_RTL (decl)
19004 && MEM_P (DECL_INCOMING_RTL (decl))
19005 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19007 tree declared_type = TREE_TYPE (decl);
19008 tree passed_type = DECL_ARG_TYPE (decl);
19009 machine_mode dmode = TYPE_MODE (declared_type);
19010 machine_mode pmode = TYPE_MODE (passed_type);
19012 /* This decl represents a formal parameter which was optimized out.
19013 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19014 all cases where (rtl == NULL_RTX) just below. */
19015 if (dmode == pmode)
19016 rtl = DECL_INCOMING_RTL (decl);
19017 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19018 && SCALAR_INT_MODE_P (dmode)
19019 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19020 && DECL_INCOMING_RTL (decl))
19022 rtx inc = DECL_INCOMING_RTL (decl);
19023 if (REG_P (inc))
19024 rtl = inc;
19025 else if (MEM_P (inc))
19027 if (BYTES_BIG_ENDIAN)
19028 rtl = adjust_address_nv (inc, dmode,
19029 GET_MODE_SIZE (pmode)
19030 - GET_MODE_SIZE (dmode));
19031 else
19032 rtl = inc;
19037 /* If the parm was passed in registers, but lives on the stack, then
19038 make a big endian correction if the mode of the type of the
19039 parameter is not the same as the mode of the rtl. */
19040 /* ??? This is the same series of checks that are made in dbxout.c before
19041 we reach the big endian correction code there. It isn't clear if all
19042 of these checks are necessary here, but keeping them all is the safe
19043 thing to do. */
19044 else if (MEM_P (rtl)
19045 && XEXP (rtl, 0) != const0_rtx
19046 && ! CONSTANT_P (XEXP (rtl, 0))
19047 /* Not passed in memory. */
19048 && !MEM_P (DECL_INCOMING_RTL (decl))
19049 /* Not passed by invisible reference. */
19050 && (!REG_P (XEXP (rtl, 0))
19051 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19052 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19053 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19054 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19055 #endif
19057 /* Big endian correction check. */
19058 && BYTES_BIG_ENDIAN
19059 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19060 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19061 < UNITS_PER_WORD))
19063 machine_mode addr_mode = get_address_mode (rtl);
19064 int offset = (UNITS_PER_WORD
19065 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19067 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19068 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19071 else if (VAR_P (decl)
19072 && rtl
19073 && MEM_P (rtl)
19074 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
19075 && BYTES_BIG_ENDIAN)
19077 machine_mode addr_mode = get_address_mode (rtl);
19078 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
19079 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
19081 /* If a variable is declared "register" yet is smaller than
19082 a register, then if we store the variable to memory, it
19083 looks like we're storing a register-sized value, when in
19084 fact we are not. We need to adjust the offset of the
19085 storage location to reflect the actual value's bytes,
19086 else gdb will not be able to display it. */
19087 if (rsize > dsize)
19088 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19089 plus_constant (addr_mode, XEXP (rtl, 0),
19090 rsize - dsize));
19093 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19094 and will have been substituted directly into all expressions that use it.
19095 C does not have such a concept, but C++ and other languages do. */
19096 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19097 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19099 if (rtl)
19100 rtl = targetm.delegitimize_address (rtl);
19102 /* If we don't look past the constant pool, we risk emitting a
19103 reference to a constant pool entry that isn't referenced from
19104 code, and thus is not emitted. */
19105 if (rtl)
19106 rtl = avoid_constant_pool_reference (rtl);
19108 /* Try harder to get a rtl. If this symbol ends up not being emitted
19109 in the current CU, resolve_addr will remove the expression referencing
19110 it. */
19111 if (rtl == NULL_RTX
19112 && VAR_P (decl)
19113 && !DECL_EXTERNAL (decl)
19114 && TREE_STATIC (decl)
19115 && DECL_NAME (decl)
19116 && !DECL_HARD_REGISTER (decl)
19117 && DECL_MODE (decl) != VOIDmode)
19119 rtl = make_decl_rtl_for_debug (decl);
19120 if (!MEM_P (rtl)
19121 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19122 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19123 rtl = NULL_RTX;
19126 return rtl;
19129 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19130 returned. If so, the decl for the COMMON block is returned, and the
19131 value is the offset into the common block for the symbol. */
19133 static tree
19134 fortran_common (tree decl, HOST_WIDE_INT *value)
19136 tree val_expr, cvar;
19137 machine_mode mode;
19138 HOST_WIDE_INT bitsize, bitpos;
19139 tree offset;
19140 int unsignedp, reversep, volatilep = 0;
19142 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19143 it does not have a value (the offset into the common area), or if it
19144 is thread local (as opposed to global) then it isn't common, and shouldn't
19145 be handled as such. */
19146 if (!VAR_P (decl)
19147 || !TREE_STATIC (decl)
19148 || !DECL_HAS_VALUE_EXPR_P (decl)
19149 || !is_fortran ())
19150 return NULL_TREE;
19152 val_expr = DECL_VALUE_EXPR (decl);
19153 if (TREE_CODE (val_expr) != COMPONENT_REF)
19154 return NULL_TREE;
19156 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19157 &unsignedp, &reversep, &volatilep);
19159 if (cvar == NULL_TREE
19160 || !VAR_P (cvar)
19161 || DECL_ARTIFICIAL (cvar)
19162 || !TREE_PUBLIC (cvar))
19163 return NULL_TREE;
19165 *value = 0;
19166 if (offset != NULL)
19168 if (!tree_fits_shwi_p (offset))
19169 return NULL_TREE;
19170 *value = tree_to_shwi (offset);
19172 if (bitpos != 0)
19173 *value += bitpos / BITS_PER_UNIT;
19175 return cvar;
19178 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19179 data attribute for a variable or a parameter. We generate the
19180 DW_AT_const_value attribute only in those cases where the given variable
19181 or parameter does not have a true "location" either in memory or in a
19182 register. This can happen (for example) when a constant is passed as an
19183 actual argument in a call to an inline function. (It's possible that
19184 these things can crop up in other ways also.) Note that one type of
19185 constant value which can be passed into an inlined function is a constant
19186 pointer. This can happen for example if an actual argument in an inlined
19187 function call evaluates to a compile-time constant address.
19189 CACHE_P is true if it is worth caching the location list for DECL,
19190 so that future calls can reuse it rather than regenerate it from scratch.
19191 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19192 since we will need to refer to them each time the function is inlined. */
19194 static bool
19195 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19197 rtx rtl;
19198 dw_loc_list_ref list;
19199 var_loc_list *loc_list;
19200 cached_dw_loc_list *cache;
19202 if (early_dwarf)
19203 return false;
19205 if (TREE_CODE (decl) == ERROR_MARK)
19206 return false;
19208 if (get_AT (die, DW_AT_location)
19209 || get_AT (die, DW_AT_const_value))
19210 return true;
19212 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19213 || TREE_CODE (decl) == RESULT_DECL);
19215 /* Try to get some constant RTL for this decl, and use that as the value of
19216 the location. */
19218 rtl = rtl_for_decl_location (decl);
19219 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19220 && add_const_value_attribute (die, rtl))
19221 return true;
19223 /* See if we have single element location list that is equivalent to
19224 a constant value. That way we are better to use add_const_value_attribute
19225 rather than expanding constant value equivalent. */
19226 loc_list = lookup_decl_loc (decl);
19227 if (loc_list
19228 && loc_list->first
19229 && loc_list->first->next == NULL
19230 && NOTE_P (loc_list->first->loc)
19231 && NOTE_VAR_LOCATION (loc_list->first->loc)
19232 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19234 struct var_loc_node *node;
19236 node = loc_list->first;
19237 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19238 if (GET_CODE (rtl) == EXPR_LIST)
19239 rtl = XEXP (rtl, 0);
19240 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19241 && add_const_value_attribute (die, rtl))
19242 return true;
19244 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19245 list several times. See if we've already cached the contents. */
19246 list = NULL;
19247 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19248 cache_p = false;
19249 if (cache_p)
19251 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19252 if (cache)
19253 list = cache->loc_list;
19255 if (list == NULL)
19257 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19258 NULL);
19259 /* It is usually worth caching this result if the decl is from
19260 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19261 if (cache_p && list && list->dw_loc_next)
19263 cached_dw_loc_list **slot
19264 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19265 DECL_UID (decl),
19266 INSERT);
19267 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19268 cache->decl_id = DECL_UID (decl);
19269 cache->loc_list = list;
19270 *slot = cache;
19273 if (list)
19275 add_AT_location_description (die, DW_AT_location, list);
19276 return true;
19278 /* None of that worked, so it must not really have a location;
19279 try adding a constant value attribute from the DECL_INITIAL. */
19280 return tree_add_const_value_attribute_for_decl (die, decl);
19283 /* Helper function for tree_add_const_value_attribute. Natively encode
19284 initializer INIT into an array. Return true if successful. */
19286 static bool
19287 native_encode_initializer (tree init, unsigned char *array, int size)
19289 tree type;
19291 if (init == NULL_TREE)
19292 return false;
19294 STRIP_NOPS (init);
19295 switch (TREE_CODE (init))
19297 case STRING_CST:
19298 type = TREE_TYPE (init);
19299 if (TREE_CODE (type) == ARRAY_TYPE)
19301 tree enttype = TREE_TYPE (type);
19302 scalar_int_mode mode;
19304 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19305 || GET_MODE_SIZE (mode) != 1)
19306 return false;
19307 if (int_size_in_bytes (type) != size)
19308 return false;
19309 if (size > TREE_STRING_LENGTH (init))
19311 memcpy (array, TREE_STRING_POINTER (init),
19312 TREE_STRING_LENGTH (init));
19313 memset (array + TREE_STRING_LENGTH (init),
19314 '\0', size - TREE_STRING_LENGTH (init));
19316 else
19317 memcpy (array, TREE_STRING_POINTER (init), size);
19318 return true;
19320 return false;
19321 case CONSTRUCTOR:
19322 type = TREE_TYPE (init);
19323 if (int_size_in_bytes (type) != size)
19324 return false;
19325 if (TREE_CODE (type) == ARRAY_TYPE)
19327 HOST_WIDE_INT min_index;
19328 unsigned HOST_WIDE_INT cnt;
19329 int curpos = 0, fieldsize;
19330 constructor_elt *ce;
19332 if (TYPE_DOMAIN (type) == NULL_TREE
19333 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19334 return false;
19336 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19337 if (fieldsize <= 0)
19338 return false;
19340 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19341 memset (array, '\0', size);
19342 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19344 tree val = ce->value;
19345 tree index = ce->index;
19346 int pos = curpos;
19347 if (index && TREE_CODE (index) == RANGE_EXPR)
19348 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19349 * fieldsize;
19350 else if (index)
19351 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19353 if (val)
19355 STRIP_NOPS (val);
19356 if (!native_encode_initializer (val, array + pos, fieldsize))
19357 return false;
19359 curpos = pos + fieldsize;
19360 if (index && TREE_CODE (index) == RANGE_EXPR)
19362 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19363 - tree_to_shwi (TREE_OPERAND (index, 0));
19364 while (count-- > 0)
19366 if (val)
19367 memcpy (array + curpos, array + pos, fieldsize);
19368 curpos += fieldsize;
19371 gcc_assert (curpos <= size);
19373 return true;
19375 else if (TREE_CODE (type) == RECORD_TYPE
19376 || TREE_CODE (type) == UNION_TYPE)
19378 tree field = NULL_TREE;
19379 unsigned HOST_WIDE_INT cnt;
19380 constructor_elt *ce;
19382 if (int_size_in_bytes (type) != size)
19383 return false;
19385 if (TREE_CODE (type) == RECORD_TYPE)
19386 field = TYPE_FIELDS (type);
19388 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19390 tree val = ce->value;
19391 int pos, fieldsize;
19393 if (ce->index != 0)
19394 field = ce->index;
19396 if (val)
19397 STRIP_NOPS (val);
19399 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19400 return false;
19402 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19403 && TYPE_DOMAIN (TREE_TYPE (field))
19404 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19405 return false;
19406 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19407 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19408 return false;
19409 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19410 pos = int_byte_position (field);
19411 gcc_assert (pos + fieldsize <= size);
19412 if (val && fieldsize != 0
19413 && !native_encode_initializer (val, array + pos, fieldsize))
19414 return false;
19416 return true;
19418 return false;
19419 case VIEW_CONVERT_EXPR:
19420 case NON_LVALUE_EXPR:
19421 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19422 default:
19423 return native_encode_expr (init, array, size) == size;
19427 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19428 attribute is the const value T. */
19430 static bool
19431 tree_add_const_value_attribute (dw_die_ref die, tree t)
19433 tree init;
19434 tree type = TREE_TYPE (t);
19435 rtx rtl;
19437 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19438 return false;
19440 init = t;
19441 gcc_assert (!DECL_P (init));
19443 if (! early_dwarf)
19445 rtl = rtl_for_decl_init (init, type);
19446 if (rtl)
19447 return add_const_value_attribute (die, rtl);
19449 /* If the host and target are sane, try harder. */
19450 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19451 && initializer_constant_valid_p (init, type))
19453 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19454 if (size > 0 && (int) size == size)
19456 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19458 if (native_encode_initializer (init, array, size))
19460 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19461 return true;
19463 ggc_free (array);
19466 return false;
19469 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19470 attribute is the const value of T, where T is an integral constant
19471 variable with static storage duration
19472 (so it can't be a PARM_DECL or a RESULT_DECL). */
19474 static bool
19475 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19478 if (!decl
19479 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19480 || (VAR_P (decl) && !TREE_STATIC (decl)))
19481 return false;
19483 if (TREE_READONLY (decl)
19484 && ! TREE_THIS_VOLATILE (decl)
19485 && DECL_INITIAL (decl))
19486 /* OK */;
19487 else
19488 return false;
19490 /* Don't add DW_AT_const_value if abstract origin already has one. */
19491 if (get_AT (var_die, DW_AT_const_value))
19492 return false;
19494 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19497 /* Convert the CFI instructions for the current function into a
19498 location list. This is used for DW_AT_frame_base when we targeting
19499 a dwarf2 consumer that does not support the dwarf3
19500 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19501 expressions. */
19503 static dw_loc_list_ref
19504 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19506 int ix;
19507 dw_fde_ref fde;
19508 dw_loc_list_ref list, *list_tail;
19509 dw_cfi_ref cfi;
19510 dw_cfa_location last_cfa, next_cfa;
19511 const char *start_label, *last_label, *section;
19512 dw_cfa_location remember;
19514 fde = cfun->fde;
19515 gcc_assert (fde != NULL);
19517 section = secname_for_decl (current_function_decl);
19518 list_tail = &list;
19519 list = NULL;
19521 memset (&next_cfa, 0, sizeof (next_cfa));
19522 next_cfa.reg = INVALID_REGNUM;
19523 remember = next_cfa;
19525 start_label = fde->dw_fde_begin;
19527 /* ??? Bald assumption that the CIE opcode list does not contain
19528 advance opcodes. */
19529 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19530 lookup_cfa_1 (cfi, &next_cfa, &remember);
19532 last_cfa = next_cfa;
19533 last_label = start_label;
19535 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19537 /* If the first partition contained no CFI adjustments, the
19538 CIE opcodes apply to the whole first partition. */
19539 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19540 fde->dw_fde_begin, fde->dw_fde_end, section);
19541 list_tail =&(*list_tail)->dw_loc_next;
19542 start_label = last_label = fde->dw_fde_second_begin;
19545 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19547 switch (cfi->dw_cfi_opc)
19549 case DW_CFA_set_loc:
19550 case DW_CFA_advance_loc1:
19551 case DW_CFA_advance_loc2:
19552 case DW_CFA_advance_loc4:
19553 if (!cfa_equal_p (&last_cfa, &next_cfa))
19555 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19556 start_label, last_label, section);
19558 list_tail = &(*list_tail)->dw_loc_next;
19559 last_cfa = next_cfa;
19560 start_label = last_label;
19562 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19563 break;
19565 case DW_CFA_advance_loc:
19566 /* The encoding is complex enough that we should never emit this. */
19567 gcc_unreachable ();
19569 default:
19570 lookup_cfa_1 (cfi, &next_cfa, &remember);
19571 break;
19573 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19575 if (!cfa_equal_p (&last_cfa, &next_cfa))
19577 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19578 start_label, last_label, section);
19580 list_tail = &(*list_tail)->dw_loc_next;
19581 last_cfa = next_cfa;
19582 start_label = last_label;
19584 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19585 start_label, fde->dw_fde_end, section);
19586 list_tail = &(*list_tail)->dw_loc_next;
19587 start_label = last_label = fde->dw_fde_second_begin;
19591 if (!cfa_equal_p (&last_cfa, &next_cfa))
19593 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19594 start_label, last_label, section);
19595 list_tail = &(*list_tail)->dw_loc_next;
19596 start_label = last_label;
19599 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19600 start_label,
19601 fde->dw_fde_second_begin
19602 ? fde->dw_fde_second_end : fde->dw_fde_end,
19603 section);
19605 if (list && list->dw_loc_next)
19606 gen_llsym (list);
19608 return list;
19611 /* Compute a displacement from the "steady-state frame pointer" to the
19612 frame base (often the same as the CFA), and store it in
19613 frame_pointer_fb_offset. OFFSET is added to the displacement
19614 before the latter is negated. */
19616 static void
19617 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19619 rtx reg, elim;
19621 #ifdef FRAME_POINTER_CFA_OFFSET
19622 reg = frame_pointer_rtx;
19623 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19624 #else
19625 reg = arg_pointer_rtx;
19626 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19627 #endif
19629 elim = (ira_use_lra_p
19630 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19631 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19632 if (GET_CODE (elim) == PLUS)
19634 offset += INTVAL (XEXP (elim, 1));
19635 elim = XEXP (elim, 0);
19638 frame_pointer_fb_offset = -offset;
19640 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19641 in which to eliminate. This is because it's stack pointer isn't
19642 directly accessible as a register within the ISA. To work around
19643 this, assume that while we cannot provide a proper value for
19644 frame_pointer_fb_offset, we won't need one either. */
19645 frame_pointer_fb_offset_valid
19646 = ((SUPPORTS_STACK_ALIGNMENT
19647 && (elim == hard_frame_pointer_rtx
19648 || elim == stack_pointer_rtx))
19649 || elim == (frame_pointer_needed
19650 ? hard_frame_pointer_rtx
19651 : stack_pointer_rtx));
19654 /* Generate a DW_AT_name attribute given some string value to be included as
19655 the value of the attribute. */
19657 static void
19658 add_name_attribute (dw_die_ref die, const char *name_string)
19660 if (name_string != NULL && *name_string != 0)
19662 if (demangle_name_func)
19663 name_string = (*demangle_name_func) (name_string);
19665 add_AT_string (die, DW_AT_name, name_string);
19669 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19670 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19671 of TYPE accordingly.
19673 ??? This is a temporary measure until after we're able to generate
19674 regular DWARF for the complex Ada type system. */
19676 static void
19677 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19678 dw_die_ref context_die)
19680 tree dtype;
19681 dw_die_ref dtype_die;
19683 if (!lang_hooks.types.descriptive_type)
19684 return;
19686 dtype = lang_hooks.types.descriptive_type (type);
19687 if (!dtype)
19688 return;
19690 dtype_die = lookup_type_die (dtype);
19691 if (!dtype_die)
19693 gen_type_die (dtype, context_die);
19694 dtype_die = lookup_type_die (dtype);
19695 gcc_assert (dtype_die);
19698 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19701 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19703 static const char *
19704 comp_dir_string (void)
19706 const char *wd;
19707 char *wd1;
19708 static const char *cached_wd = NULL;
19710 if (cached_wd != NULL)
19711 return cached_wd;
19713 wd = get_src_pwd ();
19714 if (wd == NULL)
19715 return NULL;
19717 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19719 int wdlen;
19721 wdlen = strlen (wd);
19722 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19723 strcpy (wd1, wd);
19724 wd1 [wdlen] = DIR_SEPARATOR;
19725 wd1 [wdlen + 1] = 0;
19726 wd = wd1;
19729 cached_wd = remap_debug_filename (wd);
19730 return cached_wd;
19733 /* Generate a DW_AT_comp_dir attribute for DIE. */
19735 static void
19736 add_comp_dir_attribute (dw_die_ref die)
19738 const char * wd = comp_dir_string ();
19739 if (wd != NULL)
19740 add_AT_string (die, DW_AT_comp_dir, wd);
19743 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19744 pointer computation, ...), output a representation for that bound according
19745 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19746 loc_list_from_tree for the meaning of CONTEXT. */
19748 static void
19749 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19750 int forms, struct loc_descr_context *context)
19752 dw_die_ref context_die, decl_die;
19753 dw_loc_list_ref list;
19754 bool strip_conversions = true;
19755 bool placeholder_seen = false;
19757 while (strip_conversions)
19758 switch (TREE_CODE (value))
19760 case ERROR_MARK:
19761 case SAVE_EXPR:
19762 return;
19764 CASE_CONVERT:
19765 case VIEW_CONVERT_EXPR:
19766 value = TREE_OPERAND (value, 0);
19767 break;
19769 default:
19770 strip_conversions = false;
19771 break;
19774 /* If possible and permitted, output the attribute as a constant. */
19775 if ((forms & dw_scalar_form_constant) != 0
19776 && TREE_CODE (value) == INTEGER_CST)
19778 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19780 /* If HOST_WIDE_INT is big enough then represent the bound as
19781 a constant value. We need to choose a form based on
19782 whether the type is signed or unsigned. We cannot just
19783 call add_AT_unsigned if the value itself is positive
19784 (add_AT_unsigned might add the unsigned value encoded as
19785 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19786 bounds type and then sign extend any unsigned values found
19787 for signed types. This is needed only for
19788 DW_AT_{lower,upper}_bound, since for most other attributes,
19789 consumers will treat DW_FORM_data[1248] as unsigned values,
19790 regardless of the underlying type. */
19791 if (prec <= HOST_BITS_PER_WIDE_INT
19792 || tree_fits_uhwi_p (value))
19794 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19795 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19796 else
19797 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19799 else
19800 /* Otherwise represent the bound as an unsigned value with
19801 the precision of its type. The precision and signedness
19802 of the type will be necessary to re-interpret it
19803 unambiguously. */
19804 add_AT_wide (die, attr, value);
19805 return;
19808 /* Otherwise, if it's possible and permitted too, output a reference to
19809 another DIE. */
19810 if ((forms & dw_scalar_form_reference) != 0)
19812 tree decl = NULL_TREE;
19814 /* Some type attributes reference an outer type. For instance, the upper
19815 bound of an array may reference an embedding record (this happens in
19816 Ada). */
19817 if (TREE_CODE (value) == COMPONENT_REF
19818 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19819 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19820 decl = TREE_OPERAND (value, 1);
19822 else if (VAR_P (value)
19823 || TREE_CODE (value) == PARM_DECL
19824 || TREE_CODE (value) == RESULT_DECL)
19825 decl = value;
19827 if (decl != NULL_TREE)
19829 dw_die_ref decl_die = lookup_decl_die (decl);
19831 /* ??? Can this happen, or should the variable have been bound
19832 first? Probably it can, since I imagine that we try to create
19833 the types of parameters in the order in which they exist in
19834 the list, and won't have created a forward reference to a
19835 later parameter. */
19836 if (decl_die != NULL)
19838 add_AT_die_ref (die, attr, decl_die);
19839 return;
19844 /* Last chance: try to create a stack operation procedure to evaluate the
19845 value. Do nothing if even that is not possible or permitted. */
19846 if ((forms & dw_scalar_form_exprloc) == 0)
19847 return;
19849 list = loc_list_from_tree (value, 2, context);
19850 if (context && context->placeholder_arg)
19852 placeholder_seen = context->placeholder_seen;
19853 context->placeholder_seen = false;
19855 if (list == NULL || single_element_loc_list_p (list))
19857 /* If this attribute is not a reference nor constant, it is
19858 a DWARF expression rather than location description. For that
19859 loc_list_from_tree (value, 0, &context) is needed. */
19860 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19861 if (list2 && single_element_loc_list_p (list2))
19863 if (placeholder_seen)
19865 struct dwarf_procedure_info dpi;
19866 dpi.fndecl = NULL_TREE;
19867 dpi.args_count = 1;
19868 if (!resolve_args_picking (list2->expr, 1, &dpi))
19869 return;
19871 add_AT_loc (die, attr, list2->expr);
19872 return;
19876 /* If that failed to give a single element location list, fall back to
19877 outputting this as a reference... still if permitted. */
19878 if (list == NULL
19879 || (forms & dw_scalar_form_reference) == 0
19880 || placeholder_seen)
19881 return;
19883 if (current_function_decl == 0)
19884 context_die = comp_unit_die ();
19885 else
19886 context_die = lookup_decl_die (current_function_decl);
19888 decl_die = new_die (DW_TAG_variable, context_die, value);
19889 add_AT_flag (decl_die, DW_AT_artificial, 1);
19890 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19891 context_die);
19892 add_AT_location_description (decl_die, DW_AT_location, list);
19893 add_AT_die_ref (die, attr, decl_die);
19896 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19897 default. */
19899 static int
19900 lower_bound_default (void)
19902 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19904 case DW_LANG_C:
19905 case DW_LANG_C89:
19906 case DW_LANG_C99:
19907 case DW_LANG_C11:
19908 case DW_LANG_C_plus_plus:
19909 case DW_LANG_C_plus_plus_11:
19910 case DW_LANG_C_plus_plus_14:
19911 case DW_LANG_ObjC:
19912 case DW_LANG_ObjC_plus_plus:
19913 return 0;
19914 case DW_LANG_Fortran77:
19915 case DW_LANG_Fortran90:
19916 case DW_LANG_Fortran95:
19917 case DW_LANG_Fortran03:
19918 case DW_LANG_Fortran08:
19919 return 1;
19920 case DW_LANG_UPC:
19921 case DW_LANG_D:
19922 case DW_LANG_Python:
19923 return dwarf_version >= 4 ? 0 : -1;
19924 case DW_LANG_Ada95:
19925 case DW_LANG_Ada83:
19926 case DW_LANG_Cobol74:
19927 case DW_LANG_Cobol85:
19928 case DW_LANG_Modula2:
19929 case DW_LANG_PLI:
19930 return dwarf_version >= 4 ? 1 : -1;
19931 default:
19932 return -1;
19936 /* Given a tree node describing an array bound (either lower or upper) output
19937 a representation for that bound. */
19939 static void
19940 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19941 tree bound, struct loc_descr_context *context)
19943 int dflt;
19945 while (1)
19946 switch (TREE_CODE (bound))
19948 /* Strip all conversions. */
19949 CASE_CONVERT:
19950 case VIEW_CONVERT_EXPR:
19951 bound = TREE_OPERAND (bound, 0);
19952 break;
19954 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19955 are even omitted when they are the default. */
19956 case INTEGER_CST:
19957 /* If the value for this bound is the default one, we can even omit the
19958 attribute. */
19959 if (bound_attr == DW_AT_lower_bound
19960 && tree_fits_shwi_p (bound)
19961 && (dflt = lower_bound_default ()) != -1
19962 && tree_to_shwi (bound) == dflt)
19963 return;
19965 /* FALLTHRU */
19967 default:
19968 /* Because of the complex interaction there can be with other GNAT
19969 encodings, GDB isn't ready yet to handle proper DWARF description
19970 for self-referencial subrange bounds: let GNAT encodings do the
19971 magic in such a case. */
19972 if (is_ada ()
19973 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19974 && contains_placeholder_p (bound))
19975 return;
19977 add_scalar_info (subrange_die, bound_attr, bound,
19978 dw_scalar_form_constant
19979 | dw_scalar_form_exprloc
19980 | dw_scalar_form_reference,
19981 context);
19982 return;
19986 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19987 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19988 Note that the block of subscript information for an array type also
19989 includes information about the element type of the given array type.
19991 This function reuses previously set type and bound information if
19992 available. */
19994 static void
19995 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19997 unsigned dimension_number;
19998 tree lower, upper;
19999 dw_die_ref child = type_die->die_child;
20001 for (dimension_number = 0;
20002 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20003 type = TREE_TYPE (type), dimension_number++)
20005 tree domain = TYPE_DOMAIN (type);
20007 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20008 break;
20010 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20011 and (in GNU C only) variable bounds. Handle all three forms
20012 here. */
20014 /* Find and reuse a previously generated DW_TAG_subrange_type if
20015 available.
20017 For multi-dimensional arrays, as we iterate through the
20018 various dimensions in the enclosing for loop above, we also
20019 iterate through the DIE children and pick at each
20020 DW_TAG_subrange_type previously generated (if available).
20021 Each child DW_TAG_subrange_type DIE describes the range of
20022 the current dimension. At this point we should have as many
20023 DW_TAG_subrange_type's as we have dimensions in the
20024 array. */
20025 dw_die_ref subrange_die = NULL;
20026 if (child)
20027 while (1)
20029 child = child->die_sib;
20030 if (child->die_tag == DW_TAG_subrange_type)
20031 subrange_die = child;
20032 if (child == type_die->die_child)
20034 /* If we wrapped around, stop looking next time. */
20035 child = NULL;
20036 break;
20038 if (child->die_tag == DW_TAG_subrange_type)
20039 break;
20041 if (!subrange_die)
20042 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20044 if (domain)
20046 /* We have an array type with specified bounds. */
20047 lower = TYPE_MIN_VALUE (domain);
20048 upper = TYPE_MAX_VALUE (domain);
20050 /* Define the index type. */
20051 if (TREE_TYPE (domain)
20052 && !get_AT (subrange_die, DW_AT_type))
20054 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20055 TREE_TYPE field. We can't emit debug info for this
20056 because it is an unnamed integral type. */
20057 if (TREE_CODE (domain) == INTEGER_TYPE
20058 && TYPE_NAME (domain) == NULL_TREE
20059 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20060 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20062 else
20063 add_type_attribute (subrange_die, TREE_TYPE (domain),
20064 TYPE_UNQUALIFIED, false, type_die);
20067 /* ??? If upper is NULL, the array has unspecified length,
20068 but it does have a lower bound. This happens with Fortran
20069 dimension arr(N:*)
20070 Since the debugger is definitely going to need to know N
20071 to produce useful results, go ahead and output the lower
20072 bound solo, and hope the debugger can cope. */
20074 if (!get_AT (subrange_die, DW_AT_lower_bound))
20075 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20076 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20077 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20080 /* Otherwise we have an array type with an unspecified length. The
20081 DWARF-2 spec does not say how to handle this; let's just leave out the
20082 bounds. */
20086 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20088 static void
20089 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20091 dw_die_ref decl_die;
20092 HOST_WIDE_INT size;
20093 dw_loc_descr_ref size_expr = NULL;
20095 switch (TREE_CODE (tree_node))
20097 case ERROR_MARK:
20098 size = 0;
20099 break;
20100 case ENUMERAL_TYPE:
20101 case RECORD_TYPE:
20102 case UNION_TYPE:
20103 case QUAL_UNION_TYPE:
20104 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20105 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20107 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20108 return;
20110 size_expr = type_byte_size (tree_node, &size);
20111 break;
20112 case FIELD_DECL:
20113 /* For a data member of a struct or union, the DW_AT_byte_size is
20114 generally given as the number of bytes normally allocated for an
20115 object of the *declared* type of the member itself. This is true
20116 even for bit-fields. */
20117 size = int_size_in_bytes (field_type (tree_node));
20118 break;
20119 default:
20120 gcc_unreachable ();
20123 /* Support for dynamically-sized objects was introduced by DWARFv3.
20124 At the moment, GDB does not handle variable byte sizes very well,
20125 though. */
20126 if ((dwarf_version >= 3 || !dwarf_strict)
20127 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20128 && size_expr != NULL)
20129 add_AT_loc (die, DW_AT_byte_size, size_expr);
20131 /* Note that `size' might be -1 when we get to this point. If it is, that
20132 indicates that the byte size of the entity in question is variable and
20133 that we could not generate a DWARF expression that computes it. */
20134 if (size >= 0)
20135 add_AT_unsigned (die, DW_AT_byte_size, size);
20138 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20139 alignment. */
20141 static void
20142 add_alignment_attribute (dw_die_ref die, tree tree_node)
20144 if (dwarf_version < 5 && dwarf_strict)
20145 return;
20147 unsigned align;
20149 if (DECL_P (tree_node))
20151 if (!DECL_USER_ALIGN (tree_node))
20152 return;
20154 align = DECL_ALIGN_UNIT (tree_node);
20156 else if (TYPE_P (tree_node))
20158 if (!TYPE_USER_ALIGN (tree_node))
20159 return;
20161 align = TYPE_ALIGN_UNIT (tree_node);
20163 else
20164 gcc_unreachable ();
20166 add_AT_unsigned (die, DW_AT_alignment, align);
20169 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20170 which specifies the distance in bits from the highest order bit of the
20171 "containing object" for the bit-field to the highest order bit of the
20172 bit-field itself.
20174 For any given bit-field, the "containing object" is a hypothetical object
20175 (of some integral or enum type) within which the given bit-field lives. The
20176 type of this hypothetical "containing object" is always the same as the
20177 declared type of the individual bit-field itself. The determination of the
20178 exact location of the "containing object" for a bit-field is rather
20179 complicated. It's handled by the `field_byte_offset' function (above).
20181 CTX is required: see the comment for VLR_CONTEXT.
20183 Note that it is the size (in bytes) of the hypothetical "containing object"
20184 which will be given in the DW_AT_byte_size attribute for this bit-field.
20185 (See `byte_size_attribute' above). */
20187 static inline void
20188 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20190 HOST_WIDE_INT object_offset_in_bytes;
20191 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20192 HOST_WIDE_INT bitpos_int;
20193 HOST_WIDE_INT highest_order_object_bit_offset;
20194 HOST_WIDE_INT highest_order_field_bit_offset;
20195 HOST_WIDE_INT bit_offset;
20197 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20199 /* Must be a field and a bit field. */
20200 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20202 /* We can't yet handle bit-fields whose offsets are variable, so if we
20203 encounter such things, just return without generating any attribute
20204 whatsoever. Likewise for variable or too large size. */
20205 if (! tree_fits_shwi_p (bit_position (decl))
20206 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20207 return;
20209 bitpos_int = int_bit_position (decl);
20211 /* Note that the bit offset is always the distance (in bits) from the
20212 highest-order bit of the "containing object" to the highest-order bit of
20213 the bit-field itself. Since the "high-order end" of any object or field
20214 is different on big-endian and little-endian machines, the computation
20215 below must take account of these differences. */
20216 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20217 highest_order_field_bit_offset = bitpos_int;
20219 if (! BYTES_BIG_ENDIAN)
20221 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20222 highest_order_object_bit_offset +=
20223 simple_type_size_in_bits (original_type);
20226 bit_offset
20227 = (! BYTES_BIG_ENDIAN
20228 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20229 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20231 if (bit_offset < 0)
20232 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20233 else
20234 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20237 /* For a FIELD_DECL node which represents a bit field, output an attribute
20238 which specifies the length in bits of the given field. */
20240 static inline void
20241 add_bit_size_attribute (dw_die_ref die, tree decl)
20243 /* Must be a field and a bit field. */
20244 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20245 && DECL_BIT_FIELD_TYPE (decl));
20247 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20248 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20251 /* If the compiled language is ANSI C, then add a 'prototyped'
20252 attribute, if arg types are given for the parameters of a function. */
20254 static inline void
20255 add_prototyped_attribute (dw_die_ref die, tree func_type)
20257 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20259 case DW_LANG_C:
20260 case DW_LANG_C89:
20261 case DW_LANG_C99:
20262 case DW_LANG_C11:
20263 case DW_LANG_ObjC:
20264 if (prototype_p (func_type))
20265 add_AT_flag (die, DW_AT_prototyped, 1);
20266 break;
20267 default:
20268 break;
20272 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20273 by looking in the type declaration, the object declaration equate table or
20274 the block mapping. */
20276 static inline dw_die_ref
20277 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20279 dw_die_ref origin_die = NULL;
20281 if (DECL_P (origin))
20283 dw_die_ref c;
20284 origin_die = lookup_decl_die (origin);
20285 /* "Unwrap" the decls DIE which we put in the imported unit context.
20286 We are looking for the abstract copy here. */
20287 if (in_lto_p
20288 && origin_die
20289 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20290 /* ??? Identify this better. */
20291 && c->with_offset)
20292 origin_die = c;
20294 else if (TYPE_P (origin))
20295 origin_die = lookup_type_die (origin);
20296 else if (TREE_CODE (origin) == BLOCK)
20297 origin_die = BLOCK_DIE (origin);
20299 /* XXX: Functions that are never lowered don't always have correct block
20300 trees (in the case of java, they simply have no block tree, in some other
20301 languages). For these functions, there is nothing we can really do to
20302 output correct debug info for inlined functions in all cases. Rather
20303 than die, we'll just produce deficient debug info now, in that we will
20304 have variables without a proper abstract origin. In the future, when all
20305 functions are lowered, we should re-add a gcc_assert (origin_die)
20306 here. */
20308 if (origin_die)
20309 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20310 return origin_die;
20313 /* We do not currently support the pure_virtual attribute. */
20315 static inline void
20316 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20318 if (DECL_VINDEX (func_decl))
20320 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20322 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20323 add_AT_loc (die, DW_AT_vtable_elem_location,
20324 new_loc_descr (DW_OP_constu,
20325 tree_to_shwi (DECL_VINDEX (func_decl)),
20326 0));
20328 /* GNU extension: Record what type this method came from originally. */
20329 if (debug_info_level > DINFO_LEVEL_TERSE
20330 && DECL_CONTEXT (func_decl))
20331 add_AT_die_ref (die, DW_AT_containing_type,
20332 lookup_type_die (DECL_CONTEXT (func_decl)));
20336 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20337 given decl. This used to be a vendor extension until after DWARF 4
20338 standardized it. */
20340 static void
20341 add_linkage_attr (dw_die_ref die, tree decl)
20343 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20345 /* Mimic what assemble_name_raw does with a leading '*'. */
20346 if (name[0] == '*')
20347 name = &name[1];
20349 if (dwarf_version >= 4)
20350 add_AT_string (die, DW_AT_linkage_name, name);
20351 else
20352 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20355 /* Add source coordinate attributes for the given decl. */
20357 static void
20358 add_src_coords_attributes (dw_die_ref die, tree decl)
20360 expanded_location s;
20362 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20363 return;
20364 s = expand_location (DECL_SOURCE_LOCATION (decl));
20365 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20366 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20367 if (debug_column_info && s.column)
20368 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20371 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20373 static void
20374 add_linkage_name_raw (dw_die_ref die, tree decl)
20376 /* Defer until we have an assembler name set. */
20377 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20379 limbo_die_node *asm_name;
20381 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20382 asm_name->die = die;
20383 asm_name->created_for = decl;
20384 asm_name->next = deferred_asm_name;
20385 deferred_asm_name = asm_name;
20387 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20388 add_linkage_attr (die, decl);
20391 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20393 static void
20394 add_linkage_name (dw_die_ref die, tree decl)
20396 if (debug_info_level > DINFO_LEVEL_NONE
20397 && VAR_OR_FUNCTION_DECL_P (decl)
20398 && TREE_PUBLIC (decl)
20399 && !(VAR_P (decl) && DECL_REGISTER (decl))
20400 && die->die_tag != DW_TAG_member)
20401 add_linkage_name_raw (die, decl);
20404 /* Add a DW_AT_name attribute and source coordinate attribute for the
20405 given decl, but only if it actually has a name. */
20407 static void
20408 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20409 bool no_linkage_name)
20411 tree decl_name;
20413 decl_name = DECL_NAME (decl);
20414 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20416 const char *name = dwarf2_name (decl, 0);
20417 if (name)
20418 add_name_attribute (die, name);
20419 if (! DECL_ARTIFICIAL (decl))
20420 add_src_coords_attributes (die, decl);
20422 if (!no_linkage_name)
20423 add_linkage_name (die, decl);
20426 #ifdef VMS_DEBUGGING_INFO
20427 /* Get the function's name, as described by its RTL. This may be different
20428 from the DECL_NAME name used in the source file. */
20429 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20431 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20432 XEXP (DECL_RTL (decl), 0), false);
20433 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20435 #endif /* VMS_DEBUGGING_INFO */
20438 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20440 static void
20441 add_discr_value (dw_die_ref die, dw_discr_value *value)
20443 dw_attr_node attr;
20445 attr.dw_attr = DW_AT_discr_value;
20446 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20447 attr.dw_attr_val.val_entry = NULL;
20448 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20449 if (value->pos)
20450 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20451 else
20452 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20453 add_dwarf_attr (die, &attr);
20456 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20458 static void
20459 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20461 dw_attr_node attr;
20463 attr.dw_attr = DW_AT_discr_list;
20464 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20465 attr.dw_attr_val.val_entry = NULL;
20466 attr.dw_attr_val.v.val_discr_list = discr_list;
20467 add_dwarf_attr (die, &attr);
20470 static inline dw_discr_list_ref
20471 AT_discr_list (dw_attr_node *attr)
20473 return attr->dw_attr_val.v.val_discr_list;
20476 #ifdef VMS_DEBUGGING_INFO
20477 /* Output the debug main pointer die for VMS */
20479 void
20480 dwarf2out_vms_debug_main_pointer (void)
20482 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20483 dw_die_ref die;
20485 /* Allocate the VMS debug main subprogram die. */
20486 die = ggc_cleared_alloc<die_node> ();
20487 die->die_tag = DW_TAG_subprogram;
20488 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20489 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20490 current_function_funcdef_no);
20491 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20493 /* Make it the first child of comp_unit_die (). */
20494 die->die_parent = comp_unit_die ();
20495 if (comp_unit_die ()->die_child)
20497 die->die_sib = comp_unit_die ()->die_child->die_sib;
20498 comp_unit_die ()->die_child->die_sib = die;
20500 else
20502 die->die_sib = die;
20503 comp_unit_die ()->die_child = die;
20506 #endif /* VMS_DEBUGGING_INFO */
20508 /* Push a new declaration scope. */
20510 static void
20511 push_decl_scope (tree scope)
20513 vec_safe_push (decl_scope_table, scope);
20516 /* Pop a declaration scope. */
20518 static inline void
20519 pop_decl_scope (void)
20521 decl_scope_table->pop ();
20524 /* walk_tree helper function for uses_local_type, below. */
20526 static tree
20527 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20529 if (!TYPE_P (*tp))
20530 *walk_subtrees = 0;
20531 else
20533 tree name = TYPE_NAME (*tp);
20534 if (name && DECL_P (name) && decl_function_context (name))
20535 return *tp;
20537 return NULL_TREE;
20540 /* If TYPE involves a function-local type (including a local typedef to a
20541 non-local type), returns that type; otherwise returns NULL_TREE. */
20543 static tree
20544 uses_local_type (tree type)
20546 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20547 return used;
20550 /* Return the DIE for the scope that immediately contains this type.
20551 Non-named types that do not involve a function-local type get global
20552 scope. Named types nested in namespaces or other types get their
20553 containing scope. All other types (i.e. function-local named types) get
20554 the current active scope. */
20556 static dw_die_ref
20557 scope_die_for (tree t, dw_die_ref context_die)
20559 dw_die_ref scope_die = NULL;
20560 tree containing_scope;
20562 /* Non-types always go in the current scope. */
20563 gcc_assert (TYPE_P (t));
20565 /* Use the scope of the typedef, rather than the scope of the type
20566 it refers to. */
20567 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20568 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20569 else
20570 containing_scope = TYPE_CONTEXT (t);
20572 /* Use the containing namespace if there is one. */
20573 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20575 if (context_die == lookup_decl_die (containing_scope))
20576 /* OK */;
20577 else if (debug_info_level > DINFO_LEVEL_TERSE)
20578 context_die = get_context_die (containing_scope);
20579 else
20580 containing_scope = NULL_TREE;
20583 /* Ignore function type "scopes" from the C frontend. They mean that
20584 a tagged type is local to a parmlist of a function declarator, but
20585 that isn't useful to DWARF. */
20586 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20587 containing_scope = NULL_TREE;
20589 if (SCOPE_FILE_SCOPE_P (containing_scope))
20591 /* If T uses a local type keep it local as well, to avoid references
20592 to function-local DIEs from outside the function. */
20593 if (current_function_decl && uses_local_type (t))
20594 scope_die = context_die;
20595 else
20596 scope_die = comp_unit_die ();
20598 else if (TYPE_P (containing_scope))
20600 /* For types, we can just look up the appropriate DIE. */
20601 if (debug_info_level > DINFO_LEVEL_TERSE)
20602 scope_die = get_context_die (containing_scope);
20603 else
20605 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20606 if (scope_die == NULL)
20607 scope_die = comp_unit_die ();
20610 else
20611 scope_die = context_die;
20613 return scope_die;
20616 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20618 static inline int
20619 local_scope_p (dw_die_ref context_die)
20621 for (; context_die; context_die = context_die->die_parent)
20622 if (context_die->die_tag == DW_TAG_inlined_subroutine
20623 || context_die->die_tag == DW_TAG_subprogram)
20624 return 1;
20626 return 0;
20629 /* Returns nonzero if CONTEXT_DIE is a class. */
20631 static inline int
20632 class_scope_p (dw_die_ref context_die)
20634 return (context_die
20635 && (context_die->die_tag == DW_TAG_structure_type
20636 || context_die->die_tag == DW_TAG_class_type
20637 || context_die->die_tag == DW_TAG_interface_type
20638 || context_die->die_tag == DW_TAG_union_type));
20641 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20642 whether or not to treat a DIE in this context as a declaration. */
20644 static inline int
20645 class_or_namespace_scope_p (dw_die_ref context_die)
20647 return (class_scope_p (context_die)
20648 || (context_die && context_die->die_tag == DW_TAG_namespace));
20651 /* Many forms of DIEs require a "type description" attribute. This
20652 routine locates the proper "type descriptor" die for the type given
20653 by 'type' plus any additional qualifiers given by 'cv_quals', and
20654 adds a DW_AT_type attribute below the given die. */
20656 static void
20657 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20658 bool reverse, dw_die_ref context_die)
20660 enum tree_code code = TREE_CODE (type);
20661 dw_die_ref type_die = NULL;
20663 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20664 or fixed-point type, use the inner type. This is because we have no
20665 support for unnamed types in base_type_die. This can happen if this is
20666 an Ada subrange type. Correct solution is emit a subrange type die. */
20667 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20668 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20669 type = TREE_TYPE (type), code = TREE_CODE (type);
20671 if (code == ERROR_MARK
20672 /* Handle a special case. For functions whose return type is void, we
20673 generate *no* type attribute. (Note that no object may have type
20674 `void', so this only applies to function return types). */
20675 || code == VOID_TYPE)
20676 return;
20678 type_die = modified_type_die (type,
20679 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20680 reverse,
20681 context_die);
20683 if (type_die != NULL)
20684 add_AT_die_ref (object_die, DW_AT_type, type_die);
20687 /* Given an object die, add the calling convention attribute for the
20688 function call type. */
20689 static void
20690 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20692 enum dwarf_calling_convention value = DW_CC_normal;
20694 value = ((enum dwarf_calling_convention)
20695 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20697 if (is_fortran ()
20698 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20700 /* DWARF 2 doesn't provide a way to identify a program's source-level
20701 entry point. DW_AT_calling_convention attributes are only meant
20702 to describe functions' calling conventions. However, lacking a
20703 better way to signal the Fortran main program, we used this for
20704 a long time, following existing custom. Now, DWARF 4 has
20705 DW_AT_main_subprogram, which we add below, but some tools still
20706 rely on the old way, which we thus keep. */
20707 value = DW_CC_program;
20709 if (dwarf_version >= 4 || !dwarf_strict)
20710 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20713 /* Only add the attribute if the backend requests it, and
20714 is not DW_CC_normal. */
20715 if (value && (value != DW_CC_normal))
20716 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20719 /* Given a tree pointer to a struct, class, union, or enum type node, return
20720 a pointer to the (string) tag name for the given type, or zero if the type
20721 was declared without a tag. */
20723 static const char *
20724 type_tag (const_tree type)
20726 const char *name = 0;
20728 if (TYPE_NAME (type) != 0)
20730 tree t = 0;
20732 /* Find the IDENTIFIER_NODE for the type name. */
20733 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20734 && !TYPE_NAMELESS (type))
20735 t = TYPE_NAME (type);
20737 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20738 a TYPE_DECL node, regardless of whether or not a `typedef' was
20739 involved. */
20740 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20741 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20743 /* We want to be extra verbose. Don't call dwarf_name if
20744 DECL_NAME isn't set. The default hook for decl_printable_name
20745 doesn't like that, and in this context it's correct to return
20746 0, instead of "<anonymous>" or the like. */
20747 if (DECL_NAME (TYPE_NAME (type))
20748 && !DECL_NAMELESS (TYPE_NAME (type)))
20749 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20752 /* Now get the name as a string, or invent one. */
20753 if (!name && t != 0)
20754 name = IDENTIFIER_POINTER (t);
20757 return (name == 0 || *name == '\0') ? 0 : name;
20760 /* Return the type associated with a data member, make a special check
20761 for bit field types. */
20763 static inline tree
20764 member_declared_type (const_tree member)
20766 return (DECL_BIT_FIELD_TYPE (member)
20767 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20770 /* Get the decl's label, as described by its RTL. This may be different
20771 from the DECL_NAME name used in the source file. */
20773 #if 0
20774 static const char *
20775 decl_start_label (tree decl)
20777 rtx x;
20778 const char *fnname;
20780 x = DECL_RTL (decl);
20781 gcc_assert (MEM_P (x));
20783 x = XEXP (x, 0);
20784 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20786 fnname = XSTR (x, 0);
20787 return fnname;
20789 #endif
20791 /* For variable-length arrays that have been previously generated, but
20792 may be incomplete due to missing subscript info, fill the subscript
20793 info. Return TRUE if this is one of those cases. */
20794 static bool
20795 fill_variable_array_bounds (tree type)
20797 if (TREE_ASM_WRITTEN (type)
20798 && TREE_CODE (type) == ARRAY_TYPE
20799 && variably_modified_type_p (type, NULL))
20801 dw_die_ref array_die = lookup_type_die (type);
20802 if (!array_die)
20803 return false;
20804 add_subscript_info (array_die, type, !is_ada ());
20805 return true;
20807 return false;
20810 /* These routines generate the internal representation of the DIE's for
20811 the compilation unit. Debugging information is collected by walking
20812 the declaration trees passed in from dwarf2out_decl(). */
20814 static void
20815 gen_array_type_die (tree type, dw_die_ref context_die)
20817 dw_die_ref array_die;
20819 /* GNU compilers represent multidimensional array types as sequences of one
20820 dimensional array types whose element types are themselves array types.
20821 We sometimes squish that down to a single array_type DIE with multiple
20822 subscripts in the Dwarf debugging info. The draft Dwarf specification
20823 say that we are allowed to do this kind of compression in C, because
20824 there is no difference between an array of arrays and a multidimensional
20825 array. We don't do this for Ada to remain as close as possible to the
20826 actual representation, which is especially important against the language
20827 flexibilty wrt arrays of variable size. */
20829 bool collapse_nested_arrays = !is_ada ();
20831 if (fill_variable_array_bounds (type))
20832 return;
20834 dw_die_ref scope_die = scope_die_for (type, context_die);
20835 tree element_type;
20837 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20838 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20839 if (TYPE_STRING_FLAG (type)
20840 && TREE_CODE (type) == ARRAY_TYPE
20841 && is_fortran ()
20842 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20844 HOST_WIDE_INT size;
20846 array_die = new_die (DW_TAG_string_type, scope_die, type);
20847 add_name_attribute (array_die, type_tag (type));
20848 equate_type_number_to_die (type, array_die);
20849 size = int_size_in_bytes (type);
20850 if (size >= 0)
20851 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20852 /* ??? We can't annotate types late, but for LTO we may not
20853 generate a location early either (gfortran.dg/save_6.f90). */
20854 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20855 && TYPE_DOMAIN (type) != NULL_TREE
20856 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20858 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20859 tree rszdecl = szdecl;
20861 size = int_size_in_bytes (TREE_TYPE (szdecl));
20862 if (!DECL_P (szdecl))
20864 if (TREE_CODE (szdecl) == INDIRECT_REF
20865 && DECL_P (TREE_OPERAND (szdecl, 0)))
20867 rszdecl = TREE_OPERAND (szdecl, 0);
20868 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20869 != DWARF2_ADDR_SIZE)
20870 size = 0;
20872 else
20873 size = 0;
20875 if (size > 0)
20877 dw_loc_list_ref loc
20878 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20879 NULL);
20880 if (loc)
20882 add_AT_location_description (array_die, DW_AT_string_length,
20883 loc);
20884 if (size != DWARF2_ADDR_SIZE)
20885 add_AT_unsigned (array_die, dwarf_version >= 5
20886 ? DW_AT_string_length_byte_size
20887 : DW_AT_byte_size, size);
20891 return;
20894 array_die = new_die (DW_TAG_array_type, scope_die, type);
20895 add_name_attribute (array_die, type_tag (type));
20896 equate_type_number_to_die (type, array_die);
20898 if (TREE_CODE (type) == VECTOR_TYPE)
20899 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20901 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20902 if (is_fortran ()
20903 && TREE_CODE (type) == ARRAY_TYPE
20904 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20905 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20906 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20908 #if 0
20909 /* We default the array ordering. SDB will probably do
20910 the right things even if DW_AT_ordering is not present. It's not even
20911 an issue until we start to get into multidimensional arrays anyway. If
20912 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20913 then we'll have to put the DW_AT_ordering attribute back in. (But if
20914 and when we find out that we need to put these in, we will only do so
20915 for multidimensional arrays. */
20916 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20917 #endif
20919 if (TREE_CODE (type) == VECTOR_TYPE)
20921 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20922 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20923 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20924 add_bound_info (subrange_die, DW_AT_upper_bound,
20925 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20927 else
20928 add_subscript_info (array_die, type, collapse_nested_arrays);
20930 /* Add representation of the type of the elements of this array type and
20931 emit the corresponding DIE if we haven't done it already. */
20932 element_type = TREE_TYPE (type);
20933 if (collapse_nested_arrays)
20934 while (TREE_CODE (element_type) == ARRAY_TYPE)
20936 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20937 break;
20938 element_type = TREE_TYPE (element_type);
20941 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20942 TREE_CODE (type) == ARRAY_TYPE
20943 && TYPE_REVERSE_STORAGE_ORDER (type),
20944 context_die);
20946 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20947 if (TYPE_ARTIFICIAL (type))
20948 add_AT_flag (array_die, DW_AT_artificial, 1);
20950 if (get_AT (array_die, DW_AT_name))
20951 add_pubtype (type, array_die);
20953 add_alignment_attribute (array_die, type);
20956 /* This routine generates DIE for array with hidden descriptor, details
20957 are filled into *info by a langhook. */
20959 static void
20960 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20961 dw_die_ref context_die)
20963 const dw_die_ref scope_die = scope_die_for (type, context_die);
20964 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20965 struct loc_descr_context context = { type, info->base_decl, NULL,
20966 false, false };
20967 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20968 int dim;
20970 add_name_attribute (array_die, type_tag (type));
20971 equate_type_number_to_die (type, array_die);
20973 if (info->ndimensions > 1)
20974 switch (info->ordering)
20976 case array_descr_ordering_row_major:
20977 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20978 break;
20979 case array_descr_ordering_column_major:
20980 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20981 break;
20982 default:
20983 break;
20986 if (dwarf_version >= 3 || !dwarf_strict)
20988 if (info->data_location)
20989 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20990 dw_scalar_form_exprloc, &context);
20991 if (info->associated)
20992 add_scalar_info (array_die, DW_AT_associated, info->associated,
20993 dw_scalar_form_constant
20994 | dw_scalar_form_exprloc
20995 | dw_scalar_form_reference, &context);
20996 if (info->allocated)
20997 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20998 dw_scalar_form_constant
20999 | dw_scalar_form_exprloc
21000 | dw_scalar_form_reference, &context);
21001 if (info->stride)
21003 const enum dwarf_attribute attr
21004 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21005 const int forms
21006 = (info->stride_in_bits)
21007 ? dw_scalar_form_constant
21008 : (dw_scalar_form_constant
21009 | dw_scalar_form_exprloc
21010 | dw_scalar_form_reference);
21012 add_scalar_info (array_die, attr, info->stride, forms, &context);
21015 if (dwarf_version >= 5)
21017 if (info->rank)
21019 add_scalar_info (array_die, DW_AT_rank, info->rank,
21020 dw_scalar_form_constant
21021 | dw_scalar_form_exprloc, &context);
21022 subrange_tag = DW_TAG_generic_subrange;
21023 context.placeholder_arg = true;
21027 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21029 for (dim = 0; dim < info->ndimensions; dim++)
21031 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21033 if (info->dimen[dim].bounds_type)
21034 add_type_attribute (subrange_die,
21035 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21036 false, context_die);
21037 if (info->dimen[dim].lower_bound)
21038 add_bound_info (subrange_die, DW_AT_lower_bound,
21039 info->dimen[dim].lower_bound, &context);
21040 if (info->dimen[dim].upper_bound)
21041 add_bound_info (subrange_die, DW_AT_upper_bound,
21042 info->dimen[dim].upper_bound, &context);
21043 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21044 add_scalar_info (subrange_die, DW_AT_byte_stride,
21045 info->dimen[dim].stride,
21046 dw_scalar_form_constant
21047 | dw_scalar_form_exprloc
21048 | dw_scalar_form_reference,
21049 &context);
21052 gen_type_die (info->element_type, context_die);
21053 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21054 TREE_CODE (type) == ARRAY_TYPE
21055 && TYPE_REVERSE_STORAGE_ORDER (type),
21056 context_die);
21058 if (get_AT (array_die, DW_AT_name))
21059 add_pubtype (type, array_die);
21061 add_alignment_attribute (array_die, type);
21064 #if 0
21065 static void
21066 gen_entry_point_die (tree decl, dw_die_ref context_die)
21068 tree origin = decl_ultimate_origin (decl);
21069 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21071 if (origin != NULL)
21072 add_abstract_origin_attribute (decl_die, origin);
21073 else
21075 add_name_and_src_coords_attributes (decl_die, decl);
21076 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21077 TYPE_UNQUALIFIED, false, context_die);
21080 if (DECL_ABSTRACT_P (decl))
21081 equate_decl_number_to_die (decl, decl_die);
21082 else
21083 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21085 #endif
21087 /* Walk through the list of incomplete types again, trying once more to
21088 emit full debugging info for them. */
21090 static void
21091 retry_incomplete_types (void)
21093 set_early_dwarf s;
21094 int i;
21096 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21097 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21098 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21099 vec_safe_truncate (incomplete_types, 0);
21102 /* Determine what tag to use for a record type. */
21104 static enum dwarf_tag
21105 record_type_tag (tree type)
21107 if (! lang_hooks.types.classify_record)
21108 return DW_TAG_structure_type;
21110 switch (lang_hooks.types.classify_record (type))
21112 case RECORD_IS_STRUCT:
21113 return DW_TAG_structure_type;
21115 case RECORD_IS_CLASS:
21116 return DW_TAG_class_type;
21118 case RECORD_IS_INTERFACE:
21119 if (dwarf_version >= 3 || !dwarf_strict)
21120 return DW_TAG_interface_type;
21121 return DW_TAG_structure_type;
21123 default:
21124 gcc_unreachable ();
21128 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21129 include all of the information about the enumeration values also. Each
21130 enumerated type name/value is listed as a child of the enumerated type
21131 DIE. */
21133 static dw_die_ref
21134 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21136 dw_die_ref type_die = lookup_type_die (type);
21138 if (type_die == NULL)
21140 type_die = new_die (DW_TAG_enumeration_type,
21141 scope_die_for (type, context_die), type);
21142 equate_type_number_to_die (type, type_die);
21143 add_name_attribute (type_die, type_tag (type));
21144 if (dwarf_version >= 4 || !dwarf_strict)
21146 if (ENUM_IS_SCOPED (type))
21147 add_AT_flag (type_die, DW_AT_enum_class, 1);
21148 if (ENUM_IS_OPAQUE (type))
21149 add_AT_flag (type_die, DW_AT_declaration, 1);
21151 if (!dwarf_strict)
21152 add_AT_unsigned (type_die, DW_AT_encoding,
21153 TYPE_UNSIGNED (type)
21154 ? DW_ATE_unsigned
21155 : DW_ATE_signed);
21157 else if (! TYPE_SIZE (type))
21158 return type_die;
21159 else
21160 remove_AT (type_die, DW_AT_declaration);
21162 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21163 given enum type is incomplete, do not generate the DW_AT_byte_size
21164 attribute or the DW_AT_element_list attribute. */
21165 if (TYPE_SIZE (type))
21167 tree link;
21169 TREE_ASM_WRITTEN (type) = 1;
21170 add_byte_size_attribute (type_die, type);
21171 add_alignment_attribute (type_die, type);
21172 if (dwarf_version >= 3 || !dwarf_strict)
21174 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21175 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21176 context_die);
21178 if (TYPE_STUB_DECL (type) != NULL_TREE)
21180 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21181 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21184 /* If the first reference to this type was as the return type of an
21185 inline function, then it may not have a parent. Fix this now. */
21186 if (type_die->die_parent == NULL)
21187 add_child_die (scope_die_for (type, context_die), type_die);
21189 for (link = TYPE_VALUES (type);
21190 link != NULL; link = TREE_CHAIN (link))
21192 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21193 tree value = TREE_VALUE (link);
21195 add_name_attribute (enum_die,
21196 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21198 if (TREE_CODE (value) == CONST_DECL)
21199 value = DECL_INITIAL (value);
21201 if (simple_type_size_in_bits (TREE_TYPE (value))
21202 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21204 /* For constant forms created by add_AT_unsigned DWARF
21205 consumers (GDB, elfutils, etc.) always zero extend
21206 the value. Only when the actual value is negative
21207 do we need to use add_AT_int to generate a constant
21208 form that can represent negative values. */
21209 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21210 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21211 add_AT_unsigned (enum_die, DW_AT_const_value,
21212 (unsigned HOST_WIDE_INT) val);
21213 else
21214 add_AT_int (enum_die, DW_AT_const_value, val);
21216 else
21217 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21218 that here. TODO: This should be re-worked to use correct
21219 signed/unsigned double tags for all cases. */
21220 add_AT_wide (enum_die, DW_AT_const_value, value);
21223 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21224 if (TYPE_ARTIFICIAL (type))
21225 add_AT_flag (type_die, DW_AT_artificial, 1);
21227 else
21228 add_AT_flag (type_die, DW_AT_declaration, 1);
21230 add_alignment_attribute (type_die, type);
21232 add_pubtype (type, type_die);
21234 return type_die;
21237 /* Generate a DIE to represent either a real live formal parameter decl or to
21238 represent just the type of some formal parameter position in some function
21239 type.
21241 Note that this routine is a bit unusual because its argument may be a
21242 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21243 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21244 node. If it's the former then this function is being called to output a
21245 DIE to represent a formal parameter object (or some inlining thereof). If
21246 it's the latter, then this function is only being called to output a
21247 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21248 argument type of some subprogram type.
21249 If EMIT_NAME_P is true, name and source coordinate attributes
21250 are emitted. */
21252 static dw_die_ref
21253 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21254 dw_die_ref context_die)
21256 tree node_or_origin = node ? node : origin;
21257 tree ultimate_origin;
21258 dw_die_ref parm_die = NULL;
21260 if (DECL_P (node_or_origin))
21262 parm_die = lookup_decl_die (node);
21264 /* If the contexts differ, we may not be talking about the same
21265 thing.
21266 ??? When in LTO the DIE parent is the "abstract" copy and the
21267 context_die is the specification "copy". But this whole block
21268 should eventually be no longer needed. */
21269 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21271 if (!DECL_ABSTRACT_P (node))
21273 /* This can happen when creating an inlined instance, in
21274 which case we need to create a new DIE that will get
21275 annotated with DW_AT_abstract_origin. */
21276 parm_die = NULL;
21278 else
21279 gcc_unreachable ();
21282 if (parm_die && parm_die->die_parent == NULL)
21284 /* Check that parm_die already has the right attributes that
21285 we would have added below. If any attributes are
21286 missing, fall through to add them. */
21287 if (! DECL_ABSTRACT_P (node_or_origin)
21288 && !get_AT (parm_die, DW_AT_location)
21289 && !get_AT (parm_die, DW_AT_const_value))
21290 /* We are missing location info, and are about to add it. */
21292 else
21294 add_child_die (context_die, parm_die);
21295 return parm_die;
21300 /* If we have a previously generated DIE, use it, unless this is an
21301 concrete instance (origin != NULL), in which case we need a new
21302 DIE with a corresponding DW_AT_abstract_origin. */
21303 bool reusing_die;
21304 if (parm_die && origin == NULL)
21305 reusing_die = true;
21306 else
21308 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21309 reusing_die = false;
21312 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21314 case tcc_declaration:
21315 ultimate_origin = decl_ultimate_origin (node_or_origin);
21316 if (node || ultimate_origin)
21317 origin = ultimate_origin;
21319 if (reusing_die)
21320 goto add_location;
21322 if (origin != NULL)
21323 add_abstract_origin_attribute (parm_die, origin);
21324 else if (emit_name_p)
21325 add_name_and_src_coords_attributes (parm_die, node);
21326 if (origin == NULL
21327 || (! DECL_ABSTRACT_P (node_or_origin)
21328 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21329 decl_function_context
21330 (node_or_origin))))
21332 tree type = TREE_TYPE (node_or_origin);
21333 if (decl_by_reference_p (node_or_origin))
21334 add_type_attribute (parm_die, TREE_TYPE (type),
21335 TYPE_UNQUALIFIED,
21336 false, context_die);
21337 else
21338 add_type_attribute (parm_die, type,
21339 decl_quals (node_or_origin),
21340 false, context_die);
21342 if (origin == NULL && DECL_ARTIFICIAL (node))
21343 add_AT_flag (parm_die, DW_AT_artificial, 1);
21344 add_location:
21345 if (node && node != origin)
21346 equate_decl_number_to_die (node, parm_die);
21347 if (! DECL_ABSTRACT_P (node_or_origin))
21348 add_location_or_const_value_attribute (parm_die, node_or_origin,
21349 node == NULL);
21351 break;
21353 case tcc_type:
21354 /* We were called with some kind of a ..._TYPE node. */
21355 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21356 context_die);
21357 break;
21359 default:
21360 gcc_unreachable ();
21363 return parm_die;
21366 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21367 children DW_TAG_formal_parameter DIEs representing the arguments of the
21368 parameter pack.
21370 PARM_PACK must be a function parameter pack.
21371 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21372 must point to the subsequent arguments of the function PACK_ARG belongs to.
21373 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21374 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21375 following the last one for which a DIE was generated. */
21377 static dw_die_ref
21378 gen_formal_parameter_pack_die (tree parm_pack,
21379 tree pack_arg,
21380 dw_die_ref subr_die,
21381 tree *next_arg)
21383 tree arg;
21384 dw_die_ref parm_pack_die;
21386 gcc_assert (parm_pack
21387 && lang_hooks.function_parameter_pack_p (parm_pack)
21388 && subr_die);
21390 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21391 add_src_coords_attributes (parm_pack_die, parm_pack);
21393 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21395 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21396 parm_pack))
21397 break;
21398 gen_formal_parameter_die (arg, NULL,
21399 false /* Don't emit name attribute. */,
21400 parm_pack_die);
21402 if (next_arg)
21403 *next_arg = arg;
21404 return parm_pack_die;
21407 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21408 at the end of an (ANSI prototyped) formal parameters list. */
21410 static void
21411 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21413 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21416 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21417 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21418 parameters as specified in some function type specification (except for
21419 those which appear as part of a function *definition*). */
21421 static void
21422 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21424 tree link;
21425 tree formal_type = NULL;
21426 tree first_parm_type;
21427 tree arg;
21429 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21431 arg = DECL_ARGUMENTS (function_or_method_type);
21432 function_or_method_type = TREE_TYPE (function_or_method_type);
21434 else
21435 arg = NULL_TREE;
21437 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21439 /* Make our first pass over the list of formal parameter types and output a
21440 DW_TAG_formal_parameter DIE for each one. */
21441 for (link = first_parm_type; link; )
21443 dw_die_ref parm_die;
21445 formal_type = TREE_VALUE (link);
21446 if (formal_type == void_type_node)
21447 break;
21449 /* Output a (nameless) DIE to represent the formal parameter itself. */
21450 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21452 parm_die = gen_formal_parameter_die (formal_type, NULL,
21453 true /* Emit name attribute. */,
21454 context_die);
21455 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21456 && link == first_parm_type)
21458 add_AT_flag (parm_die, DW_AT_artificial, 1);
21459 if (dwarf_version >= 3 || !dwarf_strict)
21460 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21462 else if (arg && DECL_ARTIFICIAL (arg))
21463 add_AT_flag (parm_die, DW_AT_artificial, 1);
21466 link = TREE_CHAIN (link);
21467 if (arg)
21468 arg = DECL_CHAIN (arg);
21471 /* If this function type has an ellipsis, add a
21472 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21473 if (formal_type != void_type_node)
21474 gen_unspecified_parameters_die (function_or_method_type, context_die);
21476 /* Make our second (and final) pass over the list of formal parameter types
21477 and output DIEs to represent those types (as necessary). */
21478 for (link = TYPE_ARG_TYPES (function_or_method_type);
21479 link && TREE_VALUE (link);
21480 link = TREE_CHAIN (link))
21481 gen_type_die (TREE_VALUE (link), context_die);
21484 /* We want to generate the DIE for TYPE so that we can generate the
21485 die for MEMBER, which has been defined; we will need to refer back
21486 to the member declaration nested within TYPE. If we're trying to
21487 generate minimal debug info for TYPE, processing TYPE won't do the
21488 trick; we need to attach the member declaration by hand. */
21490 static void
21491 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21493 gen_type_die (type, context_die);
21495 /* If we're trying to avoid duplicate debug info, we may not have
21496 emitted the member decl for this function. Emit it now. */
21497 if (TYPE_STUB_DECL (type)
21498 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21499 && ! lookup_decl_die (member))
21501 dw_die_ref type_die;
21502 gcc_assert (!decl_ultimate_origin (member));
21504 push_decl_scope (type);
21505 type_die = lookup_type_die_strip_naming_typedef (type);
21506 if (TREE_CODE (member) == FUNCTION_DECL)
21507 gen_subprogram_die (member, type_die);
21508 else if (TREE_CODE (member) == FIELD_DECL)
21510 /* Ignore the nameless fields that are used to skip bits but handle
21511 C++ anonymous unions and structs. */
21512 if (DECL_NAME (member) != NULL_TREE
21513 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21514 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21516 struct vlr_context vlr_ctx = {
21517 DECL_CONTEXT (member), /* struct_type */
21518 NULL_TREE /* variant_part_offset */
21520 gen_type_die (member_declared_type (member), type_die);
21521 gen_field_die (member, &vlr_ctx, type_die);
21524 else
21525 gen_variable_die (member, NULL_TREE, type_die);
21527 pop_decl_scope ();
21531 /* Forward declare these functions, because they are mutually recursive
21532 with their set_block_* pairing functions. */
21533 static void set_decl_origin_self (tree);
21535 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21536 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21537 that it points to the node itself, thus indicating that the node is its
21538 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21539 the given node is NULL, recursively descend the decl/block tree which
21540 it is the root of, and for each other ..._DECL or BLOCK node contained
21541 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21542 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21543 values to point to themselves. */
21545 static void
21546 set_block_origin_self (tree stmt)
21548 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21550 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21553 tree local_decl;
21555 for (local_decl = BLOCK_VARS (stmt);
21556 local_decl != NULL_TREE;
21557 local_decl = DECL_CHAIN (local_decl))
21558 /* Do not recurse on nested functions since the inlining status
21559 of parent and child can be different as per the DWARF spec. */
21560 if (TREE_CODE (local_decl) != FUNCTION_DECL
21561 && !DECL_EXTERNAL (local_decl))
21562 set_decl_origin_self (local_decl);
21566 tree subblock;
21568 for (subblock = BLOCK_SUBBLOCKS (stmt);
21569 subblock != NULL_TREE;
21570 subblock = BLOCK_CHAIN (subblock))
21571 set_block_origin_self (subblock); /* Recurse. */
21576 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21577 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21578 node to so that it points to the node itself, thus indicating that the
21579 node represents its own (abstract) origin. Additionally, if the
21580 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21581 the decl/block tree of which the given node is the root of, and for
21582 each other ..._DECL or BLOCK node contained therein whose
21583 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21584 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21585 point to themselves. */
21587 static void
21588 set_decl_origin_self (tree decl)
21590 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21592 DECL_ABSTRACT_ORIGIN (decl) = decl;
21593 if (TREE_CODE (decl) == FUNCTION_DECL)
21595 tree arg;
21597 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21598 DECL_ABSTRACT_ORIGIN (arg) = arg;
21599 if (DECL_INITIAL (decl) != NULL_TREE
21600 && DECL_INITIAL (decl) != error_mark_node)
21601 set_block_origin_self (DECL_INITIAL (decl));
21606 /* Mark the early DIE for DECL as the abstract instance. */
21608 static void
21609 dwarf2out_abstract_function (tree decl)
21611 dw_die_ref old_die;
21613 /* Make sure we have the actual abstract inline, not a clone. */
21614 decl = DECL_ORIGIN (decl);
21616 if (DECL_IGNORED_P (decl))
21617 return;
21619 old_die = lookup_decl_die (decl);
21620 /* With early debug we always have an old DIE unless we are in LTO
21621 and the user did not compile but only link with debug. */
21622 if (in_lto_p && ! old_die)
21623 return;
21624 gcc_assert (old_die != NULL);
21625 if (get_AT (old_die, DW_AT_inline)
21626 || get_AT (old_die, DW_AT_abstract_origin))
21627 /* We've already generated the abstract instance. */
21628 return;
21630 /* Go ahead and put DW_AT_inline on the DIE. */
21631 if (DECL_DECLARED_INLINE_P (decl))
21633 if (cgraph_function_possibly_inlined_p (decl))
21634 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21635 else
21636 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21638 else
21640 if (cgraph_function_possibly_inlined_p (decl))
21641 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21642 else
21643 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21646 if (DECL_DECLARED_INLINE_P (decl)
21647 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21648 add_AT_flag (old_die, DW_AT_artificial, 1);
21650 set_decl_origin_self (decl);
21653 /* Helper function of premark_used_types() which gets called through
21654 htab_traverse.
21656 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21657 marked as unused by prune_unused_types. */
21659 bool
21660 premark_used_types_helper (tree const &type, void *)
21662 dw_die_ref die;
21664 die = lookup_type_die (type);
21665 if (die != NULL)
21666 die->die_perennial_p = 1;
21667 return true;
21670 /* Helper function of premark_types_used_by_global_vars which gets called
21671 through htab_traverse.
21673 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21674 marked as unused by prune_unused_types. The DIE of the type is marked
21675 only if the global variable using the type will actually be emitted. */
21678 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21679 void *)
21681 struct types_used_by_vars_entry *entry;
21682 dw_die_ref die;
21684 entry = (struct types_used_by_vars_entry *) *slot;
21685 gcc_assert (entry->type != NULL
21686 && entry->var_decl != NULL);
21687 die = lookup_type_die (entry->type);
21688 if (die)
21690 /* Ask cgraph if the global variable really is to be emitted.
21691 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21692 varpool_node *node = varpool_node::get (entry->var_decl);
21693 if (node && node->definition)
21695 die->die_perennial_p = 1;
21696 /* Keep the parent DIEs as well. */
21697 while ((die = die->die_parent) && die->die_perennial_p == 0)
21698 die->die_perennial_p = 1;
21701 return 1;
21704 /* Mark all members of used_types_hash as perennial. */
21706 static void
21707 premark_used_types (struct function *fun)
21709 if (fun && fun->used_types_hash)
21710 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21713 /* Mark all members of types_used_by_vars_entry as perennial. */
21715 static void
21716 premark_types_used_by_global_vars (void)
21718 if (types_used_by_vars_hash)
21719 types_used_by_vars_hash
21720 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21723 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21724 for CA_LOC call arg loc node. */
21726 static dw_die_ref
21727 gen_call_site_die (tree decl, dw_die_ref subr_die,
21728 struct call_arg_loc_node *ca_loc)
21730 dw_die_ref stmt_die = NULL, die;
21731 tree block = ca_loc->block;
21733 while (block
21734 && block != DECL_INITIAL (decl)
21735 && TREE_CODE (block) == BLOCK)
21737 stmt_die = BLOCK_DIE (block);
21738 if (stmt_die)
21739 break;
21740 block = BLOCK_SUPERCONTEXT (block);
21742 if (stmt_die == NULL)
21743 stmt_die = subr_die;
21744 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21745 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21746 if (ca_loc->tail_call_p)
21747 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21748 if (ca_loc->symbol_ref)
21750 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21751 if (tdie)
21752 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21753 else
21754 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21755 false);
21757 return die;
21760 /* Generate a DIE to represent a declared function (either file-scope or
21761 block-local). */
21763 static void
21764 gen_subprogram_die (tree decl, dw_die_ref context_die)
21766 tree origin = decl_ultimate_origin (decl);
21767 dw_die_ref subr_die;
21768 dw_die_ref old_die = lookup_decl_die (decl);
21770 /* This function gets called multiple times for different stages of
21771 the debug process. For example, for func() in this code:
21773 namespace S
21775 void func() { ... }
21778 ...we get called 4 times. Twice in early debug and twice in
21779 late debug:
21781 Early debug
21782 -----------
21784 1. Once while generating func() within the namespace. This is
21785 the declaration. The declaration bit below is set, as the
21786 context is the namespace.
21788 A new DIE will be generated with DW_AT_declaration set.
21790 2. Once for func() itself. This is the specification. The
21791 declaration bit below is clear as the context is the CU.
21793 We will use the cached DIE from (1) to create a new DIE with
21794 DW_AT_specification pointing to the declaration in (1).
21796 Late debug via rest_of_handle_final()
21797 -------------------------------------
21799 3. Once generating func() within the namespace. This is also the
21800 declaration, as in (1), but this time we will early exit below
21801 as we have a cached DIE and a declaration needs no additional
21802 annotations (no locations), as the source declaration line
21803 info is enough.
21805 4. Once for func() itself. As in (2), this is the specification,
21806 but this time we will re-use the cached DIE, and just annotate
21807 it with the location information that should now be available.
21809 For something without namespaces, but with abstract instances, we
21810 are also called a multiple times:
21812 class Base
21814 public:
21815 Base (); // constructor declaration (1)
21818 Base::Base () { } // constructor specification (2)
21820 Early debug
21821 -----------
21823 1. Once for the Base() constructor by virtue of it being a
21824 member of the Base class. This is done via
21825 rest_of_type_compilation.
21827 This is a declaration, so a new DIE will be created with
21828 DW_AT_declaration.
21830 2. Once for the Base() constructor definition, but this time
21831 while generating the abstract instance of the base
21832 constructor (__base_ctor) which is being generated via early
21833 debug of reachable functions.
21835 Even though we have a cached version of the declaration (1),
21836 we will create a DW_AT_specification of the declaration DIE
21837 in (1).
21839 3. Once for the __base_ctor itself, but this time, we generate
21840 an DW_AT_abstract_origin version of the DW_AT_specification in
21841 (2).
21843 Late debug via rest_of_handle_final
21844 -----------------------------------
21846 4. One final time for the __base_ctor (which will have a cached
21847 DIE with DW_AT_abstract_origin created in (3). This time,
21848 we will just annotate the location information now
21849 available.
21851 int declaration = (current_function_decl != decl
21852 || class_or_namespace_scope_p (context_die));
21854 /* Now that the C++ front end lazily declares artificial member fns, we
21855 might need to retrofit the declaration into its class. */
21856 if (!declaration && !origin && !old_die
21857 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21858 && !class_or_namespace_scope_p (context_die)
21859 && debug_info_level > DINFO_LEVEL_TERSE)
21860 old_die = force_decl_die (decl);
21862 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21863 if (origin != NULL)
21865 gcc_assert (!declaration || local_scope_p (context_die));
21867 /* Fixup die_parent for the abstract instance of a nested
21868 inline function. */
21869 if (old_die && old_die->die_parent == NULL)
21870 add_child_die (context_die, old_die);
21872 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21874 /* If we have a DW_AT_abstract_origin we have a working
21875 cached version. */
21876 subr_die = old_die;
21878 else
21880 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21881 add_abstract_origin_attribute (subr_die, origin);
21882 /* This is where the actual code for a cloned function is.
21883 Let's emit linkage name attribute for it. This helps
21884 debuggers to e.g, set breakpoints into
21885 constructors/destructors when the user asks "break
21886 K::K". */
21887 add_linkage_name (subr_die, decl);
21890 /* A cached copy, possibly from early dwarf generation. Reuse as
21891 much as possible. */
21892 else if (old_die)
21894 /* A declaration that has been previously dumped needs no
21895 additional information. */
21896 if (declaration)
21897 return;
21899 if (!get_AT_flag (old_die, DW_AT_declaration)
21900 /* We can have a normal definition following an inline one in the
21901 case of redefinition of GNU C extern inlines.
21902 It seems reasonable to use AT_specification in this case. */
21903 && !get_AT (old_die, DW_AT_inline))
21905 /* Detect and ignore this case, where we are trying to output
21906 something we have already output. */
21907 if (get_AT (old_die, DW_AT_low_pc)
21908 || get_AT (old_die, DW_AT_ranges))
21909 return;
21911 /* If we have no location information, this must be a
21912 partially generated DIE from early dwarf generation.
21913 Fall through and generate it. */
21916 /* If the definition comes from the same place as the declaration,
21917 maybe use the old DIE. We always want the DIE for this function
21918 that has the *_pc attributes to be under comp_unit_die so the
21919 debugger can find it. We also need to do this for abstract
21920 instances of inlines, since the spec requires the out-of-line copy
21921 to have the same parent. For local class methods, this doesn't
21922 apply; we just use the old DIE. */
21923 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21924 struct dwarf_file_data * file_index = lookup_filename (s.file);
21925 if ((is_cu_die (old_die->die_parent)
21926 /* This condition fixes the inconsistency/ICE with the
21927 following Fortran test (or some derivative thereof) while
21928 building libgfortran:
21930 module some_m
21931 contains
21932 logical function funky (FLAG)
21933 funky = .true.
21934 end function
21935 end module
21937 || (old_die->die_parent
21938 && old_die->die_parent->die_tag == DW_TAG_module)
21939 || context_die == NULL)
21940 && (DECL_ARTIFICIAL (decl)
21941 /* The location attributes may be in the abstract origin
21942 which in the case of LTO might be not available to
21943 look at. */
21944 || get_AT (old_die, DW_AT_abstract_origin)
21945 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21946 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21947 == (unsigned) s.line)
21948 && (!debug_column_info
21949 || s.column == 0
21950 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21951 == (unsigned) s.column)))))
21953 subr_die = old_die;
21955 /* Clear out the declaration attribute, but leave the
21956 parameters so they can be augmented with location
21957 information later. Unless this was a declaration, in
21958 which case, wipe out the nameless parameters and recreate
21959 them further down. */
21960 if (remove_AT (subr_die, DW_AT_declaration))
21963 remove_AT (subr_die, DW_AT_object_pointer);
21964 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21967 /* Make a specification pointing to the previously built
21968 declaration. */
21969 else
21971 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21972 add_AT_specification (subr_die, old_die);
21973 add_pubname (decl, subr_die);
21974 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21975 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21976 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21977 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21978 if (debug_column_info
21979 && s.column
21980 && (get_AT_unsigned (old_die, DW_AT_decl_column)
21981 != (unsigned) s.column))
21982 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
21984 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21985 emit the real type on the definition die. */
21986 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
21988 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21989 if (die == auto_die || die == decltype_auto_die)
21990 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21991 TYPE_UNQUALIFIED, false, context_die);
21994 /* When we process the method declaration, we haven't seen
21995 the out-of-class defaulted definition yet, so we have to
21996 recheck now. */
21997 if ((dwarf_version >= 5 || ! dwarf_strict)
21998 && !get_AT (subr_die, DW_AT_defaulted))
22000 int defaulted
22001 = lang_hooks.decls.decl_dwarf_attribute (decl,
22002 DW_AT_defaulted);
22003 if (defaulted != -1)
22005 /* Other values must have been handled before. */
22006 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22007 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22012 /* Create a fresh DIE for anything else. */
22013 else
22015 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22017 if (TREE_PUBLIC (decl))
22018 add_AT_flag (subr_die, DW_AT_external, 1);
22020 add_name_and_src_coords_attributes (subr_die, decl);
22021 add_pubname (decl, subr_die);
22022 if (debug_info_level > DINFO_LEVEL_TERSE)
22024 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22025 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22026 TYPE_UNQUALIFIED, false, context_die);
22029 add_pure_or_virtual_attribute (subr_die, decl);
22030 if (DECL_ARTIFICIAL (decl))
22031 add_AT_flag (subr_die, DW_AT_artificial, 1);
22033 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22034 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22036 add_alignment_attribute (subr_die, decl);
22038 add_accessibility_attribute (subr_die, decl);
22041 /* Unless we have an existing non-declaration DIE, equate the new
22042 DIE. */
22043 if (!old_die || is_declaration_die (old_die))
22044 equate_decl_number_to_die (decl, subr_die);
22046 if (declaration)
22048 if (!old_die || !get_AT (old_die, DW_AT_inline))
22050 add_AT_flag (subr_die, DW_AT_declaration, 1);
22052 /* If this is an explicit function declaration then generate
22053 a DW_AT_explicit attribute. */
22054 if ((dwarf_version >= 3 || !dwarf_strict)
22055 && lang_hooks.decls.decl_dwarf_attribute (decl,
22056 DW_AT_explicit) == 1)
22057 add_AT_flag (subr_die, DW_AT_explicit, 1);
22059 /* If this is a C++11 deleted special function member then generate
22060 a DW_AT_deleted attribute. */
22061 if ((dwarf_version >= 5 || !dwarf_strict)
22062 && lang_hooks.decls.decl_dwarf_attribute (decl,
22063 DW_AT_deleted) == 1)
22064 add_AT_flag (subr_die, DW_AT_deleted, 1);
22066 /* If this is a C++11 defaulted special function member then
22067 generate a DW_AT_defaulted attribute. */
22068 if (dwarf_version >= 5 || !dwarf_strict)
22070 int defaulted
22071 = lang_hooks.decls.decl_dwarf_attribute (decl,
22072 DW_AT_defaulted);
22073 if (defaulted != -1)
22074 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22077 /* If this is a C++11 non-static member function with & ref-qualifier
22078 then generate a DW_AT_reference attribute. */
22079 if ((dwarf_version >= 5 || !dwarf_strict)
22080 && lang_hooks.decls.decl_dwarf_attribute (decl,
22081 DW_AT_reference) == 1)
22082 add_AT_flag (subr_die, DW_AT_reference, 1);
22084 /* If this is a C++11 non-static member function with &&
22085 ref-qualifier then generate a DW_AT_reference attribute. */
22086 if ((dwarf_version >= 5 || !dwarf_strict)
22087 && lang_hooks.decls.decl_dwarf_attribute (decl,
22088 DW_AT_rvalue_reference)
22089 == 1)
22090 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22093 /* For non DECL_EXTERNALs, if range information is available, fill
22094 the DIE with it. */
22095 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22097 HOST_WIDE_INT cfa_fb_offset;
22099 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22101 if (!crtl->has_bb_partition)
22103 dw_fde_ref fde = fun->fde;
22104 if (fde->dw_fde_begin)
22106 /* We have already generated the labels. */
22107 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22108 fde->dw_fde_end, false);
22110 else
22112 /* Create start/end labels and add the range. */
22113 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22114 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22115 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22116 current_function_funcdef_no);
22117 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22118 current_function_funcdef_no);
22119 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22120 false);
22123 #if VMS_DEBUGGING_INFO
22124 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22125 Section 2.3 Prologue and Epilogue Attributes:
22126 When a breakpoint is set on entry to a function, it is generally
22127 desirable for execution to be suspended, not on the very first
22128 instruction of the function, but rather at a point after the
22129 function's frame has been set up, after any language defined local
22130 declaration processing has been completed, and before execution of
22131 the first statement of the function begins. Debuggers generally
22132 cannot properly determine where this point is. Similarly for a
22133 breakpoint set on exit from a function. The prologue and epilogue
22134 attributes allow a compiler to communicate the location(s) to use. */
22137 if (fde->dw_fde_vms_end_prologue)
22138 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22139 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22141 if (fde->dw_fde_vms_begin_epilogue)
22142 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22143 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22145 #endif
22148 else
22150 /* Generate pubnames entries for the split function code ranges. */
22151 dw_fde_ref fde = fun->fde;
22153 if (fde->dw_fde_second_begin)
22155 if (dwarf_version >= 3 || !dwarf_strict)
22157 /* We should use ranges for non-contiguous code section
22158 addresses. Use the actual code range for the initial
22159 section, since the HOT/COLD labels might precede an
22160 alignment offset. */
22161 bool range_list_added = false;
22162 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22163 fde->dw_fde_end, &range_list_added,
22164 false);
22165 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22166 fde->dw_fde_second_end,
22167 &range_list_added, false);
22168 if (range_list_added)
22169 add_ranges (NULL);
22171 else
22173 /* There is no real support in DW2 for this .. so we make
22174 a work-around. First, emit the pub name for the segment
22175 containing the function label. Then make and emit a
22176 simplified subprogram DIE for the second segment with the
22177 name pre-fixed by __hot/cold_sect_of_. We use the same
22178 linkage name for the second die so that gdb will find both
22179 sections when given "b foo". */
22180 const char *name = NULL;
22181 tree decl_name = DECL_NAME (decl);
22182 dw_die_ref seg_die;
22184 /* Do the 'primary' section. */
22185 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22186 fde->dw_fde_end, false);
22188 /* Build a minimal DIE for the secondary section. */
22189 seg_die = new_die (DW_TAG_subprogram,
22190 subr_die->die_parent, decl);
22192 if (TREE_PUBLIC (decl))
22193 add_AT_flag (seg_die, DW_AT_external, 1);
22195 if (decl_name != NULL
22196 && IDENTIFIER_POINTER (decl_name) != NULL)
22198 name = dwarf2_name (decl, 1);
22199 if (! DECL_ARTIFICIAL (decl))
22200 add_src_coords_attributes (seg_die, decl);
22202 add_linkage_name (seg_die, decl);
22204 gcc_assert (name != NULL);
22205 add_pure_or_virtual_attribute (seg_die, decl);
22206 if (DECL_ARTIFICIAL (decl))
22207 add_AT_flag (seg_die, DW_AT_artificial, 1);
22209 name = concat ("__second_sect_of_", name, NULL);
22210 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22211 fde->dw_fde_second_end, false);
22212 add_name_attribute (seg_die, name);
22213 if (want_pubnames ())
22214 add_pubname_string (name, seg_die);
22217 else
22218 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22219 false);
22222 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22224 /* We define the "frame base" as the function's CFA. This is more
22225 convenient for several reasons: (1) It's stable across the prologue
22226 and epilogue, which makes it better than just a frame pointer,
22227 (2) With dwarf3, there exists a one-byte encoding that allows us
22228 to reference the .debug_frame data by proxy, but failing that,
22229 (3) We can at least reuse the code inspection and interpretation
22230 code that determines the CFA position at various points in the
22231 function. */
22232 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22234 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22235 add_AT_loc (subr_die, DW_AT_frame_base, op);
22237 else
22239 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22240 if (list->dw_loc_next)
22241 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22242 else
22243 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22246 /* Compute a displacement from the "steady-state frame pointer" to
22247 the CFA. The former is what all stack slots and argument slots
22248 will reference in the rtl; the latter is what we've told the
22249 debugger about. We'll need to adjust all frame_base references
22250 by this displacement. */
22251 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22253 if (fun->static_chain_decl)
22255 /* DWARF requires here a location expression that computes the
22256 address of the enclosing subprogram's frame base. The machinery
22257 in tree-nested.c is supposed to store this specific address in the
22258 last field of the FRAME record. */
22259 const tree frame_type
22260 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22261 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22263 tree fb_expr
22264 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22265 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22266 fb_expr, fb_decl, NULL_TREE);
22268 add_AT_location_description (subr_die, DW_AT_static_link,
22269 loc_list_from_tree (fb_expr, 0, NULL));
22272 resolve_variable_values ();
22275 /* Generate child dies for template paramaters. */
22276 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22277 gen_generic_params_dies (decl);
22279 /* Now output descriptions of the arguments for this function. This gets
22280 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22281 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22282 `...' at the end of the formal parameter list. In order to find out if
22283 there was a trailing ellipsis or not, we must instead look at the type
22284 associated with the FUNCTION_DECL. This will be a node of type
22285 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22286 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22287 an ellipsis at the end. */
22289 /* In the case where we are describing a mere function declaration, all we
22290 need to do here (and all we *can* do here) is to describe the *types* of
22291 its formal parameters. */
22292 if (debug_info_level <= DINFO_LEVEL_TERSE)
22294 else if (declaration)
22295 gen_formal_types_die (decl, subr_die);
22296 else
22298 /* Generate DIEs to represent all known formal parameters. */
22299 tree parm = DECL_ARGUMENTS (decl);
22300 tree generic_decl = early_dwarf
22301 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22302 tree generic_decl_parm = generic_decl
22303 ? DECL_ARGUMENTS (generic_decl)
22304 : NULL;
22306 /* Now we want to walk the list of parameters of the function and
22307 emit their relevant DIEs.
22309 We consider the case of DECL being an instance of a generic function
22310 as well as it being a normal function.
22312 If DECL is an instance of a generic function we walk the
22313 parameters of the generic function declaration _and_ the parameters of
22314 DECL itself. This is useful because we want to emit specific DIEs for
22315 function parameter packs and those are declared as part of the
22316 generic function declaration. In that particular case,
22317 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22318 That DIE has children DIEs representing the set of arguments
22319 of the pack. Note that the set of pack arguments can be empty.
22320 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22321 children DIE.
22323 Otherwise, we just consider the parameters of DECL. */
22324 while (generic_decl_parm || parm)
22326 if (generic_decl_parm
22327 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22328 gen_formal_parameter_pack_die (generic_decl_parm,
22329 parm, subr_die,
22330 &parm);
22331 else if (parm && !POINTER_BOUNDS_P (parm))
22333 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22335 if (early_dwarf
22336 && parm == DECL_ARGUMENTS (decl)
22337 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22338 && parm_die
22339 && (dwarf_version >= 3 || !dwarf_strict))
22340 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22342 parm = DECL_CHAIN (parm);
22344 else if (parm)
22345 parm = DECL_CHAIN (parm);
22347 if (generic_decl_parm)
22348 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22351 /* Decide whether we need an unspecified_parameters DIE at the end.
22352 There are 2 more cases to do this for: 1) the ansi ... declaration -
22353 this is detectable when the end of the arg list is not a
22354 void_type_node 2) an unprototyped function declaration (not a
22355 definition). This just means that we have no info about the
22356 parameters at all. */
22357 if (early_dwarf)
22359 if (prototype_p (TREE_TYPE (decl)))
22361 /* This is the prototyped case, check for.... */
22362 if (stdarg_p (TREE_TYPE (decl)))
22363 gen_unspecified_parameters_die (decl, subr_die);
22365 else if (DECL_INITIAL (decl) == NULL_TREE)
22366 gen_unspecified_parameters_die (decl, subr_die);
22370 if (subr_die != old_die)
22371 /* Add the calling convention attribute if requested. */
22372 add_calling_convention_attribute (subr_die, decl);
22374 /* Output Dwarf info for all of the stuff within the body of the function
22375 (if it has one - it may be just a declaration).
22377 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22378 a function. This BLOCK actually represents the outermost binding contour
22379 for the function, i.e. the contour in which the function's formal
22380 parameters and labels get declared. Curiously, it appears that the front
22381 end doesn't actually put the PARM_DECL nodes for the current function onto
22382 the BLOCK_VARS list for this outer scope, but are strung off of the
22383 DECL_ARGUMENTS list for the function instead.
22385 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22386 the LABEL_DECL nodes for the function however, and we output DWARF info
22387 for those in decls_for_scope. Just within the `outer_scope' there will be
22388 a BLOCK node representing the function's outermost pair of curly braces,
22389 and any blocks used for the base and member initializers of a C++
22390 constructor function. */
22391 tree outer_scope = DECL_INITIAL (decl);
22392 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22394 int call_site_note_count = 0;
22395 int tail_call_site_note_count = 0;
22397 /* Emit a DW_TAG_variable DIE for a named return value. */
22398 if (DECL_NAME (DECL_RESULT (decl)))
22399 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22401 /* The first time through decls_for_scope we will generate the
22402 DIEs for the locals. The second time, we fill in the
22403 location info. */
22404 decls_for_scope (outer_scope, subr_die);
22406 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22408 struct call_arg_loc_node *ca_loc;
22409 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22411 dw_die_ref die = NULL;
22412 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22413 rtx arg, next_arg;
22415 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22416 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22417 : NULL_RTX);
22418 arg; arg = next_arg)
22420 dw_loc_descr_ref reg, val;
22421 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22422 dw_die_ref cdie, tdie = NULL;
22424 next_arg = XEXP (arg, 1);
22425 if (REG_P (XEXP (XEXP (arg, 0), 0))
22426 && next_arg
22427 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22428 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22429 && REGNO (XEXP (XEXP (arg, 0), 0))
22430 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22431 next_arg = XEXP (next_arg, 1);
22432 if (mode == VOIDmode)
22434 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22435 if (mode == VOIDmode)
22436 mode = GET_MODE (XEXP (arg, 0));
22438 if (mode == VOIDmode || mode == BLKmode)
22439 continue;
22440 /* Get dynamic information about call target only if we
22441 have no static information: we cannot generate both
22442 DW_AT_call_origin and DW_AT_call_target
22443 attributes. */
22444 if (ca_loc->symbol_ref == NULL_RTX)
22446 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22448 tloc = XEXP (XEXP (arg, 0), 1);
22449 continue;
22451 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22452 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22454 tlocc = XEXP (XEXP (arg, 0), 1);
22455 continue;
22458 reg = NULL;
22459 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22460 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22461 VAR_INIT_STATUS_INITIALIZED);
22462 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22464 rtx mem = XEXP (XEXP (arg, 0), 0);
22465 reg = mem_loc_descriptor (XEXP (mem, 0),
22466 get_address_mode (mem),
22467 GET_MODE (mem),
22468 VAR_INIT_STATUS_INITIALIZED);
22470 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22471 == DEBUG_PARAMETER_REF)
22473 tree tdecl
22474 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22475 tdie = lookup_decl_die (tdecl);
22476 if (tdie == NULL)
22477 continue;
22479 else
22480 continue;
22481 if (reg == NULL
22482 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22483 != DEBUG_PARAMETER_REF)
22484 continue;
22485 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22486 VOIDmode,
22487 VAR_INIT_STATUS_INITIALIZED);
22488 if (val == NULL)
22489 continue;
22490 if (die == NULL)
22491 die = gen_call_site_die (decl, subr_die, ca_loc);
22492 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22493 NULL_TREE);
22494 if (reg != NULL)
22495 add_AT_loc (cdie, DW_AT_location, reg);
22496 else if (tdie != NULL)
22497 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22498 tdie);
22499 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22500 if (next_arg != XEXP (arg, 1))
22502 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22503 if (mode == VOIDmode)
22504 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22505 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22506 0), 1),
22507 mode, VOIDmode,
22508 VAR_INIT_STATUS_INITIALIZED);
22509 if (val != NULL)
22510 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22511 val);
22514 if (die == NULL
22515 && (ca_loc->symbol_ref || tloc))
22516 die = gen_call_site_die (decl, subr_die, ca_loc);
22517 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22519 dw_loc_descr_ref tval = NULL;
22521 if (tloc != NULL_RTX)
22522 tval = mem_loc_descriptor (tloc,
22523 GET_MODE (tloc) == VOIDmode
22524 ? Pmode : GET_MODE (tloc),
22525 VOIDmode,
22526 VAR_INIT_STATUS_INITIALIZED);
22527 if (tval)
22528 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22529 else if (tlocc != NULL_RTX)
22531 tval = mem_loc_descriptor (tlocc,
22532 GET_MODE (tlocc) == VOIDmode
22533 ? Pmode : GET_MODE (tlocc),
22534 VOIDmode,
22535 VAR_INIT_STATUS_INITIALIZED);
22536 if (tval)
22537 add_AT_loc (die,
22538 dwarf_AT (DW_AT_call_target_clobbered),
22539 tval);
22542 if (die != NULL)
22544 call_site_note_count++;
22545 if (ca_loc->tail_call_p)
22546 tail_call_site_note_count++;
22550 call_arg_locations = NULL;
22551 call_arg_loc_last = NULL;
22552 if (tail_call_site_count >= 0
22553 && tail_call_site_count == tail_call_site_note_count
22554 && (!dwarf_strict || dwarf_version >= 5))
22556 if (call_site_count >= 0
22557 && call_site_count == call_site_note_count)
22558 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22559 else
22560 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22562 call_site_count = -1;
22563 tail_call_site_count = -1;
22566 /* Mark used types after we have created DIEs for the functions scopes. */
22567 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22570 /* Returns a hash value for X (which really is a die_struct). */
22572 hashval_t
22573 block_die_hasher::hash (die_struct *d)
22575 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22578 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22579 as decl_id and die_parent of die_struct Y. */
22581 bool
22582 block_die_hasher::equal (die_struct *x, die_struct *y)
22584 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22587 /* Return TRUE if DECL, which may have been previously generated as
22588 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22589 true if decl (or its origin) is either an extern declaration or a
22590 class/namespace scoped declaration.
22592 The declare_in_namespace support causes us to get two DIEs for one
22593 variable, both of which are declarations. We want to avoid
22594 considering one to be a specification, so we must test for
22595 DECLARATION and DW_AT_declaration. */
22596 static inline bool
22597 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22599 return (old_die && TREE_STATIC (decl) && !declaration
22600 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22603 /* Return true if DECL is a local static. */
22605 static inline bool
22606 local_function_static (tree decl)
22608 gcc_assert (VAR_P (decl));
22609 return TREE_STATIC (decl)
22610 && DECL_CONTEXT (decl)
22611 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22614 /* Generate a DIE to represent a declared data object.
22615 Either DECL or ORIGIN must be non-null. */
22617 static void
22618 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22620 HOST_WIDE_INT off = 0;
22621 tree com_decl;
22622 tree decl_or_origin = decl ? decl : origin;
22623 tree ultimate_origin;
22624 dw_die_ref var_die;
22625 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22626 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22627 || class_or_namespace_scope_p (context_die));
22628 bool specialization_p = false;
22629 bool no_linkage_name = false;
22631 /* While C++ inline static data members have definitions inside of the
22632 class, force the first DIE to be a declaration, then let gen_member_die
22633 reparent it to the class context and call gen_variable_die again
22634 to create the outside of the class DIE for the definition. */
22635 if (!declaration
22636 && old_die == NULL
22637 && decl
22638 && DECL_CONTEXT (decl)
22639 && TYPE_P (DECL_CONTEXT (decl))
22640 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22642 declaration = true;
22643 if (dwarf_version < 5)
22644 no_linkage_name = true;
22647 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22648 if (decl || ultimate_origin)
22649 origin = ultimate_origin;
22650 com_decl = fortran_common (decl_or_origin, &off);
22652 /* Symbol in common gets emitted as a child of the common block, in the form
22653 of a data member. */
22654 if (com_decl)
22656 dw_die_ref com_die;
22657 dw_loc_list_ref loc = NULL;
22658 die_node com_die_arg;
22660 var_die = lookup_decl_die (decl_or_origin);
22661 if (var_die)
22663 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22665 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22666 if (loc)
22668 if (off)
22670 /* Optimize the common case. */
22671 if (single_element_loc_list_p (loc)
22672 && loc->expr->dw_loc_opc == DW_OP_addr
22673 && loc->expr->dw_loc_next == NULL
22674 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22675 == SYMBOL_REF)
22677 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22678 loc->expr->dw_loc_oprnd1.v.val_addr
22679 = plus_constant (GET_MODE (x), x , off);
22681 else
22682 loc_list_plus_const (loc, off);
22684 add_AT_location_description (var_die, DW_AT_location, loc);
22685 remove_AT (var_die, DW_AT_declaration);
22688 return;
22691 if (common_block_die_table == NULL)
22692 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22694 com_die_arg.decl_id = DECL_UID (com_decl);
22695 com_die_arg.die_parent = context_die;
22696 com_die = common_block_die_table->find (&com_die_arg);
22697 if (! early_dwarf)
22698 loc = loc_list_from_tree (com_decl, 2, NULL);
22699 if (com_die == NULL)
22701 const char *cnam
22702 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22703 die_node **slot;
22705 com_die = new_die (DW_TAG_common_block, context_die, decl);
22706 add_name_and_src_coords_attributes (com_die, com_decl);
22707 if (loc)
22709 add_AT_location_description (com_die, DW_AT_location, loc);
22710 /* Avoid sharing the same loc descriptor between
22711 DW_TAG_common_block and DW_TAG_variable. */
22712 loc = loc_list_from_tree (com_decl, 2, NULL);
22714 else if (DECL_EXTERNAL (decl_or_origin))
22715 add_AT_flag (com_die, DW_AT_declaration, 1);
22716 if (want_pubnames ())
22717 add_pubname_string (cnam, com_die); /* ??? needed? */
22718 com_die->decl_id = DECL_UID (com_decl);
22719 slot = common_block_die_table->find_slot (com_die, INSERT);
22720 *slot = com_die;
22722 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22724 add_AT_location_description (com_die, DW_AT_location, loc);
22725 loc = loc_list_from_tree (com_decl, 2, NULL);
22726 remove_AT (com_die, DW_AT_declaration);
22728 var_die = new_die (DW_TAG_variable, com_die, decl);
22729 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22730 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22731 decl_quals (decl_or_origin), false,
22732 context_die);
22733 add_alignment_attribute (var_die, decl);
22734 add_AT_flag (var_die, DW_AT_external, 1);
22735 if (loc)
22737 if (off)
22739 /* Optimize the common case. */
22740 if (single_element_loc_list_p (loc)
22741 && loc->expr->dw_loc_opc == DW_OP_addr
22742 && loc->expr->dw_loc_next == NULL
22743 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22745 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22746 loc->expr->dw_loc_oprnd1.v.val_addr
22747 = plus_constant (GET_MODE (x), x, off);
22749 else
22750 loc_list_plus_const (loc, off);
22752 add_AT_location_description (var_die, DW_AT_location, loc);
22754 else if (DECL_EXTERNAL (decl_or_origin))
22755 add_AT_flag (var_die, DW_AT_declaration, 1);
22756 if (decl)
22757 equate_decl_number_to_die (decl, var_die);
22758 return;
22761 if (old_die)
22763 if (declaration)
22765 /* A declaration that has been previously dumped, needs no
22766 further annotations, since it doesn't need location on
22767 the second pass. */
22768 return;
22770 else if (decl_will_get_specification_p (old_die, decl, declaration)
22771 && !get_AT (old_die, DW_AT_specification))
22773 /* Fall-thru so we can make a new variable die along with a
22774 DW_AT_specification. */
22776 else if (origin && old_die->die_parent != context_die)
22778 /* If we will be creating an inlined instance, we need a
22779 new DIE that will get annotated with
22780 DW_AT_abstract_origin. Clear things so we can get a
22781 new DIE. */
22782 gcc_assert (!DECL_ABSTRACT_P (decl));
22783 old_die = NULL;
22785 else
22787 /* If a DIE was dumped early, it still needs location info.
22788 Skip to where we fill the location bits. */
22789 var_die = old_die;
22791 /* ??? In LTRANS we cannot annotate early created variably
22792 modified type DIEs without copying them and adjusting all
22793 references to them. Thus we dumped them again, also add a
22794 reference to them. */
22795 tree type = TREE_TYPE (decl_or_origin);
22796 if (in_lto_p
22797 && variably_modified_type_p
22798 (type, decl_function_context (decl_or_origin)))
22800 if (decl_by_reference_p (decl_or_origin))
22801 add_type_attribute (var_die, TREE_TYPE (type),
22802 TYPE_UNQUALIFIED, false, context_die);
22803 else
22804 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22805 false, context_die);
22808 goto gen_variable_die_location;
22812 /* For static data members, the declaration in the class is supposed
22813 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22814 also in DWARF2; the specification should still be DW_TAG_variable
22815 referencing the DW_TAG_member DIE. */
22816 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22817 var_die = new_die (DW_TAG_member, context_die, decl);
22818 else
22819 var_die = new_die (DW_TAG_variable, context_die, decl);
22821 if (origin != NULL)
22822 add_abstract_origin_attribute (var_die, origin);
22824 /* Loop unrolling can create multiple blocks that refer to the same
22825 static variable, so we must test for the DW_AT_declaration flag.
22827 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22828 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22829 sharing them.
22831 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22832 else if (decl_will_get_specification_p (old_die, decl, declaration))
22834 /* This is a definition of a C++ class level static. */
22835 add_AT_specification (var_die, old_die);
22836 specialization_p = true;
22837 if (DECL_NAME (decl))
22839 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22840 struct dwarf_file_data * file_index = lookup_filename (s.file);
22842 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22843 add_AT_file (var_die, DW_AT_decl_file, file_index);
22845 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22846 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22848 if (debug_column_info
22849 && s.column
22850 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22851 != (unsigned) s.column))
22852 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22854 if (old_die->die_tag == DW_TAG_member)
22855 add_linkage_name (var_die, decl);
22858 else
22859 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22861 if ((origin == NULL && !specialization_p)
22862 || (origin != NULL
22863 && !DECL_ABSTRACT_P (decl_or_origin)
22864 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22865 decl_function_context
22866 (decl_or_origin))))
22868 tree type = TREE_TYPE (decl_or_origin);
22870 if (decl_by_reference_p (decl_or_origin))
22871 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22872 context_die);
22873 else
22874 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22875 context_die);
22878 if (origin == NULL && !specialization_p)
22880 if (TREE_PUBLIC (decl))
22881 add_AT_flag (var_die, DW_AT_external, 1);
22883 if (DECL_ARTIFICIAL (decl))
22884 add_AT_flag (var_die, DW_AT_artificial, 1);
22886 add_alignment_attribute (var_die, decl);
22888 add_accessibility_attribute (var_die, decl);
22891 if (declaration)
22892 add_AT_flag (var_die, DW_AT_declaration, 1);
22894 if (decl && (DECL_ABSTRACT_P (decl)
22895 || !old_die || is_declaration_die (old_die)))
22896 equate_decl_number_to_die (decl, var_die);
22898 gen_variable_die_location:
22899 if (! declaration
22900 && (! DECL_ABSTRACT_P (decl_or_origin)
22901 /* Local static vars are shared between all clones/inlines,
22902 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22903 already set. */
22904 || (VAR_P (decl_or_origin)
22905 && TREE_STATIC (decl_or_origin)
22906 && DECL_RTL_SET_P (decl_or_origin))))
22908 if (early_dwarf)
22909 add_pubname (decl_or_origin, var_die);
22910 else
22911 add_location_or_const_value_attribute (var_die, decl_or_origin,
22912 decl == NULL);
22914 else
22915 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22917 if ((dwarf_version >= 4 || !dwarf_strict)
22918 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22919 DW_AT_const_expr) == 1
22920 && !get_AT (var_die, DW_AT_const_expr)
22921 && !specialization_p)
22922 add_AT_flag (var_die, DW_AT_const_expr, 1);
22924 if (!dwarf_strict)
22926 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22927 DW_AT_inline);
22928 if (inl != -1
22929 && !get_AT (var_die, DW_AT_inline)
22930 && !specialization_p)
22931 add_AT_unsigned (var_die, DW_AT_inline, inl);
22935 /* Generate a DIE to represent a named constant. */
22937 static void
22938 gen_const_die (tree decl, dw_die_ref context_die)
22940 dw_die_ref const_die;
22941 tree type = TREE_TYPE (decl);
22943 const_die = lookup_decl_die (decl);
22944 if (const_die)
22945 return;
22947 const_die = new_die (DW_TAG_constant, context_die, decl);
22948 equate_decl_number_to_die (decl, const_die);
22949 add_name_and_src_coords_attributes (const_die, decl);
22950 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22951 if (TREE_PUBLIC (decl))
22952 add_AT_flag (const_die, DW_AT_external, 1);
22953 if (DECL_ARTIFICIAL (decl))
22954 add_AT_flag (const_die, DW_AT_artificial, 1);
22955 tree_add_const_value_attribute_for_decl (const_die, decl);
22958 /* Generate a DIE to represent a label identifier. */
22960 static void
22961 gen_label_die (tree decl, dw_die_ref context_die)
22963 tree origin = decl_ultimate_origin (decl);
22964 dw_die_ref lbl_die = lookup_decl_die (decl);
22965 rtx insn;
22966 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22968 if (!lbl_die)
22970 lbl_die = new_die (DW_TAG_label, context_die, decl);
22971 equate_decl_number_to_die (decl, lbl_die);
22973 if (origin != NULL)
22974 add_abstract_origin_attribute (lbl_die, origin);
22975 else
22976 add_name_and_src_coords_attributes (lbl_die, decl);
22979 if (DECL_ABSTRACT_P (decl))
22980 equate_decl_number_to_die (decl, lbl_die);
22981 else if (! early_dwarf)
22983 insn = DECL_RTL_IF_SET (decl);
22985 /* Deleted labels are programmer specified labels which have been
22986 eliminated because of various optimizations. We still emit them
22987 here so that it is possible to put breakpoints on them. */
22988 if (insn
22989 && (LABEL_P (insn)
22990 || ((NOTE_P (insn)
22991 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
22993 /* When optimization is enabled (via -O) some parts of the compiler
22994 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
22995 represent source-level labels which were explicitly declared by
22996 the user. This really shouldn't be happening though, so catch
22997 it if it ever does happen. */
22998 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23000 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23001 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23003 else if (insn
23004 && NOTE_P (insn)
23005 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23006 && CODE_LABEL_NUMBER (insn) != -1)
23008 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23009 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23014 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23015 attributes to the DIE for a block STMT, to describe where the inlined
23016 function was called from. This is similar to add_src_coords_attributes. */
23018 static inline void
23019 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23021 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23023 if (dwarf_version >= 3 || !dwarf_strict)
23025 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23026 add_AT_unsigned (die, DW_AT_call_line, s.line);
23027 if (debug_column_info && s.column)
23028 add_AT_unsigned (die, DW_AT_call_column, s.column);
23033 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23034 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23036 static inline void
23037 add_high_low_attributes (tree stmt, dw_die_ref die)
23039 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23041 if (BLOCK_FRAGMENT_CHAIN (stmt)
23042 && (dwarf_version >= 3 || !dwarf_strict))
23044 tree chain, superblock = NULL_TREE;
23045 dw_die_ref pdie;
23046 dw_attr_node *attr = NULL;
23048 if (inlined_function_outer_scope_p (stmt))
23050 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23051 BLOCK_NUMBER (stmt));
23052 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23055 /* Optimize duplicate .debug_ranges lists or even tails of
23056 lists. If this BLOCK has same ranges as its supercontext,
23057 lookup DW_AT_ranges attribute in the supercontext (and
23058 recursively so), verify that the ranges_table contains the
23059 right values and use it instead of adding a new .debug_range. */
23060 for (chain = stmt, pdie = die;
23061 BLOCK_SAME_RANGE (chain);
23062 chain = BLOCK_SUPERCONTEXT (chain))
23064 dw_attr_node *new_attr;
23066 pdie = pdie->die_parent;
23067 if (pdie == NULL)
23068 break;
23069 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23070 break;
23071 new_attr = get_AT (pdie, DW_AT_ranges);
23072 if (new_attr == NULL
23073 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23074 break;
23075 attr = new_attr;
23076 superblock = BLOCK_SUPERCONTEXT (chain);
23078 if (attr != NULL
23079 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23080 == BLOCK_NUMBER (superblock))
23081 && BLOCK_FRAGMENT_CHAIN (superblock))
23083 unsigned long off = attr->dw_attr_val.v.val_offset;
23084 unsigned long supercnt = 0, thiscnt = 0;
23085 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23086 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23088 ++supercnt;
23089 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23090 == BLOCK_NUMBER (chain));
23092 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23093 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23094 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23095 ++thiscnt;
23096 gcc_assert (supercnt >= thiscnt);
23097 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23098 false);
23099 note_rnglist_head (off + supercnt - thiscnt);
23100 return;
23103 unsigned int offset = add_ranges (stmt, true);
23104 add_AT_range_list (die, DW_AT_ranges, offset, false);
23105 note_rnglist_head (offset);
23107 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23108 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23111 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23112 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23113 chain = BLOCK_FRAGMENT_CHAIN (chain);
23115 while (chain);
23116 add_ranges (NULL);
23118 else
23120 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23121 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23122 BLOCK_NUMBER (stmt));
23123 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23124 BLOCK_NUMBER (stmt));
23125 add_AT_low_high_pc (die, label, label_high, false);
23129 /* Generate a DIE for a lexical block. */
23131 static void
23132 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23134 dw_die_ref old_die = BLOCK_DIE (stmt);
23135 dw_die_ref stmt_die = NULL;
23136 if (!old_die)
23138 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23139 BLOCK_DIE (stmt) = stmt_die;
23142 if (BLOCK_ABSTRACT (stmt))
23144 if (old_die)
23146 /* This must have been generated early and it won't even
23147 need location information since it's a DW_AT_inline
23148 function. */
23149 if (flag_checking)
23150 for (dw_die_ref c = context_die; c; c = c->die_parent)
23151 if (c->die_tag == DW_TAG_inlined_subroutine
23152 || c->die_tag == DW_TAG_subprogram)
23154 gcc_assert (get_AT (c, DW_AT_inline));
23155 break;
23157 return;
23160 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23162 /* If this is an inlined instance, create a new lexical die for
23163 anything below to attach DW_AT_abstract_origin to. */
23164 if (old_die)
23166 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23167 BLOCK_DIE (stmt) = stmt_die;
23168 old_die = NULL;
23171 tree origin = block_ultimate_origin (stmt);
23172 if (origin != NULL_TREE && origin != stmt)
23173 add_abstract_origin_attribute (stmt_die, origin);
23176 if (old_die)
23177 stmt_die = old_die;
23179 /* A non abstract block whose blocks have already been reordered
23180 should have the instruction range for this block. If so, set the
23181 high/low attributes. */
23182 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23184 gcc_assert (stmt_die);
23185 add_high_low_attributes (stmt, stmt_die);
23188 decls_for_scope (stmt, stmt_die);
23191 /* Generate a DIE for an inlined subprogram. */
23193 static void
23194 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23196 tree decl;
23198 /* The instance of function that is effectively being inlined shall not
23199 be abstract. */
23200 gcc_assert (! BLOCK_ABSTRACT (stmt));
23202 decl = block_ultimate_origin (stmt);
23204 /* Make sure any inlined functions are known to be inlineable. */
23205 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23206 || cgraph_function_possibly_inlined_p (decl));
23208 if (! BLOCK_ABSTRACT (stmt))
23210 dw_die_ref subr_die
23211 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23213 if (call_arg_locations)
23214 BLOCK_DIE (stmt) = subr_die;
23215 add_abstract_origin_attribute (subr_die, decl);
23216 if (TREE_ASM_WRITTEN (stmt))
23217 add_high_low_attributes (stmt, subr_die);
23218 add_call_src_coords_attributes (stmt, subr_die);
23220 decls_for_scope (stmt, subr_die);
23224 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23225 the comment for VLR_CONTEXT. */
23227 static void
23228 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23230 dw_die_ref decl_die;
23232 if (TREE_TYPE (decl) == error_mark_node)
23233 return;
23235 decl_die = new_die (DW_TAG_member, context_die, decl);
23236 add_name_and_src_coords_attributes (decl_die, decl);
23237 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23238 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23239 context_die);
23241 if (DECL_BIT_FIELD_TYPE (decl))
23243 add_byte_size_attribute (decl_die, decl);
23244 add_bit_size_attribute (decl_die, decl);
23245 add_bit_offset_attribute (decl_die, decl, ctx);
23248 add_alignment_attribute (decl_die, decl);
23250 /* If we have a variant part offset, then we are supposed to process a member
23251 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23252 trees. */
23253 gcc_assert (ctx->variant_part_offset == NULL_TREE
23254 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23255 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23256 add_data_member_location_attribute (decl_die, decl, ctx);
23258 if (DECL_ARTIFICIAL (decl))
23259 add_AT_flag (decl_die, DW_AT_artificial, 1);
23261 add_accessibility_attribute (decl_die, decl);
23263 /* Equate decl number to die, so that we can look up this decl later on. */
23264 equate_decl_number_to_die (decl, decl_die);
23267 /* Generate a DIE for a pointer to a member type. TYPE can be an
23268 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23269 pointer to member function. */
23271 static void
23272 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23274 if (lookup_type_die (type))
23275 return;
23277 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23278 scope_die_for (type, context_die), type);
23280 equate_type_number_to_die (type, ptr_die);
23281 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23282 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23283 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23284 context_die);
23285 add_alignment_attribute (ptr_die, type);
23287 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23288 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23290 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23291 add_AT_loc (ptr_die, DW_AT_use_location, op);
23295 static char *producer_string;
23297 /* Return a heap allocated producer string including command line options
23298 if -grecord-gcc-switches. */
23300 static char *
23301 gen_producer_string (void)
23303 size_t j;
23304 auto_vec<const char *> switches;
23305 const char *language_string = lang_hooks.name;
23306 char *producer, *tail;
23307 const char *p;
23308 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23309 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23311 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23312 switch (save_decoded_options[j].opt_index)
23314 case OPT_o:
23315 case OPT_d:
23316 case OPT_dumpbase:
23317 case OPT_dumpdir:
23318 case OPT_auxbase:
23319 case OPT_auxbase_strip:
23320 case OPT_quiet:
23321 case OPT_version:
23322 case OPT_v:
23323 case OPT_w:
23324 case OPT_L:
23325 case OPT_D:
23326 case OPT_I:
23327 case OPT_U:
23328 case OPT_SPECIAL_unknown:
23329 case OPT_SPECIAL_ignore:
23330 case OPT_SPECIAL_program_name:
23331 case OPT_SPECIAL_input_file:
23332 case OPT_grecord_gcc_switches:
23333 case OPT_gno_record_gcc_switches:
23334 case OPT__output_pch_:
23335 case OPT_fdiagnostics_show_location_:
23336 case OPT_fdiagnostics_show_option:
23337 case OPT_fdiagnostics_show_caret:
23338 case OPT_fdiagnostics_color_:
23339 case OPT_fverbose_asm:
23340 case OPT____:
23341 case OPT__sysroot_:
23342 case OPT_nostdinc:
23343 case OPT_nostdinc__:
23344 case OPT_fpreprocessed:
23345 case OPT_fltrans_output_list_:
23346 case OPT_fresolution_:
23347 case OPT_fdebug_prefix_map_:
23348 /* Ignore these. */
23349 continue;
23350 default:
23351 if (cl_options[save_decoded_options[j].opt_index].flags
23352 & CL_NO_DWARF_RECORD)
23353 continue;
23354 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23355 == '-');
23356 switch (save_decoded_options[j].canonical_option[0][1])
23358 case 'M':
23359 case 'i':
23360 case 'W':
23361 continue;
23362 case 'f':
23363 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23364 "dump", 4) == 0)
23365 continue;
23366 break;
23367 default:
23368 break;
23370 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23371 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23372 break;
23375 producer = XNEWVEC (char, plen + 1 + len + 1);
23376 tail = producer;
23377 sprintf (tail, "%s %s", language_string, version_string);
23378 tail += plen;
23380 FOR_EACH_VEC_ELT (switches, j, p)
23382 len = strlen (p);
23383 *tail = ' ';
23384 memcpy (tail + 1, p, len);
23385 tail += len + 1;
23388 *tail = '\0';
23389 return producer;
23392 /* Given a C and/or C++ language/version string return the "highest".
23393 C++ is assumed to be "higher" than C in this case. Used for merging
23394 LTO translation unit languages. */
23395 static const char *
23396 highest_c_language (const char *lang1, const char *lang2)
23398 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23399 return "GNU C++17";
23400 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23401 return "GNU C++14";
23402 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23403 return "GNU C++11";
23404 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23405 return "GNU C++98";
23407 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23408 return "GNU C11";
23409 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23410 return "GNU C99";
23411 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23412 return "GNU C89";
23414 gcc_unreachable ();
23418 /* Generate the DIE for the compilation unit. */
23420 static dw_die_ref
23421 gen_compile_unit_die (const char *filename)
23423 dw_die_ref die;
23424 const char *language_string = lang_hooks.name;
23425 int language;
23427 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23429 if (filename)
23431 add_name_attribute (die, filename);
23432 /* Don't add cwd for <built-in>. */
23433 if (filename[0] != '<')
23434 add_comp_dir_attribute (die);
23437 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23439 /* If our producer is LTO try to figure out a common language to use
23440 from the global list of translation units. */
23441 if (strcmp (language_string, "GNU GIMPLE") == 0)
23443 unsigned i;
23444 tree t;
23445 const char *common_lang = NULL;
23447 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23449 if (!TRANSLATION_UNIT_LANGUAGE (t))
23450 continue;
23451 if (!common_lang)
23452 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23453 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23455 else if (strncmp (common_lang, "GNU C", 5) == 0
23456 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23457 /* Mixing C and C++ is ok, use C++ in that case. */
23458 common_lang = highest_c_language (common_lang,
23459 TRANSLATION_UNIT_LANGUAGE (t));
23460 else
23462 /* Fall back to C. */
23463 common_lang = NULL;
23464 break;
23468 if (common_lang)
23469 language_string = common_lang;
23472 language = DW_LANG_C;
23473 if (strncmp (language_string, "GNU C", 5) == 0
23474 && ISDIGIT (language_string[5]))
23476 language = DW_LANG_C89;
23477 if (dwarf_version >= 3 || !dwarf_strict)
23479 if (strcmp (language_string, "GNU C89") != 0)
23480 language = DW_LANG_C99;
23482 if (dwarf_version >= 5 /* || !dwarf_strict */)
23483 if (strcmp (language_string, "GNU C11") == 0)
23484 language = DW_LANG_C11;
23487 else if (strncmp (language_string, "GNU C++", 7) == 0)
23489 language = DW_LANG_C_plus_plus;
23490 if (dwarf_version >= 5 /* || !dwarf_strict */)
23492 if (strcmp (language_string, "GNU C++11") == 0)
23493 language = DW_LANG_C_plus_plus_11;
23494 else if (strcmp (language_string, "GNU C++14") == 0)
23495 language = DW_LANG_C_plus_plus_14;
23496 else if (strcmp (language_string, "GNU C++17") == 0)
23497 /* For now. */
23498 language = DW_LANG_C_plus_plus_14;
23501 else if (strcmp (language_string, "GNU F77") == 0)
23502 language = DW_LANG_Fortran77;
23503 else if (dwarf_version >= 3 || !dwarf_strict)
23505 if (strcmp (language_string, "GNU Ada") == 0)
23506 language = DW_LANG_Ada95;
23507 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23509 language = DW_LANG_Fortran95;
23510 if (dwarf_version >= 5 /* || !dwarf_strict */)
23512 if (strcmp (language_string, "GNU Fortran2003") == 0)
23513 language = DW_LANG_Fortran03;
23514 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23515 language = DW_LANG_Fortran08;
23518 else if (strcmp (language_string, "GNU Objective-C") == 0)
23519 language = DW_LANG_ObjC;
23520 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23521 language = DW_LANG_ObjC_plus_plus;
23522 else if (dwarf_version >= 5 || !dwarf_strict)
23524 if (strcmp (language_string, "GNU Go") == 0)
23525 language = DW_LANG_Go;
23528 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23529 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23530 language = DW_LANG_Fortran90;
23532 add_AT_unsigned (die, DW_AT_language, language);
23534 switch (language)
23536 case DW_LANG_Fortran77:
23537 case DW_LANG_Fortran90:
23538 case DW_LANG_Fortran95:
23539 case DW_LANG_Fortran03:
23540 case DW_LANG_Fortran08:
23541 /* Fortran has case insensitive identifiers and the front-end
23542 lowercases everything. */
23543 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23544 break;
23545 default:
23546 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23547 break;
23549 return die;
23552 /* Generate the DIE for a base class. */
23554 static void
23555 gen_inheritance_die (tree binfo, tree access, tree type,
23556 dw_die_ref context_die)
23558 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23559 struct vlr_context ctx = { type, NULL };
23561 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23562 context_die);
23563 add_data_member_location_attribute (die, binfo, &ctx);
23565 if (BINFO_VIRTUAL_P (binfo))
23566 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23568 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23569 children, otherwise the default is DW_ACCESS_public. In DWARF2
23570 the default has always been DW_ACCESS_private. */
23571 if (access == access_public_node)
23573 if (dwarf_version == 2
23574 || context_die->die_tag == DW_TAG_class_type)
23575 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23577 else if (access == access_protected_node)
23578 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23579 else if (dwarf_version > 2
23580 && context_die->die_tag != DW_TAG_class_type)
23581 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23584 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23585 structure. */
23586 static bool
23587 is_variant_part (tree decl)
23589 return (TREE_CODE (decl) == FIELD_DECL
23590 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23593 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23594 return the FIELD_DECL. Return NULL_TREE otherwise. */
23596 static tree
23597 analyze_discr_in_predicate (tree operand, tree struct_type)
23599 bool continue_stripping = true;
23600 while (continue_stripping)
23601 switch (TREE_CODE (operand))
23603 CASE_CONVERT:
23604 operand = TREE_OPERAND (operand, 0);
23605 break;
23606 default:
23607 continue_stripping = false;
23608 break;
23611 /* Match field access to members of struct_type only. */
23612 if (TREE_CODE (operand) == COMPONENT_REF
23613 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23614 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23615 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23616 return TREE_OPERAND (operand, 1);
23617 else
23618 return NULL_TREE;
23621 /* Check that SRC is a constant integer that can be represented as a native
23622 integer constant (either signed or unsigned). If so, store it into DEST and
23623 return true. Return false otherwise. */
23625 static bool
23626 get_discr_value (tree src, dw_discr_value *dest)
23628 tree discr_type = TREE_TYPE (src);
23630 if (lang_hooks.types.get_debug_type)
23632 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23633 if (debug_type != NULL)
23634 discr_type = debug_type;
23637 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23638 return false;
23640 /* Signedness can vary between the original type and the debug type. This
23641 can happen for character types in Ada for instance: the character type
23642 used for code generation can be signed, to be compatible with the C one,
23643 but from a debugger point of view, it must be unsigned. */
23644 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23645 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23647 if (is_orig_unsigned != is_debug_unsigned)
23648 src = fold_convert (discr_type, src);
23650 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23651 return false;
23653 dest->pos = is_debug_unsigned;
23654 if (is_debug_unsigned)
23655 dest->v.uval = tree_to_uhwi (src);
23656 else
23657 dest->v.sval = tree_to_shwi (src);
23659 return true;
23662 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23663 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23664 store NULL_TREE in DISCR_DECL. Otherwise:
23666 - store the discriminant field in STRUCT_TYPE that controls the variant
23667 part to *DISCR_DECL
23669 - put in *DISCR_LISTS_P an array where for each variant, the item
23670 represents the corresponding matching list of discriminant values.
23672 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23673 the above array.
23675 Note that when the array is allocated (i.e. when the analysis is
23676 successful), it is up to the caller to free the array. */
23678 static void
23679 analyze_variants_discr (tree variant_part_decl,
23680 tree struct_type,
23681 tree *discr_decl,
23682 dw_discr_list_ref **discr_lists_p,
23683 unsigned *discr_lists_length)
23685 tree variant_part_type = TREE_TYPE (variant_part_decl);
23686 tree variant;
23687 dw_discr_list_ref *discr_lists;
23688 unsigned i;
23690 /* Compute how many variants there are in this variant part. */
23691 *discr_lists_length = 0;
23692 for (variant = TYPE_FIELDS (variant_part_type);
23693 variant != NULL_TREE;
23694 variant = DECL_CHAIN (variant))
23695 ++*discr_lists_length;
23697 *discr_decl = NULL_TREE;
23698 *discr_lists_p
23699 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23700 sizeof (**discr_lists_p));
23701 discr_lists = *discr_lists_p;
23703 /* And then analyze all variants to extract discriminant information for all
23704 of them. This analysis is conservative: as soon as we detect something we
23705 do not support, abort everything and pretend we found nothing. */
23706 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23707 variant != NULL_TREE;
23708 variant = DECL_CHAIN (variant), ++i)
23710 tree match_expr = DECL_QUALIFIER (variant);
23712 /* Now, try to analyze the predicate and deduce a discriminant for
23713 it. */
23714 if (match_expr == boolean_true_node)
23715 /* Typically happens for the default variant: it matches all cases that
23716 previous variants rejected. Don't output any matching value for
23717 this one. */
23718 continue;
23720 /* The following loop tries to iterate over each discriminant
23721 possibility: single values or ranges. */
23722 while (match_expr != NULL_TREE)
23724 tree next_round_match_expr;
23725 tree candidate_discr = NULL_TREE;
23726 dw_discr_list_ref new_node = NULL;
23728 /* Possibilities are matched one after the other by nested
23729 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23730 continue with the rest at next iteration. */
23731 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23733 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23734 match_expr = TREE_OPERAND (match_expr, 1);
23736 else
23737 next_round_match_expr = NULL_TREE;
23739 if (match_expr == boolean_false_node)
23740 /* This sub-expression matches nothing: just wait for the next
23741 one. */
23744 else if (TREE_CODE (match_expr) == EQ_EXPR)
23746 /* We are matching: <discr_field> == <integer_cst>
23747 This sub-expression matches a single value. */
23748 tree integer_cst = TREE_OPERAND (match_expr, 1);
23750 candidate_discr
23751 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23752 struct_type);
23754 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23755 if (!get_discr_value (integer_cst,
23756 &new_node->dw_discr_lower_bound))
23757 goto abort;
23758 new_node->dw_discr_range = false;
23761 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23763 /* We are matching:
23764 <discr_field> > <integer_cst>
23765 && <discr_field> < <integer_cst>.
23766 This sub-expression matches the range of values between the
23767 two matched integer constants. Note that comparisons can be
23768 inclusive or exclusive. */
23769 tree candidate_discr_1, candidate_discr_2;
23770 tree lower_cst, upper_cst;
23771 bool lower_cst_included, upper_cst_included;
23772 tree lower_op = TREE_OPERAND (match_expr, 0);
23773 tree upper_op = TREE_OPERAND (match_expr, 1);
23775 /* When the comparison is exclusive, the integer constant is not
23776 the discriminant range bound we are looking for: we will have
23777 to increment or decrement it. */
23778 if (TREE_CODE (lower_op) == GE_EXPR)
23779 lower_cst_included = true;
23780 else if (TREE_CODE (lower_op) == GT_EXPR)
23781 lower_cst_included = false;
23782 else
23783 goto abort;
23785 if (TREE_CODE (upper_op) == LE_EXPR)
23786 upper_cst_included = true;
23787 else if (TREE_CODE (upper_op) == LT_EXPR)
23788 upper_cst_included = false;
23789 else
23790 goto abort;
23792 /* Extract the discriminant from the first operand and check it
23793 is consistant with the same analysis in the second
23794 operand. */
23795 candidate_discr_1
23796 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23797 struct_type);
23798 candidate_discr_2
23799 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23800 struct_type);
23801 if (candidate_discr_1 == candidate_discr_2)
23802 candidate_discr = candidate_discr_1;
23803 else
23804 goto abort;
23806 /* Extract bounds from both. */
23807 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23808 lower_cst = TREE_OPERAND (lower_op, 1);
23809 upper_cst = TREE_OPERAND (upper_op, 1);
23811 if (!lower_cst_included)
23812 lower_cst
23813 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23814 build_int_cst (TREE_TYPE (lower_cst), 1));
23815 if (!upper_cst_included)
23816 upper_cst
23817 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23818 build_int_cst (TREE_TYPE (upper_cst), 1));
23820 if (!get_discr_value (lower_cst,
23821 &new_node->dw_discr_lower_bound)
23822 || !get_discr_value (upper_cst,
23823 &new_node->dw_discr_upper_bound))
23824 goto abort;
23826 new_node->dw_discr_range = true;
23829 else
23830 /* Unsupported sub-expression: we cannot determine the set of
23831 matching discriminant values. Abort everything. */
23832 goto abort;
23834 /* If the discriminant info is not consistant with what we saw so
23835 far, consider the analysis failed and abort everything. */
23836 if (candidate_discr == NULL_TREE
23837 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23838 goto abort;
23839 else
23840 *discr_decl = candidate_discr;
23842 if (new_node != NULL)
23844 new_node->dw_discr_next = discr_lists[i];
23845 discr_lists[i] = new_node;
23847 match_expr = next_round_match_expr;
23851 /* If we reach this point, we could match everything we were interested
23852 in. */
23853 return;
23855 abort:
23856 /* Clean all data structure and return no result. */
23857 free (*discr_lists_p);
23858 *discr_lists_p = NULL;
23859 *discr_decl = NULL_TREE;
23862 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23863 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23864 under CONTEXT_DIE.
23866 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23867 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23868 this type, which are record types, represent the available variants and each
23869 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23870 values are inferred from these attributes.
23872 In trees, the offsets for the fields inside these sub-records are relative
23873 to the variant part itself, whereas the corresponding DIEs should have
23874 offset attributes that are relative to the embedding record base address.
23875 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23876 must be an expression that computes the offset of the variant part to
23877 describe in DWARF. */
23879 static void
23880 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23881 dw_die_ref context_die)
23883 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23884 tree variant_part_offset = vlr_ctx->variant_part_offset;
23885 struct loc_descr_context ctx = {
23886 vlr_ctx->struct_type, /* context_type */
23887 NULL_TREE, /* base_decl */
23888 NULL, /* dpi */
23889 false, /* placeholder_arg */
23890 false /* placeholder_seen */
23893 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23894 NULL_TREE if there is no such field. */
23895 tree discr_decl = NULL_TREE;
23896 dw_discr_list_ref *discr_lists;
23897 unsigned discr_lists_length = 0;
23898 unsigned i;
23900 dw_die_ref dwarf_proc_die = NULL;
23901 dw_die_ref variant_part_die
23902 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23904 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23906 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23907 &discr_decl, &discr_lists, &discr_lists_length);
23909 if (discr_decl != NULL_TREE)
23911 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23913 if (discr_die)
23914 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23915 else
23916 /* We have no DIE for the discriminant, so just discard all
23917 discrimimant information in the output. */
23918 discr_decl = NULL_TREE;
23921 /* If the offset for this variant part is more complex than a constant,
23922 create a DWARF procedure for it so that we will not have to generate DWARF
23923 expressions for it for each member. */
23924 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23925 && (dwarf_version >= 3 || !dwarf_strict))
23927 const tree dwarf_proc_fndecl
23928 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23929 build_function_type (TREE_TYPE (variant_part_offset),
23930 NULL_TREE));
23931 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23932 const dw_loc_descr_ref dwarf_proc_body
23933 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23935 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23936 dwarf_proc_fndecl, context_die);
23937 if (dwarf_proc_die != NULL)
23938 variant_part_offset = dwarf_proc_call;
23941 /* Output DIEs for all variants. */
23942 i = 0;
23943 for (tree variant = TYPE_FIELDS (variant_part_type);
23944 variant != NULL_TREE;
23945 variant = DECL_CHAIN (variant), ++i)
23947 tree variant_type = TREE_TYPE (variant);
23948 dw_die_ref variant_die;
23950 /* All variants (i.e. members of a variant part) are supposed to be
23951 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23952 under these records. */
23953 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23955 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23956 equate_decl_number_to_die (variant, variant_die);
23958 /* Output discriminant values this variant matches, if any. */
23959 if (discr_decl == NULL || discr_lists[i] == NULL)
23960 /* In the case we have discriminant information at all, this is
23961 probably the default variant: as the standard says, don't
23962 output any discriminant value/list attribute. */
23964 else if (discr_lists[i]->dw_discr_next == NULL
23965 && !discr_lists[i]->dw_discr_range)
23966 /* If there is only one accepted value, don't bother outputting a
23967 list. */
23968 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23969 else
23970 add_discr_list (variant_die, discr_lists[i]);
23972 for (tree member = TYPE_FIELDS (variant_type);
23973 member != NULL_TREE;
23974 member = DECL_CHAIN (member))
23976 struct vlr_context vlr_sub_ctx = {
23977 vlr_ctx->struct_type, /* struct_type */
23978 NULL /* variant_part_offset */
23980 if (is_variant_part (member))
23982 /* All offsets for fields inside variant parts are relative to
23983 the top-level embedding RECORD_TYPE's base address. On the
23984 other hand, offsets in GCC's types are relative to the
23985 nested-most variant part. So we have to sum offsets each time
23986 we recurse. */
23988 vlr_sub_ctx.variant_part_offset
23989 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23990 variant_part_offset, byte_position (member));
23991 gen_variant_part (member, &vlr_sub_ctx, variant_die);
23993 else
23995 vlr_sub_ctx.variant_part_offset = variant_part_offset;
23996 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24001 free (discr_lists);
24004 /* Generate a DIE for a class member. */
24006 static void
24007 gen_member_die (tree type, dw_die_ref context_die)
24009 tree member;
24010 tree binfo = TYPE_BINFO (type);
24012 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24014 /* If this is not an incomplete type, output descriptions of each of its
24015 members. Note that as we output the DIEs necessary to represent the
24016 members of this record or union type, we will also be trying to output
24017 DIEs to represent the *types* of those members. However the `type'
24018 function (above) will specifically avoid generating type DIEs for member
24019 types *within* the list of member DIEs for this (containing) type except
24020 for those types (of members) which are explicitly marked as also being
24021 members of this (containing) type themselves. The g++ front- end can
24022 force any given type to be treated as a member of some other (containing)
24023 type by setting the TYPE_CONTEXT of the given (member) type to point to
24024 the TREE node representing the appropriate (containing) type. */
24026 /* First output info about the base classes. */
24027 if (binfo)
24029 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24030 int i;
24031 tree base;
24033 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24034 gen_inheritance_die (base,
24035 (accesses ? (*accesses)[i] : access_public_node),
24036 type,
24037 context_die);
24040 /* Now output info about the data members and type members. */
24041 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24043 struct vlr_context vlr_ctx = { type, NULL_TREE };
24044 bool static_inline_p
24045 = (TREE_STATIC (member)
24046 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24047 != -1));
24049 /* Ignore clones. */
24050 if (DECL_ABSTRACT_ORIGIN (member))
24051 continue;
24053 /* If we thought we were generating minimal debug info for TYPE
24054 and then changed our minds, some of the member declarations
24055 may have already been defined. Don't define them again, but
24056 do put them in the right order. */
24058 if (dw_die_ref child = lookup_decl_die (member))
24060 /* Handle inline static data members, which only have in-class
24061 declarations. */
24062 dw_die_ref ref = NULL;
24063 if (child->die_tag == DW_TAG_variable
24064 && child->die_parent == comp_unit_die ())
24066 ref = get_AT_ref (child, DW_AT_specification);
24067 /* For C++17 inline static data members followed by redundant
24068 out of class redeclaration, we might get here with
24069 child being the DIE created for the out of class
24070 redeclaration and with its DW_AT_specification being
24071 the DIE created for in-class definition. We want to
24072 reparent the latter, and don't want to create another
24073 DIE with DW_AT_specification in that case, because
24074 we already have one. */
24075 if (ref
24076 && static_inline_p
24077 && ref->die_tag == DW_TAG_variable
24078 && ref->die_parent == comp_unit_die ()
24079 && get_AT (ref, DW_AT_specification) == NULL)
24081 child = ref;
24082 ref = NULL;
24083 static_inline_p = false;
24087 if (child->die_tag == DW_TAG_variable
24088 && child->die_parent == comp_unit_die ()
24089 && ref == NULL)
24091 reparent_child (child, context_die);
24092 if (dwarf_version < 5)
24093 child->die_tag = DW_TAG_member;
24095 else
24096 splice_child_die (context_die, child);
24099 /* Do not generate standard DWARF for variant parts if we are generating
24100 the corresponding GNAT encodings: DIEs generated for both would
24101 conflict in our mappings. */
24102 else if (is_variant_part (member)
24103 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24105 vlr_ctx.variant_part_offset = byte_position (member);
24106 gen_variant_part (member, &vlr_ctx, context_die);
24108 else
24110 vlr_ctx.variant_part_offset = NULL_TREE;
24111 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24114 /* For C++ inline static data members emit immediately a DW_TAG_variable
24115 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24116 DW_AT_specification. */
24117 if (static_inline_p)
24119 int old_extern = DECL_EXTERNAL (member);
24120 DECL_EXTERNAL (member) = 0;
24121 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24122 DECL_EXTERNAL (member) = old_extern;
24127 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24128 is set, we pretend that the type was never defined, so we only get the
24129 member DIEs needed by later specification DIEs. */
24131 static void
24132 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24133 enum debug_info_usage usage)
24135 if (TREE_ASM_WRITTEN (type))
24137 /* Fill in the bound of variable-length fields in late dwarf if
24138 still incomplete. */
24139 if (!early_dwarf && variably_modified_type_p (type, NULL))
24140 for (tree member = TYPE_FIELDS (type);
24141 member;
24142 member = DECL_CHAIN (member))
24143 fill_variable_array_bounds (TREE_TYPE (member));
24144 return;
24147 dw_die_ref type_die = lookup_type_die (type);
24148 dw_die_ref scope_die = 0;
24149 int nested = 0;
24150 int complete = (TYPE_SIZE (type)
24151 && (! TYPE_STUB_DECL (type)
24152 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24153 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24154 complete = complete && should_emit_struct_debug (type, usage);
24156 if (type_die && ! complete)
24157 return;
24159 if (TYPE_CONTEXT (type) != NULL_TREE
24160 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24161 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24162 nested = 1;
24164 scope_die = scope_die_for (type, context_die);
24166 /* Generate child dies for template paramaters. */
24167 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24168 schedule_generic_params_dies_gen (type);
24170 if (! type_die || (nested && is_cu_die (scope_die)))
24171 /* First occurrence of type or toplevel definition of nested class. */
24173 dw_die_ref old_die = type_die;
24175 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24176 ? record_type_tag (type) : DW_TAG_union_type,
24177 scope_die, type);
24178 equate_type_number_to_die (type, type_die);
24179 if (old_die)
24180 add_AT_specification (type_die, old_die);
24181 else
24182 add_name_attribute (type_die, type_tag (type));
24184 else
24185 remove_AT (type_die, DW_AT_declaration);
24187 /* If this type has been completed, then give it a byte_size attribute and
24188 then give a list of members. */
24189 if (complete && !ns_decl)
24191 /* Prevent infinite recursion in cases where the type of some member of
24192 this type is expressed in terms of this type itself. */
24193 TREE_ASM_WRITTEN (type) = 1;
24194 add_byte_size_attribute (type_die, type);
24195 add_alignment_attribute (type_die, type);
24196 if (TYPE_STUB_DECL (type) != NULL_TREE)
24198 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24199 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24202 /* If the first reference to this type was as the return type of an
24203 inline function, then it may not have a parent. Fix this now. */
24204 if (type_die->die_parent == NULL)
24205 add_child_die (scope_die, type_die);
24207 push_decl_scope (type);
24208 gen_member_die (type, type_die);
24209 pop_decl_scope ();
24211 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24212 if (TYPE_ARTIFICIAL (type))
24213 add_AT_flag (type_die, DW_AT_artificial, 1);
24215 /* GNU extension: Record what type our vtable lives in. */
24216 if (TYPE_VFIELD (type))
24218 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24220 gen_type_die (vtype, context_die);
24221 add_AT_die_ref (type_die, DW_AT_containing_type,
24222 lookup_type_die (vtype));
24225 else
24227 add_AT_flag (type_die, DW_AT_declaration, 1);
24229 /* We don't need to do this for function-local types. */
24230 if (TYPE_STUB_DECL (type)
24231 && ! decl_function_context (TYPE_STUB_DECL (type)))
24232 vec_safe_push (incomplete_types, type);
24235 if (get_AT (type_die, DW_AT_name))
24236 add_pubtype (type, type_die);
24239 /* Generate a DIE for a subroutine _type_. */
24241 static void
24242 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24244 tree return_type = TREE_TYPE (type);
24245 dw_die_ref subr_die
24246 = new_die (DW_TAG_subroutine_type,
24247 scope_die_for (type, context_die), type);
24249 equate_type_number_to_die (type, subr_die);
24250 add_prototyped_attribute (subr_die, type);
24251 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24252 context_die);
24253 add_alignment_attribute (subr_die, type);
24254 gen_formal_types_die (type, subr_die);
24256 if (get_AT (subr_die, DW_AT_name))
24257 add_pubtype (type, subr_die);
24258 if ((dwarf_version >= 5 || !dwarf_strict)
24259 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24260 add_AT_flag (subr_die, DW_AT_reference, 1);
24261 if ((dwarf_version >= 5 || !dwarf_strict)
24262 && lang_hooks.types.type_dwarf_attribute (type,
24263 DW_AT_rvalue_reference) != -1)
24264 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24267 /* Generate a DIE for a type definition. */
24269 static void
24270 gen_typedef_die (tree decl, dw_die_ref context_die)
24272 dw_die_ref type_die;
24273 tree type;
24275 if (TREE_ASM_WRITTEN (decl))
24277 if (DECL_ORIGINAL_TYPE (decl))
24278 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24279 return;
24282 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24283 checks in process_scope_var and modified_type_die), this should be called
24284 only for original types. */
24285 gcc_assert (decl_ultimate_origin (decl) == NULL
24286 || decl_ultimate_origin (decl) == decl);
24288 TREE_ASM_WRITTEN (decl) = 1;
24289 type_die = new_die (DW_TAG_typedef, context_die, decl);
24291 add_name_and_src_coords_attributes (type_die, decl);
24292 if (DECL_ORIGINAL_TYPE (decl))
24294 type = DECL_ORIGINAL_TYPE (decl);
24295 if (type == error_mark_node)
24296 return;
24298 gcc_assert (type != TREE_TYPE (decl));
24299 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24301 else
24303 type = TREE_TYPE (decl);
24304 if (type == error_mark_node)
24305 return;
24307 if (is_naming_typedef_decl (TYPE_NAME (type)))
24309 /* Here, we are in the case of decl being a typedef naming
24310 an anonymous type, e.g:
24311 typedef struct {...} foo;
24312 In that case TREE_TYPE (decl) is not a typedef variant
24313 type and TYPE_NAME of the anonymous type is set to the
24314 TYPE_DECL of the typedef. This construct is emitted by
24315 the C++ FE.
24317 TYPE is the anonymous struct named by the typedef
24318 DECL. As we need the DW_AT_type attribute of the
24319 DW_TAG_typedef to point to the DIE of TYPE, let's
24320 generate that DIE right away. add_type_attribute
24321 called below will then pick (via lookup_type_die) that
24322 anonymous struct DIE. */
24323 if (!TREE_ASM_WRITTEN (type))
24324 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24326 /* This is a GNU Extension. We are adding a
24327 DW_AT_linkage_name attribute to the DIE of the
24328 anonymous struct TYPE. The value of that attribute
24329 is the name of the typedef decl naming the anonymous
24330 struct. This greatly eases the work of consumers of
24331 this debug info. */
24332 add_linkage_name_raw (lookup_type_die (type), decl);
24336 add_type_attribute (type_die, type, decl_quals (decl), false,
24337 context_die);
24339 if (is_naming_typedef_decl (decl))
24340 /* We want that all subsequent calls to lookup_type_die with
24341 TYPE in argument yield the DW_TAG_typedef we have just
24342 created. */
24343 equate_type_number_to_die (type, type_die);
24345 add_alignment_attribute (type_die, TREE_TYPE (decl));
24347 add_accessibility_attribute (type_die, decl);
24349 if (DECL_ABSTRACT_P (decl))
24350 equate_decl_number_to_die (decl, type_die);
24352 if (get_AT (type_die, DW_AT_name))
24353 add_pubtype (decl, type_die);
24356 /* Generate a DIE for a struct, class, enum or union type. */
24358 static void
24359 gen_tagged_type_die (tree type,
24360 dw_die_ref context_die,
24361 enum debug_info_usage usage)
24363 int need_pop;
24365 if (type == NULL_TREE
24366 || !is_tagged_type (type))
24367 return;
24369 if (TREE_ASM_WRITTEN (type))
24370 need_pop = 0;
24371 /* If this is a nested type whose containing class hasn't been written
24372 out yet, writing it out will cover this one, too. This does not apply
24373 to instantiations of member class templates; they need to be added to
24374 the containing class as they are generated. FIXME: This hurts the
24375 idea of combining type decls from multiple TUs, since we can't predict
24376 what set of template instantiations we'll get. */
24377 else if (TYPE_CONTEXT (type)
24378 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24379 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24381 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24383 if (TREE_ASM_WRITTEN (type))
24384 return;
24386 /* If that failed, attach ourselves to the stub. */
24387 push_decl_scope (TYPE_CONTEXT (type));
24388 context_die = lookup_type_die (TYPE_CONTEXT (type));
24389 need_pop = 1;
24391 else if (TYPE_CONTEXT (type) != NULL_TREE
24392 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24394 /* If this type is local to a function that hasn't been written
24395 out yet, use a NULL context for now; it will be fixed up in
24396 decls_for_scope. */
24397 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24398 /* A declaration DIE doesn't count; nested types need to go in the
24399 specification. */
24400 if (context_die && is_declaration_die (context_die))
24401 context_die = NULL;
24402 need_pop = 0;
24404 else
24406 context_die = declare_in_namespace (type, context_die);
24407 need_pop = 0;
24410 if (TREE_CODE (type) == ENUMERAL_TYPE)
24412 /* This might have been written out by the call to
24413 declare_in_namespace. */
24414 if (!TREE_ASM_WRITTEN (type))
24415 gen_enumeration_type_die (type, context_die);
24417 else
24418 gen_struct_or_union_type_die (type, context_die, usage);
24420 if (need_pop)
24421 pop_decl_scope ();
24423 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24424 it up if it is ever completed. gen_*_type_die will set it for us
24425 when appropriate. */
24428 /* Generate a type description DIE. */
24430 static void
24431 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24432 enum debug_info_usage usage)
24434 struct array_descr_info info;
24436 if (type == NULL_TREE || type == error_mark_node)
24437 return;
24439 if (flag_checking && type)
24440 verify_type (type);
24442 if (TYPE_NAME (type) != NULL_TREE
24443 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24444 && is_redundant_typedef (TYPE_NAME (type))
24445 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24446 /* The DECL of this type is a typedef we don't want to emit debug
24447 info for but we want debug info for its underlying typedef.
24448 This can happen for e.g, the injected-class-name of a C++
24449 type. */
24450 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24452 /* If TYPE is a typedef type variant, let's generate debug info
24453 for the parent typedef which TYPE is a type of. */
24454 if (typedef_variant_p (type))
24456 if (TREE_ASM_WRITTEN (type))
24457 return;
24459 tree name = TYPE_NAME (type);
24460 tree origin = decl_ultimate_origin (name);
24461 if (origin != NULL && origin != name)
24463 gen_decl_die (origin, NULL, NULL, context_die);
24464 return;
24467 /* Prevent broken recursion; we can't hand off to the same type. */
24468 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24470 /* Give typedefs the right scope. */
24471 context_die = scope_die_for (type, context_die);
24473 TREE_ASM_WRITTEN (type) = 1;
24475 gen_decl_die (name, NULL, NULL, context_die);
24476 return;
24479 /* If type is an anonymous tagged type named by a typedef, let's
24480 generate debug info for the typedef. */
24481 if (is_naming_typedef_decl (TYPE_NAME (type)))
24483 /* Use the DIE of the containing namespace as the parent DIE of
24484 the type description DIE we want to generate. */
24485 if (DECL_CONTEXT (TYPE_NAME (type))
24486 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24487 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24489 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24490 return;
24493 if (lang_hooks.types.get_debug_type)
24495 tree debug_type = lang_hooks.types.get_debug_type (type);
24497 if (debug_type != NULL_TREE && debug_type != type)
24499 gen_type_die_with_usage (debug_type, context_die, usage);
24500 return;
24504 /* We are going to output a DIE to represent the unqualified version
24505 of this type (i.e. without any const or volatile qualifiers) so
24506 get the main variant (i.e. the unqualified version) of this type
24507 now. (Vectors and arrays are special because the debugging info is in the
24508 cloned type itself. Similarly function/method types can contain extra
24509 ref-qualification). */
24510 if (TREE_CODE (type) == FUNCTION_TYPE
24511 || TREE_CODE (type) == METHOD_TYPE)
24513 /* For function/method types, can't use type_main_variant here,
24514 because that can have different ref-qualifiers for C++,
24515 but try to canonicalize. */
24516 tree main = TYPE_MAIN_VARIANT (type);
24517 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24518 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24519 && check_base_type (t, main)
24520 && check_lang_type (t, type))
24522 type = t;
24523 break;
24526 else if (TREE_CODE (type) != VECTOR_TYPE
24527 && TREE_CODE (type) != ARRAY_TYPE)
24528 type = type_main_variant (type);
24530 /* If this is an array type with hidden descriptor, handle it first. */
24531 if (!TREE_ASM_WRITTEN (type)
24532 && lang_hooks.types.get_array_descr_info)
24534 memset (&info, 0, sizeof (info));
24535 if (lang_hooks.types.get_array_descr_info (type, &info))
24537 /* Fortran sometimes emits array types with no dimension. */
24538 gcc_assert (info.ndimensions >= 0
24539 && (info.ndimensions
24540 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24541 gen_descr_array_type_die (type, &info, context_die);
24542 TREE_ASM_WRITTEN (type) = 1;
24543 return;
24547 if (TREE_ASM_WRITTEN (type))
24549 /* Variable-length types may be incomplete even if
24550 TREE_ASM_WRITTEN. For such types, fall through to
24551 gen_array_type_die() and possibly fill in
24552 DW_AT_{upper,lower}_bound attributes. */
24553 if ((TREE_CODE (type) != ARRAY_TYPE
24554 && TREE_CODE (type) != RECORD_TYPE
24555 && TREE_CODE (type) != UNION_TYPE
24556 && TREE_CODE (type) != QUAL_UNION_TYPE)
24557 || !variably_modified_type_p (type, NULL))
24558 return;
24561 switch (TREE_CODE (type))
24563 case ERROR_MARK:
24564 break;
24566 case POINTER_TYPE:
24567 case REFERENCE_TYPE:
24568 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24569 ensures that the gen_type_die recursion will terminate even if the
24570 type is recursive. Recursive types are possible in Ada. */
24571 /* ??? We could perhaps do this for all types before the switch
24572 statement. */
24573 TREE_ASM_WRITTEN (type) = 1;
24575 /* For these types, all that is required is that we output a DIE (or a
24576 set of DIEs) to represent the "basis" type. */
24577 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24578 DINFO_USAGE_IND_USE);
24579 break;
24581 case OFFSET_TYPE:
24582 /* This code is used for C++ pointer-to-data-member types.
24583 Output a description of the relevant class type. */
24584 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24585 DINFO_USAGE_IND_USE);
24587 /* Output a description of the type of the object pointed to. */
24588 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24589 DINFO_USAGE_IND_USE);
24591 /* Now output a DIE to represent this pointer-to-data-member type
24592 itself. */
24593 gen_ptr_to_mbr_type_die (type, context_die);
24594 break;
24596 case FUNCTION_TYPE:
24597 /* Force out return type (in case it wasn't forced out already). */
24598 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24599 DINFO_USAGE_DIR_USE);
24600 gen_subroutine_type_die (type, context_die);
24601 break;
24603 case METHOD_TYPE:
24604 /* Force out return type (in case it wasn't forced out already). */
24605 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24606 DINFO_USAGE_DIR_USE);
24607 gen_subroutine_type_die (type, context_die);
24608 break;
24610 case ARRAY_TYPE:
24611 case VECTOR_TYPE:
24612 gen_array_type_die (type, context_die);
24613 break;
24615 case ENUMERAL_TYPE:
24616 case RECORD_TYPE:
24617 case UNION_TYPE:
24618 case QUAL_UNION_TYPE:
24619 gen_tagged_type_die (type, context_die, usage);
24620 return;
24622 case VOID_TYPE:
24623 case INTEGER_TYPE:
24624 case REAL_TYPE:
24625 case FIXED_POINT_TYPE:
24626 case COMPLEX_TYPE:
24627 case BOOLEAN_TYPE:
24628 case POINTER_BOUNDS_TYPE:
24629 /* No DIEs needed for fundamental types. */
24630 break;
24632 case NULLPTR_TYPE:
24633 case LANG_TYPE:
24634 /* Just use DW_TAG_unspecified_type. */
24636 dw_die_ref type_die = lookup_type_die (type);
24637 if (type_die == NULL)
24639 tree name = TYPE_IDENTIFIER (type);
24640 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24641 type);
24642 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24643 equate_type_number_to_die (type, type_die);
24646 break;
24648 default:
24649 if (is_cxx_auto (type))
24651 tree name = TYPE_IDENTIFIER (type);
24652 dw_die_ref *die = (name == get_identifier ("auto")
24653 ? &auto_die : &decltype_auto_die);
24654 if (!*die)
24656 *die = new_die (DW_TAG_unspecified_type,
24657 comp_unit_die (), NULL_TREE);
24658 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24660 equate_type_number_to_die (type, *die);
24661 break;
24663 gcc_unreachable ();
24666 TREE_ASM_WRITTEN (type) = 1;
24669 static void
24670 gen_type_die (tree type, dw_die_ref context_die)
24672 if (type != error_mark_node)
24674 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24675 if (flag_checking)
24677 dw_die_ref die = lookup_type_die (type);
24678 if (die)
24679 check_die (die);
24684 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24685 things which are local to the given block. */
24687 static void
24688 gen_block_die (tree stmt, dw_die_ref context_die)
24690 int must_output_die = 0;
24691 bool inlined_func;
24693 /* Ignore blocks that are NULL. */
24694 if (stmt == NULL_TREE)
24695 return;
24697 inlined_func = inlined_function_outer_scope_p (stmt);
24699 /* If the block is one fragment of a non-contiguous block, do not
24700 process the variables, since they will have been done by the
24701 origin block. Do process subblocks. */
24702 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24704 tree sub;
24706 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24707 gen_block_die (sub, context_die);
24709 return;
24712 /* Determine if we need to output any Dwarf DIEs at all to represent this
24713 block. */
24714 if (inlined_func)
24715 /* The outer scopes for inlinings *must* always be represented. We
24716 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24717 must_output_die = 1;
24718 else
24720 /* Determine if this block directly contains any "significant"
24721 local declarations which we will need to output DIEs for. */
24722 if (debug_info_level > DINFO_LEVEL_TERSE)
24723 /* We are not in terse mode so *any* local declaration counts
24724 as being a "significant" one. */
24725 must_output_die = ((BLOCK_VARS (stmt) != NULL
24726 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24727 && (TREE_USED (stmt)
24728 || TREE_ASM_WRITTEN (stmt)
24729 || BLOCK_ABSTRACT (stmt)));
24730 else if ((TREE_USED (stmt)
24731 || TREE_ASM_WRITTEN (stmt)
24732 || BLOCK_ABSTRACT (stmt))
24733 && !dwarf2out_ignore_block (stmt))
24734 must_output_die = 1;
24737 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24738 DIE for any block which contains no significant local declarations at
24739 all. Rather, in such cases we just call `decls_for_scope' so that any
24740 needed Dwarf info for any sub-blocks will get properly generated. Note
24741 that in terse mode, our definition of what constitutes a "significant"
24742 local declaration gets restricted to include only inlined function
24743 instances and local (nested) function definitions. */
24744 if (must_output_die)
24746 if (inlined_func)
24748 /* If STMT block is abstract, that means we have been called
24749 indirectly from dwarf2out_abstract_function.
24750 That function rightfully marks the descendent blocks (of
24751 the abstract function it is dealing with) as being abstract,
24752 precisely to prevent us from emitting any
24753 DW_TAG_inlined_subroutine DIE as a descendent
24754 of an abstract function instance. So in that case, we should
24755 not call gen_inlined_subroutine_die.
24757 Later though, when cgraph asks dwarf2out to emit info
24758 for the concrete instance of the function decl into which
24759 the concrete instance of STMT got inlined, the later will lead
24760 to the generation of a DW_TAG_inlined_subroutine DIE. */
24761 if (! BLOCK_ABSTRACT (stmt))
24762 gen_inlined_subroutine_die (stmt, context_die);
24764 else
24765 gen_lexical_block_die (stmt, context_die);
24767 else
24768 decls_for_scope (stmt, context_die);
24771 /* Process variable DECL (or variable with origin ORIGIN) within
24772 block STMT and add it to CONTEXT_DIE. */
24773 static void
24774 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24776 dw_die_ref die;
24777 tree decl_or_origin = decl ? decl : origin;
24779 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24780 die = lookup_decl_die (decl_or_origin);
24781 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24783 if (TYPE_DECL_IS_STUB (decl_or_origin))
24784 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24785 else
24786 die = lookup_decl_die (decl_or_origin);
24787 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24788 if (! die && ! early_dwarf)
24789 return;
24791 else
24792 die = NULL;
24794 /* Avoid creating DIEs for local typedefs and concrete static variables that
24795 will only be pruned later. */
24796 if ((origin || decl_ultimate_origin (decl))
24797 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24798 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24800 origin = decl_ultimate_origin (decl_or_origin);
24801 if (decl && VAR_P (decl) && die != NULL)
24803 die = lookup_decl_die (origin);
24804 if (die != NULL)
24805 equate_decl_number_to_die (decl, die);
24807 return;
24810 if (die != NULL && die->die_parent == NULL)
24811 add_child_die (context_die, die);
24812 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24814 if (early_dwarf)
24815 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24816 stmt, context_die);
24818 else
24820 if (decl && DECL_P (decl))
24822 die = lookup_decl_die (decl);
24824 /* Early created DIEs do not have a parent as the decls refer
24825 to the function as DECL_CONTEXT rather than the BLOCK. */
24826 if (die && die->die_parent == NULL)
24828 gcc_assert (in_lto_p);
24829 add_child_die (context_die, die);
24833 gen_decl_die (decl, origin, NULL, context_die);
24837 /* Generate all of the decls declared within a given scope and (recursively)
24838 all of its sub-blocks. */
24840 static void
24841 decls_for_scope (tree stmt, dw_die_ref context_die)
24843 tree decl;
24844 unsigned int i;
24845 tree subblocks;
24847 /* Ignore NULL blocks. */
24848 if (stmt == NULL_TREE)
24849 return;
24851 /* Output the DIEs to represent all of the data objects and typedefs
24852 declared directly within this block but not within any nested
24853 sub-blocks. Also, nested function and tag DIEs have been
24854 generated with a parent of NULL; fix that up now. We don't
24855 have to do this if we're at -g1. */
24856 if (debug_info_level > DINFO_LEVEL_TERSE)
24858 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24859 process_scope_var (stmt, decl, NULL_TREE, context_die);
24860 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24861 origin - avoid doing this twice as we have no good way to see
24862 if we've done it once already. */
24863 if (! early_dwarf)
24864 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24866 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24867 if (decl == current_function_decl)
24868 /* Ignore declarations of the current function, while they
24869 are declarations, gen_subprogram_die would treat them
24870 as definitions again, because they are equal to
24871 current_function_decl and endlessly recurse. */;
24872 else if (TREE_CODE (decl) == FUNCTION_DECL)
24873 process_scope_var (stmt, decl, NULL_TREE, context_die);
24874 else
24875 process_scope_var (stmt, NULL_TREE, decl, context_die);
24879 /* Even if we're at -g1, we need to process the subblocks in order to get
24880 inlined call information. */
24882 /* Output the DIEs to represent all sub-blocks (and the items declared
24883 therein) of this block. */
24884 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24885 subblocks != NULL;
24886 subblocks = BLOCK_CHAIN (subblocks))
24887 gen_block_die (subblocks, context_die);
24890 /* Is this a typedef we can avoid emitting? */
24892 bool
24893 is_redundant_typedef (const_tree decl)
24895 if (TYPE_DECL_IS_STUB (decl))
24896 return true;
24898 if (DECL_ARTIFICIAL (decl)
24899 && DECL_CONTEXT (decl)
24900 && is_tagged_type (DECL_CONTEXT (decl))
24901 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24902 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24903 /* Also ignore the artificial member typedef for the class name. */
24904 return true;
24906 return false;
24909 /* Return TRUE if TYPE is a typedef that names a type for linkage
24910 purposes. This kind of typedefs is produced by the C++ FE for
24911 constructs like:
24913 typedef struct {...} foo;
24915 In that case, there is no typedef variant type produced for foo.
24916 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24917 struct type. */
24919 static bool
24920 is_naming_typedef_decl (const_tree decl)
24922 if (decl == NULL_TREE
24923 || TREE_CODE (decl) != TYPE_DECL
24924 || DECL_NAMELESS (decl)
24925 || !is_tagged_type (TREE_TYPE (decl))
24926 || DECL_IS_BUILTIN (decl)
24927 || is_redundant_typedef (decl)
24928 /* It looks like Ada produces TYPE_DECLs that are very similar
24929 to C++ naming typedefs but that have different
24930 semantics. Let's be specific to c++ for now. */
24931 || !is_cxx (decl))
24932 return FALSE;
24934 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24935 && TYPE_NAME (TREE_TYPE (decl)) == decl
24936 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24937 != TYPE_NAME (TREE_TYPE (decl))));
24940 /* Looks up the DIE for a context. */
24942 static inline dw_die_ref
24943 lookup_context_die (tree context)
24945 if (context)
24947 /* Find die that represents this context. */
24948 if (TYPE_P (context))
24950 context = TYPE_MAIN_VARIANT (context);
24951 dw_die_ref ctx = lookup_type_die (context);
24952 if (!ctx)
24953 return NULL;
24954 return strip_naming_typedef (context, ctx);
24956 else
24957 return lookup_decl_die (context);
24959 return comp_unit_die ();
24962 /* Returns the DIE for a context. */
24964 static inline dw_die_ref
24965 get_context_die (tree context)
24967 if (context)
24969 /* Find die that represents this context. */
24970 if (TYPE_P (context))
24972 context = TYPE_MAIN_VARIANT (context);
24973 return strip_naming_typedef (context, force_type_die (context));
24975 else
24976 return force_decl_die (context);
24978 return comp_unit_die ();
24981 /* Returns the DIE for decl. A DIE will always be returned. */
24983 static dw_die_ref
24984 force_decl_die (tree decl)
24986 dw_die_ref decl_die;
24987 unsigned saved_external_flag;
24988 tree save_fn = NULL_TREE;
24989 decl_die = lookup_decl_die (decl);
24990 if (!decl_die)
24992 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24994 decl_die = lookup_decl_die (decl);
24995 if (decl_die)
24996 return decl_die;
24998 switch (TREE_CODE (decl))
25000 case FUNCTION_DECL:
25001 /* Clear current_function_decl, so that gen_subprogram_die thinks
25002 that this is a declaration. At this point, we just want to force
25003 declaration die. */
25004 save_fn = current_function_decl;
25005 current_function_decl = NULL_TREE;
25006 gen_subprogram_die (decl, context_die);
25007 current_function_decl = save_fn;
25008 break;
25010 case VAR_DECL:
25011 /* Set external flag to force declaration die. Restore it after
25012 gen_decl_die() call. */
25013 saved_external_flag = DECL_EXTERNAL (decl);
25014 DECL_EXTERNAL (decl) = 1;
25015 gen_decl_die (decl, NULL, NULL, context_die);
25016 DECL_EXTERNAL (decl) = saved_external_flag;
25017 break;
25019 case NAMESPACE_DECL:
25020 if (dwarf_version >= 3 || !dwarf_strict)
25021 dwarf2out_decl (decl);
25022 else
25023 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25024 decl_die = comp_unit_die ();
25025 break;
25027 case TRANSLATION_UNIT_DECL:
25028 decl_die = comp_unit_die ();
25029 break;
25031 default:
25032 gcc_unreachable ();
25035 /* We should be able to find the DIE now. */
25036 if (!decl_die)
25037 decl_die = lookup_decl_die (decl);
25038 gcc_assert (decl_die);
25041 return decl_die;
25044 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25045 always returned. */
25047 static dw_die_ref
25048 force_type_die (tree type)
25050 dw_die_ref type_die;
25052 type_die = lookup_type_die (type);
25053 if (!type_die)
25055 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25057 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25058 false, context_die);
25059 gcc_assert (type_die);
25061 return type_die;
25064 /* Force out any required namespaces to be able to output DECL,
25065 and return the new context_die for it, if it's changed. */
25067 static dw_die_ref
25068 setup_namespace_context (tree thing, dw_die_ref context_die)
25070 tree context = (DECL_P (thing)
25071 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25072 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25073 /* Force out the namespace. */
25074 context_die = force_decl_die (context);
25076 return context_die;
25079 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25080 type) within its namespace, if appropriate.
25082 For compatibility with older debuggers, namespace DIEs only contain
25083 declarations; all definitions are emitted at CU scope, with
25084 DW_AT_specification pointing to the declaration (like with class
25085 members). */
25087 static dw_die_ref
25088 declare_in_namespace (tree thing, dw_die_ref context_die)
25090 dw_die_ref ns_context;
25092 if (debug_info_level <= DINFO_LEVEL_TERSE)
25093 return context_die;
25095 /* External declarations in the local scope only need to be emitted
25096 once, not once in the namespace and once in the scope.
25098 This avoids declaring the `extern' below in the
25099 namespace DIE as well as in the innermost scope:
25101 namespace S
25103 int i=5;
25104 int foo()
25106 int i=8;
25107 extern int i;
25108 return i;
25112 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25113 return context_die;
25115 /* If this decl is from an inlined function, then don't try to emit it in its
25116 namespace, as we will get confused. It would have already been emitted
25117 when the abstract instance of the inline function was emitted anyways. */
25118 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25119 return context_die;
25121 ns_context = setup_namespace_context (thing, context_die);
25123 if (ns_context != context_die)
25125 if (is_fortran ())
25126 return ns_context;
25127 if (DECL_P (thing))
25128 gen_decl_die (thing, NULL, NULL, ns_context);
25129 else
25130 gen_type_die (thing, ns_context);
25132 return context_die;
25135 /* Generate a DIE for a namespace or namespace alias. */
25137 static void
25138 gen_namespace_die (tree decl, dw_die_ref context_die)
25140 dw_die_ref namespace_die;
25142 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25143 they are an alias of. */
25144 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25146 /* Output a real namespace or module. */
25147 context_die = setup_namespace_context (decl, comp_unit_die ());
25148 namespace_die = new_die (is_fortran ()
25149 ? DW_TAG_module : DW_TAG_namespace,
25150 context_die, decl);
25151 /* For Fortran modules defined in different CU don't add src coords. */
25152 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25154 const char *name = dwarf2_name (decl, 0);
25155 if (name)
25156 add_name_attribute (namespace_die, name);
25158 else
25159 add_name_and_src_coords_attributes (namespace_die, decl);
25160 if (DECL_EXTERNAL (decl))
25161 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25162 equate_decl_number_to_die (decl, namespace_die);
25164 else
25166 /* Output a namespace alias. */
25168 /* Force out the namespace we are an alias of, if necessary. */
25169 dw_die_ref origin_die
25170 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25172 if (DECL_FILE_SCOPE_P (decl)
25173 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25174 context_die = setup_namespace_context (decl, comp_unit_die ());
25175 /* Now create the namespace alias DIE. */
25176 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25177 add_name_and_src_coords_attributes (namespace_die, decl);
25178 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25179 equate_decl_number_to_die (decl, namespace_die);
25181 if ((dwarf_version >= 5 || !dwarf_strict)
25182 && lang_hooks.decls.decl_dwarf_attribute (decl,
25183 DW_AT_export_symbols) == 1)
25184 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25186 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25187 if (want_pubnames ())
25188 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25191 /* Generate Dwarf debug information for a decl described by DECL.
25192 The return value is currently only meaningful for PARM_DECLs,
25193 for all other decls it returns NULL.
25195 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25196 It can be NULL otherwise. */
25198 static dw_die_ref
25199 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25200 dw_die_ref context_die)
25202 tree decl_or_origin = decl ? decl : origin;
25203 tree class_origin = NULL, ultimate_origin;
25205 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25206 return NULL;
25208 /* Ignore pointer bounds decls. */
25209 if (DECL_P (decl_or_origin)
25210 && TREE_TYPE (decl_or_origin)
25211 && POINTER_BOUNDS_P (decl_or_origin))
25212 return NULL;
25214 switch (TREE_CODE (decl_or_origin))
25216 case ERROR_MARK:
25217 break;
25219 case CONST_DECL:
25220 if (!is_fortran () && !is_ada ())
25222 /* The individual enumerators of an enum type get output when we output
25223 the Dwarf representation of the relevant enum type itself. */
25224 break;
25227 /* Emit its type. */
25228 gen_type_die (TREE_TYPE (decl), context_die);
25230 /* And its containing namespace. */
25231 context_die = declare_in_namespace (decl, context_die);
25233 gen_const_die (decl, context_die);
25234 break;
25236 case FUNCTION_DECL:
25237 #if 0
25238 /* FIXME */
25239 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25240 on local redeclarations of global functions. That seems broken. */
25241 if (current_function_decl != decl)
25242 /* This is only a declaration. */;
25243 #endif
25245 /* We should have abstract copies already and should not generate
25246 stray type DIEs in late LTO dumping. */
25247 if (! early_dwarf)
25250 /* If we're emitting a clone, emit info for the abstract instance. */
25251 else if (origin || DECL_ORIGIN (decl) != decl)
25252 dwarf2out_abstract_function (origin
25253 ? DECL_ORIGIN (origin)
25254 : DECL_ABSTRACT_ORIGIN (decl));
25256 /* If we're emitting a possibly inlined function emit it as
25257 abstract instance. */
25258 else if (cgraph_function_possibly_inlined_p (decl)
25259 && ! DECL_ABSTRACT_P (decl)
25260 && ! class_or_namespace_scope_p (context_die)
25261 /* dwarf2out_abstract_function won't emit a die if this is just
25262 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25263 that case, because that works only if we have a die. */
25264 && DECL_INITIAL (decl) != NULL_TREE)
25265 dwarf2out_abstract_function (decl);
25267 /* Otherwise we're emitting the primary DIE for this decl. */
25268 else if (debug_info_level > DINFO_LEVEL_TERSE)
25270 /* Before we describe the FUNCTION_DECL itself, make sure that we
25271 have its containing type. */
25272 if (!origin)
25273 origin = decl_class_context (decl);
25274 if (origin != NULL_TREE)
25275 gen_type_die (origin, context_die);
25277 /* And its return type. */
25278 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25280 /* And its virtual context. */
25281 if (DECL_VINDEX (decl) != NULL_TREE)
25282 gen_type_die (DECL_CONTEXT (decl), context_die);
25284 /* Make sure we have a member DIE for decl. */
25285 if (origin != NULL_TREE)
25286 gen_type_die_for_member (origin, decl, context_die);
25288 /* And its containing namespace. */
25289 context_die = declare_in_namespace (decl, context_die);
25292 /* Now output a DIE to represent the function itself. */
25293 if (decl)
25294 gen_subprogram_die (decl, context_die);
25295 break;
25297 case TYPE_DECL:
25298 /* If we are in terse mode, don't generate any DIEs to represent any
25299 actual typedefs. */
25300 if (debug_info_level <= DINFO_LEVEL_TERSE)
25301 break;
25303 /* In the special case of a TYPE_DECL node representing the declaration
25304 of some type tag, if the given TYPE_DECL is marked as having been
25305 instantiated from some other (original) TYPE_DECL node (e.g. one which
25306 was generated within the original definition of an inline function) we
25307 used to generate a special (abbreviated) DW_TAG_structure_type,
25308 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25309 should be actually referencing those DIEs, as variable DIEs with that
25310 type would be emitted already in the abstract origin, so it was always
25311 removed during unused type prunning. Don't add anything in this
25312 case. */
25313 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25314 break;
25316 if (is_redundant_typedef (decl))
25317 gen_type_die (TREE_TYPE (decl), context_die);
25318 else
25319 /* Output a DIE to represent the typedef itself. */
25320 gen_typedef_die (decl, context_die);
25321 break;
25323 case LABEL_DECL:
25324 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25325 gen_label_die (decl, context_die);
25326 break;
25328 case VAR_DECL:
25329 case RESULT_DECL:
25330 /* If we are in terse mode, don't generate any DIEs to represent any
25331 variable declarations or definitions. */
25332 if (debug_info_level <= DINFO_LEVEL_TERSE)
25333 break;
25335 /* Avoid generating stray type DIEs during late dwarf dumping.
25336 All types have been dumped early. */
25337 if (early_dwarf
25338 /* ??? But in LTRANS we cannot annotate early created variably
25339 modified type DIEs without copying them and adjusting all
25340 references to them. Dump them again as happens for inlining
25341 which copies both the decl and the types. */
25342 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25343 in VLA bound information for example. */
25344 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25345 current_function_decl)))
25347 /* Output any DIEs that are needed to specify the type of this data
25348 object. */
25349 if (decl_by_reference_p (decl_or_origin))
25350 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25351 else
25352 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25355 if (early_dwarf)
25357 /* And its containing type. */
25358 class_origin = decl_class_context (decl_or_origin);
25359 if (class_origin != NULL_TREE)
25360 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25362 /* And its containing namespace. */
25363 context_die = declare_in_namespace (decl_or_origin, context_die);
25366 /* Now output the DIE to represent the data object itself. This gets
25367 complicated because of the possibility that the VAR_DECL really
25368 represents an inlined instance of a formal parameter for an inline
25369 function. */
25370 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25371 if (ultimate_origin != NULL_TREE
25372 && TREE_CODE (ultimate_origin) == PARM_DECL)
25373 gen_formal_parameter_die (decl, origin,
25374 true /* Emit name attribute. */,
25375 context_die);
25376 else
25377 gen_variable_die (decl, origin, context_die);
25378 break;
25380 case FIELD_DECL:
25381 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25382 /* Ignore the nameless fields that are used to skip bits but handle C++
25383 anonymous unions and structs. */
25384 if (DECL_NAME (decl) != NULL_TREE
25385 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25386 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25388 gen_type_die (member_declared_type (decl), context_die);
25389 gen_field_die (decl, ctx, context_die);
25391 break;
25393 case PARM_DECL:
25394 /* Avoid generating stray type DIEs during late dwarf dumping.
25395 All types have been dumped early. */
25396 if (early_dwarf
25397 /* ??? But in LTRANS we cannot annotate early created variably
25398 modified type DIEs without copying them and adjusting all
25399 references to them. Dump them again as happens for inlining
25400 which copies both the decl and the types. */
25401 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25402 in VLA bound information for example. */
25403 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25404 current_function_decl)))
25406 if (DECL_BY_REFERENCE (decl_or_origin))
25407 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25408 else
25409 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25411 return gen_formal_parameter_die (decl, origin,
25412 true /* Emit name attribute. */,
25413 context_die);
25415 case NAMESPACE_DECL:
25416 if (dwarf_version >= 3 || !dwarf_strict)
25417 gen_namespace_die (decl, context_die);
25418 break;
25420 case IMPORTED_DECL:
25421 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25422 DECL_CONTEXT (decl), context_die);
25423 break;
25425 case NAMELIST_DECL:
25426 gen_namelist_decl (DECL_NAME (decl), context_die,
25427 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25428 break;
25430 default:
25431 /* Probably some frontend-internal decl. Assume we don't care. */
25432 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25433 break;
25436 return NULL;
25439 /* Output initial debug information for global DECL. Called at the
25440 end of the parsing process.
25442 This is the initial debug generation process. As such, the DIEs
25443 generated may be incomplete. A later debug generation pass
25444 (dwarf2out_late_global_decl) will augment the information generated
25445 in this pass (e.g., with complete location info). */
25447 static void
25448 dwarf2out_early_global_decl (tree decl)
25450 set_early_dwarf s;
25452 /* gen_decl_die() will set DECL_ABSTRACT because
25453 cgraph_function_possibly_inlined_p() returns true. This is in
25454 turn will cause DW_AT_inline attributes to be set.
25456 This happens because at early dwarf generation, there is no
25457 cgraph information, causing cgraph_function_possibly_inlined_p()
25458 to return true. Trick cgraph_function_possibly_inlined_p()
25459 while we generate dwarf early. */
25460 bool save = symtab->global_info_ready;
25461 symtab->global_info_ready = true;
25463 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25464 other DECLs and they can point to template types or other things
25465 that dwarf2out can't handle when done via dwarf2out_decl. */
25466 if (TREE_CODE (decl) != TYPE_DECL
25467 && TREE_CODE (decl) != PARM_DECL)
25469 if (TREE_CODE (decl) == FUNCTION_DECL)
25471 tree save_fndecl = current_function_decl;
25473 /* For nested functions, make sure we have DIEs for the parents first
25474 so that all nested DIEs are generated at the proper scope in the
25475 first shot. */
25476 tree context = decl_function_context (decl);
25477 if (context != NULL && lookup_decl_die (context) == NULL)
25479 current_function_decl = context;
25480 dwarf2out_decl (context);
25483 /* Emit an abstract origin of a function first. This happens
25484 with C++ constructor clones for example and makes
25485 dwarf2out_abstract_function happy which requires the early
25486 DIE of the abstract instance to be present. */
25487 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25488 dw_die_ref origin_die;
25489 if (origin != NULL
25490 /* Do not emit the DIE multiple times but make sure to
25491 process it fully here in case we just saw a declaration. */
25492 && ((origin_die = lookup_decl_die (origin)) == NULL
25493 || is_declaration_die (origin_die)))
25495 current_function_decl = origin;
25496 dwarf2out_decl (origin);
25499 /* Emit the DIE for decl but avoid doing that multiple times. */
25500 dw_die_ref old_die;
25501 if ((old_die = lookup_decl_die (decl)) == NULL
25502 || is_declaration_die (old_die))
25504 current_function_decl = decl;
25505 dwarf2out_decl (decl);
25508 current_function_decl = save_fndecl;
25510 else
25511 dwarf2out_decl (decl);
25513 symtab->global_info_ready = save;
25516 /* Output debug information for global decl DECL. Called from
25517 toplev.c after compilation proper has finished. */
25519 static void
25520 dwarf2out_late_global_decl (tree decl)
25522 /* Fill-in any location information we were unable to determine
25523 on the first pass. */
25524 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25526 dw_die_ref die = lookup_decl_die (decl);
25528 /* We may have to generate early debug late for LTO in case debug
25529 was not enabled at compile-time or the target doesn't support
25530 the LTO early debug scheme. */
25531 if (! die && in_lto_p)
25533 dwarf2out_decl (decl);
25534 die = lookup_decl_die (decl);
25537 if (die)
25539 /* We get called via the symtab code invoking late_global_decl
25540 for symbols that are optimized out. Do not add locations
25541 for those, except if they have a DECL_VALUE_EXPR, in which case
25542 they are relevant for debuggers. */
25543 varpool_node *node = varpool_node::get (decl);
25544 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25545 tree_add_const_value_attribute_for_decl (die, decl);
25546 else
25547 add_location_or_const_value_attribute (die, decl, false);
25552 /* Output debug information for type decl DECL. Called from toplev.c
25553 and from language front ends (to record built-in types). */
25554 static void
25555 dwarf2out_type_decl (tree decl, int local)
25557 if (!local)
25559 set_early_dwarf s;
25560 dwarf2out_decl (decl);
25564 /* Output debug information for imported module or decl DECL.
25565 NAME is non-NULL name in the lexical block if the decl has been renamed.
25566 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25567 that DECL belongs to.
25568 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25569 static void
25570 dwarf2out_imported_module_or_decl_1 (tree decl,
25571 tree name,
25572 tree lexical_block,
25573 dw_die_ref lexical_block_die)
25575 expanded_location xloc;
25576 dw_die_ref imported_die = NULL;
25577 dw_die_ref at_import_die;
25579 if (TREE_CODE (decl) == IMPORTED_DECL)
25581 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25582 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25583 gcc_assert (decl);
25585 else
25586 xloc = expand_location (input_location);
25588 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25590 at_import_die = force_type_die (TREE_TYPE (decl));
25591 /* For namespace N { typedef void T; } using N::T; base_type_die
25592 returns NULL, but DW_TAG_imported_declaration requires
25593 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25594 if (!at_import_die)
25596 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25597 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25598 at_import_die = lookup_type_die (TREE_TYPE (decl));
25599 gcc_assert (at_import_die);
25602 else
25604 at_import_die = lookup_decl_die (decl);
25605 if (!at_import_die)
25607 /* If we're trying to avoid duplicate debug info, we may not have
25608 emitted the member decl for this field. Emit it now. */
25609 if (TREE_CODE (decl) == FIELD_DECL)
25611 tree type = DECL_CONTEXT (decl);
25613 if (TYPE_CONTEXT (type)
25614 && TYPE_P (TYPE_CONTEXT (type))
25615 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25616 DINFO_USAGE_DIR_USE))
25617 return;
25618 gen_type_die_for_member (type, decl,
25619 get_context_die (TYPE_CONTEXT (type)));
25621 if (TREE_CODE (decl) == NAMELIST_DECL)
25622 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25623 get_context_die (DECL_CONTEXT (decl)),
25624 NULL_TREE);
25625 else
25626 at_import_die = force_decl_die (decl);
25630 if (TREE_CODE (decl) == NAMESPACE_DECL)
25632 if (dwarf_version >= 3 || !dwarf_strict)
25633 imported_die = new_die (DW_TAG_imported_module,
25634 lexical_block_die,
25635 lexical_block);
25636 else
25637 return;
25639 else
25640 imported_die = new_die (DW_TAG_imported_declaration,
25641 lexical_block_die,
25642 lexical_block);
25644 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25645 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25646 if (debug_column_info && xloc.column)
25647 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25648 if (name)
25649 add_AT_string (imported_die, DW_AT_name,
25650 IDENTIFIER_POINTER (name));
25651 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25654 /* Output debug information for imported module or decl DECL.
25655 NAME is non-NULL name in context if the decl has been renamed.
25656 CHILD is true if decl is one of the renamed decls as part of
25657 importing whole module.
25658 IMPLICIT is set if this hook is called for an implicit import
25659 such as inline namespace. */
25661 static void
25662 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25663 bool child, bool implicit)
25665 /* dw_die_ref at_import_die; */
25666 dw_die_ref scope_die;
25668 if (debug_info_level <= DINFO_LEVEL_TERSE)
25669 return;
25671 gcc_assert (decl);
25673 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25674 should be enough, for DWARF4 and older even if we emit as extension
25675 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25676 for the benefit of consumers unaware of DW_AT_export_symbols. */
25677 if (implicit
25678 && dwarf_version >= 5
25679 && lang_hooks.decls.decl_dwarf_attribute (decl,
25680 DW_AT_export_symbols) == 1)
25681 return;
25683 set_early_dwarf s;
25685 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25686 We need decl DIE for reference and scope die. First, get DIE for the decl
25687 itself. */
25689 /* Get the scope die for decl context. Use comp_unit_die for global module
25690 or decl. If die is not found for non globals, force new die. */
25691 if (context
25692 && TYPE_P (context)
25693 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25694 return;
25696 scope_die = get_context_die (context);
25698 if (child)
25700 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25701 there is nothing we can do, here. */
25702 if (dwarf_version < 3 && dwarf_strict)
25703 return;
25705 gcc_assert (scope_die->die_child);
25706 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25707 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25708 scope_die = scope_die->die_child;
25711 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25712 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25715 /* Output debug information for namelists. */
25717 static dw_die_ref
25718 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25720 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25721 tree value;
25722 unsigned i;
25724 if (debug_info_level <= DINFO_LEVEL_TERSE)
25725 return NULL;
25727 gcc_assert (scope_die != NULL);
25728 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25729 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25731 /* If there are no item_decls, we have a nondefining namelist, e.g.
25732 with USE association; hence, set DW_AT_declaration. */
25733 if (item_decls == NULL_TREE)
25735 add_AT_flag (nml_die, DW_AT_declaration, 1);
25736 return nml_die;
25739 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25741 nml_item_ref_die = lookup_decl_die (value);
25742 if (!nml_item_ref_die)
25743 nml_item_ref_die = force_decl_die (value);
25745 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25746 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25748 return nml_die;
25752 /* Write the debugging output for DECL and return the DIE. */
25754 static void
25755 dwarf2out_decl (tree decl)
25757 dw_die_ref context_die = comp_unit_die ();
25759 switch (TREE_CODE (decl))
25761 case ERROR_MARK:
25762 return;
25764 case FUNCTION_DECL:
25765 /* If we're a nested function, initially use a parent of NULL; if we're
25766 a plain function, this will be fixed up in decls_for_scope. If
25767 we're a method, it will be ignored, since we already have a DIE. */
25768 if (decl_function_context (decl)
25769 /* But if we're in terse mode, we don't care about scope. */
25770 && debug_info_level > DINFO_LEVEL_TERSE)
25771 context_die = NULL;
25772 break;
25774 case VAR_DECL:
25775 /* For local statics lookup proper context die. */
25776 if (local_function_static (decl))
25777 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25779 /* If we are in terse mode, don't generate any DIEs to represent any
25780 variable declarations or definitions. */
25781 if (debug_info_level <= DINFO_LEVEL_TERSE)
25782 return;
25783 break;
25785 case CONST_DECL:
25786 if (debug_info_level <= DINFO_LEVEL_TERSE)
25787 return;
25788 if (!is_fortran () && !is_ada ())
25789 return;
25790 if (TREE_STATIC (decl) && decl_function_context (decl))
25791 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25792 break;
25794 case NAMESPACE_DECL:
25795 case IMPORTED_DECL:
25796 if (debug_info_level <= DINFO_LEVEL_TERSE)
25797 return;
25798 if (lookup_decl_die (decl) != NULL)
25799 return;
25800 break;
25802 case TYPE_DECL:
25803 /* Don't emit stubs for types unless they are needed by other DIEs. */
25804 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25805 return;
25807 /* Don't bother trying to generate any DIEs to represent any of the
25808 normal built-in types for the language we are compiling. */
25809 if (DECL_IS_BUILTIN (decl))
25810 return;
25812 /* If we are in terse mode, don't generate any DIEs for types. */
25813 if (debug_info_level <= DINFO_LEVEL_TERSE)
25814 return;
25816 /* If we're a function-scope tag, initially use a parent of NULL;
25817 this will be fixed up in decls_for_scope. */
25818 if (decl_function_context (decl))
25819 context_die = NULL;
25821 break;
25823 case NAMELIST_DECL:
25824 break;
25826 default:
25827 return;
25830 gen_decl_die (decl, NULL, NULL, context_die);
25832 if (flag_checking)
25834 dw_die_ref die = lookup_decl_die (decl);
25835 if (die)
25836 check_die (die);
25840 /* Write the debugging output for DECL. */
25842 static void
25843 dwarf2out_function_decl (tree decl)
25845 dwarf2out_decl (decl);
25846 call_arg_locations = NULL;
25847 call_arg_loc_last = NULL;
25848 call_site_count = -1;
25849 tail_call_site_count = -1;
25850 decl_loc_table->empty ();
25851 cached_dw_loc_list_table->empty ();
25854 /* Output a marker (i.e. a label) for the beginning of the generated code for
25855 a lexical block. */
25857 static void
25858 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25859 unsigned int blocknum)
25861 switch_to_section (current_function_section ());
25862 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25865 /* Output a marker (i.e. a label) for the end of the generated code for a
25866 lexical block. */
25868 static void
25869 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25871 switch_to_section (current_function_section ());
25872 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25875 /* Returns nonzero if it is appropriate not to emit any debugging
25876 information for BLOCK, because it doesn't contain any instructions.
25878 Don't allow this for blocks with nested functions or local classes
25879 as we would end up with orphans, and in the presence of scheduling
25880 we may end up calling them anyway. */
25882 static bool
25883 dwarf2out_ignore_block (const_tree block)
25885 tree decl;
25886 unsigned int i;
25888 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25889 if (TREE_CODE (decl) == FUNCTION_DECL
25890 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25891 return 0;
25892 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25894 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25895 if (TREE_CODE (decl) == FUNCTION_DECL
25896 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25897 return 0;
25900 return 1;
25903 /* Hash table routines for file_hash. */
25905 bool
25906 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25908 return filename_cmp (p1->filename, p2) == 0;
25911 hashval_t
25912 dwarf_file_hasher::hash (dwarf_file_data *p)
25914 return htab_hash_string (p->filename);
25917 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25918 dwarf2out.c) and return its "index". The index of each (known) filename is
25919 just a unique number which is associated with only that one filename. We
25920 need such numbers for the sake of generating labels (in the .debug_sfnames
25921 section) and references to those files numbers (in the .debug_srcinfo
25922 and .debug_macinfo sections). If the filename given as an argument is not
25923 found in our current list, add it to the list and assign it the next
25924 available unique index number. */
25926 static struct dwarf_file_data *
25927 lookup_filename (const char *file_name)
25929 struct dwarf_file_data * created;
25931 if (!file_name)
25932 return NULL;
25934 dwarf_file_data **slot
25935 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25936 INSERT);
25937 if (*slot)
25938 return *slot;
25940 created = ggc_alloc<dwarf_file_data> ();
25941 created->filename = file_name;
25942 created->emitted_number = 0;
25943 *slot = created;
25944 return created;
25947 /* If the assembler will construct the file table, then translate the compiler
25948 internal file table number into the assembler file table number, and emit
25949 a .file directive if we haven't already emitted one yet. The file table
25950 numbers are different because we prune debug info for unused variables and
25951 types, which may include filenames. */
25953 static int
25954 maybe_emit_file (struct dwarf_file_data * fd)
25956 if (! fd->emitted_number)
25958 if (last_emitted_file)
25959 fd->emitted_number = last_emitted_file->emitted_number + 1;
25960 else
25961 fd->emitted_number = 1;
25962 last_emitted_file = fd;
25964 if (DWARF2_ASM_LINE_DEBUG_INFO)
25966 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25967 output_quoted_string (asm_out_file,
25968 remap_debug_filename (fd->filename));
25969 fputc ('\n', asm_out_file);
25973 return fd->emitted_number;
25976 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25977 That generation should happen after function debug info has been
25978 generated. The value of the attribute is the constant value of ARG. */
25980 static void
25981 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25983 die_arg_entry entry;
25985 if (!die || !arg)
25986 return;
25988 gcc_assert (early_dwarf);
25990 if (!tmpl_value_parm_die_table)
25991 vec_alloc (tmpl_value_parm_die_table, 32);
25993 entry.die = die;
25994 entry.arg = arg;
25995 vec_safe_push (tmpl_value_parm_die_table, entry);
25998 /* Return TRUE if T is an instance of generic type, FALSE
25999 otherwise. */
26001 static bool
26002 generic_type_p (tree t)
26004 if (t == NULL_TREE || !TYPE_P (t))
26005 return false;
26006 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26009 /* Schedule the generation of the generic parameter dies for the
26010 instance of generic type T. The proper generation itself is later
26011 done by gen_scheduled_generic_parms_dies. */
26013 static void
26014 schedule_generic_params_dies_gen (tree t)
26016 if (!generic_type_p (t))
26017 return;
26019 gcc_assert (early_dwarf);
26021 if (!generic_type_instances)
26022 vec_alloc (generic_type_instances, 256);
26024 vec_safe_push (generic_type_instances, t);
26027 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26028 by append_entry_to_tmpl_value_parm_die_table. This function must
26029 be called after function DIEs have been generated. */
26031 static void
26032 gen_remaining_tmpl_value_param_die_attribute (void)
26034 if (tmpl_value_parm_die_table)
26036 unsigned i, j;
26037 die_arg_entry *e;
26039 /* We do this in two phases - first get the cases we can
26040 handle during early-finish, preserving those we cannot
26041 (containing symbolic constants where we don't yet know
26042 whether we are going to output the referenced symbols).
26043 For those we try again at late-finish. */
26044 j = 0;
26045 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26047 if (!e->die->removed
26048 && !tree_add_const_value_attribute (e->die, e->arg))
26050 dw_loc_descr_ref loc = NULL;
26051 if (! early_dwarf
26052 && (dwarf_version >= 5 || !dwarf_strict))
26053 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26054 if (loc)
26055 add_AT_loc (e->die, DW_AT_location, loc);
26056 else
26057 (*tmpl_value_parm_die_table)[j++] = *e;
26060 tmpl_value_parm_die_table->truncate (j);
26064 /* Generate generic parameters DIEs for instances of generic types
26065 that have been previously scheduled by
26066 schedule_generic_params_dies_gen. This function must be called
26067 after all the types of the CU have been laid out. */
26069 static void
26070 gen_scheduled_generic_parms_dies (void)
26072 unsigned i;
26073 tree t;
26075 if (!generic_type_instances)
26076 return;
26078 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26079 if (COMPLETE_TYPE_P (t))
26080 gen_generic_params_dies (t);
26082 generic_type_instances = NULL;
26086 /* Replace DW_AT_name for the decl with name. */
26088 static void
26089 dwarf2out_set_name (tree decl, tree name)
26091 dw_die_ref die;
26092 dw_attr_node *attr;
26093 const char *dname;
26095 die = TYPE_SYMTAB_DIE (decl);
26096 if (!die)
26097 return;
26099 dname = dwarf2_name (name, 0);
26100 if (!dname)
26101 return;
26103 attr = get_AT (die, DW_AT_name);
26104 if (attr)
26106 struct indirect_string_node *node;
26108 node = find_AT_string (dname);
26109 /* replace the string. */
26110 attr->dw_attr_val.v.val_str = node;
26113 else
26114 add_name_attribute (die, dname);
26117 /* True if before or during processing of the first function being emitted. */
26118 static bool in_first_function_p = true;
26119 /* True if loc_note during dwarf2out_var_location call might still be
26120 before first real instruction at address equal to .Ltext0. */
26121 static bool maybe_at_text_label_p = true;
26122 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26123 static unsigned int first_loclabel_num_not_at_text_label;
26125 /* Called by the final INSN scan whenever we see a var location. We
26126 use it to drop labels in the right places, and throw the location in
26127 our lookup table. */
26129 static void
26130 dwarf2out_var_location (rtx_insn *loc_note)
26132 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26133 struct var_loc_node *newloc;
26134 rtx_insn *next_real, *next_note;
26135 rtx_insn *call_insn = NULL;
26136 static const char *last_label;
26137 static const char *last_postcall_label;
26138 static bool last_in_cold_section_p;
26139 static rtx_insn *expected_next_loc_note;
26140 tree decl;
26141 bool var_loc_p;
26143 if (!NOTE_P (loc_note))
26145 if (CALL_P (loc_note))
26147 call_site_count++;
26148 if (SIBLING_CALL_P (loc_note))
26149 tail_call_site_count++;
26150 if (optimize == 0 && !flag_var_tracking)
26152 /* When the var-tracking pass is not running, there is no note
26153 for indirect calls whose target is compile-time known. In this
26154 case, process such calls specifically so that we generate call
26155 sites for them anyway. */
26156 rtx x = PATTERN (loc_note);
26157 if (GET_CODE (x) == PARALLEL)
26158 x = XVECEXP (x, 0, 0);
26159 if (GET_CODE (x) == SET)
26160 x = SET_SRC (x);
26161 if (GET_CODE (x) == CALL)
26162 x = XEXP (x, 0);
26163 if (!MEM_P (x)
26164 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26165 || !SYMBOL_REF_DECL (XEXP (x, 0))
26166 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26167 != FUNCTION_DECL))
26169 call_insn = loc_note;
26170 loc_note = NULL;
26171 var_loc_p = false;
26173 next_real = next_real_insn (call_insn);
26174 next_note = NULL;
26175 cached_next_real_insn = NULL;
26176 goto create_label;
26180 return;
26183 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26184 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26185 return;
26187 /* Optimize processing a large consecutive sequence of location
26188 notes so we don't spend too much time in next_real_insn. If the
26189 next insn is another location note, remember the next_real_insn
26190 calculation for next time. */
26191 next_real = cached_next_real_insn;
26192 if (next_real)
26194 if (expected_next_loc_note != loc_note)
26195 next_real = NULL;
26198 next_note = NEXT_INSN (loc_note);
26199 if (! next_note
26200 || next_note->deleted ()
26201 || ! NOTE_P (next_note)
26202 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26203 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26204 next_note = NULL;
26206 if (! next_real)
26207 next_real = next_real_insn (loc_note);
26209 if (next_note)
26211 expected_next_loc_note = next_note;
26212 cached_next_real_insn = next_real;
26214 else
26215 cached_next_real_insn = NULL;
26217 /* If there are no instructions which would be affected by this note,
26218 don't do anything. */
26219 if (var_loc_p
26220 && next_real == NULL_RTX
26221 && !NOTE_DURING_CALL_P (loc_note))
26222 return;
26224 create_label:
26226 if (next_real == NULL_RTX)
26227 next_real = get_last_insn ();
26229 /* If there were any real insns between note we processed last time
26230 and this note (or if it is the first note), clear
26231 last_{,postcall_}label so that they are not reused this time. */
26232 if (last_var_location_insn == NULL_RTX
26233 || last_var_location_insn != next_real
26234 || last_in_cold_section_p != in_cold_section_p)
26236 last_label = NULL;
26237 last_postcall_label = NULL;
26240 if (var_loc_p)
26242 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26243 newloc = add_var_loc_to_decl (decl, loc_note,
26244 NOTE_DURING_CALL_P (loc_note)
26245 ? last_postcall_label : last_label);
26246 if (newloc == NULL)
26247 return;
26249 else
26251 decl = NULL_TREE;
26252 newloc = NULL;
26255 /* If there were no real insns between note we processed last time
26256 and this note, use the label we emitted last time. Otherwise
26257 create a new label and emit it. */
26258 if (last_label == NULL)
26260 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26261 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26262 loclabel_num++;
26263 last_label = ggc_strdup (loclabel);
26264 /* See if loclabel might be equal to .Ltext0. If yes,
26265 bump first_loclabel_num_not_at_text_label. */
26266 if (!have_multiple_function_sections
26267 && in_first_function_p
26268 && maybe_at_text_label_p)
26270 static rtx_insn *last_start;
26271 rtx_insn *insn;
26272 for (insn = loc_note; insn; insn = previous_insn (insn))
26273 if (insn == last_start)
26274 break;
26275 else if (!NONDEBUG_INSN_P (insn))
26276 continue;
26277 else
26279 rtx body = PATTERN (insn);
26280 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26281 continue;
26282 /* Inline asm could occupy zero bytes. */
26283 else if (GET_CODE (body) == ASM_INPUT
26284 || asm_noperands (body) >= 0)
26285 continue;
26286 #ifdef HAVE_attr_length
26287 else if (get_attr_min_length (insn) == 0)
26288 continue;
26289 #endif
26290 else
26292 /* Assume insn has non-zero length. */
26293 maybe_at_text_label_p = false;
26294 break;
26297 if (maybe_at_text_label_p)
26299 last_start = loc_note;
26300 first_loclabel_num_not_at_text_label = loclabel_num;
26305 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26306 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26308 if (!var_loc_p)
26310 struct call_arg_loc_node *ca_loc
26311 = ggc_cleared_alloc<call_arg_loc_node> ();
26312 rtx_insn *prev
26313 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26315 ca_loc->call_arg_loc_note = loc_note;
26316 ca_loc->next = NULL;
26317 ca_loc->label = last_label;
26318 gcc_assert (prev
26319 && (CALL_P (prev)
26320 || (NONJUMP_INSN_P (prev)
26321 && GET_CODE (PATTERN (prev)) == SEQUENCE
26322 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26323 if (!CALL_P (prev))
26324 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26325 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26327 /* Look for a SYMBOL_REF in the "prev" instruction. */
26328 rtx x = get_call_rtx_from (PATTERN (prev));
26329 if (x)
26331 /* Try to get the call symbol, if any. */
26332 if (MEM_P (XEXP (x, 0)))
26333 x = XEXP (x, 0);
26334 /* First, look for a memory access to a symbol_ref. */
26335 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26336 && SYMBOL_REF_DECL (XEXP (x, 0))
26337 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26338 ca_loc->symbol_ref = XEXP (x, 0);
26339 /* Otherwise, look at a compile-time known user-level function
26340 declaration. */
26341 else if (MEM_P (x)
26342 && MEM_EXPR (x)
26343 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26344 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26347 ca_loc->block = insn_scope (prev);
26348 if (call_arg_locations)
26349 call_arg_loc_last->next = ca_loc;
26350 else
26351 call_arg_locations = ca_loc;
26352 call_arg_loc_last = ca_loc;
26354 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26355 newloc->label = last_label;
26356 else
26358 if (!last_postcall_label)
26360 sprintf (loclabel, "%s-1", last_label);
26361 last_postcall_label = ggc_strdup (loclabel);
26363 newloc->label = last_postcall_label;
26366 last_var_location_insn = next_real;
26367 last_in_cold_section_p = in_cold_section_p;
26370 /* Called from finalize_size_functions for size functions so that their body
26371 can be encoded in the debug info to describe the layout of variable-length
26372 structures. */
26374 static void
26375 dwarf2out_size_function (tree decl)
26377 function_to_dwarf_procedure (decl);
26380 /* Note in one location list that text section has changed. */
26383 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26385 var_loc_list *list = *slot;
26386 if (list->first)
26387 list->last_before_switch
26388 = list->last->next ? list->last->next : list->last;
26389 return 1;
26392 /* Note in all location lists that text section has changed. */
26394 static void
26395 var_location_switch_text_section (void)
26397 if (decl_loc_table == NULL)
26398 return;
26400 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26403 /* Create a new line number table. */
26405 static dw_line_info_table *
26406 new_line_info_table (void)
26408 dw_line_info_table *table;
26410 table = ggc_cleared_alloc<dw_line_info_table> ();
26411 table->file_num = 1;
26412 table->line_num = 1;
26413 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26415 return table;
26418 /* Lookup the "current" table into which we emit line info, so
26419 that we don't have to do it for every source line. */
26421 static void
26422 set_cur_line_info_table (section *sec)
26424 dw_line_info_table *table;
26426 if (sec == text_section)
26427 table = text_section_line_info;
26428 else if (sec == cold_text_section)
26430 table = cold_text_section_line_info;
26431 if (!table)
26433 cold_text_section_line_info = table = new_line_info_table ();
26434 table->end_label = cold_end_label;
26437 else
26439 const char *end_label;
26441 if (crtl->has_bb_partition)
26443 if (in_cold_section_p)
26444 end_label = crtl->subsections.cold_section_end_label;
26445 else
26446 end_label = crtl->subsections.hot_section_end_label;
26448 else
26450 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26451 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26452 current_function_funcdef_no);
26453 end_label = ggc_strdup (label);
26456 table = new_line_info_table ();
26457 table->end_label = end_label;
26459 vec_safe_push (separate_line_info, table);
26462 if (DWARF2_ASM_LINE_DEBUG_INFO)
26463 table->is_stmt = (cur_line_info_table
26464 ? cur_line_info_table->is_stmt
26465 : DWARF_LINE_DEFAULT_IS_STMT_START);
26466 cur_line_info_table = table;
26470 /* We need to reset the locations at the beginning of each
26471 function. We can't do this in the end_function hook, because the
26472 declarations that use the locations won't have been output when
26473 that hook is called. Also compute have_multiple_function_sections here. */
26475 static void
26476 dwarf2out_begin_function (tree fun)
26478 section *sec = function_section (fun);
26480 if (sec != text_section)
26481 have_multiple_function_sections = true;
26483 if (crtl->has_bb_partition && !cold_text_section)
26485 gcc_assert (current_function_decl == fun);
26486 cold_text_section = unlikely_text_section ();
26487 switch_to_section (cold_text_section);
26488 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26489 switch_to_section (sec);
26492 dwarf2out_note_section_used ();
26493 call_site_count = 0;
26494 tail_call_site_count = 0;
26496 set_cur_line_info_table (sec);
26499 /* Helper function of dwarf2out_end_function, called only after emitting
26500 the very first function into assembly. Check if some .debug_loc range
26501 might end with a .LVL* label that could be equal to .Ltext0.
26502 In that case we must force using absolute addresses in .debug_loc ranges,
26503 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26504 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26505 list terminator.
26506 Set have_multiple_function_sections to true in that case and
26507 terminate htab traversal. */
26510 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26512 var_loc_list *entry = *slot;
26513 struct var_loc_node *node;
26515 node = entry->first;
26516 if (node && node->next && node->next->label)
26518 unsigned int i;
26519 const char *label = node->next->label;
26520 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26522 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26524 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26525 if (strcmp (label, loclabel) == 0)
26527 have_multiple_function_sections = true;
26528 return 0;
26532 return 1;
26535 /* Hook called after emitting a function into assembly.
26536 This does something only for the very first function emitted. */
26538 static void
26539 dwarf2out_end_function (unsigned int)
26541 if (in_first_function_p
26542 && !have_multiple_function_sections
26543 && first_loclabel_num_not_at_text_label
26544 && decl_loc_table)
26545 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26546 in_first_function_p = false;
26547 maybe_at_text_label_p = false;
26550 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26551 front-ends register a translation unit even before dwarf2out_init is
26552 called. */
26553 static tree main_translation_unit = NULL_TREE;
26555 /* Hook called by front-ends after they built their main translation unit.
26556 Associate comp_unit_die to UNIT. */
26558 static void
26559 dwarf2out_register_main_translation_unit (tree unit)
26561 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26562 && main_translation_unit == NULL_TREE);
26563 main_translation_unit = unit;
26564 /* If dwarf2out_init has not been called yet, it will perform the association
26565 itself looking at main_translation_unit. */
26566 if (decl_die_table != NULL)
26567 equate_decl_number_to_die (unit, comp_unit_die ());
26570 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26572 static void
26573 push_dw_line_info_entry (dw_line_info_table *table,
26574 enum dw_line_info_opcode opcode, unsigned int val)
26576 dw_line_info_entry e;
26577 e.opcode = opcode;
26578 e.val = val;
26579 vec_safe_push (table->entries, e);
26582 /* Output a label to mark the beginning of a source code line entry
26583 and record information relating to this source line, in
26584 'line_info_table' for later output of the .debug_line section. */
26585 /* ??? The discriminator parameter ought to be unsigned. */
26587 static void
26588 dwarf2out_source_line (unsigned int line, unsigned int column,
26589 const char *filename,
26590 int discriminator, bool is_stmt)
26592 unsigned int file_num;
26593 dw_line_info_table *table;
26595 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26596 return;
26598 /* The discriminator column was added in dwarf4. Simplify the below
26599 by simply removing it if we're not supposed to output it. */
26600 if (dwarf_version < 4 && dwarf_strict)
26601 discriminator = 0;
26603 if (!debug_column_info)
26604 column = 0;
26606 table = cur_line_info_table;
26607 file_num = maybe_emit_file (lookup_filename (filename));
26609 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26610 the debugger has used the second (possibly duplicate) line number
26611 at the beginning of the function to mark the end of the prologue.
26612 We could eliminate any other duplicates within the function. For
26613 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26614 that second line number entry. */
26615 /* Recall that this end-of-prologue indication is *not* the same thing
26616 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26617 to which the hook corresponds, follows the last insn that was
26618 emitted by gen_prologue. What we need is to precede the first insn
26619 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26620 insn that corresponds to something the user wrote. These may be
26621 very different locations once scheduling is enabled. */
26623 if (0 && file_num == table->file_num
26624 && line == table->line_num
26625 && column == table->column_num
26626 && discriminator == table->discrim_num
26627 && is_stmt == table->is_stmt)
26628 return;
26630 switch_to_section (current_function_section ());
26632 /* If requested, emit something human-readable. */
26633 if (flag_debug_asm)
26635 if (debug_column_info)
26636 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26637 filename, line, column);
26638 else
26639 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26640 filename, line);
26643 if (DWARF2_ASM_LINE_DEBUG_INFO)
26645 /* Emit the .loc directive understood by GNU as. */
26646 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26647 file_num, line, is_stmt, discriminator */
26648 fputs ("\t.loc ", asm_out_file);
26649 fprint_ul (asm_out_file, file_num);
26650 putc (' ', asm_out_file);
26651 fprint_ul (asm_out_file, line);
26652 putc (' ', asm_out_file);
26653 fprint_ul (asm_out_file, column);
26655 if (is_stmt != table->is_stmt)
26657 fputs (" is_stmt ", asm_out_file);
26658 putc (is_stmt ? '1' : '0', asm_out_file);
26660 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26662 gcc_assert (discriminator > 0);
26663 fputs (" discriminator ", asm_out_file);
26664 fprint_ul (asm_out_file, (unsigned long) discriminator);
26666 putc ('\n', asm_out_file);
26668 else
26670 unsigned int label_num = ++line_info_label_num;
26672 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26674 push_dw_line_info_entry (table, LI_set_address, label_num);
26675 if (file_num != table->file_num)
26676 push_dw_line_info_entry (table, LI_set_file, file_num);
26677 if (discriminator != table->discrim_num)
26678 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26679 if (is_stmt != table->is_stmt)
26680 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26681 push_dw_line_info_entry (table, LI_set_line, line);
26682 if (debug_column_info)
26683 push_dw_line_info_entry (table, LI_set_column, column);
26686 table->file_num = file_num;
26687 table->line_num = line;
26688 table->column_num = column;
26689 table->discrim_num = discriminator;
26690 table->is_stmt = is_stmt;
26691 table->in_use = true;
26694 /* Record the beginning of a new source file. */
26696 static void
26697 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26699 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26701 macinfo_entry e;
26702 e.code = DW_MACINFO_start_file;
26703 e.lineno = lineno;
26704 e.info = ggc_strdup (filename);
26705 vec_safe_push (macinfo_table, e);
26709 /* Record the end of a source file. */
26711 static void
26712 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26714 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26716 macinfo_entry e;
26717 e.code = DW_MACINFO_end_file;
26718 e.lineno = lineno;
26719 e.info = NULL;
26720 vec_safe_push (macinfo_table, e);
26724 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26725 the tail part of the directive line, i.e. the part which is past the
26726 initial whitespace, #, whitespace, directive-name, whitespace part. */
26728 static void
26729 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26730 const char *buffer ATTRIBUTE_UNUSED)
26732 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26734 macinfo_entry e;
26735 /* Insert a dummy first entry to be able to optimize the whole
26736 predefined macro block using DW_MACRO_import. */
26737 if (macinfo_table->is_empty () && lineno <= 1)
26739 e.code = 0;
26740 e.lineno = 0;
26741 e.info = NULL;
26742 vec_safe_push (macinfo_table, e);
26744 e.code = DW_MACINFO_define;
26745 e.lineno = lineno;
26746 e.info = ggc_strdup (buffer);
26747 vec_safe_push (macinfo_table, e);
26751 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26752 the tail part of the directive line, i.e. the part which is past the
26753 initial whitespace, #, whitespace, directive-name, whitespace part. */
26755 static void
26756 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26757 const char *buffer ATTRIBUTE_UNUSED)
26759 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26761 macinfo_entry e;
26762 /* Insert a dummy first entry to be able to optimize the whole
26763 predefined macro block using DW_MACRO_import. */
26764 if (macinfo_table->is_empty () && lineno <= 1)
26766 e.code = 0;
26767 e.lineno = 0;
26768 e.info = NULL;
26769 vec_safe_push (macinfo_table, e);
26771 e.code = DW_MACINFO_undef;
26772 e.lineno = lineno;
26773 e.info = ggc_strdup (buffer);
26774 vec_safe_push (macinfo_table, e);
26778 /* Helpers to manipulate hash table of CUs. */
26780 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26782 static inline hashval_t hash (const macinfo_entry *);
26783 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26786 inline hashval_t
26787 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26789 return htab_hash_string (entry->info);
26792 inline bool
26793 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26794 const macinfo_entry *entry2)
26796 return !strcmp (entry1->info, entry2->info);
26799 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26801 /* Output a single .debug_macinfo entry. */
26803 static void
26804 output_macinfo_op (macinfo_entry *ref)
26806 int file_num;
26807 size_t len;
26808 struct indirect_string_node *node;
26809 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26810 struct dwarf_file_data *fd;
26812 switch (ref->code)
26814 case DW_MACINFO_start_file:
26815 fd = lookup_filename (ref->info);
26816 file_num = maybe_emit_file (fd);
26817 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26818 dw2_asm_output_data_uleb128 (ref->lineno,
26819 "Included from line number %lu",
26820 (unsigned long) ref->lineno);
26821 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26822 break;
26823 case DW_MACINFO_end_file:
26824 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26825 break;
26826 case DW_MACINFO_define:
26827 case DW_MACINFO_undef:
26828 len = strlen (ref->info) + 1;
26829 if (!dwarf_strict
26830 && len > DWARF_OFFSET_SIZE
26831 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26832 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26834 ref->code = ref->code == DW_MACINFO_define
26835 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26836 output_macinfo_op (ref);
26837 return;
26839 dw2_asm_output_data (1, ref->code,
26840 ref->code == DW_MACINFO_define
26841 ? "Define macro" : "Undefine macro");
26842 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26843 (unsigned long) ref->lineno);
26844 dw2_asm_output_nstring (ref->info, -1, "The macro");
26845 break;
26846 case DW_MACRO_define_strp:
26847 case DW_MACRO_undef_strp:
26848 node = find_AT_string (ref->info);
26849 gcc_assert (node
26850 && (node->form == DW_FORM_strp
26851 || node->form == DW_FORM_GNU_str_index));
26852 dw2_asm_output_data (1, ref->code,
26853 ref->code == DW_MACRO_define_strp
26854 ? "Define macro strp"
26855 : "Undefine macro strp");
26856 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26857 (unsigned long) ref->lineno);
26858 if (node->form == DW_FORM_strp)
26859 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26860 debug_str_section, "The macro: \"%s\"",
26861 ref->info);
26862 else
26863 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26864 ref->info);
26865 break;
26866 case DW_MACRO_import:
26867 dw2_asm_output_data (1, ref->code, "Import");
26868 ASM_GENERATE_INTERNAL_LABEL (label,
26869 DEBUG_MACRO_SECTION_LABEL,
26870 ref->lineno + macinfo_label_base);
26871 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26872 break;
26873 default:
26874 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26875 ASM_COMMENT_START, (unsigned long) ref->code);
26876 break;
26880 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26881 other compilation unit .debug_macinfo sections. IDX is the first
26882 index of a define/undef, return the number of ops that should be
26883 emitted in a comdat .debug_macinfo section and emit
26884 a DW_MACRO_import entry referencing it.
26885 If the define/undef entry should be emitted normally, return 0. */
26887 static unsigned
26888 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26889 macinfo_hash_type **macinfo_htab)
26891 macinfo_entry *first, *second, *cur, *inc;
26892 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26893 unsigned char checksum[16];
26894 struct md5_ctx ctx;
26895 char *grp_name, *tail;
26896 const char *base;
26897 unsigned int i, count, encoded_filename_len, linebuf_len;
26898 macinfo_entry **slot;
26900 first = &(*macinfo_table)[idx];
26901 second = &(*macinfo_table)[idx + 1];
26903 /* Optimize only if there are at least two consecutive define/undef ops,
26904 and either all of them are before first DW_MACINFO_start_file
26905 with lineno {0,1} (i.e. predefined macro block), or all of them are
26906 in some included header file. */
26907 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26908 return 0;
26909 if (vec_safe_is_empty (files))
26911 if (first->lineno > 1 || second->lineno > 1)
26912 return 0;
26914 else if (first->lineno == 0)
26915 return 0;
26917 /* Find the last define/undef entry that can be grouped together
26918 with first and at the same time compute md5 checksum of their
26919 codes, linenumbers and strings. */
26920 md5_init_ctx (&ctx);
26921 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26922 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26923 break;
26924 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26925 break;
26926 else
26928 unsigned char code = cur->code;
26929 md5_process_bytes (&code, 1, &ctx);
26930 checksum_uleb128 (cur->lineno, &ctx);
26931 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26933 md5_finish_ctx (&ctx, checksum);
26934 count = i - idx;
26936 /* From the containing include filename (if any) pick up just
26937 usable characters from its basename. */
26938 if (vec_safe_is_empty (files))
26939 base = "";
26940 else
26941 base = lbasename (files->last ().info);
26942 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26943 if (ISIDNUM (base[i]) || base[i] == '.')
26944 encoded_filename_len++;
26945 /* Count . at the end. */
26946 if (encoded_filename_len)
26947 encoded_filename_len++;
26949 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26950 linebuf_len = strlen (linebuf);
26952 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26953 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26954 + 16 * 2 + 1);
26955 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26956 tail = grp_name + 4;
26957 if (encoded_filename_len)
26959 for (i = 0; base[i]; i++)
26960 if (ISIDNUM (base[i]) || base[i] == '.')
26961 *tail++ = base[i];
26962 *tail++ = '.';
26964 memcpy (tail, linebuf, linebuf_len);
26965 tail += linebuf_len;
26966 *tail++ = '.';
26967 for (i = 0; i < 16; i++)
26968 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26970 /* Construct a macinfo_entry for DW_MACRO_import
26971 in the empty vector entry before the first define/undef. */
26972 inc = &(*macinfo_table)[idx - 1];
26973 inc->code = DW_MACRO_import;
26974 inc->lineno = 0;
26975 inc->info = ggc_strdup (grp_name);
26976 if (!*macinfo_htab)
26977 *macinfo_htab = new macinfo_hash_type (10);
26978 /* Avoid emitting duplicates. */
26979 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26980 if (*slot != NULL)
26982 inc->code = 0;
26983 inc->info = NULL;
26984 /* If such an entry has been used before, just emit
26985 a DW_MACRO_import op. */
26986 inc = *slot;
26987 output_macinfo_op (inc);
26988 /* And clear all macinfo_entry in the range to avoid emitting them
26989 in the second pass. */
26990 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26992 cur->code = 0;
26993 cur->info = NULL;
26996 else
26998 *slot = inc;
26999 inc->lineno = (*macinfo_htab)->elements ();
27000 output_macinfo_op (inc);
27002 return count;
27005 /* Save any strings needed by the macinfo table in the debug str
27006 table. All strings must be collected into the table by the time
27007 index_string is called. */
27009 static void
27010 save_macinfo_strings (void)
27012 unsigned len;
27013 unsigned i;
27014 macinfo_entry *ref;
27016 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27018 switch (ref->code)
27020 /* Match the logic in output_macinfo_op to decide on
27021 indirect strings. */
27022 case DW_MACINFO_define:
27023 case DW_MACINFO_undef:
27024 len = strlen (ref->info) + 1;
27025 if (!dwarf_strict
27026 && len > DWARF_OFFSET_SIZE
27027 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27028 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27029 set_indirect_string (find_AT_string (ref->info));
27030 break;
27031 case DW_MACRO_define_strp:
27032 case DW_MACRO_undef_strp:
27033 set_indirect_string (find_AT_string (ref->info));
27034 break;
27035 default:
27036 break;
27041 /* Output macinfo section(s). */
27043 static void
27044 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27046 unsigned i;
27047 unsigned long length = vec_safe_length (macinfo_table);
27048 macinfo_entry *ref;
27049 vec<macinfo_entry, va_gc> *files = NULL;
27050 macinfo_hash_type *macinfo_htab = NULL;
27051 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27053 if (! length)
27054 return;
27056 /* output_macinfo* uses these interchangeably. */
27057 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27058 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27059 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27060 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27062 /* AIX Assembler inserts the length, so adjust the reference to match the
27063 offset expected by debuggers. */
27064 strcpy (dl_section_ref, debug_line_label);
27065 if (XCOFF_DEBUGGING_INFO)
27066 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27068 /* For .debug_macro emit the section header. */
27069 if (!dwarf_strict || dwarf_version >= 5)
27071 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27072 "DWARF macro version number");
27073 if (DWARF_OFFSET_SIZE == 8)
27074 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27075 else
27076 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27077 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27078 debug_line_section, NULL);
27081 /* In the first loop, it emits the primary .debug_macinfo section
27082 and after each emitted op the macinfo_entry is cleared.
27083 If a longer range of define/undef ops can be optimized using
27084 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27085 the vector before the first define/undef in the range and the
27086 whole range of define/undef ops is not emitted and kept. */
27087 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27089 switch (ref->code)
27091 case DW_MACINFO_start_file:
27092 vec_safe_push (files, *ref);
27093 break;
27094 case DW_MACINFO_end_file:
27095 if (!vec_safe_is_empty (files))
27096 files->pop ();
27097 break;
27098 case DW_MACINFO_define:
27099 case DW_MACINFO_undef:
27100 if ((!dwarf_strict || dwarf_version >= 5)
27101 && HAVE_COMDAT_GROUP
27102 && vec_safe_length (files) != 1
27103 && i > 0
27104 && i + 1 < length
27105 && (*macinfo_table)[i - 1].code == 0)
27107 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27108 if (count)
27110 i += count - 1;
27111 continue;
27114 break;
27115 case 0:
27116 /* A dummy entry may be inserted at the beginning to be able
27117 to optimize the whole block of predefined macros. */
27118 if (i == 0)
27119 continue;
27120 default:
27121 break;
27123 output_macinfo_op (ref);
27124 ref->info = NULL;
27125 ref->code = 0;
27128 if (!macinfo_htab)
27129 return;
27131 /* Save the number of transparent includes so we can adjust the
27132 label number for the fat LTO object DWARF. */
27133 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27135 delete macinfo_htab;
27136 macinfo_htab = NULL;
27138 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27139 terminate the current chain and switch to a new comdat .debug_macinfo
27140 section and emit the define/undef entries within it. */
27141 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27142 switch (ref->code)
27144 case 0:
27145 continue;
27146 case DW_MACRO_import:
27148 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27149 tree comdat_key = get_identifier (ref->info);
27150 /* Terminate the previous .debug_macinfo section. */
27151 dw2_asm_output_data (1, 0, "End compilation unit");
27152 targetm.asm_out.named_section (debug_macinfo_section_name,
27153 SECTION_DEBUG
27154 | SECTION_LINKONCE
27155 | (early_lto_debug
27156 ? SECTION_EXCLUDE : 0),
27157 comdat_key);
27158 ASM_GENERATE_INTERNAL_LABEL (label,
27159 DEBUG_MACRO_SECTION_LABEL,
27160 ref->lineno + macinfo_label_base);
27161 ASM_OUTPUT_LABEL (asm_out_file, label);
27162 ref->code = 0;
27163 ref->info = NULL;
27164 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27165 "DWARF macro version number");
27166 if (DWARF_OFFSET_SIZE == 8)
27167 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27168 else
27169 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27171 break;
27172 case DW_MACINFO_define:
27173 case DW_MACINFO_undef:
27174 output_macinfo_op (ref);
27175 ref->code = 0;
27176 ref->info = NULL;
27177 break;
27178 default:
27179 gcc_unreachable ();
27182 macinfo_label_base += macinfo_label_base_adj;
27185 /* Initialize the various sections and labels for dwarf output and prefix
27186 them with PREFIX if non-NULL. */
27188 static void
27189 init_sections_and_labels (bool early_lto_debug)
27191 /* As we may get called multiple times have a generation count for labels. */
27192 static unsigned generation = 0;
27194 if (early_lto_debug)
27196 if (!dwarf_split_debug_info)
27198 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27199 SECTION_DEBUG | SECTION_EXCLUDE,
27200 NULL);
27201 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27202 SECTION_DEBUG | SECTION_EXCLUDE,
27203 NULL);
27204 debug_macinfo_section_name = ((dwarf_strict && dwarf_version < 5)
27205 ? DEBUG_LTO_MACINFO_SECTION
27206 : DEBUG_LTO_MACRO_SECTION);
27207 debug_macinfo_section = get_section (debug_macinfo_section_name,
27208 SECTION_DEBUG
27209 | SECTION_EXCLUDE, NULL);
27210 /* For macro info we have to refer to a debug_line section, so similar
27211 to split-dwarf emit a skeleton one for early debug. */
27212 debug_skeleton_line_section
27213 = get_section (DEBUG_LTO_LINE_SECTION,
27214 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27215 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27216 DEBUG_SKELETON_LINE_SECTION_LABEL,
27217 generation);
27219 else
27221 /* ??? Which of the following do we need early? */
27222 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27223 SECTION_DEBUG | SECTION_EXCLUDE,
27224 NULL);
27225 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27226 SECTION_DEBUG | SECTION_EXCLUDE,
27227 NULL);
27228 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27229 SECTION_DEBUG
27230 | SECTION_EXCLUDE, NULL);
27231 debug_skeleton_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27232 SECTION_DEBUG
27233 | SECTION_EXCLUDE, NULL);
27234 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27235 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27236 generation);
27238 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27239 the main .o, but the skeleton_line goes into the split off dwo. */
27240 debug_skeleton_line_section
27241 = get_section (DEBUG_LTO_LINE_SECTION,
27242 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27243 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27244 DEBUG_SKELETON_LINE_SECTION_LABEL,
27245 generation);
27246 debug_str_offsets_section
27247 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27248 SECTION_DEBUG | SECTION_EXCLUDE,
27249 NULL);
27250 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27251 DEBUG_SKELETON_INFO_SECTION_LABEL,
27252 generation);
27253 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27254 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27255 debug_macinfo_section_name
27256 = (dwarf_strict
27257 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27258 debug_macinfo_section = get_section (debug_macinfo_section_name,
27259 SECTION_DEBUG | SECTION_EXCLUDE,
27260 NULL);
27262 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27263 DEBUG_STR_SECTION_FLAGS
27264 | SECTION_EXCLUDE, NULL);
27266 else
27268 if (!dwarf_split_debug_info)
27270 debug_info_section = get_section (DEBUG_INFO_SECTION,
27271 SECTION_DEBUG, NULL);
27272 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27273 SECTION_DEBUG, NULL);
27274 debug_loc_section = get_section (DEBUG_LOC_SECTION,
27275 SECTION_DEBUG, NULL);
27276 debug_macinfo_section_name
27277 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27278 debug_macinfo_section = get_section (debug_macinfo_section_name,
27279 SECTION_DEBUG, NULL);
27281 else
27283 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27284 SECTION_DEBUG | SECTION_EXCLUDE,
27285 NULL);
27286 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27287 SECTION_DEBUG | SECTION_EXCLUDE,
27288 NULL);
27289 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27290 SECTION_DEBUG, NULL);
27291 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27292 SECTION_DEBUG, NULL);
27293 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27294 SECTION_DEBUG, NULL);
27295 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27296 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27297 generation);
27299 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27300 stay in the main .o, but the skeleton_line goes into the
27301 split off dwo. */
27302 debug_skeleton_line_section
27303 = get_section (DEBUG_DWO_LINE_SECTION,
27304 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27305 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27306 DEBUG_SKELETON_LINE_SECTION_LABEL,
27307 generation);
27308 debug_str_offsets_section
27309 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27310 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27311 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27312 DEBUG_SKELETON_INFO_SECTION_LABEL,
27313 generation);
27314 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
27315 SECTION_DEBUG | SECTION_EXCLUDE,
27316 NULL);
27317 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27318 DEBUG_STR_DWO_SECTION_FLAGS,
27319 NULL);
27320 debug_macinfo_section_name
27321 = (dwarf_strict && dwarf_version < 5)
27322 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27323 debug_macinfo_section = get_section (debug_macinfo_section_name,
27324 SECTION_DEBUG | SECTION_EXCLUDE,
27325 NULL);
27327 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27328 SECTION_DEBUG, NULL);
27329 debug_line_section = get_section (DEBUG_LINE_SECTION,
27330 SECTION_DEBUG, NULL);
27331 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27332 SECTION_DEBUG, NULL);
27333 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27334 SECTION_DEBUG, NULL);
27335 debug_str_section = get_section (DEBUG_STR_SECTION,
27336 DEBUG_STR_SECTION_FLAGS, NULL);
27337 debug_ranges_section = get_section (dwarf_version >= 5
27338 ? DEBUG_RNGLISTS_SECTION
27339 : DEBUG_RANGES_SECTION,
27340 SECTION_DEBUG, NULL);
27341 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27342 SECTION_DEBUG, NULL);
27345 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27346 DEBUG_ABBREV_SECTION_LABEL, generation);
27347 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27348 DEBUG_INFO_SECTION_LABEL, generation);
27349 info_section_emitted = false;
27350 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27351 DEBUG_LINE_SECTION_LABEL, generation);
27352 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27353 DEBUG_RANGES_SECTION_LABEL, generation);
27354 if (dwarf_version >= 5 && dwarf_split_debug_info)
27355 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27356 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27357 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27358 DEBUG_ADDR_SECTION_LABEL, generation);
27359 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27360 (dwarf_strict && dwarf_version < 5)
27361 ? DEBUG_MACINFO_SECTION_LABEL
27362 : DEBUG_MACRO_SECTION_LABEL, generation);
27363 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27364 generation);
27366 ++generation;
27369 /* Set up for Dwarf output at the start of compilation. */
27371 static void
27372 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27374 /* Allocate the file_table. */
27375 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27377 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27378 /* Allocate the decl_die_table. */
27379 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27381 /* Allocate the decl_loc_table. */
27382 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27384 /* Allocate the cached_dw_loc_list_table. */
27385 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27387 /* Allocate the initial hunk of the decl_scope_table. */
27388 vec_alloc (decl_scope_table, 256);
27390 /* Allocate the initial hunk of the abbrev_die_table. */
27391 vec_alloc (abbrev_die_table, 256);
27392 /* Zero-th entry is allocated, but unused. */
27393 abbrev_die_table->quick_push (NULL);
27395 /* Allocate the dwarf_proc_stack_usage_map. */
27396 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27398 /* Allocate the pubtypes and pubnames vectors. */
27399 vec_alloc (pubname_table, 32);
27400 vec_alloc (pubtype_table, 32);
27402 vec_alloc (incomplete_types, 64);
27404 vec_alloc (used_rtx_array, 32);
27406 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27407 vec_alloc (macinfo_table, 64);
27408 #endif
27410 /* If front-ends already registered a main translation unit but we were not
27411 ready to perform the association, do this now. */
27412 if (main_translation_unit != NULL_TREE)
27413 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27416 /* Called before compile () starts outputtting functions, variables
27417 and toplevel asms into assembly. */
27419 static void
27420 dwarf2out_assembly_start (void)
27422 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27423 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27424 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27425 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27426 COLD_TEXT_SECTION_LABEL, 0);
27427 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27429 switch_to_section (text_section);
27430 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27431 #endif
27433 /* Make sure the line number table for .text always exists. */
27434 text_section_line_info = new_line_info_table ();
27435 text_section_line_info->end_label = text_end_label;
27437 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27438 cur_line_info_table = text_section_line_info;
27439 #endif
27441 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27442 && dwarf2out_do_cfi_asm ()
27443 && (!(flag_unwind_tables || flag_exceptions)
27444 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27445 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27448 /* A helper function for dwarf2out_finish called through
27449 htab_traverse. Assign a string its index. All strings must be
27450 collected into the table by the time index_string is called,
27451 because the indexing code relies on htab_traverse to traverse nodes
27452 in the same order for each run. */
27455 index_string (indirect_string_node **h, unsigned int *index)
27457 indirect_string_node *node = *h;
27459 find_string_form (node);
27460 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27462 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27463 node->index = *index;
27464 *index += 1;
27466 return 1;
27469 /* A helper function for output_indirect_strings called through
27470 htab_traverse. Output the offset to a string and update the
27471 current offset. */
27474 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27476 indirect_string_node *node = *h;
27478 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27480 /* Assert that this node has been assigned an index. */
27481 gcc_assert (node->index != NO_INDEX_ASSIGNED
27482 && node->index != NOT_INDEXED);
27483 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27484 "indexed string 0x%x: %s", node->index, node->str);
27485 *offset += strlen (node->str) + 1;
27487 return 1;
27490 /* A helper function for dwarf2out_finish called through
27491 htab_traverse. Output the indexed string. */
27494 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27496 struct indirect_string_node *node = *h;
27498 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27500 /* Assert that the strings are output in the same order as their
27501 indexes were assigned. */
27502 gcc_assert (*cur_idx == node->index);
27503 assemble_string (node->str, strlen (node->str) + 1);
27504 *cur_idx += 1;
27506 return 1;
27509 /* A helper function for dwarf2out_finish called through
27510 htab_traverse. Emit one queued .debug_str string. */
27513 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27515 struct indirect_string_node *node = *h;
27517 node->form = find_string_form (node);
27518 if (node->form == form && node->refcount > 0)
27520 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27521 assemble_string (node->str, strlen (node->str) + 1);
27524 return 1;
27527 /* Output the indexed string table. */
27529 static void
27530 output_indirect_strings (void)
27532 switch_to_section (debug_str_section);
27533 if (!dwarf_split_debug_info)
27534 debug_str_hash->traverse<enum dwarf_form,
27535 output_indirect_string> (DW_FORM_strp);
27536 else
27538 unsigned int offset = 0;
27539 unsigned int cur_idx = 0;
27541 skeleton_debug_str_hash->traverse<enum dwarf_form,
27542 output_indirect_string> (DW_FORM_strp);
27544 switch_to_section (debug_str_offsets_section);
27545 debug_str_hash->traverse_noresize
27546 <unsigned int *, output_index_string_offset> (&offset);
27547 switch_to_section (debug_str_dwo_section);
27548 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27549 (&cur_idx);
27553 /* Callback for htab_traverse to assign an index to an entry in the
27554 table, and to write that entry to the .debug_addr section. */
27557 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27559 addr_table_entry *entry = *slot;
27561 if (entry->refcount == 0)
27563 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27564 || entry->index == NOT_INDEXED);
27565 return 1;
27568 gcc_assert (entry->index == *cur_index);
27569 (*cur_index)++;
27571 switch (entry->kind)
27573 case ate_kind_rtx:
27574 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27575 "0x%x", entry->index);
27576 break;
27577 case ate_kind_rtx_dtprel:
27578 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27579 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27580 DWARF2_ADDR_SIZE,
27581 entry->addr.rtl);
27582 fputc ('\n', asm_out_file);
27583 break;
27584 case ate_kind_label:
27585 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27586 "0x%x", entry->index);
27587 break;
27588 default:
27589 gcc_unreachable ();
27591 return 1;
27594 /* Produce the .debug_addr section. */
27596 static void
27597 output_addr_table (void)
27599 unsigned int index = 0;
27600 if (addr_index_table == NULL || addr_index_table->size () == 0)
27601 return;
27603 switch_to_section (debug_addr_section);
27604 addr_index_table
27605 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27608 #if ENABLE_ASSERT_CHECKING
27609 /* Verify that all marks are clear. */
27611 static void
27612 verify_marks_clear (dw_die_ref die)
27614 dw_die_ref c;
27616 gcc_assert (! die->die_mark);
27617 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27619 #endif /* ENABLE_ASSERT_CHECKING */
27621 /* Clear the marks for a die and its children.
27622 Be cool if the mark isn't set. */
27624 static void
27625 prune_unmark_dies (dw_die_ref die)
27627 dw_die_ref c;
27629 if (die->die_mark)
27630 die->die_mark = 0;
27631 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27634 /* Given LOC that is referenced by a DIE we're marking as used, find all
27635 referenced DWARF procedures it references and mark them as used. */
27637 static void
27638 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27640 for (; loc != NULL; loc = loc->dw_loc_next)
27641 switch (loc->dw_loc_opc)
27643 case DW_OP_implicit_pointer:
27644 case DW_OP_convert:
27645 case DW_OP_reinterpret:
27646 case DW_OP_GNU_implicit_pointer:
27647 case DW_OP_GNU_convert:
27648 case DW_OP_GNU_reinterpret:
27649 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27650 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27651 break;
27652 case DW_OP_GNU_variable_value:
27653 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27655 dw_die_ref ref
27656 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27657 if (ref == NULL)
27658 break;
27659 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27660 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27661 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27663 /* FALLTHRU */
27664 case DW_OP_call2:
27665 case DW_OP_call4:
27666 case DW_OP_call_ref:
27667 case DW_OP_const_type:
27668 case DW_OP_GNU_const_type:
27669 case DW_OP_GNU_parameter_ref:
27670 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27671 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27672 break;
27673 case DW_OP_regval_type:
27674 case DW_OP_deref_type:
27675 case DW_OP_GNU_regval_type:
27676 case DW_OP_GNU_deref_type:
27677 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27678 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27679 break;
27680 case DW_OP_entry_value:
27681 case DW_OP_GNU_entry_value:
27682 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27683 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27684 break;
27685 default:
27686 break;
27690 /* Given DIE that we're marking as used, find any other dies
27691 it references as attributes and mark them as used. */
27693 static void
27694 prune_unused_types_walk_attribs (dw_die_ref die)
27696 dw_attr_node *a;
27697 unsigned ix;
27699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27701 switch (AT_class (a))
27703 /* Make sure DWARF procedures referenced by location descriptions will
27704 get emitted. */
27705 case dw_val_class_loc:
27706 prune_unused_types_walk_loc_descr (AT_loc (a));
27707 break;
27708 case dw_val_class_loc_list:
27709 for (dw_loc_list_ref list = AT_loc_list (a);
27710 list != NULL;
27711 list = list->dw_loc_next)
27712 prune_unused_types_walk_loc_descr (list->expr);
27713 break;
27715 case dw_val_class_die_ref:
27716 /* A reference to another DIE.
27717 Make sure that it will get emitted.
27718 If it was broken out into a comdat group, don't follow it. */
27719 if (! AT_ref (a)->comdat_type_p
27720 || a->dw_attr == DW_AT_specification)
27721 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27722 break;
27724 case dw_val_class_str:
27725 /* Set the string's refcount to 0 so that prune_unused_types_mark
27726 accounts properly for it. */
27727 a->dw_attr_val.v.val_str->refcount = 0;
27728 break;
27730 default:
27731 break;
27736 /* Mark the generic parameters and arguments children DIEs of DIE. */
27738 static void
27739 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27741 dw_die_ref c;
27743 if (die == NULL || die->die_child == NULL)
27744 return;
27745 c = die->die_child;
27748 if (is_template_parameter (c))
27749 prune_unused_types_mark (c, 1);
27750 c = c->die_sib;
27751 } while (c && c != die->die_child);
27754 /* Mark DIE as being used. If DOKIDS is true, then walk down
27755 to DIE's children. */
27757 static void
27758 prune_unused_types_mark (dw_die_ref die, int dokids)
27760 dw_die_ref c;
27762 if (die->die_mark == 0)
27764 /* We haven't done this node yet. Mark it as used. */
27765 die->die_mark = 1;
27766 /* If this is the DIE of a generic type instantiation,
27767 mark the children DIEs that describe its generic parms and
27768 args. */
27769 prune_unused_types_mark_generic_parms_dies (die);
27771 /* We also have to mark its parents as used.
27772 (But we don't want to mark our parent's kids due to this,
27773 unless it is a class.) */
27774 if (die->die_parent)
27775 prune_unused_types_mark (die->die_parent,
27776 class_scope_p (die->die_parent));
27778 /* Mark any referenced nodes. */
27779 prune_unused_types_walk_attribs (die);
27781 /* If this node is a specification,
27782 also mark the definition, if it exists. */
27783 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27784 prune_unused_types_mark (die->die_definition, 1);
27787 if (dokids && die->die_mark != 2)
27789 /* We need to walk the children, but haven't done so yet.
27790 Remember that we've walked the kids. */
27791 die->die_mark = 2;
27793 /* If this is an array type, we need to make sure our
27794 kids get marked, even if they're types. If we're
27795 breaking out types into comdat sections, do this
27796 for all type definitions. */
27797 if (die->die_tag == DW_TAG_array_type
27798 || (use_debug_types
27799 && is_type_die (die) && ! is_declaration_die (die)))
27800 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27801 else
27802 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27806 /* For local classes, look if any static member functions were emitted
27807 and if so, mark them. */
27809 static void
27810 prune_unused_types_walk_local_classes (dw_die_ref die)
27812 dw_die_ref c;
27814 if (die->die_mark == 2)
27815 return;
27817 switch (die->die_tag)
27819 case DW_TAG_structure_type:
27820 case DW_TAG_union_type:
27821 case DW_TAG_class_type:
27822 break;
27824 case DW_TAG_subprogram:
27825 if (!get_AT_flag (die, DW_AT_declaration)
27826 || die->die_definition != NULL)
27827 prune_unused_types_mark (die, 1);
27828 return;
27830 default:
27831 return;
27834 /* Mark children. */
27835 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27838 /* Walk the tree DIE and mark types that we actually use. */
27840 static void
27841 prune_unused_types_walk (dw_die_ref die)
27843 dw_die_ref c;
27845 /* Don't do anything if this node is already marked and
27846 children have been marked as well. */
27847 if (die->die_mark == 2)
27848 return;
27850 switch (die->die_tag)
27852 case DW_TAG_structure_type:
27853 case DW_TAG_union_type:
27854 case DW_TAG_class_type:
27855 if (die->die_perennial_p)
27856 break;
27858 for (c = die->die_parent; c; c = c->die_parent)
27859 if (c->die_tag == DW_TAG_subprogram)
27860 break;
27862 /* Finding used static member functions inside of classes
27863 is needed just for local classes, because for other classes
27864 static member function DIEs with DW_AT_specification
27865 are emitted outside of the DW_TAG_*_type. If we ever change
27866 it, we'd need to call this even for non-local classes. */
27867 if (c)
27868 prune_unused_types_walk_local_classes (die);
27870 /* It's a type node --- don't mark it. */
27871 return;
27873 case DW_TAG_const_type:
27874 case DW_TAG_packed_type:
27875 case DW_TAG_pointer_type:
27876 case DW_TAG_reference_type:
27877 case DW_TAG_rvalue_reference_type:
27878 case DW_TAG_volatile_type:
27879 case DW_TAG_typedef:
27880 case DW_TAG_array_type:
27881 case DW_TAG_interface_type:
27882 case DW_TAG_friend:
27883 case DW_TAG_enumeration_type:
27884 case DW_TAG_subroutine_type:
27885 case DW_TAG_string_type:
27886 case DW_TAG_set_type:
27887 case DW_TAG_subrange_type:
27888 case DW_TAG_ptr_to_member_type:
27889 case DW_TAG_file_type:
27890 /* Type nodes are useful only when other DIEs reference them --- don't
27891 mark them. */
27892 /* FALLTHROUGH */
27894 case DW_TAG_dwarf_procedure:
27895 /* Likewise for DWARF procedures. */
27897 if (die->die_perennial_p)
27898 break;
27900 return;
27902 default:
27903 /* Mark everything else. */
27904 break;
27907 if (die->die_mark == 0)
27909 die->die_mark = 1;
27911 /* Now, mark any dies referenced from here. */
27912 prune_unused_types_walk_attribs (die);
27915 die->die_mark = 2;
27917 /* Mark children. */
27918 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27921 /* Increment the string counts on strings referred to from DIE's
27922 attributes. */
27924 static void
27925 prune_unused_types_update_strings (dw_die_ref die)
27927 dw_attr_node *a;
27928 unsigned ix;
27930 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27931 if (AT_class (a) == dw_val_class_str)
27933 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27934 s->refcount++;
27935 /* Avoid unnecessarily putting strings that are used less than
27936 twice in the hash table. */
27937 if (s->refcount
27938 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27940 indirect_string_node **slot
27941 = debug_str_hash->find_slot_with_hash (s->str,
27942 htab_hash_string (s->str),
27943 INSERT);
27944 gcc_assert (*slot == NULL);
27945 *slot = s;
27950 /* Mark DIE and its children as removed. */
27952 static void
27953 mark_removed (dw_die_ref die)
27955 dw_die_ref c;
27956 die->removed = true;
27957 FOR_EACH_CHILD (die, c, mark_removed (c));
27960 /* Remove from the tree DIE any dies that aren't marked. */
27962 static void
27963 prune_unused_types_prune (dw_die_ref die)
27965 dw_die_ref c;
27967 gcc_assert (die->die_mark);
27968 prune_unused_types_update_strings (die);
27970 if (! die->die_child)
27971 return;
27973 c = die->die_child;
27974 do {
27975 dw_die_ref prev = c, next;
27976 for (c = c->die_sib; ! c->die_mark; c = next)
27977 if (c == die->die_child)
27979 /* No marked children between 'prev' and the end of the list. */
27980 if (prev == c)
27981 /* No marked children at all. */
27982 die->die_child = NULL;
27983 else
27985 prev->die_sib = c->die_sib;
27986 die->die_child = prev;
27988 c->die_sib = NULL;
27989 mark_removed (c);
27990 return;
27992 else
27994 next = c->die_sib;
27995 c->die_sib = NULL;
27996 mark_removed (c);
27999 if (c != prev->die_sib)
28000 prev->die_sib = c;
28001 prune_unused_types_prune (c);
28002 } while (c != die->die_child);
28005 /* Remove dies representing declarations that we never use. */
28007 static void
28008 prune_unused_types (void)
28010 unsigned int i;
28011 limbo_die_node *node;
28012 comdat_type_node *ctnode;
28013 pubname_entry *pub;
28014 dw_die_ref base_type;
28016 #if ENABLE_ASSERT_CHECKING
28017 /* All the marks should already be clear. */
28018 verify_marks_clear (comp_unit_die ());
28019 for (node = limbo_die_list; node; node = node->next)
28020 verify_marks_clear (node->die);
28021 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28022 verify_marks_clear (ctnode->root_die);
28023 #endif /* ENABLE_ASSERT_CHECKING */
28025 /* Mark types that are used in global variables. */
28026 premark_types_used_by_global_vars ();
28028 /* Set the mark on nodes that are actually used. */
28029 prune_unused_types_walk (comp_unit_die ());
28030 for (node = limbo_die_list; node; node = node->next)
28031 prune_unused_types_walk (node->die);
28032 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28034 prune_unused_types_walk (ctnode->root_die);
28035 prune_unused_types_mark (ctnode->type_die, 1);
28038 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28039 are unusual in that they are pubnames that are the children of pubtypes.
28040 They should only be marked via their parent DW_TAG_enumeration_type die,
28041 not as roots in themselves. */
28042 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28043 if (pub->die->die_tag != DW_TAG_enumerator)
28044 prune_unused_types_mark (pub->die, 1);
28045 for (i = 0; base_types.iterate (i, &base_type); i++)
28046 prune_unused_types_mark (base_type, 1);
28048 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28049 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28050 callees). */
28051 cgraph_node *cnode;
28052 FOR_EACH_FUNCTION (cnode)
28053 if (cnode->referred_to_p (false))
28055 dw_die_ref die = lookup_decl_die (cnode->decl);
28056 if (die == NULL || die->die_mark)
28057 continue;
28058 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28059 if (e->caller != cnode
28060 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28062 prune_unused_types_mark (die, 1);
28063 break;
28067 if (debug_str_hash)
28068 debug_str_hash->empty ();
28069 if (skeleton_debug_str_hash)
28070 skeleton_debug_str_hash->empty ();
28071 prune_unused_types_prune (comp_unit_die ());
28072 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28074 node = *pnode;
28075 if (!node->die->die_mark)
28076 *pnode = node->next;
28077 else
28079 prune_unused_types_prune (node->die);
28080 pnode = &node->next;
28083 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28084 prune_unused_types_prune (ctnode->root_die);
28086 /* Leave the marks clear. */
28087 prune_unmark_dies (comp_unit_die ());
28088 for (node = limbo_die_list; node; node = node->next)
28089 prune_unmark_dies (node->die);
28090 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28091 prune_unmark_dies (ctnode->root_die);
28094 /* Helpers to manipulate hash table of comdat type units. */
28096 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28098 static inline hashval_t hash (const comdat_type_node *);
28099 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28102 inline hashval_t
28103 comdat_type_hasher::hash (const comdat_type_node *type_node)
28105 hashval_t h;
28106 memcpy (&h, type_node->signature, sizeof (h));
28107 return h;
28110 inline bool
28111 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28112 const comdat_type_node *type_node_2)
28114 return (! memcmp (type_node_1->signature, type_node_2->signature,
28115 DWARF_TYPE_SIGNATURE_SIZE));
28118 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28119 to the location it would have been added, should we know its
28120 DECL_ASSEMBLER_NAME when we added other attributes. This will
28121 probably improve compactness of debug info, removing equivalent
28122 abbrevs, and hide any differences caused by deferring the
28123 computation of the assembler name, triggered by e.g. PCH. */
28125 static inline void
28126 move_linkage_attr (dw_die_ref die)
28128 unsigned ix = vec_safe_length (die->die_attr);
28129 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28131 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28132 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28134 while (--ix > 0)
28136 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28138 if (prev->dw_attr == DW_AT_decl_line
28139 || prev->dw_attr == DW_AT_decl_column
28140 || prev->dw_attr == DW_AT_name)
28141 break;
28144 if (ix != vec_safe_length (die->die_attr) - 1)
28146 die->die_attr->pop ();
28147 die->die_attr->quick_insert (ix, linkage);
28151 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28152 referenced from typed stack ops and count how often they are used. */
28154 static void
28155 mark_base_types (dw_loc_descr_ref loc)
28157 dw_die_ref base_type = NULL;
28159 for (; loc; loc = loc->dw_loc_next)
28161 switch (loc->dw_loc_opc)
28163 case DW_OP_regval_type:
28164 case DW_OP_deref_type:
28165 case DW_OP_GNU_regval_type:
28166 case DW_OP_GNU_deref_type:
28167 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28168 break;
28169 case DW_OP_convert:
28170 case DW_OP_reinterpret:
28171 case DW_OP_GNU_convert:
28172 case DW_OP_GNU_reinterpret:
28173 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28174 continue;
28175 /* FALLTHRU */
28176 case DW_OP_const_type:
28177 case DW_OP_GNU_const_type:
28178 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28179 break;
28180 case DW_OP_entry_value:
28181 case DW_OP_GNU_entry_value:
28182 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28183 continue;
28184 default:
28185 continue;
28187 gcc_assert (base_type->die_parent == comp_unit_die ());
28188 if (base_type->die_mark)
28189 base_type->die_mark++;
28190 else
28192 base_types.safe_push (base_type);
28193 base_type->die_mark = 1;
28198 /* Comparison function for sorting marked base types. */
28200 static int
28201 base_type_cmp (const void *x, const void *y)
28203 dw_die_ref dx = *(const dw_die_ref *) x;
28204 dw_die_ref dy = *(const dw_die_ref *) y;
28205 unsigned int byte_size1, byte_size2;
28206 unsigned int encoding1, encoding2;
28207 unsigned int align1, align2;
28208 if (dx->die_mark > dy->die_mark)
28209 return -1;
28210 if (dx->die_mark < dy->die_mark)
28211 return 1;
28212 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28213 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28214 if (byte_size1 < byte_size2)
28215 return 1;
28216 if (byte_size1 > byte_size2)
28217 return -1;
28218 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28219 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28220 if (encoding1 < encoding2)
28221 return 1;
28222 if (encoding1 > encoding2)
28223 return -1;
28224 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28225 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28226 if (align1 < align2)
28227 return 1;
28228 if (align1 > align2)
28229 return -1;
28230 return 0;
28233 /* Move base types marked by mark_base_types as early as possible
28234 in the CU, sorted by decreasing usage count both to make the
28235 uleb128 references as small as possible and to make sure they
28236 will have die_offset already computed by calc_die_sizes when
28237 sizes of typed stack loc ops is computed. */
28239 static void
28240 move_marked_base_types (void)
28242 unsigned int i;
28243 dw_die_ref base_type, die, c;
28245 if (base_types.is_empty ())
28246 return;
28248 /* Sort by decreasing usage count, they will be added again in that
28249 order later on. */
28250 base_types.qsort (base_type_cmp);
28251 die = comp_unit_die ();
28252 c = die->die_child;
28255 dw_die_ref prev = c;
28256 c = c->die_sib;
28257 while (c->die_mark)
28259 remove_child_with_prev (c, prev);
28260 /* As base types got marked, there must be at least
28261 one node other than DW_TAG_base_type. */
28262 gcc_assert (die->die_child != NULL);
28263 c = prev->die_sib;
28266 while (c != die->die_child);
28267 gcc_assert (die->die_child);
28268 c = die->die_child;
28269 for (i = 0; base_types.iterate (i, &base_type); i++)
28271 base_type->die_mark = 0;
28272 base_type->die_sib = c->die_sib;
28273 c->die_sib = base_type;
28274 c = base_type;
28278 /* Helper function for resolve_addr, attempt to resolve
28279 one CONST_STRING, return true if successful. Similarly verify that
28280 SYMBOL_REFs refer to variables emitted in the current CU. */
28282 static bool
28283 resolve_one_addr (rtx *addr)
28285 rtx rtl = *addr;
28287 if (GET_CODE (rtl) == CONST_STRING)
28289 size_t len = strlen (XSTR (rtl, 0)) + 1;
28290 tree t = build_string (len, XSTR (rtl, 0));
28291 tree tlen = size_int (len - 1);
28292 TREE_TYPE (t)
28293 = build_array_type (char_type_node, build_index_type (tlen));
28294 rtl = lookup_constant_def (t);
28295 if (!rtl || !MEM_P (rtl))
28296 return false;
28297 rtl = XEXP (rtl, 0);
28298 if (GET_CODE (rtl) == SYMBOL_REF
28299 && SYMBOL_REF_DECL (rtl)
28300 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28301 return false;
28302 vec_safe_push (used_rtx_array, rtl);
28303 *addr = rtl;
28304 return true;
28307 if (GET_CODE (rtl) == SYMBOL_REF
28308 && SYMBOL_REF_DECL (rtl))
28310 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28312 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28313 return false;
28315 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28316 return false;
28319 if (GET_CODE (rtl) == CONST)
28321 subrtx_ptr_iterator::array_type array;
28322 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28323 if (!resolve_one_addr (*iter))
28324 return false;
28327 return true;
28330 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28331 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28332 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28334 static rtx
28335 string_cst_pool_decl (tree t)
28337 rtx rtl = output_constant_def (t, 1);
28338 unsigned char *array;
28339 dw_loc_descr_ref l;
28340 tree decl;
28341 size_t len;
28342 dw_die_ref ref;
28344 if (!rtl || !MEM_P (rtl))
28345 return NULL_RTX;
28346 rtl = XEXP (rtl, 0);
28347 if (GET_CODE (rtl) != SYMBOL_REF
28348 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28349 return NULL_RTX;
28351 decl = SYMBOL_REF_DECL (rtl);
28352 if (!lookup_decl_die (decl))
28354 len = TREE_STRING_LENGTH (t);
28355 vec_safe_push (used_rtx_array, rtl);
28356 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28357 array = ggc_vec_alloc<unsigned char> (len);
28358 memcpy (array, TREE_STRING_POINTER (t), len);
28359 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28360 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28361 l->dw_loc_oprnd2.v.val_vec.length = len;
28362 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28363 l->dw_loc_oprnd2.v.val_vec.array = array;
28364 add_AT_loc (ref, DW_AT_location, l);
28365 equate_decl_number_to_die (decl, ref);
28367 return rtl;
28370 /* Helper function of resolve_addr_in_expr. LOC is
28371 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28372 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28373 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28374 with DW_OP_implicit_pointer if possible
28375 and return true, if unsuccessful, return false. */
28377 static bool
28378 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28380 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28381 HOST_WIDE_INT offset = 0;
28382 dw_die_ref ref = NULL;
28383 tree decl;
28385 if (GET_CODE (rtl) == CONST
28386 && GET_CODE (XEXP (rtl, 0)) == PLUS
28387 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28389 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28390 rtl = XEXP (XEXP (rtl, 0), 0);
28392 if (GET_CODE (rtl) == CONST_STRING)
28394 size_t len = strlen (XSTR (rtl, 0)) + 1;
28395 tree t = build_string (len, XSTR (rtl, 0));
28396 tree tlen = size_int (len - 1);
28398 TREE_TYPE (t)
28399 = build_array_type (char_type_node, build_index_type (tlen));
28400 rtl = string_cst_pool_decl (t);
28401 if (!rtl)
28402 return false;
28404 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28406 decl = SYMBOL_REF_DECL (rtl);
28407 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28409 ref = lookup_decl_die (decl);
28410 if (ref && (get_AT (ref, DW_AT_location)
28411 || get_AT (ref, DW_AT_const_value)))
28413 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28414 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28415 loc->dw_loc_oprnd1.val_entry = NULL;
28416 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28417 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28418 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28419 loc->dw_loc_oprnd2.v.val_int = offset;
28420 return true;
28424 return false;
28427 /* Helper function for resolve_addr, handle one location
28428 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28429 the location list couldn't be resolved. */
28431 static bool
28432 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28434 dw_loc_descr_ref keep = NULL;
28435 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28436 switch (loc->dw_loc_opc)
28438 case DW_OP_addr:
28439 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28441 if ((prev == NULL
28442 || prev->dw_loc_opc == DW_OP_piece
28443 || prev->dw_loc_opc == DW_OP_bit_piece)
28444 && loc->dw_loc_next
28445 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28446 && (!dwarf_strict || dwarf_version >= 5)
28447 && optimize_one_addr_into_implicit_ptr (loc))
28448 break;
28449 return false;
28451 break;
28452 case DW_OP_GNU_addr_index:
28453 case DW_OP_GNU_const_index:
28454 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28455 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28457 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28458 if (!resolve_one_addr (&rtl))
28459 return false;
28460 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28461 loc->dw_loc_oprnd1.val_entry
28462 = add_addr_table_entry (rtl, ate_kind_rtx);
28464 break;
28465 case DW_OP_const4u:
28466 case DW_OP_const8u:
28467 if (loc->dtprel
28468 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28469 return false;
28470 break;
28471 case DW_OP_plus_uconst:
28472 if (size_of_loc_descr (loc)
28473 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28475 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28477 dw_loc_descr_ref repl
28478 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28479 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28480 add_loc_descr (&repl, loc->dw_loc_next);
28481 *loc = *repl;
28483 break;
28484 case DW_OP_implicit_value:
28485 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28486 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28487 return false;
28488 break;
28489 case DW_OP_implicit_pointer:
28490 case DW_OP_GNU_implicit_pointer:
28491 case DW_OP_GNU_parameter_ref:
28492 case DW_OP_GNU_variable_value:
28493 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28495 dw_die_ref ref
28496 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28497 if (ref == NULL)
28498 return false;
28499 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28500 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28501 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28503 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28505 if (prev == NULL
28506 && loc->dw_loc_next == NULL
28507 && AT_class (a) == dw_val_class_loc)
28508 switch (a->dw_attr)
28510 /* Following attributes allow both exprloc and reference,
28511 so if the whole expression is DW_OP_GNU_variable_value
28512 alone we could transform it into reference. */
28513 case DW_AT_byte_size:
28514 case DW_AT_bit_size:
28515 case DW_AT_lower_bound:
28516 case DW_AT_upper_bound:
28517 case DW_AT_bit_stride:
28518 case DW_AT_count:
28519 case DW_AT_allocated:
28520 case DW_AT_associated:
28521 case DW_AT_byte_stride:
28522 a->dw_attr_val.val_class = dw_val_class_die_ref;
28523 a->dw_attr_val.val_entry = NULL;
28524 a->dw_attr_val.v.val_die_ref.die
28525 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28526 a->dw_attr_val.v.val_die_ref.external = 0;
28527 return true;
28528 default:
28529 break;
28531 if (dwarf_strict)
28532 return false;
28534 break;
28535 case DW_OP_const_type:
28536 case DW_OP_regval_type:
28537 case DW_OP_deref_type:
28538 case DW_OP_convert:
28539 case DW_OP_reinterpret:
28540 case DW_OP_GNU_const_type:
28541 case DW_OP_GNU_regval_type:
28542 case DW_OP_GNU_deref_type:
28543 case DW_OP_GNU_convert:
28544 case DW_OP_GNU_reinterpret:
28545 while (loc->dw_loc_next
28546 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28547 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28549 dw_die_ref base1, base2;
28550 unsigned enc1, enc2, size1, size2;
28551 if (loc->dw_loc_opc == DW_OP_regval_type
28552 || loc->dw_loc_opc == DW_OP_deref_type
28553 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28554 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28555 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28556 else if (loc->dw_loc_oprnd1.val_class
28557 == dw_val_class_unsigned_const)
28558 break;
28559 else
28560 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28561 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28562 == dw_val_class_unsigned_const)
28563 break;
28564 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28565 gcc_assert (base1->die_tag == DW_TAG_base_type
28566 && base2->die_tag == DW_TAG_base_type);
28567 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28568 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28569 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28570 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28571 if (size1 == size2
28572 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28573 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28574 && loc != keep)
28575 || enc1 == enc2))
28577 /* Optimize away next DW_OP_convert after
28578 adjusting LOC's base type die reference. */
28579 if (loc->dw_loc_opc == DW_OP_regval_type
28580 || loc->dw_loc_opc == DW_OP_deref_type
28581 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28582 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28583 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28584 else
28585 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28586 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28587 continue;
28589 /* Don't change integer DW_OP_convert after e.g. floating
28590 point typed stack entry. */
28591 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28592 keep = loc->dw_loc_next;
28593 break;
28595 break;
28596 default:
28597 break;
28599 return true;
28602 /* Helper function of resolve_addr. DIE had DW_AT_location of
28603 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28604 and DW_OP_addr couldn't be resolved. resolve_addr has already
28605 removed the DW_AT_location attribute. This function attempts to
28606 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28607 to it or DW_AT_const_value attribute, if possible. */
28609 static void
28610 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28612 if (!VAR_P (decl)
28613 || lookup_decl_die (decl) != die
28614 || DECL_EXTERNAL (decl)
28615 || !TREE_STATIC (decl)
28616 || DECL_INITIAL (decl) == NULL_TREE
28617 || DECL_P (DECL_INITIAL (decl))
28618 || get_AT (die, DW_AT_const_value))
28619 return;
28621 tree init = DECL_INITIAL (decl);
28622 HOST_WIDE_INT offset = 0;
28623 /* For variables that have been optimized away and thus
28624 don't have a memory location, see if we can emit
28625 DW_AT_const_value instead. */
28626 if (tree_add_const_value_attribute (die, init))
28627 return;
28628 if (dwarf_strict && dwarf_version < 5)
28629 return;
28630 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28631 and ADDR_EXPR refers to a decl that has DW_AT_location or
28632 DW_AT_const_value (but isn't addressable, otherwise
28633 resolving the original DW_OP_addr wouldn't fail), see if
28634 we can add DW_OP_implicit_pointer. */
28635 STRIP_NOPS (init);
28636 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28637 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28639 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28640 init = TREE_OPERAND (init, 0);
28641 STRIP_NOPS (init);
28643 if (TREE_CODE (init) != ADDR_EXPR)
28644 return;
28645 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28646 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28647 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28648 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28649 && TREE_OPERAND (init, 0) != decl))
28651 dw_die_ref ref;
28652 dw_loc_descr_ref l;
28654 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28656 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28657 if (!rtl)
28658 return;
28659 decl = SYMBOL_REF_DECL (rtl);
28661 else
28662 decl = TREE_OPERAND (init, 0);
28663 ref = lookup_decl_die (decl);
28664 if (ref == NULL
28665 || (!get_AT (ref, DW_AT_location)
28666 && !get_AT (ref, DW_AT_const_value)))
28667 return;
28668 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28669 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28670 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28671 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28672 add_AT_loc (die, DW_AT_location, l);
28676 /* Return NULL if l is a DWARF expression, or first op that is not
28677 valid DWARF expression. */
28679 static dw_loc_descr_ref
28680 non_dwarf_expression (dw_loc_descr_ref l)
28682 while (l)
28684 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28685 return l;
28686 switch (l->dw_loc_opc)
28688 case DW_OP_regx:
28689 case DW_OP_implicit_value:
28690 case DW_OP_stack_value:
28691 case DW_OP_implicit_pointer:
28692 case DW_OP_GNU_implicit_pointer:
28693 case DW_OP_GNU_parameter_ref:
28694 case DW_OP_piece:
28695 case DW_OP_bit_piece:
28696 return l;
28697 default:
28698 break;
28700 l = l->dw_loc_next;
28702 return NULL;
28705 /* Return adjusted copy of EXPR:
28706 If it is empty DWARF expression, return it.
28707 If it is valid non-empty DWARF expression,
28708 return copy of EXPR with DW_OP_deref appended to it.
28709 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28710 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28711 If it is DWARF expression followed by DW_OP_stack_value, return
28712 copy of the DWARF expression without anything appended.
28713 Otherwise, return NULL. */
28715 static dw_loc_descr_ref
28716 copy_deref_exprloc (dw_loc_descr_ref expr)
28718 dw_loc_descr_ref tail = NULL;
28720 if (expr == NULL)
28721 return NULL;
28723 dw_loc_descr_ref l = non_dwarf_expression (expr);
28724 if (l && l->dw_loc_next)
28725 return NULL;
28727 if (l)
28729 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28730 tail = new_loc_descr ((enum dwarf_location_atom)
28731 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28732 0, 0);
28733 else
28734 switch (l->dw_loc_opc)
28736 case DW_OP_regx:
28737 tail = new_loc_descr (DW_OP_bregx,
28738 l->dw_loc_oprnd1.v.val_unsigned, 0);
28739 break;
28740 case DW_OP_stack_value:
28741 break;
28742 default:
28743 return NULL;
28746 else
28747 tail = new_loc_descr (DW_OP_deref, 0, 0);
28749 dw_loc_descr_ref ret = NULL, *p = &ret;
28750 while (expr != l)
28752 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28753 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28754 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28755 p = &(*p)->dw_loc_next;
28756 expr = expr->dw_loc_next;
28758 *p = tail;
28759 return ret;
28762 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28763 reference to a variable or argument, adjust it if needed and return:
28764 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28765 attribute if present should be removed
28766 0 keep the attribute perhaps with minor modifications, no need to rescan
28767 1 if the attribute has been successfully adjusted. */
28769 static int
28770 optimize_string_length (dw_attr_node *a)
28772 dw_loc_descr_ref l = AT_loc (a), lv;
28773 dw_die_ref die;
28774 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28776 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28777 die = lookup_decl_die (decl);
28778 if (die)
28780 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28781 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28782 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28784 else
28785 return -1;
28787 else
28788 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28790 /* DWARF5 allows reference class, so we can then reference the DIE.
28791 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28792 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28794 a->dw_attr_val.val_class = dw_val_class_die_ref;
28795 a->dw_attr_val.val_entry = NULL;
28796 a->dw_attr_val.v.val_die_ref.die = die;
28797 a->dw_attr_val.v.val_die_ref.external = 0;
28798 return 0;
28801 dw_attr_node *av = get_AT (die, DW_AT_location);
28802 dw_loc_list_ref d;
28803 bool non_dwarf_expr = false;
28805 if (av == NULL)
28806 return dwarf_strict ? -1 : 0;
28807 switch (AT_class (av))
28809 case dw_val_class_loc_list:
28810 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28811 if (d->expr && non_dwarf_expression (d->expr))
28812 non_dwarf_expr = true;
28813 break;
28814 case dw_val_class_loc:
28815 lv = AT_loc (av);
28816 if (lv == NULL)
28817 return dwarf_strict ? -1 : 0;
28818 if (non_dwarf_expression (lv))
28819 non_dwarf_expr = true;
28820 break;
28821 default:
28822 return dwarf_strict ? -1 : 0;
28825 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28826 into DW_OP_call4 or DW_OP_GNU_variable_value into
28827 DW_OP_call4 DW_OP_deref, do so. */
28828 if (!non_dwarf_expr
28829 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28831 l->dw_loc_opc = DW_OP_call4;
28832 if (l->dw_loc_next)
28833 l->dw_loc_next = NULL;
28834 else
28835 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28836 return 0;
28839 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28840 copy over the DW_AT_location attribute from die to a. */
28841 if (l->dw_loc_next != NULL)
28843 a->dw_attr_val = av->dw_attr_val;
28844 return 1;
28847 dw_loc_list_ref list, *p;
28848 switch (AT_class (av))
28850 case dw_val_class_loc_list:
28851 p = &list;
28852 list = NULL;
28853 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28855 lv = copy_deref_exprloc (d->expr);
28856 if (lv)
28858 *p = new_loc_list (lv, d->begin, d->end, d->section);
28859 p = &(*p)->dw_loc_next;
28861 else if (!dwarf_strict && d->expr)
28862 return 0;
28864 if (list == NULL)
28865 return dwarf_strict ? -1 : 0;
28866 a->dw_attr_val.val_class = dw_val_class_loc_list;
28867 gen_llsym (list);
28868 *AT_loc_list_ptr (a) = list;
28869 return 1;
28870 case dw_val_class_loc:
28871 lv = copy_deref_exprloc (AT_loc (av));
28872 if (lv == NULL)
28873 return dwarf_strict ? -1 : 0;
28874 a->dw_attr_val.v.val_loc = lv;
28875 return 1;
28876 default:
28877 gcc_unreachable ();
28881 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28882 an address in .rodata section if the string literal is emitted there,
28883 or remove the containing location list or replace DW_AT_const_value
28884 with DW_AT_location and empty location expression, if it isn't found
28885 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28886 to something that has been emitted in the current CU. */
28888 static void
28889 resolve_addr (dw_die_ref die)
28891 dw_die_ref c;
28892 dw_attr_node *a;
28893 dw_loc_list_ref *curr, *start, loc;
28894 unsigned ix;
28895 bool remove_AT_byte_size = false;
28897 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28898 switch (AT_class (a))
28900 case dw_val_class_loc_list:
28901 start = curr = AT_loc_list_ptr (a);
28902 loc = *curr;
28903 gcc_assert (loc);
28904 /* The same list can be referenced more than once. See if we have
28905 already recorded the result from a previous pass. */
28906 if (loc->replaced)
28907 *curr = loc->dw_loc_next;
28908 else if (!loc->resolved_addr)
28910 /* As things stand, we do not expect or allow one die to
28911 reference a suffix of another die's location list chain.
28912 References must be identical or completely separate.
28913 There is therefore no need to cache the result of this
28914 pass on any list other than the first; doing so
28915 would lead to unnecessary writes. */
28916 while (*curr)
28918 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28919 if (!resolve_addr_in_expr (a, (*curr)->expr))
28921 dw_loc_list_ref next = (*curr)->dw_loc_next;
28922 dw_loc_descr_ref l = (*curr)->expr;
28924 if (next && (*curr)->ll_symbol)
28926 gcc_assert (!next->ll_symbol);
28927 next->ll_symbol = (*curr)->ll_symbol;
28929 if (dwarf_split_debug_info)
28930 remove_loc_list_addr_table_entries (l);
28931 *curr = next;
28933 else
28935 mark_base_types ((*curr)->expr);
28936 curr = &(*curr)->dw_loc_next;
28939 if (loc == *start)
28940 loc->resolved_addr = 1;
28941 else
28943 loc->replaced = 1;
28944 loc->dw_loc_next = *start;
28947 if (!*start)
28949 remove_AT (die, a->dw_attr);
28950 ix--;
28952 break;
28953 case dw_val_class_loc:
28955 dw_loc_descr_ref l = AT_loc (a);
28956 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28957 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28958 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28959 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28960 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28961 with DW_FORM_ref referencing the same DIE as
28962 DW_OP_GNU_variable_value used to reference. */
28963 if (a->dw_attr == DW_AT_string_length
28964 && l
28965 && l->dw_loc_opc == DW_OP_GNU_variable_value
28966 && (l->dw_loc_next == NULL
28967 || (l->dw_loc_next->dw_loc_next == NULL
28968 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28970 switch (optimize_string_length (a))
28972 case -1:
28973 remove_AT (die, a->dw_attr);
28974 ix--;
28975 /* If we drop DW_AT_string_length, we need to drop also
28976 DW_AT_{string_length_,}byte_size. */
28977 remove_AT_byte_size = true;
28978 continue;
28979 default:
28980 break;
28981 case 1:
28982 /* Even if we keep the optimized DW_AT_string_length,
28983 it might have changed AT_class, so process it again. */
28984 ix--;
28985 continue;
28988 /* For -gdwarf-2 don't attempt to optimize
28989 DW_AT_data_member_location containing
28990 DW_OP_plus_uconst - older consumers might
28991 rely on it being that op instead of a more complex,
28992 but shorter, location description. */
28993 if ((dwarf_version > 2
28994 || a->dw_attr != DW_AT_data_member_location
28995 || l == NULL
28996 || l->dw_loc_opc != DW_OP_plus_uconst
28997 || l->dw_loc_next != NULL)
28998 && !resolve_addr_in_expr (a, l))
29000 if (dwarf_split_debug_info)
29001 remove_loc_list_addr_table_entries (l);
29002 if (l != NULL
29003 && l->dw_loc_next == NULL
29004 && l->dw_loc_opc == DW_OP_addr
29005 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29006 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29007 && a->dw_attr == DW_AT_location)
29009 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29010 remove_AT (die, a->dw_attr);
29011 ix--;
29012 optimize_location_into_implicit_ptr (die, decl);
29013 break;
29015 if (a->dw_attr == DW_AT_string_length)
29016 /* If we drop DW_AT_string_length, we need to drop also
29017 DW_AT_{string_length_,}byte_size. */
29018 remove_AT_byte_size = true;
29019 remove_AT (die, a->dw_attr);
29020 ix--;
29022 else
29023 mark_base_types (l);
29025 break;
29026 case dw_val_class_addr:
29027 if (a->dw_attr == DW_AT_const_value
29028 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29030 if (AT_index (a) != NOT_INDEXED)
29031 remove_addr_table_entry (a->dw_attr_val.val_entry);
29032 remove_AT (die, a->dw_attr);
29033 ix--;
29035 if ((die->die_tag == DW_TAG_call_site
29036 && a->dw_attr == DW_AT_call_origin)
29037 || (die->die_tag == DW_TAG_GNU_call_site
29038 && a->dw_attr == DW_AT_abstract_origin))
29040 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29041 dw_die_ref tdie = lookup_decl_die (tdecl);
29042 dw_die_ref cdie;
29043 if (tdie == NULL
29044 && DECL_EXTERNAL (tdecl)
29045 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29046 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29048 dw_die_ref pdie = cdie;
29049 /* Make sure we don't add these DIEs into type units.
29050 We could emit skeleton DIEs for context (namespaces,
29051 outer structs/classes) and a skeleton DIE for the
29052 innermost context with DW_AT_signature pointing to the
29053 type unit. See PR78835. */
29054 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29055 pdie = pdie->die_parent;
29056 if (pdie == NULL)
29058 /* Creating a full DIE for tdecl is overly expensive and
29059 at this point even wrong when in the LTO phase
29060 as it can end up generating new type DIEs we didn't
29061 output and thus optimize_external_refs will crash. */
29062 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29063 add_AT_flag (tdie, DW_AT_external, 1);
29064 add_AT_flag (tdie, DW_AT_declaration, 1);
29065 add_linkage_attr (tdie, tdecl);
29066 add_name_and_src_coords_attributes (tdie, tdecl, true);
29067 equate_decl_number_to_die (tdecl, tdie);
29070 if (tdie)
29072 a->dw_attr_val.val_class = dw_val_class_die_ref;
29073 a->dw_attr_val.v.val_die_ref.die = tdie;
29074 a->dw_attr_val.v.val_die_ref.external = 0;
29076 else
29078 if (AT_index (a) != NOT_INDEXED)
29079 remove_addr_table_entry (a->dw_attr_val.val_entry);
29080 remove_AT (die, a->dw_attr);
29081 ix--;
29084 break;
29085 default:
29086 break;
29089 if (remove_AT_byte_size)
29090 remove_AT (die, dwarf_version >= 5
29091 ? DW_AT_string_length_byte_size
29092 : DW_AT_byte_size);
29094 FOR_EACH_CHILD (die, c, resolve_addr (c));
29097 /* Helper routines for optimize_location_lists.
29098 This pass tries to share identical local lists in .debug_loc
29099 section. */
29101 /* Iteratively hash operands of LOC opcode into HSTATE. */
29103 static void
29104 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29106 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29107 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29109 switch (loc->dw_loc_opc)
29111 case DW_OP_const4u:
29112 case DW_OP_const8u:
29113 if (loc->dtprel)
29114 goto hash_addr;
29115 /* FALLTHRU */
29116 case DW_OP_const1u:
29117 case DW_OP_const1s:
29118 case DW_OP_const2u:
29119 case DW_OP_const2s:
29120 case DW_OP_const4s:
29121 case DW_OP_const8s:
29122 case DW_OP_constu:
29123 case DW_OP_consts:
29124 case DW_OP_pick:
29125 case DW_OP_plus_uconst:
29126 case DW_OP_breg0:
29127 case DW_OP_breg1:
29128 case DW_OP_breg2:
29129 case DW_OP_breg3:
29130 case DW_OP_breg4:
29131 case DW_OP_breg5:
29132 case DW_OP_breg6:
29133 case DW_OP_breg7:
29134 case DW_OP_breg8:
29135 case DW_OP_breg9:
29136 case DW_OP_breg10:
29137 case DW_OP_breg11:
29138 case DW_OP_breg12:
29139 case DW_OP_breg13:
29140 case DW_OP_breg14:
29141 case DW_OP_breg15:
29142 case DW_OP_breg16:
29143 case DW_OP_breg17:
29144 case DW_OP_breg18:
29145 case DW_OP_breg19:
29146 case DW_OP_breg20:
29147 case DW_OP_breg21:
29148 case DW_OP_breg22:
29149 case DW_OP_breg23:
29150 case DW_OP_breg24:
29151 case DW_OP_breg25:
29152 case DW_OP_breg26:
29153 case DW_OP_breg27:
29154 case DW_OP_breg28:
29155 case DW_OP_breg29:
29156 case DW_OP_breg30:
29157 case DW_OP_breg31:
29158 case DW_OP_regx:
29159 case DW_OP_fbreg:
29160 case DW_OP_piece:
29161 case DW_OP_deref_size:
29162 case DW_OP_xderef_size:
29163 hstate.add_object (val1->v.val_int);
29164 break;
29165 case DW_OP_skip:
29166 case DW_OP_bra:
29168 int offset;
29170 gcc_assert (val1->val_class == dw_val_class_loc);
29171 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29172 hstate.add_object (offset);
29174 break;
29175 case DW_OP_implicit_value:
29176 hstate.add_object (val1->v.val_unsigned);
29177 switch (val2->val_class)
29179 case dw_val_class_const:
29180 hstate.add_object (val2->v.val_int);
29181 break;
29182 case dw_val_class_vec:
29184 unsigned int elt_size = val2->v.val_vec.elt_size;
29185 unsigned int len = val2->v.val_vec.length;
29187 hstate.add_int (elt_size);
29188 hstate.add_int (len);
29189 hstate.add (val2->v.val_vec.array, len * elt_size);
29191 break;
29192 case dw_val_class_const_double:
29193 hstate.add_object (val2->v.val_double.low);
29194 hstate.add_object (val2->v.val_double.high);
29195 break;
29196 case dw_val_class_wide_int:
29197 hstate.add (val2->v.val_wide->get_val (),
29198 get_full_len (*val2->v.val_wide)
29199 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29200 break;
29201 case dw_val_class_addr:
29202 inchash::add_rtx (val2->v.val_addr, hstate);
29203 break;
29204 default:
29205 gcc_unreachable ();
29207 break;
29208 case DW_OP_bregx:
29209 case DW_OP_bit_piece:
29210 hstate.add_object (val1->v.val_int);
29211 hstate.add_object (val2->v.val_int);
29212 break;
29213 case DW_OP_addr:
29214 hash_addr:
29215 if (loc->dtprel)
29217 unsigned char dtprel = 0xd1;
29218 hstate.add_object (dtprel);
29220 inchash::add_rtx (val1->v.val_addr, hstate);
29221 break;
29222 case DW_OP_GNU_addr_index:
29223 case DW_OP_GNU_const_index:
29225 if (loc->dtprel)
29227 unsigned char dtprel = 0xd1;
29228 hstate.add_object (dtprel);
29230 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29232 break;
29233 case DW_OP_implicit_pointer:
29234 case DW_OP_GNU_implicit_pointer:
29235 hstate.add_int (val2->v.val_int);
29236 break;
29237 case DW_OP_entry_value:
29238 case DW_OP_GNU_entry_value:
29239 hstate.add_object (val1->v.val_loc);
29240 break;
29241 case DW_OP_regval_type:
29242 case DW_OP_deref_type:
29243 case DW_OP_GNU_regval_type:
29244 case DW_OP_GNU_deref_type:
29246 unsigned int byte_size
29247 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29248 unsigned int encoding
29249 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29250 hstate.add_object (val1->v.val_int);
29251 hstate.add_object (byte_size);
29252 hstate.add_object (encoding);
29254 break;
29255 case DW_OP_convert:
29256 case DW_OP_reinterpret:
29257 case DW_OP_GNU_convert:
29258 case DW_OP_GNU_reinterpret:
29259 if (val1->val_class == dw_val_class_unsigned_const)
29261 hstate.add_object (val1->v.val_unsigned);
29262 break;
29264 /* FALLTHRU */
29265 case DW_OP_const_type:
29266 case DW_OP_GNU_const_type:
29268 unsigned int byte_size
29269 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29270 unsigned int encoding
29271 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29272 hstate.add_object (byte_size);
29273 hstate.add_object (encoding);
29274 if (loc->dw_loc_opc != DW_OP_const_type
29275 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29276 break;
29277 hstate.add_object (val2->val_class);
29278 switch (val2->val_class)
29280 case dw_val_class_const:
29281 hstate.add_object (val2->v.val_int);
29282 break;
29283 case dw_val_class_vec:
29285 unsigned int elt_size = val2->v.val_vec.elt_size;
29286 unsigned int len = val2->v.val_vec.length;
29288 hstate.add_object (elt_size);
29289 hstate.add_object (len);
29290 hstate.add (val2->v.val_vec.array, len * elt_size);
29292 break;
29293 case dw_val_class_const_double:
29294 hstate.add_object (val2->v.val_double.low);
29295 hstate.add_object (val2->v.val_double.high);
29296 break;
29297 case dw_val_class_wide_int:
29298 hstate.add (val2->v.val_wide->get_val (),
29299 get_full_len (*val2->v.val_wide)
29300 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29301 break;
29302 default:
29303 gcc_unreachable ();
29306 break;
29308 default:
29309 /* Other codes have no operands. */
29310 break;
29314 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29316 static inline void
29317 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29319 dw_loc_descr_ref l;
29320 bool sizes_computed = false;
29321 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29322 size_of_locs (loc);
29324 for (l = loc; l != NULL; l = l->dw_loc_next)
29326 enum dwarf_location_atom opc = l->dw_loc_opc;
29327 hstate.add_object (opc);
29328 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29330 size_of_locs (loc);
29331 sizes_computed = true;
29333 hash_loc_operands (l, hstate);
29337 /* Compute hash of the whole location list LIST_HEAD. */
29339 static inline void
29340 hash_loc_list (dw_loc_list_ref list_head)
29342 dw_loc_list_ref curr = list_head;
29343 inchash::hash hstate;
29345 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29347 hstate.add (curr->begin, strlen (curr->begin) + 1);
29348 hstate.add (curr->end, strlen (curr->end) + 1);
29349 if (curr->section)
29350 hstate.add (curr->section, strlen (curr->section) + 1);
29351 hash_locs (curr->expr, hstate);
29353 list_head->hash = hstate.end ();
29356 /* Return true if X and Y opcodes have the same operands. */
29358 static inline bool
29359 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29361 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29362 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29363 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29364 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29366 switch (x->dw_loc_opc)
29368 case DW_OP_const4u:
29369 case DW_OP_const8u:
29370 if (x->dtprel)
29371 goto hash_addr;
29372 /* FALLTHRU */
29373 case DW_OP_const1u:
29374 case DW_OP_const1s:
29375 case DW_OP_const2u:
29376 case DW_OP_const2s:
29377 case DW_OP_const4s:
29378 case DW_OP_const8s:
29379 case DW_OP_constu:
29380 case DW_OP_consts:
29381 case DW_OP_pick:
29382 case DW_OP_plus_uconst:
29383 case DW_OP_breg0:
29384 case DW_OP_breg1:
29385 case DW_OP_breg2:
29386 case DW_OP_breg3:
29387 case DW_OP_breg4:
29388 case DW_OP_breg5:
29389 case DW_OP_breg6:
29390 case DW_OP_breg7:
29391 case DW_OP_breg8:
29392 case DW_OP_breg9:
29393 case DW_OP_breg10:
29394 case DW_OP_breg11:
29395 case DW_OP_breg12:
29396 case DW_OP_breg13:
29397 case DW_OP_breg14:
29398 case DW_OP_breg15:
29399 case DW_OP_breg16:
29400 case DW_OP_breg17:
29401 case DW_OP_breg18:
29402 case DW_OP_breg19:
29403 case DW_OP_breg20:
29404 case DW_OP_breg21:
29405 case DW_OP_breg22:
29406 case DW_OP_breg23:
29407 case DW_OP_breg24:
29408 case DW_OP_breg25:
29409 case DW_OP_breg26:
29410 case DW_OP_breg27:
29411 case DW_OP_breg28:
29412 case DW_OP_breg29:
29413 case DW_OP_breg30:
29414 case DW_OP_breg31:
29415 case DW_OP_regx:
29416 case DW_OP_fbreg:
29417 case DW_OP_piece:
29418 case DW_OP_deref_size:
29419 case DW_OP_xderef_size:
29420 return valx1->v.val_int == valy1->v.val_int;
29421 case DW_OP_skip:
29422 case DW_OP_bra:
29423 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29424 can cause irrelevant differences in dw_loc_addr. */
29425 gcc_assert (valx1->val_class == dw_val_class_loc
29426 && valy1->val_class == dw_val_class_loc
29427 && (dwarf_split_debug_info
29428 || x->dw_loc_addr == y->dw_loc_addr));
29429 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29430 case DW_OP_implicit_value:
29431 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29432 || valx2->val_class != valy2->val_class)
29433 return false;
29434 switch (valx2->val_class)
29436 case dw_val_class_const:
29437 return valx2->v.val_int == valy2->v.val_int;
29438 case dw_val_class_vec:
29439 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29440 && valx2->v.val_vec.length == valy2->v.val_vec.length
29441 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29442 valx2->v.val_vec.elt_size
29443 * valx2->v.val_vec.length) == 0;
29444 case dw_val_class_const_double:
29445 return valx2->v.val_double.low == valy2->v.val_double.low
29446 && valx2->v.val_double.high == valy2->v.val_double.high;
29447 case dw_val_class_wide_int:
29448 return *valx2->v.val_wide == *valy2->v.val_wide;
29449 case dw_val_class_addr:
29450 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29451 default:
29452 gcc_unreachable ();
29454 case DW_OP_bregx:
29455 case DW_OP_bit_piece:
29456 return valx1->v.val_int == valy1->v.val_int
29457 && valx2->v.val_int == valy2->v.val_int;
29458 case DW_OP_addr:
29459 hash_addr:
29460 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29461 case DW_OP_GNU_addr_index:
29462 case DW_OP_GNU_const_index:
29464 rtx ax1 = valx1->val_entry->addr.rtl;
29465 rtx ay1 = valy1->val_entry->addr.rtl;
29466 return rtx_equal_p (ax1, ay1);
29468 case DW_OP_implicit_pointer:
29469 case DW_OP_GNU_implicit_pointer:
29470 return valx1->val_class == dw_val_class_die_ref
29471 && valx1->val_class == valy1->val_class
29472 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29473 && valx2->v.val_int == valy2->v.val_int;
29474 case DW_OP_entry_value:
29475 case DW_OP_GNU_entry_value:
29476 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29477 case DW_OP_const_type:
29478 case DW_OP_GNU_const_type:
29479 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29480 || valx2->val_class != valy2->val_class)
29481 return false;
29482 switch (valx2->val_class)
29484 case dw_val_class_const:
29485 return valx2->v.val_int == valy2->v.val_int;
29486 case dw_val_class_vec:
29487 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29488 && valx2->v.val_vec.length == valy2->v.val_vec.length
29489 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29490 valx2->v.val_vec.elt_size
29491 * valx2->v.val_vec.length) == 0;
29492 case dw_val_class_const_double:
29493 return valx2->v.val_double.low == valy2->v.val_double.low
29494 && valx2->v.val_double.high == valy2->v.val_double.high;
29495 case dw_val_class_wide_int:
29496 return *valx2->v.val_wide == *valy2->v.val_wide;
29497 default:
29498 gcc_unreachable ();
29500 case DW_OP_regval_type:
29501 case DW_OP_deref_type:
29502 case DW_OP_GNU_regval_type:
29503 case DW_OP_GNU_deref_type:
29504 return valx1->v.val_int == valy1->v.val_int
29505 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29506 case DW_OP_convert:
29507 case DW_OP_reinterpret:
29508 case DW_OP_GNU_convert:
29509 case DW_OP_GNU_reinterpret:
29510 if (valx1->val_class != valy1->val_class)
29511 return false;
29512 if (valx1->val_class == dw_val_class_unsigned_const)
29513 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29514 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29515 case DW_OP_GNU_parameter_ref:
29516 return valx1->val_class == dw_val_class_die_ref
29517 && valx1->val_class == valy1->val_class
29518 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29519 default:
29520 /* Other codes have no operands. */
29521 return true;
29525 /* Return true if DWARF location expressions X and Y are the same. */
29527 static inline bool
29528 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29530 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29531 if (x->dw_loc_opc != y->dw_loc_opc
29532 || x->dtprel != y->dtprel
29533 || !compare_loc_operands (x, y))
29534 break;
29535 return x == NULL && y == NULL;
29538 /* Hashtable helpers. */
29540 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29542 static inline hashval_t hash (const dw_loc_list_struct *);
29543 static inline bool equal (const dw_loc_list_struct *,
29544 const dw_loc_list_struct *);
29547 /* Return precomputed hash of location list X. */
29549 inline hashval_t
29550 loc_list_hasher::hash (const dw_loc_list_struct *x)
29552 return x->hash;
29555 /* Return true if location lists A and B are the same. */
29557 inline bool
29558 loc_list_hasher::equal (const dw_loc_list_struct *a,
29559 const dw_loc_list_struct *b)
29561 if (a == b)
29562 return 1;
29563 if (a->hash != b->hash)
29564 return 0;
29565 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29566 if (strcmp (a->begin, b->begin) != 0
29567 || strcmp (a->end, b->end) != 0
29568 || (a->section == NULL) != (b->section == NULL)
29569 || (a->section && strcmp (a->section, b->section) != 0)
29570 || !compare_locs (a->expr, b->expr))
29571 break;
29572 return a == NULL && b == NULL;
29575 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29578 /* Recursively optimize location lists referenced from DIE
29579 children and share them whenever possible. */
29581 static void
29582 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29584 dw_die_ref c;
29585 dw_attr_node *a;
29586 unsigned ix;
29587 dw_loc_list_struct **slot;
29589 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29590 if (AT_class (a) == dw_val_class_loc_list)
29592 dw_loc_list_ref list = AT_loc_list (a);
29593 /* TODO: perform some optimizations here, before hashing
29594 it and storing into the hash table. */
29595 hash_loc_list (list);
29596 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29597 if (*slot == NULL)
29598 *slot = list;
29599 else
29600 a->dw_attr_val.v.val_loc_list = *slot;
29603 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29607 /* Recursively assign each location list a unique index into the debug_addr
29608 section. */
29610 static void
29611 index_location_lists (dw_die_ref die)
29613 dw_die_ref c;
29614 dw_attr_node *a;
29615 unsigned ix;
29617 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29618 if (AT_class (a) == dw_val_class_loc_list)
29620 dw_loc_list_ref list = AT_loc_list (a);
29621 dw_loc_list_ref curr;
29622 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29624 /* Don't index an entry that has already been indexed
29625 or won't be output. */
29626 if (curr->begin_entry != NULL
29627 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29628 continue;
29630 curr->begin_entry
29631 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29635 FOR_EACH_CHILD (die, c, index_location_lists (c));
29638 /* Optimize location lists referenced from DIE
29639 children and share them whenever possible. */
29641 static void
29642 optimize_location_lists (dw_die_ref die)
29644 loc_list_hash_type htab (500);
29645 optimize_location_lists_1 (die, &htab);
29648 /* Traverse the limbo die list, and add parent/child links. The only
29649 dies without parents that should be here are concrete instances of
29650 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29651 For concrete instances, we can get the parent die from the abstract
29652 instance. */
29654 static void
29655 flush_limbo_die_list (void)
29657 limbo_die_node *node;
29659 /* get_context_die calls force_decl_die, which can put new DIEs on the
29660 limbo list in LTO mode when nested functions are put in a different
29661 partition than that of their parent function. */
29662 while ((node = limbo_die_list))
29664 dw_die_ref die = node->die;
29665 limbo_die_list = node->next;
29667 if (die->die_parent == NULL)
29669 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29671 if (origin && origin->die_parent)
29672 add_child_die (origin->die_parent, die);
29673 else if (is_cu_die (die))
29675 else if (seen_error ())
29676 /* It's OK to be confused by errors in the input. */
29677 add_child_die (comp_unit_die (), die);
29678 else
29680 /* In certain situations, the lexical block containing a
29681 nested function can be optimized away, which results
29682 in the nested function die being orphaned. Likewise
29683 with the return type of that nested function. Force
29684 this to be a child of the containing function.
29686 It may happen that even the containing function got fully
29687 inlined and optimized out. In that case we are lost and
29688 assign the empty child. This should not be big issue as
29689 the function is likely unreachable too. */
29690 gcc_assert (node->created_for);
29692 if (DECL_P (node->created_for))
29693 origin = get_context_die (DECL_CONTEXT (node->created_for));
29694 else if (TYPE_P (node->created_for))
29695 origin = scope_die_for (node->created_for, comp_unit_die ());
29696 else
29697 origin = comp_unit_die ();
29699 add_child_die (origin, die);
29705 /* Reset DIEs so we can output them again. */
29707 static void
29708 reset_dies (dw_die_ref die)
29710 dw_die_ref c;
29712 /* Remove stuff we re-generate. */
29713 die->die_mark = 0;
29714 die->die_offset = 0;
29715 die->die_abbrev = 0;
29716 remove_AT (die, DW_AT_sibling);
29718 FOR_EACH_CHILD (die, c, reset_dies (c));
29721 /* Output stuff that dwarf requires at the end of every file,
29722 and generate the DWARF-2 debugging info. */
29724 static void
29725 dwarf2out_finish (const char *)
29727 comdat_type_node *ctnode;
29728 dw_die_ref main_comp_unit_die;
29729 unsigned char checksum[16];
29730 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29732 /* Flush out any latecomers to the limbo party. */
29733 flush_limbo_die_list ();
29735 if (flag_checking)
29737 verify_die (comp_unit_die ());
29738 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29739 verify_die (node->die);
29742 /* We shouldn't have any symbols with delayed asm names for
29743 DIEs generated after early finish. */
29744 gcc_assert (deferred_asm_name == NULL);
29746 gen_remaining_tmpl_value_param_die_attribute ();
29748 if (flag_generate_lto || flag_generate_offload)
29750 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29752 /* Prune stuff so that dwarf2out_finish runs successfully
29753 for the fat part of the object. */
29754 reset_dies (comp_unit_die ());
29755 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29756 reset_dies (node->die);
29758 hash_table<comdat_type_hasher> comdat_type_table (100);
29759 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29761 comdat_type_node **slot
29762 = comdat_type_table.find_slot (ctnode, INSERT);
29764 /* Don't reset types twice. */
29765 if (*slot != HTAB_EMPTY_ENTRY)
29766 continue;
29768 /* Add a pointer to the line table for the main compilation unit
29769 so that the debugger can make sense of DW_AT_decl_file
29770 attributes. */
29771 if (debug_info_level >= DINFO_LEVEL_TERSE)
29772 reset_dies (ctnode->root_die);
29774 *slot = ctnode;
29777 /* Reset die CU symbol so we don't output it twice. */
29778 comp_unit_die ()->die_id.die_symbol = NULL;
29780 /* Remove DW_AT_macro from the early output. */
29781 if (have_macinfo)
29782 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29784 /* Remove indirect string decisions. */
29785 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29788 #if ENABLE_ASSERT_CHECKING
29790 dw_die_ref die = comp_unit_die (), c;
29791 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29793 #endif
29794 resolve_addr (comp_unit_die ());
29795 move_marked_base_types ();
29797 /* Initialize sections and labels used for actual assembler output. */
29798 init_sections_and_labels (false);
29800 /* Traverse the DIE's and add sibling attributes to those DIE's that
29801 have children. */
29802 add_sibling_attributes (comp_unit_die ());
29803 limbo_die_node *node;
29804 for (node = cu_die_list; node; node = node->next)
29805 add_sibling_attributes (node->die);
29806 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29807 add_sibling_attributes (ctnode->root_die);
29809 /* When splitting DWARF info, we put some attributes in the
29810 skeleton compile_unit DIE that remains in the .o, while
29811 most attributes go in the DWO compile_unit_die. */
29812 if (dwarf_split_debug_info)
29814 limbo_die_node *cu;
29815 main_comp_unit_die = gen_compile_unit_die (NULL);
29816 if (dwarf_version >= 5)
29817 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29818 cu = limbo_die_list;
29819 gcc_assert (cu->die == main_comp_unit_die);
29820 limbo_die_list = limbo_die_list->next;
29821 cu->next = cu_die_list;
29822 cu_die_list = cu;
29824 else
29825 main_comp_unit_die = comp_unit_die ();
29827 /* Output a terminator label for the .text section. */
29828 switch_to_section (text_section);
29829 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29830 if (cold_text_section)
29832 switch_to_section (cold_text_section);
29833 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29836 /* We can only use the low/high_pc attributes if all of the code was
29837 in .text. */
29838 if (!have_multiple_function_sections
29839 || (dwarf_version < 3 && dwarf_strict))
29841 /* Don't add if the CU has no associated code. */
29842 if (text_section_used)
29843 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29844 text_end_label, true);
29846 else
29848 unsigned fde_idx;
29849 dw_fde_ref fde;
29850 bool range_list_added = false;
29852 if (text_section_used)
29853 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29854 text_end_label, &range_list_added, true);
29855 if (cold_text_section_used)
29856 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29857 cold_end_label, &range_list_added, true);
29859 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29861 if (DECL_IGNORED_P (fde->decl))
29862 continue;
29863 if (!fde->in_std_section)
29864 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29865 fde->dw_fde_end, &range_list_added,
29866 true);
29867 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29868 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29869 fde->dw_fde_second_end, &range_list_added,
29870 true);
29873 if (range_list_added)
29875 /* We need to give .debug_loc and .debug_ranges an appropriate
29876 "base address". Use zero so that these addresses become
29877 absolute. Historically, we've emitted the unexpected
29878 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29879 Emit both to give time for other tools to adapt. */
29880 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29881 if (! dwarf_strict && dwarf_version < 4)
29882 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29884 add_ranges (NULL);
29888 /* AIX Assembler inserts the length, so adjust the reference to match the
29889 offset expected by debuggers. */
29890 strcpy (dl_section_ref, debug_line_section_label);
29891 if (XCOFF_DEBUGGING_INFO)
29892 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29894 if (debug_info_level >= DINFO_LEVEL_TERSE)
29895 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29896 dl_section_ref);
29898 if (have_macinfo)
29899 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
29900 macinfo_section_label);
29902 if (dwarf_split_debug_info)
29904 if (have_location_lists)
29906 if (dwarf_version >= 5)
29907 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29908 loc_section_label);
29909 /* optimize_location_lists calculates the size of the lists,
29910 so index them first, and assign indices to the entries.
29911 Although optimize_location_lists will remove entries from
29912 the table, it only does so for duplicates, and therefore
29913 only reduces ref_counts to 1. */
29914 index_location_lists (comp_unit_die ());
29917 if (addr_index_table != NULL)
29919 unsigned int index = 0;
29920 addr_index_table
29921 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29922 (&index);
29926 loc_list_idx = 0;
29927 if (have_location_lists)
29929 optimize_location_lists (comp_unit_die ());
29930 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29931 if (dwarf_version >= 5 && dwarf_split_debug_info)
29932 assign_location_list_indexes (comp_unit_die ());
29935 save_macinfo_strings ();
29937 if (dwarf_split_debug_info)
29939 unsigned int index = 0;
29941 /* Add attributes common to skeleton compile_units and
29942 type_units. Because these attributes include strings, it
29943 must be done before freezing the string table. Top-level
29944 skeleton die attrs are added when the skeleton type unit is
29945 created, so ensure it is created by this point. */
29946 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29947 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29950 /* Output all of the compilation units. We put the main one last so that
29951 the offsets are available to output_pubnames. */
29952 for (node = cu_die_list; node; node = node->next)
29953 output_comp_unit (node->die, 0, NULL);
29955 hash_table<comdat_type_hasher> comdat_type_table (100);
29956 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29958 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29960 /* Don't output duplicate types. */
29961 if (*slot != HTAB_EMPTY_ENTRY)
29962 continue;
29964 /* Add a pointer to the line table for the main compilation unit
29965 so that the debugger can make sense of DW_AT_decl_file
29966 attributes. */
29967 if (debug_info_level >= DINFO_LEVEL_TERSE)
29968 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29969 (!dwarf_split_debug_info
29970 ? dl_section_ref
29971 : debug_skeleton_line_section_label));
29973 output_comdat_type_unit (ctnode);
29974 *slot = ctnode;
29977 if (dwarf_split_debug_info)
29979 int mark;
29980 struct md5_ctx ctx;
29982 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29983 index_rnglists ();
29985 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29986 md5_init_ctx (&ctx);
29987 mark = 0;
29988 die_checksum (comp_unit_die (), &ctx, &mark);
29989 unmark_all_dies (comp_unit_die ());
29990 md5_finish_ctx (&ctx, checksum);
29992 if (dwarf_version < 5)
29994 /* Use the first 8 bytes of the checksum as the dwo_id,
29995 and add it to both comp-unit DIEs. */
29996 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29997 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30000 /* Add the base offset of the ranges table to the skeleton
30001 comp-unit DIE. */
30002 if (!vec_safe_is_empty (ranges_table))
30004 if (dwarf_version >= 5)
30005 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30006 ranges_base_label);
30007 else
30008 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30009 ranges_section_label);
30012 switch_to_section (debug_addr_section);
30013 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30014 output_addr_table ();
30017 /* Output the main compilation unit if non-empty or if .debug_macinfo
30018 or .debug_macro will be emitted. */
30019 output_comp_unit (comp_unit_die (), have_macinfo,
30020 dwarf_split_debug_info ? checksum : NULL);
30022 if (dwarf_split_debug_info && info_section_emitted)
30023 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30025 /* Output the abbreviation table. */
30026 if (vec_safe_length (abbrev_die_table) != 1)
30028 switch_to_section (debug_abbrev_section);
30029 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30030 output_abbrev_section ();
30033 /* Output location list section if necessary. */
30034 if (have_location_lists)
30036 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30037 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30038 /* Output the location lists info. */
30039 switch_to_section (debug_loc_section);
30040 if (dwarf_version >= 5)
30042 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30043 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30044 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30045 dw2_asm_output_data (4, 0xffffffff,
30046 "Initial length escape value indicating "
30047 "64-bit DWARF extension");
30048 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30049 "Length of Location Lists");
30050 ASM_OUTPUT_LABEL (asm_out_file, l1);
30051 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30052 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30053 dw2_asm_output_data (1, 0, "Segment Size");
30054 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30055 "Offset Entry Count");
30057 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30058 if (dwarf_version >= 5 && dwarf_split_debug_info)
30060 unsigned int save_loc_list_idx = loc_list_idx;
30061 loc_list_idx = 0;
30062 output_loclists_offsets (comp_unit_die ());
30063 gcc_assert (save_loc_list_idx == loc_list_idx);
30065 output_location_lists (comp_unit_die ());
30066 if (dwarf_version >= 5)
30067 ASM_OUTPUT_LABEL (asm_out_file, l2);
30070 output_pubtables ();
30072 /* Output the address range information if a CU (.debug_info section)
30073 was emitted. We output an empty table even if we had no functions
30074 to put in it. This because the consumer has no way to tell the
30075 difference between an empty table that we omitted and failure to
30076 generate a table that would have contained data. */
30077 if (info_section_emitted)
30079 switch_to_section (debug_aranges_section);
30080 output_aranges ();
30083 /* Output ranges section if necessary. */
30084 if (!vec_safe_is_empty (ranges_table))
30086 if (dwarf_version >= 5)
30087 output_rnglists ();
30088 else
30089 output_ranges ();
30092 /* Have to end the macro section. */
30093 if (have_macinfo)
30095 switch_to_section (debug_macinfo_section);
30096 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30097 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30098 : debug_skeleton_line_section_label, false);
30099 dw2_asm_output_data (1, 0, "End compilation unit");
30102 /* Output the source line correspondence table. We must do this
30103 even if there is no line information. Otherwise, on an empty
30104 translation unit, we will generate a present, but empty,
30105 .debug_info section. IRIX 6.5 `nm' will then complain when
30106 examining the file. This is done late so that any filenames
30107 used by the debug_info section are marked as 'used'. */
30108 switch_to_section (debug_line_section);
30109 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30110 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30111 output_line_info (false);
30113 if (dwarf_split_debug_info && info_section_emitted)
30115 switch_to_section (debug_skeleton_line_section);
30116 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30117 output_line_info (true);
30120 /* If we emitted any indirect strings, output the string table too. */
30121 if (debug_str_hash || skeleton_debug_str_hash)
30122 output_indirect_strings ();
30123 if (debug_line_str_hash)
30125 switch_to_section (debug_line_str_section);
30126 const enum dwarf_form form = DW_FORM_line_strp;
30127 debug_line_str_hash->traverse<enum dwarf_form,
30128 output_indirect_string> (form);
30132 /* Returns a hash value for X (which really is a variable_value_struct). */
30134 inline hashval_t
30135 variable_value_hasher::hash (variable_value_struct *x)
30137 return (hashval_t) x->decl_id;
30140 /* Return nonzero if decl_id of variable_value_struct X is the same as
30141 UID of decl Y. */
30143 inline bool
30144 variable_value_hasher::equal (variable_value_struct *x, tree y)
30146 return x->decl_id == DECL_UID (y);
30149 /* Helper function for resolve_variable_value, handle
30150 DW_OP_GNU_variable_value in one location expression.
30151 Return true if exprloc has been changed into loclist. */
30153 static bool
30154 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30156 dw_loc_descr_ref next;
30157 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30159 next = loc->dw_loc_next;
30160 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30161 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30162 continue;
30164 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30165 if (DECL_CONTEXT (decl) != current_function_decl)
30166 continue;
30168 dw_die_ref ref = lookup_decl_die (decl);
30169 if (ref)
30171 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30172 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30173 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30174 continue;
30176 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30177 if (l == NULL)
30178 continue;
30179 if (l->dw_loc_next)
30181 if (AT_class (a) != dw_val_class_loc)
30182 continue;
30183 switch (a->dw_attr)
30185 /* Following attributes allow both exprloc and loclist
30186 classes, so we can change them into a loclist. */
30187 case DW_AT_location:
30188 case DW_AT_string_length:
30189 case DW_AT_return_addr:
30190 case DW_AT_data_member_location:
30191 case DW_AT_frame_base:
30192 case DW_AT_segment:
30193 case DW_AT_static_link:
30194 case DW_AT_use_location:
30195 case DW_AT_vtable_elem_location:
30196 if (prev)
30198 prev->dw_loc_next = NULL;
30199 prepend_loc_descr_to_each (l, AT_loc (a));
30201 if (next)
30202 add_loc_descr_to_each (l, next);
30203 a->dw_attr_val.val_class = dw_val_class_loc_list;
30204 a->dw_attr_val.val_entry = NULL;
30205 a->dw_attr_val.v.val_loc_list = l;
30206 have_location_lists = true;
30207 return true;
30208 /* Following attributes allow both exprloc and reference,
30209 so if the whole expression is DW_OP_GNU_variable_value alone
30210 we could transform it into reference. */
30211 case DW_AT_byte_size:
30212 case DW_AT_bit_size:
30213 case DW_AT_lower_bound:
30214 case DW_AT_upper_bound:
30215 case DW_AT_bit_stride:
30216 case DW_AT_count:
30217 case DW_AT_allocated:
30218 case DW_AT_associated:
30219 case DW_AT_byte_stride:
30220 if (prev == NULL && next == NULL)
30221 break;
30222 /* FALLTHRU */
30223 default:
30224 if (dwarf_strict)
30225 continue;
30226 break;
30228 /* Create DW_TAG_variable that we can refer to. */
30229 gen_decl_die (decl, NULL_TREE, NULL,
30230 lookup_decl_die (current_function_decl));
30231 ref = lookup_decl_die (decl);
30232 if (ref)
30234 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30235 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30236 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30238 continue;
30240 if (prev)
30242 prev->dw_loc_next = l->expr;
30243 add_loc_descr (&prev->dw_loc_next, next);
30244 free_loc_descr (loc, NULL);
30245 next = prev->dw_loc_next;
30247 else
30249 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30250 add_loc_descr (&loc, next);
30251 next = loc;
30253 loc = prev;
30255 return false;
30258 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30260 static void
30261 resolve_variable_value (dw_die_ref die)
30263 dw_attr_node *a;
30264 dw_loc_list_ref loc;
30265 unsigned ix;
30267 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30268 switch (AT_class (a))
30270 case dw_val_class_loc:
30271 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30272 break;
30273 /* FALLTHRU */
30274 case dw_val_class_loc_list:
30275 loc = AT_loc_list (a);
30276 gcc_assert (loc);
30277 for (; loc; loc = loc->dw_loc_next)
30278 resolve_variable_value_in_expr (a, loc->expr);
30279 break;
30280 default:
30281 break;
30285 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30286 temporaries in the current function. */
30288 static void
30289 resolve_variable_values (void)
30291 if (!variable_value_hash || !current_function_decl)
30292 return;
30294 struct variable_value_struct *node
30295 = variable_value_hash->find_with_hash (current_function_decl,
30296 DECL_UID (current_function_decl));
30298 if (node == NULL)
30299 return;
30301 unsigned int i;
30302 dw_die_ref die;
30303 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30304 resolve_variable_value (die);
30307 /* Helper function for note_variable_value, handle one location
30308 expression. */
30310 static void
30311 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30313 for (; loc; loc = loc->dw_loc_next)
30314 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30315 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30317 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30318 dw_die_ref ref = lookup_decl_die (decl);
30319 if (! ref && (flag_generate_lto || flag_generate_offload))
30321 /* ??? This is somewhat a hack because we do not create DIEs
30322 for variables not in BLOCK trees early but when generating
30323 early LTO output we need the dw_val_class_decl_ref to be
30324 fully resolved. For fat LTO objects we'd also like to
30325 undo this after LTO dwarf output. */
30326 gcc_assert (DECL_CONTEXT (decl));
30327 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30328 gcc_assert (ctx != NULL);
30329 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30330 ref = lookup_decl_die (decl);
30331 gcc_assert (ref != NULL);
30333 if (ref)
30335 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30336 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30337 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30338 continue;
30340 if (VAR_P (decl)
30341 && DECL_CONTEXT (decl)
30342 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30343 && lookup_decl_die (DECL_CONTEXT (decl)))
30345 if (!variable_value_hash)
30346 variable_value_hash
30347 = hash_table<variable_value_hasher>::create_ggc (10);
30349 tree fndecl = DECL_CONTEXT (decl);
30350 struct variable_value_struct *node;
30351 struct variable_value_struct **slot
30352 = variable_value_hash->find_slot_with_hash (fndecl,
30353 DECL_UID (fndecl),
30354 INSERT);
30355 if (*slot == NULL)
30357 node = ggc_cleared_alloc<variable_value_struct> ();
30358 node->decl_id = DECL_UID (fndecl);
30359 *slot = node;
30361 else
30362 node = *slot;
30364 vec_safe_push (node->dies, die);
30369 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30370 with dw_val_class_decl_ref operand. */
30372 static void
30373 note_variable_value (dw_die_ref die)
30375 dw_die_ref c;
30376 dw_attr_node *a;
30377 dw_loc_list_ref loc;
30378 unsigned ix;
30380 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30381 switch (AT_class (a))
30383 case dw_val_class_loc_list:
30384 loc = AT_loc_list (a);
30385 gcc_assert (loc);
30386 if (!loc->noted_variable_value)
30388 loc->noted_variable_value = 1;
30389 for (; loc; loc = loc->dw_loc_next)
30390 note_variable_value_in_expr (die, loc->expr);
30392 break;
30393 case dw_val_class_loc:
30394 note_variable_value_in_expr (die, AT_loc (a));
30395 break;
30396 default:
30397 break;
30400 /* Mark children. */
30401 FOR_EACH_CHILD (die, c, note_variable_value (c));
30404 /* Perform any cleanups needed after the early debug generation pass
30405 has run. */
30407 static void
30408 dwarf2out_early_finish (const char *filename)
30410 set_early_dwarf s;
30412 /* PCH might result in DW_AT_producer string being restored from the
30413 header compilation, so always fill it with empty string initially
30414 and overwrite only here. */
30415 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30416 producer_string = gen_producer_string ();
30417 producer->dw_attr_val.v.val_str->refcount--;
30418 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30420 /* Add the name for the main input file now. We delayed this from
30421 dwarf2out_init to avoid complications with PCH. */
30422 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30423 add_comp_dir_attribute (comp_unit_die ());
30425 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30426 DW_AT_comp_dir into .debug_line_str section. */
30427 if (!DWARF2_ASM_LINE_DEBUG_INFO
30428 && dwarf_version >= 5
30429 && DWARF5_USE_DEBUG_LINE_STR)
30431 for (int i = 0; i < 2; i++)
30433 dw_attr_node *a = get_AT (comp_unit_die (),
30434 i ? DW_AT_comp_dir : DW_AT_name);
30435 if (a == NULL
30436 || AT_class (a) != dw_val_class_str
30437 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30438 continue;
30440 if (! debug_line_str_hash)
30441 debug_line_str_hash
30442 = hash_table<indirect_string_hasher>::create_ggc (10);
30444 struct indirect_string_node *node
30445 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30446 set_indirect_string (node);
30447 node->form = DW_FORM_line_strp;
30448 a->dw_attr_val.v.val_str->refcount--;
30449 a->dw_attr_val.v.val_str = node;
30453 /* With LTO early dwarf was really finished at compile-time, so make
30454 sure to adjust the phase after annotating the LTRANS CU DIE. */
30455 if (in_lto_p)
30457 early_dwarf_finished = true;
30458 return;
30461 /* Walk through the list of incomplete types again, trying once more to
30462 emit full debugging info for them. */
30463 retry_incomplete_types ();
30465 /* The point here is to flush out the limbo list so that it is empty
30466 and we don't need to stream it for LTO. */
30467 flush_limbo_die_list ();
30469 gen_scheduled_generic_parms_dies ();
30470 gen_remaining_tmpl_value_param_die_attribute ();
30472 /* Add DW_AT_linkage_name for all deferred DIEs. */
30473 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30475 tree decl = node->created_for;
30476 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30477 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30478 ended up in deferred_asm_name before we knew it was
30479 constant and never written to disk. */
30480 && DECL_ASSEMBLER_NAME (decl))
30482 add_linkage_attr (node->die, decl);
30483 move_linkage_attr (node->die);
30486 deferred_asm_name = NULL;
30488 if (flag_eliminate_unused_debug_types)
30489 prune_unused_types ();
30491 /* Generate separate COMDAT sections for type DIEs. */
30492 if (use_debug_types)
30494 break_out_comdat_types (comp_unit_die ());
30496 /* Each new type_unit DIE was added to the limbo die list when created.
30497 Since these have all been added to comdat_type_list, clear the
30498 limbo die list. */
30499 limbo_die_list = NULL;
30501 /* For each new comdat type unit, copy declarations for incomplete
30502 types to make the new unit self-contained (i.e., no direct
30503 references to the main compile unit). */
30504 for (comdat_type_node *ctnode = comdat_type_list;
30505 ctnode != NULL; ctnode = ctnode->next)
30506 copy_decls_for_unworthy_types (ctnode->root_die);
30507 copy_decls_for_unworthy_types (comp_unit_die ());
30509 /* In the process of copying declarations from one unit to another,
30510 we may have left some declarations behind that are no longer
30511 referenced. Prune them. */
30512 prune_unused_types ();
30515 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30516 with dw_val_class_decl_ref operand. */
30517 note_variable_value (comp_unit_die ());
30518 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30519 note_variable_value (node->die);
30520 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30521 ctnode = ctnode->next)
30522 note_variable_value (ctnode->root_die);
30523 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30524 note_variable_value (node->die);
30526 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30527 both the main_cu and all skeleton TUs. Making this call unconditional
30528 would end up either adding a second copy of the AT_pubnames attribute, or
30529 requiring a special case in add_top_level_skeleton_die_attrs. */
30530 if (!dwarf_split_debug_info)
30531 add_AT_pubnames (comp_unit_die ());
30533 /* The early debug phase is now finished. */
30534 early_dwarf_finished = true;
30536 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30537 if (!flag_generate_lto && !flag_generate_offload)
30538 return;
30540 /* Now as we are going to output for LTO initialize sections and labels
30541 to the LTO variants. We don't need a random-seed postfix as other
30542 LTO sections as linking the LTO debug sections into one in a partial
30543 link is fine. */
30544 init_sections_and_labels (true);
30546 /* The output below is modeled after dwarf2out_finish with all
30547 location related output removed and some LTO specific changes.
30548 Some refactoring might make both smaller and easier to match up. */
30550 /* Traverse the DIE's and add add sibling attributes to those DIE's
30551 that have children. */
30552 add_sibling_attributes (comp_unit_die ());
30553 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30554 add_sibling_attributes (node->die);
30555 for (comdat_type_node *ctnode = comdat_type_list;
30556 ctnode != NULL; ctnode = ctnode->next)
30557 add_sibling_attributes (ctnode->root_die);
30559 if (have_macinfo)
30560 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30561 macinfo_section_label);
30563 save_macinfo_strings ();
30565 /* Output all of the compilation units. We put the main one last so that
30566 the offsets are available to output_pubnames. */
30567 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30568 output_comp_unit (node->die, 0, NULL);
30570 hash_table<comdat_type_hasher> comdat_type_table (100);
30571 for (comdat_type_node *ctnode = comdat_type_list;
30572 ctnode != NULL; ctnode = ctnode->next)
30574 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30576 /* Don't output duplicate types. */
30577 if (*slot != HTAB_EMPTY_ENTRY)
30578 continue;
30580 /* Add a pointer to the line table for the main compilation unit
30581 so that the debugger can make sense of DW_AT_decl_file
30582 attributes. */
30583 if (debug_info_level >= DINFO_LEVEL_TERSE)
30584 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30585 (!dwarf_split_debug_info
30586 ? debug_line_section_label
30587 : debug_skeleton_line_section_label));
30589 output_comdat_type_unit (ctnode);
30590 *slot = ctnode;
30593 /* Stick a unique symbol to the main debuginfo section. */
30594 compute_comp_unit_symbol (comp_unit_die ());
30596 /* Output the main compilation unit. We always need it if only for
30597 the CU symbol. */
30598 output_comp_unit (comp_unit_die (), true, NULL);
30600 /* Output the abbreviation table. */
30601 if (vec_safe_length (abbrev_die_table) != 1)
30603 switch_to_section (debug_abbrev_section);
30604 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30605 output_abbrev_section ();
30608 /* Have to end the macro section. */
30609 if (have_macinfo)
30611 /* We have to save macinfo state if we need to output it again
30612 for the FAT part of the object. */
30613 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30614 if (flag_fat_lto_objects)
30615 macinfo_table = macinfo_table->copy ();
30617 switch_to_section (debug_macinfo_section);
30618 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30619 output_macinfo (debug_skeleton_line_section_label, true);
30620 dw2_asm_output_data (1, 0, "End compilation unit");
30622 /* Emit a skeleton debug_line section. */
30623 switch_to_section (debug_skeleton_line_section);
30624 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30625 output_line_info (true);
30627 if (flag_fat_lto_objects)
30629 vec_free (macinfo_table);
30630 macinfo_table = saved_macinfo_table;
30635 /* If we emitted any indirect strings, output the string table too. */
30636 if (debug_str_hash || skeleton_debug_str_hash)
30637 output_indirect_strings ();
30639 /* Switch back to the text section. */
30640 switch_to_section (text_section);
30643 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30644 within the same process. For use by toplev::finalize. */
30646 void
30647 dwarf2out_c_finalize (void)
30649 last_var_location_insn = NULL;
30650 cached_next_real_insn = NULL;
30651 used_rtx_array = NULL;
30652 incomplete_types = NULL;
30653 decl_scope_table = NULL;
30654 debug_info_section = NULL;
30655 debug_skeleton_info_section = NULL;
30656 debug_abbrev_section = NULL;
30657 debug_skeleton_abbrev_section = NULL;
30658 debug_aranges_section = NULL;
30659 debug_addr_section = NULL;
30660 debug_macinfo_section = NULL;
30661 debug_line_section = NULL;
30662 debug_skeleton_line_section = NULL;
30663 debug_loc_section = NULL;
30664 debug_pubnames_section = NULL;
30665 debug_pubtypes_section = NULL;
30666 debug_str_section = NULL;
30667 debug_line_str_section = NULL;
30668 debug_str_dwo_section = NULL;
30669 debug_str_offsets_section = NULL;
30670 debug_ranges_section = NULL;
30671 debug_frame_section = NULL;
30672 fde_vec = NULL;
30673 debug_str_hash = NULL;
30674 debug_line_str_hash = NULL;
30675 skeleton_debug_str_hash = NULL;
30676 dw2_string_counter = 0;
30677 have_multiple_function_sections = false;
30678 text_section_used = false;
30679 cold_text_section_used = false;
30680 cold_text_section = NULL;
30681 current_unit_personality = NULL;
30683 early_dwarf = false;
30684 early_dwarf_finished = false;
30686 next_die_offset = 0;
30687 single_comp_unit_die = NULL;
30688 comdat_type_list = NULL;
30689 limbo_die_list = NULL;
30690 file_table = NULL;
30691 decl_die_table = NULL;
30692 common_block_die_table = NULL;
30693 decl_loc_table = NULL;
30694 call_arg_locations = NULL;
30695 call_arg_loc_last = NULL;
30696 call_site_count = -1;
30697 tail_call_site_count = -1;
30698 cached_dw_loc_list_table = NULL;
30699 abbrev_die_table = NULL;
30700 delete dwarf_proc_stack_usage_map;
30701 dwarf_proc_stack_usage_map = NULL;
30702 line_info_label_num = 0;
30703 cur_line_info_table = NULL;
30704 text_section_line_info = NULL;
30705 cold_text_section_line_info = NULL;
30706 separate_line_info = NULL;
30707 info_section_emitted = false;
30708 pubname_table = NULL;
30709 pubtype_table = NULL;
30710 macinfo_table = NULL;
30711 ranges_table = NULL;
30712 ranges_by_label = NULL;
30713 rnglist_idx = 0;
30714 have_location_lists = false;
30715 loclabel_num = 0;
30716 poc_label_num = 0;
30717 last_emitted_file = NULL;
30718 label_num = 0;
30719 tmpl_value_parm_die_table = NULL;
30720 generic_type_instances = NULL;
30721 frame_pointer_fb_offset = 0;
30722 frame_pointer_fb_offset_valid = false;
30723 base_types.release ();
30724 XDELETEVEC (producer_string);
30725 producer_string = NULL;
30728 #include "gt-dwarf2out.h"