Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blob8e422279a61b730f598a8f287cd070cc54bd19b9
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 GTY(()) section *debug_line_section;
168 static GTY(()) section *debug_skeleton_line_section;
169 static GTY(()) section *debug_loc_section;
170 static GTY(()) section *debug_pubnames_section;
171 static GTY(()) section *debug_pubtypes_section;
172 static GTY(()) section *debug_str_section;
173 static GTY(()) section *debug_line_str_section;
174 static GTY(()) section *debug_str_dwo_section;
175 static GTY(()) section *debug_str_offsets_section;
176 static GTY(()) section *debug_ranges_section;
177 static GTY(()) section *debug_frame_section;
179 /* Maximum size (in bytes) of an artificially generated label. */
180 #define MAX_ARTIFICIAL_LABEL_BYTES 30
182 /* According to the (draft) DWARF 3 specification, the initial length
183 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
184 bytes are 0xffffffff, followed by the length stored in the next 8
185 bytes.
187 However, the SGI/MIPS ABI uses an initial length which is equal to
188 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
190 #ifndef DWARF_INITIAL_LENGTH_SIZE
191 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
192 #endif
194 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
195 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
196 #endif
198 /* Round SIZE up to the nearest BOUNDARY. */
199 #define DWARF_ROUND(SIZE,BOUNDARY) \
200 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
202 /* CIE identifier. */
203 #if HOST_BITS_PER_WIDE_INT >= 64
204 #define DWARF_CIE_ID \
205 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
206 #else
207 #define DWARF_CIE_ID DW_CIE_ID
208 #endif
211 /* A vector for a table that contains frame description
212 information for each routine. */
213 #define NOT_INDEXED (-1U)
214 #define NO_INDEX_ASSIGNED (-2U)
216 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
218 struct GTY((for_user)) indirect_string_node {
219 const char *str;
220 unsigned int refcount;
221 enum dwarf_form form;
222 char *label;
223 unsigned int index;
226 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
228 typedef const char *compare_type;
230 static hashval_t hash (indirect_string_node *);
231 static bool equal (indirect_string_node *, const char *);
234 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
236 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
238 /* With split_debug_info, both the comp_dir and dwo_name go in the
239 main object file, rather than the dwo, similar to the force_direct
240 parameter elsewhere but with additional complications:
242 1) The string is needed in both the main object file and the dwo.
243 That is, the comp_dir and dwo_name will appear in both places.
245 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
246 DW_FORM_line_strp or DW_FORM_GNU_str_index.
248 3) GCC chooses the form to use late, depending on the size and
249 reference count.
251 Rather than forcing the all debug string handling functions and
252 callers to deal with these complications, simply use a separate,
253 special-cased string table for any attribute that should go in the
254 main object file. This limits the complexity to just the places
255 that need it. */
257 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
259 static GTY(()) int dw2_string_counter;
261 /* True if the compilation unit places functions in more than one section. */
262 static GTY(()) bool have_multiple_function_sections = false;
264 /* Whether the default text and cold text sections have been used at all. */
266 static GTY(()) bool text_section_used = false;
267 static GTY(()) bool cold_text_section_used = false;
269 /* The default cold text section. */
270 static GTY(()) section *cold_text_section;
272 /* The DIE for C++14 'auto' in a function return type. */
273 static GTY(()) dw_die_ref auto_die;
275 /* The DIE for C++14 'decltype(auto)' in a function return type. */
276 static GTY(()) dw_die_ref decltype_auto_die;
278 /* Forward declarations for functions defined in this file. */
280 static void output_call_frame_info (int);
281 static void dwarf2out_note_section_used (void);
283 /* Personality decl of current unit. Used only when assembler does not support
284 personality CFI. */
285 static GTY(()) rtx current_unit_personality;
287 /* .debug_rnglists next index. */
288 static unsigned int rnglist_idx;
290 /* Data and reference forms for relocatable data. */
291 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
292 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
294 #ifndef DEBUG_FRAME_SECTION
295 #define DEBUG_FRAME_SECTION ".debug_frame"
296 #endif
298 #ifndef FUNC_BEGIN_LABEL
299 #define FUNC_BEGIN_LABEL "LFB"
300 #endif
302 #ifndef FUNC_END_LABEL
303 #define FUNC_END_LABEL "LFE"
304 #endif
306 #ifndef PROLOGUE_END_LABEL
307 #define PROLOGUE_END_LABEL "LPE"
308 #endif
310 #ifndef EPILOGUE_BEGIN_LABEL
311 #define EPILOGUE_BEGIN_LABEL "LEB"
312 #endif
314 #ifndef FRAME_BEGIN_LABEL
315 #define FRAME_BEGIN_LABEL "Lframe"
316 #endif
317 #define CIE_AFTER_SIZE_LABEL "LSCIE"
318 #define CIE_END_LABEL "LECIE"
319 #define FDE_LABEL "LSFDE"
320 #define FDE_AFTER_SIZE_LABEL "LASFDE"
321 #define FDE_END_LABEL "LEFDE"
322 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
323 #define LINE_NUMBER_END_LABEL "LELT"
324 #define LN_PROLOG_AS_LABEL "LASLTP"
325 #define LN_PROLOG_END_LABEL "LELTP"
326 #define DIE_LABEL_PREFIX "DW"
328 /* Match the base name of a file to the base name of a compilation unit. */
330 static int
331 matches_main_base (const char *path)
333 /* Cache the last query. */
334 static const char *last_path = NULL;
335 static int last_match = 0;
336 if (path != last_path)
338 const char *base;
339 int length = base_of_path (path, &base);
340 last_path = path;
341 last_match = (length == main_input_baselength
342 && memcmp (base, main_input_basename, length) == 0);
344 return last_match;
347 #ifdef DEBUG_DEBUG_STRUCT
349 static int
350 dump_struct_debug (tree type, enum debug_info_usage usage,
351 enum debug_struct_file criterion, int generic,
352 int matches, int result)
354 /* Find the type name. */
355 tree type_decl = TYPE_STUB_DECL (type);
356 tree t = type_decl;
357 const char *name = 0;
358 if (TREE_CODE (t) == TYPE_DECL)
359 t = DECL_NAME (t);
360 if (t)
361 name = IDENTIFIER_POINTER (t);
363 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
364 criterion,
365 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
366 matches ? "bas" : "hdr",
367 generic ? "gen" : "ord",
368 usage == DINFO_USAGE_DFN ? ";" :
369 usage == DINFO_USAGE_DIR_USE ? "." : "*",
370 result,
371 (void*) type_decl, name);
372 return result;
374 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
375 dump_struct_debug (type, usage, criterion, generic, matches, result)
377 #else
379 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
380 (result)
382 #endif
384 /* Get the number of HOST_WIDE_INTs needed to represent the precision
385 of the number. Some constants have a large uniform precision, so
386 we get the precision needed for the actual value of the number. */
388 static unsigned int
389 get_full_len (const wide_int &op)
391 int prec = wi::min_precision (op, UNSIGNED);
392 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
393 / HOST_BITS_PER_WIDE_INT);
396 static bool
397 should_emit_struct_debug (tree type, enum debug_info_usage usage)
399 enum debug_struct_file criterion;
400 tree type_decl;
401 bool generic = lang_hooks.types.generic_p (type);
403 if (generic)
404 criterion = debug_struct_generic[usage];
405 else
406 criterion = debug_struct_ordinary[usage];
408 if (criterion == DINFO_STRUCT_FILE_NONE)
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
410 if (criterion == DINFO_STRUCT_FILE_ANY)
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
413 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
415 if (type_decl != NULL)
417 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
418 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
420 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
421 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
424 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
427 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
428 switch to the data section instead, and write out a synthetic start label
429 for collect2 the first time around. */
431 static void
432 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
434 if (eh_frame_section == 0)
436 int flags;
438 if (EH_TABLES_CAN_BE_READ_ONLY)
440 int fde_encoding;
441 int per_encoding;
442 int lsda_encoding;
444 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
445 /*global=*/0);
446 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
447 /*global=*/1);
448 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
449 /*global=*/0);
450 flags = ((! flag_pic
451 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
452 && (fde_encoding & 0x70) != DW_EH_PE_aligned
453 && (per_encoding & 0x70) != DW_EH_PE_absptr
454 && (per_encoding & 0x70) != DW_EH_PE_aligned
455 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
456 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
457 ? 0 : SECTION_WRITE);
459 else
460 flags = SECTION_WRITE;
462 #ifdef EH_FRAME_SECTION_NAME
463 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
464 #else
465 eh_frame_section = ((flags == SECTION_WRITE)
466 ? data_section : readonly_data_section);
467 #endif /* EH_FRAME_SECTION_NAME */
470 switch_to_section (eh_frame_section);
472 #ifdef EH_FRAME_THROUGH_COLLECT2
473 /* We have no special eh_frame section. Emit special labels to guide
474 collect2. */
475 if (!back)
477 tree label = get_file_function_name ("F");
478 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
479 targetm.asm_out.globalize_label (asm_out_file,
480 IDENTIFIER_POINTER (label));
481 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
483 #endif
486 /* Switch [BACK] to the eh or debug frame table section, depending on
487 FOR_EH. */
489 static void
490 switch_to_frame_table_section (int for_eh, bool back)
492 if (for_eh)
493 switch_to_eh_frame_section (back);
494 else
496 if (!debug_frame_section)
497 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
498 SECTION_DEBUG, NULL);
499 switch_to_section (debug_frame_section);
503 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
505 enum dw_cfi_oprnd_type
506 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
508 switch (cfi)
510 case DW_CFA_nop:
511 case DW_CFA_GNU_window_save:
512 case DW_CFA_remember_state:
513 case DW_CFA_restore_state:
514 return dw_cfi_oprnd_unused;
516 case DW_CFA_set_loc:
517 case DW_CFA_advance_loc1:
518 case DW_CFA_advance_loc2:
519 case DW_CFA_advance_loc4:
520 case DW_CFA_MIPS_advance_loc8:
521 return dw_cfi_oprnd_addr;
523 case DW_CFA_offset:
524 case DW_CFA_offset_extended:
525 case DW_CFA_def_cfa:
526 case DW_CFA_offset_extended_sf:
527 case DW_CFA_def_cfa_sf:
528 case DW_CFA_restore:
529 case DW_CFA_restore_extended:
530 case DW_CFA_undefined:
531 case DW_CFA_same_value:
532 case DW_CFA_def_cfa_register:
533 case DW_CFA_register:
534 case DW_CFA_expression:
535 case DW_CFA_val_expression:
536 return dw_cfi_oprnd_reg_num;
538 case DW_CFA_def_cfa_offset:
539 case DW_CFA_GNU_args_size:
540 case DW_CFA_def_cfa_offset_sf:
541 return dw_cfi_oprnd_offset;
543 case DW_CFA_def_cfa_expression:
544 return dw_cfi_oprnd_loc;
546 default:
547 gcc_unreachable ();
551 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
553 enum dw_cfi_oprnd_type
554 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
556 switch (cfi)
558 case DW_CFA_def_cfa:
559 case DW_CFA_def_cfa_sf:
560 case DW_CFA_offset:
561 case DW_CFA_offset_extended_sf:
562 case DW_CFA_offset_extended:
563 return dw_cfi_oprnd_offset;
565 case DW_CFA_register:
566 return dw_cfi_oprnd_reg_num;
568 case DW_CFA_expression:
569 case DW_CFA_val_expression:
570 return dw_cfi_oprnd_loc;
572 default:
573 return dw_cfi_oprnd_unused;
577 /* Output one FDE. */
579 static void
580 output_fde (dw_fde_ref fde, bool for_eh, bool second,
581 char *section_start_label, int fde_encoding, char *augmentation,
582 bool any_lsda_needed, int lsda_encoding)
584 const char *begin, *end;
585 static unsigned int j;
586 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
588 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
589 /* empty */ 0);
590 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
591 for_eh + j);
592 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
593 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
594 if (!XCOFF_DEBUGGING_INFO || for_eh)
596 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
597 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
598 " indicating 64-bit DWARF extension");
599 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
600 "FDE Length");
602 ASM_OUTPUT_LABEL (asm_out_file, l1);
604 if (for_eh)
605 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
606 else
607 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
608 debug_frame_section, "FDE CIE offset");
610 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
611 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
613 if (for_eh)
615 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
616 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
617 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
618 "FDE initial location");
619 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
620 end, begin, "FDE address range");
622 else
624 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
625 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
628 if (augmentation[0])
630 if (any_lsda_needed)
632 int size = size_of_encoded_value (lsda_encoding);
634 if (lsda_encoding == DW_EH_PE_aligned)
636 int offset = ( 4 /* Length */
637 + 4 /* CIE offset */
638 + 2 * size_of_encoded_value (fde_encoding)
639 + 1 /* Augmentation size */ );
640 int pad = -offset & (PTR_SIZE - 1);
642 size += pad;
643 gcc_assert (size_of_uleb128 (size) == 1);
646 dw2_asm_output_data_uleb128 (size, "Augmentation size");
648 if (fde->uses_eh_lsda)
650 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
651 fde->funcdef_number);
652 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
653 gen_rtx_SYMBOL_REF (Pmode, l1),
654 false,
655 "Language Specific Data Area");
657 else
659 if (lsda_encoding == DW_EH_PE_aligned)
660 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
661 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
662 "Language Specific Data Area (none)");
665 else
666 dw2_asm_output_data_uleb128 (0, "Augmentation size");
669 /* Loop through the Call Frame Instructions associated with this FDE. */
670 fde->dw_fde_current_label = begin;
672 size_t from, until, i;
674 from = 0;
675 until = vec_safe_length (fde->dw_fde_cfi);
677 if (fde->dw_fde_second_begin == NULL)
679 else if (!second)
680 until = fde->dw_fde_switch_cfi_index;
681 else
682 from = fde->dw_fde_switch_cfi_index;
684 for (i = from; i < until; i++)
685 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
688 /* If we are to emit a ref/link from function bodies to their frame tables,
689 do it now. This is typically performed to make sure that tables
690 associated with functions are dragged with them and not discarded in
691 garbage collecting links. We need to do this on a per function basis to
692 cope with -ffunction-sections. */
694 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
695 /* Switch to the function section, emit the ref to the tables, and
696 switch *back* into the table section. */
697 switch_to_section (function_section (fde->decl));
698 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
699 switch_to_frame_table_section (for_eh, true);
700 #endif
702 /* Pad the FDE out to an address sized boundary. */
703 ASM_OUTPUT_ALIGN (asm_out_file,
704 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
705 ASM_OUTPUT_LABEL (asm_out_file, l2);
707 j += 2;
710 /* Return true if frame description entry FDE is needed for EH. */
712 static bool
713 fde_needed_for_eh_p (dw_fde_ref fde)
715 if (flag_asynchronous_unwind_tables)
716 return true;
718 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
719 return true;
721 if (fde->uses_eh_lsda)
722 return true;
724 /* If exceptions are enabled, we have collected nothrow info. */
725 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
726 return false;
728 return true;
731 /* Output the call frame information used to record information
732 that relates to calculating the frame pointer, and records the
733 location of saved registers. */
735 static void
736 output_call_frame_info (int for_eh)
738 unsigned int i;
739 dw_fde_ref fde;
740 dw_cfi_ref cfi;
741 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
742 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
743 bool any_lsda_needed = false;
744 char augmentation[6];
745 int augmentation_size;
746 int fde_encoding = DW_EH_PE_absptr;
747 int per_encoding = DW_EH_PE_absptr;
748 int lsda_encoding = DW_EH_PE_absptr;
749 int return_reg;
750 rtx personality = NULL;
751 int dw_cie_version;
753 /* Don't emit a CIE if there won't be any FDEs. */
754 if (!fde_vec)
755 return;
757 /* Nothing to do if the assembler's doing it all. */
758 if (dwarf2out_do_cfi_asm ())
759 return;
761 /* If we don't have any functions we'll want to unwind out of, don't emit
762 any EH unwind information. If we make FDEs linkonce, we may have to
763 emit an empty label for an FDE that wouldn't otherwise be emitted. We
764 want to avoid having an FDE kept around when the function it refers to
765 is discarded. Example where this matters: a primary function template
766 in C++ requires EH information, an explicit specialization doesn't. */
767 if (for_eh)
769 bool any_eh_needed = false;
771 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
773 if (fde->uses_eh_lsda)
774 any_eh_needed = any_lsda_needed = true;
775 else if (fde_needed_for_eh_p (fde))
776 any_eh_needed = true;
777 else if (TARGET_USES_WEAK_UNWIND_INFO)
778 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
781 if (!any_eh_needed)
782 return;
785 /* We're going to be generating comments, so turn on app. */
786 if (flag_debug_asm)
787 app_enable ();
789 /* Switch to the proper frame section, first time. */
790 switch_to_frame_table_section (for_eh, false);
792 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
793 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
795 /* Output the CIE. */
796 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
797 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
798 if (!XCOFF_DEBUGGING_INFO || for_eh)
800 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
801 dw2_asm_output_data (4, 0xffffffff,
802 "Initial length escape value indicating 64-bit DWARF extension");
803 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
804 "Length of Common Information Entry");
806 ASM_OUTPUT_LABEL (asm_out_file, l1);
808 /* Now that the CIE pointer is PC-relative for EH,
809 use 0 to identify the CIE. */
810 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
811 (for_eh ? 0 : DWARF_CIE_ID),
812 "CIE Identifier Tag");
814 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
815 use CIE version 1, unless that would produce incorrect results
816 due to overflowing the return register column. */
817 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
818 dw_cie_version = 1;
819 if (return_reg >= 256 || dwarf_version > 2)
820 dw_cie_version = 3;
821 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
823 augmentation[0] = 0;
824 augmentation_size = 0;
826 personality = current_unit_personality;
827 if (for_eh)
829 char *p;
831 /* Augmentation:
832 z Indicates that a uleb128 is present to size the
833 augmentation section.
834 L Indicates the encoding (and thus presence) of
835 an LSDA pointer in the FDE augmentation.
836 R Indicates a non-default pointer encoding for
837 FDE code pointers.
838 P Indicates the presence of an encoding + language
839 personality routine in the CIE augmentation. */
841 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
842 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
843 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
845 p = augmentation + 1;
846 if (personality)
848 *p++ = 'P';
849 augmentation_size += 1 + size_of_encoded_value (per_encoding);
850 assemble_external_libcall (personality);
852 if (any_lsda_needed)
854 *p++ = 'L';
855 augmentation_size += 1;
857 if (fde_encoding != DW_EH_PE_absptr)
859 *p++ = 'R';
860 augmentation_size += 1;
862 if (p > augmentation + 1)
864 augmentation[0] = 'z';
865 *p = '\0';
868 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
869 if (personality && per_encoding == DW_EH_PE_aligned)
871 int offset = ( 4 /* Length */
872 + 4 /* CIE Id */
873 + 1 /* CIE version */
874 + strlen (augmentation) + 1 /* Augmentation */
875 + size_of_uleb128 (1) /* Code alignment */
876 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
877 + 1 /* RA column */
878 + 1 /* Augmentation size */
879 + 1 /* Personality encoding */ );
880 int pad = -offset & (PTR_SIZE - 1);
882 augmentation_size += pad;
884 /* Augmentations should be small, so there's scarce need to
885 iterate for a solution. Die if we exceed one uleb128 byte. */
886 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
890 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
891 if (dw_cie_version >= 4)
893 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
894 dw2_asm_output_data (1, 0, "CIE Segment Size");
896 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
897 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
898 "CIE Data Alignment Factor");
900 if (dw_cie_version == 1)
901 dw2_asm_output_data (1, return_reg, "CIE RA Column");
902 else
903 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
905 if (augmentation[0])
907 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
908 if (personality)
910 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
911 eh_data_format_name (per_encoding));
912 dw2_asm_output_encoded_addr_rtx (per_encoding,
913 personality,
914 true, NULL);
917 if (any_lsda_needed)
918 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
919 eh_data_format_name (lsda_encoding));
921 if (fde_encoding != DW_EH_PE_absptr)
922 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
923 eh_data_format_name (fde_encoding));
926 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
927 output_cfi (cfi, NULL, for_eh);
929 /* Pad the CIE out to an address sized boundary. */
930 ASM_OUTPUT_ALIGN (asm_out_file,
931 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
932 ASM_OUTPUT_LABEL (asm_out_file, l2);
934 /* Loop through all of the FDE's. */
935 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
937 unsigned int k;
939 /* Don't emit EH unwind info for leaf functions that don't need it. */
940 if (for_eh && !fde_needed_for_eh_p (fde))
941 continue;
943 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
944 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
945 augmentation, any_lsda_needed, lsda_encoding);
948 if (for_eh && targetm.terminate_dw2_eh_frame_info)
949 dw2_asm_output_data (4, 0, "End of Table");
951 /* Turn off app to make assembly quicker. */
952 if (flag_debug_asm)
953 app_disable ();
956 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
958 static void
959 dwarf2out_do_cfi_startproc (bool second)
961 int enc;
962 rtx ref;
963 rtx personality = get_personality_function (current_function_decl);
965 fprintf (asm_out_file, "\t.cfi_startproc\n");
967 if (personality)
969 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
970 ref = personality;
972 /* ??? The GAS support isn't entirely consistent. We have to
973 handle indirect support ourselves, but PC-relative is done
974 in the assembler. Further, the assembler can't handle any
975 of the weirder relocation types. */
976 if (enc & DW_EH_PE_indirect)
977 ref = dw2_force_const_mem (ref, true);
979 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
980 output_addr_const (asm_out_file, ref);
981 fputc ('\n', asm_out_file);
984 if (crtl->uses_eh_lsda)
986 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
988 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
989 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
990 current_function_funcdef_no);
991 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
992 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
994 if (enc & DW_EH_PE_indirect)
995 ref = dw2_force_const_mem (ref, true);
997 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
998 output_addr_const (asm_out_file, ref);
999 fputc ('\n', asm_out_file);
1003 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1004 this allocation may be done before pass_final. */
1006 dw_fde_ref
1007 dwarf2out_alloc_current_fde (void)
1009 dw_fde_ref fde;
1011 fde = ggc_cleared_alloc<dw_fde_node> ();
1012 fde->decl = current_function_decl;
1013 fde->funcdef_number = current_function_funcdef_no;
1014 fde->fde_index = vec_safe_length (fde_vec);
1015 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1016 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1017 fde->nothrow = crtl->nothrow;
1018 fde->drap_reg = INVALID_REGNUM;
1019 fde->vdrap_reg = INVALID_REGNUM;
1021 /* Record the FDE associated with this function. */
1022 cfun->fde = fde;
1023 vec_safe_push (fde_vec, fde);
1025 return fde;
1028 /* Output a marker (i.e. a label) for the beginning of a function, before
1029 the prologue. */
1031 void
1032 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1033 unsigned int column ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, column, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 /* Describe an entry into the .debug_addr section. */
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1265 struct GTY((for_user)) addr_table_entry {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1274 GTY ((desc ("%1.kind"))) addr;
1277 /* Location lists are ranges + location descriptions for that range,
1278 so you can track variables that are in different places over
1279 their entire life. */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281 dw_loc_list_ref dw_loc_next;
1282 const char *begin; /* Label and addr_entry for start of range */
1283 addr_table_entry *begin_entry;
1284 const char *end; /* Label for end of range */
1285 char *ll_symbol; /* Label for beginning of location list.
1286 Only on head of list */
1287 const char *section; /* Section this loclist is relative to */
1288 dw_loc_descr_ref expr;
1289 hashval_t hash;
1290 /* True if all addresses in this and subsequent lists are known to be
1291 resolved. */
1292 bool resolved_addr;
1293 /* True if this list has been replaced by dw_loc_next. */
1294 bool replaced;
1295 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1296 section. */
1297 unsigned char emitted : 1;
1298 /* True if hash field is index rather than hash value. */
1299 unsigned char num_assigned : 1;
1300 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1301 unsigned char offset_emitted : 1;
1302 /* True if note_variable_value_in_expr has been called on it. */
1303 unsigned char noted_variable_value : 1;
1304 /* True if the range should be emitted even if begin and end
1305 are the same. */
1306 bool force;
1307 } dw_loc_list_node;
1309 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1310 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1312 /* Convert a DWARF stack opcode into its string name. */
1314 static const char *
1315 dwarf_stack_op_name (unsigned int op)
1317 const char *name = get_DW_OP_name (op);
1319 if (name != NULL)
1320 return name;
1322 return "OP_<unknown>";
1325 /* Return a pointer to a newly allocated location description. Location
1326 descriptions are simple expression terms that can be strung
1327 together to form more complicated location (address) descriptions. */
1329 static inline dw_loc_descr_ref
1330 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1331 unsigned HOST_WIDE_INT oprnd2)
1333 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1335 descr->dw_loc_opc = op;
1336 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1337 descr->dw_loc_oprnd1.val_entry = NULL;
1338 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1339 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1340 descr->dw_loc_oprnd2.val_entry = NULL;
1341 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1343 return descr;
1346 /* Return a pointer to a newly allocated location description for
1347 REG and OFFSET. */
1349 static inline dw_loc_descr_ref
1350 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1352 if (reg <= 31)
1353 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1354 offset, 0);
1355 else
1356 return new_loc_descr (DW_OP_bregx, reg, offset);
1359 /* Add a location description term to a location description expression. */
1361 static inline void
1362 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1364 dw_loc_descr_ref *d;
1366 /* Find the end of the chain. */
1367 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1370 *d = descr;
1373 /* Compare two location operands for exact equality. */
1375 static bool
1376 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1378 if (a->val_class != b->val_class)
1379 return false;
1380 switch (a->val_class)
1382 case dw_val_class_none:
1383 return true;
1384 case dw_val_class_addr:
1385 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1387 case dw_val_class_offset:
1388 case dw_val_class_unsigned_const:
1389 case dw_val_class_const:
1390 case dw_val_class_unsigned_const_implicit:
1391 case dw_val_class_const_implicit:
1392 case dw_val_class_range_list:
1393 /* These are all HOST_WIDE_INT, signed or unsigned. */
1394 return a->v.val_unsigned == b->v.val_unsigned;
1396 case dw_val_class_loc:
1397 return a->v.val_loc == b->v.val_loc;
1398 case dw_val_class_loc_list:
1399 return a->v.val_loc_list == b->v.val_loc_list;
1400 case dw_val_class_die_ref:
1401 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1402 case dw_val_class_fde_ref:
1403 return a->v.val_fde_index == b->v.val_fde_index;
1404 case dw_val_class_lbl_id:
1405 case dw_val_class_lineptr:
1406 case dw_val_class_macptr:
1407 case dw_val_class_loclistsptr:
1408 case dw_val_class_high_pc:
1409 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1410 case dw_val_class_str:
1411 return a->v.val_str == b->v.val_str;
1412 case dw_val_class_flag:
1413 return a->v.val_flag == b->v.val_flag;
1414 case dw_val_class_file:
1415 case dw_val_class_file_implicit:
1416 return a->v.val_file == b->v.val_file;
1417 case dw_val_class_decl_ref:
1418 return a->v.val_decl_ref == b->v.val_decl_ref;
1420 case dw_val_class_const_double:
1421 return (a->v.val_double.high == b->v.val_double.high
1422 && a->v.val_double.low == b->v.val_double.low);
1424 case dw_val_class_wide_int:
1425 return *a->v.val_wide == *b->v.val_wide;
1427 case dw_val_class_vec:
1429 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1430 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1432 return (a_len == b_len
1433 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1436 case dw_val_class_data8:
1437 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1439 case dw_val_class_vms_delta:
1440 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1441 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1443 case dw_val_class_discr_value:
1444 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1445 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1446 case dw_val_class_discr_list:
1447 /* It makes no sense comparing two discriminant value lists. */
1448 return false;
1450 gcc_unreachable ();
1453 /* Compare two location atoms for exact equality. */
1455 static bool
1456 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1458 if (a->dw_loc_opc != b->dw_loc_opc)
1459 return false;
1461 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1462 address size, but since we always allocate cleared storage it
1463 should be zero for other types of locations. */
1464 if (a->dtprel != b->dtprel)
1465 return false;
1467 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1468 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1471 /* Compare two complete location expressions for exact equality. */
1473 bool
1474 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1476 while (1)
1478 if (a == b)
1479 return true;
1480 if (a == NULL || b == NULL)
1481 return false;
1482 if (!loc_descr_equal_p_1 (a, b))
1483 return false;
1485 a = a->dw_loc_next;
1486 b = b->dw_loc_next;
1491 /* Add a constant OFFSET to a location expression. */
1493 static void
1494 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1496 dw_loc_descr_ref loc;
1497 HOST_WIDE_INT *p;
1499 gcc_assert (*list_head != NULL);
1501 if (!offset)
1502 return;
1504 /* Find the end of the chain. */
1505 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1508 p = NULL;
1509 if (loc->dw_loc_opc == DW_OP_fbreg
1510 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1511 p = &loc->dw_loc_oprnd1.v.val_int;
1512 else if (loc->dw_loc_opc == DW_OP_bregx)
1513 p = &loc->dw_loc_oprnd2.v.val_int;
1515 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1516 offset. Don't optimize if an signed integer overflow would happen. */
1517 if (p != NULL
1518 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1519 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1520 *p += offset;
1522 else if (offset > 0)
1523 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1525 else
1527 loc->dw_loc_next
1528 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1529 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1533 /* Add a constant OFFSET to a location list. */
1535 static void
1536 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1538 dw_loc_list_ref d;
1539 for (d = list_head; d != NULL; d = d->dw_loc_next)
1540 loc_descr_plus_const (&d->expr, offset);
1543 #define DWARF_REF_SIZE \
1544 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1546 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1547 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1548 DW_FORM_data16 with 128 bits. */
1549 #define DWARF_LARGEST_DATA_FORM_BITS \
1550 (dwarf_version >= 5 ? 128 : 64)
1552 /* Utility inline function for construction of ops that were GNU extension
1553 before DWARF 5. */
1554 static inline enum dwarf_location_atom
1555 dwarf_OP (enum dwarf_location_atom op)
1557 switch (op)
1559 case DW_OP_implicit_pointer:
1560 if (dwarf_version < 5)
1561 return DW_OP_GNU_implicit_pointer;
1562 break;
1564 case DW_OP_entry_value:
1565 if (dwarf_version < 5)
1566 return DW_OP_GNU_entry_value;
1567 break;
1569 case DW_OP_const_type:
1570 if (dwarf_version < 5)
1571 return DW_OP_GNU_const_type;
1572 break;
1574 case DW_OP_regval_type:
1575 if (dwarf_version < 5)
1576 return DW_OP_GNU_regval_type;
1577 break;
1579 case DW_OP_deref_type:
1580 if (dwarf_version < 5)
1581 return DW_OP_GNU_deref_type;
1582 break;
1584 case DW_OP_convert:
1585 if (dwarf_version < 5)
1586 return DW_OP_GNU_convert;
1587 break;
1589 case DW_OP_reinterpret:
1590 if (dwarf_version < 5)
1591 return DW_OP_GNU_reinterpret;
1592 break;
1594 default:
1595 break;
1597 return op;
1600 /* Similarly for attributes. */
1601 static inline enum dwarf_attribute
1602 dwarf_AT (enum dwarf_attribute at)
1604 switch (at)
1606 case DW_AT_call_return_pc:
1607 if (dwarf_version < 5)
1608 return DW_AT_low_pc;
1609 break;
1611 case DW_AT_call_tail_call:
1612 if (dwarf_version < 5)
1613 return DW_AT_GNU_tail_call;
1614 break;
1616 case DW_AT_call_origin:
1617 if (dwarf_version < 5)
1618 return DW_AT_abstract_origin;
1619 break;
1621 case DW_AT_call_target:
1622 if (dwarf_version < 5)
1623 return DW_AT_GNU_call_site_target;
1624 break;
1626 case DW_AT_call_target_clobbered:
1627 if (dwarf_version < 5)
1628 return DW_AT_GNU_call_site_target_clobbered;
1629 break;
1631 case DW_AT_call_parameter:
1632 if (dwarf_version < 5)
1633 return DW_AT_abstract_origin;
1634 break;
1636 case DW_AT_call_value:
1637 if (dwarf_version < 5)
1638 return DW_AT_GNU_call_site_value;
1639 break;
1641 case DW_AT_call_data_value:
1642 if (dwarf_version < 5)
1643 return DW_AT_GNU_call_site_data_value;
1644 break;
1646 case DW_AT_call_all_calls:
1647 if (dwarf_version < 5)
1648 return DW_AT_GNU_all_call_sites;
1649 break;
1651 case DW_AT_call_all_tail_calls:
1652 if (dwarf_version < 5)
1653 return DW_AT_GNU_all_tail_call_sites;
1654 break;
1656 case DW_AT_dwo_name:
1657 if (dwarf_version < 5)
1658 return DW_AT_GNU_dwo_name;
1659 break;
1661 default:
1662 break;
1664 return at;
1667 /* And similarly for tags. */
1668 static inline enum dwarf_tag
1669 dwarf_TAG (enum dwarf_tag tag)
1671 switch (tag)
1673 case DW_TAG_call_site:
1674 if (dwarf_version < 5)
1675 return DW_TAG_GNU_call_site;
1676 break;
1678 case DW_TAG_call_site_parameter:
1679 if (dwarf_version < 5)
1680 return DW_TAG_GNU_call_site_parameter;
1681 break;
1683 default:
1684 break;
1686 return tag;
1689 static unsigned long int get_base_type_offset (dw_die_ref);
1691 /* Return the size of a location descriptor. */
1693 static unsigned long
1694 size_of_loc_descr (dw_loc_descr_ref loc)
1696 unsigned long size = 1;
1698 switch (loc->dw_loc_opc)
1700 case DW_OP_addr:
1701 size += DWARF2_ADDR_SIZE;
1702 break;
1703 case DW_OP_GNU_addr_index:
1704 case DW_OP_GNU_const_index:
1705 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1706 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1707 break;
1708 case DW_OP_const1u:
1709 case DW_OP_const1s:
1710 size += 1;
1711 break;
1712 case DW_OP_const2u:
1713 case DW_OP_const2s:
1714 size += 2;
1715 break;
1716 case DW_OP_const4u:
1717 case DW_OP_const4s:
1718 size += 4;
1719 break;
1720 case DW_OP_const8u:
1721 case DW_OP_const8s:
1722 size += 8;
1723 break;
1724 case DW_OP_constu:
1725 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1726 break;
1727 case DW_OP_consts:
1728 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1729 break;
1730 case DW_OP_pick:
1731 size += 1;
1732 break;
1733 case DW_OP_plus_uconst:
1734 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1735 break;
1736 case DW_OP_skip:
1737 case DW_OP_bra:
1738 size += 2;
1739 break;
1740 case DW_OP_breg0:
1741 case DW_OP_breg1:
1742 case DW_OP_breg2:
1743 case DW_OP_breg3:
1744 case DW_OP_breg4:
1745 case DW_OP_breg5:
1746 case DW_OP_breg6:
1747 case DW_OP_breg7:
1748 case DW_OP_breg8:
1749 case DW_OP_breg9:
1750 case DW_OP_breg10:
1751 case DW_OP_breg11:
1752 case DW_OP_breg12:
1753 case DW_OP_breg13:
1754 case DW_OP_breg14:
1755 case DW_OP_breg15:
1756 case DW_OP_breg16:
1757 case DW_OP_breg17:
1758 case DW_OP_breg18:
1759 case DW_OP_breg19:
1760 case DW_OP_breg20:
1761 case DW_OP_breg21:
1762 case DW_OP_breg22:
1763 case DW_OP_breg23:
1764 case DW_OP_breg24:
1765 case DW_OP_breg25:
1766 case DW_OP_breg26:
1767 case DW_OP_breg27:
1768 case DW_OP_breg28:
1769 case DW_OP_breg29:
1770 case DW_OP_breg30:
1771 case DW_OP_breg31:
1772 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1773 break;
1774 case DW_OP_regx:
1775 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1776 break;
1777 case DW_OP_fbreg:
1778 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1779 break;
1780 case DW_OP_bregx:
1781 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1782 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1783 break;
1784 case DW_OP_piece:
1785 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1786 break;
1787 case DW_OP_bit_piece:
1788 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1789 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1790 break;
1791 case DW_OP_deref_size:
1792 case DW_OP_xderef_size:
1793 size += 1;
1794 break;
1795 case DW_OP_call2:
1796 size += 2;
1797 break;
1798 case DW_OP_call4:
1799 size += 4;
1800 break;
1801 case DW_OP_call_ref:
1802 case DW_OP_GNU_variable_value:
1803 size += DWARF_REF_SIZE;
1804 break;
1805 case DW_OP_implicit_value:
1806 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1807 + loc->dw_loc_oprnd1.v.val_unsigned;
1808 break;
1809 case DW_OP_implicit_pointer:
1810 case DW_OP_GNU_implicit_pointer:
1811 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1812 break;
1813 case DW_OP_entry_value:
1814 case DW_OP_GNU_entry_value:
1816 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1817 size += size_of_uleb128 (op_size) + op_size;
1818 break;
1820 case DW_OP_const_type:
1821 case DW_OP_GNU_const_type:
1823 unsigned long o
1824 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1825 size += size_of_uleb128 (o) + 1;
1826 switch (loc->dw_loc_oprnd2.val_class)
1828 case dw_val_class_vec:
1829 size += loc->dw_loc_oprnd2.v.val_vec.length
1830 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1831 break;
1832 case dw_val_class_const:
1833 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1834 break;
1835 case dw_val_class_const_double:
1836 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1837 break;
1838 case dw_val_class_wide_int:
1839 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1840 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1841 break;
1842 default:
1843 gcc_unreachable ();
1845 break;
1847 case DW_OP_regval_type:
1848 case DW_OP_GNU_regval_type:
1850 unsigned long o
1851 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1852 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1853 + size_of_uleb128 (o);
1855 break;
1856 case DW_OP_deref_type:
1857 case DW_OP_GNU_deref_type:
1859 unsigned long o
1860 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1861 size += 1 + size_of_uleb128 (o);
1863 break;
1864 case DW_OP_convert:
1865 case DW_OP_reinterpret:
1866 case DW_OP_GNU_convert:
1867 case DW_OP_GNU_reinterpret:
1868 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1869 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1870 else
1872 unsigned long o
1873 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1874 size += size_of_uleb128 (o);
1876 break;
1877 case DW_OP_GNU_parameter_ref:
1878 size += 4;
1879 break;
1880 default:
1881 break;
1884 return size;
1887 /* Return the size of a series of location descriptors. */
1889 unsigned long
1890 size_of_locs (dw_loc_descr_ref loc)
1892 dw_loc_descr_ref l;
1893 unsigned long size;
1895 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1896 field, to avoid writing to a PCH file. */
1897 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1899 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1900 break;
1901 size += size_of_loc_descr (l);
1903 if (! l)
1904 return size;
1906 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1908 l->dw_loc_addr = size;
1909 size += size_of_loc_descr (l);
1912 return size;
1915 /* Return the size of the value in a DW_AT_discr_value attribute. */
1917 static int
1918 size_of_discr_value (dw_discr_value *discr_value)
1920 if (discr_value->pos)
1921 return size_of_uleb128 (discr_value->v.uval);
1922 else
1923 return size_of_sleb128 (discr_value->v.sval);
1926 /* Return the size of the value in a DW_AT_discr_list attribute. */
1928 static int
1929 size_of_discr_list (dw_discr_list_ref discr_list)
1931 int size = 0;
1933 for (dw_discr_list_ref list = discr_list;
1934 list != NULL;
1935 list = list->dw_discr_next)
1937 /* One byte for the discriminant value descriptor, and then one or two
1938 LEB128 numbers, depending on whether it's a single case label or a
1939 range label. */
1940 size += 1;
1941 size += size_of_discr_value (&list->dw_discr_lower_bound);
1942 if (list->dw_discr_range != 0)
1943 size += size_of_discr_value (&list->dw_discr_upper_bound);
1945 return size;
1948 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1949 static void get_ref_die_offset_label (char *, dw_die_ref);
1950 static unsigned long int get_ref_die_offset (dw_die_ref);
1952 /* Output location description stack opcode's operands (if any).
1953 The for_eh_or_skip parameter controls whether register numbers are
1954 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1955 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1956 info). This should be suppressed for the cases that have not been converted
1957 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1959 static void
1960 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1962 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1963 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1965 switch (loc->dw_loc_opc)
1967 #ifdef DWARF2_DEBUGGING_INFO
1968 case DW_OP_const2u:
1969 case DW_OP_const2s:
1970 dw2_asm_output_data (2, val1->v.val_int, NULL);
1971 break;
1972 case DW_OP_const4u:
1973 if (loc->dtprel)
1975 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1976 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1977 val1->v.val_addr);
1978 fputc ('\n', asm_out_file);
1979 break;
1981 /* FALLTHRU */
1982 case DW_OP_const4s:
1983 dw2_asm_output_data (4, val1->v.val_int, NULL);
1984 break;
1985 case DW_OP_const8u:
1986 if (loc->dtprel)
1988 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1989 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1990 val1->v.val_addr);
1991 fputc ('\n', asm_out_file);
1992 break;
1994 /* FALLTHRU */
1995 case DW_OP_const8s:
1996 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1997 dw2_asm_output_data (8, val1->v.val_int, NULL);
1998 break;
1999 case DW_OP_skip:
2000 case DW_OP_bra:
2002 int offset;
2004 gcc_assert (val1->val_class == dw_val_class_loc);
2005 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2007 dw2_asm_output_data (2, offset, NULL);
2009 break;
2010 case DW_OP_implicit_value:
2011 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2012 switch (val2->val_class)
2014 case dw_val_class_const:
2015 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2016 break;
2017 case dw_val_class_vec:
2019 unsigned int elt_size = val2->v.val_vec.elt_size;
2020 unsigned int len = val2->v.val_vec.length;
2021 unsigned int i;
2022 unsigned char *p;
2024 if (elt_size > sizeof (HOST_WIDE_INT))
2026 elt_size /= 2;
2027 len *= 2;
2029 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2030 i < len;
2031 i++, p += elt_size)
2032 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2033 "fp or vector constant word %u", i);
2035 break;
2036 case dw_val_class_const_double:
2038 unsigned HOST_WIDE_INT first, second;
2040 if (WORDS_BIG_ENDIAN)
2042 first = val2->v.val_double.high;
2043 second = val2->v.val_double.low;
2045 else
2047 first = val2->v.val_double.low;
2048 second = val2->v.val_double.high;
2050 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2051 first, NULL);
2052 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2053 second, NULL);
2055 break;
2056 case dw_val_class_wide_int:
2058 int i;
2059 int len = get_full_len (*val2->v.val_wide);
2060 if (WORDS_BIG_ENDIAN)
2061 for (i = len - 1; i >= 0; --i)
2062 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2063 val2->v.val_wide->elt (i), NULL);
2064 else
2065 for (i = 0; i < len; ++i)
2066 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2067 val2->v.val_wide->elt (i), NULL);
2069 break;
2070 case dw_val_class_addr:
2071 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2072 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2073 break;
2074 default:
2075 gcc_unreachable ();
2077 break;
2078 #else
2079 case DW_OP_const2u:
2080 case DW_OP_const2s:
2081 case DW_OP_const4u:
2082 case DW_OP_const4s:
2083 case DW_OP_const8u:
2084 case DW_OP_const8s:
2085 case DW_OP_skip:
2086 case DW_OP_bra:
2087 case DW_OP_implicit_value:
2088 /* We currently don't make any attempt to make sure these are
2089 aligned properly like we do for the main unwind info, so
2090 don't support emitting things larger than a byte if we're
2091 only doing unwinding. */
2092 gcc_unreachable ();
2093 #endif
2094 case DW_OP_const1u:
2095 case DW_OP_const1s:
2096 dw2_asm_output_data (1, val1->v.val_int, NULL);
2097 break;
2098 case DW_OP_constu:
2099 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2100 break;
2101 case DW_OP_consts:
2102 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2103 break;
2104 case DW_OP_pick:
2105 dw2_asm_output_data (1, val1->v.val_int, NULL);
2106 break;
2107 case DW_OP_plus_uconst:
2108 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2109 break;
2110 case DW_OP_breg0:
2111 case DW_OP_breg1:
2112 case DW_OP_breg2:
2113 case DW_OP_breg3:
2114 case DW_OP_breg4:
2115 case DW_OP_breg5:
2116 case DW_OP_breg6:
2117 case DW_OP_breg7:
2118 case DW_OP_breg8:
2119 case DW_OP_breg9:
2120 case DW_OP_breg10:
2121 case DW_OP_breg11:
2122 case DW_OP_breg12:
2123 case DW_OP_breg13:
2124 case DW_OP_breg14:
2125 case DW_OP_breg15:
2126 case DW_OP_breg16:
2127 case DW_OP_breg17:
2128 case DW_OP_breg18:
2129 case DW_OP_breg19:
2130 case DW_OP_breg20:
2131 case DW_OP_breg21:
2132 case DW_OP_breg22:
2133 case DW_OP_breg23:
2134 case DW_OP_breg24:
2135 case DW_OP_breg25:
2136 case DW_OP_breg26:
2137 case DW_OP_breg27:
2138 case DW_OP_breg28:
2139 case DW_OP_breg29:
2140 case DW_OP_breg30:
2141 case DW_OP_breg31:
2142 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2143 break;
2144 case DW_OP_regx:
2146 unsigned r = val1->v.val_unsigned;
2147 if (for_eh_or_skip >= 0)
2148 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2149 gcc_assert (size_of_uleb128 (r)
2150 == size_of_uleb128 (val1->v.val_unsigned));
2151 dw2_asm_output_data_uleb128 (r, NULL);
2153 break;
2154 case DW_OP_fbreg:
2155 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2156 break;
2157 case DW_OP_bregx:
2159 unsigned r = val1->v.val_unsigned;
2160 if (for_eh_or_skip >= 0)
2161 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162 gcc_assert (size_of_uleb128 (r)
2163 == size_of_uleb128 (val1->v.val_unsigned));
2164 dw2_asm_output_data_uleb128 (r, NULL);
2165 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2167 break;
2168 case DW_OP_piece:
2169 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2170 break;
2171 case DW_OP_bit_piece:
2172 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2173 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2174 break;
2175 case DW_OP_deref_size:
2176 case DW_OP_xderef_size:
2177 dw2_asm_output_data (1, val1->v.val_int, NULL);
2178 break;
2180 case DW_OP_addr:
2181 if (loc->dtprel)
2183 if (targetm.asm_out.output_dwarf_dtprel)
2185 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2186 DWARF2_ADDR_SIZE,
2187 val1->v.val_addr);
2188 fputc ('\n', asm_out_file);
2190 else
2191 gcc_unreachable ();
2193 else
2195 #ifdef DWARF2_DEBUGGING_INFO
2196 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2197 #else
2198 gcc_unreachable ();
2199 #endif
2201 break;
2203 case DW_OP_GNU_addr_index:
2204 case DW_OP_GNU_const_index:
2205 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2206 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2207 "(index into .debug_addr)");
2208 break;
2210 case DW_OP_call2:
2211 case DW_OP_call4:
2213 unsigned long die_offset
2214 = get_ref_die_offset (val1->v.val_die_ref.die);
2215 /* Make sure the offset has been computed and that we can encode it as
2216 an operand. */
2217 gcc_assert (die_offset > 0
2218 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2219 ? 0xffff
2220 : 0xffffffff));
2221 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2222 die_offset, NULL);
2224 break;
2226 case DW_OP_call_ref:
2227 case DW_OP_GNU_variable_value:
2229 char label[MAX_ARTIFICIAL_LABEL_BYTES
2230 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2231 gcc_assert (val1->val_class == dw_val_class_die_ref);
2232 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2233 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2235 break;
2237 case DW_OP_implicit_pointer:
2238 case DW_OP_GNU_implicit_pointer:
2240 char label[MAX_ARTIFICIAL_LABEL_BYTES
2241 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2242 gcc_assert (val1->val_class == dw_val_class_die_ref);
2243 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2244 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2245 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2247 break;
2249 case DW_OP_entry_value:
2250 case DW_OP_GNU_entry_value:
2251 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2252 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2253 break;
2255 case DW_OP_const_type:
2256 case DW_OP_GNU_const_type:
2258 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2259 gcc_assert (o);
2260 dw2_asm_output_data_uleb128 (o, NULL);
2261 switch (val2->val_class)
2263 case dw_val_class_const:
2264 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2265 dw2_asm_output_data (1, l, NULL);
2266 dw2_asm_output_data (l, val2->v.val_int, NULL);
2267 break;
2268 case dw_val_class_vec:
2270 unsigned int elt_size = val2->v.val_vec.elt_size;
2271 unsigned int len = val2->v.val_vec.length;
2272 unsigned int i;
2273 unsigned char *p;
2275 l = len * elt_size;
2276 dw2_asm_output_data (1, l, NULL);
2277 if (elt_size > sizeof (HOST_WIDE_INT))
2279 elt_size /= 2;
2280 len *= 2;
2282 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2283 i < len;
2284 i++, p += elt_size)
2285 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2286 "fp or vector constant word %u", i);
2288 break;
2289 case dw_val_class_const_double:
2291 unsigned HOST_WIDE_INT first, second;
2292 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2294 dw2_asm_output_data (1, 2 * l, NULL);
2295 if (WORDS_BIG_ENDIAN)
2297 first = val2->v.val_double.high;
2298 second = val2->v.val_double.low;
2300 else
2302 first = val2->v.val_double.low;
2303 second = val2->v.val_double.high;
2305 dw2_asm_output_data (l, first, NULL);
2306 dw2_asm_output_data (l, second, NULL);
2308 break;
2309 case dw_val_class_wide_int:
2311 int i;
2312 int len = get_full_len (*val2->v.val_wide);
2313 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2315 dw2_asm_output_data (1, len * l, NULL);
2316 if (WORDS_BIG_ENDIAN)
2317 for (i = len - 1; i >= 0; --i)
2318 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2319 else
2320 for (i = 0; i < len; ++i)
2321 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2323 break;
2324 default:
2325 gcc_unreachable ();
2328 break;
2329 case DW_OP_regval_type:
2330 case DW_OP_GNU_regval_type:
2332 unsigned r = val1->v.val_unsigned;
2333 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2334 gcc_assert (o);
2335 if (for_eh_or_skip >= 0)
2337 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2338 gcc_assert (size_of_uleb128 (r)
2339 == size_of_uleb128 (val1->v.val_unsigned));
2341 dw2_asm_output_data_uleb128 (r, NULL);
2342 dw2_asm_output_data_uleb128 (o, NULL);
2344 break;
2345 case DW_OP_deref_type:
2346 case DW_OP_GNU_deref_type:
2348 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2349 gcc_assert (o);
2350 dw2_asm_output_data (1, val1->v.val_int, NULL);
2351 dw2_asm_output_data_uleb128 (o, NULL);
2353 break;
2354 case DW_OP_convert:
2355 case DW_OP_reinterpret:
2356 case DW_OP_GNU_convert:
2357 case DW_OP_GNU_reinterpret:
2358 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2359 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2360 else
2362 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2363 gcc_assert (o);
2364 dw2_asm_output_data_uleb128 (o, NULL);
2366 break;
2368 case DW_OP_GNU_parameter_ref:
2370 unsigned long o;
2371 gcc_assert (val1->val_class == dw_val_class_die_ref);
2372 o = get_ref_die_offset (val1->v.val_die_ref.die);
2373 dw2_asm_output_data (4, o, NULL);
2375 break;
2377 default:
2378 /* Other codes have no operands. */
2379 break;
2383 /* Output a sequence of location operations.
2384 The for_eh_or_skip parameter controls whether register numbers are
2385 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2386 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2387 info). This should be suppressed for the cases that have not been converted
2388 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2390 void
2391 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2393 for (; loc != NULL; loc = loc->dw_loc_next)
2395 enum dwarf_location_atom opc = loc->dw_loc_opc;
2396 /* Output the opcode. */
2397 if (for_eh_or_skip >= 0
2398 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2400 unsigned r = (opc - DW_OP_breg0);
2401 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2402 gcc_assert (r <= 31);
2403 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2405 else if (for_eh_or_skip >= 0
2406 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2408 unsigned r = (opc - DW_OP_reg0);
2409 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2410 gcc_assert (r <= 31);
2411 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2414 dw2_asm_output_data (1, opc,
2415 "%s", dwarf_stack_op_name (opc));
2417 /* Output the operand(s) (if any). */
2418 output_loc_operands (loc, for_eh_or_skip);
2422 /* Output location description stack opcode's operands (if any).
2423 The output is single bytes on a line, suitable for .cfi_escape. */
2425 static void
2426 output_loc_operands_raw (dw_loc_descr_ref loc)
2428 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2429 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2431 switch (loc->dw_loc_opc)
2433 case DW_OP_addr:
2434 case DW_OP_GNU_addr_index:
2435 case DW_OP_GNU_const_index:
2436 case DW_OP_implicit_value:
2437 /* We cannot output addresses in .cfi_escape, only bytes. */
2438 gcc_unreachable ();
2440 case DW_OP_const1u:
2441 case DW_OP_const1s:
2442 case DW_OP_pick:
2443 case DW_OP_deref_size:
2444 case DW_OP_xderef_size:
2445 fputc (',', asm_out_file);
2446 dw2_asm_output_data_raw (1, val1->v.val_int);
2447 break;
2449 case DW_OP_const2u:
2450 case DW_OP_const2s:
2451 fputc (',', asm_out_file);
2452 dw2_asm_output_data_raw (2, val1->v.val_int);
2453 break;
2455 case DW_OP_const4u:
2456 case DW_OP_const4s:
2457 fputc (',', asm_out_file);
2458 dw2_asm_output_data_raw (4, val1->v.val_int);
2459 break;
2461 case DW_OP_const8u:
2462 case DW_OP_const8s:
2463 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2464 fputc (',', asm_out_file);
2465 dw2_asm_output_data_raw (8, val1->v.val_int);
2466 break;
2468 case DW_OP_skip:
2469 case DW_OP_bra:
2471 int offset;
2473 gcc_assert (val1->val_class == dw_val_class_loc);
2474 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2476 fputc (',', asm_out_file);
2477 dw2_asm_output_data_raw (2, offset);
2479 break;
2481 case DW_OP_regx:
2483 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2484 gcc_assert (size_of_uleb128 (r)
2485 == size_of_uleb128 (val1->v.val_unsigned));
2486 fputc (',', asm_out_file);
2487 dw2_asm_output_data_uleb128_raw (r);
2489 break;
2491 case DW_OP_constu:
2492 case DW_OP_plus_uconst:
2493 case DW_OP_piece:
2494 fputc (',', asm_out_file);
2495 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2496 break;
2498 case DW_OP_bit_piece:
2499 fputc (',', asm_out_file);
2500 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2501 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2502 break;
2504 case DW_OP_consts:
2505 case DW_OP_breg0:
2506 case DW_OP_breg1:
2507 case DW_OP_breg2:
2508 case DW_OP_breg3:
2509 case DW_OP_breg4:
2510 case DW_OP_breg5:
2511 case DW_OP_breg6:
2512 case DW_OP_breg7:
2513 case DW_OP_breg8:
2514 case DW_OP_breg9:
2515 case DW_OP_breg10:
2516 case DW_OP_breg11:
2517 case DW_OP_breg12:
2518 case DW_OP_breg13:
2519 case DW_OP_breg14:
2520 case DW_OP_breg15:
2521 case DW_OP_breg16:
2522 case DW_OP_breg17:
2523 case DW_OP_breg18:
2524 case DW_OP_breg19:
2525 case DW_OP_breg20:
2526 case DW_OP_breg21:
2527 case DW_OP_breg22:
2528 case DW_OP_breg23:
2529 case DW_OP_breg24:
2530 case DW_OP_breg25:
2531 case DW_OP_breg26:
2532 case DW_OP_breg27:
2533 case DW_OP_breg28:
2534 case DW_OP_breg29:
2535 case DW_OP_breg30:
2536 case DW_OP_breg31:
2537 case DW_OP_fbreg:
2538 fputc (',', asm_out_file);
2539 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2540 break;
2542 case DW_OP_bregx:
2544 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2545 gcc_assert (size_of_uleb128 (r)
2546 == size_of_uleb128 (val1->v.val_unsigned));
2547 fputc (',', asm_out_file);
2548 dw2_asm_output_data_uleb128_raw (r);
2549 fputc (',', asm_out_file);
2550 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2552 break;
2554 case DW_OP_implicit_pointer:
2555 case DW_OP_entry_value:
2556 case DW_OP_const_type:
2557 case DW_OP_regval_type:
2558 case DW_OP_deref_type:
2559 case DW_OP_convert:
2560 case DW_OP_reinterpret:
2561 case DW_OP_GNU_implicit_pointer:
2562 case DW_OP_GNU_entry_value:
2563 case DW_OP_GNU_const_type:
2564 case DW_OP_GNU_regval_type:
2565 case DW_OP_GNU_deref_type:
2566 case DW_OP_GNU_convert:
2567 case DW_OP_GNU_reinterpret:
2568 case DW_OP_GNU_parameter_ref:
2569 gcc_unreachable ();
2570 break;
2572 default:
2573 /* Other codes have no operands. */
2574 break;
2578 void
2579 output_loc_sequence_raw (dw_loc_descr_ref loc)
2581 while (1)
2583 enum dwarf_location_atom opc = loc->dw_loc_opc;
2584 /* Output the opcode. */
2585 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2587 unsigned r = (opc - DW_OP_breg0);
2588 r = DWARF2_FRAME_REG_OUT (r, 1);
2589 gcc_assert (r <= 31);
2590 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2592 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2594 unsigned r = (opc - DW_OP_reg0);
2595 r = DWARF2_FRAME_REG_OUT (r, 1);
2596 gcc_assert (r <= 31);
2597 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2599 /* Output the opcode. */
2600 fprintf (asm_out_file, "%#x", opc);
2601 output_loc_operands_raw (loc);
2603 if (!loc->dw_loc_next)
2604 break;
2605 loc = loc->dw_loc_next;
2607 fputc (',', asm_out_file);
2611 /* This function builds a dwarf location descriptor sequence from a
2612 dw_cfa_location, adding the given OFFSET to the result of the
2613 expression. */
2615 struct dw_loc_descr_node *
2616 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2618 struct dw_loc_descr_node *head, *tmp;
2620 offset += cfa->offset;
2622 if (cfa->indirect)
2624 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2625 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2626 head->dw_loc_oprnd1.val_entry = NULL;
2627 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2628 add_loc_descr (&head, tmp);
2629 if (offset != 0)
2631 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2632 add_loc_descr (&head, tmp);
2635 else
2636 head = new_reg_loc_descr (cfa->reg, offset);
2638 return head;
2641 /* This function builds a dwarf location descriptor sequence for
2642 the address at OFFSET from the CFA when stack is aligned to
2643 ALIGNMENT byte. */
2645 struct dw_loc_descr_node *
2646 build_cfa_aligned_loc (dw_cfa_location *cfa,
2647 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2649 struct dw_loc_descr_node *head;
2650 unsigned int dwarf_fp
2651 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2653 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2654 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2656 head = new_reg_loc_descr (dwarf_fp, 0);
2657 add_loc_descr (&head, int_loc_descriptor (alignment));
2658 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2659 loc_descr_plus_const (&head, offset);
2661 else
2662 head = new_reg_loc_descr (dwarf_fp, offset);
2663 return head;
2666 /* And now, the support for symbolic debugging information. */
2668 /* .debug_str support. */
2670 static void dwarf2out_init (const char *);
2671 static void dwarf2out_finish (const char *);
2672 static void dwarf2out_early_finish (const char *);
2673 static void dwarf2out_assembly_start (void);
2674 static void dwarf2out_define (unsigned int, const char *);
2675 static void dwarf2out_undef (unsigned int, const char *);
2676 static void dwarf2out_start_source_file (unsigned, const char *);
2677 static void dwarf2out_end_source_file (unsigned);
2678 static void dwarf2out_function_decl (tree);
2679 static void dwarf2out_begin_block (unsigned, unsigned);
2680 static void dwarf2out_end_block (unsigned, unsigned);
2681 static bool dwarf2out_ignore_block (const_tree);
2682 static void dwarf2out_early_global_decl (tree);
2683 static void dwarf2out_late_global_decl (tree);
2684 static void dwarf2out_type_decl (tree, int);
2685 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2686 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2687 dw_die_ref);
2688 static void dwarf2out_abstract_function (tree);
2689 static void dwarf2out_var_location (rtx_insn *);
2690 static void dwarf2out_size_function (tree);
2691 static void dwarf2out_begin_function (tree);
2692 static void dwarf2out_end_function (unsigned int);
2693 static void dwarf2out_register_main_translation_unit (tree unit);
2694 static void dwarf2out_set_name (tree, tree);
2696 /* The debug hooks structure. */
2698 const struct gcc_debug_hooks dwarf2_debug_hooks =
2700 dwarf2out_init,
2701 dwarf2out_finish,
2702 dwarf2out_early_finish,
2703 dwarf2out_assembly_start,
2704 dwarf2out_define,
2705 dwarf2out_undef,
2706 dwarf2out_start_source_file,
2707 dwarf2out_end_source_file,
2708 dwarf2out_begin_block,
2709 dwarf2out_end_block,
2710 dwarf2out_ignore_block,
2711 dwarf2out_source_line,
2712 dwarf2out_begin_prologue,
2713 #if VMS_DEBUGGING_INFO
2714 dwarf2out_vms_end_prologue,
2715 dwarf2out_vms_begin_epilogue,
2716 #else
2717 debug_nothing_int_charstar,
2718 debug_nothing_int_charstar,
2719 #endif
2720 dwarf2out_end_epilogue,
2721 dwarf2out_begin_function,
2722 dwarf2out_end_function, /* end_function */
2723 dwarf2out_register_main_translation_unit,
2724 dwarf2out_function_decl, /* function_decl */
2725 dwarf2out_early_global_decl,
2726 dwarf2out_late_global_decl,
2727 dwarf2out_type_decl, /* type_decl */
2728 dwarf2out_imported_module_or_decl,
2729 debug_nothing_tree, /* deferred_inline_function */
2730 /* The DWARF 2 backend tries to reduce debugging bloat by not
2731 emitting the abstract description of inline functions until
2732 something tries to reference them. */
2733 dwarf2out_abstract_function, /* outlining_inline_function */
2734 debug_nothing_rtx_code_label, /* label */
2735 debug_nothing_int, /* handle_pch */
2736 dwarf2out_var_location,
2737 dwarf2out_size_function, /* size_function */
2738 dwarf2out_switch_text_section,
2739 dwarf2out_set_name,
2740 1, /* start_end_main_source_file */
2741 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2744 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2746 dwarf2out_init,
2747 debug_nothing_charstar,
2748 debug_nothing_charstar,
2749 dwarf2out_assembly_start,
2750 debug_nothing_int_charstar,
2751 debug_nothing_int_charstar,
2752 debug_nothing_int_charstar,
2753 debug_nothing_int,
2754 debug_nothing_int_int, /* begin_block */
2755 debug_nothing_int_int, /* end_block */
2756 debug_true_const_tree, /* ignore_block */
2757 dwarf2out_source_line, /* source_line */
2758 debug_nothing_int_int_charstar, /* begin_prologue */
2759 debug_nothing_int_charstar, /* end_prologue */
2760 debug_nothing_int_charstar, /* begin_epilogue */
2761 debug_nothing_int_charstar, /* end_epilogue */
2762 debug_nothing_tree, /* begin_function */
2763 debug_nothing_int, /* end_function */
2764 debug_nothing_tree, /* register_main_translation_unit */
2765 debug_nothing_tree, /* function_decl */
2766 debug_nothing_tree, /* early_global_decl */
2767 debug_nothing_tree, /* late_global_decl */
2768 debug_nothing_tree_int, /* type_decl */
2769 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2770 debug_nothing_tree, /* deferred_inline_function */
2771 debug_nothing_tree, /* outlining_inline_function */
2772 debug_nothing_rtx_code_label, /* label */
2773 debug_nothing_int, /* handle_pch */
2774 debug_nothing_rtx_insn, /* var_location */
2775 debug_nothing_tree, /* size_function */
2776 debug_nothing_void, /* switch_text_section */
2777 debug_nothing_tree_tree, /* set_name */
2778 0, /* start_end_main_source_file */
2779 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2782 /* NOTE: In the comments in this file, many references are made to
2783 "Debugging Information Entries". This term is abbreviated as `DIE'
2784 throughout the remainder of this file. */
2786 /* An internal representation of the DWARF output is built, and then
2787 walked to generate the DWARF debugging info. The walk of the internal
2788 representation is done after the entire program has been compiled.
2789 The types below are used to describe the internal representation. */
2791 /* Whether to put type DIEs into their own section .debug_types instead
2792 of making them part of the .debug_info section. Only supported for
2793 Dwarf V4 or higher and the user didn't disable them through
2794 -fno-debug-types-section. It is more efficient to put them in a
2795 separate comdat sections since the linker will then be able to
2796 remove duplicates. But not all tools support .debug_types sections
2797 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2798 it is DW_UT_type unit type in .debug_info section. */
2800 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2802 /* Various DIE's use offsets relative to the beginning of the
2803 .debug_info section to refer to each other. */
2805 typedef long int dw_offset;
2807 struct comdat_type_node;
2809 /* The entries in the line_info table more-or-less mirror the opcodes
2810 that are used in the real dwarf line table. Arrays of these entries
2811 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2812 supported. */
2814 enum dw_line_info_opcode {
2815 /* Emit DW_LNE_set_address; the operand is the label index. */
2816 LI_set_address,
2818 /* Emit a row to the matrix with the given line. This may be done
2819 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2820 special opcodes. */
2821 LI_set_line,
2823 /* Emit a DW_LNS_set_file. */
2824 LI_set_file,
2826 /* Emit a DW_LNS_set_column. */
2827 LI_set_column,
2829 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2830 LI_negate_stmt,
2832 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2833 LI_set_prologue_end,
2834 LI_set_epilogue_begin,
2836 /* Emit a DW_LNE_set_discriminator. */
2837 LI_set_discriminator
2840 typedef struct GTY(()) dw_line_info_struct {
2841 enum dw_line_info_opcode opcode;
2842 unsigned int val;
2843 } dw_line_info_entry;
2846 struct GTY(()) dw_line_info_table {
2847 /* The label that marks the end of this section. */
2848 const char *end_label;
2850 /* The values for the last row of the matrix, as collected in the table.
2851 These are used to minimize the changes to the next row. */
2852 unsigned int file_num;
2853 unsigned int line_num;
2854 unsigned int column_num;
2855 int discrim_num;
2856 bool is_stmt;
2857 bool in_use;
2859 vec<dw_line_info_entry, va_gc> *entries;
2863 /* Each DIE attribute has a field specifying the attribute kind,
2864 a link to the next attribute in the chain, and an attribute value.
2865 Attributes are typically linked below the DIE they modify. */
2867 typedef struct GTY(()) dw_attr_struct {
2868 enum dwarf_attribute dw_attr;
2869 dw_val_node dw_attr_val;
2871 dw_attr_node;
2874 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2875 The children of each node form a circular list linked by
2876 die_sib. die_child points to the node *before* the "first" child node. */
2878 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2879 union die_symbol_or_type_node
2881 const char * GTY ((tag ("0"))) die_symbol;
2882 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2884 GTY ((desc ("%0.comdat_type_p"))) die_id;
2885 vec<dw_attr_node, va_gc> *die_attr;
2886 dw_die_ref die_parent;
2887 dw_die_ref die_child;
2888 dw_die_ref die_sib;
2889 dw_die_ref die_definition; /* ref from a specification to its definition */
2890 dw_offset die_offset;
2891 unsigned long die_abbrev;
2892 int die_mark;
2893 unsigned int decl_id;
2894 enum dwarf_tag die_tag;
2895 /* Die is used and must not be pruned as unused. */
2896 BOOL_BITFIELD die_perennial_p : 1;
2897 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2898 /* Whether this DIE was removed from the DIE tree, for example via
2899 prune_unused_types. We don't consider those present from the
2900 DIE lookup routines. */
2901 BOOL_BITFIELD removed : 1;
2902 /* Lots of spare bits. */
2904 die_node;
2906 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2907 static bool early_dwarf;
2908 static bool early_dwarf_finished;
2909 struct set_early_dwarf {
2910 bool saved;
2911 set_early_dwarf () : saved(early_dwarf)
2913 gcc_assert (! early_dwarf_finished);
2914 early_dwarf = true;
2916 ~set_early_dwarf () { early_dwarf = saved; }
2919 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2920 #define FOR_EACH_CHILD(die, c, expr) do { \
2921 c = die->die_child; \
2922 if (c) do { \
2923 c = c->die_sib; \
2924 expr; \
2925 } while (c != die->die_child); \
2926 } while (0)
2928 /* The pubname structure */
2930 typedef struct GTY(()) pubname_struct {
2931 dw_die_ref die;
2932 const char *name;
2934 pubname_entry;
2937 struct GTY(()) dw_ranges {
2938 const char *label;
2939 /* If this is positive, it's a block number, otherwise it's a
2940 bitwise-negated index into dw_ranges_by_label. */
2941 int num;
2942 /* Index for the range list for DW_FORM_rnglistx. */
2943 unsigned int idx : 31;
2944 /* True if this range might be possibly in a different section
2945 from previous entry. */
2946 unsigned int maybe_new_sec : 1;
2949 /* A structure to hold a macinfo entry. */
2951 typedef struct GTY(()) macinfo_struct {
2952 unsigned char code;
2953 unsigned HOST_WIDE_INT lineno;
2954 const char *info;
2956 macinfo_entry;
2959 struct GTY(()) dw_ranges_by_label {
2960 const char *begin;
2961 const char *end;
2964 /* The comdat type node structure. */
2965 struct GTY(()) comdat_type_node
2967 dw_die_ref root_die;
2968 dw_die_ref type_die;
2969 dw_die_ref skeleton_die;
2970 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2971 comdat_type_node *next;
2974 /* A list of DIEs for which we can't determine ancestry (parent_die
2975 field) just yet. Later in dwarf2out_finish we will fill in the
2976 missing bits. */
2977 typedef struct GTY(()) limbo_die_struct {
2978 dw_die_ref die;
2979 /* The tree for which this DIE was created. We use this to
2980 determine ancestry later. */
2981 tree created_for;
2982 struct limbo_die_struct *next;
2984 limbo_die_node;
2986 typedef struct skeleton_chain_struct
2988 dw_die_ref old_die;
2989 dw_die_ref new_die;
2990 struct skeleton_chain_struct *parent;
2992 skeleton_chain_node;
2994 /* Define a macro which returns nonzero for a TYPE_DECL which was
2995 implicitly generated for a type.
2997 Note that, unlike the C front-end (which generates a NULL named
2998 TYPE_DECL node for each complete tagged type, each array type,
2999 and each function type node created) the C++ front-end generates
3000 a _named_ TYPE_DECL node for each tagged type node created.
3001 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3002 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3003 front-end, but for each type, tagged or not. */
3005 #define TYPE_DECL_IS_STUB(decl) \
3006 (DECL_NAME (decl) == NULL_TREE \
3007 || (DECL_ARTIFICIAL (decl) \
3008 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3009 /* This is necessary for stub decls that \
3010 appear in nested inline functions. */ \
3011 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3012 && (decl_ultimate_origin (decl) \
3013 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3015 /* Information concerning the compilation unit's programming
3016 language, and compiler version. */
3018 /* Fixed size portion of the DWARF compilation unit header. */
3019 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3020 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3021 + (dwarf_version >= 5 ? 4 : 3))
3023 /* Fixed size portion of the DWARF comdat type unit header. */
3024 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3025 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3026 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3028 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3029 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3030 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3032 /* Fixed size portion of public names info. */
3033 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3035 /* Fixed size portion of the address range info. */
3036 #define DWARF_ARANGES_HEADER_SIZE \
3037 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3038 DWARF2_ADDR_SIZE * 2) \
3039 - DWARF_INITIAL_LENGTH_SIZE)
3041 /* Size of padding portion in the address range info. It must be
3042 aligned to twice the pointer size. */
3043 #define DWARF_ARANGES_PAD_SIZE \
3044 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3045 DWARF2_ADDR_SIZE * 2) \
3046 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3048 /* Use assembler line directives if available. */
3049 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3050 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3051 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3052 #else
3053 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3054 #endif
3055 #endif
3057 /* Minimum line offset in a special line info. opcode.
3058 This value was chosen to give a reasonable range of values. */
3059 #define DWARF_LINE_BASE -10
3061 /* First special line opcode - leave room for the standard opcodes. */
3062 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3064 /* Range of line offsets in a special line info. opcode. */
3065 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3067 /* Flag that indicates the initial value of the is_stmt_start flag.
3068 In the present implementation, we do not mark any lines as
3069 the beginning of a source statement, because that information
3070 is not made available by the GCC front-end. */
3071 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3073 /* Maximum number of operations per instruction bundle. */
3074 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3075 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3076 #endif
3078 /* This location is used by calc_die_sizes() to keep track
3079 the offset of each DIE within the .debug_info section. */
3080 static unsigned long next_die_offset;
3082 /* Record the root of the DIE's built for the current compilation unit. */
3083 static GTY(()) dw_die_ref single_comp_unit_die;
3085 /* A list of type DIEs that have been separated into comdat sections. */
3086 static GTY(()) comdat_type_node *comdat_type_list;
3088 /* A list of CU DIEs that have been separated. */
3089 static GTY(()) limbo_die_node *cu_die_list;
3091 /* A list of DIEs with a NULL parent waiting to be relocated. */
3092 static GTY(()) limbo_die_node *limbo_die_list;
3094 /* A list of DIEs for which we may have to generate
3095 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3096 static GTY(()) limbo_die_node *deferred_asm_name;
3098 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3100 typedef const char *compare_type;
3102 static hashval_t hash (dwarf_file_data *);
3103 static bool equal (dwarf_file_data *, const char *);
3106 /* Filenames referenced by this compilation unit. */
3107 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3109 struct decl_die_hasher : ggc_ptr_hash<die_node>
3111 typedef tree compare_type;
3113 static hashval_t hash (die_node *);
3114 static bool equal (die_node *, tree);
3116 /* A hash table of references to DIE's that describe declarations.
3117 The key is a DECL_UID() which is a unique number identifying each decl. */
3118 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3120 struct GTY ((for_user)) variable_value_struct {
3121 unsigned int decl_id;
3122 vec<dw_die_ref, va_gc> *dies;
3125 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3127 typedef tree compare_type;
3129 static hashval_t hash (variable_value_struct *);
3130 static bool equal (variable_value_struct *, tree);
3132 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3133 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3134 DECL_CONTEXT of the referenced VAR_DECLs. */
3135 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3137 struct block_die_hasher : ggc_ptr_hash<die_struct>
3139 static hashval_t hash (die_struct *);
3140 static bool equal (die_struct *, die_struct *);
3143 /* A hash table of references to DIE's that describe COMMON blocks.
3144 The key is DECL_UID() ^ die_parent. */
3145 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3147 typedef struct GTY(()) die_arg_entry_struct {
3148 dw_die_ref die;
3149 tree arg;
3150 } die_arg_entry;
3153 /* Node of the variable location list. */
3154 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3155 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3156 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3157 in mode of the EXPR_LIST node and first EXPR_LIST operand
3158 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3159 location or NULL for padding. For larger bitsizes,
3160 mode is 0 and first operand is a CONCAT with bitsize
3161 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3162 NULL as second operand. */
3163 rtx GTY (()) loc;
3164 const char * GTY (()) label;
3165 struct var_loc_node * GTY (()) next;
3168 /* Variable location list. */
3169 struct GTY ((for_user)) var_loc_list_def {
3170 struct var_loc_node * GTY (()) first;
3172 /* Pointer to the last but one or last element of the
3173 chained list. If the list is empty, both first and
3174 last are NULL, if the list contains just one node
3175 or the last node certainly is not redundant, it points
3176 to the last node, otherwise points to the last but one.
3177 Do not mark it for GC because it is marked through the chain. */
3178 struct var_loc_node * GTY ((skip ("%h"))) last;
3180 /* Pointer to the last element before section switch,
3181 if NULL, either sections weren't switched or first
3182 is after section switch. */
3183 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3185 /* DECL_UID of the variable decl. */
3186 unsigned int decl_id;
3188 typedef struct var_loc_list_def var_loc_list;
3190 /* Call argument location list. */
3191 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3192 rtx GTY (()) call_arg_loc_note;
3193 const char * GTY (()) label;
3194 tree GTY (()) block;
3195 bool tail_call_p;
3196 rtx GTY (()) symbol_ref;
3197 struct call_arg_loc_node * GTY (()) next;
3201 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3203 typedef const_tree compare_type;
3205 static hashval_t hash (var_loc_list *);
3206 static bool equal (var_loc_list *, const_tree);
3209 /* Table of decl location linked lists. */
3210 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3212 /* Head and tail of call_arg_loc chain. */
3213 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3214 static struct call_arg_loc_node *call_arg_loc_last;
3216 /* Number of call sites in the current function. */
3217 static int call_site_count = -1;
3218 /* Number of tail call sites in the current function. */
3219 static int tail_call_site_count = -1;
3221 /* A cached location list. */
3222 struct GTY ((for_user)) cached_dw_loc_list_def {
3223 /* The DECL_UID of the decl that this entry describes. */
3224 unsigned int decl_id;
3226 /* The cached location list. */
3227 dw_loc_list_ref loc_list;
3229 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3231 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3234 typedef const_tree compare_type;
3236 static hashval_t hash (cached_dw_loc_list *);
3237 static bool equal (cached_dw_loc_list *, const_tree);
3240 /* Table of cached location lists. */
3241 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3243 /* A vector of references to DIE's that are uniquely identified by their tag,
3244 presence/absence of children DIE's, and list of attribute/value pairs. */
3245 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3247 /* A hash map to remember the stack usage for DWARF procedures. The value
3248 stored is the stack size difference between before the DWARF procedure
3249 invokation and after it returned. In other words, for a DWARF procedure
3250 that consumes N stack slots and that pushes M ones, this stores M - N. */
3251 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3253 /* A global counter for generating labels for line number data. */
3254 static unsigned int line_info_label_num;
3256 /* The current table to which we should emit line number information
3257 for the current function. This will be set up at the beginning of
3258 assembly for the function. */
3259 static GTY(()) dw_line_info_table *cur_line_info_table;
3261 /* The two default tables of line number info. */
3262 static GTY(()) dw_line_info_table *text_section_line_info;
3263 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3265 /* The set of all non-default tables of line number info. */
3266 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3268 /* A flag to tell pubnames/types export if there is an info section to
3269 refer to. */
3270 static bool info_section_emitted;
3272 /* A pointer to the base of a table that contains a list of publicly
3273 accessible names. */
3274 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3276 /* A pointer to the base of a table that contains a list of publicly
3277 accessible types. */
3278 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3280 /* A pointer to the base of a table that contains a list of macro
3281 defines/undefines (and file start/end markers). */
3282 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3284 /* True if .debug_macinfo or .debug_macros section is going to be
3285 emitted. */
3286 #define have_macinfo \
3287 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3288 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3289 && !macinfo_table->is_empty ())
3291 /* Vector of dies for which we should generate .debug_ranges info. */
3292 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3294 /* Vector of pairs of labels referenced in ranges_table. */
3295 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3297 /* Whether we have location lists that need outputting */
3298 static GTY(()) bool have_location_lists;
3300 /* Unique label counter. */
3301 static GTY(()) unsigned int loclabel_num;
3303 /* Unique label counter for point-of-call tables. */
3304 static GTY(()) unsigned int poc_label_num;
3306 /* The last file entry emitted by maybe_emit_file(). */
3307 static GTY(()) struct dwarf_file_data * last_emitted_file;
3309 /* Number of internal labels generated by gen_internal_sym(). */
3310 static GTY(()) int label_num;
3312 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3314 /* Instances of generic types for which we need to generate debug
3315 info that describe their generic parameters and arguments. That
3316 generation needs to happen once all types are properly laid out so
3317 we do it at the end of compilation. */
3318 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3320 /* Offset from the "steady-state frame pointer" to the frame base,
3321 within the current function. */
3322 static HOST_WIDE_INT frame_pointer_fb_offset;
3323 static bool frame_pointer_fb_offset_valid;
3325 static vec<dw_die_ref> base_types;
3327 /* Flags to represent a set of attribute classes for attributes that represent
3328 a scalar value (bounds, pointers, ...). */
3329 enum dw_scalar_form
3331 dw_scalar_form_constant = 0x01,
3332 dw_scalar_form_exprloc = 0x02,
3333 dw_scalar_form_reference = 0x04
3336 /* Forward declarations for functions defined in this file. */
3338 static int is_pseudo_reg (const_rtx);
3339 static tree type_main_variant (tree);
3340 static int is_tagged_type (const_tree);
3341 static const char *dwarf_tag_name (unsigned);
3342 static const char *dwarf_attr_name (unsigned);
3343 static const char *dwarf_form_name (unsigned);
3344 static tree decl_ultimate_origin (const_tree);
3345 static tree decl_class_context (tree);
3346 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3347 static inline enum dw_val_class AT_class (dw_attr_node *);
3348 static inline unsigned int AT_index (dw_attr_node *);
3349 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3350 static inline unsigned AT_flag (dw_attr_node *);
3351 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3352 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3353 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3354 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3355 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3356 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3357 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3358 unsigned int, unsigned char *);
3359 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3360 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3361 static inline const char *AT_string (dw_attr_node *);
3362 static enum dwarf_form AT_string_form (dw_attr_node *);
3363 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3364 static void add_AT_specification (dw_die_ref, dw_die_ref);
3365 static inline dw_die_ref AT_ref (dw_attr_node *);
3366 static inline int AT_ref_external (dw_attr_node *);
3367 static inline void set_AT_ref_external (dw_attr_node *, int);
3368 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3369 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3370 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3371 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3372 dw_loc_list_ref);
3373 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3374 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3375 static void remove_addr_table_entry (addr_table_entry *);
3376 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3377 static inline rtx AT_addr (dw_attr_node *);
3378 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3379 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3380 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3381 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3382 const char *);
3383 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3384 unsigned HOST_WIDE_INT);
3385 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3386 unsigned long, bool);
3387 static inline const char *AT_lbl (dw_attr_node *);
3388 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3389 static const char *get_AT_low_pc (dw_die_ref);
3390 static const char *get_AT_hi_pc (dw_die_ref);
3391 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3392 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3393 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3394 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3395 static bool is_cxx (void);
3396 static bool is_cxx (const_tree);
3397 static bool is_fortran (void);
3398 static bool is_ada (void);
3399 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3400 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3401 static void add_child_die (dw_die_ref, dw_die_ref);
3402 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3403 static dw_die_ref lookup_type_die (tree);
3404 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3405 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3406 static void equate_type_number_to_die (tree, dw_die_ref);
3407 static dw_die_ref lookup_decl_die (tree);
3408 static var_loc_list *lookup_decl_loc (const_tree);
3409 static void equate_decl_number_to_die (tree, dw_die_ref);
3410 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3411 static void print_spaces (FILE *);
3412 static void print_die (dw_die_ref, FILE *);
3413 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3414 static dw_die_ref pop_compile_unit (dw_die_ref);
3415 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3416 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3417 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3418 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3419 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3420 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3421 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3422 struct md5_ctx *, int *);
3423 struct checksum_attributes;
3424 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3425 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3426 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3427 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3428 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3429 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3430 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3431 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3432 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3433 static void compute_section_prefix (dw_die_ref);
3434 static int is_type_die (dw_die_ref);
3435 static int is_comdat_die (dw_die_ref);
3436 static int is_symbol_die (dw_die_ref);
3437 static inline bool is_template_instantiation (dw_die_ref);
3438 static void assign_symbol_names (dw_die_ref);
3439 static void break_out_includes (dw_die_ref);
3440 static int is_declaration_die (dw_die_ref);
3441 static int should_move_die_to_comdat (dw_die_ref);
3442 static dw_die_ref clone_as_declaration (dw_die_ref);
3443 static dw_die_ref clone_die (dw_die_ref);
3444 static dw_die_ref clone_tree (dw_die_ref);
3445 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3446 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3447 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3448 static dw_die_ref generate_skeleton (dw_die_ref);
3449 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3450 dw_die_ref,
3451 dw_die_ref);
3452 static void break_out_comdat_types (dw_die_ref);
3453 static void copy_decls_for_unworthy_types (dw_die_ref);
3455 static void add_sibling_attributes (dw_die_ref);
3456 static void output_location_lists (dw_die_ref);
3457 static int constant_size (unsigned HOST_WIDE_INT);
3458 static unsigned long size_of_die (dw_die_ref);
3459 static void calc_die_sizes (dw_die_ref);
3460 static void calc_base_type_die_sizes (void);
3461 static void mark_dies (dw_die_ref);
3462 static void unmark_dies (dw_die_ref);
3463 static void unmark_all_dies (dw_die_ref);
3464 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3465 static unsigned long size_of_aranges (void);
3466 static enum dwarf_form value_format (dw_attr_node *);
3467 static void output_value_format (dw_attr_node *);
3468 static void output_abbrev_section (void);
3469 static void output_die_abbrevs (unsigned long, dw_die_ref);
3470 static void output_die_symbol (dw_die_ref);
3471 static void output_die (dw_die_ref);
3472 static void output_compilation_unit_header (enum dwarf_unit_type);
3473 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3474 static void output_comdat_type_unit (comdat_type_node *);
3475 static const char *dwarf2_name (tree, int);
3476 static void add_pubname (tree, dw_die_ref);
3477 static void add_enumerator_pubname (const char *, dw_die_ref);
3478 static void add_pubname_string (const char *, dw_die_ref);
3479 static void add_pubtype (tree, dw_die_ref);
3480 static void output_pubnames (vec<pubname_entry, va_gc> *);
3481 static void output_aranges (void);
3482 static unsigned int add_ranges (const_tree, bool = false);
3483 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3484 bool *, bool);
3485 static void output_ranges (void);
3486 static dw_line_info_table *new_line_info_table (void);
3487 static void output_line_info (bool);
3488 static void output_file_names (void);
3489 static dw_die_ref base_type_die (tree, bool);
3490 static int is_base_type (tree);
3491 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3492 static int decl_quals (const_tree);
3493 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3494 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3495 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3496 static int type_is_enum (const_tree);
3497 static unsigned int dbx_reg_number (const_rtx);
3498 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3499 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3500 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3501 enum var_init_status);
3502 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3503 enum var_init_status);
3504 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3505 enum var_init_status);
3506 static int is_based_loc (const_rtx);
3507 static bool resolve_one_addr (rtx *);
3508 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3509 enum var_init_status);
3510 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3511 enum var_init_status);
3512 struct loc_descr_context;
3513 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3514 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3515 static dw_loc_list_ref loc_list_from_tree (tree, int,
3516 struct loc_descr_context *);
3517 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3518 struct loc_descr_context *);
3519 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3520 static tree field_type (const_tree);
3521 static unsigned int simple_type_align_in_bits (const_tree);
3522 static unsigned int simple_decl_align_in_bits (const_tree);
3523 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3524 struct vlr_context;
3525 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3526 HOST_WIDE_INT *);
3527 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3528 dw_loc_list_ref);
3529 static void add_data_member_location_attribute (dw_die_ref, tree,
3530 struct vlr_context *);
3531 static bool add_const_value_attribute (dw_die_ref, rtx);
3532 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3533 static void insert_wide_int (const wide_int &, unsigned char *, int);
3534 static void insert_float (const_rtx, unsigned char *);
3535 static rtx rtl_for_decl_location (tree);
3536 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3537 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3538 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3539 static void add_name_attribute (dw_die_ref, const char *);
3540 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3541 static void add_comp_dir_attribute (dw_die_ref);
3542 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3543 struct loc_descr_context *);
3544 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3545 struct loc_descr_context *);
3546 static void add_subscript_info (dw_die_ref, tree, bool);
3547 static void add_byte_size_attribute (dw_die_ref, tree);
3548 static void add_alignment_attribute (dw_die_ref, tree);
3549 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3550 struct vlr_context *);
3551 static void add_bit_size_attribute (dw_die_ref, tree);
3552 static void add_prototyped_attribute (dw_die_ref, tree);
3553 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3554 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3555 static void add_src_coords_attributes (dw_die_ref, tree);
3556 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3557 static void add_discr_value (dw_die_ref, dw_discr_value *);
3558 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3559 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3560 static void push_decl_scope (tree);
3561 static void pop_decl_scope (void);
3562 static dw_die_ref scope_die_for (tree, dw_die_ref);
3563 static inline int local_scope_p (dw_die_ref);
3564 static inline int class_scope_p (dw_die_ref);
3565 static inline int class_or_namespace_scope_p (dw_die_ref);
3566 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3567 static void add_calling_convention_attribute (dw_die_ref, tree);
3568 static const char *type_tag (const_tree);
3569 static tree member_declared_type (const_tree);
3570 #if 0
3571 static const char *decl_start_label (tree);
3572 #endif
3573 static void gen_array_type_die (tree, dw_die_ref);
3574 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3575 #if 0
3576 static void gen_entry_point_die (tree, dw_die_ref);
3577 #endif
3578 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3579 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3580 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3581 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3582 static void gen_formal_types_die (tree, dw_die_ref);
3583 static void gen_subprogram_die (tree, dw_die_ref);
3584 static void gen_variable_die (tree, tree, dw_die_ref);
3585 static void gen_const_die (tree, dw_die_ref);
3586 static void gen_label_die (tree, dw_die_ref);
3587 static void gen_lexical_block_die (tree, dw_die_ref);
3588 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3589 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3590 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3591 static dw_die_ref gen_compile_unit_die (const char *);
3592 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3593 static void gen_member_die (tree, dw_die_ref);
3594 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3595 enum debug_info_usage);
3596 static void gen_subroutine_type_die (tree, dw_die_ref);
3597 static void gen_typedef_die (tree, dw_die_ref);
3598 static void gen_type_die (tree, dw_die_ref);
3599 static void gen_block_die (tree, dw_die_ref);
3600 static void decls_for_scope (tree, dw_die_ref);
3601 static bool is_naming_typedef_decl (const_tree);
3602 static inline dw_die_ref get_context_die (tree);
3603 static void gen_namespace_die (tree, dw_die_ref);
3604 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3605 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3606 static dw_die_ref force_decl_die (tree);
3607 static dw_die_ref force_type_die (tree);
3608 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3609 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3610 static struct dwarf_file_data * lookup_filename (const char *);
3611 static void retry_incomplete_types (void);
3612 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3613 static void gen_generic_params_dies (tree);
3614 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3615 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3616 static void splice_child_die (dw_die_ref, dw_die_ref);
3617 static int file_info_cmp (const void *, const void *);
3618 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3619 const char *, const char *);
3620 static void output_loc_list (dw_loc_list_ref);
3621 static char *gen_internal_sym (const char *);
3622 static bool want_pubnames (void);
3624 static void prune_unmark_dies (dw_die_ref);
3625 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3626 static void prune_unused_types_mark (dw_die_ref, int);
3627 static void prune_unused_types_walk (dw_die_ref);
3628 static void prune_unused_types_walk_attribs (dw_die_ref);
3629 static void prune_unused_types_prune (dw_die_ref);
3630 static void prune_unused_types (void);
3631 static int maybe_emit_file (struct dwarf_file_data *fd);
3632 static inline const char *AT_vms_delta1 (dw_attr_node *);
3633 static inline const char *AT_vms_delta2 (dw_attr_node *);
3634 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3635 const char *, const char *);
3636 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3637 static void gen_remaining_tmpl_value_param_die_attribute (void);
3638 static bool generic_type_p (tree);
3639 static void schedule_generic_params_dies_gen (tree t);
3640 static void gen_scheduled_generic_parms_dies (void);
3641 static void resolve_variable_values (void);
3643 static const char *comp_dir_string (void);
3645 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3647 /* enum for tracking thread-local variables whose address is really an offset
3648 relative to the TLS pointer, which will need link-time relocation, but will
3649 not need relocation by the DWARF consumer. */
3651 enum dtprel_bool
3653 dtprel_false = 0,
3654 dtprel_true = 1
3657 /* Return the operator to use for an address of a variable. For dtprel_true, we
3658 use DW_OP_const*. For regular variables, which need both link-time
3659 relocation and consumer-level relocation (e.g., to account for shared objects
3660 loaded at a random address), we use DW_OP_addr*. */
3662 static inline enum dwarf_location_atom
3663 dw_addr_op (enum dtprel_bool dtprel)
3665 if (dtprel == dtprel_true)
3666 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3667 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3668 else
3669 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3672 /* Return a pointer to a newly allocated address location description. If
3673 dwarf_split_debug_info is true, then record the address with the appropriate
3674 relocation. */
3675 static inline dw_loc_descr_ref
3676 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3678 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3680 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3681 ref->dw_loc_oprnd1.v.val_addr = addr;
3682 ref->dtprel = dtprel;
3683 if (dwarf_split_debug_info)
3684 ref->dw_loc_oprnd1.val_entry
3685 = add_addr_table_entry (addr,
3686 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3687 else
3688 ref->dw_loc_oprnd1.val_entry = NULL;
3690 return ref;
3693 /* Section names used to hold DWARF debugging information. */
3695 #ifndef DEBUG_INFO_SECTION
3696 #define DEBUG_INFO_SECTION ".debug_info"
3697 #endif
3698 #ifndef DEBUG_DWO_INFO_SECTION
3699 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3700 #endif
3701 #ifndef DEBUG_ABBREV_SECTION
3702 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3703 #endif
3704 #ifndef DEBUG_DWO_ABBREV_SECTION
3705 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3706 #endif
3707 #ifndef DEBUG_ARANGES_SECTION
3708 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3709 #endif
3710 #ifndef DEBUG_ADDR_SECTION
3711 #define DEBUG_ADDR_SECTION ".debug_addr"
3712 #endif
3713 #ifndef DEBUG_MACINFO_SECTION
3714 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3715 #endif
3716 #ifndef DEBUG_DWO_MACINFO_SECTION
3717 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3718 #endif
3719 #ifndef DEBUG_DWO_MACRO_SECTION
3720 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3721 #endif
3722 #ifndef DEBUG_MACRO_SECTION
3723 #define DEBUG_MACRO_SECTION ".debug_macro"
3724 #endif
3725 #ifndef DEBUG_LINE_SECTION
3726 #define DEBUG_LINE_SECTION ".debug_line"
3727 #endif
3728 #ifndef DEBUG_DWO_LINE_SECTION
3729 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3730 #endif
3731 #ifndef DEBUG_LOC_SECTION
3732 #define DEBUG_LOC_SECTION ".debug_loc"
3733 #endif
3734 #ifndef DEBUG_DWO_LOC_SECTION
3735 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3736 #endif
3737 #ifndef DEBUG_LOCLISTS_SECTION
3738 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3739 #endif
3740 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3741 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3742 #endif
3743 #ifndef DEBUG_PUBNAMES_SECTION
3744 #define DEBUG_PUBNAMES_SECTION \
3745 ((debug_generate_pub_sections == 2) \
3746 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3747 #endif
3748 #ifndef DEBUG_PUBTYPES_SECTION
3749 #define DEBUG_PUBTYPES_SECTION \
3750 ((debug_generate_pub_sections == 2) \
3751 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3752 #endif
3753 #ifndef DEBUG_STR_OFFSETS_SECTION
3754 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3755 #endif
3756 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3757 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3758 #endif
3759 #ifndef DEBUG_STR_DWO_SECTION
3760 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3761 #endif
3762 #ifndef DEBUG_STR_SECTION
3763 #define DEBUG_STR_SECTION ".debug_str"
3764 #endif
3765 #ifndef DEBUG_RANGES_SECTION
3766 #define DEBUG_RANGES_SECTION ".debug_ranges"
3767 #endif
3768 #ifndef DEBUG_RNGLISTS_SECTION
3769 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3770 #endif
3771 #ifndef DEBUG_LINE_STR_SECTION
3772 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3773 #endif
3775 /* Standard ELF section names for compiled code and data. */
3776 #ifndef TEXT_SECTION_NAME
3777 #define TEXT_SECTION_NAME ".text"
3778 #endif
3780 /* Section flags for .debug_str section. */
3781 #define DEBUG_STR_SECTION_FLAGS \
3782 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3783 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3784 : SECTION_DEBUG)
3786 /* Section flags for .debug_str.dwo section. */
3787 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3789 /* Labels we insert at beginning sections we can reference instead of
3790 the section names themselves. */
3792 #ifndef TEXT_SECTION_LABEL
3793 #define TEXT_SECTION_LABEL "Ltext"
3794 #endif
3795 #ifndef COLD_TEXT_SECTION_LABEL
3796 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3797 #endif
3798 #ifndef DEBUG_LINE_SECTION_LABEL
3799 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3800 #endif
3801 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3802 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3803 #endif
3804 #ifndef DEBUG_INFO_SECTION_LABEL
3805 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3806 #endif
3807 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3808 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3809 #endif
3810 #ifndef DEBUG_ABBREV_SECTION_LABEL
3811 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3812 #endif
3813 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3814 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3815 #endif
3816 #ifndef DEBUG_ADDR_SECTION_LABEL
3817 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3818 #endif
3819 #ifndef DEBUG_LOC_SECTION_LABEL
3820 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3821 #endif
3822 #ifndef DEBUG_RANGES_SECTION_LABEL
3823 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3824 #endif
3825 #ifndef DEBUG_MACINFO_SECTION_LABEL
3826 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3827 #endif
3828 #ifndef DEBUG_MACRO_SECTION_LABEL
3829 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3830 #endif
3831 #define SKELETON_COMP_DIE_ABBREV 1
3832 #define SKELETON_TYPE_DIE_ABBREV 2
3834 /* Definitions of defaults for formats and names of various special
3835 (artificial) labels which may be generated within this file (when the -g
3836 options is used and DWARF2_DEBUGGING_INFO is in effect.
3837 If necessary, these may be overridden from within the tm.h file, but
3838 typically, overriding these defaults is unnecessary. */
3840 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3841 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3842 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3843 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3844 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3845 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3848 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3849 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3850 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3851 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3852 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3853 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3854 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3856 #ifndef TEXT_END_LABEL
3857 #define TEXT_END_LABEL "Letext"
3858 #endif
3859 #ifndef COLD_END_LABEL
3860 #define COLD_END_LABEL "Letext_cold"
3861 #endif
3862 #ifndef BLOCK_BEGIN_LABEL
3863 #define BLOCK_BEGIN_LABEL "LBB"
3864 #endif
3865 #ifndef BLOCK_END_LABEL
3866 #define BLOCK_END_LABEL "LBE"
3867 #endif
3868 #ifndef LINE_CODE_LABEL
3869 #define LINE_CODE_LABEL "LM"
3870 #endif
3873 /* Return the root of the DIE's built for the current compilation unit. */
3874 static dw_die_ref
3875 comp_unit_die (void)
3877 if (!single_comp_unit_die)
3878 single_comp_unit_die = gen_compile_unit_die (NULL);
3879 return single_comp_unit_die;
3882 /* We allow a language front-end to designate a function that is to be
3883 called to "demangle" any name before it is put into a DIE. */
3885 static const char *(*demangle_name_func) (const char *);
3887 void
3888 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3890 demangle_name_func = func;
3893 /* Test if rtl node points to a pseudo register. */
3895 static inline int
3896 is_pseudo_reg (const_rtx rtl)
3898 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3899 || (GET_CODE (rtl) == SUBREG
3900 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3903 /* Return a reference to a type, with its const and volatile qualifiers
3904 removed. */
3906 static inline tree
3907 type_main_variant (tree type)
3909 type = TYPE_MAIN_VARIANT (type);
3911 /* ??? There really should be only one main variant among any group of
3912 variants of a given type (and all of the MAIN_VARIANT values for all
3913 members of the group should point to that one type) but sometimes the C
3914 front-end messes this up for array types, so we work around that bug
3915 here. */
3916 if (TREE_CODE (type) == ARRAY_TYPE)
3917 while (type != TYPE_MAIN_VARIANT (type))
3918 type = TYPE_MAIN_VARIANT (type);
3920 return type;
3923 /* Return nonzero if the given type node represents a tagged type. */
3925 static inline int
3926 is_tagged_type (const_tree type)
3928 enum tree_code code = TREE_CODE (type);
3930 return (code == RECORD_TYPE || code == UNION_TYPE
3931 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3934 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3936 static void
3937 get_ref_die_offset_label (char *label, dw_die_ref ref)
3939 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3942 /* Return die_offset of a DIE reference to a base type. */
3944 static unsigned long int
3945 get_base_type_offset (dw_die_ref ref)
3947 if (ref->die_offset)
3948 return ref->die_offset;
3949 if (comp_unit_die ()->die_abbrev)
3951 calc_base_type_die_sizes ();
3952 gcc_assert (ref->die_offset);
3954 return ref->die_offset;
3957 /* Return die_offset of a DIE reference other than base type. */
3959 static unsigned long int
3960 get_ref_die_offset (dw_die_ref ref)
3962 gcc_assert (ref->die_offset);
3963 return ref->die_offset;
3966 /* Convert a DIE tag into its string name. */
3968 static const char *
3969 dwarf_tag_name (unsigned int tag)
3971 const char *name = get_DW_TAG_name (tag);
3973 if (name != NULL)
3974 return name;
3976 return "DW_TAG_<unknown>";
3979 /* Convert a DWARF attribute code into its string name. */
3981 static const char *
3982 dwarf_attr_name (unsigned int attr)
3984 const char *name;
3986 switch (attr)
3988 #if VMS_DEBUGGING_INFO
3989 case DW_AT_HP_prologue:
3990 return "DW_AT_HP_prologue";
3991 #else
3992 case DW_AT_MIPS_loop_unroll_factor:
3993 return "DW_AT_MIPS_loop_unroll_factor";
3994 #endif
3996 #if VMS_DEBUGGING_INFO
3997 case DW_AT_HP_epilogue:
3998 return "DW_AT_HP_epilogue";
3999 #else
4000 case DW_AT_MIPS_stride:
4001 return "DW_AT_MIPS_stride";
4002 #endif
4005 name = get_DW_AT_name (attr);
4007 if (name != NULL)
4008 return name;
4010 return "DW_AT_<unknown>";
4013 /* Convert a DWARF value form code into its string name. */
4015 static const char *
4016 dwarf_form_name (unsigned int form)
4018 const char *name = get_DW_FORM_name (form);
4020 if (name != NULL)
4021 return name;
4023 return "DW_FORM_<unknown>";
4026 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4027 instance of an inlined instance of a decl which is local to an inline
4028 function, so we have to trace all of the way back through the origin chain
4029 to find out what sort of node actually served as the original seed for the
4030 given block. */
4032 static tree
4033 decl_ultimate_origin (const_tree decl)
4035 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4036 return NULL_TREE;
4038 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4039 we're trying to output the abstract instance of this function. */
4040 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4041 return NULL_TREE;
4043 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4044 most distant ancestor, this should never happen. */
4045 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4047 return DECL_ABSTRACT_ORIGIN (decl);
4050 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4051 of a virtual function may refer to a base class, so we check the 'this'
4052 parameter. */
4054 static tree
4055 decl_class_context (tree decl)
4057 tree context = NULL_TREE;
4059 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4060 context = DECL_CONTEXT (decl);
4061 else
4062 context = TYPE_MAIN_VARIANT
4063 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4065 if (context && !TYPE_P (context))
4066 context = NULL_TREE;
4068 return context;
4071 /* Add an attribute/value pair to a DIE. */
4073 static inline void
4074 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4076 /* Maybe this should be an assert? */
4077 if (die == NULL)
4078 return;
4080 vec_safe_reserve (die->die_attr, 1);
4081 vec_safe_push (die->die_attr, *attr);
4084 static inline enum dw_val_class
4085 AT_class (dw_attr_node *a)
4087 return a->dw_attr_val.val_class;
4090 /* Return the index for any attribute that will be referenced with a
4091 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4092 are stored in dw_attr_val.v.val_str for reference counting
4093 pruning. */
4095 static inline unsigned int
4096 AT_index (dw_attr_node *a)
4098 if (AT_class (a) == dw_val_class_str)
4099 return a->dw_attr_val.v.val_str->index;
4100 else if (a->dw_attr_val.val_entry != NULL)
4101 return a->dw_attr_val.val_entry->index;
4102 return NOT_INDEXED;
4105 /* Add a flag value attribute to a DIE. */
4107 static inline void
4108 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4110 dw_attr_node attr;
4112 attr.dw_attr = attr_kind;
4113 attr.dw_attr_val.val_class = dw_val_class_flag;
4114 attr.dw_attr_val.val_entry = NULL;
4115 attr.dw_attr_val.v.val_flag = flag;
4116 add_dwarf_attr (die, &attr);
4119 static inline unsigned
4120 AT_flag (dw_attr_node *a)
4122 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4123 return a->dw_attr_val.v.val_flag;
4126 /* Add a signed integer attribute value to a DIE. */
4128 static inline void
4129 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4131 dw_attr_node attr;
4133 attr.dw_attr = attr_kind;
4134 attr.dw_attr_val.val_class = dw_val_class_const;
4135 attr.dw_attr_val.val_entry = NULL;
4136 attr.dw_attr_val.v.val_int = int_val;
4137 add_dwarf_attr (die, &attr);
4140 static inline HOST_WIDE_INT
4141 AT_int (dw_attr_node *a)
4143 gcc_assert (a && (AT_class (a) == dw_val_class_const
4144 || AT_class (a) == dw_val_class_const_implicit));
4145 return a->dw_attr_val.v.val_int;
4148 /* Add an unsigned integer attribute value to a DIE. */
4150 static inline void
4151 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4152 unsigned HOST_WIDE_INT unsigned_val)
4154 dw_attr_node attr;
4156 attr.dw_attr = attr_kind;
4157 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4158 attr.dw_attr_val.val_entry = NULL;
4159 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4160 add_dwarf_attr (die, &attr);
4163 static inline unsigned HOST_WIDE_INT
4164 AT_unsigned (dw_attr_node *a)
4166 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4167 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4168 return a->dw_attr_val.v.val_unsigned;
4171 /* Add an unsigned wide integer attribute value to a DIE. */
4173 static inline void
4174 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4175 const wide_int& w)
4177 dw_attr_node attr;
4179 attr.dw_attr = attr_kind;
4180 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4181 attr.dw_attr_val.val_entry = NULL;
4182 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4183 *attr.dw_attr_val.v.val_wide = w;
4184 add_dwarf_attr (die, &attr);
4187 /* Add an unsigned double integer attribute value to a DIE. */
4189 static inline void
4190 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4191 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4193 dw_attr_node attr;
4195 attr.dw_attr = attr_kind;
4196 attr.dw_attr_val.val_class = dw_val_class_const_double;
4197 attr.dw_attr_val.val_entry = NULL;
4198 attr.dw_attr_val.v.val_double.high = high;
4199 attr.dw_attr_val.v.val_double.low = low;
4200 add_dwarf_attr (die, &attr);
4203 /* Add a floating point attribute value to a DIE and return it. */
4205 static inline void
4206 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4207 unsigned int length, unsigned int elt_size, unsigned char *array)
4209 dw_attr_node attr;
4211 attr.dw_attr = attr_kind;
4212 attr.dw_attr_val.val_class = dw_val_class_vec;
4213 attr.dw_attr_val.val_entry = NULL;
4214 attr.dw_attr_val.v.val_vec.length = length;
4215 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4216 attr.dw_attr_val.v.val_vec.array = array;
4217 add_dwarf_attr (die, &attr);
4220 /* Add an 8-byte data attribute value to a DIE. */
4222 static inline void
4223 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4224 unsigned char data8[8])
4226 dw_attr_node attr;
4228 attr.dw_attr = attr_kind;
4229 attr.dw_attr_val.val_class = dw_val_class_data8;
4230 attr.dw_attr_val.val_entry = NULL;
4231 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4232 add_dwarf_attr (die, &attr);
4235 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4236 dwarf_split_debug_info, address attributes in dies destined for the
4237 final executable have force_direct set to avoid using indexed
4238 references. */
4240 static inline void
4241 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4242 bool force_direct)
4244 dw_attr_node attr;
4245 char * lbl_id;
4247 lbl_id = xstrdup (lbl_low);
4248 attr.dw_attr = DW_AT_low_pc;
4249 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4250 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4251 if (dwarf_split_debug_info && !force_direct)
4252 attr.dw_attr_val.val_entry
4253 = add_addr_table_entry (lbl_id, ate_kind_label);
4254 else
4255 attr.dw_attr_val.val_entry = NULL;
4256 add_dwarf_attr (die, &attr);
4258 attr.dw_attr = DW_AT_high_pc;
4259 if (dwarf_version < 4)
4260 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4261 else
4262 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4263 lbl_id = xstrdup (lbl_high);
4264 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4265 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4266 && dwarf_split_debug_info && !force_direct)
4267 attr.dw_attr_val.val_entry
4268 = add_addr_table_entry (lbl_id, ate_kind_label);
4269 else
4270 attr.dw_attr_val.val_entry = NULL;
4271 add_dwarf_attr (die, &attr);
4274 /* Hash and equality functions for debug_str_hash. */
4276 hashval_t
4277 indirect_string_hasher::hash (indirect_string_node *x)
4279 return htab_hash_string (x->str);
4282 bool
4283 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4285 return strcmp (x1->str, x2) == 0;
4288 /* Add STR to the given string hash table. */
4290 static struct indirect_string_node *
4291 find_AT_string_in_table (const char *str,
4292 hash_table<indirect_string_hasher> *table)
4294 struct indirect_string_node *node;
4296 indirect_string_node **slot
4297 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4298 if (*slot == NULL)
4300 node = ggc_cleared_alloc<indirect_string_node> ();
4301 node->str = ggc_strdup (str);
4302 *slot = node;
4304 else
4305 node = *slot;
4307 node->refcount++;
4308 return node;
4311 /* Add STR to the indirect string hash table. */
4313 static struct indirect_string_node *
4314 find_AT_string (const char *str)
4316 if (! debug_str_hash)
4317 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4319 return find_AT_string_in_table (str, debug_str_hash);
4322 /* Add a string attribute value to a DIE. */
4324 static inline void
4325 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4327 dw_attr_node attr;
4328 struct indirect_string_node *node;
4330 node = find_AT_string (str);
4332 attr.dw_attr = attr_kind;
4333 attr.dw_attr_val.val_class = dw_val_class_str;
4334 attr.dw_attr_val.val_entry = NULL;
4335 attr.dw_attr_val.v.val_str = node;
4336 add_dwarf_attr (die, &attr);
4339 static inline const char *
4340 AT_string (dw_attr_node *a)
4342 gcc_assert (a && AT_class (a) == dw_val_class_str);
4343 return a->dw_attr_val.v.val_str->str;
4346 /* Call this function directly to bypass AT_string_form's logic to put
4347 the string inline in the die. */
4349 static void
4350 set_indirect_string (struct indirect_string_node *node)
4352 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4353 /* Already indirect is a no op. */
4354 if (node->form == DW_FORM_strp
4355 || node->form == DW_FORM_line_strp
4356 || node->form == DW_FORM_GNU_str_index)
4358 gcc_assert (node->label);
4359 return;
4361 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4362 ++dw2_string_counter;
4363 node->label = xstrdup (label);
4365 if (!dwarf_split_debug_info)
4367 node->form = DW_FORM_strp;
4368 node->index = NOT_INDEXED;
4370 else
4372 node->form = DW_FORM_GNU_str_index;
4373 node->index = NO_INDEX_ASSIGNED;
4377 /* Find out whether a string should be output inline in DIE
4378 or out-of-line in .debug_str section. */
4380 static enum dwarf_form
4381 find_string_form (struct indirect_string_node *node)
4383 unsigned int len;
4385 if (node->form)
4386 return node->form;
4388 len = strlen (node->str) + 1;
4390 /* If the string is shorter or equal to the size of the reference, it is
4391 always better to put it inline. */
4392 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4393 return node->form = DW_FORM_string;
4395 /* If we cannot expect the linker to merge strings in .debug_str
4396 section, only put it into .debug_str if it is worth even in this
4397 single module. */
4398 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4399 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4400 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4401 return node->form = DW_FORM_string;
4403 set_indirect_string (node);
4405 return node->form;
4408 /* Find out whether the string referenced from the attribute should be
4409 output inline in DIE or out-of-line in .debug_str section. */
4411 static enum dwarf_form
4412 AT_string_form (dw_attr_node *a)
4414 gcc_assert (a && AT_class (a) == dw_val_class_str);
4415 return find_string_form (a->dw_attr_val.v.val_str);
4418 /* Add a DIE reference attribute value to a DIE. */
4420 static inline void
4421 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4423 dw_attr_node attr;
4424 gcc_checking_assert (targ_die != NULL);
4426 /* With LTO we can end up trying to reference something we didn't create
4427 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4428 if (targ_die == NULL)
4429 return;
4431 attr.dw_attr = attr_kind;
4432 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4433 attr.dw_attr_val.val_entry = NULL;
4434 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4435 attr.dw_attr_val.v.val_die_ref.external = 0;
4436 add_dwarf_attr (die, &attr);
4439 /* Change DIE reference REF to point to NEW_DIE instead. */
4441 static inline void
4442 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4444 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4445 ref->dw_attr_val.v.val_die_ref.die = new_die;
4446 ref->dw_attr_val.v.val_die_ref.external = 0;
4449 /* Add an AT_specification attribute to a DIE, and also make the back
4450 pointer from the specification to the definition. */
4452 static inline void
4453 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4455 add_AT_die_ref (die, DW_AT_specification, targ_die);
4456 gcc_assert (!targ_die->die_definition);
4457 targ_die->die_definition = die;
4460 static inline dw_die_ref
4461 AT_ref (dw_attr_node *a)
4463 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4464 return a->dw_attr_val.v.val_die_ref.die;
4467 static inline int
4468 AT_ref_external (dw_attr_node *a)
4470 if (a && AT_class (a) == dw_val_class_die_ref)
4471 return a->dw_attr_val.v.val_die_ref.external;
4473 return 0;
4476 static inline void
4477 set_AT_ref_external (dw_attr_node *a, int i)
4479 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4480 a->dw_attr_val.v.val_die_ref.external = i;
4483 /* Add an FDE reference attribute value to a DIE. */
4485 static inline void
4486 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4488 dw_attr_node attr;
4490 attr.dw_attr = attr_kind;
4491 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4492 attr.dw_attr_val.val_entry = NULL;
4493 attr.dw_attr_val.v.val_fde_index = targ_fde;
4494 add_dwarf_attr (die, &attr);
4497 /* Add a location description attribute value to a DIE. */
4499 static inline void
4500 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4502 dw_attr_node attr;
4504 attr.dw_attr = attr_kind;
4505 attr.dw_attr_val.val_class = dw_val_class_loc;
4506 attr.dw_attr_val.val_entry = NULL;
4507 attr.dw_attr_val.v.val_loc = loc;
4508 add_dwarf_attr (die, &attr);
4511 static inline dw_loc_descr_ref
4512 AT_loc (dw_attr_node *a)
4514 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4515 return a->dw_attr_val.v.val_loc;
4518 static inline void
4519 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4521 dw_attr_node attr;
4523 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4524 return;
4526 attr.dw_attr = attr_kind;
4527 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4528 attr.dw_attr_val.val_entry = NULL;
4529 attr.dw_attr_val.v.val_loc_list = loc_list;
4530 add_dwarf_attr (die, &attr);
4531 have_location_lists = true;
4534 static inline dw_loc_list_ref
4535 AT_loc_list (dw_attr_node *a)
4537 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4538 return a->dw_attr_val.v.val_loc_list;
4541 static inline dw_loc_list_ref *
4542 AT_loc_list_ptr (dw_attr_node *a)
4544 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4545 return &a->dw_attr_val.v.val_loc_list;
4548 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4550 static hashval_t hash (addr_table_entry *);
4551 static bool equal (addr_table_entry *, addr_table_entry *);
4554 /* Table of entries into the .debug_addr section. */
4556 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4558 /* Hash an address_table_entry. */
4560 hashval_t
4561 addr_hasher::hash (addr_table_entry *a)
4563 inchash::hash hstate;
4564 switch (a->kind)
4566 case ate_kind_rtx:
4567 hstate.add_int (0);
4568 break;
4569 case ate_kind_rtx_dtprel:
4570 hstate.add_int (1);
4571 break;
4572 case ate_kind_label:
4573 return htab_hash_string (a->addr.label);
4574 default:
4575 gcc_unreachable ();
4577 inchash::add_rtx (a->addr.rtl, hstate);
4578 return hstate.end ();
4581 /* Determine equality for two address_table_entries. */
4583 bool
4584 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4586 if (a1->kind != a2->kind)
4587 return 0;
4588 switch (a1->kind)
4590 case ate_kind_rtx:
4591 case ate_kind_rtx_dtprel:
4592 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4593 case ate_kind_label:
4594 return strcmp (a1->addr.label, a2->addr.label) == 0;
4595 default:
4596 gcc_unreachable ();
4600 /* Initialize an addr_table_entry. */
4602 void
4603 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4605 e->kind = kind;
4606 switch (kind)
4608 case ate_kind_rtx:
4609 case ate_kind_rtx_dtprel:
4610 e->addr.rtl = (rtx) addr;
4611 break;
4612 case ate_kind_label:
4613 e->addr.label = (char *) addr;
4614 break;
4616 e->refcount = 0;
4617 e->index = NO_INDEX_ASSIGNED;
4620 /* Add attr to the address table entry to the table. Defer setting an
4621 index until output time. */
4623 static addr_table_entry *
4624 add_addr_table_entry (void *addr, enum ate_kind kind)
4626 addr_table_entry *node;
4627 addr_table_entry finder;
4629 gcc_assert (dwarf_split_debug_info);
4630 if (! addr_index_table)
4631 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4632 init_addr_table_entry (&finder, kind, addr);
4633 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4635 if (*slot == HTAB_EMPTY_ENTRY)
4637 node = ggc_cleared_alloc<addr_table_entry> ();
4638 init_addr_table_entry (node, kind, addr);
4639 *slot = node;
4641 else
4642 node = *slot;
4644 node->refcount++;
4645 return node;
4648 /* Remove an entry from the addr table by decrementing its refcount.
4649 Strictly, decrementing the refcount would be enough, but the
4650 assertion that the entry is actually in the table has found
4651 bugs. */
4653 static void
4654 remove_addr_table_entry (addr_table_entry *entry)
4656 gcc_assert (dwarf_split_debug_info && addr_index_table);
4657 /* After an index is assigned, the table is frozen. */
4658 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4659 entry->refcount--;
4662 /* Given a location list, remove all addresses it refers to from the
4663 address_table. */
4665 static void
4666 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4668 for (; descr; descr = descr->dw_loc_next)
4669 if (descr->dw_loc_oprnd1.val_entry != NULL)
4671 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4672 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4676 /* A helper function for dwarf2out_finish called through
4677 htab_traverse. Assign an addr_table_entry its index. All entries
4678 must be collected into the table when this function is called,
4679 because the indexing code relies on htab_traverse to traverse nodes
4680 in the same order for each run. */
4683 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4685 addr_table_entry *node = *h;
4687 /* Don't index unreferenced nodes. */
4688 if (node->refcount == 0)
4689 return 1;
4691 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4692 node->index = *index;
4693 *index += 1;
4695 return 1;
4698 /* Add an address constant attribute value to a DIE. When using
4699 dwarf_split_debug_info, address attributes in dies destined for the
4700 final executable should be direct references--setting the parameter
4701 force_direct ensures this behavior. */
4703 static inline void
4704 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4705 bool force_direct)
4707 dw_attr_node attr;
4709 attr.dw_attr = attr_kind;
4710 attr.dw_attr_val.val_class = dw_val_class_addr;
4711 attr.dw_attr_val.v.val_addr = addr;
4712 if (dwarf_split_debug_info && !force_direct)
4713 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4714 else
4715 attr.dw_attr_val.val_entry = NULL;
4716 add_dwarf_attr (die, &attr);
4719 /* Get the RTX from to an address DIE attribute. */
4721 static inline rtx
4722 AT_addr (dw_attr_node *a)
4724 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4725 return a->dw_attr_val.v.val_addr;
4728 /* Add a file attribute value to a DIE. */
4730 static inline void
4731 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4732 struct dwarf_file_data *fd)
4734 dw_attr_node attr;
4736 attr.dw_attr = attr_kind;
4737 attr.dw_attr_val.val_class = dw_val_class_file;
4738 attr.dw_attr_val.val_entry = NULL;
4739 attr.dw_attr_val.v.val_file = fd;
4740 add_dwarf_attr (die, &attr);
4743 /* Get the dwarf_file_data from a file DIE attribute. */
4745 static inline struct dwarf_file_data *
4746 AT_file (dw_attr_node *a)
4748 gcc_assert (a && (AT_class (a) == dw_val_class_file
4749 || AT_class (a) == dw_val_class_file_implicit));
4750 return a->dw_attr_val.v.val_file;
4753 /* Add a vms delta attribute value to a DIE. */
4755 static inline void
4756 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4757 const char *lbl1, const char *lbl2)
4759 dw_attr_node attr;
4761 attr.dw_attr = attr_kind;
4762 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4763 attr.dw_attr_val.val_entry = NULL;
4764 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4765 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4766 add_dwarf_attr (die, &attr);
4769 /* Add a label identifier attribute value to a DIE. */
4771 static inline void
4772 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4773 const char *lbl_id)
4775 dw_attr_node attr;
4777 attr.dw_attr = attr_kind;
4778 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4779 attr.dw_attr_val.val_entry = NULL;
4780 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4781 if (dwarf_split_debug_info)
4782 attr.dw_attr_val.val_entry
4783 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4784 ate_kind_label);
4785 add_dwarf_attr (die, &attr);
4788 /* Add a section offset attribute value to a DIE, an offset into the
4789 debug_line section. */
4791 static inline void
4792 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4793 const char *label)
4795 dw_attr_node attr;
4797 attr.dw_attr = attr_kind;
4798 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4799 attr.dw_attr_val.val_entry = NULL;
4800 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4801 add_dwarf_attr (die, &attr);
4804 /* Add a section offset attribute value to a DIE, an offset into the
4805 debug_loclists section. */
4807 static inline void
4808 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4809 const char *label)
4811 dw_attr_node attr;
4813 attr.dw_attr = attr_kind;
4814 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4815 attr.dw_attr_val.val_entry = NULL;
4816 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4817 add_dwarf_attr (die, &attr);
4820 /* Add a section offset attribute value to a DIE, an offset into the
4821 debug_macinfo section. */
4823 static inline void
4824 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4825 const char *label)
4827 dw_attr_node attr;
4829 attr.dw_attr = attr_kind;
4830 attr.dw_attr_val.val_class = dw_val_class_macptr;
4831 attr.dw_attr_val.val_entry = NULL;
4832 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4833 add_dwarf_attr (die, &attr);
4836 /* Add an offset attribute value to a DIE. */
4838 static inline void
4839 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4840 unsigned HOST_WIDE_INT offset)
4842 dw_attr_node attr;
4844 attr.dw_attr = attr_kind;
4845 attr.dw_attr_val.val_class = dw_val_class_offset;
4846 attr.dw_attr_val.val_entry = NULL;
4847 attr.dw_attr_val.v.val_offset = offset;
4848 add_dwarf_attr (die, &attr);
4851 /* Add a range_list attribute value to a DIE. When using
4852 dwarf_split_debug_info, address attributes in dies destined for the
4853 final executable should be direct references--setting the parameter
4854 force_direct ensures this behavior. */
4856 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4857 #define RELOCATED_OFFSET (NULL)
4859 static void
4860 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4861 long unsigned int offset, bool force_direct)
4863 dw_attr_node attr;
4865 attr.dw_attr = attr_kind;
4866 attr.dw_attr_val.val_class = dw_val_class_range_list;
4867 /* For the range_list attribute, use val_entry to store whether the
4868 offset should follow split-debug-info or normal semantics. This
4869 value is read in output_range_list_offset. */
4870 if (dwarf_split_debug_info && !force_direct)
4871 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4872 else
4873 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4874 attr.dw_attr_val.v.val_offset = offset;
4875 add_dwarf_attr (die, &attr);
4878 /* Return the start label of a delta attribute. */
4880 static inline const char *
4881 AT_vms_delta1 (dw_attr_node *a)
4883 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4884 return a->dw_attr_val.v.val_vms_delta.lbl1;
4887 /* Return the end label of a delta attribute. */
4889 static inline const char *
4890 AT_vms_delta2 (dw_attr_node *a)
4892 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4893 return a->dw_attr_val.v.val_vms_delta.lbl2;
4896 static inline const char *
4897 AT_lbl (dw_attr_node *a)
4899 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4900 || AT_class (a) == dw_val_class_lineptr
4901 || AT_class (a) == dw_val_class_macptr
4902 || AT_class (a) == dw_val_class_loclistsptr
4903 || AT_class (a) == dw_val_class_high_pc));
4904 return a->dw_attr_val.v.val_lbl_id;
4907 /* Get the attribute of type attr_kind. */
4909 static dw_attr_node *
4910 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4912 dw_attr_node *a;
4913 unsigned ix;
4914 dw_die_ref spec = NULL;
4916 if (! die)
4917 return NULL;
4919 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4920 if (a->dw_attr == attr_kind)
4921 return a;
4922 else if (a->dw_attr == DW_AT_specification
4923 || a->dw_attr == DW_AT_abstract_origin)
4924 spec = AT_ref (a);
4926 if (spec)
4927 return get_AT (spec, attr_kind);
4929 return NULL;
4932 /* Returns the parent of the declaration of DIE. */
4934 static dw_die_ref
4935 get_die_parent (dw_die_ref die)
4937 dw_die_ref t;
4939 if (!die)
4940 return NULL;
4942 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4943 || (t = get_AT_ref (die, DW_AT_specification)))
4944 die = t;
4946 return die->die_parent;
4949 /* Return the "low pc" attribute value, typically associated with a subprogram
4950 DIE. Return null if the "low pc" attribute is either not present, or if it
4951 cannot be represented as an assembler label identifier. */
4953 static inline const char *
4954 get_AT_low_pc (dw_die_ref die)
4956 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4958 return a ? AT_lbl (a) : NULL;
4961 /* Return the "high pc" attribute value, typically associated with a subprogram
4962 DIE. Return null if the "high pc" attribute is either not present, or if it
4963 cannot be represented as an assembler label identifier. */
4965 static inline const char *
4966 get_AT_hi_pc (dw_die_ref die)
4968 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4970 return a ? AT_lbl (a) : NULL;
4973 /* Return the value of the string attribute designated by ATTR_KIND, or
4974 NULL if it is not present. */
4976 static inline const char *
4977 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4979 dw_attr_node *a = get_AT (die, attr_kind);
4981 return a ? AT_string (a) : NULL;
4984 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4985 if it is not present. */
4987 static inline int
4988 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4990 dw_attr_node *a = get_AT (die, attr_kind);
4992 return a ? AT_flag (a) : 0;
4995 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4996 if it is not present. */
4998 static inline unsigned
4999 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5001 dw_attr_node *a = get_AT (die, attr_kind);
5003 return a ? AT_unsigned (a) : 0;
5006 static inline dw_die_ref
5007 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5009 dw_attr_node *a = get_AT (die, attr_kind);
5011 return a ? AT_ref (a) : NULL;
5014 static inline struct dwarf_file_data *
5015 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5017 dw_attr_node *a = get_AT (die, attr_kind);
5019 return a ? AT_file (a) : NULL;
5022 /* Return TRUE if the language is C++. */
5024 static inline bool
5025 is_cxx (void)
5027 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5029 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5030 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5033 /* Return TRUE if DECL was created by the C++ frontend. */
5035 static bool
5036 is_cxx (const_tree decl)
5038 if (in_lto_p)
5040 const_tree context = decl;
5041 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5043 if (TREE_CODE (context) == BLOCK)
5044 context = BLOCK_SUPERCONTEXT (context);
5045 else
5046 context = get_containing_scope (context);
5048 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5049 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5051 return is_cxx ();
5054 /* Return TRUE if the language is Fortran. */
5056 static inline bool
5057 is_fortran (void)
5059 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5061 return (lang == DW_LANG_Fortran77
5062 || lang == DW_LANG_Fortran90
5063 || lang == DW_LANG_Fortran95
5064 || lang == DW_LANG_Fortran03
5065 || lang == DW_LANG_Fortran08);
5068 /* Return TRUE if the language is Ada. */
5070 static inline bool
5071 is_ada (void)
5073 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5075 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5078 /* Remove the specified attribute if present. Return TRUE if removal
5079 was successful. */
5081 static bool
5082 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5084 dw_attr_node *a;
5085 unsigned ix;
5087 if (! die)
5088 return false;
5090 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5091 if (a->dw_attr == attr_kind)
5093 if (AT_class (a) == dw_val_class_str)
5094 if (a->dw_attr_val.v.val_str->refcount)
5095 a->dw_attr_val.v.val_str->refcount--;
5097 /* vec::ordered_remove should help reduce the number of abbrevs
5098 that are needed. */
5099 die->die_attr->ordered_remove (ix);
5100 return true;
5102 return false;
5105 /* Remove CHILD from its parent. PREV must have the property that
5106 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5108 static void
5109 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5111 gcc_assert (child->die_parent == prev->die_parent);
5112 gcc_assert (prev->die_sib == child);
5113 if (prev == child)
5115 gcc_assert (child->die_parent->die_child == child);
5116 prev = NULL;
5118 else
5119 prev->die_sib = child->die_sib;
5120 if (child->die_parent->die_child == child)
5121 child->die_parent->die_child = prev;
5122 child->die_sib = NULL;
5125 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5126 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5128 static void
5129 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5131 dw_die_ref parent = old_child->die_parent;
5133 gcc_assert (parent == prev->die_parent);
5134 gcc_assert (prev->die_sib == old_child);
5136 new_child->die_parent = parent;
5137 if (prev == old_child)
5139 gcc_assert (parent->die_child == old_child);
5140 new_child->die_sib = new_child;
5142 else
5144 prev->die_sib = new_child;
5145 new_child->die_sib = old_child->die_sib;
5147 if (old_child->die_parent->die_child == old_child)
5148 old_child->die_parent->die_child = new_child;
5149 old_child->die_sib = NULL;
5152 /* Move all children from OLD_PARENT to NEW_PARENT. */
5154 static void
5155 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5157 dw_die_ref c;
5158 new_parent->die_child = old_parent->die_child;
5159 old_parent->die_child = NULL;
5160 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5163 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5164 matches TAG. */
5166 static void
5167 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5169 dw_die_ref c;
5171 c = die->die_child;
5172 if (c) do {
5173 dw_die_ref prev = c;
5174 c = c->die_sib;
5175 while (c->die_tag == tag)
5177 remove_child_with_prev (c, prev);
5178 c->die_parent = NULL;
5179 /* Might have removed every child. */
5180 if (die->die_child == NULL)
5181 return;
5182 c = prev->die_sib;
5184 } while (c != die->die_child);
5187 /* Add a CHILD_DIE as the last child of DIE. */
5189 static void
5190 add_child_die (dw_die_ref die, dw_die_ref child_die)
5192 /* FIXME this should probably be an assert. */
5193 if (! die || ! child_die)
5194 return;
5195 gcc_assert (die != child_die);
5197 child_die->die_parent = die;
5198 if (die->die_child)
5200 child_die->die_sib = die->die_child->die_sib;
5201 die->die_child->die_sib = child_die;
5203 else
5204 child_die->die_sib = child_die;
5205 die->die_child = child_die;
5208 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5210 static void
5211 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5212 dw_die_ref after_die)
5214 gcc_assert (die
5215 && child_die
5216 && after_die
5217 && die->die_child
5218 && die != child_die);
5220 child_die->die_parent = die;
5221 child_die->die_sib = after_die->die_sib;
5222 after_die->die_sib = child_die;
5223 if (die->die_child == after_die)
5224 die->die_child = child_die;
5227 /* Unassociate CHILD from its parent, and make its parent be
5228 NEW_PARENT. */
5230 static void
5231 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5233 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5234 if (p->die_sib == child)
5236 remove_child_with_prev (child, p);
5237 break;
5239 add_child_die (new_parent, child);
5242 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5243 is the specification, to the end of PARENT's list of children.
5244 This is done by removing and re-adding it. */
5246 static void
5247 splice_child_die (dw_die_ref parent, dw_die_ref child)
5249 /* We want the declaration DIE from inside the class, not the
5250 specification DIE at toplevel. */
5251 if (child->die_parent != parent)
5253 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5255 if (tmp)
5256 child = tmp;
5259 gcc_assert (child->die_parent == parent
5260 || (child->die_parent
5261 == get_AT_ref (parent, DW_AT_specification)));
5263 reparent_child (child, parent);
5266 /* Create and return a new die with a parent of PARENT_DIE. If
5267 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5268 associated tree T must be supplied to determine parenthood
5269 later. */
5271 static inline dw_die_ref
5272 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5274 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5276 die->die_tag = tag_value;
5278 if (parent_die != NULL)
5279 add_child_die (parent_die, die);
5280 else
5282 limbo_die_node *limbo_node;
5284 /* No DIEs created after early dwarf should end up in limbo,
5285 because the limbo list should not persist past LTO
5286 streaming. */
5287 if (tag_value != DW_TAG_compile_unit
5288 /* These are allowed because they're generated while
5289 breaking out COMDAT units late. */
5290 && tag_value != DW_TAG_type_unit
5291 && tag_value != DW_TAG_skeleton_unit
5292 && !early_dwarf
5293 /* Allow nested functions to live in limbo because they will
5294 only temporarily live there, as decls_for_scope will fix
5295 them up. */
5296 && (TREE_CODE (t) != FUNCTION_DECL
5297 || !decl_function_context (t))
5298 /* Same as nested functions above but for types. Types that
5299 are local to a function will be fixed in
5300 decls_for_scope. */
5301 && (!RECORD_OR_UNION_TYPE_P (t)
5302 || !TYPE_CONTEXT (t)
5303 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5304 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5305 especially in the ltrans stage, but once we implement LTO
5306 dwarf streaming, we should remove this exception. */
5307 && !in_lto_p)
5309 fprintf (stderr, "symbol ended up in limbo too late:");
5310 debug_generic_stmt (t);
5311 gcc_unreachable ();
5314 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5315 limbo_node->die = die;
5316 limbo_node->created_for = t;
5317 limbo_node->next = limbo_die_list;
5318 limbo_die_list = limbo_node;
5321 return die;
5324 /* Return the DIE associated with the given type specifier. */
5326 static inline dw_die_ref
5327 lookup_type_die (tree type)
5329 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5330 if (die && die->removed)
5332 TYPE_SYMTAB_DIE (type) = NULL;
5333 return NULL;
5335 return die;
5338 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5339 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5340 anonymous type instead the one of the naming typedef. */
5342 static inline dw_die_ref
5343 strip_naming_typedef (tree type, dw_die_ref type_die)
5345 if (type
5346 && TREE_CODE (type) == RECORD_TYPE
5347 && type_die
5348 && type_die->die_tag == DW_TAG_typedef
5349 && is_naming_typedef_decl (TYPE_NAME (type)))
5350 type_die = get_AT_ref (type_die, DW_AT_type);
5351 return type_die;
5354 /* Like lookup_type_die, but if type is an anonymous type named by a
5355 typedef[1], return the DIE of the anonymous type instead the one of
5356 the naming typedef. This is because in gen_typedef_die, we did
5357 equate the anonymous struct named by the typedef with the DIE of
5358 the naming typedef. So by default, lookup_type_die on an anonymous
5359 struct yields the DIE of the naming typedef.
5361 [1]: Read the comment of is_naming_typedef_decl to learn about what
5362 a naming typedef is. */
5364 static inline dw_die_ref
5365 lookup_type_die_strip_naming_typedef (tree type)
5367 dw_die_ref die = lookup_type_die (type);
5368 return strip_naming_typedef (type, die);
5371 /* Equate a DIE to a given type specifier. */
5373 static inline void
5374 equate_type_number_to_die (tree type, dw_die_ref type_die)
5376 TYPE_SYMTAB_DIE (type) = type_die;
5379 /* Returns a hash value for X (which really is a die_struct). */
5381 inline hashval_t
5382 decl_die_hasher::hash (die_node *x)
5384 return (hashval_t) x->decl_id;
5387 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5389 inline bool
5390 decl_die_hasher::equal (die_node *x, tree y)
5392 return (x->decl_id == DECL_UID (y));
5395 /* Return the DIE associated with a given declaration. */
5397 static inline dw_die_ref
5398 lookup_decl_die (tree decl)
5400 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5401 NO_INSERT);
5402 if (!die)
5403 return NULL;
5404 if ((*die)->removed)
5406 decl_die_table->clear_slot (die);
5407 return NULL;
5409 return *die;
5412 /* Returns a hash value for X (which really is a var_loc_list). */
5414 inline hashval_t
5415 decl_loc_hasher::hash (var_loc_list *x)
5417 return (hashval_t) x->decl_id;
5420 /* Return nonzero if decl_id of var_loc_list X is the same as
5421 UID of decl *Y. */
5423 inline bool
5424 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5426 return (x->decl_id == DECL_UID (y));
5429 /* Return the var_loc list associated with a given declaration. */
5431 static inline var_loc_list *
5432 lookup_decl_loc (const_tree decl)
5434 if (!decl_loc_table)
5435 return NULL;
5436 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5439 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5441 inline hashval_t
5442 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5444 return (hashval_t) x->decl_id;
5447 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5448 UID of decl *Y. */
5450 inline bool
5451 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5453 return (x->decl_id == DECL_UID (y));
5456 /* Equate a DIE to a particular declaration. */
5458 static void
5459 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5461 unsigned int decl_id = DECL_UID (decl);
5463 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5464 decl_die->decl_id = decl_id;
5467 /* Return how many bits covers PIECE EXPR_LIST. */
5469 static HOST_WIDE_INT
5470 decl_piece_bitsize (rtx piece)
5472 int ret = (int) GET_MODE (piece);
5473 if (ret)
5474 return ret;
5475 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5476 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5477 return INTVAL (XEXP (XEXP (piece, 0), 0));
5480 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5482 static rtx *
5483 decl_piece_varloc_ptr (rtx piece)
5485 if ((int) GET_MODE (piece))
5486 return &XEXP (piece, 0);
5487 else
5488 return &XEXP (XEXP (piece, 0), 1);
5491 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5492 Next is the chain of following piece nodes. */
5494 static rtx_expr_list *
5495 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5497 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5498 return alloc_EXPR_LIST (bitsize, loc_note, next);
5499 else
5500 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5501 GEN_INT (bitsize),
5502 loc_note), next);
5505 /* Return rtx that should be stored into loc field for
5506 LOC_NOTE and BITPOS/BITSIZE. */
5508 static rtx
5509 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5510 HOST_WIDE_INT bitsize)
5512 if (bitsize != -1)
5514 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5515 if (bitpos != 0)
5516 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5518 return loc_note;
5521 /* This function either modifies location piece list *DEST in
5522 place (if SRC and INNER is NULL), or copies location piece list
5523 *SRC to *DEST while modifying it. Location BITPOS is modified
5524 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5525 not copied and if needed some padding around it is added.
5526 When modifying in place, DEST should point to EXPR_LIST where
5527 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5528 to the start of the whole list and INNER points to the EXPR_LIST
5529 where earlier pieces cover PIECE_BITPOS bits. */
5531 static void
5532 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5533 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5534 HOST_WIDE_INT bitsize, rtx loc_note)
5536 HOST_WIDE_INT diff;
5537 bool copy = inner != NULL;
5539 if (copy)
5541 /* First copy all nodes preceding the current bitpos. */
5542 while (src != inner)
5544 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5545 decl_piece_bitsize (*src), NULL_RTX);
5546 dest = &XEXP (*dest, 1);
5547 src = &XEXP (*src, 1);
5550 /* Add padding if needed. */
5551 if (bitpos != piece_bitpos)
5553 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5554 copy ? NULL_RTX : *dest);
5555 dest = &XEXP (*dest, 1);
5557 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5559 gcc_assert (!copy);
5560 /* A piece with correct bitpos and bitsize already exist,
5561 just update the location for it and return. */
5562 *decl_piece_varloc_ptr (*dest) = loc_note;
5563 return;
5565 /* Add the piece that changed. */
5566 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5567 dest = &XEXP (*dest, 1);
5568 /* Skip over pieces that overlap it. */
5569 diff = bitpos - piece_bitpos + bitsize;
5570 if (!copy)
5571 src = dest;
5572 while (diff > 0 && *src)
5574 rtx piece = *src;
5575 diff -= decl_piece_bitsize (piece);
5576 if (copy)
5577 src = &XEXP (piece, 1);
5578 else
5580 *src = XEXP (piece, 1);
5581 free_EXPR_LIST_node (piece);
5584 /* Add padding if needed. */
5585 if (diff < 0 && *src)
5587 if (!copy)
5588 dest = src;
5589 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5590 dest = &XEXP (*dest, 1);
5592 if (!copy)
5593 return;
5594 /* Finally copy all nodes following it. */
5595 while (*src)
5597 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5598 decl_piece_bitsize (*src), NULL_RTX);
5599 dest = &XEXP (*dest, 1);
5600 src = &XEXP (*src, 1);
5604 /* Add a variable location node to the linked list for DECL. */
5606 static struct var_loc_node *
5607 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5609 unsigned int decl_id;
5610 var_loc_list *temp;
5611 struct var_loc_node *loc = NULL;
5612 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5614 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5616 tree realdecl = DECL_DEBUG_EXPR (decl);
5617 if (handled_component_p (realdecl)
5618 || (TREE_CODE (realdecl) == MEM_REF
5619 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5621 HOST_WIDE_INT maxsize;
5622 bool reverse;
5623 tree innerdecl
5624 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5625 &reverse);
5626 if (!DECL_P (innerdecl)
5627 || DECL_IGNORED_P (innerdecl)
5628 || TREE_STATIC (innerdecl)
5629 || bitsize <= 0
5630 || bitpos + bitsize > 256
5631 || bitsize != maxsize)
5632 return NULL;
5633 decl = innerdecl;
5637 decl_id = DECL_UID (decl);
5638 var_loc_list **slot
5639 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5640 if (*slot == NULL)
5642 temp = ggc_cleared_alloc<var_loc_list> ();
5643 temp->decl_id = decl_id;
5644 *slot = temp;
5646 else
5647 temp = *slot;
5649 /* For PARM_DECLs try to keep around the original incoming value,
5650 even if that means we'll emit a zero-range .debug_loc entry. */
5651 if (temp->last
5652 && temp->first == temp->last
5653 && TREE_CODE (decl) == PARM_DECL
5654 && NOTE_P (temp->first->loc)
5655 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5656 && DECL_INCOMING_RTL (decl)
5657 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5658 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5659 == GET_CODE (DECL_INCOMING_RTL (decl))
5660 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5661 && (bitsize != -1
5662 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5663 NOTE_VAR_LOCATION_LOC (loc_note))
5664 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5665 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5667 loc = ggc_cleared_alloc<var_loc_node> ();
5668 temp->first->next = loc;
5669 temp->last = loc;
5670 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5672 else if (temp->last)
5674 struct var_loc_node *last = temp->last, *unused = NULL;
5675 rtx *piece_loc = NULL, last_loc_note;
5676 HOST_WIDE_INT piece_bitpos = 0;
5677 if (last->next)
5679 last = last->next;
5680 gcc_assert (last->next == NULL);
5682 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5684 piece_loc = &last->loc;
5687 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5688 if (piece_bitpos + cur_bitsize > bitpos)
5689 break;
5690 piece_bitpos += cur_bitsize;
5691 piece_loc = &XEXP (*piece_loc, 1);
5693 while (*piece_loc);
5695 /* TEMP->LAST here is either pointer to the last but one or
5696 last element in the chained list, LAST is pointer to the
5697 last element. */
5698 if (label && strcmp (last->label, label) == 0)
5700 /* For SRA optimized variables if there weren't any real
5701 insns since last note, just modify the last node. */
5702 if (piece_loc != NULL)
5704 adjust_piece_list (piece_loc, NULL, NULL,
5705 bitpos, piece_bitpos, bitsize, loc_note);
5706 return NULL;
5708 /* If the last note doesn't cover any instructions, remove it. */
5709 if (temp->last != last)
5711 temp->last->next = NULL;
5712 unused = last;
5713 last = temp->last;
5714 gcc_assert (strcmp (last->label, label) != 0);
5716 else
5718 gcc_assert (temp->first == temp->last
5719 || (temp->first->next == temp->last
5720 && TREE_CODE (decl) == PARM_DECL));
5721 memset (temp->last, '\0', sizeof (*temp->last));
5722 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5723 return temp->last;
5726 if (bitsize == -1 && NOTE_P (last->loc))
5727 last_loc_note = last->loc;
5728 else if (piece_loc != NULL
5729 && *piece_loc != NULL_RTX
5730 && piece_bitpos == bitpos
5731 && decl_piece_bitsize (*piece_loc) == bitsize)
5732 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5733 else
5734 last_loc_note = NULL_RTX;
5735 /* If the current location is the same as the end of the list,
5736 and either both or neither of the locations is uninitialized,
5737 we have nothing to do. */
5738 if (last_loc_note == NULL_RTX
5739 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5740 NOTE_VAR_LOCATION_LOC (loc_note)))
5741 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5742 != NOTE_VAR_LOCATION_STATUS (loc_note))
5743 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5744 == VAR_INIT_STATUS_UNINITIALIZED)
5745 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5746 == VAR_INIT_STATUS_UNINITIALIZED))))
5748 /* Add LOC to the end of list and update LAST. If the last
5749 element of the list has been removed above, reuse its
5750 memory for the new node, otherwise allocate a new one. */
5751 if (unused)
5753 loc = unused;
5754 memset (loc, '\0', sizeof (*loc));
5756 else
5757 loc = ggc_cleared_alloc<var_loc_node> ();
5758 if (bitsize == -1 || piece_loc == NULL)
5759 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5760 else
5761 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5762 bitpos, piece_bitpos, bitsize, loc_note);
5763 last->next = loc;
5764 /* Ensure TEMP->LAST will point either to the new last but one
5765 element of the chain, or to the last element in it. */
5766 if (last != temp->last)
5767 temp->last = last;
5769 else if (unused)
5770 ggc_free (unused);
5772 else
5774 loc = ggc_cleared_alloc<var_loc_node> ();
5775 temp->first = loc;
5776 temp->last = loc;
5777 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5779 return loc;
5782 /* Keep track of the number of spaces used to indent the
5783 output of the debugging routines that print the structure of
5784 the DIE internal representation. */
5785 static int print_indent;
5787 /* Indent the line the number of spaces given by print_indent. */
5789 static inline void
5790 print_spaces (FILE *outfile)
5792 fprintf (outfile, "%*s", print_indent, "");
5795 /* Print a type signature in hex. */
5797 static inline void
5798 print_signature (FILE *outfile, char *sig)
5800 int i;
5802 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5803 fprintf (outfile, "%02x", sig[i] & 0xff);
5806 static inline void
5807 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5809 if (discr_value->pos)
5810 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5811 else
5812 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5815 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5817 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5818 RECURSE, output location descriptor operations. */
5820 static void
5821 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5823 switch (val->val_class)
5825 case dw_val_class_addr:
5826 fprintf (outfile, "address");
5827 break;
5828 case dw_val_class_offset:
5829 fprintf (outfile, "offset");
5830 break;
5831 case dw_val_class_loc:
5832 fprintf (outfile, "location descriptor");
5833 if (val->v.val_loc == NULL)
5834 fprintf (outfile, " -> <null>\n");
5835 else if (recurse)
5837 fprintf (outfile, ":\n");
5838 print_indent += 4;
5839 print_loc_descr (val->v.val_loc, outfile);
5840 print_indent -= 4;
5842 else
5843 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5844 break;
5845 case dw_val_class_loc_list:
5846 fprintf (outfile, "location list -> label:%s",
5847 val->v.val_loc_list->ll_symbol);
5848 break;
5849 case dw_val_class_range_list:
5850 fprintf (outfile, "range list");
5851 break;
5852 case dw_val_class_const:
5853 case dw_val_class_const_implicit:
5854 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5855 break;
5856 case dw_val_class_unsigned_const:
5857 case dw_val_class_unsigned_const_implicit:
5858 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5859 break;
5860 case dw_val_class_const_double:
5861 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5862 HOST_WIDE_INT_PRINT_UNSIGNED")",
5863 val->v.val_double.high,
5864 val->v.val_double.low);
5865 break;
5866 case dw_val_class_wide_int:
5868 int i = val->v.val_wide->get_len ();
5869 fprintf (outfile, "constant (");
5870 gcc_assert (i > 0);
5871 if (val->v.val_wide->elt (i - 1) == 0)
5872 fprintf (outfile, "0x");
5873 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5874 val->v.val_wide->elt (--i));
5875 while (--i >= 0)
5876 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5877 val->v.val_wide->elt (i));
5878 fprintf (outfile, ")");
5879 break;
5881 case dw_val_class_vec:
5882 fprintf (outfile, "floating-point or vector constant");
5883 break;
5884 case dw_val_class_flag:
5885 fprintf (outfile, "%u", val->v.val_flag);
5886 break;
5887 case dw_val_class_die_ref:
5888 if (val->v.val_die_ref.die != NULL)
5890 dw_die_ref die = val->v.val_die_ref.die;
5892 if (die->comdat_type_p)
5894 fprintf (outfile, "die -> signature: ");
5895 print_signature (outfile,
5896 die->die_id.die_type_node->signature);
5898 else if (die->die_id.die_symbol)
5899 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5900 else
5901 fprintf (outfile, "die -> %ld", die->die_offset);
5902 fprintf (outfile, " (%p)", (void *) die);
5904 else
5905 fprintf (outfile, "die -> <null>");
5906 break;
5907 case dw_val_class_vms_delta:
5908 fprintf (outfile, "delta: @slotcount(%s-%s)",
5909 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5910 break;
5911 case dw_val_class_lbl_id:
5912 case dw_val_class_lineptr:
5913 case dw_val_class_macptr:
5914 case dw_val_class_loclistsptr:
5915 case dw_val_class_high_pc:
5916 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5917 break;
5918 case dw_val_class_str:
5919 if (val->v.val_str->str != NULL)
5920 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5921 else
5922 fprintf (outfile, "<null>");
5923 break;
5924 case dw_val_class_file:
5925 case dw_val_class_file_implicit:
5926 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5927 val->v.val_file->emitted_number);
5928 break;
5929 case dw_val_class_data8:
5931 int i;
5933 for (i = 0; i < 8; i++)
5934 fprintf (outfile, "%02x", val->v.val_data8[i]);
5935 break;
5937 case dw_val_class_discr_value:
5938 print_discr_value (outfile, &val->v.val_discr_value);
5939 break;
5940 case dw_val_class_discr_list:
5941 for (dw_discr_list_ref node = val->v.val_discr_list;
5942 node != NULL;
5943 node = node->dw_discr_next)
5945 if (node->dw_discr_range)
5947 fprintf (outfile, " .. ");
5948 print_discr_value (outfile, &node->dw_discr_lower_bound);
5949 print_discr_value (outfile, &node->dw_discr_upper_bound);
5951 else
5952 print_discr_value (outfile, &node->dw_discr_lower_bound);
5954 if (node->dw_discr_next != NULL)
5955 fprintf (outfile, " | ");
5957 default:
5958 break;
5962 /* Likewise, for a DIE attribute. */
5964 static void
5965 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5967 print_dw_val (&a->dw_attr_val, recurse, outfile);
5971 /* Print the list of operands in the LOC location description to OUTFILE. This
5972 routine is a debugging aid only. */
5974 static void
5975 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5977 dw_loc_descr_ref l = loc;
5979 if (loc == NULL)
5981 print_spaces (outfile);
5982 fprintf (outfile, "<null>\n");
5983 return;
5986 for (l = loc; l != NULL; l = l->dw_loc_next)
5988 print_spaces (outfile);
5989 fprintf (outfile, "(%p) %s",
5990 (void *) l,
5991 dwarf_stack_op_name (l->dw_loc_opc));
5992 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5994 fprintf (outfile, " ");
5995 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5997 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5999 fprintf (outfile, ", ");
6000 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6002 fprintf (outfile, "\n");
6006 /* Print the information associated with a given DIE, and its children.
6007 This routine is a debugging aid only. */
6009 static void
6010 print_die (dw_die_ref die, FILE *outfile)
6012 dw_attr_node *a;
6013 dw_die_ref c;
6014 unsigned ix;
6016 print_spaces (outfile);
6017 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6018 die->die_offset, dwarf_tag_name (die->die_tag),
6019 (void*) die);
6020 print_spaces (outfile);
6021 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6022 fprintf (outfile, " offset: %ld", die->die_offset);
6023 fprintf (outfile, " mark: %d\n", die->die_mark);
6025 if (die->comdat_type_p)
6027 print_spaces (outfile);
6028 fprintf (outfile, " signature: ");
6029 print_signature (outfile, die->die_id.die_type_node->signature);
6030 fprintf (outfile, "\n");
6033 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6035 print_spaces (outfile);
6036 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6038 print_attribute (a, true, outfile);
6039 fprintf (outfile, "\n");
6042 if (die->die_child != NULL)
6044 print_indent += 4;
6045 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6046 print_indent -= 4;
6048 if (print_indent == 0)
6049 fprintf (outfile, "\n");
6052 /* Print the list of operations in the LOC location description. */
6054 DEBUG_FUNCTION void
6055 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6057 print_loc_descr (loc, stderr);
6060 /* Print the information collected for a given DIE. */
6062 DEBUG_FUNCTION void
6063 debug_dwarf_die (dw_die_ref die)
6065 print_die (die, stderr);
6068 DEBUG_FUNCTION void
6069 debug (die_struct &ref)
6071 print_die (&ref, stderr);
6074 DEBUG_FUNCTION void
6075 debug (die_struct *ptr)
6077 if (ptr)
6078 debug (*ptr);
6079 else
6080 fprintf (stderr, "<nil>\n");
6084 /* Print all DWARF information collected for the compilation unit.
6085 This routine is a debugging aid only. */
6087 DEBUG_FUNCTION void
6088 debug_dwarf (void)
6090 print_indent = 0;
6091 print_die (comp_unit_die (), stderr);
6094 /* Verify the DIE tree structure. */
6096 DEBUG_FUNCTION void
6097 verify_die (dw_die_ref die)
6099 gcc_assert (!die->die_mark);
6100 if (die->die_parent == NULL
6101 && die->die_sib == NULL)
6102 return;
6103 /* Verify the die_sib list is cyclic. */
6104 dw_die_ref x = die;
6107 x->die_mark = 1;
6108 x = x->die_sib;
6110 while (x && !x->die_mark);
6111 gcc_assert (x == die);
6112 x = die;
6115 /* Verify all dies have the same parent. */
6116 gcc_assert (x->die_parent == die->die_parent);
6117 if (x->die_child)
6119 /* Verify the child has the proper parent and recurse. */
6120 gcc_assert (x->die_child->die_parent == x);
6121 verify_die (x->die_child);
6123 x->die_mark = 0;
6124 x = x->die_sib;
6126 while (x && x->die_mark);
6129 /* Sanity checks on DIEs. */
6131 static void
6132 check_die (dw_die_ref die)
6134 unsigned ix;
6135 dw_attr_node *a;
6136 bool inline_found = false;
6137 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6138 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6139 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6141 switch (a->dw_attr)
6143 case DW_AT_inline:
6144 if (a->dw_attr_val.v.val_unsigned)
6145 inline_found = true;
6146 break;
6147 case DW_AT_location:
6148 ++n_location;
6149 break;
6150 case DW_AT_low_pc:
6151 ++n_low_pc;
6152 break;
6153 case DW_AT_high_pc:
6154 ++n_high_pc;
6155 break;
6156 case DW_AT_artificial:
6157 ++n_artificial;
6158 break;
6159 case DW_AT_decl_column:
6160 ++n_decl_column;
6161 break;
6162 case DW_AT_decl_line:
6163 ++n_decl_line;
6164 break;
6165 case DW_AT_decl_file:
6166 ++n_decl_file;
6167 break;
6168 default:
6169 break;
6172 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6173 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6175 fprintf (stderr, "Duplicate attributes in DIE:\n");
6176 debug_dwarf_die (die);
6177 gcc_unreachable ();
6179 if (inline_found)
6181 /* A debugging information entry that is a member of an abstract
6182 instance tree [that has DW_AT_inline] should not contain any
6183 attributes which describe aspects of the subroutine which vary
6184 between distinct inlined expansions or distinct out-of-line
6185 expansions. */
6186 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6187 gcc_assert (a->dw_attr != DW_AT_low_pc
6188 && a->dw_attr != DW_AT_high_pc
6189 && a->dw_attr != DW_AT_location
6190 && a->dw_attr != DW_AT_frame_base
6191 && a->dw_attr != DW_AT_call_all_calls
6192 && a->dw_attr != DW_AT_GNU_all_call_sites);
6196 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6197 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6198 DIE that marks the start of the DIEs for this include file. */
6200 static dw_die_ref
6201 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6203 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6204 dw_die_ref new_unit = gen_compile_unit_die (filename);
6206 new_unit->die_sib = old_unit;
6207 return new_unit;
6210 /* Close an include-file CU and reopen the enclosing one. */
6212 static dw_die_ref
6213 pop_compile_unit (dw_die_ref old_unit)
6215 dw_die_ref new_unit = old_unit->die_sib;
6217 old_unit->die_sib = NULL;
6218 return new_unit;
6221 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6222 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6223 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6225 /* Calculate the checksum of a location expression. */
6227 static inline void
6228 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6230 int tem;
6231 inchash::hash hstate;
6232 hashval_t hash;
6234 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6235 CHECKSUM (tem);
6236 hash_loc_operands (loc, hstate);
6237 hash = hstate.end();
6238 CHECKSUM (hash);
6241 /* Calculate the checksum of an attribute. */
6243 static void
6244 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6246 dw_loc_descr_ref loc;
6247 rtx r;
6249 CHECKSUM (at->dw_attr);
6251 /* We don't care that this was compiled with a different compiler
6252 snapshot; if the output is the same, that's what matters. */
6253 if (at->dw_attr == DW_AT_producer)
6254 return;
6256 switch (AT_class (at))
6258 case dw_val_class_const:
6259 case dw_val_class_const_implicit:
6260 CHECKSUM (at->dw_attr_val.v.val_int);
6261 break;
6262 case dw_val_class_unsigned_const:
6263 case dw_val_class_unsigned_const_implicit:
6264 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6265 break;
6266 case dw_val_class_const_double:
6267 CHECKSUM (at->dw_attr_val.v.val_double);
6268 break;
6269 case dw_val_class_wide_int:
6270 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6271 get_full_len (*at->dw_attr_val.v.val_wide)
6272 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6273 break;
6274 case dw_val_class_vec:
6275 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6276 (at->dw_attr_val.v.val_vec.length
6277 * at->dw_attr_val.v.val_vec.elt_size));
6278 break;
6279 case dw_val_class_flag:
6280 CHECKSUM (at->dw_attr_val.v.val_flag);
6281 break;
6282 case dw_val_class_str:
6283 CHECKSUM_STRING (AT_string (at));
6284 break;
6286 case dw_val_class_addr:
6287 r = AT_addr (at);
6288 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6289 CHECKSUM_STRING (XSTR (r, 0));
6290 break;
6292 case dw_val_class_offset:
6293 CHECKSUM (at->dw_attr_val.v.val_offset);
6294 break;
6296 case dw_val_class_loc:
6297 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6298 loc_checksum (loc, ctx);
6299 break;
6301 case dw_val_class_die_ref:
6302 die_checksum (AT_ref (at), ctx, mark);
6303 break;
6305 case dw_val_class_fde_ref:
6306 case dw_val_class_vms_delta:
6307 case dw_val_class_lbl_id:
6308 case dw_val_class_lineptr:
6309 case dw_val_class_macptr:
6310 case dw_val_class_loclistsptr:
6311 case dw_val_class_high_pc:
6312 break;
6314 case dw_val_class_file:
6315 case dw_val_class_file_implicit:
6316 CHECKSUM_STRING (AT_file (at)->filename);
6317 break;
6319 case dw_val_class_data8:
6320 CHECKSUM (at->dw_attr_val.v.val_data8);
6321 break;
6323 default:
6324 break;
6328 /* Calculate the checksum of a DIE. */
6330 static void
6331 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6333 dw_die_ref c;
6334 dw_attr_node *a;
6335 unsigned ix;
6337 /* To avoid infinite recursion. */
6338 if (die->die_mark)
6340 CHECKSUM (die->die_mark);
6341 return;
6343 die->die_mark = ++(*mark);
6345 CHECKSUM (die->die_tag);
6347 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6348 attr_checksum (a, ctx, mark);
6350 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6353 #undef CHECKSUM
6354 #undef CHECKSUM_BLOCK
6355 #undef CHECKSUM_STRING
6357 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6358 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6359 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6360 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6361 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6362 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6363 #define CHECKSUM_ATTR(FOO) \
6364 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6366 /* Calculate the checksum of a number in signed LEB128 format. */
6368 static void
6369 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6371 unsigned char byte;
6372 bool more;
6374 while (1)
6376 byte = (value & 0x7f);
6377 value >>= 7;
6378 more = !((value == 0 && (byte & 0x40) == 0)
6379 || (value == -1 && (byte & 0x40) != 0));
6380 if (more)
6381 byte |= 0x80;
6382 CHECKSUM (byte);
6383 if (!more)
6384 break;
6388 /* Calculate the checksum of a number in unsigned LEB128 format. */
6390 static void
6391 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6393 while (1)
6395 unsigned char byte = (value & 0x7f);
6396 value >>= 7;
6397 if (value != 0)
6398 /* More bytes to follow. */
6399 byte |= 0x80;
6400 CHECKSUM (byte);
6401 if (value == 0)
6402 break;
6406 /* Checksum the context of the DIE. This adds the names of any
6407 surrounding namespaces or structures to the checksum. */
6409 static void
6410 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6412 const char *name;
6413 dw_die_ref spec;
6414 int tag = die->die_tag;
6416 if (tag != DW_TAG_namespace
6417 && tag != DW_TAG_structure_type
6418 && tag != DW_TAG_class_type)
6419 return;
6421 name = get_AT_string (die, DW_AT_name);
6423 spec = get_AT_ref (die, DW_AT_specification);
6424 if (spec != NULL)
6425 die = spec;
6427 if (die->die_parent != NULL)
6428 checksum_die_context (die->die_parent, ctx);
6430 CHECKSUM_ULEB128 ('C');
6431 CHECKSUM_ULEB128 (tag);
6432 if (name != NULL)
6433 CHECKSUM_STRING (name);
6436 /* Calculate the checksum of a location expression. */
6438 static inline void
6439 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6441 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6442 were emitted as a DW_FORM_sdata instead of a location expression. */
6443 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6445 CHECKSUM_ULEB128 (DW_FORM_sdata);
6446 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6447 return;
6450 /* Otherwise, just checksum the raw location expression. */
6451 while (loc != NULL)
6453 inchash::hash hstate;
6454 hashval_t hash;
6456 CHECKSUM_ULEB128 (loc->dtprel);
6457 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6458 hash_loc_operands (loc, hstate);
6459 hash = hstate.end ();
6460 CHECKSUM (hash);
6461 loc = loc->dw_loc_next;
6465 /* Calculate the checksum of an attribute. */
6467 static void
6468 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6469 struct md5_ctx *ctx, int *mark)
6471 dw_loc_descr_ref loc;
6472 rtx r;
6474 if (AT_class (at) == dw_val_class_die_ref)
6476 dw_die_ref target_die = AT_ref (at);
6478 /* For pointer and reference types, we checksum only the (qualified)
6479 name of the target type (if there is a name). For friend entries,
6480 we checksum only the (qualified) name of the target type or function.
6481 This allows the checksum to remain the same whether the target type
6482 is complete or not. */
6483 if ((at->dw_attr == DW_AT_type
6484 && (tag == DW_TAG_pointer_type
6485 || tag == DW_TAG_reference_type
6486 || tag == DW_TAG_rvalue_reference_type
6487 || tag == DW_TAG_ptr_to_member_type))
6488 || (at->dw_attr == DW_AT_friend
6489 && tag == DW_TAG_friend))
6491 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6493 if (name_attr != NULL)
6495 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6497 if (decl == NULL)
6498 decl = target_die;
6499 CHECKSUM_ULEB128 ('N');
6500 CHECKSUM_ULEB128 (at->dw_attr);
6501 if (decl->die_parent != NULL)
6502 checksum_die_context (decl->die_parent, ctx);
6503 CHECKSUM_ULEB128 ('E');
6504 CHECKSUM_STRING (AT_string (name_attr));
6505 return;
6509 /* For all other references to another DIE, we check to see if the
6510 target DIE has already been visited. If it has, we emit a
6511 backward reference; if not, we descend recursively. */
6512 if (target_die->die_mark > 0)
6514 CHECKSUM_ULEB128 ('R');
6515 CHECKSUM_ULEB128 (at->dw_attr);
6516 CHECKSUM_ULEB128 (target_die->die_mark);
6518 else
6520 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6522 if (decl == NULL)
6523 decl = target_die;
6524 target_die->die_mark = ++(*mark);
6525 CHECKSUM_ULEB128 ('T');
6526 CHECKSUM_ULEB128 (at->dw_attr);
6527 if (decl->die_parent != NULL)
6528 checksum_die_context (decl->die_parent, ctx);
6529 die_checksum_ordered (target_die, ctx, mark);
6531 return;
6534 CHECKSUM_ULEB128 ('A');
6535 CHECKSUM_ULEB128 (at->dw_attr);
6537 switch (AT_class (at))
6539 case dw_val_class_const:
6540 case dw_val_class_const_implicit:
6541 CHECKSUM_ULEB128 (DW_FORM_sdata);
6542 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6543 break;
6545 case dw_val_class_unsigned_const:
6546 case dw_val_class_unsigned_const_implicit:
6547 CHECKSUM_ULEB128 (DW_FORM_sdata);
6548 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6549 break;
6551 case dw_val_class_const_double:
6552 CHECKSUM_ULEB128 (DW_FORM_block);
6553 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6554 CHECKSUM (at->dw_attr_val.v.val_double);
6555 break;
6557 case dw_val_class_wide_int:
6558 CHECKSUM_ULEB128 (DW_FORM_block);
6559 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6560 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6561 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6562 get_full_len (*at->dw_attr_val.v.val_wide)
6563 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6564 break;
6566 case dw_val_class_vec:
6567 CHECKSUM_ULEB128 (DW_FORM_block);
6568 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6569 * at->dw_attr_val.v.val_vec.elt_size);
6570 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6571 (at->dw_attr_val.v.val_vec.length
6572 * at->dw_attr_val.v.val_vec.elt_size));
6573 break;
6575 case dw_val_class_flag:
6576 CHECKSUM_ULEB128 (DW_FORM_flag);
6577 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6578 break;
6580 case dw_val_class_str:
6581 CHECKSUM_ULEB128 (DW_FORM_string);
6582 CHECKSUM_STRING (AT_string (at));
6583 break;
6585 case dw_val_class_addr:
6586 r = AT_addr (at);
6587 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6588 CHECKSUM_ULEB128 (DW_FORM_string);
6589 CHECKSUM_STRING (XSTR (r, 0));
6590 break;
6592 case dw_val_class_offset:
6593 CHECKSUM_ULEB128 (DW_FORM_sdata);
6594 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6595 break;
6597 case dw_val_class_loc:
6598 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6599 loc_checksum_ordered (loc, ctx);
6600 break;
6602 case dw_val_class_fde_ref:
6603 case dw_val_class_lbl_id:
6604 case dw_val_class_lineptr:
6605 case dw_val_class_macptr:
6606 case dw_val_class_loclistsptr:
6607 case dw_val_class_high_pc:
6608 break;
6610 case dw_val_class_file:
6611 case dw_val_class_file_implicit:
6612 CHECKSUM_ULEB128 (DW_FORM_string);
6613 CHECKSUM_STRING (AT_file (at)->filename);
6614 break;
6616 case dw_val_class_data8:
6617 CHECKSUM (at->dw_attr_val.v.val_data8);
6618 break;
6620 default:
6621 break;
6625 struct checksum_attributes
6627 dw_attr_node *at_name;
6628 dw_attr_node *at_type;
6629 dw_attr_node *at_friend;
6630 dw_attr_node *at_accessibility;
6631 dw_attr_node *at_address_class;
6632 dw_attr_node *at_alignment;
6633 dw_attr_node *at_allocated;
6634 dw_attr_node *at_artificial;
6635 dw_attr_node *at_associated;
6636 dw_attr_node *at_binary_scale;
6637 dw_attr_node *at_bit_offset;
6638 dw_attr_node *at_bit_size;
6639 dw_attr_node *at_bit_stride;
6640 dw_attr_node *at_byte_size;
6641 dw_attr_node *at_byte_stride;
6642 dw_attr_node *at_const_value;
6643 dw_attr_node *at_containing_type;
6644 dw_attr_node *at_count;
6645 dw_attr_node *at_data_location;
6646 dw_attr_node *at_data_member_location;
6647 dw_attr_node *at_decimal_scale;
6648 dw_attr_node *at_decimal_sign;
6649 dw_attr_node *at_default_value;
6650 dw_attr_node *at_digit_count;
6651 dw_attr_node *at_discr;
6652 dw_attr_node *at_discr_list;
6653 dw_attr_node *at_discr_value;
6654 dw_attr_node *at_encoding;
6655 dw_attr_node *at_endianity;
6656 dw_attr_node *at_explicit;
6657 dw_attr_node *at_is_optional;
6658 dw_attr_node *at_location;
6659 dw_attr_node *at_lower_bound;
6660 dw_attr_node *at_mutable;
6661 dw_attr_node *at_ordering;
6662 dw_attr_node *at_picture_string;
6663 dw_attr_node *at_prototyped;
6664 dw_attr_node *at_small;
6665 dw_attr_node *at_segment;
6666 dw_attr_node *at_string_length;
6667 dw_attr_node *at_string_length_bit_size;
6668 dw_attr_node *at_string_length_byte_size;
6669 dw_attr_node *at_threads_scaled;
6670 dw_attr_node *at_upper_bound;
6671 dw_attr_node *at_use_location;
6672 dw_attr_node *at_use_UTF8;
6673 dw_attr_node *at_variable_parameter;
6674 dw_attr_node *at_virtuality;
6675 dw_attr_node *at_visibility;
6676 dw_attr_node *at_vtable_elem_location;
6679 /* Collect the attributes that we will want to use for the checksum. */
6681 static void
6682 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6684 dw_attr_node *a;
6685 unsigned ix;
6687 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6689 switch (a->dw_attr)
6691 case DW_AT_name:
6692 attrs->at_name = a;
6693 break;
6694 case DW_AT_type:
6695 attrs->at_type = a;
6696 break;
6697 case DW_AT_friend:
6698 attrs->at_friend = a;
6699 break;
6700 case DW_AT_accessibility:
6701 attrs->at_accessibility = a;
6702 break;
6703 case DW_AT_address_class:
6704 attrs->at_address_class = a;
6705 break;
6706 case DW_AT_alignment:
6707 attrs->at_alignment = a;
6708 break;
6709 case DW_AT_allocated:
6710 attrs->at_allocated = a;
6711 break;
6712 case DW_AT_artificial:
6713 attrs->at_artificial = a;
6714 break;
6715 case DW_AT_associated:
6716 attrs->at_associated = a;
6717 break;
6718 case DW_AT_binary_scale:
6719 attrs->at_binary_scale = a;
6720 break;
6721 case DW_AT_bit_offset:
6722 attrs->at_bit_offset = a;
6723 break;
6724 case DW_AT_bit_size:
6725 attrs->at_bit_size = a;
6726 break;
6727 case DW_AT_bit_stride:
6728 attrs->at_bit_stride = a;
6729 break;
6730 case DW_AT_byte_size:
6731 attrs->at_byte_size = a;
6732 break;
6733 case DW_AT_byte_stride:
6734 attrs->at_byte_stride = a;
6735 break;
6736 case DW_AT_const_value:
6737 attrs->at_const_value = a;
6738 break;
6739 case DW_AT_containing_type:
6740 attrs->at_containing_type = a;
6741 break;
6742 case DW_AT_count:
6743 attrs->at_count = a;
6744 break;
6745 case DW_AT_data_location:
6746 attrs->at_data_location = a;
6747 break;
6748 case DW_AT_data_member_location:
6749 attrs->at_data_member_location = a;
6750 break;
6751 case DW_AT_decimal_scale:
6752 attrs->at_decimal_scale = a;
6753 break;
6754 case DW_AT_decimal_sign:
6755 attrs->at_decimal_sign = a;
6756 break;
6757 case DW_AT_default_value:
6758 attrs->at_default_value = a;
6759 break;
6760 case DW_AT_digit_count:
6761 attrs->at_digit_count = a;
6762 break;
6763 case DW_AT_discr:
6764 attrs->at_discr = a;
6765 break;
6766 case DW_AT_discr_list:
6767 attrs->at_discr_list = a;
6768 break;
6769 case DW_AT_discr_value:
6770 attrs->at_discr_value = a;
6771 break;
6772 case DW_AT_encoding:
6773 attrs->at_encoding = a;
6774 break;
6775 case DW_AT_endianity:
6776 attrs->at_endianity = a;
6777 break;
6778 case DW_AT_explicit:
6779 attrs->at_explicit = a;
6780 break;
6781 case DW_AT_is_optional:
6782 attrs->at_is_optional = a;
6783 break;
6784 case DW_AT_location:
6785 attrs->at_location = a;
6786 break;
6787 case DW_AT_lower_bound:
6788 attrs->at_lower_bound = a;
6789 break;
6790 case DW_AT_mutable:
6791 attrs->at_mutable = a;
6792 break;
6793 case DW_AT_ordering:
6794 attrs->at_ordering = a;
6795 break;
6796 case DW_AT_picture_string:
6797 attrs->at_picture_string = a;
6798 break;
6799 case DW_AT_prototyped:
6800 attrs->at_prototyped = a;
6801 break;
6802 case DW_AT_small:
6803 attrs->at_small = a;
6804 break;
6805 case DW_AT_segment:
6806 attrs->at_segment = a;
6807 break;
6808 case DW_AT_string_length:
6809 attrs->at_string_length = a;
6810 break;
6811 case DW_AT_string_length_bit_size:
6812 attrs->at_string_length_bit_size = a;
6813 break;
6814 case DW_AT_string_length_byte_size:
6815 attrs->at_string_length_byte_size = a;
6816 break;
6817 case DW_AT_threads_scaled:
6818 attrs->at_threads_scaled = a;
6819 break;
6820 case DW_AT_upper_bound:
6821 attrs->at_upper_bound = a;
6822 break;
6823 case DW_AT_use_location:
6824 attrs->at_use_location = a;
6825 break;
6826 case DW_AT_use_UTF8:
6827 attrs->at_use_UTF8 = a;
6828 break;
6829 case DW_AT_variable_parameter:
6830 attrs->at_variable_parameter = a;
6831 break;
6832 case DW_AT_virtuality:
6833 attrs->at_virtuality = a;
6834 break;
6835 case DW_AT_visibility:
6836 attrs->at_visibility = a;
6837 break;
6838 case DW_AT_vtable_elem_location:
6839 attrs->at_vtable_elem_location = a;
6840 break;
6841 default:
6842 break;
6847 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6849 static void
6850 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6852 dw_die_ref c;
6853 dw_die_ref decl;
6854 struct checksum_attributes attrs;
6856 CHECKSUM_ULEB128 ('D');
6857 CHECKSUM_ULEB128 (die->die_tag);
6859 memset (&attrs, 0, sizeof (attrs));
6861 decl = get_AT_ref (die, DW_AT_specification);
6862 if (decl != NULL)
6863 collect_checksum_attributes (&attrs, decl);
6864 collect_checksum_attributes (&attrs, die);
6866 CHECKSUM_ATTR (attrs.at_name);
6867 CHECKSUM_ATTR (attrs.at_accessibility);
6868 CHECKSUM_ATTR (attrs.at_address_class);
6869 CHECKSUM_ATTR (attrs.at_allocated);
6870 CHECKSUM_ATTR (attrs.at_artificial);
6871 CHECKSUM_ATTR (attrs.at_associated);
6872 CHECKSUM_ATTR (attrs.at_binary_scale);
6873 CHECKSUM_ATTR (attrs.at_bit_offset);
6874 CHECKSUM_ATTR (attrs.at_bit_size);
6875 CHECKSUM_ATTR (attrs.at_bit_stride);
6876 CHECKSUM_ATTR (attrs.at_byte_size);
6877 CHECKSUM_ATTR (attrs.at_byte_stride);
6878 CHECKSUM_ATTR (attrs.at_const_value);
6879 CHECKSUM_ATTR (attrs.at_containing_type);
6880 CHECKSUM_ATTR (attrs.at_count);
6881 CHECKSUM_ATTR (attrs.at_data_location);
6882 CHECKSUM_ATTR (attrs.at_data_member_location);
6883 CHECKSUM_ATTR (attrs.at_decimal_scale);
6884 CHECKSUM_ATTR (attrs.at_decimal_sign);
6885 CHECKSUM_ATTR (attrs.at_default_value);
6886 CHECKSUM_ATTR (attrs.at_digit_count);
6887 CHECKSUM_ATTR (attrs.at_discr);
6888 CHECKSUM_ATTR (attrs.at_discr_list);
6889 CHECKSUM_ATTR (attrs.at_discr_value);
6890 CHECKSUM_ATTR (attrs.at_encoding);
6891 CHECKSUM_ATTR (attrs.at_endianity);
6892 CHECKSUM_ATTR (attrs.at_explicit);
6893 CHECKSUM_ATTR (attrs.at_is_optional);
6894 CHECKSUM_ATTR (attrs.at_location);
6895 CHECKSUM_ATTR (attrs.at_lower_bound);
6896 CHECKSUM_ATTR (attrs.at_mutable);
6897 CHECKSUM_ATTR (attrs.at_ordering);
6898 CHECKSUM_ATTR (attrs.at_picture_string);
6899 CHECKSUM_ATTR (attrs.at_prototyped);
6900 CHECKSUM_ATTR (attrs.at_small);
6901 CHECKSUM_ATTR (attrs.at_segment);
6902 CHECKSUM_ATTR (attrs.at_string_length);
6903 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6904 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6905 CHECKSUM_ATTR (attrs.at_threads_scaled);
6906 CHECKSUM_ATTR (attrs.at_upper_bound);
6907 CHECKSUM_ATTR (attrs.at_use_location);
6908 CHECKSUM_ATTR (attrs.at_use_UTF8);
6909 CHECKSUM_ATTR (attrs.at_variable_parameter);
6910 CHECKSUM_ATTR (attrs.at_virtuality);
6911 CHECKSUM_ATTR (attrs.at_visibility);
6912 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6913 CHECKSUM_ATTR (attrs.at_type);
6914 CHECKSUM_ATTR (attrs.at_friend);
6915 CHECKSUM_ATTR (attrs.at_alignment);
6917 /* Checksum the child DIEs. */
6918 c = die->die_child;
6919 if (c) do {
6920 dw_attr_node *name_attr;
6922 c = c->die_sib;
6923 name_attr = get_AT (c, DW_AT_name);
6924 if (is_template_instantiation (c))
6926 /* Ignore instantiations of member type and function templates. */
6928 else if (name_attr != NULL
6929 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6931 /* Use a shallow checksum for named nested types and member
6932 functions. */
6933 CHECKSUM_ULEB128 ('S');
6934 CHECKSUM_ULEB128 (c->die_tag);
6935 CHECKSUM_STRING (AT_string (name_attr));
6937 else
6939 /* Use a deep checksum for other children. */
6940 /* Mark this DIE so it gets processed when unmarking. */
6941 if (c->die_mark == 0)
6942 c->die_mark = -1;
6943 die_checksum_ordered (c, ctx, mark);
6945 } while (c != die->die_child);
6947 CHECKSUM_ULEB128 (0);
6950 /* Add a type name and tag to a hash. */
6951 static void
6952 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6954 CHECKSUM_ULEB128 (tag);
6955 CHECKSUM_STRING (name);
6958 #undef CHECKSUM
6959 #undef CHECKSUM_STRING
6960 #undef CHECKSUM_ATTR
6961 #undef CHECKSUM_LEB128
6962 #undef CHECKSUM_ULEB128
6964 /* Generate the type signature for DIE. This is computed by generating an
6965 MD5 checksum over the DIE's tag, its relevant attributes, and its
6966 children. Attributes that are references to other DIEs are processed
6967 by recursion, using the MARK field to prevent infinite recursion.
6968 If the DIE is nested inside a namespace or another type, we also
6969 need to include that context in the signature. The lower 64 bits
6970 of the resulting MD5 checksum comprise the signature. */
6972 static void
6973 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6975 int mark;
6976 const char *name;
6977 unsigned char checksum[16];
6978 struct md5_ctx ctx;
6979 dw_die_ref decl;
6980 dw_die_ref parent;
6982 name = get_AT_string (die, DW_AT_name);
6983 decl = get_AT_ref (die, DW_AT_specification);
6984 parent = get_die_parent (die);
6986 /* First, compute a signature for just the type name (and its surrounding
6987 context, if any. This is stored in the type unit DIE for link-time
6988 ODR (one-definition rule) checking. */
6990 if (is_cxx () && name != NULL)
6992 md5_init_ctx (&ctx);
6994 /* Checksum the names of surrounding namespaces and structures. */
6995 if (parent != NULL)
6996 checksum_die_context (parent, &ctx);
6998 /* Checksum the current DIE. */
6999 die_odr_checksum (die->die_tag, name, &ctx);
7000 md5_finish_ctx (&ctx, checksum);
7002 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7005 /* Next, compute the complete type signature. */
7007 md5_init_ctx (&ctx);
7008 mark = 1;
7009 die->die_mark = mark;
7011 /* Checksum the names of surrounding namespaces and structures. */
7012 if (parent != NULL)
7013 checksum_die_context (parent, &ctx);
7015 /* Checksum the DIE and its children. */
7016 die_checksum_ordered (die, &ctx, &mark);
7017 unmark_all_dies (die);
7018 md5_finish_ctx (&ctx, checksum);
7020 /* Store the signature in the type node and link the type DIE and the
7021 type node together. */
7022 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7023 DWARF_TYPE_SIGNATURE_SIZE);
7024 die->comdat_type_p = true;
7025 die->die_id.die_type_node = type_node;
7026 type_node->type_die = die;
7028 /* If the DIE is a specification, link its declaration to the type node
7029 as well. */
7030 if (decl != NULL)
7032 decl->comdat_type_p = true;
7033 decl->die_id.die_type_node = type_node;
7037 /* Do the location expressions look same? */
7038 static inline int
7039 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7041 return loc1->dw_loc_opc == loc2->dw_loc_opc
7042 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7043 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7046 /* Do the values look the same? */
7047 static int
7048 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7050 dw_loc_descr_ref loc1, loc2;
7051 rtx r1, r2;
7053 if (v1->val_class != v2->val_class)
7054 return 0;
7056 switch (v1->val_class)
7058 case dw_val_class_const:
7059 case dw_val_class_const_implicit:
7060 return v1->v.val_int == v2->v.val_int;
7061 case dw_val_class_unsigned_const:
7062 case dw_val_class_unsigned_const_implicit:
7063 return v1->v.val_unsigned == v2->v.val_unsigned;
7064 case dw_val_class_const_double:
7065 return v1->v.val_double.high == v2->v.val_double.high
7066 && v1->v.val_double.low == v2->v.val_double.low;
7067 case dw_val_class_wide_int:
7068 return *v1->v.val_wide == *v2->v.val_wide;
7069 case dw_val_class_vec:
7070 if (v1->v.val_vec.length != v2->v.val_vec.length
7071 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7072 return 0;
7073 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7074 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7075 return 0;
7076 return 1;
7077 case dw_val_class_flag:
7078 return v1->v.val_flag == v2->v.val_flag;
7079 case dw_val_class_str:
7080 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7082 case dw_val_class_addr:
7083 r1 = v1->v.val_addr;
7084 r2 = v2->v.val_addr;
7085 if (GET_CODE (r1) != GET_CODE (r2))
7086 return 0;
7087 return !rtx_equal_p (r1, r2);
7089 case dw_val_class_offset:
7090 return v1->v.val_offset == v2->v.val_offset;
7092 case dw_val_class_loc:
7093 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7094 loc1 && loc2;
7095 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7096 if (!same_loc_p (loc1, loc2, mark))
7097 return 0;
7098 return !loc1 && !loc2;
7100 case dw_val_class_die_ref:
7101 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7103 case dw_val_class_fde_ref:
7104 case dw_val_class_vms_delta:
7105 case dw_val_class_lbl_id:
7106 case dw_val_class_lineptr:
7107 case dw_val_class_macptr:
7108 case dw_val_class_loclistsptr:
7109 case dw_val_class_high_pc:
7110 return 1;
7112 case dw_val_class_file:
7113 case dw_val_class_file_implicit:
7114 return v1->v.val_file == v2->v.val_file;
7116 case dw_val_class_data8:
7117 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7119 default:
7120 return 1;
7124 /* Do the attributes look the same? */
7126 static int
7127 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7129 if (at1->dw_attr != at2->dw_attr)
7130 return 0;
7132 /* We don't care that this was compiled with a different compiler
7133 snapshot; if the output is the same, that's what matters. */
7134 if (at1->dw_attr == DW_AT_producer)
7135 return 1;
7137 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7140 /* Do the dies look the same? */
7142 static int
7143 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7145 dw_die_ref c1, c2;
7146 dw_attr_node *a1;
7147 unsigned ix;
7149 /* To avoid infinite recursion. */
7150 if (die1->die_mark)
7151 return die1->die_mark == die2->die_mark;
7152 die1->die_mark = die2->die_mark = ++(*mark);
7154 if (die1->die_tag != die2->die_tag)
7155 return 0;
7157 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7158 return 0;
7160 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7161 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7162 return 0;
7164 c1 = die1->die_child;
7165 c2 = die2->die_child;
7166 if (! c1)
7168 if (c2)
7169 return 0;
7171 else
7172 for (;;)
7174 if (!same_die_p (c1, c2, mark))
7175 return 0;
7176 c1 = c1->die_sib;
7177 c2 = c2->die_sib;
7178 if (c1 == die1->die_child)
7180 if (c2 == die2->die_child)
7181 break;
7182 else
7183 return 0;
7187 return 1;
7190 /* Do the dies look the same? Wrapper around same_die_p. */
7192 static int
7193 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7195 int mark = 0;
7196 int ret = same_die_p (die1, die2, &mark);
7198 unmark_all_dies (die1);
7199 unmark_all_dies (die2);
7201 return ret;
7204 /* The prefix to attach to symbols on DIEs in the current comdat debug
7205 info section. */
7206 static const char *comdat_symbol_id;
7208 /* The index of the current symbol within the current comdat CU. */
7209 static unsigned int comdat_symbol_number;
7211 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7212 children, and set comdat_symbol_id accordingly. */
7214 static void
7215 compute_section_prefix (dw_die_ref unit_die)
7217 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7218 const char *base = die_name ? lbasename (die_name) : "anonymous";
7219 char *name = XALLOCAVEC (char, strlen (base) + 64);
7220 char *p;
7221 int i, mark;
7222 unsigned char checksum[16];
7223 struct md5_ctx ctx;
7225 /* Compute the checksum of the DIE, then append part of it as hex digits to
7226 the name filename of the unit. */
7228 md5_init_ctx (&ctx);
7229 mark = 0;
7230 die_checksum (unit_die, &ctx, &mark);
7231 unmark_all_dies (unit_die);
7232 md5_finish_ctx (&ctx, checksum);
7234 sprintf (name, "%s.", base);
7235 clean_symbol_name (name);
7237 p = name + strlen (name);
7238 for (i = 0; i < 4; i++)
7240 sprintf (p, "%.2x", checksum[i]);
7241 p += 2;
7244 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7245 comdat_symbol_number = 0;
7248 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7250 static int
7251 is_type_die (dw_die_ref die)
7253 switch (die->die_tag)
7255 case DW_TAG_array_type:
7256 case DW_TAG_class_type:
7257 case DW_TAG_interface_type:
7258 case DW_TAG_enumeration_type:
7259 case DW_TAG_pointer_type:
7260 case DW_TAG_reference_type:
7261 case DW_TAG_rvalue_reference_type:
7262 case DW_TAG_string_type:
7263 case DW_TAG_structure_type:
7264 case DW_TAG_subroutine_type:
7265 case DW_TAG_union_type:
7266 case DW_TAG_ptr_to_member_type:
7267 case DW_TAG_set_type:
7268 case DW_TAG_subrange_type:
7269 case DW_TAG_base_type:
7270 case DW_TAG_const_type:
7271 case DW_TAG_file_type:
7272 case DW_TAG_packed_type:
7273 case DW_TAG_volatile_type:
7274 case DW_TAG_typedef:
7275 return 1;
7276 default:
7277 return 0;
7281 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7282 Basically, we want to choose the bits that are likely to be shared between
7283 compilations (types) and leave out the bits that are specific to individual
7284 compilations (functions). */
7286 static int
7287 is_comdat_die (dw_die_ref c)
7289 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7290 we do for stabs. The advantage is a greater likelihood of sharing between
7291 objects that don't include headers in the same order (and therefore would
7292 put the base types in a different comdat). jason 8/28/00 */
7294 if (c->die_tag == DW_TAG_base_type)
7295 return 0;
7297 if (c->die_tag == DW_TAG_pointer_type
7298 || c->die_tag == DW_TAG_reference_type
7299 || c->die_tag == DW_TAG_rvalue_reference_type
7300 || c->die_tag == DW_TAG_const_type
7301 || c->die_tag == DW_TAG_volatile_type)
7303 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7305 return t ? is_comdat_die (t) : 0;
7308 return is_type_die (c);
7311 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7312 compilation unit. */
7314 static int
7315 is_symbol_die (dw_die_ref c)
7317 return (is_type_die (c)
7318 || is_declaration_die (c)
7319 || c->die_tag == DW_TAG_namespace
7320 || c->die_tag == DW_TAG_module);
7323 /* Returns true iff C is a compile-unit DIE. */
7325 static inline bool
7326 is_cu_die (dw_die_ref c)
7328 return c && (c->die_tag == DW_TAG_compile_unit
7329 || c->die_tag == DW_TAG_skeleton_unit);
7332 /* Returns true iff C is a unit DIE of some sort. */
7334 static inline bool
7335 is_unit_die (dw_die_ref c)
7337 return c && (c->die_tag == DW_TAG_compile_unit
7338 || c->die_tag == DW_TAG_partial_unit
7339 || c->die_tag == DW_TAG_type_unit
7340 || c->die_tag == DW_TAG_skeleton_unit);
7343 /* Returns true iff C is a namespace DIE. */
7345 static inline bool
7346 is_namespace_die (dw_die_ref c)
7348 return c && c->die_tag == DW_TAG_namespace;
7351 /* Returns true iff C is a class or structure DIE. */
7353 static inline bool
7354 is_class_die (dw_die_ref c)
7356 return c && (c->die_tag == DW_TAG_class_type
7357 || c->die_tag == DW_TAG_structure_type);
7360 /* Return non-zero if this DIE is a template parameter. */
7362 static inline bool
7363 is_template_parameter (dw_die_ref die)
7365 switch (die->die_tag)
7367 case DW_TAG_template_type_param:
7368 case DW_TAG_template_value_param:
7369 case DW_TAG_GNU_template_template_param:
7370 case DW_TAG_GNU_template_parameter_pack:
7371 return true;
7372 default:
7373 return false;
7377 /* Return non-zero if this DIE represents a template instantiation. */
7379 static inline bool
7380 is_template_instantiation (dw_die_ref die)
7382 dw_die_ref c;
7384 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7385 return false;
7386 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7387 return false;
7390 static char *
7391 gen_internal_sym (const char *prefix)
7393 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7395 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7396 return xstrdup (buf);
7399 /* Assign symbols to all worthy DIEs under DIE. */
7401 static void
7402 assign_symbol_names (dw_die_ref die)
7404 dw_die_ref c;
7406 if (is_symbol_die (die) && !die->comdat_type_p)
7408 if (comdat_symbol_id)
7410 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7412 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7413 comdat_symbol_id, comdat_symbol_number++);
7414 die->die_id.die_symbol = xstrdup (p);
7416 else
7417 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7420 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7423 struct cu_hash_table_entry
7425 dw_die_ref cu;
7426 unsigned min_comdat_num, max_comdat_num;
7427 struct cu_hash_table_entry *next;
7430 /* Helpers to manipulate hash table of CUs. */
7432 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7434 typedef die_struct *compare_type;
7435 static inline hashval_t hash (const cu_hash_table_entry *);
7436 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7437 static inline void remove (cu_hash_table_entry *);
7440 inline hashval_t
7441 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7443 return htab_hash_string (entry->cu->die_id.die_symbol);
7446 inline bool
7447 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7448 const die_struct *entry2)
7450 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7453 inline void
7454 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7456 struct cu_hash_table_entry *next;
7458 while (entry)
7460 next = entry->next;
7461 free (entry);
7462 entry = next;
7466 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7468 /* Check whether we have already seen this CU and set up SYM_NUM
7469 accordingly. */
7470 static int
7471 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7473 struct cu_hash_table_entry dummy;
7474 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7476 dummy.max_comdat_num = 0;
7478 slot = htable->find_slot_with_hash (cu,
7479 htab_hash_string (cu->die_id.die_symbol),
7480 INSERT);
7481 entry = *slot;
7483 for (; entry; last = entry, entry = entry->next)
7485 if (same_die_p_wrap (cu, entry->cu))
7486 break;
7489 if (entry)
7491 *sym_num = entry->min_comdat_num;
7492 return 1;
7495 entry = XCNEW (struct cu_hash_table_entry);
7496 entry->cu = cu;
7497 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7498 entry->next = *slot;
7499 *slot = entry;
7501 return 0;
7504 /* Record SYM_NUM to record of CU in HTABLE. */
7505 static void
7506 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7507 unsigned int sym_num)
7509 struct cu_hash_table_entry **slot, *entry;
7511 slot = htable->find_slot_with_hash (cu,
7512 htab_hash_string (cu->die_id.die_symbol),
7513 NO_INSERT);
7514 entry = *slot;
7516 entry->max_comdat_num = sym_num;
7519 /* Traverse the DIE (which is always comp_unit_die), and set up
7520 additional compilation units for each of the include files we see
7521 bracketed by BINCL/EINCL. */
7523 static void
7524 break_out_includes (dw_die_ref die)
7526 dw_die_ref c;
7527 dw_die_ref unit = NULL;
7528 limbo_die_node *node, **pnode;
7530 c = die->die_child;
7531 if (c) do {
7532 dw_die_ref prev = c;
7533 c = c->die_sib;
7534 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7535 || (unit && is_comdat_die (c)))
7537 dw_die_ref next = c->die_sib;
7539 /* This DIE is for a secondary CU; remove it from the main one. */
7540 remove_child_with_prev (c, prev);
7542 if (c->die_tag == DW_TAG_GNU_BINCL)
7543 unit = push_new_compile_unit (unit, c);
7544 else if (c->die_tag == DW_TAG_GNU_EINCL)
7545 unit = pop_compile_unit (unit);
7546 else
7547 add_child_die (unit, c);
7548 c = next;
7549 if (c == die->die_child)
7550 break;
7552 } while (c != die->die_child);
7554 #if 0
7555 /* We can only use this in debugging, since the frontend doesn't check
7556 to make sure that we leave every include file we enter. */
7557 gcc_assert (!unit);
7558 #endif
7560 assign_symbol_names (die);
7561 cu_hash_type cu_hash_table (10);
7562 for (node = limbo_die_list, pnode = &limbo_die_list;
7563 node;
7564 node = node->next)
7566 int is_dupl;
7568 compute_section_prefix (node->die);
7569 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7570 &comdat_symbol_number);
7571 assign_symbol_names (node->die);
7572 if (is_dupl)
7573 *pnode = node->next;
7574 else
7576 pnode = &node->next;
7577 record_comdat_symbol_number (node->die, &cu_hash_table,
7578 comdat_symbol_number);
7583 /* Return non-zero if this DIE is a declaration. */
7585 static int
7586 is_declaration_die (dw_die_ref die)
7588 dw_attr_node *a;
7589 unsigned ix;
7591 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7592 if (a->dw_attr == DW_AT_declaration)
7593 return 1;
7595 return 0;
7598 /* Return non-zero if this DIE is nested inside a subprogram. */
7600 static int
7601 is_nested_in_subprogram (dw_die_ref die)
7603 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7605 if (decl == NULL)
7606 decl = die;
7607 return local_scope_p (decl);
7610 /* Return non-zero if this DIE contains a defining declaration of a
7611 subprogram. */
7613 static int
7614 contains_subprogram_definition (dw_die_ref die)
7616 dw_die_ref c;
7618 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7619 return 1;
7620 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7621 return 0;
7624 /* Return non-zero if this is a type DIE that should be moved to a
7625 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7626 unit type. */
7628 static int
7629 should_move_die_to_comdat (dw_die_ref die)
7631 switch (die->die_tag)
7633 case DW_TAG_class_type:
7634 case DW_TAG_structure_type:
7635 case DW_TAG_enumeration_type:
7636 case DW_TAG_union_type:
7637 /* Don't move declarations, inlined instances, types nested in a
7638 subprogram, or types that contain subprogram definitions. */
7639 if (is_declaration_die (die)
7640 || get_AT (die, DW_AT_abstract_origin)
7641 || is_nested_in_subprogram (die)
7642 || contains_subprogram_definition (die))
7643 return 0;
7644 return 1;
7645 case DW_TAG_array_type:
7646 case DW_TAG_interface_type:
7647 case DW_TAG_pointer_type:
7648 case DW_TAG_reference_type:
7649 case DW_TAG_rvalue_reference_type:
7650 case DW_TAG_string_type:
7651 case DW_TAG_subroutine_type:
7652 case DW_TAG_ptr_to_member_type:
7653 case DW_TAG_set_type:
7654 case DW_TAG_subrange_type:
7655 case DW_TAG_base_type:
7656 case DW_TAG_const_type:
7657 case DW_TAG_file_type:
7658 case DW_TAG_packed_type:
7659 case DW_TAG_volatile_type:
7660 case DW_TAG_typedef:
7661 default:
7662 return 0;
7666 /* Make a clone of DIE. */
7668 static dw_die_ref
7669 clone_die (dw_die_ref die)
7671 dw_die_ref clone;
7672 dw_attr_node *a;
7673 unsigned ix;
7675 clone = ggc_cleared_alloc<die_node> ();
7676 clone->die_tag = die->die_tag;
7678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7679 add_dwarf_attr (clone, a);
7681 return clone;
7684 /* Make a clone of the tree rooted at DIE. */
7686 static dw_die_ref
7687 clone_tree (dw_die_ref die)
7689 dw_die_ref c;
7690 dw_die_ref clone = clone_die (die);
7692 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7694 return clone;
7697 /* Make a clone of DIE as a declaration. */
7699 static dw_die_ref
7700 clone_as_declaration (dw_die_ref die)
7702 dw_die_ref clone;
7703 dw_die_ref decl;
7704 dw_attr_node *a;
7705 unsigned ix;
7707 /* If the DIE is already a declaration, just clone it. */
7708 if (is_declaration_die (die))
7709 return clone_die (die);
7711 /* If the DIE is a specification, just clone its declaration DIE. */
7712 decl = get_AT_ref (die, DW_AT_specification);
7713 if (decl != NULL)
7715 clone = clone_die (decl);
7716 if (die->comdat_type_p)
7717 add_AT_die_ref (clone, DW_AT_signature, die);
7718 return clone;
7721 clone = ggc_cleared_alloc<die_node> ();
7722 clone->die_tag = die->die_tag;
7724 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7726 /* We don't want to copy over all attributes.
7727 For example we don't want DW_AT_byte_size because otherwise we will no
7728 longer have a declaration and GDB will treat it as a definition. */
7730 switch (a->dw_attr)
7732 case DW_AT_abstract_origin:
7733 case DW_AT_artificial:
7734 case DW_AT_containing_type:
7735 case DW_AT_external:
7736 case DW_AT_name:
7737 case DW_AT_type:
7738 case DW_AT_virtuality:
7739 case DW_AT_linkage_name:
7740 case DW_AT_MIPS_linkage_name:
7741 add_dwarf_attr (clone, a);
7742 break;
7743 case DW_AT_byte_size:
7744 case DW_AT_alignment:
7745 default:
7746 break;
7750 if (die->comdat_type_p)
7751 add_AT_die_ref (clone, DW_AT_signature, die);
7753 add_AT_flag (clone, DW_AT_declaration, 1);
7754 return clone;
7758 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7760 struct decl_table_entry
7762 dw_die_ref orig;
7763 dw_die_ref copy;
7766 /* Helpers to manipulate hash table of copied declarations. */
7768 /* Hashtable helpers. */
7770 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7772 typedef die_struct *compare_type;
7773 static inline hashval_t hash (const decl_table_entry *);
7774 static inline bool equal (const decl_table_entry *, const die_struct *);
7777 inline hashval_t
7778 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7780 return htab_hash_pointer (entry->orig);
7783 inline bool
7784 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7785 const die_struct *entry2)
7787 return entry1->orig == entry2;
7790 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7792 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7793 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7794 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7795 to check if the ancestor has already been copied into UNIT. */
7797 static dw_die_ref
7798 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7799 decl_hash_type *decl_table)
7801 dw_die_ref parent = die->die_parent;
7802 dw_die_ref new_parent = unit;
7803 dw_die_ref copy;
7804 decl_table_entry **slot = NULL;
7805 struct decl_table_entry *entry = NULL;
7807 if (decl_table)
7809 /* Check if the entry has already been copied to UNIT. */
7810 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7811 INSERT);
7812 if (*slot != HTAB_EMPTY_ENTRY)
7814 entry = *slot;
7815 return entry->copy;
7818 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7819 entry = XCNEW (struct decl_table_entry);
7820 entry->orig = die;
7821 entry->copy = NULL;
7822 *slot = entry;
7825 if (parent != NULL)
7827 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7828 if (spec != NULL)
7829 parent = spec;
7830 if (!is_unit_die (parent))
7831 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7834 copy = clone_as_declaration (die);
7835 add_child_die (new_parent, copy);
7837 if (decl_table)
7839 /* Record the pointer to the copy. */
7840 entry->copy = copy;
7843 return copy;
7845 /* Copy the declaration context to the new type unit DIE. This includes
7846 any surrounding namespace or type declarations. If the DIE has an
7847 AT_specification attribute, it also includes attributes and children
7848 attached to the specification, and returns a pointer to the original
7849 parent of the declaration DIE. Returns NULL otherwise. */
7851 static dw_die_ref
7852 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7854 dw_die_ref decl;
7855 dw_die_ref new_decl;
7856 dw_die_ref orig_parent = NULL;
7858 decl = get_AT_ref (die, DW_AT_specification);
7859 if (decl == NULL)
7860 decl = die;
7861 else
7863 unsigned ix;
7864 dw_die_ref c;
7865 dw_attr_node *a;
7867 /* The original DIE will be changed to a declaration, and must
7868 be moved to be a child of the original declaration DIE. */
7869 orig_parent = decl->die_parent;
7871 /* Copy the type node pointer from the new DIE to the original
7872 declaration DIE so we can forward references later. */
7873 decl->comdat_type_p = true;
7874 decl->die_id.die_type_node = die->die_id.die_type_node;
7876 remove_AT (die, DW_AT_specification);
7878 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7880 if (a->dw_attr != DW_AT_name
7881 && a->dw_attr != DW_AT_declaration
7882 && a->dw_attr != DW_AT_external)
7883 add_dwarf_attr (die, a);
7886 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7889 if (decl->die_parent != NULL
7890 && !is_unit_die (decl->die_parent))
7892 new_decl = copy_ancestor_tree (unit, decl, NULL);
7893 if (new_decl != NULL)
7895 remove_AT (new_decl, DW_AT_signature);
7896 add_AT_specification (die, new_decl);
7900 return orig_parent;
7903 /* Generate the skeleton ancestor tree for the given NODE, then clone
7904 the DIE and add the clone into the tree. */
7906 static void
7907 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7909 if (node->new_die != NULL)
7910 return;
7912 node->new_die = clone_as_declaration (node->old_die);
7914 if (node->parent != NULL)
7916 generate_skeleton_ancestor_tree (node->parent);
7917 add_child_die (node->parent->new_die, node->new_die);
7921 /* Generate a skeleton tree of DIEs containing any declarations that are
7922 found in the original tree. We traverse the tree looking for declaration
7923 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7925 static void
7926 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7928 skeleton_chain_node node;
7929 dw_die_ref c;
7930 dw_die_ref first;
7931 dw_die_ref prev = NULL;
7932 dw_die_ref next = NULL;
7934 node.parent = parent;
7936 first = c = parent->old_die->die_child;
7937 if (c)
7938 next = c->die_sib;
7939 if (c) do {
7940 if (prev == NULL || prev->die_sib == c)
7941 prev = c;
7942 c = next;
7943 next = (c == first ? NULL : c->die_sib);
7944 node.old_die = c;
7945 node.new_die = NULL;
7946 if (is_declaration_die (c))
7948 if (is_template_instantiation (c))
7950 /* Instantiated templates do not need to be cloned into the
7951 type unit. Just move the DIE and its children back to
7952 the skeleton tree (in the main CU). */
7953 remove_child_with_prev (c, prev);
7954 add_child_die (parent->new_die, c);
7955 c = prev;
7957 else if (c->comdat_type_p)
7959 /* This is the skeleton of earlier break_out_comdat_types
7960 type. Clone the existing DIE, but keep the children
7961 under the original (which is in the main CU). */
7962 dw_die_ref clone = clone_die (c);
7964 replace_child (c, clone, prev);
7965 generate_skeleton_ancestor_tree (parent);
7966 add_child_die (parent->new_die, c);
7967 c = clone;
7968 continue;
7970 else
7972 /* Clone the existing DIE, move the original to the skeleton
7973 tree (which is in the main CU), and put the clone, with
7974 all the original's children, where the original came from
7975 (which is about to be moved to the type unit). */
7976 dw_die_ref clone = clone_die (c);
7977 move_all_children (c, clone);
7979 /* If the original has a DW_AT_object_pointer attribute,
7980 it would now point to a child DIE just moved to the
7981 cloned tree, so we need to remove that attribute from
7982 the original. */
7983 remove_AT (c, DW_AT_object_pointer);
7985 replace_child (c, clone, prev);
7986 generate_skeleton_ancestor_tree (parent);
7987 add_child_die (parent->new_die, c);
7988 node.old_die = clone;
7989 node.new_die = c;
7990 c = clone;
7993 generate_skeleton_bottom_up (&node);
7994 } while (next != NULL);
7997 /* Wrapper function for generate_skeleton_bottom_up. */
7999 static dw_die_ref
8000 generate_skeleton (dw_die_ref die)
8002 skeleton_chain_node node;
8004 node.old_die = die;
8005 node.new_die = NULL;
8006 node.parent = NULL;
8008 /* If this type definition is nested inside another type,
8009 and is not an instantiation of a template, always leave
8010 at least a declaration in its place. */
8011 if (die->die_parent != NULL
8012 && is_type_die (die->die_parent)
8013 && !is_template_instantiation (die))
8014 node.new_die = clone_as_declaration (die);
8016 generate_skeleton_bottom_up (&node);
8017 return node.new_die;
8020 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8021 declaration. The original DIE is moved to a new compile unit so that
8022 existing references to it follow it to the new location. If any of the
8023 original DIE's descendants is a declaration, we need to replace the
8024 original DIE with a skeleton tree and move the declarations back into the
8025 skeleton tree. */
8027 static dw_die_ref
8028 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8029 dw_die_ref prev)
8031 dw_die_ref skeleton, orig_parent;
8033 /* Copy the declaration context to the type unit DIE. If the returned
8034 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8035 that DIE. */
8036 orig_parent = copy_declaration_context (unit, child);
8038 skeleton = generate_skeleton (child);
8039 if (skeleton == NULL)
8040 remove_child_with_prev (child, prev);
8041 else
8043 skeleton->comdat_type_p = true;
8044 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8046 /* If the original DIE was a specification, we need to put
8047 the skeleton under the parent DIE of the declaration.
8048 This leaves the original declaration in the tree, but
8049 it will be pruned later since there are no longer any
8050 references to it. */
8051 if (orig_parent != NULL)
8053 remove_child_with_prev (child, prev);
8054 add_child_die (orig_parent, skeleton);
8056 else
8057 replace_child (child, skeleton, prev);
8060 return skeleton;
8063 static void
8064 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8065 comdat_type_node *type_node,
8066 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8068 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8069 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8070 DWARF procedure references in the DW_AT_location attribute. */
8072 static dw_die_ref
8073 copy_dwarf_procedure (dw_die_ref die,
8074 comdat_type_node *type_node,
8075 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8077 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8079 /* DWARF procedures are not supposed to have children... */
8080 gcc_assert (die->die_child == NULL);
8082 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8083 gcc_assert (vec_safe_length (die->die_attr) == 1
8084 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8086 /* Do not copy more than once DWARF procedures. */
8087 bool existed;
8088 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8089 if (existed)
8090 return die_copy;
8092 die_copy = clone_die (die);
8093 add_child_die (type_node->root_die, die_copy);
8094 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8095 return die_copy;
8098 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8099 procedures in DIE's attributes. */
8101 static void
8102 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8103 comdat_type_node *type_node,
8104 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8106 dw_attr_node *a;
8107 unsigned i;
8109 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8111 dw_loc_descr_ref loc;
8113 if (a->dw_attr_val.val_class != dw_val_class_loc)
8114 continue;
8116 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8118 switch (loc->dw_loc_opc)
8120 case DW_OP_call2:
8121 case DW_OP_call4:
8122 case DW_OP_call_ref:
8123 gcc_assert (loc->dw_loc_oprnd1.val_class
8124 == dw_val_class_die_ref);
8125 loc->dw_loc_oprnd1.v.val_die_ref.die
8126 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8127 type_node,
8128 copied_dwarf_procs);
8130 default:
8131 break;
8137 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8138 rewrite references to point to the copies.
8140 References are looked for in DIE's attributes and recursively in all its
8141 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8142 mapping from old DWARF procedures to their copy. It is used not to copy
8143 twice the same DWARF procedure under TYPE_NODE. */
8145 static void
8146 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8147 comdat_type_node *type_node,
8148 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8150 dw_die_ref c;
8152 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8153 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8154 type_node,
8155 copied_dwarf_procs));
8158 /* Traverse the DIE and set up additional .debug_types or .debug_info
8159 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8160 section. */
8162 static void
8163 break_out_comdat_types (dw_die_ref die)
8165 dw_die_ref c;
8166 dw_die_ref first;
8167 dw_die_ref prev = NULL;
8168 dw_die_ref next = NULL;
8169 dw_die_ref unit = NULL;
8171 first = c = die->die_child;
8172 if (c)
8173 next = c->die_sib;
8174 if (c) do {
8175 if (prev == NULL || prev->die_sib == c)
8176 prev = c;
8177 c = next;
8178 next = (c == first ? NULL : c->die_sib);
8179 if (should_move_die_to_comdat (c))
8181 dw_die_ref replacement;
8182 comdat_type_node *type_node;
8184 /* Break out nested types into their own type units. */
8185 break_out_comdat_types (c);
8187 /* Create a new type unit DIE as the root for the new tree, and
8188 add it to the list of comdat types. */
8189 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8190 add_AT_unsigned (unit, DW_AT_language,
8191 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8192 type_node = ggc_cleared_alloc<comdat_type_node> ();
8193 type_node->root_die = unit;
8194 type_node->next = comdat_type_list;
8195 comdat_type_list = type_node;
8197 /* Generate the type signature. */
8198 generate_type_signature (c, type_node);
8200 /* Copy the declaration context, attributes, and children of the
8201 declaration into the new type unit DIE, then remove this DIE
8202 from the main CU (or replace it with a skeleton if necessary). */
8203 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8204 type_node->skeleton_die = replacement;
8206 /* Add the DIE to the new compunit. */
8207 add_child_die (unit, c);
8209 /* Types can reference DWARF procedures for type size or data location
8210 expressions. Calls in DWARF expressions cannot target procedures
8211 that are not in the same section. So we must copy DWARF procedures
8212 along with this type and then rewrite references to them. */
8213 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8214 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8216 if (replacement != NULL)
8217 c = replacement;
8219 else if (c->die_tag == DW_TAG_namespace
8220 || c->die_tag == DW_TAG_class_type
8221 || c->die_tag == DW_TAG_structure_type
8222 || c->die_tag == DW_TAG_union_type)
8224 /* Look for nested types that can be broken out. */
8225 break_out_comdat_types (c);
8227 } while (next != NULL);
8230 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8231 Enter all the cloned children into the hash table decl_table. */
8233 static dw_die_ref
8234 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8236 dw_die_ref c;
8237 dw_die_ref clone;
8238 struct decl_table_entry *entry;
8239 decl_table_entry **slot;
8241 if (die->die_tag == DW_TAG_subprogram)
8242 clone = clone_as_declaration (die);
8243 else
8244 clone = clone_die (die);
8246 slot = decl_table->find_slot_with_hash (die,
8247 htab_hash_pointer (die), INSERT);
8249 /* Assert that DIE isn't in the hash table yet. If it would be there
8250 before, the ancestors would be necessarily there as well, therefore
8251 clone_tree_partial wouldn't be called. */
8252 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8254 entry = XCNEW (struct decl_table_entry);
8255 entry->orig = die;
8256 entry->copy = clone;
8257 *slot = entry;
8259 if (die->die_tag != DW_TAG_subprogram)
8260 FOR_EACH_CHILD (die, c,
8261 add_child_die (clone, clone_tree_partial (c, decl_table)));
8263 return clone;
8266 /* Walk the DIE and its children, looking for references to incomplete
8267 or trivial types that are unmarked (i.e., that are not in the current
8268 type_unit). */
8270 static void
8271 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8273 dw_die_ref c;
8274 dw_attr_node *a;
8275 unsigned ix;
8277 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8279 if (AT_class (a) == dw_val_class_die_ref)
8281 dw_die_ref targ = AT_ref (a);
8282 decl_table_entry **slot;
8283 struct decl_table_entry *entry;
8285 if (targ->die_mark != 0 || targ->comdat_type_p)
8286 continue;
8288 slot = decl_table->find_slot_with_hash (targ,
8289 htab_hash_pointer (targ),
8290 INSERT);
8292 if (*slot != HTAB_EMPTY_ENTRY)
8294 /* TARG has already been copied, so we just need to
8295 modify the reference to point to the copy. */
8296 entry = *slot;
8297 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8299 else
8301 dw_die_ref parent = unit;
8302 dw_die_ref copy = clone_die (targ);
8304 /* Record in DECL_TABLE that TARG has been copied.
8305 Need to do this now, before the recursive call,
8306 because DECL_TABLE may be expanded and SLOT
8307 would no longer be a valid pointer. */
8308 entry = XCNEW (struct decl_table_entry);
8309 entry->orig = targ;
8310 entry->copy = copy;
8311 *slot = entry;
8313 /* If TARG is not a declaration DIE, we need to copy its
8314 children. */
8315 if (!is_declaration_die (targ))
8317 FOR_EACH_CHILD (
8318 targ, c,
8319 add_child_die (copy,
8320 clone_tree_partial (c, decl_table)));
8323 /* Make sure the cloned tree is marked as part of the
8324 type unit. */
8325 mark_dies (copy);
8327 /* If TARG has surrounding context, copy its ancestor tree
8328 into the new type unit. */
8329 if (targ->die_parent != NULL
8330 && !is_unit_die (targ->die_parent))
8331 parent = copy_ancestor_tree (unit, targ->die_parent,
8332 decl_table);
8334 add_child_die (parent, copy);
8335 a->dw_attr_val.v.val_die_ref.die = copy;
8337 /* Make sure the newly-copied DIE is walked. If it was
8338 installed in a previously-added context, it won't
8339 get visited otherwise. */
8340 if (parent != unit)
8342 /* Find the highest point of the newly-added tree,
8343 mark each node along the way, and walk from there. */
8344 parent->die_mark = 1;
8345 while (parent->die_parent
8346 && parent->die_parent->die_mark == 0)
8348 parent = parent->die_parent;
8349 parent->die_mark = 1;
8351 copy_decls_walk (unit, parent, decl_table);
8357 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8360 /* Copy declarations for "unworthy" types into the new comdat section.
8361 Incomplete types, modified types, and certain other types aren't broken
8362 out into comdat sections of their own, so they don't have a signature,
8363 and we need to copy the declaration into the same section so that we
8364 don't have an external reference. */
8366 static void
8367 copy_decls_for_unworthy_types (dw_die_ref unit)
8369 mark_dies (unit);
8370 decl_hash_type decl_table (10);
8371 copy_decls_walk (unit, unit, &decl_table);
8372 unmark_dies (unit);
8375 /* Traverse the DIE and add a sibling attribute if it may have the
8376 effect of speeding up access to siblings. To save some space,
8377 avoid generating sibling attributes for DIE's without children. */
8379 static void
8380 add_sibling_attributes (dw_die_ref die)
8382 dw_die_ref c;
8384 if (! die->die_child)
8385 return;
8387 if (die->die_parent && die != die->die_parent->die_child)
8388 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8390 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8393 /* Output all location lists for the DIE and its children. */
8395 static void
8396 output_location_lists (dw_die_ref die)
8398 dw_die_ref c;
8399 dw_attr_node *a;
8400 unsigned ix;
8402 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8403 if (AT_class (a) == dw_val_class_loc_list)
8404 output_loc_list (AT_loc_list (a));
8406 FOR_EACH_CHILD (die, c, output_location_lists (c));
8409 /* During assign_location_list_indexes and output_loclists_offset the
8410 current index, after it the number of assigned indexes (i.e. how
8411 large the .debug_loclists* offset table should be). */
8412 static unsigned int loc_list_idx;
8414 /* Output all location list offsets for the DIE and its children. */
8416 static void
8417 output_loclists_offsets (dw_die_ref die)
8419 dw_die_ref c;
8420 dw_attr_node *a;
8421 unsigned ix;
8423 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8424 if (AT_class (a) == dw_val_class_loc_list)
8426 dw_loc_list_ref l = AT_loc_list (a);
8427 if (l->offset_emitted)
8428 continue;
8429 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8430 loc_section_label, NULL);
8431 gcc_assert (l->hash == loc_list_idx);
8432 loc_list_idx++;
8433 l->offset_emitted = true;
8436 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8439 /* Recursively set indexes of location lists. */
8441 static void
8442 assign_location_list_indexes (dw_die_ref die)
8444 dw_die_ref c;
8445 dw_attr_node *a;
8446 unsigned ix;
8448 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8449 if (AT_class (a) == dw_val_class_loc_list)
8451 dw_loc_list_ref list = AT_loc_list (a);
8452 if (!list->num_assigned)
8454 list->num_assigned = true;
8455 list->hash = loc_list_idx++;
8459 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8462 /* We want to limit the number of external references, because they are
8463 larger than local references: a relocation takes multiple words, and
8464 even a sig8 reference is always eight bytes, whereas a local reference
8465 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8466 So if we encounter multiple external references to the same type DIE, we
8467 make a local typedef stub for it and redirect all references there.
8469 This is the element of the hash table for keeping track of these
8470 references. */
8472 struct external_ref
8474 dw_die_ref type;
8475 dw_die_ref stub;
8476 unsigned n_refs;
8479 /* Hashtable helpers. */
8481 struct external_ref_hasher : free_ptr_hash <external_ref>
8483 static inline hashval_t hash (const external_ref *);
8484 static inline bool equal (const external_ref *, const external_ref *);
8487 inline hashval_t
8488 external_ref_hasher::hash (const external_ref *r)
8490 dw_die_ref die = r->type;
8491 hashval_t h = 0;
8493 /* We can't use the address of the DIE for hashing, because
8494 that will make the order of the stub DIEs non-deterministic. */
8495 if (! die->comdat_type_p)
8496 /* We have a symbol; use it to compute a hash. */
8497 h = htab_hash_string (die->die_id.die_symbol);
8498 else
8500 /* We have a type signature; use a subset of the bits as the hash.
8501 The 8-byte signature is at least as large as hashval_t. */
8502 comdat_type_node *type_node = die->die_id.die_type_node;
8503 memcpy (&h, type_node->signature, sizeof (h));
8505 return h;
8508 inline bool
8509 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8511 return r1->type == r2->type;
8514 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8516 /* Return a pointer to the external_ref for references to DIE. */
8518 static struct external_ref *
8519 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8521 struct external_ref ref, *ref_p;
8522 external_ref **slot;
8524 ref.type = die;
8525 slot = map->find_slot (&ref, INSERT);
8526 if (*slot != HTAB_EMPTY_ENTRY)
8527 return *slot;
8529 ref_p = XCNEW (struct external_ref);
8530 ref_p->type = die;
8531 *slot = ref_p;
8532 return ref_p;
8535 /* Subroutine of optimize_external_refs, below.
8537 If we see a type skeleton, record it as our stub. If we see external
8538 references, remember how many we've seen. */
8540 static void
8541 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8543 dw_die_ref c;
8544 dw_attr_node *a;
8545 unsigned ix;
8546 struct external_ref *ref_p;
8548 if (is_type_die (die)
8549 && (c = get_AT_ref (die, DW_AT_signature)))
8551 /* This is a local skeleton; use it for local references. */
8552 ref_p = lookup_external_ref (map, c);
8553 ref_p->stub = die;
8556 /* Scan the DIE references, and remember any that refer to DIEs from
8557 other CUs (i.e. those which are not marked). */
8558 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8559 if (AT_class (a) == dw_val_class_die_ref
8560 && (c = AT_ref (a))->die_mark == 0
8561 && is_type_die (c))
8563 ref_p = lookup_external_ref (map, c);
8564 ref_p->n_refs++;
8567 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8570 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8571 points to an external_ref, DATA is the CU we're processing. If we don't
8572 already have a local stub, and we have multiple refs, build a stub. */
8575 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8577 struct external_ref *ref_p = *slot;
8579 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8581 /* We have multiple references to this type, so build a small stub.
8582 Both of these forms are a bit dodgy from the perspective of the
8583 DWARF standard, since technically they should have names. */
8584 dw_die_ref cu = data;
8585 dw_die_ref type = ref_p->type;
8586 dw_die_ref stub = NULL;
8588 if (type->comdat_type_p)
8590 /* If we refer to this type via sig8, use AT_signature. */
8591 stub = new_die (type->die_tag, cu, NULL_TREE);
8592 add_AT_die_ref (stub, DW_AT_signature, type);
8594 else
8596 /* Otherwise, use a typedef with no name. */
8597 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8598 add_AT_die_ref (stub, DW_AT_type, type);
8601 stub->die_mark++;
8602 ref_p->stub = stub;
8604 return 1;
8607 /* DIE is a unit; look through all the DIE references to see if there are
8608 any external references to types, and if so, create local stubs for
8609 them which will be applied in build_abbrev_table. This is useful because
8610 references to local DIEs are smaller. */
8612 static external_ref_hash_type *
8613 optimize_external_refs (dw_die_ref die)
8615 external_ref_hash_type *map = new external_ref_hash_type (10);
8616 optimize_external_refs_1 (die, map);
8617 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8618 return map;
8621 /* The following 3 variables are temporaries that are computed only during the
8622 build_abbrev_table call and used and released during the following
8623 optimize_abbrev_table call. */
8625 /* First abbrev_id that can be optimized based on usage. */
8626 static unsigned int abbrev_opt_start;
8628 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8629 abbrev_id smaller than this, because they must be already sized
8630 during build_abbrev_table). */
8631 static unsigned int abbrev_opt_base_type_end;
8633 /* Vector of usage counts during build_abbrev_table. Indexed by
8634 abbrev_id - abbrev_opt_start. */
8635 static vec<unsigned int> abbrev_usage_count;
8637 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8638 static vec<dw_die_ref> sorted_abbrev_dies;
8640 /* The format of each DIE (and its attribute value pairs) is encoded in an
8641 abbreviation table. This routine builds the abbreviation table and assigns
8642 a unique abbreviation id for each abbreviation entry. The children of each
8643 die are visited recursively. */
8645 static void
8646 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8648 unsigned int abbrev_id = 0;
8649 dw_die_ref c;
8650 dw_attr_node *a;
8651 unsigned ix;
8652 dw_die_ref abbrev;
8654 /* Scan the DIE references, and replace any that refer to
8655 DIEs from other CUs (i.e. those which are not marked) with
8656 the local stubs we built in optimize_external_refs. */
8657 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8658 if (AT_class (a) == dw_val_class_die_ref
8659 && (c = AT_ref (a))->die_mark == 0)
8661 struct external_ref *ref_p;
8662 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8664 ref_p = lookup_external_ref (extern_map, c);
8665 if (ref_p->stub && ref_p->stub != die)
8666 change_AT_die_ref (a, ref_p->stub);
8667 else
8668 /* We aren't changing this reference, so mark it external. */
8669 set_AT_ref_external (a, 1);
8672 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8674 dw_attr_node *die_a, *abbrev_a;
8675 unsigned ix;
8676 bool ok = true;
8678 if (abbrev_id == 0)
8679 continue;
8680 if (abbrev->die_tag != die->die_tag)
8681 continue;
8682 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8683 continue;
8685 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8686 continue;
8688 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8690 abbrev_a = &(*abbrev->die_attr)[ix];
8691 if ((abbrev_a->dw_attr != die_a->dw_attr)
8692 || (value_format (abbrev_a) != value_format (die_a)))
8694 ok = false;
8695 break;
8698 if (ok)
8699 break;
8702 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8704 vec_safe_push (abbrev_die_table, die);
8705 if (abbrev_opt_start)
8706 abbrev_usage_count.safe_push (0);
8708 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8710 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8711 sorted_abbrev_dies.safe_push (die);
8714 die->die_abbrev = abbrev_id;
8715 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8718 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8719 by die_abbrev's usage count, from the most commonly used
8720 abbreviation to the least. */
8722 static int
8723 die_abbrev_cmp (const void *p1, const void *p2)
8725 dw_die_ref die1 = *(const dw_die_ref *) p1;
8726 dw_die_ref die2 = *(const dw_die_ref *) p2;
8728 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8729 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8731 if (die1->die_abbrev >= abbrev_opt_base_type_end
8732 && die2->die_abbrev >= abbrev_opt_base_type_end)
8734 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8735 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8736 return -1;
8737 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8738 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8739 return 1;
8742 /* Stabilize the sort. */
8743 if (die1->die_abbrev < die2->die_abbrev)
8744 return -1;
8745 if (die1->die_abbrev > die2->die_abbrev)
8746 return 1;
8748 return 0;
8751 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8752 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8753 into dw_val_class_const_implicit or
8754 dw_val_class_unsigned_const_implicit. */
8756 static void
8757 optimize_implicit_const (unsigned int first_id, unsigned int end,
8758 vec<bool> &implicit_consts)
8760 /* It never makes sense if there is just one DIE using the abbreviation. */
8761 if (end < first_id + 2)
8762 return;
8764 dw_attr_node *a;
8765 unsigned ix, i;
8766 dw_die_ref die = sorted_abbrev_dies[first_id];
8767 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8768 if (implicit_consts[ix])
8770 enum dw_val_class new_class = dw_val_class_none;
8771 switch (AT_class (a))
8773 case dw_val_class_unsigned_const:
8774 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8775 continue;
8777 /* The .debug_abbrev section will grow by
8778 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8779 in all the DIEs using that abbreviation. */
8780 if (constant_size (AT_unsigned (a)) * (end - first_id)
8781 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8782 continue;
8784 new_class = dw_val_class_unsigned_const_implicit;
8785 break;
8787 case dw_val_class_const:
8788 new_class = dw_val_class_const_implicit;
8789 break;
8791 case dw_val_class_file:
8792 new_class = dw_val_class_file_implicit;
8793 break;
8795 default:
8796 continue;
8798 for (i = first_id; i < end; i++)
8799 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8800 = new_class;
8804 /* Attempt to optimize abbreviation table from abbrev_opt_start
8805 abbreviation above. */
8807 static void
8808 optimize_abbrev_table (void)
8810 if (abbrev_opt_start
8811 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8812 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8814 auto_vec<bool, 32> implicit_consts;
8815 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8817 unsigned int abbrev_id = abbrev_opt_start - 1;
8818 unsigned int first_id = ~0U;
8819 unsigned int last_abbrev_id = 0;
8820 unsigned int i;
8821 dw_die_ref die;
8822 if (abbrev_opt_base_type_end > abbrev_opt_start)
8823 abbrev_id = abbrev_opt_base_type_end - 1;
8824 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8825 most commonly used abbreviations come first. */
8826 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8828 dw_attr_node *a;
8829 unsigned ix;
8831 /* If calc_base_type_die_sizes has been called, the CU and
8832 base types after it can't be optimized, because we've already
8833 calculated their DIE offsets. We've sorted them first. */
8834 if (die->die_abbrev < abbrev_opt_base_type_end)
8835 continue;
8836 if (die->die_abbrev != last_abbrev_id)
8838 last_abbrev_id = die->die_abbrev;
8839 if (dwarf_version >= 5 && first_id != ~0U)
8840 optimize_implicit_const (first_id, i, implicit_consts);
8841 abbrev_id++;
8842 (*abbrev_die_table)[abbrev_id] = die;
8843 if (dwarf_version >= 5)
8845 first_id = i;
8846 implicit_consts.truncate (0);
8848 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8849 switch (AT_class (a))
8851 case dw_val_class_const:
8852 case dw_val_class_unsigned_const:
8853 case dw_val_class_file:
8854 implicit_consts.safe_push (true);
8855 break;
8856 default:
8857 implicit_consts.safe_push (false);
8858 break;
8862 else if (dwarf_version >= 5)
8864 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8865 if (!implicit_consts[ix])
8866 continue;
8867 else
8869 dw_attr_node *other_a
8870 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8871 if (!dw_val_equal_p (&a->dw_attr_val,
8872 &other_a->dw_attr_val))
8873 implicit_consts[ix] = false;
8876 die->die_abbrev = abbrev_id;
8878 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8879 if (dwarf_version >= 5 && first_id != ~0U)
8880 optimize_implicit_const (first_id, i, implicit_consts);
8883 abbrev_opt_start = 0;
8884 abbrev_opt_base_type_end = 0;
8885 abbrev_usage_count.release ();
8886 sorted_abbrev_dies.release ();
8889 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8891 static int
8892 constant_size (unsigned HOST_WIDE_INT value)
8894 int log;
8896 if (value == 0)
8897 log = 0;
8898 else
8899 log = floor_log2 (value);
8901 log = log / 8;
8902 log = 1 << (floor_log2 (log) + 1);
8904 return log;
8907 /* Return the size of a DIE as it is represented in the
8908 .debug_info section. */
8910 static unsigned long
8911 size_of_die (dw_die_ref die)
8913 unsigned long size = 0;
8914 dw_attr_node *a;
8915 unsigned ix;
8916 enum dwarf_form form;
8918 size += size_of_uleb128 (die->die_abbrev);
8919 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8921 switch (AT_class (a))
8923 case dw_val_class_addr:
8924 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8926 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8927 size += size_of_uleb128 (AT_index (a));
8929 else
8930 size += DWARF2_ADDR_SIZE;
8931 break;
8932 case dw_val_class_offset:
8933 size += DWARF_OFFSET_SIZE;
8934 break;
8935 case dw_val_class_loc:
8937 unsigned long lsize = size_of_locs (AT_loc (a));
8939 /* Block length. */
8940 if (dwarf_version >= 4)
8941 size += size_of_uleb128 (lsize);
8942 else
8943 size += constant_size (lsize);
8944 size += lsize;
8946 break;
8947 case dw_val_class_loc_list:
8948 if (dwarf_split_debug_info && dwarf_version >= 5)
8950 gcc_assert (AT_loc_list (a)->num_assigned);
8951 size += size_of_uleb128 (AT_loc_list (a)->hash);
8953 else
8954 size += DWARF_OFFSET_SIZE;
8955 break;
8956 case dw_val_class_range_list:
8957 if (value_format (a) == DW_FORM_rnglistx)
8959 gcc_assert (rnglist_idx);
8960 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8961 size += size_of_uleb128 (r->idx);
8963 else
8964 size += DWARF_OFFSET_SIZE;
8965 break;
8966 case dw_val_class_const:
8967 size += size_of_sleb128 (AT_int (a));
8968 break;
8969 case dw_val_class_unsigned_const:
8971 int csize = constant_size (AT_unsigned (a));
8972 if (dwarf_version == 3
8973 && a->dw_attr == DW_AT_data_member_location
8974 && csize >= 4)
8975 size += size_of_uleb128 (AT_unsigned (a));
8976 else
8977 size += csize;
8979 break;
8980 case dw_val_class_const_implicit:
8981 case dw_val_class_unsigned_const_implicit:
8982 case dw_val_class_file_implicit:
8983 /* These occupy no size in the DIE, just an extra sleb128 in
8984 .debug_abbrev. */
8985 break;
8986 case dw_val_class_const_double:
8987 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8988 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8989 size++; /* block */
8990 break;
8991 case dw_val_class_wide_int:
8992 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8993 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8994 if (get_full_len (*a->dw_attr_val.v.val_wide)
8995 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8996 size++; /* block */
8997 break;
8998 case dw_val_class_vec:
8999 size += constant_size (a->dw_attr_val.v.val_vec.length
9000 * a->dw_attr_val.v.val_vec.elt_size)
9001 + a->dw_attr_val.v.val_vec.length
9002 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9003 break;
9004 case dw_val_class_flag:
9005 if (dwarf_version >= 4)
9006 /* Currently all add_AT_flag calls pass in 1 as last argument,
9007 so DW_FORM_flag_present can be used. If that ever changes,
9008 we'll need to use DW_FORM_flag and have some optimization
9009 in build_abbrev_table that will change those to
9010 DW_FORM_flag_present if it is set to 1 in all DIEs using
9011 the same abbrev entry. */
9012 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9013 else
9014 size += 1;
9015 break;
9016 case dw_val_class_die_ref:
9017 if (AT_ref_external (a))
9019 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9020 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9021 is sized by target address length, whereas in DWARF3
9022 it's always sized as an offset. */
9023 if (use_debug_types)
9024 size += DWARF_TYPE_SIGNATURE_SIZE;
9025 else if (dwarf_version == 2)
9026 size += DWARF2_ADDR_SIZE;
9027 else
9028 size += DWARF_OFFSET_SIZE;
9030 else
9031 size += DWARF_OFFSET_SIZE;
9032 break;
9033 case dw_val_class_fde_ref:
9034 size += DWARF_OFFSET_SIZE;
9035 break;
9036 case dw_val_class_lbl_id:
9037 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9039 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9040 size += size_of_uleb128 (AT_index (a));
9042 else
9043 size += DWARF2_ADDR_SIZE;
9044 break;
9045 case dw_val_class_lineptr:
9046 case dw_val_class_macptr:
9047 case dw_val_class_loclistsptr:
9048 size += DWARF_OFFSET_SIZE;
9049 break;
9050 case dw_val_class_str:
9051 form = AT_string_form (a);
9052 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9053 size += DWARF_OFFSET_SIZE;
9054 else if (form == DW_FORM_GNU_str_index)
9055 size += size_of_uleb128 (AT_index (a));
9056 else
9057 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9058 break;
9059 case dw_val_class_file:
9060 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9061 break;
9062 case dw_val_class_data8:
9063 size += 8;
9064 break;
9065 case dw_val_class_vms_delta:
9066 size += DWARF_OFFSET_SIZE;
9067 break;
9068 case dw_val_class_high_pc:
9069 size += DWARF2_ADDR_SIZE;
9070 break;
9071 case dw_val_class_discr_value:
9072 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9073 break;
9074 case dw_val_class_discr_list:
9076 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9078 /* This is a block, so we have the block length and then its
9079 data. */
9080 size += constant_size (block_size) + block_size;
9082 break;
9083 default:
9084 gcc_unreachable ();
9088 return size;
9091 /* Size the debugging information associated with a given DIE. Visits the
9092 DIE's children recursively. Updates the global variable next_die_offset, on
9093 each time through. Uses the current value of next_die_offset to update the
9094 die_offset field in each DIE. */
9096 static void
9097 calc_die_sizes (dw_die_ref die)
9099 dw_die_ref c;
9101 gcc_assert (die->die_offset == 0
9102 || (unsigned long int) die->die_offset == next_die_offset);
9103 die->die_offset = next_die_offset;
9104 next_die_offset += size_of_die (die);
9106 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9108 if (die->die_child != NULL)
9109 /* Count the null byte used to terminate sibling lists. */
9110 next_die_offset += 1;
9113 /* Size just the base type children at the start of the CU.
9114 This is needed because build_abbrev needs to size locs
9115 and sizing of type based stack ops needs to know die_offset
9116 values for the base types. */
9118 static void
9119 calc_base_type_die_sizes (void)
9121 unsigned long die_offset = (dwarf_split_debug_info
9122 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9123 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9124 unsigned int i;
9125 dw_die_ref base_type;
9126 #if ENABLE_ASSERT_CHECKING
9127 dw_die_ref prev = comp_unit_die ()->die_child;
9128 #endif
9130 die_offset += size_of_die (comp_unit_die ());
9131 for (i = 0; base_types.iterate (i, &base_type); i++)
9133 #if ENABLE_ASSERT_CHECKING
9134 gcc_assert (base_type->die_offset == 0
9135 && prev->die_sib == base_type
9136 && base_type->die_child == NULL
9137 && base_type->die_abbrev);
9138 prev = base_type;
9139 #endif
9140 if (abbrev_opt_start
9141 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9142 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9143 base_type->die_offset = die_offset;
9144 die_offset += size_of_die (base_type);
9148 /* Set the marks for a die and its children. We do this so
9149 that we know whether or not a reference needs to use FORM_ref_addr; only
9150 DIEs in the same CU will be marked. We used to clear out the offset
9151 and use that as the flag, but ran into ordering problems. */
9153 static void
9154 mark_dies (dw_die_ref die)
9156 dw_die_ref c;
9158 gcc_assert (!die->die_mark);
9160 die->die_mark = 1;
9161 FOR_EACH_CHILD (die, c, mark_dies (c));
9164 /* Clear the marks for a die and its children. */
9166 static void
9167 unmark_dies (dw_die_ref die)
9169 dw_die_ref c;
9171 if (! use_debug_types)
9172 gcc_assert (die->die_mark);
9174 die->die_mark = 0;
9175 FOR_EACH_CHILD (die, c, unmark_dies (c));
9178 /* Clear the marks for a die, its children and referred dies. */
9180 static void
9181 unmark_all_dies (dw_die_ref die)
9183 dw_die_ref c;
9184 dw_attr_node *a;
9185 unsigned ix;
9187 if (!die->die_mark)
9188 return;
9189 die->die_mark = 0;
9191 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9193 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9194 if (AT_class (a) == dw_val_class_die_ref)
9195 unmark_all_dies (AT_ref (a));
9198 /* Calculate if the entry should appear in the final output file. It may be
9199 from a pruned a type. */
9201 static bool
9202 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9204 /* By limiting gnu pubnames to definitions only, gold can generate a
9205 gdb index without entries for declarations, which don't include
9206 enough information to be useful. */
9207 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9208 return false;
9210 if (table == pubname_table)
9212 /* Enumerator names are part of the pubname table, but the
9213 parent DW_TAG_enumeration_type die may have been pruned.
9214 Don't output them if that is the case. */
9215 if (p->die->die_tag == DW_TAG_enumerator &&
9216 (p->die->die_parent == NULL
9217 || !p->die->die_parent->die_perennial_p))
9218 return false;
9220 /* Everything else in the pubname table is included. */
9221 return true;
9224 /* The pubtypes table shouldn't include types that have been
9225 pruned. */
9226 return (p->die->die_offset != 0
9227 || !flag_eliminate_unused_debug_types);
9230 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9231 generated for the compilation unit. */
9233 static unsigned long
9234 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9236 unsigned long size;
9237 unsigned i;
9238 pubname_entry *p;
9239 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9241 size = DWARF_PUBNAMES_HEADER_SIZE;
9242 FOR_EACH_VEC_ELT (*names, i, p)
9243 if (include_pubname_in_output (names, p))
9244 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9246 size += DWARF_OFFSET_SIZE;
9247 return size;
9250 /* Return the size of the information in the .debug_aranges section. */
9252 static unsigned long
9253 size_of_aranges (void)
9255 unsigned long size;
9257 size = DWARF_ARANGES_HEADER_SIZE;
9259 /* Count the address/length pair for this compilation unit. */
9260 if (text_section_used)
9261 size += 2 * DWARF2_ADDR_SIZE;
9262 if (cold_text_section_used)
9263 size += 2 * DWARF2_ADDR_SIZE;
9264 if (have_multiple_function_sections)
9266 unsigned fde_idx;
9267 dw_fde_ref fde;
9269 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9271 if (DECL_IGNORED_P (fde->decl))
9272 continue;
9273 if (!fde->in_std_section)
9274 size += 2 * DWARF2_ADDR_SIZE;
9275 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9276 size += 2 * DWARF2_ADDR_SIZE;
9280 /* Count the two zero words used to terminated the address range table. */
9281 size += 2 * DWARF2_ADDR_SIZE;
9282 return size;
9285 /* Select the encoding of an attribute value. */
9287 static enum dwarf_form
9288 value_format (dw_attr_node *a)
9290 switch (AT_class (a))
9292 case dw_val_class_addr:
9293 /* Only very few attributes allow DW_FORM_addr. */
9294 switch (a->dw_attr)
9296 case DW_AT_low_pc:
9297 case DW_AT_high_pc:
9298 case DW_AT_entry_pc:
9299 case DW_AT_trampoline:
9300 return (AT_index (a) == NOT_INDEXED
9301 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9302 default:
9303 break;
9305 switch (DWARF2_ADDR_SIZE)
9307 case 1:
9308 return DW_FORM_data1;
9309 case 2:
9310 return DW_FORM_data2;
9311 case 4:
9312 return DW_FORM_data4;
9313 case 8:
9314 return DW_FORM_data8;
9315 default:
9316 gcc_unreachable ();
9318 case dw_val_class_loc_list:
9319 if (dwarf_split_debug_info
9320 && dwarf_version >= 5
9321 && AT_loc_list (a)->num_assigned)
9322 return DW_FORM_loclistx;
9323 /* FALLTHRU */
9324 case dw_val_class_range_list:
9325 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9326 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9327 care about sizes of .debug* sections in shared libraries and
9328 executables and don't take into account relocations that affect just
9329 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9330 table in the .debug_rnglists section. */
9331 if (dwarf_split_debug_info
9332 && dwarf_version >= 5
9333 && AT_class (a) == dw_val_class_range_list
9334 && rnglist_idx
9335 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9336 return DW_FORM_rnglistx;
9337 if (dwarf_version >= 4)
9338 return DW_FORM_sec_offset;
9339 /* FALLTHRU */
9340 case dw_val_class_vms_delta:
9341 case dw_val_class_offset:
9342 switch (DWARF_OFFSET_SIZE)
9344 case 4:
9345 return DW_FORM_data4;
9346 case 8:
9347 return DW_FORM_data8;
9348 default:
9349 gcc_unreachable ();
9351 case dw_val_class_loc:
9352 if (dwarf_version >= 4)
9353 return DW_FORM_exprloc;
9354 switch (constant_size (size_of_locs (AT_loc (a))))
9356 case 1:
9357 return DW_FORM_block1;
9358 case 2:
9359 return DW_FORM_block2;
9360 case 4:
9361 return DW_FORM_block4;
9362 default:
9363 gcc_unreachable ();
9365 case dw_val_class_const:
9366 return DW_FORM_sdata;
9367 case dw_val_class_unsigned_const:
9368 switch (constant_size (AT_unsigned (a)))
9370 case 1:
9371 return DW_FORM_data1;
9372 case 2:
9373 return DW_FORM_data2;
9374 case 4:
9375 /* In DWARF3 DW_AT_data_member_location with
9376 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9377 constant, so we need to use DW_FORM_udata if we need
9378 a large constant. */
9379 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9380 return DW_FORM_udata;
9381 return DW_FORM_data4;
9382 case 8:
9383 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9384 return DW_FORM_udata;
9385 return DW_FORM_data8;
9386 default:
9387 gcc_unreachable ();
9389 case dw_val_class_const_implicit:
9390 case dw_val_class_unsigned_const_implicit:
9391 case dw_val_class_file_implicit:
9392 return DW_FORM_implicit_const;
9393 case dw_val_class_const_double:
9394 switch (HOST_BITS_PER_WIDE_INT)
9396 case 8:
9397 return DW_FORM_data2;
9398 case 16:
9399 return DW_FORM_data4;
9400 case 32:
9401 return DW_FORM_data8;
9402 case 64:
9403 if (dwarf_version >= 5)
9404 return DW_FORM_data16;
9405 /* FALLTHRU */
9406 default:
9407 return DW_FORM_block1;
9409 case dw_val_class_wide_int:
9410 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9412 case 8:
9413 return DW_FORM_data1;
9414 case 16:
9415 return DW_FORM_data2;
9416 case 32:
9417 return DW_FORM_data4;
9418 case 64:
9419 return DW_FORM_data8;
9420 case 128:
9421 if (dwarf_version >= 5)
9422 return DW_FORM_data16;
9423 /* FALLTHRU */
9424 default:
9425 return DW_FORM_block1;
9427 case dw_val_class_vec:
9428 switch (constant_size (a->dw_attr_val.v.val_vec.length
9429 * a->dw_attr_val.v.val_vec.elt_size))
9431 case 1:
9432 return DW_FORM_block1;
9433 case 2:
9434 return DW_FORM_block2;
9435 case 4:
9436 return DW_FORM_block4;
9437 default:
9438 gcc_unreachable ();
9440 case dw_val_class_flag:
9441 if (dwarf_version >= 4)
9443 /* Currently all add_AT_flag calls pass in 1 as last argument,
9444 so DW_FORM_flag_present can be used. If that ever changes,
9445 we'll need to use DW_FORM_flag and have some optimization
9446 in build_abbrev_table that will change those to
9447 DW_FORM_flag_present if it is set to 1 in all DIEs using
9448 the same abbrev entry. */
9449 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9450 return DW_FORM_flag_present;
9452 return DW_FORM_flag;
9453 case dw_val_class_die_ref:
9454 if (AT_ref_external (a))
9455 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9456 else
9457 return DW_FORM_ref;
9458 case dw_val_class_fde_ref:
9459 return DW_FORM_data;
9460 case dw_val_class_lbl_id:
9461 return (AT_index (a) == NOT_INDEXED
9462 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9463 case dw_val_class_lineptr:
9464 case dw_val_class_macptr:
9465 case dw_val_class_loclistsptr:
9466 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9467 case dw_val_class_str:
9468 return AT_string_form (a);
9469 case dw_val_class_file:
9470 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9472 case 1:
9473 return DW_FORM_data1;
9474 case 2:
9475 return DW_FORM_data2;
9476 case 4:
9477 return DW_FORM_data4;
9478 default:
9479 gcc_unreachable ();
9482 case dw_val_class_data8:
9483 return DW_FORM_data8;
9485 case dw_val_class_high_pc:
9486 switch (DWARF2_ADDR_SIZE)
9488 case 1:
9489 return DW_FORM_data1;
9490 case 2:
9491 return DW_FORM_data2;
9492 case 4:
9493 return DW_FORM_data4;
9494 case 8:
9495 return DW_FORM_data8;
9496 default:
9497 gcc_unreachable ();
9500 case dw_val_class_discr_value:
9501 return (a->dw_attr_val.v.val_discr_value.pos
9502 ? DW_FORM_udata
9503 : DW_FORM_sdata);
9504 case dw_val_class_discr_list:
9505 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9507 case 1:
9508 return DW_FORM_block1;
9509 case 2:
9510 return DW_FORM_block2;
9511 case 4:
9512 return DW_FORM_block4;
9513 default:
9514 gcc_unreachable ();
9517 default:
9518 gcc_unreachable ();
9522 /* Output the encoding of an attribute value. */
9524 static void
9525 output_value_format (dw_attr_node *a)
9527 enum dwarf_form form = value_format (a);
9529 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9532 /* Given a die and id, produce the appropriate abbreviations. */
9534 static void
9535 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9537 unsigned ix;
9538 dw_attr_node *a_attr;
9540 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9541 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9542 dwarf_tag_name (abbrev->die_tag));
9544 if (abbrev->die_child != NULL)
9545 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9546 else
9547 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9549 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9551 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9552 dwarf_attr_name (a_attr->dw_attr));
9553 output_value_format (a_attr);
9554 if (value_format (a_attr) == DW_FORM_implicit_const)
9556 if (AT_class (a_attr) == dw_val_class_file_implicit)
9558 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9559 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9560 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9562 else
9563 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9567 dw2_asm_output_data (1, 0, NULL);
9568 dw2_asm_output_data (1, 0, NULL);
9572 /* Output the .debug_abbrev section which defines the DIE abbreviation
9573 table. */
9575 static void
9576 output_abbrev_section (void)
9578 unsigned int abbrev_id;
9579 dw_die_ref abbrev;
9581 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9582 if (abbrev_id != 0)
9583 output_die_abbrevs (abbrev_id, abbrev);
9585 /* Terminate the table. */
9586 dw2_asm_output_data (1, 0, NULL);
9589 /* Output a symbol we can use to refer to this DIE from another CU. */
9591 static inline void
9592 output_die_symbol (dw_die_ref die)
9594 const char *sym = die->die_id.die_symbol;
9596 gcc_assert (!die->comdat_type_p);
9598 if (sym == 0)
9599 return;
9601 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9602 /* We make these global, not weak; if the target doesn't support
9603 .linkonce, it doesn't support combining the sections, so debugging
9604 will break. */
9605 targetm.asm_out.globalize_label (asm_out_file, sym);
9607 ASM_OUTPUT_LABEL (asm_out_file, sym);
9610 /* Return a new location list, given the begin and end range, and the
9611 expression. */
9613 static inline dw_loc_list_ref
9614 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9615 const char *section)
9617 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9619 retlist->begin = begin;
9620 retlist->begin_entry = NULL;
9621 retlist->end = end;
9622 retlist->expr = expr;
9623 retlist->section = section;
9625 return retlist;
9628 /* Generate a new internal symbol for this location list node, if it
9629 hasn't got one yet. */
9631 static inline void
9632 gen_llsym (dw_loc_list_ref list)
9634 gcc_assert (!list->ll_symbol);
9635 list->ll_symbol = gen_internal_sym ("LLST");
9638 /* Output the location list given to us. */
9640 static void
9641 output_loc_list (dw_loc_list_ref list_head)
9643 if (list_head->emitted)
9644 return;
9645 list_head->emitted = true;
9647 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9649 dw_loc_list_ref curr = list_head;
9650 const char *last_section = NULL;
9651 const char *base_label = NULL;
9653 /* Walk the location list, and output each range + expression. */
9654 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9656 unsigned long size;
9657 /* Don't output an entry that starts and ends at the same address. */
9658 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9659 continue;
9660 size = size_of_locs (curr->expr);
9661 /* If the expression is too large, drop it on the floor. We could
9662 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9663 in the expression, but >= 64KB expressions for a single value
9664 in a single range are unlikely very useful. */
9665 if (dwarf_version < 5 && size > 0xffff)
9666 continue;
9667 if (dwarf_version >= 5)
9669 if (dwarf_split_debug_info)
9671 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9672 uleb128 index into .debug_addr and uleb128 length. */
9673 dw2_asm_output_data (1, DW_LLE_startx_length,
9674 "DW_LLE_startx_length (%s)",
9675 list_head->ll_symbol);
9676 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9677 "Location list range start index "
9678 "(%s)", curr->begin);
9679 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9680 For that case we probably need to emit DW_LLE_startx_endx,
9681 but we'd need 2 .debug_addr entries rather than just one. */
9682 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9683 "Location list length (%s)",
9684 list_head->ll_symbol);
9686 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9688 /* If all code is in .text section, the base address is
9689 already provided by the CU attributes. Use
9690 DW_LLE_offset_pair where both addresses are uleb128 encoded
9691 offsets against that base. */
9692 dw2_asm_output_data (1, DW_LLE_offset_pair,
9693 "DW_LLE_offset_pair (%s)",
9694 list_head->ll_symbol);
9695 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9696 "Location list begin address (%s)",
9697 list_head->ll_symbol);
9698 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9699 "Location list end address (%s)",
9700 list_head->ll_symbol);
9702 else if (HAVE_AS_LEB128)
9704 /* Otherwise, find out how many consecutive entries could share
9705 the same base entry. If just one, emit DW_LLE_start_length,
9706 otherwise emit DW_LLE_base_address for the base address
9707 followed by a series of DW_LLE_offset_pair. */
9708 if (last_section == NULL || curr->section != last_section)
9710 dw_loc_list_ref curr2;
9711 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9712 curr2 = curr2->dw_loc_next)
9714 if (strcmp (curr2->begin, curr2->end) == 0
9715 && !curr2->force)
9716 continue;
9717 break;
9719 if (curr2 == NULL || curr->section != curr2->section)
9720 last_section = NULL;
9721 else
9723 last_section = curr->section;
9724 base_label = curr->begin;
9725 dw2_asm_output_data (1, DW_LLE_base_address,
9726 "DW_LLE_base_address (%s)",
9727 list_head->ll_symbol);
9728 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9729 "Base address (%s)",
9730 list_head->ll_symbol);
9733 /* Only one entry with the same base address. Use
9734 DW_LLE_start_length with absolute address and uleb128
9735 length. */
9736 if (last_section == NULL)
9738 dw2_asm_output_data (1, DW_LLE_start_length,
9739 "DW_LLE_start_length (%s)",
9740 list_head->ll_symbol);
9741 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9742 "Location list begin address (%s)",
9743 list_head->ll_symbol);
9744 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9745 "Location list length "
9746 "(%s)", list_head->ll_symbol);
9748 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9749 DW_LLE_base_address. */
9750 else
9752 dw2_asm_output_data (1, DW_LLE_offset_pair,
9753 "DW_LLE_offset_pair (%s)",
9754 list_head->ll_symbol);
9755 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9756 "Location list begin address "
9757 "(%s)", list_head->ll_symbol);
9758 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9759 "Location list end address "
9760 "(%s)", list_head->ll_symbol);
9763 /* The assembler does not support .uleb128 directive. Emit
9764 DW_LLE_start_end with a pair of absolute addresses. */
9765 else
9767 dw2_asm_output_data (1, DW_LLE_start_end,
9768 "DW_LLE_start_end (%s)",
9769 list_head->ll_symbol);
9770 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9771 "Location list begin address (%s)",
9772 list_head->ll_symbol);
9773 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9774 "Location list end address (%s)",
9775 list_head->ll_symbol);
9778 else if (dwarf_split_debug_info)
9780 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9781 and 4 byte length. */
9782 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9783 "Location list start/length entry (%s)",
9784 list_head->ll_symbol);
9785 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9786 "Location list range start index (%s)",
9787 curr->begin);
9788 /* The length field is 4 bytes. If we ever need to support
9789 an 8-byte length, we can add a new DW_LLE code or fall back
9790 to DW_LLE_GNU_start_end_entry. */
9791 dw2_asm_output_delta (4, curr->end, curr->begin,
9792 "Location list range length (%s)",
9793 list_head->ll_symbol);
9795 else if (!have_multiple_function_sections)
9797 /* Pair of relative addresses against start of text section. */
9798 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9799 "Location list begin address (%s)",
9800 list_head->ll_symbol);
9801 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9802 "Location list end address (%s)",
9803 list_head->ll_symbol);
9805 else
9807 /* Pair of absolute addresses. */
9808 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9809 "Location list begin address (%s)",
9810 list_head->ll_symbol);
9811 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9812 "Location list end address (%s)",
9813 list_head->ll_symbol);
9816 /* Output the block length for this list of location operations. */
9817 if (dwarf_version >= 5)
9818 dw2_asm_output_data_uleb128 (size, "Location expression size");
9819 else
9821 gcc_assert (size <= 0xffff);
9822 dw2_asm_output_data (2, size, "Location expression size");
9825 output_loc_sequence (curr->expr, -1);
9828 /* And finally list termination. */
9829 if (dwarf_version >= 5)
9830 dw2_asm_output_data (1, DW_LLE_end_of_list,
9831 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9832 else if (dwarf_split_debug_info)
9833 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9834 "Location list terminator (%s)",
9835 list_head->ll_symbol);
9836 else
9838 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9839 "Location list terminator begin (%s)",
9840 list_head->ll_symbol);
9841 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9842 "Location list terminator end (%s)",
9843 list_head->ll_symbol);
9847 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9848 section. Emit a relocated reference if val_entry is NULL, otherwise,
9849 emit an indirect reference. */
9851 static void
9852 output_range_list_offset (dw_attr_node *a)
9854 const char *name = dwarf_attr_name (a->dw_attr);
9856 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9858 if (dwarf_version >= 5)
9860 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9861 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9862 debug_ranges_section, "%s", name);
9864 else
9866 char *p = strchr (ranges_section_label, '\0');
9867 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9868 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9869 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9870 debug_ranges_section, "%s", name);
9871 *p = '\0';
9874 else if (dwarf_version >= 5)
9876 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9877 gcc_assert (rnglist_idx);
9878 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9880 else
9881 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9882 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9883 "%s (offset from %s)", name, ranges_section_label);
9886 /* Output the offset into the debug_loc section. */
9888 static void
9889 output_loc_list_offset (dw_attr_node *a)
9891 char *sym = AT_loc_list (a)->ll_symbol;
9893 gcc_assert (sym);
9894 if (!dwarf_split_debug_info)
9895 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9896 "%s", dwarf_attr_name (a->dw_attr));
9897 else if (dwarf_version >= 5)
9899 gcc_assert (AT_loc_list (a)->num_assigned);
9900 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9901 dwarf_attr_name (a->dw_attr),
9902 sym);
9904 else
9905 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9906 "%s", dwarf_attr_name (a->dw_attr));
9909 /* Output an attribute's index or value appropriately. */
9911 static void
9912 output_attr_index_or_value (dw_attr_node *a)
9914 const char *name = dwarf_attr_name (a->dw_attr);
9916 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9918 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9919 return;
9921 switch (AT_class (a))
9923 case dw_val_class_addr:
9924 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9925 break;
9926 case dw_val_class_high_pc:
9927 case dw_val_class_lbl_id:
9928 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9929 break;
9930 default:
9931 gcc_unreachable ();
9935 /* Output a type signature. */
9937 static inline void
9938 output_signature (const char *sig, const char *name)
9940 int i;
9942 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9943 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9946 /* Output a discriminant value. */
9948 static inline void
9949 output_discr_value (dw_discr_value *discr_value, const char *name)
9951 if (discr_value->pos)
9952 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9953 else
9954 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9957 /* Output the DIE and its attributes. Called recursively to generate
9958 the definitions of each child DIE. */
9960 static void
9961 output_die (dw_die_ref die)
9963 dw_attr_node *a;
9964 dw_die_ref c;
9965 unsigned long size;
9966 unsigned ix;
9968 /* If someone in another CU might refer to us, set up a symbol for
9969 them to point to. */
9970 if (! die->comdat_type_p && die->die_id.die_symbol)
9971 output_die_symbol (die);
9973 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9974 (unsigned long)die->die_offset,
9975 dwarf_tag_name (die->die_tag));
9977 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9979 const char *name = dwarf_attr_name (a->dw_attr);
9981 switch (AT_class (a))
9983 case dw_val_class_addr:
9984 output_attr_index_or_value (a);
9985 break;
9987 case dw_val_class_offset:
9988 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9989 "%s", name);
9990 break;
9992 case dw_val_class_range_list:
9993 output_range_list_offset (a);
9994 break;
9996 case dw_val_class_loc:
9997 size = size_of_locs (AT_loc (a));
9999 /* Output the block length for this list of location operations. */
10000 if (dwarf_version >= 4)
10001 dw2_asm_output_data_uleb128 (size, "%s", name);
10002 else
10003 dw2_asm_output_data (constant_size (size), size, "%s", name);
10005 output_loc_sequence (AT_loc (a), -1);
10006 break;
10008 case dw_val_class_const:
10009 /* ??? It would be slightly more efficient to use a scheme like is
10010 used for unsigned constants below, but gdb 4.x does not sign
10011 extend. Gdb 5.x does sign extend. */
10012 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10013 break;
10015 case dw_val_class_unsigned_const:
10017 int csize = constant_size (AT_unsigned (a));
10018 if (dwarf_version == 3
10019 && a->dw_attr == DW_AT_data_member_location
10020 && csize >= 4)
10021 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10022 else
10023 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10025 break;
10027 case dw_val_class_const_implicit:
10028 if (flag_debug_asm)
10029 fprintf (asm_out_file, "\t\t\t%s %s ("
10030 HOST_WIDE_INT_PRINT_DEC ")\n",
10031 ASM_COMMENT_START, name, AT_int (a));
10032 break;
10034 case dw_val_class_unsigned_const_implicit:
10035 if (flag_debug_asm)
10036 fprintf (asm_out_file, "\t\t\t%s %s ("
10037 HOST_WIDE_INT_PRINT_HEX ")\n",
10038 ASM_COMMENT_START, name, AT_unsigned (a));
10039 break;
10041 case dw_val_class_const_double:
10043 unsigned HOST_WIDE_INT first, second;
10045 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10046 dw2_asm_output_data (1,
10047 HOST_BITS_PER_DOUBLE_INT
10048 / HOST_BITS_PER_CHAR,
10049 NULL);
10051 if (WORDS_BIG_ENDIAN)
10053 first = a->dw_attr_val.v.val_double.high;
10054 second = a->dw_attr_val.v.val_double.low;
10056 else
10058 first = a->dw_attr_val.v.val_double.low;
10059 second = a->dw_attr_val.v.val_double.high;
10062 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10063 first, "%s", name);
10064 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10065 second, NULL);
10067 break;
10069 case dw_val_class_wide_int:
10071 int i;
10072 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10073 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10074 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10075 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10076 * l, NULL);
10078 if (WORDS_BIG_ENDIAN)
10079 for (i = len - 1; i >= 0; --i)
10081 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10082 "%s", name);
10083 name = "";
10085 else
10086 for (i = 0; i < len; ++i)
10088 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10089 "%s", name);
10090 name = "";
10093 break;
10095 case dw_val_class_vec:
10097 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10098 unsigned int len = a->dw_attr_val.v.val_vec.length;
10099 unsigned int i;
10100 unsigned char *p;
10102 dw2_asm_output_data (constant_size (len * elt_size),
10103 len * elt_size, "%s", name);
10104 if (elt_size > sizeof (HOST_WIDE_INT))
10106 elt_size /= 2;
10107 len *= 2;
10109 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10110 i < len;
10111 i++, p += elt_size)
10112 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10113 "fp or vector constant word %u", i);
10114 break;
10117 case dw_val_class_flag:
10118 if (dwarf_version >= 4)
10120 /* Currently all add_AT_flag calls pass in 1 as last argument,
10121 so DW_FORM_flag_present can be used. If that ever changes,
10122 we'll need to use DW_FORM_flag and have some optimization
10123 in build_abbrev_table that will change those to
10124 DW_FORM_flag_present if it is set to 1 in all DIEs using
10125 the same abbrev entry. */
10126 gcc_assert (AT_flag (a) == 1);
10127 if (flag_debug_asm)
10128 fprintf (asm_out_file, "\t\t\t%s %s\n",
10129 ASM_COMMENT_START, name);
10130 break;
10132 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10133 break;
10135 case dw_val_class_loc_list:
10136 output_loc_list_offset (a);
10137 break;
10139 case dw_val_class_die_ref:
10140 if (AT_ref_external (a))
10142 if (AT_ref (a)->comdat_type_p)
10144 comdat_type_node *type_node
10145 = AT_ref (a)->die_id.die_type_node;
10147 gcc_assert (type_node);
10148 output_signature (type_node->signature, name);
10150 else
10152 const char *sym = AT_ref (a)->die_id.die_symbol;
10153 int size;
10155 gcc_assert (sym);
10156 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10157 length, whereas in DWARF3 it's always sized as an
10158 offset. */
10159 if (dwarf_version == 2)
10160 size = DWARF2_ADDR_SIZE;
10161 else
10162 size = DWARF_OFFSET_SIZE;
10163 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10164 name);
10167 else
10169 gcc_assert (AT_ref (a)->die_offset);
10170 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10171 "%s", name);
10173 break;
10175 case dw_val_class_fde_ref:
10177 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10179 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10180 a->dw_attr_val.v.val_fde_index * 2);
10181 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10182 "%s", name);
10184 break;
10186 case dw_val_class_vms_delta:
10187 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10188 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10189 AT_vms_delta2 (a), AT_vms_delta1 (a),
10190 "%s", name);
10191 #else
10192 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10193 AT_vms_delta2 (a), AT_vms_delta1 (a),
10194 "%s", name);
10195 #endif
10196 break;
10198 case dw_val_class_lbl_id:
10199 output_attr_index_or_value (a);
10200 break;
10202 case dw_val_class_lineptr:
10203 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10204 debug_line_section, "%s", name);
10205 break;
10207 case dw_val_class_macptr:
10208 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10209 debug_macinfo_section, "%s", name);
10210 break;
10212 case dw_val_class_loclistsptr:
10213 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10214 debug_loc_section, "%s", name);
10215 break;
10217 case dw_val_class_str:
10218 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10219 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10220 a->dw_attr_val.v.val_str->label,
10221 debug_str_section,
10222 "%s: \"%s\"", name, AT_string (a));
10223 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10224 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10225 a->dw_attr_val.v.val_str->label,
10226 debug_line_str_section,
10227 "%s: \"%s\"", name, AT_string (a));
10228 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10229 dw2_asm_output_data_uleb128 (AT_index (a),
10230 "%s: \"%s\"", name, AT_string (a));
10231 else
10232 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10233 break;
10235 case dw_val_class_file:
10237 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10239 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10240 a->dw_attr_val.v.val_file->filename);
10241 break;
10244 case dw_val_class_file_implicit:
10245 if (flag_debug_asm)
10246 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10247 ASM_COMMENT_START, name,
10248 maybe_emit_file (a->dw_attr_val.v.val_file),
10249 a->dw_attr_val.v.val_file->filename);
10250 break;
10252 case dw_val_class_data8:
10254 int i;
10256 for (i = 0; i < 8; i++)
10257 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10258 i == 0 ? "%s" : NULL, name);
10259 break;
10262 case dw_val_class_high_pc:
10263 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10264 get_AT_low_pc (die), "DW_AT_high_pc");
10265 break;
10267 case dw_val_class_discr_value:
10268 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10269 break;
10271 case dw_val_class_discr_list:
10273 dw_discr_list_ref list = AT_discr_list (a);
10274 const int size = size_of_discr_list (list);
10276 /* This is a block, so output its length first. */
10277 dw2_asm_output_data (constant_size (size), size,
10278 "%s: block size", name);
10280 for (; list != NULL; list = list->dw_discr_next)
10282 /* One byte for the discriminant value descriptor, and then as
10283 many LEB128 numbers as required. */
10284 if (list->dw_discr_range)
10285 dw2_asm_output_data (1, DW_DSC_range,
10286 "%s: DW_DSC_range", name);
10287 else
10288 dw2_asm_output_data (1, DW_DSC_label,
10289 "%s: DW_DSC_label", name);
10291 output_discr_value (&list->dw_discr_lower_bound, name);
10292 if (list->dw_discr_range)
10293 output_discr_value (&list->dw_discr_upper_bound, name);
10295 break;
10298 default:
10299 gcc_unreachable ();
10303 FOR_EACH_CHILD (die, c, output_die (c));
10305 /* Add null byte to terminate sibling list. */
10306 if (die->die_child != NULL)
10307 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10308 (unsigned long) die->die_offset);
10311 /* Output the compilation unit that appears at the beginning of the
10312 .debug_info section, and precedes the DIE descriptions. */
10314 static void
10315 output_compilation_unit_header (enum dwarf_unit_type ut)
10317 if (!XCOFF_DEBUGGING_INFO)
10319 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10320 dw2_asm_output_data (4, 0xffffffff,
10321 "Initial length escape value indicating 64-bit DWARF extension");
10322 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10323 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10324 "Length of Compilation Unit Info");
10327 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10328 if (dwarf_version >= 5)
10330 const char *name;
10331 switch (ut)
10333 case DW_UT_compile: name = "DW_UT_compile"; break;
10334 case DW_UT_type: name = "DW_UT_type"; break;
10335 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10336 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10337 default: gcc_unreachable ();
10339 dw2_asm_output_data (1, ut, "%s", name);
10340 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10342 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10343 debug_abbrev_section,
10344 "Offset Into Abbrev. Section");
10345 if (dwarf_version < 5)
10346 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10349 /* Output the compilation unit DIE and its children. */
10351 static void
10352 output_comp_unit (dw_die_ref die, int output_if_empty,
10353 const unsigned char *dwo_id)
10355 const char *secname, *oldsym;
10356 char *tmp;
10358 /* Unless we are outputting main CU, we may throw away empty ones. */
10359 if (!output_if_empty && die->die_child == NULL)
10360 return;
10362 /* Even if there are no children of this DIE, we must output the information
10363 about the compilation unit. Otherwise, on an empty translation unit, we
10364 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10365 will then complain when examining the file. First mark all the DIEs in
10366 this CU so we know which get local refs. */
10367 mark_dies (die);
10369 external_ref_hash_type *extern_map = optimize_external_refs (die);
10371 /* For now, optimize only the main CU, in order to optimize the rest
10372 we'd need to see all of them earlier. Leave the rest for post-linking
10373 tools like DWZ. */
10374 if (die == comp_unit_die ())
10375 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10377 build_abbrev_table (die, extern_map);
10379 optimize_abbrev_table ();
10381 delete extern_map;
10383 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10384 next_die_offset = (dwo_id
10385 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10386 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10387 calc_die_sizes (die);
10389 oldsym = die->die_id.die_symbol;
10390 if (oldsym)
10392 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10394 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10395 secname = tmp;
10396 die->die_id.die_symbol = NULL;
10397 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10399 else
10401 switch_to_section (debug_info_section);
10402 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10403 info_section_emitted = true;
10406 /* Output debugging information. */
10407 output_compilation_unit_header (dwo_id
10408 ? DW_UT_split_compile : DW_UT_compile);
10409 if (dwarf_version >= 5)
10411 if (dwo_id != NULL)
10412 for (int i = 0; i < 8; i++)
10413 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10415 output_die (die);
10417 /* Leave the marks on the main CU, so we can check them in
10418 output_pubnames. */
10419 if (oldsym)
10421 unmark_dies (die);
10422 die->die_id.die_symbol = oldsym;
10426 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10427 and .debug_pubtypes. This is configured per-target, but can be
10428 overridden by the -gpubnames or -gno-pubnames options. */
10430 static inline bool
10431 want_pubnames (void)
10433 if (debug_info_level <= DINFO_LEVEL_TERSE)
10434 return false;
10435 if (debug_generate_pub_sections != -1)
10436 return debug_generate_pub_sections;
10437 return targetm.want_debug_pub_sections;
10440 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10442 static void
10443 add_AT_pubnames (dw_die_ref die)
10445 if (want_pubnames ())
10446 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10449 /* Add a string attribute value to a skeleton DIE. */
10451 static inline void
10452 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10453 const char *str)
10455 dw_attr_node attr;
10456 struct indirect_string_node *node;
10458 if (! skeleton_debug_str_hash)
10459 skeleton_debug_str_hash
10460 = hash_table<indirect_string_hasher>::create_ggc (10);
10462 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10463 find_string_form (node);
10464 if (node->form == DW_FORM_GNU_str_index)
10465 node->form = DW_FORM_strp;
10467 attr.dw_attr = attr_kind;
10468 attr.dw_attr_val.val_class = dw_val_class_str;
10469 attr.dw_attr_val.val_entry = NULL;
10470 attr.dw_attr_val.v.val_str = node;
10471 add_dwarf_attr (die, &attr);
10474 /* Helper function to generate top-level dies for skeleton debug_info and
10475 debug_types. */
10477 static void
10478 add_top_level_skeleton_die_attrs (dw_die_ref die)
10480 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10481 const char *comp_dir = comp_dir_string ();
10483 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10484 if (comp_dir != NULL)
10485 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10486 add_AT_pubnames (die);
10487 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10490 /* Output skeleton debug sections that point to the dwo file. */
10492 static void
10493 output_skeleton_debug_sections (dw_die_ref comp_unit,
10494 const unsigned char *dwo_id)
10496 /* These attributes will be found in the full debug_info section. */
10497 remove_AT (comp_unit, DW_AT_producer);
10498 remove_AT (comp_unit, DW_AT_language);
10500 switch_to_section (debug_skeleton_info_section);
10501 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10503 /* Produce the skeleton compilation-unit header. This one differs enough from
10504 a normal CU header that it's better not to call output_compilation_unit
10505 header. */
10506 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10507 dw2_asm_output_data (4, 0xffffffff,
10508 "Initial length escape value indicating 64-bit "
10509 "DWARF extension");
10511 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10512 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10513 - DWARF_INITIAL_LENGTH_SIZE
10514 + size_of_die (comp_unit),
10515 "Length of Compilation Unit Info");
10516 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10517 if (dwarf_version >= 5)
10519 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10520 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10522 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10523 debug_skeleton_abbrev_section,
10524 "Offset Into Abbrev. Section");
10525 if (dwarf_version < 5)
10526 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10527 else
10528 for (int i = 0; i < 8; i++)
10529 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10531 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10532 output_die (comp_unit);
10534 /* Build the skeleton debug_abbrev section. */
10535 switch_to_section (debug_skeleton_abbrev_section);
10536 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10538 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10540 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10543 /* Output a comdat type unit DIE and its children. */
10545 static void
10546 output_comdat_type_unit (comdat_type_node *node)
10548 const char *secname;
10549 char *tmp;
10550 int i;
10551 #if defined (OBJECT_FORMAT_ELF)
10552 tree comdat_key;
10553 #endif
10555 /* First mark all the DIEs in this CU so we know which get local refs. */
10556 mark_dies (node->root_die);
10558 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10560 build_abbrev_table (node->root_die, extern_map);
10562 delete extern_map;
10563 extern_map = NULL;
10565 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10566 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10567 calc_die_sizes (node->root_die);
10569 #if defined (OBJECT_FORMAT_ELF)
10570 if (dwarf_version >= 5)
10572 if (!dwarf_split_debug_info)
10573 secname = ".debug_info";
10574 else
10575 secname = ".debug_info.dwo";
10577 else if (!dwarf_split_debug_info)
10578 secname = ".debug_types";
10579 else
10580 secname = ".debug_types.dwo";
10582 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10583 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10584 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10585 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10586 comdat_key = get_identifier (tmp);
10587 targetm.asm_out.named_section (secname,
10588 SECTION_DEBUG | SECTION_LINKONCE,
10589 comdat_key);
10590 #else
10591 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10592 sprintf (tmp, (dwarf_version >= 5
10593 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10594 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10595 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10596 secname = tmp;
10597 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10598 #endif
10600 /* Output debugging information. */
10601 output_compilation_unit_header (dwarf_split_debug_info
10602 ? DW_UT_split_type : DW_UT_type);
10603 output_signature (node->signature, "Type Signature");
10604 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10605 "Offset to Type DIE");
10606 output_die (node->root_die);
10608 unmark_dies (node->root_die);
10611 /* Return the DWARF2/3 pubname associated with a decl. */
10613 static const char *
10614 dwarf2_name (tree decl, int scope)
10616 if (DECL_NAMELESS (decl))
10617 return NULL;
10618 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10621 /* Add a new entry to .debug_pubnames if appropriate. */
10623 static void
10624 add_pubname_string (const char *str, dw_die_ref die)
10626 pubname_entry e;
10628 e.die = die;
10629 e.name = xstrdup (str);
10630 vec_safe_push (pubname_table, e);
10633 static void
10634 add_pubname (tree decl, dw_die_ref die)
10636 if (!want_pubnames ())
10637 return;
10639 /* Don't add items to the table when we expect that the consumer will have
10640 just read the enclosing die. For example, if the consumer is looking at a
10641 class_member, it will either be inside the class already, or will have just
10642 looked up the class to find the member. Either way, searching the class is
10643 faster than searching the index. */
10644 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10645 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10647 const char *name = dwarf2_name (decl, 1);
10649 if (name)
10650 add_pubname_string (name, die);
10654 /* Add an enumerator to the pubnames section. */
10656 static void
10657 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10659 pubname_entry e;
10661 gcc_assert (scope_name);
10662 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10663 e.die = die;
10664 vec_safe_push (pubname_table, e);
10667 /* Add a new entry to .debug_pubtypes if appropriate. */
10669 static void
10670 add_pubtype (tree decl, dw_die_ref die)
10672 pubname_entry e;
10674 if (!want_pubnames ())
10675 return;
10677 if ((TREE_PUBLIC (decl)
10678 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10679 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10681 tree scope = NULL;
10682 const char *scope_name = "";
10683 const char *sep = is_cxx () ? "::" : ".";
10684 const char *name;
10686 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10687 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10689 scope_name = lang_hooks.dwarf_name (scope, 1);
10690 if (scope_name != NULL && scope_name[0] != '\0')
10691 scope_name = concat (scope_name, sep, NULL);
10692 else
10693 scope_name = "";
10696 if (TYPE_P (decl))
10697 name = type_tag (decl);
10698 else
10699 name = lang_hooks.dwarf_name (decl, 1);
10701 /* If we don't have a name for the type, there's no point in adding
10702 it to the table. */
10703 if (name != NULL && name[0] != '\0')
10705 e.die = die;
10706 e.name = concat (scope_name, name, NULL);
10707 vec_safe_push (pubtype_table, e);
10710 /* Although it might be more consistent to add the pubinfo for the
10711 enumerators as their dies are created, they should only be added if the
10712 enum type meets the criteria above. So rather than re-check the parent
10713 enum type whenever an enumerator die is created, just output them all
10714 here. This isn't protected by the name conditional because anonymous
10715 enums don't have names. */
10716 if (die->die_tag == DW_TAG_enumeration_type)
10718 dw_die_ref c;
10720 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10725 /* Output a single entry in the pubnames table. */
10727 static void
10728 output_pubname (dw_offset die_offset, pubname_entry *entry)
10730 dw_die_ref die = entry->die;
10731 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10733 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10735 if (debug_generate_pub_sections == 2)
10737 /* This logic follows gdb's method for determining the value of the flag
10738 byte. */
10739 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10740 switch (die->die_tag)
10742 case DW_TAG_typedef:
10743 case DW_TAG_base_type:
10744 case DW_TAG_subrange_type:
10745 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10746 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10747 break;
10748 case DW_TAG_enumerator:
10749 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10750 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10751 if (!is_cxx ())
10752 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10753 break;
10754 case DW_TAG_subprogram:
10755 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10756 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10757 if (!is_ada ())
10758 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10759 break;
10760 case DW_TAG_constant:
10761 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10762 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10763 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10764 break;
10765 case DW_TAG_variable:
10766 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10767 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10768 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10769 break;
10770 case DW_TAG_namespace:
10771 case DW_TAG_imported_declaration:
10772 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10773 break;
10774 case DW_TAG_class_type:
10775 case DW_TAG_interface_type:
10776 case DW_TAG_structure_type:
10777 case DW_TAG_union_type:
10778 case DW_TAG_enumeration_type:
10779 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10780 if (!is_cxx ())
10781 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10782 break;
10783 default:
10784 /* An unusual tag. Leave the flag-byte empty. */
10785 break;
10787 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10788 "GDB-index flags");
10791 dw2_asm_output_nstring (entry->name, -1, "external name");
10795 /* Output the public names table used to speed up access to externally
10796 visible names; or the public types table used to find type definitions. */
10798 static void
10799 output_pubnames (vec<pubname_entry, va_gc> *names)
10801 unsigned i;
10802 unsigned long pubnames_length = size_of_pubnames (names);
10803 pubname_entry *pub;
10805 if (!XCOFF_DEBUGGING_INFO)
10807 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10808 dw2_asm_output_data (4, 0xffffffff,
10809 "Initial length escape value indicating 64-bit DWARF extension");
10810 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10811 "Pub Info Length");
10814 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10815 dw2_asm_output_data (2, 2, "DWARF Version");
10817 if (dwarf_split_debug_info)
10818 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10819 debug_skeleton_info_section,
10820 "Offset of Compilation Unit Info");
10821 else
10822 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10823 debug_info_section,
10824 "Offset of Compilation Unit Info");
10825 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10826 "Compilation Unit Length");
10828 FOR_EACH_VEC_ELT (*names, i, pub)
10830 if (include_pubname_in_output (names, pub))
10832 dw_offset die_offset = pub->die->die_offset;
10834 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10835 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10836 gcc_assert (pub->die->die_mark);
10838 /* If we're putting types in their own .debug_types sections,
10839 the .debug_pubtypes table will still point to the compile
10840 unit (not the type unit), so we want to use the offset of
10841 the skeleton DIE (if there is one). */
10842 if (pub->die->comdat_type_p && names == pubtype_table)
10844 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10846 if (type_node != NULL)
10847 die_offset = (type_node->skeleton_die != NULL
10848 ? type_node->skeleton_die->die_offset
10849 : comp_unit_die ()->die_offset);
10852 output_pubname (die_offset, pub);
10856 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10859 /* Output public names and types tables if necessary. */
10861 static void
10862 output_pubtables (void)
10864 if (!want_pubnames () || !info_section_emitted)
10865 return;
10867 switch_to_section (debug_pubnames_section);
10868 output_pubnames (pubname_table);
10869 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10870 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10871 simply won't look for the section. */
10872 switch_to_section (debug_pubtypes_section);
10873 output_pubnames (pubtype_table);
10877 /* Output the information that goes into the .debug_aranges table.
10878 Namely, define the beginning and ending address range of the
10879 text section generated for this compilation unit. */
10881 static void
10882 output_aranges (void)
10884 unsigned i;
10885 unsigned long aranges_length = size_of_aranges ();
10887 if (!XCOFF_DEBUGGING_INFO)
10889 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10890 dw2_asm_output_data (4, 0xffffffff,
10891 "Initial length escape value indicating 64-bit DWARF extension");
10892 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10893 "Length of Address Ranges Info");
10896 /* Version number for aranges is still 2, even up to DWARF5. */
10897 dw2_asm_output_data (2, 2, "DWARF Version");
10898 if (dwarf_split_debug_info)
10899 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10900 debug_skeleton_info_section,
10901 "Offset of Compilation Unit Info");
10902 else
10903 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10904 debug_info_section,
10905 "Offset of Compilation Unit Info");
10906 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10907 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10909 /* We need to align to twice the pointer size here. */
10910 if (DWARF_ARANGES_PAD_SIZE)
10912 /* Pad using a 2 byte words so that padding is correct for any
10913 pointer size. */
10914 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10915 2 * DWARF2_ADDR_SIZE);
10916 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10917 dw2_asm_output_data (2, 0, NULL);
10920 /* It is necessary not to output these entries if the sections were
10921 not used; if the sections were not used, the length will be 0 and
10922 the address may end up as 0 if the section is discarded by ld
10923 --gc-sections, leaving an invalid (0, 0) entry that can be
10924 confused with the terminator. */
10925 if (text_section_used)
10927 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10928 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10929 text_section_label, "Length");
10931 if (cold_text_section_used)
10933 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10934 "Address");
10935 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10936 cold_text_section_label, "Length");
10939 if (have_multiple_function_sections)
10941 unsigned fde_idx;
10942 dw_fde_ref fde;
10944 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10946 if (DECL_IGNORED_P (fde->decl))
10947 continue;
10948 if (!fde->in_std_section)
10950 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10951 "Address");
10952 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10953 fde->dw_fde_begin, "Length");
10955 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10957 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10958 "Address");
10959 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10960 fde->dw_fde_second_begin, "Length");
10965 /* Output the terminator words. */
10966 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10967 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10970 /* Add a new entry to .debug_ranges. Return its index into
10971 ranges_table vector. */
10973 static unsigned int
10974 add_ranges_num (int num, bool maybe_new_sec)
10976 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10977 vec_safe_push (ranges_table, r);
10978 return vec_safe_length (ranges_table) - 1;
10981 /* Add a new entry to .debug_ranges corresponding to a block, or a
10982 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10983 this entry might be in a different section from previous range. */
10985 static unsigned int
10986 add_ranges (const_tree block, bool maybe_new_sec)
10988 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10991 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10992 chain, or middle entry of a chain that will be directly referred to. */
10994 static void
10995 note_rnglist_head (unsigned int offset)
10997 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10998 return;
10999 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11002 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11003 When using dwarf_split_debug_info, address attributes in dies destined
11004 for the final executable should be direct references--setting the
11005 parameter force_direct ensures this behavior. */
11007 static void
11008 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11009 bool *added, bool force_direct)
11011 unsigned int in_use = vec_safe_length (ranges_by_label);
11012 unsigned int offset;
11013 dw_ranges_by_label rbl = { begin, end };
11014 vec_safe_push (ranges_by_label, rbl);
11015 offset = add_ranges_num (-(int)in_use - 1, true);
11016 if (!*added)
11018 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11019 *added = true;
11020 note_rnglist_head (offset);
11024 /* Emit .debug_ranges section. */
11026 static void
11027 output_ranges (void)
11029 unsigned i;
11030 static const char *const start_fmt = "Offset %#x";
11031 const char *fmt = start_fmt;
11032 dw_ranges *r;
11034 switch_to_section (debug_ranges_section);
11035 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11036 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11038 int block_num = r->num;
11040 if (block_num > 0)
11042 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11043 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11045 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11046 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11048 /* If all code is in the text section, then the compilation
11049 unit base address defaults to DW_AT_low_pc, which is the
11050 base of the text section. */
11051 if (!have_multiple_function_sections)
11053 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11054 text_section_label,
11055 fmt, i * 2 * DWARF2_ADDR_SIZE);
11056 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11057 text_section_label, NULL);
11060 /* Otherwise, the compilation unit base address is zero,
11061 which allows us to use absolute addresses, and not worry
11062 about whether the target supports cross-section
11063 arithmetic. */
11064 else
11066 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11067 fmt, i * 2 * DWARF2_ADDR_SIZE);
11068 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11071 fmt = NULL;
11074 /* Negative block_num stands for an index into ranges_by_label. */
11075 else if (block_num < 0)
11077 int lab_idx = - block_num - 1;
11079 if (!have_multiple_function_sections)
11081 gcc_unreachable ();
11082 #if 0
11083 /* If we ever use add_ranges_by_labels () for a single
11084 function section, all we have to do is to take out
11085 the #if 0 above. */
11086 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11087 (*ranges_by_label)[lab_idx].begin,
11088 text_section_label,
11089 fmt, i * 2 * DWARF2_ADDR_SIZE);
11090 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11091 (*ranges_by_label)[lab_idx].end,
11092 text_section_label, NULL);
11093 #endif
11095 else
11097 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11098 (*ranges_by_label)[lab_idx].begin,
11099 fmt, i * 2 * DWARF2_ADDR_SIZE);
11100 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11101 (*ranges_by_label)[lab_idx].end,
11102 NULL);
11105 else
11107 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11108 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11109 fmt = start_fmt;
11114 /* Non-zero if .debug_line_str should be used for .debug_line section
11115 strings or strings that are likely shareable with those. */
11116 #define DWARF5_USE_DEBUG_LINE_STR \
11117 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11118 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11119 /* FIXME: there is no .debug_line_str.dwo section, \
11120 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11121 && !dwarf_split_debug_info)
11123 /* Assign .debug_rnglists indexes. */
11125 static void
11126 index_rnglists (void)
11128 unsigned i;
11129 dw_ranges *r;
11131 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11132 if (r->label)
11133 r->idx = rnglist_idx++;
11136 /* Emit .debug_rnglists section. */
11138 static void
11139 output_rnglists (void)
11141 unsigned i;
11142 dw_ranges *r;
11143 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11144 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11145 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11147 switch_to_section (debug_ranges_section);
11148 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11149 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11150 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11151 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11152 dw2_asm_output_data (4, 0xffffffff,
11153 "Initial length escape value indicating "
11154 "64-bit DWARF extension");
11155 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11156 "Length of Range Lists");
11157 ASM_OUTPUT_LABEL (asm_out_file, l1);
11158 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11159 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11160 dw2_asm_output_data (1, 0, "Segment Size");
11161 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11162 about relocation sizes and primarily care about the size of .debug*
11163 sections in linked shared libraries and executables, then
11164 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11165 into it are usually larger than just DW_FORM_sec_offset offsets
11166 into the .debug_rnglists section. */
11167 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11168 "Offset Entry Count");
11169 if (dwarf_split_debug_info)
11171 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11172 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11173 if (r->label)
11174 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11175 ranges_base_label, NULL);
11178 const char *lab = "";
11179 unsigned int len = vec_safe_length (ranges_table);
11180 const char *base = NULL;
11181 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11183 int block_num = r->num;
11185 if (r->label)
11187 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11188 lab = r->label;
11190 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11191 base = NULL;
11192 if (block_num > 0)
11194 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11195 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11197 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11198 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11200 if (HAVE_AS_LEB128)
11202 /* If all code is in the text section, then the compilation
11203 unit base address defaults to DW_AT_low_pc, which is the
11204 base of the text section. */
11205 if (!have_multiple_function_sections)
11207 dw2_asm_output_data (1, DW_RLE_offset_pair,
11208 "DW_RLE_offset_pair (%s)", lab);
11209 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11210 "Range begin address (%s)", lab);
11211 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11212 "Range end address (%s)", lab);
11213 continue;
11215 if (base == NULL)
11217 dw_ranges *r2 = NULL;
11218 if (i < len - 1)
11219 r2 = &(*ranges_table)[i + 1];
11220 if (r2
11221 && r2->num != 0
11222 && r2->label == NULL
11223 && !r2->maybe_new_sec)
11225 dw2_asm_output_data (1, DW_RLE_base_address,
11226 "DW_RLE_base_address (%s)", lab);
11227 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11228 "Base address (%s)", lab);
11229 strcpy (basebuf, blabel);
11230 base = basebuf;
11233 if (base)
11235 dw2_asm_output_data (1, DW_RLE_offset_pair,
11236 "DW_RLE_offset_pair (%s)", lab);
11237 dw2_asm_output_delta_uleb128 (blabel, base,
11238 "Range begin address (%s)", lab);
11239 dw2_asm_output_delta_uleb128 (elabel, base,
11240 "Range end address (%s)", lab);
11241 continue;
11243 dw2_asm_output_data (1, DW_RLE_start_length,
11244 "DW_RLE_start_length (%s)", lab);
11245 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11246 "Range begin address (%s)", lab);
11247 dw2_asm_output_delta_uleb128 (elabel, blabel,
11248 "Range length (%s)", lab);
11250 else
11252 dw2_asm_output_data (1, DW_RLE_start_end,
11253 "DW_RLE_start_end (%s)", lab);
11254 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11255 "Range begin address (%s)", lab);
11256 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11257 "Range end address (%s)", lab);
11261 /* Negative block_num stands for an index into ranges_by_label. */
11262 else if (block_num < 0)
11264 int lab_idx = - block_num - 1;
11265 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11266 const char *elabel = (*ranges_by_label)[lab_idx].end;
11268 if (!have_multiple_function_sections)
11269 gcc_unreachable ();
11270 if (HAVE_AS_LEB128)
11272 dw2_asm_output_data (1, DW_RLE_start_length,
11273 "DW_RLE_start_length (%s)", lab);
11274 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11275 "Range begin address (%s)", lab);
11276 dw2_asm_output_delta_uleb128 (elabel, blabel,
11277 "Range length (%s)", lab);
11279 else
11281 dw2_asm_output_data (1, DW_RLE_start_end,
11282 "DW_RLE_start_end (%s)", lab);
11283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11284 "Range begin address (%s)", lab);
11285 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11286 "Range end address (%s)", lab);
11289 else
11290 dw2_asm_output_data (1, DW_RLE_end_of_list,
11291 "DW_RLE_end_of_list (%s)", lab);
11293 ASM_OUTPUT_LABEL (asm_out_file, l2);
11296 /* Data structure containing information about input files. */
11297 struct file_info
11299 const char *path; /* Complete file name. */
11300 const char *fname; /* File name part. */
11301 int length; /* Length of entire string. */
11302 struct dwarf_file_data * file_idx; /* Index in input file table. */
11303 int dir_idx; /* Index in directory table. */
11306 /* Data structure containing information about directories with source
11307 files. */
11308 struct dir_info
11310 const char *path; /* Path including directory name. */
11311 int length; /* Path length. */
11312 int prefix; /* Index of directory entry which is a prefix. */
11313 int count; /* Number of files in this directory. */
11314 int dir_idx; /* Index of directory used as base. */
11317 /* Callback function for file_info comparison. We sort by looking at
11318 the directories in the path. */
11320 static int
11321 file_info_cmp (const void *p1, const void *p2)
11323 const struct file_info *const s1 = (const struct file_info *) p1;
11324 const struct file_info *const s2 = (const struct file_info *) p2;
11325 const unsigned char *cp1;
11326 const unsigned char *cp2;
11328 /* Take care of file names without directories. We need to make sure that
11329 we return consistent values to qsort since some will get confused if
11330 we return the same value when identical operands are passed in opposite
11331 orders. So if neither has a directory, return 0 and otherwise return
11332 1 or -1 depending on which one has the directory. */
11333 if ((s1->path == s1->fname || s2->path == s2->fname))
11334 return (s2->path == s2->fname) - (s1->path == s1->fname);
11336 cp1 = (const unsigned char *) s1->path;
11337 cp2 = (const unsigned char *) s2->path;
11339 while (1)
11341 ++cp1;
11342 ++cp2;
11343 /* Reached the end of the first path? If so, handle like above. */
11344 if ((cp1 == (const unsigned char *) s1->fname)
11345 || (cp2 == (const unsigned char *) s2->fname))
11346 return ((cp2 == (const unsigned char *) s2->fname)
11347 - (cp1 == (const unsigned char *) s1->fname));
11349 /* Character of current path component the same? */
11350 else if (*cp1 != *cp2)
11351 return *cp1 - *cp2;
11355 struct file_name_acquire_data
11357 struct file_info *files;
11358 int used_files;
11359 int max_files;
11362 /* Traversal function for the hash table. */
11365 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11367 struct dwarf_file_data *d = *slot;
11368 struct file_info *fi;
11369 const char *f;
11371 gcc_assert (fnad->max_files >= d->emitted_number);
11373 if (! d->emitted_number)
11374 return 1;
11376 gcc_assert (fnad->max_files != fnad->used_files);
11378 fi = fnad->files + fnad->used_files++;
11380 /* Skip all leading "./". */
11381 f = d->filename;
11382 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11383 f += 2;
11385 /* Create a new array entry. */
11386 fi->path = f;
11387 fi->length = strlen (f);
11388 fi->file_idx = d;
11390 /* Search for the file name part. */
11391 f = strrchr (f, DIR_SEPARATOR);
11392 #if defined (DIR_SEPARATOR_2)
11394 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11396 if (g != NULL)
11398 if (f == NULL || f < g)
11399 f = g;
11402 #endif
11404 fi->fname = f == NULL ? fi->path : f + 1;
11405 return 1;
11408 /* Helper function for output_file_names. Emit a FORM encoded
11409 string STR, with assembly comment start ENTRY_KIND and
11410 index IDX */
11412 static void
11413 output_line_string (enum dwarf_form form, const char *str,
11414 const char *entry_kind, unsigned int idx)
11416 switch (form)
11418 case DW_FORM_string:
11419 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11420 break;
11421 case DW_FORM_line_strp:
11422 if (!debug_line_str_hash)
11423 debug_line_str_hash
11424 = hash_table<indirect_string_hasher>::create_ggc (10);
11426 struct indirect_string_node *node;
11427 node = find_AT_string_in_table (str, debug_line_str_hash);
11428 set_indirect_string (node);
11429 node->form = form;
11430 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11431 debug_line_str_section, "%s: %#x: \"%s\"",
11432 entry_kind, 0, node->str);
11433 break;
11434 default:
11435 gcc_unreachable ();
11439 /* Output the directory table and the file name table. We try to minimize
11440 the total amount of memory needed. A heuristic is used to avoid large
11441 slowdowns with many input files. */
11443 static void
11444 output_file_names (void)
11446 struct file_name_acquire_data fnad;
11447 int numfiles;
11448 struct file_info *files;
11449 struct dir_info *dirs;
11450 int *saved;
11451 int *savehere;
11452 int *backmap;
11453 int ndirs;
11454 int idx_offset;
11455 int i;
11457 if (!last_emitted_file)
11459 if (dwarf_version >= 5)
11461 dw2_asm_output_data (1, 0, "Directory entry format count");
11462 dw2_asm_output_data_uleb128 (0, "Directories count");
11463 dw2_asm_output_data (1, 0, "File name entry format count");
11464 dw2_asm_output_data_uleb128 (0, "File names count");
11466 else
11468 dw2_asm_output_data (1, 0, "End directory table");
11469 dw2_asm_output_data (1, 0, "End file name table");
11471 return;
11474 numfiles = last_emitted_file->emitted_number;
11476 /* Allocate the various arrays we need. */
11477 files = XALLOCAVEC (struct file_info, numfiles);
11478 dirs = XALLOCAVEC (struct dir_info, numfiles);
11480 fnad.files = files;
11481 fnad.used_files = 0;
11482 fnad.max_files = numfiles;
11483 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11484 gcc_assert (fnad.used_files == fnad.max_files);
11486 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11488 /* Find all the different directories used. */
11489 dirs[0].path = files[0].path;
11490 dirs[0].length = files[0].fname - files[0].path;
11491 dirs[0].prefix = -1;
11492 dirs[0].count = 1;
11493 dirs[0].dir_idx = 0;
11494 files[0].dir_idx = 0;
11495 ndirs = 1;
11497 for (i = 1; i < numfiles; i++)
11498 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11499 && memcmp (dirs[ndirs - 1].path, files[i].path,
11500 dirs[ndirs - 1].length) == 0)
11502 /* Same directory as last entry. */
11503 files[i].dir_idx = ndirs - 1;
11504 ++dirs[ndirs - 1].count;
11506 else
11508 int j;
11510 /* This is a new directory. */
11511 dirs[ndirs].path = files[i].path;
11512 dirs[ndirs].length = files[i].fname - files[i].path;
11513 dirs[ndirs].count = 1;
11514 dirs[ndirs].dir_idx = ndirs;
11515 files[i].dir_idx = ndirs;
11517 /* Search for a prefix. */
11518 dirs[ndirs].prefix = -1;
11519 for (j = 0; j < ndirs; j++)
11520 if (dirs[j].length < dirs[ndirs].length
11521 && dirs[j].length > 1
11522 && (dirs[ndirs].prefix == -1
11523 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11524 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11525 dirs[ndirs].prefix = j;
11527 ++ndirs;
11530 /* Now to the actual work. We have to find a subset of the directories which
11531 allow expressing the file name using references to the directory table
11532 with the least amount of characters. We do not do an exhaustive search
11533 where we would have to check out every combination of every single
11534 possible prefix. Instead we use a heuristic which provides nearly optimal
11535 results in most cases and never is much off. */
11536 saved = XALLOCAVEC (int, ndirs);
11537 savehere = XALLOCAVEC (int, ndirs);
11539 memset (saved, '\0', ndirs * sizeof (saved[0]));
11540 for (i = 0; i < ndirs; i++)
11542 int j;
11543 int total;
11545 /* We can always save some space for the current directory. But this
11546 does not mean it will be enough to justify adding the directory. */
11547 savehere[i] = dirs[i].length;
11548 total = (savehere[i] - saved[i]) * dirs[i].count;
11550 for (j = i + 1; j < ndirs; j++)
11552 savehere[j] = 0;
11553 if (saved[j] < dirs[i].length)
11555 /* Determine whether the dirs[i] path is a prefix of the
11556 dirs[j] path. */
11557 int k;
11559 k = dirs[j].prefix;
11560 while (k != -1 && k != (int) i)
11561 k = dirs[k].prefix;
11563 if (k == (int) i)
11565 /* Yes it is. We can possibly save some memory by
11566 writing the filenames in dirs[j] relative to
11567 dirs[i]. */
11568 savehere[j] = dirs[i].length;
11569 total += (savehere[j] - saved[j]) * dirs[j].count;
11574 /* Check whether we can save enough to justify adding the dirs[i]
11575 directory. */
11576 if (total > dirs[i].length + 1)
11578 /* It's worthwhile adding. */
11579 for (j = i; j < ndirs; j++)
11580 if (savehere[j] > 0)
11582 /* Remember how much we saved for this directory so far. */
11583 saved[j] = savehere[j];
11585 /* Remember the prefix directory. */
11586 dirs[j].dir_idx = i;
11591 /* Emit the directory name table. */
11592 idx_offset = dirs[0].length > 0 ? 1 : 0;
11593 enum dwarf_form str_form = DW_FORM_string;
11594 enum dwarf_form idx_form = DW_FORM_udata;
11595 if (dwarf_version >= 5)
11597 const char *comp_dir = comp_dir_string ();
11598 if (comp_dir == NULL)
11599 comp_dir = "";
11600 dw2_asm_output_data (1, 1, "Directory entry format count");
11601 if (DWARF5_USE_DEBUG_LINE_STR)
11602 str_form = DW_FORM_line_strp;
11603 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11604 dw2_asm_output_data_uleb128 (str_form, "%s",
11605 get_DW_FORM_name (str_form));
11606 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11607 if (str_form == DW_FORM_string)
11609 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11610 for (i = 1 - idx_offset; i < ndirs; i++)
11611 dw2_asm_output_nstring (dirs[i].path,
11612 dirs[i].length
11613 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11614 "Directory Entry: %#x", i + idx_offset);
11616 else
11618 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11619 for (i = 1 - idx_offset; i < ndirs; i++)
11621 const char *str
11622 = ggc_alloc_string (dirs[i].path,
11623 dirs[i].length
11624 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11625 output_line_string (str_form, str, "Directory Entry",
11626 (unsigned) i + idx_offset);
11630 else
11632 for (i = 1 - idx_offset; i < ndirs; i++)
11633 dw2_asm_output_nstring (dirs[i].path,
11634 dirs[i].length
11635 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11636 "Directory Entry: %#x", i + idx_offset);
11638 dw2_asm_output_data (1, 0, "End directory table");
11641 /* We have to emit them in the order of emitted_number since that's
11642 used in the debug info generation. To do this efficiently we
11643 generate a back-mapping of the indices first. */
11644 backmap = XALLOCAVEC (int, numfiles);
11645 for (i = 0; i < numfiles; i++)
11646 backmap[files[i].file_idx->emitted_number - 1] = i;
11648 if (dwarf_version >= 5)
11650 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11651 if (filename0 == NULL)
11652 filename0 = "";
11653 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11654 DW_FORM_data2. Choose one based on the number of directories
11655 and how much space would they occupy in each encoding.
11656 If we have at most 256 directories, all indexes fit into
11657 a single byte, so DW_FORM_data1 is most compact (if there
11658 are at most 128 directories, DW_FORM_udata would be as
11659 compact as that, but not shorter and slower to decode). */
11660 if (ndirs + idx_offset <= 256)
11661 idx_form = DW_FORM_data1;
11662 /* If there are more than 65536 directories, we have to use
11663 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11664 Otherwise, compute what space would occupy if all the indexes
11665 used DW_FORM_udata - sum - and compare that to how large would
11666 be DW_FORM_data2 encoding, and pick the more efficient one. */
11667 else if (ndirs + idx_offset <= 65536)
11669 unsigned HOST_WIDE_INT sum = 1;
11670 for (i = 0; i < numfiles; i++)
11672 int file_idx = backmap[i];
11673 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11674 sum += size_of_uleb128 (dir_idx);
11676 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11677 idx_form = DW_FORM_data2;
11679 #ifdef VMS_DEBUGGING_INFO
11680 dw2_asm_output_data (1, 4, "File name entry format count");
11681 #else
11682 dw2_asm_output_data (1, 2, "File name entry format count");
11683 #endif
11684 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11685 dw2_asm_output_data_uleb128 (str_form, "%s",
11686 get_DW_FORM_name (str_form));
11687 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11688 "DW_LNCT_directory_index");
11689 dw2_asm_output_data_uleb128 (idx_form, "%s",
11690 get_DW_FORM_name (idx_form));
11691 #ifdef VMS_DEBUGGING_INFO
11692 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11693 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11694 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11695 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11696 #endif
11697 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11699 output_line_string (str_form, filename0, "File Entry", 0);
11701 /* Include directory index. */
11702 if (idx_form != DW_FORM_udata)
11703 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11704 0, NULL);
11705 else
11706 dw2_asm_output_data_uleb128 (0, NULL);
11708 #ifdef VMS_DEBUGGING_INFO
11709 dw2_asm_output_data_uleb128 (0, NULL);
11710 dw2_asm_output_data_uleb128 (0, NULL);
11711 #endif
11714 /* Now write all the file names. */
11715 for (i = 0; i < numfiles; i++)
11717 int file_idx = backmap[i];
11718 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11720 #ifdef VMS_DEBUGGING_INFO
11721 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11723 /* Setting these fields can lead to debugger miscomparisons,
11724 but VMS Debug requires them to be set correctly. */
11726 int ver;
11727 long long cdt;
11728 long siz;
11729 int maxfilelen = (strlen (files[file_idx].path)
11730 + dirs[dir_idx].length
11731 + MAX_VMS_VERSION_LEN + 1);
11732 char *filebuf = XALLOCAVEC (char, maxfilelen);
11734 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11735 snprintf (filebuf, maxfilelen, "%s;%d",
11736 files[file_idx].path + dirs[dir_idx].length, ver);
11738 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11740 /* Include directory index. */
11741 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11742 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11743 dir_idx + idx_offset, NULL);
11744 else
11745 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11747 /* Modification time. */
11748 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11749 &cdt, 0, 0, 0) == 0)
11750 ? cdt : 0, NULL);
11752 /* File length in bytes. */
11753 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11754 0, &siz, 0, 0) == 0)
11755 ? siz : 0, NULL);
11756 #else
11757 output_line_string (str_form,
11758 files[file_idx].path + dirs[dir_idx].length,
11759 "File Entry", (unsigned) i + 1);
11761 /* Include directory index. */
11762 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11763 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11764 dir_idx + idx_offset, NULL);
11765 else
11766 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11768 if (dwarf_version >= 5)
11769 continue;
11771 /* Modification time. */
11772 dw2_asm_output_data_uleb128 (0, NULL);
11774 /* File length in bytes. */
11775 dw2_asm_output_data_uleb128 (0, NULL);
11776 #endif /* VMS_DEBUGGING_INFO */
11779 if (dwarf_version < 5)
11780 dw2_asm_output_data (1, 0, "End file name table");
11784 /* Output one line number table into the .debug_line section. */
11786 static void
11787 output_one_line_info_table (dw_line_info_table *table)
11789 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11790 unsigned int current_line = 1;
11791 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11792 dw_line_info_entry *ent;
11793 size_t i;
11795 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11797 switch (ent->opcode)
11799 case LI_set_address:
11800 /* ??? Unfortunately, we have little choice here currently, and
11801 must always use the most general form. GCC does not know the
11802 address delta itself, so we can't use DW_LNS_advance_pc. Many
11803 ports do have length attributes which will give an upper bound
11804 on the address range. We could perhaps use length attributes
11805 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11806 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11808 /* This can handle any delta. This takes
11809 4+DWARF2_ADDR_SIZE bytes. */
11810 dw2_asm_output_data (1, 0, "set address %s", line_label);
11811 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11812 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11813 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11814 break;
11816 case LI_set_line:
11817 if (ent->val == current_line)
11819 /* We still need to start a new row, so output a copy insn. */
11820 dw2_asm_output_data (1, DW_LNS_copy,
11821 "copy line %u", current_line);
11823 else
11825 int line_offset = ent->val - current_line;
11826 int line_delta = line_offset - DWARF_LINE_BASE;
11828 current_line = ent->val;
11829 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11831 /* This can handle deltas from -10 to 234, using the current
11832 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11833 This takes 1 byte. */
11834 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11835 "line %u", current_line);
11837 else
11839 /* This can handle any delta. This takes at least 4 bytes,
11840 depending on the value being encoded. */
11841 dw2_asm_output_data (1, DW_LNS_advance_line,
11842 "advance to line %u", current_line);
11843 dw2_asm_output_data_sleb128 (line_offset, NULL);
11844 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11847 break;
11849 case LI_set_file:
11850 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11851 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11852 break;
11854 case LI_set_column:
11855 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11856 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11857 break;
11859 case LI_negate_stmt:
11860 current_is_stmt = !current_is_stmt;
11861 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11862 "is_stmt %d", current_is_stmt);
11863 break;
11865 case LI_set_prologue_end:
11866 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11867 "set prologue end");
11868 break;
11870 case LI_set_epilogue_begin:
11871 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11872 "set epilogue begin");
11873 break;
11875 case LI_set_discriminator:
11876 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11877 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11878 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11879 dw2_asm_output_data_uleb128 (ent->val, NULL);
11880 break;
11884 /* Emit debug info for the address of the end of the table. */
11885 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11886 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11887 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11888 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11890 dw2_asm_output_data (1, 0, "end sequence");
11891 dw2_asm_output_data_uleb128 (1, NULL);
11892 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11895 /* Output the source line number correspondence information. This
11896 information goes into the .debug_line section. */
11898 static void
11899 output_line_info (bool prologue_only)
11901 static unsigned int generation;
11902 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11903 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11904 bool saw_one = false;
11905 int opc;
11907 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11908 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11909 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11910 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11912 if (!XCOFF_DEBUGGING_INFO)
11914 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11915 dw2_asm_output_data (4, 0xffffffff,
11916 "Initial length escape value indicating 64-bit DWARF extension");
11917 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11918 "Length of Source Line Info");
11921 ASM_OUTPUT_LABEL (asm_out_file, l1);
11923 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11924 if (dwarf_version >= 5)
11926 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11927 dw2_asm_output_data (1, 0, "Segment Size");
11929 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11930 ASM_OUTPUT_LABEL (asm_out_file, p1);
11932 /* Define the architecture-dependent minimum instruction length (in bytes).
11933 In this implementation of DWARF, this field is used for information
11934 purposes only. Since GCC generates assembly language, we have no
11935 a priori knowledge of how many instruction bytes are generated for each
11936 source line, and therefore can use only the DW_LNE_set_address and
11937 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11938 this as '1', which is "correct enough" for all architectures,
11939 and don't let the target override. */
11940 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11942 if (dwarf_version >= 4)
11943 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11944 "Maximum Operations Per Instruction");
11945 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11946 "Default is_stmt_start flag");
11947 dw2_asm_output_data (1, DWARF_LINE_BASE,
11948 "Line Base Value (Special Opcodes)");
11949 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11950 "Line Range Value (Special Opcodes)");
11951 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11952 "Special Opcode Base");
11954 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11956 int n_op_args;
11957 switch (opc)
11959 case DW_LNS_advance_pc:
11960 case DW_LNS_advance_line:
11961 case DW_LNS_set_file:
11962 case DW_LNS_set_column:
11963 case DW_LNS_fixed_advance_pc:
11964 case DW_LNS_set_isa:
11965 n_op_args = 1;
11966 break;
11967 default:
11968 n_op_args = 0;
11969 break;
11972 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11973 opc, n_op_args);
11976 /* Write out the information about the files we use. */
11977 output_file_names ();
11978 ASM_OUTPUT_LABEL (asm_out_file, p2);
11979 if (prologue_only)
11981 /* Output the marker for the end of the line number info. */
11982 ASM_OUTPUT_LABEL (asm_out_file, l2);
11983 return;
11986 if (separate_line_info)
11988 dw_line_info_table *table;
11989 size_t i;
11991 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11992 if (table->in_use)
11994 output_one_line_info_table (table);
11995 saw_one = true;
11998 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12000 output_one_line_info_table (cold_text_section_line_info);
12001 saw_one = true;
12004 /* ??? Some Darwin linkers crash on a .debug_line section with no
12005 sequences. Further, merely a DW_LNE_end_sequence entry is not
12006 sufficient -- the address column must also be initialized.
12007 Make sure to output at least one set_address/end_sequence pair,
12008 choosing .text since that section is always present. */
12009 if (text_section_line_info->in_use || !saw_one)
12010 output_one_line_info_table (text_section_line_info);
12012 /* Output the marker for the end of the line number info. */
12013 ASM_OUTPUT_LABEL (asm_out_file, l2);
12016 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12018 static inline bool
12019 need_endianity_attribute_p (bool reverse)
12021 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12024 /* Given a pointer to a tree node for some base type, return a pointer to
12025 a DIE that describes the given type. REVERSE is true if the type is
12026 to be interpreted in the reverse storage order wrt the target order.
12028 This routine must only be called for GCC type nodes that correspond to
12029 Dwarf base (fundamental) types. */
12031 static dw_die_ref
12032 base_type_die (tree type, bool reverse)
12034 dw_die_ref base_type_result;
12035 enum dwarf_type encoding;
12036 bool fpt_used = false;
12037 struct fixed_point_type_info fpt_info;
12038 tree type_bias = NULL_TREE;
12040 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12041 return 0;
12043 /* If this is a subtype that should not be emitted as a subrange type,
12044 use the base type. See subrange_type_for_debug_p. */
12045 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12046 type = TREE_TYPE (type);
12048 switch (TREE_CODE (type))
12050 case INTEGER_TYPE:
12051 if ((dwarf_version >= 4 || !dwarf_strict)
12052 && TYPE_NAME (type)
12053 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12054 && DECL_IS_BUILTIN (TYPE_NAME (type))
12055 && DECL_NAME (TYPE_NAME (type)))
12057 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12058 if (strcmp (name, "char16_t") == 0
12059 || strcmp (name, "char32_t") == 0)
12061 encoding = DW_ATE_UTF;
12062 break;
12065 if ((dwarf_version >= 3 || !dwarf_strict)
12066 && lang_hooks.types.get_fixed_point_type_info)
12068 memset (&fpt_info, 0, sizeof (fpt_info));
12069 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12071 fpt_used = true;
12072 encoding = ((TYPE_UNSIGNED (type))
12073 ? DW_ATE_unsigned_fixed
12074 : DW_ATE_signed_fixed);
12075 break;
12078 if (TYPE_STRING_FLAG (type))
12080 if (TYPE_UNSIGNED (type))
12081 encoding = DW_ATE_unsigned_char;
12082 else
12083 encoding = DW_ATE_signed_char;
12085 else if (TYPE_UNSIGNED (type))
12086 encoding = DW_ATE_unsigned;
12087 else
12088 encoding = DW_ATE_signed;
12090 if (!dwarf_strict
12091 && lang_hooks.types.get_type_bias)
12092 type_bias = lang_hooks.types.get_type_bias (type);
12093 break;
12095 case REAL_TYPE:
12096 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12098 if (dwarf_version >= 3 || !dwarf_strict)
12099 encoding = DW_ATE_decimal_float;
12100 else
12101 encoding = DW_ATE_lo_user;
12103 else
12104 encoding = DW_ATE_float;
12105 break;
12107 case FIXED_POINT_TYPE:
12108 if (!(dwarf_version >= 3 || !dwarf_strict))
12109 encoding = DW_ATE_lo_user;
12110 else if (TYPE_UNSIGNED (type))
12111 encoding = DW_ATE_unsigned_fixed;
12112 else
12113 encoding = DW_ATE_signed_fixed;
12114 break;
12116 /* Dwarf2 doesn't know anything about complex ints, so use
12117 a user defined type for it. */
12118 case COMPLEX_TYPE:
12119 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12120 encoding = DW_ATE_complex_float;
12121 else
12122 encoding = DW_ATE_lo_user;
12123 break;
12125 case BOOLEAN_TYPE:
12126 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12127 encoding = DW_ATE_boolean;
12128 break;
12130 default:
12131 /* No other TREE_CODEs are Dwarf fundamental types. */
12132 gcc_unreachable ();
12135 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12137 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12138 int_size_in_bytes (type));
12139 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12141 if (need_endianity_attribute_p (reverse))
12142 add_AT_unsigned (base_type_result, DW_AT_endianity,
12143 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12145 add_alignment_attribute (base_type_result, type);
12147 if (fpt_used)
12149 switch (fpt_info.scale_factor_kind)
12151 case fixed_point_scale_factor_binary:
12152 add_AT_int (base_type_result, DW_AT_binary_scale,
12153 fpt_info.scale_factor.binary);
12154 break;
12156 case fixed_point_scale_factor_decimal:
12157 add_AT_int (base_type_result, DW_AT_decimal_scale,
12158 fpt_info.scale_factor.decimal);
12159 break;
12161 case fixed_point_scale_factor_arbitrary:
12162 /* Arbitrary scale factors cannot be described in standard DWARF,
12163 yet. */
12164 if (!dwarf_strict)
12166 /* Describe the scale factor as a rational constant. */
12167 const dw_die_ref scale_factor
12168 = new_die (DW_TAG_constant, comp_unit_die (), type);
12170 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12171 fpt_info.scale_factor.arbitrary.numerator);
12172 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12173 fpt_info.scale_factor.arbitrary.denominator);
12175 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12177 break;
12179 default:
12180 gcc_unreachable ();
12184 if (type_bias)
12185 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12186 dw_scalar_form_constant
12187 | dw_scalar_form_exprloc
12188 | dw_scalar_form_reference,
12189 NULL);
12191 add_pubtype (type, base_type_result);
12193 return base_type_result;
12196 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12197 named 'auto' in its type: return true for it, false otherwise. */
12199 static inline bool
12200 is_cxx_auto (tree type)
12202 if (is_cxx ())
12204 tree name = TYPE_IDENTIFIER (type);
12205 if (name == get_identifier ("auto")
12206 || name == get_identifier ("decltype(auto)"))
12207 return true;
12209 return false;
12212 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12213 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12215 static inline int
12216 is_base_type (tree type)
12218 switch (TREE_CODE (type))
12220 case ERROR_MARK:
12221 case VOID_TYPE:
12222 case INTEGER_TYPE:
12223 case REAL_TYPE:
12224 case FIXED_POINT_TYPE:
12225 case COMPLEX_TYPE:
12226 case BOOLEAN_TYPE:
12227 case POINTER_BOUNDS_TYPE:
12228 return 1;
12230 case ARRAY_TYPE:
12231 case RECORD_TYPE:
12232 case UNION_TYPE:
12233 case QUAL_UNION_TYPE:
12234 case ENUMERAL_TYPE:
12235 case FUNCTION_TYPE:
12236 case METHOD_TYPE:
12237 case POINTER_TYPE:
12238 case REFERENCE_TYPE:
12239 case NULLPTR_TYPE:
12240 case OFFSET_TYPE:
12241 case LANG_TYPE:
12242 case VECTOR_TYPE:
12243 return 0;
12245 default:
12246 if (is_cxx_auto (type))
12247 return 0;
12248 gcc_unreachable ();
12251 return 0;
12254 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12255 node, return the size in bits for the type if it is a constant, or else
12256 return the alignment for the type if the type's size is not constant, or
12257 else return BITS_PER_WORD if the type actually turns out to be an
12258 ERROR_MARK node. */
12260 static inline unsigned HOST_WIDE_INT
12261 simple_type_size_in_bits (const_tree type)
12263 if (TREE_CODE (type) == ERROR_MARK)
12264 return BITS_PER_WORD;
12265 else if (TYPE_SIZE (type) == NULL_TREE)
12266 return 0;
12267 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12268 return tree_to_uhwi (TYPE_SIZE (type));
12269 else
12270 return TYPE_ALIGN (type);
12273 /* Similarly, but return an offset_int instead of UHWI. */
12275 static inline offset_int
12276 offset_int_type_size_in_bits (const_tree type)
12278 if (TREE_CODE (type) == ERROR_MARK)
12279 return BITS_PER_WORD;
12280 else if (TYPE_SIZE (type) == NULL_TREE)
12281 return 0;
12282 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12283 return wi::to_offset (TYPE_SIZE (type));
12284 else
12285 return TYPE_ALIGN (type);
12288 /* Given a pointer to a tree node for a subrange type, return a pointer
12289 to a DIE that describes the given type. */
12291 static dw_die_ref
12292 subrange_type_die (tree type, tree low, tree high, tree bias,
12293 dw_die_ref context_die)
12295 dw_die_ref subrange_die;
12296 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12298 if (context_die == NULL)
12299 context_die = comp_unit_die ();
12301 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12303 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12305 /* The size of the subrange type and its base type do not match,
12306 so we need to generate a size attribute for the subrange type. */
12307 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12310 add_alignment_attribute (subrange_die, type);
12312 if (low)
12313 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12314 if (high)
12315 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12316 if (bias && !dwarf_strict)
12317 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12318 dw_scalar_form_constant
12319 | dw_scalar_form_exprloc
12320 | dw_scalar_form_reference,
12321 NULL);
12323 return subrange_die;
12326 /* Returns the (const and/or volatile) cv_qualifiers associated with
12327 the decl node. This will normally be augmented with the
12328 cv_qualifiers of the underlying type in add_type_attribute. */
12330 static int
12331 decl_quals (const_tree decl)
12333 return ((TREE_READONLY (decl)
12334 /* The C++ front-end correctly marks reference-typed
12335 variables as readonly, but from a language (and debug
12336 info) standpoint they are not const-qualified. */
12337 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12338 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12339 | (TREE_THIS_VOLATILE (decl)
12340 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12343 /* Determine the TYPE whose qualifiers match the largest strict subset
12344 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12345 qualifiers outside QUAL_MASK. */
12347 static int
12348 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12350 tree t;
12351 int best_rank = 0, best_qual = 0, max_rank;
12353 type_quals &= qual_mask;
12354 max_rank = popcount_hwi (type_quals) - 1;
12356 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12357 t = TYPE_NEXT_VARIANT (t))
12359 int q = TYPE_QUALS (t) & qual_mask;
12361 if ((q & type_quals) == q && q != type_quals
12362 && check_base_type (t, type))
12364 int rank = popcount_hwi (q);
12366 if (rank > best_rank)
12368 best_rank = rank;
12369 best_qual = q;
12374 return best_qual;
12377 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12378 static const dwarf_qual_info_t dwarf_qual_info[] =
12380 { TYPE_QUAL_CONST, DW_TAG_const_type },
12381 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12382 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12383 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12385 static const unsigned int dwarf_qual_info_size
12386 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12388 /* If DIE is a qualified DIE of some base DIE with the same parent,
12389 return the base DIE, otherwise return NULL. Set MASK to the
12390 qualifiers added compared to the returned DIE. */
12392 static dw_die_ref
12393 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12395 unsigned int i;
12396 for (i = 0; i < dwarf_qual_info_size; i++)
12397 if (die->die_tag == dwarf_qual_info[i].t)
12398 break;
12399 if (i == dwarf_qual_info_size)
12400 return NULL;
12401 if (vec_safe_length (die->die_attr) != 1)
12402 return NULL;
12403 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12404 if (type == NULL || type->die_parent != die->die_parent)
12405 return NULL;
12406 *mask |= dwarf_qual_info[i].q;
12407 if (depth)
12409 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12410 if (ret)
12411 return ret;
12413 return type;
12416 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12417 entry that chains the modifiers specified by CV_QUALS in front of the
12418 given type. REVERSE is true if the type is to be interpreted in the
12419 reverse storage order wrt the target order. */
12421 static dw_die_ref
12422 modified_type_die (tree type, int cv_quals, bool reverse,
12423 dw_die_ref context_die)
12425 enum tree_code code = TREE_CODE (type);
12426 dw_die_ref mod_type_die;
12427 dw_die_ref sub_die = NULL;
12428 tree item_type = NULL;
12429 tree qualified_type;
12430 tree name, low, high;
12431 dw_die_ref mod_scope;
12432 /* Only these cv-qualifiers are currently handled. */
12433 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12434 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12436 if (code == ERROR_MARK)
12437 return NULL;
12439 if (lang_hooks.types.get_debug_type)
12441 tree debug_type = lang_hooks.types.get_debug_type (type);
12443 if (debug_type != NULL_TREE && debug_type != type)
12444 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12447 cv_quals &= cv_qual_mask;
12449 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12450 tag modifier (and not an attribute) old consumers won't be able
12451 to handle it. */
12452 if (dwarf_version < 3)
12453 cv_quals &= ~TYPE_QUAL_RESTRICT;
12455 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12456 if (dwarf_version < 5)
12457 cv_quals &= ~TYPE_QUAL_ATOMIC;
12459 /* See if we already have the appropriately qualified variant of
12460 this type. */
12461 qualified_type = get_qualified_type (type, cv_quals);
12463 if (qualified_type == sizetype)
12465 /* Try not to expose the internal sizetype type's name. */
12466 if (TYPE_NAME (qualified_type)
12467 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12469 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12471 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12472 && (TYPE_PRECISION (t)
12473 == TYPE_PRECISION (qualified_type))
12474 && (TYPE_UNSIGNED (t)
12475 == TYPE_UNSIGNED (qualified_type)));
12476 qualified_type = t;
12478 else if (qualified_type == sizetype
12479 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12480 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12481 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12482 qualified_type = size_type_node;
12486 /* If we do, then we can just use its DIE, if it exists. */
12487 if (qualified_type)
12489 mod_type_die = lookup_type_die (qualified_type);
12491 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12492 if (mod_type_die
12493 && (!need_endianity_attribute_p (reverse)
12494 || !is_base_type (type)
12495 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12496 return mod_type_die;
12499 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12501 /* Handle C typedef types. */
12502 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12503 && !DECL_ARTIFICIAL (name))
12505 tree dtype = TREE_TYPE (name);
12507 if (qualified_type == dtype)
12509 /* For a named type, use the typedef. */
12510 gen_type_die (qualified_type, context_die);
12511 return lookup_type_die (qualified_type);
12513 else
12515 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12516 dquals &= cv_qual_mask;
12517 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12518 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12519 /* cv-unqualified version of named type. Just use
12520 the unnamed type to which it refers. */
12521 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12522 reverse, context_die);
12523 /* Else cv-qualified version of named type; fall through. */
12527 mod_scope = scope_die_for (type, context_die);
12529 if (cv_quals)
12531 int sub_quals = 0, first_quals = 0;
12532 unsigned i;
12533 dw_die_ref first = NULL, last = NULL;
12535 /* Determine a lesser qualified type that most closely matches
12536 this one. Then generate DW_TAG_* entries for the remaining
12537 qualifiers. */
12538 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12539 cv_qual_mask);
12540 if (sub_quals && use_debug_types)
12542 bool needed = false;
12543 /* If emitting type units, make sure the order of qualifiers
12544 is canonical. Thus, start from unqualified type if
12545 an earlier qualifier is missing in sub_quals, but some later
12546 one is present there. */
12547 for (i = 0; i < dwarf_qual_info_size; i++)
12548 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12549 needed = true;
12550 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12552 sub_quals = 0;
12553 break;
12556 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12557 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12559 /* As not all intermediate qualified DIEs have corresponding
12560 tree types, ensure that qualified DIEs in the same scope
12561 as their DW_AT_type are emitted after their DW_AT_type,
12562 only with other qualified DIEs for the same type possibly
12563 in between them. Determine the range of such qualified
12564 DIEs now (first being the base type, last being corresponding
12565 last qualified DIE for it). */
12566 unsigned int count = 0;
12567 first = qualified_die_p (mod_type_die, &first_quals,
12568 dwarf_qual_info_size);
12569 if (first == NULL)
12570 first = mod_type_die;
12571 gcc_assert ((first_quals & ~sub_quals) == 0);
12572 for (count = 0, last = first;
12573 count < (1U << dwarf_qual_info_size);
12574 count++, last = last->die_sib)
12576 int quals = 0;
12577 if (last == mod_scope->die_child)
12578 break;
12579 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12580 != first)
12581 break;
12585 for (i = 0; i < dwarf_qual_info_size; i++)
12586 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12588 dw_die_ref d;
12589 if (first && first != last)
12591 for (d = first->die_sib; ; d = d->die_sib)
12593 int quals = 0;
12594 qualified_die_p (d, &quals, dwarf_qual_info_size);
12595 if (quals == (first_quals | dwarf_qual_info[i].q))
12596 break;
12597 if (d == last)
12599 d = NULL;
12600 break;
12603 if (d)
12605 mod_type_die = d;
12606 continue;
12609 if (first)
12611 d = ggc_cleared_alloc<die_node> ();
12612 d->die_tag = dwarf_qual_info[i].t;
12613 add_child_die_after (mod_scope, d, last);
12614 last = d;
12616 else
12617 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12618 if (mod_type_die)
12619 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12620 mod_type_die = d;
12621 first_quals |= dwarf_qual_info[i].q;
12624 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12626 dwarf_tag tag = DW_TAG_pointer_type;
12627 if (code == REFERENCE_TYPE)
12629 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12630 tag = DW_TAG_rvalue_reference_type;
12631 else
12632 tag = DW_TAG_reference_type;
12634 mod_type_die = new_die (tag, mod_scope, type);
12636 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12637 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12638 add_alignment_attribute (mod_type_die, type);
12639 item_type = TREE_TYPE (type);
12641 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12642 if (!ADDR_SPACE_GENERIC_P (as))
12644 int action = targetm.addr_space.debug (as);
12645 if (action >= 0)
12647 /* Positive values indicate an address_class. */
12648 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12650 else
12652 /* Negative values indicate an (inverted) segment base reg. */
12653 dw_loc_descr_ref d
12654 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12655 add_AT_loc (mod_type_die, DW_AT_segment, d);
12659 else if (code == INTEGER_TYPE
12660 && TREE_TYPE (type) != NULL_TREE
12661 && subrange_type_for_debug_p (type, &low, &high))
12663 tree bias = NULL_TREE;
12664 if (lang_hooks.types.get_type_bias)
12665 bias = lang_hooks.types.get_type_bias (type);
12666 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12667 item_type = TREE_TYPE (type);
12669 else if (is_base_type (type))
12670 mod_type_die = base_type_die (type, reverse);
12671 else
12673 gen_type_die (type, context_die);
12675 /* We have to get the type_main_variant here (and pass that to the
12676 `lookup_type_die' routine) because the ..._TYPE node we have
12677 might simply be a *copy* of some original type node (where the
12678 copy was created to help us keep track of typedef names) and
12679 that copy might have a different TYPE_UID from the original
12680 ..._TYPE node. */
12681 if (TREE_CODE (type) == FUNCTION_TYPE
12682 || TREE_CODE (type) == METHOD_TYPE)
12684 /* For function/method types, can't just use type_main_variant here,
12685 because that can have different ref-qualifiers for C++,
12686 but try to canonicalize. */
12687 tree main = TYPE_MAIN_VARIANT (type);
12688 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12689 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12690 && check_base_type (t, main)
12691 && check_lang_type (t, type))
12692 return lookup_type_die (t);
12693 return lookup_type_die (type);
12695 else if (TREE_CODE (type) != VECTOR_TYPE
12696 && TREE_CODE (type) != ARRAY_TYPE)
12697 return lookup_type_die (type_main_variant (type));
12698 else
12699 /* Vectors have the debugging information in the type,
12700 not the main variant. */
12701 return lookup_type_die (type);
12704 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12705 don't output a DW_TAG_typedef, since there isn't one in the
12706 user's program; just attach a DW_AT_name to the type.
12707 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12708 if the base type already has the same name. */
12709 if (name
12710 && ((TREE_CODE (name) != TYPE_DECL
12711 && (qualified_type == TYPE_MAIN_VARIANT (type)
12712 || (cv_quals == TYPE_UNQUALIFIED)))
12713 || (TREE_CODE (name) == TYPE_DECL
12714 && TREE_TYPE (name) == qualified_type
12715 && DECL_NAME (name))))
12717 if (TREE_CODE (name) == TYPE_DECL)
12718 /* Could just call add_name_and_src_coords_attributes here,
12719 but since this is a builtin type it doesn't have any
12720 useful source coordinates anyway. */
12721 name = DECL_NAME (name);
12722 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12724 /* This probably indicates a bug. */
12725 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12727 name = TYPE_IDENTIFIER (type);
12728 add_name_attribute (mod_type_die,
12729 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12732 if (qualified_type)
12733 equate_type_number_to_die (qualified_type, mod_type_die);
12735 if (item_type)
12736 /* We must do this after the equate_type_number_to_die call, in case
12737 this is a recursive type. This ensures that the modified_type_die
12738 recursion will terminate even if the type is recursive. Recursive
12739 types are possible in Ada. */
12740 sub_die = modified_type_die (item_type,
12741 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12742 reverse,
12743 context_die);
12745 if (sub_die != NULL)
12746 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12748 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12749 if (TYPE_ARTIFICIAL (type))
12750 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12752 return mod_type_die;
12755 /* Generate DIEs for the generic parameters of T.
12756 T must be either a generic type or a generic function.
12757 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12759 static void
12760 gen_generic_params_dies (tree t)
12762 tree parms, args;
12763 int parms_num, i;
12764 dw_die_ref die = NULL;
12765 int non_default;
12767 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12768 return;
12770 if (TYPE_P (t))
12771 die = lookup_type_die (t);
12772 else if (DECL_P (t))
12773 die = lookup_decl_die (t);
12775 gcc_assert (die);
12777 parms = lang_hooks.get_innermost_generic_parms (t);
12778 if (!parms)
12779 /* T has no generic parameter. It means T is neither a generic type
12780 or function. End of story. */
12781 return;
12783 parms_num = TREE_VEC_LENGTH (parms);
12784 args = lang_hooks.get_innermost_generic_args (t);
12785 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12786 non_default = int_cst_value (TREE_CHAIN (args));
12787 else
12788 non_default = TREE_VEC_LENGTH (args);
12789 for (i = 0; i < parms_num; i++)
12791 tree parm, arg, arg_pack_elems;
12792 dw_die_ref parm_die;
12794 parm = TREE_VEC_ELT (parms, i);
12795 arg = TREE_VEC_ELT (args, i);
12796 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12797 gcc_assert (parm && TREE_VALUE (parm) && arg);
12799 if (parm && TREE_VALUE (parm) && arg)
12801 /* If PARM represents a template parameter pack,
12802 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12803 by DW_TAG_template_*_parameter DIEs for the argument
12804 pack elements of ARG. Note that ARG would then be
12805 an argument pack. */
12806 if (arg_pack_elems)
12807 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12808 arg_pack_elems,
12809 die);
12810 else
12811 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12812 true /* emit name */, die);
12813 if (i >= non_default)
12814 add_AT_flag (parm_die, DW_AT_default_value, 1);
12819 /* Create and return a DIE for PARM which should be
12820 the representation of a generic type parameter.
12821 For instance, in the C++ front end, PARM would be a template parameter.
12822 ARG is the argument to PARM.
12823 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12824 name of the PARM.
12825 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12826 as a child node. */
12828 static dw_die_ref
12829 generic_parameter_die (tree parm, tree arg,
12830 bool emit_name_p,
12831 dw_die_ref parent_die)
12833 dw_die_ref tmpl_die = NULL;
12834 const char *name = NULL;
12836 if (!parm || !DECL_NAME (parm) || !arg)
12837 return NULL;
12839 /* We support non-type generic parameters and arguments,
12840 type generic parameters and arguments, as well as
12841 generic generic parameters (a.k.a. template template parameters in C++)
12842 and arguments. */
12843 if (TREE_CODE (parm) == PARM_DECL)
12844 /* PARM is a nontype generic parameter */
12845 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12846 else if (TREE_CODE (parm) == TYPE_DECL)
12847 /* PARM is a type generic parameter. */
12848 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12849 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12850 /* PARM is a generic generic parameter.
12851 Its DIE is a GNU extension. It shall have a
12852 DW_AT_name attribute to represent the name of the template template
12853 parameter, and a DW_AT_GNU_template_name attribute to represent the
12854 name of the template template argument. */
12855 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12856 parent_die, parm);
12857 else
12858 gcc_unreachable ();
12860 if (tmpl_die)
12862 tree tmpl_type;
12864 /* If PARM is a generic parameter pack, it means we are
12865 emitting debug info for a template argument pack element.
12866 In other terms, ARG is a template argument pack element.
12867 In that case, we don't emit any DW_AT_name attribute for
12868 the die. */
12869 if (emit_name_p)
12871 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12872 gcc_assert (name);
12873 add_AT_string (tmpl_die, DW_AT_name, name);
12876 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12878 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12879 TMPL_DIE should have a child DW_AT_type attribute that is set
12880 to the type of the argument to PARM, which is ARG.
12881 If PARM is a type generic parameter, TMPL_DIE should have a
12882 child DW_AT_type that is set to ARG. */
12883 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12884 add_type_attribute (tmpl_die, tmpl_type,
12885 (TREE_THIS_VOLATILE (tmpl_type)
12886 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12887 false, parent_die);
12889 else
12891 /* So TMPL_DIE is a DIE representing a
12892 a generic generic template parameter, a.k.a template template
12893 parameter in C++ and arg is a template. */
12895 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12896 to the name of the argument. */
12897 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12898 if (name)
12899 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12902 if (TREE_CODE (parm) == PARM_DECL)
12903 /* So PARM is a non-type generic parameter.
12904 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12905 attribute of TMPL_DIE which value represents the value
12906 of ARG.
12907 We must be careful here:
12908 The value of ARG might reference some function decls.
12909 We might currently be emitting debug info for a generic
12910 type and types are emitted before function decls, we don't
12911 know if the function decls referenced by ARG will actually be
12912 emitted after cgraph computations.
12913 So must defer the generation of the DW_AT_const_value to
12914 after cgraph is ready. */
12915 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12918 return tmpl_die;
12921 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12922 PARM_PACK must be a template parameter pack. The returned DIE
12923 will be child DIE of PARENT_DIE. */
12925 static dw_die_ref
12926 template_parameter_pack_die (tree parm_pack,
12927 tree parm_pack_args,
12928 dw_die_ref parent_die)
12930 dw_die_ref die;
12931 int j;
12933 gcc_assert (parent_die && parm_pack);
12935 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12936 add_name_and_src_coords_attributes (die, parm_pack);
12937 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12938 generic_parameter_die (parm_pack,
12939 TREE_VEC_ELT (parm_pack_args, j),
12940 false /* Don't emit DW_AT_name */,
12941 die);
12942 return die;
12945 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12946 an enumerated type. */
12948 static inline int
12949 type_is_enum (const_tree type)
12951 return TREE_CODE (type) == ENUMERAL_TYPE;
12954 /* Return the DBX register number described by a given RTL node. */
12956 static unsigned int
12957 dbx_reg_number (const_rtx rtl)
12959 unsigned regno = REGNO (rtl);
12961 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12963 #ifdef LEAF_REG_REMAP
12964 if (crtl->uses_only_leaf_regs)
12966 int leaf_reg = LEAF_REG_REMAP (regno);
12967 if (leaf_reg != -1)
12968 regno = (unsigned) leaf_reg;
12970 #endif
12972 regno = DBX_REGISTER_NUMBER (regno);
12973 gcc_assert (regno != INVALID_REGNUM);
12974 return regno;
12977 /* Optionally add a DW_OP_piece term to a location description expression.
12978 DW_OP_piece is only added if the location description expression already
12979 doesn't end with DW_OP_piece. */
12981 static void
12982 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12984 dw_loc_descr_ref loc;
12986 if (*list_head != NULL)
12988 /* Find the end of the chain. */
12989 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12992 if (loc->dw_loc_opc != DW_OP_piece)
12993 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12997 /* Return a location descriptor that designates a machine register or
12998 zero if there is none. */
13000 static dw_loc_descr_ref
13001 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13003 rtx regs;
13005 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13006 return 0;
13008 /* We only use "frame base" when we're sure we're talking about the
13009 post-prologue local stack frame. We do this by *not* running
13010 register elimination until this point, and recognizing the special
13011 argument pointer and soft frame pointer rtx's.
13012 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13013 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13014 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13016 dw_loc_descr_ref result = NULL;
13018 if (dwarf_version >= 4 || !dwarf_strict)
13020 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13021 initialized);
13022 if (result)
13023 add_loc_descr (&result,
13024 new_loc_descr (DW_OP_stack_value, 0, 0));
13026 return result;
13029 regs = targetm.dwarf_register_span (rtl);
13031 if (REG_NREGS (rtl) > 1 || regs)
13032 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13033 else
13035 unsigned int dbx_regnum = dbx_reg_number (rtl);
13036 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13037 return 0;
13038 return one_reg_loc_descriptor (dbx_regnum, initialized);
13042 /* Return a location descriptor that designates a machine register for
13043 a given hard register number. */
13045 static dw_loc_descr_ref
13046 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13048 dw_loc_descr_ref reg_loc_descr;
13050 if (regno <= 31)
13051 reg_loc_descr
13052 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13053 else
13054 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13056 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13057 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13059 return reg_loc_descr;
13062 /* Given an RTL of a register, return a location descriptor that
13063 designates a value that spans more than one register. */
13065 static dw_loc_descr_ref
13066 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13067 enum var_init_status initialized)
13069 int size, i;
13070 dw_loc_descr_ref loc_result = NULL;
13072 /* Simple, contiguous registers. */
13073 if (regs == NULL_RTX)
13075 unsigned reg = REGNO (rtl);
13076 int nregs;
13078 #ifdef LEAF_REG_REMAP
13079 if (crtl->uses_only_leaf_regs)
13081 int leaf_reg = LEAF_REG_REMAP (reg);
13082 if (leaf_reg != -1)
13083 reg = (unsigned) leaf_reg;
13085 #endif
13087 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13088 nregs = REG_NREGS (rtl);
13090 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13092 loc_result = NULL;
13093 while (nregs--)
13095 dw_loc_descr_ref t;
13097 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13098 VAR_INIT_STATUS_INITIALIZED);
13099 add_loc_descr (&loc_result, t);
13100 add_loc_descr_op_piece (&loc_result, size);
13101 ++reg;
13103 return loc_result;
13106 /* Now onto stupid register sets in non contiguous locations. */
13108 gcc_assert (GET_CODE (regs) == PARALLEL);
13110 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13111 loc_result = NULL;
13113 for (i = 0; i < XVECLEN (regs, 0); ++i)
13115 dw_loc_descr_ref t;
13117 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13118 VAR_INIT_STATUS_INITIALIZED);
13119 add_loc_descr (&loc_result, t);
13120 add_loc_descr_op_piece (&loc_result, size);
13123 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13124 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13125 return loc_result;
13128 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13130 /* Return a location descriptor that designates a constant i,
13131 as a compound operation from constant (i >> shift), constant shift
13132 and DW_OP_shl. */
13134 static dw_loc_descr_ref
13135 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13137 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13138 add_loc_descr (&ret, int_loc_descriptor (shift));
13139 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13140 return ret;
13143 /* Return a location descriptor that designates a constant. */
13145 static dw_loc_descr_ref
13146 int_loc_descriptor (HOST_WIDE_INT i)
13148 enum dwarf_location_atom op;
13150 /* Pick the smallest representation of a constant, rather than just
13151 defaulting to the LEB encoding. */
13152 if (i >= 0)
13154 int clz = clz_hwi (i);
13155 int ctz = ctz_hwi (i);
13156 if (i <= 31)
13157 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13158 else if (i <= 0xff)
13159 op = DW_OP_const1u;
13160 else if (i <= 0xffff)
13161 op = DW_OP_const2u;
13162 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13163 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13164 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13165 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13166 while DW_OP_const4u is 5 bytes. */
13167 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13168 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13169 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13170 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13171 while DW_OP_const4u is 5 bytes. */
13172 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13174 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13175 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13176 <= 4)
13178 /* As i >= 2**31, the double cast above will yield a negative number.
13179 Since wrapping is defined in DWARF expressions we can output big
13180 positive integers as small negative ones, regardless of the size
13181 of host wide ints.
13183 Here, since the evaluator will handle 32-bit values and since i >=
13184 2**31, we know it's going to be interpreted as a negative literal:
13185 store it this way if we can do better than 5 bytes this way. */
13186 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13188 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13189 op = DW_OP_const4u;
13191 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13192 least 6 bytes: see if we can do better before falling back to it. */
13193 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13194 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13195 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13196 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13197 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13198 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13199 >= HOST_BITS_PER_WIDE_INT)
13200 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13201 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13202 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13203 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13204 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13205 && size_of_uleb128 (i) > 6)
13206 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13207 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13208 else
13209 op = DW_OP_constu;
13211 else
13213 if (i >= -0x80)
13214 op = DW_OP_const1s;
13215 else if (i >= -0x8000)
13216 op = DW_OP_const2s;
13217 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13219 if (size_of_int_loc_descriptor (i) < 5)
13221 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13222 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13223 return ret;
13225 op = DW_OP_const4s;
13227 else
13229 if (size_of_int_loc_descriptor (i)
13230 < (unsigned long) 1 + size_of_sleb128 (i))
13232 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13233 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13234 return ret;
13236 op = DW_OP_consts;
13240 return new_loc_descr (op, i, 0);
13243 /* Likewise, for unsigned constants. */
13245 static dw_loc_descr_ref
13246 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13248 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13249 const unsigned HOST_WIDE_INT max_uint
13250 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13252 /* If possible, use the clever signed constants handling. */
13253 if (i <= max_int)
13254 return int_loc_descriptor ((HOST_WIDE_INT) i);
13256 /* Here, we are left with positive numbers that cannot be represented as
13257 HOST_WIDE_INT, i.e.:
13258 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13260 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13261 whereas may be better to output a negative integer: thanks to integer
13262 wrapping, we know that:
13263 x = x - 2 ** DWARF2_ADDR_SIZE
13264 = x - 2 * (max (HOST_WIDE_INT) + 1)
13265 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13266 small negative integers. Let's try that in cases it will clearly improve
13267 the encoding: there is no gain turning DW_OP_const4u into
13268 DW_OP_const4s. */
13269 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13270 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13271 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13273 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13275 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13276 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13277 const HOST_WIDE_INT second_shift
13278 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13280 /* So we finally have:
13281 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13282 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13283 return int_loc_descriptor (second_shift);
13286 /* Last chance: fallback to a simple constant operation. */
13287 return new_loc_descr
13288 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13289 ? DW_OP_const4u
13290 : DW_OP_const8u,
13291 i, 0);
13294 /* Generate and return a location description that computes the unsigned
13295 comparison of the two stack top entries (a OP b where b is the top-most
13296 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13297 LE_EXPR, GT_EXPR or GE_EXPR. */
13299 static dw_loc_descr_ref
13300 uint_comparison_loc_list (enum tree_code kind)
13302 enum dwarf_location_atom op, flip_op;
13303 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13305 switch (kind)
13307 case LT_EXPR:
13308 op = DW_OP_lt;
13309 break;
13310 case LE_EXPR:
13311 op = DW_OP_le;
13312 break;
13313 case GT_EXPR:
13314 op = DW_OP_gt;
13315 break;
13316 case GE_EXPR:
13317 op = DW_OP_ge;
13318 break;
13319 default:
13320 gcc_unreachable ();
13323 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13324 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13326 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13327 possible to perform unsigned comparisons: we just have to distinguish
13328 three cases:
13330 1. when a and b have the same sign (as signed integers); then we should
13331 return: a OP(signed) b;
13333 2. when a is a negative signed integer while b is a positive one, then a
13334 is a greater unsigned integer than b; likewise when a and b's roles
13335 are flipped.
13337 So first, compare the sign of the two operands. */
13338 ret = new_loc_descr (DW_OP_over, 0, 0);
13339 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13340 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13341 /* If they have different signs (i.e. they have different sign bits), then
13342 the stack top value has now the sign bit set and thus it's smaller than
13343 zero. */
13344 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13345 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13346 add_loc_descr (&ret, bra_node);
13348 /* We are in case 1. At this point, we know both operands have the same
13349 sign, to it's safe to use the built-in signed comparison. */
13350 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13351 add_loc_descr (&ret, jmp_node);
13353 /* We are in case 2. Here, we know both operands do not have the same sign,
13354 so we have to flip the signed comparison. */
13355 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13356 tmp = new_loc_descr (flip_op, 0, 0);
13357 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13358 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13359 add_loc_descr (&ret, tmp);
13361 /* This dummy operation is necessary to make the two branches join. */
13362 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13363 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13364 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13365 add_loc_descr (&ret, tmp);
13367 return ret;
13370 /* Likewise, but takes the location description lists (might be destructive on
13371 them). Return NULL if either is NULL or if concatenation fails. */
13373 static dw_loc_list_ref
13374 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13375 enum tree_code kind)
13377 if (left == NULL || right == NULL)
13378 return NULL;
13380 add_loc_list (&left, right);
13381 if (left == NULL)
13382 return NULL;
13384 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13385 return left;
13388 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13389 without actually allocating it. */
13391 static unsigned long
13392 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13394 return size_of_int_loc_descriptor (i >> shift)
13395 + size_of_int_loc_descriptor (shift)
13396 + 1;
13399 /* Return size_of_locs (int_loc_descriptor (i)) without
13400 actually allocating it. */
13402 static unsigned long
13403 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13405 unsigned long s;
13407 if (i >= 0)
13409 int clz, ctz;
13410 if (i <= 31)
13411 return 1;
13412 else if (i <= 0xff)
13413 return 2;
13414 else if (i <= 0xffff)
13415 return 3;
13416 clz = clz_hwi (i);
13417 ctz = ctz_hwi (i);
13418 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13419 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13420 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13421 - clz - 5);
13422 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13423 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13424 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13425 - clz - 8);
13426 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13427 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13428 <= 4)
13429 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13430 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13431 return 5;
13432 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13433 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13434 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13435 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13436 - clz - 8);
13437 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13438 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13439 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13440 - clz - 16);
13441 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13442 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13443 && s > 6)
13444 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13445 - clz - 32);
13446 else
13447 return 1 + s;
13449 else
13451 if (i >= -0x80)
13452 return 2;
13453 else if (i >= -0x8000)
13454 return 3;
13455 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13457 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13459 s = size_of_int_loc_descriptor (-i) + 1;
13460 if (s < 5)
13461 return s;
13463 return 5;
13465 else
13467 unsigned long r = 1 + size_of_sleb128 (i);
13468 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13470 s = size_of_int_loc_descriptor (-i) + 1;
13471 if (s < r)
13472 return s;
13474 return r;
13479 /* Return loc description representing "address" of integer value.
13480 This can appear only as toplevel expression. */
13482 static dw_loc_descr_ref
13483 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13485 int litsize;
13486 dw_loc_descr_ref loc_result = NULL;
13488 if (!(dwarf_version >= 4 || !dwarf_strict))
13489 return NULL;
13491 litsize = size_of_int_loc_descriptor (i);
13492 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13493 is more compact. For DW_OP_stack_value we need:
13494 litsize + 1 (DW_OP_stack_value)
13495 and for DW_OP_implicit_value:
13496 1 (DW_OP_implicit_value) + 1 (length) + size. */
13497 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13499 loc_result = int_loc_descriptor (i);
13500 add_loc_descr (&loc_result,
13501 new_loc_descr (DW_OP_stack_value, 0, 0));
13502 return loc_result;
13505 loc_result = new_loc_descr (DW_OP_implicit_value,
13506 size, 0);
13507 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13508 loc_result->dw_loc_oprnd2.v.val_int = i;
13509 return loc_result;
13512 /* Return a location descriptor that designates a base+offset location. */
13514 static dw_loc_descr_ref
13515 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13516 enum var_init_status initialized)
13518 unsigned int regno;
13519 dw_loc_descr_ref result;
13520 dw_fde_ref fde = cfun->fde;
13522 /* We only use "frame base" when we're sure we're talking about the
13523 post-prologue local stack frame. We do this by *not* running
13524 register elimination until this point, and recognizing the special
13525 argument pointer and soft frame pointer rtx's. */
13526 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13528 rtx elim = (ira_use_lra_p
13529 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13530 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13532 if (elim != reg)
13534 if (GET_CODE (elim) == PLUS)
13536 offset += INTVAL (XEXP (elim, 1));
13537 elim = XEXP (elim, 0);
13539 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13540 && (elim == hard_frame_pointer_rtx
13541 || elim == stack_pointer_rtx))
13542 || elim == (frame_pointer_needed
13543 ? hard_frame_pointer_rtx
13544 : stack_pointer_rtx));
13546 /* If drap register is used to align stack, use frame
13547 pointer + offset to access stack variables. If stack
13548 is aligned without drap, use stack pointer + offset to
13549 access stack variables. */
13550 if (crtl->stack_realign_tried
13551 && reg == frame_pointer_rtx)
13553 int base_reg
13554 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13555 ? HARD_FRAME_POINTER_REGNUM
13556 : REGNO (elim));
13557 return new_reg_loc_descr (base_reg, offset);
13560 gcc_assert (frame_pointer_fb_offset_valid);
13561 offset += frame_pointer_fb_offset;
13562 return new_loc_descr (DW_OP_fbreg, offset, 0);
13566 regno = REGNO (reg);
13567 #ifdef LEAF_REG_REMAP
13568 if (crtl->uses_only_leaf_regs)
13570 int leaf_reg = LEAF_REG_REMAP (regno);
13571 if (leaf_reg != -1)
13572 regno = (unsigned) leaf_reg;
13574 #endif
13575 regno = DWARF_FRAME_REGNUM (regno);
13577 if (!optimize && fde
13578 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13580 /* Use cfa+offset to represent the location of arguments passed
13581 on the stack when drap is used to align stack.
13582 Only do this when not optimizing, for optimized code var-tracking
13583 is supposed to track where the arguments live and the register
13584 used as vdrap or drap in some spot might be used for something
13585 else in other part of the routine. */
13586 return new_loc_descr (DW_OP_fbreg, offset, 0);
13589 if (regno <= 31)
13590 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13591 offset, 0);
13592 else
13593 result = new_loc_descr (DW_OP_bregx, regno, offset);
13595 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13596 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13598 return result;
13601 /* Return true if this RTL expression describes a base+offset calculation. */
13603 static inline int
13604 is_based_loc (const_rtx rtl)
13606 return (GET_CODE (rtl) == PLUS
13607 && ((REG_P (XEXP (rtl, 0))
13608 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13609 && CONST_INT_P (XEXP (rtl, 1)))));
13612 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13613 failed. */
13615 static dw_loc_descr_ref
13616 tls_mem_loc_descriptor (rtx mem)
13618 tree base;
13619 dw_loc_descr_ref loc_result;
13621 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13622 return NULL;
13624 base = get_base_address (MEM_EXPR (mem));
13625 if (base == NULL
13626 || !VAR_P (base)
13627 || !DECL_THREAD_LOCAL_P (base))
13628 return NULL;
13630 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13631 if (loc_result == NULL)
13632 return NULL;
13634 if (MEM_OFFSET (mem))
13635 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13637 return loc_result;
13640 /* Output debug info about reason why we failed to expand expression as dwarf
13641 expression. */
13643 static void
13644 expansion_failed (tree expr, rtx rtl, char const *reason)
13646 if (dump_file && (dump_flags & TDF_DETAILS))
13648 fprintf (dump_file, "Failed to expand as dwarf: ");
13649 if (expr)
13650 print_generic_expr (dump_file, expr, dump_flags);
13651 if (rtl)
13653 fprintf (dump_file, "\n");
13654 print_rtl (dump_file, rtl);
13656 fprintf (dump_file, "\nReason: %s\n", reason);
13660 /* Helper function for const_ok_for_output. */
13662 static bool
13663 const_ok_for_output_1 (rtx rtl)
13665 if (GET_CODE (rtl) == UNSPEC)
13667 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13668 we can't express it in the debug info. */
13669 /* Don't complain about TLS UNSPECs, those are just too hard to
13670 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13671 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13672 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13673 if (flag_checking
13674 && (XVECLEN (rtl, 0) == 0
13675 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13676 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13677 inform (current_function_decl
13678 ? DECL_SOURCE_LOCATION (current_function_decl)
13679 : UNKNOWN_LOCATION,
13680 #if NUM_UNSPEC_VALUES > 0
13681 "non-delegitimized UNSPEC %s (%d) found in variable location",
13682 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13683 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13684 XINT (rtl, 1));
13685 #else
13686 "non-delegitimized UNSPEC %d found in variable location",
13687 XINT (rtl, 1));
13688 #endif
13689 expansion_failed (NULL_TREE, rtl,
13690 "UNSPEC hasn't been delegitimized.\n");
13691 return false;
13694 if (targetm.const_not_ok_for_debug_p (rtl))
13696 expansion_failed (NULL_TREE, rtl,
13697 "Expression rejected for debug by the backend.\n");
13698 return false;
13701 /* FIXME: Refer to PR60655. It is possible for simplification
13702 of rtl expressions in var tracking to produce such expressions.
13703 We should really identify / validate expressions
13704 enclosed in CONST that can be handled by assemblers on various
13705 targets and only handle legitimate cases here. */
13706 if (GET_CODE (rtl) != SYMBOL_REF)
13708 if (GET_CODE (rtl) == NOT)
13709 return false;
13710 return true;
13713 if (CONSTANT_POOL_ADDRESS_P (rtl))
13715 bool marked;
13716 get_pool_constant_mark (rtl, &marked);
13717 /* If all references to this pool constant were optimized away,
13718 it was not output and thus we can't represent it. */
13719 if (!marked)
13721 expansion_failed (NULL_TREE, rtl,
13722 "Constant was removed from constant pool.\n");
13723 return false;
13727 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13728 return false;
13730 /* Avoid references to external symbols in debug info, on several targets
13731 the linker might even refuse to link when linking a shared library,
13732 and in many other cases the relocations for .debug_info/.debug_loc are
13733 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13734 to be defined within the same shared library or executable are fine. */
13735 if (SYMBOL_REF_EXTERNAL_P (rtl))
13737 tree decl = SYMBOL_REF_DECL (rtl);
13739 if (decl == NULL || !targetm.binds_local_p (decl))
13741 expansion_failed (NULL_TREE, rtl,
13742 "Symbol not defined in current TU.\n");
13743 return false;
13747 return true;
13750 /* Return true if constant RTL can be emitted in DW_OP_addr or
13751 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13752 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13754 static bool
13755 const_ok_for_output (rtx rtl)
13757 if (GET_CODE (rtl) == SYMBOL_REF)
13758 return const_ok_for_output_1 (rtl);
13760 if (GET_CODE (rtl) == CONST)
13762 subrtx_var_iterator::array_type array;
13763 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13764 if (!const_ok_for_output_1 (*iter))
13765 return false;
13766 return true;
13769 return true;
13772 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13773 if possible, NULL otherwise. */
13775 static dw_die_ref
13776 base_type_for_mode (machine_mode mode, bool unsignedp)
13778 dw_die_ref type_die;
13779 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13781 if (type == NULL)
13782 return NULL;
13783 switch (TREE_CODE (type))
13785 case INTEGER_TYPE:
13786 case REAL_TYPE:
13787 break;
13788 default:
13789 return NULL;
13791 type_die = lookup_type_die (type);
13792 if (!type_die)
13793 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13794 comp_unit_die ());
13795 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13796 return NULL;
13797 return type_die;
13800 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13801 type matching MODE, or, if MODE is narrower than or as wide as
13802 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13803 possible. */
13805 static dw_loc_descr_ref
13806 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13808 machine_mode outer_mode = mode;
13809 dw_die_ref type_die;
13810 dw_loc_descr_ref cvt;
13812 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13814 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13815 return op;
13817 type_die = base_type_for_mode (outer_mode, 1);
13818 if (type_die == NULL)
13819 return NULL;
13820 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13821 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13822 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13823 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13824 add_loc_descr (&op, cvt);
13825 return op;
13828 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13830 static dw_loc_descr_ref
13831 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13832 dw_loc_descr_ref op1)
13834 dw_loc_descr_ref ret = op0;
13835 add_loc_descr (&ret, op1);
13836 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13837 if (STORE_FLAG_VALUE != 1)
13839 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13840 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13842 return ret;
13845 /* Return location descriptor for signed comparison OP RTL. */
13847 static dw_loc_descr_ref
13848 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13849 machine_mode mem_mode)
13851 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13852 dw_loc_descr_ref op0, op1;
13853 int shift;
13855 if (op_mode == VOIDmode)
13856 op_mode = GET_MODE (XEXP (rtl, 1));
13857 if (op_mode == VOIDmode)
13858 return NULL;
13860 if (dwarf_strict
13861 && dwarf_version < 5
13862 && (!SCALAR_INT_MODE_P (op_mode)
13863 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13864 return NULL;
13866 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13867 VAR_INIT_STATUS_INITIALIZED);
13868 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13869 VAR_INIT_STATUS_INITIALIZED);
13871 if (op0 == NULL || op1 == NULL)
13872 return NULL;
13874 if (!SCALAR_INT_MODE_P (op_mode)
13875 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13876 return compare_loc_descriptor (op, op0, op1);
13878 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13880 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13881 dw_loc_descr_ref cvt;
13883 if (type_die == NULL)
13884 return NULL;
13885 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13886 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13887 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13888 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13889 add_loc_descr (&op0, cvt);
13890 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13891 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13892 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13893 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13894 add_loc_descr (&op1, cvt);
13895 return compare_loc_descriptor (op, op0, op1);
13898 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13899 /* For eq/ne, if the operands are known to be zero-extended,
13900 there is no need to do the fancy shifting up. */
13901 if (op == DW_OP_eq || op == DW_OP_ne)
13903 dw_loc_descr_ref last0, last1;
13904 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13906 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13908 /* deref_size zero extends, and for constants we can check
13909 whether they are zero extended or not. */
13910 if (((last0->dw_loc_opc == DW_OP_deref_size
13911 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13912 || (CONST_INT_P (XEXP (rtl, 0))
13913 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13914 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13915 && ((last1->dw_loc_opc == DW_OP_deref_size
13916 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13917 || (CONST_INT_P (XEXP (rtl, 1))
13918 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13919 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13920 return compare_loc_descriptor (op, op0, op1);
13922 /* EQ/NE comparison against constant in narrower type than
13923 DWARF2_ADDR_SIZE can be performed either as
13924 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13925 DW_OP_{eq,ne}
13927 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13928 DW_OP_{eq,ne}. Pick whatever is shorter. */
13929 if (CONST_INT_P (XEXP (rtl, 1))
13930 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13931 && (size_of_int_loc_descriptor (shift) + 1
13932 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13933 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13934 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13935 & GET_MODE_MASK (op_mode))))
13937 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13938 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13939 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13940 & GET_MODE_MASK (op_mode));
13941 return compare_loc_descriptor (op, op0, op1);
13944 add_loc_descr (&op0, int_loc_descriptor (shift));
13945 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13946 if (CONST_INT_P (XEXP (rtl, 1)))
13947 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13948 else
13950 add_loc_descr (&op1, int_loc_descriptor (shift));
13951 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13953 return compare_loc_descriptor (op, op0, op1);
13956 /* Return location descriptor for unsigned comparison OP RTL. */
13958 static dw_loc_descr_ref
13959 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13960 machine_mode mem_mode)
13962 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13963 dw_loc_descr_ref op0, op1;
13965 if (op_mode == VOIDmode)
13966 op_mode = GET_MODE (XEXP (rtl, 1));
13967 if (op_mode == VOIDmode)
13968 return NULL;
13969 if (!SCALAR_INT_MODE_P (op_mode))
13970 return NULL;
13972 if (dwarf_strict
13973 && dwarf_version < 5
13974 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13975 return NULL;
13977 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13978 VAR_INIT_STATUS_INITIALIZED);
13979 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13980 VAR_INIT_STATUS_INITIALIZED);
13982 if (op0 == NULL || op1 == NULL)
13983 return NULL;
13985 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13987 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13988 dw_loc_descr_ref last0, last1;
13989 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13991 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13993 if (CONST_INT_P (XEXP (rtl, 0)))
13994 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13995 /* deref_size zero extends, so no need to mask it again. */
13996 else if (last0->dw_loc_opc != DW_OP_deref_size
13997 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13999 add_loc_descr (&op0, int_loc_descriptor (mask));
14000 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14002 if (CONST_INT_P (XEXP (rtl, 1)))
14003 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14004 /* deref_size zero extends, so no need to mask it again. */
14005 else if (last1->dw_loc_opc != DW_OP_deref_size
14006 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14008 add_loc_descr (&op1, int_loc_descriptor (mask));
14009 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14012 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14014 HOST_WIDE_INT bias = 1;
14015 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14016 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14017 if (CONST_INT_P (XEXP (rtl, 1)))
14018 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14019 + INTVAL (XEXP (rtl, 1)));
14020 else
14021 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14022 bias, 0));
14024 return compare_loc_descriptor (op, op0, op1);
14027 /* Return location descriptor for {U,S}{MIN,MAX}. */
14029 static dw_loc_descr_ref
14030 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14031 machine_mode mem_mode)
14033 enum dwarf_location_atom op;
14034 dw_loc_descr_ref op0, op1, ret;
14035 dw_loc_descr_ref bra_node, drop_node;
14037 if (dwarf_strict
14038 && dwarf_version < 5
14039 && (!SCALAR_INT_MODE_P (mode)
14040 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14041 return NULL;
14043 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14044 VAR_INIT_STATUS_INITIALIZED);
14045 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14046 VAR_INIT_STATUS_INITIALIZED);
14048 if (op0 == NULL || op1 == NULL)
14049 return NULL;
14051 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14052 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14053 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14054 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14056 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14058 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14059 add_loc_descr (&op0, int_loc_descriptor (mask));
14060 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14061 add_loc_descr (&op1, int_loc_descriptor (mask));
14062 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14064 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14066 HOST_WIDE_INT bias = 1;
14067 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14068 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14069 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14072 else if (!SCALAR_INT_MODE_P (mode)
14073 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14075 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14076 add_loc_descr (&op0, int_loc_descriptor (shift));
14077 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14078 add_loc_descr (&op1, int_loc_descriptor (shift));
14079 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14081 else if (SCALAR_INT_MODE_P (mode)
14082 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14084 dw_die_ref type_die = base_type_for_mode (mode, 0);
14085 dw_loc_descr_ref cvt;
14086 if (type_die == NULL)
14087 return NULL;
14088 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14089 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14090 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14091 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14092 add_loc_descr (&op0, cvt);
14093 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14094 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14095 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14096 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14097 add_loc_descr (&op1, cvt);
14100 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14101 op = DW_OP_lt;
14102 else
14103 op = DW_OP_gt;
14104 ret = op0;
14105 add_loc_descr (&ret, op1);
14106 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14107 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14108 add_loc_descr (&ret, bra_node);
14109 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14110 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14111 add_loc_descr (&ret, drop_node);
14112 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14113 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14114 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14115 && SCALAR_INT_MODE_P (mode)
14116 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14117 ret = convert_descriptor_to_mode (mode, ret);
14118 return ret;
14121 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14122 but after converting arguments to type_die, afterwards
14123 convert back to unsigned. */
14125 static dw_loc_descr_ref
14126 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14127 machine_mode mode, machine_mode mem_mode)
14129 dw_loc_descr_ref cvt, op0, op1;
14131 if (type_die == NULL)
14132 return NULL;
14133 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14134 VAR_INIT_STATUS_INITIALIZED);
14135 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14136 VAR_INIT_STATUS_INITIALIZED);
14137 if (op0 == NULL || op1 == NULL)
14138 return NULL;
14139 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14140 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14141 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14142 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14143 add_loc_descr (&op0, cvt);
14144 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14145 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14146 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14147 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14148 add_loc_descr (&op1, cvt);
14149 add_loc_descr (&op0, op1);
14150 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14151 return convert_descriptor_to_mode (mode, op0);
14154 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14155 const0 is DW_OP_lit0 or corresponding typed constant,
14156 const1 is DW_OP_lit1 or corresponding typed constant
14157 and constMSB is constant with just the MSB bit set
14158 for the mode):
14159 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14160 L1: const0 DW_OP_swap
14161 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14162 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14163 L3: DW_OP_drop
14164 L4: DW_OP_nop
14166 CTZ is similar:
14167 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14168 L1: const0 DW_OP_swap
14169 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14170 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14171 L3: DW_OP_drop
14172 L4: DW_OP_nop
14174 FFS is similar:
14175 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14176 L1: const1 DW_OP_swap
14177 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14178 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14179 L3: DW_OP_drop
14180 L4: DW_OP_nop */
14182 static dw_loc_descr_ref
14183 clz_loc_descriptor (rtx rtl, machine_mode mode,
14184 machine_mode mem_mode)
14186 dw_loc_descr_ref op0, ret, tmp;
14187 HOST_WIDE_INT valv;
14188 dw_loc_descr_ref l1jump, l1label;
14189 dw_loc_descr_ref l2jump, l2label;
14190 dw_loc_descr_ref l3jump, l3label;
14191 dw_loc_descr_ref l4jump, l4label;
14192 rtx msb;
14194 if (!SCALAR_INT_MODE_P (mode)
14195 || GET_MODE (XEXP (rtl, 0)) != mode)
14196 return NULL;
14198 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14199 VAR_INIT_STATUS_INITIALIZED);
14200 if (op0 == NULL)
14201 return NULL;
14202 ret = op0;
14203 if (GET_CODE (rtl) == CLZ)
14205 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14206 valv = GET_MODE_BITSIZE (mode);
14208 else if (GET_CODE (rtl) == FFS)
14209 valv = 0;
14210 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14211 valv = GET_MODE_BITSIZE (mode);
14212 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14213 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14214 add_loc_descr (&ret, l1jump);
14215 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14216 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14217 VAR_INIT_STATUS_INITIALIZED);
14218 if (tmp == NULL)
14219 return NULL;
14220 add_loc_descr (&ret, tmp);
14221 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14222 add_loc_descr (&ret, l4jump);
14223 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14224 ? const1_rtx : const0_rtx,
14225 mode, mem_mode,
14226 VAR_INIT_STATUS_INITIALIZED);
14227 if (l1label == NULL)
14228 return NULL;
14229 add_loc_descr (&ret, l1label);
14230 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14231 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14232 add_loc_descr (&ret, l2label);
14233 if (GET_CODE (rtl) != CLZ)
14234 msb = const1_rtx;
14235 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14236 msb = GEN_INT (HOST_WIDE_INT_1U
14237 << (GET_MODE_BITSIZE (mode) - 1));
14238 else
14239 msb = immed_wide_int_const
14240 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14241 GET_MODE_PRECISION (mode)), mode);
14242 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14243 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14244 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14245 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14246 else
14247 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14248 VAR_INIT_STATUS_INITIALIZED);
14249 if (tmp == NULL)
14250 return NULL;
14251 add_loc_descr (&ret, tmp);
14252 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14253 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14254 add_loc_descr (&ret, l3jump);
14255 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14256 VAR_INIT_STATUS_INITIALIZED);
14257 if (tmp == NULL)
14258 return NULL;
14259 add_loc_descr (&ret, tmp);
14260 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14261 ? DW_OP_shl : DW_OP_shr, 0, 0));
14262 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14263 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14264 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14265 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14266 add_loc_descr (&ret, l2jump);
14267 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14268 add_loc_descr (&ret, l3label);
14269 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14270 add_loc_descr (&ret, l4label);
14271 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14272 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14273 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14274 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14275 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14276 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14277 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14278 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14279 return ret;
14282 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14283 const1 is DW_OP_lit1 or corresponding typed constant):
14284 const0 DW_OP_swap
14285 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14286 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14287 L2: DW_OP_drop
14289 PARITY is similar:
14290 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14291 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14292 L2: DW_OP_drop */
14294 static dw_loc_descr_ref
14295 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14296 machine_mode mem_mode)
14298 dw_loc_descr_ref op0, ret, tmp;
14299 dw_loc_descr_ref l1jump, l1label;
14300 dw_loc_descr_ref l2jump, l2label;
14302 if (!SCALAR_INT_MODE_P (mode)
14303 || GET_MODE (XEXP (rtl, 0)) != mode)
14304 return NULL;
14306 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14307 VAR_INIT_STATUS_INITIALIZED);
14308 if (op0 == NULL)
14309 return NULL;
14310 ret = op0;
14311 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14312 VAR_INIT_STATUS_INITIALIZED);
14313 if (tmp == NULL)
14314 return NULL;
14315 add_loc_descr (&ret, tmp);
14316 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14317 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14318 add_loc_descr (&ret, l1label);
14319 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14320 add_loc_descr (&ret, l2jump);
14321 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14322 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14323 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14324 VAR_INIT_STATUS_INITIALIZED);
14325 if (tmp == NULL)
14326 return NULL;
14327 add_loc_descr (&ret, tmp);
14328 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14329 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14330 ? DW_OP_plus : DW_OP_xor, 0, 0));
14331 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14332 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14333 VAR_INIT_STATUS_INITIALIZED);
14334 add_loc_descr (&ret, tmp);
14335 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14336 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14337 add_loc_descr (&ret, l1jump);
14338 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14339 add_loc_descr (&ret, l2label);
14340 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14341 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14342 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14343 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14344 return ret;
14347 /* BSWAP (constS is initial shift count, either 56 or 24):
14348 constS const0
14349 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14350 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14351 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14352 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14353 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14355 static dw_loc_descr_ref
14356 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14357 machine_mode mem_mode)
14359 dw_loc_descr_ref op0, ret, tmp;
14360 dw_loc_descr_ref l1jump, l1label;
14361 dw_loc_descr_ref l2jump, l2label;
14363 if (!SCALAR_INT_MODE_P (mode)
14364 || BITS_PER_UNIT != 8
14365 || (GET_MODE_BITSIZE (mode) != 32
14366 && GET_MODE_BITSIZE (mode) != 64))
14367 return NULL;
14369 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14370 VAR_INIT_STATUS_INITIALIZED);
14371 if (op0 == NULL)
14372 return NULL;
14374 ret = op0;
14375 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14376 mode, mem_mode,
14377 VAR_INIT_STATUS_INITIALIZED);
14378 if (tmp == NULL)
14379 return NULL;
14380 add_loc_descr (&ret, tmp);
14381 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14382 VAR_INIT_STATUS_INITIALIZED);
14383 if (tmp == NULL)
14384 return NULL;
14385 add_loc_descr (&ret, tmp);
14386 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14387 add_loc_descr (&ret, l1label);
14388 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14389 mode, mem_mode,
14390 VAR_INIT_STATUS_INITIALIZED);
14391 add_loc_descr (&ret, tmp);
14392 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14393 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14394 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14395 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14396 VAR_INIT_STATUS_INITIALIZED);
14397 if (tmp == NULL)
14398 return NULL;
14399 add_loc_descr (&ret, tmp);
14400 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14401 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14403 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14404 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14405 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14406 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14407 VAR_INIT_STATUS_INITIALIZED);
14408 add_loc_descr (&ret, tmp);
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14410 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14411 add_loc_descr (&ret, l2jump);
14412 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14413 VAR_INIT_STATUS_INITIALIZED);
14414 add_loc_descr (&ret, tmp);
14415 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14416 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14417 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14418 add_loc_descr (&ret, l1jump);
14419 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14420 add_loc_descr (&ret, l2label);
14421 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14422 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14423 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14424 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14425 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14426 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14427 return ret;
14430 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14431 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14432 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14433 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14435 ROTATERT is similar:
14436 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14437 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14438 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14440 static dw_loc_descr_ref
14441 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14442 machine_mode mem_mode)
14444 rtx rtlop1 = XEXP (rtl, 1);
14445 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14446 int i;
14448 if (!SCALAR_INT_MODE_P (mode))
14449 return NULL;
14451 if (GET_MODE (rtlop1) != VOIDmode
14452 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14453 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14454 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14455 VAR_INIT_STATUS_INITIALIZED);
14456 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14457 VAR_INIT_STATUS_INITIALIZED);
14458 if (op0 == NULL || op1 == NULL)
14459 return NULL;
14460 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14461 for (i = 0; i < 2; i++)
14463 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14464 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14465 mode, mem_mode,
14466 VAR_INIT_STATUS_INITIALIZED);
14467 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14468 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14469 ? DW_OP_const4u
14470 : HOST_BITS_PER_WIDE_INT == 64
14471 ? DW_OP_const8u : DW_OP_constu,
14472 GET_MODE_MASK (mode), 0);
14473 else
14474 mask[i] = NULL;
14475 if (mask[i] == NULL)
14476 return NULL;
14477 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14479 ret = op0;
14480 add_loc_descr (&ret, op1);
14481 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14482 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14483 if (GET_CODE (rtl) == ROTATERT)
14485 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14486 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14487 GET_MODE_BITSIZE (mode), 0));
14489 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14490 if (mask[0] != NULL)
14491 add_loc_descr (&ret, mask[0]);
14492 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14493 if (mask[1] != NULL)
14495 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14496 add_loc_descr (&ret, mask[1]);
14497 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14499 if (GET_CODE (rtl) == ROTATE)
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14502 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14503 GET_MODE_BITSIZE (mode), 0));
14505 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14507 return ret;
14510 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14511 for DEBUG_PARAMETER_REF RTL. */
14513 static dw_loc_descr_ref
14514 parameter_ref_descriptor (rtx rtl)
14516 dw_loc_descr_ref ret;
14517 dw_die_ref ref;
14519 if (dwarf_strict)
14520 return NULL;
14521 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14522 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14523 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14524 if (ref)
14526 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14527 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14528 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14530 else
14532 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14533 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14535 return ret;
14538 /* The following routine converts the RTL for a variable or parameter
14539 (resident in memory) into an equivalent Dwarf representation of a
14540 mechanism for getting the address of that same variable onto the top of a
14541 hypothetical "address evaluation" stack.
14543 When creating memory location descriptors, we are effectively transforming
14544 the RTL for a memory-resident object into its Dwarf postfix expression
14545 equivalent. This routine recursively descends an RTL tree, turning
14546 it into Dwarf postfix code as it goes.
14548 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14550 MEM_MODE is the mode of the memory reference, needed to handle some
14551 autoincrement addressing modes.
14553 Return 0 if we can't represent the location. */
14555 dw_loc_descr_ref
14556 mem_loc_descriptor (rtx rtl, machine_mode mode,
14557 machine_mode mem_mode,
14558 enum var_init_status initialized)
14560 dw_loc_descr_ref mem_loc_result = NULL;
14561 enum dwarf_location_atom op;
14562 dw_loc_descr_ref op0, op1;
14563 rtx inner = NULL_RTX;
14565 if (mode == VOIDmode)
14566 mode = GET_MODE (rtl);
14568 /* Note that for a dynamically sized array, the location we will generate a
14569 description of here will be the lowest numbered location which is
14570 actually within the array. That's *not* necessarily the same as the
14571 zeroth element of the array. */
14573 rtl = targetm.delegitimize_address (rtl);
14575 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14576 return NULL;
14578 switch (GET_CODE (rtl))
14580 case POST_INC:
14581 case POST_DEC:
14582 case POST_MODIFY:
14583 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14585 case SUBREG:
14586 /* The case of a subreg may arise when we have a local (register)
14587 variable or a formal (register) parameter which doesn't quite fill
14588 up an entire register. For now, just assume that it is
14589 legitimate to make the Dwarf info refer to the whole register which
14590 contains the given subreg. */
14591 if (!subreg_lowpart_p (rtl))
14592 break;
14593 inner = SUBREG_REG (rtl);
14594 /* FALLTHRU */
14595 case TRUNCATE:
14596 if (inner == NULL_RTX)
14597 inner = XEXP (rtl, 0);
14598 if (SCALAR_INT_MODE_P (mode)
14599 && SCALAR_INT_MODE_P (GET_MODE (inner))
14600 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14601 #ifdef POINTERS_EXTEND_UNSIGNED
14602 || (mode == Pmode && mem_mode != VOIDmode)
14603 #endif
14605 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14607 mem_loc_result = mem_loc_descriptor (inner,
14608 GET_MODE (inner),
14609 mem_mode, initialized);
14610 break;
14612 if (dwarf_strict && dwarf_version < 5)
14613 break;
14614 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14615 break;
14616 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14617 && (!SCALAR_INT_MODE_P (mode)
14618 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14619 break;
14620 else
14622 dw_die_ref type_die;
14623 dw_loc_descr_ref cvt;
14625 mem_loc_result = mem_loc_descriptor (inner,
14626 GET_MODE (inner),
14627 mem_mode, initialized);
14628 if (mem_loc_result == NULL)
14629 break;
14630 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14631 if (type_die == NULL)
14633 mem_loc_result = NULL;
14634 break;
14636 if (GET_MODE_SIZE (mode)
14637 != GET_MODE_SIZE (GET_MODE (inner)))
14638 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14639 else
14640 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14641 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14642 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14643 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14644 add_loc_descr (&mem_loc_result, cvt);
14645 if (SCALAR_INT_MODE_P (mode)
14646 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14648 /* Convert it to untyped afterwards. */
14649 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14650 add_loc_descr (&mem_loc_result, cvt);
14653 break;
14655 case REG:
14656 if (! SCALAR_INT_MODE_P (mode)
14657 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14658 && rtl != arg_pointer_rtx
14659 && rtl != frame_pointer_rtx
14660 #ifdef POINTERS_EXTEND_UNSIGNED
14661 && (mode != Pmode || mem_mode == VOIDmode)
14662 #endif
14665 dw_die_ref type_die;
14666 unsigned int dbx_regnum;
14668 if (dwarf_strict && dwarf_version < 5)
14669 break;
14670 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14671 break;
14672 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14673 if (type_die == NULL)
14674 break;
14676 dbx_regnum = dbx_reg_number (rtl);
14677 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14678 break;
14679 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14680 dbx_regnum, 0);
14681 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14682 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14683 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14684 break;
14686 /* Whenever a register number forms a part of the description of the
14687 method for calculating the (dynamic) address of a memory resident
14688 object, DWARF rules require the register number be referred to as
14689 a "base register". This distinction is not based in any way upon
14690 what category of register the hardware believes the given register
14691 belongs to. This is strictly DWARF terminology we're dealing with
14692 here. Note that in cases where the location of a memory-resident
14693 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14694 OP_CONST (0)) the actual DWARF location descriptor that we generate
14695 may just be OP_BASEREG (basereg). This may look deceptively like
14696 the object in question was allocated to a register (rather than in
14697 memory) so DWARF consumers need to be aware of the subtle
14698 distinction between OP_REG and OP_BASEREG. */
14699 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14700 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14701 else if (stack_realign_drap
14702 && crtl->drap_reg
14703 && crtl->args.internal_arg_pointer == rtl
14704 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14706 /* If RTL is internal_arg_pointer, which has been optimized
14707 out, use DRAP instead. */
14708 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14709 VAR_INIT_STATUS_INITIALIZED);
14711 break;
14713 case SIGN_EXTEND:
14714 case ZERO_EXTEND:
14715 if (!SCALAR_INT_MODE_P (mode))
14716 break;
14717 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14718 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14719 if (op0 == 0)
14720 break;
14721 else if (GET_CODE (rtl) == ZERO_EXTEND
14722 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14723 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14724 < HOST_BITS_PER_WIDE_INT
14725 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14726 to expand zero extend as two shifts instead of
14727 masking. */
14728 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14730 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14731 mem_loc_result = op0;
14732 add_loc_descr (&mem_loc_result,
14733 int_loc_descriptor (GET_MODE_MASK (imode)));
14734 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14736 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14738 int shift = DWARF2_ADDR_SIZE
14739 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14740 shift *= BITS_PER_UNIT;
14741 if (GET_CODE (rtl) == SIGN_EXTEND)
14742 op = DW_OP_shra;
14743 else
14744 op = DW_OP_shr;
14745 mem_loc_result = op0;
14746 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14747 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14748 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14749 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14751 else if (!dwarf_strict || dwarf_version >= 5)
14753 dw_die_ref type_die1, type_die2;
14754 dw_loc_descr_ref cvt;
14756 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14757 GET_CODE (rtl) == ZERO_EXTEND);
14758 if (type_die1 == NULL)
14759 break;
14760 type_die2 = base_type_for_mode (mode, 1);
14761 if (type_die2 == NULL)
14762 break;
14763 mem_loc_result = op0;
14764 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14765 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14766 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14767 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14768 add_loc_descr (&mem_loc_result, cvt);
14769 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14770 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14771 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14772 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14773 add_loc_descr (&mem_loc_result, cvt);
14775 break;
14777 case MEM:
14779 rtx new_rtl = avoid_constant_pool_reference (rtl);
14780 if (new_rtl != rtl)
14782 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14783 initialized);
14784 if (mem_loc_result != NULL)
14785 return mem_loc_result;
14788 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14789 get_address_mode (rtl), mode,
14790 VAR_INIT_STATUS_INITIALIZED);
14791 if (mem_loc_result == NULL)
14792 mem_loc_result = tls_mem_loc_descriptor (rtl);
14793 if (mem_loc_result != NULL)
14795 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14796 || !SCALAR_INT_MODE_P(mode))
14798 dw_die_ref type_die;
14799 dw_loc_descr_ref deref;
14801 if (dwarf_strict && dwarf_version < 5)
14802 return NULL;
14803 type_die
14804 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14805 if (type_die == NULL)
14806 return NULL;
14807 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14808 GET_MODE_SIZE (mode), 0);
14809 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14810 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14811 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14812 add_loc_descr (&mem_loc_result, deref);
14814 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14815 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14816 else
14817 add_loc_descr (&mem_loc_result,
14818 new_loc_descr (DW_OP_deref_size,
14819 GET_MODE_SIZE (mode), 0));
14821 break;
14823 case LO_SUM:
14824 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14826 case LABEL_REF:
14827 /* Some ports can transform a symbol ref into a label ref, because
14828 the symbol ref is too far away and has to be dumped into a constant
14829 pool. */
14830 case CONST:
14831 case SYMBOL_REF:
14832 if (!SCALAR_INT_MODE_P (mode)
14833 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14834 #ifdef POINTERS_EXTEND_UNSIGNED
14835 && (mode != Pmode || mem_mode == VOIDmode)
14836 #endif
14838 break;
14839 if (GET_CODE (rtl) == SYMBOL_REF
14840 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14842 dw_loc_descr_ref temp;
14844 /* If this is not defined, we have no way to emit the data. */
14845 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14846 break;
14848 temp = new_addr_loc_descr (rtl, dtprel_true);
14850 /* We check for DWARF 5 here because gdb did not implement
14851 DW_OP_form_tls_address until after 7.12. */
14852 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14853 ? DW_OP_form_tls_address
14854 : DW_OP_GNU_push_tls_address),
14855 0, 0);
14856 add_loc_descr (&mem_loc_result, temp);
14858 break;
14861 if (!const_ok_for_output (rtl))
14863 if (GET_CODE (rtl) == CONST)
14864 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14865 initialized);
14866 break;
14869 symref:
14870 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14871 vec_safe_push (used_rtx_array, rtl);
14872 break;
14874 case CONCAT:
14875 case CONCATN:
14876 case VAR_LOCATION:
14877 case DEBUG_IMPLICIT_PTR:
14878 expansion_failed (NULL_TREE, rtl,
14879 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14880 return 0;
14882 case ENTRY_VALUE:
14883 if (dwarf_strict && dwarf_version < 5)
14884 return NULL;
14885 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14887 if (!SCALAR_INT_MODE_P (mode)
14888 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14889 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14890 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14891 else
14893 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14894 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14895 return NULL;
14896 op0 = one_reg_loc_descriptor (dbx_regnum,
14897 VAR_INIT_STATUS_INITIALIZED);
14900 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14901 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14903 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14904 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14905 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14906 return NULL;
14908 else
14909 gcc_unreachable ();
14910 if (op0 == NULL)
14911 return NULL;
14912 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14913 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14914 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14915 break;
14917 case DEBUG_PARAMETER_REF:
14918 mem_loc_result = parameter_ref_descriptor (rtl);
14919 break;
14921 case PRE_MODIFY:
14922 /* Extract the PLUS expression nested inside and fall into
14923 PLUS code below. */
14924 rtl = XEXP (rtl, 1);
14925 goto plus;
14927 case PRE_INC:
14928 case PRE_DEC:
14929 /* Turn these into a PLUS expression and fall into the PLUS code
14930 below. */
14931 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14932 gen_int_mode (GET_CODE (rtl) == PRE_INC
14933 ? GET_MODE_UNIT_SIZE (mem_mode)
14934 : -GET_MODE_UNIT_SIZE (mem_mode),
14935 mode));
14937 /* fall through */
14939 case PLUS:
14940 plus:
14941 if (is_based_loc (rtl)
14942 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14943 || XEXP (rtl, 0) == arg_pointer_rtx
14944 || XEXP (rtl, 0) == frame_pointer_rtx)
14945 && SCALAR_INT_MODE_P (mode))
14946 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14947 INTVAL (XEXP (rtl, 1)),
14948 VAR_INIT_STATUS_INITIALIZED);
14949 else
14951 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14952 VAR_INIT_STATUS_INITIALIZED);
14953 if (mem_loc_result == 0)
14954 break;
14956 if (CONST_INT_P (XEXP (rtl, 1))
14957 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14958 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14959 else
14961 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14962 VAR_INIT_STATUS_INITIALIZED);
14963 if (op1 == 0)
14964 return NULL;
14965 add_loc_descr (&mem_loc_result, op1);
14966 add_loc_descr (&mem_loc_result,
14967 new_loc_descr (DW_OP_plus, 0, 0));
14970 break;
14972 /* If a pseudo-reg is optimized away, it is possible for it to
14973 be replaced with a MEM containing a multiply or shift. */
14974 case MINUS:
14975 op = DW_OP_minus;
14976 goto do_binop;
14978 case MULT:
14979 op = DW_OP_mul;
14980 goto do_binop;
14982 case DIV:
14983 if ((!dwarf_strict || dwarf_version >= 5)
14984 && SCALAR_INT_MODE_P (mode)
14985 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14987 mem_loc_result = typed_binop (DW_OP_div, rtl,
14988 base_type_for_mode (mode, 0),
14989 mode, mem_mode);
14990 break;
14992 op = DW_OP_div;
14993 goto do_binop;
14995 case UMOD:
14996 op = DW_OP_mod;
14997 goto do_binop;
14999 case ASHIFT:
15000 op = DW_OP_shl;
15001 goto do_shift;
15003 case ASHIFTRT:
15004 op = DW_OP_shra;
15005 goto do_shift;
15007 case LSHIFTRT:
15008 op = DW_OP_shr;
15009 goto do_shift;
15011 do_shift:
15012 if (!SCALAR_INT_MODE_P (mode))
15013 break;
15014 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15015 VAR_INIT_STATUS_INITIALIZED);
15017 rtx rtlop1 = XEXP (rtl, 1);
15018 if (GET_MODE (rtlop1) != VOIDmode
15019 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15020 < GET_MODE_BITSIZE (mode))
15021 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15022 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15023 VAR_INIT_STATUS_INITIALIZED);
15026 if (op0 == 0 || op1 == 0)
15027 break;
15029 mem_loc_result = op0;
15030 add_loc_descr (&mem_loc_result, op1);
15031 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15032 break;
15034 case AND:
15035 op = DW_OP_and;
15036 goto do_binop;
15038 case IOR:
15039 op = DW_OP_or;
15040 goto do_binop;
15042 case XOR:
15043 op = DW_OP_xor;
15044 goto do_binop;
15046 do_binop:
15047 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15048 VAR_INIT_STATUS_INITIALIZED);
15049 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15050 VAR_INIT_STATUS_INITIALIZED);
15052 if (op0 == 0 || op1 == 0)
15053 break;
15055 mem_loc_result = op0;
15056 add_loc_descr (&mem_loc_result, op1);
15057 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15058 break;
15060 case MOD:
15061 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15062 && (!dwarf_strict || dwarf_version >= 5))
15064 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15065 base_type_for_mode (mode, 0),
15066 mode, mem_mode);
15067 break;
15070 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15071 VAR_INIT_STATUS_INITIALIZED);
15072 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15073 VAR_INIT_STATUS_INITIALIZED);
15075 if (op0 == 0 || op1 == 0)
15076 break;
15078 mem_loc_result = op0;
15079 add_loc_descr (&mem_loc_result, op1);
15080 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15081 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15082 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15083 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15084 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15085 break;
15087 case UDIV:
15088 if ((!dwarf_strict || dwarf_version >= 5)
15089 && SCALAR_INT_MODE_P (mode))
15091 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15093 op = DW_OP_div;
15094 goto do_binop;
15096 mem_loc_result = typed_binop (DW_OP_div, rtl,
15097 base_type_for_mode (mode, 1),
15098 mode, mem_mode);
15100 break;
15102 case NOT:
15103 op = DW_OP_not;
15104 goto do_unop;
15106 case ABS:
15107 op = DW_OP_abs;
15108 goto do_unop;
15110 case NEG:
15111 op = DW_OP_neg;
15112 goto do_unop;
15114 do_unop:
15115 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15116 VAR_INIT_STATUS_INITIALIZED);
15118 if (op0 == 0)
15119 break;
15121 mem_loc_result = op0;
15122 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15123 break;
15125 case CONST_INT:
15126 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15127 #ifdef POINTERS_EXTEND_UNSIGNED
15128 || (mode == Pmode
15129 && mem_mode != VOIDmode
15130 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15131 #endif
15134 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15135 break;
15137 if ((!dwarf_strict || dwarf_version >= 5)
15138 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15139 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15141 dw_die_ref type_die = base_type_for_mode (mode, 1);
15142 machine_mode amode;
15143 if (type_die == NULL)
15144 return NULL;
15145 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15146 MODE_INT, 0);
15147 if (INTVAL (rtl) >= 0
15148 && amode != BLKmode
15149 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15150 /* const DW_OP_convert <XXX> vs.
15151 DW_OP_const_type <XXX, 1, const>. */
15152 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15153 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15155 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15156 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15157 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15158 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15159 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15160 add_loc_descr (&mem_loc_result, op0);
15161 return mem_loc_result;
15163 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15164 INTVAL (rtl));
15165 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15166 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15167 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15168 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15169 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15170 else
15172 mem_loc_result->dw_loc_oprnd2.val_class
15173 = dw_val_class_const_double;
15174 mem_loc_result->dw_loc_oprnd2.v.val_double
15175 = double_int::from_shwi (INTVAL (rtl));
15178 break;
15180 case CONST_DOUBLE:
15181 if (!dwarf_strict || dwarf_version >= 5)
15183 dw_die_ref type_die;
15185 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15186 CONST_DOUBLE rtx could represent either a large integer
15187 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15188 the value is always a floating point constant.
15190 When it is an integer, a CONST_DOUBLE is used whenever
15191 the constant requires 2 HWIs to be adequately represented.
15192 We output CONST_DOUBLEs as blocks. */
15193 if (mode == VOIDmode
15194 || (GET_MODE (rtl) == VOIDmode
15195 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15196 break;
15197 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15198 if (type_die == NULL)
15199 return NULL;
15200 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15201 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15202 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15203 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15204 #if TARGET_SUPPORTS_WIDE_INT == 0
15205 if (!SCALAR_FLOAT_MODE_P (mode))
15207 mem_loc_result->dw_loc_oprnd2.val_class
15208 = dw_val_class_const_double;
15209 mem_loc_result->dw_loc_oprnd2.v.val_double
15210 = rtx_to_double_int (rtl);
15212 else
15213 #endif
15215 unsigned int length = GET_MODE_SIZE (mode);
15216 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15218 insert_float (rtl, array);
15219 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15220 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15221 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15222 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15225 break;
15227 case CONST_WIDE_INT:
15228 if (!dwarf_strict || dwarf_version >= 5)
15230 dw_die_ref type_die;
15232 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15233 if (type_die == NULL)
15234 return NULL;
15235 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15236 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15237 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15238 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15239 mem_loc_result->dw_loc_oprnd2.val_class
15240 = dw_val_class_wide_int;
15241 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15242 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15244 break;
15246 case EQ:
15247 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15248 break;
15250 case GE:
15251 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15252 break;
15254 case GT:
15255 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15256 break;
15258 case LE:
15259 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15260 break;
15262 case LT:
15263 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15264 break;
15266 case NE:
15267 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15268 break;
15270 case GEU:
15271 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15272 break;
15274 case GTU:
15275 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15276 break;
15278 case LEU:
15279 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15280 break;
15282 case LTU:
15283 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15284 break;
15286 case UMIN:
15287 case UMAX:
15288 if (!SCALAR_INT_MODE_P (mode))
15289 break;
15290 /* FALLTHRU */
15291 case SMIN:
15292 case SMAX:
15293 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15294 break;
15296 case ZERO_EXTRACT:
15297 case SIGN_EXTRACT:
15298 if (CONST_INT_P (XEXP (rtl, 1))
15299 && CONST_INT_P (XEXP (rtl, 2))
15300 && ((unsigned) INTVAL (XEXP (rtl, 1))
15301 + (unsigned) INTVAL (XEXP (rtl, 2))
15302 <= GET_MODE_BITSIZE (mode))
15303 && SCALAR_INT_MODE_P (mode)
15304 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15305 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15307 int shift, size;
15308 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15309 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15310 if (op0 == 0)
15311 break;
15312 if (GET_CODE (rtl) == SIGN_EXTRACT)
15313 op = DW_OP_shra;
15314 else
15315 op = DW_OP_shr;
15316 mem_loc_result = op0;
15317 size = INTVAL (XEXP (rtl, 1));
15318 shift = INTVAL (XEXP (rtl, 2));
15319 if (BITS_BIG_ENDIAN)
15320 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15321 - shift - size;
15322 if (shift + size != (int) DWARF2_ADDR_SIZE)
15324 add_loc_descr (&mem_loc_result,
15325 int_loc_descriptor (DWARF2_ADDR_SIZE
15326 - shift - size));
15327 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15329 if (size != (int) DWARF2_ADDR_SIZE)
15331 add_loc_descr (&mem_loc_result,
15332 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15333 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15336 break;
15338 case IF_THEN_ELSE:
15340 dw_loc_descr_ref op2, bra_node, drop_node;
15341 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15342 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15343 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15344 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15345 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15346 VAR_INIT_STATUS_INITIALIZED);
15347 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15348 VAR_INIT_STATUS_INITIALIZED);
15349 if (op0 == NULL || op1 == NULL || op2 == NULL)
15350 break;
15352 mem_loc_result = op1;
15353 add_loc_descr (&mem_loc_result, op2);
15354 add_loc_descr (&mem_loc_result, op0);
15355 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15356 add_loc_descr (&mem_loc_result, bra_node);
15357 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15358 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15359 add_loc_descr (&mem_loc_result, drop_node);
15360 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15361 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15363 break;
15365 case FLOAT_EXTEND:
15366 case FLOAT_TRUNCATE:
15367 case FLOAT:
15368 case UNSIGNED_FLOAT:
15369 case FIX:
15370 case UNSIGNED_FIX:
15371 if (!dwarf_strict || dwarf_version >= 5)
15373 dw_die_ref type_die;
15374 dw_loc_descr_ref cvt;
15376 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15377 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15378 if (op0 == NULL)
15379 break;
15380 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15381 && (GET_CODE (rtl) == FLOAT
15382 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15383 <= DWARF2_ADDR_SIZE))
15385 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15386 GET_CODE (rtl) == UNSIGNED_FLOAT);
15387 if (type_die == NULL)
15388 break;
15389 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15390 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15391 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15392 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15393 add_loc_descr (&op0, cvt);
15395 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15396 if (type_die == NULL)
15397 break;
15398 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15399 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15400 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15401 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15402 add_loc_descr (&op0, cvt);
15403 if (SCALAR_INT_MODE_P (mode)
15404 && (GET_CODE (rtl) == FIX
15405 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15407 op0 = convert_descriptor_to_mode (mode, op0);
15408 if (op0 == NULL)
15409 break;
15411 mem_loc_result = op0;
15413 break;
15415 case CLZ:
15416 case CTZ:
15417 case FFS:
15418 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15419 break;
15421 case POPCOUNT:
15422 case PARITY:
15423 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15424 break;
15426 case BSWAP:
15427 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15428 break;
15430 case ROTATE:
15431 case ROTATERT:
15432 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15433 break;
15435 case COMPARE:
15436 /* In theory, we could implement the above. */
15437 /* DWARF cannot represent the unsigned compare operations
15438 natively. */
15439 case SS_MULT:
15440 case US_MULT:
15441 case SS_DIV:
15442 case US_DIV:
15443 case SS_PLUS:
15444 case US_PLUS:
15445 case SS_MINUS:
15446 case US_MINUS:
15447 case SS_NEG:
15448 case US_NEG:
15449 case SS_ABS:
15450 case SS_ASHIFT:
15451 case US_ASHIFT:
15452 case SS_TRUNCATE:
15453 case US_TRUNCATE:
15454 case UNORDERED:
15455 case ORDERED:
15456 case UNEQ:
15457 case UNGE:
15458 case UNGT:
15459 case UNLE:
15460 case UNLT:
15461 case LTGT:
15462 case FRACT_CONVERT:
15463 case UNSIGNED_FRACT_CONVERT:
15464 case SAT_FRACT:
15465 case UNSIGNED_SAT_FRACT:
15466 case SQRT:
15467 case ASM_OPERANDS:
15468 case VEC_MERGE:
15469 case VEC_SELECT:
15470 case VEC_CONCAT:
15471 case VEC_DUPLICATE:
15472 case UNSPEC:
15473 case HIGH:
15474 case FMA:
15475 case STRICT_LOW_PART:
15476 case CONST_VECTOR:
15477 case CONST_FIXED:
15478 case CLRSB:
15479 case CLOBBER:
15480 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15481 can't express it in the debug info. This can happen e.g. with some
15482 TLS UNSPECs. */
15483 break;
15485 case CONST_STRING:
15486 resolve_one_addr (&rtl);
15487 goto symref;
15489 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15490 the expression. An UNSPEC rtx represents a raw DWARF operation,
15491 new_loc_descr is called for it to build the operation directly.
15492 Otherwise mem_loc_descriptor is called recursively. */
15493 case PARALLEL:
15495 int index = 0;
15496 dw_loc_descr_ref exp_result = NULL;
15498 for (; index < XVECLEN (rtl, 0); index++)
15500 rtx elem = XVECEXP (rtl, 0, index);
15501 if (GET_CODE (elem) == UNSPEC)
15503 /* Each DWARF operation UNSPEC contain two operands, if
15504 one operand is not used for the operation, const0_rtx is
15505 passed. */
15506 gcc_assert (XVECLEN (elem, 0) == 2);
15508 HOST_WIDE_INT dw_op = XINT (elem, 1);
15509 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15510 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15511 exp_result
15512 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15513 oprnd2);
15515 else
15516 exp_result
15517 = mem_loc_descriptor (elem, mode, mem_mode,
15518 VAR_INIT_STATUS_INITIALIZED);
15520 if (!mem_loc_result)
15521 mem_loc_result = exp_result;
15522 else
15523 add_loc_descr (&mem_loc_result, exp_result);
15526 break;
15529 default:
15530 if (flag_checking)
15532 print_rtl (stderr, rtl);
15533 gcc_unreachable ();
15535 break;
15538 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15539 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15541 return mem_loc_result;
15544 /* Return a descriptor that describes the concatenation of two locations.
15545 This is typically a complex variable. */
15547 static dw_loc_descr_ref
15548 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15550 dw_loc_descr_ref cc_loc_result = NULL;
15551 dw_loc_descr_ref x0_ref
15552 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15553 dw_loc_descr_ref x1_ref
15554 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15556 if (x0_ref == 0 || x1_ref == 0)
15557 return 0;
15559 cc_loc_result = x0_ref;
15560 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15562 add_loc_descr (&cc_loc_result, x1_ref);
15563 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15565 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15566 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15568 return cc_loc_result;
15571 /* Return a descriptor that describes the concatenation of N
15572 locations. */
15574 static dw_loc_descr_ref
15575 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15577 unsigned int i;
15578 dw_loc_descr_ref cc_loc_result = NULL;
15579 unsigned int n = XVECLEN (concatn, 0);
15581 for (i = 0; i < n; ++i)
15583 dw_loc_descr_ref ref;
15584 rtx x = XVECEXP (concatn, 0, i);
15586 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15587 if (ref == NULL)
15588 return NULL;
15590 add_loc_descr (&cc_loc_result, ref);
15591 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15594 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15595 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15597 return cc_loc_result;
15600 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15601 for DEBUG_IMPLICIT_PTR RTL. */
15603 static dw_loc_descr_ref
15604 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15606 dw_loc_descr_ref ret;
15607 dw_die_ref ref;
15609 if (dwarf_strict && dwarf_version < 5)
15610 return NULL;
15611 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15612 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15613 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15614 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15615 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15616 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15617 if (ref)
15619 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15620 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15621 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15623 else
15625 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15626 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15628 return ret;
15631 /* Output a proper Dwarf location descriptor for a variable or parameter
15632 which is either allocated in a register or in a memory location. For a
15633 register, we just generate an OP_REG and the register number. For a
15634 memory location we provide a Dwarf postfix expression describing how to
15635 generate the (dynamic) address of the object onto the address stack.
15637 MODE is mode of the decl if this loc_descriptor is going to be used in
15638 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15639 allowed, VOIDmode otherwise.
15641 If we don't know how to describe it, return 0. */
15643 static dw_loc_descr_ref
15644 loc_descriptor (rtx rtl, machine_mode mode,
15645 enum var_init_status initialized)
15647 dw_loc_descr_ref loc_result = NULL;
15649 switch (GET_CODE (rtl))
15651 case SUBREG:
15652 /* The case of a subreg may arise when we have a local (register)
15653 variable or a formal (register) parameter which doesn't quite fill
15654 up an entire register. For now, just assume that it is
15655 legitimate to make the Dwarf info refer to the whole register which
15656 contains the given subreg. */
15657 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15658 loc_result = loc_descriptor (SUBREG_REG (rtl),
15659 GET_MODE (SUBREG_REG (rtl)), initialized);
15660 else
15661 goto do_default;
15662 break;
15664 case REG:
15665 loc_result = reg_loc_descriptor (rtl, initialized);
15666 break;
15668 case MEM:
15669 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15670 GET_MODE (rtl), initialized);
15671 if (loc_result == NULL)
15672 loc_result = tls_mem_loc_descriptor (rtl);
15673 if (loc_result == NULL)
15675 rtx new_rtl = avoid_constant_pool_reference (rtl);
15676 if (new_rtl != rtl)
15677 loc_result = loc_descriptor (new_rtl, mode, initialized);
15679 break;
15681 case CONCAT:
15682 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15683 initialized);
15684 break;
15686 case CONCATN:
15687 loc_result = concatn_loc_descriptor (rtl, initialized);
15688 break;
15690 case VAR_LOCATION:
15691 /* Single part. */
15692 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15694 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15695 if (GET_CODE (loc) == EXPR_LIST)
15696 loc = XEXP (loc, 0);
15697 loc_result = loc_descriptor (loc, mode, initialized);
15698 break;
15701 rtl = XEXP (rtl, 1);
15702 /* FALLTHRU */
15704 case PARALLEL:
15706 rtvec par_elems = XVEC (rtl, 0);
15707 int num_elem = GET_NUM_ELEM (par_elems);
15708 machine_mode mode;
15709 int i;
15711 /* Create the first one, so we have something to add to. */
15712 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15713 VOIDmode, initialized);
15714 if (loc_result == NULL)
15715 return NULL;
15716 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15717 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15718 for (i = 1; i < num_elem; i++)
15720 dw_loc_descr_ref temp;
15722 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15723 VOIDmode, initialized);
15724 if (temp == NULL)
15725 return NULL;
15726 add_loc_descr (&loc_result, temp);
15727 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15728 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15731 break;
15733 case CONST_INT:
15734 if (mode != VOIDmode && mode != BLKmode)
15735 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15736 INTVAL (rtl));
15737 break;
15739 case CONST_DOUBLE:
15740 if (mode == VOIDmode)
15741 mode = GET_MODE (rtl);
15743 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15745 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15747 /* Note that a CONST_DOUBLE rtx could represent either an integer
15748 or a floating-point constant. A CONST_DOUBLE is used whenever
15749 the constant requires more than one word in order to be
15750 adequately represented. We output CONST_DOUBLEs as blocks. */
15751 loc_result = new_loc_descr (DW_OP_implicit_value,
15752 GET_MODE_SIZE (mode), 0);
15753 #if TARGET_SUPPORTS_WIDE_INT == 0
15754 if (!SCALAR_FLOAT_MODE_P (mode))
15756 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15757 loc_result->dw_loc_oprnd2.v.val_double
15758 = rtx_to_double_int (rtl);
15760 else
15761 #endif
15763 unsigned int length = GET_MODE_SIZE (mode);
15764 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15766 insert_float (rtl, array);
15767 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15768 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15769 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15770 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15773 break;
15775 case CONST_WIDE_INT:
15776 if (mode == VOIDmode)
15777 mode = GET_MODE (rtl);
15779 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15781 loc_result = new_loc_descr (DW_OP_implicit_value,
15782 GET_MODE_SIZE (mode), 0);
15783 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15784 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15785 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15787 break;
15789 case CONST_VECTOR:
15790 if (mode == VOIDmode)
15791 mode = GET_MODE (rtl);
15793 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15795 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15796 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15797 unsigned char *array
15798 = ggc_vec_alloc<unsigned char> (length * elt_size);
15799 unsigned int i;
15800 unsigned char *p;
15801 machine_mode imode = GET_MODE_INNER (mode);
15803 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15804 switch (GET_MODE_CLASS (mode))
15806 case MODE_VECTOR_INT:
15807 for (i = 0, p = array; i < length; i++, p += elt_size)
15809 rtx elt = CONST_VECTOR_ELT (rtl, i);
15810 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15812 break;
15814 case MODE_VECTOR_FLOAT:
15815 for (i = 0, p = array; i < length; i++, p += elt_size)
15817 rtx elt = CONST_VECTOR_ELT (rtl, i);
15818 insert_float (elt, p);
15820 break;
15822 default:
15823 gcc_unreachable ();
15826 loc_result = new_loc_descr (DW_OP_implicit_value,
15827 length * elt_size, 0);
15828 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15829 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15830 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15831 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15833 break;
15835 case CONST:
15836 if (mode == VOIDmode
15837 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15838 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15839 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15841 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15842 break;
15844 /* FALLTHROUGH */
15845 case SYMBOL_REF:
15846 if (!const_ok_for_output (rtl))
15847 break;
15848 /* FALLTHROUGH */
15849 case LABEL_REF:
15850 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15851 && (dwarf_version >= 4 || !dwarf_strict))
15853 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15854 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15855 vec_safe_push (used_rtx_array, rtl);
15857 break;
15859 case DEBUG_IMPLICIT_PTR:
15860 loc_result = implicit_ptr_descriptor (rtl, 0);
15861 break;
15863 case PLUS:
15864 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15865 && CONST_INT_P (XEXP (rtl, 1)))
15867 loc_result
15868 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15869 break;
15871 /* FALLTHRU */
15872 do_default:
15873 default:
15874 if ((SCALAR_INT_MODE_P (mode)
15875 && GET_MODE (rtl) == mode
15876 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15877 && dwarf_version >= 4)
15878 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15880 /* Value expression. */
15881 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15882 if (loc_result)
15883 add_loc_descr (&loc_result,
15884 new_loc_descr (DW_OP_stack_value, 0, 0));
15886 break;
15889 return loc_result;
15892 /* We need to figure out what section we should use as the base for the
15893 address ranges where a given location is valid.
15894 1. If this particular DECL has a section associated with it, use that.
15895 2. If this function has a section associated with it, use that.
15896 3. Otherwise, use the text section.
15897 XXX: If you split a variable across multiple sections, we won't notice. */
15899 static const char *
15900 secname_for_decl (const_tree decl)
15902 const char *secname;
15904 if (VAR_OR_FUNCTION_DECL_P (decl)
15905 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15906 && DECL_SECTION_NAME (decl))
15907 secname = DECL_SECTION_NAME (decl);
15908 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15909 secname = DECL_SECTION_NAME (current_function_decl);
15910 else if (cfun && in_cold_section_p)
15911 secname = crtl->subsections.cold_section_label;
15912 else
15913 secname = text_section_label;
15915 return secname;
15918 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15920 static bool
15921 decl_by_reference_p (tree decl)
15923 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15924 || VAR_P (decl))
15925 && DECL_BY_REFERENCE (decl));
15928 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15929 for VARLOC. */
15931 static dw_loc_descr_ref
15932 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15933 enum var_init_status initialized)
15935 int have_address = 0;
15936 dw_loc_descr_ref descr;
15937 machine_mode mode;
15939 if (want_address != 2)
15941 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15942 /* Single part. */
15943 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15945 varloc = PAT_VAR_LOCATION_LOC (varloc);
15946 if (GET_CODE (varloc) == EXPR_LIST)
15947 varloc = XEXP (varloc, 0);
15948 mode = GET_MODE (varloc);
15949 if (MEM_P (varloc))
15951 rtx addr = XEXP (varloc, 0);
15952 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15953 mode, initialized);
15954 if (descr)
15955 have_address = 1;
15956 else
15958 rtx x = avoid_constant_pool_reference (varloc);
15959 if (x != varloc)
15960 descr = mem_loc_descriptor (x, mode, VOIDmode,
15961 initialized);
15964 else
15965 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15967 else
15968 return 0;
15970 else
15972 if (GET_CODE (varloc) == VAR_LOCATION)
15973 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15974 else
15975 mode = DECL_MODE (loc);
15976 descr = loc_descriptor (varloc, mode, initialized);
15977 have_address = 1;
15980 if (!descr)
15981 return 0;
15983 if (want_address == 2 && !have_address
15984 && (dwarf_version >= 4 || !dwarf_strict))
15986 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15988 expansion_failed (loc, NULL_RTX,
15989 "DWARF address size mismatch");
15990 return 0;
15992 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15993 have_address = 1;
15995 /* Show if we can't fill the request for an address. */
15996 if (want_address && !have_address)
15998 expansion_failed (loc, NULL_RTX,
15999 "Want address and only have value");
16000 return 0;
16003 /* If we've got an address and don't want one, dereference. */
16004 if (!want_address && have_address)
16006 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16007 enum dwarf_location_atom op;
16009 if (size > DWARF2_ADDR_SIZE || size == -1)
16011 expansion_failed (loc, NULL_RTX,
16012 "DWARF address size mismatch");
16013 return 0;
16015 else if (size == DWARF2_ADDR_SIZE)
16016 op = DW_OP_deref;
16017 else
16018 op = DW_OP_deref_size;
16020 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16023 return descr;
16026 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16027 if it is not possible. */
16029 static dw_loc_descr_ref
16030 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16032 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16033 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16034 else if (dwarf_version >= 3 || !dwarf_strict)
16035 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16036 else
16037 return NULL;
16040 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16041 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16043 static dw_loc_descr_ref
16044 dw_sra_loc_expr (tree decl, rtx loc)
16046 rtx p;
16047 unsigned HOST_WIDE_INT padsize = 0;
16048 dw_loc_descr_ref descr, *descr_tail;
16049 unsigned HOST_WIDE_INT decl_size;
16050 rtx varloc;
16051 enum var_init_status initialized;
16053 if (DECL_SIZE (decl) == NULL
16054 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16055 return NULL;
16057 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16058 descr = NULL;
16059 descr_tail = &descr;
16061 for (p = loc; p; p = XEXP (p, 1))
16063 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16064 rtx loc_note = *decl_piece_varloc_ptr (p);
16065 dw_loc_descr_ref cur_descr;
16066 dw_loc_descr_ref *tail, last = NULL;
16067 unsigned HOST_WIDE_INT opsize = 0;
16069 if (loc_note == NULL_RTX
16070 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16072 padsize += bitsize;
16073 continue;
16075 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16076 varloc = NOTE_VAR_LOCATION (loc_note);
16077 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16078 if (cur_descr == NULL)
16080 padsize += bitsize;
16081 continue;
16084 /* Check that cur_descr either doesn't use
16085 DW_OP_*piece operations, or their sum is equal
16086 to bitsize. Otherwise we can't embed it. */
16087 for (tail = &cur_descr; *tail != NULL;
16088 tail = &(*tail)->dw_loc_next)
16089 if ((*tail)->dw_loc_opc == DW_OP_piece)
16091 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16092 * BITS_PER_UNIT;
16093 last = *tail;
16095 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16097 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16098 last = *tail;
16101 if (last != NULL && opsize != bitsize)
16103 padsize += bitsize;
16104 /* Discard the current piece of the descriptor and release any
16105 addr_table entries it uses. */
16106 remove_loc_list_addr_table_entries (cur_descr);
16107 continue;
16110 /* If there is a hole, add DW_OP_*piece after empty DWARF
16111 expression, which means that those bits are optimized out. */
16112 if (padsize)
16114 if (padsize > decl_size)
16116 remove_loc_list_addr_table_entries (cur_descr);
16117 goto discard_descr;
16119 decl_size -= padsize;
16120 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16121 if (*descr_tail == NULL)
16123 remove_loc_list_addr_table_entries (cur_descr);
16124 goto discard_descr;
16126 descr_tail = &(*descr_tail)->dw_loc_next;
16127 padsize = 0;
16129 *descr_tail = cur_descr;
16130 descr_tail = tail;
16131 if (bitsize > decl_size)
16132 goto discard_descr;
16133 decl_size -= bitsize;
16134 if (last == NULL)
16136 HOST_WIDE_INT offset = 0;
16137 if (GET_CODE (varloc) == VAR_LOCATION
16138 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16140 varloc = PAT_VAR_LOCATION_LOC (varloc);
16141 if (GET_CODE (varloc) == EXPR_LIST)
16142 varloc = XEXP (varloc, 0);
16146 if (GET_CODE (varloc) == CONST
16147 || GET_CODE (varloc) == SIGN_EXTEND
16148 || GET_CODE (varloc) == ZERO_EXTEND)
16149 varloc = XEXP (varloc, 0);
16150 else if (GET_CODE (varloc) == SUBREG)
16151 varloc = SUBREG_REG (varloc);
16152 else
16153 break;
16155 while (1);
16156 /* DW_OP_bit_size offset should be zero for register
16157 or implicit location descriptions and empty location
16158 descriptions, but for memory addresses needs big endian
16159 adjustment. */
16160 if (MEM_P (varloc))
16162 unsigned HOST_WIDE_INT memsize
16163 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16164 if (memsize != bitsize)
16166 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16167 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16168 goto discard_descr;
16169 if (memsize < bitsize)
16170 goto discard_descr;
16171 if (BITS_BIG_ENDIAN)
16172 offset = memsize - bitsize;
16176 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16177 if (*descr_tail == NULL)
16178 goto discard_descr;
16179 descr_tail = &(*descr_tail)->dw_loc_next;
16183 /* If there were any non-empty expressions, add padding till the end of
16184 the decl. */
16185 if (descr != NULL && decl_size != 0)
16187 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16188 if (*descr_tail == NULL)
16189 goto discard_descr;
16191 return descr;
16193 discard_descr:
16194 /* Discard the descriptor and release any addr_table entries it uses. */
16195 remove_loc_list_addr_table_entries (descr);
16196 return NULL;
16199 /* Return the dwarf representation of the location list LOC_LIST of
16200 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16201 function. */
16203 static dw_loc_list_ref
16204 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16206 const char *endname, *secname;
16207 rtx varloc;
16208 enum var_init_status initialized;
16209 struct var_loc_node *node;
16210 dw_loc_descr_ref descr;
16211 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16212 dw_loc_list_ref list = NULL;
16213 dw_loc_list_ref *listp = &list;
16215 /* Now that we know what section we are using for a base,
16216 actually construct the list of locations.
16217 The first location information is what is passed to the
16218 function that creates the location list, and the remaining
16219 locations just get added on to that list.
16220 Note that we only know the start address for a location
16221 (IE location changes), so to build the range, we use
16222 the range [current location start, next location start].
16223 This means we have to special case the last node, and generate
16224 a range of [last location start, end of function label]. */
16226 secname = secname_for_decl (decl);
16228 for (node = loc_list->first; node; node = node->next)
16229 if (GET_CODE (node->loc) == EXPR_LIST
16230 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16232 if (GET_CODE (node->loc) == EXPR_LIST)
16234 /* This requires DW_OP_{,bit_}piece, which is not usable
16235 inside DWARF expressions. */
16236 if (want_address != 2)
16237 continue;
16238 descr = dw_sra_loc_expr (decl, node->loc);
16239 if (descr == NULL)
16240 continue;
16242 else
16244 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16245 varloc = NOTE_VAR_LOCATION (node->loc);
16246 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16248 if (descr)
16250 bool range_across_switch = false;
16251 /* If section switch happens in between node->label
16252 and node->next->label (or end of function) and
16253 we can't emit it as a single entry list,
16254 emit two ranges, first one ending at the end
16255 of first partition and second one starting at the
16256 beginning of second partition. */
16257 if (node == loc_list->last_before_switch
16258 && (node != loc_list->first || loc_list->first->next)
16259 && current_function_decl)
16261 endname = cfun->fde->dw_fde_end;
16262 range_across_switch = true;
16264 /* The variable has a location between NODE->LABEL and
16265 NODE->NEXT->LABEL. */
16266 else if (node->next)
16267 endname = node->next->label;
16268 /* If the variable has a location at the last label
16269 it keeps its location until the end of function. */
16270 else if (!current_function_decl)
16271 endname = text_end_label;
16272 else
16274 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16275 current_function_funcdef_no);
16276 endname = ggc_strdup (label_id);
16279 *listp = new_loc_list (descr, node->label, endname, secname);
16280 if (TREE_CODE (decl) == PARM_DECL
16281 && node == loc_list->first
16282 && NOTE_P (node->loc)
16283 && strcmp (node->label, endname) == 0)
16284 (*listp)->force = true;
16285 listp = &(*listp)->dw_loc_next;
16287 if (range_across_switch)
16289 if (GET_CODE (node->loc) == EXPR_LIST)
16290 descr = dw_sra_loc_expr (decl, node->loc);
16291 else
16293 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16294 varloc = NOTE_VAR_LOCATION (node->loc);
16295 descr = dw_loc_list_1 (decl, varloc, want_address,
16296 initialized);
16298 gcc_assert (descr);
16299 /* The variable has a location between NODE->LABEL and
16300 NODE->NEXT->LABEL. */
16301 if (node->next)
16302 endname = node->next->label;
16303 else
16304 endname = cfun->fde->dw_fde_second_end;
16305 *listp = new_loc_list (descr,
16306 cfun->fde->dw_fde_second_begin,
16307 endname, secname);
16308 listp = &(*listp)->dw_loc_next;
16313 /* Try to avoid the overhead of a location list emitting a location
16314 expression instead, but only if we didn't have more than one
16315 location entry in the first place. If some entries were not
16316 representable, we don't want to pretend a single entry that was
16317 applies to the entire scope in which the variable is
16318 available. */
16319 if (list && loc_list->first->next)
16320 gen_llsym (list);
16322 return list;
16325 /* Return if the loc_list has only single element and thus can be represented
16326 as location description. */
16328 static bool
16329 single_element_loc_list_p (dw_loc_list_ref list)
16331 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16332 return !list->ll_symbol;
16335 /* Duplicate a single element of location list. */
16337 static inline dw_loc_descr_ref
16338 copy_loc_descr (dw_loc_descr_ref ref)
16340 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16341 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16342 return copy;
16345 /* To each location in list LIST append loc descr REF. */
16347 static void
16348 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16350 dw_loc_descr_ref copy;
16351 add_loc_descr (&list->expr, ref);
16352 list = list->dw_loc_next;
16353 while (list)
16355 copy = copy_loc_descr (ref);
16356 add_loc_descr (&list->expr, copy);
16357 while (copy->dw_loc_next)
16358 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16359 list = list->dw_loc_next;
16363 /* To each location in list LIST prepend loc descr REF. */
16365 static void
16366 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16368 dw_loc_descr_ref copy;
16369 dw_loc_descr_ref ref_end = list->expr;
16370 add_loc_descr (&ref, list->expr);
16371 list->expr = ref;
16372 list = list->dw_loc_next;
16373 while (list)
16375 dw_loc_descr_ref end = list->expr;
16376 list->expr = copy = copy_loc_descr (ref);
16377 while (copy->dw_loc_next != ref_end)
16378 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16379 copy->dw_loc_next = end;
16380 list = list->dw_loc_next;
16384 /* Given two lists RET and LIST
16385 produce location list that is result of adding expression in LIST
16386 to expression in RET on each position in program.
16387 Might be destructive on both RET and LIST.
16389 TODO: We handle only simple cases of RET or LIST having at most one
16390 element. General case would involve sorting the lists in program order
16391 and merging them that will need some additional work.
16392 Adding that will improve quality of debug info especially for SRA-ed
16393 structures. */
16395 static void
16396 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16398 if (!list)
16399 return;
16400 if (!*ret)
16402 *ret = list;
16403 return;
16405 if (!list->dw_loc_next)
16407 add_loc_descr_to_each (*ret, list->expr);
16408 return;
16410 if (!(*ret)->dw_loc_next)
16412 prepend_loc_descr_to_each (list, (*ret)->expr);
16413 *ret = list;
16414 return;
16416 expansion_failed (NULL_TREE, NULL_RTX,
16417 "Don't know how to merge two non-trivial"
16418 " location lists.\n");
16419 *ret = NULL;
16420 return;
16423 /* LOC is constant expression. Try a luck, look it up in constant
16424 pool and return its loc_descr of its address. */
16426 static dw_loc_descr_ref
16427 cst_pool_loc_descr (tree loc)
16429 /* Get an RTL for this, if something has been emitted. */
16430 rtx rtl = lookup_constant_def (loc);
16432 if (!rtl || !MEM_P (rtl))
16434 gcc_assert (!rtl);
16435 return 0;
16437 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16439 /* TODO: We might get more coverage if we was actually delaying expansion
16440 of all expressions till end of compilation when constant pools are fully
16441 populated. */
16442 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16444 expansion_failed (loc, NULL_RTX,
16445 "CST value in contant pool but not marked.");
16446 return 0;
16448 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16449 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16452 /* Return dw_loc_list representing address of addr_expr LOC
16453 by looking for inner INDIRECT_REF expression and turning
16454 it into simple arithmetics.
16456 See loc_list_from_tree for the meaning of CONTEXT. */
16458 static dw_loc_list_ref
16459 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16460 loc_descr_context *context)
16462 tree obj, offset;
16463 HOST_WIDE_INT bitsize, bitpos, bytepos;
16464 machine_mode mode;
16465 int unsignedp, reversep, volatilep = 0;
16466 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16468 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16469 &bitsize, &bitpos, &offset, &mode,
16470 &unsignedp, &reversep, &volatilep);
16471 STRIP_NOPS (obj);
16472 if (bitpos % BITS_PER_UNIT)
16474 expansion_failed (loc, NULL_RTX, "bitfield access");
16475 return 0;
16477 if (!INDIRECT_REF_P (obj))
16479 expansion_failed (obj,
16480 NULL_RTX, "no indirect ref in inner refrence");
16481 return 0;
16483 if (!offset && !bitpos)
16484 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16485 context);
16486 else if (toplev
16487 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16488 && (dwarf_version >= 4 || !dwarf_strict))
16490 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16491 if (!list_ret)
16492 return 0;
16493 if (offset)
16495 /* Variable offset. */
16496 list_ret1 = loc_list_from_tree (offset, 0, context);
16497 if (list_ret1 == 0)
16498 return 0;
16499 add_loc_list (&list_ret, list_ret1);
16500 if (!list_ret)
16501 return 0;
16502 add_loc_descr_to_each (list_ret,
16503 new_loc_descr (DW_OP_plus, 0, 0));
16505 bytepos = bitpos / BITS_PER_UNIT;
16506 if (bytepos > 0)
16507 add_loc_descr_to_each (list_ret,
16508 new_loc_descr (DW_OP_plus_uconst,
16509 bytepos, 0));
16510 else if (bytepos < 0)
16511 loc_list_plus_const (list_ret, bytepos);
16512 add_loc_descr_to_each (list_ret,
16513 new_loc_descr (DW_OP_stack_value, 0, 0));
16515 return list_ret;
16518 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16519 all operations from LOC are nops, move to the last one. Insert in NOPS all
16520 operations that are skipped. */
16522 static void
16523 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16524 hash_set<dw_loc_descr_ref> &nops)
16526 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16528 nops.add (loc);
16529 loc = loc->dw_loc_next;
16533 /* Helper for loc_descr_without_nops: free the location description operation
16534 P. */
16536 bool
16537 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16539 ggc_free (loc);
16540 return true;
16543 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16544 finishes LOC. */
16546 static void
16547 loc_descr_without_nops (dw_loc_descr_ref &loc)
16549 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16550 return;
16552 /* Set of all DW_OP_nop operations we remove. */
16553 hash_set<dw_loc_descr_ref> nops;
16555 /* First, strip all prefix NOP operations in order to keep the head of the
16556 operations list. */
16557 loc_descr_to_next_no_nop (loc, nops);
16559 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16561 /* For control flow operations: strip "prefix" nops in destination
16562 labels. */
16563 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16564 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16565 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16566 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16568 /* Do the same for the operations that follow, then move to the next
16569 iteration. */
16570 if (cur->dw_loc_next != NULL)
16571 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16572 cur = cur->dw_loc_next;
16575 nops.traverse<void *, free_loc_descr> (NULL);
16579 struct dwarf_procedure_info;
16581 /* Helper structure for location descriptions generation. */
16582 struct loc_descr_context
16584 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16585 NULL_TREE if DW_OP_push_object_address in invalid for this location
16586 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16587 tree context_type;
16588 /* The ..._DECL node that should be translated as a
16589 DW_OP_push_object_address operation. */
16590 tree base_decl;
16591 /* Information about the DWARF procedure we are currently generating. NULL if
16592 we are not generating a DWARF procedure. */
16593 struct dwarf_procedure_info *dpi;
16594 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16595 by consumer. Used for DW_TAG_generic_subrange attributes. */
16596 bool placeholder_arg;
16597 /* True if PLACEHOLDER_EXPR has been seen. */
16598 bool placeholder_seen;
16601 /* DWARF procedures generation
16603 DWARF expressions (aka. location descriptions) are used to encode variable
16604 things such as sizes or offsets. Such computations can have redundant parts
16605 that can be factorized in order to reduce the size of the output debug
16606 information. This is the whole point of DWARF procedures.
16608 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16609 already factorized into functions ("size functions") in order to handle very
16610 big and complex types. Such functions are quite simple: they have integral
16611 arguments, they return an integral result and their body contains only a
16612 return statement with arithmetic expressions. This is the only kind of
16613 function we are interested in translating into DWARF procedures, here.
16615 DWARF expressions and DWARF procedure are executed using a stack, so we have
16616 to define some calling convention for them to interact. Let's say that:
16618 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16619 all arguments in reverse order (right-to-left) so that when the DWARF
16620 procedure execution starts, the first argument is the top of the stack.
16622 - Then, when returning, the DWARF procedure must have consumed all arguments
16623 on the stack, must have pushed the result and touched nothing else.
16625 - Each integral argument and the result are integral types can be hold in a
16626 single stack slot.
16628 - We call "frame offset" the number of stack slots that are "under DWARF
16629 procedure control": it includes the arguments slots, the temporaries and
16630 the result slot. Thus, it is equal to the number of arguments when the
16631 procedure execution starts and must be equal to one (the result) when it
16632 returns. */
16634 /* Helper structure used when generating operations for a DWARF procedure. */
16635 struct dwarf_procedure_info
16637 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16638 currently translated. */
16639 tree fndecl;
16640 /* The number of arguments FNDECL takes. */
16641 unsigned args_count;
16644 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16645 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16646 equate it to this DIE. */
16648 static dw_die_ref
16649 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16650 dw_die_ref parent_die)
16652 dw_die_ref dwarf_proc_die;
16654 if ((dwarf_version < 3 && dwarf_strict)
16655 || location == NULL)
16656 return NULL;
16658 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16659 if (fndecl)
16660 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16661 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16662 return dwarf_proc_die;
16665 /* Return whether TYPE is a supported type as a DWARF procedure argument
16666 type or return type (we handle only scalar types and pointer types that
16667 aren't wider than the DWARF expression evaluation stack. */
16669 static bool
16670 is_handled_procedure_type (tree type)
16672 return ((INTEGRAL_TYPE_P (type)
16673 || TREE_CODE (type) == OFFSET_TYPE
16674 || TREE_CODE (type) == POINTER_TYPE)
16675 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16678 /* Helper for resolve_args_picking: do the same but stop when coming across
16679 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16680 offset *before* evaluating the corresponding operation. */
16682 static bool
16683 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16684 struct dwarf_procedure_info *dpi,
16685 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16687 /* The "frame_offset" identifier is already used to name a macro... */
16688 unsigned frame_offset_ = initial_frame_offset;
16689 dw_loc_descr_ref l;
16691 for (l = loc; l != NULL;)
16693 bool existed;
16694 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16696 /* If we already met this node, there is nothing to compute anymore. */
16697 if (existed)
16699 /* Make sure that the stack size is consistent wherever the execution
16700 flow comes from. */
16701 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16702 break;
16704 l_frame_offset = frame_offset_;
16706 /* If needed, relocate the picking offset with respect to the frame
16707 offset. */
16708 if (l->frame_offset_rel)
16710 unsigned HOST_WIDE_INT off;
16711 switch (l->dw_loc_opc)
16713 case DW_OP_pick:
16714 off = l->dw_loc_oprnd1.v.val_unsigned;
16715 break;
16716 case DW_OP_dup:
16717 off = 0;
16718 break;
16719 case DW_OP_over:
16720 off = 1;
16721 break;
16722 default:
16723 gcc_unreachable ();
16725 /* frame_offset_ is the size of the current stack frame, including
16726 incoming arguments. Besides, the arguments are pushed
16727 right-to-left. Thus, in order to access the Nth argument from
16728 this operation node, the picking has to skip temporaries *plus*
16729 one stack slot per argument (0 for the first one, 1 for the second
16730 one, etc.).
16732 The targetted argument number (N) is already set as the operand,
16733 and the number of temporaries can be computed with:
16734 frame_offsets_ - dpi->args_count */
16735 off += frame_offset_ - dpi->args_count;
16737 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16738 if (off > 255)
16739 return false;
16741 if (off == 0)
16743 l->dw_loc_opc = DW_OP_dup;
16744 l->dw_loc_oprnd1.v.val_unsigned = 0;
16746 else if (off == 1)
16748 l->dw_loc_opc = DW_OP_over;
16749 l->dw_loc_oprnd1.v.val_unsigned = 0;
16751 else
16753 l->dw_loc_opc = DW_OP_pick;
16754 l->dw_loc_oprnd1.v.val_unsigned = off;
16758 /* Update frame_offset according to the effect the current operation has
16759 on the stack. */
16760 switch (l->dw_loc_opc)
16762 case DW_OP_deref:
16763 case DW_OP_swap:
16764 case DW_OP_rot:
16765 case DW_OP_abs:
16766 case DW_OP_neg:
16767 case DW_OP_not:
16768 case DW_OP_plus_uconst:
16769 case DW_OP_skip:
16770 case DW_OP_reg0:
16771 case DW_OP_reg1:
16772 case DW_OP_reg2:
16773 case DW_OP_reg3:
16774 case DW_OP_reg4:
16775 case DW_OP_reg5:
16776 case DW_OP_reg6:
16777 case DW_OP_reg7:
16778 case DW_OP_reg8:
16779 case DW_OP_reg9:
16780 case DW_OP_reg10:
16781 case DW_OP_reg11:
16782 case DW_OP_reg12:
16783 case DW_OP_reg13:
16784 case DW_OP_reg14:
16785 case DW_OP_reg15:
16786 case DW_OP_reg16:
16787 case DW_OP_reg17:
16788 case DW_OP_reg18:
16789 case DW_OP_reg19:
16790 case DW_OP_reg20:
16791 case DW_OP_reg21:
16792 case DW_OP_reg22:
16793 case DW_OP_reg23:
16794 case DW_OP_reg24:
16795 case DW_OP_reg25:
16796 case DW_OP_reg26:
16797 case DW_OP_reg27:
16798 case DW_OP_reg28:
16799 case DW_OP_reg29:
16800 case DW_OP_reg30:
16801 case DW_OP_reg31:
16802 case DW_OP_bregx:
16803 case DW_OP_piece:
16804 case DW_OP_deref_size:
16805 case DW_OP_nop:
16806 case DW_OP_bit_piece:
16807 case DW_OP_implicit_value:
16808 case DW_OP_stack_value:
16809 break;
16811 case DW_OP_addr:
16812 case DW_OP_const1u:
16813 case DW_OP_const1s:
16814 case DW_OP_const2u:
16815 case DW_OP_const2s:
16816 case DW_OP_const4u:
16817 case DW_OP_const4s:
16818 case DW_OP_const8u:
16819 case DW_OP_const8s:
16820 case DW_OP_constu:
16821 case DW_OP_consts:
16822 case DW_OP_dup:
16823 case DW_OP_over:
16824 case DW_OP_pick:
16825 case DW_OP_lit0:
16826 case DW_OP_lit1:
16827 case DW_OP_lit2:
16828 case DW_OP_lit3:
16829 case DW_OP_lit4:
16830 case DW_OP_lit5:
16831 case DW_OP_lit6:
16832 case DW_OP_lit7:
16833 case DW_OP_lit8:
16834 case DW_OP_lit9:
16835 case DW_OP_lit10:
16836 case DW_OP_lit11:
16837 case DW_OP_lit12:
16838 case DW_OP_lit13:
16839 case DW_OP_lit14:
16840 case DW_OP_lit15:
16841 case DW_OP_lit16:
16842 case DW_OP_lit17:
16843 case DW_OP_lit18:
16844 case DW_OP_lit19:
16845 case DW_OP_lit20:
16846 case DW_OP_lit21:
16847 case DW_OP_lit22:
16848 case DW_OP_lit23:
16849 case DW_OP_lit24:
16850 case DW_OP_lit25:
16851 case DW_OP_lit26:
16852 case DW_OP_lit27:
16853 case DW_OP_lit28:
16854 case DW_OP_lit29:
16855 case DW_OP_lit30:
16856 case DW_OP_lit31:
16857 case DW_OP_breg0:
16858 case DW_OP_breg1:
16859 case DW_OP_breg2:
16860 case DW_OP_breg3:
16861 case DW_OP_breg4:
16862 case DW_OP_breg5:
16863 case DW_OP_breg6:
16864 case DW_OP_breg7:
16865 case DW_OP_breg8:
16866 case DW_OP_breg9:
16867 case DW_OP_breg10:
16868 case DW_OP_breg11:
16869 case DW_OP_breg12:
16870 case DW_OP_breg13:
16871 case DW_OP_breg14:
16872 case DW_OP_breg15:
16873 case DW_OP_breg16:
16874 case DW_OP_breg17:
16875 case DW_OP_breg18:
16876 case DW_OP_breg19:
16877 case DW_OP_breg20:
16878 case DW_OP_breg21:
16879 case DW_OP_breg22:
16880 case DW_OP_breg23:
16881 case DW_OP_breg24:
16882 case DW_OP_breg25:
16883 case DW_OP_breg26:
16884 case DW_OP_breg27:
16885 case DW_OP_breg28:
16886 case DW_OP_breg29:
16887 case DW_OP_breg30:
16888 case DW_OP_breg31:
16889 case DW_OP_fbreg:
16890 case DW_OP_push_object_address:
16891 case DW_OP_call_frame_cfa:
16892 case DW_OP_GNU_variable_value:
16893 ++frame_offset_;
16894 break;
16896 case DW_OP_drop:
16897 case DW_OP_xderef:
16898 case DW_OP_and:
16899 case DW_OP_div:
16900 case DW_OP_minus:
16901 case DW_OP_mod:
16902 case DW_OP_mul:
16903 case DW_OP_or:
16904 case DW_OP_plus:
16905 case DW_OP_shl:
16906 case DW_OP_shr:
16907 case DW_OP_shra:
16908 case DW_OP_xor:
16909 case DW_OP_bra:
16910 case DW_OP_eq:
16911 case DW_OP_ge:
16912 case DW_OP_gt:
16913 case DW_OP_le:
16914 case DW_OP_lt:
16915 case DW_OP_ne:
16916 case DW_OP_regx:
16917 case DW_OP_xderef_size:
16918 --frame_offset_;
16919 break;
16921 case DW_OP_call2:
16922 case DW_OP_call4:
16923 case DW_OP_call_ref:
16925 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16926 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16928 if (stack_usage == NULL)
16929 return false;
16930 frame_offset_ += *stack_usage;
16931 break;
16934 case DW_OP_implicit_pointer:
16935 case DW_OP_entry_value:
16936 case DW_OP_const_type:
16937 case DW_OP_regval_type:
16938 case DW_OP_deref_type:
16939 case DW_OP_convert:
16940 case DW_OP_reinterpret:
16941 case DW_OP_form_tls_address:
16942 case DW_OP_GNU_push_tls_address:
16943 case DW_OP_GNU_uninit:
16944 case DW_OP_GNU_encoded_addr:
16945 case DW_OP_GNU_implicit_pointer:
16946 case DW_OP_GNU_entry_value:
16947 case DW_OP_GNU_const_type:
16948 case DW_OP_GNU_regval_type:
16949 case DW_OP_GNU_deref_type:
16950 case DW_OP_GNU_convert:
16951 case DW_OP_GNU_reinterpret:
16952 case DW_OP_GNU_parameter_ref:
16953 /* loc_list_from_tree will probably not output these operations for
16954 size functions, so assume they will not appear here. */
16955 /* Fall through... */
16957 default:
16958 gcc_unreachable ();
16961 /* Now, follow the control flow (except subroutine calls). */
16962 switch (l->dw_loc_opc)
16964 case DW_OP_bra:
16965 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16966 frame_offsets))
16967 return false;
16968 /* Fall through. */
16970 case DW_OP_skip:
16971 l = l->dw_loc_oprnd1.v.val_loc;
16972 break;
16974 case DW_OP_stack_value:
16975 return true;
16977 default:
16978 l = l->dw_loc_next;
16979 break;
16983 return true;
16986 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16987 operations) in order to resolve the operand of DW_OP_pick operations that
16988 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16989 offset *before* LOC is executed. Return if all relocations were
16990 successful. */
16992 static bool
16993 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16994 struct dwarf_procedure_info *dpi)
16996 /* Associate to all visited operations the frame offset *before* evaluating
16997 this operation. */
16998 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17000 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17001 frame_offsets);
17004 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17005 Return NULL if it is not possible. */
17007 static dw_die_ref
17008 function_to_dwarf_procedure (tree fndecl)
17010 struct loc_descr_context ctx;
17011 struct dwarf_procedure_info dpi;
17012 dw_die_ref dwarf_proc_die;
17013 tree tree_body = DECL_SAVED_TREE (fndecl);
17014 dw_loc_descr_ref loc_body, epilogue;
17016 tree cursor;
17017 unsigned i;
17019 /* Do not generate multiple DWARF procedures for the same function
17020 declaration. */
17021 dwarf_proc_die = lookup_decl_die (fndecl);
17022 if (dwarf_proc_die != NULL)
17023 return dwarf_proc_die;
17025 /* DWARF procedures are available starting with the DWARFv3 standard. */
17026 if (dwarf_version < 3 && dwarf_strict)
17027 return NULL;
17029 /* We handle only functions for which we still have a body, that return a
17030 supported type and that takes arguments with supported types. Note that
17031 there is no point translating functions that return nothing. */
17032 if (tree_body == NULL_TREE
17033 || DECL_RESULT (fndecl) == NULL_TREE
17034 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17035 return NULL;
17037 for (cursor = DECL_ARGUMENTS (fndecl);
17038 cursor != NULL_TREE;
17039 cursor = TREE_CHAIN (cursor))
17040 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17041 return NULL;
17043 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17044 if (TREE_CODE (tree_body) != RETURN_EXPR)
17045 return NULL;
17046 tree_body = TREE_OPERAND (tree_body, 0);
17047 if (TREE_CODE (tree_body) != MODIFY_EXPR
17048 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17049 return NULL;
17050 tree_body = TREE_OPERAND (tree_body, 1);
17052 /* Try to translate the body expression itself. Note that this will probably
17053 cause an infinite recursion if its call graph has a cycle. This is very
17054 unlikely for size functions, however, so don't bother with such things at
17055 the moment. */
17056 ctx.context_type = NULL_TREE;
17057 ctx.base_decl = NULL_TREE;
17058 ctx.dpi = &dpi;
17059 ctx.placeholder_arg = false;
17060 ctx.placeholder_seen = false;
17061 dpi.fndecl = fndecl;
17062 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17063 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17064 if (!loc_body)
17065 return NULL;
17067 /* After evaluating all operands in "loc_body", we should still have on the
17068 stack all arguments plus the desired function result (top of the stack).
17069 Generate code in order to keep only the result in our stack frame. */
17070 epilogue = NULL;
17071 for (i = 0; i < dpi.args_count; ++i)
17073 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17074 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17075 op_couple->dw_loc_next->dw_loc_next = epilogue;
17076 epilogue = op_couple;
17078 add_loc_descr (&loc_body, epilogue);
17079 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17080 return NULL;
17082 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17083 because they are considered useful. Now there is an epilogue, they are
17084 not anymore, so give it another try. */
17085 loc_descr_without_nops (loc_body);
17087 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17088 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17089 though, given that size functions do not come from source, so they should
17090 not have a dedicated DW_TAG_subprogram DIE. */
17091 dwarf_proc_die
17092 = new_dwarf_proc_die (loc_body, fndecl,
17093 get_context_die (DECL_CONTEXT (fndecl)));
17095 /* The called DWARF procedure consumes one stack slot per argument and
17096 returns one stack slot. */
17097 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17099 return dwarf_proc_die;
17103 /* Generate Dwarf location list representing LOC.
17104 If WANT_ADDRESS is false, expression computing LOC will be computed
17105 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17106 if WANT_ADDRESS is 2, expression computing address useable in location
17107 will be returned (i.e. DW_OP_reg can be used
17108 to refer to register values).
17110 CONTEXT provides information to customize the location descriptions
17111 generation. Its context_type field specifies what type is implicitly
17112 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17113 will not be generated.
17115 Its DPI field determines whether we are generating a DWARF expression for a
17116 DWARF procedure, so PARM_DECL references are processed specifically.
17118 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17119 and dpi fields were null. */
17121 static dw_loc_list_ref
17122 loc_list_from_tree_1 (tree loc, int want_address,
17123 struct loc_descr_context *context)
17125 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17126 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17127 int have_address = 0;
17128 enum dwarf_location_atom op;
17130 /* ??? Most of the time we do not take proper care for sign/zero
17131 extending the values properly. Hopefully this won't be a real
17132 problem... */
17134 if (context != NULL
17135 && context->base_decl == loc
17136 && want_address == 0)
17138 if (dwarf_version >= 3 || !dwarf_strict)
17139 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17140 NULL, NULL, NULL);
17141 else
17142 return NULL;
17145 switch (TREE_CODE (loc))
17147 case ERROR_MARK:
17148 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17149 return 0;
17151 case PLACEHOLDER_EXPR:
17152 /* This case involves extracting fields from an object to determine the
17153 position of other fields. It is supposed to appear only as the first
17154 operand of COMPONENT_REF nodes and to reference precisely the type
17155 that the context allows. */
17156 if (context != NULL
17157 && TREE_TYPE (loc) == context->context_type
17158 && want_address >= 1)
17160 if (dwarf_version >= 3 || !dwarf_strict)
17162 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17163 have_address = 1;
17164 break;
17166 else
17167 return NULL;
17169 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17170 the single argument passed by consumer. */
17171 else if (context != NULL
17172 && context->placeholder_arg
17173 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17174 && want_address == 0)
17176 ret = new_loc_descr (DW_OP_pick, 0, 0);
17177 ret->frame_offset_rel = 1;
17178 context->placeholder_seen = true;
17179 break;
17181 else
17182 expansion_failed (loc, NULL_RTX,
17183 "PLACEHOLDER_EXPR for an unexpected type");
17184 break;
17186 case CALL_EXPR:
17188 const int nargs = call_expr_nargs (loc);
17189 tree callee = get_callee_fndecl (loc);
17190 int i;
17191 dw_die_ref dwarf_proc;
17193 if (callee == NULL_TREE)
17194 goto call_expansion_failed;
17196 /* We handle only functions that return an integer. */
17197 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17198 goto call_expansion_failed;
17200 dwarf_proc = function_to_dwarf_procedure (callee);
17201 if (dwarf_proc == NULL)
17202 goto call_expansion_failed;
17204 /* Evaluate arguments right-to-left so that the first argument will
17205 be the top-most one on the stack. */
17206 for (i = nargs - 1; i >= 0; --i)
17208 dw_loc_descr_ref loc_descr
17209 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17210 context);
17212 if (loc_descr == NULL)
17213 goto call_expansion_failed;
17215 add_loc_descr (&ret, loc_descr);
17218 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17219 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17220 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17221 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17222 add_loc_descr (&ret, ret1);
17223 break;
17225 call_expansion_failed:
17226 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17227 /* There are no opcodes for these operations. */
17228 return 0;
17231 case PREINCREMENT_EXPR:
17232 case PREDECREMENT_EXPR:
17233 case POSTINCREMENT_EXPR:
17234 case POSTDECREMENT_EXPR:
17235 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17236 /* There are no opcodes for these operations. */
17237 return 0;
17239 case ADDR_EXPR:
17240 /* If we already want an address, see if there is INDIRECT_REF inside
17241 e.g. for &this->field. */
17242 if (want_address)
17244 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17245 (loc, want_address == 2, context);
17246 if (list_ret)
17247 have_address = 1;
17248 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17249 && (ret = cst_pool_loc_descr (loc)))
17250 have_address = 1;
17252 /* Otherwise, process the argument and look for the address. */
17253 if (!list_ret && !ret)
17254 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17255 else
17257 if (want_address)
17258 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17259 return NULL;
17261 break;
17263 case VAR_DECL:
17264 if (DECL_THREAD_LOCAL_P (loc))
17266 rtx rtl;
17267 enum dwarf_location_atom tls_op;
17268 enum dtprel_bool dtprel = dtprel_false;
17270 if (targetm.have_tls)
17272 /* If this is not defined, we have no way to emit the
17273 data. */
17274 if (!targetm.asm_out.output_dwarf_dtprel)
17275 return 0;
17277 /* The way DW_OP_GNU_push_tls_address is specified, we
17278 can only look up addresses of objects in the current
17279 module. We used DW_OP_addr as first op, but that's
17280 wrong, because DW_OP_addr is relocated by the debug
17281 info consumer, while DW_OP_GNU_push_tls_address
17282 operand shouldn't be. */
17283 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17284 return 0;
17285 dtprel = dtprel_true;
17286 /* We check for DWARF 5 here because gdb did not implement
17287 DW_OP_form_tls_address until after 7.12. */
17288 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17289 : DW_OP_GNU_push_tls_address);
17291 else
17293 if (!targetm.emutls.debug_form_tls_address
17294 || !(dwarf_version >= 3 || !dwarf_strict))
17295 return 0;
17296 /* We stuffed the control variable into the DECL_VALUE_EXPR
17297 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17298 no longer appear in gimple code. We used the control
17299 variable in specific so that we could pick it up here. */
17300 loc = DECL_VALUE_EXPR (loc);
17301 tls_op = DW_OP_form_tls_address;
17304 rtl = rtl_for_decl_location (loc);
17305 if (rtl == NULL_RTX)
17306 return 0;
17308 if (!MEM_P (rtl))
17309 return 0;
17310 rtl = XEXP (rtl, 0);
17311 if (! CONSTANT_P (rtl))
17312 return 0;
17314 ret = new_addr_loc_descr (rtl, dtprel);
17315 ret1 = new_loc_descr (tls_op, 0, 0);
17316 add_loc_descr (&ret, ret1);
17318 have_address = 1;
17319 break;
17321 /* FALLTHRU */
17323 case PARM_DECL:
17324 if (context != NULL && context->dpi != NULL
17325 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17327 /* We are generating code for a DWARF procedure and we want to access
17328 one of its arguments: find the appropriate argument offset and let
17329 the resolve_args_picking pass compute the offset that complies
17330 with the stack frame size. */
17331 unsigned i = 0;
17332 tree cursor;
17334 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17335 cursor != NULL_TREE && cursor != loc;
17336 cursor = TREE_CHAIN (cursor), ++i)
17338 /* If we are translating a DWARF procedure, all referenced parameters
17339 must belong to the current function. */
17340 gcc_assert (cursor != NULL_TREE);
17342 ret = new_loc_descr (DW_OP_pick, i, 0);
17343 ret->frame_offset_rel = 1;
17344 break;
17346 /* FALLTHRU */
17348 case RESULT_DECL:
17349 if (DECL_HAS_VALUE_EXPR_P (loc))
17350 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17351 want_address, context);
17352 /* FALLTHRU */
17354 case FUNCTION_DECL:
17356 rtx rtl;
17357 var_loc_list *loc_list = lookup_decl_loc (loc);
17359 if (loc_list && loc_list->first)
17361 list_ret = dw_loc_list (loc_list, loc, want_address);
17362 have_address = want_address != 0;
17363 break;
17365 rtl = rtl_for_decl_location (loc);
17366 if (rtl == NULL_RTX)
17368 if (TREE_CODE (loc) != FUNCTION_DECL
17369 && early_dwarf
17370 && current_function_decl
17371 && want_address != 1
17372 && ! DECL_IGNORED_P (loc)
17373 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17374 || POINTER_TYPE_P (TREE_TYPE (loc)))
17375 && DECL_CONTEXT (loc) == current_function_decl
17376 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17377 <= DWARF2_ADDR_SIZE))
17379 dw_die_ref ref = lookup_decl_die (loc);
17380 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17381 if (ref)
17383 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17384 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17385 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17387 else
17389 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17390 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17392 break;
17394 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17395 return 0;
17397 else if (CONST_INT_P (rtl))
17399 HOST_WIDE_INT val = INTVAL (rtl);
17400 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17401 val &= GET_MODE_MASK (DECL_MODE (loc));
17402 ret = int_loc_descriptor (val);
17404 else if (GET_CODE (rtl) == CONST_STRING)
17406 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17407 return 0;
17409 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17410 ret = new_addr_loc_descr (rtl, dtprel_false);
17411 else
17413 machine_mode mode, mem_mode;
17415 /* Certain constructs can only be represented at top-level. */
17416 if (want_address == 2)
17418 ret = loc_descriptor (rtl, VOIDmode,
17419 VAR_INIT_STATUS_INITIALIZED);
17420 have_address = 1;
17422 else
17424 mode = GET_MODE (rtl);
17425 mem_mode = VOIDmode;
17426 if (MEM_P (rtl))
17428 mem_mode = mode;
17429 mode = get_address_mode (rtl);
17430 rtl = XEXP (rtl, 0);
17431 have_address = 1;
17433 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17434 VAR_INIT_STATUS_INITIALIZED);
17436 if (!ret)
17437 expansion_failed (loc, rtl,
17438 "failed to produce loc descriptor for rtl");
17441 break;
17443 case MEM_REF:
17444 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17446 have_address = 1;
17447 goto do_plus;
17449 /* Fallthru. */
17450 case INDIRECT_REF:
17451 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17452 have_address = 1;
17453 break;
17455 case TARGET_MEM_REF:
17456 case SSA_NAME:
17457 case DEBUG_EXPR_DECL:
17458 return NULL;
17460 case COMPOUND_EXPR:
17461 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17462 context);
17464 CASE_CONVERT:
17465 case VIEW_CONVERT_EXPR:
17466 case SAVE_EXPR:
17467 case MODIFY_EXPR:
17468 case NON_LVALUE_EXPR:
17469 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17470 context);
17472 case COMPONENT_REF:
17473 case BIT_FIELD_REF:
17474 case ARRAY_REF:
17475 case ARRAY_RANGE_REF:
17476 case REALPART_EXPR:
17477 case IMAGPART_EXPR:
17479 tree obj, offset;
17480 HOST_WIDE_INT bitsize, bitpos, bytepos;
17481 machine_mode mode;
17482 int unsignedp, reversep, volatilep = 0;
17484 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17485 &unsignedp, &reversep, &volatilep);
17487 gcc_assert (obj != loc);
17489 list_ret = loc_list_from_tree_1 (obj,
17490 want_address == 2
17491 && !bitpos && !offset ? 2 : 1,
17492 context);
17493 /* TODO: We can extract value of the small expression via shifting even
17494 for nonzero bitpos. */
17495 if (list_ret == 0)
17496 return 0;
17497 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17499 expansion_failed (loc, NULL_RTX,
17500 "bitfield access");
17501 return 0;
17504 if (offset != NULL_TREE)
17506 /* Variable offset. */
17507 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17508 if (list_ret1 == 0)
17509 return 0;
17510 add_loc_list (&list_ret, list_ret1);
17511 if (!list_ret)
17512 return 0;
17513 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17516 bytepos = bitpos / BITS_PER_UNIT;
17517 if (bytepos > 0)
17518 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17519 else if (bytepos < 0)
17520 loc_list_plus_const (list_ret, bytepos);
17522 have_address = 1;
17523 break;
17526 case INTEGER_CST:
17527 if ((want_address || !tree_fits_shwi_p (loc))
17528 && (ret = cst_pool_loc_descr (loc)))
17529 have_address = 1;
17530 else if (want_address == 2
17531 && tree_fits_shwi_p (loc)
17532 && (ret = address_of_int_loc_descriptor
17533 (int_size_in_bytes (TREE_TYPE (loc)),
17534 tree_to_shwi (loc))))
17535 have_address = 1;
17536 else if (tree_fits_shwi_p (loc))
17537 ret = int_loc_descriptor (tree_to_shwi (loc));
17538 else if (tree_fits_uhwi_p (loc))
17539 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17540 else
17542 expansion_failed (loc, NULL_RTX,
17543 "Integer operand is not host integer");
17544 return 0;
17546 break;
17548 case CONSTRUCTOR:
17549 case REAL_CST:
17550 case STRING_CST:
17551 case COMPLEX_CST:
17552 if ((ret = cst_pool_loc_descr (loc)))
17553 have_address = 1;
17554 else if (TREE_CODE (loc) == CONSTRUCTOR)
17556 tree type = TREE_TYPE (loc);
17557 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17558 unsigned HOST_WIDE_INT offset = 0;
17559 unsigned HOST_WIDE_INT cnt;
17560 constructor_elt *ce;
17562 if (TREE_CODE (type) == RECORD_TYPE)
17564 /* This is very limited, but it's enough to output
17565 pointers to member functions, as long as the
17566 referenced function is defined in the current
17567 translation unit. */
17568 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17570 tree val = ce->value;
17572 tree field = ce->index;
17574 if (val)
17575 STRIP_NOPS (val);
17577 if (!field || DECL_BIT_FIELD (field))
17579 expansion_failed (loc, NULL_RTX,
17580 "bitfield in record type constructor");
17581 size = offset = (unsigned HOST_WIDE_INT)-1;
17582 ret = NULL;
17583 break;
17586 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17587 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17588 gcc_assert (pos + fieldsize <= size);
17589 if (pos < offset)
17591 expansion_failed (loc, NULL_RTX,
17592 "out-of-order fields in record constructor");
17593 size = offset = (unsigned HOST_WIDE_INT)-1;
17594 ret = NULL;
17595 break;
17597 if (pos > offset)
17599 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17600 add_loc_descr (&ret, ret1);
17601 offset = pos;
17603 if (val && fieldsize != 0)
17605 ret1 = loc_descriptor_from_tree (val, want_address, context);
17606 if (!ret1)
17608 expansion_failed (loc, NULL_RTX,
17609 "unsupported expression in field");
17610 size = offset = (unsigned HOST_WIDE_INT)-1;
17611 ret = NULL;
17612 break;
17614 add_loc_descr (&ret, ret1);
17616 if (fieldsize)
17618 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17619 add_loc_descr (&ret, ret1);
17620 offset = pos + fieldsize;
17624 if (offset != size)
17626 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17627 add_loc_descr (&ret, ret1);
17628 offset = size;
17631 have_address = !!want_address;
17633 else
17634 expansion_failed (loc, NULL_RTX,
17635 "constructor of non-record type");
17637 else
17638 /* We can construct small constants here using int_loc_descriptor. */
17639 expansion_failed (loc, NULL_RTX,
17640 "constructor or constant not in constant pool");
17641 break;
17643 case TRUTH_AND_EXPR:
17644 case TRUTH_ANDIF_EXPR:
17645 case BIT_AND_EXPR:
17646 op = DW_OP_and;
17647 goto do_binop;
17649 case TRUTH_XOR_EXPR:
17650 case BIT_XOR_EXPR:
17651 op = DW_OP_xor;
17652 goto do_binop;
17654 case TRUTH_OR_EXPR:
17655 case TRUTH_ORIF_EXPR:
17656 case BIT_IOR_EXPR:
17657 op = DW_OP_or;
17658 goto do_binop;
17660 case FLOOR_DIV_EXPR:
17661 case CEIL_DIV_EXPR:
17662 case ROUND_DIV_EXPR:
17663 case TRUNC_DIV_EXPR:
17664 case EXACT_DIV_EXPR:
17665 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17666 return 0;
17667 op = DW_OP_div;
17668 goto do_binop;
17670 case MINUS_EXPR:
17671 op = DW_OP_minus;
17672 goto do_binop;
17674 case FLOOR_MOD_EXPR:
17675 case CEIL_MOD_EXPR:
17676 case ROUND_MOD_EXPR:
17677 case TRUNC_MOD_EXPR:
17678 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17680 op = DW_OP_mod;
17681 goto do_binop;
17683 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17684 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17685 if (list_ret == 0 || list_ret1 == 0)
17686 return 0;
17688 add_loc_list (&list_ret, list_ret1);
17689 if (list_ret == 0)
17690 return 0;
17691 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17692 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17693 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17694 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17695 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17696 break;
17698 case MULT_EXPR:
17699 op = DW_OP_mul;
17700 goto do_binop;
17702 case LSHIFT_EXPR:
17703 op = DW_OP_shl;
17704 goto do_binop;
17706 case RSHIFT_EXPR:
17707 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17708 goto do_binop;
17710 case POINTER_PLUS_EXPR:
17711 case PLUS_EXPR:
17712 do_plus:
17713 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17715 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17716 smarter to encode their opposite. The DW_OP_plus_uconst operation
17717 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17718 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17719 bytes, Y being the size of the operation that pushes the opposite
17720 of the addend. So let's choose the smallest representation. */
17721 const tree tree_addend = TREE_OPERAND (loc, 1);
17722 offset_int wi_addend;
17723 HOST_WIDE_INT shwi_addend;
17724 dw_loc_descr_ref loc_naddend;
17726 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17727 if (list_ret == 0)
17728 return 0;
17730 /* Try to get the literal to push. It is the opposite of the addend,
17731 so as we rely on wrapping during DWARF evaluation, first decode
17732 the literal as a "DWARF-sized" signed number. */
17733 wi_addend = wi::to_offset (tree_addend);
17734 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17735 shwi_addend = wi_addend.to_shwi ();
17736 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17737 ? int_loc_descriptor (-shwi_addend)
17738 : NULL;
17740 if (loc_naddend != NULL
17741 && ((unsigned) size_of_uleb128 (shwi_addend)
17742 > size_of_loc_descr (loc_naddend)))
17744 add_loc_descr_to_each (list_ret, loc_naddend);
17745 add_loc_descr_to_each (list_ret,
17746 new_loc_descr (DW_OP_minus, 0, 0));
17748 else
17750 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17752 loc_naddend = loc_cur;
17753 loc_cur = loc_cur->dw_loc_next;
17754 ggc_free (loc_naddend);
17756 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17758 break;
17761 op = DW_OP_plus;
17762 goto do_binop;
17764 case LE_EXPR:
17765 op = DW_OP_le;
17766 goto do_comp_binop;
17768 case GE_EXPR:
17769 op = DW_OP_ge;
17770 goto do_comp_binop;
17772 case LT_EXPR:
17773 op = DW_OP_lt;
17774 goto do_comp_binop;
17776 case GT_EXPR:
17777 op = DW_OP_gt;
17778 goto do_comp_binop;
17780 do_comp_binop:
17781 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17783 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17784 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17785 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17786 TREE_CODE (loc));
17787 break;
17789 else
17790 goto do_binop;
17792 case EQ_EXPR:
17793 op = DW_OP_eq;
17794 goto do_binop;
17796 case NE_EXPR:
17797 op = DW_OP_ne;
17798 goto do_binop;
17800 do_binop:
17801 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17802 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17803 if (list_ret == 0 || list_ret1 == 0)
17804 return 0;
17806 add_loc_list (&list_ret, list_ret1);
17807 if (list_ret == 0)
17808 return 0;
17809 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17810 break;
17812 case TRUTH_NOT_EXPR:
17813 case BIT_NOT_EXPR:
17814 op = DW_OP_not;
17815 goto do_unop;
17817 case ABS_EXPR:
17818 op = DW_OP_abs;
17819 goto do_unop;
17821 case NEGATE_EXPR:
17822 op = DW_OP_neg;
17823 goto do_unop;
17825 do_unop:
17826 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17827 if (list_ret == 0)
17828 return 0;
17830 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17831 break;
17833 case MIN_EXPR:
17834 case MAX_EXPR:
17836 const enum tree_code code =
17837 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17839 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17840 build2 (code, integer_type_node,
17841 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17842 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17845 /* fall through */
17847 case COND_EXPR:
17849 dw_loc_descr_ref lhs
17850 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17851 dw_loc_list_ref rhs
17852 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17853 dw_loc_descr_ref bra_node, jump_node, tmp;
17855 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17856 if (list_ret == 0 || lhs == 0 || rhs == 0)
17857 return 0;
17859 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17860 add_loc_descr_to_each (list_ret, bra_node);
17862 add_loc_list (&list_ret, rhs);
17863 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17864 add_loc_descr_to_each (list_ret, jump_node);
17866 add_loc_descr_to_each (list_ret, lhs);
17867 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17868 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17870 /* ??? Need a node to point the skip at. Use a nop. */
17871 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17872 add_loc_descr_to_each (list_ret, tmp);
17873 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17874 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17876 break;
17878 case FIX_TRUNC_EXPR:
17879 return 0;
17881 default:
17882 /* Leave front-end specific codes as simply unknown. This comes
17883 up, for instance, with the C STMT_EXPR. */
17884 if ((unsigned int) TREE_CODE (loc)
17885 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17887 expansion_failed (loc, NULL_RTX,
17888 "language specific tree node");
17889 return 0;
17892 /* Otherwise this is a generic code; we should just lists all of
17893 these explicitly. We forgot one. */
17894 if (flag_checking)
17895 gcc_unreachable ();
17897 /* In a release build, we want to degrade gracefully: better to
17898 generate incomplete debugging information than to crash. */
17899 return NULL;
17902 if (!ret && !list_ret)
17903 return 0;
17905 if (want_address == 2 && !have_address
17906 && (dwarf_version >= 4 || !dwarf_strict))
17908 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17910 expansion_failed (loc, NULL_RTX,
17911 "DWARF address size mismatch");
17912 return 0;
17914 if (ret)
17915 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17916 else
17917 add_loc_descr_to_each (list_ret,
17918 new_loc_descr (DW_OP_stack_value, 0, 0));
17919 have_address = 1;
17921 /* Show if we can't fill the request for an address. */
17922 if (want_address && !have_address)
17924 expansion_failed (loc, NULL_RTX,
17925 "Want address and only have value");
17926 return 0;
17929 gcc_assert (!ret || !list_ret);
17931 /* If we've got an address and don't want one, dereference. */
17932 if (!want_address && have_address)
17934 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17936 if (size > DWARF2_ADDR_SIZE || size == -1)
17938 expansion_failed (loc, NULL_RTX,
17939 "DWARF address size mismatch");
17940 return 0;
17942 else if (size == DWARF2_ADDR_SIZE)
17943 op = DW_OP_deref;
17944 else
17945 op = DW_OP_deref_size;
17947 if (ret)
17948 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17949 else
17950 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17952 if (ret)
17953 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17955 return list_ret;
17958 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17959 expressions. */
17961 static dw_loc_list_ref
17962 loc_list_from_tree (tree loc, int want_address,
17963 struct loc_descr_context *context)
17965 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17967 for (dw_loc_list_ref loc_cur = result;
17968 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17969 loc_descr_without_nops (loc_cur->expr);
17970 return result;
17973 /* Same as above but return only single location expression. */
17974 static dw_loc_descr_ref
17975 loc_descriptor_from_tree (tree loc, int want_address,
17976 struct loc_descr_context *context)
17978 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17979 if (!ret)
17980 return NULL;
17981 if (ret->dw_loc_next)
17983 expansion_failed (loc, NULL_RTX,
17984 "Location list where only loc descriptor needed");
17985 return NULL;
17987 return ret->expr;
17990 /* Given a value, round it up to the lowest multiple of `boundary'
17991 which is not less than the value itself. */
17993 static inline HOST_WIDE_INT
17994 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17996 return (((value + boundary - 1) / boundary) * boundary);
17999 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18000 pointer to the declared type for the relevant field variable, or return
18001 `integer_type_node' if the given node turns out to be an
18002 ERROR_MARK node. */
18004 static inline tree
18005 field_type (const_tree decl)
18007 tree type;
18009 if (TREE_CODE (decl) == ERROR_MARK)
18010 return integer_type_node;
18012 type = DECL_BIT_FIELD_TYPE (decl);
18013 if (type == NULL_TREE)
18014 type = TREE_TYPE (decl);
18016 return type;
18019 /* Given a pointer to a tree node, return the alignment in bits for
18020 it, or else return BITS_PER_WORD if the node actually turns out to
18021 be an ERROR_MARK node. */
18023 static inline unsigned
18024 simple_type_align_in_bits (const_tree type)
18026 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18029 static inline unsigned
18030 simple_decl_align_in_bits (const_tree decl)
18032 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18035 /* Return the result of rounding T up to ALIGN. */
18037 static inline offset_int
18038 round_up_to_align (const offset_int &t, unsigned int align)
18040 return wi::udiv_trunc (t + align - 1, align) * align;
18043 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18044 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18045 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18046 if we fail to return the size in one of these two forms. */
18048 static dw_loc_descr_ref
18049 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18051 tree tree_size;
18052 struct loc_descr_context ctx;
18054 /* Return a constant integer in priority, if possible. */
18055 *cst_size = int_size_in_bytes (type);
18056 if (*cst_size != -1)
18057 return NULL;
18059 ctx.context_type = const_cast<tree> (type);
18060 ctx.base_decl = NULL_TREE;
18061 ctx.dpi = NULL;
18062 ctx.placeholder_arg = false;
18063 ctx.placeholder_seen = false;
18065 type = TYPE_MAIN_VARIANT (type);
18066 tree_size = TYPE_SIZE_UNIT (type);
18067 return ((tree_size != NULL_TREE)
18068 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18069 : NULL);
18072 /* Helper structure for RECORD_TYPE processing. */
18073 struct vlr_context
18075 /* Root RECORD_TYPE. It is needed to generate data member location
18076 descriptions in variable-length records (VLR), but also to cope with
18077 variants, which are composed of nested structures multiplexed with
18078 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18079 function processing a FIELD_DECL, it is required to be non null. */
18080 tree struct_type;
18081 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18082 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18083 this variant part as part of the root record (in storage units). For
18084 regular records, it must be NULL_TREE. */
18085 tree variant_part_offset;
18088 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18089 addressed byte of the "containing object" for the given FIELD_DECL. If
18090 possible, return a native constant through CST_OFFSET (in which case NULL is
18091 returned); otherwise return a DWARF expression that computes the offset.
18093 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18094 that offset is, either because the argument turns out to be a pointer to an
18095 ERROR_MARK node, or because the offset expression is too complex for us.
18097 CTX is required: see the comment for VLR_CONTEXT. */
18099 static dw_loc_descr_ref
18100 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18101 HOST_WIDE_INT *cst_offset)
18103 tree tree_result;
18104 dw_loc_list_ref loc_result;
18106 *cst_offset = 0;
18108 if (TREE_CODE (decl) == ERROR_MARK)
18109 return NULL;
18110 else
18111 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18113 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18114 case. */
18115 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18116 return NULL;
18118 #ifdef PCC_BITFIELD_TYPE_MATTERS
18119 /* We used to handle only constant offsets in all cases. Now, we handle
18120 properly dynamic byte offsets only when PCC bitfield type doesn't
18121 matter. */
18122 if (PCC_BITFIELD_TYPE_MATTERS
18123 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18125 offset_int object_offset_in_bits;
18126 offset_int object_offset_in_bytes;
18127 offset_int bitpos_int;
18128 tree type;
18129 tree field_size_tree;
18130 offset_int deepest_bitpos;
18131 offset_int field_size_in_bits;
18132 unsigned int type_align_in_bits;
18133 unsigned int decl_align_in_bits;
18134 offset_int type_size_in_bits;
18136 bitpos_int = wi::to_offset (bit_position (decl));
18137 type = field_type (decl);
18138 type_size_in_bits = offset_int_type_size_in_bits (type);
18139 type_align_in_bits = simple_type_align_in_bits (type);
18141 field_size_tree = DECL_SIZE (decl);
18143 /* The size could be unspecified if there was an error, or for
18144 a flexible array member. */
18145 if (!field_size_tree)
18146 field_size_tree = bitsize_zero_node;
18148 /* If the size of the field is not constant, use the type size. */
18149 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18150 field_size_in_bits = wi::to_offset (field_size_tree);
18151 else
18152 field_size_in_bits = type_size_in_bits;
18154 decl_align_in_bits = simple_decl_align_in_bits (decl);
18156 /* The GCC front-end doesn't make any attempt to keep track of the
18157 starting bit offset (relative to the start of the containing
18158 structure type) of the hypothetical "containing object" for a
18159 bit-field. Thus, when computing the byte offset value for the
18160 start of the "containing object" of a bit-field, we must deduce
18161 this information on our own. This can be rather tricky to do in
18162 some cases. For example, handling the following structure type
18163 definition when compiling for an i386/i486 target (which only
18164 aligns long long's to 32-bit boundaries) can be very tricky:
18166 struct S { int field1; long long field2:31; };
18168 Fortunately, there is a simple rule-of-thumb which can be used
18169 in such cases. When compiling for an i386/i486, GCC will
18170 allocate 8 bytes for the structure shown above. It decides to
18171 do this based upon one simple rule for bit-field allocation.
18172 GCC allocates each "containing object" for each bit-field at
18173 the first (i.e. lowest addressed) legitimate alignment boundary
18174 (based upon the required minimum alignment for the declared
18175 type of the field) which it can possibly use, subject to the
18176 condition that there is still enough available space remaining
18177 in the containing object (when allocated at the selected point)
18178 to fully accommodate all of the bits of the bit-field itself.
18180 This simple rule makes it obvious why GCC allocates 8 bytes for
18181 each object of the structure type shown above. When looking
18182 for a place to allocate the "containing object" for `field2',
18183 the compiler simply tries to allocate a 64-bit "containing
18184 object" at each successive 32-bit boundary (starting at zero)
18185 until it finds a place to allocate that 64- bit field such that
18186 at least 31 contiguous (and previously unallocated) bits remain
18187 within that selected 64 bit field. (As it turns out, for the
18188 example above, the compiler finds it is OK to allocate the
18189 "containing object" 64-bit field at bit-offset zero within the
18190 structure type.)
18192 Here we attempt to work backwards from the limited set of facts
18193 we're given, and we try to deduce from those facts, where GCC
18194 must have believed that the containing object started (within
18195 the structure type). The value we deduce is then used (by the
18196 callers of this routine) to generate DW_AT_location and
18197 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18198 the case of DW_AT_location, regular fields as well). */
18200 /* Figure out the bit-distance from the start of the structure to
18201 the "deepest" bit of the bit-field. */
18202 deepest_bitpos = bitpos_int + field_size_in_bits;
18204 /* This is the tricky part. Use some fancy footwork to deduce
18205 where the lowest addressed bit of the containing object must
18206 be. */
18207 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18209 /* Round up to type_align by default. This works best for
18210 bitfields. */
18211 object_offset_in_bits
18212 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18214 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18216 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18218 /* Round up to decl_align instead. */
18219 object_offset_in_bits
18220 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18223 object_offset_in_bytes
18224 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18225 if (ctx->variant_part_offset == NULL_TREE)
18227 *cst_offset = object_offset_in_bytes.to_shwi ();
18228 return NULL;
18230 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18232 else
18233 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18234 tree_result = byte_position (decl);
18236 if (ctx->variant_part_offset != NULL_TREE)
18237 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18238 ctx->variant_part_offset, tree_result);
18240 /* If the byte offset is a constant, it's simplier to handle a native
18241 constant rather than a DWARF expression. */
18242 if (TREE_CODE (tree_result) == INTEGER_CST)
18244 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18245 return NULL;
18247 struct loc_descr_context loc_ctx = {
18248 ctx->struct_type, /* context_type */
18249 NULL_TREE, /* base_decl */
18250 NULL, /* dpi */
18251 false, /* placeholder_arg */
18252 false /* placeholder_seen */
18254 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18256 /* We want a DWARF expression: abort if we only have a location list with
18257 multiple elements. */
18258 if (!loc_result || !single_element_loc_list_p (loc_result))
18259 return NULL;
18260 else
18261 return loc_result->expr;
18264 /* The following routines define various Dwarf attributes and any data
18265 associated with them. */
18267 /* Add a location description attribute value to a DIE.
18269 This emits location attributes suitable for whole variables and
18270 whole parameters. Note that the location attributes for struct fields are
18271 generated by the routine `data_member_location_attribute' below. */
18273 static inline void
18274 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18275 dw_loc_list_ref descr)
18277 if (descr == 0)
18278 return;
18279 if (single_element_loc_list_p (descr))
18280 add_AT_loc (die, attr_kind, descr->expr);
18281 else
18282 add_AT_loc_list (die, attr_kind, descr);
18285 /* Add DW_AT_accessibility attribute to DIE if needed. */
18287 static void
18288 add_accessibility_attribute (dw_die_ref die, tree decl)
18290 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18291 children, otherwise the default is DW_ACCESS_public. In DWARF2
18292 the default has always been DW_ACCESS_public. */
18293 if (TREE_PROTECTED (decl))
18294 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18295 else if (TREE_PRIVATE (decl))
18297 if (dwarf_version == 2
18298 || die->die_parent == NULL
18299 || die->die_parent->die_tag != DW_TAG_class_type)
18300 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18302 else if (dwarf_version > 2
18303 && die->die_parent
18304 && die->die_parent->die_tag == DW_TAG_class_type)
18305 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18308 /* Attach the specialized form of location attribute used for data members of
18309 struct and union types. In the special case of a FIELD_DECL node which
18310 represents a bit-field, the "offset" part of this special location
18311 descriptor must indicate the distance in bytes from the lowest-addressed
18312 byte of the containing struct or union type to the lowest-addressed byte of
18313 the "containing object" for the bit-field. (See the `field_byte_offset'
18314 function above).
18316 For any given bit-field, the "containing object" is a hypothetical object
18317 (of some integral or enum type) within which the given bit-field lives. The
18318 type of this hypothetical "containing object" is always the same as the
18319 declared type of the individual bit-field itself (for GCC anyway... the
18320 DWARF spec doesn't actually mandate this). Note that it is the size (in
18321 bytes) of the hypothetical "containing object" which will be given in the
18322 DW_AT_byte_size attribute for this bit-field. (See the
18323 `byte_size_attribute' function below.) It is also used when calculating the
18324 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18325 function below.)
18327 CTX is required: see the comment for VLR_CONTEXT. */
18329 static void
18330 add_data_member_location_attribute (dw_die_ref die,
18331 tree decl,
18332 struct vlr_context *ctx)
18334 HOST_WIDE_INT offset;
18335 dw_loc_descr_ref loc_descr = 0;
18337 if (TREE_CODE (decl) == TREE_BINFO)
18339 /* We're working on the TAG_inheritance for a base class. */
18340 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18342 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18343 aren't at a fixed offset from all (sub)objects of the same
18344 type. We need to extract the appropriate offset from our
18345 vtable. The following dwarf expression means
18347 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18349 This is specific to the V3 ABI, of course. */
18351 dw_loc_descr_ref tmp;
18353 /* Make a copy of the object address. */
18354 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18355 add_loc_descr (&loc_descr, tmp);
18357 /* Extract the vtable address. */
18358 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18359 add_loc_descr (&loc_descr, tmp);
18361 /* Calculate the address of the offset. */
18362 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18363 gcc_assert (offset < 0);
18365 tmp = int_loc_descriptor (-offset);
18366 add_loc_descr (&loc_descr, tmp);
18367 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18368 add_loc_descr (&loc_descr, tmp);
18370 /* Extract the offset. */
18371 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18372 add_loc_descr (&loc_descr, tmp);
18374 /* Add it to the object address. */
18375 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18376 add_loc_descr (&loc_descr, tmp);
18378 else
18379 offset = tree_to_shwi (BINFO_OFFSET (decl));
18381 else
18383 loc_descr = field_byte_offset (decl, ctx, &offset);
18385 /* If loc_descr is available then we know the field offset is dynamic.
18386 However, GDB does not handle dynamic field offsets very well at the
18387 moment. */
18388 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18390 loc_descr = NULL;
18391 offset = 0;
18394 /* Data member location evalutation starts with the base address on the
18395 stack. Compute the field offset and add it to this base address. */
18396 else if (loc_descr != NULL)
18397 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18400 if (! loc_descr)
18402 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18403 e.g. GDB only added support to it in November 2016. For DWARF5
18404 we need newer debug info consumers anyway. We might change this
18405 to dwarf_version >= 4 once most consumers catched up. */
18406 if (dwarf_version >= 5
18407 && TREE_CODE (decl) == FIELD_DECL
18408 && DECL_BIT_FIELD_TYPE (decl))
18410 tree off = bit_position (decl);
18411 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18413 remove_AT (die, DW_AT_byte_size);
18414 remove_AT (die, DW_AT_bit_offset);
18415 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18416 return;
18419 if (dwarf_version > 2)
18421 /* Don't need to output a location expression, just the constant. */
18422 if (offset < 0)
18423 add_AT_int (die, DW_AT_data_member_location, offset);
18424 else
18425 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18426 return;
18428 else
18430 enum dwarf_location_atom op;
18432 /* The DWARF2 standard says that we should assume that the structure
18433 address is already on the stack, so we can specify a structure
18434 field address by using DW_OP_plus_uconst. */
18435 op = DW_OP_plus_uconst;
18436 loc_descr = new_loc_descr (op, offset, 0);
18440 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18443 /* Writes integer values to dw_vec_const array. */
18445 static void
18446 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18448 while (size != 0)
18450 *dest++ = val & 0xff;
18451 val >>= 8;
18452 --size;
18456 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18458 static HOST_WIDE_INT
18459 extract_int (const unsigned char *src, unsigned int size)
18461 HOST_WIDE_INT val = 0;
18463 src += size;
18464 while (size != 0)
18466 val <<= 8;
18467 val |= *--src & 0xff;
18468 --size;
18470 return val;
18473 /* Writes wide_int values to dw_vec_const array. */
18475 static void
18476 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18478 int i;
18480 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18482 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18483 return;
18486 /* We'd have to extend this code to support odd sizes. */
18487 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18489 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18491 if (WORDS_BIG_ENDIAN)
18492 for (i = n - 1; i >= 0; i--)
18494 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18495 dest += sizeof (HOST_WIDE_INT);
18497 else
18498 for (i = 0; i < n; i++)
18500 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18501 dest += sizeof (HOST_WIDE_INT);
18505 /* Writes floating point values to dw_vec_const array. */
18507 static void
18508 insert_float (const_rtx rtl, unsigned char *array)
18510 long val[4];
18511 int i;
18513 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18515 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18516 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18518 insert_int (val[i], 4, array);
18519 array += 4;
18523 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18524 does not have a "location" either in memory or in a register. These
18525 things can arise in GNU C when a constant is passed as an actual parameter
18526 to an inlined function. They can also arise in C++ where declared
18527 constants do not necessarily get memory "homes". */
18529 static bool
18530 add_const_value_attribute (dw_die_ref die, rtx rtl)
18532 switch (GET_CODE (rtl))
18534 case CONST_INT:
18536 HOST_WIDE_INT val = INTVAL (rtl);
18538 if (val < 0)
18539 add_AT_int (die, DW_AT_const_value, val);
18540 else
18541 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18543 return true;
18545 case CONST_WIDE_INT:
18547 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18548 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18549 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18550 wide_int w = wi::zext (w1, prec);
18551 add_AT_wide (die, DW_AT_const_value, w);
18553 return true;
18555 case CONST_DOUBLE:
18556 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18557 floating-point constant. A CONST_DOUBLE is used whenever the
18558 constant requires more than one word in order to be adequately
18559 represented. */
18561 machine_mode mode = GET_MODE (rtl);
18563 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18564 add_AT_double (die, DW_AT_const_value,
18565 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18566 else
18568 unsigned int length = GET_MODE_SIZE (mode);
18569 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18571 insert_float (rtl, array);
18572 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18575 return true;
18577 case CONST_VECTOR:
18579 machine_mode mode = GET_MODE (rtl);
18580 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18581 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18582 unsigned char *array
18583 = ggc_vec_alloc<unsigned char> (length * elt_size);
18584 unsigned int i;
18585 unsigned char *p;
18586 machine_mode imode = GET_MODE_INNER (mode);
18588 switch (GET_MODE_CLASS (mode))
18590 case MODE_VECTOR_INT:
18591 for (i = 0, p = array; i < length; i++, p += elt_size)
18593 rtx elt = CONST_VECTOR_ELT (rtl, i);
18594 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18596 break;
18598 case MODE_VECTOR_FLOAT:
18599 for (i = 0, p = array; i < length; i++, p += elt_size)
18601 rtx elt = CONST_VECTOR_ELT (rtl, i);
18602 insert_float (elt, p);
18604 break;
18606 default:
18607 gcc_unreachable ();
18610 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18612 return true;
18614 case CONST_STRING:
18615 if (dwarf_version >= 4 || !dwarf_strict)
18617 dw_loc_descr_ref loc_result;
18618 resolve_one_addr (&rtl);
18619 rtl_addr:
18620 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18621 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18622 add_AT_loc (die, DW_AT_location, loc_result);
18623 vec_safe_push (used_rtx_array, rtl);
18624 return true;
18626 return false;
18628 case CONST:
18629 if (CONSTANT_P (XEXP (rtl, 0)))
18630 return add_const_value_attribute (die, XEXP (rtl, 0));
18631 /* FALLTHROUGH */
18632 case SYMBOL_REF:
18633 if (!const_ok_for_output (rtl))
18634 return false;
18635 /* FALLTHROUGH */
18636 case LABEL_REF:
18637 if (dwarf_version >= 4 || !dwarf_strict)
18638 goto rtl_addr;
18639 return false;
18641 case PLUS:
18642 /* In cases where an inlined instance of an inline function is passed
18643 the address of an `auto' variable (which is local to the caller) we
18644 can get a situation where the DECL_RTL of the artificial local
18645 variable (for the inlining) which acts as a stand-in for the
18646 corresponding formal parameter (of the inline function) will look
18647 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18648 exactly a compile-time constant expression, but it isn't the address
18649 of the (artificial) local variable either. Rather, it represents the
18650 *value* which the artificial local variable always has during its
18651 lifetime. We currently have no way to represent such quasi-constant
18652 values in Dwarf, so for now we just punt and generate nothing. */
18653 return false;
18655 case HIGH:
18656 case CONST_FIXED:
18657 return false;
18659 case MEM:
18660 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18661 && MEM_READONLY_P (rtl)
18662 && GET_MODE (rtl) == BLKmode)
18664 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18665 return true;
18667 return false;
18669 default:
18670 /* No other kinds of rtx should be possible here. */
18671 gcc_unreachable ();
18673 return false;
18676 /* Determine whether the evaluation of EXPR references any variables
18677 or functions which aren't otherwise used (and therefore may not be
18678 output). */
18679 static tree
18680 reference_to_unused (tree * tp, int * walk_subtrees,
18681 void * data ATTRIBUTE_UNUSED)
18683 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18684 *walk_subtrees = 0;
18686 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18687 && ! TREE_ASM_WRITTEN (*tp))
18688 return *tp;
18689 /* ??? The C++ FE emits debug information for using decls, so
18690 putting gcc_unreachable here falls over. See PR31899. For now
18691 be conservative. */
18692 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18693 return *tp;
18694 else if (VAR_P (*tp))
18696 varpool_node *node = varpool_node::get (*tp);
18697 if (!node || !node->definition)
18698 return *tp;
18700 else if (TREE_CODE (*tp) == FUNCTION_DECL
18701 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18703 /* The call graph machinery must have finished analyzing,
18704 optimizing and gimplifying the CU by now.
18705 So if *TP has no call graph node associated
18706 to it, it means *TP will not be emitted. */
18707 if (!cgraph_node::get (*tp))
18708 return *tp;
18710 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18711 return *tp;
18713 return NULL_TREE;
18716 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18717 for use in a later add_const_value_attribute call. */
18719 static rtx
18720 rtl_for_decl_init (tree init, tree type)
18722 rtx rtl = NULL_RTX;
18724 STRIP_NOPS (init);
18726 /* If a variable is initialized with a string constant without embedded
18727 zeros, build CONST_STRING. */
18728 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18730 tree enttype = TREE_TYPE (type);
18731 tree domain = TYPE_DOMAIN (type);
18732 machine_mode mode = TYPE_MODE (enttype);
18734 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18735 && domain
18736 && integer_zerop (TYPE_MIN_VALUE (domain))
18737 && compare_tree_int (TYPE_MAX_VALUE (domain),
18738 TREE_STRING_LENGTH (init) - 1) == 0
18739 && ((size_t) TREE_STRING_LENGTH (init)
18740 == strlen (TREE_STRING_POINTER (init)) + 1))
18742 rtl = gen_rtx_CONST_STRING (VOIDmode,
18743 ggc_strdup (TREE_STRING_POINTER (init)));
18744 rtl = gen_rtx_MEM (BLKmode, rtl);
18745 MEM_READONLY_P (rtl) = 1;
18748 /* Other aggregates, and complex values, could be represented using
18749 CONCAT: FIXME! */
18750 else if (AGGREGATE_TYPE_P (type)
18751 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18752 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18753 || TREE_CODE (type) == COMPLEX_TYPE)
18755 /* Vectors only work if their mode is supported by the target.
18756 FIXME: generic vectors ought to work too. */
18757 else if (TREE_CODE (type) == VECTOR_TYPE
18758 && !VECTOR_MODE_P (TYPE_MODE (type)))
18760 /* If the initializer is something that we know will expand into an
18761 immediate RTL constant, expand it now. We must be careful not to
18762 reference variables which won't be output. */
18763 else if (initializer_constant_valid_p (init, type)
18764 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18766 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18767 possible. */
18768 if (TREE_CODE (type) == VECTOR_TYPE)
18769 switch (TREE_CODE (init))
18771 case VECTOR_CST:
18772 break;
18773 case CONSTRUCTOR:
18774 if (TREE_CONSTANT (init))
18776 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18777 bool constant_p = true;
18778 tree value;
18779 unsigned HOST_WIDE_INT ix;
18781 /* Even when ctor is constant, it might contain non-*_CST
18782 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18783 belong into VECTOR_CST nodes. */
18784 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18785 if (!CONSTANT_CLASS_P (value))
18787 constant_p = false;
18788 break;
18791 if (constant_p)
18793 init = build_vector_from_ctor (type, elts);
18794 break;
18797 /* FALLTHRU */
18799 default:
18800 return NULL;
18803 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18805 /* If expand_expr returns a MEM, it wasn't immediate. */
18806 gcc_assert (!rtl || !MEM_P (rtl));
18809 return rtl;
18812 /* Generate RTL for the variable DECL to represent its location. */
18814 static rtx
18815 rtl_for_decl_location (tree decl)
18817 rtx rtl;
18819 /* Here we have to decide where we are going to say the parameter "lives"
18820 (as far as the debugger is concerned). We only have a couple of
18821 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18823 DECL_RTL normally indicates where the parameter lives during most of the
18824 activation of the function. If optimization is enabled however, this
18825 could be either NULL or else a pseudo-reg. Both of those cases indicate
18826 that the parameter doesn't really live anywhere (as far as the code
18827 generation parts of GCC are concerned) during most of the function's
18828 activation. That will happen (for example) if the parameter is never
18829 referenced within the function.
18831 We could just generate a location descriptor here for all non-NULL
18832 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18833 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18834 where DECL_RTL is NULL or is a pseudo-reg.
18836 Note however that we can only get away with using DECL_INCOMING_RTL as
18837 a backup substitute for DECL_RTL in certain limited cases. In cases
18838 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18839 we can be sure that the parameter was passed using the same type as it is
18840 declared to have within the function, and that its DECL_INCOMING_RTL
18841 points us to a place where a value of that type is passed.
18843 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18844 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18845 because in these cases DECL_INCOMING_RTL points us to a value of some
18846 type which is *different* from the type of the parameter itself. Thus,
18847 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18848 such cases, the debugger would end up (for example) trying to fetch a
18849 `float' from a place which actually contains the first part of a
18850 `double'. That would lead to really incorrect and confusing
18851 output at debug-time.
18853 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18854 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18855 are a couple of exceptions however. On little-endian machines we can
18856 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18857 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18858 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18859 when (on a little-endian machine) a non-prototyped function has a
18860 parameter declared to be of type `short' or `char'. In such cases,
18861 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18862 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18863 passed `int' value. If the debugger then uses that address to fetch
18864 a `short' or a `char' (on a little-endian machine) the result will be
18865 the correct data, so we allow for such exceptional cases below.
18867 Note that our goal here is to describe the place where the given formal
18868 parameter lives during most of the function's activation (i.e. between the
18869 end of the prologue and the start of the epilogue). We'll do that as best
18870 as we can. Note however that if the given formal parameter is modified
18871 sometime during the execution of the function, then a stack backtrace (at
18872 debug-time) will show the function as having been called with the *new*
18873 value rather than the value which was originally passed in. This happens
18874 rarely enough that it is not a major problem, but it *is* a problem, and
18875 I'd like to fix it.
18877 A future version of dwarf2out.c may generate two additional attributes for
18878 any given DW_TAG_formal_parameter DIE which will describe the "passed
18879 type" and the "passed location" for the given formal parameter in addition
18880 to the attributes we now generate to indicate the "declared type" and the
18881 "active location" for each parameter. This additional set of attributes
18882 could be used by debuggers for stack backtraces. Separately, note that
18883 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18884 This happens (for example) for inlined-instances of inline function formal
18885 parameters which are never referenced. This really shouldn't be
18886 happening. All PARM_DECL nodes should get valid non-NULL
18887 DECL_INCOMING_RTL values. FIXME. */
18889 /* Use DECL_RTL as the "location" unless we find something better. */
18890 rtl = DECL_RTL_IF_SET (decl);
18892 /* When generating abstract instances, ignore everything except
18893 constants, symbols living in memory, and symbols living in
18894 fixed registers. */
18895 if (! reload_completed)
18897 if (rtl
18898 && (CONSTANT_P (rtl)
18899 || (MEM_P (rtl)
18900 && CONSTANT_P (XEXP (rtl, 0)))
18901 || (REG_P (rtl)
18902 && VAR_P (decl)
18903 && TREE_STATIC (decl))))
18905 rtl = targetm.delegitimize_address (rtl);
18906 return rtl;
18908 rtl = NULL_RTX;
18910 else if (TREE_CODE (decl) == PARM_DECL)
18912 if (rtl == NULL_RTX
18913 || is_pseudo_reg (rtl)
18914 || (MEM_P (rtl)
18915 && is_pseudo_reg (XEXP (rtl, 0))
18916 && DECL_INCOMING_RTL (decl)
18917 && MEM_P (DECL_INCOMING_RTL (decl))
18918 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18920 tree declared_type = TREE_TYPE (decl);
18921 tree passed_type = DECL_ARG_TYPE (decl);
18922 machine_mode dmode = TYPE_MODE (declared_type);
18923 machine_mode pmode = TYPE_MODE (passed_type);
18925 /* This decl represents a formal parameter which was optimized out.
18926 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18927 all cases where (rtl == NULL_RTX) just below. */
18928 if (dmode == pmode)
18929 rtl = DECL_INCOMING_RTL (decl);
18930 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18931 && SCALAR_INT_MODE_P (dmode)
18932 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18933 && DECL_INCOMING_RTL (decl))
18935 rtx inc = DECL_INCOMING_RTL (decl);
18936 if (REG_P (inc))
18937 rtl = inc;
18938 else if (MEM_P (inc))
18940 if (BYTES_BIG_ENDIAN)
18941 rtl = adjust_address_nv (inc, dmode,
18942 GET_MODE_SIZE (pmode)
18943 - GET_MODE_SIZE (dmode));
18944 else
18945 rtl = inc;
18950 /* If the parm was passed in registers, but lives on the stack, then
18951 make a big endian correction if the mode of the type of the
18952 parameter is not the same as the mode of the rtl. */
18953 /* ??? This is the same series of checks that are made in dbxout.c before
18954 we reach the big endian correction code there. It isn't clear if all
18955 of these checks are necessary here, but keeping them all is the safe
18956 thing to do. */
18957 else if (MEM_P (rtl)
18958 && XEXP (rtl, 0) != const0_rtx
18959 && ! CONSTANT_P (XEXP (rtl, 0))
18960 /* Not passed in memory. */
18961 && !MEM_P (DECL_INCOMING_RTL (decl))
18962 /* Not passed by invisible reference. */
18963 && (!REG_P (XEXP (rtl, 0))
18964 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18965 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18966 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18967 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18968 #endif
18970 /* Big endian correction check. */
18971 && BYTES_BIG_ENDIAN
18972 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18973 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18974 < UNITS_PER_WORD))
18976 machine_mode addr_mode = get_address_mode (rtl);
18977 int offset = (UNITS_PER_WORD
18978 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18980 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18981 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18984 else if (VAR_P (decl)
18985 && rtl
18986 && MEM_P (rtl)
18987 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18988 && BYTES_BIG_ENDIAN)
18990 machine_mode addr_mode = get_address_mode (rtl);
18991 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18992 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18994 /* If a variable is declared "register" yet is smaller than
18995 a register, then if we store the variable to memory, it
18996 looks like we're storing a register-sized value, when in
18997 fact we are not. We need to adjust the offset of the
18998 storage location to reflect the actual value's bytes,
18999 else gdb will not be able to display it. */
19000 if (rsize > dsize)
19001 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19002 plus_constant (addr_mode, XEXP (rtl, 0),
19003 rsize - dsize));
19006 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19007 and will have been substituted directly into all expressions that use it.
19008 C does not have such a concept, but C++ and other languages do. */
19009 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19010 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19012 if (rtl)
19013 rtl = targetm.delegitimize_address (rtl);
19015 /* If we don't look past the constant pool, we risk emitting a
19016 reference to a constant pool entry that isn't referenced from
19017 code, and thus is not emitted. */
19018 if (rtl)
19019 rtl = avoid_constant_pool_reference (rtl);
19021 /* Try harder to get a rtl. If this symbol ends up not being emitted
19022 in the current CU, resolve_addr will remove the expression referencing
19023 it. */
19024 if (rtl == NULL_RTX
19025 && VAR_P (decl)
19026 && !DECL_EXTERNAL (decl)
19027 && TREE_STATIC (decl)
19028 && DECL_NAME (decl)
19029 && !DECL_HARD_REGISTER (decl)
19030 && DECL_MODE (decl) != VOIDmode)
19032 rtl = make_decl_rtl_for_debug (decl);
19033 if (!MEM_P (rtl)
19034 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19035 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19036 rtl = NULL_RTX;
19039 return rtl;
19042 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19043 returned. If so, the decl for the COMMON block is returned, and the
19044 value is the offset into the common block for the symbol. */
19046 static tree
19047 fortran_common (tree decl, HOST_WIDE_INT *value)
19049 tree val_expr, cvar;
19050 machine_mode mode;
19051 HOST_WIDE_INT bitsize, bitpos;
19052 tree offset;
19053 int unsignedp, reversep, volatilep = 0;
19055 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19056 it does not have a value (the offset into the common area), or if it
19057 is thread local (as opposed to global) then it isn't common, and shouldn't
19058 be handled as such. */
19059 if (!VAR_P (decl)
19060 || !TREE_STATIC (decl)
19061 || !DECL_HAS_VALUE_EXPR_P (decl)
19062 || !is_fortran ())
19063 return NULL_TREE;
19065 val_expr = DECL_VALUE_EXPR (decl);
19066 if (TREE_CODE (val_expr) != COMPONENT_REF)
19067 return NULL_TREE;
19069 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19070 &unsignedp, &reversep, &volatilep);
19072 if (cvar == NULL_TREE
19073 || !VAR_P (cvar)
19074 || DECL_ARTIFICIAL (cvar)
19075 || !TREE_PUBLIC (cvar))
19076 return NULL_TREE;
19078 *value = 0;
19079 if (offset != NULL)
19081 if (!tree_fits_shwi_p (offset))
19082 return NULL_TREE;
19083 *value = tree_to_shwi (offset);
19085 if (bitpos != 0)
19086 *value += bitpos / BITS_PER_UNIT;
19088 return cvar;
19091 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19092 data attribute for a variable or a parameter. We generate the
19093 DW_AT_const_value attribute only in those cases where the given variable
19094 or parameter does not have a true "location" either in memory or in a
19095 register. This can happen (for example) when a constant is passed as an
19096 actual argument in a call to an inline function. (It's possible that
19097 these things can crop up in other ways also.) Note that one type of
19098 constant value which can be passed into an inlined function is a constant
19099 pointer. This can happen for example if an actual argument in an inlined
19100 function call evaluates to a compile-time constant address.
19102 CACHE_P is true if it is worth caching the location list for DECL,
19103 so that future calls can reuse it rather than regenerate it from scratch.
19104 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19105 since we will need to refer to them each time the function is inlined. */
19107 static bool
19108 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19110 rtx rtl;
19111 dw_loc_list_ref list;
19112 var_loc_list *loc_list;
19113 cached_dw_loc_list *cache;
19115 if (early_dwarf)
19116 return false;
19118 if (TREE_CODE (decl) == ERROR_MARK)
19119 return false;
19121 if (get_AT (die, DW_AT_location)
19122 || get_AT (die, DW_AT_const_value))
19123 return true;
19125 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19126 || TREE_CODE (decl) == RESULT_DECL);
19128 /* Try to get some constant RTL for this decl, and use that as the value of
19129 the location. */
19131 rtl = rtl_for_decl_location (decl);
19132 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19133 && add_const_value_attribute (die, rtl))
19134 return true;
19136 /* See if we have single element location list that is equivalent to
19137 a constant value. That way we are better to use add_const_value_attribute
19138 rather than expanding constant value equivalent. */
19139 loc_list = lookup_decl_loc (decl);
19140 if (loc_list
19141 && loc_list->first
19142 && loc_list->first->next == NULL
19143 && NOTE_P (loc_list->first->loc)
19144 && NOTE_VAR_LOCATION (loc_list->first->loc)
19145 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19147 struct var_loc_node *node;
19149 node = loc_list->first;
19150 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19151 if (GET_CODE (rtl) == EXPR_LIST)
19152 rtl = XEXP (rtl, 0);
19153 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19154 && add_const_value_attribute (die, rtl))
19155 return true;
19157 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19158 list several times. See if we've already cached the contents. */
19159 list = NULL;
19160 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19161 cache_p = false;
19162 if (cache_p)
19164 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19165 if (cache)
19166 list = cache->loc_list;
19168 if (list == NULL)
19170 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19171 NULL);
19172 /* It is usually worth caching this result if the decl is from
19173 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19174 if (cache_p && list && list->dw_loc_next)
19176 cached_dw_loc_list **slot
19177 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19178 DECL_UID (decl),
19179 INSERT);
19180 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19181 cache->decl_id = DECL_UID (decl);
19182 cache->loc_list = list;
19183 *slot = cache;
19186 if (list)
19188 add_AT_location_description (die, DW_AT_location, list);
19189 return true;
19191 /* None of that worked, so it must not really have a location;
19192 try adding a constant value attribute from the DECL_INITIAL. */
19193 return tree_add_const_value_attribute_for_decl (die, decl);
19196 /* Helper function for tree_add_const_value_attribute. Natively encode
19197 initializer INIT into an array. Return true if successful. */
19199 static bool
19200 native_encode_initializer (tree init, unsigned char *array, int size)
19202 tree type;
19204 if (init == NULL_TREE)
19205 return false;
19207 STRIP_NOPS (init);
19208 switch (TREE_CODE (init))
19210 case STRING_CST:
19211 type = TREE_TYPE (init);
19212 if (TREE_CODE (type) == ARRAY_TYPE)
19214 tree enttype = TREE_TYPE (type);
19215 machine_mode mode = TYPE_MODE (enttype);
19217 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19218 return false;
19219 if (int_size_in_bytes (type) != size)
19220 return false;
19221 if (size > TREE_STRING_LENGTH (init))
19223 memcpy (array, TREE_STRING_POINTER (init),
19224 TREE_STRING_LENGTH (init));
19225 memset (array + TREE_STRING_LENGTH (init),
19226 '\0', size - TREE_STRING_LENGTH (init));
19228 else
19229 memcpy (array, TREE_STRING_POINTER (init), size);
19230 return true;
19232 return false;
19233 case CONSTRUCTOR:
19234 type = TREE_TYPE (init);
19235 if (int_size_in_bytes (type) != size)
19236 return false;
19237 if (TREE_CODE (type) == ARRAY_TYPE)
19239 HOST_WIDE_INT min_index;
19240 unsigned HOST_WIDE_INT cnt;
19241 int curpos = 0, fieldsize;
19242 constructor_elt *ce;
19244 if (TYPE_DOMAIN (type) == NULL_TREE
19245 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19246 return false;
19248 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19249 if (fieldsize <= 0)
19250 return false;
19252 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19253 memset (array, '\0', size);
19254 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19256 tree val = ce->value;
19257 tree index = ce->index;
19258 int pos = curpos;
19259 if (index && TREE_CODE (index) == RANGE_EXPR)
19260 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19261 * fieldsize;
19262 else if (index)
19263 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19265 if (val)
19267 STRIP_NOPS (val);
19268 if (!native_encode_initializer (val, array + pos, fieldsize))
19269 return false;
19271 curpos = pos + fieldsize;
19272 if (index && TREE_CODE (index) == RANGE_EXPR)
19274 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19275 - tree_to_shwi (TREE_OPERAND (index, 0));
19276 while (count-- > 0)
19278 if (val)
19279 memcpy (array + curpos, array + pos, fieldsize);
19280 curpos += fieldsize;
19283 gcc_assert (curpos <= size);
19285 return true;
19287 else if (TREE_CODE (type) == RECORD_TYPE
19288 || TREE_CODE (type) == UNION_TYPE)
19290 tree field = NULL_TREE;
19291 unsigned HOST_WIDE_INT cnt;
19292 constructor_elt *ce;
19294 if (int_size_in_bytes (type) != size)
19295 return false;
19297 if (TREE_CODE (type) == RECORD_TYPE)
19298 field = TYPE_FIELDS (type);
19300 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19302 tree val = ce->value;
19303 int pos, fieldsize;
19305 if (ce->index != 0)
19306 field = ce->index;
19308 if (val)
19309 STRIP_NOPS (val);
19311 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19312 return false;
19314 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19315 && TYPE_DOMAIN (TREE_TYPE (field))
19316 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19317 return false;
19318 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19319 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19320 return false;
19321 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19322 pos = int_byte_position (field);
19323 gcc_assert (pos + fieldsize <= size);
19324 if (val && fieldsize != 0
19325 && !native_encode_initializer (val, array + pos, fieldsize))
19326 return false;
19328 return true;
19330 return false;
19331 case VIEW_CONVERT_EXPR:
19332 case NON_LVALUE_EXPR:
19333 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19334 default:
19335 return native_encode_expr (init, array, size) == size;
19339 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19340 attribute is the const value T. */
19342 static bool
19343 tree_add_const_value_attribute (dw_die_ref die, tree t)
19345 tree init;
19346 tree type = TREE_TYPE (t);
19347 rtx rtl;
19349 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19350 return false;
19352 init = t;
19353 gcc_assert (!DECL_P (init));
19355 if (! early_dwarf)
19357 rtl = rtl_for_decl_init (init, type);
19358 if (rtl)
19359 return add_const_value_attribute (die, rtl);
19361 /* If the host and target are sane, try harder. */
19362 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19363 && initializer_constant_valid_p (init, type))
19365 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19366 if (size > 0 && (int) size == size)
19368 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19370 if (native_encode_initializer (init, array, size))
19372 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19373 return true;
19375 ggc_free (array);
19378 return false;
19381 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19382 attribute is the const value of T, where T is an integral constant
19383 variable with static storage duration
19384 (so it can't be a PARM_DECL or a RESULT_DECL). */
19386 static bool
19387 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19390 if (!decl
19391 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19392 || (VAR_P (decl) && !TREE_STATIC (decl)))
19393 return false;
19395 if (TREE_READONLY (decl)
19396 && ! TREE_THIS_VOLATILE (decl)
19397 && DECL_INITIAL (decl))
19398 /* OK */;
19399 else
19400 return false;
19402 /* Don't add DW_AT_const_value if abstract origin already has one. */
19403 if (get_AT (var_die, DW_AT_const_value))
19404 return false;
19406 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19409 /* Convert the CFI instructions for the current function into a
19410 location list. This is used for DW_AT_frame_base when we targeting
19411 a dwarf2 consumer that does not support the dwarf3
19412 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19413 expressions. */
19415 static dw_loc_list_ref
19416 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19418 int ix;
19419 dw_fde_ref fde;
19420 dw_loc_list_ref list, *list_tail;
19421 dw_cfi_ref cfi;
19422 dw_cfa_location last_cfa, next_cfa;
19423 const char *start_label, *last_label, *section;
19424 dw_cfa_location remember;
19426 fde = cfun->fde;
19427 gcc_assert (fde != NULL);
19429 section = secname_for_decl (current_function_decl);
19430 list_tail = &list;
19431 list = NULL;
19433 memset (&next_cfa, 0, sizeof (next_cfa));
19434 next_cfa.reg = INVALID_REGNUM;
19435 remember = next_cfa;
19437 start_label = fde->dw_fde_begin;
19439 /* ??? Bald assumption that the CIE opcode list does not contain
19440 advance opcodes. */
19441 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19442 lookup_cfa_1 (cfi, &next_cfa, &remember);
19444 last_cfa = next_cfa;
19445 last_label = start_label;
19447 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19449 /* If the first partition contained no CFI adjustments, the
19450 CIE opcodes apply to the whole first partition. */
19451 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19452 fde->dw_fde_begin, fde->dw_fde_end, section);
19453 list_tail =&(*list_tail)->dw_loc_next;
19454 start_label = last_label = fde->dw_fde_second_begin;
19457 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19459 switch (cfi->dw_cfi_opc)
19461 case DW_CFA_set_loc:
19462 case DW_CFA_advance_loc1:
19463 case DW_CFA_advance_loc2:
19464 case DW_CFA_advance_loc4:
19465 if (!cfa_equal_p (&last_cfa, &next_cfa))
19467 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19468 start_label, last_label, section);
19470 list_tail = &(*list_tail)->dw_loc_next;
19471 last_cfa = next_cfa;
19472 start_label = last_label;
19474 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19475 break;
19477 case DW_CFA_advance_loc:
19478 /* The encoding is complex enough that we should never emit this. */
19479 gcc_unreachable ();
19481 default:
19482 lookup_cfa_1 (cfi, &next_cfa, &remember);
19483 break;
19485 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19487 if (!cfa_equal_p (&last_cfa, &next_cfa))
19489 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19490 start_label, last_label, section);
19492 list_tail = &(*list_tail)->dw_loc_next;
19493 last_cfa = next_cfa;
19494 start_label = last_label;
19496 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19497 start_label, fde->dw_fde_end, section);
19498 list_tail = &(*list_tail)->dw_loc_next;
19499 start_label = last_label = fde->dw_fde_second_begin;
19503 if (!cfa_equal_p (&last_cfa, &next_cfa))
19505 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19506 start_label, last_label, section);
19507 list_tail = &(*list_tail)->dw_loc_next;
19508 start_label = last_label;
19511 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19512 start_label,
19513 fde->dw_fde_second_begin
19514 ? fde->dw_fde_second_end : fde->dw_fde_end,
19515 section);
19517 if (list && list->dw_loc_next)
19518 gen_llsym (list);
19520 return list;
19523 /* Compute a displacement from the "steady-state frame pointer" to the
19524 frame base (often the same as the CFA), and store it in
19525 frame_pointer_fb_offset. OFFSET is added to the displacement
19526 before the latter is negated. */
19528 static void
19529 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19531 rtx reg, elim;
19533 #ifdef FRAME_POINTER_CFA_OFFSET
19534 reg = frame_pointer_rtx;
19535 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19536 #else
19537 reg = arg_pointer_rtx;
19538 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19539 #endif
19541 elim = (ira_use_lra_p
19542 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19543 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19544 if (GET_CODE (elim) == PLUS)
19546 offset += INTVAL (XEXP (elim, 1));
19547 elim = XEXP (elim, 0);
19550 frame_pointer_fb_offset = -offset;
19552 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19553 in which to eliminate. This is because it's stack pointer isn't
19554 directly accessible as a register within the ISA. To work around
19555 this, assume that while we cannot provide a proper value for
19556 frame_pointer_fb_offset, we won't need one either. */
19557 frame_pointer_fb_offset_valid
19558 = ((SUPPORTS_STACK_ALIGNMENT
19559 && (elim == hard_frame_pointer_rtx
19560 || elim == stack_pointer_rtx))
19561 || elim == (frame_pointer_needed
19562 ? hard_frame_pointer_rtx
19563 : stack_pointer_rtx));
19566 /* Generate a DW_AT_name attribute given some string value to be included as
19567 the value of the attribute. */
19569 static void
19570 add_name_attribute (dw_die_ref die, const char *name_string)
19572 if (name_string != NULL && *name_string != 0)
19574 if (demangle_name_func)
19575 name_string = (*demangle_name_func) (name_string);
19577 add_AT_string (die, DW_AT_name, name_string);
19581 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19582 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19583 of TYPE accordingly.
19585 ??? This is a temporary measure until after we're able to generate
19586 regular DWARF for the complex Ada type system. */
19588 static void
19589 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19590 dw_die_ref context_die)
19592 tree dtype;
19593 dw_die_ref dtype_die;
19595 if (!lang_hooks.types.descriptive_type)
19596 return;
19598 dtype = lang_hooks.types.descriptive_type (type);
19599 if (!dtype)
19600 return;
19602 dtype_die = lookup_type_die (dtype);
19603 if (!dtype_die)
19605 gen_type_die (dtype, context_die);
19606 dtype_die = lookup_type_die (dtype);
19607 gcc_assert (dtype_die);
19610 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19613 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19615 static const char *
19616 comp_dir_string (void)
19618 const char *wd;
19619 char *wd1;
19620 static const char *cached_wd = NULL;
19622 if (cached_wd != NULL)
19623 return cached_wd;
19625 wd = get_src_pwd ();
19626 if (wd == NULL)
19627 return NULL;
19629 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19631 int wdlen;
19633 wdlen = strlen (wd);
19634 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19635 strcpy (wd1, wd);
19636 wd1 [wdlen] = DIR_SEPARATOR;
19637 wd1 [wdlen + 1] = 0;
19638 wd = wd1;
19641 cached_wd = remap_debug_filename (wd);
19642 return cached_wd;
19645 /* Generate a DW_AT_comp_dir attribute for DIE. */
19647 static void
19648 add_comp_dir_attribute (dw_die_ref die)
19650 const char * wd = comp_dir_string ();
19651 if (wd != NULL)
19652 add_AT_string (die, DW_AT_comp_dir, wd);
19655 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19656 pointer computation, ...), output a representation for that bound according
19657 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19658 loc_list_from_tree for the meaning of CONTEXT. */
19660 static void
19661 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19662 int forms, struct loc_descr_context *context)
19664 dw_die_ref context_die, decl_die;
19665 dw_loc_list_ref list;
19666 bool strip_conversions = true;
19667 bool placeholder_seen = false;
19669 while (strip_conversions)
19670 switch (TREE_CODE (value))
19672 case ERROR_MARK:
19673 case SAVE_EXPR:
19674 return;
19676 CASE_CONVERT:
19677 case VIEW_CONVERT_EXPR:
19678 value = TREE_OPERAND (value, 0);
19679 break;
19681 default:
19682 strip_conversions = false;
19683 break;
19686 /* If possible and permitted, output the attribute as a constant. */
19687 if ((forms & dw_scalar_form_constant) != 0
19688 && TREE_CODE (value) == INTEGER_CST)
19690 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19692 /* If HOST_WIDE_INT is big enough then represent the bound as
19693 a constant value. We need to choose a form based on
19694 whether the type is signed or unsigned. We cannot just
19695 call add_AT_unsigned if the value itself is positive
19696 (add_AT_unsigned might add the unsigned value encoded as
19697 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19698 bounds type and then sign extend any unsigned values found
19699 for signed types. This is needed only for
19700 DW_AT_{lower,upper}_bound, since for most other attributes,
19701 consumers will treat DW_FORM_data[1248] as unsigned values,
19702 regardless of the underlying type. */
19703 if (prec <= HOST_BITS_PER_WIDE_INT
19704 || tree_fits_uhwi_p (value))
19706 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19707 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19708 else
19709 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19711 else
19712 /* Otherwise represent the bound as an unsigned value with
19713 the precision of its type. The precision and signedness
19714 of the type will be necessary to re-interpret it
19715 unambiguously. */
19716 add_AT_wide (die, attr, value);
19717 return;
19720 /* Otherwise, if it's possible and permitted too, output a reference to
19721 another DIE. */
19722 if ((forms & dw_scalar_form_reference) != 0)
19724 tree decl = NULL_TREE;
19726 /* Some type attributes reference an outer type. For instance, the upper
19727 bound of an array may reference an embedding record (this happens in
19728 Ada). */
19729 if (TREE_CODE (value) == COMPONENT_REF
19730 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19731 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19732 decl = TREE_OPERAND (value, 1);
19734 else if (VAR_P (value)
19735 || TREE_CODE (value) == PARM_DECL
19736 || TREE_CODE (value) == RESULT_DECL)
19737 decl = value;
19739 if (decl != NULL_TREE)
19741 dw_die_ref decl_die = lookup_decl_die (decl);
19743 /* ??? Can this happen, or should the variable have been bound
19744 first? Probably it can, since I imagine that we try to create
19745 the types of parameters in the order in which they exist in
19746 the list, and won't have created a forward reference to a
19747 later parameter. */
19748 if (decl_die != NULL)
19750 add_AT_die_ref (die, attr, decl_die);
19751 return;
19756 /* Last chance: try to create a stack operation procedure to evaluate the
19757 value. Do nothing if even that is not possible or permitted. */
19758 if ((forms & dw_scalar_form_exprloc) == 0)
19759 return;
19761 list = loc_list_from_tree (value, 2, context);
19762 if (context && context->placeholder_arg)
19764 placeholder_seen = context->placeholder_seen;
19765 context->placeholder_seen = false;
19767 if (list == NULL || single_element_loc_list_p (list))
19769 /* If this attribute is not a reference nor constant, it is
19770 a DWARF expression rather than location description. For that
19771 loc_list_from_tree (value, 0, &context) is needed. */
19772 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19773 if (list2 && single_element_loc_list_p (list2))
19775 if (placeholder_seen)
19777 struct dwarf_procedure_info dpi;
19778 dpi.fndecl = NULL_TREE;
19779 dpi.args_count = 1;
19780 if (!resolve_args_picking (list2->expr, 1, &dpi))
19781 return;
19783 add_AT_loc (die, attr, list2->expr);
19784 return;
19788 /* If that failed to give a single element location list, fall back to
19789 outputting this as a reference... still if permitted. */
19790 if (list == NULL
19791 || (forms & dw_scalar_form_reference) == 0
19792 || placeholder_seen)
19793 return;
19795 if (current_function_decl == 0)
19796 context_die = comp_unit_die ();
19797 else
19798 context_die = lookup_decl_die (current_function_decl);
19800 decl_die = new_die (DW_TAG_variable, context_die, value);
19801 add_AT_flag (decl_die, DW_AT_artificial, 1);
19802 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19803 context_die);
19804 add_AT_location_description (decl_die, DW_AT_location, list);
19805 add_AT_die_ref (die, attr, decl_die);
19808 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19809 default. */
19811 static int
19812 lower_bound_default (void)
19814 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19816 case DW_LANG_C:
19817 case DW_LANG_C89:
19818 case DW_LANG_C99:
19819 case DW_LANG_C11:
19820 case DW_LANG_C_plus_plus:
19821 case DW_LANG_C_plus_plus_11:
19822 case DW_LANG_C_plus_plus_14:
19823 case DW_LANG_ObjC:
19824 case DW_LANG_ObjC_plus_plus:
19825 return 0;
19826 case DW_LANG_Fortran77:
19827 case DW_LANG_Fortran90:
19828 case DW_LANG_Fortran95:
19829 case DW_LANG_Fortran03:
19830 case DW_LANG_Fortran08:
19831 return 1;
19832 case DW_LANG_UPC:
19833 case DW_LANG_D:
19834 case DW_LANG_Python:
19835 return dwarf_version >= 4 ? 0 : -1;
19836 case DW_LANG_Ada95:
19837 case DW_LANG_Ada83:
19838 case DW_LANG_Cobol74:
19839 case DW_LANG_Cobol85:
19840 case DW_LANG_Modula2:
19841 case DW_LANG_PLI:
19842 return dwarf_version >= 4 ? 1 : -1;
19843 default:
19844 return -1;
19848 /* Given a tree node describing an array bound (either lower or upper) output
19849 a representation for that bound. */
19851 static void
19852 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19853 tree bound, struct loc_descr_context *context)
19855 int dflt;
19857 while (1)
19858 switch (TREE_CODE (bound))
19860 /* Strip all conversions. */
19861 CASE_CONVERT:
19862 case VIEW_CONVERT_EXPR:
19863 bound = TREE_OPERAND (bound, 0);
19864 break;
19866 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19867 are even omitted when they are the default. */
19868 case INTEGER_CST:
19869 /* If the value for this bound is the default one, we can even omit the
19870 attribute. */
19871 if (bound_attr == DW_AT_lower_bound
19872 && tree_fits_shwi_p (bound)
19873 && (dflt = lower_bound_default ()) != -1
19874 && tree_to_shwi (bound) == dflt)
19875 return;
19877 /* FALLTHRU */
19879 default:
19880 /* Because of the complex interaction there can be with other GNAT
19881 encodings, GDB isn't ready yet to handle proper DWARF description
19882 for self-referencial subrange bounds: let GNAT encodings do the
19883 magic in such a case. */
19884 if (is_ada ()
19885 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19886 && contains_placeholder_p (bound))
19887 return;
19889 add_scalar_info (subrange_die, bound_attr, bound,
19890 dw_scalar_form_constant
19891 | dw_scalar_form_exprloc
19892 | dw_scalar_form_reference,
19893 context);
19894 return;
19898 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19899 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19900 Note that the block of subscript information for an array type also
19901 includes information about the element type of the given array type.
19903 This function reuses previously set type and bound information if
19904 available. */
19906 static void
19907 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19909 unsigned dimension_number;
19910 tree lower, upper;
19911 dw_die_ref child = type_die->die_child;
19913 for (dimension_number = 0;
19914 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19915 type = TREE_TYPE (type), dimension_number++)
19917 tree domain = TYPE_DOMAIN (type);
19919 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19920 break;
19922 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19923 and (in GNU C only) variable bounds. Handle all three forms
19924 here. */
19926 /* Find and reuse a previously generated DW_TAG_subrange_type if
19927 available.
19929 For multi-dimensional arrays, as we iterate through the
19930 various dimensions in the enclosing for loop above, we also
19931 iterate through the DIE children and pick at each
19932 DW_TAG_subrange_type previously generated (if available).
19933 Each child DW_TAG_subrange_type DIE describes the range of
19934 the current dimension. At this point we should have as many
19935 DW_TAG_subrange_type's as we have dimensions in the
19936 array. */
19937 dw_die_ref subrange_die = NULL;
19938 if (child)
19939 while (1)
19941 child = child->die_sib;
19942 if (child->die_tag == DW_TAG_subrange_type)
19943 subrange_die = child;
19944 if (child == type_die->die_child)
19946 /* If we wrapped around, stop looking next time. */
19947 child = NULL;
19948 break;
19950 if (child->die_tag == DW_TAG_subrange_type)
19951 break;
19953 if (!subrange_die)
19954 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19956 if (domain)
19958 /* We have an array type with specified bounds. */
19959 lower = TYPE_MIN_VALUE (domain);
19960 upper = TYPE_MAX_VALUE (domain);
19962 /* Define the index type. */
19963 if (TREE_TYPE (domain)
19964 && !get_AT (subrange_die, DW_AT_type))
19966 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19967 TREE_TYPE field. We can't emit debug info for this
19968 because it is an unnamed integral type. */
19969 if (TREE_CODE (domain) == INTEGER_TYPE
19970 && TYPE_NAME (domain) == NULL_TREE
19971 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19972 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19974 else
19975 add_type_attribute (subrange_die, TREE_TYPE (domain),
19976 TYPE_UNQUALIFIED, false, type_die);
19979 /* ??? If upper is NULL, the array has unspecified length,
19980 but it does have a lower bound. This happens with Fortran
19981 dimension arr(N:*)
19982 Since the debugger is definitely going to need to know N
19983 to produce useful results, go ahead and output the lower
19984 bound solo, and hope the debugger can cope. */
19986 if (!get_AT (subrange_die, DW_AT_lower_bound))
19987 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19988 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19989 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19992 /* Otherwise we have an array type with an unspecified length. The
19993 DWARF-2 spec does not say how to handle this; let's just leave out the
19994 bounds. */
19998 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20000 static void
20001 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20003 dw_die_ref decl_die;
20004 HOST_WIDE_INT size;
20005 dw_loc_descr_ref size_expr = NULL;
20007 switch (TREE_CODE (tree_node))
20009 case ERROR_MARK:
20010 size = 0;
20011 break;
20012 case ENUMERAL_TYPE:
20013 case RECORD_TYPE:
20014 case UNION_TYPE:
20015 case QUAL_UNION_TYPE:
20016 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20017 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20019 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20020 return;
20022 size_expr = type_byte_size (tree_node, &size);
20023 break;
20024 case FIELD_DECL:
20025 /* For a data member of a struct or union, the DW_AT_byte_size is
20026 generally given as the number of bytes normally allocated for an
20027 object of the *declared* type of the member itself. This is true
20028 even for bit-fields. */
20029 size = int_size_in_bytes (field_type (tree_node));
20030 break;
20031 default:
20032 gcc_unreachable ();
20035 /* Support for dynamically-sized objects was introduced by DWARFv3.
20036 At the moment, GDB does not handle variable byte sizes very well,
20037 though. */
20038 if ((dwarf_version >= 3 || !dwarf_strict)
20039 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20040 && size_expr != NULL)
20041 add_AT_loc (die, DW_AT_byte_size, size_expr);
20043 /* Note that `size' might be -1 when we get to this point. If it is, that
20044 indicates that the byte size of the entity in question is variable and
20045 that we could not generate a DWARF expression that computes it. */
20046 if (size >= 0)
20047 add_AT_unsigned (die, DW_AT_byte_size, size);
20050 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20051 alignment. */
20053 static void
20054 add_alignment_attribute (dw_die_ref die, tree tree_node)
20056 if (dwarf_version < 5 && dwarf_strict)
20057 return;
20059 unsigned align;
20061 if (DECL_P (tree_node))
20063 if (!DECL_USER_ALIGN (tree_node))
20064 return;
20066 align = DECL_ALIGN_UNIT (tree_node);
20068 else if (TYPE_P (tree_node))
20070 if (!TYPE_USER_ALIGN (tree_node))
20071 return;
20073 align = TYPE_ALIGN_UNIT (tree_node);
20075 else
20076 gcc_unreachable ();
20078 add_AT_unsigned (die, DW_AT_alignment, align);
20081 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20082 which specifies the distance in bits from the highest order bit of the
20083 "containing object" for the bit-field to the highest order bit of the
20084 bit-field itself.
20086 For any given bit-field, the "containing object" is a hypothetical object
20087 (of some integral or enum type) within which the given bit-field lives. The
20088 type of this hypothetical "containing object" is always the same as the
20089 declared type of the individual bit-field itself. The determination of the
20090 exact location of the "containing object" for a bit-field is rather
20091 complicated. It's handled by the `field_byte_offset' function (above).
20093 CTX is required: see the comment for VLR_CONTEXT.
20095 Note that it is the size (in bytes) of the hypothetical "containing object"
20096 which will be given in the DW_AT_byte_size attribute for this bit-field.
20097 (See `byte_size_attribute' above). */
20099 static inline void
20100 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20102 HOST_WIDE_INT object_offset_in_bytes;
20103 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20104 HOST_WIDE_INT bitpos_int;
20105 HOST_WIDE_INT highest_order_object_bit_offset;
20106 HOST_WIDE_INT highest_order_field_bit_offset;
20107 HOST_WIDE_INT bit_offset;
20109 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20111 /* Must be a field and a bit field. */
20112 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20114 /* We can't yet handle bit-fields whose offsets are variable, so if we
20115 encounter such things, just return without generating any attribute
20116 whatsoever. Likewise for variable or too large size. */
20117 if (! tree_fits_shwi_p (bit_position (decl))
20118 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20119 return;
20121 bitpos_int = int_bit_position (decl);
20123 /* Note that the bit offset is always the distance (in bits) from the
20124 highest-order bit of the "containing object" to the highest-order bit of
20125 the bit-field itself. Since the "high-order end" of any object or field
20126 is different on big-endian and little-endian machines, the computation
20127 below must take account of these differences. */
20128 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20129 highest_order_field_bit_offset = bitpos_int;
20131 if (! BYTES_BIG_ENDIAN)
20133 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20134 highest_order_object_bit_offset +=
20135 simple_type_size_in_bits (original_type);
20138 bit_offset
20139 = (! BYTES_BIG_ENDIAN
20140 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20141 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20143 if (bit_offset < 0)
20144 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20145 else
20146 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20149 /* For a FIELD_DECL node which represents a bit field, output an attribute
20150 which specifies the length in bits of the given field. */
20152 static inline void
20153 add_bit_size_attribute (dw_die_ref die, tree decl)
20155 /* Must be a field and a bit field. */
20156 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20157 && DECL_BIT_FIELD_TYPE (decl));
20159 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20160 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20163 /* If the compiled language is ANSI C, then add a 'prototyped'
20164 attribute, if arg types are given for the parameters of a function. */
20166 static inline void
20167 add_prototyped_attribute (dw_die_ref die, tree func_type)
20169 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20171 case DW_LANG_C:
20172 case DW_LANG_C89:
20173 case DW_LANG_C99:
20174 case DW_LANG_C11:
20175 case DW_LANG_ObjC:
20176 if (prototype_p (func_type))
20177 add_AT_flag (die, DW_AT_prototyped, 1);
20178 break;
20179 default:
20180 break;
20184 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20185 by looking in the type declaration, the object declaration equate table or
20186 the block mapping. */
20188 static inline dw_die_ref
20189 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20191 dw_die_ref origin_die = NULL;
20193 if (TREE_CODE (origin) != FUNCTION_DECL
20194 && TREE_CODE (origin) != BLOCK)
20196 /* We may have gotten separated from the block for the inlined
20197 function, if we're in an exception handler or some such; make
20198 sure that the abstract function has been written out.
20200 Doing this for nested functions is wrong, however; functions are
20201 distinct units, and our context might not even be inline. */
20202 tree fn = origin;
20204 if (TYPE_P (fn))
20205 fn = TYPE_STUB_DECL (fn);
20207 fn = decl_function_context (fn);
20208 if (fn)
20209 dwarf2out_abstract_function (fn);
20212 if (DECL_P (origin))
20213 origin_die = lookup_decl_die (origin);
20214 else if (TYPE_P (origin))
20215 origin_die = lookup_type_die (origin);
20216 else if (TREE_CODE (origin) == BLOCK)
20217 origin_die = BLOCK_DIE (origin);
20219 /* XXX: Functions that are never lowered don't always have correct block
20220 trees (in the case of java, they simply have no block tree, in some other
20221 languages). For these functions, there is nothing we can really do to
20222 output correct debug info for inlined functions in all cases. Rather
20223 than die, we'll just produce deficient debug info now, in that we will
20224 have variables without a proper abstract origin. In the future, when all
20225 functions are lowered, we should re-add a gcc_assert (origin_die)
20226 here. */
20228 if (origin_die)
20229 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20230 return origin_die;
20233 /* We do not currently support the pure_virtual attribute. */
20235 static inline void
20236 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20238 if (DECL_VINDEX (func_decl))
20240 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20242 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20243 add_AT_loc (die, DW_AT_vtable_elem_location,
20244 new_loc_descr (DW_OP_constu,
20245 tree_to_shwi (DECL_VINDEX (func_decl)),
20246 0));
20248 /* GNU extension: Record what type this method came from originally. */
20249 if (debug_info_level > DINFO_LEVEL_TERSE
20250 && DECL_CONTEXT (func_decl))
20251 add_AT_die_ref (die, DW_AT_containing_type,
20252 lookup_type_die (DECL_CONTEXT (func_decl)));
20256 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20257 given decl. This used to be a vendor extension until after DWARF 4
20258 standardized it. */
20260 static void
20261 add_linkage_attr (dw_die_ref die, tree decl)
20263 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20265 /* Mimic what assemble_name_raw does with a leading '*'. */
20266 if (name[0] == '*')
20267 name = &name[1];
20269 if (dwarf_version >= 4)
20270 add_AT_string (die, DW_AT_linkage_name, name);
20271 else
20272 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20275 /* Add source coordinate attributes for the given decl. */
20277 static void
20278 add_src_coords_attributes (dw_die_ref die, tree decl)
20280 expanded_location s;
20282 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20283 return;
20284 s = expand_location (DECL_SOURCE_LOCATION (decl));
20285 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20286 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20287 if (debug_column_info && s.column)
20288 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20291 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20293 static void
20294 add_linkage_name_raw (dw_die_ref die, tree decl)
20296 /* Defer until we have an assembler name set. */
20297 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20299 limbo_die_node *asm_name;
20301 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20302 asm_name->die = die;
20303 asm_name->created_for = decl;
20304 asm_name->next = deferred_asm_name;
20305 deferred_asm_name = asm_name;
20307 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20308 add_linkage_attr (die, decl);
20311 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20313 static void
20314 add_linkage_name (dw_die_ref die, tree decl)
20316 if (debug_info_level > DINFO_LEVEL_NONE
20317 && VAR_OR_FUNCTION_DECL_P (decl)
20318 && TREE_PUBLIC (decl)
20319 && !(VAR_P (decl) && DECL_REGISTER (decl))
20320 && die->die_tag != DW_TAG_member)
20321 add_linkage_name_raw (die, decl);
20324 /* Add a DW_AT_name attribute and source coordinate attribute for the
20325 given decl, but only if it actually has a name. */
20327 static void
20328 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20329 bool no_linkage_name)
20331 tree decl_name;
20333 decl_name = DECL_NAME (decl);
20334 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20336 const char *name = dwarf2_name (decl, 0);
20337 if (name)
20338 add_name_attribute (die, name);
20339 if (! DECL_ARTIFICIAL (decl))
20340 add_src_coords_attributes (die, decl);
20342 if (!no_linkage_name)
20343 add_linkage_name (die, decl);
20346 #ifdef VMS_DEBUGGING_INFO
20347 /* Get the function's name, as described by its RTL. This may be different
20348 from the DECL_NAME name used in the source file. */
20349 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20351 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20352 XEXP (DECL_RTL (decl), 0), false);
20353 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20355 #endif /* VMS_DEBUGGING_INFO */
20358 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20360 static void
20361 add_discr_value (dw_die_ref die, dw_discr_value *value)
20363 dw_attr_node attr;
20365 attr.dw_attr = DW_AT_discr_value;
20366 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20367 attr.dw_attr_val.val_entry = NULL;
20368 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20369 if (value->pos)
20370 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20371 else
20372 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20373 add_dwarf_attr (die, &attr);
20376 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20378 static void
20379 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20381 dw_attr_node attr;
20383 attr.dw_attr = DW_AT_discr_list;
20384 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20385 attr.dw_attr_val.val_entry = NULL;
20386 attr.dw_attr_val.v.val_discr_list = discr_list;
20387 add_dwarf_attr (die, &attr);
20390 static inline dw_discr_list_ref
20391 AT_discr_list (dw_attr_node *attr)
20393 return attr->dw_attr_val.v.val_discr_list;
20396 #ifdef VMS_DEBUGGING_INFO
20397 /* Output the debug main pointer die for VMS */
20399 void
20400 dwarf2out_vms_debug_main_pointer (void)
20402 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20403 dw_die_ref die;
20405 /* Allocate the VMS debug main subprogram die. */
20406 die = ggc_cleared_alloc<die_node> ();
20407 die->die_tag = DW_TAG_subprogram;
20408 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20409 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20410 current_function_funcdef_no);
20411 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20413 /* Make it the first child of comp_unit_die (). */
20414 die->die_parent = comp_unit_die ();
20415 if (comp_unit_die ()->die_child)
20417 die->die_sib = comp_unit_die ()->die_child->die_sib;
20418 comp_unit_die ()->die_child->die_sib = die;
20420 else
20422 die->die_sib = die;
20423 comp_unit_die ()->die_child = die;
20426 #endif /* VMS_DEBUGGING_INFO */
20428 /* Push a new declaration scope. */
20430 static void
20431 push_decl_scope (tree scope)
20433 vec_safe_push (decl_scope_table, scope);
20436 /* Pop a declaration scope. */
20438 static inline void
20439 pop_decl_scope (void)
20441 decl_scope_table->pop ();
20444 /* walk_tree helper function for uses_local_type, below. */
20446 static tree
20447 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20449 if (!TYPE_P (*tp))
20450 *walk_subtrees = 0;
20451 else
20453 tree name = TYPE_NAME (*tp);
20454 if (name && DECL_P (name) && decl_function_context (name))
20455 return *tp;
20457 return NULL_TREE;
20460 /* If TYPE involves a function-local type (including a local typedef to a
20461 non-local type), returns that type; otherwise returns NULL_TREE. */
20463 static tree
20464 uses_local_type (tree type)
20466 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20467 return used;
20470 /* Return the DIE for the scope that immediately contains this type.
20471 Non-named types that do not involve a function-local type get global
20472 scope. Named types nested in namespaces or other types get their
20473 containing scope. All other types (i.e. function-local named types) get
20474 the current active scope. */
20476 static dw_die_ref
20477 scope_die_for (tree t, dw_die_ref context_die)
20479 dw_die_ref scope_die = NULL;
20480 tree containing_scope;
20482 /* Non-types always go in the current scope. */
20483 gcc_assert (TYPE_P (t));
20485 /* Use the scope of the typedef, rather than the scope of the type
20486 it refers to. */
20487 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20488 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20489 else
20490 containing_scope = TYPE_CONTEXT (t);
20492 /* Use the containing namespace if there is one. */
20493 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20495 if (context_die == lookup_decl_die (containing_scope))
20496 /* OK */;
20497 else if (debug_info_level > DINFO_LEVEL_TERSE)
20498 context_die = get_context_die (containing_scope);
20499 else
20500 containing_scope = NULL_TREE;
20503 /* Ignore function type "scopes" from the C frontend. They mean that
20504 a tagged type is local to a parmlist of a function declarator, but
20505 that isn't useful to DWARF. */
20506 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20507 containing_scope = NULL_TREE;
20509 if (SCOPE_FILE_SCOPE_P (containing_scope))
20511 /* If T uses a local type keep it local as well, to avoid references
20512 to function-local DIEs from outside the function. */
20513 if (current_function_decl && uses_local_type (t))
20514 scope_die = context_die;
20515 else
20516 scope_die = comp_unit_die ();
20518 else if (TYPE_P (containing_scope))
20520 /* For types, we can just look up the appropriate DIE. */
20521 if (debug_info_level > DINFO_LEVEL_TERSE)
20522 scope_die = get_context_die (containing_scope);
20523 else
20525 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20526 if (scope_die == NULL)
20527 scope_die = comp_unit_die ();
20530 else
20531 scope_die = context_die;
20533 return scope_die;
20536 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20538 static inline int
20539 local_scope_p (dw_die_ref context_die)
20541 for (; context_die; context_die = context_die->die_parent)
20542 if (context_die->die_tag == DW_TAG_inlined_subroutine
20543 || context_die->die_tag == DW_TAG_subprogram)
20544 return 1;
20546 return 0;
20549 /* Returns nonzero if CONTEXT_DIE is a class. */
20551 static inline int
20552 class_scope_p (dw_die_ref context_die)
20554 return (context_die
20555 && (context_die->die_tag == DW_TAG_structure_type
20556 || context_die->die_tag == DW_TAG_class_type
20557 || context_die->die_tag == DW_TAG_interface_type
20558 || context_die->die_tag == DW_TAG_union_type));
20561 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20562 whether or not to treat a DIE in this context as a declaration. */
20564 static inline int
20565 class_or_namespace_scope_p (dw_die_ref context_die)
20567 return (class_scope_p (context_die)
20568 || (context_die && context_die->die_tag == DW_TAG_namespace));
20571 /* Many forms of DIEs require a "type description" attribute. This
20572 routine locates the proper "type descriptor" die for the type given
20573 by 'type' plus any additional qualifiers given by 'cv_quals', and
20574 adds a DW_AT_type attribute below the given die. */
20576 static void
20577 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20578 bool reverse, dw_die_ref context_die)
20580 enum tree_code code = TREE_CODE (type);
20581 dw_die_ref type_die = NULL;
20583 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20584 or fixed-point type, use the inner type. This is because we have no
20585 support for unnamed types in base_type_die. This can happen if this is
20586 an Ada subrange type. Correct solution is emit a subrange type die. */
20587 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20588 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20589 type = TREE_TYPE (type), code = TREE_CODE (type);
20591 if (code == ERROR_MARK
20592 /* Handle a special case. For functions whose return type is void, we
20593 generate *no* type attribute. (Note that no object may have type
20594 `void', so this only applies to function return types). */
20595 || code == VOID_TYPE)
20596 return;
20598 type_die = modified_type_die (type,
20599 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20600 reverse,
20601 context_die);
20603 if (type_die != NULL)
20604 add_AT_die_ref (object_die, DW_AT_type, type_die);
20607 /* Given an object die, add the calling convention attribute for the
20608 function call type. */
20609 static void
20610 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20612 enum dwarf_calling_convention value = DW_CC_normal;
20614 value = ((enum dwarf_calling_convention)
20615 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20617 if (is_fortran ()
20618 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20620 /* DWARF 2 doesn't provide a way to identify a program's source-level
20621 entry point. DW_AT_calling_convention attributes are only meant
20622 to describe functions' calling conventions. However, lacking a
20623 better way to signal the Fortran main program, we used this for
20624 a long time, following existing custom. Now, DWARF 4 has
20625 DW_AT_main_subprogram, which we add below, but some tools still
20626 rely on the old way, which we thus keep. */
20627 value = DW_CC_program;
20629 if (dwarf_version >= 4 || !dwarf_strict)
20630 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20633 /* Only add the attribute if the backend requests it, and
20634 is not DW_CC_normal. */
20635 if (value && (value != DW_CC_normal))
20636 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20639 /* Given a tree pointer to a struct, class, union, or enum type node, return
20640 a pointer to the (string) tag name for the given type, or zero if the type
20641 was declared without a tag. */
20643 static const char *
20644 type_tag (const_tree type)
20646 const char *name = 0;
20648 if (TYPE_NAME (type) != 0)
20650 tree t = 0;
20652 /* Find the IDENTIFIER_NODE for the type name. */
20653 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20654 && !TYPE_NAMELESS (type))
20655 t = TYPE_NAME (type);
20657 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20658 a TYPE_DECL node, regardless of whether or not a `typedef' was
20659 involved. */
20660 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20661 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20663 /* We want to be extra verbose. Don't call dwarf_name if
20664 DECL_NAME isn't set. The default hook for decl_printable_name
20665 doesn't like that, and in this context it's correct to return
20666 0, instead of "<anonymous>" or the like. */
20667 if (DECL_NAME (TYPE_NAME (type))
20668 && !DECL_NAMELESS (TYPE_NAME (type)))
20669 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20672 /* Now get the name as a string, or invent one. */
20673 if (!name && t != 0)
20674 name = IDENTIFIER_POINTER (t);
20677 return (name == 0 || *name == '\0') ? 0 : name;
20680 /* Return the type associated with a data member, make a special check
20681 for bit field types. */
20683 static inline tree
20684 member_declared_type (const_tree member)
20686 return (DECL_BIT_FIELD_TYPE (member)
20687 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20690 /* Get the decl's label, as described by its RTL. This may be different
20691 from the DECL_NAME name used in the source file. */
20693 #if 0
20694 static const char *
20695 decl_start_label (tree decl)
20697 rtx x;
20698 const char *fnname;
20700 x = DECL_RTL (decl);
20701 gcc_assert (MEM_P (x));
20703 x = XEXP (x, 0);
20704 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20706 fnname = XSTR (x, 0);
20707 return fnname;
20709 #endif
20711 /* For variable-length arrays that have been previously generated, but
20712 may be incomplete due to missing subscript info, fill the subscript
20713 info. Return TRUE if this is one of those cases. */
20714 static bool
20715 fill_variable_array_bounds (tree type)
20717 if (TREE_ASM_WRITTEN (type)
20718 && TREE_CODE (type) == ARRAY_TYPE
20719 && variably_modified_type_p (type, NULL))
20721 dw_die_ref array_die = lookup_type_die (type);
20722 if (!array_die)
20723 return false;
20724 add_subscript_info (array_die, type, !is_ada ());
20725 return true;
20727 return false;
20730 /* These routines generate the internal representation of the DIE's for
20731 the compilation unit. Debugging information is collected by walking
20732 the declaration trees passed in from dwarf2out_decl(). */
20734 static void
20735 gen_array_type_die (tree type, dw_die_ref context_die)
20737 dw_die_ref array_die;
20739 /* GNU compilers represent multidimensional array types as sequences of one
20740 dimensional array types whose element types are themselves array types.
20741 We sometimes squish that down to a single array_type DIE with multiple
20742 subscripts in the Dwarf debugging info. The draft Dwarf specification
20743 say that we are allowed to do this kind of compression in C, because
20744 there is no difference between an array of arrays and a multidimensional
20745 array. We don't do this for Ada to remain as close as possible to the
20746 actual representation, which is especially important against the language
20747 flexibilty wrt arrays of variable size. */
20749 bool collapse_nested_arrays = !is_ada ();
20751 if (fill_variable_array_bounds (type))
20752 return;
20754 dw_die_ref scope_die = scope_die_for (type, context_die);
20755 tree element_type;
20757 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20758 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20759 if (TYPE_STRING_FLAG (type)
20760 && TREE_CODE (type) == ARRAY_TYPE
20761 && is_fortran ()
20762 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20764 HOST_WIDE_INT size;
20766 array_die = new_die (DW_TAG_string_type, scope_die, type);
20767 add_name_attribute (array_die, type_tag (type));
20768 equate_type_number_to_die (type, array_die);
20769 size = int_size_in_bytes (type);
20770 if (size >= 0)
20771 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20772 else if (TYPE_DOMAIN (type) != NULL_TREE
20773 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20775 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20776 tree rszdecl = szdecl;
20778 size = int_size_in_bytes (TREE_TYPE (szdecl));
20779 if (!DECL_P (szdecl))
20781 if (TREE_CODE (szdecl) == INDIRECT_REF
20782 && DECL_P (TREE_OPERAND (szdecl, 0)))
20784 rszdecl = TREE_OPERAND (szdecl, 0);
20785 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20786 != DWARF2_ADDR_SIZE)
20787 size = 0;
20789 else
20790 size = 0;
20792 if (size > 0)
20794 dw_loc_list_ref loc
20795 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20796 NULL);
20797 if (loc)
20799 add_AT_location_description (array_die, DW_AT_string_length,
20800 loc);
20801 if (size != DWARF2_ADDR_SIZE)
20802 add_AT_unsigned (array_die, dwarf_version >= 5
20803 ? DW_AT_string_length_byte_size
20804 : DW_AT_byte_size, size);
20808 return;
20811 array_die = new_die (DW_TAG_array_type, scope_die, type);
20812 add_name_attribute (array_die, type_tag (type));
20813 equate_type_number_to_die (type, array_die);
20815 if (TREE_CODE (type) == VECTOR_TYPE)
20816 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20818 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20819 if (is_fortran ()
20820 && TREE_CODE (type) == ARRAY_TYPE
20821 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20822 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20823 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20825 #if 0
20826 /* We default the array ordering. SDB will probably do
20827 the right things even if DW_AT_ordering is not present. It's not even
20828 an issue until we start to get into multidimensional arrays anyway. If
20829 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20830 then we'll have to put the DW_AT_ordering attribute back in. (But if
20831 and when we find out that we need to put these in, we will only do so
20832 for multidimensional arrays. */
20833 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20834 #endif
20836 if (TREE_CODE (type) == VECTOR_TYPE)
20838 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20839 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20840 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20841 add_bound_info (subrange_die, DW_AT_upper_bound,
20842 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20844 else
20845 add_subscript_info (array_die, type, collapse_nested_arrays);
20847 /* Add representation of the type of the elements of this array type and
20848 emit the corresponding DIE if we haven't done it already. */
20849 element_type = TREE_TYPE (type);
20850 if (collapse_nested_arrays)
20851 while (TREE_CODE (element_type) == ARRAY_TYPE)
20853 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20854 break;
20855 element_type = TREE_TYPE (element_type);
20858 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20859 TREE_CODE (type) == ARRAY_TYPE
20860 && TYPE_REVERSE_STORAGE_ORDER (type),
20861 context_die);
20863 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20864 if (TYPE_ARTIFICIAL (type))
20865 add_AT_flag (array_die, DW_AT_artificial, 1);
20867 if (get_AT (array_die, DW_AT_name))
20868 add_pubtype (type, array_die);
20870 add_alignment_attribute (array_die, type);
20873 /* This routine generates DIE for array with hidden descriptor, details
20874 are filled into *info by a langhook. */
20876 static void
20877 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20878 dw_die_ref context_die)
20880 const dw_die_ref scope_die = scope_die_for (type, context_die);
20881 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20882 struct loc_descr_context context = { type, info->base_decl, NULL,
20883 false, false };
20884 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20885 int dim;
20887 add_name_attribute (array_die, type_tag (type));
20888 equate_type_number_to_die (type, array_die);
20890 if (info->ndimensions > 1)
20891 switch (info->ordering)
20893 case array_descr_ordering_row_major:
20894 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20895 break;
20896 case array_descr_ordering_column_major:
20897 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20898 break;
20899 default:
20900 break;
20903 if (dwarf_version >= 3 || !dwarf_strict)
20905 if (info->data_location)
20906 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20907 dw_scalar_form_exprloc, &context);
20908 if (info->associated)
20909 add_scalar_info (array_die, DW_AT_associated, info->associated,
20910 dw_scalar_form_constant
20911 | dw_scalar_form_exprloc
20912 | dw_scalar_form_reference, &context);
20913 if (info->allocated)
20914 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20915 dw_scalar_form_constant
20916 | dw_scalar_form_exprloc
20917 | dw_scalar_form_reference, &context);
20918 if (info->stride)
20920 const enum dwarf_attribute attr
20921 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20922 const int forms
20923 = (info->stride_in_bits)
20924 ? dw_scalar_form_constant
20925 : (dw_scalar_form_constant
20926 | dw_scalar_form_exprloc
20927 | dw_scalar_form_reference);
20929 add_scalar_info (array_die, attr, info->stride, forms, &context);
20932 if (dwarf_version >= 5)
20934 if (info->rank)
20936 add_scalar_info (array_die, DW_AT_rank, info->rank,
20937 dw_scalar_form_constant
20938 | dw_scalar_form_exprloc, &context);
20939 subrange_tag = DW_TAG_generic_subrange;
20940 context.placeholder_arg = true;
20944 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20946 for (dim = 0; dim < info->ndimensions; dim++)
20948 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20950 if (info->dimen[dim].bounds_type)
20951 add_type_attribute (subrange_die,
20952 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20953 false, context_die);
20954 if (info->dimen[dim].lower_bound)
20955 add_bound_info (subrange_die, DW_AT_lower_bound,
20956 info->dimen[dim].lower_bound, &context);
20957 if (info->dimen[dim].upper_bound)
20958 add_bound_info (subrange_die, DW_AT_upper_bound,
20959 info->dimen[dim].upper_bound, &context);
20960 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20961 add_scalar_info (subrange_die, DW_AT_byte_stride,
20962 info->dimen[dim].stride,
20963 dw_scalar_form_constant
20964 | dw_scalar_form_exprloc
20965 | dw_scalar_form_reference,
20966 &context);
20969 gen_type_die (info->element_type, context_die);
20970 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20971 TREE_CODE (type) == ARRAY_TYPE
20972 && TYPE_REVERSE_STORAGE_ORDER (type),
20973 context_die);
20975 if (get_AT (array_die, DW_AT_name))
20976 add_pubtype (type, array_die);
20978 add_alignment_attribute (array_die, type);
20981 #if 0
20982 static void
20983 gen_entry_point_die (tree decl, dw_die_ref context_die)
20985 tree origin = decl_ultimate_origin (decl);
20986 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20988 if (origin != NULL)
20989 add_abstract_origin_attribute (decl_die, origin);
20990 else
20992 add_name_and_src_coords_attributes (decl_die, decl);
20993 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20994 TYPE_UNQUALIFIED, false, context_die);
20997 if (DECL_ABSTRACT_P (decl))
20998 equate_decl_number_to_die (decl, decl_die);
20999 else
21000 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21002 #endif
21004 /* Walk through the list of incomplete types again, trying once more to
21005 emit full debugging info for them. */
21007 static void
21008 retry_incomplete_types (void)
21010 set_early_dwarf s;
21011 int i;
21013 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21014 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21015 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21016 vec_safe_truncate (incomplete_types, 0);
21019 /* Determine what tag to use for a record type. */
21021 static enum dwarf_tag
21022 record_type_tag (tree type)
21024 if (! lang_hooks.types.classify_record)
21025 return DW_TAG_structure_type;
21027 switch (lang_hooks.types.classify_record (type))
21029 case RECORD_IS_STRUCT:
21030 return DW_TAG_structure_type;
21032 case RECORD_IS_CLASS:
21033 return DW_TAG_class_type;
21035 case RECORD_IS_INTERFACE:
21036 if (dwarf_version >= 3 || !dwarf_strict)
21037 return DW_TAG_interface_type;
21038 return DW_TAG_structure_type;
21040 default:
21041 gcc_unreachable ();
21045 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21046 include all of the information about the enumeration values also. Each
21047 enumerated type name/value is listed as a child of the enumerated type
21048 DIE. */
21050 static dw_die_ref
21051 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21053 dw_die_ref type_die = lookup_type_die (type);
21055 if (type_die == NULL)
21057 type_die = new_die (DW_TAG_enumeration_type,
21058 scope_die_for (type, context_die), type);
21059 equate_type_number_to_die (type, type_die);
21060 add_name_attribute (type_die, type_tag (type));
21061 if (dwarf_version >= 4 || !dwarf_strict)
21063 if (ENUM_IS_SCOPED (type))
21064 add_AT_flag (type_die, DW_AT_enum_class, 1);
21065 if (ENUM_IS_OPAQUE (type))
21066 add_AT_flag (type_die, DW_AT_declaration, 1);
21068 if (!dwarf_strict)
21069 add_AT_unsigned (type_die, DW_AT_encoding,
21070 TYPE_UNSIGNED (type)
21071 ? DW_ATE_unsigned
21072 : DW_ATE_signed);
21074 else if (! TYPE_SIZE (type))
21075 return type_die;
21076 else
21077 remove_AT (type_die, DW_AT_declaration);
21079 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21080 given enum type is incomplete, do not generate the DW_AT_byte_size
21081 attribute or the DW_AT_element_list attribute. */
21082 if (TYPE_SIZE (type))
21084 tree link;
21086 TREE_ASM_WRITTEN (type) = 1;
21087 add_byte_size_attribute (type_die, type);
21088 add_alignment_attribute (type_die, type);
21089 if (dwarf_version >= 3 || !dwarf_strict)
21091 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21092 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21093 context_die);
21095 if (TYPE_STUB_DECL (type) != NULL_TREE)
21097 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21098 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21101 /* If the first reference to this type was as the return type of an
21102 inline function, then it may not have a parent. Fix this now. */
21103 if (type_die->die_parent == NULL)
21104 add_child_die (scope_die_for (type, context_die), type_die);
21106 for (link = TYPE_VALUES (type);
21107 link != NULL; link = TREE_CHAIN (link))
21109 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21110 tree value = TREE_VALUE (link);
21112 add_name_attribute (enum_die,
21113 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21115 if (TREE_CODE (value) == CONST_DECL)
21116 value = DECL_INITIAL (value);
21118 if (simple_type_size_in_bits (TREE_TYPE (value))
21119 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21121 /* For constant forms created by add_AT_unsigned DWARF
21122 consumers (GDB, elfutils, etc.) always zero extend
21123 the value. Only when the actual value is negative
21124 do we need to use add_AT_int to generate a constant
21125 form that can represent negative values. */
21126 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21127 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21128 add_AT_unsigned (enum_die, DW_AT_const_value,
21129 (unsigned HOST_WIDE_INT) val);
21130 else
21131 add_AT_int (enum_die, DW_AT_const_value, val);
21133 else
21134 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21135 that here. TODO: This should be re-worked to use correct
21136 signed/unsigned double tags for all cases. */
21137 add_AT_wide (enum_die, DW_AT_const_value, value);
21140 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21141 if (TYPE_ARTIFICIAL (type))
21142 add_AT_flag (type_die, DW_AT_artificial, 1);
21144 else
21145 add_AT_flag (type_die, DW_AT_declaration, 1);
21147 add_alignment_attribute (type_die, type);
21149 add_pubtype (type, type_die);
21151 return type_die;
21154 /* Generate a DIE to represent either a real live formal parameter decl or to
21155 represent just the type of some formal parameter position in some function
21156 type.
21158 Note that this routine is a bit unusual because its argument may be a
21159 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21160 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21161 node. If it's the former then this function is being called to output a
21162 DIE to represent a formal parameter object (or some inlining thereof). If
21163 it's the latter, then this function is only being called to output a
21164 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21165 argument type of some subprogram type.
21166 If EMIT_NAME_P is true, name and source coordinate attributes
21167 are emitted. */
21169 static dw_die_ref
21170 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21171 dw_die_ref context_die)
21173 tree node_or_origin = node ? node : origin;
21174 tree ultimate_origin;
21175 dw_die_ref parm_die = NULL;
21177 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21179 parm_die = lookup_decl_die (node);
21181 /* If the contexts differ, we may not be talking about the same
21182 thing. */
21183 if (parm_die && parm_die->die_parent != context_die)
21185 if (!DECL_ABSTRACT_P (node))
21187 /* This can happen when creating an inlined instance, in
21188 which case we need to create a new DIE that will get
21189 annotated with DW_AT_abstract_origin. */
21190 parm_die = NULL;
21192 else
21194 /* FIXME: Reuse DIE even with a differing context.
21196 This can happen when calling
21197 dwarf2out_abstract_function to build debug info for
21198 the abstract instance of a function for which we have
21199 already generated a DIE in
21200 dwarf2out_early_global_decl.
21202 Once we remove dwarf2out_abstract_function, we should
21203 have a call to gcc_unreachable here. */
21207 if (parm_die && parm_die->die_parent == NULL)
21209 /* Check that parm_die already has the right attributes that
21210 we would have added below. If any attributes are
21211 missing, fall through to add them. */
21212 if (! DECL_ABSTRACT_P (node_or_origin)
21213 && !get_AT (parm_die, DW_AT_location)
21214 && !get_AT (parm_die, DW_AT_const_value))
21215 /* We are missing location info, and are about to add it. */
21217 else
21219 add_child_die (context_die, parm_die);
21220 return parm_die;
21225 /* If we have a previously generated DIE, use it, unless this is an
21226 concrete instance (origin != NULL), in which case we need a new
21227 DIE with a corresponding DW_AT_abstract_origin. */
21228 bool reusing_die;
21229 if (parm_die && origin == NULL)
21230 reusing_die = true;
21231 else
21233 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21234 reusing_die = false;
21237 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21239 case tcc_declaration:
21240 ultimate_origin = decl_ultimate_origin (node_or_origin);
21241 if (node || ultimate_origin)
21242 origin = ultimate_origin;
21244 if (reusing_die)
21245 goto add_location;
21247 if (origin != NULL)
21248 add_abstract_origin_attribute (parm_die, origin);
21249 else if (emit_name_p)
21250 add_name_and_src_coords_attributes (parm_die, node);
21251 if (origin == NULL
21252 || (! DECL_ABSTRACT_P (node_or_origin)
21253 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21254 decl_function_context
21255 (node_or_origin))))
21257 tree type = TREE_TYPE (node_or_origin);
21258 if (decl_by_reference_p (node_or_origin))
21259 add_type_attribute (parm_die, TREE_TYPE (type),
21260 TYPE_UNQUALIFIED,
21261 false, context_die);
21262 else
21263 add_type_attribute (parm_die, type,
21264 decl_quals (node_or_origin),
21265 false, context_die);
21267 if (origin == NULL && DECL_ARTIFICIAL (node))
21268 add_AT_flag (parm_die, DW_AT_artificial, 1);
21269 add_location:
21270 if (node && node != origin)
21271 equate_decl_number_to_die (node, parm_die);
21272 if (! DECL_ABSTRACT_P (node_or_origin))
21273 add_location_or_const_value_attribute (parm_die, node_or_origin,
21274 node == NULL);
21276 break;
21278 case tcc_type:
21279 /* We were called with some kind of a ..._TYPE node. */
21280 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21281 context_die);
21282 break;
21284 default:
21285 gcc_unreachable ();
21288 return parm_die;
21291 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21292 children DW_TAG_formal_parameter DIEs representing the arguments of the
21293 parameter pack.
21295 PARM_PACK must be a function parameter pack.
21296 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21297 must point to the subsequent arguments of the function PACK_ARG belongs to.
21298 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21299 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21300 following the last one for which a DIE was generated. */
21302 static dw_die_ref
21303 gen_formal_parameter_pack_die (tree parm_pack,
21304 tree pack_arg,
21305 dw_die_ref subr_die,
21306 tree *next_arg)
21308 tree arg;
21309 dw_die_ref parm_pack_die;
21311 gcc_assert (parm_pack
21312 && lang_hooks.function_parameter_pack_p (parm_pack)
21313 && subr_die);
21315 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21316 add_src_coords_attributes (parm_pack_die, parm_pack);
21318 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21320 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21321 parm_pack))
21322 break;
21323 gen_formal_parameter_die (arg, NULL,
21324 false /* Don't emit name attribute. */,
21325 parm_pack_die);
21327 if (next_arg)
21328 *next_arg = arg;
21329 return parm_pack_die;
21332 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21333 at the end of an (ANSI prototyped) formal parameters list. */
21335 static void
21336 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21338 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21341 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21342 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21343 parameters as specified in some function type specification (except for
21344 those which appear as part of a function *definition*). */
21346 static void
21347 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21349 tree link;
21350 tree formal_type = NULL;
21351 tree first_parm_type;
21352 tree arg;
21354 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21356 arg = DECL_ARGUMENTS (function_or_method_type);
21357 function_or_method_type = TREE_TYPE (function_or_method_type);
21359 else
21360 arg = NULL_TREE;
21362 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21364 /* Make our first pass over the list of formal parameter types and output a
21365 DW_TAG_formal_parameter DIE for each one. */
21366 for (link = first_parm_type; link; )
21368 dw_die_ref parm_die;
21370 formal_type = TREE_VALUE (link);
21371 if (formal_type == void_type_node)
21372 break;
21374 /* Output a (nameless) DIE to represent the formal parameter itself. */
21375 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21377 parm_die = gen_formal_parameter_die (formal_type, NULL,
21378 true /* Emit name attribute. */,
21379 context_die);
21380 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21381 && link == first_parm_type)
21383 add_AT_flag (parm_die, DW_AT_artificial, 1);
21384 if (dwarf_version >= 3 || !dwarf_strict)
21385 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21387 else if (arg && DECL_ARTIFICIAL (arg))
21388 add_AT_flag (parm_die, DW_AT_artificial, 1);
21391 link = TREE_CHAIN (link);
21392 if (arg)
21393 arg = DECL_CHAIN (arg);
21396 /* If this function type has an ellipsis, add a
21397 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21398 if (formal_type != void_type_node)
21399 gen_unspecified_parameters_die (function_or_method_type, context_die);
21401 /* Make our second (and final) pass over the list of formal parameter types
21402 and output DIEs to represent those types (as necessary). */
21403 for (link = TYPE_ARG_TYPES (function_or_method_type);
21404 link && TREE_VALUE (link);
21405 link = TREE_CHAIN (link))
21406 gen_type_die (TREE_VALUE (link), context_die);
21409 /* We want to generate the DIE for TYPE so that we can generate the
21410 die for MEMBER, which has been defined; we will need to refer back
21411 to the member declaration nested within TYPE. If we're trying to
21412 generate minimal debug info for TYPE, processing TYPE won't do the
21413 trick; we need to attach the member declaration by hand. */
21415 static void
21416 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21418 gen_type_die (type, context_die);
21420 /* If we're trying to avoid duplicate debug info, we may not have
21421 emitted the member decl for this function. Emit it now. */
21422 if (TYPE_STUB_DECL (type)
21423 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21424 && ! lookup_decl_die (member))
21426 dw_die_ref type_die;
21427 gcc_assert (!decl_ultimate_origin (member));
21429 push_decl_scope (type);
21430 type_die = lookup_type_die_strip_naming_typedef (type);
21431 if (TREE_CODE (member) == FUNCTION_DECL)
21432 gen_subprogram_die (member, type_die);
21433 else if (TREE_CODE (member) == FIELD_DECL)
21435 /* Ignore the nameless fields that are used to skip bits but handle
21436 C++ anonymous unions and structs. */
21437 if (DECL_NAME (member) != NULL_TREE
21438 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21439 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21441 struct vlr_context vlr_ctx = {
21442 DECL_CONTEXT (member), /* struct_type */
21443 NULL_TREE /* variant_part_offset */
21445 gen_type_die (member_declared_type (member), type_die);
21446 gen_field_die (member, &vlr_ctx, type_die);
21449 else
21450 gen_variable_die (member, NULL_TREE, type_die);
21452 pop_decl_scope ();
21456 /* Forward declare these functions, because they are mutually recursive
21457 with their set_block_* pairing functions. */
21458 static void set_decl_origin_self (tree);
21459 static void set_decl_abstract_flags (tree, vec<tree> &);
21461 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21462 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21463 that it points to the node itself, thus indicating that the node is its
21464 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21465 the given node is NULL, recursively descend the decl/block tree which
21466 it is the root of, and for each other ..._DECL or BLOCK node contained
21467 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21468 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21469 values to point to themselves. */
21471 static void
21472 set_block_origin_self (tree stmt)
21474 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21476 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21479 tree local_decl;
21481 for (local_decl = BLOCK_VARS (stmt);
21482 local_decl != NULL_TREE;
21483 local_decl = DECL_CHAIN (local_decl))
21484 /* Do not recurse on nested functions since the inlining status
21485 of parent and child can be different as per the DWARF spec. */
21486 if (TREE_CODE (local_decl) != FUNCTION_DECL
21487 && !DECL_EXTERNAL (local_decl))
21488 set_decl_origin_self (local_decl);
21492 tree subblock;
21494 for (subblock = BLOCK_SUBBLOCKS (stmt);
21495 subblock != NULL_TREE;
21496 subblock = BLOCK_CHAIN (subblock))
21497 set_block_origin_self (subblock); /* Recurse. */
21502 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21503 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21504 node to so that it points to the node itself, thus indicating that the
21505 node represents its own (abstract) origin. Additionally, if the
21506 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21507 the decl/block tree of which the given node is the root of, and for
21508 each other ..._DECL or BLOCK node contained therein whose
21509 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21510 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21511 point to themselves. */
21513 static void
21514 set_decl_origin_self (tree decl)
21516 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21518 DECL_ABSTRACT_ORIGIN (decl) = decl;
21519 if (TREE_CODE (decl) == FUNCTION_DECL)
21521 tree arg;
21523 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21524 DECL_ABSTRACT_ORIGIN (arg) = arg;
21525 if (DECL_INITIAL (decl) != NULL_TREE
21526 && DECL_INITIAL (decl) != error_mark_node)
21527 set_block_origin_self (DECL_INITIAL (decl));
21532 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21533 and if it wasn't 1 before, push it to abstract_vec vector.
21534 For all local decls and all local sub-blocks (recursively) do it
21535 too. */
21537 static void
21538 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21540 tree local_decl;
21541 tree subblock;
21542 unsigned int i;
21544 if (!BLOCK_ABSTRACT (stmt))
21546 abstract_vec.safe_push (stmt);
21547 BLOCK_ABSTRACT (stmt) = 1;
21550 for (local_decl = BLOCK_VARS (stmt);
21551 local_decl != NULL_TREE;
21552 local_decl = DECL_CHAIN (local_decl))
21553 if (! DECL_EXTERNAL (local_decl))
21554 set_decl_abstract_flags (local_decl, abstract_vec);
21556 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21558 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21559 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21560 || TREE_CODE (local_decl) == PARM_DECL)
21561 set_decl_abstract_flags (local_decl, abstract_vec);
21564 for (subblock = BLOCK_SUBBLOCKS (stmt);
21565 subblock != NULL_TREE;
21566 subblock = BLOCK_CHAIN (subblock))
21567 set_block_abstract_flags (subblock, abstract_vec);
21570 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21571 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21572 In the case where the decl is a FUNCTION_DECL also set the abstract
21573 flags for all of the parameters, local vars, local
21574 blocks and sub-blocks (recursively). */
21576 static void
21577 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21579 if (!DECL_ABSTRACT_P (decl))
21581 abstract_vec.safe_push (decl);
21582 DECL_ABSTRACT_P (decl) = 1;
21585 if (TREE_CODE (decl) == FUNCTION_DECL)
21587 tree arg;
21589 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21590 if (!DECL_ABSTRACT_P (arg))
21592 abstract_vec.safe_push (arg);
21593 DECL_ABSTRACT_P (arg) = 1;
21595 if (DECL_INITIAL (decl) != NULL_TREE
21596 && DECL_INITIAL (decl) != error_mark_node)
21597 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21601 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21602 may later generate inlined and/or out-of-line instances of.
21604 FIXME: In the early-dwarf world, this function, and most of the
21605 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21606 the abstract instance. All we would need to do is annotate
21607 the early DIE with the appropriate DW_AT_inline in late
21608 dwarf (perhaps in gen_inlined_subroutine_die).
21610 However, we can't do this yet, because LTO streaming of DIEs
21611 has not been implemented yet. */
21613 static void
21614 dwarf2out_abstract_function (tree decl)
21616 dw_die_ref old_die;
21617 tree save_fn;
21618 tree context;
21619 hash_table<decl_loc_hasher> *old_decl_loc_table;
21620 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21621 int old_call_site_count, old_tail_call_site_count;
21622 struct call_arg_loc_node *old_call_arg_locations;
21624 /* Make sure we have the actual abstract inline, not a clone. */
21625 decl = DECL_ORIGIN (decl);
21627 old_die = lookup_decl_die (decl);
21628 if (old_die && get_AT (old_die, DW_AT_inline))
21629 /* We've already generated the abstract instance. */
21630 return;
21632 /* We can be called while recursively when seeing block defining inlined subroutine
21633 DIE. Be sure to not clobber the outer location table nor use it or we would
21634 get locations in abstract instantces. */
21635 old_decl_loc_table = decl_loc_table;
21636 decl_loc_table = NULL;
21637 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21638 cached_dw_loc_list_table = NULL;
21639 old_call_arg_locations = call_arg_locations;
21640 call_arg_locations = NULL;
21641 old_call_site_count = call_site_count;
21642 call_site_count = -1;
21643 old_tail_call_site_count = tail_call_site_count;
21644 tail_call_site_count = -1;
21646 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21647 we don't get confused by DECL_ABSTRACT_P. */
21648 if (debug_info_level > DINFO_LEVEL_TERSE)
21650 context = decl_class_context (decl);
21651 if (context)
21652 gen_type_die_for_member
21653 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21656 /* Pretend we've just finished compiling this function. */
21657 save_fn = current_function_decl;
21658 current_function_decl = decl;
21660 auto_vec<tree, 64> abstract_vec;
21661 set_decl_abstract_flags (decl, abstract_vec);
21662 dwarf2out_decl (decl);
21663 unsigned int i;
21664 tree t;
21665 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21666 if (TREE_CODE (t) == BLOCK)
21667 BLOCK_ABSTRACT (t) = 0;
21668 else
21669 DECL_ABSTRACT_P (t) = 0;
21671 current_function_decl = save_fn;
21672 decl_loc_table = old_decl_loc_table;
21673 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21674 call_arg_locations = old_call_arg_locations;
21675 call_site_count = old_call_site_count;
21676 tail_call_site_count = old_tail_call_site_count;
21679 /* Helper function of premark_used_types() which gets called through
21680 htab_traverse.
21682 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21683 marked as unused by prune_unused_types. */
21685 bool
21686 premark_used_types_helper (tree const &type, void *)
21688 dw_die_ref die;
21690 die = lookup_type_die (type);
21691 if (die != NULL)
21692 die->die_perennial_p = 1;
21693 return true;
21696 /* Helper function of premark_types_used_by_global_vars which gets called
21697 through htab_traverse.
21699 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21700 marked as unused by prune_unused_types. The DIE of the type is marked
21701 only if the global variable using the type will actually be emitted. */
21704 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21705 void *)
21707 struct types_used_by_vars_entry *entry;
21708 dw_die_ref die;
21710 entry = (struct types_used_by_vars_entry *) *slot;
21711 gcc_assert (entry->type != NULL
21712 && entry->var_decl != NULL);
21713 die = lookup_type_die (entry->type);
21714 if (die)
21716 /* Ask cgraph if the global variable really is to be emitted.
21717 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21718 varpool_node *node = varpool_node::get (entry->var_decl);
21719 if (node && node->definition)
21721 die->die_perennial_p = 1;
21722 /* Keep the parent DIEs as well. */
21723 while ((die = die->die_parent) && die->die_perennial_p == 0)
21724 die->die_perennial_p = 1;
21727 return 1;
21730 /* Mark all members of used_types_hash as perennial. */
21732 static void
21733 premark_used_types (struct function *fun)
21735 if (fun && fun->used_types_hash)
21736 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21739 /* Mark all members of types_used_by_vars_entry as perennial. */
21741 static void
21742 premark_types_used_by_global_vars (void)
21744 if (types_used_by_vars_hash)
21745 types_used_by_vars_hash
21746 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21749 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21750 for CA_LOC call arg loc node. */
21752 static dw_die_ref
21753 gen_call_site_die (tree decl, dw_die_ref subr_die,
21754 struct call_arg_loc_node *ca_loc)
21756 dw_die_ref stmt_die = NULL, die;
21757 tree block = ca_loc->block;
21759 while (block
21760 && block != DECL_INITIAL (decl)
21761 && TREE_CODE (block) == BLOCK)
21763 stmt_die = BLOCK_DIE (block);
21764 if (stmt_die)
21765 break;
21766 block = BLOCK_SUPERCONTEXT (block);
21768 if (stmt_die == NULL)
21769 stmt_die = subr_die;
21770 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21771 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21772 if (ca_loc->tail_call_p)
21773 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21774 if (ca_loc->symbol_ref)
21776 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21777 if (tdie)
21778 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21779 else
21780 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21781 false);
21783 return die;
21786 /* Generate a DIE to represent a declared function (either file-scope or
21787 block-local). */
21789 static void
21790 gen_subprogram_die (tree decl, dw_die_ref context_die)
21792 tree origin = decl_ultimate_origin (decl);
21793 dw_die_ref subr_die;
21794 dw_die_ref old_die = lookup_decl_die (decl);
21796 /* This function gets called multiple times for different stages of
21797 the debug process. For example, for func() in this code:
21799 namespace S
21801 void func() { ... }
21804 ...we get called 4 times. Twice in early debug and twice in
21805 late debug:
21807 Early debug
21808 -----------
21810 1. Once while generating func() within the namespace. This is
21811 the declaration. The declaration bit below is set, as the
21812 context is the namespace.
21814 A new DIE will be generated with DW_AT_declaration set.
21816 2. Once for func() itself. This is the specification. The
21817 declaration bit below is clear as the context is the CU.
21819 We will use the cached DIE from (1) to create a new DIE with
21820 DW_AT_specification pointing to the declaration in (1).
21822 Late debug via rest_of_handle_final()
21823 -------------------------------------
21825 3. Once generating func() within the namespace. This is also the
21826 declaration, as in (1), but this time we will early exit below
21827 as we have a cached DIE and a declaration needs no additional
21828 annotations (no locations), as the source declaration line
21829 info is enough.
21831 4. Once for func() itself. As in (2), this is the specification,
21832 but this time we will re-use the cached DIE, and just annotate
21833 it with the location information that should now be available.
21835 For something without namespaces, but with abstract instances, we
21836 are also called a multiple times:
21838 class Base
21840 public:
21841 Base (); // constructor declaration (1)
21844 Base::Base () { } // constructor specification (2)
21846 Early debug
21847 -----------
21849 1. Once for the Base() constructor by virtue of it being a
21850 member of the Base class. This is done via
21851 rest_of_type_compilation.
21853 This is a declaration, so a new DIE will be created with
21854 DW_AT_declaration.
21856 2. Once for the Base() constructor definition, but this time
21857 while generating the abstract instance of the base
21858 constructor (__base_ctor) which is being generated via early
21859 debug of reachable functions.
21861 Even though we have a cached version of the declaration (1),
21862 we will create a DW_AT_specification of the declaration DIE
21863 in (1).
21865 3. Once for the __base_ctor itself, but this time, we generate
21866 an DW_AT_abstract_origin version of the DW_AT_specification in
21867 (2).
21869 Late debug via rest_of_handle_final
21870 -----------------------------------
21872 4. One final time for the __base_ctor (which will have a cached
21873 DIE with DW_AT_abstract_origin created in (3). This time,
21874 we will just annotate the location information now
21875 available.
21877 int declaration = (current_function_decl != decl
21878 || class_or_namespace_scope_p (context_die));
21880 /* Now that the C++ front end lazily declares artificial member fns, we
21881 might need to retrofit the declaration into its class. */
21882 if (!declaration && !origin && !old_die
21883 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21884 && !class_or_namespace_scope_p (context_die)
21885 && debug_info_level > DINFO_LEVEL_TERSE)
21886 old_die = force_decl_die (decl);
21888 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21889 if (origin != NULL)
21891 gcc_assert (!declaration || local_scope_p (context_die));
21893 /* Fixup die_parent for the abstract instance of a nested
21894 inline function. */
21895 if (old_die && old_die->die_parent == NULL)
21896 add_child_die (context_die, old_die);
21898 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21900 /* If we have a DW_AT_abstract_origin we have a working
21901 cached version. */
21902 subr_die = old_die;
21904 else
21906 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21907 add_abstract_origin_attribute (subr_die, origin);
21908 /* This is where the actual code for a cloned function is.
21909 Let's emit linkage name attribute for it. This helps
21910 debuggers to e.g, set breakpoints into
21911 constructors/destructors when the user asks "break
21912 K::K". */
21913 add_linkage_name (subr_die, decl);
21916 /* A cached copy, possibly from early dwarf generation. Reuse as
21917 much as possible. */
21918 else if (old_die)
21920 /* A declaration that has been previously dumped needs no
21921 additional information. */
21922 if (declaration)
21923 return;
21925 if (!get_AT_flag (old_die, DW_AT_declaration)
21926 /* We can have a normal definition following an inline one in the
21927 case of redefinition of GNU C extern inlines.
21928 It seems reasonable to use AT_specification in this case. */
21929 && !get_AT (old_die, DW_AT_inline))
21931 /* Detect and ignore this case, where we are trying to output
21932 something we have already output. */
21933 if (get_AT (old_die, DW_AT_low_pc)
21934 || get_AT (old_die, DW_AT_ranges))
21935 return;
21937 /* If we have no location information, this must be a
21938 partially generated DIE from early dwarf generation.
21939 Fall through and generate it. */
21942 /* If the definition comes from the same place as the declaration,
21943 maybe use the old DIE. We always want the DIE for this function
21944 that has the *_pc attributes to be under comp_unit_die so the
21945 debugger can find it. We also need to do this for abstract
21946 instances of inlines, since the spec requires the out-of-line copy
21947 to have the same parent. For local class methods, this doesn't
21948 apply; we just use the old DIE. */
21949 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21950 struct dwarf_file_data * file_index = lookup_filename (s.file);
21951 if ((is_cu_die (old_die->die_parent)
21952 /* This condition fixes the inconsistency/ICE with the
21953 following Fortran test (or some derivative thereof) while
21954 building libgfortran:
21956 module some_m
21957 contains
21958 logical function funky (FLAG)
21959 funky = .true.
21960 end function
21961 end module
21963 || (old_die->die_parent
21964 && old_die->die_parent->die_tag == DW_TAG_module)
21965 || context_die == NULL)
21966 && (DECL_ARTIFICIAL (decl)
21967 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21968 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21969 == (unsigned) s.line)
21970 && (!debug_column_info
21971 || s.column == 0
21972 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21973 == (unsigned) s.column)))))
21975 subr_die = old_die;
21977 /* Clear out the declaration attribute, but leave the
21978 parameters so they can be augmented with location
21979 information later. Unless this was a declaration, in
21980 which case, wipe out the nameless parameters and recreate
21981 them further down. */
21982 if (remove_AT (subr_die, DW_AT_declaration))
21985 remove_AT (subr_die, DW_AT_object_pointer);
21986 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21989 /* Make a specification pointing to the previously built
21990 declaration. */
21991 else
21993 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21994 add_AT_specification (subr_die, old_die);
21995 add_pubname (decl, subr_die);
21996 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21997 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21998 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21999 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22000 if (debug_column_info
22001 && s.column
22002 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22003 != (unsigned) s.column))
22004 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22006 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22007 emit the real type on the definition die. */
22008 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22010 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22011 if (die == auto_die || die == decltype_auto_die)
22012 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22013 TYPE_UNQUALIFIED, false, context_die);
22016 /* When we process the method declaration, we haven't seen
22017 the out-of-class defaulted definition yet, so we have to
22018 recheck now. */
22019 if ((dwarf_version >= 5 || ! dwarf_strict)
22020 && !get_AT (subr_die, DW_AT_defaulted))
22022 int defaulted
22023 = lang_hooks.decls.decl_dwarf_attribute (decl,
22024 DW_AT_defaulted);
22025 if (defaulted != -1)
22027 /* Other values must have been handled before. */
22028 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22029 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22034 /* Create a fresh DIE for anything else. */
22035 else
22037 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22039 if (TREE_PUBLIC (decl))
22040 add_AT_flag (subr_die, DW_AT_external, 1);
22042 add_name_and_src_coords_attributes (subr_die, decl);
22043 add_pubname (decl, subr_die);
22044 if (debug_info_level > DINFO_LEVEL_TERSE)
22046 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22047 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22048 TYPE_UNQUALIFIED, false, context_die);
22051 add_pure_or_virtual_attribute (subr_die, decl);
22052 if (DECL_ARTIFICIAL (decl))
22053 add_AT_flag (subr_die, DW_AT_artificial, 1);
22055 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22056 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22058 add_alignment_attribute (subr_die, decl);
22060 add_accessibility_attribute (subr_die, decl);
22063 /* Unless we have an existing non-declaration DIE, equate the new
22064 DIE. */
22065 if (!old_die || is_declaration_die (old_die))
22066 equate_decl_number_to_die (decl, subr_die);
22068 if (declaration)
22070 if (!old_die || !get_AT (old_die, DW_AT_inline))
22072 add_AT_flag (subr_die, DW_AT_declaration, 1);
22074 /* If this is an explicit function declaration then generate
22075 a DW_AT_explicit attribute. */
22076 if ((dwarf_version >= 3 || !dwarf_strict)
22077 && lang_hooks.decls.decl_dwarf_attribute (decl,
22078 DW_AT_explicit) == 1)
22079 add_AT_flag (subr_die, DW_AT_explicit, 1);
22081 /* If this is a C++11 deleted special function member then generate
22082 a DW_AT_deleted attribute. */
22083 if ((dwarf_version >= 5 || !dwarf_strict)
22084 && lang_hooks.decls.decl_dwarf_attribute (decl,
22085 DW_AT_deleted) == 1)
22086 add_AT_flag (subr_die, DW_AT_deleted, 1);
22088 /* If this is a C++11 defaulted special function member then
22089 generate a DW_AT_defaulted attribute. */
22090 if (dwarf_version >= 5 || !dwarf_strict)
22092 int defaulted
22093 = lang_hooks.decls.decl_dwarf_attribute (decl,
22094 DW_AT_defaulted);
22095 if (defaulted != -1)
22096 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22099 /* If this is a C++11 non-static member function with & ref-qualifier
22100 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_reference) == 1)
22104 add_AT_flag (subr_die, DW_AT_reference, 1);
22106 /* If this is a C++11 non-static member function with &&
22107 ref-qualifier then generate a DW_AT_reference attribute. */
22108 if ((dwarf_version >= 5 || !dwarf_strict)
22109 && lang_hooks.decls.decl_dwarf_attribute (decl,
22110 DW_AT_rvalue_reference)
22111 == 1)
22112 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22115 /* Tag abstract instances with DW_AT_inline. */
22116 else if (DECL_ABSTRACT_P (decl))
22118 if (DECL_DECLARED_INLINE_P (decl))
22120 if (cgraph_function_possibly_inlined_p (decl))
22121 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22122 else
22123 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22125 else
22127 if (cgraph_function_possibly_inlined_p (decl))
22128 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22129 else
22130 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22133 if (DECL_DECLARED_INLINE_P (decl)
22134 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22135 add_AT_flag (subr_die, DW_AT_artificial, 1);
22137 /* For non DECL_EXTERNALs, if range information is available, fill
22138 the DIE with it. */
22139 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22141 HOST_WIDE_INT cfa_fb_offset;
22143 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22145 if (!crtl->has_bb_partition)
22147 dw_fde_ref fde = fun->fde;
22148 if (fde->dw_fde_begin)
22150 /* We have already generated the labels. */
22151 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22152 fde->dw_fde_end, false);
22154 else
22156 /* Create start/end labels and add the range. */
22157 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22158 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22159 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22160 current_function_funcdef_no);
22161 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22162 current_function_funcdef_no);
22163 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22164 false);
22167 #if VMS_DEBUGGING_INFO
22168 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22169 Section 2.3 Prologue and Epilogue Attributes:
22170 When a breakpoint is set on entry to a function, it is generally
22171 desirable for execution to be suspended, not on the very first
22172 instruction of the function, but rather at a point after the
22173 function's frame has been set up, after any language defined local
22174 declaration processing has been completed, and before execution of
22175 the first statement of the function begins. Debuggers generally
22176 cannot properly determine where this point is. Similarly for a
22177 breakpoint set on exit from a function. The prologue and epilogue
22178 attributes allow a compiler to communicate the location(s) to use. */
22181 if (fde->dw_fde_vms_end_prologue)
22182 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22183 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22185 if (fde->dw_fde_vms_begin_epilogue)
22186 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22187 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22189 #endif
22192 else
22194 /* Generate pubnames entries for the split function code ranges. */
22195 dw_fde_ref fde = fun->fde;
22197 if (fde->dw_fde_second_begin)
22199 if (dwarf_version >= 3 || !dwarf_strict)
22201 /* We should use ranges for non-contiguous code section
22202 addresses. Use the actual code range for the initial
22203 section, since the HOT/COLD labels might precede an
22204 alignment offset. */
22205 bool range_list_added = false;
22206 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22207 fde->dw_fde_end, &range_list_added,
22208 false);
22209 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22210 fde->dw_fde_second_end,
22211 &range_list_added, false);
22212 if (range_list_added)
22213 add_ranges (NULL);
22215 else
22217 /* There is no real support in DW2 for this .. so we make
22218 a work-around. First, emit the pub name for the segment
22219 containing the function label. Then make and emit a
22220 simplified subprogram DIE for the second segment with the
22221 name pre-fixed by __hot/cold_sect_of_. We use the same
22222 linkage name for the second die so that gdb will find both
22223 sections when given "b foo". */
22224 const char *name = NULL;
22225 tree decl_name = DECL_NAME (decl);
22226 dw_die_ref seg_die;
22228 /* Do the 'primary' section. */
22229 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22230 fde->dw_fde_end, false);
22232 /* Build a minimal DIE for the secondary section. */
22233 seg_die = new_die (DW_TAG_subprogram,
22234 subr_die->die_parent, decl);
22236 if (TREE_PUBLIC (decl))
22237 add_AT_flag (seg_die, DW_AT_external, 1);
22239 if (decl_name != NULL
22240 && IDENTIFIER_POINTER (decl_name) != NULL)
22242 name = dwarf2_name (decl, 1);
22243 if (! DECL_ARTIFICIAL (decl))
22244 add_src_coords_attributes (seg_die, decl);
22246 add_linkage_name (seg_die, decl);
22248 gcc_assert (name != NULL);
22249 add_pure_or_virtual_attribute (seg_die, decl);
22250 if (DECL_ARTIFICIAL (decl))
22251 add_AT_flag (seg_die, DW_AT_artificial, 1);
22253 name = concat ("__second_sect_of_", name, NULL);
22254 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22255 fde->dw_fde_second_end, false);
22256 add_name_attribute (seg_die, name);
22257 if (want_pubnames ())
22258 add_pubname_string (name, seg_die);
22261 else
22262 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22263 false);
22266 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22268 /* We define the "frame base" as the function's CFA. This is more
22269 convenient for several reasons: (1) It's stable across the prologue
22270 and epilogue, which makes it better than just a frame pointer,
22271 (2) With dwarf3, there exists a one-byte encoding that allows us
22272 to reference the .debug_frame data by proxy, but failing that,
22273 (3) We can at least reuse the code inspection and interpretation
22274 code that determines the CFA position at various points in the
22275 function. */
22276 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22278 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22279 add_AT_loc (subr_die, DW_AT_frame_base, op);
22281 else
22283 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22284 if (list->dw_loc_next)
22285 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22286 else
22287 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22290 /* Compute a displacement from the "steady-state frame pointer" to
22291 the CFA. The former is what all stack slots and argument slots
22292 will reference in the rtl; the latter is what we've told the
22293 debugger about. We'll need to adjust all frame_base references
22294 by this displacement. */
22295 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22297 if (fun->static_chain_decl)
22299 /* DWARF requires here a location expression that computes the
22300 address of the enclosing subprogram's frame base. The machinery
22301 in tree-nested.c is supposed to store this specific address in the
22302 last field of the FRAME record. */
22303 const tree frame_type
22304 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22305 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22307 tree fb_expr
22308 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22309 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22310 fb_expr, fb_decl, NULL_TREE);
22312 add_AT_location_description (subr_die, DW_AT_static_link,
22313 loc_list_from_tree (fb_expr, 0, NULL));
22316 resolve_variable_values ();
22319 /* Generate child dies for template paramaters. */
22320 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22321 gen_generic_params_dies (decl);
22323 /* Now output descriptions of the arguments for this function. This gets
22324 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22325 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22326 `...' at the end of the formal parameter list. In order to find out if
22327 there was a trailing ellipsis or not, we must instead look at the type
22328 associated with the FUNCTION_DECL. This will be a node of type
22329 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22330 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22331 an ellipsis at the end. */
22333 /* In the case where we are describing a mere function declaration, all we
22334 need to do here (and all we *can* do here) is to describe the *types* of
22335 its formal parameters. */
22336 if (debug_info_level <= DINFO_LEVEL_TERSE)
22338 else if (declaration)
22339 gen_formal_types_die (decl, subr_die);
22340 else
22342 /* Generate DIEs to represent all known formal parameters. */
22343 tree parm = DECL_ARGUMENTS (decl);
22344 tree generic_decl = early_dwarf
22345 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22346 tree generic_decl_parm = generic_decl
22347 ? DECL_ARGUMENTS (generic_decl)
22348 : NULL;
22350 /* Now we want to walk the list of parameters of the function and
22351 emit their relevant DIEs.
22353 We consider the case of DECL being an instance of a generic function
22354 as well as it being a normal function.
22356 If DECL is an instance of a generic function we walk the
22357 parameters of the generic function declaration _and_ the parameters of
22358 DECL itself. This is useful because we want to emit specific DIEs for
22359 function parameter packs and those are declared as part of the
22360 generic function declaration. In that particular case,
22361 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22362 That DIE has children DIEs representing the set of arguments
22363 of the pack. Note that the set of pack arguments can be empty.
22364 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22365 children DIE.
22367 Otherwise, we just consider the parameters of DECL. */
22368 while (generic_decl_parm || parm)
22370 if (generic_decl_parm
22371 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22372 gen_formal_parameter_pack_die (generic_decl_parm,
22373 parm, subr_die,
22374 &parm);
22375 else if (parm && !POINTER_BOUNDS_P (parm))
22377 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22379 if (parm == DECL_ARGUMENTS (decl)
22380 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22381 && parm_die
22382 && (dwarf_version >= 3 || !dwarf_strict))
22383 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22385 parm = DECL_CHAIN (parm);
22387 else if (parm)
22388 parm = DECL_CHAIN (parm);
22390 if (generic_decl_parm)
22391 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22394 /* Decide whether we need an unspecified_parameters DIE at the end.
22395 There are 2 more cases to do this for: 1) the ansi ... declaration -
22396 this is detectable when the end of the arg list is not a
22397 void_type_node 2) an unprototyped function declaration (not a
22398 definition). This just means that we have no info about the
22399 parameters at all. */
22400 if (early_dwarf)
22402 if (prototype_p (TREE_TYPE (decl)))
22404 /* This is the prototyped case, check for.... */
22405 if (stdarg_p (TREE_TYPE (decl)))
22406 gen_unspecified_parameters_die (decl, subr_die);
22408 else if (DECL_INITIAL (decl) == NULL_TREE)
22409 gen_unspecified_parameters_die (decl, subr_die);
22413 if (subr_die != old_die)
22414 /* Add the calling convention attribute if requested. */
22415 add_calling_convention_attribute (subr_die, decl);
22417 /* Output Dwarf info for all of the stuff within the body of the function
22418 (if it has one - it may be just a declaration).
22420 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22421 a function. This BLOCK actually represents the outermost binding contour
22422 for the function, i.e. the contour in which the function's formal
22423 parameters and labels get declared. Curiously, it appears that the front
22424 end doesn't actually put the PARM_DECL nodes for the current function onto
22425 the BLOCK_VARS list for this outer scope, but are strung off of the
22426 DECL_ARGUMENTS list for the function instead.
22428 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22429 the LABEL_DECL nodes for the function however, and we output DWARF info
22430 for those in decls_for_scope. Just within the `outer_scope' there will be
22431 a BLOCK node representing the function's outermost pair of curly braces,
22432 and any blocks used for the base and member initializers of a C++
22433 constructor function. */
22434 tree outer_scope = DECL_INITIAL (decl);
22435 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22437 int call_site_note_count = 0;
22438 int tail_call_site_note_count = 0;
22440 /* Emit a DW_TAG_variable DIE for a named return value. */
22441 if (DECL_NAME (DECL_RESULT (decl)))
22442 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22444 /* The first time through decls_for_scope we will generate the
22445 DIEs for the locals. The second time, we fill in the
22446 location info. */
22447 decls_for_scope (outer_scope, subr_die);
22449 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22451 struct call_arg_loc_node *ca_loc;
22452 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22454 dw_die_ref die = NULL;
22455 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22456 rtx arg, next_arg;
22458 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22459 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22460 : NULL_RTX);
22461 arg; arg = next_arg)
22463 dw_loc_descr_ref reg, val;
22464 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22465 dw_die_ref cdie, tdie = NULL;
22467 next_arg = XEXP (arg, 1);
22468 if (REG_P (XEXP (XEXP (arg, 0), 0))
22469 && next_arg
22470 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22471 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22472 && REGNO (XEXP (XEXP (arg, 0), 0))
22473 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22474 next_arg = XEXP (next_arg, 1);
22475 if (mode == VOIDmode)
22477 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22478 if (mode == VOIDmode)
22479 mode = GET_MODE (XEXP (arg, 0));
22481 if (mode == VOIDmode || mode == BLKmode)
22482 continue;
22483 /* Get dynamic information about call target only if we
22484 have no static information: we cannot generate both
22485 DW_AT_call_origin and DW_AT_call_target
22486 attributes. */
22487 if (ca_loc->symbol_ref == NULL_RTX)
22489 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22491 tloc = XEXP (XEXP (arg, 0), 1);
22492 continue;
22494 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22495 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22497 tlocc = XEXP (XEXP (arg, 0), 1);
22498 continue;
22501 reg = NULL;
22502 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22503 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22504 VAR_INIT_STATUS_INITIALIZED);
22505 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22507 rtx mem = XEXP (XEXP (arg, 0), 0);
22508 reg = mem_loc_descriptor (XEXP (mem, 0),
22509 get_address_mode (mem),
22510 GET_MODE (mem),
22511 VAR_INIT_STATUS_INITIALIZED);
22513 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22514 == DEBUG_PARAMETER_REF)
22516 tree tdecl
22517 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22518 tdie = lookup_decl_die (tdecl);
22519 if (tdie == NULL)
22520 continue;
22522 else
22523 continue;
22524 if (reg == NULL
22525 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22526 != DEBUG_PARAMETER_REF)
22527 continue;
22528 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22529 VOIDmode,
22530 VAR_INIT_STATUS_INITIALIZED);
22531 if (val == NULL)
22532 continue;
22533 if (die == NULL)
22534 die = gen_call_site_die (decl, subr_die, ca_loc);
22535 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22536 NULL_TREE);
22537 if (reg != NULL)
22538 add_AT_loc (cdie, DW_AT_location, reg);
22539 else if (tdie != NULL)
22540 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22541 tdie);
22542 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22543 if (next_arg != XEXP (arg, 1))
22545 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22546 if (mode == VOIDmode)
22547 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22548 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22549 0), 1),
22550 mode, VOIDmode,
22551 VAR_INIT_STATUS_INITIALIZED);
22552 if (val != NULL)
22553 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22554 val);
22557 if (die == NULL
22558 && (ca_loc->symbol_ref || tloc))
22559 die = gen_call_site_die (decl, subr_die, ca_loc);
22560 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22562 dw_loc_descr_ref tval = NULL;
22564 if (tloc != NULL_RTX)
22565 tval = mem_loc_descriptor (tloc,
22566 GET_MODE (tloc) == VOIDmode
22567 ? Pmode : GET_MODE (tloc),
22568 VOIDmode,
22569 VAR_INIT_STATUS_INITIALIZED);
22570 if (tval)
22571 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22572 else if (tlocc != NULL_RTX)
22574 tval = mem_loc_descriptor (tlocc,
22575 GET_MODE (tlocc) == VOIDmode
22576 ? Pmode : GET_MODE (tlocc),
22577 VOIDmode,
22578 VAR_INIT_STATUS_INITIALIZED);
22579 if (tval)
22580 add_AT_loc (die,
22581 dwarf_AT (DW_AT_call_target_clobbered),
22582 tval);
22585 if (die != NULL)
22587 call_site_note_count++;
22588 if (ca_loc->tail_call_p)
22589 tail_call_site_note_count++;
22593 call_arg_locations = NULL;
22594 call_arg_loc_last = NULL;
22595 if (tail_call_site_count >= 0
22596 && tail_call_site_count == tail_call_site_note_count
22597 && (!dwarf_strict || dwarf_version >= 5))
22599 if (call_site_count >= 0
22600 && call_site_count == call_site_note_count)
22601 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22602 else
22603 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22605 call_site_count = -1;
22606 tail_call_site_count = -1;
22609 /* Mark used types after we have created DIEs for the functions scopes. */
22610 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22613 /* Returns a hash value for X (which really is a die_struct). */
22615 hashval_t
22616 block_die_hasher::hash (die_struct *d)
22618 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22621 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22622 as decl_id and die_parent of die_struct Y. */
22624 bool
22625 block_die_hasher::equal (die_struct *x, die_struct *y)
22627 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22630 /* Return TRUE if DECL, which may have been previously generated as
22631 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22632 true if decl (or its origin) is either an extern declaration or a
22633 class/namespace scoped declaration.
22635 The declare_in_namespace support causes us to get two DIEs for one
22636 variable, both of which are declarations. We want to avoid
22637 considering one to be a specification, so we must test for
22638 DECLARATION and DW_AT_declaration. */
22639 static inline bool
22640 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22642 return (old_die && TREE_STATIC (decl) && !declaration
22643 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22646 /* Return true if DECL is a local static. */
22648 static inline bool
22649 local_function_static (tree decl)
22651 gcc_assert (VAR_P (decl));
22652 return TREE_STATIC (decl)
22653 && DECL_CONTEXT (decl)
22654 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22657 /* Generate a DIE to represent a declared data object.
22658 Either DECL or ORIGIN must be non-null. */
22660 static void
22661 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22663 HOST_WIDE_INT off = 0;
22664 tree com_decl;
22665 tree decl_or_origin = decl ? decl : origin;
22666 tree ultimate_origin;
22667 dw_die_ref var_die;
22668 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22669 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22670 || class_or_namespace_scope_p (context_die));
22671 bool specialization_p = false;
22672 bool no_linkage_name = false;
22674 /* While C++ inline static data members have definitions inside of the
22675 class, force the first DIE to be a declaration, then let gen_member_die
22676 reparent it to the class context and call gen_variable_die again
22677 to create the outside of the class DIE for the definition. */
22678 if (!declaration
22679 && old_die == NULL
22680 && decl
22681 && DECL_CONTEXT (decl)
22682 && TYPE_P (DECL_CONTEXT (decl))
22683 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22685 declaration = true;
22686 if (dwarf_version < 5)
22687 no_linkage_name = true;
22690 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22691 if (decl || ultimate_origin)
22692 origin = ultimate_origin;
22693 com_decl = fortran_common (decl_or_origin, &off);
22695 /* Symbol in common gets emitted as a child of the common block, in the form
22696 of a data member. */
22697 if (com_decl)
22699 dw_die_ref com_die;
22700 dw_loc_list_ref loc = NULL;
22701 die_node com_die_arg;
22703 var_die = lookup_decl_die (decl_or_origin);
22704 if (var_die)
22706 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22708 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22709 if (loc)
22711 if (off)
22713 /* Optimize the common case. */
22714 if (single_element_loc_list_p (loc)
22715 && loc->expr->dw_loc_opc == DW_OP_addr
22716 && loc->expr->dw_loc_next == NULL
22717 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22718 == SYMBOL_REF)
22720 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22721 loc->expr->dw_loc_oprnd1.v.val_addr
22722 = plus_constant (GET_MODE (x), x , off);
22724 else
22725 loc_list_plus_const (loc, off);
22727 add_AT_location_description (var_die, DW_AT_location, loc);
22728 remove_AT (var_die, DW_AT_declaration);
22731 return;
22734 if (common_block_die_table == NULL)
22735 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22737 com_die_arg.decl_id = DECL_UID (com_decl);
22738 com_die_arg.die_parent = context_die;
22739 com_die = common_block_die_table->find (&com_die_arg);
22740 if (! early_dwarf)
22741 loc = loc_list_from_tree (com_decl, 2, NULL);
22742 if (com_die == NULL)
22744 const char *cnam
22745 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22746 die_node **slot;
22748 com_die = new_die (DW_TAG_common_block, context_die, decl);
22749 add_name_and_src_coords_attributes (com_die, com_decl);
22750 if (loc)
22752 add_AT_location_description (com_die, DW_AT_location, loc);
22753 /* Avoid sharing the same loc descriptor between
22754 DW_TAG_common_block and DW_TAG_variable. */
22755 loc = loc_list_from_tree (com_decl, 2, NULL);
22757 else if (DECL_EXTERNAL (decl_or_origin))
22758 add_AT_flag (com_die, DW_AT_declaration, 1);
22759 if (want_pubnames ())
22760 add_pubname_string (cnam, com_die); /* ??? needed? */
22761 com_die->decl_id = DECL_UID (com_decl);
22762 slot = common_block_die_table->find_slot (com_die, INSERT);
22763 *slot = com_die;
22765 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22767 add_AT_location_description (com_die, DW_AT_location, loc);
22768 loc = loc_list_from_tree (com_decl, 2, NULL);
22769 remove_AT (com_die, DW_AT_declaration);
22771 var_die = new_die (DW_TAG_variable, com_die, decl);
22772 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22773 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22774 decl_quals (decl_or_origin), false,
22775 context_die);
22776 add_alignment_attribute (var_die, decl);
22777 add_AT_flag (var_die, DW_AT_external, 1);
22778 if (loc)
22780 if (off)
22782 /* Optimize the common case. */
22783 if (single_element_loc_list_p (loc)
22784 && loc->expr->dw_loc_opc == DW_OP_addr
22785 && loc->expr->dw_loc_next == NULL
22786 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22788 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22789 loc->expr->dw_loc_oprnd1.v.val_addr
22790 = plus_constant (GET_MODE (x), x, off);
22792 else
22793 loc_list_plus_const (loc, off);
22795 add_AT_location_description (var_die, DW_AT_location, loc);
22797 else if (DECL_EXTERNAL (decl_or_origin))
22798 add_AT_flag (var_die, DW_AT_declaration, 1);
22799 if (decl)
22800 equate_decl_number_to_die (decl, var_die);
22801 return;
22804 if (old_die)
22806 if (declaration)
22808 /* A declaration that has been previously dumped, needs no
22809 further annotations, since it doesn't need location on
22810 the second pass. */
22811 return;
22813 else if (decl_will_get_specification_p (old_die, decl, declaration)
22814 && !get_AT (old_die, DW_AT_specification))
22816 /* Fall-thru so we can make a new variable die along with a
22817 DW_AT_specification. */
22819 else if (origin && old_die->die_parent != context_die)
22821 /* If we will be creating an inlined instance, we need a
22822 new DIE that will get annotated with
22823 DW_AT_abstract_origin. Clear things so we can get a
22824 new DIE. */
22825 gcc_assert (!DECL_ABSTRACT_P (decl));
22826 old_die = NULL;
22828 else
22830 /* If a DIE was dumped early, it still needs location info.
22831 Skip to where we fill the location bits. */
22832 var_die = old_die;
22833 goto gen_variable_die_location;
22837 /* For static data members, the declaration in the class is supposed
22838 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22839 also in DWARF2; the specification should still be DW_TAG_variable
22840 referencing the DW_TAG_member DIE. */
22841 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22842 var_die = new_die (DW_TAG_member, context_die, decl);
22843 else
22844 var_die = new_die (DW_TAG_variable, context_die, decl);
22846 if (origin != NULL)
22847 add_abstract_origin_attribute (var_die, origin);
22849 /* Loop unrolling can create multiple blocks that refer to the same
22850 static variable, so we must test for the DW_AT_declaration flag.
22852 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22853 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22854 sharing them.
22856 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22857 else if (decl_will_get_specification_p (old_die, decl, declaration))
22859 /* This is a definition of a C++ class level static. */
22860 add_AT_specification (var_die, old_die);
22861 specialization_p = true;
22862 if (DECL_NAME (decl))
22864 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22865 struct dwarf_file_data * file_index = lookup_filename (s.file);
22867 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22868 add_AT_file (var_die, DW_AT_decl_file, file_index);
22870 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22871 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22873 if (debug_column_info
22874 && s.column
22875 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22876 != (unsigned) s.column))
22877 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22879 if (old_die->die_tag == DW_TAG_member)
22880 add_linkage_name (var_die, decl);
22883 else
22884 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22886 if ((origin == NULL && !specialization_p)
22887 || (origin != NULL
22888 && !DECL_ABSTRACT_P (decl_or_origin)
22889 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22890 decl_function_context
22891 (decl_or_origin))))
22893 tree type = TREE_TYPE (decl_or_origin);
22895 if (decl_by_reference_p (decl_or_origin))
22896 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22897 context_die);
22898 else
22899 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22900 context_die);
22903 if (origin == NULL && !specialization_p)
22905 if (TREE_PUBLIC (decl))
22906 add_AT_flag (var_die, DW_AT_external, 1);
22908 if (DECL_ARTIFICIAL (decl))
22909 add_AT_flag (var_die, DW_AT_artificial, 1);
22911 add_alignment_attribute (var_die, decl);
22913 add_accessibility_attribute (var_die, decl);
22916 if (declaration)
22917 add_AT_flag (var_die, DW_AT_declaration, 1);
22919 if (decl && (DECL_ABSTRACT_P (decl)
22920 || !old_die || is_declaration_die (old_die)))
22921 equate_decl_number_to_die (decl, var_die);
22923 gen_variable_die_location:
22924 if (! declaration
22925 && (! DECL_ABSTRACT_P (decl_or_origin)
22926 /* Local static vars are shared between all clones/inlines,
22927 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22928 already set. */
22929 || (VAR_P (decl_or_origin)
22930 && TREE_STATIC (decl_or_origin)
22931 && DECL_RTL_SET_P (decl_or_origin))))
22933 if (early_dwarf)
22934 add_pubname (decl_or_origin, var_die);
22935 else
22936 add_location_or_const_value_attribute (var_die, decl_or_origin,
22937 decl == NULL);
22939 else
22940 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22942 if ((dwarf_version >= 4 || !dwarf_strict)
22943 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22944 DW_AT_const_expr) == 1
22945 && !get_AT (var_die, DW_AT_const_expr)
22946 && !specialization_p)
22947 add_AT_flag (var_die, DW_AT_const_expr, 1);
22949 if (!dwarf_strict)
22951 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22952 DW_AT_inline);
22953 if (inl != -1
22954 && !get_AT (var_die, DW_AT_inline)
22955 && !specialization_p)
22956 add_AT_unsigned (var_die, DW_AT_inline, inl);
22960 /* Generate a DIE to represent a named constant. */
22962 static void
22963 gen_const_die (tree decl, dw_die_ref context_die)
22965 dw_die_ref const_die;
22966 tree type = TREE_TYPE (decl);
22968 const_die = lookup_decl_die (decl);
22969 if (const_die)
22970 return;
22972 const_die = new_die (DW_TAG_constant, context_die, decl);
22973 equate_decl_number_to_die (decl, const_die);
22974 add_name_and_src_coords_attributes (const_die, decl);
22975 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22976 if (TREE_PUBLIC (decl))
22977 add_AT_flag (const_die, DW_AT_external, 1);
22978 if (DECL_ARTIFICIAL (decl))
22979 add_AT_flag (const_die, DW_AT_artificial, 1);
22980 tree_add_const_value_attribute_for_decl (const_die, decl);
22983 /* Generate a DIE to represent a label identifier. */
22985 static void
22986 gen_label_die (tree decl, dw_die_ref context_die)
22988 tree origin = decl_ultimate_origin (decl);
22989 dw_die_ref lbl_die = lookup_decl_die (decl);
22990 rtx insn;
22991 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22993 if (!lbl_die)
22995 lbl_die = new_die (DW_TAG_label, context_die, decl);
22996 equate_decl_number_to_die (decl, lbl_die);
22998 if (origin != NULL)
22999 add_abstract_origin_attribute (lbl_die, origin);
23000 else
23001 add_name_and_src_coords_attributes (lbl_die, decl);
23004 if (DECL_ABSTRACT_P (decl))
23005 equate_decl_number_to_die (decl, lbl_die);
23006 else if (! early_dwarf)
23008 insn = DECL_RTL_IF_SET (decl);
23010 /* Deleted labels are programmer specified labels which have been
23011 eliminated because of various optimizations. We still emit them
23012 here so that it is possible to put breakpoints on them. */
23013 if (insn
23014 && (LABEL_P (insn)
23015 || ((NOTE_P (insn)
23016 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23018 /* When optimization is enabled (via -O) some parts of the compiler
23019 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23020 represent source-level labels which were explicitly declared by
23021 the user. This really shouldn't be happening though, so catch
23022 it if it ever does happen. */
23023 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23025 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23026 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23028 else if (insn
23029 && NOTE_P (insn)
23030 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23031 && CODE_LABEL_NUMBER (insn) != -1)
23033 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23034 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23039 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23040 attributes to the DIE for a block STMT, to describe where the inlined
23041 function was called from. This is similar to add_src_coords_attributes. */
23043 static inline void
23044 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23046 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23048 if (dwarf_version >= 3 || !dwarf_strict)
23050 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23051 add_AT_unsigned (die, DW_AT_call_line, s.line);
23052 if (debug_column_info && s.column)
23053 add_AT_unsigned (die, DW_AT_call_column, s.column);
23058 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23059 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23061 static inline void
23062 add_high_low_attributes (tree stmt, dw_die_ref die)
23064 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23066 if (BLOCK_FRAGMENT_CHAIN (stmt)
23067 && (dwarf_version >= 3 || !dwarf_strict))
23069 tree chain, superblock = NULL_TREE;
23070 dw_die_ref pdie;
23071 dw_attr_node *attr = NULL;
23073 if (inlined_function_outer_scope_p (stmt))
23075 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23076 BLOCK_NUMBER (stmt));
23077 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23080 /* Optimize duplicate .debug_ranges lists or even tails of
23081 lists. If this BLOCK has same ranges as its supercontext,
23082 lookup DW_AT_ranges attribute in the supercontext (and
23083 recursively so), verify that the ranges_table contains the
23084 right values and use it instead of adding a new .debug_range. */
23085 for (chain = stmt, pdie = die;
23086 BLOCK_SAME_RANGE (chain);
23087 chain = BLOCK_SUPERCONTEXT (chain))
23089 dw_attr_node *new_attr;
23091 pdie = pdie->die_parent;
23092 if (pdie == NULL)
23093 break;
23094 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23095 break;
23096 new_attr = get_AT (pdie, DW_AT_ranges);
23097 if (new_attr == NULL
23098 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23099 break;
23100 attr = new_attr;
23101 superblock = BLOCK_SUPERCONTEXT (chain);
23103 if (attr != NULL
23104 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23105 == BLOCK_NUMBER (superblock))
23106 && BLOCK_FRAGMENT_CHAIN (superblock))
23108 unsigned long off = attr->dw_attr_val.v.val_offset;
23109 unsigned long supercnt = 0, thiscnt = 0;
23110 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23111 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23113 ++supercnt;
23114 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23115 == BLOCK_NUMBER (chain));
23117 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23118 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23119 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23120 ++thiscnt;
23121 gcc_assert (supercnt >= thiscnt);
23122 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23123 false);
23124 note_rnglist_head (off + supercnt - thiscnt);
23125 return;
23128 unsigned int offset = add_ranges (stmt, true);
23129 add_AT_range_list (die, DW_AT_ranges, offset, false);
23130 note_rnglist_head (offset);
23132 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23133 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23136 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23137 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23138 chain = BLOCK_FRAGMENT_CHAIN (chain);
23140 while (chain);
23141 add_ranges (NULL);
23143 else
23145 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23146 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23147 BLOCK_NUMBER (stmt));
23148 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23149 BLOCK_NUMBER (stmt));
23150 add_AT_low_high_pc (die, label, label_high, false);
23154 /* Generate a DIE for a lexical block. */
23156 static void
23157 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23159 dw_die_ref old_die = BLOCK_DIE (stmt);
23160 dw_die_ref stmt_die = NULL;
23161 if (!old_die)
23163 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23164 BLOCK_DIE (stmt) = stmt_die;
23167 if (BLOCK_ABSTRACT (stmt))
23169 if (old_die)
23171 /* This must have been generated early and it won't even
23172 need location information since it's a DW_AT_inline
23173 function. */
23174 if (flag_checking)
23175 for (dw_die_ref c = context_die; c; c = c->die_parent)
23176 if (c->die_tag == DW_TAG_inlined_subroutine
23177 || c->die_tag == DW_TAG_subprogram)
23179 gcc_assert (get_AT (c, DW_AT_inline));
23180 break;
23182 return;
23185 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23187 /* If this is an inlined instance, create a new lexical die for
23188 anything below to attach DW_AT_abstract_origin to. */
23189 if (old_die)
23191 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23192 BLOCK_DIE (stmt) = stmt_die;
23193 old_die = NULL;
23196 tree origin = block_ultimate_origin (stmt);
23197 if (origin != NULL_TREE && origin != stmt)
23198 add_abstract_origin_attribute (stmt_die, origin);
23201 if (old_die)
23202 stmt_die = old_die;
23204 /* A non abstract block whose blocks have already been reordered
23205 should have the instruction range for this block. If so, set the
23206 high/low attributes. */
23207 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23209 gcc_assert (stmt_die);
23210 add_high_low_attributes (stmt, stmt_die);
23213 decls_for_scope (stmt, stmt_die);
23216 /* Generate a DIE for an inlined subprogram. */
23218 static void
23219 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23221 tree decl;
23223 /* The instance of function that is effectively being inlined shall not
23224 be abstract. */
23225 gcc_assert (! BLOCK_ABSTRACT (stmt));
23227 decl = block_ultimate_origin (stmt);
23229 /* Make sure any inlined functions are known to be inlineable. */
23230 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23231 || cgraph_function_possibly_inlined_p (decl));
23233 /* Emit info for the abstract instance first, if we haven't yet. We
23234 must emit this even if the block is abstract, otherwise when we
23235 emit the block below (or elsewhere), we may end up trying to emit
23236 a die whose origin die hasn't been emitted, and crashing. */
23237 dwarf2out_abstract_function (decl);
23239 if (! BLOCK_ABSTRACT (stmt))
23241 dw_die_ref subr_die
23242 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23244 if (call_arg_locations)
23245 BLOCK_DIE (stmt) = subr_die;
23246 add_abstract_origin_attribute (subr_die, decl);
23247 if (TREE_ASM_WRITTEN (stmt))
23248 add_high_low_attributes (stmt, subr_die);
23249 add_call_src_coords_attributes (stmt, subr_die);
23251 decls_for_scope (stmt, subr_die);
23255 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23256 the comment for VLR_CONTEXT. */
23258 static void
23259 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23261 dw_die_ref decl_die;
23263 if (TREE_TYPE (decl) == error_mark_node)
23264 return;
23266 decl_die = new_die (DW_TAG_member, context_die, decl);
23267 add_name_and_src_coords_attributes (decl_die, decl);
23268 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23269 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23270 context_die);
23272 if (DECL_BIT_FIELD_TYPE (decl))
23274 add_byte_size_attribute (decl_die, decl);
23275 add_bit_size_attribute (decl_die, decl);
23276 add_bit_offset_attribute (decl_die, decl, ctx);
23279 add_alignment_attribute (decl_die, decl);
23281 /* If we have a variant part offset, then we are supposed to process a member
23282 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23283 trees. */
23284 gcc_assert (ctx->variant_part_offset == NULL_TREE
23285 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23286 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23287 add_data_member_location_attribute (decl_die, decl, ctx);
23289 if (DECL_ARTIFICIAL (decl))
23290 add_AT_flag (decl_die, DW_AT_artificial, 1);
23292 add_accessibility_attribute (decl_die, decl);
23294 /* Equate decl number to die, so that we can look up this decl later on. */
23295 equate_decl_number_to_die (decl, decl_die);
23298 /* Generate a DIE for a pointer to a member type. TYPE can be an
23299 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23300 pointer to member function. */
23302 static void
23303 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23305 if (lookup_type_die (type))
23306 return;
23308 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23309 scope_die_for (type, context_die), type);
23311 equate_type_number_to_die (type, ptr_die);
23312 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23313 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23314 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23315 context_die);
23316 add_alignment_attribute (ptr_die, type);
23318 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23319 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23321 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23322 add_AT_loc (ptr_die, DW_AT_use_location, op);
23326 static char *producer_string;
23328 /* Return a heap allocated producer string including command line options
23329 if -grecord-gcc-switches. */
23331 static char *
23332 gen_producer_string (void)
23334 size_t j;
23335 auto_vec<const char *> switches;
23336 const char *language_string = lang_hooks.name;
23337 char *producer, *tail;
23338 const char *p;
23339 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23340 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23342 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23343 switch (save_decoded_options[j].opt_index)
23345 case OPT_o:
23346 case OPT_d:
23347 case OPT_dumpbase:
23348 case OPT_dumpdir:
23349 case OPT_auxbase:
23350 case OPT_auxbase_strip:
23351 case OPT_quiet:
23352 case OPT_version:
23353 case OPT_v:
23354 case OPT_w:
23355 case OPT_L:
23356 case OPT_D:
23357 case OPT_I:
23358 case OPT_U:
23359 case OPT_SPECIAL_unknown:
23360 case OPT_SPECIAL_ignore:
23361 case OPT_SPECIAL_program_name:
23362 case OPT_SPECIAL_input_file:
23363 case OPT_grecord_gcc_switches:
23364 case OPT_gno_record_gcc_switches:
23365 case OPT__output_pch_:
23366 case OPT_fdiagnostics_show_location_:
23367 case OPT_fdiagnostics_show_option:
23368 case OPT_fdiagnostics_show_caret:
23369 case OPT_fdiagnostics_color_:
23370 case OPT_fverbose_asm:
23371 case OPT____:
23372 case OPT__sysroot_:
23373 case OPT_nostdinc:
23374 case OPT_nostdinc__:
23375 case OPT_fpreprocessed:
23376 case OPT_fltrans_output_list_:
23377 case OPT_fresolution_:
23378 case OPT_fdebug_prefix_map_:
23379 /* Ignore these. */
23380 continue;
23381 default:
23382 if (cl_options[save_decoded_options[j].opt_index].flags
23383 & CL_NO_DWARF_RECORD)
23384 continue;
23385 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23386 == '-');
23387 switch (save_decoded_options[j].canonical_option[0][1])
23389 case 'M':
23390 case 'i':
23391 case 'W':
23392 continue;
23393 case 'f':
23394 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23395 "dump", 4) == 0)
23396 continue;
23397 break;
23398 default:
23399 break;
23401 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23402 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23403 break;
23406 producer = XNEWVEC (char, plen + 1 + len + 1);
23407 tail = producer;
23408 sprintf (tail, "%s %s", language_string, version_string);
23409 tail += plen;
23411 FOR_EACH_VEC_ELT (switches, j, p)
23413 len = strlen (p);
23414 *tail = ' ';
23415 memcpy (tail + 1, p, len);
23416 tail += len + 1;
23419 *tail = '\0';
23420 return producer;
23423 /* Given a C and/or C++ language/version string return the "highest".
23424 C++ is assumed to be "higher" than C in this case. Used for merging
23425 LTO translation unit languages. */
23426 static const char *
23427 highest_c_language (const char *lang1, const char *lang2)
23429 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23430 return "GNU C++14";
23431 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23432 return "GNU C++11";
23433 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23434 return "GNU C++98";
23436 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23437 return "GNU C11";
23438 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23439 return "GNU C99";
23440 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23441 return "GNU C89";
23443 gcc_unreachable ();
23447 /* Generate the DIE for the compilation unit. */
23449 static dw_die_ref
23450 gen_compile_unit_die (const char *filename)
23452 dw_die_ref die;
23453 const char *language_string = lang_hooks.name;
23454 int language;
23456 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23458 if (filename)
23460 add_name_attribute (die, filename);
23461 /* Don't add cwd for <built-in>. */
23462 if (filename[0] != '<')
23463 add_comp_dir_attribute (die);
23466 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23468 /* If our producer is LTO try to figure out a common language to use
23469 from the global list of translation units. */
23470 if (strcmp (language_string, "GNU GIMPLE") == 0)
23472 unsigned i;
23473 tree t;
23474 const char *common_lang = NULL;
23476 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23478 if (!TRANSLATION_UNIT_LANGUAGE (t))
23479 continue;
23480 if (!common_lang)
23481 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23482 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23484 else if (strncmp (common_lang, "GNU C", 5) == 0
23485 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23486 /* Mixing C and C++ is ok, use C++ in that case. */
23487 common_lang = highest_c_language (common_lang,
23488 TRANSLATION_UNIT_LANGUAGE (t));
23489 else
23491 /* Fall back to C. */
23492 common_lang = NULL;
23493 break;
23497 if (common_lang)
23498 language_string = common_lang;
23501 language = DW_LANG_C;
23502 if (strncmp (language_string, "GNU C", 5) == 0
23503 && ISDIGIT (language_string[5]))
23505 language = DW_LANG_C89;
23506 if (dwarf_version >= 3 || !dwarf_strict)
23508 if (strcmp (language_string, "GNU C89") != 0)
23509 language = DW_LANG_C99;
23511 if (dwarf_version >= 5 /* || !dwarf_strict */)
23512 if (strcmp (language_string, "GNU C11") == 0)
23513 language = DW_LANG_C11;
23516 else if (strncmp (language_string, "GNU C++", 7) == 0)
23518 language = DW_LANG_C_plus_plus;
23519 if (dwarf_version >= 5 /* || !dwarf_strict */)
23521 if (strcmp (language_string, "GNU C++11") == 0)
23522 language = DW_LANG_C_plus_plus_11;
23523 else if (strcmp (language_string, "GNU C++14") == 0)
23524 language = DW_LANG_C_plus_plus_14;
23527 else if (strcmp (language_string, "GNU F77") == 0)
23528 language = DW_LANG_Fortran77;
23529 else if (dwarf_version >= 3 || !dwarf_strict)
23531 if (strcmp (language_string, "GNU Ada") == 0)
23532 language = DW_LANG_Ada95;
23533 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23535 language = DW_LANG_Fortran95;
23536 if (dwarf_version >= 5 /* || !dwarf_strict */)
23538 if (strcmp (language_string, "GNU Fortran2003") == 0)
23539 language = DW_LANG_Fortran03;
23540 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23541 language = DW_LANG_Fortran08;
23544 else if (strcmp (language_string, "GNU Objective-C") == 0)
23545 language = DW_LANG_ObjC;
23546 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23547 language = DW_LANG_ObjC_plus_plus;
23548 else if (dwarf_version >= 5 || !dwarf_strict)
23550 if (strcmp (language_string, "GNU Go") == 0)
23551 language = DW_LANG_Go;
23554 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23555 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23556 language = DW_LANG_Fortran90;
23558 add_AT_unsigned (die, DW_AT_language, language);
23560 switch (language)
23562 case DW_LANG_Fortran77:
23563 case DW_LANG_Fortran90:
23564 case DW_LANG_Fortran95:
23565 case DW_LANG_Fortran03:
23566 case DW_LANG_Fortran08:
23567 /* Fortran has case insensitive identifiers and the front-end
23568 lowercases everything. */
23569 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23570 break;
23571 default:
23572 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23573 break;
23575 return die;
23578 /* Generate the DIE for a base class. */
23580 static void
23581 gen_inheritance_die (tree binfo, tree access, tree type,
23582 dw_die_ref context_die)
23584 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23585 struct vlr_context ctx = { type, NULL };
23587 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23588 context_die);
23589 add_data_member_location_attribute (die, binfo, &ctx);
23591 if (BINFO_VIRTUAL_P (binfo))
23592 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23594 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23595 children, otherwise the default is DW_ACCESS_public. In DWARF2
23596 the default has always been DW_ACCESS_private. */
23597 if (access == access_public_node)
23599 if (dwarf_version == 2
23600 || context_die->die_tag == DW_TAG_class_type)
23601 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23603 else if (access == access_protected_node)
23604 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23605 else if (dwarf_version > 2
23606 && context_die->die_tag != DW_TAG_class_type)
23607 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23610 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23611 structure. */
23612 static bool
23613 is_variant_part (tree decl)
23615 return (TREE_CODE (decl) == FIELD_DECL
23616 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23619 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23620 return the FIELD_DECL. Return NULL_TREE otherwise. */
23622 static tree
23623 analyze_discr_in_predicate (tree operand, tree struct_type)
23625 bool continue_stripping = true;
23626 while (continue_stripping)
23627 switch (TREE_CODE (operand))
23629 CASE_CONVERT:
23630 operand = TREE_OPERAND (operand, 0);
23631 break;
23632 default:
23633 continue_stripping = false;
23634 break;
23637 /* Match field access to members of struct_type only. */
23638 if (TREE_CODE (operand) == COMPONENT_REF
23639 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23640 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23641 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23642 return TREE_OPERAND (operand, 1);
23643 else
23644 return NULL_TREE;
23647 /* Check that SRC is a constant integer that can be represented as a native
23648 integer constant (either signed or unsigned). If so, store it into DEST and
23649 return true. Return false otherwise. */
23651 static bool
23652 get_discr_value (tree src, dw_discr_value *dest)
23654 tree discr_type = TREE_TYPE (src);
23656 if (lang_hooks.types.get_debug_type)
23658 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23659 if (debug_type != NULL)
23660 discr_type = debug_type;
23663 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23664 return false;
23666 /* Signedness can vary between the original type and the debug type. This
23667 can happen for character types in Ada for instance: the character type
23668 used for code generation can be signed, to be compatible with the C one,
23669 but from a debugger point of view, it must be unsigned. */
23670 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23671 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23673 if (is_orig_unsigned != is_debug_unsigned)
23674 src = fold_convert (discr_type, src);
23676 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23677 return false;
23679 dest->pos = is_debug_unsigned;
23680 if (is_debug_unsigned)
23681 dest->v.uval = tree_to_uhwi (src);
23682 else
23683 dest->v.sval = tree_to_shwi (src);
23685 return true;
23688 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23689 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23690 store NULL_TREE in DISCR_DECL. Otherwise:
23692 - store the discriminant field in STRUCT_TYPE that controls the variant
23693 part to *DISCR_DECL
23695 - put in *DISCR_LISTS_P an array where for each variant, the item
23696 represents the corresponding matching list of discriminant values.
23698 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23699 the above array.
23701 Note that when the array is allocated (i.e. when the analysis is
23702 successful), it is up to the caller to free the array. */
23704 static void
23705 analyze_variants_discr (tree variant_part_decl,
23706 tree struct_type,
23707 tree *discr_decl,
23708 dw_discr_list_ref **discr_lists_p,
23709 unsigned *discr_lists_length)
23711 tree variant_part_type = TREE_TYPE (variant_part_decl);
23712 tree variant;
23713 dw_discr_list_ref *discr_lists;
23714 unsigned i;
23716 /* Compute how many variants there are in this variant part. */
23717 *discr_lists_length = 0;
23718 for (variant = TYPE_FIELDS (variant_part_type);
23719 variant != NULL_TREE;
23720 variant = DECL_CHAIN (variant))
23721 ++*discr_lists_length;
23723 *discr_decl = NULL_TREE;
23724 *discr_lists_p
23725 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23726 sizeof (**discr_lists_p));
23727 discr_lists = *discr_lists_p;
23729 /* And then analyze all variants to extract discriminant information for all
23730 of them. This analysis is conservative: as soon as we detect something we
23731 do not support, abort everything and pretend we found nothing. */
23732 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23733 variant != NULL_TREE;
23734 variant = DECL_CHAIN (variant), ++i)
23736 tree match_expr = DECL_QUALIFIER (variant);
23738 /* Now, try to analyze the predicate and deduce a discriminant for
23739 it. */
23740 if (match_expr == boolean_true_node)
23741 /* Typically happens for the default variant: it matches all cases that
23742 previous variants rejected. Don't output any matching value for
23743 this one. */
23744 continue;
23746 /* The following loop tries to iterate over each discriminant
23747 possibility: single values or ranges. */
23748 while (match_expr != NULL_TREE)
23750 tree next_round_match_expr;
23751 tree candidate_discr = NULL_TREE;
23752 dw_discr_list_ref new_node = NULL;
23754 /* Possibilities are matched one after the other by nested
23755 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23756 continue with the rest at next iteration. */
23757 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23759 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23760 match_expr = TREE_OPERAND (match_expr, 1);
23762 else
23763 next_round_match_expr = NULL_TREE;
23765 if (match_expr == boolean_false_node)
23766 /* This sub-expression matches nothing: just wait for the next
23767 one. */
23770 else if (TREE_CODE (match_expr) == EQ_EXPR)
23772 /* We are matching: <discr_field> == <integer_cst>
23773 This sub-expression matches a single value. */
23774 tree integer_cst = TREE_OPERAND (match_expr, 1);
23776 candidate_discr
23777 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23778 struct_type);
23780 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23781 if (!get_discr_value (integer_cst,
23782 &new_node->dw_discr_lower_bound))
23783 goto abort;
23784 new_node->dw_discr_range = false;
23787 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23789 /* We are matching:
23790 <discr_field> > <integer_cst>
23791 && <discr_field> < <integer_cst>.
23792 This sub-expression matches the range of values between the
23793 two matched integer constants. Note that comparisons can be
23794 inclusive or exclusive. */
23795 tree candidate_discr_1, candidate_discr_2;
23796 tree lower_cst, upper_cst;
23797 bool lower_cst_included, upper_cst_included;
23798 tree lower_op = TREE_OPERAND (match_expr, 0);
23799 tree upper_op = TREE_OPERAND (match_expr, 1);
23801 /* When the comparison is exclusive, the integer constant is not
23802 the discriminant range bound we are looking for: we will have
23803 to increment or decrement it. */
23804 if (TREE_CODE (lower_op) == GE_EXPR)
23805 lower_cst_included = true;
23806 else if (TREE_CODE (lower_op) == GT_EXPR)
23807 lower_cst_included = false;
23808 else
23809 goto abort;
23811 if (TREE_CODE (upper_op) == LE_EXPR)
23812 upper_cst_included = true;
23813 else if (TREE_CODE (upper_op) == LT_EXPR)
23814 upper_cst_included = false;
23815 else
23816 goto abort;
23818 /* Extract the discriminant from the first operand and check it
23819 is consistant with the same analysis in the second
23820 operand. */
23821 candidate_discr_1
23822 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23823 struct_type);
23824 candidate_discr_2
23825 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23826 struct_type);
23827 if (candidate_discr_1 == candidate_discr_2)
23828 candidate_discr = candidate_discr_1;
23829 else
23830 goto abort;
23832 /* Extract bounds from both. */
23833 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23834 lower_cst = TREE_OPERAND (lower_op, 1);
23835 upper_cst = TREE_OPERAND (upper_op, 1);
23837 if (!lower_cst_included)
23838 lower_cst
23839 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23840 build_int_cst (TREE_TYPE (lower_cst), 1));
23841 if (!upper_cst_included)
23842 upper_cst
23843 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23844 build_int_cst (TREE_TYPE (upper_cst), 1));
23846 if (!get_discr_value (lower_cst,
23847 &new_node->dw_discr_lower_bound)
23848 || !get_discr_value (upper_cst,
23849 &new_node->dw_discr_upper_bound))
23850 goto abort;
23852 new_node->dw_discr_range = true;
23855 else
23856 /* Unsupported sub-expression: we cannot determine the set of
23857 matching discriminant values. Abort everything. */
23858 goto abort;
23860 /* If the discriminant info is not consistant with what we saw so
23861 far, consider the analysis failed and abort everything. */
23862 if (candidate_discr == NULL_TREE
23863 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23864 goto abort;
23865 else
23866 *discr_decl = candidate_discr;
23868 if (new_node != NULL)
23870 new_node->dw_discr_next = discr_lists[i];
23871 discr_lists[i] = new_node;
23873 match_expr = next_round_match_expr;
23877 /* If we reach this point, we could match everything we were interested
23878 in. */
23879 return;
23881 abort:
23882 /* Clean all data structure and return no result. */
23883 free (*discr_lists_p);
23884 *discr_lists_p = NULL;
23885 *discr_decl = NULL_TREE;
23888 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23889 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23890 under CONTEXT_DIE.
23892 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23893 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23894 this type, which are record types, represent the available variants and each
23895 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23896 values are inferred from these attributes.
23898 In trees, the offsets for the fields inside these sub-records are relative
23899 to the variant part itself, whereas the corresponding DIEs should have
23900 offset attributes that are relative to the embedding record base address.
23901 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23902 must be an expression that computes the offset of the variant part to
23903 describe in DWARF. */
23905 static void
23906 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23907 dw_die_ref context_die)
23909 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23910 tree variant_part_offset = vlr_ctx->variant_part_offset;
23911 struct loc_descr_context ctx = {
23912 vlr_ctx->struct_type, /* context_type */
23913 NULL_TREE, /* base_decl */
23914 NULL, /* dpi */
23915 false, /* placeholder_arg */
23916 false /* placeholder_seen */
23919 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23920 NULL_TREE if there is no such field. */
23921 tree discr_decl = NULL_TREE;
23922 dw_discr_list_ref *discr_lists;
23923 unsigned discr_lists_length = 0;
23924 unsigned i;
23926 dw_die_ref dwarf_proc_die = NULL;
23927 dw_die_ref variant_part_die
23928 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23930 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23932 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23933 &discr_decl, &discr_lists, &discr_lists_length);
23935 if (discr_decl != NULL_TREE)
23937 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23939 if (discr_die)
23940 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23941 else
23942 /* We have no DIE for the discriminant, so just discard all
23943 discrimimant information in the output. */
23944 discr_decl = NULL_TREE;
23947 /* If the offset for this variant part is more complex than a constant,
23948 create a DWARF procedure for it so that we will not have to generate DWARF
23949 expressions for it for each member. */
23950 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23951 && (dwarf_version >= 3 || !dwarf_strict))
23953 const tree dwarf_proc_fndecl
23954 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23955 build_function_type (TREE_TYPE (variant_part_offset),
23956 NULL_TREE));
23957 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23958 const dw_loc_descr_ref dwarf_proc_body
23959 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23961 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23962 dwarf_proc_fndecl, context_die);
23963 if (dwarf_proc_die != NULL)
23964 variant_part_offset = dwarf_proc_call;
23967 /* Output DIEs for all variants. */
23968 i = 0;
23969 for (tree variant = TYPE_FIELDS (variant_part_type);
23970 variant != NULL_TREE;
23971 variant = DECL_CHAIN (variant), ++i)
23973 tree variant_type = TREE_TYPE (variant);
23974 dw_die_ref variant_die;
23976 /* All variants (i.e. members of a variant part) are supposed to be
23977 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23978 under these records. */
23979 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23981 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23982 equate_decl_number_to_die (variant, variant_die);
23984 /* Output discriminant values this variant matches, if any. */
23985 if (discr_decl == NULL || discr_lists[i] == NULL)
23986 /* In the case we have discriminant information at all, this is
23987 probably the default variant: as the standard says, don't
23988 output any discriminant value/list attribute. */
23990 else if (discr_lists[i]->dw_discr_next == NULL
23991 && !discr_lists[i]->dw_discr_range)
23992 /* If there is only one accepted value, don't bother outputting a
23993 list. */
23994 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23995 else
23996 add_discr_list (variant_die, discr_lists[i]);
23998 for (tree member = TYPE_FIELDS (variant_type);
23999 member != NULL_TREE;
24000 member = DECL_CHAIN (member))
24002 struct vlr_context vlr_sub_ctx = {
24003 vlr_ctx->struct_type, /* struct_type */
24004 NULL /* variant_part_offset */
24006 if (is_variant_part (member))
24008 /* All offsets for fields inside variant parts are relative to
24009 the top-level embedding RECORD_TYPE's base address. On the
24010 other hand, offsets in GCC's types are relative to the
24011 nested-most variant part. So we have to sum offsets each time
24012 we recurse. */
24014 vlr_sub_ctx.variant_part_offset
24015 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24016 variant_part_offset, byte_position (member));
24017 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24019 else
24021 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24022 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24027 free (discr_lists);
24030 /* Generate a DIE for a class member. */
24032 static void
24033 gen_member_die (tree type, dw_die_ref context_die)
24035 tree member;
24036 tree binfo = TYPE_BINFO (type);
24038 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24040 /* If this is not an incomplete type, output descriptions of each of its
24041 members. Note that as we output the DIEs necessary to represent the
24042 members of this record or union type, we will also be trying to output
24043 DIEs to represent the *types* of those members. However the `type'
24044 function (above) will specifically avoid generating type DIEs for member
24045 types *within* the list of member DIEs for this (containing) type except
24046 for those types (of members) which are explicitly marked as also being
24047 members of this (containing) type themselves. The g++ front- end can
24048 force any given type to be treated as a member of some other (containing)
24049 type by setting the TYPE_CONTEXT of the given (member) type to point to
24050 the TREE node representing the appropriate (containing) type. */
24052 /* First output info about the base classes. */
24053 if (binfo)
24055 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24056 int i;
24057 tree base;
24059 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24060 gen_inheritance_die (base,
24061 (accesses ? (*accesses)[i] : access_public_node),
24062 type,
24063 context_die);
24066 /* Now output info about the data members and type members. */
24067 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24069 struct vlr_context vlr_ctx = { type, NULL_TREE };
24070 bool static_inline_p
24071 = (TREE_STATIC (member)
24072 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24073 != -1));
24075 /* Ignore clones. */
24076 if (DECL_ABSTRACT_ORIGIN (member))
24077 continue;
24079 /* If we thought we were generating minimal debug info for TYPE
24080 and then changed our minds, some of the member declarations
24081 may have already been defined. Don't define them again, but
24082 do put them in the right order. */
24084 if (dw_die_ref child = lookup_decl_die (member))
24086 /* Handle inline static data members, which only have in-class
24087 declarations. */
24088 dw_die_ref ref = NULL;
24089 if (child->die_tag == DW_TAG_variable
24090 && child->die_parent == comp_unit_die ())
24092 ref = get_AT_ref (child, DW_AT_specification);
24093 /* For C++17 inline static data members followed by redundant
24094 out of class redeclaration, we might get here with
24095 child being the DIE created for the out of class
24096 redeclaration and with its DW_AT_specification being
24097 the DIE created for in-class definition. We want to
24098 reparent the latter, and don't want to create another
24099 DIE with DW_AT_specification in that case, because
24100 we already have one. */
24101 if (ref
24102 && static_inline_p
24103 && ref->die_tag == DW_TAG_variable
24104 && ref->die_parent == comp_unit_die ()
24105 && get_AT (ref, DW_AT_specification) == NULL)
24107 child = ref;
24108 ref = NULL;
24109 static_inline_p = false;
24113 if (child->die_tag == DW_TAG_variable
24114 && child->die_parent == comp_unit_die ()
24115 && ref == NULL)
24117 reparent_child (child, context_die);
24118 if (dwarf_version < 5)
24119 child->die_tag = DW_TAG_member;
24121 else
24122 splice_child_die (context_die, child);
24125 /* Do not generate standard DWARF for variant parts if we are generating
24126 the corresponding GNAT encodings: DIEs generated for both would
24127 conflict in our mappings. */
24128 else if (is_variant_part (member)
24129 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24131 vlr_ctx.variant_part_offset = byte_position (member);
24132 gen_variant_part (member, &vlr_ctx, context_die);
24134 else
24136 vlr_ctx.variant_part_offset = NULL_TREE;
24137 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24140 /* For C++ inline static data members emit immediately a DW_TAG_variable
24141 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24142 DW_AT_specification. */
24143 if (static_inline_p)
24145 int old_extern = DECL_EXTERNAL (member);
24146 DECL_EXTERNAL (member) = 0;
24147 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24148 DECL_EXTERNAL (member) = old_extern;
24153 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24154 is set, we pretend that the type was never defined, so we only get the
24155 member DIEs needed by later specification DIEs. */
24157 static void
24158 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24159 enum debug_info_usage usage)
24161 if (TREE_ASM_WRITTEN (type))
24163 /* Fill in the bound of variable-length fields in late dwarf if
24164 still incomplete. */
24165 if (!early_dwarf && variably_modified_type_p (type, NULL))
24166 for (tree member = TYPE_FIELDS (type);
24167 member;
24168 member = DECL_CHAIN (member))
24169 fill_variable_array_bounds (TREE_TYPE (member));
24170 return;
24173 dw_die_ref type_die = lookup_type_die (type);
24174 dw_die_ref scope_die = 0;
24175 int nested = 0;
24176 int complete = (TYPE_SIZE (type)
24177 && (! TYPE_STUB_DECL (type)
24178 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24179 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24180 complete = complete && should_emit_struct_debug (type, usage);
24182 if (type_die && ! complete)
24183 return;
24185 if (TYPE_CONTEXT (type) != NULL_TREE
24186 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24187 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24188 nested = 1;
24190 scope_die = scope_die_for (type, context_die);
24192 /* Generate child dies for template paramaters. */
24193 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24194 schedule_generic_params_dies_gen (type);
24196 if (! type_die || (nested && is_cu_die (scope_die)))
24197 /* First occurrence of type or toplevel definition of nested class. */
24199 dw_die_ref old_die = type_die;
24201 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24202 ? record_type_tag (type) : DW_TAG_union_type,
24203 scope_die, type);
24204 equate_type_number_to_die (type, type_die);
24205 if (old_die)
24206 add_AT_specification (type_die, old_die);
24207 else
24208 add_name_attribute (type_die, type_tag (type));
24210 else
24211 remove_AT (type_die, DW_AT_declaration);
24213 /* If this type has been completed, then give it a byte_size attribute and
24214 then give a list of members. */
24215 if (complete && !ns_decl)
24217 /* Prevent infinite recursion in cases where the type of some member of
24218 this type is expressed in terms of this type itself. */
24219 TREE_ASM_WRITTEN (type) = 1;
24220 add_byte_size_attribute (type_die, type);
24221 add_alignment_attribute (type_die, type);
24222 if (TYPE_STUB_DECL (type) != NULL_TREE)
24224 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24225 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24228 /* If the first reference to this type was as the return type of an
24229 inline function, then it may not have a parent. Fix this now. */
24230 if (type_die->die_parent == NULL)
24231 add_child_die (scope_die, type_die);
24233 push_decl_scope (type);
24234 gen_member_die (type, type_die);
24235 pop_decl_scope ();
24237 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24238 if (TYPE_ARTIFICIAL (type))
24239 add_AT_flag (type_die, DW_AT_artificial, 1);
24241 /* GNU extension: Record what type our vtable lives in. */
24242 if (TYPE_VFIELD (type))
24244 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24246 gen_type_die (vtype, context_die);
24247 add_AT_die_ref (type_die, DW_AT_containing_type,
24248 lookup_type_die (vtype));
24251 else
24253 add_AT_flag (type_die, DW_AT_declaration, 1);
24255 /* We don't need to do this for function-local types. */
24256 if (TYPE_STUB_DECL (type)
24257 && ! decl_function_context (TYPE_STUB_DECL (type)))
24258 vec_safe_push (incomplete_types, type);
24261 if (get_AT (type_die, DW_AT_name))
24262 add_pubtype (type, type_die);
24265 /* Generate a DIE for a subroutine _type_. */
24267 static void
24268 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24270 tree return_type = TREE_TYPE (type);
24271 dw_die_ref subr_die
24272 = new_die (DW_TAG_subroutine_type,
24273 scope_die_for (type, context_die), type);
24275 equate_type_number_to_die (type, subr_die);
24276 add_prototyped_attribute (subr_die, type);
24277 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24278 context_die);
24279 add_alignment_attribute (subr_die, type);
24280 gen_formal_types_die (type, subr_die);
24282 if (get_AT (subr_die, DW_AT_name))
24283 add_pubtype (type, subr_die);
24284 if ((dwarf_version >= 5 || !dwarf_strict)
24285 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24286 add_AT_flag (subr_die, DW_AT_reference, 1);
24287 if ((dwarf_version >= 5 || !dwarf_strict)
24288 && lang_hooks.types.type_dwarf_attribute (type,
24289 DW_AT_rvalue_reference) != -1)
24290 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24293 /* Generate a DIE for a type definition. */
24295 static void
24296 gen_typedef_die (tree decl, dw_die_ref context_die)
24298 dw_die_ref type_die;
24299 tree origin;
24301 if (TREE_ASM_WRITTEN (decl))
24303 if (DECL_ORIGINAL_TYPE (decl))
24304 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24305 return;
24308 TREE_ASM_WRITTEN (decl) = 1;
24309 type_die = new_die (DW_TAG_typedef, context_die, decl);
24310 origin = decl_ultimate_origin (decl);
24311 if (origin != NULL)
24312 add_abstract_origin_attribute (type_die, origin);
24313 else
24315 tree type = TREE_TYPE (decl);
24317 if (type == error_mark_node)
24318 return;
24320 add_name_and_src_coords_attributes (type_die, decl);
24321 if (DECL_ORIGINAL_TYPE (decl))
24323 type = DECL_ORIGINAL_TYPE (decl);
24325 if (type == error_mark_node)
24326 return;
24328 gcc_assert (type != TREE_TYPE (decl));
24329 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24331 else
24333 if (is_naming_typedef_decl (TYPE_NAME (type)))
24335 /* Here, we are in the case of decl being a typedef naming
24336 an anonymous type, e.g:
24337 typedef struct {...} foo;
24338 In that case TREE_TYPE (decl) is not a typedef variant
24339 type and TYPE_NAME of the anonymous type is set to the
24340 TYPE_DECL of the typedef. This construct is emitted by
24341 the C++ FE.
24343 TYPE is the anonymous struct named by the typedef
24344 DECL. As we need the DW_AT_type attribute of the
24345 DW_TAG_typedef to point to the DIE of TYPE, let's
24346 generate that DIE right away. add_type_attribute
24347 called below will then pick (via lookup_type_die) that
24348 anonymous struct DIE. */
24349 if (!TREE_ASM_WRITTEN (type))
24350 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24352 /* This is a GNU Extension. We are adding a
24353 DW_AT_linkage_name attribute to the DIE of the
24354 anonymous struct TYPE. The value of that attribute
24355 is the name of the typedef decl naming the anonymous
24356 struct. This greatly eases the work of consumers of
24357 this debug info. */
24358 add_linkage_name_raw (lookup_type_die (type), decl);
24362 add_type_attribute (type_die, type, decl_quals (decl), false,
24363 context_die);
24365 if (is_naming_typedef_decl (decl))
24366 /* We want that all subsequent calls to lookup_type_die with
24367 TYPE in argument yield the DW_TAG_typedef we have just
24368 created. */
24369 equate_type_number_to_die (type, type_die);
24371 type = TREE_TYPE (decl);
24373 add_alignment_attribute (type_die, type);
24375 add_accessibility_attribute (type_die, decl);
24378 if (DECL_ABSTRACT_P (decl))
24379 equate_decl_number_to_die (decl, type_die);
24381 if (get_AT (type_die, DW_AT_name))
24382 add_pubtype (decl, type_die);
24385 /* Generate a DIE for a struct, class, enum or union type. */
24387 static void
24388 gen_tagged_type_die (tree type,
24389 dw_die_ref context_die,
24390 enum debug_info_usage usage)
24392 int need_pop;
24394 if (type == NULL_TREE
24395 || !is_tagged_type (type))
24396 return;
24398 if (TREE_ASM_WRITTEN (type))
24399 need_pop = 0;
24400 /* If this is a nested type whose containing class hasn't been written
24401 out yet, writing it out will cover this one, too. This does not apply
24402 to instantiations of member class templates; they need to be added to
24403 the containing class as they are generated. FIXME: This hurts the
24404 idea of combining type decls from multiple TUs, since we can't predict
24405 what set of template instantiations we'll get. */
24406 else if (TYPE_CONTEXT (type)
24407 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24408 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24410 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24412 if (TREE_ASM_WRITTEN (type))
24413 return;
24415 /* If that failed, attach ourselves to the stub. */
24416 push_decl_scope (TYPE_CONTEXT (type));
24417 context_die = lookup_type_die (TYPE_CONTEXT (type));
24418 need_pop = 1;
24420 else if (TYPE_CONTEXT (type) != NULL_TREE
24421 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24423 /* If this type is local to a function that hasn't been written
24424 out yet, use a NULL context for now; it will be fixed up in
24425 decls_for_scope. */
24426 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24427 /* A declaration DIE doesn't count; nested types need to go in the
24428 specification. */
24429 if (context_die && is_declaration_die (context_die))
24430 context_die = NULL;
24431 need_pop = 0;
24433 else
24435 context_die = declare_in_namespace (type, context_die);
24436 need_pop = 0;
24439 if (TREE_CODE (type) == ENUMERAL_TYPE)
24441 /* This might have been written out by the call to
24442 declare_in_namespace. */
24443 if (!TREE_ASM_WRITTEN (type))
24444 gen_enumeration_type_die (type, context_die);
24446 else
24447 gen_struct_or_union_type_die (type, context_die, usage);
24449 if (need_pop)
24450 pop_decl_scope ();
24452 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24453 it up if it is ever completed. gen_*_type_die will set it for us
24454 when appropriate. */
24457 /* Generate a type description DIE. */
24459 static void
24460 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24461 enum debug_info_usage usage)
24463 struct array_descr_info info;
24465 if (type == NULL_TREE || type == error_mark_node)
24466 return;
24468 if (flag_checking && type)
24469 verify_type (type);
24471 if (TYPE_NAME (type) != NULL_TREE
24472 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24473 && is_redundant_typedef (TYPE_NAME (type))
24474 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24475 /* The DECL of this type is a typedef we don't want to emit debug
24476 info for but we want debug info for its underlying typedef.
24477 This can happen for e.g, the injected-class-name of a C++
24478 type. */
24479 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24481 /* If TYPE is a typedef type variant, let's generate debug info
24482 for the parent typedef which TYPE is a type of. */
24483 if (typedef_variant_p (type))
24485 if (TREE_ASM_WRITTEN (type))
24486 return;
24488 /* Prevent broken recursion; we can't hand off to the same type. */
24489 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24491 /* Give typedefs the right scope. */
24492 context_die = scope_die_for (type, context_die);
24494 TREE_ASM_WRITTEN (type) = 1;
24496 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24497 return;
24500 /* If type is an anonymous tagged type named by a typedef, let's
24501 generate debug info for the typedef. */
24502 if (is_naming_typedef_decl (TYPE_NAME (type)))
24504 /* Use the DIE of the containing namespace as the parent DIE of
24505 the type description DIE we want to generate. */
24506 if (DECL_CONTEXT (TYPE_NAME (type))
24507 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24508 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24510 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24511 return;
24514 if (lang_hooks.types.get_debug_type)
24516 tree debug_type = lang_hooks.types.get_debug_type (type);
24518 if (debug_type != NULL_TREE && debug_type != type)
24520 gen_type_die_with_usage (debug_type, context_die, usage);
24521 return;
24525 /* We are going to output a DIE to represent the unqualified version
24526 of this type (i.e. without any const or volatile qualifiers) so
24527 get the main variant (i.e. the unqualified version) of this type
24528 now. (Vectors and arrays are special because the debugging info is in the
24529 cloned type itself. Similarly function/method types can contain extra
24530 ref-qualification). */
24531 if (TREE_CODE (type) == FUNCTION_TYPE
24532 || TREE_CODE (type) == METHOD_TYPE)
24534 /* For function/method types, can't use type_main_variant here,
24535 because that can have different ref-qualifiers for C++,
24536 but try to canonicalize. */
24537 tree main = TYPE_MAIN_VARIANT (type);
24538 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24539 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24540 && check_base_type (t, main)
24541 && check_lang_type (t, type))
24543 type = t;
24544 break;
24547 else if (TREE_CODE (type) != VECTOR_TYPE
24548 && TREE_CODE (type) != ARRAY_TYPE)
24549 type = type_main_variant (type);
24551 /* If this is an array type with hidden descriptor, handle it first. */
24552 if (!TREE_ASM_WRITTEN (type)
24553 && lang_hooks.types.get_array_descr_info)
24555 memset (&info, 0, sizeof (info));
24556 if (lang_hooks.types.get_array_descr_info (type, &info))
24558 /* Fortran sometimes emits array types with no dimension. */
24559 gcc_assert (info.ndimensions >= 0
24560 && (info.ndimensions
24561 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24562 gen_descr_array_type_die (type, &info, context_die);
24563 TREE_ASM_WRITTEN (type) = 1;
24564 return;
24568 if (TREE_ASM_WRITTEN (type))
24570 /* Variable-length types may be incomplete even if
24571 TREE_ASM_WRITTEN. For such types, fall through to
24572 gen_array_type_die() and possibly fill in
24573 DW_AT_{upper,lower}_bound attributes. */
24574 if ((TREE_CODE (type) != ARRAY_TYPE
24575 && TREE_CODE (type) != RECORD_TYPE
24576 && TREE_CODE (type) != UNION_TYPE
24577 && TREE_CODE (type) != QUAL_UNION_TYPE)
24578 || !variably_modified_type_p (type, NULL))
24579 return;
24582 switch (TREE_CODE (type))
24584 case ERROR_MARK:
24585 break;
24587 case POINTER_TYPE:
24588 case REFERENCE_TYPE:
24589 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24590 ensures that the gen_type_die recursion will terminate even if the
24591 type is recursive. Recursive types are possible in Ada. */
24592 /* ??? We could perhaps do this for all types before the switch
24593 statement. */
24594 TREE_ASM_WRITTEN (type) = 1;
24596 /* For these types, all that is required is that we output a DIE (or a
24597 set of DIEs) to represent the "basis" type. */
24598 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24599 DINFO_USAGE_IND_USE);
24600 break;
24602 case OFFSET_TYPE:
24603 /* This code is used for C++ pointer-to-data-member types.
24604 Output a description of the relevant class type. */
24605 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24606 DINFO_USAGE_IND_USE);
24608 /* Output a description of the type of the object pointed to. */
24609 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24610 DINFO_USAGE_IND_USE);
24612 /* Now output a DIE to represent this pointer-to-data-member type
24613 itself. */
24614 gen_ptr_to_mbr_type_die (type, context_die);
24615 break;
24617 case FUNCTION_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 METHOD_TYPE:
24625 /* Force out return type (in case it wasn't forced out already). */
24626 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24627 DINFO_USAGE_DIR_USE);
24628 gen_subroutine_type_die (type, context_die);
24629 break;
24631 case ARRAY_TYPE:
24632 case VECTOR_TYPE:
24633 gen_array_type_die (type, context_die);
24634 break;
24636 case ENUMERAL_TYPE:
24637 case RECORD_TYPE:
24638 case UNION_TYPE:
24639 case QUAL_UNION_TYPE:
24640 gen_tagged_type_die (type, context_die, usage);
24641 return;
24643 case VOID_TYPE:
24644 case INTEGER_TYPE:
24645 case REAL_TYPE:
24646 case FIXED_POINT_TYPE:
24647 case COMPLEX_TYPE:
24648 case BOOLEAN_TYPE:
24649 case POINTER_BOUNDS_TYPE:
24650 /* No DIEs needed for fundamental types. */
24651 break;
24653 case NULLPTR_TYPE:
24654 case LANG_TYPE:
24655 /* Just use DW_TAG_unspecified_type. */
24657 dw_die_ref type_die = lookup_type_die (type);
24658 if (type_die == NULL)
24660 tree name = TYPE_IDENTIFIER (type);
24661 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24662 type);
24663 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24664 equate_type_number_to_die (type, type_die);
24667 break;
24669 default:
24670 if (is_cxx_auto (type))
24672 tree name = TYPE_IDENTIFIER (type);
24673 dw_die_ref *die = (name == get_identifier ("auto")
24674 ? &auto_die : &decltype_auto_die);
24675 if (!*die)
24677 *die = new_die (DW_TAG_unspecified_type,
24678 comp_unit_die (), NULL_TREE);
24679 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24681 equate_type_number_to_die (type, *die);
24682 break;
24684 gcc_unreachable ();
24687 TREE_ASM_WRITTEN (type) = 1;
24690 static void
24691 gen_type_die (tree type, dw_die_ref context_die)
24693 if (type != error_mark_node)
24695 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24696 if (flag_checking)
24698 dw_die_ref die = lookup_type_die (type);
24699 if (die)
24700 check_die (die);
24705 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24706 things which are local to the given block. */
24708 static void
24709 gen_block_die (tree stmt, dw_die_ref context_die)
24711 int must_output_die = 0;
24712 bool inlined_func;
24714 /* Ignore blocks that are NULL. */
24715 if (stmt == NULL_TREE)
24716 return;
24718 inlined_func = inlined_function_outer_scope_p (stmt);
24720 /* If the block is one fragment of a non-contiguous block, do not
24721 process the variables, since they will have been done by the
24722 origin block. Do process subblocks. */
24723 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24725 tree sub;
24727 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24728 gen_block_die (sub, context_die);
24730 return;
24733 /* Determine if we need to output any Dwarf DIEs at all to represent this
24734 block. */
24735 if (inlined_func)
24736 /* The outer scopes for inlinings *must* always be represented. We
24737 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24738 must_output_die = 1;
24739 else
24741 /* Determine if this block directly contains any "significant"
24742 local declarations which we will need to output DIEs for. */
24743 if (debug_info_level > DINFO_LEVEL_TERSE)
24744 /* We are not in terse mode so *any* local declaration counts
24745 as being a "significant" one. */
24746 must_output_die = ((BLOCK_VARS (stmt) != NULL
24747 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24748 && (TREE_USED (stmt)
24749 || TREE_ASM_WRITTEN (stmt)
24750 || BLOCK_ABSTRACT (stmt)));
24751 else if ((TREE_USED (stmt)
24752 || TREE_ASM_WRITTEN (stmt)
24753 || BLOCK_ABSTRACT (stmt))
24754 && !dwarf2out_ignore_block (stmt))
24755 must_output_die = 1;
24758 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24759 DIE for any block which contains no significant local declarations at
24760 all. Rather, in such cases we just call `decls_for_scope' so that any
24761 needed Dwarf info for any sub-blocks will get properly generated. Note
24762 that in terse mode, our definition of what constitutes a "significant"
24763 local declaration gets restricted to include only inlined function
24764 instances and local (nested) function definitions. */
24765 if (must_output_die)
24767 if (inlined_func)
24769 /* If STMT block is abstract, that means we have been called
24770 indirectly from dwarf2out_abstract_function.
24771 That function rightfully marks the descendent blocks (of
24772 the abstract function it is dealing with) as being abstract,
24773 precisely to prevent us from emitting any
24774 DW_TAG_inlined_subroutine DIE as a descendent
24775 of an abstract function instance. So in that case, we should
24776 not call gen_inlined_subroutine_die.
24778 Later though, when cgraph asks dwarf2out to emit info
24779 for the concrete instance of the function decl into which
24780 the concrete instance of STMT got inlined, the later will lead
24781 to the generation of a DW_TAG_inlined_subroutine DIE. */
24782 if (! BLOCK_ABSTRACT (stmt))
24783 gen_inlined_subroutine_die (stmt, context_die);
24785 else
24786 gen_lexical_block_die (stmt, context_die);
24788 else
24789 decls_for_scope (stmt, context_die);
24792 /* Process variable DECL (or variable with origin ORIGIN) within
24793 block STMT and add it to CONTEXT_DIE. */
24794 static void
24795 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24797 dw_die_ref die;
24798 tree decl_or_origin = decl ? decl : origin;
24800 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24801 die = lookup_decl_die (decl_or_origin);
24802 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24804 if (TYPE_DECL_IS_STUB (decl_or_origin))
24805 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24806 else
24807 die = lookup_decl_die (decl_or_origin);
24808 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24809 if (! die && ! early_dwarf)
24810 return;
24812 else
24813 die = NULL;
24815 if (die != NULL && die->die_parent == NULL)
24816 add_child_die (context_die, die);
24817 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24819 if (early_dwarf)
24820 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24821 stmt, context_die);
24823 else
24824 gen_decl_die (decl, origin, NULL, context_die);
24827 /* Generate all of the decls declared within a given scope and (recursively)
24828 all of its sub-blocks. */
24830 static void
24831 decls_for_scope (tree stmt, dw_die_ref context_die)
24833 tree decl;
24834 unsigned int i;
24835 tree subblocks;
24837 /* Ignore NULL blocks. */
24838 if (stmt == NULL_TREE)
24839 return;
24841 /* Output the DIEs to represent all of the data objects and typedefs
24842 declared directly within this block but not within any nested
24843 sub-blocks. Also, nested function and tag DIEs have been
24844 generated with a parent of NULL; fix that up now. We don't
24845 have to do this if we're at -g1. */
24846 if (debug_info_level > DINFO_LEVEL_TERSE)
24848 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24849 process_scope_var (stmt, decl, NULL_TREE, context_die);
24850 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24851 origin - avoid doing this twice as we have no good way to see
24852 if we've done it once already. */
24853 if (! early_dwarf)
24854 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24856 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24857 if (decl == current_function_decl)
24858 /* Ignore declarations of the current function, while they
24859 are declarations, gen_subprogram_die would treat them
24860 as definitions again, because they are equal to
24861 current_function_decl and endlessly recurse. */;
24862 else if (TREE_CODE (decl) == FUNCTION_DECL)
24863 process_scope_var (stmt, decl, NULL_TREE, context_die);
24864 else
24865 process_scope_var (stmt, NULL_TREE, decl, context_die);
24869 /* Even if we're at -g1, we need to process the subblocks in order to get
24870 inlined call information. */
24872 /* Output the DIEs to represent all sub-blocks (and the items declared
24873 therein) of this block. */
24874 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24875 subblocks != NULL;
24876 subblocks = BLOCK_CHAIN (subblocks))
24877 gen_block_die (subblocks, context_die);
24880 /* Is this a typedef we can avoid emitting? */
24882 bool
24883 is_redundant_typedef (const_tree decl)
24885 if (TYPE_DECL_IS_STUB (decl))
24886 return true;
24888 if (DECL_ARTIFICIAL (decl)
24889 && DECL_CONTEXT (decl)
24890 && is_tagged_type (DECL_CONTEXT (decl))
24891 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24892 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24893 /* Also ignore the artificial member typedef for the class name. */
24894 return true;
24896 return false;
24899 /* Return TRUE if TYPE is a typedef that names a type for linkage
24900 purposes. This kind of typedefs is produced by the C++ FE for
24901 constructs like:
24903 typedef struct {...} foo;
24905 In that case, there is no typedef variant type produced for foo.
24906 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24907 struct type. */
24909 static bool
24910 is_naming_typedef_decl (const_tree decl)
24912 if (decl == NULL_TREE
24913 || TREE_CODE (decl) != TYPE_DECL
24914 || DECL_NAMELESS (decl)
24915 || !is_tagged_type (TREE_TYPE (decl))
24916 || DECL_IS_BUILTIN (decl)
24917 || is_redundant_typedef (decl)
24918 /* It looks like Ada produces TYPE_DECLs that are very similar
24919 to C++ naming typedefs but that have different
24920 semantics. Let's be specific to c++ for now. */
24921 || !is_cxx (decl))
24922 return FALSE;
24924 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24925 && TYPE_NAME (TREE_TYPE (decl)) == decl
24926 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24927 != TYPE_NAME (TREE_TYPE (decl))));
24930 /* Looks up the DIE for a context. */
24932 static inline dw_die_ref
24933 lookup_context_die (tree context)
24935 if (context)
24937 /* Find die that represents this context. */
24938 if (TYPE_P (context))
24940 context = TYPE_MAIN_VARIANT (context);
24941 dw_die_ref ctx = lookup_type_die (context);
24942 if (!ctx)
24943 return NULL;
24944 return strip_naming_typedef (context, ctx);
24946 else
24947 return lookup_decl_die (context);
24949 return comp_unit_die ();
24952 /* Returns the DIE for a context. */
24954 static inline dw_die_ref
24955 get_context_die (tree context)
24957 if (context)
24959 /* Find die that represents this context. */
24960 if (TYPE_P (context))
24962 context = TYPE_MAIN_VARIANT (context);
24963 return strip_naming_typedef (context, force_type_die (context));
24965 else
24966 return force_decl_die (context);
24968 return comp_unit_die ();
24971 /* Returns the DIE for decl. A DIE will always be returned. */
24973 static dw_die_ref
24974 force_decl_die (tree decl)
24976 dw_die_ref decl_die;
24977 unsigned saved_external_flag;
24978 tree save_fn = NULL_TREE;
24979 decl_die = lookup_decl_die (decl);
24980 if (!decl_die)
24982 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24984 decl_die = lookup_decl_die (decl);
24985 if (decl_die)
24986 return decl_die;
24988 switch (TREE_CODE (decl))
24990 case FUNCTION_DECL:
24991 /* Clear current_function_decl, so that gen_subprogram_die thinks
24992 that this is a declaration. At this point, we just want to force
24993 declaration die. */
24994 save_fn = current_function_decl;
24995 current_function_decl = NULL_TREE;
24996 gen_subprogram_die (decl, context_die);
24997 current_function_decl = save_fn;
24998 break;
25000 case VAR_DECL:
25001 /* Set external flag to force declaration die. Restore it after
25002 gen_decl_die() call. */
25003 saved_external_flag = DECL_EXTERNAL (decl);
25004 DECL_EXTERNAL (decl) = 1;
25005 gen_decl_die (decl, NULL, NULL, context_die);
25006 DECL_EXTERNAL (decl) = saved_external_flag;
25007 break;
25009 case NAMESPACE_DECL:
25010 if (dwarf_version >= 3 || !dwarf_strict)
25011 dwarf2out_decl (decl);
25012 else
25013 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25014 decl_die = comp_unit_die ();
25015 break;
25017 case TRANSLATION_UNIT_DECL:
25018 decl_die = comp_unit_die ();
25019 break;
25021 default:
25022 gcc_unreachable ();
25025 /* We should be able to find the DIE now. */
25026 if (!decl_die)
25027 decl_die = lookup_decl_die (decl);
25028 gcc_assert (decl_die);
25031 return decl_die;
25034 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25035 always returned. */
25037 static dw_die_ref
25038 force_type_die (tree type)
25040 dw_die_ref type_die;
25042 type_die = lookup_type_die (type);
25043 if (!type_die)
25045 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25047 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25048 false, context_die);
25049 gcc_assert (type_die);
25051 return type_die;
25054 /* Force out any required namespaces to be able to output DECL,
25055 and return the new context_die for it, if it's changed. */
25057 static dw_die_ref
25058 setup_namespace_context (tree thing, dw_die_ref context_die)
25060 tree context = (DECL_P (thing)
25061 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25062 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25063 /* Force out the namespace. */
25064 context_die = force_decl_die (context);
25066 return context_die;
25069 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25070 type) within its namespace, if appropriate.
25072 For compatibility with older debuggers, namespace DIEs only contain
25073 declarations; all definitions are emitted at CU scope, with
25074 DW_AT_specification pointing to the declaration (like with class
25075 members). */
25077 static dw_die_ref
25078 declare_in_namespace (tree thing, dw_die_ref context_die)
25080 dw_die_ref ns_context;
25082 if (debug_info_level <= DINFO_LEVEL_TERSE)
25083 return context_die;
25085 /* External declarations in the local scope only need to be emitted
25086 once, not once in the namespace and once in the scope.
25088 This avoids declaring the `extern' below in the
25089 namespace DIE as well as in the innermost scope:
25091 namespace S
25093 int i=5;
25094 int foo()
25096 int i=8;
25097 extern int i;
25098 return i;
25102 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25103 return context_die;
25105 /* If this decl is from an inlined function, then don't try to emit it in its
25106 namespace, as we will get confused. It would have already been emitted
25107 when the abstract instance of the inline function was emitted anyways. */
25108 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25109 return context_die;
25111 ns_context = setup_namespace_context (thing, context_die);
25113 if (ns_context != context_die)
25115 if (is_fortran ())
25116 return ns_context;
25117 if (DECL_P (thing))
25118 gen_decl_die (thing, NULL, NULL, ns_context);
25119 else
25120 gen_type_die (thing, ns_context);
25122 return context_die;
25125 /* Generate a DIE for a namespace or namespace alias. */
25127 static void
25128 gen_namespace_die (tree decl, dw_die_ref context_die)
25130 dw_die_ref namespace_die;
25132 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25133 they are an alias of. */
25134 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25136 /* Output a real namespace or module. */
25137 context_die = setup_namespace_context (decl, comp_unit_die ());
25138 namespace_die = new_die (is_fortran ()
25139 ? DW_TAG_module : DW_TAG_namespace,
25140 context_die, decl);
25141 /* For Fortran modules defined in different CU don't add src coords. */
25142 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25144 const char *name = dwarf2_name (decl, 0);
25145 if (name)
25146 add_name_attribute (namespace_die, name);
25148 else
25149 add_name_and_src_coords_attributes (namespace_die, decl);
25150 if (DECL_EXTERNAL (decl))
25151 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25152 equate_decl_number_to_die (decl, namespace_die);
25154 else
25156 /* Output a namespace alias. */
25158 /* Force out the namespace we are an alias of, if necessary. */
25159 dw_die_ref origin_die
25160 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25162 if (DECL_FILE_SCOPE_P (decl)
25163 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25164 context_die = setup_namespace_context (decl, comp_unit_die ());
25165 /* Now create the namespace alias DIE. */
25166 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25167 add_name_and_src_coords_attributes (namespace_die, decl);
25168 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25169 equate_decl_number_to_die (decl, namespace_die);
25171 if ((dwarf_version >= 5 || !dwarf_strict)
25172 && lang_hooks.decls.decl_dwarf_attribute (decl,
25173 DW_AT_export_symbols) == 1)
25174 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25176 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25177 if (want_pubnames ())
25178 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25181 /* Generate Dwarf debug information for a decl described by DECL.
25182 The return value is currently only meaningful for PARM_DECLs,
25183 for all other decls it returns NULL.
25185 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25186 It can be NULL otherwise. */
25188 static dw_die_ref
25189 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25190 dw_die_ref context_die)
25192 tree decl_or_origin = decl ? decl : origin;
25193 tree class_origin = NULL, ultimate_origin;
25195 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25196 return NULL;
25198 /* Ignore pointer bounds decls. */
25199 if (DECL_P (decl_or_origin)
25200 && TREE_TYPE (decl_or_origin)
25201 && POINTER_BOUNDS_P (decl_or_origin))
25202 return NULL;
25204 switch (TREE_CODE (decl_or_origin))
25206 case ERROR_MARK:
25207 break;
25209 case CONST_DECL:
25210 if (!is_fortran () && !is_ada ())
25212 /* The individual enumerators of an enum type get output when we output
25213 the Dwarf representation of the relevant enum type itself. */
25214 break;
25217 /* Emit its type. */
25218 gen_type_die (TREE_TYPE (decl), context_die);
25220 /* And its containing namespace. */
25221 context_die = declare_in_namespace (decl, context_die);
25223 gen_const_die (decl, context_die);
25224 break;
25226 case FUNCTION_DECL:
25227 #if 0
25228 /* FIXME */
25229 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25230 on local redeclarations of global functions. That seems broken. */
25231 if (current_function_decl != decl)
25232 /* This is only a declaration. */;
25233 #endif
25235 /* If we're emitting a clone, emit info for the abstract instance. */
25236 if (origin || DECL_ORIGIN (decl) != decl)
25237 dwarf2out_abstract_function (origin
25238 ? DECL_ORIGIN (origin)
25239 : DECL_ABSTRACT_ORIGIN (decl));
25241 /* If we're emitting an out-of-line copy of an inline function,
25242 emit info for the abstract instance and set up to refer to it. */
25243 else if (cgraph_function_possibly_inlined_p (decl)
25244 && ! DECL_ABSTRACT_P (decl)
25245 && ! class_or_namespace_scope_p (context_die)
25246 /* dwarf2out_abstract_function won't emit a die if this is just
25247 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25248 that case, because that works only if we have a die. */
25249 && DECL_INITIAL (decl) != NULL_TREE)
25251 dwarf2out_abstract_function (decl);
25252 set_decl_origin_self (decl);
25255 /* Otherwise we're emitting the primary DIE for this decl. */
25256 else if (debug_info_level > DINFO_LEVEL_TERSE)
25258 /* Before we describe the FUNCTION_DECL itself, make sure that we
25259 have its containing type. */
25260 if (!origin)
25261 origin = decl_class_context (decl);
25262 if (origin != NULL_TREE)
25263 gen_type_die (origin, context_die);
25265 /* And its return type. */
25266 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25268 /* And its virtual context. */
25269 if (DECL_VINDEX (decl) != NULL_TREE)
25270 gen_type_die (DECL_CONTEXT (decl), context_die);
25272 /* Make sure we have a member DIE for decl. */
25273 if (origin != NULL_TREE)
25274 gen_type_die_for_member (origin, decl, context_die);
25276 /* And its containing namespace. */
25277 context_die = declare_in_namespace (decl, context_die);
25280 /* Now output a DIE to represent the function itself. */
25281 if (decl)
25282 gen_subprogram_die (decl, context_die);
25283 break;
25285 case TYPE_DECL:
25286 /* If we are in terse mode, don't generate any DIEs to represent any
25287 actual typedefs. */
25288 if (debug_info_level <= DINFO_LEVEL_TERSE)
25289 break;
25291 /* In the special case of a TYPE_DECL node representing the declaration
25292 of some type tag, if the given TYPE_DECL is marked as having been
25293 instantiated from some other (original) TYPE_DECL node (e.g. one which
25294 was generated within the original definition of an inline function) we
25295 used to generate a special (abbreviated) DW_TAG_structure_type,
25296 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25297 should be actually referencing those DIEs, as variable DIEs with that
25298 type would be emitted already in the abstract origin, so it was always
25299 removed during unused type prunning. Don't add anything in this
25300 case. */
25301 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25302 break;
25304 if (is_redundant_typedef (decl))
25305 gen_type_die (TREE_TYPE (decl), context_die);
25306 else
25307 /* Output a DIE to represent the typedef itself. */
25308 gen_typedef_die (decl, context_die);
25309 break;
25311 case LABEL_DECL:
25312 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25313 gen_label_die (decl, context_die);
25314 break;
25316 case VAR_DECL:
25317 case RESULT_DECL:
25318 /* If we are in terse mode, don't generate any DIEs to represent any
25319 variable declarations or definitions. */
25320 if (debug_info_level <= DINFO_LEVEL_TERSE)
25321 break;
25323 /* Output any DIEs that are needed to specify the type of this data
25324 object. */
25325 if (decl_by_reference_p (decl_or_origin))
25326 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25327 else
25328 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25330 /* And its containing type. */
25331 class_origin = decl_class_context (decl_or_origin);
25332 if (class_origin != NULL_TREE)
25333 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25335 /* And its containing namespace. */
25336 context_die = declare_in_namespace (decl_or_origin, context_die);
25338 /* Now output the DIE to represent the data object itself. This gets
25339 complicated because of the possibility that the VAR_DECL really
25340 represents an inlined instance of a formal parameter for an inline
25341 function. */
25342 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25343 if (ultimate_origin != NULL_TREE
25344 && TREE_CODE (ultimate_origin) == PARM_DECL)
25345 gen_formal_parameter_die (decl, origin,
25346 true /* Emit name attribute. */,
25347 context_die);
25348 else
25349 gen_variable_die (decl, origin, context_die);
25350 break;
25352 case FIELD_DECL:
25353 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25354 /* Ignore the nameless fields that are used to skip bits but handle C++
25355 anonymous unions and structs. */
25356 if (DECL_NAME (decl) != NULL_TREE
25357 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25358 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25360 gen_type_die (member_declared_type (decl), context_die);
25361 gen_field_die (decl, ctx, context_die);
25363 break;
25365 case PARM_DECL:
25366 if (DECL_BY_REFERENCE (decl_or_origin))
25367 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25368 else
25369 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25370 return gen_formal_parameter_die (decl, origin,
25371 true /* Emit name attribute. */,
25372 context_die);
25374 case NAMESPACE_DECL:
25375 if (dwarf_version >= 3 || !dwarf_strict)
25376 gen_namespace_die (decl, context_die);
25377 break;
25379 case IMPORTED_DECL:
25380 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25381 DECL_CONTEXT (decl), context_die);
25382 break;
25384 case NAMELIST_DECL:
25385 gen_namelist_decl (DECL_NAME (decl), context_die,
25386 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25387 break;
25389 default:
25390 /* Probably some frontend-internal decl. Assume we don't care. */
25391 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25392 break;
25395 return NULL;
25398 /* Output initial debug information for global DECL. Called at the
25399 end of the parsing process.
25401 This is the initial debug generation process. As such, the DIEs
25402 generated may be incomplete. A later debug generation pass
25403 (dwarf2out_late_global_decl) will augment the information generated
25404 in this pass (e.g., with complete location info). */
25406 static void
25407 dwarf2out_early_global_decl (tree decl)
25409 set_early_dwarf s;
25411 /* gen_decl_die() will set DECL_ABSTRACT because
25412 cgraph_function_possibly_inlined_p() returns true. This is in
25413 turn will cause DW_AT_inline attributes to be set.
25415 This happens because at early dwarf generation, there is no
25416 cgraph information, causing cgraph_function_possibly_inlined_p()
25417 to return true. Trick cgraph_function_possibly_inlined_p()
25418 while we generate dwarf early. */
25419 bool save = symtab->global_info_ready;
25420 symtab->global_info_ready = true;
25422 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25423 other DECLs and they can point to template types or other things
25424 that dwarf2out can't handle when done via dwarf2out_decl. */
25425 if (TREE_CODE (decl) != TYPE_DECL
25426 && TREE_CODE (decl) != PARM_DECL)
25428 tree save_fndecl = current_function_decl;
25429 if (TREE_CODE (decl) == FUNCTION_DECL)
25431 /* For nested functions, make sure we have DIEs for the parents first
25432 so that all nested DIEs are generated at the proper scope in the
25433 first shot. */
25434 tree context = decl_function_context (decl);
25435 if (context != NULL && lookup_decl_die (context) == NULL)
25437 current_function_decl = context;
25438 dwarf2out_decl (context);
25441 current_function_decl = decl;
25443 dwarf2out_decl (decl);
25444 if (TREE_CODE (decl) == FUNCTION_DECL)
25445 current_function_decl = save_fndecl;
25447 symtab->global_info_ready = save;
25450 /* Output debug information for global decl DECL. Called from
25451 toplev.c after compilation proper has finished. */
25453 static void
25454 dwarf2out_late_global_decl (tree decl)
25456 /* Fill-in any location information we were unable to determine
25457 on the first pass. */
25458 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25460 dw_die_ref die = lookup_decl_die (decl);
25462 /* We have to generate early debug late for LTO. */
25463 if (! die && in_lto_p)
25465 dwarf2out_decl (decl);
25466 die = lookup_decl_die (decl);
25469 if (die)
25471 /* We get called via the symtab code invoking late_global_decl
25472 for symbols that are optimized out. Do not add locations
25473 for those, except if they have a DECL_VALUE_EXPR, in which case
25474 they are relevant for debuggers. */
25475 varpool_node *node = varpool_node::get (decl);
25476 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25477 tree_add_const_value_attribute_for_decl (die, decl);
25478 else
25479 add_location_or_const_value_attribute (die, decl, false);
25484 /* Output debug information for type decl DECL. Called from toplev.c
25485 and from language front ends (to record built-in types). */
25486 static void
25487 dwarf2out_type_decl (tree decl, int local)
25489 if (!local)
25491 set_early_dwarf s;
25492 dwarf2out_decl (decl);
25496 /* Output debug information for imported module or decl DECL.
25497 NAME is non-NULL name in the lexical block if the decl has been renamed.
25498 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25499 that DECL belongs to.
25500 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25501 static void
25502 dwarf2out_imported_module_or_decl_1 (tree decl,
25503 tree name,
25504 tree lexical_block,
25505 dw_die_ref lexical_block_die)
25507 expanded_location xloc;
25508 dw_die_ref imported_die = NULL;
25509 dw_die_ref at_import_die;
25511 if (TREE_CODE (decl) == IMPORTED_DECL)
25513 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25514 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25515 gcc_assert (decl);
25517 else
25518 xloc = expand_location (input_location);
25520 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25522 at_import_die = force_type_die (TREE_TYPE (decl));
25523 /* For namespace N { typedef void T; } using N::T; base_type_die
25524 returns NULL, but DW_TAG_imported_declaration requires
25525 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25526 if (!at_import_die)
25528 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25529 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25530 at_import_die = lookup_type_die (TREE_TYPE (decl));
25531 gcc_assert (at_import_die);
25534 else
25536 at_import_die = lookup_decl_die (decl);
25537 if (!at_import_die)
25539 /* If we're trying to avoid duplicate debug info, we may not have
25540 emitted the member decl for this field. Emit it now. */
25541 if (TREE_CODE (decl) == FIELD_DECL)
25543 tree type = DECL_CONTEXT (decl);
25545 if (TYPE_CONTEXT (type)
25546 && TYPE_P (TYPE_CONTEXT (type))
25547 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25548 DINFO_USAGE_DIR_USE))
25549 return;
25550 gen_type_die_for_member (type, decl,
25551 get_context_die (TYPE_CONTEXT (type)));
25553 if (TREE_CODE (decl) == NAMELIST_DECL)
25554 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25555 get_context_die (DECL_CONTEXT (decl)),
25556 NULL_TREE);
25557 else
25558 at_import_die = force_decl_die (decl);
25562 if (TREE_CODE (decl) == NAMESPACE_DECL)
25564 if (dwarf_version >= 3 || !dwarf_strict)
25565 imported_die = new_die (DW_TAG_imported_module,
25566 lexical_block_die,
25567 lexical_block);
25568 else
25569 return;
25571 else
25572 imported_die = new_die (DW_TAG_imported_declaration,
25573 lexical_block_die,
25574 lexical_block);
25576 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25577 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25578 if (debug_column_info && xloc.column)
25579 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25580 if (name)
25581 add_AT_string (imported_die, DW_AT_name,
25582 IDENTIFIER_POINTER (name));
25583 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25586 /* Output debug information for imported module or decl DECL.
25587 NAME is non-NULL name in context if the decl has been renamed.
25588 CHILD is true if decl is one of the renamed decls as part of
25589 importing whole module.
25590 IMPLICIT is set if this hook is called for an implicit import
25591 such as inline namespace. */
25593 static void
25594 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25595 bool child, bool implicit)
25597 /* dw_die_ref at_import_die; */
25598 dw_die_ref scope_die;
25600 if (debug_info_level <= DINFO_LEVEL_TERSE)
25601 return;
25603 gcc_assert (decl);
25605 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25606 should be enough, for DWARF4 and older even if we emit as extension
25607 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25608 for the benefit of consumers unaware of DW_AT_export_symbols. */
25609 if (implicit
25610 && dwarf_version >= 5
25611 && lang_hooks.decls.decl_dwarf_attribute (decl,
25612 DW_AT_export_symbols) == 1)
25613 return;
25615 set_early_dwarf s;
25617 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25618 We need decl DIE for reference and scope die. First, get DIE for the decl
25619 itself. */
25621 /* Get the scope die for decl context. Use comp_unit_die for global module
25622 or decl. If die is not found for non globals, force new die. */
25623 if (context
25624 && TYPE_P (context)
25625 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25626 return;
25628 scope_die = get_context_die (context);
25630 if (child)
25632 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25633 there is nothing we can do, here. */
25634 if (dwarf_version < 3 && dwarf_strict)
25635 return;
25637 gcc_assert (scope_die->die_child);
25638 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25639 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25640 scope_die = scope_die->die_child;
25643 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25644 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25647 /* Output debug information for namelists. */
25649 static dw_die_ref
25650 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25652 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25653 tree value;
25654 unsigned i;
25656 if (debug_info_level <= DINFO_LEVEL_TERSE)
25657 return NULL;
25659 gcc_assert (scope_die != NULL);
25660 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25661 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25663 /* If there are no item_decls, we have a nondefining namelist, e.g.
25664 with USE association; hence, set DW_AT_declaration. */
25665 if (item_decls == NULL_TREE)
25667 add_AT_flag (nml_die, DW_AT_declaration, 1);
25668 return nml_die;
25671 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25673 nml_item_ref_die = lookup_decl_die (value);
25674 if (!nml_item_ref_die)
25675 nml_item_ref_die = force_decl_die (value);
25677 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25678 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25680 return nml_die;
25684 /* Write the debugging output for DECL and return the DIE. */
25686 static void
25687 dwarf2out_decl (tree decl)
25689 dw_die_ref context_die = comp_unit_die ();
25691 switch (TREE_CODE (decl))
25693 case ERROR_MARK:
25694 return;
25696 case FUNCTION_DECL:
25697 /* If we're a nested function, initially use a parent of NULL; if we're
25698 a plain function, this will be fixed up in decls_for_scope. If
25699 we're a method, it will be ignored, since we already have a DIE. */
25700 if (decl_function_context (decl)
25701 /* But if we're in terse mode, we don't care about scope. */
25702 && debug_info_level > DINFO_LEVEL_TERSE)
25703 context_die = NULL;
25704 break;
25706 case VAR_DECL:
25707 /* For local statics lookup proper context die. */
25708 if (local_function_static (decl))
25709 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25711 /* If we are in terse mode, don't generate any DIEs to represent any
25712 variable declarations or definitions. */
25713 if (debug_info_level <= DINFO_LEVEL_TERSE)
25714 return;
25715 break;
25717 case CONST_DECL:
25718 if (debug_info_level <= DINFO_LEVEL_TERSE)
25719 return;
25720 if (!is_fortran () && !is_ada ())
25721 return;
25722 if (TREE_STATIC (decl) && decl_function_context (decl))
25723 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25724 break;
25726 case NAMESPACE_DECL:
25727 case IMPORTED_DECL:
25728 if (debug_info_level <= DINFO_LEVEL_TERSE)
25729 return;
25730 if (lookup_decl_die (decl) != NULL)
25731 return;
25732 break;
25734 case TYPE_DECL:
25735 /* Don't emit stubs for types unless they are needed by other DIEs. */
25736 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25737 return;
25739 /* Don't bother trying to generate any DIEs to represent any of the
25740 normal built-in types for the language we are compiling. */
25741 if (DECL_IS_BUILTIN (decl))
25742 return;
25744 /* If we are in terse mode, don't generate any DIEs for types. */
25745 if (debug_info_level <= DINFO_LEVEL_TERSE)
25746 return;
25748 /* If we're a function-scope tag, initially use a parent of NULL;
25749 this will be fixed up in decls_for_scope. */
25750 if (decl_function_context (decl))
25751 context_die = NULL;
25753 break;
25755 case NAMELIST_DECL:
25756 break;
25758 default:
25759 return;
25762 gen_decl_die (decl, NULL, NULL, context_die);
25764 if (flag_checking)
25766 dw_die_ref die = lookup_decl_die (decl);
25767 if (die)
25768 check_die (die);
25772 /* Write the debugging output for DECL. */
25774 static void
25775 dwarf2out_function_decl (tree decl)
25777 dwarf2out_decl (decl);
25778 call_arg_locations = NULL;
25779 call_arg_loc_last = NULL;
25780 call_site_count = -1;
25781 tail_call_site_count = -1;
25782 decl_loc_table->empty ();
25783 cached_dw_loc_list_table->empty ();
25786 /* Output a marker (i.e. a label) for the beginning of the generated code for
25787 a lexical block. */
25789 static void
25790 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25791 unsigned int blocknum)
25793 switch_to_section (current_function_section ());
25794 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25797 /* Output a marker (i.e. a label) for the end of the generated code for a
25798 lexical block. */
25800 static void
25801 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25803 switch_to_section (current_function_section ());
25804 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25807 /* Returns nonzero if it is appropriate not to emit any debugging
25808 information for BLOCK, because it doesn't contain any instructions.
25810 Don't allow this for blocks with nested functions or local classes
25811 as we would end up with orphans, and in the presence of scheduling
25812 we may end up calling them anyway. */
25814 static bool
25815 dwarf2out_ignore_block (const_tree block)
25817 tree decl;
25818 unsigned int i;
25820 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25821 if (TREE_CODE (decl) == FUNCTION_DECL
25822 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25823 return 0;
25824 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25826 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25827 if (TREE_CODE (decl) == FUNCTION_DECL
25828 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25829 return 0;
25832 return 1;
25835 /* Hash table routines for file_hash. */
25837 bool
25838 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25840 return filename_cmp (p1->filename, p2) == 0;
25843 hashval_t
25844 dwarf_file_hasher::hash (dwarf_file_data *p)
25846 return htab_hash_string (p->filename);
25849 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25850 dwarf2out.c) and return its "index". The index of each (known) filename is
25851 just a unique number which is associated with only that one filename. We
25852 need such numbers for the sake of generating labels (in the .debug_sfnames
25853 section) and references to those files numbers (in the .debug_srcinfo
25854 and .debug_macinfo sections). If the filename given as an argument is not
25855 found in our current list, add it to the list and assign it the next
25856 available unique index number. */
25858 static struct dwarf_file_data *
25859 lookup_filename (const char *file_name)
25861 struct dwarf_file_data * created;
25863 if (!file_name)
25864 return NULL;
25866 dwarf_file_data **slot
25867 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25868 INSERT);
25869 if (*slot)
25870 return *slot;
25872 created = ggc_alloc<dwarf_file_data> ();
25873 created->filename = file_name;
25874 created->emitted_number = 0;
25875 *slot = created;
25876 return created;
25879 /* If the assembler will construct the file table, then translate the compiler
25880 internal file table number into the assembler file table number, and emit
25881 a .file directive if we haven't already emitted one yet. The file table
25882 numbers are different because we prune debug info for unused variables and
25883 types, which may include filenames. */
25885 static int
25886 maybe_emit_file (struct dwarf_file_data * fd)
25888 if (! fd->emitted_number)
25890 if (last_emitted_file)
25891 fd->emitted_number = last_emitted_file->emitted_number + 1;
25892 else
25893 fd->emitted_number = 1;
25894 last_emitted_file = fd;
25896 if (DWARF2_ASM_LINE_DEBUG_INFO)
25898 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25899 output_quoted_string (asm_out_file,
25900 remap_debug_filename (fd->filename));
25901 fputc ('\n', asm_out_file);
25905 return fd->emitted_number;
25908 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25909 That generation should happen after function debug info has been
25910 generated. The value of the attribute is the constant value of ARG. */
25912 static void
25913 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25915 die_arg_entry entry;
25917 if (!die || !arg)
25918 return;
25920 gcc_assert (early_dwarf);
25922 if (!tmpl_value_parm_die_table)
25923 vec_alloc (tmpl_value_parm_die_table, 32);
25925 entry.die = die;
25926 entry.arg = arg;
25927 vec_safe_push (tmpl_value_parm_die_table, entry);
25930 /* Return TRUE if T is an instance of generic type, FALSE
25931 otherwise. */
25933 static bool
25934 generic_type_p (tree t)
25936 if (t == NULL_TREE || !TYPE_P (t))
25937 return false;
25938 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25941 /* Schedule the generation of the generic parameter dies for the
25942 instance of generic type T. The proper generation itself is later
25943 done by gen_scheduled_generic_parms_dies. */
25945 static void
25946 schedule_generic_params_dies_gen (tree t)
25948 if (!generic_type_p (t))
25949 return;
25951 gcc_assert (early_dwarf);
25953 if (!generic_type_instances)
25954 vec_alloc (generic_type_instances, 256);
25956 vec_safe_push (generic_type_instances, t);
25959 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25960 by append_entry_to_tmpl_value_parm_die_table. This function must
25961 be called after function DIEs have been generated. */
25963 static void
25964 gen_remaining_tmpl_value_param_die_attribute (void)
25966 if (tmpl_value_parm_die_table)
25968 unsigned i, j;
25969 die_arg_entry *e;
25971 /* We do this in two phases - first get the cases we can
25972 handle during early-finish, preserving those we cannot
25973 (containing symbolic constants where we don't yet know
25974 whether we are going to output the referenced symbols).
25975 For those we try again at late-finish. */
25976 j = 0;
25977 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25979 if (!tree_add_const_value_attribute (e->die, e->arg))
25981 dw_loc_descr_ref loc = NULL;
25982 if (! early_dwarf
25983 && (dwarf_version >= 5 || !dwarf_strict))
25984 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25985 if (loc)
25986 add_AT_loc (e->die, DW_AT_location, loc);
25987 else
25988 (*tmpl_value_parm_die_table)[j++] = *e;
25991 tmpl_value_parm_die_table->truncate (j);
25995 /* Generate generic parameters DIEs for instances of generic types
25996 that have been previously scheduled by
25997 schedule_generic_params_dies_gen. This function must be called
25998 after all the types of the CU have been laid out. */
26000 static void
26001 gen_scheduled_generic_parms_dies (void)
26003 unsigned i;
26004 tree t;
26006 if (!generic_type_instances)
26007 return;
26009 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26010 if (COMPLETE_TYPE_P (t))
26011 gen_generic_params_dies (t);
26013 generic_type_instances = NULL;
26017 /* Replace DW_AT_name for the decl with name. */
26019 static void
26020 dwarf2out_set_name (tree decl, tree name)
26022 dw_die_ref die;
26023 dw_attr_node *attr;
26024 const char *dname;
26026 die = TYPE_SYMTAB_DIE (decl);
26027 if (!die)
26028 return;
26030 dname = dwarf2_name (name, 0);
26031 if (!dname)
26032 return;
26034 attr = get_AT (die, DW_AT_name);
26035 if (attr)
26037 struct indirect_string_node *node;
26039 node = find_AT_string (dname);
26040 /* replace the string. */
26041 attr->dw_attr_val.v.val_str = node;
26044 else
26045 add_name_attribute (die, dname);
26048 /* True if before or during processing of the first function being emitted. */
26049 static bool in_first_function_p = true;
26050 /* True if loc_note during dwarf2out_var_location call might still be
26051 before first real instruction at address equal to .Ltext0. */
26052 static bool maybe_at_text_label_p = true;
26053 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26054 static unsigned int first_loclabel_num_not_at_text_label;
26056 /* Called by the final INSN scan whenever we see a var location. We
26057 use it to drop labels in the right places, and throw the location in
26058 our lookup table. */
26060 static void
26061 dwarf2out_var_location (rtx_insn *loc_note)
26063 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26064 struct var_loc_node *newloc;
26065 rtx_insn *next_real, *next_note;
26066 rtx_insn *call_insn = NULL;
26067 static const char *last_label;
26068 static const char *last_postcall_label;
26069 static bool last_in_cold_section_p;
26070 static rtx_insn *expected_next_loc_note;
26071 tree decl;
26072 bool var_loc_p;
26074 if (!NOTE_P (loc_note))
26076 if (CALL_P (loc_note))
26078 call_site_count++;
26079 if (SIBLING_CALL_P (loc_note))
26080 tail_call_site_count++;
26081 if (optimize == 0 && !flag_var_tracking)
26083 /* When the var-tracking pass is not running, there is no note
26084 for indirect calls whose target is compile-time known. In this
26085 case, process such calls specifically so that we generate call
26086 sites for them anyway. */
26087 rtx x = PATTERN (loc_note);
26088 if (GET_CODE (x) == PARALLEL)
26089 x = XVECEXP (x, 0, 0);
26090 if (GET_CODE (x) == SET)
26091 x = SET_SRC (x);
26092 if (GET_CODE (x) == CALL)
26093 x = XEXP (x, 0);
26094 if (!MEM_P (x)
26095 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26096 || !SYMBOL_REF_DECL (XEXP (x, 0))
26097 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26098 != FUNCTION_DECL))
26100 call_insn = loc_note;
26101 loc_note = NULL;
26102 var_loc_p = false;
26104 next_real = next_real_insn (call_insn);
26105 next_note = NULL;
26106 cached_next_real_insn = NULL;
26107 goto create_label;
26111 return;
26114 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26115 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26116 return;
26118 /* Optimize processing a large consecutive sequence of location
26119 notes so we don't spend too much time in next_real_insn. If the
26120 next insn is another location note, remember the next_real_insn
26121 calculation for next time. */
26122 next_real = cached_next_real_insn;
26123 if (next_real)
26125 if (expected_next_loc_note != loc_note)
26126 next_real = NULL;
26129 next_note = NEXT_INSN (loc_note);
26130 if (! next_note
26131 || next_note->deleted ()
26132 || ! NOTE_P (next_note)
26133 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26134 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26135 next_note = NULL;
26137 if (! next_real)
26138 next_real = next_real_insn (loc_note);
26140 if (next_note)
26142 expected_next_loc_note = next_note;
26143 cached_next_real_insn = next_real;
26145 else
26146 cached_next_real_insn = NULL;
26148 /* If there are no instructions which would be affected by this note,
26149 don't do anything. */
26150 if (var_loc_p
26151 && next_real == NULL_RTX
26152 && !NOTE_DURING_CALL_P (loc_note))
26153 return;
26155 create_label:
26157 if (next_real == NULL_RTX)
26158 next_real = get_last_insn ();
26160 /* If there were any real insns between note we processed last time
26161 and this note (or if it is the first note), clear
26162 last_{,postcall_}label so that they are not reused this time. */
26163 if (last_var_location_insn == NULL_RTX
26164 || last_var_location_insn != next_real
26165 || last_in_cold_section_p != in_cold_section_p)
26167 last_label = NULL;
26168 last_postcall_label = NULL;
26171 if (var_loc_p)
26173 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26174 newloc = add_var_loc_to_decl (decl, loc_note,
26175 NOTE_DURING_CALL_P (loc_note)
26176 ? last_postcall_label : last_label);
26177 if (newloc == NULL)
26178 return;
26180 else
26182 decl = NULL_TREE;
26183 newloc = NULL;
26186 /* If there were no real insns between note we processed last time
26187 and this note, use the label we emitted last time. Otherwise
26188 create a new label and emit it. */
26189 if (last_label == NULL)
26191 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26192 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26193 loclabel_num++;
26194 last_label = ggc_strdup (loclabel);
26195 /* See if loclabel might be equal to .Ltext0. If yes,
26196 bump first_loclabel_num_not_at_text_label. */
26197 if (!have_multiple_function_sections
26198 && in_first_function_p
26199 && maybe_at_text_label_p)
26201 static rtx_insn *last_start;
26202 rtx_insn *insn;
26203 for (insn = loc_note; insn; insn = previous_insn (insn))
26204 if (insn == last_start)
26205 break;
26206 else if (!NONDEBUG_INSN_P (insn))
26207 continue;
26208 else
26210 rtx body = PATTERN (insn);
26211 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26212 continue;
26213 /* Inline asm could occupy zero bytes. */
26214 else if (GET_CODE (body) == ASM_INPUT
26215 || asm_noperands (body) >= 0)
26216 continue;
26217 #ifdef HAVE_attr_length
26218 else if (get_attr_min_length (insn) == 0)
26219 continue;
26220 #endif
26221 else
26223 /* Assume insn has non-zero length. */
26224 maybe_at_text_label_p = false;
26225 break;
26228 if (maybe_at_text_label_p)
26230 last_start = loc_note;
26231 first_loclabel_num_not_at_text_label = loclabel_num;
26236 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26237 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26239 if (!var_loc_p)
26241 struct call_arg_loc_node *ca_loc
26242 = ggc_cleared_alloc<call_arg_loc_node> ();
26243 rtx_insn *prev
26244 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26246 ca_loc->call_arg_loc_note = loc_note;
26247 ca_loc->next = NULL;
26248 ca_loc->label = last_label;
26249 gcc_assert (prev
26250 && (CALL_P (prev)
26251 || (NONJUMP_INSN_P (prev)
26252 && GET_CODE (PATTERN (prev)) == SEQUENCE
26253 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26254 if (!CALL_P (prev))
26255 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26256 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26258 /* Look for a SYMBOL_REF in the "prev" instruction. */
26259 rtx x = get_call_rtx_from (PATTERN (prev));
26260 if (x)
26262 /* Try to get the call symbol, if any. */
26263 if (MEM_P (XEXP (x, 0)))
26264 x = XEXP (x, 0);
26265 /* First, look for a memory access to a symbol_ref. */
26266 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26267 && SYMBOL_REF_DECL (XEXP (x, 0))
26268 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26269 ca_loc->symbol_ref = XEXP (x, 0);
26270 /* Otherwise, look at a compile-time known user-level function
26271 declaration. */
26272 else if (MEM_P (x)
26273 && MEM_EXPR (x)
26274 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26275 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26278 ca_loc->block = insn_scope (prev);
26279 if (call_arg_locations)
26280 call_arg_loc_last->next = ca_loc;
26281 else
26282 call_arg_locations = ca_loc;
26283 call_arg_loc_last = ca_loc;
26285 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26286 newloc->label = last_label;
26287 else
26289 if (!last_postcall_label)
26291 sprintf (loclabel, "%s-1", last_label);
26292 last_postcall_label = ggc_strdup (loclabel);
26294 newloc->label = last_postcall_label;
26297 last_var_location_insn = next_real;
26298 last_in_cold_section_p = in_cold_section_p;
26301 /* Called from finalize_size_functions for size functions so that their body
26302 can be encoded in the debug info to describe the layout of variable-length
26303 structures. */
26305 static void
26306 dwarf2out_size_function (tree decl)
26308 function_to_dwarf_procedure (decl);
26311 /* Note in one location list that text section has changed. */
26314 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26316 var_loc_list *list = *slot;
26317 if (list->first)
26318 list->last_before_switch
26319 = list->last->next ? list->last->next : list->last;
26320 return 1;
26323 /* Note in all location lists that text section has changed. */
26325 static void
26326 var_location_switch_text_section (void)
26328 if (decl_loc_table == NULL)
26329 return;
26331 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26334 /* Create a new line number table. */
26336 static dw_line_info_table *
26337 new_line_info_table (void)
26339 dw_line_info_table *table;
26341 table = ggc_cleared_alloc<dw_line_info_table> ();
26342 table->file_num = 1;
26343 table->line_num = 1;
26344 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26346 return table;
26349 /* Lookup the "current" table into which we emit line info, so
26350 that we don't have to do it for every source line. */
26352 static void
26353 set_cur_line_info_table (section *sec)
26355 dw_line_info_table *table;
26357 if (sec == text_section)
26358 table = text_section_line_info;
26359 else if (sec == cold_text_section)
26361 table = cold_text_section_line_info;
26362 if (!table)
26364 cold_text_section_line_info = table = new_line_info_table ();
26365 table->end_label = cold_end_label;
26368 else
26370 const char *end_label;
26372 if (crtl->has_bb_partition)
26374 if (in_cold_section_p)
26375 end_label = crtl->subsections.cold_section_end_label;
26376 else
26377 end_label = crtl->subsections.hot_section_end_label;
26379 else
26381 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26382 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26383 current_function_funcdef_no);
26384 end_label = ggc_strdup (label);
26387 table = new_line_info_table ();
26388 table->end_label = end_label;
26390 vec_safe_push (separate_line_info, table);
26393 if (DWARF2_ASM_LINE_DEBUG_INFO)
26394 table->is_stmt = (cur_line_info_table
26395 ? cur_line_info_table->is_stmt
26396 : DWARF_LINE_DEFAULT_IS_STMT_START);
26397 cur_line_info_table = table;
26401 /* We need to reset the locations at the beginning of each
26402 function. We can't do this in the end_function hook, because the
26403 declarations that use the locations won't have been output when
26404 that hook is called. Also compute have_multiple_function_sections here. */
26406 static void
26407 dwarf2out_begin_function (tree fun)
26409 section *sec = function_section (fun);
26411 if (sec != text_section)
26412 have_multiple_function_sections = true;
26414 if (crtl->has_bb_partition && !cold_text_section)
26416 gcc_assert (current_function_decl == fun);
26417 cold_text_section = unlikely_text_section ();
26418 switch_to_section (cold_text_section);
26419 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26420 switch_to_section (sec);
26423 dwarf2out_note_section_used ();
26424 call_site_count = 0;
26425 tail_call_site_count = 0;
26427 set_cur_line_info_table (sec);
26430 /* Helper function of dwarf2out_end_function, called only after emitting
26431 the very first function into assembly. Check if some .debug_loc range
26432 might end with a .LVL* label that could be equal to .Ltext0.
26433 In that case we must force using absolute addresses in .debug_loc ranges,
26434 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26435 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26436 list terminator.
26437 Set have_multiple_function_sections to true in that case and
26438 terminate htab traversal. */
26441 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26443 var_loc_list *entry = *slot;
26444 struct var_loc_node *node;
26446 node = entry->first;
26447 if (node && node->next && node->next->label)
26449 unsigned int i;
26450 const char *label = node->next->label;
26451 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26453 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26455 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26456 if (strcmp (label, loclabel) == 0)
26458 have_multiple_function_sections = true;
26459 return 0;
26463 return 1;
26466 /* Hook called after emitting a function into assembly.
26467 This does something only for the very first function emitted. */
26469 static void
26470 dwarf2out_end_function (unsigned int)
26472 if (in_first_function_p
26473 && !have_multiple_function_sections
26474 && first_loclabel_num_not_at_text_label
26475 && decl_loc_table)
26476 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26477 in_first_function_p = false;
26478 maybe_at_text_label_p = false;
26481 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26482 front-ends register a translation unit even before dwarf2out_init is
26483 called. */
26484 static tree main_translation_unit = NULL_TREE;
26486 /* Hook called by front-ends after they built their main translation unit.
26487 Associate comp_unit_die to UNIT. */
26489 static void
26490 dwarf2out_register_main_translation_unit (tree unit)
26492 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26493 && main_translation_unit == NULL_TREE);
26494 main_translation_unit = unit;
26495 /* If dwarf2out_init has not been called yet, it will perform the association
26496 itself looking at main_translation_unit. */
26497 if (decl_die_table != NULL)
26498 equate_decl_number_to_die (unit, comp_unit_die ());
26501 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26503 static void
26504 push_dw_line_info_entry (dw_line_info_table *table,
26505 enum dw_line_info_opcode opcode, unsigned int val)
26507 dw_line_info_entry e;
26508 e.opcode = opcode;
26509 e.val = val;
26510 vec_safe_push (table->entries, e);
26513 /* Output a label to mark the beginning of a source code line entry
26514 and record information relating to this source line, in
26515 'line_info_table' for later output of the .debug_line section. */
26516 /* ??? The discriminator parameter ought to be unsigned. */
26518 static void
26519 dwarf2out_source_line (unsigned int line, unsigned int column,
26520 const char *filename,
26521 int discriminator, bool is_stmt)
26523 unsigned int file_num;
26524 dw_line_info_table *table;
26526 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26527 return;
26529 /* The discriminator column was added in dwarf4. Simplify the below
26530 by simply removing it if we're not supposed to output it. */
26531 if (dwarf_version < 4 && dwarf_strict)
26532 discriminator = 0;
26534 if (!debug_column_info)
26535 column = 0;
26537 table = cur_line_info_table;
26538 file_num = maybe_emit_file (lookup_filename (filename));
26540 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26541 the debugger has used the second (possibly duplicate) line number
26542 at the beginning of the function to mark the end of the prologue.
26543 We could eliminate any other duplicates within the function. For
26544 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26545 that second line number entry. */
26546 /* Recall that this end-of-prologue indication is *not* the same thing
26547 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26548 to which the hook corresponds, follows the last insn that was
26549 emitted by gen_prologue. What we need is to precede the first insn
26550 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26551 insn that corresponds to something the user wrote. These may be
26552 very different locations once scheduling is enabled. */
26554 if (0 && file_num == table->file_num
26555 && line == table->line_num
26556 && column == table->column_num
26557 && discriminator == table->discrim_num
26558 && is_stmt == table->is_stmt)
26559 return;
26561 switch_to_section (current_function_section ());
26563 /* If requested, emit something human-readable. */
26564 if (flag_debug_asm)
26566 if (debug_column_info)
26567 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26568 filename, line, column);
26569 else
26570 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26571 filename, line);
26574 if (DWARF2_ASM_LINE_DEBUG_INFO)
26576 /* Emit the .loc directive understood by GNU as. */
26577 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26578 file_num, line, is_stmt, discriminator */
26579 fputs ("\t.loc ", asm_out_file);
26580 fprint_ul (asm_out_file, file_num);
26581 putc (' ', asm_out_file);
26582 fprint_ul (asm_out_file, line);
26583 putc (' ', asm_out_file);
26584 if (debug_column_info)
26585 fprint_ul (asm_out_file, column);
26586 else
26587 putc ('0', asm_out_file);
26589 if (is_stmt != table->is_stmt)
26591 fputs (" is_stmt ", asm_out_file);
26592 putc (is_stmt ? '1' : '0', asm_out_file);
26594 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26596 gcc_assert (discriminator > 0);
26597 fputs (" discriminator ", asm_out_file);
26598 fprint_ul (asm_out_file, (unsigned long) discriminator);
26600 putc ('\n', asm_out_file);
26602 else
26604 unsigned int label_num = ++line_info_label_num;
26606 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26608 push_dw_line_info_entry (table, LI_set_address, label_num);
26609 if (file_num != table->file_num)
26610 push_dw_line_info_entry (table, LI_set_file, file_num);
26611 if (discriminator != table->discrim_num)
26612 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26613 if (is_stmt != table->is_stmt)
26614 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26615 push_dw_line_info_entry (table, LI_set_line, line);
26616 if (debug_column_info)
26617 push_dw_line_info_entry (table, LI_set_column, column);
26620 table->file_num = file_num;
26621 table->line_num = line;
26622 table->column_num = column;
26623 table->discrim_num = discriminator;
26624 table->is_stmt = is_stmt;
26625 table->in_use = true;
26628 /* Record the beginning of a new source file. */
26630 static void
26631 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26633 if (flag_eliminate_dwarf2_dups)
26635 /* Record the beginning of the file for break_out_includes. */
26636 dw_die_ref bincl_die;
26638 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26639 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26642 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26644 macinfo_entry e;
26645 e.code = DW_MACINFO_start_file;
26646 e.lineno = lineno;
26647 e.info = ggc_strdup (filename);
26648 vec_safe_push (macinfo_table, e);
26652 /* Record the end of a source file. */
26654 static void
26655 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26657 if (flag_eliminate_dwarf2_dups)
26658 /* Record the end of the file for break_out_includes. */
26659 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26661 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26663 macinfo_entry e;
26664 e.code = DW_MACINFO_end_file;
26665 e.lineno = lineno;
26666 e.info = NULL;
26667 vec_safe_push (macinfo_table, e);
26671 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26672 the tail part of the directive line, i.e. the part which is past the
26673 initial whitespace, #, whitespace, directive-name, whitespace part. */
26675 static void
26676 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26677 const char *buffer ATTRIBUTE_UNUSED)
26679 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26681 macinfo_entry e;
26682 /* Insert a dummy first entry to be able to optimize the whole
26683 predefined macro block using DW_MACRO_import. */
26684 if (macinfo_table->is_empty () && lineno <= 1)
26686 e.code = 0;
26687 e.lineno = 0;
26688 e.info = NULL;
26689 vec_safe_push (macinfo_table, e);
26691 e.code = DW_MACINFO_define;
26692 e.lineno = lineno;
26693 e.info = ggc_strdup (buffer);
26694 vec_safe_push (macinfo_table, e);
26698 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26699 the tail part of the directive line, i.e. the part which is past the
26700 initial whitespace, #, whitespace, directive-name, whitespace part. */
26702 static void
26703 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26704 const char *buffer ATTRIBUTE_UNUSED)
26706 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26708 macinfo_entry e;
26709 /* Insert a dummy first entry to be able to optimize the whole
26710 predefined macro block using DW_MACRO_import. */
26711 if (macinfo_table->is_empty () && lineno <= 1)
26713 e.code = 0;
26714 e.lineno = 0;
26715 e.info = NULL;
26716 vec_safe_push (macinfo_table, e);
26718 e.code = DW_MACINFO_undef;
26719 e.lineno = lineno;
26720 e.info = ggc_strdup (buffer);
26721 vec_safe_push (macinfo_table, e);
26725 /* Helpers to manipulate hash table of CUs. */
26727 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26729 static inline hashval_t hash (const macinfo_entry *);
26730 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26733 inline hashval_t
26734 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26736 return htab_hash_string (entry->info);
26739 inline bool
26740 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26741 const macinfo_entry *entry2)
26743 return !strcmp (entry1->info, entry2->info);
26746 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26748 /* Output a single .debug_macinfo entry. */
26750 static void
26751 output_macinfo_op (macinfo_entry *ref)
26753 int file_num;
26754 size_t len;
26755 struct indirect_string_node *node;
26756 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26757 struct dwarf_file_data *fd;
26759 switch (ref->code)
26761 case DW_MACINFO_start_file:
26762 fd = lookup_filename (ref->info);
26763 file_num = maybe_emit_file (fd);
26764 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26765 dw2_asm_output_data_uleb128 (ref->lineno,
26766 "Included from line number %lu",
26767 (unsigned long) ref->lineno);
26768 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26769 break;
26770 case DW_MACINFO_end_file:
26771 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26772 break;
26773 case DW_MACINFO_define:
26774 case DW_MACINFO_undef:
26775 len = strlen (ref->info) + 1;
26776 if (!dwarf_strict
26777 && len > DWARF_OFFSET_SIZE
26778 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26779 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26781 ref->code = ref->code == DW_MACINFO_define
26782 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26783 output_macinfo_op (ref);
26784 return;
26786 dw2_asm_output_data (1, ref->code,
26787 ref->code == DW_MACINFO_define
26788 ? "Define macro" : "Undefine macro");
26789 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26790 (unsigned long) ref->lineno);
26791 dw2_asm_output_nstring (ref->info, -1, "The macro");
26792 break;
26793 case DW_MACRO_define_strp:
26794 case DW_MACRO_undef_strp:
26795 node = find_AT_string (ref->info);
26796 gcc_assert (node
26797 && (node->form == DW_FORM_strp
26798 || node->form == DW_FORM_GNU_str_index));
26799 dw2_asm_output_data (1, ref->code,
26800 ref->code == DW_MACRO_define_strp
26801 ? "Define macro strp"
26802 : "Undefine macro strp");
26803 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26804 (unsigned long) ref->lineno);
26805 if (node->form == DW_FORM_strp)
26806 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26807 debug_str_section, "The macro: \"%s\"",
26808 ref->info);
26809 else
26810 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26811 ref->info);
26812 break;
26813 case DW_MACRO_import:
26814 dw2_asm_output_data (1, ref->code, "Import");
26815 ASM_GENERATE_INTERNAL_LABEL (label,
26816 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26817 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26818 break;
26819 default:
26820 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26821 ASM_COMMENT_START, (unsigned long) ref->code);
26822 break;
26826 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26827 other compilation unit .debug_macinfo sections. IDX is the first
26828 index of a define/undef, return the number of ops that should be
26829 emitted in a comdat .debug_macinfo section and emit
26830 a DW_MACRO_import entry referencing it.
26831 If the define/undef entry should be emitted normally, return 0. */
26833 static unsigned
26834 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26835 macinfo_hash_type **macinfo_htab)
26837 macinfo_entry *first, *second, *cur, *inc;
26838 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26839 unsigned char checksum[16];
26840 struct md5_ctx ctx;
26841 char *grp_name, *tail;
26842 const char *base;
26843 unsigned int i, count, encoded_filename_len, linebuf_len;
26844 macinfo_entry **slot;
26846 first = &(*macinfo_table)[idx];
26847 second = &(*macinfo_table)[idx + 1];
26849 /* Optimize only if there are at least two consecutive define/undef ops,
26850 and either all of them are before first DW_MACINFO_start_file
26851 with lineno {0,1} (i.e. predefined macro block), or all of them are
26852 in some included header file. */
26853 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26854 return 0;
26855 if (vec_safe_is_empty (files))
26857 if (first->lineno > 1 || second->lineno > 1)
26858 return 0;
26860 else if (first->lineno == 0)
26861 return 0;
26863 /* Find the last define/undef entry that can be grouped together
26864 with first and at the same time compute md5 checksum of their
26865 codes, linenumbers and strings. */
26866 md5_init_ctx (&ctx);
26867 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26868 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26869 break;
26870 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26871 break;
26872 else
26874 unsigned char code = cur->code;
26875 md5_process_bytes (&code, 1, &ctx);
26876 checksum_uleb128 (cur->lineno, &ctx);
26877 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26879 md5_finish_ctx (&ctx, checksum);
26880 count = i - idx;
26882 /* From the containing include filename (if any) pick up just
26883 usable characters from its basename. */
26884 if (vec_safe_is_empty (files))
26885 base = "";
26886 else
26887 base = lbasename (files->last ().info);
26888 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26889 if (ISIDNUM (base[i]) || base[i] == '.')
26890 encoded_filename_len++;
26891 /* Count . at the end. */
26892 if (encoded_filename_len)
26893 encoded_filename_len++;
26895 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26896 linebuf_len = strlen (linebuf);
26898 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26899 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26900 + 16 * 2 + 1);
26901 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26902 tail = grp_name + 4;
26903 if (encoded_filename_len)
26905 for (i = 0; base[i]; i++)
26906 if (ISIDNUM (base[i]) || base[i] == '.')
26907 *tail++ = base[i];
26908 *tail++ = '.';
26910 memcpy (tail, linebuf, linebuf_len);
26911 tail += linebuf_len;
26912 *tail++ = '.';
26913 for (i = 0; i < 16; i++)
26914 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26916 /* Construct a macinfo_entry for DW_MACRO_import
26917 in the empty vector entry before the first define/undef. */
26918 inc = &(*macinfo_table)[idx - 1];
26919 inc->code = DW_MACRO_import;
26920 inc->lineno = 0;
26921 inc->info = ggc_strdup (grp_name);
26922 if (!*macinfo_htab)
26923 *macinfo_htab = new macinfo_hash_type (10);
26924 /* Avoid emitting duplicates. */
26925 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26926 if (*slot != NULL)
26928 inc->code = 0;
26929 inc->info = NULL;
26930 /* If such an entry has been used before, just emit
26931 a DW_MACRO_import op. */
26932 inc = *slot;
26933 output_macinfo_op (inc);
26934 /* And clear all macinfo_entry in the range to avoid emitting them
26935 in the second pass. */
26936 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26938 cur->code = 0;
26939 cur->info = NULL;
26942 else
26944 *slot = inc;
26945 inc->lineno = (*macinfo_htab)->elements ();
26946 output_macinfo_op (inc);
26948 return count;
26951 /* Save any strings needed by the macinfo table in the debug str
26952 table. All strings must be collected into the table by the time
26953 index_string is called. */
26955 static void
26956 save_macinfo_strings (void)
26958 unsigned len;
26959 unsigned i;
26960 macinfo_entry *ref;
26962 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26964 switch (ref->code)
26966 /* Match the logic in output_macinfo_op to decide on
26967 indirect strings. */
26968 case DW_MACINFO_define:
26969 case DW_MACINFO_undef:
26970 len = strlen (ref->info) + 1;
26971 if (!dwarf_strict
26972 && len > DWARF_OFFSET_SIZE
26973 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26974 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26975 set_indirect_string (find_AT_string (ref->info));
26976 break;
26977 case DW_MACRO_define_strp:
26978 case DW_MACRO_undef_strp:
26979 set_indirect_string (find_AT_string (ref->info));
26980 break;
26981 default:
26982 break;
26987 /* Output macinfo section(s). */
26989 static void
26990 output_macinfo (void)
26992 unsigned i;
26993 unsigned long length = vec_safe_length (macinfo_table);
26994 macinfo_entry *ref;
26995 vec<macinfo_entry, va_gc> *files = NULL;
26996 macinfo_hash_type *macinfo_htab = NULL;
26997 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
26999 if (! length)
27000 return;
27002 /* output_macinfo* uses these interchangeably. */
27003 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27004 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27005 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27006 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27008 /* AIX Assembler inserts the length, so adjust the reference to match the
27009 offset expected by debuggers. */
27010 strcpy (dl_section_ref, debug_line_section_label);
27011 if (XCOFF_DEBUGGING_INFO)
27012 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27014 /* For .debug_macro emit the section header. */
27015 if (!dwarf_strict || dwarf_version >= 5)
27017 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27018 "DWARF macro version number");
27019 if (DWARF_OFFSET_SIZE == 8)
27020 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27021 else
27022 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27023 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27024 (!dwarf_split_debug_info ? dl_section_ref
27025 : debug_skeleton_line_section_label),
27026 debug_line_section, NULL);
27029 /* In the first loop, it emits the primary .debug_macinfo section
27030 and after each emitted op the macinfo_entry is cleared.
27031 If a longer range of define/undef ops can be optimized using
27032 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27033 the vector before the first define/undef in the range and the
27034 whole range of define/undef ops is not emitted and kept. */
27035 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27037 switch (ref->code)
27039 case DW_MACINFO_start_file:
27040 vec_safe_push (files, *ref);
27041 break;
27042 case DW_MACINFO_end_file:
27043 if (!vec_safe_is_empty (files))
27044 files->pop ();
27045 break;
27046 case DW_MACINFO_define:
27047 case DW_MACINFO_undef:
27048 if ((!dwarf_strict || dwarf_version >= 5)
27049 && HAVE_COMDAT_GROUP
27050 && vec_safe_length (files) != 1
27051 && i > 0
27052 && i + 1 < length
27053 && (*macinfo_table)[i - 1].code == 0)
27055 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27056 if (count)
27058 i += count - 1;
27059 continue;
27062 break;
27063 case 0:
27064 /* A dummy entry may be inserted at the beginning to be able
27065 to optimize the whole block of predefined macros. */
27066 if (i == 0)
27067 continue;
27068 default:
27069 break;
27071 output_macinfo_op (ref);
27072 ref->info = NULL;
27073 ref->code = 0;
27076 if (!macinfo_htab)
27077 return;
27079 delete macinfo_htab;
27080 macinfo_htab = NULL;
27082 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27083 terminate the current chain and switch to a new comdat .debug_macinfo
27084 section and emit the define/undef entries within it. */
27085 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27086 switch (ref->code)
27088 case 0:
27089 continue;
27090 case DW_MACRO_import:
27092 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27093 tree comdat_key = get_identifier (ref->info);
27094 /* Terminate the previous .debug_macinfo section. */
27095 dw2_asm_output_data (1, 0, "End compilation unit");
27096 targetm.asm_out.named_section (debug_macinfo_section_name,
27097 SECTION_DEBUG
27098 | SECTION_LINKONCE,
27099 comdat_key);
27100 ASM_GENERATE_INTERNAL_LABEL (label,
27101 DEBUG_MACRO_SECTION_LABEL,
27102 ref->lineno);
27103 ASM_OUTPUT_LABEL (asm_out_file, label);
27104 ref->code = 0;
27105 ref->info = NULL;
27106 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27107 "DWARF macro version number");
27108 if (DWARF_OFFSET_SIZE == 8)
27109 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27110 else
27111 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27113 break;
27114 case DW_MACINFO_define:
27115 case DW_MACINFO_undef:
27116 output_macinfo_op (ref);
27117 ref->code = 0;
27118 ref->info = NULL;
27119 break;
27120 default:
27121 gcc_unreachable ();
27125 /* Initialize the various sections and labels for dwarf output. */
27127 static void
27128 init_sections_and_labels (void)
27130 if (!dwarf_split_debug_info)
27132 debug_info_section = get_section (DEBUG_INFO_SECTION,
27133 SECTION_DEBUG, NULL);
27134 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27135 SECTION_DEBUG, NULL);
27136 debug_loc_section = get_section (dwarf_version >= 5
27137 ? DEBUG_LOCLISTS_SECTION
27138 : DEBUG_LOC_SECTION,
27139 SECTION_DEBUG, NULL);
27140 debug_macinfo_section_name
27141 = (dwarf_strict && dwarf_version < 5)
27142 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27143 debug_macinfo_section = get_section (debug_macinfo_section_name,
27144 SECTION_DEBUG, NULL);
27146 else
27148 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27149 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27150 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27151 SECTION_DEBUG | SECTION_EXCLUDE,
27152 NULL);
27153 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27154 SECTION_DEBUG, NULL);
27155 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27156 SECTION_DEBUG, NULL);
27157 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27158 SECTION_DEBUG, NULL);
27159 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27160 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27162 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27163 the main .o, but the skeleton_line goes into the split off dwo. */
27164 debug_skeleton_line_section
27165 = get_section (DEBUG_DWO_LINE_SECTION,
27166 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27167 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27168 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27169 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27170 SECTION_DEBUG | SECTION_EXCLUDE,
27171 NULL);
27172 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27173 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27174 debug_loc_section = get_section (dwarf_version >= 5
27175 ? DEBUG_DWO_LOCLISTS_SECTION
27176 : DEBUG_DWO_LOC_SECTION,
27177 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27178 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27179 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27180 debug_macinfo_section_name
27181 = (dwarf_strict && dwarf_version < 5)
27182 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27183 debug_macinfo_section = get_section (debug_macinfo_section_name,
27184 SECTION_DEBUG | SECTION_EXCLUDE,
27185 NULL);
27187 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27188 SECTION_DEBUG, NULL);
27189 debug_line_section = get_section (DEBUG_LINE_SECTION,
27190 SECTION_DEBUG, NULL);
27191 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27192 SECTION_DEBUG, NULL);
27193 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27194 SECTION_DEBUG, NULL);
27195 debug_str_section = get_section (DEBUG_STR_SECTION,
27196 DEBUG_STR_SECTION_FLAGS, NULL);
27197 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27198 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27199 DEBUG_STR_SECTION_FLAGS, NULL);
27201 debug_ranges_section = get_section (dwarf_version >= 5
27202 ? DEBUG_RNGLISTS_SECTION
27203 : DEBUG_RANGES_SECTION,
27204 SECTION_DEBUG, NULL);
27205 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27206 SECTION_DEBUG, NULL);
27208 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27209 DEBUG_ABBREV_SECTION_LABEL, 0);
27210 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27211 DEBUG_INFO_SECTION_LABEL, 0);
27212 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27213 DEBUG_LINE_SECTION_LABEL, 0);
27214 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27215 DEBUG_RANGES_SECTION_LABEL, 0);
27216 if (dwarf_version >= 5 && dwarf_split_debug_info)
27217 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27218 DEBUG_RANGES_SECTION_LABEL, 1);
27219 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27220 DEBUG_ADDR_SECTION_LABEL, 0);
27221 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27222 (dwarf_strict && dwarf_version < 5)
27223 ? DEBUG_MACINFO_SECTION_LABEL
27224 : DEBUG_MACRO_SECTION_LABEL, 0);
27225 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27228 /* Set up for Dwarf output at the start of compilation. */
27230 static void
27231 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27233 /* This option is currently broken, see (PR53118 and PR46102). */
27234 if (flag_eliminate_dwarf2_dups
27235 && strstr (lang_hooks.name, "C++"))
27237 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27238 flag_eliminate_dwarf2_dups = 0;
27241 /* Allocate the file_table. */
27242 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27244 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27245 /* Allocate the decl_die_table. */
27246 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27248 /* Allocate the decl_loc_table. */
27249 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27251 /* Allocate the cached_dw_loc_list_table. */
27252 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27254 /* Allocate the initial hunk of the decl_scope_table. */
27255 vec_alloc (decl_scope_table, 256);
27257 /* Allocate the initial hunk of the abbrev_die_table. */
27258 vec_alloc (abbrev_die_table, 256);
27259 /* Zero-th entry is allocated, but unused. */
27260 abbrev_die_table->quick_push (NULL);
27262 /* Allocate the dwarf_proc_stack_usage_map. */
27263 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27265 /* Allocate the pubtypes and pubnames vectors. */
27266 vec_alloc (pubname_table, 32);
27267 vec_alloc (pubtype_table, 32);
27269 vec_alloc (incomplete_types, 64);
27271 vec_alloc (used_rtx_array, 32);
27273 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27274 vec_alloc (macinfo_table, 64);
27275 #endif
27277 /* If front-ends already registered a main translation unit but we were not
27278 ready to perform the association, do this now. */
27279 if (main_translation_unit != NULL_TREE)
27280 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27283 /* Called before compile () starts outputtting functions, variables
27284 and toplevel asms into assembly. */
27286 static void
27287 dwarf2out_assembly_start (void)
27289 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27290 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27291 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27292 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27293 COLD_TEXT_SECTION_LABEL, 0);
27294 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27296 switch_to_section (text_section);
27297 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27298 #endif
27300 /* Make sure the line number table for .text always exists. */
27301 text_section_line_info = new_line_info_table ();
27302 text_section_line_info->end_label = text_end_label;
27304 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27305 cur_line_info_table = text_section_line_info;
27306 #endif
27308 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27309 && dwarf2out_do_cfi_asm ()
27310 && (!(flag_unwind_tables || flag_exceptions)
27311 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27312 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27315 /* A helper function for dwarf2out_finish called through
27316 htab_traverse. Assign a string its index. All strings must be
27317 collected into the table by the time index_string is called,
27318 because the indexing code relies on htab_traverse to traverse nodes
27319 in the same order for each run. */
27322 index_string (indirect_string_node **h, unsigned int *index)
27324 indirect_string_node *node = *h;
27326 find_string_form (node);
27327 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27329 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27330 node->index = *index;
27331 *index += 1;
27333 return 1;
27336 /* A helper function for output_indirect_strings called through
27337 htab_traverse. Output the offset to a string and update the
27338 current offset. */
27341 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27343 indirect_string_node *node = *h;
27345 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27347 /* Assert that this node has been assigned an index. */
27348 gcc_assert (node->index != NO_INDEX_ASSIGNED
27349 && node->index != NOT_INDEXED);
27350 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27351 "indexed string 0x%x: %s", node->index, node->str);
27352 *offset += strlen (node->str) + 1;
27354 return 1;
27357 /* A helper function for dwarf2out_finish called through
27358 htab_traverse. Output the indexed string. */
27361 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27363 struct indirect_string_node *node = *h;
27365 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27367 /* Assert that the strings are output in the same order as their
27368 indexes were assigned. */
27369 gcc_assert (*cur_idx == node->index);
27370 assemble_string (node->str, strlen (node->str) + 1);
27371 *cur_idx += 1;
27373 return 1;
27376 /* A helper function for dwarf2out_finish called through
27377 htab_traverse. Emit one queued .debug_str string. */
27380 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27382 struct indirect_string_node *node = *h;
27384 node->form = find_string_form (node);
27385 if (node->form == form && node->refcount > 0)
27387 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27388 assemble_string (node->str, strlen (node->str) + 1);
27391 return 1;
27394 /* Output the indexed string table. */
27396 static void
27397 output_indirect_strings (void)
27399 switch_to_section (debug_str_section);
27400 if (!dwarf_split_debug_info)
27401 debug_str_hash->traverse<enum dwarf_form,
27402 output_indirect_string> (DW_FORM_strp);
27403 else
27405 unsigned int offset = 0;
27406 unsigned int cur_idx = 0;
27408 skeleton_debug_str_hash->traverse<enum dwarf_form,
27409 output_indirect_string> (DW_FORM_strp);
27411 switch_to_section (debug_str_offsets_section);
27412 debug_str_hash->traverse_noresize
27413 <unsigned int *, output_index_string_offset> (&offset);
27414 switch_to_section (debug_str_dwo_section);
27415 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27416 (&cur_idx);
27420 /* Callback for htab_traverse to assign an index to an entry in the
27421 table, and to write that entry to the .debug_addr section. */
27424 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27426 addr_table_entry *entry = *slot;
27428 if (entry->refcount == 0)
27430 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27431 || entry->index == NOT_INDEXED);
27432 return 1;
27435 gcc_assert (entry->index == *cur_index);
27436 (*cur_index)++;
27438 switch (entry->kind)
27440 case ate_kind_rtx:
27441 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27442 "0x%x", entry->index);
27443 break;
27444 case ate_kind_rtx_dtprel:
27445 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27446 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27447 DWARF2_ADDR_SIZE,
27448 entry->addr.rtl);
27449 fputc ('\n', asm_out_file);
27450 break;
27451 case ate_kind_label:
27452 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27453 "0x%x", entry->index);
27454 break;
27455 default:
27456 gcc_unreachable ();
27458 return 1;
27461 /* Produce the .debug_addr section. */
27463 static void
27464 output_addr_table (void)
27466 unsigned int index = 0;
27467 if (addr_index_table == NULL || addr_index_table->size () == 0)
27468 return;
27470 switch_to_section (debug_addr_section);
27471 addr_index_table
27472 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27475 #if ENABLE_ASSERT_CHECKING
27476 /* Verify that all marks are clear. */
27478 static void
27479 verify_marks_clear (dw_die_ref die)
27481 dw_die_ref c;
27483 gcc_assert (! die->die_mark);
27484 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27486 #endif /* ENABLE_ASSERT_CHECKING */
27488 /* Clear the marks for a die and its children.
27489 Be cool if the mark isn't set. */
27491 static void
27492 prune_unmark_dies (dw_die_ref die)
27494 dw_die_ref c;
27496 if (die->die_mark)
27497 die->die_mark = 0;
27498 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27501 /* Given LOC that is referenced by a DIE we're marking as used, find all
27502 referenced DWARF procedures it references and mark them as used. */
27504 static void
27505 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27507 for (; loc != NULL; loc = loc->dw_loc_next)
27508 switch (loc->dw_loc_opc)
27510 case DW_OP_implicit_pointer:
27511 case DW_OP_convert:
27512 case DW_OP_reinterpret:
27513 case DW_OP_GNU_implicit_pointer:
27514 case DW_OP_GNU_convert:
27515 case DW_OP_GNU_reinterpret:
27516 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27517 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27518 break;
27519 case DW_OP_GNU_variable_value:
27520 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27522 dw_die_ref ref
27523 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27524 if (ref == NULL)
27525 break;
27526 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27527 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27528 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27530 /* FALLTHRU */
27531 case DW_OP_call2:
27532 case DW_OP_call4:
27533 case DW_OP_call_ref:
27534 case DW_OP_const_type:
27535 case DW_OP_GNU_const_type:
27536 case DW_OP_GNU_parameter_ref:
27537 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27538 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27539 break;
27540 case DW_OP_regval_type:
27541 case DW_OP_deref_type:
27542 case DW_OP_GNU_regval_type:
27543 case DW_OP_GNU_deref_type:
27544 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27545 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27546 break;
27547 case DW_OP_entry_value:
27548 case DW_OP_GNU_entry_value:
27549 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27550 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27551 break;
27552 default:
27553 break;
27557 /* Given DIE that we're marking as used, find any other dies
27558 it references as attributes and mark them as used. */
27560 static void
27561 prune_unused_types_walk_attribs (dw_die_ref die)
27563 dw_attr_node *a;
27564 unsigned ix;
27566 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27568 switch (AT_class (a))
27570 /* Make sure DWARF procedures referenced by location descriptions will
27571 get emitted. */
27572 case dw_val_class_loc:
27573 prune_unused_types_walk_loc_descr (AT_loc (a));
27574 break;
27575 case dw_val_class_loc_list:
27576 for (dw_loc_list_ref list = AT_loc_list (a);
27577 list != NULL;
27578 list = list->dw_loc_next)
27579 prune_unused_types_walk_loc_descr (list->expr);
27580 break;
27582 case dw_val_class_die_ref:
27583 /* A reference to another DIE.
27584 Make sure that it will get emitted.
27585 If it was broken out into a comdat group, don't follow it. */
27586 if (! AT_ref (a)->comdat_type_p
27587 || a->dw_attr == DW_AT_specification)
27588 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27589 break;
27591 case dw_val_class_str:
27592 /* Set the string's refcount to 0 so that prune_unused_types_mark
27593 accounts properly for it. */
27594 a->dw_attr_val.v.val_str->refcount = 0;
27595 break;
27597 default:
27598 break;
27603 /* Mark the generic parameters and arguments children DIEs of DIE. */
27605 static void
27606 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27608 dw_die_ref c;
27610 if (die == NULL || die->die_child == NULL)
27611 return;
27612 c = die->die_child;
27615 if (is_template_parameter (c))
27616 prune_unused_types_mark (c, 1);
27617 c = c->die_sib;
27618 } while (c && c != die->die_child);
27621 /* Mark DIE as being used. If DOKIDS is true, then walk down
27622 to DIE's children. */
27624 static void
27625 prune_unused_types_mark (dw_die_ref die, int dokids)
27627 dw_die_ref c;
27629 if (die->die_mark == 0)
27631 /* We haven't done this node yet. Mark it as used. */
27632 die->die_mark = 1;
27633 /* If this is the DIE of a generic type instantiation,
27634 mark the children DIEs that describe its generic parms and
27635 args. */
27636 prune_unused_types_mark_generic_parms_dies (die);
27638 /* We also have to mark its parents as used.
27639 (But we don't want to mark our parent's kids due to this,
27640 unless it is a class.) */
27641 if (die->die_parent)
27642 prune_unused_types_mark (die->die_parent,
27643 class_scope_p (die->die_parent));
27645 /* Mark any referenced nodes. */
27646 prune_unused_types_walk_attribs (die);
27648 /* If this node is a specification,
27649 also mark the definition, if it exists. */
27650 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27651 prune_unused_types_mark (die->die_definition, 1);
27654 if (dokids && die->die_mark != 2)
27656 /* We need to walk the children, but haven't done so yet.
27657 Remember that we've walked the kids. */
27658 die->die_mark = 2;
27660 /* If this is an array type, we need to make sure our
27661 kids get marked, even if they're types. If we're
27662 breaking out types into comdat sections, do this
27663 for all type definitions. */
27664 if (die->die_tag == DW_TAG_array_type
27665 || (use_debug_types
27666 && is_type_die (die) && ! is_declaration_die (die)))
27667 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27668 else
27669 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27673 /* For local classes, look if any static member functions were emitted
27674 and if so, mark them. */
27676 static void
27677 prune_unused_types_walk_local_classes (dw_die_ref die)
27679 dw_die_ref c;
27681 if (die->die_mark == 2)
27682 return;
27684 switch (die->die_tag)
27686 case DW_TAG_structure_type:
27687 case DW_TAG_union_type:
27688 case DW_TAG_class_type:
27689 break;
27691 case DW_TAG_subprogram:
27692 if (!get_AT_flag (die, DW_AT_declaration)
27693 || die->die_definition != NULL)
27694 prune_unused_types_mark (die, 1);
27695 return;
27697 default:
27698 return;
27701 /* Mark children. */
27702 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27705 /* Walk the tree DIE and mark types that we actually use. */
27707 static void
27708 prune_unused_types_walk (dw_die_ref die)
27710 dw_die_ref c;
27712 /* Don't do anything if this node is already marked and
27713 children have been marked as well. */
27714 if (die->die_mark == 2)
27715 return;
27717 switch (die->die_tag)
27719 case DW_TAG_structure_type:
27720 case DW_TAG_union_type:
27721 case DW_TAG_class_type:
27722 if (die->die_perennial_p)
27723 break;
27725 for (c = die->die_parent; c; c = c->die_parent)
27726 if (c->die_tag == DW_TAG_subprogram)
27727 break;
27729 /* Finding used static member functions inside of classes
27730 is needed just for local classes, because for other classes
27731 static member function DIEs with DW_AT_specification
27732 are emitted outside of the DW_TAG_*_type. If we ever change
27733 it, we'd need to call this even for non-local classes. */
27734 if (c)
27735 prune_unused_types_walk_local_classes (die);
27737 /* It's a type node --- don't mark it. */
27738 return;
27740 case DW_TAG_const_type:
27741 case DW_TAG_packed_type:
27742 case DW_TAG_pointer_type:
27743 case DW_TAG_reference_type:
27744 case DW_TAG_rvalue_reference_type:
27745 case DW_TAG_volatile_type:
27746 case DW_TAG_typedef:
27747 case DW_TAG_array_type:
27748 case DW_TAG_interface_type:
27749 case DW_TAG_friend:
27750 case DW_TAG_enumeration_type:
27751 case DW_TAG_subroutine_type:
27752 case DW_TAG_string_type:
27753 case DW_TAG_set_type:
27754 case DW_TAG_subrange_type:
27755 case DW_TAG_ptr_to_member_type:
27756 case DW_TAG_file_type:
27757 /* Type nodes are useful only when other DIEs reference them --- don't
27758 mark them. */
27759 /* FALLTHROUGH */
27761 case DW_TAG_dwarf_procedure:
27762 /* Likewise for DWARF procedures. */
27764 if (die->die_perennial_p)
27765 break;
27767 return;
27769 default:
27770 /* Mark everything else. */
27771 break;
27774 if (die->die_mark == 0)
27776 die->die_mark = 1;
27778 /* Now, mark any dies referenced from here. */
27779 prune_unused_types_walk_attribs (die);
27782 die->die_mark = 2;
27784 /* Mark children. */
27785 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27788 /* Increment the string counts on strings referred to from DIE's
27789 attributes. */
27791 static void
27792 prune_unused_types_update_strings (dw_die_ref die)
27794 dw_attr_node *a;
27795 unsigned ix;
27797 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27798 if (AT_class (a) == dw_val_class_str)
27800 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27801 s->refcount++;
27802 /* Avoid unnecessarily putting strings that are used less than
27803 twice in the hash table. */
27804 if (s->refcount
27805 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27807 indirect_string_node **slot
27808 = debug_str_hash->find_slot_with_hash (s->str,
27809 htab_hash_string (s->str),
27810 INSERT);
27811 gcc_assert (*slot == NULL);
27812 *slot = s;
27817 /* Mark DIE and its children as removed. */
27819 static void
27820 mark_removed (dw_die_ref die)
27822 dw_die_ref c;
27823 die->removed = true;
27824 FOR_EACH_CHILD (die, c, mark_removed (c));
27827 /* Remove from the tree DIE any dies that aren't marked. */
27829 static void
27830 prune_unused_types_prune (dw_die_ref die)
27832 dw_die_ref c;
27834 gcc_assert (die->die_mark);
27835 prune_unused_types_update_strings (die);
27837 if (! die->die_child)
27838 return;
27840 c = die->die_child;
27841 do {
27842 dw_die_ref prev = c, next;
27843 for (c = c->die_sib; ! c->die_mark; c = next)
27844 if (c == die->die_child)
27846 /* No marked children between 'prev' and the end of the list. */
27847 if (prev == c)
27848 /* No marked children at all. */
27849 die->die_child = NULL;
27850 else
27852 prev->die_sib = c->die_sib;
27853 die->die_child = prev;
27855 c->die_sib = NULL;
27856 mark_removed (c);
27857 return;
27859 else
27861 next = c->die_sib;
27862 c->die_sib = NULL;
27863 mark_removed (c);
27866 if (c != prev->die_sib)
27867 prev->die_sib = c;
27868 prune_unused_types_prune (c);
27869 } while (c != die->die_child);
27872 /* Remove dies representing declarations that we never use. */
27874 static void
27875 prune_unused_types (void)
27877 unsigned int i;
27878 limbo_die_node *node;
27879 comdat_type_node *ctnode;
27880 pubname_entry *pub;
27881 dw_die_ref base_type;
27883 #if ENABLE_ASSERT_CHECKING
27884 /* All the marks should already be clear. */
27885 verify_marks_clear (comp_unit_die ());
27886 for (node = limbo_die_list; node; node = node->next)
27887 verify_marks_clear (node->die);
27888 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27889 verify_marks_clear (ctnode->root_die);
27890 #endif /* ENABLE_ASSERT_CHECKING */
27892 /* Mark types that are used in global variables. */
27893 premark_types_used_by_global_vars ();
27895 /* Set the mark on nodes that are actually used. */
27896 prune_unused_types_walk (comp_unit_die ());
27897 for (node = limbo_die_list; node; node = node->next)
27898 prune_unused_types_walk (node->die);
27899 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27901 prune_unused_types_walk (ctnode->root_die);
27902 prune_unused_types_mark (ctnode->type_die, 1);
27905 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27906 are unusual in that they are pubnames that are the children of pubtypes.
27907 They should only be marked via their parent DW_TAG_enumeration_type die,
27908 not as roots in themselves. */
27909 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27910 if (pub->die->die_tag != DW_TAG_enumerator)
27911 prune_unused_types_mark (pub->die, 1);
27912 for (i = 0; base_types.iterate (i, &base_type); i++)
27913 prune_unused_types_mark (base_type, 1);
27915 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27916 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27917 callees). */
27918 cgraph_node *cnode;
27919 FOR_EACH_FUNCTION (cnode)
27920 if (cnode->referred_to_p (false))
27922 dw_die_ref die = lookup_decl_die (cnode->decl);
27923 if (die == NULL || die->die_mark)
27924 continue;
27925 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27926 if (e->caller != cnode
27927 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27929 prune_unused_types_mark (die, 1);
27930 break;
27934 if (debug_str_hash)
27935 debug_str_hash->empty ();
27936 if (skeleton_debug_str_hash)
27937 skeleton_debug_str_hash->empty ();
27938 prune_unused_types_prune (comp_unit_die ());
27939 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27941 node = *pnode;
27942 if (!node->die->die_mark)
27943 *pnode = node->next;
27944 else
27946 prune_unused_types_prune (node->die);
27947 pnode = &node->next;
27950 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27951 prune_unused_types_prune (ctnode->root_die);
27953 /* Leave the marks clear. */
27954 prune_unmark_dies (comp_unit_die ());
27955 for (node = limbo_die_list; node; node = node->next)
27956 prune_unmark_dies (node->die);
27957 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27958 prune_unmark_dies (ctnode->root_die);
27961 /* Helpers to manipulate hash table of comdat type units. */
27963 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27965 static inline hashval_t hash (const comdat_type_node *);
27966 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27969 inline hashval_t
27970 comdat_type_hasher::hash (const comdat_type_node *type_node)
27972 hashval_t h;
27973 memcpy (&h, type_node->signature, sizeof (h));
27974 return h;
27977 inline bool
27978 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27979 const comdat_type_node *type_node_2)
27981 return (! memcmp (type_node_1->signature, type_node_2->signature,
27982 DWARF_TYPE_SIGNATURE_SIZE));
27985 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27986 to the location it would have been added, should we know its
27987 DECL_ASSEMBLER_NAME when we added other attributes. This will
27988 probably improve compactness of debug info, removing equivalent
27989 abbrevs, and hide any differences caused by deferring the
27990 computation of the assembler name, triggered by e.g. PCH. */
27992 static inline void
27993 move_linkage_attr (dw_die_ref die)
27995 unsigned ix = vec_safe_length (die->die_attr);
27996 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27998 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27999 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28001 while (--ix > 0)
28003 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28005 if (prev->dw_attr == DW_AT_decl_line
28006 || prev->dw_attr == DW_AT_decl_column
28007 || prev->dw_attr == DW_AT_name)
28008 break;
28011 if (ix != vec_safe_length (die->die_attr) - 1)
28013 die->die_attr->pop ();
28014 die->die_attr->quick_insert (ix, linkage);
28018 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28019 referenced from typed stack ops and count how often they are used. */
28021 static void
28022 mark_base_types (dw_loc_descr_ref loc)
28024 dw_die_ref base_type = NULL;
28026 for (; loc; loc = loc->dw_loc_next)
28028 switch (loc->dw_loc_opc)
28030 case DW_OP_regval_type:
28031 case DW_OP_deref_type:
28032 case DW_OP_GNU_regval_type:
28033 case DW_OP_GNU_deref_type:
28034 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28035 break;
28036 case DW_OP_convert:
28037 case DW_OP_reinterpret:
28038 case DW_OP_GNU_convert:
28039 case DW_OP_GNU_reinterpret:
28040 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28041 continue;
28042 /* FALLTHRU */
28043 case DW_OP_const_type:
28044 case DW_OP_GNU_const_type:
28045 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28046 break;
28047 case DW_OP_entry_value:
28048 case DW_OP_GNU_entry_value:
28049 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28050 continue;
28051 default:
28052 continue;
28054 gcc_assert (base_type->die_parent == comp_unit_die ());
28055 if (base_type->die_mark)
28056 base_type->die_mark++;
28057 else
28059 base_types.safe_push (base_type);
28060 base_type->die_mark = 1;
28065 /* Comparison function for sorting marked base types. */
28067 static int
28068 base_type_cmp (const void *x, const void *y)
28070 dw_die_ref dx = *(const dw_die_ref *) x;
28071 dw_die_ref dy = *(const dw_die_ref *) y;
28072 unsigned int byte_size1, byte_size2;
28073 unsigned int encoding1, encoding2;
28074 unsigned int align1, align2;
28075 if (dx->die_mark > dy->die_mark)
28076 return -1;
28077 if (dx->die_mark < dy->die_mark)
28078 return 1;
28079 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28080 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28081 if (byte_size1 < byte_size2)
28082 return 1;
28083 if (byte_size1 > byte_size2)
28084 return -1;
28085 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28086 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28087 if (encoding1 < encoding2)
28088 return 1;
28089 if (encoding1 > encoding2)
28090 return -1;
28091 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28092 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28093 if (align1 < align2)
28094 return 1;
28095 if (align1 > align2)
28096 return -1;
28097 return 0;
28100 /* Move base types marked by mark_base_types as early as possible
28101 in the CU, sorted by decreasing usage count both to make the
28102 uleb128 references as small as possible and to make sure they
28103 will have die_offset already computed by calc_die_sizes when
28104 sizes of typed stack loc ops is computed. */
28106 static void
28107 move_marked_base_types (void)
28109 unsigned int i;
28110 dw_die_ref base_type, die, c;
28112 if (base_types.is_empty ())
28113 return;
28115 /* Sort by decreasing usage count, they will be added again in that
28116 order later on. */
28117 base_types.qsort (base_type_cmp);
28118 die = comp_unit_die ();
28119 c = die->die_child;
28122 dw_die_ref prev = c;
28123 c = c->die_sib;
28124 while (c->die_mark)
28126 remove_child_with_prev (c, prev);
28127 /* As base types got marked, there must be at least
28128 one node other than DW_TAG_base_type. */
28129 gcc_assert (die->die_child != NULL);
28130 c = prev->die_sib;
28133 while (c != die->die_child);
28134 gcc_assert (die->die_child);
28135 c = die->die_child;
28136 for (i = 0; base_types.iterate (i, &base_type); i++)
28138 base_type->die_mark = 0;
28139 base_type->die_sib = c->die_sib;
28140 c->die_sib = base_type;
28141 c = base_type;
28145 /* Helper function for resolve_addr, attempt to resolve
28146 one CONST_STRING, return true if successful. Similarly verify that
28147 SYMBOL_REFs refer to variables emitted in the current CU. */
28149 static bool
28150 resolve_one_addr (rtx *addr)
28152 rtx rtl = *addr;
28154 if (GET_CODE (rtl) == CONST_STRING)
28156 size_t len = strlen (XSTR (rtl, 0)) + 1;
28157 tree t = build_string (len, XSTR (rtl, 0));
28158 tree tlen = size_int (len - 1);
28159 TREE_TYPE (t)
28160 = build_array_type (char_type_node, build_index_type (tlen));
28161 rtl = lookup_constant_def (t);
28162 if (!rtl || !MEM_P (rtl))
28163 return false;
28164 rtl = XEXP (rtl, 0);
28165 if (GET_CODE (rtl) == SYMBOL_REF
28166 && SYMBOL_REF_DECL (rtl)
28167 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28168 return false;
28169 vec_safe_push (used_rtx_array, rtl);
28170 *addr = rtl;
28171 return true;
28174 if (GET_CODE (rtl) == SYMBOL_REF
28175 && SYMBOL_REF_DECL (rtl))
28177 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28179 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28180 return false;
28182 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28183 return false;
28186 if (GET_CODE (rtl) == CONST)
28188 subrtx_ptr_iterator::array_type array;
28189 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28190 if (!resolve_one_addr (*iter))
28191 return false;
28194 return true;
28197 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28198 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28199 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28201 static rtx
28202 string_cst_pool_decl (tree t)
28204 rtx rtl = output_constant_def (t, 1);
28205 unsigned char *array;
28206 dw_loc_descr_ref l;
28207 tree decl;
28208 size_t len;
28209 dw_die_ref ref;
28211 if (!rtl || !MEM_P (rtl))
28212 return NULL_RTX;
28213 rtl = XEXP (rtl, 0);
28214 if (GET_CODE (rtl) != SYMBOL_REF
28215 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28216 return NULL_RTX;
28218 decl = SYMBOL_REF_DECL (rtl);
28219 if (!lookup_decl_die (decl))
28221 len = TREE_STRING_LENGTH (t);
28222 vec_safe_push (used_rtx_array, rtl);
28223 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28224 array = ggc_vec_alloc<unsigned char> (len);
28225 memcpy (array, TREE_STRING_POINTER (t), len);
28226 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28227 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28228 l->dw_loc_oprnd2.v.val_vec.length = len;
28229 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28230 l->dw_loc_oprnd2.v.val_vec.array = array;
28231 add_AT_loc (ref, DW_AT_location, l);
28232 equate_decl_number_to_die (decl, ref);
28234 return rtl;
28237 /* Helper function of resolve_addr_in_expr. LOC is
28238 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28239 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28240 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28241 with DW_OP_implicit_pointer if possible
28242 and return true, if unsuccessful, return false. */
28244 static bool
28245 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28247 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28248 HOST_WIDE_INT offset = 0;
28249 dw_die_ref ref = NULL;
28250 tree decl;
28252 if (GET_CODE (rtl) == CONST
28253 && GET_CODE (XEXP (rtl, 0)) == PLUS
28254 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28256 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28257 rtl = XEXP (XEXP (rtl, 0), 0);
28259 if (GET_CODE (rtl) == CONST_STRING)
28261 size_t len = strlen (XSTR (rtl, 0)) + 1;
28262 tree t = build_string (len, XSTR (rtl, 0));
28263 tree tlen = size_int (len - 1);
28265 TREE_TYPE (t)
28266 = build_array_type (char_type_node, build_index_type (tlen));
28267 rtl = string_cst_pool_decl (t);
28268 if (!rtl)
28269 return false;
28271 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28273 decl = SYMBOL_REF_DECL (rtl);
28274 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28276 ref = lookup_decl_die (decl);
28277 if (ref && (get_AT (ref, DW_AT_location)
28278 || get_AT (ref, DW_AT_const_value)))
28280 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28281 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28282 loc->dw_loc_oprnd1.val_entry = NULL;
28283 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28284 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28285 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28286 loc->dw_loc_oprnd2.v.val_int = offset;
28287 return true;
28291 return false;
28294 /* Helper function for resolve_addr, handle one location
28295 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28296 the location list couldn't be resolved. */
28298 static bool
28299 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28301 dw_loc_descr_ref keep = NULL;
28302 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28303 switch (loc->dw_loc_opc)
28305 case DW_OP_addr:
28306 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28308 if ((prev == NULL
28309 || prev->dw_loc_opc == DW_OP_piece
28310 || prev->dw_loc_opc == DW_OP_bit_piece)
28311 && loc->dw_loc_next
28312 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28313 && (!dwarf_strict || dwarf_version >= 5)
28314 && optimize_one_addr_into_implicit_ptr (loc))
28315 break;
28316 return false;
28318 break;
28319 case DW_OP_GNU_addr_index:
28320 case DW_OP_GNU_const_index:
28321 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28322 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28324 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28325 if (!resolve_one_addr (&rtl))
28326 return false;
28327 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28328 loc->dw_loc_oprnd1.val_entry
28329 = add_addr_table_entry (rtl, ate_kind_rtx);
28331 break;
28332 case DW_OP_const4u:
28333 case DW_OP_const8u:
28334 if (loc->dtprel
28335 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28336 return false;
28337 break;
28338 case DW_OP_plus_uconst:
28339 if (size_of_loc_descr (loc)
28340 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28342 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28344 dw_loc_descr_ref repl
28345 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28346 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28347 add_loc_descr (&repl, loc->dw_loc_next);
28348 *loc = *repl;
28350 break;
28351 case DW_OP_implicit_value:
28352 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28353 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28354 return false;
28355 break;
28356 case DW_OP_implicit_pointer:
28357 case DW_OP_GNU_implicit_pointer:
28358 case DW_OP_GNU_parameter_ref:
28359 case DW_OP_GNU_variable_value:
28360 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28362 dw_die_ref ref
28363 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28364 if (ref == NULL)
28365 return false;
28366 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28367 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28368 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28370 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28372 if (prev == NULL
28373 && loc->dw_loc_next == NULL
28374 && AT_class (a) == dw_val_class_loc)
28375 switch (a->dw_attr)
28377 /* Following attributes allow both exprloc and reference,
28378 so if the whole expression is DW_OP_GNU_variable_value
28379 alone we could transform it into reference. */
28380 case DW_AT_byte_size:
28381 case DW_AT_bit_size:
28382 case DW_AT_lower_bound:
28383 case DW_AT_upper_bound:
28384 case DW_AT_bit_stride:
28385 case DW_AT_count:
28386 case DW_AT_allocated:
28387 case DW_AT_associated:
28388 case DW_AT_byte_stride:
28389 a->dw_attr_val.val_class = dw_val_class_die_ref;
28390 a->dw_attr_val.val_entry = NULL;
28391 a->dw_attr_val.v.val_die_ref.die
28392 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28393 a->dw_attr_val.v.val_die_ref.external = 0;
28394 return true;
28395 default:
28396 break;
28398 if (dwarf_strict)
28399 return false;
28401 break;
28402 case DW_OP_const_type:
28403 case DW_OP_regval_type:
28404 case DW_OP_deref_type:
28405 case DW_OP_convert:
28406 case DW_OP_reinterpret:
28407 case DW_OP_GNU_const_type:
28408 case DW_OP_GNU_regval_type:
28409 case DW_OP_GNU_deref_type:
28410 case DW_OP_GNU_convert:
28411 case DW_OP_GNU_reinterpret:
28412 while (loc->dw_loc_next
28413 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28414 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28416 dw_die_ref base1, base2;
28417 unsigned enc1, enc2, size1, size2;
28418 if (loc->dw_loc_opc == DW_OP_regval_type
28419 || loc->dw_loc_opc == DW_OP_deref_type
28420 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28421 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28422 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28423 else if (loc->dw_loc_oprnd1.val_class
28424 == dw_val_class_unsigned_const)
28425 break;
28426 else
28427 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28428 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28429 == dw_val_class_unsigned_const)
28430 break;
28431 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28432 gcc_assert (base1->die_tag == DW_TAG_base_type
28433 && base2->die_tag == DW_TAG_base_type);
28434 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28435 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28436 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28437 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28438 if (size1 == size2
28439 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28440 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28441 && loc != keep)
28442 || enc1 == enc2))
28444 /* Optimize away next DW_OP_convert after
28445 adjusting LOC's base type die reference. */
28446 if (loc->dw_loc_opc == DW_OP_regval_type
28447 || loc->dw_loc_opc == DW_OP_deref_type
28448 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28449 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28450 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28451 else
28452 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28453 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28454 continue;
28456 /* Don't change integer DW_OP_convert after e.g. floating
28457 point typed stack entry. */
28458 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28459 keep = loc->dw_loc_next;
28460 break;
28462 break;
28463 default:
28464 break;
28466 return true;
28469 /* Helper function of resolve_addr. DIE had DW_AT_location of
28470 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28471 and DW_OP_addr couldn't be resolved. resolve_addr has already
28472 removed the DW_AT_location attribute. This function attempts to
28473 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28474 to it or DW_AT_const_value attribute, if possible. */
28476 static void
28477 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28479 if (!VAR_P (decl)
28480 || lookup_decl_die (decl) != die
28481 || DECL_EXTERNAL (decl)
28482 || !TREE_STATIC (decl)
28483 || DECL_INITIAL (decl) == NULL_TREE
28484 || DECL_P (DECL_INITIAL (decl))
28485 || get_AT (die, DW_AT_const_value))
28486 return;
28488 tree init = DECL_INITIAL (decl);
28489 HOST_WIDE_INT offset = 0;
28490 /* For variables that have been optimized away and thus
28491 don't have a memory location, see if we can emit
28492 DW_AT_const_value instead. */
28493 if (tree_add_const_value_attribute (die, init))
28494 return;
28495 if (dwarf_strict && dwarf_version < 5)
28496 return;
28497 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28498 and ADDR_EXPR refers to a decl that has DW_AT_location or
28499 DW_AT_const_value (but isn't addressable, otherwise
28500 resolving the original DW_OP_addr wouldn't fail), see if
28501 we can add DW_OP_implicit_pointer. */
28502 STRIP_NOPS (init);
28503 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28504 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28506 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28507 init = TREE_OPERAND (init, 0);
28508 STRIP_NOPS (init);
28510 if (TREE_CODE (init) != ADDR_EXPR)
28511 return;
28512 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28513 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28514 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28515 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28516 && TREE_OPERAND (init, 0) != decl))
28518 dw_die_ref ref;
28519 dw_loc_descr_ref l;
28521 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28523 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28524 if (!rtl)
28525 return;
28526 decl = SYMBOL_REF_DECL (rtl);
28528 else
28529 decl = TREE_OPERAND (init, 0);
28530 ref = lookup_decl_die (decl);
28531 if (ref == NULL
28532 || (!get_AT (ref, DW_AT_location)
28533 && !get_AT (ref, DW_AT_const_value)))
28534 return;
28535 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28536 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28537 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28538 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28539 add_AT_loc (die, DW_AT_location, l);
28543 /* Return NULL if l is a DWARF expression, or first op that is not
28544 valid DWARF expression. */
28546 static dw_loc_descr_ref
28547 non_dwarf_expression (dw_loc_descr_ref l)
28549 while (l)
28551 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28552 return l;
28553 switch (l->dw_loc_opc)
28555 case DW_OP_regx:
28556 case DW_OP_implicit_value:
28557 case DW_OP_stack_value:
28558 case DW_OP_implicit_pointer:
28559 case DW_OP_GNU_implicit_pointer:
28560 case DW_OP_GNU_parameter_ref:
28561 case DW_OP_piece:
28562 case DW_OP_bit_piece:
28563 return l;
28564 default:
28565 break;
28567 l = l->dw_loc_next;
28569 return NULL;
28572 /* Return adjusted copy of EXPR:
28573 If it is empty DWARF expression, return it.
28574 If it is valid non-empty DWARF expression,
28575 return copy of EXPR with DW_OP_deref appended to it.
28576 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28577 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28578 If it is DWARF expression followed by DW_OP_stack_value, return
28579 copy of the DWARF expression without anything appended.
28580 Otherwise, return NULL. */
28582 static dw_loc_descr_ref
28583 copy_deref_exprloc (dw_loc_descr_ref expr)
28585 dw_loc_descr_ref tail = NULL;
28587 if (expr == NULL)
28588 return NULL;
28590 dw_loc_descr_ref l = non_dwarf_expression (expr);
28591 if (l && l->dw_loc_next)
28592 return NULL;
28594 if (l)
28596 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28597 tail = new_loc_descr ((enum dwarf_location_atom)
28598 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28599 0, 0);
28600 else
28601 switch (l->dw_loc_opc)
28603 case DW_OP_regx:
28604 tail = new_loc_descr (DW_OP_bregx,
28605 l->dw_loc_oprnd1.v.val_unsigned, 0);
28606 break;
28607 case DW_OP_stack_value:
28608 break;
28609 default:
28610 return NULL;
28613 else
28614 tail = new_loc_descr (DW_OP_deref, 0, 0);
28616 dw_loc_descr_ref ret = NULL, *p = &ret;
28617 while (expr != l)
28619 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28620 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28621 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28622 p = &(*p)->dw_loc_next;
28623 expr = expr->dw_loc_next;
28625 *p = tail;
28626 return ret;
28629 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28630 reference to a variable or argument, adjust it if needed and return:
28631 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28632 attribute if present should be removed
28633 0 keep the attribute perhaps with minor modifications, no need to rescan
28634 1 if the attribute has been successfully adjusted. */
28636 static int
28637 optimize_string_length (dw_attr_node *a)
28639 dw_loc_descr_ref l = AT_loc (a), lv;
28640 dw_die_ref die;
28641 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28643 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28644 die = lookup_decl_die (decl);
28645 if (die)
28647 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28648 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28649 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28651 else
28652 return -1;
28654 else
28655 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28657 /* DWARF5 allows reference class, so we can then reference the DIE.
28658 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28659 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28661 a->dw_attr_val.val_class = dw_val_class_die_ref;
28662 a->dw_attr_val.val_entry = NULL;
28663 a->dw_attr_val.v.val_die_ref.die = die;
28664 a->dw_attr_val.v.val_die_ref.external = 0;
28665 return 0;
28668 dw_attr_node *av = get_AT (die, DW_AT_location);
28669 dw_loc_list_ref d;
28670 bool non_dwarf_expr = false;
28672 if (av == NULL)
28673 return dwarf_strict ? -1 : 0;
28674 switch (AT_class (av))
28676 case dw_val_class_loc_list:
28677 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28678 if (d->expr && non_dwarf_expression (d->expr))
28679 non_dwarf_expr = true;
28680 break;
28681 case dw_val_class_loc:
28682 lv = AT_loc (av);
28683 if (lv == NULL)
28684 return dwarf_strict ? -1 : 0;
28685 if (non_dwarf_expression (lv))
28686 non_dwarf_expr = true;
28687 break;
28688 default:
28689 return dwarf_strict ? -1 : 0;
28692 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28693 into DW_OP_call4 or DW_OP_GNU_variable_value into
28694 DW_OP_call4 DW_OP_deref, do so. */
28695 if (!non_dwarf_expr
28696 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28698 l->dw_loc_opc = DW_OP_call4;
28699 if (l->dw_loc_next)
28700 l->dw_loc_next = NULL;
28701 else
28702 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28703 return 0;
28706 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28707 copy over the DW_AT_location attribute from die to a. */
28708 if (l->dw_loc_next != NULL)
28710 a->dw_attr_val = av->dw_attr_val;
28711 return 1;
28714 dw_loc_list_ref list, *p;
28715 switch (AT_class (av))
28717 case dw_val_class_loc_list:
28718 p = &list;
28719 list = NULL;
28720 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28722 lv = copy_deref_exprloc (d->expr);
28723 if (lv)
28725 *p = new_loc_list (lv, d->begin, d->end, d->section);
28726 p = &(*p)->dw_loc_next;
28728 else if (!dwarf_strict && d->expr)
28729 return 0;
28731 if (list == NULL)
28732 return dwarf_strict ? -1 : 0;
28733 a->dw_attr_val.val_class = dw_val_class_loc_list;
28734 gen_llsym (list);
28735 *AT_loc_list_ptr (a) = list;
28736 return 1;
28737 case dw_val_class_loc:
28738 lv = copy_deref_exprloc (AT_loc (av));
28739 if (lv == NULL)
28740 return dwarf_strict ? -1 : 0;
28741 a->dw_attr_val.v.val_loc = lv;
28742 return 1;
28743 default:
28744 gcc_unreachable ();
28748 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28749 an address in .rodata section if the string literal is emitted there,
28750 or remove the containing location list or replace DW_AT_const_value
28751 with DW_AT_location and empty location expression, if it isn't found
28752 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28753 to something that has been emitted in the current CU. */
28755 static void
28756 resolve_addr (dw_die_ref die)
28758 dw_die_ref c;
28759 dw_attr_node *a;
28760 dw_loc_list_ref *curr, *start, loc;
28761 unsigned ix;
28762 bool remove_AT_byte_size = false;
28764 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28765 switch (AT_class (a))
28767 case dw_val_class_loc_list:
28768 start = curr = AT_loc_list_ptr (a);
28769 loc = *curr;
28770 gcc_assert (loc);
28771 /* The same list can be referenced more than once. See if we have
28772 already recorded the result from a previous pass. */
28773 if (loc->replaced)
28774 *curr = loc->dw_loc_next;
28775 else if (!loc->resolved_addr)
28777 /* As things stand, we do not expect or allow one die to
28778 reference a suffix of another die's location list chain.
28779 References must be identical or completely separate.
28780 There is therefore no need to cache the result of this
28781 pass on any list other than the first; doing so
28782 would lead to unnecessary writes. */
28783 while (*curr)
28785 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28786 if (!resolve_addr_in_expr (a, (*curr)->expr))
28788 dw_loc_list_ref next = (*curr)->dw_loc_next;
28789 dw_loc_descr_ref l = (*curr)->expr;
28791 if (next && (*curr)->ll_symbol)
28793 gcc_assert (!next->ll_symbol);
28794 next->ll_symbol = (*curr)->ll_symbol;
28796 if (dwarf_split_debug_info)
28797 remove_loc_list_addr_table_entries (l);
28798 *curr = next;
28800 else
28802 mark_base_types ((*curr)->expr);
28803 curr = &(*curr)->dw_loc_next;
28806 if (loc == *start)
28807 loc->resolved_addr = 1;
28808 else
28810 loc->replaced = 1;
28811 loc->dw_loc_next = *start;
28814 if (!*start)
28816 remove_AT (die, a->dw_attr);
28817 ix--;
28819 break;
28820 case dw_val_class_loc:
28822 dw_loc_descr_ref l = AT_loc (a);
28823 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28824 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28825 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28826 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28827 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28828 with DW_FORM_ref referencing the same DIE as
28829 DW_OP_GNU_variable_value used to reference. */
28830 if (a->dw_attr == DW_AT_string_length
28831 && l
28832 && l->dw_loc_opc == DW_OP_GNU_variable_value
28833 && (l->dw_loc_next == NULL
28834 || (l->dw_loc_next->dw_loc_next == NULL
28835 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28837 switch (optimize_string_length (a))
28839 case -1:
28840 remove_AT (die, a->dw_attr);
28841 ix--;
28842 /* If we drop DW_AT_string_length, we need to drop also
28843 DW_AT_{string_length_,}byte_size. */
28844 remove_AT_byte_size = true;
28845 continue;
28846 default:
28847 break;
28848 case 1:
28849 /* Even if we keep the optimized DW_AT_string_length,
28850 it might have changed AT_class, so process it again. */
28851 ix--;
28852 continue;
28855 /* For -gdwarf-2 don't attempt to optimize
28856 DW_AT_data_member_location containing
28857 DW_OP_plus_uconst - older consumers might
28858 rely on it being that op instead of a more complex,
28859 but shorter, location description. */
28860 if ((dwarf_version > 2
28861 || a->dw_attr != DW_AT_data_member_location
28862 || l == NULL
28863 || l->dw_loc_opc != DW_OP_plus_uconst
28864 || l->dw_loc_next != NULL)
28865 && !resolve_addr_in_expr (a, l))
28867 if (dwarf_split_debug_info)
28868 remove_loc_list_addr_table_entries (l);
28869 if (l != NULL
28870 && l->dw_loc_next == NULL
28871 && l->dw_loc_opc == DW_OP_addr
28872 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28873 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28874 && a->dw_attr == DW_AT_location)
28876 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28877 remove_AT (die, a->dw_attr);
28878 ix--;
28879 optimize_location_into_implicit_ptr (die, decl);
28880 break;
28882 if (a->dw_attr == DW_AT_string_length)
28883 /* If we drop DW_AT_string_length, we need to drop also
28884 DW_AT_{string_length_,}byte_size. */
28885 remove_AT_byte_size = true;
28886 remove_AT (die, a->dw_attr);
28887 ix--;
28889 else
28890 mark_base_types (l);
28892 break;
28893 case dw_val_class_addr:
28894 if (a->dw_attr == DW_AT_const_value
28895 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28897 if (AT_index (a) != NOT_INDEXED)
28898 remove_addr_table_entry (a->dw_attr_val.val_entry);
28899 remove_AT (die, a->dw_attr);
28900 ix--;
28902 if ((die->die_tag == DW_TAG_call_site
28903 && a->dw_attr == DW_AT_call_origin)
28904 || (die->die_tag == DW_TAG_GNU_call_site
28905 && a->dw_attr == DW_AT_abstract_origin))
28907 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28908 dw_die_ref tdie = lookup_decl_die (tdecl);
28909 dw_die_ref cdie;
28910 if (tdie == NULL
28911 && DECL_EXTERNAL (tdecl)
28912 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28913 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28915 dw_die_ref pdie = cdie;
28916 /* Make sure we don't add these DIEs into type units.
28917 We could emit skeleton DIEs for context (namespaces,
28918 outer structs/classes) and a skeleton DIE for the
28919 innermost context with DW_AT_signature pointing to the
28920 type unit. See PR78835. */
28921 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28922 pdie = pdie->die_parent;
28923 if (pdie == NULL)
28925 /* Creating a full DIE for tdecl is overly expensive and
28926 at this point even wrong when in the LTO phase
28927 as it can end up generating new type DIEs we didn't
28928 output and thus optimize_external_refs will crash. */
28929 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28930 add_AT_flag (tdie, DW_AT_external, 1);
28931 add_AT_flag (tdie, DW_AT_declaration, 1);
28932 add_linkage_attr (tdie, tdecl);
28933 add_name_and_src_coords_attributes (tdie, tdecl);
28934 equate_decl_number_to_die (tdecl, tdie);
28937 if (tdie)
28939 a->dw_attr_val.val_class = dw_val_class_die_ref;
28940 a->dw_attr_val.v.val_die_ref.die = tdie;
28941 a->dw_attr_val.v.val_die_ref.external = 0;
28943 else
28945 if (AT_index (a) != NOT_INDEXED)
28946 remove_addr_table_entry (a->dw_attr_val.val_entry);
28947 remove_AT (die, a->dw_attr);
28948 ix--;
28951 break;
28952 default:
28953 break;
28956 if (remove_AT_byte_size)
28957 remove_AT (die, dwarf_version >= 5
28958 ? DW_AT_string_length_byte_size
28959 : DW_AT_byte_size);
28961 FOR_EACH_CHILD (die, c, resolve_addr (c));
28964 /* Helper routines for optimize_location_lists.
28965 This pass tries to share identical local lists in .debug_loc
28966 section. */
28968 /* Iteratively hash operands of LOC opcode into HSTATE. */
28970 static void
28971 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28973 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28974 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28976 switch (loc->dw_loc_opc)
28978 case DW_OP_const4u:
28979 case DW_OP_const8u:
28980 if (loc->dtprel)
28981 goto hash_addr;
28982 /* FALLTHRU */
28983 case DW_OP_const1u:
28984 case DW_OP_const1s:
28985 case DW_OP_const2u:
28986 case DW_OP_const2s:
28987 case DW_OP_const4s:
28988 case DW_OP_const8s:
28989 case DW_OP_constu:
28990 case DW_OP_consts:
28991 case DW_OP_pick:
28992 case DW_OP_plus_uconst:
28993 case DW_OP_breg0:
28994 case DW_OP_breg1:
28995 case DW_OP_breg2:
28996 case DW_OP_breg3:
28997 case DW_OP_breg4:
28998 case DW_OP_breg5:
28999 case DW_OP_breg6:
29000 case DW_OP_breg7:
29001 case DW_OP_breg8:
29002 case DW_OP_breg9:
29003 case DW_OP_breg10:
29004 case DW_OP_breg11:
29005 case DW_OP_breg12:
29006 case DW_OP_breg13:
29007 case DW_OP_breg14:
29008 case DW_OP_breg15:
29009 case DW_OP_breg16:
29010 case DW_OP_breg17:
29011 case DW_OP_breg18:
29012 case DW_OP_breg19:
29013 case DW_OP_breg20:
29014 case DW_OP_breg21:
29015 case DW_OP_breg22:
29016 case DW_OP_breg23:
29017 case DW_OP_breg24:
29018 case DW_OP_breg25:
29019 case DW_OP_breg26:
29020 case DW_OP_breg27:
29021 case DW_OP_breg28:
29022 case DW_OP_breg29:
29023 case DW_OP_breg30:
29024 case DW_OP_breg31:
29025 case DW_OP_regx:
29026 case DW_OP_fbreg:
29027 case DW_OP_piece:
29028 case DW_OP_deref_size:
29029 case DW_OP_xderef_size:
29030 hstate.add_object (val1->v.val_int);
29031 break;
29032 case DW_OP_skip:
29033 case DW_OP_bra:
29035 int offset;
29037 gcc_assert (val1->val_class == dw_val_class_loc);
29038 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29039 hstate.add_object (offset);
29041 break;
29042 case DW_OP_implicit_value:
29043 hstate.add_object (val1->v.val_unsigned);
29044 switch (val2->val_class)
29046 case dw_val_class_const:
29047 hstate.add_object (val2->v.val_int);
29048 break;
29049 case dw_val_class_vec:
29051 unsigned int elt_size = val2->v.val_vec.elt_size;
29052 unsigned int len = val2->v.val_vec.length;
29054 hstate.add_int (elt_size);
29055 hstate.add_int (len);
29056 hstate.add (val2->v.val_vec.array, len * elt_size);
29058 break;
29059 case dw_val_class_const_double:
29060 hstate.add_object (val2->v.val_double.low);
29061 hstate.add_object (val2->v.val_double.high);
29062 break;
29063 case dw_val_class_wide_int:
29064 hstate.add (val2->v.val_wide->get_val (),
29065 get_full_len (*val2->v.val_wide)
29066 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29067 break;
29068 case dw_val_class_addr:
29069 inchash::add_rtx (val2->v.val_addr, hstate);
29070 break;
29071 default:
29072 gcc_unreachable ();
29074 break;
29075 case DW_OP_bregx:
29076 case DW_OP_bit_piece:
29077 hstate.add_object (val1->v.val_int);
29078 hstate.add_object (val2->v.val_int);
29079 break;
29080 case DW_OP_addr:
29081 hash_addr:
29082 if (loc->dtprel)
29084 unsigned char dtprel = 0xd1;
29085 hstate.add_object (dtprel);
29087 inchash::add_rtx (val1->v.val_addr, hstate);
29088 break;
29089 case DW_OP_GNU_addr_index:
29090 case DW_OP_GNU_const_index:
29092 if (loc->dtprel)
29094 unsigned char dtprel = 0xd1;
29095 hstate.add_object (dtprel);
29097 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29099 break;
29100 case DW_OP_implicit_pointer:
29101 case DW_OP_GNU_implicit_pointer:
29102 hstate.add_int (val2->v.val_int);
29103 break;
29104 case DW_OP_entry_value:
29105 case DW_OP_GNU_entry_value:
29106 hstate.add_object (val1->v.val_loc);
29107 break;
29108 case DW_OP_regval_type:
29109 case DW_OP_deref_type:
29110 case DW_OP_GNU_regval_type:
29111 case DW_OP_GNU_deref_type:
29113 unsigned int byte_size
29114 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29115 unsigned int encoding
29116 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29117 hstate.add_object (val1->v.val_int);
29118 hstate.add_object (byte_size);
29119 hstate.add_object (encoding);
29121 break;
29122 case DW_OP_convert:
29123 case DW_OP_reinterpret:
29124 case DW_OP_GNU_convert:
29125 case DW_OP_GNU_reinterpret:
29126 if (val1->val_class == dw_val_class_unsigned_const)
29128 hstate.add_object (val1->v.val_unsigned);
29129 break;
29131 /* FALLTHRU */
29132 case DW_OP_const_type:
29133 case DW_OP_GNU_const_type:
29135 unsigned int byte_size
29136 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29137 unsigned int encoding
29138 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29139 hstate.add_object (byte_size);
29140 hstate.add_object (encoding);
29141 if (loc->dw_loc_opc != DW_OP_const_type
29142 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29143 break;
29144 hstate.add_object (val2->val_class);
29145 switch (val2->val_class)
29147 case dw_val_class_const:
29148 hstate.add_object (val2->v.val_int);
29149 break;
29150 case dw_val_class_vec:
29152 unsigned int elt_size = val2->v.val_vec.elt_size;
29153 unsigned int len = val2->v.val_vec.length;
29155 hstate.add_object (elt_size);
29156 hstate.add_object (len);
29157 hstate.add (val2->v.val_vec.array, len * elt_size);
29159 break;
29160 case dw_val_class_const_double:
29161 hstate.add_object (val2->v.val_double.low);
29162 hstate.add_object (val2->v.val_double.high);
29163 break;
29164 case dw_val_class_wide_int:
29165 hstate.add (val2->v.val_wide->get_val (),
29166 get_full_len (*val2->v.val_wide)
29167 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29168 break;
29169 default:
29170 gcc_unreachable ();
29173 break;
29175 default:
29176 /* Other codes have no operands. */
29177 break;
29181 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29183 static inline void
29184 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29186 dw_loc_descr_ref l;
29187 bool sizes_computed = false;
29188 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29189 size_of_locs (loc);
29191 for (l = loc; l != NULL; l = l->dw_loc_next)
29193 enum dwarf_location_atom opc = l->dw_loc_opc;
29194 hstate.add_object (opc);
29195 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29197 size_of_locs (loc);
29198 sizes_computed = true;
29200 hash_loc_operands (l, hstate);
29204 /* Compute hash of the whole location list LIST_HEAD. */
29206 static inline void
29207 hash_loc_list (dw_loc_list_ref list_head)
29209 dw_loc_list_ref curr = list_head;
29210 inchash::hash hstate;
29212 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29214 hstate.add (curr->begin, strlen (curr->begin) + 1);
29215 hstate.add (curr->end, strlen (curr->end) + 1);
29216 if (curr->section)
29217 hstate.add (curr->section, strlen (curr->section) + 1);
29218 hash_locs (curr->expr, hstate);
29220 list_head->hash = hstate.end ();
29223 /* Return true if X and Y opcodes have the same operands. */
29225 static inline bool
29226 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29228 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29229 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29230 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29231 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29233 switch (x->dw_loc_opc)
29235 case DW_OP_const4u:
29236 case DW_OP_const8u:
29237 if (x->dtprel)
29238 goto hash_addr;
29239 /* FALLTHRU */
29240 case DW_OP_const1u:
29241 case DW_OP_const1s:
29242 case DW_OP_const2u:
29243 case DW_OP_const2s:
29244 case DW_OP_const4s:
29245 case DW_OP_const8s:
29246 case DW_OP_constu:
29247 case DW_OP_consts:
29248 case DW_OP_pick:
29249 case DW_OP_plus_uconst:
29250 case DW_OP_breg0:
29251 case DW_OP_breg1:
29252 case DW_OP_breg2:
29253 case DW_OP_breg3:
29254 case DW_OP_breg4:
29255 case DW_OP_breg5:
29256 case DW_OP_breg6:
29257 case DW_OP_breg7:
29258 case DW_OP_breg8:
29259 case DW_OP_breg9:
29260 case DW_OP_breg10:
29261 case DW_OP_breg11:
29262 case DW_OP_breg12:
29263 case DW_OP_breg13:
29264 case DW_OP_breg14:
29265 case DW_OP_breg15:
29266 case DW_OP_breg16:
29267 case DW_OP_breg17:
29268 case DW_OP_breg18:
29269 case DW_OP_breg19:
29270 case DW_OP_breg20:
29271 case DW_OP_breg21:
29272 case DW_OP_breg22:
29273 case DW_OP_breg23:
29274 case DW_OP_breg24:
29275 case DW_OP_breg25:
29276 case DW_OP_breg26:
29277 case DW_OP_breg27:
29278 case DW_OP_breg28:
29279 case DW_OP_breg29:
29280 case DW_OP_breg30:
29281 case DW_OP_breg31:
29282 case DW_OP_regx:
29283 case DW_OP_fbreg:
29284 case DW_OP_piece:
29285 case DW_OP_deref_size:
29286 case DW_OP_xderef_size:
29287 return valx1->v.val_int == valy1->v.val_int;
29288 case DW_OP_skip:
29289 case DW_OP_bra:
29290 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29291 can cause irrelevant differences in dw_loc_addr. */
29292 gcc_assert (valx1->val_class == dw_val_class_loc
29293 && valy1->val_class == dw_val_class_loc
29294 && (dwarf_split_debug_info
29295 || x->dw_loc_addr == y->dw_loc_addr));
29296 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29297 case DW_OP_implicit_value:
29298 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29299 || valx2->val_class != valy2->val_class)
29300 return false;
29301 switch (valx2->val_class)
29303 case dw_val_class_const:
29304 return valx2->v.val_int == valy2->v.val_int;
29305 case dw_val_class_vec:
29306 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29307 && valx2->v.val_vec.length == valy2->v.val_vec.length
29308 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29309 valx2->v.val_vec.elt_size
29310 * valx2->v.val_vec.length) == 0;
29311 case dw_val_class_const_double:
29312 return valx2->v.val_double.low == valy2->v.val_double.low
29313 && valx2->v.val_double.high == valy2->v.val_double.high;
29314 case dw_val_class_wide_int:
29315 return *valx2->v.val_wide == *valy2->v.val_wide;
29316 case dw_val_class_addr:
29317 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29318 default:
29319 gcc_unreachable ();
29321 case DW_OP_bregx:
29322 case DW_OP_bit_piece:
29323 return valx1->v.val_int == valy1->v.val_int
29324 && valx2->v.val_int == valy2->v.val_int;
29325 case DW_OP_addr:
29326 hash_addr:
29327 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29328 case DW_OP_GNU_addr_index:
29329 case DW_OP_GNU_const_index:
29331 rtx ax1 = valx1->val_entry->addr.rtl;
29332 rtx ay1 = valy1->val_entry->addr.rtl;
29333 return rtx_equal_p (ax1, ay1);
29335 case DW_OP_implicit_pointer:
29336 case DW_OP_GNU_implicit_pointer:
29337 return valx1->val_class == dw_val_class_die_ref
29338 && valx1->val_class == valy1->val_class
29339 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29340 && valx2->v.val_int == valy2->v.val_int;
29341 case DW_OP_entry_value:
29342 case DW_OP_GNU_entry_value:
29343 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29344 case DW_OP_const_type:
29345 case DW_OP_GNU_const_type:
29346 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29347 || valx2->val_class != valy2->val_class)
29348 return false;
29349 switch (valx2->val_class)
29351 case dw_val_class_const:
29352 return valx2->v.val_int == valy2->v.val_int;
29353 case dw_val_class_vec:
29354 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29355 && valx2->v.val_vec.length == valy2->v.val_vec.length
29356 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29357 valx2->v.val_vec.elt_size
29358 * valx2->v.val_vec.length) == 0;
29359 case dw_val_class_const_double:
29360 return valx2->v.val_double.low == valy2->v.val_double.low
29361 && valx2->v.val_double.high == valy2->v.val_double.high;
29362 case dw_val_class_wide_int:
29363 return *valx2->v.val_wide == *valy2->v.val_wide;
29364 default:
29365 gcc_unreachable ();
29367 case DW_OP_regval_type:
29368 case DW_OP_deref_type:
29369 case DW_OP_GNU_regval_type:
29370 case DW_OP_GNU_deref_type:
29371 return valx1->v.val_int == valy1->v.val_int
29372 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29373 case DW_OP_convert:
29374 case DW_OP_reinterpret:
29375 case DW_OP_GNU_convert:
29376 case DW_OP_GNU_reinterpret:
29377 if (valx1->val_class != valy1->val_class)
29378 return false;
29379 if (valx1->val_class == dw_val_class_unsigned_const)
29380 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29381 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29382 case DW_OP_GNU_parameter_ref:
29383 return valx1->val_class == dw_val_class_die_ref
29384 && valx1->val_class == valy1->val_class
29385 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29386 default:
29387 /* Other codes have no operands. */
29388 return true;
29392 /* Return true if DWARF location expressions X and Y are the same. */
29394 static inline bool
29395 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29397 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29398 if (x->dw_loc_opc != y->dw_loc_opc
29399 || x->dtprel != y->dtprel
29400 || !compare_loc_operands (x, y))
29401 break;
29402 return x == NULL && y == NULL;
29405 /* Hashtable helpers. */
29407 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29409 static inline hashval_t hash (const dw_loc_list_struct *);
29410 static inline bool equal (const dw_loc_list_struct *,
29411 const dw_loc_list_struct *);
29414 /* Return precomputed hash of location list X. */
29416 inline hashval_t
29417 loc_list_hasher::hash (const dw_loc_list_struct *x)
29419 return x->hash;
29422 /* Return true if location lists A and B are the same. */
29424 inline bool
29425 loc_list_hasher::equal (const dw_loc_list_struct *a,
29426 const dw_loc_list_struct *b)
29428 if (a == b)
29429 return 1;
29430 if (a->hash != b->hash)
29431 return 0;
29432 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29433 if (strcmp (a->begin, b->begin) != 0
29434 || strcmp (a->end, b->end) != 0
29435 || (a->section == NULL) != (b->section == NULL)
29436 || (a->section && strcmp (a->section, b->section) != 0)
29437 || !compare_locs (a->expr, b->expr))
29438 break;
29439 return a == NULL && b == NULL;
29442 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29445 /* Recursively optimize location lists referenced from DIE
29446 children and share them whenever possible. */
29448 static void
29449 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29451 dw_die_ref c;
29452 dw_attr_node *a;
29453 unsigned ix;
29454 dw_loc_list_struct **slot;
29456 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29457 if (AT_class (a) == dw_val_class_loc_list)
29459 dw_loc_list_ref list = AT_loc_list (a);
29460 /* TODO: perform some optimizations here, before hashing
29461 it and storing into the hash table. */
29462 hash_loc_list (list);
29463 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29464 if (*slot == NULL)
29465 *slot = list;
29466 else
29467 a->dw_attr_val.v.val_loc_list = *slot;
29470 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29474 /* Recursively assign each location list a unique index into the debug_addr
29475 section. */
29477 static void
29478 index_location_lists (dw_die_ref die)
29480 dw_die_ref c;
29481 dw_attr_node *a;
29482 unsigned ix;
29484 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29485 if (AT_class (a) == dw_val_class_loc_list)
29487 dw_loc_list_ref list = AT_loc_list (a);
29488 dw_loc_list_ref curr;
29489 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29491 /* Don't index an entry that has already been indexed
29492 or won't be output. */
29493 if (curr->begin_entry != NULL
29494 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29495 continue;
29497 curr->begin_entry
29498 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29502 FOR_EACH_CHILD (die, c, index_location_lists (c));
29505 /* Optimize location lists referenced from DIE
29506 children and share them whenever possible. */
29508 static void
29509 optimize_location_lists (dw_die_ref die)
29511 loc_list_hash_type htab (500);
29512 optimize_location_lists_1 (die, &htab);
29515 /* Traverse the limbo die list, and add parent/child links. The only
29516 dies without parents that should be here are concrete instances of
29517 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29518 For concrete instances, we can get the parent die from the abstract
29519 instance. */
29521 static void
29522 flush_limbo_die_list (void)
29524 limbo_die_node *node;
29526 /* get_context_die calls force_decl_die, which can put new DIEs on the
29527 limbo list in LTO mode when nested functions are put in a different
29528 partition than that of their parent function. */
29529 while ((node = limbo_die_list))
29531 dw_die_ref die = node->die;
29532 limbo_die_list = node->next;
29534 if (die->die_parent == NULL)
29536 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29538 if (origin && origin->die_parent)
29539 add_child_die (origin->die_parent, die);
29540 else if (is_cu_die (die))
29542 else if (seen_error ())
29543 /* It's OK to be confused by errors in the input. */
29544 add_child_die (comp_unit_die (), die);
29545 else
29547 /* In certain situations, the lexical block containing a
29548 nested function can be optimized away, which results
29549 in the nested function die being orphaned. Likewise
29550 with the return type of that nested function. Force
29551 this to be a child of the containing function.
29553 It may happen that even the containing function got fully
29554 inlined and optimized out. In that case we are lost and
29555 assign the empty child. This should not be big issue as
29556 the function is likely unreachable too. */
29557 gcc_assert (node->created_for);
29559 if (DECL_P (node->created_for))
29560 origin = get_context_die (DECL_CONTEXT (node->created_for));
29561 else if (TYPE_P (node->created_for))
29562 origin = scope_die_for (node->created_for, comp_unit_die ());
29563 else
29564 origin = comp_unit_die ();
29566 add_child_die (origin, die);
29572 /* Output stuff that dwarf requires at the end of every file,
29573 and generate the DWARF-2 debugging info. */
29575 static void
29576 dwarf2out_finish (const char *)
29578 comdat_type_node *ctnode;
29579 dw_die_ref main_comp_unit_die;
29580 unsigned char checksum[16];
29581 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29583 /* Flush out any latecomers to the limbo party. */
29584 flush_limbo_die_list ();
29586 if (flag_checking)
29588 verify_die (comp_unit_die ());
29589 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29590 verify_die (node->die);
29593 /* We shouldn't have any symbols with delayed asm names for
29594 DIEs generated after early finish. */
29595 gcc_assert (deferred_asm_name == NULL);
29597 gen_remaining_tmpl_value_param_die_attribute ();
29599 #if ENABLE_ASSERT_CHECKING
29601 dw_die_ref die = comp_unit_die (), c;
29602 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29604 #endif
29605 resolve_addr (comp_unit_die ());
29606 move_marked_base_types ();
29608 /* Initialize sections and labels used for actual assembler output. */
29609 init_sections_and_labels ();
29611 /* Traverse the DIE's and add sibling attributes to those DIE's that
29612 have children. */
29613 add_sibling_attributes (comp_unit_die ());
29614 limbo_die_node *node;
29615 for (node = cu_die_list; node; node = node->next)
29616 add_sibling_attributes (node->die);
29617 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29618 add_sibling_attributes (ctnode->root_die);
29620 /* When splitting DWARF info, we put some attributes in the
29621 skeleton compile_unit DIE that remains in the .o, while
29622 most attributes go in the DWO compile_unit_die. */
29623 if (dwarf_split_debug_info)
29625 limbo_die_node *cu;
29626 main_comp_unit_die = gen_compile_unit_die (NULL);
29627 if (dwarf_version >= 5)
29628 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29629 cu = limbo_die_list;
29630 gcc_assert (cu->die == main_comp_unit_die);
29631 limbo_die_list = limbo_die_list->next;
29632 cu->next = cu_die_list;
29633 cu_die_list = cu;
29635 else
29636 main_comp_unit_die = comp_unit_die ();
29638 /* Output a terminator label for the .text section. */
29639 switch_to_section (text_section);
29640 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29641 if (cold_text_section)
29643 switch_to_section (cold_text_section);
29644 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29647 /* We can only use the low/high_pc attributes if all of the code was
29648 in .text. */
29649 if (!have_multiple_function_sections
29650 || (dwarf_version < 3 && dwarf_strict))
29652 /* Don't add if the CU has no associated code. */
29653 if (text_section_used)
29654 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29655 text_end_label, true);
29657 else
29659 unsigned fde_idx;
29660 dw_fde_ref fde;
29661 bool range_list_added = false;
29663 if (text_section_used)
29664 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29665 text_end_label, &range_list_added, true);
29666 if (cold_text_section_used)
29667 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29668 cold_end_label, &range_list_added, true);
29670 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29672 if (DECL_IGNORED_P (fde->decl))
29673 continue;
29674 if (!fde->in_std_section)
29675 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29676 fde->dw_fde_end, &range_list_added,
29677 true);
29678 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29679 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29680 fde->dw_fde_second_end, &range_list_added,
29681 true);
29684 if (range_list_added)
29686 /* We need to give .debug_loc and .debug_ranges an appropriate
29687 "base address". Use zero so that these addresses become
29688 absolute. Historically, we've emitted the unexpected
29689 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29690 Emit both to give time for other tools to adapt. */
29691 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29692 if (! dwarf_strict && dwarf_version < 4)
29693 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29695 add_ranges (NULL);
29699 /* AIX Assembler inserts the length, so adjust the reference to match the
29700 offset expected by debuggers. */
29701 strcpy (dl_section_ref, debug_line_section_label);
29702 if (XCOFF_DEBUGGING_INFO)
29703 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29705 if (debug_info_level >= DINFO_LEVEL_TERSE)
29706 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29707 dl_section_ref);
29709 if (have_macinfo)
29710 add_AT_macptr (comp_unit_die (),
29711 dwarf_version >= 5 ? DW_AT_macros
29712 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29713 macinfo_section_label);
29715 if (dwarf_split_debug_info)
29717 if (have_location_lists)
29719 if (dwarf_version >= 5)
29720 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29721 loc_section_label);
29722 /* optimize_location_lists calculates the size of the lists,
29723 so index them first, and assign indices to the entries.
29724 Although optimize_location_lists will remove entries from
29725 the table, it only does so for duplicates, and therefore
29726 only reduces ref_counts to 1. */
29727 index_location_lists (comp_unit_die ());
29730 if (addr_index_table != NULL)
29732 unsigned int index = 0;
29733 addr_index_table
29734 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29735 (&index);
29739 loc_list_idx = 0;
29740 if (have_location_lists)
29742 optimize_location_lists (comp_unit_die ());
29743 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29744 if (dwarf_version >= 5 && dwarf_split_debug_info)
29745 assign_location_list_indexes (comp_unit_die ());
29748 save_macinfo_strings ();
29750 if (dwarf_split_debug_info)
29752 unsigned int index = 0;
29754 /* Add attributes common to skeleton compile_units and
29755 type_units. Because these attributes include strings, it
29756 must be done before freezing the string table. Top-level
29757 skeleton die attrs are added when the skeleton type unit is
29758 created, so ensure it is created by this point. */
29759 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29760 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29763 /* Output all of the compilation units. We put the main one last so that
29764 the offsets are available to output_pubnames. */
29765 for (node = cu_die_list; node; node = node->next)
29766 output_comp_unit (node->die, 0, NULL);
29768 hash_table<comdat_type_hasher> comdat_type_table (100);
29769 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29771 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29773 /* Don't output duplicate types. */
29774 if (*slot != HTAB_EMPTY_ENTRY)
29775 continue;
29777 /* Add a pointer to the line table for the main compilation unit
29778 so that the debugger can make sense of DW_AT_decl_file
29779 attributes. */
29780 if (debug_info_level >= DINFO_LEVEL_TERSE)
29781 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29782 (!dwarf_split_debug_info
29783 ? dl_section_ref
29784 : debug_skeleton_line_section_label));
29786 output_comdat_type_unit (ctnode);
29787 *slot = ctnode;
29790 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29791 both the main_cu and all skeleton TUs. Making this call unconditional
29792 would end up either adding a second copy of the AT_pubnames attribute, or
29793 requiring a special case in add_top_level_skeleton_die_attrs. */
29794 if (!dwarf_split_debug_info)
29795 add_AT_pubnames (comp_unit_die ());
29797 if (dwarf_split_debug_info)
29799 int mark;
29800 struct md5_ctx ctx;
29802 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29803 index_rnglists ();
29805 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29806 md5_init_ctx (&ctx);
29807 mark = 0;
29808 die_checksum (comp_unit_die (), &ctx, &mark);
29809 unmark_all_dies (comp_unit_die ());
29810 md5_finish_ctx (&ctx, checksum);
29812 if (dwarf_version < 5)
29814 /* Use the first 8 bytes of the checksum as the dwo_id,
29815 and add it to both comp-unit DIEs. */
29816 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29817 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29820 /* Add the base offset of the ranges table to the skeleton
29821 comp-unit DIE. */
29822 if (!vec_safe_is_empty (ranges_table))
29824 if (dwarf_version >= 5)
29825 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29826 ranges_base_label);
29827 else
29828 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29829 ranges_section_label);
29832 switch_to_section (debug_addr_section);
29833 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29834 output_addr_table ();
29837 /* Output the main compilation unit if non-empty or if .debug_macinfo
29838 or .debug_macro will be emitted. */
29839 output_comp_unit (comp_unit_die (), have_macinfo,
29840 dwarf_split_debug_info ? checksum : NULL);
29842 if (dwarf_split_debug_info && info_section_emitted)
29843 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29845 /* Output the abbreviation table. */
29846 if (vec_safe_length (abbrev_die_table) != 1)
29848 switch_to_section (debug_abbrev_section);
29849 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29850 output_abbrev_section ();
29853 /* Output location list section if necessary. */
29854 if (have_location_lists)
29856 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29857 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29858 /* Output the location lists info. */
29859 switch_to_section (debug_loc_section);
29860 if (dwarf_version >= 5)
29862 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29863 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29864 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29865 dw2_asm_output_data (4, 0xffffffff,
29866 "Initial length escape value indicating "
29867 "64-bit DWARF extension");
29868 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29869 "Length of Location Lists");
29870 ASM_OUTPUT_LABEL (asm_out_file, l1);
29871 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29872 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29873 dw2_asm_output_data (1, 0, "Segment Size");
29874 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29875 "Offset Entry Count");
29877 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29878 if (dwarf_version >= 5 && dwarf_split_debug_info)
29880 unsigned int save_loc_list_idx = loc_list_idx;
29881 loc_list_idx = 0;
29882 output_loclists_offsets (comp_unit_die ());
29883 gcc_assert (save_loc_list_idx == loc_list_idx);
29885 output_location_lists (comp_unit_die ());
29886 if (dwarf_version >= 5)
29887 ASM_OUTPUT_LABEL (asm_out_file, l2);
29890 output_pubtables ();
29892 /* Output the address range information if a CU (.debug_info section)
29893 was emitted. We output an empty table even if we had no functions
29894 to put in it. This because the consumer has no way to tell the
29895 difference between an empty table that we omitted and failure to
29896 generate a table that would have contained data. */
29897 if (info_section_emitted)
29899 switch_to_section (debug_aranges_section);
29900 output_aranges ();
29903 /* Output ranges section if necessary. */
29904 if (!vec_safe_is_empty (ranges_table))
29906 if (dwarf_version >= 5)
29907 output_rnglists ();
29908 else
29909 output_ranges ();
29912 /* Have to end the macro section. */
29913 if (have_macinfo)
29915 switch_to_section (debug_macinfo_section);
29916 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29917 output_macinfo ();
29918 dw2_asm_output_data (1, 0, "End compilation unit");
29921 /* Output the source line correspondence table. We must do this
29922 even if there is no line information. Otherwise, on an empty
29923 translation unit, we will generate a present, but empty,
29924 .debug_info section. IRIX 6.5 `nm' will then complain when
29925 examining the file. This is done late so that any filenames
29926 used by the debug_info section are marked as 'used'. */
29927 switch_to_section (debug_line_section);
29928 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29929 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29930 output_line_info (false);
29932 if (dwarf_split_debug_info && info_section_emitted)
29934 switch_to_section (debug_skeleton_line_section);
29935 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29936 output_line_info (true);
29939 /* If we emitted any indirect strings, output the string table too. */
29940 if (debug_str_hash || skeleton_debug_str_hash)
29941 output_indirect_strings ();
29942 if (debug_line_str_hash)
29944 switch_to_section (debug_line_str_section);
29945 const enum dwarf_form form = DW_FORM_line_strp;
29946 debug_line_str_hash->traverse<enum dwarf_form,
29947 output_indirect_string> (form);
29951 /* Returns a hash value for X (which really is a variable_value_struct). */
29953 inline hashval_t
29954 variable_value_hasher::hash (variable_value_struct *x)
29956 return (hashval_t) x->decl_id;
29959 /* Return nonzero if decl_id of variable_value_struct X is the same as
29960 UID of decl Y. */
29962 inline bool
29963 variable_value_hasher::equal (variable_value_struct *x, tree y)
29965 return x->decl_id == DECL_UID (y);
29968 /* Helper function for resolve_variable_value, handle
29969 DW_OP_GNU_variable_value in one location expression.
29970 Return true if exprloc has been changed into loclist. */
29972 static bool
29973 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29975 dw_loc_descr_ref next;
29976 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
29978 next = loc->dw_loc_next;
29979 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
29980 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
29981 continue;
29983 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
29984 if (DECL_CONTEXT (decl) != current_function_decl)
29985 continue;
29987 dw_die_ref ref = lookup_decl_die (decl);
29988 if (ref)
29990 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29991 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29992 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29993 continue;
29995 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
29996 if (l == NULL)
29997 continue;
29998 if (l->dw_loc_next)
30000 if (AT_class (a) != dw_val_class_loc)
30001 continue;
30002 switch (a->dw_attr)
30004 /* Following attributes allow both exprloc and loclist
30005 classes, so we can change them into a loclist. */
30006 case DW_AT_location:
30007 case DW_AT_string_length:
30008 case DW_AT_return_addr:
30009 case DW_AT_data_member_location:
30010 case DW_AT_frame_base:
30011 case DW_AT_segment:
30012 case DW_AT_static_link:
30013 case DW_AT_use_location:
30014 case DW_AT_vtable_elem_location:
30015 if (prev)
30017 prev->dw_loc_next = NULL;
30018 prepend_loc_descr_to_each (l, AT_loc (a));
30020 if (next)
30021 add_loc_descr_to_each (l, next);
30022 a->dw_attr_val.val_class = dw_val_class_loc_list;
30023 a->dw_attr_val.val_entry = NULL;
30024 a->dw_attr_val.v.val_loc_list = l;
30025 have_location_lists = true;
30026 return true;
30027 /* Following attributes allow both exprloc and reference,
30028 so if the whole expression is DW_OP_GNU_variable_value alone
30029 we could transform it into reference. */
30030 case DW_AT_byte_size:
30031 case DW_AT_bit_size:
30032 case DW_AT_lower_bound:
30033 case DW_AT_upper_bound:
30034 case DW_AT_bit_stride:
30035 case DW_AT_count:
30036 case DW_AT_allocated:
30037 case DW_AT_associated:
30038 case DW_AT_byte_stride:
30039 if (prev == NULL && next == NULL)
30040 break;
30041 /* FALLTHRU */
30042 default:
30043 if (dwarf_strict)
30044 continue;
30045 break;
30047 /* Create DW_TAG_variable that we can refer to. */
30048 gen_decl_die (decl, NULL_TREE, NULL,
30049 lookup_decl_die (current_function_decl));
30050 ref = lookup_decl_die (decl);
30051 if (ref)
30053 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30054 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30055 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30057 continue;
30059 if (prev)
30061 prev->dw_loc_next = l->expr;
30062 add_loc_descr (&prev->dw_loc_next, next);
30063 free_loc_descr (loc, NULL);
30064 next = prev->dw_loc_next;
30066 else
30068 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30069 add_loc_descr (&loc, next);
30070 next = loc;
30072 loc = prev;
30074 return false;
30077 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30079 static void
30080 resolve_variable_value (dw_die_ref die)
30082 dw_attr_node *a;
30083 dw_loc_list_ref loc;
30084 unsigned ix;
30086 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30087 switch (AT_class (a))
30089 case dw_val_class_loc:
30090 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30091 break;
30092 /* FALLTHRU */
30093 case dw_val_class_loc_list:
30094 loc = AT_loc_list (a);
30095 gcc_assert (loc);
30096 for (; loc; loc = loc->dw_loc_next)
30097 resolve_variable_value_in_expr (a, loc->expr);
30098 break;
30099 default:
30100 break;
30104 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30105 temporaries in the current function. */
30107 static void
30108 resolve_variable_values (void)
30110 if (!variable_value_hash || !current_function_decl)
30111 return;
30113 struct variable_value_struct *node
30114 = variable_value_hash->find_with_hash (current_function_decl,
30115 DECL_UID (current_function_decl));
30117 if (node == NULL)
30118 return;
30120 unsigned int i;
30121 dw_die_ref die;
30122 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30123 resolve_variable_value (die);
30126 /* Helper function for note_variable_value, handle one location
30127 expression. */
30129 static void
30130 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30132 for (; loc; loc = loc->dw_loc_next)
30133 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30134 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30136 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30137 dw_die_ref ref = lookup_decl_die (decl);
30138 if (ref)
30140 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30141 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30142 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30143 continue;
30145 if (VAR_P (decl)
30146 && DECL_CONTEXT (decl)
30147 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30148 && lookup_decl_die (DECL_CONTEXT (decl)))
30150 if (!variable_value_hash)
30151 variable_value_hash
30152 = hash_table<variable_value_hasher>::create_ggc (10);
30154 tree fndecl = DECL_CONTEXT (decl);
30155 struct variable_value_struct *node;
30156 struct variable_value_struct **slot
30157 = variable_value_hash->find_slot_with_hash (fndecl,
30158 DECL_UID (fndecl),
30159 INSERT);
30160 if (*slot == NULL)
30162 node = ggc_cleared_alloc<variable_value_struct> ();
30163 node->decl_id = DECL_UID (fndecl);
30164 *slot = node;
30166 else
30167 node = *slot;
30169 vec_safe_push (node->dies, die);
30174 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30175 with dw_val_class_decl_ref operand. */
30177 static void
30178 note_variable_value (dw_die_ref die)
30180 dw_die_ref c;
30181 dw_attr_node *a;
30182 dw_loc_list_ref loc;
30183 unsigned ix;
30185 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30186 switch (AT_class (a))
30188 case dw_val_class_loc_list:
30189 loc = AT_loc_list (a);
30190 gcc_assert (loc);
30191 if (!loc->noted_variable_value)
30193 loc->noted_variable_value = 1;
30194 for (; loc; loc = loc->dw_loc_next)
30195 note_variable_value_in_expr (die, loc->expr);
30197 break;
30198 case dw_val_class_loc:
30199 note_variable_value_in_expr (die, AT_loc (a));
30200 break;
30201 default:
30202 break;
30205 /* Mark children. */
30206 FOR_EACH_CHILD (die, c, note_variable_value (c));
30209 /* Perform any cleanups needed after the early debug generation pass
30210 has run. */
30212 static void
30213 dwarf2out_early_finish (const char *filename)
30215 set_early_dwarf s;
30217 /* PCH might result in DW_AT_producer string being restored from the
30218 header compilation, so always fill it with empty string initially
30219 and overwrite only here. */
30220 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30221 producer_string = gen_producer_string ();
30222 producer->dw_attr_val.v.val_str->refcount--;
30223 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30225 /* Add the name for the main input file now. We delayed this from
30226 dwarf2out_init to avoid complications with PCH. */
30227 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30228 add_comp_dir_attribute (comp_unit_die ());
30230 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30231 DW_AT_comp_dir into .debug_line_str section. */
30232 if (!DWARF2_ASM_LINE_DEBUG_INFO
30233 && dwarf_version >= 5
30234 && DWARF5_USE_DEBUG_LINE_STR)
30236 for (int i = 0; i < 2; i++)
30238 dw_attr_node *a = get_AT (comp_unit_die (),
30239 i ? DW_AT_comp_dir : DW_AT_name);
30240 if (a == NULL
30241 || AT_class (a) != dw_val_class_str
30242 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30243 continue;
30245 if (! debug_line_str_hash)
30246 debug_line_str_hash
30247 = hash_table<indirect_string_hasher>::create_ggc (10);
30249 struct indirect_string_node *node
30250 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30251 set_indirect_string (node);
30252 node->form = DW_FORM_line_strp;
30253 a->dw_attr_val.v.val_str->refcount--;
30254 a->dw_attr_val.v.val_str = node;
30258 /* With LTO early dwarf was really finished at compile-time, so make
30259 sure to adjust the phase after annotating the LTRANS CU DIE. */
30260 if (in_lto_p)
30262 early_dwarf_finished = true;
30263 return;
30266 /* Walk through the list of incomplete types again, trying once more to
30267 emit full debugging info for them. */
30268 retry_incomplete_types ();
30270 /* The point here is to flush out the limbo list so that it is empty
30271 and we don't need to stream it for LTO. */
30272 flush_limbo_die_list ();
30274 gen_scheduled_generic_parms_dies ();
30275 gen_remaining_tmpl_value_param_die_attribute ();
30277 /* Add DW_AT_linkage_name for all deferred DIEs. */
30278 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30280 tree decl = node->created_for;
30281 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30282 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30283 ended up in deferred_asm_name before we knew it was
30284 constant and never written to disk. */
30285 && DECL_ASSEMBLER_NAME (decl))
30287 add_linkage_attr (node->die, decl);
30288 move_linkage_attr (node->die);
30291 deferred_asm_name = NULL;
30293 if (flag_eliminate_unused_debug_types)
30294 prune_unused_types ();
30296 /* Generate separate COMDAT sections for type DIEs. */
30297 if (use_debug_types)
30299 break_out_comdat_types (comp_unit_die ());
30301 /* Each new type_unit DIE was added to the limbo die list when created.
30302 Since these have all been added to comdat_type_list, clear the
30303 limbo die list. */
30304 limbo_die_list = NULL;
30306 /* For each new comdat type unit, copy declarations for incomplete
30307 types to make the new unit self-contained (i.e., no direct
30308 references to the main compile unit). */
30309 for (comdat_type_node *ctnode = comdat_type_list;
30310 ctnode != NULL; ctnode = ctnode->next)
30311 copy_decls_for_unworthy_types (ctnode->root_die);
30312 copy_decls_for_unworthy_types (comp_unit_die ());
30314 /* In the process of copying declarations from one unit to another,
30315 we may have left some declarations behind that are no longer
30316 referenced. Prune them. */
30317 prune_unused_types ();
30320 /* Generate separate CUs for each of the include files we've seen.
30321 They will go into limbo_die_list and from there to cu_die_list. */
30322 if (flag_eliminate_dwarf2_dups)
30324 gcc_assert (limbo_die_list == NULL);
30325 break_out_includes (comp_unit_die ());
30326 limbo_die_node *cu;
30327 while ((cu = limbo_die_list))
30329 limbo_die_list = cu->next;
30330 cu->next = cu_die_list;
30331 cu_die_list = cu;
30335 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30336 with dw_val_class_decl_ref operand. */
30337 note_variable_value (comp_unit_die ());
30338 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30339 note_variable_value (node->die);
30340 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30341 ctnode = ctnode->next)
30342 note_variable_value (ctnode->root_die);
30343 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30344 note_variable_value (node->die);
30346 /* The early debug phase is now finished. */
30347 early_dwarf_finished = true;
30350 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30351 within the same process. For use by toplev::finalize. */
30353 void
30354 dwarf2out_c_finalize (void)
30356 last_var_location_insn = NULL;
30357 cached_next_real_insn = NULL;
30358 used_rtx_array = NULL;
30359 incomplete_types = NULL;
30360 decl_scope_table = NULL;
30361 debug_info_section = NULL;
30362 debug_skeleton_info_section = NULL;
30363 debug_abbrev_section = NULL;
30364 debug_skeleton_abbrev_section = NULL;
30365 debug_aranges_section = NULL;
30366 debug_addr_section = NULL;
30367 debug_macinfo_section = NULL;
30368 debug_line_section = NULL;
30369 debug_skeleton_line_section = NULL;
30370 debug_loc_section = NULL;
30371 debug_pubnames_section = NULL;
30372 debug_pubtypes_section = NULL;
30373 debug_str_section = NULL;
30374 debug_line_str_section = NULL;
30375 debug_str_dwo_section = NULL;
30376 debug_str_offsets_section = NULL;
30377 debug_ranges_section = NULL;
30378 debug_frame_section = NULL;
30379 fde_vec = NULL;
30380 debug_str_hash = NULL;
30381 debug_line_str_hash = NULL;
30382 skeleton_debug_str_hash = NULL;
30383 dw2_string_counter = 0;
30384 have_multiple_function_sections = false;
30385 text_section_used = false;
30386 cold_text_section_used = false;
30387 cold_text_section = NULL;
30388 current_unit_personality = NULL;
30390 early_dwarf = false;
30391 early_dwarf_finished = false;
30393 next_die_offset = 0;
30394 single_comp_unit_die = NULL;
30395 comdat_type_list = NULL;
30396 limbo_die_list = NULL;
30397 file_table = NULL;
30398 decl_die_table = NULL;
30399 common_block_die_table = NULL;
30400 decl_loc_table = NULL;
30401 call_arg_locations = NULL;
30402 call_arg_loc_last = NULL;
30403 call_site_count = -1;
30404 tail_call_site_count = -1;
30405 cached_dw_loc_list_table = NULL;
30406 abbrev_die_table = NULL;
30407 delete dwarf_proc_stack_usage_map;
30408 dwarf_proc_stack_usage_map = NULL;
30409 line_info_label_num = 0;
30410 cur_line_info_table = NULL;
30411 text_section_line_info = NULL;
30412 cold_text_section_line_info = NULL;
30413 separate_line_info = NULL;
30414 info_section_emitted = false;
30415 pubname_table = NULL;
30416 pubtype_table = NULL;
30417 macinfo_table = NULL;
30418 ranges_table = NULL;
30419 ranges_by_label = NULL;
30420 rnglist_idx = 0;
30421 have_location_lists = false;
30422 loclabel_num = 0;
30423 poc_label_num = 0;
30424 last_emitted_file = NULL;
30425 label_num = 0;
30426 tmpl_value_parm_die_table = NULL;
30427 generic_type_instances = NULL;
30428 frame_pointer_fb_offset = 0;
30429 frame_pointer_fb_offset_valid = false;
30430 base_types.release ();
30431 XDELETEVEC (producer_string);
30432 producer_string = NULL;
30435 #include "gt-dwarf2out.h"