2017-02-20 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / dwarf2out.c
blob14c14f3c9ec72aefde5b5f58d942b1f5fbe4b4d7
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"
96 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
97 int, bool);
98 static rtx_insn *last_var_location_insn;
99 static rtx_insn *cached_next_real_insn;
100 static void dwarf2out_decl (tree);
102 #ifndef XCOFF_DEBUGGING_INFO
103 #define XCOFF_DEBUGGING_INFO 0
104 #endif
106 #ifndef HAVE_XCOFF_DWARF_EXTRAS
107 #define HAVE_XCOFF_DWARF_EXTRAS 0
108 #endif
110 #ifdef VMS_DEBUGGING_INFO
111 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
113 /* Define this macro to be a nonzero value if the directory specifications
114 which are output in the debug info should end with a separator. */
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
116 /* Define this macro to evaluate to a nonzero value if GCC should refrain
117 from generating indirect strings in DWARF2 debug information, for instance
118 if your target is stuck with an old version of GDB that is unable to
119 process them properly or uses VMS Debug. */
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
121 #else
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
124 #endif
126 /* ??? Poison these here until it can be done generically. They've been
127 totally replaced in this file; make sure it stays that way. */
128 #undef DWARF2_UNWIND_INFO
129 #undef DWARF2_FRAME_INFO
130 #if (GCC_VERSION >= 3000)
131 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
132 #endif
134 /* The size of the target's pointer type. */
135 #ifndef PTR_SIZE
136 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
137 #endif
139 /* Array of RTXes referenced by the debugging information, which therefore
140 must be kept around forever. */
141 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
143 /* A pointer to the base of a list of incomplete types which might be
144 completed at some later time. incomplete_types_list needs to be a
145 vec<tree, va_gc> *because we want to tell the garbage collector about
146 it. */
147 static GTY(()) vec<tree, va_gc> *incomplete_types;
149 /* A pointer to the base of a table of references to declaration
150 scopes. This table is a display which tracks the nesting
151 of declaration scopes at the current scope and containing
152 scopes. This table is used to find the proper place to
153 define type declaration DIE's. */
154 static GTY(()) vec<tree, va_gc> *decl_scope_table;
156 /* Pointers to various DWARF2 sections. */
157 static GTY(()) section *debug_info_section;
158 static GTY(()) section *debug_skeleton_info_section;
159 static GTY(()) section *debug_abbrev_section;
160 static GTY(()) section *debug_skeleton_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_addr_section;
163 static GTY(()) section *debug_macinfo_section;
164 static const char *debug_macinfo_section_name;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_frame_section;
177 /* Maximum size (in bytes) of an artificially generated label. */
178 #define MAX_ARTIFICIAL_LABEL_BYTES 30
180 /* According to the (draft) DWARF 3 specification, the initial length
181 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
182 bytes are 0xffffffff, followed by the length stored in the next 8
183 bytes.
185 However, the SGI/MIPS ABI uses an initial length which is equal to
186 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
188 #ifndef DWARF_INITIAL_LENGTH_SIZE
189 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
190 #endif
192 /* Round SIZE up to the nearest BOUNDARY. */
193 #define DWARF_ROUND(SIZE,BOUNDARY) \
194 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
196 /* CIE identifier. */
197 #if HOST_BITS_PER_WIDE_INT >= 64
198 #define DWARF_CIE_ID \
199 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
200 #else
201 #define DWARF_CIE_ID DW_CIE_ID
202 #endif
205 /* A vector for a table that contains frame description
206 information for each routine. */
207 #define NOT_INDEXED (-1U)
208 #define NO_INDEX_ASSIGNED (-2U)
210 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
212 struct GTY((for_user)) indirect_string_node {
213 const char *str;
214 unsigned int refcount;
215 enum dwarf_form form;
216 char *label;
217 unsigned int index;
220 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
222 typedef const char *compare_type;
224 static hashval_t hash (indirect_string_node *);
225 static bool equal (indirect_string_node *, const char *);
228 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
230 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
232 /* With split_debug_info, both the comp_dir and dwo_name go in the
233 main object file, rather than the dwo, similar to the force_direct
234 parameter elsewhere but with additional complications:
236 1) The string is needed in both the main object file and the dwo.
237 That is, the comp_dir and dwo_name will appear in both places.
239 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
240 DW_FORM_line_strp or DW_FORM_GNU_str_index.
242 3) GCC chooses the form to use late, depending on the size and
243 reference count.
245 Rather than forcing the all debug string handling functions and
246 callers to deal with these complications, simply use a separate,
247 special-cased string table for any attribute that should go in the
248 main object file. This limits the complexity to just the places
249 that need it. */
251 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
253 static GTY(()) int dw2_string_counter;
255 /* True if the compilation unit places functions in more than one section. */
256 static GTY(()) bool have_multiple_function_sections = false;
258 /* Whether the default text and cold text sections have been used at all. */
260 static GTY(()) bool text_section_used = false;
261 static GTY(()) bool cold_text_section_used = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* The DIE for C++14 'auto' in a function return type. */
267 static GTY(()) dw_die_ref auto_die;
269 /* The DIE for C++14 'decltype(auto)' in a function return type. */
270 static GTY(()) dw_die_ref decltype_auto_die;
272 /* Forward declarations for functions defined in this file. */
274 static void output_call_frame_info (int);
275 static void dwarf2out_note_section_used (void);
277 /* Personality decl of current unit. Used only when assembler does not support
278 personality CFI. */
279 static GTY(()) rtx current_unit_personality;
281 /* .debug_rnglists next index. */
282 static unsigned int rnglist_idx;
284 /* Data and reference forms for relocatable data. */
285 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
286 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
288 #ifndef DEBUG_FRAME_SECTION
289 #define DEBUG_FRAME_SECTION ".debug_frame"
290 #endif
292 #ifndef FUNC_BEGIN_LABEL
293 #define FUNC_BEGIN_LABEL "LFB"
294 #endif
296 #ifndef FUNC_END_LABEL
297 #define FUNC_END_LABEL "LFE"
298 #endif
300 #ifndef PROLOGUE_END_LABEL
301 #define PROLOGUE_END_LABEL "LPE"
302 #endif
304 #ifndef EPILOGUE_BEGIN_LABEL
305 #define EPILOGUE_BEGIN_LABEL "LEB"
306 #endif
308 #ifndef FRAME_BEGIN_LABEL
309 #define FRAME_BEGIN_LABEL "Lframe"
310 #endif
311 #define CIE_AFTER_SIZE_LABEL "LSCIE"
312 #define CIE_END_LABEL "LECIE"
313 #define FDE_LABEL "LSFDE"
314 #define FDE_AFTER_SIZE_LABEL "LASFDE"
315 #define FDE_END_LABEL "LEFDE"
316 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
317 #define LINE_NUMBER_END_LABEL "LELT"
318 #define LN_PROLOG_AS_LABEL "LASLTP"
319 #define LN_PROLOG_END_LABEL "LELTP"
320 #define DIE_LABEL_PREFIX "DW"
322 /* Match the base name of a file to the base name of a compilation unit. */
324 static int
325 matches_main_base (const char *path)
327 /* Cache the last query. */
328 static const char *last_path = NULL;
329 static int last_match = 0;
330 if (path != last_path)
332 const char *base;
333 int length = base_of_path (path, &base);
334 last_path = path;
335 last_match = (length == main_input_baselength
336 && memcmp (base, main_input_basename, length) == 0);
338 return last_match;
341 #ifdef DEBUG_DEBUG_STRUCT
343 static int
344 dump_struct_debug (tree type, enum debug_info_usage usage,
345 enum debug_struct_file criterion, int generic,
346 int matches, int result)
348 /* Find the type name. */
349 tree type_decl = TYPE_STUB_DECL (type);
350 tree t = type_decl;
351 const char *name = 0;
352 if (TREE_CODE (t) == TYPE_DECL)
353 t = DECL_NAME (t);
354 if (t)
355 name = IDENTIFIER_POINTER (t);
357 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
358 criterion,
359 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
360 matches ? "bas" : "hdr",
361 generic ? "gen" : "ord",
362 usage == DINFO_USAGE_DFN ? ";" :
363 usage == DINFO_USAGE_DIR_USE ? "." : "*",
364 result,
365 (void*) type_decl, name);
366 return result;
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369 dump_struct_debug (type, usage, criterion, generic, matches, result)
371 #else
373 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
374 (result)
376 #endif
378 /* Get the number of HOST_WIDE_INTs needed to represent the precision
379 of the number. Some constants have a large uniform precision, so
380 we get the precision needed for the actual value of the number. */
382 static unsigned int
383 get_full_len (const wide_int &op)
385 int prec = wi::min_precision (op, UNSIGNED);
386 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
387 / HOST_BITS_PER_WIDE_INT);
390 static bool
391 should_emit_struct_debug (tree type, enum debug_info_usage usage)
393 enum debug_struct_file criterion;
394 tree type_decl;
395 bool generic = lang_hooks.types.generic_p (type);
397 if (generic)
398 criterion = debug_struct_generic[usage];
399 else
400 criterion = debug_struct_ordinary[usage];
402 if (criterion == DINFO_STRUCT_FILE_NONE)
403 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
404 if (criterion == DINFO_STRUCT_FILE_ANY)
405 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
409 if (type_decl != NULL)
411 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
415 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
418 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
421 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
422 switch to the data section instead, and write out a synthetic start label
423 for collect2 the first time around. */
425 static void
426 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
428 if (eh_frame_section == 0)
430 int flags;
432 if (EH_TABLES_CAN_BE_READ_ONLY)
434 int fde_encoding;
435 int per_encoding;
436 int lsda_encoding;
438 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
439 /*global=*/0);
440 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
441 /*global=*/1);
442 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
443 /*global=*/0);
444 flags = ((! flag_pic
445 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
446 && (fde_encoding & 0x70) != DW_EH_PE_aligned
447 && (per_encoding & 0x70) != DW_EH_PE_absptr
448 && (per_encoding & 0x70) != DW_EH_PE_aligned
449 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
450 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
451 ? 0 : SECTION_WRITE);
453 else
454 flags = SECTION_WRITE;
456 #ifdef EH_FRAME_SECTION_NAME
457 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
458 #else
459 eh_frame_section = ((flags == SECTION_WRITE)
460 ? data_section : readonly_data_section);
461 #endif /* EH_FRAME_SECTION_NAME */
464 switch_to_section (eh_frame_section);
466 #ifdef EH_FRAME_THROUGH_COLLECT2
467 /* We have no special eh_frame section. Emit special labels to guide
468 collect2. */
469 if (!back)
471 tree label = get_file_function_name ("F");
472 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
473 targetm.asm_out.globalize_label (asm_out_file,
474 IDENTIFIER_POINTER (label));
475 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
477 #endif
480 /* Switch [BACK] to the eh or debug frame table section, depending on
481 FOR_EH. */
483 static void
484 switch_to_frame_table_section (int for_eh, bool back)
486 if (for_eh)
487 switch_to_eh_frame_section (back);
488 else
490 if (!debug_frame_section)
491 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
492 SECTION_DEBUG, NULL);
493 switch_to_section (debug_frame_section);
497 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
499 enum dw_cfi_oprnd_type
500 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
502 switch (cfi)
504 case DW_CFA_nop:
505 case DW_CFA_GNU_window_save:
506 case DW_CFA_remember_state:
507 case DW_CFA_restore_state:
508 return dw_cfi_oprnd_unused;
510 case DW_CFA_set_loc:
511 case DW_CFA_advance_loc1:
512 case DW_CFA_advance_loc2:
513 case DW_CFA_advance_loc4:
514 case DW_CFA_MIPS_advance_loc8:
515 return dw_cfi_oprnd_addr;
517 case DW_CFA_offset:
518 case DW_CFA_offset_extended:
519 case DW_CFA_def_cfa:
520 case DW_CFA_offset_extended_sf:
521 case DW_CFA_def_cfa_sf:
522 case DW_CFA_restore:
523 case DW_CFA_restore_extended:
524 case DW_CFA_undefined:
525 case DW_CFA_same_value:
526 case DW_CFA_def_cfa_register:
527 case DW_CFA_register:
528 case DW_CFA_expression:
529 case DW_CFA_val_expression:
530 return dw_cfi_oprnd_reg_num;
532 case DW_CFA_def_cfa_offset:
533 case DW_CFA_GNU_args_size:
534 case DW_CFA_def_cfa_offset_sf:
535 return dw_cfi_oprnd_offset;
537 case DW_CFA_def_cfa_expression:
538 return dw_cfi_oprnd_loc;
540 default:
541 gcc_unreachable ();
545 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
547 enum dw_cfi_oprnd_type
548 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
550 switch (cfi)
552 case DW_CFA_def_cfa:
553 case DW_CFA_def_cfa_sf:
554 case DW_CFA_offset:
555 case DW_CFA_offset_extended_sf:
556 case DW_CFA_offset_extended:
557 return dw_cfi_oprnd_offset;
559 case DW_CFA_register:
560 return dw_cfi_oprnd_reg_num;
562 case DW_CFA_expression:
563 case DW_CFA_val_expression:
564 return dw_cfi_oprnd_loc;
566 default:
567 return dw_cfi_oprnd_unused;
571 /* Output one FDE. */
573 static void
574 output_fde (dw_fde_ref fde, bool for_eh, bool second,
575 char *section_start_label, int fde_encoding, char *augmentation,
576 bool any_lsda_needed, int lsda_encoding)
578 const char *begin, *end;
579 static unsigned int j;
580 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
582 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
583 /* empty */ 0);
584 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
585 for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
587 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
588 if (!XCOFF_DEBUGGING_INFO || for_eh)
590 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
591 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
592 " indicating 64-bit DWARF extension");
593 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
594 "FDE Length");
596 ASM_OUTPUT_LABEL (asm_out_file, l1);
598 if (for_eh)
599 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
600 else
601 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
602 debug_frame_section, "FDE CIE offset");
604 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
605 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
607 if (for_eh)
609 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
610 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
611 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
612 "FDE initial location");
613 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
614 end, begin, "FDE address range");
616 else
618 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
622 if (augmentation[0])
624 if (any_lsda_needed)
626 int size = size_of_encoded_value (lsda_encoding);
628 if (lsda_encoding == DW_EH_PE_aligned)
630 int offset = ( 4 /* Length */
631 + 4 /* CIE offset */
632 + 2 * size_of_encoded_value (fde_encoding)
633 + 1 /* Augmentation size */ );
634 int pad = -offset & (PTR_SIZE - 1);
636 size += pad;
637 gcc_assert (size_of_uleb128 (size) == 1);
640 dw2_asm_output_data_uleb128 (size, "Augmentation size");
642 if (fde->uses_eh_lsda)
644 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
645 fde->funcdef_number);
646 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
647 gen_rtx_SYMBOL_REF (Pmode, l1),
648 false,
649 "Language Specific Data Area");
651 else
653 if (lsda_encoding == DW_EH_PE_aligned)
654 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
655 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
656 "Language Specific Data Area (none)");
659 else
660 dw2_asm_output_data_uleb128 (0, "Augmentation size");
663 /* Loop through the Call Frame Instructions associated with this FDE. */
664 fde->dw_fde_current_label = begin;
666 size_t from, until, i;
668 from = 0;
669 until = vec_safe_length (fde->dw_fde_cfi);
671 if (fde->dw_fde_second_begin == NULL)
673 else if (!second)
674 until = fde->dw_fde_switch_cfi_index;
675 else
676 from = fde->dw_fde_switch_cfi_index;
678 for (i = from; i < until; i++)
679 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
682 /* If we are to emit a ref/link from function bodies to their frame tables,
683 do it now. This is typically performed to make sure that tables
684 associated with functions are dragged with them and not discarded in
685 garbage collecting links. We need to do this on a per function basis to
686 cope with -ffunction-sections. */
688 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
689 /* Switch to the function section, emit the ref to the tables, and
690 switch *back* into the table section. */
691 switch_to_section (function_section (fde->decl));
692 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
693 switch_to_frame_table_section (for_eh, true);
694 #endif
696 /* Pad the FDE out to an address sized boundary. */
697 ASM_OUTPUT_ALIGN (asm_out_file,
698 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
699 ASM_OUTPUT_LABEL (asm_out_file, l2);
701 j += 2;
704 /* Return true if frame description entry FDE is needed for EH. */
706 static bool
707 fde_needed_for_eh_p (dw_fde_ref fde)
709 if (flag_asynchronous_unwind_tables)
710 return true;
712 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
713 return true;
715 if (fde->uses_eh_lsda)
716 return true;
718 /* If exceptions are enabled, we have collected nothrow info. */
719 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
720 return false;
722 return true;
725 /* Output the call frame information used to record information
726 that relates to calculating the frame pointer, and records the
727 location of saved registers. */
729 static void
730 output_call_frame_info (int for_eh)
732 unsigned int i;
733 dw_fde_ref fde;
734 dw_cfi_ref cfi;
735 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
736 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
737 bool any_lsda_needed = false;
738 char augmentation[6];
739 int augmentation_size;
740 int fde_encoding = DW_EH_PE_absptr;
741 int per_encoding = DW_EH_PE_absptr;
742 int lsda_encoding = DW_EH_PE_absptr;
743 int return_reg;
744 rtx personality = NULL;
745 int dw_cie_version;
747 /* Don't emit a CIE if there won't be any FDEs. */
748 if (!fde_vec)
749 return;
751 /* Nothing to do if the assembler's doing it all. */
752 if (dwarf2out_do_cfi_asm ())
753 return;
755 /* If we don't have any functions we'll want to unwind out of, don't emit
756 any EH unwind information. If we make FDEs linkonce, we may have to
757 emit an empty label for an FDE that wouldn't otherwise be emitted. We
758 want to avoid having an FDE kept around when the function it refers to
759 is discarded. Example where this matters: a primary function template
760 in C++ requires EH information, an explicit specialization doesn't. */
761 if (for_eh)
763 bool any_eh_needed = false;
765 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
767 if (fde->uses_eh_lsda)
768 any_eh_needed = any_lsda_needed = true;
769 else if (fde_needed_for_eh_p (fde))
770 any_eh_needed = true;
771 else if (TARGET_USES_WEAK_UNWIND_INFO)
772 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
775 if (!any_eh_needed)
776 return;
779 /* We're going to be generating comments, so turn on app. */
780 if (flag_debug_asm)
781 app_enable ();
783 /* Switch to the proper frame section, first time. */
784 switch_to_frame_table_section (for_eh, false);
786 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
787 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
789 /* Output the CIE. */
790 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
791 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
792 if (!XCOFF_DEBUGGING_INFO || for_eh)
794 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
795 dw2_asm_output_data (4, 0xffffffff,
796 "Initial length escape value indicating 64-bit DWARF extension");
797 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
798 "Length of Common Information Entry");
800 ASM_OUTPUT_LABEL (asm_out_file, l1);
802 /* Now that the CIE pointer is PC-relative for EH,
803 use 0 to identify the CIE. */
804 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
805 (for_eh ? 0 : DWARF_CIE_ID),
806 "CIE Identifier Tag");
808 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
809 use CIE version 1, unless that would produce incorrect results
810 due to overflowing the return register column. */
811 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
812 dw_cie_version = 1;
813 if (return_reg >= 256 || dwarf_version > 2)
814 dw_cie_version = 3;
815 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
817 augmentation[0] = 0;
818 augmentation_size = 0;
820 personality = current_unit_personality;
821 if (for_eh)
823 char *p;
825 /* Augmentation:
826 z Indicates that a uleb128 is present to size the
827 augmentation section.
828 L Indicates the encoding (and thus presence) of
829 an LSDA pointer in the FDE augmentation.
830 R Indicates a non-default pointer encoding for
831 FDE code pointers.
832 P Indicates the presence of an encoding + language
833 personality routine in the CIE augmentation. */
835 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
836 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
837 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
839 p = augmentation + 1;
840 if (personality)
842 *p++ = 'P';
843 augmentation_size += 1 + size_of_encoded_value (per_encoding);
844 assemble_external_libcall (personality);
846 if (any_lsda_needed)
848 *p++ = 'L';
849 augmentation_size += 1;
851 if (fde_encoding != DW_EH_PE_absptr)
853 *p++ = 'R';
854 augmentation_size += 1;
856 if (p > augmentation + 1)
858 augmentation[0] = 'z';
859 *p = '\0';
862 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
863 if (personality && per_encoding == DW_EH_PE_aligned)
865 int offset = ( 4 /* Length */
866 + 4 /* CIE Id */
867 + 1 /* CIE version */
868 + strlen (augmentation) + 1 /* Augmentation */
869 + size_of_uleb128 (1) /* Code alignment */
870 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
871 + 1 /* RA column */
872 + 1 /* Augmentation size */
873 + 1 /* Personality encoding */ );
874 int pad = -offset & (PTR_SIZE - 1);
876 augmentation_size += pad;
878 /* Augmentations should be small, so there's scarce need to
879 iterate for a solution. Die if we exceed one uleb128 byte. */
880 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
884 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
885 if (dw_cie_version >= 4)
887 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
888 dw2_asm_output_data (1, 0, "CIE Segment Size");
890 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
891 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
892 "CIE Data Alignment Factor");
894 if (dw_cie_version == 1)
895 dw2_asm_output_data (1, return_reg, "CIE RA Column");
896 else
897 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
899 if (augmentation[0])
901 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
902 if (personality)
904 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
905 eh_data_format_name (per_encoding));
906 dw2_asm_output_encoded_addr_rtx (per_encoding,
907 personality,
908 true, NULL);
911 if (any_lsda_needed)
912 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
913 eh_data_format_name (lsda_encoding));
915 if (fde_encoding != DW_EH_PE_absptr)
916 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
917 eh_data_format_name (fde_encoding));
920 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
921 output_cfi (cfi, NULL, for_eh);
923 /* Pad the CIE out to an address sized boundary. */
924 ASM_OUTPUT_ALIGN (asm_out_file,
925 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
926 ASM_OUTPUT_LABEL (asm_out_file, l2);
928 /* Loop through all of the FDE's. */
929 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
931 unsigned int k;
933 /* Don't emit EH unwind info for leaf functions that don't need it. */
934 if (for_eh && !fde_needed_for_eh_p (fde))
935 continue;
937 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
938 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
939 augmentation, any_lsda_needed, lsda_encoding);
942 if (for_eh && targetm.terminate_dw2_eh_frame_info)
943 dw2_asm_output_data (4, 0, "End of Table");
945 /* Turn off app to make assembly quicker. */
946 if (flag_debug_asm)
947 app_disable ();
950 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
952 static void
953 dwarf2out_do_cfi_startproc (bool second)
955 int enc;
956 rtx ref;
957 rtx personality = get_personality_function (current_function_decl);
959 fprintf (asm_out_file, "\t.cfi_startproc\n");
961 if (personality)
963 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
964 ref = personality;
966 /* ??? The GAS support isn't entirely consistent. We have to
967 handle indirect support ourselves, but PC-relative is done
968 in the assembler. Further, the assembler can't handle any
969 of the weirder relocation types. */
970 if (enc & DW_EH_PE_indirect)
971 ref = dw2_force_const_mem (ref, true);
973 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
974 output_addr_const (asm_out_file, ref);
975 fputc ('\n', asm_out_file);
978 if (crtl->uses_eh_lsda)
980 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
982 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
983 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
984 current_function_funcdef_no);
985 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
986 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
988 if (enc & DW_EH_PE_indirect)
989 ref = dw2_force_const_mem (ref, true);
991 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
992 output_addr_const (asm_out_file, ref);
993 fputc ('\n', asm_out_file);
997 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
998 this allocation may be done before pass_final. */
1000 dw_fde_ref
1001 dwarf2out_alloc_current_fde (void)
1003 dw_fde_ref fde;
1005 fde = ggc_cleared_alloc<dw_fde_node> ();
1006 fde->decl = current_function_decl;
1007 fde->funcdef_number = current_function_funcdef_no;
1008 fde->fde_index = vec_safe_length (fde_vec);
1009 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1010 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1011 fde->nothrow = crtl->nothrow;
1012 fde->drap_reg = INVALID_REGNUM;
1013 fde->vdrap_reg = INVALID_REGNUM;
1015 /* Record the FDE associated with this function. */
1016 cfun->fde = fde;
1017 vec_safe_push (fde_vec, fde);
1019 return fde;
1022 /* Output a marker (i.e. a label) for the beginning of a function, before
1023 the prologue. */
1025 void
1026 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1027 unsigned int column ATTRIBUTE_UNUSED,
1028 const char *file ATTRIBUTE_UNUSED)
1030 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1031 char * dup_label;
1032 dw_fde_ref fde;
1033 section *fnsec;
1034 bool do_frame;
1036 current_function_func_begin_label = NULL;
1038 do_frame = dwarf2out_do_frame ();
1040 /* ??? current_function_func_begin_label is also used by except.c for
1041 call-site information. We must emit this label if it might be used. */
1042 if (!do_frame
1043 && (!flag_exceptions
1044 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1045 return;
1047 fnsec = function_section (current_function_decl);
1048 switch_to_section (fnsec);
1049 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1050 current_function_funcdef_no);
1051 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1052 current_function_funcdef_no);
1053 dup_label = xstrdup (label);
1054 current_function_func_begin_label = dup_label;
1056 /* We can elide the fde allocation if we're not emitting debug info. */
1057 if (!do_frame)
1058 return;
1060 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1061 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1062 would include pass_dwarf2_frame. If we've not created the FDE yet,
1063 do so now. */
1064 fde = cfun->fde;
1065 if (fde == NULL)
1066 fde = dwarf2out_alloc_current_fde ();
1068 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1069 fde->dw_fde_begin = dup_label;
1070 fde->dw_fde_current_label = dup_label;
1071 fde->in_std_section = (fnsec == text_section
1072 || (cold_text_section && fnsec == cold_text_section));
1074 /* We only want to output line number information for the genuine dwarf2
1075 prologue case, not the eh frame case. */
1076 #ifdef DWARF2_DEBUGGING_INFO
1077 if (file)
1078 dwarf2out_source_line (line, column, file, 0, true);
1079 #endif
1081 if (dwarf2out_do_cfi_asm ())
1082 dwarf2out_do_cfi_startproc (false);
1083 else
1085 rtx personality = get_personality_function (current_function_decl);
1086 if (!current_unit_personality)
1087 current_unit_personality = personality;
1089 /* We cannot keep a current personality per function as without CFI
1090 asm, at the point where we emit the CFI data, there is no current
1091 function anymore. */
1092 if (personality && current_unit_personality != personality)
1093 sorry ("multiple EH personalities are supported only with assemblers "
1094 "supporting .cfi_personality directive");
1098 /* Output a marker (i.e. a label) for the end of the generated code
1099 for a function prologue. This gets called *after* the prologue code has
1100 been generated. */
1102 void
1103 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1104 const char *file ATTRIBUTE_UNUSED)
1106 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1108 /* Output a label to mark the endpoint of the code generated for this
1109 function. */
1110 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1111 current_function_funcdef_no);
1112 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1113 current_function_funcdef_no);
1114 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1117 /* Output a marker (i.e. a label) for the beginning of the generated code
1118 for a function epilogue. This gets called *before* the prologue code has
1119 been generated. */
1121 void
1122 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1123 const char *file ATTRIBUTE_UNUSED)
1125 dw_fde_ref fde = cfun->fde;
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128 if (fde->dw_fde_vms_begin_epilogue)
1129 return;
1131 /* Output a label to mark the endpoint of the code generated for this
1132 function. */
1133 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1134 current_function_funcdef_no);
1135 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1136 current_function_funcdef_no);
1137 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1140 /* Output a marker (i.e. a label) for the absolute end of the generated code
1141 for a function definition. This gets called *after* the epilogue code has
1142 been generated. */
1144 void
1145 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1146 const char *file ATTRIBUTE_UNUSED)
1148 dw_fde_ref fde;
1149 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1151 last_var_location_insn = NULL;
1152 cached_next_real_insn = NULL;
1154 if (dwarf2out_do_cfi_asm ())
1155 fprintf (asm_out_file, "\t.cfi_endproc\n");
1157 /* Output a label to mark the endpoint of the code generated for this
1158 function. */
1159 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1160 current_function_funcdef_no);
1161 ASM_OUTPUT_LABEL (asm_out_file, label);
1162 fde = cfun->fde;
1163 gcc_assert (fde != NULL);
1164 if (fde->dw_fde_second_begin == NULL)
1165 fde->dw_fde_end = xstrdup (label);
1168 void
1169 dwarf2out_frame_finish (void)
1171 /* Output call frame information. */
1172 if (targetm.debug_unwind_info () == UI_DWARF2)
1173 output_call_frame_info (0);
1175 /* Output another copy for the unwinder. */
1176 if ((flag_unwind_tables || flag_exceptions)
1177 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1178 output_call_frame_info (1);
1181 /* Note that the current function section is being used for code. */
1183 static void
1184 dwarf2out_note_section_used (void)
1186 section *sec = current_function_section ();
1187 if (sec == text_section)
1188 text_section_used = true;
1189 else if (sec == cold_text_section)
1190 cold_text_section_used = true;
1193 static void var_location_switch_text_section (void);
1194 static void set_cur_line_info_table (section *);
1196 void
1197 dwarf2out_switch_text_section (void)
1199 section *sect;
1200 dw_fde_ref fde = cfun->fde;
1202 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1204 if (!in_cold_section_p)
1206 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1207 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1208 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1210 else
1212 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1216 have_multiple_function_sections = true;
1218 /* There is no need to mark used sections when not debugging. */
1219 if (cold_text_section != NULL)
1220 dwarf2out_note_section_used ();
1222 if (dwarf2out_do_cfi_asm ())
1223 fprintf (asm_out_file, "\t.cfi_endproc\n");
1225 /* Now do the real section switch. */
1226 sect = current_function_section ();
1227 switch_to_section (sect);
1229 fde->second_in_std_section
1230 = (sect == text_section
1231 || (cold_text_section && sect == cold_text_section));
1233 if (dwarf2out_do_cfi_asm ())
1234 dwarf2out_do_cfi_startproc (true);
1236 var_location_switch_text_section ();
1238 if (cold_text_section != NULL)
1239 set_cur_line_info_table (sect);
1242 /* And now, the subset of the debugging information support code necessary
1243 for emitting location expressions. */
1245 /* Data about a single source file. */
1246 struct GTY((for_user)) dwarf_file_data {
1247 const char * filename;
1248 int emitted_number;
1251 /* Describe an entry into the .debug_addr section. */
1253 enum ate_kind {
1254 ate_kind_rtx,
1255 ate_kind_rtx_dtprel,
1256 ate_kind_label
1259 struct GTY((for_user)) addr_table_entry {
1260 enum ate_kind kind;
1261 unsigned int refcount;
1262 unsigned int index;
1263 union addr_table_entry_struct_union
1265 rtx GTY ((tag ("0"))) rtl;
1266 char * GTY ((tag ("1"))) label;
1268 GTY ((desc ("%1.kind"))) addr;
1271 /* Location lists are ranges + location descriptions for that range,
1272 so you can track variables that are in different places over
1273 their entire life. */
1274 typedef struct GTY(()) dw_loc_list_struct {
1275 dw_loc_list_ref dw_loc_next;
1276 const char *begin; /* Label and addr_entry for start of range */
1277 addr_table_entry *begin_entry;
1278 const char *end; /* Label for end of range */
1279 char *ll_symbol; /* Label for beginning of location list.
1280 Only on head of list */
1281 const char *section; /* Section this loclist is relative to */
1282 dw_loc_descr_ref expr;
1283 hashval_t hash;
1284 /* True if all addresses in this and subsequent lists are known to be
1285 resolved. */
1286 bool resolved_addr;
1287 /* True if this list has been replaced by dw_loc_next. */
1288 bool replaced;
1289 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1290 section. */
1291 unsigned char emitted : 1;
1292 /* True if hash field is index rather than hash value. */
1293 unsigned char num_assigned : 1;
1294 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1295 unsigned char offset_emitted : 1;
1296 /* True if the range should be emitted even if begin and end
1297 are the same. */
1298 bool force;
1299 } dw_loc_list_node;
1301 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1302 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1304 /* Convert a DWARF stack opcode into its string name. */
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1309 const char *name = get_DW_OP_name (op);
1311 if (name != NULL)
1312 return name;
1314 return "OP_<unknown>";
1317 /* Return a pointer to a newly allocated location description. Location
1318 descriptions are simple expression terms that can be strung
1319 together to form more complicated location (address) descriptions. */
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323 unsigned HOST_WIDE_INT oprnd2)
1325 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1327 descr->dw_loc_opc = op;
1328 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1329 descr->dw_loc_oprnd1.val_entry = NULL;
1330 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1331 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1332 descr->dw_loc_oprnd2.val_entry = NULL;
1333 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1335 return descr;
1338 /* Return a pointer to a newly allocated location description for
1339 REG and OFFSET. */
1341 static inline dw_loc_descr_ref
1342 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1344 if (reg <= 31)
1345 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1346 offset, 0);
1347 else
1348 return new_loc_descr (DW_OP_bregx, reg, offset);
1351 /* Add a location description term to a location description expression. */
1353 static inline void
1354 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1356 dw_loc_descr_ref *d;
1358 /* Find the end of the chain. */
1359 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1362 *d = descr;
1365 /* Compare two location operands for exact equality. */
1367 static bool
1368 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1370 if (a->val_class != b->val_class)
1371 return false;
1372 switch (a->val_class)
1374 case dw_val_class_none:
1375 return true;
1376 case dw_val_class_addr:
1377 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1379 case dw_val_class_offset:
1380 case dw_val_class_unsigned_const:
1381 case dw_val_class_const:
1382 case dw_val_class_unsigned_const_implicit:
1383 case dw_val_class_const_implicit:
1384 case dw_val_class_range_list:
1385 /* These are all HOST_WIDE_INT, signed or unsigned. */
1386 return a->v.val_unsigned == b->v.val_unsigned;
1388 case dw_val_class_loc:
1389 return a->v.val_loc == b->v.val_loc;
1390 case dw_val_class_loc_list:
1391 return a->v.val_loc_list == b->v.val_loc_list;
1392 case dw_val_class_die_ref:
1393 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1394 case dw_val_class_fde_ref:
1395 return a->v.val_fde_index == b->v.val_fde_index;
1396 case dw_val_class_lbl_id:
1397 case dw_val_class_lineptr:
1398 case dw_val_class_macptr:
1399 case dw_val_class_loclistsptr:
1400 case dw_val_class_high_pc:
1401 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1402 case dw_val_class_str:
1403 return a->v.val_str == b->v.val_str;
1404 case dw_val_class_flag:
1405 return a->v.val_flag == b->v.val_flag;
1406 case dw_val_class_file:
1407 case dw_val_class_file_implicit:
1408 return a->v.val_file == b->v.val_file;
1409 case dw_val_class_decl_ref:
1410 return a->v.val_decl_ref == b->v.val_decl_ref;
1412 case dw_val_class_const_double:
1413 return (a->v.val_double.high == b->v.val_double.high
1414 && a->v.val_double.low == b->v.val_double.low);
1416 case dw_val_class_wide_int:
1417 return *a->v.val_wide == *b->v.val_wide;
1419 case dw_val_class_vec:
1421 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1422 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1424 return (a_len == b_len
1425 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1428 case dw_val_class_data8:
1429 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1431 case dw_val_class_vms_delta:
1432 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1433 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1435 case dw_val_class_discr_value:
1436 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1437 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1438 case dw_val_class_discr_list:
1439 /* It makes no sense comparing two discriminant value lists. */
1440 return false;
1442 gcc_unreachable ();
1445 /* Compare two location atoms for exact equality. */
1447 static bool
1448 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1450 if (a->dw_loc_opc != b->dw_loc_opc)
1451 return false;
1453 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1454 address size, but since we always allocate cleared storage it
1455 should be zero for other types of locations. */
1456 if (a->dtprel != b->dtprel)
1457 return false;
1459 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1460 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1463 /* Compare two complete location expressions for exact equality. */
1465 bool
1466 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1468 while (1)
1470 if (a == b)
1471 return true;
1472 if (a == NULL || b == NULL)
1473 return false;
1474 if (!loc_descr_equal_p_1 (a, b))
1475 return false;
1477 a = a->dw_loc_next;
1478 b = b->dw_loc_next;
1483 /* Add a constant OFFSET to a location expression. */
1485 static void
1486 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1488 dw_loc_descr_ref loc;
1489 HOST_WIDE_INT *p;
1491 gcc_assert (*list_head != NULL);
1493 if (!offset)
1494 return;
1496 /* Find the end of the chain. */
1497 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1500 p = NULL;
1501 if (loc->dw_loc_opc == DW_OP_fbreg
1502 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1503 p = &loc->dw_loc_oprnd1.v.val_int;
1504 else if (loc->dw_loc_opc == DW_OP_bregx)
1505 p = &loc->dw_loc_oprnd2.v.val_int;
1507 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1508 offset. Don't optimize if an signed integer overflow would happen. */
1509 if (p != NULL
1510 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1511 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1512 *p += offset;
1514 else if (offset > 0)
1515 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1517 else
1519 loc->dw_loc_next
1520 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1521 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1525 /* Add a constant OFFSET to a location list. */
1527 static void
1528 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1530 dw_loc_list_ref d;
1531 for (d = list_head; d != NULL; d = d->dw_loc_next)
1532 loc_descr_plus_const (&d->expr, offset);
1535 #define DWARF_REF_SIZE \
1536 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1538 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1539 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1540 DW_FORM_data16 with 128 bits. */
1541 #define DWARF_LARGEST_DATA_FORM_BITS \
1542 (dwarf_version >= 5 ? 128 : 64)
1544 /* Utility inline function for construction of ops that were GNU extension
1545 before DWARF 5. */
1546 static inline enum dwarf_location_atom
1547 dwarf_OP (enum dwarf_location_atom op)
1549 switch (op)
1551 case DW_OP_implicit_pointer:
1552 if (dwarf_version < 5)
1553 return DW_OP_GNU_implicit_pointer;
1554 break;
1556 case DW_OP_entry_value:
1557 if (dwarf_version < 5)
1558 return DW_OP_GNU_entry_value;
1559 break;
1561 case DW_OP_const_type:
1562 if (dwarf_version < 5)
1563 return DW_OP_GNU_const_type;
1564 break;
1566 case DW_OP_regval_type:
1567 if (dwarf_version < 5)
1568 return DW_OP_GNU_regval_type;
1569 break;
1571 case DW_OP_deref_type:
1572 if (dwarf_version < 5)
1573 return DW_OP_GNU_deref_type;
1574 break;
1576 case DW_OP_convert:
1577 if (dwarf_version < 5)
1578 return DW_OP_GNU_convert;
1579 break;
1581 case DW_OP_reinterpret:
1582 if (dwarf_version < 5)
1583 return DW_OP_GNU_reinterpret;
1584 break;
1586 default:
1587 break;
1589 return op;
1592 /* Similarly for attributes. */
1593 static inline enum dwarf_attribute
1594 dwarf_AT (enum dwarf_attribute at)
1596 switch (at)
1598 case DW_AT_call_return_pc:
1599 if (dwarf_version < 5)
1600 return DW_AT_low_pc;
1601 break;
1603 case DW_AT_call_tail_call:
1604 if (dwarf_version < 5)
1605 return DW_AT_GNU_tail_call;
1606 break;
1608 case DW_AT_call_origin:
1609 if (dwarf_version < 5)
1610 return DW_AT_abstract_origin;
1611 break;
1613 case DW_AT_call_target:
1614 if (dwarf_version < 5)
1615 return DW_AT_GNU_call_site_target;
1616 break;
1618 case DW_AT_call_target_clobbered:
1619 if (dwarf_version < 5)
1620 return DW_AT_GNU_call_site_target_clobbered;
1621 break;
1623 case DW_AT_call_parameter:
1624 if (dwarf_version < 5)
1625 return DW_AT_abstract_origin;
1626 break;
1628 case DW_AT_call_value:
1629 if (dwarf_version < 5)
1630 return DW_AT_GNU_call_site_value;
1631 break;
1633 case DW_AT_call_data_value:
1634 if (dwarf_version < 5)
1635 return DW_AT_GNU_call_site_data_value;
1636 break;
1638 case DW_AT_call_all_calls:
1639 if (dwarf_version < 5)
1640 return DW_AT_GNU_all_call_sites;
1641 break;
1643 case DW_AT_call_all_tail_calls:
1644 if (dwarf_version < 5)
1645 return DW_AT_GNU_all_tail_call_sites;
1646 break;
1648 case DW_AT_dwo_name:
1649 if (dwarf_version < 5)
1650 return DW_AT_GNU_dwo_name;
1651 break;
1653 default:
1654 break;
1656 return at;
1659 /* And similarly for tags. */
1660 static inline enum dwarf_tag
1661 dwarf_TAG (enum dwarf_tag tag)
1663 switch (tag)
1665 case DW_TAG_call_site:
1666 if (dwarf_version < 5)
1667 return DW_TAG_GNU_call_site;
1668 break;
1670 case DW_TAG_call_site_parameter:
1671 if (dwarf_version < 5)
1672 return DW_TAG_GNU_call_site_parameter;
1673 break;
1675 default:
1676 break;
1678 return tag;
1681 static unsigned long int get_base_type_offset (dw_die_ref);
1683 /* Return the size of a location descriptor. */
1685 static unsigned long
1686 size_of_loc_descr (dw_loc_descr_ref loc)
1688 unsigned long size = 1;
1690 switch (loc->dw_loc_opc)
1692 case DW_OP_addr:
1693 size += DWARF2_ADDR_SIZE;
1694 break;
1695 case DW_OP_GNU_addr_index:
1696 case DW_OP_GNU_const_index:
1697 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1698 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1699 break;
1700 case DW_OP_const1u:
1701 case DW_OP_const1s:
1702 size += 1;
1703 break;
1704 case DW_OP_const2u:
1705 case DW_OP_const2s:
1706 size += 2;
1707 break;
1708 case DW_OP_const4u:
1709 case DW_OP_const4s:
1710 size += 4;
1711 break;
1712 case DW_OP_const8u:
1713 case DW_OP_const8s:
1714 size += 8;
1715 break;
1716 case DW_OP_constu:
1717 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1718 break;
1719 case DW_OP_consts:
1720 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1721 break;
1722 case DW_OP_pick:
1723 size += 1;
1724 break;
1725 case DW_OP_plus_uconst:
1726 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1727 break;
1728 case DW_OP_skip:
1729 case DW_OP_bra:
1730 size += 2;
1731 break;
1732 case DW_OP_breg0:
1733 case DW_OP_breg1:
1734 case DW_OP_breg2:
1735 case DW_OP_breg3:
1736 case DW_OP_breg4:
1737 case DW_OP_breg5:
1738 case DW_OP_breg6:
1739 case DW_OP_breg7:
1740 case DW_OP_breg8:
1741 case DW_OP_breg9:
1742 case DW_OP_breg10:
1743 case DW_OP_breg11:
1744 case DW_OP_breg12:
1745 case DW_OP_breg13:
1746 case DW_OP_breg14:
1747 case DW_OP_breg15:
1748 case DW_OP_breg16:
1749 case DW_OP_breg17:
1750 case DW_OP_breg18:
1751 case DW_OP_breg19:
1752 case DW_OP_breg20:
1753 case DW_OP_breg21:
1754 case DW_OP_breg22:
1755 case DW_OP_breg23:
1756 case DW_OP_breg24:
1757 case DW_OP_breg25:
1758 case DW_OP_breg26:
1759 case DW_OP_breg27:
1760 case DW_OP_breg28:
1761 case DW_OP_breg29:
1762 case DW_OP_breg30:
1763 case DW_OP_breg31:
1764 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1765 break;
1766 case DW_OP_regx:
1767 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1768 break;
1769 case DW_OP_fbreg:
1770 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1771 break;
1772 case DW_OP_bregx:
1773 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1774 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1775 break;
1776 case DW_OP_piece:
1777 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1778 break;
1779 case DW_OP_bit_piece:
1780 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1781 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1782 break;
1783 case DW_OP_deref_size:
1784 case DW_OP_xderef_size:
1785 size += 1;
1786 break;
1787 case DW_OP_call2:
1788 size += 2;
1789 break;
1790 case DW_OP_call4:
1791 size += 4;
1792 break;
1793 case DW_OP_call_ref:
1794 size += DWARF_REF_SIZE;
1795 break;
1796 case DW_OP_implicit_value:
1797 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1798 + loc->dw_loc_oprnd1.v.val_unsigned;
1799 break;
1800 case DW_OP_implicit_pointer:
1801 case DW_OP_GNU_implicit_pointer:
1802 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1803 break;
1804 case DW_OP_entry_value:
1805 case DW_OP_GNU_entry_value:
1807 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1808 size += size_of_uleb128 (op_size) + op_size;
1809 break;
1811 case DW_OP_const_type:
1812 case DW_OP_GNU_const_type:
1814 unsigned long o
1815 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1816 size += size_of_uleb128 (o) + 1;
1817 switch (loc->dw_loc_oprnd2.val_class)
1819 case dw_val_class_vec:
1820 size += loc->dw_loc_oprnd2.v.val_vec.length
1821 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1822 break;
1823 case dw_val_class_const:
1824 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1825 break;
1826 case dw_val_class_const_double:
1827 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1828 break;
1829 case dw_val_class_wide_int:
1830 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1831 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1832 break;
1833 default:
1834 gcc_unreachable ();
1836 break;
1838 case DW_OP_regval_type:
1839 case DW_OP_GNU_regval_type:
1841 unsigned long o
1842 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1843 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1844 + size_of_uleb128 (o);
1846 break;
1847 case DW_OP_deref_type:
1848 case DW_OP_GNU_deref_type:
1850 unsigned long o
1851 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1852 size += 1 + size_of_uleb128 (o);
1854 break;
1855 case DW_OP_convert:
1856 case DW_OP_reinterpret:
1857 case DW_OP_GNU_convert:
1858 case DW_OP_GNU_reinterpret:
1859 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1860 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1861 else
1863 unsigned long o
1864 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1865 size += size_of_uleb128 (o);
1867 break;
1868 case DW_OP_GNU_parameter_ref:
1869 size += 4;
1870 break;
1871 default:
1872 break;
1875 return size;
1878 /* Return the size of a series of location descriptors. */
1880 unsigned long
1881 size_of_locs (dw_loc_descr_ref loc)
1883 dw_loc_descr_ref l;
1884 unsigned long size;
1886 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1887 field, to avoid writing to a PCH file. */
1888 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1890 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1891 break;
1892 size += size_of_loc_descr (l);
1894 if (! l)
1895 return size;
1897 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1899 l->dw_loc_addr = size;
1900 size += size_of_loc_descr (l);
1903 return size;
1906 /* Return the size of the value in a DW_AT_discr_value attribute. */
1908 static int
1909 size_of_discr_value (dw_discr_value *discr_value)
1911 if (discr_value->pos)
1912 return size_of_uleb128 (discr_value->v.uval);
1913 else
1914 return size_of_sleb128 (discr_value->v.sval);
1917 /* Return the size of the value in a DW_AT_discr_list attribute. */
1919 static int
1920 size_of_discr_list (dw_discr_list_ref discr_list)
1922 int size = 0;
1924 for (dw_discr_list_ref list = discr_list;
1925 list != NULL;
1926 list = list->dw_discr_next)
1928 /* One byte for the discriminant value descriptor, and then one or two
1929 LEB128 numbers, depending on whether it's a single case label or a
1930 range label. */
1931 size += 1;
1932 size += size_of_discr_value (&list->dw_discr_lower_bound);
1933 if (list->dw_discr_range != 0)
1934 size += size_of_discr_value (&list->dw_discr_upper_bound);
1936 return size;
1939 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1940 static void get_ref_die_offset_label (char *, dw_die_ref);
1941 static unsigned long int get_ref_die_offset (dw_die_ref);
1943 /* Output location description stack opcode's operands (if any).
1944 The for_eh_or_skip parameter controls whether register numbers are
1945 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1946 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1947 info). This should be suppressed for the cases that have not been converted
1948 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1950 static void
1951 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1953 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1954 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1956 switch (loc->dw_loc_opc)
1958 #ifdef DWARF2_DEBUGGING_INFO
1959 case DW_OP_const2u:
1960 case DW_OP_const2s:
1961 dw2_asm_output_data (2, val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_const4u:
1964 if (loc->dtprel)
1966 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1967 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1968 val1->v.val_addr);
1969 fputc ('\n', asm_out_file);
1970 break;
1972 /* FALLTHRU */
1973 case DW_OP_const4s:
1974 dw2_asm_output_data (4, val1->v.val_int, NULL);
1975 break;
1976 case DW_OP_const8u:
1977 if (loc->dtprel)
1979 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1980 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1981 val1->v.val_addr);
1982 fputc ('\n', asm_out_file);
1983 break;
1985 /* FALLTHRU */
1986 case DW_OP_const8s:
1987 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1988 dw2_asm_output_data (8, val1->v.val_int, NULL);
1989 break;
1990 case DW_OP_skip:
1991 case DW_OP_bra:
1993 int offset;
1995 gcc_assert (val1->val_class == dw_val_class_loc);
1996 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1998 dw2_asm_output_data (2, offset, NULL);
2000 break;
2001 case DW_OP_implicit_value:
2002 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2003 switch (val2->val_class)
2005 case dw_val_class_const:
2006 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2007 break;
2008 case dw_val_class_vec:
2010 unsigned int elt_size = val2->v.val_vec.elt_size;
2011 unsigned int len = val2->v.val_vec.length;
2012 unsigned int i;
2013 unsigned char *p;
2015 if (elt_size > sizeof (HOST_WIDE_INT))
2017 elt_size /= 2;
2018 len *= 2;
2020 for (i = 0, p = val2->v.val_vec.array;
2021 i < len;
2022 i++, p += elt_size)
2023 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2024 "fp or vector constant word %u", i);
2026 break;
2027 case dw_val_class_const_double:
2029 unsigned HOST_WIDE_INT first, second;
2031 if (WORDS_BIG_ENDIAN)
2033 first = val2->v.val_double.high;
2034 second = val2->v.val_double.low;
2036 else
2038 first = val2->v.val_double.low;
2039 second = val2->v.val_double.high;
2041 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2042 first, NULL);
2043 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2044 second, NULL);
2046 break;
2047 case dw_val_class_wide_int:
2049 int i;
2050 int len = get_full_len (*val2->v.val_wide);
2051 if (WORDS_BIG_ENDIAN)
2052 for (i = len - 1; i >= 0; --i)
2053 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2054 val2->v.val_wide->elt (i), NULL);
2055 else
2056 for (i = 0; i < len; ++i)
2057 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2058 val2->v.val_wide->elt (i), NULL);
2060 break;
2061 case dw_val_class_addr:
2062 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2063 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2064 break;
2065 default:
2066 gcc_unreachable ();
2068 break;
2069 #else
2070 case DW_OP_const2u:
2071 case DW_OP_const2s:
2072 case DW_OP_const4u:
2073 case DW_OP_const4s:
2074 case DW_OP_const8u:
2075 case DW_OP_const8s:
2076 case DW_OP_skip:
2077 case DW_OP_bra:
2078 case DW_OP_implicit_value:
2079 /* We currently don't make any attempt to make sure these are
2080 aligned properly like we do for the main unwind info, so
2081 don't support emitting things larger than a byte if we're
2082 only doing unwinding. */
2083 gcc_unreachable ();
2084 #endif
2085 case DW_OP_const1u:
2086 case DW_OP_const1s:
2087 dw2_asm_output_data (1, val1->v.val_int, NULL);
2088 break;
2089 case DW_OP_constu:
2090 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2091 break;
2092 case DW_OP_consts:
2093 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2094 break;
2095 case DW_OP_pick:
2096 dw2_asm_output_data (1, val1->v.val_int, NULL);
2097 break;
2098 case DW_OP_plus_uconst:
2099 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2100 break;
2101 case DW_OP_breg0:
2102 case DW_OP_breg1:
2103 case DW_OP_breg2:
2104 case DW_OP_breg3:
2105 case DW_OP_breg4:
2106 case DW_OP_breg5:
2107 case DW_OP_breg6:
2108 case DW_OP_breg7:
2109 case DW_OP_breg8:
2110 case DW_OP_breg9:
2111 case DW_OP_breg10:
2112 case DW_OP_breg11:
2113 case DW_OP_breg12:
2114 case DW_OP_breg13:
2115 case DW_OP_breg14:
2116 case DW_OP_breg15:
2117 case DW_OP_breg16:
2118 case DW_OP_breg17:
2119 case DW_OP_breg18:
2120 case DW_OP_breg19:
2121 case DW_OP_breg20:
2122 case DW_OP_breg21:
2123 case DW_OP_breg22:
2124 case DW_OP_breg23:
2125 case DW_OP_breg24:
2126 case DW_OP_breg25:
2127 case DW_OP_breg26:
2128 case DW_OP_breg27:
2129 case DW_OP_breg28:
2130 case DW_OP_breg29:
2131 case DW_OP_breg30:
2132 case DW_OP_breg31:
2133 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2134 break;
2135 case DW_OP_regx:
2137 unsigned r = val1->v.val_unsigned;
2138 if (for_eh_or_skip >= 0)
2139 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2140 gcc_assert (size_of_uleb128 (r)
2141 == size_of_uleb128 (val1->v.val_unsigned));
2142 dw2_asm_output_data_uleb128 (r, NULL);
2144 break;
2145 case DW_OP_fbreg:
2146 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2147 break;
2148 case DW_OP_bregx:
2150 unsigned r = val1->v.val_unsigned;
2151 if (for_eh_or_skip >= 0)
2152 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2153 gcc_assert (size_of_uleb128 (r)
2154 == size_of_uleb128 (val1->v.val_unsigned));
2155 dw2_asm_output_data_uleb128 (r, NULL);
2156 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2158 break;
2159 case DW_OP_piece:
2160 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2161 break;
2162 case DW_OP_bit_piece:
2163 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2164 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2165 break;
2166 case DW_OP_deref_size:
2167 case DW_OP_xderef_size:
2168 dw2_asm_output_data (1, val1->v.val_int, NULL);
2169 break;
2171 case DW_OP_addr:
2172 if (loc->dtprel)
2174 if (targetm.asm_out.output_dwarf_dtprel)
2176 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2177 DWARF2_ADDR_SIZE,
2178 val1->v.val_addr);
2179 fputc ('\n', asm_out_file);
2181 else
2182 gcc_unreachable ();
2184 else
2186 #ifdef DWARF2_DEBUGGING_INFO
2187 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2188 #else
2189 gcc_unreachable ();
2190 #endif
2192 break;
2194 case DW_OP_GNU_addr_index:
2195 case DW_OP_GNU_const_index:
2196 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2197 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2198 "(index into .debug_addr)");
2199 break;
2201 case DW_OP_call2:
2202 case DW_OP_call4:
2204 unsigned long die_offset
2205 = get_ref_die_offset (val1->v.val_die_ref.die);
2206 /* Make sure the offset has been computed and that we can encode it as
2207 an operand. */
2208 gcc_assert (die_offset > 0
2209 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2210 ? 0xffff
2211 : 0xffffffff));
2212 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2213 die_offset, NULL);
2215 break;
2217 case DW_OP_implicit_pointer:
2218 case DW_OP_GNU_implicit_pointer:
2220 char label[MAX_ARTIFICIAL_LABEL_BYTES
2221 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2222 gcc_assert (val1->val_class == dw_val_class_die_ref);
2223 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2224 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2225 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2227 break;
2229 case DW_OP_entry_value:
2230 case DW_OP_GNU_entry_value:
2231 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2232 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2233 break;
2235 case DW_OP_const_type:
2236 case DW_OP_GNU_const_type:
2238 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2239 gcc_assert (o);
2240 dw2_asm_output_data_uleb128 (o, NULL);
2241 switch (val2->val_class)
2243 case dw_val_class_const:
2244 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2245 dw2_asm_output_data (1, l, NULL);
2246 dw2_asm_output_data (l, val2->v.val_int, NULL);
2247 break;
2248 case dw_val_class_vec:
2250 unsigned int elt_size = val2->v.val_vec.elt_size;
2251 unsigned int len = val2->v.val_vec.length;
2252 unsigned int i;
2253 unsigned char *p;
2255 l = len * elt_size;
2256 dw2_asm_output_data (1, l, NULL);
2257 if (elt_size > sizeof (HOST_WIDE_INT))
2259 elt_size /= 2;
2260 len *= 2;
2262 for (i = 0, p = val2->v.val_vec.array;
2263 i < len;
2264 i++, p += elt_size)
2265 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2266 "fp or vector constant word %u", i);
2268 break;
2269 case dw_val_class_const_double:
2271 unsigned HOST_WIDE_INT first, second;
2272 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2274 dw2_asm_output_data (1, 2 * l, NULL);
2275 if (WORDS_BIG_ENDIAN)
2277 first = val2->v.val_double.high;
2278 second = val2->v.val_double.low;
2280 else
2282 first = val2->v.val_double.low;
2283 second = val2->v.val_double.high;
2285 dw2_asm_output_data (l, first, NULL);
2286 dw2_asm_output_data (l, second, NULL);
2288 break;
2289 case dw_val_class_wide_int:
2291 int i;
2292 int len = get_full_len (*val2->v.val_wide);
2293 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2295 dw2_asm_output_data (1, len * l, NULL);
2296 if (WORDS_BIG_ENDIAN)
2297 for (i = len - 1; i >= 0; --i)
2298 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2299 else
2300 for (i = 0; i < len; ++i)
2301 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2303 break;
2304 default:
2305 gcc_unreachable ();
2308 break;
2309 case DW_OP_regval_type:
2310 case DW_OP_GNU_regval_type:
2312 unsigned r = val1->v.val_unsigned;
2313 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2314 gcc_assert (o);
2315 if (for_eh_or_skip >= 0)
2317 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2318 gcc_assert (size_of_uleb128 (r)
2319 == size_of_uleb128 (val1->v.val_unsigned));
2321 dw2_asm_output_data_uleb128 (r, NULL);
2322 dw2_asm_output_data_uleb128 (o, NULL);
2324 break;
2325 case DW_OP_deref_type:
2326 case DW_OP_GNU_deref_type:
2328 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2329 gcc_assert (o);
2330 dw2_asm_output_data (1, val1->v.val_int, NULL);
2331 dw2_asm_output_data_uleb128 (o, NULL);
2333 break;
2334 case DW_OP_convert:
2335 case DW_OP_reinterpret:
2336 case DW_OP_GNU_convert:
2337 case DW_OP_GNU_reinterpret:
2338 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2339 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2340 else
2342 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2343 gcc_assert (o);
2344 dw2_asm_output_data_uleb128 (o, NULL);
2346 break;
2348 case DW_OP_GNU_parameter_ref:
2350 unsigned long o;
2351 gcc_assert (val1->val_class == dw_val_class_die_ref);
2352 o = get_ref_die_offset (val1->v.val_die_ref.die);
2353 dw2_asm_output_data (4, o, NULL);
2355 break;
2357 default:
2358 /* Other codes have no operands. */
2359 break;
2363 /* Output a sequence of location operations.
2364 The for_eh_or_skip parameter controls whether register numbers are
2365 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2366 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2367 info). This should be suppressed for the cases that have not been converted
2368 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2370 void
2371 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2373 for (; loc != NULL; loc = loc->dw_loc_next)
2375 enum dwarf_location_atom opc = loc->dw_loc_opc;
2376 /* Output the opcode. */
2377 if (for_eh_or_skip >= 0
2378 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2380 unsigned r = (opc - DW_OP_breg0);
2381 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2382 gcc_assert (r <= 31);
2383 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2385 else if (for_eh_or_skip >= 0
2386 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2388 unsigned r = (opc - DW_OP_reg0);
2389 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2390 gcc_assert (r <= 31);
2391 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2394 dw2_asm_output_data (1, opc,
2395 "%s", dwarf_stack_op_name (opc));
2397 /* Output the operand(s) (if any). */
2398 output_loc_operands (loc, for_eh_or_skip);
2402 /* Output location description stack opcode's operands (if any).
2403 The output is single bytes on a line, suitable for .cfi_escape. */
2405 static void
2406 output_loc_operands_raw (dw_loc_descr_ref loc)
2408 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2409 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2411 switch (loc->dw_loc_opc)
2413 case DW_OP_addr:
2414 case DW_OP_GNU_addr_index:
2415 case DW_OP_GNU_const_index:
2416 case DW_OP_implicit_value:
2417 /* We cannot output addresses in .cfi_escape, only bytes. */
2418 gcc_unreachable ();
2420 case DW_OP_const1u:
2421 case DW_OP_const1s:
2422 case DW_OP_pick:
2423 case DW_OP_deref_size:
2424 case DW_OP_xderef_size:
2425 fputc (',', asm_out_file);
2426 dw2_asm_output_data_raw (1, val1->v.val_int);
2427 break;
2429 case DW_OP_const2u:
2430 case DW_OP_const2s:
2431 fputc (',', asm_out_file);
2432 dw2_asm_output_data_raw (2, val1->v.val_int);
2433 break;
2435 case DW_OP_const4u:
2436 case DW_OP_const4s:
2437 fputc (',', asm_out_file);
2438 dw2_asm_output_data_raw (4, val1->v.val_int);
2439 break;
2441 case DW_OP_const8u:
2442 case DW_OP_const8s:
2443 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2444 fputc (',', asm_out_file);
2445 dw2_asm_output_data_raw (8, val1->v.val_int);
2446 break;
2448 case DW_OP_skip:
2449 case DW_OP_bra:
2451 int offset;
2453 gcc_assert (val1->val_class == dw_val_class_loc);
2454 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2456 fputc (',', asm_out_file);
2457 dw2_asm_output_data_raw (2, offset);
2459 break;
2461 case DW_OP_regx:
2463 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2464 gcc_assert (size_of_uleb128 (r)
2465 == size_of_uleb128 (val1->v.val_unsigned));
2466 fputc (',', asm_out_file);
2467 dw2_asm_output_data_uleb128_raw (r);
2469 break;
2471 case DW_OP_constu:
2472 case DW_OP_plus_uconst:
2473 case DW_OP_piece:
2474 fputc (',', asm_out_file);
2475 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2476 break;
2478 case DW_OP_bit_piece:
2479 fputc (',', asm_out_file);
2480 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2481 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2482 break;
2484 case DW_OP_consts:
2485 case DW_OP_breg0:
2486 case DW_OP_breg1:
2487 case DW_OP_breg2:
2488 case DW_OP_breg3:
2489 case DW_OP_breg4:
2490 case DW_OP_breg5:
2491 case DW_OP_breg6:
2492 case DW_OP_breg7:
2493 case DW_OP_breg8:
2494 case DW_OP_breg9:
2495 case DW_OP_breg10:
2496 case DW_OP_breg11:
2497 case DW_OP_breg12:
2498 case DW_OP_breg13:
2499 case DW_OP_breg14:
2500 case DW_OP_breg15:
2501 case DW_OP_breg16:
2502 case DW_OP_breg17:
2503 case DW_OP_breg18:
2504 case DW_OP_breg19:
2505 case DW_OP_breg20:
2506 case DW_OP_breg21:
2507 case DW_OP_breg22:
2508 case DW_OP_breg23:
2509 case DW_OP_breg24:
2510 case DW_OP_breg25:
2511 case DW_OP_breg26:
2512 case DW_OP_breg27:
2513 case DW_OP_breg28:
2514 case DW_OP_breg29:
2515 case DW_OP_breg30:
2516 case DW_OP_breg31:
2517 case DW_OP_fbreg:
2518 fputc (',', asm_out_file);
2519 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2520 break;
2522 case DW_OP_bregx:
2524 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2525 gcc_assert (size_of_uleb128 (r)
2526 == size_of_uleb128 (val1->v.val_unsigned));
2527 fputc (',', asm_out_file);
2528 dw2_asm_output_data_uleb128_raw (r);
2529 fputc (',', asm_out_file);
2530 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2532 break;
2534 case DW_OP_implicit_pointer:
2535 case DW_OP_entry_value:
2536 case DW_OP_const_type:
2537 case DW_OP_regval_type:
2538 case DW_OP_deref_type:
2539 case DW_OP_convert:
2540 case DW_OP_reinterpret:
2541 case DW_OP_GNU_implicit_pointer:
2542 case DW_OP_GNU_entry_value:
2543 case DW_OP_GNU_const_type:
2544 case DW_OP_GNU_regval_type:
2545 case DW_OP_GNU_deref_type:
2546 case DW_OP_GNU_convert:
2547 case DW_OP_GNU_reinterpret:
2548 case DW_OP_GNU_parameter_ref:
2549 gcc_unreachable ();
2550 break;
2552 default:
2553 /* Other codes have no operands. */
2554 break;
2558 void
2559 output_loc_sequence_raw (dw_loc_descr_ref loc)
2561 while (1)
2563 enum dwarf_location_atom opc = loc->dw_loc_opc;
2564 /* Output the opcode. */
2565 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2567 unsigned r = (opc - DW_OP_breg0);
2568 r = DWARF2_FRAME_REG_OUT (r, 1);
2569 gcc_assert (r <= 31);
2570 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2572 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2574 unsigned r = (opc - DW_OP_reg0);
2575 r = DWARF2_FRAME_REG_OUT (r, 1);
2576 gcc_assert (r <= 31);
2577 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2579 /* Output the opcode. */
2580 fprintf (asm_out_file, "%#x", opc);
2581 output_loc_operands_raw (loc);
2583 if (!loc->dw_loc_next)
2584 break;
2585 loc = loc->dw_loc_next;
2587 fputc (',', asm_out_file);
2591 /* This function builds a dwarf location descriptor sequence from a
2592 dw_cfa_location, adding the given OFFSET to the result of the
2593 expression. */
2595 struct dw_loc_descr_node *
2596 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2598 struct dw_loc_descr_node *head, *tmp;
2600 offset += cfa->offset;
2602 if (cfa->indirect)
2604 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2605 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2606 head->dw_loc_oprnd1.val_entry = NULL;
2607 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2608 add_loc_descr (&head, tmp);
2609 if (offset != 0)
2611 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2612 add_loc_descr (&head, tmp);
2615 else
2616 head = new_reg_loc_descr (cfa->reg, offset);
2618 return head;
2621 /* This function builds a dwarf location descriptor sequence for
2622 the address at OFFSET from the CFA when stack is aligned to
2623 ALIGNMENT byte. */
2625 struct dw_loc_descr_node *
2626 build_cfa_aligned_loc (dw_cfa_location *cfa,
2627 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2629 struct dw_loc_descr_node *head;
2630 unsigned int dwarf_fp
2631 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2633 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2634 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2636 head = new_reg_loc_descr (dwarf_fp, 0);
2637 add_loc_descr (&head, int_loc_descriptor (alignment));
2638 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2639 loc_descr_plus_const (&head, offset);
2641 else
2642 head = new_reg_loc_descr (dwarf_fp, offset);
2643 return head;
2646 /* And now, the support for symbolic debugging information. */
2648 /* .debug_str support. */
2650 static void dwarf2out_init (const char *);
2651 static void dwarf2out_finish (const char *);
2652 static void dwarf2out_early_finish (const char *);
2653 static void dwarf2out_assembly_start (void);
2654 static void dwarf2out_define (unsigned int, const char *);
2655 static void dwarf2out_undef (unsigned int, const char *);
2656 static void dwarf2out_start_source_file (unsigned, const char *);
2657 static void dwarf2out_end_source_file (unsigned);
2658 static void dwarf2out_function_decl (tree);
2659 static void dwarf2out_begin_block (unsigned, unsigned);
2660 static void dwarf2out_end_block (unsigned, unsigned);
2661 static bool dwarf2out_ignore_block (const_tree);
2662 static void dwarf2out_early_global_decl (tree);
2663 static void dwarf2out_late_global_decl (tree);
2664 static void dwarf2out_type_decl (tree, int);
2665 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2666 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2667 dw_die_ref);
2668 static void dwarf2out_abstract_function (tree);
2669 static void dwarf2out_var_location (rtx_insn *);
2670 static void dwarf2out_size_function (tree);
2671 static void dwarf2out_begin_function (tree);
2672 static void dwarf2out_end_function (unsigned int);
2673 static void dwarf2out_register_main_translation_unit (tree unit);
2674 static void dwarf2out_set_name (tree, tree);
2676 /* The debug hooks structure. */
2678 const struct gcc_debug_hooks dwarf2_debug_hooks =
2680 dwarf2out_init,
2681 dwarf2out_finish,
2682 dwarf2out_early_finish,
2683 dwarf2out_assembly_start,
2684 dwarf2out_define,
2685 dwarf2out_undef,
2686 dwarf2out_start_source_file,
2687 dwarf2out_end_source_file,
2688 dwarf2out_begin_block,
2689 dwarf2out_end_block,
2690 dwarf2out_ignore_block,
2691 dwarf2out_source_line,
2692 dwarf2out_begin_prologue,
2693 #if VMS_DEBUGGING_INFO
2694 dwarf2out_vms_end_prologue,
2695 dwarf2out_vms_begin_epilogue,
2696 #else
2697 debug_nothing_int_charstar,
2698 debug_nothing_int_charstar,
2699 #endif
2700 dwarf2out_end_epilogue,
2701 dwarf2out_begin_function,
2702 dwarf2out_end_function, /* end_function */
2703 dwarf2out_register_main_translation_unit,
2704 dwarf2out_function_decl, /* function_decl */
2705 dwarf2out_early_global_decl,
2706 dwarf2out_late_global_decl,
2707 dwarf2out_type_decl, /* type_decl */
2708 dwarf2out_imported_module_or_decl,
2709 debug_nothing_tree, /* deferred_inline_function */
2710 /* The DWARF 2 backend tries to reduce debugging bloat by not
2711 emitting the abstract description of inline functions until
2712 something tries to reference them. */
2713 dwarf2out_abstract_function, /* outlining_inline_function */
2714 debug_nothing_rtx_code_label, /* label */
2715 debug_nothing_int, /* handle_pch */
2716 dwarf2out_var_location,
2717 dwarf2out_size_function, /* size_function */
2718 dwarf2out_switch_text_section,
2719 dwarf2out_set_name,
2720 1, /* start_end_main_source_file */
2721 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2724 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2726 dwarf2out_init,
2727 debug_nothing_charstar,
2728 debug_nothing_charstar,
2729 dwarf2out_assembly_start,
2730 debug_nothing_int_charstar,
2731 debug_nothing_int_charstar,
2732 debug_nothing_int_charstar,
2733 debug_nothing_int,
2734 debug_nothing_int_int, /* begin_block */
2735 debug_nothing_int_int, /* end_block */
2736 debug_true_const_tree, /* ignore_block */
2737 dwarf2out_source_line, /* source_line */
2738 debug_nothing_int_int_charstar, /* begin_prologue */
2739 debug_nothing_int_charstar, /* end_prologue */
2740 debug_nothing_int_charstar, /* begin_epilogue */
2741 debug_nothing_int_charstar, /* end_epilogue */
2742 debug_nothing_tree, /* begin_function */
2743 debug_nothing_int, /* end_function */
2744 debug_nothing_tree, /* register_main_translation_unit */
2745 debug_nothing_tree, /* function_decl */
2746 debug_nothing_tree, /* early_global_decl */
2747 debug_nothing_tree, /* late_global_decl */
2748 debug_nothing_tree_int, /* type_decl */
2749 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2750 debug_nothing_tree, /* deferred_inline_function */
2751 debug_nothing_tree, /* outlining_inline_function */
2752 debug_nothing_rtx_code_label, /* label */
2753 debug_nothing_int, /* handle_pch */
2754 debug_nothing_rtx_insn, /* var_location */
2755 debug_nothing_tree, /* size_function */
2756 debug_nothing_void, /* switch_text_section */
2757 debug_nothing_tree_tree, /* set_name */
2758 0, /* start_end_main_source_file */
2759 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2762 /* NOTE: In the comments in this file, many references are made to
2763 "Debugging Information Entries". This term is abbreviated as `DIE'
2764 throughout the remainder of this file. */
2766 /* An internal representation of the DWARF output is built, and then
2767 walked to generate the DWARF debugging info. The walk of the internal
2768 representation is done after the entire program has been compiled.
2769 The types below are used to describe the internal representation. */
2771 /* Whether to put type DIEs into their own section .debug_types instead
2772 of making them part of the .debug_info section. Only supported for
2773 Dwarf V4 or higher and the user didn't disable them through
2774 -fno-debug-types-section. It is more efficient to put them in a
2775 separate comdat sections since the linker will then be able to
2776 remove duplicates. But not all tools support .debug_types sections
2777 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2778 it is DW_UT_type unit type in .debug_info section. */
2780 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2782 /* Various DIE's use offsets relative to the beginning of the
2783 .debug_info section to refer to each other. */
2785 typedef long int dw_offset;
2787 struct comdat_type_node;
2789 /* The entries in the line_info table more-or-less mirror the opcodes
2790 that are used in the real dwarf line table. Arrays of these entries
2791 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2792 supported. */
2794 enum dw_line_info_opcode {
2795 /* Emit DW_LNE_set_address; the operand is the label index. */
2796 LI_set_address,
2798 /* Emit a row to the matrix with the given line. This may be done
2799 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2800 special opcodes. */
2801 LI_set_line,
2803 /* Emit a DW_LNS_set_file. */
2804 LI_set_file,
2806 /* Emit a DW_LNS_set_column. */
2807 LI_set_column,
2809 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2810 LI_negate_stmt,
2812 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2813 LI_set_prologue_end,
2814 LI_set_epilogue_begin,
2816 /* Emit a DW_LNE_set_discriminator. */
2817 LI_set_discriminator
2820 typedef struct GTY(()) dw_line_info_struct {
2821 enum dw_line_info_opcode opcode;
2822 unsigned int val;
2823 } dw_line_info_entry;
2826 struct GTY(()) dw_line_info_table {
2827 /* The label that marks the end of this section. */
2828 const char *end_label;
2830 /* The values for the last row of the matrix, as collected in the table.
2831 These are used to minimize the changes to the next row. */
2832 unsigned int file_num;
2833 unsigned int line_num;
2834 unsigned int column_num;
2835 int discrim_num;
2836 bool is_stmt;
2837 bool in_use;
2839 vec<dw_line_info_entry, va_gc> *entries;
2843 /* Each DIE attribute has a field specifying the attribute kind,
2844 a link to the next attribute in the chain, and an attribute value.
2845 Attributes are typically linked below the DIE they modify. */
2847 typedef struct GTY(()) dw_attr_struct {
2848 enum dwarf_attribute dw_attr;
2849 dw_val_node dw_attr_val;
2851 dw_attr_node;
2854 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2855 The children of each node form a circular list linked by
2856 die_sib. die_child points to the node *before* the "first" child node. */
2858 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2859 union die_symbol_or_type_node
2861 const char * GTY ((tag ("0"))) die_symbol;
2862 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2864 GTY ((desc ("%0.comdat_type_p"))) die_id;
2865 vec<dw_attr_node, va_gc> *die_attr;
2866 dw_die_ref die_parent;
2867 dw_die_ref die_child;
2868 dw_die_ref die_sib;
2869 dw_die_ref die_definition; /* ref from a specification to its definition */
2870 dw_offset die_offset;
2871 unsigned long die_abbrev;
2872 int die_mark;
2873 unsigned int decl_id;
2874 enum dwarf_tag die_tag;
2875 /* Die is used and must not be pruned as unused. */
2876 BOOL_BITFIELD die_perennial_p : 1;
2877 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2878 /* Whether this DIE was removed from the DIE tree, for example via
2879 prune_unused_types. We don't consider those present from the
2880 DIE lookup routines. */
2881 BOOL_BITFIELD removed : 1;
2882 /* Lots of spare bits. */
2884 die_node;
2886 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2887 static bool early_dwarf;
2888 static bool early_dwarf_finished;
2889 struct set_early_dwarf {
2890 bool saved;
2891 set_early_dwarf () : saved(early_dwarf)
2893 gcc_assert (! early_dwarf_finished);
2894 early_dwarf = true;
2896 ~set_early_dwarf () { early_dwarf = saved; }
2899 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2900 #define FOR_EACH_CHILD(die, c, expr) do { \
2901 c = die->die_child; \
2902 if (c) do { \
2903 c = c->die_sib; \
2904 expr; \
2905 } while (c != die->die_child); \
2906 } while (0)
2908 /* The pubname structure */
2910 typedef struct GTY(()) pubname_struct {
2911 dw_die_ref die;
2912 const char *name;
2914 pubname_entry;
2917 struct GTY(()) dw_ranges {
2918 const char *label;
2919 /* If this is positive, it's a block number, otherwise it's a
2920 bitwise-negated index into dw_ranges_by_label. */
2921 int num;
2922 /* Index for the range list for DW_FORM_rnglistx. */
2923 unsigned int idx : 31;
2924 /* True if this range might be possibly in a different section
2925 from previous entry. */
2926 unsigned int maybe_new_sec : 1;
2929 /* A structure to hold a macinfo entry. */
2931 typedef struct GTY(()) macinfo_struct {
2932 unsigned char code;
2933 unsigned HOST_WIDE_INT lineno;
2934 const char *info;
2936 macinfo_entry;
2939 struct GTY(()) dw_ranges_by_label {
2940 const char *begin;
2941 const char *end;
2944 /* The comdat type node structure. */
2945 struct GTY(()) comdat_type_node
2947 dw_die_ref root_die;
2948 dw_die_ref type_die;
2949 dw_die_ref skeleton_die;
2950 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2951 comdat_type_node *next;
2954 /* A list of DIEs for which we can't determine ancestry (parent_die
2955 field) just yet. Later in dwarf2out_finish we will fill in the
2956 missing bits. */
2957 typedef struct GTY(()) limbo_die_struct {
2958 dw_die_ref die;
2959 /* The tree for which this DIE was created. We use this to
2960 determine ancestry later. */
2961 tree created_for;
2962 struct limbo_die_struct *next;
2964 limbo_die_node;
2966 typedef struct skeleton_chain_struct
2968 dw_die_ref old_die;
2969 dw_die_ref new_die;
2970 struct skeleton_chain_struct *parent;
2972 skeleton_chain_node;
2974 /* Define a macro which returns nonzero for a TYPE_DECL which was
2975 implicitly generated for a type.
2977 Note that, unlike the C front-end (which generates a NULL named
2978 TYPE_DECL node for each complete tagged type, each array type,
2979 and each function type node created) the C++ front-end generates
2980 a _named_ TYPE_DECL node for each tagged type node created.
2981 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2982 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2983 front-end, but for each type, tagged or not. */
2985 #define TYPE_DECL_IS_STUB(decl) \
2986 (DECL_NAME (decl) == NULL_TREE \
2987 || (DECL_ARTIFICIAL (decl) \
2988 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2989 /* This is necessary for stub decls that \
2990 appear in nested inline functions. */ \
2991 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2992 && (decl_ultimate_origin (decl) \
2993 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2995 /* Information concerning the compilation unit's programming
2996 language, and compiler version. */
2998 /* Fixed size portion of the DWARF compilation unit header. */
2999 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3000 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3001 + (dwarf_version >= 5 ? 4 : 3))
3003 /* Fixed size portion of the DWARF comdat type unit header. */
3004 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3005 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3006 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3008 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3009 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3010 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3012 /* Fixed size portion of public names info. */
3013 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3015 /* Fixed size portion of the address range info. */
3016 #define DWARF_ARANGES_HEADER_SIZE \
3017 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3018 DWARF2_ADDR_SIZE * 2) \
3019 - DWARF_INITIAL_LENGTH_SIZE)
3021 /* Size of padding portion in the address range info. It must be
3022 aligned to twice the pointer size. */
3023 #define DWARF_ARANGES_PAD_SIZE \
3024 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3025 DWARF2_ADDR_SIZE * 2) \
3026 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3028 /* Use assembler line directives if available. */
3029 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3030 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3031 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3032 #else
3033 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3034 #endif
3035 #endif
3037 /* Minimum line offset in a special line info. opcode.
3038 This value was chosen to give a reasonable range of values. */
3039 #define DWARF_LINE_BASE -10
3041 /* First special line opcode - leave room for the standard opcodes. */
3042 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3044 /* Range of line offsets in a special line info. opcode. */
3045 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3047 /* Flag that indicates the initial value of the is_stmt_start flag.
3048 In the present implementation, we do not mark any lines as
3049 the beginning of a source statement, because that information
3050 is not made available by the GCC front-end. */
3051 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3053 /* Maximum number of operations per instruction bundle. */
3054 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3055 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3056 #endif
3058 /* This location is used by calc_die_sizes() to keep track
3059 the offset of each DIE within the .debug_info section. */
3060 static unsigned long next_die_offset;
3062 /* Record the root of the DIE's built for the current compilation unit. */
3063 static GTY(()) dw_die_ref single_comp_unit_die;
3065 /* A list of type DIEs that have been separated into comdat sections. */
3066 static GTY(()) comdat_type_node *comdat_type_list;
3068 /* A list of CU DIEs that have been separated. */
3069 static GTY(()) limbo_die_node *cu_die_list;
3071 /* A list of DIEs with a NULL parent waiting to be relocated. */
3072 static GTY(()) limbo_die_node *limbo_die_list;
3074 /* A list of DIEs for which we may have to generate
3075 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3076 static GTY(()) limbo_die_node *deferred_asm_name;
3078 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3080 typedef const char *compare_type;
3082 static hashval_t hash (dwarf_file_data *);
3083 static bool equal (dwarf_file_data *, const char *);
3086 /* Filenames referenced by this compilation unit. */
3087 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3089 struct decl_die_hasher : ggc_ptr_hash<die_node>
3091 typedef tree compare_type;
3093 static hashval_t hash (die_node *);
3094 static bool equal (die_node *, tree);
3096 /* A hash table of references to DIE's that describe declarations.
3097 The key is a DECL_UID() which is a unique number identifying each decl. */
3098 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3100 struct block_die_hasher : ggc_ptr_hash<die_struct>
3102 static hashval_t hash (die_struct *);
3103 static bool equal (die_struct *, die_struct *);
3106 /* A hash table of references to DIE's that describe COMMON blocks.
3107 The key is DECL_UID() ^ die_parent. */
3108 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3110 typedef struct GTY(()) die_arg_entry_struct {
3111 dw_die_ref die;
3112 tree arg;
3113 } die_arg_entry;
3116 /* Node of the variable location list. */
3117 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3118 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3119 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3120 in mode of the EXPR_LIST node and first EXPR_LIST operand
3121 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3122 location or NULL for padding. For larger bitsizes,
3123 mode is 0 and first operand is a CONCAT with bitsize
3124 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3125 NULL as second operand. */
3126 rtx GTY (()) loc;
3127 const char * GTY (()) label;
3128 struct var_loc_node * GTY (()) next;
3131 /* Variable location list. */
3132 struct GTY ((for_user)) var_loc_list_def {
3133 struct var_loc_node * GTY (()) first;
3135 /* Pointer to the last but one or last element of the
3136 chained list. If the list is empty, both first and
3137 last are NULL, if the list contains just one node
3138 or the last node certainly is not redundant, it points
3139 to the last node, otherwise points to the last but one.
3140 Do not mark it for GC because it is marked through the chain. */
3141 struct var_loc_node * GTY ((skip ("%h"))) last;
3143 /* Pointer to the last element before section switch,
3144 if NULL, either sections weren't switched or first
3145 is after section switch. */
3146 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3148 /* DECL_UID of the variable decl. */
3149 unsigned int decl_id;
3151 typedef struct var_loc_list_def var_loc_list;
3153 /* Call argument location list. */
3154 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3155 rtx GTY (()) call_arg_loc_note;
3156 const char * GTY (()) label;
3157 tree GTY (()) block;
3158 bool tail_call_p;
3159 rtx GTY (()) symbol_ref;
3160 struct call_arg_loc_node * GTY (()) next;
3164 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3166 typedef const_tree compare_type;
3168 static hashval_t hash (var_loc_list *);
3169 static bool equal (var_loc_list *, const_tree);
3172 /* Table of decl location linked lists. */
3173 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3175 /* Head and tail of call_arg_loc chain. */
3176 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3177 static struct call_arg_loc_node *call_arg_loc_last;
3179 /* Number of call sites in the current function. */
3180 static int call_site_count = -1;
3181 /* Number of tail call sites in the current function. */
3182 static int tail_call_site_count = -1;
3184 /* A cached location list. */
3185 struct GTY ((for_user)) cached_dw_loc_list_def {
3186 /* The DECL_UID of the decl that this entry describes. */
3187 unsigned int decl_id;
3189 /* The cached location list. */
3190 dw_loc_list_ref loc_list;
3192 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3194 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3197 typedef const_tree compare_type;
3199 static hashval_t hash (cached_dw_loc_list *);
3200 static bool equal (cached_dw_loc_list *, const_tree);
3203 /* Table of cached location lists. */
3204 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3206 /* A vector of references to DIE's that are uniquely identified by their tag,
3207 presence/absence of children DIE's, and list of attribute/value pairs. */
3208 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3210 /* A hash map to remember the stack usage for DWARF procedures. The value
3211 stored is the stack size difference between before the DWARF procedure
3212 invokation and after it returned. In other words, for a DWARF procedure
3213 that consumes N stack slots and that pushes M ones, this stores M - N. */
3214 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3216 /* A global counter for generating labels for line number data. */
3217 static unsigned int line_info_label_num;
3219 /* The current table to which we should emit line number information
3220 for the current function. This will be set up at the beginning of
3221 assembly for the function. */
3222 static GTY(()) dw_line_info_table *cur_line_info_table;
3224 /* The two default tables of line number info. */
3225 static GTY(()) dw_line_info_table *text_section_line_info;
3226 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3228 /* The set of all non-default tables of line number info. */
3229 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3231 /* A flag to tell pubnames/types export if there is an info section to
3232 refer to. */
3233 static bool info_section_emitted;
3235 /* A pointer to the base of a table that contains a list of publicly
3236 accessible names. */
3237 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3239 /* A pointer to the base of a table that contains a list of publicly
3240 accessible types. */
3241 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3243 /* A pointer to the base of a table that contains a list of macro
3244 defines/undefines (and file start/end markers). */
3245 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3247 /* True if .debug_macinfo or .debug_macros section is going to be
3248 emitted. */
3249 #define have_macinfo \
3250 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3251 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3252 && !macinfo_table->is_empty ())
3254 /* Vector of dies for which we should generate .debug_ranges info. */
3255 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3257 /* Vector of pairs of labels referenced in ranges_table. */
3258 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3260 /* Whether we have location lists that need outputting */
3261 static GTY(()) bool have_location_lists;
3263 /* Unique label counter. */
3264 static GTY(()) unsigned int loclabel_num;
3266 /* Unique label counter for point-of-call tables. */
3267 static GTY(()) unsigned int poc_label_num;
3269 /* The last file entry emitted by maybe_emit_file(). */
3270 static GTY(()) struct dwarf_file_data * last_emitted_file;
3272 /* Number of internal labels generated by gen_internal_sym(). */
3273 static GTY(()) int label_num;
3275 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3277 /* Instances of generic types for which we need to generate debug
3278 info that describe their generic parameters and arguments. That
3279 generation needs to happen once all types are properly laid out so
3280 we do it at the end of compilation. */
3281 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3283 /* Offset from the "steady-state frame pointer" to the frame base,
3284 within the current function. */
3285 static HOST_WIDE_INT frame_pointer_fb_offset;
3286 static bool frame_pointer_fb_offset_valid;
3288 static vec<dw_die_ref> base_types;
3290 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3291 once all arguments are parsed. */
3292 static vec<dw_die_ref> *string_types;
3294 /* Flags to represent a set of attribute classes for attributes that represent
3295 a scalar value (bounds, pointers, ...). */
3296 enum dw_scalar_form
3298 dw_scalar_form_constant = 0x01,
3299 dw_scalar_form_exprloc = 0x02,
3300 dw_scalar_form_reference = 0x04
3303 /* Forward declarations for functions defined in this file. */
3305 static int is_pseudo_reg (const_rtx);
3306 static tree type_main_variant (tree);
3307 static int is_tagged_type (const_tree);
3308 static const char *dwarf_tag_name (unsigned);
3309 static const char *dwarf_attr_name (unsigned);
3310 static const char *dwarf_form_name (unsigned);
3311 static tree decl_ultimate_origin (const_tree);
3312 static tree decl_class_context (tree);
3313 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3314 static inline enum dw_val_class AT_class (dw_attr_node *);
3315 static inline unsigned int AT_index (dw_attr_node *);
3316 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3317 static inline unsigned AT_flag (dw_attr_node *);
3318 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3319 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3320 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3321 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3322 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3323 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3324 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3325 unsigned int, unsigned char *);
3326 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3327 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3328 static inline const char *AT_string (dw_attr_node *);
3329 static enum dwarf_form AT_string_form (dw_attr_node *);
3330 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3331 static void add_AT_specification (dw_die_ref, dw_die_ref);
3332 static inline dw_die_ref AT_ref (dw_attr_node *);
3333 static inline int AT_ref_external (dw_attr_node *);
3334 static inline void set_AT_ref_external (dw_attr_node *, int);
3335 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3336 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3337 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3338 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3339 dw_loc_list_ref);
3340 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3341 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3342 static void remove_addr_table_entry (addr_table_entry *);
3343 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3344 static inline rtx AT_addr (dw_attr_node *);
3345 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3346 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3347 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3348 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3349 const char *);
3350 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3351 unsigned HOST_WIDE_INT);
3352 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3353 unsigned long, bool);
3354 static inline const char *AT_lbl (dw_attr_node *);
3355 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3356 static const char *get_AT_low_pc (dw_die_ref);
3357 static const char *get_AT_hi_pc (dw_die_ref);
3358 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3359 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3360 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3361 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3362 static bool is_cxx (void);
3363 static bool is_cxx (const_tree);
3364 static bool is_fortran (void);
3365 static bool is_ada (void);
3366 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3367 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3368 static void add_child_die (dw_die_ref, dw_die_ref);
3369 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3370 static dw_die_ref lookup_type_die (tree);
3371 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3372 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3373 static void equate_type_number_to_die (tree, dw_die_ref);
3374 static dw_die_ref lookup_decl_die (tree);
3375 static var_loc_list *lookup_decl_loc (const_tree);
3376 static void equate_decl_number_to_die (tree, dw_die_ref);
3377 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3378 static void print_spaces (FILE *);
3379 static void print_die (dw_die_ref, FILE *);
3380 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3381 static dw_die_ref pop_compile_unit (dw_die_ref);
3382 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3383 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3384 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3385 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3386 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3387 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3388 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3389 struct md5_ctx *, int *);
3390 struct checksum_attributes;
3391 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3392 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3393 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3394 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3395 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3396 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3397 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3398 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3399 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3400 static void compute_section_prefix (dw_die_ref);
3401 static int is_type_die (dw_die_ref);
3402 static int is_comdat_die (dw_die_ref);
3403 static int is_symbol_die (dw_die_ref);
3404 static inline bool is_template_instantiation (dw_die_ref);
3405 static void assign_symbol_names (dw_die_ref);
3406 static void break_out_includes (dw_die_ref);
3407 static int is_declaration_die (dw_die_ref);
3408 static int should_move_die_to_comdat (dw_die_ref);
3409 static dw_die_ref clone_as_declaration (dw_die_ref);
3410 static dw_die_ref clone_die (dw_die_ref);
3411 static dw_die_ref clone_tree (dw_die_ref);
3412 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3413 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3414 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3415 static dw_die_ref generate_skeleton (dw_die_ref);
3416 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3417 dw_die_ref,
3418 dw_die_ref);
3419 static void break_out_comdat_types (dw_die_ref);
3420 static void copy_decls_for_unworthy_types (dw_die_ref);
3422 static void add_sibling_attributes (dw_die_ref);
3423 static void output_location_lists (dw_die_ref);
3424 static int constant_size (unsigned HOST_WIDE_INT);
3425 static unsigned long size_of_die (dw_die_ref);
3426 static void calc_die_sizes (dw_die_ref);
3427 static void calc_base_type_die_sizes (void);
3428 static void mark_dies (dw_die_ref);
3429 static void unmark_dies (dw_die_ref);
3430 static void unmark_all_dies (dw_die_ref);
3431 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3432 static unsigned long size_of_aranges (void);
3433 static enum dwarf_form value_format (dw_attr_node *);
3434 static void output_value_format (dw_attr_node *);
3435 static void output_abbrev_section (void);
3436 static void output_die_abbrevs (unsigned long, dw_die_ref);
3437 static void output_die_symbol (dw_die_ref);
3438 static void output_die (dw_die_ref);
3439 static void output_compilation_unit_header (enum dwarf_unit_type);
3440 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3441 static void output_comdat_type_unit (comdat_type_node *);
3442 static const char *dwarf2_name (tree, int);
3443 static void add_pubname (tree, dw_die_ref);
3444 static void add_enumerator_pubname (const char *, dw_die_ref);
3445 static void add_pubname_string (const char *, dw_die_ref);
3446 static void add_pubtype (tree, dw_die_ref);
3447 static void output_pubnames (vec<pubname_entry, va_gc> *);
3448 static void output_aranges (void);
3449 static unsigned int add_ranges (const_tree, bool = false);
3450 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3451 bool *, bool);
3452 static void output_ranges (void);
3453 static dw_line_info_table *new_line_info_table (void);
3454 static void output_line_info (bool);
3455 static void output_file_names (void);
3456 static dw_die_ref base_type_die (tree, bool);
3457 static int is_base_type (tree);
3458 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3459 static int decl_quals (const_tree);
3460 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3461 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3462 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3463 static int type_is_enum (const_tree);
3464 static unsigned int dbx_reg_number (const_rtx);
3465 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3466 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3467 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3468 enum var_init_status);
3469 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3470 enum var_init_status);
3471 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3472 enum var_init_status);
3473 static int is_based_loc (const_rtx);
3474 static bool resolve_one_addr (rtx *);
3475 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3476 enum var_init_status);
3477 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3478 enum var_init_status);
3479 struct loc_descr_context;
3480 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3481 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3482 static dw_loc_list_ref loc_list_from_tree (tree, int,
3483 struct loc_descr_context *);
3484 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3485 struct loc_descr_context *);
3486 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3487 static tree field_type (const_tree);
3488 static unsigned int simple_type_align_in_bits (const_tree);
3489 static unsigned int simple_decl_align_in_bits (const_tree);
3490 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3491 struct vlr_context;
3492 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3493 HOST_WIDE_INT *);
3494 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3495 dw_loc_list_ref);
3496 static void add_data_member_location_attribute (dw_die_ref, tree,
3497 struct vlr_context *);
3498 static bool add_const_value_attribute (dw_die_ref, rtx);
3499 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3500 static void insert_wide_int (const wide_int &, unsigned char *, int);
3501 static void insert_float (const_rtx, unsigned char *);
3502 static rtx rtl_for_decl_location (tree);
3503 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3504 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3505 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3506 static void add_name_attribute (dw_die_ref, const char *);
3507 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3508 static void add_comp_dir_attribute (dw_die_ref);
3509 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3510 struct loc_descr_context *);
3511 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3512 struct loc_descr_context *);
3513 static void add_subscript_info (dw_die_ref, tree, bool);
3514 static void add_byte_size_attribute (dw_die_ref, tree);
3515 static void add_alignment_attribute (dw_die_ref, tree);
3516 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3517 struct vlr_context *);
3518 static void add_bit_size_attribute (dw_die_ref, tree);
3519 static void add_prototyped_attribute (dw_die_ref, tree);
3520 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3521 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3522 static void add_src_coords_attributes (dw_die_ref, tree);
3523 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3524 static void add_discr_value (dw_die_ref, dw_discr_value *);
3525 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3526 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3527 static void push_decl_scope (tree);
3528 static void pop_decl_scope (void);
3529 static dw_die_ref scope_die_for (tree, dw_die_ref);
3530 static inline int local_scope_p (dw_die_ref);
3531 static inline int class_scope_p (dw_die_ref);
3532 static inline int class_or_namespace_scope_p (dw_die_ref);
3533 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3534 static void add_calling_convention_attribute (dw_die_ref, tree);
3535 static const char *type_tag (const_tree);
3536 static tree member_declared_type (const_tree);
3537 #if 0
3538 static const char *decl_start_label (tree);
3539 #endif
3540 static void gen_array_type_die (tree, dw_die_ref);
3541 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3542 #if 0
3543 static void gen_entry_point_die (tree, dw_die_ref);
3544 #endif
3545 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3546 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3547 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3548 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3549 static void gen_formal_types_die (tree, dw_die_ref);
3550 static void gen_subprogram_die (tree, dw_die_ref);
3551 static void gen_variable_die (tree, tree, dw_die_ref);
3552 static void gen_const_die (tree, dw_die_ref);
3553 static void gen_label_die (tree, dw_die_ref);
3554 static void gen_lexical_block_die (tree, dw_die_ref);
3555 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3556 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3557 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3558 static dw_die_ref gen_compile_unit_die (const char *);
3559 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3560 static void gen_member_die (tree, dw_die_ref);
3561 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3562 enum debug_info_usage);
3563 static void gen_subroutine_type_die (tree, dw_die_ref);
3564 static void gen_typedef_die (tree, dw_die_ref);
3565 static void gen_type_die (tree, dw_die_ref);
3566 static void gen_block_die (tree, dw_die_ref);
3567 static void decls_for_scope (tree, dw_die_ref);
3568 static bool is_naming_typedef_decl (const_tree);
3569 static inline dw_die_ref get_context_die (tree);
3570 static void gen_namespace_die (tree, dw_die_ref);
3571 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3572 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3573 static dw_die_ref force_decl_die (tree);
3574 static dw_die_ref force_type_die (tree);
3575 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3576 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3577 static struct dwarf_file_data * lookup_filename (const char *);
3578 static void retry_incomplete_types (void);
3579 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3580 static void gen_generic_params_dies (tree);
3581 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3582 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3583 static void splice_child_die (dw_die_ref, dw_die_ref);
3584 static int file_info_cmp (const void *, const void *);
3585 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3586 const char *, const char *);
3587 static void output_loc_list (dw_loc_list_ref);
3588 static char *gen_internal_sym (const char *);
3589 static bool want_pubnames (void);
3591 static void prune_unmark_dies (dw_die_ref);
3592 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3593 static void prune_unused_types_mark (dw_die_ref, int);
3594 static void prune_unused_types_walk (dw_die_ref);
3595 static void prune_unused_types_walk_attribs (dw_die_ref);
3596 static void prune_unused_types_prune (dw_die_ref);
3597 static void prune_unused_types (void);
3598 static int maybe_emit_file (struct dwarf_file_data *fd);
3599 static inline const char *AT_vms_delta1 (dw_attr_node *);
3600 static inline const char *AT_vms_delta2 (dw_attr_node *);
3601 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3602 const char *, const char *);
3603 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3604 static void gen_remaining_tmpl_value_param_die_attribute (void);
3605 static bool generic_type_p (tree);
3606 static void schedule_generic_params_dies_gen (tree t);
3607 static void gen_scheduled_generic_parms_dies (void);
3609 static const char *comp_dir_string (void);
3611 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3613 /* enum for tracking thread-local variables whose address is really an offset
3614 relative to the TLS pointer, which will need link-time relocation, but will
3615 not need relocation by the DWARF consumer. */
3617 enum dtprel_bool
3619 dtprel_false = 0,
3620 dtprel_true = 1
3623 /* Return the operator to use for an address of a variable. For dtprel_true, we
3624 use DW_OP_const*. For regular variables, which need both link-time
3625 relocation and consumer-level relocation (e.g., to account for shared objects
3626 loaded at a random address), we use DW_OP_addr*. */
3628 static inline enum dwarf_location_atom
3629 dw_addr_op (enum dtprel_bool dtprel)
3631 if (dtprel == dtprel_true)
3632 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3633 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3634 else
3635 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3638 /* Return a pointer to a newly allocated address location description. If
3639 dwarf_split_debug_info is true, then record the address with the appropriate
3640 relocation. */
3641 static inline dw_loc_descr_ref
3642 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3644 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3646 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3647 ref->dw_loc_oprnd1.v.val_addr = addr;
3648 ref->dtprel = dtprel;
3649 if (dwarf_split_debug_info)
3650 ref->dw_loc_oprnd1.val_entry
3651 = add_addr_table_entry (addr,
3652 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3653 else
3654 ref->dw_loc_oprnd1.val_entry = NULL;
3656 return ref;
3659 /* Section names used to hold DWARF debugging information. */
3661 #ifndef DEBUG_INFO_SECTION
3662 #define DEBUG_INFO_SECTION ".debug_info"
3663 #endif
3664 #ifndef DEBUG_DWO_INFO_SECTION
3665 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3666 #endif
3667 #ifndef DEBUG_ABBREV_SECTION
3668 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3669 #endif
3670 #ifndef DEBUG_DWO_ABBREV_SECTION
3671 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3672 #endif
3673 #ifndef DEBUG_ARANGES_SECTION
3674 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3675 #endif
3676 #ifndef DEBUG_ADDR_SECTION
3677 #define DEBUG_ADDR_SECTION ".debug_addr"
3678 #endif
3679 #ifndef DEBUG_MACINFO_SECTION
3680 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3681 #endif
3682 #ifndef DEBUG_DWO_MACINFO_SECTION
3683 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3684 #endif
3685 #ifndef DEBUG_DWO_MACRO_SECTION
3686 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3687 #endif
3688 #ifndef DEBUG_MACRO_SECTION
3689 #define DEBUG_MACRO_SECTION ".debug_macro"
3690 #endif
3691 #ifndef DEBUG_LINE_SECTION
3692 #define DEBUG_LINE_SECTION ".debug_line"
3693 #endif
3694 #ifndef DEBUG_DWO_LINE_SECTION
3695 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3696 #endif
3697 #ifndef DEBUG_LOC_SECTION
3698 #define DEBUG_LOC_SECTION ".debug_loc"
3699 #endif
3700 #ifndef DEBUG_DWO_LOC_SECTION
3701 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3702 #endif
3703 #ifndef DEBUG_LOCLISTS_SECTION
3704 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3705 #endif
3706 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3707 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3708 #endif
3709 #ifndef DEBUG_PUBNAMES_SECTION
3710 #define DEBUG_PUBNAMES_SECTION \
3711 ((debug_generate_pub_sections == 2) \
3712 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3713 #endif
3714 #ifndef DEBUG_PUBTYPES_SECTION
3715 #define DEBUG_PUBTYPES_SECTION \
3716 ((debug_generate_pub_sections == 2) \
3717 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3718 #endif
3719 #ifndef DEBUG_STR_OFFSETS_SECTION
3720 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3721 #endif
3722 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3723 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3724 #endif
3725 #ifndef DEBUG_STR_DWO_SECTION
3726 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3727 #endif
3728 #ifndef DEBUG_STR_SECTION
3729 #define DEBUG_STR_SECTION ".debug_str"
3730 #endif
3731 #ifndef DEBUG_RANGES_SECTION
3732 #define DEBUG_RANGES_SECTION ".debug_ranges"
3733 #endif
3734 #ifndef DEBUG_RNGLISTS_SECTION
3735 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3736 #endif
3737 #ifndef DEBUG_LINE_STR_SECTION
3738 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3739 #endif
3741 /* Standard ELF section names for compiled code and data. */
3742 #ifndef TEXT_SECTION_NAME
3743 #define TEXT_SECTION_NAME ".text"
3744 #endif
3746 /* Section flags for .debug_str section. */
3747 #define DEBUG_STR_SECTION_FLAGS \
3748 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3749 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3750 : SECTION_DEBUG)
3752 /* Section flags for .debug_str.dwo section. */
3753 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3755 /* Labels we insert at beginning sections we can reference instead of
3756 the section names themselves. */
3758 #ifndef TEXT_SECTION_LABEL
3759 #define TEXT_SECTION_LABEL "Ltext"
3760 #endif
3761 #ifndef COLD_TEXT_SECTION_LABEL
3762 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3763 #endif
3764 #ifndef DEBUG_LINE_SECTION_LABEL
3765 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3766 #endif
3767 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3768 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3769 #endif
3770 #ifndef DEBUG_INFO_SECTION_LABEL
3771 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3772 #endif
3773 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3774 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3775 #endif
3776 #ifndef DEBUG_ABBREV_SECTION_LABEL
3777 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3778 #endif
3779 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3780 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3781 #endif
3782 #ifndef DEBUG_ADDR_SECTION_LABEL
3783 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3784 #endif
3785 #ifndef DEBUG_LOC_SECTION_LABEL
3786 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3787 #endif
3788 #ifndef DEBUG_RANGES_SECTION_LABEL
3789 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3790 #endif
3791 #ifndef DEBUG_MACINFO_SECTION_LABEL
3792 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3793 #endif
3794 #ifndef DEBUG_MACRO_SECTION_LABEL
3795 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3796 #endif
3797 #define SKELETON_COMP_DIE_ABBREV 1
3798 #define SKELETON_TYPE_DIE_ABBREV 2
3800 /* Definitions of defaults for formats and names of various special
3801 (artificial) labels which may be generated within this file (when the -g
3802 options is used and DWARF2_DEBUGGING_INFO is in effect.
3803 If necessary, these may be overridden from within the tm.h file, but
3804 typically, overriding these defaults is unnecessary. */
3806 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3807 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3808 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3809 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3810 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3811 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3812 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3813 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3814 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3815 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3816 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3817 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3818 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3819 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3820 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3822 #ifndef TEXT_END_LABEL
3823 #define TEXT_END_LABEL "Letext"
3824 #endif
3825 #ifndef COLD_END_LABEL
3826 #define COLD_END_LABEL "Letext_cold"
3827 #endif
3828 #ifndef BLOCK_BEGIN_LABEL
3829 #define BLOCK_BEGIN_LABEL "LBB"
3830 #endif
3831 #ifndef BLOCK_END_LABEL
3832 #define BLOCK_END_LABEL "LBE"
3833 #endif
3834 #ifndef LINE_CODE_LABEL
3835 #define LINE_CODE_LABEL "LM"
3836 #endif
3839 /* Return the root of the DIE's built for the current compilation unit. */
3840 static dw_die_ref
3841 comp_unit_die (void)
3843 if (!single_comp_unit_die)
3844 single_comp_unit_die = gen_compile_unit_die (NULL);
3845 return single_comp_unit_die;
3848 /* We allow a language front-end to designate a function that is to be
3849 called to "demangle" any name before it is put into a DIE. */
3851 static const char *(*demangle_name_func) (const char *);
3853 void
3854 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3856 demangle_name_func = func;
3859 /* Test if rtl node points to a pseudo register. */
3861 static inline int
3862 is_pseudo_reg (const_rtx rtl)
3864 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3865 || (GET_CODE (rtl) == SUBREG
3866 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3869 /* Return a reference to a type, with its const and volatile qualifiers
3870 removed. */
3872 static inline tree
3873 type_main_variant (tree type)
3875 type = TYPE_MAIN_VARIANT (type);
3877 /* ??? There really should be only one main variant among any group of
3878 variants of a given type (and all of the MAIN_VARIANT values for all
3879 members of the group should point to that one type) but sometimes the C
3880 front-end messes this up for array types, so we work around that bug
3881 here. */
3882 if (TREE_CODE (type) == ARRAY_TYPE)
3883 while (type != TYPE_MAIN_VARIANT (type))
3884 type = TYPE_MAIN_VARIANT (type);
3886 return type;
3889 /* Return nonzero if the given type node represents a tagged type. */
3891 static inline int
3892 is_tagged_type (const_tree type)
3894 enum tree_code code = TREE_CODE (type);
3896 return (code == RECORD_TYPE || code == UNION_TYPE
3897 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3900 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3902 static void
3903 get_ref_die_offset_label (char *label, dw_die_ref ref)
3905 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3908 /* Return die_offset of a DIE reference to a base type. */
3910 static unsigned long int
3911 get_base_type_offset (dw_die_ref ref)
3913 if (ref->die_offset)
3914 return ref->die_offset;
3915 if (comp_unit_die ()->die_abbrev)
3917 calc_base_type_die_sizes ();
3918 gcc_assert (ref->die_offset);
3920 return ref->die_offset;
3923 /* Return die_offset of a DIE reference other than base type. */
3925 static unsigned long int
3926 get_ref_die_offset (dw_die_ref ref)
3928 gcc_assert (ref->die_offset);
3929 return ref->die_offset;
3932 /* Convert a DIE tag into its string name. */
3934 static const char *
3935 dwarf_tag_name (unsigned int tag)
3937 const char *name = get_DW_TAG_name (tag);
3939 if (name != NULL)
3940 return name;
3942 return "DW_TAG_<unknown>";
3945 /* Convert a DWARF attribute code into its string name. */
3947 static const char *
3948 dwarf_attr_name (unsigned int attr)
3950 const char *name;
3952 switch (attr)
3954 #if VMS_DEBUGGING_INFO
3955 case DW_AT_HP_prologue:
3956 return "DW_AT_HP_prologue";
3957 #else
3958 case DW_AT_MIPS_loop_unroll_factor:
3959 return "DW_AT_MIPS_loop_unroll_factor";
3960 #endif
3962 #if VMS_DEBUGGING_INFO
3963 case DW_AT_HP_epilogue:
3964 return "DW_AT_HP_epilogue";
3965 #else
3966 case DW_AT_MIPS_stride:
3967 return "DW_AT_MIPS_stride";
3968 #endif
3971 name = get_DW_AT_name (attr);
3973 if (name != NULL)
3974 return name;
3976 return "DW_AT_<unknown>";
3979 /* Convert a DWARF value form code into its string name. */
3981 static const char *
3982 dwarf_form_name (unsigned int form)
3984 const char *name = get_DW_FORM_name (form);
3986 if (name != NULL)
3987 return name;
3989 return "DW_FORM_<unknown>";
3992 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3993 instance of an inlined instance of a decl which is local to an inline
3994 function, so we have to trace all of the way back through the origin chain
3995 to find out what sort of node actually served as the original seed for the
3996 given block. */
3998 static tree
3999 decl_ultimate_origin (const_tree decl)
4001 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4002 return NULL_TREE;
4004 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4005 we're trying to output the abstract instance of this function. */
4006 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4007 return NULL_TREE;
4009 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4010 most distant ancestor, this should never happen. */
4011 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4013 return DECL_ABSTRACT_ORIGIN (decl);
4016 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4017 of a virtual function may refer to a base class, so we check the 'this'
4018 parameter. */
4020 static tree
4021 decl_class_context (tree decl)
4023 tree context = NULL_TREE;
4025 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4026 context = DECL_CONTEXT (decl);
4027 else
4028 context = TYPE_MAIN_VARIANT
4029 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4031 if (context && !TYPE_P (context))
4032 context = NULL_TREE;
4034 return context;
4037 /* Add an attribute/value pair to a DIE. */
4039 static inline void
4040 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4042 /* Maybe this should be an assert? */
4043 if (die == NULL)
4044 return;
4046 vec_safe_reserve (die->die_attr, 1);
4047 vec_safe_push (die->die_attr, *attr);
4050 static inline enum dw_val_class
4051 AT_class (dw_attr_node *a)
4053 return a->dw_attr_val.val_class;
4056 /* Return the index for any attribute that will be referenced with a
4057 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4058 are stored in dw_attr_val.v.val_str for reference counting
4059 pruning. */
4061 static inline unsigned int
4062 AT_index (dw_attr_node *a)
4064 if (AT_class (a) == dw_val_class_str)
4065 return a->dw_attr_val.v.val_str->index;
4066 else if (a->dw_attr_val.val_entry != NULL)
4067 return a->dw_attr_val.val_entry->index;
4068 return NOT_INDEXED;
4071 /* Add a flag value attribute to a DIE. */
4073 static inline void
4074 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4076 dw_attr_node attr;
4078 attr.dw_attr = attr_kind;
4079 attr.dw_attr_val.val_class = dw_val_class_flag;
4080 attr.dw_attr_val.val_entry = NULL;
4081 attr.dw_attr_val.v.val_flag = flag;
4082 add_dwarf_attr (die, &attr);
4085 static inline unsigned
4086 AT_flag (dw_attr_node *a)
4088 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4089 return a->dw_attr_val.v.val_flag;
4092 /* Add a signed integer attribute value to a DIE. */
4094 static inline void
4095 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4097 dw_attr_node attr;
4099 attr.dw_attr = attr_kind;
4100 attr.dw_attr_val.val_class = dw_val_class_const;
4101 attr.dw_attr_val.val_entry = NULL;
4102 attr.dw_attr_val.v.val_int = int_val;
4103 add_dwarf_attr (die, &attr);
4106 static inline HOST_WIDE_INT
4107 AT_int (dw_attr_node *a)
4109 gcc_assert (a && (AT_class (a) == dw_val_class_const
4110 || AT_class (a) == dw_val_class_const_implicit));
4111 return a->dw_attr_val.v.val_int;
4114 /* Add an unsigned integer attribute value to a DIE. */
4116 static inline void
4117 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4118 unsigned HOST_WIDE_INT unsigned_val)
4120 dw_attr_node attr;
4122 attr.dw_attr = attr_kind;
4123 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4124 attr.dw_attr_val.val_entry = NULL;
4125 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4126 add_dwarf_attr (die, &attr);
4129 static inline unsigned HOST_WIDE_INT
4130 AT_unsigned (dw_attr_node *a)
4132 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4133 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4134 return a->dw_attr_val.v.val_unsigned;
4137 /* Add an unsigned wide integer attribute value to a DIE. */
4139 static inline void
4140 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4141 const wide_int& w)
4143 dw_attr_node attr;
4145 attr.dw_attr = attr_kind;
4146 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4147 attr.dw_attr_val.val_entry = NULL;
4148 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4149 *attr.dw_attr_val.v.val_wide = w;
4150 add_dwarf_attr (die, &attr);
4153 /* Add an unsigned double integer attribute value to a DIE. */
4155 static inline void
4156 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4157 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4159 dw_attr_node attr;
4161 attr.dw_attr = attr_kind;
4162 attr.dw_attr_val.val_class = dw_val_class_const_double;
4163 attr.dw_attr_val.val_entry = NULL;
4164 attr.dw_attr_val.v.val_double.high = high;
4165 attr.dw_attr_val.v.val_double.low = low;
4166 add_dwarf_attr (die, &attr);
4169 /* Add a floating point attribute value to a DIE and return it. */
4171 static inline void
4172 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4173 unsigned int length, unsigned int elt_size, unsigned char *array)
4175 dw_attr_node attr;
4177 attr.dw_attr = attr_kind;
4178 attr.dw_attr_val.val_class = dw_val_class_vec;
4179 attr.dw_attr_val.val_entry = NULL;
4180 attr.dw_attr_val.v.val_vec.length = length;
4181 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4182 attr.dw_attr_val.v.val_vec.array = array;
4183 add_dwarf_attr (die, &attr);
4186 /* Add an 8-byte data attribute value to a DIE. */
4188 static inline void
4189 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4190 unsigned char data8[8])
4192 dw_attr_node attr;
4194 attr.dw_attr = attr_kind;
4195 attr.dw_attr_val.val_class = dw_val_class_data8;
4196 attr.dw_attr_val.val_entry = NULL;
4197 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4198 add_dwarf_attr (die, &attr);
4201 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4202 dwarf_split_debug_info, address attributes in dies destined for the
4203 final executable have force_direct set to avoid using indexed
4204 references. */
4206 static inline void
4207 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4208 bool force_direct)
4210 dw_attr_node attr;
4211 char * lbl_id;
4213 lbl_id = xstrdup (lbl_low);
4214 attr.dw_attr = DW_AT_low_pc;
4215 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4216 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4217 if (dwarf_split_debug_info && !force_direct)
4218 attr.dw_attr_val.val_entry
4219 = add_addr_table_entry (lbl_id, ate_kind_label);
4220 else
4221 attr.dw_attr_val.val_entry = NULL;
4222 add_dwarf_attr (die, &attr);
4224 attr.dw_attr = DW_AT_high_pc;
4225 if (dwarf_version < 4)
4226 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4227 else
4228 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4229 lbl_id = xstrdup (lbl_high);
4230 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4231 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4232 && dwarf_split_debug_info && !force_direct)
4233 attr.dw_attr_val.val_entry
4234 = add_addr_table_entry (lbl_id, ate_kind_label);
4235 else
4236 attr.dw_attr_val.val_entry = NULL;
4237 add_dwarf_attr (die, &attr);
4240 /* Hash and equality functions for debug_str_hash. */
4242 hashval_t
4243 indirect_string_hasher::hash (indirect_string_node *x)
4245 return htab_hash_string (x->str);
4248 bool
4249 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4251 return strcmp (x1->str, x2) == 0;
4254 /* Add STR to the given string hash table. */
4256 static struct indirect_string_node *
4257 find_AT_string_in_table (const char *str,
4258 hash_table<indirect_string_hasher> *table)
4260 struct indirect_string_node *node;
4262 indirect_string_node **slot
4263 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4264 if (*slot == NULL)
4266 node = ggc_cleared_alloc<indirect_string_node> ();
4267 node->str = ggc_strdup (str);
4268 *slot = node;
4270 else
4271 node = *slot;
4273 node->refcount++;
4274 return node;
4277 /* Add STR to the indirect string hash table. */
4279 static struct indirect_string_node *
4280 find_AT_string (const char *str)
4282 if (! debug_str_hash)
4283 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4285 return find_AT_string_in_table (str, debug_str_hash);
4288 /* Add a string attribute value to a DIE. */
4290 static inline void
4291 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4293 dw_attr_node attr;
4294 struct indirect_string_node *node;
4296 node = find_AT_string (str);
4298 attr.dw_attr = attr_kind;
4299 attr.dw_attr_val.val_class = dw_val_class_str;
4300 attr.dw_attr_val.val_entry = NULL;
4301 attr.dw_attr_val.v.val_str = node;
4302 add_dwarf_attr (die, &attr);
4305 static inline const char *
4306 AT_string (dw_attr_node *a)
4308 gcc_assert (a && AT_class (a) == dw_val_class_str);
4309 return a->dw_attr_val.v.val_str->str;
4312 /* Call this function directly to bypass AT_string_form's logic to put
4313 the string inline in the die. */
4315 static void
4316 set_indirect_string (struct indirect_string_node *node)
4318 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4319 /* Already indirect is a no op. */
4320 if (node->form == DW_FORM_strp
4321 || node->form == DW_FORM_line_strp
4322 || node->form == DW_FORM_GNU_str_index)
4324 gcc_assert (node->label);
4325 return;
4327 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4328 ++dw2_string_counter;
4329 node->label = xstrdup (label);
4331 if (!dwarf_split_debug_info)
4333 node->form = DW_FORM_strp;
4334 node->index = NOT_INDEXED;
4336 else
4338 node->form = DW_FORM_GNU_str_index;
4339 node->index = NO_INDEX_ASSIGNED;
4343 /* Find out whether a string should be output inline in DIE
4344 or out-of-line in .debug_str section. */
4346 static enum dwarf_form
4347 find_string_form (struct indirect_string_node *node)
4349 unsigned int len;
4351 if (node->form)
4352 return node->form;
4354 len = strlen (node->str) + 1;
4356 /* If the string is shorter or equal to the size of the reference, it is
4357 always better to put it inline. */
4358 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4359 return node->form = DW_FORM_string;
4361 /* If we cannot expect the linker to merge strings in .debug_str
4362 section, only put it into .debug_str if it is worth even in this
4363 single module. */
4364 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4365 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4366 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4367 return node->form = DW_FORM_string;
4369 set_indirect_string (node);
4371 return node->form;
4374 /* Find out whether the string referenced from the attribute should be
4375 output inline in DIE or out-of-line in .debug_str section. */
4377 static enum dwarf_form
4378 AT_string_form (dw_attr_node *a)
4380 gcc_assert (a && AT_class (a) == dw_val_class_str);
4381 return find_string_form (a->dw_attr_val.v.val_str);
4384 /* Add a DIE reference attribute value to a DIE. */
4386 static inline void
4387 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4389 dw_attr_node attr;
4390 gcc_checking_assert (targ_die != NULL);
4392 /* With LTO we can end up trying to reference something we didn't create
4393 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4394 if (targ_die == NULL)
4395 return;
4397 attr.dw_attr = attr_kind;
4398 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4399 attr.dw_attr_val.val_entry = NULL;
4400 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4401 attr.dw_attr_val.v.val_die_ref.external = 0;
4402 add_dwarf_attr (die, &attr);
4405 /* Change DIE reference REF to point to NEW_DIE instead. */
4407 static inline void
4408 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4410 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4411 ref->dw_attr_val.v.val_die_ref.die = new_die;
4412 ref->dw_attr_val.v.val_die_ref.external = 0;
4415 /* Add an AT_specification attribute to a DIE, and also make the back
4416 pointer from the specification to the definition. */
4418 static inline void
4419 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4421 add_AT_die_ref (die, DW_AT_specification, targ_die);
4422 gcc_assert (!targ_die->die_definition);
4423 targ_die->die_definition = die;
4426 static inline dw_die_ref
4427 AT_ref (dw_attr_node *a)
4429 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4430 return a->dw_attr_val.v.val_die_ref.die;
4433 static inline int
4434 AT_ref_external (dw_attr_node *a)
4436 if (a && AT_class (a) == dw_val_class_die_ref)
4437 return a->dw_attr_val.v.val_die_ref.external;
4439 return 0;
4442 static inline void
4443 set_AT_ref_external (dw_attr_node *a, int i)
4445 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4446 a->dw_attr_val.v.val_die_ref.external = i;
4449 /* Add an FDE reference attribute value to a DIE. */
4451 static inline void
4452 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4454 dw_attr_node attr;
4456 attr.dw_attr = attr_kind;
4457 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4458 attr.dw_attr_val.val_entry = NULL;
4459 attr.dw_attr_val.v.val_fde_index = targ_fde;
4460 add_dwarf_attr (die, &attr);
4463 /* Add a location description attribute value to a DIE. */
4465 static inline void
4466 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4468 dw_attr_node attr;
4470 attr.dw_attr = attr_kind;
4471 attr.dw_attr_val.val_class = dw_val_class_loc;
4472 attr.dw_attr_val.val_entry = NULL;
4473 attr.dw_attr_val.v.val_loc = loc;
4474 add_dwarf_attr (die, &attr);
4477 static inline dw_loc_descr_ref
4478 AT_loc (dw_attr_node *a)
4480 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4481 return a->dw_attr_val.v.val_loc;
4484 static inline void
4485 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4487 dw_attr_node attr;
4489 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4490 return;
4492 attr.dw_attr = attr_kind;
4493 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4494 attr.dw_attr_val.val_entry = NULL;
4495 attr.dw_attr_val.v.val_loc_list = loc_list;
4496 add_dwarf_attr (die, &attr);
4497 have_location_lists = true;
4500 static inline dw_loc_list_ref
4501 AT_loc_list (dw_attr_node *a)
4503 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4504 return a->dw_attr_val.v.val_loc_list;
4507 static inline dw_loc_list_ref *
4508 AT_loc_list_ptr (dw_attr_node *a)
4510 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4511 return &a->dw_attr_val.v.val_loc_list;
4514 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4516 static hashval_t hash (addr_table_entry *);
4517 static bool equal (addr_table_entry *, addr_table_entry *);
4520 /* Table of entries into the .debug_addr section. */
4522 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4524 /* Hash an address_table_entry. */
4526 hashval_t
4527 addr_hasher::hash (addr_table_entry *a)
4529 inchash::hash hstate;
4530 switch (a->kind)
4532 case ate_kind_rtx:
4533 hstate.add_int (0);
4534 break;
4535 case ate_kind_rtx_dtprel:
4536 hstate.add_int (1);
4537 break;
4538 case ate_kind_label:
4539 return htab_hash_string (a->addr.label);
4540 default:
4541 gcc_unreachable ();
4543 inchash::add_rtx (a->addr.rtl, hstate);
4544 return hstate.end ();
4547 /* Determine equality for two address_table_entries. */
4549 bool
4550 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4552 if (a1->kind != a2->kind)
4553 return 0;
4554 switch (a1->kind)
4556 case ate_kind_rtx:
4557 case ate_kind_rtx_dtprel:
4558 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4559 case ate_kind_label:
4560 return strcmp (a1->addr.label, a2->addr.label) == 0;
4561 default:
4562 gcc_unreachable ();
4566 /* Initialize an addr_table_entry. */
4568 void
4569 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4571 e->kind = kind;
4572 switch (kind)
4574 case ate_kind_rtx:
4575 case ate_kind_rtx_dtprel:
4576 e->addr.rtl = (rtx) addr;
4577 break;
4578 case ate_kind_label:
4579 e->addr.label = (char *) addr;
4580 break;
4582 e->refcount = 0;
4583 e->index = NO_INDEX_ASSIGNED;
4586 /* Add attr to the address table entry to the table. Defer setting an
4587 index until output time. */
4589 static addr_table_entry *
4590 add_addr_table_entry (void *addr, enum ate_kind kind)
4592 addr_table_entry *node;
4593 addr_table_entry finder;
4595 gcc_assert (dwarf_split_debug_info);
4596 if (! addr_index_table)
4597 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4598 init_addr_table_entry (&finder, kind, addr);
4599 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4601 if (*slot == HTAB_EMPTY_ENTRY)
4603 node = ggc_cleared_alloc<addr_table_entry> ();
4604 init_addr_table_entry (node, kind, addr);
4605 *slot = node;
4607 else
4608 node = *slot;
4610 node->refcount++;
4611 return node;
4614 /* Remove an entry from the addr table by decrementing its refcount.
4615 Strictly, decrementing the refcount would be enough, but the
4616 assertion that the entry is actually in the table has found
4617 bugs. */
4619 static void
4620 remove_addr_table_entry (addr_table_entry *entry)
4622 gcc_assert (dwarf_split_debug_info && addr_index_table);
4623 /* After an index is assigned, the table is frozen. */
4624 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4625 entry->refcount--;
4628 /* Given a location list, remove all addresses it refers to from the
4629 address_table. */
4631 static void
4632 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4634 for (; descr; descr = descr->dw_loc_next)
4635 if (descr->dw_loc_oprnd1.val_entry != NULL)
4637 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4638 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4642 /* A helper function for dwarf2out_finish called through
4643 htab_traverse. Assign an addr_table_entry its index. All entries
4644 must be collected into the table when this function is called,
4645 because the indexing code relies on htab_traverse to traverse nodes
4646 in the same order for each run. */
4649 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4651 addr_table_entry *node = *h;
4653 /* Don't index unreferenced nodes. */
4654 if (node->refcount == 0)
4655 return 1;
4657 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4658 node->index = *index;
4659 *index += 1;
4661 return 1;
4664 /* Add an address constant attribute value to a DIE. When using
4665 dwarf_split_debug_info, address attributes in dies destined for the
4666 final executable should be direct references--setting the parameter
4667 force_direct ensures this behavior. */
4669 static inline void
4670 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4671 bool force_direct)
4673 dw_attr_node attr;
4675 attr.dw_attr = attr_kind;
4676 attr.dw_attr_val.val_class = dw_val_class_addr;
4677 attr.dw_attr_val.v.val_addr = addr;
4678 if (dwarf_split_debug_info && !force_direct)
4679 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4680 else
4681 attr.dw_attr_val.val_entry = NULL;
4682 add_dwarf_attr (die, &attr);
4685 /* Get the RTX from to an address DIE attribute. */
4687 static inline rtx
4688 AT_addr (dw_attr_node *a)
4690 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4691 return a->dw_attr_val.v.val_addr;
4694 /* Add a file attribute value to a DIE. */
4696 static inline void
4697 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4698 struct dwarf_file_data *fd)
4700 dw_attr_node attr;
4702 attr.dw_attr = attr_kind;
4703 attr.dw_attr_val.val_class = dw_val_class_file;
4704 attr.dw_attr_val.val_entry = NULL;
4705 attr.dw_attr_val.v.val_file = fd;
4706 add_dwarf_attr (die, &attr);
4709 /* Get the dwarf_file_data from a file DIE attribute. */
4711 static inline struct dwarf_file_data *
4712 AT_file (dw_attr_node *a)
4714 gcc_assert (a && (AT_class (a) == dw_val_class_file
4715 || AT_class (a) == dw_val_class_file_implicit));
4716 return a->dw_attr_val.v.val_file;
4719 /* Add a vms delta attribute value to a DIE. */
4721 static inline void
4722 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4723 const char *lbl1, const char *lbl2)
4725 dw_attr_node attr;
4727 attr.dw_attr = attr_kind;
4728 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4729 attr.dw_attr_val.val_entry = NULL;
4730 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4731 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4732 add_dwarf_attr (die, &attr);
4735 /* Add a label identifier attribute value to a DIE. */
4737 static inline void
4738 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4739 const char *lbl_id)
4741 dw_attr_node attr;
4743 attr.dw_attr = attr_kind;
4744 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4745 attr.dw_attr_val.val_entry = NULL;
4746 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4747 if (dwarf_split_debug_info)
4748 attr.dw_attr_val.val_entry
4749 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4750 ate_kind_label);
4751 add_dwarf_attr (die, &attr);
4754 /* Add a section offset attribute value to a DIE, an offset into the
4755 debug_line section. */
4757 static inline void
4758 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4759 const char *label)
4761 dw_attr_node attr;
4763 attr.dw_attr = attr_kind;
4764 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4765 attr.dw_attr_val.val_entry = NULL;
4766 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4767 add_dwarf_attr (die, &attr);
4770 /* Add a section offset attribute value to a DIE, an offset into the
4771 debug_loclists section. */
4773 static inline void
4774 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4775 const char *label)
4777 dw_attr_node attr;
4779 attr.dw_attr = attr_kind;
4780 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4781 attr.dw_attr_val.val_entry = NULL;
4782 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4783 add_dwarf_attr (die, &attr);
4786 /* Add a section offset attribute value to a DIE, an offset into the
4787 debug_macinfo section. */
4789 static inline void
4790 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4791 const char *label)
4793 dw_attr_node attr;
4795 attr.dw_attr = attr_kind;
4796 attr.dw_attr_val.val_class = dw_val_class_macptr;
4797 attr.dw_attr_val.val_entry = NULL;
4798 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4799 add_dwarf_attr (die, &attr);
4802 /* Add an offset attribute value to a DIE. */
4804 static inline void
4805 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4806 unsigned HOST_WIDE_INT offset)
4808 dw_attr_node attr;
4810 attr.dw_attr = attr_kind;
4811 attr.dw_attr_val.val_class = dw_val_class_offset;
4812 attr.dw_attr_val.val_entry = NULL;
4813 attr.dw_attr_val.v.val_offset = offset;
4814 add_dwarf_attr (die, &attr);
4817 /* Add a range_list attribute value to a DIE. When using
4818 dwarf_split_debug_info, address attributes in dies destined for the
4819 final executable should be direct references--setting the parameter
4820 force_direct ensures this behavior. */
4822 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4823 #define RELOCATED_OFFSET (NULL)
4825 static void
4826 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4827 long unsigned int offset, bool force_direct)
4829 dw_attr_node attr;
4831 attr.dw_attr = attr_kind;
4832 attr.dw_attr_val.val_class = dw_val_class_range_list;
4833 /* For the range_list attribute, use val_entry to store whether the
4834 offset should follow split-debug-info or normal semantics. This
4835 value is read in output_range_list_offset. */
4836 if (dwarf_split_debug_info && !force_direct)
4837 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4838 else
4839 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4840 attr.dw_attr_val.v.val_offset = offset;
4841 add_dwarf_attr (die, &attr);
4844 /* Return the start label of a delta attribute. */
4846 static inline const char *
4847 AT_vms_delta1 (dw_attr_node *a)
4849 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4850 return a->dw_attr_val.v.val_vms_delta.lbl1;
4853 /* Return the end label of a delta attribute. */
4855 static inline const char *
4856 AT_vms_delta2 (dw_attr_node *a)
4858 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4859 return a->dw_attr_val.v.val_vms_delta.lbl2;
4862 static inline const char *
4863 AT_lbl (dw_attr_node *a)
4865 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4866 || AT_class (a) == dw_val_class_lineptr
4867 || AT_class (a) == dw_val_class_macptr
4868 || AT_class (a) == dw_val_class_loclistsptr
4869 || AT_class (a) == dw_val_class_high_pc));
4870 return a->dw_attr_val.v.val_lbl_id;
4873 /* Get the attribute of type attr_kind. */
4875 static dw_attr_node *
4876 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4878 dw_attr_node *a;
4879 unsigned ix;
4880 dw_die_ref spec = NULL;
4882 if (! die)
4883 return NULL;
4885 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4886 if (a->dw_attr == attr_kind)
4887 return a;
4888 else if (a->dw_attr == DW_AT_specification
4889 || a->dw_attr == DW_AT_abstract_origin)
4890 spec = AT_ref (a);
4892 if (spec)
4893 return get_AT (spec, attr_kind);
4895 return NULL;
4898 /* Returns the parent of the declaration of DIE. */
4900 static dw_die_ref
4901 get_die_parent (dw_die_ref die)
4903 dw_die_ref t;
4905 if (!die)
4906 return NULL;
4908 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4909 || (t = get_AT_ref (die, DW_AT_specification)))
4910 die = t;
4912 return die->die_parent;
4915 /* Return the "low pc" attribute value, typically associated with a subprogram
4916 DIE. Return null if the "low pc" attribute is either not present, or if it
4917 cannot be represented as an assembler label identifier. */
4919 static inline const char *
4920 get_AT_low_pc (dw_die_ref die)
4922 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4924 return a ? AT_lbl (a) : NULL;
4927 /* Return the "high pc" attribute value, typically associated with a subprogram
4928 DIE. Return null if the "high pc" attribute is either not present, or if it
4929 cannot be represented as an assembler label identifier. */
4931 static inline const char *
4932 get_AT_hi_pc (dw_die_ref die)
4934 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4936 return a ? AT_lbl (a) : NULL;
4939 /* Return the value of the string attribute designated by ATTR_KIND, or
4940 NULL if it is not present. */
4942 static inline const char *
4943 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4945 dw_attr_node *a = get_AT (die, attr_kind);
4947 return a ? AT_string (a) : NULL;
4950 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4951 if it is not present. */
4953 static inline int
4954 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4956 dw_attr_node *a = get_AT (die, attr_kind);
4958 return a ? AT_flag (a) : 0;
4961 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4962 if it is not present. */
4964 static inline unsigned
4965 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4967 dw_attr_node *a = get_AT (die, attr_kind);
4969 return a ? AT_unsigned (a) : 0;
4972 static inline dw_die_ref
4973 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4975 dw_attr_node *a = get_AT (die, attr_kind);
4977 return a ? AT_ref (a) : NULL;
4980 static inline struct dwarf_file_data *
4981 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4983 dw_attr_node *a = get_AT (die, attr_kind);
4985 return a ? AT_file (a) : NULL;
4988 /* Return TRUE if the language is C++. */
4990 static inline bool
4991 is_cxx (void)
4993 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4995 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4996 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4999 /* Return TRUE if DECL was created by the C++ frontend. */
5001 static bool
5002 is_cxx (const_tree decl)
5004 if (in_lto_p)
5006 const_tree context = decl;
5007 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5009 if (TREE_CODE (context) == BLOCK)
5010 context = BLOCK_SUPERCONTEXT (context);
5011 else
5012 context = get_containing_scope (context);
5014 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5015 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5017 return is_cxx ();
5020 /* Return TRUE if the language is Java. */
5022 static inline bool
5023 is_java (void)
5025 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5027 return lang == DW_LANG_Java;
5030 /* Return TRUE if the language is Fortran. */
5032 static inline bool
5033 is_fortran (void)
5035 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5037 return (lang == DW_LANG_Fortran77
5038 || lang == DW_LANG_Fortran90
5039 || lang == DW_LANG_Fortran95
5040 || lang == DW_LANG_Fortran03
5041 || lang == DW_LANG_Fortran08);
5044 /* Return TRUE if the language is Ada. */
5046 static inline bool
5047 is_ada (void)
5049 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5051 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5054 /* Remove the specified attribute if present. Return TRUE if removal
5055 was successful. */
5057 static bool
5058 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5060 dw_attr_node *a;
5061 unsigned ix;
5063 if (! die)
5064 return false;
5066 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5067 if (a->dw_attr == attr_kind)
5069 if (AT_class (a) == dw_val_class_str)
5070 if (a->dw_attr_val.v.val_str->refcount)
5071 a->dw_attr_val.v.val_str->refcount--;
5073 /* vec::ordered_remove should help reduce the number of abbrevs
5074 that are needed. */
5075 die->die_attr->ordered_remove (ix);
5076 return true;
5078 return false;
5081 /* Remove CHILD from its parent. PREV must have the property that
5082 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5084 static void
5085 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5087 gcc_assert (child->die_parent == prev->die_parent);
5088 gcc_assert (prev->die_sib == child);
5089 if (prev == child)
5091 gcc_assert (child->die_parent->die_child == child);
5092 prev = NULL;
5094 else
5095 prev->die_sib = child->die_sib;
5096 if (child->die_parent->die_child == child)
5097 child->die_parent->die_child = prev;
5098 child->die_sib = NULL;
5101 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5102 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5104 static void
5105 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5107 dw_die_ref parent = old_child->die_parent;
5109 gcc_assert (parent == prev->die_parent);
5110 gcc_assert (prev->die_sib == old_child);
5112 new_child->die_parent = parent;
5113 if (prev == old_child)
5115 gcc_assert (parent->die_child == old_child);
5116 new_child->die_sib = new_child;
5118 else
5120 prev->die_sib = new_child;
5121 new_child->die_sib = old_child->die_sib;
5123 if (old_child->die_parent->die_child == old_child)
5124 old_child->die_parent->die_child = new_child;
5125 old_child->die_sib = NULL;
5128 /* Move all children from OLD_PARENT to NEW_PARENT. */
5130 static void
5131 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5133 dw_die_ref c;
5134 new_parent->die_child = old_parent->die_child;
5135 old_parent->die_child = NULL;
5136 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5139 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5140 matches TAG. */
5142 static void
5143 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5145 dw_die_ref c;
5147 c = die->die_child;
5148 if (c) do {
5149 dw_die_ref prev = c;
5150 c = c->die_sib;
5151 while (c->die_tag == tag)
5153 remove_child_with_prev (c, prev);
5154 c->die_parent = NULL;
5155 /* Might have removed every child. */
5156 if (die->die_child == NULL)
5157 return;
5158 c = prev->die_sib;
5160 } while (c != die->die_child);
5163 /* Add a CHILD_DIE as the last child of DIE. */
5165 static void
5166 add_child_die (dw_die_ref die, dw_die_ref child_die)
5168 /* FIXME this should probably be an assert. */
5169 if (! die || ! child_die)
5170 return;
5171 gcc_assert (die != child_die);
5173 child_die->die_parent = die;
5174 if (die->die_child)
5176 child_die->die_sib = die->die_child->die_sib;
5177 die->die_child->die_sib = child_die;
5179 else
5180 child_die->die_sib = child_die;
5181 die->die_child = child_die;
5184 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5186 static void
5187 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5188 dw_die_ref after_die)
5190 gcc_assert (die
5191 && child_die
5192 && after_die
5193 && die->die_child
5194 && die != child_die);
5196 child_die->die_parent = die;
5197 child_die->die_sib = after_die->die_sib;
5198 after_die->die_sib = child_die;
5199 if (die->die_child == after_die)
5200 die->die_child = child_die;
5203 /* Unassociate CHILD from its parent, and make its parent be
5204 NEW_PARENT. */
5206 static void
5207 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5209 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5210 if (p->die_sib == child)
5212 remove_child_with_prev (child, p);
5213 break;
5215 add_child_die (new_parent, child);
5218 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5219 is the specification, to the end of PARENT's list of children.
5220 This is done by removing and re-adding it. */
5222 static void
5223 splice_child_die (dw_die_ref parent, dw_die_ref child)
5225 /* We want the declaration DIE from inside the class, not the
5226 specification DIE at toplevel. */
5227 if (child->die_parent != parent)
5229 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5231 if (tmp)
5232 child = tmp;
5235 gcc_assert (child->die_parent == parent
5236 || (child->die_parent
5237 == get_AT_ref (parent, DW_AT_specification)));
5239 reparent_child (child, parent);
5242 /* Create and return a new die with a parent of PARENT_DIE. If
5243 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5244 associated tree T must be supplied to determine parenthood
5245 later. */
5247 static inline dw_die_ref
5248 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5250 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5252 die->die_tag = tag_value;
5254 if (parent_die != NULL)
5255 add_child_die (parent_die, die);
5256 else
5258 limbo_die_node *limbo_node;
5260 /* No DIEs created after early dwarf should end up in limbo,
5261 because the limbo list should not persist past LTO
5262 streaming. */
5263 if (tag_value != DW_TAG_compile_unit
5264 /* These are allowed because they're generated while
5265 breaking out COMDAT units late. */
5266 && tag_value != DW_TAG_type_unit
5267 && tag_value != DW_TAG_skeleton_unit
5268 && !early_dwarf
5269 /* Allow nested functions to live in limbo because they will
5270 only temporarily live there, as decls_for_scope will fix
5271 them up. */
5272 && (TREE_CODE (t) != FUNCTION_DECL
5273 || !decl_function_context (t))
5274 /* Same as nested functions above but for types. Types that
5275 are local to a function will be fixed in
5276 decls_for_scope. */
5277 && (!RECORD_OR_UNION_TYPE_P (t)
5278 || !TYPE_CONTEXT (t)
5279 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5280 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5281 especially in the ltrans stage, but once we implement LTO
5282 dwarf streaming, we should remove this exception. */
5283 && !in_lto_p)
5285 fprintf (stderr, "symbol ended up in limbo too late:");
5286 debug_generic_stmt (t);
5287 gcc_unreachable ();
5290 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5291 limbo_node->die = die;
5292 limbo_node->created_for = t;
5293 limbo_node->next = limbo_die_list;
5294 limbo_die_list = limbo_node;
5297 return die;
5300 /* Return the DIE associated with the given type specifier. */
5302 static inline dw_die_ref
5303 lookup_type_die (tree type)
5305 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5306 if (die && die->removed)
5308 TYPE_SYMTAB_DIE (type) = NULL;
5309 return NULL;
5311 return die;
5314 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5315 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5316 anonymous type instead the one of the naming typedef. */
5318 static inline dw_die_ref
5319 strip_naming_typedef (tree type, dw_die_ref type_die)
5321 if (type
5322 && TREE_CODE (type) == RECORD_TYPE
5323 && type_die
5324 && type_die->die_tag == DW_TAG_typedef
5325 && is_naming_typedef_decl (TYPE_NAME (type)))
5326 type_die = get_AT_ref (type_die, DW_AT_type);
5327 return type_die;
5330 /* Like lookup_type_die, but if type is an anonymous type named by a
5331 typedef[1], return the DIE of the anonymous type instead the one of
5332 the naming typedef. This is because in gen_typedef_die, we did
5333 equate the anonymous struct named by the typedef with the DIE of
5334 the naming typedef. So by default, lookup_type_die on an anonymous
5335 struct yields the DIE of the naming typedef.
5337 [1]: Read the comment of is_naming_typedef_decl to learn about what
5338 a naming typedef is. */
5340 static inline dw_die_ref
5341 lookup_type_die_strip_naming_typedef (tree type)
5343 dw_die_ref die = lookup_type_die (type);
5344 return strip_naming_typedef (type, die);
5347 /* Equate a DIE to a given type specifier. */
5349 static inline void
5350 equate_type_number_to_die (tree type, dw_die_ref type_die)
5352 TYPE_SYMTAB_DIE (type) = type_die;
5355 /* Returns a hash value for X (which really is a die_struct). */
5357 inline hashval_t
5358 decl_die_hasher::hash (die_node *x)
5360 return (hashval_t) x->decl_id;
5363 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5365 inline bool
5366 decl_die_hasher::equal (die_node *x, tree y)
5368 return (x->decl_id == DECL_UID (y));
5371 /* Return the DIE associated with a given declaration. */
5373 static inline dw_die_ref
5374 lookup_decl_die (tree decl)
5376 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5377 NO_INSERT);
5378 if (!die)
5379 return NULL;
5380 if ((*die)->removed)
5382 decl_die_table->clear_slot (die);
5383 return NULL;
5385 return *die;
5388 /* Returns a hash value for X (which really is a var_loc_list). */
5390 inline hashval_t
5391 decl_loc_hasher::hash (var_loc_list *x)
5393 return (hashval_t) x->decl_id;
5396 /* Return nonzero if decl_id of var_loc_list X is the same as
5397 UID of decl *Y. */
5399 inline bool
5400 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5402 return (x->decl_id == DECL_UID (y));
5405 /* Return the var_loc list associated with a given declaration. */
5407 static inline var_loc_list *
5408 lookup_decl_loc (const_tree decl)
5410 if (!decl_loc_table)
5411 return NULL;
5412 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5415 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5417 inline hashval_t
5418 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5420 return (hashval_t) x->decl_id;
5423 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5424 UID of decl *Y. */
5426 inline bool
5427 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5429 return (x->decl_id == DECL_UID (y));
5432 /* Equate a DIE to a particular declaration. */
5434 static void
5435 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5437 unsigned int decl_id = DECL_UID (decl);
5439 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5440 decl_die->decl_id = decl_id;
5443 /* Return how many bits covers PIECE EXPR_LIST. */
5445 static HOST_WIDE_INT
5446 decl_piece_bitsize (rtx piece)
5448 int ret = (int) GET_MODE (piece);
5449 if (ret)
5450 return ret;
5451 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5452 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5453 return INTVAL (XEXP (XEXP (piece, 0), 0));
5456 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5458 static rtx *
5459 decl_piece_varloc_ptr (rtx piece)
5461 if ((int) GET_MODE (piece))
5462 return &XEXP (piece, 0);
5463 else
5464 return &XEXP (XEXP (piece, 0), 1);
5467 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5468 Next is the chain of following piece nodes. */
5470 static rtx_expr_list *
5471 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5473 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5474 return alloc_EXPR_LIST (bitsize, loc_note, next);
5475 else
5476 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5477 GEN_INT (bitsize),
5478 loc_note), next);
5481 /* Return rtx that should be stored into loc field for
5482 LOC_NOTE and BITPOS/BITSIZE. */
5484 static rtx
5485 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5486 HOST_WIDE_INT bitsize)
5488 if (bitsize != -1)
5490 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5491 if (bitpos != 0)
5492 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5494 return loc_note;
5497 /* This function either modifies location piece list *DEST in
5498 place (if SRC and INNER is NULL), or copies location piece list
5499 *SRC to *DEST while modifying it. Location BITPOS is modified
5500 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5501 not copied and if needed some padding around it is added.
5502 When modifying in place, DEST should point to EXPR_LIST where
5503 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5504 to the start of the whole list and INNER points to the EXPR_LIST
5505 where earlier pieces cover PIECE_BITPOS bits. */
5507 static void
5508 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5509 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5510 HOST_WIDE_INT bitsize, rtx loc_note)
5512 HOST_WIDE_INT diff;
5513 bool copy = inner != NULL;
5515 if (copy)
5517 /* First copy all nodes preceding the current bitpos. */
5518 while (src != inner)
5520 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5521 decl_piece_bitsize (*src), NULL_RTX);
5522 dest = &XEXP (*dest, 1);
5523 src = &XEXP (*src, 1);
5526 /* Add padding if needed. */
5527 if (bitpos != piece_bitpos)
5529 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5530 copy ? NULL_RTX : *dest);
5531 dest = &XEXP (*dest, 1);
5533 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5535 gcc_assert (!copy);
5536 /* A piece with correct bitpos and bitsize already exist,
5537 just update the location for it and return. */
5538 *decl_piece_varloc_ptr (*dest) = loc_note;
5539 return;
5541 /* Add the piece that changed. */
5542 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5543 dest = &XEXP (*dest, 1);
5544 /* Skip over pieces that overlap it. */
5545 diff = bitpos - piece_bitpos + bitsize;
5546 if (!copy)
5547 src = dest;
5548 while (diff > 0 && *src)
5550 rtx piece = *src;
5551 diff -= decl_piece_bitsize (piece);
5552 if (copy)
5553 src = &XEXP (piece, 1);
5554 else
5556 *src = XEXP (piece, 1);
5557 free_EXPR_LIST_node (piece);
5560 /* Add padding if needed. */
5561 if (diff < 0 && *src)
5563 if (!copy)
5564 dest = src;
5565 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5566 dest = &XEXP (*dest, 1);
5568 if (!copy)
5569 return;
5570 /* Finally copy all nodes following it. */
5571 while (*src)
5573 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5574 decl_piece_bitsize (*src), NULL_RTX);
5575 dest = &XEXP (*dest, 1);
5576 src = &XEXP (*src, 1);
5580 /* Add a variable location node to the linked list for DECL. */
5582 static struct var_loc_node *
5583 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5585 unsigned int decl_id;
5586 var_loc_list *temp;
5587 struct var_loc_node *loc = NULL;
5588 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5590 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5592 tree realdecl = DECL_DEBUG_EXPR (decl);
5593 if (handled_component_p (realdecl)
5594 || (TREE_CODE (realdecl) == MEM_REF
5595 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5597 HOST_WIDE_INT maxsize;
5598 bool reverse;
5599 tree innerdecl
5600 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5601 &reverse);
5602 if (!DECL_P (innerdecl)
5603 || DECL_IGNORED_P (innerdecl)
5604 || TREE_STATIC (innerdecl)
5605 || bitsize <= 0
5606 || bitpos + bitsize > 256
5607 || bitsize != maxsize)
5608 return NULL;
5609 decl = innerdecl;
5613 decl_id = DECL_UID (decl);
5614 var_loc_list **slot
5615 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5616 if (*slot == NULL)
5618 temp = ggc_cleared_alloc<var_loc_list> ();
5619 temp->decl_id = decl_id;
5620 *slot = temp;
5622 else
5623 temp = *slot;
5625 /* For PARM_DECLs try to keep around the original incoming value,
5626 even if that means we'll emit a zero-range .debug_loc entry. */
5627 if (temp->last
5628 && temp->first == temp->last
5629 && TREE_CODE (decl) == PARM_DECL
5630 && NOTE_P (temp->first->loc)
5631 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5632 && DECL_INCOMING_RTL (decl)
5633 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5634 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5635 == GET_CODE (DECL_INCOMING_RTL (decl))
5636 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5637 && (bitsize != -1
5638 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5639 NOTE_VAR_LOCATION_LOC (loc_note))
5640 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5641 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5643 loc = ggc_cleared_alloc<var_loc_node> ();
5644 temp->first->next = loc;
5645 temp->last = loc;
5646 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5648 else if (temp->last)
5650 struct var_loc_node *last = temp->last, *unused = NULL;
5651 rtx *piece_loc = NULL, last_loc_note;
5652 HOST_WIDE_INT piece_bitpos = 0;
5653 if (last->next)
5655 last = last->next;
5656 gcc_assert (last->next == NULL);
5658 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5660 piece_loc = &last->loc;
5663 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5664 if (piece_bitpos + cur_bitsize > bitpos)
5665 break;
5666 piece_bitpos += cur_bitsize;
5667 piece_loc = &XEXP (*piece_loc, 1);
5669 while (*piece_loc);
5671 /* TEMP->LAST here is either pointer to the last but one or
5672 last element in the chained list, LAST is pointer to the
5673 last element. */
5674 if (label && strcmp (last->label, label) == 0)
5676 /* For SRA optimized variables if there weren't any real
5677 insns since last note, just modify the last node. */
5678 if (piece_loc != NULL)
5680 adjust_piece_list (piece_loc, NULL, NULL,
5681 bitpos, piece_bitpos, bitsize, loc_note);
5682 return NULL;
5684 /* If the last note doesn't cover any instructions, remove it. */
5685 if (temp->last != last)
5687 temp->last->next = NULL;
5688 unused = last;
5689 last = temp->last;
5690 gcc_assert (strcmp (last->label, label) != 0);
5692 else
5694 gcc_assert (temp->first == temp->last
5695 || (temp->first->next == temp->last
5696 && TREE_CODE (decl) == PARM_DECL));
5697 memset (temp->last, '\0', sizeof (*temp->last));
5698 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5699 return temp->last;
5702 if (bitsize == -1 && NOTE_P (last->loc))
5703 last_loc_note = last->loc;
5704 else if (piece_loc != NULL
5705 && *piece_loc != NULL_RTX
5706 && piece_bitpos == bitpos
5707 && decl_piece_bitsize (*piece_loc) == bitsize)
5708 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5709 else
5710 last_loc_note = NULL_RTX;
5711 /* If the current location is the same as the end of the list,
5712 and either both or neither of the locations is uninitialized,
5713 we have nothing to do. */
5714 if (last_loc_note == NULL_RTX
5715 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5716 NOTE_VAR_LOCATION_LOC (loc_note)))
5717 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5718 != NOTE_VAR_LOCATION_STATUS (loc_note))
5719 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5720 == VAR_INIT_STATUS_UNINITIALIZED)
5721 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5722 == VAR_INIT_STATUS_UNINITIALIZED))))
5724 /* Add LOC to the end of list and update LAST. If the last
5725 element of the list has been removed above, reuse its
5726 memory for the new node, otherwise allocate a new one. */
5727 if (unused)
5729 loc = unused;
5730 memset (loc, '\0', sizeof (*loc));
5732 else
5733 loc = ggc_cleared_alloc<var_loc_node> ();
5734 if (bitsize == -1 || piece_loc == NULL)
5735 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5736 else
5737 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5738 bitpos, piece_bitpos, bitsize, loc_note);
5739 last->next = loc;
5740 /* Ensure TEMP->LAST will point either to the new last but one
5741 element of the chain, or to the last element in it. */
5742 if (last != temp->last)
5743 temp->last = last;
5745 else if (unused)
5746 ggc_free (unused);
5748 else
5750 loc = ggc_cleared_alloc<var_loc_node> ();
5751 temp->first = loc;
5752 temp->last = loc;
5753 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5755 return loc;
5758 /* Keep track of the number of spaces used to indent the
5759 output of the debugging routines that print the structure of
5760 the DIE internal representation. */
5761 static int print_indent;
5763 /* Indent the line the number of spaces given by print_indent. */
5765 static inline void
5766 print_spaces (FILE *outfile)
5768 fprintf (outfile, "%*s", print_indent, "");
5771 /* Print a type signature in hex. */
5773 static inline void
5774 print_signature (FILE *outfile, char *sig)
5776 int i;
5778 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5779 fprintf (outfile, "%02x", sig[i] & 0xff);
5782 static inline void
5783 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5785 if (discr_value->pos)
5786 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5787 else
5788 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5791 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5793 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5794 RECURSE, output location descriptor operations. */
5796 static void
5797 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5799 switch (val->val_class)
5801 case dw_val_class_addr:
5802 fprintf (outfile, "address");
5803 break;
5804 case dw_val_class_offset:
5805 fprintf (outfile, "offset");
5806 break;
5807 case dw_val_class_loc:
5808 fprintf (outfile, "location descriptor");
5809 if (val->v.val_loc == NULL)
5810 fprintf (outfile, " -> <null>\n");
5811 else if (recurse)
5813 fprintf (outfile, ":\n");
5814 print_indent += 4;
5815 print_loc_descr (val->v.val_loc, outfile);
5816 print_indent -= 4;
5818 else
5819 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5820 break;
5821 case dw_val_class_loc_list:
5822 fprintf (outfile, "location list -> label:%s",
5823 val->v.val_loc_list->ll_symbol);
5824 break;
5825 case dw_val_class_range_list:
5826 fprintf (outfile, "range list");
5827 break;
5828 case dw_val_class_const:
5829 case dw_val_class_const_implicit:
5830 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5831 break;
5832 case dw_val_class_unsigned_const:
5833 case dw_val_class_unsigned_const_implicit:
5834 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5835 break;
5836 case dw_val_class_const_double:
5837 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5838 HOST_WIDE_INT_PRINT_UNSIGNED")",
5839 val->v.val_double.high,
5840 val->v.val_double.low);
5841 break;
5842 case dw_val_class_wide_int:
5844 int i = val->v.val_wide->get_len ();
5845 fprintf (outfile, "constant (");
5846 gcc_assert (i > 0);
5847 if (val->v.val_wide->elt (i - 1) == 0)
5848 fprintf (outfile, "0x");
5849 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5850 val->v.val_wide->elt (--i));
5851 while (--i >= 0)
5852 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5853 val->v.val_wide->elt (i));
5854 fprintf (outfile, ")");
5855 break;
5857 case dw_val_class_vec:
5858 fprintf (outfile, "floating-point or vector constant");
5859 break;
5860 case dw_val_class_flag:
5861 fprintf (outfile, "%u", val->v.val_flag);
5862 break;
5863 case dw_val_class_die_ref:
5864 if (val->v.val_die_ref.die != NULL)
5866 dw_die_ref die = val->v.val_die_ref.die;
5868 if (die->comdat_type_p)
5870 fprintf (outfile, "die -> signature: ");
5871 print_signature (outfile,
5872 die->die_id.die_type_node->signature);
5874 else if (die->die_id.die_symbol)
5875 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5876 else
5877 fprintf (outfile, "die -> %ld", die->die_offset);
5878 fprintf (outfile, " (%p)", (void *) die);
5880 else
5881 fprintf (outfile, "die -> <null>");
5882 break;
5883 case dw_val_class_vms_delta:
5884 fprintf (outfile, "delta: @slotcount(%s-%s)",
5885 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5886 break;
5887 case dw_val_class_lbl_id:
5888 case dw_val_class_lineptr:
5889 case dw_val_class_macptr:
5890 case dw_val_class_loclistsptr:
5891 case dw_val_class_high_pc:
5892 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5893 break;
5894 case dw_val_class_str:
5895 if (val->v.val_str->str != NULL)
5896 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5897 else
5898 fprintf (outfile, "<null>");
5899 break;
5900 case dw_val_class_file:
5901 case dw_val_class_file_implicit:
5902 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5903 val->v.val_file->emitted_number);
5904 break;
5905 case dw_val_class_data8:
5907 int i;
5909 for (i = 0; i < 8; i++)
5910 fprintf (outfile, "%02x", val->v.val_data8[i]);
5911 break;
5913 case dw_val_class_discr_value:
5914 print_discr_value (outfile, &val->v.val_discr_value);
5915 break;
5916 case dw_val_class_discr_list:
5917 for (dw_discr_list_ref node = val->v.val_discr_list;
5918 node != NULL;
5919 node = node->dw_discr_next)
5921 if (node->dw_discr_range)
5923 fprintf (outfile, " .. ");
5924 print_discr_value (outfile, &node->dw_discr_lower_bound);
5925 print_discr_value (outfile, &node->dw_discr_upper_bound);
5927 else
5928 print_discr_value (outfile, &node->dw_discr_lower_bound);
5930 if (node->dw_discr_next != NULL)
5931 fprintf (outfile, " | ");
5933 default:
5934 break;
5938 /* Likewise, for a DIE attribute. */
5940 static void
5941 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5943 print_dw_val (&a->dw_attr_val, recurse, outfile);
5947 /* Print the list of operands in the LOC location description to OUTFILE. This
5948 routine is a debugging aid only. */
5950 static void
5951 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5953 dw_loc_descr_ref l = loc;
5955 if (loc == NULL)
5957 print_spaces (outfile);
5958 fprintf (outfile, "<null>\n");
5959 return;
5962 for (l = loc; l != NULL; l = l->dw_loc_next)
5964 print_spaces (outfile);
5965 fprintf (outfile, "(%p) %s",
5966 (void *) l,
5967 dwarf_stack_op_name (l->dw_loc_opc));
5968 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5970 fprintf (outfile, " ");
5971 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5973 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5975 fprintf (outfile, ", ");
5976 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5978 fprintf (outfile, "\n");
5982 /* Print the information associated with a given DIE, and its children.
5983 This routine is a debugging aid only. */
5985 static void
5986 print_die (dw_die_ref die, FILE *outfile)
5988 dw_attr_node *a;
5989 dw_die_ref c;
5990 unsigned ix;
5992 print_spaces (outfile);
5993 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5994 die->die_offset, dwarf_tag_name (die->die_tag),
5995 (void*) die);
5996 print_spaces (outfile);
5997 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5998 fprintf (outfile, " offset: %ld", die->die_offset);
5999 fprintf (outfile, " mark: %d\n", die->die_mark);
6001 if (die->comdat_type_p)
6003 print_spaces (outfile);
6004 fprintf (outfile, " signature: ");
6005 print_signature (outfile, die->die_id.die_type_node->signature);
6006 fprintf (outfile, "\n");
6009 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6011 print_spaces (outfile);
6012 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6014 print_attribute (a, true, outfile);
6015 fprintf (outfile, "\n");
6018 if (die->die_child != NULL)
6020 print_indent += 4;
6021 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6022 print_indent -= 4;
6024 if (print_indent == 0)
6025 fprintf (outfile, "\n");
6028 /* Print the list of operations in the LOC location description. */
6030 DEBUG_FUNCTION void
6031 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6033 print_loc_descr (loc, stderr);
6036 /* Print the information collected for a given DIE. */
6038 DEBUG_FUNCTION void
6039 debug_dwarf_die (dw_die_ref die)
6041 print_die (die, stderr);
6044 DEBUG_FUNCTION void
6045 debug (die_struct &ref)
6047 print_die (&ref, stderr);
6050 DEBUG_FUNCTION void
6051 debug (die_struct *ptr)
6053 if (ptr)
6054 debug (*ptr);
6055 else
6056 fprintf (stderr, "<nil>\n");
6060 /* Print all DWARF information collected for the compilation unit.
6061 This routine is a debugging aid only. */
6063 DEBUG_FUNCTION void
6064 debug_dwarf (void)
6066 print_indent = 0;
6067 print_die (comp_unit_die (), stderr);
6070 /* Verify the DIE tree structure. */
6072 DEBUG_FUNCTION void
6073 verify_die (dw_die_ref die)
6075 gcc_assert (!die->die_mark);
6076 if (die->die_parent == NULL
6077 && die->die_sib == NULL)
6078 return;
6079 /* Verify the die_sib list is cyclic. */
6080 dw_die_ref x = die;
6083 x->die_mark = 1;
6084 x = x->die_sib;
6086 while (x && !x->die_mark);
6087 gcc_assert (x == die);
6088 x = die;
6091 /* Verify all dies have the same parent. */
6092 gcc_assert (x->die_parent == die->die_parent);
6093 if (x->die_child)
6095 /* Verify the child has the proper parent and recurse. */
6096 gcc_assert (x->die_child->die_parent == x);
6097 verify_die (x->die_child);
6099 x->die_mark = 0;
6100 x = x->die_sib;
6102 while (x && x->die_mark);
6105 /* Sanity checks on DIEs. */
6107 static void
6108 check_die (dw_die_ref die)
6110 unsigned ix;
6111 dw_attr_node *a;
6112 bool inline_found = false;
6113 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6114 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6115 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6117 switch (a->dw_attr)
6119 case DW_AT_inline:
6120 if (a->dw_attr_val.v.val_unsigned)
6121 inline_found = true;
6122 break;
6123 case DW_AT_location:
6124 ++n_location;
6125 break;
6126 case DW_AT_low_pc:
6127 ++n_low_pc;
6128 break;
6129 case DW_AT_high_pc:
6130 ++n_high_pc;
6131 break;
6132 case DW_AT_artificial:
6133 ++n_artificial;
6134 break;
6135 case DW_AT_decl_column:
6136 ++n_decl_column;
6137 break;
6138 case DW_AT_decl_line:
6139 ++n_decl_line;
6140 break;
6141 case DW_AT_decl_file:
6142 ++n_decl_file;
6143 break;
6144 default:
6145 break;
6148 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6149 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6151 fprintf (stderr, "Duplicate attributes in DIE:\n");
6152 debug_dwarf_die (die);
6153 gcc_unreachable ();
6155 if (inline_found)
6157 /* A debugging information entry that is a member of an abstract
6158 instance tree [that has DW_AT_inline] should not contain any
6159 attributes which describe aspects of the subroutine which vary
6160 between distinct inlined expansions or distinct out-of-line
6161 expansions. */
6162 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6163 gcc_assert (a->dw_attr != DW_AT_low_pc
6164 && a->dw_attr != DW_AT_high_pc
6165 && a->dw_attr != DW_AT_location
6166 && a->dw_attr != DW_AT_frame_base
6167 && a->dw_attr != DW_AT_call_all_calls
6168 && a->dw_attr != DW_AT_GNU_all_call_sites);
6172 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6173 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6174 DIE that marks the start of the DIEs for this include file. */
6176 static dw_die_ref
6177 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6179 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6180 dw_die_ref new_unit = gen_compile_unit_die (filename);
6182 new_unit->die_sib = old_unit;
6183 return new_unit;
6186 /* Close an include-file CU and reopen the enclosing one. */
6188 static dw_die_ref
6189 pop_compile_unit (dw_die_ref old_unit)
6191 dw_die_ref new_unit = old_unit->die_sib;
6193 old_unit->die_sib = NULL;
6194 return new_unit;
6197 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6198 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6199 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6201 /* Calculate the checksum of a location expression. */
6203 static inline void
6204 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6206 int tem;
6207 inchash::hash hstate;
6208 hashval_t hash;
6210 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6211 CHECKSUM (tem);
6212 hash_loc_operands (loc, hstate);
6213 hash = hstate.end();
6214 CHECKSUM (hash);
6217 /* Calculate the checksum of an attribute. */
6219 static void
6220 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6222 dw_loc_descr_ref loc;
6223 rtx r;
6225 CHECKSUM (at->dw_attr);
6227 /* We don't care that this was compiled with a different compiler
6228 snapshot; if the output is the same, that's what matters. */
6229 if (at->dw_attr == DW_AT_producer)
6230 return;
6232 switch (AT_class (at))
6234 case dw_val_class_const:
6235 case dw_val_class_const_implicit:
6236 CHECKSUM (at->dw_attr_val.v.val_int);
6237 break;
6238 case dw_val_class_unsigned_const:
6239 case dw_val_class_unsigned_const_implicit:
6240 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6241 break;
6242 case dw_val_class_const_double:
6243 CHECKSUM (at->dw_attr_val.v.val_double);
6244 break;
6245 case dw_val_class_wide_int:
6246 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6247 get_full_len (*at->dw_attr_val.v.val_wide)
6248 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6249 break;
6250 case dw_val_class_vec:
6251 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6252 (at->dw_attr_val.v.val_vec.length
6253 * at->dw_attr_val.v.val_vec.elt_size));
6254 break;
6255 case dw_val_class_flag:
6256 CHECKSUM (at->dw_attr_val.v.val_flag);
6257 break;
6258 case dw_val_class_str:
6259 CHECKSUM_STRING (AT_string (at));
6260 break;
6262 case dw_val_class_addr:
6263 r = AT_addr (at);
6264 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6265 CHECKSUM_STRING (XSTR (r, 0));
6266 break;
6268 case dw_val_class_offset:
6269 CHECKSUM (at->dw_attr_val.v.val_offset);
6270 break;
6272 case dw_val_class_loc:
6273 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6274 loc_checksum (loc, ctx);
6275 break;
6277 case dw_val_class_die_ref:
6278 die_checksum (AT_ref (at), ctx, mark);
6279 break;
6281 case dw_val_class_fde_ref:
6282 case dw_val_class_vms_delta:
6283 case dw_val_class_lbl_id:
6284 case dw_val_class_lineptr:
6285 case dw_val_class_macptr:
6286 case dw_val_class_loclistsptr:
6287 case dw_val_class_high_pc:
6288 break;
6290 case dw_val_class_file:
6291 case dw_val_class_file_implicit:
6292 CHECKSUM_STRING (AT_file (at)->filename);
6293 break;
6295 case dw_val_class_data8:
6296 CHECKSUM (at->dw_attr_val.v.val_data8);
6297 break;
6299 default:
6300 break;
6304 /* Calculate the checksum of a DIE. */
6306 static void
6307 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6309 dw_die_ref c;
6310 dw_attr_node *a;
6311 unsigned ix;
6313 /* To avoid infinite recursion. */
6314 if (die->die_mark)
6316 CHECKSUM (die->die_mark);
6317 return;
6319 die->die_mark = ++(*mark);
6321 CHECKSUM (die->die_tag);
6323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6324 attr_checksum (a, ctx, mark);
6326 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6329 #undef CHECKSUM
6330 #undef CHECKSUM_BLOCK
6331 #undef CHECKSUM_STRING
6333 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6334 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6335 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6336 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6337 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6338 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6339 #define CHECKSUM_ATTR(FOO) \
6340 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6342 /* Calculate the checksum of a number in signed LEB128 format. */
6344 static void
6345 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6347 unsigned char byte;
6348 bool more;
6350 while (1)
6352 byte = (value & 0x7f);
6353 value >>= 7;
6354 more = !((value == 0 && (byte & 0x40) == 0)
6355 || (value == -1 && (byte & 0x40) != 0));
6356 if (more)
6357 byte |= 0x80;
6358 CHECKSUM (byte);
6359 if (!more)
6360 break;
6364 /* Calculate the checksum of a number in unsigned LEB128 format. */
6366 static void
6367 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6369 while (1)
6371 unsigned char byte = (value & 0x7f);
6372 value >>= 7;
6373 if (value != 0)
6374 /* More bytes to follow. */
6375 byte |= 0x80;
6376 CHECKSUM (byte);
6377 if (value == 0)
6378 break;
6382 /* Checksum the context of the DIE. This adds the names of any
6383 surrounding namespaces or structures to the checksum. */
6385 static void
6386 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6388 const char *name;
6389 dw_die_ref spec;
6390 int tag = die->die_tag;
6392 if (tag != DW_TAG_namespace
6393 && tag != DW_TAG_structure_type
6394 && tag != DW_TAG_class_type)
6395 return;
6397 name = get_AT_string (die, DW_AT_name);
6399 spec = get_AT_ref (die, DW_AT_specification);
6400 if (spec != NULL)
6401 die = spec;
6403 if (die->die_parent != NULL)
6404 checksum_die_context (die->die_parent, ctx);
6406 CHECKSUM_ULEB128 ('C');
6407 CHECKSUM_ULEB128 (tag);
6408 if (name != NULL)
6409 CHECKSUM_STRING (name);
6412 /* Calculate the checksum of a location expression. */
6414 static inline void
6415 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6417 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6418 were emitted as a DW_FORM_sdata instead of a location expression. */
6419 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6421 CHECKSUM_ULEB128 (DW_FORM_sdata);
6422 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6423 return;
6426 /* Otherwise, just checksum the raw location expression. */
6427 while (loc != NULL)
6429 inchash::hash hstate;
6430 hashval_t hash;
6432 CHECKSUM_ULEB128 (loc->dtprel);
6433 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6434 hash_loc_operands (loc, hstate);
6435 hash = hstate.end ();
6436 CHECKSUM (hash);
6437 loc = loc->dw_loc_next;
6441 /* Calculate the checksum of an attribute. */
6443 static void
6444 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6445 struct md5_ctx *ctx, int *mark)
6447 dw_loc_descr_ref loc;
6448 rtx r;
6450 if (AT_class (at) == dw_val_class_die_ref)
6452 dw_die_ref target_die = AT_ref (at);
6454 /* For pointer and reference types, we checksum only the (qualified)
6455 name of the target type (if there is a name). For friend entries,
6456 we checksum only the (qualified) name of the target type or function.
6457 This allows the checksum to remain the same whether the target type
6458 is complete or not. */
6459 if ((at->dw_attr == DW_AT_type
6460 && (tag == DW_TAG_pointer_type
6461 || tag == DW_TAG_reference_type
6462 || tag == DW_TAG_rvalue_reference_type
6463 || tag == DW_TAG_ptr_to_member_type))
6464 || (at->dw_attr == DW_AT_friend
6465 && tag == DW_TAG_friend))
6467 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6469 if (name_attr != NULL)
6471 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6473 if (decl == NULL)
6474 decl = target_die;
6475 CHECKSUM_ULEB128 ('N');
6476 CHECKSUM_ULEB128 (at->dw_attr);
6477 if (decl->die_parent != NULL)
6478 checksum_die_context (decl->die_parent, ctx);
6479 CHECKSUM_ULEB128 ('E');
6480 CHECKSUM_STRING (AT_string (name_attr));
6481 return;
6485 /* For all other references to another DIE, we check to see if the
6486 target DIE has already been visited. If it has, we emit a
6487 backward reference; if not, we descend recursively. */
6488 if (target_die->die_mark > 0)
6490 CHECKSUM_ULEB128 ('R');
6491 CHECKSUM_ULEB128 (at->dw_attr);
6492 CHECKSUM_ULEB128 (target_die->die_mark);
6494 else
6496 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6498 if (decl == NULL)
6499 decl = target_die;
6500 target_die->die_mark = ++(*mark);
6501 CHECKSUM_ULEB128 ('T');
6502 CHECKSUM_ULEB128 (at->dw_attr);
6503 if (decl->die_parent != NULL)
6504 checksum_die_context (decl->die_parent, ctx);
6505 die_checksum_ordered (target_die, ctx, mark);
6507 return;
6510 CHECKSUM_ULEB128 ('A');
6511 CHECKSUM_ULEB128 (at->dw_attr);
6513 switch (AT_class (at))
6515 case dw_val_class_const:
6516 case dw_val_class_const_implicit:
6517 CHECKSUM_ULEB128 (DW_FORM_sdata);
6518 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6519 break;
6521 case dw_val_class_unsigned_const:
6522 case dw_val_class_unsigned_const_implicit:
6523 CHECKSUM_ULEB128 (DW_FORM_sdata);
6524 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6525 break;
6527 case dw_val_class_const_double:
6528 CHECKSUM_ULEB128 (DW_FORM_block);
6529 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6530 CHECKSUM (at->dw_attr_val.v.val_double);
6531 break;
6533 case dw_val_class_wide_int:
6534 CHECKSUM_ULEB128 (DW_FORM_block);
6535 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6536 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6537 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6538 get_full_len (*at->dw_attr_val.v.val_wide)
6539 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6540 break;
6542 case dw_val_class_vec:
6543 CHECKSUM_ULEB128 (DW_FORM_block);
6544 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6545 * at->dw_attr_val.v.val_vec.elt_size);
6546 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6547 (at->dw_attr_val.v.val_vec.length
6548 * at->dw_attr_val.v.val_vec.elt_size));
6549 break;
6551 case dw_val_class_flag:
6552 CHECKSUM_ULEB128 (DW_FORM_flag);
6553 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6554 break;
6556 case dw_val_class_str:
6557 CHECKSUM_ULEB128 (DW_FORM_string);
6558 CHECKSUM_STRING (AT_string (at));
6559 break;
6561 case dw_val_class_addr:
6562 r = AT_addr (at);
6563 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6564 CHECKSUM_ULEB128 (DW_FORM_string);
6565 CHECKSUM_STRING (XSTR (r, 0));
6566 break;
6568 case dw_val_class_offset:
6569 CHECKSUM_ULEB128 (DW_FORM_sdata);
6570 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6571 break;
6573 case dw_val_class_loc:
6574 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6575 loc_checksum_ordered (loc, ctx);
6576 break;
6578 case dw_val_class_fde_ref:
6579 case dw_val_class_lbl_id:
6580 case dw_val_class_lineptr:
6581 case dw_val_class_macptr:
6582 case dw_val_class_loclistsptr:
6583 case dw_val_class_high_pc:
6584 break;
6586 case dw_val_class_file:
6587 case dw_val_class_file_implicit:
6588 CHECKSUM_ULEB128 (DW_FORM_string);
6589 CHECKSUM_STRING (AT_file (at)->filename);
6590 break;
6592 case dw_val_class_data8:
6593 CHECKSUM (at->dw_attr_val.v.val_data8);
6594 break;
6596 default:
6597 break;
6601 struct checksum_attributes
6603 dw_attr_node *at_name;
6604 dw_attr_node *at_type;
6605 dw_attr_node *at_friend;
6606 dw_attr_node *at_accessibility;
6607 dw_attr_node *at_address_class;
6608 dw_attr_node *at_allocated;
6609 dw_attr_node *at_artificial;
6610 dw_attr_node *at_associated;
6611 dw_attr_node *at_binary_scale;
6612 dw_attr_node *at_bit_offset;
6613 dw_attr_node *at_bit_size;
6614 dw_attr_node *at_bit_stride;
6615 dw_attr_node *at_byte_size;
6616 dw_attr_node *at_byte_stride;
6617 dw_attr_node *at_const_value;
6618 dw_attr_node *at_containing_type;
6619 dw_attr_node *at_count;
6620 dw_attr_node *at_data_location;
6621 dw_attr_node *at_data_member_location;
6622 dw_attr_node *at_decimal_scale;
6623 dw_attr_node *at_decimal_sign;
6624 dw_attr_node *at_default_value;
6625 dw_attr_node *at_digit_count;
6626 dw_attr_node *at_discr;
6627 dw_attr_node *at_discr_list;
6628 dw_attr_node *at_discr_value;
6629 dw_attr_node *at_encoding;
6630 dw_attr_node *at_endianity;
6631 dw_attr_node *at_explicit;
6632 dw_attr_node *at_is_optional;
6633 dw_attr_node *at_location;
6634 dw_attr_node *at_lower_bound;
6635 dw_attr_node *at_mutable;
6636 dw_attr_node *at_ordering;
6637 dw_attr_node *at_picture_string;
6638 dw_attr_node *at_prototyped;
6639 dw_attr_node *at_small;
6640 dw_attr_node *at_segment;
6641 dw_attr_node *at_string_length;
6642 dw_attr_node *at_string_length_bit_size;
6643 dw_attr_node *at_string_length_byte_size;
6644 dw_attr_node *at_threads_scaled;
6645 dw_attr_node *at_upper_bound;
6646 dw_attr_node *at_use_location;
6647 dw_attr_node *at_use_UTF8;
6648 dw_attr_node *at_variable_parameter;
6649 dw_attr_node *at_virtuality;
6650 dw_attr_node *at_visibility;
6651 dw_attr_node *at_vtable_elem_location;
6654 /* Collect the attributes that we will want to use for the checksum. */
6656 static void
6657 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6659 dw_attr_node *a;
6660 unsigned ix;
6662 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6664 switch (a->dw_attr)
6666 case DW_AT_name:
6667 attrs->at_name = a;
6668 break;
6669 case DW_AT_type:
6670 attrs->at_type = a;
6671 break;
6672 case DW_AT_friend:
6673 attrs->at_friend = a;
6674 break;
6675 case DW_AT_accessibility:
6676 attrs->at_accessibility = a;
6677 break;
6678 case DW_AT_address_class:
6679 attrs->at_address_class = a;
6680 break;
6681 case DW_AT_allocated:
6682 attrs->at_allocated = a;
6683 break;
6684 case DW_AT_artificial:
6685 attrs->at_artificial = a;
6686 break;
6687 case DW_AT_associated:
6688 attrs->at_associated = a;
6689 break;
6690 case DW_AT_binary_scale:
6691 attrs->at_binary_scale = a;
6692 break;
6693 case DW_AT_bit_offset:
6694 attrs->at_bit_offset = a;
6695 break;
6696 case DW_AT_bit_size:
6697 attrs->at_bit_size = a;
6698 break;
6699 case DW_AT_bit_stride:
6700 attrs->at_bit_stride = a;
6701 break;
6702 case DW_AT_byte_size:
6703 attrs->at_byte_size = a;
6704 break;
6705 case DW_AT_byte_stride:
6706 attrs->at_byte_stride = a;
6707 break;
6708 case DW_AT_const_value:
6709 attrs->at_const_value = a;
6710 break;
6711 case DW_AT_containing_type:
6712 attrs->at_containing_type = a;
6713 break;
6714 case DW_AT_count:
6715 attrs->at_count = a;
6716 break;
6717 case DW_AT_data_location:
6718 attrs->at_data_location = a;
6719 break;
6720 case DW_AT_data_member_location:
6721 attrs->at_data_member_location = a;
6722 break;
6723 case DW_AT_decimal_scale:
6724 attrs->at_decimal_scale = a;
6725 break;
6726 case DW_AT_decimal_sign:
6727 attrs->at_decimal_sign = a;
6728 break;
6729 case DW_AT_default_value:
6730 attrs->at_default_value = a;
6731 break;
6732 case DW_AT_digit_count:
6733 attrs->at_digit_count = a;
6734 break;
6735 case DW_AT_discr:
6736 attrs->at_discr = a;
6737 break;
6738 case DW_AT_discr_list:
6739 attrs->at_discr_list = a;
6740 break;
6741 case DW_AT_discr_value:
6742 attrs->at_discr_value = a;
6743 break;
6744 case DW_AT_encoding:
6745 attrs->at_encoding = a;
6746 break;
6747 case DW_AT_endianity:
6748 attrs->at_endianity = a;
6749 break;
6750 case DW_AT_explicit:
6751 attrs->at_explicit = a;
6752 break;
6753 case DW_AT_is_optional:
6754 attrs->at_is_optional = a;
6755 break;
6756 case DW_AT_location:
6757 attrs->at_location = a;
6758 break;
6759 case DW_AT_lower_bound:
6760 attrs->at_lower_bound = a;
6761 break;
6762 case DW_AT_mutable:
6763 attrs->at_mutable = a;
6764 break;
6765 case DW_AT_ordering:
6766 attrs->at_ordering = a;
6767 break;
6768 case DW_AT_picture_string:
6769 attrs->at_picture_string = a;
6770 break;
6771 case DW_AT_prototyped:
6772 attrs->at_prototyped = a;
6773 break;
6774 case DW_AT_small:
6775 attrs->at_small = a;
6776 break;
6777 case DW_AT_segment:
6778 attrs->at_segment = a;
6779 break;
6780 case DW_AT_string_length:
6781 attrs->at_string_length = a;
6782 break;
6783 case DW_AT_string_length_bit_size:
6784 attrs->at_string_length_bit_size = a;
6785 break;
6786 case DW_AT_string_length_byte_size:
6787 attrs->at_string_length_byte_size = a;
6788 break;
6789 case DW_AT_threads_scaled:
6790 attrs->at_threads_scaled = a;
6791 break;
6792 case DW_AT_upper_bound:
6793 attrs->at_upper_bound = a;
6794 break;
6795 case DW_AT_use_location:
6796 attrs->at_use_location = a;
6797 break;
6798 case DW_AT_use_UTF8:
6799 attrs->at_use_UTF8 = a;
6800 break;
6801 case DW_AT_variable_parameter:
6802 attrs->at_variable_parameter = a;
6803 break;
6804 case DW_AT_virtuality:
6805 attrs->at_virtuality = a;
6806 break;
6807 case DW_AT_visibility:
6808 attrs->at_visibility = a;
6809 break;
6810 case DW_AT_vtable_elem_location:
6811 attrs->at_vtable_elem_location = a;
6812 break;
6813 default:
6814 break;
6819 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6821 static void
6822 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6824 dw_die_ref c;
6825 dw_die_ref decl;
6826 struct checksum_attributes attrs;
6828 CHECKSUM_ULEB128 ('D');
6829 CHECKSUM_ULEB128 (die->die_tag);
6831 memset (&attrs, 0, sizeof (attrs));
6833 decl = get_AT_ref (die, DW_AT_specification);
6834 if (decl != NULL)
6835 collect_checksum_attributes (&attrs, decl);
6836 collect_checksum_attributes (&attrs, die);
6838 CHECKSUM_ATTR (attrs.at_name);
6839 CHECKSUM_ATTR (attrs.at_accessibility);
6840 CHECKSUM_ATTR (attrs.at_address_class);
6841 CHECKSUM_ATTR (attrs.at_allocated);
6842 CHECKSUM_ATTR (attrs.at_artificial);
6843 CHECKSUM_ATTR (attrs.at_associated);
6844 CHECKSUM_ATTR (attrs.at_binary_scale);
6845 CHECKSUM_ATTR (attrs.at_bit_offset);
6846 CHECKSUM_ATTR (attrs.at_bit_size);
6847 CHECKSUM_ATTR (attrs.at_bit_stride);
6848 CHECKSUM_ATTR (attrs.at_byte_size);
6849 CHECKSUM_ATTR (attrs.at_byte_stride);
6850 CHECKSUM_ATTR (attrs.at_const_value);
6851 CHECKSUM_ATTR (attrs.at_containing_type);
6852 CHECKSUM_ATTR (attrs.at_count);
6853 CHECKSUM_ATTR (attrs.at_data_location);
6854 CHECKSUM_ATTR (attrs.at_data_member_location);
6855 CHECKSUM_ATTR (attrs.at_decimal_scale);
6856 CHECKSUM_ATTR (attrs.at_decimal_sign);
6857 CHECKSUM_ATTR (attrs.at_default_value);
6858 CHECKSUM_ATTR (attrs.at_digit_count);
6859 CHECKSUM_ATTR (attrs.at_discr);
6860 CHECKSUM_ATTR (attrs.at_discr_list);
6861 CHECKSUM_ATTR (attrs.at_discr_value);
6862 CHECKSUM_ATTR (attrs.at_encoding);
6863 CHECKSUM_ATTR (attrs.at_endianity);
6864 CHECKSUM_ATTR (attrs.at_explicit);
6865 CHECKSUM_ATTR (attrs.at_is_optional);
6866 CHECKSUM_ATTR (attrs.at_location);
6867 CHECKSUM_ATTR (attrs.at_lower_bound);
6868 CHECKSUM_ATTR (attrs.at_mutable);
6869 CHECKSUM_ATTR (attrs.at_ordering);
6870 CHECKSUM_ATTR (attrs.at_picture_string);
6871 CHECKSUM_ATTR (attrs.at_prototyped);
6872 CHECKSUM_ATTR (attrs.at_small);
6873 CHECKSUM_ATTR (attrs.at_segment);
6874 CHECKSUM_ATTR (attrs.at_string_length);
6875 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6876 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6877 CHECKSUM_ATTR (attrs.at_threads_scaled);
6878 CHECKSUM_ATTR (attrs.at_upper_bound);
6879 CHECKSUM_ATTR (attrs.at_use_location);
6880 CHECKSUM_ATTR (attrs.at_use_UTF8);
6881 CHECKSUM_ATTR (attrs.at_variable_parameter);
6882 CHECKSUM_ATTR (attrs.at_virtuality);
6883 CHECKSUM_ATTR (attrs.at_visibility);
6884 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6885 CHECKSUM_ATTR (attrs.at_type);
6886 CHECKSUM_ATTR (attrs.at_friend);
6888 /* Checksum the child DIEs. */
6889 c = die->die_child;
6890 if (c) do {
6891 dw_attr_node *name_attr;
6893 c = c->die_sib;
6894 name_attr = get_AT (c, DW_AT_name);
6895 if (is_template_instantiation (c))
6897 /* Ignore instantiations of member type and function templates. */
6899 else if (name_attr != NULL
6900 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6902 /* Use a shallow checksum for named nested types and member
6903 functions. */
6904 CHECKSUM_ULEB128 ('S');
6905 CHECKSUM_ULEB128 (c->die_tag);
6906 CHECKSUM_STRING (AT_string (name_attr));
6908 else
6910 /* Use a deep checksum for other children. */
6911 /* Mark this DIE so it gets processed when unmarking. */
6912 if (c->die_mark == 0)
6913 c->die_mark = -1;
6914 die_checksum_ordered (c, ctx, mark);
6916 } while (c != die->die_child);
6918 CHECKSUM_ULEB128 (0);
6921 /* Add a type name and tag to a hash. */
6922 static void
6923 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6925 CHECKSUM_ULEB128 (tag);
6926 CHECKSUM_STRING (name);
6929 #undef CHECKSUM
6930 #undef CHECKSUM_STRING
6931 #undef CHECKSUM_ATTR
6932 #undef CHECKSUM_LEB128
6933 #undef CHECKSUM_ULEB128
6935 /* Generate the type signature for DIE. This is computed by generating an
6936 MD5 checksum over the DIE's tag, its relevant attributes, and its
6937 children. Attributes that are references to other DIEs are processed
6938 by recursion, using the MARK field to prevent infinite recursion.
6939 If the DIE is nested inside a namespace or another type, we also
6940 need to include that context in the signature. The lower 64 bits
6941 of the resulting MD5 checksum comprise the signature. */
6943 static void
6944 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6946 int mark;
6947 const char *name;
6948 unsigned char checksum[16];
6949 struct md5_ctx ctx;
6950 dw_die_ref decl;
6951 dw_die_ref parent;
6953 name = get_AT_string (die, DW_AT_name);
6954 decl = get_AT_ref (die, DW_AT_specification);
6955 parent = get_die_parent (die);
6957 /* First, compute a signature for just the type name (and its surrounding
6958 context, if any. This is stored in the type unit DIE for link-time
6959 ODR (one-definition rule) checking. */
6961 if (is_cxx () && name != NULL)
6963 md5_init_ctx (&ctx);
6965 /* Checksum the names of surrounding namespaces and structures. */
6966 if (parent != NULL)
6967 checksum_die_context (parent, &ctx);
6969 /* Checksum the current DIE. */
6970 die_odr_checksum (die->die_tag, name, &ctx);
6971 md5_finish_ctx (&ctx, checksum);
6973 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6976 /* Next, compute the complete type signature. */
6978 md5_init_ctx (&ctx);
6979 mark = 1;
6980 die->die_mark = mark;
6982 /* Checksum the names of surrounding namespaces and structures. */
6983 if (parent != NULL)
6984 checksum_die_context (parent, &ctx);
6986 /* Checksum the DIE and its children. */
6987 die_checksum_ordered (die, &ctx, &mark);
6988 unmark_all_dies (die);
6989 md5_finish_ctx (&ctx, checksum);
6991 /* Store the signature in the type node and link the type DIE and the
6992 type node together. */
6993 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6994 DWARF_TYPE_SIGNATURE_SIZE);
6995 die->comdat_type_p = true;
6996 die->die_id.die_type_node = type_node;
6997 type_node->type_die = die;
6999 /* If the DIE is a specification, link its declaration to the type node
7000 as well. */
7001 if (decl != NULL)
7003 decl->comdat_type_p = true;
7004 decl->die_id.die_type_node = type_node;
7008 /* Do the location expressions look same? */
7009 static inline int
7010 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7012 return loc1->dw_loc_opc == loc2->dw_loc_opc
7013 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7014 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7017 /* Do the values look the same? */
7018 static int
7019 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7021 dw_loc_descr_ref loc1, loc2;
7022 rtx r1, r2;
7024 if (v1->val_class != v2->val_class)
7025 return 0;
7027 switch (v1->val_class)
7029 case dw_val_class_const:
7030 case dw_val_class_const_implicit:
7031 return v1->v.val_int == v2->v.val_int;
7032 case dw_val_class_unsigned_const:
7033 case dw_val_class_unsigned_const_implicit:
7034 return v1->v.val_unsigned == v2->v.val_unsigned;
7035 case dw_val_class_const_double:
7036 return v1->v.val_double.high == v2->v.val_double.high
7037 && v1->v.val_double.low == v2->v.val_double.low;
7038 case dw_val_class_wide_int:
7039 return *v1->v.val_wide == *v2->v.val_wide;
7040 case dw_val_class_vec:
7041 if (v1->v.val_vec.length != v2->v.val_vec.length
7042 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7043 return 0;
7044 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7045 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7046 return 0;
7047 return 1;
7048 case dw_val_class_flag:
7049 return v1->v.val_flag == v2->v.val_flag;
7050 case dw_val_class_str:
7051 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7053 case dw_val_class_addr:
7054 r1 = v1->v.val_addr;
7055 r2 = v2->v.val_addr;
7056 if (GET_CODE (r1) != GET_CODE (r2))
7057 return 0;
7058 return !rtx_equal_p (r1, r2);
7060 case dw_val_class_offset:
7061 return v1->v.val_offset == v2->v.val_offset;
7063 case dw_val_class_loc:
7064 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7065 loc1 && loc2;
7066 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7067 if (!same_loc_p (loc1, loc2, mark))
7068 return 0;
7069 return !loc1 && !loc2;
7071 case dw_val_class_die_ref:
7072 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7074 case dw_val_class_fde_ref:
7075 case dw_val_class_vms_delta:
7076 case dw_val_class_lbl_id:
7077 case dw_val_class_lineptr:
7078 case dw_val_class_macptr:
7079 case dw_val_class_loclistsptr:
7080 case dw_val_class_high_pc:
7081 return 1;
7083 case dw_val_class_file:
7084 case dw_val_class_file_implicit:
7085 return v1->v.val_file == v2->v.val_file;
7087 case dw_val_class_data8:
7088 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7090 default:
7091 return 1;
7095 /* Do the attributes look the same? */
7097 static int
7098 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7100 if (at1->dw_attr != at2->dw_attr)
7101 return 0;
7103 /* We don't care that this was compiled with a different compiler
7104 snapshot; if the output is the same, that's what matters. */
7105 if (at1->dw_attr == DW_AT_producer)
7106 return 1;
7108 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7111 /* Do the dies look the same? */
7113 static int
7114 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7116 dw_die_ref c1, c2;
7117 dw_attr_node *a1;
7118 unsigned ix;
7120 /* To avoid infinite recursion. */
7121 if (die1->die_mark)
7122 return die1->die_mark == die2->die_mark;
7123 die1->die_mark = die2->die_mark = ++(*mark);
7125 if (die1->die_tag != die2->die_tag)
7126 return 0;
7128 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7129 return 0;
7131 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7132 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7133 return 0;
7135 c1 = die1->die_child;
7136 c2 = die2->die_child;
7137 if (! c1)
7139 if (c2)
7140 return 0;
7142 else
7143 for (;;)
7145 if (!same_die_p (c1, c2, mark))
7146 return 0;
7147 c1 = c1->die_sib;
7148 c2 = c2->die_sib;
7149 if (c1 == die1->die_child)
7151 if (c2 == die2->die_child)
7152 break;
7153 else
7154 return 0;
7158 return 1;
7161 /* Do the dies look the same? Wrapper around same_die_p. */
7163 static int
7164 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7166 int mark = 0;
7167 int ret = same_die_p (die1, die2, &mark);
7169 unmark_all_dies (die1);
7170 unmark_all_dies (die2);
7172 return ret;
7175 /* The prefix to attach to symbols on DIEs in the current comdat debug
7176 info section. */
7177 static const char *comdat_symbol_id;
7179 /* The index of the current symbol within the current comdat CU. */
7180 static unsigned int comdat_symbol_number;
7182 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7183 children, and set comdat_symbol_id accordingly. */
7185 static void
7186 compute_section_prefix (dw_die_ref unit_die)
7188 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7189 const char *base = die_name ? lbasename (die_name) : "anonymous";
7190 char *name = XALLOCAVEC (char, strlen (base) + 64);
7191 char *p;
7192 int i, mark;
7193 unsigned char checksum[16];
7194 struct md5_ctx ctx;
7196 /* Compute the checksum of the DIE, then append part of it as hex digits to
7197 the name filename of the unit. */
7199 md5_init_ctx (&ctx);
7200 mark = 0;
7201 die_checksum (unit_die, &ctx, &mark);
7202 unmark_all_dies (unit_die);
7203 md5_finish_ctx (&ctx, checksum);
7205 sprintf (name, "%s.", base);
7206 clean_symbol_name (name);
7208 p = name + strlen (name);
7209 for (i = 0; i < 4; i++)
7211 sprintf (p, "%.2x", checksum[i]);
7212 p += 2;
7215 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7216 comdat_symbol_number = 0;
7219 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7221 static int
7222 is_type_die (dw_die_ref die)
7224 switch (die->die_tag)
7226 case DW_TAG_array_type:
7227 case DW_TAG_class_type:
7228 case DW_TAG_interface_type:
7229 case DW_TAG_enumeration_type:
7230 case DW_TAG_pointer_type:
7231 case DW_TAG_reference_type:
7232 case DW_TAG_rvalue_reference_type:
7233 case DW_TAG_string_type:
7234 case DW_TAG_structure_type:
7235 case DW_TAG_subroutine_type:
7236 case DW_TAG_union_type:
7237 case DW_TAG_ptr_to_member_type:
7238 case DW_TAG_set_type:
7239 case DW_TAG_subrange_type:
7240 case DW_TAG_base_type:
7241 case DW_TAG_const_type:
7242 case DW_TAG_file_type:
7243 case DW_TAG_packed_type:
7244 case DW_TAG_volatile_type:
7245 case DW_TAG_typedef:
7246 return 1;
7247 default:
7248 return 0;
7252 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7253 Basically, we want to choose the bits that are likely to be shared between
7254 compilations (types) and leave out the bits that are specific to individual
7255 compilations (functions). */
7257 static int
7258 is_comdat_die (dw_die_ref c)
7260 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7261 we do for stabs. The advantage is a greater likelihood of sharing between
7262 objects that don't include headers in the same order (and therefore would
7263 put the base types in a different comdat). jason 8/28/00 */
7265 if (c->die_tag == DW_TAG_base_type)
7266 return 0;
7268 if (c->die_tag == DW_TAG_pointer_type
7269 || c->die_tag == DW_TAG_reference_type
7270 || c->die_tag == DW_TAG_rvalue_reference_type
7271 || c->die_tag == DW_TAG_const_type
7272 || c->die_tag == DW_TAG_volatile_type)
7274 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7276 return t ? is_comdat_die (t) : 0;
7279 return is_type_die (c);
7282 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7283 compilation unit. */
7285 static int
7286 is_symbol_die (dw_die_ref c)
7288 return (is_type_die (c)
7289 || is_declaration_die (c)
7290 || c->die_tag == DW_TAG_namespace
7291 || c->die_tag == DW_TAG_module);
7294 /* Returns true iff C is a compile-unit DIE. */
7296 static inline bool
7297 is_cu_die (dw_die_ref c)
7299 return c && (c->die_tag == DW_TAG_compile_unit
7300 || c->die_tag == DW_TAG_skeleton_unit);
7303 /* Returns true iff C is a unit DIE of some sort. */
7305 static inline bool
7306 is_unit_die (dw_die_ref c)
7308 return c && (c->die_tag == DW_TAG_compile_unit
7309 || c->die_tag == DW_TAG_partial_unit
7310 || c->die_tag == DW_TAG_type_unit
7311 || c->die_tag == DW_TAG_skeleton_unit);
7314 /* Returns true iff C is a namespace DIE. */
7316 static inline bool
7317 is_namespace_die (dw_die_ref c)
7319 return c && c->die_tag == DW_TAG_namespace;
7322 /* Returns true iff C is a class or structure DIE. */
7324 static inline bool
7325 is_class_die (dw_die_ref c)
7327 return c && (c->die_tag == DW_TAG_class_type
7328 || c->die_tag == DW_TAG_structure_type);
7331 /* Return non-zero if this DIE is a template parameter. */
7333 static inline bool
7334 is_template_parameter (dw_die_ref die)
7336 switch (die->die_tag)
7338 case DW_TAG_template_type_param:
7339 case DW_TAG_template_value_param:
7340 case DW_TAG_GNU_template_template_param:
7341 case DW_TAG_GNU_template_parameter_pack:
7342 return true;
7343 default:
7344 return false;
7348 /* Return non-zero if this DIE represents a template instantiation. */
7350 static inline bool
7351 is_template_instantiation (dw_die_ref die)
7353 dw_die_ref c;
7355 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7356 return false;
7357 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7358 return false;
7361 static char *
7362 gen_internal_sym (const char *prefix)
7364 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7366 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7367 return xstrdup (buf);
7370 /* Assign symbols to all worthy DIEs under DIE. */
7372 static void
7373 assign_symbol_names (dw_die_ref die)
7375 dw_die_ref c;
7377 if (is_symbol_die (die) && !die->comdat_type_p)
7379 if (comdat_symbol_id)
7381 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7383 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7384 comdat_symbol_id, comdat_symbol_number++);
7385 die->die_id.die_symbol = xstrdup (p);
7387 else
7388 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7391 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7394 struct cu_hash_table_entry
7396 dw_die_ref cu;
7397 unsigned min_comdat_num, max_comdat_num;
7398 struct cu_hash_table_entry *next;
7401 /* Helpers to manipulate hash table of CUs. */
7403 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7405 typedef die_struct *compare_type;
7406 static inline hashval_t hash (const cu_hash_table_entry *);
7407 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7408 static inline void remove (cu_hash_table_entry *);
7411 inline hashval_t
7412 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7414 return htab_hash_string (entry->cu->die_id.die_symbol);
7417 inline bool
7418 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7419 const die_struct *entry2)
7421 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7424 inline void
7425 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7427 struct cu_hash_table_entry *next;
7429 while (entry)
7431 next = entry->next;
7432 free (entry);
7433 entry = next;
7437 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7439 /* Check whether we have already seen this CU and set up SYM_NUM
7440 accordingly. */
7441 static int
7442 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7444 struct cu_hash_table_entry dummy;
7445 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7447 dummy.max_comdat_num = 0;
7449 slot = htable->find_slot_with_hash (cu,
7450 htab_hash_string (cu->die_id.die_symbol),
7451 INSERT);
7452 entry = *slot;
7454 for (; entry; last = entry, entry = entry->next)
7456 if (same_die_p_wrap (cu, entry->cu))
7457 break;
7460 if (entry)
7462 *sym_num = entry->min_comdat_num;
7463 return 1;
7466 entry = XCNEW (struct cu_hash_table_entry);
7467 entry->cu = cu;
7468 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7469 entry->next = *slot;
7470 *slot = entry;
7472 return 0;
7475 /* Record SYM_NUM to record of CU in HTABLE. */
7476 static void
7477 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7478 unsigned int sym_num)
7480 struct cu_hash_table_entry **slot, *entry;
7482 slot = htable->find_slot_with_hash (cu,
7483 htab_hash_string (cu->die_id.die_symbol),
7484 NO_INSERT);
7485 entry = *slot;
7487 entry->max_comdat_num = sym_num;
7490 /* Traverse the DIE (which is always comp_unit_die), and set up
7491 additional compilation units for each of the include files we see
7492 bracketed by BINCL/EINCL. */
7494 static void
7495 break_out_includes (dw_die_ref die)
7497 dw_die_ref c;
7498 dw_die_ref unit = NULL;
7499 limbo_die_node *node, **pnode;
7501 c = die->die_child;
7502 if (c) do {
7503 dw_die_ref prev = c;
7504 c = c->die_sib;
7505 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7506 || (unit && is_comdat_die (c)))
7508 dw_die_ref next = c->die_sib;
7510 /* This DIE is for a secondary CU; remove it from the main one. */
7511 remove_child_with_prev (c, prev);
7513 if (c->die_tag == DW_TAG_GNU_BINCL)
7514 unit = push_new_compile_unit (unit, c);
7515 else if (c->die_tag == DW_TAG_GNU_EINCL)
7516 unit = pop_compile_unit (unit);
7517 else
7518 add_child_die (unit, c);
7519 c = next;
7520 if (c == die->die_child)
7521 break;
7523 } while (c != die->die_child);
7525 #if 0
7526 /* We can only use this in debugging, since the frontend doesn't check
7527 to make sure that we leave every include file we enter. */
7528 gcc_assert (!unit);
7529 #endif
7531 assign_symbol_names (die);
7532 cu_hash_type cu_hash_table (10);
7533 for (node = limbo_die_list, pnode = &limbo_die_list;
7534 node;
7535 node = node->next)
7537 int is_dupl;
7539 compute_section_prefix (node->die);
7540 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7541 &comdat_symbol_number);
7542 assign_symbol_names (node->die);
7543 if (is_dupl)
7544 *pnode = node->next;
7545 else
7547 pnode = &node->next;
7548 record_comdat_symbol_number (node->die, &cu_hash_table,
7549 comdat_symbol_number);
7554 /* Return non-zero if this DIE is a declaration. */
7556 static int
7557 is_declaration_die (dw_die_ref die)
7559 dw_attr_node *a;
7560 unsigned ix;
7562 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7563 if (a->dw_attr == DW_AT_declaration)
7564 return 1;
7566 return 0;
7569 /* Return non-zero if this DIE is nested inside a subprogram. */
7571 static int
7572 is_nested_in_subprogram (dw_die_ref die)
7574 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7576 if (decl == NULL)
7577 decl = die;
7578 return local_scope_p (decl);
7581 /* Return non-zero if this DIE contains a defining declaration of a
7582 subprogram. */
7584 static int
7585 contains_subprogram_definition (dw_die_ref die)
7587 dw_die_ref c;
7589 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7590 return 1;
7591 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7592 return 0;
7595 /* Return non-zero if this is a type DIE that should be moved to a
7596 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7597 unit type. */
7599 static int
7600 should_move_die_to_comdat (dw_die_ref die)
7602 switch (die->die_tag)
7604 case DW_TAG_class_type:
7605 case DW_TAG_structure_type:
7606 case DW_TAG_enumeration_type:
7607 case DW_TAG_union_type:
7608 /* Don't move declarations, inlined instances, types nested in a
7609 subprogram, or types that contain subprogram definitions. */
7610 if (is_declaration_die (die)
7611 || get_AT (die, DW_AT_abstract_origin)
7612 || is_nested_in_subprogram (die)
7613 || contains_subprogram_definition (die))
7614 return 0;
7615 return 1;
7616 case DW_TAG_array_type:
7617 case DW_TAG_interface_type:
7618 case DW_TAG_pointer_type:
7619 case DW_TAG_reference_type:
7620 case DW_TAG_rvalue_reference_type:
7621 case DW_TAG_string_type:
7622 case DW_TAG_subroutine_type:
7623 case DW_TAG_ptr_to_member_type:
7624 case DW_TAG_set_type:
7625 case DW_TAG_subrange_type:
7626 case DW_TAG_base_type:
7627 case DW_TAG_const_type:
7628 case DW_TAG_file_type:
7629 case DW_TAG_packed_type:
7630 case DW_TAG_volatile_type:
7631 case DW_TAG_typedef:
7632 default:
7633 return 0;
7637 /* Make a clone of DIE. */
7639 static dw_die_ref
7640 clone_die (dw_die_ref die)
7642 dw_die_ref clone;
7643 dw_attr_node *a;
7644 unsigned ix;
7646 clone = ggc_cleared_alloc<die_node> ();
7647 clone->die_tag = die->die_tag;
7649 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7650 add_dwarf_attr (clone, a);
7652 return clone;
7655 /* Make a clone of the tree rooted at DIE. */
7657 static dw_die_ref
7658 clone_tree (dw_die_ref die)
7660 dw_die_ref c;
7661 dw_die_ref clone = clone_die (die);
7663 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7665 return clone;
7668 /* Make a clone of DIE as a declaration. */
7670 static dw_die_ref
7671 clone_as_declaration (dw_die_ref die)
7673 dw_die_ref clone;
7674 dw_die_ref decl;
7675 dw_attr_node *a;
7676 unsigned ix;
7678 /* If the DIE is already a declaration, just clone it. */
7679 if (is_declaration_die (die))
7680 return clone_die (die);
7682 /* If the DIE is a specification, just clone its declaration DIE. */
7683 decl = get_AT_ref (die, DW_AT_specification);
7684 if (decl != NULL)
7686 clone = clone_die (decl);
7687 if (die->comdat_type_p)
7688 add_AT_die_ref (clone, DW_AT_signature, die);
7689 return clone;
7692 clone = ggc_cleared_alloc<die_node> ();
7693 clone->die_tag = die->die_tag;
7695 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7697 /* We don't want to copy over all attributes.
7698 For example we don't want DW_AT_byte_size because otherwise we will no
7699 longer have a declaration and GDB will treat it as a definition. */
7701 switch (a->dw_attr)
7703 case DW_AT_abstract_origin:
7704 case DW_AT_artificial:
7705 case DW_AT_containing_type:
7706 case DW_AT_external:
7707 case DW_AT_name:
7708 case DW_AT_type:
7709 case DW_AT_virtuality:
7710 case DW_AT_linkage_name:
7711 case DW_AT_MIPS_linkage_name:
7712 add_dwarf_attr (clone, a);
7713 break;
7714 case DW_AT_byte_size:
7715 case DW_AT_alignment:
7716 default:
7717 break;
7721 if (die->comdat_type_p)
7722 add_AT_die_ref (clone, DW_AT_signature, die);
7724 add_AT_flag (clone, DW_AT_declaration, 1);
7725 return clone;
7729 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7731 struct decl_table_entry
7733 dw_die_ref orig;
7734 dw_die_ref copy;
7737 /* Helpers to manipulate hash table of copied declarations. */
7739 /* Hashtable helpers. */
7741 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7743 typedef die_struct *compare_type;
7744 static inline hashval_t hash (const decl_table_entry *);
7745 static inline bool equal (const decl_table_entry *, const die_struct *);
7748 inline hashval_t
7749 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7751 return htab_hash_pointer (entry->orig);
7754 inline bool
7755 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7756 const die_struct *entry2)
7758 return entry1->orig == entry2;
7761 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7763 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7764 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7765 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7766 to check if the ancestor has already been copied into UNIT. */
7768 static dw_die_ref
7769 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7770 decl_hash_type *decl_table)
7772 dw_die_ref parent = die->die_parent;
7773 dw_die_ref new_parent = unit;
7774 dw_die_ref copy;
7775 decl_table_entry **slot = NULL;
7776 struct decl_table_entry *entry = NULL;
7778 if (decl_table)
7780 /* Check if the entry has already been copied to UNIT. */
7781 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7782 INSERT);
7783 if (*slot != HTAB_EMPTY_ENTRY)
7785 entry = *slot;
7786 return entry->copy;
7789 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7790 entry = XCNEW (struct decl_table_entry);
7791 entry->orig = die;
7792 entry->copy = NULL;
7793 *slot = entry;
7796 if (parent != NULL)
7798 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7799 if (spec != NULL)
7800 parent = spec;
7801 if (!is_unit_die (parent))
7802 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7805 copy = clone_as_declaration (die);
7806 add_child_die (new_parent, copy);
7808 if (decl_table)
7810 /* Record the pointer to the copy. */
7811 entry->copy = copy;
7814 return copy;
7816 /* Copy the declaration context to the new type unit DIE. This includes
7817 any surrounding namespace or type declarations. If the DIE has an
7818 AT_specification attribute, it also includes attributes and children
7819 attached to the specification, and returns a pointer to the original
7820 parent of the declaration DIE. Returns NULL otherwise. */
7822 static dw_die_ref
7823 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7825 dw_die_ref decl;
7826 dw_die_ref new_decl;
7827 dw_die_ref orig_parent = NULL;
7829 decl = get_AT_ref (die, DW_AT_specification);
7830 if (decl == NULL)
7831 decl = die;
7832 else
7834 unsigned ix;
7835 dw_die_ref c;
7836 dw_attr_node *a;
7838 /* The original DIE will be changed to a declaration, and must
7839 be moved to be a child of the original declaration DIE. */
7840 orig_parent = decl->die_parent;
7842 /* Copy the type node pointer from the new DIE to the original
7843 declaration DIE so we can forward references later. */
7844 decl->comdat_type_p = true;
7845 decl->die_id.die_type_node = die->die_id.die_type_node;
7847 remove_AT (die, DW_AT_specification);
7849 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7851 if (a->dw_attr != DW_AT_name
7852 && a->dw_attr != DW_AT_declaration
7853 && a->dw_attr != DW_AT_external)
7854 add_dwarf_attr (die, a);
7857 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7860 if (decl->die_parent != NULL
7861 && !is_unit_die (decl->die_parent))
7863 new_decl = copy_ancestor_tree (unit, decl, NULL);
7864 if (new_decl != NULL)
7866 remove_AT (new_decl, DW_AT_signature);
7867 add_AT_specification (die, new_decl);
7871 return orig_parent;
7874 /* Generate the skeleton ancestor tree for the given NODE, then clone
7875 the DIE and add the clone into the tree. */
7877 static void
7878 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7880 if (node->new_die != NULL)
7881 return;
7883 node->new_die = clone_as_declaration (node->old_die);
7885 if (node->parent != NULL)
7887 generate_skeleton_ancestor_tree (node->parent);
7888 add_child_die (node->parent->new_die, node->new_die);
7892 /* Generate a skeleton tree of DIEs containing any declarations that are
7893 found in the original tree. We traverse the tree looking for declaration
7894 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7896 static void
7897 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7899 skeleton_chain_node node;
7900 dw_die_ref c;
7901 dw_die_ref first;
7902 dw_die_ref prev = NULL;
7903 dw_die_ref next = NULL;
7905 node.parent = parent;
7907 first = c = parent->old_die->die_child;
7908 if (c)
7909 next = c->die_sib;
7910 if (c) do {
7911 if (prev == NULL || prev->die_sib == c)
7912 prev = c;
7913 c = next;
7914 next = (c == first ? NULL : c->die_sib);
7915 node.old_die = c;
7916 node.new_die = NULL;
7917 if (is_declaration_die (c))
7919 if (is_template_instantiation (c))
7921 /* Instantiated templates do not need to be cloned into the
7922 type unit. Just move the DIE and its children back to
7923 the skeleton tree (in the main CU). */
7924 remove_child_with_prev (c, prev);
7925 add_child_die (parent->new_die, c);
7926 c = prev;
7928 else if (c->comdat_type_p)
7930 /* This is the skeleton of earlier break_out_comdat_types
7931 type. Clone the existing DIE, but keep the children
7932 under the original (which is in the main CU). */
7933 dw_die_ref clone = clone_die (c);
7935 replace_child (c, clone, prev);
7936 generate_skeleton_ancestor_tree (parent);
7937 add_child_die (parent->new_die, c);
7938 c = clone;
7939 continue;
7941 else
7943 /* Clone the existing DIE, move the original to the skeleton
7944 tree (which is in the main CU), and put the clone, with
7945 all the original's children, where the original came from
7946 (which is about to be moved to the type unit). */
7947 dw_die_ref clone = clone_die (c);
7948 move_all_children (c, clone);
7950 /* If the original has a DW_AT_object_pointer attribute,
7951 it would now point to a child DIE just moved to the
7952 cloned tree, so we need to remove that attribute from
7953 the original. */
7954 remove_AT (c, DW_AT_object_pointer);
7956 replace_child (c, clone, prev);
7957 generate_skeleton_ancestor_tree (parent);
7958 add_child_die (parent->new_die, c);
7959 node.old_die = clone;
7960 node.new_die = c;
7961 c = clone;
7964 generate_skeleton_bottom_up (&node);
7965 } while (next != NULL);
7968 /* Wrapper function for generate_skeleton_bottom_up. */
7970 static dw_die_ref
7971 generate_skeleton (dw_die_ref die)
7973 skeleton_chain_node node;
7975 node.old_die = die;
7976 node.new_die = NULL;
7977 node.parent = NULL;
7979 /* If this type definition is nested inside another type,
7980 and is not an instantiation of a template, always leave
7981 at least a declaration in its place. */
7982 if (die->die_parent != NULL
7983 && is_type_die (die->die_parent)
7984 && !is_template_instantiation (die))
7985 node.new_die = clone_as_declaration (die);
7987 generate_skeleton_bottom_up (&node);
7988 return node.new_die;
7991 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7992 declaration. The original DIE is moved to a new compile unit so that
7993 existing references to it follow it to the new location. If any of the
7994 original DIE's descendants is a declaration, we need to replace the
7995 original DIE with a skeleton tree and move the declarations back into the
7996 skeleton tree. */
7998 static dw_die_ref
7999 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8000 dw_die_ref prev)
8002 dw_die_ref skeleton, orig_parent;
8004 /* Copy the declaration context to the type unit DIE. If the returned
8005 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8006 that DIE. */
8007 orig_parent = copy_declaration_context (unit, child);
8009 skeleton = generate_skeleton (child);
8010 if (skeleton == NULL)
8011 remove_child_with_prev (child, prev);
8012 else
8014 skeleton->comdat_type_p = true;
8015 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8017 /* If the original DIE was a specification, we need to put
8018 the skeleton under the parent DIE of the declaration.
8019 This leaves the original declaration in the tree, but
8020 it will be pruned later since there are no longer any
8021 references to it. */
8022 if (orig_parent != NULL)
8024 remove_child_with_prev (child, prev);
8025 add_child_die (orig_parent, skeleton);
8027 else
8028 replace_child (child, skeleton, prev);
8031 return skeleton;
8034 static void
8035 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8036 comdat_type_node *type_node,
8037 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8039 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8040 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8041 DWARF procedure references in the DW_AT_location attribute. */
8043 static dw_die_ref
8044 copy_dwarf_procedure (dw_die_ref die,
8045 comdat_type_node *type_node,
8046 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8048 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8050 /* DWARF procedures are not supposed to have children... */
8051 gcc_assert (die->die_child == NULL);
8053 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8054 gcc_assert (vec_safe_length (die->die_attr) == 1
8055 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8057 /* Do not copy more than once DWARF procedures. */
8058 bool existed;
8059 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8060 if (existed)
8061 return die_copy;
8063 die_copy = clone_die (die);
8064 add_child_die (type_node->root_die, die_copy);
8065 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8066 return die_copy;
8069 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8070 procedures in DIE's attributes. */
8072 static void
8073 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8074 comdat_type_node *type_node,
8075 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8077 dw_attr_node *a;
8078 unsigned i;
8080 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8082 dw_loc_descr_ref loc;
8084 if (a->dw_attr_val.val_class != dw_val_class_loc)
8085 continue;
8087 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8089 switch (loc->dw_loc_opc)
8091 case DW_OP_call2:
8092 case DW_OP_call4:
8093 case DW_OP_call_ref:
8094 gcc_assert (loc->dw_loc_oprnd1.val_class
8095 == dw_val_class_die_ref);
8096 loc->dw_loc_oprnd1.v.val_die_ref.die
8097 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8098 type_node,
8099 copied_dwarf_procs);
8101 default:
8102 break;
8108 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8109 rewrite references to point to the copies.
8111 References are looked for in DIE's attributes and recursively in all its
8112 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8113 mapping from old DWARF procedures to their copy. It is used not to copy
8114 twice the same DWARF procedure under TYPE_NODE. */
8116 static void
8117 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8118 comdat_type_node *type_node,
8119 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8121 dw_die_ref c;
8123 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8124 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8125 type_node,
8126 copied_dwarf_procs));
8129 /* Traverse the DIE and set up additional .debug_types or .debug_info
8130 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8131 section. */
8133 static void
8134 break_out_comdat_types (dw_die_ref die)
8136 dw_die_ref c;
8137 dw_die_ref first;
8138 dw_die_ref prev = NULL;
8139 dw_die_ref next = NULL;
8140 dw_die_ref unit = NULL;
8142 first = c = die->die_child;
8143 if (c)
8144 next = c->die_sib;
8145 if (c) do {
8146 if (prev == NULL || prev->die_sib == c)
8147 prev = c;
8148 c = next;
8149 next = (c == first ? NULL : c->die_sib);
8150 if (should_move_die_to_comdat (c))
8152 dw_die_ref replacement;
8153 comdat_type_node *type_node;
8155 /* Break out nested types into their own type units. */
8156 break_out_comdat_types (c);
8158 /* Create a new type unit DIE as the root for the new tree, and
8159 add it to the list of comdat types. */
8160 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8161 add_AT_unsigned (unit, DW_AT_language,
8162 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8163 type_node = ggc_cleared_alloc<comdat_type_node> ();
8164 type_node->root_die = unit;
8165 type_node->next = comdat_type_list;
8166 comdat_type_list = type_node;
8168 /* Generate the type signature. */
8169 generate_type_signature (c, type_node);
8171 /* Copy the declaration context, attributes, and children of the
8172 declaration into the new type unit DIE, then remove this DIE
8173 from the main CU (or replace it with a skeleton if necessary). */
8174 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8175 type_node->skeleton_die = replacement;
8177 /* Add the DIE to the new compunit. */
8178 add_child_die (unit, c);
8180 /* Types can reference DWARF procedures for type size or data location
8181 expressions. Calls in DWARF expressions cannot target procedures
8182 that are not in the same section. So we must copy DWARF procedures
8183 along with this type and then rewrite references to them. */
8184 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8185 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8187 if (replacement != NULL)
8188 c = replacement;
8190 else if (c->die_tag == DW_TAG_namespace
8191 || c->die_tag == DW_TAG_class_type
8192 || c->die_tag == DW_TAG_structure_type
8193 || c->die_tag == DW_TAG_union_type)
8195 /* Look for nested types that can be broken out. */
8196 break_out_comdat_types (c);
8198 } while (next != NULL);
8201 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8202 Enter all the cloned children into the hash table decl_table. */
8204 static dw_die_ref
8205 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8207 dw_die_ref c;
8208 dw_die_ref clone;
8209 struct decl_table_entry *entry;
8210 decl_table_entry **slot;
8212 if (die->die_tag == DW_TAG_subprogram)
8213 clone = clone_as_declaration (die);
8214 else
8215 clone = clone_die (die);
8217 slot = decl_table->find_slot_with_hash (die,
8218 htab_hash_pointer (die), INSERT);
8220 /* Assert that DIE isn't in the hash table yet. If it would be there
8221 before, the ancestors would be necessarily there as well, therefore
8222 clone_tree_partial wouldn't be called. */
8223 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8225 entry = XCNEW (struct decl_table_entry);
8226 entry->orig = die;
8227 entry->copy = clone;
8228 *slot = entry;
8230 if (die->die_tag != DW_TAG_subprogram)
8231 FOR_EACH_CHILD (die, c,
8232 add_child_die (clone, clone_tree_partial (c, decl_table)));
8234 return clone;
8237 /* Walk the DIE and its children, looking for references to incomplete
8238 or trivial types that are unmarked (i.e., that are not in the current
8239 type_unit). */
8241 static void
8242 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8244 dw_die_ref c;
8245 dw_attr_node *a;
8246 unsigned ix;
8248 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8250 if (AT_class (a) == dw_val_class_die_ref)
8252 dw_die_ref targ = AT_ref (a);
8253 decl_table_entry **slot;
8254 struct decl_table_entry *entry;
8256 if (targ->die_mark != 0 || targ->comdat_type_p)
8257 continue;
8259 slot = decl_table->find_slot_with_hash (targ,
8260 htab_hash_pointer (targ),
8261 INSERT);
8263 if (*slot != HTAB_EMPTY_ENTRY)
8265 /* TARG has already been copied, so we just need to
8266 modify the reference to point to the copy. */
8267 entry = *slot;
8268 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8270 else
8272 dw_die_ref parent = unit;
8273 dw_die_ref copy = clone_die (targ);
8275 /* Record in DECL_TABLE that TARG has been copied.
8276 Need to do this now, before the recursive call,
8277 because DECL_TABLE may be expanded and SLOT
8278 would no longer be a valid pointer. */
8279 entry = XCNEW (struct decl_table_entry);
8280 entry->orig = targ;
8281 entry->copy = copy;
8282 *slot = entry;
8284 /* If TARG is not a declaration DIE, we need to copy its
8285 children. */
8286 if (!is_declaration_die (targ))
8288 FOR_EACH_CHILD (
8289 targ, c,
8290 add_child_die (copy,
8291 clone_tree_partial (c, decl_table)));
8294 /* Make sure the cloned tree is marked as part of the
8295 type unit. */
8296 mark_dies (copy);
8298 /* If TARG has surrounding context, copy its ancestor tree
8299 into the new type unit. */
8300 if (targ->die_parent != NULL
8301 && !is_unit_die (targ->die_parent))
8302 parent = copy_ancestor_tree (unit, targ->die_parent,
8303 decl_table);
8305 add_child_die (parent, copy);
8306 a->dw_attr_val.v.val_die_ref.die = copy;
8308 /* Make sure the newly-copied DIE is walked. If it was
8309 installed in a previously-added context, it won't
8310 get visited otherwise. */
8311 if (parent != unit)
8313 /* Find the highest point of the newly-added tree,
8314 mark each node along the way, and walk from there. */
8315 parent->die_mark = 1;
8316 while (parent->die_parent
8317 && parent->die_parent->die_mark == 0)
8319 parent = parent->die_parent;
8320 parent->die_mark = 1;
8322 copy_decls_walk (unit, parent, decl_table);
8328 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8331 /* Copy declarations for "unworthy" types into the new comdat section.
8332 Incomplete types, modified types, and certain other types aren't broken
8333 out into comdat sections of their own, so they don't have a signature,
8334 and we need to copy the declaration into the same section so that we
8335 don't have an external reference. */
8337 static void
8338 copy_decls_for_unworthy_types (dw_die_ref unit)
8340 mark_dies (unit);
8341 decl_hash_type decl_table (10);
8342 copy_decls_walk (unit, unit, &decl_table);
8343 unmark_dies (unit);
8346 /* Traverse the DIE and add a sibling attribute if it may have the
8347 effect of speeding up access to siblings. To save some space,
8348 avoid generating sibling attributes for DIE's without children. */
8350 static void
8351 add_sibling_attributes (dw_die_ref die)
8353 dw_die_ref c;
8355 if (! die->die_child)
8356 return;
8358 if (die->die_parent && die != die->die_parent->die_child)
8359 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8361 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8364 /* Output all location lists for the DIE and its children. */
8366 static void
8367 output_location_lists (dw_die_ref die)
8369 dw_die_ref c;
8370 dw_attr_node *a;
8371 unsigned ix;
8373 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8374 if (AT_class (a) == dw_val_class_loc_list)
8375 output_loc_list (AT_loc_list (a));
8377 FOR_EACH_CHILD (die, c, output_location_lists (c));
8380 /* During assign_location_list_indexes and output_loclists_offset the
8381 current index, after it the number of assigned indexes (i.e. how
8382 large the .debug_loclists* offset table should be). */
8383 static unsigned int loc_list_idx;
8385 /* Output all location list offsets for the DIE and its children. */
8387 static void
8388 output_loclists_offsets (dw_die_ref die)
8390 dw_die_ref c;
8391 dw_attr_node *a;
8392 unsigned ix;
8394 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8395 if (AT_class (a) == dw_val_class_loc_list)
8397 dw_loc_list_ref l = AT_loc_list (a);
8398 if (l->offset_emitted)
8399 continue;
8400 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8401 loc_section_label, NULL);
8402 gcc_assert (l->hash == loc_list_idx);
8403 loc_list_idx++;
8404 l->offset_emitted = true;
8407 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8410 /* Recursively set indexes of location lists. */
8412 static void
8413 assign_location_list_indexes (dw_die_ref die)
8415 dw_die_ref c;
8416 dw_attr_node *a;
8417 unsigned ix;
8419 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8420 if (AT_class (a) == dw_val_class_loc_list)
8422 dw_loc_list_ref list = AT_loc_list (a);
8423 if (!list->num_assigned)
8425 list->num_assigned = true;
8426 list->hash = loc_list_idx++;
8430 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8433 /* We want to limit the number of external references, because they are
8434 larger than local references: a relocation takes multiple words, and
8435 even a sig8 reference is always eight bytes, whereas a local reference
8436 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8437 So if we encounter multiple external references to the same type DIE, we
8438 make a local typedef stub for it and redirect all references there.
8440 This is the element of the hash table for keeping track of these
8441 references. */
8443 struct external_ref
8445 dw_die_ref type;
8446 dw_die_ref stub;
8447 unsigned n_refs;
8450 /* Hashtable helpers. */
8452 struct external_ref_hasher : free_ptr_hash <external_ref>
8454 static inline hashval_t hash (const external_ref *);
8455 static inline bool equal (const external_ref *, const external_ref *);
8458 inline hashval_t
8459 external_ref_hasher::hash (const external_ref *r)
8461 dw_die_ref die = r->type;
8462 hashval_t h = 0;
8464 /* We can't use the address of the DIE for hashing, because
8465 that will make the order of the stub DIEs non-deterministic. */
8466 if (! die->comdat_type_p)
8467 /* We have a symbol; use it to compute a hash. */
8468 h = htab_hash_string (die->die_id.die_symbol);
8469 else
8471 /* We have a type signature; use a subset of the bits as the hash.
8472 The 8-byte signature is at least as large as hashval_t. */
8473 comdat_type_node *type_node = die->die_id.die_type_node;
8474 memcpy (&h, type_node->signature, sizeof (h));
8476 return h;
8479 inline bool
8480 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8482 return r1->type == r2->type;
8485 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8487 /* Return a pointer to the external_ref for references to DIE. */
8489 static struct external_ref *
8490 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8492 struct external_ref ref, *ref_p;
8493 external_ref **slot;
8495 ref.type = die;
8496 slot = map->find_slot (&ref, INSERT);
8497 if (*slot != HTAB_EMPTY_ENTRY)
8498 return *slot;
8500 ref_p = XCNEW (struct external_ref);
8501 ref_p->type = die;
8502 *slot = ref_p;
8503 return ref_p;
8506 /* Subroutine of optimize_external_refs, below.
8508 If we see a type skeleton, record it as our stub. If we see external
8509 references, remember how many we've seen. */
8511 static void
8512 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8514 dw_die_ref c;
8515 dw_attr_node *a;
8516 unsigned ix;
8517 struct external_ref *ref_p;
8519 if (is_type_die (die)
8520 && (c = get_AT_ref (die, DW_AT_signature)))
8522 /* This is a local skeleton; use it for local references. */
8523 ref_p = lookup_external_ref (map, c);
8524 ref_p->stub = die;
8527 /* Scan the DIE references, and remember any that refer to DIEs from
8528 other CUs (i.e. those which are not marked). */
8529 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8530 if (AT_class (a) == dw_val_class_die_ref
8531 && (c = AT_ref (a))->die_mark == 0
8532 && is_type_die (c))
8534 ref_p = lookup_external_ref (map, c);
8535 ref_p->n_refs++;
8538 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8541 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8542 points to an external_ref, DATA is the CU we're processing. If we don't
8543 already have a local stub, and we have multiple refs, build a stub. */
8546 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8548 struct external_ref *ref_p = *slot;
8550 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8552 /* We have multiple references to this type, so build a small stub.
8553 Both of these forms are a bit dodgy from the perspective of the
8554 DWARF standard, since technically they should have names. */
8555 dw_die_ref cu = data;
8556 dw_die_ref type = ref_p->type;
8557 dw_die_ref stub = NULL;
8559 if (type->comdat_type_p)
8561 /* If we refer to this type via sig8, use AT_signature. */
8562 stub = new_die (type->die_tag, cu, NULL_TREE);
8563 add_AT_die_ref (stub, DW_AT_signature, type);
8565 else
8567 /* Otherwise, use a typedef with no name. */
8568 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8569 add_AT_die_ref (stub, DW_AT_type, type);
8572 stub->die_mark++;
8573 ref_p->stub = stub;
8575 return 1;
8578 /* DIE is a unit; look through all the DIE references to see if there are
8579 any external references to types, and if so, create local stubs for
8580 them which will be applied in build_abbrev_table. This is useful because
8581 references to local DIEs are smaller. */
8583 static external_ref_hash_type *
8584 optimize_external_refs (dw_die_ref die)
8586 external_ref_hash_type *map = new external_ref_hash_type (10);
8587 optimize_external_refs_1 (die, map);
8588 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8589 return map;
8592 /* The following 3 variables are temporaries that are computed only during the
8593 build_abbrev_table call and used and released during the following
8594 optimize_abbrev_table call. */
8596 /* First abbrev_id that can be optimized based on usage. */
8597 static unsigned int abbrev_opt_start;
8599 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8600 abbrev_id smaller than this, because they must be already sized
8601 during build_abbrev_table). */
8602 static unsigned int abbrev_opt_base_type_end;
8604 /* Vector of usage counts during build_abbrev_table. Indexed by
8605 abbrev_id - abbrev_opt_start. */
8606 static vec<unsigned int> abbrev_usage_count;
8608 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8609 static vec<dw_die_ref> sorted_abbrev_dies;
8611 /* The format of each DIE (and its attribute value pairs) is encoded in an
8612 abbreviation table. This routine builds the abbreviation table and assigns
8613 a unique abbreviation id for each abbreviation entry. The children of each
8614 die are visited recursively. */
8616 static void
8617 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8619 unsigned int abbrev_id = 0;
8620 dw_die_ref c;
8621 dw_attr_node *a;
8622 unsigned ix;
8623 dw_die_ref abbrev;
8625 /* Scan the DIE references, and replace any that refer to
8626 DIEs from other CUs (i.e. those which are not marked) with
8627 the local stubs we built in optimize_external_refs. */
8628 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8629 if (AT_class (a) == dw_val_class_die_ref
8630 && (c = AT_ref (a))->die_mark == 0)
8632 struct external_ref *ref_p;
8633 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8635 ref_p = lookup_external_ref (extern_map, c);
8636 if (ref_p->stub && ref_p->stub != die)
8637 change_AT_die_ref (a, ref_p->stub);
8638 else
8639 /* We aren't changing this reference, so mark it external. */
8640 set_AT_ref_external (a, 1);
8643 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8645 dw_attr_node *die_a, *abbrev_a;
8646 unsigned ix;
8647 bool ok = true;
8649 if (abbrev_id == 0)
8650 continue;
8651 if (abbrev->die_tag != die->die_tag)
8652 continue;
8653 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8654 continue;
8656 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8657 continue;
8659 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8661 abbrev_a = &(*abbrev->die_attr)[ix];
8662 if ((abbrev_a->dw_attr != die_a->dw_attr)
8663 || (value_format (abbrev_a) != value_format (die_a)))
8665 ok = false;
8666 break;
8669 if (ok)
8670 break;
8673 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8675 vec_safe_push (abbrev_die_table, die);
8676 if (abbrev_opt_start)
8677 abbrev_usage_count.safe_push (0);
8679 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8681 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8682 sorted_abbrev_dies.safe_push (die);
8685 die->die_abbrev = abbrev_id;
8686 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8689 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8690 by die_abbrev's usage count, from the most commonly used
8691 abbreviation to the least. */
8693 static int
8694 die_abbrev_cmp (const void *p1, const void *p2)
8696 dw_die_ref die1 = *(const dw_die_ref *) p1;
8697 dw_die_ref die2 = *(const dw_die_ref *) p2;
8699 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8700 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8702 if (die1->die_abbrev >= abbrev_opt_base_type_end
8703 && die2->die_abbrev >= abbrev_opt_base_type_end)
8705 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8706 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8707 return -1;
8708 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8709 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8710 return 1;
8713 /* Stabilize the sort. */
8714 if (die1->die_abbrev < die2->die_abbrev)
8715 return -1;
8716 if (die1->die_abbrev > die2->die_abbrev)
8717 return 1;
8719 return 0;
8722 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8723 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8724 into dw_val_class_const_implicit or
8725 dw_val_class_unsigned_const_implicit. */
8727 static void
8728 optimize_implicit_const (unsigned int first_id, unsigned int end,
8729 vec<bool> &implicit_consts)
8731 /* It never makes sense if there is just one DIE using the abbreviation. */
8732 if (end < first_id + 2)
8733 return;
8735 dw_attr_node *a;
8736 unsigned ix, i;
8737 dw_die_ref die = sorted_abbrev_dies[first_id];
8738 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8739 if (implicit_consts[ix])
8741 enum dw_val_class new_class = dw_val_class_none;
8742 switch (AT_class (a))
8744 case dw_val_class_unsigned_const:
8745 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8746 continue;
8748 /* The .debug_abbrev section will grow by
8749 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8750 in all the DIEs using that abbreviation. */
8751 if (constant_size (AT_unsigned (a)) * (end - first_id)
8752 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8753 continue;
8755 new_class = dw_val_class_unsigned_const_implicit;
8756 break;
8758 case dw_val_class_const:
8759 new_class = dw_val_class_const_implicit;
8760 break;
8762 case dw_val_class_file:
8763 new_class = dw_val_class_file_implicit;
8764 break;
8766 default:
8767 continue;
8769 for (i = first_id; i < end; i++)
8770 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8771 = new_class;
8775 /* Attempt to optimize abbreviation table from abbrev_opt_start
8776 abbreviation above. */
8778 static void
8779 optimize_abbrev_table (void)
8781 if (abbrev_opt_start
8782 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8783 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8785 auto_vec<bool, 32> implicit_consts;
8786 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8788 unsigned int abbrev_id = abbrev_opt_start - 1;
8789 unsigned int first_id = ~0U;
8790 unsigned int last_abbrev_id = 0;
8791 unsigned int i;
8792 dw_die_ref die;
8793 if (abbrev_opt_base_type_end > abbrev_opt_start)
8794 abbrev_id = abbrev_opt_base_type_end - 1;
8795 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8796 most commonly used abbreviations come first. */
8797 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8799 dw_attr_node *a;
8800 unsigned ix;
8802 /* If calc_base_type_die_sizes has been called, the CU and
8803 base types after it can't be optimized, because we've already
8804 calculated their DIE offsets. We've sorted them first. */
8805 if (die->die_abbrev < abbrev_opt_base_type_end)
8806 continue;
8807 if (die->die_abbrev != last_abbrev_id)
8809 last_abbrev_id = die->die_abbrev;
8810 if (dwarf_version >= 5 && first_id != ~0U)
8811 optimize_implicit_const (first_id, i, implicit_consts);
8812 abbrev_id++;
8813 (*abbrev_die_table)[abbrev_id] = die;
8814 if (dwarf_version >= 5)
8816 first_id = i;
8817 implicit_consts.truncate (0);
8819 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8820 switch (AT_class (a))
8822 case dw_val_class_const:
8823 case dw_val_class_unsigned_const:
8824 case dw_val_class_file:
8825 implicit_consts.safe_push (true);
8826 break;
8827 default:
8828 implicit_consts.safe_push (false);
8829 break;
8833 else if (dwarf_version >= 5)
8835 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8836 if (!implicit_consts[ix])
8837 continue;
8838 else
8840 dw_attr_node *other_a
8841 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8842 if (!dw_val_equal_p (&a->dw_attr_val,
8843 &other_a->dw_attr_val))
8844 implicit_consts[ix] = false;
8847 die->die_abbrev = abbrev_id;
8849 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8850 if (dwarf_version >= 5 && first_id != ~0U)
8851 optimize_implicit_const (first_id, i, implicit_consts);
8854 abbrev_opt_start = 0;
8855 abbrev_opt_base_type_end = 0;
8856 abbrev_usage_count.release ();
8857 sorted_abbrev_dies.release ();
8860 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8862 static int
8863 constant_size (unsigned HOST_WIDE_INT value)
8865 int log;
8867 if (value == 0)
8868 log = 0;
8869 else
8870 log = floor_log2 (value);
8872 log = log / 8;
8873 log = 1 << (floor_log2 (log) + 1);
8875 return log;
8878 /* Return the size of a DIE as it is represented in the
8879 .debug_info section. */
8881 static unsigned long
8882 size_of_die (dw_die_ref die)
8884 unsigned long size = 0;
8885 dw_attr_node *a;
8886 unsigned ix;
8887 enum dwarf_form form;
8889 size += size_of_uleb128 (die->die_abbrev);
8890 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8892 switch (AT_class (a))
8894 case dw_val_class_addr:
8895 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8897 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8898 size += size_of_uleb128 (AT_index (a));
8900 else
8901 size += DWARF2_ADDR_SIZE;
8902 break;
8903 case dw_val_class_offset:
8904 size += DWARF_OFFSET_SIZE;
8905 break;
8906 case dw_val_class_loc:
8908 unsigned long lsize = size_of_locs (AT_loc (a));
8910 /* Block length. */
8911 if (dwarf_version >= 4)
8912 size += size_of_uleb128 (lsize);
8913 else
8914 size += constant_size (lsize);
8915 size += lsize;
8917 break;
8918 case dw_val_class_loc_list:
8919 if (dwarf_split_debug_info && dwarf_version >= 5)
8921 gcc_assert (AT_loc_list (a)->num_assigned);
8922 size += size_of_uleb128 (AT_loc_list (a)->hash);
8924 else
8925 size += DWARF_OFFSET_SIZE;
8926 break;
8927 case dw_val_class_range_list:
8928 if (value_format (a) == DW_FORM_rnglistx)
8930 gcc_assert (rnglist_idx);
8931 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8932 size += size_of_uleb128 (r->idx);
8934 else
8935 size += DWARF_OFFSET_SIZE;
8936 break;
8937 case dw_val_class_const:
8938 size += size_of_sleb128 (AT_int (a));
8939 break;
8940 case dw_val_class_unsigned_const:
8942 int csize = constant_size (AT_unsigned (a));
8943 if (dwarf_version == 3
8944 && a->dw_attr == DW_AT_data_member_location
8945 && csize >= 4)
8946 size += size_of_uleb128 (AT_unsigned (a));
8947 else
8948 size += csize;
8950 break;
8951 case dw_val_class_const_implicit:
8952 case dw_val_class_unsigned_const_implicit:
8953 case dw_val_class_file_implicit:
8954 /* These occupy no size in the DIE, just an extra sleb128 in
8955 .debug_abbrev. */
8956 break;
8957 case dw_val_class_const_double:
8958 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8959 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8960 size++; /* block */
8961 break;
8962 case dw_val_class_wide_int:
8963 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8964 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8965 if (get_full_len (*a->dw_attr_val.v.val_wide)
8966 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8967 size++; /* block */
8968 break;
8969 case dw_val_class_vec:
8970 size += constant_size (a->dw_attr_val.v.val_vec.length
8971 * a->dw_attr_val.v.val_vec.elt_size)
8972 + a->dw_attr_val.v.val_vec.length
8973 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8974 break;
8975 case dw_val_class_flag:
8976 if (dwarf_version >= 4)
8977 /* Currently all add_AT_flag calls pass in 1 as last argument,
8978 so DW_FORM_flag_present can be used. If that ever changes,
8979 we'll need to use DW_FORM_flag and have some optimization
8980 in build_abbrev_table that will change those to
8981 DW_FORM_flag_present if it is set to 1 in all DIEs using
8982 the same abbrev entry. */
8983 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8984 else
8985 size += 1;
8986 break;
8987 case dw_val_class_die_ref:
8988 if (AT_ref_external (a))
8990 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8991 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8992 is sized by target address length, whereas in DWARF3
8993 it's always sized as an offset. */
8994 if (use_debug_types)
8995 size += DWARF_TYPE_SIGNATURE_SIZE;
8996 else if (dwarf_version == 2)
8997 size += DWARF2_ADDR_SIZE;
8998 else
8999 size += DWARF_OFFSET_SIZE;
9001 else
9002 size += DWARF_OFFSET_SIZE;
9003 break;
9004 case dw_val_class_fde_ref:
9005 size += DWARF_OFFSET_SIZE;
9006 break;
9007 case dw_val_class_lbl_id:
9008 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9010 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9011 size += size_of_uleb128 (AT_index (a));
9013 else
9014 size += DWARF2_ADDR_SIZE;
9015 break;
9016 case dw_val_class_lineptr:
9017 case dw_val_class_macptr:
9018 case dw_val_class_loclistsptr:
9019 size += DWARF_OFFSET_SIZE;
9020 break;
9021 case dw_val_class_str:
9022 form = AT_string_form (a);
9023 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9024 size += DWARF_OFFSET_SIZE;
9025 else if (form == DW_FORM_GNU_str_index)
9026 size += size_of_uleb128 (AT_index (a));
9027 else
9028 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9029 break;
9030 case dw_val_class_file:
9031 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9032 break;
9033 case dw_val_class_data8:
9034 size += 8;
9035 break;
9036 case dw_val_class_vms_delta:
9037 size += DWARF_OFFSET_SIZE;
9038 break;
9039 case dw_val_class_high_pc:
9040 size += DWARF2_ADDR_SIZE;
9041 break;
9042 case dw_val_class_discr_value:
9043 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9044 break;
9045 case dw_val_class_discr_list:
9047 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9049 /* This is a block, so we have the block length and then its
9050 data. */
9051 size += constant_size (block_size) + block_size;
9053 break;
9054 default:
9055 gcc_unreachable ();
9059 return size;
9062 /* Size the debugging information associated with a given DIE. Visits the
9063 DIE's children recursively. Updates the global variable next_die_offset, on
9064 each time through. Uses the current value of next_die_offset to update the
9065 die_offset field in each DIE. */
9067 static void
9068 calc_die_sizes (dw_die_ref die)
9070 dw_die_ref c;
9072 gcc_assert (die->die_offset == 0
9073 || (unsigned long int) die->die_offset == next_die_offset);
9074 die->die_offset = next_die_offset;
9075 next_die_offset += size_of_die (die);
9077 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9079 if (die->die_child != NULL)
9080 /* Count the null byte used to terminate sibling lists. */
9081 next_die_offset += 1;
9084 /* Size just the base type children at the start of the CU.
9085 This is needed because build_abbrev needs to size locs
9086 and sizing of type based stack ops needs to know die_offset
9087 values for the base types. */
9089 static void
9090 calc_base_type_die_sizes (void)
9092 unsigned long die_offset = (dwarf_split_debug_info
9093 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9094 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9095 unsigned int i;
9096 dw_die_ref base_type;
9097 #if ENABLE_ASSERT_CHECKING
9098 dw_die_ref prev = comp_unit_die ()->die_child;
9099 #endif
9101 die_offset += size_of_die (comp_unit_die ());
9102 for (i = 0; base_types.iterate (i, &base_type); i++)
9104 #if ENABLE_ASSERT_CHECKING
9105 gcc_assert (base_type->die_offset == 0
9106 && prev->die_sib == base_type
9107 && base_type->die_child == NULL
9108 && base_type->die_abbrev);
9109 prev = base_type;
9110 #endif
9111 if (abbrev_opt_start
9112 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9113 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9114 base_type->die_offset = die_offset;
9115 die_offset += size_of_die (base_type);
9119 /* Set the marks for a die and its children. We do this so
9120 that we know whether or not a reference needs to use FORM_ref_addr; only
9121 DIEs in the same CU will be marked. We used to clear out the offset
9122 and use that as the flag, but ran into ordering problems. */
9124 static void
9125 mark_dies (dw_die_ref die)
9127 dw_die_ref c;
9129 gcc_assert (!die->die_mark);
9131 die->die_mark = 1;
9132 FOR_EACH_CHILD (die, c, mark_dies (c));
9135 /* Clear the marks for a die and its children. */
9137 static void
9138 unmark_dies (dw_die_ref die)
9140 dw_die_ref c;
9142 if (! use_debug_types)
9143 gcc_assert (die->die_mark);
9145 die->die_mark = 0;
9146 FOR_EACH_CHILD (die, c, unmark_dies (c));
9149 /* Clear the marks for a die, its children and referred dies. */
9151 static void
9152 unmark_all_dies (dw_die_ref die)
9154 dw_die_ref c;
9155 dw_attr_node *a;
9156 unsigned ix;
9158 if (!die->die_mark)
9159 return;
9160 die->die_mark = 0;
9162 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9164 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9165 if (AT_class (a) == dw_val_class_die_ref)
9166 unmark_all_dies (AT_ref (a));
9169 /* Calculate if the entry should appear in the final output file. It may be
9170 from a pruned a type. */
9172 static bool
9173 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9175 /* By limiting gnu pubnames to definitions only, gold can generate a
9176 gdb index without entries for declarations, which don't include
9177 enough information to be useful. */
9178 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9179 return false;
9181 if (table == pubname_table)
9183 /* Enumerator names are part of the pubname table, but the
9184 parent DW_TAG_enumeration_type die may have been pruned.
9185 Don't output them if that is the case. */
9186 if (p->die->die_tag == DW_TAG_enumerator &&
9187 (p->die->die_parent == NULL
9188 || !p->die->die_parent->die_perennial_p))
9189 return false;
9191 /* Everything else in the pubname table is included. */
9192 return true;
9195 /* The pubtypes table shouldn't include types that have been
9196 pruned. */
9197 return (p->die->die_offset != 0
9198 || !flag_eliminate_unused_debug_types);
9201 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9202 generated for the compilation unit. */
9204 static unsigned long
9205 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9207 unsigned long size;
9208 unsigned i;
9209 pubname_entry *p;
9210 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9212 size = DWARF_PUBNAMES_HEADER_SIZE;
9213 FOR_EACH_VEC_ELT (*names, i, p)
9214 if (include_pubname_in_output (names, p))
9215 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9217 size += DWARF_OFFSET_SIZE;
9218 return size;
9221 /* Return the size of the information in the .debug_aranges section. */
9223 static unsigned long
9224 size_of_aranges (void)
9226 unsigned long size;
9228 size = DWARF_ARANGES_HEADER_SIZE;
9230 /* Count the address/length pair for this compilation unit. */
9231 if (text_section_used)
9232 size += 2 * DWARF2_ADDR_SIZE;
9233 if (cold_text_section_used)
9234 size += 2 * DWARF2_ADDR_SIZE;
9235 if (have_multiple_function_sections)
9237 unsigned fde_idx;
9238 dw_fde_ref fde;
9240 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9242 if (DECL_IGNORED_P (fde->decl))
9243 continue;
9244 if (!fde->in_std_section)
9245 size += 2 * DWARF2_ADDR_SIZE;
9246 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9247 size += 2 * DWARF2_ADDR_SIZE;
9251 /* Count the two zero words used to terminated the address range table. */
9252 size += 2 * DWARF2_ADDR_SIZE;
9253 return size;
9256 /* Select the encoding of an attribute value. */
9258 static enum dwarf_form
9259 value_format (dw_attr_node *a)
9261 switch (AT_class (a))
9263 case dw_val_class_addr:
9264 /* Only very few attributes allow DW_FORM_addr. */
9265 switch (a->dw_attr)
9267 case DW_AT_low_pc:
9268 case DW_AT_high_pc:
9269 case DW_AT_entry_pc:
9270 case DW_AT_trampoline:
9271 return (AT_index (a) == NOT_INDEXED
9272 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9273 default:
9274 break;
9276 switch (DWARF2_ADDR_SIZE)
9278 case 1:
9279 return DW_FORM_data1;
9280 case 2:
9281 return DW_FORM_data2;
9282 case 4:
9283 return DW_FORM_data4;
9284 case 8:
9285 return DW_FORM_data8;
9286 default:
9287 gcc_unreachable ();
9289 case dw_val_class_loc_list:
9290 if (dwarf_split_debug_info
9291 && dwarf_version >= 5
9292 && AT_loc_list (a)->num_assigned)
9293 return DW_FORM_loclistx;
9294 /* FALLTHRU */
9295 case dw_val_class_range_list:
9296 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9297 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9298 care about sizes of .debug* sections in shared libraries and
9299 executables and don't take into account relocations that affect just
9300 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9301 table in the .debug_rnglists section. */
9302 if (dwarf_split_debug_info
9303 && dwarf_version >= 5
9304 && AT_class (a) == dw_val_class_range_list
9305 && rnglist_idx
9306 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9307 return DW_FORM_rnglistx;
9308 if (dwarf_version >= 4)
9309 return DW_FORM_sec_offset;
9310 /* FALLTHRU */
9311 case dw_val_class_vms_delta:
9312 case dw_val_class_offset:
9313 switch (DWARF_OFFSET_SIZE)
9315 case 4:
9316 return DW_FORM_data4;
9317 case 8:
9318 return DW_FORM_data8;
9319 default:
9320 gcc_unreachable ();
9322 case dw_val_class_loc:
9323 if (dwarf_version >= 4)
9324 return DW_FORM_exprloc;
9325 switch (constant_size (size_of_locs (AT_loc (a))))
9327 case 1:
9328 return DW_FORM_block1;
9329 case 2:
9330 return DW_FORM_block2;
9331 case 4:
9332 return DW_FORM_block4;
9333 default:
9334 gcc_unreachable ();
9336 case dw_val_class_const:
9337 return DW_FORM_sdata;
9338 case dw_val_class_unsigned_const:
9339 switch (constant_size (AT_unsigned (a)))
9341 case 1:
9342 return DW_FORM_data1;
9343 case 2:
9344 return DW_FORM_data2;
9345 case 4:
9346 /* In DWARF3 DW_AT_data_member_location with
9347 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9348 constant, so we need to use DW_FORM_udata if we need
9349 a large constant. */
9350 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9351 return DW_FORM_udata;
9352 return DW_FORM_data4;
9353 case 8:
9354 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9355 return DW_FORM_udata;
9356 return DW_FORM_data8;
9357 default:
9358 gcc_unreachable ();
9360 case dw_val_class_const_implicit:
9361 case dw_val_class_unsigned_const_implicit:
9362 case dw_val_class_file_implicit:
9363 return DW_FORM_implicit_const;
9364 case dw_val_class_const_double:
9365 switch (HOST_BITS_PER_WIDE_INT)
9367 case 8:
9368 return DW_FORM_data2;
9369 case 16:
9370 return DW_FORM_data4;
9371 case 32:
9372 return DW_FORM_data8;
9373 case 64:
9374 if (dwarf_version >= 5)
9375 return DW_FORM_data16;
9376 /* FALLTHRU */
9377 default:
9378 return DW_FORM_block1;
9380 case dw_val_class_wide_int:
9381 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9383 case 8:
9384 return DW_FORM_data1;
9385 case 16:
9386 return DW_FORM_data2;
9387 case 32:
9388 return DW_FORM_data4;
9389 case 64:
9390 return DW_FORM_data8;
9391 case 128:
9392 if (dwarf_version >= 5)
9393 return DW_FORM_data16;
9394 /* FALLTHRU */
9395 default:
9396 return DW_FORM_block1;
9398 case dw_val_class_vec:
9399 switch (constant_size (a->dw_attr_val.v.val_vec.length
9400 * a->dw_attr_val.v.val_vec.elt_size))
9402 case 1:
9403 return DW_FORM_block1;
9404 case 2:
9405 return DW_FORM_block2;
9406 case 4:
9407 return DW_FORM_block4;
9408 default:
9409 gcc_unreachable ();
9411 case dw_val_class_flag:
9412 if (dwarf_version >= 4)
9414 /* Currently all add_AT_flag calls pass in 1 as last argument,
9415 so DW_FORM_flag_present can be used. If that ever changes,
9416 we'll need to use DW_FORM_flag and have some optimization
9417 in build_abbrev_table that will change those to
9418 DW_FORM_flag_present if it is set to 1 in all DIEs using
9419 the same abbrev entry. */
9420 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9421 return DW_FORM_flag_present;
9423 return DW_FORM_flag;
9424 case dw_val_class_die_ref:
9425 if (AT_ref_external (a))
9426 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9427 else
9428 return DW_FORM_ref;
9429 case dw_val_class_fde_ref:
9430 return DW_FORM_data;
9431 case dw_val_class_lbl_id:
9432 return (AT_index (a) == NOT_INDEXED
9433 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9434 case dw_val_class_lineptr:
9435 case dw_val_class_macptr:
9436 case dw_val_class_loclistsptr:
9437 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9438 case dw_val_class_str:
9439 return AT_string_form (a);
9440 case dw_val_class_file:
9441 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9443 case 1:
9444 return DW_FORM_data1;
9445 case 2:
9446 return DW_FORM_data2;
9447 case 4:
9448 return DW_FORM_data4;
9449 default:
9450 gcc_unreachable ();
9453 case dw_val_class_data8:
9454 return DW_FORM_data8;
9456 case dw_val_class_high_pc:
9457 switch (DWARF2_ADDR_SIZE)
9459 case 1:
9460 return DW_FORM_data1;
9461 case 2:
9462 return DW_FORM_data2;
9463 case 4:
9464 return DW_FORM_data4;
9465 case 8:
9466 return DW_FORM_data8;
9467 default:
9468 gcc_unreachable ();
9471 case dw_val_class_discr_value:
9472 return (a->dw_attr_val.v.val_discr_value.pos
9473 ? DW_FORM_udata
9474 : DW_FORM_sdata);
9475 case dw_val_class_discr_list:
9476 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9478 case 1:
9479 return DW_FORM_block1;
9480 case 2:
9481 return DW_FORM_block2;
9482 case 4:
9483 return DW_FORM_block4;
9484 default:
9485 gcc_unreachable ();
9488 default:
9489 gcc_unreachable ();
9493 /* Output the encoding of an attribute value. */
9495 static void
9496 output_value_format (dw_attr_node *a)
9498 enum dwarf_form form = value_format (a);
9500 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9503 /* Given a die and id, produce the appropriate abbreviations. */
9505 static void
9506 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9508 unsigned ix;
9509 dw_attr_node *a_attr;
9511 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9512 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9513 dwarf_tag_name (abbrev->die_tag));
9515 if (abbrev->die_child != NULL)
9516 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9517 else
9518 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9520 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9522 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9523 dwarf_attr_name (a_attr->dw_attr));
9524 output_value_format (a_attr);
9525 if (value_format (a_attr) == DW_FORM_implicit_const)
9527 if (AT_class (a_attr) == dw_val_class_file_implicit)
9529 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9530 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9531 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9533 else
9534 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9538 dw2_asm_output_data (1, 0, NULL);
9539 dw2_asm_output_data (1, 0, NULL);
9543 /* Output the .debug_abbrev section which defines the DIE abbreviation
9544 table. */
9546 static void
9547 output_abbrev_section (void)
9549 unsigned int abbrev_id;
9550 dw_die_ref abbrev;
9552 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9553 if (abbrev_id != 0)
9554 output_die_abbrevs (abbrev_id, abbrev);
9556 /* Terminate the table. */
9557 dw2_asm_output_data (1, 0, NULL);
9560 /* Output a symbol we can use to refer to this DIE from another CU. */
9562 static inline void
9563 output_die_symbol (dw_die_ref die)
9565 const char *sym = die->die_id.die_symbol;
9567 gcc_assert (!die->comdat_type_p);
9569 if (sym == 0)
9570 return;
9572 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9573 /* We make these global, not weak; if the target doesn't support
9574 .linkonce, it doesn't support combining the sections, so debugging
9575 will break. */
9576 targetm.asm_out.globalize_label (asm_out_file, sym);
9578 ASM_OUTPUT_LABEL (asm_out_file, sym);
9581 /* Return a new location list, given the begin and end range, and the
9582 expression. */
9584 static inline dw_loc_list_ref
9585 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9586 const char *section)
9588 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9590 retlist->begin = begin;
9591 retlist->begin_entry = NULL;
9592 retlist->end = end;
9593 retlist->expr = expr;
9594 retlist->section = section;
9596 return retlist;
9599 /* Generate a new internal symbol for this location list node, if it
9600 hasn't got one yet. */
9602 static inline void
9603 gen_llsym (dw_loc_list_ref list)
9605 gcc_assert (!list->ll_symbol);
9606 list->ll_symbol = gen_internal_sym ("LLST");
9609 /* Output the location list given to us. */
9611 static void
9612 output_loc_list (dw_loc_list_ref list_head)
9614 if (list_head->emitted)
9615 return;
9616 list_head->emitted = true;
9618 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9620 dw_loc_list_ref curr = list_head;
9621 const char *last_section = NULL;
9622 const char *base_label = NULL;
9624 /* Walk the location list, and output each range + expression. */
9625 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9627 unsigned long size;
9628 /* Don't output an entry that starts and ends at the same address. */
9629 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9630 continue;
9631 size = size_of_locs (curr->expr);
9632 /* If the expression is too large, drop it on the floor. We could
9633 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9634 in the expression, but >= 64KB expressions for a single value
9635 in a single range are unlikely very useful. */
9636 if (dwarf_version < 5 && size > 0xffff)
9637 continue;
9638 if (dwarf_version >= 5)
9640 if (dwarf_split_debug_info)
9642 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9643 uleb128 index into .debug_addr and uleb128 length. */
9644 dw2_asm_output_data (1, DW_LLE_startx_length,
9645 "DW_LLE_startx_length (%s)",
9646 list_head->ll_symbol);
9647 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9648 "Location list range start index "
9649 "(%s)", curr->begin);
9650 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9651 For that case we probably need to emit DW_LLE_startx_endx,
9652 but we'd need 2 .debug_addr entries rather than just one. */
9653 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9654 "Location list length (%s)",
9655 list_head->ll_symbol);
9657 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9659 /* If all code is in .text section, the base address is
9660 already provided by the CU attributes. Use
9661 DW_LLE_offset_pair where both addresses are uleb128 encoded
9662 offsets against that base. */
9663 dw2_asm_output_data (1, DW_LLE_offset_pair,
9664 "DW_LLE_offset_pair (%s)",
9665 list_head->ll_symbol);
9666 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9667 "Location list begin address (%s)",
9668 list_head->ll_symbol);
9669 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9670 "Location list end address (%s)",
9671 list_head->ll_symbol);
9673 else if (HAVE_AS_LEB128)
9675 /* Otherwise, find out how many consecutive entries could share
9676 the same base entry. If just one, emit DW_LLE_start_length,
9677 otherwise emit DW_LLE_base_address for the base address
9678 followed by a series of DW_LLE_offset_pair. */
9679 if (last_section == NULL || curr->section != last_section)
9681 dw_loc_list_ref curr2;
9682 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9683 curr2 = curr2->dw_loc_next)
9685 if (strcmp (curr2->begin, curr2->end) == 0
9686 && !curr2->force)
9687 continue;
9688 break;
9690 if (curr2 == NULL || curr->section != curr2->section)
9691 last_section = NULL;
9692 else
9694 last_section = curr->section;
9695 base_label = curr->begin;
9696 dw2_asm_output_data (1, DW_LLE_base_address,
9697 "DW_LLE_base_address (%s)",
9698 list_head->ll_symbol);
9699 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9700 "Base address (%s)",
9701 list_head->ll_symbol);
9704 /* Only one entry with the same base address. Use
9705 DW_LLE_start_length with absolute address and uleb128
9706 length. */
9707 if (last_section == NULL)
9709 dw2_asm_output_data (1, DW_LLE_start_length,
9710 "DW_LLE_start_length (%s)",
9711 list_head->ll_symbol);
9712 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9713 "Location list begin address (%s)",
9714 list_head->ll_symbol);
9715 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9716 "Location list length "
9717 "(%s)", list_head->ll_symbol);
9719 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9720 DW_LLE_base_address. */
9721 else
9723 dw2_asm_output_data (1, DW_LLE_offset_pair,
9724 "DW_LLE_offset_pair (%s)",
9725 list_head->ll_symbol);
9726 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9727 "Location list begin address "
9728 "(%s)", list_head->ll_symbol);
9729 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9730 "Location list end address "
9731 "(%s)", list_head->ll_symbol);
9734 /* The assembler does not support .uleb128 directive. Emit
9735 DW_LLE_start_end with a pair of absolute addresses. */
9736 else
9738 dw2_asm_output_data (1, DW_LLE_start_end,
9739 "DW_LLE_start_end (%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_addr (DWARF2_ADDR_SIZE, curr->end,
9745 "Location list end address (%s)",
9746 list_head->ll_symbol);
9749 else if (dwarf_split_debug_info)
9751 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9752 and 4 byte length. */
9753 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9754 "Location list start/length entry (%s)",
9755 list_head->ll_symbol);
9756 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9757 "Location list range start index (%s)",
9758 curr->begin);
9759 /* The length field is 4 bytes. If we ever need to support
9760 an 8-byte length, we can add a new DW_LLE code or fall back
9761 to DW_LLE_GNU_start_end_entry. */
9762 dw2_asm_output_delta (4, curr->end, curr->begin,
9763 "Location list range length (%s)",
9764 list_head->ll_symbol);
9766 else if (!have_multiple_function_sections)
9768 /* Pair of relative addresses against start of text section. */
9769 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9770 "Location list begin address (%s)",
9771 list_head->ll_symbol);
9772 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9773 "Location list end address (%s)",
9774 list_head->ll_symbol);
9776 else
9778 /* Pair of absolute addresses. */
9779 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9780 "Location list begin address (%s)",
9781 list_head->ll_symbol);
9782 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9783 "Location list end address (%s)",
9784 list_head->ll_symbol);
9787 /* Output the block length for this list of location operations. */
9788 if (dwarf_version >= 5)
9789 dw2_asm_output_data_uleb128 (size, "Location expression size");
9790 else
9792 gcc_assert (size <= 0xffff);
9793 dw2_asm_output_data (2, size, "Location expression size");
9796 output_loc_sequence (curr->expr, -1);
9799 /* And finally list termination. */
9800 if (dwarf_version >= 5)
9801 dw2_asm_output_data (1, DW_LLE_end_of_list,
9802 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9803 else if (dwarf_split_debug_info)
9804 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9805 "Location list terminator (%s)",
9806 list_head->ll_symbol);
9807 else
9809 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9810 "Location list terminator begin (%s)",
9811 list_head->ll_symbol);
9812 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9813 "Location list terminator end (%s)",
9814 list_head->ll_symbol);
9818 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9819 section. Emit a relocated reference if val_entry is NULL, otherwise,
9820 emit an indirect reference. */
9822 static void
9823 output_range_list_offset (dw_attr_node *a)
9825 const char *name = dwarf_attr_name (a->dw_attr);
9827 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9829 if (dwarf_version >= 5)
9831 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9832 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9833 debug_ranges_section, "%s", name);
9835 else
9837 char *p = strchr (ranges_section_label, '\0');
9838 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9839 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9840 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9841 debug_ranges_section, "%s", name);
9842 *p = '\0';
9845 else if (dwarf_version >= 5)
9847 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9848 gcc_assert (rnglist_idx);
9849 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9851 else
9852 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9853 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9854 "%s (offset from %s)", name, ranges_section_label);
9857 /* Output the offset into the debug_loc section. */
9859 static void
9860 output_loc_list_offset (dw_attr_node *a)
9862 char *sym = AT_loc_list (a)->ll_symbol;
9864 gcc_assert (sym);
9865 if (!dwarf_split_debug_info)
9866 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9867 "%s", dwarf_attr_name (a->dw_attr));
9868 else if (dwarf_version >= 5)
9870 gcc_assert (AT_loc_list (a)->num_assigned);
9871 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9872 dwarf_attr_name (a->dw_attr),
9873 sym);
9875 else
9876 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9877 "%s", dwarf_attr_name (a->dw_attr));
9880 /* Output an attribute's index or value appropriately. */
9882 static void
9883 output_attr_index_or_value (dw_attr_node *a)
9885 const char *name = dwarf_attr_name (a->dw_attr);
9887 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9889 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9890 return;
9892 switch (AT_class (a))
9894 case dw_val_class_addr:
9895 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9896 break;
9897 case dw_val_class_high_pc:
9898 case dw_val_class_lbl_id:
9899 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9900 break;
9901 default:
9902 gcc_unreachable ();
9906 /* Output a type signature. */
9908 static inline void
9909 output_signature (const char *sig, const char *name)
9911 int i;
9913 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9914 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9917 /* Output a discriminant value. */
9919 static inline void
9920 output_discr_value (dw_discr_value *discr_value, const char *name)
9922 if (discr_value->pos)
9923 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9924 else
9925 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9928 /* Output the DIE and its attributes. Called recursively to generate
9929 the definitions of each child DIE. */
9931 static void
9932 output_die (dw_die_ref die)
9934 dw_attr_node *a;
9935 dw_die_ref c;
9936 unsigned long size;
9937 unsigned ix;
9939 /* If someone in another CU might refer to us, set up a symbol for
9940 them to point to. */
9941 if (! die->comdat_type_p && die->die_id.die_symbol)
9942 output_die_symbol (die);
9944 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9945 (unsigned long)die->die_offset,
9946 dwarf_tag_name (die->die_tag));
9948 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9950 const char *name = dwarf_attr_name (a->dw_attr);
9952 switch (AT_class (a))
9954 case dw_val_class_addr:
9955 output_attr_index_or_value (a);
9956 break;
9958 case dw_val_class_offset:
9959 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9960 "%s", name);
9961 break;
9963 case dw_val_class_range_list:
9964 output_range_list_offset (a);
9965 break;
9967 case dw_val_class_loc:
9968 size = size_of_locs (AT_loc (a));
9970 /* Output the block length for this list of location operations. */
9971 if (dwarf_version >= 4)
9972 dw2_asm_output_data_uleb128 (size, "%s", name);
9973 else
9974 dw2_asm_output_data (constant_size (size), size, "%s", name);
9976 output_loc_sequence (AT_loc (a), -1);
9977 break;
9979 case dw_val_class_const:
9980 /* ??? It would be slightly more efficient to use a scheme like is
9981 used for unsigned constants below, but gdb 4.x does not sign
9982 extend. Gdb 5.x does sign extend. */
9983 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9984 break;
9986 case dw_val_class_unsigned_const:
9988 int csize = constant_size (AT_unsigned (a));
9989 if (dwarf_version == 3
9990 && a->dw_attr == DW_AT_data_member_location
9991 && csize >= 4)
9992 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9993 else
9994 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9996 break;
9998 case dw_val_class_const_implicit:
9999 if (flag_debug_asm)
10000 fprintf (asm_out_file, "\t\t\t%s %s ("
10001 HOST_WIDE_INT_PRINT_DEC ")\n",
10002 ASM_COMMENT_START, name, AT_int (a));
10003 break;
10005 case dw_val_class_unsigned_const_implicit:
10006 if (flag_debug_asm)
10007 fprintf (asm_out_file, "\t\t\t%s %s ("
10008 HOST_WIDE_INT_PRINT_HEX ")\n",
10009 ASM_COMMENT_START, name, AT_unsigned (a));
10010 break;
10012 case dw_val_class_const_double:
10014 unsigned HOST_WIDE_INT first, second;
10016 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10017 dw2_asm_output_data (1,
10018 HOST_BITS_PER_DOUBLE_INT
10019 / HOST_BITS_PER_CHAR,
10020 NULL);
10022 if (WORDS_BIG_ENDIAN)
10024 first = a->dw_attr_val.v.val_double.high;
10025 second = a->dw_attr_val.v.val_double.low;
10027 else
10029 first = a->dw_attr_val.v.val_double.low;
10030 second = a->dw_attr_val.v.val_double.high;
10033 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10034 first, "%s", name);
10035 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10036 second, NULL);
10038 break;
10040 case dw_val_class_wide_int:
10042 int i;
10043 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10044 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10045 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10046 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10047 * l, NULL);
10049 if (WORDS_BIG_ENDIAN)
10050 for (i = len - 1; i >= 0; --i)
10052 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10053 "%s", name);
10054 name = "";
10056 else
10057 for (i = 0; i < len; ++i)
10059 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10060 "%s", name);
10061 name = "";
10064 break;
10066 case dw_val_class_vec:
10068 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10069 unsigned int len = a->dw_attr_val.v.val_vec.length;
10070 unsigned int i;
10071 unsigned char *p;
10073 dw2_asm_output_data (constant_size (len * elt_size),
10074 len * elt_size, "%s", name);
10075 if (elt_size > sizeof (HOST_WIDE_INT))
10077 elt_size /= 2;
10078 len *= 2;
10080 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10081 i < len;
10082 i++, p += elt_size)
10083 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10084 "fp or vector constant word %u", i);
10085 break;
10088 case dw_val_class_flag:
10089 if (dwarf_version >= 4)
10091 /* Currently all add_AT_flag calls pass in 1 as last argument,
10092 so DW_FORM_flag_present can be used. If that ever changes,
10093 we'll need to use DW_FORM_flag and have some optimization
10094 in build_abbrev_table that will change those to
10095 DW_FORM_flag_present if it is set to 1 in all DIEs using
10096 the same abbrev entry. */
10097 gcc_assert (AT_flag (a) == 1);
10098 if (flag_debug_asm)
10099 fprintf (asm_out_file, "\t\t\t%s %s\n",
10100 ASM_COMMENT_START, name);
10101 break;
10103 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10104 break;
10106 case dw_val_class_loc_list:
10107 output_loc_list_offset (a);
10108 break;
10110 case dw_val_class_die_ref:
10111 if (AT_ref_external (a))
10113 if (AT_ref (a)->comdat_type_p)
10115 comdat_type_node *type_node
10116 = AT_ref (a)->die_id.die_type_node;
10118 gcc_assert (type_node);
10119 output_signature (type_node->signature, name);
10121 else
10123 const char *sym = AT_ref (a)->die_id.die_symbol;
10124 int size;
10126 gcc_assert (sym);
10127 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10128 length, whereas in DWARF3 it's always sized as an
10129 offset. */
10130 if (dwarf_version == 2)
10131 size = DWARF2_ADDR_SIZE;
10132 else
10133 size = DWARF_OFFSET_SIZE;
10134 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10135 name);
10138 else
10140 gcc_assert (AT_ref (a)->die_offset);
10141 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10142 "%s", name);
10144 break;
10146 case dw_val_class_fde_ref:
10148 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10150 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10151 a->dw_attr_val.v.val_fde_index * 2);
10152 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10153 "%s", name);
10155 break;
10157 case dw_val_class_vms_delta:
10158 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10159 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10160 AT_vms_delta2 (a), AT_vms_delta1 (a),
10161 "%s", name);
10162 #else
10163 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10164 AT_vms_delta2 (a), AT_vms_delta1 (a),
10165 "%s", name);
10166 #endif
10167 break;
10169 case dw_val_class_lbl_id:
10170 output_attr_index_or_value (a);
10171 break;
10173 case dw_val_class_lineptr:
10174 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10175 debug_line_section, "%s", name);
10176 break;
10178 case dw_val_class_macptr:
10179 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10180 debug_macinfo_section, "%s", name);
10181 break;
10183 case dw_val_class_loclistsptr:
10184 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10185 debug_loc_section, "%s", name);
10186 break;
10188 case dw_val_class_str:
10189 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10190 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10191 a->dw_attr_val.v.val_str->label,
10192 debug_str_section,
10193 "%s: \"%s\"", name, AT_string (a));
10194 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10195 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10196 a->dw_attr_val.v.val_str->label,
10197 debug_line_str_section,
10198 "%s: \"%s\"", name, AT_string (a));
10199 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10200 dw2_asm_output_data_uleb128 (AT_index (a),
10201 "%s: \"%s\"", name, AT_string (a));
10202 else
10203 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10204 break;
10206 case dw_val_class_file:
10208 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10210 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10211 a->dw_attr_val.v.val_file->filename);
10212 break;
10215 case dw_val_class_file_implicit:
10216 if (flag_debug_asm)
10217 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10218 ASM_COMMENT_START, name,
10219 maybe_emit_file (a->dw_attr_val.v.val_file),
10220 a->dw_attr_val.v.val_file->filename);
10221 break;
10223 case dw_val_class_data8:
10225 int i;
10227 for (i = 0; i < 8; i++)
10228 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10229 i == 0 ? "%s" : NULL, name);
10230 break;
10233 case dw_val_class_high_pc:
10234 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10235 get_AT_low_pc (die), "DW_AT_high_pc");
10236 break;
10238 case dw_val_class_discr_value:
10239 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10240 break;
10242 case dw_val_class_discr_list:
10244 dw_discr_list_ref list = AT_discr_list (a);
10245 const int size = size_of_discr_list (list);
10247 /* This is a block, so output its length first. */
10248 dw2_asm_output_data (constant_size (size), size,
10249 "%s: block size", name);
10251 for (; list != NULL; list = list->dw_discr_next)
10253 /* One byte for the discriminant value descriptor, and then as
10254 many LEB128 numbers as required. */
10255 if (list->dw_discr_range)
10256 dw2_asm_output_data (1, DW_DSC_range,
10257 "%s: DW_DSC_range", name);
10258 else
10259 dw2_asm_output_data (1, DW_DSC_label,
10260 "%s: DW_DSC_label", name);
10262 output_discr_value (&list->dw_discr_lower_bound, name);
10263 if (list->dw_discr_range)
10264 output_discr_value (&list->dw_discr_upper_bound, name);
10266 break;
10269 default:
10270 gcc_unreachable ();
10274 FOR_EACH_CHILD (die, c, output_die (c));
10276 /* Add null byte to terminate sibling list. */
10277 if (die->die_child != NULL)
10278 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10279 (unsigned long) die->die_offset);
10282 /* Output the compilation unit that appears at the beginning of the
10283 .debug_info section, and precedes the DIE descriptions. */
10285 static void
10286 output_compilation_unit_header (enum dwarf_unit_type ut)
10288 if (!XCOFF_DEBUGGING_INFO)
10290 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10291 dw2_asm_output_data (4, 0xffffffff,
10292 "Initial length escape value indicating 64-bit DWARF extension");
10293 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10294 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10295 "Length of Compilation Unit Info");
10298 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10299 if (dwarf_version >= 5)
10301 const char *name;
10302 switch (ut)
10304 case DW_UT_compile: name = "DW_UT_compile"; break;
10305 case DW_UT_type: name = "DW_UT_type"; break;
10306 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10307 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10308 default: gcc_unreachable ();
10310 dw2_asm_output_data (1, ut, "%s", name);
10311 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10313 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10314 debug_abbrev_section,
10315 "Offset Into Abbrev. Section");
10316 if (dwarf_version < 5)
10317 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10320 /* Output the compilation unit DIE and its children. */
10322 static void
10323 output_comp_unit (dw_die_ref die, int output_if_empty,
10324 const unsigned char *dwo_id)
10326 const char *secname, *oldsym;
10327 char *tmp;
10329 /* Unless we are outputting main CU, we may throw away empty ones. */
10330 if (!output_if_empty && die->die_child == NULL)
10331 return;
10333 /* Even if there are no children of this DIE, we must output the information
10334 about the compilation unit. Otherwise, on an empty translation unit, we
10335 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10336 will then complain when examining the file. First mark all the DIEs in
10337 this CU so we know which get local refs. */
10338 mark_dies (die);
10340 external_ref_hash_type *extern_map = optimize_external_refs (die);
10342 /* For now, optimize only the main CU, in order to optimize the rest
10343 we'd need to see all of them earlier. Leave the rest for post-linking
10344 tools like DWZ. */
10345 if (die == comp_unit_die ())
10346 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10348 build_abbrev_table (die, extern_map);
10350 optimize_abbrev_table ();
10352 delete extern_map;
10354 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10355 next_die_offset = (dwo_id
10356 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10357 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10358 calc_die_sizes (die);
10360 oldsym = die->die_id.die_symbol;
10361 if (oldsym)
10363 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10365 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10366 secname = tmp;
10367 die->die_id.die_symbol = NULL;
10368 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10370 else
10372 switch_to_section (debug_info_section);
10373 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10374 info_section_emitted = true;
10377 /* Output debugging information. */
10378 output_compilation_unit_header (dwo_id
10379 ? DW_UT_split_compile : DW_UT_compile);
10380 if (dwarf_version >= 5)
10382 if (dwo_id != NULL)
10383 for (int i = 0; i < 8; i++)
10384 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10386 output_die (die);
10388 /* Leave the marks on the main CU, so we can check them in
10389 output_pubnames. */
10390 if (oldsym)
10392 unmark_dies (die);
10393 die->die_id.die_symbol = oldsym;
10397 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10398 and .debug_pubtypes. This is configured per-target, but can be
10399 overridden by the -gpubnames or -gno-pubnames options. */
10401 static inline bool
10402 want_pubnames (void)
10404 if (debug_info_level <= DINFO_LEVEL_TERSE)
10405 return false;
10406 if (debug_generate_pub_sections != -1)
10407 return debug_generate_pub_sections;
10408 return targetm.want_debug_pub_sections;
10411 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10413 static void
10414 add_AT_pubnames (dw_die_ref die)
10416 if (want_pubnames ())
10417 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10420 /* Add a string attribute value to a skeleton DIE. */
10422 static inline void
10423 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10424 const char *str)
10426 dw_attr_node attr;
10427 struct indirect_string_node *node;
10429 if (! skeleton_debug_str_hash)
10430 skeleton_debug_str_hash
10431 = hash_table<indirect_string_hasher>::create_ggc (10);
10433 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10434 find_string_form (node);
10435 if (node->form == DW_FORM_GNU_str_index)
10436 node->form = DW_FORM_strp;
10438 attr.dw_attr = attr_kind;
10439 attr.dw_attr_val.val_class = dw_val_class_str;
10440 attr.dw_attr_val.val_entry = NULL;
10441 attr.dw_attr_val.v.val_str = node;
10442 add_dwarf_attr (die, &attr);
10445 /* Helper function to generate top-level dies for skeleton debug_info and
10446 debug_types. */
10448 static void
10449 add_top_level_skeleton_die_attrs (dw_die_ref die)
10451 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10452 const char *comp_dir = comp_dir_string ();
10454 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10455 if (comp_dir != NULL)
10456 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10457 add_AT_pubnames (die);
10458 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10461 /* Output skeleton debug sections that point to the dwo file. */
10463 static void
10464 output_skeleton_debug_sections (dw_die_ref comp_unit,
10465 const unsigned char *dwo_id)
10467 /* These attributes will be found in the full debug_info section. */
10468 remove_AT (comp_unit, DW_AT_producer);
10469 remove_AT (comp_unit, DW_AT_language);
10471 switch_to_section (debug_skeleton_info_section);
10472 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10474 /* Produce the skeleton compilation-unit header. This one differs enough from
10475 a normal CU header that it's better not to call output_compilation_unit
10476 header. */
10477 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10478 dw2_asm_output_data (4, 0xffffffff,
10479 "Initial length escape value indicating 64-bit "
10480 "DWARF extension");
10482 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10483 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10484 - DWARF_INITIAL_LENGTH_SIZE
10485 + size_of_die (comp_unit),
10486 "Length of Compilation Unit Info");
10487 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10488 if (dwarf_version >= 5)
10490 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10491 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10493 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10494 debug_skeleton_abbrev_section,
10495 "Offset Into Abbrev. Section");
10496 if (dwarf_version < 5)
10497 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10498 else
10499 for (int i = 0; i < 8; i++)
10500 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10502 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10503 output_die (comp_unit);
10505 /* Build the skeleton debug_abbrev section. */
10506 switch_to_section (debug_skeleton_abbrev_section);
10507 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10509 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10511 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10514 /* Output a comdat type unit DIE and its children. */
10516 static void
10517 output_comdat_type_unit (comdat_type_node *node)
10519 const char *secname;
10520 char *tmp;
10521 int i;
10522 #if defined (OBJECT_FORMAT_ELF)
10523 tree comdat_key;
10524 #endif
10526 /* First mark all the DIEs in this CU so we know which get local refs. */
10527 mark_dies (node->root_die);
10529 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10531 build_abbrev_table (node->root_die, extern_map);
10533 delete extern_map;
10534 extern_map = NULL;
10536 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10537 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10538 calc_die_sizes (node->root_die);
10540 #if defined (OBJECT_FORMAT_ELF)
10541 if (dwarf_version >= 5)
10543 if (!dwarf_split_debug_info)
10544 secname = ".debug_info";
10545 else
10546 secname = ".debug_info.dwo";
10548 else if (!dwarf_split_debug_info)
10549 secname = ".debug_types";
10550 else
10551 secname = ".debug_types.dwo";
10553 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10554 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10555 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10556 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10557 comdat_key = get_identifier (tmp);
10558 targetm.asm_out.named_section (secname,
10559 SECTION_DEBUG | SECTION_LINKONCE,
10560 comdat_key);
10561 #else
10562 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10563 sprintf (tmp, (dwarf_version >= 5
10564 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10565 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10566 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10567 secname = tmp;
10568 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10569 #endif
10571 /* Output debugging information. */
10572 output_compilation_unit_header (dwarf_split_debug_info
10573 ? DW_UT_split_type : DW_UT_type);
10574 output_signature (node->signature, "Type Signature");
10575 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10576 "Offset to Type DIE");
10577 output_die (node->root_die);
10579 unmark_dies (node->root_die);
10582 /* Return the DWARF2/3 pubname associated with a decl. */
10584 static const char *
10585 dwarf2_name (tree decl, int scope)
10587 if (DECL_NAMELESS (decl))
10588 return NULL;
10589 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10592 /* Add a new entry to .debug_pubnames if appropriate. */
10594 static void
10595 add_pubname_string (const char *str, dw_die_ref die)
10597 pubname_entry e;
10599 e.die = die;
10600 e.name = xstrdup (str);
10601 vec_safe_push (pubname_table, e);
10604 static void
10605 add_pubname (tree decl, dw_die_ref die)
10607 if (!want_pubnames ())
10608 return;
10610 /* Don't add items to the table when we expect that the consumer will have
10611 just read the enclosing die. For example, if the consumer is looking at a
10612 class_member, it will either be inside the class already, or will have just
10613 looked up the class to find the member. Either way, searching the class is
10614 faster than searching the index. */
10615 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10616 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10618 const char *name = dwarf2_name (decl, 1);
10620 if (name)
10621 add_pubname_string (name, die);
10625 /* Add an enumerator to the pubnames section. */
10627 static void
10628 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10630 pubname_entry e;
10632 gcc_assert (scope_name);
10633 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10634 e.die = die;
10635 vec_safe_push (pubname_table, e);
10638 /* Add a new entry to .debug_pubtypes if appropriate. */
10640 static void
10641 add_pubtype (tree decl, dw_die_ref die)
10643 pubname_entry e;
10645 if (!want_pubnames ())
10646 return;
10648 if ((TREE_PUBLIC (decl)
10649 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10650 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10652 tree scope = NULL;
10653 const char *scope_name = "";
10654 const char *sep = is_cxx () ? "::" : ".";
10655 const char *name;
10657 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10658 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10660 scope_name = lang_hooks.dwarf_name (scope, 1);
10661 if (scope_name != NULL && scope_name[0] != '\0')
10662 scope_name = concat (scope_name, sep, NULL);
10663 else
10664 scope_name = "";
10667 if (TYPE_P (decl))
10668 name = type_tag (decl);
10669 else
10670 name = lang_hooks.dwarf_name (decl, 1);
10672 /* If we don't have a name for the type, there's no point in adding
10673 it to the table. */
10674 if (name != NULL && name[0] != '\0')
10676 e.die = die;
10677 e.name = concat (scope_name, name, NULL);
10678 vec_safe_push (pubtype_table, e);
10681 /* Although it might be more consistent to add the pubinfo for the
10682 enumerators as their dies are created, they should only be added if the
10683 enum type meets the criteria above. So rather than re-check the parent
10684 enum type whenever an enumerator die is created, just output them all
10685 here. This isn't protected by the name conditional because anonymous
10686 enums don't have names. */
10687 if (die->die_tag == DW_TAG_enumeration_type)
10689 dw_die_ref c;
10691 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10696 /* Output a single entry in the pubnames table. */
10698 static void
10699 output_pubname (dw_offset die_offset, pubname_entry *entry)
10701 dw_die_ref die = entry->die;
10702 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10704 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10706 if (debug_generate_pub_sections == 2)
10708 /* This logic follows gdb's method for determining the value of the flag
10709 byte. */
10710 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10711 switch (die->die_tag)
10713 case DW_TAG_typedef:
10714 case DW_TAG_base_type:
10715 case DW_TAG_subrange_type:
10716 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10717 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10718 break;
10719 case DW_TAG_enumerator:
10720 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10721 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10722 if (!is_cxx () && !is_java ())
10723 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10724 break;
10725 case DW_TAG_subprogram:
10726 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10727 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10728 if (!is_ada ())
10729 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10730 break;
10731 case DW_TAG_constant:
10732 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10733 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10734 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10735 break;
10736 case DW_TAG_variable:
10737 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10738 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10739 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10740 break;
10741 case DW_TAG_namespace:
10742 case DW_TAG_imported_declaration:
10743 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10744 break;
10745 case DW_TAG_class_type:
10746 case DW_TAG_interface_type:
10747 case DW_TAG_structure_type:
10748 case DW_TAG_union_type:
10749 case DW_TAG_enumeration_type:
10750 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10751 if (!is_cxx () && !is_java ())
10752 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10753 break;
10754 default:
10755 /* An unusual tag. Leave the flag-byte empty. */
10756 break;
10758 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10759 "GDB-index flags");
10762 dw2_asm_output_nstring (entry->name, -1, "external name");
10766 /* Output the public names table used to speed up access to externally
10767 visible names; or the public types table used to find type definitions. */
10769 static void
10770 output_pubnames (vec<pubname_entry, va_gc> *names)
10772 unsigned i;
10773 unsigned long pubnames_length = size_of_pubnames (names);
10774 pubname_entry *pub;
10776 if (!XCOFF_DEBUGGING_INFO)
10778 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10779 dw2_asm_output_data (4, 0xffffffff,
10780 "Initial length escape value indicating 64-bit DWARF extension");
10781 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10782 "Pub Info Length");
10785 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10786 dw2_asm_output_data (2, 2, "DWARF Version");
10788 if (dwarf_split_debug_info)
10789 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10790 debug_skeleton_info_section,
10791 "Offset of Compilation Unit Info");
10792 else
10793 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10794 debug_info_section,
10795 "Offset of Compilation Unit Info");
10796 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10797 "Compilation Unit Length");
10799 FOR_EACH_VEC_ELT (*names, i, pub)
10801 if (include_pubname_in_output (names, pub))
10803 dw_offset die_offset = pub->die->die_offset;
10805 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10806 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10807 gcc_assert (pub->die->die_mark);
10809 /* If we're putting types in their own .debug_types sections,
10810 the .debug_pubtypes table will still point to the compile
10811 unit (not the type unit), so we want to use the offset of
10812 the skeleton DIE (if there is one). */
10813 if (pub->die->comdat_type_p && names == pubtype_table)
10815 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10817 if (type_node != NULL)
10818 die_offset = (type_node->skeleton_die != NULL
10819 ? type_node->skeleton_die->die_offset
10820 : comp_unit_die ()->die_offset);
10823 output_pubname (die_offset, pub);
10827 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10830 /* Output public names and types tables if necessary. */
10832 static void
10833 output_pubtables (void)
10835 if (!want_pubnames () || !info_section_emitted)
10836 return;
10838 switch_to_section (debug_pubnames_section);
10839 output_pubnames (pubname_table);
10840 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10841 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10842 simply won't look for the section. */
10843 switch_to_section (debug_pubtypes_section);
10844 output_pubnames (pubtype_table);
10848 /* Output the information that goes into the .debug_aranges table.
10849 Namely, define the beginning and ending address range of the
10850 text section generated for this compilation unit. */
10852 static void
10853 output_aranges (void)
10855 unsigned i;
10856 unsigned long aranges_length = size_of_aranges ();
10858 if (!XCOFF_DEBUGGING_INFO)
10860 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10861 dw2_asm_output_data (4, 0xffffffff,
10862 "Initial length escape value indicating 64-bit DWARF extension");
10863 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10864 "Length of Address Ranges Info");
10867 /* Version number for aranges is still 2, even up to DWARF5. */
10868 dw2_asm_output_data (2, 2, "DWARF Version");
10869 if (dwarf_split_debug_info)
10870 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10871 debug_skeleton_info_section,
10872 "Offset of Compilation Unit Info");
10873 else
10874 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10875 debug_info_section,
10876 "Offset of Compilation Unit Info");
10877 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10878 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10880 /* We need to align to twice the pointer size here. */
10881 if (DWARF_ARANGES_PAD_SIZE)
10883 /* Pad using a 2 byte words so that padding is correct for any
10884 pointer size. */
10885 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10886 2 * DWARF2_ADDR_SIZE);
10887 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10888 dw2_asm_output_data (2, 0, NULL);
10891 /* It is necessary not to output these entries if the sections were
10892 not used; if the sections were not used, the length will be 0 and
10893 the address may end up as 0 if the section is discarded by ld
10894 --gc-sections, leaving an invalid (0, 0) entry that can be
10895 confused with the terminator. */
10896 if (text_section_used)
10898 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10899 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10900 text_section_label, "Length");
10902 if (cold_text_section_used)
10904 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10905 "Address");
10906 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10907 cold_text_section_label, "Length");
10910 if (have_multiple_function_sections)
10912 unsigned fde_idx;
10913 dw_fde_ref fde;
10915 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10917 if (DECL_IGNORED_P (fde->decl))
10918 continue;
10919 if (!fde->in_std_section)
10921 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10922 "Address");
10923 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10924 fde->dw_fde_begin, "Length");
10926 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10928 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10929 "Address");
10930 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10931 fde->dw_fde_second_begin, "Length");
10936 /* Output the terminator words. */
10937 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10938 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10941 /* Add a new entry to .debug_ranges. Return its index into
10942 ranges_table vector. */
10944 static unsigned int
10945 add_ranges_num (int num, bool maybe_new_sec)
10947 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10948 vec_safe_push (ranges_table, r);
10949 return vec_safe_length (ranges_table) - 1;
10952 /* Add a new entry to .debug_ranges corresponding to a block, or a
10953 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10954 this entry might be in a different section from previous range. */
10956 static unsigned int
10957 add_ranges (const_tree block, bool maybe_new_sec)
10959 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10962 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10963 chain, or middle entry of a chain that will be directly referred to. */
10965 static void
10966 note_rnglist_head (unsigned int offset)
10968 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10969 return;
10970 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
10973 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10974 When using dwarf_split_debug_info, address attributes in dies destined
10975 for the final executable should be direct references--setting the
10976 parameter force_direct ensures this behavior. */
10978 static void
10979 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10980 bool *added, bool force_direct)
10982 unsigned int in_use = vec_safe_length (ranges_by_label);
10983 unsigned int offset;
10984 dw_ranges_by_label rbl = { begin, end };
10985 vec_safe_push (ranges_by_label, rbl);
10986 offset = add_ranges_num (-(int)in_use - 1, true);
10987 if (!*added)
10989 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10990 *added = true;
10991 note_rnglist_head (offset);
10995 /* Emit .debug_ranges section. */
10997 static void
10998 output_ranges (void)
11000 unsigned i;
11001 static const char *const start_fmt = "Offset %#x";
11002 const char *fmt = start_fmt;
11003 dw_ranges *r;
11005 switch_to_section (debug_ranges_section);
11006 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11007 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11009 int block_num = r->num;
11011 if (block_num > 0)
11013 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11014 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11016 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11017 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11019 /* If all code is in the text section, then the compilation
11020 unit base address defaults to DW_AT_low_pc, which is the
11021 base of the text section. */
11022 if (!have_multiple_function_sections)
11024 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11025 text_section_label,
11026 fmt, i * 2 * DWARF2_ADDR_SIZE);
11027 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11028 text_section_label, NULL);
11031 /* Otherwise, the compilation unit base address is zero,
11032 which allows us to use absolute addresses, and not worry
11033 about whether the target supports cross-section
11034 arithmetic. */
11035 else
11037 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11038 fmt, i * 2 * DWARF2_ADDR_SIZE);
11039 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11042 fmt = NULL;
11045 /* Negative block_num stands for an index into ranges_by_label. */
11046 else if (block_num < 0)
11048 int lab_idx = - block_num - 1;
11050 if (!have_multiple_function_sections)
11052 gcc_unreachable ();
11053 #if 0
11054 /* If we ever use add_ranges_by_labels () for a single
11055 function section, all we have to do is to take out
11056 the #if 0 above. */
11057 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11058 (*ranges_by_label)[lab_idx].begin,
11059 text_section_label,
11060 fmt, i * 2 * DWARF2_ADDR_SIZE);
11061 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11062 (*ranges_by_label)[lab_idx].end,
11063 text_section_label, NULL);
11064 #endif
11066 else
11068 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11069 (*ranges_by_label)[lab_idx].begin,
11070 fmt, i * 2 * DWARF2_ADDR_SIZE);
11071 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11072 (*ranges_by_label)[lab_idx].end,
11073 NULL);
11076 else
11078 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11079 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11080 fmt = start_fmt;
11085 /* Non-zero if .debug_line_str should be used for .debug_line section
11086 strings or strings that are likely shareable with those. */
11087 #define DWARF5_USE_DEBUG_LINE_STR \
11088 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11089 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11090 /* FIXME: there is no .debug_line_str.dwo section, \
11091 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11092 && !dwarf_split_debug_info)
11094 /* Assign .debug_rnglists indexes. */
11096 static void
11097 index_rnglists (void)
11099 unsigned i;
11100 dw_ranges *r;
11102 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11103 if (r->label)
11104 r->idx = rnglist_idx++;
11107 /* Emit .debug_rnglists section. */
11109 static void
11110 output_rnglists (void)
11112 unsigned i;
11113 dw_ranges *r;
11114 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11115 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11116 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11118 switch_to_section (debug_ranges_section);
11119 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11120 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11121 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11122 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11123 dw2_asm_output_data (4, 0xffffffff,
11124 "Initial length escape value indicating "
11125 "64-bit DWARF extension");
11126 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11127 "Length of Range Lists");
11128 ASM_OUTPUT_LABEL (asm_out_file, l1);
11129 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11130 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11131 dw2_asm_output_data (1, 0, "Segment Size");
11132 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11133 about relocation sizes and primarily care about the size of .debug*
11134 sections in linked shared libraries and executables, then
11135 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11136 into it are usually larger than just DW_FORM_sec_offset offsets
11137 into the .debug_rnglists section. */
11138 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11139 "Offset Entry Count");
11140 if (dwarf_split_debug_info)
11142 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11143 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11144 if (r->label)
11145 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11146 ranges_base_label, NULL);
11149 const char *lab = "";
11150 unsigned int len = vec_safe_length (ranges_table);
11151 const char *base = NULL;
11152 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11154 int block_num = r->num;
11156 if (r->label)
11158 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11159 lab = r->label;
11161 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11162 base = NULL;
11163 if (block_num > 0)
11165 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11166 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11168 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11169 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11171 if (HAVE_AS_LEB128)
11173 /* If all code is in the text section, then the compilation
11174 unit base address defaults to DW_AT_low_pc, which is the
11175 base of the text section. */
11176 if (!have_multiple_function_sections)
11178 dw2_asm_output_data (1, DW_RLE_offset_pair,
11179 "DW_RLE_offset_pair (%s)", lab);
11180 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11181 "Range begin address (%s)", lab);
11182 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11183 "Range end address (%s)", lab);
11184 continue;
11186 if (base == NULL)
11188 dw_ranges *r2 = NULL;
11189 if (i < len - 1)
11190 r2 = &(*ranges_table)[i + 1];
11191 if (r2
11192 && r2->num != 0
11193 && r2->label == NULL
11194 && !r2->maybe_new_sec)
11196 dw2_asm_output_data (1, DW_RLE_base_address,
11197 "DW_RLE_base_address (%s)", lab);
11198 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11199 "Base address (%s)", lab);
11200 strcpy (basebuf, blabel);
11201 base = basebuf;
11204 if (base)
11206 dw2_asm_output_data (1, DW_RLE_offset_pair,
11207 "DW_RLE_offset_pair (%s)", lab);
11208 dw2_asm_output_delta_uleb128 (blabel, base,
11209 "Range begin address (%s)", lab);
11210 dw2_asm_output_delta_uleb128 (elabel, base,
11211 "Range end address (%s)", lab);
11212 continue;
11214 dw2_asm_output_data (1, DW_RLE_start_length,
11215 "DW_RLE_start_length (%s)", lab);
11216 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11217 "Range begin address (%s)", lab);
11218 dw2_asm_output_delta_uleb128 (elabel, blabel,
11219 "Range length (%s)", lab);
11221 else
11223 dw2_asm_output_data (1, DW_RLE_start_end,
11224 "DW_RLE_start_end (%s)", lab);
11225 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11226 "Range begin address (%s)", lab);
11227 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11228 "Range end address (%s)", lab);
11232 /* Negative block_num stands for an index into ranges_by_label. */
11233 else if (block_num < 0)
11235 int lab_idx = - block_num - 1;
11236 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11237 const char *elabel = (*ranges_by_label)[lab_idx].end;
11239 if (!have_multiple_function_sections)
11240 gcc_unreachable ();
11241 if (HAVE_AS_LEB128)
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);
11260 else
11261 dw2_asm_output_data (1, DW_RLE_end_of_list,
11262 "DW_RLE_end_of_list (%s)", lab);
11264 ASM_OUTPUT_LABEL (asm_out_file, l2);
11267 /* Data structure containing information about input files. */
11268 struct file_info
11270 const char *path; /* Complete file name. */
11271 const char *fname; /* File name part. */
11272 int length; /* Length of entire string. */
11273 struct dwarf_file_data * file_idx; /* Index in input file table. */
11274 int dir_idx; /* Index in directory table. */
11277 /* Data structure containing information about directories with source
11278 files. */
11279 struct dir_info
11281 const char *path; /* Path including directory name. */
11282 int length; /* Path length. */
11283 int prefix; /* Index of directory entry which is a prefix. */
11284 int count; /* Number of files in this directory. */
11285 int dir_idx; /* Index of directory used as base. */
11288 /* Callback function for file_info comparison. We sort by looking at
11289 the directories in the path. */
11291 static int
11292 file_info_cmp (const void *p1, const void *p2)
11294 const struct file_info *const s1 = (const struct file_info *) p1;
11295 const struct file_info *const s2 = (const struct file_info *) p2;
11296 const unsigned char *cp1;
11297 const unsigned char *cp2;
11299 /* Take care of file names without directories. We need to make sure that
11300 we return consistent values to qsort since some will get confused if
11301 we return the same value when identical operands are passed in opposite
11302 orders. So if neither has a directory, return 0 and otherwise return
11303 1 or -1 depending on which one has the directory. */
11304 if ((s1->path == s1->fname || s2->path == s2->fname))
11305 return (s2->path == s2->fname) - (s1->path == s1->fname);
11307 cp1 = (const unsigned char *) s1->path;
11308 cp2 = (const unsigned char *) s2->path;
11310 while (1)
11312 ++cp1;
11313 ++cp2;
11314 /* Reached the end of the first path? If so, handle like above. */
11315 if ((cp1 == (const unsigned char *) s1->fname)
11316 || (cp2 == (const unsigned char *) s2->fname))
11317 return ((cp2 == (const unsigned char *) s2->fname)
11318 - (cp1 == (const unsigned char *) s1->fname));
11320 /* Character of current path component the same? */
11321 else if (*cp1 != *cp2)
11322 return *cp1 - *cp2;
11326 struct file_name_acquire_data
11328 struct file_info *files;
11329 int used_files;
11330 int max_files;
11333 /* Traversal function for the hash table. */
11336 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11338 struct dwarf_file_data *d = *slot;
11339 struct file_info *fi;
11340 const char *f;
11342 gcc_assert (fnad->max_files >= d->emitted_number);
11344 if (! d->emitted_number)
11345 return 1;
11347 gcc_assert (fnad->max_files != fnad->used_files);
11349 fi = fnad->files + fnad->used_files++;
11351 /* Skip all leading "./". */
11352 f = d->filename;
11353 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11354 f += 2;
11356 /* Create a new array entry. */
11357 fi->path = f;
11358 fi->length = strlen (f);
11359 fi->file_idx = d;
11361 /* Search for the file name part. */
11362 f = strrchr (f, DIR_SEPARATOR);
11363 #if defined (DIR_SEPARATOR_2)
11365 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11367 if (g != NULL)
11369 if (f == NULL || f < g)
11370 f = g;
11373 #endif
11375 fi->fname = f == NULL ? fi->path : f + 1;
11376 return 1;
11379 /* Helper function for output_file_names. Emit a FORM encoded
11380 string STR, with assembly comment start ENTRY_KIND and
11381 index IDX */
11383 static void
11384 output_line_string (enum dwarf_form form, const char *str,
11385 const char *entry_kind, unsigned int idx)
11387 switch (form)
11389 case DW_FORM_string:
11390 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11391 break;
11392 case DW_FORM_line_strp:
11393 if (!debug_line_str_hash)
11394 debug_line_str_hash
11395 = hash_table<indirect_string_hasher>::create_ggc (10);
11397 struct indirect_string_node *node;
11398 node = find_AT_string_in_table (str, debug_line_str_hash);
11399 set_indirect_string (node);
11400 node->form = form;
11401 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11402 debug_line_str_section, "%s: %#x: \"%s\"",
11403 entry_kind, 0, node->str);
11404 break;
11405 default:
11406 gcc_unreachable ();
11410 /* Output the directory table and the file name table. We try to minimize
11411 the total amount of memory needed. A heuristic is used to avoid large
11412 slowdowns with many input files. */
11414 static void
11415 output_file_names (void)
11417 struct file_name_acquire_data fnad;
11418 int numfiles;
11419 struct file_info *files;
11420 struct dir_info *dirs;
11421 int *saved;
11422 int *savehere;
11423 int *backmap;
11424 int ndirs;
11425 int idx_offset;
11426 int i;
11428 if (!last_emitted_file)
11430 if (dwarf_version >= 5)
11432 dw2_asm_output_data (1, 0, "Directory entry format count");
11433 dw2_asm_output_data_uleb128 (0, "Directories count");
11434 dw2_asm_output_data (1, 0, "File name entry format count");
11435 dw2_asm_output_data_uleb128 (0, "File names count");
11437 else
11439 dw2_asm_output_data (1, 0, "End directory table");
11440 dw2_asm_output_data (1, 0, "End file name table");
11442 return;
11445 numfiles = last_emitted_file->emitted_number;
11447 /* Allocate the various arrays we need. */
11448 files = XALLOCAVEC (struct file_info, numfiles);
11449 dirs = XALLOCAVEC (struct dir_info, numfiles);
11451 fnad.files = files;
11452 fnad.used_files = 0;
11453 fnad.max_files = numfiles;
11454 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11455 gcc_assert (fnad.used_files == fnad.max_files);
11457 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11459 /* Find all the different directories used. */
11460 dirs[0].path = files[0].path;
11461 dirs[0].length = files[0].fname - files[0].path;
11462 dirs[0].prefix = -1;
11463 dirs[0].count = 1;
11464 dirs[0].dir_idx = 0;
11465 files[0].dir_idx = 0;
11466 ndirs = 1;
11468 for (i = 1; i < numfiles; i++)
11469 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11470 && memcmp (dirs[ndirs - 1].path, files[i].path,
11471 dirs[ndirs - 1].length) == 0)
11473 /* Same directory as last entry. */
11474 files[i].dir_idx = ndirs - 1;
11475 ++dirs[ndirs - 1].count;
11477 else
11479 int j;
11481 /* This is a new directory. */
11482 dirs[ndirs].path = files[i].path;
11483 dirs[ndirs].length = files[i].fname - files[i].path;
11484 dirs[ndirs].count = 1;
11485 dirs[ndirs].dir_idx = ndirs;
11486 files[i].dir_idx = ndirs;
11488 /* Search for a prefix. */
11489 dirs[ndirs].prefix = -1;
11490 for (j = 0; j < ndirs; j++)
11491 if (dirs[j].length < dirs[ndirs].length
11492 && dirs[j].length > 1
11493 && (dirs[ndirs].prefix == -1
11494 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11495 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11496 dirs[ndirs].prefix = j;
11498 ++ndirs;
11501 /* Now to the actual work. We have to find a subset of the directories which
11502 allow expressing the file name using references to the directory table
11503 with the least amount of characters. We do not do an exhaustive search
11504 where we would have to check out every combination of every single
11505 possible prefix. Instead we use a heuristic which provides nearly optimal
11506 results in most cases and never is much off. */
11507 saved = XALLOCAVEC (int, ndirs);
11508 savehere = XALLOCAVEC (int, ndirs);
11510 memset (saved, '\0', ndirs * sizeof (saved[0]));
11511 for (i = 0; i < ndirs; i++)
11513 int j;
11514 int total;
11516 /* We can always save some space for the current directory. But this
11517 does not mean it will be enough to justify adding the directory. */
11518 savehere[i] = dirs[i].length;
11519 total = (savehere[i] - saved[i]) * dirs[i].count;
11521 for (j = i + 1; j < ndirs; j++)
11523 savehere[j] = 0;
11524 if (saved[j] < dirs[i].length)
11526 /* Determine whether the dirs[i] path is a prefix of the
11527 dirs[j] path. */
11528 int k;
11530 k = dirs[j].prefix;
11531 while (k != -1 && k != (int) i)
11532 k = dirs[k].prefix;
11534 if (k == (int) i)
11536 /* Yes it is. We can possibly save some memory by
11537 writing the filenames in dirs[j] relative to
11538 dirs[i]. */
11539 savehere[j] = dirs[i].length;
11540 total += (savehere[j] - saved[j]) * dirs[j].count;
11545 /* Check whether we can save enough to justify adding the dirs[i]
11546 directory. */
11547 if (total > dirs[i].length + 1)
11549 /* It's worthwhile adding. */
11550 for (j = i; j < ndirs; j++)
11551 if (savehere[j] > 0)
11553 /* Remember how much we saved for this directory so far. */
11554 saved[j] = savehere[j];
11556 /* Remember the prefix directory. */
11557 dirs[j].dir_idx = i;
11562 /* Emit the directory name table. */
11563 idx_offset = dirs[0].length > 0 ? 1 : 0;
11564 enum dwarf_form str_form = DW_FORM_string;
11565 enum dwarf_form idx_form = DW_FORM_udata;
11566 if (dwarf_version >= 5)
11568 const char *comp_dir = comp_dir_string ();
11569 if (comp_dir == NULL)
11570 comp_dir = "";
11571 dw2_asm_output_data (1, 1, "Directory entry format count");
11572 if (DWARF5_USE_DEBUG_LINE_STR)
11573 str_form = DW_FORM_line_strp;
11574 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11575 dw2_asm_output_data_uleb128 (str_form, "%s",
11576 get_DW_FORM_name (str_form));
11577 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11578 if (str_form == DW_FORM_string)
11580 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11581 for (i = 1 - idx_offset; i < ndirs; i++)
11582 dw2_asm_output_nstring (dirs[i].path,
11583 dirs[i].length
11584 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11585 "Directory Entry: %#x", i + idx_offset);
11587 else
11589 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11590 for (i = 1 - idx_offset; i < ndirs; i++)
11592 const char *str
11593 = ggc_alloc_string (dirs[i].path,
11594 dirs[i].length
11595 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11596 output_line_string (str_form, str, "Directory Entry",
11597 (unsigned) i + idx_offset);
11601 else
11603 for (i = 1 - idx_offset; i < ndirs; i++)
11604 dw2_asm_output_nstring (dirs[i].path,
11605 dirs[i].length
11606 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11607 "Directory Entry: %#x", i + idx_offset);
11609 dw2_asm_output_data (1, 0, "End directory table");
11612 /* We have to emit them in the order of emitted_number since that's
11613 used in the debug info generation. To do this efficiently we
11614 generate a back-mapping of the indices first. */
11615 backmap = XALLOCAVEC (int, numfiles);
11616 for (i = 0; i < numfiles; i++)
11617 backmap[files[i].file_idx->emitted_number - 1] = i;
11619 if (dwarf_version >= 5)
11621 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11622 if (filename0 == NULL)
11623 filename0 = "";
11624 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11625 DW_FORM_data2. Choose one based on the number of directories
11626 and how much space would they occupy in each encoding.
11627 If we have at most 256 directories, all indexes fit into
11628 a single byte, so DW_FORM_data1 is most compact (if there
11629 are at most 128 directories, DW_FORM_udata would be as
11630 compact as that, but not shorter and slower to decode). */
11631 if (ndirs + idx_offset <= 256)
11632 idx_form = DW_FORM_data1;
11633 /* If there are more than 65536 directories, we have to use
11634 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11635 Otherwise, compute what space would occupy if all the indexes
11636 used DW_FORM_udata - sum - and compare that to how large would
11637 be DW_FORM_data2 encoding, and pick the more efficient one. */
11638 else if (ndirs + idx_offset <= 65536)
11640 unsigned HOST_WIDE_INT sum = 1;
11641 for (i = 0; i < numfiles; i++)
11643 int file_idx = backmap[i];
11644 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11645 sum += size_of_uleb128 (dir_idx);
11647 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11648 idx_form = DW_FORM_data2;
11650 #ifdef VMS_DEBUGGING_INFO
11651 dw2_asm_output_data (1, 4, "File name entry format count");
11652 #else
11653 dw2_asm_output_data (1, 2, "File name entry format count");
11654 #endif
11655 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11656 dw2_asm_output_data_uleb128 (str_form, "%s",
11657 get_DW_FORM_name (str_form));
11658 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11659 "DW_LNCT_directory_index");
11660 dw2_asm_output_data_uleb128 (idx_form, "%s",
11661 get_DW_FORM_name (idx_form));
11662 #ifdef VMS_DEBUGGING_INFO
11663 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11664 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11665 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11666 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11667 #endif
11668 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11670 output_line_string (str_form, filename0, "File Entry", 0);
11672 /* Include directory index. */
11673 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11674 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11675 0, NULL);
11676 else
11677 dw2_asm_output_data_uleb128 (0, NULL);
11679 #ifdef VMS_DEBUGGING_INFO
11680 dw2_asm_output_data_uleb128 (0, NULL);
11681 dw2_asm_output_data_uleb128 (0, NULL);
11682 #endif
11685 /* Now write all the file names. */
11686 for (i = 0; i < numfiles; i++)
11688 int file_idx = backmap[i];
11689 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11691 #ifdef VMS_DEBUGGING_INFO
11692 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11694 /* Setting these fields can lead to debugger miscomparisons,
11695 but VMS Debug requires them to be set correctly. */
11697 int ver;
11698 long long cdt;
11699 long siz;
11700 int maxfilelen = (strlen (files[file_idx].path)
11701 + dirs[dir_idx].length
11702 + MAX_VMS_VERSION_LEN + 1);
11703 char *filebuf = XALLOCAVEC (char, maxfilelen);
11705 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11706 snprintf (filebuf, maxfilelen, "%s;%d",
11707 files[file_idx].path + dirs[dir_idx].length, ver);
11709 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11711 /* Include directory index. */
11712 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11713 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11714 dir_idx + idx_offset, NULL);
11715 else
11716 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11718 /* Modification time. */
11719 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11720 &cdt, 0, 0, 0) == 0)
11721 ? cdt : 0, NULL);
11723 /* File length in bytes. */
11724 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11725 0, &siz, 0, 0) == 0)
11726 ? siz : 0, NULL);
11727 #else
11728 output_line_string (str_form,
11729 files[file_idx].path + dirs[dir_idx].length,
11730 "File Entry", (unsigned) i + 1);
11732 /* Include directory index. */
11733 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11734 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11735 dir_idx + idx_offset, NULL);
11736 else
11737 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11739 if (dwarf_version >= 5)
11740 continue;
11742 /* Modification time. */
11743 dw2_asm_output_data_uleb128 (0, NULL);
11745 /* File length in bytes. */
11746 dw2_asm_output_data_uleb128 (0, NULL);
11747 #endif /* VMS_DEBUGGING_INFO */
11750 if (dwarf_version < 5)
11751 dw2_asm_output_data (1, 0, "End file name table");
11755 /* Output one line number table into the .debug_line section. */
11757 static void
11758 output_one_line_info_table (dw_line_info_table *table)
11760 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11761 unsigned int current_line = 1;
11762 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11763 dw_line_info_entry *ent;
11764 size_t i;
11766 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11768 switch (ent->opcode)
11770 case LI_set_address:
11771 /* ??? Unfortunately, we have little choice here currently, and
11772 must always use the most general form. GCC does not know the
11773 address delta itself, so we can't use DW_LNS_advance_pc. Many
11774 ports do have length attributes which will give an upper bound
11775 on the address range. We could perhaps use length attributes
11776 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11777 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11779 /* This can handle any delta. This takes
11780 4+DWARF2_ADDR_SIZE bytes. */
11781 dw2_asm_output_data (1, 0, "set address %s", line_label);
11782 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11783 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11784 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11785 break;
11787 case LI_set_line:
11788 if (ent->val == current_line)
11790 /* We still need to start a new row, so output a copy insn. */
11791 dw2_asm_output_data (1, DW_LNS_copy,
11792 "copy line %u", current_line);
11794 else
11796 int line_offset = ent->val - current_line;
11797 int line_delta = line_offset - DWARF_LINE_BASE;
11799 current_line = ent->val;
11800 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11802 /* This can handle deltas from -10 to 234, using the current
11803 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11804 This takes 1 byte. */
11805 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11806 "line %u", current_line);
11808 else
11810 /* This can handle any delta. This takes at least 4 bytes,
11811 depending on the value being encoded. */
11812 dw2_asm_output_data (1, DW_LNS_advance_line,
11813 "advance to line %u", current_line);
11814 dw2_asm_output_data_sleb128 (line_offset, NULL);
11815 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11818 break;
11820 case LI_set_file:
11821 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11822 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11823 break;
11825 case LI_set_column:
11826 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11827 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11828 break;
11830 case LI_negate_stmt:
11831 current_is_stmt = !current_is_stmt;
11832 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11833 "is_stmt %d", current_is_stmt);
11834 break;
11836 case LI_set_prologue_end:
11837 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11838 "set prologue end");
11839 break;
11841 case LI_set_epilogue_begin:
11842 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11843 "set epilogue begin");
11844 break;
11846 case LI_set_discriminator:
11847 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11848 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11849 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11850 dw2_asm_output_data_uleb128 (ent->val, NULL);
11851 break;
11855 /* Emit debug info for the address of the end of the table. */
11856 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11857 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11858 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11859 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11861 dw2_asm_output_data (1, 0, "end sequence");
11862 dw2_asm_output_data_uleb128 (1, NULL);
11863 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11866 /* Output the source line number correspondence information. This
11867 information goes into the .debug_line section. */
11869 static void
11870 output_line_info (bool prologue_only)
11872 static unsigned int generation;
11873 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11874 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11875 bool saw_one = false;
11876 int opc;
11878 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11879 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11880 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11881 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11883 if (!XCOFF_DEBUGGING_INFO)
11885 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11886 dw2_asm_output_data (4, 0xffffffff,
11887 "Initial length escape value indicating 64-bit DWARF extension");
11888 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11889 "Length of Source Line Info");
11892 ASM_OUTPUT_LABEL (asm_out_file, l1);
11894 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11895 if (dwarf_version >= 5)
11897 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11898 dw2_asm_output_data (1, 0, "Segment Size");
11900 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11901 ASM_OUTPUT_LABEL (asm_out_file, p1);
11903 /* Define the architecture-dependent minimum instruction length (in bytes).
11904 In this implementation of DWARF, this field is used for information
11905 purposes only. Since GCC generates assembly language, we have no
11906 a priori knowledge of how many instruction bytes are generated for each
11907 source line, and therefore can use only the DW_LNE_set_address and
11908 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11909 this as '1', which is "correct enough" for all architectures,
11910 and don't let the target override. */
11911 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11913 if (dwarf_version >= 4)
11914 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11915 "Maximum Operations Per Instruction");
11916 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11917 "Default is_stmt_start flag");
11918 dw2_asm_output_data (1, DWARF_LINE_BASE,
11919 "Line Base Value (Special Opcodes)");
11920 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11921 "Line Range Value (Special Opcodes)");
11922 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11923 "Special Opcode Base");
11925 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11927 int n_op_args;
11928 switch (opc)
11930 case DW_LNS_advance_pc:
11931 case DW_LNS_advance_line:
11932 case DW_LNS_set_file:
11933 case DW_LNS_set_column:
11934 case DW_LNS_fixed_advance_pc:
11935 case DW_LNS_set_isa:
11936 n_op_args = 1;
11937 break;
11938 default:
11939 n_op_args = 0;
11940 break;
11943 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11944 opc, n_op_args);
11947 /* Write out the information about the files we use. */
11948 output_file_names ();
11949 ASM_OUTPUT_LABEL (asm_out_file, p2);
11950 if (prologue_only)
11952 /* Output the marker for the end of the line number info. */
11953 ASM_OUTPUT_LABEL (asm_out_file, l2);
11954 return;
11957 if (separate_line_info)
11959 dw_line_info_table *table;
11960 size_t i;
11962 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11963 if (table->in_use)
11965 output_one_line_info_table (table);
11966 saw_one = true;
11969 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11971 output_one_line_info_table (cold_text_section_line_info);
11972 saw_one = true;
11975 /* ??? Some Darwin linkers crash on a .debug_line section with no
11976 sequences. Further, merely a DW_LNE_end_sequence entry is not
11977 sufficient -- the address column must also be initialized.
11978 Make sure to output at least one set_address/end_sequence pair,
11979 choosing .text since that section is always present. */
11980 if (text_section_line_info->in_use || !saw_one)
11981 output_one_line_info_table (text_section_line_info);
11983 /* Output the marker for the end of the line number info. */
11984 ASM_OUTPUT_LABEL (asm_out_file, l2);
11987 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
11989 static inline bool
11990 need_endianity_attribute_p (bool reverse)
11992 return reverse && (dwarf_version >= 3 || !dwarf_strict);
11995 /* Given a pointer to a tree node for some base type, return a pointer to
11996 a DIE that describes the given type. REVERSE is true if the type is
11997 to be interpreted in the reverse storage order wrt the target order.
11999 This routine must only be called for GCC type nodes that correspond to
12000 Dwarf base (fundamental) types. */
12002 static dw_die_ref
12003 base_type_die (tree type, bool reverse)
12005 dw_die_ref base_type_result;
12006 enum dwarf_type encoding;
12007 bool fpt_used = false;
12008 struct fixed_point_type_info fpt_info;
12009 tree type_bias = NULL_TREE;
12011 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12012 return 0;
12014 /* If this is a subtype that should not be emitted as a subrange type,
12015 use the base type. See subrange_type_for_debug_p. */
12016 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12017 type = TREE_TYPE (type);
12019 switch (TREE_CODE (type))
12021 case INTEGER_TYPE:
12022 if ((dwarf_version >= 4 || !dwarf_strict)
12023 && TYPE_NAME (type)
12024 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12025 && DECL_IS_BUILTIN (TYPE_NAME (type))
12026 && DECL_NAME (TYPE_NAME (type)))
12028 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12029 if (strcmp (name, "char16_t") == 0
12030 || strcmp (name, "char32_t") == 0)
12032 encoding = DW_ATE_UTF;
12033 break;
12036 if ((dwarf_version >= 3 || !dwarf_strict)
12037 && lang_hooks.types.get_fixed_point_type_info)
12039 memset (&fpt_info, 0, sizeof (fpt_info));
12040 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12042 fpt_used = true;
12043 encoding = ((TYPE_UNSIGNED (type))
12044 ? DW_ATE_unsigned_fixed
12045 : DW_ATE_signed_fixed);
12046 break;
12049 if (TYPE_STRING_FLAG (type))
12051 if (TYPE_UNSIGNED (type))
12052 encoding = DW_ATE_unsigned_char;
12053 else
12054 encoding = DW_ATE_signed_char;
12056 else if (TYPE_UNSIGNED (type))
12057 encoding = DW_ATE_unsigned;
12058 else
12059 encoding = DW_ATE_signed;
12061 if (!dwarf_strict
12062 && lang_hooks.types.get_type_bias)
12063 type_bias = lang_hooks.types.get_type_bias (type);
12064 break;
12066 case REAL_TYPE:
12067 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12069 if (dwarf_version >= 3 || !dwarf_strict)
12070 encoding = DW_ATE_decimal_float;
12071 else
12072 encoding = DW_ATE_lo_user;
12074 else
12075 encoding = DW_ATE_float;
12076 break;
12078 case FIXED_POINT_TYPE:
12079 if (!(dwarf_version >= 3 || !dwarf_strict))
12080 encoding = DW_ATE_lo_user;
12081 else if (TYPE_UNSIGNED (type))
12082 encoding = DW_ATE_unsigned_fixed;
12083 else
12084 encoding = DW_ATE_signed_fixed;
12085 break;
12087 /* Dwarf2 doesn't know anything about complex ints, so use
12088 a user defined type for it. */
12089 case COMPLEX_TYPE:
12090 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12091 encoding = DW_ATE_complex_float;
12092 else
12093 encoding = DW_ATE_lo_user;
12094 break;
12096 case BOOLEAN_TYPE:
12097 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12098 encoding = DW_ATE_boolean;
12099 break;
12101 default:
12102 /* No other TREE_CODEs are Dwarf fundamental types. */
12103 gcc_unreachable ();
12106 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12108 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12109 int_size_in_bytes (type));
12110 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12112 if (need_endianity_attribute_p (reverse))
12113 add_AT_unsigned (base_type_result, DW_AT_endianity,
12114 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12116 add_alignment_attribute (base_type_result, type);
12118 if (fpt_used)
12120 switch (fpt_info.scale_factor_kind)
12122 case fixed_point_scale_factor_binary:
12123 add_AT_int (base_type_result, DW_AT_binary_scale,
12124 fpt_info.scale_factor.binary);
12125 break;
12127 case fixed_point_scale_factor_decimal:
12128 add_AT_int (base_type_result, DW_AT_decimal_scale,
12129 fpt_info.scale_factor.decimal);
12130 break;
12132 case fixed_point_scale_factor_arbitrary:
12133 /* Arbitrary scale factors cannot be described in standard DWARF,
12134 yet. */
12135 if (!dwarf_strict)
12137 /* Describe the scale factor as a rational constant. */
12138 const dw_die_ref scale_factor
12139 = new_die (DW_TAG_constant, comp_unit_die (), type);
12141 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12142 fpt_info.scale_factor.arbitrary.numerator);
12143 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12144 fpt_info.scale_factor.arbitrary.denominator);
12146 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12148 break;
12150 default:
12151 gcc_unreachable ();
12155 if (type_bias)
12156 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12157 dw_scalar_form_constant
12158 | dw_scalar_form_exprloc
12159 | dw_scalar_form_reference,
12160 NULL);
12162 add_pubtype (type, base_type_result);
12164 return base_type_result;
12167 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12168 named 'auto' in its type: return true for it, false otherwise. */
12170 static inline bool
12171 is_cxx_auto (tree type)
12173 if (is_cxx ())
12175 tree name = TYPE_IDENTIFIER (type);
12176 if (name == get_identifier ("auto")
12177 || name == get_identifier ("decltype(auto)"))
12178 return true;
12180 return false;
12183 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12184 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12186 static inline int
12187 is_base_type (tree type)
12189 switch (TREE_CODE (type))
12191 case ERROR_MARK:
12192 case VOID_TYPE:
12193 case INTEGER_TYPE:
12194 case REAL_TYPE:
12195 case FIXED_POINT_TYPE:
12196 case COMPLEX_TYPE:
12197 case BOOLEAN_TYPE:
12198 case POINTER_BOUNDS_TYPE:
12199 return 1;
12201 case ARRAY_TYPE:
12202 case RECORD_TYPE:
12203 case UNION_TYPE:
12204 case QUAL_UNION_TYPE:
12205 case ENUMERAL_TYPE:
12206 case FUNCTION_TYPE:
12207 case METHOD_TYPE:
12208 case POINTER_TYPE:
12209 case REFERENCE_TYPE:
12210 case NULLPTR_TYPE:
12211 case OFFSET_TYPE:
12212 case LANG_TYPE:
12213 case VECTOR_TYPE:
12214 return 0;
12216 default:
12217 if (is_cxx_auto (type))
12218 return 0;
12219 gcc_unreachable ();
12222 return 0;
12225 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12226 node, return the size in bits for the type if it is a constant, or else
12227 return the alignment for the type if the type's size is not constant, or
12228 else return BITS_PER_WORD if the type actually turns out to be an
12229 ERROR_MARK node. */
12231 static inline unsigned HOST_WIDE_INT
12232 simple_type_size_in_bits (const_tree type)
12234 if (TREE_CODE (type) == ERROR_MARK)
12235 return BITS_PER_WORD;
12236 else if (TYPE_SIZE (type) == NULL_TREE)
12237 return 0;
12238 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12239 return tree_to_uhwi (TYPE_SIZE (type));
12240 else
12241 return TYPE_ALIGN (type);
12244 /* Similarly, but return an offset_int instead of UHWI. */
12246 static inline offset_int
12247 offset_int_type_size_in_bits (const_tree type)
12249 if (TREE_CODE (type) == ERROR_MARK)
12250 return BITS_PER_WORD;
12251 else if (TYPE_SIZE (type) == NULL_TREE)
12252 return 0;
12253 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12254 return wi::to_offset (TYPE_SIZE (type));
12255 else
12256 return TYPE_ALIGN (type);
12259 /* Given a pointer to a tree node for a subrange type, return a pointer
12260 to a DIE that describes the given type. */
12262 static dw_die_ref
12263 subrange_type_die (tree type, tree low, tree high, tree bias,
12264 dw_die_ref context_die)
12266 dw_die_ref subrange_die;
12267 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12269 if (context_die == NULL)
12270 context_die = comp_unit_die ();
12272 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12274 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12276 /* The size of the subrange type and its base type do not match,
12277 so we need to generate a size attribute for the subrange type. */
12278 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12281 add_alignment_attribute (subrange_die, type);
12283 if (low)
12284 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12285 if (high)
12286 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12287 if (bias && !dwarf_strict)
12288 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12289 dw_scalar_form_constant
12290 | dw_scalar_form_exprloc
12291 | dw_scalar_form_reference,
12292 NULL);
12294 return subrange_die;
12297 /* Returns the (const and/or volatile) cv_qualifiers associated with
12298 the decl node. This will normally be augmented with the
12299 cv_qualifiers of the underlying type in add_type_attribute. */
12301 static int
12302 decl_quals (const_tree decl)
12304 return ((TREE_READONLY (decl)
12305 /* The C++ front-end correctly marks reference-typed
12306 variables as readonly, but from a language (and debug
12307 info) standpoint they are not const-qualified. */
12308 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12309 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12310 | (TREE_THIS_VOLATILE (decl)
12311 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12314 /* Determine the TYPE whose qualifiers match the largest strict subset
12315 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12316 qualifiers outside QUAL_MASK. */
12318 static int
12319 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12321 tree t;
12322 int best_rank = 0, best_qual = 0, max_rank;
12324 type_quals &= qual_mask;
12325 max_rank = popcount_hwi (type_quals) - 1;
12327 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12328 t = TYPE_NEXT_VARIANT (t))
12330 int q = TYPE_QUALS (t) & qual_mask;
12332 if ((q & type_quals) == q && q != type_quals
12333 && check_base_type (t, type))
12335 int rank = popcount_hwi (q);
12337 if (rank > best_rank)
12339 best_rank = rank;
12340 best_qual = q;
12345 return best_qual;
12348 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12349 static const dwarf_qual_info_t dwarf_qual_info[] =
12351 { TYPE_QUAL_CONST, DW_TAG_const_type },
12352 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12353 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12354 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12356 static const unsigned int dwarf_qual_info_size
12357 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12359 /* If DIE is a qualified DIE of some base DIE with the same parent,
12360 return the base DIE, otherwise return NULL. Set MASK to the
12361 qualifiers added compared to the returned DIE. */
12363 static dw_die_ref
12364 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12366 unsigned int i;
12367 for (i = 0; i < dwarf_qual_info_size; i++)
12368 if (die->die_tag == dwarf_qual_info[i].t)
12369 break;
12370 if (i == dwarf_qual_info_size)
12371 return NULL;
12372 if (vec_safe_length (die->die_attr) != 1)
12373 return NULL;
12374 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12375 if (type == NULL || type->die_parent != die->die_parent)
12376 return NULL;
12377 *mask |= dwarf_qual_info[i].q;
12378 if (depth)
12380 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12381 if (ret)
12382 return ret;
12384 return type;
12387 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12388 entry that chains the modifiers specified by CV_QUALS in front of the
12389 given type. REVERSE is true if the type is to be interpreted in the
12390 reverse storage order wrt the target order. */
12392 static dw_die_ref
12393 modified_type_die (tree type, int cv_quals, bool reverse,
12394 dw_die_ref context_die)
12396 enum tree_code code = TREE_CODE (type);
12397 dw_die_ref mod_type_die;
12398 dw_die_ref sub_die = NULL;
12399 tree item_type = NULL;
12400 tree qualified_type;
12401 tree name, low, high;
12402 dw_die_ref mod_scope;
12403 /* Only these cv-qualifiers are currently handled. */
12404 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12405 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12407 if (code == ERROR_MARK)
12408 return NULL;
12410 if (lang_hooks.types.get_debug_type)
12412 tree debug_type = lang_hooks.types.get_debug_type (type);
12414 if (debug_type != NULL_TREE && debug_type != type)
12415 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12418 cv_quals &= cv_qual_mask;
12420 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12421 tag modifier (and not an attribute) old consumers won't be able
12422 to handle it. */
12423 if (dwarf_version < 3)
12424 cv_quals &= ~TYPE_QUAL_RESTRICT;
12426 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12427 if (dwarf_version < 5)
12428 cv_quals &= ~TYPE_QUAL_ATOMIC;
12430 /* See if we already have the appropriately qualified variant of
12431 this type. */
12432 qualified_type = get_qualified_type (type, cv_quals);
12434 if (qualified_type == sizetype
12435 && TYPE_NAME (qualified_type)
12436 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12438 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12440 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12441 && TYPE_PRECISION (t)
12442 == TYPE_PRECISION (qualified_type)
12443 && TYPE_UNSIGNED (t)
12444 == TYPE_UNSIGNED (qualified_type));
12445 qualified_type = t;
12448 /* If we do, then we can just use its DIE, if it exists. */
12449 if (qualified_type)
12451 mod_type_die = lookup_type_die (qualified_type);
12453 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12454 if (mod_type_die
12455 && (!need_endianity_attribute_p (reverse)
12456 || !is_base_type (type)
12457 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12458 return mod_type_die;
12461 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12463 /* Handle C typedef types. */
12464 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12465 && !DECL_ARTIFICIAL (name))
12467 tree dtype = TREE_TYPE (name);
12469 if (qualified_type == dtype)
12471 /* For a named type, use the typedef. */
12472 gen_type_die (qualified_type, context_die);
12473 return lookup_type_die (qualified_type);
12475 else
12477 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12478 dquals &= cv_qual_mask;
12479 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12480 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12481 /* cv-unqualified version of named type. Just use
12482 the unnamed type to which it refers. */
12483 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12484 reverse, context_die);
12485 /* Else cv-qualified version of named type; fall through. */
12489 mod_scope = scope_die_for (type, context_die);
12491 if (cv_quals)
12493 int sub_quals = 0, first_quals = 0;
12494 unsigned i;
12495 dw_die_ref first = NULL, last = NULL;
12497 /* Determine a lesser qualified type that most closely matches
12498 this one. Then generate DW_TAG_* entries for the remaining
12499 qualifiers. */
12500 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12501 cv_qual_mask);
12502 if (sub_quals && use_debug_types)
12504 bool needed = false;
12505 /* If emitting type units, make sure the order of qualifiers
12506 is canonical. Thus, start from unqualified type if
12507 an earlier qualifier is missing in sub_quals, but some later
12508 one is present there. */
12509 for (i = 0; i < dwarf_qual_info_size; i++)
12510 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12511 needed = true;
12512 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12514 sub_quals = 0;
12515 break;
12518 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12519 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12521 /* As not all intermediate qualified DIEs have corresponding
12522 tree types, ensure that qualified DIEs in the same scope
12523 as their DW_AT_type are emitted after their DW_AT_type,
12524 only with other qualified DIEs for the same type possibly
12525 in between them. Determine the range of such qualified
12526 DIEs now (first being the base type, last being corresponding
12527 last qualified DIE for it). */
12528 unsigned int count = 0;
12529 first = qualified_die_p (mod_type_die, &first_quals,
12530 dwarf_qual_info_size);
12531 if (first == NULL)
12532 first = mod_type_die;
12533 gcc_assert ((first_quals & ~sub_quals) == 0);
12534 for (count = 0, last = first;
12535 count < (1U << dwarf_qual_info_size);
12536 count++, last = last->die_sib)
12538 int quals = 0;
12539 if (last == mod_scope->die_child)
12540 break;
12541 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12542 != first)
12543 break;
12547 for (i = 0; i < dwarf_qual_info_size; i++)
12548 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12550 dw_die_ref d;
12551 if (first && first != last)
12553 for (d = first->die_sib; ; d = d->die_sib)
12555 int quals = 0;
12556 qualified_die_p (d, &quals, dwarf_qual_info_size);
12557 if (quals == (first_quals | dwarf_qual_info[i].q))
12558 break;
12559 if (d == last)
12561 d = NULL;
12562 break;
12565 if (d)
12567 mod_type_die = d;
12568 continue;
12571 if (first)
12573 d = ggc_cleared_alloc<die_node> ();
12574 d->die_tag = dwarf_qual_info[i].t;
12575 add_child_die_after (mod_scope, d, last);
12576 last = d;
12578 else
12579 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12580 if (mod_type_die)
12581 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12582 mod_type_die = d;
12583 first_quals |= dwarf_qual_info[i].q;
12586 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12588 dwarf_tag tag = DW_TAG_pointer_type;
12589 if (code == REFERENCE_TYPE)
12591 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12592 tag = DW_TAG_rvalue_reference_type;
12593 else
12594 tag = DW_TAG_reference_type;
12596 mod_type_die = new_die (tag, mod_scope, type);
12598 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12599 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12600 add_alignment_attribute (mod_type_die, type);
12601 item_type = TREE_TYPE (type);
12603 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12604 if (!ADDR_SPACE_GENERIC_P (as))
12606 int action = targetm.addr_space.debug (as);
12607 if (action >= 0)
12609 /* Positive values indicate an address_class. */
12610 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12612 else
12614 /* Negative values indicate an (inverted) segment base reg. */
12615 dw_loc_descr_ref d
12616 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12617 add_AT_loc (mod_type_die, DW_AT_segment, d);
12621 else if (code == INTEGER_TYPE
12622 && TREE_TYPE (type) != NULL_TREE
12623 && subrange_type_for_debug_p (type, &low, &high))
12625 tree bias = NULL_TREE;
12626 if (lang_hooks.types.get_type_bias)
12627 bias = lang_hooks.types.get_type_bias (type);
12628 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12629 item_type = TREE_TYPE (type);
12631 else if (is_base_type (type))
12632 mod_type_die = base_type_die (type, reverse);
12633 else
12635 gen_type_die (type, context_die);
12637 /* We have to get the type_main_variant here (and pass that to the
12638 `lookup_type_die' routine) because the ..._TYPE node we have
12639 might simply be a *copy* of some original type node (where the
12640 copy was created to help us keep track of typedef names) and
12641 that copy might have a different TYPE_UID from the original
12642 ..._TYPE node. */
12643 if (TREE_CODE (type) == FUNCTION_TYPE
12644 || TREE_CODE (type) == METHOD_TYPE)
12646 /* For function/method types, can't just use type_main_variant here,
12647 because that can have different ref-qualifiers for C++,
12648 but try to canonicalize. */
12649 tree main = TYPE_MAIN_VARIANT (type);
12650 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12651 if (check_base_type (t, main) && check_lang_type (t, type))
12652 return lookup_type_die (t);
12653 return lookup_type_die (type);
12655 else if (TREE_CODE (type) != VECTOR_TYPE
12656 && TREE_CODE (type) != ARRAY_TYPE)
12657 return lookup_type_die (type_main_variant (type));
12658 else
12659 /* Vectors have the debugging information in the type,
12660 not the main variant. */
12661 return lookup_type_die (type);
12664 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12665 don't output a DW_TAG_typedef, since there isn't one in the
12666 user's program; just attach a DW_AT_name to the type.
12667 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12668 if the base type already has the same name. */
12669 if (name
12670 && ((TREE_CODE (name) != TYPE_DECL
12671 && (qualified_type == TYPE_MAIN_VARIANT (type)
12672 || (cv_quals == TYPE_UNQUALIFIED)))
12673 || (TREE_CODE (name) == TYPE_DECL
12674 && TREE_TYPE (name) == qualified_type
12675 && DECL_NAME (name))))
12677 if (TREE_CODE (name) == TYPE_DECL)
12678 /* Could just call add_name_and_src_coords_attributes here,
12679 but since this is a builtin type it doesn't have any
12680 useful source coordinates anyway. */
12681 name = DECL_NAME (name);
12682 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12684 /* This probably indicates a bug. */
12685 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12687 name = TYPE_IDENTIFIER (type);
12688 add_name_attribute (mod_type_die,
12689 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12692 if (qualified_type)
12693 equate_type_number_to_die (qualified_type, mod_type_die);
12695 if (item_type)
12696 /* We must do this after the equate_type_number_to_die call, in case
12697 this is a recursive type. This ensures that the modified_type_die
12698 recursion will terminate even if the type is recursive. Recursive
12699 types are possible in Ada. */
12700 sub_die = modified_type_die (item_type,
12701 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12702 reverse,
12703 context_die);
12705 if (sub_die != NULL)
12706 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12708 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12709 if (TYPE_ARTIFICIAL (type))
12710 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12712 return mod_type_die;
12715 /* Generate DIEs for the generic parameters of T.
12716 T must be either a generic type or a generic function.
12717 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12719 static void
12720 gen_generic_params_dies (tree t)
12722 tree parms, args;
12723 int parms_num, i;
12724 dw_die_ref die = NULL;
12725 int non_default;
12727 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12728 return;
12730 if (TYPE_P (t))
12731 die = lookup_type_die (t);
12732 else if (DECL_P (t))
12733 die = lookup_decl_die (t);
12735 gcc_assert (die);
12737 parms = lang_hooks.get_innermost_generic_parms (t);
12738 if (!parms)
12739 /* T has no generic parameter. It means T is neither a generic type
12740 or function. End of story. */
12741 return;
12743 parms_num = TREE_VEC_LENGTH (parms);
12744 args = lang_hooks.get_innermost_generic_args (t);
12745 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12746 non_default = int_cst_value (TREE_CHAIN (args));
12747 else
12748 non_default = TREE_VEC_LENGTH (args);
12749 for (i = 0; i < parms_num; i++)
12751 tree parm, arg, arg_pack_elems;
12752 dw_die_ref parm_die;
12754 parm = TREE_VEC_ELT (parms, i);
12755 arg = TREE_VEC_ELT (args, i);
12756 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12757 gcc_assert (parm && TREE_VALUE (parm) && arg);
12759 if (parm && TREE_VALUE (parm) && arg)
12761 /* If PARM represents a template parameter pack,
12762 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12763 by DW_TAG_template_*_parameter DIEs for the argument
12764 pack elements of ARG. Note that ARG would then be
12765 an argument pack. */
12766 if (arg_pack_elems)
12767 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12768 arg_pack_elems,
12769 die);
12770 else
12771 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12772 true /* emit name */, die);
12773 if (i >= non_default)
12774 add_AT_flag (parm_die, DW_AT_default_value, 1);
12779 /* Create and return a DIE for PARM which should be
12780 the representation of a generic type parameter.
12781 For instance, in the C++ front end, PARM would be a template parameter.
12782 ARG is the argument to PARM.
12783 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12784 name of the PARM.
12785 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12786 as a child node. */
12788 static dw_die_ref
12789 generic_parameter_die (tree parm, tree arg,
12790 bool emit_name_p,
12791 dw_die_ref parent_die)
12793 dw_die_ref tmpl_die = NULL;
12794 const char *name = NULL;
12796 if (!parm || !DECL_NAME (parm) || !arg)
12797 return NULL;
12799 /* We support non-type generic parameters and arguments,
12800 type generic parameters and arguments, as well as
12801 generic generic parameters (a.k.a. template template parameters in C++)
12802 and arguments. */
12803 if (TREE_CODE (parm) == PARM_DECL)
12804 /* PARM is a nontype generic parameter */
12805 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12806 else if (TREE_CODE (parm) == TYPE_DECL)
12807 /* PARM is a type generic parameter. */
12808 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12809 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12810 /* PARM is a generic generic parameter.
12811 Its DIE is a GNU extension. It shall have a
12812 DW_AT_name attribute to represent the name of the template template
12813 parameter, and a DW_AT_GNU_template_name attribute to represent the
12814 name of the template template argument. */
12815 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12816 parent_die, parm);
12817 else
12818 gcc_unreachable ();
12820 if (tmpl_die)
12822 tree tmpl_type;
12824 /* If PARM is a generic parameter pack, it means we are
12825 emitting debug info for a template argument pack element.
12826 In other terms, ARG is a template argument pack element.
12827 In that case, we don't emit any DW_AT_name attribute for
12828 the die. */
12829 if (emit_name_p)
12831 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12832 gcc_assert (name);
12833 add_AT_string (tmpl_die, DW_AT_name, name);
12836 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12838 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12839 TMPL_DIE should have a child DW_AT_type attribute that is set
12840 to the type of the argument to PARM, which is ARG.
12841 If PARM is a type generic parameter, TMPL_DIE should have a
12842 child DW_AT_type that is set to ARG. */
12843 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12844 add_type_attribute (tmpl_die, tmpl_type,
12845 (TREE_THIS_VOLATILE (tmpl_type)
12846 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12847 false, parent_die);
12849 else
12851 /* So TMPL_DIE is a DIE representing a
12852 a generic generic template parameter, a.k.a template template
12853 parameter in C++ and arg is a template. */
12855 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12856 to the name of the argument. */
12857 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12858 if (name)
12859 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12862 if (TREE_CODE (parm) == PARM_DECL)
12863 /* So PARM is a non-type generic parameter.
12864 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12865 attribute of TMPL_DIE which value represents the value
12866 of ARG.
12867 We must be careful here:
12868 The value of ARG might reference some function decls.
12869 We might currently be emitting debug info for a generic
12870 type and types are emitted before function decls, we don't
12871 know if the function decls referenced by ARG will actually be
12872 emitted after cgraph computations.
12873 So must defer the generation of the DW_AT_const_value to
12874 after cgraph is ready. */
12875 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12878 return tmpl_die;
12881 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12882 PARM_PACK must be a template parameter pack. The returned DIE
12883 will be child DIE of PARENT_DIE. */
12885 static dw_die_ref
12886 template_parameter_pack_die (tree parm_pack,
12887 tree parm_pack_args,
12888 dw_die_ref parent_die)
12890 dw_die_ref die;
12891 int j;
12893 gcc_assert (parent_die && parm_pack);
12895 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12896 add_name_and_src_coords_attributes (die, parm_pack);
12897 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12898 generic_parameter_die (parm_pack,
12899 TREE_VEC_ELT (parm_pack_args, j),
12900 false /* Don't emit DW_AT_name */,
12901 die);
12902 return die;
12905 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12906 an enumerated type. */
12908 static inline int
12909 type_is_enum (const_tree type)
12911 return TREE_CODE (type) == ENUMERAL_TYPE;
12914 /* Return the DBX register number described by a given RTL node. */
12916 static unsigned int
12917 dbx_reg_number (const_rtx rtl)
12919 unsigned regno = REGNO (rtl);
12921 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12923 #ifdef LEAF_REG_REMAP
12924 if (crtl->uses_only_leaf_regs)
12926 int leaf_reg = LEAF_REG_REMAP (regno);
12927 if (leaf_reg != -1)
12928 regno = (unsigned) leaf_reg;
12930 #endif
12932 regno = DBX_REGISTER_NUMBER (regno);
12933 gcc_assert (regno != INVALID_REGNUM);
12934 return regno;
12937 /* Optionally add a DW_OP_piece term to a location description expression.
12938 DW_OP_piece is only added if the location description expression already
12939 doesn't end with DW_OP_piece. */
12941 static void
12942 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12944 dw_loc_descr_ref loc;
12946 if (*list_head != NULL)
12948 /* Find the end of the chain. */
12949 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12952 if (loc->dw_loc_opc != DW_OP_piece)
12953 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12957 /* Return a location descriptor that designates a machine register or
12958 zero if there is none. */
12960 static dw_loc_descr_ref
12961 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12963 rtx regs;
12965 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12966 return 0;
12968 /* We only use "frame base" when we're sure we're talking about the
12969 post-prologue local stack frame. We do this by *not* running
12970 register elimination until this point, and recognizing the special
12971 argument pointer and soft frame pointer rtx's.
12972 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
12973 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
12974 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
12976 dw_loc_descr_ref result = NULL;
12978 if (dwarf_version >= 4 || !dwarf_strict)
12980 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
12981 initialized);
12982 if (result)
12983 add_loc_descr (&result,
12984 new_loc_descr (DW_OP_stack_value, 0, 0));
12986 return result;
12989 regs = targetm.dwarf_register_span (rtl);
12991 if (REG_NREGS (rtl) > 1 || regs)
12992 return multiple_reg_loc_descriptor (rtl, regs, initialized);
12993 else
12995 unsigned int dbx_regnum = dbx_reg_number (rtl);
12996 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12997 return 0;
12998 return one_reg_loc_descriptor (dbx_regnum, initialized);
13002 /* Return a location descriptor that designates a machine register for
13003 a given hard register number. */
13005 static dw_loc_descr_ref
13006 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13008 dw_loc_descr_ref reg_loc_descr;
13010 if (regno <= 31)
13011 reg_loc_descr
13012 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13013 else
13014 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13016 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13017 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13019 return reg_loc_descr;
13022 /* Given an RTL of a register, return a location descriptor that
13023 designates a value that spans more than one register. */
13025 static dw_loc_descr_ref
13026 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13027 enum var_init_status initialized)
13029 int size, i;
13030 dw_loc_descr_ref loc_result = NULL;
13032 /* Simple, contiguous registers. */
13033 if (regs == NULL_RTX)
13035 unsigned reg = REGNO (rtl);
13036 int nregs;
13038 #ifdef LEAF_REG_REMAP
13039 if (crtl->uses_only_leaf_regs)
13041 int leaf_reg = LEAF_REG_REMAP (reg);
13042 if (leaf_reg != -1)
13043 reg = (unsigned) leaf_reg;
13045 #endif
13047 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13048 nregs = REG_NREGS (rtl);
13050 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13052 loc_result = NULL;
13053 while (nregs--)
13055 dw_loc_descr_ref t;
13057 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13058 VAR_INIT_STATUS_INITIALIZED);
13059 add_loc_descr (&loc_result, t);
13060 add_loc_descr_op_piece (&loc_result, size);
13061 ++reg;
13063 return loc_result;
13066 /* Now onto stupid register sets in non contiguous locations. */
13068 gcc_assert (GET_CODE (regs) == PARALLEL);
13070 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13071 loc_result = NULL;
13073 for (i = 0; i < XVECLEN (regs, 0); ++i)
13075 dw_loc_descr_ref t;
13077 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13078 VAR_INIT_STATUS_INITIALIZED);
13079 add_loc_descr (&loc_result, t);
13080 add_loc_descr_op_piece (&loc_result, size);
13083 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13084 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13085 return loc_result;
13088 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13090 /* Return a location descriptor that designates a constant i,
13091 as a compound operation from constant (i >> shift), constant shift
13092 and DW_OP_shl. */
13094 static dw_loc_descr_ref
13095 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13097 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13098 add_loc_descr (&ret, int_loc_descriptor (shift));
13099 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13100 return ret;
13103 /* Return a location descriptor that designates a constant. */
13105 static dw_loc_descr_ref
13106 int_loc_descriptor (HOST_WIDE_INT i)
13108 enum dwarf_location_atom op;
13110 /* Pick the smallest representation of a constant, rather than just
13111 defaulting to the LEB encoding. */
13112 if (i >= 0)
13114 int clz = clz_hwi (i);
13115 int ctz = ctz_hwi (i);
13116 if (i <= 31)
13117 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13118 else if (i <= 0xff)
13119 op = DW_OP_const1u;
13120 else if (i <= 0xffff)
13121 op = DW_OP_const2u;
13122 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13123 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13124 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13125 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13126 while DW_OP_const4u is 5 bytes. */
13127 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13128 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13129 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13130 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13131 while DW_OP_const4u is 5 bytes. */
13132 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13134 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13135 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13136 <= 4)
13138 /* As i >= 2**31, the double cast above will yield a negative number.
13139 Since wrapping is defined in DWARF expressions we can output big
13140 positive integers as small negative ones, regardless of the size
13141 of host wide ints.
13143 Here, since the evaluator will handle 32-bit values and since i >=
13144 2**31, we know it's going to be interpreted as a negative literal:
13145 store it this way if we can do better than 5 bytes this way. */
13146 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13148 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13149 op = DW_OP_const4u;
13151 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13152 least 6 bytes: see if we can do better before falling back to it. */
13153 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13154 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13155 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13156 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13157 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13158 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13159 >= HOST_BITS_PER_WIDE_INT)
13160 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13161 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13162 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13163 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13164 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13165 && size_of_uleb128 (i) > 6)
13166 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13167 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13168 else
13169 op = DW_OP_constu;
13171 else
13173 if (i >= -0x80)
13174 op = DW_OP_const1s;
13175 else if (i >= -0x8000)
13176 op = DW_OP_const2s;
13177 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13179 if (size_of_int_loc_descriptor (i) < 5)
13181 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13182 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13183 return ret;
13185 op = DW_OP_const4s;
13187 else
13189 if (size_of_int_loc_descriptor (i)
13190 < (unsigned long) 1 + size_of_sleb128 (i))
13192 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13193 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13194 return ret;
13196 op = DW_OP_consts;
13200 return new_loc_descr (op, i, 0);
13203 /* Likewise, for unsigned constants. */
13205 static dw_loc_descr_ref
13206 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13208 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13209 const unsigned HOST_WIDE_INT max_uint
13210 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13212 /* If possible, use the clever signed constants handling. */
13213 if (i <= max_int)
13214 return int_loc_descriptor ((HOST_WIDE_INT) i);
13216 /* Here, we are left with positive numbers that cannot be represented as
13217 HOST_WIDE_INT, i.e.:
13218 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13220 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13221 whereas may be better to output a negative integer: thanks to integer
13222 wrapping, we know that:
13223 x = x - 2 ** DWARF2_ADDR_SIZE
13224 = x - 2 * (max (HOST_WIDE_INT) + 1)
13225 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13226 small negative integers. Let's try that in cases it will clearly improve
13227 the encoding: there is no gain turning DW_OP_const4u into
13228 DW_OP_const4s. */
13229 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13230 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13231 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13233 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13235 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13236 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13237 const HOST_WIDE_INT second_shift
13238 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13240 /* So we finally have:
13241 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13242 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13243 return int_loc_descriptor (second_shift);
13246 /* Last chance: fallback to a simple constant operation. */
13247 return new_loc_descr
13248 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13249 ? DW_OP_const4u
13250 : DW_OP_const8u,
13251 i, 0);
13254 /* Generate and return a location description that computes the unsigned
13255 comparison of the two stack top entries (a OP b where b is the top-most
13256 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13257 LE_EXPR, GT_EXPR or GE_EXPR. */
13259 static dw_loc_descr_ref
13260 uint_comparison_loc_list (enum tree_code kind)
13262 enum dwarf_location_atom op, flip_op;
13263 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13265 switch (kind)
13267 case LT_EXPR:
13268 op = DW_OP_lt;
13269 break;
13270 case LE_EXPR:
13271 op = DW_OP_le;
13272 break;
13273 case GT_EXPR:
13274 op = DW_OP_gt;
13275 break;
13276 case GE_EXPR:
13277 op = DW_OP_ge;
13278 break;
13279 default:
13280 gcc_unreachable ();
13283 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13284 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13286 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13287 possible to perform unsigned comparisons: we just have to distinguish
13288 three cases:
13290 1. when a and b have the same sign (as signed integers); then we should
13291 return: a OP(signed) b;
13293 2. when a is a negative signed integer while b is a positive one, then a
13294 is a greater unsigned integer than b; likewise when a and b's roles
13295 are flipped.
13297 So first, compare the sign of the two operands. */
13298 ret = new_loc_descr (DW_OP_over, 0, 0);
13299 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13300 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13301 /* If they have different signs (i.e. they have different sign bits), then
13302 the stack top value has now the sign bit set and thus it's smaller than
13303 zero. */
13304 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13305 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13306 add_loc_descr (&ret, bra_node);
13308 /* We are in case 1. At this point, we know both operands have the same
13309 sign, to it's safe to use the built-in signed comparison. */
13310 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13311 add_loc_descr (&ret, jmp_node);
13313 /* We are in case 2. Here, we know both operands do not have the same sign,
13314 so we have to flip the signed comparison. */
13315 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13316 tmp = new_loc_descr (flip_op, 0, 0);
13317 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13318 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13319 add_loc_descr (&ret, tmp);
13321 /* This dummy operation is necessary to make the two branches join. */
13322 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13323 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13324 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13325 add_loc_descr (&ret, tmp);
13327 return ret;
13330 /* Likewise, but takes the location description lists (might be destructive on
13331 them). Return NULL if either is NULL or if concatenation fails. */
13333 static dw_loc_list_ref
13334 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13335 enum tree_code kind)
13337 if (left == NULL || right == NULL)
13338 return NULL;
13340 add_loc_list (&left, right);
13341 if (left == NULL)
13342 return NULL;
13344 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13345 return left;
13348 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13349 without actually allocating it. */
13351 static unsigned long
13352 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13354 return size_of_int_loc_descriptor (i >> shift)
13355 + size_of_int_loc_descriptor (shift)
13356 + 1;
13359 /* Return size_of_locs (int_loc_descriptor (i)) without
13360 actually allocating it. */
13362 static unsigned long
13363 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13365 unsigned long s;
13367 if (i >= 0)
13369 int clz, ctz;
13370 if (i <= 31)
13371 return 1;
13372 else if (i <= 0xff)
13373 return 2;
13374 else if (i <= 0xffff)
13375 return 3;
13376 clz = clz_hwi (i);
13377 ctz = ctz_hwi (i);
13378 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13379 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13380 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13381 - clz - 5);
13382 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13383 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13384 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13385 - clz - 8);
13386 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13387 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13388 <= 4)
13389 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13390 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13391 return 5;
13392 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13393 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13394 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13395 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13396 - clz - 8);
13397 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13398 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13399 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13400 - clz - 16);
13401 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13402 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13403 && s > 6)
13404 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13405 - clz - 32);
13406 else
13407 return 1 + s;
13409 else
13411 if (i >= -0x80)
13412 return 2;
13413 else if (i >= -0x8000)
13414 return 3;
13415 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13417 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13419 s = size_of_int_loc_descriptor (-i) + 1;
13420 if (s < 5)
13421 return s;
13423 return 5;
13425 else
13427 unsigned long r = 1 + size_of_sleb128 (i);
13428 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13430 s = size_of_int_loc_descriptor (-i) + 1;
13431 if (s < r)
13432 return s;
13434 return r;
13439 /* Return loc description representing "address" of integer value.
13440 This can appear only as toplevel expression. */
13442 static dw_loc_descr_ref
13443 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13445 int litsize;
13446 dw_loc_descr_ref loc_result = NULL;
13448 if (!(dwarf_version >= 4 || !dwarf_strict))
13449 return NULL;
13451 litsize = size_of_int_loc_descriptor (i);
13452 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13453 is more compact. For DW_OP_stack_value we need:
13454 litsize + 1 (DW_OP_stack_value)
13455 and for DW_OP_implicit_value:
13456 1 (DW_OP_implicit_value) + 1 (length) + size. */
13457 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13459 loc_result = int_loc_descriptor (i);
13460 add_loc_descr (&loc_result,
13461 new_loc_descr (DW_OP_stack_value, 0, 0));
13462 return loc_result;
13465 loc_result = new_loc_descr (DW_OP_implicit_value,
13466 size, 0);
13467 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13468 loc_result->dw_loc_oprnd2.v.val_int = i;
13469 return loc_result;
13472 /* Return a location descriptor that designates a base+offset location. */
13474 static dw_loc_descr_ref
13475 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13476 enum var_init_status initialized)
13478 unsigned int regno;
13479 dw_loc_descr_ref result;
13480 dw_fde_ref fde = cfun->fde;
13482 /* We only use "frame base" when we're sure we're talking about the
13483 post-prologue local stack frame. We do this by *not* running
13484 register elimination until this point, and recognizing the special
13485 argument pointer and soft frame pointer rtx's. */
13486 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13488 rtx elim = (ira_use_lra_p
13489 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13490 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13492 if (elim != reg)
13494 if (GET_CODE (elim) == PLUS)
13496 offset += INTVAL (XEXP (elim, 1));
13497 elim = XEXP (elim, 0);
13499 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13500 && (elim == hard_frame_pointer_rtx
13501 || elim == stack_pointer_rtx))
13502 || elim == (frame_pointer_needed
13503 ? hard_frame_pointer_rtx
13504 : stack_pointer_rtx));
13506 /* If drap register is used to align stack, use frame
13507 pointer + offset to access stack variables. If stack
13508 is aligned without drap, use stack pointer + offset to
13509 access stack variables. */
13510 if (crtl->stack_realign_tried
13511 && reg == frame_pointer_rtx)
13513 int base_reg
13514 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13515 ? HARD_FRAME_POINTER_REGNUM
13516 : REGNO (elim));
13517 return new_reg_loc_descr (base_reg, offset);
13520 gcc_assert (frame_pointer_fb_offset_valid);
13521 offset += frame_pointer_fb_offset;
13522 return new_loc_descr (DW_OP_fbreg, offset, 0);
13526 regno = REGNO (reg);
13527 #ifdef LEAF_REG_REMAP
13528 if (crtl->uses_only_leaf_regs)
13530 int leaf_reg = LEAF_REG_REMAP (regno);
13531 if (leaf_reg != -1)
13532 regno = (unsigned) leaf_reg;
13534 #endif
13535 regno = DWARF_FRAME_REGNUM (regno);
13537 if (!optimize && fde
13538 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13540 /* Use cfa+offset to represent the location of arguments passed
13541 on the stack when drap is used to align stack.
13542 Only do this when not optimizing, for optimized code var-tracking
13543 is supposed to track where the arguments live and the register
13544 used as vdrap or drap in some spot might be used for something
13545 else in other part of the routine. */
13546 return new_loc_descr (DW_OP_fbreg, offset, 0);
13549 if (regno <= 31)
13550 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13551 offset, 0);
13552 else
13553 result = new_loc_descr (DW_OP_bregx, regno, offset);
13555 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13556 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13558 return result;
13561 /* Return true if this RTL expression describes a base+offset calculation. */
13563 static inline int
13564 is_based_loc (const_rtx rtl)
13566 return (GET_CODE (rtl) == PLUS
13567 && ((REG_P (XEXP (rtl, 0))
13568 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13569 && CONST_INT_P (XEXP (rtl, 1)))));
13572 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13573 failed. */
13575 static dw_loc_descr_ref
13576 tls_mem_loc_descriptor (rtx mem)
13578 tree base;
13579 dw_loc_descr_ref loc_result;
13581 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13582 return NULL;
13584 base = get_base_address (MEM_EXPR (mem));
13585 if (base == NULL
13586 || !VAR_P (base)
13587 || !DECL_THREAD_LOCAL_P (base))
13588 return NULL;
13590 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13591 if (loc_result == NULL)
13592 return NULL;
13594 if (MEM_OFFSET (mem))
13595 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13597 return loc_result;
13600 /* Output debug info about reason why we failed to expand expression as dwarf
13601 expression. */
13603 static void
13604 expansion_failed (tree expr, rtx rtl, char const *reason)
13606 if (dump_file && (dump_flags & TDF_DETAILS))
13608 fprintf (dump_file, "Failed to expand as dwarf: ");
13609 if (expr)
13610 print_generic_expr (dump_file, expr, dump_flags);
13611 if (rtl)
13613 fprintf (dump_file, "\n");
13614 print_rtl (dump_file, rtl);
13616 fprintf (dump_file, "\nReason: %s\n", reason);
13620 /* Helper function for const_ok_for_output. */
13622 static bool
13623 const_ok_for_output_1 (rtx rtl)
13625 if (GET_CODE (rtl) == UNSPEC)
13627 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13628 we can't express it in the debug info. */
13629 /* Don't complain about TLS UNSPECs, those are just too hard to
13630 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13631 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13632 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13633 if (flag_checking
13634 && (XVECLEN (rtl, 0) == 0
13635 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13636 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13637 inform (current_function_decl
13638 ? DECL_SOURCE_LOCATION (current_function_decl)
13639 : UNKNOWN_LOCATION,
13640 #if NUM_UNSPEC_VALUES > 0
13641 "non-delegitimized UNSPEC %s (%d) found in variable location",
13642 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13643 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13644 XINT (rtl, 1));
13645 #else
13646 "non-delegitimized UNSPEC %d found in variable location",
13647 XINT (rtl, 1));
13648 #endif
13649 expansion_failed (NULL_TREE, rtl,
13650 "UNSPEC hasn't been delegitimized.\n");
13651 return false;
13654 if (targetm.const_not_ok_for_debug_p (rtl))
13656 expansion_failed (NULL_TREE, rtl,
13657 "Expression rejected for debug by the backend.\n");
13658 return false;
13661 /* FIXME: Refer to PR60655. It is possible for simplification
13662 of rtl expressions in var tracking to produce such expressions.
13663 We should really identify / validate expressions
13664 enclosed in CONST that can be handled by assemblers on various
13665 targets and only handle legitimate cases here. */
13666 if (GET_CODE (rtl) != SYMBOL_REF)
13668 if (GET_CODE (rtl) == NOT)
13669 return false;
13670 return true;
13673 if (CONSTANT_POOL_ADDRESS_P (rtl))
13675 bool marked;
13676 get_pool_constant_mark (rtl, &marked);
13677 /* If all references to this pool constant were optimized away,
13678 it was not output and thus we can't represent it. */
13679 if (!marked)
13681 expansion_failed (NULL_TREE, rtl,
13682 "Constant was removed from constant pool.\n");
13683 return false;
13687 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13688 return false;
13690 /* Avoid references to external symbols in debug info, on several targets
13691 the linker might even refuse to link when linking a shared library,
13692 and in many other cases the relocations for .debug_info/.debug_loc are
13693 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13694 to be defined within the same shared library or executable are fine. */
13695 if (SYMBOL_REF_EXTERNAL_P (rtl))
13697 tree decl = SYMBOL_REF_DECL (rtl);
13699 if (decl == NULL || !targetm.binds_local_p (decl))
13701 expansion_failed (NULL_TREE, rtl,
13702 "Symbol not defined in current TU.\n");
13703 return false;
13707 return true;
13710 /* Return true if constant RTL can be emitted in DW_OP_addr or
13711 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13712 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13714 static bool
13715 const_ok_for_output (rtx rtl)
13717 if (GET_CODE (rtl) == SYMBOL_REF)
13718 return const_ok_for_output_1 (rtl);
13720 if (GET_CODE (rtl) == CONST)
13722 subrtx_var_iterator::array_type array;
13723 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13724 if (!const_ok_for_output_1 (*iter))
13725 return false;
13726 return true;
13729 return true;
13732 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13733 if possible, NULL otherwise. */
13735 static dw_die_ref
13736 base_type_for_mode (machine_mode mode, bool unsignedp)
13738 dw_die_ref type_die;
13739 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13741 if (type == NULL)
13742 return NULL;
13743 switch (TREE_CODE (type))
13745 case INTEGER_TYPE:
13746 case REAL_TYPE:
13747 break;
13748 default:
13749 return NULL;
13751 type_die = lookup_type_die (type);
13752 if (!type_die)
13753 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13754 comp_unit_die ());
13755 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13756 return NULL;
13757 return type_die;
13760 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13761 type matching MODE, or, if MODE is narrower than or as wide as
13762 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13763 possible. */
13765 static dw_loc_descr_ref
13766 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13768 machine_mode outer_mode = mode;
13769 dw_die_ref type_die;
13770 dw_loc_descr_ref cvt;
13772 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13774 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13775 return op;
13777 type_die = base_type_for_mode (outer_mode, 1);
13778 if (type_die == NULL)
13779 return NULL;
13780 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13781 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13782 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13783 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13784 add_loc_descr (&op, cvt);
13785 return op;
13788 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13790 static dw_loc_descr_ref
13791 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13792 dw_loc_descr_ref op1)
13794 dw_loc_descr_ref ret = op0;
13795 add_loc_descr (&ret, op1);
13796 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13797 if (STORE_FLAG_VALUE != 1)
13799 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13800 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13802 return ret;
13805 /* Return location descriptor for signed comparison OP RTL. */
13807 static dw_loc_descr_ref
13808 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13809 machine_mode mem_mode)
13811 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13812 dw_loc_descr_ref op0, op1;
13813 int shift;
13815 if (op_mode == VOIDmode)
13816 op_mode = GET_MODE (XEXP (rtl, 1));
13817 if (op_mode == VOIDmode)
13818 return NULL;
13820 if (dwarf_strict
13821 && dwarf_version < 5
13822 && (!SCALAR_INT_MODE_P (op_mode)
13823 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13824 return NULL;
13826 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13827 VAR_INIT_STATUS_INITIALIZED);
13828 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13829 VAR_INIT_STATUS_INITIALIZED);
13831 if (op0 == NULL || op1 == NULL)
13832 return NULL;
13834 if (!SCALAR_INT_MODE_P (op_mode)
13835 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13836 return compare_loc_descriptor (op, op0, op1);
13838 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13840 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13841 dw_loc_descr_ref cvt;
13843 if (type_die == NULL)
13844 return NULL;
13845 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13846 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13847 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13848 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13849 add_loc_descr (&op0, cvt);
13850 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13851 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13852 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13853 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13854 add_loc_descr (&op1, cvt);
13855 return compare_loc_descriptor (op, op0, op1);
13858 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13859 /* For eq/ne, if the operands are known to be zero-extended,
13860 there is no need to do the fancy shifting up. */
13861 if (op == DW_OP_eq || op == DW_OP_ne)
13863 dw_loc_descr_ref last0, last1;
13864 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13866 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13868 /* deref_size zero extends, and for constants we can check
13869 whether they are zero extended or not. */
13870 if (((last0->dw_loc_opc == DW_OP_deref_size
13871 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13872 || (CONST_INT_P (XEXP (rtl, 0))
13873 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13874 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13875 && ((last1->dw_loc_opc == DW_OP_deref_size
13876 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13877 || (CONST_INT_P (XEXP (rtl, 1))
13878 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13879 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13880 return compare_loc_descriptor (op, op0, op1);
13882 /* EQ/NE comparison against constant in narrower type than
13883 DWARF2_ADDR_SIZE can be performed either as
13884 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13885 DW_OP_{eq,ne}
13887 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13888 DW_OP_{eq,ne}. Pick whatever is shorter. */
13889 if (CONST_INT_P (XEXP (rtl, 1))
13890 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13891 && (size_of_int_loc_descriptor (shift) + 1
13892 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13893 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13894 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13895 & GET_MODE_MASK (op_mode))))
13897 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13898 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13899 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13900 & GET_MODE_MASK (op_mode));
13901 return compare_loc_descriptor (op, op0, op1);
13904 add_loc_descr (&op0, int_loc_descriptor (shift));
13905 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13906 if (CONST_INT_P (XEXP (rtl, 1)))
13907 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13908 else
13910 add_loc_descr (&op1, int_loc_descriptor (shift));
13911 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13913 return compare_loc_descriptor (op, op0, op1);
13916 /* Return location descriptor for unsigned comparison OP RTL. */
13918 static dw_loc_descr_ref
13919 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13920 machine_mode mem_mode)
13922 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13923 dw_loc_descr_ref op0, op1;
13925 if (op_mode == VOIDmode)
13926 op_mode = GET_MODE (XEXP (rtl, 1));
13927 if (op_mode == VOIDmode)
13928 return NULL;
13929 if (!SCALAR_INT_MODE_P (op_mode))
13930 return NULL;
13932 if (dwarf_strict
13933 && dwarf_version < 5
13934 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13935 return NULL;
13937 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13938 VAR_INIT_STATUS_INITIALIZED);
13939 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13940 VAR_INIT_STATUS_INITIALIZED);
13942 if (op0 == NULL || op1 == NULL)
13943 return NULL;
13945 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13947 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13948 dw_loc_descr_ref last0, last1;
13949 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13951 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13953 if (CONST_INT_P (XEXP (rtl, 0)))
13954 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13955 /* deref_size zero extends, so no need to mask it again. */
13956 else if (last0->dw_loc_opc != DW_OP_deref_size
13957 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13959 add_loc_descr (&op0, int_loc_descriptor (mask));
13960 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13962 if (CONST_INT_P (XEXP (rtl, 1)))
13963 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13964 /* deref_size zero extends, so no need to mask it again. */
13965 else if (last1->dw_loc_opc != DW_OP_deref_size
13966 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13968 add_loc_descr (&op1, int_loc_descriptor (mask));
13969 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13972 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13974 HOST_WIDE_INT bias = 1;
13975 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13976 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13977 if (CONST_INT_P (XEXP (rtl, 1)))
13978 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13979 + INTVAL (XEXP (rtl, 1)));
13980 else
13981 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13982 bias, 0));
13984 return compare_loc_descriptor (op, op0, op1);
13987 /* Return location descriptor for {U,S}{MIN,MAX}. */
13989 static dw_loc_descr_ref
13990 minmax_loc_descriptor (rtx rtl, machine_mode mode,
13991 machine_mode mem_mode)
13993 enum dwarf_location_atom op;
13994 dw_loc_descr_ref op0, op1, ret;
13995 dw_loc_descr_ref bra_node, drop_node;
13997 if (dwarf_strict
13998 && dwarf_version < 5
13999 && (!SCALAR_INT_MODE_P (mode)
14000 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14001 return NULL;
14003 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14004 VAR_INIT_STATUS_INITIALIZED);
14005 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14006 VAR_INIT_STATUS_INITIALIZED);
14008 if (op0 == NULL || op1 == NULL)
14009 return NULL;
14011 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14012 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14013 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14014 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14016 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14018 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14019 add_loc_descr (&op0, int_loc_descriptor (mask));
14020 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14021 add_loc_descr (&op1, int_loc_descriptor (mask));
14022 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14024 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14026 HOST_WIDE_INT bias = 1;
14027 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14028 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14029 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14032 else if (!SCALAR_INT_MODE_P (mode)
14033 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14035 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14036 add_loc_descr (&op0, int_loc_descriptor (shift));
14037 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14038 add_loc_descr (&op1, int_loc_descriptor (shift));
14039 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14041 else if (SCALAR_INT_MODE_P (mode)
14042 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14044 dw_die_ref type_die = base_type_for_mode (mode, 0);
14045 dw_loc_descr_ref cvt;
14046 if (type_die == NULL)
14047 return NULL;
14048 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14049 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14050 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14051 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14052 add_loc_descr (&op0, cvt);
14053 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14054 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14055 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14056 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14057 add_loc_descr (&op1, cvt);
14060 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14061 op = DW_OP_lt;
14062 else
14063 op = DW_OP_gt;
14064 ret = op0;
14065 add_loc_descr (&ret, op1);
14066 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14067 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14068 add_loc_descr (&ret, bra_node);
14069 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14070 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14071 add_loc_descr (&ret, drop_node);
14072 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14073 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14074 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14075 && SCALAR_INT_MODE_P (mode)
14076 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14077 ret = convert_descriptor_to_mode (mode, ret);
14078 return ret;
14081 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14082 but after converting arguments to type_die, afterwards
14083 convert back to unsigned. */
14085 static dw_loc_descr_ref
14086 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14087 machine_mode mode, machine_mode mem_mode)
14089 dw_loc_descr_ref cvt, op0, op1;
14091 if (type_die == NULL)
14092 return NULL;
14093 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14094 VAR_INIT_STATUS_INITIALIZED);
14095 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14096 VAR_INIT_STATUS_INITIALIZED);
14097 if (op0 == NULL || op1 == NULL)
14098 return NULL;
14099 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14100 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14101 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14102 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14103 add_loc_descr (&op0, cvt);
14104 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14105 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14106 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14107 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14108 add_loc_descr (&op1, cvt);
14109 add_loc_descr (&op0, op1);
14110 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14111 return convert_descriptor_to_mode (mode, op0);
14114 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14115 const0 is DW_OP_lit0 or corresponding typed constant,
14116 const1 is DW_OP_lit1 or corresponding typed constant
14117 and constMSB is constant with just the MSB bit set
14118 for the mode):
14119 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14120 L1: const0 DW_OP_swap
14121 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14122 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14123 L3: DW_OP_drop
14124 L4: DW_OP_nop
14126 CTZ is similar:
14127 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14128 L1: const0 DW_OP_swap
14129 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14130 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14131 L3: DW_OP_drop
14132 L4: DW_OP_nop
14134 FFS is similar:
14135 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14136 L1: const1 DW_OP_swap
14137 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14138 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14139 L3: DW_OP_drop
14140 L4: DW_OP_nop */
14142 static dw_loc_descr_ref
14143 clz_loc_descriptor (rtx rtl, machine_mode mode,
14144 machine_mode mem_mode)
14146 dw_loc_descr_ref op0, ret, tmp;
14147 HOST_WIDE_INT valv;
14148 dw_loc_descr_ref l1jump, l1label;
14149 dw_loc_descr_ref l2jump, l2label;
14150 dw_loc_descr_ref l3jump, l3label;
14151 dw_loc_descr_ref l4jump, l4label;
14152 rtx msb;
14154 if (!SCALAR_INT_MODE_P (mode)
14155 || GET_MODE (XEXP (rtl, 0)) != mode)
14156 return NULL;
14158 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14159 VAR_INIT_STATUS_INITIALIZED);
14160 if (op0 == NULL)
14161 return NULL;
14162 ret = op0;
14163 if (GET_CODE (rtl) == CLZ)
14165 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14166 valv = GET_MODE_BITSIZE (mode);
14168 else if (GET_CODE (rtl) == FFS)
14169 valv = 0;
14170 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14171 valv = GET_MODE_BITSIZE (mode);
14172 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14173 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14174 add_loc_descr (&ret, l1jump);
14175 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14176 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14177 VAR_INIT_STATUS_INITIALIZED);
14178 if (tmp == NULL)
14179 return NULL;
14180 add_loc_descr (&ret, tmp);
14181 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14182 add_loc_descr (&ret, l4jump);
14183 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14184 ? const1_rtx : const0_rtx,
14185 mode, mem_mode,
14186 VAR_INIT_STATUS_INITIALIZED);
14187 if (l1label == NULL)
14188 return NULL;
14189 add_loc_descr (&ret, l1label);
14190 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14191 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14192 add_loc_descr (&ret, l2label);
14193 if (GET_CODE (rtl) != CLZ)
14194 msb = const1_rtx;
14195 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14196 msb = GEN_INT (HOST_WIDE_INT_1U
14197 << (GET_MODE_BITSIZE (mode) - 1));
14198 else
14199 msb = immed_wide_int_const
14200 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14201 GET_MODE_PRECISION (mode)), mode);
14202 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14203 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14204 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14205 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14206 else
14207 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14208 VAR_INIT_STATUS_INITIALIZED);
14209 if (tmp == NULL)
14210 return NULL;
14211 add_loc_descr (&ret, tmp);
14212 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14213 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14214 add_loc_descr (&ret, l3jump);
14215 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14216 VAR_INIT_STATUS_INITIALIZED);
14217 if (tmp == NULL)
14218 return NULL;
14219 add_loc_descr (&ret, tmp);
14220 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14221 ? DW_OP_shl : DW_OP_shr, 0, 0));
14222 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14223 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14224 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14225 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14226 add_loc_descr (&ret, l2jump);
14227 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14228 add_loc_descr (&ret, l3label);
14229 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14230 add_loc_descr (&ret, l4label);
14231 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14232 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14233 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14234 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14235 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14236 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14237 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14238 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14239 return ret;
14242 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14243 const1 is DW_OP_lit1 or corresponding typed constant):
14244 const0 DW_OP_swap
14245 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14246 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14247 L2: DW_OP_drop
14249 PARITY is similar:
14250 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14251 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14252 L2: DW_OP_drop */
14254 static dw_loc_descr_ref
14255 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14256 machine_mode mem_mode)
14258 dw_loc_descr_ref op0, ret, tmp;
14259 dw_loc_descr_ref l1jump, l1label;
14260 dw_loc_descr_ref l2jump, l2label;
14262 if (!SCALAR_INT_MODE_P (mode)
14263 || GET_MODE (XEXP (rtl, 0)) != mode)
14264 return NULL;
14266 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14267 VAR_INIT_STATUS_INITIALIZED);
14268 if (op0 == NULL)
14269 return NULL;
14270 ret = op0;
14271 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14272 VAR_INIT_STATUS_INITIALIZED);
14273 if (tmp == NULL)
14274 return NULL;
14275 add_loc_descr (&ret, tmp);
14276 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14277 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14278 add_loc_descr (&ret, l1label);
14279 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14280 add_loc_descr (&ret, l2jump);
14281 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14282 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14283 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14284 VAR_INIT_STATUS_INITIALIZED);
14285 if (tmp == NULL)
14286 return NULL;
14287 add_loc_descr (&ret, tmp);
14288 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14289 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14290 ? DW_OP_plus : DW_OP_xor, 0, 0));
14291 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14292 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14293 VAR_INIT_STATUS_INITIALIZED);
14294 add_loc_descr (&ret, tmp);
14295 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14296 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14297 add_loc_descr (&ret, l1jump);
14298 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14299 add_loc_descr (&ret, l2label);
14300 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14301 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14302 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14303 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14304 return ret;
14307 /* BSWAP (constS is initial shift count, either 56 or 24):
14308 constS const0
14309 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14310 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14311 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14312 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14313 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14315 static dw_loc_descr_ref
14316 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14317 machine_mode mem_mode)
14319 dw_loc_descr_ref op0, ret, tmp;
14320 dw_loc_descr_ref l1jump, l1label;
14321 dw_loc_descr_ref l2jump, l2label;
14323 if (!SCALAR_INT_MODE_P (mode)
14324 || BITS_PER_UNIT != 8
14325 || (GET_MODE_BITSIZE (mode) != 32
14326 && GET_MODE_BITSIZE (mode) != 64))
14327 return NULL;
14329 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14330 VAR_INIT_STATUS_INITIALIZED);
14331 if (op0 == NULL)
14332 return NULL;
14334 ret = op0;
14335 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14336 mode, mem_mode,
14337 VAR_INIT_STATUS_INITIALIZED);
14338 if (tmp == NULL)
14339 return NULL;
14340 add_loc_descr (&ret, tmp);
14341 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14342 VAR_INIT_STATUS_INITIALIZED);
14343 if (tmp == NULL)
14344 return NULL;
14345 add_loc_descr (&ret, tmp);
14346 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14347 add_loc_descr (&ret, l1label);
14348 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14349 mode, mem_mode,
14350 VAR_INIT_STATUS_INITIALIZED);
14351 add_loc_descr (&ret, tmp);
14352 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14353 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14354 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14355 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14356 VAR_INIT_STATUS_INITIALIZED);
14357 if (tmp == NULL)
14358 return NULL;
14359 add_loc_descr (&ret, tmp);
14360 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14361 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14362 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14363 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14364 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14365 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14366 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14367 VAR_INIT_STATUS_INITIALIZED);
14368 add_loc_descr (&ret, tmp);
14369 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14370 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14371 add_loc_descr (&ret, l2jump);
14372 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14373 VAR_INIT_STATUS_INITIALIZED);
14374 add_loc_descr (&ret, tmp);
14375 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14376 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14377 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14378 add_loc_descr (&ret, l1jump);
14379 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14380 add_loc_descr (&ret, l2label);
14381 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14382 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14383 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14384 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14385 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14386 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14387 return ret;
14390 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14391 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14392 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14393 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14395 ROTATERT is similar:
14396 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14397 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14398 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14400 static dw_loc_descr_ref
14401 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14402 machine_mode mem_mode)
14404 rtx rtlop1 = XEXP (rtl, 1);
14405 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14406 int i;
14408 if (!SCALAR_INT_MODE_P (mode))
14409 return NULL;
14411 if (GET_MODE (rtlop1) != VOIDmode
14412 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14413 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14414 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14415 VAR_INIT_STATUS_INITIALIZED);
14416 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14417 VAR_INIT_STATUS_INITIALIZED);
14418 if (op0 == NULL || op1 == NULL)
14419 return NULL;
14420 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14421 for (i = 0; i < 2; i++)
14423 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14424 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14425 mode, mem_mode,
14426 VAR_INIT_STATUS_INITIALIZED);
14427 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14428 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14429 ? DW_OP_const4u
14430 : HOST_BITS_PER_WIDE_INT == 64
14431 ? DW_OP_const8u : DW_OP_constu,
14432 GET_MODE_MASK (mode), 0);
14433 else
14434 mask[i] = NULL;
14435 if (mask[i] == NULL)
14436 return NULL;
14437 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14439 ret = op0;
14440 add_loc_descr (&ret, op1);
14441 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14442 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14443 if (GET_CODE (rtl) == ROTATERT)
14445 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14446 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14447 GET_MODE_BITSIZE (mode), 0));
14449 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14450 if (mask[0] != NULL)
14451 add_loc_descr (&ret, mask[0]);
14452 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14453 if (mask[1] != NULL)
14455 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14456 add_loc_descr (&ret, mask[1]);
14457 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14459 if (GET_CODE (rtl) == ROTATE)
14461 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14462 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14463 GET_MODE_BITSIZE (mode), 0));
14465 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14466 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14467 return ret;
14470 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14471 for DEBUG_PARAMETER_REF RTL. */
14473 static dw_loc_descr_ref
14474 parameter_ref_descriptor (rtx rtl)
14476 dw_loc_descr_ref ret;
14477 dw_die_ref ref;
14479 if (dwarf_strict)
14480 return NULL;
14481 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14482 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14483 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14484 if (ref)
14486 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14487 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14488 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14490 else
14492 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14493 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14495 return ret;
14498 /* The following routine converts the RTL for a variable or parameter
14499 (resident in memory) into an equivalent Dwarf representation of a
14500 mechanism for getting the address of that same variable onto the top of a
14501 hypothetical "address evaluation" stack.
14503 When creating memory location descriptors, we are effectively transforming
14504 the RTL for a memory-resident object into its Dwarf postfix expression
14505 equivalent. This routine recursively descends an RTL tree, turning
14506 it into Dwarf postfix code as it goes.
14508 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14510 MEM_MODE is the mode of the memory reference, needed to handle some
14511 autoincrement addressing modes.
14513 Return 0 if we can't represent the location. */
14515 dw_loc_descr_ref
14516 mem_loc_descriptor (rtx rtl, machine_mode mode,
14517 machine_mode mem_mode,
14518 enum var_init_status initialized)
14520 dw_loc_descr_ref mem_loc_result = NULL;
14521 enum dwarf_location_atom op;
14522 dw_loc_descr_ref op0, op1;
14523 rtx inner = NULL_RTX;
14525 if (mode == VOIDmode)
14526 mode = GET_MODE (rtl);
14528 /* Note that for a dynamically sized array, the location we will generate a
14529 description of here will be the lowest numbered location which is
14530 actually within the array. That's *not* necessarily the same as the
14531 zeroth element of the array. */
14533 rtl = targetm.delegitimize_address (rtl);
14535 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14536 return NULL;
14538 switch (GET_CODE (rtl))
14540 case POST_INC:
14541 case POST_DEC:
14542 case POST_MODIFY:
14543 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14545 case SUBREG:
14546 /* The case of a subreg may arise when we have a local (register)
14547 variable or a formal (register) parameter which doesn't quite fill
14548 up an entire register. For now, just assume that it is
14549 legitimate to make the Dwarf info refer to the whole register which
14550 contains the given subreg. */
14551 if (!subreg_lowpart_p (rtl))
14552 break;
14553 inner = SUBREG_REG (rtl);
14554 /* FALLTHRU */
14555 case TRUNCATE:
14556 if (inner == NULL_RTX)
14557 inner = XEXP (rtl, 0);
14558 if (SCALAR_INT_MODE_P (mode)
14559 && SCALAR_INT_MODE_P (GET_MODE (inner))
14560 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14561 #ifdef POINTERS_EXTEND_UNSIGNED
14562 || (mode == Pmode && mem_mode != VOIDmode)
14563 #endif
14565 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14567 mem_loc_result = mem_loc_descriptor (inner,
14568 GET_MODE (inner),
14569 mem_mode, initialized);
14570 break;
14572 if (dwarf_strict && dwarf_version < 5)
14573 break;
14574 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14575 break;
14576 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14577 && (!SCALAR_INT_MODE_P (mode)
14578 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14579 break;
14580 else
14582 dw_die_ref type_die;
14583 dw_loc_descr_ref cvt;
14585 mem_loc_result = mem_loc_descriptor (inner,
14586 GET_MODE (inner),
14587 mem_mode, initialized);
14588 if (mem_loc_result == NULL)
14589 break;
14590 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14591 if (type_die == NULL)
14593 mem_loc_result = NULL;
14594 break;
14596 if (GET_MODE_SIZE (mode)
14597 != GET_MODE_SIZE (GET_MODE (inner)))
14598 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14599 else
14600 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14601 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14602 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14603 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14604 add_loc_descr (&mem_loc_result, cvt);
14605 if (SCALAR_INT_MODE_P (mode)
14606 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14608 /* Convert it to untyped afterwards. */
14609 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14610 add_loc_descr (&mem_loc_result, cvt);
14613 break;
14615 case REG:
14616 if (! SCALAR_INT_MODE_P (mode)
14617 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14618 && rtl != arg_pointer_rtx
14619 && rtl != frame_pointer_rtx
14620 #ifdef POINTERS_EXTEND_UNSIGNED
14621 && (mode != Pmode || mem_mode == VOIDmode)
14622 #endif
14625 dw_die_ref type_die;
14626 unsigned int dbx_regnum;
14628 if (dwarf_strict && dwarf_version < 5)
14629 break;
14630 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14631 break;
14632 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14633 if (type_die == NULL)
14634 break;
14636 dbx_regnum = dbx_reg_number (rtl);
14637 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14638 break;
14639 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14640 dbx_regnum, 0);
14641 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14642 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14643 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14644 break;
14646 /* Whenever a register number forms a part of the description of the
14647 method for calculating the (dynamic) address of a memory resident
14648 object, DWARF rules require the register number be referred to as
14649 a "base register". This distinction is not based in any way upon
14650 what category of register the hardware believes the given register
14651 belongs to. This is strictly DWARF terminology we're dealing with
14652 here. Note that in cases where the location of a memory-resident
14653 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14654 OP_CONST (0)) the actual DWARF location descriptor that we generate
14655 may just be OP_BASEREG (basereg). This may look deceptively like
14656 the object in question was allocated to a register (rather than in
14657 memory) so DWARF consumers need to be aware of the subtle
14658 distinction between OP_REG and OP_BASEREG. */
14659 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14660 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14661 else if (stack_realign_drap
14662 && crtl->drap_reg
14663 && crtl->args.internal_arg_pointer == rtl
14664 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14666 /* If RTL is internal_arg_pointer, which has been optimized
14667 out, use DRAP instead. */
14668 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14669 VAR_INIT_STATUS_INITIALIZED);
14671 break;
14673 case SIGN_EXTEND:
14674 case ZERO_EXTEND:
14675 if (!SCALAR_INT_MODE_P (mode))
14676 break;
14677 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14678 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14679 if (op0 == 0)
14680 break;
14681 else if (GET_CODE (rtl) == ZERO_EXTEND
14682 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14683 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14684 < HOST_BITS_PER_WIDE_INT
14685 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14686 to expand zero extend as two shifts instead of
14687 masking. */
14688 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14690 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14691 mem_loc_result = op0;
14692 add_loc_descr (&mem_loc_result,
14693 int_loc_descriptor (GET_MODE_MASK (imode)));
14694 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14696 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14698 int shift = DWARF2_ADDR_SIZE
14699 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14700 shift *= BITS_PER_UNIT;
14701 if (GET_CODE (rtl) == SIGN_EXTEND)
14702 op = DW_OP_shra;
14703 else
14704 op = DW_OP_shr;
14705 mem_loc_result = op0;
14706 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14707 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14708 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14709 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14711 else if (!dwarf_strict || dwarf_version >= 5)
14713 dw_die_ref type_die1, type_die2;
14714 dw_loc_descr_ref cvt;
14716 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14717 GET_CODE (rtl) == ZERO_EXTEND);
14718 if (type_die1 == NULL)
14719 break;
14720 type_die2 = base_type_for_mode (mode, 1);
14721 if (type_die2 == NULL)
14722 break;
14723 mem_loc_result = op0;
14724 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14725 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14726 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14727 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14728 add_loc_descr (&mem_loc_result, cvt);
14729 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14730 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14731 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14732 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14733 add_loc_descr (&mem_loc_result, cvt);
14735 break;
14737 case MEM:
14739 rtx new_rtl = avoid_constant_pool_reference (rtl);
14740 if (new_rtl != rtl)
14742 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14743 initialized);
14744 if (mem_loc_result != NULL)
14745 return mem_loc_result;
14748 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14749 get_address_mode (rtl), mode,
14750 VAR_INIT_STATUS_INITIALIZED);
14751 if (mem_loc_result == NULL)
14752 mem_loc_result = tls_mem_loc_descriptor (rtl);
14753 if (mem_loc_result != NULL)
14755 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14756 || !SCALAR_INT_MODE_P(mode))
14758 dw_die_ref type_die;
14759 dw_loc_descr_ref deref;
14761 if (dwarf_strict && dwarf_version < 5)
14762 return NULL;
14763 type_die
14764 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14765 if (type_die == NULL)
14766 return NULL;
14767 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14768 GET_MODE_SIZE (mode), 0);
14769 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14770 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14771 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14772 add_loc_descr (&mem_loc_result, deref);
14774 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14775 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14776 else
14777 add_loc_descr (&mem_loc_result,
14778 new_loc_descr (DW_OP_deref_size,
14779 GET_MODE_SIZE (mode), 0));
14781 break;
14783 case LO_SUM:
14784 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14786 case LABEL_REF:
14787 /* Some ports can transform a symbol ref into a label ref, because
14788 the symbol ref is too far away and has to be dumped into a constant
14789 pool. */
14790 case CONST:
14791 case SYMBOL_REF:
14792 if (!SCALAR_INT_MODE_P (mode)
14793 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14794 #ifdef POINTERS_EXTEND_UNSIGNED
14795 && (mode != Pmode || mem_mode == VOIDmode)
14796 #endif
14798 break;
14799 if (GET_CODE (rtl) == SYMBOL_REF
14800 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14802 dw_loc_descr_ref temp;
14804 /* If this is not defined, we have no way to emit the data. */
14805 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14806 break;
14808 temp = new_addr_loc_descr (rtl, dtprel_true);
14810 /* We check for DWARF 5 here because gdb did not implement
14811 DW_OP_form_tls_address until after 7.12. */
14812 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14813 ? DW_OP_form_tls_address
14814 : DW_OP_GNU_push_tls_address),
14815 0, 0);
14816 add_loc_descr (&mem_loc_result, temp);
14818 break;
14821 if (!const_ok_for_output (rtl))
14823 if (GET_CODE (rtl) == CONST)
14824 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14825 initialized);
14826 break;
14829 symref:
14830 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14831 vec_safe_push (used_rtx_array, rtl);
14832 break;
14834 case CONCAT:
14835 case CONCATN:
14836 case VAR_LOCATION:
14837 case DEBUG_IMPLICIT_PTR:
14838 expansion_failed (NULL_TREE, rtl,
14839 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14840 return 0;
14842 case ENTRY_VALUE:
14843 if (dwarf_strict && dwarf_version < 5)
14844 return NULL;
14845 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14847 if (!SCALAR_INT_MODE_P (mode)
14848 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14849 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14850 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14851 else
14853 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14854 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14855 return NULL;
14856 op0 = one_reg_loc_descriptor (dbx_regnum,
14857 VAR_INIT_STATUS_INITIALIZED);
14860 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14861 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14863 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14864 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14865 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14866 return NULL;
14868 else
14869 gcc_unreachable ();
14870 if (op0 == NULL)
14871 return NULL;
14872 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14873 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14874 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14875 break;
14877 case DEBUG_PARAMETER_REF:
14878 mem_loc_result = parameter_ref_descriptor (rtl);
14879 break;
14881 case PRE_MODIFY:
14882 /* Extract the PLUS expression nested inside and fall into
14883 PLUS code below. */
14884 rtl = XEXP (rtl, 1);
14885 goto plus;
14887 case PRE_INC:
14888 case PRE_DEC:
14889 /* Turn these into a PLUS expression and fall into the PLUS code
14890 below. */
14891 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14892 gen_int_mode (GET_CODE (rtl) == PRE_INC
14893 ? GET_MODE_UNIT_SIZE (mem_mode)
14894 : -GET_MODE_UNIT_SIZE (mem_mode),
14895 mode));
14897 /* fall through */
14899 case PLUS:
14900 plus:
14901 if (is_based_loc (rtl)
14902 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14903 || XEXP (rtl, 0) == arg_pointer_rtx
14904 || XEXP (rtl, 0) == frame_pointer_rtx)
14905 && SCALAR_INT_MODE_P (mode))
14906 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14907 INTVAL (XEXP (rtl, 1)),
14908 VAR_INIT_STATUS_INITIALIZED);
14909 else
14911 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14912 VAR_INIT_STATUS_INITIALIZED);
14913 if (mem_loc_result == 0)
14914 break;
14916 if (CONST_INT_P (XEXP (rtl, 1))
14917 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14918 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14919 else
14921 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14922 VAR_INIT_STATUS_INITIALIZED);
14923 if (op1 == 0)
14924 return NULL;
14925 add_loc_descr (&mem_loc_result, op1);
14926 add_loc_descr (&mem_loc_result,
14927 new_loc_descr (DW_OP_plus, 0, 0));
14930 break;
14932 /* If a pseudo-reg is optimized away, it is possible for it to
14933 be replaced with a MEM containing a multiply or shift. */
14934 case MINUS:
14935 op = DW_OP_minus;
14936 goto do_binop;
14938 case MULT:
14939 op = DW_OP_mul;
14940 goto do_binop;
14942 case DIV:
14943 if ((!dwarf_strict || dwarf_version >= 5)
14944 && SCALAR_INT_MODE_P (mode)
14945 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14947 mem_loc_result = typed_binop (DW_OP_div, rtl,
14948 base_type_for_mode (mode, 0),
14949 mode, mem_mode);
14950 break;
14952 op = DW_OP_div;
14953 goto do_binop;
14955 case UMOD:
14956 op = DW_OP_mod;
14957 goto do_binop;
14959 case ASHIFT:
14960 op = DW_OP_shl;
14961 goto do_shift;
14963 case ASHIFTRT:
14964 op = DW_OP_shra;
14965 goto do_shift;
14967 case LSHIFTRT:
14968 op = DW_OP_shr;
14969 goto do_shift;
14971 do_shift:
14972 if (!SCALAR_INT_MODE_P (mode))
14973 break;
14974 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14975 VAR_INIT_STATUS_INITIALIZED);
14977 rtx rtlop1 = XEXP (rtl, 1);
14978 if (GET_MODE (rtlop1) != VOIDmode
14979 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14980 < GET_MODE_BITSIZE (mode))
14981 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14982 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14983 VAR_INIT_STATUS_INITIALIZED);
14986 if (op0 == 0 || op1 == 0)
14987 break;
14989 mem_loc_result = op0;
14990 add_loc_descr (&mem_loc_result, op1);
14991 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14992 break;
14994 case AND:
14995 op = DW_OP_and;
14996 goto do_binop;
14998 case IOR:
14999 op = DW_OP_or;
15000 goto do_binop;
15002 case XOR:
15003 op = DW_OP_xor;
15004 goto do_binop;
15006 do_binop:
15007 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15008 VAR_INIT_STATUS_INITIALIZED);
15009 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15010 VAR_INIT_STATUS_INITIALIZED);
15012 if (op0 == 0 || op1 == 0)
15013 break;
15015 mem_loc_result = op0;
15016 add_loc_descr (&mem_loc_result, op1);
15017 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15018 break;
15020 case MOD:
15021 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15022 && (!dwarf_strict || dwarf_version >= 5))
15024 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15025 base_type_for_mode (mode, 0),
15026 mode, mem_mode);
15027 break;
15030 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15031 VAR_INIT_STATUS_INITIALIZED);
15032 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15033 VAR_INIT_STATUS_INITIALIZED);
15035 if (op0 == 0 || op1 == 0)
15036 break;
15038 mem_loc_result = op0;
15039 add_loc_descr (&mem_loc_result, op1);
15040 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15041 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15042 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15043 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15044 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15045 break;
15047 case UDIV:
15048 if ((!dwarf_strict || dwarf_version >= 5)
15049 && SCALAR_INT_MODE_P (mode))
15051 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15053 op = DW_OP_div;
15054 goto do_binop;
15056 mem_loc_result = typed_binop (DW_OP_div, rtl,
15057 base_type_for_mode (mode, 1),
15058 mode, mem_mode);
15060 break;
15062 case NOT:
15063 op = DW_OP_not;
15064 goto do_unop;
15066 case ABS:
15067 op = DW_OP_abs;
15068 goto do_unop;
15070 case NEG:
15071 op = DW_OP_neg;
15072 goto do_unop;
15074 do_unop:
15075 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15076 VAR_INIT_STATUS_INITIALIZED);
15078 if (op0 == 0)
15079 break;
15081 mem_loc_result = op0;
15082 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15083 break;
15085 case CONST_INT:
15086 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15087 #ifdef POINTERS_EXTEND_UNSIGNED
15088 || (mode == Pmode
15089 && mem_mode != VOIDmode
15090 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15091 #endif
15094 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15095 break;
15097 if ((!dwarf_strict || dwarf_version >= 5)
15098 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15099 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15101 dw_die_ref type_die = base_type_for_mode (mode, 1);
15102 machine_mode amode;
15103 if (type_die == NULL)
15104 return NULL;
15105 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15106 MODE_INT, 0);
15107 if (INTVAL (rtl) >= 0
15108 && amode != BLKmode
15109 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15110 /* const DW_OP_convert <XXX> vs.
15111 DW_OP_const_type <XXX, 1, const>. */
15112 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15113 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15115 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15116 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15117 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15118 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15119 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15120 add_loc_descr (&mem_loc_result, op0);
15121 return mem_loc_result;
15123 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15124 INTVAL (rtl));
15125 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15126 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15127 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15128 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15129 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15130 else
15132 mem_loc_result->dw_loc_oprnd2.val_class
15133 = dw_val_class_const_double;
15134 mem_loc_result->dw_loc_oprnd2.v.val_double
15135 = double_int::from_shwi (INTVAL (rtl));
15138 break;
15140 case CONST_DOUBLE:
15141 if (!dwarf_strict || dwarf_version >= 5)
15143 dw_die_ref type_die;
15145 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15146 CONST_DOUBLE rtx could represent either a large integer
15147 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15148 the value is always a floating point constant.
15150 When it is an integer, a CONST_DOUBLE is used whenever
15151 the constant requires 2 HWIs to be adequately represented.
15152 We output CONST_DOUBLEs as blocks. */
15153 if (mode == VOIDmode
15154 || (GET_MODE (rtl) == VOIDmode
15155 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15156 break;
15157 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15158 if (type_die == NULL)
15159 return NULL;
15160 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15161 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15162 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15163 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15164 #if TARGET_SUPPORTS_WIDE_INT == 0
15165 if (!SCALAR_FLOAT_MODE_P (mode))
15167 mem_loc_result->dw_loc_oprnd2.val_class
15168 = dw_val_class_const_double;
15169 mem_loc_result->dw_loc_oprnd2.v.val_double
15170 = rtx_to_double_int (rtl);
15172 else
15173 #endif
15175 unsigned int length = GET_MODE_SIZE (mode);
15176 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15178 insert_float (rtl, array);
15179 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15180 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15181 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15182 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15185 break;
15187 case CONST_WIDE_INT:
15188 if (!dwarf_strict || dwarf_version >= 5)
15190 dw_die_ref type_die;
15192 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15193 if (type_die == NULL)
15194 return NULL;
15195 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15196 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15197 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15198 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15199 mem_loc_result->dw_loc_oprnd2.val_class
15200 = dw_val_class_wide_int;
15201 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15202 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15204 break;
15206 case EQ:
15207 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15208 break;
15210 case GE:
15211 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15212 break;
15214 case GT:
15215 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15216 break;
15218 case LE:
15219 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15220 break;
15222 case LT:
15223 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15224 break;
15226 case NE:
15227 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15228 break;
15230 case GEU:
15231 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15232 break;
15234 case GTU:
15235 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15236 break;
15238 case LEU:
15239 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15240 break;
15242 case LTU:
15243 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15244 break;
15246 case UMIN:
15247 case UMAX:
15248 if (!SCALAR_INT_MODE_P (mode))
15249 break;
15250 /* FALLTHRU */
15251 case SMIN:
15252 case SMAX:
15253 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15254 break;
15256 case ZERO_EXTRACT:
15257 case SIGN_EXTRACT:
15258 if (CONST_INT_P (XEXP (rtl, 1))
15259 && CONST_INT_P (XEXP (rtl, 2))
15260 && ((unsigned) INTVAL (XEXP (rtl, 1))
15261 + (unsigned) INTVAL (XEXP (rtl, 2))
15262 <= GET_MODE_BITSIZE (mode))
15263 && SCALAR_INT_MODE_P (mode)
15264 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15265 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15267 int shift, size;
15268 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15269 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15270 if (op0 == 0)
15271 break;
15272 if (GET_CODE (rtl) == SIGN_EXTRACT)
15273 op = DW_OP_shra;
15274 else
15275 op = DW_OP_shr;
15276 mem_loc_result = op0;
15277 size = INTVAL (XEXP (rtl, 1));
15278 shift = INTVAL (XEXP (rtl, 2));
15279 if (BITS_BIG_ENDIAN)
15280 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15281 - shift - size;
15282 if (shift + size != (int) DWARF2_ADDR_SIZE)
15284 add_loc_descr (&mem_loc_result,
15285 int_loc_descriptor (DWARF2_ADDR_SIZE
15286 - shift - size));
15287 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15289 if (size != (int) DWARF2_ADDR_SIZE)
15291 add_loc_descr (&mem_loc_result,
15292 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15293 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15296 break;
15298 case IF_THEN_ELSE:
15300 dw_loc_descr_ref op2, bra_node, drop_node;
15301 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15302 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15303 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15304 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15305 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15306 VAR_INIT_STATUS_INITIALIZED);
15307 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15308 VAR_INIT_STATUS_INITIALIZED);
15309 if (op0 == NULL || op1 == NULL || op2 == NULL)
15310 break;
15312 mem_loc_result = op1;
15313 add_loc_descr (&mem_loc_result, op2);
15314 add_loc_descr (&mem_loc_result, op0);
15315 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15316 add_loc_descr (&mem_loc_result, bra_node);
15317 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15318 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15319 add_loc_descr (&mem_loc_result, drop_node);
15320 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15321 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15323 break;
15325 case FLOAT_EXTEND:
15326 case FLOAT_TRUNCATE:
15327 case FLOAT:
15328 case UNSIGNED_FLOAT:
15329 case FIX:
15330 case UNSIGNED_FIX:
15331 if (!dwarf_strict || dwarf_version >= 5)
15333 dw_die_ref type_die;
15334 dw_loc_descr_ref cvt;
15336 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15337 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15338 if (op0 == NULL)
15339 break;
15340 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15341 && (GET_CODE (rtl) == FLOAT
15342 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15343 <= DWARF2_ADDR_SIZE))
15345 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15346 GET_CODE (rtl) == UNSIGNED_FLOAT);
15347 if (type_die == NULL)
15348 break;
15349 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15350 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15351 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15352 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15353 add_loc_descr (&op0, cvt);
15355 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15356 if (type_die == NULL)
15357 break;
15358 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15359 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15360 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15361 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15362 add_loc_descr (&op0, cvt);
15363 if (SCALAR_INT_MODE_P (mode)
15364 && (GET_CODE (rtl) == FIX
15365 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15367 op0 = convert_descriptor_to_mode (mode, op0);
15368 if (op0 == NULL)
15369 break;
15371 mem_loc_result = op0;
15373 break;
15375 case CLZ:
15376 case CTZ:
15377 case FFS:
15378 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15379 break;
15381 case POPCOUNT:
15382 case PARITY:
15383 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15384 break;
15386 case BSWAP:
15387 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15388 break;
15390 case ROTATE:
15391 case ROTATERT:
15392 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15393 break;
15395 case COMPARE:
15396 /* In theory, we could implement the above. */
15397 /* DWARF cannot represent the unsigned compare operations
15398 natively. */
15399 case SS_MULT:
15400 case US_MULT:
15401 case SS_DIV:
15402 case US_DIV:
15403 case SS_PLUS:
15404 case US_PLUS:
15405 case SS_MINUS:
15406 case US_MINUS:
15407 case SS_NEG:
15408 case US_NEG:
15409 case SS_ABS:
15410 case SS_ASHIFT:
15411 case US_ASHIFT:
15412 case SS_TRUNCATE:
15413 case US_TRUNCATE:
15414 case UNORDERED:
15415 case ORDERED:
15416 case UNEQ:
15417 case UNGE:
15418 case UNGT:
15419 case UNLE:
15420 case UNLT:
15421 case LTGT:
15422 case FRACT_CONVERT:
15423 case UNSIGNED_FRACT_CONVERT:
15424 case SAT_FRACT:
15425 case UNSIGNED_SAT_FRACT:
15426 case SQRT:
15427 case ASM_OPERANDS:
15428 case VEC_MERGE:
15429 case VEC_SELECT:
15430 case VEC_CONCAT:
15431 case VEC_DUPLICATE:
15432 case UNSPEC:
15433 case HIGH:
15434 case FMA:
15435 case STRICT_LOW_PART:
15436 case CONST_VECTOR:
15437 case CONST_FIXED:
15438 case CLRSB:
15439 case CLOBBER:
15440 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15441 can't express it in the debug info. This can happen e.g. with some
15442 TLS UNSPECs. */
15443 break;
15445 case CONST_STRING:
15446 resolve_one_addr (&rtl);
15447 goto symref;
15449 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15450 the expression. An UNSPEC rtx represents a raw DWARF operation,
15451 new_loc_descr is called for it to build the operation directly.
15452 Otherwise mem_loc_descriptor is called recursively. */
15453 case PARALLEL:
15455 int index = 0;
15456 dw_loc_descr_ref exp_result = NULL;
15458 for (; index < XVECLEN (rtl, 0); index++)
15460 rtx elem = XVECEXP (rtl, 0, index);
15461 if (GET_CODE (elem) == UNSPEC)
15463 /* Each DWARF operation UNSPEC contain two operands, if
15464 one operand is not used for the operation, const0_rtx is
15465 passed. */
15466 gcc_assert (XVECLEN (elem, 0) == 2);
15468 HOST_WIDE_INT dw_op = XINT (elem, 1);
15469 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15470 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15471 exp_result
15472 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15473 oprnd2);
15475 else
15476 exp_result
15477 = mem_loc_descriptor (elem, mode, mem_mode,
15478 VAR_INIT_STATUS_INITIALIZED);
15480 if (!mem_loc_result)
15481 mem_loc_result = exp_result;
15482 else
15483 add_loc_descr (&mem_loc_result, exp_result);
15486 break;
15489 default:
15490 if (flag_checking)
15492 print_rtl (stderr, rtl);
15493 gcc_unreachable ();
15495 break;
15498 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15499 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15501 return mem_loc_result;
15504 /* Return a descriptor that describes the concatenation of two locations.
15505 This is typically a complex variable. */
15507 static dw_loc_descr_ref
15508 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15510 dw_loc_descr_ref cc_loc_result = NULL;
15511 dw_loc_descr_ref x0_ref
15512 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15513 dw_loc_descr_ref x1_ref
15514 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15516 if (x0_ref == 0 || x1_ref == 0)
15517 return 0;
15519 cc_loc_result = x0_ref;
15520 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15522 add_loc_descr (&cc_loc_result, x1_ref);
15523 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15525 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15526 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15528 return cc_loc_result;
15531 /* Return a descriptor that describes the concatenation of N
15532 locations. */
15534 static dw_loc_descr_ref
15535 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15537 unsigned int i;
15538 dw_loc_descr_ref cc_loc_result = NULL;
15539 unsigned int n = XVECLEN (concatn, 0);
15541 for (i = 0; i < n; ++i)
15543 dw_loc_descr_ref ref;
15544 rtx x = XVECEXP (concatn, 0, i);
15546 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15547 if (ref == NULL)
15548 return NULL;
15550 add_loc_descr (&cc_loc_result, ref);
15551 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15554 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15555 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15557 return cc_loc_result;
15560 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15561 for DEBUG_IMPLICIT_PTR RTL. */
15563 static dw_loc_descr_ref
15564 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15566 dw_loc_descr_ref ret;
15567 dw_die_ref ref;
15569 if (dwarf_strict && dwarf_version < 5)
15570 return NULL;
15571 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15572 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15573 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15574 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15575 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15576 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15577 if (ref)
15579 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15580 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15581 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15583 else
15585 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15586 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15588 return ret;
15591 /* Output a proper Dwarf location descriptor for a variable or parameter
15592 which is either allocated in a register or in a memory location. For a
15593 register, we just generate an OP_REG and the register number. For a
15594 memory location we provide a Dwarf postfix expression describing how to
15595 generate the (dynamic) address of the object onto the address stack.
15597 MODE is mode of the decl if this loc_descriptor is going to be used in
15598 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15599 allowed, VOIDmode otherwise.
15601 If we don't know how to describe it, return 0. */
15603 static dw_loc_descr_ref
15604 loc_descriptor (rtx rtl, machine_mode mode,
15605 enum var_init_status initialized)
15607 dw_loc_descr_ref loc_result = NULL;
15609 switch (GET_CODE (rtl))
15611 case SUBREG:
15612 /* The case of a subreg may arise when we have a local (register)
15613 variable or a formal (register) parameter which doesn't quite fill
15614 up an entire register. For now, just assume that it is
15615 legitimate to make the Dwarf info refer to the whole register which
15616 contains the given subreg. */
15617 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15618 loc_result = loc_descriptor (SUBREG_REG (rtl),
15619 GET_MODE (SUBREG_REG (rtl)), initialized);
15620 else
15621 goto do_default;
15622 break;
15624 case REG:
15625 loc_result = reg_loc_descriptor (rtl, initialized);
15626 break;
15628 case MEM:
15629 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15630 GET_MODE (rtl), initialized);
15631 if (loc_result == NULL)
15632 loc_result = tls_mem_loc_descriptor (rtl);
15633 if (loc_result == NULL)
15635 rtx new_rtl = avoid_constant_pool_reference (rtl);
15636 if (new_rtl != rtl)
15637 loc_result = loc_descriptor (new_rtl, mode, initialized);
15639 break;
15641 case CONCAT:
15642 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15643 initialized);
15644 break;
15646 case CONCATN:
15647 loc_result = concatn_loc_descriptor (rtl, initialized);
15648 break;
15650 case VAR_LOCATION:
15651 /* Single part. */
15652 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15654 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15655 if (GET_CODE (loc) == EXPR_LIST)
15656 loc = XEXP (loc, 0);
15657 loc_result = loc_descriptor (loc, mode, initialized);
15658 break;
15661 rtl = XEXP (rtl, 1);
15662 /* FALLTHRU */
15664 case PARALLEL:
15666 rtvec par_elems = XVEC (rtl, 0);
15667 int num_elem = GET_NUM_ELEM (par_elems);
15668 machine_mode mode;
15669 int i;
15671 /* Create the first one, so we have something to add to. */
15672 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15673 VOIDmode, initialized);
15674 if (loc_result == NULL)
15675 return NULL;
15676 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15677 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15678 for (i = 1; i < num_elem; i++)
15680 dw_loc_descr_ref temp;
15682 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15683 VOIDmode, initialized);
15684 if (temp == NULL)
15685 return NULL;
15686 add_loc_descr (&loc_result, temp);
15687 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15688 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15691 break;
15693 case CONST_INT:
15694 if (mode != VOIDmode && mode != BLKmode)
15695 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15696 INTVAL (rtl));
15697 break;
15699 case CONST_DOUBLE:
15700 if (mode == VOIDmode)
15701 mode = GET_MODE (rtl);
15703 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15705 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15707 /* Note that a CONST_DOUBLE rtx could represent either an integer
15708 or a floating-point constant. A CONST_DOUBLE is used whenever
15709 the constant requires more than one word in order to be
15710 adequately represented. We output CONST_DOUBLEs as blocks. */
15711 loc_result = new_loc_descr (DW_OP_implicit_value,
15712 GET_MODE_SIZE (mode), 0);
15713 #if TARGET_SUPPORTS_WIDE_INT == 0
15714 if (!SCALAR_FLOAT_MODE_P (mode))
15716 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15717 loc_result->dw_loc_oprnd2.v.val_double
15718 = rtx_to_double_int (rtl);
15720 else
15721 #endif
15723 unsigned int length = GET_MODE_SIZE (mode);
15724 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15726 insert_float (rtl, array);
15727 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15728 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15729 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15730 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15733 break;
15735 case CONST_WIDE_INT:
15736 if (mode == VOIDmode)
15737 mode = GET_MODE (rtl);
15739 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15741 loc_result = new_loc_descr (DW_OP_implicit_value,
15742 GET_MODE_SIZE (mode), 0);
15743 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15744 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15745 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15747 break;
15749 case CONST_VECTOR:
15750 if (mode == VOIDmode)
15751 mode = GET_MODE (rtl);
15753 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15755 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15756 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15757 unsigned char *array
15758 = ggc_vec_alloc<unsigned char> (length * elt_size);
15759 unsigned int i;
15760 unsigned char *p;
15761 machine_mode imode = GET_MODE_INNER (mode);
15763 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15764 switch (GET_MODE_CLASS (mode))
15766 case MODE_VECTOR_INT:
15767 for (i = 0, p = array; i < length; i++, p += elt_size)
15769 rtx elt = CONST_VECTOR_ELT (rtl, i);
15770 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15772 break;
15774 case MODE_VECTOR_FLOAT:
15775 for (i = 0, p = array; i < length; i++, p += elt_size)
15777 rtx elt = CONST_VECTOR_ELT (rtl, i);
15778 insert_float (elt, p);
15780 break;
15782 default:
15783 gcc_unreachable ();
15786 loc_result = new_loc_descr (DW_OP_implicit_value,
15787 length * elt_size, 0);
15788 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15789 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15790 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15791 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15793 break;
15795 case CONST:
15796 if (mode == VOIDmode
15797 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15798 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15799 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15801 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15802 break;
15804 /* FALLTHROUGH */
15805 case SYMBOL_REF:
15806 if (!const_ok_for_output (rtl))
15807 break;
15808 /* FALLTHROUGH */
15809 case LABEL_REF:
15810 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15811 && (dwarf_version >= 4 || !dwarf_strict))
15813 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15814 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15815 vec_safe_push (used_rtx_array, rtl);
15817 break;
15819 case DEBUG_IMPLICIT_PTR:
15820 loc_result = implicit_ptr_descriptor (rtl, 0);
15821 break;
15823 case PLUS:
15824 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15825 && CONST_INT_P (XEXP (rtl, 1)))
15827 loc_result
15828 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15829 break;
15831 /* FALLTHRU */
15832 do_default:
15833 default:
15834 if ((SCALAR_INT_MODE_P (mode)
15835 && GET_MODE (rtl) == mode
15836 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15837 && dwarf_version >= 4)
15838 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15840 /* Value expression. */
15841 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15842 if (loc_result)
15843 add_loc_descr (&loc_result,
15844 new_loc_descr (DW_OP_stack_value, 0, 0));
15846 break;
15849 return loc_result;
15852 /* We need to figure out what section we should use as the base for the
15853 address ranges where a given location is valid.
15854 1. If this particular DECL has a section associated with it, use that.
15855 2. If this function has a section associated with it, use that.
15856 3. Otherwise, use the text section.
15857 XXX: If you split a variable across multiple sections, we won't notice. */
15859 static const char *
15860 secname_for_decl (const_tree decl)
15862 const char *secname;
15864 if (VAR_OR_FUNCTION_DECL_P (decl)
15865 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15866 && DECL_SECTION_NAME (decl))
15867 secname = DECL_SECTION_NAME (decl);
15868 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15869 secname = DECL_SECTION_NAME (current_function_decl);
15870 else if (cfun && in_cold_section_p)
15871 secname = crtl->subsections.cold_section_label;
15872 else
15873 secname = text_section_label;
15875 return secname;
15878 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15880 static bool
15881 decl_by_reference_p (tree decl)
15883 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15884 || VAR_P (decl))
15885 && DECL_BY_REFERENCE (decl));
15888 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15889 for VARLOC. */
15891 static dw_loc_descr_ref
15892 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15893 enum var_init_status initialized)
15895 int have_address = 0;
15896 dw_loc_descr_ref descr;
15897 machine_mode mode;
15899 if (want_address != 2)
15901 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15902 /* Single part. */
15903 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15905 varloc = PAT_VAR_LOCATION_LOC (varloc);
15906 if (GET_CODE (varloc) == EXPR_LIST)
15907 varloc = XEXP (varloc, 0);
15908 mode = GET_MODE (varloc);
15909 if (MEM_P (varloc))
15911 rtx addr = XEXP (varloc, 0);
15912 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15913 mode, initialized);
15914 if (descr)
15915 have_address = 1;
15916 else
15918 rtx x = avoid_constant_pool_reference (varloc);
15919 if (x != varloc)
15920 descr = mem_loc_descriptor (x, mode, VOIDmode,
15921 initialized);
15924 else
15925 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15927 else
15928 return 0;
15930 else
15932 if (GET_CODE (varloc) == VAR_LOCATION)
15933 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15934 else
15935 mode = DECL_MODE (loc);
15936 descr = loc_descriptor (varloc, mode, initialized);
15937 have_address = 1;
15940 if (!descr)
15941 return 0;
15943 if (want_address == 2 && !have_address
15944 && (dwarf_version >= 4 || !dwarf_strict))
15946 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15948 expansion_failed (loc, NULL_RTX,
15949 "DWARF address size mismatch");
15950 return 0;
15952 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15953 have_address = 1;
15955 /* Show if we can't fill the request for an address. */
15956 if (want_address && !have_address)
15958 expansion_failed (loc, NULL_RTX,
15959 "Want address and only have value");
15960 return 0;
15963 /* If we've got an address and don't want one, dereference. */
15964 if (!want_address && have_address)
15966 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15967 enum dwarf_location_atom op;
15969 if (size > DWARF2_ADDR_SIZE || size == -1)
15971 expansion_failed (loc, NULL_RTX,
15972 "DWARF address size mismatch");
15973 return 0;
15975 else if (size == DWARF2_ADDR_SIZE)
15976 op = DW_OP_deref;
15977 else
15978 op = DW_OP_deref_size;
15980 add_loc_descr (&descr, new_loc_descr (op, size, 0));
15983 return descr;
15986 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15987 if it is not possible. */
15989 static dw_loc_descr_ref
15990 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15992 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15993 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15994 else if (dwarf_version >= 3 || !dwarf_strict)
15995 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15996 else
15997 return NULL;
16000 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16001 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16003 static dw_loc_descr_ref
16004 dw_sra_loc_expr (tree decl, rtx loc)
16006 rtx p;
16007 unsigned HOST_WIDE_INT padsize = 0;
16008 dw_loc_descr_ref descr, *descr_tail;
16009 unsigned HOST_WIDE_INT decl_size;
16010 rtx varloc;
16011 enum var_init_status initialized;
16013 if (DECL_SIZE (decl) == NULL
16014 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16015 return NULL;
16017 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16018 descr = NULL;
16019 descr_tail = &descr;
16021 for (p = loc; p; p = XEXP (p, 1))
16023 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16024 rtx loc_note = *decl_piece_varloc_ptr (p);
16025 dw_loc_descr_ref cur_descr;
16026 dw_loc_descr_ref *tail, last = NULL;
16027 unsigned HOST_WIDE_INT opsize = 0;
16029 if (loc_note == NULL_RTX
16030 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16032 padsize += bitsize;
16033 continue;
16035 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16036 varloc = NOTE_VAR_LOCATION (loc_note);
16037 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16038 if (cur_descr == NULL)
16040 padsize += bitsize;
16041 continue;
16044 /* Check that cur_descr either doesn't use
16045 DW_OP_*piece operations, or their sum is equal
16046 to bitsize. Otherwise we can't embed it. */
16047 for (tail = &cur_descr; *tail != NULL;
16048 tail = &(*tail)->dw_loc_next)
16049 if ((*tail)->dw_loc_opc == DW_OP_piece)
16051 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16052 * BITS_PER_UNIT;
16053 last = *tail;
16055 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16057 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16058 last = *tail;
16061 if (last != NULL && opsize != bitsize)
16063 padsize += bitsize;
16064 /* Discard the current piece of the descriptor and release any
16065 addr_table entries it uses. */
16066 remove_loc_list_addr_table_entries (cur_descr);
16067 continue;
16070 /* If there is a hole, add DW_OP_*piece after empty DWARF
16071 expression, which means that those bits are optimized out. */
16072 if (padsize)
16074 if (padsize > decl_size)
16076 remove_loc_list_addr_table_entries (cur_descr);
16077 goto discard_descr;
16079 decl_size -= padsize;
16080 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16081 if (*descr_tail == NULL)
16083 remove_loc_list_addr_table_entries (cur_descr);
16084 goto discard_descr;
16086 descr_tail = &(*descr_tail)->dw_loc_next;
16087 padsize = 0;
16089 *descr_tail = cur_descr;
16090 descr_tail = tail;
16091 if (bitsize > decl_size)
16092 goto discard_descr;
16093 decl_size -= bitsize;
16094 if (last == NULL)
16096 HOST_WIDE_INT offset = 0;
16097 if (GET_CODE (varloc) == VAR_LOCATION
16098 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16100 varloc = PAT_VAR_LOCATION_LOC (varloc);
16101 if (GET_CODE (varloc) == EXPR_LIST)
16102 varloc = XEXP (varloc, 0);
16106 if (GET_CODE (varloc) == CONST
16107 || GET_CODE (varloc) == SIGN_EXTEND
16108 || GET_CODE (varloc) == ZERO_EXTEND)
16109 varloc = XEXP (varloc, 0);
16110 else if (GET_CODE (varloc) == SUBREG)
16111 varloc = SUBREG_REG (varloc);
16112 else
16113 break;
16115 while (1);
16116 /* DW_OP_bit_size offset should be zero for register
16117 or implicit location descriptions and empty location
16118 descriptions, but for memory addresses needs big endian
16119 adjustment. */
16120 if (MEM_P (varloc))
16122 unsigned HOST_WIDE_INT memsize
16123 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16124 if (memsize != bitsize)
16126 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16127 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16128 goto discard_descr;
16129 if (memsize < bitsize)
16130 goto discard_descr;
16131 if (BITS_BIG_ENDIAN)
16132 offset = memsize - bitsize;
16136 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16137 if (*descr_tail == NULL)
16138 goto discard_descr;
16139 descr_tail = &(*descr_tail)->dw_loc_next;
16143 /* If there were any non-empty expressions, add padding till the end of
16144 the decl. */
16145 if (descr != NULL && decl_size != 0)
16147 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16148 if (*descr_tail == NULL)
16149 goto discard_descr;
16151 return descr;
16153 discard_descr:
16154 /* Discard the descriptor and release any addr_table entries it uses. */
16155 remove_loc_list_addr_table_entries (descr);
16156 return NULL;
16159 /* Return the dwarf representation of the location list LOC_LIST of
16160 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16161 function. */
16163 static dw_loc_list_ref
16164 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16166 const char *endname, *secname;
16167 rtx varloc;
16168 enum var_init_status initialized;
16169 struct var_loc_node *node;
16170 dw_loc_descr_ref descr;
16171 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16172 dw_loc_list_ref list = NULL;
16173 dw_loc_list_ref *listp = &list;
16175 /* Now that we know what section we are using for a base,
16176 actually construct the list of locations.
16177 The first location information is what is passed to the
16178 function that creates the location list, and the remaining
16179 locations just get added on to that list.
16180 Note that we only know the start address for a location
16181 (IE location changes), so to build the range, we use
16182 the range [current location start, next location start].
16183 This means we have to special case the last node, and generate
16184 a range of [last location start, end of function label]. */
16186 secname = secname_for_decl (decl);
16188 for (node = loc_list->first; node; node = node->next)
16189 if (GET_CODE (node->loc) == EXPR_LIST
16190 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16192 if (GET_CODE (node->loc) == EXPR_LIST)
16194 /* This requires DW_OP_{,bit_}piece, which is not usable
16195 inside DWARF expressions. */
16196 if (want_address != 2)
16197 continue;
16198 descr = dw_sra_loc_expr (decl, node->loc);
16199 if (descr == NULL)
16200 continue;
16202 else
16204 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16205 varloc = NOTE_VAR_LOCATION (node->loc);
16206 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16208 if (descr)
16210 bool range_across_switch = false;
16211 /* If section switch happens in between node->label
16212 and node->next->label (or end of function) and
16213 we can't emit it as a single entry list,
16214 emit two ranges, first one ending at the end
16215 of first partition and second one starting at the
16216 beginning of second partition. */
16217 if (node == loc_list->last_before_switch
16218 && (node != loc_list->first || loc_list->first->next)
16219 && current_function_decl)
16221 endname = cfun->fde->dw_fde_end;
16222 range_across_switch = true;
16224 /* The variable has a location between NODE->LABEL and
16225 NODE->NEXT->LABEL. */
16226 else if (node->next)
16227 endname = node->next->label;
16228 /* If the variable has a location at the last label
16229 it keeps its location until the end of function. */
16230 else if (!current_function_decl)
16231 endname = text_end_label;
16232 else
16234 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16235 current_function_funcdef_no);
16236 endname = ggc_strdup (label_id);
16239 *listp = new_loc_list (descr, node->label, endname, secname);
16240 if (TREE_CODE (decl) == PARM_DECL
16241 && node == loc_list->first
16242 && NOTE_P (node->loc)
16243 && strcmp (node->label, endname) == 0)
16244 (*listp)->force = true;
16245 listp = &(*listp)->dw_loc_next;
16247 if (range_across_switch)
16249 if (GET_CODE (node->loc) == EXPR_LIST)
16250 descr = dw_sra_loc_expr (decl, node->loc);
16251 else
16253 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16254 varloc = NOTE_VAR_LOCATION (node->loc);
16255 descr = dw_loc_list_1 (decl, varloc, want_address,
16256 initialized);
16258 gcc_assert (descr);
16259 /* The variable has a location between NODE->LABEL and
16260 NODE->NEXT->LABEL. */
16261 if (node->next)
16262 endname = node->next->label;
16263 else
16264 endname = cfun->fde->dw_fde_second_end;
16265 *listp = new_loc_list (descr,
16266 cfun->fde->dw_fde_second_begin,
16267 endname, secname);
16268 listp = &(*listp)->dw_loc_next;
16273 /* Try to avoid the overhead of a location list emitting a location
16274 expression instead, but only if we didn't have more than one
16275 location entry in the first place. If some entries were not
16276 representable, we don't want to pretend a single entry that was
16277 applies to the entire scope in which the variable is
16278 available. */
16279 if (list && loc_list->first->next)
16280 gen_llsym (list);
16282 return list;
16285 /* Return if the loc_list has only single element and thus can be represented
16286 as location description. */
16288 static bool
16289 single_element_loc_list_p (dw_loc_list_ref list)
16291 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16292 return !list->ll_symbol;
16295 /* To each location in list LIST add loc descr REF. */
16297 static void
16298 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16300 dw_loc_descr_ref copy;
16301 add_loc_descr (&list->expr, ref);
16302 list = list->dw_loc_next;
16303 while (list)
16305 copy = ggc_alloc<dw_loc_descr_node> ();
16306 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16307 add_loc_descr (&list->expr, copy);
16308 while (copy->dw_loc_next)
16310 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
16311 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16312 copy->dw_loc_next = new_copy;
16313 copy = new_copy;
16315 list = list->dw_loc_next;
16319 /* Given two lists RET and LIST
16320 produce location list that is result of adding expression in LIST
16321 to expression in RET on each position in program.
16322 Might be destructive on both RET and LIST.
16324 TODO: We handle only simple cases of RET or LIST having at most one
16325 element. General case would inolve sorting the lists in program order
16326 and merging them that will need some additional work.
16327 Adding that will improve quality of debug info especially for SRA-ed
16328 structures. */
16330 static void
16331 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16333 if (!list)
16334 return;
16335 if (!*ret)
16337 *ret = list;
16338 return;
16340 if (!list->dw_loc_next)
16342 add_loc_descr_to_each (*ret, list->expr);
16343 return;
16345 if (!(*ret)->dw_loc_next)
16347 add_loc_descr_to_each (list, (*ret)->expr);
16348 *ret = list;
16349 return;
16351 expansion_failed (NULL_TREE, NULL_RTX,
16352 "Don't know how to merge two non-trivial"
16353 " location lists.\n");
16354 *ret = NULL;
16355 return;
16358 /* LOC is constant expression. Try a luck, look it up in constant
16359 pool and return its loc_descr of its address. */
16361 static dw_loc_descr_ref
16362 cst_pool_loc_descr (tree loc)
16364 /* Get an RTL for this, if something has been emitted. */
16365 rtx rtl = lookup_constant_def (loc);
16367 if (!rtl || !MEM_P (rtl))
16369 gcc_assert (!rtl);
16370 return 0;
16372 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16374 /* TODO: We might get more coverage if we was actually delaying expansion
16375 of all expressions till end of compilation when constant pools are fully
16376 populated. */
16377 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16379 expansion_failed (loc, NULL_RTX,
16380 "CST value in contant pool but not marked.");
16381 return 0;
16383 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16384 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16387 /* Return dw_loc_list representing address of addr_expr LOC
16388 by looking for inner INDIRECT_REF expression and turning
16389 it into simple arithmetics.
16391 See loc_list_from_tree for the meaning of CONTEXT. */
16393 static dw_loc_list_ref
16394 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16395 loc_descr_context *context)
16397 tree obj, offset;
16398 HOST_WIDE_INT bitsize, bitpos, bytepos;
16399 machine_mode mode;
16400 int unsignedp, reversep, volatilep = 0;
16401 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16403 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16404 &bitsize, &bitpos, &offset, &mode,
16405 &unsignedp, &reversep, &volatilep);
16406 STRIP_NOPS (obj);
16407 if (bitpos % BITS_PER_UNIT)
16409 expansion_failed (loc, NULL_RTX, "bitfield access");
16410 return 0;
16412 if (!INDIRECT_REF_P (obj))
16414 expansion_failed (obj,
16415 NULL_RTX, "no indirect ref in inner refrence");
16416 return 0;
16418 if (!offset && !bitpos)
16419 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16420 context);
16421 else if (toplev
16422 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16423 && (dwarf_version >= 4 || !dwarf_strict))
16425 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16426 if (!list_ret)
16427 return 0;
16428 if (offset)
16430 /* Variable offset. */
16431 list_ret1 = loc_list_from_tree (offset, 0, context);
16432 if (list_ret1 == 0)
16433 return 0;
16434 add_loc_list (&list_ret, list_ret1);
16435 if (!list_ret)
16436 return 0;
16437 add_loc_descr_to_each (list_ret,
16438 new_loc_descr (DW_OP_plus, 0, 0));
16440 bytepos = bitpos / BITS_PER_UNIT;
16441 if (bytepos > 0)
16442 add_loc_descr_to_each (list_ret,
16443 new_loc_descr (DW_OP_plus_uconst,
16444 bytepos, 0));
16445 else if (bytepos < 0)
16446 loc_list_plus_const (list_ret, bytepos);
16447 add_loc_descr_to_each (list_ret,
16448 new_loc_descr (DW_OP_stack_value, 0, 0));
16450 return list_ret;
16453 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16454 all operations from LOC are nops, move to the last one. Insert in NOPS all
16455 operations that are skipped. */
16457 static void
16458 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16459 hash_set<dw_loc_descr_ref> &nops)
16461 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16463 nops.add (loc);
16464 loc = loc->dw_loc_next;
16468 /* Helper for loc_descr_without_nops: free the location description operation
16469 P. */
16471 bool
16472 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16474 ggc_free (loc);
16475 return true;
16478 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16479 finishes LOC. */
16481 static void
16482 loc_descr_without_nops (dw_loc_descr_ref &loc)
16484 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16485 return;
16487 /* Set of all DW_OP_nop operations we remove. */
16488 hash_set<dw_loc_descr_ref> nops;
16490 /* First, strip all prefix NOP operations in order to keep the head of the
16491 operations list. */
16492 loc_descr_to_next_no_nop (loc, nops);
16494 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16496 /* For control flow operations: strip "prefix" nops in destination
16497 labels. */
16498 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16499 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16500 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16501 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16503 /* Do the same for the operations that follow, then move to the next
16504 iteration. */
16505 if (cur->dw_loc_next != NULL)
16506 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16507 cur = cur->dw_loc_next;
16510 nops.traverse<void *, free_loc_descr> (NULL);
16514 struct dwarf_procedure_info;
16516 /* Helper structure for location descriptions generation. */
16517 struct loc_descr_context
16519 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16520 NULL_TREE if DW_OP_push_object_address in invalid for this location
16521 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16522 tree context_type;
16523 /* The ..._DECL node that should be translated as a
16524 DW_OP_push_object_address operation. */
16525 tree base_decl;
16526 /* Information about the DWARF procedure we are currently generating. NULL if
16527 we are not generating a DWARF procedure. */
16528 struct dwarf_procedure_info *dpi;
16529 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16530 by consumer. Used for DW_TAG_generic_subrange attributes. */
16531 bool placeholder_arg;
16532 /* True if PLACEHOLDER_EXPR has been seen. */
16533 bool placeholder_seen;
16536 /* DWARF procedures generation
16538 DWARF expressions (aka. location descriptions) are used to encode variable
16539 things such as sizes or offsets. Such computations can have redundant parts
16540 that can be factorized in order to reduce the size of the output debug
16541 information. This is the whole point of DWARF procedures.
16543 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16544 already factorized into functions ("size functions") in order to handle very
16545 big and complex types. Such functions are quite simple: they have integral
16546 arguments, they return an integral result and their body contains only a
16547 return statement with arithmetic expressions. This is the only kind of
16548 function we are interested in translating into DWARF procedures, here.
16550 DWARF expressions and DWARF procedure are executed using a stack, so we have
16551 to define some calling convention for them to interact. Let's say that:
16553 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16554 all arguments in reverse order (right-to-left) so that when the DWARF
16555 procedure execution starts, the first argument is the top of the stack.
16557 - Then, when returning, the DWARF procedure must have consumed all arguments
16558 on the stack, must have pushed the result and touched nothing else.
16560 - Each integral argument and the result are integral types can be hold in a
16561 single stack slot.
16563 - We call "frame offset" the number of stack slots that are "under DWARF
16564 procedure control": it includes the arguments slots, the temporaries and
16565 the result slot. Thus, it is equal to the number of arguments when the
16566 procedure execution starts and must be equal to one (the result) when it
16567 returns. */
16569 /* Helper structure used when generating operations for a DWARF procedure. */
16570 struct dwarf_procedure_info
16572 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16573 currently translated. */
16574 tree fndecl;
16575 /* The number of arguments FNDECL takes. */
16576 unsigned args_count;
16579 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16580 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16581 equate it to this DIE. */
16583 static dw_die_ref
16584 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16585 dw_die_ref parent_die)
16587 dw_die_ref dwarf_proc_die;
16589 if ((dwarf_version < 3 && dwarf_strict)
16590 || location == NULL)
16591 return NULL;
16593 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16594 if (fndecl)
16595 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16596 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16597 return dwarf_proc_die;
16600 /* Return whether TYPE is a supported type as a DWARF procedure argument
16601 type or return type (we handle only scalar types and pointer types that
16602 aren't wider than the DWARF expression evaluation stack. */
16604 static bool
16605 is_handled_procedure_type (tree type)
16607 return ((INTEGRAL_TYPE_P (type)
16608 || TREE_CODE (type) == OFFSET_TYPE
16609 || TREE_CODE (type) == POINTER_TYPE)
16610 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16613 /* Helper for resolve_args_picking: do the same but stop when coming across
16614 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16615 offset *before* evaluating the corresponding operation. */
16617 static bool
16618 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16619 struct dwarf_procedure_info *dpi,
16620 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16622 /* The "frame_offset" identifier is already used to name a macro... */
16623 unsigned frame_offset_ = initial_frame_offset;
16624 dw_loc_descr_ref l;
16626 for (l = loc; l != NULL;)
16628 bool existed;
16629 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16631 /* If we already met this node, there is nothing to compute anymore. */
16632 if (existed)
16634 /* Make sure that the stack size is consistent wherever the execution
16635 flow comes from. */
16636 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16637 break;
16639 l_frame_offset = frame_offset_;
16641 /* If needed, relocate the picking offset with respect to the frame
16642 offset. */
16643 if (l->frame_offset_rel)
16645 unsigned HOST_WIDE_INT off;
16646 switch (l->dw_loc_opc)
16648 case DW_OP_pick:
16649 off = l->dw_loc_oprnd1.v.val_unsigned;
16650 break;
16651 case DW_OP_dup:
16652 off = 0;
16653 break;
16654 case DW_OP_over:
16655 off = 1;
16656 break;
16657 default:
16658 gcc_unreachable ();
16660 /* frame_offset_ is the size of the current stack frame, including
16661 incoming arguments. Besides, the arguments are pushed
16662 right-to-left. Thus, in order to access the Nth argument from
16663 this operation node, the picking has to skip temporaries *plus*
16664 one stack slot per argument (0 for the first one, 1 for the second
16665 one, etc.).
16667 The targetted argument number (N) is already set as the operand,
16668 and the number of temporaries can be computed with:
16669 frame_offsets_ - dpi->args_count */
16670 off += frame_offset_ - dpi->args_count;
16672 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16673 if (off > 255)
16674 return false;
16676 if (off == 0)
16678 l->dw_loc_opc = DW_OP_dup;
16679 l->dw_loc_oprnd1.v.val_unsigned = 0;
16681 else if (off == 1)
16683 l->dw_loc_opc = DW_OP_over;
16684 l->dw_loc_oprnd1.v.val_unsigned = 0;
16686 else
16688 l->dw_loc_opc = DW_OP_pick;
16689 l->dw_loc_oprnd1.v.val_unsigned = off;
16693 /* Update frame_offset according to the effect the current operation has
16694 on the stack. */
16695 switch (l->dw_loc_opc)
16697 case DW_OP_deref:
16698 case DW_OP_swap:
16699 case DW_OP_rot:
16700 case DW_OP_abs:
16701 case DW_OP_neg:
16702 case DW_OP_not:
16703 case DW_OP_plus_uconst:
16704 case DW_OP_skip:
16705 case DW_OP_reg0:
16706 case DW_OP_reg1:
16707 case DW_OP_reg2:
16708 case DW_OP_reg3:
16709 case DW_OP_reg4:
16710 case DW_OP_reg5:
16711 case DW_OP_reg6:
16712 case DW_OP_reg7:
16713 case DW_OP_reg8:
16714 case DW_OP_reg9:
16715 case DW_OP_reg10:
16716 case DW_OP_reg11:
16717 case DW_OP_reg12:
16718 case DW_OP_reg13:
16719 case DW_OP_reg14:
16720 case DW_OP_reg15:
16721 case DW_OP_reg16:
16722 case DW_OP_reg17:
16723 case DW_OP_reg18:
16724 case DW_OP_reg19:
16725 case DW_OP_reg20:
16726 case DW_OP_reg21:
16727 case DW_OP_reg22:
16728 case DW_OP_reg23:
16729 case DW_OP_reg24:
16730 case DW_OP_reg25:
16731 case DW_OP_reg26:
16732 case DW_OP_reg27:
16733 case DW_OP_reg28:
16734 case DW_OP_reg29:
16735 case DW_OP_reg30:
16736 case DW_OP_reg31:
16737 case DW_OP_bregx:
16738 case DW_OP_piece:
16739 case DW_OP_deref_size:
16740 case DW_OP_nop:
16741 case DW_OP_bit_piece:
16742 case DW_OP_implicit_value:
16743 case DW_OP_stack_value:
16744 break;
16746 case DW_OP_addr:
16747 case DW_OP_const1u:
16748 case DW_OP_const1s:
16749 case DW_OP_const2u:
16750 case DW_OP_const2s:
16751 case DW_OP_const4u:
16752 case DW_OP_const4s:
16753 case DW_OP_const8u:
16754 case DW_OP_const8s:
16755 case DW_OP_constu:
16756 case DW_OP_consts:
16757 case DW_OP_dup:
16758 case DW_OP_over:
16759 case DW_OP_pick:
16760 case DW_OP_lit0:
16761 case DW_OP_lit1:
16762 case DW_OP_lit2:
16763 case DW_OP_lit3:
16764 case DW_OP_lit4:
16765 case DW_OP_lit5:
16766 case DW_OP_lit6:
16767 case DW_OP_lit7:
16768 case DW_OP_lit8:
16769 case DW_OP_lit9:
16770 case DW_OP_lit10:
16771 case DW_OP_lit11:
16772 case DW_OP_lit12:
16773 case DW_OP_lit13:
16774 case DW_OP_lit14:
16775 case DW_OP_lit15:
16776 case DW_OP_lit16:
16777 case DW_OP_lit17:
16778 case DW_OP_lit18:
16779 case DW_OP_lit19:
16780 case DW_OP_lit20:
16781 case DW_OP_lit21:
16782 case DW_OP_lit22:
16783 case DW_OP_lit23:
16784 case DW_OP_lit24:
16785 case DW_OP_lit25:
16786 case DW_OP_lit26:
16787 case DW_OP_lit27:
16788 case DW_OP_lit28:
16789 case DW_OP_lit29:
16790 case DW_OP_lit30:
16791 case DW_OP_lit31:
16792 case DW_OP_breg0:
16793 case DW_OP_breg1:
16794 case DW_OP_breg2:
16795 case DW_OP_breg3:
16796 case DW_OP_breg4:
16797 case DW_OP_breg5:
16798 case DW_OP_breg6:
16799 case DW_OP_breg7:
16800 case DW_OP_breg8:
16801 case DW_OP_breg9:
16802 case DW_OP_breg10:
16803 case DW_OP_breg11:
16804 case DW_OP_breg12:
16805 case DW_OP_breg13:
16806 case DW_OP_breg14:
16807 case DW_OP_breg15:
16808 case DW_OP_breg16:
16809 case DW_OP_breg17:
16810 case DW_OP_breg18:
16811 case DW_OP_breg19:
16812 case DW_OP_breg20:
16813 case DW_OP_breg21:
16814 case DW_OP_breg22:
16815 case DW_OP_breg23:
16816 case DW_OP_breg24:
16817 case DW_OP_breg25:
16818 case DW_OP_breg26:
16819 case DW_OP_breg27:
16820 case DW_OP_breg28:
16821 case DW_OP_breg29:
16822 case DW_OP_breg30:
16823 case DW_OP_breg31:
16824 case DW_OP_fbreg:
16825 case DW_OP_push_object_address:
16826 case DW_OP_call_frame_cfa:
16827 ++frame_offset_;
16828 break;
16830 case DW_OP_drop:
16831 case DW_OP_xderef:
16832 case DW_OP_and:
16833 case DW_OP_div:
16834 case DW_OP_minus:
16835 case DW_OP_mod:
16836 case DW_OP_mul:
16837 case DW_OP_or:
16838 case DW_OP_plus:
16839 case DW_OP_shl:
16840 case DW_OP_shr:
16841 case DW_OP_shra:
16842 case DW_OP_xor:
16843 case DW_OP_bra:
16844 case DW_OP_eq:
16845 case DW_OP_ge:
16846 case DW_OP_gt:
16847 case DW_OP_le:
16848 case DW_OP_lt:
16849 case DW_OP_ne:
16850 case DW_OP_regx:
16851 case DW_OP_xderef_size:
16852 --frame_offset_;
16853 break;
16855 case DW_OP_call2:
16856 case DW_OP_call4:
16857 case DW_OP_call_ref:
16859 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16860 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16862 if (stack_usage == NULL)
16863 return false;
16864 frame_offset_ += *stack_usage;
16865 break;
16868 case DW_OP_implicit_pointer:
16869 case DW_OP_entry_value:
16870 case DW_OP_const_type:
16871 case DW_OP_regval_type:
16872 case DW_OP_deref_type:
16873 case DW_OP_convert:
16874 case DW_OP_reinterpret:
16875 case DW_OP_form_tls_address:
16876 case DW_OP_GNU_push_tls_address:
16877 case DW_OP_GNU_uninit:
16878 case DW_OP_GNU_encoded_addr:
16879 case DW_OP_GNU_implicit_pointer:
16880 case DW_OP_GNU_entry_value:
16881 case DW_OP_GNU_const_type:
16882 case DW_OP_GNU_regval_type:
16883 case DW_OP_GNU_deref_type:
16884 case DW_OP_GNU_convert:
16885 case DW_OP_GNU_reinterpret:
16886 case DW_OP_GNU_parameter_ref:
16887 /* loc_list_from_tree will probably not output these operations for
16888 size functions, so assume they will not appear here. */
16889 /* Fall through... */
16891 default:
16892 gcc_unreachable ();
16895 /* Now, follow the control flow (except subroutine calls). */
16896 switch (l->dw_loc_opc)
16898 case DW_OP_bra:
16899 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16900 frame_offsets))
16901 return false;
16902 /* Fall through. */
16904 case DW_OP_skip:
16905 l = l->dw_loc_oprnd1.v.val_loc;
16906 break;
16908 case DW_OP_stack_value:
16909 return true;
16911 default:
16912 l = l->dw_loc_next;
16913 break;
16917 return true;
16920 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16921 operations) in order to resolve the operand of DW_OP_pick operations that
16922 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16923 offset *before* LOC is executed. Return if all relocations were
16924 successful. */
16926 static bool
16927 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16928 struct dwarf_procedure_info *dpi)
16930 /* Associate to all visited operations the frame offset *before* evaluating
16931 this operation. */
16932 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16934 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16935 frame_offsets);
16938 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16939 Return NULL if it is not possible. */
16941 static dw_die_ref
16942 function_to_dwarf_procedure (tree fndecl)
16944 struct loc_descr_context ctx;
16945 struct dwarf_procedure_info dpi;
16946 dw_die_ref dwarf_proc_die;
16947 tree tree_body = DECL_SAVED_TREE (fndecl);
16948 dw_loc_descr_ref loc_body, epilogue;
16950 tree cursor;
16951 unsigned i;
16953 /* Do not generate multiple DWARF procedures for the same function
16954 declaration. */
16955 dwarf_proc_die = lookup_decl_die (fndecl);
16956 if (dwarf_proc_die != NULL)
16957 return dwarf_proc_die;
16959 /* DWARF procedures are available starting with the DWARFv3 standard. */
16960 if (dwarf_version < 3 && dwarf_strict)
16961 return NULL;
16963 /* We handle only functions for which we still have a body, that return a
16964 supported type and that takes arguments with supported types. Note that
16965 there is no point translating functions that return nothing. */
16966 if (tree_body == NULL_TREE
16967 || DECL_RESULT (fndecl) == NULL_TREE
16968 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
16969 return NULL;
16971 for (cursor = DECL_ARGUMENTS (fndecl);
16972 cursor != NULL_TREE;
16973 cursor = TREE_CHAIN (cursor))
16974 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
16975 return NULL;
16977 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
16978 if (TREE_CODE (tree_body) != RETURN_EXPR)
16979 return NULL;
16980 tree_body = TREE_OPERAND (tree_body, 0);
16981 if (TREE_CODE (tree_body) != MODIFY_EXPR
16982 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
16983 return NULL;
16984 tree_body = TREE_OPERAND (tree_body, 1);
16986 /* Try to translate the body expression itself. Note that this will probably
16987 cause an infinite recursion if its call graph has a cycle. This is very
16988 unlikely for size functions, however, so don't bother with such things at
16989 the moment. */
16990 ctx.context_type = NULL_TREE;
16991 ctx.base_decl = NULL_TREE;
16992 ctx.dpi = &dpi;
16993 ctx.placeholder_arg = false;
16994 ctx.placeholder_seen = false;
16995 dpi.fndecl = fndecl;
16996 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
16997 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
16998 if (!loc_body)
16999 return NULL;
17001 /* After evaluating all operands in "loc_body", we should still have on the
17002 stack all arguments plus the desired function result (top of the stack).
17003 Generate code in order to keep only the result in our stack frame. */
17004 epilogue = NULL;
17005 for (i = 0; i < dpi.args_count; ++i)
17007 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17008 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17009 op_couple->dw_loc_next->dw_loc_next = epilogue;
17010 epilogue = op_couple;
17012 add_loc_descr (&loc_body, epilogue);
17013 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17014 return NULL;
17016 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17017 because they are considered useful. Now there is an epilogue, they are
17018 not anymore, so give it another try. */
17019 loc_descr_without_nops (loc_body);
17021 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17022 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17023 though, given that size functions do not come from source, so they should
17024 not have a dedicated DW_TAG_subprogram DIE. */
17025 dwarf_proc_die
17026 = new_dwarf_proc_die (loc_body, fndecl,
17027 get_context_die (DECL_CONTEXT (fndecl)));
17029 /* The called DWARF procedure consumes one stack slot per argument and
17030 returns one stack slot. */
17031 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17033 return dwarf_proc_die;
17037 /* Generate Dwarf location list representing LOC.
17038 If WANT_ADDRESS is false, expression computing LOC will be computed
17039 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17040 if WANT_ADDRESS is 2, expression computing address useable in location
17041 will be returned (i.e. DW_OP_reg can be used
17042 to refer to register values).
17044 CONTEXT provides information to customize the location descriptions
17045 generation. Its context_type field specifies what type is implicitly
17046 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17047 will not be generated.
17049 Its DPI field determines whether we are generating a DWARF expression for a
17050 DWARF procedure, so PARM_DECL references are processed specifically.
17052 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17053 and dpi fields were null. */
17055 static dw_loc_list_ref
17056 loc_list_from_tree_1 (tree loc, int want_address,
17057 struct loc_descr_context *context)
17059 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17060 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17061 int have_address = 0;
17062 enum dwarf_location_atom op;
17064 /* ??? Most of the time we do not take proper care for sign/zero
17065 extending the values properly. Hopefully this won't be a real
17066 problem... */
17068 if (context != NULL
17069 && context->base_decl == loc
17070 && want_address == 0)
17072 if (dwarf_version >= 3 || !dwarf_strict)
17073 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17074 NULL, NULL, NULL);
17075 else
17076 return NULL;
17079 switch (TREE_CODE (loc))
17081 case ERROR_MARK:
17082 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17083 return 0;
17085 case PLACEHOLDER_EXPR:
17086 /* This case involves extracting fields from an object to determine the
17087 position of other fields. It is supposed to appear only as the first
17088 operand of COMPONENT_REF nodes and to reference precisely the type
17089 that the context allows. */
17090 if (context != NULL
17091 && TREE_TYPE (loc) == context->context_type
17092 && want_address >= 1)
17094 if (dwarf_version >= 3 || !dwarf_strict)
17096 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17097 have_address = 1;
17098 break;
17100 else
17101 return NULL;
17103 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17104 the single argument passed by consumer. */
17105 else if (context != NULL
17106 && context->placeholder_arg
17107 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17108 && want_address == 0)
17110 ret = new_loc_descr (DW_OP_pick, 0, 0);
17111 ret->frame_offset_rel = 1;
17112 context->placeholder_seen = true;
17113 break;
17115 else
17116 expansion_failed (loc, NULL_RTX,
17117 "PLACEHOLDER_EXPR for an unexpected type");
17118 break;
17120 case CALL_EXPR:
17122 const int nargs = call_expr_nargs (loc);
17123 tree callee = get_callee_fndecl (loc);
17124 int i;
17125 dw_die_ref dwarf_proc;
17127 if (callee == NULL_TREE)
17128 goto call_expansion_failed;
17130 /* We handle only functions that return an integer. */
17131 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17132 goto call_expansion_failed;
17134 dwarf_proc = function_to_dwarf_procedure (callee);
17135 if (dwarf_proc == NULL)
17136 goto call_expansion_failed;
17138 /* Evaluate arguments right-to-left so that the first argument will
17139 be the top-most one on the stack. */
17140 for (i = nargs - 1; i >= 0; --i)
17142 dw_loc_descr_ref loc_descr
17143 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17144 context);
17146 if (loc_descr == NULL)
17147 goto call_expansion_failed;
17149 add_loc_descr (&ret, loc_descr);
17152 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17153 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17154 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17155 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17156 add_loc_descr (&ret, ret1);
17157 break;
17159 call_expansion_failed:
17160 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17161 /* There are no opcodes for these operations. */
17162 return 0;
17165 case PREINCREMENT_EXPR:
17166 case PREDECREMENT_EXPR:
17167 case POSTINCREMENT_EXPR:
17168 case POSTDECREMENT_EXPR:
17169 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17170 /* There are no opcodes for these operations. */
17171 return 0;
17173 case ADDR_EXPR:
17174 /* If we already want an address, see if there is INDIRECT_REF inside
17175 e.g. for &this->field. */
17176 if (want_address)
17178 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17179 (loc, want_address == 2, context);
17180 if (list_ret)
17181 have_address = 1;
17182 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17183 && (ret = cst_pool_loc_descr (loc)))
17184 have_address = 1;
17186 /* Otherwise, process the argument and look for the address. */
17187 if (!list_ret && !ret)
17188 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17189 else
17191 if (want_address)
17192 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17193 return NULL;
17195 break;
17197 case VAR_DECL:
17198 if (DECL_THREAD_LOCAL_P (loc))
17200 rtx rtl;
17201 enum dwarf_location_atom tls_op;
17202 enum dtprel_bool dtprel = dtprel_false;
17204 if (targetm.have_tls)
17206 /* If this is not defined, we have no way to emit the
17207 data. */
17208 if (!targetm.asm_out.output_dwarf_dtprel)
17209 return 0;
17211 /* The way DW_OP_GNU_push_tls_address is specified, we
17212 can only look up addresses of objects in the current
17213 module. We used DW_OP_addr as first op, but that's
17214 wrong, because DW_OP_addr is relocated by the debug
17215 info consumer, while DW_OP_GNU_push_tls_address
17216 operand shouldn't be. */
17217 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17218 return 0;
17219 dtprel = dtprel_true;
17220 /* We check for DWARF 5 here because gdb did not implement
17221 DW_OP_form_tls_address until after 7.12. */
17222 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17223 : DW_OP_GNU_push_tls_address);
17225 else
17227 if (!targetm.emutls.debug_form_tls_address
17228 || !(dwarf_version >= 3 || !dwarf_strict))
17229 return 0;
17230 /* We stuffed the control variable into the DECL_VALUE_EXPR
17231 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17232 no longer appear in gimple code. We used the control
17233 variable in specific so that we could pick it up here. */
17234 loc = DECL_VALUE_EXPR (loc);
17235 tls_op = DW_OP_form_tls_address;
17238 rtl = rtl_for_decl_location (loc);
17239 if (rtl == NULL_RTX)
17240 return 0;
17242 if (!MEM_P (rtl))
17243 return 0;
17244 rtl = XEXP (rtl, 0);
17245 if (! CONSTANT_P (rtl))
17246 return 0;
17248 ret = new_addr_loc_descr (rtl, dtprel);
17249 ret1 = new_loc_descr (tls_op, 0, 0);
17250 add_loc_descr (&ret, ret1);
17252 have_address = 1;
17253 break;
17255 /* FALLTHRU */
17257 case PARM_DECL:
17258 if (context != NULL && context->dpi != NULL
17259 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17261 /* We are generating code for a DWARF procedure and we want to access
17262 one of its arguments: find the appropriate argument offset and let
17263 the resolve_args_picking pass compute the offset that complies
17264 with the stack frame size. */
17265 unsigned i = 0;
17266 tree cursor;
17268 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17269 cursor != NULL_TREE && cursor != loc;
17270 cursor = TREE_CHAIN (cursor), ++i)
17272 /* If we are translating a DWARF procedure, all referenced parameters
17273 must belong to the current function. */
17274 gcc_assert (cursor != NULL_TREE);
17276 ret = new_loc_descr (DW_OP_pick, i, 0);
17277 ret->frame_offset_rel = 1;
17278 break;
17280 /* FALLTHRU */
17282 case RESULT_DECL:
17283 if (DECL_HAS_VALUE_EXPR_P (loc))
17284 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17285 want_address, context);
17286 /* FALLTHRU */
17288 case FUNCTION_DECL:
17290 rtx rtl;
17291 var_loc_list *loc_list = lookup_decl_loc (loc);
17293 if (loc_list && loc_list->first)
17295 list_ret = dw_loc_list (loc_list, loc, want_address);
17296 have_address = want_address != 0;
17297 break;
17299 rtl = rtl_for_decl_location (loc);
17300 if (rtl == NULL_RTX)
17302 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17303 return 0;
17305 else if (CONST_INT_P (rtl))
17307 HOST_WIDE_INT val = INTVAL (rtl);
17308 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17309 val &= GET_MODE_MASK (DECL_MODE (loc));
17310 ret = int_loc_descriptor (val);
17312 else if (GET_CODE (rtl) == CONST_STRING)
17314 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17315 return 0;
17317 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17318 ret = new_addr_loc_descr (rtl, dtprel_false);
17319 else
17321 machine_mode mode, mem_mode;
17323 /* Certain constructs can only be represented at top-level. */
17324 if (want_address == 2)
17326 ret = loc_descriptor (rtl, VOIDmode,
17327 VAR_INIT_STATUS_INITIALIZED);
17328 have_address = 1;
17330 else
17332 mode = GET_MODE (rtl);
17333 mem_mode = VOIDmode;
17334 if (MEM_P (rtl))
17336 mem_mode = mode;
17337 mode = get_address_mode (rtl);
17338 rtl = XEXP (rtl, 0);
17339 have_address = 1;
17341 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17342 VAR_INIT_STATUS_INITIALIZED);
17344 if (!ret)
17345 expansion_failed (loc, rtl,
17346 "failed to produce loc descriptor for rtl");
17349 break;
17351 case MEM_REF:
17352 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17354 have_address = 1;
17355 goto do_plus;
17357 /* Fallthru. */
17358 case INDIRECT_REF:
17359 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17360 have_address = 1;
17361 break;
17363 case TARGET_MEM_REF:
17364 case SSA_NAME:
17365 case DEBUG_EXPR_DECL:
17366 return NULL;
17368 case COMPOUND_EXPR:
17369 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17370 context);
17372 CASE_CONVERT:
17373 case VIEW_CONVERT_EXPR:
17374 case SAVE_EXPR:
17375 case MODIFY_EXPR:
17376 case NON_LVALUE_EXPR:
17377 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17378 context);
17380 case COMPONENT_REF:
17381 case BIT_FIELD_REF:
17382 case ARRAY_REF:
17383 case ARRAY_RANGE_REF:
17384 case REALPART_EXPR:
17385 case IMAGPART_EXPR:
17387 tree obj, offset;
17388 HOST_WIDE_INT bitsize, bitpos, bytepos;
17389 machine_mode mode;
17390 int unsignedp, reversep, volatilep = 0;
17392 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17393 &unsignedp, &reversep, &volatilep);
17395 gcc_assert (obj != loc);
17397 list_ret = loc_list_from_tree_1 (obj,
17398 want_address == 2
17399 && !bitpos && !offset ? 2 : 1,
17400 context);
17401 /* TODO: We can extract value of the small expression via shifting even
17402 for nonzero bitpos. */
17403 if (list_ret == 0)
17404 return 0;
17405 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17407 expansion_failed (loc, NULL_RTX,
17408 "bitfield access");
17409 return 0;
17412 if (offset != NULL_TREE)
17414 /* Variable offset. */
17415 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17416 if (list_ret1 == 0)
17417 return 0;
17418 add_loc_list (&list_ret, list_ret1);
17419 if (!list_ret)
17420 return 0;
17421 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17424 bytepos = bitpos / BITS_PER_UNIT;
17425 if (bytepos > 0)
17426 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17427 else if (bytepos < 0)
17428 loc_list_plus_const (list_ret, bytepos);
17430 have_address = 1;
17431 break;
17434 case INTEGER_CST:
17435 if ((want_address || !tree_fits_shwi_p (loc))
17436 && (ret = cst_pool_loc_descr (loc)))
17437 have_address = 1;
17438 else if (want_address == 2
17439 && tree_fits_shwi_p (loc)
17440 && (ret = address_of_int_loc_descriptor
17441 (int_size_in_bytes (TREE_TYPE (loc)),
17442 tree_to_shwi (loc))))
17443 have_address = 1;
17444 else if (tree_fits_shwi_p (loc))
17445 ret = int_loc_descriptor (tree_to_shwi (loc));
17446 else if (tree_fits_uhwi_p (loc))
17447 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17448 else
17450 expansion_failed (loc, NULL_RTX,
17451 "Integer operand is not host integer");
17452 return 0;
17454 break;
17456 case CONSTRUCTOR:
17457 case REAL_CST:
17458 case STRING_CST:
17459 case COMPLEX_CST:
17460 if ((ret = cst_pool_loc_descr (loc)))
17461 have_address = 1;
17462 else if (TREE_CODE (loc) == CONSTRUCTOR)
17464 tree type = TREE_TYPE (loc);
17465 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17466 unsigned HOST_WIDE_INT offset = 0;
17467 unsigned HOST_WIDE_INT cnt;
17468 constructor_elt *ce;
17470 if (TREE_CODE (type) == RECORD_TYPE)
17472 /* This is very limited, but it's enough to output
17473 pointers to member functions, as long as the
17474 referenced function is defined in the current
17475 translation unit. */
17476 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17478 tree val = ce->value;
17480 tree field = ce->index;
17482 if (val)
17483 STRIP_NOPS (val);
17485 if (!field || DECL_BIT_FIELD (field))
17487 expansion_failed (loc, NULL_RTX,
17488 "bitfield in record type constructor");
17489 size = offset = (unsigned HOST_WIDE_INT)-1;
17490 ret = NULL;
17491 break;
17494 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17495 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17496 gcc_assert (pos + fieldsize <= size);
17497 if (pos < offset)
17499 expansion_failed (loc, NULL_RTX,
17500 "out-of-order fields in record constructor");
17501 size = offset = (unsigned HOST_WIDE_INT)-1;
17502 ret = NULL;
17503 break;
17505 if (pos > offset)
17507 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17508 add_loc_descr (&ret, ret1);
17509 offset = pos;
17511 if (val && fieldsize != 0)
17513 ret1 = loc_descriptor_from_tree (val, want_address, context);
17514 if (!ret1)
17516 expansion_failed (loc, NULL_RTX,
17517 "unsupported expression in field");
17518 size = offset = (unsigned HOST_WIDE_INT)-1;
17519 ret = NULL;
17520 break;
17522 add_loc_descr (&ret, ret1);
17524 if (fieldsize)
17526 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17527 add_loc_descr (&ret, ret1);
17528 offset = pos + fieldsize;
17532 if (offset != size)
17534 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17535 add_loc_descr (&ret, ret1);
17536 offset = size;
17539 have_address = !!want_address;
17541 else
17542 expansion_failed (loc, NULL_RTX,
17543 "constructor of non-record type");
17545 else
17546 /* We can construct small constants here using int_loc_descriptor. */
17547 expansion_failed (loc, NULL_RTX,
17548 "constructor or constant not in constant pool");
17549 break;
17551 case TRUTH_AND_EXPR:
17552 case TRUTH_ANDIF_EXPR:
17553 case BIT_AND_EXPR:
17554 op = DW_OP_and;
17555 goto do_binop;
17557 case TRUTH_XOR_EXPR:
17558 case BIT_XOR_EXPR:
17559 op = DW_OP_xor;
17560 goto do_binop;
17562 case TRUTH_OR_EXPR:
17563 case TRUTH_ORIF_EXPR:
17564 case BIT_IOR_EXPR:
17565 op = DW_OP_or;
17566 goto do_binop;
17568 case FLOOR_DIV_EXPR:
17569 case CEIL_DIV_EXPR:
17570 case ROUND_DIV_EXPR:
17571 case TRUNC_DIV_EXPR:
17572 case EXACT_DIV_EXPR:
17573 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17574 return 0;
17575 op = DW_OP_div;
17576 goto do_binop;
17578 case MINUS_EXPR:
17579 op = DW_OP_minus;
17580 goto do_binop;
17582 case FLOOR_MOD_EXPR:
17583 case CEIL_MOD_EXPR:
17584 case ROUND_MOD_EXPR:
17585 case TRUNC_MOD_EXPR:
17586 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17588 op = DW_OP_mod;
17589 goto do_binop;
17591 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17592 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17593 if (list_ret == 0 || list_ret1 == 0)
17594 return 0;
17596 add_loc_list (&list_ret, list_ret1);
17597 if (list_ret == 0)
17598 return 0;
17599 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17600 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17601 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17602 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17603 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17604 break;
17606 case MULT_EXPR:
17607 op = DW_OP_mul;
17608 goto do_binop;
17610 case LSHIFT_EXPR:
17611 op = DW_OP_shl;
17612 goto do_binop;
17614 case RSHIFT_EXPR:
17615 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17616 goto do_binop;
17618 case POINTER_PLUS_EXPR:
17619 case PLUS_EXPR:
17620 do_plus:
17621 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17623 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17624 smarter to encode their opposite. The DW_OP_plus_uconst operation
17625 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17626 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17627 bytes, Y being the size of the operation that pushes the opposite
17628 of the addend. So let's choose the smallest representation. */
17629 const tree tree_addend = TREE_OPERAND (loc, 1);
17630 offset_int wi_addend;
17631 HOST_WIDE_INT shwi_addend;
17632 dw_loc_descr_ref loc_naddend;
17634 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17635 if (list_ret == 0)
17636 return 0;
17638 /* Try to get the literal to push. It is the opposite of the addend,
17639 so as we rely on wrapping during DWARF evaluation, first decode
17640 the literal as a "DWARF-sized" signed number. */
17641 wi_addend = wi::to_offset (tree_addend);
17642 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17643 shwi_addend = wi_addend.to_shwi ();
17644 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17645 ? int_loc_descriptor (-shwi_addend)
17646 : NULL;
17648 if (loc_naddend != NULL
17649 && ((unsigned) size_of_uleb128 (shwi_addend)
17650 > size_of_loc_descr (loc_naddend)))
17652 add_loc_descr_to_each (list_ret, loc_naddend);
17653 add_loc_descr_to_each (list_ret,
17654 new_loc_descr (DW_OP_minus, 0, 0));
17656 else
17658 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17660 loc_naddend = loc_cur;
17661 loc_cur = loc_cur->dw_loc_next;
17662 ggc_free (loc_naddend);
17664 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17666 break;
17669 op = DW_OP_plus;
17670 goto do_binop;
17672 case LE_EXPR:
17673 op = DW_OP_le;
17674 goto do_comp_binop;
17676 case GE_EXPR:
17677 op = DW_OP_ge;
17678 goto do_comp_binop;
17680 case LT_EXPR:
17681 op = DW_OP_lt;
17682 goto do_comp_binop;
17684 case GT_EXPR:
17685 op = DW_OP_gt;
17686 goto do_comp_binop;
17688 do_comp_binop:
17689 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17691 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17692 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17693 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17694 TREE_CODE (loc));
17695 break;
17697 else
17698 goto do_binop;
17700 case EQ_EXPR:
17701 op = DW_OP_eq;
17702 goto do_binop;
17704 case NE_EXPR:
17705 op = DW_OP_ne;
17706 goto do_binop;
17708 do_binop:
17709 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17710 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17711 if (list_ret == 0 || list_ret1 == 0)
17712 return 0;
17714 add_loc_list (&list_ret, list_ret1);
17715 if (list_ret == 0)
17716 return 0;
17717 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17718 break;
17720 case TRUTH_NOT_EXPR:
17721 case BIT_NOT_EXPR:
17722 op = DW_OP_not;
17723 goto do_unop;
17725 case ABS_EXPR:
17726 op = DW_OP_abs;
17727 goto do_unop;
17729 case NEGATE_EXPR:
17730 op = DW_OP_neg;
17731 goto do_unop;
17733 do_unop:
17734 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17735 if (list_ret == 0)
17736 return 0;
17738 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17739 break;
17741 case MIN_EXPR:
17742 case MAX_EXPR:
17744 const enum tree_code code =
17745 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17747 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17748 build2 (code, integer_type_node,
17749 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17750 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17753 /* fall through */
17755 case COND_EXPR:
17757 dw_loc_descr_ref lhs
17758 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17759 dw_loc_list_ref rhs
17760 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17761 dw_loc_descr_ref bra_node, jump_node, tmp;
17763 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17764 if (list_ret == 0 || lhs == 0 || rhs == 0)
17765 return 0;
17767 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17768 add_loc_descr_to_each (list_ret, bra_node);
17770 add_loc_list (&list_ret, rhs);
17771 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17772 add_loc_descr_to_each (list_ret, jump_node);
17774 add_loc_descr_to_each (list_ret, lhs);
17775 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17776 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17778 /* ??? Need a node to point the skip at. Use a nop. */
17779 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17780 add_loc_descr_to_each (list_ret, tmp);
17781 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17782 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17784 break;
17786 case FIX_TRUNC_EXPR:
17787 return 0;
17789 default:
17790 /* Leave front-end specific codes as simply unknown. This comes
17791 up, for instance, with the C STMT_EXPR. */
17792 if ((unsigned int) TREE_CODE (loc)
17793 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17795 expansion_failed (loc, NULL_RTX,
17796 "language specific tree node");
17797 return 0;
17800 /* Otherwise this is a generic code; we should just lists all of
17801 these explicitly. We forgot one. */
17802 if (flag_checking)
17803 gcc_unreachable ();
17805 /* In a release build, we want to degrade gracefully: better to
17806 generate incomplete debugging information than to crash. */
17807 return NULL;
17810 if (!ret && !list_ret)
17811 return 0;
17813 if (want_address == 2 && !have_address
17814 && (dwarf_version >= 4 || !dwarf_strict))
17816 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17818 expansion_failed (loc, NULL_RTX,
17819 "DWARF address size mismatch");
17820 return 0;
17822 if (ret)
17823 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17824 else
17825 add_loc_descr_to_each (list_ret,
17826 new_loc_descr (DW_OP_stack_value, 0, 0));
17827 have_address = 1;
17829 /* Show if we can't fill the request for an address. */
17830 if (want_address && !have_address)
17832 expansion_failed (loc, NULL_RTX,
17833 "Want address and only have value");
17834 return 0;
17837 gcc_assert (!ret || !list_ret);
17839 /* If we've got an address and don't want one, dereference. */
17840 if (!want_address && have_address)
17842 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17844 if (size > DWARF2_ADDR_SIZE || size == -1)
17846 expansion_failed (loc, NULL_RTX,
17847 "DWARF address size mismatch");
17848 return 0;
17850 else if (size == DWARF2_ADDR_SIZE)
17851 op = DW_OP_deref;
17852 else
17853 op = DW_OP_deref_size;
17855 if (ret)
17856 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17857 else
17858 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17860 if (ret)
17861 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17863 return list_ret;
17866 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17867 expressions. */
17869 static dw_loc_list_ref
17870 loc_list_from_tree (tree loc, int want_address,
17871 struct loc_descr_context *context)
17873 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17875 for (dw_loc_list_ref loc_cur = result;
17876 loc_cur != NULL; loc_cur =
17877 loc_cur->dw_loc_next)
17878 loc_descr_without_nops (loc_cur->expr);
17879 return result;
17882 /* Same as above but return only single location expression. */
17883 static dw_loc_descr_ref
17884 loc_descriptor_from_tree (tree loc, int want_address,
17885 struct loc_descr_context *context)
17887 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17888 if (!ret)
17889 return NULL;
17890 if (ret->dw_loc_next)
17892 expansion_failed (loc, NULL_RTX,
17893 "Location list where only loc descriptor needed");
17894 return NULL;
17896 return ret->expr;
17899 /* Given a value, round it up to the lowest multiple of `boundary'
17900 which is not less than the value itself. */
17902 static inline HOST_WIDE_INT
17903 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17905 return (((value + boundary - 1) / boundary) * boundary);
17908 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17909 pointer to the declared type for the relevant field variable, or return
17910 `integer_type_node' if the given node turns out to be an
17911 ERROR_MARK node. */
17913 static inline tree
17914 field_type (const_tree decl)
17916 tree type;
17918 if (TREE_CODE (decl) == ERROR_MARK)
17919 return integer_type_node;
17921 type = DECL_BIT_FIELD_TYPE (decl);
17922 if (type == NULL_TREE)
17923 type = TREE_TYPE (decl);
17925 return type;
17928 /* Given a pointer to a tree node, return the alignment in bits for
17929 it, or else return BITS_PER_WORD if the node actually turns out to
17930 be an ERROR_MARK node. */
17932 static inline unsigned
17933 simple_type_align_in_bits (const_tree type)
17935 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17938 static inline unsigned
17939 simple_decl_align_in_bits (const_tree decl)
17941 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17944 /* Return the result of rounding T up to ALIGN. */
17946 static inline offset_int
17947 round_up_to_align (const offset_int &t, unsigned int align)
17949 return wi::udiv_trunc (t + align - 1, align) * align;
17952 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
17953 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
17954 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
17955 if we fail to return the size in one of these two forms. */
17957 static dw_loc_descr_ref
17958 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
17960 tree tree_size;
17961 struct loc_descr_context ctx;
17963 /* Return a constant integer in priority, if possible. */
17964 *cst_size = int_size_in_bytes (type);
17965 if (*cst_size != -1)
17966 return NULL;
17968 ctx.context_type = const_cast<tree> (type);
17969 ctx.base_decl = NULL_TREE;
17970 ctx.dpi = NULL;
17971 ctx.placeholder_arg = false;
17972 ctx.placeholder_seen = false;
17974 type = TYPE_MAIN_VARIANT (type);
17975 tree_size = TYPE_SIZE_UNIT (type);
17976 return ((tree_size != NULL_TREE)
17977 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
17978 : NULL);
17981 /* Helper structure for RECORD_TYPE processing. */
17982 struct vlr_context
17984 /* Root RECORD_TYPE. It is needed to generate data member location
17985 descriptions in variable-length records (VLR), but also to cope with
17986 variants, which are composed of nested structures multiplexed with
17987 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
17988 function processing a FIELD_DECL, it is required to be non null. */
17989 tree struct_type;
17990 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
17991 QUAL_UNION_TYPE), this holds an expression that computes the offset for
17992 this variant part as part of the root record (in storage units). For
17993 regular records, it must be NULL_TREE. */
17994 tree variant_part_offset;
17997 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
17998 addressed byte of the "containing object" for the given FIELD_DECL. If
17999 possible, return a native constant through CST_OFFSET (in which case NULL is
18000 returned); otherwise return a DWARF expression that computes the offset.
18002 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18003 that offset is, either because the argument turns out to be a pointer to an
18004 ERROR_MARK node, or because the offset expression is too complex for us.
18006 CTX is required: see the comment for VLR_CONTEXT. */
18008 static dw_loc_descr_ref
18009 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18010 HOST_WIDE_INT *cst_offset)
18012 tree tree_result;
18013 dw_loc_list_ref loc_result;
18015 *cst_offset = 0;
18017 if (TREE_CODE (decl) == ERROR_MARK)
18018 return NULL;
18019 else
18020 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18022 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18023 case. */
18024 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18025 return NULL;
18027 #ifdef PCC_BITFIELD_TYPE_MATTERS
18028 /* We used to handle only constant offsets in all cases. Now, we handle
18029 properly dynamic byte offsets only when PCC bitfield type doesn't
18030 matter. */
18031 if (PCC_BITFIELD_TYPE_MATTERS
18032 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18034 offset_int object_offset_in_bits;
18035 offset_int object_offset_in_bytes;
18036 offset_int bitpos_int;
18037 tree type;
18038 tree field_size_tree;
18039 offset_int deepest_bitpos;
18040 offset_int field_size_in_bits;
18041 unsigned int type_align_in_bits;
18042 unsigned int decl_align_in_bits;
18043 offset_int type_size_in_bits;
18045 bitpos_int = wi::to_offset (bit_position (decl));
18046 type = field_type (decl);
18047 type_size_in_bits = offset_int_type_size_in_bits (type);
18048 type_align_in_bits = simple_type_align_in_bits (type);
18050 field_size_tree = DECL_SIZE (decl);
18052 /* The size could be unspecified if there was an error, or for
18053 a flexible array member. */
18054 if (!field_size_tree)
18055 field_size_tree = bitsize_zero_node;
18057 /* If the size of the field is not constant, use the type size. */
18058 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18059 field_size_in_bits = wi::to_offset (field_size_tree);
18060 else
18061 field_size_in_bits = type_size_in_bits;
18063 decl_align_in_bits = simple_decl_align_in_bits (decl);
18065 /* The GCC front-end doesn't make any attempt to keep track of the
18066 starting bit offset (relative to the start of the containing
18067 structure type) of the hypothetical "containing object" for a
18068 bit-field. Thus, when computing the byte offset value for the
18069 start of the "containing object" of a bit-field, we must deduce
18070 this information on our own. This can be rather tricky to do in
18071 some cases. For example, handling the following structure type
18072 definition when compiling for an i386/i486 target (which only
18073 aligns long long's to 32-bit boundaries) can be very tricky:
18075 struct S { int field1; long long field2:31; };
18077 Fortunately, there is a simple rule-of-thumb which can be used
18078 in such cases. When compiling for an i386/i486, GCC will
18079 allocate 8 bytes for the structure shown above. It decides to
18080 do this based upon one simple rule for bit-field allocation.
18081 GCC allocates each "containing object" for each bit-field at
18082 the first (i.e. lowest addressed) legitimate alignment boundary
18083 (based upon the required minimum alignment for the declared
18084 type of the field) which it can possibly use, subject to the
18085 condition that there is still enough available space remaining
18086 in the containing object (when allocated at the selected point)
18087 to fully accommodate all of the bits of the bit-field itself.
18089 This simple rule makes it obvious why GCC allocates 8 bytes for
18090 each object of the structure type shown above. When looking
18091 for a place to allocate the "containing object" for `field2',
18092 the compiler simply tries to allocate a 64-bit "containing
18093 object" at each successive 32-bit boundary (starting at zero)
18094 until it finds a place to allocate that 64- bit field such that
18095 at least 31 contiguous (and previously unallocated) bits remain
18096 within that selected 64 bit field. (As it turns out, for the
18097 example above, the compiler finds it is OK to allocate the
18098 "containing object" 64-bit field at bit-offset zero within the
18099 structure type.)
18101 Here we attempt to work backwards from the limited set of facts
18102 we're given, and we try to deduce from those facts, where GCC
18103 must have believed that the containing object started (within
18104 the structure type). The value we deduce is then used (by the
18105 callers of this routine) to generate DW_AT_location and
18106 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18107 the case of DW_AT_location, regular fields as well). */
18109 /* Figure out the bit-distance from the start of the structure to
18110 the "deepest" bit of the bit-field. */
18111 deepest_bitpos = bitpos_int + field_size_in_bits;
18113 /* This is the tricky part. Use some fancy footwork to deduce
18114 where the lowest addressed bit of the containing object must
18115 be. */
18116 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18118 /* Round up to type_align by default. This works best for
18119 bitfields. */
18120 object_offset_in_bits
18121 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18123 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18125 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18127 /* Round up to decl_align instead. */
18128 object_offset_in_bits
18129 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18132 object_offset_in_bytes
18133 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18134 if (ctx->variant_part_offset == NULL_TREE)
18136 *cst_offset = object_offset_in_bytes.to_shwi ();
18137 return NULL;
18139 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18141 else
18142 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18143 tree_result = byte_position (decl);
18145 if (ctx->variant_part_offset != NULL_TREE)
18146 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18147 ctx->variant_part_offset, tree_result);
18149 /* If the byte offset is a constant, it's simplier to handle a native
18150 constant rather than a DWARF expression. */
18151 if (TREE_CODE (tree_result) == INTEGER_CST)
18153 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18154 return NULL;
18156 struct loc_descr_context loc_ctx = {
18157 ctx->struct_type, /* context_type */
18158 NULL_TREE, /* base_decl */
18159 NULL, /* dpi */
18160 false, /* placeholder_arg */
18161 false /* placeholder_seen */
18163 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18165 /* We want a DWARF expression: abort if we only have a location list with
18166 multiple elements. */
18167 if (!loc_result || !single_element_loc_list_p (loc_result))
18168 return NULL;
18169 else
18170 return loc_result->expr;
18173 /* The following routines define various Dwarf attributes and any data
18174 associated with them. */
18176 /* Add a location description attribute value to a DIE.
18178 This emits location attributes suitable for whole variables and
18179 whole parameters. Note that the location attributes for struct fields are
18180 generated by the routine `data_member_location_attribute' below. */
18182 static inline void
18183 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18184 dw_loc_list_ref descr)
18186 if (descr == 0)
18187 return;
18188 if (single_element_loc_list_p (descr))
18189 add_AT_loc (die, attr_kind, descr->expr);
18190 else
18191 add_AT_loc_list (die, attr_kind, descr);
18194 /* Add DW_AT_accessibility attribute to DIE if needed. */
18196 static void
18197 add_accessibility_attribute (dw_die_ref die, tree decl)
18199 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18200 children, otherwise the default is DW_ACCESS_public. In DWARF2
18201 the default has always been DW_ACCESS_public. */
18202 if (TREE_PROTECTED (decl))
18203 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18204 else if (TREE_PRIVATE (decl))
18206 if (dwarf_version == 2
18207 || die->die_parent == NULL
18208 || die->die_parent->die_tag != DW_TAG_class_type)
18209 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18211 else if (dwarf_version > 2
18212 && die->die_parent
18213 && die->die_parent->die_tag == DW_TAG_class_type)
18214 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18217 /* Attach the specialized form of location attribute used for data members of
18218 struct and union types. In the special case of a FIELD_DECL node which
18219 represents a bit-field, the "offset" part of this special location
18220 descriptor must indicate the distance in bytes from the lowest-addressed
18221 byte of the containing struct or union type to the lowest-addressed byte of
18222 the "containing object" for the bit-field. (See the `field_byte_offset'
18223 function above).
18225 For any given bit-field, the "containing object" is a hypothetical object
18226 (of some integral or enum type) within which the given bit-field lives. The
18227 type of this hypothetical "containing object" is always the same as the
18228 declared type of the individual bit-field itself (for GCC anyway... the
18229 DWARF spec doesn't actually mandate this). Note that it is the size (in
18230 bytes) of the hypothetical "containing object" which will be given in the
18231 DW_AT_byte_size attribute for this bit-field. (See the
18232 `byte_size_attribute' function below.) It is also used when calculating the
18233 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18234 function below.)
18236 CTX is required: see the comment for VLR_CONTEXT. */
18238 static void
18239 add_data_member_location_attribute (dw_die_ref die,
18240 tree decl,
18241 struct vlr_context *ctx)
18243 HOST_WIDE_INT offset;
18244 dw_loc_descr_ref loc_descr = 0;
18246 if (TREE_CODE (decl) == TREE_BINFO)
18248 /* We're working on the TAG_inheritance for a base class. */
18249 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18251 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18252 aren't at a fixed offset from all (sub)objects of the same
18253 type. We need to extract the appropriate offset from our
18254 vtable. The following dwarf expression means
18256 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18258 This is specific to the V3 ABI, of course. */
18260 dw_loc_descr_ref tmp;
18262 /* Make a copy of the object address. */
18263 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18264 add_loc_descr (&loc_descr, tmp);
18266 /* Extract the vtable address. */
18267 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18268 add_loc_descr (&loc_descr, tmp);
18270 /* Calculate the address of the offset. */
18271 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18272 gcc_assert (offset < 0);
18274 tmp = int_loc_descriptor (-offset);
18275 add_loc_descr (&loc_descr, tmp);
18276 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18277 add_loc_descr (&loc_descr, tmp);
18279 /* Extract the offset. */
18280 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18281 add_loc_descr (&loc_descr, tmp);
18283 /* Add it to the object address. */
18284 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18285 add_loc_descr (&loc_descr, tmp);
18287 else
18288 offset = tree_to_shwi (BINFO_OFFSET (decl));
18290 else
18292 loc_descr = field_byte_offset (decl, ctx, &offset);
18294 /* If loc_descr is available then we know the field offset is dynamic.
18295 However, GDB does not handle dynamic field offsets very well at the
18296 moment. */
18297 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18299 loc_descr = NULL;
18300 offset = 0;
18303 /* Data member location evalutation starts with the base address on the
18304 stack. Compute the field offset and add it to this base address. */
18305 else if (loc_descr != NULL)
18306 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18309 if (! loc_descr)
18311 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18312 e.g. GDB only added support to it in November 2016. For DWARF5
18313 we need newer debug info consumers anyway. We might change this
18314 to dwarf_version >= 4 once most consumers catched up. */
18315 if (dwarf_version >= 5
18316 && TREE_CODE (decl) == FIELD_DECL
18317 && DECL_BIT_FIELD_TYPE (decl))
18319 tree off = bit_position (decl);
18320 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18322 remove_AT (die, DW_AT_byte_size);
18323 remove_AT (die, DW_AT_bit_offset);
18324 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18325 return;
18328 if (dwarf_version > 2)
18330 /* Don't need to output a location expression, just the constant. */
18331 if (offset < 0)
18332 add_AT_int (die, DW_AT_data_member_location, offset);
18333 else
18334 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18335 return;
18337 else
18339 enum dwarf_location_atom op;
18341 /* The DWARF2 standard says that we should assume that the structure
18342 address is already on the stack, so we can specify a structure
18343 field address by using DW_OP_plus_uconst. */
18344 op = DW_OP_plus_uconst;
18345 loc_descr = new_loc_descr (op, offset, 0);
18349 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18352 /* Writes integer values to dw_vec_const array. */
18354 static void
18355 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18357 while (size != 0)
18359 *dest++ = val & 0xff;
18360 val >>= 8;
18361 --size;
18365 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18367 static HOST_WIDE_INT
18368 extract_int (const unsigned char *src, unsigned int size)
18370 HOST_WIDE_INT val = 0;
18372 src += size;
18373 while (size != 0)
18375 val <<= 8;
18376 val |= *--src & 0xff;
18377 --size;
18379 return val;
18382 /* Writes wide_int values to dw_vec_const array. */
18384 static void
18385 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18387 int i;
18389 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18391 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18392 return;
18395 /* We'd have to extend this code to support odd sizes. */
18396 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18398 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18400 if (WORDS_BIG_ENDIAN)
18401 for (i = n - 1; i >= 0; i--)
18403 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18404 dest += sizeof (HOST_WIDE_INT);
18406 else
18407 for (i = 0; i < n; i++)
18409 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18410 dest += sizeof (HOST_WIDE_INT);
18414 /* Writes floating point values to dw_vec_const array. */
18416 static void
18417 insert_float (const_rtx rtl, unsigned char *array)
18419 long val[4];
18420 int i;
18422 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18424 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18425 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18427 insert_int (val[i], 4, array);
18428 array += 4;
18432 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18433 does not have a "location" either in memory or in a register. These
18434 things can arise in GNU C when a constant is passed as an actual parameter
18435 to an inlined function. They can also arise in C++ where declared
18436 constants do not necessarily get memory "homes". */
18438 static bool
18439 add_const_value_attribute (dw_die_ref die, rtx rtl)
18441 switch (GET_CODE (rtl))
18443 case CONST_INT:
18445 HOST_WIDE_INT val = INTVAL (rtl);
18447 if (val < 0)
18448 add_AT_int (die, DW_AT_const_value, val);
18449 else
18450 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18452 return true;
18454 case CONST_WIDE_INT:
18456 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18457 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18458 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18459 wide_int w = wi::zext (w1, prec);
18460 add_AT_wide (die, DW_AT_const_value, w);
18462 return true;
18464 case CONST_DOUBLE:
18465 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18466 floating-point constant. A CONST_DOUBLE is used whenever the
18467 constant requires more than one word in order to be adequately
18468 represented. */
18470 machine_mode mode = GET_MODE (rtl);
18472 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18473 add_AT_double (die, DW_AT_const_value,
18474 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18475 else
18477 unsigned int length = GET_MODE_SIZE (mode);
18478 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18480 insert_float (rtl, array);
18481 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18484 return true;
18486 case CONST_VECTOR:
18488 machine_mode mode = GET_MODE (rtl);
18489 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18490 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18491 unsigned char *array
18492 = ggc_vec_alloc<unsigned char> (length * elt_size);
18493 unsigned int i;
18494 unsigned char *p;
18495 machine_mode imode = GET_MODE_INNER (mode);
18497 switch (GET_MODE_CLASS (mode))
18499 case MODE_VECTOR_INT:
18500 for (i = 0, p = array; i < length; i++, p += elt_size)
18502 rtx elt = CONST_VECTOR_ELT (rtl, i);
18503 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18505 break;
18507 case MODE_VECTOR_FLOAT:
18508 for (i = 0, p = array; i < length; i++, p += elt_size)
18510 rtx elt = CONST_VECTOR_ELT (rtl, i);
18511 insert_float (elt, p);
18513 break;
18515 default:
18516 gcc_unreachable ();
18519 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18521 return true;
18523 case CONST_STRING:
18524 if (dwarf_version >= 4 || !dwarf_strict)
18526 dw_loc_descr_ref loc_result;
18527 resolve_one_addr (&rtl);
18528 rtl_addr:
18529 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18530 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18531 add_AT_loc (die, DW_AT_location, loc_result);
18532 vec_safe_push (used_rtx_array, rtl);
18533 return true;
18535 return false;
18537 case CONST:
18538 if (CONSTANT_P (XEXP (rtl, 0)))
18539 return add_const_value_attribute (die, XEXP (rtl, 0));
18540 /* FALLTHROUGH */
18541 case SYMBOL_REF:
18542 if (!const_ok_for_output (rtl))
18543 return false;
18544 /* FALLTHROUGH */
18545 case LABEL_REF:
18546 if (dwarf_version >= 4 || !dwarf_strict)
18547 goto rtl_addr;
18548 return false;
18550 case PLUS:
18551 /* In cases where an inlined instance of an inline function is passed
18552 the address of an `auto' variable (which is local to the caller) we
18553 can get a situation where the DECL_RTL of the artificial local
18554 variable (for the inlining) which acts as a stand-in for the
18555 corresponding formal parameter (of the inline function) will look
18556 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18557 exactly a compile-time constant expression, but it isn't the address
18558 of the (artificial) local variable either. Rather, it represents the
18559 *value* which the artificial local variable always has during its
18560 lifetime. We currently have no way to represent such quasi-constant
18561 values in Dwarf, so for now we just punt and generate nothing. */
18562 return false;
18564 case HIGH:
18565 case CONST_FIXED:
18566 return false;
18568 case MEM:
18569 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18570 && MEM_READONLY_P (rtl)
18571 && GET_MODE (rtl) == BLKmode)
18573 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18574 return true;
18576 return false;
18578 default:
18579 /* No other kinds of rtx should be possible here. */
18580 gcc_unreachable ();
18582 return false;
18585 /* Determine whether the evaluation of EXPR references any variables
18586 or functions which aren't otherwise used (and therefore may not be
18587 output). */
18588 static tree
18589 reference_to_unused (tree * tp, int * walk_subtrees,
18590 void * data ATTRIBUTE_UNUSED)
18592 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18593 *walk_subtrees = 0;
18595 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18596 && ! TREE_ASM_WRITTEN (*tp))
18597 return *tp;
18598 /* ??? The C++ FE emits debug information for using decls, so
18599 putting gcc_unreachable here falls over. See PR31899. For now
18600 be conservative. */
18601 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18602 return *tp;
18603 else if (VAR_P (*tp))
18605 varpool_node *node = varpool_node::get (*tp);
18606 if (!node || !node->definition)
18607 return *tp;
18609 else if (TREE_CODE (*tp) == FUNCTION_DECL
18610 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18612 /* The call graph machinery must have finished analyzing,
18613 optimizing and gimplifying the CU by now.
18614 So if *TP has no call graph node associated
18615 to it, it means *TP will not be emitted. */
18616 if (!cgraph_node::get (*tp))
18617 return *tp;
18619 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18620 return *tp;
18622 return NULL_TREE;
18625 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18626 for use in a later add_const_value_attribute call. */
18628 static rtx
18629 rtl_for_decl_init (tree init, tree type)
18631 rtx rtl = NULL_RTX;
18633 STRIP_NOPS (init);
18635 /* If a variable is initialized with a string constant without embedded
18636 zeros, build CONST_STRING. */
18637 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18639 tree enttype = TREE_TYPE (type);
18640 tree domain = TYPE_DOMAIN (type);
18641 machine_mode mode = TYPE_MODE (enttype);
18643 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18644 && domain
18645 && integer_zerop (TYPE_MIN_VALUE (domain))
18646 && compare_tree_int (TYPE_MAX_VALUE (domain),
18647 TREE_STRING_LENGTH (init) - 1) == 0
18648 && ((size_t) TREE_STRING_LENGTH (init)
18649 == strlen (TREE_STRING_POINTER (init)) + 1))
18651 rtl = gen_rtx_CONST_STRING (VOIDmode,
18652 ggc_strdup (TREE_STRING_POINTER (init)));
18653 rtl = gen_rtx_MEM (BLKmode, rtl);
18654 MEM_READONLY_P (rtl) = 1;
18657 /* Other aggregates, and complex values, could be represented using
18658 CONCAT: FIXME! */
18659 else if (AGGREGATE_TYPE_P (type)
18660 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18661 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18662 || TREE_CODE (type) == COMPLEX_TYPE)
18664 /* Vectors only work if their mode is supported by the target.
18665 FIXME: generic vectors ought to work too. */
18666 else if (TREE_CODE (type) == VECTOR_TYPE
18667 && !VECTOR_MODE_P (TYPE_MODE (type)))
18669 /* If the initializer is something that we know will expand into an
18670 immediate RTL constant, expand it now. We must be careful not to
18671 reference variables which won't be output. */
18672 else if (initializer_constant_valid_p (init, type)
18673 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18675 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18676 possible. */
18677 if (TREE_CODE (type) == VECTOR_TYPE)
18678 switch (TREE_CODE (init))
18680 case VECTOR_CST:
18681 break;
18682 case CONSTRUCTOR:
18683 if (TREE_CONSTANT (init))
18685 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18686 bool constant_p = true;
18687 tree value;
18688 unsigned HOST_WIDE_INT ix;
18690 /* Even when ctor is constant, it might contain non-*_CST
18691 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18692 belong into VECTOR_CST nodes. */
18693 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18694 if (!CONSTANT_CLASS_P (value))
18696 constant_p = false;
18697 break;
18700 if (constant_p)
18702 init = build_vector_from_ctor (type, elts);
18703 break;
18706 /* FALLTHRU */
18708 default:
18709 return NULL;
18712 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18714 /* If expand_expr returns a MEM, it wasn't immediate. */
18715 gcc_assert (!rtl || !MEM_P (rtl));
18718 return rtl;
18721 /* Generate RTL for the variable DECL to represent its location. */
18723 static rtx
18724 rtl_for_decl_location (tree decl)
18726 rtx rtl;
18728 /* Here we have to decide where we are going to say the parameter "lives"
18729 (as far as the debugger is concerned). We only have a couple of
18730 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18732 DECL_RTL normally indicates where the parameter lives during most of the
18733 activation of the function. If optimization is enabled however, this
18734 could be either NULL or else a pseudo-reg. Both of those cases indicate
18735 that the parameter doesn't really live anywhere (as far as the code
18736 generation parts of GCC are concerned) during most of the function's
18737 activation. That will happen (for example) if the parameter is never
18738 referenced within the function.
18740 We could just generate a location descriptor here for all non-NULL
18741 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18742 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18743 where DECL_RTL is NULL or is a pseudo-reg.
18745 Note however that we can only get away with using DECL_INCOMING_RTL as
18746 a backup substitute for DECL_RTL in certain limited cases. In cases
18747 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18748 we can be sure that the parameter was passed using the same type as it is
18749 declared to have within the function, and that its DECL_INCOMING_RTL
18750 points us to a place where a value of that type is passed.
18752 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18753 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18754 because in these cases DECL_INCOMING_RTL points us to a value of some
18755 type which is *different* from the type of the parameter itself. Thus,
18756 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18757 such cases, the debugger would end up (for example) trying to fetch a
18758 `float' from a place which actually contains the first part of a
18759 `double'. That would lead to really incorrect and confusing
18760 output at debug-time.
18762 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18763 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18764 are a couple of exceptions however. On little-endian machines we can
18765 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18766 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18767 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18768 when (on a little-endian machine) a non-prototyped function has a
18769 parameter declared to be of type `short' or `char'. In such cases,
18770 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18771 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18772 passed `int' value. If the debugger then uses that address to fetch
18773 a `short' or a `char' (on a little-endian machine) the result will be
18774 the correct data, so we allow for such exceptional cases below.
18776 Note that our goal here is to describe the place where the given formal
18777 parameter lives during most of the function's activation (i.e. between the
18778 end of the prologue and the start of the epilogue). We'll do that as best
18779 as we can. Note however that if the given formal parameter is modified
18780 sometime during the execution of the function, then a stack backtrace (at
18781 debug-time) will show the function as having been called with the *new*
18782 value rather than the value which was originally passed in. This happens
18783 rarely enough that it is not a major problem, but it *is* a problem, and
18784 I'd like to fix it.
18786 A future version of dwarf2out.c may generate two additional attributes for
18787 any given DW_TAG_formal_parameter DIE which will describe the "passed
18788 type" and the "passed location" for the given formal parameter in addition
18789 to the attributes we now generate to indicate the "declared type" and the
18790 "active location" for each parameter. This additional set of attributes
18791 could be used by debuggers for stack backtraces. Separately, note that
18792 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18793 This happens (for example) for inlined-instances of inline function formal
18794 parameters which are never referenced. This really shouldn't be
18795 happening. All PARM_DECL nodes should get valid non-NULL
18796 DECL_INCOMING_RTL values. FIXME. */
18798 /* Use DECL_RTL as the "location" unless we find something better. */
18799 rtl = DECL_RTL_IF_SET (decl);
18801 /* When generating abstract instances, ignore everything except
18802 constants, symbols living in memory, and symbols living in
18803 fixed registers. */
18804 if (! reload_completed)
18806 if (rtl
18807 && (CONSTANT_P (rtl)
18808 || (MEM_P (rtl)
18809 && CONSTANT_P (XEXP (rtl, 0)))
18810 || (REG_P (rtl)
18811 && VAR_P (decl)
18812 && TREE_STATIC (decl))))
18814 rtl = targetm.delegitimize_address (rtl);
18815 return rtl;
18817 rtl = NULL_RTX;
18819 else if (TREE_CODE (decl) == PARM_DECL)
18821 if (rtl == NULL_RTX
18822 || is_pseudo_reg (rtl)
18823 || (MEM_P (rtl)
18824 && is_pseudo_reg (XEXP (rtl, 0))
18825 && DECL_INCOMING_RTL (decl)
18826 && MEM_P (DECL_INCOMING_RTL (decl))
18827 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18829 tree declared_type = TREE_TYPE (decl);
18830 tree passed_type = DECL_ARG_TYPE (decl);
18831 machine_mode dmode = TYPE_MODE (declared_type);
18832 machine_mode pmode = TYPE_MODE (passed_type);
18834 /* This decl represents a formal parameter which was optimized out.
18835 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18836 all cases where (rtl == NULL_RTX) just below. */
18837 if (dmode == pmode)
18838 rtl = DECL_INCOMING_RTL (decl);
18839 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18840 && SCALAR_INT_MODE_P (dmode)
18841 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18842 && DECL_INCOMING_RTL (decl))
18844 rtx inc = DECL_INCOMING_RTL (decl);
18845 if (REG_P (inc))
18846 rtl = inc;
18847 else if (MEM_P (inc))
18849 if (BYTES_BIG_ENDIAN)
18850 rtl = adjust_address_nv (inc, dmode,
18851 GET_MODE_SIZE (pmode)
18852 - GET_MODE_SIZE (dmode));
18853 else
18854 rtl = inc;
18859 /* If the parm was passed in registers, but lives on the stack, then
18860 make a big endian correction if the mode of the type of the
18861 parameter is not the same as the mode of the rtl. */
18862 /* ??? This is the same series of checks that are made in dbxout.c before
18863 we reach the big endian correction code there. It isn't clear if all
18864 of these checks are necessary here, but keeping them all is the safe
18865 thing to do. */
18866 else if (MEM_P (rtl)
18867 && XEXP (rtl, 0) != const0_rtx
18868 && ! CONSTANT_P (XEXP (rtl, 0))
18869 /* Not passed in memory. */
18870 && !MEM_P (DECL_INCOMING_RTL (decl))
18871 /* Not passed by invisible reference. */
18872 && (!REG_P (XEXP (rtl, 0))
18873 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18874 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18875 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18876 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18877 #endif
18879 /* Big endian correction check. */
18880 && BYTES_BIG_ENDIAN
18881 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18882 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18883 < UNITS_PER_WORD))
18885 machine_mode addr_mode = get_address_mode (rtl);
18886 int offset = (UNITS_PER_WORD
18887 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18889 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18890 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18893 else if (VAR_P (decl)
18894 && rtl
18895 && MEM_P (rtl)
18896 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18897 && BYTES_BIG_ENDIAN)
18899 machine_mode addr_mode = get_address_mode (rtl);
18900 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18901 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18903 /* If a variable is declared "register" yet is smaller than
18904 a register, then if we store the variable to memory, it
18905 looks like we're storing a register-sized value, when in
18906 fact we are not. We need to adjust the offset of the
18907 storage location to reflect the actual value's bytes,
18908 else gdb will not be able to display it. */
18909 if (rsize > dsize)
18910 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18911 plus_constant (addr_mode, XEXP (rtl, 0),
18912 rsize - dsize));
18915 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18916 and will have been substituted directly into all expressions that use it.
18917 C does not have such a concept, but C++ and other languages do. */
18918 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18919 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18921 if (rtl)
18922 rtl = targetm.delegitimize_address (rtl);
18924 /* If we don't look past the constant pool, we risk emitting a
18925 reference to a constant pool entry that isn't referenced from
18926 code, and thus is not emitted. */
18927 if (rtl)
18928 rtl = avoid_constant_pool_reference (rtl);
18930 /* Try harder to get a rtl. If this symbol ends up not being emitted
18931 in the current CU, resolve_addr will remove the expression referencing
18932 it. */
18933 if (rtl == NULL_RTX
18934 && VAR_P (decl)
18935 && !DECL_EXTERNAL (decl)
18936 && TREE_STATIC (decl)
18937 && DECL_NAME (decl)
18938 && !DECL_HARD_REGISTER (decl)
18939 && DECL_MODE (decl) != VOIDmode)
18941 rtl = make_decl_rtl_for_debug (decl);
18942 if (!MEM_P (rtl)
18943 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
18944 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
18945 rtl = NULL_RTX;
18948 return rtl;
18951 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
18952 returned. If so, the decl for the COMMON block is returned, and the
18953 value is the offset into the common block for the symbol. */
18955 static tree
18956 fortran_common (tree decl, HOST_WIDE_INT *value)
18958 tree val_expr, cvar;
18959 machine_mode mode;
18960 HOST_WIDE_INT bitsize, bitpos;
18961 tree offset;
18962 int unsignedp, reversep, volatilep = 0;
18964 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18965 it does not have a value (the offset into the common area), or if it
18966 is thread local (as opposed to global) then it isn't common, and shouldn't
18967 be handled as such. */
18968 if (!VAR_P (decl)
18969 || !TREE_STATIC (decl)
18970 || !DECL_HAS_VALUE_EXPR_P (decl)
18971 || !is_fortran ())
18972 return NULL_TREE;
18974 val_expr = DECL_VALUE_EXPR (decl);
18975 if (TREE_CODE (val_expr) != COMPONENT_REF)
18976 return NULL_TREE;
18978 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
18979 &unsignedp, &reversep, &volatilep);
18981 if (cvar == NULL_TREE
18982 || !VAR_P (cvar)
18983 || DECL_ARTIFICIAL (cvar)
18984 || !TREE_PUBLIC (cvar))
18985 return NULL_TREE;
18987 *value = 0;
18988 if (offset != NULL)
18990 if (!tree_fits_shwi_p (offset))
18991 return NULL_TREE;
18992 *value = tree_to_shwi (offset);
18994 if (bitpos != 0)
18995 *value += bitpos / BITS_PER_UNIT;
18997 return cvar;
19000 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19001 data attribute for a variable or a parameter. We generate the
19002 DW_AT_const_value attribute only in those cases where the given variable
19003 or parameter does not have a true "location" either in memory or in a
19004 register. This can happen (for example) when a constant is passed as an
19005 actual argument in a call to an inline function. (It's possible that
19006 these things can crop up in other ways also.) Note that one type of
19007 constant value which can be passed into an inlined function is a constant
19008 pointer. This can happen for example if an actual argument in an inlined
19009 function call evaluates to a compile-time constant address.
19011 CACHE_P is true if it is worth caching the location list for DECL,
19012 so that future calls can reuse it rather than regenerate it from scratch.
19013 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19014 since we will need to refer to them each time the function is inlined. */
19016 static bool
19017 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19019 rtx rtl;
19020 dw_loc_list_ref list;
19021 var_loc_list *loc_list;
19022 cached_dw_loc_list *cache;
19024 if (early_dwarf)
19025 return false;
19027 if (TREE_CODE (decl) == ERROR_MARK)
19028 return false;
19030 if (get_AT (die, DW_AT_location)
19031 || get_AT (die, DW_AT_const_value))
19032 return true;
19034 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19035 || TREE_CODE (decl) == RESULT_DECL);
19037 /* Try to get some constant RTL for this decl, and use that as the value of
19038 the location. */
19040 rtl = rtl_for_decl_location (decl);
19041 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19042 && add_const_value_attribute (die, rtl))
19043 return true;
19045 /* See if we have single element location list that is equivalent to
19046 a constant value. That way we are better to use add_const_value_attribute
19047 rather than expanding constant value equivalent. */
19048 loc_list = lookup_decl_loc (decl);
19049 if (loc_list
19050 && loc_list->first
19051 && loc_list->first->next == NULL
19052 && NOTE_P (loc_list->first->loc)
19053 && NOTE_VAR_LOCATION (loc_list->first->loc)
19054 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19056 struct var_loc_node *node;
19058 node = loc_list->first;
19059 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19060 if (GET_CODE (rtl) == EXPR_LIST)
19061 rtl = XEXP (rtl, 0);
19062 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19063 && add_const_value_attribute (die, rtl))
19064 return true;
19066 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19067 list several times. See if we've already cached the contents. */
19068 list = NULL;
19069 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19070 cache_p = false;
19071 if (cache_p)
19073 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19074 if (cache)
19075 list = cache->loc_list;
19077 if (list == NULL)
19079 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19080 NULL);
19081 /* It is usually worth caching this result if the decl is from
19082 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19083 if (cache_p && list && list->dw_loc_next)
19085 cached_dw_loc_list **slot
19086 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19087 DECL_UID (decl),
19088 INSERT);
19089 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19090 cache->decl_id = DECL_UID (decl);
19091 cache->loc_list = list;
19092 *slot = cache;
19095 if (list)
19097 add_AT_location_description (die, DW_AT_location, list);
19098 return true;
19100 /* None of that worked, so it must not really have a location;
19101 try adding a constant value attribute from the DECL_INITIAL. */
19102 return tree_add_const_value_attribute_for_decl (die, decl);
19105 /* Helper function for tree_add_const_value_attribute. Natively encode
19106 initializer INIT into an array. Return true if successful. */
19108 static bool
19109 native_encode_initializer (tree init, unsigned char *array, int size)
19111 tree type;
19113 if (init == NULL_TREE)
19114 return false;
19116 STRIP_NOPS (init);
19117 switch (TREE_CODE (init))
19119 case STRING_CST:
19120 type = TREE_TYPE (init);
19121 if (TREE_CODE (type) == ARRAY_TYPE)
19123 tree enttype = TREE_TYPE (type);
19124 machine_mode mode = TYPE_MODE (enttype);
19126 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19127 return false;
19128 if (int_size_in_bytes (type) != size)
19129 return false;
19130 if (size > TREE_STRING_LENGTH (init))
19132 memcpy (array, TREE_STRING_POINTER (init),
19133 TREE_STRING_LENGTH (init));
19134 memset (array + TREE_STRING_LENGTH (init),
19135 '\0', size - TREE_STRING_LENGTH (init));
19137 else
19138 memcpy (array, TREE_STRING_POINTER (init), size);
19139 return true;
19141 return false;
19142 case CONSTRUCTOR:
19143 type = TREE_TYPE (init);
19144 if (int_size_in_bytes (type) != size)
19145 return false;
19146 if (TREE_CODE (type) == ARRAY_TYPE)
19148 HOST_WIDE_INT min_index;
19149 unsigned HOST_WIDE_INT cnt;
19150 int curpos = 0, fieldsize;
19151 constructor_elt *ce;
19153 if (TYPE_DOMAIN (type) == NULL_TREE
19154 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19155 return false;
19157 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19158 if (fieldsize <= 0)
19159 return false;
19161 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19162 memset (array, '\0', size);
19163 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19165 tree val = ce->value;
19166 tree index = ce->index;
19167 int pos = curpos;
19168 if (index && TREE_CODE (index) == RANGE_EXPR)
19169 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19170 * fieldsize;
19171 else if (index)
19172 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19174 if (val)
19176 STRIP_NOPS (val);
19177 if (!native_encode_initializer (val, array + pos, fieldsize))
19178 return false;
19180 curpos = pos + fieldsize;
19181 if (index && TREE_CODE (index) == RANGE_EXPR)
19183 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19184 - tree_to_shwi (TREE_OPERAND (index, 0));
19185 while (count-- > 0)
19187 if (val)
19188 memcpy (array + curpos, array + pos, fieldsize);
19189 curpos += fieldsize;
19192 gcc_assert (curpos <= size);
19194 return true;
19196 else if (TREE_CODE (type) == RECORD_TYPE
19197 || TREE_CODE (type) == UNION_TYPE)
19199 tree field = NULL_TREE;
19200 unsigned HOST_WIDE_INT cnt;
19201 constructor_elt *ce;
19203 if (int_size_in_bytes (type) != size)
19204 return false;
19206 if (TREE_CODE (type) == RECORD_TYPE)
19207 field = TYPE_FIELDS (type);
19209 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19211 tree val = ce->value;
19212 int pos, fieldsize;
19214 if (ce->index != 0)
19215 field = ce->index;
19217 if (val)
19218 STRIP_NOPS (val);
19220 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19221 return false;
19223 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19224 && TYPE_DOMAIN (TREE_TYPE (field))
19225 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19226 return false;
19227 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19228 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19229 return false;
19230 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19231 pos = int_byte_position (field);
19232 gcc_assert (pos + fieldsize <= size);
19233 if (val && fieldsize != 0
19234 && !native_encode_initializer (val, array + pos, fieldsize))
19235 return false;
19237 return true;
19239 return false;
19240 case VIEW_CONVERT_EXPR:
19241 case NON_LVALUE_EXPR:
19242 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19243 default:
19244 return native_encode_expr (init, array, size) == size;
19248 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19249 attribute is the const value T. */
19251 static bool
19252 tree_add_const_value_attribute (dw_die_ref die, tree t)
19254 tree init;
19255 tree type = TREE_TYPE (t);
19256 rtx rtl;
19258 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19259 return false;
19261 init = t;
19262 gcc_assert (!DECL_P (init));
19264 if (! early_dwarf)
19266 rtl = rtl_for_decl_init (init, type);
19267 if (rtl)
19268 return add_const_value_attribute (die, rtl);
19270 /* If the host and target are sane, try harder. */
19271 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19272 && initializer_constant_valid_p (init, type))
19274 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19275 if (size > 0 && (int) size == size)
19277 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19279 if (native_encode_initializer (init, array, size))
19281 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19282 return true;
19284 ggc_free (array);
19287 return false;
19290 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19291 attribute is the const value of T, where T is an integral constant
19292 variable with static storage duration
19293 (so it can't be a PARM_DECL or a RESULT_DECL). */
19295 static bool
19296 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19299 if (!decl
19300 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19301 || (VAR_P (decl) && !TREE_STATIC (decl)))
19302 return false;
19304 if (TREE_READONLY (decl)
19305 && ! TREE_THIS_VOLATILE (decl)
19306 && DECL_INITIAL (decl))
19307 /* OK */;
19308 else
19309 return false;
19311 /* Don't add DW_AT_const_value if abstract origin already has one. */
19312 if (get_AT (var_die, DW_AT_const_value))
19313 return false;
19315 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19318 /* Convert the CFI instructions for the current function into a
19319 location list. This is used for DW_AT_frame_base when we targeting
19320 a dwarf2 consumer that does not support the dwarf3
19321 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19322 expressions. */
19324 static dw_loc_list_ref
19325 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19327 int ix;
19328 dw_fde_ref fde;
19329 dw_loc_list_ref list, *list_tail;
19330 dw_cfi_ref cfi;
19331 dw_cfa_location last_cfa, next_cfa;
19332 const char *start_label, *last_label, *section;
19333 dw_cfa_location remember;
19335 fde = cfun->fde;
19336 gcc_assert (fde != NULL);
19338 section = secname_for_decl (current_function_decl);
19339 list_tail = &list;
19340 list = NULL;
19342 memset (&next_cfa, 0, sizeof (next_cfa));
19343 next_cfa.reg = INVALID_REGNUM;
19344 remember = next_cfa;
19346 start_label = fde->dw_fde_begin;
19348 /* ??? Bald assumption that the CIE opcode list does not contain
19349 advance opcodes. */
19350 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19351 lookup_cfa_1 (cfi, &next_cfa, &remember);
19353 last_cfa = next_cfa;
19354 last_label = start_label;
19356 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19358 /* If the first partition contained no CFI adjustments, the
19359 CIE opcodes apply to the whole first partition. */
19360 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19361 fde->dw_fde_begin, fde->dw_fde_end, section);
19362 list_tail =&(*list_tail)->dw_loc_next;
19363 start_label = last_label = fde->dw_fde_second_begin;
19366 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19368 switch (cfi->dw_cfi_opc)
19370 case DW_CFA_set_loc:
19371 case DW_CFA_advance_loc1:
19372 case DW_CFA_advance_loc2:
19373 case DW_CFA_advance_loc4:
19374 if (!cfa_equal_p (&last_cfa, &next_cfa))
19376 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19377 start_label, last_label, section);
19379 list_tail = &(*list_tail)->dw_loc_next;
19380 last_cfa = next_cfa;
19381 start_label = last_label;
19383 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19384 break;
19386 case DW_CFA_advance_loc:
19387 /* The encoding is complex enough that we should never emit this. */
19388 gcc_unreachable ();
19390 default:
19391 lookup_cfa_1 (cfi, &next_cfa, &remember);
19392 break;
19394 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19396 if (!cfa_equal_p (&last_cfa, &next_cfa))
19398 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19399 start_label, last_label, section);
19401 list_tail = &(*list_tail)->dw_loc_next;
19402 last_cfa = next_cfa;
19403 start_label = last_label;
19405 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19406 start_label, fde->dw_fde_end, section);
19407 list_tail = &(*list_tail)->dw_loc_next;
19408 start_label = last_label = fde->dw_fde_second_begin;
19412 if (!cfa_equal_p (&last_cfa, &next_cfa))
19414 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19415 start_label, last_label, section);
19416 list_tail = &(*list_tail)->dw_loc_next;
19417 start_label = last_label;
19420 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19421 start_label,
19422 fde->dw_fde_second_begin
19423 ? fde->dw_fde_second_end : fde->dw_fde_end,
19424 section);
19426 if (list && list->dw_loc_next)
19427 gen_llsym (list);
19429 return list;
19432 /* Compute a displacement from the "steady-state frame pointer" to the
19433 frame base (often the same as the CFA), and store it in
19434 frame_pointer_fb_offset. OFFSET is added to the displacement
19435 before the latter is negated. */
19437 static void
19438 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19440 rtx reg, elim;
19442 #ifdef FRAME_POINTER_CFA_OFFSET
19443 reg = frame_pointer_rtx;
19444 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19445 #else
19446 reg = arg_pointer_rtx;
19447 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19448 #endif
19450 elim = (ira_use_lra_p
19451 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19452 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19453 if (GET_CODE (elim) == PLUS)
19455 offset += INTVAL (XEXP (elim, 1));
19456 elim = XEXP (elim, 0);
19459 frame_pointer_fb_offset = -offset;
19461 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19462 in which to eliminate. This is because it's stack pointer isn't
19463 directly accessible as a register within the ISA. To work around
19464 this, assume that while we cannot provide a proper value for
19465 frame_pointer_fb_offset, we won't need one either. */
19466 frame_pointer_fb_offset_valid
19467 = ((SUPPORTS_STACK_ALIGNMENT
19468 && (elim == hard_frame_pointer_rtx
19469 || elim == stack_pointer_rtx))
19470 || elim == (frame_pointer_needed
19471 ? hard_frame_pointer_rtx
19472 : stack_pointer_rtx));
19475 /* Generate a DW_AT_name attribute given some string value to be included as
19476 the value of the attribute. */
19478 static void
19479 add_name_attribute (dw_die_ref die, const char *name_string)
19481 if (name_string != NULL && *name_string != 0)
19483 if (demangle_name_func)
19484 name_string = (*demangle_name_func) (name_string);
19486 add_AT_string (die, DW_AT_name, name_string);
19490 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19491 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19492 of TYPE accordingly.
19494 ??? This is a temporary measure until after we're able to generate
19495 regular DWARF for the complex Ada type system. */
19497 static void
19498 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19499 dw_die_ref context_die)
19501 tree dtype;
19502 dw_die_ref dtype_die;
19504 if (!lang_hooks.types.descriptive_type)
19505 return;
19507 dtype = lang_hooks.types.descriptive_type (type);
19508 if (!dtype)
19509 return;
19511 dtype_die = lookup_type_die (dtype);
19512 if (!dtype_die)
19514 gen_type_die (dtype, context_die);
19515 dtype_die = lookup_type_die (dtype);
19516 gcc_assert (dtype_die);
19519 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19522 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19524 static const char *
19525 comp_dir_string (void)
19527 const char *wd;
19528 char *wd1;
19529 static const char *cached_wd = NULL;
19531 if (cached_wd != NULL)
19532 return cached_wd;
19534 wd = get_src_pwd ();
19535 if (wd == NULL)
19536 return NULL;
19538 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19540 int wdlen;
19542 wdlen = strlen (wd);
19543 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19544 strcpy (wd1, wd);
19545 wd1 [wdlen] = DIR_SEPARATOR;
19546 wd1 [wdlen + 1] = 0;
19547 wd = wd1;
19550 cached_wd = remap_debug_filename (wd);
19551 return cached_wd;
19554 /* Generate a DW_AT_comp_dir attribute for DIE. */
19556 static void
19557 add_comp_dir_attribute (dw_die_ref die)
19559 const char * wd = comp_dir_string ();
19560 if (wd != NULL)
19561 add_AT_string (die, DW_AT_comp_dir, wd);
19564 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19565 pointer computation, ...), output a representation for that bound according
19566 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19567 loc_list_from_tree for the meaning of CONTEXT. */
19569 static void
19570 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19571 int forms, struct loc_descr_context *context)
19573 dw_die_ref context_die, decl_die;
19574 dw_loc_list_ref list;
19575 bool strip_conversions = true;
19576 bool placeholder_seen = false;
19578 while (strip_conversions)
19579 switch (TREE_CODE (value))
19581 case ERROR_MARK:
19582 case SAVE_EXPR:
19583 return;
19585 CASE_CONVERT:
19586 case VIEW_CONVERT_EXPR:
19587 value = TREE_OPERAND (value, 0);
19588 break;
19590 default:
19591 strip_conversions = false;
19592 break;
19595 /* If possible and permitted, output the attribute as a constant. */
19596 if ((forms & dw_scalar_form_constant) != 0
19597 && TREE_CODE (value) == INTEGER_CST)
19599 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19601 /* If HOST_WIDE_INT is big enough then represent the bound as
19602 a constant value. We need to choose a form based on
19603 whether the type is signed or unsigned. We cannot just
19604 call add_AT_unsigned if the value itself is positive
19605 (add_AT_unsigned might add the unsigned value encoded as
19606 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19607 bounds type and then sign extend any unsigned values found
19608 for signed types. This is needed only for
19609 DW_AT_{lower,upper}_bound, since for most other attributes,
19610 consumers will treat DW_FORM_data[1248] as unsigned values,
19611 regardless of the underlying type. */
19612 if (prec <= HOST_BITS_PER_WIDE_INT
19613 || tree_fits_uhwi_p (value))
19615 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19616 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19617 else
19618 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19620 else
19621 /* Otherwise represent the bound as an unsigned value with
19622 the precision of its type. The precision and signedness
19623 of the type will be necessary to re-interpret it
19624 unambiguously. */
19625 add_AT_wide (die, attr, value);
19626 return;
19629 /* Otherwise, if it's possible and permitted too, output a reference to
19630 another DIE. */
19631 if ((forms & dw_scalar_form_reference) != 0)
19633 tree decl = NULL_TREE;
19635 /* Some type attributes reference an outer type. For instance, the upper
19636 bound of an array may reference an embedding record (this happens in
19637 Ada). */
19638 if (TREE_CODE (value) == COMPONENT_REF
19639 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19640 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19641 decl = TREE_OPERAND (value, 1);
19643 else if (VAR_P (value)
19644 || TREE_CODE (value) == PARM_DECL
19645 || TREE_CODE (value) == RESULT_DECL)
19646 decl = value;
19648 if (decl != NULL_TREE)
19650 dw_die_ref decl_die = lookup_decl_die (decl);
19652 /* ??? Can this happen, or should the variable have been bound
19653 first? Probably it can, since I imagine that we try to create
19654 the types of parameters in the order in which they exist in
19655 the list, and won't have created a forward reference to a
19656 later parameter. */
19657 if (decl_die != NULL)
19659 add_AT_die_ref (die, attr, decl_die);
19660 return;
19665 /* Last chance: try to create a stack operation procedure to evaluate the
19666 value. Do nothing if even that is not possible or permitted. */
19667 if ((forms & dw_scalar_form_exprloc) == 0)
19668 return;
19670 list = loc_list_from_tree (value, 2, context);
19671 if (context && context->placeholder_arg)
19673 placeholder_seen = context->placeholder_seen;
19674 context->placeholder_seen = false;
19676 if (list == NULL || single_element_loc_list_p (list))
19678 /* If this attribute is not a reference nor constant, it is
19679 a DWARF expression rather than location description. For that
19680 loc_list_from_tree (value, 0, &context) is needed. */
19681 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19682 if (list2 && single_element_loc_list_p (list2))
19684 if (placeholder_seen)
19686 struct dwarf_procedure_info dpi;
19687 dpi.fndecl = NULL_TREE;
19688 dpi.args_count = 1;
19689 if (!resolve_args_picking (list2->expr, 1, &dpi))
19690 return;
19692 add_AT_loc (die, attr, list2->expr);
19693 return;
19697 /* If that failed to give a single element location list, fall back to
19698 outputting this as a reference... still if permitted. */
19699 if (list == NULL
19700 || (forms & dw_scalar_form_reference) == 0
19701 || placeholder_seen)
19702 return;
19704 if (current_function_decl == 0)
19705 context_die = comp_unit_die ();
19706 else
19707 context_die = lookup_decl_die (current_function_decl);
19709 decl_die = new_die (DW_TAG_variable, context_die, value);
19710 add_AT_flag (decl_die, DW_AT_artificial, 1);
19711 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19712 context_die);
19713 add_AT_location_description (decl_die, DW_AT_location, list);
19714 add_AT_die_ref (die, attr, decl_die);
19717 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19718 default. */
19720 static int
19721 lower_bound_default (void)
19723 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19725 case DW_LANG_C:
19726 case DW_LANG_C89:
19727 case DW_LANG_C99:
19728 case DW_LANG_C11:
19729 case DW_LANG_C_plus_plus:
19730 case DW_LANG_C_plus_plus_11:
19731 case DW_LANG_C_plus_plus_14:
19732 case DW_LANG_ObjC:
19733 case DW_LANG_ObjC_plus_plus:
19734 case DW_LANG_Java:
19735 return 0;
19736 case DW_LANG_Fortran77:
19737 case DW_LANG_Fortran90:
19738 case DW_LANG_Fortran95:
19739 case DW_LANG_Fortran03:
19740 case DW_LANG_Fortran08:
19741 return 1;
19742 case DW_LANG_UPC:
19743 case DW_LANG_D:
19744 case DW_LANG_Python:
19745 return dwarf_version >= 4 ? 0 : -1;
19746 case DW_LANG_Ada95:
19747 case DW_LANG_Ada83:
19748 case DW_LANG_Cobol74:
19749 case DW_LANG_Cobol85:
19750 case DW_LANG_Pascal83:
19751 case DW_LANG_Modula2:
19752 case DW_LANG_PLI:
19753 return dwarf_version >= 4 ? 1 : -1;
19754 default:
19755 return -1;
19759 /* Given a tree node describing an array bound (either lower or upper) output
19760 a representation for that bound. */
19762 static void
19763 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19764 tree bound, struct loc_descr_context *context)
19766 int dflt;
19768 while (1)
19769 switch (TREE_CODE (bound))
19771 /* Strip all conversions. */
19772 CASE_CONVERT:
19773 case VIEW_CONVERT_EXPR:
19774 bound = TREE_OPERAND (bound, 0);
19775 break;
19777 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19778 are even omitted when they are the default. */
19779 case INTEGER_CST:
19780 /* If the value for this bound is the default one, we can even omit the
19781 attribute. */
19782 if (bound_attr == DW_AT_lower_bound
19783 && tree_fits_shwi_p (bound)
19784 && (dflt = lower_bound_default ()) != -1
19785 && tree_to_shwi (bound) == dflt)
19786 return;
19788 /* FALLTHRU */
19790 default:
19791 /* Because of the complex interaction there can be with other GNAT
19792 encodings, GDB isn't ready yet to handle proper DWARF description
19793 for self-referencial subrange bounds: let GNAT encodings do the
19794 magic in such a case. */
19795 if (is_ada ()
19796 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19797 && contains_placeholder_p (bound))
19798 return;
19800 add_scalar_info (subrange_die, bound_attr, bound,
19801 dw_scalar_form_constant
19802 | dw_scalar_form_exprloc
19803 | dw_scalar_form_reference,
19804 context);
19805 return;
19809 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19810 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19811 Note that the block of subscript information for an array type also
19812 includes information about the element type of the given array type.
19814 This function reuses previously set type and bound information if
19815 available. */
19817 static void
19818 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19820 unsigned dimension_number;
19821 tree lower, upper;
19822 dw_die_ref child = type_die->die_child;
19824 for (dimension_number = 0;
19825 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19826 type = TREE_TYPE (type), dimension_number++)
19828 tree domain = TYPE_DOMAIN (type);
19830 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19831 break;
19833 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19834 and (in GNU C only) variable bounds. Handle all three forms
19835 here. */
19837 /* Find and reuse a previously generated DW_TAG_subrange_type if
19838 available.
19840 For multi-dimensional arrays, as we iterate through the
19841 various dimensions in the enclosing for loop above, we also
19842 iterate through the DIE children and pick at each
19843 DW_TAG_subrange_type previously generated (if available).
19844 Each child DW_TAG_subrange_type DIE describes the range of
19845 the current dimension. At this point we should have as many
19846 DW_TAG_subrange_type's as we have dimensions in the
19847 array. */
19848 dw_die_ref subrange_die = NULL;
19849 if (child)
19850 while (1)
19852 child = child->die_sib;
19853 if (child->die_tag == DW_TAG_subrange_type)
19854 subrange_die = child;
19855 if (child == type_die->die_child)
19857 /* If we wrapped around, stop looking next time. */
19858 child = NULL;
19859 break;
19861 if (child->die_tag == DW_TAG_subrange_type)
19862 break;
19864 if (!subrange_die)
19865 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19867 if (domain)
19869 /* We have an array type with specified bounds. */
19870 lower = TYPE_MIN_VALUE (domain);
19871 upper = TYPE_MAX_VALUE (domain);
19873 /* Define the index type. */
19874 if (TREE_TYPE (domain)
19875 && !get_AT (subrange_die, DW_AT_type))
19877 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19878 TREE_TYPE field. We can't emit debug info for this
19879 because it is an unnamed integral type. */
19880 if (TREE_CODE (domain) == INTEGER_TYPE
19881 && TYPE_NAME (domain) == NULL_TREE
19882 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19883 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19885 else
19886 add_type_attribute (subrange_die, TREE_TYPE (domain),
19887 TYPE_UNQUALIFIED, false, type_die);
19890 /* ??? If upper is NULL, the array has unspecified length,
19891 but it does have a lower bound. This happens with Fortran
19892 dimension arr(N:*)
19893 Since the debugger is definitely going to need to know N
19894 to produce useful results, go ahead and output the lower
19895 bound solo, and hope the debugger can cope. */
19897 if (!get_AT (subrange_die, DW_AT_lower_bound))
19898 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19899 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19900 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19903 /* Otherwise we have an array type with an unspecified length. The
19904 DWARF-2 spec does not say how to handle this; let's just leave out the
19905 bounds. */
19909 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19911 static void
19912 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19914 dw_die_ref decl_die;
19915 HOST_WIDE_INT size;
19916 dw_loc_descr_ref size_expr = NULL;
19918 switch (TREE_CODE (tree_node))
19920 case ERROR_MARK:
19921 size = 0;
19922 break;
19923 case ENUMERAL_TYPE:
19924 case RECORD_TYPE:
19925 case UNION_TYPE:
19926 case QUAL_UNION_TYPE:
19927 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
19928 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
19930 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
19931 return;
19933 size_expr = type_byte_size (tree_node, &size);
19934 break;
19935 case FIELD_DECL:
19936 /* For a data member of a struct or union, the DW_AT_byte_size is
19937 generally given as the number of bytes normally allocated for an
19938 object of the *declared* type of the member itself. This is true
19939 even for bit-fields. */
19940 size = int_size_in_bytes (field_type (tree_node));
19941 break;
19942 default:
19943 gcc_unreachable ();
19946 /* Support for dynamically-sized objects was introduced by DWARFv3.
19947 At the moment, GDB does not handle variable byte sizes very well,
19948 though. */
19949 if ((dwarf_version >= 3 || !dwarf_strict)
19950 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
19951 && size_expr != NULL)
19952 add_AT_loc (die, DW_AT_byte_size, size_expr);
19954 /* Note that `size' might be -1 when we get to this point. If it is, that
19955 indicates that the byte size of the entity in question is variable and
19956 that we could not generate a DWARF expression that computes it. */
19957 if (size >= 0)
19958 add_AT_unsigned (die, DW_AT_byte_size, size);
19961 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
19962 alignment. */
19964 static void
19965 add_alignment_attribute (dw_die_ref die, tree tree_node)
19967 if (dwarf_version < 5 && dwarf_strict)
19968 return;
19970 unsigned align;
19972 if (DECL_P (tree_node))
19974 if (!DECL_USER_ALIGN (tree_node))
19975 return;
19977 align = DECL_ALIGN_UNIT (tree_node);
19979 else if (TYPE_P (tree_node))
19981 if (!TYPE_USER_ALIGN (tree_node))
19982 return;
19984 align = TYPE_ALIGN_UNIT (tree_node);
19986 else
19987 gcc_unreachable ();
19989 add_AT_unsigned (die, DW_AT_alignment, align);
19992 /* For a FIELD_DECL node which represents a bit-field, output an attribute
19993 which specifies the distance in bits from the highest order bit of the
19994 "containing object" for the bit-field to the highest order bit of the
19995 bit-field itself.
19997 For any given bit-field, the "containing object" is a hypothetical object
19998 (of some integral or enum type) within which the given bit-field lives. The
19999 type of this hypothetical "containing object" is always the same as the
20000 declared type of the individual bit-field itself. The determination of the
20001 exact location of the "containing object" for a bit-field is rather
20002 complicated. It's handled by the `field_byte_offset' function (above).
20004 CTX is required: see the comment for VLR_CONTEXT.
20006 Note that it is the size (in bytes) of the hypothetical "containing object"
20007 which will be given in the DW_AT_byte_size attribute for this bit-field.
20008 (See `byte_size_attribute' above). */
20010 static inline void
20011 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20013 HOST_WIDE_INT object_offset_in_bytes;
20014 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20015 HOST_WIDE_INT bitpos_int;
20016 HOST_WIDE_INT highest_order_object_bit_offset;
20017 HOST_WIDE_INT highest_order_field_bit_offset;
20018 HOST_WIDE_INT bit_offset;
20020 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20022 /* Must be a field and a bit field. */
20023 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20025 /* We can't yet handle bit-fields whose offsets are variable, so if we
20026 encounter such things, just return without generating any attribute
20027 whatsoever. Likewise for variable or too large size. */
20028 if (! tree_fits_shwi_p (bit_position (decl))
20029 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20030 return;
20032 bitpos_int = int_bit_position (decl);
20034 /* Note that the bit offset is always the distance (in bits) from the
20035 highest-order bit of the "containing object" to the highest-order bit of
20036 the bit-field itself. Since the "high-order end" of any object or field
20037 is different on big-endian and little-endian machines, the computation
20038 below must take account of these differences. */
20039 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20040 highest_order_field_bit_offset = bitpos_int;
20042 if (! BYTES_BIG_ENDIAN)
20044 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20045 highest_order_object_bit_offset +=
20046 simple_type_size_in_bits (original_type);
20049 bit_offset
20050 = (! BYTES_BIG_ENDIAN
20051 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20052 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20054 if (bit_offset < 0)
20055 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20056 else
20057 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20060 /* For a FIELD_DECL node which represents a bit field, output an attribute
20061 which specifies the length in bits of the given field. */
20063 static inline void
20064 add_bit_size_attribute (dw_die_ref die, tree decl)
20066 /* Must be a field and a bit field. */
20067 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20068 && DECL_BIT_FIELD_TYPE (decl));
20070 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20071 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20074 /* If the compiled language is ANSI C, then add a 'prototyped'
20075 attribute, if arg types are given for the parameters of a function. */
20077 static inline void
20078 add_prototyped_attribute (dw_die_ref die, tree func_type)
20080 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20082 case DW_LANG_C:
20083 case DW_LANG_C89:
20084 case DW_LANG_C99:
20085 case DW_LANG_C11:
20086 case DW_LANG_ObjC:
20087 if (prototype_p (func_type))
20088 add_AT_flag (die, DW_AT_prototyped, 1);
20089 break;
20090 default:
20091 break;
20095 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20096 by looking in the type declaration, the object declaration equate table or
20097 the block mapping. */
20099 static inline dw_die_ref
20100 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20102 dw_die_ref origin_die = NULL;
20104 if (TREE_CODE (origin) != FUNCTION_DECL
20105 && TREE_CODE (origin) != BLOCK)
20107 /* We may have gotten separated from the block for the inlined
20108 function, if we're in an exception handler or some such; make
20109 sure that the abstract function has been written out.
20111 Doing this for nested functions is wrong, however; functions are
20112 distinct units, and our context might not even be inline. */
20113 tree fn = origin;
20115 if (TYPE_P (fn))
20116 fn = TYPE_STUB_DECL (fn);
20118 fn = decl_function_context (fn);
20119 if (fn)
20120 dwarf2out_abstract_function (fn);
20123 if (DECL_P (origin))
20124 origin_die = lookup_decl_die (origin);
20125 else if (TYPE_P (origin))
20126 origin_die = lookup_type_die (origin);
20127 else if (TREE_CODE (origin) == BLOCK)
20128 origin_die = BLOCK_DIE (origin);
20130 /* XXX: Functions that are never lowered don't always have correct block
20131 trees (in the case of java, they simply have no block tree, in some other
20132 languages). For these functions, there is nothing we can really do to
20133 output correct debug info for inlined functions in all cases. Rather
20134 than die, we'll just produce deficient debug info now, in that we will
20135 have variables without a proper abstract origin. In the future, when all
20136 functions are lowered, we should re-add a gcc_assert (origin_die)
20137 here. */
20139 if (origin_die)
20140 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20141 return origin_die;
20144 /* We do not currently support the pure_virtual attribute. */
20146 static inline void
20147 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20149 if (DECL_VINDEX (func_decl))
20151 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20153 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20154 add_AT_loc (die, DW_AT_vtable_elem_location,
20155 new_loc_descr (DW_OP_constu,
20156 tree_to_shwi (DECL_VINDEX (func_decl)),
20157 0));
20159 /* GNU extension: Record what type this method came from originally. */
20160 if (debug_info_level > DINFO_LEVEL_TERSE
20161 && DECL_CONTEXT (func_decl))
20162 add_AT_die_ref (die, DW_AT_containing_type,
20163 lookup_type_die (DECL_CONTEXT (func_decl)));
20167 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20168 given decl. This used to be a vendor extension until after DWARF 4
20169 standardized it. */
20171 static void
20172 add_linkage_attr (dw_die_ref die, tree decl)
20174 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20176 /* Mimic what assemble_name_raw does with a leading '*'. */
20177 if (name[0] == '*')
20178 name = &name[1];
20180 if (dwarf_version >= 4)
20181 add_AT_string (die, DW_AT_linkage_name, name);
20182 else
20183 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20186 /* Add source coordinate attributes for the given decl. */
20188 static void
20189 add_src_coords_attributes (dw_die_ref die, tree decl)
20191 expanded_location s;
20193 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20194 return;
20195 s = expand_location (DECL_SOURCE_LOCATION (decl));
20196 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20197 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20198 if (debug_column_info && s.column)
20199 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20202 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20204 static void
20205 add_linkage_name_raw (dw_die_ref die, tree decl)
20207 /* Defer until we have an assembler name set. */
20208 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20210 limbo_die_node *asm_name;
20212 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20213 asm_name->die = die;
20214 asm_name->created_for = decl;
20215 asm_name->next = deferred_asm_name;
20216 deferred_asm_name = asm_name;
20218 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20219 add_linkage_attr (die, decl);
20222 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20224 static void
20225 add_linkage_name (dw_die_ref die, tree decl)
20227 if (debug_info_level > DINFO_LEVEL_NONE
20228 && VAR_OR_FUNCTION_DECL_P (decl)
20229 && TREE_PUBLIC (decl)
20230 && !(VAR_P (decl) && DECL_REGISTER (decl))
20231 && die->die_tag != DW_TAG_member)
20232 add_linkage_name_raw (die, decl);
20235 /* Add a DW_AT_name attribute and source coordinate attribute for the
20236 given decl, but only if it actually has a name. */
20238 static void
20239 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20240 bool no_linkage_name)
20242 tree decl_name;
20244 decl_name = DECL_NAME (decl);
20245 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20247 const char *name = dwarf2_name (decl, 0);
20248 if (name)
20249 add_name_attribute (die, name);
20250 if (! DECL_ARTIFICIAL (decl))
20251 add_src_coords_attributes (die, decl);
20253 if (!no_linkage_name)
20254 add_linkage_name (die, decl);
20257 #ifdef VMS_DEBUGGING_INFO
20258 /* Get the function's name, as described by its RTL. This may be different
20259 from the DECL_NAME name used in the source file. */
20260 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20262 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20263 XEXP (DECL_RTL (decl), 0), false);
20264 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20266 #endif /* VMS_DEBUGGING_INFO */
20269 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20271 static void
20272 add_discr_value (dw_die_ref die, dw_discr_value *value)
20274 dw_attr_node attr;
20276 attr.dw_attr = DW_AT_discr_value;
20277 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20278 attr.dw_attr_val.val_entry = NULL;
20279 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20280 if (value->pos)
20281 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20282 else
20283 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20284 add_dwarf_attr (die, &attr);
20287 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20289 static void
20290 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20292 dw_attr_node attr;
20294 attr.dw_attr = DW_AT_discr_list;
20295 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20296 attr.dw_attr_val.val_entry = NULL;
20297 attr.dw_attr_val.v.val_discr_list = discr_list;
20298 add_dwarf_attr (die, &attr);
20301 static inline dw_discr_list_ref
20302 AT_discr_list (dw_attr_node *attr)
20304 return attr->dw_attr_val.v.val_discr_list;
20307 #ifdef VMS_DEBUGGING_INFO
20308 /* Output the debug main pointer die for VMS */
20310 void
20311 dwarf2out_vms_debug_main_pointer (void)
20313 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20314 dw_die_ref die;
20316 /* Allocate the VMS debug main subprogram die. */
20317 die = ggc_cleared_alloc<die_node> ();
20318 die->die_tag = DW_TAG_subprogram;
20319 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20320 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20321 current_function_funcdef_no);
20322 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20324 /* Make it the first child of comp_unit_die (). */
20325 die->die_parent = comp_unit_die ();
20326 if (comp_unit_die ()->die_child)
20328 die->die_sib = comp_unit_die ()->die_child->die_sib;
20329 comp_unit_die ()->die_child->die_sib = die;
20331 else
20333 die->die_sib = die;
20334 comp_unit_die ()->die_child = die;
20337 #endif /* VMS_DEBUGGING_INFO */
20339 /* Push a new declaration scope. */
20341 static void
20342 push_decl_scope (tree scope)
20344 vec_safe_push (decl_scope_table, scope);
20347 /* Pop a declaration scope. */
20349 static inline void
20350 pop_decl_scope (void)
20352 decl_scope_table->pop ();
20355 /* walk_tree helper function for uses_local_type, below. */
20357 static tree
20358 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20360 if (!TYPE_P (*tp))
20361 *walk_subtrees = 0;
20362 else
20364 tree name = TYPE_NAME (*tp);
20365 if (name && DECL_P (name) && decl_function_context (name))
20366 return *tp;
20368 return NULL_TREE;
20371 /* If TYPE involves a function-local type (including a local typedef to a
20372 non-local type), returns that type; otherwise returns NULL_TREE. */
20374 static tree
20375 uses_local_type (tree type)
20377 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20378 return used;
20381 /* Return the DIE for the scope that immediately contains this type.
20382 Non-named types that do not involve a function-local type get global
20383 scope. Named types nested in namespaces or other types get their
20384 containing scope. All other types (i.e. function-local named types) get
20385 the current active scope. */
20387 static dw_die_ref
20388 scope_die_for (tree t, dw_die_ref context_die)
20390 dw_die_ref scope_die = NULL;
20391 tree containing_scope;
20393 /* Non-types always go in the current scope. */
20394 gcc_assert (TYPE_P (t));
20396 /* Use the scope of the typedef, rather than the scope of the type
20397 it refers to. */
20398 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20399 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20400 else
20401 containing_scope = TYPE_CONTEXT (t);
20403 /* Use the containing namespace if there is one. */
20404 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20406 if (context_die == lookup_decl_die (containing_scope))
20407 /* OK */;
20408 else if (debug_info_level > DINFO_LEVEL_TERSE)
20409 context_die = get_context_die (containing_scope);
20410 else
20411 containing_scope = NULL_TREE;
20414 /* Ignore function type "scopes" from the C frontend. They mean that
20415 a tagged type is local to a parmlist of a function declarator, but
20416 that isn't useful to DWARF. */
20417 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20418 containing_scope = NULL_TREE;
20420 if (SCOPE_FILE_SCOPE_P (containing_scope))
20422 /* If T uses a local type keep it local as well, to avoid references
20423 to function-local DIEs from outside the function. */
20424 if (current_function_decl && uses_local_type (t))
20425 scope_die = context_die;
20426 else
20427 scope_die = comp_unit_die ();
20429 else if (TYPE_P (containing_scope))
20431 /* For types, we can just look up the appropriate DIE. */
20432 if (debug_info_level > DINFO_LEVEL_TERSE)
20433 scope_die = get_context_die (containing_scope);
20434 else
20436 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20437 if (scope_die == NULL)
20438 scope_die = comp_unit_die ();
20441 else
20442 scope_die = context_die;
20444 return scope_die;
20447 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20449 static inline int
20450 local_scope_p (dw_die_ref context_die)
20452 for (; context_die; context_die = context_die->die_parent)
20453 if (context_die->die_tag == DW_TAG_inlined_subroutine
20454 || context_die->die_tag == DW_TAG_subprogram)
20455 return 1;
20457 return 0;
20460 /* Returns nonzero if CONTEXT_DIE is a class. */
20462 static inline int
20463 class_scope_p (dw_die_ref context_die)
20465 return (context_die
20466 && (context_die->die_tag == DW_TAG_structure_type
20467 || context_die->die_tag == DW_TAG_class_type
20468 || context_die->die_tag == DW_TAG_interface_type
20469 || context_die->die_tag == DW_TAG_union_type));
20472 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20473 whether or not to treat a DIE in this context as a declaration. */
20475 static inline int
20476 class_or_namespace_scope_p (dw_die_ref context_die)
20478 return (class_scope_p (context_die)
20479 || (context_die && context_die->die_tag == DW_TAG_namespace));
20482 /* Many forms of DIEs require a "type description" attribute. This
20483 routine locates the proper "type descriptor" die for the type given
20484 by 'type' plus any additional qualifiers given by 'cv_quals', and
20485 adds a DW_AT_type attribute below the given die. */
20487 static void
20488 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20489 bool reverse, dw_die_ref context_die)
20491 enum tree_code code = TREE_CODE (type);
20492 dw_die_ref type_die = NULL;
20494 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20495 or fixed-point type, use the inner type. This is because we have no
20496 support for unnamed types in base_type_die. This can happen if this is
20497 an Ada subrange type. Correct solution is emit a subrange type die. */
20498 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20499 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20500 type = TREE_TYPE (type), code = TREE_CODE (type);
20502 if (code == ERROR_MARK
20503 /* Handle a special case. For functions whose return type is void, we
20504 generate *no* type attribute. (Note that no object may have type
20505 `void', so this only applies to function return types). */
20506 || code == VOID_TYPE)
20507 return;
20509 type_die = modified_type_die (type,
20510 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20511 reverse,
20512 context_die);
20514 if (type_die != NULL)
20515 add_AT_die_ref (object_die, DW_AT_type, type_die);
20518 /* Given an object die, add the calling convention attribute for the
20519 function call type. */
20520 static void
20521 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20523 enum dwarf_calling_convention value = DW_CC_normal;
20525 value = ((enum dwarf_calling_convention)
20526 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20528 if (is_fortran ()
20529 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20531 /* DWARF 2 doesn't provide a way to identify a program's source-level
20532 entry point. DW_AT_calling_convention attributes are only meant
20533 to describe functions' calling conventions. However, lacking a
20534 better way to signal the Fortran main program, we used this for
20535 a long time, following existing custom. Now, DWARF 4 has
20536 DW_AT_main_subprogram, which we add below, but some tools still
20537 rely on the old way, which we thus keep. */
20538 value = DW_CC_program;
20540 if (dwarf_version >= 4 || !dwarf_strict)
20541 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20544 /* Only add the attribute if the backend requests it, and
20545 is not DW_CC_normal. */
20546 if (value && (value != DW_CC_normal))
20547 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20550 /* Given a tree pointer to a struct, class, union, or enum type node, return
20551 a pointer to the (string) tag name for the given type, or zero if the type
20552 was declared without a tag. */
20554 static const char *
20555 type_tag (const_tree type)
20557 const char *name = 0;
20559 if (TYPE_NAME (type) != 0)
20561 tree t = 0;
20563 /* Find the IDENTIFIER_NODE for the type name. */
20564 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20565 && !TYPE_NAMELESS (type))
20566 t = TYPE_NAME (type);
20568 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20569 a TYPE_DECL node, regardless of whether or not a `typedef' was
20570 involved. */
20571 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20572 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20574 /* We want to be extra verbose. Don't call dwarf_name if
20575 DECL_NAME isn't set. The default hook for decl_printable_name
20576 doesn't like that, and in this context it's correct to return
20577 0, instead of "<anonymous>" or the like. */
20578 if (DECL_NAME (TYPE_NAME (type))
20579 && !DECL_NAMELESS (TYPE_NAME (type)))
20580 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20583 /* Now get the name as a string, or invent one. */
20584 if (!name && t != 0)
20585 name = IDENTIFIER_POINTER (t);
20588 return (name == 0 || *name == '\0') ? 0 : name;
20591 /* Return the type associated with a data member, make a special check
20592 for bit field types. */
20594 static inline tree
20595 member_declared_type (const_tree member)
20597 return (DECL_BIT_FIELD_TYPE (member)
20598 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20601 /* Get the decl's label, as described by its RTL. This may be different
20602 from the DECL_NAME name used in the source file. */
20604 #if 0
20605 static const char *
20606 decl_start_label (tree decl)
20608 rtx x;
20609 const char *fnname;
20611 x = DECL_RTL (decl);
20612 gcc_assert (MEM_P (x));
20614 x = XEXP (x, 0);
20615 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20617 fnname = XSTR (x, 0);
20618 return fnname;
20620 #endif
20622 /* For variable-length arrays that have been previously generated, but
20623 may be incomplete due to missing subscript info, fill the subscript
20624 info. Return TRUE if this is one of those cases. */
20625 static bool
20626 fill_variable_array_bounds (tree type)
20628 if (TREE_ASM_WRITTEN (type)
20629 && TREE_CODE (type) == ARRAY_TYPE
20630 && variably_modified_type_p (type, NULL))
20632 dw_die_ref array_die = lookup_type_die (type);
20633 if (!array_die)
20634 return false;
20635 add_subscript_info (array_die, type, !is_ada ());
20636 return true;
20638 return false;
20641 /* These routines generate the internal representation of the DIE's for
20642 the compilation unit. Debugging information is collected by walking
20643 the declaration trees passed in from dwarf2out_decl(). */
20645 static void
20646 gen_array_type_die (tree type, dw_die_ref context_die)
20648 dw_die_ref array_die;
20650 /* GNU compilers represent multidimensional array types as sequences of one
20651 dimensional array types whose element types are themselves array types.
20652 We sometimes squish that down to a single array_type DIE with multiple
20653 subscripts in the Dwarf debugging info. The draft Dwarf specification
20654 say that we are allowed to do this kind of compression in C, because
20655 there is no difference between an array of arrays and a multidimensional
20656 array. We don't do this for Ada to remain as close as possible to the
20657 actual representation, which is especially important against the language
20658 flexibilty wrt arrays of variable size. */
20660 bool collapse_nested_arrays = !is_ada ();
20662 if (fill_variable_array_bounds (type))
20663 return;
20665 dw_die_ref scope_die = scope_die_for (type, context_die);
20666 tree element_type;
20668 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20669 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20670 if (TYPE_STRING_FLAG (type)
20671 && TREE_CODE (type) == ARRAY_TYPE
20672 && is_fortran ()
20673 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20675 HOST_WIDE_INT size;
20677 array_die = new_die (DW_TAG_string_type, scope_die, type);
20678 add_name_attribute (array_die, type_tag (type));
20679 equate_type_number_to_die (type, array_die);
20680 size = int_size_in_bytes (type);
20681 if (size >= 0)
20682 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20683 else if (TYPE_DOMAIN (type) != NULL_TREE
20684 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20686 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20687 tree rszdecl = szdecl;
20688 HOST_WIDE_INT rsize = 0;
20690 size = int_size_in_bytes (TREE_TYPE (szdecl));
20691 if (!DECL_P (szdecl))
20693 if (TREE_CODE (szdecl) == INDIRECT_REF
20694 && DECL_P (TREE_OPERAND (szdecl, 0)))
20696 rszdecl = TREE_OPERAND (szdecl, 0);
20697 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
20698 if (rsize <= 0)
20699 size = 0;
20701 else
20702 size = 0;
20704 if (size > 0)
20706 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
20707 if (loc == NULL
20708 && early_dwarf
20709 && current_function_decl
20710 && DECL_CONTEXT (rszdecl) == current_function_decl)
20712 dw_die_ref ref = lookup_decl_die (rszdecl);
20713 dw_loc_descr_ref l = NULL;
20714 if (ref)
20716 l = new_loc_descr (DW_OP_call4, 0, 0);
20717 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20718 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
20719 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
20721 else if (TREE_CODE (rszdecl) == PARM_DECL
20722 && string_types)
20724 l = new_loc_descr (DW_OP_call4, 0, 0);
20725 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
20726 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
20727 string_types->safe_push (array_die);
20729 if (l && rszdecl != szdecl)
20731 if (rsize == DWARF2_ADDR_SIZE)
20732 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
20733 0, 0));
20734 else
20735 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
20736 rsize, 0));
20738 if (l)
20739 loc = new_loc_list (l, NULL, NULL, NULL);
20741 if (loc)
20743 add_AT_location_description (array_die, DW_AT_string_length,
20744 loc);
20745 if (size != DWARF2_ADDR_SIZE)
20746 add_AT_unsigned (array_die, dwarf_version >= 5
20747 ? DW_AT_string_length_byte_size
20748 : DW_AT_byte_size, size);
20752 return;
20755 array_die = new_die (DW_TAG_array_type, scope_die, type);
20756 add_name_attribute (array_die, type_tag (type));
20757 equate_type_number_to_die (type, array_die);
20759 if (TREE_CODE (type) == VECTOR_TYPE)
20760 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20762 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20763 if (is_fortran ()
20764 && TREE_CODE (type) == ARRAY_TYPE
20765 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20766 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20767 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20769 #if 0
20770 /* We default the array ordering. SDB will probably do
20771 the right things even if DW_AT_ordering is not present. It's not even
20772 an issue until we start to get into multidimensional arrays anyway. If
20773 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20774 then we'll have to put the DW_AT_ordering attribute back in. (But if
20775 and when we find out that we need to put these in, we will only do so
20776 for multidimensional arrays. */
20777 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20778 #endif
20780 if (TREE_CODE (type) == VECTOR_TYPE)
20782 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20783 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20784 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20785 add_bound_info (subrange_die, DW_AT_upper_bound,
20786 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20788 else
20789 add_subscript_info (array_die, type, collapse_nested_arrays);
20791 /* Add representation of the type of the elements of this array type and
20792 emit the corresponding DIE if we haven't done it already. */
20793 element_type = TREE_TYPE (type);
20794 if (collapse_nested_arrays)
20795 while (TREE_CODE (element_type) == ARRAY_TYPE)
20797 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20798 break;
20799 element_type = TREE_TYPE (element_type);
20802 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20803 TREE_CODE (type) == ARRAY_TYPE
20804 && TYPE_REVERSE_STORAGE_ORDER (type),
20805 context_die);
20807 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20808 if (TYPE_ARTIFICIAL (type))
20809 add_AT_flag (array_die, DW_AT_artificial, 1);
20811 if (get_AT (array_die, DW_AT_name))
20812 add_pubtype (type, array_die);
20814 add_alignment_attribute (array_die, type);
20817 /* After all arguments are created, adjust any DW_TAG_string_type
20818 DIEs DW_AT_string_length attributes. */
20820 static void
20821 adjust_string_types (void)
20823 dw_die_ref array_die;
20824 unsigned int i;
20825 FOR_EACH_VEC_ELT (*string_types, i, array_die)
20827 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
20828 if (a == NULL)
20829 continue;
20830 dw_loc_descr_ref loc = AT_loc (a);
20831 gcc_assert (loc->dw_loc_opc == DW_OP_call4
20832 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
20833 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20834 if (ref)
20836 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20837 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20838 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20840 else
20842 remove_AT (array_die, DW_AT_string_length);
20843 remove_AT (array_die, dwarf_version >= 5
20844 ? DW_AT_string_length_byte_size
20845 : DW_AT_byte_size);
20850 /* This routine generates DIE for array with hidden descriptor, details
20851 are filled into *info by a langhook. */
20853 static void
20854 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20855 dw_die_ref context_die)
20857 const dw_die_ref scope_die = scope_die_for (type, context_die);
20858 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20859 struct loc_descr_context context = { type, info->base_decl, NULL,
20860 false, false };
20861 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20862 int dim;
20864 add_name_attribute (array_die, type_tag (type));
20865 equate_type_number_to_die (type, array_die);
20867 if (info->ndimensions > 1)
20868 switch (info->ordering)
20870 case array_descr_ordering_row_major:
20871 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20872 break;
20873 case array_descr_ordering_column_major:
20874 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20875 break;
20876 default:
20877 break;
20880 if (dwarf_version >= 3 || !dwarf_strict)
20882 if (info->data_location)
20883 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20884 dw_scalar_form_exprloc, &context);
20885 if (info->associated)
20886 add_scalar_info (array_die, DW_AT_associated, info->associated,
20887 dw_scalar_form_constant
20888 | dw_scalar_form_exprloc
20889 | dw_scalar_form_reference, &context);
20890 if (info->allocated)
20891 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20892 dw_scalar_form_constant
20893 | dw_scalar_form_exprloc
20894 | dw_scalar_form_reference, &context);
20895 if (info->stride)
20897 const enum dwarf_attribute attr
20898 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20899 const int forms
20900 = (info->stride_in_bits)
20901 ? dw_scalar_form_constant
20902 : (dw_scalar_form_constant
20903 | dw_scalar_form_exprloc
20904 | dw_scalar_form_reference);
20906 add_scalar_info (array_die, attr, info->stride, forms, &context);
20909 if (dwarf_version >= 5)
20911 if (info->rank)
20913 add_scalar_info (array_die, DW_AT_rank, info->rank,
20914 dw_scalar_form_constant
20915 | dw_scalar_form_exprloc, &context);
20916 subrange_tag = DW_TAG_generic_subrange;
20917 context.placeholder_arg = true;
20921 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20923 for (dim = 0; dim < info->ndimensions; dim++)
20925 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20927 if (info->dimen[dim].bounds_type)
20928 add_type_attribute (subrange_die,
20929 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20930 false, context_die);
20931 if (info->dimen[dim].lower_bound)
20932 add_bound_info (subrange_die, DW_AT_lower_bound,
20933 info->dimen[dim].lower_bound, &context);
20934 if (info->dimen[dim].upper_bound)
20935 add_bound_info (subrange_die, DW_AT_upper_bound,
20936 info->dimen[dim].upper_bound, &context);
20937 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20938 add_scalar_info (subrange_die, DW_AT_byte_stride,
20939 info->dimen[dim].stride,
20940 dw_scalar_form_constant
20941 | dw_scalar_form_exprloc
20942 | dw_scalar_form_reference,
20943 &context);
20946 gen_type_die (info->element_type, context_die);
20947 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20948 TREE_CODE (type) == ARRAY_TYPE
20949 && TYPE_REVERSE_STORAGE_ORDER (type),
20950 context_die);
20952 if (get_AT (array_die, DW_AT_name))
20953 add_pubtype (type, array_die);
20955 add_alignment_attribute (array_die, type);
20958 #if 0
20959 static void
20960 gen_entry_point_die (tree decl, dw_die_ref context_die)
20962 tree origin = decl_ultimate_origin (decl);
20963 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20965 if (origin != NULL)
20966 add_abstract_origin_attribute (decl_die, origin);
20967 else
20969 add_name_and_src_coords_attributes (decl_die, decl);
20970 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20971 TYPE_UNQUALIFIED, false, context_die);
20974 if (DECL_ABSTRACT_P (decl))
20975 equate_decl_number_to_die (decl, decl_die);
20976 else
20977 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20979 #endif
20981 /* Walk through the list of incomplete types again, trying once more to
20982 emit full debugging info for them. */
20984 static void
20985 retry_incomplete_types (void)
20987 set_early_dwarf s;
20988 int i;
20990 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
20991 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
20992 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
20993 vec_safe_truncate (incomplete_types, 0);
20996 /* Determine what tag to use for a record type. */
20998 static enum dwarf_tag
20999 record_type_tag (tree type)
21001 if (! lang_hooks.types.classify_record)
21002 return DW_TAG_structure_type;
21004 switch (lang_hooks.types.classify_record (type))
21006 case RECORD_IS_STRUCT:
21007 return DW_TAG_structure_type;
21009 case RECORD_IS_CLASS:
21010 return DW_TAG_class_type;
21012 case RECORD_IS_INTERFACE:
21013 if (dwarf_version >= 3 || !dwarf_strict)
21014 return DW_TAG_interface_type;
21015 return DW_TAG_structure_type;
21017 default:
21018 gcc_unreachable ();
21022 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21023 include all of the information about the enumeration values also. Each
21024 enumerated type name/value is listed as a child of the enumerated type
21025 DIE. */
21027 static dw_die_ref
21028 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21030 dw_die_ref type_die = lookup_type_die (type);
21032 if (type_die == NULL)
21034 type_die = new_die (DW_TAG_enumeration_type,
21035 scope_die_for (type, context_die), type);
21036 equate_type_number_to_die (type, type_die);
21037 add_name_attribute (type_die, type_tag (type));
21038 if (dwarf_version >= 4 || !dwarf_strict)
21040 if (ENUM_IS_SCOPED (type))
21041 add_AT_flag (type_die, DW_AT_enum_class, 1);
21042 if (ENUM_IS_OPAQUE (type))
21043 add_AT_flag (type_die, DW_AT_declaration, 1);
21045 if (!dwarf_strict)
21046 add_AT_unsigned (type_die, DW_AT_encoding,
21047 TYPE_UNSIGNED (type)
21048 ? DW_ATE_unsigned
21049 : DW_ATE_signed);
21051 else if (! TYPE_SIZE (type))
21052 return type_die;
21053 else
21054 remove_AT (type_die, DW_AT_declaration);
21056 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21057 given enum type is incomplete, do not generate the DW_AT_byte_size
21058 attribute or the DW_AT_element_list attribute. */
21059 if (TYPE_SIZE (type))
21061 tree link;
21063 TREE_ASM_WRITTEN (type) = 1;
21064 add_byte_size_attribute (type_die, type);
21065 add_alignment_attribute (type_die, type);
21066 if (dwarf_version >= 3 || !dwarf_strict)
21068 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21069 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21070 context_die);
21072 if (TYPE_STUB_DECL (type) != NULL_TREE)
21074 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21075 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21078 /* If the first reference to this type was as the return type of an
21079 inline function, then it may not have a parent. Fix this now. */
21080 if (type_die->die_parent == NULL)
21081 add_child_die (scope_die_for (type, context_die), type_die);
21083 for (link = TYPE_VALUES (type);
21084 link != NULL; link = TREE_CHAIN (link))
21086 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21087 tree value = TREE_VALUE (link);
21089 add_name_attribute (enum_die,
21090 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21092 if (TREE_CODE (value) == CONST_DECL)
21093 value = DECL_INITIAL (value);
21095 if (simple_type_size_in_bits (TREE_TYPE (value))
21096 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21098 /* For constant forms created by add_AT_unsigned DWARF
21099 consumers (GDB, elfutils, etc.) always zero extend
21100 the value. Only when the actual value is negative
21101 do we need to use add_AT_int to generate a constant
21102 form that can represent negative values. */
21103 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21104 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21105 add_AT_unsigned (enum_die, DW_AT_const_value,
21106 (unsigned HOST_WIDE_INT) val);
21107 else
21108 add_AT_int (enum_die, DW_AT_const_value, val);
21110 else
21111 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21112 that here. TODO: This should be re-worked to use correct
21113 signed/unsigned double tags for all cases. */
21114 add_AT_wide (enum_die, DW_AT_const_value, value);
21117 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21118 if (TYPE_ARTIFICIAL (type))
21119 add_AT_flag (type_die, DW_AT_artificial, 1);
21121 else
21122 add_AT_flag (type_die, DW_AT_declaration, 1);
21124 add_alignment_attribute (type_die, type);
21126 add_pubtype (type, type_die);
21128 return type_die;
21131 /* Generate a DIE to represent either a real live formal parameter decl or to
21132 represent just the type of some formal parameter position in some function
21133 type.
21135 Note that this routine is a bit unusual because its argument may be a
21136 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21137 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21138 node. If it's the former then this function is being called to output a
21139 DIE to represent a formal parameter object (or some inlining thereof). If
21140 it's the latter, then this function is only being called to output a
21141 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21142 argument type of some subprogram type.
21143 If EMIT_NAME_P is true, name and source coordinate attributes
21144 are emitted. */
21146 static dw_die_ref
21147 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21148 dw_die_ref context_die)
21150 tree node_or_origin = node ? node : origin;
21151 tree ultimate_origin;
21152 dw_die_ref parm_die = NULL;
21154 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21156 parm_die = lookup_decl_die (node);
21158 /* If the contexts differ, we may not be talking about the same
21159 thing. */
21160 if (parm_die && parm_die->die_parent != context_die)
21162 if (!DECL_ABSTRACT_P (node))
21164 /* This can happen when creating an inlined instance, in
21165 which case we need to create a new DIE that will get
21166 annotated with DW_AT_abstract_origin. */
21167 parm_die = NULL;
21169 else
21171 /* FIXME: Reuse DIE even with a differing context.
21173 This can happen when calling
21174 dwarf2out_abstract_function to build debug info for
21175 the abstract instance of a function for which we have
21176 already generated a DIE in
21177 dwarf2out_early_global_decl.
21179 Once we remove dwarf2out_abstract_function, we should
21180 have a call to gcc_unreachable here. */
21184 if (parm_die && parm_die->die_parent == NULL)
21186 /* Check that parm_die already has the right attributes that
21187 we would have added below. If any attributes are
21188 missing, fall through to add them. */
21189 if (! DECL_ABSTRACT_P (node_or_origin)
21190 && !get_AT (parm_die, DW_AT_location)
21191 && !get_AT (parm_die, DW_AT_const_value))
21192 /* We are missing location info, and are about to add it. */
21194 else
21196 add_child_die (context_die, parm_die);
21197 return parm_die;
21202 /* If we have a previously generated DIE, use it, unless this is an
21203 concrete instance (origin != NULL), in which case we need a new
21204 DIE with a corresponding DW_AT_abstract_origin. */
21205 bool reusing_die;
21206 if (parm_die && origin == NULL)
21207 reusing_die = true;
21208 else
21210 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21211 reusing_die = false;
21214 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21216 case tcc_declaration:
21217 ultimate_origin = decl_ultimate_origin (node_or_origin);
21218 if (node || ultimate_origin)
21219 origin = ultimate_origin;
21221 if (reusing_die)
21222 goto add_location;
21224 if (origin != NULL)
21225 add_abstract_origin_attribute (parm_die, origin);
21226 else if (emit_name_p)
21227 add_name_and_src_coords_attributes (parm_die, node);
21228 if (origin == NULL
21229 || (! DECL_ABSTRACT_P (node_or_origin)
21230 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21231 decl_function_context
21232 (node_or_origin))))
21234 tree type = TREE_TYPE (node_or_origin);
21235 if (decl_by_reference_p (node_or_origin))
21236 add_type_attribute (parm_die, TREE_TYPE (type),
21237 TYPE_UNQUALIFIED,
21238 false, context_die);
21239 else
21240 add_type_attribute (parm_die, type,
21241 decl_quals (node_or_origin),
21242 false, context_die);
21244 if (origin == NULL && DECL_ARTIFICIAL (node))
21245 add_AT_flag (parm_die, DW_AT_artificial, 1);
21246 add_location:
21247 if (node && node != origin)
21248 equate_decl_number_to_die (node, parm_die);
21249 if (! DECL_ABSTRACT_P (node_or_origin))
21250 add_location_or_const_value_attribute (parm_die, node_or_origin,
21251 node == NULL);
21253 break;
21255 case tcc_type:
21256 /* We were called with some kind of a ..._TYPE node. */
21257 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21258 context_die);
21259 break;
21261 default:
21262 gcc_unreachable ();
21265 return parm_die;
21268 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21269 children DW_TAG_formal_parameter DIEs representing the arguments of the
21270 parameter pack.
21272 PARM_PACK must be a function parameter pack.
21273 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21274 must point to the subsequent arguments of the function PACK_ARG belongs to.
21275 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21276 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21277 following the last one for which a DIE was generated. */
21279 static dw_die_ref
21280 gen_formal_parameter_pack_die (tree parm_pack,
21281 tree pack_arg,
21282 dw_die_ref subr_die,
21283 tree *next_arg)
21285 tree arg;
21286 dw_die_ref parm_pack_die;
21288 gcc_assert (parm_pack
21289 && lang_hooks.function_parameter_pack_p (parm_pack)
21290 && subr_die);
21292 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21293 add_src_coords_attributes (parm_pack_die, parm_pack);
21295 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21297 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21298 parm_pack))
21299 break;
21300 gen_formal_parameter_die (arg, NULL,
21301 false /* Don't emit name attribute. */,
21302 parm_pack_die);
21304 if (next_arg)
21305 *next_arg = arg;
21306 return parm_pack_die;
21309 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21310 at the end of an (ANSI prototyped) formal parameters list. */
21312 static void
21313 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21315 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21318 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21319 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21320 parameters as specified in some function type specification (except for
21321 those which appear as part of a function *definition*). */
21323 static void
21324 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21326 tree link;
21327 tree formal_type = NULL;
21328 tree first_parm_type;
21329 tree arg;
21331 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21333 arg = DECL_ARGUMENTS (function_or_method_type);
21334 function_or_method_type = TREE_TYPE (function_or_method_type);
21336 else
21337 arg = NULL_TREE;
21339 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21341 /* Make our first pass over the list of formal parameter types and output a
21342 DW_TAG_formal_parameter DIE for each one. */
21343 for (link = first_parm_type; link; )
21345 dw_die_ref parm_die;
21347 formal_type = TREE_VALUE (link);
21348 if (formal_type == void_type_node)
21349 break;
21351 /* Output a (nameless) DIE to represent the formal parameter itself. */
21352 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21354 parm_die = gen_formal_parameter_die (formal_type, NULL,
21355 true /* Emit name attribute. */,
21356 context_die);
21357 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21358 && link == first_parm_type)
21360 add_AT_flag (parm_die, DW_AT_artificial, 1);
21361 if (dwarf_version >= 3 || !dwarf_strict)
21362 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21364 else if (arg && DECL_ARTIFICIAL (arg))
21365 add_AT_flag (parm_die, DW_AT_artificial, 1);
21368 link = TREE_CHAIN (link);
21369 if (arg)
21370 arg = DECL_CHAIN (arg);
21373 /* If this function type has an ellipsis, add a
21374 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21375 if (formal_type != void_type_node)
21376 gen_unspecified_parameters_die (function_or_method_type, context_die);
21378 /* Make our second (and final) pass over the list of formal parameter types
21379 and output DIEs to represent those types (as necessary). */
21380 for (link = TYPE_ARG_TYPES (function_or_method_type);
21381 link && TREE_VALUE (link);
21382 link = TREE_CHAIN (link))
21383 gen_type_die (TREE_VALUE (link), context_die);
21386 /* We want to generate the DIE for TYPE so that we can generate the
21387 die for MEMBER, which has been defined; we will need to refer back
21388 to the member declaration nested within TYPE. If we're trying to
21389 generate minimal debug info for TYPE, processing TYPE won't do the
21390 trick; we need to attach the member declaration by hand. */
21392 static void
21393 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21395 gen_type_die (type, context_die);
21397 /* If we're trying to avoid duplicate debug info, we may not have
21398 emitted the member decl for this function. Emit it now. */
21399 if (TYPE_STUB_DECL (type)
21400 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21401 && ! lookup_decl_die (member))
21403 dw_die_ref type_die;
21404 gcc_assert (!decl_ultimate_origin (member));
21406 push_decl_scope (type);
21407 type_die = lookup_type_die_strip_naming_typedef (type);
21408 if (TREE_CODE (member) == FUNCTION_DECL)
21409 gen_subprogram_die (member, type_die);
21410 else if (TREE_CODE (member) == FIELD_DECL)
21412 /* Ignore the nameless fields that are used to skip bits but handle
21413 C++ anonymous unions and structs. */
21414 if (DECL_NAME (member) != NULL_TREE
21415 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21416 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21418 struct vlr_context vlr_ctx = {
21419 DECL_CONTEXT (member), /* struct_type */
21420 NULL_TREE /* variant_part_offset */
21422 gen_type_die (member_declared_type (member), type_die);
21423 gen_field_die (member, &vlr_ctx, type_die);
21426 else
21427 gen_variable_die (member, NULL_TREE, type_die);
21429 pop_decl_scope ();
21433 /* Forward declare these functions, because they are mutually recursive
21434 with their set_block_* pairing functions. */
21435 static void set_decl_origin_self (tree);
21436 static void set_decl_abstract_flags (tree, vec<tree> &);
21438 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21439 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21440 that it points to the node itself, thus indicating that the node is its
21441 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21442 the given node is NULL, recursively descend the decl/block tree which
21443 it is the root of, and for each other ..._DECL or BLOCK node contained
21444 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21445 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21446 values to point to themselves. */
21448 static void
21449 set_block_origin_self (tree stmt)
21451 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21453 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21456 tree local_decl;
21458 for (local_decl = BLOCK_VARS (stmt);
21459 local_decl != NULL_TREE;
21460 local_decl = DECL_CHAIN (local_decl))
21461 /* Do not recurse on nested functions since the inlining status
21462 of parent and child can be different as per the DWARF spec. */
21463 if (TREE_CODE (local_decl) != FUNCTION_DECL
21464 && !DECL_EXTERNAL (local_decl))
21465 set_decl_origin_self (local_decl);
21469 tree subblock;
21471 for (subblock = BLOCK_SUBBLOCKS (stmt);
21472 subblock != NULL_TREE;
21473 subblock = BLOCK_CHAIN (subblock))
21474 set_block_origin_self (subblock); /* Recurse. */
21479 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21480 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21481 node to so that it points to the node itself, thus indicating that the
21482 node represents its own (abstract) origin. Additionally, if the
21483 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21484 the decl/block tree of which the given node is the root of, and for
21485 each other ..._DECL or BLOCK node contained therein whose
21486 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21487 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21488 point to themselves. */
21490 static void
21491 set_decl_origin_self (tree decl)
21493 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21495 DECL_ABSTRACT_ORIGIN (decl) = decl;
21496 if (TREE_CODE (decl) == FUNCTION_DECL)
21498 tree arg;
21500 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21501 DECL_ABSTRACT_ORIGIN (arg) = arg;
21502 if (DECL_INITIAL (decl) != NULL_TREE
21503 && DECL_INITIAL (decl) != error_mark_node)
21504 set_block_origin_self (DECL_INITIAL (decl));
21509 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21510 and if it wasn't 1 before, push it to abstract_vec vector.
21511 For all local decls and all local sub-blocks (recursively) do it
21512 too. */
21514 static void
21515 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21517 tree local_decl;
21518 tree subblock;
21519 unsigned int i;
21521 if (!BLOCK_ABSTRACT (stmt))
21523 abstract_vec.safe_push (stmt);
21524 BLOCK_ABSTRACT (stmt) = 1;
21527 for (local_decl = BLOCK_VARS (stmt);
21528 local_decl != NULL_TREE;
21529 local_decl = DECL_CHAIN (local_decl))
21530 if (! DECL_EXTERNAL (local_decl))
21531 set_decl_abstract_flags (local_decl, abstract_vec);
21533 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21535 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21536 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21537 || TREE_CODE (local_decl) == PARM_DECL)
21538 set_decl_abstract_flags (local_decl, abstract_vec);
21541 for (subblock = BLOCK_SUBBLOCKS (stmt);
21542 subblock != NULL_TREE;
21543 subblock = BLOCK_CHAIN (subblock))
21544 set_block_abstract_flags (subblock, abstract_vec);
21547 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21548 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21549 In the case where the decl is a FUNCTION_DECL also set the abstract
21550 flags for all of the parameters, local vars, local
21551 blocks and sub-blocks (recursively). */
21553 static void
21554 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21556 if (!DECL_ABSTRACT_P (decl))
21558 abstract_vec.safe_push (decl);
21559 DECL_ABSTRACT_P (decl) = 1;
21562 if (TREE_CODE (decl) == FUNCTION_DECL)
21564 tree arg;
21566 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21567 if (!DECL_ABSTRACT_P (arg))
21569 abstract_vec.safe_push (arg);
21570 DECL_ABSTRACT_P (arg) = 1;
21572 if (DECL_INITIAL (decl) != NULL_TREE
21573 && DECL_INITIAL (decl) != error_mark_node)
21574 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21578 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21579 may later generate inlined and/or out-of-line instances of.
21581 FIXME: In the early-dwarf world, this function, and most of the
21582 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21583 the abstract instance. All we would need to do is annotate
21584 the early DIE with the appropriate DW_AT_inline in late
21585 dwarf (perhaps in gen_inlined_subroutine_die).
21587 However, we can't do this yet, because LTO streaming of DIEs
21588 has not been implemented yet. */
21590 static void
21591 dwarf2out_abstract_function (tree decl)
21593 dw_die_ref old_die;
21594 tree save_fn;
21595 tree context;
21596 hash_table<decl_loc_hasher> *old_decl_loc_table;
21597 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21598 int old_call_site_count, old_tail_call_site_count;
21599 struct call_arg_loc_node *old_call_arg_locations;
21601 /* Make sure we have the actual abstract inline, not a clone. */
21602 decl = DECL_ORIGIN (decl);
21604 old_die = lookup_decl_die (decl);
21605 if (old_die && get_AT (old_die, DW_AT_inline))
21606 /* We've already generated the abstract instance. */
21607 return;
21609 /* We can be called while recursively when seeing block defining inlined subroutine
21610 DIE. Be sure to not clobber the outer location table nor use it or we would
21611 get locations in abstract instantces. */
21612 old_decl_loc_table = decl_loc_table;
21613 decl_loc_table = NULL;
21614 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21615 cached_dw_loc_list_table = NULL;
21616 old_call_arg_locations = call_arg_locations;
21617 call_arg_locations = NULL;
21618 old_call_site_count = call_site_count;
21619 call_site_count = -1;
21620 old_tail_call_site_count = tail_call_site_count;
21621 tail_call_site_count = -1;
21623 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21624 we don't get confused by DECL_ABSTRACT_P. */
21625 if (debug_info_level > DINFO_LEVEL_TERSE)
21627 context = decl_class_context (decl);
21628 if (context)
21629 gen_type_die_for_member
21630 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21633 /* Pretend we've just finished compiling this function. */
21634 save_fn = current_function_decl;
21635 current_function_decl = decl;
21637 auto_vec<tree, 64> abstract_vec;
21638 set_decl_abstract_flags (decl, abstract_vec);
21639 dwarf2out_decl (decl);
21640 unsigned int i;
21641 tree t;
21642 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21643 if (TREE_CODE (t) == BLOCK)
21644 BLOCK_ABSTRACT (t) = 0;
21645 else
21646 DECL_ABSTRACT_P (t) = 0;
21648 current_function_decl = save_fn;
21649 decl_loc_table = old_decl_loc_table;
21650 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21651 call_arg_locations = old_call_arg_locations;
21652 call_site_count = old_call_site_count;
21653 tail_call_site_count = old_tail_call_site_count;
21656 /* Helper function of premark_used_types() which gets called through
21657 htab_traverse.
21659 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21660 marked as unused by prune_unused_types. */
21662 bool
21663 premark_used_types_helper (tree const &type, void *)
21665 dw_die_ref die;
21667 die = lookup_type_die (type);
21668 if (die != NULL)
21669 die->die_perennial_p = 1;
21670 return true;
21673 /* Helper function of premark_types_used_by_global_vars which gets called
21674 through htab_traverse.
21676 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21677 marked as unused by prune_unused_types. The DIE of the type is marked
21678 only if the global variable using the type will actually be emitted. */
21681 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21682 void *)
21684 struct types_used_by_vars_entry *entry;
21685 dw_die_ref die;
21687 entry = (struct types_used_by_vars_entry *) *slot;
21688 gcc_assert (entry->type != NULL
21689 && entry->var_decl != NULL);
21690 die = lookup_type_die (entry->type);
21691 if (die)
21693 /* Ask cgraph if the global variable really is to be emitted.
21694 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21695 varpool_node *node = varpool_node::get (entry->var_decl);
21696 if (node && node->definition)
21698 die->die_perennial_p = 1;
21699 /* Keep the parent DIEs as well. */
21700 while ((die = die->die_parent) && die->die_perennial_p == 0)
21701 die->die_perennial_p = 1;
21704 return 1;
21707 /* Mark all members of used_types_hash as perennial. */
21709 static void
21710 premark_used_types (struct function *fun)
21712 if (fun && fun->used_types_hash)
21713 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21716 /* Mark all members of types_used_by_vars_entry as perennial. */
21718 static void
21719 premark_types_used_by_global_vars (void)
21721 if (types_used_by_vars_hash)
21722 types_used_by_vars_hash
21723 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21726 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21727 for CA_LOC call arg loc node. */
21729 static dw_die_ref
21730 gen_call_site_die (tree decl, dw_die_ref subr_die,
21731 struct call_arg_loc_node *ca_loc)
21733 dw_die_ref stmt_die = NULL, die;
21734 tree block = ca_loc->block;
21736 while (block
21737 && block != DECL_INITIAL (decl)
21738 && TREE_CODE (block) == BLOCK)
21740 stmt_die = BLOCK_DIE (block);
21741 if (stmt_die)
21742 break;
21743 block = BLOCK_SUPERCONTEXT (block);
21745 if (stmt_die == NULL)
21746 stmt_die = subr_die;
21747 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21748 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21749 if (ca_loc->tail_call_p)
21750 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21751 if (ca_loc->symbol_ref)
21753 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21754 if (tdie)
21755 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21756 else
21757 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21758 false);
21760 return die;
21763 /* Generate a DIE to represent a declared function (either file-scope or
21764 block-local). */
21766 static void
21767 gen_subprogram_die (tree decl, dw_die_ref context_die)
21769 tree origin = decl_ultimate_origin (decl);
21770 dw_die_ref subr_die;
21771 dw_die_ref old_die = lookup_decl_die (decl);
21773 /* This function gets called multiple times for different stages of
21774 the debug process. For example, for func() in this code:
21776 namespace S
21778 void func() { ... }
21781 ...we get called 4 times. Twice in early debug and twice in
21782 late debug:
21784 Early debug
21785 -----------
21787 1. Once while generating func() within the namespace. This is
21788 the declaration. The declaration bit below is set, as the
21789 context is the namespace.
21791 A new DIE will be generated with DW_AT_declaration set.
21793 2. Once for func() itself. This is the specification. The
21794 declaration bit below is clear as the context is the CU.
21796 We will use the cached DIE from (1) to create a new DIE with
21797 DW_AT_specification pointing to the declaration in (1).
21799 Late debug via rest_of_handle_final()
21800 -------------------------------------
21802 3. Once generating func() within the namespace. This is also the
21803 declaration, as in (1), but this time we will early exit below
21804 as we have a cached DIE and a declaration needs no additional
21805 annotations (no locations), as the source declaration line
21806 info is enough.
21808 4. Once for func() itself. As in (2), this is the specification,
21809 but this time we will re-use the cached DIE, and just annotate
21810 it with the location information that should now be available.
21812 For something without namespaces, but with abstract instances, we
21813 are also called a multiple times:
21815 class Base
21817 public:
21818 Base (); // constructor declaration (1)
21821 Base::Base () { } // constructor specification (2)
21823 Early debug
21824 -----------
21826 1. Once for the Base() constructor by virtue of it being a
21827 member of the Base class. This is done via
21828 rest_of_type_compilation.
21830 This is a declaration, so a new DIE will be created with
21831 DW_AT_declaration.
21833 2. Once for the Base() constructor definition, but this time
21834 while generating the abstract instance of the base
21835 constructor (__base_ctor) which is being generated via early
21836 debug of reachable functions.
21838 Even though we have a cached version of the declaration (1),
21839 we will create a DW_AT_specification of the declaration DIE
21840 in (1).
21842 3. Once for the __base_ctor itself, but this time, we generate
21843 an DW_AT_abstract_origin version of the DW_AT_specification in
21844 (2).
21846 Late debug via rest_of_handle_final
21847 -----------------------------------
21849 4. One final time for the __base_ctor (which will have a cached
21850 DIE with DW_AT_abstract_origin created in (3). This time,
21851 we will just annotate the location information now
21852 available.
21854 int declaration = (current_function_decl != decl
21855 || class_or_namespace_scope_p (context_die));
21857 /* Now that the C++ front end lazily declares artificial member fns, we
21858 might need to retrofit the declaration into its class. */
21859 if (!declaration && !origin && !old_die
21860 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21861 && !class_or_namespace_scope_p (context_die)
21862 && debug_info_level > DINFO_LEVEL_TERSE)
21863 old_die = force_decl_die (decl);
21865 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21866 if (origin != NULL)
21868 gcc_assert (!declaration || local_scope_p (context_die));
21870 /* Fixup die_parent for the abstract instance of a nested
21871 inline function. */
21872 if (old_die && old_die->die_parent == NULL)
21873 add_child_die (context_die, old_die);
21875 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21877 /* If we have a DW_AT_abstract_origin we have a working
21878 cached version. */
21879 subr_die = old_die;
21881 else
21883 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21884 add_abstract_origin_attribute (subr_die, origin);
21885 /* This is where the actual code for a cloned function is.
21886 Let's emit linkage name attribute for it. This helps
21887 debuggers to e.g, set breakpoints into
21888 constructors/destructors when the user asks "break
21889 K::K". */
21890 add_linkage_name (subr_die, decl);
21893 /* A cached copy, possibly from early dwarf generation. Reuse as
21894 much as possible. */
21895 else if (old_die)
21897 /* A declaration that has been previously dumped needs no
21898 additional information. */
21899 if (declaration)
21900 return;
21902 if (!get_AT_flag (old_die, DW_AT_declaration)
21903 /* We can have a normal definition following an inline one in the
21904 case of redefinition of GNU C extern inlines.
21905 It seems reasonable to use AT_specification in this case. */
21906 && !get_AT (old_die, DW_AT_inline))
21908 /* Detect and ignore this case, where we are trying to output
21909 something we have already output. */
21910 if (get_AT (old_die, DW_AT_low_pc)
21911 || get_AT (old_die, DW_AT_ranges))
21912 return;
21914 /* If we have no location information, this must be a
21915 partially generated DIE from early dwarf generation.
21916 Fall through and generate it. */
21919 /* If the definition comes from the same place as the declaration,
21920 maybe use the old DIE. We always want the DIE for this function
21921 that has the *_pc attributes to be under comp_unit_die so the
21922 debugger can find it. We also need to do this for abstract
21923 instances of inlines, since the spec requires the out-of-line copy
21924 to have the same parent. For local class methods, this doesn't
21925 apply; we just use the old DIE. */
21926 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21927 struct dwarf_file_data * file_index = lookup_filename (s.file);
21928 if ((is_cu_die (old_die->die_parent)
21929 /* This condition fixes the inconsistency/ICE with the
21930 following Fortran test (or some derivative thereof) while
21931 building libgfortran:
21933 module some_m
21934 contains
21935 logical function funky (FLAG)
21936 funky = .true.
21937 end function
21938 end module
21940 || (old_die->die_parent
21941 && old_die->die_parent->die_tag == DW_TAG_module)
21942 || context_die == NULL)
21943 && (DECL_ARTIFICIAL (decl)
21944 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21945 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21946 == (unsigned) s.line)
21947 && (!debug_column_info
21948 || s.column == 0
21949 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21950 == (unsigned) s.column)))))
21952 subr_die = old_die;
21954 /* Clear out the declaration attribute, but leave the
21955 parameters so they can be augmented with location
21956 information later. Unless this was a declaration, in
21957 which case, wipe out the nameless parameters and recreate
21958 them further down. */
21959 if (remove_AT (subr_die, DW_AT_declaration))
21962 remove_AT (subr_die, DW_AT_object_pointer);
21963 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21966 /* Make a specification pointing to the previously built
21967 declaration. */
21968 else
21970 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21971 add_AT_specification (subr_die, old_die);
21972 add_pubname (decl, subr_die);
21973 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21974 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21975 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21976 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21977 if (debug_column_info
21978 && s.column
21979 && (get_AT_unsigned (old_die, DW_AT_decl_column)
21980 != (unsigned) s.column))
21981 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
21983 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21984 emit the real type on the definition die. */
21985 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
21987 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21988 if (die == auto_die || die == decltype_auto_die)
21989 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21990 TYPE_UNQUALIFIED, false, context_die);
21993 /* When we process the method declaration, we haven't seen
21994 the out-of-class defaulted definition yet, so we have to
21995 recheck now. */
21996 if ((dwarf_version >= 5 || ! dwarf_strict)
21997 && !get_AT (subr_die, DW_AT_defaulted))
21999 int defaulted
22000 = lang_hooks.decls.decl_dwarf_attribute (decl,
22001 DW_AT_defaulted);
22002 if (defaulted != -1)
22004 /* Other values must have been handled before. */
22005 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22006 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22011 /* Create a fresh DIE for anything else. */
22012 else
22014 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22016 if (TREE_PUBLIC (decl))
22017 add_AT_flag (subr_die, DW_AT_external, 1);
22019 add_name_and_src_coords_attributes (subr_die, decl);
22020 add_pubname (decl, subr_die);
22021 if (debug_info_level > DINFO_LEVEL_TERSE)
22023 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22024 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22025 TYPE_UNQUALIFIED, false, context_die);
22028 add_pure_or_virtual_attribute (subr_die, decl);
22029 if (DECL_ARTIFICIAL (decl))
22030 add_AT_flag (subr_die, DW_AT_artificial, 1);
22032 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22033 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22035 add_alignment_attribute (subr_die, decl);
22037 add_accessibility_attribute (subr_die, decl);
22040 /* Unless we have an existing non-declaration DIE, equate the new
22041 DIE. */
22042 if (!old_die || is_declaration_die (old_die))
22043 equate_decl_number_to_die (decl, subr_die);
22045 if (declaration)
22047 if (!old_die || !get_AT (old_die, DW_AT_inline))
22049 add_AT_flag (subr_die, DW_AT_declaration, 1);
22051 /* If this is an explicit function declaration then generate
22052 a DW_AT_explicit attribute. */
22053 if ((dwarf_version >= 3 || !dwarf_strict)
22054 && lang_hooks.decls.decl_dwarf_attribute (decl,
22055 DW_AT_explicit) == 1)
22056 add_AT_flag (subr_die, DW_AT_explicit, 1);
22058 /* If this is a C++11 deleted special function member then generate
22059 a DW_AT_deleted attribute. */
22060 if ((dwarf_version >= 5 || !dwarf_strict)
22061 && lang_hooks.decls.decl_dwarf_attribute (decl,
22062 DW_AT_deleted) == 1)
22063 add_AT_flag (subr_die, DW_AT_deleted, 1);
22065 /* If this is a C++11 defaulted special function member then
22066 generate a DW_AT_defaulted attribute. */
22067 if (dwarf_version >= 5 || !dwarf_strict)
22069 int defaulted
22070 = lang_hooks.decls.decl_dwarf_attribute (decl,
22071 DW_AT_defaulted);
22072 if (defaulted != -1)
22073 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22076 /* If this is a C++11 non-static member function with & ref-qualifier
22077 then generate a DW_AT_reference attribute. */
22078 if ((dwarf_version >= 5 || !dwarf_strict)
22079 && lang_hooks.decls.decl_dwarf_attribute (decl,
22080 DW_AT_reference) == 1)
22081 add_AT_flag (subr_die, DW_AT_reference, 1);
22083 /* If this is a C++11 non-static member function with &&
22084 ref-qualifier then generate a DW_AT_reference attribute. */
22085 if ((dwarf_version >= 5 || !dwarf_strict)
22086 && lang_hooks.decls.decl_dwarf_attribute (decl,
22087 DW_AT_rvalue_reference)
22088 == 1)
22089 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22092 /* Tag abstract instances with DW_AT_inline. */
22093 else if (DECL_ABSTRACT_P (decl))
22095 if (DECL_DECLARED_INLINE_P (decl))
22097 if (cgraph_function_possibly_inlined_p (decl))
22098 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22099 else
22100 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22102 else
22104 if (cgraph_function_possibly_inlined_p (decl))
22105 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22106 else
22107 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22110 if (DECL_DECLARED_INLINE_P (decl)
22111 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22112 add_AT_flag (subr_die, DW_AT_artificial, 1);
22114 /* For non DECL_EXTERNALs, if range information is available, fill
22115 the DIE with it. */
22116 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22118 HOST_WIDE_INT cfa_fb_offset;
22120 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22122 if (!flag_reorder_blocks_and_partition)
22124 dw_fde_ref fde = fun->fde;
22125 if (fde->dw_fde_begin)
22127 /* We have already generated the labels. */
22128 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22129 fde->dw_fde_end, false);
22131 else
22133 /* Create start/end labels and add the range. */
22134 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22135 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22136 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22137 current_function_funcdef_no);
22138 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22139 current_function_funcdef_no);
22140 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22141 false);
22144 #if VMS_DEBUGGING_INFO
22145 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22146 Section 2.3 Prologue and Epilogue Attributes:
22147 When a breakpoint is set on entry to a function, it is generally
22148 desirable for execution to be suspended, not on the very first
22149 instruction of the function, but rather at a point after the
22150 function's frame has been set up, after any language defined local
22151 declaration processing has been completed, and before execution of
22152 the first statement of the function begins. Debuggers generally
22153 cannot properly determine where this point is. Similarly for a
22154 breakpoint set on exit from a function. The prologue and epilogue
22155 attributes allow a compiler to communicate the location(s) to use. */
22158 if (fde->dw_fde_vms_end_prologue)
22159 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22160 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22162 if (fde->dw_fde_vms_begin_epilogue)
22163 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22164 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22166 #endif
22169 else
22171 /* Generate pubnames entries for the split function code ranges. */
22172 dw_fde_ref fde = fun->fde;
22174 if (fde->dw_fde_second_begin)
22176 if (dwarf_version >= 3 || !dwarf_strict)
22178 /* We should use ranges for non-contiguous code section
22179 addresses. Use the actual code range for the initial
22180 section, since the HOT/COLD labels might precede an
22181 alignment offset. */
22182 bool range_list_added = false;
22183 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22184 fde->dw_fde_end, &range_list_added,
22185 false);
22186 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22187 fde->dw_fde_second_end,
22188 &range_list_added, false);
22189 if (range_list_added)
22190 add_ranges (NULL);
22192 else
22194 /* There is no real support in DW2 for this .. so we make
22195 a work-around. First, emit the pub name for the segment
22196 containing the function label. Then make and emit a
22197 simplified subprogram DIE for the second segment with the
22198 name pre-fixed by __hot/cold_sect_of_. We use the same
22199 linkage name for the second die so that gdb will find both
22200 sections when given "b foo". */
22201 const char *name = NULL;
22202 tree decl_name = DECL_NAME (decl);
22203 dw_die_ref seg_die;
22205 /* Do the 'primary' section. */
22206 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22207 fde->dw_fde_end, false);
22209 /* Build a minimal DIE for the secondary section. */
22210 seg_die = new_die (DW_TAG_subprogram,
22211 subr_die->die_parent, decl);
22213 if (TREE_PUBLIC (decl))
22214 add_AT_flag (seg_die, DW_AT_external, 1);
22216 if (decl_name != NULL
22217 && IDENTIFIER_POINTER (decl_name) != NULL)
22219 name = dwarf2_name (decl, 1);
22220 if (! DECL_ARTIFICIAL (decl))
22221 add_src_coords_attributes (seg_die, decl);
22223 add_linkage_name (seg_die, decl);
22225 gcc_assert (name != NULL);
22226 add_pure_or_virtual_attribute (seg_die, decl);
22227 if (DECL_ARTIFICIAL (decl))
22228 add_AT_flag (seg_die, DW_AT_artificial, 1);
22230 name = concat ("__second_sect_of_", name, NULL);
22231 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22232 fde->dw_fde_second_end, false);
22233 add_name_attribute (seg_die, name);
22234 if (want_pubnames ())
22235 add_pubname_string (name, seg_die);
22238 else
22239 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22240 false);
22243 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22245 /* We define the "frame base" as the function's CFA. This is more
22246 convenient for several reasons: (1) It's stable across the prologue
22247 and epilogue, which makes it better than just a frame pointer,
22248 (2) With dwarf3, there exists a one-byte encoding that allows us
22249 to reference the .debug_frame data by proxy, but failing that,
22250 (3) We can at least reuse the code inspection and interpretation
22251 code that determines the CFA position at various points in the
22252 function. */
22253 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22255 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22256 add_AT_loc (subr_die, DW_AT_frame_base, op);
22258 else
22260 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22261 if (list->dw_loc_next)
22262 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22263 else
22264 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22267 /* Compute a displacement from the "steady-state frame pointer" to
22268 the CFA. The former is what all stack slots and argument slots
22269 will reference in the rtl; the latter is what we've told the
22270 debugger about. We'll need to adjust all frame_base references
22271 by this displacement. */
22272 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22274 if (fun->static_chain_decl)
22276 /* DWARF requires here a location expression that computes the
22277 address of the enclosing subprogram's frame base. The machinery
22278 in tree-nested.c is supposed to store this specific address in the
22279 last field of the FRAME record. */
22280 const tree frame_type
22281 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22282 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22284 tree fb_expr
22285 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22286 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22287 fb_expr, fb_decl, NULL_TREE);
22289 add_AT_location_description (subr_die, DW_AT_static_link,
22290 loc_list_from_tree (fb_expr, 0, NULL));
22294 /* Generate child dies for template paramaters. */
22295 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22296 gen_generic_params_dies (decl);
22298 /* Now output descriptions of the arguments for this function. This gets
22299 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22300 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22301 `...' at the end of the formal parameter list. In order to find out if
22302 there was a trailing ellipsis or not, we must instead look at the type
22303 associated with the FUNCTION_DECL. This will be a node of type
22304 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22305 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22306 an ellipsis at the end. */
22308 /* In the case where we are describing a mere function declaration, all we
22309 need to do here (and all we *can* do here) is to describe the *types* of
22310 its formal parameters. */
22311 if (debug_info_level <= DINFO_LEVEL_TERSE)
22313 else if (declaration)
22314 gen_formal_types_die (decl, subr_die);
22315 else
22317 /* Generate DIEs to represent all known formal parameters. */
22318 tree parm = DECL_ARGUMENTS (decl);
22319 tree generic_decl = early_dwarf
22320 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22321 tree generic_decl_parm = generic_decl
22322 ? DECL_ARGUMENTS (generic_decl)
22323 : NULL;
22324 auto_vec<dw_die_ref> string_types_vec;
22325 if (string_types == NULL)
22326 string_types = &string_types_vec;
22328 /* Now we want to walk the list of parameters of the function and
22329 emit their relevant DIEs.
22331 We consider the case of DECL being an instance of a generic function
22332 as well as it being a normal function.
22334 If DECL is an instance of a generic function we walk the
22335 parameters of the generic function declaration _and_ the parameters of
22336 DECL itself. This is useful because we want to emit specific DIEs for
22337 function parameter packs and those are declared as part of the
22338 generic function declaration. In that particular case,
22339 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22340 That DIE has children DIEs representing the set of arguments
22341 of the pack. Note that the set of pack arguments can be empty.
22342 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22343 children DIE.
22345 Otherwise, we just consider the parameters of DECL. */
22346 while (generic_decl_parm || parm)
22348 if (generic_decl_parm
22349 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22350 gen_formal_parameter_pack_die (generic_decl_parm,
22351 parm, subr_die,
22352 &parm);
22353 else if (parm && !POINTER_BOUNDS_P (parm))
22355 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22357 if (parm == DECL_ARGUMENTS (decl)
22358 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22359 && parm_die
22360 && (dwarf_version >= 3 || !dwarf_strict))
22361 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22363 parm = DECL_CHAIN (parm);
22365 else if (parm)
22366 parm = DECL_CHAIN (parm);
22368 if (generic_decl_parm)
22369 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22372 /* Decide whether we need an unspecified_parameters DIE at the end.
22373 There are 2 more cases to do this for: 1) the ansi ... declaration -
22374 this is detectable when the end of the arg list is not a
22375 void_type_node 2) an unprototyped function declaration (not a
22376 definition). This just means that we have no info about the
22377 parameters at all. */
22378 if (early_dwarf)
22380 if (prototype_p (TREE_TYPE (decl)))
22382 /* This is the prototyped case, check for.... */
22383 if (stdarg_p (TREE_TYPE (decl)))
22384 gen_unspecified_parameters_die (decl, subr_die);
22386 else if (DECL_INITIAL (decl) == NULL_TREE)
22387 gen_unspecified_parameters_die (decl, subr_die);
22390 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
22391 have DIEs. */
22392 if (string_types == &string_types_vec)
22394 adjust_string_types ();
22395 string_types = NULL;
22399 if (subr_die != old_die)
22400 /* Add the calling convention attribute if requested. */
22401 add_calling_convention_attribute (subr_die, decl);
22403 /* Output Dwarf info for all of the stuff within the body of the function
22404 (if it has one - it may be just a declaration).
22406 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22407 a function. This BLOCK actually represents the outermost binding contour
22408 for the function, i.e. the contour in which the function's formal
22409 parameters and labels get declared. Curiously, it appears that the front
22410 end doesn't actually put the PARM_DECL nodes for the current function onto
22411 the BLOCK_VARS list for this outer scope, but are strung off of the
22412 DECL_ARGUMENTS list for the function instead.
22414 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22415 the LABEL_DECL nodes for the function however, and we output DWARF info
22416 for those in decls_for_scope. Just within the `outer_scope' there will be
22417 a BLOCK node representing the function's outermost pair of curly braces,
22418 and any blocks used for the base and member initializers of a C++
22419 constructor function. */
22420 tree outer_scope = DECL_INITIAL (decl);
22421 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22423 int call_site_note_count = 0;
22424 int tail_call_site_note_count = 0;
22426 /* Emit a DW_TAG_variable DIE for a named return value. */
22427 if (DECL_NAME (DECL_RESULT (decl)))
22428 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22430 /* The first time through decls_for_scope we will generate the
22431 DIEs for the locals. The second time, we fill in the
22432 location info. */
22433 decls_for_scope (outer_scope, subr_die);
22435 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22437 struct call_arg_loc_node *ca_loc;
22438 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22440 dw_die_ref die = NULL;
22441 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22442 rtx arg, next_arg;
22444 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22445 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22446 : NULL_RTX);
22447 arg; arg = next_arg)
22449 dw_loc_descr_ref reg, val;
22450 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22451 dw_die_ref cdie, tdie = NULL;
22453 next_arg = XEXP (arg, 1);
22454 if (REG_P (XEXP (XEXP (arg, 0), 0))
22455 && next_arg
22456 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22457 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22458 && REGNO (XEXP (XEXP (arg, 0), 0))
22459 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22460 next_arg = XEXP (next_arg, 1);
22461 if (mode == VOIDmode)
22463 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22464 if (mode == VOIDmode)
22465 mode = GET_MODE (XEXP (arg, 0));
22467 if (mode == VOIDmode || mode == BLKmode)
22468 continue;
22469 /* Get dynamic information about call target only if we
22470 have no static information: we cannot generate both
22471 DW_AT_call_origin and DW_AT_call_target
22472 attributes. */
22473 if (ca_loc->symbol_ref == NULL_RTX)
22475 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22477 tloc = XEXP (XEXP (arg, 0), 1);
22478 continue;
22480 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22481 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22483 tlocc = XEXP (XEXP (arg, 0), 1);
22484 continue;
22487 reg = NULL;
22488 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22489 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22490 VAR_INIT_STATUS_INITIALIZED);
22491 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22493 rtx mem = XEXP (XEXP (arg, 0), 0);
22494 reg = mem_loc_descriptor (XEXP (mem, 0),
22495 get_address_mode (mem),
22496 GET_MODE (mem),
22497 VAR_INIT_STATUS_INITIALIZED);
22499 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22500 == DEBUG_PARAMETER_REF)
22502 tree tdecl
22503 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22504 tdie = lookup_decl_die (tdecl);
22505 if (tdie == NULL)
22506 continue;
22508 else
22509 continue;
22510 if (reg == NULL
22511 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22512 != DEBUG_PARAMETER_REF)
22513 continue;
22514 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22515 VOIDmode,
22516 VAR_INIT_STATUS_INITIALIZED);
22517 if (val == NULL)
22518 continue;
22519 if (die == NULL)
22520 die = gen_call_site_die (decl, subr_die, ca_loc);
22521 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22522 NULL_TREE);
22523 if (reg != NULL)
22524 add_AT_loc (cdie, DW_AT_location, reg);
22525 else if (tdie != NULL)
22526 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22527 tdie);
22528 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22529 if (next_arg != XEXP (arg, 1))
22531 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22532 if (mode == VOIDmode)
22533 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22534 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22535 0), 1),
22536 mode, VOIDmode,
22537 VAR_INIT_STATUS_INITIALIZED);
22538 if (val != NULL)
22539 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22540 val);
22543 if (die == NULL
22544 && (ca_loc->symbol_ref || tloc))
22545 die = gen_call_site_die (decl, subr_die, ca_loc);
22546 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22548 dw_loc_descr_ref tval = NULL;
22550 if (tloc != NULL_RTX)
22551 tval = mem_loc_descriptor (tloc,
22552 GET_MODE (tloc) == VOIDmode
22553 ? Pmode : GET_MODE (tloc),
22554 VOIDmode,
22555 VAR_INIT_STATUS_INITIALIZED);
22556 if (tval)
22557 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22558 else if (tlocc != NULL_RTX)
22560 tval = mem_loc_descriptor (tlocc,
22561 GET_MODE (tlocc) == VOIDmode
22562 ? Pmode : GET_MODE (tlocc),
22563 VOIDmode,
22564 VAR_INIT_STATUS_INITIALIZED);
22565 if (tval)
22566 add_AT_loc (die,
22567 dwarf_AT (DW_AT_call_target_clobbered),
22568 tval);
22571 if (die != NULL)
22573 call_site_note_count++;
22574 if (ca_loc->tail_call_p)
22575 tail_call_site_note_count++;
22579 call_arg_locations = NULL;
22580 call_arg_loc_last = NULL;
22581 if (tail_call_site_count >= 0
22582 && tail_call_site_count == tail_call_site_note_count
22583 && (!dwarf_strict || dwarf_version >= 5))
22585 if (call_site_count >= 0
22586 && call_site_count == call_site_note_count)
22587 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22588 else
22589 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22591 call_site_count = -1;
22592 tail_call_site_count = -1;
22595 /* Mark used types after we have created DIEs for the functions scopes. */
22596 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22599 /* Returns a hash value for X (which really is a die_struct). */
22601 hashval_t
22602 block_die_hasher::hash (die_struct *d)
22604 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22607 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22608 as decl_id and die_parent of die_struct Y. */
22610 bool
22611 block_die_hasher::equal (die_struct *x, die_struct *y)
22613 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22616 /* Return TRUE if DECL, which may have been previously generated as
22617 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22618 true if decl (or its origin) is either an extern declaration or a
22619 class/namespace scoped declaration.
22621 The declare_in_namespace support causes us to get two DIEs for one
22622 variable, both of which are declarations. We want to avoid
22623 considering one to be a specification, so we must test for
22624 DECLARATION and DW_AT_declaration. */
22625 static inline bool
22626 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22628 return (old_die && TREE_STATIC (decl) && !declaration
22629 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22632 /* Return true if DECL is a local static. */
22634 static inline bool
22635 local_function_static (tree decl)
22637 gcc_assert (VAR_P (decl));
22638 return TREE_STATIC (decl)
22639 && DECL_CONTEXT (decl)
22640 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22643 /* Generate a DIE to represent a declared data object.
22644 Either DECL or ORIGIN must be non-null. */
22646 static void
22647 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22649 HOST_WIDE_INT off = 0;
22650 tree com_decl;
22651 tree decl_or_origin = decl ? decl : origin;
22652 tree ultimate_origin;
22653 dw_die_ref var_die;
22654 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22655 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22656 || class_or_namespace_scope_p (context_die));
22657 bool specialization_p = false;
22658 bool no_linkage_name = false;
22660 /* While C++ inline static data members have definitions inside of the
22661 class, force the first DIE to be a declaration, then let gen_member_die
22662 reparent it to the class context and call gen_variable_die again
22663 to create the outside of the class DIE for the definition. */
22664 if (!declaration
22665 && old_die == NULL
22666 && decl
22667 && DECL_CONTEXT (decl)
22668 && TYPE_P (DECL_CONTEXT (decl))
22669 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22671 declaration = true;
22672 no_linkage_name = true;
22675 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22676 if (decl || ultimate_origin)
22677 origin = ultimate_origin;
22678 com_decl = fortran_common (decl_or_origin, &off);
22680 /* Symbol in common gets emitted as a child of the common block, in the form
22681 of a data member. */
22682 if (com_decl)
22684 dw_die_ref com_die;
22685 dw_loc_list_ref loc = NULL;
22686 die_node com_die_arg;
22688 var_die = lookup_decl_die (decl_or_origin);
22689 if (var_die)
22691 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22693 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22694 if (loc)
22696 if (off)
22698 /* Optimize the common case. */
22699 if (single_element_loc_list_p (loc)
22700 && loc->expr->dw_loc_opc == DW_OP_addr
22701 && loc->expr->dw_loc_next == NULL
22702 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22703 == SYMBOL_REF)
22705 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22706 loc->expr->dw_loc_oprnd1.v.val_addr
22707 = plus_constant (GET_MODE (x), x , off);
22709 else
22710 loc_list_plus_const (loc, off);
22712 add_AT_location_description (var_die, DW_AT_location, loc);
22713 remove_AT (var_die, DW_AT_declaration);
22716 return;
22719 if (common_block_die_table == NULL)
22720 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22722 com_die_arg.decl_id = DECL_UID (com_decl);
22723 com_die_arg.die_parent = context_die;
22724 com_die = common_block_die_table->find (&com_die_arg);
22725 if (! early_dwarf)
22726 loc = loc_list_from_tree (com_decl, 2, NULL);
22727 if (com_die == NULL)
22729 const char *cnam
22730 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22731 die_node **slot;
22733 com_die = new_die (DW_TAG_common_block, context_die, decl);
22734 add_name_and_src_coords_attributes (com_die, com_decl);
22735 if (loc)
22737 add_AT_location_description (com_die, DW_AT_location, loc);
22738 /* Avoid sharing the same loc descriptor between
22739 DW_TAG_common_block and DW_TAG_variable. */
22740 loc = loc_list_from_tree (com_decl, 2, NULL);
22742 else if (DECL_EXTERNAL (decl_or_origin))
22743 add_AT_flag (com_die, DW_AT_declaration, 1);
22744 if (want_pubnames ())
22745 add_pubname_string (cnam, com_die); /* ??? needed? */
22746 com_die->decl_id = DECL_UID (com_decl);
22747 slot = common_block_die_table->find_slot (com_die, INSERT);
22748 *slot = com_die;
22750 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22752 add_AT_location_description (com_die, DW_AT_location, loc);
22753 loc = loc_list_from_tree (com_decl, 2, NULL);
22754 remove_AT (com_die, DW_AT_declaration);
22756 var_die = new_die (DW_TAG_variable, com_die, decl);
22757 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22758 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22759 decl_quals (decl_or_origin), false,
22760 context_die);
22761 add_alignment_attribute (var_die, decl);
22762 add_AT_flag (var_die, DW_AT_external, 1);
22763 if (loc)
22765 if (off)
22767 /* Optimize the common case. */
22768 if (single_element_loc_list_p (loc)
22769 && loc->expr->dw_loc_opc == DW_OP_addr
22770 && loc->expr->dw_loc_next == NULL
22771 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22773 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22774 loc->expr->dw_loc_oprnd1.v.val_addr
22775 = plus_constant (GET_MODE (x), x, off);
22777 else
22778 loc_list_plus_const (loc, off);
22780 add_AT_location_description (var_die, DW_AT_location, loc);
22782 else if (DECL_EXTERNAL (decl_or_origin))
22783 add_AT_flag (var_die, DW_AT_declaration, 1);
22784 if (decl)
22785 equate_decl_number_to_die (decl, var_die);
22786 return;
22789 if (old_die)
22791 if (declaration)
22793 /* A declaration that has been previously dumped, needs no
22794 further annotations, since it doesn't need location on
22795 the second pass. */
22796 return;
22798 else if (decl_will_get_specification_p (old_die, decl, declaration)
22799 && !get_AT (old_die, DW_AT_specification))
22801 /* Fall-thru so we can make a new variable die along with a
22802 DW_AT_specification. */
22804 else if (origin && old_die->die_parent != context_die)
22806 /* If we will be creating an inlined instance, we need a
22807 new DIE that will get annotated with
22808 DW_AT_abstract_origin. Clear things so we can get a
22809 new DIE. */
22810 gcc_assert (!DECL_ABSTRACT_P (decl));
22811 old_die = NULL;
22813 else
22815 /* If a DIE was dumped early, it still needs location info.
22816 Skip to where we fill the location bits. */
22817 var_die = old_die;
22818 goto gen_variable_die_location;
22822 /* For static data members, the declaration in the class is supposed
22823 to have DW_TAG_member tag; the specification should still be
22824 DW_TAG_variable referencing the DW_TAG_member DIE. */
22825 if (declaration && class_scope_p (context_die))
22826 var_die = new_die (DW_TAG_member, context_die, decl);
22827 else
22828 var_die = new_die (DW_TAG_variable, context_die, decl);
22830 if (origin != NULL)
22831 add_abstract_origin_attribute (var_die, origin);
22833 /* Loop unrolling can create multiple blocks that refer to the same
22834 static variable, so we must test for the DW_AT_declaration flag.
22836 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22837 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22838 sharing them.
22840 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22841 else if (decl_will_get_specification_p (old_die, decl, declaration))
22843 /* This is a definition of a C++ class level static. */
22844 add_AT_specification (var_die, old_die);
22845 specialization_p = true;
22846 if (DECL_NAME (decl))
22848 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22849 struct dwarf_file_data * file_index = lookup_filename (s.file);
22851 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22852 add_AT_file (var_die, DW_AT_decl_file, file_index);
22854 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22855 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22857 if (debug_column_info
22858 && s.column
22859 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22860 != (unsigned) s.column))
22861 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22863 if (old_die->die_tag == DW_TAG_member)
22864 add_linkage_name (var_die, decl);
22867 else
22868 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22870 if ((origin == NULL && !specialization_p)
22871 || (origin != NULL
22872 && !DECL_ABSTRACT_P (decl_or_origin)
22873 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22874 decl_function_context
22875 (decl_or_origin))))
22877 tree type = TREE_TYPE (decl_or_origin);
22879 if (decl_by_reference_p (decl_or_origin))
22880 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22881 context_die);
22882 else
22883 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22884 context_die);
22887 if (origin == NULL && !specialization_p)
22889 if (TREE_PUBLIC (decl))
22890 add_AT_flag (var_die, DW_AT_external, 1);
22892 if (DECL_ARTIFICIAL (decl))
22893 add_AT_flag (var_die, DW_AT_artificial, 1);
22895 add_alignment_attribute (var_die, decl);
22897 add_accessibility_attribute (var_die, decl);
22900 if (declaration)
22901 add_AT_flag (var_die, DW_AT_declaration, 1);
22903 if (decl && (DECL_ABSTRACT_P (decl)
22904 || !old_die || is_declaration_die (old_die)))
22905 equate_decl_number_to_die (decl, var_die);
22907 gen_variable_die_location:
22908 if (! declaration
22909 && (! DECL_ABSTRACT_P (decl_or_origin)
22910 /* Local static vars are shared between all clones/inlines,
22911 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22912 already set. */
22913 || (VAR_P (decl_or_origin)
22914 && TREE_STATIC (decl_or_origin)
22915 && DECL_RTL_SET_P (decl_or_origin))))
22917 if (early_dwarf)
22918 add_pubname (decl_or_origin, var_die);
22919 else
22920 add_location_or_const_value_attribute (var_die, decl_or_origin,
22921 decl == NULL);
22923 else
22924 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22926 if ((dwarf_version >= 4 || !dwarf_strict)
22927 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22928 DW_AT_const_expr) == 1
22929 && !get_AT (var_die, DW_AT_const_expr)
22930 && !specialization_p)
22931 add_AT_flag (var_die, DW_AT_const_expr, 1);
22933 if (!dwarf_strict)
22935 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22936 DW_AT_inline);
22937 if (inl != -1
22938 && !get_AT (var_die, DW_AT_inline)
22939 && !specialization_p)
22940 add_AT_unsigned (var_die, DW_AT_inline, inl);
22944 /* Generate a DIE to represent a named constant. */
22946 static void
22947 gen_const_die (tree decl, dw_die_ref context_die)
22949 dw_die_ref const_die;
22950 tree type = TREE_TYPE (decl);
22952 const_die = lookup_decl_die (decl);
22953 if (const_die)
22954 return;
22956 const_die = new_die (DW_TAG_constant, context_die, decl);
22957 equate_decl_number_to_die (decl, const_die);
22958 add_name_and_src_coords_attributes (const_die, decl);
22959 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22960 if (TREE_PUBLIC (decl))
22961 add_AT_flag (const_die, DW_AT_external, 1);
22962 if (DECL_ARTIFICIAL (decl))
22963 add_AT_flag (const_die, DW_AT_artificial, 1);
22964 tree_add_const_value_attribute_for_decl (const_die, decl);
22967 /* Generate a DIE to represent a label identifier. */
22969 static void
22970 gen_label_die (tree decl, dw_die_ref context_die)
22972 tree origin = decl_ultimate_origin (decl);
22973 dw_die_ref lbl_die = lookup_decl_die (decl);
22974 rtx insn;
22975 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22977 if (!lbl_die)
22979 lbl_die = new_die (DW_TAG_label, context_die, decl);
22980 equate_decl_number_to_die (decl, lbl_die);
22982 if (origin != NULL)
22983 add_abstract_origin_attribute (lbl_die, origin);
22984 else
22985 add_name_and_src_coords_attributes (lbl_die, decl);
22988 if (DECL_ABSTRACT_P (decl))
22989 equate_decl_number_to_die (decl, lbl_die);
22990 else if (! early_dwarf)
22992 insn = DECL_RTL_IF_SET (decl);
22994 /* Deleted labels are programmer specified labels which have been
22995 eliminated because of various optimizations. We still emit them
22996 here so that it is possible to put breakpoints on them. */
22997 if (insn
22998 && (LABEL_P (insn)
22999 || ((NOTE_P (insn)
23000 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23002 /* When optimization is enabled (via -O) some parts of the compiler
23003 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23004 represent source-level labels which were explicitly declared by
23005 the user. This really shouldn't be happening though, so catch
23006 it if it ever does happen. */
23007 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23009 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23010 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23012 else if (insn
23013 && NOTE_P (insn)
23014 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23015 && CODE_LABEL_NUMBER (insn) != -1)
23017 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23018 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23023 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23024 attributes to the DIE for a block STMT, to describe where the inlined
23025 function was called from. This is similar to add_src_coords_attributes. */
23027 static inline void
23028 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23030 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23032 if (dwarf_version >= 3 || !dwarf_strict)
23034 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23035 add_AT_unsigned (die, DW_AT_call_line, s.line);
23036 if (debug_column_info && s.column)
23037 add_AT_unsigned (die, DW_AT_call_column, s.column);
23042 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23043 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23045 static inline void
23046 add_high_low_attributes (tree stmt, dw_die_ref die)
23048 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23050 if (BLOCK_FRAGMENT_CHAIN (stmt)
23051 && (dwarf_version >= 3 || !dwarf_strict))
23053 tree chain, superblock = NULL_TREE;
23054 dw_die_ref pdie;
23055 dw_attr_node *attr = NULL;
23057 if (inlined_function_outer_scope_p (stmt))
23059 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23060 BLOCK_NUMBER (stmt));
23061 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23064 /* Optimize duplicate .debug_ranges lists or even tails of
23065 lists. If this BLOCK has same ranges as its supercontext,
23066 lookup DW_AT_ranges attribute in the supercontext (and
23067 recursively so), verify that the ranges_table contains the
23068 right values and use it instead of adding a new .debug_range. */
23069 for (chain = stmt, pdie = die;
23070 BLOCK_SAME_RANGE (chain);
23071 chain = BLOCK_SUPERCONTEXT (chain))
23073 dw_attr_node *new_attr;
23075 pdie = pdie->die_parent;
23076 if (pdie == NULL)
23077 break;
23078 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23079 break;
23080 new_attr = get_AT (pdie, DW_AT_ranges);
23081 if (new_attr == NULL
23082 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23083 break;
23084 attr = new_attr;
23085 superblock = BLOCK_SUPERCONTEXT (chain);
23087 if (attr != NULL
23088 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23089 == BLOCK_NUMBER (superblock))
23090 && BLOCK_FRAGMENT_CHAIN (superblock))
23092 unsigned long off = attr->dw_attr_val.v.val_offset;
23093 unsigned long supercnt = 0, thiscnt = 0;
23094 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23095 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23097 ++supercnt;
23098 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23099 == BLOCK_NUMBER (chain));
23101 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23102 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23103 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23104 ++thiscnt;
23105 gcc_assert (supercnt >= thiscnt);
23106 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23107 false);
23108 note_rnglist_head (off + supercnt - thiscnt);
23109 return;
23112 unsigned int offset = add_ranges (stmt, true);
23113 add_AT_range_list (die, DW_AT_ranges, offset, false);
23114 note_rnglist_head (offset);
23116 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23117 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23120 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23121 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23122 chain = BLOCK_FRAGMENT_CHAIN (chain);
23124 while (chain);
23125 add_ranges (NULL);
23127 else
23129 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23130 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23131 BLOCK_NUMBER (stmt));
23132 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23133 BLOCK_NUMBER (stmt));
23134 add_AT_low_high_pc (die, label, label_high, false);
23138 /* Generate a DIE for a lexical block. */
23140 static void
23141 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23143 dw_die_ref old_die = BLOCK_DIE (stmt);
23144 dw_die_ref stmt_die = NULL;
23145 if (!old_die)
23147 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23148 BLOCK_DIE (stmt) = stmt_die;
23151 if (BLOCK_ABSTRACT (stmt))
23153 if (old_die)
23155 /* This must have been generated early and it won't even
23156 need location information since it's a DW_AT_inline
23157 function. */
23158 if (flag_checking)
23159 for (dw_die_ref c = context_die; c; c = c->die_parent)
23160 if (c->die_tag == DW_TAG_inlined_subroutine
23161 || c->die_tag == DW_TAG_subprogram)
23163 gcc_assert (get_AT (c, DW_AT_inline));
23164 break;
23166 return;
23169 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23171 /* If this is an inlined instance, create a new lexical die for
23172 anything below to attach DW_AT_abstract_origin to. */
23173 if (old_die)
23175 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23176 BLOCK_DIE (stmt) = stmt_die;
23177 old_die = NULL;
23180 tree origin = block_ultimate_origin (stmt);
23181 if (origin != NULL_TREE && origin != stmt)
23182 add_abstract_origin_attribute (stmt_die, origin);
23185 if (old_die)
23186 stmt_die = old_die;
23188 /* A non abstract block whose blocks have already been reordered
23189 should have the instruction range for this block. If so, set the
23190 high/low attributes. */
23191 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23193 gcc_assert (stmt_die);
23194 add_high_low_attributes (stmt, stmt_die);
23197 decls_for_scope (stmt, stmt_die);
23200 /* Generate a DIE for an inlined subprogram. */
23202 static void
23203 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23205 tree decl;
23207 /* The instance of function that is effectively being inlined shall not
23208 be abstract. */
23209 gcc_assert (! BLOCK_ABSTRACT (stmt));
23211 decl = block_ultimate_origin (stmt);
23213 /* Make sure any inlined functions are known to be inlineable. */
23214 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23215 || cgraph_function_possibly_inlined_p (decl));
23217 /* Emit info for the abstract instance first, if we haven't yet. We
23218 must emit this even if the block is abstract, otherwise when we
23219 emit the block below (or elsewhere), we may end up trying to emit
23220 a die whose origin die hasn't been emitted, and crashing. */
23221 dwarf2out_abstract_function (decl);
23223 if (! BLOCK_ABSTRACT (stmt))
23225 dw_die_ref subr_die
23226 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23228 if (call_arg_locations)
23229 BLOCK_DIE (stmt) = subr_die;
23230 add_abstract_origin_attribute (subr_die, decl);
23231 if (TREE_ASM_WRITTEN (stmt))
23232 add_high_low_attributes (stmt, subr_die);
23233 add_call_src_coords_attributes (stmt, subr_die);
23235 decls_for_scope (stmt, subr_die);
23239 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23240 the comment for VLR_CONTEXT. */
23242 static void
23243 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23245 dw_die_ref decl_die;
23247 if (TREE_TYPE (decl) == error_mark_node)
23248 return;
23250 decl_die = new_die (DW_TAG_member, context_die, decl);
23251 add_name_and_src_coords_attributes (decl_die, decl);
23252 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23253 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23254 context_die);
23256 if (DECL_BIT_FIELD_TYPE (decl))
23258 add_byte_size_attribute (decl_die, decl);
23259 add_bit_size_attribute (decl_die, decl);
23260 add_bit_offset_attribute (decl_die, decl, ctx);
23263 add_alignment_attribute (decl_die, decl);
23265 /* If we have a variant part offset, then we are supposed to process a member
23266 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23267 trees. */
23268 gcc_assert (ctx->variant_part_offset == NULL_TREE
23269 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23270 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23271 add_data_member_location_attribute (decl_die, decl, ctx);
23273 if (DECL_ARTIFICIAL (decl))
23274 add_AT_flag (decl_die, DW_AT_artificial, 1);
23276 add_accessibility_attribute (decl_die, decl);
23278 /* Equate decl number to die, so that we can look up this decl later on. */
23279 equate_decl_number_to_die (decl, decl_die);
23282 #if 0
23283 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23284 Use modified_type_die instead.
23285 We keep this code here just in case these types of DIEs may be needed to
23286 represent certain things in other languages (e.g. Pascal) someday. */
23288 static void
23289 gen_pointer_type_die (tree type, dw_die_ref context_die)
23291 dw_die_ref ptr_die
23292 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23294 equate_type_number_to_die (type, ptr_die);
23295 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23296 context_die);
23297 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23300 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23301 Use modified_type_die instead.
23302 We keep this code here just in case these types of DIEs may be needed to
23303 represent certain things in other languages (e.g. Pascal) someday. */
23305 static void
23306 gen_reference_type_die (tree type, dw_die_ref context_die)
23308 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23310 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23311 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23312 else
23313 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23315 equate_type_number_to_die (type, ref_die);
23316 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23317 context_die);
23318 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23320 #endif
23322 /* Generate a DIE for a pointer to a member type. TYPE can be an
23323 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23324 pointer to member function. */
23326 static void
23327 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23329 if (lookup_type_die (type))
23330 return;
23332 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23333 scope_die_for (type, context_die), type);
23335 equate_type_number_to_die (type, ptr_die);
23336 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23337 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23338 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23339 context_die);
23340 add_alignment_attribute (ptr_die, type);
23342 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23343 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23345 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23346 add_AT_loc (ptr_die, DW_AT_use_location, op);
23350 static char *producer_string;
23352 /* Return a heap allocated producer string including command line options
23353 if -grecord-gcc-switches. */
23355 static char *
23356 gen_producer_string (void)
23358 size_t j;
23359 auto_vec<const char *> switches;
23360 const char *language_string = lang_hooks.name;
23361 char *producer, *tail;
23362 const char *p;
23363 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23364 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23366 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23367 switch (save_decoded_options[j].opt_index)
23369 case OPT_o:
23370 case OPT_d:
23371 case OPT_dumpbase:
23372 case OPT_dumpdir:
23373 case OPT_auxbase:
23374 case OPT_auxbase_strip:
23375 case OPT_quiet:
23376 case OPT_version:
23377 case OPT_v:
23378 case OPT_w:
23379 case OPT_L:
23380 case OPT_D:
23381 case OPT_I:
23382 case OPT_U:
23383 case OPT_SPECIAL_unknown:
23384 case OPT_SPECIAL_ignore:
23385 case OPT_SPECIAL_program_name:
23386 case OPT_SPECIAL_input_file:
23387 case OPT_grecord_gcc_switches:
23388 case OPT_gno_record_gcc_switches:
23389 case OPT__output_pch_:
23390 case OPT_fdiagnostics_show_location_:
23391 case OPT_fdiagnostics_show_option:
23392 case OPT_fdiagnostics_show_caret:
23393 case OPT_fdiagnostics_color_:
23394 case OPT_fverbose_asm:
23395 case OPT____:
23396 case OPT__sysroot_:
23397 case OPT_nostdinc:
23398 case OPT_nostdinc__:
23399 case OPT_fpreprocessed:
23400 case OPT_fltrans_output_list_:
23401 case OPT_fresolution_:
23402 case OPT_fdebug_prefix_map_:
23403 /* Ignore these. */
23404 continue;
23405 default:
23406 if (cl_options[save_decoded_options[j].opt_index].flags
23407 & CL_NO_DWARF_RECORD)
23408 continue;
23409 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23410 == '-');
23411 switch (save_decoded_options[j].canonical_option[0][1])
23413 case 'M':
23414 case 'i':
23415 case 'W':
23416 continue;
23417 case 'f':
23418 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23419 "dump", 4) == 0)
23420 continue;
23421 break;
23422 default:
23423 break;
23425 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23426 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23427 break;
23430 producer = XNEWVEC (char, plen + 1 + len + 1);
23431 tail = producer;
23432 sprintf (tail, "%s %s", language_string, version_string);
23433 tail += plen;
23435 FOR_EACH_VEC_ELT (switches, j, p)
23437 len = strlen (p);
23438 *tail = ' ';
23439 memcpy (tail + 1, p, len);
23440 tail += len + 1;
23443 *tail = '\0';
23444 return producer;
23447 /* Given a C and/or C++ language/version string return the "highest".
23448 C++ is assumed to be "higher" than C in this case. Used for merging
23449 LTO translation unit languages. */
23450 static const char *
23451 highest_c_language (const char *lang1, const char *lang2)
23453 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23454 return "GNU C++14";
23455 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23456 return "GNU C++11";
23457 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23458 return "GNU C++98";
23460 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23461 return "GNU C11";
23462 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23463 return "GNU C99";
23464 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23465 return "GNU C89";
23467 gcc_unreachable ();
23471 /* Generate the DIE for the compilation unit. */
23473 static dw_die_ref
23474 gen_compile_unit_die (const char *filename)
23476 dw_die_ref die;
23477 const char *language_string = lang_hooks.name;
23478 int language;
23480 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23482 if (filename)
23484 add_name_attribute (die, filename);
23485 /* Don't add cwd for <built-in>. */
23486 if (filename[0] != '<')
23487 add_comp_dir_attribute (die);
23490 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23492 /* If our producer is LTO try to figure out a common language to use
23493 from the global list of translation units. */
23494 if (strcmp (language_string, "GNU GIMPLE") == 0)
23496 unsigned i;
23497 tree t;
23498 const char *common_lang = NULL;
23500 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23502 if (!TRANSLATION_UNIT_LANGUAGE (t))
23503 continue;
23504 if (!common_lang)
23505 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23506 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23508 else if (strncmp (common_lang, "GNU C", 5) == 0
23509 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23510 /* Mixing C and C++ is ok, use C++ in that case. */
23511 common_lang = highest_c_language (common_lang,
23512 TRANSLATION_UNIT_LANGUAGE (t));
23513 else
23515 /* Fall back to C. */
23516 common_lang = NULL;
23517 break;
23521 if (common_lang)
23522 language_string = common_lang;
23525 language = DW_LANG_C;
23526 if (strncmp (language_string, "GNU C", 5) == 0
23527 && ISDIGIT (language_string[5]))
23529 language = DW_LANG_C89;
23530 if (dwarf_version >= 3 || !dwarf_strict)
23532 if (strcmp (language_string, "GNU C89") != 0)
23533 language = DW_LANG_C99;
23535 if (dwarf_version >= 5 /* || !dwarf_strict */)
23536 if (strcmp (language_string, "GNU C11") == 0)
23537 language = DW_LANG_C11;
23540 else if (strncmp (language_string, "GNU C++", 7) == 0)
23542 language = DW_LANG_C_plus_plus;
23543 if (dwarf_version >= 5 /* || !dwarf_strict */)
23545 if (strcmp (language_string, "GNU C++11") == 0)
23546 language = DW_LANG_C_plus_plus_11;
23547 else if (strcmp (language_string, "GNU C++14") == 0)
23548 language = DW_LANG_C_plus_plus_14;
23551 else if (strcmp (language_string, "GNU F77") == 0)
23552 language = DW_LANG_Fortran77;
23553 else if (strcmp (language_string, "GNU Pascal") == 0)
23554 language = DW_LANG_Pascal83;
23555 else if (dwarf_version >= 3 || !dwarf_strict)
23557 if (strcmp (language_string, "GNU Ada") == 0)
23558 language = DW_LANG_Ada95;
23559 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23561 language = DW_LANG_Fortran95;
23562 if (dwarf_version >= 5 /* || !dwarf_strict */)
23564 if (strcmp (language_string, "GNU Fortran2003") == 0)
23565 language = DW_LANG_Fortran03;
23566 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23567 language = DW_LANG_Fortran08;
23570 else if (strcmp (language_string, "GNU Java") == 0)
23571 language = DW_LANG_Java;
23572 else if (strcmp (language_string, "GNU Objective-C") == 0)
23573 language = DW_LANG_ObjC;
23574 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23575 language = DW_LANG_ObjC_plus_plus;
23576 else if (dwarf_version >= 5 || !dwarf_strict)
23578 if (strcmp (language_string, "GNU Go") == 0)
23579 language = DW_LANG_Go;
23582 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23583 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23584 language = DW_LANG_Fortran90;
23586 add_AT_unsigned (die, DW_AT_language, language);
23588 switch (language)
23590 case DW_LANG_Fortran77:
23591 case DW_LANG_Fortran90:
23592 case DW_LANG_Fortran95:
23593 case DW_LANG_Fortran03:
23594 case DW_LANG_Fortran08:
23595 /* Fortran has case insensitive identifiers and the front-end
23596 lowercases everything. */
23597 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23598 break;
23599 default:
23600 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23601 break;
23603 return die;
23606 /* Generate the DIE for a base class. */
23608 static void
23609 gen_inheritance_die (tree binfo, tree access, tree type,
23610 dw_die_ref context_die)
23612 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23613 struct vlr_context ctx = { type, NULL };
23615 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23616 context_die);
23617 add_data_member_location_attribute (die, binfo, &ctx);
23619 if (BINFO_VIRTUAL_P (binfo))
23620 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23622 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23623 children, otherwise the default is DW_ACCESS_public. In DWARF2
23624 the default has always been DW_ACCESS_private. */
23625 if (access == access_public_node)
23627 if (dwarf_version == 2
23628 || context_die->die_tag == DW_TAG_class_type)
23629 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23631 else if (access == access_protected_node)
23632 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23633 else if (dwarf_version > 2
23634 && context_die->die_tag != DW_TAG_class_type)
23635 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23638 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23639 structure. */
23640 static bool
23641 is_variant_part (tree decl)
23643 return (TREE_CODE (decl) == FIELD_DECL
23644 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23647 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23648 return the FIELD_DECL. Return NULL_TREE otherwise. */
23650 static tree
23651 analyze_discr_in_predicate (tree operand, tree struct_type)
23653 bool continue_stripping = true;
23654 while (continue_stripping)
23655 switch (TREE_CODE (operand))
23657 CASE_CONVERT:
23658 operand = TREE_OPERAND (operand, 0);
23659 break;
23660 default:
23661 continue_stripping = false;
23662 break;
23665 /* Match field access to members of struct_type only. */
23666 if (TREE_CODE (operand) == COMPONENT_REF
23667 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23668 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23669 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23670 return TREE_OPERAND (operand, 1);
23671 else
23672 return NULL_TREE;
23675 /* Check that SRC is a constant integer that can be represented as a native
23676 integer constant (either signed or unsigned). If so, store it into DEST and
23677 return true. Return false otherwise. */
23679 static bool
23680 get_discr_value (tree src, dw_discr_value *dest)
23682 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23684 if (TREE_CODE (src) != INTEGER_CST
23685 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23686 return false;
23688 dest->pos = is_unsigned;
23689 if (is_unsigned)
23690 dest->v.uval = tree_to_uhwi (src);
23691 else
23692 dest->v.sval = tree_to_shwi (src);
23694 return true;
23697 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23698 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23699 store NULL_TREE in DISCR_DECL. Otherwise:
23701 - store the discriminant field in STRUCT_TYPE that controls the variant
23702 part to *DISCR_DECL
23704 - put in *DISCR_LISTS_P an array where for each variant, the item
23705 represents the corresponding matching list of discriminant values.
23707 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23708 the above array.
23710 Note that when the array is allocated (i.e. when the analysis is
23711 successful), it is up to the caller to free the array. */
23713 static void
23714 analyze_variants_discr (tree variant_part_decl,
23715 tree struct_type,
23716 tree *discr_decl,
23717 dw_discr_list_ref **discr_lists_p,
23718 unsigned *discr_lists_length)
23720 tree variant_part_type = TREE_TYPE (variant_part_decl);
23721 tree variant;
23722 dw_discr_list_ref *discr_lists;
23723 unsigned i;
23725 /* Compute how many variants there are in this variant part. */
23726 *discr_lists_length = 0;
23727 for (variant = TYPE_FIELDS (variant_part_type);
23728 variant != NULL_TREE;
23729 variant = DECL_CHAIN (variant))
23730 ++*discr_lists_length;
23732 *discr_decl = NULL_TREE;
23733 *discr_lists_p
23734 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23735 sizeof (**discr_lists_p));
23736 discr_lists = *discr_lists_p;
23738 /* And then analyze all variants to extract discriminant information for all
23739 of them. This analysis is conservative: as soon as we detect something we
23740 do not support, abort everything and pretend we found nothing. */
23741 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23742 variant != NULL_TREE;
23743 variant = DECL_CHAIN (variant), ++i)
23745 tree match_expr = DECL_QUALIFIER (variant);
23747 /* Now, try to analyze the predicate and deduce a discriminant for
23748 it. */
23749 if (match_expr == boolean_true_node)
23750 /* Typically happens for the default variant: it matches all cases that
23751 previous variants rejected. Don't output any matching value for
23752 this one. */
23753 continue;
23755 /* The following loop tries to iterate over each discriminant
23756 possibility: single values or ranges. */
23757 while (match_expr != NULL_TREE)
23759 tree next_round_match_expr;
23760 tree candidate_discr = NULL_TREE;
23761 dw_discr_list_ref new_node = NULL;
23763 /* Possibilities are matched one after the other by nested
23764 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23765 continue with the rest at next iteration. */
23766 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23768 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23769 match_expr = TREE_OPERAND (match_expr, 1);
23771 else
23772 next_round_match_expr = NULL_TREE;
23774 if (match_expr == boolean_false_node)
23775 /* This sub-expression matches nothing: just wait for the next
23776 one. */
23779 else if (TREE_CODE (match_expr) == EQ_EXPR)
23781 /* We are matching: <discr_field> == <integer_cst>
23782 This sub-expression matches a single value. */
23783 tree integer_cst = TREE_OPERAND (match_expr, 1);
23785 candidate_discr
23786 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23787 struct_type);
23789 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23790 if (!get_discr_value (integer_cst,
23791 &new_node->dw_discr_lower_bound))
23792 goto abort;
23793 new_node->dw_discr_range = false;
23796 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23798 /* We are matching:
23799 <discr_field> > <integer_cst>
23800 && <discr_field> < <integer_cst>.
23801 This sub-expression matches the range of values between the
23802 two matched integer constants. Note that comparisons can be
23803 inclusive or exclusive. */
23804 tree candidate_discr_1, candidate_discr_2;
23805 tree lower_cst, upper_cst;
23806 bool lower_cst_included, upper_cst_included;
23807 tree lower_op = TREE_OPERAND (match_expr, 0);
23808 tree upper_op = TREE_OPERAND (match_expr, 1);
23810 /* When the comparison is exclusive, the integer constant is not
23811 the discriminant range bound we are looking for: we will have
23812 to increment or decrement it. */
23813 if (TREE_CODE (lower_op) == GE_EXPR)
23814 lower_cst_included = true;
23815 else if (TREE_CODE (lower_op) == GT_EXPR)
23816 lower_cst_included = false;
23817 else
23818 goto abort;
23820 if (TREE_CODE (upper_op) == LE_EXPR)
23821 upper_cst_included = true;
23822 else if (TREE_CODE (upper_op) == LT_EXPR)
23823 upper_cst_included = false;
23824 else
23825 goto abort;
23827 /* Extract the discriminant from the first operand and check it
23828 is consistant with the same analysis in the second
23829 operand. */
23830 candidate_discr_1
23831 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23832 struct_type);
23833 candidate_discr_2
23834 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23835 struct_type);
23836 if (candidate_discr_1 == candidate_discr_2)
23837 candidate_discr = candidate_discr_1;
23838 else
23839 goto abort;
23841 /* Extract bounds from both. */
23842 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23843 lower_cst = TREE_OPERAND (lower_op, 1);
23844 upper_cst = TREE_OPERAND (upper_op, 1);
23846 if (!lower_cst_included)
23847 lower_cst
23848 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23849 build_int_cst (TREE_TYPE (lower_cst), 1));
23850 if (!upper_cst_included)
23851 upper_cst
23852 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23853 build_int_cst (TREE_TYPE (upper_cst), 1));
23855 if (!get_discr_value (lower_cst,
23856 &new_node->dw_discr_lower_bound)
23857 || !get_discr_value (upper_cst,
23858 &new_node->dw_discr_upper_bound))
23859 goto abort;
23861 new_node->dw_discr_range = true;
23864 else
23865 /* Unsupported sub-expression: we cannot determine the set of
23866 matching discriminant values. Abort everything. */
23867 goto abort;
23869 /* If the discriminant info is not consistant with what we saw so
23870 far, consider the analysis failed and abort everything. */
23871 if (candidate_discr == NULL_TREE
23872 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23873 goto abort;
23874 else
23875 *discr_decl = candidate_discr;
23877 if (new_node != NULL)
23879 new_node->dw_discr_next = discr_lists[i];
23880 discr_lists[i] = new_node;
23882 match_expr = next_round_match_expr;
23886 /* If we reach this point, we could match everything we were interested
23887 in. */
23888 return;
23890 abort:
23891 /* Clean all data structure and return no result. */
23892 free (*discr_lists_p);
23893 *discr_lists_p = NULL;
23894 *discr_decl = NULL_TREE;
23897 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23898 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23899 under CONTEXT_DIE.
23901 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23902 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23903 this type, which are record types, represent the available variants and each
23904 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23905 values are inferred from these attributes.
23907 In trees, the offsets for the fields inside these sub-records are relative
23908 to the variant part itself, whereas the corresponding DIEs should have
23909 offset attributes that are relative to the embedding record base address.
23910 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23911 must be an expression that computes the offset of the variant part to
23912 describe in DWARF. */
23914 static void
23915 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23916 dw_die_ref context_die)
23918 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23919 tree variant_part_offset = vlr_ctx->variant_part_offset;
23920 struct loc_descr_context ctx = {
23921 vlr_ctx->struct_type, /* context_type */
23922 NULL_TREE, /* base_decl */
23923 NULL, /* dpi */
23924 false, /* placeholder_arg */
23925 false /* placeholder_seen */
23928 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23929 NULL_TREE if there is no such field. */
23930 tree discr_decl = NULL_TREE;
23931 dw_discr_list_ref *discr_lists;
23932 unsigned discr_lists_length = 0;
23933 unsigned i;
23935 dw_die_ref dwarf_proc_die = NULL;
23936 dw_die_ref variant_part_die
23937 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23939 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23941 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23942 &discr_decl, &discr_lists, &discr_lists_length);
23944 if (discr_decl != NULL_TREE)
23946 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23948 if (discr_die)
23949 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23950 else
23951 /* We have no DIE for the discriminant, so just discard all
23952 discrimimant information in the output. */
23953 discr_decl = NULL_TREE;
23956 /* If the offset for this variant part is more complex than a constant,
23957 create a DWARF procedure for it so that we will not have to generate DWARF
23958 expressions for it for each member. */
23959 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23960 && (dwarf_version >= 3 || !dwarf_strict))
23962 const tree dwarf_proc_fndecl
23963 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23964 build_function_type (TREE_TYPE (variant_part_offset),
23965 NULL_TREE));
23966 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23967 const dw_loc_descr_ref dwarf_proc_body
23968 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23970 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23971 dwarf_proc_fndecl, context_die);
23972 if (dwarf_proc_die != NULL)
23973 variant_part_offset = dwarf_proc_call;
23976 /* Output DIEs for all variants. */
23977 i = 0;
23978 for (tree variant = TYPE_FIELDS (variant_part_type);
23979 variant != NULL_TREE;
23980 variant = DECL_CHAIN (variant), ++i)
23982 tree variant_type = TREE_TYPE (variant);
23983 dw_die_ref variant_die;
23985 /* All variants (i.e. members of a variant part) are supposed to be
23986 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23987 under these records. */
23988 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23990 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23991 equate_decl_number_to_die (variant, variant_die);
23993 /* Output discriminant values this variant matches, if any. */
23994 if (discr_decl == NULL || discr_lists[i] == NULL)
23995 /* In the case we have discriminant information at all, this is
23996 probably the default variant: as the standard says, don't
23997 output any discriminant value/list attribute. */
23999 else if (discr_lists[i]->dw_discr_next == NULL
24000 && !discr_lists[i]->dw_discr_range)
24001 /* If there is only one accepted value, don't bother outputting a
24002 list. */
24003 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24004 else
24005 add_discr_list (variant_die, discr_lists[i]);
24007 for (tree member = TYPE_FIELDS (variant_type);
24008 member != NULL_TREE;
24009 member = DECL_CHAIN (member))
24011 struct vlr_context vlr_sub_ctx = {
24012 vlr_ctx->struct_type, /* struct_type */
24013 NULL /* variant_part_offset */
24015 if (is_variant_part (member))
24017 /* All offsets for fields inside variant parts are relative to
24018 the top-level embedding RECORD_TYPE's base address. On the
24019 other hand, offsets in GCC's types are relative to the
24020 nested-most variant part. So we have to sum offsets each time
24021 we recurse. */
24023 vlr_sub_ctx.variant_part_offset
24024 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24025 variant_part_offset, byte_position (member));
24026 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24028 else
24030 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24031 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24036 free (discr_lists);
24039 /* Generate a DIE for a class member. */
24041 static void
24042 gen_member_die (tree type, dw_die_ref context_die)
24044 tree member;
24045 tree binfo = TYPE_BINFO (type);
24046 dw_die_ref child;
24048 /* If this is not an incomplete type, output descriptions of each of its
24049 members. Note that as we output the DIEs necessary to represent the
24050 members of this record or union type, we will also be trying to output
24051 DIEs to represent the *types* of those members. However the `type'
24052 function (above) will specifically avoid generating type DIEs for member
24053 types *within* the list of member DIEs for this (containing) type except
24054 for those types (of members) which are explicitly marked as also being
24055 members of this (containing) type themselves. The g++ front- end can
24056 force any given type to be treated as a member of some other (containing)
24057 type by setting the TYPE_CONTEXT of the given (member) type to point to
24058 the TREE node representing the appropriate (containing) type. */
24060 /* First output info about the base classes. */
24061 if (binfo)
24063 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24064 int i;
24065 tree base;
24067 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24068 gen_inheritance_die (base,
24069 (accesses ? (*accesses)[i] : access_public_node),
24070 type,
24071 context_die);
24074 /* Now output info about the data members and type members. */
24075 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24077 struct vlr_context vlr_ctx = { type, NULL_TREE };
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 child = lookup_decl_die (member);
24085 if (child)
24087 /* Handle inline static data members, which only have in-class
24088 declarations. */
24089 if (child->die_tag == DW_TAG_variable
24090 && child->die_parent == comp_unit_die ()
24091 && get_AT (child, DW_AT_specification) == NULL)
24093 reparent_child (child, context_die);
24094 child->die_tag = DW_TAG_member;
24096 else
24097 splice_child_die (context_die, child);
24100 /* Do not generate standard DWARF for variant parts if we are generating
24101 the corresponding GNAT encodings: DIEs generated for both would
24102 conflict in our mappings. */
24103 else if (is_variant_part (member)
24104 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24106 vlr_ctx.variant_part_offset = byte_position (member);
24107 gen_variant_part (member, &vlr_ctx, context_die);
24109 else
24111 vlr_ctx.variant_part_offset = NULL_TREE;
24112 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24115 /* For C++ inline static data members emit immediately a DW_TAG_variable
24116 DIE that will refer to that DW_TAG_member through
24117 DW_AT_specification. */
24118 if (TREE_STATIC (member)
24119 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24120 != -1))
24122 int old_extern = DECL_EXTERNAL (member);
24123 DECL_EXTERNAL (member) = 0;
24124 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24125 DECL_EXTERNAL (member) = old_extern;
24129 /* We do not keep type methods in type variants. */
24130 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24131 /* Now output info about the function members (if any). */
24132 if (TYPE_METHODS (type) != error_mark_node)
24133 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24135 /* Don't include clones in the member list. */
24136 if (DECL_ABSTRACT_ORIGIN (member))
24137 continue;
24138 /* Nor constructors for anonymous classes. */
24139 if (DECL_ARTIFICIAL (member)
24140 && dwarf2_name (member, 0) == NULL)
24141 continue;
24143 child = lookup_decl_die (member);
24144 if (child)
24145 splice_child_die (context_die, child);
24146 else
24147 gen_decl_die (member, NULL, NULL, context_die);
24151 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24152 is set, we pretend that the type was never defined, so we only get the
24153 member DIEs needed by later specification DIEs. */
24155 static void
24156 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24157 enum debug_info_usage usage)
24159 if (TREE_ASM_WRITTEN (type))
24161 /* Fill in the bound of variable-length fields in late dwarf if
24162 still incomplete. */
24163 if (!early_dwarf && variably_modified_type_p (type, NULL))
24164 for (tree member = TYPE_FIELDS (type);
24165 member;
24166 member = DECL_CHAIN (member))
24167 fill_variable_array_bounds (TREE_TYPE (member));
24168 return;
24171 dw_die_ref type_die = lookup_type_die (type);
24172 dw_die_ref scope_die = 0;
24173 int nested = 0;
24174 int complete = (TYPE_SIZE (type)
24175 && (! TYPE_STUB_DECL (type)
24176 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24177 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24178 complete = complete && should_emit_struct_debug (type, usage);
24180 if (type_die && ! complete)
24181 return;
24183 if (TYPE_CONTEXT (type) != NULL_TREE
24184 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24185 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24186 nested = 1;
24188 scope_die = scope_die_for (type, context_die);
24190 /* Generate child dies for template paramaters. */
24191 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24192 schedule_generic_params_dies_gen (type);
24194 if (! type_die || (nested && is_cu_die (scope_die)))
24195 /* First occurrence of type or toplevel definition of nested class. */
24197 dw_die_ref old_die = type_die;
24199 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24200 ? record_type_tag (type) : DW_TAG_union_type,
24201 scope_die, type);
24202 equate_type_number_to_die (type, type_die);
24203 if (old_die)
24204 add_AT_specification (type_die, old_die);
24205 else
24206 add_name_attribute (type_die, type_tag (type));
24208 else
24209 remove_AT (type_die, DW_AT_declaration);
24211 /* If this type has been completed, then give it a byte_size attribute and
24212 then give a list of members. */
24213 if (complete && !ns_decl)
24215 /* Prevent infinite recursion in cases where the type of some member of
24216 this type is expressed in terms of this type itself. */
24217 TREE_ASM_WRITTEN (type) = 1;
24218 add_byte_size_attribute (type_die, type);
24219 add_alignment_attribute (type_die, type);
24220 if (TYPE_STUB_DECL (type) != NULL_TREE)
24222 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24223 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24226 /* If the first reference to this type was as the return type of an
24227 inline function, then it may not have a parent. Fix this now. */
24228 if (type_die->die_parent == NULL)
24229 add_child_die (scope_die, type_die);
24231 push_decl_scope (type);
24232 gen_member_die (type, type_die);
24233 pop_decl_scope ();
24235 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24236 if (TYPE_ARTIFICIAL (type))
24237 add_AT_flag (type_die, DW_AT_artificial, 1);
24239 /* GNU extension: Record what type our vtable lives in. */
24240 if (TYPE_VFIELD (type))
24242 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24244 gen_type_die (vtype, context_die);
24245 add_AT_die_ref (type_die, DW_AT_containing_type,
24246 lookup_type_die (vtype));
24249 else
24251 add_AT_flag (type_die, DW_AT_declaration, 1);
24253 /* We don't need to do this for function-local types. */
24254 if (TYPE_STUB_DECL (type)
24255 && ! decl_function_context (TYPE_STUB_DECL (type)))
24256 vec_safe_push (incomplete_types, type);
24259 if (get_AT (type_die, DW_AT_name))
24260 add_pubtype (type, type_die);
24263 /* Generate a DIE for a subroutine _type_. */
24265 static void
24266 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24268 tree return_type = TREE_TYPE (type);
24269 dw_die_ref subr_die
24270 = new_die (DW_TAG_subroutine_type,
24271 scope_die_for (type, context_die), type);
24273 equate_type_number_to_die (type, subr_die);
24274 add_prototyped_attribute (subr_die, type);
24275 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24276 context_die);
24277 add_alignment_attribute (subr_die, type);
24278 gen_formal_types_die (type, subr_die);
24280 if (get_AT (subr_die, DW_AT_name))
24281 add_pubtype (type, subr_die);
24282 if ((dwarf_version >= 5 || !dwarf_strict)
24283 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24284 add_AT_flag (subr_die, DW_AT_reference, 1);
24285 if ((dwarf_version >= 5 || !dwarf_strict)
24286 && lang_hooks.types.type_dwarf_attribute (type,
24287 DW_AT_rvalue_reference) != -1)
24288 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24291 /* Generate a DIE for a type definition. */
24293 static void
24294 gen_typedef_die (tree decl, dw_die_ref context_die)
24296 dw_die_ref type_die;
24297 tree origin;
24299 if (TREE_ASM_WRITTEN (decl))
24301 if (DECL_ORIGINAL_TYPE (decl))
24302 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24303 return;
24306 TREE_ASM_WRITTEN (decl) = 1;
24307 type_die = new_die (DW_TAG_typedef, context_die, decl);
24308 origin = decl_ultimate_origin (decl);
24309 if (origin != NULL)
24310 add_abstract_origin_attribute (type_die, origin);
24311 else
24313 tree type = TREE_TYPE (decl);
24315 if (type == error_mark_node)
24316 return;
24318 add_name_and_src_coords_attributes (type_die, decl);
24319 if (DECL_ORIGINAL_TYPE (decl))
24321 type = DECL_ORIGINAL_TYPE (decl);
24323 if (type == error_mark_node)
24324 return;
24326 gcc_assert (type != TREE_TYPE (decl));
24327 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24329 else
24331 if (is_naming_typedef_decl (TYPE_NAME (type)))
24333 /* Here, we are in the case of decl being a typedef naming
24334 an anonymous type, e.g:
24335 typedef struct {...} foo;
24336 In that case TREE_TYPE (decl) is not a typedef variant
24337 type and TYPE_NAME of the anonymous type is set to the
24338 TYPE_DECL of the typedef. This construct is emitted by
24339 the C++ FE.
24341 TYPE is the anonymous struct named by the typedef
24342 DECL. As we need the DW_AT_type attribute of the
24343 DW_TAG_typedef to point to the DIE of TYPE, let's
24344 generate that DIE right away. add_type_attribute
24345 called below will then pick (via lookup_type_die) that
24346 anonymous struct DIE. */
24347 if (!TREE_ASM_WRITTEN (type))
24348 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24350 /* This is a GNU Extension. We are adding a
24351 DW_AT_linkage_name attribute to the DIE of the
24352 anonymous struct TYPE. The value of that attribute
24353 is the name of the typedef decl naming the anonymous
24354 struct. This greatly eases the work of consumers of
24355 this debug info. */
24356 add_linkage_name_raw (lookup_type_die (type), decl);
24360 add_type_attribute (type_die, type, decl_quals (decl), false,
24361 context_die);
24363 if (is_naming_typedef_decl (decl))
24364 /* We want that all subsequent calls to lookup_type_die with
24365 TYPE in argument yield the DW_TAG_typedef we have just
24366 created. */
24367 equate_type_number_to_die (type, type_die);
24369 type = TREE_TYPE (decl);
24371 add_alignment_attribute (type_die, type);
24373 add_accessibility_attribute (type_die, decl);
24376 if (DECL_ABSTRACT_P (decl))
24377 equate_decl_number_to_die (decl, type_die);
24379 if (get_AT (type_die, DW_AT_name))
24380 add_pubtype (decl, type_die);
24383 /* Generate a DIE for a struct, class, enum or union type. */
24385 static void
24386 gen_tagged_type_die (tree type,
24387 dw_die_ref context_die,
24388 enum debug_info_usage usage)
24390 int need_pop;
24392 if (type == NULL_TREE
24393 || !is_tagged_type (type))
24394 return;
24396 if (TREE_ASM_WRITTEN (type))
24397 need_pop = 0;
24398 /* If this is a nested type whose containing class hasn't been written
24399 out yet, writing it out will cover this one, too. This does not apply
24400 to instantiations of member class templates; they need to be added to
24401 the containing class as they are generated. FIXME: This hurts the
24402 idea of combining type decls from multiple TUs, since we can't predict
24403 what set of template instantiations we'll get. */
24404 else if (TYPE_CONTEXT (type)
24405 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24406 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24408 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24410 if (TREE_ASM_WRITTEN (type))
24411 return;
24413 /* If that failed, attach ourselves to the stub. */
24414 push_decl_scope (TYPE_CONTEXT (type));
24415 context_die = lookup_type_die (TYPE_CONTEXT (type));
24416 need_pop = 1;
24418 else if (TYPE_CONTEXT (type) != NULL_TREE
24419 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24421 /* If this type is local to a function that hasn't been written
24422 out yet, use a NULL context for now; it will be fixed up in
24423 decls_for_scope. */
24424 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24425 /* A declaration DIE doesn't count; nested types need to go in the
24426 specification. */
24427 if (context_die && is_declaration_die (context_die))
24428 context_die = NULL;
24429 need_pop = 0;
24431 else
24433 context_die = declare_in_namespace (type, context_die);
24434 need_pop = 0;
24437 if (TREE_CODE (type) == ENUMERAL_TYPE)
24439 /* This might have been written out by the call to
24440 declare_in_namespace. */
24441 if (!TREE_ASM_WRITTEN (type))
24442 gen_enumeration_type_die (type, context_die);
24444 else
24445 gen_struct_or_union_type_die (type, context_die, usage);
24447 if (need_pop)
24448 pop_decl_scope ();
24450 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24451 it up if it is ever completed. gen_*_type_die will set it for us
24452 when appropriate. */
24455 /* Generate a type description DIE. */
24457 static void
24458 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24459 enum debug_info_usage usage)
24461 struct array_descr_info info;
24463 if (type == NULL_TREE || type == error_mark_node)
24464 return;
24466 if (flag_checking && type)
24467 verify_type (type);
24469 if (TYPE_NAME (type) != NULL_TREE
24470 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24471 && is_redundant_typedef (TYPE_NAME (type))
24472 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24473 /* The DECL of this type is a typedef we don't want to emit debug
24474 info for but we want debug info for its underlying typedef.
24475 This can happen for e.g, the injected-class-name of a C++
24476 type. */
24477 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24479 /* If TYPE is a typedef type variant, let's generate debug info
24480 for the parent typedef which TYPE is a type of. */
24481 if (typedef_variant_p (type))
24483 if (TREE_ASM_WRITTEN (type))
24484 return;
24486 /* Prevent broken recursion; we can't hand off to the same type. */
24487 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24489 /* Give typedefs the right scope. */
24490 context_die = scope_die_for (type, context_die);
24492 TREE_ASM_WRITTEN (type) = 1;
24494 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24495 return;
24498 /* If type is an anonymous tagged type named by a typedef, let's
24499 generate debug info for the typedef. */
24500 if (is_naming_typedef_decl (TYPE_NAME (type)))
24502 /* Use the DIE of the containing namespace as the parent DIE of
24503 the type description DIE we want to generate. */
24504 if (DECL_CONTEXT (TYPE_NAME (type))
24505 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24506 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24508 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24509 return;
24512 if (lang_hooks.types.get_debug_type)
24514 tree debug_type = lang_hooks.types.get_debug_type (type);
24516 if (debug_type != NULL_TREE && debug_type != type)
24518 gen_type_die_with_usage (debug_type, context_die, usage);
24519 return;
24523 /* We are going to output a DIE to represent the unqualified version
24524 of this type (i.e. without any const or volatile qualifiers) so
24525 get the main variant (i.e. the unqualified version) of this type
24526 now. (Vectors and arrays are special because the debugging info is in the
24527 cloned type itself. Similarly function/method types can contain extra
24528 ref-qualification). */
24529 if (TREE_CODE (type) == FUNCTION_TYPE
24530 || TREE_CODE (type) == METHOD_TYPE)
24532 /* For function/method types, can't use type_main_variant here,
24533 because that can have different ref-qualifiers for C++,
24534 but try to canonicalize. */
24535 tree main = TYPE_MAIN_VARIANT (type);
24536 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24538 if (check_base_type (t, main) && check_lang_type (t, type))
24540 type = t;
24541 break;
24545 else if (TREE_CODE (type) != VECTOR_TYPE
24546 && TREE_CODE (type) != ARRAY_TYPE)
24547 type = type_main_variant (type);
24549 /* If this is an array type with hidden descriptor, handle it first. */
24550 if (!TREE_ASM_WRITTEN (type)
24551 && lang_hooks.types.get_array_descr_info)
24553 memset (&info, 0, sizeof (info));
24554 if (lang_hooks.types.get_array_descr_info (type, &info))
24556 /* Fortran sometimes emits array types with no dimension. */
24557 gcc_assert (info.ndimensions >= 0
24558 && (info.ndimensions
24559 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24560 gen_descr_array_type_die (type, &info, context_die);
24561 TREE_ASM_WRITTEN (type) = 1;
24562 return;
24566 if (TREE_ASM_WRITTEN (type))
24568 /* Variable-length types may be incomplete even if
24569 TREE_ASM_WRITTEN. For such types, fall through to
24570 gen_array_type_die() and possibly fill in
24571 DW_AT_{upper,lower}_bound attributes. */
24572 if ((TREE_CODE (type) != ARRAY_TYPE
24573 && TREE_CODE (type) != RECORD_TYPE
24574 && TREE_CODE (type) != UNION_TYPE
24575 && TREE_CODE (type) != QUAL_UNION_TYPE)
24576 || !variably_modified_type_p (type, NULL))
24577 return;
24580 switch (TREE_CODE (type))
24582 case ERROR_MARK:
24583 break;
24585 case POINTER_TYPE:
24586 case REFERENCE_TYPE:
24587 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24588 ensures that the gen_type_die recursion will terminate even if the
24589 type is recursive. Recursive types are possible in Ada. */
24590 /* ??? We could perhaps do this for all types before the switch
24591 statement. */
24592 TREE_ASM_WRITTEN (type) = 1;
24594 /* For these types, all that is required is that we output a DIE (or a
24595 set of DIEs) to represent the "basis" type. */
24596 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24597 DINFO_USAGE_IND_USE);
24598 break;
24600 case OFFSET_TYPE:
24601 /* This code is used for C++ pointer-to-data-member types.
24602 Output a description of the relevant class type. */
24603 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24604 DINFO_USAGE_IND_USE);
24606 /* Output a description of the type of the object pointed to. */
24607 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24608 DINFO_USAGE_IND_USE);
24610 /* Now output a DIE to represent this pointer-to-data-member type
24611 itself. */
24612 gen_ptr_to_mbr_type_die (type, context_die);
24613 break;
24615 case FUNCTION_TYPE:
24616 /* Force out return type (in case it wasn't forced out already). */
24617 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24618 DINFO_USAGE_DIR_USE);
24619 gen_subroutine_type_die (type, context_die);
24620 break;
24622 case METHOD_TYPE:
24623 /* Force out return type (in case it wasn't forced out already). */
24624 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24625 DINFO_USAGE_DIR_USE);
24626 gen_subroutine_type_die (type, context_die);
24627 break;
24629 case ARRAY_TYPE:
24630 case VECTOR_TYPE:
24631 gen_array_type_die (type, context_die);
24632 break;
24634 case ENUMERAL_TYPE:
24635 case RECORD_TYPE:
24636 case UNION_TYPE:
24637 case QUAL_UNION_TYPE:
24638 gen_tagged_type_die (type, context_die, usage);
24639 return;
24641 case VOID_TYPE:
24642 case INTEGER_TYPE:
24643 case REAL_TYPE:
24644 case FIXED_POINT_TYPE:
24645 case COMPLEX_TYPE:
24646 case BOOLEAN_TYPE:
24647 case POINTER_BOUNDS_TYPE:
24648 /* No DIEs needed for fundamental types. */
24649 break;
24651 case NULLPTR_TYPE:
24652 case LANG_TYPE:
24653 /* Just use DW_TAG_unspecified_type. */
24655 dw_die_ref type_die = lookup_type_die (type);
24656 if (type_die == NULL)
24658 tree name = TYPE_IDENTIFIER (type);
24659 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24660 type);
24661 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24662 equate_type_number_to_die (type, type_die);
24665 break;
24667 default:
24668 if (is_cxx_auto (type))
24670 tree name = TYPE_IDENTIFIER (type);
24671 dw_die_ref *die = (name == get_identifier ("auto")
24672 ? &auto_die : &decltype_auto_die);
24673 if (!*die)
24675 *die = new_die (DW_TAG_unspecified_type,
24676 comp_unit_die (), NULL_TREE);
24677 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24679 equate_type_number_to_die (type, *die);
24680 break;
24682 gcc_unreachable ();
24685 TREE_ASM_WRITTEN (type) = 1;
24688 static void
24689 gen_type_die (tree type, dw_die_ref context_die)
24691 if (type != error_mark_node)
24693 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24694 if (flag_checking)
24696 dw_die_ref die = lookup_type_die (type);
24697 if (die)
24698 check_die (die);
24703 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24704 things which are local to the given block. */
24706 static void
24707 gen_block_die (tree stmt, dw_die_ref context_die)
24709 int must_output_die = 0;
24710 bool inlined_func;
24712 /* Ignore blocks that are NULL. */
24713 if (stmt == NULL_TREE)
24714 return;
24716 inlined_func = inlined_function_outer_scope_p (stmt);
24718 /* If the block is one fragment of a non-contiguous block, do not
24719 process the variables, since they will have been done by the
24720 origin block. Do process subblocks. */
24721 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24723 tree sub;
24725 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24726 gen_block_die (sub, context_die);
24728 return;
24731 /* Determine if we need to output any Dwarf DIEs at all to represent this
24732 block. */
24733 if (inlined_func)
24734 /* The outer scopes for inlinings *must* always be represented. We
24735 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24736 must_output_die = 1;
24737 else
24739 /* Determine if this block directly contains any "significant"
24740 local declarations which we will need to output DIEs for. */
24741 if (debug_info_level > DINFO_LEVEL_TERSE)
24742 /* We are not in terse mode so *any* local declaration counts
24743 as being a "significant" one. */
24744 must_output_die = ((BLOCK_VARS (stmt) != NULL
24745 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24746 && (TREE_USED (stmt)
24747 || TREE_ASM_WRITTEN (stmt)
24748 || BLOCK_ABSTRACT (stmt)));
24749 else if ((TREE_USED (stmt)
24750 || TREE_ASM_WRITTEN (stmt)
24751 || BLOCK_ABSTRACT (stmt))
24752 && !dwarf2out_ignore_block (stmt))
24753 must_output_die = 1;
24756 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24757 DIE for any block which contains no significant local declarations at
24758 all. Rather, in such cases we just call `decls_for_scope' so that any
24759 needed Dwarf info for any sub-blocks will get properly generated. Note
24760 that in terse mode, our definition of what constitutes a "significant"
24761 local declaration gets restricted to include only inlined function
24762 instances and local (nested) function definitions. */
24763 if (must_output_die)
24765 if (inlined_func)
24767 /* If STMT block is abstract, that means we have been called
24768 indirectly from dwarf2out_abstract_function.
24769 That function rightfully marks the descendent blocks (of
24770 the abstract function it is dealing with) as being abstract,
24771 precisely to prevent us from emitting any
24772 DW_TAG_inlined_subroutine DIE as a descendent
24773 of an abstract function instance. So in that case, we should
24774 not call gen_inlined_subroutine_die.
24776 Later though, when cgraph asks dwarf2out to emit info
24777 for the concrete instance of the function decl into which
24778 the concrete instance of STMT got inlined, the later will lead
24779 to the generation of a DW_TAG_inlined_subroutine DIE. */
24780 if (! BLOCK_ABSTRACT (stmt))
24781 gen_inlined_subroutine_die (stmt, context_die);
24783 else
24784 gen_lexical_block_die (stmt, context_die);
24786 else
24787 decls_for_scope (stmt, context_die);
24790 /* Process variable DECL (or variable with origin ORIGIN) within
24791 block STMT and add it to CONTEXT_DIE. */
24792 static void
24793 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24795 dw_die_ref die;
24796 tree decl_or_origin = decl ? decl : origin;
24798 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24799 die = lookup_decl_die (decl_or_origin);
24800 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24802 if (TYPE_DECL_IS_STUB (decl_or_origin))
24803 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24804 else
24805 die = lookup_decl_die (decl_or_origin);
24806 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24807 if (! die && ! early_dwarf)
24808 return;
24810 else
24811 die = NULL;
24813 if (die != NULL && die->die_parent == NULL)
24814 add_child_die (context_die, die);
24815 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24817 if (early_dwarf)
24818 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24819 stmt, context_die);
24821 else
24822 gen_decl_die (decl, origin, NULL, context_die);
24825 /* Generate all of the decls declared within a given scope and (recursively)
24826 all of its sub-blocks. */
24828 static void
24829 decls_for_scope (tree stmt, dw_die_ref context_die)
24831 tree decl;
24832 unsigned int i;
24833 tree subblocks;
24835 /* Ignore NULL blocks. */
24836 if (stmt == NULL_TREE)
24837 return;
24839 /* Output the DIEs to represent all of the data objects and typedefs
24840 declared directly within this block but not within any nested
24841 sub-blocks. Also, nested function and tag DIEs have been
24842 generated with a parent of NULL; fix that up now. We don't
24843 have to do this if we're at -g1. */
24844 if (debug_info_level > DINFO_LEVEL_TERSE)
24846 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24847 process_scope_var (stmt, decl, NULL_TREE, context_die);
24848 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24849 origin - avoid doing this twice as we have no good way to see
24850 if we've done it once already. */
24851 if (! early_dwarf)
24852 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24853 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24854 context_die);
24857 /* Even if we're at -g1, we need to process the subblocks in order to get
24858 inlined call information. */
24860 /* Output the DIEs to represent all sub-blocks (and the items declared
24861 therein) of this block. */
24862 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24863 subblocks != NULL;
24864 subblocks = BLOCK_CHAIN (subblocks))
24865 gen_block_die (subblocks, context_die);
24868 /* Is this a typedef we can avoid emitting? */
24870 bool
24871 is_redundant_typedef (const_tree decl)
24873 if (TYPE_DECL_IS_STUB (decl))
24874 return true;
24876 if (DECL_ARTIFICIAL (decl)
24877 && DECL_CONTEXT (decl)
24878 && is_tagged_type (DECL_CONTEXT (decl))
24879 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24880 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24881 /* Also ignore the artificial member typedef for the class name. */
24882 return true;
24884 return false;
24887 /* Return TRUE if TYPE is a typedef that names a type for linkage
24888 purposes. This kind of typedefs is produced by the C++ FE for
24889 constructs like:
24891 typedef struct {...} foo;
24893 In that case, there is no typedef variant type produced for foo.
24894 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24895 struct type. */
24897 static bool
24898 is_naming_typedef_decl (const_tree decl)
24900 if (decl == NULL_TREE
24901 || TREE_CODE (decl) != TYPE_DECL
24902 || DECL_NAMELESS (decl)
24903 || !is_tagged_type (TREE_TYPE (decl))
24904 || DECL_IS_BUILTIN (decl)
24905 || is_redundant_typedef (decl)
24906 /* It looks like Ada produces TYPE_DECLs that are very similar
24907 to C++ naming typedefs but that have different
24908 semantics. Let's be specific to c++ for now. */
24909 || !is_cxx (decl))
24910 return FALSE;
24912 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24913 && TYPE_NAME (TREE_TYPE (decl)) == decl
24914 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24915 != TYPE_NAME (TREE_TYPE (decl))));
24918 /* Looks up the DIE for a context. */
24920 static inline dw_die_ref
24921 lookup_context_die (tree context)
24923 if (context)
24925 /* Find die that represents this context. */
24926 if (TYPE_P (context))
24928 context = TYPE_MAIN_VARIANT (context);
24929 dw_die_ref ctx = lookup_type_die (context);
24930 if (!ctx)
24931 return NULL;
24932 return strip_naming_typedef (context, ctx);
24934 else
24935 return lookup_decl_die (context);
24937 return comp_unit_die ();
24940 /* Returns the DIE for a context. */
24942 static inline dw_die_ref
24943 get_context_die (tree context)
24945 if (context)
24947 /* Find die that represents this context. */
24948 if (TYPE_P (context))
24950 context = TYPE_MAIN_VARIANT (context);
24951 return strip_naming_typedef (context, force_type_die (context));
24953 else
24954 return force_decl_die (context);
24956 return comp_unit_die ();
24959 /* Returns the DIE for decl. A DIE will always be returned. */
24961 static dw_die_ref
24962 force_decl_die (tree decl)
24964 dw_die_ref decl_die;
24965 unsigned saved_external_flag;
24966 tree save_fn = NULL_TREE;
24967 decl_die = lookup_decl_die (decl);
24968 if (!decl_die)
24970 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24972 decl_die = lookup_decl_die (decl);
24973 if (decl_die)
24974 return decl_die;
24976 switch (TREE_CODE (decl))
24978 case FUNCTION_DECL:
24979 /* Clear current_function_decl, so that gen_subprogram_die thinks
24980 that this is a declaration. At this point, we just want to force
24981 declaration die. */
24982 save_fn = current_function_decl;
24983 current_function_decl = NULL_TREE;
24984 gen_subprogram_die (decl, context_die);
24985 current_function_decl = save_fn;
24986 break;
24988 case VAR_DECL:
24989 /* Set external flag to force declaration die. Restore it after
24990 gen_decl_die() call. */
24991 saved_external_flag = DECL_EXTERNAL (decl);
24992 DECL_EXTERNAL (decl) = 1;
24993 gen_decl_die (decl, NULL, NULL, context_die);
24994 DECL_EXTERNAL (decl) = saved_external_flag;
24995 break;
24997 case NAMESPACE_DECL:
24998 if (dwarf_version >= 3 || !dwarf_strict)
24999 dwarf2out_decl (decl);
25000 else
25001 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25002 decl_die = comp_unit_die ();
25003 break;
25005 case TRANSLATION_UNIT_DECL:
25006 decl_die = comp_unit_die ();
25007 break;
25009 default:
25010 gcc_unreachable ();
25013 /* We should be able to find the DIE now. */
25014 if (!decl_die)
25015 decl_die = lookup_decl_die (decl);
25016 gcc_assert (decl_die);
25019 return decl_die;
25022 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25023 always returned. */
25025 static dw_die_ref
25026 force_type_die (tree type)
25028 dw_die_ref type_die;
25030 type_die = lookup_type_die (type);
25031 if (!type_die)
25033 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25035 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25036 false, context_die);
25037 gcc_assert (type_die);
25039 return type_die;
25042 /* Force out any required namespaces to be able to output DECL,
25043 and return the new context_die for it, if it's changed. */
25045 static dw_die_ref
25046 setup_namespace_context (tree thing, dw_die_ref context_die)
25048 tree context = (DECL_P (thing)
25049 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25050 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25051 /* Force out the namespace. */
25052 context_die = force_decl_die (context);
25054 return context_die;
25057 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25058 type) within its namespace, if appropriate.
25060 For compatibility with older debuggers, namespace DIEs only contain
25061 declarations; all definitions are emitted at CU scope, with
25062 DW_AT_specification pointing to the declaration (like with class
25063 members). */
25065 static dw_die_ref
25066 declare_in_namespace (tree thing, dw_die_ref context_die)
25068 dw_die_ref ns_context;
25070 if (debug_info_level <= DINFO_LEVEL_TERSE)
25071 return context_die;
25073 /* External declarations in the local scope only need to be emitted
25074 once, not once in the namespace and once in the scope.
25076 This avoids declaring the `extern' below in the
25077 namespace DIE as well as in the innermost scope:
25079 namespace S
25081 int i=5;
25082 int foo()
25084 int i=8;
25085 extern int i;
25086 return i;
25090 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25091 return context_die;
25093 /* If this decl is from an inlined function, then don't try to emit it in its
25094 namespace, as we will get confused. It would have already been emitted
25095 when the abstract instance of the inline function was emitted anyways. */
25096 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25097 return context_die;
25099 ns_context = setup_namespace_context (thing, context_die);
25101 if (ns_context != context_die)
25103 if (is_fortran ())
25104 return ns_context;
25105 if (DECL_P (thing))
25106 gen_decl_die (thing, NULL, NULL, ns_context);
25107 else
25108 gen_type_die (thing, ns_context);
25110 return context_die;
25113 /* Generate a DIE for a namespace or namespace alias. */
25115 static void
25116 gen_namespace_die (tree decl, dw_die_ref context_die)
25118 dw_die_ref namespace_die;
25120 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25121 they are an alias of. */
25122 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25124 /* Output a real namespace or module. */
25125 context_die = setup_namespace_context (decl, comp_unit_die ());
25126 namespace_die = new_die (is_fortran ()
25127 ? DW_TAG_module : DW_TAG_namespace,
25128 context_die, decl);
25129 /* For Fortran modules defined in different CU don't add src coords. */
25130 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25132 const char *name = dwarf2_name (decl, 0);
25133 if (name)
25134 add_name_attribute (namespace_die, name);
25136 else
25137 add_name_and_src_coords_attributes (namespace_die, decl);
25138 if (DECL_EXTERNAL (decl))
25139 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25140 equate_decl_number_to_die (decl, namespace_die);
25142 else
25144 /* Output a namespace alias. */
25146 /* Force out the namespace we are an alias of, if necessary. */
25147 dw_die_ref origin_die
25148 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25150 if (DECL_FILE_SCOPE_P (decl)
25151 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25152 context_die = setup_namespace_context (decl, comp_unit_die ());
25153 /* Now create the namespace alias DIE. */
25154 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25155 add_name_and_src_coords_attributes (namespace_die, decl);
25156 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25157 equate_decl_number_to_die (decl, namespace_die);
25159 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25160 if (want_pubnames ())
25161 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25164 /* Generate Dwarf debug information for a decl described by DECL.
25165 The return value is currently only meaningful for PARM_DECLs,
25166 for all other decls it returns NULL.
25168 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25169 It can be NULL otherwise. */
25171 static dw_die_ref
25172 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25173 dw_die_ref context_die)
25175 tree decl_or_origin = decl ? decl : origin;
25176 tree class_origin = NULL, ultimate_origin;
25178 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25179 return NULL;
25181 /* Ignore pointer bounds decls. */
25182 if (DECL_P (decl_or_origin)
25183 && TREE_TYPE (decl_or_origin)
25184 && POINTER_BOUNDS_P (decl_or_origin))
25185 return NULL;
25187 switch (TREE_CODE (decl_or_origin))
25189 case ERROR_MARK:
25190 break;
25192 case CONST_DECL:
25193 if (!is_fortran () && !is_ada ())
25195 /* The individual enumerators of an enum type get output when we output
25196 the Dwarf representation of the relevant enum type itself. */
25197 break;
25200 /* Emit its type. */
25201 gen_type_die (TREE_TYPE (decl), context_die);
25203 /* And its containing namespace. */
25204 context_die = declare_in_namespace (decl, context_die);
25206 gen_const_die (decl, context_die);
25207 break;
25209 case FUNCTION_DECL:
25210 /* Don't output any DIEs to represent mere function declarations,
25211 unless they are class members or explicit block externs. */
25212 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25213 && DECL_FILE_SCOPE_P (decl_or_origin)
25214 && (current_function_decl == NULL_TREE
25215 || DECL_ARTIFICIAL (decl_or_origin)))
25216 break;
25218 #if 0
25219 /* FIXME */
25220 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25221 on local redeclarations of global functions. That seems broken. */
25222 if (current_function_decl != decl)
25223 /* This is only a declaration. */;
25224 #endif
25226 /* If we're emitting a clone, emit info for the abstract instance. */
25227 if (origin || DECL_ORIGIN (decl) != decl)
25228 dwarf2out_abstract_function (origin
25229 ? DECL_ORIGIN (origin)
25230 : DECL_ABSTRACT_ORIGIN (decl));
25232 /* If we're emitting an out-of-line copy of an inline function,
25233 emit info for the abstract instance and set up to refer to it. */
25234 else if (cgraph_function_possibly_inlined_p (decl)
25235 && ! DECL_ABSTRACT_P (decl)
25236 && ! class_or_namespace_scope_p (context_die)
25237 /* dwarf2out_abstract_function won't emit a die if this is just
25238 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25239 that case, because that works only if we have a die. */
25240 && DECL_INITIAL (decl) != NULL_TREE)
25242 dwarf2out_abstract_function (decl);
25243 set_decl_origin_self (decl);
25246 /* Otherwise we're emitting the primary DIE for this decl. */
25247 else if (debug_info_level > DINFO_LEVEL_TERSE)
25249 /* Before we describe the FUNCTION_DECL itself, make sure that we
25250 have its containing type. */
25251 if (!origin)
25252 origin = decl_class_context (decl);
25253 if (origin != NULL_TREE)
25254 gen_type_die (origin, context_die);
25256 /* And its return type. */
25257 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25259 /* And its virtual context. */
25260 if (DECL_VINDEX (decl) != NULL_TREE)
25261 gen_type_die (DECL_CONTEXT (decl), context_die);
25263 /* Make sure we have a member DIE for decl. */
25264 if (origin != NULL_TREE)
25265 gen_type_die_for_member (origin, decl, context_die);
25267 /* And its containing namespace. */
25268 context_die = declare_in_namespace (decl, context_die);
25271 /* Now output a DIE to represent the function itself. */
25272 if (decl)
25273 gen_subprogram_die (decl, context_die);
25274 break;
25276 case TYPE_DECL:
25277 /* If we are in terse mode, don't generate any DIEs to represent any
25278 actual typedefs. */
25279 if (debug_info_level <= DINFO_LEVEL_TERSE)
25280 break;
25282 /* In the special case of a TYPE_DECL node representing the declaration
25283 of some type tag, if the given TYPE_DECL is marked as having been
25284 instantiated from some other (original) TYPE_DECL node (e.g. one which
25285 was generated within the original definition of an inline function) we
25286 used to generate a special (abbreviated) DW_TAG_structure_type,
25287 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25288 should be actually referencing those DIEs, as variable DIEs with that
25289 type would be emitted already in the abstract origin, so it was always
25290 removed during unused type prunning. Don't add anything in this
25291 case. */
25292 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25293 break;
25295 if (is_redundant_typedef (decl))
25296 gen_type_die (TREE_TYPE (decl), context_die);
25297 else
25298 /* Output a DIE to represent the typedef itself. */
25299 gen_typedef_die (decl, context_die);
25300 break;
25302 case LABEL_DECL:
25303 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25304 gen_label_die (decl, context_die);
25305 break;
25307 case VAR_DECL:
25308 case RESULT_DECL:
25309 /* If we are in terse mode, don't generate any DIEs to represent any
25310 variable declarations or definitions. */
25311 if (debug_info_level <= DINFO_LEVEL_TERSE)
25312 break;
25314 /* Output any DIEs that are needed to specify the type of this data
25315 object. */
25316 if (decl_by_reference_p (decl_or_origin))
25317 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25318 else
25319 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25321 /* And its containing type. */
25322 class_origin = decl_class_context (decl_or_origin);
25323 if (class_origin != NULL_TREE)
25324 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25326 /* And its containing namespace. */
25327 context_die = declare_in_namespace (decl_or_origin, context_die);
25329 /* Now output the DIE to represent the data object itself. This gets
25330 complicated because of the possibility that the VAR_DECL really
25331 represents an inlined instance of a formal parameter for an inline
25332 function. */
25333 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25334 if (ultimate_origin != NULL_TREE
25335 && TREE_CODE (ultimate_origin) == PARM_DECL)
25336 gen_formal_parameter_die (decl, origin,
25337 true /* Emit name attribute. */,
25338 context_die);
25339 else
25340 gen_variable_die (decl, origin, context_die);
25341 break;
25343 case FIELD_DECL:
25344 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25345 /* Ignore the nameless fields that are used to skip bits but handle C++
25346 anonymous unions and structs. */
25347 if (DECL_NAME (decl) != NULL_TREE
25348 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25349 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25351 gen_type_die (member_declared_type (decl), context_die);
25352 gen_field_die (decl, ctx, context_die);
25354 break;
25356 case PARM_DECL:
25357 if (DECL_BY_REFERENCE (decl_or_origin))
25358 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25359 else
25360 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25361 return gen_formal_parameter_die (decl, origin,
25362 true /* Emit name attribute. */,
25363 context_die);
25365 case NAMESPACE_DECL:
25366 if (dwarf_version >= 3 || !dwarf_strict)
25367 gen_namespace_die (decl, context_die);
25368 break;
25370 case IMPORTED_DECL:
25371 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25372 DECL_CONTEXT (decl), context_die);
25373 break;
25375 case NAMELIST_DECL:
25376 gen_namelist_decl (DECL_NAME (decl), context_die,
25377 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25378 break;
25380 default:
25381 /* Probably some frontend-internal decl. Assume we don't care. */
25382 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25383 break;
25386 return NULL;
25389 /* Output initial debug information for global DECL. Called at the
25390 end of the parsing process.
25392 This is the initial debug generation process. As such, the DIEs
25393 generated may be incomplete. A later debug generation pass
25394 (dwarf2out_late_global_decl) will augment the information generated
25395 in this pass (e.g., with complete location info). */
25397 static void
25398 dwarf2out_early_global_decl (tree decl)
25400 set_early_dwarf s;
25402 /* gen_decl_die() will set DECL_ABSTRACT because
25403 cgraph_function_possibly_inlined_p() returns true. This is in
25404 turn will cause DW_AT_inline attributes to be set.
25406 This happens because at early dwarf generation, there is no
25407 cgraph information, causing cgraph_function_possibly_inlined_p()
25408 to return true. Trick cgraph_function_possibly_inlined_p()
25409 while we generate dwarf early. */
25410 bool save = symtab->global_info_ready;
25411 symtab->global_info_ready = true;
25413 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25414 other DECLs and they can point to template types or other things
25415 that dwarf2out can't handle when done via dwarf2out_decl. */
25416 if (TREE_CODE (decl) != TYPE_DECL
25417 && TREE_CODE (decl) != PARM_DECL)
25419 tree save_fndecl = current_function_decl;
25420 if (TREE_CODE (decl) == FUNCTION_DECL)
25422 /* No cfun means the symbol has no body, so there's nothing
25423 to emit. */
25424 if (!DECL_STRUCT_FUNCTION (decl))
25425 goto early_decl_exit;
25427 /* For nested functions, make sure we have DIEs for the parents first
25428 so that all nested DIEs are generated at the proper scope in the
25429 first shot. */
25430 tree context = decl_function_context (decl);
25431 if (context != NULL && lookup_decl_die (context) == NULL)
25433 current_function_decl = context;
25434 dwarf2out_decl (context);
25437 current_function_decl = decl;
25439 dwarf2out_decl (decl);
25440 if (TREE_CODE (decl) == FUNCTION_DECL)
25441 current_function_decl = save_fndecl;
25443 early_decl_exit:
25444 symtab->global_info_ready = save;
25447 /* Output debug information for global decl DECL. Called from
25448 toplev.c after compilation proper has finished. */
25450 static void
25451 dwarf2out_late_global_decl (tree decl)
25453 /* Fill-in any location information we were unable to determine
25454 on the first pass. */
25455 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25457 dw_die_ref die = lookup_decl_die (decl);
25459 /* We have to generate early debug late for LTO. */
25460 if (! die && in_lto_p)
25462 dwarf2out_decl (decl);
25463 die = lookup_decl_die (decl);
25466 if (die)
25468 /* We get called via the symtab code invoking late_global_decl
25469 for symbols that are optimized out. Do not add locations
25470 for those. */
25471 varpool_node *node = varpool_node::get (decl);
25472 if (! node || ! node->definition)
25473 tree_add_const_value_attribute_for_decl (die, decl);
25474 else
25475 add_location_or_const_value_attribute (die, decl, false);
25480 /* Output debug information for type decl DECL. Called from toplev.c
25481 and from language front ends (to record built-in types). */
25482 static void
25483 dwarf2out_type_decl (tree decl, int local)
25485 if (!local)
25487 set_early_dwarf s;
25488 dwarf2out_decl (decl);
25492 /* Output debug information for imported module or decl DECL.
25493 NAME is non-NULL name in the lexical block if the decl has been renamed.
25494 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25495 that DECL belongs to.
25496 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25497 static void
25498 dwarf2out_imported_module_or_decl_1 (tree decl,
25499 tree name,
25500 tree lexical_block,
25501 dw_die_ref lexical_block_die)
25503 expanded_location xloc;
25504 dw_die_ref imported_die = NULL;
25505 dw_die_ref at_import_die;
25507 if (TREE_CODE (decl) == IMPORTED_DECL)
25509 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25510 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25511 gcc_assert (decl);
25513 else
25514 xloc = expand_location (input_location);
25516 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25518 at_import_die = force_type_die (TREE_TYPE (decl));
25519 /* For namespace N { typedef void T; } using N::T; base_type_die
25520 returns NULL, but DW_TAG_imported_declaration requires
25521 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25522 if (!at_import_die)
25524 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25525 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25526 at_import_die = lookup_type_die (TREE_TYPE (decl));
25527 gcc_assert (at_import_die);
25530 else
25532 at_import_die = lookup_decl_die (decl);
25533 if (!at_import_die)
25535 /* If we're trying to avoid duplicate debug info, we may not have
25536 emitted the member decl for this field. Emit it now. */
25537 if (TREE_CODE (decl) == FIELD_DECL)
25539 tree type = DECL_CONTEXT (decl);
25541 if (TYPE_CONTEXT (type)
25542 && TYPE_P (TYPE_CONTEXT (type))
25543 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25544 DINFO_USAGE_DIR_USE))
25545 return;
25546 gen_type_die_for_member (type, decl,
25547 get_context_die (TYPE_CONTEXT (type)));
25549 if (TREE_CODE (decl) == NAMELIST_DECL)
25550 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25551 get_context_die (DECL_CONTEXT (decl)),
25552 NULL_TREE);
25553 else
25554 at_import_die = force_decl_die (decl);
25558 if (TREE_CODE (decl) == NAMESPACE_DECL)
25560 if (dwarf_version >= 3 || !dwarf_strict)
25561 imported_die = new_die (DW_TAG_imported_module,
25562 lexical_block_die,
25563 lexical_block);
25564 else
25565 return;
25567 else
25568 imported_die = new_die (DW_TAG_imported_declaration,
25569 lexical_block_die,
25570 lexical_block);
25572 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25573 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25574 if (debug_column_info && xloc.column)
25575 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25576 if (name)
25577 add_AT_string (imported_die, DW_AT_name,
25578 IDENTIFIER_POINTER (name));
25579 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25582 /* Output debug information for imported module or decl DECL.
25583 NAME is non-NULL name in context if the decl has been renamed.
25584 CHILD is true if decl is one of the renamed decls as part of
25585 importing whole module. */
25587 static void
25588 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25589 bool child)
25591 /* dw_die_ref at_import_die; */
25592 dw_die_ref scope_die;
25594 if (debug_info_level <= DINFO_LEVEL_TERSE)
25595 return;
25597 gcc_assert (decl);
25599 set_early_dwarf s;
25601 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25602 We need decl DIE for reference and scope die. First, get DIE for the decl
25603 itself. */
25605 /* Get the scope die for decl context. Use comp_unit_die for global module
25606 or decl. If die is not found for non globals, force new die. */
25607 if (context
25608 && TYPE_P (context)
25609 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25610 return;
25612 scope_die = get_context_die (context);
25614 if (child)
25616 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25617 there is nothing we can do, here. */
25618 if (dwarf_version < 3 && dwarf_strict)
25619 return;
25621 gcc_assert (scope_die->die_child);
25622 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25623 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25624 scope_die = scope_die->die_child;
25627 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25628 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25631 /* Output debug information for namelists. */
25633 static dw_die_ref
25634 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25636 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25637 tree value;
25638 unsigned i;
25640 if (debug_info_level <= DINFO_LEVEL_TERSE)
25641 return NULL;
25643 gcc_assert (scope_die != NULL);
25644 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25645 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25647 /* If there are no item_decls, we have a nondefining namelist, e.g.
25648 with USE association; hence, set DW_AT_declaration. */
25649 if (item_decls == NULL_TREE)
25651 add_AT_flag (nml_die, DW_AT_declaration, 1);
25652 return nml_die;
25655 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25657 nml_item_ref_die = lookup_decl_die (value);
25658 if (!nml_item_ref_die)
25659 nml_item_ref_die = force_decl_die (value);
25661 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25662 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25664 return nml_die;
25668 /* Write the debugging output for DECL and return the DIE. */
25670 static void
25671 dwarf2out_decl (tree decl)
25673 dw_die_ref context_die = comp_unit_die ();
25675 switch (TREE_CODE (decl))
25677 case ERROR_MARK:
25678 return;
25680 case FUNCTION_DECL:
25681 /* What we would really like to do here is to filter out all mere
25682 file-scope declarations of file-scope functions which are never
25683 referenced later within this translation unit (and keep all of ones
25684 that *are* referenced later on) but we aren't clairvoyant, so we have
25685 no idea which functions will be referenced in the future (i.e. later
25686 on within the current translation unit). So here we just ignore all
25687 file-scope function declarations which are not also definitions. If
25688 and when the debugger needs to know something about these functions,
25689 it will have to hunt around and find the DWARF information associated
25690 with the definition of the function.
25692 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25693 nodes represent definitions and which ones represent mere
25694 declarations. We have to check DECL_INITIAL instead. That's because
25695 the C front-end supports some weird semantics for "extern inline"
25696 function definitions. These can get inlined within the current
25697 translation unit (and thus, we need to generate Dwarf info for their
25698 abstract instances so that the Dwarf info for the concrete inlined
25699 instances can have something to refer to) but the compiler never
25700 generates any out-of-lines instances of such things (despite the fact
25701 that they *are* definitions).
25703 The important point is that the C front-end marks these "extern
25704 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25705 them anyway. Note that the C++ front-end also plays some similar games
25706 for inline function definitions appearing within include files which
25707 also contain `#pragma interface' pragmas.
25709 If we are called from dwarf2out_abstract_function output a DIE
25710 anyway. We can end up here this way with early inlining and LTO
25711 where the inlined function is output in a different LTRANS unit
25712 or not at all. */
25713 if (DECL_INITIAL (decl) == NULL_TREE
25714 && ! DECL_ABSTRACT_P (decl))
25715 return;
25717 /* If we're a nested function, initially use a parent of NULL; if we're
25718 a plain function, this will be fixed up in decls_for_scope. If
25719 we're a method, it will be ignored, since we already have a DIE. */
25720 if (decl_function_context (decl)
25721 /* But if we're in terse mode, we don't care about scope. */
25722 && debug_info_level > DINFO_LEVEL_TERSE)
25723 context_die = NULL;
25724 break;
25726 case VAR_DECL:
25727 /* For local statics lookup proper context die. */
25728 if (local_function_static (decl))
25729 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25731 /* If we are in terse mode, don't generate any DIEs to represent any
25732 variable declarations or definitions. */
25733 if (debug_info_level <= DINFO_LEVEL_TERSE)
25734 return;
25735 break;
25737 case CONST_DECL:
25738 if (debug_info_level <= DINFO_LEVEL_TERSE)
25739 return;
25740 if (!is_fortran () && !is_ada ())
25741 return;
25742 if (TREE_STATIC (decl) && decl_function_context (decl))
25743 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25744 break;
25746 case NAMESPACE_DECL:
25747 case IMPORTED_DECL:
25748 if (debug_info_level <= DINFO_LEVEL_TERSE)
25749 return;
25750 if (lookup_decl_die (decl) != NULL)
25751 return;
25752 break;
25754 case TYPE_DECL:
25755 /* Don't emit stubs for types unless they are needed by other DIEs. */
25756 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25757 return;
25759 /* Don't bother trying to generate any DIEs to represent any of the
25760 normal built-in types for the language we are compiling. */
25761 if (DECL_IS_BUILTIN (decl))
25762 return;
25764 /* If we are in terse mode, don't generate any DIEs for types. */
25765 if (debug_info_level <= DINFO_LEVEL_TERSE)
25766 return;
25768 /* If we're a function-scope tag, initially use a parent of NULL;
25769 this will be fixed up in decls_for_scope. */
25770 if (decl_function_context (decl))
25771 context_die = NULL;
25773 break;
25775 case NAMELIST_DECL:
25776 break;
25778 default:
25779 return;
25782 gen_decl_die (decl, NULL, NULL, context_die);
25784 if (flag_checking)
25786 dw_die_ref die = lookup_decl_die (decl);
25787 if (die)
25788 check_die (die);
25792 /* Write the debugging output for DECL. */
25794 static void
25795 dwarf2out_function_decl (tree decl)
25797 dwarf2out_decl (decl);
25798 call_arg_locations = NULL;
25799 call_arg_loc_last = NULL;
25800 call_site_count = -1;
25801 tail_call_site_count = -1;
25802 decl_loc_table->empty ();
25803 cached_dw_loc_list_table->empty ();
25806 /* Output a marker (i.e. a label) for the beginning of the generated code for
25807 a lexical block. */
25809 static void
25810 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25811 unsigned int blocknum)
25813 switch_to_section (current_function_section ());
25814 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25817 /* Output a marker (i.e. a label) for the end of the generated code for a
25818 lexical block. */
25820 static void
25821 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25823 switch_to_section (current_function_section ());
25824 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25827 /* Returns nonzero if it is appropriate not to emit any debugging
25828 information for BLOCK, because it doesn't contain any instructions.
25830 Don't allow this for blocks with nested functions or local classes
25831 as we would end up with orphans, and in the presence of scheduling
25832 we may end up calling them anyway. */
25834 static bool
25835 dwarf2out_ignore_block (const_tree block)
25837 tree decl;
25838 unsigned int i;
25840 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25841 if (TREE_CODE (decl) == FUNCTION_DECL
25842 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25843 return 0;
25844 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25846 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25847 if (TREE_CODE (decl) == FUNCTION_DECL
25848 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25849 return 0;
25852 return 1;
25855 /* Hash table routines for file_hash. */
25857 bool
25858 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25860 return filename_cmp (p1->filename, p2) == 0;
25863 hashval_t
25864 dwarf_file_hasher::hash (dwarf_file_data *p)
25866 return htab_hash_string (p->filename);
25869 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25870 dwarf2out.c) and return its "index". The index of each (known) filename is
25871 just a unique number which is associated with only that one filename. We
25872 need such numbers for the sake of generating labels (in the .debug_sfnames
25873 section) and references to those files numbers (in the .debug_srcinfo
25874 and .debug_macinfo sections). If the filename given as an argument is not
25875 found in our current list, add it to the list and assign it the next
25876 available unique index number. */
25878 static struct dwarf_file_data *
25879 lookup_filename (const char *file_name)
25881 struct dwarf_file_data * created;
25883 if (!file_name)
25884 return NULL;
25886 dwarf_file_data **slot
25887 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25888 INSERT);
25889 if (*slot)
25890 return *slot;
25892 created = ggc_alloc<dwarf_file_data> ();
25893 created->filename = file_name;
25894 created->emitted_number = 0;
25895 *slot = created;
25896 return created;
25899 /* If the assembler will construct the file table, then translate the compiler
25900 internal file table number into the assembler file table number, and emit
25901 a .file directive if we haven't already emitted one yet. The file table
25902 numbers are different because we prune debug info for unused variables and
25903 types, which may include filenames. */
25905 static int
25906 maybe_emit_file (struct dwarf_file_data * fd)
25908 if (! fd->emitted_number)
25910 if (last_emitted_file)
25911 fd->emitted_number = last_emitted_file->emitted_number + 1;
25912 else
25913 fd->emitted_number = 1;
25914 last_emitted_file = fd;
25916 if (DWARF2_ASM_LINE_DEBUG_INFO)
25918 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25919 output_quoted_string (asm_out_file,
25920 remap_debug_filename (fd->filename));
25921 fputc ('\n', asm_out_file);
25925 return fd->emitted_number;
25928 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25929 That generation should happen after function debug info has been
25930 generated. The value of the attribute is the constant value of ARG. */
25932 static void
25933 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25935 die_arg_entry entry;
25937 if (!die || !arg)
25938 return;
25940 gcc_assert (early_dwarf);
25942 if (!tmpl_value_parm_die_table)
25943 vec_alloc (tmpl_value_parm_die_table, 32);
25945 entry.die = die;
25946 entry.arg = arg;
25947 vec_safe_push (tmpl_value_parm_die_table, entry);
25950 /* Return TRUE if T is an instance of generic type, FALSE
25951 otherwise. */
25953 static bool
25954 generic_type_p (tree t)
25956 if (t == NULL_TREE || !TYPE_P (t))
25957 return false;
25958 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25961 /* Schedule the generation of the generic parameter dies for the
25962 instance of generic type T. The proper generation itself is later
25963 done by gen_scheduled_generic_parms_dies. */
25965 static void
25966 schedule_generic_params_dies_gen (tree t)
25968 if (!generic_type_p (t))
25969 return;
25971 gcc_assert (early_dwarf);
25973 if (!generic_type_instances)
25974 vec_alloc (generic_type_instances, 256);
25976 vec_safe_push (generic_type_instances, t);
25979 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25980 by append_entry_to_tmpl_value_parm_die_table. This function must
25981 be called after function DIEs have been generated. */
25983 static void
25984 gen_remaining_tmpl_value_param_die_attribute (void)
25986 if (tmpl_value_parm_die_table)
25988 unsigned i, j;
25989 die_arg_entry *e;
25991 /* We do this in two phases - first get the cases we can
25992 handle during early-finish, preserving those we cannot
25993 (containing symbolic constants where we don't yet know
25994 whether we are going to output the referenced symbols).
25995 For those we try again at late-finish. */
25996 j = 0;
25997 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25999 if (!tree_add_const_value_attribute (e->die, e->arg))
26001 dw_loc_descr_ref loc = NULL;
26002 if (! early_dwarf
26003 && (dwarf_version >= 5 || !dwarf_strict))
26004 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26005 if (loc)
26006 add_AT_loc (e->die, DW_AT_location, loc);
26007 else
26008 (*tmpl_value_parm_die_table)[j++] = *e;
26011 tmpl_value_parm_die_table->truncate (j);
26015 /* Generate generic parameters DIEs for instances of generic types
26016 that have been previously scheduled by
26017 schedule_generic_params_dies_gen. This function must be called
26018 after all the types of the CU have been laid out. */
26020 static void
26021 gen_scheduled_generic_parms_dies (void)
26023 unsigned i;
26024 tree t;
26026 if (!generic_type_instances)
26027 return;
26029 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26030 if (COMPLETE_TYPE_P (t))
26031 gen_generic_params_dies (t);
26033 generic_type_instances = NULL;
26037 /* Replace DW_AT_name for the decl with name. */
26039 static void
26040 dwarf2out_set_name (tree decl, tree name)
26042 dw_die_ref die;
26043 dw_attr_node *attr;
26044 const char *dname;
26046 die = TYPE_SYMTAB_DIE (decl);
26047 if (!die)
26048 return;
26050 dname = dwarf2_name (name, 0);
26051 if (!dname)
26052 return;
26054 attr = get_AT (die, DW_AT_name);
26055 if (attr)
26057 struct indirect_string_node *node;
26059 node = find_AT_string (dname);
26060 /* replace the string. */
26061 attr->dw_attr_val.v.val_str = node;
26064 else
26065 add_name_attribute (die, dname);
26068 /* True if before or during processing of the first function being emitted. */
26069 static bool in_first_function_p = true;
26070 /* True if loc_note during dwarf2out_var_location call might still be
26071 before first real instruction at address equal to .Ltext0. */
26072 static bool maybe_at_text_label_p = true;
26073 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26074 static unsigned int first_loclabel_num_not_at_text_label;
26076 /* Called by the final INSN scan whenever we see a var location. We
26077 use it to drop labels in the right places, and throw the location in
26078 our lookup table. */
26080 static void
26081 dwarf2out_var_location (rtx_insn *loc_note)
26083 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26084 struct var_loc_node *newloc;
26085 rtx_insn *next_real, *next_note;
26086 rtx_insn *call_insn = NULL;
26087 static const char *last_label;
26088 static const char *last_postcall_label;
26089 static bool last_in_cold_section_p;
26090 static rtx_insn *expected_next_loc_note;
26091 tree decl;
26092 bool var_loc_p;
26094 if (!NOTE_P (loc_note))
26096 if (CALL_P (loc_note))
26098 call_site_count++;
26099 if (SIBLING_CALL_P (loc_note))
26100 tail_call_site_count++;
26101 if (optimize == 0 && !flag_var_tracking)
26103 /* When the var-tracking pass is not running, there is no note
26104 for indirect calls whose target is compile-time known. In this
26105 case, process such calls specifically so that we generate call
26106 sites for them anyway. */
26107 rtx x = PATTERN (loc_note);
26108 if (GET_CODE (x) == PARALLEL)
26109 x = XVECEXP (x, 0, 0);
26110 if (GET_CODE (x) == SET)
26111 x = SET_SRC (x);
26112 if (GET_CODE (x) == CALL)
26113 x = XEXP (x, 0);
26114 if (!MEM_P (x)
26115 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26116 || !SYMBOL_REF_DECL (XEXP (x, 0))
26117 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26118 != FUNCTION_DECL))
26120 call_insn = loc_note;
26121 loc_note = NULL;
26122 var_loc_p = false;
26124 next_real = next_real_insn (call_insn);
26125 next_note = NULL;
26126 cached_next_real_insn = NULL;
26127 goto create_label;
26131 return;
26134 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26135 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26136 return;
26138 /* Optimize processing a large consecutive sequence of location
26139 notes so we don't spend too much time in next_real_insn. If the
26140 next insn is another location note, remember the next_real_insn
26141 calculation for next time. */
26142 next_real = cached_next_real_insn;
26143 if (next_real)
26145 if (expected_next_loc_note != loc_note)
26146 next_real = NULL;
26149 next_note = NEXT_INSN (loc_note);
26150 if (! next_note
26151 || next_note->deleted ()
26152 || ! NOTE_P (next_note)
26153 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26154 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26155 next_note = NULL;
26157 if (! next_real)
26158 next_real = next_real_insn (loc_note);
26160 if (next_note)
26162 expected_next_loc_note = next_note;
26163 cached_next_real_insn = next_real;
26165 else
26166 cached_next_real_insn = NULL;
26168 /* If there are no instructions which would be affected by this note,
26169 don't do anything. */
26170 if (var_loc_p
26171 && next_real == NULL_RTX
26172 && !NOTE_DURING_CALL_P (loc_note))
26173 return;
26175 create_label:
26177 if (next_real == NULL_RTX)
26178 next_real = get_last_insn ();
26180 /* If there were any real insns between note we processed last time
26181 and this note (or if it is the first note), clear
26182 last_{,postcall_}label so that they are not reused this time. */
26183 if (last_var_location_insn == NULL_RTX
26184 || last_var_location_insn != next_real
26185 || last_in_cold_section_p != in_cold_section_p)
26187 last_label = NULL;
26188 last_postcall_label = NULL;
26191 if (var_loc_p)
26193 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26194 newloc = add_var_loc_to_decl (decl, loc_note,
26195 NOTE_DURING_CALL_P (loc_note)
26196 ? last_postcall_label : last_label);
26197 if (newloc == NULL)
26198 return;
26200 else
26202 decl = NULL_TREE;
26203 newloc = NULL;
26206 /* If there were no real insns between note we processed last time
26207 and this note, use the label we emitted last time. Otherwise
26208 create a new label and emit it. */
26209 if (last_label == NULL)
26211 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26212 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26213 loclabel_num++;
26214 last_label = ggc_strdup (loclabel);
26215 /* See if loclabel might be equal to .Ltext0. If yes,
26216 bump first_loclabel_num_not_at_text_label. */
26217 if (!have_multiple_function_sections
26218 && in_first_function_p
26219 && maybe_at_text_label_p)
26221 static rtx_insn *last_start;
26222 rtx_insn *insn;
26223 for (insn = loc_note; insn; insn = previous_insn (insn))
26224 if (insn == last_start)
26225 break;
26226 else if (!NONDEBUG_INSN_P (insn))
26227 continue;
26228 else
26230 rtx body = PATTERN (insn);
26231 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26232 continue;
26233 /* Inline asm could occupy zero bytes. */
26234 else if (GET_CODE (body) == ASM_INPUT
26235 || asm_noperands (body) >= 0)
26236 continue;
26237 #ifdef HAVE_attr_length
26238 else if (get_attr_min_length (insn) == 0)
26239 continue;
26240 #endif
26241 else
26243 /* Assume insn has non-zero length. */
26244 maybe_at_text_label_p = false;
26245 break;
26248 if (maybe_at_text_label_p)
26250 last_start = loc_note;
26251 first_loclabel_num_not_at_text_label = loclabel_num;
26256 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26257 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26259 if (!var_loc_p)
26261 struct call_arg_loc_node *ca_loc
26262 = ggc_cleared_alloc<call_arg_loc_node> ();
26263 rtx_insn *prev
26264 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26266 ca_loc->call_arg_loc_note = loc_note;
26267 ca_loc->next = NULL;
26268 ca_loc->label = last_label;
26269 gcc_assert (prev
26270 && (CALL_P (prev)
26271 || (NONJUMP_INSN_P (prev)
26272 && GET_CODE (PATTERN (prev)) == SEQUENCE
26273 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26274 if (!CALL_P (prev))
26275 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26276 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26278 /* Look for a SYMBOL_REF in the "prev" instruction. */
26279 rtx x = get_call_rtx_from (PATTERN (prev));
26280 if (x)
26282 /* Try to get the call symbol, if any. */
26283 if (MEM_P (XEXP (x, 0)))
26284 x = XEXP (x, 0);
26285 /* First, look for a memory access to a symbol_ref. */
26286 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26287 && SYMBOL_REF_DECL (XEXP (x, 0))
26288 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26289 ca_loc->symbol_ref = XEXP (x, 0);
26290 /* Otherwise, look at a compile-time known user-level function
26291 declaration. */
26292 else if (MEM_P (x)
26293 && MEM_EXPR (x)
26294 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26295 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26298 ca_loc->block = insn_scope (prev);
26299 if (call_arg_locations)
26300 call_arg_loc_last->next = ca_loc;
26301 else
26302 call_arg_locations = ca_loc;
26303 call_arg_loc_last = ca_loc;
26305 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26306 newloc->label = last_label;
26307 else
26309 if (!last_postcall_label)
26311 sprintf (loclabel, "%s-1", last_label);
26312 last_postcall_label = ggc_strdup (loclabel);
26314 newloc->label = last_postcall_label;
26317 last_var_location_insn = next_real;
26318 last_in_cold_section_p = in_cold_section_p;
26321 /* Called from finalize_size_functions for size functions so that their body
26322 can be encoded in the debug info to describe the layout of variable-length
26323 structures. */
26325 static void
26326 dwarf2out_size_function (tree decl)
26328 function_to_dwarf_procedure (decl);
26331 /* Note in one location list that text section has changed. */
26334 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26336 var_loc_list *list = *slot;
26337 if (list->first)
26338 list->last_before_switch
26339 = list->last->next ? list->last->next : list->last;
26340 return 1;
26343 /* Note in all location lists that text section has changed. */
26345 static void
26346 var_location_switch_text_section (void)
26348 if (decl_loc_table == NULL)
26349 return;
26351 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26354 /* Create a new line number table. */
26356 static dw_line_info_table *
26357 new_line_info_table (void)
26359 dw_line_info_table *table;
26361 table = ggc_cleared_alloc<dw_line_info_table> ();
26362 table->file_num = 1;
26363 table->line_num = 1;
26364 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26366 return table;
26369 /* Lookup the "current" table into which we emit line info, so
26370 that we don't have to do it for every source line. */
26372 static void
26373 set_cur_line_info_table (section *sec)
26375 dw_line_info_table *table;
26377 if (sec == text_section)
26378 table = text_section_line_info;
26379 else if (sec == cold_text_section)
26381 table = cold_text_section_line_info;
26382 if (!table)
26384 cold_text_section_line_info = table = new_line_info_table ();
26385 table->end_label = cold_end_label;
26388 else
26390 const char *end_label;
26392 if (flag_reorder_blocks_and_partition)
26394 if (in_cold_section_p)
26395 end_label = crtl->subsections.cold_section_end_label;
26396 else
26397 end_label = crtl->subsections.hot_section_end_label;
26399 else
26401 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26402 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26403 current_function_funcdef_no);
26404 end_label = ggc_strdup (label);
26407 table = new_line_info_table ();
26408 table->end_label = end_label;
26410 vec_safe_push (separate_line_info, table);
26413 if (DWARF2_ASM_LINE_DEBUG_INFO)
26414 table->is_stmt = (cur_line_info_table
26415 ? cur_line_info_table->is_stmt
26416 : DWARF_LINE_DEFAULT_IS_STMT_START);
26417 cur_line_info_table = table;
26421 /* We need to reset the locations at the beginning of each
26422 function. We can't do this in the end_function hook, because the
26423 declarations that use the locations won't have been output when
26424 that hook is called. Also compute have_multiple_function_sections here. */
26426 static void
26427 dwarf2out_begin_function (tree fun)
26429 section *sec = function_section (fun);
26431 if (sec != text_section)
26432 have_multiple_function_sections = true;
26434 if (flag_reorder_blocks_and_partition && !cold_text_section)
26436 gcc_assert (current_function_decl == fun);
26437 cold_text_section = unlikely_text_section ();
26438 switch_to_section (cold_text_section);
26439 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26440 switch_to_section (sec);
26443 dwarf2out_note_section_used ();
26444 call_site_count = 0;
26445 tail_call_site_count = 0;
26447 set_cur_line_info_table (sec);
26450 /* Helper function of dwarf2out_end_function, called only after emitting
26451 the very first function into assembly. Check if some .debug_loc range
26452 might end with a .LVL* label that could be equal to .Ltext0.
26453 In that case we must force using absolute addresses in .debug_loc ranges,
26454 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26455 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26456 list terminator.
26457 Set have_multiple_function_sections to true in that case and
26458 terminate htab traversal. */
26461 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26463 var_loc_list *entry = *slot;
26464 struct var_loc_node *node;
26466 node = entry->first;
26467 if (node && node->next && node->next->label)
26469 unsigned int i;
26470 const char *label = node->next->label;
26471 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26473 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26475 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26476 if (strcmp (label, loclabel) == 0)
26478 have_multiple_function_sections = true;
26479 return 0;
26483 return 1;
26486 /* Hook called after emitting a function into assembly.
26487 This does something only for the very first function emitted. */
26489 static void
26490 dwarf2out_end_function (unsigned int)
26492 if (in_first_function_p
26493 && !have_multiple_function_sections
26494 && first_loclabel_num_not_at_text_label
26495 && decl_loc_table)
26496 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26497 in_first_function_p = false;
26498 maybe_at_text_label_p = false;
26501 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26502 front-ends register a translation unit even before dwarf2out_init is
26503 called. */
26504 static tree main_translation_unit = NULL_TREE;
26506 /* Hook called by front-ends after they built their main translation unit.
26507 Associate comp_unit_die to UNIT. */
26509 static void
26510 dwarf2out_register_main_translation_unit (tree unit)
26512 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26513 && main_translation_unit == NULL_TREE);
26514 main_translation_unit = unit;
26515 /* If dwarf2out_init has not been called yet, it will perform the association
26516 itself looking at main_translation_unit. */
26517 if (decl_die_table != NULL)
26518 equate_decl_number_to_die (unit, comp_unit_die ());
26521 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26523 static void
26524 push_dw_line_info_entry (dw_line_info_table *table,
26525 enum dw_line_info_opcode opcode, unsigned int val)
26527 dw_line_info_entry e;
26528 e.opcode = opcode;
26529 e.val = val;
26530 vec_safe_push (table->entries, e);
26533 /* Output a label to mark the beginning of a source code line entry
26534 and record information relating to this source line, in
26535 'line_info_table' for later output of the .debug_line section. */
26536 /* ??? The discriminator parameter ought to be unsigned. */
26538 static void
26539 dwarf2out_source_line (unsigned int line, unsigned int column,
26540 const char *filename,
26541 int discriminator, bool is_stmt)
26543 unsigned int file_num;
26544 dw_line_info_table *table;
26546 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26547 return;
26549 /* The discriminator column was added in dwarf4. Simplify the below
26550 by simply removing it if we're not supposed to output it. */
26551 if (dwarf_version < 4 && dwarf_strict)
26552 discriminator = 0;
26554 if (!debug_column_info)
26555 column = 0;
26557 table = cur_line_info_table;
26558 file_num = maybe_emit_file (lookup_filename (filename));
26560 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26561 the debugger has used the second (possibly duplicate) line number
26562 at the beginning of the function to mark the end of the prologue.
26563 We could eliminate any other duplicates within the function. For
26564 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26565 that second line number entry. */
26566 /* Recall that this end-of-prologue indication is *not* the same thing
26567 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26568 to which the hook corresponds, follows the last insn that was
26569 emitted by gen_prologue. What we need is to precede the first insn
26570 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26571 insn that corresponds to something the user wrote. These may be
26572 very different locations once scheduling is enabled. */
26574 if (0 && file_num == table->file_num
26575 && line == table->line_num
26576 && column == table->column_num
26577 && discriminator == table->discrim_num
26578 && is_stmt == table->is_stmt)
26579 return;
26581 switch_to_section (current_function_section ());
26583 /* If requested, emit something human-readable. */
26584 if (flag_debug_asm)
26586 if (debug_column_info)
26587 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26588 filename, line, column);
26589 else
26590 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26591 filename, line);
26594 if (DWARF2_ASM_LINE_DEBUG_INFO)
26596 /* Emit the .loc directive understood by GNU as. */
26597 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26598 file_num, line, is_stmt, discriminator */
26599 fputs ("\t.loc ", asm_out_file);
26600 fprint_ul (asm_out_file, file_num);
26601 putc (' ', asm_out_file);
26602 fprint_ul (asm_out_file, line);
26603 putc (' ', asm_out_file);
26604 if (debug_column_info)
26605 fprint_ul (asm_out_file, column);
26606 else
26607 putc ('0', asm_out_file);
26609 if (is_stmt != table->is_stmt)
26611 fputs (" is_stmt ", asm_out_file);
26612 putc (is_stmt ? '1' : '0', asm_out_file);
26614 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26616 gcc_assert (discriminator > 0);
26617 fputs (" discriminator ", asm_out_file);
26618 fprint_ul (asm_out_file, (unsigned long) discriminator);
26620 putc ('\n', asm_out_file);
26622 else
26624 unsigned int label_num = ++line_info_label_num;
26626 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26628 push_dw_line_info_entry (table, LI_set_address, label_num);
26629 if (file_num != table->file_num)
26630 push_dw_line_info_entry (table, LI_set_file, file_num);
26631 if (discriminator != table->discrim_num)
26632 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26633 if (is_stmt != table->is_stmt)
26634 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26635 push_dw_line_info_entry (table, LI_set_line, line);
26636 if (debug_column_info)
26637 push_dw_line_info_entry (table, LI_set_column, column);
26640 table->file_num = file_num;
26641 table->line_num = line;
26642 table->column_num = column;
26643 table->discrim_num = discriminator;
26644 table->is_stmt = is_stmt;
26645 table->in_use = true;
26648 /* Record the beginning of a new source file. */
26650 static void
26651 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26653 if (flag_eliminate_dwarf2_dups)
26655 /* Record the beginning of the file for break_out_includes. */
26656 dw_die_ref bincl_die;
26658 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26659 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26662 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26664 macinfo_entry e;
26665 e.code = DW_MACINFO_start_file;
26666 e.lineno = lineno;
26667 e.info = ggc_strdup (filename);
26668 vec_safe_push (macinfo_table, e);
26672 /* Record the end of a source file. */
26674 static void
26675 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26677 if (flag_eliminate_dwarf2_dups)
26678 /* Record the end of the file for break_out_includes. */
26679 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26681 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26683 macinfo_entry e;
26684 e.code = DW_MACINFO_end_file;
26685 e.lineno = lineno;
26686 e.info = NULL;
26687 vec_safe_push (macinfo_table, e);
26691 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26692 the tail part of the directive line, i.e. the part which is past the
26693 initial whitespace, #, whitespace, directive-name, whitespace part. */
26695 static void
26696 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26697 const char *buffer ATTRIBUTE_UNUSED)
26699 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26701 macinfo_entry e;
26702 /* Insert a dummy first entry to be able to optimize the whole
26703 predefined macro block using DW_MACRO_import. */
26704 if (macinfo_table->is_empty () && lineno <= 1)
26706 e.code = 0;
26707 e.lineno = 0;
26708 e.info = NULL;
26709 vec_safe_push (macinfo_table, e);
26711 e.code = DW_MACINFO_define;
26712 e.lineno = lineno;
26713 e.info = ggc_strdup (buffer);
26714 vec_safe_push (macinfo_table, e);
26718 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26719 the tail part of the directive line, i.e. the part which is past the
26720 initial whitespace, #, whitespace, directive-name, whitespace part. */
26722 static void
26723 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26724 const char *buffer ATTRIBUTE_UNUSED)
26726 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26728 macinfo_entry e;
26729 /* Insert a dummy first entry to be able to optimize the whole
26730 predefined macro block using DW_MACRO_import. */
26731 if (macinfo_table->is_empty () && lineno <= 1)
26733 e.code = 0;
26734 e.lineno = 0;
26735 e.info = NULL;
26736 vec_safe_push (macinfo_table, e);
26738 e.code = DW_MACINFO_undef;
26739 e.lineno = lineno;
26740 e.info = ggc_strdup (buffer);
26741 vec_safe_push (macinfo_table, e);
26745 /* Helpers to manipulate hash table of CUs. */
26747 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26749 static inline hashval_t hash (const macinfo_entry *);
26750 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26753 inline hashval_t
26754 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26756 return htab_hash_string (entry->info);
26759 inline bool
26760 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26761 const macinfo_entry *entry2)
26763 return !strcmp (entry1->info, entry2->info);
26766 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26768 /* Output a single .debug_macinfo entry. */
26770 static void
26771 output_macinfo_op (macinfo_entry *ref)
26773 int file_num;
26774 size_t len;
26775 struct indirect_string_node *node;
26776 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26777 struct dwarf_file_data *fd;
26779 switch (ref->code)
26781 case DW_MACINFO_start_file:
26782 fd = lookup_filename (ref->info);
26783 file_num = maybe_emit_file (fd);
26784 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26785 dw2_asm_output_data_uleb128 (ref->lineno,
26786 "Included from line number %lu",
26787 (unsigned long) ref->lineno);
26788 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26789 break;
26790 case DW_MACINFO_end_file:
26791 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26792 break;
26793 case DW_MACINFO_define:
26794 case DW_MACINFO_undef:
26795 len = strlen (ref->info) + 1;
26796 if (!dwarf_strict
26797 && len > DWARF_OFFSET_SIZE
26798 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26799 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26801 ref->code = ref->code == DW_MACINFO_define
26802 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26803 output_macinfo_op (ref);
26804 return;
26806 dw2_asm_output_data (1, ref->code,
26807 ref->code == DW_MACINFO_define
26808 ? "Define macro" : "Undefine macro");
26809 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26810 (unsigned long) ref->lineno);
26811 dw2_asm_output_nstring (ref->info, -1, "The macro");
26812 break;
26813 case DW_MACRO_define_strp:
26814 case DW_MACRO_undef_strp:
26815 node = find_AT_string (ref->info);
26816 gcc_assert (node
26817 && (node->form == DW_FORM_strp
26818 || node->form == DW_FORM_GNU_str_index));
26819 dw2_asm_output_data (1, ref->code,
26820 ref->code == DW_MACRO_define_strp
26821 ? "Define macro strp"
26822 : "Undefine macro strp");
26823 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26824 (unsigned long) ref->lineno);
26825 if (node->form == DW_FORM_strp)
26826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26827 debug_str_section, "The macro: \"%s\"",
26828 ref->info);
26829 else
26830 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26831 ref->info);
26832 break;
26833 case DW_MACRO_import:
26834 dw2_asm_output_data (1, ref->code, "Import");
26835 ASM_GENERATE_INTERNAL_LABEL (label,
26836 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26837 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26838 break;
26839 default:
26840 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26841 ASM_COMMENT_START, (unsigned long) ref->code);
26842 break;
26846 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26847 other compilation unit .debug_macinfo sections. IDX is the first
26848 index of a define/undef, return the number of ops that should be
26849 emitted in a comdat .debug_macinfo section and emit
26850 a DW_MACRO_import entry referencing it.
26851 If the define/undef entry should be emitted normally, return 0. */
26853 static unsigned
26854 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26855 macinfo_hash_type **macinfo_htab)
26857 macinfo_entry *first, *second, *cur, *inc;
26858 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26859 unsigned char checksum[16];
26860 struct md5_ctx ctx;
26861 char *grp_name, *tail;
26862 const char *base;
26863 unsigned int i, count, encoded_filename_len, linebuf_len;
26864 macinfo_entry **slot;
26866 first = &(*macinfo_table)[idx];
26867 second = &(*macinfo_table)[idx + 1];
26869 /* Optimize only if there are at least two consecutive define/undef ops,
26870 and either all of them are before first DW_MACINFO_start_file
26871 with lineno {0,1} (i.e. predefined macro block), or all of them are
26872 in some included header file. */
26873 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26874 return 0;
26875 if (vec_safe_is_empty (files))
26877 if (first->lineno > 1 || second->lineno > 1)
26878 return 0;
26880 else if (first->lineno == 0)
26881 return 0;
26883 /* Find the last define/undef entry that can be grouped together
26884 with first and at the same time compute md5 checksum of their
26885 codes, linenumbers and strings. */
26886 md5_init_ctx (&ctx);
26887 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26888 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26889 break;
26890 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26891 break;
26892 else
26894 unsigned char code = cur->code;
26895 md5_process_bytes (&code, 1, &ctx);
26896 checksum_uleb128 (cur->lineno, &ctx);
26897 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26899 md5_finish_ctx (&ctx, checksum);
26900 count = i - idx;
26902 /* From the containing include filename (if any) pick up just
26903 usable characters from its basename. */
26904 if (vec_safe_is_empty (files))
26905 base = "";
26906 else
26907 base = lbasename (files->last ().info);
26908 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26909 if (ISIDNUM (base[i]) || base[i] == '.')
26910 encoded_filename_len++;
26911 /* Count . at the end. */
26912 if (encoded_filename_len)
26913 encoded_filename_len++;
26915 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26916 linebuf_len = strlen (linebuf);
26918 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26919 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26920 + 16 * 2 + 1);
26921 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26922 tail = grp_name + 4;
26923 if (encoded_filename_len)
26925 for (i = 0; base[i]; i++)
26926 if (ISIDNUM (base[i]) || base[i] == '.')
26927 *tail++ = base[i];
26928 *tail++ = '.';
26930 memcpy (tail, linebuf, linebuf_len);
26931 tail += linebuf_len;
26932 *tail++ = '.';
26933 for (i = 0; i < 16; i++)
26934 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26936 /* Construct a macinfo_entry for DW_MACRO_import
26937 in the empty vector entry before the first define/undef. */
26938 inc = &(*macinfo_table)[idx - 1];
26939 inc->code = DW_MACRO_import;
26940 inc->lineno = 0;
26941 inc->info = ggc_strdup (grp_name);
26942 if (!*macinfo_htab)
26943 *macinfo_htab = new macinfo_hash_type (10);
26944 /* Avoid emitting duplicates. */
26945 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26946 if (*slot != NULL)
26948 inc->code = 0;
26949 inc->info = NULL;
26950 /* If such an entry has been used before, just emit
26951 a DW_MACRO_import op. */
26952 inc = *slot;
26953 output_macinfo_op (inc);
26954 /* And clear all macinfo_entry in the range to avoid emitting them
26955 in the second pass. */
26956 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26958 cur->code = 0;
26959 cur->info = NULL;
26962 else
26964 *slot = inc;
26965 inc->lineno = (*macinfo_htab)->elements ();
26966 output_macinfo_op (inc);
26968 return count;
26971 /* Save any strings needed by the macinfo table in the debug str
26972 table. All strings must be collected into the table by the time
26973 index_string is called. */
26975 static void
26976 save_macinfo_strings (void)
26978 unsigned len;
26979 unsigned i;
26980 macinfo_entry *ref;
26982 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26984 switch (ref->code)
26986 /* Match the logic in output_macinfo_op to decide on
26987 indirect strings. */
26988 case DW_MACINFO_define:
26989 case DW_MACINFO_undef:
26990 len = strlen (ref->info) + 1;
26991 if (!dwarf_strict
26992 && len > DWARF_OFFSET_SIZE
26993 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26994 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26995 set_indirect_string (find_AT_string (ref->info));
26996 break;
26997 case DW_MACRO_define_strp:
26998 case DW_MACRO_undef_strp:
26999 set_indirect_string (find_AT_string (ref->info));
27000 break;
27001 default:
27002 break;
27007 /* Output macinfo section(s). */
27009 static void
27010 output_macinfo (void)
27012 unsigned i;
27013 unsigned long length = vec_safe_length (macinfo_table);
27014 macinfo_entry *ref;
27015 vec<macinfo_entry, va_gc> *files = NULL;
27016 macinfo_hash_type *macinfo_htab = NULL;
27018 if (! length)
27019 return;
27021 /* output_macinfo* uses these interchangeably. */
27022 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27023 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27024 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27025 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27027 /* For .debug_macro emit the section header. */
27028 if (!dwarf_strict || dwarf_version >= 5)
27030 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27031 "DWARF macro version number");
27032 if (DWARF_OFFSET_SIZE == 8)
27033 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27034 else
27035 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27036 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27037 (!dwarf_split_debug_info ? debug_line_section_label
27038 : debug_skeleton_line_section_label),
27039 debug_line_section, NULL);
27042 /* In the first loop, it emits the primary .debug_macinfo section
27043 and after each emitted op the macinfo_entry is cleared.
27044 If a longer range of define/undef ops can be optimized using
27045 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27046 the vector before the first define/undef in the range and the
27047 whole range of define/undef ops is not emitted and kept. */
27048 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27050 switch (ref->code)
27052 case DW_MACINFO_start_file:
27053 vec_safe_push (files, *ref);
27054 break;
27055 case DW_MACINFO_end_file:
27056 if (!vec_safe_is_empty (files))
27057 files->pop ();
27058 break;
27059 case DW_MACINFO_define:
27060 case DW_MACINFO_undef:
27061 if ((!dwarf_strict || dwarf_version >= 5)
27062 && HAVE_COMDAT_GROUP
27063 && vec_safe_length (files) != 1
27064 && i > 0
27065 && i + 1 < length
27066 && (*macinfo_table)[i - 1].code == 0)
27068 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27069 if (count)
27071 i += count - 1;
27072 continue;
27075 break;
27076 case 0:
27077 /* A dummy entry may be inserted at the beginning to be able
27078 to optimize the whole block of predefined macros. */
27079 if (i == 0)
27080 continue;
27081 default:
27082 break;
27084 output_macinfo_op (ref);
27085 ref->info = NULL;
27086 ref->code = 0;
27089 if (!macinfo_htab)
27090 return;
27092 delete macinfo_htab;
27093 macinfo_htab = NULL;
27095 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27096 terminate the current chain and switch to a new comdat .debug_macinfo
27097 section and emit the define/undef entries within it. */
27098 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27099 switch (ref->code)
27101 case 0:
27102 continue;
27103 case DW_MACRO_import:
27105 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27106 tree comdat_key = get_identifier (ref->info);
27107 /* Terminate the previous .debug_macinfo section. */
27108 dw2_asm_output_data (1, 0, "End compilation unit");
27109 targetm.asm_out.named_section (debug_macinfo_section_name,
27110 SECTION_DEBUG
27111 | SECTION_LINKONCE,
27112 comdat_key);
27113 ASM_GENERATE_INTERNAL_LABEL (label,
27114 DEBUG_MACRO_SECTION_LABEL,
27115 ref->lineno);
27116 ASM_OUTPUT_LABEL (asm_out_file, label);
27117 ref->code = 0;
27118 ref->info = NULL;
27119 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27120 "DWARF macro version number");
27121 if (DWARF_OFFSET_SIZE == 8)
27122 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27123 else
27124 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27126 break;
27127 case DW_MACINFO_define:
27128 case DW_MACINFO_undef:
27129 output_macinfo_op (ref);
27130 ref->code = 0;
27131 ref->info = NULL;
27132 break;
27133 default:
27134 gcc_unreachable ();
27138 /* Initialize the various sections and labels for dwarf output. */
27140 static void
27141 init_sections_and_labels (void)
27143 if (!dwarf_split_debug_info)
27145 debug_info_section = get_section (DEBUG_INFO_SECTION,
27146 SECTION_DEBUG, NULL);
27147 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27148 SECTION_DEBUG, NULL);
27149 debug_loc_section = get_section (dwarf_version >= 5
27150 ? DEBUG_LOCLISTS_SECTION
27151 : DEBUG_LOC_SECTION,
27152 SECTION_DEBUG, NULL);
27153 debug_macinfo_section_name
27154 = (dwarf_strict && dwarf_version < 5)
27155 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27156 debug_macinfo_section = get_section (debug_macinfo_section_name,
27157 SECTION_DEBUG, NULL);
27159 else
27161 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27162 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27163 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27164 SECTION_DEBUG | SECTION_EXCLUDE,
27165 NULL);
27166 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27167 SECTION_DEBUG, NULL);
27168 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27169 SECTION_DEBUG, NULL);
27170 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27171 SECTION_DEBUG, NULL);
27172 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27173 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27175 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27176 the main .o, but the skeleton_line goes into the split off dwo. */
27177 debug_skeleton_line_section
27178 = get_section (DEBUG_DWO_LINE_SECTION,
27179 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27180 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27181 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27182 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27183 SECTION_DEBUG | SECTION_EXCLUDE,
27184 NULL);
27185 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27186 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27187 debug_loc_section = get_section (dwarf_version >= 5
27188 ? DEBUG_DWO_LOCLISTS_SECTION
27189 : DEBUG_DWO_LOC_SECTION,
27190 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27191 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27192 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27193 debug_macinfo_section_name
27194 = (dwarf_strict && dwarf_version < 5)
27195 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27196 debug_macinfo_section = get_section (debug_macinfo_section_name,
27197 SECTION_DEBUG | SECTION_EXCLUDE,
27198 NULL);
27200 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27201 SECTION_DEBUG, NULL);
27202 debug_line_section = get_section (DEBUG_LINE_SECTION,
27203 SECTION_DEBUG, NULL);
27204 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27205 SECTION_DEBUG, NULL);
27206 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27207 SECTION_DEBUG, NULL);
27208 debug_str_section = get_section (DEBUG_STR_SECTION,
27209 DEBUG_STR_SECTION_FLAGS, NULL);
27210 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27211 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27212 DEBUG_STR_SECTION_FLAGS, NULL);
27214 debug_ranges_section = get_section (dwarf_version >= 5
27215 ? DEBUG_RNGLISTS_SECTION
27216 : DEBUG_RANGES_SECTION,
27217 SECTION_DEBUG, NULL);
27218 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27219 SECTION_DEBUG, NULL);
27221 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27222 DEBUG_ABBREV_SECTION_LABEL, 0);
27223 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27224 DEBUG_INFO_SECTION_LABEL, 0);
27225 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27226 DEBUG_LINE_SECTION_LABEL, 0);
27227 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27228 DEBUG_RANGES_SECTION_LABEL, 0);
27229 if (dwarf_version >= 5 && dwarf_split_debug_info)
27230 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27231 DEBUG_RANGES_SECTION_LABEL, 1);
27232 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27233 DEBUG_ADDR_SECTION_LABEL, 0);
27234 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27235 (dwarf_strict && dwarf_version < 5)
27236 ? DEBUG_MACINFO_SECTION_LABEL
27237 : DEBUG_MACRO_SECTION_LABEL, 0);
27238 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27241 /* Set up for Dwarf output at the start of compilation. */
27243 static void
27244 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27246 /* This option is currently broken, see (PR53118 and PR46102). */
27247 if (flag_eliminate_dwarf2_dups
27248 && strstr (lang_hooks.name, "C++"))
27250 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27251 flag_eliminate_dwarf2_dups = 0;
27254 /* Allocate the file_table. */
27255 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27257 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27258 /* Allocate the decl_die_table. */
27259 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27261 /* Allocate the decl_loc_table. */
27262 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27264 /* Allocate the cached_dw_loc_list_table. */
27265 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27267 /* Allocate the initial hunk of the decl_scope_table. */
27268 vec_alloc (decl_scope_table, 256);
27270 /* Allocate the initial hunk of the abbrev_die_table. */
27271 vec_alloc (abbrev_die_table, 256);
27272 /* Zero-th entry is allocated, but unused. */
27273 abbrev_die_table->quick_push (NULL);
27275 /* Allocate the dwarf_proc_stack_usage_map. */
27276 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27278 /* Allocate the pubtypes and pubnames vectors. */
27279 vec_alloc (pubname_table, 32);
27280 vec_alloc (pubtype_table, 32);
27282 vec_alloc (incomplete_types, 64);
27284 vec_alloc (used_rtx_array, 32);
27286 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27287 vec_alloc (macinfo_table, 64);
27288 #endif
27290 /* If front-ends already registered a main translation unit but we were not
27291 ready to perform the association, do this now. */
27292 if (main_translation_unit != NULL_TREE)
27293 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27296 /* Called before compile () starts outputtting functions, variables
27297 and toplevel asms into assembly. */
27299 static void
27300 dwarf2out_assembly_start (void)
27302 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27303 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27304 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27305 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27306 COLD_TEXT_SECTION_LABEL, 0);
27307 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27309 switch_to_section (text_section);
27310 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27311 #endif
27313 /* Make sure the line number table for .text always exists. */
27314 text_section_line_info = new_line_info_table ();
27315 text_section_line_info->end_label = text_end_label;
27317 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27318 cur_line_info_table = text_section_line_info;
27319 #endif
27321 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27322 && dwarf2out_do_cfi_asm ()
27323 && (!(flag_unwind_tables || flag_exceptions)
27324 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27325 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27328 /* A helper function for dwarf2out_finish called through
27329 htab_traverse. Assign a string its index. All strings must be
27330 collected into the table by the time index_string is called,
27331 because the indexing code relies on htab_traverse to traverse nodes
27332 in the same order for each run. */
27335 index_string (indirect_string_node **h, unsigned int *index)
27337 indirect_string_node *node = *h;
27339 find_string_form (node);
27340 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27342 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27343 node->index = *index;
27344 *index += 1;
27346 return 1;
27349 /* A helper function for output_indirect_strings called through
27350 htab_traverse. Output the offset to a string and update the
27351 current offset. */
27354 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27356 indirect_string_node *node = *h;
27358 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27360 /* Assert that this node has been assigned an index. */
27361 gcc_assert (node->index != NO_INDEX_ASSIGNED
27362 && node->index != NOT_INDEXED);
27363 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27364 "indexed string 0x%x: %s", node->index, node->str);
27365 *offset += strlen (node->str) + 1;
27367 return 1;
27370 /* A helper function for dwarf2out_finish called through
27371 htab_traverse. Output the indexed string. */
27374 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27376 struct indirect_string_node *node = *h;
27378 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27380 /* Assert that the strings are output in the same order as their
27381 indexes were assigned. */
27382 gcc_assert (*cur_idx == node->index);
27383 assemble_string (node->str, strlen (node->str) + 1);
27384 *cur_idx += 1;
27386 return 1;
27389 /* A helper function for dwarf2out_finish called through
27390 htab_traverse. Emit one queued .debug_str string. */
27393 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27395 struct indirect_string_node *node = *h;
27397 node->form = find_string_form (node);
27398 if (node->form == form && node->refcount > 0)
27400 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27401 assemble_string (node->str, strlen (node->str) + 1);
27404 return 1;
27407 /* Output the indexed string table. */
27409 static void
27410 output_indirect_strings (void)
27412 switch_to_section (debug_str_section);
27413 if (!dwarf_split_debug_info)
27414 debug_str_hash->traverse<enum dwarf_form,
27415 output_indirect_string> (DW_FORM_strp);
27416 else
27418 unsigned int offset = 0;
27419 unsigned int cur_idx = 0;
27421 skeleton_debug_str_hash->traverse<enum dwarf_form,
27422 output_indirect_string> (DW_FORM_strp);
27424 switch_to_section (debug_str_offsets_section);
27425 debug_str_hash->traverse_noresize
27426 <unsigned int *, output_index_string_offset> (&offset);
27427 switch_to_section (debug_str_dwo_section);
27428 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27429 (&cur_idx);
27433 /* Callback for htab_traverse to assign an index to an entry in the
27434 table, and to write that entry to the .debug_addr section. */
27437 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27439 addr_table_entry *entry = *slot;
27441 if (entry->refcount == 0)
27443 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27444 || entry->index == NOT_INDEXED);
27445 return 1;
27448 gcc_assert (entry->index == *cur_index);
27449 (*cur_index)++;
27451 switch (entry->kind)
27453 case ate_kind_rtx:
27454 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27455 "0x%x", entry->index);
27456 break;
27457 case ate_kind_rtx_dtprel:
27458 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27459 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27460 DWARF2_ADDR_SIZE,
27461 entry->addr.rtl);
27462 fputc ('\n', asm_out_file);
27463 break;
27464 case ate_kind_label:
27465 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27466 "0x%x", entry->index);
27467 break;
27468 default:
27469 gcc_unreachable ();
27471 return 1;
27474 /* Produce the .debug_addr section. */
27476 static void
27477 output_addr_table (void)
27479 unsigned int index = 0;
27480 if (addr_index_table == NULL || addr_index_table->size () == 0)
27481 return;
27483 switch_to_section (debug_addr_section);
27484 addr_index_table
27485 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27488 #if ENABLE_ASSERT_CHECKING
27489 /* Verify that all marks are clear. */
27491 static void
27492 verify_marks_clear (dw_die_ref die)
27494 dw_die_ref c;
27496 gcc_assert (! die->die_mark);
27497 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27499 #endif /* ENABLE_ASSERT_CHECKING */
27501 /* Clear the marks for a die and its children.
27502 Be cool if the mark isn't set. */
27504 static void
27505 prune_unmark_dies (dw_die_ref die)
27507 dw_die_ref c;
27509 if (die->die_mark)
27510 die->die_mark = 0;
27511 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27514 /* Given LOC that is referenced by a DIE we're marking as used, find all
27515 referenced DWARF procedures it references and mark them as used. */
27517 static void
27518 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27520 for (; loc != NULL; loc = loc->dw_loc_next)
27521 switch (loc->dw_loc_opc)
27523 case DW_OP_implicit_pointer:
27524 case DW_OP_convert:
27525 case DW_OP_reinterpret:
27526 case DW_OP_GNU_implicit_pointer:
27527 case DW_OP_GNU_convert:
27528 case DW_OP_GNU_reinterpret:
27529 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27530 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27531 break;
27532 case DW_OP_call2:
27533 case DW_OP_call4:
27534 case DW_OP_call_ref:
27535 case DW_OP_const_type:
27536 case DW_OP_GNU_const_type:
27537 case DW_OP_GNU_parameter_ref:
27538 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27539 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27540 break;
27541 case DW_OP_regval_type:
27542 case DW_OP_deref_type:
27543 case DW_OP_GNU_regval_type:
27544 case DW_OP_GNU_deref_type:
27545 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27546 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27547 break;
27548 case DW_OP_entry_value:
27549 case DW_OP_GNU_entry_value:
27550 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27551 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27552 break;
27553 default:
27554 break;
27558 /* Given DIE that we're marking as used, find any other dies
27559 it references as attributes and mark them as used. */
27561 static void
27562 prune_unused_types_walk_attribs (dw_die_ref die)
27564 dw_attr_node *a;
27565 unsigned ix;
27567 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27569 switch (AT_class (a))
27571 /* Make sure DWARF procedures referenced by location descriptions will
27572 get emitted. */
27573 case dw_val_class_loc:
27574 prune_unused_types_walk_loc_descr (AT_loc (a));
27575 break;
27576 case dw_val_class_loc_list:
27577 for (dw_loc_list_ref list = AT_loc_list (a);
27578 list != NULL;
27579 list = list->dw_loc_next)
27580 prune_unused_types_walk_loc_descr (list->expr);
27581 break;
27583 case dw_val_class_die_ref:
27584 /* A reference to another DIE.
27585 Make sure that it will get emitted.
27586 If it was broken out into a comdat group, don't follow it. */
27587 if (! AT_ref (a)->comdat_type_p
27588 || a->dw_attr == DW_AT_specification)
27589 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27590 break;
27592 case dw_val_class_str:
27593 /* Set the string's refcount to 0 so that prune_unused_types_mark
27594 accounts properly for it. */
27595 a->dw_attr_val.v.val_str->refcount = 0;
27596 break;
27598 default:
27599 break;
27604 /* Mark the generic parameters and arguments children DIEs of DIE. */
27606 static void
27607 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27609 dw_die_ref c;
27611 if (die == NULL || die->die_child == NULL)
27612 return;
27613 c = die->die_child;
27616 if (is_template_parameter (c))
27617 prune_unused_types_mark (c, 1);
27618 c = c->die_sib;
27619 } while (c && c != die->die_child);
27622 /* Mark DIE as being used. If DOKIDS is true, then walk down
27623 to DIE's children. */
27625 static void
27626 prune_unused_types_mark (dw_die_ref die, int dokids)
27628 dw_die_ref c;
27630 if (die->die_mark == 0)
27632 /* We haven't done this node yet. Mark it as used. */
27633 die->die_mark = 1;
27634 /* If this is the DIE of a generic type instantiation,
27635 mark the children DIEs that describe its generic parms and
27636 args. */
27637 prune_unused_types_mark_generic_parms_dies (die);
27639 /* We also have to mark its parents as used.
27640 (But we don't want to mark our parent's kids due to this,
27641 unless it is a class.) */
27642 if (die->die_parent)
27643 prune_unused_types_mark (die->die_parent,
27644 class_scope_p (die->die_parent));
27646 /* Mark any referenced nodes. */
27647 prune_unused_types_walk_attribs (die);
27649 /* If this node is a specification,
27650 also mark the definition, if it exists. */
27651 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27652 prune_unused_types_mark (die->die_definition, 1);
27655 if (dokids && die->die_mark != 2)
27657 /* We need to walk the children, but haven't done so yet.
27658 Remember that we've walked the kids. */
27659 die->die_mark = 2;
27661 /* If this is an array type, we need to make sure our
27662 kids get marked, even if they're types. If we're
27663 breaking out types into comdat sections, do this
27664 for all type definitions. */
27665 if (die->die_tag == DW_TAG_array_type
27666 || (use_debug_types
27667 && is_type_die (die) && ! is_declaration_die (die)))
27668 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27669 else
27670 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27674 /* For local classes, look if any static member functions were emitted
27675 and if so, mark them. */
27677 static void
27678 prune_unused_types_walk_local_classes (dw_die_ref die)
27680 dw_die_ref c;
27682 if (die->die_mark == 2)
27683 return;
27685 switch (die->die_tag)
27687 case DW_TAG_structure_type:
27688 case DW_TAG_union_type:
27689 case DW_TAG_class_type:
27690 break;
27692 case DW_TAG_subprogram:
27693 if (!get_AT_flag (die, DW_AT_declaration)
27694 || die->die_definition != NULL)
27695 prune_unused_types_mark (die, 1);
27696 return;
27698 default:
27699 return;
27702 /* Mark children. */
27703 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27706 /* Walk the tree DIE and mark types that we actually use. */
27708 static void
27709 prune_unused_types_walk (dw_die_ref die)
27711 dw_die_ref c;
27713 /* Don't do anything if this node is already marked and
27714 children have been marked as well. */
27715 if (die->die_mark == 2)
27716 return;
27718 switch (die->die_tag)
27720 case DW_TAG_structure_type:
27721 case DW_TAG_union_type:
27722 case DW_TAG_class_type:
27723 if (die->die_perennial_p)
27724 break;
27726 for (c = die->die_parent; c; c = c->die_parent)
27727 if (c->die_tag == DW_TAG_subprogram)
27728 break;
27730 /* Finding used static member functions inside of classes
27731 is needed just for local classes, because for other classes
27732 static member function DIEs with DW_AT_specification
27733 are emitted outside of the DW_TAG_*_type. If we ever change
27734 it, we'd need to call this even for non-local classes. */
27735 if (c)
27736 prune_unused_types_walk_local_classes (die);
27738 /* It's a type node --- don't mark it. */
27739 return;
27741 case DW_TAG_const_type:
27742 case DW_TAG_packed_type:
27743 case DW_TAG_pointer_type:
27744 case DW_TAG_reference_type:
27745 case DW_TAG_rvalue_reference_type:
27746 case DW_TAG_volatile_type:
27747 case DW_TAG_typedef:
27748 case DW_TAG_array_type:
27749 case DW_TAG_interface_type:
27750 case DW_TAG_friend:
27751 case DW_TAG_enumeration_type:
27752 case DW_TAG_subroutine_type:
27753 case DW_TAG_string_type:
27754 case DW_TAG_set_type:
27755 case DW_TAG_subrange_type:
27756 case DW_TAG_ptr_to_member_type:
27757 case DW_TAG_file_type:
27758 /* Type nodes are useful only when other DIEs reference them --- don't
27759 mark them. */
27760 /* FALLTHROUGH */
27762 case DW_TAG_dwarf_procedure:
27763 /* Likewise for DWARF procedures. */
27765 if (die->die_perennial_p)
27766 break;
27768 return;
27770 default:
27771 /* Mark everything else. */
27772 break;
27775 if (die->die_mark == 0)
27777 die->die_mark = 1;
27779 /* Now, mark any dies referenced from here. */
27780 prune_unused_types_walk_attribs (die);
27783 die->die_mark = 2;
27785 /* Mark children. */
27786 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27789 /* Increment the string counts on strings referred to from DIE's
27790 attributes. */
27792 static void
27793 prune_unused_types_update_strings (dw_die_ref die)
27795 dw_attr_node *a;
27796 unsigned ix;
27798 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27799 if (AT_class (a) == dw_val_class_str)
27801 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27802 s->refcount++;
27803 /* Avoid unnecessarily putting strings that are used less than
27804 twice in the hash table. */
27805 if (s->refcount
27806 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27808 indirect_string_node **slot
27809 = debug_str_hash->find_slot_with_hash (s->str,
27810 htab_hash_string (s->str),
27811 INSERT);
27812 gcc_assert (*slot == NULL);
27813 *slot = s;
27818 /* Mark DIE and its children as removed. */
27820 static void
27821 mark_removed (dw_die_ref die)
27823 dw_die_ref c;
27824 die->removed = true;
27825 FOR_EACH_CHILD (die, c, mark_removed (c));
27828 /* Remove from the tree DIE any dies that aren't marked. */
27830 static void
27831 prune_unused_types_prune (dw_die_ref die)
27833 dw_die_ref c;
27835 gcc_assert (die->die_mark);
27836 prune_unused_types_update_strings (die);
27838 if (! die->die_child)
27839 return;
27841 c = die->die_child;
27842 do {
27843 dw_die_ref prev = c, next;
27844 for (c = c->die_sib; ! c->die_mark; c = next)
27845 if (c == die->die_child)
27847 /* No marked children between 'prev' and the end of the list. */
27848 if (prev == c)
27849 /* No marked children at all. */
27850 die->die_child = NULL;
27851 else
27853 prev->die_sib = c->die_sib;
27854 die->die_child = prev;
27856 c->die_sib = NULL;
27857 mark_removed (c);
27858 return;
27860 else
27862 next = c->die_sib;
27863 c->die_sib = NULL;
27864 mark_removed (c);
27867 if (c != prev->die_sib)
27868 prev->die_sib = c;
27869 prune_unused_types_prune (c);
27870 } while (c != die->die_child);
27873 /* Remove dies representing declarations that we never use. */
27875 static void
27876 prune_unused_types (void)
27878 unsigned int i;
27879 limbo_die_node *node;
27880 comdat_type_node *ctnode;
27881 pubname_entry *pub;
27882 dw_die_ref base_type;
27884 #if ENABLE_ASSERT_CHECKING
27885 /* All the marks should already be clear. */
27886 verify_marks_clear (comp_unit_die ());
27887 for (node = limbo_die_list; node; node = node->next)
27888 verify_marks_clear (node->die);
27889 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27890 verify_marks_clear (ctnode->root_die);
27891 #endif /* ENABLE_ASSERT_CHECKING */
27893 /* Mark types that are used in global variables. */
27894 premark_types_used_by_global_vars ();
27896 /* Set the mark on nodes that are actually used. */
27897 prune_unused_types_walk (comp_unit_die ());
27898 for (node = limbo_die_list; node; node = node->next)
27899 prune_unused_types_walk (node->die);
27900 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27902 prune_unused_types_walk (ctnode->root_die);
27903 prune_unused_types_mark (ctnode->type_die, 1);
27906 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27907 are unusual in that they are pubnames that are the children of pubtypes.
27908 They should only be marked via their parent DW_TAG_enumeration_type die,
27909 not as roots in themselves. */
27910 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27911 if (pub->die->die_tag != DW_TAG_enumerator)
27912 prune_unused_types_mark (pub->die, 1);
27913 for (i = 0; base_types.iterate (i, &base_type); i++)
27914 prune_unused_types_mark (base_type, 1);
27916 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27917 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27918 callees). */
27919 cgraph_node *cnode;
27920 FOR_EACH_FUNCTION (cnode)
27921 if (cnode->referred_to_p (false))
27923 dw_die_ref die = lookup_decl_die (cnode->decl);
27924 if (die == NULL || die->die_mark)
27925 continue;
27926 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27927 if (e->caller != cnode
27928 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27930 prune_unused_types_mark (die, 1);
27931 break;
27935 if (debug_str_hash)
27936 debug_str_hash->empty ();
27937 if (skeleton_debug_str_hash)
27938 skeleton_debug_str_hash->empty ();
27939 prune_unused_types_prune (comp_unit_die ());
27940 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27942 node = *pnode;
27943 if (!node->die->die_mark)
27944 *pnode = node->next;
27945 else
27947 prune_unused_types_prune (node->die);
27948 pnode = &node->next;
27951 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27952 prune_unused_types_prune (ctnode->root_die);
27954 /* Leave the marks clear. */
27955 prune_unmark_dies (comp_unit_die ());
27956 for (node = limbo_die_list; node; node = node->next)
27957 prune_unmark_dies (node->die);
27958 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27959 prune_unmark_dies (ctnode->root_die);
27962 /* Helpers to manipulate hash table of comdat type units. */
27964 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27966 static inline hashval_t hash (const comdat_type_node *);
27967 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27970 inline hashval_t
27971 comdat_type_hasher::hash (const comdat_type_node *type_node)
27973 hashval_t h;
27974 memcpy (&h, type_node->signature, sizeof (h));
27975 return h;
27978 inline bool
27979 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27980 const comdat_type_node *type_node_2)
27982 return (! memcmp (type_node_1->signature, type_node_2->signature,
27983 DWARF_TYPE_SIGNATURE_SIZE));
27986 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27987 to the location it would have been added, should we know its
27988 DECL_ASSEMBLER_NAME when we added other attributes. This will
27989 probably improve compactness of debug info, removing equivalent
27990 abbrevs, and hide any differences caused by deferring the
27991 computation of the assembler name, triggered by e.g. PCH. */
27993 static inline void
27994 move_linkage_attr (dw_die_ref die)
27996 unsigned ix = vec_safe_length (die->die_attr);
27997 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27999 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28000 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28002 while (--ix > 0)
28004 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28006 if (prev->dw_attr == DW_AT_decl_line
28007 || prev->dw_attr == DW_AT_decl_column
28008 || prev->dw_attr == DW_AT_name)
28009 break;
28012 if (ix != vec_safe_length (die->die_attr) - 1)
28014 die->die_attr->pop ();
28015 die->die_attr->quick_insert (ix, linkage);
28019 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28020 referenced from typed stack ops and count how often they are used. */
28022 static void
28023 mark_base_types (dw_loc_descr_ref loc)
28025 dw_die_ref base_type = NULL;
28027 for (; loc; loc = loc->dw_loc_next)
28029 switch (loc->dw_loc_opc)
28031 case DW_OP_regval_type:
28032 case DW_OP_deref_type:
28033 case DW_OP_GNU_regval_type:
28034 case DW_OP_GNU_deref_type:
28035 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28036 break;
28037 case DW_OP_convert:
28038 case DW_OP_reinterpret:
28039 case DW_OP_GNU_convert:
28040 case DW_OP_GNU_reinterpret:
28041 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28042 continue;
28043 /* FALLTHRU */
28044 case DW_OP_const_type:
28045 case DW_OP_GNU_const_type:
28046 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28047 break;
28048 case DW_OP_entry_value:
28049 case DW_OP_GNU_entry_value:
28050 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28051 continue;
28052 default:
28053 continue;
28055 gcc_assert (base_type->die_parent == comp_unit_die ());
28056 if (base_type->die_mark)
28057 base_type->die_mark++;
28058 else
28060 base_types.safe_push (base_type);
28061 base_type->die_mark = 1;
28066 /* Comparison function for sorting marked base types. */
28068 static int
28069 base_type_cmp (const void *x, const void *y)
28071 dw_die_ref dx = *(const dw_die_ref *) x;
28072 dw_die_ref dy = *(const dw_die_ref *) y;
28073 unsigned int byte_size1, byte_size2;
28074 unsigned int encoding1, encoding2;
28075 unsigned int align1, align2;
28076 if (dx->die_mark > dy->die_mark)
28077 return -1;
28078 if (dx->die_mark < dy->die_mark)
28079 return 1;
28080 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28081 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28082 if (byte_size1 < byte_size2)
28083 return 1;
28084 if (byte_size1 > byte_size2)
28085 return -1;
28086 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28087 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28088 if (encoding1 < encoding2)
28089 return 1;
28090 if (encoding1 > encoding2)
28091 return -1;
28092 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28093 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28094 if (align1 < align2)
28095 return 1;
28096 if (align1 > align2)
28097 return -1;
28098 return 0;
28101 /* Move base types marked by mark_base_types as early as possible
28102 in the CU, sorted by decreasing usage count both to make the
28103 uleb128 references as small as possible and to make sure they
28104 will have die_offset already computed by calc_die_sizes when
28105 sizes of typed stack loc ops is computed. */
28107 static void
28108 move_marked_base_types (void)
28110 unsigned int i;
28111 dw_die_ref base_type, die, c;
28113 if (base_types.is_empty ())
28114 return;
28116 /* Sort by decreasing usage count, they will be added again in that
28117 order later on. */
28118 base_types.qsort (base_type_cmp);
28119 die = comp_unit_die ();
28120 c = die->die_child;
28123 dw_die_ref prev = c;
28124 c = c->die_sib;
28125 while (c->die_mark)
28127 remove_child_with_prev (c, prev);
28128 /* As base types got marked, there must be at least
28129 one node other than DW_TAG_base_type. */
28130 gcc_assert (die->die_child != NULL);
28131 c = prev->die_sib;
28134 while (c != die->die_child);
28135 gcc_assert (die->die_child);
28136 c = die->die_child;
28137 for (i = 0; base_types.iterate (i, &base_type); i++)
28139 base_type->die_mark = 0;
28140 base_type->die_sib = c->die_sib;
28141 c->die_sib = base_type;
28142 c = base_type;
28146 /* Helper function for resolve_addr, attempt to resolve
28147 one CONST_STRING, return true if successful. Similarly verify that
28148 SYMBOL_REFs refer to variables emitted in the current CU. */
28150 static bool
28151 resolve_one_addr (rtx *addr)
28153 rtx rtl = *addr;
28155 if (GET_CODE (rtl) == CONST_STRING)
28157 size_t len = strlen (XSTR (rtl, 0)) + 1;
28158 tree t = build_string (len, XSTR (rtl, 0));
28159 tree tlen = size_int (len - 1);
28160 TREE_TYPE (t)
28161 = build_array_type (char_type_node, build_index_type (tlen));
28162 rtl = lookup_constant_def (t);
28163 if (!rtl || !MEM_P (rtl))
28164 return false;
28165 rtl = XEXP (rtl, 0);
28166 if (GET_CODE (rtl) == SYMBOL_REF
28167 && SYMBOL_REF_DECL (rtl)
28168 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28169 return false;
28170 vec_safe_push (used_rtx_array, rtl);
28171 *addr = rtl;
28172 return true;
28175 if (GET_CODE (rtl) == SYMBOL_REF
28176 && SYMBOL_REF_DECL (rtl))
28178 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28180 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28181 return false;
28183 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28184 return false;
28187 if (GET_CODE (rtl) == CONST)
28189 subrtx_ptr_iterator::array_type array;
28190 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28191 if (!resolve_one_addr (*iter))
28192 return false;
28195 return true;
28198 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28199 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28200 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28202 static rtx
28203 string_cst_pool_decl (tree t)
28205 rtx rtl = output_constant_def (t, 1);
28206 unsigned char *array;
28207 dw_loc_descr_ref l;
28208 tree decl;
28209 size_t len;
28210 dw_die_ref ref;
28212 if (!rtl || !MEM_P (rtl))
28213 return NULL_RTX;
28214 rtl = XEXP (rtl, 0);
28215 if (GET_CODE (rtl) != SYMBOL_REF
28216 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28217 return NULL_RTX;
28219 decl = SYMBOL_REF_DECL (rtl);
28220 if (!lookup_decl_die (decl))
28222 len = TREE_STRING_LENGTH (t);
28223 vec_safe_push (used_rtx_array, rtl);
28224 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28225 array = ggc_vec_alloc<unsigned char> (len);
28226 memcpy (array, TREE_STRING_POINTER (t), len);
28227 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28228 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28229 l->dw_loc_oprnd2.v.val_vec.length = len;
28230 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28231 l->dw_loc_oprnd2.v.val_vec.array = array;
28232 add_AT_loc (ref, DW_AT_location, l);
28233 equate_decl_number_to_die (decl, ref);
28235 return rtl;
28238 /* Helper function of resolve_addr_in_expr. LOC is
28239 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28240 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28241 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28242 with DW_OP_implicit_pointer if possible
28243 and return true, if unsuccessful, return false. */
28245 static bool
28246 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28248 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28249 HOST_WIDE_INT offset = 0;
28250 dw_die_ref ref = NULL;
28251 tree decl;
28253 if (GET_CODE (rtl) == CONST
28254 && GET_CODE (XEXP (rtl, 0)) == PLUS
28255 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28257 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28258 rtl = XEXP (XEXP (rtl, 0), 0);
28260 if (GET_CODE (rtl) == CONST_STRING)
28262 size_t len = strlen (XSTR (rtl, 0)) + 1;
28263 tree t = build_string (len, XSTR (rtl, 0));
28264 tree tlen = size_int (len - 1);
28266 TREE_TYPE (t)
28267 = build_array_type (char_type_node, build_index_type (tlen));
28268 rtl = string_cst_pool_decl (t);
28269 if (!rtl)
28270 return false;
28272 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28274 decl = SYMBOL_REF_DECL (rtl);
28275 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28277 ref = lookup_decl_die (decl);
28278 if (ref && (get_AT (ref, DW_AT_location)
28279 || get_AT (ref, DW_AT_const_value)))
28281 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28282 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28283 loc->dw_loc_oprnd1.val_entry = NULL;
28284 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28285 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28286 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28287 loc->dw_loc_oprnd2.v.val_int = offset;
28288 return true;
28292 return false;
28295 /* Helper function for resolve_addr, handle one location
28296 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28297 the location list couldn't be resolved. */
28299 static bool
28300 resolve_addr_in_expr (dw_loc_descr_ref loc)
28302 dw_loc_descr_ref keep = NULL;
28303 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28304 switch (loc->dw_loc_opc)
28306 case DW_OP_addr:
28307 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28309 if ((prev == NULL
28310 || prev->dw_loc_opc == DW_OP_piece
28311 || prev->dw_loc_opc == DW_OP_bit_piece)
28312 && loc->dw_loc_next
28313 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28314 && (!dwarf_strict || dwarf_version >= 5)
28315 && optimize_one_addr_into_implicit_ptr (loc))
28316 break;
28317 return false;
28319 break;
28320 case DW_OP_GNU_addr_index:
28321 case DW_OP_GNU_const_index:
28322 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28323 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28325 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28326 if (!resolve_one_addr (&rtl))
28327 return false;
28328 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28329 loc->dw_loc_oprnd1.val_entry
28330 = add_addr_table_entry (rtl, ate_kind_rtx);
28332 break;
28333 case DW_OP_const4u:
28334 case DW_OP_const8u:
28335 if (loc->dtprel
28336 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28337 return false;
28338 break;
28339 case DW_OP_plus_uconst:
28340 if (size_of_loc_descr (loc)
28341 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28343 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28345 dw_loc_descr_ref repl
28346 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28347 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28348 add_loc_descr (&repl, loc->dw_loc_next);
28349 *loc = *repl;
28351 break;
28352 case DW_OP_implicit_value:
28353 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28354 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28355 return false;
28356 break;
28357 case DW_OP_implicit_pointer:
28358 case DW_OP_GNU_implicit_pointer:
28359 case DW_OP_GNU_parameter_ref:
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 break;
28371 case DW_OP_const_type:
28372 case DW_OP_regval_type:
28373 case DW_OP_deref_type:
28374 case DW_OP_convert:
28375 case DW_OP_reinterpret:
28376 case DW_OP_GNU_const_type:
28377 case DW_OP_GNU_regval_type:
28378 case DW_OP_GNU_deref_type:
28379 case DW_OP_GNU_convert:
28380 case DW_OP_GNU_reinterpret:
28381 while (loc->dw_loc_next
28382 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28383 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28385 dw_die_ref base1, base2;
28386 unsigned enc1, enc2, size1, size2;
28387 if (loc->dw_loc_opc == DW_OP_regval_type
28388 || loc->dw_loc_opc == DW_OP_deref_type
28389 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28390 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28391 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28392 else if (loc->dw_loc_oprnd1.val_class
28393 == dw_val_class_unsigned_const)
28394 break;
28395 else
28396 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28397 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28398 == dw_val_class_unsigned_const)
28399 break;
28400 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28401 gcc_assert (base1->die_tag == DW_TAG_base_type
28402 && base2->die_tag == DW_TAG_base_type);
28403 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28404 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28405 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28406 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28407 if (size1 == size2
28408 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28409 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28410 && loc != keep)
28411 || enc1 == enc2))
28413 /* Optimize away next DW_OP_convert after
28414 adjusting LOC's base type die reference. */
28415 if (loc->dw_loc_opc == DW_OP_regval_type
28416 || loc->dw_loc_opc == DW_OP_deref_type
28417 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28418 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28419 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28420 else
28421 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28422 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28423 continue;
28425 /* Don't change integer DW_OP_convert after e.g. floating
28426 point typed stack entry. */
28427 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28428 keep = loc->dw_loc_next;
28429 break;
28431 break;
28432 default:
28433 break;
28435 return true;
28438 /* Helper function of resolve_addr. DIE had DW_AT_location of
28439 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28440 and DW_OP_addr couldn't be resolved. resolve_addr has already
28441 removed the DW_AT_location attribute. This function attempts to
28442 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28443 to it or DW_AT_const_value attribute, if possible. */
28445 static void
28446 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28448 if (!VAR_P (decl)
28449 || lookup_decl_die (decl) != die
28450 || DECL_EXTERNAL (decl)
28451 || !TREE_STATIC (decl)
28452 || DECL_INITIAL (decl) == NULL_TREE
28453 || DECL_P (DECL_INITIAL (decl))
28454 || get_AT (die, DW_AT_const_value))
28455 return;
28457 tree init = DECL_INITIAL (decl);
28458 HOST_WIDE_INT offset = 0;
28459 /* For variables that have been optimized away and thus
28460 don't have a memory location, see if we can emit
28461 DW_AT_const_value instead. */
28462 if (tree_add_const_value_attribute (die, init))
28463 return;
28464 if (dwarf_strict && dwarf_version < 5)
28465 return;
28466 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28467 and ADDR_EXPR refers to a decl that has DW_AT_location or
28468 DW_AT_const_value (but isn't addressable, otherwise
28469 resolving the original DW_OP_addr wouldn't fail), see if
28470 we can add DW_OP_implicit_pointer. */
28471 STRIP_NOPS (init);
28472 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28473 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28475 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28476 init = TREE_OPERAND (init, 0);
28477 STRIP_NOPS (init);
28479 if (TREE_CODE (init) != ADDR_EXPR)
28480 return;
28481 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28482 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28483 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28484 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28485 && TREE_OPERAND (init, 0) != decl))
28487 dw_die_ref ref;
28488 dw_loc_descr_ref l;
28490 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28492 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28493 if (!rtl)
28494 return;
28495 decl = SYMBOL_REF_DECL (rtl);
28497 else
28498 decl = TREE_OPERAND (init, 0);
28499 ref = lookup_decl_die (decl);
28500 if (ref == NULL
28501 || (!get_AT (ref, DW_AT_location)
28502 && !get_AT (ref, DW_AT_const_value)))
28503 return;
28504 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28505 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28506 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28507 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28508 add_AT_loc (die, DW_AT_location, l);
28512 /* Return NULL if l is a DWARF expression, or first op that is not
28513 valid DWARF expression. */
28515 static dw_loc_descr_ref
28516 non_dwarf_expression (dw_loc_descr_ref l)
28518 while (l)
28520 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28521 return l;
28522 switch (l->dw_loc_opc)
28524 case DW_OP_regx:
28525 case DW_OP_implicit_value:
28526 case DW_OP_stack_value:
28527 case DW_OP_implicit_pointer:
28528 case DW_OP_GNU_implicit_pointer:
28529 case DW_OP_GNU_parameter_ref:
28530 case DW_OP_piece:
28531 case DW_OP_bit_piece:
28532 return l;
28533 default:
28534 break;
28536 l = l->dw_loc_next;
28538 return NULL;
28541 /* Return adjusted copy of EXPR:
28542 If it is empty DWARF expression, return it.
28543 If it is valid non-empty DWARF expression,
28544 return copy of EXPR with copy of DEREF appended to it.
28545 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28546 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
28547 and no DEREF.
28548 If it is DWARF expression followed by DW_OP_stack_value, return
28549 copy of the DWARF expression without anything appended.
28550 Otherwise, return NULL. */
28552 static dw_loc_descr_ref
28553 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
28556 if (expr == NULL)
28557 return NULL;
28559 dw_loc_descr_ref l = non_dwarf_expression (expr);
28560 if (l && l->dw_loc_next)
28561 return NULL;
28563 if (l)
28565 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28566 deref = new_loc_descr ((enum dwarf_location_atom)
28567 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28568 0, 0);
28569 else
28570 switch (l->dw_loc_opc)
28572 case DW_OP_regx:
28573 deref = new_loc_descr (DW_OP_bregx,
28574 l->dw_loc_oprnd1.v.val_unsigned, 0);
28575 break;
28576 case DW_OP_stack_value:
28577 deref = NULL;
28578 break;
28579 default:
28580 return NULL;
28583 else
28584 deref = new_loc_descr (deref->dw_loc_opc,
28585 deref->dw_loc_oprnd1.v.val_int, 0);
28587 dw_loc_descr_ref ret = NULL, *p = &ret;
28588 while (expr != l)
28590 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28591 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28592 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28593 p = &(*p)->dw_loc_next;
28594 expr = expr->dw_loc_next;
28596 *p = deref;
28597 return ret;
28600 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
28601 or argument, adjust it if needed and return:
28602 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28603 attribute if present should be removed
28604 0 keep the attribute as is if the referenced var or argument has
28605 only DWARF expression that covers all ranges
28606 1 if the attribute has been successfully adjusted. */
28608 static int
28609 optimize_string_length (dw_attr_node *a)
28611 dw_loc_descr_ref l = AT_loc (a), lv;
28612 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
28613 dw_attr_node *av = get_AT (die, DW_AT_location);
28614 dw_loc_list_ref d;
28615 bool non_dwarf_expr = false;
28617 if (av == NULL)
28618 return -1;
28619 switch (AT_class (av))
28621 case dw_val_class_loc_list:
28622 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28623 if (d->expr && non_dwarf_expression (d->expr))
28624 non_dwarf_expr = true;
28625 break;
28626 case dw_val_class_loc:
28627 lv = AT_loc (av);
28628 if (lv == NULL)
28629 return -1;
28630 if (non_dwarf_expression (lv))
28631 non_dwarf_expr = true;
28632 break;
28633 default:
28634 return -1;
28637 /* If it is safe to keep DW_OP_call4 in, keep it. */
28638 if (!non_dwarf_expr
28639 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
28640 return 0;
28642 /* If not dereferencing the DW_OP_call4 afterwards, we can just
28643 copy over the DW_AT_location attribute from die to a. */
28644 if (l->dw_loc_next == NULL)
28646 a->dw_attr_val = av->dw_attr_val;
28647 return 1;
28650 dw_loc_list_ref list, *p;
28651 switch (AT_class (av))
28653 case dw_val_class_loc_list:
28654 p = &list;
28655 list = NULL;
28656 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28658 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
28659 if (lv)
28661 *p = new_loc_list (lv, d->begin, d->end, d->section);
28662 p = &(*p)->dw_loc_next;
28665 if (list == NULL)
28666 return -1;
28667 a->dw_attr_val.val_class = dw_val_class_loc_list;
28668 gen_llsym (list);
28669 *AT_loc_list_ptr (a) = list;
28670 return 1;
28671 case dw_val_class_loc:
28672 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
28673 if (lv == NULL)
28674 return -1;
28675 a->dw_attr_val.v.val_loc = lv;
28676 return 1;
28677 default:
28678 gcc_unreachable ();
28682 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28683 an address in .rodata section if the string literal is emitted there,
28684 or remove the containing location list or replace DW_AT_const_value
28685 with DW_AT_location and empty location expression, if it isn't found
28686 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28687 to something that has been emitted in the current CU. */
28689 static void
28690 resolve_addr (dw_die_ref die)
28692 dw_die_ref c;
28693 dw_attr_node *a;
28694 dw_loc_list_ref *curr, *start, loc;
28695 unsigned ix;
28696 bool remove_AT_byte_size = false;
28698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28699 switch (AT_class (a))
28701 case dw_val_class_loc_list:
28702 start = curr = AT_loc_list_ptr (a);
28703 loc = *curr;
28704 gcc_assert (loc);
28705 /* The same list can be referenced more than once. See if we have
28706 already recorded the result from a previous pass. */
28707 if (loc->replaced)
28708 *curr = loc->dw_loc_next;
28709 else if (!loc->resolved_addr)
28711 /* As things stand, we do not expect or allow one die to
28712 reference a suffix of another die's location list chain.
28713 References must be identical or completely separate.
28714 There is therefore no need to cache the result of this
28715 pass on any list other than the first; doing so
28716 would lead to unnecessary writes. */
28717 while (*curr)
28719 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28720 if (!resolve_addr_in_expr ((*curr)->expr))
28722 dw_loc_list_ref next = (*curr)->dw_loc_next;
28723 dw_loc_descr_ref l = (*curr)->expr;
28725 if (next && (*curr)->ll_symbol)
28727 gcc_assert (!next->ll_symbol);
28728 next->ll_symbol = (*curr)->ll_symbol;
28730 if (dwarf_split_debug_info)
28731 remove_loc_list_addr_table_entries (l);
28732 *curr = next;
28734 else
28736 mark_base_types ((*curr)->expr);
28737 curr = &(*curr)->dw_loc_next;
28740 if (loc == *start)
28741 loc->resolved_addr = 1;
28742 else
28744 loc->replaced = 1;
28745 loc->dw_loc_next = *start;
28748 if (!*start)
28750 remove_AT (die, a->dw_attr);
28751 ix--;
28753 break;
28754 case dw_val_class_loc:
28756 dw_loc_descr_ref l = AT_loc (a);
28757 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
28758 DW_AT_string_length is only a rough approximation; unfortunately
28759 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
28760 needs a DWARF expression, while DW_AT_location of the referenced
28761 variable or argument might be any location description. */
28762 if (a->dw_attr == DW_AT_string_length
28763 && l
28764 && l->dw_loc_opc == DW_OP_call4
28765 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
28766 && (l->dw_loc_next == NULL
28767 || (l->dw_loc_next->dw_loc_next == NULL
28768 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
28769 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
28771 switch (optimize_string_length (a))
28773 case -1:
28774 remove_AT (die, a->dw_attr);
28775 ix--;
28776 /* If we drop DW_AT_string_length, we need to drop also
28777 DW_AT_{string_length_,}byte_size. */
28778 remove_AT_byte_size = true;
28779 continue;
28780 default:
28781 break;
28782 case 1:
28783 /* Even if we keep the optimized DW_AT_string_length,
28784 it might have changed AT_class, so process it again. */
28785 ix--;
28786 continue;
28789 /* For -gdwarf-2 don't attempt to optimize
28790 DW_AT_data_member_location containing
28791 DW_OP_plus_uconst - older consumers might
28792 rely on it being that op instead of a more complex,
28793 but shorter, location description. */
28794 if ((dwarf_version > 2
28795 || a->dw_attr != DW_AT_data_member_location
28796 || l == NULL
28797 || l->dw_loc_opc != DW_OP_plus_uconst
28798 || l->dw_loc_next != NULL)
28799 && !resolve_addr_in_expr (l))
28801 if (dwarf_split_debug_info)
28802 remove_loc_list_addr_table_entries (l);
28803 if (l != NULL
28804 && l->dw_loc_next == NULL
28805 && l->dw_loc_opc == DW_OP_addr
28806 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28807 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28808 && a->dw_attr == DW_AT_location)
28810 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28811 remove_AT (die, a->dw_attr);
28812 ix--;
28813 optimize_location_into_implicit_ptr (die, decl);
28814 break;
28816 remove_AT (die, a->dw_attr);
28817 ix--;
28819 else
28820 mark_base_types (l);
28822 break;
28823 case dw_val_class_addr:
28824 if (a->dw_attr == DW_AT_const_value
28825 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28827 if (AT_index (a) != NOT_INDEXED)
28828 remove_addr_table_entry (a->dw_attr_val.val_entry);
28829 remove_AT (die, a->dw_attr);
28830 ix--;
28832 if ((die->die_tag == DW_TAG_call_site
28833 && a->dw_attr == DW_AT_call_origin)
28834 || (die->die_tag == DW_TAG_GNU_call_site
28835 && a->dw_attr == DW_AT_abstract_origin))
28837 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28838 dw_die_ref tdie = lookup_decl_die (tdecl);
28839 dw_die_ref cdie;
28840 if (tdie == NULL
28841 && DECL_EXTERNAL (tdecl)
28842 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28843 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28845 dw_die_ref pdie = cdie;
28846 /* Make sure we don't add these DIEs into type units.
28847 We could emit skeleton DIEs for context (namespaces,
28848 outer structs/classes) and a skeleton DIE for the
28849 innermost context with DW_AT_signature pointing to the
28850 type unit. See PR78835. */
28851 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28852 pdie = pdie->die_parent;
28853 if (pdie == NULL)
28855 /* Creating a full DIE for tdecl is overly expensive and
28856 at this point even wrong when in the LTO phase
28857 as it can end up generating new type DIEs we didn't
28858 output and thus optimize_external_refs will crash. */
28859 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28860 add_AT_flag (tdie, DW_AT_external, 1);
28861 add_AT_flag (tdie, DW_AT_declaration, 1);
28862 add_linkage_attr (tdie, tdecl);
28863 add_name_and_src_coords_attributes (tdie, tdecl);
28864 equate_decl_number_to_die (tdecl, tdie);
28867 if (tdie)
28869 a->dw_attr_val.val_class = dw_val_class_die_ref;
28870 a->dw_attr_val.v.val_die_ref.die = tdie;
28871 a->dw_attr_val.v.val_die_ref.external = 0;
28873 else
28875 if (AT_index (a) != NOT_INDEXED)
28876 remove_addr_table_entry (a->dw_attr_val.val_entry);
28877 remove_AT (die, a->dw_attr);
28878 ix--;
28881 break;
28882 default:
28883 break;
28886 if (remove_AT_byte_size)
28887 remove_AT (die, dwarf_version >= 5
28888 ? DW_AT_string_length_byte_size
28889 : DW_AT_byte_size);
28891 FOR_EACH_CHILD (die, c, resolve_addr (c));
28894 /* Helper routines for optimize_location_lists.
28895 This pass tries to share identical local lists in .debug_loc
28896 section. */
28898 /* Iteratively hash operands of LOC opcode into HSTATE. */
28900 static void
28901 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28903 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28904 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28906 switch (loc->dw_loc_opc)
28908 case DW_OP_const4u:
28909 case DW_OP_const8u:
28910 if (loc->dtprel)
28911 goto hash_addr;
28912 /* FALLTHRU */
28913 case DW_OP_const1u:
28914 case DW_OP_const1s:
28915 case DW_OP_const2u:
28916 case DW_OP_const2s:
28917 case DW_OP_const4s:
28918 case DW_OP_const8s:
28919 case DW_OP_constu:
28920 case DW_OP_consts:
28921 case DW_OP_pick:
28922 case DW_OP_plus_uconst:
28923 case DW_OP_breg0:
28924 case DW_OP_breg1:
28925 case DW_OP_breg2:
28926 case DW_OP_breg3:
28927 case DW_OP_breg4:
28928 case DW_OP_breg5:
28929 case DW_OP_breg6:
28930 case DW_OP_breg7:
28931 case DW_OP_breg8:
28932 case DW_OP_breg9:
28933 case DW_OP_breg10:
28934 case DW_OP_breg11:
28935 case DW_OP_breg12:
28936 case DW_OP_breg13:
28937 case DW_OP_breg14:
28938 case DW_OP_breg15:
28939 case DW_OP_breg16:
28940 case DW_OP_breg17:
28941 case DW_OP_breg18:
28942 case DW_OP_breg19:
28943 case DW_OP_breg20:
28944 case DW_OP_breg21:
28945 case DW_OP_breg22:
28946 case DW_OP_breg23:
28947 case DW_OP_breg24:
28948 case DW_OP_breg25:
28949 case DW_OP_breg26:
28950 case DW_OP_breg27:
28951 case DW_OP_breg28:
28952 case DW_OP_breg29:
28953 case DW_OP_breg30:
28954 case DW_OP_breg31:
28955 case DW_OP_regx:
28956 case DW_OP_fbreg:
28957 case DW_OP_piece:
28958 case DW_OP_deref_size:
28959 case DW_OP_xderef_size:
28960 hstate.add_object (val1->v.val_int);
28961 break;
28962 case DW_OP_skip:
28963 case DW_OP_bra:
28965 int offset;
28967 gcc_assert (val1->val_class == dw_val_class_loc);
28968 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
28969 hstate.add_object (offset);
28971 break;
28972 case DW_OP_implicit_value:
28973 hstate.add_object (val1->v.val_unsigned);
28974 switch (val2->val_class)
28976 case dw_val_class_const:
28977 hstate.add_object (val2->v.val_int);
28978 break;
28979 case dw_val_class_vec:
28981 unsigned int elt_size = val2->v.val_vec.elt_size;
28982 unsigned int len = val2->v.val_vec.length;
28984 hstate.add_int (elt_size);
28985 hstate.add_int (len);
28986 hstate.add (val2->v.val_vec.array, len * elt_size);
28988 break;
28989 case dw_val_class_const_double:
28990 hstate.add_object (val2->v.val_double.low);
28991 hstate.add_object (val2->v.val_double.high);
28992 break;
28993 case dw_val_class_wide_int:
28994 hstate.add (val2->v.val_wide->get_val (),
28995 get_full_len (*val2->v.val_wide)
28996 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28997 break;
28998 case dw_val_class_addr:
28999 inchash::add_rtx (val2->v.val_addr, hstate);
29000 break;
29001 default:
29002 gcc_unreachable ();
29004 break;
29005 case DW_OP_bregx:
29006 case DW_OP_bit_piece:
29007 hstate.add_object (val1->v.val_int);
29008 hstate.add_object (val2->v.val_int);
29009 break;
29010 case DW_OP_addr:
29011 hash_addr:
29012 if (loc->dtprel)
29014 unsigned char dtprel = 0xd1;
29015 hstate.add_object (dtprel);
29017 inchash::add_rtx (val1->v.val_addr, hstate);
29018 break;
29019 case DW_OP_GNU_addr_index:
29020 case DW_OP_GNU_const_index:
29022 if (loc->dtprel)
29024 unsigned char dtprel = 0xd1;
29025 hstate.add_object (dtprel);
29027 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29029 break;
29030 case DW_OP_implicit_pointer:
29031 case DW_OP_GNU_implicit_pointer:
29032 hstate.add_int (val2->v.val_int);
29033 break;
29034 case DW_OP_entry_value:
29035 case DW_OP_GNU_entry_value:
29036 hstate.add_object (val1->v.val_loc);
29037 break;
29038 case DW_OP_regval_type:
29039 case DW_OP_deref_type:
29040 case DW_OP_GNU_regval_type:
29041 case DW_OP_GNU_deref_type:
29043 unsigned int byte_size
29044 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29045 unsigned int encoding
29046 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29047 hstate.add_object (val1->v.val_int);
29048 hstate.add_object (byte_size);
29049 hstate.add_object (encoding);
29051 break;
29052 case DW_OP_convert:
29053 case DW_OP_reinterpret:
29054 case DW_OP_GNU_convert:
29055 case DW_OP_GNU_reinterpret:
29056 if (val1->val_class == dw_val_class_unsigned_const)
29058 hstate.add_object (val1->v.val_unsigned);
29059 break;
29061 /* FALLTHRU */
29062 case DW_OP_const_type:
29063 case DW_OP_GNU_const_type:
29065 unsigned int byte_size
29066 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29067 unsigned int encoding
29068 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29069 hstate.add_object (byte_size);
29070 hstate.add_object (encoding);
29071 if (loc->dw_loc_opc != DW_OP_const_type
29072 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29073 break;
29074 hstate.add_object (val2->val_class);
29075 switch (val2->val_class)
29077 case dw_val_class_const:
29078 hstate.add_object (val2->v.val_int);
29079 break;
29080 case dw_val_class_vec:
29082 unsigned int elt_size = val2->v.val_vec.elt_size;
29083 unsigned int len = val2->v.val_vec.length;
29085 hstate.add_object (elt_size);
29086 hstate.add_object (len);
29087 hstate.add (val2->v.val_vec.array, len * elt_size);
29089 break;
29090 case dw_val_class_const_double:
29091 hstate.add_object (val2->v.val_double.low);
29092 hstate.add_object (val2->v.val_double.high);
29093 break;
29094 case dw_val_class_wide_int:
29095 hstate.add (val2->v.val_wide->get_val (),
29096 get_full_len (*val2->v.val_wide)
29097 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29098 break;
29099 default:
29100 gcc_unreachable ();
29103 break;
29105 default:
29106 /* Other codes have no operands. */
29107 break;
29111 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29113 static inline void
29114 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29116 dw_loc_descr_ref l;
29117 bool sizes_computed = false;
29118 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29119 size_of_locs (loc);
29121 for (l = loc; l != NULL; l = l->dw_loc_next)
29123 enum dwarf_location_atom opc = l->dw_loc_opc;
29124 hstate.add_object (opc);
29125 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29127 size_of_locs (loc);
29128 sizes_computed = true;
29130 hash_loc_operands (l, hstate);
29134 /* Compute hash of the whole location list LIST_HEAD. */
29136 static inline void
29137 hash_loc_list (dw_loc_list_ref list_head)
29139 dw_loc_list_ref curr = list_head;
29140 inchash::hash hstate;
29142 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29144 hstate.add (curr->begin, strlen (curr->begin) + 1);
29145 hstate.add (curr->end, strlen (curr->end) + 1);
29146 if (curr->section)
29147 hstate.add (curr->section, strlen (curr->section) + 1);
29148 hash_locs (curr->expr, hstate);
29150 list_head->hash = hstate.end ();
29153 /* Return true if X and Y opcodes have the same operands. */
29155 static inline bool
29156 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29158 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29159 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29160 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29161 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29163 switch (x->dw_loc_opc)
29165 case DW_OP_const4u:
29166 case DW_OP_const8u:
29167 if (x->dtprel)
29168 goto hash_addr;
29169 /* FALLTHRU */
29170 case DW_OP_const1u:
29171 case DW_OP_const1s:
29172 case DW_OP_const2u:
29173 case DW_OP_const2s:
29174 case DW_OP_const4s:
29175 case DW_OP_const8s:
29176 case DW_OP_constu:
29177 case DW_OP_consts:
29178 case DW_OP_pick:
29179 case DW_OP_plus_uconst:
29180 case DW_OP_breg0:
29181 case DW_OP_breg1:
29182 case DW_OP_breg2:
29183 case DW_OP_breg3:
29184 case DW_OP_breg4:
29185 case DW_OP_breg5:
29186 case DW_OP_breg6:
29187 case DW_OP_breg7:
29188 case DW_OP_breg8:
29189 case DW_OP_breg9:
29190 case DW_OP_breg10:
29191 case DW_OP_breg11:
29192 case DW_OP_breg12:
29193 case DW_OP_breg13:
29194 case DW_OP_breg14:
29195 case DW_OP_breg15:
29196 case DW_OP_breg16:
29197 case DW_OP_breg17:
29198 case DW_OP_breg18:
29199 case DW_OP_breg19:
29200 case DW_OP_breg20:
29201 case DW_OP_breg21:
29202 case DW_OP_breg22:
29203 case DW_OP_breg23:
29204 case DW_OP_breg24:
29205 case DW_OP_breg25:
29206 case DW_OP_breg26:
29207 case DW_OP_breg27:
29208 case DW_OP_breg28:
29209 case DW_OP_breg29:
29210 case DW_OP_breg30:
29211 case DW_OP_breg31:
29212 case DW_OP_regx:
29213 case DW_OP_fbreg:
29214 case DW_OP_piece:
29215 case DW_OP_deref_size:
29216 case DW_OP_xderef_size:
29217 return valx1->v.val_int == valy1->v.val_int;
29218 case DW_OP_skip:
29219 case DW_OP_bra:
29220 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29221 can cause irrelevant differences in dw_loc_addr. */
29222 gcc_assert (valx1->val_class == dw_val_class_loc
29223 && valy1->val_class == dw_val_class_loc
29224 && (dwarf_split_debug_info
29225 || x->dw_loc_addr == y->dw_loc_addr));
29226 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29227 case DW_OP_implicit_value:
29228 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29229 || valx2->val_class != valy2->val_class)
29230 return false;
29231 switch (valx2->val_class)
29233 case dw_val_class_const:
29234 return valx2->v.val_int == valy2->v.val_int;
29235 case dw_val_class_vec:
29236 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29237 && valx2->v.val_vec.length == valy2->v.val_vec.length
29238 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29239 valx2->v.val_vec.elt_size
29240 * valx2->v.val_vec.length) == 0;
29241 case dw_val_class_const_double:
29242 return valx2->v.val_double.low == valy2->v.val_double.low
29243 && valx2->v.val_double.high == valy2->v.val_double.high;
29244 case dw_val_class_wide_int:
29245 return *valx2->v.val_wide == *valy2->v.val_wide;
29246 case dw_val_class_addr:
29247 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29248 default:
29249 gcc_unreachable ();
29251 case DW_OP_bregx:
29252 case DW_OP_bit_piece:
29253 return valx1->v.val_int == valy1->v.val_int
29254 && valx2->v.val_int == valy2->v.val_int;
29255 case DW_OP_addr:
29256 hash_addr:
29257 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29258 case DW_OP_GNU_addr_index:
29259 case DW_OP_GNU_const_index:
29261 rtx ax1 = valx1->val_entry->addr.rtl;
29262 rtx ay1 = valy1->val_entry->addr.rtl;
29263 return rtx_equal_p (ax1, ay1);
29265 case DW_OP_implicit_pointer:
29266 case DW_OP_GNU_implicit_pointer:
29267 return valx1->val_class == dw_val_class_die_ref
29268 && valx1->val_class == valy1->val_class
29269 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29270 && valx2->v.val_int == valy2->v.val_int;
29271 case DW_OP_entry_value:
29272 case DW_OP_GNU_entry_value:
29273 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29274 case DW_OP_const_type:
29275 case DW_OP_GNU_const_type:
29276 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29277 || valx2->val_class != valy2->val_class)
29278 return false;
29279 switch (valx2->val_class)
29281 case dw_val_class_const:
29282 return valx2->v.val_int == valy2->v.val_int;
29283 case dw_val_class_vec:
29284 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29285 && valx2->v.val_vec.length == valy2->v.val_vec.length
29286 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29287 valx2->v.val_vec.elt_size
29288 * valx2->v.val_vec.length) == 0;
29289 case dw_val_class_const_double:
29290 return valx2->v.val_double.low == valy2->v.val_double.low
29291 && valx2->v.val_double.high == valy2->v.val_double.high;
29292 case dw_val_class_wide_int:
29293 return *valx2->v.val_wide == *valy2->v.val_wide;
29294 default:
29295 gcc_unreachable ();
29297 case DW_OP_regval_type:
29298 case DW_OP_deref_type:
29299 case DW_OP_GNU_regval_type:
29300 case DW_OP_GNU_deref_type:
29301 return valx1->v.val_int == valy1->v.val_int
29302 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29303 case DW_OP_convert:
29304 case DW_OP_reinterpret:
29305 case DW_OP_GNU_convert:
29306 case DW_OP_GNU_reinterpret:
29307 if (valx1->val_class != valy1->val_class)
29308 return false;
29309 if (valx1->val_class == dw_val_class_unsigned_const)
29310 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29311 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29312 case DW_OP_GNU_parameter_ref:
29313 return valx1->val_class == dw_val_class_die_ref
29314 && valx1->val_class == valy1->val_class
29315 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29316 default:
29317 /* Other codes have no operands. */
29318 return true;
29322 /* Return true if DWARF location expressions X and Y are the same. */
29324 static inline bool
29325 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29327 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29328 if (x->dw_loc_opc != y->dw_loc_opc
29329 || x->dtprel != y->dtprel
29330 || !compare_loc_operands (x, y))
29331 break;
29332 return x == NULL && y == NULL;
29335 /* Hashtable helpers. */
29337 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29339 static inline hashval_t hash (const dw_loc_list_struct *);
29340 static inline bool equal (const dw_loc_list_struct *,
29341 const dw_loc_list_struct *);
29344 /* Return precomputed hash of location list X. */
29346 inline hashval_t
29347 loc_list_hasher::hash (const dw_loc_list_struct *x)
29349 return x->hash;
29352 /* Return true if location lists A and B are the same. */
29354 inline bool
29355 loc_list_hasher::equal (const dw_loc_list_struct *a,
29356 const dw_loc_list_struct *b)
29358 if (a == b)
29359 return 1;
29360 if (a->hash != b->hash)
29361 return 0;
29362 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29363 if (strcmp (a->begin, b->begin) != 0
29364 || strcmp (a->end, b->end) != 0
29365 || (a->section == NULL) != (b->section == NULL)
29366 || (a->section && strcmp (a->section, b->section) != 0)
29367 || !compare_locs (a->expr, b->expr))
29368 break;
29369 return a == NULL && b == NULL;
29372 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29375 /* Recursively optimize location lists referenced from DIE
29376 children and share them whenever possible. */
29378 static void
29379 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29381 dw_die_ref c;
29382 dw_attr_node *a;
29383 unsigned ix;
29384 dw_loc_list_struct **slot;
29386 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29387 if (AT_class (a) == dw_val_class_loc_list)
29389 dw_loc_list_ref list = AT_loc_list (a);
29390 /* TODO: perform some optimizations here, before hashing
29391 it and storing into the hash table. */
29392 hash_loc_list (list);
29393 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29394 if (*slot == NULL)
29395 *slot = list;
29396 else
29397 a->dw_attr_val.v.val_loc_list = *slot;
29400 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29404 /* Recursively assign each location list a unique index into the debug_addr
29405 section. */
29407 static void
29408 index_location_lists (dw_die_ref die)
29410 dw_die_ref c;
29411 dw_attr_node *a;
29412 unsigned ix;
29414 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29415 if (AT_class (a) == dw_val_class_loc_list)
29417 dw_loc_list_ref list = AT_loc_list (a);
29418 dw_loc_list_ref curr;
29419 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29421 /* Don't index an entry that has already been indexed
29422 or won't be output. */
29423 if (curr->begin_entry != NULL
29424 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29425 continue;
29427 curr->begin_entry
29428 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29432 FOR_EACH_CHILD (die, c, index_location_lists (c));
29435 /* Optimize location lists referenced from DIE
29436 children and share them whenever possible. */
29438 static void
29439 optimize_location_lists (dw_die_ref die)
29441 loc_list_hash_type htab (500);
29442 optimize_location_lists_1 (die, &htab);
29445 /* Traverse the limbo die list, and add parent/child links. The only
29446 dies without parents that should be here are concrete instances of
29447 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29448 For concrete instances, we can get the parent die from the abstract
29449 instance. */
29451 static void
29452 flush_limbo_die_list (void)
29454 limbo_die_node *node;
29456 /* get_context_die calls force_decl_die, which can put new DIEs on the
29457 limbo list in LTO mode when nested functions are put in a different
29458 partition than that of their parent function. */
29459 while ((node = limbo_die_list))
29461 dw_die_ref die = node->die;
29462 limbo_die_list = node->next;
29464 if (die->die_parent == NULL)
29466 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29468 if (origin && origin->die_parent)
29469 add_child_die (origin->die_parent, die);
29470 else if (is_cu_die (die))
29472 else if (seen_error ())
29473 /* It's OK to be confused by errors in the input. */
29474 add_child_die (comp_unit_die (), die);
29475 else
29477 /* In certain situations, the lexical block containing a
29478 nested function can be optimized away, which results
29479 in the nested function die being orphaned. Likewise
29480 with the return type of that nested function. Force
29481 this to be a child of the containing function.
29483 It may happen that even the containing function got fully
29484 inlined and optimized out. In that case we are lost and
29485 assign the empty child. This should not be big issue as
29486 the function is likely unreachable too. */
29487 gcc_assert (node->created_for);
29489 if (DECL_P (node->created_for))
29490 origin = get_context_die (DECL_CONTEXT (node->created_for));
29491 else if (TYPE_P (node->created_for))
29492 origin = scope_die_for (node->created_for, comp_unit_die ());
29493 else
29494 origin = comp_unit_die ();
29496 add_child_die (origin, die);
29502 /* Output stuff that dwarf requires at the end of every file,
29503 and generate the DWARF-2 debugging info. */
29505 static void
29506 dwarf2out_finish (const char *)
29508 comdat_type_node *ctnode;
29509 dw_die_ref main_comp_unit_die;
29510 unsigned char checksum[16];
29512 /* Flush out any latecomers to the limbo party. */
29513 flush_limbo_die_list ();
29515 if (flag_checking)
29517 verify_die (comp_unit_die ());
29518 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29519 verify_die (node->die);
29522 /* We shouldn't have any symbols with delayed asm names for
29523 DIEs generated after early finish. */
29524 gcc_assert (deferred_asm_name == NULL);
29526 gen_remaining_tmpl_value_param_die_attribute ();
29528 #if ENABLE_ASSERT_CHECKING
29530 dw_die_ref die = comp_unit_die (), c;
29531 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29533 #endif
29534 resolve_addr (comp_unit_die ());
29535 move_marked_base_types ();
29537 /* Initialize sections and labels used for actual assembler output. */
29538 init_sections_and_labels ();
29540 /* Traverse the DIE's and add sibling attributes to those DIE's that
29541 have children. */
29542 add_sibling_attributes (comp_unit_die ());
29543 limbo_die_node *node;
29544 for (node = cu_die_list; node; node = node->next)
29545 add_sibling_attributes (node->die);
29546 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29547 add_sibling_attributes (ctnode->root_die);
29549 /* When splitting DWARF info, we put some attributes in the
29550 skeleton compile_unit DIE that remains in the .o, while
29551 most attributes go in the DWO compile_unit_die. */
29552 if (dwarf_split_debug_info)
29554 limbo_die_node *cu;
29555 main_comp_unit_die = gen_compile_unit_die (NULL);
29556 if (dwarf_version >= 5)
29557 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29558 cu = limbo_die_list;
29559 gcc_assert (cu->die == main_comp_unit_die);
29560 limbo_die_list = limbo_die_list->next;
29561 cu->next = cu_die_list;
29562 cu_die_list = cu;
29564 else
29565 main_comp_unit_die = comp_unit_die ();
29567 /* Output a terminator label for the .text section. */
29568 switch_to_section (text_section);
29569 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29570 if (cold_text_section)
29572 switch_to_section (cold_text_section);
29573 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29576 /* We can only use the low/high_pc attributes if all of the code was
29577 in .text. */
29578 if (!have_multiple_function_sections
29579 || (dwarf_version < 3 && dwarf_strict))
29581 /* Don't add if the CU has no associated code. */
29582 if (text_section_used)
29583 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29584 text_end_label, true);
29586 else
29588 unsigned fde_idx;
29589 dw_fde_ref fde;
29590 bool range_list_added = false;
29592 if (text_section_used)
29593 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29594 text_end_label, &range_list_added, true);
29595 if (cold_text_section_used)
29596 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29597 cold_end_label, &range_list_added, true);
29599 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29601 if (DECL_IGNORED_P (fde->decl))
29602 continue;
29603 if (!fde->in_std_section)
29604 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29605 fde->dw_fde_end, &range_list_added,
29606 true);
29607 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29608 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29609 fde->dw_fde_second_end, &range_list_added,
29610 true);
29613 if (range_list_added)
29615 /* We need to give .debug_loc and .debug_ranges an appropriate
29616 "base address". Use zero so that these addresses become
29617 absolute. Historically, we've emitted the unexpected
29618 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29619 Emit both to give time for other tools to adapt. */
29620 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29621 if (! dwarf_strict && dwarf_version < 4)
29622 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29624 add_ranges (NULL);
29628 if (debug_info_level >= DINFO_LEVEL_TERSE)
29629 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29630 debug_line_section_label);
29632 if (have_macinfo)
29633 add_AT_macptr (comp_unit_die (),
29634 dwarf_version >= 5 ? DW_AT_macros
29635 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29636 macinfo_section_label);
29638 if (dwarf_split_debug_info)
29640 if (have_location_lists)
29642 if (dwarf_version >= 5)
29643 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29644 loc_section_label);
29645 /* optimize_location_lists calculates the size of the lists,
29646 so index them first, and assign indices to the entries.
29647 Although optimize_location_lists will remove entries from
29648 the table, it only does so for duplicates, and therefore
29649 only reduces ref_counts to 1. */
29650 index_location_lists (comp_unit_die ());
29653 if (addr_index_table != NULL)
29655 unsigned int index = 0;
29656 addr_index_table
29657 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29658 (&index);
29662 loc_list_idx = 0;
29663 if (have_location_lists)
29665 optimize_location_lists (comp_unit_die ());
29666 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29667 if (dwarf_version >= 5 && dwarf_split_debug_info)
29668 assign_location_list_indexes (comp_unit_die ());
29671 save_macinfo_strings ();
29673 if (dwarf_split_debug_info)
29675 unsigned int index = 0;
29677 /* Add attributes common to skeleton compile_units and
29678 type_units. Because these attributes include strings, it
29679 must be done before freezing the string table. Top-level
29680 skeleton die attrs are added when the skeleton type unit is
29681 created, so ensure it is created by this point. */
29682 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29683 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29686 /* Output all of the compilation units. We put the main one last so that
29687 the offsets are available to output_pubnames. */
29688 for (node = cu_die_list; node; node = node->next)
29689 output_comp_unit (node->die, 0, NULL);
29691 hash_table<comdat_type_hasher> comdat_type_table (100);
29692 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29694 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29696 /* Don't output duplicate types. */
29697 if (*slot != HTAB_EMPTY_ENTRY)
29698 continue;
29700 /* Add a pointer to the line table for the main compilation unit
29701 so that the debugger can make sense of DW_AT_decl_file
29702 attributes. */
29703 if (debug_info_level >= DINFO_LEVEL_TERSE)
29704 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29705 (!dwarf_split_debug_info
29706 ? debug_line_section_label
29707 : debug_skeleton_line_section_label));
29709 output_comdat_type_unit (ctnode);
29710 *slot = ctnode;
29713 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29714 both the main_cu and all skeleton TUs. Making this call unconditional
29715 would end up either adding a second copy of the AT_pubnames attribute, or
29716 requiring a special case in add_top_level_skeleton_die_attrs. */
29717 if (!dwarf_split_debug_info)
29718 add_AT_pubnames (comp_unit_die ());
29720 if (dwarf_split_debug_info)
29722 int mark;
29723 struct md5_ctx ctx;
29725 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29726 index_rnglists ();
29728 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29729 md5_init_ctx (&ctx);
29730 mark = 0;
29731 die_checksum (comp_unit_die (), &ctx, &mark);
29732 unmark_all_dies (comp_unit_die ());
29733 md5_finish_ctx (&ctx, checksum);
29735 if (dwarf_version < 5)
29737 /* Use the first 8 bytes of the checksum as the dwo_id,
29738 and add it to both comp-unit DIEs. */
29739 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29740 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29743 /* Add the base offset of the ranges table to the skeleton
29744 comp-unit DIE. */
29745 if (!vec_safe_is_empty (ranges_table))
29747 if (dwarf_version >= 5)
29748 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29749 ranges_base_label);
29750 else
29751 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29752 ranges_section_label);
29755 switch_to_section (debug_addr_section);
29756 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29757 output_addr_table ();
29760 /* Output the main compilation unit if non-empty or if .debug_macinfo
29761 or .debug_macro will be emitted. */
29762 output_comp_unit (comp_unit_die (), have_macinfo,
29763 dwarf_split_debug_info ? checksum : NULL);
29765 if (dwarf_split_debug_info && info_section_emitted)
29766 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29768 /* Output the abbreviation table. */
29769 if (vec_safe_length (abbrev_die_table) != 1)
29771 switch_to_section (debug_abbrev_section);
29772 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29773 output_abbrev_section ();
29776 /* Output location list section if necessary. */
29777 if (have_location_lists)
29779 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29780 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29781 /* Output the location lists info. */
29782 switch_to_section (debug_loc_section);
29783 if (dwarf_version >= 5)
29785 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29786 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29787 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29788 dw2_asm_output_data (4, 0xffffffff,
29789 "Initial length escape value indicating "
29790 "64-bit DWARF extension");
29791 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29792 "Length of Location Lists");
29793 ASM_OUTPUT_LABEL (asm_out_file, l1);
29794 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29795 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29796 dw2_asm_output_data (1, 0, "Segment Size");
29797 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29798 "Offset Entry Count");
29800 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29801 if (dwarf_version >= 5 && dwarf_split_debug_info)
29803 unsigned int save_loc_list_idx = loc_list_idx;
29804 loc_list_idx = 0;
29805 output_loclists_offsets (comp_unit_die ());
29806 gcc_assert (save_loc_list_idx == loc_list_idx);
29808 output_location_lists (comp_unit_die ());
29809 if (dwarf_version >= 5)
29810 ASM_OUTPUT_LABEL (asm_out_file, l2);
29813 output_pubtables ();
29815 /* Output the address range information if a CU (.debug_info section)
29816 was emitted. We output an empty table even if we had no functions
29817 to put in it. This because the consumer has no way to tell the
29818 difference between an empty table that we omitted and failure to
29819 generate a table that would have contained data. */
29820 if (info_section_emitted)
29822 switch_to_section (debug_aranges_section);
29823 output_aranges ();
29826 /* Output ranges section if necessary. */
29827 if (!vec_safe_is_empty (ranges_table))
29829 if (dwarf_version >= 5)
29830 output_rnglists ();
29831 else
29832 output_ranges ();
29835 /* Have to end the macro section. */
29836 if (have_macinfo)
29838 switch_to_section (debug_macinfo_section);
29839 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29840 output_macinfo ();
29841 dw2_asm_output_data (1, 0, "End compilation unit");
29844 /* Output the source line correspondence table. We must do this
29845 even if there is no line information. Otherwise, on an empty
29846 translation unit, we will generate a present, but empty,
29847 .debug_info section. IRIX 6.5 `nm' will then complain when
29848 examining the file. This is done late so that any filenames
29849 used by the debug_info section are marked as 'used'. */
29850 switch_to_section (debug_line_section);
29851 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29852 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29853 output_line_info (false);
29855 if (dwarf_split_debug_info && info_section_emitted)
29857 switch_to_section (debug_skeleton_line_section);
29858 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29859 output_line_info (true);
29862 /* If we emitted any indirect strings, output the string table too. */
29863 if (debug_str_hash || skeleton_debug_str_hash)
29864 output_indirect_strings ();
29865 if (debug_line_str_hash)
29867 switch_to_section (debug_line_str_section);
29868 const enum dwarf_form form = DW_FORM_line_strp;
29869 debug_line_str_hash->traverse<enum dwarf_form,
29870 output_indirect_string> (form);
29874 /* Perform any cleanups needed after the early debug generation pass
29875 has run. */
29877 static void
29878 dwarf2out_early_finish (const char *filename)
29880 set_early_dwarf s;
29882 /* PCH might result in DW_AT_producer string being restored from the
29883 header compilation, so always fill it with empty string initially
29884 and overwrite only here. */
29885 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
29886 producer_string = gen_producer_string ();
29887 producer->dw_attr_val.v.val_str->refcount--;
29888 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
29890 /* Add the name for the main input file now. We delayed this from
29891 dwarf2out_init to avoid complications with PCH. */
29892 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
29893 add_comp_dir_attribute (comp_unit_die ());
29895 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
29896 DW_AT_comp_dir into .debug_line_str section. */
29897 if (!DWARF2_ASM_LINE_DEBUG_INFO
29898 && dwarf_version >= 5
29899 && DWARF5_USE_DEBUG_LINE_STR)
29901 for (int i = 0; i < 2; i++)
29903 dw_attr_node *a = get_AT (comp_unit_die (),
29904 i ? DW_AT_comp_dir : DW_AT_name);
29905 if (a == NULL
29906 || AT_class (a) != dw_val_class_str
29907 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
29908 continue;
29910 if (! debug_line_str_hash)
29911 debug_line_str_hash
29912 = hash_table<indirect_string_hasher>::create_ggc (10);
29914 struct indirect_string_node *node
29915 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
29916 set_indirect_string (node);
29917 node->form = DW_FORM_line_strp;
29918 a->dw_attr_val.v.val_str->refcount--;
29919 a->dw_attr_val.v.val_str = node;
29923 /* With LTO early dwarf was really finished at compile-time, so make
29924 sure to adjust the phase after annotating the LTRANS CU DIE. */
29925 if (in_lto_p)
29927 early_dwarf_finished = true;
29928 return;
29931 /* Walk through the list of incomplete types again, trying once more to
29932 emit full debugging info for them. */
29933 retry_incomplete_types ();
29935 /* The point here is to flush out the limbo list so that it is empty
29936 and we don't need to stream it for LTO. */
29937 flush_limbo_die_list ();
29939 gen_scheduled_generic_parms_dies ();
29940 gen_remaining_tmpl_value_param_die_attribute ();
29942 /* Add DW_AT_linkage_name for all deferred DIEs. */
29943 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
29945 tree decl = node->created_for;
29946 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
29947 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
29948 ended up in deferred_asm_name before we knew it was
29949 constant and never written to disk. */
29950 && DECL_ASSEMBLER_NAME (decl))
29952 add_linkage_attr (node->die, decl);
29953 move_linkage_attr (node->die);
29956 deferred_asm_name = NULL;
29958 if (flag_eliminate_unused_debug_types)
29959 prune_unused_types ();
29961 /* Generate separate COMDAT sections for type DIEs. */
29962 if (use_debug_types)
29964 break_out_comdat_types (comp_unit_die ());
29966 /* Each new type_unit DIE was added to the limbo die list when created.
29967 Since these have all been added to comdat_type_list, clear the
29968 limbo die list. */
29969 limbo_die_list = NULL;
29971 /* For each new comdat type unit, copy declarations for incomplete
29972 types to make the new unit self-contained (i.e., no direct
29973 references to the main compile unit). */
29974 for (comdat_type_node *ctnode = comdat_type_list;
29975 ctnode != NULL; ctnode = ctnode->next)
29976 copy_decls_for_unworthy_types (ctnode->root_die);
29977 copy_decls_for_unworthy_types (comp_unit_die ());
29979 /* In the process of copying declarations from one unit to another,
29980 we may have left some declarations behind that are no longer
29981 referenced. Prune them. */
29982 prune_unused_types ();
29985 /* Generate separate CUs for each of the include files we've seen.
29986 They will go into limbo_die_list and from there to cu_die_list. */
29987 if (flag_eliminate_dwarf2_dups)
29989 gcc_assert (limbo_die_list == NULL);
29990 break_out_includes (comp_unit_die ());
29991 limbo_die_node *cu;
29992 while ((cu = limbo_die_list))
29994 limbo_die_list = cu->next;
29995 cu->next = cu_die_list;
29996 cu_die_list = cu;
30000 /* The early debug phase is now finished. */
30001 early_dwarf_finished = true;
30004 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30005 within the same process. For use by toplev::finalize. */
30007 void
30008 dwarf2out_c_finalize (void)
30010 last_var_location_insn = NULL;
30011 cached_next_real_insn = NULL;
30012 used_rtx_array = NULL;
30013 incomplete_types = NULL;
30014 decl_scope_table = NULL;
30015 debug_info_section = NULL;
30016 debug_skeleton_info_section = NULL;
30017 debug_abbrev_section = NULL;
30018 debug_skeleton_abbrev_section = NULL;
30019 debug_aranges_section = NULL;
30020 debug_addr_section = NULL;
30021 debug_macinfo_section = NULL;
30022 debug_line_section = NULL;
30023 debug_skeleton_line_section = NULL;
30024 debug_loc_section = NULL;
30025 debug_pubnames_section = NULL;
30026 debug_pubtypes_section = NULL;
30027 debug_str_section = NULL;
30028 debug_line_str_section = NULL;
30029 debug_str_dwo_section = NULL;
30030 debug_str_offsets_section = NULL;
30031 debug_ranges_section = NULL;
30032 debug_frame_section = NULL;
30033 fde_vec = NULL;
30034 debug_str_hash = NULL;
30035 debug_line_str_hash = NULL;
30036 skeleton_debug_str_hash = NULL;
30037 dw2_string_counter = 0;
30038 have_multiple_function_sections = false;
30039 text_section_used = false;
30040 cold_text_section_used = false;
30041 cold_text_section = NULL;
30042 current_unit_personality = NULL;
30044 early_dwarf = false;
30045 early_dwarf_finished = false;
30047 next_die_offset = 0;
30048 single_comp_unit_die = NULL;
30049 comdat_type_list = NULL;
30050 limbo_die_list = NULL;
30051 file_table = NULL;
30052 decl_die_table = NULL;
30053 common_block_die_table = NULL;
30054 decl_loc_table = NULL;
30055 call_arg_locations = NULL;
30056 call_arg_loc_last = NULL;
30057 call_site_count = -1;
30058 tail_call_site_count = -1;
30059 cached_dw_loc_list_table = NULL;
30060 abbrev_die_table = NULL;
30061 delete dwarf_proc_stack_usage_map;
30062 dwarf_proc_stack_usage_map = NULL;
30063 line_info_label_num = 0;
30064 cur_line_info_table = NULL;
30065 text_section_line_info = NULL;
30066 cold_text_section_line_info = NULL;
30067 separate_line_info = NULL;
30068 info_section_emitted = false;
30069 pubname_table = NULL;
30070 pubtype_table = NULL;
30071 macinfo_table = NULL;
30072 ranges_table = NULL;
30073 ranges_by_label = NULL;
30074 rnglist_idx = 0;
30075 have_location_lists = false;
30076 loclabel_num = 0;
30077 poc_label_num = 0;
30078 last_emitted_file = NULL;
30079 label_num = 0;
30080 tmpl_value_parm_die_table = NULL;
30081 generic_type_instances = NULL;
30082 frame_pointer_fb_offset = 0;
30083 frame_pointer_fb_offset_valid = false;
30084 base_types.release ();
30085 XDELETEVEC (producer_string);
30086 producer_string = NULL;
30089 #include "gt-dwarf2out.h"