* cfghooks.c (verify_flow_info): Disable check that all probabilities
[official-gcc.git] / gcc / dwarf2out.c
blobbeab5e4ce8e881d31e541402a40ae75c8a76a491
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_INFO_SECTION
3706 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3707 #endif
3708 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3709 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3710 #endif
3711 #ifndef DEBUG_ABBREV_SECTION
3712 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3713 #endif
3714 #ifndef DEBUG_LTO_ABBREV_SECTION
3715 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3716 #endif
3717 #ifndef DEBUG_DWO_ABBREV_SECTION
3718 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3719 #endif
3720 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3721 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
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_LTO_MACINFO_SECTION
3733 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3734 #endif
3735 #ifndef DEBUG_DWO_MACINFO_SECTION
3736 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3737 #endif
3738 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3739 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3740 #endif
3741 #ifndef DEBUG_MACRO_SECTION
3742 #define DEBUG_MACRO_SECTION ".debug_macro"
3743 #endif
3744 #ifndef DEBUG_LTO_MACRO_SECTION
3745 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3746 #endif
3747 #ifndef DEBUG_DWO_MACRO_SECTION
3748 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3749 #endif
3750 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3751 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3752 #endif
3753 #ifndef DEBUG_LINE_SECTION
3754 #define DEBUG_LINE_SECTION ".debug_line"
3755 #endif
3756 #ifndef DEBUG_LTO_LINE_SECTION
3757 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3758 #endif
3759 #ifndef DEBUG_DWO_LINE_SECTION
3760 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3761 #endif
3762 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3763 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3764 #endif
3765 #ifndef DEBUG_LOC_SECTION
3766 #define DEBUG_LOC_SECTION ".debug_loc"
3767 #endif
3768 #ifndef DEBUG_DWO_LOC_SECTION
3769 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3770 #endif
3771 #ifndef DEBUG_LOCLISTS_SECTION
3772 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3773 #endif
3774 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3775 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3776 #endif
3777 #ifndef DEBUG_PUBNAMES_SECTION
3778 #define DEBUG_PUBNAMES_SECTION \
3779 ((debug_generate_pub_sections == 2) \
3780 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3781 #endif
3782 #ifndef DEBUG_PUBTYPES_SECTION
3783 #define DEBUG_PUBTYPES_SECTION \
3784 ((debug_generate_pub_sections == 2) \
3785 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3786 #endif
3787 #ifndef DEBUG_STR_OFFSETS_SECTION
3788 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3789 #endif
3790 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3791 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3792 #endif
3793 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3794 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3795 #endif
3796 #ifndef DEBUG_STR_SECTION
3797 #define DEBUG_STR_SECTION ".debug_str"
3798 #endif
3799 #ifndef DEBUG_LTO_STR_SECTION
3800 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3801 #endif
3802 #ifndef DEBUG_STR_DWO_SECTION
3803 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3804 #endif
3805 #ifndef DEBUG_LTO_STR_DWO_SECTION
3806 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3807 #endif
3808 #ifndef DEBUG_RANGES_SECTION
3809 #define DEBUG_RANGES_SECTION ".debug_ranges"
3810 #endif
3811 #ifndef DEBUG_RNGLISTS_SECTION
3812 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3813 #endif
3814 #ifndef DEBUG_LINE_STR_SECTION
3815 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3816 #endif
3817 #ifndef DEBUG_LTO_LINE_STR_SECTION
3818 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3819 #endif
3821 /* Standard ELF section names for compiled code and data. */
3822 #ifndef TEXT_SECTION_NAME
3823 #define TEXT_SECTION_NAME ".text"
3824 #endif
3826 /* Section flags for .debug_str section. */
3827 #define DEBUG_STR_SECTION_FLAGS \
3828 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3829 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3830 : SECTION_DEBUG)
3832 /* Section flags for .debug_str.dwo section. */
3833 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3835 /* Attribute used to refer to the macro section. */
3836 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3837 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3839 /* Labels we insert at beginning sections we can reference instead of
3840 the section names themselves. */
3842 #ifndef TEXT_SECTION_LABEL
3843 #define TEXT_SECTION_LABEL "Ltext"
3844 #endif
3845 #ifndef COLD_TEXT_SECTION_LABEL
3846 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3847 #endif
3848 #ifndef DEBUG_LINE_SECTION_LABEL
3849 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3850 #endif
3851 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3852 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3853 #endif
3854 #ifndef DEBUG_INFO_SECTION_LABEL
3855 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3856 #endif
3857 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3858 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3859 #endif
3860 #ifndef DEBUG_ABBREV_SECTION_LABEL
3861 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3862 #endif
3863 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3864 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3865 #endif
3866 #ifndef DEBUG_ADDR_SECTION_LABEL
3867 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3868 #endif
3869 #ifndef DEBUG_LOC_SECTION_LABEL
3870 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3871 #endif
3872 #ifndef DEBUG_RANGES_SECTION_LABEL
3873 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3874 #endif
3875 #ifndef DEBUG_MACINFO_SECTION_LABEL
3876 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3877 #endif
3878 #ifndef DEBUG_MACRO_SECTION_LABEL
3879 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3880 #endif
3881 #define SKELETON_COMP_DIE_ABBREV 1
3882 #define SKELETON_TYPE_DIE_ABBREV 2
3884 /* Definitions of defaults for formats and names of various special
3885 (artificial) labels which may be generated within this file (when the -g
3886 options is used and DWARF2_DEBUGGING_INFO is in effect.
3887 If necessary, these may be overridden from within the tm.h file, but
3888 typically, overriding these defaults is unnecessary. */
3890 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3891 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3892 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3893 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3894 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3895 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3899 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3900 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3901 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3902 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3903 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3904 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3906 #ifndef TEXT_END_LABEL
3907 #define TEXT_END_LABEL "Letext"
3908 #endif
3909 #ifndef COLD_END_LABEL
3910 #define COLD_END_LABEL "Letext_cold"
3911 #endif
3912 #ifndef BLOCK_BEGIN_LABEL
3913 #define BLOCK_BEGIN_LABEL "LBB"
3914 #endif
3915 #ifndef BLOCK_END_LABEL
3916 #define BLOCK_END_LABEL "LBE"
3917 #endif
3918 #ifndef LINE_CODE_LABEL
3919 #define LINE_CODE_LABEL "LM"
3920 #endif
3923 /* Return the root of the DIE's built for the current compilation unit. */
3924 static dw_die_ref
3925 comp_unit_die (void)
3927 if (!single_comp_unit_die)
3928 single_comp_unit_die = gen_compile_unit_die (NULL);
3929 return single_comp_unit_die;
3932 /* We allow a language front-end to designate a function that is to be
3933 called to "demangle" any name before it is put into a DIE. */
3935 static const char *(*demangle_name_func) (const char *);
3937 void
3938 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3940 demangle_name_func = func;
3943 /* Test if rtl node points to a pseudo register. */
3945 static inline int
3946 is_pseudo_reg (const_rtx rtl)
3948 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3949 || (GET_CODE (rtl) == SUBREG
3950 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3953 /* Return a reference to a type, with its const and volatile qualifiers
3954 removed. */
3956 static inline tree
3957 type_main_variant (tree type)
3959 type = TYPE_MAIN_VARIANT (type);
3961 /* ??? There really should be only one main variant among any group of
3962 variants of a given type (and all of the MAIN_VARIANT values for all
3963 members of the group should point to that one type) but sometimes the C
3964 front-end messes this up for array types, so we work around that bug
3965 here. */
3966 if (TREE_CODE (type) == ARRAY_TYPE)
3967 while (type != TYPE_MAIN_VARIANT (type))
3968 type = TYPE_MAIN_VARIANT (type);
3970 return type;
3973 /* Return nonzero if the given type node represents a tagged type. */
3975 static inline int
3976 is_tagged_type (const_tree type)
3978 enum tree_code code = TREE_CODE (type);
3980 return (code == RECORD_TYPE || code == UNION_TYPE
3981 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3984 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3986 static void
3987 get_ref_die_offset_label (char *label, dw_die_ref ref)
3989 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3992 /* Return die_offset of a DIE reference to a base type. */
3994 static unsigned long int
3995 get_base_type_offset (dw_die_ref ref)
3997 if (ref->die_offset)
3998 return ref->die_offset;
3999 if (comp_unit_die ()->die_abbrev)
4001 calc_base_type_die_sizes ();
4002 gcc_assert (ref->die_offset);
4004 return ref->die_offset;
4007 /* Return die_offset of a DIE reference other than base type. */
4009 static unsigned long int
4010 get_ref_die_offset (dw_die_ref ref)
4012 gcc_assert (ref->die_offset);
4013 return ref->die_offset;
4016 /* Convert a DIE tag into its string name. */
4018 static const char *
4019 dwarf_tag_name (unsigned int tag)
4021 const char *name = get_DW_TAG_name (tag);
4023 if (name != NULL)
4024 return name;
4026 return "DW_TAG_<unknown>";
4029 /* Convert a DWARF attribute code into its string name. */
4031 static const char *
4032 dwarf_attr_name (unsigned int attr)
4034 const char *name;
4036 switch (attr)
4038 #if VMS_DEBUGGING_INFO
4039 case DW_AT_HP_prologue:
4040 return "DW_AT_HP_prologue";
4041 #else
4042 case DW_AT_MIPS_loop_unroll_factor:
4043 return "DW_AT_MIPS_loop_unroll_factor";
4044 #endif
4046 #if VMS_DEBUGGING_INFO
4047 case DW_AT_HP_epilogue:
4048 return "DW_AT_HP_epilogue";
4049 #else
4050 case DW_AT_MIPS_stride:
4051 return "DW_AT_MIPS_stride";
4052 #endif
4055 name = get_DW_AT_name (attr);
4057 if (name != NULL)
4058 return name;
4060 return "DW_AT_<unknown>";
4063 /* Convert a DWARF value form code into its string name. */
4065 static const char *
4066 dwarf_form_name (unsigned int form)
4068 const char *name = get_DW_FORM_name (form);
4070 if (name != NULL)
4071 return name;
4073 return "DW_FORM_<unknown>";
4076 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4077 instance of an inlined instance of a decl which is local to an inline
4078 function, so we have to trace all of the way back through the origin chain
4079 to find out what sort of node actually served as the original seed for the
4080 given block. */
4082 static tree
4083 decl_ultimate_origin (const_tree decl)
4085 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4086 return NULL_TREE;
4088 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4089 we're trying to output the abstract instance of this function. */
4090 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4091 return NULL_TREE;
4093 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4094 most distant ancestor, this should never happen. */
4095 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4097 return DECL_ABSTRACT_ORIGIN (decl);
4100 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4101 of a virtual function may refer to a base class, so we check the 'this'
4102 parameter. */
4104 static tree
4105 decl_class_context (tree decl)
4107 tree context = NULL_TREE;
4109 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4110 context = DECL_CONTEXT (decl);
4111 else
4112 context = TYPE_MAIN_VARIANT
4113 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4115 if (context && !TYPE_P (context))
4116 context = NULL_TREE;
4118 return context;
4121 /* Add an attribute/value pair to a DIE. */
4123 static inline void
4124 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4126 /* Maybe this should be an assert? */
4127 if (die == NULL)
4128 return;
4130 if (flag_checking)
4132 /* Check we do not add duplicate attrs. Can't use get_AT here
4133 because that recurses to the specification/abstract origin DIE. */
4134 dw_attr_node *a;
4135 unsigned ix;
4136 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4137 gcc_assert (a->dw_attr != attr->dw_attr);
4140 vec_safe_reserve (die->die_attr, 1);
4141 vec_safe_push (die->die_attr, *attr);
4144 static inline enum dw_val_class
4145 AT_class (dw_attr_node *a)
4147 return a->dw_attr_val.val_class;
4150 /* Return the index for any attribute that will be referenced with a
4151 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4152 are stored in dw_attr_val.v.val_str for reference counting
4153 pruning. */
4155 static inline unsigned int
4156 AT_index (dw_attr_node *a)
4158 if (AT_class (a) == dw_val_class_str)
4159 return a->dw_attr_val.v.val_str->index;
4160 else if (a->dw_attr_val.val_entry != NULL)
4161 return a->dw_attr_val.val_entry->index;
4162 return NOT_INDEXED;
4165 /* Add a flag value attribute to a DIE. */
4167 static inline void
4168 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4170 dw_attr_node attr;
4172 attr.dw_attr = attr_kind;
4173 attr.dw_attr_val.val_class = dw_val_class_flag;
4174 attr.dw_attr_val.val_entry = NULL;
4175 attr.dw_attr_val.v.val_flag = flag;
4176 add_dwarf_attr (die, &attr);
4179 static inline unsigned
4180 AT_flag (dw_attr_node *a)
4182 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4183 return a->dw_attr_val.v.val_flag;
4186 /* Add a signed integer attribute value to a DIE. */
4188 static inline void
4189 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4191 dw_attr_node attr;
4193 attr.dw_attr = attr_kind;
4194 attr.dw_attr_val.val_class = dw_val_class_const;
4195 attr.dw_attr_val.val_entry = NULL;
4196 attr.dw_attr_val.v.val_int = int_val;
4197 add_dwarf_attr (die, &attr);
4200 static inline HOST_WIDE_INT
4201 AT_int (dw_attr_node *a)
4203 gcc_assert (a && (AT_class (a) == dw_val_class_const
4204 || AT_class (a) == dw_val_class_const_implicit));
4205 return a->dw_attr_val.v.val_int;
4208 /* Add an unsigned integer attribute value to a DIE. */
4210 static inline void
4211 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4212 unsigned HOST_WIDE_INT unsigned_val)
4214 dw_attr_node attr;
4216 attr.dw_attr = attr_kind;
4217 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4218 attr.dw_attr_val.val_entry = NULL;
4219 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4220 add_dwarf_attr (die, &attr);
4223 static inline unsigned HOST_WIDE_INT
4224 AT_unsigned (dw_attr_node *a)
4226 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4227 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4228 return a->dw_attr_val.v.val_unsigned;
4231 /* Add an unsigned wide integer attribute value to a DIE. */
4233 static inline void
4234 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4235 const wide_int& w)
4237 dw_attr_node attr;
4239 attr.dw_attr = attr_kind;
4240 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4241 attr.dw_attr_val.val_entry = NULL;
4242 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4243 *attr.dw_attr_val.v.val_wide = w;
4244 add_dwarf_attr (die, &attr);
4247 /* Add an unsigned double integer attribute value to a DIE. */
4249 static inline void
4250 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4251 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4253 dw_attr_node attr;
4255 attr.dw_attr = attr_kind;
4256 attr.dw_attr_val.val_class = dw_val_class_const_double;
4257 attr.dw_attr_val.val_entry = NULL;
4258 attr.dw_attr_val.v.val_double.high = high;
4259 attr.dw_attr_val.v.val_double.low = low;
4260 add_dwarf_attr (die, &attr);
4263 /* Add a floating point attribute value to a DIE and return it. */
4265 static inline void
4266 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4267 unsigned int length, unsigned int elt_size, unsigned char *array)
4269 dw_attr_node attr;
4271 attr.dw_attr = attr_kind;
4272 attr.dw_attr_val.val_class = dw_val_class_vec;
4273 attr.dw_attr_val.val_entry = NULL;
4274 attr.dw_attr_val.v.val_vec.length = length;
4275 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4276 attr.dw_attr_val.v.val_vec.array = array;
4277 add_dwarf_attr (die, &attr);
4280 /* Add an 8-byte data attribute value to a DIE. */
4282 static inline void
4283 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4284 unsigned char data8[8])
4286 dw_attr_node attr;
4288 attr.dw_attr = attr_kind;
4289 attr.dw_attr_val.val_class = dw_val_class_data8;
4290 attr.dw_attr_val.val_entry = NULL;
4291 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4292 add_dwarf_attr (die, &attr);
4295 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4296 dwarf_split_debug_info, address attributes in dies destined for the
4297 final executable have force_direct set to avoid using indexed
4298 references. */
4300 static inline void
4301 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4302 bool force_direct)
4304 dw_attr_node attr;
4305 char * lbl_id;
4307 lbl_id = xstrdup (lbl_low);
4308 attr.dw_attr = DW_AT_low_pc;
4309 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4310 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4311 if (dwarf_split_debug_info && !force_direct)
4312 attr.dw_attr_val.val_entry
4313 = add_addr_table_entry (lbl_id, ate_kind_label);
4314 else
4315 attr.dw_attr_val.val_entry = NULL;
4316 add_dwarf_attr (die, &attr);
4318 attr.dw_attr = DW_AT_high_pc;
4319 if (dwarf_version < 4)
4320 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4321 else
4322 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4323 lbl_id = xstrdup (lbl_high);
4324 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4325 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4326 && dwarf_split_debug_info && !force_direct)
4327 attr.dw_attr_val.val_entry
4328 = add_addr_table_entry (lbl_id, ate_kind_label);
4329 else
4330 attr.dw_attr_val.val_entry = NULL;
4331 add_dwarf_attr (die, &attr);
4334 /* Hash and equality functions for debug_str_hash. */
4336 hashval_t
4337 indirect_string_hasher::hash (indirect_string_node *x)
4339 return htab_hash_string (x->str);
4342 bool
4343 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4345 return strcmp (x1->str, x2) == 0;
4348 /* Add STR to the given string hash table. */
4350 static struct indirect_string_node *
4351 find_AT_string_in_table (const char *str,
4352 hash_table<indirect_string_hasher> *table)
4354 struct indirect_string_node *node;
4356 indirect_string_node **slot
4357 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4358 if (*slot == NULL)
4360 node = ggc_cleared_alloc<indirect_string_node> ();
4361 node->str = ggc_strdup (str);
4362 *slot = node;
4364 else
4365 node = *slot;
4367 node->refcount++;
4368 return node;
4371 /* Add STR to the indirect string hash table. */
4373 static struct indirect_string_node *
4374 find_AT_string (const char *str)
4376 if (! debug_str_hash)
4377 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4379 return find_AT_string_in_table (str, debug_str_hash);
4382 /* Add a string attribute value to a DIE. */
4384 static inline void
4385 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4387 dw_attr_node attr;
4388 struct indirect_string_node *node;
4390 node = find_AT_string (str);
4392 attr.dw_attr = attr_kind;
4393 attr.dw_attr_val.val_class = dw_val_class_str;
4394 attr.dw_attr_val.val_entry = NULL;
4395 attr.dw_attr_val.v.val_str = node;
4396 add_dwarf_attr (die, &attr);
4399 static inline const char *
4400 AT_string (dw_attr_node *a)
4402 gcc_assert (a && AT_class (a) == dw_val_class_str);
4403 return a->dw_attr_val.v.val_str->str;
4406 /* Call this function directly to bypass AT_string_form's logic to put
4407 the string inline in the die. */
4409 static void
4410 set_indirect_string (struct indirect_string_node *node)
4412 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4413 /* Already indirect is a no op. */
4414 if (node->form == DW_FORM_strp
4415 || node->form == DW_FORM_line_strp
4416 || node->form == DW_FORM_GNU_str_index)
4418 gcc_assert (node->label);
4419 return;
4421 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4422 ++dw2_string_counter;
4423 node->label = xstrdup (label);
4425 if (!dwarf_split_debug_info)
4427 node->form = DW_FORM_strp;
4428 node->index = NOT_INDEXED;
4430 else
4432 node->form = DW_FORM_GNU_str_index;
4433 node->index = NO_INDEX_ASSIGNED;
4437 /* A helper function for dwarf2out_finish, called to reset indirect
4438 string decisions done for early LTO dwarf output before fat object
4439 dwarf output. */
4442 reset_indirect_string (indirect_string_node **h, void *)
4444 struct indirect_string_node *node = *h;
4445 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4447 free (node->label);
4448 node->label = NULL;
4449 node->form = (dwarf_form) 0;
4450 node->index = 0;
4452 return 1;
4455 /* Find out whether a string should be output inline in DIE
4456 or out-of-line in .debug_str section. */
4458 static enum dwarf_form
4459 find_string_form (struct indirect_string_node *node)
4461 unsigned int len;
4463 if (node->form)
4464 return node->form;
4466 len = strlen (node->str) + 1;
4468 /* If the string is shorter or equal to the size of the reference, it is
4469 always better to put it inline. */
4470 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4471 return node->form = DW_FORM_string;
4473 /* If we cannot expect the linker to merge strings in .debug_str
4474 section, only put it into .debug_str if it is worth even in this
4475 single module. */
4476 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4477 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4478 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4479 return node->form = DW_FORM_string;
4481 set_indirect_string (node);
4483 return node->form;
4486 /* Find out whether the string referenced from the attribute should be
4487 output inline in DIE or out-of-line in .debug_str section. */
4489 static enum dwarf_form
4490 AT_string_form (dw_attr_node *a)
4492 gcc_assert (a && AT_class (a) == dw_val_class_str);
4493 return find_string_form (a->dw_attr_val.v.val_str);
4496 /* Add a DIE reference attribute value to a DIE. */
4498 static inline void
4499 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4501 dw_attr_node attr;
4502 gcc_checking_assert (targ_die != NULL);
4504 /* With LTO we can end up trying to reference something we didn't create
4505 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4506 if (targ_die == NULL)
4507 return;
4509 attr.dw_attr = attr_kind;
4510 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4511 attr.dw_attr_val.val_entry = NULL;
4512 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4513 attr.dw_attr_val.v.val_die_ref.external = 0;
4514 add_dwarf_attr (die, &attr);
4517 /* Change DIE reference REF to point to NEW_DIE instead. */
4519 static inline void
4520 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4522 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4523 ref->dw_attr_val.v.val_die_ref.die = new_die;
4524 ref->dw_attr_val.v.val_die_ref.external = 0;
4527 /* Add an AT_specification attribute to a DIE, and also make the back
4528 pointer from the specification to the definition. */
4530 static inline void
4531 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4533 add_AT_die_ref (die, DW_AT_specification, targ_die);
4534 gcc_assert (!targ_die->die_definition);
4535 targ_die->die_definition = die;
4538 static inline dw_die_ref
4539 AT_ref (dw_attr_node *a)
4541 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4542 return a->dw_attr_val.v.val_die_ref.die;
4545 static inline int
4546 AT_ref_external (dw_attr_node *a)
4548 if (a && AT_class (a) == dw_val_class_die_ref)
4549 return a->dw_attr_val.v.val_die_ref.external;
4551 return 0;
4554 static inline void
4555 set_AT_ref_external (dw_attr_node *a, int i)
4557 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4558 a->dw_attr_val.v.val_die_ref.external = i;
4561 /* Add an FDE reference attribute value to a DIE. */
4563 static inline void
4564 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4566 dw_attr_node attr;
4568 attr.dw_attr = attr_kind;
4569 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4570 attr.dw_attr_val.val_entry = NULL;
4571 attr.dw_attr_val.v.val_fde_index = targ_fde;
4572 add_dwarf_attr (die, &attr);
4575 /* Add a location description attribute value to a DIE. */
4577 static inline void
4578 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_loc;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_loc = loc;
4586 add_dwarf_attr (die, &attr);
4589 static inline dw_loc_descr_ref
4590 AT_loc (dw_attr_node *a)
4592 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4593 return a->dw_attr_val.v.val_loc;
4596 static inline void
4597 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4599 dw_attr_node attr;
4601 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4602 return;
4604 attr.dw_attr = attr_kind;
4605 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4606 attr.dw_attr_val.val_entry = NULL;
4607 attr.dw_attr_val.v.val_loc_list = loc_list;
4608 add_dwarf_attr (die, &attr);
4609 have_location_lists = true;
4612 static inline dw_loc_list_ref
4613 AT_loc_list (dw_attr_node *a)
4615 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4616 return a->dw_attr_val.v.val_loc_list;
4619 static inline dw_loc_list_ref *
4620 AT_loc_list_ptr (dw_attr_node *a)
4622 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4623 return &a->dw_attr_val.v.val_loc_list;
4626 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4628 static hashval_t hash (addr_table_entry *);
4629 static bool equal (addr_table_entry *, addr_table_entry *);
4632 /* Table of entries into the .debug_addr section. */
4634 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4636 /* Hash an address_table_entry. */
4638 hashval_t
4639 addr_hasher::hash (addr_table_entry *a)
4641 inchash::hash hstate;
4642 switch (a->kind)
4644 case ate_kind_rtx:
4645 hstate.add_int (0);
4646 break;
4647 case ate_kind_rtx_dtprel:
4648 hstate.add_int (1);
4649 break;
4650 case ate_kind_label:
4651 return htab_hash_string (a->addr.label);
4652 default:
4653 gcc_unreachable ();
4655 inchash::add_rtx (a->addr.rtl, hstate);
4656 return hstate.end ();
4659 /* Determine equality for two address_table_entries. */
4661 bool
4662 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4664 if (a1->kind != a2->kind)
4665 return 0;
4666 switch (a1->kind)
4668 case ate_kind_rtx:
4669 case ate_kind_rtx_dtprel:
4670 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4671 case ate_kind_label:
4672 return strcmp (a1->addr.label, a2->addr.label) == 0;
4673 default:
4674 gcc_unreachable ();
4678 /* Initialize an addr_table_entry. */
4680 void
4681 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4683 e->kind = kind;
4684 switch (kind)
4686 case ate_kind_rtx:
4687 case ate_kind_rtx_dtprel:
4688 e->addr.rtl = (rtx) addr;
4689 break;
4690 case ate_kind_label:
4691 e->addr.label = (char *) addr;
4692 break;
4694 e->refcount = 0;
4695 e->index = NO_INDEX_ASSIGNED;
4698 /* Add attr to the address table entry to the table. Defer setting an
4699 index until output time. */
4701 static addr_table_entry *
4702 add_addr_table_entry (void *addr, enum ate_kind kind)
4704 addr_table_entry *node;
4705 addr_table_entry finder;
4707 gcc_assert (dwarf_split_debug_info);
4708 if (! addr_index_table)
4709 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4710 init_addr_table_entry (&finder, kind, addr);
4711 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4713 if (*slot == HTAB_EMPTY_ENTRY)
4715 node = ggc_cleared_alloc<addr_table_entry> ();
4716 init_addr_table_entry (node, kind, addr);
4717 *slot = node;
4719 else
4720 node = *slot;
4722 node->refcount++;
4723 return node;
4726 /* Remove an entry from the addr table by decrementing its refcount.
4727 Strictly, decrementing the refcount would be enough, but the
4728 assertion that the entry is actually in the table has found
4729 bugs. */
4731 static void
4732 remove_addr_table_entry (addr_table_entry *entry)
4734 gcc_assert (dwarf_split_debug_info && addr_index_table);
4735 /* After an index is assigned, the table is frozen. */
4736 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4737 entry->refcount--;
4740 /* Given a location list, remove all addresses it refers to from the
4741 address_table. */
4743 static void
4744 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4746 for (; descr; descr = descr->dw_loc_next)
4747 if (descr->dw_loc_oprnd1.val_entry != NULL)
4749 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4750 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4754 /* A helper function for dwarf2out_finish called through
4755 htab_traverse. Assign an addr_table_entry its index. All entries
4756 must be collected into the table when this function is called,
4757 because the indexing code relies on htab_traverse to traverse nodes
4758 in the same order for each run. */
4761 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4763 addr_table_entry *node = *h;
4765 /* Don't index unreferenced nodes. */
4766 if (node->refcount == 0)
4767 return 1;
4769 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4770 node->index = *index;
4771 *index += 1;
4773 return 1;
4776 /* Add an address constant attribute value to a DIE. When using
4777 dwarf_split_debug_info, address attributes in dies destined for the
4778 final executable should be direct references--setting the parameter
4779 force_direct ensures this behavior. */
4781 static inline void
4782 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4783 bool force_direct)
4785 dw_attr_node attr;
4787 attr.dw_attr = attr_kind;
4788 attr.dw_attr_val.val_class = dw_val_class_addr;
4789 attr.dw_attr_val.v.val_addr = addr;
4790 if (dwarf_split_debug_info && !force_direct)
4791 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4792 else
4793 attr.dw_attr_val.val_entry = NULL;
4794 add_dwarf_attr (die, &attr);
4797 /* Get the RTX from to an address DIE attribute. */
4799 static inline rtx
4800 AT_addr (dw_attr_node *a)
4802 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4803 return a->dw_attr_val.v.val_addr;
4806 /* Add a file attribute value to a DIE. */
4808 static inline void
4809 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4810 struct dwarf_file_data *fd)
4812 dw_attr_node attr;
4814 attr.dw_attr = attr_kind;
4815 attr.dw_attr_val.val_class = dw_val_class_file;
4816 attr.dw_attr_val.val_entry = NULL;
4817 attr.dw_attr_val.v.val_file = fd;
4818 add_dwarf_attr (die, &attr);
4821 /* Get the dwarf_file_data from a file DIE attribute. */
4823 static inline struct dwarf_file_data *
4824 AT_file (dw_attr_node *a)
4826 gcc_assert (a && (AT_class (a) == dw_val_class_file
4827 || AT_class (a) == dw_val_class_file_implicit));
4828 return a->dw_attr_val.v.val_file;
4831 /* Add a vms delta attribute value to a DIE. */
4833 static inline void
4834 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4835 const char *lbl1, const char *lbl2)
4837 dw_attr_node attr;
4839 attr.dw_attr = attr_kind;
4840 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4841 attr.dw_attr_val.val_entry = NULL;
4842 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4843 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4844 add_dwarf_attr (die, &attr);
4847 /* Add a label identifier attribute value to a DIE. */
4849 static inline void
4850 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4851 const char *lbl_id)
4853 dw_attr_node attr;
4855 attr.dw_attr = attr_kind;
4856 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4857 attr.dw_attr_val.val_entry = NULL;
4858 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4859 if (dwarf_split_debug_info)
4860 attr.dw_attr_val.val_entry
4861 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4862 ate_kind_label);
4863 add_dwarf_attr (die, &attr);
4866 /* Add a section offset attribute value to a DIE, an offset into the
4867 debug_line section. */
4869 static inline void
4870 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4871 const char *label)
4873 dw_attr_node attr;
4875 attr.dw_attr = attr_kind;
4876 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4877 attr.dw_attr_val.val_entry = NULL;
4878 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4879 add_dwarf_attr (die, &attr);
4882 /* Add a section offset attribute value to a DIE, an offset into the
4883 debug_loclists section. */
4885 static inline void
4886 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4887 const char *label)
4889 dw_attr_node attr;
4891 attr.dw_attr = attr_kind;
4892 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4893 attr.dw_attr_val.val_entry = NULL;
4894 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4895 add_dwarf_attr (die, &attr);
4898 /* Add a section offset attribute value to a DIE, an offset into the
4899 debug_macinfo section. */
4901 static inline void
4902 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4903 const char *label)
4905 dw_attr_node attr;
4907 attr.dw_attr = attr_kind;
4908 attr.dw_attr_val.val_class = dw_val_class_macptr;
4909 attr.dw_attr_val.val_entry = NULL;
4910 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4911 add_dwarf_attr (die, &attr);
4914 /* Add an offset attribute value to a DIE. */
4916 static inline void
4917 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4918 unsigned HOST_WIDE_INT offset)
4920 dw_attr_node attr;
4922 attr.dw_attr = attr_kind;
4923 attr.dw_attr_val.val_class = dw_val_class_offset;
4924 attr.dw_attr_val.val_entry = NULL;
4925 attr.dw_attr_val.v.val_offset = offset;
4926 add_dwarf_attr (die, &attr);
4929 /* Add a range_list attribute value to a DIE. When using
4930 dwarf_split_debug_info, address attributes in dies destined for the
4931 final executable should be direct references--setting the parameter
4932 force_direct ensures this behavior. */
4934 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4935 #define RELOCATED_OFFSET (NULL)
4937 static void
4938 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4939 long unsigned int offset, bool force_direct)
4941 dw_attr_node attr;
4943 attr.dw_attr = attr_kind;
4944 attr.dw_attr_val.val_class = dw_val_class_range_list;
4945 /* For the range_list attribute, use val_entry to store whether the
4946 offset should follow split-debug-info or normal semantics. This
4947 value is read in output_range_list_offset. */
4948 if (dwarf_split_debug_info && !force_direct)
4949 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4950 else
4951 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4952 attr.dw_attr_val.v.val_offset = offset;
4953 add_dwarf_attr (die, &attr);
4956 /* Return the start label of a delta attribute. */
4958 static inline const char *
4959 AT_vms_delta1 (dw_attr_node *a)
4961 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4962 return a->dw_attr_val.v.val_vms_delta.lbl1;
4965 /* Return the end label of a delta attribute. */
4967 static inline const char *
4968 AT_vms_delta2 (dw_attr_node *a)
4970 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4971 return a->dw_attr_val.v.val_vms_delta.lbl2;
4974 static inline const char *
4975 AT_lbl (dw_attr_node *a)
4977 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4978 || AT_class (a) == dw_val_class_lineptr
4979 || AT_class (a) == dw_val_class_macptr
4980 || AT_class (a) == dw_val_class_loclistsptr
4981 || AT_class (a) == dw_val_class_high_pc));
4982 return a->dw_attr_val.v.val_lbl_id;
4985 /* Get the attribute of type attr_kind. */
4987 static dw_attr_node *
4988 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4990 dw_attr_node *a;
4991 unsigned ix;
4992 dw_die_ref spec = NULL;
4994 if (! die)
4995 return NULL;
4997 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4998 if (a->dw_attr == attr_kind)
4999 return a;
5000 else if (a->dw_attr == DW_AT_specification
5001 || a->dw_attr == DW_AT_abstract_origin)
5002 spec = AT_ref (a);
5004 if (spec)
5005 return get_AT (spec, attr_kind);
5007 return NULL;
5010 /* Returns the parent of the declaration of DIE. */
5012 static dw_die_ref
5013 get_die_parent (dw_die_ref die)
5015 dw_die_ref t;
5017 if (!die)
5018 return NULL;
5020 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5021 || (t = get_AT_ref (die, DW_AT_specification)))
5022 die = t;
5024 return die->die_parent;
5027 /* Return the "low pc" attribute value, typically associated with a subprogram
5028 DIE. Return null if the "low pc" attribute is either not present, or if it
5029 cannot be represented as an assembler label identifier. */
5031 static inline const char *
5032 get_AT_low_pc (dw_die_ref die)
5034 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5036 return a ? AT_lbl (a) : NULL;
5039 /* Return the "high pc" attribute value, typically associated with a subprogram
5040 DIE. Return null if the "high pc" attribute is either not present, or if it
5041 cannot be represented as an assembler label identifier. */
5043 static inline const char *
5044 get_AT_hi_pc (dw_die_ref die)
5046 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5048 return a ? AT_lbl (a) : NULL;
5051 /* Return the value of the string attribute designated by ATTR_KIND, or
5052 NULL if it is not present. */
5054 static inline const char *
5055 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5057 dw_attr_node *a = get_AT (die, attr_kind);
5059 return a ? AT_string (a) : NULL;
5062 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5063 if it is not present. */
5065 static inline int
5066 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5068 dw_attr_node *a = get_AT (die, attr_kind);
5070 return a ? AT_flag (a) : 0;
5073 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5074 if it is not present. */
5076 static inline unsigned
5077 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5079 dw_attr_node *a = get_AT (die, attr_kind);
5081 return a ? AT_unsigned (a) : 0;
5084 static inline dw_die_ref
5085 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5087 dw_attr_node *a = get_AT (die, attr_kind);
5089 return a ? AT_ref (a) : NULL;
5092 static inline struct dwarf_file_data *
5093 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5095 dw_attr_node *a = get_AT (die, attr_kind);
5097 return a ? AT_file (a) : NULL;
5100 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
5102 static const_tree
5103 get_ultimate_context (const_tree decl)
5105 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
5107 if (TREE_CODE (decl) == BLOCK)
5108 decl = BLOCK_SUPERCONTEXT (decl);
5109 else
5110 decl = get_containing_scope (decl);
5112 return decl;
5115 /* Return TRUE if the language is C++. */
5117 static inline bool
5118 is_cxx (void)
5120 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5122 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5123 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5126 /* Return TRUE if DECL was created by the C++ frontend. */
5128 static bool
5129 is_cxx (const_tree decl)
5131 if (in_lto_p)
5133 const_tree context = get_ultimate_context (decl);
5134 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5135 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5137 return is_cxx ();
5140 /* Return TRUE if the language is Fortran. */
5142 static inline bool
5143 is_fortran (void)
5145 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5147 return (lang == DW_LANG_Fortran77
5148 || lang == DW_LANG_Fortran90
5149 || lang == DW_LANG_Fortran95
5150 || lang == DW_LANG_Fortran03
5151 || lang == DW_LANG_Fortran08);
5154 static inline bool
5155 is_fortran (const_tree decl)
5157 if (in_lto_p)
5159 const_tree context = get_ultimate_context (decl);
5160 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5161 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5162 "GNU Fortran", 11) == 0
5163 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5164 "GNU F77") == 0);
5166 return is_fortran ();
5169 /* Return TRUE if the language is Ada. */
5171 static inline bool
5172 is_ada (void)
5174 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5176 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5179 /* Remove the specified attribute if present. Return TRUE if removal
5180 was successful. */
5182 static bool
5183 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5185 dw_attr_node *a;
5186 unsigned ix;
5188 if (! die)
5189 return false;
5191 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5192 if (a->dw_attr == attr_kind)
5194 if (AT_class (a) == dw_val_class_str)
5195 if (a->dw_attr_val.v.val_str->refcount)
5196 a->dw_attr_val.v.val_str->refcount--;
5198 /* vec::ordered_remove should help reduce the number of abbrevs
5199 that are needed. */
5200 die->die_attr->ordered_remove (ix);
5201 return true;
5203 return false;
5206 /* Remove CHILD from its parent. PREV must have the property that
5207 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5209 static void
5210 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5212 gcc_assert (child->die_parent == prev->die_parent);
5213 gcc_assert (prev->die_sib == child);
5214 if (prev == child)
5216 gcc_assert (child->die_parent->die_child == child);
5217 prev = NULL;
5219 else
5220 prev->die_sib = child->die_sib;
5221 if (child->die_parent->die_child == child)
5222 child->die_parent->die_child = prev;
5223 child->die_sib = NULL;
5226 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5227 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5229 static void
5230 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5232 dw_die_ref parent = old_child->die_parent;
5234 gcc_assert (parent == prev->die_parent);
5235 gcc_assert (prev->die_sib == old_child);
5237 new_child->die_parent = parent;
5238 if (prev == old_child)
5240 gcc_assert (parent->die_child == old_child);
5241 new_child->die_sib = new_child;
5243 else
5245 prev->die_sib = new_child;
5246 new_child->die_sib = old_child->die_sib;
5248 if (old_child->die_parent->die_child == old_child)
5249 old_child->die_parent->die_child = new_child;
5250 old_child->die_sib = NULL;
5253 /* Move all children from OLD_PARENT to NEW_PARENT. */
5255 static void
5256 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5258 dw_die_ref c;
5259 new_parent->die_child = old_parent->die_child;
5260 old_parent->die_child = NULL;
5261 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5264 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5265 matches TAG. */
5267 static void
5268 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5270 dw_die_ref c;
5272 c = die->die_child;
5273 if (c) do {
5274 dw_die_ref prev = c;
5275 c = c->die_sib;
5276 while (c->die_tag == tag)
5278 remove_child_with_prev (c, prev);
5279 c->die_parent = NULL;
5280 /* Might have removed every child. */
5281 if (die->die_child == NULL)
5282 return;
5283 c = prev->die_sib;
5285 } while (c != die->die_child);
5288 /* Add a CHILD_DIE as the last child of DIE. */
5290 static void
5291 add_child_die (dw_die_ref die, dw_die_ref child_die)
5293 /* FIXME this should probably be an assert. */
5294 if (! die || ! child_die)
5295 return;
5296 gcc_assert (die != child_die);
5298 child_die->die_parent = die;
5299 if (die->die_child)
5301 child_die->die_sib = die->die_child->die_sib;
5302 die->die_child->die_sib = child_die;
5304 else
5305 child_die->die_sib = child_die;
5306 die->die_child = child_die;
5309 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5311 static void
5312 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5313 dw_die_ref after_die)
5315 gcc_assert (die
5316 && child_die
5317 && after_die
5318 && die->die_child
5319 && die != child_die);
5321 child_die->die_parent = die;
5322 child_die->die_sib = after_die->die_sib;
5323 after_die->die_sib = child_die;
5324 if (die->die_child == after_die)
5325 die->die_child = child_die;
5328 /* Unassociate CHILD from its parent, and make its parent be
5329 NEW_PARENT. */
5331 static void
5332 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5334 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5335 if (p->die_sib == child)
5337 remove_child_with_prev (child, p);
5338 break;
5340 add_child_die (new_parent, child);
5343 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5344 is the specification, to the end of PARENT's list of children.
5345 This is done by removing and re-adding it. */
5347 static void
5348 splice_child_die (dw_die_ref parent, dw_die_ref child)
5350 /* We want the declaration DIE from inside the class, not the
5351 specification DIE at toplevel. */
5352 if (child->die_parent != parent)
5354 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5356 if (tmp)
5357 child = tmp;
5360 gcc_assert (child->die_parent == parent
5361 || (child->die_parent
5362 == get_AT_ref (parent, DW_AT_specification)));
5364 reparent_child (child, parent);
5367 /* Create and return a new die with a parent of PARENT_DIE. If
5368 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5369 associated tree T must be supplied to determine parenthood
5370 later. */
5372 static inline dw_die_ref
5373 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5375 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5377 die->die_tag = tag_value;
5379 if (parent_die != NULL)
5380 add_child_die (parent_die, die);
5381 else
5383 limbo_die_node *limbo_node;
5385 /* No DIEs created after early dwarf should end up in limbo,
5386 because the limbo list should not persist past LTO
5387 streaming. */
5388 if (tag_value != DW_TAG_compile_unit
5389 /* These are allowed because they're generated while
5390 breaking out COMDAT units late. */
5391 && tag_value != DW_TAG_type_unit
5392 && tag_value != DW_TAG_skeleton_unit
5393 && !early_dwarf
5394 /* Allow nested functions to live in limbo because they will
5395 only temporarily live there, as decls_for_scope will fix
5396 them up. */
5397 && (TREE_CODE (t) != FUNCTION_DECL
5398 || !decl_function_context (t))
5399 /* Same as nested functions above but for types. Types that
5400 are local to a function will be fixed in
5401 decls_for_scope. */
5402 && (!RECORD_OR_UNION_TYPE_P (t)
5403 || !TYPE_CONTEXT (t)
5404 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5405 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5406 especially in the ltrans stage, but once we implement LTO
5407 dwarf streaming, we should remove this exception. */
5408 && !in_lto_p)
5410 fprintf (stderr, "symbol ended up in limbo too late:");
5411 debug_generic_stmt (t);
5412 gcc_unreachable ();
5415 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5416 limbo_node->die = die;
5417 limbo_node->created_for = t;
5418 limbo_node->next = limbo_die_list;
5419 limbo_die_list = limbo_node;
5422 return die;
5425 /* Return the DIE associated with the given type specifier. */
5427 static inline dw_die_ref
5428 lookup_type_die (tree type)
5430 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5431 if (die && die->removed)
5433 TYPE_SYMTAB_DIE (type) = NULL;
5434 return NULL;
5436 return die;
5439 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5440 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5441 anonymous type instead the one of the naming typedef. */
5443 static inline dw_die_ref
5444 strip_naming_typedef (tree type, dw_die_ref type_die)
5446 if (type
5447 && TREE_CODE (type) == RECORD_TYPE
5448 && type_die
5449 && type_die->die_tag == DW_TAG_typedef
5450 && is_naming_typedef_decl (TYPE_NAME (type)))
5451 type_die = get_AT_ref (type_die, DW_AT_type);
5452 return type_die;
5455 /* Like lookup_type_die, but if type is an anonymous type named by a
5456 typedef[1], return the DIE of the anonymous type instead the one of
5457 the naming typedef. This is because in gen_typedef_die, we did
5458 equate the anonymous struct named by the typedef with the DIE of
5459 the naming typedef. So by default, lookup_type_die on an anonymous
5460 struct yields the DIE of the naming typedef.
5462 [1]: Read the comment of is_naming_typedef_decl to learn about what
5463 a naming typedef is. */
5465 static inline dw_die_ref
5466 lookup_type_die_strip_naming_typedef (tree type)
5468 dw_die_ref die = lookup_type_die (type);
5469 return strip_naming_typedef (type, die);
5472 /* Equate a DIE to a given type specifier. */
5474 static inline void
5475 equate_type_number_to_die (tree type, dw_die_ref type_die)
5477 TYPE_SYMTAB_DIE (type) = type_die;
5480 /* Returns a hash value for X (which really is a die_struct). */
5482 inline hashval_t
5483 decl_die_hasher::hash (die_node *x)
5485 return (hashval_t) x->decl_id;
5488 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5490 inline bool
5491 decl_die_hasher::equal (die_node *x, tree y)
5493 return (x->decl_id == DECL_UID (y));
5496 /* Return the DIE associated with a given declaration. */
5498 static inline dw_die_ref
5499 lookup_decl_die (tree decl)
5501 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5502 NO_INSERT);
5503 if (!die)
5504 return NULL;
5505 if ((*die)->removed)
5507 decl_die_table->clear_slot (die);
5508 return NULL;
5510 return *die;
5514 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5515 style reference. Return true if we found one refering to a DIE for
5516 DECL, otherwise return false. */
5518 static bool
5519 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5520 unsigned HOST_WIDE_INT *off)
5522 dw_die_ref die;
5524 if (flag_wpa && !decl_die_table)
5525 return false;
5527 if (TREE_CODE (decl) == BLOCK)
5528 die = BLOCK_DIE (decl);
5529 else
5530 die = lookup_decl_die (decl);
5531 if (!die)
5532 return false;
5534 /* During WPA stage we currently use DIEs to store the
5535 decl <-> label + offset map. That's quite inefficient but it
5536 works for now. */
5537 if (flag_wpa)
5539 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5540 if (!ref)
5542 gcc_assert (die == comp_unit_die ());
5543 return false;
5545 *off = ref->die_offset;
5546 *sym = ref->die_id.die_symbol;
5547 return true;
5550 /* Similar to get_ref_die_offset_label, but using the "correct"
5551 label. */
5552 *off = die->die_offset;
5553 while (die->die_parent)
5554 die = die->die_parent;
5555 /* For the containing CU DIE we compute a die_symbol in
5556 compute_comp_unit_symbol. */
5557 gcc_assert (die->die_tag == DW_TAG_compile_unit
5558 && die->die_id.die_symbol != NULL);
5559 *sym = die->die_id.die_symbol;
5560 return true;
5563 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5565 static void
5566 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5567 const char *symbol, HOST_WIDE_INT offset)
5569 /* Create a fake DIE that contains the reference. Don't use
5570 new_die because we don't want to end up in the limbo list. */
5571 dw_die_ref ref = ggc_cleared_alloc<die_node> ();
5572 ref->die_tag = die->die_tag;
5573 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5574 ref->die_offset = offset;
5575 ref->with_offset = 1;
5576 add_AT_die_ref (die, attr_kind, ref);
5579 /* Create a DIE for DECL if required and add a reference to a DIE
5580 at SYMBOL + OFFSET which contains attributes dumped early. */
5582 static void
5583 dwarf2out_register_external_die (tree decl, const char *sym,
5584 unsigned HOST_WIDE_INT off)
5586 if (debug_info_level == DINFO_LEVEL_NONE)
5587 return;
5589 if (flag_wpa && !decl_die_table)
5590 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5592 dw_die_ref die
5593 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5594 gcc_assert (!die);
5596 tree ctx;
5597 dw_die_ref parent = NULL;
5598 /* Need to lookup a DIE for the decls context - the containing
5599 function or translation unit. */
5600 if (TREE_CODE (decl) == BLOCK)
5602 ctx = BLOCK_SUPERCONTEXT (decl);
5603 /* ??? We do not output DIEs for all scopes thus skip as
5604 many DIEs as needed. */
5605 while (TREE_CODE (ctx) == BLOCK
5606 && !BLOCK_DIE (ctx))
5607 ctx = BLOCK_SUPERCONTEXT (ctx);
5609 else
5610 ctx = DECL_CONTEXT (decl);
5611 while (ctx && TYPE_P (ctx))
5612 ctx = TYPE_CONTEXT (ctx);
5613 if (ctx)
5615 if (TREE_CODE (ctx) == BLOCK)
5616 parent = BLOCK_DIE (ctx);
5617 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5618 /* Keep the 1:1 association during WPA. */
5619 && !flag_wpa)
5620 /* Otherwise all late annotations go to the main CU which
5621 imports the original CUs. */
5622 parent = comp_unit_die ();
5623 else if (TREE_CODE (ctx) == FUNCTION_DECL
5624 && TREE_CODE (decl) != PARM_DECL
5625 && TREE_CODE (decl) != BLOCK)
5626 /* Leave function local entities parent determination to when
5627 we process scope vars. */
5629 else
5630 parent = lookup_decl_die (ctx);
5632 else
5633 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5634 Handle this case gracefully by globalizing stuff. */
5635 parent = comp_unit_die ();
5636 /* Create a DIE "stub". */
5637 switch (TREE_CODE (decl))
5639 case TRANSLATION_UNIT_DECL:
5640 if (! flag_wpa)
5642 die = comp_unit_die ();
5643 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5644 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5645 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5646 to create a DIE for the original CUs. */
5647 return;
5649 /* Keep the 1:1 association during WPA. */
5650 die = new_die (DW_TAG_compile_unit, NULL, decl);
5651 break;
5652 case NAMESPACE_DECL:
5653 if (is_fortran (decl))
5654 die = new_die (DW_TAG_module, parent, decl);
5655 else
5656 die = new_die (DW_TAG_namespace, parent, decl);
5657 break;
5658 case FUNCTION_DECL:
5659 die = new_die (DW_TAG_subprogram, parent, decl);
5660 break;
5661 case VAR_DECL:
5662 die = new_die (DW_TAG_variable, parent, decl);
5663 break;
5664 case RESULT_DECL:
5665 die = new_die (DW_TAG_variable, parent, decl);
5666 break;
5667 case PARM_DECL:
5668 die = new_die (DW_TAG_formal_parameter, parent, decl);
5669 break;
5670 case CONST_DECL:
5671 die = new_die (DW_TAG_constant, parent, decl);
5672 break;
5673 case LABEL_DECL:
5674 die = new_die (DW_TAG_label, parent, decl);
5675 break;
5676 case BLOCK:
5677 die = new_die (DW_TAG_lexical_block, parent, decl);
5678 break;
5679 default:
5680 gcc_unreachable ();
5682 if (TREE_CODE (decl) == BLOCK)
5683 BLOCK_DIE (decl) = die;
5684 else
5685 equate_decl_number_to_die (decl, die);
5687 /* Add a reference to the DIE providing early debug at $sym + off. */
5688 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5691 /* Returns a hash value for X (which really is a var_loc_list). */
5693 inline hashval_t
5694 decl_loc_hasher::hash (var_loc_list *x)
5696 return (hashval_t) x->decl_id;
5699 /* Return nonzero if decl_id of var_loc_list X is the same as
5700 UID of decl *Y. */
5702 inline bool
5703 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5705 return (x->decl_id == DECL_UID (y));
5708 /* Return the var_loc list associated with a given declaration. */
5710 static inline var_loc_list *
5711 lookup_decl_loc (const_tree decl)
5713 if (!decl_loc_table)
5714 return NULL;
5715 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5718 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5720 inline hashval_t
5721 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5723 return (hashval_t) x->decl_id;
5726 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5727 UID of decl *Y. */
5729 inline bool
5730 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5732 return (x->decl_id == DECL_UID (y));
5735 /* Equate a DIE to a particular declaration. */
5737 static void
5738 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5740 unsigned int decl_id = DECL_UID (decl);
5742 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5743 decl_die->decl_id = decl_id;
5746 /* Return how many bits covers PIECE EXPR_LIST. */
5748 static HOST_WIDE_INT
5749 decl_piece_bitsize (rtx piece)
5751 int ret = (int) GET_MODE (piece);
5752 if (ret)
5753 return ret;
5754 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5755 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5756 return INTVAL (XEXP (XEXP (piece, 0), 0));
5759 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5761 static rtx *
5762 decl_piece_varloc_ptr (rtx piece)
5764 if ((int) GET_MODE (piece))
5765 return &XEXP (piece, 0);
5766 else
5767 return &XEXP (XEXP (piece, 0), 1);
5770 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5771 Next is the chain of following piece nodes. */
5773 static rtx_expr_list *
5774 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5776 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5777 return alloc_EXPR_LIST (bitsize, loc_note, next);
5778 else
5779 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5780 GEN_INT (bitsize),
5781 loc_note), next);
5784 /* Return rtx that should be stored into loc field for
5785 LOC_NOTE and BITPOS/BITSIZE. */
5787 static rtx
5788 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5789 HOST_WIDE_INT bitsize)
5791 if (bitsize != -1)
5793 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5794 if (bitpos != 0)
5795 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5797 return loc_note;
5800 /* This function either modifies location piece list *DEST in
5801 place (if SRC and INNER is NULL), or copies location piece list
5802 *SRC to *DEST while modifying it. Location BITPOS is modified
5803 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5804 not copied and if needed some padding around it is added.
5805 When modifying in place, DEST should point to EXPR_LIST where
5806 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5807 to the start of the whole list and INNER points to the EXPR_LIST
5808 where earlier pieces cover PIECE_BITPOS bits. */
5810 static void
5811 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5812 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5813 HOST_WIDE_INT bitsize, rtx loc_note)
5815 HOST_WIDE_INT diff;
5816 bool copy = inner != NULL;
5818 if (copy)
5820 /* First copy all nodes preceding the current bitpos. */
5821 while (src != inner)
5823 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5824 decl_piece_bitsize (*src), NULL_RTX);
5825 dest = &XEXP (*dest, 1);
5826 src = &XEXP (*src, 1);
5829 /* Add padding if needed. */
5830 if (bitpos != piece_bitpos)
5832 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5833 copy ? NULL_RTX : *dest);
5834 dest = &XEXP (*dest, 1);
5836 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5838 gcc_assert (!copy);
5839 /* A piece with correct bitpos and bitsize already exist,
5840 just update the location for it and return. */
5841 *decl_piece_varloc_ptr (*dest) = loc_note;
5842 return;
5844 /* Add the piece that changed. */
5845 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5846 dest = &XEXP (*dest, 1);
5847 /* Skip over pieces that overlap it. */
5848 diff = bitpos - piece_bitpos + bitsize;
5849 if (!copy)
5850 src = dest;
5851 while (diff > 0 && *src)
5853 rtx piece = *src;
5854 diff -= decl_piece_bitsize (piece);
5855 if (copy)
5856 src = &XEXP (piece, 1);
5857 else
5859 *src = XEXP (piece, 1);
5860 free_EXPR_LIST_node (piece);
5863 /* Add padding if needed. */
5864 if (diff < 0 && *src)
5866 if (!copy)
5867 dest = src;
5868 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5869 dest = &XEXP (*dest, 1);
5871 if (!copy)
5872 return;
5873 /* Finally copy all nodes following it. */
5874 while (*src)
5876 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5877 decl_piece_bitsize (*src), NULL_RTX);
5878 dest = &XEXP (*dest, 1);
5879 src = &XEXP (*src, 1);
5883 /* Add a variable location node to the linked list for DECL. */
5885 static struct var_loc_node *
5886 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5888 unsigned int decl_id;
5889 var_loc_list *temp;
5890 struct var_loc_node *loc = NULL;
5891 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5893 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5895 tree realdecl = DECL_DEBUG_EXPR (decl);
5896 if (handled_component_p (realdecl)
5897 || (TREE_CODE (realdecl) == MEM_REF
5898 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5900 HOST_WIDE_INT maxsize;
5901 bool reverse;
5902 tree innerdecl
5903 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5904 &reverse);
5905 if (!DECL_P (innerdecl)
5906 || DECL_IGNORED_P (innerdecl)
5907 || TREE_STATIC (innerdecl)
5908 || bitsize <= 0
5909 || bitpos + bitsize > 256
5910 || bitsize != maxsize)
5911 return NULL;
5912 decl = innerdecl;
5916 decl_id = DECL_UID (decl);
5917 var_loc_list **slot
5918 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5919 if (*slot == NULL)
5921 temp = ggc_cleared_alloc<var_loc_list> ();
5922 temp->decl_id = decl_id;
5923 *slot = temp;
5925 else
5926 temp = *slot;
5928 /* For PARM_DECLs try to keep around the original incoming value,
5929 even if that means we'll emit a zero-range .debug_loc entry. */
5930 if (temp->last
5931 && temp->first == temp->last
5932 && TREE_CODE (decl) == PARM_DECL
5933 && NOTE_P (temp->first->loc)
5934 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5935 && DECL_INCOMING_RTL (decl)
5936 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5937 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5938 == GET_CODE (DECL_INCOMING_RTL (decl))
5939 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5940 && (bitsize != -1
5941 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5942 NOTE_VAR_LOCATION_LOC (loc_note))
5943 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5944 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5946 loc = ggc_cleared_alloc<var_loc_node> ();
5947 temp->first->next = loc;
5948 temp->last = loc;
5949 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5951 else if (temp->last)
5953 struct var_loc_node *last = temp->last, *unused = NULL;
5954 rtx *piece_loc = NULL, last_loc_note;
5955 HOST_WIDE_INT piece_bitpos = 0;
5956 if (last->next)
5958 last = last->next;
5959 gcc_assert (last->next == NULL);
5961 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5963 piece_loc = &last->loc;
5966 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5967 if (piece_bitpos + cur_bitsize > bitpos)
5968 break;
5969 piece_bitpos += cur_bitsize;
5970 piece_loc = &XEXP (*piece_loc, 1);
5972 while (*piece_loc);
5974 /* TEMP->LAST here is either pointer to the last but one or
5975 last element in the chained list, LAST is pointer to the
5976 last element. */
5977 if (label && strcmp (last->label, label) == 0)
5979 /* For SRA optimized variables if there weren't any real
5980 insns since last note, just modify the last node. */
5981 if (piece_loc != NULL)
5983 adjust_piece_list (piece_loc, NULL, NULL,
5984 bitpos, piece_bitpos, bitsize, loc_note);
5985 return NULL;
5987 /* If the last note doesn't cover any instructions, remove it. */
5988 if (temp->last != last)
5990 temp->last->next = NULL;
5991 unused = last;
5992 last = temp->last;
5993 gcc_assert (strcmp (last->label, label) != 0);
5995 else
5997 gcc_assert (temp->first == temp->last
5998 || (temp->first->next == temp->last
5999 && TREE_CODE (decl) == PARM_DECL));
6000 memset (temp->last, '\0', sizeof (*temp->last));
6001 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6002 return temp->last;
6005 if (bitsize == -1 && NOTE_P (last->loc))
6006 last_loc_note = last->loc;
6007 else if (piece_loc != NULL
6008 && *piece_loc != NULL_RTX
6009 && piece_bitpos == bitpos
6010 && decl_piece_bitsize (*piece_loc) == bitsize)
6011 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6012 else
6013 last_loc_note = NULL_RTX;
6014 /* If the current location is the same as the end of the list,
6015 and either both or neither of the locations is uninitialized,
6016 we have nothing to do. */
6017 if (last_loc_note == NULL_RTX
6018 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6019 NOTE_VAR_LOCATION_LOC (loc_note)))
6020 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6021 != NOTE_VAR_LOCATION_STATUS (loc_note))
6022 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6023 == VAR_INIT_STATUS_UNINITIALIZED)
6024 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6025 == VAR_INIT_STATUS_UNINITIALIZED))))
6027 /* Add LOC to the end of list and update LAST. If the last
6028 element of the list has been removed above, reuse its
6029 memory for the new node, otherwise allocate a new one. */
6030 if (unused)
6032 loc = unused;
6033 memset (loc, '\0', sizeof (*loc));
6035 else
6036 loc = ggc_cleared_alloc<var_loc_node> ();
6037 if (bitsize == -1 || piece_loc == NULL)
6038 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6039 else
6040 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6041 bitpos, piece_bitpos, bitsize, loc_note);
6042 last->next = loc;
6043 /* Ensure TEMP->LAST will point either to the new last but one
6044 element of the chain, or to the last element in it. */
6045 if (last != temp->last)
6046 temp->last = last;
6048 else if (unused)
6049 ggc_free (unused);
6051 else
6053 loc = ggc_cleared_alloc<var_loc_node> ();
6054 temp->first = loc;
6055 temp->last = loc;
6056 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6058 return loc;
6061 /* Keep track of the number of spaces used to indent the
6062 output of the debugging routines that print the structure of
6063 the DIE internal representation. */
6064 static int print_indent;
6066 /* Indent the line the number of spaces given by print_indent. */
6068 static inline void
6069 print_spaces (FILE *outfile)
6071 fprintf (outfile, "%*s", print_indent, "");
6074 /* Print a type signature in hex. */
6076 static inline void
6077 print_signature (FILE *outfile, char *sig)
6079 int i;
6081 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6082 fprintf (outfile, "%02x", sig[i] & 0xff);
6085 static inline void
6086 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6088 if (discr_value->pos)
6089 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6090 else
6091 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6094 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6096 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6097 RECURSE, output location descriptor operations. */
6099 static void
6100 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6102 switch (val->val_class)
6104 case dw_val_class_addr:
6105 fprintf (outfile, "address");
6106 break;
6107 case dw_val_class_offset:
6108 fprintf (outfile, "offset");
6109 break;
6110 case dw_val_class_loc:
6111 fprintf (outfile, "location descriptor");
6112 if (val->v.val_loc == NULL)
6113 fprintf (outfile, " -> <null>\n");
6114 else if (recurse)
6116 fprintf (outfile, ":\n");
6117 print_indent += 4;
6118 print_loc_descr (val->v.val_loc, outfile);
6119 print_indent -= 4;
6121 else
6122 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6123 break;
6124 case dw_val_class_loc_list:
6125 fprintf (outfile, "location list -> label:%s",
6126 val->v.val_loc_list->ll_symbol);
6127 break;
6128 case dw_val_class_range_list:
6129 fprintf (outfile, "range list");
6130 break;
6131 case dw_val_class_const:
6132 case dw_val_class_const_implicit:
6133 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6134 break;
6135 case dw_val_class_unsigned_const:
6136 case dw_val_class_unsigned_const_implicit:
6137 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6138 break;
6139 case dw_val_class_const_double:
6140 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6141 HOST_WIDE_INT_PRINT_UNSIGNED")",
6142 val->v.val_double.high,
6143 val->v.val_double.low);
6144 break;
6145 case dw_val_class_wide_int:
6147 int i = val->v.val_wide->get_len ();
6148 fprintf (outfile, "constant (");
6149 gcc_assert (i > 0);
6150 if (val->v.val_wide->elt (i - 1) == 0)
6151 fprintf (outfile, "0x");
6152 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6153 val->v.val_wide->elt (--i));
6154 while (--i >= 0)
6155 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6156 val->v.val_wide->elt (i));
6157 fprintf (outfile, ")");
6158 break;
6160 case dw_val_class_vec:
6161 fprintf (outfile, "floating-point or vector constant");
6162 break;
6163 case dw_val_class_flag:
6164 fprintf (outfile, "%u", val->v.val_flag);
6165 break;
6166 case dw_val_class_die_ref:
6167 if (val->v.val_die_ref.die != NULL)
6169 dw_die_ref die = val->v.val_die_ref.die;
6171 if (die->comdat_type_p)
6173 fprintf (outfile, "die -> signature: ");
6174 print_signature (outfile,
6175 die->die_id.die_type_node->signature);
6177 else if (die->die_id.die_symbol)
6179 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6180 if (die->with_offset)
6181 fprintf (outfile, " + %ld", die->die_offset);
6183 else
6184 fprintf (outfile, "die -> %ld", die->die_offset);
6185 fprintf (outfile, " (%p)", (void *) die);
6187 else
6188 fprintf (outfile, "die -> <null>");
6189 break;
6190 case dw_val_class_vms_delta:
6191 fprintf (outfile, "delta: @slotcount(%s-%s)",
6192 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6193 break;
6194 case dw_val_class_lbl_id:
6195 case dw_val_class_lineptr:
6196 case dw_val_class_macptr:
6197 case dw_val_class_loclistsptr:
6198 case dw_val_class_high_pc:
6199 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6200 break;
6201 case dw_val_class_str:
6202 if (val->v.val_str->str != NULL)
6203 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6204 else
6205 fprintf (outfile, "<null>");
6206 break;
6207 case dw_val_class_file:
6208 case dw_val_class_file_implicit:
6209 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6210 val->v.val_file->emitted_number);
6211 break;
6212 case dw_val_class_data8:
6214 int i;
6216 for (i = 0; i < 8; i++)
6217 fprintf (outfile, "%02x", val->v.val_data8[i]);
6218 break;
6220 case dw_val_class_discr_value:
6221 print_discr_value (outfile, &val->v.val_discr_value);
6222 break;
6223 case dw_val_class_discr_list:
6224 for (dw_discr_list_ref node = val->v.val_discr_list;
6225 node != NULL;
6226 node = node->dw_discr_next)
6228 if (node->dw_discr_range)
6230 fprintf (outfile, " .. ");
6231 print_discr_value (outfile, &node->dw_discr_lower_bound);
6232 print_discr_value (outfile, &node->dw_discr_upper_bound);
6234 else
6235 print_discr_value (outfile, &node->dw_discr_lower_bound);
6237 if (node->dw_discr_next != NULL)
6238 fprintf (outfile, " | ");
6240 default:
6241 break;
6245 /* Likewise, for a DIE attribute. */
6247 static void
6248 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6250 print_dw_val (&a->dw_attr_val, recurse, outfile);
6254 /* Print the list of operands in the LOC location description to OUTFILE. This
6255 routine is a debugging aid only. */
6257 static void
6258 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6260 dw_loc_descr_ref l = loc;
6262 if (loc == NULL)
6264 print_spaces (outfile);
6265 fprintf (outfile, "<null>\n");
6266 return;
6269 for (l = loc; l != NULL; l = l->dw_loc_next)
6271 print_spaces (outfile);
6272 fprintf (outfile, "(%p) %s",
6273 (void *) l,
6274 dwarf_stack_op_name (l->dw_loc_opc));
6275 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6277 fprintf (outfile, " ");
6278 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6280 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6282 fprintf (outfile, ", ");
6283 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6285 fprintf (outfile, "\n");
6289 /* Print the information associated with a given DIE, and its children.
6290 This routine is a debugging aid only. */
6292 static void
6293 print_die (dw_die_ref die, FILE *outfile)
6295 dw_attr_node *a;
6296 dw_die_ref c;
6297 unsigned ix;
6299 print_spaces (outfile);
6300 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6301 die->die_offset, dwarf_tag_name (die->die_tag),
6302 (void*) die);
6303 print_spaces (outfile);
6304 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6305 fprintf (outfile, " offset: %ld", die->die_offset);
6306 fprintf (outfile, " mark: %d\n", die->die_mark);
6308 if (die->comdat_type_p)
6310 print_spaces (outfile);
6311 fprintf (outfile, " signature: ");
6312 print_signature (outfile, die->die_id.die_type_node->signature);
6313 fprintf (outfile, "\n");
6316 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6318 print_spaces (outfile);
6319 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6321 print_attribute (a, true, outfile);
6322 fprintf (outfile, "\n");
6325 if (die->die_child != NULL)
6327 print_indent += 4;
6328 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6329 print_indent -= 4;
6331 if (print_indent == 0)
6332 fprintf (outfile, "\n");
6335 /* Print the list of operations in the LOC location description. */
6337 DEBUG_FUNCTION void
6338 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6340 print_loc_descr (loc, stderr);
6343 /* Print the information collected for a given DIE. */
6345 DEBUG_FUNCTION void
6346 debug_dwarf_die (dw_die_ref die)
6348 print_die (die, stderr);
6351 DEBUG_FUNCTION void
6352 debug (die_struct &ref)
6354 print_die (&ref, stderr);
6357 DEBUG_FUNCTION void
6358 debug (die_struct *ptr)
6360 if (ptr)
6361 debug (*ptr);
6362 else
6363 fprintf (stderr, "<nil>\n");
6367 /* Print all DWARF information collected for the compilation unit.
6368 This routine is a debugging aid only. */
6370 DEBUG_FUNCTION void
6371 debug_dwarf (void)
6373 print_indent = 0;
6374 print_die (comp_unit_die (), stderr);
6377 /* Verify the DIE tree structure. */
6379 DEBUG_FUNCTION void
6380 verify_die (dw_die_ref die)
6382 gcc_assert (!die->die_mark);
6383 if (die->die_parent == NULL
6384 && die->die_sib == NULL)
6385 return;
6386 /* Verify the die_sib list is cyclic. */
6387 dw_die_ref x = die;
6390 x->die_mark = 1;
6391 x = x->die_sib;
6393 while (x && !x->die_mark);
6394 gcc_assert (x == die);
6395 x = die;
6398 /* Verify all dies have the same parent. */
6399 gcc_assert (x->die_parent == die->die_parent);
6400 if (x->die_child)
6402 /* Verify the child has the proper parent and recurse. */
6403 gcc_assert (x->die_child->die_parent == x);
6404 verify_die (x->die_child);
6406 x->die_mark = 0;
6407 x = x->die_sib;
6409 while (x && x->die_mark);
6412 /* Sanity checks on DIEs. */
6414 static void
6415 check_die (dw_die_ref die)
6417 unsigned ix;
6418 dw_attr_node *a;
6419 bool inline_found = false;
6420 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6421 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6422 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6424 switch (a->dw_attr)
6426 case DW_AT_inline:
6427 if (a->dw_attr_val.v.val_unsigned)
6428 inline_found = true;
6429 break;
6430 case DW_AT_location:
6431 ++n_location;
6432 break;
6433 case DW_AT_low_pc:
6434 ++n_low_pc;
6435 break;
6436 case DW_AT_high_pc:
6437 ++n_high_pc;
6438 break;
6439 case DW_AT_artificial:
6440 ++n_artificial;
6441 break;
6442 case DW_AT_decl_column:
6443 ++n_decl_column;
6444 break;
6445 case DW_AT_decl_line:
6446 ++n_decl_line;
6447 break;
6448 case DW_AT_decl_file:
6449 ++n_decl_file;
6450 break;
6451 default:
6452 break;
6455 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6456 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6458 fprintf (stderr, "Duplicate attributes in DIE:\n");
6459 debug_dwarf_die (die);
6460 gcc_unreachable ();
6462 if (inline_found)
6464 /* A debugging information entry that is a member of an abstract
6465 instance tree [that has DW_AT_inline] should not contain any
6466 attributes which describe aspects of the subroutine which vary
6467 between distinct inlined expansions or distinct out-of-line
6468 expansions. */
6469 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6470 gcc_assert (a->dw_attr != DW_AT_low_pc
6471 && a->dw_attr != DW_AT_high_pc
6472 && a->dw_attr != DW_AT_location
6473 && a->dw_attr != DW_AT_frame_base
6474 && a->dw_attr != DW_AT_call_all_calls
6475 && a->dw_attr != DW_AT_GNU_all_call_sites);
6479 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6480 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6481 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6483 /* Calculate the checksum of a location expression. */
6485 static inline void
6486 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6488 int tem;
6489 inchash::hash hstate;
6490 hashval_t hash;
6492 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6493 CHECKSUM (tem);
6494 hash_loc_operands (loc, hstate);
6495 hash = hstate.end();
6496 CHECKSUM (hash);
6499 /* Calculate the checksum of an attribute. */
6501 static void
6502 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6504 dw_loc_descr_ref loc;
6505 rtx r;
6507 CHECKSUM (at->dw_attr);
6509 /* We don't care that this was compiled with a different compiler
6510 snapshot; if the output is the same, that's what matters. */
6511 if (at->dw_attr == DW_AT_producer)
6512 return;
6514 switch (AT_class (at))
6516 case dw_val_class_const:
6517 case dw_val_class_const_implicit:
6518 CHECKSUM (at->dw_attr_val.v.val_int);
6519 break;
6520 case dw_val_class_unsigned_const:
6521 case dw_val_class_unsigned_const_implicit:
6522 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6523 break;
6524 case dw_val_class_const_double:
6525 CHECKSUM (at->dw_attr_val.v.val_double);
6526 break;
6527 case dw_val_class_wide_int:
6528 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6529 get_full_len (*at->dw_attr_val.v.val_wide)
6530 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6531 break;
6532 case dw_val_class_vec:
6533 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6534 (at->dw_attr_val.v.val_vec.length
6535 * at->dw_attr_val.v.val_vec.elt_size));
6536 break;
6537 case dw_val_class_flag:
6538 CHECKSUM (at->dw_attr_val.v.val_flag);
6539 break;
6540 case dw_val_class_str:
6541 CHECKSUM_STRING (AT_string (at));
6542 break;
6544 case dw_val_class_addr:
6545 r = AT_addr (at);
6546 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6547 CHECKSUM_STRING (XSTR (r, 0));
6548 break;
6550 case dw_val_class_offset:
6551 CHECKSUM (at->dw_attr_val.v.val_offset);
6552 break;
6554 case dw_val_class_loc:
6555 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6556 loc_checksum (loc, ctx);
6557 break;
6559 case dw_val_class_die_ref:
6560 die_checksum (AT_ref (at), ctx, mark);
6561 break;
6563 case dw_val_class_fde_ref:
6564 case dw_val_class_vms_delta:
6565 case dw_val_class_lbl_id:
6566 case dw_val_class_lineptr:
6567 case dw_val_class_macptr:
6568 case dw_val_class_loclistsptr:
6569 case dw_val_class_high_pc:
6570 break;
6572 case dw_val_class_file:
6573 case dw_val_class_file_implicit:
6574 CHECKSUM_STRING (AT_file (at)->filename);
6575 break;
6577 case dw_val_class_data8:
6578 CHECKSUM (at->dw_attr_val.v.val_data8);
6579 break;
6581 default:
6582 break;
6586 /* Calculate the checksum of a DIE. */
6588 static void
6589 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6591 dw_die_ref c;
6592 dw_attr_node *a;
6593 unsigned ix;
6595 /* To avoid infinite recursion. */
6596 if (die->die_mark)
6598 CHECKSUM (die->die_mark);
6599 return;
6601 die->die_mark = ++(*mark);
6603 CHECKSUM (die->die_tag);
6605 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6606 attr_checksum (a, ctx, mark);
6608 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6611 #undef CHECKSUM
6612 #undef CHECKSUM_BLOCK
6613 #undef CHECKSUM_STRING
6615 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6616 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6617 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6618 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6619 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6620 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6621 #define CHECKSUM_ATTR(FOO) \
6622 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6624 /* Calculate the checksum of a number in signed LEB128 format. */
6626 static void
6627 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6629 unsigned char byte;
6630 bool more;
6632 while (1)
6634 byte = (value & 0x7f);
6635 value >>= 7;
6636 more = !((value == 0 && (byte & 0x40) == 0)
6637 || (value == -1 && (byte & 0x40) != 0));
6638 if (more)
6639 byte |= 0x80;
6640 CHECKSUM (byte);
6641 if (!more)
6642 break;
6646 /* Calculate the checksum of a number in unsigned LEB128 format. */
6648 static void
6649 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6651 while (1)
6653 unsigned char byte = (value & 0x7f);
6654 value >>= 7;
6655 if (value != 0)
6656 /* More bytes to follow. */
6657 byte |= 0x80;
6658 CHECKSUM (byte);
6659 if (value == 0)
6660 break;
6664 /* Checksum the context of the DIE. This adds the names of any
6665 surrounding namespaces or structures to the checksum. */
6667 static void
6668 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6670 const char *name;
6671 dw_die_ref spec;
6672 int tag = die->die_tag;
6674 if (tag != DW_TAG_namespace
6675 && tag != DW_TAG_structure_type
6676 && tag != DW_TAG_class_type)
6677 return;
6679 name = get_AT_string (die, DW_AT_name);
6681 spec = get_AT_ref (die, DW_AT_specification);
6682 if (spec != NULL)
6683 die = spec;
6685 if (die->die_parent != NULL)
6686 checksum_die_context (die->die_parent, ctx);
6688 CHECKSUM_ULEB128 ('C');
6689 CHECKSUM_ULEB128 (tag);
6690 if (name != NULL)
6691 CHECKSUM_STRING (name);
6694 /* Calculate the checksum of a location expression. */
6696 static inline void
6697 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6699 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6700 were emitted as a DW_FORM_sdata instead of a location expression. */
6701 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6703 CHECKSUM_ULEB128 (DW_FORM_sdata);
6704 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6705 return;
6708 /* Otherwise, just checksum the raw location expression. */
6709 while (loc != NULL)
6711 inchash::hash hstate;
6712 hashval_t hash;
6714 CHECKSUM_ULEB128 (loc->dtprel);
6715 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6716 hash_loc_operands (loc, hstate);
6717 hash = hstate.end ();
6718 CHECKSUM (hash);
6719 loc = loc->dw_loc_next;
6723 /* Calculate the checksum of an attribute. */
6725 static void
6726 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6727 struct md5_ctx *ctx, int *mark)
6729 dw_loc_descr_ref loc;
6730 rtx r;
6732 if (AT_class (at) == dw_val_class_die_ref)
6734 dw_die_ref target_die = AT_ref (at);
6736 /* For pointer and reference types, we checksum only the (qualified)
6737 name of the target type (if there is a name). For friend entries,
6738 we checksum only the (qualified) name of the target type or function.
6739 This allows the checksum to remain the same whether the target type
6740 is complete or not. */
6741 if ((at->dw_attr == DW_AT_type
6742 && (tag == DW_TAG_pointer_type
6743 || tag == DW_TAG_reference_type
6744 || tag == DW_TAG_rvalue_reference_type
6745 || tag == DW_TAG_ptr_to_member_type))
6746 || (at->dw_attr == DW_AT_friend
6747 && tag == DW_TAG_friend))
6749 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6751 if (name_attr != NULL)
6753 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6755 if (decl == NULL)
6756 decl = target_die;
6757 CHECKSUM_ULEB128 ('N');
6758 CHECKSUM_ULEB128 (at->dw_attr);
6759 if (decl->die_parent != NULL)
6760 checksum_die_context (decl->die_parent, ctx);
6761 CHECKSUM_ULEB128 ('E');
6762 CHECKSUM_STRING (AT_string (name_attr));
6763 return;
6767 /* For all other references to another DIE, we check to see if the
6768 target DIE has already been visited. If it has, we emit a
6769 backward reference; if not, we descend recursively. */
6770 if (target_die->die_mark > 0)
6772 CHECKSUM_ULEB128 ('R');
6773 CHECKSUM_ULEB128 (at->dw_attr);
6774 CHECKSUM_ULEB128 (target_die->die_mark);
6776 else
6778 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6780 if (decl == NULL)
6781 decl = target_die;
6782 target_die->die_mark = ++(*mark);
6783 CHECKSUM_ULEB128 ('T');
6784 CHECKSUM_ULEB128 (at->dw_attr);
6785 if (decl->die_parent != NULL)
6786 checksum_die_context (decl->die_parent, ctx);
6787 die_checksum_ordered (target_die, ctx, mark);
6789 return;
6792 CHECKSUM_ULEB128 ('A');
6793 CHECKSUM_ULEB128 (at->dw_attr);
6795 switch (AT_class (at))
6797 case dw_val_class_const:
6798 case dw_val_class_const_implicit:
6799 CHECKSUM_ULEB128 (DW_FORM_sdata);
6800 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6801 break;
6803 case dw_val_class_unsigned_const:
6804 case dw_val_class_unsigned_const_implicit:
6805 CHECKSUM_ULEB128 (DW_FORM_sdata);
6806 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6807 break;
6809 case dw_val_class_const_double:
6810 CHECKSUM_ULEB128 (DW_FORM_block);
6811 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6812 CHECKSUM (at->dw_attr_val.v.val_double);
6813 break;
6815 case dw_val_class_wide_int:
6816 CHECKSUM_ULEB128 (DW_FORM_block);
6817 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6818 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6819 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6820 get_full_len (*at->dw_attr_val.v.val_wide)
6821 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6822 break;
6824 case dw_val_class_vec:
6825 CHECKSUM_ULEB128 (DW_FORM_block);
6826 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6827 * at->dw_attr_val.v.val_vec.elt_size);
6828 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6829 (at->dw_attr_val.v.val_vec.length
6830 * at->dw_attr_val.v.val_vec.elt_size));
6831 break;
6833 case dw_val_class_flag:
6834 CHECKSUM_ULEB128 (DW_FORM_flag);
6835 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6836 break;
6838 case dw_val_class_str:
6839 CHECKSUM_ULEB128 (DW_FORM_string);
6840 CHECKSUM_STRING (AT_string (at));
6841 break;
6843 case dw_val_class_addr:
6844 r = AT_addr (at);
6845 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6846 CHECKSUM_ULEB128 (DW_FORM_string);
6847 CHECKSUM_STRING (XSTR (r, 0));
6848 break;
6850 case dw_val_class_offset:
6851 CHECKSUM_ULEB128 (DW_FORM_sdata);
6852 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6853 break;
6855 case dw_val_class_loc:
6856 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6857 loc_checksum_ordered (loc, ctx);
6858 break;
6860 case dw_val_class_fde_ref:
6861 case dw_val_class_lbl_id:
6862 case dw_val_class_lineptr:
6863 case dw_val_class_macptr:
6864 case dw_val_class_loclistsptr:
6865 case dw_val_class_high_pc:
6866 break;
6868 case dw_val_class_file:
6869 case dw_val_class_file_implicit:
6870 CHECKSUM_ULEB128 (DW_FORM_string);
6871 CHECKSUM_STRING (AT_file (at)->filename);
6872 break;
6874 case dw_val_class_data8:
6875 CHECKSUM (at->dw_attr_val.v.val_data8);
6876 break;
6878 default:
6879 break;
6883 struct checksum_attributes
6885 dw_attr_node *at_name;
6886 dw_attr_node *at_type;
6887 dw_attr_node *at_friend;
6888 dw_attr_node *at_accessibility;
6889 dw_attr_node *at_address_class;
6890 dw_attr_node *at_alignment;
6891 dw_attr_node *at_allocated;
6892 dw_attr_node *at_artificial;
6893 dw_attr_node *at_associated;
6894 dw_attr_node *at_binary_scale;
6895 dw_attr_node *at_bit_offset;
6896 dw_attr_node *at_bit_size;
6897 dw_attr_node *at_bit_stride;
6898 dw_attr_node *at_byte_size;
6899 dw_attr_node *at_byte_stride;
6900 dw_attr_node *at_const_value;
6901 dw_attr_node *at_containing_type;
6902 dw_attr_node *at_count;
6903 dw_attr_node *at_data_location;
6904 dw_attr_node *at_data_member_location;
6905 dw_attr_node *at_decimal_scale;
6906 dw_attr_node *at_decimal_sign;
6907 dw_attr_node *at_default_value;
6908 dw_attr_node *at_digit_count;
6909 dw_attr_node *at_discr;
6910 dw_attr_node *at_discr_list;
6911 dw_attr_node *at_discr_value;
6912 dw_attr_node *at_encoding;
6913 dw_attr_node *at_endianity;
6914 dw_attr_node *at_explicit;
6915 dw_attr_node *at_is_optional;
6916 dw_attr_node *at_location;
6917 dw_attr_node *at_lower_bound;
6918 dw_attr_node *at_mutable;
6919 dw_attr_node *at_ordering;
6920 dw_attr_node *at_picture_string;
6921 dw_attr_node *at_prototyped;
6922 dw_attr_node *at_small;
6923 dw_attr_node *at_segment;
6924 dw_attr_node *at_string_length;
6925 dw_attr_node *at_string_length_bit_size;
6926 dw_attr_node *at_string_length_byte_size;
6927 dw_attr_node *at_threads_scaled;
6928 dw_attr_node *at_upper_bound;
6929 dw_attr_node *at_use_location;
6930 dw_attr_node *at_use_UTF8;
6931 dw_attr_node *at_variable_parameter;
6932 dw_attr_node *at_virtuality;
6933 dw_attr_node *at_visibility;
6934 dw_attr_node *at_vtable_elem_location;
6937 /* Collect the attributes that we will want to use for the checksum. */
6939 static void
6940 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6942 dw_attr_node *a;
6943 unsigned ix;
6945 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6947 switch (a->dw_attr)
6949 case DW_AT_name:
6950 attrs->at_name = a;
6951 break;
6952 case DW_AT_type:
6953 attrs->at_type = a;
6954 break;
6955 case DW_AT_friend:
6956 attrs->at_friend = a;
6957 break;
6958 case DW_AT_accessibility:
6959 attrs->at_accessibility = a;
6960 break;
6961 case DW_AT_address_class:
6962 attrs->at_address_class = a;
6963 break;
6964 case DW_AT_alignment:
6965 attrs->at_alignment = a;
6966 break;
6967 case DW_AT_allocated:
6968 attrs->at_allocated = a;
6969 break;
6970 case DW_AT_artificial:
6971 attrs->at_artificial = a;
6972 break;
6973 case DW_AT_associated:
6974 attrs->at_associated = a;
6975 break;
6976 case DW_AT_binary_scale:
6977 attrs->at_binary_scale = a;
6978 break;
6979 case DW_AT_bit_offset:
6980 attrs->at_bit_offset = a;
6981 break;
6982 case DW_AT_bit_size:
6983 attrs->at_bit_size = a;
6984 break;
6985 case DW_AT_bit_stride:
6986 attrs->at_bit_stride = a;
6987 break;
6988 case DW_AT_byte_size:
6989 attrs->at_byte_size = a;
6990 break;
6991 case DW_AT_byte_stride:
6992 attrs->at_byte_stride = a;
6993 break;
6994 case DW_AT_const_value:
6995 attrs->at_const_value = a;
6996 break;
6997 case DW_AT_containing_type:
6998 attrs->at_containing_type = a;
6999 break;
7000 case DW_AT_count:
7001 attrs->at_count = a;
7002 break;
7003 case DW_AT_data_location:
7004 attrs->at_data_location = a;
7005 break;
7006 case DW_AT_data_member_location:
7007 attrs->at_data_member_location = a;
7008 break;
7009 case DW_AT_decimal_scale:
7010 attrs->at_decimal_scale = a;
7011 break;
7012 case DW_AT_decimal_sign:
7013 attrs->at_decimal_sign = a;
7014 break;
7015 case DW_AT_default_value:
7016 attrs->at_default_value = a;
7017 break;
7018 case DW_AT_digit_count:
7019 attrs->at_digit_count = a;
7020 break;
7021 case DW_AT_discr:
7022 attrs->at_discr = a;
7023 break;
7024 case DW_AT_discr_list:
7025 attrs->at_discr_list = a;
7026 break;
7027 case DW_AT_discr_value:
7028 attrs->at_discr_value = a;
7029 break;
7030 case DW_AT_encoding:
7031 attrs->at_encoding = a;
7032 break;
7033 case DW_AT_endianity:
7034 attrs->at_endianity = a;
7035 break;
7036 case DW_AT_explicit:
7037 attrs->at_explicit = a;
7038 break;
7039 case DW_AT_is_optional:
7040 attrs->at_is_optional = a;
7041 break;
7042 case DW_AT_location:
7043 attrs->at_location = a;
7044 break;
7045 case DW_AT_lower_bound:
7046 attrs->at_lower_bound = a;
7047 break;
7048 case DW_AT_mutable:
7049 attrs->at_mutable = a;
7050 break;
7051 case DW_AT_ordering:
7052 attrs->at_ordering = a;
7053 break;
7054 case DW_AT_picture_string:
7055 attrs->at_picture_string = a;
7056 break;
7057 case DW_AT_prototyped:
7058 attrs->at_prototyped = a;
7059 break;
7060 case DW_AT_small:
7061 attrs->at_small = a;
7062 break;
7063 case DW_AT_segment:
7064 attrs->at_segment = a;
7065 break;
7066 case DW_AT_string_length:
7067 attrs->at_string_length = a;
7068 break;
7069 case DW_AT_string_length_bit_size:
7070 attrs->at_string_length_bit_size = a;
7071 break;
7072 case DW_AT_string_length_byte_size:
7073 attrs->at_string_length_byte_size = a;
7074 break;
7075 case DW_AT_threads_scaled:
7076 attrs->at_threads_scaled = a;
7077 break;
7078 case DW_AT_upper_bound:
7079 attrs->at_upper_bound = a;
7080 break;
7081 case DW_AT_use_location:
7082 attrs->at_use_location = a;
7083 break;
7084 case DW_AT_use_UTF8:
7085 attrs->at_use_UTF8 = a;
7086 break;
7087 case DW_AT_variable_parameter:
7088 attrs->at_variable_parameter = a;
7089 break;
7090 case DW_AT_virtuality:
7091 attrs->at_virtuality = a;
7092 break;
7093 case DW_AT_visibility:
7094 attrs->at_visibility = a;
7095 break;
7096 case DW_AT_vtable_elem_location:
7097 attrs->at_vtable_elem_location = a;
7098 break;
7099 default:
7100 break;
7105 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7107 static void
7108 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7110 dw_die_ref c;
7111 dw_die_ref decl;
7112 struct checksum_attributes attrs;
7114 CHECKSUM_ULEB128 ('D');
7115 CHECKSUM_ULEB128 (die->die_tag);
7117 memset (&attrs, 0, sizeof (attrs));
7119 decl = get_AT_ref (die, DW_AT_specification);
7120 if (decl != NULL)
7121 collect_checksum_attributes (&attrs, decl);
7122 collect_checksum_attributes (&attrs, die);
7124 CHECKSUM_ATTR (attrs.at_name);
7125 CHECKSUM_ATTR (attrs.at_accessibility);
7126 CHECKSUM_ATTR (attrs.at_address_class);
7127 CHECKSUM_ATTR (attrs.at_allocated);
7128 CHECKSUM_ATTR (attrs.at_artificial);
7129 CHECKSUM_ATTR (attrs.at_associated);
7130 CHECKSUM_ATTR (attrs.at_binary_scale);
7131 CHECKSUM_ATTR (attrs.at_bit_offset);
7132 CHECKSUM_ATTR (attrs.at_bit_size);
7133 CHECKSUM_ATTR (attrs.at_bit_stride);
7134 CHECKSUM_ATTR (attrs.at_byte_size);
7135 CHECKSUM_ATTR (attrs.at_byte_stride);
7136 CHECKSUM_ATTR (attrs.at_const_value);
7137 CHECKSUM_ATTR (attrs.at_containing_type);
7138 CHECKSUM_ATTR (attrs.at_count);
7139 CHECKSUM_ATTR (attrs.at_data_location);
7140 CHECKSUM_ATTR (attrs.at_data_member_location);
7141 CHECKSUM_ATTR (attrs.at_decimal_scale);
7142 CHECKSUM_ATTR (attrs.at_decimal_sign);
7143 CHECKSUM_ATTR (attrs.at_default_value);
7144 CHECKSUM_ATTR (attrs.at_digit_count);
7145 CHECKSUM_ATTR (attrs.at_discr);
7146 CHECKSUM_ATTR (attrs.at_discr_list);
7147 CHECKSUM_ATTR (attrs.at_discr_value);
7148 CHECKSUM_ATTR (attrs.at_encoding);
7149 CHECKSUM_ATTR (attrs.at_endianity);
7150 CHECKSUM_ATTR (attrs.at_explicit);
7151 CHECKSUM_ATTR (attrs.at_is_optional);
7152 CHECKSUM_ATTR (attrs.at_location);
7153 CHECKSUM_ATTR (attrs.at_lower_bound);
7154 CHECKSUM_ATTR (attrs.at_mutable);
7155 CHECKSUM_ATTR (attrs.at_ordering);
7156 CHECKSUM_ATTR (attrs.at_picture_string);
7157 CHECKSUM_ATTR (attrs.at_prototyped);
7158 CHECKSUM_ATTR (attrs.at_small);
7159 CHECKSUM_ATTR (attrs.at_segment);
7160 CHECKSUM_ATTR (attrs.at_string_length);
7161 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7162 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7163 CHECKSUM_ATTR (attrs.at_threads_scaled);
7164 CHECKSUM_ATTR (attrs.at_upper_bound);
7165 CHECKSUM_ATTR (attrs.at_use_location);
7166 CHECKSUM_ATTR (attrs.at_use_UTF8);
7167 CHECKSUM_ATTR (attrs.at_variable_parameter);
7168 CHECKSUM_ATTR (attrs.at_virtuality);
7169 CHECKSUM_ATTR (attrs.at_visibility);
7170 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7171 CHECKSUM_ATTR (attrs.at_type);
7172 CHECKSUM_ATTR (attrs.at_friend);
7173 CHECKSUM_ATTR (attrs.at_alignment);
7175 /* Checksum the child DIEs. */
7176 c = die->die_child;
7177 if (c) do {
7178 dw_attr_node *name_attr;
7180 c = c->die_sib;
7181 name_attr = get_AT (c, DW_AT_name);
7182 if (is_template_instantiation (c))
7184 /* Ignore instantiations of member type and function templates. */
7186 else if (name_attr != NULL
7187 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7189 /* Use a shallow checksum for named nested types and member
7190 functions. */
7191 CHECKSUM_ULEB128 ('S');
7192 CHECKSUM_ULEB128 (c->die_tag);
7193 CHECKSUM_STRING (AT_string (name_attr));
7195 else
7197 /* Use a deep checksum for other children. */
7198 /* Mark this DIE so it gets processed when unmarking. */
7199 if (c->die_mark == 0)
7200 c->die_mark = -1;
7201 die_checksum_ordered (c, ctx, mark);
7203 } while (c != die->die_child);
7205 CHECKSUM_ULEB128 (0);
7208 /* Add a type name and tag to a hash. */
7209 static void
7210 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7212 CHECKSUM_ULEB128 (tag);
7213 CHECKSUM_STRING (name);
7216 #undef CHECKSUM
7217 #undef CHECKSUM_STRING
7218 #undef CHECKSUM_ATTR
7219 #undef CHECKSUM_LEB128
7220 #undef CHECKSUM_ULEB128
7222 /* Generate the type signature for DIE. This is computed by generating an
7223 MD5 checksum over the DIE's tag, its relevant attributes, and its
7224 children. Attributes that are references to other DIEs are processed
7225 by recursion, using the MARK field to prevent infinite recursion.
7226 If the DIE is nested inside a namespace or another type, we also
7227 need to include that context in the signature. The lower 64 bits
7228 of the resulting MD5 checksum comprise the signature. */
7230 static void
7231 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7233 int mark;
7234 const char *name;
7235 unsigned char checksum[16];
7236 struct md5_ctx ctx;
7237 dw_die_ref decl;
7238 dw_die_ref parent;
7240 name = get_AT_string (die, DW_AT_name);
7241 decl = get_AT_ref (die, DW_AT_specification);
7242 parent = get_die_parent (die);
7244 /* First, compute a signature for just the type name (and its surrounding
7245 context, if any. This is stored in the type unit DIE for link-time
7246 ODR (one-definition rule) checking. */
7248 if (is_cxx () && name != NULL)
7250 md5_init_ctx (&ctx);
7252 /* Checksum the names of surrounding namespaces and structures. */
7253 if (parent != NULL)
7254 checksum_die_context (parent, &ctx);
7256 /* Checksum the current DIE. */
7257 die_odr_checksum (die->die_tag, name, &ctx);
7258 md5_finish_ctx (&ctx, checksum);
7260 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7263 /* Next, compute the complete type signature. */
7265 md5_init_ctx (&ctx);
7266 mark = 1;
7267 die->die_mark = mark;
7269 /* Checksum the names of surrounding namespaces and structures. */
7270 if (parent != NULL)
7271 checksum_die_context (parent, &ctx);
7273 /* Checksum the DIE and its children. */
7274 die_checksum_ordered (die, &ctx, &mark);
7275 unmark_all_dies (die);
7276 md5_finish_ctx (&ctx, checksum);
7278 /* Store the signature in the type node and link the type DIE and the
7279 type node together. */
7280 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7281 DWARF_TYPE_SIGNATURE_SIZE);
7282 die->comdat_type_p = true;
7283 die->die_id.die_type_node = type_node;
7284 type_node->type_die = die;
7286 /* If the DIE is a specification, link its declaration to the type node
7287 as well. */
7288 if (decl != NULL)
7290 decl->comdat_type_p = true;
7291 decl->die_id.die_type_node = type_node;
7295 /* Do the location expressions look same? */
7296 static inline int
7297 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7299 return loc1->dw_loc_opc == loc2->dw_loc_opc
7300 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7301 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7304 /* Do the values look the same? */
7305 static int
7306 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7308 dw_loc_descr_ref loc1, loc2;
7309 rtx r1, r2;
7311 if (v1->val_class != v2->val_class)
7312 return 0;
7314 switch (v1->val_class)
7316 case dw_val_class_const:
7317 case dw_val_class_const_implicit:
7318 return v1->v.val_int == v2->v.val_int;
7319 case dw_val_class_unsigned_const:
7320 case dw_val_class_unsigned_const_implicit:
7321 return v1->v.val_unsigned == v2->v.val_unsigned;
7322 case dw_val_class_const_double:
7323 return v1->v.val_double.high == v2->v.val_double.high
7324 && v1->v.val_double.low == v2->v.val_double.low;
7325 case dw_val_class_wide_int:
7326 return *v1->v.val_wide == *v2->v.val_wide;
7327 case dw_val_class_vec:
7328 if (v1->v.val_vec.length != v2->v.val_vec.length
7329 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7330 return 0;
7331 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7332 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7333 return 0;
7334 return 1;
7335 case dw_val_class_flag:
7336 return v1->v.val_flag == v2->v.val_flag;
7337 case dw_val_class_str:
7338 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7340 case dw_val_class_addr:
7341 r1 = v1->v.val_addr;
7342 r2 = v2->v.val_addr;
7343 if (GET_CODE (r1) != GET_CODE (r2))
7344 return 0;
7345 return !rtx_equal_p (r1, r2);
7347 case dw_val_class_offset:
7348 return v1->v.val_offset == v2->v.val_offset;
7350 case dw_val_class_loc:
7351 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7352 loc1 && loc2;
7353 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7354 if (!same_loc_p (loc1, loc2, mark))
7355 return 0;
7356 return !loc1 && !loc2;
7358 case dw_val_class_die_ref:
7359 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7361 case dw_val_class_fde_ref:
7362 case dw_val_class_vms_delta:
7363 case dw_val_class_lbl_id:
7364 case dw_val_class_lineptr:
7365 case dw_val_class_macptr:
7366 case dw_val_class_loclistsptr:
7367 case dw_val_class_high_pc:
7368 return 1;
7370 case dw_val_class_file:
7371 case dw_val_class_file_implicit:
7372 return v1->v.val_file == v2->v.val_file;
7374 case dw_val_class_data8:
7375 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7377 default:
7378 return 1;
7382 /* Do the attributes look the same? */
7384 static int
7385 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7387 if (at1->dw_attr != at2->dw_attr)
7388 return 0;
7390 /* We don't care that this was compiled with a different compiler
7391 snapshot; if the output is the same, that's what matters. */
7392 if (at1->dw_attr == DW_AT_producer)
7393 return 1;
7395 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7398 /* Do the dies look the same? */
7400 static int
7401 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7403 dw_die_ref c1, c2;
7404 dw_attr_node *a1;
7405 unsigned ix;
7407 /* To avoid infinite recursion. */
7408 if (die1->die_mark)
7409 return die1->die_mark == die2->die_mark;
7410 die1->die_mark = die2->die_mark = ++(*mark);
7412 if (die1->die_tag != die2->die_tag)
7413 return 0;
7415 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7416 return 0;
7418 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7419 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7420 return 0;
7422 c1 = die1->die_child;
7423 c2 = die2->die_child;
7424 if (! c1)
7426 if (c2)
7427 return 0;
7429 else
7430 for (;;)
7432 if (!same_die_p (c1, c2, mark))
7433 return 0;
7434 c1 = c1->die_sib;
7435 c2 = c2->die_sib;
7436 if (c1 == die1->die_child)
7438 if (c2 == die2->die_child)
7439 break;
7440 else
7441 return 0;
7445 return 1;
7448 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7449 children, and set die_symbol. */
7451 static void
7452 compute_comp_unit_symbol (dw_die_ref unit_die)
7454 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7455 const char *base = die_name ? lbasename (die_name) : "anonymous";
7456 char *name = XALLOCAVEC (char, strlen (base) + 64);
7457 char *p;
7458 int i, mark;
7459 unsigned char checksum[16];
7460 struct md5_ctx ctx;
7462 /* Compute the checksum of the DIE, then append part of it as hex digits to
7463 the name filename of the unit. */
7465 md5_init_ctx (&ctx);
7466 mark = 0;
7467 die_checksum (unit_die, &ctx, &mark);
7468 unmark_all_dies (unit_die);
7469 md5_finish_ctx (&ctx, checksum);
7471 /* When we this for comp_unit_die () we have a DW_AT_name that might
7472 not start with a letter but with anything valid for filenames and
7473 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7474 character is not a letter. */
7475 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7476 clean_symbol_name (name);
7478 p = name + strlen (name);
7479 for (i = 0; i < 4; i++)
7481 sprintf (p, "%.2x", checksum[i]);
7482 p += 2;
7485 unit_die->die_id.die_symbol = xstrdup (name);
7488 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7490 static int
7491 is_type_die (dw_die_ref die)
7493 switch (die->die_tag)
7495 case DW_TAG_array_type:
7496 case DW_TAG_class_type:
7497 case DW_TAG_interface_type:
7498 case DW_TAG_enumeration_type:
7499 case DW_TAG_pointer_type:
7500 case DW_TAG_reference_type:
7501 case DW_TAG_rvalue_reference_type:
7502 case DW_TAG_string_type:
7503 case DW_TAG_structure_type:
7504 case DW_TAG_subroutine_type:
7505 case DW_TAG_union_type:
7506 case DW_TAG_ptr_to_member_type:
7507 case DW_TAG_set_type:
7508 case DW_TAG_subrange_type:
7509 case DW_TAG_base_type:
7510 case DW_TAG_const_type:
7511 case DW_TAG_file_type:
7512 case DW_TAG_packed_type:
7513 case DW_TAG_volatile_type:
7514 case DW_TAG_typedef:
7515 return 1;
7516 default:
7517 return 0;
7521 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7522 Basically, we want to choose the bits that are likely to be shared between
7523 compilations (types) and leave out the bits that are specific to individual
7524 compilations (functions). */
7526 static int
7527 is_comdat_die (dw_die_ref c)
7529 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7530 we do for stabs. The advantage is a greater likelihood of sharing between
7531 objects that don't include headers in the same order (and therefore would
7532 put the base types in a different comdat). jason 8/28/00 */
7534 if (c->die_tag == DW_TAG_base_type)
7535 return 0;
7537 if (c->die_tag == DW_TAG_pointer_type
7538 || c->die_tag == DW_TAG_reference_type
7539 || c->die_tag == DW_TAG_rvalue_reference_type
7540 || c->die_tag == DW_TAG_const_type
7541 || c->die_tag == DW_TAG_volatile_type)
7543 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7545 return t ? is_comdat_die (t) : 0;
7548 return is_type_die (c);
7551 /* Returns true iff C is a compile-unit DIE. */
7553 static inline bool
7554 is_cu_die (dw_die_ref c)
7556 return c && (c->die_tag == DW_TAG_compile_unit
7557 || c->die_tag == DW_TAG_skeleton_unit);
7560 /* Returns true iff C is a unit DIE of some sort. */
7562 static inline bool
7563 is_unit_die (dw_die_ref c)
7565 return c && (c->die_tag == DW_TAG_compile_unit
7566 || c->die_tag == DW_TAG_partial_unit
7567 || c->die_tag == DW_TAG_type_unit
7568 || c->die_tag == DW_TAG_skeleton_unit);
7571 /* Returns true iff C is a namespace DIE. */
7573 static inline bool
7574 is_namespace_die (dw_die_ref c)
7576 return c && c->die_tag == DW_TAG_namespace;
7579 /* Returns true iff C is a class or structure DIE. */
7581 static inline bool
7582 is_class_die (dw_die_ref c)
7584 return c && (c->die_tag == DW_TAG_class_type
7585 || c->die_tag == DW_TAG_structure_type);
7588 /* Return non-zero if this DIE is a template parameter. */
7590 static inline bool
7591 is_template_parameter (dw_die_ref die)
7593 switch (die->die_tag)
7595 case DW_TAG_template_type_param:
7596 case DW_TAG_template_value_param:
7597 case DW_TAG_GNU_template_template_param:
7598 case DW_TAG_GNU_template_parameter_pack:
7599 return true;
7600 default:
7601 return false;
7605 /* Return non-zero if this DIE represents a template instantiation. */
7607 static inline bool
7608 is_template_instantiation (dw_die_ref die)
7610 dw_die_ref c;
7612 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7613 return false;
7614 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7615 return false;
7618 static char *
7619 gen_internal_sym (const char *prefix)
7621 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7623 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7624 return xstrdup (buf);
7627 /* Return non-zero if this DIE is a declaration. */
7629 static int
7630 is_declaration_die (dw_die_ref die)
7632 dw_attr_node *a;
7633 unsigned ix;
7635 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7636 if (a->dw_attr == DW_AT_declaration)
7637 return 1;
7639 return 0;
7642 /* Return non-zero if this DIE is nested inside a subprogram. */
7644 static int
7645 is_nested_in_subprogram (dw_die_ref die)
7647 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7649 if (decl == NULL)
7650 decl = die;
7651 return local_scope_p (decl);
7654 /* Return non-zero if this DIE contains a defining declaration of a
7655 subprogram. */
7657 static int
7658 contains_subprogram_definition (dw_die_ref die)
7660 dw_die_ref c;
7662 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7663 return 1;
7664 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7665 return 0;
7668 /* Return non-zero if this is a type DIE that should be moved to a
7669 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7670 unit type. */
7672 static int
7673 should_move_die_to_comdat (dw_die_ref die)
7675 switch (die->die_tag)
7677 case DW_TAG_class_type:
7678 case DW_TAG_structure_type:
7679 case DW_TAG_enumeration_type:
7680 case DW_TAG_union_type:
7681 /* Don't move declarations, inlined instances, types nested in a
7682 subprogram, or types that contain subprogram definitions. */
7683 if (is_declaration_die (die)
7684 || get_AT (die, DW_AT_abstract_origin)
7685 || is_nested_in_subprogram (die)
7686 || contains_subprogram_definition (die))
7687 return 0;
7688 return 1;
7689 case DW_TAG_array_type:
7690 case DW_TAG_interface_type:
7691 case DW_TAG_pointer_type:
7692 case DW_TAG_reference_type:
7693 case DW_TAG_rvalue_reference_type:
7694 case DW_TAG_string_type:
7695 case DW_TAG_subroutine_type:
7696 case DW_TAG_ptr_to_member_type:
7697 case DW_TAG_set_type:
7698 case DW_TAG_subrange_type:
7699 case DW_TAG_base_type:
7700 case DW_TAG_const_type:
7701 case DW_TAG_file_type:
7702 case DW_TAG_packed_type:
7703 case DW_TAG_volatile_type:
7704 case DW_TAG_typedef:
7705 default:
7706 return 0;
7710 /* Make a clone of DIE. */
7712 static dw_die_ref
7713 clone_die (dw_die_ref die)
7715 dw_die_ref clone;
7716 dw_attr_node *a;
7717 unsigned ix;
7719 clone = ggc_cleared_alloc<die_node> ();
7720 clone->die_tag = die->die_tag;
7722 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7723 add_dwarf_attr (clone, a);
7725 return clone;
7728 /* Make a clone of the tree rooted at DIE. */
7730 static dw_die_ref
7731 clone_tree (dw_die_ref die)
7733 dw_die_ref c;
7734 dw_die_ref clone = clone_die (die);
7736 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7738 return clone;
7741 /* Make a clone of DIE as a declaration. */
7743 static dw_die_ref
7744 clone_as_declaration (dw_die_ref die)
7746 dw_die_ref clone;
7747 dw_die_ref decl;
7748 dw_attr_node *a;
7749 unsigned ix;
7751 /* If the DIE is already a declaration, just clone it. */
7752 if (is_declaration_die (die))
7753 return clone_die (die);
7755 /* If the DIE is a specification, just clone its declaration DIE. */
7756 decl = get_AT_ref (die, DW_AT_specification);
7757 if (decl != NULL)
7759 clone = clone_die (decl);
7760 if (die->comdat_type_p)
7761 add_AT_die_ref (clone, DW_AT_signature, die);
7762 return clone;
7765 clone = ggc_cleared_alloc<die_node> ();
7766 clone->die_tag = die->die_tag;
7768 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7770 /* We don't want to copy over all attributes.
7771 For example we don't want DW_AT_byte_size because otherwise we will no
7772 longer have a declaration and GDB will treat it as a definition. */
7774 switch (a->dw_attr)
7776 case DW_AT_abstract_origin:
7777 case DW_AT_artificial:
7778 case DW_AT_containing_type:
7779 case DW_AT_external:
7780 case DW_AT_name:
7781 case DW_AT_type:
7782 case DW_AT_virtuality:
7783 case DW_AT_linkage_name:
7784 case DW_AT_MIPS_linkage_name:
7785 add_dwarf_attr (clone, a);
7786 break;
7787 case DW_AT_byte_size:
7788 case DW_AT_alignment:
7789 default:
7790 break;
7794 if (die->comdat_type_p)
7795 add_AT_die_ref (clone, DW_AT_signature, die);
7797 add_AT_flag (clone, DW_AT_declaration, 1);
7798 return clone;
7802 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7804 struct decl_table_entry
7806 dw_die_ref orig;
7807 dw_die_ref copy;
7810 /* Helpers to manipulate hash table of copied declarations. */
7812 /* Hashtable helpers. */
7814 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7816 typedef die_struct *compare_type;
7817 static inline hashval_t hash (const decl_table_entry *);
7818 static inline bool equal (const decl_table_entry *, const die_struct *);
7821 inline hashval_t
7822 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7824 return htab_hash_pointer (entry->orig);
7827 inline bool
7828 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7829 const die_struct *entry2)
7831 return entry1->orig == entry2;
7834 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7836 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7837 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7838 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7839 to check if the ancestor has already been copied into UNIT. */
7841 static dw_die_ref
7842 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7843 decl_hash_type *decl_table)
7845 dw_die_ref parent = die->die_parent;
7846 dw_die_ref new_parent = unit;
7847 dw_die_ref copy;
7848 decl_table_entry **slot = NULL;
7849 struct decl_table_entry *entry = NULL;
7851 if (decl_table)
7853 /* Check if the entry has already been copied to UNIT. */
7854 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7855 INSERT);
7856 if (*slot != HTAB_EMPTY_ENTRY)
7858 entry = *slot;
7859 return entry->copy;
7862 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7863 entry = XCNEW (struct decl_table_entry);
7864 entry->orig = die;
7865 entry->copy = NULL;
7866 *slot = entry;
7869 if (parent != NULL)
7871 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7872 if (spec != NULL)
7873 parent = spec;
7874 if (!is_unit_die (parent))
7875 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7878 copy = clone_as_declaration (die);
7879 add_child_die (new_parent, copy);
7881 if (decl_table)
7883 /* Record the pointer to the copy. */
7884 entry->copy = copy;
7887 return copy;
7889 /* Copy the declaration context to the new type unit DIE. This includes
7890 any surrounding namespace or type declarations. If the DIE has an
7891 AT_specification attribute, it also includes attributes and children
7892 attached to the specification, and returns a pointer to the original
7893 parent of the declaration DIE. Returns NULL otherwise. */
7895 static dw_die_ref
7896 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7898 dw_die_ref decl;
7899 dw_die_ref new_decl;
7900 dw_die_ref orig_parent = NULL;
7902 decl = get_AT_ref (die, DW_AT_specification);
7903 if (decl == NULL)
7904 decl = die;
7905 else
7907 unsigned ix;
7908 dw_die_ref c;
7909 dw_attr_node *a;
7911 /* The original DIE will be changed to a declaration, and must
7912 be moved to be a child of the original declaration DIE. */
7913 orig_parent = decl->die_parent;
7915 /* Copy the type node pointer from the new DIE to the original
7916 declaration DIE so we can forward references later. */
7917 decl->comdat_type_p = true;
7918 decl->die_id.die_type_node = die->die_id.die_type_node;
7920 remove_AT (die, DW_AT_specification);
7922 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7924 if (a->dw_attr != DW_AT_name
7925 && a->dw_attr != DW_AT_declaration
7926 && a->dw_attr != DW_AT_external)
7927 add_dwarf_attr (die, a);
7930 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7933 if (decl->die_parent != NULL
7934 && !is_unit_die (decl->die_parent))
7936 new_decl = copy_ancestor_tree (unit, decl, NULL);
7937 if (new_decl != NULL)
7939 remove_AT (new_decl, DW_AT_signature);
7940 add_AT_specification (die, new_decl);
7944 return orig_parent;
7947 /* Generate the skeleton ancestor tree for the given NODE, then clone
7948 the DIE and add the clone into the tree. */
7950 static void
7951 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7953 if (node->new_die != NULL)
7954 return;
7956 node->new_die = clone_as_declaration (node->old_die);
7958 if (node->parent != NULL)
7960 generate_skeleton_ancestor_tree (node->parent);
7961 add_child_die (node->parent->new_die, node->new_die);
7965 /* Generate a skeleton tree of DIEs containing any declarations that are
7966 found in the original tree. We traverse the tree looking for declaration
7967 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7969 static void
7970 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7972 skeleton_chain_node node;
7973 dw_die_ref c;
7974 dw_die_ref first;
7975 dw_die_ref prev = NULL;
7976 dw_die_ref next = NULL;
7978 node.parent = parent;
7980 first = c = parent->old_die->die_child;
7981 if (c)
7982 next = c->die_sib;
7983 if (c) do {
7984 if (prev == NULL || prev->die_sib == c)
7985 prev = c;
7986 c = next;
7987 next = (c == first ? NULL : c->die_sib);
7988 node.old_die = c;
7989 node.new_die = NULL;
7990 if (is_declaration_die (c))
7992 if (is_template_instantiation (c))
7994 /* Instantiated templates do not need to be cloned into the
7995 type unit. Just move the DIE and its children back to
7996 the skeleton tree (in the main CU). */
7997 remove_child_with_prev (c, prev);
7998 add_child_die (parent->new_die, c);
7999 c = prev;
8001 else if (c->comdat_type_p)
8003 /* This is the skeleton of earlier break_out_comdat_types
8004 type. Clone the existing DIE, but keep the children
8005 under the original (which is in the main CU). */
8006 dw_die_ref clone = clone_die (c);
8008 replace_child (c, clone, prev);
8009 generate_skeleton_ancestor_tree (parent);
8010 add_child_die (parent->new_die, c);
8011 c = clone;
8012 continue;
8014 else
8016 /* Clone the existing DIE, move the original to the skeleton
8017 tree (which is in the main CU), and put the clone, with
8018 all the original's children, where the original came from
8019 (which is about to be moved to the type unit). */
8020 dw_die_ref clone = clone_die (c);
8021 move_all_children (c, clone);
8023 /* If the original has a DW_AT_object_pointer attribute,
8024 it would now point to a child DIE just moved to the
8025 cloned tree, so we need to remove that attribute from
8026 the original. */
8027 remove_AT (c, DW_AT_object_pointer);
8029 replace_child (c, clone, prev);
8030 generate_skeleton_ancestor_tree (parent);
8031 add_child_die (parent->new_die, c);
8032 node.old_die = clone;
8033 node.new_die = c;
8034 c = clone;
8037 generate_skeleton_bottom_up (&node);
8038 } while (next != NULL);
8041 /* Wrapper function for generate_skeleton_bottom_up. */
8043 static dw_die_ref
8044 generate_skeleton (dw_die_ref die)
8046 skeleton_chain_node node;
8048 node.old_die = die;
8049 node.new_die = NULL;
8050 node.parent = NULL;
8052 /* If this type definition is nested inside another type,
8053 and is not an instantiation of a template, always leave
8054 at least a declaration in its place. */
8055 if (die->die_parent != NULL
8056 && is_type_die (die->die_parent)
8057 && !is_template_instantiation (die))
8058 node.new_die = clone_as_declaration (die);
8060 generate_skeleton_bottom_up (&node);
8061 return node.new_die;
8064 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8065 declaration. The original DIE is moved to a new compile unit so that
8066 existing references to it follow it to the new location. If any of the
8067 original DIE's descendants is a declaration, we need to replace the
8068 original DIE with a skeleton tree and move the declarations back into the
8069 skeleton tree. */
8071 static dw_die_ref
8072 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8073 dw_die_ref prev)
8075 dw_die_ref skeleton, orig_parent;
8077 /* Copy the declaration context to the type unit DIE. If the returned
8078 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8079 that DIE. */
8080 orig_parent = copy_declaration_context (unit, child);
8082 skeleton = generate_skeleton (child);
8083 if (skeleton == NULL)
8084 remove_child_with_prev (child, prev);
8085 else
8087 skeleton->comdat_type_p = true;
8088 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8090 /* If the original DIE was a specification, we need to put
8091 the skeleton under the parent DIE of the declaration.
8092 This leaves the original declaration in the tree, but
8093 it will be pruned later since there are no longer any
8094 references to it. */
8095 if (orig_parent != NULL)
8097 remove_child_with_prev (child, prev);
8098 add_child_die (orig_parent, skeleton);
8100 else
8101 replace_child (child, skeleton, prev);
8104 return skeleton;
8107 static void
8108 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8109 comdat_type_node *type_node,
8110 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8112 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8113 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8114 DWARF procedure references in the DW_AT_location attribute. */
8116 static dw_die_ref
8117 copy_dwarf_procedure (dw_die_ref die,
8118 comdat_type_node *type_node,
8119 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8121 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8123 /* DWARF procedures are not supposed to have children... */
8124 gcc_assert (die->die_child == NULL);
8126 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8127 gcc_assert (vec_safe_length (die->die_attr) == 1
8128 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8130 /* Do not copy more than once DWARF procedures. */
8131 bool existed;
8132 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8133 if (existed)
8134 return die_copy;
8136 die_copy = clone_die (die);
8137 add_child_die (type_node->root_die, die_copy);
8138 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8139 return die_copy;
8142 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8143 procedures in DIE's attributes. */
8145 static void
8146 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8147 comdat_type_node *type_node,
8148 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8150 dw_attr_node *a;
8151 unsigned i;
8153 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8155 dw_loc_descr_ref loc;
8157 if (a->dw_attr_val.val_class != dw_val_class_loc)
8158 continue;
8160 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8162 switch (loc->dw_loc_opc)
8164 case DW_OP_call2:
8165 case DW_OP_call4:
8166 case DW_OP_call_ref:
8167 gcc_assert (loc->dw_loc_oprnd1.val_class
8168 == dw_val_class_die_ref);
8169 loc->dw_loc_oprnd1.v.val_die_ref.die
8170 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8171 type_node,
8172 copied_dwarf_procs);
8174 default:
8175 break;
8181 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8182 rewrite references to point to the copies.
8184 References are looked for in DIE's attributes and recursively in all its
8185 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8186 mapping from old DWARF procedures to their copy. It is used not to copy
8187 twice the same DWARF procedure under TYPE_NODE. */
8189 static void
8190 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8191 comdat_type_node *type_node,
8192 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8194 dw_die_ref c;
8196 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8197 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8198 type_node,
8199 copied_dwarf_procs));
8202 /* Traverse the DIE and set up additional .debug_types or .debug_info
8203 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8204 section. */
8206 static void
8207 break_out_comdat_types (dw_die_ref die)
8209 dw_die_ref c;
8210 dw_die_ref first;
8211 dw_die_ref prev = NULL;
8212 dw_die_ref next = NULL;
8213 dw_die_ref unit = NULL;
8215 first = c = die->die_child;
8216 if (c)
8217 next = c->die_sib;
8218 if (c) do {
8219 if (prev == NULL || prev->die_sib == c)
8220 prev = c;
8221 c = next;
8222 next = (c == first ? NULL : c->die_sib);
8223 if (should_move_die_to_comdat (c))
8225 dw_die_ref replacement;
8226 comdat_type_node *type_node;
8228 /* Break out nested types into their own type units. */
8229 break_out_comdat_types (c);
8231 /* Create a new type unit DIE as the root for the new tree, and
8232 add it to the list of comdat types. */
8233 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8234 add_AT_unsigned (unit, DW_AT_language,
8235 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8236 type_node = ggc_cleared_alloc<comdat_type_node> ();
8237 type_node->root_die = unit;
8238 type_node->next = comdat_type_list;
8239 comdat_type_list = type_node;
8241 /* Generate the type signature. */
8242 generate_type_signature (c, type_node);
8244 /* Copy the declaration context, attributes, and children of the
8245 declaration into the new type unit DIE, then remove this DIE
8246 from the main CU (or replace it with a skeleton if necessary). */
8247 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8248 type_node->skeleton_die = replacement;
8250 /* Add the DIE to the new compunit. */
8251 add_child_die (unit, c);
8253 /* Types can reference DWARF procedures for type size or data location
8254 expressions. Calls in DWARF expressions cannot target procedures
8255 that are not in the same section. So we must copy DWARF procedures
8256 along with this type and then rewrite references to them. */
8257 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8258 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8260 if (replacement != NULL)
8261 c = replacement;
8263 else if (c->die_tag == DW_TAG_namespace
8264 || c->die_tag == DW_TAG_class_type
8265 || c->die_tag == DW_TAG_structure_type
8266 || c->die_tag == DW_TAG_union_type)
8268 /* Look for nested types that can be broken out. */
8269 break_out_comdat_types (c);
8271 } while (next != NULL);
8274 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8275 Enter all the cloned children into the hash table decl_table. */
8277 static dw_die_ref
8278 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8280 dw_die_ref c;
8281 dw_die_ref clone;
8282 struct decl_table_entry *entry;
8283 decl_table_entry **slot;
8285 if (die->die_tag == DW_TAG_subprogram)
8286 clone = clone_as_declaration (die);
8287 else
8288 clone = clone_die (die);
8290 slot = decl_table->find_slot_with_hash (die,
8291 htab_hash_pointer (die), INSERT);
8293 /* Assert that DIE isn't in the hash table yet. If it would be there
8294 before, the ancestors would be necessarily there as well, therefore
8295 clone_tree_partial wouldn't be called. */
8296 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8298 entry = XCNEW (struct decl_table_entry);
8299 entry->orig = die;
8300 entry->copy = clone;
8301 *slot = entry;
8303 if (die->die_tag != DW_TAG_subprogram)
8304 FOR_EACH_CHILD (die, c,
8305 add_child_die (clone, clone_tree_partial (c, decl_table)));
8307 return clone;
8310 /* Walk the DIE and its children, looking for references to incomplete
8311 or trivial types that are unmarked (i.e., that are not in the current
8312 type_unit). */
8314 static void
8315 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8317 dw_die_ref c;
8318 dw_attr_node *a;
8319 unsigned ix;
8321 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8323 if (AT_class (a) == dw_val_class_die_ref)
8325 dw_die_ref targ = AT_ref (a);
8326 decl_table_entry **slot;
8327 struct decl_table_entry *entry;
8329 if (targ->die_mark != 0 || targ->comdat_type_p)
8330 continue;
8332 slot = decl_table->find_slot_with_hash (targ,
8333 htab_hash_pointer (targ),
8334 INSERT);
8336 if (*slot != HTAB_EMPTY_ENTRY)
8338 /* TARG has already been copied, so we just need to
8339 modify the reference to point to the copy. */
8340 entry = *slot;
8341 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8343 else
8345 dw_die_ref parent = unit;
8346 dw_die_ref copy = clone_die (targ);
8348 /* Record in DECL_TABLE that TARG has been copied.
8349 Need to do this now, before the recursive call,
8350 because DECL_TABLE may be expanded and SLOT
8351 would no longer be a valid pointer. */
8352 entry = XCNEW (struct decl_table_entry);
8353 entry->orig = targ;
8354 entry->copy = copy;
8355 *slot = entry;
8357 /* If TARG is not a declaration DIE, we need to copy its
8358 children. */
8359 if (!is_declaration_die (targ))
8361 FOR_EACH_CHILD (
8362 targ, c,
8363 add_child_die (copy,
8364 clone_tree_partial (c, decl_table)));
8367 /* Make sure the cloned tree is marked as part of the
8368 type unit. */
8369 mark_dies (copy);
8371 /* If TARG has surrounding context, copy its ancestor tree
8372 into the new type unit. */
8373 if (targ->die_parent != NULL
8374 && !is_unit_die (targ->die_parent))
8375 parent = copy_ancestor_tree (unit, targ->die_parent,
8376 decl_table);
8378 add_child_die (parent, copy);
8379 a->dw_attr_val.v.val_die_ref.die = copy;
8381 /* Make sure the newly-copied DIE is walked. If it was
8382 installed in a previously-added context, it won't
8383 get visited otherwise. */
8384 if (parent != unit)
8386 /* Find the highest point of the newly-added tree,
8387 mark each node along the way, and walk from there. */
8388 parent->die_mark = 1;
8389 while (parent->die_parent
8390 && parent->die_parent->die_mark == 0)
8392 parent = parent->die_parent;
8393 parent->die_mark = 1;
8395 copy_decls_walk (unit, parent, decl_table);
8401 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8404 /* Copy declarations for "unworthy" types into the new comdat section.
8405 Incomplete types, modified types, and certain other types aren't broken
8406 out into comdat sections of their own, so they don't have a signature,
8407 and we need to copy the declaration into the same section so that we
8408 don't have an external reference. */
8410 static void
8411 copy_decls_for_unworthy_types (dw_die_ref unit)
8413 mark_dies (unit);
8414 decl_hash_type decl_table (10);
8415 copy_decls_walk (unit, unit, &decl_table);
8416 unmark_dies (unit);
8419 /* Traverse the DIE and add a sibling attribute if it may have the
8420 effect of speeding up access to siblings. To save some space,
8421 avoid generating sibling attributes for DIE's without children. */
8423 static void
8424 add_sibling_attributes (dw_die_ref die)
8426 dw_die_ref c;
8428 if (! die->die_child)
8429 return;
8431 if (die->die_parent && die != die->die_parent->die_child)
8432 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8434 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8437 /* Output all location lists for the DIE and its children. */
8439 static void
8440 output_location_lists (dw_die_ref die)
8442 dw_die_ref c;
8443 dw_attr_node *a;
8444 unsigned ix;
8446 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8447 if (AT_class (a) == dw_val_class_loc_list)
8448 output_loc_list (AT_loc_list (a));
8450 FOR_EACH_CHILD (die, c, output_location_lists (c));
8453 /* During assign_location_list_indexes and output_loclists_offset the
8454 current index, after it the number of assigned indexes (i.e. how
8455 large the .debug_loclists* offset table should be). */
8456 static unsigned int loc_list_idx;
8458 /* Output all location list offsets for the DIE and its children. */
8460 static void
8461 output_loclists_offsets (dw_die_ref die)
8463 dw_die_ref c;
8464 dw_attr_node *a;
8465 unsigned ix;
8467 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8468 if (AT_class (a) == dw_val_class_loc_list)
8470 dw_loc_list_ref l = AT_loc_list (a);
8471 if (l->offset_emitted)
8472 continue;
8473 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8474 loc_section_label, NULL);
8475 gcc_assert (l->hash == loc_list_idx);
8476 loc_list_idx++;
8477 l->offset_emitted = true;
8480 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8483 /* Recursively set indexes of location lists. */
8485 static void
8486 assign_location_list_indexes (dw_die_ref die)
8488 dw_die_ref c;
8489 dw_attr_node *a;
8490 unsigned ix;
8492 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8493 if (AT_class (a) == dw_val_class_loc_list)
8495 dw_loc_list_ref list = AT_loc_list (a);
8496 if (!list->num_assigned)
8498 list->num_assigned = true;
8499 list->hash = loc_list_idx++;
8503 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8506 /* We want to limit the number of external references, because they are
8507 larger than local references: a relocation takes multiple words, and
8508 even a sig8 reference is always eight bytes, whereas a local reference
8509 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8510 So if we encounter multiple external references to the same type DIE, we
8511 make a local typedef stub for it and redirect all references there.
8513 This is the element of the hash table for keeping track of these
8514 references. */
8516 struct external_ref
8518 dw_die_ref type;
8519 dw_die_ref stub;
8520 unsigned n_refs;
8523 /* Hashtable helpers. */
8525 struct external_ref_hasher : free_ptr_hash <external_ref>
8527 static inline hashval_t hash (const external_ref *);
8528 static inline bool equal (const external_ref *, const external_ref *);
8531 inline hashval_t
8532 external_ref_hasher::hash (const external_ref *r)
8534 dw_die_ref die = r->type;
8535 hashval_t h = 0;
8537 /* We can't use the address of the DIE for hashing, because
8538 that will make the order of the stub DIEs non-deterministic. */
8539 if (! die->comdat_type_p)
8540 /* We have a symbol; use it to compute a hash. */
8541 h = htab_hash_string (die->die_id.die_symbol);
8542 else
8544 /* We have a type signature; use a subset of the bits as the hash.
8545 The 8-byte signature is at least as large as hashval_t. */
8546 comdat_type_node *type_node = die->die_id.die_type_node;
8547 memcpy (&h, type_node->signature, sizeof (h));
8549 return h;
8552 inline bool
8553 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8555 return r1->type == r2->type;
8558 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8560 /* Return a pointer to the external_ref for references to DIE. */
8562 static struct external_ref *
8563 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8565 struct external_ref ref, *ref_p;
8566 external_ref **slot;
8568 ref.type = die;
8569 slot = map->find_slot (&ref, INSERT);
8570 if (*slot != HTAB_EMPTY_ENTRY)
8571 return *slot;
8573 ref_p = XCNEW (struct external_ref);
8574 ref_p->type = die;
8575 *slot = ref_p;
8576 return ref_p;
8579 /* Subroutine of optimize_external_refs, below.
8581 If we see a type skeleton, record it as our stub. If we see external
8582 references, remember how many we've seen. */
8584 static void
8585 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8587 dw_die_ref c;
8588 dw_attr_node *a;
8589 unsigned ix;
8590 struct external_ref *ref_p;
8592 if (is_type_die (die)
8593 && (c = get_AT_ref (die, DW_AT_signature)))
8595 /* This is a local skeleton; use it for local references. */
8596 ref_p = lookup_external_ref (map, c);
8597 ref_p->stub = die;
8600 /* Scan the DIE references, and remember any that refer to DIEs from
8601 other CUs (i.e. those which are not marked). */
8602 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8603 if (AT_class (a) == dw_val_class_die_ref
8604 && (c = AT_ref (a))->die_mark == 0
8605 && is_type_die (c))
8607 ref_p = lookup_external_ref (map, c);
8608 ref_p->n_refs++;
8611 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8614 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8615 points to an external_ref, DATA is the CU we're processing. If we don't
8616 already have a local stub, and we have multiple refs, build a stub. */
8619 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8621 struct external_ref *ref_p = *slot;
8623 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8625 /* We have multiple references to this type, so build a small stub.
8626 Both of these forms are a bit dodgy from the perspective of the
8627 DWARF standard, since technically they should have names. */
8628 dw_die_ref cu = data;
8629 dw_die_ref type = ref_p->type;
8630 dw_die_ref stub = NULL;
8632 if (type->comdat_type_p)
8634 /* If we refer to this type via sig8, use AT_signature. */
8635 stub = new_die (type->die_tag, cu, NULL_TREE);
8636 add_AT_die_ref (stub, DW_AT_signature, type);
8638 else
8640 /* Otherwise, use a typedef with no name. */
8641 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8642 add_AT_die_ref (stub, DW_AT_type, type);
8645 stub->die_mark++;
8646 ref_p->stub = stub;
8648 return 1;
8651 /* DIE is a unit; look through all the DIE references to see if there are
8652 any external references to types, and if so, create local stubs for
8653 them which will be applied in build_abbrev_table. This is useful because
8654 references to local DIEs are smaller. */
8656 static external_ref_hash_type *
8657 optimize_external_refs (dw_die_ref die)
8659 external_ref_hash_type *map = new external_ref_hash_type (10);
8660 optimize_external_refs_1 (die, map);
8661 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8662 return map;
8665 /* The following 3 variables are temporaries that are computed only during the
8666 build_abbrev_table call and used and released during the following
8667 optimize_abbrev_table call. */
8669 /* First abbrev_id that can be optimized based on usage. */
8670 static unsigned int abbrev_opt_start;
8672 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8673 abbrev_id smaller than this, because they must be already sized
8674 during build_abbrev_table). */
8675 static unsigned int abbrev_opt_base_type_end;
8677 /* Vector of usage counts during build_abbrev_table. Indexed by
8678 abbrev_id - abbrev_opt_start. */
8679 static vec<unsigned int> abbrev_usage_count;
8681 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8682 static vec<dw_die_ref> sorted_abbrev_dies;
8684 /* The format of each DIE (and its attribute value pairs) is encoded in an
8685 abbreviation table. This routine builds the abbreviation table and assigns
8686 a unique abbreviation id for each abbreviation entry. The children of each
8687 die are visited recursively. */
8689 static void
8690 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8692 unsigned int abbrev_id = 0;
8693 dw_die_ref c;
8694 dw_attr_node *a;
8695 unsigned ix;
8696 dw_die_ref abbrev;
8698 /* Scan the DIE references, and replace any that refer to
8699 DIEs from other CUs (i.e. those which are not marked) with
8700 the local stubs we built in optimize_external_refs. */
8701 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8702 if (AT_class (a) == dw_val_class_die_ref
8703 && (c = AT_ref (a))->die_mark == 0)
8705 struct external_ref *ref_p;
8706 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8708 ref_p = lookup_external_ref (extern_map, c);
8709 if (ref_p->stub && ref_p->stub != die)
8710 change_AT_die_ref (a, ref_p->stub);
8711 else
8712 /* We aren't changing this reference, so mark it external. */
8713 set_AT_ref_external (a, 1);
8716 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8718 dw_attr_node *die_a, *abbrev_a;
8719 unsigned ix;
8720 bool ok = true;
8722 if (abbrev_id == 0)
8723 continue;
8724 if (abbrev->die_tag != die->die_tag)
8725 continue;
8726 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8727 continue;
8729 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8730 continue;
8732 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8734 abbrev_a = &(*abbrev->die_attr)[ix];
8735 if ((abbrev_a->dw_attr != die_a->dw_attr)
8736 || (value_format (abbrev_a) != value_format (die_a)))
8738 ok = false;
8739 break;
8742 if (ok)
8743 break;
8746 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8748 vec_safe_push (abbrev_die_table, die);
8749 if (abbrev_opt_start)
8750 abbrev_usage_count.safe_push (0);
8752 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8754 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8755 sorted_abbrev_dies.safe_push (die);
8758 die->die_abbrev = abbrev_id;
8759 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8762 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8763 by die_abbrev's usage count, from the most commonly used
8764 abbreviation to the least. */
8766 static int
8767 die_abbrev_cmp (const void *p1, const void *p2)
8769 dw_die_ref die1 = *(const dw_die_ref *) p1;
8770 dw_die_ref die2 = *(const dw_die_ref *) p2;
8772 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8773 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8775 if (die1->die_abbrev >= abbrev_opt_base_type_end
8776 && die2->die_abbrev >= abbrev_opt_base_type_end)
8778 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8779 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8780 return -1;
8781 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8782 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8783 return 1;
8786 /* Stabilize the sort. */
8787 if (die1->die_abbrev < die2->die_abbrev)
8788 return -1;
8789 if (die1->die_abbrev > die2->die_abbrev)
8790 return 1;
8792 return 0;
8795 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8796 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8797 into dw_val_class_const_implicit or
8798 dw_val_class_unsigned_const_implicit. */
8800 static void
8801 optimize_implicit_const (unsigned int first_id, unsigned int end,
8802 vec<bool> &implicit_consts)
8804 /* It never makes sense if there is just one DIE using the abbreviation. */
8805 if (end < first_id + 2)
8806 return;
8808 dw_attr_node *a;
8809 unsigned ix, i;
8810 dw_die_ref die = sorted_abbrev_dies[first_id];
8811 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8812 if (implicit_consts[ix])
8814 enum dw_val_class new_class = dw_val_class_none;
8815 switch (AT_class (a))
8817 case dw_val_class_unsigned_const:
8818 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8819 continue;
8821 /* The .debug_abbrev section will grow by
8822 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8823 in all the DIEs using that abbreviation. */
8824 if (constant_size (AT_unsigned (a)) * (end - first_id)
8825 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8826 continue;
8828 new_class = dw_val_class_unsigned_const_implicit;
8829 break;
8831 case dw_val_class_const:
8832 new_class = dw_val_class_const_implicit;
8833 break;
8835 case dw_val_class_file:
8836 new_class = dw_val_class_file_implicit;
8837 break;
8839 default:
8840 continue;
8842 for (i = first_id; i < end; i++)
8843 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8844 = new_class;
8848 /* Attempt to optimize abbreviation table from abbrev_opt_start
8849 abbreviation above. */
8851 static void
8852 optimize_abbrev_table (void)
8854 if (abbrev_opt_start
8855 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8856 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8858 auto_vec<bool, 32> implicit_consts;
8859 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8861 unsigned int abbrev_id = abbrev_opt_start - 1;
8862 unsigned int first_id = ~0U;
8863 unsigned int last_abbrev_id = 0;
8864 unsigned int i;
8865 dw_die_ref die;
8866 if (abbrev_opt_base_type_end > abbrev_opt_start)
8867 abbrev_id = abbrev_opt_base_type_end - 1;
8868 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8869 most commonly used abbreviations come first. */
8870 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8872 dw_attr_node *a;
8873 unsigned ix;
8875 /* If calc_base_type_die_sizes has been called, the CU and
8876 base types after it can't be optimized, because we've already
8877 calculated their DIE offsets. We've sorted them first. */
8878 if (die->die_abbrev < abbrev_opt_base_type_end)
8879 continue;
8880 if (die->die_abbrev != last_abbrev_id)
8882 last_abbrev_id = die->die_abbrev;
8883 if (dwarf_version >= 5 && first_id != ~0U)
8884 optimize_implicit_const (first_id, i, implicit_consts);
8885 abbrev_id++;
8886 (*abbrev_die_table)[abbrev_id] = die;
8887 if (dwarf_version >= 5)
8889 first_id = i;
8890 implicit_consts.truncate (0);
8892 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8893 switch (AT_class (a))
8895 case dw_val_class_const:
8896 case dw_val_class_unsigned_const:
8897 case dw_val_class_file:
8898 implicit_consts.safe_push (true);
8899 break;
8900 default:
8901 implicit_consts.safe_push (false);
8902 break;
8906 else if (dwarf_version >= 5)
8908 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8909 if (!implicit_consts[ix])
8910 continue;
8911 else
8913 dw_attr_node *other_a
8914 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8915 if (!dw_val_equal_p (&a->dw_attr_val,
8916 &other_a->dw_attr_val))
8917 implicit_consts[ix] = false;
8920 die->die_abbrev = abbrev_id;
8922 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8923 if (dwarf_version >= 5 && first_id != ~0U)
8924 optimize_implicit_const (first_id, i, implicit_consts);
8927 abbrev_opt_start = 0;
8928 abbrev_opt_base_type_end = 0;
8929 abbrev_usage_count.release ();
8930 sorted_abbrev_dies.release ();
8933 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8935 static int
8936 constant_size (unsigned HOST_WIDE_INT value)
8938 int log;
8940 if (value == 0)
8941 log = 0;
8942 else
8943 log = floor_log2 (value);
8945 log = log / 8;
8946 log = 1 << (floor_log2 (log) + 1);
8948 return log;
8951 /* Return the size of a DIE as it is represented in the
8952 .debug_info section. */
8954 static unsigned long
8955 size_of_die (dw_die_ref die)
8957 unsigned long size = 0;
8958 dw_attr_node *a;
8959 unsigned ix;
8960 enum dwarf_form form;
8962 size += size_of_uleb128 (die->die_abbrev);
8963 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8965 switch (AT_class (a))
8967 case dw_val_class_addr:
8968 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8970 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8971 size += size_of_uleb128 (AT_index (a));
8973 else
8974 size += DWARF2_ADDR_SIZE;
8975 break;
8976 case dw_val_class_offset:
8977 size += DWARF_OFFSET_SIZE;
8978 break;
8979 case dw_val_class_loc:
8981 unsigned long lsize = size_of_locs (AT_loc (a));
8983 /* Block length. */
8984 if (dwarf_version >= 4)
8985 size += size_of_uleb128 (lsize);
8986 else
8987 size += constant_size (lsize);
8988 size += lsize;
8990 break;
8991 case dw_val_class_loc_list:
8992 if (dwarf_split_debug_info && dwarf_version >= 5)
8994 gcc_assert (AT_loc_list (a)->num_assigned);
8995 size += size_of_uleb128 (AT_loc_list (a)->hash);
8997 else
8998 size += DWARF_OFFSET_SIZE;
8999 break;
9000 case dw_val_class_range_list:
9001 if (value_format (a) == DW_FORM_rnglistx)
9003 gcc_assert (rnglist_idx);
9004 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9005 size += size_of_uleb128 (r->idx);
9007 else
9008 size += DWARF_OFFSET_SIZE;
9009 break;
9010 case dw_val_class_const:
9011 size += size_of_sleb128 (AT_int (a));
9012 break;
9013 case dw_val_class_unsigned_const:
9015 int csize = constant_size (AT_unsigned (a));
9016 if (dwarf_version == 3
9017 && a->dw_attr == DW_AT_data_member_location
9018 && csize >= 4)
9019 size += size_of_uleb128 (AT_unsigned (a));
9020 else
9021 size += csize;
9023 break;
9024 case dw_val_class_const_implicit:
9025 case dw_val_class_unsigned_const_implicit:
9026 case dw_val_class_file_implicit:
9027 /* These occupy no size in the DIE, just an extra sleb128 in
9028 .debug_abbrev. */
9029 break;
9030 case dw_val_class_const_double:
9031 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9032 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9033 size++; /* block */
9034 break;
9035 case dw_val_class_wide_int:
9036 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9037 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9038 if (get_full_len (*a->dw_attr_val.v.val_wide)
9039 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9040 size++; /* block */
9041 break;
9042 case dw_val_class_vec:
9043 size += constant_size (a->dw_attr_val.v.val_vec.length
9044 * a->dw_attr_val.v.val_vec.elt_size)
9045 + a->dw_attr_val.v.val_vec.length
9046 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9047 break;
9048 case dw_val_class_flag:
9049 if (dwarf_version >= 4)
9050 /* Currently all add_AT_flag calls pass in 1 as last argument,
9051 so DW_FORM_flag_present can be used. If that ever changes,
9052 we'll need to use DW_FORM_flag and have some optimization
9053 in build_abbrev_table that will change those to
9054 DW_FORM_flag_present if it is set to 1 in all DIEs using
9055 the same abbrev entry. */
9056 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9057 else
9058 size += 1;
9059 break;
9060 case dw_val_class_die_ref:
9061 if (AT_ref_external (a))
9063 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9064 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9065 is sized by target address length, whereas in DWARF3
9066 it's always sized as an offset. */
9067 if (use_debug_types)
9068 size += DWARF_TYPE_SIGNATURE_SIZE;
9069 else if (dwarf_version == 2)
9070 size += DWARF2_ADDR_SIZE;
9071 else
9072 size += DWARF_OFFSET_SIZE;
9074 else
9075 size += DWARF_OFFSET_SIZE;
9076 break;
9077 case dw_val_class_fde_ref:
9078 size += DWARF_OFFSET_SIZE;
9079 break;
9080 case dw_val_class_lbl_id:
9081 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9083 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9084 size += size_of_uleb128 (AT_index (a));
9086 else
9087 size += DWARF2_ADDR_SIZE;
9088 break;
9089 case dw_val_class_lineptr:
9090 case dw_val_class_macptr:
9091 case dw_val_class_loclistsptr:
9092 size += DWARF_OFFSET_SIZE;
9093 break;
9094 case dw_val_class_str:
9095 form = AT_string_form (a);
9096 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9097 size += DWARF_OFFSET_SIZE;
9098 else if (form == DW_FORM_GNU_str_index)
9099 size += size_of_uleb128 (AT_index (a));
9100 else
9101 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9102 break;
9103 case dw_val_class_file:
9104 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9105 break;
9106 case dw_val_class_data8:
9107 size += 8;
9108 break;
9109 case dw_val_class_vms_delta:
9110 size += DWARF_OFFSET_SIZE;
9111 break;
9112 case dw_val_class_high_pc:
9113 size += DWARF2_ADDR_SIZE;
9114 break;
9115 case dw_val_class_discr_value:
9116 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9117 break;
9118 case dw_val_class_discr_list:
9120 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9122 /* This is a block, so we have the block length and then its
9123 data. */
9124 size += constant_size (block_size) + block_size;
9126 break;
9127 default:
9128 gcc_unreachable ();
9132 return size;
9135 /* Size the debugging information associated with a given DIE. Visits the
9136 DIE's children recursively. Updates the global variable next_die_offset, on
9137 each time through. Uses the current value of next_die_offset to update the
9138 die_offset field in each DIE. */
9140 static void
9141 calc_die_sizes (dw_die_ref die)
9143 dw_die_ref c;
9145 gcc_assert (die->die_offset == 0
9146 || (unsigned long int) die->die_offset == next_die_offset);
9147 die->die_offset = next_die_offset;
9148 next_die_offset += size_of_die (die);
9150 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9152 if (die->die_child != NULL)
9153 /* Count the null byte used to terminate sibling lists. */
9154 next_die_offset += 1;
9157 /* Size just the base type children at the start of the CU.
9158 This is needed because build_abbrev needs to size locs
9159 and sizing of type based stack ops needs to know die_offset
9160 values for the base types. */
9162 static void
9163 calc_base_type_die_sizes (void)
9165 unsigned long die_offset = (dwarf_split_debug_info
9166 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9167 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9168 unsigned int i;
9169 dw_die_ref base_type;
9170 #if ENABLE_ASSERT_CHECKING
9171 dw_die_ref prev = comp_unit_die ()->die_child;
9172 #endif
9174 die_offset += size_of_die (comp_unit_die ());
9175 for (i = 0; base_types.iterate (i, &base_type); i++)
9177 #if ENABLE_ASSERT_CHECKING
9178 gcc_assert (base_type->die_offset == 0
9179 && prev->die_sib == base_type
9180 && base_type->die_child == NULL
9181 && base_type->die_abbrev);
9182 prev = base_type;
9183 #endif
9184 if (abbrev_opt_start
9185 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9186 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9187 base_type->die_offset = die_offset;
9188 die_offset += size_of_die (base_type);
9192 /* Set the marks for a die and its children. We do this so
9193 that we know whether or not a reference needs to use FORM_ref_addr; only
9194 DIEs in the same CU will be marked. We used to clear out the offset
9195 and use that as the flag, but ran into ordering problems. */
9197 static void
9198 mark_dies (dw_die_ref die)
9200 dw_die_ref c;
9202 gcc_assert (!die->die_mark);
9204 die->die_mark = 1;
9205 FOR_EACH_CHILD (die, c, mark_dies (c));
9208 /* Clear the marks for a die and its children. */
9210 static void
9211 unmark_dies (dw_die_ref die)
9213 dw_die_ref c;
9215 if (! use_debug_types)
9216 gcc_assert (die->die_mark);
9218 die->die_mark = 0;
9219 FOR_EACH_CHILD (die, c, unmark_dies (c));
9222 /* Clear the marks for a die, its children and referred dies. */
9224 static void
9225 unmark_all_dies (dw_die_ref die)
9227 dw_die_ref c;
9228 dw_attr_node *a;
9229 unsigned ix;
9231 if (!die->die_mark)
9232 return;
9233 die->die_mark = 0;
9235 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9237 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9238 if (AT_class (a) == dw_val_class_die_ref)
9239 unmark_all_dies (AT_ref (a));
9242 /* Calculate if the entry should appear in the final output file. It may be
9243 from a pruned a type. */
9245 static bool
9246 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9248 /* By limiting gnu pubnames to definitions only, gold can generate a
9249 gdb index without entries for declarations, which don't include
9250 enough information to be useful. */
9251 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9252 return false;
9254 if (table == pubname_table)
9256 /* Enumerator names are part of the pubname table, but the
9257 parent DW_TAG_enumeration_type die may have been pruned.
9258 Don't output them if that is the case. */
9259 if (p->die->die_tag == DW_TAG_enumerator &&
9260 (p->die->die_parent == NULL
9261 || !p->die->die_parent->die_perennial_p))
9262 return false;
9264 /* Everything else in the pubname table is included. */
9265 return true;
9268 /* The pubtypes table shouldn't include types that have been
9269 pruned. */
9270 return (p->die->die_offset != 0
9271 || !flag_eliminate_unused_debug_types);
9274 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9275 generated for the compilation unit. */
9277 static unsigned long
9278 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9280 unsigned long size;
9281 unsigned i;
9282 pubname_entry *p;
9283 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9285 size = DWARF_PUBNAMES_HEADER_SIZE;
9286 FOR_EACH_VEC_ELT (*names, i, p)
9287 if (include_pubname_in_output (names, p))
9288 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9290 size += DWARF_OFFSET_SIZE;
9291 return size;
9294 /* Return the size of the information in the .debug_aranges section. */
9296 static unsigned long
9297 size_of_aranges (void)
9299 unsigned long size;
9301 size = DWARF_ARANGES_HEADER_SIZE;
9303 /* Count the address/length pair for this compilation unit. */
9304 if (text_section_used)
9305 size += 2 * DWARF2_ADDR_SIZE;
9306 if (cold_text_section_used)
9307 size += 2 * DWARF2_ADDR_SIZE;
9308 if (have_multiple_function_sections)
9310 unsigned fde_idx;
9311 dw_fde_ref fde;
9313 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9315 if (DECL_IGNORED_P (fde->decl))
9316 continue;
9317 if (!fde->in_std_section)
9318 size += 2 * DWARF2_ADDR_SIZE;
9319 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9320 size += 2 * DWARF2_ADDR_SIZE;
9324 /* Count the two zero words used to terminated the address range table. */
9325 size += 2 * DWARF2_ADDR_SIZE;
9326 return size;
9329 /* Select the encoding of an attribute value. */
9331 static enum dwarf_form
9332 value_format (dw_attr_node *a)
9334 switch (AT_class (a))
9336 case dw_val_class_addr:
9337 /* Only very few attributes allow DW_FORM_addr. */
9338 switch (a->dw_attr)
9340 case DW_AT_low_pc:
9341 case DW_AT_high_pc:
9342 case DW_AT_entry_pc:
9343 case DW_AT_trampoline:
9344 return (AT_index (a) == NOT_INDEXED
9345 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9346 default:
9347 break;
9349 switch (DWARF2_ADDR_SIZE)
9351 case 1:
9352 return DW_FORM_data1;
9353 case 2:
9354 return DW_FORM_data2;
9355 case 4:
9356 return DW_FORM_data4;
9357 case 8:
9358 return DW_FORM_data8;
9359 default:
9360 gcc_unreachable ();
9362 case dw_val_class_loc_list:
9363 if (dwarf_split_debug_info
9364 && dwarf_version >= 5
9365 && AT_loc_list (a)->num_assigned)
9366 return DW_FORM_loclistx;
9367 /* FALLTHRU */
9368 case dw_val_class_range_list:
9369 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9370 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9371 care about sizes of .debug* sections in shared libraries and
9372 executables and don't take into account relocations that affect just
9373 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9374 table in the .debug_rnglists section. */
9375 if (dwarf_split_debug_info
9376 && dwarf_version >= 5
9377 && AT_class (a) == dw_val_class_range_list
9378 && rnglist_idx
9379 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9380 return DW_FORM_rnglistx;
9381 if (dwarf_version >= 4)
9382 return DW_FORM_sec_offset;
9383 /* FALLTHRU */
9384 case dw_val_class_vms_delta:
9385 case dw_val_class_offset:
9386 switch (DWARF_OFFSET_SIZE)
9388 case 4:
9389 return DW_FORM_data4;
9390 case 8:
9391 return DW_FORM_data8;
9392 default:
9393 gcc_unreachable ();
9395 case dw_val_class_loc:
9396 if (dwarf_version >= 4)
9397 return DW_FORM_exprloc;
9398 switch (constant_size (size_of_locs (AT_loc (a))))
9400 case 1:
9401 return DW_FORM_block1;
9402 case 2:
9403 return DW_FORM_block2;
9404 case 4:
9405 return DW_FORM_block4;
9406 default:
9407 gcc_unreachable ();
9409 case dw_val_class_const:
9410 return DW_FORM_sdata;
9411 case dw_val_class_unsigned_const:
9412 switch (constant_size (AT_unsigned (a)))
9414 case 1:
9415 return DW_FORM_data1;
9416 case 2:
9417 return DW_FORM_data2;
9418 case 4:
9419 /* In DWARF3 DW_AT_data_member_location with
9420 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9421 constant, so we need to use DW_FORM_udata if we need
9422 a large constant. */
9423 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9424 return DW_FORM_udata;
9425 return DW_FORM_data4;
9426 case 8:
9427 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9428 return DW_FORM_udata;
9429 return DW_FORM_data8;
9430 default:
9431 gcc_unreachable ();
9433 case dw_val_class_const_implicit:
9434 case dw_val_class_unsigned_const_implicit:
9435 case dw_val_class_file_implicit:
9436 return DW_FORM_implicit_const;
9437 case dw_val_class_const_double:
9438 switch (HOST_BITS_PER_WIDE_INT)
9440 case 8:
9441 return DW_FORM_data2;
9442 case 16:
9443 return DW_FORM_data4;
9444 case 32:
9445 return DW_FORM_data8;
9446 case 64:
9447 if (dwarf_version >= 5)
9448 return DW_FORM_data16;
9449 /* FALLTHRU */
9450 default:
9451 return DW_FORM_block1;
9453 case dw_val_class_wide_int:
9454 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9456 case 8:
9457 return DW_FORM_data1;
9458 case 16:
9459 return DW_FORM_data2;
9460 case 32:
9461 return DW_FORM_data4;
9462 case 64:
9463 return DW_FORM_data8;
9464 case 128:
9465 if (dwarf_version >= 5)
9466 return DW_FORM_data16;
9467 /* FALLTHRU */
9468 default:
9469 return DW_FORM_block1;
9471 case dw_val_class_vec:
9472 switch (constant_size (a->dw_attr_val.v.val_vec.length
9473 * a->dw_attr_val.v.val_vec.elt_size))
9475 case 1:
9476 return DW_FORM_block1;
9477 case 2:
9478 return DW_FORM_block2;
9479 case 4:
9480 return DW_FORM_block4;
9481 default:
9482 gcc_unreachable ();
9484 case dw_val_class_flag:
9485 if (dwarf_version >= 4)
9487 /* Currently all add_AT_flag calls pass in 1 as last argument,
9488 so DW_FORM_flag_present can be used. If that ever changes,
9489 we'll need to use DW_FORM_flag and have some optimization
9490 in build_abbrev_table that will change those to
9491 DW_FORM_flag_present if it is set to 1 in all DIEs using
9492 the same abbrev entry. */
9493 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9494 return DW_FORM_flag_present;
9496 return DW_FORM_flag;
9497 case dw_val_class_die_ref:
9498 if (AT_ref_external (a))
9499 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9500 else
9501 return DW_FORM_ref;
9502 case dw_val_class_fde_ref:
9503 return DW_FORM_data;
9504 case dw_val_class_lbl_id:
9505 return (AT_index (a) == NOT_INDEXED
9506 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9507 case dw_val_class_lineptr:
9508 case dw_val_class_macptr:
9509 case dw_val_class_loclistsptr:
9510 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9511 case dw_val_class_str:
9512 return AT_string_form (a);
9513 case dw_val_class_file:
9514 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9516 case 1:
9517 return DW_FORM_data1;
9518 case 2:
9519 return DW_FORM_data2;
9520 case 4:
9521 return DW_FORM_data4;
9522 default:
9523 gcc_unreachable ();
9526 case dw_val_class_data8:
9527 return DW_FORM_data8;
9529 case dw_val_class_high_pc:
9530 switch (DWARF2_ADDR_SIZE)
9532 case 1:
9533 return DW_FORM_data1;
9534 case 2:
9535 return DW_FORM_data2;
9536 case 4:
9537 return DW_FORM_data4;
9538 case 8:
9539 return DW_FORM_data8;
9540 default:
9541 gcc_unreachable ();
9544 case dw_val_class_discr_value:
9545 return (a->dw_attr_val.v.val_discr_value.pos
9546 ? DW_FORM_udata
9547 : DW_FORM_sdata);
9548 case dw_val_class_discr_list:
9549 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9551 case 1:
9552 return DW_FORM_block1;
9553 case 2:
9554 return DW_FORM_block2;
9555 case 4:
9556 return DW_FORM_block4;
9557 default:
9558 gcc_unreachable ();
9561 default:
9562 gcc_unreachable ();
9566 /* Output the encoding of an attribute value. */
9568 static void
9569 output_value_format (dw_attr_node *a)
9571 enum dwarf_form form = value_format (a);
9573 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9576 /* Given a die and id, produce the appropriate abbreviations. */
9578 static void
9579 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9581 unsigned ix;
9582 dw_attr_node *a_attr;
9584 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9585 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9586 dwarf_tag_name (abbrev->die_tag));
9588 if (abbrev->die_child != NULL)
9589 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9590 else
9591 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9593 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9595 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9596 dwarf_attr_name (a_attr->dw_attr));
9597 output_value_format (a_attr);
9598 if (value_format (a_attr) == DW_FORM_implicit_const)
9600 if (AT_class (a_attr) == dw_val_class_file_implicit)
9602 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9603 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9604 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9606 else
9607 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9611 dw2_asm_output_data (1, 0, NULL);
9612 dw2_asm_output_data (1, 0, NULL);
9616 /* Output the .debug_abbrev section which defines the DIE abbreviation
9617 table. */
9619 static void
9620 output_abbrev_section (void)
9622 unsigned int abbrev_id;
9623 dw_die_ref abbrev;
9625 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9626 if (abbrev_id != 0)
9627 output_die_abbrevs (abbrev_id, abbrev);
9629 /* Terminate the table. */
9630 dw2_asm_output_data (1, 0, NULL);
9633 /* Return a new location list, given the begin and end range, and the
9634 expression. */
9636 static inline dw_loc_list_ref
9637 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9638 const char *section)
9640 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9642 retlist->begin = begin;
9643 retlist->begin_entry = NULL;
9644 retlist->end = end;
9645 retlist->expr = expr;
9646 retlist->section = section;
9648 return retlist;
9651 /* Generate a new internal symbol for this location list node, if it
9652 hasn't got one yet. */
9654 static inline void
9655 gen_llsym (dw_loc_list_ref list)
9657 gcc_assert (!list->ll_symbol);
9658 list->ll_symbol = gen_internal_sym ("LLST");
9661 /* Output the location list given to us. */
9663 static void
9664 output_loc_list (dw_loc_list_ref list_head)
9666 if (list_head->emitted)
9667 return;
9668 list_head->emitted = true;
9670 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9672 dw_loc_list_ref curr = list_head;
9673 const char *last_section = NULL;
9674 const char *base_label = NULL;
9676 /* Walk the location list, and output each range + expression. */
9677 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9679 unsigned long size;
9680 /* Don't output an entry that starts and ends at the same address. */
9681 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9682 continue;
9683 size = size_of_locs (curr->expr);
9684 /* If the expression is too large, drop it on the floor. We could
9685 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9686 in the expression, but >= 64KB expressions for a single value
9687 in a single range are unlikely very useful. */
9688 if (dwarf_version < 5 && size > 0xffff)
9689 continue;
9690 if (dwarf_version >= 5)
9692 if (dwarf_split_debug_info)
9694 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9695 uleb128 index into .debug_addr and uleb128 length. */
9696 dw2_asm_output_data (1, DW_LLE_startx_length,
9697 "DW_LLE_startx_length (%s)",
9698 list_head->ll_symbol);
9699 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9700 "Location list range start index "
9701 "(%s)", curr->begin);
9702 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9703 For that case we probably need to emit DW_LLE_startx_endx,
9704 but we'd need 2 .debug_addr entries rather than just one. */
9705 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9706 "Location list length (%s)",
9707 list_head->ll_symbol);
9709 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9711 /* If all code is in .text section, the base address is
9712 already provided by the CU attributes. Use
9713 DW_LLE_offset_pair where both addresses are uleb128 encoded
9714 offsets against that base. */
9715 dw2_asm_output_data (1, DW_LLE_offset_pair,
9716 "DW_LLE_offset_pair (%s)",
9717 list_head->ll_symbol);
9718 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9719 "Location list begin address (%s)",
9720 list_head->ll_symbol);
9721 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9722 "Location list end address (%s)",
9723 list_head->ll_symbol);
9725 else if (HAVE_AS_LEB128)
9727 /* Otherwise, find out how many consecutive entries could share
9728 the same base entry. If just one, emit DW_LLE_start_length,
9729 otherwise emit DW_LLE_base_address for the base address
9730 followed by a series of DW_LLE_offset_pair. */
9731 if (last_section == NULL || curr->section != last_section)
9733 dw_loc_list_ref curr2;
9734 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9735 curr2 = curr2->dw_loc_next)
9737 if (strcmp (curr2->begin, curr2->end) == 0
9738 && !curr2->force)
9739 continue;
9740 break;
9742 if (curr2 == NULL || curr->section != curr2->section)
9743 last_section = NULL;
9744 else
9746 last_section = curr->section;
9747 base_label = curr->begin;
9748 dw2_asm_output_data (1, DW_LLE_base_address,
9749 "DW_LLE_base_address (%s)",
9750 list_head->ll_symbol);
9751 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9752 "Base address (%s)",
9753 list_head->ll_symbol);
9756 /* Only one entry with the same base address. Use
9757 DW_LLE_start_length with absolute address and uleb128
9758 length. */
9759 if (last_section == NULL)
9761 dw2_asm_output_data (1, DW_LLE_start_length,
9762 "DW_LLE_start_length (%s)",
9763 list_head->ll_symbol);
9764 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9765 "Location list begin address (%s)",
9766 list_head->ll_symbol);
9767 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9768 "Location list length "
9769 "(%s)", list_head->ll_symbol);
9771 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9772 DW_LLE_base_address. */
9773 else
9775 dw2_asm_output_data (1, DW_LLE_offset_pair,
9776 "DW_LLE_offset_pair (%s)",
9777 list_head->ll_symbol);
9778 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9779 "Location list begin address "
9780 "(%s)", list_head->ll_symbol);
9781 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9782 "Location list end address "
9783 "(%s)", list_head->ll_symbol);
9786 /* The assembler does not support .uleb128 directive. Emit
9787 DW_LLE_start_end with a pair of absolute addresses. */
9788 else
9790 dw2_asm_output_data (1, DW_LLE_start_end,
9791 "DW_LLE_start_end (%s)",
9792 list_head->ll_symbol);
9793 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9794 "Location list begin address (%s)",
9795 list_head->ll_symbol);
9796 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9797 "Location list end address (%s)",
9798 list_head->ll_symbol);
9801 else if (dwarf_split_debug_info)
9803 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9804 and 4 byte length. */
9805 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9806 "Location list start/length entry (%s)",
9807 list_head->ll_symbol);
9808 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9809 "Location list range start index (%s)",
9810 curr->begin);
9811 /* The length field is 4 bytes. If we ever need to support
9812 an 8-byte length, we can add a new DW_LLE code or fall back
9813 to DW_LLE_GNU_start_end_entry. */
9814 dw2_asm_output_delta (4, curr->end, curr->begin,
9815 "Location list range length (%s)",
9816 list_head->ll_symbol);
9818 else if (!have_multiple_function_sections)
9820 /* Pair of relative addresses against start of text section. */
9821 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9822 "Location list begin address (%s)",
9823 list_head->ll_symbol);
9824 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9825 "Location list end address (%s)",
9826 list_head->ll_symbol);
9828 else
9830 /* Pair of absolute addresses. */
9831 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9832 "Location list begin address (%s)",
9833 list_head->ll_symbol);
9834 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9835 "Location list end address (%s)",
9836 list_head->ll_symbol);
9839 /* Output the block length for this list of location operations. */
9840 if (dwarf_version >= 5)
9841 dw2_asm_output_data_uleb128 (size, "Location expression size");
9842 else
9844 gcc_assert (size <= 0xffff);
9845 dw2_asm_output_data (2, size, "Location expression size");
9848 output_loc_sequence (curr->expr, -1);
9851 /* And finally list termination. */
9852 if (dwarf_version >= 5)
9853 dw2_asm_output_data (1, DW_LLE_end_of_list,
9854 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9855 else if (dwarf_split_debug_info)
9856 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9857 "Location list terminator (%s)",
9858 list_head->ll_symbol);
9859 else
9861 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9862 "Location list terminator begin (%s)",
9863 list_head->ll_symbol);
9864 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9865 "Location list terminator end (%s)",
9866 list_head->ll_symbol);
9870 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9871 section. Emit a relocated reference if val_entry is NULL, otherwise,
9872 emit an indirect reference. */
9874 static void
9875 output_range_list_offset (dw_attr_node *a)
9877 const char *name = dwarf_attr_name (a->dw_attr);
9879 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9881 if (dwarf_version >= 5)
9883 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9884 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9885 debug_ranges_section, "%s", name);
9887 else
9889 char *p = strchr (ranges_section_label, '\0');
9890 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9891 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9892 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9893 debug_ranges_section, "%s", name);
9894 *p = '\0';
9897 else if (dwarf_version >= 5)
9899 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9900 gcc_assert (rnglist_idx);
9901 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9903 else
9904 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9905 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9906 "%s (offset from %s)", name, ranges_section_label);
9909 /* Output the offset into the debug_loc section. */
9911 static void
9912 output_loc_list_offset (dw_attr_node *a)
9914 char *sym = AT_loc_list (a)->ll_symbol;
9916 gcc_assert (sym);
9917 if (!dwarf_split_debug_info)
9918 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9919 "%s", dwarf_attr_name (a->dw_attr));
9920 else if (dwarf_version >= 5)
9922 gcc_assert (AT_loc_list (a)->num_assigned);
9923 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9924 dwarf_attr_name (a->dw_attr),
9925 sym);
9927 else
9928 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9929 "%s", dwarf_attr_name (a->dw_attr));
9932 /* Output an attribute's index or value appropriately. */
9934 static void
9935 output_attr_index_or_value (dw_attr_node *a)
9937 const char *name = dwarf_attr_name (a->dw_attr);
9939 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9941 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9942 return;
9944 switch (AT_class (a))
9946 case dw_val_class_addr:
9947 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9948 break;
9949 case dw_val_class_high_pc:
9950 case dw_val_class_lbl_id:
9951 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9952 break;
9953 default:
9954 gcc_unreachable ();
9958 /* Output a type signature. */
9960 static inline void
9961 output_signature (const char *sig, const char *name)
9963 int i;
9965 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9966 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9969 /* Output a discriminant value. */
9971 static inline void
9972 output_discr_value (dw_discr_value *discr_value, const char *name)
9974 if (discr_value->pos)
9975 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9976 else
9977 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9980 /* Output the DIE and its attributes. Called recursively to generate
9981 the definitions of each child DIE. */
9983 static void
9984 output_die (dw_die_ref die)
9986 dw_attr_node *a;
9987 dw_die_ref c;
9988 unsigned long size;
9989 unsigned ix;
9991 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9992 (unsigned long)die->die_offset,
9993 dwarf_tag_name (die->die_tag));
9995 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9997 const char *name = dwarf_attr_name (a->dw_attr);
9999 switch (AT_class (a))
10001 case dw_val_class_addr:
10002 output_attr_index_or_value (a);
10003 break;
10005 case dw_val_class_offset:
10006 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10007 "%s", name);
10008 break;
10010 case dw_val_class_range_list:
10011 output_range_list_offset (a);
10012 break;
10014 case dw_val_class_loc:
10015 size = size_of_locs (AT_loc (a));
10017 /* Output the block length for this list of location operations. */
10018 if (dwarf_version >= 4)
10019 dw2_asm_output_data_uleb128 (size, "%s", name);
10020 else
10021 dw2_asm_output_data (constant_size (size), size, "%s", name);
10023 output_loc_sequence (AT_loc (a), -1);
10024 break;
10026 case dw_val_class_const:
10027 /* ??? It would be slightly more efficient to use a scheme like is
10028 used for unsigned constants below, but gdb 4.x does not sign
10029 extend. Gdb 5.x does sign extend. */
10030 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10031 break;
10033 case dw_val_class_unsigned_const:
10035 int csize = constant_size (AT_unsigned (a));
10036 if (dwarf_version == 3
10037 && a->dw_attr == DW_AT_data_member_location
10038 && csize >= 4)
10039 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10040 else
10041 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10043 break;
10045 case dw_val_class_const_implicit:
10046 if (flag_debug_asm)
10047 fprintf (asm_out_file, "\t\t\t%s %s ("
10048 HOST_WIDE_INT_PRINT_DEC ")\n",
10049 ASM_COMMENT_START, name, AT_int (a));
10050 break;
10052 case dw_val_class_unsigned_const_implicit:
10053 if (flag_debug_asm)
10054 fprintf (asm_out_file, "\t\t\t%s %s ("
10055 HOST_WIDE_INT_PRINT_HEX ")\n",
10056 ASM_COMMENT_START, name, AT_unsigned (a));
10057 break;
10059 case dw_val_class_const_double:
10061 unsigned HOST_WIDE_INT first, second;
10063 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10064 dw2_asm_output_data (1,
10065 HOST_BITS_PER_DOUBLE_INT
10066 / HOST_BITS_PER_CHAR,
10067 NULL);
10069 if (WORDS_BIG_ENDIAN)
10071 first = a->dw_attr_val.v.val_double.high;
10072 second = a->dw_attr_val.v.val_double.low;
10074 else
10076 first = a->dw_attr_val.v.val_double.low;
10077 second = a->dw_attr_val.v.val_double.high;
10080 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10081 first, "%s", name);
10082 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10083 second, NULL);
10085 break;
10087 case dw_val_class_wide_int:
10089 int i;
10090 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10091 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10092 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10093 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10094 * l, NULL);
10096 if (WORDS_BIG_ENDIAN)
10097 for (i = len - 1; i >= 0; --i)
10099 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10100 "%s", name);
10101 name = "";
10103 else
10104 for (i = 0; i < len; ++i)
10106 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10107 "%s", name);
10108 name = "";
10111 break;
10113 case dw_val_class_vec:
10115 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10116 unsigned int len = a->dw_attr_val.v.val_vec.length;
10117 unsigned int i;
10118 unsigned char *p;
10120 dw2_asm_output_data (constant_size (len * elt_size),
10121 len * elt_size, "%s", name);
10122 if (elt_size > sizeof (HOST_WIDE_INT))
10124 elt_size /= 2;
10125 len *= 2;
10127 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10128 i < len;
10129 i++, p += elt_size)
10130 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10131 "fp or vector constant word %u", i);
10132 break;
10135 case dw_val_class_flag:
10136 if (dwarf_version >= 4)
10138 /* Currently all add_AT_flag calls pass in 1 as last argument,
10139 so DW_FORM_flag_present can be used. If that ever changes,
10140 we'll need to use DW_FORM_flag and have some optimization
10141 in build_abbrev_table that will change those to
10142 DW_FORM_flag_present if it is set to 1 in all DIEs using
10143 the same abbrev entry. */
10144 gcc_assert (AT_flag (a) == 1);
10145 if (flag_debug_asm)
10146 fprintf (asm_out_file, "\t\t\t%s %s\n",
10147 ASM_COMMENT_START, name);
10148 break;
10150 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10151 break;
10153 case dw_val_class_loc_list:
10154 output_loc_list_offset (a);
10155 break;
10157 case dw_val_class_die_ref:
10158 if (AT_ref_external (a))
10160 if (AT_ref (a)->comdat_type_p)
10162 comdat_type_node *type_node
10163 = AT_ref (a)->die_id.die_type_node;
10165 gcc_assert (type_node);
10166 output_signature (type_node->signature, name);
10168 else
10170 const char *sym = AT_ref (a)->die_id.die_symbol;
10171 int size;
10173 gcc_assert (sym);
10174 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10175 length, whereas in DWARF3 it's always sized as an
10176 offset. */
10177 if (dwarf_version == 2)
10178 size = DWARF2_ADDR_SIZE;
10179 else
10180 size = DWARF_OFFSET_SIZE;
10181 /* ??? We cannot unconditionally output die_offset if
10182 non-zero - others might create references to those
10183 DIEs via symbols.
10184 And we do not clear its DIE offset after outputting it
10185 (and the label refers to the actual DIEs, not the
10186 DWARF CU unit header which is when using label + offset
10187 would be the correct thing to do).
10188 ??? This is the reason for the with_offset flag. */
10189 if (AT_ref (a)->with_offset)
10190 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10191 debug_info_section, "%s", name);
10192 else
10193 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10194 name);
10197 else
10199 gcc_assert (AT_ref (a)->die_offset);
10200 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10201 "%s", name);
10203 break;
10205 case dw_val_class_fde_ref:
10207 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10209 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10210 a->dw_attr_val.v.val_fde_index * 2);
10211 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10212 "%s", name);
10214 break;
10216 case dw_val_class_vms_delta:
10217 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10218 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10219 AT_vms_delta2 (a), AT_vms_delta1 (a),
10220 "%s", name);
10221 #else
10222 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10223 AT_vms_delta2 (a), AT_vms_delta1 (a),
10224 "%s", name);
10225 #endif
10226 break;
10228 case dw_val_class_lbl_id:
10229 output_attr_index_or_value (a);
10230 break;
10232 case dw_val_class_lineptr:
10233 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10234 debug_line_section, "%s", name);
10235 break;
10237 case dw_val_class_macptr:
10238 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10239 debug_macinfo_section, "%s", name);
10240 break;
10242 case dw_val_class_loclistsptr:
10243 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10244 debug_loc_section, "%s", name);
10245 break;
10247 case dw_val_class_str:
10248 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10249 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10250 a->dw_attr_val.v.val_str->label,
10251 debug_str_section,
10252 "%s: \"%s\"", name, AT_string (a));
10253 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10254 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10255 a->dw_attr_val.v.val_str->label,
10256 debug_line_str_section,
10257 "%s: \"%s\"", name, AT_string (a));
10258 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10259 dw2_asm_output_data_uleb128 (AT_index (a),
10260 "%s: \"%s\"", name, AT_string (a));
10261 else
10262 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10263 break;
10265 case dw_val_class_file:
10267 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10269 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10270 a->dw_attr_val.v.val_file->filename);
10271 break;
10274 case dw_val_class_file_implicit:
10275 if (flag_debug_asm)
10276 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10277 ASM_COMMENT_START, name,
10278 maybe_emit_file (a->dw_attr_val.v.val_file),
10279 a->dw_attr_val.v.val_file->filename);
10280 break;
10282 case dw_val_class_data8:
10284 int i;
10286 for (i = 0; i < 8; i++)
10287 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10288 i == 0 ? "%s" : NULL, name);
10289 break;
10292 case dw_val_class_high_pc:
10293 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10294 get_AT_low_pc (die), "DW_AT_high_pc");
10295 break;
10297 case dw_val_class_discr_value:
10298 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10299 break;
10301 case dw_val_class_discr_list:
10303 dw_discr_list_ref list = AT_discr_list (a);
10304 const int size = size_of_discr_list (list);
10306 /* This is a block, so output its length first. */
10307 dw2_asm_output_data (constant_size (size), size,
10308 "%s: block size", name);
10310 for (; list != NULL; list = list->dw_discr_next)
10312 /* One byte for the discriminant value descriptor, and then as
10313 many LEB128 numbers as required. */
10314 if (list->dw_discr_range)
10315 dw2_asm_output_data (1, DW_DSC_range,
10316 "%s: DW_DSC_range", name);
10317 else
10318 dw2_asm_output_data (1, DW_DSC_label,
10319 "%s: DW_DSC_label", name);
10321 output_discr_value (&list->dw_discr_lower_bound, name);
10322 if (list->dw_discr_range)
10323 output_discr_value (&list->dw_discr_upper_bound, name);
10325 break;
10328 default:
10329 gcc_unreachable ();
10333 FOR_EACH_CHILD (die, c, output_die (c));
10335 /* Add null byte to terminate sibling list. */
10336 if (die->die_child != NULL)
10337 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10338 (unsigned long) die->die_offset);
10341 /* Output the compilation unit that appears at the beginning of the
10342 .debug_info section, and precedes the DIE descriptions. */
10344 static void
10345 output_compilation_unit_header (enum dwarf_unit_type ut)
10347 if (!XCOFF_DEBUGGING_INFO)
10349 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10350 dw2_asm_output_data (4, 0xffffffff,
10351 "Initial length escape value indicating 64-bit DWARF extension");
10352 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10353 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10354 "Length of Compilation Unit Info");
10357 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10358 if (dwarf_version >= 5)
10360 const char *name;
10361 switch (ut)
10363 case DW_UT_compile: name = "DW_UT_compile"; break;
10364 case DW_UT_type: name = "DW_UT_type"; break;
10365 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10366 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10367 default: gcc_unreachable ();
10369 dw2_asm_output_data (1, ut, "%s", name);
10370 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10372 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10373 debug_abbrev_section,
10374 "Offset Into Abbrev. Section");
10375 if (dwarf_version < 5)
10376 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10379 /* Output the compilation unit DIE and its children. */
10381 static void
10382 output_comp_unit (dw_die_ref die, int output_if_empty,
10383 const unsigned char *dwo_id)
10385 const char *secname, *oldsym;
10386 char *tmp;
10388 /* Unless we are outputting main CU, we may throw away empty ones. */
10389 if (!output_if_empty && die->die_child == NULL)
10390 return;
10392 /* Even if there are no children of this DIE, we must output the information
10393 about the compilation unit. Otherwise, on an empty translation unit, we
10394 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10395 will then complain when examining the file. First mark all the DIEs in
10396 this CU so we know which get local refs. */
10397 mark_dies (die);
10399 external_ref_hash_type *extern_map = optimize_external_refs (die);
10401 /* For now, optimize only the main CU, in order to optimize the rest
10402 we'd need to see all of them earlier. Leave the rest for post-linking
10403 tools like DWZ. */
10404 if (die == comp_unit_die ())
10405 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10407 build_abbrev_table (die, extern_map);
10409 optimize_abbrev_table ();
10411 delete extern_map;
10413 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10414 next_die_offset = (dwo_id
10415 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10416 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10417 calc_die_sizes (die);
10419 oldsym = die->die_id.die_symbol;
10420 if (oldsym && die->comdat_type_p)
10422 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10424 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10425 secname = tmp;
10426 die->die_id.die_symbol = NULL;
10427 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10429 else
10431 switch_to_section (debug_info_section);
10432 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10433 info_section_emitted = true;
10436 /* For LTO cross unit DIE refs we want a symbol on the start of the
10437 debuginfo section, not on the CU DIE. */
10438 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10440 /* ??? No way to get visibility assembled without a decl. */
10441 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10442 get_identifier (oldsym), char_type_node);
10443 TREE_PUBLIC (decl) = true;
10444 TREE_STATIC (decl) = true;
10445 DECL_ARTIFICIAL (decl) = true;
10446 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10447 DECL_VISIBILITY_SPECIFIED (decl) = true;
10448 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10449 #ifdef ASM_WEAKEN_LABEL
10450 /* We prefer a .weak because that handles duplicates from duplicate
10451 archive members in a graceful way. */
10452 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10453 #else
10454 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10455 #endif
10456 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10459 /* Output debugging information. */
10460 output_compilation_unit_header (dwo_id
10461 ? DW_UT_split_compile : DW_UT_compile);
10462 if (dwarf_version >= 5)
10464 if (dwo_id != NULL)
10465 for (int i = 0; i < 8; i++)
10466 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10468 output_die (die);
10470 /* Leave the marks on the main CU, so we can check them in
10471 output_pubnames. */
10472 if (oldsym)
10474 unmark_dies (die);
10475 die->die_id.die_symbol = oldsym;
10479 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10480 and .debug_pubtypes. This is configured per-target, but can be
10481 overridden by the -gpubnames or -gno-pubnames options. */
10483 static inline bool
10484 want_pubnames (void)
10486 if (debug_info_level <= DINFO_LEVEL_TERSE)
10487 return false;
10488 if (debug_generate_pub_sections != -1)
10489 return debug_generate_pub_sections;
10490 return targetm.want_debug_pub_sections;
10493 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10495 static void
10496 add_AT_pubnames (dw_die_ref die)
10498 if (want_pubnames ())
10499 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10502 /* Add a string attribute value to a skeleton DIE. */
10504 static inline void
10505 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10506 const char *str)
10508 dw_attr_node attr;
10509 struct indirect_string_node *node;
10511 if (! skeleton_debug_str_hash)
10512 skeleton_debug_str_hash
10513 = hash_table<indirect_string_hasher>::create_ggc (10);
10515 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10516 find_string_form (node);
10517 if (node->form == DW_FORM_GNU_str_index)
10518 node->form = DW_FORM_strp;
10520 attr.dw_attr = attr_kind;
10521 attr.dw_attr_val.val_class = dw_val_class_str;
10522 attr.dw_attr_val.val_entry = NULL;
10523 attr.dw_attr_val.v.val_str = node;
10524 add_dwarf_attr (die, &attr);
10527 /* Helper function to generate top-level dies for skeleton debug_info and
10528 debug_types. */
10530 static void
10531 add_top_level_skeleton_die_attrs (dw_die_ref die)
10533 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10534 const char *comp_dir = comp_dir_string ();
10536 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10537 if (comp_dir != NULL)
10538 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10539 add_AT_pubnames (die);
10540 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10543 /* Output skeleton debug sections that point to the dwo file. */
10545 static void
10546 output_skeleton_debug_sections (dw_die_ref comp_unit,
10547 const unsigned char *dwo_id)
10549 /* These attributes will be found in the full debug_info section. */
10550 remove_AT (comp_unit, DW_AT_producer);
10551 remove_AT (comp_unit, DW_AT_language);
10553 switch_to_section (debug_skeleton_info_section);
10554 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10556 /* Produce the skeleton compilation-unit header. This one differs enough from
10557 a normal CU header that it's better not to call output_compilation_unit
10558 header. */
10559 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10560 dw2_asm_output_data (4, 0xffffffff,
10561 "Initial length escape value indicating 64-bit "
10562 "DWARF extension");
10564 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10565 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10566 - DWARF_INITIAL_LENGTH_SIZE
10567 + size_of_die (comp_unit),
10568 "Length of Compilation Unit Info");
10569 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10570 if (dwarf_version >= 5)
10572 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10573 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10575 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10576 debug_skeleton_abbrev_section,
10577 "Offset Into Abbrev. Section");
10578 if (dwarf_version < 5)
10579 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10580 else
10581 for (int i = 0; i < 8; i++)
10582 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10584 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10585 output_die (comp_unit);
10587 /* Build the skeleton debug_abbrev section. */
10588 switch_to_section (debug_skeleton_abbrev_section);
10589 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10591 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10593 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10596 /* Output a comdat type unit DIE and its children. */
10598 static void
10599 output_comdat_type_unit (comdat_type_node *node)
10601 const char *secname;
10602 char *tmp;
10603 int i;
10604 #if defined (OBJECT_FORMAT_ELF)
10605 tree comdat_key;
10606 #endif
10608 /* First mark all the DIEs in this CU so we know which get local refs. */
10609 mark_dies (node->root_die);
10611 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10613 build_abbrev_table (node->root_die, extern_map);
10615 delete extern_map;
10616 extern_map = NULL;
10618 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10619 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10620 calc_die_sizes (node->root_die);
10622 #if defined (OBJECT_FORMAT_ELF)
10623 if (dwarf_version >= 5)
10625 if (!dwarf_split_debug_info)
10626 secname = ".debug_info";
10627 else
10628 secname = ".debug_info.dwo";
10630 else if (!dwarf_split_debug_info)
10631 secname = ".debug_types";
10632 else
10633 secname = ".debug_types.dwo";
10635 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10636 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10637 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10638 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10639 comdat_key = get_identifier (tmp);
10640 targetm.asm_out.named_section (secname,
10641 SECTION_DEBUG | SECTION_LINKONCE,
10642 comdat_key);
10643 #else
10644 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10645 sprintf (tmp, (dwarf_version >= 5
10646 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10647 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10648 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10649 secname = tmp;
10650 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10651 #endif
10653 /* Output debugging information. */
10654 output_compilation_unit_header (dwarf_split_debug_info
10655 ? DW_UT_split_type : DW_UT_type);
10656 output_signature (node->signature, "Type Signature");
10657 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10658 "Offset to Type DIE");
10659 output_die (node->root_die);
10661 unmark_dies (node->root_die);
10664 /* Return the DWARF2/3 pubname associated with a decl. */
10666 static const char *
10667 dwarf2_name (tree decl, int scope)
10669 if (DECL_NAMELESS (decl))
10670 return NULL;
10671 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10674 /* Add a new entry to .debug_pubnames if appropriate. */
10676 static void
10677 add_pubname_string (const char *str, dw_die_ref die)
10679 pubname_entry e;
10681 e.die = die;
10682 e.name = xstrdup (str);
10683 vec_safe_push (pubname_table, e);
10686 static void
10687 add_pubname (tree decl, dw_die_ref die)
10689 if (!want_pubnames ())
10690 return;
10692 /* Don't add items to the table when we expect that the consumer will have
10693 just read the enclosing die. For example, if the consumer is looking at a
10694 class_member, it will either be inside the class already, or will have just
10695 looked up the class to find the member. Either way, searching the class is
10696 faster than searching the index. */
10697 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10698 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10700 const char *name = dwarf2_name (decl, 1);
10702 if (name)
10703 add_pubname_string (name, die);
10707 /* Add an enumerator to the pubnames section. */
10709 static void
10710 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10712 pubname_entry e;
10714 gcc_assert (scope_name);
10715 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10716 e.die = die;
10717 vec_safe_push (pubname_table, e);
10720 /* Add a new entry to .debug_pubtypes if appropriate. */
10722 static void
10723 add_pubtype (tree decl, dw_die_ref die)
10725 pubname_entry e;
10727 if (!want_pubnames ())
10728 return;
10730 if ((TREE_PUBLIC (decl)
10731 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10732 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10734 tree scope = NULL;
10735 const char *scope_name = "";
10736 const char *sep = is_cxx () ? "::" : ".";
10737 const char *name;
10739 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10740 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10742 scope_name = lang_hooks.dwarf_name (scope, 1);
10743 if (scope_name != NULL && scope_name[0] != '\0')
10744 scope_name = concat (scope_name, sep, NULL);
10745 else
10746 scope_name = "";
10749 if (TYPE_P (decl))
10750 name = type_tag (decl);
10751 else
10752 name = lang_hooks.dwarf_name (decl, 1);
10754 /* If we don't have a name for the type, there's no point in adding
10755 it to the table. */
10756 if (name != NULL && name[0] != '\0')
10758 e.die = die;
10759 e.name = concat (scope_name, name, NULL);
10760 vec_safe_push (pubtype_table, e);
10763 /* Although it might be more consistent to add the pubinfo for the
10764 enumerators as their dies are created, they should only be added if the
10765 enum type meets the criteria above. So rather than re-check the parent
10766 enum type whenever an enumerator die is created, just output them all
10767 here. This isn't protected by the name conditional because anonymous
10768 enums don't have names. */
10769 if (die->die_tag == DW_TAG_enumeration_type)
10771 dw_die_ref c;
10773 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10778 /* Output a single entry in the pubnames table. */
10780 static void
10781 output_pubname (dw_offset die_offset, pubname_entry *entry)
10783 dw_die_ref die = entry->die;
10784 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10786 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10788 if (debug_generate_pub_sections == 2)
10790 /* This logic follows gdb's method for determining the value of the flag
10791 byte. */
10792 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10793 switch (die->die_tag)
10795 case DW_TAG_typedef:
10796 case DW_TAG_base_type:
10797 case DW_TAG_subrange_type:
10798 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10799 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10800 break;
10801 case DW_TAG_enumerator:
10802 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10803 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10804 if (!is_cxx ())
10805 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10806 break;
10807 case DW_TAG_subprogram:
10808 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10809 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10810 if (!is_ada ())
10811 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10812 break;
10813 case DW_TAG_constant:
10814 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10815 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10816 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10817 break;
10818 case DW_TAG_variable:
10819 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10820 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10821 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10822 break;
10823 case DW_TAG_namespace:
10824 case DW_TAG_imported_declaration:
10825 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10826 break;
10827 case DW_TAG_class_type:
10828 case DW_TAG_interface_type:
10829 case DW_TAG_structure_type:
10830 case DW_TAG_union_type:
10831 case DW_TAG_enumeration_type:
10832 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10833 if (!is_cxx ())
10834 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10835 break;
10836 default:
10837 /* An unusual tag. Leave the flag-byte empty. */
10838 break;
10840 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10841 "GDB-index flags");
10844 dw2_asm_output_nstring (entry->name, -1, "external name");
10848 /* Output the public names table used to speed up access to externally
10849 visible names; or the public types table used to find type definitions. */
10851 static void
10852 output_pubnames (vec<pubname_entry, va_gc> *names)
10854 unsigned i;
10855 unsigned long pubnames_length = size_of_pubnames (names);
10856 pubname_entry *pub;
10858 if (!XCOFF_DEBUGGING_INFO)
10860 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10861 dw2_asm_output_data (4, 0xffffffff,
10862 "Initial length escape value indicating 64-bit DWARF extension");
10863 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10864 "Pub Info Length");
10867 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10868 dw2_asm_output_data (2, 2, "DWARF Version");
10870 if (dwarf_split_debug_info)
10871 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10872 debug_skeleton_info_section,
10873 "Offset of Compilation Unit Info");
10874 else
10875 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10876 debug_info_section,
10877 "Offset of Compilation Unit Info");
10878 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10879 "Compilation Unit Length");
10881 FOR_EACH_VEC_ELT (*names, i, pub)
10883 if (include_pubname_in_output (names, pub))
10885 dw_offset die_offset = pub->die->die_offset;
10887 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10888 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10889 gcc_assert (pub->die->die_mark);
10891 /* If we're putting types in their own .debug_types sections,
10892 the .debug_pubtypes table will still point to the compile
10893 unit (not the type unit), so we want to use the offset of
10894 the skeleton DIE (if there is one). */
10895 if (pub->die->comdat_type_p && names == pubtype_table)
10897 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10899 if (type_node != NULL)
10900 die_offset = (type_node->skeleton_die != NULL
10901 ? type_node->skeleton_die->die_offset
10902 : comp_unit_die ()->die_offset);
10905 output_pubname (die_offset, pub);
10909 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10912 /* Output public names and types tables if necessary. */
10914 static void
10915 output_pubtables (void)
10917 if (!want_pubnames () || !info_section_emitted)
10918 return;
10920 switch_to_section (debug_pubnames_section);
10921 output_pubnames (pubname_table);
10922 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10923 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10924 simply won't look for the section. */
10925 switch_to_section (debug_pubtypes_section);
10926 output_pubnames (pubtype_table);
10930 /* Output the information that goes into the .debug_aranges table.
10931 Namely, define the beginning and ending address range of the
10932 text section generated for this compilation unit. */
10934 static void
10935 output_aranges (void)
10937 unsigned i;
10938 unsigned long aranges_length = size_of_aranges ();
10940 if (!XCOFF_DEBUGGING_INFO)
10942 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10943 dw2_asm_output_data (4, 0xffffffff,
10944 "Initial length escape value indicating 64-bit DWARF extension");
10945 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10946 "Length of Address Ranges Info");
10949 /* Version number for aranges is still 2, even up to DWARF5. */
10950 dw2_asm_output_data (2, 2, "DWARF Version");
10951 if (dwarf_split_debug_info)
10952 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10953 debug_skeleton_info_section,
10954 "Offset of Compilation Unit Info");
10955 else
10956 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10957 debug_info_section,
10958 "Offset of Compilation Unit Info");
10959 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10960 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10962 /* We need to align to twice the pointer size here. */
10963 if (DWARF_ARANGES_PAD_SIZE)
10965 /* Pad using a 2 byte words so that padding is correct for any
10966 pointer size. */
10967 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10968 2 * DWARF2_ADDR_SIZE);
10969 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10970 dw2_asm_output_data (2, 0, NULL);
10973 /* It is necessary not to output these entries if the sections were
10974 not used; if the sections were not used, the length will be 0 and
10975 the address may end up as 0 if the section is discarded by ld
10976 --gc-sections, leaving an invalid (0, 0) entry that can be
10977 confused with the terminator. */
10978 if (text_section_used)
10980 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10981 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10982 text_section_label, "Length");
10984 if (cold_text_section_used)
10986 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10987 "Address");
10988 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10989 cold_text_section_label, "Length");
10992 if (have_multiple_function_sections)
10994 unsigned fde_idx;
10995 dw_fde_ref fde;
10997 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10999 if (DECL_IGNORED_P (fde->decl))
11000 continue;
11001 if (!fde->in_std_section)
11003 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11004 "Address");
11005 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11006 fde->dw_fde_begin, "Length");
11008 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11010 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11011 "Address");
11012 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11013 fde->dw_fde_second_begin, "Length");
11018 /* Output the terminator words. */
11019 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11020 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11023 /* Add a new entry to .debug_ranges. Return its index into
11024 ranges_table vector. */
11026 static unsigned int
11027 add_ranges_num (int num, bool maybe_new_sec)
11029 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11030 vec_safe_push (ranges_table, r);
11031 return vec_safe_length (ranges_table) - 1;
11034 /* Add a new entry to .debug_ranges corresponding to a block, or a
11035 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11036 this entry might be in a different section from previous range. */
11038 static unsigned int
11039 add_ranges (const_tree block, bool maybe_new_sec)
11041 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11044 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11045 chain, or middle entry of a chain that will be directly referred to. */
11047 static void
11048 note_rnglist_head (unsigned int offset)
11050 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11051 return;
11052 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11055 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11056 When using dwarf_split_debug_info, address attributes in dies destined
11057 for the final executable should be direct references--setting the
11058 parameter force_direct ensures this behavior. */
11060 static void
11061 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11062 bool *added, bool force_direct)
11064 unsigned int in_use = vec_safe_length (ranges_by_label);
11065 unsigned int offset;
11066 dw_ranges_by_label rbl = { begin, end };
11067 vec_safe_push (ranges_by_label, rbl);
11068 offset = add_ranges_num (-(int)in_use - 1, true);
11069 if (!*added)
11071 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11072 *added = true;
11073 note_rnglist_head (offset);
11077 /* Emit .debug_ranges section. */
11079 static void
11080 output_ranges (void)
11082 unsigned i;
11083 static const char *const start_fmt = "Offset %#x";
11084 const char *fmt = start_fmt;
11085 dw_ranges *r;
11087 switch_to_section (debug_ranges_section);
11088 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11089 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11091 int block_num = r->num;
11093 if (block_num > 0)
11095 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11096 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11098 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11099 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11101 /* If all code is in the text section, then the compilation
11102 unit base address defaults to DW_AT_low_pc, which is the
11103 base of the text section. */
11104 if (!have_multiple_function_sections)
11106 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11107 text_section_label,
11108 fmt, i * 2 * DWARF2_ADDR_SIZE);
11109 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11110 text_section_label, NULL);
11113 /* Otherwise, the compilation unit base address is zero,
11114 which allows us to use absolute addresses, and not worry
11115 about whether the target supports cross-section
11116 arithmetic. */
11117 else
11119 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11120 fmt, i * 2 * DWARF2_ADDR_SIZE);
11121 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11124 fmt = NULL;
11127 /* Negative block_num stands for an index into ranges_by_label. */
11128 else if (block_num < 0)
11130 int lab_idx = - block_num - 1;
11132 if (!have_multiple_function_sections)
11134 gcc_unreachable ();
11135 #if 0
11136 /* If we ever use add_ranges_by_labels () for a single
11137 function section, all we have to do is to take out
11138 the #if 0 above. */
11139 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11140 (*ranges_by_label)[lab_idx].begin,
11141 text_section_label,
11142 fmt, i * 2 * DWARF2_ADDR_SIZE);
11143 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11144 (*ranges_by_label)[lab_idx].end,
11145 text_section_label, NULL);
11146 #endif
11148 else
11150 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11151 (*ranges_by_label)[lab_idx].begin,
11152 fmt, i * 2 * DWARF2_ADDR_SIZE);
11153 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11154 (*ranges_by_label)[lab_idx].end,
11155 NULL);
11158 else
11160 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11161 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11162 fmt = start_fmt;
11167 /* Non-zero if .debug_line_str should be used for .debug_line section
11168 strings or strings that are likely shareable with those. */
11169 #define DWARF5_USE_DEBUG_LINE_STR \
11170 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11171 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11172 /* FIXME: there is no .debug_line_str.dwo section, \
11173 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11174 && !dwarf_split_debug_info)
11176 /* Assign .debug_rnglists indexes. */
11178 static void
11179 index_rnglists (void)
11181 unsigned i;
11182 dw_ranges *r;
11184 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11185 if (r->label)
11186 r->idx = rnglist_idx++;
11189 /* Emit .debug_rnglists section. */
11191 static void
11192 output_rnglists (void)
11194 unsigned i;
11195 dw_ranges *r;
11196 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11197 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11198 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11200 switch_to_section (debug_ranges_section);
11201 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11202 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11203 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11204 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11205 dw2_asm_output_data (4, 0xffffffff,
11206 "Initial length escape value indicating "
11207 "64-bit DWARF extension");
11208 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11209 "Length of Range Lists");
11210 ASM_OUTPUT_LABEL (asm_out_file, l1);
11211 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11212 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11213 dw2_asm_output_data (1, 0, "Segment Size");
11214 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11215 about relocation sizes and primarily care about the size of .debug*
11216 sections in linked shared libraries and executables, then
11217 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11218 into it are usually larger than just DW_FORM_sec_offset offsets
11219 into the .debug_rnglists section. */
11220 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11221 "Offset Entry Count");
11222 if (dwarf_split_debug_info)
11224 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11225 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11226 if (r->label)
11227 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11228 ranges_base_label, NULL);
11231 const char *lab = "";
11232 unsigned int len = vec_safe_length (ranges_table);
11233 const char *base = NULL;
11234 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11236 int block_num = r->num;
11238 if (r->label)
11240 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11241 lab = r->label;
11243 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11244 base = NULL;
11245 if (block_num > 0)
11247 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11248 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11250 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11251 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11253 if (HAVE_AS_LEB128)
11255 /* If all code is in the text section, then the compilation
11256 unit base address defaults to DW_AT_low_pc, which is the
11257 base of the text section. */
11258 if (!have_multiple_function_sections)
11260 dw2_asm_output_data (1, DW_RLE_offset_pair,
11261 "DW_RLE_offset_pair (%s)", lab);
11262 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11263 "Range begin address (%s)", lab);
11264 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11265 "Range end address (%s)", lab);
11266 continue;
11268 if (base == NULL)
11270 dw_ranges *r2 = NULL;
11271 if (i < len - 1)
11272 r2 = &(*ranges_table)[i + 1];
11273 if (r2
11274 && r2->num != 0
11275 && r2->label == NULL
11276 && !r2->maybe_new_sec)
11278 dw2_asm_output_data (1, DW_RLE_base_address,
11279 "DW_RLE_base_address (%s)", lab);
11280 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11281 "Base address (%s)", lab);
11282 strcpy (basebuf, blabel);
11283 base = basebuf;
11286 if (base)
11288 dw2_asm_output_data (1, DW_RLE_offset_pair,
11289 "DW_RLE_offset_pair (%s)", lab);
11290 dw2_asm_output_delta_uleb128 (blabel, base,
11291 "Range begin address (%s)", lab);
11292 dw2_asm_output_delta_uleb128 (elabel, base,
11293 "Range end address (%s)", lab);
11294 continue;
11296 dw2_asm_output_data (1, DW_RLE_start_length,
11297 "DW_RLE_start_length (%s)", lab);
11298 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11299 "Range begin address (%s)", lab);
11300 dw2_asm_output_delta_uleb128 (elabel, blabel,
11301 "Range length (%s)", lab);
11303 else
11305 dw2_asm_output_data (1, DW_RLE_start_end,
11306 "DW_RLE_start_end (%s)", lab);
11307 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11308 "Range begin address (%s)", lab);
11309 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11310 "Range end address (%s)", lab);
11314 /* Negative block_num stands for an index into ranges_by_label. */
11315 else if (block_num < 0)
11317 int lab_idx = - block_num - 1;
11318 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11319 const char *elabel = (*ranges_by_label)[lab_idx].end;
11321 if (!have_multiple_function_sections)
11322 gcc_unreachable ();
11323 if (HAVE_AS_LEB128)
11325 dw2_asm_output_data (1, DW_RLE_start_length,
11326 "DW_RLE_start_length (%s)", lab);
11327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11328 "Range begin address (%s)", lab);
11329 dw2_asm_output_delta_uleb128 (elabel, blabel,
11330 "Range length (%s)", lab);
11332 else
11334 dw2_asm_output_data (1, DW_RLE_start_end,
11335 "DW_RLE_start_end (%s)", lab);
11336 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11337 "Range begin address (%s)", lab);
11338 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11339 "Range end address (%s)", lab);
11342 else
11343 dw2_asm_output_data (1, DW_RLE_end_of_list,
11344 "DW_RLE_end_of_list (%s)", lab);
11346 ASM_OUTPUT_LABEL (asm_out_file, l2);
11349 /* Data structure containing information about input files. */
11350 struct file_info
11352 const char *path; /* Complete file name. */
11353 const char *fname; /* File name part. */
11354 int length; /* Length of entire string. */
11355 struct dwarf_file_data * file_idx; /* Index in input file table. */
11356 int dir_idx; /* Index in directory table. */
11359 /* Data structure containing information about directories with source
11360 files. */
11361 struct dir_info
11363 const char *path; /* Path including directory name. */
11364 int length; /* Path length. */
11365 int prefix; /* Index of directory entry which is a prefix. */
11366 int count; /* Number of files in this directory. */
11367 int dir_idx; /* Index of directory used as base. */
11370 /* Callback function for file_info comparison. We sort by looking at
11371 the directories in the path. */
11373 static int
11374 file_info_cmp (const void *p1, const void *p2)
11376 const struct file_info *const s1 = (const struct file_info *) p1;
11377 const struct file_info *const s2 = (const struct file_info *) p2;
11378 const unsigned char *cp1;
11379 const unsigned char *cp2;
11381 /* Take care of file names without directories. We need to make sure that
11382 we return consistent values to qsort since some will get confused if
11383 we return the same value when identical operands are passed in opposite
11384 orders. So if neither has a directory, return 0 and otherwise return
11385 1 or -1 depending on which one has the directory. */
11386 if ((s1->path == s1->fname || s2->path == s2->fname))
11387 return (s2->path == s2->fname) - (s1->path == s1->fname);
11389 cp1 = (const unsigned char *) s1->path;
11390 cp2 = (const unsigned char *) s2->path;
11392 while (1)
11394 ++cp1;
11395 ++cp2;
11396 /* Reached the end of the first path? If so, handle like above. */
11397 if ((cp1 == (const unsigned char *) s1->fname)
11398 || (cp2 == (const unsigned char *) s2->fname))
11399 return ((cp2 == (const unsigned char *) s2->fname)
11400 - (cp1 == (const unsigned char *) s1->fname));
11402 /* Character of current path component the same? */
11403 else if (*cp1 != *cp2)
11404 return *cp1 - *cp2;
11408 struct file_name_acquire_data
11410 struct file_info *files;
11411 int used_files;
11412 int max_files;
11415 /* Traversal function for the hash table. */
11418 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11420 struct dwarf_file_data *d = *slot;
11421 struct file_info *fi;
11422 const char *f;
11424 gcc_assert (fnad->max_files >= d->emitted_number);
11426 if (! d->emitted_number)
11427 return 1;
11429 gcc_assert (fnad->max_files != fnad->used_files);
11431 fi = fnad->files + fnad->used_files++;
11433 /* Skip all leading "./". */
11434 f = d->filename;
11435 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11436 f += 2;
11438 /* Create a new array entry. */
11439 fi->path = f;
11440 fi->length = strlen (f);
11441 fi->file_idx = d;
11443 /* Search for the file name part. */
11444 f = strrchr (f, DIR_SEPARATOR);
11445 #if defined (DIR_SEPARATOR_2)
11447 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11449 if (g != NULL)
11451 if (f == NULL || f < g)
11452 f = g;
11455 #endif
11457 fi->fname = f == NULL ? fi->path : f + 1;
11458 return 1;
11461 /* Helper function for output_file_names. Emit a FORM encoded
11462 string STR, with assembly comment start ENTRY_KIND and
11463 index IDX */
11465 static void
11466 output_line_string (enum dwarf_form form, const char *str,
11467 const char *entry_kind, unsigned int idx)
11469 switch (form)
11471 case DW_FORM_string:
11472 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11473 break;
11474 case DW_FORM_line_strp:
11475 if (!debug_line_str_hash)
11476 debug_line_str_hash
11477 = hash_table<indirect_string_hasher>::create_ggc (10);
11479 struct indirect_string_node *node;
11480 node = find_AT_string_in_table (str, debug_line_str_hash);
11481 set_indirect_string (node);
11482 node->form = form;
11483 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11484 debug_line_str_section, "%s: %#x: \"%s\"",
11485 entry_kind, 0, node->str);
11486 break;
11487 default:
11488 gcc_unreachable ();
11492 /* Output the directory table and the file name table. We try to minimize
11493 the total amount of memory needed. A heuristic is used to avoid large
11494 slowdowns with many input files. */
11496 static void
11497 output_file_names (void)
11499 struct file_name_acquire_data fnad;
11500 int numfiles;
11501 struct file_info *files;
11502 struct dir_info *dirs;
11503 int *saved;
11504 int *savehere;
11505 int *backmap;
11506 int ndirs;
11507 int idx_offset;
11508 int i;
11510 if (!last_emitted_file)
11512 if (dwarf_version >= 5)
11514 dw2_asm_output_data (1, 0, "Directory entry format count");
11515 dw2_asm_output_data_uleb128 (0, "Directories count");
11516 dw2_asm_output_data (1, 0, "File name entry format count");
11517 dw2_asm_output_data_uleb128 (0, "File names count");
11519 else
11521 dw2_asm_output_data (1, 0, "End directory table");
11522 dw2_asm_output_data (1, 0, "End file name table");
11524 return;
11527 numfiles = last_emitted_file->emitted_number;
11529 /* Allocate the various arrays we need. */
11530 files = XALLOCAVEC (struct file_info, numfiles);
11531 dirs = XALLOCAVEC (struct dir_info, numfiles);
11533 fnad.files = files;
11534 fnad.used_files = 0;
11535 fnad.max_files = numfiles;
11536 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11537 gcc_assert (fnad.used_files == fnad.max_files);
11539 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11541 /* Find all the different directories used. */
11542 dirs[0].path = files[0].path;
11543 dirs[0].length = files[0].fname - files[0].path;
11544 dirs[0].prefix = -1;
11545 dirs[0].count = 1;
11546 dirs[0].dir_idx = 0;
11547 files[0].dir_idx = 0;
11548 ndirs = 1;
11550 for (i = 1; i < numfiles; i++)
11551 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11552 && memcmp (dirs[ndirs - 1].path, files[i].path,
11553 dirs[ndirs - 1].length) == 0)
11555 /* Same directory as last entry. */
11556 files[i].dir_idx = ndirs - 1;
11557 ++dirs[ndirs - 1].count;
11559 else
11561 int j;
11563 /* This is a new directory. */
11564 dirs[ndirs].path = files[i].path;
11565 dirs[ndirs].length = files[i].fname - files[i].path;
11566 dirs[ndirs].count = 1;
11567 dirs[ndirs].dir_idx = ndirs;
11568 files[i].dir_idx = ndirs;
11570 /* Search for a prefix. */
11571 dirs[ndirs].prefix = -1;
11572 for (j = 0; j < ndirs; j++)
11573 if (dirs[j].length < dirs[ndirs].length
11574 && dirs[j].length > 1
11575 && (dirs[ndirs].prefix == -1
11576 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11577 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11578 dirs[ndirs].prefix = j;
11580 ++ndirs;
11583 /* Now to the actual work. We have to find a subset of the directories which
11584 allow expressing the file name using references to the directory table
11585 with the least amount of characters. We do not do an exhaustive search
11586 where we would have to check out every combination of every single
11587 possible prefix. Instead we use a heuristic which provides nearly optimal
11588 results in most cases and never is much off. */
11589 saved = XALLOCAVEC (int, ndirs);
11590 savehere = XALLOCAVEC (int, ndirs);
11592 memset (saved, '\0', ndirs * sizeof (saved[0]));
11593 for (i = 0; i < ndirs; i++)
11595 int j;
11596 int total;
11598 /* We can always save some space for the current directory. But this
11599 does not mean it will be enough to justify adding the directory. */
11600 savehere[i] = dirs[i].length;
11601 total = (savehere[i] - saved[i]) * dirs[i].count;
11603 for (j = i + 1; j < ndirs; j++)
11605 savehere[j] = 0;
11606 if (saved[j] < dirs[i].length)
11608 /* Determine whether the dirs[i] path is a prefix of the
11609 dirs[j] path. */
11610 int k;
11612 k = dirs[j].prefix;
11613 while (k != -1 && k != (int) i)
11614 k = dirs[k].prefix;
11616 if (k == (int) i)
11618 /* Yes it is. We can possibly save some memory by
11619 writing the filenames in dirs[j] relative to
11620 dirs[i]. */
11621 savehere[j] = dirs[i].length;
11622 total += (savehere[j] - saved[j]) * dirs[j].count;
11627 /* Check whether we can save enough to justify adding the dirs[i]
11628 directory. */
11629 if (total > dirs[i].length + 1)
11631 /* It's worthwhile adding. */
11632 for (j = i; j < ndirs; j++)
11633 if (savehere[j] > 0)
11635 /* Remember how much we saved for this directory so far. */
11636 saved[j] = savehere[j];
11638 /* Remember the prefix directory. */
11639 dirs[j].dir_idx = i;
11644 /* Emit the directory name table. */
11645 idx_offset = dirs[0].length > 0 ? 1 : 0;
11646 enum dwarf_form str_form = DW_FORM_string;
11647 enum dwarf_form idx_form = DW_FORM_udata;
11648 if (dwarf_version >= 5)
11650 const char *comp_dir = comp_dir_string ();
11651 if (comp_dir == NULL)
11652 comp_dir = "";
11653 dw2_asm_output_data (1, 1, "Directory entry format count");
11654 if (DWARF5_USE_DEBUG_LINE_STR)
11655 str_form = DW_FORM_line_strp;
11656 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11657 dw2_asm_output_data_uleb128 (str_form, "%s",
11658 get_DW_FORM_name (str_form));
11659 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11660 if (str_form == DW_FORM_string)
11662 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11663 for (i = 1 - idx_offset; i < ndirs; i++)
11664 dw2_asm_output_nstring (dirs[i].path,
11665 dirs[i].length
11666 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11667 "Directory Entry: %#x", i + idx_offset);
11669 else
11671 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11672 for (i = 1 - idx_offset; i < ndirs; i++)
11674 const char *str
11675 = ggc_alloc_string (dirs[i].path,
11676 dirs[i].length
11677 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11678 output_line_string (str_form, str, "Directory Entry",
11679 (unsigned) i + idx_offset);
11683 else
11685 for (i = 1 - idx_offset; i < ndirs; i++)
11686 dw2_asm_output_nstring (dirs[i].path,
11687 dirs[i].length
11688 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11689 "Directory Entry: %#x", i + idx_offset);
11691 dw2_asm_output_data (1, 0, "End directory table");
11694 /* We have to emit them in the order of emitted_number since that's
11695 used in the debug info generation. To do this efficiently we
11696 generate a back-mapping of the indices first. */
11697 backmap = XALLOCAVEC (int, numfiles);
11698 for (i = 0; i < numfiles; i++)
11699 backmap[files[i].file_idx->emitted_number - 1] = i;
11701 if (dwarf_version >= 5)
11703 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11704 if (filename0 == NULL)
11705 filename0 = "";
11706 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11707 DW_FORM_data2. Choose one based on the number of directories
11708 and how much space would they occupy in each encoding.
11709 If we have at most 256 directories, all indexes fit into
11710 a single byte, so DW_FORM_data1 is most compact (if there
11711 are at most 128 directories, DW_FORM_udata would be as
11712 compact as that, but not shorter and slower to decode). */
11713 if (ndirs + idx_offset <= 256)
11714 idx_form = DW_FORM_data1;
11715 /* If there are more than 65536 directories, we have to use
11716 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11717 Otherwise, compute what space would occupy if all the indexes
11718 used DW_FORM_udata - sum - and compare that to how large would
11719 be DW_FORM_data2 encoding, and pick the more efficient one. */
11720 else if (ndirs + idx_offset <= 65536)
11722 unsigned HOST_WIDE_INT sum = 1;
11723 for (i = 0; i < numfiles; i++)
11725 int file_idx = backmap[i];
11726 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11727 sum += size_of_uleb128 (dir_idx);
11729 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11730 idx_form = DW_FORM_data2;
11732 #ifdef VMS_DEBUGGING_INFO
11733 dw2_asm_output_data (1, 4, "File name entry format count");
11734 #else
11735 dw2_asm_output_data (1, 2, "File name entry format count");
11736 #endif
11737 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11738 dw2_asm_output_data_uleb128 (str_form, "%s",
11739 get_DW_FORM_name (str_form));
11740 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11741 "DW_LNCT_directory_index");
11742 dw2_asm_output_data_uleb128 (idx_form, "%s",
11743 get_DW_FORM_name (idx_form));
11744 #ifdef VMS_DEBUGGING_INFO
11745 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11746 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11747 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11748 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11749 #endif
11750 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11752 output_line_string (str_form, filename0, "File Entry", 0);
11754 /* Include directory index. */
11755 if (idx_form != DW_FORM_udata)
11756 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11757 0, NULL);
11758 else
11759 dw2_asm_output_data_uleb128 (0, NULL);
11761 #ifdef VMS_DEBUGGING_INFO
11762 dw2_asm_output_data_uleb128 (0, NULL);
11763 dw2_asm_output_data_uleb128 (0, NULL);
11764 #endif
11767 /* Now write all the file names. */
11768 for (i = 0; i < numfiles; i++)
11770 int file_idx = backmap[i];
11771 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11773 #ifdef VMS_DEBUGGING_INFO
11774 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11776 /* Setting these fields can lead to debugger miscomparisons,
11777 but VMS Debug requires them to be set correctly. */
11779 int ver;
11780 long long cdt;
11781 long siz;
11782 int maxfilelen = (strlen (files[file_idx].path)
11783 + dirs[dir_idx].length
11784 + MAX_VMS_VERSION_LEN + 1);
11785 char *filebuf = XALLOCAVEC (char, maxfilelen);
11787 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11788 snprintf (filebuf, maxfilelen, "%s;%d",
11789 files[file_idx].path + dirs[dir_idx].length, ver);
11791 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11793 /* Include directory index. */
11794 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11795 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11796 dir_idx + idx_offset, NULL);
11797 else
11798 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11800 /* Modification time. */
11801 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11802 &cdt, 0, 0, 0) == 0)
11803 ? cdt : 0, NULL);
11805 /* File length in bytes. */
11806 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11807 0, &siz, 0, 0) == 0)
11808 ? siz : 0, NULL);
11809 #else
11810 output_line_string (str_form,
11811 files[file_idx].path + dirs[dir_idx].length,
11812 "File Entry", (unsigned) i + 1);
11814 /* Include directory index. */
11815 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11816 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11817 dir_idx + idx_offset, NULL);
11818 else
11819 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11821 if (dwarf_version >= 5)
11822 continue;
11824 /* Modification time. */
11825 dw2_asm_output_data_uleb128 (0, NULL);
11827 /* File length in bytes. */
11828 dw2_asm_output_data_uleb128 (0, NULL);
11829 #endif /* VMS_DEBUGGING_INFO */
11832 if (dwarf_version < 5)
11833 dw2_asm_output_data (1, 0, "End file name table");
11837 /* Output one line number table into the .debug_line section. */
11839 static void
11840 output_one_line_info_table (dw_line_info_table *table)
11842 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11843 unsigned int current_line = 1;
11844 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11845 dw_line_info_entry *ent;
11846 size_t i;
11848 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11850 switch (ent->opcode)
11852 case LI_set_address:
11853 /* ??? Unfortunately, we have little choice here currently, and
11854 must always use the most general form. GCC does not know the
11855 address delta itself, so we can't use DW_LNS_advance_pc. Many
11856 ports do have length attributes which will give an upper bound
11857 on the address range. We could perhaps use length attributes
11858 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11859 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11861 /* This can handle any delta. This takes
11862 4+DWARF2_ADDR_SIZE bytes. */
11863 dw2_asm_output_data (1, 0, "set address %s", line_label);
11864 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11865 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11866 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11867 break;
11869 case LI_set_line:
11870 if (ent->val == current_line)
11872 /* We still need to start a new row, so output a copy insn. */
11873 dw2_asm_output_data (1, DW_LNS_copy,
11874 "copy line %u", current_line);
11876 else
11878 int line_offset = ent->val - current_line;
11879 int line_delta = line_offset - DWARF_LINE_BASE;
11881 current_line = ent->val;
11882 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11884 /* This can handle deltas from -10 to 234, using the current
11885 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11886 This takes 1 byte. */
11887 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11888 "line %u", current_line);
11890 else
11892 /* This can handle any delta. This takes at least 4 bytes,
11893 depending on the value being encoded. */
11894 dw2_asm_output_data (1, DW_LNS_advance_line,
11895 "advance to line %u", current_line);
11896 dw2_asm_output_data_sleb128 (line_offset, NULL);
11897 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11900 break;
11902 case LI_set_file:
11903 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11904 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11905 break;
11907 case LI_set_column:
11908 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11909 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11910 break;
11912 case LI_negate_stmt:
11913 current_is_stmt = !current_is_stmt;
11914 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11915 "is_stmt %d", current_is_stmt);
11916 break;
11918 case LI_set_prologue_end:
11919 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11920 "set prologue end");
11921 break;
11923 case LI_set_epilogue_begin:
11924 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11925 "set epilogue begin");
11926 break;
11928 case LI_set_discriminator:
11929 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11930 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11931 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11932 dw2_asm_output_data_uleb128 (ent->val, NULL);
11933 break;
11937 /* Emit debug info for the address of the end of the table. */
11938 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11939 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11940 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11941 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11943 dw2_asm_output_data (1, 0, "end sequence");
11944 dw2_asm_output_data_uleb128 (1, NULL);
11945 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11948 /* Output the source line number correspondence information. This
11949 information goes into the .debug_line section. */
11951 static void
11952 output_line_info (bool prologue_only)
11954 static unsigned int generation;
11955 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11956 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11957 bool saw_one = false;
11958 int opc;
11960 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11961 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11962 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11963 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11965 if (!XCOFF_DEBUGGING_INFO)
11967 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11968 dw2_asm_output_data (4, 0xffffffff,
11969 "Initial length escape value indicating 64-bit DWARF extension");
11970 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11971 "Length of Source Line Info");
11974 ASM_OUTPUT_LABEL (asm_out_file, l1);
11976 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11977 if (dwarf_version >= 5)
11979 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11980 dw2_asm_output_data (1, 0, "Segment Size");
11982 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11983 ASM_OUTPUT_LABEL (asm_out_file, p1);
11985 /* Define the architecture-dependent minimum instruction length (in bytes).
11986 In this implementation of DWARF, this field is used for information
11987 purposes only. Since GCC generates assembly language, we have no
11988 a priori knowledge of how many instruction bytes are generated for each
11989 source line, and therefore can use only the DW_LNE_set_address and
11990 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11991 this as '1', which is "correct enough" for all architectures,
11992 and don't let the target override. */
11993 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11995 if (dwarf_version >= 4)
11996 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11997 "Maximum Operations Per Instruction");
11998 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11999 "Default is_stmt_start flag");
12000 dw2_asm_output_data (1, DWARF_LINE_BASE,
12001 "Line Base Value (Special Opcodes)");
12002 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12003 "Line Range Value (Special Opcodes)");
12004 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12005 "Special Opcode Base");
12007 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12009 int n_op_args;
12010 switch (opc)
12012 case DW_LNS_advance_pc:
12013 case DW_LNS_advance_line:
12014 case DW_LNS_set_file:
12015 case DW_LNS_set_column:
12016 case DW_LNS_fixed_advance_pc:
12017 case DW_LNS_set_isa:
12018 n_op_args = 1;
12019 break;
12020 default:
12021 n_op_args = 0;
12022 break;
12025 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12026 opc, n_op_args);
12029 /* Write out the information about the files we use. */
12030 output_file_names ();
12031 ASM_OUTPUT_LABEL (asm_out_file, p2);
12032 if (prologue_only)
12034 /* Output the marker for the end of the line number info. */
12035 ASM_OUTPUT_LABEL (asm_out_file, l2);
12036 return;
12039 if (separate_line_info)
12041 dw_line_info_table *table;
12042 size_t i;
12044 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12045 if (table->in_use)
12047 output_one_line_info_table (table);
12048 saw_one = true;
12051 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12053 output_one_line_info_table (cold_text_section_line_info);
12054 saw_one = true;
12057 /* ??? Some Darwin linkers crash on a .debug_line section with no
12058 sequences. Further, merely a DW_LNE_end_sequence entry is not
12059 sufficient -- the address column must also be initialized.
12060 Make sure to output at least one set_address/end_sequence pair,
12061 choosing .text since that section is always present. */
12062 if (text_section_line_info->in_use || !saw_one)
12063 output_one_line_info_table (text_section_line_info);
12065 /* Output the marker for the end of the line number info. */
12066 ASM_OUTPUT_LABEL (asm_out_file, l2);
12069 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12071 static inline bool
12072 need_endianity_attribute_p (bool reverse)
12074 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12077 /* Given a pointer to a tree node for some base type, return a pointer to
12078 a DIE that describes the given type. REVERSE is true if the type is
12079 to be interpreted in the reverse storage order wrt the target order.
12081 This routine must only be called for GCC type nodes that correspond to
12082 Dwarf base (fundamental) types. */
12084 static dw_die_ref
12085 base_type_die (tree type, bool reverse)
12087 dw_die_ref base_type_result;
12088 enum dwarf_type encoding;
12089 bool fpt_used = false;
12090 struct fixed_point_type_info fpt_info;
12091 tree type_bias = NULL_TREE;
12093 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12094 return 0;
12096 /* If this is a subtype that should not be emitted as a subrange type,
12097 use the base type. See subrange_type_for_debug_p. */
12098 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12099 type = TREE_TYPE (type);
12101 switch (TREE_CODE (type))
12103 case INTEGER_TYPE:
12104 if ((dwarf_version >= 4 || !dwarf_strict)
12105 && TYPE_NAME (type)
12106 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12107 && DECL_IS_BUILTIN (TYPE_NAME (type))
12108 && DECL_NAME (TYPE_NAME (type)))
12110 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12111 if (strcmp (name, "char16_t") == 0
12112 || strcmp (name, "char32_t") == 0)
12114 encoding = DW_ATE_UTF;
12115 break;
12118 if ((dwarf_version >= 3 || !dwarf_strict)
12119 && lang_hooks.types.get_fixed_point_type_info)
12121 memset (&fpt_info, 0, sizeof (fpt_info));
12122 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12124 fpt_used = true;
12125 encoding = ((TYPE_UNSIGNED (type))
12126 ? DW_ATE_unsigned_fixed
12127 : DW_ATE_signed_fixed);
12128 break;
12131 if (TYPE_STRING_FLAG (type))
12133 if (TYPE_UNSIGNED (type))
12134 encoding = DW_ATE_unsigned_char;
12135 else
12136 encoding = DW_ATE_signed_char;
12138 else if (TYPE_UNSIGNED (type))
12139 encoding = DW_ATE_unsigned;
12140 else
12141 encoding = DW_ATE_signed;
12143 if (!dwarf_strict
12144 && lang_hooks.types.get_type_bias)
12145 type_bias = lang_hooks.types.get_type_bias (type);
12146 break;
12148 case REAL_TYPE:
12149 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12151 if (dwarf_version >= 3 || !dwarf_strict)
12152 encoding = DW_ATE_decimal_float;
12153 else
12154 encoding = DW_ATE_lo_user;
12156 else
12157 encoding = DW_ATE_float;
12158 break;
12160 case FIXED_POINT_TYPE:
12161 if (!(dwarf_version >= 3 || !dwarf_strict))
12162 encoding = DW_ATE_lo_user;
12163 else if (TYPE_UNSIGNED (type))
12164 encoding = DW_ATE_unsigned_fixed;
12165 else
12166 encoding = DW_ATE_signed_fixed;
12167 break;
12169 /* Dwarf2 doesn't know anything about complex ints, so use
12170 a user defined type for it. */
12171 case COMPLEX_TYPE:
12172 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12173 encoding = DW_ATE_complex_float;
12174 else
12175 encoding = DW_ATE_lo_user;
12176 break;
12178 case BOOLEAN_TYPE:
12179 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12180 encoding = DW_ATE_boolean;
12181 break;
12183 default:
12184 /* No other TREE_CODEs are Dwarf fundamental types. */
12185 gcc_unreachable ();
12188 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12190 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12191 int_size_in_bytes (type));
12192 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12194 if (need_endianity_attribute_p (reverse))
12195 add_AT_unsigned (base_type_result, DW_AT_endianity,
12196 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12198 add_alignment_attribute (base_type_result, type);
12200 if (fpt_used)
12202 switch (fpt_info.scale_factor_kind)
12204 case fixed_point_scale_factor_binary:
12205 add_AT_int (base_type_result, DW_AT_binary_scale,
12206 fpt_info.scale_factor.binary);
12207 break;
12209 case fixed_point_scale_factor_decimal:
12210 add_AT_int (base_type_result, DW_AT_decimal_scale,
12211 fpt_info.scale_factor.decimal);
12212 break;
12214 case fixed_point_scale_factor_arbitrary:
12215 /* Arbitrary scale factors cannot be described in standard DWARF,
12216 yet. */
12217 if (!dwarf_strict)
12219 /* Describe the scale factor as a rational constant. */
12220 const dw_die_ref scale_factor
12221 = new_die (DW_TAG_constant, comp_unit_die (), type);
12223 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12224 fpt_info.scale_factor.arbitrary.numerator);
12225 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12226 fpt_info.scale_factor.arbitrary.denominator);
12228 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12230 break;
12232 default:
12233 gcc_unreachable ();
12237 if (type_bias)
12238 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12239 dw_scalar_form_constant
12240 | dw_scalar_form_exprloc
12241 | dw_scalar_form_reference,
12242 NULL);
12244 add_pubtype (type, base_type_result);
12246 return base_type_result;
12249 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12250 named 'auto' in its type: return true for it, false otherwise. */
12252 static inline bool
12253 is_cxx_auto (tree type)
12255 if (is_cxx ())
12257 tree name = TYPE_IDENTIFIER (type);
12258 if (name == get_identifier ("auto")
12259 || name == get_identifier ("decltype(auto)"))
12260 return true;
12262 return false;
12265 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12266 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12268 static inline int
12269 is_base_type (tree type)
12271 switch (TREE_CODE (type))
12273 case ERROR_MARK:
12274 case VOID_TYPE:
12275 case INTEGER_TYPE:
12276 case REAL_TYPE:
12277 case FIXED_POINT_TYPE:
12278 case COMPLEX_TYPE:
12279 case BOOLEAN_TYPE:
12280 case POINTER_BOUNDS_TYPE:
12281 return 1;
12283 case ARRAY_TYPE:
12284 case RECORD_TYPE:
12285 case UNION_TYPE:
12286 case QUAL_UNION_TYPE:
12287 case ENUMERAL_TYPE:
12288 case FUNCTION_TYPE:
12289 case METHOD_TYPE:
12290 case POINTER_TYPE:
12291 case REFERENCE_TYPE:
12292 case NULLPTR_TYPE:
12293 case OFFSET_TYPE:
12294 case LANG_TYPE:
12295 case VECTOR_TYPE:
12296 return 0;
12298 default:
12299 if (is_cxx_auto (type))
12300 return 0;
12301 gcc_unreachable ();
12304 return 0;
12307 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12308 node, return the size in bits for the type if it is a constant, or else
12309 return the alignment for the type if the type's size is not constant, or
12310 else return BITS_PER_WORD if the type actually turns out to be an
12311 ERROR_MARK node. */
12313 static inline unsigned HOST_WIDE_INT
12314 simple_type_size_in_bits (const_tree type)
12316 if (TREE_CODE (type) == ERROR_MARK)
12317 return BITS_PER_WORD;
12318 else if (TYPE_SIZE (type) == NULL_TREE)
12319 return 0;
12320 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12321 return tree_to_uhwi (TYPE_SIZE (type));
12322 else
12323 return TYPE_ALIGN (type);
12326 /* Similarly, but return an offset_int instead of UHWI. */
12328 static inline offset_int
12329 offset_int_type_size_in_bits (const_tree type)
12331 if (TREE_CODE (type) == ERROR_MARK)
12332 return BITS_PER_WORD;
12333 else if (TYPE_SIZE (type) == NULL_TREE)
12334 return 0;
12335 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12336 return wi::to_offset (TYPE_SIZE (type));
12337 else
12338 return TYPE_ALIGN (type);
12341 /* Given a pointer to a tree node for a subrange type, return a pointer
12342 to a DIE that describes the given type. */
12344 static dw_die_ref
12345 subrange_type_die (tree type, tree low, tree high, tree bias,
12346 dw_die_ref context_die)
12348 dw_die_ref subrange_die;
12349 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12351 if (context_die == NULL)
12352 context_die = comp_unit_die ();
12354 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12356 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12358 /* The size of the subrange type and its base type do not match,
12359 so we need to generate a size attribute for the subrange type. */
12360 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12363 add_alignment_attribute (subrange_die, type);
12365 if (low)
12366 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12367 if (high)
12368 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12369 if (bias && !dwarf_strict)
12370 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12371 dw_scalar_form_constant
12372 | dw_scalar_form_exprloc
12373 | dw_scalar_form_reference,
12374 NULL);
12376 return subrange_die;
12379 /* Returns the (const and/or volatile) cv_qualifiers associated with
12380 the decl node. This will normally be augmented with the
12381 cv_qualifiers of the underlying type in add_type_attribute. */
12383 static int
12384 decl_quals (const_tree decl)
12386 return ((TREE_READONLY (decl)
12387 /* The C++ front-end correctly marks reference-typed
12388 variables as readonly, but from a language (and debug
12389 info) standpoint they are not const-qualified. */
12390 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12391 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12392 | (TREE_THIS_VOLATILE (decl)
12393 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12396 /* Determine the TYPE whose qualifiers match the largest strict subset
12397 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12398 qualifiers outside QUAL_MASK. */
12400 static int
12401 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12403 tree t;
12404 int best_rank = 0, best_qual = 0, max_rank;
12406 type_quals &= qual_mask;
12407 max_rank = popcount_hwi (type_quals) - 1;
12409 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12410 t = TYPE_NEXT_VARIANT (t))
12412 int q = TYPE_QUALS (t) & qual_mask;
12414 if ((q & type_quals) == q && q != type_quals
12415 && check_base_type (t, type))
12417 int rank = popcount_hwi (q);
12419 if (rank > best_rank)
12421 best_rank = rank;
12422 best_qual = q;
12427 return best_qual;
12430 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12431 static const dwarf_qual_info_t dwarf_qual_info[] =
12433 { TYPE_QUAL_CONST, DW_TAG_const_type },
12434 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12435 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12436 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12438 static const unsigned int dwarf_qual_info_size
12439 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12441 /* If DIE is a qualified DIE of some base DIE with the same parent,
12442 return the base DIE, otherwise return NULL. Set MASK to the
12443 qualifiers added compared to the returned DIE. */
12445 static dw_die_ref
12446 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12448 unsigned int i;
12449 for (i = 0; i < dwarf_qual_info_size; i++)
12450 if (die->die_tag == dwarf_qual_info[i].t)
12451 break;
12452 if (i == dwarf_qual_info_size)
12453 return NULL;
12454 if (vec_safe_length (die->die_attr) != 1)
12455 return NULL;
12456 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12457 if (type == NULL || type->die_parent != die->die_parent)
12458 return NULL;
12459 *mask |= dwarf_qual_info[i].q;
12460 if (depth)
12462 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12463 if (ret)
12464 return ret;
12466 return type;
12469 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12470 entry that chains the modifiers specified by CV_QUALS in front of the
12471 given type. REVERSE is true if the type is to be interpreted in the
12472 reverse storage order wrt the target order. */
12474 static dw_die_ref
12475 modified_type_die (tree type, int cv_quals, bool reverse,
12476 dw_die_ref context_die)
12478 enum tree_code code = TREE_CODE (type);
12479 dw_die_ref mod_type_die;
12480 dw_die_ref sub_die = NULL;
12481 tree item_type = NULL;
12482 tree qualified_type;
12483 tree name, low, high;
12484 dw_die_ref mod_scope;
12485 /* Only these cv-qualifiers are currently handled. */
12486 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12487 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12489 if (code == ERROR_MARK)
12490 return NULL;
12492 if (lang_hooks.types.get_debug_type)
12494 tree debug_type = lang_hooks.types.get_debug_type (type);
12496 if (debug_type != NULL_TREE && debug_type != type)
12497 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12500 cv_quals &= cv_qual_mask;
12502 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12503 tag modifier (and not an attribute) old consumers won't be able
12504 to handle it. */
12505 if (dwarf_version < 3)
12506 cv_quals &= ~TYPE_QUAL_RESTRICT;
12508 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12509 if (dwarf_version < 5)
12510 cv_quals &= ~TYPE_QUAL_ATOMIC;
12512 /* See if we already have the appropriately qualified variant of
12513 this type. */
12514 qualified_type = get_qualified_type (type, cv_quals);
12516 if (qualified_type == sizetype)
12518 /* Try not to expose the internal sizetype type's name. */
12519 if (TYPE_NAME (qualified_type)
12520 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12522 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12524 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12525 && (TYPE_PRECISION (t)
12526 == TYPE_PRECISION (qualified_type))
12527 && (TYPE_UNSIGNED (t)
12528 == TYPE_UNSIGNED (qualified_type)));
12529 qualified_type = t;
12531 else if (qualified_type == sizetype
12532 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12533 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12534 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12535 qualified_type = size_type_node;
12539 /* If we do, then we can just use its DIE, if it exists. */
12540 if (qualified_type)
12542 mod_type_die = lookup_type_die (qualified_type);
12544 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12545 if (mod_type_die
12546 && (!need_endianity_attribute_p (reverse)
12547 || !is_base_type (type)
12548 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12549 return mod_type_die;
12552 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12554 /* Handle C typedef types. */
12555 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12556 && !DECL_ARTIFICIAL (name))
12558 tree dtype = TREE_TYPE (name);
12560 if (qualified_type == dtype)
12562 tree origin = decl_ultimate_origin (name);
12564 /* Typedef variants that have an abstract origin don't get their own
12565 type DIE (see gen_typedef_die), so fall back on the ultimate
12566 abstract origin instead. */
12567 if (origin != NULL && origin != name)
12568 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12569 context_die);
12571 /* For a named type, use the typedef. */
12572 gen_type_die (qualified_type, context_die);
12573 return lookup_type_die (qualified_type);
12575 else
12577 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12578 dquals &= cv_qual_mask;
12579 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12580 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12581 /* cv-unqualified version of named type. Just use
12582 the unnamed type to which it refers. */
12583 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12584 reverse, context_die);
12585 /* Else cv-qualified version of named type; fall through. */
12589 mod_scope = scope_die_for (type, context_die);
12591 if (cv_quals)
12593 int sub_quals = 0, first_quals = 0;
12594 unsigned i;
12595 dw_die_ref first = NULL, last = NULL;
12597 /* Determine a lesser qualified type that most closely matches
12598 this one. Then generate DW_TAG_* entries for the remaining
12599 qualifiers. */
12600 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12601 cv_qual_mask);
12602 if (sub_quals && use_debug_types)
12604 bool needed = false;
12605 /* If emitting type units, make sure the order of qualifiers
12606 is canonical. Thus, start from unqualified type if
12607 an earlier qualifier is missing in sub_quals, but some later
12608 one is present there. */
12609 for (i = 0; i < dwarf_qual_info_size; i++)
12610 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12611 needed = true;
12612 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12614 sub_quals = 0;
12615 break;
12618 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12619 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12621 /* As not all intermediate qualified DIEs have corresponding
12622 tree types, ensure that qualified DIEs in the same scope
12623 as their DW_AT_type are emitted after their DW_AT_type,
12624 only with other qualified DIEs for the same type possibly
12625 in between them. Determine the range of such qualified
12626 DIEs now (first being the base type, last being corresponding
12627 last qualified DIE for it). */
12628 unsigned int count = 0;
12629 first = qualified_die_p (mod_type_die, &first_quals,
12630 dwarf_qual_info_size);
12631 if (first == NULL)
12632 first = mod_type_die;
12633 gcc_assert ((first_quals & ~sub_quals) == 0);
12634 for (count = 0, last = first;
12635 count < (1U << dwarf_qual_info_size);
12636 count++, last = last->die_sib)
12638 int quals = 0;
12639 if (last == mod_scope->die_child)
12640 break;
12641 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12642 != first)
12643 break;
12647 for (i = 0; i < dwarf_qual_info_size; i++)
12648 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12650 dw_die_ref d;
12651 if (first && first != last)
12653 for (d = first->die_sib; ; d = d->die_sib)
12655 int quals = 0;
12656 qualified_die_p (d, &quals, dwarf_qual_info_size);
12657 if (quals == (first_quals | dwarf_qual_info[i].q))
12658 break;
12659 if (d == last)
12661 d = NULL;
12662 break;
12665 if (d)
12667 mod_type_die = d;
12668 continue;
12671 if (first)
12673 d = ggc_cleared_alloc<die_node> ();
12674 d->die_tag = dwarf_qual_info[i].t;
12675 add_child_die_after (mod_scope, d, last);
12676 last = d;
12678 else
12679 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12680 if (mod_type_die)
12681 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12682 mod_type_die = d;
12683 first_quals |= dwarf_qual_info[i].q;
12686 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12688 dwarf_tag tag = DW_TAG_pointer_type;
12689 if (code == REFERENCE_TYPE)
12691 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12692 tag = DW_TAG_rvalue_reference_type;
12693 else
12694 tag = DW_TAG_reference_type;
12696 mod_type_die = new_die (tag, mod_scope, type);
12698 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12699 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12700 add_alignment_attribute (mod_type_die, type);
12701 item_type = TREE_TYPE (type);
12703 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12704 if (!ADDR_SPACE_GENERIC_P (as))
12706 int action = targetm.addr_space.debug (as);
12707 if (action >= 0)
12709 /* Positive values indicate an address_class. */
12710 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12712 else
12714 /* Negative values indicate an (inverted) segment base reg. */
12715 dw_loc_descr_ref d
12716 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12717 add_AT_loc (mod_type_die, DW_AT_segment, d);
12721 else if (code == INTEGER_TYPE
12722 && TREE_TYPE (type) != NULL_TREE
12723 && subrange_type_for_debug_p (type, &low, &high))
12725 tree bias = NULL_TREE;
12726 if (lang_hooks.types.get_type_bias)
12727 bias = lang_hooks.types.get_type_bias (type);
12728 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12729 item_type = TREE_TYPE (type);
12731 else if (is_base_type (type))
12732 mod_type_die = base_type_die (type, reverse);
12733 else
12735 gen_type_die (type, context_die);
12737 /* We have to get the type_main_variant here (and pass that to the
12738 `lookup_type_die' routine) because the ..._TYPE node we have
12739 might simply be a *copy* of some original type node (where the
12740 copy was created to help us keep track of typedef names) and
12741 that copy might have a different TYPE_UID from the original
12742 ..._TYPE node. */
12743 if (TREE_CODE (type) == FUNCTION_TYPE
12744 || TREE_CODE (type) == METHOD_TYPE)
12746 /* For function/method types, can't just use type_main_variant here,
12747 because that can have different ref-qualifiers for C++,
12748 but try to canonicalize. */
12749 tree main = TYPE_MAIN_VARIANT (type);
12750 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12751 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12752 && check_base_type (t, main)
12753 && check_lang_type (t, type))
12754 return lookup_type_die (t);
12755 return lookup_type_die (type);
12757 else if (TREE_CODE (type) != VECTOR_TYPE
12758 && TREE_CODE (type) != ARRAY_TYPE)
12759 return lookup_type_die (type_main_variant (type));
12760 else
12761 /* Vectors have the debugging information in the type,
12762 not the main variant. */
12763 return lookup_type_die (type);
12766 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12767 don't output a DW_TAG_typedef, since there isn't one in the
12768 user's program; just attach a DW_AT_name to the type.
12769 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12770 if the base type already has the same name. */
12771 if (name
12772 && ((TREE_CODE (name) != TYPE_DECL
12773 && (qualified_type == TYPE_MAIN_VARIANT (type)
12774 || (cv_quals == TYPE_UNQUALIFIED)))
12775 || (TREE_CODE (name) == TYPE_DECL
12776 && TREE_TYPE (name) == qualified_type
12777 && DECL_NAME (name))))
12779 if (TREE_CODE (name) == TYPE_DECL)
12780 /* Could just call add_name_and_src_coords_attributes here,
12781 but since this is a builtin type it doesn't have any
12782 useful source coordinates anyway. */
12783 name = DECL_NAME (name);
12784 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12786 /* This probably indicates a bug. */
12787 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12789 name = TYPE_IDENTIFIER (type);
12790 add_name_attribute (mod_type_die,
12791 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12794 if (qualified_type)
12795 equate_type_number_to_die (qualified_type, mod_type_die);
12797 if (item_type)
12798 /* We must do this after the equate_type_number_to_die call, in case
12799 this is a recursive type. This ensures that the modified_type_die
12800 recursion will terminate even if the type is recursive. Recursive
12801 types are possible in Ada. */
12802 sub_die = modified_type_die (item_type,
12803 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12804 reverse,
12805 context_die);
12807 if (sub_die != NULL)
12808 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12810 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12811 if (TYPE_ARTIFICIAL (type))
12812 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12814 return mod_type_die;
12817 /* Generate DIEs for the generic parameters of T.
12818 T must be either a generic type or a generic function.
12819 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12821 static void
12822 gen_generic_params_dies (tree t)
12824 tree parms, args;
12825 int parms_num, i;
12826 dw_die_ref die = NULL;
12827 int non_default;
12829 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12830 return;
12832 if (TYPE_P (t))
12833 die = lookup_type_die (t);
12834 else if (DECL_P (t))
12835 die = lookup_decl_die (t);
12837 gcc_assert (die);
12839 parms = lang_hooks.get_innermost_generic_parms (t);
12840 if (!parms)
12841 /* T has no generic parameter. It means T is neither a generic type
12842 or function. End of story. */
12843 return;
12845 parms_num = TREE_VEC_LENGTH (parms);
12846 args = lang_hooks.get_innermost_generic_args (t);
12847 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12848 non_default = int_cst_value (TREE_CHAIN (args));
12849 else
12850 non_default = TREE_VEC_LENGTH (args);
12851 for (i = 0; i < parms_num; i++)
12853 tree parm, arg, arg_pack_elems;
12854 dw_die_ref parm_die;
12856 parm = TREE_VEC_ELT (parms, i);
12857 arg = TREE_VEC_ELT (args, i);
12858 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12859 gcc_assert (parm && TREE_VALUE (parm) && arg);
12861 if (parm && TREE_VALUE (parm) && arg)
12863 /* If PARM represents a template parameter pack,
12864 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12865 by DW_TAG_template_*_parameter DIEs for the argument
12866 pack elements of ARG. Note that ARG would then be
12867 an argument pack. */
12868 if (arg_pack_elems)
12869 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12870 arg_pack_elems,
12871 die);
12872 else
12873 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12874 true /* emit name */, die);
12875 if (i >= non_default)
12876 add_AT_flag (parm_die, DW_AT_default_value, 1);
12881 /* Create and return a DIE for PARM which should be
12882 the representation of a generic type parameter.
12883 For instance, in the C++ front end, PARM would be a template parameter.
12884 ARG is the argument to PARM.
12885 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12886 name of the PARM.
12887 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12888 as a child node. */
12890 static dw_die_ref
12891 generic_parameter_die (tree parm, tree arg,
12892 bool emit_name_p,
12893 dw_die_ref parent_die)
12895 dw_die_ref tmpl_die = NULL;
12896 const char *name = NULL;
12898 if (!parm || !DECL_NAME (parm) || !arg)
12899 return NULL;
12901 /* We support non-type generic parameters and arguments,
12902 type generic parameters and arguments, as well as
12903 generic generic parameters (a.k.a. template template parameters in C++)
12904 and arguments. */
12905 if (TREE_CODE (parm) == PARM_DECL)
12906 /* PARM is a nontype generic parameter */
12907 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12908 else if (TREE_CODE (parm) == TYPE_DECL)
12909 /* PARM is a type generic parameter. */
12910 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12911 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12912 /* PARM is a generic generic parameter.
12913 Its DIE is a GNU extension. It shall have a
12914 DW_AT_name attribute to represent the name of the template template
12915 parameter, and a DW_AT_GNU_template_name attribute to represent the
12916 name of the template template argument. */
12917 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12918 parent_die, parm);
12919 else
12920 gcc_unreachable ();
12922 if (tmpl_die)
12924 tree tmpl_type;
12926 /* If PARM is a generic parameter pack, it means we are
12927 emitting debug info for a template argument pack element.
12928 In other terms, ARG is a template argument pack element.
12929 In that case, we don't emit any DW_AT_name attribute for
12930 the die. */
12931 if (emit_name_p)
12933 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12934 gcc_assert (name);
12935 add_AT_string (tmpl_die, DW_AT_name, name);
12938 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12940 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12941 TMPL_DIE should have a child DW_AT_type attribute that is set
12942 to the type of the argument to PARM, which is ARG.
12943 If PARM is a type generic parameter, TMPL_DIE should have a
12944 child DW_AT_type that is set to ARG. */
12945 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12946 add_type_attribute (tmpl_die, tmpl_type,
12947 (TREE_THIS_VOLATILE (tmpl_type)
12948 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12949 false, parent_die);
12951 else
12953 /* So TMPL_DIE is a DIE representing a
12954 a generic generic template parameter, a.k.a template template
12955 parameter in C++ and arg is a template. */
12957 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12958 to the name of the argument. */
12959 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12960 if (name)
12961 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12964 if (TREE_CODE (parm) == PARM_DECL)
12965 /* So PARM is a non-type generic parameter.
12966 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12967 attribute of TMPL_DIE which value represents the value
12968 of ARG.
12969 We must be careful here:
12970 The value of ARG might reference some function decls.
12971 We might currently be emitting debug info for a generic
12972 type and types are emitted before function decls, we don't
12973 know if the function decls referenced by ARG will actually be
12974 emitted after cgraph computations.
12975 So must defer the generation of the DW_AT_const_value to
12976 after cgraph is ready. */
12977 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12980 return tmpl_die;
12983 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12984 PARM_PACK must be a template parameter pack. The returned DIE
12985 will be child DIE of PARENT_DIE. */
12987 static dw_die_ref
12988 template_parameter_pack_die (tree parm_pack,
12989 tree parm_pack_args,
12990 dw_die_ref parent_die)
12992 dw_die_ref die;
12993 int j;
12995 gcc_assert (parent_die && parm_pack);
12997 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12998 add_name_and_src_coords_attributes (die, parm_pack);
12999 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13000 generic_parameter_die (parm_pack,
13001 TREE_VEC_ELT (parm_pack_args, j),
13002 false /* Don't emit DW_AT_name */,
13003 die);
13004 return die;
13007 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13008 an enumerated type. */
13010 static inline int
13011 type_is_enum (const_tree type)
13013 return TREE_CODE (type) == ENUMERAL_TYPE;
13016 /* Return the DBX register number described by a given RTL node. */
13018 static unsigned int
13019 dbx_reg_number (const_rtx rtl)
13021 unsigned regno = REGNO (rtl);
13023 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13025 #ifdef LEAF_REG_REMAP
13026 if (crtl->uses_only_leaf_regs)
13028 int leaf_reg = LEAF_REG_REMAP (regno);
13029 if (leaf_reg != -1)
13030 regno = (unsigned) leaf_reg;
13032 #endif
13034 regno = DBX_REGISTER_NUMBER (regno);
13035 gcc_assert (regno != INVALID_REGNUM);
13036 return regno;
13039 /* Optionally add a DW_OP_piece term to a location description expression.
13040 DW_OP_piece is only added if the location description expression already
13041 doesn't end with DW_OP_piece. */
13043 static void
13044 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13046 dw_loc_descr_ref loc;
13048 if (*list_head != NULL)
13050 /* Find the end of the chain. */
13051 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13054 if (loc->dw_loc_opc != DW_OP_piece)
13055 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13059 /* Return a location descriptor that designates a machine register or
13060 zero if there is none. */
13062 static dw_loc_descr_ref
13063 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13065 rtx regs;
13067 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13068 return 0;
13070 /* We only use "frame base" when we're sure we're talking about the
13071 post-prologue local stack frame. We do this by *not* running
13072 register elimination until this point, and recognizing the special
13073 argument pointer and soft frame pointer rtx's.
13074 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13075 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13076 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13078 dw_loc_descr_ref result = NULL;
13080 if (dwarf_version >= 4 || !dwarf_strict)
13082 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13083 initialized);
13084 if (result)
13085 add_loc_descr (&result,
13086 new_loc_descr (DW_OP_stack_value, 0, 0));
13088 return result;
13091 regs = targetm.dwarf_register_span (rtl);
13093 if (REG_NREGS (rtl) > 1 || regs)
13094 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13095 else
13097 unsigned int dbx_regnum = dbx_reg_number (rtl);
13098 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13099 return 0;
13100 return one_reg_loc_descriptor (dbx_regnum, initialized);
13104 /* Return a location descriptor that designates a machine register for
13105 a given hard register number. */
13107 static dw_loc_descr_ref
13108 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13110 dw_loc_descr_ref reg_loc_descr;
13112 if (regno <= 31)
13113 reg_loc_descr
13114 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13115 else
13116 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13118 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13119 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13121 return reg_loc_descr;
13124 /* Given an RTL of a register, return a location descriptor that
13125 designates a value that spans more than one register. */
13127 static dw_loc_descr_ref
13128 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13129 enum var_init_status initialized)
13131 int size, i;
13132 dw_loc_descr_ref loc_result = NULL;
13134 /* Simple, contiguous registers. */
13135 if (regs == NULL_RTX)
13137 unsigned reg = REGNO (rtl);
13138 int nregs;
13140 #ifdef LEAF_REG_REMAP
13141 if (crtl->uses_only_leaf_regs)
13143 int leaf_reg = LEAF_REG_REMAP (reg);
13144 if (leaf_reg != -1)
13145 reg = (unsigned) leaf_reg;
13147 #endif
13149 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13150 nregs = REG_NREGS (rtl);
13152 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13154 loc_result = NULL;
13155 while (nregs--)
13157 dw_loc_descr_ref t;
13159 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13160 VAR_INIT_STATUS_INITIALIZED);
13161 add_loc_descr (&loc_result, t);
13162 add_loc_descr_op_piece (&loc_result, size);
13163 ++reg;
13165 return loc_result;
13168 /* Now onto stupid register sets in non contiguous locations. */
13170 gcc_assert (GET_CODE (regs) == PARALLEL);
13172 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13173 loc_result = NULL;
13175 for (i = 0; i < XVECLEN (regs, 0); ++i)
13177 dw_loc_descr_ref t;
13179 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13180 VAR_INIT_STATUS_INITIALIZED);
13181 add_loc_descr (&loc_result, t);
13182 add_loc_descr_op_piece (&loc_result, size);
13185 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13186 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13187 return loc_result;
13190 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13192 /* Return a location descriptor that designates a constant i,
13193 as a compound operation from constant (i >> shift), constant shift
13194 and DW_OP_shl. */
13196 static dw_loc_descr_ref
13197 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13199 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13200 add_loc_descr (&ret, int_loc_descriptor (shift));
13201 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13202 return ret;
13205 /* Return a location descriptor that designates a constant. */
13207 static dw_loc_descr_ref
13208 int_loc_descriptor (HOST_WIDE_INT i)
13210 enum dwarf_location_atom op;
13212 /* Pick the smallest representation of a constant, rather than just
13213 defaulting to the LEB encoding. */
13214 if (i >= 0)
13216 int clz = clz_hwi (i);
13217 int ctz = ctz_hwi (i);
13218 if (i <= 31)
13219 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13220 else if (i <= 0xff)
13221 op = DW_OP_const1u;
13222 else if (i <= 0xffff)
13223 op = DW_OP_const2u;
13224 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13225 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13226 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13227 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13228 while DW_OP_const4u is 5 bytes. */
13229 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13230 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13231 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13232 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13233 while DW_OP_const4u is 5 bytes. */
13234 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13236 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13237 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13238 <= 4)
13240 /* As i >= 2**31, the double cast above will yield a negative number.
13241 Since wrapping is defined in DWARF expressions we can output big
13242 positive integers as small negative ones, regardless of the size
13243 of host wide ints.
13245 Here, since the evaluator will handle 32-bit values and since i >=
13246 2**31, we know it's going to be interpreted as a negative literal:
13247 store it this way if we can do better than 5 bytes this way. */
13248 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13250 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13251 op = DW_OP_const4u;
13253 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13254 least 6 bytes: see if we can do better before falling back to it. */
13255 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13256 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13257 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13258 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13259 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13260 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13261 >= HOST_BITS_PER_WIDE_INT)
13262 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13263 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13264 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13265 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13266 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13267 && size_of_uleb128 (i) > 6)
13268 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13269 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13270 else
13271 op = DW_OP_constu;
13273 else
13275 if (i >= -0x80)
13276 op = DW_OP_const1s;
13277 else if (i >= -0x8000)
13278 op = DW_OP_const2s;
13279 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13281 if (size_of_int_loc_descriptor (i) < 5)
13283 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13284 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13285 return ret;
13287 op = DW_OP_const4s;
13289 else
13291 if (size_of_int_loc_descriptor (i)
13292 < (unsigned long) 1 + size_of_sleb128 (i))
13294 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13295 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13296 return ret;
13298 op = DW_OP_consts;
13302 return new_loc_descr (op, i, 0);
13305 /* Likewise, for unsigned constants. */
13307 static dw_loc_descr_ref
13308 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13310 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13311 const unsigned HOST_WIDE_INT max_uint
13312 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13314 /* If possible, use the clever signed constants handling. */
13315 if (i <= max_int)
13316 return int_loc_descriptor ((HOST_WIDE_INT) i);
13318 /* Here, we are left with positive numbers that cannot be represented as
13319 HOST_WIDE_INT, i.e.:
13320 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13322 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13323 whereas may be better to output a negative integer: thanks to integer
13324 wrapping, we know that:
13325 x = x - 2 ** DWARF2_ADDR_SIZE
13326 = x - 2 * (max (HOST_WIDE_INT) + 1)
13327 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13328 small negative integers. Let's try that in cases it will clearly improve
13329 the encoding: there is no gain turning DW_OP_const4u into
13330 DW_OP_const4s. */
13331 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13332 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13333 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13335 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13337 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13338 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13339 const HOST_WIDE_INT second_shift
13340 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13342 /* So we finally have:
13343 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13344 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13345 return int_loc_descriptor (second_shift);
13348 /* Last chance: fallback to a simple constant operation. */
13349 return new_loc_descr
13350 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13351 ? DW_OP_const4u
13352 : DW_OP_const8u,
13353 i, 0);
13356 /* Generate and return a location description that computes the unsigned
13357 comparison of the two stack top entries (a OP b where b is the top-most
13358 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13359 LE_EXPR, GT_EXPR or GE_EXPR. */
13361 static dw_loc_descr_ref
13362 uint_comparison_loc_list (enum tree_code kind)
13364 enum dwarf_location_atom op, flip_op;
13365 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13367 switch (kind)
13369 case LT_EXPR:
13370 op = DW_OP_lt;
13371 break;
13372 case LE_EXPR:
13373 op = DW_OP_le;
13374 break;
13375 case GT_EXPR:
13376 op = DW_OP_gt;
13377 break;
13378 case GE_EXPR:
13379 op = DW_OP_ge;
13380 break;
13381 default:
13382 gcc_unreachable ();
13385 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13386 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13388 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13389 possible to perform unsigned comparisons: we just have to distinguish
13390 three cases:
13392 1. when a and b have the same sign (as signed integers); then we should
13393 return: a OP(signed) b;
13395 2. when a is a negative signed integer while b is a positive one, then a
13396 is a greater unsigned integer than b; likewise when a and b's roles
13397 are flipped.
13399 So first, compare the sign of the two operands. */
13400 ret = new_loc_descr (DW_OP_over, 0, 0);
13401 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13402 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13403 /* If they have different signs (i.e. they have different sign bits), then
13404 the stack top value has now the sign bit set and thus it's smaller than
13405 zero. */
13406 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13407 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13408 add_loc_descr (&ret, bra_node);
13410 /* We are in case 1. At this point, we know both operands have the same
13411 sign, to it's safe to use the built-in signed comparison. */
13412 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13413 add_loc_descr (&ret, jmp_node);
13415 /* We are in case 2. Here, we know both operands do not have the same sign,
13416 so we have to flip the signed comparison. */
13417 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13418 tmp = new_loc_descr (flip_op, 0, 0);
13419 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13420 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13421 add_loc_descr (&ret, tmp);
13423 /* This dummy operation is necessary to make the two branches join. */
13424 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13425 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13426 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13427 add_loc_descr (&ret, tmp);
13429 return ret;
13432 /* Likewise, but takes the location description lists (might be destructive on
13433 them). Return NULL if either is NULL or if concatenation fails. */
13435 static dw_loc_list_ref
13436 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13437 enum tree_code kind)
13439 if (left == NULL || right == NULL)
13440 return NULL;
13442 add_loc_list (&left, right);
13443 if (left == NULL)
13444 return NULL;
13446 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13447 return left;
13450 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13451 without actually allocating it. */
13453 static unsigned long
13454 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13456 return size_of_int_loc_descriptor (i >> shift)
13457 + size_of_int_loc_descriptor (shift)
13458 + 1;
13461 /* Return size_of_locs (int_loc_descriptor (i)) without
13462 actually allocating it. */
13464 static unsigned long
13465 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13467 unsigned long s;
13469 if (i >= 0)
13471 int clz, ctz;
13472 if (i <= 31)
13473 return 1;
13474 else if (i <= 0xff)
13475 return 2;
13476 else if (i <= 0xffff)
13477 return 3;
13478 clz = clz_hwi (i);
13479 ctz = ctz_hwi (i);
13480 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13481 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13482 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13483 - clz - 5);
13484 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13485 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13486 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13487 - clz - 8);
13488 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13489 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13490 <= 4)
13491 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13492 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13493 return 5;
13494 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13495 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13496 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13497 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13498 - clz - 8);
13499 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13500 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13501 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13502 - clz - 16);
13503 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13504 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13505 && s > 6)
13506 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13507 - clz - 32);
13508 else
13509 return 1 + s;
13511 else
13513 if (i >= -0x80)
13514 return 2;
13515 else if (i >= -0x8000)
13516 return 3;
13517 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13519 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13521 s = size_of_int_loc_descriptor (-i) + 1;
13522 if (s < 5)
13523 return s;
13525 return 5;
13527 else
13529 unsigned long r = 1 + size_of_sleb128 (i);
13530 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13532 s = size_of_int_loc_descriptor (-i) + 1;
13533 if (s < r)
13534 return s;
13536 return r;
13541 /* Return loc description representing "address" of integer value.
13542 This can appear only as toplevel expression. */
13544 static dw_loc_descr_ref
13545 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13547 int litsize;
13548 dw_loc_descr_ref loc_result = NULL;
13550 if (!(dwarf_version >= 4 || !dwarf_strict))
13551 return NULL;
13553 litsize = size_of_int_loc_descriptor (i);
13554 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13555 is more compact. For DW_OP_stack_value we need:
13556 litsize + 1 (DW_OP_stack_value)
13557 and for DW_OP_implicit_value:
13558 1 (DW_OP_implicit_value) + 1 (length) + size. */
13559 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13561 loc_result = int_loc_descriptor (i);
13562 add_loc_descr (&loc_result,
13563 new_loc_descr (DW_OP_stack_value, 0, 0));
13564 return loc_result;
13567 loc_result = new_loc_descr (DW_OP_implicit_value,
13568 size, 0);
13569 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13570 loc_result->dw_loc_oprnd2.v.val_int = i;
13571 return loc_result;
13574 /* Return a location descriptor that designates a base+offset location. */
13576 static dw_loc_descr_ref
13577 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13578 enum var_init_status initialized)
13580 unsigned int regno;
13581 dw_loc_descr_ref result;
13582 dw_fde_ref fde = cfun->fde;
13584 /* We only use "frame base" when we're sure we're talking about the
13585 post-prologue local stack frame. We do this by *not* running
13586 register elimination until this point, and recognizing the special
13587 argument pointer and soft frame pointer rtx's. */
13588 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13590 rtx elim = (ira_use_lra_p
13591 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13592 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13594 if (elim != reg)
13596 if (GET_CODE (elim) == PLUS)
13598 offset += INTVAL (XEXP (elim, 1));
13599 elim = XEXP (elim, 0);
13601 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13602 && (elim == hard_frame_pointer_rtx
13603 || elim == stack_pointer_rtx))
13604 || elim == (frame_pointer_needed
13605 ? hard_frame_pointer_rtx
13606 : stack_pointer_rtx));
13608 /* If drap register is used to align stack, use frame
13609 pointer + offset to access stack variables. If stack
13610 is aligned without drap, use stack pointer + offset to
13611 access stack variables. */
13612 if (crtl->stack_realign_tried
13613 && reg == frame_pointer_rtx)
13615 int base_reg
13616 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13617 ? HARD_FRAME_POINTER_REGNUM
13618 : REGNO (elim));
13619 return new_reg_loc_descr (base_reg, offset);
13622 gcc_assert (frame_pointer_fb_offset_valid);
13623 offset += frame_pointer_fb_offset;
13624 return new_loc_descr (DW_OP_fbreg, offset, 0);
13628 regno = REGNO (reg);
13629 #ifdef LEAF_REG_REMAP
13630 if (crtl->uses_only_leaf_regs)
13632 int leaf_reg = LEAF_REG_REMAP (regno);
13633 if (leaf_reg != -1)
13634 regno = (unsigned) leaf_reg;
13636 #endif
13637 regno = DWARF_FRAME_REGNUM (regno);
13639 if (!optimize && fde
13640 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13642 /* Use cfa+offset to represent the location of arguments passed
13643 on the stack when drap is used to align stack.
13644 Only do this when not optimizing, for optimized code var-tracking
13645 is supposed to track where the arguments live and the register
13646 used as vdrap or drap in some spot might be used for something
13647 else in other part of the routine. */
13648 return new_loc_descr (DW_OP_fbreg, offset, 0);
13651 if (regno <= 31)
13652 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13653 offset, 0);
13654 else
13655 result = new_loc_descr (DW_OP_bregx, regno, offset);
13657 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13658 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13660 return result;
13663 /* Return true if this RTL expression describes a base+offset calculation. */
13665 static inline int
13666 is_based_loc (const_rtx rtl)
13668 return (GET_CODE (rtl) == PLUS
13669 && ((REG_P (XEXP (rtl, 0))
13670 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13671 && CONST_INT_P (XEXP (rtl, 1)))));
13674 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13675 failed. */
13677 static dw_loc_descr_ref
13678 tls_mem_loc_descriptor (rtx mem)
13680 tree base;
13681 dw_loc_descr_ref loc_result;
13683 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13684 return NULL;
13686 base = get_base_address (MEM_EXPR (mem));
13687 if (base == NULL
13688 || !VAR_P (base)
13689 || !DECL_THREAD_LOCAL_P (base))
13690 return NULL;
13692 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13693 if (loc_result == NULL)
13694 return NULL;
13696 if (MEM_OFFSET (mem))
13697 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13699 return loc_result;
13702 /* Output debug info about reason why we failed to expand expression as dwarf
13703 expression. */
13705 static void
13706 expansion_failed (tree expr, rtx rtl, char const *reason)
13708 if (dump_file && (dump_flags & TDF_DETAILS))
13710 fprintf (dump_file, "Failed to expand as dwarf: ");
13711 if (expr)
13712 print_generic_expr (dump_file, expr, dump_flags);
13713 if (rtl)
13715 fprintf (dump_file, "\n");
13716 print_rtl (dump_file, rtl);
13718 fprintf (dump_file, "\nReason: %s\n", reason);
13722 /* Helper function for const_ok_for_output. */
13724 static bool
13725 const_ok_for_output_1 (rtx rtl)
13727 if (GET_CODE (rtl) == UNSPEC)
13729 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13730 we can't express it in the debug info. */
13731 /* Don't complain about TLS UNSPECs, those are just too hard to
13732 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13733 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13734 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13735 if (flag_checking
13736 && (XVECLEN (rtl, 0) == 0
13737 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13738 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13739 inform (current_function_decl
13740 ? DECL_SOURCE_LOCATION (current_function_decl)
13741 : UNKNOWN_LOCATION,
13742 #if NUM_UNSPEC_VALUES > 0
13743 "non-delegitimized UNSPEC %s (%d) found in variable location",
13744 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13745 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13746 XINT (rtl, 1));
13747 #else
13748 "non-delegitimized UNSPEC %d found in variable location",
13749 XINT (rtl, 1));
13750 #endif
13751 expansion_failed (NULL_TREE, rtl,
13752 "UNSPEC hasn't been delegitimized.\n");
13753 return false;
13756 if (targetm.const_not_ok_for_debug_p (rtl))
13758 expansion_failed (NULL_TREE, rtl,
13759 "Expression rejected for debug by the backend.\n");
13760 return false;
13763 /* FIXME: Refer to PR60655. It is possible for simplification
13764 of rtl expressions in var tracking to produce such expressions.
13765 We should really identify / validate expressions
13766 enclosed in CONST that can be handled by assemblers on various
13767 targets and only handle legitimate cases here. */
13768 if (GET_CODE (rtl) != SYMBOL_REF)
13770 if (GET_CODE (rtl) == NOT)
13771 return false;
13772 return true;
13775 if (CONSTANT_POOL_ADDRESS_P (rtl))
13777 bool marked;
13778 get_pool_constant_mark (rtl, &marked);
13779 /* If all references to this pool constant were optimized away,
13780 it was not output and thus we can't represent it. */
13781 if (!marked)
13783 expansion_failed (NULL_TREE, rtl,
13784 "Constant was removed from constant pool.\n");
13785 return false;
13789 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13790 return false;
13792 /* Avoid references to external symbols in debug info, on several targets
13793 the linker might even refuse to link when linking a shared library,
13794 and in many other cases the relocations for .debug_info/.debug_loc are
13795 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13796 to be defined within the same shared library or executable are fine. */
13797 if (SYMBOL_REF_EXTERNAL_P (rtl))
13799 tree decl = SYMBOL_REF_DECL (rtl);
13801 if (decl == NULL || !targetm.binds_local_p (decl))
13803 expansion_failed (NULL_TREE, rtl,
13804 "Symbol not defined in current TU.\n");
13805 return false;
13809 return true;
13812 /* Return true if constant RTL can be emitted in DW_OP_addr or
13813 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13814 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13816 static bool
13817 const_ok_for_output (rtx rtl)
13819 if (GET_CODE (rtl) == SYMBOL_REF)
13820 return const_ok_for_output_1 (rtl);
13822 if (GET_CODE (rtl) == CONST)
13824 subrtx_var_iterator::array_type array;
13825 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13826 if (!const_ok_for_output_1 (*iter))
13827 return false;
13828 return true;
13831 return true;
13834 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13835 if possible, NULL otherwise. */
13837 static dw_die_ref
13838 base_type_for_mode (machine_mode mode, bool unsignedp)
13840 dw_die_ref type_die;
13841 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13843 if (type == NULL)
13844 return NULL;
13845 switch (TREE_CODE (type))
13847 case INTEGER_TYPE:
13848 case REAL_TYPE:
13849 break;
13850 default:
13851 return NULL;
13853 type_die = lookup_type_die (type);
13854 if (!type_die)
13855 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13856 comp_unit_die ());
13857 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13858 return NULL;
13859 return type_die;
13862 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13863 type matching MODE, or, if MODE is narrower than or as wide as
13864 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13865 possible. */
13867 static dw_loc_descr_ref
13868 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13870 machine_mode outer_mode = mode;
13871 dw_die_ref type_die;
13872 dw_loc_descr_ref cvt;
13874 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13876 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13877 return op;
13879 type_die = base_type_for_mode (outer_mode, 1);
13880 if (type_die == NULL)
13881 return NULL;
13882 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13883 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13884 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13885 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13886 add_loc_descr (&op, cvt);
13887 return op;
13890 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13892 static dw_loc_descr_ref
13893 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13894 dw_loc_descr_ref op1)
13896 dw_loc_descr_ref ret = op0;
13897 add_loc_descr (&ret, op1);
13898 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13899 if (STORE_FLAG_VALUE != 1)
13901 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13902 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13904 return ret;
13907 /* Subroutine of scompare_loc_descriptor for the case in which we're
13908 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13909 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13911 static dw_loc_descr_ref
13912 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13913 scalar_int_mode op_mode,
13914 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13916 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13917 dw_loc_descr_ref cvt;
13919 if (type_die == NULL)
13920 return NULL;
13921 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13922 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13923 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13924 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13925 add_loc_descr (&op0, cvt);
13926 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13927 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13928 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13929 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13930 add_loc_descr (&op1, cvt);
13931 return compare_loc_descriptor (op, op0, op1);
13934 /* Subroutine of scompare_loc_descriptor for the case in which we're
13935 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13936 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13938 static dw_loc_descr_ref
13939 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13940 scalar_int_mode op_mode,
13941 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13943 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13944 /* For eq/ne, if the operands are known to be zero-extended,
13945 there is no need to do the fancy shifting up. */
13946 if (op == DW_OP_eq || op == DW_OP_ne)
13948 dw_loc_descr_ref last0, last1;
13949 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13951 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13953 /* deref_size zero extends, and for constants we can check
13954 whether they are zero extended or not. */
13955 if (((last0->dw_loc_opc == DW_OP_deref_size
13956 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13957 || (CONST_INT_P (XEXP (rtl, 0))
13958 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13959 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13960 && ((last1->dw_loc_opc == DW_OP_deref_size
13961 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13962 || (CONST_INT_P (XEXP (rtl, 1))
13963 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13964 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13965 return compare_loc_descriptor (op, op0, op1);
13967 /* EQ/NE comparison against constant in narrower type than
13968 DWARF2_ADDR_SIZE can be performed either as
13969 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13970 DW_OP_{eq,ne}
13972 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13973 DW_OP_{eq,ne}. Pick whatever is shorter. */
13974 if (CONST_INT_P (XEXP (rtl, 1))
13975 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13976 && (size_of_int_loc_descriptor (shift) + 1
13977 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13978 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13979 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13980 & GET_MODE_MASK (op_mode))))
13982 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13983 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13984 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13985 & GET_MODE_MASK (op_mode));
13986 return compare_loc_descriptor (op, op0, op1);
13989 add_loc_descr (&op0, int_loc_descriptor (shift));
13990 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13991 if (CONST_INT_P (XEXP (rtl, 1)))
13992 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13993 else
13995 add_loc_descr (&op1, int_loc_descriptor (shift));
13996 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13998 return compare_loc_descriptor (op, op0, op1);
14001 /* Return location descriptor for unsigned comparison OP RTL. */
14003 static dw_loc_descr_ref
14004 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14005 machine_mode mem_mode)
14007 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14008 dw_loc_descr_ref op0, op1;
14010 if (op_mode == VOIDmode)
14011 op_mode = GET_MODE (XEXP (rtl, 1));
14012 if (op_mode == VOIDmode)
14013 return NULL;
14015 scalar_int_mode int_op_mode;
14016 if (dwarf_strict
14017 && dwarf_version < 5
14018 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14019 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14020 return NULL;
14022 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14023 VAR_INIT_STATUS_INITIALIZED);
14024 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14025 VAR_INIT_STATUS_INITIALIZED);
14027 if (op0 == NULL || op1 == NULL)
14028 return NULL;
14030 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14032 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14033 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14035 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14036 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14038 return compare_loc_descriptor (op, op0, op1);
14041 /* Return location descriptor for unsigned comparison OP RTL. */
14043 static dw_loc_descr_ref
14044 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14045 machine_mode mem_mode)
14047 dw_loc_descr_ref op0, op1;
14049 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14050 if (test_op_mode == VOIDmode)
14051 test_op_mode = GET_MODE (XEXP (rtl, 1));
14053 scalar_int_mode op_mode;
14054 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14055 return NULL;
14057 if (dwarf_strict
14058 && dwarf_version < 5
14059 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14060 return NULL;
14062 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14063 VAR_INIT_STATUS_INITIALIZED);
14064 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14065 VAR_INIT_STATUS_INITIALIZED);
14067 if (op0 == NULL || op1 == NULL)
14068 return NULL;
14070 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14072 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14073 dw_loc_descr_ref last0, last1;
14074 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14076 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14078 if (CONST_INT_P (XEXP (rtl, 0)))
14079 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14080 /* deref_size zero extends, so no need to mask it again. */
14081 else if (last0->dw_loc_opc != DW_OP_deref_size
14082 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14084 add_loc_descr (&op0, int_loc_descriptor (mask));
14085 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14087 if (CONST_INT_P (XEXP (rtl, 1)))
14088 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14089 /* deref_size zero extends, so no need to mask it again. */
14090 else if (last1->dw_loc_opc != DW_OP_deref_size
14091 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14093 add_loc_descr (&op1, int_loc_descriptor (mask));
14094 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14097 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14099 HOST_WIDE_INT bias = 1;
14100 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14101 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14102 if (CONST_INT_P (XEXP (rtl, 1)))
14103 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14104 + INTVAL (XEXP (rtl, 1)));
14105 else
14106 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14107 bias, 0));
14109 return compare_loc_descriptor (op, op0, op1);
14112 /* Return location descriptor for {U,S}{MIN,MAX}. */
14114 static dw_loc_descr_ref
14115 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14116 machine_mode mem_mode)
14118 enum dwarf_location_atom op;
14119 dw_loc_descr_ref op0, op1, ret;
14120 dw_loc_descr_ref bra_node, drop_node;
14122 scalar_int_mode int_mode;
14123 if (dwarf_strict
14124 && dwarf_version < 5
14125 && (!is_a <scalar_int_mode> (mode, &int_mode)
14126 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14127 return NULL;
14129 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14130 VAR_INIT_STATUS_INITIALIZED);
14131 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14132 VAR_INIT_STATUS_INITIALIZED);
14134 if (op0 == NULL || op1 == NULL)
14135 return NULL;
14137 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14138 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14139 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14140 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14142 /* Checked by the caller. */
14143 int_mode = as_a <scalar_int_mode> (mode);
14144 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14146 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14147 add_loc_descr (&op0, int_loc_descriptor (mask));
14148 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14149 add_loc_descr (&op1, int_loc_descriptor (mask));
14150 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14152 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14154 HOST_WIDE_INT bias = 1;
14155 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14156 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14157 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14160 else if (is_a <scalar_int_mode> (mode, &int_mode)
14161 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14163 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14164 add_loc_descr (&op0, int_loc_descriptor (shift));
14165 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14166 add_loc_descr (&op1, int_loc_descriptor (shift));
14167 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14169 else if (is_a <scalar_int_mode> (mode, &int_mode)
14170 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14172 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14173 dw_loc_descr_ref cvt;
14174 if (type_die == NULL)
14175 return NULL;
14176 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14177 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14178 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14179 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14180 add_loc_descr (&op0, cvt);
14181 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14182 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14183 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14184 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14185 add_loc_descr (&op1, cvt);
14188 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14189 op = DW_OP_lt;
14190 else
14191 op = DW_OP_gt;
14192 ret = op0;
14193 add_loc_descr (&ret, op1);
14194 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14195 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14196 add_loc_descr (&ret, bra_node);
14197 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14198 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14199 add_loc_descr (&ret, drop_node);
14200 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14201 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14202 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14203 && is_a <scalar_int_mode> (mode, &int_mode)
14204 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14205 ret = convert_descriptor_to_mode (int_mode, ret);
14206 return ret;
14209 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14210 but after converting arguments to type_die, afterwards
14211 convert back to unsigned. */
14213 static dw_loc_descr_ref
14214 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14215 scalar_int_mode mode, machine_mode mem_mode)
14217 dw_loc_descr_ref cvt, op0, op1;
14219 if (type_die == NULL)
14220 return NULL;
14221 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14222 VAR_INIT_STATUS_INITIALIZED);
14223 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14224 VAR_INIT_STATUS_INITIALIZED);
14225 if (op0 == NULL || op1 == NULL)
14226 return NULL;
14227 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14228 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14229 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14230 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14231 add_loc_descr (&op0, cvt);
14232 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14233 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14234 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14235 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14236 add_loc_descr (&op1, cvt);
14237 add_loc_descr (&op0, op1);
14238 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14239 return convert_descriptor_to_mode (mode, op0);
14242 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14243 const0 is DW_OP_lit0 or corresponding typed constant,
14244 const1 is DW_OP_lit1 or corresponding typed constant
14245 and constMSB is constant with just the MSB bit set
14246 for the mode):
14247 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14248 L1: const0 DW_OP_swap
14249 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14250 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14251 L3: DW_OP_drop
14252 L4: DW_OP_nop
14254 CTZ is similar:
14255 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14256 L1: const0 DW_OP_swap
14257 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14258 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14259 L3: DW_OP_drop
14260 L4: DW_OP_nop
14262 FFS is similar:
14263 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14264 L1: const1 DW_OP_swap
14265 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14266 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14267 L3: DW_OP_drop
14268 L4: DW_OP_nop */
14270 static dw_loc_descr_ref
14271 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14272 machine_mode mem_mode)
14274 dw_loc_descr_ref op0, ret, tmp;
14275 HOST_WIDE_INT valv;
14276 dw_loc_descr_ref l1jump, l1label;
14277 dw_loc_descr_ref l2jump, l2label;
14278 dw_loc_descr_ref l3jump, l3label;
14279 dw_loc_descr_ref l4jump, l4label;
14280 rtx msb;
14282 if (GET_MODE (XEXP (rtl, 0)) != mode)
14283 return NULL;
14285 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14286 VAR_INIT_STATUS_INITIALIZED);
14287 if (op0 == NULL)
14288 return NULL;
14289 ret = op0;
14290 if (GET_CODE (rtl) == CLZ)
14292 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14293 valv = GET_MODE_BITSIZE (mode);
14295 else if (GET_CODE (rtl) == FFS)
14296 valv = 0;
14297 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14298 valv = GET_MODE_BITSIZE (mode);
14299 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14300 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14301 add_loc_descr (&ret, l1jump);
14302 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14303 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14304 VAR_INIT_STATUS_INITIALIZED);
14305 if (tmp == NULL)
14306 return NULL;
14307 add_loc_descr (&ret, tmp);
14308 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14309 add_loc_descr (&ret, l4jump);
14310 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14311 ? const1_rtx : const0_rtx,
14312 mode, mem_mode,
14313 VAR_INIT_STATUS_INITIALIZED);
14314 if (l1label == NULL)
14315 return NULL;
14316 add_loc_descr (&ret, l1label);
14317 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14318 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14319 add_loc_descr (&ret, l2label);
14320 if (GET_CODE (rtl) != CLZ)
14321 msb = const1_rtx;
14322 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14323 msb = GEN_INT (HOST_WIDE_INT_1U
14324 << (GET_MODE_BITSIZE (mode) - 1));
14325 else
14326 msb = immed_wide_int_const
14327 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14328 GET_MODE_PRECISION (mode)), mode);
14329 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14330 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14331 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14332 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14333 else
14334 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14335 VAR_INIT_STATUS_INITIALIZED);
14336 if (tmp == NULL)
14337 return NULL;
14338 add_loc_descr (&ret, tmp);
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14340 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14341 add_loc_descr (&ret, l3jump);
14342 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14343 VAR_INIT_STATUS_INITIALIZED);
14344 if (tmp == NULL)
14345 return NULL;
14346 add_loc_descr (&ret, tmp);
14347 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14348 ? DW_OP_shl : DW_OP_shr, 0, 0));
14349 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14350 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14351 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14352 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14353 add_loc_descr (&ret, l2jump);
14354 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14355 add_loc_descr (&ret, l3label);
14356 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14357 add_loc_descr (&ret, l4label);
14358 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14359 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14360 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14361 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14362 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14363 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14364 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14365 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14366 return ret;
14369 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14370 const1 is DW_OP_lit1 or corresponding typed constant):
14371 const0 DW_OP_swap
14372 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14373 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14374 L2: DW_OP_drop
14376 PARITY is similar:
14377 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14378 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14379 L2: DW_OP_drop */
14381 static dw_loc_descr_ref
14382 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14383 machine_mode mem_mode)
14385 dw_loc_descr_ref op0, ret, tmp;
14386 dw_loc_descr_ref l1jump, l1label;
14387 dw_loc_descr_ref l2jump, l2label;
14389 if (GET_MODE (XEXP (rtl, 0)) != mode)
14390 return NULL;
14392 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14393 VAR_INIT_STATUS_INITIALIZED);
14394 if (op0 == NULL)
14395 return NULL;
14396 ret = op0;
14397 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14398 VAR_INIT_STATUS_INITIALIZED);
14399 if (tmp == NULL)
14400 return NULL;
14401 add_loc_descr (&ret, tmp);
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14403 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14404 add_loc_descr (&ret, l1label);
14405 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14406 add_loc_descr (&ret, l2jump);
14407 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14408 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14409 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14410 VAR_INIT_STATUS_INITIALIZED);
14411 if (tmp == NULL)
14412 return NULL;
14413 add_loc_descr (&ret, tmp);
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14415 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14416 ? DW_OP_plus : DW_OP_xor, 0, 0));
14417 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14418 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14419 VAR_INIT_STATUS_INITIALIZED);
14420 add_loc_descr (&ret, tmp);
14421 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14422 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14423 add_loc_descr (&ret, l1jump);
14424 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14425 add_loc_descr (&ret, l2label);
14426 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14427 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14428 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14429 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14430 return ret;
14433 /* BSWAP (constS is initial shift count, either 56 or 24):
14434 constS const0
14435 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14436 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14437 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14438 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14439 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14441 static dw_loc_descr_ref
14442 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14443 machine_mode mem_mode)
14445 dw_loc_descr_ref op0, ret, tmp;
14446 dw_loc_descr_ref l1jump, l1label;
14447 dw_loc_descr_ref l2jump, l2label;
14449 if (BITS_PER_UNIT != 8
14450 || (GET_MODE_BITSIZE (mode) != 32
14451 && GET_MODE_BITSIZE (mode) != 64))
14452 return NULL;
14454 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14455 VAR_INIT_STATUS_INITIALIZED);
14456 if (op0 == NULL)
14457 return NULL;
14459 ret = op0;
14460 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14461 mode, mem_mode,
14462 VAR_INIT_STATUS_INITIALIZED);
14463 if (tmp == NULL)
14464 return NULL;
14465 add_loc_descr (&ret, tmp);
14466 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14467 VAR_INIT_STATUS_INITIALIZED);
14468 if (tmp == NULL)
14469 return NULL;
14470 add_loc_descr (&ret, tmp);
14471 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14472 add_loc_descr (&ret, l1label);
14473 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14474 mode, mem_mode,
14475 VAR_INIT_STATUS_INITIALIZED);
14476 add_loc_descr (&ret, tmp);
14477 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14478 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14479 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14480 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14481 VAR_INIT_STATUS_INITIALIZED);
14482 if (tmp == NULL)
14483 return NULL;
14484 add_loc_descr (&ret, tmp);
14485 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14486 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14487 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14488 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14489 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14491 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14492 VAR_INIT_STATUS_INITIALIZED);
14493 add_loc_descr (&ret, tmp);
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14495 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14496 add_loc_descr (&ret, l2jump);
14497 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14498 VAR_INIT_STATUS_INITIALIZED);
14499 add_loc_descr (&ret, tmp);
14500 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14502 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14503 add_loc_descr (&ret, l1jump);
14504 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14505 add_loc_descr (&ret, l2label);
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14507 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14508 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14509 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14510 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14511 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14512 return ret;
14515 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14516 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14517 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14518 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14520 ROTATERT is similar:
14521 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14522 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14523 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14525 static dw_loc_descr_ref
14526 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14527 machine_mode mem_mode)
14529 rtx rtlop1 = XEXP (rtl, 1);
14530 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14531 int i;
14533 if (GET_MODE (rtlop1) != VOIDmode
14534 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14535 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14536 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14537 VAR_INIT_STATUS_INITIALIZED);
14538 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14539 VAR_INIT_STATUS_INITIALIZED);
14540 if (op0 == NULL || op1 == NULL)
14541 return NULL;
14542 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14543 for (i = 0; i < 2; i++)
14545 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14546 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14547 mode, mem_mode,
14548 VAR_INIT_STATUS_INITIALIZED);
14549 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14550 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14551 ? DW_OP_const4u
14552 : HOST_BITS_PER_WIDE_INT == 64
14553 ? DW_OP_const8u : DW_OP_constu,
14554 GET_MODE_MASK (mode), 0);
14555 else
14556 mask[i] = NULL;
14557 if (mask[i] == NULL)
14558 return NULL;
14559 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14561 ret = op0;
14562 add_loc_descr (&ret, op1);
14563 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14564 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14565 if (GET_CODE (rtl) == ROTATERT)
14567 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14568 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14569 GET_MODE_BITSIZE (mode), 0));
14571 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14572 if (mask[0] != NULL)
14573 add_loc_descr (&ret, mask[0]);
14574 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14575 if (mask[1] != NULL)
14577 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14578 add_loc_descr (&ret, mask[1]);
14579 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14581 if (GET_CODE (rtl) == ROTATE)
14583 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14584 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14585 GET_MODE_BITSIZE (mode), 0));
14587 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14588 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14589 return ret;
14592 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14593 for DEBUG_PARAMETER_REF RTL. */
14595 static dw_loc_descr_ref
14596 parameter_ref_descriptor (rtx rtl)
14598 dw_loc_descr_ref ret;
14599 dw_die_ref ref;
14601 if (dwarf_strict)
14602 return NULL;
14603 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14604 /* With LTO during LTRANS we get the late DIE that refers to the early
14605 DIE, thus we add another indirection here. This seems to confuse
14606 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14607 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14608 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14609 if (ref)
14611 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14612 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14613 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14615 else
14617 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14618 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14620 return ret;
14623 /* The following routine converts the RTL for a variable or parameter
14624 (resident in memory) into an equivalent Dwarf representation of a
14625 mechanism for getting the address of that same variable onto the top of a
14626 hypothetical "address evaluation" stack.
14628 When creating memory location descriptors, we are effectively transforming
14629 the RTL for a memory-resident object into its Dwarf postfix expression
14630 equivalent. This routine recursively descends an RTL tree, turning
14631 it into Dwarf postfix code as it goes.
14633 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14635 MEM_MODE is the mode of the memory reference, needed to handle some
14636 autoincrement addressing modes.
14638 Return 0 if we can't represent the location. */
14640 dw_loc_descr_ref
14641 mem_loc_descriptor (rtx rtl, machine_mode mode,
14642 machine_mode mem_mode,
14643 enum var_init_status initialized)
14645 dw_loc_descr_ref mem_loc_result = NULL;
14646 enum dwarf_location_atom op;
14647 dw_loc_descr_ref op0, op1;
14648 rtx inner = NULL_RTX;
14650 if (mode == VOIDmode)
14651 mode = GET_MODE (rtl);
14653 /* Note that for a dynamically sized array, the location we will generate a
14654 description of here will be the lowest numbered location which is
14655 actually within the array. That's *not* necessarily the same as the
14656 zeroth element of the array. */
14658 rtl = targetm.delegitimize_address (rtl);
14660 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14661 return NULL;
14663 scalar_int_mode int_mode, inner_mode, op1_mode;
14664 switch (GET_CODE (rtl))
14666 case POST_INC:
14667 case POST_DEC:
14668 case POST_MODIFY:
14669 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14671 case SUBREG:
14672 /* The case of a subreg may arise when we have a local (register)
14673 variable or a formal (register) parameter which doesn't quite fill
14674 up an entire register. For now, just assume that it is
14675 legitimate to make the Dwarf info refer to the whole register which
14676 contains the given subreg. */
14677 if (!subreg_lowpart_p (rtl))
14678 break;
14679 inner = SUBREG_REG (rtl);
14680 /* FALLTHRU */
14681 case TRUNCATE:
14682 if (inner == NULL_RTX)
14683 inner = XEXP (rtl, 0);
14684 if (is_a <scalar_int_mode> (mode, &int_mode)
14685 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14686 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14687 #ifdef POINTERS_EXTEND_UNSIGNED
14688 || (int_mode == Pmode && mem_mode != VOIDmode)
14689 #endif
14691 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14693 mem_loc_result = mem_loc_descriptor (inner,
14694 inner_mode,
14695 mem_mode, initialized);
14696 break;
14698 if (dwarf_strict && dwarf_version < 5)
14699 break;
14700 if (is_a <scalar_int_mode> (mode, &int_mode)
14701 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14702 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14703 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14705 dw_die_ref type_die;
14706 dw_loc_descr_ref cvt;
14708 mem_loc_result = mem_loc_descriptor (inner,
14709 GET_MODE (inner),
14710 mem_mode, initialized);
14711 if (mem_loc_result == NULL)
14712 break;
14713 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14714 if (type_die == NULL)
14716 mem_loc_result = NULL;
14717 break;
14719 if (GET_MODE_SIZE (mode)
14720 != GET_MODE_SIZE (GET_MODE (inner)))
14721 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14722 else
14723 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14724 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14725 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14726 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14727 add_loc_descr (&mem_loc_result, cvt);
14728 if (is_a <scalar_int_mode> (mode, &int_mode)
14729 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14731 /* Convert it to untyped afterwards. */
14732 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14733 add_loc_descr (&mem_loc_result, cvt);
14736 break;
14738 case REG:
14739 if (!is_a <scalar_int_mode> (mode, &int_mode)
14740 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14741 && rtl != arg_pointer_rtx
14742 && rtl != frame_pointer_rtx
14743 #ifdef POINTERS_EXTEND_UNSIGNED
14744 && (int_mode != Pmode || mem_mode == VOIDmode)
14745 #endif
14748 dw_die_ref type_die;
14749 unsigned int dbx_regnum;
14751 if (dwarf_strict && dwarf_version < 5)
14752 break;
14753 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14754 break;
14755 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14756 if (type_die == NULL)
14757 break;
14759 dbx_regnum = dbx_reg_number (rtl);
14760 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14761 break;
14762 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14763 dbx_regnum, 0);
14764 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14765 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14766 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14767 break;
14769 /* Whenever a register number forms a part of the description of the
14770 method for calculating the (dynamic) address of a memory resident
14771 object, DWARF rules require the register number be referred to as
14772 a "base register". This distinction is not based in any way upon
14773 what category of register the hardware believes the given register
14774 belongs to. This is strictly DWARF terminology we're dealing with
14775 here. Note that in cases where the location of a memory-resident
14776 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14777 OP_CONST (0)) the actual DWARF location descriptor that we generate
14778 may just be OP_BASEREG (basereg). This may look deceptively like
14779 the object in question was allocated to a register (rather than in
14780 memory) so DWARF consumers need to be aware of the subtle
14781 distinction between OP_REG and OP_BASEREG. */
14782 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14783 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14784 else if (stack_realign_drap
14785 && crtl->drap_reg
14786 && crtl->args.internal_arg_pointer == rtl
14787 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14789 /* If RTL is internal_arg_pointer, which has been optimized
14790 out, use DRAP instead. */
14791 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14792 VAR_INIT_STATUS_INITIALIZED);
14794 break;
14796 case SIGN_EXTEND:
14797 case ZERO_EXTEND:
14798 if (!is_a <scalar_int_mode> (mode, &int_mode)
14799 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14800 break;
14801 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14802 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14803 if (op0 == 0)
14804 break;
14805 else if (GET_CODE (rtl) == ZERO_EXTEND
14806 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14807 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14808 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14809 to expand zero extend as two shifts instead of
14810 masking. */
14811 && GET_MODE_SIZE (inner_mode) <= 4)
14813 mem_loc_result = op0;
14814 add_loc_descr (&mem_loc_result,
14815 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14816 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14818 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14820 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14821 shift *= BITS_PER_UNIT;
14822 if (GET_CODE (rtl) == SIGN_EXTEND)
14823 op = DW_OP_shra;
14824 else
14825 op = DW_OP_shr;
14826 mem_loc_result = op0;
14827 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14828 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14829 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14830 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14832 else if (!dwarf_strict || dwarf_version >= 5)
14834 dw_die_ref type_die1, type_die2;
14835 dw_loc_descr_ref cvt;
14837 type_die1 = base_type_for_mode (inner_mode,
14838 GET_CODE (rtl) == ZERO_EXTEND);
14839 if (type_die1 == NULL)
14840 break;
14841 type_die2 = base_type_for_mode (int_mode, 1);
14842 if (type_die2 == NULL)
14843 break;
14844 mem_loc_result = op0;
14845 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14846 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14847 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14848 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14849 add_loc_descr (&mem_loc_result, cvt);
14850 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14851 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14852 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14853 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14854 add_loc_descr (&mem_loc_result, cvt);
14856 break;
14858 case MEM:
14860 rtx new_rtl = avoid_constant_pool_reference (rtl);
14861 if (new_rtl != rtl)
14863 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14864 initialized);
14865 if (mem_loc_result != NULL)
14866 return mem_loc_result;
14869 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14870 get_address_mode (rtl), mode,
14871 VAR_INIT_STATUS_INITIALIZED);
14872 if (mem_loc_result == NULL)
14873 mem_loc_result = tls_mem_loc_descriptor (rtl);
14874 if (mem_loc_result != NULL)
14876 if (!is_a <scalar_int_mode> (mode, &int_mode)
14877 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14879 dw_die_ref type_die;
14880 dw_loc_descr_ref deref;
14882 if (dwarf_strict && dwarf_version < 5)
14883 return NULL;
14884 type_die
14885 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14886 if (type_die == NULL)
14887 return NULL;
14888 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14889 GET_MODE_SIZE (mode), 0);
14890 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14891 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14892 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14893 add_loc_descr (&mem_loc_result, deref);
14895 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14896 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14897 else
14898 add_loc_descr (&mem_loc_result,
14899 new_loc_descr (DW_OP_deref_size,
14900 GET_MODE_SIZE (int_mode), 0));
14902 break;
14904 case LO_SUM:
14905 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14907 case LABEL_REF:
14908 /* Some ports can transform a symbol ref into a label ref, because
14909 the symbol ref is too far away and has to be dumped into a constant
14910 pool. */
14911 case CONST:
14912 case SYMBOL_REF:
14913 if (!is_a <scalar_int_mode> (mode, &int_mode)
14914 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14915 #ifdef POINTERS_EXTEND_UNSIGNED
14916 && (int_mode != Pmode || mem_mode == VOIDmode)
14917 #endif
14919 break;
14920 if (GET_CODE (rtl) == SYMBOL_REF
14921 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14923 dw_loc_descr_ref temp;
14925 /* If this is not defined, we have no way to emit the data. */
14926 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14927 break;
14929 temp = new_addr_loc_descr (rtl, dtprel_true);
14931 /* We check for DWARF 5 here because gdb did not implement
14932 DW_OP_form_tls_address until after 7.12. */
14933 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14934 ? DW_OP_form_tls_address
14935 : DW_OP_GNU_push_tls_address),
14936 0, 0);
14937 add_loc_descr (&mem_loc_result, temp);
14939 break;
14942 if (!const_ok_for_output (rtl))
14944 if (GET_CODE (rtl) == CONST)
14945 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14946 mem_mode, initialized);
14947 break;
14950 symref:
14951 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14952 vec_safe_push (used_rtx_array, rtl);
14953 break;
14955 case CONCAT:
14956 case CONCATN:
14957 case VAR_LOCATION:
14958 case DEBUG_IMPLICIT_PTR:
14959 expansion_failed (NULL_TREE, rtl,
14960 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14961 return 0;
14963 case ENTRY_VALUE:
14964 if (dwarf_strict && dwarf_version < 5)
14965 return NULL;
14966 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14968 if (!is_a <scalar_int_mode> (mode, &int_mode)
14969 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14970 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14971 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14972 else
14974 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14975 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14976 return NULL;
14977 op0 = one_reg_loc_descriptor (dbx_regnum,
14978 VAR_INIT_STATUS_INITIALIZED);
14981 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14982 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14984 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14985 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14986 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14987 return NULL;
14989 else
14990 gcc_unreachable ();
14991 if (op0 == NULL)
14992 return NULL;
14993 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14994 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14995 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14996 break;
14998 case DEBUG_PARAMETER_REF:
14999 mem_loc_result = parameter_ref_descriptor (rtl);
15000 break;
15002 case PRE_MODIFY:
15003 /* Extract the PLUS expression nested inside and fall into
15004 PLUS code below. */
15005 rtl = XEXP (rtl, 1);
15006 goto plus;
15008 case PRE_INC:
15009 case PRE_DEC:
15010 /* Turn these into a PLUS expression and fall into the PLUS code
15011 below. */
15012 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15013 gen_int_mode (GET_CODE (rtl) == PRE_INC
15014 ? GET_MODE_UNIT_SIZE (mem_mode)
15015 : -GET_MODE_UNIT_SIZE (mem_mode),
15016 mode));
15018 /* fall through */
15020 case PLUS:
15021 plus:
15022 if (is_based_loc (rtl)
15023 && is_a <scalar_int_mode> (mode, &int_mode)
15024 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15025 || XEXP (rtl, 0) == arg_pointer_rtx
15026 || XEXP (rtl, 0) == frame_pointer_rtx))
15027 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15028 INTVAL (XEXP (rtl, 1)),
15029 VAR_INIT_STATUS_INITIALIZED);
15030 else
15032 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15033 VAR_INIT_STATUS_INITIALIZED);
15034 if (mem_loc_result == 0)
15035 break;
15037 if (CONST_INT_P (XEXP (rtl, 1))
15038 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15039 <= DWARF2_ADDR_SIZE))
15040 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15041 else
15043 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15044 VAR_INIT_STATUS_INITIALIZED);
15045 if (op1 == 0)
15046 return NULL;
15047 add_loc_descr (&mem_loc_result, op1);
15048 add_loc_descr (&mem_loc_result,
15049 new_loc_descr (DW_OP_plus, 0, 0));
15052 break;
15054 /* If a pseudo-reg is optimized away, it is possible for it to
15055 be replaced with a MEM containing a multiply or shift. */
15056 case MINUS:
15057 op = DW_OP_minus;
15058 goto do_binop;
15060 case MULT:
15061 op = DW_OP_mul;
15062 goto do_binop;
15064 case DIV:
15065 if ((!dwarf_strict || dwarf_version >= 5)
15066 && is_a <scalar_int_mode> (mode, &int_mode)
15067 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15069 mem_loc_result = typed_binop (DW_OP_div, rtl,
15070 base_type_for_mode (mode, 0),
15071 int_mode, mem_mode);
15072 break;
15074 op = DW_OP_div;
15075 goto do_binop;
15077 case UMOD:
15078 op = DW_OP_mod;
15079 goto do_binop;
15081 case ASHIFT:
15082 op = DW_OP_shl;
15083 goto do_shift;
15085 case ASHIFTRT:
15086 op = DW_OP_shra;
15087 goto do_shift;
15089 case LSHIFTRT:
15090 op = DW_OP_shr;
15091 goto do_shift;
15093 do_shift:
15094 if (!is_a <scalar_int_mode> (mode, &int_mode))
15095 break;
15096 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15097 VAR_INIT_STATUS_INITIALIZED);
15099 rtx rtlop1 = XEXP (rtl, 1);
15100 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15101 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15102 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15103 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15104 VAR_INIT_STATUS_INITIALIZED);
15107 if (op0 == 0 || op1 == 0)
15108 break;
15110 mem_loc_result = op0;
15111 add_loc_descr (&mem_loc_result, op1);
15112 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15113 break;
15115 case AND:
15116 op = DW_OP_and;
15117 goto do_binop;
15119 case IOR:
15120 op = DW_OP_or;
15121 goto do_binop;
15123 case XOR:
15124 op = DW_OP_xor;
15125 goto do_binop;
15127 do_binop:
15128 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15129 VAR_INIT_STATUS_INITIALIZED);
15130 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15131 VAR_INIT_STATUS_INITIALIZED);
15133 if (op0 == 0 || op1 == 0)
15134 break;
15136 mem_loc_result = op0;
15137 add_loc_descr (&mem_loc_result, op1);
15138 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15139 break;
15141 case MOD:
15142 if ((!dwarf_strict || dwarf_version >= 5)
15143 && is_a <scalar_int_mode> (mode, &int_mode)
15144 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15146 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15147 base_type_for_mode (mode, 0),
15148 int_mode, mem_mode);
15149 break;
15152 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15153 VAR_INIT_STATUS_INITIALIZED);
15154 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15155 VAR_INIT_STATUS_INITIALIZED);
15157 if (op0 == 0 || op1 == 0)
15158 break;
15160 mem_loc_result = op0;
15161 add_loc_descr (&mem_loc_result, op1);
15162 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15163 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15164 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15165 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15166 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15167 break;
15169 case UDIV:
15170 if ((!dwarf_strict || dwarf_version >= 5)
15171 && is_a <scalar_int_mode> (mode, &int_mode))
15173 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15175 op = DW_OP_div;
15176 goto do_binop;
15178 mem_loc_result = typed_binop (DW_OP_div, rtl,
15179 base_type_for_mode (int_mode, 1),
15180 int_mode, mem_mode);
15182 break;
15184 case NOT:
15185 op = DW_OP_not;
15186 goto do_unop;
15188 case ABS:
15189 op = DW_OP_abs;
15190 goto do_unop;
15192 case NEG:
15193 op = DW_OP_neg;
15194 goto do_unop;
15196 do_unop:
15197 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15198 VAR_INIT_STATUS_INITIALIZED);
15200 if (op0 == 0)
15201 break;
15203 mem_loc_result = op0;
15204 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15205 break;
15207 case CONST_INT:
15208 if (!is_a <scalar_int_mode> (mode, &int_mode)
15209 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15210 #ifdef POINTERS_EXTEND_UNSIGNED
15211 || (int_mode == Pmode
15212 && mem_mode != VOIDmode
15213 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15214 #endif
15217 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15218 break;
15220 if ((!dwarf_strict || dwarf_version >= 5)
15221 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15222 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15224 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15225 scalar_int_mode amode;
15226 if (type_die == NULL)
15227 return NULL;
15228 if (INTVAL (rtl) >= 0
15229 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15230 .exists (&amode))
15231 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15232 /* const DW_OP_convert <XXX> vs.
15233 DW_OP_const_type <XXX, 1, const>. */
15234 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15235 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15237 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15238 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15239 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15240 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15241 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15242 add_loc_descr (&mem_loc_result, op0);
15243 return mem_loc_result;
15245 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15246 INTVAL (rtl));
15247 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15248 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15249 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15250 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15251 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15252 else
15254 mem_loc_result->dw_loc_oprnd2.val_class
15255 = dw_val_class_const_double;
15256 mem_loc_result->dw_loc_oprnd2.v.val_double
15257 = double_int::from_shwi (INTVAL (rtl));
15260 break;
15262 case CONST_DOUBLE:
15263 if (!dwarf_strict || dwarf_version >= 5)
15265 dw_die_ref type_die;
15267 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15268 CONST_DOUBLE rtx could represent either a large integer
15269 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15270 the value is always a floating point constant.
15272 When it is an integer, a CONST_DOUBLE is used whenever
15273 the constant requires 2 HWIs to be adequately represented.
15274 We output CONST_DOUBLEs as blocks. */
15275 if (mode == VOIDmode
15276 || (GET_MODE (rtl) == VOIDmode
15277 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15278 break;
15279 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15280 if (type_die == NULL)
15281 return NULL;
15282 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15283 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15284 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15285 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15286 #if TARGET_SUPPORTS_WIDE_INT == 0
15287 if (!SCALAR_FLOAT_MODE_P (mode))
15289 mem_loc_result->dw_loc_oprnd2.val_class
15290 = dw_val_class_const_double;
15291 mem_loc_result->dw_loc_oprnd2.v.val_double
15292 = rtx_to_double_int (rtl);
15294 else
15295 #endif
15297 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15298 unsigned int length = GET_MODE_SIZE (float_mode);
15299 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15301 insert_float (rtl, array);
15302 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15303 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15304 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15305 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15308 break;
15310 case CONST_WIDE_INT:
15311 if (!dwarf_strict || dwarf_version >= 5)
15313 dw_die_ref type_die;
15315 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15316 if (type_die == NULL)
15317 return NULL;
15318 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15319 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15320 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15321 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15322 mem_loc_result->dw_loc_oprnd2.val_class
15323 = dw_val_class_wide_int;
15324 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15325 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15327 break;
15329 case EQ:
15330 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15331 break;
15333 case GE:
15334 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15335 break;
15337 case GT:
15338 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15339 break;
15341 case LE:
15342 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15343 break;
15345 case LT:
15346 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15347 break;
15349 case NE:
15350 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15351 break;
15353 case GEU:
15354 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15355 break;
15357 case GTU:
15358 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15359 break;
15361 case LEU:
15362 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15363 break;
15365 case LTU:
15366 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15367 break;
15369 case UMIN:
15370 case UMAX:
15371 if (!SCALAR_INT_MODE_P (mode))
15372 break;
15373 /* FALLTHRU */
15374 case SMIN:
15375 case SMAX:
15376 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15377 break;
15379 case ZERO_EXTRACT:
15380 case SIGN_EXTRACT:
15381 if (CONST_INT_P (XEXP (rtl, 1))
15382 && CONST_INT_P (XEXP (rtl, 2))
15383 && is_a <scalar_int_mode> (mode, &int_mode)
15384 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15385 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15386 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15387 && ((unsigned) INTVAL (XEXP (rtl, 1))
15388 + (unsigned) INTVAL (XEXP (rtl, 2))
15389 <= GET_MODE_BITSIZE (int_mode)))
15391 int shift, size;
15392 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15393 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15394 if (op0 == 0)
15395 break;
15396 if (GET_CODE (rtl) == SIGN_EXTRACT)
15397 op = DW_OP_shra;
15398 else
15399 op = DW_OP_shr;
15400 mem_loc_result = op0;
15401 size = INTVAL (XEXP (rtl, 1));
15402 shift = INTVAL (XEXP (rtl, 2));
15403 if (BITS_BIG_ENDIAN)
15404 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15405 if (shift + size != (int) DWARF2_ADDR_SIZE)
15407 add_loc_descr (&mem_loc_result,
15408 int_loc_descriptor (DWARF2_ADDR_SIZE
15409 - shift - size));
15410 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15412 if (size != (int) DWARF2_ADDR_SIZE)
15414 add_loc_descr (&mem_loc_result,
15415 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15416 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15419 break;
15421 case IF_THEN_ELSE:
15423 dw_loc_descr_ref op2, bra_node, drop_node;
15424 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15425 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15426 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15427 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15428 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15429 VAR_INIT_STATUS_INITIALIZED);
15430 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15431 VAR_INIT_STATUS_INITIALIZED);
15432 if (op0 == NULL || op1 == NULL || op2 == NULL)
15433 break;
15435 mem_loc_result = op1;
15436 add_loc_descr (&mem_loc_result, op2);
15437 add_loc_descr (&mem_loc_result, op0);
15438 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15439 add_loc_descr (&mem_loc_result, bra_node);
15440 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15441 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15442 add_loc_descr (&mem_loc_result, drop_node);
15443 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15444 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15446 break;
15448 case FLOAT_EXTEND:
15449 case FLOAT_TRUNCATE:
15450 case FLOAT:
15451 case UNSIGNED_FLOAT:
15452 case FIX:
15453 case UNSIGNED_FIX:
15454 if (!dwarf_strict || dwarf_version >= 5)
15456 dw_die_ref type_die;
15457 dw_loc_descr_ref cvt;
15459 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15460 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15461 if (op0 == NULL)
15462 break;
15463 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15464 && (GET_CODE (rtl) == FLOAT
15465 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15467 type_die = base_type_for_mode (int_mode,
15468 GET_CODE (rtl) == UNSIGNED_FLOAT);
15469 if (type_die == NULL)
15470 break;
15471 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15472 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15473 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15474 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15475 add_loc_descr (&op0, cvt);
15477 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15478 if (type_die == NULL)
15479 break;
15480 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15481 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15482 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15483 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15484 add_loc_descr (&op0, cvt);
15485 if (is_a <scalar_int_mode> (mode, &int_mode)
15486 && (GET_CODE (rtl) == FIX
15487 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15489 op0 = convert_descriptor_to_mode (int_mode, op0);
15490 if (op0 == NULL)
15491 break;
15493 mem_loc_result = op0;
15495 break;
15497 case CLZ:
15498 case CTZ:
15499 case FFS:
15500 if (is_a <scalar_int_mode> (mode, &int_mode))
15501 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15502 break;
15504 case POPCOUNT:
15505 case PARITY:
15506 if (is_a <scalar_int_mode> (mode, &int_mode))
15507 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15508 break;
15510 case BSWAP:
15511 if (is_a <scalar_int_mode> (mode, &int_mode))
15512 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15513 break;
15515 case ROTATE:
15516 case ROTATERT:
15517 if (is_a <scalar_int_mode> (mode, &int_mode))
15518 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15519 break;
15521 case COMPARE:
15522 /* In theory, we could implement the above. */
15523 /* DWARF cannot represent the unsigned compare operations
15524 natively. */
15525 case SS_MULT:
15526 case US_MULT:
15527 case SS_DIV:
15528 case US_DIV:
15529 case SS_PLUS:
15530 case US_PLUS:
15531 case SS_MINUS:
15532 case US_MINUS:
15533 case SS_NEG:
15534 case US_NEG:
15535 case SS_ABS:
15536 case SS_ASHIFT:
15537 case US_ASHIFT:
15538 case SS_TRUNCATE:
15539 case US_TRUNCATE:
15540 case UNORDERED:
15541 case ORDERED:
15542 case UNEQ:
15543 case UNGE:
15544 case UNGT:
15545 case UNLE:
15546 case UNLT:
15547 case LTGT:
15548 case FRACT_CONVERT:
15549 case UNSIGNED_FRACT_CONVERT:
15550 case SAT_FRACT:
15551 case UNSIGNED_SAT_FRACT:
15552 case SQRT:
15553 case ASM_OPERANDS:
15554 case VEC_MERGE:
15555 case VEC_SELECT:
15556 case VEC_CONCAT:
15557 case VEC_DUPLICATE:
15558 case UNSPEC:
15559 case HIGH:
15560 case FMA:
15561 case STRICT_LOW_PART:
15562 case CONST_VECTOR:
15563 case CONST_FIXED:
15564 case CLRSB:
15565 case CLOBBER:
15566 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15567 can't express it in the debug info. This can happen e.g. with some
15568 TLS UNSPECs. */
15569 break;
15571 case CONST_STRING:
15572 resolve_one_addr (&rtl);
15573 goto symref;
15575 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15576 the expression. An UNSPEC rtx represents a raw DWARF operation,
15577 new_loc_descr is called for it to build the operation directly.
15578 Otherwise mem_loc_descriptor is called recursively. */
15579 case PARALLEL:
15581 int index = 0;
15582 dw_loc_descr_ref exp_result = NULL;
15584 for (; index < XVECLEN (rtl, 0); index++)
15586 rtx elem = XVECEXP (rtl, 0, index);
15587 if (GET_CODE (elem) == UNSPEC)
15589 /* Each DWARF operation UNSPEC contain two operands, if
15590 one operand is not used for the operation, const0_rtx is
15591 passed. */
15592 gcc_assert (XVECLEN (elem, 0) == 2);
15594 HOST_WIDE_INT dw_op = XINT (elem, 1);
15595 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15596 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15597 exp_result
15598 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15599 oprnd2);
15601 else
15602 exp_result
15603 = mem_loc_descriptor (elem, mode, mem_mode,
15604 VAR_INIT_STATUS_INITIALIZED);
15606 if (!mem_loc_result)
15607 mem_loc_result = exp_result;
15608 else
15609 add_loc_descr (&mem_loc_result, exp_result);
15612 break;
15615 default:
15616 if (flag_checking)
15618 print_rtl (stderr, rtl);
15619 gcc_unreachable ();
15621 break;
15624 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15625 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15627 return mem_loc_result;
15630 /* Return a descriptor that describes the concatenation of two locations.
15631 This is typically a complex variable. */
15633 static dw_loc_descr_ref
15634 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15636 dw_loc_descr_ref cc_loc_result = NULL;
15637 dw_loc_descr_ref x0_ref
15638 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15639 dw_loc_descr_ref x1_ref
15640 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15642 if (x0_ref == 0 || x1_ref == 0)
15643 return 0;
15645 cc_loc_result = x0_ref;
15646 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15648 add_loc_descr (&cc_loc_result, x1_ref);
15649 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15651 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15652 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15654 return cc_loc_result;
15657 /* Return a descriptor that describes the concatenation of N
15658 locations. */
15660 static dw_loc_descr_ref
15661 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15663 unsigned int i;
15664 dw_loc_descr_ref cc_loc_result = NULL;
15665 unsigned int n = XVECLEN (concatn, 0);
15667 for (i = 0; i < n; ++i)
15669 dw_loc_descr_ref ref;
15670 rtx x = XVECEXP (concatn, 0, i);
15672 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15673 if (ref == NULL)
15674 return NULL;
15676 add_loc_descr (&cc_loc_result, ref);
15677 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15680 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15681 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15683 return cc_loc_result;
15686 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15687 for DEBUG_IMPLICIT_PTR RTL. */
15689 static dw_loc_descr_ref
15690 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15692 dw_loc_descr_ref ret;
15693 dw_die_ref ref;
15695 if (dwarf_strict && dwarf_version < 5)
15696 return NULL;
15697 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15698 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15699 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15700 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15701 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15702 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15703 if (ref)
15705 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15706 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15707 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15709 else
15711 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15712 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15714 return ret;
15717 /* Output a proper Dwarf location descriptor for a variable or parameter
15718 which is either allocated in a register or in a memory location. For a
15719 register, we just generate an OP_REG and the register number. For a
15720 memory location we provide a Dwarf postfix expression describing how to
15721 generate the (dynamic) address of the object onto the address stack.
15723 MODE is mode of the decl if this loc_descriptor is going to be used in
15724 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15725 allowed, VOIDmode otherwise.
15727 If we don't know how to describe it, return 0. */
15729 static dw_loc_descr_ref
15730 loc_descriptor (rtx rtl, machine_mode mode,
15731 enum var_init_status initialized)
15733 dw_loc_descr_ref loc_result = NULL;
15734 scalar_int_mode int_mode;
15736 switch (GET_CODE (rtl))
15738 case SUBREG:
15739 /* The case of a subreg may arise when we have a local (register)
15740 variable or a formal (register) parameter which doesn't quite fill
15741 up an entire register. For now, just assume that it is
15742 legitimate to make the Dwarf info refer to the whole register which
15743 contains the given subreg. */
15744 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15745 loc_result = loc_descriptor (SUBREG_REG (rtl),
15746 GET_MODE (SUBREG_REG (rtl)), initialized);
15747 else
15748 goto do_default;
15749 break;
15751 case REG:
15752 loc_result = reg_loc_descriptor (rtl, initialized);
15753 break;
15755 case MEM:
15756 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15757 GET_MODE (rtl), initialized);
15758 if (loc_result == NULL)
15759 loc_result = tls_mem_loc_descriptor (rtl);
15760 if (loc_result == NULL)
15762 rtx new_rtl = avoid_constant_pool_reference (rtl);
15763 if (new_rtl != rtl)
15764 loc_result = loc_descriptor (new_rtl, mode, initialized);
15766 break;
15768 case CONCAT:
15769 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15770 initialized);
15771 break;
15773 case CONCATN:
15774 loc_result = concatn_loc_descriptor (rtl, initialized);
15775 break;
15777 case VAR_LOCATION:
15778 /* Single part. */
15779 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15781 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15782 if (GET_CODE (loc) == EXPR_LIST)
15783 loc = XEXP (loc, 0);
15784 loc_result = loc_descriptor (loc, mode, initialized);
15785 break;
15788 rtl = XEXP (rtl, 1);
15789 /* FALLTHRU */
15791 case PARALLEL:
15793 rtvec par_elems = XVEC (rtl, 0);
15794 int num_elem = GET_NUM_ELEM (par_elems);
15795 machine_mode mode;
15796 int i;
15798 /* Create the first one, so we have something to add to. */
15799 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15800 VOIDmode, initialized);
15801 if (loc_result == NULL)
15802 return NULL;
15803 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15804 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15805 for (i = 1; i < num_elem; i++)
15807 dw_loc_descr_ref temp;
15809 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15810 VOIDmode, initialized);
15811 if (temp == NULL)
15812 return NULL;
15813 add_loc_descr (&loc_result, temp);
15814 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15815 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15818 break;
15820 case CONST_INT:
15821 if (mode != VOIDmode && mode != BLKmode)
15823 int_mode = as_a <scalar_int_mode> (mode);
15824 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15825 INTVAL (rtl));
15827 break;
15829 case CONST_DOUBLE:
15830 if (mode == VOIDmode)
15831 mode = GET_MODE (rtl);
15833 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15835 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15837 /* Note that a CONST_DOUBLE rtx could represent either an integer
15838 or a floating-point constant. A CONST_DOUBLE is used whenever
15839 the constant requires more than one word in order to be
15840 adequately represented. We output CONST_DOUBLEs as blocks. */
15841 scalar_mode smode = as_a <scalar_mode> (mode);
15842 loc_result = new_loc_descr (DW_OP_implicit_value,
15843 GET_MODE_SIZE (smode), 0);
15844 #if TARGET_SUPPORTS_WIDE_INT == 0
15845 if (!SCALAR_FLOAT_MODE_P (smode))
15847 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15848 loc_result->dw_loc_oprnd2.v.val_double
15849 = rtx_to_double_int (rtl);
15851 else
15852 #endif
15854 unsigned int length = GET_MODE_SIZE (smode);
15855 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15857 insert_float (rtl, array);
15858 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15859 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15860 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15861 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15864 break;
15866 case CONST_WIDE_INT:
15867 if (mode == VOIDmode)
15868 mode = GET_MODE (rtl);
15870 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15872 int_mode = as_a <scalar_int_mode> (mode);
15873 loc_result = new_loc_descr (DW_OP_implicit_value,
15874 GET_MODE_SIZE (int_mode), 0);
15875 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15876 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15877 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15879 break;
15881 case CONST_VECTOR:
15882 if (mode == VOIDmode)
15883 mode = GET_MODE (rtl);
15885 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15887 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15888 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15889 unsigned char *array
15890 = ggc_vec_alloc<unsigned char> (length * elt_size);
15891 unsigned int i;
15892 unsigned char *p;
15893 machine_mode imode = GET_MODE_INNER (mode);
15895 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15896 switch (GET_MODE_CLASS (mode))
15898 case MODE_VECTOR_INT:
15899 for (i = 0, p = array; i < length; i++, p += elt_size)
15901 rtx elt = CONST_VECTOR_ELT (rtl, i);
15902 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15904 break;
15906 case MODE_VECTOR_FLOAT:
15907 for (i = 0, p = array; i < length; i++, p += elt_size)
15909 rtx elt = CONST_VECTOR_ELT (rtl, i);
15910 insert_float (elt, p);
15912 break;
15914 default:
15915 gcc_unreachable ();
15918 loc_result = new_loc_descr (DW_OP_implicit_value,
15919 length * elt_size, 0);
15920 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15921 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15922 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15923 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15925 break;
15927 case CONST:
15928 if (mode == VOIDmode
15929 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15930 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15931 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15933 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15934 break;
15936 /* FALLTHROUGH */
15937 case SYMBOL_REF:
15938 if (!const_ok_for_output (rtl))
15939 break;
15940 /* FALLTHROUGH */
15941 case LABEL_REF:
15942 if (is_a <scalar_int_mode> (mode, &int_mode)
15943 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15944 && (dwarf_version >= 4 || !dwarf_strict))
15946 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15947 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15948 vec_safe_push (used_rtx_array, rtl);
15950 break;
15952 case DEBUG_IMPLICIT_PTR:
15953 loc_result = implicit_ptr_descriptor (rtl, 0);
15954 break;
15956 case PLUS:
15957 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15958 && CONST_INT_P (XEXP (rtl, 1)))
15960 loc_result
15961 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15962 break;
15964 /* FALLTHRU */
15965 do_default:
15966 default:
15967 if ((is_a <scalar_int_mode> (mode, &int_mode)
15968 && GET_MODE (rtl) == int_mode
15969 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15970 && dwarf_version >= 4)
15971 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15973 /* Value expression. */
15974 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15975 if (loc_result)
15976 add_loc_descr (&loc_result,
15977 new_loc_descr (DW_OP_stack_value, 0, 0));
15979 break;
15982 return loc_result;
15985 /* We need to figure out what section we should use as the base for the
15986 address ranges where a given location is valid.
15987 1. If this particular DECL has a section associated with it, use that.
15988 2. If this function has a section associated with it, use that.
15989 3. Otherwise, use the text section.
15990 XXX: If you split a variable across multiple sections, we won't notice. */
15992 static const char *
15993 secname_for_decl (const_tree decl)
15995 const char *secname;
15997 if (VAR_OR_FUNCTION_DECL_P (decl)
15998 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15999 && DECL_SECTION_NAME (decl))
16000 secname = DECL_SECTION_NAME (decl);
16001 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16002 secname = DECL_SECTION_NAME (current_function_decl);
16003 else if (cfun && in_cold_section_p)
16004 secname = crtl->subsections.cold_section_label;
16005 else
16006 secname = text_section_label;
16008 return secname;
16011 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16013 static bool
16014 decl_by_reference_p (tree decl)
16016 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16017 || VAR_P (decl))
16018 && DECL_BY_REFERENCE (decl));
16021 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16022 for VARLOC. */
16024 static dw_loc_descr_ref
16025 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16026 enum var_init_status initialized)
16028 int have_address = 0;
16029 dw_loc_descr_ref descr;
16030 machine_mode mode;
16032 if (want_address != 2)
16034 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16035 /* Single part. */
16036 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16038 varloc = PAT_VAR_LOCATION_LOC (varloc);
16039 if (GET_CODE (varloc) == EXPR_LIST)
16040 varloc = XEXP (varloc, 0);
16041 mode = GET_MODE (varloc);
16042 if (MEM_P (varloc))
16044 rtx addr = XEXP (varloc, 0);
16045 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16046 mode, initialized);
16047 if (descr)
16048 have_address = 1;
16049 else
16051 rtx x = avoid_constant_pool_reference (varloc);
16052 if (x != varloc)
16053 descr = mem_loc_descriptor (x, mode, VOIDmode,
16054 initialized);
16057 else
16058 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16060 else
16061 return 0;
16063 else
16065 if (GET_CODE (varloc) == VAR_LOCATION)
16066 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16067 else
16068 mode = DECL_MODE (loc);
16069 descr = loc_descriptor (varloc, mode, initialized);
16070 have_address = 1;
16073 if (!descr)
16074 return 0;
16076 if (want_address == 2 && !have_address
16077 && (dwarf_version >= 4 || !dwarf_strict))
16079 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16081 expansion_failed (loc, NULL_RTX,
16082 "DWARF address size mismatch");
16083 return 0;
16085 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16086 have_address = 1;
16088 /* Show if we can't fill the request for an address. */
16089 if (want_address && !have_address)
16091 expansion_failed (loc, NULL_RTX,
16092 "Want address and only have value");
16093 return 0;
16096 /* If we've got an address and don't want one, dereference. */
16097 if (!want_address && have_address)
16099 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16100 enum dwarf_location_atom op;
16102 if (size > DWARF2_ADDR_SIZE || size == -1)
16104 expansion_failed (loc, NULL_RTX,
16105 "DWARF address size mismatch");
16106 return 0;
16108 else if (size == DWARF2_ADDR_SIZE)
16109 op = DW_OP_deref;
16110 else
16111 op = DW_OP_deref_size;
16113 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16116 return descr;
16119 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16120 if it is not possible. */
16122 static dw_loc_descr_ref
16123 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16125 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16126 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16127 else if (dwarf_version >= 3 || !dwarf_strict)
16128 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16129 else
16130 return NULL;
16133 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16134 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16136 static dw_loc_descr_ref
16137 dw_sra_loc_expr (tree decl, rtx loc)
16139 rtx p;
16140 unsigned HOST_WIDE_INT padsize = 0;
16141 dw_loc_descr_ref descr, *descr_tail;
16142 unsigned HOST_WIDE_INT decl_size;
16143 rtx varloc;
16144 enum var_init_status initialized;
16146 if (DECL_SIZE (decl) == NULL
16147 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16148 return NULL;
16150 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16151 descr = NULL;
16152 descr_tail = &descr;
16154 for (p = loc; p; p = XEXP (p, 1))
16156 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16157 rtx loc_note = *decl_piece_varloc_ptr (p);
16158 dw_loc_descr_ref cur_descr;
16159 dw_loc_descr_ref *tail, last = NULL;
16160 unsigned HOST_WIDE_INT opsize = 0;
16162 if (loc_note == NULL_RTX
16163 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16165 padsize += bitsize;
16166 continue;
16168 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16169 varloc = NOTE_VAR_LOCATION (loc_note);
16170 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16171 if (cur_descr == NULL)
16173 padsize += bitsize;
16174 continue;
16177 /* Check that cur_descr either doesn't use
16178 DW_OP_*piece operations, or their sum is equal
16179 to bitsize. Otherwise we can't embed it. */
16180 for (tail = &cur_descr; *tail != NULL;
16181 tail = &(*tail)->dw_loc_next)
16182 if ((*tail)->dw_loc_opc == DW_OP_piece)
16184 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16185 * BITS_PER_UNIT;
16186 last = *tail;
16188 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16190 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16191 last = *tail;
16194 if (last != NULL && opsize != bitsize)
16196 padsize += bitsize;
16197 /* Discard the current piece of the descriptor and release any
16198 addr_table entries it uses. */
16199 remove_loc_list_addr_table_entries (cur_descr);
16200 continue;
16203 /* If there is a hole, add DW_OP_*piece after empty DWARF
16204 expression, which means that those bits are optimized out. */
16205 if (padsize)
16207 if (padsize > decl_size)
16209 remove_loc_list_addr_table_entries (cur_descr);
16210 goto discard_descr;
16212 decl_size -= padsize;
16213 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16214 if (*descr_tail == NULL)
16216 remove_loc_list_addr_table_entries (cur_descr);
16217 goto discard_descr;
16219 descr_tail = &(*descr_tail)->dw_loc_next;
16220 padsize = 0;
16222 *descr_tail = cur_descr;
16223 descr_tail = tail;
16224 if (bitsize > decl_size)
16225 goto discard_descr;
16226 decl_size -= bitsize;
16227 if (last == NULL)
16229 HOST_WIDE_INT offset = 0;
16230 if (GET_CODE (varloc) == VAR_LOCATION
16231 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16233 varloc = PAT_VAR_LOCATION_LOC (varloc);
16234 if (GET_CODE (varloc) == EXPR_LIST)
16235 varloc = XEXP (varloc, 0);
16239 if (GET_CODE (varloc) == CONST
16240 || GET_CODE (varloc) == SIGN_EXTEND
16241 || GET_CODE (varloc) == ZERO_EXTEND)
16242 varloc = XEXP (varloc, 0);
16243 else if (GET_CODE (varloc) == SUBREG)
16244 varloc = SUBREG_REG (varloc);
16245 else
16246 break;
16248 while (1);
16249 /* DW_OP_bit_size offset should be zero for register
16250 or implicit location descriptions and empty location
16251 descriptions, but for memory addresses needs big endian
16252 adjustment. */
16253 if (MEM_P (varloc))
16255 unsigned HOST_WIDE_INT memsize
16256 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16257 if (memsize != bitsize)
16259 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16260 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16261 goto discard_descr;
16262 if (memsize < bitsize)
16263 goto discard_descr;
16264 if (BITS_BIG_ENDIAN)
16265 offset = memsize - bitsize;
16269 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16270 if (*descr_tail == NULL)
16271 goto discard_descr;
16272 descr_tail = &(*descr_tail)->dw_loc_next;
16276 /* If there were any non-empty expressions, add padding till the end of
16277 the decl. */
16278 if (descr != NULL && decl_size != 0)
16280 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16281 if (*descr_tail == NULL)
16282 goto discard_descr;
16284 return descr;
16286 discard_descr:
16287 /* Discard the descriptor and release any addr_table entries it uses. */
16288 remove_loc_list_addr_table_entries (descr);
16289 return NULL;
16292 /* Return the dwarf representation of the location list LOC_LIST of
16293 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16294 function. */
16296 static dw_loc_list_ref
16297 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16299 const char *endname, *secname;
16300 rtx varloc;
16301 enum var_init_status initialized;
16302 struct var_loc_node *node;
16303 dw_loc_descr_ref descr;
16304 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16305 dw_loc_list_ref list = NULL;
16306 dw_loc_list_ref *listp = &list;
16308 /* Now that we know what section we are using for a base,
16309 actually construct the list of locations.
16310 The first location information is what is passed to the
16311 function that creates the location list, and the remaining
16312 locations just get added on to that list.
16313 Note that we only know the start address for a location
16314 (IE location changes), so to build the range, we use
16315 the range [current location start, next location start].
16316 This means we have to special case the last node, and generate
16317 a range of [last location start, end of function label]. */
16319 secname = secname_for_decl (decl);
16321 for (node = loc_list->first; node; node = node->next)
16322 if (GET_CODE (node->loc) == EXPR_LIST
16323 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16325 if (GET_CODE (node->loc) == EXPR_LIST)
16327 /* This requires DW_OP_{,bit_}piece, which is not usable
16328 inside DWARF expressions. */
16329 if (want_address != 2)
16330 continue;
16331 descr = dw_sra_loc_expr (decl, node->loc);
16332 if (descr == NULL)
16333 continue;
16335 else
16337 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16338 varloc = NOTE_VAR_LOCATION (node->loc);
16339 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16341 if (descr)
16343 bool range_across_switch = false;
16344 /* If section switch happens in between node->label
16345 and node->next->label (or end of function) and
16346 we can't emit it as a single entry list,
16347 emit two ranges, first one ending at the end
16348 of first partition and second one starting at the
16349 beginning of second partition. */
16350 if (node == loc_list->last_before_switch
16351 && (node != loc_list->first || loc_list->first->next)
16352 && current_function_decl)
16354 endname = cfun->fde->dw_fde_end;
16355 range_across_switch = true;
16357 /* The variable has a location between NODE->LABEL and
16358 NODE->NEXT->LABEL. */
16359 else if (node->next)
16360 endname = node->next->label;
16361 /* If the variable has a location at the last label
16362 it keeps its location until the end of function. */
16363 else if (!current_function_decl)
16364 endname = text_end_label;
16365 else
16367 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16368 current_function_funcdef_no);
16369 endname = ggc_strdup (label_id);
16372 *listp = new_loc_list (descr, node->label, endname, secname);
16373 if (TREE_CODE (decl) == PARM_DECL
16374 && node == loc_list->first
16375 && NOTE_P (node->loc)
16376 && strcmp (node->label, endname) == 0)
16377 (*listp)->force = true;
16378 listp = &(*listp)->dw_loc_next;
16380 if (range_across_switch)
16382 if (GET_CODE (node->loc) == EXPR_LIST)
16383 descr = dw_sra_loc_expr (decl, node->loc);
16384 else
16386 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16387 varloc = NOTE_VAR_LOCATION (node->loc);
16388 descr = dw_loc_list_1 (decl, varloc, want_address,
16389 initialized);
16391 gcc_assert (descr);
16392 /* The variable has a location between NODE->LABEL and
16393 NODE->NEXT->LABEL. */
16394 if (node->next)
16395 endname = node->next->label;
16396 else
16397 endname = cfun->fde->dw_fde_second_end;
16398 *listp = new_loc_list (descr,
16399 cfun->fde->dw_fde_second_begin,
16400 endname, secname);
16401 listp = &(*listp)->dw_loc_next;
16406 /* Try to avoid the overhead of a location list emitting a location
16407 expression instead, but only if we didn't have more than one
16408 location entry in the first place. If some entries were not
16409 representable, we don't want to pretend a single entry that was
16410 applies to the entire scope in which the variable is
16411 available. */
16412 if (list && loc_list->first->next)
16413 gen_llsym (list);
16415 return list;
16418 /* Return if the loc_list has only single element and thus can be represented
16419 as location description. */
16421 static bool
16422 single_element_loc_list_p (dw_loc_list_ref list)
16424 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16425 return !list->ll_symbol;
16428 /* Duplicate a single element of location list. */
16430 static inline dw_loc_descr_ref
16431 copy_loc_descr (dw_loc_descr_ref ref)
16433 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16434 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16435 return copy;
16438 /* To each location in list LIST append loc descr REF. */
16440 static void
16441 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16443 dw_loc_descr_ref copy;
16444 add_loc_descr (&list->expr, ref);
16445 list = list->dw_loc_next;
16446 while (list)
16448 copy = copy_loc_descr (ref);
16449 add_loc_descr (&list->expr, copy);
16450 while (copy->dw_loc_next)
16451 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16452 list = list->dw_loc_next;
16456 /* To each location in list LIST prepend loc descr REF. */
16458 static void
16459 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16461 dw_loc_descr_ref copy;
16462 dw_loc_descr_ref ref_end = list->expr;
16463 add_loc_descr (&ref, list->expr);
16464 list->expr = ref;
16465 list = list->dw_loc_next;
16466 while (list)
16468 dw_loc_descr_ref end = list->expr;
16469 list->expr = copy = copy_loc_descr (ref);
16470 while (copy->dw_loc_next != ref_end)
16471 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16472 copy->dw_loc_next = end;
16473 list = list->dw_loc_next;
16477 /* Given two lists RET and LIST
16478 produce location list that is result of adding expression in LIST
16479 to expression in RET on each position in program.
16480 Might be destructive on both RET and LIST.
16482 TODO: We handle only simple cases of RET or LIST having at most one
16483 element. General case would involve sorting the lists in program order
16484 and merging them that will need some additional work.
16485 Adding that will improve quality of debug info especially for SRA-ed
16486 structures. */
16488 static void
16489 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16491 if (!list)
16492 return;
16493 if (!*ret)
16495 *ret = list;
16496 return;
16498 if (!list->dw_loc_next)
16500 add_loc_descr_to_each (*ret, list->expr);
16501 return;
16503 if (!(*ret)->dw_loc_next)
16505 prepend_loc_descr_to_each (list, (*ret)->expr);
16506 *ret = list;
16507 return;
16509 expansion_failed (NULL_TREE, NULL_RTX,
16510 "Don't know how to merge two non-trivial"
16511 " location lists.\n");
16512 *ret = NULL;
16513 return;
16516 /* LOC is constant expression. Try a luck, look it up in constant
16517 pool and return its loc_descr of its address. */
16519 static dw_loc_descr_ref
16520 cst_pool_loc_descr (tree loc)
16522 /* Get an RTL for this, if something has been emitted. */
16523 rtx rtl = lookup_constant_def (loc);
16525 if (!rtl || !MEM_P (rtl))
16527 gcc_assert (!rtl);
16528 return 0;
16530 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16532 /* TODO: We might get more coverage if we was actually delaying expansion
16533 of all expressions till end of compilation when constant pools are fully
16534 populated. */
16535 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16537 expansion_failed (loc, NULL_RTX,
16538 "CST value in contant pool but not marked.");
16539 return 0;
16541 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16542 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16545 /* Return dw_loc_list representing address of addr_expr LOC
16546 by looking for inner INDIRECT_REF expression and turning
16547 it into simple arithmetics.
16549 See loc_list_from_tree for the meaning of CONTEXT. */
16551 static dw_loc_list_ref
16552 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16553 loc_descr_context *context)
16555 tree obj, offset;
16556 HOST_WIDE_INT bitsize, bitpos, bytepos;
16557 machine_mode mode;
16558 int unsignedp, reversep, volatilep = 0;
16559 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16561 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16562 &bitsize, &bitpos, &offset, &mode,
16563 &unsignedp, &reversep, &volatilep);
16564 STRIP_NOPS (obj);
16565 if (bitpos % BITS_PER_UNIT)
16567 expansion_failed (loc, NULL_RTX, "bitfield access");
16568 return 0;
16570 if (!INDIRECT_REF_P (obj))
16572 expansion_failed (obj,
16573 NULL_RTX, "no indirect ref in inner refrence");
16574 return 0;
16576 if (!offset && !bitpos)
16577 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16578 context);
16579 else if (toplev
16580 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16581 && (dwarf_version >= 4 || !dwarf_strict))
16583 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16584 if (!list_ret)
16585 return 0;
16586 if (offset)
16588 /* Variable offset. */
16589 list_ret1 = loc_list_from_tree (offset, 0, context);
16590 if (list_ret1 == 0)
16591 return 0;
16592 add_loc_list (&list_ret, list_ret1);
16593 if (!list_ret)
16594 return 0;
16595 add_loc_descr_to_each (list_ret,
16596 new_loc_descr (DW_OP_plus, 0, 0));
16598 bytepos = bitpos / BITS_PER_UNIT;
16599 if (bytepos > 0)
16600 add_loc_descr_to_each (list_ret,
16601 new_loc_descr (DW_OP_plus_uconst,
16602 bytepos, 0));
16603 else if (bytepos < 0)
16604 loc_list_plus_const (list_ret, bytepos);
16605 add_loc_descr_to_each (list_ret,
16606 new_loc_descr (DW_OP_stack_value, 0, 0));
16608 return list_ret;
16611 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16612 all operations from LOC are nops, move to the last one. Insert in NOPS all
16613 operations that are skipped. */
16615 static void
16616 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16617 hash_set<dw_loc_descr_ref> &nops)
16619 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16621 nops.add (loc);
16622 loc = loc->dw_loc_next;
16626 /* Helper for loc_descr_without_nops: free the location description operation
16627 P. */
16629 bool
16630 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16632 ggc_free (loc);
16633 return true;
16636 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16637 finishes LOC. */
16639 static void
16640 loc_descr_without_nops (dw_loc_descr_ref &loc)
16642 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16643 return;
16645 /* Set of all DW_OP_nop operations we remove. */
16646 hash_set<dw_loc_descr_ref> nops;
16648 /* First, strip all prefix NOP operations in order to keep the head of the
16649 operations list. */
16650 loc_descr_to_next_no_nop (loc, nops);
16652 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16654 /* For control flow operations: strip "prefix" nops in destination
16655 labels. */
16656 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16657 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16658 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16659 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16661 /* Do the same for the operations that follow, then move to the next
16662 iteration. */
16663 if (cur->dw_loc_next != NULL)
16664 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16665 cur = cur->dw_loc_next;
16668 nops.traverse<void *, free_loc_descr> (NULL);
16672 struct dwarf_procedure_info;
16674 /* Helper structure for location descriptions generation. */
16675 struct loc_descr_context
16677 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16678 NULL_TREE if DW_OP_push_object_address in invalid for this location
16679 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16680 tree context_type;
16681 /* The ..._DECL node that should be translated as a
16682 DW_OP_push_object_address operation. */
16683 tree base_decl;
16684 /* Information about the DWARF procedure we are currently generating. NULL if
16685 we are not generating a DWARF procedure. */
16686 struct dwarf_procedure_info *dpi;
16687 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16688 by consumer. Used for DW_TAG_generic_subrange attributes. */
16689 bool placeholder_arg;
16690 /* True if PLACEHOLDER_EXPR has been seen. */
16691 bool placeholder_seen;
16694 /* DWARF procedures generation
16696 DWARF expressions (aka. location descriptions) are used to encode variable
16697 things such as sizes or offsets. Such computations can have redundant parts
16698 that can be factorized in order to reduce the size of the output debug
16699 information. This is the whole point of DWARF procedures.
16701 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16702 already factorized into functions ("size functions") in order to handle very
16703 big and complex types. Such functions are quite simple: they have integral
16704 arguments, they return an integral result and their body contains only a
16705 return statement with arithmetic expressions. This is the only kind of
16706 function we are interested in translating into DWARF procedures, here.
16708 DWARF expressions and DWARF procedure are executed using a stack, so we have
16709 to define some calling convention for them to interact. Let's say that:
16711 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16712 all arguments in reverse order (right-to-left) so that when the DWARF
16713 procedure execution starts, the first argument is the top of the stack.
16715 - Then, when returning, the DWARF procedure must have consumed all arguments
16716 on the stack, must have pushed the result and touched nothing else.
16718 - Each integral argument and the result are integral types can be hold in a
16719 single stack slot.
16721 - We call "frame offset" the number of stack slots that are "under DWARF
16722 procedure control": it includes the arguments slots, the temporaries and
16723 the result slot. Thus, it is equal to the number of arguments when the
16724 procedure execution starts and must be equal to one (the result) when it
16725 returns. */
16727 /* Helper structure used when generating operations for a DWARF procedure. */
16728 struct dwarf_procedure_info
16730 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16731 currently translated. */
16732 tree fndecl;
16733 /* The number of arguments FNDECL takes. */
16734 unsigned args_count;
16737 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16738 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16739 equate it to this DIE. */
16741 static dw_die_ref
16742 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16743 dw_die_ref parent_die)
16745 dw_die_ref dwarf_proc_die;
16747 if ((dwarf_version < 3 && dwarf_strict)
16748 || location == NULL)
16749 return NULL;
16751 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16752 if (fndecl)
16753 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16754 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16755 return dwarf_proc_die;
16758 /* Return whether TYPE is a supported type as a DWARF procedure argument
16759 type or return type (we handle only scalar types and pointer types that
16760 aren't wider than the DWARF expression evaluation stack. */
16762 static bool
16763 is_handled_procedure_type (tree type)
16765 return ((INTEGRAL_TYPE_P (type)
16766 || TREE_CODE (type) == OFFSET_TYPE
16767 || TREE_CODE (type) == POINTER_TYPE)
16768 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16771 /* Helper for resolve_args_picking: do the same but stop when coming across
16772 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16773 offset *before* evaluating the corresponding operation. */
16775 static bool
16776 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16777 struct dwarf_procedure_info *dpi,
16778 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16780 /* The "frame_offset" identifier is already used to name a macro... */
16781 unsigned frame_offset_ = initial_frame_offset;
16782 dw_loc_descr_ref l;
16784 for (l = loc; l != NULL;)
16786 bool existed;
16787 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16789 /* If we already met this node, there is nothing to compute anymore. */
16790 if (existed)
16792 /* Make sure that the stack size is consistent wherever the execution
16793 flow comes from. */
16794 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16795 break;
16797 l_frame_offset = frame_offset_;
16799 /* If needed, relocate the picking offset with respect to the frame
16800 offset. */
16801 if (l->frame_offset_rel)
16803 unsigned HOST_WIDE_INT off;
16804 switch (l->dw_loc_opc)
16806 case DW_OP_pick:
16807 off = l->dw_loc_oprnd1.v.val_unsigned;
16808 break;
16809 case DW_OP_dup:
16810 off = 0;
16811 break;
16812 case DW_OP_over:
16813 off = 1;
16814 break;
16815 default:
16816 gcc_unreachable ();
16818 /* frame_offset_ is the size of the current stack frame, including
16819 incoming arguments. Besides, the arguments are pushed
16820 right-to-left. Thus, in order to access the Nth argument from
16821 this operation node, the picking has to skip temporaries *plus*
16822 one stack slot per argument (0 for the first one, 1 for the second
16823 one, etc.).
16825 The targetted argument number (N) is already set as the operand,
16826 and the number of temporaries can be computed with:
16827 frame_offsets_ - dpi->args_count */
16828 off += frame_offset_ - dpi->args_count;
16830 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16831 if (off > 255)
16832 return false;
16834 if (off == 0)
16836 l->dw_loc_opc = DW_OP_dup;
16837 l->dw_loc_oprnd1.v.val_unsigned = 0;
16839 else if (off == 1)
16841 l->dw_loc_opc = DW_OP_over;
16842 l->dw_loc_oprnd1.v.val_unsigned = 0;
16844 else
16846 l->dw_loc_opc = DW_OP_pick;
16847 l->dw_loc_oprnd1.v.val_unsigned = off;
16851 /* Update frame_offset according to the effect the current operation has
16852 on the stack. */
16853 switch (l->dw_loc_opc)
16855 case DW_OP_deref:
16856 case DW_OP_swap:
16857 case DW_OP_rot:
16858 case DW_OP_abs:
16859 case DW_OP_neg:
16860 case DW_OP_not:
16861 case DW_OP_plus_uconst:
16862 case DW_OP_skip:
16863 case DW_OP_reg0:
16864 case DW_OP_reg1:
16865 case DW_OP_reg2:
16866 case DW_OP_reg3:
16867 case DW_OP_reg4:
16868 case DW_OP_reg5:
16869 case DW_OP_reg6:
16870 case DW_OP_reg7:
16871 case DW_OP_reg8:
16872 case DW_OP_reg9:
16873 case DW_OP_reg10:
16874 case DW_OP_reg11:
16875 case DW_OP_reg12:
16876 case DW_OP_reg13:
16877 case DW_OP_reg14:
16878 case DW_OP_reg15:
16879 case DW_OP_reg16:
16880 case DW_OP_reg17:
16881 case DW_OP_reg18:
16882 case DW_OP_reg19:
16883 case DW_OP_reg20:
16884 case DW_OP_reg21:
16885 case DW_OP_reg22:
16886 case DW_OP_reg23:
16887 case DW_OP_reg24:
16888 case DW_OP_reg25:
16889 case DW_OP_reg26:
16890 case DW_OP_reg27:
16891 case DW_OP_reg28:
16892 case DW_OP_reg29:
16893 case DW_OP_reg30:
16894 case DW_OP_reg31:
16895 case DW_OP_bregx:
16896 case DW_OP_piece:
16897 case DW_OP_deref_size:
16898 case DW_OP_nop:
16899 case DW_OP_bit_piece:
16900 case DW_OP_implicit_value:
16901 case DW_OP_stack_value:
16902 break;
16904 case DW_OP_addr:
16905 case DW_OP_const1u:
16906 case DW_OP_const1s:
16907 case DW_OP_const2u:
16908 case DW_OP_const2s:
16909 case DW_OP_const4u:
16910 case DW_OP_const4s:
16911 case DW_OP_const8u:
16912 case DW_OP_const8s:
16913 case DW_OP_constu:
16914 case DW_OP_consts:
16915 case DW_OP_dup:
16916 case DW_OP_over:
16917 case DW_OP_pick:
16918 case DW_OP_lit0:
16919 case DW_OP_lit1:
16920 case DW_OP_lit2:
16921 case DW_OP_lit3:
16922 case DW_OP_lit4:
16923 case DW_OP_lit5:
16924 case DW_OP_lit6:
16925 case DW_OP_lit7:
16926 case DW_OP_lit8:
16927 case DW_OP_lit9:
16928 case DW_OP_lit10:
16929 case DW_OP_lit11:
16930 case DW_OP_lit12:
16931 case DW_OP_lit13:
16932 case DW_OP_lit14:
16933 case DW_OP_lit15:
16934 case DW_OP_lit16:
16935 case DW_OP_lit17:
16936 case DW_OP_lit18:
16937 case DW_OP_lit19:
16938 case DW_OP_lit20:
16939 case DW_OP_lit21:
16940 case DW_OP_lit22:
16941 case DW_OP_lit23:
16942 case DW_OP_lit24:
16943 case DW_OP_lit25:
16944 case DW_OP_lit26:
16945 case DW_OP_lit27:
16946 case DW_OP_lit28:
16947 case DW_OP_lit29:
16948 case DW_OP_lit30:
16949 case DW_OP_lit31:
16950 case DW_OP_breg0:
16951 case DW_OP_breg1:
16952 case DW_OP_breg2:
16953 case DW_OP_breg3:
16954 case DW_OP_breg4:
16955 case DW_OP_breg5:
16956 case DW_OP_breg6:
16957 case DW_OP_breg7:
16958 case DW_OP_breg8:
16959 case DW_OP_breg9:
16960 case DW_OP_breg10:
16961 case DW_OP_breg11:
16962 case DW_OP_breg12:
16963 case DW_OP_breg13:
16964 case DW_OP_breg14:
16965 case DW_OP_breg15:
16966 case DW_OP_breg16:
16967 case DW_OP_breg17:
16968 case DW_OP_breg18:
16969 case DW_OP_breg19:
16970 case DW_OP_breg20:
16971 case DW_OP_breg21:
16972 case DW_OP_breg22:
16973 case DW_OP_breg23:
16974 case DW_OP_breg24:
16975 case DW_OP_breg25:
16976 case DW_OP_breg26:
16977 case DW_OP_breg27:
16978 case DW_OP_breg28:
16979 case DW_OP_breg29:
16980 case DW_OP_breg30:
16981 case DW_OP_breg31:
16982 case DW_OP_fbreg:
16983 case DW_OP_push_object_address:
16984 case DW_OP_call_frame_cfa:
16985 case DW_OP_GNU_variable_value:
16986 ++frame_offset_;
16987 break;
16989 case DW_OP_drop:
16990 case DW_OP_xderef:
16991 case DW_OP_and:
16992 case DW_OP_div:
16993 case DW_OP_minus:
16994 case DW_OP_mod:
16995 case DW_OP_mul:
16996 case DW_OP_or:
16997 case DW_OP_plus:
16998 case DW_OP_shl:
16999 case DW_OP_shr:
17000 case DW_OP_shra:
17001 case DW_OP_xor:
17002 case DW_OP_bra:
17003 case DW_OP_eq:
17004 case DW_OP_ge:
17005 case DW_OP_gt:
17006 case DW_OP_le:
17007 case DW_OP_lt:
17008 case DW_OP_ne:
17009 case DW_OP_regx:
17010 case DW_OP_xderef_size:
17011 --frame_offset_;
17012 break;
17014 case DW_OP_call2:
17015 case DW_OP_call4:
17016 case DW_OP_call_ref:
17018 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17019 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17021 if (stack_usage == NULL)
17022 return false;
17023 frame_offset_ += *stack_usage;
17024 break;
17027 case DW_OP_implicit_pointer:
17028 case DW_OP_entry_value:
17029 case DW_OP_const_type:
17030 case DW_OP_regval_type:
17031 case DW_OP_deref_type:
17032 case DW_OP_convert:
17033 case DW_OP_reinterpret:
17034 case DW_OP_form_tls_address:
17035 case DW_OP_GNU_push_tls_address:
17036 case DW_OP_GNU_uninit:
17037 case DW_OP_GNU_encoded_addr:
17038 case DW_OP_GNU_implicit_pointer:
17039 case DW_OP_GNU_entry_value:
17040 case DW_OP_GNU_const_type:
17041 case DW_OP_GNU_regval_type:
17042 case DW_OP_GNU_deref_type:
17043 case DW_OP_GNU_convert:
17044 case DW_OP_GNU_reinterpret:
17045 case DW_OP_GNU_parameter_ref:
17046 /* loc_list_from_tree will probably not output these operations for
17047 size functions, so assume they will not appear here. */
17048 /* Fall through... */
17050 default:
17051 gcc_unreachable ();
17054 /* Now, follow the control flow (except subroutine calls). */
17055 switch (l->dw_loc_opc)
17057 case DW_OP_bra:
17058 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17059 frame_offsets))
17060 return false;
17061 /* Fall through. */
17063 case DW_OP_skip:
17064 l = l->dw_loc_oprnd1.v.val_loc;
17065 break;
17067 case DW_OP_stack_value:
17068 return true;
17070 default:
17071 l = l->dw_loc_next;
17072 break;
17076 return true;
17079 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17080 operations) in order to resolve the operand of DW_OP_pick operations that
17081 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17082 offset *before* LOC is executed. Return if all relocations were
17083 successful. */
17085 static bool
17086 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17087 struct dwarf_procedure_info *dpi)
17089 /* Associate to all visited operations the frame offset *before* evaluating
17090 this operation. */
17091 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17093 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17094 frame_offsets);
17097 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17098 Return NULL if it is not possible. */
17100 static dw_die_ref
17101 function_to_dwarf_procedure (tree fndecl)
17103 struct loc_descr_context ctx;
17104 struct dwarf_procedure_info dpi;
17105 dw_die_ref dwarf_proc_die;
17106 tree tree_body = DECL_SAVED_TREE (fndecl);
17107 dw_loc_descr_ref loc_body, epilogue;
17109 tree cursor;
17110 unsigned i;
17112 /* Do not generate multiple DWARF procedures for the same function
17113 declaration. */
17114 dwarf_proc_die = lookup_decl_die (fndecl);
17115 if (dwarf_proc_die != NULL)
17116 return dwarf_proc_die;
17118 /* DWARF procedures are available starting with the DWARFv3 standard. */
17119 if (dwarf_version < 3 && dwarf_strict)
17120 return NULL;
17122 /* We handle only functions for which we still have a body, that return a
17123 supported type and that takes arguments with supported types. Note that
17124 there is no point translating functions that return nothing. */
17125 if (tree_body == NULL_TREE
17126 || DECL_RESULT (fndecl) == NULL_TREE
17127 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17128 return NULL;
17130 for (cursor = DECL_ARGUMENTS (fndecl);
17131 cursor != NULL_TREE;
17132 cursor = TREE_CHAIN (cursor))
17133 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17134 return NULL;
17136 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17137 if (TREE_CODE (tree_body) != RETURN_EXPR)
17138 return NULL;
17139 tree_body = TREE_OPERAND (tree_body, 0);
17140 if (TREE_CODE (tree_body) != MODIFY_EXPR
17141 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17142 return NULL;
17143 tree_body = TREE_OPERAND (tree_body, 1);
17145 /* Try to translate the body expression itself. Note that this will probably
17146 cause an infinite recursion if its call graph has a cycle. This is very
17147 unlikely for size functions, however, so don't bother with such things at
17148 the moment. */
17149 ctx.context_type = NULL_TREE;
17150 ctx.base_decl = NULL_TREE;
17151 ctx.dpi = &dpi;
17152 ctx.placeholder_arg = false;
17153 ctx.placeholder_seen = false;
17154 dpi.fndecl = fndecl;
17155 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17156 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17157 if (!loc_body)
17158 return NULL;
17160 /* After evaluating all operands in "loc_body", we should still have on the
17161 stack all arguments plus the desired function result (top of the stack).
17162 Generate code in order to keep only the result in our stack frame. */
17163 epilogue = NULL;
17164 for (i = 0; i < dpi.args_count; ++i)
17166 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17167 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17168 op_couple->dw_loc_next->dw_loc_next = epilogue;
17169 epilogue = op_couple;
17171 add_loc_descr (&loc_body, epilogue);
17172 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17173 return NULL;
17175 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17176 because they are considered useful. Now there is an epilogue, they are
17177 not anymore, so give it another try. */
17178 loc_descr_without_nops (loc_body);
17180 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17181 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17182 though, given that size functions do not come from source, so they should
17183 not have a dedicated DW_TAG_subprogram DIE. */
17184 dwarf_proc_die
17185 = new_dwarf_proc_die (loc_body, fndecl,
17186 get_context_die (DECL_CONTEXT (fndecl)));
17188 /* The called DWARF procedure consumes one stack slot per argument and
17189 returns one stack slot. */
17190 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17192 return dwarf_proc_die;
17196 /* Generate Dwarf location list representing LOC.
17197 If WANT_ADDRESS is false, expression computing LOC will be computed
17198 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17199 if WANT_ADDRESS is 2, expression computing address useable in location
17200 will be returned (i.e. DW_OP_reg can be used
17201 to refer to register values).
17203 CONTEXT provides information to customize the location descriptions
17204 generation. Its context_type field specifies what type is implicitly
17205 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17206 will not be generated.
17208 Its DPI field determines whether we are generating a DWARF expression for a
17209 DWARF procedure, so PARM_DECL references are processed specifically.
17211 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17212 and dpi fields were null. */
17214 static dw_loc_list_ref
17215 loc_list_from_tree_1 (tree loc, int want_address,
17216 struct loc_descr_context *context)
17218 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17219 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17220 int have_address = 0;
17221 enum dwarf_location_atom op;
17223 /* ??? Most of the time we do not take proper care for sign/zero
17224 extending the values properly. Hopefully this won't be a real
17225 problem... */
17227 if (context != NULL
17228 && context->base_decl == loc
17229 && want_address == 0)
17231 if (dwarf_version >= 3 || !dwarf_strict)
17232 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17233 NULL, NULL, NULL);
17234 else
17235 return NULL;
17238 switch (TREE_CODE (loc))
17240 case ERROR_MARK:
17241 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17242 return 0;
17244 case PLACEHOLDER_EXPR:
17245 /* This case involves extracting fields from an object to determine the
17246 position of other fields. It is supposed to appear only as the first
17247 operand of COMPONENT_REF nodes and to reference precisely the type
17248 that the context allows. */
17249 if (context != NULL
17250 && TREE_TYPE (loc) == context->context_type
17251 && want_address >= 1)
17253 if (dwarf_version >= 3 || !dwarf_strict)
17255 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17256 have_address = 1;
17257 break;
17259 else
17260 return NULL;
17262 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17263 the single argument passed by consumer. */
17264 else if (context != NULL
17265 && context->placeholder_arg
17266 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17267 && want_address == 0)
17269 ret = new_loc_descr (DW_OP_pick, 0, 0);
17270 ret->frame_offset_rel = 1;
17271 context->placeholder_seen = true;
17272 break;
17274 else
17275 expansion_failed (loc, NULL_RTX,
17276 "PLACEHOLDER_EXPR for an unexpected type");
17277 break;
17279 case CALL_EXPR:
17281 const int nargs = call_expr_nargs (loc);
17282 tree callee = get_callee_fndecl (loc);
17283 int i;
17284 dw_die_ref dwarf_proc;
17286 if (callee == NULL_TREE)
17287 goto call_expansion_failed;
17289 /* We handle only functions that return an integer. */
17290 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17291 goto call_expansion_failed;
17293 dwarf_proc = function_to_dwarf_procedure (callee);
17294 if (dwarf_proc == NULL)
17295 goto call_expansion_failed;
17297 /* Evaluate arguments right-to-left so that the first argument will
17298 be the top-most one on the stack. */
17299 for (i = nargs - 1; i >= 0; --i)
17301 dw_loc_descr_ref loc_descr
17302 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17303 context);
17305 if (loc_descr == NULL)
17306 goto call_expansion_failed;
17308 add_loc_descr (&ret, loc_descr);
17311 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17312 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17313 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17314 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17315 add_loc_descr (&ret, ret1);
17316 break;
17318 call_expansion_failed:
17319 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17320 /* There are no opcodes for these operations. */
17321 return 0;
17324 case PREINCREMENT_EXPR:
17325 case PREDECREMENT_EXPR:
17326 case POSTINCREMENT_EXPR:
17327 case POSTDECREMENT_EXPR:
17328 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17329 /* There are no opcodes for these operations. */
17330 return 0;
17332 case ADDR_EXPR:
17333 /* If we already want an address, see if there is INDIRECT_REF inside
17334 e.g. for &this->field. */
17335 if (want_address)
17337 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17338 (loc, want_address == 2, context);
17339 if (list_ret)
17340 have_address = 1;
17341 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17342 && (ret = cst_pool_loc_descr (loc)))
17343 have_address = 1;
17345 /* Otherwise, process the argument and look for the address. */
17346 if (!list_ret && !ret)
17347 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17348 else
17350 if (want_address)
17351 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17352 return NULL;
17354 break;
17356 case VAR_DECL:
17357 if (DECL_THREAD_LOCAL_P (loc))
17359 rtx rtl;
17360 enum dwarf_location_atom tls_op;
17361 enum dtprel_bool dtprel = dtprel_false;
17363 if (targetm.have_tls)
17365 /* If this is not defined, we have no way to emit the
17366 data. */
17367 if (!targetm.asm_out.output_dwarf_dtprel)
17368 return 0;
17370 /* The way DW_OP_GNU_push_tls_address is specified, we
17371 can only look up addresses of objects in the current
17372 module. We used DW_OP_addr as first op, but that's
17373 wrong, because DW_OP_addr is relocated by the debug
17374 info consumer, while DW_OP_GNU_push_tls_address
17375 operand shouldn't be. */
17376 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17377 return 0;
17378 dtprel = dtprel_true;
17379 /* We check for DWARF 5 here because gdb did not implement
17380 DW_OP_form_tls_address until after 7.12. */
17381 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17382 : DW_OP_GNU_push_tls_address);
17384 else
17386 if (!targetm.emutls.debug_form_tls_address
17387 || !(dwarf_version >= 3 || !dwarf_strict))
17388 return 0;
17389 /* We stuffed the control variable into the DECL_VALUE_EXPR
17390 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17391 no longer appear in gimple code. We used the control
17392 variable in specific so that we could pick it up here. */
17393 loc = DECL_VALUE_EXPR (loc);
17394 tls_op = DW_OP_form_tls_address;
17397 rtl = rtl_for_decl_location (loc);
17398 if (rtl == NULL_RTX)
17399 return 0;
17401 if (!MEM_P (rtl))
17402 return 0;
17403 rtl = XEXP (rtl, 0);
17404 if (! CONSTANT_P (rtl))
17405 return 0;
17407 ret = new_addr_loc_descr (rtl, dtprel);
17408 ret1 = new_loc_descr (tls_op, 0, 0);
17409 add_loc_descr (&ret, ret1);
17411 have_address = 1;
17412 break;
17414 /* FALLTHRU */
17416 case PARM_DECL:
17417 if (context != NULL && context->dpi != NULL
17418 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17420 /* We are generating code for a DWARF procedure and we want to access
17421 one of its arguments: find the appropriate argument offset and let
17422 the resolve_args_picking pass compute the offset that complies
17423 with the stack frame size. */
17424 unsigned i = 0;
17425 tree cursor;
17427 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17428 cursor != NULL_TREE && cursor != loc;
17429 cursor = TREE_CHAIN (cursor), ++i)
17431 /* If we are translating a DWARF procedure, all referenced parameters
17432 must belong to the current function. */
17433 gcc_assert (cursor != NULL_TREE);
17435 ret = new_loc_descr (DW_OP_pick, i, 0);
17436 ret->frame_offset_rel = 1;
17437 break;
17439 /* FALLTHRU */
17441 case RESULT_DECL:
17442 if (DECL_HAS_VALUE_EXPR_P (loc))
17443 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17444 want_address, context);
17445 /* FALLTHRU */
17447 case FUNCTION_DECL:
17449 rtx rtl;
17450 var_loc_list *loc_list = lookup_decl_loc (loc);
17452 if (loc_list && loc_list->first)
17454 list_ret = dw_loc_list (loc_list, loc, want_address);
17455 have_address = want_address != 0;
17456 break;
17458 rtl = rtl_for_decl_location (loc);
17459 if (rtl == NULL_RTX)
17461 if (TREE_CODE (loc) != FUNCTION_DECL
17462 && early_dwarf
17463 && current_function_decl
17464 && want_address != 1
17465 && ! DECL_IGNORED_P (loc)
17466 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17467 || POINTER_TYPE_P (TREE_TYPE (loc)))
17468 && DECL_CONTEXT (loc) == current_function_decl
17469 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17470 <= DWARF2_ADDR_SIZE))
17472 dw_die_ref ref = lookup_decl_die (loc);
17473 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17474 if (ref)
17476 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17477 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17478 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17480 else
17482 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17483 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17485 break;
17487 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17488 return 0;
17490 else if (CONST_INT_P (rtl))
17492 HOST_WIDE_INT val = INTVAL (rtl);
17493 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17494 val &= GET_MODE_MASK (DECL_MODE (loc));
17495 ret = int_loc_descriptor (val);
17497 else if (GET_CODE (rtl) == CONST_STRING)
17499 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17500 return 0;
17502 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17503 ret = new_addr_loc_descr (rtl, dtprel_false);
17504 else
17506 machine_mode mode, mem_mode;
17508 /* Certain constructs can only be represented at top-level. */
17509 if (want_address == 2)
17511 ret = loc_descriptor (rtl, VOIDmode,
17512 VAR_INIT_STATUS_INITIALIZED);
17513 have_address = 1;
17515 else
17517 mode = GET_MODE (rtl);
17518 mem_mode = VOIDmode;
17519 if (MEM_P (rtl))
17521 mem_mode = mode;
17522 mode = get_address_mode (rtl);
17523 rtl = XEXP (rtl, 0);
17524 have_address = 1;
17526 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17527 VAR_INIT_STATUS_INITIALIZED);
17529 if (!ret)
17530 expansion_failed (loc, rtl,
17531 "failed to produce loc descriptor for rtl");
17534 break;
17536 case MEM_REF:
17537 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17539 have_address = 1;
17540 goto do_plus;
17542 /* Fallthru. */
17543 case INDIRECT_REF:
17544 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17545 have_address = 1;
17546 break;
17548 case TARGET_MEM_REF:
17549 case SSA_NAME:
17550 case DEBUG_EXPR_DECL:
17551 return NULL;
17553 case COMPOUND_EXPR:
17554 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17555 context);
17557 CASE_CONVERT:
17558 case VIEW_CONVERT_EXPR:
17559 case SAVE_EXPR:
17560 case MODIFY_EXPR:
17561 case NON_LVALUE_EXPR:
17562 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17563 context);
17565 case COMPONENT_REF:
17566 case BIT_FIELD_REF:
17567 case ARRAY_REF:
17568 case ARRAY_RANGE_REF:
17569 case REALPART_EXPR:
17570 case IMAGPART_EXPR:
17572 tree obj, offset;
17573 HOST_WIDE_INT bitsize, bitpos, bytepos;
17574 machine_mode mode;
17575 int unsignedp, reversep, volatilep = 0;
17577 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17578 &unsignedp, &reversep, &volatilep);
17580 gcc_assert (obj != loc);
17582 list_ret = loc_list_from_tree_1 (obj,
17583 want_address == 2
17584 && !bitpos && !offset ? 2 : 1,
17585 context);
17586 /* TODO: We can extract value of the small expression via shifting even
17587 for nonzero bitpos. */
17588 if (list_ret == 0)
17589 return 0;
17590 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17592 expansion_failed (loc, NULL_RTX,
17593 "bitfield access");
17594 return 0;
17597 if (offset != NULL_TREE)
17599 /* Variable offset. */
17600 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17601 if (list_ret1 == 0)
17602 return 0;
17603 add_loc_list (&list_ret, list_ret1);
17604 if (!list_ret)
17605 return 0;
17606 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17609 bytepos = bitpos / BITS_PER_UNIT;
17610 if (bytepos > 0)
17611 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17612 else if (bytepos < 0)
17613 loc_list_plus_const (list_ret, bytepos);
17615 have_address = 1;
17616 break;
17619 case INTEGER_CST:
17620 if ((want_address || !tree_fits_shwi_p (loc))
17621 && (ret = cst_pool_loc_descr (loc)))
17622 have_address = 1;
17623 else if (want_address == 2
17624 && tree_fits_shwi_p (loc)
17625 && (ret = address_of_int_loc_descriptor
17626 (int_size_in_bytes (TREE_TYPE (loc)),
17627 tree_to_shwi (loc))))
17628 have_address = 1;
17629 else if (tree_fits_shwi_p (loc))
17630 ret = int_loc_descriptor (tree_to_shwi (loc));
17631 else if (tree_fits_uhwi_p (loc))
17632 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17633 else
17635 expansion_failed (loc, NULL_RTX,
17636 "Integer operand is not host integer");
17637 return 0;
17639 break;
17641 case CONSTRUCTOR:
17642 case REAL_CST:
17643 case STRING_CST:
17644 case COMPLEX_CST:
17645 if ((ret = cst_pool_loc_descr (loc)))
17646 have_address = 1;
17647 else if (TREE_CODE (loc) == CONSTRUCTOR)
17649 tree type = TREE_TYPE (loc);
17650 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17651 unsigned HOST_WIDE_INT offset = 0;
17652 unsigned HOST_WIDE_INT cnt;
17653 constructor_elt *ce;
17655 if (TREE_CODE (type) == RECORD_TYPE)
17657 /* This is very limited, but it's enough to output
17658 pointers to member functions, as long as the
17659 referenced function is defined in the current
17660 translation unit. */
17661 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17663 tree val = ce->value;
17665 tree field = ce->index;
17667 if (val)
17668 STRIP_NOPS (val);
17670 if (!field || DECL_BIT_FIELD (field))
17672 expansion_failed (loc, NULL_RTX,
17673 "bitfield in record type constructor");
17674 size = offset = (unsigned HOST_WIDE_INT)-1;
17675 ret = NULL;
17676 break;
17679 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17680 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17681 gcc_assert (pos + fieldsize <= size);
17682 if (pos < offset)
17684 expansion_failed (loc, NULL_RTX,
17685 "out-of-order fields in record constructor");
17686 size = offset = (unsigned HOST_WIDE_INT)-1;
17687 ret = NULL;
17688 break;
17690 if (pos > offset)
17692 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17693 add_loc_descr (&ret, ret1);
17694 offset = pos;
17696 if (val && fieldsize != 0)
17698 ret1 = loc_descriptor_from_tree (val, want_address, context);
17699 if (!ret1)
17701 expansion_failed (loc, NULL_RTX,
17702 "unsupported expression in field");
17703 size = offset = (unsigned HOST_WIDE_INT)-1;
17704 ret = NULL;
17705 break;
17707 add_loc_descr (&ret, ret1);
17709 if (fieldsize)
17711 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17712 add_loc_descr (&ret, ret1);
17713 offset = pos + fieldsize;
17717 if (offset != size)
17719 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17720 add_loc_descr (&ret, ret1);
17721 offset = size;
17724 have_address = !!want_address;
17726 else
17727 expansion_failed (loc, NULL_RTX,
17728 "constructor of non-record type");
17730 else
17731 /* We can construct small constants here using int_loc_descriptor. */
17732 expansion_failed (loc, NULL_RTX,
17733 "constructor or constant not in constant pool");
17734 break;
17736 case TRUTH_AND_EXPR:
17737 case TRUTH_ANDIF_EXPR:
17738 case BIT_AND_EXPR:
17739 op = DW_OP_and;
17740 goto do_binop;
17742 case TRUTH_XOR_EXPR:
17743 case BIT_XOR_EXPR:
17744 op = DW_OP_xor;
17745 goto do_binop;
17747 case TRUTH_OR_EXPR:
17748 case TRUTH_ORIF_EXPR:
17749 case BIT_IOR_EXPR:
17750 op = DW_OP_or;
17751 goto do_binop;
17753 case FLOOR_DIV_EXPR:
17754 case CEIL_DIV_EXPR:
17755 case ROUND_DIV_EXPR:
17756 case TRUNC_DIV_EXPR:
17757 case EXACT_DIV_EXPR:
17758 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17759 return 0;
17760 op = DW_OP_div;
17761 goto do_binop;
17763 case MINUS_EXPR:
17764 op = DW_OP_minus;
17765 goto do_binop;
17767 case FLOOR_MOD_EXPR:
17768 case CEIL_MOD_EXPR:
17769 case ROUND_MOD_EXPR:
17770 case TRUNC_MOD_EXPR:
17771 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17773 op = DW_OP_mod;
17774 goto do_binop;
17776 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17777 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17778 if (list_ret == 0 || list_ret1 == 0)
17779 return 0;
17781 add_loc_list (&list_ret, list_ret1);
17782 if (list_ret == 0)
17783 return 0;
17784 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17785 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17786 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17787 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17788 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17789 break;
17791 case MULT_EXPR:
17792 op = DW_OP_mul;
17793 goto do_binop;
17795 case LSHIFT_EXPR:
17796 op = DW_OP_shl;
17797 goto do_binop;
17799 case RSHIFT_EXPR:
17800 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17801 goto do_binop;
17803 case POINTER_PLUS_EXPR:
17804 case PLUS_EXPR:
17805 do_plus:
17806 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17808 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17809 smarter to encode their opposite. The DW_OP_plus_uconst operation
17810 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17811 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17812 bytes, Y being the size of the operation that pushes the opposite
17813 of the addend. So let's choose the smallest representation. */
17814 const tree tree_addend = TREE_OPERAND (loc, 1);
17815 offset_int wi_addend;
17816 HOST_WIDE_INT shwi_addend;
17817 dw_loc_descr_ref loc_naddend;
17819 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17820 if (list_ret == 0)
17821 return 0;
17823 /* Try to get the literal to push. It is the opposite of the addend,
17824 so as we rely on wrapping during DWARF evaluation, first decode
17825 the literal as a "DWARF-sized" signed number. */
17826 wi_addend = wi::to_offset (tree_addend);
17827 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17828 shwi_addend = wi_addend.to_shwi ();
17829 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17830 ? int_loc_descriptor (-shwi_addend)
17831 : NULL;
17833 if (loc_naddend != NULL
17834 && ((unsigned) size_of_uleb128 (shwi_addend)
17835 > size_of_loc_descr (loc_naddend)))
17837 add_loc_descr_to_each (list_ret, loc_naddend);
17838 add_loc_descr_to_each (list_ret,
17839 new_loc_descr (DW_OP_minus, 0, 0));
17841 else
17843 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17845 loc_naddend = loc_cur;
17846 loc_cur = loc_cur->dw_loc_next;
17847 ggc_free (loc_naddend);
17849 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17851 break;
17854 op = DW_OP_plus;
17855 goto do_binop;
17857 case LE_EXPR:
17858 op = DW_OP_le;
17859 goto do_comp_binop;
17861 case GE_EXPR:
17862 op = DW_OP_ge;
17863 goto do_comp_binop;
17865 case LT_EXPR:
17866 op = DW_OP_lt;
17867 goto do_comp_binop;
17869 case GT_EXPR:
17870 op = DW_OP_gt;
17871 goto do_comp_binop;
17873 do_comp_binop:
17874 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17876 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17877 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17878 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17879 TREE_CODE (loc));
17880 break;
17882 else
17883 goto do_binop;
17885 case EQ_EXPR:
17886 op = DW_OP_eq;
17887 goto do_binop;
17889 case NE_EXPR:
17890 op = DW_OP_ne;
17891 goto do_binop;
17893 do_binop:
17894 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17895 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17896 if (list_ret == 0 || list_ret1 == 0)
17897 return 0;
17899 add_loc_list (&list_ret, list_ret1);
17900 if (list_ret == 0)
17901 return 0;
17902 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17903 break;
17905 case TRUTH_NOT_EXPR:
17906 case BIT_NOT_EXPR:
17907 op = DW_OP_not;
17908 goto do_unop;
17910 case ABS_EXPR:
17911 op = DW_OP_abs;
17912 goto do_unop;
17914 case NEGATE_EXPR:
17915 op = DW_OP_neg;
17916 goto do_unop;
17918 do_unop:
17919 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17920 if (list_ret == 0)
17921 return 0;
17923 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17924 break;
17926 case MIN_EXPR:
17927 case MAX_EXPR:
17929 const enum tree_code code =
17930 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17932 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17933 build2 (code, integer_type_node,
17934 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17935 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17938 /* fall through */
17940 case COND_EXPR:
17942 dw_loc_descr_ref lhs
17943 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17944 dw_loc_list_ref rhs
17945 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17946 dw_loc_descr_ref bra_node, jump_node, tmp;
17948 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17949 if (list_ret == 0 || lhs == 0 || rhs == 0)
17950 return 0;
17952 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17953 add_loc_descr_to_each (list_ret, bra_node);
17955 add_loc_list (&list_ret, rhs);
17956 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17957 add_loc_descr_to_each (list_ret, jump_node);
17959 add_loc_descr_to_each (list_ret, lhs);
17960 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17961 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17963 /* ??? Need a node to point the skip at. Use a nop. */
17964 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17965 add_loc_descr_to_each (list_ret, tmp);
17966 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17967 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17969 break;
17971 case FIX_TRUNC_EXPR:
17972 return 0;
17974 default:
17975 /* Leave front-end specific codes as simply unknown. This comes
17976 up, for instance, with the C STMT_EXPR. */
17977 if ((unsigned int) TREE_CODE (loc)
17978 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17980 expansion_failed (loc, NULL_RTX,
17981 "language specific tree node");
17982 return 0;
17985 /* Otherwise this is a generic code; we should just lists all of
17986 these explicitly. We forgot one. */
17987 if (flag_checking)
17988 gcc_unreachable ();
17990 /* In a release build, we want to degrade gracefully: better to
17991 generate incomplete debugging information than to crash. */
17992 return NULL;
17995 if (!ret && !list_ret)
17996 return 0;
17998 if (want_address == 2 && !have_address
17999 && (dwarf_version >= 4 || !dwarf_strict))
18001 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18003 expansion_failed (loc, NULL_RTX,
18004 "DWARF address size mismatch");
18005 return 0;
18007 if (ret)
18008 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18009 else
18010 add_loc_descr_to_each (list_ret,
18011 new_loc_descr (DW_OP_stack_value, 0, 0));
18012 have_address = 1;
18014 /* Show if we can't fill the request for an address. */
18015 if (want_address && !have_address)
18017 expansion_failed (loc, NULL_RTX,
18018 "Want address and only have value");
18019 return 0;
18022 gcc_assert (!ret || !list_ret);
18024 /* If we've got an address and don't want one, dereference. */
18025 if (!want_address && have_address)
18027 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18029 if (size > DWARF2_ADDR_SIZE || size == -1)
18031 expansion_failed (loc, NULL_RTX,
18032 "DWARF address size mismatch");
18033 return 0;
18035 else if (size == DWARF2_ADDR_SIZE)
18036 op = DW_OP_deref;
18037 else
18038 op = DW_OP_deref_size;
18040 if (ret)
18041 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18042 else
18043 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18045 if (ret)
18046 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18048 return list_ret;
18051 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18052 expressions. */
18054 static dw_loc_list_ref
18055 loc_list_from_tree (tree loc, int want_address,
18056 struct loc_descr_context *context)
18058 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18060 for (dw_loc_list_ref loc_cur = result;
18061 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18062 loc_descr_without_nops (loc_cur->expr);
18063 return result;
18066 /* Same as above but return only single location expression. */
18067 static dw_loc_descr_ref
18068 loc_descriptor_from_tree (tree loc, int want_address,
18069 struct loc_descr_context *context)
18071 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18072 if (!ret)
18073 return NULL;
18074 if (ret->dw_loc_next)
18076 expansion_failed (loc, NULL_RTX,
18077 "Location list where only loc descriptor needed");
18078 return NULL;
18080 return ret->expr;
18083 /* Given a value, round it up to the lowest multiple of `boundary'
18084 which is not less than the value itself. */
18086 static inline HOST_WIDE_INT
18087 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18089 return (((value + boundary - 1) / boundary) * boundary);
18092 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18093 pointer to the declared type for the relevant field variable, or return
18094 `integer_type_node' if the given node turns out to be an
18095 ERROR_MARK node. */
18097 static inline tree
18098 field_type (const_tree decl)
18100 tree type;
18102 if (TREE_CODE (decl) == ERROR_MARK)
18103 return integer_type_node;
18105 type = DECL_BIT_FIELD_TYPE (decl);
18106 if (type == NULL_TREE)
18107 type = TREE_TYPE (decl);
18109 return type;
18112 /* Given a pointer to a tree node, return the alignment in bits for
18113 it, or else return BITS_PER_WORD if the node actually turns out to
18114 be an ERROR_MARK node. */
18116 static inline unsigned
18117 simple_type_align_in_bits (const_tree type)
18119 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18122 static inline unsigned
18123 simple_decl_align_in_bits (const_tree decl)
18125 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18128 /* Return the result of rounding T up to ALIGN. */
18130 static inline offset_int
18131 round_up_to_align (const offset_int &t, unsigned int align)
18133 return wi::udiv_trunc (t + align - 1, align) * align;
18136 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18137 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18138 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18139 if we fail to return the size in one of these two forms. */
18141 static dw_loc_descr_ref
18142 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18144 tree tree_size;
18145 struct loc_descr_context ctx;
18147 /* Return a constant integer in priority, if possible. */
18148 *cst_size = int_size_in_bytes (type);
18149 if (*cst_size != -1)
18150 return NULL;
18152 ctx.context_type = const_cast<tree> (type);
18153 ctx.base_decl = NULL_TREE;
18154 ctx.dpi = NULL;
18155 ctx.placeholder_arg = false;
18156 ctx.placeholder_seen = false;
18158 type = TYPE_MAIN_VARIANT (type);
18159 tree_size = TYPE_SIZE_UNIT (type);
18160 return ((tree_size != NULL_TREE)
18161 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18162 : NULL);
18165 /* Helper structure for RECORD_TYPE processing. */
18166 struct vlr_context
18168 /* Root RECORD_TYPE. It is needed to generate data member location
18169 descriptions in variable-length records (VLR), but also to cope with
18170 variants, which are composed of nested structures multiplexed with
18171 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18172 function processing a FIELD_DECL, it is required to be non null. */
18173 tree struct_type;
18174 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18175 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18176 this variant part as part of the root record (in storage units). For
18177 regular records, it must be NULL_TREE. */
18178 tree variant_part_offset;
18181 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18182 addressed byte of the "containing object" for the given FIELD_DECL. If
18183 possible, return a native constant through CST_OFFSET (in which case NULL is
18184 returned); otherwise return a DWARF expression that computes the offset.
18186 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18187 that offset is, either because the argument turns out to be a pointer to an
18188 ERROR_MARK node, or because the offset expression is too complex for us.
18190 CTX is required: see the comment for VLR_CONTEXT. */
18192 static dw_loc_descr_ref
18193 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18194 HOST_WIDE_INT *cst_offset)
18196 tree tree_result;
18197 dw_loc_list_ref loc_result;
18199 *cst_offset = 0;
18201 if (TREE_CODE (decl) == ERROR_MARK)
18202 return NULL;
18203 else
18204 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18206 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18207 case. */
18208 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18209 return NULL;
18211 #ifdef PCC_BITFIELD_TYPE_MATTERS
18212 /* We used to handle only constant offsets in all cases. Now, we handle
18213 properly dynamic byte offsets only when PCC bitfield type doesn't
18214 matter. */
18215 if (PCC_BITFIELD_TYPE_MATTERS
18216 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18218 offset_int object_offset_in_bits;
18219 offset_int object_offset_in_bytes;
18220 offset_int bitpos_int;
18221 tree type;
18222 tree field_size_tree;
18223 offset_int deepest_bitpos;
18224 offset_int field_size_in_bits;
18225 unsigned int type_align_in_bits;
18226 unsigned int decl_align_in_bits;
18227 offset_int type_size_in_bits;
18229 bitpos_int = wi::to_offset (bit_position (decl));
18230 type = field_type (decl);
18231 type_size_in_bits = offset_int_type_size_in_bits (type);
18232 type_align_in_bits = simple_type_align_in_bits (type);
18234 field_size_tree = DECL_SIZE (decl);
18236 /* The size could be unspecified if there was an error, or for
18237 a flexible array member. */
18238 if (!field_size_tree)
18239 field_size_tree = bitsize_zero_node;
18241 /* If the size of the field is not constant, use the type size. */
18242 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18243 field_size_in_bits = wi::to_offset (field_size_tree);
18244 else
18245 field_size_in_bits = type_size_in_bits;
18247 decl_align_in_bits = simple_decl_align_in_bits (decl);
18249 /* The GCC front-end doesn't make any attempt to keep track of the
18250 starting bit offset (relative to the start of the containing
18251 structure type) of the hypothetical "containing object" for a
18252 bit-field. Thus, when computing the byte offset value for the
18253 start of the "containing object" of a bit-field, we must deduce
18254 this information on our own. This can be rather tricky to do in
18255 some cases. For example, handling the following structure type
18256 definition when compiling for an i386/i486 target (which only
18257 aligns long long's to 32-bit boundaries) can be very tricky:
18259 struct S { int field1; long long field2:31; };
18261 Fortunately, there is a simple rule-of-thumb which can be used
18262 in such cases. When compiling for an i386/i486, GCC will
18263 allocate 8 bytes for the structure shown above. It decides to
18264 do this based upon one simple rule for bit-field allocation.
18265 GCC allocates each "containing object" for each bit-field at
18266 the first (i.e. lowest addressed) legitimate alignment boundary
18267 (based upon the required minimum alignment for the declared
18268 type of the field) which it can possibly use, subject to the
18269 condition that there is still enough available space remaining
18270 in the containing object (when allocated at the selected point)
18271 to fully accommodate all of the bits of the bit-field itself.
18273 This simple rule makes it obvious why GCC allocates 8 bytes for
18274 each object of the structure type shown above. When looking
18275 for a place to allocate the "containing object" for `field2',
18276 the compiler simply tries to allocate a 64-bit "containing
18277 object" at each successive 32-bit boundary (starting at zero)
18278 until it finds a place to allocate that 64- bit field such that
18279 at least 31 contiguous (and previously unallocated) bits remain
18280 within that selected 64 bit field. (As it turns out, for the
18281 example above, the compiler finds it is OK to allocate the
18282 "containing object" 64-bit field at bit-offset zero within the
18283 structure type.)
18285 Here we attempt to work backwards from the limited set of facts
18286 we're given, and we try to deduce from those facts, where GCC
18287 must have believed that the containing object started (within
18288 the structure type). The value we deduce is then used (by the
18289 callers of this routine) to generate DW_AT_location and
18290 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18291 the case of DW_AT_location, regular fields as well). */
18293 /* Figure out the bit-distance from the start of the structure to
18294 the "deepest" bit of the bit-field. */
18295 deepest_bitpos = bitpos_int + field_size_in_bits;
18297 /* This is the tricky part. Use some fancy footwork to deduce
18298 where the lowest addressed bit of the containing object must
18299 be. */
18300 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18302 /* Round up to type_align by default. This works best for
18303 bitfields. */
18304 object_offset_in_bits
18305 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18307 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18309 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18311 /* Round up to decl_align instead. */
18312 object_offset_in_bits
18313 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18316 object_offset_in_bytes
18317 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18318 if (ctx->variant_part_offset == NULL_TREE)
18320 *cst_offset = object_offset_in_bytes.to_shwi ();
18321 return NULL;
18323 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18325 else
18326 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18327 tree_result = byte_position (decl);
18329 if (ctx->variant_part_offset != NULL_TREE)
18330 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18331 ctx->variant_part_offset, tree_result);
18333 /* If the byte offset is a constant, it's simplier to handle a native
18334 constant rather than a DWARF expression. */
18335 if (TREE_CODE (tree_result) == INTEGER_CST)
18337 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18338 return NULL;
18340 struct loc_descr_context loc_ctx = {
18341 ctx->struct_type, /* context_type */
18342 NULL_TREE, /* base_decl */
18343 NULL, /* dpi */
18344 false, /* placeholder_arg */
18345 false /* placeholder_seen */
18347 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18349 /* We want a DWARF expression: abort if we only have a location list with
18350 multiple elements. */
18351 if (!loc_result || !single_element_loc_list_p (loc_result))
18352 return NULL;
18353 else
18354 return loc_result->expr;
18357 /* The following routines define various Dwarf attributes and any data
18358 associated with them. */
18360 /* Add a location description attribute value to a DIE.
18362 This emits location attributes suitable for whole variables and
18363 whole parameters. Note that the location attributes for struct fields are
18364 generated by the routine `data_member_location_attribute' below. */
18366 static inline void
18367 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18368 dw_loc_list_ref descr)
18370 if (descr == 0)
18371 return;
18372 if (single_element_loc_list_p (descr))
18373 add_AT_loc (die, attr_kind, descr->expr);
18374 else
18375 add_AT_loc_list (die, attr_kind, descr);
18378 /* Add DW_AT_accessibility attribute to DIE if needed. */
18380 static void
18381 add_accessibility_attribute (dw_die_ref die, tree decl)
18383 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18384 children, otherwise the default is DW_ACCESS_public. In DWARF2
18385 the default has always been DW_ACCESS_public. */
18386 if (TREE_PROTECTED (decl))
18387 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18388 else if (TREE_PRIVATE (decl))
18390 if (dwarf_version == 2
18391 || die->die_parent == NULL
18392 || die->die_parent->die_tag != DW_TAG_class_type)
18393 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18395 else if (dwarf_version > 2
18396 && die->die_parent
18397 && die->die_parent->die_tag == DW_TAG_class_type)
18398 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18401 /* Attach the specialized form of location attribute used for data members of
18402 struct and union types. In the special case of a FIELD_DECL node which
18403 represents a bit-field, the "offset" part of this special location
18404 descriptor must indicate the distance in bytes from the lowest-addressed
18405 byte of the containing struct or union type to the lowest-addressed byte of
18406 the "containing object" for the bit-field. (See the `field_byte_offset'
18407 function above).
18409 For any given bit-field, the "containing object" is a hypothetical object
18410 (of some integral or enum type) within which the given bit-field lives. The
18411 type of this hypothetical "containing object" is always the same as the
18412 declared type of the individual bit-field itself (for GCC anyway... the
18413 DWARF spec doesn't actually mandate this). Note that it is the size (in
18414 bytes) of the hypothetical "containing object" which will be given in the
18415 DW_AT_byte_size attribute for this bit-field. (See the
18416 `byte_size_attribute' function below.) It is also used when calculating the
18417 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18418 function below.)
18420 CTX is required: see the comment for VLR_CONTEXT. */
18422 static void
18423 add_data_member_location_attribute (dw_die_ref die,
18424 tree decl,
18425 struct vlr_context *ctx)
18427 HOST_WIDE_INT offset;
18428 dw_loc_descr_ref loc_descr = 0;
18430 if (TREE_CODE (decl) == TREE_BINFO)
18432 /* We're working on the TAG_inheritance for a base class. */
18433 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18435 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18436 aren't at a fixed offset from all (sub)objects of the same
18437 type. We need to extract the appropriate offset from our
18438 vtable. The following dwarf expression means
18440 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18442 This is specific to the V3 ABI, of course. */
18444 dw_loc_descr_ref tmp;
18446 /* Make a copy of the object address. */
18447 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18448 add_loc_descr (&loc_descr, tmp);
18450 /* Extract the vtable address. */
18451 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18452 add_loc_descr (&loc_descr, tmp);
18454 /* Calculate the address of the offset. */
18455 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18456 gcc_assert (offset < 0);
18458 tmp = int_loc_descriptor (-offset);
18459 add_loc_descr (&loc_descr, tmp);
18460 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18461 add_loc_descr (&loc_descr, tmp);
18463 /* Extract the offset. */
18464 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18465 add_loc_descr (&loc_descr, tmp);
18467 /* Add it to the object address. */
18468 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18469 add_loc_descr (&loc_descr, tmp);
18471 else
18472 offset = tree_to_shwi (BINFO_OFFSET (decl));
18474 else
18476 loc_descr = field_byte_offset (decl, ctx, &offset);
18478 /* If loc_descr is available then we know the field offset is dynamic.
18479 However, GDB does not handle dynamic field offsets very well at the
18480 moment. */
18481 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18483 loc_descr = NULL;
18484 offset = 0;
18487 /* Data member location evalutation starts with the base address on the
18488 stack. Compute the field offset and add it to this base address. */
18489 else if (loc_descr != NULL)
18490 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18493 if (! loc_descr)
18495 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18496 e.g. GDB only added support to it in November 2016. For DWARF5
18497 we need newer debug info consumers anyway. We might change this
18498 to dwarf_version >= 4 once most consumers catched up. */
18499 if (dwarf_version >= 5
18500 && TREE_CODE (decl) == FIELD_DECL
18501 && DECL_BIT_FIELD_TYPE (decl))
18503 tree off = bit_position (decl);
18504 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18506 remove_AT (die, DW_AT_byte_size);
18507 remove_AT (die, DW_AT_bit_offset);
18508 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18509 return;
18512 if (dwarf_version > 2)
18514 /* Don't need to output a location expression, just the constant. */
18515 if (offset < 0)
18516 add_AT_int (die, DW_AT_data_member_location, offset);
18517 else
18518 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18519 return;
18521 else
18523 enum dwarf_location_atom op;
18525 /* The DWARF2 standard says that we should assume that the structure
18526 address is already on the stack, so we can specify a structure
18527 field address by using DW_OP_plus_uconst. */
18528 op = DW_OP_plus_uconst;
18529 loc_descr = new_loc_descr (op, offset, 0);
18533 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18536 /* Writes integer values to dw_vec_const array. */
18538 static void
18539 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18541 while (size != 0)
18543 *dest++ = val & 0xff;
18544 val >>= 8;
18545 --size;
18549 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18551 static HOST_WIDE_INT
18552 extract_int (const unsigned char *src, unsigned int size)
18554 HOST_WIDE_INT val = 0;
18556 src += size;
18557 while (size != 0)
18559 val <<= 8;
18560 val |= *--src & 0xff;
18561 --size;
18563 return val;
18566 /* Writes wide_int values to dw_vec_const array. */
18568 static void
18569 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18571 int i;
18573 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18575 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18576 return;
18579 /* We'd have to extend this code to support odd sizes. */
18580 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18582 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18584 if (WORDS_BIG_ENDIAN)
18585 for (i = n - 1; i >= 0; i--)
18587 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18588 dest += sizeof (HOST_WIDE_INT);
18590 else
18591 for (i = 0; i < n; i++)
18593 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18594 dest += sizeof (HOST_WIDE_INT);
18598 /* Writes floating point values to dw_vec_const array. */
18600 static void
18601 insert_float (const_rtx rtl, unsigned char *array)
18603 long val[4];
18604 int i;
18605 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18607 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18609 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18610 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18612 insert_int (val[i], 4, array);
18613 array += 4;
18617 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18618 does not have a "location" either in memory or in a register. These
18619 things can arise in GNU C when a constant is passed as an actual parameter
18620 to an inlined function. They can also arise in C++ where declared
18621 constants do not necessarily get memory "homes". */
18623 static bool
18624 add_const_value_attribute (dw_die_ref die, rtx rtl)
18626 switch (GET_CODE (rtl))
18628 case CONST_INT:
18630 HOST_WIDE_INT val = INTVAL (rtl);
18632 if (val < 0)
18633 add_AT_int (die, DW_AT_const_value, val);
18634 else
18635 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18637 return true;
18639 case CONST_WIDE_INT:
18641 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18642 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18643 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18644 wide_int w = wi::zext (w1, prec);
18645 add_AT_wide (die, DW_AT_const_value, w);
18647 return true;
18649 case CONST_DOUBLE:
18650 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18651 floating-point constant. A CONST_DOUBLE is used whenever the
18652 constant requires more than one word in order to be adequately
18653 represented. */
18654 if (TARGET_SUPPORTS_WIDE_INT == 0
18655 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18656 add_AT_double (die, DW_AT_const_value,
18657 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18658 else
18660 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18661 unsigned int length = GET_MODE_SIZE (mode);
18662 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18664 insert_float (rtl, array);
18665 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18667 return true;
18669 case CONST_VECTOR:
18671 machine_mode mode = GET_MODE (rtl);
18672 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18673 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18674 unsigned char *array
18675 = ggc_vec_alloc<unsigned char> (length * elt_size);
18676 unsigned int i;
18677 unsigned char *p;
18678 machine_mode imode = GET_MODE_INNER (mode);
18680 switch (GET_MODE_CLASS (mode))
18682 case MODE_VECTOR_INT:
18683 for (i = 0, p = array; i < length; i++, p += elt_size)
18685 rtx elt = CONST_VECTOR_ELT (rtl, i);
18686 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18688 break;
18690 case MODE_VECTOR_FLOAT:
18691 for (i = 0, p = array; i < length; i++, p += elt_size)
18693 rtx elt = CONST_VECTOR_ELT (rtl, i);
18694 insert_float (elt, p);
18696 break;
18698 default:
18699 gcc_unreachable ();
18702 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18704 return true;
18706 case CONST_STRING:
18707 if (dwarf_version >= 4 || !dwarf_strict)
18709 dw_loc_descr_ref loc_result;
18710 resolve_one_addr (&rtl);
18711 rtl_addr:
18712 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18713 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18714 add_AT_loc (die, DW_AT_location, loc_result);
18715 vec_safe_push (used_rtx_array, rtl);
18716 return true;
18718 return false;
18720 case CONST:
18721 if (CONSTANT_P (XEXP (rtl, 0)))
18722 return add_const_value_attribute (die, XEXP (rtl, 0));
18723 /* FALLTHROUGH */
18724 case SYMBOL_REF:
18725 if (!const_ok_for_output (rtl))
18726 return false;
18727 /* FALLTHROUGH */
18728 case LABEL_REF:
18729 if (dwarf_version >= 4 || !dwarf_strict)
18730 goto rtl_addr;
18731 return false;
18733 case PLUS:
18734 /* In cases where an inlined instance of an inline function is passed
18735 the address of an `auto' variable (which is local to the caller) we
18736 can get a situation where the DECL_RTL of the artificial local
18737 variable (for the inlining) which acts as a stand-in for the
18738 corresponding formal parameter (of the inline function) will look
18739 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18740 exactly a compile-time constant expression, but it isn't the address
18741 of the (artificial) local variable either. Rather, it represents the
18742 *value* which the artificial local variable always has during its
18743 lifetime. We currently have no way to represent such quasi-constant
18744 values in Dwarf, so for now we just punt and generate nothing. */
18745 return false;
18747 case HIGH:
18748 case CONST_FIXED:
18749 return false;
18751 case MEM:
18752 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18753 && MEM_READONLY_P (rtl)
18754 && GET_MODE (rtl) == BLKmode)
18756 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18757 return true;
18759 return false;
18761 default:
18762 /* No other kinds of rtx should be possible here. */
18763 gcc_unreachable ();
18765 return false;
18768 /* Determine whether the evaluation of EXPR references any variables
18769 or functions which aren't otherwise used (and therefore may not be
18770 output). */
18771 static tree
18772 reference_to_unused (tree * tp, int * walk_subtrees,
18773 void * data ATTRIBUTE_UNUSED)
18775 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18776 *walk_subtrees = 0;
18778 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18779 && ! TREE_ASM_WRITTEN (*tp))
18780 return *tp;
18781 /* ??? The C++ FE emits debug information for using decls, so
18782 putting gcc_unreachable here falls over. See PR31899. For now
18783 be conservative. */
18784 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18785 return *tp;
18786 else if (VAR_P (*tp))
18788 varpool_node *node = varpool_node::get (*tp);
18789 if (!node || !node->definition)
18790 return *tp;
18792 else if (TREE_CODE (*tp) == FUNCTION_DECL
18793 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18795 /* The call graph machinery must have finished analyzing,
18796 optimizing and gimplifying the CU by now.
18797 So if *TP has no call graph node associated
18798 to it, it means *TP will not be emitted. */
18799 if (!cgraph_node::get (*tp))
18800 return *tp;
18802 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18803 return *tp;
18805 return NULL_TREE;
18808 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18809 for use in a later add_const_value_attribute call. */
18811 static rtx
18812 rtl_for_decl_init (tree init, tree type)
18814 rtx rtl = NULL_RTX;
18816 STRIP_NOPS (init);
18818 /* If a variable is initialized with a string constant without embedded
18819 zeros, build CONST_STRING. */
18820 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18822 tree enttype = TREE_TYPE (type);
18823 tree domain = TYPE_DOMAIN (type);
18824 scalar_int_mode mode;
18826 if (is_int_mode (TYPE_MODE (enttype), &mode)
18827 && GET_MODE_SIZE (mode) == 1
18828 && domain
18829 && integer_zerop (TYPE_MIN_VALUE (domain))
18830 && compare_tree_int (TYPE_MAX_VALUE (domain),
18831 TREE_STRING_LENGTH (init) - 1) == 0
18832 && ((size_t) TREE_STRING_LENGTH (init)
18833 == strlen (TREE_STRING_POINTER (init)) + 1))
18835 rtl = gen_rtx_CONST_STRING (VOIDmode,
18836 ggc_strdup (TREE_STRING_POINTER (init)));
18837 rtl = gen_rtx_MEM (BLKmode, rtl);
18838 MEM_READONLY_P (rtl) = 1;
18841 /* Other aggregates, and complex values, could be represented using
18842 CONCAT: FIXME! */
18843 else if (AGGREGATE_TYPE_P (type)
18844 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18845 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18846 || TREE_CODE (type) == COMPLEX_TYPE)
18848 /* Vectors only work if their mode is supported by the target.
18849 FIXME: generic vectors ought to work too. */
18850 else if (TREE_CODE (type) == VECTOR_TYPE
18851 && !VECTOR_MODE_P (TYPE_MODE (type)))
18853 /* If the initializer is something that we know will expand into an
18854 immediate RTL constant, expand it now. We must be careful not to
18855 reference variables which won't be output. */
18856 else if (initializer_constant_valid_p (init, type)
18857 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18859 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18860 possible. */
18861 if (TREE_CODE (type) == VECTOR_TYPE)
18862 switch (TREE_CODE (init))
18864 case VECTOR_CST:
18865 break;
18866 case CONSTRUCTOR:
18867 if (TREE_CONSTANT (init))
18869 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18870 bool constant_p = true;
18871 tree value;
18872 unsigned HOST_WIDE_INT ix;
18874 /* Even when ctor is constant, it might contain non-*_CST
18875 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18876 belong into VECTOR_CST nodes. */
18877 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18878 if (!CONSTANT_CLASS_P (value))
18880 constant_p = false;
18881 break;
18884 if (constant_p)
18886 init = build_vector_from_ctor (type, elts);
18887 break;
18890 /* FALLTHRU */
18892 default:
18893 return NULL;
18896 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18898 /* If expand_expr returns a MEM, it wasn't immediate. */
18899 gcc_assert (!rtl || !MEM_P (rtl));
18902 return rtl;
18905 /* Generate RTL for the variable DECL to represent its location. */
18907 static rtx
18908 rtl_for_decl_location (tree decl)
18910 rtx rtl;
18912 /* Here we have to decide where we are going to say the parameter "lives"
18913 (as far as the debugger is concerned). We only have a couple of
18914 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18916 DECL_RTL normally indicates where the parameter lives during most of the
18917 activation of the function. If optimization is enabled however, this
18918 could be either NULL or else a pseudo-reg. Both of those cases indicate
18919 that the parameter doesn't really live anywhere (as far as the code
18920 generation parts of GCC are concerned) during most of the function's
18921 activation. That will happen (for example) if the parameter is never
18922 referenced within the function.
18924 We could just generate a location descriptor here for all non-NULL
18925 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18926 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18927 where DECL_RTL is NULL or is a pseudo-reg.
18929 Note however that we can only get away with using DECL_INCOMING_RTL as
18930 a backup substitute for DECL_RTL in certain limited cases. In cases
18931 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18932 we can be sure that the parameter was passed using the same type as it is
18933 declared to have within the function, and that its DECL_INCOMING_RTL
18934 points us to a place where a value of that type is passed.
18936 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18937 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18938 because in these cases DECL_INCOMING_RTL points us to a value of some
18939 type which is *different* from the type of the parameter itself. Thus,
18940 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18941 such cases, the debugger would end up (for example) trying to fetch a
18942 `float' from a place which actually contains the first part of a
18943 `double'. That would lead to really incorrect and confusing
18944 output at debug-time.
18946 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18947 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18948 are a couple of exceptions however. On little-endian machines we can
18949 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18950 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18951 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18952 when (on a little-endian machine) a non-prototyped function has a
18953 parameter declared to be of type `short' or `char'. In such cases,
18954 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18955 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18956 passed `int' value. If the debugger then uses that address to fetch
18957 a `short' or a `char' (on a little-endian machine) the result will be
18958 the correct data, so we allow for such exceptional cases below.
18960 Note that our goal here is to describe the place where the given formal
18961 parameter lives during most of the function's activation (i.e. between the
18962 end of the prologue and the start of the epilogue). We'll do that as best
18963 as we can. Note however that if the given formal parameter is modified
18964 sometime during the execution of the function, then a stack backtrace (at
18965 debug-time) will show the function as having been called with the *new*
18966 value rather than the value which was originally passed in. This happens
18967 rarely enough that it is not a major problem, but it *is* a problem, and
18968 I'd like to fix it.
18970 A future version of dwarf2out.c may generate two additional attributes for
18971 any given DW_TAG_formal_parameter DIE which will describe the "passed
18972 type" and the "passed location" for the given formal parameter in addition
18973 to the attributes we now generate to indicate the "declared type" and the
18974 "active location" for each parameter. This additional set of attributes
18975 could be used by debuggers for stack backtraces. Separately, note that
18976 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18977 This happens (for example) for inlined-instances of inline function formal
18978 parameters which are never referenced. This really shouldn't be
18979 happening. All PARM_DECL nodes should get valid non-NULL
18980 DECL_INCOMING_RTL values. FIXME. */
18982 /* Use DECL_RTL as the "location" unless we find something better. */
18983 rtl = DECL_RTL_IF_SET (decl);
18985 /* When generating abstract instances, ignore everything except
18986 constants, symbols living in memory, and symbols living in
18987 fixed registers. */
18988 if (! reload_completed)
18990 if (rtl
18991 && (CONSTANT_P (rtl)
18992 || (MEM_P (rtl)
18993 && CONSTANT_P (XEXP (rtl, 0)))
18994 || (REG_P (rtl)
18995 && VAR_P (decl)
18996 && TREE_STATIC (decl))))
18998 rtl = targetm.delegitimize_address (rtl);
18999 return rtl;
19001 rtl = NULL_RTX;
19003 else if (TREE_CODE (decl) == PARM_DECL)
19005 if (rtl == NULL_RTX
19006 || is_pseudo_reg (rtl)
19007 || (MEM_P (rtl)
19008 && is_pseudo_reg (XEXP (rtl, 0))
19009 && DECL_INCOMING_RTL (decl)
19010 && MEM_P (DECL_INCOMING_RTL (decl))
19011 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19013 tree declared_type = TREE_TYPE (decl);
19014 tree passed_type = DECL_ARG_TYPE (decl);
19015 machine_mode dmode = TYPE_MODE (declared_type);
19016 machine_mode pmode = TYPE_MODE (passed_type);
19018 /* This decl represents a formal parameter which was optimized out.
19019 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19020 all cases where (rtl == NULL_RTX) just below. */
19021 if (dmode == pmode)
19022 rtl = DECL_INCOMING_RTL (decl);
19023 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19024 && SCALAR_INT_MODE_P (dmode)
19025 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19026 && DECL_INCOMING_RTL (decl))
19028 rtx inc = DECL_INCOMING_RTL (decl);
19029 if (REG_P (inc))
19030 rtl = inc;
19031 else if (MEM_P (inc))
19033 if (BYTES_BIG_ENDIAN)
19034 rtl = adjust_address_nv (inc, dmode,
19035 GET_MODE_SIZE (pmode)
19036 - GET_MODE_SIZE (dmode));
19037 else
19038 rtl = inc;
19043 /* If the parm was passed in registers, but lives on the stack, then
19044 make a big endian correction if the mode of the type of the
19045 parameter is not the same as the mode of the rtl. */
19046 /* ??? This is the same series of checks that are made in dbxout.c before
19047 we reach the big endian correction code there. It isn't clear if all
19048 of these checks are necessary here, but keeping them all is the safe
19049 thing to do. */
19050 else if (MEM_P (rtl)
19051 && XEXP (rtl, 0) != const0_rtx
19052 && ! CONSTANT_P (XEXP (rtl, 0))
19053 /* Not passed in memory. */
19054 && !MEM_P (DECL_INCOMING_RTL (decl))
19055 /* Not passed by invisible reference. */
19056 && (!REG_P (XEXP (rtl, 0))
19057 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19058 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19059 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19060 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19061 #endif
19063 /* Big endian correction check. */
19064 && BYTES_BIG_ENDIAN
19065 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19066 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19067 < UNITS_PER_WORD))
19069 machine_mode addr_mode = get_address_mode (rtl);
19070 int offset = (UNITS_PER_WORD
19071 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19073 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19074 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19077 else if (VAR_P (decl)
19078 && rtl
19079 && MEM_P (rtl)
19080 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19082 machine_mode addr_mode = get_address_mode (rtl);
19083 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19084 GET_MODE (rtl));
19086 /* If a variable is declared "register" yet is smaller than
19087 a register, then if we store the variable to memory, it
19088 looks like we're storing a register-sized value, when in
19089 fact we are not. We need to adjust the offset of the
19090 storage location to reflect the actual value's bytes,
19091 else gdb will not be able to display it. */
19092 if (offset != 0)
19093 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19094 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19097 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19098 and will have been substituted directly into all expressions that use it.
19099 C does not have such a concept, but C++ and other languages do. */
19100 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19101 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19103 if (rtl)
19104 rtl = targetm.delegitimize_address (rtl);
19106 /* If we don't look past the constant pool, we risk emitting a
19107 reference to a constant pool entry that isn't referenced from
19108 code, and thus is not emitted. */
19109 if (rtl)
19110 rtl = avoid_constant_pool_reference (rtl);
19112 /* Try harder to get a rtl. If this symbol ends up not being emitted
19113 in the current CU, resolve_addr will remove the expression referencing
19114 it. */
19115 if (rtl == NULL_RTX
19116 && VAR_P (decl)
19117 && !DECL_EXTERNAL (decl)
19118 && TREE_STATIC (decl)
19119 && DECL_NAME (decl)
19120 && !DECL_HARD_REGISTER (decl)
19121 && DECL_MODE (decl) != VOIDmode)
19123 rtl = make_decl_rtl_for_debug (decl);
19124 if (!MEM_P (rtl)
19125 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19126 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19127 rtl = NULL_RTX;
19130 return rtl;
19133 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19134 returned. If so, the decl for the COMMON block is returned, and the
19135 value is the offset into the common block for the symbol. */
19137 static tree
19138 fortran_common (tree decl, HOST_WIDE_INT *value)
19140 tree val_expr, cvar;
19141 machine_mode mode;
19142 HOST_WIDE_INT bitsize, bitpos;
19143 tree offset;
19144 int unsignedp, reversep, volatilep = 0;
19146 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19147 it does not have a value (the offset into the common area), or if it
19148 is thread local (as opposed to global) then it isn't common, and shouldn't
19149 be handled as such. */
19150 if (!VAR_P (decl)
19151 || !TREE_STATIC (decl)
19152 || !DECL_HAS_VALUE_EXPR_P (decl)
19153 || !is_fortran ())
19154 return NULL_TREE;
19156 val_expr = DECL_VALUE_EXPR (decl);
19157 if (TREE_CODE (val_expr) != COMPONENT_REF)
19158 return NULL_TREE;
19160 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19161 &unsignedp, &reversep, &volatilep);
19163 if (cvar == NULL_TREE
19164 || !VAR_P (cvar)
19165 || DECL_ARTIFICIAL (cvar)
19166 || !TREE_PUBLIC (cvar))
19167 return NULL_TREE;
19169 *value = 0;
19170 if (offset != NULL)
19172 if (!tree_fits_shwi_p (offset))
19173 return NULL_TREE;
19174 *value = tree_to_shwi (offset);
19176 if (bitpos != 0)
19177 *value += bitpos / BITS_PER_UNIT;
19179 return cvar;
19182 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19183 data attribute for a variable or a parameter. We generate the
19184 DW_AT_const_value attribute only in those cases where the given variable
19185 or parameter does not have a true "location" either in memory or in a
19186 register. This can happen (for example) when a constant is passed as an
19187 actual argument in a call to an inline function. (It's possible that
19188 these things can crop up in other ways also.) Note that one type of
19189 constant value which can be passed into an inlined function is a constant
19190 pointer. This can happen for example if an actual argument in an inlined
19191 function call evaluates to a compile-time constant address.
19193 CACHE_P is true if it is worth caching the location list for DECL,
19194 so that future calls can reuse it rather than regenerate it from scratch.
19195 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19196 since we will need to refer to them each time the function is inlined. */
19198 static bool
19199 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19201 rtx rtl;
19202 dw_loc_list_ref list;
19203 var_loc_list *loc_list;
19204 cached_dw_loc_list *cache;
19206 if (early_dwarf)
19207 return false;
19209 if (TREE_CODE (decl) == ERROR_MARK)
19210 return false;
19212 if (get_AT (die, DW_AT_location)
19213 || get_AT (die, DW_AT_const_value))
19214 return true;
19216 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19217 || TREE_CODE (decl) == RESULT_DECL);
19219 /* Try to get some constant RTL for this decl, and use that as the value of
19220 the location. */
19222 rtl = rtl_for_decl_location (decl);
19223 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19224 && add_const_value_attribute (die, rtl))
19225 return true;
19227 /* See if we have single element location list that is equivalent to
19228 a constant value. That way we are better to use add_const_value_attribute
19229 rather than expanding constant value equivalent. */
19230 loc_list = lookup_decl_loc (decl);
19231 if (loc_list
19232 && loc_list->first
19233 && loc_list->first->next == NULL
19234 && NOTE_P (loc_list->first->loc)
19235 && NOTE_VAR_LOCATION (loc_list->first->loc)
19236 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19238 struct var_loc_node *node;
19240 node = loc_list->first;
19241 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19242 if (GET_CODE (rtl) == EXPR_LIST)
19243 rtl = XEXP (rtl, 0);
19244 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19245 && add_const_value_attribute (die, rtl))
19246 return true;
19248 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19249 list several times. See if we've already cached the contents. */
19250 list = NULL;
19251 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19252 cache_p = false;
19253 if (cache_p)
19255 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19256 if (cache)
19257 list = cache->loc_list;
19259 if (list == NULL)
19261 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19262 NULL);
19263 /* It is usually worth caching this result if the decl is from
19264 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19265 if (cache_p && list && list->dw_loc_next)
19267 cached_dw_loc_list **slot
19268 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19269 DECL_UID (decl),
19270 INSERT);
19271 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19272 cache->decl_id = DECL_UID (decl);
19273 cache->loc_list = list;
19274 *slot = cache;
19277 if (list)
19279 add_AT_location_description (die, DW_AT_location, list);
19280 return true;
19282 /* None of that worked, so it must not really have a location;
19283 try adding a constant value attribute from the DECL_INITIAL. */
19284 return tree_add_const_value_attribute_for_decl (die, decl);
19287 /* Helper function for tree_add_const_value_attribute. Natively encode
19288 initializer INIT into an array. Return true if successful. */
19290 static bool
19291 native_encode_initializer (tree init, unsigned char *array, int size)
19293 tree type;
19295 if (init == NULL_TREE)
19296 return false;
19298 STRIP_NOPS (init);
19299 switch (TREE_CODE (init))
19301 case STRING_CST:
19302 type = TREE_TYPE (init);
19303 if (TREE_CODE (type) == ARRAY_TYPE)
19305 tree enttype = TREE_TYPE (type);
19306 scalar_int_mode mode;
19308 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19309 || GET_MODE_SIZE (mode) != 1)
19310 return false;
19311 if (int_size_in_bytes (type) != size)
19312 return false;
19313 if (size > TREE_STRING_LENGTH (init))
19315 memcpy (array, TREE_STRING_POINTER (init),
19316 TREE_STRING_LENGTH (init));
19317 memset (array + TREE_STRING_LENGTH (init),
19318 '\0', size - TREE_STRING_LENGTH (init));
19320 else
19321 memcpy (array, TREE_STRING_POINTER (init), size);
19322 return true;
19324 return false;
19325 case CONSTRUCTOR:
19326 type = TREE_TYPE (init);
19327 if (int_size_in_bytes (type) != size)
19328 return false;
19329 if (TREE_CODE (type) == ARRAY_TYPE)
19331 HOST_WIDE_INT min_index;
19332 unsigned HOST_WIDE_INT cnt;
19333 int curpos = 0, fieldsize;
19334 constructor_elt *ce;
19336 if (TYPE_DOMAIN (type) == NULL_TREE
19337 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19338 return false;
19340 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19341 if (fieldsize <= 0)
19342 return false;
19344 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19345 memset (array, '\0', size);
19346 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19348 tree val = ce->value;
19349 tree index = ce->index;
19350 int pos = curpos;
19351 if (index && TREE_CODE (index) == RANGE_EXPR)
19352 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19353 * fieldsize;
19354 else if (index)
19355 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19357 if (val)
19359 STRIP_NOPS (val);
19360 if (!native_encode_initializer (val, array + pos, fieldsize))
19361 return false;
19363 curpos = pos + fieldsize;
19364 if (index && TREE_CODE (index) == RANGE_EXPR)
19366 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19367 - tree_to_shwi (TREE_OPERAND (index, 0));
19368 while (count-- > 0)
19370 if (val)
19371 memcpy (array + curpos, array + pos, fieldsize);
19372 curpos += fieldsize;
19375 gcc_assert (curpos <= size);
19377 return true;
19379 else if (TREE_CODE (type) == RECORD_TYPE
19380 || TREE_CODE (type) == UNION_TYPE)
19382 tree field = NULL_TREE;
19383 unsigned HOST_WIDE_INT cnt;
19384 constructor_elt *ce;
19386 if (int_size_in_bytes (type) != size)
19387 return false;
19389 if (TREE_CODE (type) == RECORD_TYPE)
19390 field = TYPE_FIELDS (type);
19392 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19394 tree val = ce->value;
19395 int pos, fieldsize;
19397 if (ce->index != 0)
19398 field = ce->index;
19400 if (val)
19401 STRIP_NOPS (val);
19403 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19404 return false;
19406 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19407 && TYPE_DOMAIN (TREE_TYPE (field))
19408 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19409 return false;
19410 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19411 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19412 return false;
19413 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19414 pos = int_byte_position (field);
19415 gcc_assert (pos + fieldsize <= size);
19416 if (val && fieldsize != 0
19417 && !native_encode_initializer (val, array + pos, fieldsize))
19418 return false;
19420 return true;
19422 return false;
19423 case VIEW_CONVERT_EXPR:
19424 case NON_LVALUE_EXPR:
19425 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19426 default:
19427 return native_encode_expr (init, array, size) == size;
19431 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19432 attribute is the const value T. */
19434 static bool
19435 tree_add_const_value_attribute (dw_die_ref die, tree t)
19437 tree init;
19438 tree type = TREE_TYPE (t);
19439 rtx rtl;
19441 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19442 return false;
19444 init = t;
19445 gcc_assert (!DECL_P (init));
19447 if (TREE_CODE (init) == INTEGER_CST)
19449 if (tree_fits_uhwi_p (init))
19451 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19452 return true;
19454 if (tree_fits_shwi_p (init))
19456 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19457 return true;
19460 if (! early_dwarf)
19462 rtl = rtl_for_decl_init (init, type);
19463 if (rtl)
19464 return add_const_value_attribute (die, rtl);
19466 /* If the host and target are sane, try harder. */
19467 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19468 && initializer_constant_valid_p (init, type))
19470 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19471 if (size > 0 && (int) size == size)
19473 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19475 if (native_encode_initializer (init, array, size))
19477 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19478 return true;
19480 ggc_free (array);
19483 return false;
19486 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19487 attribute is the const value of T, where T is an integral constant
19488 variable with static storage duration
19489 (so it can't be a PARM_DECL or a RESULT_DECL). */
19491 static bool
19492 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19495 if (!decl
19496 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19497 || (VAR_P (decl) && !TREE_STATIC (decl)))
19498 return false;
19500 if (TREE_READONLY (decl)
19501 && ! TREE_THIS_VOLATILE (decl)
19502 && DECL_INITIAL (decl))
19503 /* OK */;
19504 else
19505 return false;
19507 /* Don't add DW_AT_const_value if abstract origin already has one. */
19508 if (get_AT (var_die, DW_AT_const_value))
19509 return false;
19511 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19514 /* Convert the CFI instructions for the current function into a
19515 location list. This is used for DW_AT_frame_base when we targeting
19516 a dwarf2 consumer that does not support the dwarf3
19517 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19518 expressions. */
19520 static dw_loc_list_ref
19521 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19523 int ix;
19524 dw_fde_ref fde;
19525 dw_loc_list_ref list, *list_tail;
19526 dw_cfi_ref cfi;
19527 dw_cfa_location last_cfa, next_cfa;
19528 const char *start_label, *last_label, *section;
19529 dw_cfa_location remember;
19531 fde = cfun->fde;
19532 gcc_assert (fde != NULL);
19534 section = secname_for_decl (current_function_decl);
19535 list_tail = &list;
19536 list = NULL;
19538 memset (&next_cfa, 0, sizeof (next_cfa));
19539 next_cfa.reg = INVALID_REGNUM;
19540 remember = next_cfa;
19542 start_label = fde->dw_fde_begin;
19544 /* ??? Bald assumption that the CIE opcode list does not contain
19545 advance opcodes. */
19546 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19547 lookup_cfa_1 (cfi, &next_cfa, &remember);
19549 last_cfa = next_cfa;
19550 last_label = start_label;
19552 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19554 /* If the first partition contained no CFI adjustments, the
19555 CIE opcodes apply to the whole first partition. */
19556 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19557 fde->dw_fde_begin, fde->dw_fde_end, section);
19558 list_tail =&(*list_tail)->dw_loc_next;
19559 start_label = last_label = fde->dw_fde_second_begin;
19562 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19564 switch (cfi->dw_cfi_opc)
19566 case DW_CFA_set_loc:
19567 case DW_CFA_advance_loc1:
19568 case DW_CFA_advance_loc2:
19569 case DW_CFA_advance_loc4:
19570 if (!cfa_equal_p (&last_cfa, &next_cfa))
19572 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19573 start_label, last_label, section);
19575 list_tail = &(*list_tail)->dw_loc_next;
19576 last_cfa = next_cfa;
19577 start_label = last_label;
19579 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19580 break;
19582 case DW_CFA_advance_loc:
19583 /* The encoding is complex enough that we should never emit this. */
19584 gcc_unreachable ();
19586 default:
19587 lookup_cfa_1 (cfi, &next_cfa, &remember);
19588 break;
19590 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19592 if (!cfa_equal_p (&last_cfa, &next_cfa))
19594 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19595 start_label, last_label, section);
19597 list_tail = &(*list_tail)->dw_loc_next;
19598 last_cfa = next_cfa;
19599 start_label = last_label;
19601 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19602 start_label, fde->dw_fde_end, section);
19603 list_tail = &(*list_tail)->dw_loc_next;
19604 start_label = last_label = fde->dw_fde_second_begin;
19608 if (!cfa_equal_p (&last_cfa, &next_cfa))
19610 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19611 start_label, last_label, section);
19612 list_tail = &(*list_tail)->dw_loc_next;
19613 start_label = last_label;
19616 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19617 start_label,
19618 fde->dw_fde_second_begin
19619 ? fde->dw_fde_second_end : fde->dw_fde_end,
19620 section);
19622 if (list && list->dw_loc_next)
19623 gen_llsym (list);
19625 return list;
19628 /* Compute a displacement from the "steady-state frame pointer" to the
19629 frame base (often the same as the CFA), and store it in
19630 frame_pointer_fb_offset. OFFSET is added to the displacement
19631 before the latter is negated. */
19633 static void
19634 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19636 rtx reg, elim;
19638 #ifdef FRAME_POINTER_CFA_OFFSET
19639 reg = frame_pointer_rtx;
19640 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19641 #else
19642 reg = arg_pointer_rtx;
19643 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19644 #endif
19646 elim = (ira_use_lra_p
19647 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19648 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19649 if (GET_CODE (elim) == PLUS)
19651 offset += INTVAL (XEXP (elim, 1));
19652 elim = XEXP (elim, 0);
19655 frame_pointer_fb_offset = -offset;
19657 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19658 in which to eliminate. This is because it's stack pointer isn't
19659 directly accessible as a register within the ISA. To work around
19660 this, assume that while we cannot provide a proper value for
19661 frame_pointer_fb_offset, we won't need one either. */
19662 frame_pointer_fb_offset_valid
19663 = ((SUPPORTS_STACK_ALIGNMENT
19664 && (elim == hard_frame_pointer_rtx
19665 || elim == stack_pointer_rtx))
19666 || elim == (frame_pointer_needed
19667 ? hard_frame_pointer_rtx
19668 : stack_pointer_rtx));
19671 /* Generate a DW_AT_name attribute given some string value to be included as
19672 the value of the attribute. */
19674 static void
19675 add_name_attribute (dw_die_ref die, const char *name_string)
19677 if (name_string != NULL && *name_string != 0)
19679 if (demangle_name_func)
19680 name_string = (*demangle_name_func) (name_string);
19682 add_AT_string (die, DW_AT_name, name_string);
19686 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19687 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19688 of TYPE accordingly.
19690 ??? This is a temporary measure until after we're able to generate
19691 regular DWARF for the complex Ada type system. */
19693 static void
19694 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19695 dw_die_ref context_die)
19697 tree dtype;
19698 dw_die_ref dtype_die;
19700 if (!lang_hooks.types.descriptive_type)
19701 return;
19703 dtype = lang_hooks.types.descriptive_type (type);
19704 if (!dtype)
19705 return;
19707 dtype_die = lookup_type_die (dtype);
19708 if (!dtype_die)
19710 gen_type_die (dtype, context_die);
19711 dtype_die = lookup_type_die (dtype);
19712 gcc_assert (dtype_die);
19715 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19718 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19720 static const char *
19721 comp_dir_string (void)
19723 const char *wd;
19724 char *wd1;
19725 static const char *cached_wd = NULL;
19727 if (cached_wd != NULL)
19728 return cached_wd;
19730 wd = get_src_pwd ();
19731 if (wd == NULL)
19732 return NULL;
19734 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19736 int wdlen;
19738 wdlen = strlen (wd);
19739 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19740 strcpy (wd1, wd);
19741 wd1 [wdlen] = DIR_SEPARATOR;
19742 wd1 [wdlen + 1] = 0;
19743 wd = wd1;
19746 cached_wd = remap_debug_filename (wd);
19747 return cached_wd;
19750 /* Generate a DW_AT_comp_dir attribute for DIE. */
19752 static void
19753 add_comp_dir_attribute (dw_die_ref die)
19755 const char * wd = comp_dir_string ();
19756 if (wd != NULL)
19757 add_AT_string (die, DW_AT_comp_dir, wd);
19760 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19761 pointer computation, ...), output a representation for that bound according
19762 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19763 loc_list_from_tree for the meaning of CONTEXT. */
19765 static void
19766 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19767 int forms, struct loc_descr_context *context)
19769 dw_die_ref context_die, decl_die;
19770 dw_loc_list_ref list;
19771 bool strip_conversions = true;
19772 bool placeholder_seen = false;
19774 while (strip_conversions)
19775 switch (TREE_CODE (value))
19777 case ERROR_MARK:
19778 case SAVE_EXPR:
19779 return;
19781 CASE_CONVERT:
19782 case VIEW_CONVERT_EXPR:
19783 value = TREE_OPERAND (value, 0);
19784 break;
19786 default:
19787 strip_conversions = false;
19788 break;
19791 /* If possible and permitted, output the attribute as a constant. */
19792 if ((forms & dw_scalar_form_constant) != 0
19793 && TREE_CODE (value) == INTEGER_CST)
19795 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19797 /* If HOST_WIDE_INT is big enough then represent the bound as
19798 a constant value. We need to choose a form based on
19799 whether the type is signed or unsigned. We cannot just
19800 call add_AT_unsigned if the value itself is positive
19801 (add_AT_unsigned might add the unsigned value encoded as
19802 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19803 bounds type and then sign extend any unsigned values found
19804 for signed types. This is needed only for
19805 DW_AT_{lower,upper}_bound, since for most other attributes,
19806 consumers will treat DW_FORM_data[1248] as unsigned values,
19807 regardless of the underlying type. */
19808 if (prec <= HOST_BITS_PER_WIDE_INT
19809 || tree_fits_uhwi_p (value))
19811 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19812 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19813 else
19814 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19816 else
19817 /* Otherwise represent the bound as an unsigned value with
19818 the precision of its type. The precision and signedness
19819 of the type will be necessary to re-interpret it
19820 unambiguously. */
19821 add_AT_wide (die, attr, wi::to_wide (value));
19822 return;
19825 /* Otherwise, if it's possible and permitted too, output a reference to
19826 another DIE. */
19827 if ((forms & dw_scalar_form_reference) != 0)
19829 tree decl = NULL_TREE;
19831 /* Some type attributes reference an outer type. For instance, the upper
19832 bound of an array may reference an embedding record (this happens in
19833 Ada). */
19834 if (TREE_CODE (value) == COMPONENT_REF
19835 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19836 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19837 decl = TREE_OPERAND (value, 1);
19839 else if (VAR_P (value)
19840 || TREE_CODE (value) == PARM_DECL
19841 || TREE_CODE (value) == RESULT_DECL)
19842 decl = value;
19844 if (decl != NULL_TREE)
19846 dw_die_ref decl_die = lookup_decl_die (decl);
19848 /* ??? Can this happen, or should the variable have been bound
19849 first? Probably it can, since I imagine that we try to create
19850 the types of parameters in the order in which they exist in
19851 the list, and won't have created a forward reference to a
19852 later parameter. */
19853 if (decl_die != NULL)
19855 add_AT_die_ref (die, attr, decl_die);
19856 return;
19861 /* Last chance: try to create a stack operation procedure to evaluate the
19862 value. Do nothing if even that is not possible or permitted. */
19863 if ((forms & dw_scalar_form_exprloc) == 0)
19864 return;
19866 list = loc_list_from_tree (value, 2, context);
19867 if (context && context->placeholder_arg)
19869 placeholder_seen = context->placeholder_seen;
19870 context->placeholder_seen = false;
19872 if (list == NULL || single_element_loc_list_p (list))
19874 /* If this attribute is not a reference nor constant, it is
19875 a DWARF expression rather than location description. For that
19876 loc_list_from_tree (value, 0, &context) is needed. */
19877 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19878 if (list2 && single_element_loc_list_p (list2))
19880 if (placeholder_seen)
19882 struct dwarf_procedure_info dpi;
19883 dpi.fndecl = NULL_TREE;
19884 dpi.args_count = 1;
19885 if (!resolve_args_picking (list2->expr, 1, &dpi))
19886 return;
19888 add_AT_loc (die, attr, list2->expr);
19889 return;
19893 /* If that failed to give a single element location list, fall back to
19894 outputting this as a reference... still if permitted. */
19895 if (list == NULL
19896 || (forms & dw_scalar_form_reference) == 0
19897 || placeholder_seen)
19898 return;
19900 if (current_function_decl == 0)
19901 context_die = comp_unit_die ();
19902 else
19903 context_die = lookup_decl_die (current_function_decl);
19905 decl_die = new_die (DW_TAG_variable, context_die, value);
19906 add_AT_flag (decl_die, DW_AT_artificial, 1);
19907 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19908 context_die);
19909 add_AT_location_description (decl_die, DW_AT_location, list);
19910 add_AT_die_ref (die, attr, decl_die);
19913 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19914 default. */
19916 static int
19917 lower_bound_default (void)
19919 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19921 case DW_LANG_C:
19922 case DW_LANG_C89:
19923 case DW_LANG_C99:
19924 case DW_LANG_C11:
19925 case DW_LANG_C_plus_plus:
19926 case DW_LANG_C_plus_plus_11:
19927 case DW_LANG_C_plus_plus_14:
19928 case DW_LANG_ObjC:
19929 case DW_LANG_ObjC_plus_plus:
19930 return 0;
19931 case DW_LANG_Fortran77:
19932 case DW_LANG_Fortran90:
19933 case DW_LANG_Fortran95:
19934 case DW_LANG_Fortran03:
19935 case DW_LANG_Fortran08:
19936 return 1;
19937 case DW_LANG_UPC:
19938 case DW_LANG_D:
19939 case DW_LANG_Python:
19940 return dwarf_version >= 4 ? 0 : -1;
19941 case DW_LANG_Ada95:
19942 case DW_LANG_Ada83:
19943 case DW_LANG_Cobol74:
19944 case DW_LANG_Cobol85:
19945 case DW_LANG_Modula2:
19946 case DW_LANG_PLI:
19947 return dwarf_version >= 4 ? 1 : -1;
19948 default:
19949 return -1;
19953 /* Given a tree node describing an array bound (either lower or upper) output
19954 a representation for that bound. */
19956 static void
19957 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19958 tree bound, struct loc_descr_context *context)
19960 int dflt;
19962 while (1)
19963 switch (TREE_CODE (bound))
19965 /* Strip all conversions. */
19966 CASE_CONVERT:
19967 case VIEW_CONVERT_EXPR:
19968 bound = TREE_OPERAND (bound, 0);
19969 break;
19971 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19972 are even omitted when they are the default. */
19973 case INTEGER_CST:
19974 /* If the value for this bound is the default one, we can even omit the
19975 attribute. */
19976 if (bound_attr == DW_AT_lower_bound
19977 && tree_fits_shwi_p (bound)
19978 && (dflt = lower_bound_default ()) != -1
19979 && tree_to_shwi (bound) == dflt)
19980 return;
19982 /* FALLTHRU */
19984 default:
19985 /* Because of the complex interaction there can be with other GNAT
19986 encodings, GDB isn't ready yet to handle proper DWARF description
19987 for self-referencial subrange bounds: let GNAT encodings do the
19988 magic in such a case. */
19989 if (is_ada ()
19990 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19991 && contains_placeholder_p (bound))
19992 return;
19994 add_scalar_info (subrange_die, bound_attr, bound,
19995 dw_scalar_form_constant
19996 | dw_scalar_form_exprloc
19997 | dw_scalar_form_reference,
19998 context);
19999 return;
20003 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20004 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20005 Note that the block of subscript information for an array type also
20006 includes information about the element type of the given array type.
20008 This function reuses previously set type and bound information if
20009 available. */
20011 static void
20012 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20014 unsigned dimension_number;
20015 tree lower, upper;
20016 dw_die_ref child = type_die->die_child;
20018 for (dimension_number = 0;
20019 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20020 type = TREE_TYPE (type), dimension_number++)
20022 tree domain = TYPE_DOMAIN (type);
20024 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20025 break;
20027 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20028 and (in GNU C only) variable bounds. Handle all three forms
20029 here. */
20031 /* Find and reuse a previously generated DW_TAG_subrange_type if
20032 available.
20034 For multi-dimensional arrays, as we iterate through the
20035 various dimensions in the enclosing for loop above, we also
20036 iterate through the DIE children and pick at each
20037 DW_TAG_subrange_type previously generated (if available).
20038 Each child DW_TAG_subrange_type DIE describes the range of
20039 the current dimension. At this point we should have as many
20040 DW_TAG_subrange_type's as we have dimensions in the
20041 array. */
20042 dw_die_ref subrange_die = NULL;
20043 if (child)
20044 while (1)
20046 child = child->die_sib;
20047 if (child->die_tag == DW_TAG_subrange_type)
20048 subrange_die = child;
20049 if (child == type_die->die_child)
20051 /* If we wrapped around, stop looking next time. */
20052 child = NULL;
20053 break;
20055 if (child->die_tag == DW_TAG_subrange_type)
20056 break;
20058 if (!subrange_die)
20059 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20061 if (domain)
20063 /* We have an array type with specified bounds. */
20064 lower = TYPE_MIN_VALUE (domain);
20065 upper = TYPE_MAX_VALUE (domain);
20067 /* Define the index type. */
20068 if (TREE_TYPE (domain)
20069 && !get_AT (subrange_die, DW_AT_type))
20071 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20072 TREE_TYPE field. We can't emit debug info for this
20073 because it is an unnamed integral type. */
20074 if (TREE_CODE (domain) == INTEGER_TYPE
20075 && TYPE_NAME (domain) == NULL_TREE
20076 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20077 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20079 else
20080 add_type_attribute (subrange_die, TREE_TYPE (domain),
20081 TYPE_UNQUALIFIED, false, type_die);
20084 /* ??? If upper is NULL, the array has unspecified length,
20085 but it does have a lower bound. This happens with Fortran
20086 dimension arr(N:*)
20087 Since the debugger is definitely going to need to know N
20088 to produce useful results, go ahead and output the lower
20089 bound solo, and hope the debugger can cope. */
20091 if (!get_AT (subrange_die, DW_AT_lower_bound))
20092 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20093 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20094 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20097 /* Otherwise we have an array type with an unspecified length. The
20098 DWARF-2 spec does not say how to handle this; let's just leave out the
20099 bounds. */
20103 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20105 static void
20106 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20108 dw_die_ref decl_die;
20109 HOST_WIDE_INT size;
20110 dw_loc_descr_ref size_expr = NULL;
20112 switch (TREE_CODE (tree_node))
20114 case ERROR_MARK:
20115 size = 0;
20116 break;
20117 case ENUMERAL_TYPE:
20118 case RECORD_TYPE:
20119 case UNION_TYPE:
20120 case QUAL_UNION_TYPE:
20121 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20122 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20124 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20125 return;
20127 size_expr = type_byte_size (tree_node, &size);
20128 break;
20129 case FIELD_DECL:
20130 /* For a data member of a struct or union, the DW_AT_byte_size is
20131 generally given as the number of bytes normally allocated for an
20132 object of the *declared* type of the member itself. This is true
20133 even for bit-fields. */
20134 size = int_size_in_bytes (field_type (tree_node));
20135 break;
20136 default:
20137 gcc_unreachable ();
20140 /* Support for dynamically-sized objects was introduced by DWARFv3.
20141 At the moment, GDB does not handle variable byte sizes very well,
20142 though. */
20143 if ((dwarf_version >= 3 || !dwarf_strict)
20144 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20145 && size_expr != NULL)
20146 add_AT_loc (die, DW_AT_byte_size, size_expr);
20148 /* Note that `size' might be -1 when we get to this point. If it is, that
20149 indicates that the byte size of the entity in question is variable and
20150 that we could not generate a DWARF expression that computes it. */
20151 if (size >= 0)
20152 add_AT_unsigned (die, DW_AT_byte_size, size);
20155 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20156 alignment. */
20158 static void
20159 add_alignment_attribute (dw_die_ref die, tree tree_node)
20161 if (dwarf_version < 5 && dwarf_strict)
20162 return;
20164 unsigned align;
20166 if (DECL_P (tree_node))
20168 if (!DECL_USER_ALIGN (tree_node))
20169 return;
20171 align = DECL_ALIGN_UNIT (tree_node);
20173 else if (TYPE_P (tree_node))
20175 if (!TYPE_USER_ALIGN (tree_node))
20176 return;
20178 align = TYPE_ALIGN_UNIT (tree_node);
20180 else
20181 gcc_unreachable ();
20183 add_AT_unsigned (die, DW_AT_alignment, align);
20186 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20187 which specifies the distance in bits from the highest order bit of the
20188 "containing object" for the bit-field to the highest order bit of the
20189 bit-field itself.
20191 For any given bit-field, the "containing object" is a hypothetical object
20192 (of some integral or enum type) within which the given bit-field lives. The
20193 type of this hypothetical "containing object" is always the same as the
20194 declared type of the individual bit-field itself. The determination of the
20195 exact location of the "containing object" for a bit-field is rather
20196 complicated. It's handled by the `field_byte_offset' function (above).
20198 CTX is required: see the comment for VLR_CONTEXT.
20200 Note that it is the size (in bytes) of the hypothetical "containing object"
20201 which will be given in the DW_AT_byte_size attribute for this bit-field.
20202 (See `byte_size_attribute' above). */
20204 static inline void
20205 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20207 HOST_WIDE_INT object_offset_in_bytes;
20208 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20209 HOST_WIDE_INT bitpos_int;
20210 HOST_WIDE_INT highest_order_object_bit_offset;
20211 HOST_WIDE_INT highest_order_field_bit_offset;
20212 HOST_WIDE_INT bit_offset;
20214 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20216 /* Must be a field and a bit field. */
20217 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20219 /* We can't yet handle bit-fields whose offsets are variable, so if we
20220 encounter such things, just return without generating any attribute
20221 whatsoever. Likewise for variable or too large size. */
20222 if (! tree_fits_shwi_p (bit_position (decl))
20223 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20224 return;
20226 bitpos_int = int_bit_position (decl);
20228 /* Note that the bit offset is always the distance (in bits) from the
20229 highest-order bit of the "containing object" to the highest-order bit of
20230 the bit-field itself. Since the "high-order end" of any object or field
20231 is different on big-endian and little-endian machines, the computation
20232 below must take account of these differences. */
20233 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20234 highest_order_field_bit_offset = bitpos_int;
20236 if (! BYTES_BIG_ENDIAN)
20238 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20239 highest_order_object_bit_offset +=
20240 simple_type_size_in_bits (original_type);
20243 bit_offset
20244 = (! BYTES_BIG_ENDIAN
20245 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20246 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20248 if (bit_offset < 0)
20249 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20250 else
20251 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20254 /* For a FIELD_DECL node which represents a bit field, output an attribute
20255 which specifies the length in bits of the given field. */
20257 static inline void
20258 add_bit_size_attribute (dw_die_ref die, tree decl)
20260 /* Must be a field and a bit field. */
20261 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20262 && DECL_BIT_FIELD_TYPE (decl));
20264 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20265 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20268 /* If the compiled language is ANSI C, then add a 'prototyped'
20269 attribute, if arg types are given for the parameters of a function. */
20271 static inline void
20272 add_prototyped_attribute (dw_die_ref die, tree func_type)
20274 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20276 case DW_LANG_C:
20277 case DW_LANG_C89:
20278 case DW_LANG_C99:
20279 case DW_LANG_C11:
20280 case DW_LANG_ObjC:
20281 if (prototype_p (func_type))
20282 add_AT_flag (die, DW_AT_prototyped, 1);
20283 break;
20284 default:
20285 break;
20289 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20290 by looking in the type declaration, the object declaration equate table or
20291 the block mapping. */
20293 static inline dw_die_ref
20294 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20296 dw_die_ref origin_die = NULL;
20298 if (DECL_P (origin))
20300 dw_die_ref c;
20301 origin_die = lookup_decl_die (origin);
20302 /* "Unwrap" the decls DIE which we put in the imported unit context.
20303 We are looking for the abstract copy here. */
20304 if (in_lto_p
20305 && origin_die
20306 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20307 /* ??? Identify this better. */
20308 && c->with_offset)
20309 origin_die = c;
20311 else if (TYPE_P (origin))
20312 origin_die = lookup_type_die (origin);
20313 else if (TREE_CODE (origin) == BLOCK)
20314 origin_die = BLOCK_DIE (origin);
20316 /* XXX: Functions that are never lowered don't always have correct block
20317 trees (in the case of java, they simply have no block tree, in some other
20318 languages). For these functions, there is nothing we can really do to
20319 output correct debug info for inlined functions in all cases. Rather
20320 than die, we'll just produce deficient debug info now, in that we will
20321 have variables without a proper abstract origin. In the future, when all
20322 functions are lowered, we should re-add a gcc_assert (origin_die)
20323 here. */
20325 if (origin_die)
20326 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20327 return origin_die;
20330 /* We do not currently support the pure_virtual attribute. */
20332 static inline void
20333 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20335 if (DECL_VINDEX (func_decl))
20337 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20339 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20340 add_AT_loc (die, DW_AT_vtable_elem_location,
20341 new_loc_descr (DW_OP_constu,
20342 tree_to_shwi (DECL_VINDEX (func_decl)),
20343 0));
20345 /* GNU extension: Record what type this method came from originally. */
20346 if (debug_info_level > DINFO_LEVEL_TERSE
20347 && DECL_CONTEXT (func_decl))
20348 add_AT_die_ref (die, DW_AT_containing_type,
20349 lookup_type_die (DECL_CONTEXT (func_decl)));
20353 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20354 given decl. This used to be a vendor extension until after DWARF 4
20355 standardized it. */
20357 static void
20358 add_linkage_attr (dw_die_ref die, tree decl)
20360 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20362 /* Mimic what assemble_name_raw does with a leading '*'. */
20363 if (name[0] == '*')
20364 name = &name[1];
20366 if (dwarf_version >= 4)
20367 add_AT_string (die, DW_AT_linkage_name, name);
20368 else
20369 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20372 /* Add source coordinate attributes for the given decl. */
20374 static void
20375 add_src_coords_attributes (dw_die_ref die, tree decl)
20377 expanded_location s;
20379 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20380 return;
20381 s = expand_location (DECL_SOURCE_LOCATION (decl));
20382 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20383 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20384 if (debug_column_info && s.column)
20385 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20388 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20390 static void
20391 add_linkage_name_raw (dw_die_ref die, tree decl)
20393 /* Defer until we have an assembler name set. */
20394 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20396 limbo_die_node *asm_name;
20398 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20399 asm_name->die = die;
20400 asm_name->created_for = decl;
20401 asm_name->next = deferred_asm_name;
20402 deferred_asm_name = asm_name;
20404 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20405 add_linkage_attr (die, decl);
20408 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20410 static void
20411 add_linkage_name (dw_die_ref die, tree decl)
20413 if (debug_info_level > DINFO_LEVEL_NONE
20414 && VAR_OR_FUNCTION_DECL_P (decl)
20415 && TREE_PUBLIC (decl)
20416 && !(VAR_P (decl) && DECL_REGISTER (decl))
20417 && die->die_tag != DW_TAG_member)
20418 add_linkage_name_raw (die, decl);
20421 /* Add a DW_AT_name attribute and source coordinate attribute for the
20422 given decl, but only if it actually has a name. */
20424 static void
20425 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20426 bool no_linkage_name)
20428 tree decl_name;
20430 decl_name = DECL_NAME (decl);
20431 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20433 const char *name = dwarf2_name (decl, 0);
20434 if (name)
20435 add_name_attribute (die, name);
20436 if (! DECL_ARTIFICIAL (decl))
20437 add_src_coords_attributes (die, decl);
20439 if (!no_linkage_name)
20440 add_linkage_name (die, decl);
20443 #ifdef VMS_DEBUGGING_INFO
20444 /* Get the function's name, as described by its RTL. This may be different
20445 from the DECL_NAME name used in the source file. */
20446 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20448 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20449 XEXP (DECL_RTL (decl), 0), false);
20450 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20452 #endif /* VMS_DEBUGGING_INFO */
20455 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20457 static void
20458 add_discr_value (dw_die_ref die, dw_discr_value *value)
20460 dw_attr_node attr;
20462 attr.dw_attr = DW_AT_discr_value;
20463 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20464 attr.dw_attr_val.val_entry = NULL;
20465 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20466 if (value->pos)
20467 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20468 else
20469 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20470 add_dwarf_attr (die, &attr);
20473 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20475 static void
20476 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20478 dw_attr_node attr;
20480 attr.dw_attr = DW_AT_discr_list;
20481 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20482 attr.dw_attr_val.val_entry = NULL;
20483 attr.dw_attr_val.v.val_discr_list = discr_list;
20484 add_dwarf_attr (die, &attr);
20487 static inline dw_discr_list_ref
20488 AT_discr_list (dw_attr_node *attr)
20490 return attr->dw_attr_val.v.val_discr_list;
20493 #ifdef VMS_DEBUGGING_INFO
20494 /* Output the debug main pointer die for VMS */
20496 void
20497 dwarf2out_vms_debug_main_pointer (void)
20499 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20500 dw_die_ref die;
20502 /* Allocate the VMS debug main subprogram die. */
20503 die = ggc_cleared_alloc<die_node> ();
20504 die->die_tag = DW_TAG_subprogram;
20505 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20506 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20507 current_function_funcdef_no);
20508 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20510 /* Make it the first child of comp_unit_die (). */
20511 die->die_parent = comp_unit_die ();
20512 if (comp_unit_die ()->die_child)
20514 die->die_sib = comp_unit_die ()->die_child->die_sib;
20515 comp_unit_die ()->die_child->die_sib = die;
20517 else
20519 die->die_sib = die;
20520 comp_unit_die ()->die_child = die;
20523 #endif /* VMS_DEBUGGING_INFO */
20525 /* Push a new declaration scope. */
20527 static void
20528 push_decl_scope (tree scope)
20530 vec_safe_push (decl_scope_table, scope);
20533 /* Pop a declaration scope. */
20535 static inline void
20536 pop_decl_scope (void)
20538 decl_scope_table->pop ();
20541 /* walk_tree helper function for uses_local_type, below. */
20543 static tree
20544 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20546 if (!TYPE_P (*tp))
20547 *walk_subtrees = 0;
20548 else
20550 tree name = TYPE_NAME (*tp);
20551 if (name && DECL_P (name) && decl_function_context (name))
20552 return *tp;
20554 return NULL_TREE;
20557 /* If TYPE involves a function-local type (including a local typedef to a
20558 non-local type), returns that type; otherwise returns NULL_TREE. */
20560 static tree
20561 uses_local_type (tree type)
20563 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20564 return used;
20567 /* Return the DIE for the scope that immediately contains this type.
20568 Non-named types that do not involve a function-local type get global
20569 scope. Named types nested in namespaces or other types get their
20570 containing scope. All other types (i.e. function-local named types) get
20571 the current active scope. */
20573 static dw_die_ref
20574 scope_die_for (tree t, dw_die_ref context_die)
20576 dw_die_ref scope_die = NULL;
20577 tree containing_scope;
20579 /* Non-types always go in the current scope. */
20580 gcc_assert (TYPE_P (t));
20582 /* Use the scope of the typedef, rather than the scope of the type
20583 it refers to. */
20584 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20585 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20586 else
20587 containing_scope = TYPE_CONTEXT (t);
20589 /* Use the containing namespace if there is one. */
20590 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20592 if (context_die == lookup_decl_die (containing_scope))
20593 /* OK */;
20594 else if (debug_info_level > DINFO_LEVEL_TERSE)
20595 context_die = get_context_die (containing_scope);
20596 else
20597 containing_scope = NULL_TREE;
20600 /* Ignore function type "scopes" from the C frontend. They mean that
20601 a tagged type is local to a parmlist of a function declarator, but
20602 that isn't useful to DWARF. */
20603 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20604 containing_scope = NULL_TREE;
20606 if (SCOPE_FILE_SCOPE_P (containing_scope))
20608 /* If T uses a local type keep it local as well, to avoid references
20609 to function-local DIEs from outside the function. */
20610 if (current_function_decl && uses_local_type (t))
20611 scope_die = context_die;
20612 else
20613 scope_die = comp_unit_die ();
20615 else if (TYPE_P (containing_scope))
20617 /* For types, we can just look up the appropriate DIE. */
20618 if (debug_info_level > DINFO_LEVEL_TERSE)
20619 scope_die = get_context_die (containing_scope);
20620 else
20622 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20623 if (scope_die == NULL)
20624 scope_die = comp_unit_die ();
20627 else
20628 scope_die = context_die;
20630 return scope_die;
20633 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20635 static inline int
20636 local_scope_p (dw_die_ref context_die)
20638 for (; context_die; context_die = context_die->die_parent)
20639 if (context_die->die_tag == DW_TAG_inlined_subroutine
20640 || context_die->die_tag == DW_TAG_subprogram)
20641 return 1;
20643 return 0;
20646 /* Returns nonzero if CONTEXT_DIE is a class. */
20648 static inline int
20649 class_scope_p (dw_die_ref context_die)
20651 return (context_die
20652 && (context_die->die_tag == DW_TAG_structure_type
20653 || context_die->die_tag == DW_TAG_class_type
20654 || context_die->die_tag == DW_TAG_interface_type
20655 || context_die->die_tag == DW_TAG_union_type));
20658 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20659 whether or not to treat a DIE in this context as a declaration. */
20661 static inline int
20662 class_or_namespace_scope_p (dw_die_ref context_die)
20664 return (class_scope_p (context_die)
20665 || (context_die && context_die->die_tag == DW_TAG_namespace));
20668 /* Many forms of DIEs require a "type description" attribute. This
20669 routine locates the proper "type descriptor" die for the type given
20670 by 'type' plus any additional qualifiers given by 'cv_quals', and
20671 adds a DW_AT_type attribute below the given die. */
20673 static void
20674 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20675 bool reverse, dw_die_ref context_die)
20677 enum tree_code code = TREE_CODE (type);
20678 dw_die_ref type_die = NULL;
20680 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20681 or fixed-point type, use the inner type. This is because we have no
20682 support for unnamed types in base_type_die. This can happen if this is
20683 an Ada subrange type. Correct solution is emit a subrange type die. */
20684 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20685 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20686 type = TREE_TYPE (type), code = TREE_CODE (type);
20688 if (code == ERROR_MARK
20689 /* Handle a special case. For functions whose return type is void, we
20690 generate *no* type attribute. (Note that no object may have type
20691 `void', so this only applies to function return types). */
20692 || code == VOID_TYPE)
20693 return;
20695 type_die = modified_type_die (type,
20696 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20697 reverse,
20698 context_die);
20700 if (type_die != NULL)
20701 add_AT_die_ref (object_die, DW_AT_type, type_die);
20704 /* Given an object die, add the calling convention attribute for the
20705 function call type. */
20706 static void
20707 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20709 enum dwarf_calling_convention value = DW_CC_normal;
20711 value = ((enum dwarf_calling_convention)
20712 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20714 if (is_fortran ()
20715 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20717 /* DWARF 2 doesn't provide a way to identify a program's source-level
20718 entry point. DW_AT_calling_convention attributes are only meant
20719 to describe functions' calling conventions. However, lacking a
20720 better way to signal the Fortran main program, we used this for
20721 a long time, following existing custom. Now, DWARF 4 has
20722 DW_AT_main_subprogram, which we add below, but some tools still
20723 rely on the old way, which we thus keep. */
20724 value = DW_CC_program;
20726 if (dwarf_version >= 4 || !dwarf_strict)
20727 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20730 /* Only add the attribute if the backend requests it, and
20731 is not DW_CC_normal. */
20732 if (value && (value != DW_CC_normal))
20733 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20736 /* Given a tree pointer to a struct, class, union, or enum type node, return
20737 a pointer to the (string) tag name for the given type, or zero if the type
20738 was declared without a tag. */
20740 static const char *
20741 type_tag (const_tree type)
20743 const char *name = 0;
20745 if (TYPE_NAME (type) != 0)
20747 tree t = 0;
20749 /* Find the IDENTIFIER_NODE for the type name. */
20750 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20751 && !TYPE_NAMELESS (type))
20752 t = TYPE_NAME (type);
20754 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20755 a TYPE_DECL node, regardless of whether or not a `typedef' was
20756 involved. */
20757 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20758 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20760 /* We want to be extra verbose. Don't call dwarf_name if
20761 DECL_NAME isn't set. The default hook for decl_printable_name
20762 doesn't like that, and in this context it's correct to return
20763 0, instead of "<anonymous>" or the like. */
20764 if (DECL_NAME (TYPE_NAME (type))
20765 && !DECL_NAMELESS (TYPE_NAME (type)))
20766 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20769 /* Now get the name as a string, or invent one. */
20770 if (!name && t != 0)
20771 name = IDENTIFIER_POINTER (t);
20774 return (name == 0 || *name == '\0') ? 0 : name;
20777 /* Return the type associated with a data member, make a special check
20778 for bit field types. */
20780 static inline tree
20781 member_declared_type (const_tree member)
20783 return (DECL_BIT_FIELD_TYPE (member)
20784 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20787 /* Get the decl's label, as described by its RTL. This may be different
20788 from the DECL_NAME name used in the source file. */
20790 #if 0
20791 static const char *
20792 decl_start_label (tree decl)
20794 rtx x;
20795 const char *fnname;
20797 x = DECL_RTL (decl);
20798 gcc_assert (MEM_P (x));
20800 x = XEXP (x, 0);
20801 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20803 fnname = XSTR (x, 0);
20804 return fnname;
20806 #endif
20808 /* For variable-length arrays that have been previously generated, but
20809 may be incomplete due to missing subscript info, fill the subscript
20810 info. Return TRUE if this is one of those cases. */
20811 static bool
20812 fill_variable_array_bounds (tree type)
20814 if (TREE_ASM_WRITTEN (type)
20815 && TREE_CODE (type) == ARRAY_TYPE
20816 && variably_modified_type_p (type, NULL))
20818 dw_die_ref array_die = lookup_type_die (type);
20819 if (!array_die)
20820 return false;
20821 add_subscript_info (array_die, type, !is_ada ());
20822 return true;
20824 return false;
20827 /* These routines generate the internal representation of the DIE's for
20828 the compilation unit. Debugging information is collected by walking
20829 the declaration trees passed in from dwarf2out_decl(). */
20831 static void
20832 gen_array_type_die (tree type, dw_die_ref context_die)
20834 dw_die_ref array_die;
20836 /* GNU compilers represent multidimensional array types as sequences of one
20837 dimensional array types whose element types are themselves array types.
20838 We sometimes squish that down to a single array_type DIE with multiple
20839 subscripts in the Dwarf debugging info. The draft Dwarf specification
20840 say that we are allowed to do this kind of compression in C, because
20841 there is no difference between an array of arrays and a multidimensional
20842 array. We don't do this for Ada to remain as close as possible to the
20843 actual representation, which is especially important against the language
20844 flexibilty wrt arrays of variable size. */
20846 bool collapse_nested_arrays = !is_ada ();
20848 if (fill_variable_array_bounds (type))
20849 return;
20851 dw_die_ref scope_die = scope_die_for (type, context_die);
20852 tree element_type;
20854 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20855 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20856 if (TYPE_STRING_FLAG (type)
20857 && TREE_CODE (type) == ARRAY_TYPE
20858 && is_fortran ()
20859 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20861 HOST_WIDE_INT size;
20863 array_die = new_die (DW_TAG_string_type, scope_die, type);
20864 add_name_attribute (array_die, type_tag (type));
20865 equate_type_number_to_die (type, array_die);
20866 size = int_size_in_bytes (type);
20867 if (size >= 0)
20868 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20869 /* ??? We can't annotate types late, but for LTO we may not
20870 generate a location early either (gfortran.dg/save_6.f90). */
20871 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20872 && TYPE_DOMAIN (type) != NULL_TREE
20873 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20875 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20876 tree rszdecl = szdecl;
20878 size = int_size_in_bytes (TREE_TYPE (szdecl));
20879 if (!DECL_P (szdecl))
20881 if (TREE_CODE (szdecl) == INDIRECT_REF
20882 && DECL_P (TREE_OPERAND (szdecl, 0)))
20884 rszdecl = TREE_OPERAND (szdecl, 0);
20885 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20886 != DWARF2_ADDR_SIZE)
20887 size = 0;
20889 else
20890 size = 0;
20892 if (size > 0)
20894 dw_loc_list_ref loc
20895 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20896 NULL);
20897 if (loc)
20899 add_AT_location_description (array_die, DW_AT_string_length,
20900 loc);
20901 if (size != DWARF2_ADDR_SIZE)
20902 add_AT_unsigned (array_die, dwarf_version >= 5
20903 ? DW_AT_string_length_byte_size
20904 : DW_AT_byte_size, size);
20908 return;
20911 array_die = new_die (DW_TAG_array_type, scope_die, type);
20912 add_name_attribute (array_die, type_tag (type));
20913 equate_type_number_to_die (type, array_die);
20915 if (TREE_CODE (type) == VECTOR_TYPE)
20916 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20918 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20919 if (is_fortran ()
20920 && TREE_CODE (type) == ARRAY_TYPE
20921 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20922 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20923 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20925 #if 0
20926 /* We default the array ordering. SDB will probably do
20927 the right things even if DW_AT_ordering is not present. It's not even
20928 an issue until we start to get into multidimensional arrays anyway. If
20929 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20930 then we'll have to put the DW_AT_ordering attribute back in. (But if
20931 and when we find out that we need to put these in, we will only do so
20932 for multidimensional arrays. */
20933 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20934 #endif
20936 if (TREE_CODE (type) == VECTOR_TYPE)
20938 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20939 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20940 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20941 add_bound_info (subrange_die, DW_AT_upper_bound,
20942 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20944 else
20945 add_subscript_info (array_die, type, collapse_nested_arrays);
20947 /* Add representation of the type of the elements of this array type and
20948 emit the corresponding DIE if we haven't done it already. */
20949 element_type = TREE_TYPE (type);
20950 if (collapse_nested_arrays)
20951 while (TREE_CODE (element_type) == ARRAY_TYPE)
20953 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20954 break;
20955 element_type = TREE_TYPE (element_type);
20958 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20959 TREE_CODE (type) == ARRAY_TYPE
20960 && TYPE_REVERSE_STORAGE_ORDER (type),
20961 context_die);
20963 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20964 if (TYPE_ARTIFICIAL (type))
20965 add_AT_flag (array_die, DW_AT_artificial, 1);
20967 if (get_AT (array_die, DW_AT_name))
20968 add_pubtype (type, array_die);
20970 add_alignment_attribute (array_die, type);
20973 /* This routine generates DIE for array with hidden descriptor, details
20974 are filled into *info by a langhook. */
20976 static void
20977 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20978 dw_die_ref context_die)
20980 const dw_die_ref scope_die = scope_die_for (type, context_die);
20981 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20982 struct loc_descr_context context = { type, info->base_decl, NULL,
20983 false, false };
20984 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20985 int dim;
20987 add_name_attribute (array_die, type_tag (type));
20988 equate_type_number_to_die (type, array_die);
20990 if (info->ndimensions > 1)
20991 switch (info->ordering)
20993 case array_descr_ordering_row_major:
20994 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20995 break;
20996 case array_descr_ordering_column_major:
20997 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20998 break;
20999 default:
21000 break;
21003 if (dwarf_version >= 3 || !dwarf_strict)
21005 if (info->data_location)
21006 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21007 dw_scalar_form_exprloc, &context);
21008 if (info->associated)
21009 add_scalar_info (array_die, DW_AT_associated, info->associated,
21010 dw_scalar_form_constant
21011 | dw_scalar_form_exprloc
21012 | dw_scalar_form_reference, &context);
21013 if (info->allocated)
21014 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21015 dw_scalar_form_constant
21016 | dw_scalar_form_exprloc
21017 | dw_scalar_form_reference, &context);
21018 if (info->stride)
21020 const enum dwarf_attribute attr
21021 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21022 const int forms
21023 = (info->stride_in_bits)
21024 ? dw_scalar_form_constant
21025 : (dw_scalar_form_constant
21026 | dw_scalar_form_exprloc
21027 | dw_scalar_form_reference);
21029 add_scalar_info (array_die, attr, info->stride, forms, &context);
21032 if (dwarf_version >= 5)
21034 if (info->rank)
21036 add_scalar_info (array_die, DW_AT_rank, info->rank,
21037 dw_scalar_form_constant
21038 | dw_scalar_form_exprloc, &context);
21039 subrange_tag = DW_TAG_generic_subrange;
21040 context.placeholder_arg = true;
21044 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21046 for (dim = 0; dim < info->ndimensions; dim++)
21048 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21050 if (info->dimen[dim].bounds_type)
21051 add_type_attribute (subrange_die,
21052 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21053 false, context_die);
21054 if (info->dimen[dim].lower_bound)
21055 add_bound_info (subrange_die, DW_AT_lower_bound,
21056 info->dimen[dim].lower_bound, &context);
21057 if (info->dimen[dim].upper_bound)
21058 add_bound_info (subrange_die, DW_AT_upper_bound,
21059 info->dimen[dim].upper_bound, &context);
21060 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21061 add_scalar_info (subrange_die, DW_AT_byte_stride,
21062 info->dimen[dim].stride,
21063 dw_scalar_form_constant
21064 | dw_scalar_form_exprloc
21065 | dw_scalar_form_reference,
21066 &context);
21069 gen_type_die (info->element_type, context_die);
21070 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21071 TREE_CODE (type) == ARRAY_TYPE
21072 && TYPE_REVERSE_STORAGE_ORDER (type),
21073 context_die);
21075 if (get_AT (array_die, DW_AT_name))
21076 add_pubtype (type, array_die);
21078 add_alignment_attribute (array_die, type);
21081 #if 0
21082 static void
21083 gen_entry_point_die (tree decl, dw_die_ref context_die)
21085 tree origin = decl_ultimate_origin (decl);
21086 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21088 if (origin != NULL)
21089 add_abstract_origin_attribute (decl_die, origin);
21090 else
21092 add_name_and_src_coords_attributes (decl_die, decl);
21093 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21094 TYPE_UNQUALIFIED, false, context_die);
21097 if (DECL_ABSTRACT_P (decl))
21098 equate_decl_number_to_die (decl, decl_die);
21099 else
21100 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21102 #endif
21104 /* Walk through the list of incomplete types again, trying once more to
21105 emit full debugging info for them. */
21107 static void
21108 retry_incomplete_types (void)
21110 set_early_dwarf s;
21111 int i;
21113 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21114 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21115 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21116 vec_safe_truncate (incomplete_types, 0);
21119 /* Determine what tag to use for a record type. */
21121 static enum dwarf_tag
21122 record_type_tag (tree type)
21124 if (! lang_hooks.types.classify_record)
21125 return DW_TAG_structure_type;
21127 switch (lang_hooks.types.classify_record (type))
21129 case RECORD_IS_STRUCT:
21130 return DW_TAG_structure_type;
21132 case RECORD_IS_CLASS:
21133 return DW_TAG_class_type;
21135 case RECORD_IS_INTERFACE:
21136 if (dwarf_version >= 3 || !dwarf_strict)
21137 return DW_TAG_interface_type;
21138 return DW_TAG_structure_type;
21140 default:
21141 gcc_unreachable ();
21145 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21146 include all of the information about the enumeration values also. Each
21147 enumerated type name/value is listed as a child of the enumerated type
21148 DIE. */
21150 static dw_die_ref
21151 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21153 dw_die_ref type_die = lookup_type_die (type);
21155 if (type_die == NULL)
21157 type_die = new_die (DW_TAG_enumeration_type,
21158 scope_die_for (type, context_die), type);
21159 equate_type_number_to_die (type, type_die);
21160 add_name_attribute (type_die, type_tag (type));
21161 if (dwarf_version >= 4 || !dwarf_strict)
21163 if (ENUM_IS_SCOPED (type))
21164 add_AT_flag (type_die, DW_AT_enum_class, 1);
21165 if (ENUM_IS_OPAQUE (type))
21166 add_AT_flag (type_die, DW_AT_declaration, 1);
21168 if (!dwarf_strict)
21169 add_AT_unsigned (type_die, DW_AT_encoding,
21170 TYPE_UNSIGNED (type)
21171 ? DW_ATE_unsigned
21172 : DW_ATE_signed);
21174 else if (! TYPE_SIZE (type))
21175 return type_die;
21176 else
21177 remove_AT (type_die, DW_AT_declaration);
21179 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21180 given enum type is incomplete, do not generate the DW_AT_byte_size
21181 attribute or the DW_AT_element_list attribute. */
21182 if (TYPE_SIZE (type))
21184 tree link;
21186 TREE_ASM_WRITTEN (type) = 1;
21187 add_byte_size_attribute (type_die, type);
21188 add_alignment_attribute (type_die, type);
21189 if (dwarf_version >= 3 || !dwarf_strict)
21191 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21192 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21193 context_die);
21195 if (TYPE_STUB_DECL (type) != NULL_TREE)
21197 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21198 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21201 /* If the first reference to this type was as the return type of an
21202 inline function, then it may not have a parent. Fix this now. */
21203 if (type_die->die_parent == NULL)
21204 add_child_die (scope_die_for (type, context_die), type_die);
21206 for (link = TYPE_VALUES (type);
21207 link != NULL; link = TREE_CHAIN (link))
21209 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21210 tree value = TREE_VALUE (link);
21212 add_name_attribute (enum_die,
21213 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21215 if (TREE_CODE (value) == CONST_DECL)
21216 value = DECL_INITIAL (value);
21218 if (simple_type_size_in_bits (TREE_TYPE (value))
21219 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21221 /* For constant forms created by add_AT_unsigned DWARF
21222 consumers (GDB, elfutils, etc.) always zero extend
21223 the value. Only when the actual value is negative
21224 do we need to use add_AT_int to generate a constant
21225 form that can represent negative values. */
21226 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21227 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21228 add_AT_unsigned (enum_die, DW_AT_const_value,
21229 (unsigned HOST_WIDE_INT) val);
21230 else
21231 add_AT_int (enum_die, DW_AT_const_value, val);
21233 else
21234 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21235 that here. TODO: This should be re-worked to use correct
21236 signed/unsigned double tags for all cases. */
21237 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21240 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21241 if (TYPE_ARTIFICIAL (type))
21242 add_AT_flag (type_die, DW_AT_artificial, 1);
21244 else
21245 add_AT_flag (type_die, DW_AT_declaration, 1);
21247 add_pubtype (type, type_die);
21249 return type_die;
21252 /* Generate a DIE to represent either a real live formal parameter decl or to
21253 represent just the type of some formal parameter position in some function
21254 type.
21256 Note that this routine is a bit unusual because its argument may be a
21257 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21258 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21259 node. If it's the former then this function is being called to output a
21260 DIE to represent a formal parameter object (or some inlining thereof). If
21261 it's the latter, then this function is only being called to output a
21262 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21263 argument type of some subprogram type.
21264 If EMIT_NAME_P is true, name and source coordinate attributes
21265 are emitted. */
21267 static dw_die_ref
21268 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21269 dw_die_ref context_die)
21271 tree node_or_origin = node ? node : origin;
21272 tree ultimate_origin;
21273 dw_die_ref parm_die = NULL;
21275 if (DECL_P (node_or_origin))
21277 parm_die = lookup_decl_die (node);
21279 /* If the contexts differ, we may not be talking about the same
21280 thing.
21281 ??? When in LTO the DIE parent is the "abstract" copy and the
21282 context_die is the specification "copy". But this whole block
21283 should eventually be no longer needed. */
21284 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21286 if (!DECL_ABSTRACT_P (node))
21288 /* This can happen when creating an inlined instance, in
21289 which case we need to create a new DIE that will get
21290 annotated with DW_AT_abstract_origin. */
21291 parm_die = NULL;
21293 else
21294 gcc_unreachable ();
21297 if (parm_die && parm_die->die_parent == NULL)
21299 /* Check that parm_die already has the right attributes that
21300 we would have added below. If any attributes are
21301 missing, fall through to add them. */
21302 if (! DECL_ABSTRACT_P (node_or_origin)
21303 && !get_AT (parm_die, DW_AT_location)
21304 && !get_AT (parm_die, DW_AT_const_value))
21305 /* We are missing location info, and are about to add it. */
21307 else
21309 add_child_die (context_die, parm_die);
21310 return parm_die;
21315 /* If we have a previously generated DIE, use it, unless this is an
21316 concrete instance (origin != NULL), in which case we need a new
21317 DIE with a corresponding DW_AT_abstract_origin. */
21318 bool reusing_die;
21319 if (parm_die && origin == NULL)
21320 reusing_die = true;
21321 else
21323 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21324 reusing_die = false;
21327 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21329 case tcc_declaration:
21330 ultimate_origin = decl_ultimate_origin (node_or_origin);
21331 if (node || ultimate_origin)
21332 origin = ultimate_origin;
21334 if (reusing_die)
21335 goto add_location;
21337 if (origin != NULL)
21338 add_abstract_origin_attribute (parm_die, origin);
21339 else if (emit_name_p)
21340 add_name_and_src_coords_attributes (parm_die, node);
21341 if (origin == NULL
21342 || (! DECL_ABSTRACT_P (node_or_origin)
21343 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21344 decl_function_context
21345 (node_or_origin))))
21347 tree type = TREE_TYPE (node_or_origin);
21348 if (decl_by_reference_p (node_or_origin))
21349 add_type_attribute (parm_die, TREE_TYPE (type),
21350 TYPE_UNQUALIFIED,
21351 false, context_die);
21352 else
21353 add_type_attribute (parm_die, type,
21354 decl_quals (node_or_origin),
21355 false, context_die);
21357 if (origin == NULL && DECL_ARTIFICIAL (node))
21358 add_AT_flag (parm_die, DW_AT_artificial, 1);
21359 add_location:
21360 if (node && node != origin)
21361 equate_decl_number_to_die (node, parm_die);
21362 if (! DECL_ABSTRACT_P (node_or_origin))
21363 add_location_or_const_value_attribute (parm_die, node_or_origin,
21364 node == NULL);
21366 break;
21368 case tcc_type:
21369 /* We were called with some kind of a ..._TYPE node. */
21370 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21371 context_die);
21372 break;
21374 default:
21375 gcc_unreachable ();
21378 return parm_die;
21381 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21382 children DW_TAG_formal_parameter DIEs representing the arguments of the
21383 parameter pack.
21385 PARM_PACK must be a function parameter pack.
21386 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21387 must point to the subsequent arguments of the function PACK_ARG belongs to.
21388 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21389 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21390 following the last one for which a DIE was generated. */
21392 static dw_die_ref
21393 gen_formal_parameter_pack_die (tree parm_pack,
21394 tree pack_arg,
21395 dw_die_ref subr_die,
21396 tree *next_arg)
21398 tree arg;
21399 dw_die_ref parm_pack_die;
21401 gcc_assert (parm_pack
21402 && lang_hooks.function_parameter_pack_p (parm_pack)
21403 && subr_die);
21405 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21406 add_src_coords_attributes (parm_pack_die, parm_pack);
21408 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21410 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21411 parm_pack))
21412 break;
21413 gen_formal_parameter_die (arg, NULL,
21414 false /* Don't emit name attribute. */,
21415 parm_pack_die);
21417 if (next_arg)
21418 *next_arg = arg;
21419 return parm_pack_die;
21422 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21423 at the end of an (ANSI prototyped) formal parameters list. */
21425 static void
21426 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21428 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21431 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21432 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21433 parameters as specified in some function type specification (except for
21434 those which appear as part of a function *definition*). */
21436 static void
21437 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21439 tree link;
21440 tree formal_type = NULL;
21441 tree first_parm_type;
21442 tree arg;
21444 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21446 arg = DECL_ARGUMENTS (function_or_method_type);
21447 function_or_method_type = TREE_TYPE (function_or_method_type);
21449 else
21450 arg = NULL_TREE;
21452 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21454 /* Make our first pass over the list of formal parameter types and output a
21455 DW_TAG_formal_parameter DIE for each one. */
21456 for (link = first_parm_type; link; )
21458 dw_die_ref parm_die;
21460 formal_type = TREE_VALUE (link);
21461 if (formal_type == void_type_node)
21462 break;
21464 /* Output a (nameless) DIE to represent the formal parameter itself. */
21465 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21467 parm_die = gen_formal_parameter_die (formal_type, NULL,
21468 true /* Emit name attribute. */,
21469 context_die);
21470 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21471 && link == first_parm_type)
21473 add_AT_flag (parm_die, DW_AT_artificial, 1);
21474 if (dwarf_version >= 3 || !dwarf_strict)
21475 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21477 else if (arg && DECL_ARTIFICIAL (arg))
21478 add_AT_flag (parm_die, DW_AT_artificial, 1);
21481 link = TREE_CHAIN (link);
21482 if (arg)
21483 arg = DECL_CHAIN (arg);
21486 /* If this function type has an ellipsis, add a
21487 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21488 if (formal_type != void_type_node)
21489 gen_unspecified_parameters_die (function_or_method_type, context_die);
21491 /* Make our second (and final) pass over the list of formal parameter types
21492 and output DIEs to represent those types (as necessary). */
21493 for (link = TYPE_ARG_TYPES (function_or_method_type);
21494 link && TREE_VALUE (link);
21495 link = TREE_CHAIN (link))
21496 gen_type_die (TREE_VALUE (link), context_die);
21499 /* We want to generate the DIE for TYPE so that we can generate the
21500 die for MEMBER, which has been defined; we will need to refer back
21501 to the member declaration nested within TYPE. If we're trying to
21502 generate minimal debug info for TYPE, processing TYPE won't do the
21503 trick; we need to attach the member declaration by hand. */
21505 static void
21506 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21508 gen_type_die (type, context_die);
21510 /* If we're trying to avoid duplicate debug info, we may not have
21511 emitted the member decl for this function. Emit it now. */
21512 if (TYPE_STUB_DECL (type)
21513 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21514 && ! lookup_decl_die (member))
21516 dw_die_ref type_die;
21517 gcc_assert (!decl_ultimate_origin (member));
21519 push_decl_scope (type);
21520 type_die = lookup_type_die_strip_naming_typedef (type);
21521 if (TREE_CODE (member) == FUNCTION_DECL)
21522 gen_subprogram_die (member, type_die);
21523 else if (TREE_CODE (member) == FIELD_DECL)
21525 /* Ignore the nameless fields that are used to skip bits but handle
21526 C++ anonymous unions and structs. */
21527 if (DECL_NAME (member) != NULL_TREE
21528 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21529 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21531 struct vlr_context vlr_ctx = {
21532 DECL_CONTEXT (member), /* struct_type */
21533 NULL_TREE /* variant_part_offset */
21535 gen_type_die (member_declared_type (member), type_die);
21536 gen_field_die (member, &vlr_ctx, type_die);
21539 else
21540 gen_variable_die (member, NULL_TREE, type_die);
21542 pop_decl_scope ();
21546 /* Forward declare these functions, because they are mutually recursive
21547 with their set_block_* pairing functions. */
21548 static void set_decl_origin_self (tree);
21550 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21551 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21552 that it points to the node itself, thus indicating that the node is its
21553 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21554 the given node is NULL, recursively descend the decl/block tree which
21555 it is the root of, and for each other ..._DECL or BLOCK node contained
21556 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21557 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21558 values to point to themselves. */
21560 static void
21561 set_block_origin_self (tree stmt)
21563 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21565 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21568 tree local_decl;
21570 for (local_decl = BLOCK_VARS (stmt);
21571 local_decl != NULL_TREE;
21572 local_decl = DECL_CHAIN (local_decl))
21573 /* Do not recurse on nested functions since the inlining status
21574 of parent and child can be different as per the DWARF spec. */
21575 if (TREE_CODE (local_decl) != FUNCTION_DECL
21576 && !DECL_EXTERNAL (local_decl))
21577 set_decl_origin_self (local_decl);
21581 tree subblock;
21583 for (subblock = BLOCK_SUBBLOCKS (stmt);
21584 subblock != NULL_TREE;
21585 subblock = BLOCK_CHAIN (subblock))
21586 set_block_origin_self (subblock); /* Recurse. */
21591 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21592 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21593 node to so that it points to the node itself, thus indicating that the
21594 node represents its own (abstract) origin. Additionally, if the
21595 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21596 the decl/block tree of which the given node is the root of, and for
21597 each other ..._DECL or BLOCK node contained therein whose
21598 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21599 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21600 point to themselves. */
21602 static void
21603 set_decl_origin_self (tree decl)
21605 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21607 DECL_ABSTRACT_ORIGIN (decl) = decl;
21608 if (TREE_CODE (decl) == FUNCTION_DECL)
21610 tree arg;
21612 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21613 DECL_ABSTRACT_ORIGIN (arg) = arg;
21614 if (DECL_INITIAL (decl) != NULL_TREE
21615 && DECL_INITIAL (decl) != error_mark_node)
21616 set_block_origin_self (DECL_INITIAL (decl));
21621 /* Mark the early DIE for DECL as the abstract instance. */
21623 static void
21624 dwarf2out_abstract_function (tree decl)
21626 dw_die_ref old_die;
21628 /* Make sure we have the actual abstract inline, not a clone. */
21629 decl = DECL_ORIGIN (decl);
21631 if (DECL_IGNORED_P (decl))
21632 return;
21634 old_die = lookup_decl_die (decl);
21635 /* With early debug we always have an old DIE unless we are in LTO
21636 and the user did not compile but only link with debug. */
21637 if (in_lto_p && ! old_die)
21638 return;
21639 gcc_assert (old_die != NULL);
21640 if (get_AT (old_die, DW_AT_inline)
21641 || get_AT (old_die, DW_AT_abstract_origin))
21642 /* We've already generated the abstract instance. */
21643 return;
21645 /* Go ahead and put DW_AT_inline on the DIE. */
21646 if (DECL_DECLARED_INLINE_P (decl))
21648 if (cgraph_function_possibly_inlined_p (decl))
21649 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21650 else
21651 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21653 else
21655 if (cgraph_function_possibly_inlined_p (decl))
21656 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21657 else
21658 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21661 if (DECL_DECLARED_INLINE_P (decl)
21662 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21663 add_AT_flag (old_die, DW_AT_artificial, 1);
21665 set_decl_origin_self (decl);
21668 /* Helper function of premark_used_types() which gets called through
21669 htab_traverse.
21671 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21672 marked as unused by prune_unused_types. */
21674 bool
21675 premark_used_types_helper (tree const &type, void *)
21677 dw_die_ref die;
21679 die = lookup_type_die (type);
21680 if (die != NULL)
21681 die->die_perennial_p = 1;
21682 return true;
21685 /* Helper function of premark_types_used_by_global_vars which gets called
21686 through htab_traverse.
21688 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21689 marked as unused by prune_unused_types. The DIE of the type is marked
21690 only if the global variable using the type will actually be emitted. */
21693 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21694 void *)
21696 struct types_used_by_vars_entry *entry;
21697 dw_die_ref die;
21699 entry = (struct types_used_by_vars_entry *) *slot;
21700 gcc_assert (entry->type != NULL
21701 && entry->var_decl != NULL);
21702 die = lookup_type_die (entry->type);
21703 if (die)
21705 /* Ask cgraph if the global variable really is to be emitted.
21706 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21707 varpool_node *node = varpool_node::get (entry->var_decl);
21708 if (node && node->definition)
21710 die->die_perennial_p = 1;
21711 /* Keep the parent DIEs as well. */
21712 while ((die = die->die_parent) && die->die_perennial_p == 0)
21713 die->die_perennial_p = 1;
21716 return 1;
21719 /* Mark all members of used_types_hash as perennial. */
21721 static void
21722 premark_used_types (struct function *fun)
21724 if (fun && fun->used_types_hash)
21725 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21728 /* Mark all members of types_used_by_vars_entry as perennial. */
21730 static void
21731 premark_types_used_by_global_vars (void)
21733 if (types_used_by_vars_hash)
21734 types_used_by_vars_hash
21735 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21738 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21739 for CA_LOC call arg loc node. */
21741 static dw_die_ref
21742 gen_call_site_die (tree decl, dw_die_ref subr_die,
21743 struct call_arg_loc_node *ca_loc)
21745 dw_die_ref stmt_die = NULL, die;
21746 tree block = ca_loc->block;
21748 while (block
21749 && block != DECL_INITIAL (decl)
21750 && TREE_CODE (block) == BLOCK)
21752 stmt_die = BLOCK_DIE (block);
21753 if (stmt_die)
21754 break;
21755 block = BLOCK_SUPERCONTEXT (block);
21757 if (stmt_die == NULL)
21758 stmt_die = subr_die;
21759 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21760 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21761 if (ca_loc->tail_call_p)
21762 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21763 if (ca_loc->symbol_ref)
21765 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21766 if (tdie)
21767 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21768 else
21769 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21770 false);
21772 return die;
21775 /* Generate a DIE to represent a declared function (either file-scope or
21776 block-local). */
21778 static void
21779 gen_subprogram_die (tree decl, dw_die_ref context_die)
21781 tree origin = decl_ultimate_origin (decl);
21782 dw_die_ref subr_die;
21783 dw_die_ref old_die = lookup_decl_die (decl);
21785 /* This function gets called multiple times for different stages of
21786 the debug process. For example, for func() in this code:
21788 namespace S
21790 void func() { ... }
21793 ...we get called 4 times. Twice in early debug and twice in
21794 late debug:
21796 Early debug
21797 -----------
21799 1. Once while generating func() within the namespace. This is
21800 the declaration. The declaration bit below is set, as the
21801 context is the namespace.
21803 A new DIE will be generated with DW_AT_declaration set.
21805 2. Once for func() itself. This is the specification. The
21806 declaration bit below is clear as the context is the CU.
21808 We will use the cached DIE from (1) to create a new DIE with
21809 DW_AT_specification pointing to the declaration in (1).
21811 Late debug via rest_of_handle_final()
21812 -------------------------------------
21814 3. Once generating func() within the namespace. This is also the
21815 declaration, as in (1), but this time we will early exit below
21816 as we have a cached DIE and a declaration needs no additional
21817 annotations (no locations), as the source declaration line
21818 info is enough.
21820 4. Once for func() itself. As in (2), this is the specification,
21821 but this time we will re-use the cached DIE, and just annotate
21822 it with the location information that should now be available.
21824 For something without namespaces, but with abstract instances, we
21825 are also called a multiple times:
21827 class Base
21829 public:
21830 Base (); // constructor declaration (1)
21833 Base::Base () { } // constructor specification (2)
21835 Early debug
21836 -----------
21838 1. Once for the Base() constructor by virtue of it being a
21839 member of the Base class. This is done via
21840 rest_of_type_compilation.
21842 This is a declaration, so a new DIE will be created with
21843 DW_AT_declaration.
21845 2. Once for the Base() constructor definition, but this time
21846 while generating the abstract instance of the base
21847 constructor (__base_ctor) which is being generated via early
21848 debug of reachable functions.
21850 Even though we have a cached version of the declaration (1),
21851 we will create a DW_AT_specification of the declaration DIE
21852 in (1).
21854 3. Once for the __base_ctor itself, but this time, we generate
21855 an DW_AT_abstract_origin version of the DW_AT_specification in
21856 (2).
21858 Late debug via rest_of_handle_final
21859 -----------------------------------
21861 4. One final time for the __base_ctor (which will have a cached
21862 DIE with DW_AT_abstract_origin created in (3). This time,
21863 we will just annotate the location information now
21864 available.
21866 int declaration = (current_function_decl != decl
21867 || class_or_namespace_scope_p (context_die));
21869 /* Now that the C++ front end lazily declares artificial member fns, we
21870 might need to retrofit the declaration into its class. */
21871 if (!declaration && !origin && !old_die
21872 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21873 && !class_or_namespace_scope_p (context_die)
21874 && debug_info_level > DINFO_LEVEL_TERSE)
21875 old_die = force_decl_die (decl);
21877 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21878 if (origin != NULL)
21880 gcc_assert (!declaration || local_scope_p (context_die));
21882 /* Fixup die_parent for the abstract instance of a nested
21883 inline function. */
21884 if (old_die && old_die->die_parent == NULL)
21885 add_child_die (context_die, old_die);
21887 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21889 /* If we have a DW_AT_abstract_origin we have a working
21890 cached version. */
21891 subr_die = old_die;
21893 else
21895 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21896 add_abstract_origin_attribute (subr_die, origin);
21897 /* This is where the actual code for a cloned function is.
21898 Let's emit linkage name attribute for it. This helps
21899 debuggers to e.g, set breakpoints into
21900 constructors/destructors when the user asks "break
21901 K::K". */
21902 add_linkage_name (subr_die, decl);
21905 /* A cached copy, possibly from early dwarf generation. Reuse as
21906 much as possible. */
21907 else if (old_die)
21909 /* A declaration that has been previously dumped needs no
21910 additional information. */
21911 if (declaration)
21912 return;
21914 if (!get_AT_flag (old_die, DW_AT_declaration)
21915 /* We can have a normal definition following an inline one in the
21916 case of redefinition of GNU C extern inlines.
21917 It seems reasonable to use AT_specification in this case. */
21918 && !get_AT (old_die, DW_AT_inline))
21920 /* Detect and ignore this case, where we are trying to output
21921 something we have already output. */
21922 if (get_AT (old_die, DW_AT_low_pc)
21923 || get_AT (old_die, DW_AT_ranges))
21924 return;
21926 /* If we have no location information, this must be a
21927 partially generated DIE from early dwarf generation.
21928 Fall through and generate it. */
21931 /* If the definition comes from the same place as the declaration,
21932 maybe use the old DIE. We always want the DIE for this function
21933 that has the *_pc attributes to be under comp_unit_die so the
21934 debugger can find it. We also need to do this for abstract
21935 instances of inlines, since the spec requires the out-of-line copy
21936 to have the same parent. For local class methods, this doesn't
21937 apply; we just use the old DIE. */
21938 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21939 struct dwarf_file_data * file_index = lookup_filename (s.file);
21940 if ((is_cu_die (old_die->die_parent)
21941 /* This condition fixes the inconsistency/ICE with the
21942 following Fortran test (or some derivative thereof) while
21943 building libgfortran:
21945 module some_m
21946 contains
21947 logical function funky (FLAG)
21948 funky = .true.
21949 end function
21950 end module
21952 || (old_die->die_parent
21953 && old_die->die_parent->die_tag == DW_TAG_module)
21954 || context_die == NULL)
21955 && (DECL_ARTIFICIAL (decl)
21956 /* The location attributes may be in the abstract origin
21957 which in the case of LTO might be not available to
21958 look at. */
21959 || get_AT (old_die, DW_AT_abstract_origin)
21960 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21961 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21962 == (unsigned) s.line)
21963 && (!debug_column_info
21964 || s.column == 0
21965 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21966 == (unsigned) s.column)))))
21968 subr_die = old_die;
21970 /* Clear out the declaration attribute, but leave the
21971 parameters so they can be augmented with location
21972 information later. Unless this was a declaration, in
21973 which case, wipe out the nameless parameters and recreate
21974 them further down. */
21975 if (remove_AT (subr_die, DW_AT_declaration))
21978 remove_AT (subr_die, DW_AT_object_pointer);
21979 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21982 /* Make a specification pointing to the previously built
21983 declaration. */
21984 else
21986 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21987 add_AT_specification (subr_die, old_die);
21988 add_pubname (decl, subr_die);
21989 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21990 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21991 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21992 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21993 if (debug_column_info
21994 && s.column
21995 && (get_AT_unsigned (old_die, DW_AT_decl_column)
21996 != (unsigned) s.column))
21997 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
21999 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22000 emit the real type on the definition die. */
22001 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22003 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22004 if (die == auto_die || die == decltype_auto_die)
22005 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22006 TYPE_UNQUALIFIED, false, context_die);
22009 /* When we process the method declaration, we haven't seen
22010 the out-of-class defaulted definition yet, so we have to
22011 recheck now. */
22012 if ((dwarf_version >= 5 || ! dwarf_strict)
22013 && !get_AT (subr_die, DW_AT_defaulted))
22015 int defaulted
22016 = lang_hooks.decls.decl_dwarf_attribute (decl,
22017 DW_AT_defaulted);
22018 if (defaulted != -1)
22020 /* Other values must have been handled before. */
22021 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22022 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22027 /* Create a fresh DIE for anything else. */
22028 else
22030 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22032 if (TREE_PUBLIC (decl))
22033 add_AT_flag (subr_die, DW_AT_external, 1);
22035 add_name_and_src_coords_attributes (subr_die, decl);
22036 add_pubname (decl, subr_die);
22037 if (debug_info_level > DINFO_LEVEL_TERSE)
22039 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22040 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22041 TYPE_UNQUALIFIED, false, context_die);
22044 add_pure_or_virtual_attribute (subr_die, decl);
22045 if (DECL_ARTIFICIAL (decl))
22046 add_AT_flag (subr_die, DW_AT_artificial, 1);
22048 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22049 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22051 add_alignment_attribute (subr_die, decl);
22053 add_accessibility_attribute (subr_die, decl);
22056 /* Unless we have an existing non-declaration DIE, equate the new
22057 DIE. */
22058 if (!old_die || is_declaration_die (old_die))
22059 equate_decl_number_to_die (decl, subr_die);
22061 if (declaration)
22063 if (!old_die || !get_AT (old_die, DW_AT_inline))
22065 add_AT_flag (subr_die, DW_AT_declaration, 1);
22067 /* If this is an explicit function declaration then generate
22068 a DW_AT_explicit attribute. */
22069 if ((dwarf_version >= 3 || !dwarf_strict)
22070 && lang_hooks.decls.decl_dwarf_attribute (decl,
22071 DW_AT_explicit) == 1)
22072 add_AT_flag (subr_die, DW_AT_explicit, 1);
22074 /* If this is a C++11 deleted special function member then generate
22075 a DW_AT_deleted attribute. */
22076 if ((dwarf_version >= 5 || !dwarf_strict)
22077 && lang_hooks.decls.decl_dwarf_attribute (decl,
22078 DW_AT_deleted) == 1)
22079 add_AT_flag (subr_die, DW_AT_deleted, 1);
22081 /* If this is a C++11 defaulted special function member then
22082 generate a DW_AT_defaulted attribute. */
22083 if (dwarf_version >= 5 || !dwarf_strict)
22085 int defaulted
22086 = lang_hooks.decls.decl_dwarf_attribute (decl,
22087 DW_AT_defaulted);
22088 if (defaulted != -1)
22089 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22092 /* If this is a C++11 non-static member function with & ref-qualifier
22093 then generate a DW_AT_reference attribute. */
22094 if ((dwarf_version >= 5 || !dwarf_strict)
22095 && lang_hooks.decls.decl_dwarf_attribute (decl,
22096 DW_AT_reference) == 1)
22097 add_AT_flag (subr_die, DW_AT_reference, 1);
22099 /* If this is a C++11 non-static member function with &&
22100 ref-qualifier then generate a DW_AT_reference attribute. */
22101 if ((dwarf_version >= 5 || !dwarf_strict)
22102 && lang_hooks.decls.decl_dwarf_attribute (decl,
22103 DW_AT_rvalue_reference)
22104 == 1)
22105 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22108 /* For non DECL_EXTERNALs, if range information is available, fill
22109 the DIE with it. */
22110 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22112 HOST_WIDE_INT cfa_fb_offset;
22114 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22116 if (!crtl->has_bb_partition)
22118 dw_fde_ref fde = fun->fde;
22119 if (fde->dw_fde_begin)
22121 /* We have already generated the labels. */
22122 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22123 fde->dw_fde_end, false);
22125 else
22127 /* Create start/end labels and add the range. */
22128 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22129 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22130 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22131 current_function_funcdef_no);
22132 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22133 current_function_funcdef_no);
22134 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22135 false);
22138 #if VMS_DEBUGGING_INFO
22139 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22140 Section 2.3 Prologue and Epilogue Attributes:
22141 When a breakpoint is set on entry to a function, it is generally
22142 desirable for execution to be suspended, not on the very first
22143 instruction of the function, but rather at a point after the
22144 function's frame has been set up, after any language defined local
22145 declaration processing has been completed, and before execution of
22146 the first statement of the function begins. Debuggers generally
22147 cannot properly determine where this point is. Similarly for a
22148 breakpoint set on exit from a function. The prologue and epilogue
22149 attributes allow a compiler to communicate the location(s) to use. */
22152 if (fde->dw_fde_vms_end_prologue)
22153 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22154 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22156 if (fde->dw_fde_vms_begin_epilogue)
22157 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22158 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22160 #endif
22163 else
22165 /* Generate pubnames entries for the split function code ranges. */
22166 dw_fde_ref fde = fun->fde;
22168 if (fde->dw_fde_second_begin)
22170 if (dwarf_version >= 3 || !dwarf_strict)
22172 /* We should use ranges for non-contiguous code section
22173 addresses. Use the actual code range for the initial
22174 section, since the HOT/COLD labels might precede an
22175 alignment offset. */
22176 bool range_list_added = false;
22177 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22178 fde->dw_fde_end, &range_list_added,
22179 false);
22180 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22181 fde->dw_fde_second_end,
22182 &range_list_added, false);
22183 if (range_list_added)
22184 add_ranges (NULL);
22186 else
22188 /* There is no real support in DW2 for this .. so we make
22189 a work-around. First, emit the pub name for the segment
22190 containing the function label. Then make and emit a
22191 simplified subprogram DIE for the second segment with the
22192 name pre-fixed by __hot/cold_sect_of_. We use the same
22193 linkage name for the second die so that gdb will find both
22194 sections when given "b foo". */
22195 const char *name = NULL;
22196 tree decl_name = DECL_NAME (decl);
22197 dw_die_ref seg_die;
22199 /* Do the 'primary' section. */
22200 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22201 fde->dw_fde_end, false);
22203 /* Build a minimal DIE for the secondary section. */
22204 seg_die = new_die (DW_TAG_subprogram,
22205 subr_die->die_parent, decl);
22207 if (TREE_PUBLIC (decl))
22208 add_AT_flag (seg_die, DW_AT_external, 1);
22210 if (decl_name != NULL
22211 && IDENTIFIER_POINTER (decl_name) != NULL)
22213 name = dwarf2_name (decl, 1);
22214 if (! DECL_ARTIFICIAL (decl))
22215 add_src_coords_attributes (seg_die, decl);
22217 add_linkage_name (seg_die, decl);
22219 gcc_assert (name != NULL);
22220 add_pure_or_virtual_attribute (seg_die, decl);
22221 if (DECL_ARTIFICIAL (decl))
22222 add_AT_flag (seg_die, DW_AT_artificial, 1);
22224 name = concat ("__second_sect_of_", name, NULL);
22225 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22226 fde->dw_fde_second_end, false);
22227 add_name_attribute (seg_die, name);
22228 if (want_pubnames ())
22229 add_pubname_string (name, seg_die);
22232 else
22233 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22234 false);
22237 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22239 /* We define the "frame base" as the function's CFA. This is more
22240 convenient for several reasons: (1) It's stable across the prologue
22241 and epilogue, which makes it better than just a frame pointer,
22242 (2) With dwarf3, there exists a one-byte encoding that allows us
22243 to reference the .debug_frame data by proxy, but failing that,
22244 (3) We can at least reuse the code inspection and interpretation
22245 code that determines the CFA position at various points in the
22246 function. */
22247 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22249 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22250 add_AT_loc (subr_die, DW_AT_frame_base, op);
22252 else
22254 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22255 if (list->dw_loc_next)
22256 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22257 else
22258 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22261 /* Compute a displacement from the "steady-state frame pointer" to
22262 the CFA. The former is what all stack slots and argument slots
22263 will reference in the rtl; the latter is what we've told the
22264 debugger about. We'll need to adjust all frame_base references
22265 by this displacement. */
22266 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22268 if (fun->static_chain_decl)
22270 /* DWARF requires here a location expression that computes the
22271 address of the enclosing subprogram's frame base. The machinery
22272 in tree-nested.c is supposed to store this specific address in the
22273 last field of the FRAME record. */
22274 const tree frame_type
22275 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22276 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22278 tree fb_expr
22279 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22280 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22281 fb_expr, fb_decl, NULL_TREE);
22283 add_AT_location_description (subr_die, DW_AT_static_link,
22284 loc_list_from_tree (fb_expr, 0, NULL));
22287 resolve_variable_values ();
22290 /* Generate child dies for template paramaters. */
22291 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22292 gen_generic_params_dies (decl);
22294 /* Now output descriptions of the arguments for this function. This gets
22295 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22296 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22297 `...' at the end of the formal parameter list. In order to find out if
22298 there was a trailing ellipsis or not, we must instead look at the type
22299 associated with the FUNCTION_DECL. This will be a node of type
22300 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22301 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22302 an ellipsis at the end. */
22304 /* In the case where we are describing a mere function declaration, all we
22305 need to do here (and all we *can* do here) is to describe the *types* of
22306 its formal parameters. */
22307 if (debug_info_level <= DINFO_LEVEL_TERSE)
22309 else if (declaration)
22310 gen_formal_types_die (decl, subr_die);
22311 else
22313 /* Generate DIEs to represent all known formal parameters. */
22314 tree parm = DECL_ARGUMENTS (decl);
22315 tree generic_decl = early_dwarf
22316 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22317 tree generic_decl_parm = generic_decl
22318 ? DECL_ARGUMENTS (generic_decl)
22319 : NULL;
22321 /* Now we want to walk the list of parameters of the function and
22322 emit their relevant DIEs.
22324 We consider the case of DECL being an instance of a generic function
22325 as well as it being a normal function.
22327 If DECL is an instance of a generic function we walk the
22328 parameters of the generic function declaration _and_ the parameters of
22329 DECL itself. This is useful because we want to emit specific DIEs for
22330 function parameter packs and those are declared as part of the
22331 generic function declaration. In that particular case,
22332 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22333 That DIE has children DIEs representing the set of arguments
22334 of the pack. Note that the set of pack arguments can be empty.
22335 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22336 children DIE.
22338 Otherwise, we just consider the parameters of DECL. */
22339 while (generic_decl_parm || parm)
22341 if (generic_decl_parm
22342 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22343 gen_formal_parameter_pack_die (generic_decl_parm,
22344 parm, subr_die,
22345 &parm);
22346 else if (parm && !POINTER_BOUNDS_P (parm))
22348 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22350 if (early_dwarf
22351 && parm == DECL_ARGUMENTS (decl)
22352 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22353 && parm_die
22354 && (dwarf_version >= 3 || !dwarf_strict))
22355 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22357 parm = DECL_CHAIN (parm);
22359 else if (parm)
22360 parm = DECL_CHAIN (parm);
22362 if (generic_decl_parm)
22363 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22366 /* Decide whether we need an unspecified_parameters DIE at the end.
22367 There are 2 more cases to do this for: 1) the ansi ... declaration -
22368 this is detectable when the end of the arg list is not a
22369 void_type_node 2) an unprototyped function declaration (not a
22370 definition). This just means that we have no info about the
22371 parameters at all. */
22372 if (early_dwarf)
22374 if (prototype_p (TREE_TYPE (decl)))
22376 /* This is the prototyped case, check for.... */
22377 if (stdarg_p (TREE_TYPE (decl)))
22378 gen_unspecified_parameters_die (decl, subr_die);
22380 else if (DECL_INITIAL (decl) == NULL_TREE)
22381 gen_unspecified_parameters_die (decl, subr_die);
22385 if (subr_die != old_die)
22386 /* Add the calling convention attribute if requested. */
22387 add_calling_convention_attribute (subr_die, decl);
22389 /* Output Dwarf info for all of the stuff within the body of the function
22390 (if it has one - it may be just a declaration).
22392 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22393 a function. This BLOCK actually represents the outermost binding contour
22394 for the function, i.e. the contour in which the function's formal
22395 parameters and labels get declared. Curiously, it appears that the front
22396 end doesn't actually put the PARM_DECL nodes for the current function onto
22397 the BLOCK_VARS list for this outer scope, but are strung off of the
22398 DECL_ARGUMENTS list for the function instead.
22400 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22401 the LABEL_DECL nodes for the function however, and we output DWARF info
22402 for those in decls_for_scope. Just within the `outer_scope' there will be
22403 a BLOCK node representing the function's outermost pair of curly braces,
22404 and any blocks used for the base and member initializers of a C++
22405 constructor function. */
22406 tree outer_scope = DECL_INITIAL (decl);
22407 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22409 int call_site_note_count = 0;
22410 int tail_call_site_note_count = 0;
22412 /* Emit a DW_TAG_variable DIE for a named return value. */
22413 if (DECL_NAME (DECL_RESULT (decl)))
22414 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22416 /* The first time through decls_for_scope we will generate the
22417 DIEs for the locals. The second time, we fill in the
22418 location info. */
22419 decls_for_scope (outer_scope, subr_die);
22421 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22423 struct call_arg_loc_node *ca_loc;
22424 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22426 dw_die_ref die = NULL;
22427 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22428 rtx arg, next_arg;
22430 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22431 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22432 : NULL_RTX);
22433 arg; arg = next_arg)
22435 dw_loc_descr_ref reg, val;
22436 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22437 dw_die_ref cdie, tdie = NULL;
22439 next_arg = XEXP (arg, 1);
22440 if (REG_P (XEXP (XEXP (arg, 0), 0))
22441 && next_arg
22442 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22443 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22444 && REGNO (XEXP (XEXP (arg, 0), 0))
22445 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22446 next_arg = XEXP (next_arg, 1);
22447 if (mode == VOIDmode)
22449 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22450 if (mode == VOIDmode)
22451 mode = GET_MODE (XEXP (arg, 0));
22453 if (mode == VOIDmode || mode == BLKmode)
22454 continue;
22455 /* Get dynamic information about call target only if we
22456 have no static information: we cannot generate both
22457 DW_AT_call_origin and DW_AT_call_target
22458 attributes. */
22459 if (ca_loc->symbol_ref == NULL_RTX)
22461 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22463 tloc = XEXP (XEXP (arg, 0), 1);
22464 continue;
22466 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22467 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22469 tlocc = XEXP (XEXP (arg, 0), 1);
22470 continue;
22473 reg = NULL;
22474 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22475 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22476 VAR_INIT_STATUS_INITIALIZED);
22477 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22479 rtx mem = XEXP (XEXP (arg, 0), 0);
22480 reg = mem_loc_descriptor (XEXP (mem, 0),
22481 get_address_mode (mem),
22482 GET_MODE (mem),
22483 VAR_INIT_STATUS_INITIALIZED);
22485 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22486 == DEBUG_PARAMETER_REF)
22488 tree tdecl
22489 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22490 tdie = lookup_decl_die (tdecl);
22491 if (tdie == NULL)
22492 continue;
22494 else
22495 continue;
22496 if (reg == NULL
22497 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22498 != DEBUG_PARAMETER_REF)
22499 continue;
22500 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22501 VOIDmode,
22502 VAR_INIT_STATUS_INITIALIZED);
22503 if (val == NULL)
22504 continue;
22505 if (die == NULL)
22506 die = gen_call_site_die (decl, subr_die, ca_loc);
22507 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22508 NULL_TREE);
22509 if (reg != NULL)
22510 add_AT_loc (cdie, DW_AT_location, reg);
22511 else if (tdie != NULL)
22512 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22513 tdie);
22514 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22515 if (next_arg != XEXP (arg, 1))
22517 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22518 if (mode == VOIDmode)
22519 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22520 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22521 0), 1),
22522 mode, VOIDmode,
22523 VAR_INIT_STATUS_INITIALIZED);
22524 if (val != NULL)
22525 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22526 val);
22529 if (die == NULL
22530 && (ca_loc->symbol_ref || tloc))
22531 die = gen_call_site_die (decl, subr_die, ca_loc);
22532 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22534 dw_loc_descr_ref tval = NULL;
22536 if (tloc != NULL_RTX)
22537 tval = mem_loc_descriptor (tloc,
22538 GET_MODE (tloc) == VOIDmode
22539 ? Pmode : GET_MODE (tloc),
22540 VOIDmode,
22541 VAR_INIT_STATUS_INITIALIZED);
22542 if (tval)
22543 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22544 else if (tlocc != NULL_RTX)
22546 tval = mem_loc_descriptor (tlocc,
22547 GET_MODE (tlocc) == VOIDmode
22548 ? Pmode : GET_MODE (tlocc),
22549 VOIDmode,
22550 VAR_INIT_STATUS_INITIALIZED);
22551 if (tval)
22552 add_AT_loc (die,
22553 dwarf_AT (DW_AT_call_target_clobbered),
22554 tval);
22557 if (die != NULL)
22559 call_site_note_count++;
22560 if (ca_loc->tail_call_p)
22561 tail_call_site_note_count++;
22565 call_arg_locations = NULL;
22566 call_arg_loc_last = NULL;
22567 if (tail_call_site_count >= 0
22568 && tail_call_site_count == tail_call_site_note_count
22569 && (!dwarf_strict || dwarf_version >= 5))
22571 if (call_site_count >= 0
22572 && call_site_count == call_site_note_count)
22573 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22574 else
22575 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22577 call_site_count = -1;
22578 tail_call_site_count = -1;
22581 /* Mark used types after we have created DIEs for the functions scopes. */
22582 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22585 /* Returns a hash value for X (which really is a die_struct). */
22587 hashval_t
22588 block_die_hasher::hash (die_struct *d)
22590 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22593 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22594 as decl_id and die_parent of die_struct Y. */
22596 bool
22597 block_die_hasher::equal (die_struct *x, die_struct *y)
22599 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22602 /* Return TRUE if DECL, which may have been previously generated as
22603 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22604 true if decl (or its origin) is either an extern declaration or a
22605 class/namespace scoped declaration.
22607 The declare_in_namespace support causes us to get two DIEs for one
22608 variable, both of which are declarations. We want to avoid
22609 considering one to be a specification, so we must test for
22610 DECLARATION and DW_AT_declaration. */
22611 static inline bool
22612 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22614 return (old_die && TREE_STATIC (decl) && !declaration
22615 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22618 /* Return true if DECL is a local static. */
22620 static inline bool
22621 local_function_static (tree decl)
22623 gcc_assert (VAR_P (decl));
22624 return TREE_STATIC (decl)
22625 && DECL_CONTEXT (decl)
22626 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22629 /* Generate a DIE to represent a declared data object.
22630 Either DECL or ORIGIN must be non-null. */
22632 static void
22633 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22635 HOST_WIDE_INT off = 0;
22636 tree com_decl;
22637 tree decl_or_origin = decl ? decl : origin;
22638 tree ultimate_origin;
22639 dw_die_ref var_die;
22640 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22641 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22642 || class_or_namespace_scope_p (context_die));
22643 bool specialization_p = false;
22644 bool no_linkage_name = false;
22646 /* While C++ inline static data members have definitions inside of the
22647 class, force the first DIE to be a declaration, then let gen_member_die
22648 reparent it to the class context and call gen_variable_die again
22649 to create the outside of the class DIE for the definition. */
22650 if (!declaration
22651 && old_die == NULL
22652 && decl
22653 && DECL_CONTEXT (decl)
22654 && TYPE_P (DECL_CONTEXT (decl))
22655 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22657 declaration = true;
22658 if (dwarf_version < 5)
22659 no_linkage_name = true;
22662 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22663 if (decl || ultimate_origin)
22664 origin = ultimate_origin;
22665 com_decl = fortran_common (decl_or_origin, &off);
22667 /* Symbol in common gets emitted as a child of the common block, in the form
22668 of a data member. */
22669 if (com_decl)
22671 dw_die_ref com_die;
22672 dw_loc_list_ref loc = NULL;
22673 die_node com_die_arg;
22675 var_die = lookup_decl_die (decl_or_origin);
22676 if (var_die)
22678 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22680 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22681 if (loc)
22683 if (off)
22685 /* Optimize the common case. */
22686 if (single_element_loc_list_p (loc)
22687 && loc->expr->dw_loc_opc == DW_OP_addr
22688 && loc->expr->dw_loc_next == NULL
22689 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22690 == SYMBOL_REF)
22692 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22693 loc->expr->dw_loc_oprnd1.v.val_addr
22694 = plus_constant (GET_MODE (x), x , off);
22696 else
22697 loc_list_plus_const (loc, off);
22699 add_AT_location_description (var_die, DW_AT_location, loc);
22700 remove_AT (var_die, DW_AT_declaration);
22703 return;
22706 if (common_block_die_table == NULL)
22707 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22709 com_die_arg.decl_id = DECL_UID (com_decl);
22710 com_die_arg.die_parent = context_die;
22711 com_die = common_block_die_table->find (&com_die_arg);
22712 if (! early_dwarf)
22713 loc = loc_list_from_tree (com_decl, 2, NULL);
22714 if (com_die == NULL)
22716 const char *cnam
22717 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22718 die_node **slot;
22720 com_die = new_die (DW_TAG_common_block, context_die, decl);
22721 add_name_and_src_coords_attributes (com_die, com_decl);
22722 if (loc)
22724 add_AT_location_description (com_die, DW_AT_location, loc);
22725 /* Avoid sharing the same loc descriptor between
22726 DW_TAG_common_block and DW_TAG_variable. */
22727 loc = loc_list_from_tree (com_decl, 2, NULL);
22729 else if (DECL_EXTERNAL (decl_or_origin))
22730 add_AT_flag (com_die, DW_AT_declaration, 1);
22731 if (want_pubnames ())
22732 add_pubname_string (cnam, com_die); /* ??? needed? */
22733 com_die->decl_id = DECL_UID (com_decl);
22734 slot = common_block_die_table->find_slot (com_die, INSERT);
22735 *slot = com_die;
22737 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22739 add_AT_location_description (com_die, DW_AT_location, loc);
22740 loc = loc_list_from_tree (com_decl, 2, NULL);
22741 remove_AT (com_die, DW_AT_declaration);
22743 var_die = new_die (DW_TAG_variable, com_die, decl);
22744 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22745 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22746 decl_quals (decl_or_origin), false,
22747 context_die);
22748 add_alignment_attribute (var_die, decl);
22749 add_AT_flag (var_die, DW_AT_external, 1);
22750 if (loc)
22752 if (off)
22754 /* Optimize the common case. */
22755 if (single_element_loc_list_p (loc)
22756 && loc->expr->dw_loc_opc == DW_OP_addr
22757 && loc->expr->dw_loc_next == NULL
22758 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22760 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22761 loc->expr->dw_loc_oprnd1.v.val_addr
22762 = plus_constant (GET_MODE (x), x, off);
22764 else
22765 loc_list_plus_const (loc, off);
22767 add_AT_location_description (var_die, DW_AT_location, loc);
22769 else if (DECL_EXTERNAL (decl_or_origin))
22770 add_AT_flag (var_die, DW_AT_declaration, 1);
22771 if (decl)
22772 equate_decl_number_to_die (decl, var_die);
22773 return;
22776 if (old_die)
22778 if (declaration)
22780 /* A declaration that has been previously dumped, needs no
22781 further annotations, since it doesn't need location on
22782 the second pass. */
22783 return;
22785 else if (decl_will_get_specification_p (old_die, decl, declaration)
22786 && !get_AT (old_die, DW_AT_specification))
22788 /* Fall-thru so we can make a new variable die along with a
22789 DW_AT_specification. */
22791 else if (origin && old_die->die_parent != context_die)
22793 /* If we will be creating an inlined instance, we need a
22794 new DIE that will get annotated with
22795 DW_AT_abstract_origin. Clear things so we can get a
22796 new DIE. */
22797 gcc_assert (!DECL_ABSTRACT_P (decl));
22798 old_die = NULL;
22800 else
22802 /* If a DIE was dumped early, it still needs location info.
22803 Skip to where we fill the location bits. */
22804 var_die = old_die;
22806 /* ??? In LTRANS we cannot annotate early created variably
22807 modified type DIEs without copying them and adjusting all
22808 references to them. Thus we dumped them again, also add a
22809 reference to them. */
22810 tree type = TREE_TYPE (decl_or_origin);
22811 if (in_lto_p
22812 && variably_modified_type_p
22813 (type, decl_function_context (decl_or_origin)))
22815 if (decl_by_reference_p (decl_or_origin))
22816 add_type_attribute (var_die, TREE_TYPE (type),
22817 TYPE_UNQUALIFIED, false, context_die);
22818 else
22819 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22820 false, context_die);
22823 goto gen_variable_die_location;
22827 /* For static data members, the declaration in the class is supposed
22828 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22829 also in DWARF2; the specification should still be DW_TAG_variable
22830 referencing the DW_TAG_member DIE. */
22831 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22832 var_die = new_die (DW_TAG_member, context_die, decl);
22833 else
22834 var_die = new_die (DW_TAG_variable, context_die, decl);
22836 if (origin != NULL)
22837 add_abstract_origin_attribute (var_die, origin);
22839 /* Loop unrolling can create multiple blocks that refer to the same
22840 static variable, so we must test for the DW_AT_declaration flag.
22842 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22843 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22844 sharing them.
22846 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22847 else if (decl_will_get_specification_p (old_die, decl, declaration))
22849 /* This is a definition of a C++ class level static. */
22850 add_AT_specification (var_die, old_die);
22851 specialization_p = true;
22852 if (DECL_NAME (decl))
22854 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22855 struct dwarf_file_data * file_index = lookup_filename (s.file);
22857 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22858 add_AT_file (var_die, DW_AT_decl_file, file_index);
22860 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22861 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22863 if (debug_column_info
22864 && s.column
22865 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22866 != (unsigned) s.column))
22867 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22869 if (old_die->die_tag == DW_TAG_member)
22870 add_linkage_name (var_die, decl);
22873 else
22874 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22876 if ((origin == NULL && !specialization_p)
22877 || (origin != NULL
22878 && !DECL_ABSTRACT_P (decl_or_origin)
22879 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22880 decl_function_context
22881 (decl_or_origin))))
22883 tree type = TREE_TYPE (decl_or_origin);
22885 if (decl_by_reference_p (decl_or_origin))
22886 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22887 context_die);
22888 else
22889 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22890 context_die);
22893 if (origin == NULL && !specialization_p)
22895 if (TREE_PUBLIC (decl))
22896 add_AT_flag (var_die, DW_AT_external, 1);
22898 if (DECL_ARTIFICIAL (decl))
22899 add_AT_flag (var_die, DW_AT_artificial, 1);
22901 add_alignment_attribute (var_die, decl);
22903 add_accessibility_attribute (var_die, decl);
22906 if (declaration)
22907 add_AT_flag (var_die, DW_AT_declaration, 1);
22909 if (decl && (DECL_ABSTRACT_P (decl)
22910 || !old_die || is_declaration_die (old_die)))
22911 equate_decl_number_to_die (decl, var_die);
22913 gen_variable_die_location:
22914 if (! declaration
22915 && (! DECL_ABSTRACT_P (decl_or_origin)
22916 /* Local static vars are shared between all clones/inlines,
22917 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22918 already set. */
22919 || (VAR_P (decl_or_origin)
22920 && TREE_STATIC (decl_or_origin)
22921 && DECL_RTL_SET_P (decl_or_origin))))
22923 if (early_dwarf)
22924 add_pubname (decl_or_origin, var_die);
22925 else
22926 add_location_or_const_value_attribute (var_die, decl_or_origin,
22927 decl == NULL);
22929 else
22930 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22932 if ((dwarf_version >= 4 || !dwarf_strict)
22933 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22934 DW_AT_const_expr) == 1
22935 && !get_AT (var_die, DW_AT_const_expr)
22936 && !specialization_p)
22937 add_AT_flag (var_die, DW_AT_const_expr, 1);
22939 if (!dwarf_strict)
22941 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22942 DW_AT_inline);
22943 if (inl != -1
22944 && !get_AT (var_die, DW_AT_inline)
22945 && !specialization_p)
22946 add_AT_unsigned (var_die, DW_AT_inline, inl);
22950 /* Generate a DIE to represent a named constant. */
22952 static void
22953 gen_const_die (tree decl, dw_die_ref context_die)
22955 dw_die_ref const_die;
22956 tree type = TREE_TYPE (decl);
22958 const_die = lookup_decl_die (decl);
22959 if (const_die)
22960 return;
22962 const_die = new_die (DW_TAG_constant, context_die, decl);
22963 equate_decl_number_to_die (decl, const_die);
22964 add_name_and_src_coords_attributes (const_die, decl);
22965 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22966 if (TREE_PUBLIC (decl))
22967 add_AT_flag (const_die, DW_AT_external, 1);
22968 if (DECL_ARTIFICIAL (decl))
22969 add_AT_flag (const_die, DW_AT_artificial, 1);
22970 tree_add_const_value_attribute_for_decl (const_die, decl);
22973 /* Generate a DIE to represent a label identifier. */
22975 static void
22976 gen_label_die (tree decl, dw_die_ref context_die)
22978 tree origin = decl_ultimate_origin (decl);
22979 dw_die_ref lbl_die = lookup_decl_die (decl);
22980 rtx insn;
22981 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22983 if (!lbl_die)
22985 lbl_die = new_die (DW_TAG_label, context_die, decl);
22986 equate_decl_number_to_die (decl, lbl_die);
22988 if (origin != NULL)
22989 add_abstract_origin_attribute (lbl_die, origin);
22990 else
22991 add_name_and_src_coords_attributes (lbl_die, decl);
22994 if (DECL_ABSTRACT_P (decl))
22995 equate_decl_number_to_die (decl, lbl_die);
22996 else if (! early_dwarf)
22998 insn = DECL_RTL_IF_SET (decl);
23000 /* Deleted labels are programmer specified labels which have been
23001 eliminated because of various optimizations. We still emit them
23002 here so that it is possible to put breakpoints on them. */
23003 if (insn
23004 && (LABEL_P (insn)
23005 || ((NOTE_P (insn)
23006 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23008 /* When optimization is enabled (via -O) some parts of the compiler
23009 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23010 represent source-level labels which were explicitly declared by
23011 the user. This really shouldn't be happening though, so catch
23012 it if it ever does happen. */
23013 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23015 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23016 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23018 else if (insn
23019 && NOTE_P (insn)
23020 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23021 && CODE_LABEL_NUMBER (insn) != -1)
23023 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23024 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23029 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23030 attributes to the DIE for a block STMT, to describe where the inlined
23031 function was called from. This is similar to add_src_coords_attributes. */
23033 static inline void
23034 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23036 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23038 if (dwarf_version >= 3 || !dwarf_strict)
23040 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23041 add_AT_unsigned (die, DW_AT_call_line, s.line);
23042 if (debug_column_info && s.column)
23043 add_AT_unsigned (die, DW_AT_call_column, s.column);
23048 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23049 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23051 static inline void
23052 add_high_low_attributes (tree stmt, dw_die_ref die)
23054 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23056 if (BLOCK_FRAGMENT_CHAIN (stmt)
23057 && (dwarf_version >= 3 || !dwarf_strict))
23059 tree chain, superblock = NULL_TREE;
23060 dw_die_ref pdie;
23061 dw_attr_node *attr = NULL;
23063 if (inlined_function_outer_scope_p (stmt))
23065 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23066 BLOCK_NUMBER (stmt));
23067 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23070 /* Optimize duplicate .debug_ranges lists or even tails of
23071 lists. If this BLOCK has same ranges as its supercontext,
23072 lookup DW_AT_ranges attribute in the supercontext (and
23073 recursively so), verify that the ranges_table contains the
23074 right values and use it instead of adding a new .debug_range. */
23075 for (chain = stmt, pdie = die;
23076 BLOCK_SAME_RANGE (chain);
23077 chain = BLOCK_SUPERCONTEXT (chain))
23079 dw_attr_node *new_attr;
23081 pdie = pdie->die_parent;
23082 if (pdie == NULL)
23083 break;
23084 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23085 break;
23086 new_attr = get_AT (pdie, DW_AT_ranges);
23087 if (new_attr == NULL
23088 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23089 break;
23090 attr = new_attr;
23091 superblock = BLOCK_SUPERCONTEXT (chain);
23093 if (attr != NULL
23094 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23095 == BLOCK_NUMBER (superblock))
23096 && BLOCK_FRAGMENT_CHAIN (superblock))
23098 unsigned long off = attr->dw_attr_val.v.val_offset;
23099 unsigned long supercnt = 0, thiscnt = 0;
23100 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23101 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23103 ++supercnt;
23104 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23105 == BLOCK_NUMBER (chain));
23107 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23108 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23109 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23110 ++thiscnt;
23111 gcc_assert (supercnt >= thiscnt);
23112 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23113 false);
23114 note_rnglist_head (off + supercnt - thiscnt);
23115 return;
23118 unsigned int offset = add_ranges (stmt, true);
23119 add_AT_range_list (die, DW_AT_ranges, offset, false);
23120 note_rnglist_head (offset);
23122 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23123 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23126 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23127 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23128 chain = BLOCK_FRAGMENT_CHAIN (chain);
23130 while (chain);
23131 add_ranges (NULL);
23133 else
23135 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23136 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23137 BLOCK_NUMBER (stmt));
23138 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23139 BLOCK_NUMBER (stmt));
23140 add_AT_low_high_pc (die, label, label_high, false);
23144 /* Generate a DIE for a lexical block. */
23146 static void
23147 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23149 dw_die_ref old_die = BLOCK_DIE (stmt);
23150 dw_die_ref stmt_die = NULL;
23151 if (!old_die)
23153 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23154 BLOCK_DIE (stmt) = stmt_die;
23157 if (BLOCK_ABSTRACT (stmt))
23159 if (old_die)
23161 /* This must have been generated early and it won't even
23162 need location information since it's a DW_AT_inline
23163 function. */
23164 if (flag_checking)
23165 for (dw_die_ref c = context_die; c; c = c->die_parent)
23166 if (c->die_tag == DW_TAG_inlined_subroutine
23167 || c->die_tag == DW_TAG_subprogram)
23169 gcc_assert (get_AT (c, DW_AT_inline));
23170 break;
23172 return;
23175 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23177 /* If this is an inlined instance, create a new lexical die for
23178 anything below to attach DW_AT_abstract_origin to. */
23179 if (old_die)
23181 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23182 BLOCK_DIE (stmt) = stmt_die;
23183 old_die = NULL;
23186 tree origin = block_ultimate_origin (stmt);
23187 if (origin != NULL_TREE && origin != stmt)
23188 add_abstract_origin_attribute (stmt_die, origin);
23191 if (old_die)
23192 stmt_die = old_die;
23194 /* A non abstract block whose blocks have already been reordered
23195 should have the instruction range for this block. If so, set the
23196 high/low attributes. */
23197 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23199 gcc_assert (stmt_die);
23200 add_high_low_attributes (stmt, stmt_die);
23203 decls_for_scope (stmt, stmt_die);
23206 /* Generate a DIE for an inlined subprogram. */
23208 static void
23209 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23211 tree decl;
23213 /* The instance of function that is effectively being inlined shall not
23214 be abstract. */
23215 gcc_assert (! BLOCK_ABSTRACT (stmt));
23217 decl = block_ultimate_origin (stmt);
23219 /* Make sure any inlined functions are known to be inlineable. */
23220 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23221 || cgraph_function_possibly_inlined_p (decl));
23223 if (! BLOCK_ABSTRACT (stmt))
23225 dw_die_ref subr_die
23226 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23228 if (call_arg_locations)
23229 BLOCK_DIE (stmt) = subr_die;
23230 add_abstract_origin_attribute (subr_die, decl);
23231 if (TREE_ASM_WRITTEN (stmt))
23232 add_high_low_attributes (stmt, subr_die);
23233 add_call_src_coords_attributes (stmt, subr_die);
23235 decls_for_scope (stmt, subr_die);
23239 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23240 the comment for VLR_CONTEXT. */
23242 static void
23243 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23245 dw_die_ref decl_die;
23247 if (TREE_TYPE (decl) == error_mark_node)
23248 return;
23250 decl_die = new_die (DW_TAG_member, context_die, decl);
23251 add_name_and_src_coords_attributes (decl_die, decl);
23252 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23253 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23254 context_die);
23256 if (DECL_BIT_FIELD_TYPE (decl))
23258 add_byte_size_attribute (decl_die, decl);
23259 add_bit_size_attribute (decl_die, decl);
23260 add_bit_offset_attribute (decl_die, decl, ctx);
23263 add_alignment_attribute (decl_die, decl);
23265 /* If we have a variant part offset, then we are supposed to process a member
23266 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23267 trees. */
23268 gcc_assert (ctx->variant_part_offset == NULL_TREE
23269 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23270 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23271 add_data_member_location_attribute (decl_die, decl, ctx);
23273 if (DECL_ARTIFICIAL (decl))
23274 add_AT_flag (decl_die, DW_AT_artificial, 1);
23276 add_accessibility_attribute (decl_die, decl);
23278 /* Equate decl number to die, so that we can look up this decl later on. */
23279 equate_decl_number_to_die (decl, decl_die);
23282 /* Generate a DIE for a pointer to a member type. TYPE can be an
23283 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23284 pointer to member function. */
23286 static void
23287 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23289 if (lookup_type_die (type))
23290 return;
23292 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23293 scope_die_for (type, context_die), type);
23295 equate_type_number_to_die (type, ptr_die);
23296 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23297 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23298 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23299 context_die);
23300 add_alignment_attribute (ptr_die, type);
23302 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23303 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23305 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23306 add_AT_loc (ptr_die, DW_AT_use_location, op);
23310 static char *producer_string;
23312 /* Return a heap allocated producer string including command line options
23313 if -grecord-gcc-switches. */
23315 static char *
23316 gen_producer_string (void)
23318 size_t j;
23319 auto_vec<const char *> switches;
23320 const char *language_string = lang_hooks.name;
23321 char *producer, *tail;
23322 const char *p;
23323 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23324 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23326 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23327 switch (save_decoded_options[j].opt_index)
23329 case OPT_o:
23330 case OPT_d:
23331 case OPT_dumpbase:
23332 case OPT_dumpdir:
23333 case OPT_auxbase:
23334 case OPT_auxbase_strip:
23335 case OPT_quiet:
23336 case OPT_version:
23337 case OPT_v:
23338 case OPT_w:
23339 case OPT_L:
23340 case OPT_D:
23341 case OPT_I:
23342 case OPT_U:
23343 case OPT_SPECIAL_unknown:
23344 case OPT_SPECIAL_ignore:
23345 case OPT_SPECIAL_program_name:
23346 case OPT_SPECIAL_input_file:
23347 case OPT_grecord_gcc_switches:
23348 case OPT__output_pch_:
23349 case OPT_fdiagnostics_show_location_:
23350 case OPT_fdiagnostics_show_option:
23351 case OPT_fdiagnostics_show_caret:
23352 case OPT_fdiagnostics_color_:
23353 case OPT_fverbose_asm:
23354 case OPT____:
23355 case OPT__sysroot_:
23356 case OPT_nostdinc:
23357 case OPT_nostdinc__:
23358 case OPT_fpreprocessed:
23359 case OPT_fltrans_output_list_:
23360 case OPT_fresolution_:
23361 case OPT_fdebug_prefix_map_:
23362 /* Ignore these. */
23363 continue;
23364 default:
23365 if (cl_options[save_decoded_options[j].opt_index].flags
23366 & CL_NO_DWARF_RECORD)
23367 continue;
23368 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23369 == '-');
23370 switch (save_decoded_options[j].canonical_option[0][1])
23372 case 'M':
23373 case 'i':
23374 case 'W':
23375 continue;
23376 case 'f':
23377 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23378 "dump", 4) == 0)
23379 continue;
23380 break;
23381 default:
23382 break;
23384 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23385 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23386 break;
23389 producer = XNEWVEC (char, plen + 1 + len + 1);
23390 tail = producer;
23391 sprintf (tail, "%s %s", language_string, version_string);
23392 tail += plen;
23394 FOR_EACH_VEC_ELT (switches, j, p)
23396 len = strlen (p);
23397 *tail = ' ';
23398 memcpy (tail + 1, p, len);
23399 tail += len + 1;
23402 *tail = '\0';
23403 return producer;
23406 /* Given a C and/or C++ language/version string return the "highest".
23407 C++ is assumed to be "higher" than C in this case. Used for merging
23408 LTO translation unit languages. */
23409 static const char *
23410 highest_c_language (const char *lang1, const char *lang2)
23412 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23413 return "GNU C++17";
23414 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23415 return "GNU C++14";
23416 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23417 return "GNU C++11";
23418 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23419 return "GNU C++98";
23421 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23422 return "GNU C11";
23423 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23424 return "GNU C99";
23425 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23426 return "GNU C89";
23428 gcc_unreachable ();
23432 /* Generate the DIE for the compilation unit. */
23434 static dw_die_ref
23435 gen_compile_unit_die (const char *filename)
23437 dw_die_ref die;
23438 const char *language_string = lang_hooks.name;
23439 int language;
23441 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23443 if (filename)
23445 add_name_attribute (die, filename);
23446 /* Don't add cwd for <built-in>. */
23447 if (filename[0] != '<')
23448 add_comp_dir_attribute (die);
23451 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23453 /* If our producer is LTO try to figure out a common language to use
23454 from the global list of translation units. */
23455 if (strcmp (language_string, "GNU GIMPLE") == 0)
23457 unsigned i;
23458 tree t;
23459 const char *common_lang = NULL;
23461 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23463 if (!TRANSLATION_UNIT_LANGUAGE (t))
23464 continue;
23465 if (!common_lang)
23466 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23467 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23469 else if (strncmp (common_lang, "GNU C", 5) == 0
23470 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23471 /* Mixing C and C++ is ok, use C++ in that case. */
23472 common_lang = highest_c_language (common_lang,
23473 TRANSLATION_UNIT_LANGUAGE (t));
23474 else
23476 /* Fall back to C. */
23477 common_lang = NULL;
23478 break;
23482 if (common_lang)
23483 language_string = common_lang;
23486 language = DW_LANG_C;
23487 if (strncmp (language_string, "GNU C", 5) == 0
23488 && ISDIGIT (language_string[5]))
23490 language = DW_LANG_C89;
23491 if (dwarf_version >= 3 || !dwarf_strict)
23493 if (strcmp (language_string, "GNU C89") != 0)
23494 language = DW_LANG_C99;
23496 if (dwarf_version >= 5 /* || !dwarf_strict */)
23497 if (strcmp (language_string, "GNU C11") == 0)
23498 language = DW_LANG_C11;
23501 else if (strncmp (language_string, "GNU C++", 7) == 0)
23503 language = DW_LANG_C_plus_plus;
23504 if (dwarf_version >= 5 /* || !dwarf_strict */)
23506 if (strcmp (language_string, "GNU C++11") == 0)
23507 language = DW_LANG_C_plus_plus_11;
23508 else if (strcmp (language_string, "GNU C++14") == 0)
23509 language = DW_LANG_C_plus_plus_14;
23510 else if (strcmp (language_string, "GNU C++17") == 0)
23511 /* For now. */
23512 language = DW_LANG_C_plus_plus_14;
23515 else if (strcmp (language_string, "GNU F77") == 0)
23516 language = DW_LANG_Fortran77;
23517 else if (dwarf_version >= 3 || !dwarf_strict)
23519 if (strcmp (language_string, "GNU Ada") == 0)
23520 language = DW_LANG_Ada95;
23521 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23523 language = DW_LANG_Fortran95;
23524 if (dwarf_version >= 5 /* || !dwarf_strict */)
23526 if (strcmp (language_string, "GNU Fortran2003") == 0)
23527 language = DW_LANG_Fortran03;
23528 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23529 language = DW_LANG_Fortran08;
23532 else if (strcmp (language_string, "GNU Objective-C") == 0)
23533 language = DW_LANG_ObjC;
23534 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23535 language = DW_LANG_ObjC_plus_plus;
23536 else if (dwarf_version >= 5 || !dwarf_strict)
23538 if (strcmp (language_string, "GNU Go") == 0)
23539 language = DW_LANG_Go;
23542 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23543 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23544 language = DW_LANG_Fortran90;
23546 add_AT_unsigned (die, DW_AT_language, language);
23548 switch (language)
23550 case DW_LANG_Fortran77:
23551 case DW_LANG_Fortran90:
23552 case DW_LANG_Fortran95:
23553 case DW_LANG_Fortran03:
23554 case DW_LANG_Fortran08:
23555 /* Fortran has case insensitive identifiers and the front-end
23556 lowercases everything. */
23557 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23558 break;
23559 default:
23560 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23561 break;
23563 return die;
23566 /* Generate the DIE for a base class. */
23568 static void
23569 gen_inheritance_die (tree binfo, tree access, tree type,
23570 dw_die_ref context_die)
23572 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23573 struct vlr_context ctx = { type, NULL };
23575 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23576 context_die);
23577 add_data_member_location_attribute (die, binfo, &ctx);
23579 if (BINFO_VIRTUAL_P (binfo))
23580 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23582 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23583 children, otherwise the default is DW_ACCESS_public. In DWARF2
23584 the default has always been DW_ACCESS_private. */
23585 if (access == access_public_node)
23587 if (dwarf_version == 2
23588 || context_die->die_tag == DW_TAG_class_type)
23589 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23591 else if (access == access_protected_node)
23592 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23593 else if (dwarf_version > 2
23594 && context_die->die_tag != DW_TAG_class_type)
23595 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23598 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23599 structure. */
23600 static bool
23601 is_variant_part (tree decl)
23603 return (TREE_CODE (decl) == FIELD_DECL
23604 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23607 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23608 return the FIELD_DECL. Return NULL_TREE otherwise. */
23610 static tree
23611 analyze_discr_in_predicate (tree operand, tree struct_type)
23613 bool continue_stripping = true;
23614 while (continue_stripping)
23615 switch (TREE_CODE (operand))
23617 CASE_CONVERT:
23618 operand = TREE_OPERAND (operand, 0);
23619 break;
23620 default:
23621 continue_stripping = false;
23622 break;
23625 /* Match field access to members of struct_type only. */
23626 if (TREE_CODE (operand) == COMPONENT_REF
23627 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23628 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23629 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23630 return TREE_OPERAND (operand, 1);
23631 else
23632 return NULL_TREE;
23635 /* Check that SRC is a constant integer that can be represented as a native
23636 integer constant (either signed or unsigned). If so, store it into DEST and
23637 return true. Return false otherwise. */
23639 static bool
23640 get_discr_value (tree src, dw_discr_value *dest)
23642 tree discr_type = TREE_TYPE (src);
23644 if (lang_hooks.types.get_debug_type)
23646 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23647 if (debug_type != NULL)
23648 discr_type = debug_type;
23651 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23652 return false;
23654 /* Signedness can vary between the original type and the debug type. This
23655 can happen for character types in Ada for instance: the character type
23656 used for code generation can be signed, to be compatible with the C one,
23657 but from a debugger point of view, it must be unsigned. */
23658 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23659 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23661 if (is_orig_unsigned != is_debug_unsigned)
23662 src = fold_convert (discr_type, src);
23664 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23665 return false;
23667 dest->pos = is_debug_unsigned;
23668 if (is_debug_unsigned)
23669 dest->v.uval = tree_to_uhwi (src);
23670 else
23671 dest->v.sval = tree_to_shwi (src);
23673 return true;
23676 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23677 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23678 store NULL_TREE in DISCR_DECL. Otherwise:
23680 - store the discriminant field in STRUCT_TYPE that controls the variant
23681 part to *DISCR_DECL
23683 - put in *DISCR_LISTS_P an array where for each variant, the item
23684 represents the corresponding matching list of discriminant values.
23686 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23687 the above array.
23689 Note that when the array is allocated (i.e. when the analysis is
23690 successful), it is up to the caller to free the array. */
23692 static void
23693 analyze_variants_discr (tree variant_part_decl,
23694 tree struct_type,
23695 tree *discr_decl,
23696 dw_discr_list_ref **discr_lists_p,
23697 unsigned *discr_lists_length)
23699 tree variant_part_type = TREE_TYPE (variant_part_decl);
23700 tree variant;
23701 dw_discr_list_ref *discr_lists;
23702 unsigned i;
23704 /* Compute how many variants there are in this variant part. */
23705 *discr_lists_length = 0;
23706 for (variant = TYPE_FIELDS (variant_part_type);
23707 variant != NULL_TREE;
23708 variant = DECL_CHAIN (variant))
23709 ++*discr_lists_length;
23711 *discr_decl = NULL_TREE;
23712 *discr_lists_p
23713 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23714 sizeof (**discr_lists_p));
23715 discr_lists = *discr_lists_p;
23717 /* And then analyze all variants to extract discriminant information for all
23718 of them. This analysis is conservative: as soon as we detect something we
23719 do not support, abort everything and pretend we found nothing. */
23720 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23721 variant != NULL_TREE;
23722 variant = DECL_CHAIN (variant), ++i)
23724 tree match_expr = DECL_QUALIFIER (variant);
23726 /* Now, try to analyze the predicate and deduce a discriminant for
23727 it. */
23728 if (match_expr == boolean_true_node)
23729 /* Typically happens for the default variant: it matches all cases that
23730 previous variants rejected. Don't output any matching value for
23731 this one. */
23732 continue;
23734 /* The following loop tries to iterate over each discriminant
23735 possibility: single values or ranges. */
23736 while (match_expr != NULL_TREE)
23738 tree next_round_match_expr;
23739 tree candidate_discr = NULL_TREE;
23740 dw_discr_list_ref new_node = NULL;
23742 /* Possibilities are matched one after the other by nested
23743 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23744 continue with the rest at next iteration. */
23745 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23747 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23748 match_expr = TREE_OPERAND (match_expr, 1);
23750 else
23751 next_round_match_expr = NULL_TREE;
23753 if (match_expr == boolean_false_node)
23754 /* This sub-expression matches nothing: just wait for the next
23755 one. */
23758 else if (TREE_CODE (match_expr) == EQ_EXPR)
23760 /* We are matching: <discr_field> == <integer_cst>
23761 This sub-expression matches a single value. */
23762 tree integer_cst = TREE_OPERAND (match_expr, 1);
23764 candidate_discr
23765 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23766 struct_type);
23768 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23769 if (!get_discr_value (integer_cst,
23770 &new_node->dw_discr_lower_bound))
23771 goto abort;
23772 new_node->dw_discr_range = false;
23775 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23777 /* We are matching:
23778 <discr_field> > <integer_cst>
23779 && <discr_field> < <integer_cst>.
23780 This sub-expression matches the range of values between the
23781 two matched integer constants. Note that comparisons can be
23782 inclusive or exclusive. */
23783 tree candidate_discr_1, candidate_discr_2;
23784 tree lower_cst, upper_cst;
23785 bool lower_cst_included, upper_cst_included;
23786 tree lower_op = TREE_OPERAND (match_expr, 0);
23787 tree upper_op = TREE_OPERAND (match_expr, 1);
23789 /* When the comparison is exclusive, the integer constant is not
23790 the discriminant range bound we are looking for: we will have
23791 to increment or decrement it. */
23792 if (TREE_CODE (lower_op) == GE_EXPR)
23793 lower_cst_included = true;
23794 else if (TREE_CODE (lower_op) == GT_EXPR)
23795 lower_cst_included = false;
23796 else
23797 goto abort;
23799 if (TREE_CODE (upper_op) == LE_EXPR)
23800 upper_cst_included = true;
23801 else if (TREE_CODE (upper_op) == LT_EXPR)
23802 upper_cst_included = false;
23803 else
23804 goto abort;
23806 /* Extract the discriminant from the first operand and check it
23807 is consistant with the same analysis in the second
23808 operand. */
23809 candidate_discr_1
23810 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23811 struct_type);
23812 candidate_discr_2
23813 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23814 struct_type);
23815 if (candidate_discr_1 == candidate_discr_2)
23816 candidate_discr = candidate_discr_1;
23817 else
23818 goto abort;
23820 /* Extract bounds from both. */
23821 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23822 lower_cst = TREE_OPERAND (lower_op, 1);
23823 upper_cst = TREE_OPERAND (upper_op, 1);
23825 if (!lower_cst_included)
23826 lower_cst
23827 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23828 build_int_cst (TREE_TYPE (lower_cst), 1));
23829 if (!upper_cst_included)
23830 upper_cst
23831 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23832 build_int_cst (TREE_TYPE (upper_cst), 1));
23834 if (!get_discr_value (lower_cst,
23835 &new_node->dw_discr_lower_bound)
23836 || !get_discr_value (upper_cst,
23837 &new_node->dw_discr_upper_bound))
23838 goto abort;
23840 new_node->dw_discr_range = true;
23843 else
23844 /* Unsupported sub-expression: we cannot determine the set of
23845 matching discriminant values. Abort everything. */
23846 goto abort;
23848 /* If the discriminant info is not consistant with what we saw so
23849 far, consider the analysis failed and abort everything. */
23850 if (candidate_discr == NULL_TREE
23851 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23852 goto abort;
23853 else
23854 *discr_decl = candidate_discr;
23856 if (new_node != NULL)
23858 new_node->dw_discr_next = discr_lists[i];
23859 discr_lists[i] = new_node;
23861 match_expr = next_round_match_expr;
23865 /* If we reach this point, we could match everything we were interested
23866 in. */
23867 return;
23869 abort:
23870 /* Clean all data structure and return no result. */
23871 free (*discr_lists_p);
23872 *discr_lists_p = NULL;
23873 *discr_decl = NULL_TREE;
23876 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23877 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23878 under CONTEXT_DIE.
23880 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23881 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23882 this type, which are record types, represent the available variants and each
23883 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23884 values are inferred from these attributes.
23886 In trees, the offsets for the fields inside these sub-records are relative
23887 to the variant part itself, whereas the corresponding DIEs should have
23888 offset attributes that are relative to the embedding record base address.
23889 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23890 must be an expression that computes the offset of the variant part to
23891 describe in DWARF. */
23893 static void
23894 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23895 dw_die_ref context_die)
23897 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23898 tree variant_part_offset = vlr_ctx->variant_part_offset;
23899 struct loc_descr_context ctx = {
23900 vlr_ctx->struct_type, /* context_type */
23901 NULL_TREE, /* base_decl */
23902 NULL, /* dpi */
23903 false, /* placeholder_arg */
23904 false /* placeholder_seen */
23907 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23908 NULL_TREE if there is no such field. */
23909 tree discr_decl = NULL_TREE;
23910 dw_discr_list_ref *discr_lists;
23911 unsigned discr_lists_length = 0;
23912 unsigned i;
23914 dw_die_ref dwarf_proc_die = NULL;
23915 dw_die_ref variant_part_die
23916 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23918 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23920 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23921 &discr_decl, &discr_lists, &discr_lists_length);
23923 if (discr_decl != NULL_TREE)
23925 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23927 if (discr_die)
23928 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23929 else
23930 /* We have no DIE for the discriminant, so just discard all
23931 discrimimant information in the output. */
23932 discr_decl = NULL_TREE;
23935 /* If the offset for this variant part is more complex than a constant,
23936 create a DWARF procedure for it so that we will not have to generate DWARF
23937 expressions for it for each member. */
23938 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23939 && (dwarf_version >= 3 || !dwarf_strict))
23941 const tree dwarf_proc_fndecl
23942 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23943 build_function_type (TREE_TYPE (variant_part_offset),
23944 NULL_TREE));
23945 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23946 const dw_loc_descr_ref dwarf_proc_body
23947 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23949 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23950 dwarf_proc_fndecl, context_die);
23951 if (dwarf_proc_die != NULL)
23952 variant_part_offset = dwarf_proc_call;
23955 /* Output DIEs for all variants. */
23956 i = 0;
23957 for (tree variant = TYPE_FIELDS (variant_part_type);
23958 variant != NULL_TREE;
23959 variant = DECL_CHAIN (variant), ++i)
23961 tree variant_type = TREE_TYPE (variant);
23962 dw_die_ref variant_die;
23964 /* All variants (i.e. members of a variant part) are supposed to be
23965 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23966 under these records. */
23967 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23969 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23970 equate_decl_number_to_die (variant, variant_die);
23972 /* Output discriminant values this variant matches, if any. */
23973 if (discr_decl == NULL || discr_lists[i] == NULL)
23974 /* In the case we have discriminant information at all, this is
23975 probably the default variant: as the standard says, don't
23976 output any discriminant value/list attribute. */
23978 else if (discr_lists[i]->dw_discr_next == NULL
23979 && !discr_lists[i]->dw_discr_range)
23980 /* If there is only one accepted value, don't bother outputting a
23981 list. */
23982 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23983 else
23984 add_discr_list (variant_die, discr_lists[i]);
23986 for (tree member = TYPE_FIELDS (variant_type);
23987 member != NULL_TREE;
23988 member = DECL_CHAIN (member))
23990 struct vlr_context vlr_sub_ctx = {
23991 vlr_ctx->struct_type, /* struct_type */
23992 NULL /* variant_part_offset */
23994 if (is_variant_part (member))
23996 /* All offsets for fields inside variant parts are relative to
23997 the top-level embedding RECORD_TYPE's base address. On the
23998 other hand, offsets in GCC's types are relative to the
23999 nested-most variant part. So we have to sum offsets each time
24000 we recurse. */
24002 vlr_sub_ctx.variant_part_offset
24003 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24004 variant_part_offset, byte_position (member));
24005 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24007 else
24009 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24010 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24015 free (discr_lists);
24018 /* Generate a DIE for a class member. */
24020 static void
24021 gen_member_die (tree type, dw_die_ref context_die)
24023 tree member;
24024 tree binfo = TYPE_BINFO (type);
24026 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24028 /* If this is not an incomplete type, output descriptions of each of its
24029 members. Note that as we output the DIEs necessary to represent the
24030 members of this record or union type, we will also be trying to output
24031 DIEs to represent the *types* of those members. However the `type'
24032 function (above) will specifically avoid generating type DIEs for member
24033 types *within* the list of member DIEs for this (containing) type except
24034 for those types (of members) which are explicitly marked as also being
24035 members of this (containing) type themselves. The g++ front- end can
24036 force any given type to be treated as a member of some other (containing)
24037 type by setting the TYPE_CONTEXT of the given (member) type to point to
24038 the TREE node representing the appropriate (containing) type. */
24040 /* First output info about the base classes. */
24041 if (binfo)
24043 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24044 int i;
24045 tree base;
24047 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24048 gen_inheritance_die (base,
24049 (accesses ? (*accesses)[i] : access_public_node),
24050 type,
24051 context_die);
24054 /* Now output info about the data members and type members. */
24055 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24057 struct vlr_context vlr_ctx = { type, NULL_TREE };
24058 bool static_inline_p
24059 = (TREE_STATIC (member)
24060 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24061 != -1));
24063 /* Ignore clones. */
24064 if (DECL_ABSTRACT_ORIGIN (member))
24065 continue;
24067 /* If we thought we were generating minimal debug info for TYPE
24068 and then changed our minds, some of the member declarations
24069 may have already been defined. Don't define them again, but
24070 do put them in the right order. */
24072 if (dw_die_ref child = lookup_decl_die (member))
24074 /* Handle inline static data members, which only have in-class
24075 declarations. */
24076 dw_die_ref ref = NULL;
24077 if (child->die_tag == DW_TAG_variable
24078 && child->die_parent == comp_unit_die ())
24080 ref = get_AT_ref (child, DW_AT_specification);
24081 /* For C++17 inline static data members followed by redundant
24082 out of class redeclaration, we might get here with
24083 child being the DIE created for the out of class
24084 redeclaration and with its DW_AT_specification being
24085 the DIE created for in-class definition. We want to
24086 reparent the latter, and don't want to create another
24087 DIE with DW_AT_specification in that case, because
24088 we already have one. */
24089 if (ref
24090 && static_inline_p
24091 && ref->die_tag == DW_TAG_variable
24092 && ref->die_parent == comp_unit_die ()
24093 && get_AT (ref, DW_AT_specification) == NULL)
24095 child = ref;
24096 ref = NULL;
24097 static_inline_p = false;
24101 if (child->die_tag == DW_TAG_variable
24102 && child->die_parent == comp_unit_die ()
24103 && ref == NULL)
24105 reparent_child (child, context_die);
24106 if (dwarf_version < 5)
24107 child->die_tag = DW_TAG_member;
24109 else
24110 splice_child_die (context_die, child);
24113 /* Do not generate standard DWARF for variant parts if we are generating
24114 the corresponding GNAT encodings: DIEs generated for both would
24115 conflict in our mappings. */
24116 else if (is_variant_part (member)
24117 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24119 vlr_ctx.variant_part_offset = byte_position (member);
24120 gen_variant_part (member, &vlr_ctx, context_die);
24122 else
24124 vlr_ctx.variant_part_offset = NULL_TREE;
24125 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24128 /* For C++ inline static data members emit immediately a DW_TAG_variable
24129 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24130 DW_AT_specification. */
24131 if (static_inline_p)
24133 int old_extern = DECL_EXTERNAL (member);
24134 DECL_EXTERNAL (member) = 0;
24135 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24136 DECL_EXTERNAL (member) = old_extern;
24141 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24142 is set, we pretend that the type was never defined, so we only get the
24143 member DIEs needed by later specification DIEs. */
24145 static void
24146 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24147 enum debug_info_usage usage)
24149 if (TREE_ASM_WRITTEN (type))
24151 /* Fill in the bound of variable-length fields in late dwarf if
24152 still incomplete. */
24153 if (!early_dwarf && variably_modified_type_p (type, NULL))
24154 for (tree member = TYPE_FIELDS (type);
24155 member;
24156 member = DECL_CHAIN (member))
24157 fill_variable_array_bounds (TREE_TYPE (member));
24158 return;
24161 dw_die_ref type_die = lookup_type_die (type);
24162 dw_die_ref scope_die = 0;
24163 int nested = 0;
24164 int complete = (TYPE_SIZE (type)
24165 && (! TYPE_STUB_DECL (type)
24166 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24167 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24168 complete = complete && should_emit_struct_debug (type, usage);
24170 if (type_die && ! complete)
24171 return;
24173 if (TYPE_CONTEXT (type) != NULL_TREE
24174 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24175 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24176 nested = 1;
24178 scope_die = scope_die_for (type, context_die);
24180 /* Generate child dies for template paramaters. */
24181 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24182 schedule_generic_params_dies_gen (type);
24184 if (! type_die || (nested && is_cu_die (scope_die)))
24185 /* First occurrence of type or toplevel definition of nested class. */
24187 dw_die_ref old_die = type_die;
24189 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24190 ? record_type_tag (type) : DW_TAG_union_type,
24191 scope_die, type);
24192 equate_type_number_to_die (type, type_die);
24193 if (old_die)
24194 add_AT_specification (type_die, old_die);
24195 else
24196 add_name_attribute (type_die, type_tag (type));
24198 else
24199 remove_AT (type_die, DW_AT_declaration);
24201 /* If this type has been completed, then give it a byte_size attribute and
24202 then give a list of members. */
24203 if (complete && !ns_decl)
24205 /* Prevent infinite recursion in cases where the type of some member of
24206 this type is expressed in terms of this type itself. */
24207 TREE_ASM_WRITTEN (type) = 1;
24208 add_byte_size_attribute (type_die, type);
24209 add_alignment_attribute (type_die, type);
24210 if (TYPE_STUB_DECL (type) != NULL_TREE)
24212 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24213 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24216 /* If the first reference to this type was as the return type of an
24217 inline function, then it may not have a parent. Fix this now. */
24218 if (type_die->die_parent == NULL)
24219 add_child_die (scope_die, type_die);
24221 push_decl_scope (type);
24222 gen_member_die (type, type_die);
24223 pop_decl_scope ();
24225 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24226 if (TYPE_ARTIFICIAL (type))
24227 add_AT_flag (type_die, DW_AT_artificial, 1);
24229 /* GNU extension: Record what type our vtable lives in. */
24230 if (TYPE_VFIELD (type))
24232 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24234 gen_type_die (vtype, context_die);
24235 add_AT_die_ref (type_die, DW_AT_containing_type,
24236 lookup_type_die (vtype));
24239 else
24241 add_AT_flag (type_die, DW_AT_declaration, 1);
24243 /* We don't need to do this for function-local types. */
24244 if (TYPE_STUB_DECL (type)
24245 && ! decl_function_context (TYPE_STUB_DECL (type)))
24246 vec_safe_push (incomplete_types, type);
24249 if (get_AT (type_die, DW_AT_name))
24250 add_pubtype (type, type_die);
24253 /* Generate a DIE for a subroutine _type_. */
24255 static void
24256 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24258 tree return_type = TREE_TYPE (type);
24259 dw_die_ref subr_die
24260 = new_die (DW_TAG_subroutine_type,
24261 scope_die_for (type, context_die), type);
24263 equate_type_number_to_die (type, subr_die);
24264 add_prototyped_attribute (subr_die, type);
24265 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24266 context_die);
24267 add_alignment_attribute (subr_die, type);
24268 gen_formal_types_die (type, subr_die);
24270 if (get_AT (subr_die, DW_AT_name))
24271 add_pubtype (type, subr_die);
24272 if ((dwarf_version >= 5 || !dwarf_strict)
24273 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24274 add_AT_flag (subr_die, DW_AT_reference, 1);
24275 if ((dwarf_version >= 5 || !dwarf_strict)
24276 && lang_hooks.types.type_dwarf_attribute (type,
24277 DW_AT_rvalue_reference) != -1)
24278 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24281 /* Generate a DIE for a type definition. */
24283 static void
24284 gen_typedef_die (tree decl, dw_die_ref context_die)
24286 dw_die_ref type_die;
24287 tree type;
24289 if (TREE_ASM_WRITTEN (decl))
24291 if (DECL_ORIGINAL_TYPE (decl))
24292 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24293 return;
24296 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24297 checks in process_scope_var and modified_type_die), this should be called
24298 only for original types. */
24299 gcc_assert (decl_ultimate_origin (decl) == NULL
24300 || decl_ultimate_origin (decl) == decl);
24302 TREE_ASM_WRITTEN (decl) = 1;
24303 type_die = new_die (DW_TAG_typedef, context_die, decl);
24305 add_name_and_src_coords_attributes (type_die, decl);
24306 if (DECL_ORIGINAL_TYPE (decl))
24308 type = DECL_ORIGINAL_TYPE (decl);
24309 if (type == error_mark_node)
24310 return;
24312 gcc_assert (type != TREE_TYPE (decl));
24313 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24315 else
24317 type = TREE_TYPE (decl);
24318 if (type == error_mark_node)
24319 return;
24321 if (is_naming_typedef_decl (TYPE_NAME (type)))
24323 /* Here, we are in the case of decl being a typedef naming
24324 an anonymous type, e.g:
24325 typedef struct {...} foo;
24326 In that case TREE_TYPE (decl) is not a typedef variant
24327 type and TYPE_NAME of the anonymous type is set to the
24328 TYPE_DECL of the typedef. This construct is emitted by
24329 the C++ FE.
24331 TYPE is the anonymous struct named by the typedef
24332 DECL. As we need the DW_AT_type attribute of the
24333 DW_TAG_typedef to point to the DIE of TYPE, let's
24334 generate that DIE right away. add_type_attribute
24335 called below will then pick (via lookup_type_die) that
24336 anonymous struct DIE. */
24337 if (!TREE_ASM_WRITTEN (type))
24338 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24340 /* This is a GNU Extension. We are adding a
24341 DW_AT_linkage_name attribute to the DIE of the
24342 anonymous struct TYPE. The value of that attribute
24343 is the name of the typedef decl naming the anonymous
24344 struct. This greatly eases the work of consumers of
24345 this debug info. */
24346 add_linkage_name_raw (lookup_type_die (type), decl);
24350 add_type_attribute (type_die, type, decl_quals (decl), false,
24351 context_die);
24353 if (is_naming_typedef_decl (decl))
24354 /* We want that all subsequent calls to lookup_type_die with
24355 TYPE in argument yield the DW_TAG_typedef we have just
24356 created. */
24357 equate_type_number_to_die (type, type_die);
24359 add_alignment_attribute (type_die, TREE_TYPE (decl));
24361 add_accessibility_attribute (type_die, decl);
24363 if (DECL_ABSTRACT_P (decl))
24364 equate_decl_number_to_die (decl, type_die);
24366 if (get_AT (type_die, DW_AT_name))
24367 add_pubtype (decl, type_die);
24370 /* Generate a DIE for a struct, class, enum or union type. */
24372 static void
24373 gen_tagged_type_die (tree type,
24374 dw_die_ref context_die,
24375 enum debug_info_usage usage)
24377 int need_pop;
24379 if (type == NULL_TREE
24380 || !is_tagged_type (type))
24381 return;
24383 if (TREE_ASM_WRITTEN (type))
24384 need_pop = 0;
24385 /* If this is a nested type whose containing class hasn't been written
24386 out yet, writing it out will cover this one, too. This does not apply
24387 to instantiations of member class templates; they need to be added to
24388 the containing class as they are generated. FIXME: This hurts the
24389 idea of combining type decls from multiple TUs, since we can't predict
24390 what set of template instantiations we'll get. */
24391 else if (TYPE_CONTEXT (type)
24392 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24393 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24395 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24397 if (TREE_ASM_WRITTEN (type))
24398 return;
24400 /* If that failed, attach ourselves to the stub. */
24401 push_decl_scope (TYPE_CONTEXT (type));
24402 context_die = lookup_type_die (TYPE_CONTEXT (type));
24403 need_pop = 1;
24405 else if (TYPE_CONTEXT (type) != NULL_TREE
24406 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24408 /* If this type is local to a function that hasn't been written
24409 out yet, use a NULL context for now; it will be fixed up in
24410 decls_for_scope. */
24411 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24412 /* A declaration DIE doesn't count; nested types need to go in the
24413 specification. */
24414 if (context_die && is_declaration_die (context_die))
24415 context_die = NULL;
24416 need_pop = 0;
24418 else
24420 context_die = declare_in_namespace (type, context_die);
24421 need_pop = 0;
24424 if (TREE_CODE (type) == ENUMERAL_TYPE)
24426 /* This might have been written out by the call to
24427 declare_in_namespace. */
24428 if (!TREE_ASM_WRITTEN (type))
24429 gen_enumeration_type_die (type, context_die);
24431 else
24432 gen_struct_or_union_type_die (type, context_die, usage);
24434 if (need_pop)
24435 pop_decl_scope ();
24437 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24438 it up if it is ever completed. gen_*_type_die will set it for us
24439 when appropriate. */
24442 /* Generate a type description DIE. */
24444 static void
24445 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24446 enum debug_info_usage usage)
24448 struct array_descr_info info;
24450 if (type == NULL_TREE || type == error_mark_node)
24451 return;
24453 if (flag_checking && type)
24454 verify_type (type);
24456 if (TYPE_NAME (type) != NULL_TREE
24457 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24458 && is_redundant_typedef (TYPE_NAME (type))
24459 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24460 /* The DECL of this type is a typedef we don't want to emit debug
24461 info for but we want debug info for its underlying typedef.
24462 This can happen for e.g, the injected-class-name of a C++
24463 type. */
24464 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24466 /* If TYPE is a typedef type variant, let's generate debug info
24467 for the parent typedef which TYPE is a type of. */
24468 if (typedef_variant_p (type))
24470 if (TREE_ASM_WRITTEN (type))
24471 return;
24473 tree name = TYPE_NAME (type);
24474 tree origin = decl_ultimate_origin (name);
24475 if (origin != NULL && origin != name)
24477 gen_decl_die (origin, NULL, NULL, context_die);
24478 return;
24481 /* Prevent broken recursion; we can't hand off to the same type. */
24482 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24484 /* Give typedefs the right scope. */
24485 context_die = scope_die_for (type, context_die);
24487 TREE_ASM_WRITTEN (type) = 1;
24489 gen_decl_die (name, NULL, NULL, context_die);
24490 return;
24493 /* If type is an anonymous tagged type named by a typedef, let's
24494 generate debug info for the typedef. */
24495 if (is_naming_typedef_decl (TYPE_NAME (type)))
24497 /* Use the DIE of the containing namespace as the parent DIE of
24498 the type description DIE we want to generate. */
24499 if (DECL_CONTEXT (TYPE_NAME (type))
24500 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24501 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24503 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24504 return;
24507 if (lang_hooks.types.get_debug_type)
24509 tree debug_type = lang_hooks.types.get_debug_type (type);
24511 if (debug_type != NULL_TREE && debug_type != type)
24513 gen_type_die_with_usage (debug_type, context_die, usage);
24514 return;
24518 /* We are going to output a DIE to represent the unqualified version
24519 of this type (i.e. without any const or volatile qualifiers) so
24520 get the main variant (i.e. the unqualified version) of this type
24521 now. (Vectors and arrays are special because the debugging info is in the
24522 cloned type itself. Similarly function/method types can contain extra
24523 ref-qualification). */
24524 if (TREE_CODE (type) == FUNCTION_TYPE
24525 || TREE_CODE (type) == METHOD_TYPE)
24527 /* For function/method types, can't use type_main_variant here,
24528 because that can have different ref-qualifiers for C++,
24529 but try to canonicalize. */
24530 tree main = TYPE_MAIN_VARIANT (type);
24531 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24532 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24533 && check_base_type (t, main)
24534 && check_lang_type (t, type))
24536 type = t;
24537 break;
24540 else if (TREE_CODE (type) != VECTOR_TYPE
24541 && TREE_CODE (type) != ARRAY_TYPE)
24542 type = type_main_variant (type);
24544 /* If this is an array type with hidden descriptor, handle it first. */
24545 if (!TREE_ASM_WRITTEN (type)
24546 && lang_hooks.types.get_array_descr_info)
24548 memset (&info, 0, sizeof (info));
24549 if (lang_hooks.types.get_array_descr_info (type, &info))
24551 /* Fortran sometimes emits array types with no dimension. */
24552 gcc_assert (info.ndimensions >= 0
24553 && (info.ndimensions
24554 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24555 gen_descr_array_type_die (type, &info, context_die);
24556 TREE_ASM_WRITTEN (type) = 1;
24557 return;
24561 if (TREE_ASM_WRITTEN (type))
24563 /* Variable-length types may be incomplete even if
24564 TREE_ASM_WRITTEN. For such types, fall through to
24565 gen_array_type_die() and possibly fill in
24566 DW_AT_{upper,lower}_bound attributes. */
24567 if ((TREE_CODE (type) != ARRAY_TYPE
24568 && TREE_CODE (type) != RECORD_TYPE
24569 && TREE_CODE (type) != UNION_TYPE
24570 && TREE_CODE (type) != QUAL_UNION_TYPE)
24571 || !variably_modified_type_p (type, NULL))
24572 return;
24575 switch (TREE_CODE (type))
24577 case ERROR_MARK:
24578 break;
24580 case POINTER_TYPE:
24581 case REFERENCE_TYPE:
24582 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24583 ensures that the gen_type_die recursion will terminate even if the
24584 type is recursive. Recursive types are possible in Ada. */
24585 /* ??? We could perhaps do this for all types before the switch
24586 statement. */
24587 TREE_ASM_WRITTEN (type) = 1;
24589 /* For these types, all that is required is that we output a DIE (or a
24590 set of DIEs) to represent the "basis" type. */
24591 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24592 DINFO_USAGE_IND_USE);
24593 break;
24595 case OFFSET_TYPE:
24596 /* This code is used for C++ pointer-to-data-member types.
24597 Output a description of the relevant class type. */
24598 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24599 DINFO_USAGE_IND_USE);
24601 /* Output a description of the type of the object pointed to. */
24602 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24603 DINFO_USAGE_IND_USE);
24605 /* Now output a DIE to represent this pointer-to-data-member type
24606 itself. */
24607 gen_ptr_to_mbr_type_die (type, context_die);
24608 break;
24610 case FUNCTION_TYPE:
24611 /* Force out return type (in case it wasn't forced out already). */
24612 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24613 DINFO_USAGE_DIR_USE);
24614 gen_subroutine_type_die (type, context_die);
24615 break;
24617 case METHOD_TYPE:
24618 /* Force out return type (in case it wasn't forced out already). */
24619 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24620 DINFO_USAGE_DIR_USE);
24621 gen_subroutine_type_die (type, context_die);
24622 break;
24624 case ARRAY_TYPE:
24625 case VECTOR_TYPE:
24626 gen_array_type_die (type, context_die);
24627 break;
24629 case ENUMERAL_TYPE:
24630 case RECORD_TYPE:
24631 case UNION_TYPE:
24632 case QUAL_UNION_TYPE:
24633 gen_tagged_type_die (type, context_die, usage);
24634 return;
24636 case VOID_TYPE:
24637 case INTEGER_TYPE:
24638 case REAL_TYPE:
24639 case FIXED_POINT_TYPE:
24640 case COMPLEX_TYPE:
24641 case BOOLEAN_TYPE:
24642 case POINTER_BOUNDS_TYPE:
24643 /* No DIEs needed for fundamental types. */
24644 break;
24646 case NULLPTR_TYPE:
24647 case LANG_TYPE:
24648 /* Just use DW_TAG_unspecified_type. */
24650 dw_die_ref type_die = lookup_type_die (type);
24651 if (type_die == NULL)
24653 tree name = TYPE_IDENTIFIER (type);
24654 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24655 type);
24656 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24657 equate_type_number_to_die (type, type_die);
24660 break;
24662 default:
24663 if (is_cxx_auto (type))
24665 tree name = TYPE_IDENTIFIER (type);
24666 dw_die_ref *die = (name == get_identifier ("auto")
24667 ? &auto_die : &decltype_auto_die);
24668 if (!*die)
24670 *die = new_die (DW_TAG_unspecified_type,
24671 comp_unit_die (), NULL_TREE);
24672 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24674 equate_type_number_to_die (type, *die);
24675 break;
24677 gcc_unreachable ();
24680 TREE_ASM_WRITTEN (type) = 1;
24683 static void
24684 gen_type_die (tree type, dw_die_ref context_die)
24686 if (type != error_mark_node)
24688 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24689 if (flag_checking)
24691 dw_die_ref die = lookup_type_die (type);
24692 if (die)
24693 check_die (die);
24698 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24699 things which are local to the given block. */
24701 static void
24702 gen_block_die (tree stmt, dw_die_ref context_die)
24704 int must_output_die = 0;
24705 bool inlined_func;
24707 /* Ignore blocks that are NULL. */
24708 if (stmt == NULL_TREE)
24709 return;
24711 inlined_func = inlined_function_outer_scope_p (stmt);
24713 /* If the block is one fragment of a non-contiguous block, do not
24714 process the variables, since they will have been done by the
24715 origin block. Do process subblocks. */
24716 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24718 tree sub;
24720 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24721 gen_block_die (sub, context_die);
24723 return;
24726 /* Determine if we need to output any Dwarf DIEs at all to represent this
24727 block. */
24728 if (inlined_func)
24729 /* The outer scopes for inlinings *must* always be represented. We
24730 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24731 must_output_die = 1;
24732 else
24734 /* Determine if this block directly contains any "significant"
24735 local declarations which we will need to output DIEs for. */
24736 if (debug_info_level > DINFO_LEVEL_TERSE)
24737 /* We are not in terse mode so *any* local declaration counts
24738 as being a "significant" one. */
24739 must_output_die = ((BLOCK_VARS (stmt) != NULL
24740 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24741 && (TREE_USED (stmt)
24742 || TREE_ASM_WRITTEN (stmt)
24743 || BLOCK_ABSTRACT (stmt)));
24744 else if ((TREE_USED (stmt)
24745 || TREE_ASM_WRITTEN (stmt)
24746 || BLOCK_ABSTRACT (stmt))
24747 && !dwarf2out_ignore_block (stmt))
24748 must_output_die = 1;
24751 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24752 DIE for any block which contains no significant local declarations at
24753 all. Rather, in such cases we just call `decls_for_scope' so that any
24754 needed Dwarf info for any sub-blocks will get properly generated. Note
24755 that in terse mode, our definition of what constitutes a "significant"
24756 local declaration gets restricted to include only inlined function
24757 instances and local (nested) function definitions. */
24758 if (must_output_die)
24760 if (inlined_func)
24762 /* If STMT block is abstract, that means we have been called
24763 indirectly from dwarf2out_abstract_function.
24764 That function rightfully marks the descendent blocks (of
24765 the abstract function it is dealing with) as being abstract,
24766 precisely to prevent us from emitting any
24767 DW_TAG_inlined_subroutine DIE as a descendent
24768 of an abstract function instance. So in that case, we should
24769 not call gen_inlined_subroutine_die.
24771 Later though, when cgraph asks dwarf2out to emit info
24772 for the concrete instance of the function decl into which
24773 the concrete instance of STMT got inlined, the later will lead
24774 to the generation of a DW_TAG_inlined_subroutine DIE. */
24775 if (! BLOCK_ABSTRACT (stmt))
24776 gen_inlined_subroutine_die (stmt, context_die);
24778 else
24779 gen_lexical_block_die (stmt, context_die);
24781 else
24782 decls_for_scope (stmt, context_die);
24785 /* Process variable DECL (or variable with origin ORIGIN) within
24786 block STMT and add it to CONTEXT_DIE. */
24787 static void
24788 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24790 dw_die_ref die;
24791 tree decl_or_origin = decl ? decl : origin;
24793 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24794 die = lookup_decl_die (decl_or_origin);
24795 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24797 if (TYPE_DECL_IS_STUB (decl_or_origin))
24798 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24799 else
24800 die = lookup_decl_die (decl_or_origin);
24801 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24802 if (! die && ! early_dwarf)
24803 return;
24805 else
24806 die = NULL;
24808 /* Avoid creating DIEs for local typedefs and concrete static variables that
24809 will only be pruned later. */
24810 if ((origin || decl_ultimate_origin (decl))
24811 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24812 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24814 origin = decl_ultimate_origin (decl_or_origin);
24815 if (decl && VAR_P (decl) && die != NULL)
24817 die = lookup_decl_die (origin);
24818 if (die != NULL)
24819 equate_decl_number_to_die (decl, die);
24821 return;
24824 if (die != NULL && die->die_parent == NULL)
24825 add_child_die (context_die, die);
24826 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24828 if (early_dwarf)
24829 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24830 stmt, context_die);
24832 else
24834 if (decl && DECL_P (decl))
24836 die = lookup_decl_die (decl);
24838 /* Early created DIEs do not have a parent as the decls refer
24839 to the function as DECL_CONTEXT rather than the BLOCK. */
24840 if (die && die->die_parent == NULL)
24842 gcc_assert (in_lto_p);
24843 add_child_die (context_die, die);
24847 gen_decl_die (decl, origin, NULL, context_die);
24851 /* Generate all of the decls declared within a given scope and (recursively)
24852 all of its sub-blocks. */
24854 static void
24855 decls_for_scope (tree stmt, dw_die_ref context_die)
24857 tree decl;
24858 unsigned int i;
24859 tree subblocks;
24861 /* Ignore NULL blocks. */
24862 if (stmt == NULL_TREE)
24863 return;
24865 /* Output the DIEs to represent all of the data objects and typedefs
24866 declared directly within this block but not within any nested
24867 sub-blocks. Also, nested function and tag DIEs have been
24868 generated with a parent of NULL; fix that up now. We don't
24869 have to do this if we're at -g1. */
24870 if (debug_info_level > DINFO_LEVEL_TERSE)
24872 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24873 process_scope_var (stmt, decl, NULL_TREE, context_die);
24874 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24875 origin - avoid doing this twice as we have no good way to see
24876 if we've done it once already. */
24877 if (! early_dwarf)
24878 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24880 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24881 if (decl == current_function_decl)
24882 /* Ignore declarations of the current function, while they
24883 are declarations, gen_subprogram_die would treat them
24884 as definitions again, because they are equal to
24885 current_function_decl and endlessly recurse. */;
24886 else if (TREE_CODE (decl) == FUNCTION_DECL)
24887 process_scope_var (stmt, decl, NULL_TREE, context_die);
24888 else
24889 process_scope_var (stmt, NULL_TREE, decl, context_die);
24893 /* Even if we're at -g1, we need to process the subblocks in order to get
24894 inlined call information. */
24896 /* Output the DIEs to represent all sub-blocks (and the items declared
24897 therein) of this block. */
24898 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24899 subblocks != NULL;
24900 subblocks = BLOCK_CHAIN (subblocks))
24901 gen_block_die (subblocks, context_die);
24904 /* Is this a typedef we can avoid emitting? */
24906 bool
24907 is_redundant_typedef (const_tree decl)
24909 if (TYPE_DECL_IS_STUB (decl))
24910 return true;
24912 if (DECL_ARTIFICIAL (decl)
24913 && DECL_CONTEXT (decl)
24914 && is_tagged_type (DECL_CONTEXT (decl))
24915 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24916 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24917 /* Also ignore the artificial member typedef for the class name. */
24918 return true;
24920 return false;
24923 /* Return TRUE if TYPE is a typedef that names a type for linkage
24924 purposes. This kind of typedefs is produced by the C++ FE for
24925 constructs like:
24927 typedef struct {...} foo;
24929 In that case, there is no typedef variant type produced for foo.
24930 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24931 struct type. */
24933 static bool
24934 is_naming_typedef_decl (const_tree decl)
24936 if (decl == NULL_TREE
24937 || TREE_CODE (decl) != TYPE_DECL
24938 || DECL_NAMELESS (decl)
24939 || !is_tagged_type (TREE_TYPE (decl))
24940 || DECL_IS_BUILTIN (decl)
24941 || is_redundant_typedef (decl)
24942 /* It looks like Ada produces TYPE_DECLs that are very similar
24943 to C++ naming typedefs but that have different
24944 semantics. Let's be specific to c++ for now. */
24945 || !is_cxx (decl))
24946 return FALSE;
24948 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24949 && TYPE_NAME (TREE_TYPE (decl)) == decl
24950 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24951 != TYPE_NAME (TREE_TYPE (decl))));
24954 /* Looks up the DIE for a context. */
24956 static inline dw_die_ref
24957 lookup_context_die (tree context)
24959 if (context)
24961 /* Find die that represents this context. */
24962 if (TYPE_P (context))
24964 context = TYPE_MAIN_VARIANT (context);
24965 dw_die_ref ctx = lookup_type_die (context);
24966 if (!ctx)
24967 return NULL;
24968 return strip_naming_typedef (context, ctx);
24970 else
24971 return lookup_decl_die (context);
24973 return comp_unit_die ();
24976 /* Returns the DIE for a context. */
24978 static inline dw_die_ref
24979 get_context_die (tree context)
24981 if (context)
24983 /* Find die that represents this context. */
24984 if (TYPE_P (context))
24986 context = TYPE_MAIN_VARIANT (context);
24987 return strip_naming_typedef (context, force_type_die (context));
24989 else
24990 return force_decl_die (context);
24992 return comp_unit_die ();
24995 /* Returns the DIE for decl. A DIE will always be returned. */
24997 static dw_die_ref
24998 force_decl_die (tree decl)
25000 dw_die_ref decl_die;
25001 unsigned saved_external_flag;
25002 tree save_fn = NULL_TREE;
25003 decl_die = lookup_decl_die (decl);
25004 if (!decl_die)
25006 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25008 decl_die = lookup_decl_die (decl);
25009 if (decl_die)
25010 return decl_die;
25012 switch (TREE_CODE (decl))
25014 case FUNCTION_DECL:
25015 /* Clear current_function_decl, so that gen_subprogram_die thinks
25016 that this is a declaration. At this point, we just want to force
25017 declaration die. */
25018 save_fn = current_function_decl;
25019 current_function_decl = NULL_TREE;
25020 gen_subprogram_die (decl, context_die);
25021 current_function_decl = save_fn;
25022 break;
25024 case VAR_DECL:
25025 /* Set external flag to force declaration die. Restore it after
25026 gen_decl_die() call. */
25027 saved_external_flag = DECL_EXTERNAL (decl);
25028 DECL_EXTERNAL (decl) = 1;
25029 gen_decl_die (decl, NULL, NULL, context_die);
25030 DECL_EXTERNAL (decl) = saved_external_flag;
25031 break;
25033 case NAMESPACE_DECL:
25034 if (dwarf_version >= 3 || !dwarf_strict)
25035 dwarf2out_decl (decl);
25036 else
25037 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25038 decl_die = comp_unit_die ();
25039 break;
25041 case TRANSLATION_UNIT_DECL:
25042 decl_die = comp_unit_die ();
25043 break;
25045 default:
25046 gcc_unreachable ();
25049 /* We should be able to find the DIE now. */
25050 if (!decl_die)
25051 decl_die = lookup_decl_die (decl);
25052 gcc_assert (decl_die);
25055 return decl_die;
25058 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25059 always returned. */
25061 static dw_die_ref
25062 force_type_die (tree type)
25064 dw_die_ref type_die;
25066 type_die = lookup_type_die (type);
25067 if (!type_die)
25069 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25071 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25072 false, context_die);
25073 gcc_assert (type_die);
25075 return type_die;
25078 /* Force out any required namespaces to be able to output DECL,
25079 and return the new context_die for it, if it's changed. */
25081 static dw_die_ref
25082 setup_namespace_context (tree thing, dw_die_ref context_die)
25084 tree context = (DECL_P (thing)
25085 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25086 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25087 /* Force out the namespace. */
25088 context_die = force_decl_die (context);
25090 return context_die;
25093 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25094 type) within its namespace, if appropriate.
25096 For compatibility with older debuggers, namespace DIEs only contain
25097 declarations; all definitions are emitted at CU scope, with
25098 DW_AT_specification pointing to the declaration (like with class
25099 members). */
25101 static dw_die_ref
25102 declare_in_namespace (tree thing, dw_die_ref context_die)
25104 dw_die_ref ns_context;
25106 if (debug_info_level <= DINFO_LEVEL_TERSE)
25107 return context_die;
25109 /* External declarations in the local scope only need to be emitted
25110 once, not once in the namespace and once in the scope.
25112 This avoids declaring the `extern' below in the
25113 namespace DIE as well as in the innermost scope:
25115 namespace S
25117 int i=5;
25118 int foo()
25120 int i=8;
25121 extern int i;
25122 return i;
25126 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25127 return context_die;
25129 /* If this decl is from an inlined function, then don't try to emit it in its
25130 namespace, as we will get confused. It would have already been emitted
25131 when the abstract instance of the inline function was emitted anyways. */
25132 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25133 return context_die;
25135 ns_context = setup_namespace_context (thing, context_die);
25137 if (ns_context != context_die)
25139 if (is_fortran ())
25140 return ns_context;
25141 if (DECL_P (thing))
25142 gen_decl_die (thing, NULL, NULL, ns_context);
25143 else
25144 gen_type_die (thing, ns_context);
25146 return context_die;
25149 /* Generate a DIE for a namespace or namespace alias. */
25151 static void
25152 gen_namespace_die (tree decl, dw_die_ref context_die)
25154 dw_die_ref namespace_die;
25156 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25157 they are an alias of. */
25158 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25160 /* Output a real namespace or module. */
25161 context_die = setup_namespace_context (decl, comp_unit_die ());
25162 namespace_die = new_die (is_fortran ()
25163 ? DW_TAG_module : DW_TAG_namespace,
25164 context_die, decl);
25165 /* For Fortran modules defined in different CU don't add src coords. */
25166 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25168 const char *name = dwarf2_name (decl, 0);
25169 if (name)
25170 add_name_attribute (namespace_die, name);
25172 else
25173 add_name_and_src_coords_attributes (namespace_die, decl);
25174 if (DECL_EXTERNAL (decl))
25175 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25176 equate_decl_number_to_die (decl, namespace_die);
25178 else
25180 /* Output a namespace alias. */
25182 /* Force out the namespace we are an alias of, if necessary. */
25183 dw_die_ref origin_die
25184 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25186 if (DECL_FILE_SCOPE_P (decl)
25187 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25188 context_die = setup_namespace_context (decl, comp_unit_die ());
25189 /* Now create the namespace alias DIE. */
25190 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25191 add_name_and_src_coords_attributes (namespace_die, decl);
25192 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25193 equate_decl_number_to_die (decl, namespace_die);
25195 if ((dwarf_version >= 5 || !dwarf_strict)
25196 && lang_hooks.decls.decl_dwarf_attribute (decl,
25197 DW_AT_export_symbols) == 1)
25198 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25200 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25201 if (want_pubnames ())
25202 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25205 /* Generate Dwarf debug information for a decl described by DECL.
25206 The return value is currently only meaningful for PARM_DECLs,
25207 for all other decls it returns NULL.
25209 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25210 It can be NULL otherwise. */
25212 static dw_die_ref
25213 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25214 dw_die_ref context_die)
25216 tree decl_or_origin = decl ? decl : origin;
25217 tree class_origin = NULL, ultimate_origin;
25219 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25220 return NULL;
25222 /* Ignore pointer bounds decls. */
25223 if (DECL_P (decl_or_origin)
25224 && TREE_TYPE (decl_or_origin)
25225 && POINTER_BOUNDS_P (decl_or_origin))
25226 return NULL;
25228 switch (TREE_CODE (decl_or_origin))
25230 case ERROR_MARK:
25231 break;
25233 case CONST_DECL:
25234 if (!is_fortran () && !is_ada ())
25236 /* The individual enumerators of an enum type get output when we output
25237 the Dwarf representation of the relevant enum type itself. */
25238 break;
25241 /* Emit its type. */
25242 gen_type_die (TREE_TYPE (decl), context_die);
25244 /* And its containing namespace. */
25245 context_die = declare_in_namespace (decl, context_die);
25247 gen_const_die (decl, context_die);
25248 break;
25250 case FUNCTION_DECL:
25251 #if 0
25252 /* FIXME */
25253 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25254 on local redeclarations of global functions. That seems broken. */
25255 if (current_function_decl != decl)
25256 /* This is only a declaration. */;
25257 #endif
25259 /* We should have abstract copies already and should not generate
25260 stray type DIEs in late LTO dumping. */
25261 if (! early_dwarf)
25264 /* If we're emitting a clone, emit info for the abstract instance. */
25265 else if (origin || DECL_ORIGIN (decl) != decl)
25266 dwarf2out_abstract_function (origin
25267 ? DECL_ORIGIN (origin)
25268 : DECL_ABSTRACT_ORIGIN (decl));
25270 /* If we're emitting a possibly inlined function emit it as
25271 abstract instance. */
25272 else if (cgraph_function_possibly_inlined_p (decl)
25273 && ! DECL_ABSTRACT_P (decl)
25274 && ! class_or_namespace_scope_p (context_die)
25275 /* dwarf2out_abstract_function won't emit a die if this is just
25276 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25277 that case, because that works only if we have a die. */
25278 && DECL_INITIAL (decl) != NULL_TREE)
25279 dwarf2out_abstract_function (decl);
25281 /* Otherwise we're emitting the primary DIE for this decl. */
25282 else if (debug_info_level > DINFO_LEVEL_TERSE)
25284 /* Before we describe the FUNCTION_DECL itself, make sure that we
25285 have its containing type. */
25286 if (!origin)
25287 origin = decl_class_context (decl);
25288 if (origin != NULL_TREE)
25289 gen_type_die (origin, context_die);
25291 /* And its return type. */
25292 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25294 /* And its virtual context. */
25295 if (DECL_VINDEX (decl) != NULL_TREE)
25296 gen_type_die (DECL_CONTEXT (decl), context_die);
25298 /* Make sure we have a member DIE for decl. */
25299 if (origin != NULL_TREE)
25300 gen_type_die_for_member (origin, decl, context_die);
25302 /* And its containing namespace. */
25303 context_die = declare_in_namespace (decl, context_die);
25306 /* Now output a DIE to represent the function itself. */
25307 if (decl)
25308 gen_subprogram_die (decl, context_die);
25309 break;
25311 case TYPE_DECL:
25312 /* If we are in terse mode, don't generate any DIEs to represent any
25313 actual typedefs. */
25314 if (debug_info_level <= DINFO_LEVEL_TERSE)
25315 break;
25317 /* In the special case of a TYPE_DECL node representing the declaration
25318 of some type tag, if the given TYPE_DECL is marked as having been
25319 instantiated from some other (original) TYPE_DECL node (e.g. one which
25320 was generated within the original definition of an inline function) we
25321 used to generate a special (abbreviated) DW_TAG_structure_type,
25322 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25323 should be actually referencing those DIEs, as variable DIEs with that
25324 type would be emitted already in the abstract origin, so it was always
25325 removed during unused type prunning. Don't add anything in this
25326 case. */
25327 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25328 break;
25330 if (is_redundant_typedef (decl))
25331 gen_type_die (TREE_TYPE (decl), context_die);
25332 else
25333 /* Output a DIE to represent the typedef itself. */
25334 gen_typedef_die (decl, context_die);
25335 break;
25337 case LABEL_DECL:
25338 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25339 gen_label_die (decl, context_die);
25340 break;
25342 case VAR_DECL:
25343 case RESULT_DECL:
25344 /* If we are in terse mode, don't generate any DIEs to represent any
25345 variable declarations or definitions. */
25346 if (debug_info_level <= DINFO_LEVEL_TERSE)
25347 break;
25349 /* Avoid generating stray type DIEs during late dwarf dumping.
25350 All types have been dumped early. */
25351 if (early_dwarf
25352 /* ??? But in LTRANS we cannot annotate early created variably
25353 modified type DIEs without copying them and adjusting all
25354 references to them. Dump them again as happens for inlining
25355 which copies both the decl and the types. */
25356 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25357 in VLA bound information for example. */
25358 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25359 current_function_decl)))
25361 /* Output any DIEs that are needed to specify the type of this data
25362 object. */
25363 if (decl_by_reference_p (decl_or_origin))
25364 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25365 else
25366 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25369 if (early_dwarf)
25371 /* And its containing type. */
25372 class_origin = decl_class_context (decl_or_origin);
25373 if (class_origin != NULL_TREE)
25374 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25376 /* And its containing namespace. */
25377 context_die = declare_in_namespace (decl_or_origin, context_die);
25380 /* Now output the DIE to represent the data object itself. This gets
25381 complicated because of the possibility that the VAR_DECL really
25382 represents an inlined instance of a formal parameter for an inline
25383 function. */
25384 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25385 if (ultimate_origin != NULL_TREE
25386 && TREE_CODE (ultimate_origin) == PARM_DECL)
25387 gen_formal_parameter_die (decl, origin,
25388 true /* Emit name attribute. */,
25389 context_die);
25390 else
25391 gen_variable_die (decl, origin, context_die);
25392 break;
25394 case FIELD_DECL:
25395 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25396 /* Ignore the nameless fields that are used to skip bits but handle C++
25397 anonymous unions and structs. */
25398 if (DECL_NAME (decl) != NULL_TREE
25399 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25400 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25402 gen_type_die (member_declared_type (decl), context_die);
25403 gen_field_die (decl, ctx, context_die);
25405 break;
25407 case PARM_DECL:
25408 /* Avoid generating stray type DIEs during late dwarf dumping.
25409 All types have been dumped early. */
25410 if (early_dwarf
25411 /* ??? But in LTRANS we cannot annotate early created variably
25412 modified type DIEs without copying them and adjusting all
25413 references to them. Dump them again as happens for inlining
25414 which copies both the decl and the types. */
25415 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25416 in VLA bound information for example. */
25417 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25418 current_function_decl)))
25420 if (DECL_BY_REFERENCE (decl_or_origin))
25421 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25422 else
25423 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25425 return gen_formal_parameter_die (decl, origin,
25426 true /* Emit name attribute. */,
25427 context_die);
25429 case NAMESPACE_DECL:
25430 if (dwarf_version >= 3 || !dwarf_strict)
25431 gen_namespace_die (decl, context_die);
25432 break;
25434 case IMPORTED_DECL:
25435 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25436 DECL_CONTEXT (decl), context_die);
25437 break;
25439 case NAMELIST_DECL:
25440 gen_namelist_decl (DECL_NAME (decl), context_die,
25441 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25442 break;
25444 default:
25445 /* Probably some frontend-internal decl. Assume we don't care. */
25446 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25447 break;
25450 return NULL;
25453 /* Output initial debug information for global DECL. Called at the
25454 end of the parsing process.
25456 This is the initial debug generation process. As such, the DIEs
25457 generated may be incomplete. A later debug generation pass
25458 (dwarf2out_late_global_decl) will augment the information generated
25459 in this pass (e.g., with complete location info). */
25461 static void
25462 dwarf2out_early_global_decl (tree decl)
25464 set_early_dwarf s;
25466 /* gen_decl_die() will set DECL_ABSTRACT because
25467 cgraph_function_possibly_inlined_p() returns true. This is in
25468 turn will cause DW_AT_inline attributes to be set.
25470 This happens because at early dwarf generation, there is no
25471 cgraph information, causing cgraph_function_possibly_inlined_p()
25472 to return true. Trick cgraph_function_possibly_inlined_p()
25473 while we generate dwarf early. */
25474 bool save = symtab->global_info_ready;
25475 symtab->global_info_ready = true;
25477 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25478 other DECLs and they can point to template types or other things
25479 that dwarf2out can't handle when done via dwarf2out_decl. */
25480 if (TREE_CODE (decl) != TYPE_DECL
25481 && TREE_CODE (decl) != PARM_DECL)
25483 if (TREE_CODE (decl) == FUNCTION_DECL)
25485 tree save_fndecl = current_function_decl;
25487 /* For nested functions, make sure we have DIEs for the parents first
25488 so that all nested DIEs are generated at the proper scope in the
25489 first shot. */
25490 tree context = decl_function_context (decl);
25491 if (context != NULL)
25493 dw_die_ref context_die = lookup_decl_die (context);
25494 current_function_decl = context;
25496 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25497 enough so that it lands in its own context. This avoids type
25498 pruning issues later on. */
25499 if (context_die == NULL || is_declaration_die (context_die))
25500 dwarf2out_decl (context);
25503 /* Emit an abstract origin of a function first. This happens
25504 with C++ constructor clones for example and makes
25505 dwarf2out_abstract_function happy which requires the early
25506 DIE of the abstract instance to be present. */
25507 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25508 dw_die_ref origin_die;
25509 if (origin != NULL
25510 /* Do not emit the DIE multiple times but make sure to
25511 process it fully here in case we just saw a declaration. */
25512 && ((origin_die = lookup_decl_die (origin)) == NULL
25513 || is_declaration_die (origin_die)))
25515 current_function_decl = origin;
25516 dwarf2out_decl (origin);
25519 /* Emit the DIE for decl but avoid doing that multiple times. */
25520 dw_die_ref old_die;
25521 if ((old_die = lookup_decl_die (decl)) == NULL
25522 || is_declaration_die (old_die))
25524 current_function_decl = decl;
25525 dwarf2out_decl (decl);
25528 current_function_decl = save_fndecl;
25530 else
25531 dwarf2out_decl (decl);
25533 symtab->global_info_ready = save;
25536 /* Output debug information for global decl DECL. Called from
25537 toplev.c after compilation proper has finished. */
25539 static void
25540 dwarf2out_late_global_decl (tree decl)
25542 /* Fill-in any location information we were unable to determine
25543 on the first pass. */
25544 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25546 dw_die_ref die = lookup_decl_die (decl);
25548 /* We may have to generate early debug late for LTO in case debug
25549 was not enabled at compile-time or the target doesn't support
25550 the LTO early debug scheme. */
25551 if (! die && in_lto_p)
25553 dwarf2out_decl (decl);
25554 die = lookup_decl_die (decl);
25557 if (die)
25559 /* We get called via the symtab code invoking late_global_decl
25560 for symbols that are optimized out. Do not add locations
25561 for those, except if they have a DECL_VALUE_EXPR, in which case
25562 they are relevant for debuggers. */
25563 varpool_node *node = varpool_node::get (decl);
25564 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25565 tree_add_const_value_attribute_for_decl (die, decl);
25566 else
25567 add_location_or_const_value_attribute (die, decl, false);
25572 /* Output debug information for type decl DECL. Called from toplev.c
25573 and from language front ends (to record built-in types). */
25574 static void
25575 dwarf2out_type_decl (tree decl, int local)
25577 if (!local)
25579 set_early_dwarf s;
25580 dwarf2out_decl (decl);
25584 /* Output debug information for imported module or decl DECL.
25585 NAME is non-NULL name in the lexical block if the decl has been renamed.
25586 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25587 that DECL belongs to.
25588 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25589 static void
25590 dwarf2out_imported_module_or_decl_1 (tree decl,
25591 tree name,
25592 tree lexical_block,
25593 dw_die_ref lexical_block_die)
25595 expanded_location xloc;
25596 dw_die_ref imported_die = NULL;
25597 dw_die_ref at_import_die;
25599 if (TREE_CODE (decl) == IMPORTED_DECL)
25601 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25602 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25603 gcc_assert (decl);
25605 else
25606 xloc = expand_location (input_location);
25608 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25610 at_import_die = force_type_die (TREE_TYPE (decl));
25611 /* For namespace N { typedef void T; } using N::T; base_type_die
25612 returns NULL, but DW_TAG_imported_declaration requires
25613 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25614 if (!at_import_die)
25616 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25617 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25618 at_import_die = lookup_type_die (TREE_TYPE (decl));
25619 gcc_assert (at_import_die);
25622 else
25624 at_import_die = lookup_decl_die (decl);
25625 if (!at_import_die)
25627 /* If we're trying to avoid duplicate debug info, we may not have
25628 emitted the member decl for this field. Emit it now. */
25629 if (TREE_CODE (decl) == FIELD_DECL)
25631 tree type = DECL_CONTEXT (decl);
25633 if (TYPE_CONTEXT (type)
25634 && TYPE_P (TYPE_CONTEXT (type))
25635 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25636 DINFO_USAGE_DIR_USE))
25637 return;
25638 gen_type_die_for_member (type, decl,
25639 get_context_die (TYPE_CONTEXT (type)));
25641 if (TREE_CODE (decl) == NAMELIST_DECL)
25642 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25643 get_context_die (DECL_CONTEXT (decl)),
25644 NULL_TREE);
25645 else
25646 at_import_die = force_decl_die (decl);
25650 if (TREE_CODE (decl) == NAMESPACE_DECL)
25652 if (dwarf_version >= 3 || !dwarf_strict)
25653 imported_die = new_die (DW_TAG_imported_module,
25654 lexical_block_die,
25655 lexical_block);
25656 else
25657 return;
25659 else
25660 imported_die = new_die (DW_TAG_imported_declaration,
25661 lexical_block_die,
25662 lexical_block);
25664 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25665 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25666 if (debug_column_info && xloc.column)
25667 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25668 if (name)
25669 add_AT_string (imported_die, DW_AT_name,
25670 IDENTIFIER_POINTER (name));
25671 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25674 /* Output debug information for imported module or decl DECL.
25675 NAME is non-NULL name in context if the decl has been renamed.
25676 CHILD is true if decl is one of the renamed decls as part of
25677 importing whole module.
25678 IMPLICIT is set if this hook is called for an implicit import
25679 such as inline namespace. */
25681 static void
25682 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25683 bool child, bool implicit)
25685 /* dw_die_ref at_import_die; */
25686 dw_die_ref scope_die;
25688 if (debug_info_level <= DINFO_LEVEL_TERSE)
25689 return;
25691 gcc_assert (decl);
25693 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25694 should be enough, for DWARF4 and older even if we emit as extension
25695 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25696 for the benefit of consumers unaware of DW_AT_export_symbols. */
25697 if (implicit
25698 && dwarf_version >= 5
25699 && lang_hooks.decls.decl_dwarf_attribute (decl,
25700 DW_AT_export_symbols) == 1)
25701 return;
25703 set_early_dwarf s;
25705 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25706 We need decl DIE for reference and scope die. First, get DIE for the decl
25707 itself. */
25709 /* Get the scope die for decl context. Use comp_unit_die for global module
25710 or decl. If die is not found for non globals, force new die. */
25711 if (context
25712 && TYPE_P (context)
25713 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25714 return;
25716 scope_die = get_context_die (context);
25718 if (child)
25720 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25721 there is nothing we can do, here. */
25722 if (dwarf_version < 3 && dwarf_strict)
25723 return;
25725 gcc_assert (scope_die->die_child);
25726 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25727 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25728 scope_die = scope_die->die_child;
25731 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25732 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25735 /* Output debug information for namelists. */
25737 static dw_die_ref
25738 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25740 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25741 tree value;
25742 unsigned i;
25744 if (debug_info_level <= DINFO_LEVEL_TERSE)
25745 return NULL;
25747 gcc_assert (scope_die != NULL);
25748 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25749 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25751 /* If there are no item_decls, we have a nondefining namelist, e.g.
25752 with USE association; hence, set DW_AT_declaration. */
25753 if (item_decls == NULL_TREE)
25755 add_AT_flag (nml_die, DW_AT_declaration, 1);
25756 return nml_die;
25759 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25761 nml_item_ref_die = lookup_decl_die (value);
25762 if (!nml_item_ref_die)
25763 nml_item_ref_die = force_decl_die (value);
25765 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25766 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25768 return nml_die;
25772 /* Write the debugging output for DECL and return the DIE. */
25774 static void
25775 dwarf2out_decl (tree decl)
25777 dw_die_ref context_die = comp_unit_die ();
25779 switch (TREE_CODE (decl))
25781 case ERROR_MARK:
25782 return;
25784 case FUNCTION_DECL:
25785 /* If we're a nested function, initially use a parent of NULL; if we're
25786 a plain function, this will be fixed up in decls_for_scope. If
25787 we're a method, it will be ignored, since we already have a DIE. */
25788 if (decl_function_context (decl)
25789 /* But if we're in terse mode, we don't care about scope. */
25790 && debug_info_level > DINFO_LEVEL_TERSE)
25791 context_die = NULL;
25792 break;
25794 case VAR_DECL:
25795 /* For local statics lookup proper context die. */
25796 if (local_function_static (decl))
25797 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25799 /* If we are in terse mode, don't generate any DIEs to represent any
25800 variable declarations or definitions. */
25801 if (debug_info_level <= DINFO_LEVEL_TERSE)
25802 return;
25803 break;
25805 case CONST_DECL:
25806 if (debug_info_level <= DINFO_LEVEL_TERSE)
25807 return;
25808 if (!is_fortran () && !is_ada ())
25809 return;
25810 if (TREE_STATIC (decl) && decl_function_context (decl))
25811 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25812 break;
25814 case NAMESPACE_DECL:
25815 case IMPORTED_DECL:
25816 if (debug_info_level <= DINFO_LEVEL_TERSE)
25817 return;
25818 if (lookup_decl_die (decl) != NULL)
25819 return;
25820 break;
25822 case TYPE_DECL:
25823 /* Don't emit stubs for types unless they are needed by other DIEs. */
25824 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25825 return;
25827 /* Don't bother trying to generate any DIEs to represent any of the
25828 normal built-in types for the language we are compiling. */
25829 if (DECL_IS_BUILTIN (decl))
25830 return;
25832 /* If we are in terse mode, don't generate any DIEs for types. */
25833 if (debug_info_level <= DINFO_LEVEL_TERSE)
25834 return;
25836 /* If we're a function-scope tag, initially use a parent of NULL;
25837 this will be fixed up in decls_for_scope. */
25838 if (decl_function_context (decl))
25839 context_die = NULL;
25841 break;
25843 case NAMELIST_DECL:
25844 break;
25846 default:
25847 return;
25850 gen_decl_die (decl, NULL, NULL, context_die);
25852 if (flag_checking)
25854 dw_die_ref die = lookup_decl_die (decl);
25855 if (die)
25856 check_die (die);
25860 /* Write the debugging output for DECL. */
25862 static void
25863 dwarf2out_function_decl (tree decl)
25865 dwarf2out_decl (decl);
25866 call_arg_locations = NULL;
25867 call_arg_loc_last = NULL;
25868 call_site_count = -1;
25869 tail_call_site_count = -1;
25870 decl_loc_table->empty ();
25871 cached_dw_loc_list_table->empty ();
25874 /* Output a marker (i.e. a label) for the beginning of the generated code for
25875 a lexical block. */
25877 static void
25878 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25879 unsigned int blocknum)
25881 switch_to_section (current_function_section ());
25882 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25885 /* Output a marker (i.e. a label) for the end of the generated code for a
25886 lexical block. */
25888 static void
25889 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25891 switch_to_section (current_function_section ());
25892 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25895 /* Returns nonzero if it is appropriate not to emit any debugging
25896 information for BLOCK, because it doesn't contain any instructions.
25898 Don't allow this for blocks with nested functions or local classes
25899 as we would end up with orphans, and in the presence of scheduling
25900 we may end up calling them anyway. */
25902 static bool
25903 dwarf2out_ignore_block (const_tree block)
25905 tree decl;
25906 unsigned int i;
25908 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25909 if (TREE_CODE (decl) == FUNCTION_DECL
25910 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25911 return 0;
25912 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25914 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25915 if (TREE_CODE (decl) == FUNCTION_DECL
25916 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25917 return 0;
25920 return 1;
25923 /* Hash table routines for file_hash. */
25925 bool
25926 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25928 return filename_cmp (p1->filename, p2) == 0;
25931 hashval_t
25932 dwarf_file_hasher::hash (dwarf_file_data *p)
25934 return htab_hash_string (p->filename);
25937 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25938 dwarf2out.c) and return its "index". The index of each (known) filename is
25939 just a unique number which is associated with only that one filename. We
25940 need such numbers for the sake of generating labels (in the .debug_sfnames
25941 section) and references to those files numbers (in the .debug_srcinfo
25942 and .debug_macinfo sections). If the filename given as an argument is not
25943 found in our current list, add it to the list and assign it the next
25944 available unique index number. */
25946 static struct dwarf_file_data *
25947 lookup_filename (const char *file_name)
25949 struct dwarf_file_data * created;
25951 if (!file_name)
25952 return NULL;
25954 dwarf_file_data **slot
25955 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25956 INSERT);
25957 if (*slot)
25958 return *slot;
25960 created = ggc_alloc<dwarf_file_data> ();
25961 created->filename = file_name;
25962 created->emitted_number = 0;
25963 *slot = created;
25964 return created;
25967 /* If the assembler will construct the file table, then translate the compiler
25968 internal file table number into the assembler file table number, and emit
25969 a .file directive if we haven't already emitted one yet. The file table
25970 numbers are different because we prune debug info for unused variables and
25971 types, which may include filenames. */
25973 static int
25974 maybe_emit_file (struct dwarf_file_data * fd)
25976 if (! fd->emitted_number)
25978 if (last_emitted_file)
25979 fd->emitted_number = last_emitted_file->emitted_number + 1;
25980 else
25981 fd->emitted_number = 1;
25982 last_emitted_file = fd;
25984 if (DWARF2_ASM_LINE_DEBUG_INFO)
25986 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25987 output_quoted_string (asm_out_file,
25988 remap_debug_filename (fd->filename));
25989 fputc ('\n', asm_out_file);
25993 return fd->emitted_number;
25996 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25997 That generation should happen after function debug info has been
25998 generated. The value of the attribute is the constant value of ARG. */
26000 static void
26001 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26003 die_arg_entry entry;
26005 if (!die || !arg)
26006 return;
26008 gcc_assert (early_dwarf);
26010 if (!tmpl_value_parm_die_table)
26011 vec_alloc (tmpl_value_parm_die_table, 32);
26013 entry.die = die;
26014 entry.arg = arg;
26015 vec_safe_push (tmpl_value_parm_die_table, entry);
26018 /* Return TRUE if T is an instance of generic type, FALSE
26019 otherwise. */
26021 static bool
26022 generic_type_p (tree t)
26024 if (t == NULL_TREE || !TYPE_P (t))
26025 return false;
26026 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26029 /* Schedule the generation of the generic parameter dies for the
26030 instance of generic type T. The proper generation itself is later
26031 done by gen_scheduled_generic_parms_dies. */
26033 static void
26034 schedule_generic_params_dies_gen (tree t)
26036 if (!generic_type_p (t))
26037 return;
26039 gcc_assert (early_dwarf);
26041 if (!generic_type_instances)
26042 vec_alloc (generic_type_instances, 256);
26044 vec_safe_push (generic_type_instances, t);
26047 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26048 by append_entry_to_tmpl_value_parm_die_table. This function must
26049 be called after function DIEs have been generated. */
26051 static void
26052 gen_remaining_tmpl_value_param_die_attribute (void)
26054 if (tmpl_value_parm_die_table)
26056 unsigned i, j;
26057 die_arg_entry *e;
26059 /* We do this in two phases - first get the cases we can
26060 handle during early-finish, preserving those we cannot
26061 (containing symbolic constants where we don't yet know
26062 whether we are going to output the referenced symbols).
26063 For those we try again at late-finish. */
26064 j = 0;
26065 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26067 if (!e->die->removed
26068 && !tree_add_const_value_attribute (e->die, e->arg))
26070 dw_loc_descr_ref loc = NULL;
26071 if (! early_dwarf
26072 && (dwarf_version >= 5 || !dwarf_strict))
26073 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26074 if (loc)
26075 add_AT_loc (e->die, DW_AT_location, loc);
26076 else
26077 (*tmpl_value_parm_die_table)[j++] = *e;
26080 tmpl_value_parm_die_table->truncate (j);
26084 /* Generate generic parameters DIEs for instances of generic types
26085 that have been previously scheduled by
26086 schedule_generic_params_dies_gen. This function must be called
26087 after all the types of the CU have been laid out. */
26089 static void
26090 gen_scheduled_generic_parms_dies (void)
26092 unsigned i;
26093 tree t;
26095 if (!generic_type_instances)
26096 return;
26098 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26099 if (COMPLETE_TYPE_P (t))
26100 gen_generic_params_dies (t);
26102 generic_type_instances = NULL;
26106 /* Replace DW_AT_name for the decl with name. */
26108 static void
26109 dwarf2out_set_name (tree decl, tree name)
26111 dw_die_ref die;
26112 dw_attr_node *attr;
26113 const char *dname;
26115 die = TYPE_SYMTAB_DIE (decl);
26116 if (!die)
26117 return;
26119 dname = dwarf2_name (name, 0);
26120 if (!dname)
26121 return;
26123 attr = get_AT (die, DW_AT_name);
26124 if (attr)
26126 struct indirect_string_node *node;
26128 node = find_AT_string (dname);
26129 /* replace the string. */
26130 attr->dw_attr_val.v.val_str = node;
26133 else
26134 add_name_attribute (die, dname);
26137 /* True if before or during processing of the first function being emitted. */
26138 static bool in_first_function_p = true;
26139 /* True if loc_note during dwarf2out_var_location call might still be
26140 before first real instruction at address equal to .Ltext0. */
26141 static bool maybe_at_text_label_p = true;
26142 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26143 static unsigned int first_loclabel_num_not_at_text_label;
26145 /* Called by the final INSN scan whenever we see a var location. We
26146 use it to drop labels in the right places, and throw the location in
26147 our lookup table. */
26149 static void
26150 dwarf2out_var_location (rtx_insn *loc_note)
26152 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26153 struct var_loc_node *newloc;
26154 rtx_insn *next_real, *next_note;
26155 rtx_insn *call_insn = NULL;
26156 static const char *last_label;
26157 static const char *last_postcall_label;
26158 static bool last_in_cold_section_p;
26159 static rtx_insn *expected_next_loc_note;
26160 tree decl;
26161 bool var_loc_p;
26163 if (!NOTE_P (loc_note))
26165 if (CALL_P (loc_note))
26167 call_site_count++;
26168 if (SIBLING_CALL_P (loc_note))
26169 tail_call_site_count++;
26170 if (optimize == 0 && !flag_var_tracking)
26172 /* When the var-tracking pass is not running, there is no note
26173 for indirect calls whose target is compile-time known. In this
26174 case, process such calls specifically so that we generate call
26175 sites for them anyway. */
26176 rtx x = PATTERN (loc_note);
26177 if (GET_CODE (x) == PARALLEL)
26178 x = XVECEXP (x, 0, 0);
26179 if (GET_CODE (x) == SET)
26180 x = SET_SRC (x);
26181 if (GET_CODE (x) == CALL)
26182 x = XEXP (x, 0);
26183 if (!MEM_P (x)
26184 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26185 || !SYMBOL_REF_DECL (XEXP (x, 0))
26186 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26187 != FUNCTION_DECL))
26189 call_insn = loc_note;
26190 loc_note = NULL;
26191 var_loc_p = false;
26193 next_real = next_real_insn (call_insn);
26194 next_note = NULL;
26195 cached_next_real_insn = NULL;
26196 goto create_label;
26200 return;
26203 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26204 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26205 return;
26207 /* Optimize processing a large consecutive sequence of location
26208 notes so we don't spend too much time in next_real_insn. If the
26209 next insn is another location note, remember the next_real_insn
26210 calculation for next time. */
26211 next_real = cached_next_real_insn;
26212 if (next_real)
26214 if (expected_next_loc_note != loc_note)
26215 next_real = NULL;
26218 next_note = NEXT_INSN (loc_note);
26219 if (! next_note
26220 || next_note->deleted ()
26221 || ! NOTE_P (next_note)
26222 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26223 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26224 next_note = NULL;
26226 if (! next_real)
26227 next_real = next_real_insn (loc_note);
26229 if (next_note)
26231 expected_next_loc_note = next_note;
26232 cached_next_real_insn = next_real;
26234 else
26235 cached_next_real_insn = NULL;
26237 /* If there are no instructions which would be affected by this note,
26238 don't do anything. */
26239 if (var_loc_p
26240 && next_real == NULL_RTX
26241 && !NOTE_DURING_CALL_P (loc_note))
26242 return;
26244 create_label:
26246 if (next_real == NULL_RTX)
26247 next_real = get_last_insn ();
26249 /* If there were any real insns between note we processed last time
26250 and this note (or if it is the first note), clear
26251 last_{,postcall_}label so that they are not reused this time. */
26252 if (last_var_location_insn == NULL_RTX
26253 || last_var_location_insn != next_real
26254 || last_in_cold_section_p != in_cold_section_p)
26256 last_label = NULL;
26257 last_postcall_label = NULL;
26260 if (var_loc_p)
26262 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26263 newloc = add_var_loc_to_decl (decl, loc_note,
26264 NOTE_DURING_CALL_P (loc_note)
26265 ? last_postcall_label : last_label);
26266 if (newloc == NULL)
26267 return;
26269 else
26271 decl = NULL_TREE;
26272 newloc = NULL;
26275 /* If there were no real insns between note we processed last time
26276 and this note, use the label we emitted last time. Otherwise
26277 create a new label and emit it. */
26278 if (last_label == NULL)
26280 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26281 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26282 loclabel_num++;
26283 last_label = ggc_strdup (loclabel);
26284 /* See if loclabel might be equal to .Ltext0. If yes,
26285 bump first_loclabel_num_not_at_text_label. */
26286 if (!have_multiple_function_sections
26287 && in_first_function_p
26288 && maybe_at_text_label_p)
26290 static rtx_insn *last_start;
26291 rtx_insn *insn;
26292 for (insn = loc_note; insn; insn = previous_insn (insn))
26293 if (insn == last_start)
26294 break;
26295 else if (!NONDEBUG_INSN_P (insn))
26296 continue;
26297 else
26299 rtx body = PATTERN (insn);
26300 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26301 continue;
26302 /* Inline asm could occupy zero bytes. */
26303 else if (GET_CODE (body) == ASM_INPUT
26304 || asm_noperands (body) >= 0)
26305 continue;
26306 #ifdef HAVE_attr_length
26307 else if (get_attr_min_length (insn) == 0)
26308 continue;
26309 #endif
26310 else
26312 /* Assume insn has non-zero length. */
26313 maybe_at_text_label_p = false;
26314 break;
26317 if (maybe_at_text_label_p)
26319 last_start = loc_note;
26320 first_loclabel_num_not_at_text_label = loclabel_num;
26325 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26326 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26328 if (!var_loc_p)
26330 struct call_arg_loc_node *ca_loc
26331 = ggc_cleared_alloc<call_arg_loc_node> ();
26332 rtx_insn *prev
26333 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26335 ca_loc->call_arg_loc_note = loc_note;
26336 ca_loc->next = NULL;
26337 ca_loc->label = last_label;
26338 gcc_assert (prev
26339 && (CALL_P (prev)
26340 || (NONJUMP_INSN_P (prev)
26341 && GET_CODE (PATTERN (prev)) == SEQUENCE
26342 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26343 if (!CALL_P (prev))
26344 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26345 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26347 /* Look for a SYMBOL_REF in the "prev" instruction. */
26348 rtx x = get_call_rtx_from (PATTERN (prev));
26349 if (x)
26351 /* Try to get the call symbol, if any. */
26352 if (MEM_P (XEXP (x, 0)))
26353 x = XEXP (x, 0);
26354 /* First, look for a memory access to a symbol_ref. */
26355 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26356 && SYMBOL_REF_DECL (XEXP (x, 0))
26357 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26358 ca_loc->symbol_ref = XEXP (x, 0);
26359 /* Otherwise, look at a compile-time known user-level function
26360 declaration. */
26361 else if (MEM_P (x)
26362 && MEM_EXPR (x)
26363 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26364 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26367 ca_loc->block = insn_scope (prev);
26368 if (call_arg_locations)
26369 call_arg_loc_last->next = ca_loc;
26370 else
26371 call_arg_locations = ca_loc;
26372 call_arg_loc_last = ca_loc;
26374 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26375 newloc->label = last_label;
26376 else
26378 if (!last_postcall_label)
26380 sprintf (loclabel, "%s-1", last_label);
26381 last_postcall_label = ggc_strdup (loclabel);
26383 newloc->label = last_postcall_label;
26386 last_var_location_insn = next_real;
26387 last_in_cold_section_p = in_cold_section_p;
26390 /* Called from finalize_size_functions for size functions so that their body
26391 can be encoded in the debug info to describe the layout of variable-length
26392 structures. */
26394 static void
26395 dwarf2out_size_function (tree decl)
26397 function_to_dwarf_procedure (decl);
26400 /* Note in one location list that text section has changed. */
26403 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26405 var_loc_list *list = *slot;
26406 if (list->first)
26407 list->last_before_switch
26408 = list->last->next ? list->last->next : list->last;
26409 return 1;
26412 /* Note in all location lists that text section has changed. */
26414 static void
26415 var_location_switch_text_section (void)
26417 if (decl_loc_table == NULL)
26418 return;
26420 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26423 /* Create a new line number table. */
26425 static dw_line_info_table *
26426 new_line_info_table (void)
26428 dw_line_info_table *table;
26430 table = ggc_cleared_alloc<dw_line_info_table> ();
26431 table->file_num = 1;
26432 table->line_num = 1;
26433 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26435 return table;
26438 /* Lookup the "current" table into which we emit line info, so
26439 that we don't have to do it for every source line. */
26441 static void
26442 set_cur_line_info_table (section *sec)
26444 dw_line_info_table *table;
26446 if (sec == text_section)
26447 table = text_section_line_info;
26448 else if (sec == cold_text_section)
26450 table = cold_text_section_line_info;
26451 if (!table)
26453 cold_text_section_line_info = table = new_line_info_table ();
26454 table->end_label = cold_end_label;
26457 else
26459 const char *end_label;
26461 if (crtl->has_bb_partition)
26463 if (in_cold_section_p)
26464 end_label = crtl->subsections.cold_section_end_label;
26465 else
26466 end_label = crtl->subsections.hot_section_end_label;
26468 else
26470 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26471 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26472 current_function_funcdef_no);
26473 end_label = ggc_strdup (label);
26476 table = new_line_info_table ();
26477 table->end_label = end_label;
26479 vec_safe_push (separate_line_info, table);
26482 if (DWARF2_ASM_LINE_DEBUG_INFO)
26483 table->is_stmt = (cur_line_info_table
26484 ? cur_line_info_table->is_stmt
26485 : DWARF_LINE_DEFAULT_IS_STMT_START);
26486 cur_line_info_table = table;
26490 /* We need to reset the locations at the beginning of each
26491 function. We can't do this in the end_function hook, because the
26492 declarations that use the locations won't have been output when
26493 that hook is called. Also compute have_multiple_function_sections here. */
26495 static void
26496 dwarf2out_begin_function (tree fun)
26498 section *sec = function_section (fun);
26500 if (sec != text_section)
26501 have_multiple_function_sections = true;
26503 if (crtl->has_bb_partition && !cold_text_section)
26505 gcc_assert (current_function_decl == fun);
26506 cold_text_section = unlikely_text_section ();
26507 switch_to_section (cold_text_section);
26508 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26509 switch_to_section (sec);
26512 dwarf2out_note_section_used ();
26513 call_site_count = 0;
26514 tail_call_site_count = 0;
26516 set_cur_line_info_table (sec);
26519 /* Helper function of dwarf2out_end_function, called only after emitting
26520 the very first function into assembly. Check if some .debug_loc range
26521 might end with a .LVL* label that could be equal to .Ltext0.
26522 In that case we must force using absolute addresses in .debug_loc ranges,
26523 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26524 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26525 list terminator.
26526 Set have_multiple_function_sections to true in that case and
26527 terminate htab traversal. */
26530 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26532 var_loc_list *entry = *slot;
26533 struct var_loc_node *node;
26535 node = entry->first;
26536 if (node && node->next && node->next->label)
26538 unsigned int i;
26539 const char *label = node->next->label;
26540 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26542 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26544 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26545 if (strcmp (label, loclabel) == 0)
26547 have_multiple_function_sections = true;
26548 return 0;
26552 return 1;
26555 /* Hook called after emitting a function into assembly.
26556 This does something only for the very first function emitted. */
26558 static void
26559 dwarf2out_end_function (unsigned int)
26561 if (in_first_function_p
26562 && !have_multiple_function_sections
26563 && first_loclabel_num_not_at_text_label
26564 && decl_loc_table)
26565 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26566 in_first_function_p = false;
26567 maybe_at_text_label_p = false;
26570 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26571 front-ends register a translation unit even before dwarf2out_init is
26572 called. */
26573 static tree main_translation_unit = NULL_TREE;
26575 /* Hook called by front-ends after they built their main translation unit.
26576 Associate comp_unit_die to UNIT. */
26578 static void
26579 dwarf2out_register_main_translation_unit (tree unit)
26581 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26582 && main_translation_unit == NULL_TREE);
26583 main_translation_unit = unit;
26584 /* If dwarf2out_init has not been called yet, it will perform the association
26585 itself looking at main_translation_unit. */
26586 if (decl_die_table != NULL)
26587 equate_decl_number_to_die (unit, comp_unit_die ());
26590 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26592 static void
26593 push_dw_line_info_entry (dw_line_info_table *table,
26594 enum dw_line_info_opcode opcode, unsigned int val)
26596 dw_line_info_entry e;
26597 e.opcode = opcode;
26598 e.val = val;
26599 vec_safe_push (table->entries, e);
26602 /* Output a label to mark the beginning of a source code line entry
26603 and record information relating to this source line, in
26604 'line_info_table' for later output of the .debug_line section. */
26605 /* ??? The discriminator parameter ought to be unsigned. */
26607 static void
26608 dwarf2out_source_line (unsigned int line, unsigned int column,
26609 const char *filename,
26610 int discriminator, bool is_stmt)
26612 unsigned int file_num;
26613 dw_line_info_table *table;
26615 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26616 return;
26618 /* The discriminator column was added in dwarf4. Simplify the below
26619 by simply removing it if we're not supposed to output it. */
26620 if (dwarf_version < 4 && dwarf_strict)
26621 discriminator = 0;
26623 if (!debug_column_info)
26624 column = 0;
26626 table = cur_line_info_table;
26627 file_num = maybe_emit_file (lookup_filename (filename));
26629 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26630 the debugger has used the second (possibly duplicate) line number
26631 at the beginning of the function to mark the end of the prologue.
26632 We could eliminate any other duplicates within the function. For
26633 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26634 that second line number entry. */
26635 /* Recall that this end-of-prologue indication is *not* the same thing
26636 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26637 to which the hook corresponds, follows the last insn that was
26638 emitted by gen_prologue. What we need is to precede the first insn
26639 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26640 insn that corresponds to something the user wrote. These may be
26641 very different locations once scheduling is enabled. */
26643 if (0 && file_num == table->file_num
26644 && line == table->line_num
26645 && column == table->column_num
26646 && discriminator == table->discrim_num
26647 && is_stmt == table->is_stmt)
26648 return;
26650 switch_to_section (current_function_section ());
26652 /* If requested, emit something human-readable. */
26653 if (flag_debug_asm)
26655 if (debug_column_info)
26656 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26657 filename, line, column);
26658 else
26659 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26660 filename, line);
26663 if (DWARF2_ASM_LINE_DEBUG_INFO)
26665 /* Emit the .loc directive understood by GNU as. */
26666 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26667 file_num, line, is_stmt, discriminator */
26668 fputs ("\t.loc ", asm_out_file);
26669 fprint_ul (asm_out_file, file_num);
26670 putc (' ', asm_out_file);
26671 fprint_ul (asm_out_file, line);
26672 putc (' ', asm_out_file);
26673 fprint_ul (asm_out_file, column);
26675 if (is_stmt != table->is_stmt)
26677 fputs (" is_stmt ", asm_out_file);
26678 putc (is_stmt ? '1' : '0', asm_out_file);
26680 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26682 gcc_assert (discriminator > 0);
26683 fputs (" discriminator ", asm_out_file);
26684 fprint_ul (asm_out_file, (unsigned long) discriminator);
26686 putc ('\n', asm_out_file);
26688 else
26690 unsigned int label_num = ++line_info_label_num;
26692 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26694 push_dw_line_info_entry (table, LI_set_address, label_num);
26695 if (file_num != table->file_num)
26696 push_dw_line_info_entry (table, LI_set_file, file_num);
26697 if (discriminator != table->discrim_num)
26698 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26699 if (is_stmt != table->is_stmt)
26700 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26701 push_dw_line_info_entry (table, LI_set_line, line);
26702 if (debug_column_info)
26703 push_dw_line_info_entry (table, LI_set_column, column);
26706 table->file_num = file_num;
26707 table->line_num = line;
26708 table->column_num = column;
26709 table->discrim_num = discriminator;
26710 table->is_stmt = is_stmt;
26711 table->in_use = true;
26714 /* Record the beginning of a new source file. */
26716 static void
26717 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26719 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26721 macinfo_entry e;
26722 e.code = DW_MACINFO_start_file;
26723 e.lineno = lineno;
26724 e.info = ggc_strdup (filename);
26725 vec_safe_push (macinfo_table, e);
26729 /* Record the end of a source file. */
26731 static void
26732 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26734 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26736 macinfo_entry e;
26737 e.code = DW_MACINFO_end_file;
26738 e.lineno = lineno;
26739 e.info = NULL;
26740 vec_safe_push (macinfo_table, e);
26744 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26745 the tail part of the directive line, i.e. the part which is past the
26746 initial whitespace, #, whitespace, directive-name, whitespace part. */
26748 static void
26749 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26750 const char *buffer ATTRIBUTE_UNUSED)
26752 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26754 macinfo_entry e;
26755 /* Insert a dummy first entry to be able to optimize the whole
26756 predefined macro block using DW_MACRO_import. */
26757 if (macinfo_table->is_empty () && lineno <= 1)
26759 e.code = 0;
26760 e.lineno = 0;
26761 e.info = NULL;
26762 vec_safe_push (macinfo_table, e);
26764 e.code = DW_MACINFO_define;
26765 e.lineno = lineno;
26766 e.info = ggc_strdup (buffer);
26767 vec_safe_push (macinfo_table, e);
26771 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26772 the tail part of the directive line, i.e. the part which is past the
26773 initial whitespace, #, whitespace, directive-name, whitespace part. */
26775 static void
26776 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26777 const char *buffer ATTRIBUTE_UNUSED)
26779 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26781 macinfo_entry e;
26782 /* Insert a dummy first entry to be able to optimize the whole
26783 predefined macro block using DW_MACRO_import. */
26784 if (macinfo_table->is_empty () && lineno <= 1)
26786 e.code = 0;
26787 e.lineno = 0;
26788 e.info = NULL;
26789 vec_safe_push (macinfo_table, e);
26791 e.code = DW_MACINFO_undef;
26792 e.lineno = lineno;
26793 e.info = ggc_strdup (buffer);
26794 vec_safe_push (macinfo_table, e);
26798 /* Helpers to manipulate hash table of CUs. */
26800 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26802 static inline hashval_t hash (const macinfo_entry *);
26803 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26806 inline hashval_t
26807 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26809 return htab_hash_string (entry->info);
26812 inline bool
26813 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26814 const macinfo_entry *entry2)
26816 return !strcmp (entry1->info, entry2->info);
26819 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26821 /* Output a single .debug_macinfo entry. */
26823 static void
26824 output_macinfo_op (macinfo_entry *ref)
26826 int file_num;
26827 size_t len;
26828 struct indirect_string_node *node;
26829 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26830 struct dwarf_file_data *fd;
26832 switch (ref->code)
26834 case DW_MACINFO_start_file:
26835 fd = lookup_filename (ref->info);
26836 file_num = maybe_emit_file (fd);
26837 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26838 dw2_asm_output_data_uleb128 (ref->lineno,
26839 "Included from line number %lu",
26840 (unsigned long) ref->lineno);
26841 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26842 break;
26843 case DW_MACINFO_end_file:
26844 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26845 break;
26846 case DW_MACINFO_define:
26847 case DW_MACINFO_undef:
26848 len = strlen (ref->info) + 1;
26849 if (!dwarf_strict
26850 && len > DWARF_OFFSET_SIZE
26851 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26852 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26854 ref->code = ref->code == DW_MACINFO_define
26855 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26856 output_macinfo_op (ref);
26857 return;
26859 dw2_asm_output_data (1, ref->code,
26860 ref->code == DW_MACINFO_define
26861 ? "Define macro" : "Undefine macro");
26862 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26863 (unsigned long) ref->lineno);
26864 dw2_asm_output_nstring (ref->info, -1, "The macro");
26865 break;
26866 case DW_MACRO_define_strp:
26867 case DW_MACRO_undef_strp:
26868 node = find_AT_string (ref->info);
26869 gcc_assert (node
26870 && (node->form == DW_FORM_strp
26871 || node->form == DW_FORM_GNU_str_index));
26872 dw2_asm_output_data (1, ref->code,
26873 ref->code == DW_MACRO_define_strp
26874 ? "Define macro strp"
26875 : "Undefine macro strp");
26876 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26877 (unsigned long) ref->lineno);
26878 if (node->form == DW_FORM_strp)
26879 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26880 debug_str_section, "The macro: \"%s\"",
26881 ref->info);
26882 else
26883 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26884 ref->info);
26885 break;
26886 case DW_MACRO_import:
26887 dw2_asm_output_data (1, ref->code, "Import");
26888 ASM_GENERATE_INTERNAL_LABEL (label,
26889 DEBUG_MACRO_SECTION_LABEL,
26890 ref->lineno + macinfo_label_base);
26891 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26892 break;
26893 default:
26894 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26895 ASM_COMMENT_START, (unsigned long) ref->code);
26896 break;
26900 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26901 other compilation unit .debug_macinfo sections. IDX is the first
26902 index of a define/undef, return the number of ops that should be
26903 emitted in a comdat .debug_macinfo section and emit
26904 a DW_MACRO_import entry referencing it.
26905 If the define/undef entry should be emitted normally, return 0. */
26907 static unsigned
26908 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26909 macinfo_hash_type **macinfo_htab)
26911 macinfo_entry *first, *second, *cur, *inc;
26912 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26913 unsigned char checksum[16];
26914 struct md5_ctx ctx;
26915 char *grp_name, *tail;
26916 const char *base;
26917 unsigned int i, count, encoded_filename_len, linebuf_len;
26918 macinfo_entry **slot;
26920 first = &(*macinfo_table)[idx];
26921 second = &(*macinfo_table)[idx + 1];
26923 /* Optimize only if there are at least two consecutive define/undef ops,
26924 and either all of them are before first DW_MACINFO_start_file
26925 with lineno {0,1} (i.e. predefined macro block), or all of them are
26926 in some included header file. */
26927 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26928 return 0;
26929 if (vec_safe_is_empty (files))
26931 if (first->lineno > 1 || second->lineno > 1)
26932 return 0;
26934 else if (first->lineno == 0)
26935 return 0;
26937 /* Find the last define/undef entry that can be grouped together
26938 with first and at the same time compute md5 checksum of their
26939 codes, linenumbers and strings. */
26940 md5_init_ctx (&ctx);
26941 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26942 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26943 break;
26944 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26945 break;
26946 else
26948 unsigned char code = cur->code;
26949 md5_process_bytes (&code, 1, &ctx);
26950 checksum_uleb128 (cur->lineno, &ctx);
26951 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26953 md5_finish_ctx (&ctx, checksum);
26954 count = i - idx;
26956 /* From the containing include filename (if any) pick up just
26957 usable characters from its basename. */
26958 if (vec_safe_is_empty (files))
26959 base = "";
26960 else
26961 base = lbasename (files->last ().info);
26962 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26963 if (ISIDNUM (base[i]) || base[i] == '.')
26964 encoded_filename_len++;
26965 /* Count . at the end. */
26966 if (encoded_filename_len)
26967 encoded_filename_len++;
26969 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26970 linebuf_len = strlen (linebuf);
26972 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26973 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26974 + 16 * 2 + 1);
26975 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26976 tail = grp_name + 4;
26977 if (encoded_filename_len)
26979 for (i = 0; base[i]; i++)
26980 if (ISIDNUM (base[i]) || base[i] == '.')
26981 *tail++ = base[i];
26982 *tail++ = '.';
26984 memcpy (tail, linebuf, linebuf_len);
26985 tail += linebuf_len;
26986 *tail++ = '.';
26987 for (i = 0; i < 16; i++)
26988 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26990 /* Construct a macinfo_entry for DW_MACRO_import
26991 in the empty vector entry before the first define/undef. */
26992 inc = &(*macinfo_table)[idx - 1];
26993 inc->code = DW_MACRO_import;
26994 inc->lineno = 0;
26995 inc->info = ggc_strdup (grp_name);
26996 if (!*macinfo_htab)
26997 *macinfo_htab = new macinfo_hash_type (10);
26998 /* Avoid emitting duplicates. */
26999 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27000 if (*slot != NULL)
27002 inc->code = 0;
27003 inc->info = NULL;
27004 /* If such an entry has been used before, just emit
27005 a DW_MACRO_import op. */
27006 inc = *slot;
27007 output_macinfo_op (inc);
27008 /* And clear all macinfo_entry in the range to avoid emitting them
27009 in the second pass. */
27010 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27012 cur->code = 0;
27013 cur->info = NULL;
27016 else
27018 *slot = inc;
27019 inc->lineno = (*macinfo_htab)->elements ();
27020 output_macinfo_op (inc);
27022 return count;
27025 /* Save any strings needed by the macinfo table in the debug str
27026 table. All strings must be collected into the table by the time
27027 index_string is called. */
27029 static void
27030 save_macinfo_strings (void)
27032 unsigned len;
27033 unsigned i;
27034 macinfo_entry *ref;
27036 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27038 switch (ref->code)
27040 /* Match the logic in output_macinfo_op to decide on
27041 indirect strings. */
27042 case DW_MACINFO_define:
27043 case DW_MACINFO_undef:
27044 len = strlen (ref->info) + 1;
27045 if (!dwarf_strict
27046 && len > DWARF_OFFSET_SIZE
27047 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27048 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27049 set_indirect_string (find_AT_string (ref->info));
27050 break;
27051 case DW_MACRO_define_strp:
27052 case DW_MACRO_undef_strp:
27053 set_indirect_string (find_AT_string (ref->info));
27054 break;
27055 default:
27056 break;
27061 /* Output macinfo section(s). */
27063 static void
27064 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27066 unsigned i;
27067 unsigned long length = vec_safe_length (macinfo_table);
27068 macinfo_entry *ref;
27069 vec<macinfo_entry, va_gc> *files = NULL;
27070 macinfo_hash_type *macinfo_htab = NULL;
27071 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27073 if (! length)
27074 return;
27076 /* output_macinfo* uses these interchangeably. */
27077 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27078 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27079 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27080 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27082 /* AIX Assembler inserts the length, so adjust the reference to match the
27083 offset expected by debuggers. */
27084 strcpy (dl_section_ref, debug_line_label);
27085 if (XCOFF_DEBUGGING_INFO)
27086 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27088 /* For .debug_macro emit the section header. */
27089 if (!dwarf_strict || dwarf_version >= 5)
27091 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27092 "DWARF macro version number");
27093 if (DWARF_OFFSET_SIZE == 8)
27094 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27095 else
27096 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27097 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27098 debug_line_section, NULL);
27101 /* In the first loop, it emits the primary .debug_macinfo section
27102 and after each emitted op the macinfo_entry is cleared.
27103 If a longer range of define/undef ops can be optimized using
27104 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27105 the vector before the first define/undef in the range and the
27106 whole range of define/undef ops is not emitted and kept. */
27107 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27109 switch (ref->code)
27111 case DW_MACINFO_start_file:
27112 vec_safe_push (files, *ref);
27113 break;
27114 case DW_MACINFO_end_file:
27115 if (!vec_safe_is_empty (files))
27116 files->pop ();
27117 break;
27118 case DW_MACINFO_define:
27119 case DW_MACINFO_undef:
27120 if ((!dwarf_strict || dwarf_version >= 5)
27121 && HAVE_COMDAT_GROUP
27122 && vec_safe_length (files) != 1
27123 && i > 0
27124 && i + 1 < length
27125 && (*macinfo_table)[i - 1].code == 0)
27127 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27128 if (count)
27130 i += count - 1;
27131 continue;
27134 break;
27135 case 0:
27136 /* A dummy entry may be inserted at the beginning to be able
27137 to optimize the whole block of predefined macros. */
27138 if (i == 0)
27139 continue;
27140 default:
27141 break;
27143 output_macinfo_op (ref);
27144 ref->info = NULL;
27145 ref->code = 0;
27148 if (!macinfo_htab)
27149 return;
27151 /* Save the number of transparent includes so we can adjust the
27152 label number for the fat LTO object DWARF. */
27153 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27155 delete macinfo_htab;
27156 macinfo_htab = NULL;
27158 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27159 terminate the current chain and switch to a new comdat .debug_macinfo
27160 section and emit the define/undef entries within it. */
27161 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27162 switch (ref->code)
27164 case 0:
27165 continue;
27166 case DW_MACRO_import:
27168 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27169 tree comdat_key = get_identifier (ref->info);
27170 /* Terminate the previous .debug_macinfo section. */
27171 dw2_asm_output_data (1, 0, "End compilation unit");
27172 targetm.asm_out.named_section (debug_macinfo_section_name,
27173 SECTION_DEBUG
27174 | SECTION_LINKONCE
27175 | (early_lto_debug
27176 ? SECTION_EXCLUDE : 0),
27177 comdat_key);
27178 ASM_GENERATE_INTERNAL_LABEL (label,
27179 DEBUG_MACRO_SECTION_LABEL,
27180 ref->lineno + macinfo_label_base);
27181 ASM_OUTPUT_LABEL (asm_out_file, label);
27182 ref->code = 0;
27183 ref->info = NULL;
27184 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27185 "DWARF macro version number");
27186 if (DWARF_OFFSET_SIZE == 8)
27187 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27188 else
27189 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27191 break;
27192 case DW_MACINFO_define:
27193 case DW_MACINFO_undef:
27194 output_macinfo_op (ref);
27195 ref->code = 0;
27196 ref->info = NULL;
27197 break;
27198 default:
27199 gcc_unreachable ();
27202 macinfo_label_base += macinfo_label_base_adj;
27205 /* Initialize the various sections and labels for dwarf output and prefix
27206 them with PREFIX if non-NULL. */
27208 static void
27209 init_sections_and_labels (bool early_lto_debug)
27211 /* As we may get called multiple times have a generation count for
27212 labels. */
27213 static unsigned generation = 0;
27215 if (early_lto_debug)
27217 if (!dwarf_split_debug_info)
27219 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27220 SECTION_DEBUG | SECTION_EXCLUDE,
27221 NULL);
27222 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27223 SECTION_DEBUG | SECTION_EXCLUDE,
27224 NULL);
27225 debug_macinfo_section_name
27226 = ((dwarf_strict && dwarf_version < 5)
27227 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27228 debug_macinfo_section = get_section (debug_macinfo_section_name,
27229 SECTION_DEBUG
27230 | SECTION_EXCLUDE, NULL);
27231 /* For macro info we have to refer to a debug_line section, so
27232 similar to split-dwarf emit a skeleton one for early debug. */
27233 debug_skeleton_line_section
27234 = get_section (DEBUG_LTO_LINE_SECTION,
27235 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27236 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27237 DEBUG_SKELETON_LINE_SECTION_LABEL,
27238 generation);
27240 else
27242 /* ??? Which of the following do we need early? */
27243 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27244 SECTION_DEBUG | SECTION_EXCLUDE,
27245 NULL);
27246 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27247 SECTION_DEBUG | SECTION_EXCLUDE,
27248 NULL);
27249 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27250 SECTION_DEBUG
27251 | SECTION_EXCLUDE, NULL);
27252 debug_skeleton_abbrev_section
27253 = get_section (DEBUG_LTO_ABBREV_SECTION,
27254 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27255 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27256 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27257 generation);
27259 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27260 stay in the main .o, but the skeleton_line goes into the split
27261 off dwo. */
27262 debug_skeleton_line_section
27263 = get_section (DEBUG_LTO_LINE_SECTION,
27264 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27265 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27266 DEBUG_SKELETON_LINE_SECTION_LABEL,
27267 generation);
27268 debug_str_offsets_section
27269 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27270 SECTION_DEBUG | SECTION_EXCLUDE,
27271 NULL);
27272 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27273 DEBUG_SKELETON_INFO_SECTION_LABEL,
27274 generation);
27275 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27276 DEBUG_STR_DWO_SECTION_FLAGS,
27277 NULL);
27278 debug_macinfo_section_name
27279 = ((dwarf_strict && dwarf_version < 5)
27280 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27281 debug_macinfo_section = get_section (debug_macinfo_section_name,
27282 SECTION_DEBUG | SECTION_EXCLUDE,
27283 NULL);
27285 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27286 DEBUG_STR_SECTION_FLAGS
27287 | SECTION_EXCLUDE, NULL);
27288 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27289 debug_line_str_section
27290 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27291 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27293 else
27295 if (!dwarf_split_debug_info)
27297 debug_info_section = get_section (DEBUG_INFO_SECTION,
27298 SECTION_DEBUG, NULL);
27299 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27300 SECTION_DEBUG, NULL);
27301 debug_loc_section = get_section (dwarf_version >= 5
27302 ? DEBUG_LOCLISTS_SECTION
27303 : DEBUG_LOC_SECTION,
27304 SECTION_DEBUG, NULL);
27305 debug_macinfo_section_name
27306 = ((dwarf_strict && dwarf_version < 5)
27307 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27308 debug_macinfo_section = get_section (debug_macinfo_section_name,
27309 SECTION_DEBUG, NULL);
27311 else
27313 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27314 SECTION_DEBUG | SECTION_EXCLUDE,
27315 NULL);
27316 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27317 SECTION_DEBUG | SECTION_EXCLUDE,
27318 NULL);
27319 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27320 SECTION_DEBUG, NULL);
27321 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27322 SECTION_DEBUG, NULL);
27323 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27324 SECTION_DEBUG, NULL);
27325 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27326 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27327 generation);
27329 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27330 stay in the main .o, but the skeleton_line goes into the
27331 split off dwo. */
27332 debug_skeleton_line_section
27333 = get_section (DEBUG_DWO_LINE_SECTION,
27334 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27335 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27336 DEBUG_SKELETON_LINE_SECTION_LABEL,
27337 generation);
27338 debug_str_offsets_section
27339 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27340 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27341 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27342 DEBUG_SKELETON_INFO_SECTION_LABEL,
27343 generation);
27344 debug_loc_section = get_section (dwarf_version >= 5
27345 ? DEBUG_DWO_LOCLISTS_SECTION
27346 : DEBUG_DWO_LOC_SECTION,
27347 SECTION_DEBUG | SECTION_EXCLUDE,
27348 NULL);
27349 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27350 DEBUG_STR_DWO_SECTION_FLAGS,
27351 NULL);
27352 debug_macinfo_section_name
27353 = ((dwarf_strict && dwarf_version < 5)
27354 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27355 debug_macinfo_section = get_section (debug_macinfo_section_name,
27356 SECTION_DEBUG | SECTION_EXCLUDE,
27357 NULL);
27359 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27360 SECTION_DEBUG, NULL);
27361 debug_line_section = get_section (DEBUG_LINE_SECTION,
27362 SECTION_DEBUG, NULL);
27363 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27364 SECTION_DEBUG, NULL);
27365 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27366 SECTION_DEBUG, NULL);
27367 debug_str_section = get_section (DEBUG_STR_SECTION,
27368 DEBUG_STR_SECTION_FLAGS, NULL);
27369 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27370 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27371 DEBUG_STR_SECTION_FLAGS, NULL);
27372 debug_ranges_section = get_section (dwarf_version >= 5
27373 ? DEBUG_RNGLISTS_SECTION
27374 : DEBUG_RANGES_SECTION,
27375 SECTION_DEBUG, NULL);
27376 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27377 SECTION_DEBUG, NULL);
27380 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27381 DEBUG_ABBREV_SECTION_LABEL, generation);
27382 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27383 DEBUG_INFO_SECTION_LABEL, generation);
27384 info_section_emitted = false;
27385 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27386 DEBUG_LINE_SECTION_LABEL, generation);
27387 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27388 DEBUG_RANGES_SECTION_LABEL, generation);
27389 if (dwarf_version >= 5 && dwarf_split_debug_info)
27390 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27391 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27392 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27393 DEBUG_ADDR_SECTION_LABEL, generation);
27394 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27395 (dwarf_strict && dwarf_version < 5)
27396 ? DEBUG_MACINFO_SECTION_LABEL
27397 : DEBUG_MACRO_SECTION_LABEL, generation);
27398 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27399 generation);
27401 ++generation;
27404 /* Set up for Dwarf output at the start of compilation. */
27406 static void
27407 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27409 /* Allocate the file_table. */
27410 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27412 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27413 /* Allocate the decl_die_table. */
27414 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27416 /* Allocate the decl_loc_table. */
27417 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27419 /* Allocate the cached_dw_loc_list_table. */
27420 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27422 /* Allocate the initial hunk of the decl_scope_table. */
27423 vec_alloc (decl_scope_table, 256);
27425 /* Allocate the initial hunk of the abbrev_die_table. */
27426 vec_alloc (abbrev_die_table, 256);
27427 /* Zero-th entry is allocated, but unused. */
27428 abbrev_die_table->quick_push (NULL);
27430 /* Allocate the dwarf_proc_stack_usage_map. */
27431 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27433 /* Allocate the pubtypes and pubnames vectors. */
27434 vec_alloc (pubname_table, 32);
27435 vec_alloc (pubtype_table, 32);
27437 vec_alloc (incomplete_types, 64);
27439 vec_alloc (used_rtx_array, 32);
27441 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27442 vec_alloc (macinfo_table, 64);
27443 #endif
27445 /* If front-ends already registered a main translation unit but we were not
27446 ready to perform the association, do this now. */
27447 if (main_translation_unit != NULL_TREE)
27448 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27451 /* Called before compile () starts outputtting functions, variables
27452 and toplevel asms into assembly. */
27454 static void
27455 dwarf2out_assembly_start (void)
27457 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27458 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27459 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27460 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27461 COLD_TEXT_SECTION_LABEL, 0);
27462 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27464 switch_to_section (text_section);
27465 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27466 #endif
27468 /* Make sure the line number table for .text always exists. */
27469 text_section_line_info = new_line_info_table ();
27470 text_section_line_info->end_label = text_end_label;
27472 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27473 cur_line_info_table = text_section_line_info;
27474 #endif
27476 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27477 && dwarf2out_do_cfi_asm ()
27478 && (!(flag_unwind_tables || flag_exceptions)
27479 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27480 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27483 /* A helper function for dwarf2out_finish called through
27484 htab_traverse. Assign a string its index. All strings must be
27485 collected into the table by the time index_string is called,
27486 because the indexing code relies on htab_traverse to traverse nodes
27487 in the same order for each run. */
27490 index_string (indirect_string_node **h, unsigned int *index)
27492 indirect_string_node *node = *h;
27494 find_string_form (node);
27495 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27497 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27498 node->index = *index;
27499 *index += 1;
27501 return 1;
27504 /* A helper function for output_indirect_strings called through
27505 htab_traverse. Output the offset to a string and update the
27506 current offset. */
27509 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27511 indirect_string_node *node = *h;
27513 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27515 /* Assert that this node has been assigned an index. */
27516 gcc_assert (node->index != NO_INDEX_ASSIGNED
27517 && node->index != NOT_INDEXED);
27518 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27519 "indexed string 0x%x: %s", node->index, node->str);
27520 *offset += strlen (node->str) + 1;
27522 return 1;
27525 /* A helper function for dwarf2out_finish called through
27526 htab_traverse. Output the indexed string. */
27529 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27531 struct indirect_string_node *node = *h;
27533 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27535 /* Assert that the strings are output in the same order as their
27536 indexes were assigned. */
27537 gcc_assert (*cur_idx == node->index);
27538 assemble_string (node->str, strlen (node->str) + 1);
27539 *cur_idx += 1;
27541 return 1;
27544 /* A helper function for dwarf2out_finish called through
27545 htab_traverse. Emit one queued .debug_str string. */
27548 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27550 struct indirect_string_node *node = *h;
27552 node->form = find_string_form (node);
27553 if (node->form == form && node->refcount > 0)
27555 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27556 assemble_string (node->str, strlen (node->str) + 1);
27559 return 1;
27562 /* Output the indexed string table. */
27564 static void
27565 output_indirect_strings (void)
27567 switch_to_section (debug_str_section);
27568 if (!dwarf_split_debug_info)
27569 debug_str_hash->traverse<enum dwarf_form,
27570 output_indirect_string> (DW_FORM_strp);
27571 else
27573 unsigned int offset = 0;
27574 unsigned int cur_idx = 0;
27576 skeleton_debug_str_hash->traverse<enum dwarf_form,
27577 output_indirect_string> (DW_FORM_strp);
27579 switch_to_section (debug_str_offsets_section);
27580 debug_str_hash->traverse_noresize
27581 <unsigned int *, output_index_string_offset> (&offset);
27582 switch_to_section (debug_str_dwo_section);
27583 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27584 (&cur_idx);
27588 /* Callback for htab_traverse to assign an index to an entry in the
27589 table, and to write that entry to the .debug_addr section. */
27592 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27594 addr_table_entry *entry = *slot;
27596 if (entry->refcount == 0)
27598 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27599 || entry->index == NOT_INDEXED);
27600 return 1;
27603 gcc_assert (entry->index == *cur_index);
27604 (*cur_index)++;
27606 switch (entry->kind)
27608 case ate_kind_rtx:
27609 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27610 "0x%x", entry->index);
27611 break;
27612 case ate_kind_rtx_dtprel:
27613 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27614 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27615 DWARF2_ADDR_SIZE,
27616 entry->addr.rtl);
27617 fputc ('\n', asm_out_file);
27618 break;
27619 case ate_kind_label:
27620 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27621 "0x%x", entry->index);
27622 break;
27623 default:
27624 gcc_unreachable ();
27626 return 1;
27629 /* Produce the .debug_addr section. */
27631 static void
27632 output_addr_table (void)
27634 unsigned int index = 0;
27635 if (addr_index_table == NULL || addr_index_table->size () == 0)
27636 return;
27638 switch_to_section (debug_addr_section);
27639 addr_index_table
27640 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27643 #if ENABLE_ASSERT_CHECKING
27644 /* Verify that all marks are clear. */
27646 static void
27647 verify_marks_clear (dw_die_ref die)
27649 dw_die_ref c;
27651 gcc_assert (! die->die_mark);
27652 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27654 #endif /* ENABLE_ASSERT_CHECKING */
27656 /* Clear the marks for a die and its children.
27657 Be cool if the mark isn't set. */
27659 static void
27660 prune_unmark_dies (dw_die_ref die)
27662 dw_die_ref c;
27664 if (die->die_mark)
27665 die->die_mark = 0;
27666 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27669 /* Given LOC that is referenced by a DIE we're marking as used, find all
27670 referenced DWARF procedures it references and mark them as used. */
27672 static void
27673 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27675 for (; loc != NULL; loc = loc->dw_loc_next)
27676 switch (loc->dw_loc_opc)
27678 case DW_OP_implicit_pointer:
27679 case DW_OP_convert:
27680 case DW_OP_reinterpret:
27681 case DW_OP_GNU_implicit_pointer:
27682 case DW_OP_GNU_convert:
27683 case DW_OP_GNU_reinterpret:
27684 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27685 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27686 break;
27687 case DW_OP_GNU_variable_value:
27688 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27690 dw_die_ref ref
27691 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27692 if (ref == NULL)
27693 break;
27694 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27695 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27696 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27698 /* FALLTHRU */
27699 case DW_OP_call2:
27700 case DW_OP_call4:
27701 case DW_OP_call_ref:
27702 case DW_OP_const_type:
27703 case DW_OP_GNU_const_type:
27704 case DW_OP_GNU_parameter_ref:
27705 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27706 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27707 break;
27708 case DW_OP_regval_type:
27709 case DW_OP_deref_type:
27710 case DW_OP_GNU_regval_type:
27711 case DW_OP_GNU_deref_type:
27712 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27713 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27714 break;
27715 case DW_OP_entry_value:
27716 case DW_OP_GNU_entry_value:
27717 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27718 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27719 break;
27720 default:
27721 break;
27725 /* Given DIE that we're marking as used, find any other dies
27726 it references as attributes and mark them as used. */
27728 static void
27729 prune_unused_types_walk_attribs (dw_die_ref die)
27731 dw_attr_node *a;
27732 unsigned ix;
27734 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27736 switch (AT_class (a))
27738 /* Make sure DWARF procedures referenced by location descriptions will
27739 get emitted. */
27740 case dw_val_class_loc:
27741 prune_unused_types_walk_loc_descr (AT_loc (a));
27742 break;
27743 case dw_val_class_loc_list:
27744 for (dw_loc_list_ref list = AT_loc_list (a);
27745 list != NULL;
27746 list = list->dw_loc_next)
27747 prune_unused_types_walk_loc_descr (list->expr);
27748 break;
27750 case dw_val_class_die_ref:
27751 /* A reference to another DIE.
27752 Make sure that it will get emitted.
27753 If it was broken out into a comdat group, don't follow it. */
27754 if (! AT_ref (a)->comdat_type_p
27755 || a->dw_attr == DW_AT_specification)
27756 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27757 break;
27759 case dw_val_class_str:
27760 /* Set the string's refcount to 0 so that prune_unused_types_mark
27761 accounts properly for it. */
27762 a->dw_attr_val.v.val_str->refcount = 0;
27763 break;
27765 default:
27766 break;
27771 /* Mark the generic parameters and arguments children DIEs of DIE. */
27773 static void
27774 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27776 dw_die_ref c;
27778 if (die == NULL || die->die_child == NULL)
27779 return;
27780 c = die->die_child;
27783 if (is_template_parameter (c))
27784 prune_unused_types_mark (c, 1);
27785 c = c->die_sib;
27786 } while (c && c != die->die_child);
27789 /* Mark DIE as being used. If DOKIDS is true, then walk down
27790 to DIE's children. */
27792 static void
27793 prune_unused_types_mark (dw_die_ref die, int dokids)
27795 dw_die_ref c;
27797 if (die->die_mark == 0)
27799 /* We haven't done this node yet. Mark it as used. */
27800 die->die_mark = 1;
27801 /* If this is the DIE of a generic type instantiation,
27802 mark the children DIEs that describe its generic parms and
27803 args. */
27804 prune_unused_types_mark_generic_parms_dies (die);
27806 /* We also have to mark its parents as used.
27807 (But we don't want to mark our parent's kids due to this,
27808 unless it is a class.) */
27809 if (die->die_parent)
27810 prune_unused_types_mark (die->die_parent,
27811 class_scope_p (die->die_parent));
27813 /* Mark any referenced nodes. */
27814 prune_unused_types_walk_attribs (die);
27816 /* If this node is a specification,
27817 also mark the definition, if it exists. */
27818 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27819 prune_unused_types_mark (die->die_definition, 1);
27822 if (dokids && die->die_mark != 2)
27824 /* We need to walk the children, but haven't done so yet.
27825 Remember that we've walked the kids. */
27826 die->die_mark = 2;
27828 /* If this is an array type, we need to make sure our
27829 kids get marked, even if they're types. If we're
27830 breaking out types into comdat sections, do this
27831 for all type definitions. */
27832 if (die->die_tag == DW_TAG_array_type
27833 || (use_debug_types
27834 && is_type_die (die) && ! is_declaration_die (die)))
27835 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27836 else
27837 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27841 /* For local classes, look if any static member functions were emitted
27842 and if so, mark them. */
27844 static void
27845 prune_unused_types_walk_local_classes (dw_die_ref die)
27847 dw_die_ref c;
27849 if (die->die_mark == 2)
27850 return;
27852 switch (die->die_tag)
27854 case DW_TAG_structure_type:
27855 case DW_TAG_union_type:
27856 case DW_TAG_class_type:
27857 break;
27859 case DW_TAG_subprogram:
27860 if (!get_AT_flag (die, DW_AT_declaration)
27861 || die->die_definition != NULL)
27862 prune_unused_types_mark (die, 1);
27863 return;
27865 default:
27866 return;
27869 /* Mark children. */
27870 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27873 /* Walk the tree DIE and mark types that we actually use. */
27875 static void
27876 prune_unused_types_walk (dw_die_ref die)
27878 dw_die_ref c;
27880 /* Don't do anything if this node is already marked and
27881 children have been marked as well. */
27882 if (die->die_mark == 2)
27883 return;
27885 switch (die->die_tag)
27887 case DW_TAG_structure_type:
27888 case DW_TAG_union_type:
27889 case DW_TAG_class_type:
27890 if (die->die_perennial_p)
27891 break;
27893 for (c = die->die_parent; c; c = c->die_parent)
27894 if (c->die_tag == DW_TAG_subprogram)
27895 break;
27897 /* Finding used static member functions inside of classes
27898 is needed just for local classes, because for other classes
27899 static member function DIEs with DW_AT_specification
27900 are emitted outside of the DW_TAG_*_type. If we ever change
27901 it, we'd need to call this even for non-local classes. */
27902 if (c)
27903 prune_unused_types_walk_local_classes (die);
27905 /* It's a type node --- don't mark it. */
27906 return;
27908 case DW_TAG_const_type:
27909 case DW_TAG_packed_type:
27910 case DW_TAG_pointer_type:
27911 case DW_TAG_reference_type:
27912 case DW_TAG_rvalue_reference_type:
27913 case DW_TAG_volatile_type:
27914 case DW_TAG_typedef:
27915 case DW_TAG_array_type:
27916 case DW_TAG_interface_type:
27917 case DW_TAG_friend:
27918 case DW_TAG_enumeration_type:
27919 case DW_TAG_subroutine_type:
27920 case DW_TAG_string_type:
27921 case DW_TAG_set_type:
27922 case DW_TAG_subrange_type:
27923 case DW_TAG_ptr_to_member_type:
27924 case DW_TAG_file_type:
27925 /* Type nodes are useful only when other DIEs reference them --- don't
27926 mark them. */
27927 /* FALLTHROUGH */
27929 case DW_TAG_dwarf_procedure:
27930 /* Likewise for DWARF procedures. */
27932 if (die->die_perennial_p)
27933 break;
27935 return;
27937 default:
27938 /* Mark everything else. */
27939 break;
27942 if (die->die_mark == 0)
27944 die->die_mark = 1;
27946 /* Now, mark any dies referenced from here. */
27947 prune_unused_types_walk_attribs (die);
27950 die->die_mark = 2;
27952 /* Mark children. */
27953 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27956 /* Increment the string counts on strings referred to from DIE's
27957 attributes. */
27959 static void
27960 prune_unused_types_update_strings (dw_die_ref die)
27962 dw_attr_node *a;
27963 unsigned ix;
27965 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27966 if (AT_class (a) == dw_val_class_str)
27968 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27969 s->refcount++;
27970 /* Avoid unnecessarily putting strings that are used less than
27971 twice in the hash table. */
27972 if (s->refcount
27973 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27975 indirect_string_node **slot
27976 = debug_str_hash->find_slot_with_hash (s->str,
27977 htab_hash_string (s->str),
27978 INSERT);
27979 gcc_assert (*slot == NULL);
27980 *slot = s;
27985 /* Mark DIE and its children as removed. */
27987 static void
27988 mark_removed (dw_die_ref die)
27990 dw_die_ref c;
27991 die->removed = true;
27992 FOR_EACH_CHILD (die, c, mark_removed (c));
27995 /* Remove from the tree DIE any dies that aren't marked. */
27997 static void
27998 prune_unused_types_prune (dw_die_ref die)
28000 dw_die_ref c;
28002 gcc_assert (die->die_mark);
28003 prune_unused_types_update_strings (die);
28005 if (! die->die_child)
28006 return;
28008 c = die->die_child;
28009 do {
28010 dw_die_ref prev = c, next;
28011 for (c = c->die_sib; ! c->die_mark; c = next)
28012 if (c == die->die_child)
28014 /* No marked children between 'prev' and the end of the list. */
28015 if (prev == c)
28016 /* No marked children at all. */
28017 die->die_child = NULL;
28018 else
28020 prev->die_sib = c->die_sib;
28021 die->die_child = prev;
28023 c->die_sib = NULL;
28024 mark_removed (c);
28025 return;
28027 else
28029 next = c->die_sib;
28030 c->die_sib = NULL;
28031 mark_removed (c);
28034 if (c != prev->die_sib)
28035 prev->die_sib = c;
28036 prune_unused_types_prune (c);
28037 } while (c != die->die_child);
28040 /* Remove dies representing declarations that we never use. */
28042 static void
28043 prune_unused_types (void)
28045 unsigned int i;
28046 limbo_die_node *node;
28047 comdat_type_node *ctnode;
28048 pubname_entry *pub;
28049 dw_die_ref base_type;
28051 #if ENABLE_ASSERT_CHECKING
28052 /* All the marks should already be clear. */
28053 verify_marks_clear (comp_unit_die ());
28054 for (node = limbo_die_list; node; node = node->next)
28055 verify_marks_clear (node->die);
28056 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28057 verify_marks_clear (ctnode->root_die);
28058 #endif /* ENABLE_ASSERT_CHECKING */
28060 /* Mark types that are used in global variables. */
28061 premark_types_used_by_global_vars ();
28063 /* Set the mark on nodes that are actually used. */
28064 prune_unused_types_walk (comp_unit_die ());
28065 for (node = limbo_die_list; node; node = node->next)
28066 prune_unused_types_walk (node->die);
28067 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28069 prune_unused_types_walk (ctnode->root_die);
28070 prune_unused_types_mark (ctnode->type_die, 1);
28073 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28074 are unusual in that they are pubnames that are the children of pubtypes.
28075 They should only be marked via their parent DW_TAG_enumeration_type die,
28076 not as roots in themselves. */
28077 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28078 if (pub->die->die_tag != DW_TAG_enumerator)
28079 prune_unused_types_mark (pub->die, 1);
28080 for (i = 0; base_types.iterate (i, &base_type); i++)
28081 prune_unused_types_mark (base_type, 1);
28083 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28084 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28085 callees). */
28086 cgraph_node *cnode;
28087 FOR_EACH_FUNCTION (cnode)
28088 if (cnode->referred_to_p (false))
28090 dw_die_ref die = lookup_decl_die (cnode->decl);
28091 if (die == NULL || die->die_mark)
28092 continue;
28093 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28094 if (e->caller != cnode
28095 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28097 prune_unused_types_mark (die, 1);
28098 break;
28102 if (debug_str_hash)
28103 debug_str_hash->empty ();
28104 if (skeleton_debug_str_hash)
28105 skeleton_debug_str_hash->empty ();
28106 prune_unused_types_prune (comp_unit_die ());
28107 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28109 node = *pnode;
28110 if (!node->die->die_mark)
28111 *pnode = node->next;
28112 else
28114 prune_unused_types_prune (node->die);
28115 pnode = &node->next;
28118 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28119 prune_unused_types_prune (ctnode->root_die);
28121 /* Leave the marks clear. */
28122 prune_unmark_dies (comp_unit_die ());
28123 for (node = limbo_die_list; node; node = node->next)
28124 prune_unmark_dies (node->die);
28125 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28126 prune_unmark_dies (ctnode->root_die);
28129 /* Helpers to manipulate hash table of comdat type units. */
28131 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28133 static inline hashval_t hash (const comdat_type_node *);
28134 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28137 inline hashval_t
28138 comdat_type_hasher::hash (const comdat_type_node *type_node)
28140 hashval_t h;
28141 memcpy (&h, type_node->signature, sizeof (h));
28142 return h;
28145 inline bool
28146 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28147 const comdat_type_node *type_node_2)
28149 return (! memcmp (type_node_1->signature, type_node_2->signature,
28150 DWARF_TYPE_SIGNATURE_SIZE));
28153 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28154 to the location it would have been added, should we know its
28155 DECL_ASSEMBLER_NAME when we added other attributes. This will
28156 probably improve compactness of debug info, removing equivalent
28157 abbrevs, and hide any differences caused by deferring the
28158 computation of the assembler name, triggered by e.g. PCH. */
28160 static inline void
28161 move_linkage_attr (dw_die_ref die)
28163 unsigned ix = vec_safe_length (die->die_attr);
28164 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28166 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28167 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28169 while (--ix > 0)
28171 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28173 if (prev->dw_attr == DW_AT_decl_line
28174 || prev->dw_attr == DW_AT_decl_column
28175 || prev->dw_attr == DW_AT_name)
28176 break;
28179 if (ix != vec_safe_length (die->die_attr) - 1)
28181 die->die_attr->pop ();
28182 die->die_attr->quick_insert (ix, linkage);
28186 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28187 referenced from typed stack ops and count how often they are used. */
28189 static void
28190 mark_base_types (dw_loc_descr_ref loc)
28192 dw_die_ref base_type = NULL;
28194 for (; loc; loc = loc->dw_loc_next)
28196 switch (loc->dw_loc_opc)
28198 case DW_OP_regval_type:
28199 case DW_OP_deref_type:
28200 case DW_OP_GNU_regval_type:
28201 case DW_OP_GNU_deref_type:
28202 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28203 break;
28204 case DW_OP_convert:
28205 case DW_OP_reinterpret:
28206 case DW_OP_GNU_convert:
28207 case DW_OP_GNU_reinterpret:
28208 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28209 continue;
28210 /* FALLTHRU */
28211 case DW_OP_const_type:
28212 case DW_OP_GNU_const_type:
28213 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28214 break;
28215 case DW_OP_entry_value:
28216 case DW_OP_GNU_entry_value:
28217 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28218 continue;
28219 default:
28220 continue;
28222 gcc_assert (base_type->die_parent == comp_unit_die ());
28223 if (base_type->die_mark)
28224 base_type->die_mark++;
28225 else
28227 base_types.safe_push (base_type);
28228 base_type->die_mark = 1;
28233 /* Comparison function for sorting marked base types. */
28235 static int
28236 base_type_cmp (const void *x, const void *y)
28238 dw_die_ref dx = *(const dw_die_ref *) x;
28239 dw_die_ref dy = *(const dw_die_ref *) y;
28240 unsigned int byte_size1, byte_size2;
28241 unsigned int encoding1, encoding2;
28242 unsigned int align1, align2;
28243 if (dx->die_mark > dy->die_mark)
28244 return -1;
28245 if (dx->die_mark < dy->die_mark)
28246 return 1;
28247 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28248 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28249 if (byte_size1 < byte_size2)
28250 return 1;
28251 if (byte_size1 > byte_size2)
28252 return -1;
28253 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28254 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28255 if (encoding1 < encoding2)
28256 return 1;
28257 if (encoding1 > encoding2)
28258 return -1;
28259 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28260 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28261 if (align1 < align2)
28262 return 1;
28263 if (align1 > align2)
28264 return -1;
28265 return 0;
28268 /* Move base types marked by mark_base_types as early as possible
28269 in the CU, sorted by decreasing usage count both to make the
28270 uleb128 references as small as possible and to make sure they
28271 will have die_offset already computed by calc_die_sizes when
28272 sizes of typed stack loc ops is computed. */
28274 static void
28275 move_marked_base_types (void)
28277 unsigned int i;
28278 dw_die_ref base_type, die, c;
28280 if (base_types.is_empty ())
28281 return;
28283 /* Sort by decreasing usage count, they will be added again in that
28284 order later on. */
28285 base_types.qsort (base_type_cmp);
28286 die = comp_unit_die ();
28287 c = die->die_child;
28290 dw_die_ref prev = c;
28291 c = c->die_sib;
28292 while (c->die_mark)
28294 remove_child_with_prev (c, prev);
28295 /* As base types got marked, there must be at least
28296 one node other than DW_TAG_base_type. */
28297 gcc_assert (die->die_child != NULL);
28298 c = prev->die_sib;
28301 while (c != die->die_child);
28302 gcc_assert (die->die_child);
28303 c = die->die_child;
28304 for (i = 0; base_types.iterate (i, &base_type); i++)
28306 base_type->die_mark = 0;
28307 base_type->die_sib = c->die_sib;
28308 c->die_sib = base_type;
28309 c = base_type;
28313 /* Helper function for resolve_addr, attempt to resolve
28314 one CONST_STRING, return true if successful. Similarly verify that
28315 SYMBOL_REFs refer to variables emitted in the current CU. */
28317 static bool
28318 resolve_one_addr (rtx *addr)
28320 rtx rtl = *addr;
28322 if (GET_CODE (rtl) == CONST_STRING)
28324 size_t len = strlen (XSTR (rtl, 0)) + 1;
28325 tree t = build_string (len, XSTR (rtl, 0));
28326 tree tlen = size_int (len - 1);
28327 TREE_TYPE (t)
28328 = build_array_type (char_type_node, build_index_type (tlen));
28329 rtl = lookup_constant_def (t);
28330 if (!rtl || !MEM_P (rtl))
28331 return false;
28332 rtl = XEXP (rtl, 0);
28333 if (GET_CODE (rtl) == SYMBOL_REF
28334 && SYMBOL_REF_DECL (rtl)
28335 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28336 return false;
28337 vec_safe_push (used_rtx_array, rtl);
28338 *addr = rtl;
28339 return true;
28342 if (GET_CODE (rtl) == SYMBOL_REF
28343 && SYMBOL_REF_DECL (rtl))
28345 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28347 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28348 return false;
28350 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28351 return false;
28354 if (GET_CODE (rtl) == CONST)
28356 subrtx_ptr_iterator::array_type array;
28357 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28358 if (!resolve_one_addr (*iter))
28359 return false;
28362 return true;
28365 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28366 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28367 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28369 static rtx
28370 string_cst_pool_decl (tree t)
28372 rtx rtl = output_constant_def (t, 1);
28373 unsigned char *array;
28374 dw_loc_descr_ref l;
28375 tree decl;
28376 size_t len;
28377 dw_die_ref ref;
28379 if (!rtl || !MEM_P (rtl))
28380 return NULL_RTX;
28381 rtl = XEXP (rtl, 0);
28382 if (GET_CODE (rtl) != SYMBOL_REF
28383 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28384 return NULL_RTX;
28386 decl = SYMBOL_REF_DECL (rtl);
28387 if (!lookup_decl_die (decl))
28389 len = TREE_STRING_LENGTH (t);
28390 vec_safe_push (used_rtx_array, rtl);
28391 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28392 array = ggc_vec_alloc<unsigned char> (len);
28393 memcpy (array, TREE_STRING_POINTER (t), len);
28394 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28395 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28396 l->dw_loc_oprnd2.v.val_vec.length = len;
28397 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28398 l->dw_loc_oprnd2.v.val_vec.array = array;
28399 add_AT_loc (ref, DW_AT_location, l);
28400 equate_decl_number_to_die (decl, ref);
28402 return rtl;
28405 /* Helper function of resolve_addr_in_expr. LOC is
28406 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28407 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28408 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28409 with DW_OP_implicit_pointer if possible
28410 and return true, if unsuccessful, return false. */
28412 static bool
28413 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28415 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28416 HOST_WIDE_INT offset = 0;
28417 dw_die_ref ref = NULL;
28418 tree decl;
28420 if (GET_CODE (rtl) == CONST
28421 && GET_CODE (XEXP (rtl, 0)) == PLUS
28422 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28424 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28425 rtl = XEXP (XEXP (rtl, 0), 0);
28427 if (GET_CODE (rtl) == CONST_STRING)
28429 size_t len = strlen (XSTR (rtl, 0)) + 1;
28430 tree t = build_string (len, XSTR (rtl, 0));
28431 tree tlen = size_int (len - 1);
28433 TREE_TYPE (t)
28434 = build_array_type (char_type_node, build_index_type (tlen));
28435 rtl = string_cst_pool_decl (t);
28436 if (!rtl)
28437 return false;
28439 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28441 decl = SYMBOL_REF_DECL (rtl);
28442 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28444 ref = lookup_decl_die (decl);
28445 if (ref && (get_AT (ref, DW_AT_location)
28446 || get_AT (ref, DW_AT_const_value)))
28448 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28449 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28450 loc->dw_loc_oprnd1.val_entry = NULL;
28451 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28452 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28453 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28454 loc->dw_loc_oprnd2.v.val_int = offset;
28455 return true;
28459 return false;
28462 /* Helper function for resolve_addr, handle one location
28463 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28464 the location list couldn't be resolved. */
28466 static bool
28467 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28469 dw_loc_descr_ref keep = NULL;
28470 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28471 switch (loc->dw_loc_opc)
28473 case DW_OP_addr:
28474 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28476 if ((prev == NULL
28477 || prev->dw_loc_opc == DW_OP_piece
28478 || prev->dw_loc_opc == DW_OP_bit_piece)
28479 && loc->dw_loc_next
28480 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28481 && (!dwarf_strict || dwarf_version >= 5)
28482 && optimize_one_addr_into_implicit_ptr (loc))
28483 break;
28484 return false;
28486 break;
28487 case DW_OP_GNU_addr_index:
28488 case DW_OP_GNU_const_index:
28489 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28490 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28492 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28493 if (!resolve_one_addr (&rtl))
28494 return false;
28495 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28496 loc->dw_loc_oprnd1.val_entry
28497 = add_addr_table_entry (rtl, ate_kind_rtx);
28499 break;
28500 case DW_OP_const4u:
28501 case DW_OP_const8u:
28502 if (loc->dtprel
28503 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28504 return false;
28505 break;
28506 case DW_OP_plus_uconst:
28507 if (size_of_loc_descr (loc)
28508 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28510 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28512 dw_loc_descr_ref repl
28513 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28514 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28515 add_loc_descr (&repl, loc->dw_loc_next);
28516 *loc = *repl;
28518 break;
28519 case DW_OP_implicit_value:
28520 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28521 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28522 return false;
28523 break;
28524 case DW_OP_implicit_pointer:
28525 case DW_OP_GNU_implicit_pointer:
28526 case DW_OP_GNU_parameter_ref:
28527 case DW_OP_GNU_variable_value:
28528 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28530 dw_die_ref ref
28531 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28532 if (ref == NULL)
28533 return false;
28534 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28535 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28536 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28538 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28540 if (prev == NULL
28541 && loc->dw_loc_next == NULL
28542 && AT_class (a) == dw_val_class_loc)
28543 switch (a->dw_attr)
28545 /* Following attributes allow both exprloc and reference,
28546 so if the whole expression is DW_OP_GNU_variable_value
28547 alone we could transform it into reference. */
28548 case DW_AT_byte_size:
28549 case DW_AT_bit_size:
28550 case DW_AT_lower_bound:
28551 case DW_AT_upper_bound:
28552 case DW_AT_bit_stride:
28553 case DW_AT_count:
28554 case DW_AT_allocated:
28555 case DW_AT_associated:
28556 case DW_AT_byte_stride:
28557 a->dw_attr_val.val_class = dw_val_class_die_ref;
28558 a->dw_attr_val.val_entry = NULL;
28559 a->dw_attr_val.v.val_die_ref.die
28560 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28561 a->dw_attr_val.v.val_die_ref.external = 0;
28562 return true;
28563 default:
28564 break;
28566 if (dwarf_strict)
28567 return false;
28569 break;
28570 case DW_OP_const_type:
28571 case DW_OP_regval_type:
28572 case DW_OP_deref_type:
28573 case DW_OP_convert:
28574 case DW_OP_reinterpret:
28575 case DW_OP_GNU_const_type:
28576 case DW_OP_GNU_regval_type:
28577 case DW_OP_GNU_deref_type:
28578 case DW_OP_GNU_convert:
28579 case DW_OP_GNU_reinterpret:
28580 while (loc->dw_loc_next
28581 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28582 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28584 dw_die_ref base1, base2;
28585 unsigned enc1, enc2, size1, size2;
28586 if (loc->dw_loc_opc == DW_OP_regval_type
28587 || loc->dw_loc_opc == DW_OP_deref_type
28588 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28589 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28590 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28591 else if (loc->dw_loc_oprnd1.val_class
28592 == dw_val_class_unsigned_const)
28593 break;
28594 else
28595 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28596 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28597 == dw_val_class_unsigned_const)
28598 break;
28599 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28600 gcc_assert (base1->die_tag == DW_TAG_base_type
28601 && base2->die_tag == DW_TAG_base_type);
28602 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28603 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28604 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28605 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28606 if (size1 == size2
28607 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28608 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28609 && loc != keep)
28610 || enc1 == enc2))
28612 /* Optimize away next DW_OP_convert after
28613 adjusting LOC's base type die reference. */
28614 if (loc->dw_loc_opc == DW_OP_regval_type
28615 || loc->dw_loc_opc == DW_OP_deref_type
28616 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28617 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28618 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28619 else
28620 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28621 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28622 continue;
28624 /* Don't change integer DW_OP_convert after e.g. floating
28625 point typed stack entry. */
28626 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28627 keep = loc->dw_loc_next;
28628 break;
28630 break;
28631 default:
28632 break;
28634 return true;
28637 /* Helper function of resolve_addr. DIE had DW_AT_location of
28638 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28639 and DW_OP_addr couldn't be resolved. resolve_addr has already
28640 removed the DW_AT_location attribute. This function attempts to
28641 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28642 to it or DW_AT_const_value attribute, if possible. */
28644 static void
28645 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28647 if (!VAR_P (decl)
28648 || lookup_decl_die (decl) != die
28649 || DECL_EXTERNAL (decl)
28650 || !TREE_STATIC (decl)
28651 || DECL_INITIAL (decl) == NULL_TREE
28652 || DECL_P (DECL_INITIAL (decl))
28653 || get_AT (die, DW_AT_const_value))
28654 return;
28656 tree init = DECL_INITIAL (decl);
28657 HOST_WIDE_INT offset = 0;
28658 /* For variables that have been optimized away and thus
28659 don't have a memory location, see if we can emit
28660 DW_AT_const_value instead. */
28661 if (tree_add_const_value_attribute (die, init))
28662 return;
28663 if (dwarf_strict && dwarf_version < 5)
28664 return;
28665 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28666 and ADDR_EXPR refers to a decl that has DW_AT_location or
28667 DW_AT_const_value (but isn't addressable, otherwise
28668 resolving the original DW_OP_addr wouldn't fail), see if
28669 we can add DW_OP_implicit_pointer. */
28670 STRIP_NOPS (init);
28671 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28672 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28674 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28675 init = TREE_OPERAND (init, 0);
28676 STRIP_NOPS (init);
28678 if (TREE_CODE (init) != ADDR_EXPR)
28679 return;
28680 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28681 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28682 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28683 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28684 && TREE_OPERAND (init, 0) != decl))
28686 dw_die_ref ref;
28687 dw_loc_descr_ref l;
28689 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28691 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28692 if (!rtl)
28693 return;
28694 decl = SYMBOL_REF_DECL (rtl);
28696 else
28697 decl = TREE_OPERAND (init, 0);
28698 ref = lookup_decl_die (decl);
28699 if (ref == NULL
28700 || (!get_AT (ref, DW_AT_location)
28701 && !get_AT (ref, DW_AT_const_value)))
28702 return;
28703 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28704 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28705 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28706 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28707 add_AT_loc (die, DW_AT_location, l);
28711 /* Return NULL if l is a DWARF expression, or first op that is not
28712 valid DWARF expression. */
28714 static dw_loc_descr_ref
28715 non_dwarf_expression (dw_loc_descr_ref l)
28717 while (l)
28719 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28720 return l;
28721 switch (l->dw_loc_opc)
28723 case DW_OP_regx:
28724 case DW_OP_implicit_value:
28725 case DW_OP_stack_value:
28726 case DW_OP_implicit_pointer:
28727 case DW_OP_GNU_implicit_pointer:
28728 case DW_OP_GNU_parameter_ref:
28729 case DW_OP_piece:
28730 case DW_OP_bit_piece:
28731 return l;
28732 default:
28733 break;
28735 l = l->dw_loc_next;
28737 return NULL;
28740 /* Return adjusted copy of EXPR:
28741 If it is empty DWARF expression, return it.
28742 If it is valid non-empty DWARF expression,
28743 return copy of EXPR with DW_OP_deref appended to it.
28744 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28745 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28746 If it is DWARF expression followed by DW_OP_stack_value, return
28747 copy of the DWARF expression without anything appended.
28748 Otherwise, return NULL. */
28750 static dw_loc_descr_ref
28751 copy_deref_exprloc (dw_loc_descr_ref expr)
28753 dw_loc_descr_ref tail = NULL;
28755 if (expr == NULL)
28756 return NULL;
28758 dw_loc_descr_ref l = non_dwarf_expression (expr);
28759 if (l && l->dw_loc_next)
28760 return NULL;
28762 if (l)
28764 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28765 tail = new_loc_descr ((enum dwarf_location_atom)
28766 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28767 0, 0);
28768 else
28769 switch (l->dw_loc_opc)
28771 case DW_OP_regx:
28772 tail = new_loc_descr (DW_OP_bregx,
28773 l->dw_loc_oprnd1.v.val_unsigned, 0);
28774 break;
28775 case DW_OP_stack_value:
28776 break;
28777 default:
28778 return NULL;
28781 else
28782 tail = new_loc_descr (DW_OP_deref, 0, 0);
28784 dw_loc_descr_ref ret = NULL, *p = &ret;
28785 while (expr != l)
28787 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28788 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28789 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28790 p = &(*p)->dw_loc_next;
28791 expr = expr->dw_loc_next;
28793 *p = tail;
28794 return ret;
28797 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28798 reference to a variable or argument, adjust it if needed and return:
28799 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28800 attribute if present should be removed
28801 0 keep the attribute perhaps with minor modifications, no need to rescan
28802 1 if the attribute has been successfully adjusted. */
28804 static int
28805 optimize_string_length (dw_attr_node *a)
28807 dw_loc_descr_ref l = AT_loc (a), lv;
28808 dw_die_ref die;
28809 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28811 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28812 die = lookup_decl_die (decl);
28813 if (die)
28815 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28816 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28817 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28819 else
28820 return -1;
28822 else
28823 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28825 /* DWARF5 allows reference class, so we can then reference the DIE.
28826 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28827 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28829 a->dw_attr_val.val_class = dw_val_class_die_ref;
28830 a->dw_attr_val.val_entry = NULL;
28831 a->dw_attr_val.v.val_die_ref.die = die;
28832 a->dw_attr_val.v.val_die_ref.external = 0;
28833 return 0;
28836 dw_attr_node *av = get_AT (die, DW_AT_location);
28837 dw_loc_list_ref d;
28838 bool non_dwarf_expr = false;
28840 if (av == NULL)
28841 return dwarf_strict ? -1 : 0;
28842 switch (AT_class (av))
28844 case dw_val_class_loc_list:
28845 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28846 if (d->expr && non_dwarf_expression (d->expr))
28847 non_dwarf_expr = true;
28848 break;
28849 case dw_val_class_loc:
28850 lv = AT_loc (av);
28851 if (lv == NULL)
28852 return dwarf_strict ? -1 : 0;
28853 if (non_dwarf_expression (lv))
28854 non_dwarf_expr = true;
28855 break;
28856 default:
28857 return dwarf_strict ? -1 : 0;
28860 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28861 into DW_OP_call4 or DW_OP_GNU_variable_value into
28862 DW_OP_call4 DW_OP_deref, do so. */
28863 if (!non_dwarf_expr
28864 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28866 l->dw_loc_opc = DW_OP_call4;
28867 if (l->dw_loc_next)
28868 l->dw_loc_next = NULL;
28869 else
28870 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28871 return 0;
28874 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28875 copy over the DW_AT_location attribute from die to a. */
28876 if (l->dw_loc_next != NULL)
28878 a->dw_attr_val = av->dw_attr_val;
28879 return 1;
28882 dw_loc_list_ref list, *p;
28883 switch (AT_class (av))
28885 case dw_val_class_loc_list:
28886 p = &list;
28887 list = NULL;
28888 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28890 lv = copy_deref_exprloc (d->expr);
28891 if (lv)
28893 *p = new_loc_list (lv, d->begin, d->end, d->section);
28894 p = &(*p)->dw_loc_next;
28896 else if (!dwarf_strict && d->expr)
28897 return 0;
28899 if (list == NULL)
28900 return dwarf_strict ? -1 : 0;
28901 a->dw_attr_val.val_class = dw_val_class_loc_list;
28902 gen_llsym (list);
28903 *AT_loc_list_ptr (a) = list;
28904 return 1;
28905 case dw_val_class_loc:
28906 lv = copy_deref_exprloc (AT_loc (av));
28907 if (lv == NULL)
28908 return dwarf_strict ? -1 : 0;
28909 a->dw_attr_val.v.val_loc = lv;
28910 return 1;
28911 default:
28912 gcc_unreachable ();
28916 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28917 an address in .rodata section if the string literal is emitted there,
28918 or remove the containing location list or replace DW_AT_const_value
28919 with DW_AT_location and empty location expression, if it isn't found
28920 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28921 to something that has been emitted in the current CU. */
28923 static void
28924 resolve_addr (dw_die_ref die)
28926 dw_die_ref c;
28927 dw_attr_node *a;
28928 dw_loc_list_ref *curr, *start, loc;
28929 unsigned ix;
28930 bool remove_AT_byte_size = false;
28932 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28933 switch (AT_class (a))
28935 case dw_val_class_loc_list:
28936 start = curr = AT_loc_list_ptr (a);
28937 loc = *curr;
28938 gcc_assert (loc);
28939 /* The same list can be referenced more than once. See if we have
28940 already recorded the result from a previous pass. */
28941 if (loc->replaced)
28942 *curr = loc->dw_loc_next;
28943 else if (!loc->resolved_addr)
28945 /* As things stand, we do not expect or allow one die to
28946 reference a suffix of another die's location list chain.
28947 References must be identical or completely separate.
28948 There is therefore no need to cache the result of this
28949 pass on any list other than the first; doing so
28950 would lead to unnecessary writes. */
28951 while (*curr)
28953 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28954 if (!resolve_addr_in_expr (a, (*curr)->expr))
28956 dw_loc_list_ref next = (*curr)->dw_loc_next;
28957 dw_loc_descr_ref l = (*curr)->expr;
28959 if (next && (*curr)->ll_symbol)
28961 gcc_assert (!next->ll_symbol);
28962 next->ll_symbol = (*curr)->ll_symbol;
28964 if (dwarf_split_debug_info)
28965 remove_loc_list_addr_table_entries (l);
28966 *curr = next;
28968 else
28970 mark_base_types ((*curr)->expr);
28971 curr = &(*curr)->dw_loc_next;
28974 if (loc == *start)
28975 loc->resolved_addr = 1;
28976 else
28978 loc->replaced = 1;
28979 loc->dw_loc_next = *start;
28982 if (!*start)
28984 remove_AT (die, a->dw_attr);
28985 ix--;
28987 break;
28988 case dw_val_class_loc:
28990 dw_loc_descr_ref l = AT_loc (a);
28991 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28992 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28993 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28994 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28995 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28996 with DW_FORM_ref referencing the same DIE as
28997 DW_OP_GNU_variable_value used to reference. */
28998 if (a->dw_attr == DW_AT_string_length
28999 && l
29000 && l->dw_loc_opc == DW_OP_GNU_variable_value
29001 && (l->dw_loc_next == NULL
29002 || (l->dw_loc_next->dw_loc_next == NULL
29003 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29005 switch (optimize_string_length (a))
29007 case -1:
29008 remove_AT (die, a->dw_attr);
29009 ix--;
29010 /* If we drop DW_AT_string_length, we need to drop also
29011 DW_AT_{string_length_,}byte_size. */
29012 remove_AT_byte_size = true;
29013 continue;
29014 default:
29015 break;
29016 case 1:
29017 /* Even if we keep the optimized DW_AT_string_length,
29018 it might have changed AT_class, so process it again. */
29019 ix--;
29020 continue;
29023 /* For -gdwarf-2 don't attempt to optimize
29024 DW_AT_data_member_location containing
29025 DW_OP_plus_uconst - older consumers might
29026 rely on it being that op instead of a more complex,
29027 but shorter, location description. */
29028 if ((dwarf_version > 2
29029 || a->dw_attr != DW_AT_data_member_location
29030 || l == NULL
29031 || l->dw_loc_opc != DW_OP_plus_uconst
29032 || l->dw_loc_next != NULL)
29033 && !resolve_addr_in_expr (a, l))
29035 if (dwarf_split_debug_info)
29036 remove_loc_list_addr_table_entries (l);
29037 if (l != NULL
29038 && l->dw_loc_next == NULL
29039 && l->dw_loc_opc == DW_OP_addr
29040 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29041 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29042 && a->dw_attr == DW_AT_location)
29044 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29045 remove_AT (die, a->dw_attr);
29046 ix--;
29047 optimize_location_into_implicit_ptr (die, decl);
29048 break;
29050 if (a->dw_attr == DW_AT_string_length)
29051 /* If we drop DW_AT_string_length, we need to drop also
29052 DW_AT_{string_length_,}byte_size. */
29053 remove_AT_byte_size = true;
29054 remove_AT (die, a->dw_attr);
29055 ix--;
29057 else
29058 mark_base_types (l);
29060 break;
29061 case dw_val_class_addr:
29062 if (a->dw_attr == DW_AT_const_value
29063 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29065 if (AT_index (a) != NOT_INDEXED)
29066 remove_addr_table_entry (a->dw_attr_val.val_entry);
29067 remove_AT (die, a->dw_attr);
29068 ix--;
29070 if ((die->die_tag == DW_TAG_call_site
29071 && a->dw_attr == DW_AT_call_origin)
29072 || (die->die_tag == DW_TAG_GNU_call_site
29073 && a->dw_attr == DW_AT_abstract_origin))
29075 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29076 dw_die_ref tdie = lookup_decl_die (tdecl);
29077 dw_die_ref cdie;
29078 if (tdie == NULL
29079 && DECL_EXTERNAL (tdecl)
29080 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29081 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29083 dw_die_ref pdie = cdie;
29084 /* Make sure we don't add these DIEs into type units.
29085 We could emit skeleton DIEs for context (namespaces,
29086 outer structs/classes) and a skeleton DIE for the
29087 innermost context with DW_AT_signature pointing to the
29088 type unit. See PR78835. */
29089 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29090 pdie = pdie->die_parent;
29091 if (pdie == NULL)
29093 /* Creating a full DIE for tdecl is overly expensive and
29094 at this point even wrong when in the LTO phase
29095 as it can end up generating new type DIEs we didn't
29096 output and thus optimize_external_refs will crash. */
29097 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29098 add_AT_flag (tdie, DW_AT_external, 1);
29099 add_AT_flag (tdie, DW_AT_declaration, 1);
29100 add_linkage_attr (tdie, tdecl);
29101 add_name_and_src_coords_attributes (tdie, tdecl, true);
29102 equate_decl_number_to_die (tdecl, tdie);
29105 if (tdie)
29107 a->dw_attr_val.val_class = dw_val_class_die_ref;
29108 a->dw_attr_val.v.val_die_ref.die = tdie;
29109 a->dw_attr_val.v.val_die_ref.external = 0;
29111 else
29113 if (AT_index (a) != NOT_INDEXED)
29114 remove_addr_table_entry (a->dw_attr_val.val_entry);
29115 remove_AT (die, a->dw_attr);
29116 ix--;
29119 break;
29120 default:
29121 break;
29124 if (remove_AT_byte_size)
29125 remove_AT (die, dwarf_version >= 5
29126 ? DW_AT_string_length_byte_size
29127 : DW_AT_byte_size);
29129 FOR_EACH_CHILD (die, c, resolve_addr (c));
29132 /* Helper routines for optimize_location_lists.
29133 This pass tries to share identical local lists in .debug_loc
29134 section. */
29136 /* Iteratively hash operands of LOC opcode into HSTATE. */
29138 static void
29139 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29141 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29142 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29144 switch (loc->dw_loc_opc)
29146 case DW_OP_const4u:
29147 case DW_OP_const8u:
29148 if (loc->dtprel)
29149 goto hash_addr;
29150 /* FALLTHRU */
29151 case DW_OP_const1u:
29152 case DW_OP_const1s:
29153 case DW_OP_const2u:
29154 case DW_OP_const2s:
29155 case DW_OP_const4s:
29156 case DW_OP_const8s:
29157 case DW_OP_constu:
29158 case DW_OP_consts:
29159 case DW_OP_pick:
29160 case DW_OP_plus_uconst:
29161 case DW_OP_breg0:
29162 case DW_OP_breg1:
29163 case DW_OP_breg2:
29164 case DW_OP_breg3:
29165 case DW_OP_breg4:
29166 case DW_OP_breg5:
29167 case DW_OP_breg6:
29168 case DW_OP_breg7:
29169 case DW_OP_breg8:
29170 case DW_OP_breg9:
29171 case DW_OP_breg10:
29172 case DW_OP_breg11:
29173 case DW_OP_breg12:
29174 case DW_OP_breg13:
29175 case DW_OP_breg14:
29176 case DW_OP_breg15:
29177 case DW_OP_breg16:
29178 case DW_OP_breg17:
29179 case DW_OP_breg18:
29180 case DW_OP_breg19:
29181 case DW_OP_breg20:
29182 case DW_OP_breg21:
29183 case DW_OP_breg22:
29184 case DW_OP_breg23:
29185 case DW_OP_breg24:
29186 case DW_OP_breg25:
29187 case DW_OP_breg26:
29188 case DW_OP_breg27:
29189 case DW_OP_breg28:
29190 case DW_OP_breg29:
29191 case DW_OP_breg30:
29192 case DW_OP_breg31:
29193 case DW_OP_regx:
29194 case DW_OP_fbreg:
29195 case DW_OP_piece:
29196 case DW_OP_deref_size:
29197 case DW_OP_xderef_size:
29198 hstate.add_object (val1->v.val_int);
29199 break;
29200 case DW_OP_skip:
29201 case DW_OP_bra:
29203 int offset;
29205 gcc_assert (val1->val_class == dw_val_class_loc);
29206 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29207 hstate.add_object (offset);
29209 break;
29210 case DW_OP_implicit_value:
29211 hstate.add_object (val1->v.val_unsigned);
29212 switch (val2->val_class)
29214 case dw_val_class_const:
29215 hstate.add_object (val2->v.val_int);
29216 break;
29217 case dw_val_class_vec:
29219 unsigned int elt_size = val2->v.val_vec.elt_size;
29220 unsigned int len = val2->v.val_vec.length;
29222 hstate.add_int (elt_size);
29223 hstate.add_int (len);
29224 hstate.add (val2->v.val_vec.array, len * elt_size);
29226 break;
29227 case dw_val_class_const_double:
29228 hstate.add_object (val2->v.val_double.low);
29229 hstate.add_object (val2->v.val_double.high);
29230 break;
29231 case dw_val_class_wide_int:
29232 hstate.add (val2->v.val_wide->get_val (),
29233 get_full_len (*val2->v.val_wide)
29234 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29235 break;
29236 case dw_val_class_addr:
29237 inchash::add_rtx (val2->v.val_addr, hstate);
29238 break;
29239 default:
29240 gcc_unreachable ();
29242 break;
29243 case DW_OP_bregx:
29244 case DW_OP_bit_piece:
29245 hstate.add_object (val1->v.val_int);
29246 hstate.add_object (val2->v.val_int);
29247 break;
29248 case DW_OP_addr:
29249 hash_addr:
29250 if (loc->dtprel)
29252 unsigned char dtprel = 0xd1;
29253 hstate.add_object (dtprel);
29255 inchash::add_rtx (val1->v.val_addr, hstate);
29256 break;
29257 case DW_OP_GNU_addr_index:
29258 case DW_OP_GNU_const_index:
29260 if (loc->dtprel)
29262 unsigned char dtprel = 0xd1;
29263 hstate.add_object (dtprel);
29265 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29267 break;
29268 case DW_OP_implicit_pointer:
29269 case DW_OP_GNU_implicit_pointer:
29270 hstate.add_int (val2->v.val_int);
29271 break;
29272 case DW_OP_entry_value:
29273 case DW_OP_GNU_entry_value:
29274 hstate.add_object (val1->v.val_loc);
29275 break;
29276 case DW_OP_regval_type:
29277 case DW_OP_deref_type:
29278 case DW_OP_GNU_regval_type:
29279 case DW_OP_GNU_deref_type:
29281 unsigned int byte_size
29282 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29283 unsigned int encoding
29284 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29285 hstate.add_object (val1->v.val_int);
29286 hstate.add_object (byte_size);
29287 hstate.add_object (encoding);
29289 break;
29290 case DW_OP_convert:
29291 case DW_OP_reinterpret:
29292 case DW_OP_GNU_convert:
29293 case DW_OP_GNU_reinterpret:
29294 if (val1->val_class == dw_val_class_unsigned_const)
29296 hstate.add_object (val1->v.val_unsigned);
29297 break;
29299 /* FALLTHRU */
29300 case DW_OP_const_type:
29301 case DW_OP_GNU_const_type:
29303 unsigned int byte_size
29304 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29305 unsigned int encoding
29306 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29307 hstate.add_object (byte_size);
29308 hstate.add_object (encoding);
29309 if (loc->dw_loc_opc != DW_OP_const_type
29310 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29311 break;
29312 hstate.add_object (val2->val_class);
29313 switch (val2->val_class)
29315 case dw_val_class_const:
29316 hstate.add_object (val2->v.val_int);
29317 break;
29318 case dw_val_class_vec:
29320 unsigned int elt_size = val2->v.val_vec.elt_size;
29321 unsigned int len = val2->v.val_vec.length;
29323 hstate.add_object (elt_size);
29324 hstate.add_object (len);
29325 hstate.add (val2->v.val_vec.array, len * elt_size);
29327 break;
29328 case dw_val_class_const_double:
29329 hstate.add_object (val2->v.val_double.low);
29330 hstate.add_object (val2->v.val_double.high);
29331 break;
29332 case dw_val_class_wide_int:
29333 hstate.add (val2->v.val_wide->get_val (),
29334 get_full_len (*val2->v.val_wide)
29335 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29336 break;
29337 default:
29338 gcc_unreachable ();
29341 break;
29343 default:
29344 /* Other codes have no operands. */
29345 break;
29349 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29351 static inline void
29352 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29354 dw_loc_descr_ref l;
29355 bool sizes_computed = false;
29356 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29357 size_of_locs (loc);
29359 for (l = loc; l != NULL; l = l->dw_loc_next)
29361 enum dwarf_location_atom opc = l->dw_loc_opc;
29362 hstate.add_object (opc);
29363 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29365 size_of_locs (loc);
29366 sizes_computed = true;
29368 hash_loc_operands (l, hstate);
29372 /* Compute hash of the whole location list LIST_HEAD. */
29374 static inline void
29375 hash_loc_list (dw_loc_list_ref list_head)
29377 dw_loc_list_ref curr = list_head;
29378 inchash::hash hstate;
29380 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29382 hstate.add (curr->begin, strlen (curr->begin) + 1);
29383 hstate.add (curr->end, strlen (curr->end) + 1);
29384 if (curr->section)
29385 hstate.add (curr->section, strlen (curr->section) + 1);
29386 hash_locs (curr->expr, hstate);
29388 list_head->hash = hstate.end ();
29391 /* Return true if X and Y opcodes have the same operands. */
29393 static inline bool
29394 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29396 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29397 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29398 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29399 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29401 switch (x->dw_loc_opc)
29403 case DW_OP_const4u:
29404 case DW_OP_const8u:
29405 if (x->dtprel)
29406 goto hash_addr;
29407 /* FALLTHRU */
29408 case DW_OP_const1u:
29409 case DW_OP_const1s:
29410 case DW_OP_const2u:
29411 case DW_OP_const2s:
29412 case DW_OP_const4s:
29413 case DW_OP_const8s:
29414 case DW_OP_constu:
29415 case DW_OP_consts:
29416 case DW_OP_pick:
29417 case DW_OP_plus_uconst:
29418 case DW_OP_breg0:
29419 case DW_OP_breg1:
29420 case DW_OP_breg2:
29421 case DW_OP_breg3:
29422 case DW_OP_breg4:
29423 case DW_OP_breg5:
29424 case DW_OP_breg6:
29425 case DW_OP_breg7:
29426 case DW_OP_breg8:
29427 case DW_OP_breg9:
29428 case DW_OP_breg10:
29429 case DW_OP_breg11:
29430 case DW_OP_breg12:
29431 case DW_OP_breg13:
29432 case DW_OP_breg14:
29433 case DW_OP_breg15:
29434 case DW_OP_breg16:
29435 case DW_OP_breg17:
29436 case DW_OP_breg18:
29437 case DW_OP_breg19:
29438 case DW_OP_breg20:
29439 case DW_OP_breg21:
29440 case DW_OP_breg22:
29441 case DW_OP_breg23:
29442 case DW_OP_breg24:
29443 case DW_OP_breg25:
29444 case DW_OP_breg26:
29445 case DW_OP_breg27:
29446 case DW_OP_breg28:
29447 case DW_OP_breg29:
29448 case DW_OP_breg30:
29449 case DW_OP_breg31:
29450 case DW_OP_regx:
29451 case DW_OP_fbreg:
29452 case DW_OP_piece:
29453 case DW_OP_deref_size:
29454 case DW_OP_xderef_size:
29455 return valx1->v.val_int == valy1->v.val_int;
29456 case DW_OP_skip:
29457 case DW_OP_bra:
29458 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29459 can cause irrelevant differences in dw_loc_addr. */
29460 gcc_assert (valx1->val_class == dw_val_class_loc
29461 && valy1->val_class == dw_val_class_loc
29462 && (dwarf_split_debug_info
29463 || x->dw_loc_addr == y->dw_loc_addr));
29464 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29465 case DW_OP_implicit_value:
29466 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29467 || valx2->val_class != valy2->val_class)
29468 return false;
29469 switch (valx2->val_class)
29471 case dw_val_class_const:
29472 return valx2->v.val_int == valy2->v.val_int;
29473 case dw_val_class_vec:
29474 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29475 && valx2->v.val_vec.length == valy2->v.val_vec.length
29476 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29477 valx2->v.val_vec.elt_size
29478 * valx2->v.val_vec.length) == 0;
29479 case dw_val_class_const_double:
29480 return valx2->v.val_double.low == valy2->v.val_double.low
29481 && valx2->v.val_double.high == valy2->v.val_double.high;
29482 case dw_val_class_wide_int:
29483 return *valx2->v.val_wide == *valy2->v.val_wide;
29484 case dw_val_class_addr:
29485 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29486 default:
29487 gcc_unreachable ();
29489 case DW_OP_bregx:
29490 case DW_OP_bit_piece:
29491 return valx1->v.val_int == valy1->v.val_int
29492 && valx2->v.val_int == valy2->v.val_int;
29493 case DW_OP_addr:
29494 hash_addr:
29495 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29496 case DW_OP_GNU_addr_index:
29497 case DW_OP_GNU_const_index:
29499 rtx ax1 = valx1->val_entry->addr.rtl;
29500 rtx ay1 = valy1->val_entry->addr.rtl;
29501 return rtx_equal_p (ax1, ay1);
29503 case DW_OP_implicit_pointer:
29504 case DW_OP_GNU_implicit_pointer:
29505 return valx1->val_class == dw_val_class_die_ref
29506 && valx1->val_class == valy1->val_class
29507 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29508 && valx2->v.val_int == valy2->v.val_int;
29509 case DW_OP_entry_value:
29510 case DW_OP_GNU_entry_value:
29511 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29512 case DW_OP_const_type:
29513 case DW_OP_GNU_const_type:
29514 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29515 || valx2->val_class != valy2->val_class)
29516 return false;
29517 switch (valx2->val_class)
29519 case dw_val_class_const:
29520 return valx2->v.val_int == valy2->v.val_int;
29521 case dw_val_class_vec:
29522 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29523 && valx2->v.val_vec.length == valy2->v.val_vec.length
29524 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29525 valx2->v.val_vec.elt_size
29526 * valx2->v.val_vec.length) == 0;
29527 case dw_val_class_const_double:
29528 return valx2->v.val_double.low == valy2->v.val_double.low
29529 && valx2->v.val_double.high == valy2->v.val_double.high;
29530 case dw_val_class_wide_int:
29531 return *valx2->v.val_wide == *valy2->v.val_wide;
29532 default:
29533 gcc_unreachable ();
29535 case DW_OP_regval_type:
29536 case DW_OP_deref_type:
29537 case DW_OP_GNU_regval_type:
29538 case DW_OP_GNU_deref_type:
29539 return valx1->v.val_int == valy1->v.val_int
29540 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29541 case DW_OP_convert:
29542 case DW_OP_reinterpret:
29543 case DW_OP_GNU_convert:
29544 case DW_OP_GNU_reinterpret:
29545 if (valx1->val_class != valy1->val_class)
29546 return false;
29547 if (valx1->val_class == dw_val_class_unsigned_const)
29548 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29549 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29550 case DW_OP_GNU_parameter_ref:
29551 return valx1->val_class == dw_val_class_die_ref
29552 && valx1->val_class == valy1->val_class
29553 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29554 default:
29555 /* Other codes have no operands. */
29556 return true;
29560 /* Return true if DWARF location expressions X and Y are the same. */
29562 static inline bool
29563 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29565 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29566 if (x->dw_loc_opc != y->dw_loc_opc
29567 || x->dtprel != y->dtprel
29568 || !compare_loc_operands (x, y))
29569 break;
29570 return x == NULL && y == NULL;
29573 /* Hashtable helpers. */
29575 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29577 static inline hashval_t hash (const dw_loc_list_struct *);
29578 static inline bool equal (const dw_loc_list_struct *,
29579 const dw_loc_list_struct *);
29582 /* Return precomputed hash of location list X. */
29584 inline hashval_t
29585 loc_list_hasher::hash (const dw_loc_list_struct *x)
29587 return x->hash;
29590 /* Return true if location lists A and B are the same. */
29592 inline bool
29593 loc_list_hasher::equal (const dw_loc_list_struct *a,
29594 const dw_loc_list_struct *b)
29596 if (a == b)
29597 return 1;
29598 if (a->hash != b->hash)
29599 return 0;
29600 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29601 if (strcmp (a->begin, b->begin) != 0
29602 || strcmp (a->end, b->end) != 0
29603 || (a->section == NULL) != (b->section == NULL)
29604 || (a->section && strcmp (a->section, b->section) != 0)
29605 || !compare_locs (a->expr, b->expr))
29606 break;
29607 return a == NULL && b == NULL;
29610 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29613 /* Recursively optimize location lists referenced from DIE
29614 children and share them whenever possible. */
29616 static void
29617 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29619 dw_die_ref c;
29620 dw_attr_node *a;
29621 unsigned ix;
29622 dw_loc_list_struct **slot;
29624 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29625 if (AT_class (a) == dw_val_class_loc_list)
29627 dw_loc_list_ref list = AT_loc_list (a);
29628 /* TODO: perform some optimizations here, before hashing
29629 it and storing into the hash table. */
29630 hash_loc_list (list);
29631 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29632 if (*slot == NULL)
29633 *slot = list;
29634 else
29635 a->dw_attr_val.v.val_loc_list = *slot;
29638 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29642 /* Recursively assign each location list a unique index into the debug_addr
29643 section. */
29645 static void
29646 index_location_lists (dw_die_ref die)
29648 dw_die_ref c;
29649 dw_attr_node *a;
29650 unsigned ix;
29652 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29653 if (AT_class (a) == dw_val_class_loc_list)
29655 dw_loc_list_ref list = AT_loc_list (a);
29656 dw_loc_list_ref curr;
29657 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29659 /* Don't index an entry that has already been indexed
29660 or won't be output. */
29661 if (curr->begin_entry != NULL
29662 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29663 continue;
29665 curr->begin_entry
29666 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29670 FOR_EACH_CHILD (die, c, index_location_lists (c));
29673 /* Optimize location lists referenced from DIE
29674 children and share them whenever possible. */
29676 static void
29677 optimize_location_lists (dw_die_ref die)
29679 loc_list_hash_type htab (500);
29680 optimize_location_lists_1 (die, &htab);
29683 /* Traverse the limbo die list, and add parent/child links. The only
29684 dies without parents that should be here are concrete instances of
29685 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29686 For concrete instances, we can get the parent die from the abstract
29687 instance. */
29689 static void
29690 flush_limbo_die_list (void)
29692 limbo_die_node *node;
29694 /* get_context_die calls force_decl_die, which can put new DIEs on the
29695 limbo list in LTO mode when nested functions are put in a different
29696 partition than that of their parent function. */
29697 while ((node = limbo_die_list))
29699 dw_die_ref die = node->die;
29700 limbo_die_list = node->next;
29702 if (die->die_parent == NULL)
29704 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29706 if (origin && origin->die_parent)
29707 add_child_die (origin->die_parent, die);
29708 else if (is_cu_die (die))
29710 else if (seen_error ())
29711 /* It's OK to be confused by errors in the input. */
29712 add_child_die (comp_unit_die (), die);
29713 else
29715 /* In certain situations, the lexical block containing a
29716 nested function can be optimized away, which results
29717 in the nested function die being orphaned. Likewise
29718 with the return type of that nested function. Force
29719 this to be a child of the containing function.
29721 It may happen that even the containing function got fully
29722 inlined and optimized out. In that case we are lost and
29723 assign the empty child. This should not be big issue as
29724 the function is likely unreachable too. */
29725 gcc_assert (node->created_for);
29727 if (DECL_P (node->created_for))
29728 origin = get_context_die (DECL_CONTEXT (node->created_for));
29729 else if (TYPE_P (node->created_for))
29730 origin = scope_die_for (node->created_for, comp_unit_die ());
29731 else
29732 origin = comp_unit_die ();
29734 add_child_die (origin, die);
29740 /* Reset DIEs so we can output them again. */
29742 static void
29743 reset_dies (dw_die_ref die)
29745 dw_die_ref c;
29747 /* Remove stuff we re-generate. */
29748 die->die_mark = 0;
29749 die->die_offset = 0;
29750 die->die_abbrev = 0;
29751 remove_AT (die, DW_AT_sibling);
29753 FOR_EACH_CHILD (die, c, reset_dies (c));
29756 /* Output stuff that dwarf requires at the end of every file,
29757 and generate the DWARF-2 debugging info. */
29759 static void
29760 dwarf2out_finish (const char *)
29762 comdat_type_node *ctnode;
29763 dw_die_ref main_comp_unit_die;
29764 unsigned char checksum[16];
29765 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29767 /* Flush out any latecomers to the limbo party. */
29768 flush_limbo_die_list ();
29770 if (flag_checking)
29772 verify_die (comp_unit_die ());
29773 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29774 verify_die (node->die);
29777 /* We shouldn't have any symbols with delayed asm names for
29778 DIEs generated after early finish. */
29779 gcc_assert (deferred_asm_name == NULL);
29781 gen_remaining_tmpl_value_param_die_attribute ();
29783 if (flag_generate_lto || flag_generate_offload)
29785 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29787 /* Prune stuff so that dwarf2out_finish runs successfully
29788 for the fat part of the object. */
29789 reset_dies (comp_unit_die ());
29790 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29791 reset_dies (node->die);
29793 hash_table<comdat_type_hasher> comdat_type_table (100);
29794 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29796 comdat_type_node **slot
29797 = comdat_type_table.find_slot (ctnode, INSERT);
29799 /* Don't reset types twice. */
29800 if (*slot != HTAB_EMPTY_ENTRY)
29801 continue;
29803 /* Add a pointer to the line table for the main compilation unit
29804 so that the debugger can make sense of DW_AT_decl_file
29805 attributes. */
29806 if (debug_info_level >= DINFO_LEVEL_TERSE)
29807 reset_dies (ctnode->root_die);
29809 *slot = ctnode;
29812 /* Reset die CU symbol so we don't output it twice. */
29813 comp_unit_die ()->die_id.die_symbol = NULL;
29815 /* Remove DW_AT_macro from the early output. */
29816 if (have_macinfo)
29817 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29819 /* Remove indirect string decisions. */
29820 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29823 #if ENABLE_ASSERT_CHECKING
29825 dw_die_ref die = comp_unit_die (), c;
29826 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29828 #endif
29829 resolve_addr (comp_unit_die ());
29830 move_marked_base_types ();
29832 /* Initialize sections and labels used for actual assembler output. */
29833 init_sections_and_labels (false);
29835 /* Traverse the DIE's and add sibling attributes to those DIE's that
29836 have children. */
29837 add_sibling_attributes (comp_unit_die ());
29838 limbo_die_node *node;
29839 for (node = cu_die_list; node; node = node->next)
29840 add_sibling_attributes (node->die);
29841 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29842 add_sibling_attributes (ctnode->root_die);
29844 /* When splitting DWARF info, we put some attributes in the
29845 skeleton compile_unit DIE that remains in the .o, while
29846 most attributes go in the DWO compile_unit_die. */
29847 if (dwarf_split_debug_info)
29849 limbo_die_node *cu;
29850 main_comp_unit_die = gen_compile_unit_die (NULL);
29851 if (dwarf_version >= 5)
29852 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29853 cu = limbo_die_list;
29854 gcc_assert (cu->die == main_comp_unit_die);
29855 limbo_die_list = limbo_die_list->next;
29856 cu->next = cu_die_list;
29857 cu_die_list = cu;
29859 else
29860 main_comp_unit_die = comp_unit_die ();
29862 /* Output a terminator label for the .text section. */
29863 switch_to_section (text_section);
29864 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29865 if (cold_text_section)
29867 switch_to_section (cold_text_section);
29868 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29871 /* We can only use the low/high_pc attributes if all of the code was
29872 in .text. */
29873 if (!have_multiple_function_sections
29874 || (dwarf_version < 3 && dwarf_strict))
29876 /* Don't add if the CU has no associated code. */
29877 if (text_section_used)
29878 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29879 text_end_label, true);
29881 else
29883 unsigned fde_idx;
29884 dw_fde_ref fde;
29885 bool range_list_added = false;
29887 if (text_section_used)
29888 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29889 text_end_label, &range_list_added, true);
29890 if (cold_text_section_used)
29891 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29892 cold_end_label, &range_list_added, true);
29894 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29896 if (DECL_IGNORED_P (fde->decl))
29897 continue;
29898 if (!fde->in_std_section)
29899 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29900 fde->dw_fde_end, &range_list_added,
29901 true);
29902 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29903 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29904 fde->dw_fde_second_end, &range_list_added,
29905 true);
29908 if (range_list_added)
29910 /* We need to give .debug_loc and .debug_ranges an appropriate
29911 "base address". Use zero so that these addresses become
29912 absolute. Historically, we've emitted the unexpected
29913 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29914 Emit both to give time for other tools to adapt. */
29915 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29916 if (! dwarf_strict && dwarf_version < 4)
29917 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29919 add_ranges (NULL);
29923 /* AIX Assembler inserts the length, so adjust the reference to match the
29924 offset expected by debuggers. */
29925 strcpy (dl_section_ref, debug_line_section_label);
29926 if (XCOFF_DEBUGGING_INFO)
29927 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29929 if (debug_info_level >= DINFO_LEVEL_TERSE)
29930 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29931 dl_section_ref);
29933 if (have_macinfo)
29934 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
29935 macinfo_section_label);
29937 if (dwarf_split_debug_info)
29939 if (have_location_lists)
29941 if (dwarf_version >= 5)
29942 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29943 loc_section_label);
29944 /* optimize_location_lists calculates the size of the lists,
29945 so index them first, and assign indices to the entries.
29946 Although optimize_location_lists will remove entries from
29947 the table, it only does so for duplicates, and therefore
29948 only reduces ref_counts to 1. */
29949 index_location_lists (comp_unit_die ());
29952 if (addr_index_table != NULL)
29954 unsigned int index = 0;
29955 addr_index_table
29956 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29957 (&index);
29961 loc_list_idx = 0;
29962 if (have_location_lists)
29964 optimize_location_lists (comp_unit_die ());
29965 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29966 if (dwarf_version >= 5 && dwarf_split_debug_info)
29967 assign_location_list_indexes (comp_unit_die ());
29970 save_macinfo_strings ();
29972 if (dwarf_split_debug_info)
29974 unsigned int index = 0;
29976 /* Add attributes common to skeleton compile_units and
29977 type_units. Because these attributes include strings, it
29978 must be done before freezing the string table. Top-level
29979 skeleton die attrs are added when the skeleton type unit is
29980 created, so ensure it is created by this point. */
29981 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29982 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29985 /* Output all of the compilation units. We put the main one last so that
29986 the offsets are available to output_pubnames. */
29987 for (node = cu_die_list; node; node = node->next)
29988 output_comp_unit (node->die, 0, NULL);
29990 hash_table<comdat_type_hasher> comdat_type_table (100);
29991 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29993 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29995 /* Don't output duplicate types. */
29996 if (*slot != HTAB_EMPTY_ENTRY)
29997 continue;
29999 /* Add a pointer to the line table for the main compilation unit
30000 so that the debugger can make sense of DW_AT_decl_file
30001 attributes. */
30002 if (debug_info_level >= DINFO_LEVEL_TERSE)
30003 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30004 (!dwarf_split_debug_info
30005 ? dl_section_ref
30006 : debug_skeleton_line_section_label));
30008 output_comdat_type_unit (ctnode);
30009 *slot = ctnode;
30012 if (dwarf_split_debug_info)
30014 int mark;
30015 struct md5_ctx ctx;
30017 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30018 index_rnglists ();
30020 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30021 md5_init_ctx (&ctx);
30022 mark = 0;
30023 die_checksum (comp_unit_die (), &ctx, &mark);
30024 unmark_all_dies (comp_unit_die ());
30025 md5_finish_ctx (&ctx, checksum);
30027 if (dwarf_version < 5)
30029 /* Use the first 8 bytes of the checksum as the dwo_id,
30030 and add it to both comp-unit DIEs. */
30031 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30032 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30035 /* Add the base offset of the ranges table to the skeleton
30036 comp-unit DIE. */
30037 if (!vec_safe_is_empty (ranges_table))
30039 if (dwarf_version >= 5)
30040 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30041 ranges_base_label);
30042 else
30043 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30044 ranges_section_label);
30047 switch_to_section (debug_addr_section);
30048 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30049 output_addr_table ();
30052 /* Output the main compilation unit if non-empty or if .debug_macinfo
30053 or .debug_macro will be emitted. */
30054 output_comp_unit (comp_unit_die (), have_macinfo,
30055 dwarf_split_debug_info ? checksum : NULL);
30057 if (dwarf_split_debug_info && info_section_emitted)
30058 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30060 /* Output the abbreviation table. */
30061 if (vec_safe_length (abbrev_die_table) != 1)
30063 switch_to_section (debug_abbrev_section);
30064 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30065 output_abbrev_section ();
30068 /* Output location list section if necessary. */
30069 if (have_location_lists)
30071 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30072 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30073 /* Output the location lists info. */
30074 switch_to_section (debug_loc_section);
30075 if (dwarf_version >= 5)
30077 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30078 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30079 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30080 dw2_asm_output_data (4, 0xffffffff,
30081 "Initial length escape value indicating "
30082 "64-bit DWARF extension");
30083 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30084 "Length of Location Lists");
30085 ASM_OUTPUT_LABEL (asm_out_file, l1);
30086 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30087 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30088 dw2_asm_output_data (1, 0, "Segment Size");
30089 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30090 "Offset Entry Count");
30092 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30093 if (dwarf_version >= 5 && dwarf_split_debug_info)
30095 unsigned int save_loc_list_idx = loc_list_idx;
30096 loc_list_idx = 0;
30097 output_loclists_offsets (comp_unit_die ());
30098 gcc_assert (save_loc_list_idx == loc_list_idx);
30100 output_location_lists (comp_unit_die ());
30101 if (dwarf_version >= 5)
30102 ASM_OUTPUT_LABEL (asm_out_file, l2);
30105 output_pubtables ();
30107 /* Output the address range information if a CU (.debug_info section)
30108 was emitted. We output an empty table even if we had no functions
30109 to put in it. This because the consumer has no way to tell the
30110 difference between an empty table that we omitted and failure to
30111 generate a table that would have contained data. */
30112 if (info_section_emitted)
30114 switch_to_section (debug_aranges_section);
30115 output_aranges ();
30118 /* Output ranges section if necessary. */
30119 if (!vec_safe_is_empty (ranges_table))
30121 if (dwarf_version >= 5)
30122 output_rnglists ();
30123 else
30124 output_ranges ();
30127 /* Have to end the macro section. */
30128 if (have_macinfo)
30130 switch_to_section (debug_macinfo_section);
30131 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30132 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30133 : debug_skeleton_line_section_label, false);
30134 dw2_asm_output_data (1, 0, "End compilation unit");
30137 /* Output the source line correspondence table. We must do this
30138 even if there is no line information. Otherwise, on an empty
30139 translation unit, we will generate a present, but empty,
30140 .debug_info section. IRIX 6.5 `nm' will then complain when
30141 examining the file. This is done late so that any filenames
30142 used by the debug_info section are marked as 'used'. */
30143 switch_to_section (debug_line_section);
30144 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30145 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30146 output_line_info (false);
30148 if (dwarf_split_debug_info && info_section_emitted)
30150 switch_to_section (debug_skeleton_line_section);
30151 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30152 output_line_info (true);
30155 /* If we emitted any indirect strings, output the string table too. */
30156 if (debug_str_hash || skeleton_debug_str_hash)
30157 output_indirect_strings ();
30158 if (debug_line_str_hash)
30160 switch_to_section (debug_line_str_section);
30161 const enum dwarf_form form = DW_FORM_line_strp;
30162 debug_line_str_hash->traverse<enum dwarf_form,
30163 output_indirect_string> (form);
30167 /* Returns a hash value for X (which really is a variable_value_struct). */
30169 inline hashval_t
30170 variable_value_hasher::hash (variable_value_struct *x)
30172 return (hashval_t) x->decl_id;
30175 /* Return nonzero if decl_id of variable_value_struct X is the same as
30176 UID of decl Y. */
30178 inline bool
30179 variable_value_hasher::equal (variable_value_struct *x, tree y)
30181 return x->decl_id == DECL_UID (y);
30184 /* Helper function for resolve_variable_value, handle
30185 DW_OP_GNU_variable_value in one location expression.
30186 Return true if exprloc has been changed into loclist. */
30188 static bool
30189 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30191 dw_loc_descr_ref next;
30192 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30194 next = loc->dw_loc_next;
30195 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30196 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30197 continue;
30199 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30200 if (DECL_CONTEXT (decl) != current_function_decl)
30201 continue;
30203 dw_die_ref ref = lookup_decl_die (decl);
30204 if (ref)
30206 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30207 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30208 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30209 continue;
30211 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30212 if (l == NULL)
30213 continue;
30214 if (l->dw_loc_next)
30216 if (AT_class (a) != dw_val_class_loc)
30217 continue;
30218 switch (a->dw_attr)
30220 /* Following attributes allow both exprloc and loclist
30221 classes, so we can change them into a loclist. */
30222 case DW_AT_location:
30223 case DW_AT_string_length:
30224 case DW_AT_return_addr:
30225 case DW_AT_data_member_location:
30226 case DW_AT_frame_base:
30227 case DW_AT_segment:
30228 case DW_AT_static_link:
30229 case DW_AT_use_location:
30230 case DW_AT_vtable_elem_location:
30231 if (prev)
30233 prev->dw_loc_next = NULL;
30234 prepend_loc_descr_to_each (l, AT_loc (a));
30236 if (next)
30237 add_loc_descr_to_each (l, next);
30238 a->dw_attr_val.val_class = dw_val_class_loc_list;
30239 a->dw_attr_val.val_entry = NULL;
30240 a->dw_attr_val.v.val_loc_list = l;
30241 have_location_lists = true;
30242 return true;
30243 /* Following attributes allow both exprloc and reference,
30244 so if the whole expression is DW_OP_GNU_variable_value alone
30245 we could transform it into reference. */
30246 case DW_AT_byte_size:
30247 case DW_AT_bit_size:
30248 case DW_AT_lower_bound:
30249 case DW_AT_upper_bound:
30250 case DW_AT_bit_stride:
30251 case DW_AT_count:
30252 case DW_AT_allocated:
30253 case DW_AT_associated:
30254 case DW_AT_byte_stride:
30255 if (prev == NULL && next == NULL)
30256 break;
30257 /* FALLTHRU */
30258 default:
30259 if (dwarf_strict)
30260 continue;
30261 break;
30263 /* Create DW_TAG_variable that we can refer to. */
30264 gen_decl_die (decl, NULL_TREE, NULL,
30265 lookup_decl_die (current_function_decl));
30266 ref = lookup_decl_die (decl);
30267 if (ref)
30269 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30270 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30271 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30273 continue;
30275 if (prev)
30277 prev->dw_loc_next = l->expr;
30278 add_loc_descr (&prev->dw_loc_next, next);
30279 free_loc_descr (loc, NULL);
30280 next = prev->dw_loc_next;
30282 else
30284 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30285 add_loc_descr (&loc, next);
30286 next = loc;
30288 loc = prev;
30290 return false;
30293 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30295 static void
30296 resolve_variable_value (dw_die_ref die)
30298 dw_attr_node *a;
30299 dw_loc_list_ref loc;
30300 unsigned ix;
30302 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30303 switch (AT_class (a))
30305 case dw_val_class_loc:
30306 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30307 break;
30308 /* FALLTHRU */
30309 case dw_val_class_loc_list:
30310 loc = AT_loc_list (a);
30311 gcc_assert (loc);
30312 for (; loc; loc = loc->dw_loc_next)
30313 resolve_variable_value_in_expr (a, loc->expr);
30314 break;
30315 default:
30316 break;
30320 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30321 temporaries in the current function. */
30323 static void
30324 resolve_variable_values (void)
30326 if (!variable_value_hash || !current_function_decl)
30327 return;
30329 struct variable_value_struct *node
30330 = variable_value_hash->find_with_hash (current_function_decl,
30331 DECL_UID (current_function_decl));
30333 if (node == NULL)
30334 return;
30336 unsigned int i;
30337 dw_die_ref die;
30338 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30339 resolve_variable_value (die);
30342 /* Helper function for note_variable_value, handle one location
30343 expression. */
30345 static void
30346 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30348 for (; loc; loc = loc->dw_loc_next)
30349 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30350 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30352 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30353 dw_die_ref ref = lookup_decl_die (decl);
30354 if (! ref && (flag_generate_lto || flag_generate_offload))
30356 /* ??? This is somewhat a hack because we do not create DIEs
30357 for variables not in BLOCK trees early but when generating
30358 early LTO output we need the dw_val_class_decl_ref to be
30359 fully resolved. For fat LTO objects we'd also like to
30360 undo this after LTO dwarf output. */
30361 gcc_assert (DECL_CONTEXT (decl));
30362 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30363 gcc_assert (ctx != NULL);
30364 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30365 ref = lookup_decl_die (decl);
30366 gcc_assert (ref != NULL);
30368 if (ref)
30370 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30371 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30372 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30373 continue;
30375 if (VAR_P (decl)
30376 && DECL_CONTEXT (decl)
30377 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30378 && lookup_decl_die (DECL_CONTEXT (decl)))
30380 if (!variable_value_hash)
30381 variable_value_hash
30382 = hash_table<variable_value_hasher>::create_ggc (10);
30384 tree fndecl = DECL_CONTEXT (decl);
30385 struct variable_value_struct *node;
30386 struct variable_value_struct **slot
30387 = variable_value_hash->find_slot_with_hash (fndecl,
30388 DECL_UID (fndecl),
30389 INSERT);
30390 if (*slot == NULL)
30392 node = ggc_cleared_alloc<variable_value_struct> ();
30393 node->decl_id = DECL_UID (fndecl);
30394 *slot = node;
30396 else
30397 node = *slot;
30399 vec_safe_push (node->dies, die);
30404 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30405 with dw_val_class_decl_ref operand. */
30407 static void
30408 note_variable_value (dw_die_ref die)
30410 dw_die_ref c;
30411 dw_attr_node *a;
30412 dw_loc_list_ref loc;
30413 unsigned ix;
30415 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30416 switch (AT_class (a))
30418 case dw_val_class_loc_list:
30419 loc = AT_loc_list (a);
30420 gcc_assert (loc);
30421 if (!loc->noted_variable_value)
30423 loc->noted_variable_value = 1;
30424 for (; loc; loc = loc->dw_loc_next)
30425 note_variable_value_in_expr (die, loc->expr);
30427 break;
30428 case dw_val_class_loc:
30429 note_variable_value_in_expr (die, AT_loc (a));
30430 break;
30431 default:
30432 break;
30435 /* Mark children. */
30436 FOR_EACH_CHILD (die, c, note_variable_value (c));
30439 /* Perform any cleanups needed after the early debug generation pass
30440 has run. */
30442 static void
30443 dwarf2out_early_finish (const char *filename)
30445 set_early_dwarf s;
30447 /* PCH might result in DW_AT_producer string being restored from the
30448 header compilation, so always fill it with empty string initially
30449 and overwrite only here. */
30450 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30451 producer_string = gen_producer_string ();
30452 producer->dw_attr_val.v.val_str->refcount--;
30453 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30455 /* Add the name for the main input file now. We delayed this from
30456 dwarf2out_init to avoid complications with PCH. */
30457 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30458 add_comp_dir_attribute (comp_unit_die ());
30460 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30461 DW_AT_comp_dir into .debug_line_str section. */
30462 if (!DWARF2_ASM_LINE_DEBUG_INFO
30463 && dwarf_version >= 5
30464 && DWARF5_USE_DEBUG_LINE_STR)
30466 for (int i = 0; i < 2; i++)
30468 dw_attr_node *a = get_AT (comp_unit_die (),
30469 i ? DW_AT_comp_dir : DW_AT_name);
30470 if (a == NULL
30471 || AT_class (a) != dw_val_class_str
30472 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30473 continue;
30475 if (! debug_line_str_hash)
30476 debug_line_str_hash
30477 = hash_table<indirect_string_hasher>::create_ggc (10);
30479 struct indirect_string_node *node
30480 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30481 set_indirect_string (node);
30482 node->form = DW_FORM_line_strp;
30483 a->dw_attr_val.v.val_str->refcount--;
30484 a->dw_attr_val.v.val_str = node;
30488 /* With LTO early dwarf was really finished at compile-time, so make
30489 sure to adjust the phase after annotating the LTRANS CU DIE. */
30490 if (in_lto_p)
30492 early_dwarf_finished = true;
30493 return;
30496 /* Walk through the list of incomplete types again, trying once more to
30497 emit full debugging info for them. */
30498 retry_incomplete_types ();
30500 /* The point here is to flush out the limbo list so that it is empty
30501 and we don't need to stream it for LTO. */
30502 flush_limbo_die_list ();
30504 gen_scheduled_generic_parms_dies ();
30505 gen_remaining_tmpl_value_param_die_attribute ();
30507 /* Add DW_AT_linkage_name for all deferred DIEs. */
30508 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30510 tree decl = node->created_for;
30511 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30512 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30513 ended up in deferred_asm_name before we knew it was
30514 constant and never written to disk. */
30515 && DECL_ASSEMBLER_NAME (decl))
30517 add_linkage_attr (node->die, decl);
30518 move_linkage_attr (node->die);
30521 deferred_asm_name = NULL;
30523 if (flag_eliminate_unused_debug_types)
30524 prune_unused_types ();
30526 /* Generate separate COMDAT sections for type DIEs. */
30527 if (use_debug_types)
30529 break_out_comdat_types (comp_unit_die ());
30531 /* Each new type_unit DIE was added to the limbo die list when created.
30532 Since these have all been added to comdat_type_list, clear the
30533 limbo die list. */
30534 limbo_die_list = NULL;
30536 /* For each new comdat type unit, copy declarations for incomplete
30537 types to make the new unit self-contained (i.e., no direct
30538 references to the main compile unit). */
30539 for (comdat_type_node *ctnode = comdat_type_list;
30540 ctnode != NULL; ctnode = ctnode->next)
30541 copy_decls_for_unworthy_types (ctnode->root_die);
30542 copy_decls_for_unworthy_types (comp_unit_die ());
30544 /* In the process of copying declarations from one unit to another,
30545 we may have left some declarations behind that are no longer
30546 referenced. Prune them. */
30547 prune_unused_types ();
30550 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30551 with dw_val_class_decl_ref operand. */
30552 note_variable_value (comp_unit_die ());
30553 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30554 note_variable_value (node->die);
30555 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30556 ctnode = ctnode->next)
30557 note_variable_value (ctnode->root_die);
30558 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30559 note_variable_value (node->die);
30561 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30562 both the main_cu and all skeleton TUs. Making this call unconditional
30563 would end up either adding a second copy of the AT_pubnames attribute, or
30564 requiring a special case in add_top_level_skeleton_die_attrs. */
30565 if (!dwarf_split_debug_info)
30566 add_AT_pubnames (comp_unit_die ());
30568 /* The early debug phase is now finished. */
30569 early_dwarf_finished = true;
30571 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30572 if (!flag_generate_lto && !flag_generate_offload)
30573 return;
30575 /* Now as we are going to output for LTO initialize sections and labels
30576 to the LTO variants. We don't need a random-seed postfix as other
30577 LTO sections as linking the LTO debug sections into one in a partial
30578 link is fine. */
30579 init_sections_and_labels (true);
30581 /* The output below is modeled after dwarf2out_finish with all
30582 location related output removed and some LTO specific changes.
30583 Some refactoring might make both smaller and easier to match up. */
30585 /* Traverse the DIE's and add add sibling attributes to those DIE's
30586 that have children. */
30587 add_sibling_attributes (comp_unit_die ());
30588 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30589 add_sibling_attributes (node->die);
30590 for (comdat_type_node *ctnode = comdat_type_list;
30591 ctnode != NULL; ctnode = ctnode->next)
30592 add_sibling_attributes (ctnode->root_die);
30594 if (have_macinfo)
30595 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30596 macinfo_section_label);
30598 save_macinfo_strings ();
30600 /* Output all of the compilation units. We put the main one last so that
30601 the offsets are available to output_pubnames. */
30602 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30603 output_comp_unit (node->die, 0, NULL);
30605 hash_table<comdat_type_hasher> comdat_type_table (100);
30606 for (comdat_type_node *ctnode = comdat_type_list;
30607 ctnode != NULL; ctnode = ctnode->next)
30609 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30611 /* Don't output duplicate types. */
30612 if (*slot != HTAB_EMPTY_ENTRY)
30613 continue;
30615 /* Add a pointer to the line table for the main compilation unit
30616 so that the debugger can make sense of DW_AT_decl_file
30617 attributes. */
30618 if (debug_info_level >= DINFO_LEVEL_TERSE)
30619 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30620 (!dwarf_split_debug_info
30621 ? debug_line_section_label
30622 : debug_skeleton_line_section_label));
30624 output_comdat_type_unit (ctnode);
30625 *slot = ctnode;
30628 /* Stick a unique symbol to the main debuginfo section. */
30629 compute_comp_unit_symbol (comp_unit_die ());
30631 /* Output the main compilation unit. We always need it if only for
30632 the CU symbol. */
30633 output_comp_unit (comp_unit_die (), true, NULL);
30635 /* Output the abbreviation table. */
30636 if (vec_safe_length (abbrev_die_table) != 1)
30638 switch_to_section (debug_abbrev_section);
30639 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30640 output_abbrev_section ();
30643 /* Have to end the macro section. */
30644 if (have_macinfo)
30646 /* We have to save macinfo state if we need to output it again
30647 for the FAT part of the object. */
30648 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30649 if (flag_fat_lto_objects)
30650 macinfo_table = macinfo_table->copy ();
30652 switch_to_section (debug_macinfo_section);
30653 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30654 output_macinfo (debug_skeleton_line_section_label, true);
30655 dw2_asm_output_data (1, 0, "End compilation unit");
30657 /* Emit a skeleton debug_line section. */
30658 switch_to_section (debug_skeleton_line_section);
30659 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30660 output_line_info (true);
30662 if (flag_fat_lto_objects)
30664 vec_free (macinfo_table);
30665 macinfo_table = saved_macinfo_table;
30670 /* If we emitted any indirect strings, output the string table too. */
30671 if (debug_str_hash || skeleton_debug_str_hash)
30672 output_indirect_strings ();
30674 /* Switch back to the text section. */
30675 switch_to_section (text_section);
30678 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30679 within the same process. For use by toplev::finalize. */
30681 void
30682 dwarf2out_c_finalize (void)
30684 last_var_location_insn = NULL;
30685 cached_next_real_insn = NULL;
30686 used_rtx_array = NULL;
30687 incomplete_types = NULL;
30688 decl_scope_table = NULL;
30689 debug_info_section = NULL;
30690 debug_skeleton_info_section = NULL;
30691 debug_abbrev_section = NULL;
30692 debug_skeleton_abbrev_section = NULL;
30693 debug_aranges_section = NULL;
30694 debug_addr_section = NULL;
30695 debug_macinfo_section = NULL;
30696 debug_line_section = NULL;
30697 debug_skeleton_line_section = NULL;
30698 debug_loc_section = NULL;
30699 debug_pubnames_section = NULL;
30700 debug_pubtypes_section = NULL;
30701 debug_str_section = NULL;
30702 debug_line_str_section = NULL;
30703 debug_str_dwo_section = NULL;
30704 debug_str_offsets_section = NULL;
30705 debug_ranges_section = NULL;
30706 debug_frame_section = NULL;
30707 fde_vec = NULL;
30708 debug_str_hash = NULL;
30709 debug_line_str_hash = NULL;
30710 skeleton_debug_str_hash = NULL;
30711 dw2_string_counter = 0;
30712 have_multiple_function_sections = false;
30713 text_section_used = false;
30714 cold_text_section_used = false;
30715 cold_text_section = NULL;
30716 current_unit_personality = NULL;
30718 early_dwarf = false;
30719 early_dwarf_finished = false;
30721 next_die_offset = 0;
30722 single_comp_unit_die = NULL;
30723 comdat_type_list = NULL;
30724 limbo_die_list = NULL;
30725 file_table = NULL;
30726 decl_die_table = NULL;
30727 common_block_die_table = NULL;
30728 decl_loc_table = NULL;
30729 call_arg_locations = NULL;
30730 call_arg_loc_last = NULL;
30731 call_site_count = -1;
30732 tail_call_site_count = -1;
30733 cached_dw_loc_list_table = NULL;
30734 abbrev_die_table = NULL;
30735 delete dwarf_proc_stack_usage_map;
30736 dwarf_proc_stack_usage_map = NULL;
30737 line_info_label_num = 0;
30738 cur_line_info_table = NULL;
30739 text_section_line_info = NULL;
30740 cold_text_section_line_info = NULL;
30741 separate_line_info = NULL;
30742 info_section_emitted = false;
30743 pubname_table = NULL;
30744 pubtype_table = NULL;
30745 macinfo_table = NULL;
30746 ranges_table = NULL;
30747 ranges_by_label = NULL;
30748 rnglist_idx = 0;
30749 have_location_lists = false;
30750 loclabel_num = 0;
30751 poc_label_num = 0;
30752 last_emitted_file = NULL;
30753 label_num = 0;
30754 tmpl_value_parm_die_table = NULL;
30755 generic_type_instances = NULL;
30756 frame_pointer_fb_offset = 0;
30757 frame_pointer_fb_offset_valid = false;
30758 base_types.release ();
30759 XDELETEVEC (producer_string);
30760 producer_string = NULL;
30763 #include "gt-dwarf2out.h"