2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / dwarf2out.c
blobc1b7c7b10e3cfadcdbc80eae08bcb39a8e7aa09a
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 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 "tm.h"
62 #include "rtl.h"
63 #include "alias.h"
64 #include "tree.h"
65 #include "fold-const.h"
66 #include "stringpool.h"
67 #include "stor-layout.h"
68 #include "varasm.h"
69 #include "function.h"
70 #include "emit-rtl.h"
71 #include "version.h"
72 #include "flags.h"
73 #include "regs.h"
74 #include "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "output.h"
78 #include "expmed.h"
79 #include "dojump.h"
80 #include "explow.h"
81 #include "calls.h"
82 #include "stmt.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "cgraph.h"
98 #include "ira.h"
99 #include "lra.h"
100 #include "dumpfile.h"
101 #include "opts.h"
102 #include "tree-dfa.h"
103 #include "gdb/gdb-index.h"
104 #include "rtl-iter.h"
106 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
107 static rtx_insn *last_var_location_insn;
108 static rtx_insn *cached_next_real_insn;
109 static void dwarf2out_decl (tree);
111 #ifndef XCOFF_DEBUGGING_INFO
112 #define XCOFF_DEBUGGING_INFO 0
113 #endif
115 #ifndef HAVE_XCOFF_DWARF_EXTRAS
116 #define HAVE_XCOFF_DWARF_EXTRAS 0
117 #endif
119 #ifdef VMS_DEBUGGING_INFO
120 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
122 /* Define this macro to be a nonzero value if the directory specifications
123 which are output in the debug info should end with a separator. */
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
125 /* Define this macro to evaluate to a nonzero value if GCC should refrain
126 from generating indirect strings in DWARF2 debug information, for instance
127 if your target is stuck with an old version of GDB that is unable to
128 process them properly or uses VMS Debug. */
129 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
130 #else
131 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
132 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
133 #endif
135 /* ??? Poison these here until it can be done generically. They've been
136 totally replaced in this file; make sure it stays that way. */
137 #undef DWARF2_UNWIND_INFO
138 #undef DWARF2_FRAME_INFO
139 #if (GCC_VERSION >= 3000)
140 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
141 #endif
143 /* The size of the target's pointer type. */
144 #ifndef PTR_SIZE
145 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
146 #endif
148 /* Array of RTXes referenced by the debugging information, which therefore
149 must be kept around forever. */
150 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
152 /* A pointer to the base of a list of incomplete types which might be
153 completed at some later time. incomplete_types_list needs to be a
154 vec<tree, va_gc> *because we want to tell the garbage collector about
155 it. */
156 static GTY(()) vec<tree, va_gc> *incomplete_types;
158 /* A pointer to the base of a table of references to declaration
159 scopes. This table is a display which tracks the nesting
160 of declaration scopes at the current scope and containing
161 scopes. This table is used to find the proper place to
162 define type declaration DIE's. */
163 static GTY(()) vec<tree, va_gc> *decl_scope_table;
165 /* Pointers to various DWARF2 sections. */
166 static GTY(()) section *debug_info_section;
167 static GTY(()) section *debug_skeleton_info_section;
168 static GTY(()) section *debug_abbrev_section;
169 static GTY(()) section *debug_skeleton_abbrev_section;
170 static GTY(()) section *debug_aranges_section;
171 static GTY(()) section *debug_addr_section;
172 static GTY(()) section *debug_macinfo_section;
173 static GTY(()) section *debug_line_section;
174 static GTY(()) section *debug_skeleton_line_section;
175 static GTY(()) section *debug_loc_section;
176 static GTY(()) section *debug_pubnames_section;
177 static GTY(()) section *debug_pubtypes_section;
178 static GTY(()) section *debug_str_section;
179 static GTY(()) section *debug_str_dwo_section;
180 static GTY(()) section *debug_str_offsets_section;
181 static GTY(()) section *debug_ranges_section;
182 static GTY(()) section *debug_frame_section;
184 /* Maximum size (in bytes) of an artificially generated label. */
185 #define MAX_ARTIFICIAL_LABEL_BYTES 30
187 /* According to the (draft) DWARF 3 specification, the initial length
188 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
189 bytes are 0xffffffff, followed by the length stored in the next 8
190 bytes.
192 However, the SGI/MIPS ABI uses an initial length which is equal to
193 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
195 #ifndef DWARF_INITIAL_LENGTH_SIZE
196 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
197 #endif
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
229 typedef const char *compare_type;
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
237 /* With split_debug_info, both the comp_dir and dwo_name go in the
238 main object file, rather than the dwo, similar to the force_direct
239 parameter elsewhere but with additional complications:
241 1) The string is needed in both the main object file and the dwo.
242 That is, the comp_dir and dwo_name will appear in both places.
244 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
245 DW_FORM_GNU_str_index.
247 3) GCC chooses the form to use late, depending on the size and
248 reference count.
250 Rather than forcing the all debug string handling functions and
251 callers to deal with these complications, simply use a separate,
252 special-cased string table for any attribute that should go in the
253 main object file. This limits the complexity to just the places
254 that need it. */
256 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258 static GTY(()) int dw2_string_counter;
260 /* True if the compilation unit places functions in more than one section. */
261 static GTY(()) bool have_multiple_function_sections = false;
263 /* Whether the default text and cold text sections have been used at all. */
265 static GTY(()) bool text_section_used = false;
266 static GTY(()) bool cold_text_section_used = false;
268 /* The default cold text section. */
269 static GTY(()) section *cold_text_section;
271 /* The DIE for C++14 'auto' in a function return type. */
272 static GTY(()) dw_die_ref auto_die;
274 /* The DIE for C++14 'decltype(auto)' in a function return type. */
275 static GTY(()) dw_die_ref decltype_auto_die;
277 /* Forward declarations for functions defined in this file. */
279 static char *stripattributes (const char *);
280 static void output_call_frame_info (int);
281 static void dwarf2out_note_section_used (void);
283 /* Personality decl of current unit. Used only when assembler does not support
284 personality CFI. */
285 static GTY(()) rtx current_unit_personality;
287 /* Data and reference forms for relocatable data. */
288 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
289 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
291 #ifndef DEBUG_FRAME_SECTION
292 #define DEBUG_FRAME_SECTION ".debug_frame"
293 #endif
295 #ifndef FUNC_BEGIN_LABEL
296 #define FUNC_BEGIN_LABEL "LFB"
297 #endif
299 #ifndef FUNC_END_LABEL
300 #define FUNC_END_LABEL "LFE"
301 #endif
303 #ifndef PROLOGUE_END_LABEL
304 #define PROLOGUE_END_LABEL "LPE"
305 #endif
307 #ifndef EPILOGUE_BEGIN_LABEL
308 #define EPILOGUE_BEGIN_LABEL "LEB"
309 #endif
311 #ifndef FRAME_BEGIN_LABEL
312 #define FRAME_BEGIN_LABEL "Lframe"
313 #endif
314 #define CIE_AFTER_SIZE_LABEL "LSCIE"
315 #define CIE_END_LABEL "LECIE"
316 #define FDE_LABEL "LSFDE"
317 #define FDE_AFTER_SIZE_LABEL "LASFDE"
318 #define FDE_END_LABEL "LEFDE"
319 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
320 #define LINE_NUMBER_END_LABEL "LELT"
321 #define LN_PROLOG_AS_LABEL "LASLTP"
322 #define LN_PROLOG_END_LABEL "LELTP"
323 #define DIE_LABEL_PREFIX "DW"
325 /* Match the base name of a file to the base name of a compilation unit. */
327 static int
328 matches_main_base (const char *path)
330 /* Cache the last query. */
331 static const char *last_path = NULL;
332 static int last_match = 0;
333 if (path != last_path)
335 const char *base;
336 int length = base_of_path (path, &base);
337 last_path = path;
338 last_match = (length == main_input_baselength
339 && memcmp (base, main_input_basename, length) == 0);
341 return last_match;
344 #ifdef DEBUG_DEBUG_STRUCT
346 static int
347 dump_struct_debug (tree type, enum debug_info_usage usage,
348 enum debug_struct_file criterion, int generic,
349 int matches, int result)
351 /* Find the type name. */
352 tree type_decl = TYPE_STUB_DECL (type);
353 tree t = type_decl;
354 const char *name = 0;
355 if (TREE_CODE (t) == TYPE_DECL)
356 t = DECL_NAME (t);
357 if (t)
358 name = IDENTIFIER_POINTER (t);
360 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
361 criterion,
362 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
363 matches ? "bas" : "hdr",
364 generic ? "gen" : "ord",
365 usage == DINFO_USAGE_DFN ? ";" :
366 usage == DINFO_USAGE_DIR_USE ? "." : "*",
367 result,
368 (void*) type_decl, name);
369 return result;
371 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
372 dump_struct_debug (type, usage, criterion, generic, matches, result)
374 #else
376 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
377 (result)
379 #endif
381 /* Get the number of HOST_WIDE_INTs needed to represent the precision
382 of the number. */
384 static unsigned int
385 get_full_len (const wide_int &op)
387 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
388 / HOST_BITS_PER_WIDE_INT);
391 static bool
392 should_emit_struct_debug (tree type, enum debug_info_usage usage)
394 enum debug_struct_file criterion;
395 tree type_decl;
396 bool generic = lang_hooks.types.generic_p (type);
398 if (generic)
399 criterion = debug_struct_generic[usage];
400 else
401 criterion = debug_struct_ordinary[usage];
403 if (criterion == DINFO_STRUCT_FILE_NONE)
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
405 if (criterion == DINFO_STRUCT_FILE_ANY)
406 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
408 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
410 if (type_decl != NULL)
412 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
413 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
415 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
416 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
419 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
422 /* Return a pointer to a copy of the section string name S with all
423 attributes stripped off, and an asterisk prepended (for assemble_name). */
425 static inline char *
426 stripattributes (const char *s)
428 char *stripped = XNEWVEC (char, strlen (s) + 2);
429 char *p = stripped;
431 *p++ = '*';
433 while (*s && *s != ',')
434 *p++ = *s++;
436 *p = '\0';
437 return stripped;
440 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
441 switch to the data section instead, and write out a synthetic start label
442 for collect2 the first time around. */
444 static void
445 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
447 if (eh_frame_section == 0)
449 int flags;
451 if (EH_TABLES_CAN_BE_READ_ONLY)
453 int fde_encoding;
454 int per_encoding;
455 int lsda_encoding;
457 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
458 /*global=*/0);
459 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
460 /*global=*/1);
461 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
462 /*global=*/0);
463 flags = ((! flag_pic
464 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
465 && (fde_encoding & 0x70) != DW_EH_PE_aligned
466 && (per_encoding & 0x70) != DW_EH_PE_absptr
467 && (per_encoding & 0x70) != DW_EH_PE_aligned
468 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
469 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
470 ? 0 : SECTION_WRITE);
472 else
473 flags = SECTION_WRITE;
475 #ifdef EH_FRAME_SECTION_NAME
476 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
477 #else
478 eh_frame_section = ((flags == SECTION_WRITE)
479 ? data_section : readonly_data_section);
480 #endif /* EH_FRAME_SECTION_NAME */
483 switch_to_section (eh_frame_section);
485 #ifdef EH_FRAME_THROUGH_COLLECT2
486 /* We have no special eh_frame section. Emit special labels to guide
487 collect2. */
488 if (!back)
490 tree label = get_file_function_name ("F");
491 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
492 targetm.asm_out.globalize_label (asm_out_file,
493 IDENTIFIER_POINTER (label));
494 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
496 #endif
499 /* Switch [BACK] to the eh or debug frame table section, depending on
500 FOR_EH. */
502 static void
503 switch_to_frame_table_section (int for_eh, bool back)
505 if (for_eh)
506 switch_to_eh_frame_section (back);
507 else
509 if (!debug_frame_section)
510 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
511 SECTION_DEBUG, NULL);
512 switch_to_section (debug_frame_section);
516 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
518 enum dw_cfi_oprnd_type
519 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
521 switch (cfi)
523 case DW_CFA_nop:
524 case DW_CFA_GNU_window_save:
525 case DW_CFA_remember_state:
526 case DW_CFA_restore_state:
527 return dw_cfi_oprnd_unused;
529 case DW_CFA_set_loc:
530 case DW_CFA_advance_loc1:
531 case DW_CFA_advance_loc2:
532 case DW_CFA_advance_loc4:
533 case DW_CFA_MIPS_advance_loc8:
534 return dw_cfi_oprnd_addr;
536 case DW_CFA_offset:
537 case DW_CFA_offset_extended:
538 case DW_CFA_def_cfa:
539 case DW_CFA_offset_extended_sf:
540 case DW_CFA_def_cfa_sf:
541 case DW_CFA_restore:
542 case DW_CFA_restore_extended:
543 case DW_CFA_undefined:
544 case DW_CFA_same_value:
545 case DW_CFA_def_cfa_register:
546 case DW_CFA_register:
547 case DW_CFA_expression:
548 return dw_cfi_oprnd_reg_num;
550 case DW_CFA_def_cfa_offset:
551 case DW_CFA_GNU_args_size:
552 case DW_CFA_def_cfa_offset_sf:
553 return dw_cfi_oprnd_offset;
555 case DW_CFA_def_cfa_expression:
556 return dw_cfi_oprnd_loc;
558 default:
559 gcc_unreachable ();
563 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
565 enum dw_cfi_oprnd_type
566 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
568 switch (cfi)
570 case DW_CFA_def_cfa:
571 case DW_CFA_def_cfa_sf:
572 case DW_CFA_offset:
573 case DW_CFA_offset_extended_sf:
574 case DW_CFA_offset_extended:
575 return dw_cfi_oprnd_offset;
577 case DW_CFA_register:
578 return dw_cfi_oprnd_reg_num;
580 case DW_CFA_expression:
581 return dw_cfi_oprnd_loc;
583 default:
584 return dw_cfi_oprnd_unused;
588 /* Output one FDE. */
590 static void
591 output_fde (dw_fde_ref fde, bool for_eh, bool second,
592 char *section_start_label, int fde_encoding, char *augmentation,
593 bool any_lsda_needed, int lsda_encoding)
595 const char *begin, *end;
596 static unsigned int j;
597 char l1[20], l2[20];
599 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
600 /* empty */ 0);
601 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
602 for_eh + j);
603 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
604 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
605 if (!XCOFF_DEBUGGING_INFO || for_eh)
607 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
608 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
609 " indicating 64-bit DWARF extension");
610 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
611 "FDE Length");
613 ASM_OUTPUT_LABEL (asm_out_file, l1);
615 if (for_eh)
616 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
617 else
618 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
619 debug_frame_section, "FDE CIE offset");
621 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
622 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
624 if (for_eh)
626 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
627 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
628 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
629 "FDE initial location");
630 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
631 end, begin, "FDE address range");
633 else
635 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
636 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
639 if (augmentation[0])
641 if (any_lsda_needed)
643 int size = size_of_encoded_value (lsda_encoding);
645 if (lsda_encoding == DW_EH_PE_aligned)
647 int offset = ( 4 /* Length */
648 + 4 /* CIE offset */
649 + 2 * size_of_encoded_value (fde_encoding)
650 + 1 /* Augmentation size */ );
651 int pad = -offset & (PTR_SIZE - 1);
653 size += pad;
654 gcc_assert (size_of_uleb128 (size) == 1);
657 dw2_asm_output_data_uleb128 (size, "Augmentation size");
659 if (fde->uses_eh_lsda)
661 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
662 fde->funcdef_number);
663 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
664 gen_rtx_SYMBOL_REF (Pmode, l1),
665 false,
666 "Language Specific Data Area");
668 else
670 if (lsda_encoding == DW_EH_PE_aligned)
671 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
672 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
673 "Language Specific Data Area (none)");
676 else
677 dw2_asm_output_data_uleb128 (0, "Augmentation size");
680 /* Loop through the Call Frame Instructions associated with this FDE. */
681 fde->dw_fde_current_label = begin;
683 size_t from, until, i;
685 from = 0;
686 until = vec_safe_length (fde->dw_fde_cfi);
688 if (fde->dw_fde_second_begin == NULL)
690 else if (!second)
691 until = fde->dw_fde_switch_cfi_index;
692 else
693 from = fde->dw_fde_switch_cfi_index;
695 for (i = from; i < until; i++)
696 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
699 /* If we are to emit a ref/link from function bodies to their frame tables,
700 do it now. This is typically performed to make sure that tables
701 associated with functions are dragged with them and not discarded in
702 garbage collecting links. We need to do this on a per function basis to
703 cope with -ffunction-sections. */
705 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
706 /* Switch to the function section, emit the ref to the tables, and
707 switch *back* into the table section. */
708 switch_to_section (function_section (fde->decl));
709 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
710 switch_to_frame_table_section (for_eh, true);
711 #endif
713 /* Pad the FDE out to an address sized boundary. */
714 ASM_OUTPUT_ALIGN (asm_out_file,
715 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
716 ASM_OUTPUT_LABEL (asm_out_file, l2);
718 j += 2;
721 /* Return true if frame description entry FDE is needed for EH. */
723 static bool
724 fde_needed_for_eh_p (dw_fde_ref fde)
726 if (flag_asynchronous_unwind_tables)
727 return true;
729 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
730 return true;
732 if (fde->uses_eh_lsda)
733 return true;
735 /* If exceptions are enabled, we have collected nothrow info. */
736 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
737 return false;
739 return true;
742 /* Output the call frame information used to record information
743 that relates to calculating the frame pointer, and records the
744 location of saved registers. */
746 static void
747 output_call_frame_info (int for_eh)
749 unsigned int i;
750 dw_fde_ref fde;
751 dw_cfi_ref cfi;
752 char l1[20], l2[20], section_start_label[20];
753 bool any_lsda_needed = false;
754 char augmentation[6];
755 int augmentation_size;
756 int fde_encoding = DW_EH_PE_absptr;
757 int per_encoding = DW_EH_PE_absptr;
758 int lsda_encoding = DW_EH_PE_absptr;
759 int return_reg;
760 rtx personality = NULL;
761 int dw_cie_version;
763 /* Don't emit a CIE if there won't be any FDEs. */
764 if (!fde_vec)
765 return;
767 /* Nothing to do if the assembler's doing it all. */
768 if (dwarf2out_do_cfi_asm ())
769 return;
771 /* If we don't have any functions we'll want to unwind out of, don't emit
772 any EH unwind information. If we make FDEs linkonce, we may have to
773 emit an empty label for an FDE that wouldn't otherwise be emitted. We
774 want to avoid having an FDE kept around when the function it refers to
775 is discarded. Example where this matters: a primary function template
776 in C++ requires EH information, an explicit specialization doesn't. */
777 if (for_eh)
779 bool any_eh_needed = false;
781 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
783 if (fde->uses_eh_lsda)
784 any_eh_needed = any_lsda_needed = true;
785 else if (fde_needed_for_eh_p (fde))
786 any_eh_needed = true;
787 else if (TARGET_USES_WEAK_UNWIND_INFO)
788 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
791 if (!any_eh_needed)
792 return;
795 /* We're going to be generating comments, so turn on app. */
796 if (flag_debug_asm)
797 app_enable ();
799 /* Switch to the proper frame section, first time. */
800 switch_to_frame_table_section (for_eh, false);
802 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
803 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
805 /* Output the CIE. */
806 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
807 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
808 if (!XCOFF_DEBUGGING_INFO || for_eh)
810 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
811 dw2_asm_output_data (4, 0xffffffff,
812 "Initial length escape value indicating 64-bit DWARF extension");
813 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
814 "Length of Common Information Entry");
816 ASM_OUTPUT_LABEL (asm_out_file, l1);
818 /* Now that the CIE pointer is PC-relative for EH,
819 use 0 to identify the CIE. */
820 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
821 (for_eh ? 0 : DWARF_CIE_ID),
822 "CIE Identifier Tag");
824 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
825 use CIE version 1, unless that would produce incorrect results
826 due to overflowing the return register column. */
827 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
828 dw_cie_version = 1;
829 if (return_reg >= 256 || dwarf_version > 2)
830 dw_cie_version = 3;
831 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
833 augmentation[0] = 0;
834 augmentation_size = 0;
836 personality = current_unit_personality;
837 if (for_eh)
839 char *p;
841 /* Augmentation:
842 z Indicates that a uleb128 is present to size the
843 augmentation section.
844 L Indicates the encoding (and thus presence) of
845 an LSDA pointer in the FDE augmentation.
846 R Indicates a non-default pointer encoding for
847 FDE code pointers.
848 P Indicates the presence of an encoding + language
849 personality routine in the CIE augmentation. */
851 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
852 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
853 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
855 p = augmentation + 1;
856 if (personality)
858 *p++ = 'P';
859 augmentation_size += 1 + size_of_encoded_value (per_encoding);
860 assemble_external_libcall (personality);
862 if (any_lsda_needed)
864 *p++ = 'L';
865 augmentation_size += 1;
867 if (fde_encoding != DW_EH_PE_absptr)
869 *p++ = 'R';
870 augmentation_size += 1;
872 if (p > augmentation + 1)
874 augmentation[0] = 'z';
875 *p = '\0';
878 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
879 if (personality && per_encoding == DW_EH_PE_aligned)
881 int offset = ( 4 /* Length */
882 + 4 /* CIE Id */
883 + 1 /* CIE version */
884 + strlen (augmentation) + 1 /* Augmentation */
885 + size_of_uleb128 (1) /* Code alignment */
886 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
887 + 1 /* RA column */
888 + 1 /* Augmentation size */
889 + 1 /* Personality encoding */ );
890 int pad = -offset & (PTR_SIZE - 1);
892 augmentation_size += pad;
894 /* Augmentations should be small, so there's scarce need to
895 iterate for a solution. Die if we exceed one uleb128 byte. */
896 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
900 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
901 if (dw_cie_version >= 4)
903 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
904 dw2_asm_output_data (1, 0, "CIE Segment Size");
906 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
907 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
908 "CIE Data Alignment Factor");
910 if (dw_cie_version == 1)
911 dw2_asm_output_data (1, return_reg, "CIE RA Column");
912 else
913 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
915 if (augmentation[0])
917 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
918 if (personality)
920 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
921 eh_data_format_name (per_encoding));
922 dw2_asm_output_encoded_addr_rtx (per_encoding,
923 personality,
924 true, NULL);
927 if (any_lsda_needed)
928 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
929 eh_data_format_name (lsda_encoding));
931 if (fde_encoding != DW_EH_PE_absptr)
932 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
933 eh_data_format_name (fde_encoding));
936 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
937 output_cfi (cfi, NULL, for_eh);
939 /* Pad the CIE out to an address sized boundary. */
940 ASM_OUTPUT_ALIGN (asm_out_file,
941 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
942 ASM_OUTPUT_LABEL (asm_out_file, l2);
944 /* Loop through all of the FDE's. */
945 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
947 unsigned int k;
949 /* Don't emit EH unwind info for leaf functions that don't need it. */
950 if (for_eh && !fde_needed_for_eh_p (fde))
951 continue;
953 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
954 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
955 augmentation, any_lsda_needed, lsda_encoding);
958 if (for_eh && targetm.terminate_dw2_eh_frame_info)
959 dw2_asm_output_data (4, 0, "End of Table");
961 /* Turn off app to make assembly quicker. */
962 if (flag_debug_asm)
963 app_disable ();
966 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
968 static void
969 dwarf2out_do_cfi_startproc (bool second)
971 int enc;
972 rtx ref;
973 rtx personality = get_personality_function (current_function_decl);
975 fprintf (asm_out_file, "\t.cfi_startproc\n");
977 if (personality)
979 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
980 ref = personality;
982 /* ??? The GAS support isn't entirely consistent. We have to
983 handle indirect support ourselves, but PC-relative is done
984 in the assembler. Further, the assembler can't handle any
985 of the weirder relocation types. */
986 if (enc & DW_EH_PE_indirect)
987 ref = dw2_force_const_mem (ref, true);
989 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
990 output_addr_const (asm_out_file, ref);
991 fputc ('\n', asm_out_file);
994 if (crtl->uses_eh_lsda)
996 char lab[20];
998 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
999 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1000 current_function_funcdef_no);
1001 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1002 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1004 if (enc & DW_EH_PE_indirect)
1005 ref = dw2_force_const_mem (ref, true);
1007 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1008 output_addr_const (asm_out_file, ref);
1009 fputc ('\n', asm_out_file);
1013 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1014 this allocation may be done before pass_final. */
1016 dw_fde_ref
1017 dwarf2out_alloc_current_fde (void)
1019 dw_fde_ref fde;
1021 fde = ggc_cleared_alloc<dw_fde_node> ();
1022 fde->decl = current_function_decl;
1023 fde->funcdef_number = current_function_funcdef_no;
1024 fde->fde_index = vec_safe_length (fde_vec);
1025 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1026 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1027 fde->nothrow = crtl->nothrow;
1028 fde->drap_reg = INVALID_REGNUM;
1029 fde->vdrap_reg = INVALID_REGNUM;
1031 /* Record the FDE associated with this function. */
1032 cfun->fde = fde;
1033 vec_safe_push (fde_vec, fde);
1035 return fde;
1038 /* Output a marker (i.e. a label) for the beginning of a function, before
1039 the prologue. */
1041 void
1042 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1043 const char *file ATTRIBUTE_UNUSED)
1045 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1046 char * dup_label;
1047 dw_fde_ref fde;
1048 section *fnsec;
1049 bool do_frame;
1051 current_function_func_begin_label = NULL;
1053 do_frame = dwarf2out_do_frame ();
1055 /* ??? current_function_func_begin_label is also used by except.c for
1056 call-site information. We must emit this label if it might be used. */
1057 if (!do_frame
1058 && (!flag_exceptions
1059 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1060 return;
1062 fnsec = function_section (current_function_decl);
1063 switch_to_section (fnsec);
1064 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1065 current_function_funcdef_no);
1066 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1067 current_function_funcdef_no);
1068 dup_label = xstrdup (label);
1069 current_function_func_begin_label = dup_label;
1071 /* We can elide the fde allocation if we're not emitting debug info. */
1072 if (!do_frame)
1073 return;
1075 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1076 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1077 would include pass_dwarf2_frame. If we've not created the FDE yet,
1078 do so now. */
1079 fde = cfun->fde;
1080 if (fde == NULL)
1081 fde = dwarf2out_alloc_current_fde ();
1083 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1084 fde->dw_fde_begin = dup_label;
1085 fde->dw_fde_current_label = dup_label;
1086 fde->in_std_section = (fnsec == text_section
1087 || (cold_text_section && fnsec == cold_text_section));
1089 /* We only want to output line number information for the genuine dwarf2
1090 prologue case, not the eh frame case. */
1091 #ifdef DWARF2_DEBUGGING_INFO
1092 if (file)
1093 dwarf2out_source_line (line, file, 0, true);
1094 #endif
1096 if (dwarf2out_do_cfi_asm ())
1097 dwarf2out_do_cfi_startproc (false);
1098 else
1100 rtx personality = get_personality_function (current_function_decl);
1101 if (!current_unit_personality)
1102 current_unit_personality = personality;
1104 /* We cannot keep a current personality per function as without CFI
1105 asm, at the point where we emit the CFI data, there is no current
1106 function anymore. */
1107 if (personality && current_unit_personality != personality)
1108 sorry ("multiple EH personalities are supported only with assemblers "
1109 "supporting .cfi_personality directive");
1113 /* Output a marker (i.e. a label) for the end of the generated code
1114 for a function prologue. This gets called *after* the prologue code has
1115 been generated. */
1117 void
1118 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1119 const char *file ATTRIBUTE_UNUSED)
1121 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1123 /* Output a label to mark the endpoint of the code generated for this
1124 function. */
1125 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1126 current_function_funcdef_no);
1127 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1128 current_function_funcdef_no);
1129 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1132 /* Output a marker (i.e. a label) for the beginning of the generated code
1133 for a function epilogue. This gets called *before* the prologue code has
1134 been generated. */
1136 void
1137 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1138 const char *file ATTRIBUTE_UNUSED)
1140 dw_fde_ref fde = cfun->fde;
1141 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1143 if (fde->dw_fde_vms_begin_epilogue)
1144 return;
1146 /* Output a label to mark the endpoint of the code generated for this
1147 function. */
1148 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1149 current_function_funcdef_no);
1150 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1151 current_function_funcdef_no);
1152 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1155 /* Output a marker (i.e. a label) for the absolute end of the generated code
1156 for a function definition. This gets called *after* the epilogue code has
1157 been generated. */
1159 void
1160 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1161 const char *file ATTRIBUTE_UNUSED)
1163 dw_fde_ref fde;
1164 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1166 last_var_location_insn = NULL;
1167 cached_next_real_insn = NULL;
1169 if (dwarf2out_do_cfi_asm ())
1170 fprintf (asm_out_file, "\t.cfi_endproc\n");
1172 /* Output a label to mark the endpoint of the code generated for this
1173 function. */
1174 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1175 current_function_funcdef_no);
1176 ASM_OUTPUT_LABEL (asm_out_file, label);
1177 fde = cfun->fde;
1178 gcc_assert (fde != NULL);
1179 if (fde->dw_fde_second_begin == NULL)
1180 fde->dw_fde_end = xstrdup (label);
1183 void
1184 dwarf2out_frame_finish (void)
1186 /* Output call frame information. */
1187 if (targetm.debug_unwind_info () == UI_DWARF2)
1188 output_call_frame_info (0);
1190 /* Output another copy for the unwinder. */
1191 if ((flag_unwind_tables || flag_exceptions)
1192 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1193 output_call_frame_info (1);
1196 /* Note that the current function section is being used for code. */
1198 static void
1199 dwarf2out_note_section_used (void)
1201 section *sec = current_function_section ();
1202 if (sec == text_section)
1203 text_section_used = true;
1204 else if (sec == cold_text_section)
1205 cold_text_section_used = true;
1208 static void var_location_switch_text_section (void);
1209 static void set_cur_line_info_table (section *);
1211 void
1212 dwarf2out_switch_text_section (void)
1214 section *sect;
1215 dw_fde_ref fde = cfun->fde;
1217 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1219 if (!in_cold_section_p)
1221 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1222 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1223 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1225 else
1227 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1228 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1229 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1231 have_multiple_function_sections = true;
1233 /* There is no need to mark used sections when not debugging. */
1234 if (cold_text_section != NULL)
1235 dwarf2out_note_section_used ();
1237 if (dwarf2out_do_cfi_asm ())
1238 fprintf (asm_out_file, "\t.cfi_endproc\n");
1240 /* Now do the real section switch. */
1241 sect = current_function_section ();
1242 switch_to_section (sect);
1244 fde->second_in_std_section
1245 = (sect == text_section
1246 || (cold_text_section && sect == cold_text_section));
1248 if (dwarf2out_do_cfi_asm ())
1249 dwarf2out_do_cfi_startproc (true);
1251 var_location_switch_text_section ();
1253 if (cold_text_section != NULL)
1254 set_cur_line_info_table (sect);
1257 /* And now, the subset of the debugging information support code necessary
1258 for emitting location expressions. */
1260 /* Data about a single source file. */
1261 struct GTY((for_user)) dwarf_file_data {
1262 const char * filename;
1263 int emitted_number;
1266 /* Describe an entry into the .debug_addr section. */
1268 enum ate_kind {
1269 ate_kind_rtx,
1270 ate_kind_rtx_dtprel,
1271 ate_kind_label
1274 struct GTY((for_user)) addr_table_entry {
1275 enum ate_kind kind;
1276 unsigned int refcount;
1277 unsigned int index;
1278 union addr_table_entry_struct_union
1280 rtx GTY ((tag ("0"))) rtl;
1281 char * GTY ((tag ("1"))) label;
1283 GTY ((desc ("%1.kind"))) addr;
1286 /* Location lists are ranges + location descriptions for that range,
1287 so you can track variables that are in different places over
1288 their entire life. */
1289 typedef struct GTY(()) dw_loc_list_struct {
1290 dw_loc_list_ref dw_loc_next;
1291 const char *begin; /* Label and addr_entry for start of range */
1292 addr_table_entry *begin_entry;
1293 const char *end; /* Label for end of range */
1294 char *ll_symbol; /* Label for beginning of location list.
1295 Only on head of list */
1296 const char *section; /* Section this loclist is relative to */
1297 dw_loc_descr_ref expr;
1298 hashval_t hash;
1299 /* True if all addresses in this and subsequent lists are known to be
1300 resolved. */
1301 bool resolved_addr;
1302 /* True if this list has been replaced by dw_loc_next. */
1303 bool replaced;
1304 bool emitted;
1305 /* True if the range should be emitted even if begin and end
1306 are the same. */
1307 bool force;
1308 } dw_loc_list_node;
1310 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1312 /* Convert a DWARF stack opcode into its string name. */
1314 static const char *
1315 dwarf_stack_op_name (unsigned int op)
1317 const char *name = get_DW_OP_name (op);
1319 if (name != NULL)
1320 return name;
1322 return "OP_<unknown>";
1325 /* Return a pointer to a newly allocated location description. Location
1326 descriptions are simple expression terms that can be strung
1327 together to form more complicated location (address) descriptions. */
1329 static inline dw_loc_descr_ref
1330 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1331 unsigned HOST_WIDE_INT oprnd2)
1333 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1335 descr->dw_loc_opc = op;
1336 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1337 descr->dw_loc_oprnd1.val_entry = NULL;
1338 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1339 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1340 descr->dw_loc_oprnd2.val_entry = NULL;
1341 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1343 return descr;
1346 /* Return a pointer to a newly allocated location description for
1347 REG and OFFSET. */
1349 static inline dw_loc_descr_ref
1350 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1352 if (reg <= 31)
1353 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1354 offset, 0);
1355 else
1356 return new_loc_descr (DW_OP_bregx, reg, offset);
1359 /* Add a location description term to a location description expression. */
1361 static inline void
1362 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1364 dw_loc_descr_ref *d;
1366 /* Find the end of the chain. */
1367 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1370 *d = descr;
1373 /* Compare two location operands for exact equality. */
1375 static bool
1376 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1378 if (a->val_class != b->val_class)
1379 return false;
1380 switch (a->val_class)
1382 case dw_val_class_none:
1383 return true;
1384 case dw_val_class_addr:
1385 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1387 case dw_val_class_offset:
1388 case dw_val_class_unsigned_const:
1389 case dw_val_class_const:
1390 case dw_val_class_range_list:
1391 case dw_val_class_lineptr:
1392 case dw_val_class_macptr:
1393 /* These are all HOST_WIDE_INT, signed or unsigned. */
1394 return a->v.val_unsigned == b->v.val_unsigned;
1396 case dw_val_class_loc:
1397 return a->v.val_loc == b->v.val_loc;
1398 case dw_val_class_loc_list:
1399 return a->v.val_loc_list == b->v.val_loc_list;
1400 case dw_val_class_die_ref:
1401 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1402 case dw_val_class_fde_ref:
1403 return a->v.val_fde_index == b->v.val_fde_index;
1404 case dw_val_class_lbl_id:
1405 case dw_val_class_high_pc:
1406 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1407 case dw_val_class_str:
1408 return a->v.val_str == b->v.val_str;
1409 case dw_val_class_flag:
1410 return a->v.val_flag == b->v.val_flag;
1411 case dw_val_class_file:
1412 return a->v.val_file == b->v.val_file;
1413 case dw_val_class_decl_ref:
1414 return a->v.val_decl_ref == b->v.val_decl_ref;
1416 case dw_val_class_const_double:
1417 return (a->v.val_double.high == b->v.val_double.high
1418 && a->v.val_double.low == b->v.val_double.low);
1420 case dw_val_class_wide_int:
1421 return *a->v.val_wide == *b->v.val_wide;
1423 case dw_val_class_vec:
1425 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1426 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1428 return (a_len == b_len
1429 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1432 case dw_val_class_data8:
1433 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1435 case dw_val_class_vms_delta:
1436 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1437 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1439 gcc_unreachable ();
1442 /* Compare two location atoms for exact equality. */
1444 static bool
1445 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1447 if (a->dw_loc_opc != b->dw_loc_opc)
1448 return false;
1450 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1451 address size, but since we always allocate cleared storage it
1452 should be zero for other types of locations. */
1453 if (a->dtprel != b->dtprel)
1454 return false;
1456 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1457 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1460 /* Compare two complete location expressions for exact equality. */
1462 bool
1463 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1465 while (1)
1467 if (a == b)
1468 return true;
1469 if (a == NULL || b == NULL)
1470 return false;
1471 if (!loc_descr_equal_p_1 (a, b))
1472 return false;
1474 a = a->dw_loc_next;
1475 b = b->dw_loc_next;
1480 /* Add a constant OFFSET to a location expression. */
1482 static void
1483 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1485 dw_loc_descr_ref loc;
1486 HOST_WIDE_INT *p;
1488 gcc_assert (*list_head != NULL);
1490 if (!offset)
1491 return;
1493 /* Find the end of the chain. */
1494 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1497 p = NULL;
1498 if (loc->dw_loc_opc == DW_OP_fbreg
1499 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1500 p = &loc->dw_loc_oprnd1.v.val_int;
1501 else if (loc->dw_loc_opc == DW_OP_bregx)
1502 p = &loc->dw_loc_oprnd2.v.val_int;
1504 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1505 offset. Don't optimize if an signed integer overflow would happen. */
1506 if (p != NULL
1507 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1508 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1509 *p += offset;
1511 else if (offset > 0)
1512 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1514 else
1516 loc->dw_loc_next = int_loc_descriptor (-offset);
1517 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1521 /* Add a constant OFFSET to a location list. */
1523 static void
1524 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1526 dw_loc_list_ref d;
1527 for (d = list_head; d != NULL; d = d->dw_loc_next)
1528 loc_descr_plus_const (&d->expr, offset);
1531 #define DWARF_REF_SIZE \
1532 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1534 static unsigned long int get_base_type_offset (dw_die_ref);
1536 /* Return the size of a location descriptor. */
1538 static unsigned long
1539 size_of_loc_descr (dw_loc_descr_ref loc)
1541 unsigned long size = 1;
1543 switch (loc->dw_loc_opc)
1545 case DW_OP_addr:
1546 size += DWARF2_ADDR_SIZE;
1547 break;
1548 case DW_OP_GNU_addr_index:
1549 case DW_OP_GNU_const_index:
1550 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1551 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1552 break;
1553 case DW_OP_const1u:
1554 case DW_OP_const1s:
1555 size += 1;
1556 break;
1557 case DW_OP_const2u:
1558 case DW_OP_const2s:
1559 size += 2;
1560 break;
1561 case DW_OP_const4u:
1562 case DW_OP_const4s:
1563 size += 4;
1564 break;
1565 case DW_OP_const8u:
1566 case DW_OP_const8s:
1567 size += 8;
1568 break;
1569 case DW_OP_constu:
1570 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1571 break;
1572 case DW_OP_consts:
1573 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1574 break;
1575 case DW_OP_pick:
1576 size += 1;
1577 break;
1578 case DW_OP_plus_uconst:
1579 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1580 break;
1581 case DW_OP_skip:
1582 case DW_OP_bra:
1583 size += 2;
1584 break;
1585 case DW_OP_breg0:
1586 case DW_OP_breg1:
1587 case DW_OP_breg2:
1588 case DW_OP_breg3:
1589 case DW_OP_breg4:
1590 case DW_OP_breg5:
1591 case DW_OP_breg6:
1592 case DW_OP_breg7:
1593 case DW_OP_breg8:
1594 case DW_OP_breg9:
1595 case DW_OP_breg10:
1596 case DW_OP_breg11:
1597 case DW_OP_breg12:
1598 case DW_OP_breg13:
1599 case DW_OP_breg14:
1600 case DW_OP_breg15:
1601 case DW_OP_breg16:
1602 case DW_OP_breg17:
1603 case DW_OP_breg18:
1604 case DW_OP_breg19:
1605 case DW_OP_breg20:
1606 case DW_OP_breg21:
1607 case DW_OP_breg22:
1608 case DW_OP_breg23:
1609 case DW_OP_breg24:
1610 case DW_OP_breg25:
1611 case DW_OP_breg26:
1612 case DW_OP_breg27:
1613 case DW_OP_breg28:
1614 case DW_OP_breg29:
1615 case DW_OP_breg30:
1616 case DW_OP_breg31:
1617 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1618 break;
1619 case DW_OP_regx:
1620 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621 break;
1622 case DW_OP_fbreg:
1623 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1624 break;
1625 case DW_OP_bregx:
1626 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1627 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1628 break;
1629 case DW_OP_piece:
1630 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1631 break;
1632 case DW_OP_bit_piece:
1633 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1634 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1635 break;
1636 case DW_OP_deref_size:
1637 case DW_OP_xderef_size:
1638 size += 1;
1639 break;
1640 case DW_OP_call2:
1641 size += 2;
1642 break;
1643 case DW_OP_call4:
1644 size += 4;
1645 break;
1646 case DW_OP_call_ref:
1647 size += DWARF_REF_SIZE;
1648 break;
1649 case DW_OP_implicit_value:
1650 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1651 + loc->dw_loc_oprnd1.v.val_unsigned;
1652 break;
1653 case DW_OP_GNU_implicit_pointer:
1654 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1655 break;
1656 case DW_OP_GNU_entry_value:
1658 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1659 size += size_of_uleb128 (op_size) + op_size;
1660 break;
1662 case DW_OP_GNU_const_type:
1664 unsigned long o
1665 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1666 size += size_of_uleb128 (o) + 1;
1667 switch (loc->dw_loc_oprnd2.val_class)
1669 case dw_val_class_vec:
1670 size += loc->dw_loc_oprnd2.v.val_vec.length
1671 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1672 break;
1673 case dw_val_class_const:
1674 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1675 break;
1676 case dw_val_class_const_double:
1677 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1678 break;
1679 case dw_val_class_wide_int:
1680 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1681 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1682 break;
1683 default:
1684 gcc_unreachable ();
1686 break;
1688 case DW_OP_GNU_regval_type:
1690 unsigned long o
1691 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1692 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1693 + size_of_uleb128 (o);
1695 break;
1696 case DW_OP_GNU_deref_type:
1698 unsigned long o
1699 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1700 size += 1 + size_of_uleb128 (o);
1702 break;
1703 case DW_OP_GNU_convert:
1704 case DW_OP_GNU_reinterpret:
1705 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1706 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1707 else
1709 unsigned long o
1710 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1711 size += size_of_uleb128 (o);
1713 break;
1714 case DW_OP_GNU_parameter_ref:
1715 size += 4;
1716 break;
1717 default:
1718 break;
1721 return size;
1724 /* Return the size of a series of location descriptors. */
1726 unsigned long
1727 size_of_locs (dw_loc_descr_ref loc)
1729 dw_loc_descr_ref l;
1730 unsigned long size;
1732 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1733 field, to avoid writing to a PCH file. */
1734 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1736 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1737 break;
1738 size += size_of_loc_descr (l);
1740 if (! l)
1741 return size;
1743 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1745 l->dw_loc_addr = size;
1746 size += size_of_loc_descr (l);
1749 return size;
1752 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1753 static void get_ref_die_offset_label (char *, dw_die_ref);
1754 static unsigned long int get_ref_die_offset (dw_die_ref);
1756 /* Output location description stack opcode's operands (if any).
1757 The for_eh_or_skip parameter controls whether register numbers are
1758 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1759 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1760 info). This should be suppressed for the cases that have not been converted
1761 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1763 static void
1764 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1766 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1767 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1769 switch (loc->dw_loc_opc)
1771 #ifdef DWARF2_DEBUGGING_INFO
1772 case DW_OP_const2u:
1773 case DW_OP_const2s:
1774 dw2_asm_output_data (2, val1->v.val_int, NULL);
1775 break;
1776 case DW_OP_const4u:
1777 if (loc->dtprel)
1779 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1780 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1781 val1->v.val_addr);
1782 fputc ('\n', asm_out_file);
1783 break;
1785 /* FALLTHRU */
1786 case DW_OP_const4s:
1787 dw2_asm_output_data (4, val1->v.val_int, NULL);
1788 break;
1789 case DW_OP_const8u:
1790 if (loc->dtprel)
1792 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1793 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1794 val1->v.val_addr);
1795 fputc ('\n', asm_out_file);
1796 break;
1798 /* FALLTHRU */
1799 case DW_OP_const8s:
1800 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1801 dw2_asm_output_data (8, val1->v.val_int, NULL);
1802 break;
1803 case DW_OP_skip:
1804 case DW_OP_bra:
1806 int offset;
1808 gcc_assert (val1->val_class == dw_val_class_loc);
1809 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1811 dw2_asm_output_data (2, offset, NULL);
1813 break;
1814 case DW_OP_implicit_value:
1815 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1816 switch (val2->val_class)
1818 case dw_val_class_const:
1819 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1820 break;
1821 case dw_val_class_vec:
1823 unsigned int elt_size = val2->v.val_vec.elt_size;
1824 unsigned int len = val2->v.val_vec.length;
1825 unsigned int i;
1826 unsigned char *p;
1828 if (elt_size > sizeof (HOST_WIDE_INT))
1830 elt_size /= 2;
1831 len *= 2;
1833 for (i = 0, p = val2->v.val_vec.array;
1834 i < len;
1835 i++, p += elt_size)
1836 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1837 "fp or vector constant word %u", i);
1839 break;
1840 case dw_val_class_const_double:
1842 unsigned HOST_WIDE_INT first, second;
1844 if (WORDS_BIG_ENDIAN)
1846 first = val2->v.val_double.high;
1847 second = val2->v.val_double.low;
1849 else
1851 first = val2->v.val_double.low;
1852 second = val2->v.val_double.high;
1854 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1855 first, NULL);
1856 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857 second, NULL);
1859 break;
1860 case dw_val_class_wide_int:
1862 int i;
1863 int len = get_full_len (*val2->v.val_wide);
1864 if (WORDS_BIG_ENDIAN)
1865 for (i = len - 1; i >= 0; --i)
1866 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1867 val2->v.val_wide->elt (i), NULL);
1868 else
1869 for (i = 0; i < len; ++i)
1870 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871 val2->v.val_wide->elt (i), NULL);
1873 break;
1874 case dw_val_class_addr:
1875 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1876 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1877 break;
1878 default:
1879 gcc_unreachable ();
1881 break;
1882 #else
1883 case DW_OP_const2u:
1884 case DW_OP_const2s:
1885 case DW_OP_const4u:
1886 case DW_OP_const4s:
1887 case DW_OP_const8u:
1888 case DW_OP_const8s:
1889 case DW_OP_skip:
1890 case DW_OP_bra:
1891 case DW_OP_implicit_value:
1892 /* We currently don't make any attempt to make sure these are
1893 aligned properly like we do for the main unwind info, so
1894 don't support emitting things larger than a byte if we're
1895 only doing unwinding. */
1896 gcc_unreachable ();
1897 #endif
1898 case DW_OP_const1u:
1899 case DW_OP_const1s:
1900 dw2_asm_output_data (1, val1->v.val_int, NULL);
1901 break;
1902 case DW_OP_constu:
1903 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1904 break;
1905 case DW_OP_consts:
1906 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1907 break;
1908 case DW_OP_pick:
1909 dw2_asm_output_data (1, val1->v.val_int, NULL);
1910 break;
1911 case DW_OP_plus_uconst:
1912 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1913 break;
1914 case DW_OP_breg0:
1915 case DW_OP_breg1:
1916 case DW_OP_breg2:
1917 case DW_OP_breg3:
1918 case DW_OP_breg4:
1919 case DW_OP_breg5:
1920 case DW_OP_breg6:
1921 case DW_OP_breg7:
1922 case DW_OP_breg8:
1923 case DW_OP_breg9:
1924 case DW_OP_breg10:
1925 case DW_OP_breg11:
1926 case DW_OP_breg12:
1927 case DW_OP_breg13:
1928 case DW_OP_breg14:
1929 case DW_OP_breg15:
1930 case DW_OP_breg16:
1931 case DW_OP_breg17:
1932 case DW_OP_breg18:
1933 case DW_OP_breg19:
1934 case DW_OP_breg20:
1935 case DW_OP_breg21:
1936 case DW_OP_breg22:
1937 case DW_OP_breg23:
1938 case DW_OP_breg24:
1939 case DW_OP_breg25:
1940 case DW_OP_breg26:
1941 case DW_OP_breg27:
1942 case DW_OP_breg28:
1943 case DW_OP_breg29:
1944 case DW_OP_breg30:
1945 case DW_OP_breg31:
1946 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1947 break;
1948 case DW_OP_regx:
1950 unsigned r = val1->v.val_unsigned;
1951 if (for_eh_or_skip >= 0)
1952 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1953 gcc_assert (size_of_uleb128 (r)
1954 == size_of_uleb128 (val1->v.val_unsigned));
1955 dw2_asm_output_data_uleb128 (r, NULL);
1957 break;
1958 case DW_OP_fbreg:
1959 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1960 break;
1961 case DW_OP_bregx:
1963 unsigned r = val1->v.val_unsigned;
1964 if (for_eh_or_skip >= 0)
1965 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1966 gcc_assert (size_of_uleb128 (r)
1967 == size_of_uleb128 (val1->v.val_unsigned));
1968 dw2_asm_output_data_uleb128 (r, NULL);
1969 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1971 break;
1972 case DW_OP_piece:
1973 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1974 break;
1975 case DW_OP_bit_piece:
1976 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1977 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1978 break;
1979 case DW_OP_deref_size:
1980 case DW_OP_xderef_size:
1981 dw2_asm_output_data (1, val1->v.val_int, NULL);
1982 break;
1984 case DW_OP_addr:
1985 if (loc->dtprel)
1987 if (targetm.asm_out.output_dwarf_dtprel)
1989 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1990 DWARF2_ADDR_SIZE,
1991 val1->v.val_addr);
1992 fputc ('\n', asm_out_file);
1994 else
1995 gcc_unreachable ();
1997 else
1999 #ifdef DWARF2_DEBUGGING_INFO
2000 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2001 #else
2002 gcc_unreachable ();
2003 #endif
2005 break;
2007 case DW_OP_GNU_addr_index:
2008 case DW_OP_GNU_const_index:
2009 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2010 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2011 "(index into .debug_addr)");
2012 break;
2014 case DW_OP_GNU_implicit_pointer:
2016 char label[MAX_ARTIFICIAL_LABEL_BYTES
2017 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2018 gcc_assert (val1->val_class == dw_val_class_die_ref);
2019 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2020 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2021 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2023 break;
2025 case DW_OP_GNU_entry_value:
2026 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2027 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2028 break;
2030 case DW_OP_GNU_const_type:
2032 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2033 gcc_assert (o);
2034 dw2_asm_output_data_uleb128 (o, NULL);
2035 switch (val2->val_class)
2037 case dw_val_class_const:
2038 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2039 dw2_asm_output_data (1, l, NULL);
2040 dw2_asm_output_data (l, val2->v.val_int, NULL);
2041 break;
2042 case dw_val_class_vec:
2044 unsigned int elt_size = val2->v.val_vec.elt_size;
2045 unsigned int len = val2->v.val_vec.length;
2046 unsigned int i;
2047 unsigned char *p;
2049 l = len * elt_size;
2050 dw2_asm_output_data (1, l, NULL);
2051 if (elt_size > sizeof (HOST_WIDE_INT))
2053 elt_size /= 2;
2054 len *= 2;
2056 for (i = 0, p = val2->v.val_vec.array;
2057 i < len;
2058 i++, p += elt_size)
2059 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2060 "fp or vector constant word %u", i);
2062 break;
2063 case dw_val_class_const_double:
2065 unsigned HOST_WIDE_INT first, second;
2066 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2068 dw2_asm_output_data (1, 2 * l, NULL);
2069 if (WORDS_BIG_ENDIAN)
2071 first = val2->v.val_double.high;
2072 second = val2->v.val_double.low;
2074 else
2076 first = val2->v.val_double.low;
2077 second = val2->v.val_double.high;
2079 dw2_asm_output_data (l, first, NULL);
2080 dw2_asm_output_data (l, second, NULL);
2082 break;
2083 case dw_val_class_wide_int:
2085 int i;
2086 int len = get_full_len (*val2->v.val_wide);
2087 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2089 dw2_asm_output_data (1, len * l, NULL);
2090 if (WORDS_BIG_ENDIAN)
2091 for (i = len - 1; i >= 0; --i)
2092 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2093 else
2094 for (i = 0; i < len; ++i)
2095 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2097 break;
2098 default:
2099 gcc_unreachable ();
2102 break;
2103 case DW_OP_GNU_regval_type:
2105 unsigned r = val1->v.val_unsigned;
2106 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2107 gcc_assert (o);
2108 if (for_eh_or_skip >= 0)
2110 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2111 gcc_assert (size_of_uleb128 (r)
2112 == size_of_uleb128 (val1->v.val_unsigned));
2114 dw2_asm_output_data_uleb128 (r, NULL);
2115 dw2_asm_output_data_uleb128 (o, NULL);
2117 break;
2118 case DW_OP_GNU_deref_type:
2120 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2121 gcc_assert (o);
2122 dw2_asm_output_data (1, val1->v.val_int, NULL);
2123 dw2_asm_output_data_uleb128 (o, NULL);
2125 break;
2126 case DW_OP_GNU_convert:
2127 case DW_OP_GNU_reinterpret:
2128 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2129 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2130 else
2132 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2133 gcc_assert (o);
2134 dw2_asm_output_data_uleb128 (o, NULL);
2136 break;
2138 case DW_OP_GNU_parameter_ref:
2140 unsigned long o;
2141 gcc_assert (val1->val_class == dw_val_class_die_ref);
2142 o = get_ref_die_offset (val1->v.val_die_ref.die);
2143 dw2_asm_output_data (4, o, NULL);
2145 break;
2147 default:
2148 /* Other codes have no operands. */
2149 break;
2153 /* Output a sequence of location operations.
2154 The for_eh_or_skip parameter controls whether register numbers are
2155 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2156 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2157 info). This should be suppressed for the cases that have not been converted
2158 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2160 void
2161 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2163 for (; loc != NULL; loc = loc->dw_loc_next)
2165 enum dwarf_location_atom opc = loc->dw_loc_opc;
2166 /* Output the opcode. */
2167 if (for_eh_or_skip >= 0
2168 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2170 unsigned r = (opc - DW_OP_breg0);
2171 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2172 gcc_assert (r <= 31);
2173 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2175 else if (for_eh_or_skip >= 0
2176 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2178 unsigned r = (opc - DW_OP_reg0);
2179 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2180 gcc_assert (r <= 31);
2181 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2184 dw2_asm_output_data (1, opc,
2185 "%s", dwarf_stack_op_name (opc));
2187 /* Output the operand(s) (if any). */
2188 output_loc_operands (loc, for_eh_or_skip);
2192 /* Output location description stack opcode's operands (if any).
2193 The output is single bytes on a line, suitable for .cfi_escape. */
2195 static void
2196 output_loc_operands_raw (dw_loc_descr_ref loc)
2198 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2199 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2201 switch (loc->dw_loc_opc)
2203 case DW_OP_addr:
2204 case DW_OP_GNU_addr_index:
2205 case DW_OP_GNU_const_index:
2206 case DW_OP_implicit_value:
2207 /* We cannot output addresses in .cfi_escape, only bytes. */
2208 gcc_unreachable ();
2210 case DW_OP_const1u:
2211 case DW_OP_const1s:
2212 case DW_OP_pick:
2213 case DW_OP_deref_size:
2214 case DW_OP_xderef_size:
2215 fputc (',', asm_out_file);
2216 dw2_asm_output_data_raw (1, val1->v.val_int);
2217 break;
2219 case DW_OP_const2u:
2220 case DW_OP_const2s:
2221 fputc (',', asm_out_file);
2222 dw2_asm_output_data_raw (2, val1->v.val_int);
2223 break;
2225 case DW_OP_const4u:
2226 case DW_OP_const4s:
2227 fputc (',', asm_out_file);
2228 dw2_asm_output_data_raw (4, val1->v.val_int);
2229 break;
2231 case DW_OP_const8u:
2232 case DW_OP_const8s:
2233 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2234 fputc (',', asm_out_file);
2235 dw2_asm_output_data_raw (8, val1->v.val_int);
2236 break;
2238 case DW_OP_skip:
2239 case DW_OP_bra:
2241 int offset;
2243 gcc_assert (val1->val_class == dw_val_class_loc);
2244 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2246 fputc (',', asm_out_file);
2247 dw2_asm_output_data_raw (2, offset);
2249 break;
2251 case DW_OP_regx:
2253 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2254 gcc_assert (size_of_uleb128 (r)
2255 == size_of_uleb128 (val1->v.val_unsigned));
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (r);
2259 break;
2261 case DW_OP_constu:
2262 case DW_OP_plus_uconst:
2263 case DW_OP_piece:
2264 fputc (',', asm_out_file);
2265 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2266 break;
2268 case DW_OP_bit_piece:
2269 fputc (',', asm_out_file);
2270 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2271 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2272 break;
2274 case DW_OP_consts:
2275 case DW_OP_breg0:
2276 case DW_OP_breg1:
2277 case DW_OP_breg2:
2278 case DW_OP_breg3:
2279 case DW_OP_breg4:
2280 case DW_OP_breg5:
2281 case DW_OP_breg6:
2282 case DW_OP_breg7:
2283 case DW_OP_breg8:
2284 case DW_OP_breg9:
2285 case DW_OP_breg10:
2286 case DW_OP_breg11:
2287 case DW_OP_breg12:
2288 case DW_OP_breg13:
2289 case DW_OP_breg14:
2290 case DW_OP_breg15:
2291 case DW_OP_breg16:
2292 case DW_OP_breg17:
2293 case DW_OP_breg18:
2294 case DW_OP_breg19:
2295 case DW_OP_breg20:
2296 case DW_OP_breg21:
2297 case DW_OP_breg22:
2298 case DW_OP_breg23:
2299 case DW_OP_breg24:
2300 case DW_OP_breg25:
2301 case DW_OP_breg26:
2302 case DW_OP_breg27:
2303 case DW_OP_breg28:
2304 case DW_OP_breg29:
2305 case DW_OP_breg30:
2306 case DW_OP_breg31:
2307 case DW_OP_fbreg:
2308 fputc (',', asm_out_file);
2309 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2310 break;
2312 case DW_OP_bregx:
2314 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2315 gcc_assert (size_of_uleb128 (r)
2316 == size_of_uleb128 (val1->v.val_unsigned));
2317 fputc (',', asm_out_file);
2318 dw2_asm_output_data_uleb128_raw (r);
2319 fputc (',', asm_out_file);
2320 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2322 break;
2324 case DW_OP_GNU_implicit_pointer:
2325 case DW_OP_GNU_entry_value:
2326 case DW_OP_GNU_const_type:
2327 case DW_OP_GNU_regval_type:
2328 case DW_OP_GNU_deref_type:
2329 case DW_OP_GNU_convert:
2330 case DW_OP_GNU_reinterpret:
2331 case DW_OP_GNU_parameter_ref:
2332 gcc_unreachable ();
2333 break;
2335 default:
2336 /* Other codes have no operands. */
2337 break;
2341 void
2342 output_loc_sequence_raw (dw_loc_descr_ref loc)
2344 while (1)
2346 enum dwarf_location_atom opc = loc->dw_loc_opc;
2347 /* Output the opcode. */
2348 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2350 unsigned r = (opc - DW_OP_breg0);
2351 r = DWARF2_FRAME_REG_OUT (r, 1);
2352 gcc_assert (r <= 31);
2353 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2355 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2357 unsigned r = (opc - DW_OP_reg0);
2358 r = DWARF2_FRAME_REG_OUT (r, 1);
2359 gcc_assert (r <= 31);
2360 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2362 /* Output the opcode. */
2363 fprintf (asm_out_file, "%#x", opc);
2364 output_loc_operands_raw (loc);
2366 if (!loc->dw_loc_next)
2367 break;
2368 loc = loc->dw_loc_next;
2370 fputc (',', asm_out_file);
2374 /* This function builds a dwarf location descriptor sequence from a
2375 dw_cfa_location, adding the given OFFSET to the result of the
2376 expression. */
2378 struct dw_loc_descr_node *
2379 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2381 struct dw_loc_descr_node *head, *tmp;
2383 offset += cfa->offset;
2385 if (cfa->indirect)
2387 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2388 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2389 head->dw_loc_oprnd1.val_entry = NULL;
2390 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2391 add_loc_descr (&head, tmp);
2392 if (offset != 0)
2394 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2395 add_loc_descr (&head, tmp);
2398 else
2399 head = new_reg_loc_descr (cfa->reg, offset);
2401 return head;
2404 /* This function builds a dwarf location descriptor sequence for
2405 the address at OFFSET from the CFA when stack is aligned to
2406 ALIGNMENT byte. */
2408 struct dw_loc_descr_node *
2409 build_cfa_aligned_loc (dw_cfa_location *cfa,
2410 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2412 struct dw_loc_descr_node *head;
2413 unsigned int dwarf_fp
2414 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2416 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2417 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2419 head = new_reg_loc_descr (dwarf_fp, 0);
2420 add_loc_descr (&head, int_loc_descriptor (alignment));
2421 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2422 loc_descr_plus_const (&head, offset);
2424 else
2425 head = new_reg_loc_descr (dwarf_fp, offset);
2426 return head;
2429 /* And now, the support for symbolic debugging information. */
2431 /* .debug_str support. */
2433 static void dwarf2out_init (const char *);
2434 static void dwarf2out_finish (const char *);
2435 static void dwarf2out_early_finish (void);
2436 static void dwarf2out_assembly_start (void);
2437 static void dwarf2out_define (unsigned int, const char *);
2438 static void dwarf2out_undef (unsigned int, const char *);
2439 static void dwarf2out_start_source_file (unsigned, const char *);
2440 static void dwarf2out_end_source_file (unsigned);
2441 static void dwarf2out_function_decl (tree);
2442 static void dwarf2out_begin_block (unsigned, unsigned);
2443 static void dwarf2out_end_block (unsigned, unsigned);
2444 static bool dwarf2out_ignore_block (const_tree);
2445 static void dwarf2out_early_global_decl (tree);
2446 static void dwarf2out_late_global_decl (tree);
2447 static void dwarf2out_type_decl (tree, int);
2448 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2449 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2450 dw_die_ref);
2451 static void dwarf2out_abstract_function (tree);
2452 static void dwarf2out_var_location (rtx_insn *);
2453 static void dwarf2out_begin_function (tree);
2454 static void dwarf2out_end_function (unsigned int);
2455 static void dwarf2out_register_main_translation_unit (tree unit);
2456 static void dwarf2out_set_name (tree, tree);
2458 /* The debug hooks structure. */
2460 const struct gcc_debug_hooks dwarf2_debug_hooks =
2462 dwarf2out_init,
2463 dwarf2out_finish,
2464 dwarf2out_early_finish,
2465 dwarf2out_assembly_start,
2466 dwarf2out_define,
2467 dwarf2out_undef,
2468 dwarf2out_start_source_file,
2469 dwarf2out_end_source_file,
2470 dwarf2out_begin_block,
2471 dwarf2out_end_block,
2472 dwarf2out_ignore_block,
2473 dwarf2out_source_line,
2474 dwarf2out_begin_prologue,
2475 #if VMS_DEBUGGING_INFO
2476 dwarf2out_vms_end_prologue,
2477 dwarf2out_vms_begin_epilogue,
2478 #else
2479 debug_nothing_int_charstar,
2480 debug_nothing_int_charstar,
2481 #endif
2482 dwarf2out_end_epilogue,
2483 dwarf2out_begin_function,
2484 dwarf2out_end_function, /* end_function */
2485 dwarf2out_register_main_translation_unit,
2486 dwarf2out_function_decl, /* function_decl */
2487 dwarf2out_early_global_decl,
2488 dwarf2out_late_global_decl,
2489 dwarf2out_type_decl, /* type_decl */
2490 dwarf2out_imported_module_or_decl,
2491 debug_nothing_tree, /* deferred_inline_function */
2492 /* The DWARF 2 backend tries to reduce debugging bloat by not
2493 emitting the abstract description of inline functions until
2494 something tries to reference them. */
2495 dwarf2out_abstract_function, /* outlining_inline_function */
2496 debug_nothing_rtx_code_label, /* label */
2497 debug_nothing_int, /* handle_pch */
2498 dwarf2out_var_location,
2499 dwarf2out_switch_text_section,
2500 dwarf2out_set_name,
2501 1, /* start_end_main_source_file */
2502 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2505 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2507 dwarf2out_init,
2508 debug_nothing_charstar,
2509 debug_nothing_void,
2510 debug_nothing_void,
2511 debug_nothing_int_charstar,
2512 debug_nothing_int_charstar,
2513 debug_nothing_int_charstar,
2514 debug_nothing_int,
2515 debug_nothing_int_int, /* begin_block */
2516 debug_nothing_int_int, /* end_block */
2517 debug_true_const_tree, /* ignore_block */
2518 dwarf2out_source_line, /* source_line */
2519 debug_nothing_int_charstar, /* begin_prologue */
2520 debug_nothing_int_charstar, /* end_prologue */
2521 debug_nothing_int_charstar, /* begin_epilogue */
2522 debug_nothing_int_charstar, /* end_epilogue */
2523 debug_nothing_tree, /* begin_function */
2524 debug_nothing_int, /* end_function */
2525 debug_nothing_tree, /* register_main_translation_unit */
2526 debug_nothing_tree, /* function_decl */
2527 debug_nothing_tree, /* early_global_decl */
2528 debug_nothing_tree, /* late_global_decl */
2529 debug_nothing_tree_int, /* type_decl */
2530 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2531 debug_nothing_tree, /* deferred_inline_function */
2532 debug_nothing_tree, /* outlining_inline_function */
2533 debug_nothing_rtx_code_label, /* label */
2534 debug_nothing_int, /* handle_pch */
2535 debug_nothing_rtx_insn, /* var_location */
2536 debug_nothing_void, /* switch_text_section */
2537 debug_nothing_tree_tree, /* set_name */
2538 0, /* start_end_main_source_file */
2539 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2542 /* NOTE: In the comments in this file, many references are made to
2543 "Debugging Information Entries". This term is abbreviated as `DIE'
2544 throughout the remainder of this file. */
2546 /* An internal representation of the DWARF output is built, and then
2547 walked to generate the DWARF debugging info. The walk of the internal
2548 representation is done after the entire program has been compiled.
2549 The types below are used to describe the internal representation. */
2551 /* Whether to put type DIEs into their own section .debug_types instead
2552 of making them part of the .debug_info section. Only supported for
2553 Dwarf V4 or higher and the user didn't disable them through
2554 -fno-debug-types-section. It is more efficient to put them in a
2555 separate comdat sections since the linker will then be able to
2556 remove duplicates. But not all tools support .debug_types sections
2557 yet. */
2559 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2561 /* Various DIE's use offsets relative to the beginning of the
2562 .debug_info section to refer to each other. */
2564 typedef long int dw_offset;
2566 struct comdat_type_node;
2568 /* The entries in the line_info table more-or-less mirror the opcodes
2569 that are used in the real dwarf line table. Arrays of these entries
2570 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2571 supported. */
2573 enum dw_line_info_opcode {
2574 /* Emit DW_LNE_set_address; the operand is the label index. */
2575 LI_set_address,
2577 /* Emit a row to the matrix with the given line. This may be done
2578 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2579 special opcodes. */
2580 LI_set_line,
2582 /* Emit a DW_LNS_set_file. */
2583 LI_set_file,
2585 /* Emit a DW_LNS_set_column. */
2586 LI_set_column,
2588 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2589 LI_negate_stmt,
2591 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2592 LI_set_prologue_end,
2593 LI_set_epilogue_begin,
2595 /* Emit a DW_LNE_set_discriminator. */
2596 LI_set_discriminator
2599 typedef struct GTY(()) dw_line_info_struct {
2600 enum dw_line_info_opcode opcode;
2601 unsigned int val;
2602 } dw_line_info_entry;
2605 struct GTY(()) dw_line_info_table {
2606 /* The label that marks the end of this section. */
2607 const char *end_label;
2609 /* The values for the last row of the matrix, as collected in the table.
2610 These are used to minimize the changes to the next row. */
2611 unsigned int file_num;
2612 unsigned int line_num;
2613 unsigned int column_num;
2614 int discrim_num;
2615 bool is_stmt;
2616 bool in_use;
2618 vec<dw_line_info_entry, va_gc> *entries;
2622 /* Each DIE attribute has a field specifying the attribute kind,
2623 a link to the next attribute in the chain, and an attribute value.
2624 Attributes are typically linked below the DIE they modify. */
2626 typedef struct GTY(()) dw_attr_struct {
2627 enum dwarf_attribute dw_attr;
2628 dw_val_node dw_attr_val;
2630 dw_attr_node;
2633 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2634 The children of each node form a circular list linked by
2635 die_sib. die_child points to the node *before* the "first" child node. */
2637 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2638 union die_symbol_or_type_node
2640 const char * GTY ((tag ("0"))) die_symbol;
2641 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2643 GTY ((desc ("%0.comdat_type_p"))) die_id;
2644 vec<dw_attr_node, va_gc> *die_attr;
2645 dw_die_ref die_parent;
2646 dw_die_ref die_child;
2647 dw_die_ref die_sib;
2648 dw_die_ref die_definition; /* ref from a specification to its definition */
2649 dw_offset die_offset;
2650 unsigned long die_abbrev;
2651 int die_mark;
2652 unsigned int decl_id;
2653 enum dwarf_tag die_tag;
2654 /* Die is used and must not be pruned as unused. */
2655 BOOL_BITFIELD die_perennial_p : 1;
2656 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2657 /* Lots of spare bits. */
2659 die_node;
2661 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2662 static bool early_dwarf;
2663 struct set_early_dwarf {
2664 bool saved;
2665 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2666 ~set_early_dwarf () { early_dwarf = saved; }
2669 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2670 #define FOR_EACH_CHILD(die, c, expr) do { \
2671 c = die->die_child; \
2672 if (c) do { \
2673 c = c->die_sib; \
2674 expr; \
2675 } while (c != die->die_child); \
2676 } while (0)
2678 /* The pubname structure */
2680 typedef struct GTY(()) pubname_struct {
2681 dw_die_ref die;
2682 const char *name;
2684 pubname_entry;
2687 struct GTY(()) dw_ranges {
2688 /* If this is positive, it's a block number, otherwise it's a
2689 bitwise-negated index into dw_ranges_by_label. */
2690 int num;
2693 /* A structure to hold a macinfo entry. */
2695 typedef struct GTY(()) macinfo_struct {
2696 unsigned char code;
2697 unsigned HOST_WIDE_INT lineno;
2698 const char *info;
2700 macinfo_entry;
2703 struct GTY(()) dw_ranges_by_label {
2704 const char *begin;
2705 const char *end;
2708 /* The comdat type node structure. */
2709 struct GTY(()) comdat_type_node
2711 dw_die_ref root_die;
2712 dw_die_ref type_die;
2713 dw_die_ref skeleton_die;
2714 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2715 comdat_type_node *next;
2718 /* A list of DIEs for which we can't determine ancestry (parent_die
2719 field) just yet. Later in dwarf2out_finish we will fill in the
2720 missing bits. */
2721 typedef struct GTY(()) limbo_die_struct {
2722 dw_die_ref die;
2723 /* The tree for which this DIE was created. We use this to
2724 determine ancestry later. */
2725 tree created_for;
2726 struct limbo_die_struct *next;
2728 limbo_die_node;
2730 typedef struct skeleton_chain_struct
2732 dw_die_ref old_die;
2733 dw_die_ref new_die;
2734 struct skeleton_chain_struct *parent;
2736 skeleton_chain_node;
2738 /* Define a macro which returns nonzero for a TYPE_DECL which was
2739 implicitly generated for a type.
2741 Note that, unlike the C front-end (which generates a NULL named
2742 TYPE_DECL node for each complete tagged type, each array type,
2743 and each function type node created) the C++ front-end generates
2744 a _named_ TYPE_DECL node for each tagged type node created.
2745 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2746 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2747 front-end, but for each type, tagged or not. */
2749 #define TYPE_DECL_IS_STUB(decl) \
2750 (DECL_NAME (decl) == NULL_TREE \
2751 || (DECL_ARTIFICIAL (decl) \
2752 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2753 /* This is necessary for stub decls that \
2754 appear in nested inline functions. */ \
2755 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2756 && (decl_ultimate_origin (decl) \
2757 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2759 /* Information concerning the compilation unit's programming
2760 language, and compiler version. */
2762 /* Fixed size portion of the DWARF compilation unit header. */
2763 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2764 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2766 /* Fixed size portion of the DWARF comdat type unit header. */
2767 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2768 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2769 + DWARF_OFFSET_SIZE)
2771 /* Fixed size portion of public names info. */
2772 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2774 /* Fixed size portion of the address range info. */
2775 #define DWARF_ARANGES_HEADER_SIZE \
2776 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2777 DWARF2_ADDR_SIZE * 2) \
2778 - DWARF_INITIAL_LENGTH_SIZE)
2780 /* Size of padding portion in the address range info. It must be
2781 aligned to twice the pointer size. */
2782 #define DWARF_ARANGES_PAD_SIZE \
2783 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2784 DWARF2_ADDR_SIZE * 2) \
2785 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2787 /* Use assembler line directives if available. */
2788 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2789 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2790 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2791 #else
2792 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2793 #endif
2794 #endif
2796 /* Minimum line offset in a special line info. opcode.
2797 This value was chosen to give a reasonable range of values. */
2798 #define DWARF_LINE_BASE -10
2800 /* First special line opcode - leave room for the standard opcodes. */
2801 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2803 /* Range of line offsets in a special line info. opcode. */
2804 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2806 /* Flag that indicates the initial value of the is_stmt_start flag.
2807 In the present implementation, we do not mark any lines as
2808 the beginning of a source statement, because that information
2809 is not made available by the GCC front-end. */
2810 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2812 /* Maximum number of operations per instruction bundle. */
2813 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2814 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2815 #endif
2817 /* This location is used by calc_die_sizes() to keep track
2818 the offset of each DIE within the .debug_info section. */
2819 static unsigned long next_die_offset;
2821 /* Record the root of the DIE's built for the current compilation unit. */
2822 static GTY(()) dw_die_ref single_comp_unit_die;
2824 /* A list of type DIEs that have been separated into comdat sections. */
2825 static GTY(()) comdat_type_node *comdat_type_list;
2827 /* A list of DIEs with a NULL parent waiting to be relocated. */
2828 static GTY(()) limbo_die_node *limbo_die_list;
2830 /* A list of DIEs for which we may have to generate
2831 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2832 static GTY(()) limbo_die_node *deferred_asm_name;
2834 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2836 typedef const char *compare_type;
2838 static hashval_t hash (dwarf_file_data *);
2839 static bool equal (dwarf_file_data *, const char *);
2842 /* Filenames referenced by this compilation unit. */
2843 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2845 struct decl_die_hasher : ggc_ptr_hash<die_node>
2847 typedef tree compare_type;
2849 static hashval_t hash (die_node *);
2850 static bool equal (die_node *, tree);
2852 /* A hash table of references to DIE's that describe declarations.
2853 The key is a DECL_UID() which is a unique number identifying each decl. */
2854 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2856 struct block_die_hasher : ggc_ptr_hash<die_struct>
2858 static hashval_t hash (die_struct *);
2859 static bool equal (die_struct *, die_struct *);
2862 /* A hash table of references to DIE's that describe COMMON blocks.
2863 The key is DECL_UID() ^ die_parent. */
2864 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2866 typedef struct GTY(()) die_arg_entry_struct {
2867 dw_die_ref die;
2868 tree arg;
2869 } die_arg_entry;
2872 /* Node of the variable location list. */
2873 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2874 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2875 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2876 in mode of the EXPR_LIST node and first EXPR_LIST operand
2877 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2878 location or NULL for padding. For larger bitsizes,
2879 mode is 0 and first operand is a CONCAT with bitsize
2880 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2881 NULL as second operand. */
2882 rtx GTY (()) loc;
2883 const char * GTY (()) label;
2884 struct var_loc_node * GTY (()) next;
2887 /* Variable location list. */
2888 struct GTY ((for_user)) var_loc_list_def {
2889 struct var_loc_node * GTY (()) first;
2891 /* Pointer to the last but one or last element of the
2892 chained list. If the list is empty, both first and
2893 last are NULL, if the list contains just one node
2894 or the last node certainly is not redundant, it points
2895 to the last node, otherwise points to the last but one.
2896 Do not mark it for GC because it is marked through the chain. */
2897 struct var_loc_node * GTY ((skip ("%h"))) last;
2899 /* Pointer to the last element before section switch,
2900 if NULL, either sections weren't switched or first
2901 is after section switch. */
2902 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2904 /* DECL_UID of the variable decl. */
2905 unsigned int decl_id;
2907 typedef struct var_loc_list_def var_loc_list;
2909 /* Call argument location list. */
2910 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2911 rtx GTY (()) call_arg_loc_note;
2912 const char * GTY (()) label;
2913 tree GTY (()) block;
2914 bool tail_call_p;
2915 rtx GTY (()) symbol_ref;
2916 struct call_arg_loc_node * GTY (()) next;
2920 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2922 typedef const_tree compare_type;
2924 static hashval_t hash (var_loc_list *);
2925 static bool equal (var_loc_list *, const_tree);
2928 /* Table of decl location linked lists. */
2929 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2931 /* Head and tail of call_arg_loc chain. */
2932 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2933 static struct call_arg_loc_node *call_arg_loc_last;
2935 /* Number of call sites in the current function. */
2936 static int call_site_count = -1;
2937 /* Number of tail call sites in the current function. */
2938 static int tail_call_site_count = -1;
2940 /* A cached location list. */
2941 struct GTY ((for_user)) cached_dw_loc_list_def {
2942 /* The DECL_UID of the decl that this entry describes. */
2943 unsigned int decl_id;
2945 /* The cached location list. */
2946 dw_loc_list_ref loc_list;
2948 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2950 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2953 typedef const_tree compare_type;
2955 static hashval_t hash (cached_dw_loc_list *);
2956 static bool equal (cached_dw_loc_list *, const_tree);
2959 /* Table of cached location lists. */
2960 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2962 /* A pointer to the base of a list of references to DIE's that
2963 are uniquely identified by their tag, presence/absence of
2964 children DIE's, and list of attribute/value pairs. */
2965 static GTY((length ("abbrev_die_table_allocated")))
2966 dw_die_ref *abbrev_die_table;
2968 /* Number of elements currently allocated for abbrev_die_table. */
2969 static GTY(()) unsigned abbrev_die_table_allocated;
2971 /* Number of elements in abbrev_die_table currently in use. */
2972 static GTY(()) unsigned abbrev_die_table_in_use;
2974 /* Size (in elements) of increments by which we may expand the
2975 abbrev_die_table. */
2976 #define ABBREV_DIE_TABLE_INCREMENT 256
2978 /* A global counter for generating labels for line number data. */
2979 static unsigned int line_info_label_num;
2981 /* The current table to which we should emit line number information
2982 for the current function. This will be set up at the beginning of
2983 assembly for the function. */
2984 static dw_line_info_table *cur_line_info_table;
2986 /* The two default tables of line number info. */
2987 static GTY(()) dw_line_info_table *text_section_line_info;
2988 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2990 /* The set of all non-default tables of line number info. */
2991 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
2993 /* A flag to tell pubnames/types export if there is an info section to
2994 refer to. */
2995 static bool info_section_emitted;
2997 /* A pointer to the base of a table that contains a list of publicly
2998 accessible names. */
2999 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3001 /* A pointer to the base of a table that contains a list of publicly
3002 accessible types. */
3003 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3005 /* A pointer to the base of a table that contains a list of macro
3006 defines/undefines (and file start/end markers). */
3007 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3009 /* True if .debug_macinfo or .debug_macros section is going to be
3010 emitted. */
3011 #define have_macinfo \
3012 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3013 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3014 && !macinfo_table->is_empty ())
3016 /* Array of dies for which we should generate .debug_ranges info. */
3017 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3019 /* Number of elements currently allocated for ranges_table. */
3020 static GTY(()) unsigned ranges_table_allocated;
3022 /* Number of elements in ranges_table currently in use. */
3023 static GTY(()) unsigned ranges_table_in_use;
3025 /* Array of pairs of labels referenced in ranges_table. */
3026 static GTY ((length ("ranges_by_label_allocated")))
3027 dw_ranges_by_label *ranges_by_label;
3029 /* Number of elements currently allocated for ranges_by_label. */
3030 static GTY(()) unsigned ranges_by_label_allocated;
3032 /* Number of elements in ranges_by_label currently in use. */
3033 static GTY(()) unsigned ranges_by_label_in_use;
3035 /* Size (in elements) of increments by which we may expand the
3036 ranges_table. */
3037 #define RANGES_TABLE_INCREMENT 64
3039 /* Whether we have location lists that need outputting */
3040 static GTY(()) bool have_location_lists;
3042 /* Unique label counter. */
3043 static GTY(()) unsigned int loclabel_num;
3045 /* Unique label counter for point-of-call tables. */
3046 static GTY(()) unsigned int poc_label_num;
3048 /* The last file entry emitted by maybe_emit_file(). */
3049 static GTY(()) struct dwarf_file_data * last_emitted_file;
3051 /* Number of internal labels generated by gen_internal_sym(). */
3052 static GTY(()) int label_num;
3054 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3056 /* Instances of generic types for which we need to generate debug
3057 info that describe their generic parameters and arguments. That
3058 generation needs to happen once all types are properly laid out so
3059 we do it at the end of compilation. */
3060 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3062 /* Offset from the "steady-state frame pointer" to the frame base,
3063 within the current function. */
3064 static HOST_WIDE_INT frame_pointer_fb_offset;
3065 static bool frame_pointer_fb_offset_valid;
3067 static vec<dw_die_ref> base_types;
3069 /* Flags to represent a set of attribute classes for attributes that represent
3070 a scalar value (bounds, pointers, ...). */
3071 enum dw_scalar_form
3073 dw_scalar_form_constant = 0x01,
3074 dw_scalar_form_exprloc = 0x02,
3075 dw_scalar_form_reference = 0x04
3078 /* Forward declarations for functions defined in this file. */
3080 static int is_pseudo_reg (const_rtx);
3081 static tree type_main_variant (tree);
3082 static int is_tagged_type (const_tree);
3083 static const char *dwarf_tag_name (unsigned);
3084 static const char *dwarf_attr_name (unsigned);
3085 static const char *dwarf_form_name (unsigned);
3086 static tree decl_ultimate_origin (const_tree);
3087 static tree decl_class_context (tree);
3088 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3089 static inline enum dw_val_class AT_class (dw_attr_node *);
3090 static inline unsigned int AT_index (dw_attr_node *);
3091 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3092 static inline unsigned AT_flag (dw_attr_node *);
3093 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3094 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3095 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3096 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3097 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3098 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3099 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3100 unsigned int, unsigned char *);
3101 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3102 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3103 static inline const char *AT_string (dw_attr_node *);
3104 static enum dwarf_form AT_string_form (dw_attr_node *);
3105 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3106 static void add_AT_specification (dw_die_ref, dw_die_ref);
3107 static inline dw_die_ref AT_ref (dw_attr_node *);
3108 static inline int AT_ref_external (dw_attr_node *);
3109 static inline void set_AT_ref_external (dw_attr_node *, int);
3110 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3111 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3112 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3113 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3114 dw_loc_list_ref);
3115 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3116 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3117 static void remove_addr_table_entry (addr_table_entry *);
3118 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3119 static inline rtx AT_addr (dw_attr_node *);
3120 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3121 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3122 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3123 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3124 unsigned HOST_WIDE_INT);
3125 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3126 unsigned long, bool);
3127 static inline const char *AT_lbl (dw_attr_node *);
3128 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3129 static const char *get_AT_low_pc (dw_die_ref);
3130 static const char *get_AT_hi_pc (dw_die_ref);
3131 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3132 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3133 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3134 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3135 static bool is_cxx (void);
3136 static bool is_fortran (void);
3137 static bool is_ada (void);
3138 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3139 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3140 static void add_child_die (dw_die_ref, dw_die_ref);
3141 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3142 static dw_die_ref lookup_type_die (tree);
3143 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3144 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3145 static void equate_type_number_to_die (tree, dw_die_ref);
3146 static dw_die_ref lookup_decl_die (tree);
3147 static var_loc_list *lookup_decl_loc (const_tree);
3148 static void equate_decl_number_to_die (tree, dw_die_ref);
3149 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3150 static void print_spaces (FILE *);
3151 static void print_die (dw_die_ref, FILE *);
3152 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3153 static dw_die_ref pop_compile_unit (dw_die_ref);
3154 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3155 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3156 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3157 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3158 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3159 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3160 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3161 struct md5_ctx *, int *);
3162 struct checksum_attributes;
3163 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3164 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3165 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3166 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3167 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3168 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3169 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3170 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3171 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3172 static void compute_section_prefix (dw_die_ref);
3173 static int is_type_die (dw_die_ref);
3174 static int is_comdat_die (dw_die_ref);
3175 static int is_symbol_die (dw_die_ref);
3176 static inline bool is_template_instantiation (dw_die_ref);
3177 static void assign_symbol_names (dw_die_ref);
3178 static void break_out_includes (dw_die_ref);
3179 static int is_declaration_die (dw_die_ref);
3180 static int should_move_die_to_comdat (dw_die_ref);
3181 static dw_die_ref clone_as_declaration (dw_die_ref);
3182 static dw_die_ref clone_die (dw_die_ref);
3183 static dw_die_ref clone_tree (dw_die_ref);
3184 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3185 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3186 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3187 static dw_die_ref generate_skeleton (dw_die_ref);
3188 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3189 dw_die_ref,
3190 dw_die_ref);
3191 static void break_out_comdat_types (dw_die_ref);
3192 static void copy_decls_for_unworthy_types (dw_die_ref);
3194 static void add_sibling_attributes (dw_die_ref);
3195 static void output_location_lists (dw_die_ref);
3196 static int constant_size (unsigned HOST_WIDE_INT);
3197 static unsigned long size_of_die (dw_die_ref);
3198 static void calc_die_sizes (dw_die_ref);
3199 static void calc_base_type_die_sizes (void);
3200 static void mark_dies (dw_die_ref);
3201 static void unmark_dies (dw_die_ref);
3202 static void unmark_all_dies (dw_die_ref);
3203 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3204 static unsigned long size_of_aranges (void);
3205 static enum dwarf_form value_format (dw_attr_node *);
3206 static void output_value_format (dw_attr_node *);
3207 static void output_abbrev_section (void);
3208 static void output_die_abbrevs (unsigned long, dw_die_ref);
3209 static void output_die_symbol (dw_die_ref);
3210 static void output_die (dw_die_ref);
3211 static void output_compilation_unit_header (void);
3212 static void output_comp_unit (dw_die_ref, int);
3213 static void output_comdat_type_unit (comdat_type_node *);
3214 static const char *dwarf2_name (tree, int);
3215 static void add_pubname (tree, dw_die_ref);
3216 static void add_enumerator_pubname (const char *, dw_die_ref);
3217 static void add_pubname_string (const char *, dw_die_ref);
3218 static void add_pubtype (tree, dw_die_ref);
3219 static void output_pubnames (vec<pubname_entry, va_gc> *);
3220 static void output_aranges (void);
3221 static unsigned int add_ranges_num (int);
3222 static unsigned int add_ranges (const_tree);
3223 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3224 bool *, bool);
3225 static void output_ranges (void);
3226 static dw_line_info_table *new_line_info_table (void);
3227 static void output_line_info (bool);
3228 static void output_file_names (void);
3229 static dw_die_ref base_type_die (tree);
3230 static int is_base_type (tree);
3231 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3232 static int decl_quals (const_tree);
3233 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3234 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3235 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3236 static int type_is_enum (const_tree);
3237 static unsigned int dbx_reg_number (const_rtx);
3238 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3239 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3240 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3241 enum var_init_status);
3242 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3243 enum var_init_status);
3244 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3245 enum var_init_status);
3246 static int is_based_loc (const_rtx);
3247 static bool resolve_one_addr (rtx *);
3248 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3249 enum var_init_status);
3250 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3251 enum var_init_status);
3252 struct loc_descr_context;
3253 static dw_loc_list_ref loc_list_from_tree (tree, int,
3254 const struct loc_descr_context *);
3255 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3256 const struct loc_descr_context *);
3257 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3258 static tree field_type (const_tree);
3259 static unsigned int simple_type_align_in_bits (const_tree);
3260 static unsigned int simple_decl_align_in_bits (const_tree);
3261 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3262 static HOST_WIDE_INT field_byte_offset (const_tree);
3263 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3264 dw_loc_list_ref);
3265 static void add_data_member_location_attribute (dw_die_ref, tree);
3266 static bool add_const_value_attribute (dw_die_ref, rtx);
3267 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3268 static void insert_wide_int (const wide_int &, unsigned char *, int);
3269 static void insert_float (const_rtx, unsigned char *);
3270 static rtx rtl_for_decl_location (tree);
3271 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3272 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3273 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3274 static void add_name_attribute (dw_die_ref, const char *);
3275 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3276 static void add_comp_dir_attribute (dw_die_ref);
3277 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3278 const struct loc_descr_context *);
3279 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3280 const struct loc_descr_context *);
3281 static void add_subscript_info (dw_die_ref, tree, bool);
3282 static void add_byte_size_attribute (dw_die_ref, tree);
3283 static void add_bit_offset_attribute (dw_die_ref, tree);
3284 static void add_bit_size_attribute (dw_die_ref, tree);
3285 static void add_prototyped_attribute (dw_die_ref, tree);
3286 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3287 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3288 static void add_src_coords_attributes (dw_die_ref, tree);
3289 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3290 static void push_decl_scope (tree);
3291 static void pop_decl_scope (void);
3292 static dw_die_ref scope_die_for (tree, dw_die_ref);
3293 static inline int local_scope_p (dw_die_ref);
3294 static inline int class_scope_p (dw_die_ref);
3295 static inline int class_or_namespace_scope_p (dw_die_ref);
3296 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3297 static void add_calling_convention_attribute (dw_die_ref, tree);
3298 static const char *type_tag (const_tree);
3299 static tree member_declared_type (const_tree);
3300 #if 0
3301 static const char *decl_start_label (tree);
3302 #endif
3303 static void gen_array_type_die (tree, dw_die_ref);
3304 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3305 #if 0
3306 static void gen_entry_point_die (tree, dw_die_ref);
3307 #endif
3308 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3309 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3310 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3311 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3312 static void gen_formal_types_die (tree, dw_die_ref);
3313 static void gen_subprogram_die (tree, dw_die_ref);
3314 static void gen_variable_die (tree, tree, dw_die_ref);
3315 static void gen_const_die (tree, dw_die_ref);
3316 static void gen_label_die (tree, dw_die_ref);
3317 static void gen_lexical_block_die (tree, dw_die_ref);
3318 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3319 static void gen_field_die (tree, dw_die_ref);
3320 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3321 static dw_die_ref gen_compile_unit_die (const char *);
3322 static void gen_inheritance_die (tree, tree, dw_die_ref);
3323 static void gen_member_die (tree, dw_die_ref);
3324 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3325 enum debug_info_usage);
3326 static void gen_subroutine_type_die (tree, dw_die_ref);
3327 static void gen_typedef_die (tree, dw_die_ref);
3328 static void gen_type_die (tree, dw_die_ref);
3329 static void gen_block_die (tree, dw_die_ref);
3330 static void decls_for_scope (tree, dw_die_ref);
3331 static inline int is_redundant_typedef (const_tree);
3332 static bool is_naming_typedef_decl (const_tree);
3333 static inline dw_die_ref get_context_die (tree);
3334 static void gen_namespace_die (tree, dw_die_ref);
3335 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3336 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3337 static dw_die_ref force_decl_die (tree);
3338 static dw_die_ref force_type_die (tree);
3339 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3340 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3341 static struct dwarf_file_data * lookup_filename (const char *);
3342 static void retry_incomplete_types (void);
3343 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3344 static void gen_generic_params_dies (tree);
3345 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3346 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3347 static void splice_child_die (dw_die_ref, dw_die_ref);
3348 static int file_info_cmp (const void *, const void *);
3349 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3350 const char *, const char *);
3351 static void output_loc_list (dw_loc_list_ref);
3352 static char *gen_internal_sym (const char *);
3353 static bool want_pubnames (void);
3355 static void prune_unmark_dies (dw_die_ref);
3356 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3357 static void prune_unused_types_mark (dw_die_ref, int);
3358 static void prune_unused_types_walk (dw_die_ref);
3359 static void prune_unused_types_walk_attribs (dw_die_ref);
3360 static void prune_unused_types_prune (dw_die_ref);
3361 static void prune_unused_types (void);
3362 static int maybe_emit_file (struct dwarf_file_data *fd);
3363 static inline const char *AT_vms_delta1 (dw_attr_node *);
3364 static inline const char *AT_vms_delta2 (dw_attr_node *);
3365 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3366 const char *, const char *);
3367 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3368 static void gen_remaining_tmpl_value_param_die_attribute (void);
3369 static bool generic_type_p (tree);
3370 static void schedule_generic_params_dies_gen (tree t);
3371 static void gen_scheduled_generic_parms_dies (void);
3373 static const char *comp_dir_string (void);
3375 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3377 /* enum for tracking thread-local variables whose address is really an offset
3378 relative to the TLS pointer, which will need link-time relocation, but will
3379 not need relocation by the DWARF consumer. */
3381 enum dtprel_bool
3383 dtprel_false = 0,
3384 dtprel_true = 1
3387 /* Return the operator to use for an address of a variable. For dtprel_true, we
3388 use DW_OP_const*. For regular variables, which need both link-time
3389 relocation and consumer-level relocation (e.g., to account for shared objects
3390 loaded at a random address), we use DW_OP_addr*. */
3392 static inline enum dwarf_location_atom
3393 dw_addr_op (enum dtprel_bool dtprel)
3395 if (dtprel == dtprel_true)
3396 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3397 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3398 else
3399 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3402 /* Return a pointer to a newly allocated address location description. If
3403 dwarf_split_debug_info is true, then record the address with the appropriate
3404 relocation. */
3405 static inline dw_loc_descr_ref
3406 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3408 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3410 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3411 ref->dw_loc_oprnd1.v.val_addr = addr;
3412 ref->dtprel = dtprel;
3413 if (dwarf_split_debug_info)
3414 ref->dw_loc_oprnd1.val_entry
3415 = add_addr_table_entry (addr,
3416 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3417 else
3418 ref->dw_loc_oprnd1.val_entry = NULL;
3420 return ref;
3423 /* Section names used to hold DWARF debugging information. */
3425 #ifndef DEBUG_INFO_SECTION
3426 #define DEBUG_INFO_SECTION ".debug_info"
3427 #endif
3428 #ifndef DEBUG_DWO_INFO_SECTION
3429 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3430 #endif
3431 #ifndef DEBUG_ABBREV_SECTION
3432 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3433 #endif
3434 #ifndef DEBUG_DWO_ABBREV_SECTION
3435 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3436 #endif
3437 #ifndef DEBUG_ARANGES_SECTION
3438 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3439 #endif
3440 #ifndef DEBUG_ADDR_SECTION
3441 #define DEBUG_ADDR_SECTION ".debug_addr"
3442 #endif
3443 #ifndef DEBUG_NORM_MACINFO_SECTION
3444 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3445 #endif
3446 #ifndef DEBUG_DWO_MACINFO_SECTION
3447 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3448 #endif
3449 #ifndef DEBUG_MACINFO_SECTION
3450 #define DEBUG_MACINFO_SECTION \
3451 (!dwarf_split_debug_info \
3452 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3453 #endif
3454 #ifndef DEBUG_NORM_MACRO_SECTION
3455 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3456 #endif
3457 #ifndef DEBUG_DWO_MACRO_SECTION
3458 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3459 #endif
3460 #ifndef DEBUG_MACRO_SECTION
3461 #define DEBUG_MACRO_SECTION \
3462 (!dwarf_split_debug_info \
3463 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3464 #endif
3465 #ifndef DEBUG_LINE_SECTION
3466 #define DEBUG_LINE_SECTION ".debug_line"
3467 #endif
3468 #ifndef DEBUG_DWO_LINE_SECTION
3469 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3470 #endif
3471 #ifndef DEBUG_LOC_SECTION
3472 #define DEBUG_LOC_SECTION ".debug_loc"
3473 #endif
3474 #ifndef DEBUG_DWO_LOC_SECTION
3475 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3476 #endif
3477 #ifndef DEBUG_PUBNAMES_SECTION
3478 #define DEBUG_PUBNAMES_SECTION \
3479 ((debug_generate_pub_sections == 2) \
3480 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3481 #endif
3482 #ifndef DEBUG_PUBTYPES_SECTION
3483 #define DEBUG_PUBTYPES_SECTION \
3484 ((debug_generate_pub_sections == 2) \
3485 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3486 #endif
3487 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3488 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3489 #ifndef DEBUG_STR_OFFSETS_SECTION
3490 #define DEBUG_STR_OFFSETS_SECTION \
3491 (!dwarf_split_debug_info \
3492 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3493 #endif
3494 #ifndef DEBUG_STR_DWO_SECTION
3495 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3496 #endif
3497 #ifndef DEBUG_STR_SECTION
3498 #define DEBUG_STR_SECTION ".debug_str"
3499 #endif
3500 #ifndef DEBUG_RANGES_SECTION
3501 #define DEBUG_RANGES_SECTION ".debug_ranges"
3502 #endif
3504 /* Standard ELF section names for compiled code and data. */
3505 #ifndef TEXT_SECTION_NAME
3506 #define TEXT_SECTION_NAME ".text"
3507 #endif
3509 /* Section flags for .debug_macinfo/.debug_macro section. */
3510 #define DEBUG_MACRO_SECTION_FLAGS \
3511 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3513 /* Section flags for .debug_str section. */
3514 #define DEBUG_STR_SECTION_FLAGS \
3515 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3516 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3517 : SECTION_DEBUG)
3519 /* Section flags for .debug_str.dwo section. */
3520 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3522 /* Labels we insert at beginning sections we can reference instead of
3523 the section names themselves. */
3525 #ifndef TEXT_SECTION_LABEL
3526 #define TEXT_SECTION_LABEL "Ltext"
3527 #endif
3528 #ifndef COLD_TEXT_SECTION_LABEL
3529 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3530 #endif
3531 #ifndef DEBUG_LINE_SECTION_LABEL
3532 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3533 #endif
3534 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3535 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3536 #endif
3537 #ifndef DEBUG_INFO_SECTION_LABEL
3538 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3539 #endif
3540 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3541 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3542 #endif
3543 #ifndef DEBUG_ABBREV_SECTION_LABEL
3544 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3545 #endif
3546 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3547 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3548 #endif
3549 #ifndef DEBUG_ADDR_SECTION_LABEL
3550 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3551 #endif
3552 #ifndef DEBUG_LOC_SECTION_LABEL
3553 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3554 #endif
3555 #ifndef DEBUG_RANGES_SECTION_LABEL
3556 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3557 #endif
3558 #ifndef DEBUG_MACINFO_SECTION_LABEL
3559 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3560 #endif
3561 #ifndef DEBUG_MACRO_SECTION_LABEL
3562 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3563 #endif
3564 #define SKELETON_COMP_DIE_ABBREV 1
3565 #define SKELETON_TYPE_DIE_ABBREV 2
3567 /* Definitions of defaults for formats and names of various special
3568 (artificial) labels which may be generated within this file (when the -g
3569 options is used and DWARF2_DEBUGGING_INFO is in effect.
3570 If necessary, these may be overridden from within the tm.h file, but
3571 typically, overriding these defaults is unnecessary. */
3573 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3574 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3575 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3583 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3584 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3585 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3586 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3588 #ifndef TEXT_END_LABEL
3589 #define TEXT_END_LABEL "Letext"
3590 #endif
3591 #ifndef COLD_END_LABEL
3592 #define COLD_END_LABEL "Letext_cold"
3593 #endif
3594 #ifndef BLOCK_BEGIN_LABEL
3595 #define BLOCK_BEGIN_LABEL "LBB"
3596 #endif
3597 #ifndef BLOCK_END_LABEL
3598 #define BLOCK_END_LABEL "LBE"
3599 #endif
3600 #ifndef LINE_CODE_LABEL
3601 #define LINE_CODE_LABEL "LM"
3602 #endif
3605 /* Return the root of the DIE's built for the current compilation unit. */
3606 static dw_die_ref
3607 comp_unit_die (void)
3609 if (!single_comp_unit_die)
3610 single_comp_unit_die = gen_compile_unit_die (NULL);
3611 return single_comp_unit_die;
3614 /* We allow a language front-end to designate a function that is to be
3615 called to "demangle" any name before it is put into a DIE. */
3617 static const char *(*demangle_name_func) (const char *);
3619 void
3620 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3622 demangle_name_func = func;
3625 /* Test if rtl node points to a pseudo register. */
3627 static inline int
3628 is_pseudo_reg (const_rtx rtl)
3630 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3631 || (GET_CODE (rtl) == SUBREG
3632 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3635 /* Return a reference to a type, with its const and volatile qualifiers
3636 removed. */
3638 static inline tree
3639 type_main_variant (tree type)
3641 type = TYPE_MAIN_VARIANT (type);
3643 /* ??? There really should be only one main variant among any group of
3644 variants of a given type (and all of the MAIN_VARIANT values for all
3645 members of the group should point to that one type) but sometimes the C
3646 front-end messes this up for array types, so we work around that bug
3647 here. */
3648 if (TREE_CODE (type) == ARRAY_TYPE)
3649 while (type != TYPE_MAIN_VARIANT (type))
3650 type = TYPE_MAIN_VARIANT (type);
3652 return type;
3655 /* Return nonzero if the given type node represents a tagged type. */
3657 static inline int
3658 is_tagged_type (const_tree type)
3660 enum tree_code code = TREE_CODE (type);
3662 return (code == RECORD_TYPE || code == UNION_TYPE
3663 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3666 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3668 static void
3669 get_ref_die_offset_label (char *label, dw_die_ref ref)
3671 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3674 /* Return die_offset of a DIE reference to a base type. */
3676 static unsigned long int
3677 get_base_type_offset (dw_die_ref ref)
3679 if (ref->die_offset)
3680 return ref->die_offset;
3681 if (comp_unit_die ()->die_abbrev)
3683 calc_base_type_die_sizes ();
3684 gcc_assert (ref->die_offset);
3686 return ref->die_offset;
3689 /* Return die_offset of a DIE reference other than base type. */
3691 static unsigned long int
3692 get_ref_die_offset (dw_die_ref ref)
3694 gcc_assert (ref->die_offset);
3695 return ref->die_offset;
3698 /* Convert a DIE tag into its string name. */
3700 static const char *
3701 dwarf_tag_name (unsigned int tag)
3703 const char *name = get_DW_TAG_name (tag);
3705 if (name != NULL)
3706 return name;
3708 return "DW_TAG_<unknown>";
3711 /* Convert a DWARF attribute code into its string name. */
3713 static const char *
3714 dwarf_attr_name (unsigned int attr)
3716 const char *name;
3718 switch (attr)
3720 #if VMS_DEBUGGING_INFO
3721 case DW_AT_HP_prologue:
3722 return "DW_AT_HP_prologue";
3723 #else
3724 case DW_AT_MIPS_loop_unroll_factor:
3725 return "DW_AT_MIPS_loop_unroll_factor";
3726 #endif
3728 #if VMS_DEBUGGING_INFO
3729 case DW_AT_HP_epilogue:
3730 return "DW_AT_HP_epilogue";
3731 #else
3732 case DW_AT_MIPS_stride:
3733 return "DW_AT_MIPS_stride";
3734 #endif
3737 name = get_DW_AT_name (attr);
3739 if (name != NULL)
3740 return name;
3742 return "DW_AT_<unknown>";
3745 /* Convert a DWARF value form code into its string name. */
3747 static const char *
3748 dwarf_form_name (unsigned int form)
3750 const char *name = get_DW_FORM_name (form);
3752 if (name != NULL)
3753 return name;
3755 return "DW_FORM_<unknown>";
3758 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3759 instance of an inlined instance of a decl which is local to an inline
3760 function, so we have to trace all of the way back through the origin chain
3761 to find out what sort of node actually served as the original seed for the
3762 given block. */
3764 static tree
3765 decl_ultimate_origin (const_tree decl)
3767 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3768 return NULL_TREE;
3770 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3771 we're trying to output the abstract instance of this function. */
3772 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3773 return NULL_TREE;
3775 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3776 most distant ancestor, this should never happen. */
3777 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3779 return DECL_ABSTRACT_ORIGIN (decl);
3782 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3783 of a virtual function may refer to a base class, so we check the 'this'
3784 parameter. */
3786 static tree
3787 decl_class_context (tree decl)
3789 tree context = NULL_TREE;
3791 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3792 context = DECL_CONTEXT (decl);
3793 else
3794 context = TYPE_MAIN_VARIANT
3795 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3797 if (context && !TYPE_P (context))
3798 context = NULL_TREE;
3800 return context;
3803 /* Add an attribute/value pair to a DIE. */
3805 static inline void
3806 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3808 /* Maybe this should be an assert? */
3809 if (die == NULL)
3810 return;
3812 vec_safe_reserve (die->die_attr, 1);
3813 vec_safe_push (die->die_attr, *attr);
3816 static inline enum dw_val_class
3817 AT_class (dw_attr_node *a)
3819 return a->dw_attr_val.val_class;
3822 /* Return the index for any attribute that will be referenced with a
3823 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3824 are stored in dw_attr_val.v.val_str for reference counting
3825 pruning. */
3827 static inline unsigned int
3828 AT_index (dw_attr_node *a)
3830 if (AT_class (a) == dw_val_class_str)
3831 return a->dw_attr_val.v.val_str->index;
3832 else if (a->dw_attr_val.val_entry != NULL)
3833 return a->dw_attr_val.val_entry->index;
3834 return NOT_INDEXED;
3837 /* Add a flag value attribute to a DIE. */
3839 static inline void
3840 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3842 dw_attr_node attr;
3844 attr.dw_attr = attr_kind;
3845 attr.dw_attr_val.val_class = dw_val_class_flag;
3846 attr.dw_attr_val.val_entry = NULL;
3847 attr.dw_attr_val.v.val_flag = flag;
3848 add_dwarf_attr (die, &attr);
3851 static inline unsigned
3852 AT_flag (dw_attr_node *a)
3854 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3855 return a->dw_attr_val.v.val_flag;
3858 /* Add a signed integer attribute value to a DIE. */
3860 static inline void
3861 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3863 dw_attr_node attr;
3865 attr.dw_attr = attr_kind;
3866 attr.dw_attr_val.val_class = dw_val_class_const;
3867 attr.dw_attr_val.val_entry = NULL;
3868 attr.dw_attr_val.v.val_int = int_val;
3869 add_dwarf_attr (die, &attr);
3872 static inline HOST_WIDE_INT
3873 AT_int (dw_attr_node *a)
3875 gcc_assert (a && AT_class (a) == dw_val_class_const);
3876 return a->dw_attr_val.v.val_int;
3879 /* Add an unsigned integer attribute value to a DIE. */
3881 static inline void
3882 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3883 unsigned HOST_WIDE_INT unsigned_val)
3885 dw_attr_node attr;
3887 attr.dw_attr = attr_kind;
3888 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3889 attr.dw_attr_val.val_entry = NULL;
3890 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3891 add_dwarf_attr (die, &attr);
3894 static inline unsigned HOST_WIDE_INT
3895 AT_unsigned (dw_attr_node *a)
3897 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3898 return a->dw_attr_val.v.val_unsigned;
3901 /* Add an unsigned wide integer attribute value to a DIE. */
3903 static inline void
3904 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3905 const wide_int& w)
3907 dw_attr_node attr;
3909 attr.dw_attr = attr_kind;
3910 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3911 attr.dw_attr_val.val_entry = NULL;
3912 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3913 *attr.dw_attr_val.v.val_wide = w;
3914 add_dwarf_attr (die, &attr);
3917 /* Add an unsigned double integer attribute value to a DIE. */
3919 static inline void
3920 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3921 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3923 dw_attr_node attr;
3925 attr.dw_attr = attr_kind;
3926 attr.dw_attr_val.val_class = dw_val_class_const_double;
3927 attr.dw_attr_val.val_entry = NULL;
3928 attr.dw_attr_val.v.val_double.high = high;
3929 attr.dw_attr_val.v.val_double.low = low;
3930 add_dwarf_attr (die, &attr);
3933 /* Add a floating point attribute value to a DIE and return it. */
3935 static inline void
3936 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3937 unsigned int length, unsigned int elt_size, unsigned char *array)
3939 dw_attr_node attr;
3941 attr.dw_attr = attr_kind;
3942 attr.dw_attr_val.val_class = dw_val_class_vec;
3943 attr.dw_attr_val.val_entry = NULL;
3944 attr.dw_attr_val.v.val_vec.length = length;
3945 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3946 attr.dw_attr_val.v.val_vec.array = array;
3947 add_dwarf_attr (die, &attr);
3950 /* Add an 8-byte data attribute value to a DIE. */
3952 static inline void
3953 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3954 unsigned char data8[8])
3956 dw_attr_node attr;
3958 attr.dw_attr = attr_kind;
3959 attr.dw_attr_val.val_class = dw_val_class_data8;
3960 attr.dw_attr_val.val_entry = NULL;
3961 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3962 add_dwarf_attr (die, &attr);
3965 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3966 dwarf_split_debug_info, address attributes in dies destined for the
3967 final executable have force_direct set to avoid using indexed
3968 references. */
3970 static inline void
3971 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3972 bool force_direct)
3974 dw_attr_node attr;
3975 char * lbl_id;
3977 lbl_id = xstrdup (lbl_low);
3978 attr.dw_attr = DW_AT_low_pc;
3979 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3980 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3981 if (dwarf_split_debug_info && !force_direct)
3982 attr.dw_attr_val.val_entry
3983 = add_addr_table_entry (lbl_id, ate_kind_label);
3984 else
3985 attr.dw_attr_val.val_entry = NULL;
3986 add_dwarf_attr (die, &attr);
3988 attr.dw_attr = DW_AT_high_pc;
3989 if (dwarf_version < 4)
3990 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3991 else
3992 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3993 lbl_id = xstrdup (lbl_high);
3994 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3995 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3996 && dwarf_split_debug_info && !force_direct)
3997 attr.dw_attr_val.val_entry
3998 = add_addr_table_entry (lbl_id, ate_kind_label);
3999 else
4000 attr.dw_attr_val.val_entry = NULL;
4001 add_dwarf_attr (die, &attr);
4004 /* Hash and equality functions for debug_str_hash. */
4006 hashval_t
4007 indirect_string_hasher::hash (indirect_string_node *x)
4009 return htab_hash_string (x->str);
4012 bool
4013 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4015 return strcmp (x1->str, x2) == 0;
4018 /* Add STR to the given string hash table. */
4020 static struct indirect_string_node *
4021 find_AT_string_in_table (const char *str,
4022 hash_table<indirect_string_hasher> *table)
4024 struct indirect_string_node *node;
4026 indirect_string_node **slot
4027 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4028 if (*slot == NULL)
4030 node = ggc_cleared_alloc<indirect_string_node> ();
4031 node->str = ggc_strdup (str);
4032 *slot = node;
4034 else
4035 node = *slot;
4037 node->refcount++;
4038 return node;
4041 /* Add STR to the indirect string hash table. */
4043 static struct indirect_string_node *
4044 find_AT_string (const char *str)
4046 if (! debug_str_hash)
4047 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4049 return find_AT_string_in_table (str, debug_str_hash);
4052 /* Add a string attribute value to a DIE. */
4054 static inline void
4055 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4057 dw_attr_node attr;
4058 struct indirect_string_node *node;
4060 node = find_AT_string (str);
4062 attr.dw_attr = attr_kind;
4063 attr.dw_attr_val.val_class = dw_val_class_str;
4064 attr.dw_attr_val.val_entry = NULL;
4065 attr.dw_attr_val.v.val_str = node;
4066 add_dwarf_attr (die, &attr);
4069 static inline const char *
4070 AT_string (dw_attr_node *a)
4072 gcc_assert (a && AT_class (a) == dw_val_class_str);
4073 return a->dw_attr_val.v.val_str->str;
4076 /* Call this function directly to bypass AT_string_form's logic to put
4077 the string inline in the die. */
4079 static void
4080 set_indirect_string (struct indirect_string_node *node)
4082 char label[32];
4083 /* Already indirect is a no op. */
4084 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4086 gcc_assert (node->label);
4087 return;
4089 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4090 ++dw2_string_counter;
4091 node->label = xstrdup (label);
4093 if (!dwarf_split_debug_info)
4095 node->form = DW_FORM_strp;
4096 node->index = NOT_INDEXED;
4098 else
4100 node->form = DW_FORM_GNU_str_index;
4101 node->index = NO_INDEX_ASSIGNED;
4105 /* Find out whether a string should be output inline in DIE
4106 or out-of-line in .debug_str section. */
4108 static enum dwarf_form
4109 find_string_form (struct indirect_string_node *node)
4111 unsigned int len;
4113 if (node->form)
4114 return node->form;
4116 len = strlen (node->str) + 1;
4118 /* If the string is shorter or equal to the size of the reference, it is
4119 always better to put it inline. */
4120 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4121 return node->form = DW_FORM_string;
4123 /* If we cannot expect the linker to merge strings in .debug_str
4124 section, only put it into .debug_str if it is worth even in this
4125 single module. */
4126 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4127 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4128 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4129 return node->form = DW_FORM_string;
4131 set_indirect_string (node);
4133 return node->form;
4136 /* Find out whether the string referenced from the attribute should be
4137 output inline in DIE or out-of-line in .debug_str section. */
4139 static enum dwarf_form
4140 AT_string_form (dw_attr_node *a)
4142 gcc_assert (a && AT_class (a) == dw_val_class_str);
4143 return find_string_form (a->dw_attr_val.v.val_str);
4146 /* Add a DIE reference attribute value to a DIE. */
4148 static inline void
4149 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4151 dw_attr_node attr;
4153 #ifdef ENABLE_CHECKING
4154 gcc_assert (targ_die != NULL);
4155 #else
4156 /* With LTO we can end up trying to reference something we didn't create
4157 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4158 if (targ_die == NULL)
4159 return;
4160 #endif
4162 attr.dw_attr = attr_kind;
4163 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4164 attr.dw_attr_val.val_entry = NULL;
4165 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4166 attr.dw_attr_val.v.val_die_ref.external = 0;
4167 add_dwarf_attr (die, &attr);
4170 /* Change DIE reference REF to point to NEW_DIE instead. */
4172 static inline void
4173 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4175 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4176 ref->dw_attr_val.v.val_die_ref.die = new_die;
4177 ref->dw_attr_val.v.val_die_ref.external = 0;
4180 /* Add an AT_specification attribute to a DIE, and also make the back
4181 pointer from the specification to the definition. */
4183 static inline void
4184 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4186 add_AT_die_ref (die, DW_AT_specification, targ_die);
4187 gcc_assert (!targ_die->die_definition);
4188 targ_die->die_definition = die;
4191 static inline dw_die_ref
4192 AT_ref (dw_attr_node *a)
4194 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4195 return a->dw_attr_val.v.val_die_ref.die;
4198 static inline int
4199 AT_ref_external (dw_attr_node *a)
4201 if (a && AT_class (a) == dw_val_class_die_ref)
4202 return a->dw_attr_val.v.val_die_ref.external;
4204 return 0;
4207 static inline void
4208 set_AT_ref_external (dw_attr_node *a, int i)
4210 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4211 a->dw_attr_val.v.val_die_ref.external = i;
4214 /* Add an FDE reference attribute value to a DIE. */
4216 static inline void
4217 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4219 dw_attr_node attr;
4221 attr.dw_attr = attr_kind;
4222 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4223 attr.dw_attr_val.val_entry = NULL;
4224 attr.dw_attr_val.v.val_fde_index = targ_fde;
4225 add_dwarf_attr (die, &attr);
4228 /* Add a location description attribute value to a DIE. */
4230 static inline void
4231 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4233 dw_attr_node attr;
4235 attr.dw_attr = attr_kind;
4236 attr.dw_attr_val.val_class = dw_val_class_loc;
4237 attr.dw_attr_val.val_entry = NULL;
4238 attr.dw_attr_val.v.val_loc = loc;
4239 add_dwarf_attr (die, &attr);
4242 static inline dw_loc_descr_ref
4243 AT_loc (dw_attr_node *a)
4245 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4246 return a->dw_attr_val.v.val_loc;
4249 static inline void
4250 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4252 dw_attr_node attr;
4254 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4255 return;
4257 attr.dw_attr = attr_kind;
4258 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4259 attr.dw_attr_val.val_entry = NULL;
4260 attr.dw_attr_val.v.val_loc_list = loc_list;
4261 add_dwarf_attr (die, &attr);
4262 have_location_lists = true;
4265 static inline dw_loc_list_ref
4266 AT_loc_list (dw_attr_node *a)
4268 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4269 return a->dw_attr_val.v.val_loc_list;
4272 static inline dw_loc_list_ref *
4273 AT_loc_list_ptr (dw_attr_node *a)
4275 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4276 return &a->dw_attr_val.v.val_loc_list;
4279 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4281 static hashval_t hash (addr_table_entry *);
4282 static bool equal (addr_table_entry *, addr_table_entry *);
4285 /* Table of entries into the .debug_addr section. */
4287 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4289 /* Hash an address_table_entry. */
4291 hashval_t
4292 addr_hasher::hash (addr_table_entry *a)
4294 inchash::hash hstate;
4295 switch (a->kind)
4297 case ate_kind_rtx:
4298 hstate.add_int (0);
4299 break;
4300 case ate_kind_rtx_dtprel:
4301 hstate.add_int (1);
4302 break;
4303 case ate_kind_label:
4304 return htab_hash_string (a->addr.label);
4305 default:
4306 gcc_unreachable ();
4308 inchash::add_rtx (a->addr.rtl, hstate);
4309 return hstate.end ();
4312 /* Determine equality for two address_table_entries. */
4314 bool
4315 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4317 if (a1->kind != a2->kind)
4318 return 0;
4319 switch (a1->kind)
4321 case ate_kind_rtx:
4322 case ate_kind_rtx_dtprel:
4323 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4324 case ate_kind_label:
4325 return strcmp (a1->addr.label, a2->addr.label) == 0;
4326 default:
4327 gcc_unreachable ();
4331 /* Initialize an addr_table_entry. */
4333 void
4334 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4336 e->kind = kind;
4337 switch (kind)
4339 case ate_kind_rtx:
4340 case ate_kind_rtx_dtprel:
4341 e->addr.rtl = (rtx) addr;
4342 break;
4343 case ate_kind_label:
4344 e->addr.label = (char *) addr;
4345 break;
4347 e->refcount = 0;
4348 e->index = NO_INDEX_ASSIGNED;
4351 /* Add attr to the address table entry to the table. Defer setting an
4352 index until output time. */
4354 static addr_table_entry *
4355 add_addr_table_entry (void *addr, enum ate_kind kind)
4357 addr_table_entry *node;
4358 addr_table_entry finder;
4360 gcc_assert (dwarf_split_debug_info);
4361 if (! addr_index_table)
4362 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4363 init_addr_table_entry (&finder, kind, addr);
4364 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4366 if (*slot == HTAB_EMPTY_ENTRY)
4368 node = ggc_cleared_alloc<addr_table_entry> ();
4369 init_addr_table_entry (node, kind, addr);
4370 *slot = node;
4372 else
4373 node = *slot;
4375 node->refcount++;
4376 return node;
4379 /* Remove an entry from the addr table by decrementing its refcount.
4380 Strictly, decrementing the refcount would be enough, but the
4381 assertion that the entry is actually in the table has found
4382 bugs. */
4384 static void
4385 remove_addr_table_entry (addr_table_entry *entry)
4387 gcc_assert (dwarf_split_debug_info && addr_index_table);
4388 /* After an index is assigned, the table is frozen. */
4389 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4390 entry->refcount--;
4393 /* Given a location list, remove all addresses it refers to from the
4394 address_table. */
4396 static void
4397 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4399 for (; descr; descr = descr->dw_loc_next)
4400 if (descr->dw_loc_oprnd1.val_entry != NULL)
4402 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4403 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4407 /* A helper function for dwarf2out_finish called through
4408 htab_traverse. Assign an addr_table_entry its index. All entries
4409 must be collected into the table when this function is called,
4410 because the indexing code relies on htab_traverse to traverse nodes
4411 in the same order for each run. */
4414 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4416 addr_table_entry *node = *h;
4418 /* Don't index unreferenced nodes. */
4419 if (node->refcount == 0)
4420 return 1;
4422 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4423 node->index = *index;
4424 *index += 1;
4426 return 1;
4429 /* Add an address constant attribute value to a DIE. When using
4430 dwarf_split_debug_info, address attributes in dies destined for the
4431 final executable should be direct references--setting the parameter
4432 force_direct ensures this behavior. */
4434 static inline void
4435 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4436 bool force_direct)
4438 dw_attr_node attr;
4440 attr.dw_attr = attr_kind;
4441 attr.dw_attr_val.val_class = dw_val_class_addr;
4442 attr.dw_attr_val.v.val_addr = addr;
4443 if (dwarf_split_debug_info && !force_direct)
4444 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4445 else
4446 attr.dw_attr_val.val_entry = NULL;
4447 add_dwarf_attr (die, &attr);
4450 /* Get the RTX from to an address DIE attribute. */
4452 static inline rtx
4453 AT_addr (dw_attr_node *a)
4455 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4456 return a->dw_attr_val.v.val_addr;
4459 /* Add a file attribute value to a DIE. */
4461 static inline void
4462 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4463 struct dwarf_file_data *fd)
4465 dw_attr_node attr;
4467 attr.dw_attr = attr_kind;
4468 attr.dw_attr_val.val_class = dw_val_class_file;
4469 attr.dw_attr_val.val_entry = NULL;
4470 attr.dw_attr_val.v.val_file = fd;
4471 add_dwarf_attr (die, &attr);
4474 /* Get the dwarf_file_data from a file DIE attribute. */
4476 static inline struct dwarf_file_data *
4477 AT_file (dw_attr_node *a)
4479 gcc_assert (a && AT_class (a) == dw_val_class_file);
4480 return a->dw_attr_val.v.val_file;
4483 /* Add a vms delta attribute value to a DIE. */
4485 static inline void
4486 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4487 const char *lbl1, const char *lbl2)
4489 dw_attr_node attr;
4491 attr.dw_attr = attr_kind;
4492 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4493 attr.dw_attr_val.val_entry = NULL;
4494 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4495 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4496 add_dwarf_attr (die, &attr);
4499 /* Add a label identifier attribute value to a DIE. */
4501 static inline void
4502 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4503 const char *lbl_id)
4505 dw_attr_node attr;
4507 attr.dw_attr = attr_kind;
4508 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4509 attr.dw_attr_val.val_entry = NULL;
4510 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4511 if (dwarf_split_debug_info)
4512 attr.dw_attr_val.val_entry
4513 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4514 ate_kind_label);
4515 add_dwarf_attr (die, &attr);
4518 /* Add a section offset attribute value to a DIE, an offset into the
4519 debug_line section. */
4521 static inline void
4522 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4523 const char *label)
4525 dw_attr_node attr;
4527 attr.dw_attr = attr_kind;
4528 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4529 attr.dw_attr_val.val_entry = NULL;
4530 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4531 add_dwarf_attr (die, &attr);
4534 /* Add a section offset attribute value to a DIE, an offset into the
4535 debug_macinfo section. */
4537 static inline void
4538 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4539 const char *label)
4541 dw_attr_node attr;
4543 attr.dw_attr = attr_kind;
4544 attr.dw_attr_val.val_class = dw_val_class_macptr;
4545 attr.dw_attr_val.val_entry = NULL;
4546 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4547 add_dwarf_attr (die, &attr);
4550 /* Add an offset attribute value to a DIE. */
4552 static inline void
4553 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4554 unsigned HOST_WIDE_INT offset)
4556 dw_attr_node attr;
4558 attr.dw_attr = attr_kind;
4559 attr.dw_attr_val.val_class = dw_val_class_offset;
4560 attr.dw_attr_val.val_entry = NULL;
4561 attr.dw_attr_val.v.val_offset = offset;
4562 add_dwarf_attr (die, &attr);
4565 /* Add a range_list attribute value to a DIE. When using
4566 dwarf_split_debug_info, address attributes in dies destined for the
4567 final executable should be direct references--setting the parameter
4568 force_direct ensures this behavior. */
4570 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4571 #define RELOCATED_OFFSET (NULL)
4573 static void
4574 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4575 long unsigned int offset, bool force_direct)
4577 dw_attr_node attr;
4579 attr.dw_attr = attr_kind;
4580 attr.dw_attr_val.val_class = dw_val_class_range_list;
4581 /* For the range_list attribute, use val_entry to store whether the
4582 offset should follow split-debug-info or normal semantics. This
4583 value is read in output_range_list_offset. */
4584 if (dwarf_split_debug_info && !force_direct)
4585 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4586 else
4587 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4588 attr.dw_attr_val.v.val_offset = offset;
4589 add_dwarf_attr (die, &attr);
4592 /* Return the start label of a delta attribute. */
4594 static inline const char *
4595 AT_vms_delta1 (dw_attr_node *a)
4597 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4598 return a->dw_attr_val.v.val_vms_delta.lbl1;
4601 /* Return the end label of a delta attribute. */
4603 static inline const char *
4604 AT_vms_delta2 (dw_attr_node *a)
4606 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4607 return a->dw_attr_val.v.val_vms_delta.lbl2;
4610 static inline const char *
4611 AT_lbl (dw_attr_node *a)
4613 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4614 || AT_class (a) == dw_val_class_lineptr
4615 || AT_class (a) == dw_val_class_macptr
4616 || AT_class (a) == dw_val_class_high_pc));
4617 return a->dw_attr_val.v.val_lbl_id;
4620 /* Get the attribute of type attr_kind. */
4622 static dw_attr_node *
4623 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4625 dw_attr_node *a;
4626 unsigned ix;
4627 dw_die_ref spec = NULL;
4629 if (! die)
4630 return NULL;
4632 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4633 if (a->dw_attr == attr_kind)
4634 return a;
4635 else if (a->dw_attr == DW_AT_specification
4636 || a->dw_attr == DW_AT_abstract_origin)
4637 spec = AT_ref (a);
4639 if (spec)
4640 return get_AT (spec, attr_kind);
4642 return NULL;
4645 /* Returns the parent of the declaration of DIE. */
4647 static dw_die_ref
4648 get_die_parent (dw_die_ref die)
4650 dw_die_ref t;
4652 if (!die)
4653 return NULL;
4655 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4656 || (t = get_AT_ref (die, DW_AT_specification)))
4657 die = t;
4659 return die->die_parent;
4662 /* Return the "low pc" attribute value, typically associated with a subprogram
4663 DIE. Return null if the "low pc" attribute is either not present, or if it
4664 cannot be represented as an assembler label identifier. */
4666 static inline const char *
4667 get_AT_low_pc (dw_die_ref die)
4669 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4671 return a ? AT_lbl (a) : NULL;
4674 /* Return the "high pc" attribute value, typically associated with a subprogram
4675 DIE. Return null if the "high pc" attribute is either not present, or if it
4676 cannot be represented as an assembler label identifier. */
4678 static inline const char *
4679 get_AT_hi_pc (dw_die_ref die)
4681 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4683 return a ? AT_lbl (a) : NULL;
4686 /* Return the value of the string attribute designated by ATTR_KIND, or
4687 NULL if it is not present. */
4689 static inline const char *
4690 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4692 dw_attr_node *a = get_AT (die, attr_kind);
4694 return a ? AT_string (a) : NULL;
4697 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4698 if it is not present. */
4700 static inline int
4701 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4703 dw_attr_node *a = get_AT (die, attr_kind);
4705 return a ? AT_flag (a) : 0;
4708 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4709 if it is not present. */
4711 static inline unsigned
4712 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4714 dw_attr_node *a = get_AT (die, attr_kind);
4716 return a ? AT_unsigned (a) : 0;
4719 static inline dw_die_ref
4720 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4722 dw_attr_node *a = get_AT (die, attr_kind);
4724 return a ? AT_ref (a) : NULL;
4727 static inline struct dwarf_file_data *
4728 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4730 dw_attr_node *a = get_AT (die, attr_kind);
4732 return a ? AT_file (a) : NULL;
4735 /* Return TRUE if the language is C++. */
4737 static inline bool
4738 is_cxx (void)
4740 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4742 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4743 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4746 /* Return TRUE if the language is Java. */
4748 static inline bool
4749 is_java (void)
4751 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4753 return lang == DW_LANG_Java;
4756 /* Return TRUE if the language is Fortran. */
4758 static inline bool
4759 is_fortran (void)
4761 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4763 return (lang == DW_LANG_Fortran77
4764 || lang == DW_LANG_Fortran90
4765 || lang == DW_LANG_Fortran95
4766 || lang == DW_LANG_Fortran03
4767 || lang == DW_LANG_Fortran08);
4770 /* Return TRUE if the language is Ada. */
4772 static inline bool
4773 is_ada (void)
4775 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4777 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4780 /* Remove the specified attribute if present. Return TRUE if removal
4781 was successful. */
4783 static bool
4784 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4786 dw_attr_node *a;
4787 unsigned ix;
4789 if (! die)
4790 return false;
4792 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4793 if (a->dw_attr == attr_kind)
4795 if (AT_class (a) == dw_val_class_str)
4796 if (a->dw_attr_val.v.val_str->refcount)
4797 a->dw_attr_val.v.val_str->refcount--;
4799 /* vec::ordered_remove should help reduce the number of abbrevs
4800 that are needed. */
4801 die->die_attr->ordered_remove (ix);
4802 return true;
4804 return false;
4807 /* Remove CHILD from its parent. PREV must have the property that
4808 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4810 static void
4811 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4813 gcc_assert (child->die_parent == prev->die_parent);
4814 gcc_assert (prev->die_sib == child);
4815 if (prev == child)
4817 gcc_assert (child->die_parent->die_child == child);
4818 prev = NULL;
4820 else
4821 prev->die_sib = child->die_sib;
4822 if (child->die_parent->die_child == child)
4823 child->die_parent->die_child = prev;
4826 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4827 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4829 static void
4830 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4832 dw_die_ref parent = old_child->die_parent;
4834 gcc_assert (parent == prev->die_parent);
4835 gcc_assert (prev->die_sib == old_child);
4837 new_child->die_parent = parent;
4838 if (prev == old_child)
4840 gcc_assert (parent->die_child == old_child);
4841 new_child->die_sib = new_child;
4843 else
4845 prev->die_sib = new_child;
4846 new_child->die_sib = old_child->die_sib;
4848 if (old_child->die_parent->die_child == old_child)
4849 old_child->die_parent->die_child = new_child;
4852 /* Move all children from OLD_PARENT to NEW_PARENT. */
4854 static void
4855 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4857 dw_die_ref c;
4858 new_parent->die_child = old_parent->die_child;
4859 old_parent->die_child = NULL;
4860 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4863 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4864 matches TAG. */
4866 static void
4867 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4869 dw_die_ref c;
4871 c = die->die_child;
4872 if (c) do {
4873 dw_die_ref prev = c;
4874 c = c->die_sib;
4875 while (c->die_tag == tag)
4877 remove_child_with_prev (c, prev);
4878 c->die_parent = NULL;
4879 /* Might have removed every child. */
4880 if (c == c->die_sib)
4881 return;
4882 c = c->die_sib;
4884 } while (c != die->die_child);
4887 /* Add a CHILD_DIE as the last child of DIE. */
4889 static void
4890 add_child_die (dw_die_ref die, dw_die_ref child_die)
4892 /* FIXME this should probably be an assert. */
4893 if (! die || ! child_die)
4894 return;
4895 gcc_assert (die != child_die);
4897 child_die->die_parent = die;
4898 if (die->die_child)
4900 child_die->die_sib = die->die_child->die_sib;
4901 die->die_child->die_sib = child_die;
4903 else
4904 child_die->die_sib = child_die;
4905 die->die_child = child_die;
4908 /* Unassociate CHILD from its parent, and make its parent be
4909 NEW_PARENT. */
4911 static void
4912 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4914 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4915 if (p->die_sib == child)
4917 remove_child_with_prev (child, p);
4918 break;
4920 add_child_die (new_parent, child);
4923 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4924 is the specification, to the end of PARENT's list of children.
4925 This is done by removing and re-adding it. */
4927 static void
4928 splice_child_die (dw_die_ref parent, dw_die_ref child)
4930 /* We want the declaration DIE from inside the class, not the
4931 specification DIE at toplevel. */
4932 if (child->die_parent != parent)
4934 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4936 if (tmp)
4937 child = tmp;
4940 gcc_assert (child->die_parent == parent
4941 || (child->die_parent
4942 == get_AT_ref (parent, DW_AT_specification)));
4944 reparent_child (child, parent);
4947 /* Create and return a new die with a parent of PARENT_DIE. If
4948 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4949 associated tree T must be supplied to determine parenthood
4950 later. */
4952 static inline dw_die_ref
4953 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4955 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4957 die->die_tag = tag_value;
4959 if (parent_die != NULL)
4960 add_child_die (parent_die, die);
4961 else
4963 limbo_die_node *limbo_node;
4965 /* No DIEs created after early dwarf should end up in limbo,
4966 because the limbo list should not persist past LTO
4967 streaming. */
4968 if (tag_value != DW_TAG_compile_unit
4969 /* These are allowed because they're generated while
4970 breaking out COMDAT units late. */
4971 && tag_value != DW_TAG_type_unit
4972 && !early_dwarf
4973 /* Allow nested functions to live in limbo because they will
4974 only temporarily live there, as decls_for_scope will fix
4975 them up. */
4976 && (TREE_CODE (t) != FUNCTION_DECL
4977 || !decl_function_context (t))
4978 /* Same as nested functions above but for types. Types that
4979 are local to a function will be fixed in
4980 decls_for_scope. */
4981 && (!RECORD_OR_UNION_TYPE_P (t)
4982 || !TYPE_CONTEXT (t)
4983 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4984 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4985 especially in the ltrans stage, but once we implement LTO
4986 dwarf streaming, we should remove this exception. */
4987 && !in_lto_p)
4989 fprintf (stderr, "symbol ended up in limbo too late:");
4990 debug_generic_stmt (t);
4991 gcc_unreachable ();
4994 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4995 limbo_node->die = die;
4996 limbo_node->created_for = t;
4997 limbo_node->next = limbo_die_list;
4998 limbo_die_list = limbo_node;
5001 return die;
5004 /* Return the DIE associated with the given type specifier. */
5006 static inline dw_die_ref
5007 lookup_type_die (tree type)
5009 return TYPE_SYMTAB_DIE (type);
5012 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5013 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5014 anonymous type instead the one of the naming typedef. */
5016 static inline dw_die_ref
5017 strip_naming_typedef (tree type, dw_die_ref type_die)
5019 if (type
5020 && TREE_CODE (type) == RECORD_TYPE
5021 && type_die
5022 && type_die->die_tag == DW_TAG_typedef
5023 && is_naming_typedef_decl (TYPE_NAME (type)))
5024 type_die = get_AT_ref (type_die, DW_AT_type);
5025 return type_die;
5028 /* Like lookup_type_die, but if type is an anonymous type named by a
5029 typedef[1], return the DIE of the anonymous type instead the one of
5030 the naming typedef. This is because in gen_typedef_die, we did
5031 equate the anonymous struct named by the typedef with the DIE of
5032 the naming typedef. So by default, lookup_type_die on an anonymous
5033 struct yields the DIE of the naming typedef.
5035 [1]: Read the comment of is_naming_typedef_decl to learn about what
5036 a naming typedef is. */
5038 static inline dw_die_ref
5039 lookup_type_die_strip_naming_typedef (tree type)
5041 dw_die_ref die = lookup_type_die (type);
5042 return strip_naming_typedef (type, die);
5045 /* Equate a DIE to a given type specifier. */
5047 static inline void
5048 equate_type_number_to_die (tree type, dw_die_ref type_die)
5050 TYPE_SYMTAB_DIE (type) = type_die;
5053 /* Returns a hash value for X (which really is a die_struct). */
5055 inline hashval_t
5056 decl_die_hasher::hash (die_node *x)
5058 return (hashval_t) x->decl_id;
5061 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5063 inline bool
5064 decl_die_hasher::equal (die_node *x, tree y)
5066 return (x->decl_id == DECL_UID (y));
5069 /* Return the DIE associated with a given declaration. */
5071 static inline dw_die_ref
5072 lookup_decl_die (tree decl)
5074 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5077 /* Returns a hash value for X (which really is a var_loc_list). */
5079 inline hashval_t
5080 decl_loc_hasher::hash (var_loc_list *x)
5082 return (hashval_t) x->decl_id;
5085 /* Return nonzero if decl_id of var_loc_list X is the same as
5086 UID of decl *Y. */
5088 inline bool
5089 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5091 return (x->decl_id == DECL_UID (y));
5094 /* Return the var_loc list associated with a given declaration. */
5096 static inline var_loc_list *
5097 lookup_decl_loc (const_tree decl)
5099 if (!decl_loc_table)
5100 return NULL;
5101 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5104 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5106 inline hashval_t
5107 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5109 return (hashval_t) x->decl_id;
5112 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5113 UID of decl *Y. */
5115 inline bool
5116 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5118 return (x->decl_id == DECL_UID (y));
5121 /* Equate a DIE to a particular declaration. */
5123 static void
5124 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5126 unsigned int decl_id = DECL_UID (decl);
5128 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5129 decl_die->decl_id = decl_id;
5132 /* Return how many bits covers PIECE EXPR_LIST. */
5134 static HOST_WIDE_INT
5135 decl_piece_bitsize (rtx piece)
5137 int ret = (int) GET_MODE (piece);
5138 if (ret)
5139 return ret;
5140 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5141 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5142 return INTVAL (XEXP (XEXP (piece, 0), 0));
5145 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5147 static rtx *
5148 decl_piece_varloc_ptr (rtx piece)
5150 if ((int) GET_MODE (piece))
5151 return &XEXP (piece, 0);
5152 else
5153 return &XEXP (XEXP (piece, 0), 1);
5156 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5157 Next is the chain of following piece nodes. */
5159 static rtx_expr_list *
5160 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5162 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5163 return alloc_EXPR_LIST (bitsize, loc_note, next);
5164 else
5165 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5166 GEN_INT (bitsize),
5167 loc_note), next);
5170 /* Return rtx that should be stored into loc field for
5171 LOC_NOTE and BITPOS/BITSIZE. */
5173 static rtx
5174 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5175 HOST_WIDE_INT bitsize)
5177 if (bitsize != -1)
5179 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5180 if (bitpos != 0)
5181 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5183 return loc_note;
5186 /* This function either modifies location piece list *DEST in
5187 place (if SRC and INNER is NULL), or copies location piece list
5188 *SRC to *DEST while modifying it. Location BITPOS is modified
5189 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5190 not copied and if needed some padding around it is added.
5191 When modifying in place, DEST should point to EXPR_LIST where
5192 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5193 to the start of the whole list and INNER points to the EXPR_LIST
5194 where earlier pieces cover PIECE_BITPOS bits. */
5196 static void
5197 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5198 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5199 HOST_WIDE_INT bitsize, rtx loc_note)
5201 HOST_WIDE_INT diff;
5202 bool copy = inner != NULL;
5204 if (copy)
5206 /* First copy all nodes preceding the current bitpos. */
5207 while (src != inner)
5209 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5210 decl_piece_bitsize (*src), NULL_RTX);
5211 dest = &XEXP (*dest, 1);
5212 src = &XEXP (*src, 1);
5215 /* Add padding if needed. */
5216 if (bitpos != piece_bitpos)
5218 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5219 copy ? NULL_RTX : *dest);
5220 dest = &XEXP (*dest, 1);
5222 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5224 gcc_assert (!copy);
5225 /* A piece with correct bitpos and bitsize already exist,
5226 just update the location for it and return. */
5227 *decl_piece_varloc_ptr (*dest) = loc_note;
5228 return;
5230 /* Add the piece that changed. */
5231 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5232 dest = &XEXP (*dest, 1);
5233 /* Skip over pieces that overlap it. */
5234 diff = bitpos - piece_bitpos + bitsize;
5235 if (!copy)
5236 src = dest;
5237 while (diff > 0 && *src)
5239 rtx piece = *src;
5240 diff -= decl_piece_bitsize (piece);
5241 if (copy)
5242 src = &XEXP (piece, 1);
5243 else
5245 *src = XEXP (piece, 1);
5246 free_EXPR_LIST_node (piece);
5249 /* Add padding if needed. */
5250 if (diff < 0 && *src)
5252 if (!copy)
5253 dest = src;
5254 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5255 dest = &XEXP (*dest, 1);
5257 if (!copy)
5258 return;
5259 /* Finally copy all nodes following it. */
5260 while (*src)
5262 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5263 decl_piece_bitsize (*src), NULL_RTX);
5264 dest = &XEXP (*dest, 1);
5265 src = &XEXP (*src, 1);
5269 /* Add a variable location node to the linked list for DECL. */
5271 static struct var_loc_node *
5272 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5274 unsigned int decl_id;
5275 var_loc_list *temp;
5276 struct var_loc_node *loc = NULL;
5277 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5279 if (TREE_CODE (decl) == VAR_DECL
5280 && DECL_HAS_DEBUG_EXPR_P (decl))
5282 tree realdecl = DECL_DEBUG_EXPR (decl);
5283 if (handled_component_p (realdecl)
5284 || (TREE_CODE (realdecl) == MEM_REF
5285 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5287 HOST_WIDE_INT maxsize;
5288 tree innerdecl;
5289 innerdecl
5290 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5291 if (!DECL_P (innerdecl)
5292 || DECL_IGNORED_P (innerdecl)
5293 || TREE_STATIC (innerdecl)
5294 || bitsize <= 0
5295 || bitpos + bitsize > 256
5296 || bitsize != maxsize)
5297 return NULL;
5298 decl = innerdecl;
5302 decl_id = DECL_UID (decl);
5303 var_loc_list **slot
5304 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5305 if (*slot == NULL)
5307 temp = ggc_cleared_alloc<var_loc_list> ();
5308 temp->decl_id = decl_id;
5309 *slot = temp;
5311 else
5312 temp = *slot;
5314 /* For PARM_DECLs try to keep around the original incoming value,
5315 even if that means we'll emit a zero-range .debug_loc entry. */
5316 if (temp->last
5317 && temp->first == temp->last
5318 && TREE_CODE (decl) == PARM_DECL
5319 && NOTE_P (temp->first->loc)
5320 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5321 && DECL_INCOMING_RTL (decl)
5322 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5323 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5324 == GET_CODE (DECL_INCOMING_RTL (decl))
5325 && prev_real_insn (temp->first->loc) == NULL_RTX
5326 && (bitsize != -1
5327 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5328 NOTE_VAR_LOCATION_LOC (loc_note))
5329 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5330 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5332 loc = ggc_cleared_alloc<var_loc_node> ();
5333 temp->first->next = loc;
5334 temp->last = loc;
5335 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5337 else if (temp->last)
5339 struct var_loc_node *last = temp->last, *unused = NULL;
5340 rtx *piece_loc = NULL, last_loc_note;
5341 HOST_WIDE_INT piece_bitpos = 0;
5342 if (last->next)
5344 last = last->next;
5345 gcc_assert (last->next == NULL);
5347 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5349 piece_loc = &last->loc;
5352 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5353 if (piece_bitpos + cur_bitsize > bitpos)
5354 break;
5355 piece_bitpos += cur_bitsize;
5356 piece_loc = &XEXP (*piece_loc, 1);
5358 while (*piece_loc);
5360 /* TEMP->LAST here is either pointer to the last but one or
5361 last element in the chained list, LAST is pointer to the
5362 last element. */
5363 if (label && strcmp (last->label, label) == 0)
5365 /* For SRA optimized variables if there weren't any real
5366 insns since last note, just modify the last node. */
5367 if (piece_loc != NULL)
5369 adjust_piece_list (piece_loc, NULL, NULL,
5370 bitpos, piece_bitpos, bitsize, loc_note);
5371 return NULL;
5373 /* If the last note doesn't cover any instructions, remove it. */
5374 if (temp->last != last)
5376 temp->last->next = NULL;
5377 unused = last;
5378 last = temp->last;
5379 gcc_assert (strcmp (last->label, label) != 0);
5381 else
5383 gcc_assert (temp->first == temp->last
5384 || (temp->first->next == temp->last
5385 && TREE_CODE (decl) == PARM_DECL));
5386 memset (temp->last, '\0', sizeof (*temp->last));
5387 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5388 return temp->last;
5391 if (bitsize == -1 && NOTE_P (last->loc))
5392 last_loc_note = last->loc;
5393 else if (piece_loc != NULL
5394 && *piece_loc != NULL_RTX
5395 && piece_bitpos == bitpos
5396 && decl_piece_bitsize (*piece_loc) == bitsize)
5397 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5398 else
5399 last_loc_note = NULL_RTX;
5400 /* If the current location is the same as the end of the list,
5401 and either both or neither of the locations is uninitialized,
5402 we have nothing to do. */
5403 if (last_loc_note == NULL_RTX
5404 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5405 NOTE_VAR_LOCATION_LOC (loc_note)))
5406 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5407 != NOTE_VAR_LOCATION_STATUS (loc_note))
5408 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5409 == VAR_INIT_STATUS_UNINITIALIZED)
5410 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5411 == VAR_INIT_STATUS_UNINITIALIZED))))
5413 /* Add LOC to the end of list and update LAST. If the last
5414 element of the list has been removed above, reuse its
5415 memory for the new node, otherwise allocate a new one. */
5416 if (unused)
5418 loc = unused;
5419 memset (loc, '\0', sizeof (*loc));
5421 else
5422 loc = ggc_cleared_alloc<var_loc_node> ();
5423 if (bitsize == -1 || piece_loc == NULL)
5424 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5425 else
5426 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5427 bitpos, piece_bitpos, bitsize, loc_note);
5428 last->next = loc;
5429 /* Ensure TEMP->LAST will point either to the new last but one
5430 element of the chain, or to the last element in it. */
5431 if (last != temp->last)
5432 temp->last = last;
5434 else if (unused)
5435 ggc_free (unused);
5437 else
5439 loc = ggc_cleared_alloc<var_loc_node> ();
5440 temp->first = loc;
5441 temp->last = loc;
5442 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5444 return loc;
5447 /* Keep track of the number of spaces used to indent the
5448 output of the debugging routines that print the structure of
5449 the DIE internal representation. */
5450 static int print_indent;
5452 /* Indent the line the number of spaces given by print_indent. */
5454 static inline void
5455 print_spaces (FILE *outfile)
5457 fprintf (outfile, "%*s", print_indent, "");
5460 /* Print a type signature in hex. */
5462 static inline void
5463 print_signature (FILE *outfile, char *sig)
5465 int i;
5467 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5468 fprintf (outfile, "%02x", sig[i] & 0xff);
5471 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5473 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5474 RECURSE, output location descriptor operations. */
5476 static void
5477 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5479 switch (val->val_class)
5481 case dw_val_class_addr:
5482 fprintf (outfile, "address");
5483 break;
5484 case dw_val_class_offset:
5485 fprintf (outfile, "offset");
5486 break;
5487 case dw_val_class_loc:
5488 fprintf (outfile, "location descriptor");
5489 if (val->v.val_loc == NULL)
5490 fprintf (outfile, " -> <null>\n");
5491 else if (recurse)
5493 fprintf (outfile, ":\n");
5494 print_indent += 4;
5495 print_loc_descr (val->v.val_loc, outfile);
5496 print_indent -= 4;
5498 else
5499 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5500 break;
5501 case dw_val_class_loc_list:
5502 fprintf (outfile, "location list -> label:%s",
5503 val->v.val_loc_list->ll_symbol);
5504 break;
5505 case dw_val_class_range_list:
5506 fprintf (outfile, "range list");
5507 break;
5508 case dw_val_class_const:
5509 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5510 break;
5511 case dw_val_class_unsigned_const:
5512 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5513 break;
5514 case dw_val_class_const_double:
5515 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5516 HOST_WIDE_INT_PRINT_UNSIGNED")",
5517 val->v.val_double.high,
5518 val->v.val_double.low);
5519 break;
5520 case dw_val_class_wide_int:
5522 int i = val->v.val_wide->get_len ();
5523 fprintf (outfile, "constant (");
5524 gcc_assert (i > 0);
5525 if (val->v.val_wide->elt (i - 1) == 0)
5526 fprintf (outfile, "0x");
5527 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5528 val->v.val_wide->elt (--i));
5529 while (--i >= 0)
5530 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5531 val->v.val_wide->elt (i));
5532 fprintf (outfile, ")");
5533 break;
5535 case dw_val_class_vec:
5536 fprintf (outfile, "floating-point or vector constant");
5537 break;
5538 case dw_val_class_flag:
5539 fprintf (outfile, "%u", val->v.val_flag);
5540 break;
5541 case dw_val_class_die_ref:
5542 if (val->v.val_die_ref.die != NULL)
5544 dw_die_ref die = val->v.val_die_ref.die;
5546 if (die->comdat_type_p)
5548 fprintf (outfile, "die -> signature: ");
5549 print_signature (outfile,
5550 die->die_id.die_type_node->signature);
5552 else if (die->die_id.die_symbol)
5553 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5554 else
5555 fprintf (outfile, "die -> %ld", die->die_offset);
5556 fprintf (outfile, " (%p)", (void *) die);
5558 else
5559 fprintf (outfile, "die -> <null>");
5560 break;
5561 case dw_val_class_vms_delta:
5562 fprintf (outfile, "delta: @slotcount(%s-%s)",
5563 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5564 break;
5565 case dw_val_class_lbl_id:
5566 case dw_val_class_lineptr:
5567 case dw_val_class_macptr:
5568 case dw_val_class_high_pc:
5569 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5570 break;
5571 case dw_val_class_str:
5572 if (val->v.val_str->str != NULL)
5573 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5574 else
5575 fprintf (outfile, "<null>");
5576 break;
5577 case dw_val_class_file:
5578 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5579 val->v.val_file->emitted_number);
5580 break;
5581 case dw_val_class_data8:
5583 int i;
5585 for (i = 0; i < 8; i++)
5586 fprintf (outfile, "%02x", val->v.val_data8[i]);
5587 break;
5589 default:
5590 break;
5594 /* Likewise, for a DIE attribute. */
5596 static void
5597 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5599 print_dw_val (&a->dw_attr_val, recurse, outfile);
5603 /* Print the list of operands in the LOC location description to OUTFILE. This
5604 routine is a debugging aid only. */
5606 static void
5607 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5609 dw_loc_descr_ref l = loc;
5611 if (loc == NULL)
5613 print_spaces (outfile);
5614 fprintf (outfile, "<null>\n");
5615 return;
5618 for (l = loc; l != NULL; l = l->dw_loc_next)
5620 print_spaces (outfile);
5621 fprintf (outfile, "(%p) %s",
5622 (void *) l,
5623 dwarf_stack_op_name (l->dw_loc_opc));
5624 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5626 fprintf (outfile, " ");
5627 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5629 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5631 fprintf (outfile, ", ");
5632 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5634 fprintf (outfile, "\n");
5638 /* Print the information associated with a given DIE, and its children.
5639 This routine is a debugging aid only. */
5641 static void
5642 print_die (dw_die_ref die, FILE *outfile)
5644 dw_attr_node *a;
5645 dw_die_ref c;
5646 unsigned ix;
5648 print_spaces (outfile);
5649 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5650 die->die_offset, dwarf_tag_name (die->die_tag),
5651 (void*) die);
5652 print_spaces (outfile);
5653 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5654 fprintf (outfile, " offset: %ld", die->die_offset);
5655 fprintf (outfile, " mark: %d\n", die->die_mark);
5657 if (die->comdat_type_p)
5659 print_spaces (outfile);
5660 fprintf (outfile, " signature: ");
5661 print_signature (outfile, die->die_id.die_type_node->signature);
5662 fprintf (outfile, "\n");
5665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5667 print_spaces (outfile);
5668 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5670 print_attribute (a, true, outfile);
5671 fprintf (outfile, "\n");
5674 if (die->die_child != NULL)
5676 print_indent += 4;
5677 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5678 print_indent -= 4;
5680 if (print_indent == 0)
5681 fprintf (outfile, "\n");
5684 /* Print the list of operations in the LOC location description. */
5686 DEBUG_FUNCTION void
5687 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5689 print_loc_descr (loc, stderr);
5692 /* Print the information collected for a given DIE. */
5694 DEBUG_FUNCTION void
5695 debug_dwarf_die (dw_die_ref die)
5697 print_die (die, stderr);
5700 DEBUG_FUNCTION void
5701 debug (die_struct &ref)
5703 print_die (&ref, stderr);
5706 DEBUG_FUNCTION void
5707 debug (die_struct *ptr)
5709 if (ptr)
5710 debug (*ptr);
5711 else
5712 fprintf (stderr, "<nil>\n");
5716 /* Print all DWARF information collected for the compilation unit.
5717 This routine is a debugging aid only. */
5719 DEBUG_FUNCTION void
5720 debug_dwarf (void)
5722 print_indent = 0;
5723 print_die (comp_unit_die (), stderr);
5726 #ifdef ENABLE_CHECKING
5727 /* Sanity checks on DIEs. */
5729 static void
5730 check_die (dw_die_ref die)
5732 unsigned ix;
5733 dw_attr_node *a;
5734 bool inline_found = false;
5735 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5736 int n_decl_line = 0, n_decl_file = 0;
5737 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5739 switch (a->dw_attr)
5741 case DW_AT_inline:
5742 if (a->dw_attr_val.v.val_unsigned)
5743 inline_found = true;
5744 break;
5745 case DW_AT_location:
5746 ++n_location;
5747 break;
5748 case DW_AT_low_pc:
5749 ++n_low_pc;
5750 break;
5751 case DW_AT_high_pc:
5752 ++n_high_pc;
5753 break;
5754 case DW_AT_artificial:
5755 ++n_artificial;
5756 break;
5757 case DW_AT_decl_line:
5758 ++n_decl_line;
5759 break;
5760 case DW_AT_decl_file:
5761 ++n_decl_file;
5762 break;
5763 default:
5764 break;
5767 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5768 || n_decl_line > 1 || n_decl_file > 1)
5770 fprintf (stderr, "Duplicate attributes in DIE:\n");
5771 debug_dwarf_die (die);
5772 gcc_unreachable ();
5774 if (inline_found)
5776 /* A debugging information entry that is a member of an abstract
5777 instance tree [that has DW_AT_inline] should not contain any
5778 attributes which describe aspects of the subroutine which vary
5779 between distinct inlined expansions or distinct out-of-line
5780 expansions. */
5781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5782 gcc_assert (a->dw_attr != DW_AT_low_pc
5783 && a->dw_attr != DW_AT_high_pc
5784 && a->dw_attr != DW_AT_location
5785 && a->dw_attr != DW_AT_frame_base
5786 && a->dw_attr != DW_AT_GNU_all_call_sites);
5789 #endif
5791 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5792 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5793 DIE that marks the start of the DIEs for this include file. */
5795 static dw_die_ref
5796 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5798 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5799 dw_die_ref new_unit = gen_compile_unit_die (filename);
5801 new_unit->die_sib = old_unit;
5802 return new_unit;
5805 /* Close an include-file CU and reopen the enclosing one. */
5807 static dw_die_ref
5808 pop_compile_unit (dw_die_ref old_unit)
5810 dw_die_ref new_unit = old_unit->die_sib;
5812 old_unit->die_sib = NULL;
5813 return new_unit;
5816 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5817 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5818 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5820 /* Calculate the checksum of a location expression. */
5822 static inline void
5823 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5825 int tem;
5826 inchash::hash hstate;
5827 hashval_t hash;
5829 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5830 CHECKSUM (tem);
5831 hash_loc_operands (loc, hstate);
5832 hash = hstate.end();
5833 CHECKSUM (hash);
5836 /* Calculate the checksum of an attribute. */
5838 static void
5839 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5841 dw_loc_descr_ref loc;
5842 rtx r;
5844 CHECKSUM (at->dw_attr);
5846 /* We don't care that this was compiled with a different compiler
5847 snapshot; if the output is the same, that's what matters. */
5848 if (at->dw_attr == DW_AT_producer)
5849 return;
5851 switch (AT_class (at))
5853 case dw_val_class_const:
5854 CHECKSUM (at->dw_attr_val.v.val_int);
5855 break;
5856 case dw_val_class_unsigned_const:
5857 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5858 break;
5859 case dw_val_class_const_double:
5860 CHECKSUM (at->dw_attr_val.v.val_double);
5861 break;
5862 case dw_val_class_wide_int:
5863 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5864 get_full_len (*at->dw_attr_val.v.val_wide)
5865 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5866 break;
5867 case dw_val_class_vec:
5868 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5869 (at->dw_attr_val.v.val_vec.length
5870 * at->dw_attr_val.v.val_vec.elt_size));
5871 break;
5872 case dw_val_class_flag:
5873 CHECKSUM (at->dw_attr_val.v.val_flag);
5874 break;
5875 case dw_val_class_str:
5876 CHECKSUM_STRING (AT_string (at));
5877 break;
5879 case dw_val_class_addr:
5880 r = AT_addr (at);
5881 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5882 CHECKSUM_STRING (XSTR (r, 0));
5883 break;
5885 case dw_val_class_offset:
5886 CHECKSUM (at->dw_attr_val.v.val_offset);
5887 break;
5889 case dw_val_class_loc:
5890 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5891 loc_checksum (loc, ctx);
5892 break;
5894 case dw_val_class_die_ref:
5895 die_checksum (AT_ref (at), ctx, mark);
5896 break;
5898 case dw_val_class_fde_ref:
5899 case dw_val_class_vms_delta:
5900 case dw_val_class_lbl_id:
5901 case dw_val_class_lineptr:
5902 case dw_val_class_macptr:
5903 case dw_val_class_high_pc:
5904 break;
5906 case dw_val_class_file:
5907 CHECKSUM_STRING (AT_file (at)->filename);
5908 break;
5910 case dw_val_class_data8:
5911 CHECKSUM (at->dw_attr_val.v.val_data8);
5912 break;
5914 default:
5915 break;
5919 /* Calculate the checksum of a DIE. */
5921 static void
5922 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5924 dw_die_ref c;
5925 dw_attr_node *a;
5926 unsigned ix;
5928 /* To avoid infinite recursion. */
5929 if (die->die_mark)
5931 CHECKSUM (die->die_mark);
5932 return;
5934 die->die_mark = ++(*mark);
5936 CHECKSUM (die->die_tag);
5938 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5939 attr_checksum (a, ctx, mark);
5941 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5944 #undef CHECKSUM
5945 #undef CHECKSUM_BLOCK
5946 #undef CHECKSUM_STRING
5948 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5949 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5950 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5951 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5952 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5953 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5954 #define CHECKSUM_ATTR(FOO) \
5955 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5957 /* Calculate the checksum of a number in signed LEB128 format. */
5959 static void
5960 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5962 unsigned char byte;
5963 bool more;
5965 while (1)
5967 byte = (value & 0x7f);
5968 value >>= 7;
5969 more = !((value == 0 && (byte & 0x40) == 0)
5970 || (value == -1 && (byte & 0x40) != 0));
5971 if (more)
5972 byte |= 0x80;
5973 CHECKSUM (byte);
5974 if (!more)
5975 break;
5979 /* Calculate the checksum of a number in unsigned LEB128 format. */
5981 static void
5982 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5984 while (1)
5986 unsigned char byte = (value & 0x7f);
5987 value >>= 7;
5988 if (value != 0)
5989 /* More bytes to follow. */
5990 byte |= 0x80;
5991 CHECKSUM (byte);
5992 if (value == 0)
5993 break;
5997 /* Checksum the context of the DIE. This adds the names of any
5998 surrounding namespaces or structures to the checksum. */
6000 static void
6001 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6003 const char *name;
6004 dw_die_ref spec;
6005 int tag = die->die_tag;
6007 if (tag != DW_TAG_namespace
6008 && tag != DW_TAG_structure_type
6009 && tag != DW_TAG_class_type)
6010 return;
6012 name = get_AT_string (die, DW_AT_name);
6014 spec = get_AT_ref (die, DW_AT_specification);
6015 if (spec != NULL)
6016 die = spec;
6018 if (die->die_parent != NULL)
6019 checksum_die_context (die->die_parent, ctx);
6021 CHECKSUM_ULEB128 ('C');
6022 CHECKSUM_ULEB128 (tag);
6023 if (name != NULL)
6024 CHECKSUM_STRING (name);
6027 /* Calculate the checksum of a location expression. */
6029 static inline void
6030 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6032 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6033 were emitted as a DW_FORM_sdata instead of a location expression. */
6034 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6036 CHECKSUM_ULEB128 (DW_FORM_sdata);
6037 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6038 return;
6041 /* Otherwise, just checksum the raw location expression. */
6042 while (loc != NULL)
6044 inchash::hash hstate;
6045 hashval_t hash;
6047 CHECKSUM_ULEB128 (loc->dtprel);
6048 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6049 hash_loc_operands (loc, hstate);
6050 hash = hstate.end ();
6051 CHECKSUM (hash);
6052 loc = loc->dw_loc_next;
6056 /* Calculate the checksum of an attribute. */
6058 static void
6059 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6060 struct md5_ctx *ctx, int *mark)
6062 dw_loc_descr_ref loc;
6063 rtx r;
6065 if (AT_class (at) == dw_val_class_die_ref)
6067 dw_die_ref target_die = AT_ref (at);
6069 /* For pointer and reference types, we checksum only the (qualified)
6070 name of the target type (if there is a name). For friend entries,
6071 we checksum only the (qualified) name of the target type or function.
6072 This allows the checksum to remain the same whether the target type
6073 is complete or not. */
6074 if ((at->dw_attr == DW_AT_type
6075 && (tag == DW_TAG_pointer_type
6076 || tag == DW_TAG_reference_type
6077 || tag == DW_TAG_rvalue_reference_type
6078 || tag == DW_TAG_ptr_to_member_type))
6079 || (at->dw_attr == DW_AT_friend
6080 && tag == DW_TAG_friend))
6082 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6084 if (name_attr != NULL)
6086 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6088 if (decl == NULL)
6089 decl = target_die;
6090 CHECKSUM_ULEB128 ('N');
6091 CHECKSUM_ULEB128 (at->dw_attr);
6092 if (decl->die_parent != NULL)
6093 checksum_die_context (decl->die_parent, ctx);
6094 CHECKSUM_ULEB128 ('E');
6095 CHECKSUM_STRING (AT_string (name_attr));
6096 return;
6100 /* For all other references to another DIE, we check to see if the
6101 target DIE has already been visited. If it has, we emit a
6102 backward reference; if not, we descend recursively. */
6103 if (target_die->die_mark > 0)
6105 CHECKSUM_ULEB128 ('R');
6106 CHECKSUM_ULEB128 (at->dw_attr);
6107 CHECKSUM_ULEB128 (target_die->die_mark);
6109 else
6111 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6113 if (decl == NULL)
6114 decl = target_die;
6115 target_die->die_mark = ++(*mark);
6116 CHECKSUM_ULEB128 ('T');
6117 CHECKSUM_ULEB128 (at->dw_attr);
6118 if (decl->die_parent != NULL)
6119 checksum_die_context (decl->die_parent, ctx);
6120 die_checksum_ordered (target_die, ctx, mark);
6122 return;
6125 CHECKSUM_ULEB128 ('A');
6126 CHECKSUM_ULEB128 (at->dw_attr);
6128 switch (AT_class (at))
6130 case dw_val_class_const:
6131 CHECKSUM_ULEB128 (DW_FORM_sdata);
6132 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6133 break;
6135 case dw_val_class_unsigned_const:
6136 CHECKSUM_ULEB128 (DW_FORM_sdata);
6137 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6138 break;
6140 case dw_val_class_const_double:
6141 CHECKSUM_ULEB128 (DW_FORM_block);
6142 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6143 CHECKSUM (at->dw_attr_val.v.val_double);
6144 break;
6146 case dw_val_class_wide_int:
6147 CHECKSUM_ULEB128 (DW_FORM_block);
6148 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6149 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6150 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6151 get_full_len (*at->dw_attr_val.v.val_wide)
6152 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6153 break;
6155 case dw_val_class_vec:
6156 CHECKSUM_ULEB128 (DW_FORM_block);
6157 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6158 * at->dw_attr_val.v.val_vec.elt_size);
6159 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6160 (at->dw_attr_val.v.val_vec.length
6161 * at->dw_attr_val.v.val_vec.elt_size));
6162 break;
6164 case dw_val_class_flag:
6165 CHECKSUM_ULEB128 (DW_FORM_flag);
6166 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6167 break;
6169 case dw_val_class_str:
6170 CHECKSUM_ULEB128 (DW_FORM_string);
6171 CHECKSUM_STRING (AT_string (at));
6172 break;
6174 case dw_val_class_addr:
6175 r = AT_addr (at);
6176 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6177 CHECKSUM_ULEB128 (DW_FORM_string);
6178 CHECKSUM_STRING (XSTR (r, 0));
6179 break;
6181 case dw_val_class_offset:
6182 CHECKSUM_ULEB128 (DW_FORM_sdata);
6183 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6184 break;
6186 case dw_val_class_loc:
6187 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6188 loc_checksum_ordered (loc, ctx);
6189 break;
6191 case dw_val_class_fde_ref:
6192 case dw_val_class_lbl_id:
6193 case dw_val_class_lineptr:
6194 case dw_val_class_macptr:
6195 case dw_val_class_high_pc:
6196 break;
6198 case dw_val_class_file:
6199 CHECKSUM_ULEB128 (DW_FORM_string);
6200 CHECKSUM_STRING (AT_file (at)->filename);
6201 break;
6203 case dw_val_class_data8:
6204 CHECKSUM (at->dw_attr_val.v.val_data8);
6205 break;
6207 default:
6208 break;
6212 struct checksum_attributes
6214 dw_attr_node *at_name;
6215 dw_attr_node *at_type;
6216 dw_attr_node *at_friend;
6217 dw_attr_node *at_accessibility;
6218 dw_attr_node *at_address_class;
6219 dw_attr_node *at_allocated;
6220 dw_attr_node *at_artificial;
6221 dw_attr_node *at_associated;
6222 dw_attr_node *at_binary_scale;
6223 dw_attr_node *at_bit_offset;
6224 dw_attr_node *at_bit_size;
6225 dw_attr_node *at_bit_stride;
6226 dw_attr_node *at_byte_size;
6227 dw_attr_node *at_byte_stride;
6228 dw_attr_node *at_const_value;
6229 dw_attr_node *at_containing_type;
6230 dw_attr_node *at_count;
6231 dw_attr_node *at_data_location;
6232 dw_attr_node *at_data_member_location;
6233 dw_attr_node *at_decimal_scale;
6234 dw_attr_node *at_decimal_sign;
6235 dw_attr_node *at_default_value;
6236 dw_attr_node *at_digit_count;
6237 dw_attr_node *at_discr;
6238 dw_attr_node *at_discr_list;
6239 dw_attr_node *at_discr_value;
6240 dw_attr_node *at_encoding;
6241 dw_attr_node *at_endianity;
6242 dw_attr_node *at_explicit;
6243 dw_attr_node *at_is_optional;
6244 dw_attr_node *at_location;
6245 dw_attr_node *at_lower_bound;
6246 dw_attr_node *at_mutable;
6247 dw_attr_node *at_ordering;
6248 dw_attr_node *at_picture_string;
6249 dw_attr_node *at_prototyped;
6250 dw_attr_node *at_small;
6251 dw_attr_node *at_segment;
6252 dw_attr_node *at_string_length;
6253 dw_attr_node *at_threads_scaled;
6254 dw_attr_node *at_upper_bound;
6255 dw_attr_node *at_use_location;
6256 dw_attr_node *at_use_UTF8;
6257 dw_attr_node *at_variable_parameter;
6258 dw_attr_node *at_virtuality;
6259 dw_attr_node *at_visibility;
6260 dw_attr_node *at_vtable_elem_location;
6263 /* Collect the attributes that we will want to use for the checksum. */
6265 static void
6266 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6268 dw_attr_node *a;
6269 unsigned ix;
6271 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6273 switch (a->dw_attr)
6275 case DW_AT_name:
6276 attrs->at_name = a;
6277 break;
6278 case DW_AT_type:
6279 attrs->at_type = a;
6280 break;
6281 case DW_AT_friend:
6282 attrs->at_friend = a;
6283 break;
6284 case DW_AT_accessibility:
6285 attrs->at_accessibility = a;
6286 break;
6287 case DW_AT_address_class:
6288 attrs->at_address_class = a;
6289 break;
6290 case DW_AT_allocated:
6291 attrs->at_allocated = a;
6292 break;
6293 case DW_AT_artificial:
6294 attrs->at_artificial = a;
6295 break;
6296 case DW_AT_associated:
6297 attrs->at_associated = a;
6298 break;
6299 case DW_AT_binary_scale:
6300 attrs->at_binary_scale = a;
6301 break;
6302 case DW_AT_bit_offset:
6303 attrs->at_bit_offset = a;
6304 break;
6305 case DW_AT_bit_size:
6306 attrs->at_bit_size = a;
6307 break;
6308 case DW_AT_bit_stride:
6309 attrs->at_bit_stride = a;
6310 break;
6311 case DW_AT_byte_size:
6312 attrs->at_byte_size = a;
6313 break;
6314 case DW_AT_byte_stride:
6315 attrs->at_byte_stride = a;
6316 break;
6317 case DW_AT_const_value:
6318 attrs->at_const_value = a;
6319 break;
6320 case DW_AT_containing_type:
6321 attrs->at_containing_type = a;
6322 break;
6323 case DW_AT_count:
6324 attrs->at_count = a;
6325 break;
6326 case DW_AT_data_location:
6327 attrs->at_data_location = a;
6328 break;
6329 case DW_AT_data_member_location:
6330 attrs->at_data_member_location = a;
6331 break;
6332 case DW_AT_decimal_scale:
6333 attrs->at_decimal_scale = a;
6334 break;
6335 case DW_AT_decimal_sign:
6336 attrs->at_decimal_sign = a;
6337 break;
6338 case DW_AT_default_value:
6339 attrs->at_default_value = a;
6340 break;
6341 case DW_AT_digit_count:
6342 attrs->at_digit_count = a;
6343 break;
6344 case DW_AT_discr:
6345 attrs->at_discr = a;
6346 break;
6347 case DW_AT_discr_list:
6348 attrs->at_discr_list = a;
6349 break;
6350 case DW_AT_discr_value:
6351 attrs->at_discr_value = a;
6352 break;
6353 case DW_AT_encoding:
6354 attrs->at_encoding = a;
6355 break;
6356 case DW_AT_endianity:
6357 attrs->at_endianity = a;
6358 break;
6359 case DW_AT_explicit:
6360 attrs->at_explicit = a;
6361 break;
6362 case DW_AT_is_optional:
6363 attrs->at_is_optional = a;
6364 break;
6365 case DW_AT_location:
6366 attrs->at_location = a;
6367 break;
6368 case DW_AT_lower_bound:
6369 attrs->at_lower_bound = a;
6370 break;
6371 case DW_AT_mutable:
6372 attrs->at_mutable = a;
6373 break;
6374 case DW_AT_ordering:
6375 attrs->at_ordering = a;
6376 break;
6377 case DW_AT_picture_string:
6378 attrs->at_picture_string = a;
6379 break;
6380 case DW_AT_prototyped:
6381 attrs->at_prototyped = a;
6382 break;
6383 case DW_AT_small:
6384 attrs->at_small = a;
6385 break;
6386 case DW_AT_segment:
6387 attrs->at_segment = a;
6388 break;
6389 case DW_AT_string_length:
6390 attrs->at_string_length = a;
6391 break;
6392 case DW_AT_threads_scaled:
6393 attrs->at_threads_scaled = a;
6394 break;
6395 case DW_AT_upper_bound:
6396 attrs->at_upper_bound = a;
6397 break;
6398 case DW_AT_use_location:
6399 attrs->at_use_location = a;
6400 break;
6401 case DW_AT_use_UTF8:
6402 attrs->at_use_UTF8 = a;
6403 break;
6404 case DW_AT_variable_parameter:
6405 attrs->at_variable_parameter = a;
6406 break;
6407 case DW_AT_virtuality:
6408 attrs->at_virtuality = a;
6409 break;
6410 case DW_AT_visibility:
6411 attrs->at_visibility = a;
6412 break;
6413 case DW_AT_vtable_elem_location:
6414 attrs->at_vtable_elem_location = a;
6415 break;
6416 default:
6417 break;
6422 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6424 static void
6425 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6427 dw_die_ref c;
6428 dw_die_ref decl;
6429 struct checksum_attributes attrs;
6431 CHECKSUM_ULEB128 ('D');
6432 CHECKSUM_ULEB128 (die->die_tag);
6434 memset (&attrs, 0, sizeof (attrs));
6436 decl = get_AT_ref (die, DW_AT_specification);
6437 if (decl != NULL)
6438 collect_checksum_attributes (&attrs, decl);
6439 collect_checksum_attributes (&attrs, die);
6441 CHECKSUM_ATTR (attrs.at_name);
6442 CHECKSUM_ATTR (attrs.at_accessibility);
6443 CHECKSUM_ATTR (attrs.at_address_class);
6444 CHECKSUM_ATTR (attrs.at_allocated);
6445 CHECKSUM_ATTR (attrs.at_artificial);
6446 CHECKSUM_ATTR (attrs.at_associated);
6447 CHECKSUM_ATTR (attrs.at_binary_scale);
6448 CHECKSUM_ATTR (attrs.at_bit_offset);
6449 CHECKSUM_ATTR (attrs.at_bit_size);
6450 CHECKSUM_ATTR (attrs.at_bit_stride);
6451 CHECKSUM_ATTR (attrs.at_byte_size);
6452 CHECKSUM_ATTR (attrs.at_byte_stride);
6453 CHECKSUM_ATTR (attrs.at_const_value);
6454 CHECKSUM_ATTR (attrs.at_containing_type);
6455 CHECKSUM_ATTR (attrs.at_count);
6456 CHECKSUM_ATTR (attrs.at_data_location);
6457 CHECKSUM_ATTR (attrs.at_data_member_location);
6458 CHECKSUM_ATTR (attrs.at_decimal_scale);
6459 CHECKSUM_ATTR (attrs.at_decimal_sign);
6460 CHECKSUM_ATTR (attrs.at_default_value);
6461 CHECKSUM_ATTR (attrs.at_digit_count);
6462 CHECKSUM_ATTR (attrs.at_discr);
6463 CHECKSUM_ATTR (attrs.at_discr_list);
6464 CHECKSUM_ATTR (attrs.at_discr_value);
6465 CHECKSUM_ATTR (attrs.at_encoding);
6466 CHECKSUM_ATTR (attrs.at_endianity);
6467 CHECKSUM_ATTR (attrs.at_explicit);
6468 CHECKSUM_ATTR (attrs.at_is_optional);
6469 CHECKSUM_ATTR (attrs.at_location);
6470 CHECKSUM_ATTR (attrs.at_lower_bound);
6471 CHECKSUM_ATTR (attrs.at_mutable);
6472 CHECKSUM_ATTR (attrs.at_ordering);
6473 CHECKSUM_ATTR (attrs.at_picture_string);
6474 CHECKSUM_ATTR (attrs.at_prototyped);
6475 CHECKSUM_ATTR (attrs.at_small);
6476 CHECKSUM_ATTR (attrs.at_segment);
6477 CHECKSUM_ATTR (attrs.at_string_length);
6478 CHECKSUM_ATTR (attrs.at_threads_scaled);
6479 CHECKSUM_ATTR (attrs.at_upper_bound);
6480 CHECKSUM_ATTR (attrs.at_use_location);
6481 CHECKSUM_ATTR (attrs.at_use_UTF8);
6482 CHECKSUM_ATTR (attrs.at_variable_parameter);
6483 CHECKSUM_ATTR (attrs.at_virtuality);
6484 CHECKSUM_ATTR (attrs.at_visibility);
6485 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6486 CHECKSUM_ATTR (attrs.at_type);
6487 CHECKSUM_ATTR (attrs.at_friend);
6489 /* Checksum the child DIEs. */
6490 c = die->die_child;
6491 if (c) do {
6492 dw_attr_node *name_attr;
6494 c = c->die_sib;
6495 name_attr = get_AT (c, DW_AT_name);
6496 if (is_template_instantiation (c))
6498 /* Ignore instantiations of member type and function templates. */
6500 else if (name_attr != NULL
6501 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6503 /* Use a shallow checksum for named nested types and member
6504 functions. */
6505 CHECKSUM_ULEB128 ('S');
6506 CHECKSUM_ULEB128 (c->die_tag);
6507 CHECKSUM_STRING (AT_string (name_attr));
6509 else
6511 /* Use a deep checksum for other children. */
6512 /* Mark this DIE so it gets processed when unmarking. */
6513 if (c->die_mark == 0)
6514 c->die_mark = -1;
6515 die_checksum_ordered (c, ctx, mark);
6517 } while (c != die->die_child);
6519 CHECKSUM_ULEB128 (0);
6522 /* Add a type name and tag to a hash. */
6523 static void
6524 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6526 CHECKSUM_ULEB128 (tag);
6527 CHECKSUM_STRING (name);
6530 #undef CHECKSUM
6531 #undef CHECKSUM_STRING
6532 #undef CHECKSUM_ATTR
6533 #undef CHECKSUM_LEB128
6534 #undef CHECKSUM_ULEB128
6536 /* Generate the type signature for DIE. This is computed by generating an
6537 MD5 checksum over the DIE's tag, its relevant attributes, and its
6538 children. Attributes that are references to other DIEs are processed
6539 by recursion, using the MARK field to prevent infinite recursion.
6540 If the DIE is nested inside a namespace or another type, we also
6541 need to include that context in the signature. The lower 64 bits
6542 of the resulting MD5 checksum comprise the signature. */
6544 static void
6545 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6547 int mark;
6548 const char *name;
6549 unsigned char checksum[16];
6550 struct md5_ctx ctx;
6551 dw_die_ref decl;
6552 dw_die_ref parent;
6554 name = get_AT_string (die, DW_AT_name);
6555 decl = get_AT_ref (die, DW_AT_specification);
6556 parent = get_die_parent (die);
6558 /* First, compute a signature for just the type name (and its surrounding
6559 context, if any. This is stored in the type unit DIE for link-time
6560 ODR (one-definition rule) checking. */
6562 if (is_cxx () && name != NULL)
6564 md5_init_ctx (&ctx);
6566 /* Checksum the names of surrounding namespaces and structures. */
6567 if (parent != NULL)
6568 checksum_die_context (parent, &ctx);
6570 /* Checksum the current DIE. */
6571 die_odr_checksum (die->die_tag, name, &ctx);
6572 md5_finish_ctx (&ctx, checksum);
6574 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6577 /* Next, compute the complete type signature. */
6579 md5_init_ctx (&ctx);
6580 mark = 1;
6581 die->die_mark = mark;
6583 /* Checksum the names of surrounding namespaces and structures. */
6584 if (parent != NULL)
6585 checksum_die_context (parent, &ctx);
6587 /* Checksum the DIE and its children. */
6588 die_checksum_ordered (die, &ctx, &mark);
6589 unmark_all_dies (die);
6590 md5_finish_ctx (&ctx, checksum);
6592 /* Store the signature in the type node and link the type DIE and the
6593 type node together. */
6594 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6595 DWARF_TYPE_SIGNATURE_SIZE);
6596 die->comdat_type_p = true;
6597 die->die_id.die_type_node = type_node;
6598 type_node->type_die = die;
6600 /* If the DIE is a specification, link its declaration to the type node
6601 as well. */
6602 if (decl != NULL)
6604 decl->comdat_type_p = true;
6605 decl->die_id.die_type_node = type_node;
6609 /* Do the location expressions look same? */
6610 static inline int
6611 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6613 return loc1->dw_loc_opc == loc2->dw_loc_opc
6614 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6615 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6618 /* Do the values look the same? */
6619 static int
6620 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6622 dw_loc_descr_ref loc1, loc2;
6623 rtx r1, r2;
6625 if (v1->val_class != v2->val_class)
6626 return 0;
6628 switch (v1->val_class)
6630 case dw_val_class_const:
6631 return v1->v.val_int == v2->v.val_int;
6632 case dw_val_class_unsigned_const:
6633 return v1->v.val_unsigned == v2->v.val_unsigned;
6634 case dw_val_class_const_double:
6635 return v1->v.val_double.high == v2->v.val_double.high
6636 && v1->v.val_double.low == v2->v.val_double.low;
6637 case dw_val_class_wide_int:
6638 return *v1->v.val_wide == *v2->v.val_wide;
6639 case dw_val_class_vec:
6640 if (v1->v.val_vec.length != v2->v.val_vec.length
6641 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6642 return 0;
6643 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6644 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6645 return 0;
6646 return 1;
6647 case dw_val_class_flag:
6648 return v1->v.val_flag == v2->v.val_flag;
6649 case dw_val_class_str:
6650 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6652 case dw_val_class_addr:
6653 r1 = v1->v.val_addr;
6654 r2 = v2->v.val_addr;
6655 if (GET_CODE (r1) != GET_CODE (r2))
6656 return 0;
6657 return !rtx_equal_p (r1, r2);
6659 case dw_val_class_offset:
6660 return v1->v.val_offset == v2->v.val_offset;
6662 case dw_val_class_loc:
6663 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6664 loc1 && loc2;
6665 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6666 if (!same_loc_p (loc1, loc2, mark))
6667 return 0;
6668 return !loc1 && !loc2;
6670 case dw_val_class_die_ref:
6671 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6673 case dw_val_class_fde_ref:
6674 case dw_val_class_vms_delta:
6675 case dw_val_class_lbl_id:
6676 case dw_val_class_lineptr:
6677 case dw_val_class_macptr:
6678 case dw_val_class_high_pc:
6679 return 1;
6681 case dw_val_class_file:
6682 return v1->v.val_file == v2->v.val_file;
6684 case dw_val_class_data8:
6685 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6687 default:
6688 return 1;
6692 /* Do the attributes look the same? */
6694 static int
6695 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6697 if (at1->dw_attr != at2->dw_attr)
6698 return 0;
6700 /* We don't care that this was compiled with a different compiler
6701 snapshot; if the output is the same, that's what matters. */
6702 if (at1->dw_attr == DW_AT_producer)
6703 return 1;
6705 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6708 /* Do the dies look the same? */
6710 static int
6711 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6713 dw_die_ref c1, c2;
6714 dw_attr_node *a1;
6715 unsigned ix;
6717 /* To avoid infinite recursion. */
6718 if (die1->die_mark)
6719 return die1->die_mark == die2->die_mark;
6720 die1->die_mark = die2->die_mark = ++(*mark);
6722 if (die1->die_tag != die2->die_tag)
6723 return 0;
6725 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6726 return 0;
6728 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6729 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6730 return 0;
6732 c1 = die1->die_child;
6733 c2 = die2->die_child;
6734 if (! c1)
6736 if (c2)
6737 return 0;
6739 else
6740 for (;;)
6742 if (!same_die_p (c1, c2, mark))
6743 return 0;
6744 c1 = c1->die_sib;
6745 c2 = c2->die_sib;
6746 if (c1 == die1->die_child)
6748 if (c2 == die2->die_child)
6749 break;
6750 else
6751 return 0;
6755 return 1;
6758 /* Do the dies look the same? Wrapper around same_die_p. */
6760 static int
6761 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6763 int mark = 0;
6764 int ret = same_die_p (die1, die2, &mark);
6766 unmark_all_dies (die1);
6767 unmark_all_dies (die2);
6769 return ret;
6772 /* The prefix to attach to symbols on DIEs in the current comdat debug
6773 info section. */
6774 static const char *comdat_symbol_id;
6776 /* The index of the current symbol within the current comdat CU. */
6777 static unsigned int comdat_symbol_number;
6779 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6780 children, and set comdat_symbol_id accordingly. */
6782 static void
6783 compute_section_prefix (dw_die_ref unit_die)
6785 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6786 const char *base = die_name ? lbasename (die_name) : "anonymous";
6787 char *name = XALLOCAVEC (char, strlen (base) + 64);
6788 char *p;
6789 int i, mark;
6790 unsigned char checksum[16];
6791 struct md5_ctx ctx;
6793 /* Compute the checksum of the DIE, then append part of it as hex digits to
6794 the name filename of the unit. */
6796 md5_init_ctx (&ctx);
6797 mark = 0;
6798 die_checksum (unit_die, &ctx, &mark);
6799 unmark_all_dies (unit_die);
6800 md5_finish_ctx (&ctx, checksum);
6802 sprintf (name, "%s.", base);
6803 clean_symbol_name (name);
6805 p = name + strlen (name);
6806 for (i = 0; i < 4; i++)
6808 sprintf (p, "%.2x", checksum[i]);
6809 p += 2;
6812 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6813 comdat_symbol_number = 0;
6816 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6818 static int
6819 is_type_die (dw_die_ref die)
6821 switch (die->die_tag)
6823 case DW_TAG_array_type:
6824 case DW_TAG_class_type:
6825 case DW_TAG_interface_type:
6826 case DW_TAG_enumeration_type:
6827 case DW_TAG_pointer_type:
6828 case DW_TAG_reference_type:
6829 case DW_TAG_rvalue_reference_type:
6830 case DW_TAG_string_type:
6831 case DW_TAG_structure_type:
6832 case DW_TAG_subroutine_type:
6833 case DW_TAG_union_type:
6834 case DW_TAG_ptr_to_member_type:
6835 case DW_TAG_set_type:
6836 case DW_TAG_subrange_type:
6837 case DW_TAG_base_type:
6838 case DW_TAG_const_type:
6839 case DW_TAG_file_type:
6840 case DW_TAG_packed_type:
6841 case DW_TAG_volatile_type:
6842 case DW_TAG_typedef:
6843 return 1;
6844 default:
6845 return 0;
6849 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6850 Basically, we want to choose the bits that are likely to be shared between
6851 compilations (types) and leave out the bits that are specific to individual
6852 compilations (functions). */
6854 static int
6855 is_comdat_die (dw_die_ref c)
6857 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6858 we do for stabs. The advantage is a greater likelihood of sharing between
6859 objects that don't include headers in the same order (and therefore would
6860 put the base types in a different comdat). jason 8/28/00 */
6862 if (c->die_tag == DW_TAG_base_type)
6863 return 0;
6865 if (c->die_tag == DW_TAG_pointer_type
6866 || c->die_tag == DW_TAG_reference_type
6867 || c->die_tag == DW_TAG_rvalue_reference_type
6868 || c->die_tag == DW_TAG_const_type
6869 || c->die_tag == DW_TAG_volatile_type)
6871 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6873 return t ? is_comdat_die (t) : 0;
6876 return is_type_die (c);
6879 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6880 compilation unit. */
6882 static int
6883 is_symbol_die (dw_die_ref c)
6885 return (is_type_die (c)
6886 || is_declaration_die (c)
6887 || c->die_tag == DW_TAG_namespace
6888 || c->die_tag == DW_TAG_module);
6891 /* Returns true iff C is a compile-unit DIE. */
6893 static inline bool
6894 is_cu_die (dw_die_ref c)
6896 return c && c->die_tag == DW_TAG_compile_unit;
6899 /* Returns true iff C is a unit DIE of some sort. */
6901 static inline bool
6902 is_unit_die (dw_die_ref c)
6904 return c && (c->die_tag == DW_TAG_compile_unit
6905 || c->die_tag == DW_TAG_partial_unit
6906 || c->die_tag == DW_TAG_type_unit);
6909 /* Returns true iff C is a namespace DIE. */
6911 static inline bool
6912 is_namespace_die (dw_die_ref c)
6914 return c && c->die_tag == DW_TAG_namespace;
6917 /* Returns true iff C is a class or structure DIE. */
6919 static inline bool
6920 is_class_die (dw_die_ref c)
6922 return c && (c->die_tag == DW_TAG_class_type
6923 || c->die_tag == DW_TAG_structure_type);
6926 /* Return non-zero if this DIE is a template parameter. */
6928 static inline bool
6929 is_template_parameter (dw_die_ref die)
6931 switch (die->die_tag)
6933 case DW_TAG_template_type_param:
6934 case DW_TAG_template_value_param:
6935 case DW_TAG_GNU_template_template_param:
6936 case DW_TAG_GNU_template_parameter_pack:
6937 return true;
6938 default:
6939 return false;
6943 /* Return non-zero if this DIE represents a template instantiation. */
6945 static inline bool
6946 is_template_instantiation (dw_die_ref die)
6948 dw_die_ref c;
6950 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6951 return false;
6952 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6953 return false;
6956 static char *
6957 gen_internal_sym (const char *prefix)
6959 char buf[256];
6961 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6962 return xstrdup (buf);
6965 /* Assign symbols to all worthy DIEs under DIE. */
6967 static void
6968 assign_symbol_names (dw_die_ref die)
6970 dw_die_ref c;
6972 if (is_symbol_die (die) && !die->comdat_type_p)
6974 if (comdat_symbol_id)
6976 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6978 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6979 comdat_symbol_id, comdat_symbol_number++);
6980 die->die_id.die_symbol = xstrdup (p);
6982 else
6983 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6986 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6989 struct cu_hash_table_entry
6991 dw_die_ref cu;
6992 unsigned min_comdat_num, max_comdat_num;
6993 struct cu_hash_table_entry *next;
6996 /* Helpers to manipulate hash table of CUs. */
6998 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7000 typedef die_struct *compare_type;
7001 static inline hashval_t hash (const cu_hash_table_entry *);
7002 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7003 static inline void remove (cu_hash_table_entry *);
7006 inline hashval_t
7007 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7009 return htab_hash_string (entry->cu->die_id.die_symbol);
7012 inline bool
7013 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7014 const die_struct *entry2)
7016 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7019 inline void
7020 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7022 struct cu_hash_table_entry *next;
7024 while (entry)
7026 next = entry->next;
7027 free (entry);
7028 entry = next;
7032 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7034 /* Check whether we have already seen this CU and set up SYM_NUM
7035 accordingly. */
7036 static int
7037 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7039 struct cu_hash_table_entry dummy;
7040 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7042 dummy.max_comdat_num = 0;
7044 slot = htable->find_slot_with_hash (cu,
7045 htab_hash_string (cu->die_id.die_symbol),
7046 INSERT);
7047 entry = *slot;
7049 for (; entry; last = entry, entry = entry->next)
7051 if (same_die_p_wrap (cu, entry->cu))
7052 break;
7055 if (entry)
7057 *sym_num = entry->min_comdat_num;
7058 return 1;
7061 entry = XCNEW (struct cu_hash_table_entry);
7062 entry->cu = cu;
7063 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7064 entry->next = *slot;
7065 *slot = entry;
7067 return 0;
7070 /* Record SYM_NUM to record of CU in HTABLE. */
7071 static void
7072 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7073 unsigned int sym_num)
7075 struct cu_hash_table_entry **slot, *entry;
7077 slot = htable->find_slot_with_hash (cu,
7078 htab_hash_string (cu->die_id.die_symbol),
7079 NO_INSERT);
7080 entry = *slot;
7082 entry->max_comdat_num = sym_num;
7085 /* Traverse the DIE (which is always comp_unit_die), and set up
7086 additional compilation units for each of the include files we see
7087 bracketed by BINCL/EINCL. */
7089 static void
7090 break_out_includes (dw_die_ref die)
7092 dw_die_ref c;
7093 dw_die_ref unit = NULL;
7094 limbo_die_node *node, **pnode;
7096 c = die->die_child;
7097 if (c) do {
7098 dw_die_ref prev = c;
7099 c = c->die_sib;
7100 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7101 || (unit && is_comdat_die (c)))
7103 dw_die_ref next = c->die_sib;
7105 /* This DIE is for a secondary CU; remove it from the main one. */
7106 remove_child_with_prev (c, prev);
7108 if (c->die_tag == DW_TAG_GNU_BINCL)
7109 unit = push_new_compile_unit (unit, c);
7110 else if (c->die_tag == DW_TAG_GNU_EINCL)
7111 unit = pop_compile_unit (unit);
7112 else
7113 add_child_die (unit, c);
7114 c = next;
7115 if (c == die->die_child)
7116 break;
7118 } while (c != die->die_child);
7120 #if 0
7121 /* We can only use this in debugging, since the frontend doesn't check
7122 to make sure that we leave every include file we enter. */
7123 gcc_assert (!unit);
7124 #endif
7126 assign_symbol_names (die);
7127 cu_hash_type cu_hash_table (10);
7128 for (node = limbo_die_list, pnode = &limbo_die_list;
7129 node;
7130 node = node->next)
7132 int is_dupl;
7134 compute_section_prefix (node->die);
7135 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7136 &comdat_symbol_number);
7137 assign_symbol_names (node->die);
7138 if (is_dupl)
7139 *pnode = node->next;
7140 else
7142 pnode = &node->next;
7143 record_comdat_symbol_number (node->die, &cu_hash_table,
7144 comdat_symbol_number);
7149 /* Return non-zero if this DIE is a declaration. */
7151 static int
7152 is_declaration_die (dw_die_ref die)
7154 dw_attr_node *a;
7155 unsigned ix;
7157 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7158 if (a->dw_attr == DW_AT_declaration)
7159 return 1;
7161 return 0;
7164 /* Return non-zero if this DIE is nested inside a subprogram. */
7166 static int
7167 is_nested_in_subprogram (dw_die_ref die)
7169 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7171 if (decl == NULL)
7172 decl = die;
7173 return local_scope_p (decl);
7176 /* Return non-zero if this DIE contains a defining declaration of a
7177 subprogram. */
7179 static int
7180 contains_subprogram_definition (dw_die_ref die)
7182 dw_die_ref c;
7184 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7185 return 1;
7186 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7187 return 0;
7190 /* Return non-zero if this is a type DIE that should be moved to a
7191 COMDAT .debug_types section. */
7193 static int
7194 should_move_die_to_comdat (dw_die_ref die)
7196 switch (die->die_tag)
7198 case DW_TAG_class_type:
7199 case DW_TAG_structure_type:
7200 case DW_TAG_enumeration_type:
7201 case DW_TAG_union_type:
7202 /* Don't move declarations, inlined instances, types nested in a
7203 subprogram, or types that contain subprogram definitions. */
7204 if (is_declaration_die (die)
7205 || get_AT (die, DW_AT_abstract_origin)
7206 || is_nested_in_subprogram (die)
7207 || contains_subprogram_definition (die))
7208 return 0;
7209 return 1;
7210 case DW_TAG_array_type:
7211 case DW_TAG_interface_type:
7212 case DW_TAG_pointer_type:
7213 case DW_TAG_reference_type:
7214 case DW_TAG_rvalue_reference_type:
7215 case DW_TAG_string_type:
7216 case DW_TAG_subroutine_type:
7217 case DW_TAG_ptr_to_member_type:
7218 case DW_TAG_set_type:
7219 case DW_TAG_subrange_type:
7220 case DW_TAG_base_type:
7221 case DW_TAG_const_type:
7222 case DW_TAG_file_type:
7223 case DW_TAG_packed_type:
7224 case DW_TAG_volatile_type:
7225 case DW_TAG_typedef:
7226 default:
7227 return 0;
7231 /* Make a clone of DIE. */
7233 static dw_die_ref
7234 clone_die (dw_die_ref die)
7236 dw_die_ref clone;
7237 dw_attr_node *a;
7238 unsigned ix;
7240 clone = ggc_cleared_alloc<die_node> ();
7241 clone->die_tag = die->die_tag;
7243 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7244 add_dwarf_attr (clone, a);
7246 return clone;
7249 /* Make a clone of the tree rooted at DIE. */
7251 static dw_die_ref
7252 clone_tree (dw_die_ref die)
7254 dw_die_ref c;
7255 dw_die_ref clone = clone_die (die);
7257 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7259 return clone;
7262 /* Make a clone of DIE as a declaration. */
7264 static dw_die_ref
7265 clone_as_declaration (dw_die_ref die)
7267 dw_die_ref clone;
7268 dw_die_ref decl;
7269 dw_attr_node *a;
7270 unsigned ix;
7272 /* If the DIE is already a declaration, just clone it. */
7273 if (is_declaration_die (die))
7274 return clone_die (die);
7276 /* If the DIE is a specification, just clone its declaration DIE. */
7277 decl = get_AT_ref (die, DW_AT_specification);
7278 if (decl != NULL)
7280 clone = clone_die (decl);
7281 if (die->comdat_type_p)
7282 add_AT_die_ref (clone, DW_AT_signature, die);
7283 return clone;
7286 clone = ggc_cleared_alloc<die_node> ();
7287 clone->die_tag = die->die_tag;
7289 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7291 /* We don't want to copy over all attributes.
7292 For example we don't want DW_AT_byte_size because otherwise we will no
7293 longer have a declaration and GDB will treat it as a definition. */
7295 switch (a->dw_attr)
7297 case DW_AT_abstract_origin:
7298 case DW_AT_artificial:
7299 case DW_AT_containing_type:
7300 case DW_AT_external:
7301 case DW_AT_name:
7302 case DW_AT_type:
7303 case DW_AT_virtuality:
7304 case DW_AT_linkage_name:
7305 case DW_AT_MIPS_linkage_name:
7306 add_dwarf_attr (clone, a);
7307 break;
7308 case DW_AT_byte_size:
7309 default:
7310 break;
7314 if (die->comdat_type_p)
7315 add_AT_die_ref (clone, DW_AT_signature, die);
7317 add_AT_flag (clone, DW_AT_declaration, 1);
7318 return clone;
7322 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7324 struct decl_table_entry
7326 dw_die_ref orig;
7327 dw_die_ref copy;
7330 /* Helpers to manipulate hash table of copied declarations. */
7332 /* Hashtable helpers. */
7334 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7336 typedef die_struct *compare_type;
7337 static inline hashval_t hash (const decl_table_entry *);
7338 static inline bool equal (const decl_table_entry *, const die_struct *);
7341 inline hashval_t
7342 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7344 return htab_hash_pointer (entry->orig);
7347 inline bool
7348 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7349 const die_struct *entry2)
7351 return entry1->orig == entry2;
7354 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7356 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7357 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7358 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7359 to check if the ancestor has already been copied into UNIT. */
7361 static dw_die_ref
7362 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7363 decl_hash_type *decl_table)
7365 dw_die_ref parent = die->die_parent;
7366 dw_die_ref new_parent = unit;
7367 dw_die_ref copy;
7368 decl_table_entry **slot = NULL;
7369 struct decl_table_entry *entry = NULL;
7371 if (decl_table)
7373 /* Check if the entry has already been copied to UNIT. */
7374 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7375 INSERT);
7376 if (*slot != HTAB_EMPTY_ENTRY)
7378 entry = *slot;
7379 return entry->copy;
7382 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7383 entry = XCNEW (struct decl_table_entry);
7384 entry->orig = die;
7385 entry->copy = NULL;
7386 *slot = entry;
7389 if (parent != NULL)
7391 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7392 if (spec != NULL)
7393 parent = spec;
7394 if (!is_unit_die (parent))
7395 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7398 copy = clone_as_declaration (die);
7399 add_child_die (new_parent, copy);
7401 if (decl_table)
7403 /* Record the pointer to the copy. */
7404 entry->copy = copy;
7407 return copy;
7409 /* Copy the declaration context to the new type unit DIE. This includes
7410 any surrounding namespace or type declarations. If the DIE has an
7411 AT_specification attribute, it also includes attributes and children
7412 attached to the specification, and returns a pointer to the original
7413 parent of the declaration DIE. Returns NULL otherwise. */
7415 static dw_die_ref
7416 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7418 dw_die_ref decl;
7419 dw_die_ref new_decl;
7420 dw_die_ref orig_parent = NULL;
7422 decl = get_AT_ref (die, DW_AT_specification);
7423 if (decl == NULL)
7424 decl = die;
7425 else
7427 unsigned ix;
7428 dw_die_ref c;
7429 dw_attr_node *a;
7431 /* The original DIE will be changed to a declaration, and must
7432 be moved to be a child of the original declaration DIE. */
7433 orig_parent = decl->die_parent;
7435 /* Copy the type node pointer from the new DIE to the original
7436 declaration DIE so we can forward references later. */
7437 decl->comdat_type_p = true;
7438 decl->die_id.die_type_node = die->die_id.die_type_node;
7440 remove_AT (die, DW_AT_specification);
7442 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7444 if (a->dw_attr != DW_AT_name
7445 && a->dw_attr != DW_AT_declaration
7446 && a->dw_attr != DW_AT_external)
7447 add_dwarf_attr (die, a);
7450 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7453 if (decl->die_parent != NULL
7454 && !is_unit_die (decl->die_parent))
7456 new_decl = copy_ancestor_tree (unit, decl, NULL);
7457 if (new_decl != NULL)
7459 remove_AT (new_decl, DW_AT_signature);
7460 add_AT_specification (die, new_decl);
7464 return orig_parent;
7467 /* Generate the skeleton ancestor tree for the given NODE, then clone
7468 the DIE and add the clone into the tree. */
7470 static void
7471 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7473 if (node->new_die != NULL)
7474 return;
7476 node->new_die = clone_as_declaration (node->old_die);
7478 if (node->parent != NULL)
7480 generate_skeleton_ancestor_tree (node->parent);
7481 add_child_die (node->parent->new_die, node->new_die);
7485 /* Generate a skeleton tree of DIEs containing any declarations that are
7486 found in the original tree. We traverse the tree looking for declaration
7487 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7489 static void
7490 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7492 skeleton_chain_node node;
7493 dw_die_ref c;
7494 dw_die_ref first;
7495 dw_die_ref prev = NULL;
7496 dw_die_ref next = NULL;
7498 node.parent = parent;
7500 first = c = parent->old_die->die_child;
7501 if (c)
7502 next = c->die_sib;
7503 if (c) do {
7504 if (prev == NULL || prev->die_sib == c)
7505 prev = c;
7506 c = next;
7507 next = (c == first ? NULL : c->die_sib);
7508 node.old_die = c;
7509 node.new_die = NULL;
7510 if (is_declaration_die (c))
7512 if (is_template_instantiation (c))
7514 /* Instantiated templates do not need to be cloned into the
7515 type unit. Just move the DIE and its children back to
7516 the skeleton tree (in the main CU). */
7517 remove_child_with_prev (c, prev);
7518 add_child_die (parent->new_die, c);
7519 c = prev;
7521 else
7523 /* Clone the existing DIE, move the original to the skeleton
7524 tree (which is in the main CU), and put the clone, with
7525 all the original's children, where the original came from
7526 (which is about to be moved to the type unit). */
7527 dw_die_ref clone = clone_die (c);
7528 move_all_children (c, clone);
7530 /* If the original has a DW_AT_object_pointer attribute,
7531 it would now point to a child DIE just moved to the
7532 cloned tree, so we need to remove that attribute from
7533 the original. */
7534 remove_AT (c, DW_AT_object_pointer);
7536 replace_child (c, clone, prev);
7537 generate_skeleton_ancestor_tree (parent);
7538 add_child_die (parent->new_die, c);
7539 node.new_die = c;
7540 c = clone;
7543 generate_skeleton_bottom_up (&node);
7544 } while (next != NULL);
7547 /* Wrapper function for generate_skeleton_bottom_up. */
7549 static dw_die_ref
7550 generate_skeleton (dw_die_ref die)
7552 skeleton_chain_node node;
7554 node.old_die = die;
7555 node.new_die = NULL;
7556 node.parent = NULL;
7558 /* If this type definition is nested inside another type,
7559 and is not an instantiation of a template, always leave
7560 at least a declaration in its place. */
7561 if (die->die_parent != NULL
7562 && is_type_die (die->die_parent)
7563 && !is_template_instantiation (die))
7564 node.new_die = clone_as_declaration (die);
7566 generate_skeleton_bottom_up (&node);
7567 return node.new_die;
7570 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7571 declaration. The original DIE is moved to a new compile unit so that
7572 existing references to it follow it to the new location. If any of the
7573 original DIE's descendants is a declaration, we need to replace the
7574 original DIE with a skeleton tree and move the declarations back into the
7575 skeleton tree. */
7577 static dw_die_ref
7578 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7579 dw_die_ref prev)
7581 dw_die_ref skeleton, orig_parent;
7583 /* Copy the declaration context to the type unit DIE. If the returned
7584 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7585 that DIE. */
7586 orig_parent = copy_declaration_context (unit, child);
7588 skeleton = generate_skeleton (child);
7589 if (skeleton == NULL)
7590 remove_child_with_prev (child, prev);
7591 else
7593 skeleton->comdat_type_p = true;
7594 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7596 /* If the original DIE was a specification, we need to put
7597 the skeleton under the parent DIE of the declaration.
7598 This leaves the original declaration in the tree, but
7599 it will be pruned later since there are no longer any
7600 references to it. */
7601 if (orig_parent != NULL)
7603 remove_child_with_prev (child, prev);
7604 add_child_die (orig_parent, skeleton);
7606 else
7607 replace_child (child, skeleton, prev);
7610 return skeleton;
7613 /* Traverse the DIE and set up additional .debug_types sections for each
7614 type worthy of being placed in a COMDAT section. */
7616 static void
7617 break_out_comdat_types (dw_die_ref die)
7619 dw_die_ref c;
7620 dw_die_ref first;
7621 dw_die_ref prev = NULL;
7622 dw_die_ref next = NULL;
7623 dw_die_ref unit = NULL;
7625 first = c = die->die_child;
7626 if (c)
7627 next = c->die_sib;
7628 if (c) do {
7629 if (prev == NULL || prev->die_sib == c)
7630 prev = c;
7631 c = next;
7632 next = (c == first ? NULL : c->die_sib);
7633 if (should_move_die_to_comdat (c))
7635 dw_die_ref replacement;
7636 comdat_type_node *type_node;
7638 /* Break out nested types into their own type units. */
7639 break_out_comdat_types (c);
7641 /* Create a new type unit DIE as the root for the new tree, and
7642 add it to the list of comdat types. */
7643 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7644 add_AT_unsigned (unit, DW_AT_language,
7645 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7646 type_node = ggc_cleared_alloc<comdat_type_node> ();
7647 type_node->root_die = unit;
7648 type_node->next = comdat_type_list;
7649 comdat_type_list = type_node;
7651 /* Generate the type signature. */
7652 generate_type_signature (c, type_node);
7654 /* Copy the declaration context, attributes, and children of the
7655 declaration into the new type unit DIE, then remove this DIE
7656 from the main CU (or replace it with a skeleton if necessary). */
7657 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7658 type_node->skeleton_die = replacement;
7660 /* Add the DIE to the new compunit. */
7661 add_child_die (unit, c);
7663 if (replacement != NULL)
7664 c = replacement;
7666 else if (c->die_tag == DW_TAG_namespace
7667 || c->die_tag == DW_TAG_class_type
7668 || c->die_tag == DW_TAG_structure_type
7669 || c->die_tag == DW_TAG_union_type)
7671 /* Look for nested types that can be broken out. */
7672 break_out_comdat_types (c);
7674 } while (next != NULL);
7677 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7678 Enter all the cloned children into the hash table decl_table. */
7680 static dw_die_ref
7681 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7683 dw_die_ref c;
7684 dw_die_ref clone;
7685 struct decl_table_entry *entry;
7686 decl_table_entry **slot;
7688 if (die->die_tag == DW_TAG_subprogram)
7689 clone = clone_as_declaration (die);
7690 else
7691 clone = clone_die (die);
7693 slot = decl_table->find_slot_with_hash (die,
7694 htab_hash_pointer (die), INSERT);
7696 /* Assert that DIE isn't in the hash table yet. If it would be there
7697 before, the ancestors would be necessarily there as well, therefore
7698 clone_tree_partial wouldn't be called. */
7699 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7701 entry = XCNEW (struct decl_table_entry);
7702 entry->orig = die;
7703 entry->copy = clone;
7704 *slot = entry;
7706 if (die->die_tag != DW_TAG_subprogram)
7707 FOR_EACH_CHILD (die, c,
7708 add_child_die (clone, clone_tree_partial (c, decl_table)));
7710 return clone;
7713 /* Walk the DIE and its children, looking for references to incomplete
7714 or trivial types that are unmarked (i.e., that are not in the current
7715 type_unit). */
7717 static void
7718 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7720 dw_die_ref c;
7721 dw_attr_node *a;
7722 unsigned ix;
7724 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7726 if (AT_class (a) == dw_val_class_die_ref)
7728 dw_die_ref targ = AT_ref (a);
7729 decl_table_entry **slot;
7730 struct decl_table_entry *entry;
7732 if (targ->die_mark != 0 || targ->comdat_type_p)
7733 continue;
7735 slot = decl_table->find_slot_with_hash (targ,
7736 htab_hash_pointer (targ),
7737 INSERT);
7739 if (*slot != HTAB_EMPTY_ENTRY)
7741 /* TARG has already been copied, so we just need to
7742 modify the reference to point to the copy. */
7743 entry = *slot;
7744 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7746 else
7748 dw_die_ref parent = unit;
7749 dw_die_ref copy = clone_die (targ);
7751 /* Record in DECL_TABLE that TARG has been copied.
7752 Need to do this now, before the recursive call,
7753 because DECL_TABLE may be expanded and SLOT
7754 would no longer be a valid pointer. */
7755 entry = XCNEW (struct decl_table_entry);
7756 entry->orig = targ;
7757 entry->copy = copy;
7758 *slot = entry;
7760 /* If TARG is not a declaration DIE, we need to copy its
7761 children. */
7762 if (!is_declaration_die (targ))
7764 FOR_EACH_CHILD (
7765 targ, c,
7766 add_child_die (copy,
7767 clone_tree_partial (c, decl_table)));
7770 /* Make sure the cloned tree is marked as part of the
7771 type unit. */
7772 mark_dies (copy);
7774 /* If TARG has surrounding context, copy its ancestor tree
7775 into the new type unit. */
7776 if (targ->die_parent != NULL
7777 && !is_unit_die (targ->die_parent))
7778 parent = copy_ancestor_tree (unit, targ->die_parent,
7779 decl_table);
7781 add_child_die (parent, copy);
7782 a->dw_attr_val.v.val_die_ref.die = copy;
7784 /* Make sure the newly-copied DIE is walked. If it was
7785 installed in a previously-added context, it won't
7786 get visited otherwise. */
7787 if (parent != unit)
7789 /* Find the highest point of the newly-added tree,
7790 mark each node along the way, and walk from there. */
7791 parent->die_mark = 1;
7792 while (parent->die_parent
7793 && parent->die_parent->die_mark == 0)
7795 parent = parent->die_parent;
7796 parent->die_mark = 1;
7798 copy_decls_walk (unit, parent, decl_table);
7804 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7807 /* Copy declarations for "unworthy" types into the new comdat section.
7808 Incomplete types, modified types, and certain other types aren't broken
7809 out into comdat sections of their own, so they don't have a signature,
7810 and we need to copy the declaration into the same section so that we
7811 don't have an external reference. */
7813 static void
7814 copy_decls_for_unworthy_types (dw_die_ref unit)
7816 mark_dies (unit);
7817 decl_hash_type decl_table (10);
7818 copy_decls_walk (unit, unit, &decl_table);
7819 unmark_dies (unit);
7822 /* Traverse the DIE and add a sibling attribute if it may have the
7823 effect of speeding up access to siblings. To save some space,
7824 avoid generating sibling attributes for DIE's without children. */
7826 static void
7827 add_sibling_attributes (dw_die_ref die)
7829 dw_die_ref c;
7831 if (! die->die_child)
7832 return;
7834 if (die->die_parent && die != die->die_parent->die_child)
7835 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7837 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7840 /* Output all location lists for the DIE and its children. */
7842 static void
7843 output_location_lists (dw_die_ref die)
7845 dw_die_ref c;
7846 dw_attr_node *a;
7847 unsigned ix;
7849 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7850 if (AT_class (a) == dw_val_class_loc_list)
7851 output_loc_list (AT_loc_list (a));
7853 FOR_EACH_CHILD (die, c, output_location_lists (c));
7856 /* We want to limit the number of external references, because they are
7857 larger than local references: a relocation takes multiple words, and
7858 even a sig8 reference is always eight bytes, whereas a local reference
7859 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7860 So if we encounter multiple external references to the same type DIE, we
7861 make a local typedef stub for it and redirect all references there.
7863 This is the element of the hash table for keeping track of these
7864 references. */
7866 struct external_ref
7868 dw_die_ref type;
7869 dw_die_ref stub;
7870 unsigned n_refs;
7873 /* Hashtable helpers. */
7875 struct external_ref_hasher : free_ptr_hash <external_ref>
7877 static inline hashval_t hash (const external_ref *);
7878 static inline bool equal (const external_ref *, const external_ref *);
7881 inline hashval_t
7882 external_ref_hasher::hash (const external_ref *r)
7884 dw_die_ref die = r->type;
7885 hashval_t h = 0;
7887 /* We can't use the address of the DIE for hashing, because
7888 that will make the order of the stub DIEs non-deterministic. */
7889 if (! die->comdat_type_p)
7890 /* We have a symbol; use it to compute a hash. */
7891 h = htab_hash_string (die->die_id.die_symbol);
7892 else
7894 /* We have a type signature; use a subset of the bits as the hash.
7895 The 8-byte signature is at least as large as hashval_t. */
7896 comdat_type_node *type_node = die->die_id.die_type_node;
7897 memcpy (&h, type_node->signature, sizeof (h));
7899 return h;
7902 inline bool
7903 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7905 return r1->type == r2->type;
7908 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7910 /* Return a pointer to the external_ref for references to DIE. */
7912 static struct external_ref *
7913 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7915 struct external_ref ref, *ref_p;
7916 external_ref **slot;
7918 ref.type = die;
7919 slot = map->find_slot (&ref, INSERT);
7920 if (*slot != HTAB_EMPTY_ENTRY)
7921 return *slot;
7923 ref_p = XCNEW (struct external_ref);
7924 ref_p->type = die;
7925 *slot = ref_p;
7926 return ref_p;
7929 /* Subroutine of optimize_external_refs, below.
7931 If we see a type skeleton, record it as our stub. If we see external
7932 references, remember how many we've seen. */
7934 static void
7935 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7937 dw_die_ref c;
7938 dw_attr_node *a;
7939 unsigned ix;
7940 struct external_ref *ref_p;
7942 if (is_type_die (die)
7943 && (c = get_AT_ref (die, DW_AT_signature)))
7945 /* This is a local skeleton; use it for local references. */
7946 ref_p = lookup_external_ref (map, c);
7947 ref_p->stub = die;
7950 /* Scan the DIE references, and remember any that refer to DIEs from
7951 other CUs (i.e. those which are not marked). */
7952 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7953 if (AT_class (a) == dw_val_class_die_ref
7954 && (c = AT_ref (a))->die_mark == 0
7955 && is_type_die (c))
7957 ref_p = lookup_external_ref (map, c);
7958 ref_p->n_refs++;
7961 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7964 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7965 points to an external_ref, DATA is the CU we're processing. If we don't
7966 already have a local stub, and we have multiple refs, build a stub. */
7969 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7971 struct external_ref *ref_p = *slot;
7973 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7975 /* We have multiple references to this type, so build a small stub.
7976 Both of these forms are a bit dodgy from the perspective of the
7977 DWARF standard, since technically they should have names. */
7978 dw_die_ref cu = data;
7979 dw_die_ref type = ref_p->type;
7980 dw_die_ref stub = NULL;
7982 if (type->comdat_type_p)
7984 /* If we refer to this type via sig8, use AT_signature. */
7985 stub = new_die (type->die_tag, cu, NULL_TREE);
7986 add_AT_die_ref (stub, DW_AT_signature, type);
7988 else
7990 /* Otherwise, use a typedef with no name. */
7991 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7992 add_AT_die_ref (stub, DW_AT_type, type);
7995 stub->die_mark++;
7996 ref_p->stub = stub;
7998 return 1;
8001 /* DIE is a unit; look through all the DIE references to see if there are
8002 any external references to types, and if so, create local stubs for
8003 them which will be applied in build_abbrev_table. This is useful because
8004 references to local DIEs are smaller. */
8006 static external_ref_hash_type *
8007 optimize_external_refs (dw_die_ref die)
8009 external_ref_hash_type *map = new external_ref_hash_type (10);
8010 optimize_external_refs_1 (die, map);
8011 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8012 return map;
8015 /* The format of each DIE (and its attribute value pairs) is encoded in an
8016 abbreviation table. This routine builds the abbreviation table and assigns
8017 a unique abbreviation id for each abbreviation entry. The children of each
8018 die are visited recursively. */
8020 static void
8021 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8023 unsigned long abbrev_id;
8024 unsigned int n_alloc;
8025 dw_die_ref c;
8026 dw_attr_node *a;
8027 unsigned ix;
8029 /* Scan the DIE references, and replace any that refer to
8030 DIEs from other CUs (i.e. those which are not marked) with
8031 the local stubs we built in optimize_external_refs. */
8032 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8033 if (AT_class (a) == dw_val_class_die_ref
8034 && (c = AT_ref (a))->die_mark == 0)
8036 struct external_ref *ref_p;
8037 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8039 ref_p = lookup_external_ref (extern_map, c);
8040 if (ref_p->stub && ref_p->stub != die)
8041 change_AT_die_ref (a, ref_p->stub);
8042 else
8043 /* We aren't changing this reference, so mark it external. */
8044 set_AT_ref_external (a, 1);
8047 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8049 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8050 dw_attr_node *die_a, *abbrev_a;
8051 unsigned ix;
8052 bool ok = true;
8054 if (abbrev->die_tag != die->die_tag)
8055 continue;
8056 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8057 continue;
8059 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8060 continue;
8062 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8064 abbrev_a = &(*abbrev->die_attr)[ix];
8065 if ((abbrev_a->dw_attr != die_a->dw_attr)
8066 || (value_format (abbrev_a) != value_format (die_a)))
8068 ok = false;
8069 break;
8072 if (ok)
8073 break;
8076 if (abbrev_id >= abbrev_die_table_in_use)
8078 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8080 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8081 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8082 n_alloc);
8084 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8085 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8086 abbrev_die_table_allocated = n_alloc;
8089 ++abbrev_die_table_in_use;
8090 abbrev_die_table[abbrev_id] = die;
8093 die->die_abbrev = abbrev_id;
8094 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8097 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8099 static int
8100 constant_size (unsigned HOST_WIDE_INT value)
8102 int log;
8104 if (value == 0)
8105 log = 0;
8106 else
8107 log = floor_log2 (value);
8109 log = log / 8;
8110 log = 1 << (floor_log2 (log) + 1);
8112 return log;
8115 /* Return the size of a DIE as it is represented in the
8116 .debug_info section. */
8118 static unsigned long
8119 size_of_die (dw_die_ref die)
8121 unsigned long size = 0;
8122 dw_attr_node *a;
8123 unsigned ix;
8124 enum dwarf_form form;
8126 size += size_of_uleb128 (die->die_abbrev);
8127 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8129 switch (AT_class (a))
8131 case dw_val_class_addr:
8132 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8134 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8135 size += size_of_uleb128 (AT_index (a));
8137 else
8138 size += DWARF2_ADDR_SIZE;
8139 break;
8140 case dw_val_class_offset:
8141 size += DWARF_OFFSET_SIZE;
8142 break;
8143 case dw_val_class_loc:
8145 unsigned long lsize = size_of_locs (AT_loc (a));
8147 /* Block length. */
8148 if (dwarf_version >= 4)
8149 size += size_of_uleb128 (lsize);
8150 else
8151 size += constant_size (lsize);
8152 size += lsize;
8154 break;
8155 case dw_val_class_loc_list:
8156 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8158 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8159 size += size_of_uleb128 (AT_index (a));
8161 else
8162 size += DWARF_OFFSET_SIZE;
8163 break;
8164 case dw_val_class_range_list:
8165 size += DWARF_OFFSET_SIZE;
8166 break;
8167 case dw_val_class_const:
8168 size += size_of_sleb128 (AT_int (a));
8169 break;
8170 case dw_val_class_unsigned_const:
8172 int csize = constant_size (AT_unsigned (a));
8173 if (dwarf_version == 3
8174 && a->dw_attr == DW_AT_data_member_location
8175 && csize >= 4)
8176 size += size_of_uleb128 (AT_unsigned (a));
8177 else
8178 size += csize;
8180 break;
8181 case dw_val_class_const_double:
8182 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8183 if (HOST_BITS_PER_WIDE_INT >= 64)
8184 size++; /* block */
8185 break;
8186 case dw_val_class_wide_int:
8187 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8188 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8189 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8190 > 64)
8191 size++; /* block */
8192 break;
8193 case dw_val_class_vec:
8194 size += constant_size (a->dw_attr_val.v.val_vec.length
8195 * a->dw_attr_val.v.val_vec.elt_size)
8196 + a->dw_attr_val.v.val_vec.length
8197 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8198 break;
8199 case dw_val_class_flag:
8200 if (dwarf_version >= 4)
8201 /* Currently all add_AT_flag calls pass in 1 as last argument,
8202 so DW_FORM_flag_present can be used. If that ever changes,
8203 we'll need to use DW_FORM_flag and have some optimization
8204 in build_abbrev_table that will change those to
8205 DW_FORM_flag_present if it is set to 1 in all DIEs using
8206 the same abbrev entry. */
8207 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8208 else
8209 size += 1;
8210 break;
8211 case dw_val_class_die_ref:
8212 if (AT_ref_external (a))
8214 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8215 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8216 is sized by target address length, whereas in DWARF3
8217 it's always sized as an offset. */
8218 if (use_debug_types)
8219 size += DWARF_TYPE_SIGNATURE_SIZE;
8220 else if (dwarf_version == 2)
8221 size += DWARF2_ADDR_SIZE;
8222 else
8223 size += DWARF_OFFSET_SIZE;
8225 else
8226 size += DWARF_OFFSET_SIZE;
8227 break;
8228 case dw_val_class_fde_ref:
8229 size += DWARF_OFFSET_SIZE;
8230 break;
8231 case dw_val_class_lbl_id:
8232 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8234 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8235 size += size_of_uleb128 (AT_index (a));
8237 else
8238 size += DWARF2_ADDR_SIZE;
8239 break;
8240 case dw_val_class_lineptr:
8241 case dw_val_class_macptr:
8242 size += DWARF_OFFSET_SIZE;
8243 break;
8244 case dw_val_class_str:
8245 form = AT_string_form (a);
8246 if (form == DW_FORM_strp)
8247 size += DWARF_OFFSET_SIZE;
8248 else if (form == DW_FORM_GNU_str_index)
8249 size += size_of_uleb128 (AT_index (a));
8250 else
8251 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8252 break;
8253 case dw_val_class_file:
8254 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8255 break;
8256 case dw_val_class_data8:
8257 size += 8;
8258 break;
8259 case dw_val_class_vms_delta:
8260 size += DWARF_OFFSET_SIZE;
8261 break;
8262 case dw_val_class_high_pc:
8263 size += DWARF2_ADDR_SIZE;
8264 break;
8265 default:
8266 gcc_unreachable ();
8270 return size;
8273 /* Size the debugging information associated with a given DIE. Visits the
8274 DIE's children recursively. Updates the global variable next_die_offset, on
8275 each time through. Uses the current value of next_die_offset to update the
8276 die_offset field in each DIE. */
8278 static void
8279 calc_die_sizes (dw_die_ref die)
8281 dw_die_ref c;
8283 gcc_assert (die->die_offset == 0
8284 || (unsigned long int) die->die_offset == next_die_offset);
8285 die->die_offset = next_die_offset;
8286 next_die_offset += size_of_die (die);
8288 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8290 if (die->die_child != NULL)
8291 /* Count the null byte used to terminate sibling lists. */
8292 next_die_offset += 1;
8295 /* Size just the base type children at the start of the CU.
8296 This is needed because build_abbrev needs to size locs
8297 and sizing of type based stack ops needs to know die_offset
8298 values for the base types. */
8300 static void
8301 calc_base_type_die_sizes (void)
8303 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8304 unsigned int i;
8305 dw_die_ref base_type;
8306 #if ENABLE_ASSERT_CHECKING
8307 dw_die_ref prev = comp_unit_die ()->die_child;
8308 #endif
8310 die_offset += size_of_die (comp_unit_die ());
8311 for (i = 0; base_types.iterate (i, &base_type); i++)
8313 #if ENABLE_ASSERT_CHECKING
8314 gcc_assert (base_type->die_offset == 0
8315 && prev->die_sib == base_type
8316 && base_type->die_child == NULL
8317 && base_type->die_abbrev);
8318 prev = base_type;
8319 #endif
8320 base_type->die_offset = die_offset;
8321 die_offset += size_of_die (base_type);
8325 /* Set the marks for a die and its children. We do this so
8326 that we know whether or not a reference needs to use FORM_ref_addr; only
8327 DIEs in the same CU will be marked. We used to clear out the offset
8328 and use that as the flag, but ran into ordering problems. */
8330 static void
8331 mark_dies (dw_die_ref die)
8333 dw_die_ref c;
8335 gcc_assert (!die->die_mark);
8337 die->die_mark = 1;
8338 FOR_EACH_CHILD (die, c, mark_dies (c));
8341 /* Clear the marks for a die and its children. */
8343 static void
8344 unmark_dies (dw_die_ref die)
8346 dw_die_ref c;
8348 if (! use_debug_types)
8349 gcc_assert (die->die_mark);
8351 die->die_mark = 0;
8352 FOR_EACH_CHILD (die, c, unmark_dies (c));
8355 /* Clear the marks for a die, its children and referred dies. */
8357 static void
8358 unmark_all_dies (dw_die_ref die)
8360 dw_die_ref c;
8361 dw_attr_node *a;
8362 unsigned ix;
8364 if (!die->die_mark)
8365 return;
8366 die->die_mark = 0;
8368 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8370 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8371 if (AT_class (a) == dw_val_class_die_ref)
8372 unmark_all_dies (AT_ref (a));
8375 /* Calculate if the entry should appear in the final output file. It may be
8376 from a pruned a type. */
8378 static bool
8379 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8381 /* By limiting gnu pubnames to definitions only, gold can generate a
8382 gdb index without entries for declarations, which don't include
8383 enough information to be useful. */
8384 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8385 return false;
8387 if (table == pubname_table)
8389 /* Enumerator names are part of the pubname table, but the
8390 parent DW_TAG_enumeration_type die may have been pruned.
8391 Don't output them if that is the case. */
8392 if (p->die->die_tag == DW_TAG_enumerator &&
8393 (p->die->die_parent == NULL
8394 || !p->die->die_parent->die_perennial_p))
8395 return false;
8397 /* Everything else in the pubname table is included. */
8398 return true;
8401 /* The pubtypes table shouldn't include types that have been
8402 pruned. */
8403 return (p->die->die_offset != 0
8404 || !flag_eliminate_unused_debug_types);
8407 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8408 generated for the compilation unit. */
8410 static unsigned long
8411 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8413 unsigned long size;
8414 unsigned i;
8415 pubname_entry *p;
8416 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8418 size = DWARF_PUBNAMES_HEADER_SIZE;
8419 FOR_EACH_VEC_ELT (*names, i, p)
8420 if (include_pubname_in_output (names, p))
8421 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8423 size += DWARF_OFFSET_SIZE;
8424 return size;
8427 /* Return the size of the information in the .debug_aranges section. */
8429 static unsigned long
8430 size_of_aranges (void)
8432 unsigned long size;
8434 size = DWARF_ARANGES_HEADER_SIZE;
8436 /* Count the address/length pair for this compilation unit. */
8437 if (text_section_used)
8438 size += 2 * DWARF2_ADDR_SIZE;
8439 if (cold_text_section_used)
8440 size += 2 * DWARF2_ADDR_SIZE;
8441 if (have_multiple_function_sections)
8443 unsigned fde_idx;
8444 dw_fde_ref fde;
8446 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8448 if (DECL_IGNORED_P (fde->decl))
8449 continue;
8450 if (!fde->in_std_section)
8451 size += 2 * DWARF2_ADDR_SIZE;
8452 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8453 size += 2 * DWARF2_ADDR_SIZE;
8457 /* Count the two zero words used to terminated the address range table. */
8458 size += 2 * DWARF2_ADDR_SIZE;
8459 return size;
8462 /* Select the encoding of an attribute value. */
8464 static enum dwarf_form
8465 value_format (dw_attr_node *a)
8467 switch (AT_class (a))
8469 case dw_val_class_addr:
8470 /* Only very few attributes allow DW_FORM_addr. */
8471 switch (a->dw_attr)
8473 case DW_AT_low_pc:
8474 case DW_AT_high_pc:
8475 case DW_AT_entry_pc:
8476 case DW_AT_trampoline:
8477 return (AT_index (a) == NOT_INDEXED
8478 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8479 default:
8480 break;
8482 switch (DWARF2_ADDR_SIZE)
8484 case 1:
8485 return DW_FORM_data1;
8486 case 2:
8487 return DW_FORM_data2;
8488 case 4:
8489 return DW_FORM_data4;
8490 case 8:
8491 return DW_FORM_data8;
8492 default:
8493 gcc_unreachable ();
8495 case dw_val_class_range_list:
8496 case dw_val_class_loc_list:
8497 if (dwarf_version >= 4)
8498 return DW_FORM_sec_offset;
8499 /* FALLTHRU */
8500 case dw_val_class_vms_delta:
8501 case dw_val_class_offset:
8502 switch (DWARF_OFFSET_SIZE)
8504 case 4:
8505 return DW_FORM_data4;
8506 case 8:
8507 return DW_FORM_data8;
8508 default:
8509 gcc_unreachable ();
8511 case dw_val_class_loc:
8512 if (dwarf_version >= 4)
8513 return DW_FORM_exprloc;
8514 switch (constant_size (size_of_locs (AT_loc (a))))
8516 case 1:
8517 return DW_FORM_block1;
8518 case 2:
8519 return DW_FORM_block2;
8520 case 4:
8521 return DW_FORM_block4;
8522 default:
8523 gcc_unreachable ();
8525 case dw_val_class_const:
8526 return DW_FORM_sdata;
8527 case dw_val_class_unsigned_const:
8528 switch (constant_size (AT_unsigned (a)))
8530 case 1:
8531 return DW_FORM_data1;
8532 case 2:
8533 return DW_FORM_data2;
8534 case 4:
8535 /* In DWARF3 DW_AT_data_member_location with
8536 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8537 constant, so we need to use DW_FORM_udata if we need
8538 a large constant. */
8539 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8540 return DW_FORM_udata;
8541 return DW_FORM_data4;
8542 case 8:
8543 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8544 return DW_FORM_udata;
8545 return DW_FORM_data8;
8546 default:
8547 gcc_unreachable ();
8549 case dw_val_class_const_double:
8550 switch (HOST_BITS_PER_WIDE_INT)
8552 case 8:
8553 return DW_FORM_data2;
8554 case 16:
8555 return DW_FORM_data4;
8556 case 32:
8557 return DW_FORM_data8;
8558 case 64:
8559 default:
8560 return DW_FORM_block1;
8562 case dw_val_class_wide_int:
8563 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8565 case 8:
8566 return DW_FORM_data1;
8567 case 16:
8568 return DW_FORM_data2;
8569 case 32:
8570 return DW_FORM_data4;
8571 case 64:
8572 return DW_FORM_data8;
8573 default:
8574 return DW_FORM_block1;
8576 case dw_val_class_vec:
8577 switch (constant_size (a->dw_attr_val.v.val_vec.length
8578 * a->dw_attr_val.v.val_vec.elt_size))
8580 case 1:
8581 return DW_FORM_block1;
8582 case 2:
8583 return DW_FORM_block2;
8584 case 4:
8585 return DW_FORM_block4;
8586 default:
8587 gcc_unreachable ();
8589 case dw_val_class_flag:
8590 if (dwarf_version >= 4)
8592 /* Currently all add_AT_flag calls pass in 1 as last argument,
8593 so DW_FORM_flag_present can be used. If that ever changes,
8594 we'll need to use DW_FORM_flag and have some optimization
8595 in build_abbrev_table that will change those to
8596 DW_FORM_flag_present if it is set to 1 in all DIEs using
8597 the same abbrev entry. */
8598 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8599 return DW_FORM_flag_present;
8601 return DW_FORM_flag;
8602 case dw_val_class_die_ref:
8603 if (AT_ref_external (a))
8604 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8605 else
8606 return DW_FORM_ref;
8607 case dw_val_class_fde_ref:
8608 return DW_FORM_data;
8609 case dw_val_class_lbl_id:
8610 return (AT_index (a) == NOT_INDEXED
8611 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8612 case dw_val_class_lineptr:
8613 case dw_val_class_macptr:
8614 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8615 case dw_val_class_str:
8616 return AT_string_form (a);
8617 case dw_val_class_file:
8618 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8620 case 1:
8621 return DW_FORM_data1;
8622 case 2:
8623 return DW_FORM_data2;
8624 case 4:
8625 return DW_FORM_data4;
8626 default:
8627 gcc_unreachable ();
8630 case dw_val_class_data8:
8631 return DW_FORM_data8;
8633 case dw_val_class_high_pc:
8634 switch (DWARF2_ADDR_SIZE)
8636 case 1:
8637 return DW_FORM_data1;
8638 case 2:
8639 return DW_FORM_data2;
8640 case 4:
8641 return DW_FORM_data4;
8642 case 8:
8643 return DW_FORM_data8;
8644 default:
8645 gcc_unreachable ();
8648 default:
8649 gcc_unreachable ();
8653 /* Output the encoding of an attribute value. */
8655 static void
8656 output_value_format (dw_attr_node *a)
8658 enum dwarf_form form = value_format (a);
8660 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8663 /* Given a die and id, produce the appropriate abbreviations. */
8665 static void
8666 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8668 unsigned ix;
8669 dw_attr_node *a_attr;
8671 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8672 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8673 dwarf_tag_name (abbrev->die_tag));
8675 if (abbrev->die_child != NULL)
8676 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8677 else
8678 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8680 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8682 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8683 dwarf_attr_name (a_attr->dw_attr));
8684 output_value_format (a_attr);
8687 dw2_asm_output_data (1, 0, NULL);
8688 dw2_asm_output_data (1, 0, NULL);
8692 /* Output the .debug_abbrev section which defines the DIE abbreviation
8693 table. */
8695 static void
8696 output_abbrev_section (void)
8698 unsigned long abbrev_id;
8700 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8701 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8703 /* Terminate the table. */
8704 dw2_asm_output_data (1, 0, NULL);
8707 /* Output a symbol we can use to refer to this DIE from another CU. */
8709 static inline void
8710 output_die_symbol (dw_die_ref die)
8712 const char *sym = die->die_id.die_symbol;
8714 gcc_assert (!die->comdat_type_p);
8716 if (sym == 0)
8717 return;
8719 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8720 /* We make these global, not weak; if the target doesn't support
8721 .linkonce, it doesn't support combining the sections, so debugging
8722 will break. */
8723 targetm.asm_out.globalize_label (asm_out_file, sym);
8725 ASM_OUTPUT_LABEL (asm_out_file, sym);
8728 /* Return a new location list, given the begin and end range, and the
8729 expression. */
8731 static inline dw_loc_list_ref
8732 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8733 const char *section)
8735 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8737 retlist->begin = begin;
8738 retlist->begin_entry = NULL;
8739 retlist->end = end;
8740 retlist->expr = expr;
8741 retlist->section = section;
8743 return retlist;
8746 /* Generate a new internal symbol for this location list node, if it
8747 hasn't got one yet. */
8749 static inline void
8750 gen_llsym (dw_loc_list_ref list)
8752 gcc_assert (!list->ll_symbol);
8753 list->ll_symbol = gen_internal_sym ("LLST");
8756 /* Output the location list given to us. */
8758 static void
8759 output_loc_list (dw_loc_list_ref list_head)
8761 dw_loc_list_ref curr = list_head;
8763 if (list_head->emitted)
8764 return;
8765 list_head->emitted = true;
8767 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8769 /* Walk the location list, and output each range + expression. */
8770 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8772 unsigned long size;
8773 /* Don't output an entry that starts and ends at the same address. */
8774 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8775 continue;
8776 size = size_of_locs (curr->expr);
8777 /* If the expression is too large, drop it on the floor. We could
8778 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8779 in the expression, but >= 64KB expressions for a single value
8780 in a single range are unlikely very useful. */
8781 if (size > 0xffff)
8782 continue;
8783 if (dwarf_split_debug_info)
8785 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8786 "Location list start/length entry (%s)",
8787 list_head->ll_symbol);
8788 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8789 "Location list range start index (%s)",
8790 curr->begin);
8791 /* The length field is 4 bytes. If we ever need to support
8792 an 8-byte length, we can add a new DW_LLE code or fall back
8793 to DW_LLE_GNU_start_end_entry. */
8794 dw2_asm_output_delta (4, curr->end, curr->begin,
8795 "Location list range length (%s)",
8796 list_head->ll_symbol);
8798 else if (!have_multiple_function_sections)
8800 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8801 "Location list begin address (%s)",
8802 list_head->ll_symbol);
8803 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8804 "Location list end address (%s)",
8805 list_head->ll_symbol);
8807 else
8809 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8810 "Location list begin address (%s)",
8811 list_head->ll_symbol);
8812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8813 "Location list end address (%s)",
8814 list_head->ll_symbol);
8817 /* Output the block length for this list of location operations. */
8818 gcc_assert (size <= 0xffff);
8819 dw2_asm_output_data (2, size, "%s", "Location expression size");
8821 output_loc_sequence (curr->expr, -1);
8824 if (dwarf_split_debug_info)
8825 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8826 "Location list terminator (%s)",
8827 list_head->ll_symbol);
8828 else
8830 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8831 "Location list terminator begin (%s)",
8832 list_head->ll_symbol);
8833 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8834 "Location list terminator end (%s)",
8835 list_head->ll_symbol);
8839 /* Output a range_list offset into the debug_range section. Emit a
8840 relocated reference if val_entry is NULL, otherwise, emit an
8841 indirect reference. */
8843 static void
8844 output_range_list_offset (dw_attr_node *a)
8846 const char *name = dwarf_attr_name (a->dw_attr);
8848 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8850 char *p = strchr (ranges_section_label, '\0');
8851 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8852 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8853 debug_ranges_section, "%s", name);
8854 *p = '\0';
8856 else
8857 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8858 "%s (offset from %s)", name, ranges_section_label);
8861 /* Output the offset into the debug_loc section. */
8863 static void
8864 output_loc_list_offset (dw_attr_node *a)
8866 char *sym = AT_loc_list (a)->ll_symbol;
8868 gcc_assert (sym);
8869 if (dwarf_split_debug_info)
8870 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8871 "%s", dwarf_attr_name (a->dw_attr));
8872 else
8873 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8874 "%s", dwarf_attr_name (a->dw_attr));
8877 /* Output an attribute's index or value appropriately. */
8879 static void
8880 output_attr_index_or_value (dw_attr_node *a)
8882 const char *name = dwarf_attr_name (a->dw_attr);
8884 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8886 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8887 return;
8889 switch (AT_class (a))
8891 case dw_val_class_addr:
8892 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8893 break;
8894 case dw_val_class_high_pc:
8895 case dw_val_class_lbl_id:
8896 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8897 break;
8898 case dw_val_class_loc_list:
8899 output_loc_list_offset (a);
8900 break;
8901 default:
8902 gcc_unreachable ();
8906 /* Output a type signature. */
8908 static inline void
8909 output_signature (const char *sig, const char *name)
8911 int i;
8913 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8914 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8917 /* Output the DIE and its attributes. Called recursively to generate
8918 the definitions of each child DIE. */
8920 static void
8921 output_die (dw_die_ref die)
8923 dw_attr_node *a;
8924 dw_die_ref c;
8925 unsigned long size;
8926 unsigned ix;
8928 /* If someone in another CU might refer to us, set up a symbol for
8929 them to point to. */
8930 if (! die->comdat_type_p && die->die_id.die_symbol)
8931 output_die_symbol (die);
8933 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8934 (unsigned long)die->die_offset,
8935 dwarf_tag_name (die->die_tag));
8937 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8939 const char *name = dwarf_attr_name (a->dw_attr);
8941 switch (AT_class (a))
8943 case dw_val_class_addr:
8944 output_attr_index_or_value (a);
8945 break;
8947 case dw_val_class_offset:
8948 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8949 "%s", name);
8950 break;
8952 case dw_val_class_range_list:
8953 output_range_list_offset (a);
8954 break;
8956 case dw_val_class_loc:
8957 size = size_of_locs (AT_loc (a));
8959 /* Output the block length for this list of location operations. */
8960 if (dwarf_version >= 4)
8961 dw2_asm_output_data_uleb128 (size, "%s", name);
8962 else
8963 dw2_asm_output_data (constant_size (size), size, "%s", name);
8965 output_loc_sequence (AT_loc (a), -1);
8966 break;
8968 case dw_val_class_const:
8969 /* ??? It would be slightly more efficient to use a scheme like is
8970 used for unsigned constants below, but gdb 4.x does not sign
8971 extend. Gdb 5.x does sign extend. */
8972 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8973 break;
8975 case dw_val_class_unsigned_const:
8977 int csize = constant_size (AT_unsigned (a));
8978 if (dwarf_version == 3
8979 && a->dw_attr == DW_AT_data_member_location
8980 && csize >= 4)
8981 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8982 else
8983 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8985 break;
8987 case dw_val_class_const_double:
8989 unsigned HOST_WIDE_INT first, second;
8991 if (HOST_BITS_PER_WIDE_INT >= 64)
8992 dw2_asm_output_data (1,
8993 HOST_BITS_PER_DOUBLE_INT
8994 / HOST_BITS_PER_CHAR,
8995 NULL);
8997 if (WORDS_BIG_ENDIAN)
8999 first = a->dw_attr_val.v.val_double.high;
9000 second = a->dw_attr_val.v.val_double.low;
9002 else
9004 first = a->dw_attr_val.v.val_double.low;
9005 second = a->dw_attr_val.v.val_double.high;
9008 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9009 first, "%s", name);
9010 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9011 second, NULL);
9013 break;
9015 case dw_val_class_wide_int:
9017 int i;
9018 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9019 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9020 if (len * HOST_BITS_PER_WIDE_INT > 64)
9021 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9022 NULL);
9024 if (WORDS_BIG_ENDIAN)
9025 for (i = len - 1; i >= 0; --i)
9027 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9028 "%s", name);
9029 name = NULL;
9031 else
9032 for (i = 0; i < len; ++i)
9034 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9035 "%s", name);
9036 name = NULL;
9039 break;
9041 case dw_val_class_vec:
9043 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9044 unsigned int len = a->dw_attr_val.v.val_vec.length;
9045 unsigned int i;
9046 unsigned char *p;
9048 dw2_asm_output_data (constant_size (len * elt_size),
9049 len * elt_size, "%s", name);
9050 if (elt_size > sizeof (HOST_WIDE_INT))
9052 elt_size /= 2;
9053 len *= 2;
9055 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9056 i < len;
9057 i++, p += elt_size)
9058 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9059 "fp or vector constant word %u", i);
9060 break;
9063 case dw_val_class_flag:
9064 if (dwarf_version >= 4)
9066 /* Currently all add_AT_flag calls pass in 1 as last argument,
9067 so DW_FORM_flag_present can be used. If that ever changes,
9068 we'll need to use DW_FORM_flag and have some optimization
9069 in build_abbrev_table that will change those to
9070 DW_FORM_flag_present if it is set to 1 in all DIEs using
9071 the same abbrev entry. */
9072 gcc_assert (AT_flag (a) == 1);
9073 if (flag_debug_asm)
9074 fprintf (asm_out_file, "\t\t\t%s %s\n",
9075 ASM_COMMENT_START, name);
9076 break;
9078 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9079 break;
9081 case dw_val_class_loc_list:
9082 output_attr_index_or_value (a);
9083 break;
9085 case dw_val_class_die_ref:
9086 if (AT_ref_external (a))
9088 if (AT_ref (a)->comdat_type_p)
9090 comdat_type_node *type_node =
9091 AT_ref (a)->die_id.die_type_node;
9093 gcc_assert (type_node);
9094 output_signature (type_node->signature, name);
9096 else
9098 const char *sym = AT_ref (a)->die_id.die_symbol;
9099 int size;
9101 gcc_assert (sym);
9102 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9103 length, whereas in DWARF3 it's always sized as an
9104 offset. */
9105 if (dwarf_version == 2)
9106 size = DWARF2_ADDR_SIZE;
9107 else
9108 size = DWARF_OFFSET_SIZE;
9109 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9110 name);
9113 else
9115 gcc_assert (AT_ref (a)->die_offset);
9116 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9117 "%s", name);
9119 break;
9121 case dw_val_class_fde_ref:
9123 char l1[20];
9125 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9126 a->dw_attr_val.v.val_fde_index * 2);
9127 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9128 "%s", name);
9130 break;
9132 case dw_val_class_vms_delta:
9133 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9134 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9135 AT_vms_delta2 (a), AT_vms_delta1 (a),
9136 "%s", name);
9137 #else
9138 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9139 AT_vms_delta2 (a), AT_vms_delta1 (a),
9140 "%s", name);
9141 #endif
9142 break;
9144 case dw_val_class_lbl_id:
9145 output_attr_index_or_value (a);
9146 break;
9148 case dw_val_class_lineptr:
9149 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9150 debug_line_section, "%s", name);
9151 break;
9153 case dw_val_class_macptr:
9154 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9155 debug_macinfo_section, "%s", name);
9156 break;
9158 case dw_val_class_str:
9159 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9160 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9161 a->dw_attr_val.v.val_str->label,
9162 debug_str_section,
9163 "%s: \"%s\"", name, AT_string (a));
9164 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9165 dw2_asm_output_data_uleb128 (AT_index (a),
9166 "%s: \"%s\"", name, AT_string (a));
9167 else
9168 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9169 break;
9171 case dw_val_class_file:
9173 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9175 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9176 a->dw_attr_val.v.val_file->filename);
9177 break;
9180 case dw_val_class_data8:
9182 int i;
9184 for (i = 0; i < 8; i++)
9185 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9186 i == 0 ? "%s" : NULL, name);
9187 break;
9190 case dw_val_class_high_pc:
9191 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9192 get_AT_low_pc (die), "DW_AT_high_pc");
9193 break;
9195 default:
9196 gcc_unreachable ();
9200 FOR_EACH_CHILD (die, c, output_die (c));
9202 /* Add null byte to terminate sibling list. */
9203 if (die->die_child != NULL)
9204 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9205 (unsigned long) die->die_offset);
9208 /* Output the compilation unit that appears at the beginning of the
9209 .debug_info section, and precedes the DIE descriptions. */
9211 static void
9212 output_compilation_unit_header (void)
9214 /* We don't support actual DWARFv5 units yet, we just use some
9215 DWARFv5 draft DIE tags in DWARFv4 format. */
9216 int ver = dwarf_version < 5 ? dwarf_version : 4;
9218 if (!XCOFF_DEBUGGING_INFO)
9220 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9221 dw2_asm_output_data (4, 0xffffffff,
9222 "Initial length escape value indicating 64-bit DWARF extension");
9223 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9224 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9225 "Length of Compilation Unit Info");
9228 dw2_asm_output_data (2, ver, "DWARF version number");
9229 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9230 debug_abbrev_section,
9231 "Offset Into Abbrev. Section");
9232 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9235 /* Output the compilation unit DIE and its children. */
9237 static void
9238 output_comp_unit (dw_die_ref die, int output_if_empty)
9240 const char *secname, *oldsym;
9241 char *tmp;
9243 /* Unless we are outputting main CU, we may throw away empty ones. */
9244 if (!output_if_empty && die->die_child == NULL)
9245 return;
9247 /* Even if there are no children of this DIE, we must output the information
9248 about the compilation unit. Otherwise, on an empty translation unit, we
9249 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9250 will then complain when examining the file. First mark all the DIEs in
9251 this CU so we know which get local refs. */
9252 mark_dies (die);
9254 external_ref_hash_type *extern_map = optimize_external_refs (die);
9256 build_abbrev_table (die, extern_map);
9258 delete extern_map;
9260 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9261 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9262 calc_die_sizes (die);
9264 oldsym = die->die_id.die_symbol;
9265 if (oldsym)
9267 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9269 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9270 secname = tmp;
9271 die->die_id.die_symbol = NULL;
9272 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9274 else
9276 switch_to_section (debug_info_section);
9277 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9278 info_section_emitted = true;
9281 /* Output debugging information. */
9282 output_compilation_unit_header ();
9283 output_die (die);
9285 /* Leave the marks on the main CU, so we can check them in
9286 output_pubnames. */
9287 if (oldsym)
9289 unmark_dies (die);
9290 die->die_id.die_symbol = oldsym;
9294 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9295 and .debug_pubtypes. This is configured per-target, but can be
9296 overridden by the -gpubnames or -gno-pubnames options. */
9298 static inline bool
9299 want_pubnames (void)
9301 if (debug_info_level <= DINFO_LEVEL_TERSE)
9302 return false;
9303 if (debug_generate_pub_sections != -1)
9304 return debug_generate_pub_sections;
9305 return targetm.want_debug_pub_sections;
9308 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9310 static void
9311 add_AT_pubnames (dw_die_ref die)
9313 if (want_pubnames ())
9314 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9317 /* Add a string attribute value to a skeleton DIE. */
9319 static inline void
9320 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9321 const char *str)
9323 dw_attr_node attr;
9324 struct indirect_string_node *node;
9326 if (! skeleton_debug_str_hash)
9327 skeleton_debug_str_hash
9328 = hash_table<indirect_string_hasher>::create_ggc (10);
9330 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9331 find_string_form (node);
9332 if (node->form == DW_FORM_GNU_str_index)
9333 node->form = DW_FORM_strp;
9335 attr.dw_attr = attr_kind;
9336 attr.dw_attr_val.val_class = dw_val_class_str;
9337 attr.dw_attr_val.val_entry = NULL;
9338 attr.dw_attr_val.v.val_str = node;
9339 add_dwarf_attr (die, &attr);
9342 /* Helper function to generate top-level dies for skeleton debug_info and
9343 debug_types. */
9345 static void
9346 add_top_level_skeleton_die_attrs (dw_die_ref die)
9348 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9349 const char *comp_dir = comp_dir_string ();
9351 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9352 if (comp_dir != NULL)
9353 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9354 add_AT_pubnames (die);
9355 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9358 /* Output skeleton debug sections that point to the dwo file. */
9360 static void
9361 output_skeleton_debug_sections (dw_die_ref comp_unit)
9363 /* We don't support actual DWARFv5 units yet, we just use some
9364 DWARFv5 draft DIE tags in DWARFv4 format. */
9365 int ver = dwarf_version < 5 ? dwarf_version : 4;
9367 /* These attributes will be found in the full debug_info section. */
9368 remove_AT (comp_unit, DW_AT_producer);
9369 remove_AT (comp_unit, DW_AT_language);
9371 switch_to_section (debug_skeleton_info_section);
9372 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9374 /* Produce the skeleton compilation-unit header. This one differs enough from
9375 a normal CU header that it's better not to call output_compilation_unit
9376 header. */
9377 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9378 dw2_asm_output_data (4, 0xffffffff,
9379 "Initial length escape value indicating 64-bit DWARF extension");
9381 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9382 DWARF_COMPILE_UNIT_HEADER_SIZE
9383 - DWARF_INITIAL_LENGTH_SIZE
9384 + size_of_die (comp_unit),
9385 "Length of Compilation Unit Info");
9386 dw2_asm_output_data (2, ver, "DWARF version number");
9387 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9388 debug_abbrev_section,
9389 "Offset Into Abbrev. Section");
9390 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9392 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9393 output_die (comp_unit);
9395 /* Build the skeleton debug_abbrev section. */
9396 switch_to_section (debug_skeleton_abbrev_section);
9397 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9399 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9401 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9404 /* Output a comdat type unit DIE and its children. */
9406 static void
9407 output_comdat_type_unit (comdat_type_node *node)
9409 const char *secname;
9410 char *tmp;
9411 int i;
9412 #if defined (OBJECT_FORMAT_ELF)
9413 tree comdat_key;
9414 #endif
9416 /* First mark all the DIEs in this CU so we know which get local refs. */
9417 mark_dies (node->root_die);
9419 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9421 build_abbrev_table (node->root_die, extern_map);
9423 delete extern_map;
9424 extern_map = NULL;
9426 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9427 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9428 calc_die_sizes (node->root_die);
9430 #if defined (OBJECT_FORMAT_ELF)
9431 if (!dwarf_split_debug_info)
9432 secname = ".debug_types";
9433 else
9434 secname = ".debug_types.dwo";
9436 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9437 sprintf (tmp, "wt.");
9438 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9439 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9440 comdat_key = get_identifier (tmp);
9441 targetm.asm_out.named_section (secname,
9442 SECTION_DEBUG | SECTION_LINKONCE,
9443 comdat_key);
9444 #else
9445 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9446 sprintf (tmp, ".gnu.linkonce.wt.");
9447 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9448 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9449 secname = tmp;
9450 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9451 #endif
9453 /* Output debugging information. */
9454 output_compilation_unit_header ();
9455 output_signature (node->signature, "Type Signature");
9456 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9457 "Offset to Type DIE");
9458 output_die (node->root_die);
9460 unmark_dies (node->root_die);
9463 /* Return the DWARF2/3 pubname associated with a decl. */
9465 static const char *
9466 dwarf2_name (tree decl, int scope)
9468 if (DECL_NAMELESS (decl))
9469 return NULL;
9470 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9473 /* Add a new entry to .debug_pubnames if appropriate. */
9475 static void
9476 add_pubname_string (const char *str, dw_die_ref die)
9478 pubname_entry e;
9480 e.die = die;
9481 e.name = xstrdup (str);
9482 vec_safe_push (pubname_table, e);
9485 static void
9486 add_pubname (tree decl, dw_die_ref die)
9488 if (!want_pubnames ())
9489 return;
9491 /* Don't add items to the table when we expect that the consumer will have
9492 just read the enclosing die. For example, if the consumer is looking at a
9493 class_member, it will either be inside the class already, or will have just
9494 looked up the class to find the member. Either way, searching the class is
9495 faster than searching the index. */
9496 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9497 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9499 const char *name = dwarf2_name (decl, 1);
9501 if (name)
9502 add_pubname_string (name, die);
9506 /* Add an enumerator to the pubnames section. */
9508 static void
9509 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9511 pubname_entry e;
9513 gcc_assert (scope_name);
9514 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9515 e.die = die;
9516 vec_safe_push (pubname_table, e);
9519 /* Add a new entry to .debug_pubtypes if appropriate. */
9521 static void
9522 add_pubtype (tree decl, dw_die_ref die)
9524 pubname_entry e;
9526 if (!want_pubnames ())
9527 return;
9529 if ((TREE_PUBLIC (decl)
9530 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9531 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9533 tree scope = NULL;
9534 const char *scope_name = "";
9535 const char *sep = is_cxx () ? "::" : ".";
9536 const char *name;
9538 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9539 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9541 scope_name = lang_hooks.dwarf_name (scope, 1);
9542 if (scope_name != NULL && scope_name[0] != '\0')
9543 scope_name = concat (scope_name, sep, NULL);
9544 else
9545 scope_name = "";
9548 if (TYPE_P (decl))
9549 name = type_tag (decl);
9550 else
9551 name = lang_hooks.dwarf_name (decl, 1);
9553 /* If we don't have a name for the type, there's no point in adding
9554 it to the table. */
9555 if (name != NULL && name[0] != '\0')
9557 e.die = die;
9558 e.name = concat (scope_name, name, NULL);
9559 vec_safe_push (pubtype_table, e);
9562 /* Although it might be more consistent to add the pubinfo for the
9563 enumerators as their dies are created, they should only be added if the
9564 enum type meets the criteria above. So rather than re-check the parent
9565 enum type whenever an enumerator die is created, just output them all
9566 here. This isn't protected by the name conditional because anonymous
9567 enums don't have names. */
9568 if (die->die_tag == DW_TAG_enumeration_type)
9570 dw_die_ref c;
9572 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9577 /* Output a single entry in the pubnames table. */
9579 static void
9580 output_pubname (dw_offset die_offset, pubname_entry *entry)
9582 dw_die_ref die = entry->die;
9583 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9585 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9587 if (debug_generate_pub_sections == 2)
9589 /* This logic follows gdb's method for determining the value of the flag
9590 byte. */
9591 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9592 switch (die->die_tag)
9594 case DW_TAG_typedef:
9595 case DW_TAG_base_type:
9596 case DW_TAG_subrange_type:
9597 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9598 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9599 break;
9600 case DW_TAG_enumerator:
9601 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9602 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9603 if (!is_cxx () && !is_java ())
9604 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9605 break;
9606 case DW_TAG_subprogram:
9607 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9608 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9609 if (!is_ada ())
9610 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9611 break;
9612 case DW_TAG_constant:
9613 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9614 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9615 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9616 break;
9617 case DW_TAG_variable:
9618 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9619 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9620 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9621 break;
9622 case DW_TAG_namespace:
9623 case DW_TAG_imported_declaration:
9624 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9625 break;
9626 case DW_TAG_class_type:
9627 case DW_TAG_interface_type:
9628 case DW_TAG_structure_type:
9629 case DW_TAG_union_type:
9630 case DW_TAG_enumeration_type:
9631 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9632 if (!is_cxx () && !is_java ())
9633 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9634 break;
9635 default:
9636 /* An unusual tag. Leave the flag-byte empty. */
9637 break;
9639 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9640 "GDB-index flags");
9643 dw2_asm_output_nstring (entry->name, -1, "external name");
9647 /* Output the public names table used to speed up access to externally
9648 visible names; or the public types table used to find type definitions. */
9650 static void
9651 output_pubnames (vec<pubname_entry, va_gc> *names)
9653 unsigned i;
9654 unsigned long pubnames_length = size_of_pubnames (names);
9655 pubname_entry *pub;
9657 if (!XCOFF_DEBUGGING_INFO)
9659 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9660 dw2_asm_output_data (4, 0xffffffff,
9661 "Initial length escape value indicating 64-bit DWARF extension");
9662 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9663 "Pub Info Length");
9666 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9667 dw2_asm_output_data (2, 2, "DWARF Version");
9669 if (dwarf_split_debug_info)
9670 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9671 debug_skeleton_info_section,
9672 "Offset of Compilation Unit Info");
9673 else
9674 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9675 debug_info_section,
9676 "Offset of Compilation Unit Info");
9677 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9678 "Compilation Unit Length");
9680 FOR_EACH_VEC_ELT (*names, i, pub)
9682 if (include_pubname_in_output (names, pub))
9684 dw_offset die_offset = pub->die->die_offset;
9686 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9687 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9688 gcc_assert (pub->die->die_mark);
9690 /* If we're putting types in their own .debug_types sections,
9691 the .debug_pubtypes table will still point to the compile
9692 unit (not the type unit), so we want to use the offset of
9693 the skeleton DIE (if there is one). */
9694 if (pub->die->comdat_type_p && names == pubtype_table)
9696 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9698 if (type_node != NULL)
9699 die_offset = (type_node->skeleton_die != NULL
9700 ? type_node->skeleton_die->die_offset
9701 : comp_unit_die ()->die_offset);
9704 output_pubname (die_offset, pub);
9708 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9711 /* Output public names and types tables if necessary. */
9713 static void
9714 output_pubtables (void)
9716 if (!want_pubnames () || !info_section_emitted)
9717 return;
9719 switch_to_section (debug_pubnames_section);
9720 output_pubnames (pubname_table);
9721 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9722 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9723 simply won't look for the section. */
9724 switch_to_section (debug_pubtypes_section);
9725 output_pubnames (pubtype_table);
9729 /* Output the information that goes into the .debug_aranges table.
9730 Namely, define the beginning and ending address range of the
9731 text section generated for this compilation unit. */
9733 static void
9734 output_aranges (void)
9736 unsigned i;
9737 unsigned long aranges_length = size_of_aranges ();
9739 if (!XCOFF_DEBUGGING_INFO)
9741 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9742 dw2_asm_output_data (4, 0xffffffff,
9743 "Initial length escape value indicating 64-bit DWARF extension");
9744 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9745 "Length of Address Ranges Info");
9748 /* Version number for aranges is still 2, even up to DWARF5. */
9749 dw2_asm_output_data (2, 2, "DWARF Version");
9750 if (dwarf_split_debug_info)
9751 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9752 debug_skeleton_info_section,
9753 "Offset of Compilation Unit Info");
9754 else
9755 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9756 debug_info_section,
9757 "Offset of Compilation Unit Info");
9758 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9759 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9761 /* We need to align to twice the pointer size here. */
9762 if (DWARF_ARANGES_PAD_SIZE)
9764 /* Pad using a 2 byte words so that padding is correct for any
9765 pointer size. */
9766 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9767 2 * DWARF2_ADDR_SIZE);
9768 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9769 dw2_asm_output_data (2, 0, NULL);
9772 /* It is necessary not to output these entries if the sections were
9773 not used; if the sections were not used, the length will be 0 and
9774 the address may end up as 0 if the section is discarded by ld
9775 --gc-sections, leaving an invalid (0, 0) entry that can be
9776 confused with the terminator. */
9777 if (text_section_used)
9779 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9780 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9781 text_section_label, "Length");
9783 if (cold_text_section_used)
9785 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9786 "Address");
9787 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9788 cold_text_section_label, "Length");
9791 if (have_multiple_function_sections)
9793 unsigned fde_idx;
9794 dw_fde_ref fde;
9796 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9798 if (DECL_IGNORED_P (fde->decl))
9799 continue;
9800 if (!fde->in_std_section)
9802 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9803 "Address");
9804 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9805 fde->dw_fde_begin, "Length");
9807 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9809 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9810 "Address");
9811 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9812 fde->dw_fde_second_begin, "Length");
9817 /* Output the terminator words. */
9818 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9819 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9822 /* Add a new entry to .debug_ranges. Return the offset at which it
9823 was placed. */
9825 static unsigned int
9826 add_ranges_num (int num)
9828 unsigned int in_use = ranges_table_in_use;
9830 if (in_use == ranges_table_allocated)
9832 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9833 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
9834 ranges_table_allocated);
9835 memset (ranges_table + ranges_table_in_use, 0,
9836 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
9839 ranges_table[in_use].num = num;
9840 ranges_table_in_use = in_use + 1;
9842 return in_use * 2 * DWARF2_ADDR_SIZE;
9845 /* Add a new entry to .debug_ranges corresponding to a block, or a
9846 range terminator if BLOCK is NULL. */
9848 static unsigned int
9849 add_ranges (const_tree block)
9851 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9854 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9855 When using dwarf_split_debug_info, address attributes in dies destined
9856 for the final executable should be direct references--setting the
9857 parameter force_direct ensures this behavior. */
9859 static void
9860 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9861 bool *added, bool force_direct)
9863 unsigned int in_use = ranges_by_label_in_use;
9864 unsigned int offset;
9866 if (in_use == ranges_by_label_allocated)
9868 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9869 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
9870 ranges_by_label_allocated);
9871 memset (ranges_by_label + ranges_by_label_in_use, 0,
9872 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
9875 ranges_by_label[in_use].begin = begin;
9876 ranges_by_label[in_use].end = end;
9877 ranges_by_label_in_use = in_use + 1;
9879 offset = add_ranges_num (-(int)in_use - 1);
9880 if (!*added)
9882 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9883 *added = true;
9887 static void
9888 output_ranges (void)
9890 unsigned i;
9891 static const char *const start_fmt = "Offset %#x";
9892 const char *fmt = start_fmt;
9894 for (i = 0; i < ranges_table_in_use; i++)
9896 int block_num = ranges_table[i].num;
9898 if (block_num > 0)
9900 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9901 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9903 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9904 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9906 /* If all code is in the text section, then the compilation
9907 unit base address defaults to DW_AT_low_pc, which is the
9908 base of the text section. */
9909 if (!have_multiple_function_sections)
9911 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9912 text_section_label,
9913 fmt, i * 2 * DWARF2_ADDR_SIZE);
9914 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9915 text_section_label, NULL);
9918 /* Otherwise, the compilation unit base address is zero,
9919 which allows us to use absolute addresses, and not worry
9920 about whether the target supports cross-section
9921 arithmetic. */
9922 else
9924 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9925 fmt, i * 2 * DWARF2_ADDR_SIZE);
9926 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9929 fmt = NULL;
9932 /* Negative block_num stands for an index into ranges_by_label. */
9933 else if (block_num < 0)
9935 int lab_idx = - block_num - 1;
9937 if (!have_multiple_function_sections)
9939 gcc_unreachable ();
9940 #if 0
9941 /* If we ever use add_ranges_by_labels () for a single
9942 function section, all we have to do is to take out
9943 the #if 0 above. */
9944 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9945 ranges_by_label[lab_idx].begin,
9946 text_section_label,
9947 fmt, i * 2 * DWARF2_ADDR_SIZE);
9948 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9949 ranges_by_label[lab_idx].end,
9950 text_section_label, NULL);
9951 #endif
9953 else
9955 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9956 ranges_by_label[lab_idx].begin,
9957 fmt, i * 2 * DWARF2_ADDR_SIZE);
9958 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9959 ranges_by_label[lab_idx].end,
9960 NULL);
9963 else
9965 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9966 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9967 fmt = start_fmt;
9972 /* Data structure containing information about input files. */
9973 struct file_info
9975 const char *path; /* Complete file name. */
9976 const char *fname; /* File name part. */
9977 int length; /* Length of entire string. */
9978 struct dwarf_file_data * file_idx; /* Index in input file table. */
9979 int dir_idx; /* Index in directory table. */
9982 /* Data structure containing information about directories with source
9983 files. */
9984 struct dir_info
9986 const char *path; /* Path including directory name. */
9987 int length; /* Path length. */
9988 int prefix; /* Index of directory entry which is a prefix. */
9989 int count; /* Number of files in this directory. */
9990 int dir_idx; /* Index of directory used as base. */
9993 /* Callback function for file_info comparison. We sort by looking at
9994 the directories in the path. */
9996 static int
9997 file_info_cmp (const void *p1, const void *p2)
9999 const struct file_info *const s1 = (const struct file_info *) p1;
10000 const struct file_info *const s2 = (const struct file_info *) p2;
10001 const unsigned char *cp1;
10002 const unsigned char *cp2;
10004 /* Take care of file names without directories. We need to make sure that
10005 we return consistent values to qsort since some will get confused if
10006 we return the same value when identical operands are passed in opposite
10007 orders. So if neither has a directory, return 0 and otherwise return
10008 1 or -1 depending on which one has the directory. */
10009 if ((s1->path == s1->fname || s2->path == s2->fname))
10010 return (s2->path == s2->fname) - (s1->path == s1->fname);
10012 cp1 = (const unsigned char *) s1->path;
10013 cp2 = (const unsigned char *) s2->path;
10015 while (1)
10017 ++cp1;
10018 ++cp2;
10019 /* Reached the end of the first path? If so, handle like above. */
10020 if ((cp1 == (const unsigned char *) s1->fname)
10021 || (cp2 == (const unsigned char *) s2->fname))
10022 return ((cp2 == (const unsigned char *) s2->fname)
10023 - (cp1 == (const unsigned char *) s1->fname));
10025 /* Character of current path component the same? */
10026 else if (*cp1 != *cp2)
10027 return *cp1 - *cp2;
10031 struct file_name_acquire_data
10033 struct file_info *files;
10034 int used_files;
10035 int max_files;
10038 /* Traversal function for the hash table. */
10041 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10043 struct dwarf_file_data *d = *slot;
10044 struct file_info *fi;
10045 const char *f;
10047 gcc_assert (fnad->max_files >= d->emitted_number);
10049 if (! d->emitted_number)
10050 return 1;
10052 gcc_assert (fnad->max_files != fnad->used_files);
10054 fi = fnad->files + fnad->used_files++;
10056 /* Skip all leading "./". */
10057 f = d->filename;
10058 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10059 f += 2;
10061 /* Create a new array entry. */
10062 fi->path = f;
10063 fi->length = strlen (f);
10064 fi->file_idx = d;
10066 /* Search for the file name part. */
10067 f = strrchr (f, DIR_SEPARATOR);
10068 #if defined (DIR_SEPARATOR_2)
10070 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10072 if (g != NULL)
10074 if (f == NULL || f < g)
10075 f = g;
10078 #endif
10080 fi->fname = f == NULL ? fi->path : f + 1;
10081 return 1;
10084 /* Output the directory table and the file name table. We try to minimize
10085 the total amount of memory needed. A heuristic is used to avoid large
10086 slowdowns with many input files. */
10088 static void
10089 output_file_names (void)
10091 struct file_name_acquire_data fnad;
10092 int numfiles;
10093 struct file_info *files;
10094 struct dir_info *dirs;
10095 int *saved;
10096 int *savehere;
10097 int *backmap;
10098 int ndirs;
10099 int idx_offset;
10100 int i;
10102 if (!last_emitted_file)
10104 dw2_asm_output_data (1, 0, "End directory table");
10105 dw2_asm_output_data (1, 0, "End file name table");
10106 return;
10109 numfiles = last_emitted_file->emitted_number;
10111 /* Allocate the various arrays we need. */
10112 files = XALLOCAVEC (struct file_info, numfiles);
10113 dirs = XALLOCAVEC (struct dir_info, numfiles);
10115 fnad.files = files;
10116 fnad.used_files = 0;
10117 fnad.max_files = numfiles;
10118 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10119 gcc_assert (fnad.used_files == fnad.max_files);
10121 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10123 /* Find all the different directories used. */
10124 dirs[0].path = files[0].path;
10125 dirs[0].length = files[0].fname - files[0].path;
10126 dirs[0].prefix = -1;
10127 dirs[0].count = 1;
10128 dirs[0].dir_idx = 0;
10129 files[0].dir_idx = 0;
10130 ndirs = 1;
10132 for (i = 1; i < numfiles; i++)
10133 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10134 && memcmp (dirs[ndirs - 1].path, files[i].path,
10135 dirs[ndirs - 1].length) == 0)
10137 /* Same directory as last entry. */
10138 files[i].dir_idx = ndirs - 1;
10139 ++dirs[ndirs - 1].count;
10141 else
10143 int j;
10145 /* This is a new directory. */
10146 dirs[ndirs].path = files[i].path;
10147 dirs[ndirs].length = files[i].fname - files[i].path;
10148 dirs[ndirs].count = 1;
10149 dirs[ndirs].dir_idx = ndirs;
10150 files[i].dir_idx = ndirs;
10152 /* Search for a prefix. */
10153 dirs[ndirs].prefix = -1;
10154 for (j = 0; j < ndirs; j++)
10155 if (dirs[j].length < dirs[ndirs].length
10156 && dirs[j].length > 1
10157 && (dirs[ndirs].prefix == -1
10158 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10159 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10160 dirs[ndirs].prefix = j;
10162 ++ndirs;
10165 /* Now to the actual work. We have to find a subset of the directories which
10166 allow expressing the file name using references to the directory table
10167 with the least amount of characters. We do not do an exhaustive search
10168 where we would have to check out every combination of every single
10169 possible prefix. Instead we use a heuristic which provides nearly optimal
10170 results in most cases and never is much off. */
10171 saved = XALLOCAVEC (int, ndirs);
10172 savehere = XALLOCAVEC (int, ndirs);
10174 memset (saved, '\0', ndirs * sizeof (saved[0]));
10175 for (i = 0; i < ndirs; i++)
10177 int j;
10178 int total;
10180 /* We can always save some space for the current directory. But this
10181 does not mean it will be enough to justify adding the directory. */
10182 savehere[i] = dirs[i].length;
10183 total = (savehere[i] - saved[i]) * dirs[i].count;
10185 for (j = i + 1; j < ndirs; j++)
10187 savehere[j] = 0;
10188 if (saved[j] < dirs[i].length)
10190 /* Determine whether the dirs[i] path is a prefix of the
10191 dirs[j] path. */
10192 int k;
10194 k = dirs[j].prefix;
10195 while (k != -1 && k != (int) i)
10196 k = dirs[k].prefix;
10198 if (k == (int) i)
10200 /* Yes it is. We can possibly save some memory by
10201 writing the filenames in dirs[j] relative to
10202 dirs[i]. */
10203 savehere[j] = dirs[i].length;
10204 total += (savehere[j] - saved[j]) * dirs[j].count;
10209 /* Check whether we can save enough to justify adding the dirs[i]
10210 directory. */
10211 if (total > dirs[i].length + 1)
10213 /* It's worthwhile adding. */
10214 for (j = i; j < ndirs; j++)
10215 if (savehere[j] > 0)
10217 /* Remember how much we saved for this directory so far. */
10218 saved[j] = savehere[j];
10220 /* Remember the prefix directory. */
10221 dirs[j].dir_idx = i;
10226 /* Emit the directory name table. */
10227 idx_offset = dirs[0].length > 0 ? 1 : 0;
10228 for (i = 1 - idx_offset; i < ndirs; i++)
10229 dw2_asm_output_nstring (dirs[i].path,
10230 dirs[i].length
10231 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10232 "Directory Entry: %#x", i + idx_offset);
10234 dw2_asm_output_data (1, 0, "End directory table");
10236 /* We have to emit them in the order of emitted_number since that's
10237 used in the debug info generation. To do this efficiently we
10238 generate a back-mapping of the indices first. */
10239 backmap = XALLOCAVEC (int, numfiles);
10240 for (i = 0; i < numfiles; i++)
10241 backmap[files[i].file_idx->emitted_number - 1] = i;
10243 /* Now write all the file names. */
10244 for (i = 0; i < numfiles; i++)
10246 int file_idx = backmap[i];
10247 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10249 #ifdef VMS_DEBUGGING_INFO
10250 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10252 /* Setting these fields can lead to debugger miscomparisons,
10253 but VMS Debug requires them to be set correctly. */
10255 int ver;
10256 long long cdt;
10257 long siz;
10258 int maxfilelen = strlen (files[file_idx].path)
10259 + dirs[dir_idx].length
10260 + MAX_VMS_VERSION_LEN + 1;
10261 char *filebuf = XALLOCAVEC (char, maxfilelen);
10263 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10264 snprintf (filebuf, maxfilelen, "%s;%d",
10265 files[file_idx].path + dirs[dir_idx].length, ver);
10267 dw2_asm_output_nstring
10268 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10270 /* Include directory index. */
10271 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10273 /* Modification time. */
10274 dw2_asm_output_data_uleb128
10275 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10276 ? cdt : 0,
10277 NULL);
10279 /* File length in bytes. */
10280 dw2_asm_output_data_uleb128
10281 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10282 ? siz : 0,
10283 NULL);
10284 #else
10285 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10286 "File Entry: %#x", (unsigned) i + 1);
10288 /* Include directory index. */
10289 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10291 /* Modification time. */
10292 dw2_asm_output_data_uleb128 (0, NULL);
10294 /* File length in bytes. */
10295 dw2_asm_output_data_uleb128 (0, NULL);
10296 #endif /* VMS_DEBUGGING_INFO */
10299 dw2_asm_output_data (1, 0, "End file name table");
10303 /* Output one line number table into the .debug_line section. */
10305 static void
10306 output_one_line_info_table (dw_line_info_table *table)
10308 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10309 unsigned int current_line = 1;
10310 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10311 dw_line_info_entry *ent;
10312 size_t i;
10314 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10316 switch (ent->opcode)
10318 case LI_set_address:
10319 /* ??? Unfortunately, we have little choice here currently, and
10320 must always use the most general form. GCC does not know the
10321 address delta itself, so we can't use DW_LNS_advance_pc. Many
10322 ports do have length attributes which will give an upper bound
10323 on the address range. We could perhaps use length attributes
10324 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10325 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10327 /* This can handle any delta. This takes
10328 4+DWARF2_ADDR_SIZE bytes. */
10329 dw2_asm_output_data (1, 0, "set address %s", line_label);
10330 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10331 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10332 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10333 break;
10335 case LI_set_line:
10336 if (ent->val == current_line)
10338 /* We still need to start a new row, so output a copy insn. */
10339 dw2_asm_output_data (1, DW_LNS_copy,
10340 "copy line %u", current_line);
10342 else
10344 int line_offset = ent->val - current_line;
10345 int line_delta = line_offset - DWARF_LINE_BASE;
10347 current_line = ent->val;
10348 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10350 /* This can handle deltas from -10 to 234, using the current
10351 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10352 This takes 1 byte. */
10353 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10354 "line %u", current_line);
10356 else
10358 /* This can handle any delta. This takes at least 4 bytes,
10359 depending on the value being encoded. */
10360 dw2_asm_output_data (1, DW_LNS_advance_line,
10361 "advance to line %u", current_line);
10362 dw2_asm_output_data_sleb128 (line_offset, NULL);
10363 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10366 break;
10368 case LI_set_file:
10369 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10370 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10371 break;
10373 case LI_set_column:
10374 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10375 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10376 break;
10378 case LI_negate_stmt:
10379 current_is_stmt = !current_is_stmt;
10380 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10381 "is_stmt %d", current_is_stmt);
10382 break;
10384 case LI_set_prologue_end:
10385 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10386 "set prologue end");
10387 break;
10389 case LI_set_epilogue_begin:
10390 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10391 "set epilogue begin");
10392 break;
10394 case LI_set_discriminator:
10395 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10396 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10397 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10398 dw2_asm_output_data_uleb128 (ent->val, NULL);
10399 break;
10403 /* Emit debug info for the address of the end of the table. */
10404 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10405 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10406 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10407 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10409 dw2_asm_output_data (1, 0, "end sequence");
10410 dw2_asm_output_data_uleb128 (1, NULL);
10411 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10414 /* Output the source line number correspondence information. This
10415 information goes into the .debug_line section. */
10417 static void
10418 output_line_info (bool prologue_only)
10420 char l1[20], l2[20], p1[20], p2[20];
10421 /* We don't support DWARFv5 line tables yet. */
10422 int ver = dwarf_version < 5 ? dwarf_version : 4;
10423 bool saw_one = false;
10424 int opc;
10426 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10427 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10428 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10429 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10431 if (!XCOFF_DEBUGGING_INFO)
10433 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10434 dw2_asm_output_data (4, 0xffffffff,
10435 "Initial length escape value indicating 64-bit DWARF extension");
10436 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10437 "Length of Source Line Info");
10440 ASM_OUTPUT_LABEL (asm_out_file, l1);
10442 dw2_asm_output_data (2, ver, "DWARF Version");
10443 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10444 ASM_OUTPUT_LABEL (asm_out_file, p1);
10446 /* Define the architecture-dependent minimum instruction length (in bytes).
10447 In this implementation of DWARF, this field is used for information
10448 purposes only. Since GCC generates assembly language, we have no
10449 a priori knowledge of how many instruction bytes are generated for each
10450 source line, and therefore can use only the DW_LNE_set_address and
10451 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10452 this as '1', which is "correct enough" for all architectures,
10453 and don't let the target override. */
10454 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10456 if (ver >= 4)
10457 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10458 "Maximum Operations Per Instruction");
10459 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10460 "Default is_stmt_start flag");
10461 dw2_asm_output_data (1, DWARF_LINE_BASE,
10462 "Line Base Value (Special Opcodes)");
10463 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10464 "Line Range Value (Special Opcodes)");
10465 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10466 "Special Opcode Base");
10468 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10470 int n_op_args;
10471 switch (opc)
10473 case DW_LNS_advance_pc:
10474 case DW_LNS_advance_line:
10475 case DW_LNS_set_file:
10476 case DW_LNS_set_column:
10477 case DW_LNS_fixed_advance_pc:
10478 case DW_LNS_set_isa:
10479 n_op_args = 1;
10480 break;
10481 default:
10482 n_op_args = 0;
10483 break;
10486 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10487 opc, n_op_args);
10490 /* Write out the information about the files we use. */
10491 output_file_names ();
10492 ASM_OUTPUT_LABEL (asm_out_file, p2);
10493 if (prologue_only)
10495 /* Output the marker for the end of the line number info. */
10496 ASM_OUTPUT_LABEL (asm_out_file, l2);
10497 return;
10500 if (separate_line_info)
10502 dw_line_info_table *table;
10503 size_t i;
10505 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10506 if (table->in_use)
10508 output_one_line_info_table (table);
10509 saw_one = true;
10512 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10514 output_one_line_info_table (cold_text_section_line_info);
10515 saw_one = true;
10518 /* ??? Some Darwin linkers crash on a .debug_line section with no
10519 sequences. Further, merely a DW_LNE_end_sequence entry is not
10520 sufficient -- the address column must also be initialized.
10521 Make sure to output at least one set_address/end_sequence pair,
10522 choosing .text since that section is always present. */
10523 if (text_section_line_info->in_use || !saw_one)
10524 output_one_line_info_table (text_section_line_info);
10526 /* Output the marker for the end of the line number info. */
10527 ASM_OUTPUT_LABEL (asm_out_file, l2);
10530 /* Given a pointer to a tree node for some base type, return a pointer to
10531 a DIE that describes the given type.
10533 This routine must only be called for GCC type nodes that correspond to
10534 Dwarf base (fundamental) types. */
10536 static dw_die_ref
10537 base_type_die (tree type)
10539 dw_die_ref base_type_result;
10540 enum dwarf_type encoding;
10542 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10543 return 0;
10545 /* If this is a subtype that should not be emitted as a subrange type,
10546 use the base type. See subrange_type_for_debug_p. */
10547 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10548 type = TREE_TYPE (type);
10550 switch (TREE_CODE (type))
10552 case INTEGER_TYPE:
10553 if ((dwarf_version >= 4 || !dwarf_strict)
10554 && TYPE_NAME (type)
10555 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10556 && DECL_IS_BUILTIN (TYPE_NAME (type))
10557 && DECL_NAME (TYPE_NAME (type)))
10559 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10560 if (strcmp (name, "char16_t") == 0
10561 || strcmp (name, "char32_t") == 0)
10563 encoding = DW_ATE_UTF;
10564 break;
10567 if (TYPE_STRING_FLAG (type))
10569 if (TYPE_UNSIGNED (type))
10570 encoding = DW_ATE_unsigned_char;
10571 else
10572 encoding = DW_ATE_signed_char;
10574 else if (TYPE_UNSIGNED (type))
10575 encoding = DW_ATE_unsigned;
10576 else
10577 encoding = DW_ATE_signed;
10578 break;
10580 case REAL_TYPE:
10581 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10583 if (dwarf_version >= 3 || !dwarf_strict)
10584 encoding = DW_ATE_decimal_float;
10585 else
10586 encoding = DW_ATE_lo_user;
10588 else
10589 encoding = DW_ATE_float;
10590 break;
10592 case FIXED_POINT_TYPE:
10593 if (!(dwarf_version >= 3 || !dwarf_strict))
10594 encoding = DW_ATE_lo_user;
10595 else if (TYPE_UNSIGNED (type))
10596 encoding = DW_ATE_unsigned_fixed;
10597 else
10598 encoding = DW_ATE_signed_fixed;
10599 break;
10601 /* Dwarf2 doesn't know anything about complex ints, so use
10602 a user defined type for it. */
10603 case COMPLEX_TYPE:
10604 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10605 encoding = DW_ATE_complex_float;
10606 else
10607 encoding = DW_ATE_lo_user;
10608 break;
10610 case BOOLEAN_TYPE:
10611 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10612 encoding = DW_ATE_boolean;
10613 break;
10615 default:
10616 /* No other TREE_CODEs are Dwarf fundamental types. */
10617 gcc_unreachable ();
10620 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10622 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10623 int_size_in_bytes (type));
10624 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10625 add_pubtype (type, base_type_result);
10627 return base_type_result;
10630 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10631 named 'auto' in its type: return true for it, false otherwise. */
10633 static inline bool
10634 is_cxx_auto (tree type)
10636 if (is_cxx ())
10638 tree name = TYPE_IDENTIFIER (type);
10639 if (name == get_identifier ("auto")
10640 || name == get_identifier ("decltype(auto)"))
10641 return true;
10643 return false;
10646 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10647 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10649 static inline int
10650 is_base_type (tree type)
10652 switch (TREE_CODE (type))
10654 case ERROR_MARK:
10655 case VOID_TYPE:
10656 case INTEGER_TYPE:
10657 case REAL_TYPE:
10658 case FIXED_POINT_TYPE:
10659 case COMPLEX_TYPE:
10660 case BOOLEAN_TYPE:
10661 case POINTER_BOUNDS_TYPE:
10662 return 1;
10664 case ARRAY_TYPE:
10665 case RECORD_TYPE:
10666 case UNION_TYPE:
10667 case QUAL_UNION_TYPE:
10668 case ENUMERAL_TYPE:
10669 case FUNCTION_TYPE:
10670 case METHOD_TYPE:
10671 case POINTER_TYPE:
10672 case REFERENCE_TYPE:
10673 case NULLPTR_TYPE:
10674 case OFFSET_TYPE:
10675 case LANG_TYPE:
10676 case VECTOR_TYPE:
10677 return 0;
10679 default:
10680 if (is_cxx_auto (type))
10681 return 0;
10682 gcc_unreachable ();
10685 return 0;
10688 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10689 node, return the size in bits for the type if it is a constant, or else
10690 return the alignment for the type if the type's size is not constant, or
10691 else return BITS_PER_WORD if the type actually turns out to be an
10692 ERROR_MARK node. */
10694 static inline unsigned HOST_WIDE_INT
10695 simple_type_size_in_bits (const_tree type)
10697 if (TREE_CODE (type) == ERROR_MARK)
10698 return BITS_PER_WORD;
10699 else if (TYPE_SIZE (type) == NULL_TREE)
10700 return 0;
10701 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10702 return tree_to_uhwi (TYPE_SIZE (type));
10703 else
10704 return TYPE_ALIGN (type);
10707 /* Similarly, but return an offset_int instead of UHWI. */
10709 static inline offset_int
10710 offset_int_type_size_in_bits (const_tree type)
10712 if (TREE_CODE (type) == ERROR_MARK)
10713 return BITS_PER_WORD;
10714 else if (TYPE_SIZE (type) == NULL_TREE)
10715 return 0;
10716 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10717 return wi::to_offset (TYPE_SIZE (type));
10718 else
10719 return TYPE_ALIGN (type);
10722 /* Given a pointer to a tree node for a subrange type, return a pointer
10723 to a DIE that describes the given type. */
10725 static dw_die_ref
10726 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10728 dw_die_ref subrange_die;
10729 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10731 if (context_die == NULL)
10732 context_die = comp_unit_die ();
10734 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10736 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10738 /* The size of the subrange type and its base type do not match,
10739 so we need to generate a size attribute for the subrange type. */
10740 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10743 if (low)
10744 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10745 if (high)
10746 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10748 return subrange_die;
10751 /* Returns the (const and/or volatile) cv_qualifiers associated with
10752 the decl node. This will normally be augmented with the
10753 cv_qualifiers of the underlying type in add_type_attribute. */
10755 static int
10756 decl_quals (const_tree decl)
10758 return ((TREE_READONLY (decl)
10759 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10760 | (TREE_THIS_VOLATILE (decl)
10761 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10764 /* Determine the TYPE whose qualifiers match the largest strict subset
10765 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10766 qualifiers outside QUAL_MASK. */
10768 static int
10769 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10771 tree t;
10772 int best_rank = 0, best_qual = 0, max_rank;
10774 type_quals &= qual_mask;
10775 max_rank = popcount_hwi (type_quals) - 1;
10777 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10778 t = TYPE_NEXT_VARIANT (t))
10780 int q = TYPE_QUALS (t) & qual_mask;
10782 if ((q & type_quals) == q && q != type_quals
10783 && check_base_type (t, type))
10785 int rank = popcount_hwi (q);
10787 if (rank > best_rank)
10789 best_rank = rank;
10790 best_qual = q;
10795 return best_qual;
10798 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10799 entry that chains various modifiers in front of the given type. */
10801 static dw_die_ref
10802 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10804 enum tree_code code = TREE_CODE (type);
10805 dw_die_ref mod_type_die;
10806 dw_die_ref sub_die = NULL;
10807 tree item_type = NULL;
10808 tree qualified_type;
10809 tree name, low, high;
10810 dw_die_ref mod_scope;
10811 /* Only these cv-qualifiers are currently handled. */
10812 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10813 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10815 if (code == ERROR_MARK)
10816 return NULL;
10818 cv_quals &= cv_qual_mask;
10820 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10821 tag modifier (and not an attribute) old consumers won't be able
10822 to handle it. */
10823 if (dwarf_version < 3)
10824 cv_quals &= ~TYPE_QUAL_RESTRICT;
10826 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10827 if (dwarf_version < 5)
10828 cv_quals &= ~TYPE_QUAL_ATOMIC;
10830 /* See if we already have the appropriately qualified variant of
10831 this type. */
10832 qualified_type = get_qualified_type (type, cv_quals);
10834 if (qualified_type == sizetype
10835 && TYPE_NAME (qualified_type)
10836 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10838 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10840 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10841 && TYPE_PRECISION (t)
10842 == TYPE_PRECISION (qualified_type)
10843 && TYPE_UNSIGNED (t)
10844 == TYPE_UNSIGNED (qualified_type));
10845 qualified_type = t;
10848 /* If we do, then we can just use its DIE, if it exists. */
10849 if (qualified_type)
10851 mod_type_die = lookup_type_die (qualified_type);
10852 if (mod_type_die)
10853 return mod_type_die;
10856 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10858 /* Handle C typedef types. */
10859 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10860 && !DECL_ARTIFICIAL (name))
10862 tree dtype = TREE_TYPE (name);
10864 if (qualified_type == dtype)
10866 /* For a named type, use the typedef. */
10867 gen_type_die (qualified_type, context_die);
10868 return lookup_type_die (qualified_type);
10870 else
10872 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10873 dquals &= cv_qual_mask;
10874 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10875 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10876 /* cv-unqualified version of named type. Just use
10877 the unnamed type to which it refers. */
10878 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10879 cv_quals, context_die);
10880 /* Else cv-qualified version of named type; fall through. */
10884 mod_scope = scope_die_for (type, context_die);
10886 if (cv_quals)
10888 struct qual_info { int q; enum dwarf_tag t; };
10889 static const struct qual_info qual_info[] =
10891 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10892 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10893 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10894 { TYPE_QUAL_CONST, DW_TAG_const_type },
10896 int sub_quals;
10897 unsigned i;
10899 /* Determine a lesser qualified type that most closely matches
10900 this one. Then generate DW_TAG_* entries for the remaining
10901 qualifiers. */
10902 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10903 cv_qual_mask);
10904 mod_type_die = modified_type_die (type, sub_quals, context_die);
10906 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10907 if (qual_info[i].q & cv_quals & ~sub_quals)
10909 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10910 if (mod_type_die)
10911 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10912 mod_type_die = d;
10915 else if (code == POINTER_TYPE)
10917 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10918 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10919 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10920 item_type = TREE_TYPE (type);
10921 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10922 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10923 TYPE_ADDR_SPACE (item_type));
10925 else if (code == REFERENCE_TYPE)
10927 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10928 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10929 type);
10930 else
10931 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10932 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10933 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10934 item_type = TREE_TYPE (type);
10935 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10936 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10937 TYPE_ADDR_SPACE (item_type));
10939 else if (code == INTEGER_TYPE
10940 && TREE_TYPE (type) != NULL_TREE
10941 && subrange_type_for_debug_p (type, &low, &high))
10943 mod_type_die = subrange_type_die (type, low, high, context_die);
10944 item_type = TREE_TYPE (type);
10946 else if (is_base_type (type))
10947 mod_type_die = base_type_die (type);
10948 else
10950 gen_type_die (type, context_die);
10952 /* We have to get the type_main_variant here (and pass that to the
10953 `lookup_type_die' routine) because the ..._TYPE node we have
10954 might simply be a *copy* of some original type node (where the
10955 copy was created to help us keep track of typedef names) and
10956 that copy might have a different TYPE_UID from the original
10957 ..._TYPE node. */
10958 if (TREE_CODE (type) != VECTOR_TYPE)
10959 return lookup_type_die (type_main_variant (type));
10960 else
10961 /* Vectors have the debugging information in the type,
10962 not the main variant. */
10963 return lookup_type_die (type);
10966 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10967 don't output a DW_TAG_typedef, since there isn't one in the
10968 user's program; just attach a DW_AT_name to the type.
10969 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10970 if the base type already has the same name. */
10971 if (name
10972 && ((TREE_CODE (name) != TYPE_DECL
10973 && (qualified_type == TYPE_MAIN_VARIANT (type)
10974 || (cv_quals == TYPE_UNQUALIFIED)))
10975 || (TREE_CODE (name) == TYPE_DECL
10976 && TREE_TYPE (name) == qualified_type
10977 && DECL_NAME (name))))
10979 if (TREE_CODE (name) == TYPE_DECL)
10980 /* Could just call add_name_and_src_coords_attributes here,
10981 but since this is a builtin type it doesn't have any
10982 useful source coordinates anyway. */
10983 name = DECL_NAME (name);
10984 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10986 /* This probably indicates a bug. */
10987 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10989 name = TYPE_IDENTIFIER (type);
10990 add_name_attribute (mod_type_die,
10991 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10994 if (qualified_type)
10995 equate_type_number_to_die (qualified_type, mod_type_die);
10997 if (item_type)
10998 /* We must do this after the equate_type_number_to_die call, in case
10999 this is a recursive type. This ensures that the modified_type_die
11000 recursion will terminate even if the type is recursive. Recursive
11001 types are possible in Ada. */
11002 sub_die = modified_type_die (item_type,
11003 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11004 context_die);
11006 if (sub_die != NULL)
11007 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11009 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11010 if (TYPE_ARTIFICIAL (type))
11011 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11013 return mod_type_die;
11016 /* Generate DIEs for the generic parameters of T.
11017 T must be either a generic type or a generic function.
11018 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11020 static void
11021 gen_generic_params_dies (tree t)
11023 tree parms, args;
11024 int parms_num, i;
11025 dw_die_ref die = NULL;
11026 int non_default;
11028 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11029 return;
11031 if (TYPE_P (t))
11032 die = lookup_type_die (t);
11033 else if (DECL_P (t))
11034 die = lookup_decl_die (t);
11036 gcc_assert (die);
11038 parms = lang_hooks.get_innermost_generic_parms (t);
11039 if (!parms)
11040 /* T has no generic parameter. It means T is neither a generic type
11041 or function. End of story. */
11042 return;
11044 parms_num = TREE_VEC_LENGTH (parms);
11045 args = lang_hooks.get_innermost_generic_args (t);
11046 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11047 non_default = int_cst_value (TREE_CHAIN (args));
11048 else
11049 non_default = TREE_VEC_LENGTH (args);
11050 for (i = 0; i < parms_num; i++)
11052 tree parm, arg, arg_pack_elems;
11053 dw_die_ref parm_die;
11055 parm = TREE_VEC_ELT (parms, i);
11056 arg = TREE_VEC_ELT (args, i);
11057 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11058 gcc_assert (parm && TREE_VALUE (parm) && arg);
11060 if (parm && TREE_VALUE (parm) && arg)
11062 /* If PARM represents a template parameter pack,
11063 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11064 by DW_TAG_template_*_parameter DIEs for the argument
11065 pack elements of ARG. Note that ARG would then be
11066 an argument pack. */
11067 if (arg_pack_elems)
11068 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11069 arg_pack_elems,
11070 die);
11071 else
11072 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11073 true /* emit name */, die);
11074 if (i >= non_default)
11075 add_AT_flag (parm_die, DW_AT_default_value, 1);
11080 /* Create and return a DIE for PARM which should be
11081 the representation of a generic type parameter.
11082 For instance, in the C++ front end, PARM would be a template parameter.
11083 ARG is the argument to PARM.
11084 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11085 name of the PARM.
11086 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11087 as a child node. */
11089 static dw_die_ref
11090 generic_parameter_die (tree parm, tree arg,
11091 bool emit_name_p,
11092 dw_die_ref parent_die)
11094 dw_die_ref tmpl_die = NULL;
11095 const char *name = NULL;
11097 if (!parm || !DECL_NAME (parm) || !arg)
11098 return NULL;
11100 /* We support non-type generic parameters and arguments,
11101 type generic parameters and arguments, as well as
11102 generic generic parameters (a.k.a. template template parameters in C++)
11103 and arguments. */
11104 if (TREE_CODE (parm) == PARM_DECL)
11105 /* PARM is a nontype generic parameter */
11106 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11107 else if (TREE_CODE (parm) == TYPE_DECL)
11108 /* PARM is a type generic parameter. */
11109 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11110 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11111 /* PARM is a generic generic parameter.
11112 Its DIE is a GNU extension. It shall have a
11113 DW_AT_name attribute to represent the name of the template template
11114 parameter, and a DW_AT_GNU_template_name attribute to represent the
11115 name of the template template argument. */
11116 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11117 parent_die, parm);
11118 else
11119 gcc_unreachable ();
11121 if (tmpl_die)
11123 tree tmpl_type;
11125 /* If PARM is a generic parameter pack, it means we are
11126 emitting debug info for a template argument pack element.
11127 In other terms, ARG is a template argument pack element.
11128 In that case, we don't emit any DW_AT_name attribute for
11129 the die. */
11130 if (emit_name_p)
11132 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11133 gcc_assert (name);
11134 add_AT_string (tmpl_die, DW_AT_name, name);
11137 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11139 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11140 TMPL_DIE should have a child DW_AT_type attribute that is set
11141 to the type of the argument to PARM, which is ARG.
11142 If PARM is a type generic parameter, TMPL_DIE should have a
11143 child DW_AT_type that is set to ARG. */
11144 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11145 add_type_attribute (tmpl_die, tmpl_type,
11146 (TREE_THIS_VOLATILE (tmpl_type)
11147 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11148 parent_die);
11150 else
11152 /* So TMPL_DIE is a DIE representing a
11153 a generic generic template parameter, a.k.a template template
11154 parameter in C++ and arg is a template. */
11156 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11157 to the name of the argument. */
11158 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11159 if (name)
11160 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11163 if (TREE_CODE (parm) == PARM_DECL)
11164 /* So PARM is a non-type generic parameter.
11165 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11166 attribute of TMPL_DIE which value represents the value
11167 of ARG.
11168 We must be careful here:
11169 The value of ARG might reference some function decls.
11170 We might currently be emitting debug info for a generic
11171 type and types are emitted before function decls, we don't
11172 know if the function decls referenced by ARG will actually be
11173 emitted after cgraph computations.
11174 So must defer the generation of the DW_AT_const_value to
11175 after cgraph is ready. */
11176 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11179 return tmpl_die;
11182 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11183 PARM_PACK must be a template parameter pack. The returned DIE
11184 will be child DIE of PARENT_DIE. */
11186 static dw_die_ref
11187 template_parameter_pack_die (tree parm_pack,
11188 tree parm_pack_args,
11189 dw_die_ref parent_die)
11191 dw_die_ref die;
11192 int j;
11194 gcc_assert (parent_die && parm_pack);
11196 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11197 add_name_and_src_coords_attributes (die, parm_pack);
11198 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11199 generic_parameter_die (parm_pack,
11200 TREE_VEC_ELT (parm_pack_args, j),
11201 false /* Don't emit DW_AT_name */,
11202 die);
11203 return die;
11206 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11207 an enumerated type. */
11209 static inline int
11210 type_is_enum (const_tree type)
11212 return TREE_CODE (type) == ENUMERAL_TYPE;
11215 /* Return the DBX register number described by a given RTL node. */
11217 static unsigned int
11218 dbx_reg_number (const_rtx rtl)
11220 unsigned regno = REGNO (rtl);
11222 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11224 #ifdef LEAF_REG_REMAP
11225 if (crtl->uses_only_leaf_regs)
11227 int leaf_reg = LEAF_REG_REMAP (regno);
11228 if (leaf_reg != -1)
11229 regno = (unsigned) leaf_reg;
11231 #endif
11233 regno = DBX_REGISTER_NUMBER (regno);
11234 gcc_assert (regno != INVALID_REGNUM);
11235 return regno;
11238 /* Optionally add a DW_OP_piece term to a location description expression.
11239 DW_OP_piece is only added if the location description expression already
11240 doesn't end with DW_OP_piece. */
11242 static void
11243 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11245 dw_loc_descr_ref loc;
11247 if (*list_head != NULL)
11249 /* Find the end of the chain. */
11250 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11253 if (loc->dw_loc_opc != DW_OP_piece)
11254 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11258 /* Return a location descriptor that designates a machine register or
11259 zero if there is none. */
11261 static dw_loc_descr_ref
11262 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11264 rtx regs;
11266 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11267 return 0;
11269 /* We only use "frame base" when we're sure we're talking about the
11270 post-prologue local stack frame. We do this by *not* running
11271 register elimination until this point, and recognizing the special
11272 argument pointer and soft frame pointer rtx's.
11273 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11274 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11275 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11277 dw_loc_descr_ref result = NULL;
11279 if (dwarf_version >= 4 || !dwarf_strict)
11281 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11282 initialized);
11283 if (result)
11284 add_loc_descr (&result,
11285 new_loc_descr (DW_OP_stack_value, 0, 0));
11287 return result;
11290 regs = targetm.dwarf_register_span (rtl);
11292 if (REG_NREGS (rtl) > 1 || regs)
11293 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11294 else
11296 unsigned int dbx_regnum = dbx_reg_number (rtl);
11297 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11298 return 0;
11299 return one_reg_loc_descriptor (dbx_regnum, initialized);
11303 /* Return a location descriptor that designates a machine register for
11304 a given hard register number. */
11306 static dw_loc_descr_ref
11307 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11309 dw_loc_descr_ref reg_loc_descr;
11311 if (regno <= 31)
11312 reg_loc_descr
11313 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11314 else
11315 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11317 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11318 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11320 return reg_loc_descr;
11323 /* Given an RTL of a register, return a location descriptor that
11324 designates a value that spans more than one register. */
11326 static dw_loc_descr_ref
11327 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11328 enum var_init_status initialized)
11330 int size, i;
11331 dw_loc_descr_ref loc_result = NULL;
11333 /* Simple, contiguous registers. */
11334 if (regs == NULL_RTX)
11336 unsigned reg = REGNO (rtl);
11337 int nregs;
11339 #ifdef LEAF_REG_REMAP
11340 if (crtl->uses_only_leaf_regs)
11342 int leaf_reg = LEAF_REG_REMAP (reg);
11343 if (leaf_reg != -1)
11344 reg = (unsigned) leaf_reg;
11346 #endif
11348 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11349 nregs = REG_NREGS (rtl);
11351 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11353 loc_result = NULL;
11354 while (nregs--)
11356 dw_loc_descr_ref t;
11358 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11359 VAR_INIT_STATUS_INITIALIZED);
11360 add_loc_descr (&loc_result, t);
11361 add_loc_descr_op_piece (&loc_result, size);
11362 ++reg;
11364 return loc_result;
11367 /* Now onto stupid register sets in non contiguous locations. */
11369 gcc_assert (GET_CODE (regs) == PARALLEL);
11371 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11372 loc_result = NULL;
11374 for (i = 0; i < XVECLEN (regs, 0); ++i)
11376 dw_loc_descr_ref t;
11378 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11379 VAR_INIT_STATUS_INITIALIZED);
11380 add_loc_descr (&loc_result, t);
11381 add_loc_descr_op_piece (&loc_result, size);
11384 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11385 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11386 return loc_result;
11389 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11391 /* Return a location descriptor that designates a constant i,
11392 as a compound operation from constant (i >> shift), constant shift
11393 and DW_OP_shl. */
11395 static dw_loc_descr_ref
11396 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11398 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11399 add_loc_descr (&ret, int_loc_descriptor (shift));
11400 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11401 return ret;
11404 /* Return a location descriptor that designates a constant. */
11406 static dw_loc_descr_ref
11407 int_loc_descriptor (HOST_WIDE_INT i)
11409 enum dwarf_location_atom op;
11411 /* Pick the smallest representation of a constant, rather than just
11412 defaulting to the LEB encoding. */
11413 if (i >= 0)
11415 int clz = clz_hwi (i);
11416 int ctz = ctz_hwi (i);
11417 if (i <= 31)
11418 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11419 else if (i <= 0xff)
11420 op = DW_OP_const1u;
11421 else if (i <= 0xffff)
11422 op = DW_OP_const2u;
11423 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11424 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11425 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11426 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11427 while DW_OP_const4u is 5 bytes. */
11428 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11429 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11430 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11431 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11432 while DW_OP_const4u is 5 bytes. */
11433 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11434 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11435 op = DW_OP_const4u;
11436 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11437 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11438 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11439 while DW_OP_constu of constant >= 0x100000000 takes at least
11440 6 bytes. */
11441 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11442 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11443 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11444 >= HOST_BITS_PER_WIDE_INT)
11445 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11446 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11447 while DW_OP_constu takes in this case at least 6 bytes. */
11448 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11449 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11450 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11451 && size_of_uleb128 (i) > 6)
11452 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11453 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11454 else
11455 op = DW_OP_constu;
11457 else
11459 if (i >= -0x80)
11460 op = DW_OP_const1s;
11461 else if (i >= -0x8000)
11462 op = DW_OP_const2s;
11463 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11465 if (size_of_int_loc_descriptor (i) < 5)
11467 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11468 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11469 return ret;
11471 op = DW_OP_const4s;
11473 else
11475 if (size_of_int_loc_descriptor (i)
11476 < (unsigned long) 1 + size_of_sleb128 (i))
11478 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11479 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11480 return ret;
11482 op = DW_OP_consts;
11486 return new_loc_descr (op, i, 0);
11489 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11490 without actually allocating it. */
11492 static unsigned long
11493 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11495 return size_of_int_loc_descriptor (i >> shift)
11496 + size_of_int_loc_descriptor (shift)
11497 + 1;
11500 /* Return size_of_locs (int_loc_descriptor (i)) without
11501 actually allocating it. */
11503 static unsigned long
11504 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11506 unsigned long s;
11508 if (i >= 0)
11510 int clz, ctz;
11511 if (i <= 31)
11512 return 1;
11513 else if (i <= 0xff)
11514 return 2;
11515 else if (i <= 0xffff)
11516 return 3;
11517 clz = clz_hwi (i);
11518 ctz = ctz_hwi (i);
11519 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11520 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11521 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11522 - clz - 5);
11523 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11524 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11525 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11526 - clz - 8);
11527 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11528 return 5;
11529 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11530 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11531 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11532 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11533 - clz - 8);
11534 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11535 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11536 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11537 - clz - 16);
11538 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11539 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11540 && s > 6)
11541 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11542 - clz - 32);
11543 else
11544 return 1 + s;
11546 else
11548 if (i >= -0x80)
11549 return 2;
11550 else if (i >= -0x8000)
11551 return 3;
11552 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11554 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11556 s = size_of_int_loc_descriptor (-i) + 1;
11557 if (s < 5)
11558 return s;
11560 return 5;
11562 else
11564 unsigned long r = 1 + size_of_sleb128 (i);
11565 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11567 s = size_of_int_loc_descriptor (-i) + 1;
11568 if (s < r)
11569 return s;
11571 return r;
11576 /* Return loc description representing "address" of integer value.
11577 This can appear only as toplevel expression. */
11579 static dw_loc_descr_ref
11580 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11582 int litsize;
11583 dw_loc_descr_ref loc_result = NULL;
11585 if (!(dwarf_version >= 4 || !dwarf_strict))
11586 return NULL;
11588 litsize = size_of_int_loc_descriptor (i);
11589 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11590 is more compact. For DW_OP_stack_value we need:
11591 litsize + 1 (DW_OP_stack_value)
11592 and for DW_OP_implicit_value:
11593 1 (DW_OP_implicit_value) + 1 (length) + size. */
11594 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11596 loc_result = int_loc_descriptor (i);
11597 add_loc_descr (&loc_result,
11598 new_loc_descr (DW_OP_stack_value, 0, 0));
11599 return loc_result;
11602 loc_result = new_loc_descr (DW_OP_implicit_value,
11603 size, 0);
11604 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11605 loc_result->dw_loc_oprnd2.v.val_int = i;
11606 return loc_result;
11609 /* Return a location descriptor that designates a base+offset location. */
11611 static dw_loc_descr_ref
11612 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11613 enum var_init_status initialized)
11615 unsigned int regno;
11616 dw_loc_descr_ref result;
11617 dw_fde_ref fde = cfun->fde;
11619 /* We only use "frame base" when we're sure we're talking about the
11620 post-prologue local stack frame. We do this by *not* running
11621 register elimination until this point, and recognizing the special
11622 argument pointer and soft frame pointer rtx's. */
11623 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11625 rtx elim = (ira_use_lra_p
11626 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11627 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11629 if (elim != reg)
11631 if (GET_CODE (elim) == PLUS)
11633 offset += INTVAL (XEXP (elim, 1));
11634 elim = XEXP (elim, 0);
11636 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11637 && (elim == hard_frame_pointer_rtx
11638 || elim == stack_pointer_rtx))
11639 || elim == (frame_pointer_needed
11640 ? hard_frame_pointer_rtx
11641 : stack_pointer_rtx));
11643 /* If drap register is used to align stack, use frame
11644 pointer + offset to access stack variables. If stack
11645 is aligned without drap, use stack pointer + offset to
11646 access stack variables. */
11647 if (crtl->stack_realign_tried
11648 && reg == frame_pointer_rtx)
11650 int base_reg
11651 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11652 ? HARD_FRAME_POINTER_REGNUM
11653 : REGNO (elim));
11654 return new_reg_loc_descr (base_reg, offset);
11657 gcc_assert (frame_pointer_fb_offset_valid);
11658 offset += frame_pointer_fb_offset;
11659 return new_loc_descr (DW_OP_fbreg, offset, 0);
11663 regno = REGNO (reg);
11664 #ifdef LEAF_REG_REMAP
11665 if (crtl->uses_only_leaf_regs)
11667 int leaf_reg = LEAF_REG_REMAP (regno);
11668 if (leaf_reg != -1)
11669 regno = (unsigned) leaf_reg;
11671 #endif
11672 regno = DWARF_FRAME_REGNUM (regno);
11674 if (!optimize && fde
11675 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11677 /* Use cfa+offset to represent the location of arguments passed
11678 on the stack when drap is used to align stack.
11679 Only do this when not optimizing, for optimized code var-tracking
11680 is supposed to track where the arguments live and the register
11681 used as vdrap or drap in some spot might be used for something
11682 else in other part of the routine. */
11683 return new_loc_descr (DW_OP_fbreg, offset, 0);
11686 if (regno <= 31)
11687 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11688 offset, 0);
11689 else
11690 result = new_loc_descr (DW_OP_bregx, regno, offset);
11692 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11693 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11695 return result;
11698 /* Return true if this RTL expression describes a base+offset calculation. */
11700 static inline int
11701 is_based_loc (const_rtx rtl)
11703 return (GET_CODE (rtl) == PLUS
11704 && ((REG_P (XEXP (rtl, 0))
11705 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11706 && CONST_INT_P (XEXP (rtl, 1)))));
11709 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11710 failed. */
11712 static dw_loc_descr_ref
11713 tls_mem_loc_descriptor (rtx mem)
11715 tree base;
11716 dw_loc_descr_ref loc_result;
11718 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11719 return NULL;
11721 base = get_base_address (MEM_EXPR (mem));
11722 if (base == NULL
11723 || TREE_CODE (base) != VAR_DECL
11724 || !DECL_THREAD_LOCAL_P (base))
11725 return NULL;
11727 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11728 if (loc_result == NULL)
11729 return NULL;
11731 if (MEM_OFFSET (mem))
11732 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11734 return loc_result;
11737 /* Output debug info about reason why we failed to expand expression as dwarf
11738 expression. */
11740 static void
11741 expansion_failed (tree expr, rtx rtl, char const *reason)
11743 if (dump_file && (dump_flags & TDF_DETAILS))
11745 fprintf (dump_file, "Failed to expand as dwarf: ");
11746 if (expr)
11747 print_generic_expr (dump_file, expr, dump_flags);
11748 if (rtl)
11750 fprintf (dump_file, "\n");
11751 print_rtl (dump_file, rtl);
11753 fprintf (dump_file, "\nReason: %s\n", reason);
11757 /* Helper function for const_ok_for_output. */
11759 static bool
11760 const_ok_for_output_1 (rtx rtl)
11762 if (GET_CODE (rtl) == UNSPEC)
11764 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11765 we can't express it in the debug info. */
11766 #ifdef ENABLE_CHECKING
11767 /* Don't complain about TLS UNSPECs, those are just too hard to
11768 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11769 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11770 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11771 if (XVECLEN (rtl, 0) == 0
11772 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11773 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11774 inform (current_function_decl
11775 ? DECL_SOURCE_LOCATION (current_function_decl)
11776 : UNKNOWN_LOCATION,
11777 #if NUM_UNSPEC_VALUES > 0
11778 "non-delegitimized UNSPEC %s (%d) found in variable location",
11779 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11780 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11781 XINT (rtl, 1));
11782 #else
11783 "non-delegitimized UNSPEC %d found in variable location",
11784 XINT (rtl, 1));
11785 #endif
11786 #endif
11787 expansion_failed (NULL_TREE, rtl,
11788 "UNSPEC hasn't been delegitimized.\n");
11789 return false;
11792 if (targetm.const_not_ok_for_debug_p (rtl))
11794 expansion_failed (NULL_TREE, rtl,
11795 "Expression rejected for debug by the backend.\n");
11796 return false;
11799 /* FIXME: Refer to PR60655. It is possible for simplification
11800 of rtl expressions in var tracking to produce such expressions.
11801 We should really identify / validate expressions
11802 enclosed in CONST that can be handled by assemblers on various
11803 targets and only handle legitimate cases here. */
11804 if (GET_CODE (rtl) != SYMBOL_REF)
11806 if (GET_CODE (rtl) == NOT)
11807 return false;
11808 return true;
11811 if (CONSTANT_POOL_ADDRESS_P (rtl))
11813 bool marked;
11814 get_pool_constant_mark (rtl, &marked);
11815 /* If all references to this pool constant were optimized away,
11816 it was not output and thus we can't represent it. */
11817 if (!marked)
11819 expansion_failed (NULL_TREE, rtl,
11820 "Constant was removed from constant pool.\n");
11821 return false;
11825 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11826 return false;
11828 /* Avoid references to external symbols in debug info, on several targets
11829 the linker might even refuse to link when linking a shared library,
11830 and in many other cases the relocations for .debug_info/.debug_loc are
11831 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11832 to be defined within the same shared library or executable are fine. */
11833 if (SYMBOL_REF_EXTERNAL_P (rtl))
11835 tree decl = SYMBOL_REF_DECL (rtl);
11837 if (decl == NULL || !targetm.binds_local_p (decl))
11839 expansion_failed (NULL_TREE, rtl,
11840 "Symbol not defined in current TU.\n");
11841 return false;
11845 return true;
11848 /* Return true if constant RTL can be emitted in DW_OP_addr or
11849 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11850 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11852 static bool
11853 const_ok_for_output (rtx rtl)
11855 if (GET_CODE (rtl) == SYMBOL_REF)
11856 return const_ok_for_output_1 (rtl);
11858 if (GET_CODE (rtl) == CONST)
11860 subrtx_var_iterator::array_type array;
11861 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11862 if (!const_ok_for_output_1 (*iter))
11863 return false;
11864 return true;
11867 return true;
11870 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11871 if possible, NULL otherwise. */
11873 static dw_die_ref
11874 base_type_for_mode (machine_mode mode, bool unsignedp)
11876 dw_die_ref type_die;
11877 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11879 if (type == NULL)
11880 return NULL;
11881 switch (TREE_CODE (type))
11883 case INTEGER_TYPE:
11884 case REAL_TYPE:
11885 break;
11886 default:
11887 return NULL;
11889 type_die = lookup_type_die (type);
11890 if (!type_die)
11891 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11892 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11893 return NULL;
11894 return type_die;
11897 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11898 type matching MODE, or, if MODE is narrower than or as wide as
11899 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11900 possible. */
11902 static dw_loc_descr_ref
11903 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11905 machine_mode outer_mode = mode;
11906 dw_die_ref type_die;
11907 dw_loc_descr_ref cvt;
11909 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11911 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11912 return op;
11914 type_die = base_type_for_mode (outer_mode, 1);
11915 if (type_die == NULL)
11916 return NULL;
11917 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11918 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11919 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11920 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11921 add_loc_descr (&op, cvt);
11922 return op;
11925 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11927 static dw_loc_descr_ref
11928 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11929 dw_loc_descr_ref op1)
11931 dw_loc_descr_ref ret = op0;
11932 add_loc_descr (&ret, op1);
11933 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11934 if (STORE_FLAG_VALUE != 1)
11936 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11937 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11939 return ret;
11942 /* Return location descriptor for signed comparison OP RTL. */
11944 static dw_loc_descr_ref
11945 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11946 machine_mode mem_mode)
11948 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11949 dw_loc_descr_ref op0, op1;
11950 int shift;
11952 if (op_mode == VOIDmode)
11953 op_mode = GET_MODE (XEXP (rtl, 1));
11954 if (op_mode == VOIDmode)
11955 return NULL;
11957 if (dwarf_strict
11958 && (GET_MODE_CLASS (op_mode) != MODE_INT
11959 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11960 return NULL;
11962 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11963 VAR_INIT_STATUS_INITIALIZED);
11964 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11965 VAR_INIT_STATUS_INITIALIZED);
11967 if (op0 == NULL || op1 == NULL)
11968 return NULL;
11970 if (GET_MODE_CLASS (op_mode) != MODE_INT
11971 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11972 return compare_loc_descriptor (op, op0, op1);
11974 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11976 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11977 dw_loc_descr_ref cvt;
11979 if (type_die == NULL)
11980 return NULL;
11981 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11982 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11983 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11984 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11985 add_loc_descr (&op0, cvt);
11986 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11987 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11988 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11989 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11990 add_loc_descr (&op1, cvt);
11991 return compare_loc_descriptor (op, op0, op1);
11994 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11995 /* For eq/ne, if the operands are known to be zero-extended,
11996 there is no need to do the fancy shifting up. */
11997 if (op == DW_OP_eq || op == DW_OP_ne)
11999 dw_loc_descr_ref last0, last1;
12000 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12002 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12004 /* deref_size zero extends, and for constants we can check
12005 whether they are zero extended or not. */
12006 if (((last0->dw_loc_opc == DW_OP_deref_size
12007 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12008 || (CONST_INT_P (XEXP (rtl, 0))
12009 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12010 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12011 && ((last1->dw_loc_opc == DW_OP_deref_size
12012 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12013 || (CONST_INT_P (XEXP (rtl, 1))
12014 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12015 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12016 return compare_loc_descriptor (op, op0, op1);
12018 /* EQ/NE comparison against constant in narrower type than
12019 DWARF2_ADDR_SIZE can be performed either as
12020 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12021 DW_OP_{eq,ne}
12023 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12024 DW_OP_{eq,ne}. Pick whatever is shorter. */
12025 if (CONST_INT_P (XEXP (rtl, 1))
12026 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12027 && (size_of_int_loc_descriptor (shift) + 1
12028 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12029 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12030 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12031 & GET_MODE_MASK (op_mode))))
12033 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12034 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12035 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12036 & GET_MODE_MASK (op_mode));
12037 return compare_loc_descriptor (op, op0, op1);
12040 add_loc_descr (&op0, int_loc_descriptor (shift));
12041 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12042 if (CONST_INT_P (XEXP (rtl, 1)))
12043 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12044 else
12046 add_loc_descr (&op1, int_loc_descriptor (shift));
12047 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12049 return compare_loc_descriptor (op, op0, op1);
12052 /* Return location descriptor for unsigned comparison OP RTL. */
12054 static dw_loc_descr_ref
12055 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12056 machine_mode mem_mode)
12058 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12059 dw_loc_descr_ref op0, op1;
12061 if (op_mode == VOIDmode)
12062 op_mode = GET_MODE (XEXP (rtl, 1));
12063 if (op_mode == VOIDmode)
12064 return NULL;
12065 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12066 return NULL;
12068 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12069 return NULL;
12071 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12072 VAR_INIT_STATUS_INITIALIZED);
12073 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12074 VAR_INIT_STATUS_INITIALIZED);
12076 if (op0 == NULL || op1 == NULL)
12077 return NULL;
12079 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12081 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12082 dw_loc_descr_ref last0, last1;
12083 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12085 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12087 if (CONST_INT_P (XEXP (rtl, 0)))
12088 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12089 /* deref_size zero extends, so no need to mask it again. */
12090 else if (last0->dw_loc_opc != DW_OP_deref_size
12091 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12093 add_loc_descr (&op0, int_loc_descriptor (mask));
12094 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12096 if (CONST_INT_P (XEXP (rtl, 1)))
12097 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12098 /* deref_size zero extends, so no need to mask it again. */
12099 else if (last1->dw_loc_opc != DW_OP_deref_size
12100 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12102 add_loc_descr (&op1, int_loc_descriptor (mask));
12103 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12106 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12108 HOST_WIDE_INT bias = 1;
12109 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12110 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12111 if (CONST_INT_P (XEXP (rtl, 1)))
12112 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12113 + INTVAL (XEXP (rtl, 1)));
12114 else
12115 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12116 bias, 0));
12118 return compare_loc_descriptor (op, op0, op1);
12121 /* Return location descriptor for {U,S}{MIN,MAX}. */
12123 static dw_loc_descr_ref
12124 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12125 machine_mode mem_mode)
12127 enum dwarf_location_atom op;
12128 dw_loc_descr_ref op0, op1, ret;
12129 dw_loc_descr_ref bra_node, drop_node;
12131 if (dwarf_strict
12132 && (GET_MODE_CLASS (mode) != MODE_INT
12133 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12134 return NULL;
12136 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12137 VAR_INIT_STATUS_INITIALIZED);
12138 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12139 VAR_INIT_STATUS_INITIALIZED);
12141 if (op0 == NULL || op1 == NULL)
12142 return NULL;
12144 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12145 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12146 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12147 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12149 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12151 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12152 add_loc_descr (&op0, int_loc_descriptor (mask));
12153 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12154 add_loc_descr (&op1, int_loc_descriptor (mask));
12155 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12157 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12159 HOST_WIDE_INT bias = 1;
12160 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12161 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12162 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12165 else if (GET_MODE_CLASS (mode) == MODE_INT
12166 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12168 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12169 add_loc_descr (&op0, int_loc_descriptor (shift));
12170 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12171 add_loc_descr (&op1, int_loc_descriptor (shift));
12172 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12174 else if (GET_MODE_CLASS (mode) == MODE_INT
12175 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12177 dw_die_ref type_die = base_type_for_mode (mode, 0);
12178 dw_loc_descr_ref cvt;
12179 if (type_die == NULL)
12180 return NULL;
12181 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12182 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12183 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12184 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12185 add_loc_descr (&op0, cvt);
12186 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12187 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12188 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12189 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12190 add_loc_descr (&op1, cvt);
12193 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12194 op = DW_OP_lt;
12195 else
12196 op = DW_OP_gt;
12197 ret = op0;
12198 add_loc_descr (&ret, op1);
12199 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12200 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12201 add_loc_descr (&ret, bra_node);
12202 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12203 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12204 add_loc_descr (&ret, drop_node);
12205 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12206 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12207 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12208 && GET_MODE_CLASS (mode) == MODE_INT
12209 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12210 ret = convert_descriptor_to_mode (mode, ret);
12211 return ret;
12214 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12215 but after converting arguments to type_die, afterwards
12216 convert back to unsigned. */
12218 static dw_loc_descr_ref
12219 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12220 machine_mode mode, machine_mode mem_mode)
12222 dw_loc_descr_ref cvt, op0, op1;
12224 if (type_die == NULL)
12225 return NULL;
12226 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12227 VAR_INIT_STATUS_INITIALIZED);
12228 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12229 VAR_INIT_STATUS_INITIALIZED);
12230 if (op0 == NULL || op1 == NULL)
12231 return NULL;
12232 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12233 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12234 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12235 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12236 add_loc_descr (&op0, cvt);
12237 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12238 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12239 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12240 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12241 add_loc_descr (&op1, cvt);
12242 add_loc_descr (&op0, op1);
12243 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12244 return convert_descriptor_to_mode (mode, op0);
12247 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12248 const0 is DW_OP_lit0 or corresponding typed constant,
12249 const1 is DW_OP_lit1 or corresponding typed constant
12250 and constMSB is constant with just the MSB bit set
12251 for the mode):
12252 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12253 L1: const0 DW_OP_swap
12254 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12255 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12256 L3: DW_OP_drop
12257 L4: DW_OP_nop
12259 CTZ is similar:
12260 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12261 L1: const0 DW_OP_swap
12262 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12263 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12264 L3: DW_OP_drop
12265 L4: DW_OP_nop
12267 FFS is similar:
12268 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12269 L1: const1 DW_OP_swap
12270 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12271 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12272 L3: DW_OP_drop
12273 L4: DW_OP_nop */
12275 static dw_loc_descr_ref
12276 clz_loc_descriptor (rtx rtl, machine_mode mode,
12277 machine_mode mem_mode)
12279 dw_loc_descr_ref op0, ret, tmp;
12280 HOST_WIDE_INT valv;
12281 dw_loc_descr_ref l1jump, l1label;
12282 dw_loc_descr_ref l2jump, l2label;
12283 dw_loc_descr_ref l3jump, l3label;
12284 dw_loc_descr_ref l4jump, l4label;
12285 rtx msb;
12287 if (GET_MODE_CLASS (mode) != MODE_INT
12288 || GET_MODE (XEXP (rtl, 0)) != mode)
12289 return NULL;
12291 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12292 VAR_INIT_STATUS_INITIALIZED);
12293 if (op0 == NULL)
12294 return NULL;
12295 ret = op0;
12296 if (GET_CODE (rtl) == CLZ)
12298 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12299 valv = GET_MODE_BITSIZE (mode);
12301 else if (GET_CODE (rtl) == FFS)
12302 valv = 0;
12303 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12304 valv = GET_MODE_BITSIZE (mode);
12305 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12306 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12307 add_loc_descr (&ret, l1jump);
12308 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12309 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12310 VAR_INIT_STATUS_INITIALIZED);
12311 if (tmp == NULL)
12312 return NULL;
12313 add_loc_descr (&ret, tmp);
12314 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12315 add_loc_descr (&ret, l4jump);
12316 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12317 ? const1_rtx : const0_rtx,
12318 mode, mem_mode,
12319 VAR_INIT_STATUS_INITIALIZED);
12320 if (l1label == NULL)
12321 return NULL;
12322 add_loc_descr (&ret, l1label);
12323 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12324 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12325 add_loc_descr (&ret, l2label);
12326 if (GET_CODE (rtl) != CLZ)
12327 msb = const1_rtx;
12328 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12329 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12330 << (GET_MODE_BITSIZE (mode) - 1));
12331 else
12332 msb = immed_wide_int_const
12333 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12334 GET_MODE_PRECISION (mode)), mode);
12335 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12336 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12337 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12338 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12339 else
12340 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12341 VAR_INIT_STATUS_INITIALIZED);
12342 if (tmp == NULL)
12343 return NULL;
12344 add_loc_descr (&ret, tmp);
12345 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12346 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12347 add_loc_descr (&ret, l3jump);
12348 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12349 VAR_INIT_STATUS_INITIALIZED);
12350 if (tmp == NULL)
12351 return NULL;
12352 add_loc_descr (&ret, tmp);
12353 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12354 ? DW_OP_shl : DW_OP_shr, 0, 0));
12355 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12356 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12357 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12358 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12359 add_loc_descr (&ret, l2jump);
12360 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12361 add_loc_descr (&ret, l3label);
12362 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12363 add_loc_descr (&ret, l4label);
12364 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12365 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12366 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12367 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12368 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12369 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12370 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12371 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12372 return ret;
12375 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12376 const1 is DW_OP_lit1 or corresponding typed constant):
12377 const0 DW_OP_swap
12378 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12379 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12380 L2: DW_OP_drop
12382 PARITY is similar:
12383 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12384 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12385 L2: DW_OP_drop */
12387 static dw_loc_descr_ref
12388 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12389 machine_mode mem_mode)
12391 dw_loc_descr_ref op0, ret, tmp;
12392 dw_loc_descr_ref l1jump, l1label;
12393 dw_loc_descr_ref l2jump, l2label;
12395 if (GET_MODE_CLASS (mode) != MODE_INT
12396 || GET_MODE (XEXP (rtl, 0)) != mode)
12397 return NULL;
12399 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12400 VAR_INIT_STATUS_INITIALIZED);
12401 if (op0 == NULL)
12402 return NULL;
12403 ret = op0;
12404 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12405 VAR_INIT_STATUS_INITIALIZED);
12406 if (tmp == NULL)
12407 return NULL;
12408 add_loc_descr (&ret, tmp);
12409 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12410 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12411 add_loc_descr (&ret, l1label);
12412 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12413 add_loc_descr (&ret, l2jump);
12414 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12415 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12416 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12417 VAR_INIT_STATUS_INITIALIZED);
12418 if (tmp == NULL)
12419 return NULL;
12420 add_loc_descr (&ret, tmp);
12421 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12422 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12423 ? DW_OP_plus : DW_OP_xor, 0, 0));
12424 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12425 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12426 VAR_INIT_STATUS_INITIALIZED);
12427 add_loc_descr (&ret, tmp);
12428 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12429 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12430 add_loc_descr (&ret, l1jump);
12431 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12432 add_loc_descr (&ret, l2label);
12433 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12434 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12435 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12436 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12437 return ret;
12440 /* BSWAP (constS is initial shift count, either 56 or 24):
12441 constS const0
12442 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12443 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12444 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12445 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12446 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12448 static dw_loc_descr_ref
12449 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12450 machine_mode mem_mode)
12452 dw_loc_descr_ref op0, ret, tmp;
12453 dw_loc_descr_ref l1jump, l1label;
12454 dw_loc_descr_ref l2jump, l2label;
12456 if (GET_MODE_CLASS (mode) != MODE_INT
12457 || BITS_PER_UNIT != 8
12458 || (GET_MODE_BITSIZE (mode) != 32
12459 && GET_MODE_BITSIZE (mode) != 64))
12460 return NULL;
12462 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12463 VAR_INIT_STATUS_INITIALIZED);
12464 if (op0 == NULL)
12465 return NULL;
12467 ret = op0;
12468 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12469 mode, mem_mode,
12470 VAR_INIT_STATUS_INITIALIZED);
12471 if (tmp == NULL)
12472 return NULL;
12473 add_loc_descr (&ret, tmp);
12474 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12475 VAR_INIT_STATUS_INITIALIZED);
12476 if (tmp == NULL)
12477 return NULL;
12478 add_loc_descr (&ret, tmp);
12479 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12480 add_loc_descr (&ret, l1label);
12481 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12482 mode, mem_mode,
12483 VAR_INIT_STATUS_INITIALIZED);
12484 add_loc_descr (&ret, tmp);
12485 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12486 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12487 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12488 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12489 VAR_INIT_STATUS_INITIALIZED);
12490 if (tmp == NULL)
12491 return NULL;
12492 add_loc_descr (&ret, tmp);
12493 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12494 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12495 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12496 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12497 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12498 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12499 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12500 VAR_INIT_STATUS_INITIALIZED);
12501 add_loc_descr (&ret, tmp);
12502 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12503 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12504 add_loc_descr (&ret, l2jump);
12505 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12506 VAR_INIT_STATUS_INITIALIZED);
12507 add_loc_descr (&ret, tmp);
12508 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12509 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12510 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12511 add_loc_descr (&ret, l1jump);
12512 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12513 add_loc_descr (&ret, l2label);
12514 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12515 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12516 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12517 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12518 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12519 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12520 return ret;
12523 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12524 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12525 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12526 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12528 ROTATERT is similar:
12529 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12530 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12531 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12533 static dw_loc_descr_ref
12534 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12535 machine_mode mem_mode)
12537 rtx rtlop1 = XEXP (rtl, 1);
12538 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12539 int i;
12541 if (GET_MODE_CLASS (mode) != MODE_INT)
12542 return NULL;
12544 if (GET_MODE (rtlop1) != VOIDmode
12545 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12546 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12547 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12548 VAR_INIT_STATUS_INITIALIZED);
12549 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12550 VAR_INIT_STATUS_INITIALIZED);
12551 if (op0 == NULL || op1 == NULL)
12552 return NULL;
12553 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12554 for (i = 0; i < 2; i++)
12556 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12557 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12558 mode, mem_mode,
12559 VAR_INIT_STATUS_INITIALIZED);
12560 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12561 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12562 ? DW_OP_const4u
12563 : HOST_BITS_PER_WIDE_INT == 64
12564 ? DW_OP_const8u : DW_OP_constu,
12565 GET_MODE_MASK (mode), 0);
12566 else
12567 mask[i] = NULL;
12568 if (mask[i] == NULL)
12569 return NULL;
12570 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12572 ret = op0;
12573 add_loc_descr (&ret, op1);
12574 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12575 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12576 if (GET_CODE (rtl) == ROTATERT)
12578 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12579 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12580 GET_MODE_BITSIZE (mode), 0));
12582 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12583 if (mask[0] != NULL)
12584 add_loc_descr (&ret, mask[0]);
12585 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12586 if (mask[1] != NULL)
12588 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12589 add_loc_descr (&ret, mask[1]);
12590 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12592 if (GET_CODE (rtl) == ROTATE)
12594 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12595 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12596 GET_MODE_BITSIZE (mode), 0));
12598 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12599 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12600 return ret;
12603 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12604 for DEBUG_PARAMETER_REF RTL. */
12606 static dw_loc_descr_ref
12607 parameter_ref_descriptor (rtx rtl)
12609 dw_loc_descr_ref ret;
12610 dw_die_ref ref;
12612 if (dwarf_strict)
12613 return NULL;
12614 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12615 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12616 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12617 if (ref)
12619 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12620 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12621 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12623 else
12625 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12626 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12628 return ret;
12631 /* The following routine converts the RTL for a variable or parameter
12632 (resident in memory) into an equivalent Dwarf representation of a
12633 mechanism for getting the address of that same variable onto the top of a
12634 hypothetical "address evaluation" stack.
12636 When creating memory location descriptors, we are effectively transforming
12637 the RTL for a memory-resident object into its Dwarf postfix expression
12638 equivalent. This routine recursively descends an RTL tree, turning
12639 it into Dwarf postfix code as it goes.
12641 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12643 MEM_MODE is the mode of the memory reference, needed to handle some
12644 autoincrement addressing modes.
12646 Return 0 if we can't represent the location. */
12648 dw_loc_descr_ref
12649 mem_loc_descriptor (rtx rtl, machine_mode mode,
12650 machine_mode mem_mode,
12651 enum var_init_status initialized)
12653 dw_loc_descr_ref mem_loc_result = NULL;
12654 enum dwarf_location_atom op;
12655 dw_loc_descr_ref op0, op1;
12656 rtx inner = NULL_RTX;
12658 if (mode == VOIDmode)
12659 mode = GET_MODE (rtl);
12661 /* Note that for a dynamically sized array, the location we will generate a
12662 description of here will be the lowest numbered location which is
12663 actually within the array. That's *not* necessarily the same as the
12664 zeroth element of the array. */
12666 rtl = targetm.delegitimize_address (rtl);
12668 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12669 return NULL;
12671 switch (GET_CODE (rtl))
12673 case POST_INC:
12674 case POST_DEC:
12675 case POST_MODIFY:
12676 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12678 case SUBREG:
12679 /* The case of a subreg may arise when we have a local (register)
12680 variable or a formal (register) parameter which doesn't quite fill
12681 up an entire register. For now, just assume that it is
12682 legitimate to make the Dwarf info refer to the whole register which
12683 contains the given subreg. */
12684 if (!subreg_lowpart_p (rtl))
12685 break;
12686 inner = SUBREG_REG (rtl);
12687 case TRUNCATE:
12688 if (inner == NULL_RTX)
12689 inner = XEXP (rtl, 0);
12690 if (GET_MODE_CLASS (mode) == MODE_INT
12691 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12692 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12693 #ifdef POINTERS_EXTEND_UNSIGNED
12694 || (mode == Pmode && mem_mode != VOIDmode)
12695 #endif
12697 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12699 mem_loc_result = mem_loc_descriptor (inner,
12700 GET_MODE (inner),
12701 mem_mode, initialized);
12702 break;
12704 if (dwarf_strict)
12705 break;
12706 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12707 break;
12708 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12709 && (GET_MODE_CLASS (mode) != MODE_INT
12710 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12711 break;
12712 else
12714 dw_die_ref type_die;
12715 dw_loc_descr_ref cvt;
12717 mem_loc_result = mem_loc_descriptor (inner,
12718 GET_MODE (inner),
12719 mem_mode, initialized);
12720 if (mem_loc_result == NULL)
12721 break;
12722 type_die = base_type_for_mode (mode,
12723 GET_MODE_CLASS (mode) == MODE_INT);
12724 if (type_die == NULL)
12726 mem_loc_result = NULL;
12727 break;
12729 if (GET_MODE_SIZE (mode)
12730 != GET_MODE_SIZE (GET_MODE (inner)))
12731 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12732 else
12733 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12734 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12735 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12736 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12737 add_loc_descr (&mem_loc_result, cvt);
12739 break;
12741 case REG:
12742 if (GET_MODE_CLASS (mode) != MODE_INT
12743 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12744 && rtl != arg_pointer_rtx
12745 && rtl != frame_pointer_rtx
12746 #ifdef POINTERS_EXTEND_UNSIGNED
12747 && (mode != Pmode || mem_mode == VOIDmode)
12748 #endif
12751 dw_die_ref type_die;
12752 unsigned int dbx_regnum;
12754 if (dwarf_strict)
12755 break;
12756 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12757 break;
12758 type_die = base_type_for_mode (mode,
12759 GET_MODE_CLASS (mode) == MODE_INT);
12760 if (type_die == NULL)
12761 break;
12763 dbx_regnum = dbx_reg_number (rtl);
12764 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12765 break;
12766 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12767 dbx_regnum, 0);
12768 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12769 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12770 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12771 break;
12773 /* Whenever a register number forms a part of the description of the
12774 method for calculating the (dynamic) address of a memory resident
12775 object, DWARF rules require the register number be referred to as
12776 a "base register". This distinction is not based in any way upon
12777 what category of register the hardware believes the given register
12778 belongs to. This is strictly DWARF terminology we're dealing with
12779 here. Note that in cases where the location of a memory-resident
12780 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12781 OP_CONST (0)) the actual DWARF location descriptor that we generate
12782 may just be OP_BASEREG (basereg). This may look deceptively like
12783 the object in question was allocated to a register (rather than in
12784 memory) so DWARF consumers need to be aware of the subtle
12785 distinction between OP_REG and OP_BASEREG. */
12786 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12787 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12788 else if (stack_realign_drap
12789 && crtl->drap_reg
12790 && crtl->args.internal_arg_pointer == rtl
12791 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12793 /* If RTL is internal_arg_pointer, which has been optimized
12794 out, use DRAP instead. */
12795 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12796 VAR_INIT_STATUS_INITIALIZED);
12798 break;
12800 case SIGN_EXTEND:
12801 case ZERO_EXTEND:
12802 if (GET_MODE_CLASS (mode) != MODE_INT)
12803 break;
12804 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12805 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12806 if (op0 == 0)
12807 break;
12808 else if (GET_CODE (rtl) == ZERO_EXTEND
12809 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12810 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12811 < HOST_BITS_PER_WIDE_INT
12812 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12813 to expand zero extend as two shifts instead of
12814 masking. */
12815 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12817 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12818 mem_loc_result = op0;
12819 add_loc_descr (&mem_loc_result,
12820 int_loc_descriptor (GET_MODE_MASK (imode)));
12821 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12823 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12825 int shift = DWARF2_ADDR_SIZE
12826 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12827 shift *= BITS_PER_UNIT;
12828 if (GET_CODE (rtl) == SIGN_EXTEND)
12829 op = DW_OP_shra;
12830 else
12831 op = DW_OP_shr;
12832 mem_loc_result = op0;
12833 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12834 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12835 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12836 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12838 else if (!dwarf_strict)
12840 dw_die_ref type_die1, type_die2;
12841 dw_loc_descr_ref cvt;
12843 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12844 GET_CODE (rtl) == ZERO_EXTEND);
12845 if (type_die1 == NULL)
12846 break;
12847 type_die2 = base_type_for_mode (mode, 1);
12848 if (type_die2 == NULL)
12849 break;
12850 mem_loc_result = op0;
12851 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12852 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12853 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12854 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12855 add_loc_descr (&mem_loc_result, cvt);
12856 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12857 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12858 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12859 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12860 add_loc_descr (&mem_loc_result, cvt);
12862 break;
12864 case MEM:
12866 rtx new_rtl = avoid_constant_pool_reference (rtl);
12867 if (new_rtl != rtl)
12869 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12870 initialized);
12871 if (mem_loc_result != NULL)
12872 return mem_loc_result;
12875 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12876 get_address_mode (rtl), mode,
12877 VAR_INIT_STATUS_INITIALIZED);
12878 if (mem_loc_result == NULL)
12879 mem_loc_result = tls_mem_loc_descriptor (rtl);
12880 if (mem_loc_result != NULL)
12882 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12883 || GET_MODE_CLASS (mode) != MODE_INT)
12885 dw_die_ref type_die;
12886 dw_loc_descr_ref deref;
12888 if (dwarf_strict)
12889 return NULL;
12890 type_die
12891 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12892 if (type_die == NULL)
12893 return NULL;
12894 deref = new_loc_descr (DW_OP_GNU_deref_type,
12895 GET_MODE_SIZE (mode), 0);
12896 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12897 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12898 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12899 add_loc_descr (&mem_loc_result, deref);
12901 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12902 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12903 else
12904 add_loc_descr (&mem_loc_result,
12905 new_loc_descr (DW_OP_deref_size,
12906 GET_MODE_SIZE (mode), 0));
12908 break;
12910 case LO_SUM:
12911 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12913 case LABEL_REF:
12914 /* Some ports can transform a symbol ref into a label ref, because
12915 the symbol ref is too far away and has to be dumped into a constant
12916 pool. */
12917 case CONST:
12918 case SYMBOL_REF:
12919 if ((GET_MODE_CLASS (mode) != MODE_INT
12920 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12921 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12922 #ifdef POINTERS_EXTEND_UNSIGNED
12923 && (mode != Pmode || mem_mode == VOIDmode)
12924 #endif
12926 break;
12927 if (GET_CODE (rtl) == SYMBOL_REF
12928 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12930 dw_loc_descr_ref temp;
12932 /* If this is not defined, we have no way to emit the data. */
12933 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12934 break;
12936 temp = new_addr_loc_descr (rtl, dtprel_true);
12938 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12939 add_loc_descr (&mem_loc_result, temp);
12941 break;
12944 if (!const_ok_for_output (rtl))
12946 if (GET_CODE (rtl) == CONST)
12947 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12948 initialized);
12949 break;
12952 symref:
12953 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12954 vec_safe_push (used_rtx_array, rtl);
12955 break;
12957 case CONCAT:
12958 case CONCATN:
12959 case VAR_LOCATION:
12960 case DEBUG_IMPLICIT_PTR:
12961 expansion_failed (NULL_TREE, rtl,
12962 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12963 return 0;
12965 case ENTRY_VALUE:
12966 if (dwarf_strict)
12967 return NULL;
12968 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12970 if (GET_MODE_CLASS (mode) != MODE_INT
12971 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12972 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12973 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12974 else
12976 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12977 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12978 return NULL;
12979 op0 = one_reg_loc_descriptor (dbx_regnum,
12980 VAR_INIT_STATUS_INITIALIZED);
12983 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12984 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12986 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12987 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12988 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12989 return NULL;
12991 else
12992 gcc_unreachable ();
12993 if (op0 == NULL)
12994 return NULL;
12995 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12996 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12997 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12998 break;
13000 case DEBUG_PARAMETER_REF:
13001 mem_loc_result = parameter_ref_descriptor (rtl);
13002 break;
13004 case PRE_MODIFY:
13005 /* Extract the PLUS expression nested inside and fall into
13006 PLUS code below. */
13007 rtl = XEXP (rtl, 1);
13008 goto plus;
13010 case PRE_INC:
13011 case PRE_DEC:
13012 /* Turn these into a PLUS expression and fall into the PLUS code
13013 below. */
13014 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13015 gen_int_mode (GET_CODE (rtl) == PRE_INC
13016 ? GET_MODE_UNIT_SIZE (mem_mode)
13017 : -GET_MODE_UNIT_SIZE (mem_mode),
13018 mode));
13020 /* ... fall through ... */
13022 case PLUS:
13023 plus:
13024 if (is_based_loc (rtl)
13025 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13026 || XEXP (rtl, 0) == arg_pointer_rtx
13027 || XEXP (rtl, 0) == frame_pointer_rtx)
13028 && GET_MODE_CLASS (mode) == MODE_INT)
13029 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13030 INTVAL (XEXP (rtl, 1)),
13031 VAR_INIT_STATUS_INITIALIZED);
13032 else
13034 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13035 VAR_INIT_STATUS_INITIALIZED);
13036 if (mem_loc_result == 0)
13037 break;
13039 if (CONST_INT_P (XEXP (rtl, 1))
13040 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13041 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13042 else
13044 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13045 VAR_INIT_STATUS_INITIALIZED);
13046 if (op1 == 0)
13047 return NULL;
13048 add_loc_descr (&mem_loc_result, op1);
13049 add_loc_descr (&mem_loc_result,
13050 new_loc_descr (DW_OP_plus, 0, 0));
13053 break;
13055 /* If a pseudo-reg is optimized away, it is possible for it to
13056 be replaced with a MEM containing a multiply or shift. */
13057 case MINUS:
13058 op = DW_OP_minus;
13059 goto do_binop;
13061 case MULT:
13062 op = DW_OP_mul;
13063 goto do_binop;
13065 case DIV:
13066 if (!dwarf_strict
13067 && GET_MODE_CLASS (mode) == MODE_INT
13068 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13070 mem_loc_result = typed_binop (DW_OP_div, rtl,
13071 base_type_for_mode (mode, 0),
13072 mode, mem_mode);
13073 break;
13075 op = DW_OP_div;
13076 goto do_binop;
13078 case UMOD:
13079 op = DW_OP_mod;
13080 goto do_binop;
13082 case ASHIFT:
13083 op = DW_OP_shl;
13084 goto do_shift;
13086 case ASHIFTRT:
13087 op = DW_OP_shra;
13088 goto do_shift;
13090 case LSHIFTRT:
13091 op = DW_OP_shr;
13092 goto do_shift;
13094 do_shift:
13095 if (GET_MODE_CLASS (mode) != MODE_INT)
13096 break;
13097 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13098 VAR_INIT_STATUS_INITIALIZED);
13100 rtx rtlop1 = XEXP (rtl, 1);
13101 if (GET_MODE (rtlop1) != VOIDmode
13102 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13103 < GET_MODE_BITSIZE (mode))
13104 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13105 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13106 VAR_INIT_STATUS_INITIALIZED);
13109 if (op0 == 0 || op1 == 0)
13110 break;
13112 mem_loc_result = op0;
13113 add_loc_descr (&mem_loc_result, op1);
13114 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13115 break;
13117 case AND:
13118 op = DW_OP_and;
13119 goto do_binop;
13121 case IOR:
13122 op = DW_OP_or;
13123 goto do_binop;
13125 case XOR:
13126 op = DW_OP_xor;
13127 goto do_binop;
13129 do_binop:
13130 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13131 VAR_INIT_STATUS_INITIALIZED);
13132 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13133 VAR_INIT_STATUS_INITIALIZED);
13135 if (op0 == 0 || op1 == 0)
13136 break;
13138 mem_loc_result = op0;
13139 add_loc_descr (&mem_loc_result, op1);
13140 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13141 break;
13143 case MOD:
13144 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13146 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13147 base_type_for_mode (mode, 0),
13148 mode, mem_mode);
13149 break;
13152 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13153 VAR_INIT_STATUS_INITIALIZED);
13154 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13155 VAR_INIT_STATUS_INITIALIZED);
13157 if (op0 == 0 || op1 == 0)
13158 break;
13160 mem_loc_result = op0;
13161 add_loc_descr (&mem_loc_result, op1);
13162 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13163 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13164 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13165 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13166 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13167 break;
13169 case UDIV:
13170 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13172 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13174 op = DW_OP_div;
13175 goto do_binop;
13177 mem_loc_result = typed_binop (DW_OP_div, rtl,
13178 base_type_for_mode (mode, 1),
13179 mode, mem_mode);
13181 break;
13183 case NOT:
13184 op = DW_OP_not;
13185 goto do_unop;
13187 case ABS:
13188 op = DW_OP_abs;
13189 goto do_unop;
13191 case NEG:
13192 op = DW_OP_neg;
13193 goto do_unop;
13195 do_unop:
13196 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13197 VAR_INIT_STATUS_INITIALIZED);
13199 if (op0 == 0)
13200 break;
13202 mem_loc_result = op0;
13203 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13204 break;
13206 case CONST_INT:
13207 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13208 #ifdef POINTERS_EXTEND_UNSIGNED
13209 || (mode == Pmode
13210 && mem_mode != VOIDmode
13211 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13212 #endif
13215 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13216 break;
13218 if (!dwarf_strict
13219 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13220 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13222 dw_die_ref type_die = base_type_for_mode (mode, 1);
13223 machine_mode amode;
13224 if (type_die == NULL)
13225 return NULL;
13226 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13227 MODE_INT, 0);
13228 if (INTVAL (rtl) >= 0
13229 && amode != BLKmode
13230 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13231 /* const DW_OP_GNU_convert <XXX> vs.
13232 DW_OP_GNU_const_type <XXX, 1, const>. */
13233 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13234 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13236 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13237 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13238 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13239 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13240 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13241 add_loc_descr (&mem_loc_result, op0);
13242 return mem_loc_result;
13244 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13245 INTVAL (rtl));
13246 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13247 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13248 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13249 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13250 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13251 else
13253 mem_loc_result->dw_loc_oprnd2.val_class
13254 = dw_val_class_const_double;
13255 mem_loc_result->dw_loc_oprnd2.v.val_double
13256 = double_int::from_shwi (INTVAL (rtl));
13259 break;
13261 case CONST_DOUBLE:
13262 if (!dwarf_strict)
13264 dw_die_ref type_die;
13266 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13267 CONST_DOUBLE rtx could represent either a large integer
13268 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13269 the value is always a floating point constant.
13271 When it is an integer, a CONST_DOUBLE is used whenever
13272 the constant requires 2 HWIs to be adequately represented.
13273 We output CONST_DOUBLEs as blocks. */
13274 if (mode == VOIDmode
13275 || (GET_MODE (rtl) == VOIDmode
13276 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13277 break;
13278 type_die = base_type_for_mode (mode,
13279 GET_MODE_CLASS (mode) == MODE_INT);
13280 if (type_die == NULL)
13281 return NULL;
13282 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13283 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13284 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13285 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13286 #if TARGET_SUPPORTS_WIDE_INT == 0
13287 if (!SCALAR_FLOAT_MODE_P (mode))
13289 mem_loc_result->dw_loc_oprnd2.val_class
13290 = dw_val_class_const_double;
13291 mem_loc_result->dw_loc_oprnd2.v.val_double
13292 = rtx_to_double_int (rtl);
13294 else
13295 #endif
13297 unsigned int length = GET_MODE_SIZE (mode);
13298 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13300 insert_float (rtl, array);
13301 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13302 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13303 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13304 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13307 break;
13309 case CONST_WIDE_INT:
13310 if (!dwarf_strict)
13312 dw_die_ref type_die;
13314 type_die = base_type_for_mode (mode,
13315 GET_MODE_CLASS (mode) == MODE_INT);
13316 if (type_die == NULL)
13317 return NULL;
13318 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13319 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13320 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13321 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13322 mem_loc_result->dw_loc_oprnd2.val_class
13323 = dw_val_class_wide_int;
13324 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13325 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13327 break;
13329 case EQ:
13330 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13331 break;
13333 case GE:
13334 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13335 break;
13337 case GT:
13338 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13339 break;
13341 case LE:
13342 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13343 break;
13345 case LT:
13346 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13347 break;
13349 case NE:
13350 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13351 break;
13353 case GEU:
13354 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13355 break;
13357 case GTU:
13358 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13359 break;
13361 case LEU:
13362 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13363 break;
13365 case LTU:
13366 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13367 break;
13369 case UMIN:
13370 case UMAX:
13371 if (GET_MODE_CLASS (mode) != MODE_INT)
13372 break;
13373 /* FALLTHRU */
13374 case SMIN:
13375 case SMAX:
13376 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13377 break;
13379 case ZERO_EXTRACT:
13380 case SIGN_EXTRACT:
13381 if (CONST_INT_P (XEXP (rtl, 1))
13382 && CONST_INT_P (XEXP (rtl, 2))
13383 && ((unsigned) INTVAL (XEXP (rtl, 1))
13384 + (unsigned) INTVAL (XEXP (rtl, 2))
13385 <= GET_MODE_BITSIZE (mode))
13386 && GET_MODE_CLASS (mode) == MODE_INT
13387 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13388 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13390 int shift, size;
13391 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13392 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13393 if (op0 == 0)
13394 break;
13395 if (GET_CODE (rtl) == SIGN_EXTRACT)
13396 op = DW_OP_shra;
13397 else
13398 op = DW_OP_shr;
13399 mem_loc_result = op0;
13400 size = INTVAL (XEXP (rtl, 1));
13401 shift = INTVAL (XEXP (rtl, 2));
13402 if (BITS_BIG_ENDIAN)
13403 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13404 - shift - size;
13405 if (shift + size != (int) DWARF2_ADDR_SIZE)
13407 add_loc_descr (&mem_loc_result,
13408 int_loc_descriptor (DWARF2_ADDR_SIZE
13409 - shift - size));
13410 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13412 if (size != (int) DWARF2_ADDR_SIZE)
13414 add_loc_descr (&mem_loc_result,
13415 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13416 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13419 break;
13421 case IF_THEN_ELSE:
13423 dw_loc_descr_ref op2, bra_node, drop_node;
13424 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13425 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13426 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13427 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13428 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13429 VAR_INIT_STATUS_INITIALIZED);
13430 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13431 VAR_INIT_STATUS_INITIALIZED);
13432 if (op0 == NULL || op1 == NULL || op2 == NULL)
13433 break;
13435 mem_loc_result = op1;
13436 add_loc_descr (&mem_loc_result, op2);
13437 add_loc_descr (&mem_loc_result, op0);
13438 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13439 add_loc_descr (&mem_loc_result, bra_node);
13440 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13441 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13442 add_loc_descr (&mem_loc_result, drop_node);
13443 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13444 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13446 break;
13448 case FLOAT_EXTEND:
13449 case FLOAT_TRUNCATE:
13450 case FLOAT:
13451 case UNSIGNED_FLOAT:
13452 case FIX:
13453 case UNSIGNED_FIX:
13454 if (!dwarf_strict)
13456 dw_die_ref type_die;
13457 dw_loc_descr_ref cvt;
13459 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13460 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13461 if (op0 == NULL)
13462 break;
13463 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13464 && (GET_CODE (rtl) == FLOAT
13465 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13466 <= DWARF2_ADDR_SIZE))
13468 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13469 GET_CODE (rtl) == UNSIGNED_FLOAT);
13470 if (type_die == NULL)
13471 break;
13472 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13473 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13474 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13475 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13476 add_loc_descr (&op0, cvt);
13478 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13479 if (type_die == NULL)
13480 break;
13481 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13482 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13483 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13484 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13485 add_loc_descr (&op0, cvt);
13486 if (GET_MODE_CLASS (mode) == MODE_INT
13487 && (GET_CODE (rtl) == FIX
13488 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13490 op0 = convert_descriptor_to_mode (mode, op0);
13491 if (op0 == NULL)
13492 break;
13494 mem_loc_result = op0;
13496 break;
13498 case CLZ:
13499 case CTZ:
13500 case FFS:
13501 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13502 break;
13504 case POPCOUNT:
13505 case PARITY:
13506 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13507 break;
13509 case BSWAP:
13510 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13511 break;
13513 case ROTATE:
13514 case ROTATERT:
13515 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13516 break;
13518 case COMPARE:
13519 /* In theory, we could implement the above. */
13520 /* DWARF cannot represent the unsigned compare operations
13521 natively. */
13522 case SS_MULT:
13523 case US_MULT:
13524 case SS_DIV:
13525 case US_DIV:
13526 case SS_PLUS:
13527 case US_PLUS:
13528 case SS_MINUS:
13529 case US_MINUS:
13530 case SS_NEG:
13531 case US_NEG:
13532 case SS_ABS:
13533 case SS_ASHIFT:
13534 case US_ASHIFT:
13535 case SS_TRUNCATE:
13536 case US_TRUNCATE:
13537 case UNORDERED:
13538 case ORDERED:
13539 case UNEQ:
13540 case UNGE:
13541 case UNGT:
13542 case UNLE:
13543 case UNLT:
13544 case LTGT:
13545 case FRACT_CONVERT:
13546 case UNSIGNED_FRACT_CONVERT:
13547 case SAT_FRACT:
13548 case UNSIGNED_SAT_FRACT:
13549 case SQRT:
13550 case ASM_OPERANDS:
13551 case VEC_MERGE:
13552 case VEC_SELECT:
13553 case VEC_CONCAT:
13554 case VEC_DUPLICATE:
13555 case UNSPEC:
13556 case HIGH:
13557 case FMA:
13558 case STRICT_LOW_PART:
13559 case CONST_VECTOR:
13560 case CONST_FIXED:
13561 case CLRSB:
13562 case CLOBBER:
13563 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13564 can't express it in the debug info. This can happen e.g. with some
13565 TLS UNSPECs. */
13566 break;
13568 case CONST_STRING:
13569 resolve_one_addr (&rtl);
13570 goto symref;
13572 default:
13573 #ifdef ENABLE_CHECKING
13574 print_rtl (stderr, rtl);
13575 gcc_unreachable ();
13576 #else
13577 break;
13578 #endif
13581 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13582 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13584 return mem_loc_result;
13587 /* Return a descriptor that describes the concatenation of two locations.
13588 This is typically a complex variable. */
13590 static dw_loc_descr_ref
13591 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13593 dw_loc_descr_ref cc_loc_result = NULL;
13594 dw_loc_descr_ref x0_ref
13595 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13596 dw_loc_descr_ref x1_ref
13597 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13599 if (x0_ref == 0 || x1_ref == 0)
13600 return 0;
13602 cc_loc_result = x0_ref;
13603 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13605 add_loc_descr (&cc_loc_result, x1_ref);
13606 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13608 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13609 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13611 return cc_loc_result;
13614 /* Return a descriptor that describes the concatenation of N
13615 locations. */
13617 static dw_loc_descr_ref
13618 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13620 unsigned int i;
13621 dw_loc_descr_ref cc_loc_result = NULL;
13622 unsigned int n = XVECLEN (concatn, 0);
13624 for (i = 0; i < n; ++i)
13626 dw_loc_descr_ref ref;
13627 rtx x = XVECEXP (concatn, 0, i);
13629 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13630 if (ref == NULL)
13631 return NULL;
13633 add_loc_descr (&cc_loc_result, ref);
13634 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13637 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13638 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13640 return cc_loc_result;
13643 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13644 for DEBUG_IMPLICIT_PTR RTL. */
13646 static dw_loc_descr_ref
13647 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13649 dw_loc_descr_ref ret;
13650 dw_die_ref ref;
13652 if (dwarf_strict)
13653 return NULL;
13654 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13655 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13656 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13657 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13658 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13659 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13660 if (ref)
13662 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13663 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13664 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13666 else
13668 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13669 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13671 return ret;
13674 /* Output a proper Dwarf location descriptor for a variable or parameter
13675 which is either allocated in a register or in a memory location. For a
13676 register, we just generate an OP_REG and the register number. For a
13677 memory location we provide a Dwarf postfix expression describing how to
13678 generate the (dynamic) address of the object onto the address stack.
13680 MODE is mode of the decl if this loc_descriptor is going to be used in
13681 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13682 allowed, VOIDmode otherwise.
13684 If we don't know how to describe it, return 0. */
13686 static dw_loc_descr_ref
13687 loc_descriptor (rtx rtl, machine_mode mode,
13688 enum var_init_status initialized)
13690 dw_loc_descr_ref loc_result = NULL;
13692 switch (GET_CODE (rtl))
13694 case SUBREG:
13695 /* The case of a subreg may arise when we have a local (register)
13696 variable or a formal (register) parameter which doesn't quite fill
13697 up an entire register. For now, just assume that it is
13698 legitimate to make the Dwarf info refer to the whole register which
13699 contains the given subreg. */
13700 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13701 loc_result = loc_descriptor (SUBREG_REG (rtl),
13702 GET_MODE (SUBREG_REG (rtl)), initialized);
13703 else
13704 goto do_default;
13705 break;
13707 case REG:
13708 loc_result = reg_loc_descriptor (rtl, initialized);
13709 break;
13711 case MEM:
13712 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13713 GET_MODE (rtl), initialized);
13714 if (loc_result == NULL)
13715 loc_result = tls_mem_loc_descriptor (rtl);
13716 if (loc_result == NULL)
13718 rtx new_rtl = avoid_constant_pool_reference (rtl);
13719 if (new_rtl != rtl)
13720 loc_result = loc_descriptor (new_rtl, mode, initialized);
13722 break;
13724 case CONCAT:
13725 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13726 initialized);
13727 break;
13729 case CONCATN:
13730 loc_result = concatn_loc_descriptor (rtl, initialized);
13731 break;
13733 case VAR_LOCATION:
13734 /* Single part. */
13735 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13737 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13738 if (GET_CODE (loc) == EXPR_LIST)
13739 loc = XEXP (loc, 0);
13740 loc_result = loc_descriptor (loc, mode, initialized);
13741 break;
13744 rtl = XEXP (rtl, 1);
13745 /* FALLTHRU */
13747 case PARALLEL:
13749 rtvec par_elems = XVEC (rtl, 0);
13750 int num_elem = GET_NUM_ELEM (par_elems);
13751 machine_mode mode;
13752 int i;
13754 /* Create the first one, so we have something to add to. */
13755 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13756 VOIDmode, initialized);
13757 if (loc_result == NULL)
13758 return NULL;
13759 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13760 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13761 for (i = 1; i < num_elem; i++)
13763 dw_loc_descr_ref temp;
13765 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13766 VOIDmode, initialized);
13767 if (temp == NULL)
13768 return NULL;
13769 add_loc_descr (&loc_result, temp);
13770 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13771 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13774 break;
13776 case CONST_INT:
13777 if (mode != VOIDmode && mode != BLKmode)
13778 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13779 INTVAL (rtl));
13780 break;
13782 case CONST_DOUBLE:
13783 if (mode == VOIDmode)
13784 mode = GET_MODE (rtl);
13786 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13788 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13790 /* Note that a CONST_DOUBLE rtx could represent either an integer
13791 or a floating-point constant. A CONST_DOUBLE is used whenever
13792 the constant requires more than one word in order to be
13793 adequately represented. We output CONST_DOUBLEs as blocks. */
13794 loc_result = new_loc_descr (DW_OP_implicit_value,
13795 GET_MODE_SIZE (mode), 0);
13796 #if TARGET_SUPPORTS_WIDE_INT == 0
13797 if (!SCALAR_FLOAT_MODE_P (mode))
13799 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13800 loc_result->dw_loc_oprnd2.v.val_double
13801 = rtx_to_double_int (rtl);
13803 else
13804 #endif
13806 unsigned int length = GET_MODE_SIZE (mode);
13807 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13809 insert_float (rtl, array);
13810 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13811 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13812 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13813 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13816 break;
13818 case CONST_WIDE_INT:
13819 if (mode == VOIDmode)
13820 mode = GET_MODE (rtl);
13822 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13824 loc_result = new_loc_descr (DW_OP_implicit_value,
13825 GET_MODE_SIZE (mode), 0);
13826 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13827 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13828 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13830 break;
13832 case CONST_VECTOR:
13833 if (mode == VOIDmode)
13834 mode = GET_MODE (rtl);
13836 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13838 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13839 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13840 unsigned char *array
13841 = ggc_vec_alloc<unsigned char> (length * elt_size);
13842 unsigned int i;
13843 unsigned char *p;
13844 machine_mode imode = GET_MODE_INNER (mode);
13846 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13847 switch (GET_MODE_CLASS (mode))
13849 case MODE_VECTOR_INT:
13850 for (i = 0, p = array; i < length; i++, p += elt_size)
13852 rtx elt = CONST_VECTOR_ELT (rtl, i);
13853 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13855 break;
13857 case MODE_VECTOR_FLOAT:
13858 for (i = 0, p = array; i < length; i++, p += elt_size)
13860 rtx elt = CONST_VECTOR_ELT (rtl, i);
13861 insert_float (elt, p);
13863 break;
13865 default:
13866 gcc_unreachable ();
13869 loc_result = new_loc_descr (DW_OP_implicit_value,
13870 length * elt_size, 0);
13871 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13872 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13873 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13874 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13876 break;
13878 case CONST:
13879 if (mode == VOIDmode
13880 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13881 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13882 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13884 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13885 break;
13887 /* FALLTHROUGH */
13888 case SYMBOL_REF:
13889 if (!const_ok_for_output (rtl))
13890 break;
13891 case LABEL_REF:
13892 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13893 && (dwarf_version >= 4 || !dwarf_strict))
13895 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13896 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13897 vec_safe_push (used_rtx_array, rtl);
13899 break;
13901 case DEBUG_IMPLICIT_PTR:
13902 loc_result = implicit_ptr_descriptor (rtl, 0);
13903 break;
13905 case PLUS:
13906 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13907 && CONST_INT_P (XEXP (rtl, 1)))
13909 loc_result
13910 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13911 break;
13913 /* FALLTHRU */
13914 do_default:
13915 default:
13916 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13917 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13918 && dwarf_version >= 4)
13919 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13921 /* Value expression. */
13922 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13923 if (loc_result)
13924 add_loc_descr (&loc_result,
13925 new_loc_descr (DW_OP_stack_value, 0, 0));
13927 break;
13930 return loc_result;
13933 /* We need to figure out what section we should use as the base for the
13934 address ranges where a given location is valid.
13935 1. If this particular DECL has a section associated with it, use that.
13936 2. If this function has a section associated with it, use that.
13937 3. Otherwise, use the text section.
13938 XXX: If you split a variable across multiple sections, we won't notice. */
13940 static const char *
13941 secname_for_decl (const_tree decl)
13943 const char *secname;
13945 if (VAR_OR_FUNCTION_DECL_P (decl)
13946 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13947 && DECL_SECTION_NAME (decl))
13948 secname = DECL_SECTION_NAME (decl);
13949 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13950 secname = DECL_SECTION_NAME (current_function_decl);
13951 else if (cfun && in_cold_section_p)
13952 secname = crtl->subsections.cold_section_label;
13953 else
13954 secname = text_section_label;
13956 return secname;
13959 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13961 static bool
13962 decl_by_reference_p (tree decl)
13964 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13965 || TREE_CODE (decl) == VAR_DECL)
13966 && DECL_BY_REFERENCE (decl));
13969 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13970 for VARLOC. */
13972 static dw_loc_descr_ref
13973 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13974 enum var_init_status initialized)
13976 int have_address = 0;
13977 dw_loc_descr_ref descr;
13978 machine_mode mode;
13980 if (want_address != 2)
13982 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13983 /* Single part. */
13984 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13986 varloc = PAT_VAR_LOCATION_LOC (varloc);
13987 if (GET_CODE (varloc) == EXPR_LIST)
13988 varloc = XEXP (varloc, 0);
13989 mode = GET_MODE (varloc);
13990 if (MEM_P (varloc))
13992 rtx addr = XEXP (varloc, 0);
13993 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13994 mode, initialized);
13995 if (descr)
13996 have_address = 1;
13997 else
13999 rtx x = avoid_constant_pool_reference (varloc);
14000 if (x != varloc)
14001 descr = mem_loc_descriptor (x, mode, VOIDmode,
14002 initialized);
14005 else
14006 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14008 else
14009 return 0;
14011 else
14013 if (GET_CODE (varloc) == VAR_LOCATION)
14014 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14015 else
14016 mode = DECL_MODE (loc);
14017 descr = loc_descriptor (varloc, mode, initialized);
14018 have_address = 1;
14021 if (!descr)
14022 return 0;
14024 if (want_address == 2 && !have_address
14025 && (dwarf_version >= 4 || !dwarf_strict))
14027 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14029 expansion_failed (loc, NULL_RTX,
14030 "DWARF address size mismatch");
14031 return 0;
14033 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14034 have_address = 1;
14036 /* Show if we can't fill the request for an address. */
14037 if (want_address && !have_address)
14039 expansion_failed (loc, NULL_RTX,
14040 "Want address and only have value");
14041 return 0;
14044 /* If we've got an address and don't want one, dereference. */
14045 if (!want_address && have_address)
14047 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14048 enum dwarf_location_atom op;
14050 if (size > DWARF2_ADDR_SIZE || size == -1)
14052 expansion_failed (loc, NULL_RTX,
14053 "DWARF address size mismatch");
14054 return 0;
14056 else if (size == DWARF2_ADDR_SIZE)
14057 op = DW_OP_deref;
14058 else
14059 op = DW_OP_deref_size;
14061 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14064 return descr;
14067 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14068 if it is not possible. */
14070 static dw_loc_descr_ref
14071 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14073 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14074 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14075 else if (dwarf_version >= 3 || !dwarf_strict)
14076 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14077 else
14078 return NULL;
14081 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14082 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14084 static dw_loc_descr_ref
14085 dw_sra_loc_expr (tree decl, rtx loc)
14087 rtx p;
14088 unsigned HOST_WIDE_INT padsize = 0;
14089 dw_loc_descr_ref descr, *descr_tail;
14090 unsigned HOST_WIDE_INT decl_size;
14091 rtx varloc;
14092 enum var_init_status initialized;
14094 if (DECL_SIZE (decl) == NULL
14095 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14096 return NULL;
14098 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14099 descr = NULL;
14100 descr_tail = &descr;
14102 for (p = loc; p; p = XEXP (p, 1))
14104 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14105 rtx loc_note = *decl_piece_varloc_ptr (p);
14106 dw_loc_descr_ref cur_descr;
14107 dw_loc_descr_ref *tail, last = NULL;
14108 unsigned HOST_WIDE_INT opsize = 0;
14110 if (loc_note == NULL_RTX
14111 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14113 padsize += bitsize;
14114 continue;
14116 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14117 varloc = NOTE_VAR_LOCATION (loc_note);
14118 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14119 if (cur_descr == NULL)
14121 padsize += bitsize;
14122 continue;
14125 /* Check that cur_descr either doesn't use
14126 DW_OP_*piece operations, or their sum is equal
14127 to bitsize. Otherwise we can't embed it. */
14128 for (tail = &cur_descr; *tail != NULL;
14129 tail = &(*tail)->dw_loc_next)
14130 if ((*tail)->dw_loc_opc == DW_OP_piece)
14132 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14133 * BITS_PER_UNIT;
14134 last = *tail;
14136 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14138 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14139 last = *tail;
14142 if (last != NULL && opsize != bitsize)
14144 padsize += bitsize;
14145 /* Discard the current piece of the descriptor and release any
14146 addr_table entries it uses. */
14147 remove_loc_list_addr_table_entries (cur_descr);
14148 continue;
14151 /* If there is a hole, add DW_OP_*piece after empty DWARF
14152 expression, which means that those bits are optimized out. */
14153 if (padsize)
14155 if (padsize > decl_size)
14157 remove_loc_list_addr_table_entries (cur_descr);
14158 goto discard_descr;
14160 decl_size -= padsize;
14161 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14162 if (*descr_tail == NULL)
14164 remove_loc_list_addr_table_entries (cur_descr);
14165 goto discard_descr;
14167 descr_tail = &(*descr_tail)->dw_loc_next;
14168 padsize = 0;
14170 *descr_tail = cur_descr;
14171 descr_tail = tail;
14172 if (bitsize > decl_size)
14173 goto discard_descr;
14174 decl_size -= bitsize;
14175 if (last == NULL)
14177 HOST_WIDE_INT offset = 0;
14178 if (GET_CODE (varloc) == VAR_LOCATION
14179 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14181 varloc = PAT_VAR_LOCATION_LOC (varloc);
14182 if (GET_CODE (varloc) == EXPR_LIST)
14183 varloc = XEXP (varloc, 0);
14187 if (GET_CODE (varloc) == CONST
14188 || GET_CODE (varloc) == SIGN_EXTEND
14189 || GET_CODE (varloc) == ZERO_EXTEND)
14190 varloc = XEXP (varloc, 0);
14191 else if (GET_CODE (varloc) == SUBREG)
14192 varloc = SUBREG_REG (varloc);
14193 else
14194 break;
14196 while (1);
14197 /* DW_OP_bit_size offset should be zero for register
14198 or implicit location descriptions and empty location
14199 descriptions, but for memory addresses needs big endian
14200 adjustment. */
14201 if (MEM_P (varloc))
14203 unsigned HOST_WIDE_INT memsize
14204 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14205 if (memsize != bitsize)
14207 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14208 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14209 goto discard_descr;
14210 if (memsize < bitsize)
14211 goto discard_descr;
14212 if (BITS_BIG_ENDIAN)
14213 offset = memsize - bitsize;
14217 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14218 if (*descr_tail == NULL)
14219 goto discard_descr;
14220 descr_tail = &(*descr_tail)->dw_loc_next;
14224 /* If there were any non-empty expressions, add padding till the end of
14225 the decl. */
14226 if (descr != NULL && decl_size != 0)
14228 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14229 if (*descr_tail == NULL)
14230 goto discard_descr;
14232 return descr;
14234 discard_descr:
14235 /* Discard the descriptor and release any addr_table entries it uses. */
14236 remove_loc_list_addr_table_entries (descr);
14237 return NULL;
14240 /* Return the dwarf representation of the location list LOC_LIST of
14241 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14242 function. */
14244 static dw_loc_list_ref
14245 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14247 const char *endname, *secname;
14248 rtx varloc;
14249 enum var_init_status initialized;
14250 struct var_loc_node *node;
14251 dw_loc_descr_ref descr;
14252 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14253 dw_loc_list_ref list = NULL;
14254 dw_loc_list_ref *listp = &list;
14256 /* Now that we know what section we are using for a base,
14257 actually construct the list of locations.
14258 The first location information is what is passed to the
14259 function that creates the location list, and the remaining
14260 locations just get added on to that list.
14261 Note that we only know the start address for a location
14262 (IE location changes), so to build the range, we use
14263 the range [current location start, next location start].
14264 This means we have to special case the last node, and generate
14265 a range of [last location start, end of function label]. */
14267 secname = secname_for_decl (decl);
14269 for (node = loc_list->first; node; node = node->next)
14270 if (GET_CODE (node->loc) == EXPR_LIST
14271 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14273 if (GET_CODE (node->loc) == EXPR_LIST)
14275 /* This requires DW_OP_{,bit_}piece, which is not usable
14276 inside DWARF expressions. */
14277 if (want_address != 2)
14278 continue;
14279 descr = dw_sra_loc_expr (decl, node->loc);
14280 if (descr == NULL)
14281 continue;
14283 else
14285 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14286 varloc = NOTE_VAR_LOCATION (node->loc);
14287 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14289 if (descr)
14291 bool range_across_switch = false;
14292 /* If section switch happens in between node->label
14293 and node->next->label (or end of function) and
14294 we can't emit it as a single entry list,
14295 emit two ranges, first one ending at the end
14296 of first partition and second one starting at the
14297 beginning of second partition. */
14298 if (node == loc_list->last_before_switch
14299 && (node != loc_list->first || loc_list->first->next)
14300 && current_function_decl)
14302 endname = cfun->fde->dw_fde_end;
14303 range_across_switch = true;
14305 /* The variable has a location between NODE->LABEL and
14306 NODE->NEXT->LABEL. */
14307 else if (node->next)
14308 endname = node->next->label;
14309 /* If the variable has a location at the last label
14310 it keeps its location until the end of function. */
14311 else if (!current_function_decl)
14312 endname = text_end_label;
14313 else
14315 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14316 current_function_funcdef_no);
14317 endname = ggc_strdup (label_id);
14320 *listp = new_loc_list (descr, node->label, endname, secname);
14321 if (TREE_CODE (decl) == PARM_DECL
14322 && node == loc_list->first
14323 && NOTE_P (node->loc)
14324 && strcmp (node->label, endname) == 0)
14325 (*listp)->force = true;
14326 listp = &(*listp)->dw_loc_next;
14328 if (range_across_switch)
14330 if (GET_CODE (node->loc) == EXPR_LIST)
14331 descr = dw_sra_loc_expr (decl, node->loc);
14332 else
14334 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14335 varloc = NOTE_VAR_LOCATION (node->loc);
14336 descr = dw_loc_list_1 (decl, varloc, want_address,
14337 initialized);
14339 gcc_assert (descr);
14340 /* The variable has a location between NODE->LABEL and
14341 NODE->NEXT->LABEL. */
14342 if (node->next)
14343 endname = node->next->label;
14344 else
14345 endname = cfun->fde->dw_fde_second_end;
14346 *listp = new_loc_list (descr,
14347 cfun->fde->dw_fde_second_begin,
14348 endname, secname);
14349 listp = &(*listp)->dw_loc_next;
14354 /* Try to avoid the overhead of a location list emitting a location
14355 expression instead, but only if we didn't have more than one
14356 location entry in the first place. If some entries were not
14357 representable, we don't want to pretend a single entry that was
14358 applies to the entire scope in which the variable is
14359 available. */
14360 if (list && loc_list->first->next)
14361 gen_llsym (list);
14363 return list;
14366 /* Return if the loc_list has only single element and thus can be represented
14367 as location description. */
14369 static bool
14370 single_element_loc_list_p (dw_loc_list_ref list)
14372 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14373 return !list->ll_symbol;
14376 /* To each location in list LIST add loc descr REF. */
14378 static void
14379 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14381 dw_loc_descr_ref copy;
14382 add_loc_descr (&list->expr, ref);
14383 list = list->dw_loc_next;
14384 while (list)
14386 copy = ggc_alloc<dw_loc_descr_node> ();
14387 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14388 add_loc_descr (&list->expr, copy);
14389 while (copy->dw_loc_next)
14391 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14392 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14393 copy->dw_loc_next = new_copy;
14394 copy = new_copy;
14396 list = list->dw_loc_next;
14400 /* Given two lists RET and LIST
14401 produce location list that is result of adding expression in LIST
14402 to expression in RET on each position in program.
14403 Might be destructive on both RET and LIST.
14405 TODO: We handle only simple cases of RET or LIST having at most one
14406 element. General case would inolve sorting the lists in program order
14407 and merging them that will need some additional work.
14408 Adding that will improve quality of debug info especially for SRA-ed
14409 structures. */
14411 static void
14412 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14414 if (!list)
14415 return;
14416 if (!*ret)
14418 *ret = list;
14419 return;
14421 if (!list->dw_loc_next)
14423 add_loc_descr_to_each (*ret, list->expr);
14424 return;
14426 if (!(*ret)->dw_loc_next)
14428 add_loc_descr_to_each (list, (*ret)->expr);
14429 *ret = list;
14430 return;
14432 expansion_failed (NULL_TREE, NULL_RTX,
14433 "Don't know how to merge two non-trivial"
14434 " location lists.\n");
14435 *ret = NULL;
14436 return;
14439 /* LOC is constant expression. Try a luck, look it up in constant
14440 pool and return its loc_descr of its address. */
14442 static dw_loc_descr_ref
14443 cst_pool_loc_descr (tree loc)
14445 /* Get an RTL for this, if something has been emitted. */
14446 rtx rtl = lookup_constant_def (loc);
14448 if (!rtl || !MEM_P (rtl))
14450 gcc_assert (!rtl);
14451 return 0;
14453 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14455 /* TODO: We might get more coverage if we was actually delaying expansion
14456 of all expressions till end of compilation when constant pools are fully
14457 populated. */
14458 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14460 expansion_failed (loc, NULL_RTX,
14461 "CST value in contant pool but not marked.");
14462 return 0;
14464 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14465 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14468 /* Return dw_loc_list representing address of addr_expr LOC
14469 by looking for inner INDIRECT_REF expression and turning
14470 it into simple arithmetics.
14472 See loc_list_from_tree for the meaning of CONTEXT. */
14474 static dw_loc_list_ref
14475 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14476 const loc_descr_context *context)
14478 tree obj, offset;
14479 HOST_WIDE_INT bitsize, bitpos, bytepos;
14480 machine_mode mode;
14481 int unsignedp, volatilep = 0;
14482 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14484 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14485 &bitsize, &bitpos, &offset, &mode,
14486 &unsignedp, &volatilep, false);
14487 STRIP_NOPS (obj);
14488 if (bitpos % BITS_PER_UNIT)
14490 expansion_failed (loc, NULL_RTX, "bitfield access");
14491 return 0;
14493 if (!INDIRECT_REF_P (obj))
14495 expansion_failed (obj,
14496 NULL_RTX, "no indirect ref in inner refrence");
14497 return 0;
14499 if (!offset && !bitpos)
14500 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14501 context);
14502 else if (toplev
14503 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14504 && (dwarf_version >= 4 || !dwarf_strict))
14506 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14507 if (!list_ret)
14508 return 0;
14509 if (offset)
14511 /* Variable offset. */
14512 list_ret1 = loc_list_from_tree (offset, 0, context);
14513 if (list_ret1 == 0)
14514 return 0;
14515 add_loc_list (&list_ret, list_ret1);
14516 if (!list_ret)
14517 return 0;
14518 add_loc_descr_to_each (list_ret,
14519 new_loc_descr (DW_OP_plus, 0, 0));
14521 bytepos = bitpos / BITS_PER_UNIT;
14522 if (bytepos > 0)
14523 add_loc_descr_to_each (list_ret,
14524 new_loc_descr (DW_OP_plus_uconst,
14525 bytepos, 0));
14526 else if (bytepos < 0)
14527 loc_list_plus_const (list_ret, bytepos);
14528 add_loc_descr_to_each (list_ret,
14529 new_loc_descr (DW_OP_stack_value, 0, 0));
14531 return list_ret;
14535 /* Helper structure for location descriptions generation. */
14536 struct loc_descr_context
14538 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14539 NULL_TREE if DW_OP_push_object_address in invalid for this location
14540 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14541 tree context_type;
14542 /* The ..._DECL node that should be translated as a
14543 DW_OP_push_object_address operation. */
14544 tree base_decl;
14547 /* Generate Dwarf location list representing LOC.
14548 If WANT_ADDRESS is false, expression computing LOC will be computed
14549 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14550 if WANT_ADDRESS is 2, expression computing address useable in location
14551 will be returned (i.e. DW_OP_reg can be used
14552 to refer to register values).
14554 CONTEXT provides information to customize the location descriptions
14555 generation. Its context_type field specifies what type is implicitly
14556 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14557 will not be generated.
14559 If CONTEXT is NULL, the behavior is the same as if both context_type and
14560 base_decl fields were NULL_TREE. */
14562 static dw_loc_list_ref
14563 loc_list_from_tree (tree loc, int want_address,
14564 const struct loc_descr_context *context)
14566 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14567 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14568 int have_address = 0;
14569 enum dwarf_location_atom op;
14571 /* ??? Most of the time we do not take proper care for sign/zero
14572 extending the values properly. Hopefully this won't be a real
14573 problem... */
14575 if (context != NULL
14576 && context->base_decl == loc
14577 && want_address == 0)
14579 if (dwarf_version >= 3 || !dwarf_strict)
14580 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14581 NULL, NULL, NULL);
14582 else
14583 return NULL;
14586 switch (TREE_CODE (loc))
14588 case ERROR_MARK:
14589 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14590 return 0;
14592 case PLACEHOLDER_EXPR:
14593 /* This case involves extracting fields from an object to determine the
14594 position of other fields. It is supposed to appear only as the first
14595 operand of COMPONENT_REF nodes and to reference precisely the type
14596 that the context allows. */
14597 if (context != NULL
14598 && TREE_TYPE (loc) == context->context_type
14599 && want_address >= 1)
14601 if (dwarf_version >= 3 || !dwarf_strict)
14603 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14604 have_address = 1;
14605 break;
14607 else
14608 return NULL;
14610 else
14611 expansion_failed (loc, NULL_RTX,
14612 "PLACEHOLDER_EXPR for an unexpected type");
14613 break;
14615 case CALL_EXPR:
14616 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14617 /* There are no opcodes for these operations. */
14618 return 0;
14620 case PREINCREMENT_EXPR:
14621 case PREDECREMENT_EXPR:
14622 case POSTINCREMENT_EXPR:
14623 case POSTDECREMENT_EXPR:
14624 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14625 /* There are no opcodes for these operations. */
14626 return 0;
14628 case ADDR_EXPR:
14629 /* If we already want an address, see if there is INDIRECT_REF inside
14630 e.g. for &this->field. */
14631 if (want_address)
14633 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14634 (loc, want_address == 2, context);
14635 if (list_ret)
14636 have_address = 1;
14637 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14638 && (ret = cst_pool_loc_descr (loc)))
14639 have_address = 1;
14641 /* Otherwise, process the argument and look for the address. */
14642 if (!list_ret && !ret)
14643 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14644 else
14646 if (want_address)
14647 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14648 return NULL;
14650 break;
14652 case VAR_DECL:
14653 if (DECL_THREAD_LOCAL_P (loc))
14655 rtx rtl;
14656 enum dwarf_location_atom tls_op;
14657 enum dtprel_bool dtprel = dtprel_false;
14659 if (targetm.have_tls)
14661 /* If this is not defined, we have no way to emit the
14662 data. */
14663 if (!targetm.asm_out.output_dwarf_dtprel)
14664 return 0;
14666 /* The way DW_OP_GNU_push_tls_address is specified, we
14667 can only look up addresses of objects in the current
14668 module. We used DW_OP_addr as first op, but that's
14669 wrong, because DW_OP_addr is relocated by the debug
14670 info consumer, while DW_OP_GNU_push_tls_address
14671 operand shouldn't be. */
14672 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14673 return 0;
14674 dtprel = dtprel_true;
14675 tls_op = DW_OP_GNU_push_tls_address;
14677 else
14679 if (!targetm.emutls.debug_form_tls_address
14680 || !(dwarf_version >= 3 || !dwarf_strict))
14681 return 0;
14682 /* We stuffed the control variable into the DECL_VALUE_EXPR
14683 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14684 no longer appear in gimple code. We used the control
14685 variable in specific so that we could pick it up here. */
14686 loc = DECL_VALUE_EXPR (loc);
14687 tls_op = DW_OP_form_tls_address;
14690 rtl = rtl_for_decl_location (loc);
14691 if (rtl == NULL_RTX)
14692 return 0;
14694 if (!MEM_P (rtl))
14695 return 0;
14696 rtl = XEXP (rtl, 0);
14697 if (! CONSTANT_P (rtl))
14698 return 0;
14700 ret = new_addr_loc_descr (rtl, dtprel);
14701 ret1 = new_loc_descr (tls_op, 0, 0);
14702 add_loc_descr (&ret, ret1);
14704 have_address = 1;
14705 break;
14707 /* FALLTHRU */
14709 case PARM_DECL:
14710 case RESULT_DECL:
14711 if (DECL_HAS_VALUE_EXPR_P (loc))
14712 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14713 want_address, context);
14714 /* FALLTHRU */
14716 case FUNCTION_DECL:
14718 rtx rtl;
14719 var_loc_list *loc_list = lookup_decl_loc (loc);
14721 if (loc_list && loc_list->first)
14723 list_ret = dw_loc_list (loc_list, loc, want_address);
14724 have_address = want_address != 0;
14725 break;
14727 rtl = rtl_for_decl_location (loc);
14728 if (rtl == NULL_RTX)
14730 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14731 return 0;
14733 else if (CONST_INT_P (rtl))
14735 HOST_WIDE_INT val = INTVAL (rtl);
14736 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14737 val &= GET_MODE_MASK (DECL_MODE (loc));
14738 ret = int_loc_descriptor (val);
14740 else if (GET_CODE (rtl) == CONST_STRING)
14742 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14743 return 0;
14745 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14746 ret = new_addr_loc_descr (rtl, dtprel_false);
14747 else
14749 machine_mode mode, mem_mode;
14751 /* Certain constructs can only be represented at top-level. */
14752 if (want_address == 2)
14754 ret = loc_descriptor (rtl, VOIDmode,
14755 VAR_INIT_STATUS_INITIALIZED);
14756 have_address = 1;
14758 else
14760 mode = GET_MODE (rtl);
14761 mem_mode = VOIDmode;
14762 if (MEM_P (rtl))
14764 mem_mode = mode;
14765 mode = get_address_mode (rtl);
14766 rtl = XEXP (rtl, 0);
14767 have_address = 1;
14769 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14770 VAR_INIT_STATUS_INITIALIZED);
14772 if (!ret)
14773 expansion_failed (loc, rtl,
14774 "failed to produce loc descriptor for rtl");
14777 break;
14779 case MEM_REF:
14780 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14782 have_address = 1;
14783 goto do_plus;
14785 /* Fallthru. */
14786 case INDIRECT_REF:
14787 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14788 have_address = 1;
14789 break;
14791 case TARGET_MEM_REF:
14792 case SSA_NAME:
14793 case DEBUG_EXPR_DECL:
14794 return NULL;
14796 case COMPOUND_EXPR:
14797 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14799 CASE_CONVERT:
14800 case VIEW_CONVERT_EXPR:
14801 case SAVE_EXPR:
14802 case MODIFY_EXPR:
14803 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14805 case COMPONENT_REF:
14806 case BIT_FIELD_REF:
14807 case ARRAY_REF:
14808 case ARRAY_RANGE_REF:
14809 case REALPART_EXPR:
14810 case IMAGPART_EXPR:
14812 tree obj, offset;
14813 HOST_WIDE_INT bitsize, bitpos, bytepos;
14814 machine_mode mode;
14815 int unsignedp, volatilep = 0;
14817 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14818 &unsignedp, &volatilep, false);
14820 gcc_assert (obj != loc);
14822 list_ret = loc_list_from_tree (obj,
14823 want_address == 2
14824 && !bitpos && !offset ? 2 : 1,
14825 context);
14826 /* TODO: We can extract value of the small expression via shifting even
14827 for nonzero bitpos. */
14828 if (list_ret == 0)
14829 return 0;
14830 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14832 expansion_failed (loc, NULL_RTX,
14833 "bitfield access");
14834 return 0;
14837 if (offset != NULL_TREE)
14839 /* Variable offset. */
14840 list_ret1 = loc_list_from_tree (offset, 0, context);
14841 if (list_ret1 == 0)
14842 return 0;
14843 add_loc_list (&list_ret, list_ret1);
14844 if (!list_ret)
14845 return 0;
14846 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14849 bytepos = bitpos / BITS_PER_UNIT;
14850 if (bytepos > 0)
14851 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14852 else if (bytepos < 0)
14853 loc_list_plus_const (list_ret, bytepos);
14855 have_address = 1;
14856 break;
14859 case INTEGER_CST:
14860 if ((want_address || !tree_fits_shwi_p (loc))
14861 && (ret = cst_pool_loc_descr (loc)))
14862 have_address = 1;
14863 else if (want_address == 2
14864 && tree_fits_shwi_p (loc)
14865 && (ret = address_of_int_loc_descriptor
14866 (int_size_in_bytes (TREE_TYPE (loc)),
14867 tree_to_shwi (loc))))
14868 have_address = 1;
14869 else if (tree_fits_shwi_p (loc))
14870 ret = int_loc_descriptor (tree_to_shwi (loc));
14871 else
14873 expansion_failed (loc, NULL_RTX,
14874 "Integer operand is not host integer");
14875 return 0;
14877 break;
14879 case CONSTRUCTOR:
14880 case REAL_CST:
14881 case STRING_CST:
14882 case COMPLEX_CST:
14883 if ((ret = cst_pool_loc_descr (loc)))
14884 have_address = 1;
14885 else
14886 /* We can construct small constants here using int_loc_descriptor. */
14887 expansion_failed (loc, NULL_RTX,
14888 "constructor or constant not in constant pool");
14889 break;
14891 case TRUTH_AND_EXPR:
14892 case TRUTH_ANDIF_EXPR:
14893 case BIT_AND_EXPR:
14894 op = DW_OP_and;
14895 goto do_binop;
14897 case TRUTH_XOR_EXPR:
14898 case BIT_XOR_EXPR:
14899 op = DW_OP_xor;
14900 goto do_binop;
14902 case TRUTH_OR_EXPR:
14903 case TRUTH_ORIF_EXPR:
14904 case BIT_IOR_EXPR:
14905 op = DW_OP_or;
14906 goto do_binop;
14908 case FLOOR_DIV_EXPR:
14909 case CEIL_DIV_EXPR:
14910 case ROUND_DIV_EXPR:
14911 case TRUNC_DIV_EXPR:
14912 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14913 return 0;
14914 op = DW_OP_div;
14915 goto do_binop;
14917 case MINUS_EXPR:
14918 op = DW_OP_minus;
14919 goto do_binop;
14921 case FLOOR_MOD_EXPR:
14922 case CEIL_MOD_EXPR:
14923 case ROUND_MOD_EXPR:
14924 case TRUNC_MOD_EXPR:
14925 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14927 op = DW_OP_mod;
14928 goto do_binop;
14930 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14931 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14932 if (list_ret == 0 || list_ret1 == 0)
14933 return 0;
14935 add_loc_list (&list_ret, list_ret1);
14936 if (list_ret == 0)
14937 return 0;
14938 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14939 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14940 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14941 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14942 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14943 break;
14945 case MULT_EXPR:
14946 op = DW_OP_mul;
14947 goto do_binop;
14949 case LSHIFT_EXPR:
14950 op = DW_OP_shl;
14951 goto do_binop;
14953 case RSHIFT_EXPR:
14954 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14955 goto do_binop;
14957 case POINTER_PLUS_EXPR:
14958 case PLUS_EXPR:
14959 do_plus:
14960 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14962 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14963 if (list_ret == 0)
14964 return 0;
14966 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14967 break;
14970 op = DW_OP_plus;
14971 goto do_binop;
14973 case LE_EXPR:
14974 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14975 return 0;
14977 op = DW_OP_le;
14978 goto do_binop;
14980 case GE_EXPR:
14981 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14982 return 0;
14984 op = DW_OP_ge;
14985 goto do_binop;
14987 case LT_EXPR:
14988 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14989 return 0;
14991 op = DW_OP_lt;
14992 goto do_binop;
14994 case GT_EXPR:
14995 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14996 return 0;
14998 op = DW_OP_gt;
14999 goto do_binop;
15001 case EQ_EXPR:
15002 op = DW_OP_eq;
15003 goto do_binop;
15005 case NE_EXPR:
15006 op = DW_OP_ne;
15007 goto do_binop;
15009 do_binop:
15010 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15011 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
15012 if (list_ret == 0 || list_ret1 == 0)
15013 return 0;
15015 add_loc_list (&list_ret, list_ret1);
15016 if (list_ret == 0)
15017 return 0;
15018 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15019 break;
15021 case TRUTH_NOT_EXPR:
15022 case BIT_NOT_EXPR:
15023 op = DW_OP_not;
15024 goto do_unop;
15026 case ABS_EXPR:
15027 op = DW_OP_abs;
15028 goto do_unop;
15030 case NEGATE_EXPR:
15031 op = DW_OP_neg;
15032 goto do_unop;
15034 do_unop:
15035 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15036 if (list_ret == 0)
15037 return 0;
15039 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15040 break;
15042 case MIN_EXPR:
15043 case MAX_EXPR:
15045 const enum tree_code code =
15046 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15048 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15049 build2 (code, integer_type_node,
15050 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15051 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15054 /* ... fall through ... */
15056 case COND_EXPR:
15058 dw_loc_descr_ref lhs
15059 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15060 dw_loc_list_ref rhs
15061 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15062 dw_loc_descr_ref bra_node, jump_node, tmp;
15064 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15065 if (list_ret == 0 || lhs == 0 || rhs == 0)
15066 return 0;
15068 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15069 add_loc_descr_to_each (list_ret, bra_node);
15071 add_loc_list (&list_ret, rhs);
15072 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15073 add_loc_descr_to_each (list_ret, jump_node);
15075 add_loc_descr_to_each (list_ret, lhs);
15076 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15077 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15079 /* ??? Need a node to point the skip at. Use a nop. */
15080 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15081 add_loc_descr_to_each (list_ret, tmp);
15082 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15083 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15085 break;
15087 case FIX_TRUNC_EXPR:
15088 return 0;
15090 default:
15091 /* Leave front-end specific codes as simply unknown. This comes
15092 up, for instance, with the C STMT_EXPR. */
15093 if ((unsigned int) TREE_CODE (loc)
15094 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15096 expansion_failed (loc, NULL_RTX,
15097 "language specific tree node");
15098 return 0;
15101 #ifdef ENABLE_CHECKING
15102 /* Otherwise this is a generic code; we should just lists all of
15103 these explicitly. We forgot one. */
15104 gcc_unreachable ();
15105 #else
15106 /* In a release build, we want to degrade gracefully: better to
15107 generate incomplete debugging information than to crash. */
15108 return NULL;
15109 #endif
15112 if (!ret && !list_ret)
15113 return 0;
15115 if (want_address == 2 && !have_address
15116 && (dwarf_version >= 4 || !dwarf_strict))
15118 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15120 expansion_failed (loc, NULL_RTX,
15121 "DWARF address size mismatch");
15122 return 0;
15124 if (ret)
15125 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15126 else
15127 add_loc_descr_to_each (list_ret,
15128 new_loc_descr (DW_OP_stack_value, 0, 0));
15129 have_address = 1;
15131 /* Show if we can't fill the request for an address. */
15132 if (want_address && !have_address)
15134 expansion_failed (loc, NULL_RTX,
15135 "Want address and only have value");
15136 return 0;
15139 gcc_assert (!ret || !list_ret);
15141 /* If we've got an address and don't want one, dereference. */
15142 if (!want_address && have_address)
15144 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15146 if (size > DWARF2_ADDR_SIZE || size == -1)
15148 expansion_failed (loc, NULL_RTX,
15149 "DWARF address size mismatch");
15150 return 0;
15152 else if (size == DWARF2_ADDR_SIZE)
15153 op = DW_OP_deref;
15154 else
15155 op = DW_OP_deref_size;
15157 if (ret)
15158 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15159 else
15160 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15162 if (ret)
15163 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15165 return list_ret;
15168 /* Same as above but return only single location expression. */
15169 static dw_loc_descr_ref
15170 loc_descriptor_from_tree (tree loc, int want_address,
15171 const struct loc_descr_context *context)
15173 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15174 if (!ret)
15175 return NULL;
15176 if (ret->dw_loc_next)
15178 expansion_failed (loc, NULL_RTX,
15179 "Location list where only loc descriptor needed");
15180 return NULL;
15182 return ret->expr;
15185 /* Given a value, round it up to the lowest multiple of `boundary'
15186 which is not less than the value itself. */
15188 static inline HOST_WIDE_INT
15189 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15191 return (((value + boundary - 1) / boundary) * boundary);
15194 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15195 pointer to the declared type for the relevant field variable, or return
15196 `integer_type_node' if the given node turns out to be an
15197 ERROR_MARK node. */
15199 static inline tree
15200 field_type (const_tree decl)
15202 tree type;
15204 if (TREE_CODE (decl) == ERROR_MARK)
15205 return integer_type_node;
15207 type = DECL_BIT_FIELD_TYPE (decl);
15208 if (type == NULL_TREE)
15209 type = TREE_TYPE (decl);
15211 return type;
15214 /* Given a pointer to a tree node, return the alignment in bits for
15215 it, or else return BITS_PER_WORD if the node actually turns out to
15216 be an ERROR_MARK node. */
15218 static inline unsigned
15219 simple_type_align_in_bits (const_tree type)
15221 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15224 static inline unsigned
15225 simple_decl_align_in_bits (const_tree decl)
15227 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15230 /* Return the result of rounding T up to ALIGN. */
15232 static inline offset_int
15233 round_up_to_align (const offset_int &t, unsigned int align)
15235 return wi::udiv_trunc (t + align - 1, align) * align;
15238 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15239 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15240 or return 0 if we are unable to determine what that offset is, either
15241 because the argument turns out to be a pointer to an ERROR_MARK node, or
15242 because the offset is actually variable. (We can't handle the latter case
15243 just yet). */
15245 static HOST_WIDE_INT
15246 field_byte_offset (const_tree decl)
15248 offset_int object_offset_in_bits;
15249 offset_int object_offset_in_bytes;
15250 offset_int bitpos_int;
15252 if (TREE_CODE (decl) == ERROR_MARK)
15253 return 0;
15255 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15257 /* We cannot yet cope with fields whose positions are variable, so
15258 for now, when we see such things, we simply return 0. Someday, we may
15259 be able to handle such cases, but it will be damn difficult. */
15260 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15261 return 0;
15263 bitpos_int = wi::to_offset (bit_position (decl));
15265 if (PCC_BITFIELD_TYPE_MATTERS)
15267 tree type;
15268 tree field_size_tree;
15269 offset_int deepest_bitpos;
15270 offset_int field_size_in_bits;
15271 unsigned int type_align_in_bits;
15272 unsigned int decl_align_in_bits;
15273 offset_int type_size_in_bits;
15275 type = field_type (decl);
15276 type_size_in_bits = offset_int_type_size_in_bits (type);
15277 type_align_in_bits = simple_type_align_in_bits (type);
15279 field_size_tree = DECL_SIZE (decl);
15281 /* The size could be unspecified if there was an error, or for
15282 a flexible array member. */
15283 if (!field_size_tree)
15284 field_size_tree = bitsize_zero_node;
15286 /* If the size of the field is not constant, use the type size. */
15287 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15288 field_size_in_bits = wi::to_offset (field_size_tree);
15289 else
15290 field_size_in_bits = type_size_in_bits;
15292 decl_align_in_bits = simple_decl_align_in_bits (decl);
15294 /* The GCC front-end doesn't make any attempt to keep track of the
15295 starting bit offset (relative to the start of the containing
15296 structure type) of the hypothetical "containing object" for a
15297 bit-field. Thus, when computing the byte offset value for the
15298 start of the "containing object" of a bit-field, we must deduce
15299 this information on our own. This can be rather tricky to do in
15300 some cases. For example, handling the following structure type
15301 definition when compiling for an i386/i486 target (which only
15302 aligns long long's to 32-bit boundaries) can be very tricky:
15304 struct S { int field1; long long field2:31; };
15306 Fortunately, there is a simple rule-of-thumb which can be used
15307 in such cases. When compiling for an i386/i486, GCC will
15308 allocate 8 bytes for the structure shown above. It decides to
15309 do this based upon one simple rule for bit-field allocation.
15310 GCC allocates each "containing object" for each bit-field at
15311 the first (i.e. lowest addressed) legitimate alignment boundary
15312 (based upon the required minimum alignment for the declared
15313 type of the field) which it can possibly use, subject to the
15314 condition that there is still enough available space remaining
15315 in the containing object (when allocated at the selected point)
15316 to fully accommodate all of the bits of the bit-field itself.
15318 This simple rule makes it obvious why GCC allocates 8 bytes for
15319 each object of the structure type shown above. When looking
15320 for a place to allocate the "containing object" for `field2',
15321 the compiler simply tries to allocate a 64-bit "containing
15322 object" at each successive 32-bit boundary (starting at zero)
15323 until it finds a place to allocate that 64- bit field such that
15324 at least 31 contiguous (and previously unallocated) bits remain
15325 within that selected 64 bit field. (As it turns out, for the
15326 example above, the compiler finds it is OK to allocate the
15327 "containing object" 64-bit field at bit-offset zero within the
15328 structure type.)
15330 Here we attempt to work backwards from the limited set of facts
15331 we're given, and we try to deduce from those facts, where GCC
15332 must have believed that the containing object started (within
15333 the structure type). The value we deduce is then used (by the
15334 callers of this routine) to generate DW_AT_location and
15335 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15336 the case of DW_AT_location, regular fields as well). */
15338 /* Figure out the bit-distance from the start of the structure to
15339 the "deepest" bit of the bit-field. */
15340 deepest_bitpos = bitpos_int + field_size_in_bits;
15342 /* This is the tricky part. Use some fancy footwork to deduce
15343 where the lowest addressed bit of the containing object must
15344 be. */
15345 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15347 /* Round up to type_align by default. This works best for
15348 bitfields. */
15349 object_offset_in_bits
15350 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15352 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15354 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15356 /* Round up to decl_align instead. */
15357 object_offset_in_bits
15358 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15361 else
15362 object_offset_in_bits = bitpos_int;
15364 object_offset_in_bytes
15365 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15366 return object_offset_in_bytes.to_shwi ();
15369 /* The following routines define various Dwarf attributes and any data
15370 associated with them. */
15372 /* Add a location description attribute value to a DIE.
15374 This emits location attributes suitable for whole variables and
15375 whole parameters. Note that the location attributes for struct fields are
15376 generated by the routine `data_member_location_attribute' below. */
15378 static inline void
15379 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15380 dw_loc_list_ref descr)
15382 if (descr == 0)
15383 return;
15384 if (single_element_loc_list_p (descr))
15385 add_AT_loc (die, attr_kind, descr->expr);
15386 else
15387 add_AT_loc_list (die, attr_kind, descr);
15390 /* Add DW_AT_accessibility attribute to DIE if needed. */
15392 static void
15393 add_accessibility_attribute (dw_die_ref die, tree decl)
15395 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15396 children, otherwise the default is DW_ACCESS_public. In DWARF2
15397 the default has always been DW_ACCESS_public. */
15398 if (TREE_PROTECTED (decl))
15399 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15400 else if (TREE_PRIVATE (decl))
15402 if (dwarf_version == 2
15403 || die->die_parent == NULL
15404 || die->die_parent->die_tag != DW_TAG_class_type)
15405 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15407 else if (dwarf_version > 2
15408 && die->die_parent
15409 && die->die_parent->die_tag == DW_TAG_class_type)
15410 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15413 /* Attach the specialized form of location attribute used for data members of
15414 struct and union types. In the special case of a FIELD_DECL node which
15415 represents a bit-field, the "offset" part of this special location
15416 descriptor must indicate the distance in bytes from the lowest-addressed
15417 byte of the containing struct or union type to the lowest-addressed byte of
15418 the "containing object" for the bit-field. (See the `field_byte_offset'
15419 function above).
15421 For any given bit-field, the "containing object" is a hypothetical object
15422 (of some integral or enum type) within which the given bit-field lives. The
15423 type of this hypothetical "containing object" is always the same as the
15424 declared type of the individual bit-field itself (for GCC anyway... the
15425 DWARF spec doesn't actually mandate this). Note that it is the size (in
15426 bytes) of the hypothetical "containing object" which will be given in the
15427 DW_AT_byte_size attribute for this bit-field. (See the
15428 `byte_size_attribute' function below.) It is also used when calculating the
15429 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15430 function below.) */
15432 static void
15433 add_data_member_location_attribute (dw_die_ref die, tree decl)
15435 HOST_WIDE_INT offset;
15436 dw_loc_descr_ref loc_descr = 0;
15438 if (TREE_CODE (decl) == TREE_BINFO)
15440 /* We're working on the TAG_inheritance for a base class. */
15441 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15443 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15444 aren't at a fixed offset from all (sub)objects of the same
15445 type. We need to extract the appropriate offset from our
15446 vtable. The following dwarf expression means
15448 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15450 This is specific to the V3 ABI, of course. */
15452 dw_loc_descr_ref tmp;
15454 /* Make a copy of the object address. */
15455 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15456 add_loc_descr (&loc_descr, tmp);
15458 /* Extract the vtable address. */
15459 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15460 add_loc_descr (&loc_descr, tmp);
15462 /* Calculate the address of the offset. */
15463 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15464 gcc_assert (offset < 0);
15466 tmp = int_loc_descriptor (-offset);
15467 add_loc_descr (&loc_descr, tmp);
15468 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15469 add_loc_descr (&loc_descr, tmp);
15471 /* Extract the offset. */
15472 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15473 add_loc_descr (&loc_descr, tmp);
15475 /* Add it to the object address. */
15476 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15477 add_loc_descr (&loc_descr, tmp);
15479 else
15480 offset = tree_to_shwi (BINFO_OFFSET (decl));
15482 else
15483 offset = field_byte_offset (decl);
15485 if (! loc_descr)
15487 if (dwarf_version > 2)
15489 /* Don't need to output a location expression, just the constant. */
15490 if (offset < 0)
15491 add_AT_int (die, DW_AT_data_member_location, offset);
15492 else
15493 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15494 return;
15496 else
15498 enum dwarf_location_atom op;
15500 /* The DWARF2 standard says that we should assume that the structure
15501 address is already on the stack, so we can specify a structure
15502 field address by using DW_OP_plus_uconst. */
15503 op = DW_OP_plus_uconst;
15504 loc_descr = new_loc_descr (op, offset, 0);
15508 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15511 /* Writes integer values to dw_vec_const array. */
15513 static void
15514 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15516 while (size != 0)
15518 *dest++ = val & 0xff;
15519 val >>= 8;
15520 --size;
15524 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15526 static HOST_WIDE_INT
15527 extract_int (const unsigned char *src, unsigned int size)
15529 HOST_WIDE_INT val = 0;
15531 src += size;
15532 while (size != 0)
15534 val <<= 8;
15535 val |= *--src & 0xff;
15536 --size;
15538 return val;
15541 /* Writes wide_int values to dw_vec_const array. */
15543 static void
15544 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15546 int i;
15548 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15550 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15551 return;
15554 /* We'd have to extend this code to support odd sizes. */
15555 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15557 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15559 if (WORDS_BIG_ENDIAN)
15560 for (i = n - 1; i >= 0; i--)
15562 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15563 dest += sizeof (HOST_WIDE_INT);
15565 else
15566 for (i = 0; i < n; i++)
15568 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15569 dest += sizeof (HOST_WIDE_INT);
15573 /* Writes floating point values to dw_vec_const array. */
15575 static void
15576 insert_float (const_rtx rtl, unsigned char *array)
15578 long val[4];
15579 int i;
15581 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
15583 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15584 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15586 insert_int (val[i], 4, array);
15587 array += 4;
15591 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15592 does not have a "location" either in memory or in a register. These
15593 things can arise in GNU C when a constant is passed as an actual parameter
15594 to an inlined function. They can also arise in C++ where declared
15595 constants do not necessarily get memory "homes". */
15597 static bool
15598 add_const_value_attribute (dw_die_ref die, rtx rtl)
15600 switch (GET_CODE (rtl))
15602 case CONST_INT:
15604 HOST_WIDE_INT val = INTVAL (rtl);
15606 if (val < 0)
15607 add_AT_int (die, DW_AT_const_value, val);
15608 else
15609 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15611 return true;
15613 case CONST_WIDE_INT:
15614 add_AT_wide (die, DW_AT_const_value,
15615 std::make_pair (rtl, GET_MODE (rtl)));
15616 return true;
15618 case CONST_DOUBLE:
15619 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15620 floating-point constant. A CONST_DOUBLE is used whenever the
15621 constant requires more than one word in order to be adequately
15622 represented. */
15624 machine_mode mode = GET_MODE (rtl);
15626 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15627 add_AT_double (die, DW_AT_const_value,
15628 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15629 else
15631 unsigned int length = GET_MODE_SIZE (mode);
15632 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15634 insert_float (rtl, array);
15635 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15638 return true;
15640 case CONST_VECTOR:
15642 machine_mode mode = GET_MODE (rtl);
15643 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15644 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15645 unsigned char *array
15646 = ggc_vec_alloc<unsigned char> (length * elt_size);
15647 unsigned int i;
15648 unsigned char *p;
15649 machine_mode imode = GET_MODE_INNER (mode);
15651 switch (GET_MODE_CLASS (mode))
15653 case MODE_VECTOR_INT:
15654 for (i = 0, p = array; i < length; i++, p += elt_size)
15656 rtx elt = CONST_VECTOR_ELT (rtl, i);
15657 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15659 break;
15661 case MODE_VECTOR_FLOAT:
15662 for (i = 0, p = array; i < length; i++, p += elt_size)
15664 rtx elt = CONST_VECTOR_ELT (rtl, i);
15665 insert_float (elt, p);
15667 break;
15669 default:
15670 gcc_unreachable ();
15673 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15675 return true;
15677 case CONST_STRING:
15678 if (dwarf_version >= 4 || !dwarf_strict)
15680 dw_loc_descr_ref loc_result;
15681 resolve_one_addr (&rtl);
15682 rtl_addr:
15683 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15684 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15685 add_AT_loc (die, DW_AT_location, loc_result);
15686 vec_safe_push (used_rtx_array, rtl);
15687 return true;
15689 return false;
15691 case CONST:
15692 if (CONSTANT_P (XEXP (rtl, 0)))
15693 return add_const_value_attribute (die, XEXP (rtl, 0));
15694 /* FALLTHROUGH */
15695 case SYMBOL_REF:
15696 if (!const_ok_for_output (rtl))
15697 return false;
15698 case LABEL_REF:
15699 if (dwarf_version >= 4 || !dwarf_strict)
15700 goto rtl_addr;
15701 return false;
15703 case PLUS:
15704 /* In cases where an inlined instance of an inline function is passed
15705 the address of an `auto' variable (which is local to the caller) we
15706 can get a situation where the DECL_RTL of the artificial local
15707 variable (for the inlining) which acts as a stand-in for the
15708 corresponding formal parameter (of the inline function) will look
15709 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15710 exactly a compile-time constant expression, but it isn't the address
15711 of the (artificial) local variable either. Rather, it represents the
15712 *value* which the artificial local variable always has during its
15713 lifetime. We currently have no way to represent such quasi-constant
15714 values in Dwarf, so for now we just punt and generate nothing. */
15715 return false;
15717 case HIGH:
15718 case CONST_FIXED:
15719 return false;
15721 case MEM:
15722 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15723 && MEM_READONLY_P (rtl)
15724 && GET_MODE (rtl) == BLKmode)
15726 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15727 return true;
15729 return false;
15731 default:
15732 /* No other kinds of rtx should be possible here. */
15733 gcc_unreachable ();
15735 return false;
15738 /* Determine whether the evaluation of EXPR references any variables
15739 or functions which aren't otherwise used (and therefore may not be
15740 output). */
15741 static tree
15742 reference_to_unused (tree * tp, int * walk_subtrees,
15743 void * data ATTRIBUTE_UNUSED)
15745 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15746 *walk_subtrees = 0;
15748 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15749 && ! TREE_ASM_WRITTEN (*tp))
15750 return *tp;
15751 /* ??? The C++ FE emits debug information for using decls, so
15752 putting gcc_unreachable here falls over. See PR31899. For now
15753 be conservative. */
15754 else if (!symtab->global_info_ready
15755 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15756 return *tp;
15757 else if (TREE_CODE (*tp) == VAR_DECL)
15759 varpool_node *node = varpool_node::get (*tp);
15760 if (!node || !node->definition)
15761 return *tp;
15763 else if (TREE_CODE (*tp) == FUNCTION_DECL
15764 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15766 /* The call graph machinery must have finished analyzing,
15767 optimizing and gimplifying the CU by now.
15768 So if *TP has no call graph node associated
15769 to it, it means *TP will not be emitted. */
15770 if (!cgraph_node::get (*tp))
15771 return *tp;
15773 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15774 return *tp;
15776 return NULL_TREE;
15779 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15780 for use in a later add_const_value_attribute call. */
15782 static rtx
15783 rtl_for_decl_init (tree init, tree type)
15785 rtx rtl = NULL_RTX;
15787 STRIP_NOPS (init);
15789 /* If a variable is initialized with a string constant without embedded
15790 zeros, build CONST_STRING. */
15791 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15793 tree enttype = TREE_TYPE (type);
15794 tree domain = TYPE_DOMAIN (type);
15795 machine_mode mode = TYPE_MODE (enttype);
15797 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15798 && domain
15799 && integer_zerop (TYPE_MIN_VALUE (domain))
15800 && compare_tree_int (TYPE_MAX_VALUE (domain),
15801 TREE_STRING_LENGTH (init) - 1) == 0
15802 && ((size_t) TREE_STRING_LENGTH (init)
15803 == strlen (TREE_STRING_POINTER (init)) + 1))
15805 rtl = gen_rtx_CONST_STRING (VOIDmode,
15806 ggc_strdup (TREE_STRING_POINTER (init)));
15807 rtl = gen_rtx_MEM (BLKmode, rtl);
15808 MEM_READONLY_P (rtl) = 1;
15811 /* Other aggregates, and complex values, could be represented using
15812 CONCAT: FIXME! */
15813 else if (AGGREGATE_TYPE_P (type)
15814 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15815 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15816 || TREE_CODE (type) == COMPLEX_TYPE)
15818 /* Vectors only work if their mode is supported by the target.
15819 FIXME: generic vectors ought to work too. */
15820 else if (TREE_CODE (type) == VECTOR_TYPE
15821 && !VECTOR_MODE_P (TYPE_MODE (type)))
15823 /* If the initializer is something that we know will expand into an
15824 immediate RTL constant, expand it now. We must be careful not to
15825 reference variables which won't be output. */
15826 else if (initializer_constant_valid_p (init, type)
15827 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15829 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15830 possible. */
15831 if (TREE_CODE (type) == VECTOR_TYPE)
15832 switch (TREE_CODE (init))
15834 case VECTOR_CST:
15835 break;
15836 case CONSTRUCTOR:
15837 if (TREE_CONSTANT (init))
15839 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15840 bool constant_p = true;
15841 tree value;
15842 unsigned HOST_WIDE_INT ix;
15844 /* Even when ctor is constant, it might contain non-*_CST
15845 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15846 belong into VECTOR_CST nodes. */
15847 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15848 if (!CONSTANT_CLASS_P (value))
15850 constant_p = false;
15851 break;
15854 if (constant_p)
15856 init = build_vector_from_ctor (type, elts);
15857 break;
15860 /* FALLTHRU */
15862 default:
15863 return NULL;
15866 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15868 /* If expand_expr returns a MEM, it wasn't immediate. */
15869 gcc_assert (!rtl || !MEM_P (rtl));
15872 return rtl;
15875 /* Generate RTL for the variable DECL to represent its location. */
15877 static rtx
15878 rtl_for_decl_location (tree decl)
15880 rtx rtl;
15882 /* Here we have to decide where we are going to say the parameter "lives"
15883 (as far as the debugger is concerned). We only have a couple of
15884 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15886 DECL_RTL normally indicates where the parameter lives during most of the
15887 activation of the function. If optimization is enabled however, this
15888 could be either NULL or else a pseudo-reg. Both of those cases indicate
15889 that the parameter doesn't really live anywhere (as far as the code
15890 generation parts of GCC are concerned) during most of the function's
15891 activation. That will happen (for example) if the parameter is never
15892 referenced within the function.
15894 We could just generate a location descriptor here for all non-NULL
15895 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15896 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15897 where DECL_RTL is NULL or is a pseudo-reg.
15899 Note however that we can only get away with using DECL_INCOMING_RTL as
15900 a backup substitute for DECL_RTL in certain limited cases. In cases
15901 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15902 we can be sure that the parameter was passed using the same type as it is
15903 declared to have within the function, and that its DECL_INCOMING_RTL
15904 points us to a place where a value of that type is passed.
15906 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15907 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15908 because in these cases DECL_INCOMING_RTL points us to a value of some
15909 type which is *different* from the type of the parameter itself. Thus,
15910 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15911 such cases, the debugger would end up (for example) trying to fetch a
15912 `float' from a place which actually contains the first part of a
15913 `double'. That would lead to really incorrect and confusing
15914 output at debug-time.
15916 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15917 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15918 are a couple of exceptions however. On little-endian machines we can
15919 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15920 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15921 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15922 when (on a little-endian machine) a non-prototyped function has a
15923 parameter declared to be of type `short' or `char'. In such cases,
15924 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15925 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15926 passed `int' value. If the debugger then uses that address to fetch
15927 a `short' or a `char' (on a little-endian machine) the result will be
15928 the correct data, so we allow for such exceptional cases below.
15930 Note that our goal here is to describe the place where the given formal
15931 parameter lives during most of the function's activation (i.e. between the
15932 end of the prologue and the start of the epilogue). We'll do that as best
15933 as we can. Note however that if the given formal parameter is modified
15934 sometime during the execution of the function, then a stack backtrace (at
15935 debug-time) will show the function as having been called with the *new*
15936 value rather than the value which was originally passed in. This happens
15937 rarely enough that it is not a major problem, but it *is* a problem, and
15938 I'd like to fix it.
15940 A future version of dwarf2out.c may generate two additional attributes for
15941 any given DW_TAG_formal_parameter DIE which will describe the "passed
15942 type" and the "passed location" for the given formal parameter in addition
15943 to the attributes we now generate to indicate the "declared type" and the
15944 "active location" for each parameter. This additional set of attributes
15945 could be used by debuggers for stack backtraces. Separately, note that
15946 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15947 This happens (for example) for inlined-instances of inline function formal
15948 parameters which are never referenced. This really shouldn't be
15949 happening. All PARM_DECL nodes should get valid non-NULL
15950 DECL_INCOMING_RTL values. FIXME. */
15952 /* Use DECL_RTL as the "location" unless we find something better. */
15953 rtl = DECL_RTL_IF_SET (decl);
15955 /* When generating abstract instances, ignore everything except
15956 constants, symbols living in memory, and symbols living in
15957 fixed registers. */
15958 if (! reload_completed)
15960 if (rtl
15961 && (CONSTANT_P (rtl)
15962 || (MEM_P (rtl)
15963 && CONSTANT_P (XEXP (rtl, 0)))
15964 || (REG_P (rtl)
15965 && TREE_CODE (decl) == VAR_DECL
15966 && TREE_STATIC (decl))))
15968 rtl = targetm.delegitimize_address (rtl);
15969 return rtl;
15971 rtl = NULL_RTX;
15973 else if (TREE_CODE (decl) == PARM_DECL)
15975 if (rtl == NULL_RTX
15976 || is_pseudo_reg (rtl)
15977 || (MEM_P (rtl)
15978 && is_pseudo_reg (XEXP (rtl, 0))
15979 && DECL_INCOMING_RTL (decl)
15980 && MEM_P (DECL_INCOMING_RTL (decl))
15981 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15983 tree declared_type = TREE_TYPE (decl);
15984 tree passed_type = DECL_ARG_TYPE (decl);
15985 machine_mode dmode = TYPE_MODE (declared_type);
15986 machine_mode pmode = TYPE_MODE (passed_type);
15988 /* This decl represents a formal parameter which was optimized out.
15989 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15990 all cases where (rtl == NULL_RTX) just below. */
15991 if (dmode == pmode)
15992 rtl = DECL_INCOMING_RTL (decl);
15993 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15994 && SCALAR_INT_MODE_P (dmode)
15995 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15996 && DECL_INCOMING_RTL (decl))
15998 rtx inc = DECL_INCOMING_RTL (decl);
15999 if (REG_P (inc))
16000 rtl = inc;
16001 else if (MEM_P (inc))
16003 if (BYTES_BIG_ENDIAN)
16004 rtl = adjust_address_nv (inc, dmode,
16005 GET_MODE_SIZE (pmode)
16006 - GET_MODE_SIZE (dmode));
16007 else
16008 rtl = inc;
16013 /* If the parm was passed in registers, but lives on the stack, then
16014 make a big endian correction if the mode of the type of the
16015 parameter is not the same as the mode of the rtl. */
16016 /* ??? This is the same series of checks that are made in dbxout.c before
16017 we reach the big endian correction code there. It isn't clear if all
16018 of these checks are necessary here, but keeping them all is the safe
16019 thing to do. */
16020 else if (MEM_P (rtl)
16021 && XEXP (rtl, 0) != const0_rtx
16022 && ! CONSTANT_P (XEXP (rtl, 0))
16023 /* Not passed in memory. */
16024 && !MEM_P (DECL_INCOMING_RTL (decl))
16025 /* Not passed by invisible reference. */
16026 && (!REG_P (XEXP (rtl, 0))
16027 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16028 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16029 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16030 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16031 #endif
16033 /* Big endian correction check. */
16034 && BYTES_BIG_ENDIAN
16035 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16036 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16037 < UNITS_PER_WORD))
16039 machine_mode addr_mode = get_address_mode (rtl);
16040 int offset = (UNITS_PER_WORD
16041 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16043 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16044 plus_constant (addr_mode, XEXP (rtl, 0), offset));
16047 else if (TREE_CODE (decl) == VAR_DECL
16048 && rtl
16049 && MEM_P (rtl)
16050 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16051 && BYTES_BIG_ENDIAN)
16053 machine_mode addr_mode = get_address_mode (rtl);
16054 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16055 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16057 /* If a variable is declared "register" yet is smaller than
16058 a register, then if we store the variable to memory, it
16059 looks like we're storing a register-sized value, when in
16060 fact we are not. We need to adjust the offset of the
16061 storage location to reflect the actual value's bytes,
16062 else gdb will not be able to display it. */
16063 if (rsize > dsize)
16064 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16065 plus_constant (addr_mode, XEXP (rtl, 0),
16066 rsize - dsize));
16069 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16070 and will have been substituted directly into all expressions that use it.
16071 C does not have such a concept, but C++ and other languages do. */
16072 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16073 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16075 if (rtl)
16076 rtl = targetm.delegitimize_address (rtl);
16078 /* If we don't look past the constant pool, we risk emitting a
16079 reference to a constant pool entry that isn't referenced from
16080 code, and thus is not emitted. */
16081 if (rtl)
16082 rtl = avoid_constant_pool_reference (rtl);
16084 /* Try harder to get a rtl. If this symbol ends up not being emitted
16085 in the current CU, resolve_addr will remove the expression referencing
16086 it. */
16087 if (rtl == NULL_RTX
16088 && TREE_CODE (decl) == VAR_DECL
16089 && !DECL_EXTERNAL (decl)
16090 && TREE_STATIC (decl)
16091 && DECL_NAME (decl)
16092 && !DECL_HARD_REGISTER (decl)
16093 && DECL_MODE (decl) != VOIDmode)
16095 rtl = make_decl_rtl_for_debug (decl);
16096 if (!MEM_P (rtl)
16097 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16098 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16099 rtl = NULL_RTX;
16102 return rtl;
16105 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16106 returned. If so, the decl for the COMMON block is returned, and the
16107 value is the offset into the common block for the symbol. */
16109 static tree
16110 fortran_common (tree decl, HOST_WIDE_INT *value)
16112 tree val_expr, cvar;
16113 machine_mode mode;
16114 HOST_WIDE_INT bitsize, bitpos;
16115 tree offset;
16116 int unsignedp, volatilep = 0;
16118 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16119 it does not have a value (the offset into the common area), or if it
16120 is thread local (as opposed to global) then it isn't common, and shouldn't
16121 be handled as such. */
16122 if (TREE_CODE (decl) != VAR_DECL
16123 || !TREE_STATIC (decl)
16124 || !DECL_HAS_VALUE_EXPR_P (decl)
16125 || !is_fortran ())
16126 return NULL_TREE;
16128 val_expr = DECL_VALUE_EXPR (decl);
16129 if (TREE_CODE (val_expr) != COMPONENT_REF)
16130 return NULL_TREE;
16132 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16133 &mode, &unsignedp, &volatilep, true);
16135 if (cvar == NULL_TREE
16136 || TREE_CODE (cvar) != VAR_DECL
16137 || DECL_ARTIFICIAL (cvar)
16138 || !TREE_PUBLIC (cvar))
16139 return NULL_TREE;
16141 *value = 0;
16142 if (offset != NULL)
16144 if (!tree_fits_shwi_p (offset))
16145 return NULL_TREE;
16146 *value = tree_to_shwi (offset);
16148 if (bitpos != 0)
16149 *value += bitpos / BITS_PER_UNIT;
16151 return cvar;
16154 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16155 data attribute for a variable or a parameter. We generate the
16156 DW_AT_const_value attribute only in those cases where the given variable
16157 or parameter does not have a true "location" either in memory or in a
16158 register. This can happen (for example) when a constant is passed as an
16159 actual argument in a call to an inline function. (It's possible that
16160 these things can crop up in other ways also.) Note that one type of
16161 constant value which can be passed into an inlined function is a constant
16162 pointer. This can happen for example if an actual argument in an inlined
16163 function call evaluates to a compile-time constant address.
16165 CACHE_P is true if it is worth caching the location list for DECL,
16166 so that future calls can reuse it rather than regenerate it from scratch.
16167 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16168 since we will need to refer to them each time the function is inlined. */
16170 static bool
16171 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
16173 rtx rtl;
16174 dw_loc_list_ref list;
16175 var_loc_list *loc_list;
16176 cached_dw_loc_list *cache;
16178 if (early_dwarf)
16179 return false;
16181 if (TREE_CODE (decl) == ERROR_MARK)
16182 return false;
16184 if (get_AT (die, DW_AT_location)
16185 || get_AT (die, DW_AT_const_value))
16186 return true;
16188 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16189 || TREE_CODE (decl) == RESULT_DECL);
16191 /* Try to get some constant RTL for this decl, and use that as the value of
16192 the location. */
16194 rtl = rtl_for_decl_location (decl);
16195 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16196 && add_const_value_attribute (die, rtl))
16197 return true;
16199 /* See if we have single element location list that is equivalent to
16200 a constant value. That way we are better to use add_const_value_attribute
16201 rather than expanding constant value equivalent. */
16202 loc_list = lookup_decl_loc (decl);
16203 if (loc_list
16204 && loc_list->first
16205 && loc_list->first->next == NULL
16206 && NOTE_P (loc_list->first->loc)
16207 && NOTE_VAR_LOCATION (loc_list->first->loc)
16208 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16210 struct var_loc_node *node;
16212 node = loc_list->first;
16213 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16214 if (GET_CODE (rtl) == EXPR_LIST)
16215 rtl = XEXP (rtl, 0);
16216 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16217 && add_const_value_attribute (die, rtl))
16218 return true;
16220 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16221 list several times. See if we've already cached the contents. */
16222 list = NULL;
16223 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16224 cache_p = false;
16225 if (cache_p)
16227 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16228 if (cache)
16229 list = cache->loc_list;
16231 if (list == NULL)
16233 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16234 NULL);
16235 /* It is usually worth caching this result if the decl is from
16236 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16237 if (cache_p && list && list->dw_loc_next)
16239 cached_dw_loc_list **slot
16240 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16241 DECL_UID (decl),
16242 INSERT);
16243 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16244 cache->decl_id = DECL_UID (decl);
16245 cache->loc_list = list;
16246 *slot = cache;
16249 if (list)
16251 add_AT_location_description (die, DW_AT_location, list);
16252 return true;
16254 /* None of that worked, so it must not really have a location;
16255 try adding a constant value attribute from the DECL_INITIAL. */
16256 return tree_add_const_value_attribute_for_decl (die, decl);
16259 /* Helper function for tree_add_const_value_attribute. Natively encode
16260 initializer INIT into an array. Return true if successful. */
16262 static bool
16263 native_encode_initializer (tree init, unsigned char *array, int size)
16265 tree type;
16267 if (init == NULL_TREE)
16268 return false;
16270 STRIP_NOPS (init);
16271 switch (TREE_CODE (init))
16273 case STRING_CST:
16274 type = TREE_TYPE (init);
16275 if (TREE_CODE (type) == ARRAY_TYPE)
16277 tree enttype = TREE_TYPE (type);
16278 machine_mode mode = TYPE_MODE (enttype);
16280 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16281 return false;
16282 if (int_size_in_bytes (type) != size)
16283 return false;
16284 if (size > TREE_STRING_LENGTH (init))
16286 memcpy (array, TREE_STRING_POINTER (init),
16287 TREE_STRING_LENGTH (init));
16288 memset (array + TREE_STRING_LENGTH (init),
16289 '\0', size - TREE_STRING_LENGTH (init));
16291 else
16292 memcpy (array, TREE_STRING_POINTER (init), size);
16293 return true;
16295 return false;
16296 case CONSTRUCTOR:
16297 type = TREE_TYPE (init);
16298 if (int_size_in_bytes (type) != size)
16299 return false;
16300 if (TREE_CODE (type) == ARRAY_TYPE)
16302 HOST_WIDE_INT min_index;
16303 unsigned HOST_WIDE_INT cnt;
16304 int curpos = 0, fieldsize;
16305 constructor_elt *ce;
16307 if (TYPE_DOMAIN (type) == NULL_TREE
16308 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16309 return false;
16311 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16312 if (fieldsize <= 0)
16313 return false;
16315 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16316 memset (array, '\0', size);
16317 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16319 tree val = ce->value;
16320 tree index = ce->index;
16321 int pos = curpos;
16322 if (index && TREE_CODE (index) == RANGE_EXPR)
16323 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16324 * fieldsize;
16325 else if (index)
16326 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16328 if (val)
16330 STRIP_NOPS (val);
16331 if (!native_encode_initializer (val, array + pos, fieldsize))
16332 return false;
16334 curpos = pos + fieldsize;
16335 if (index && TREE_CODE (index) == RANGE_EXPR)
16337 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16338 - tree_to_shwi (TREE_OPERAND (index, 0));
16339 while (count-- > 0)
16341 if (val)
16342 memcpy (array + curpos, array + pos, fieldsize);
16343 curpos += fieldsize;
16346 gcc_assert (curpos <= size);
16348 return true;
16350 else if (TREE_CODE (type) == RECORD_TYPE
16351 || TREE_CODE (type) == UNION_TYPE)
16353 tree field = NULL_TREE;
16354 unsigned HOST_WIDE_INT cnt;
16355 constructor_elt *ce;
16357 if (int_size_in_bytes (type) != size)
16358 return false;
16360 if (TREE_CODE (type) == RECORD_TYPE)
16361 field = TYPE_FIELDS (type);
16363 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16365 tree val = ce->value;
16366 int pos, fieldsize;
16368 if (ce->index != 0)
16369 field = ce->index;
16371 if (val)
16372 STRIP_NOPS (val);
16374 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16375 return false;
16377 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16378 && TYPE_DOMAIN (TREE_TYPE (field))
16379 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16380 return false;
16381 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16382 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16383 return false;
16384 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16385 pos = int_byte_position (field);
16386 gcc_assert (pos + fieldsize <= size);
16387 if (val
16388 && !native_encode_initializer (val, array + pos, fieldsize))
16389 return false;
16391 return true;
16393 return false;
16394 case VIEW_CONVERT_EXPR:
16395 case NON_LVALUE_EXPR:
16396 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16397 default:
16398 return native_encode_expr (init, array, size) == size;
16402 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16403 attribute is the const value T. */
16405 static bool
16406 tree_add_const_value_attribute (dw_die_ref die, tree t)
16408 tree init;
16409 tree type = TREE_TYPE (t);
16410 rtx rtl;
16412 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16413 return false;
16415 init = t;
16416 gcc_assert (!DECL_P (init));
16418 rtl = rtl_for_decl_init (init, type);
16419 if (rtl)
16420 return add_const_value_attribute (die, rtl);
16421 /* If the host and target are sane, try harder. */
16422 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16423 && initializer_constant_valid_p (init, type))
16425 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16426 if (size > 0 && (int) size == size)
16428 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16430 if (native_encode_initializer (init, array, size))
16432 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16433 return true;
16435 ggc_free (array);
16438 return false;
16441 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16442 attribute is the const value of T, where T is an integral constant
16443 variable with static storage duration
16444 (so it can't be a PARM_DECL or a RESULT_DECL). */
16446 static bool
16447 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16450 if (!decl
16451 || (TREE_CODE (decl) != VAR_DECL
16452 && TREE_CODE (decl) != CONST_DECL)
16453 || (TREE_CODE (decl) == VAR_DECL
16454 && !TREE_STATIC (decl)))
16455 return false;
16457 if (TREE_READONLY (decl)
16458 && ! TREE_THIS_VOLATILE (decl)
16459 && DECL_INITIAL (decl))
16460 /* OK */;
16461 else
16462 return false;
16464 /* Don't add DW_AT_const_value if abstract origin already has one. */
16465 if (get_AT (var_die, DW_AT_const_value))
16466 return false;
16468 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16471 /* Convert the CFI instructions for the current function into a
16472 location list. This is used for DW_AT_frame_base when we targeting
16473 a dwarf2 consumer that does not support the dwarf3
16474 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16475 expressions. */
16477 static dw_loc_list_ref
16478 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16480 int ix;
16481 dw_fde_ref fde;
16482 dw_loc_list_ref list, *list_tail;
16483 dw_cfi_ref cfi;
16484 dw_cfa_location last_cfa, next_cfa;
16485 const char *start_label, *last_label, *section;
16486 dw_cfa_location remember;
16488 fde = cfun->fde;
16489 gcc_assert (fde != NULL);
16491 section = secname_for_decl (current_function_decl);
16492 list_tail = &list;
16493 list = NULL;
16495 memset (&next_cfa, 0, sizeof (next_cfa));
16496 next_cfa.reg = INVALID_REGNUM;
16497 remember = next_cfa;
16499 start_label = fde->dw_fde_begin;
16501 /* ??? Bald assumption that the CIE opcode list does not contain
16502 advance opcodes. */
16503 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16504 lookup_cfa_1 (cfi, &next_cfa, &remember);
16506 last_cfa = next_cfa;
16507 last_label = start_label;
16509 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16511 /* If the first partition contained no CFI adjustments, the
16512 CIE opcodes apply to the whole first partition. */
16513 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16514 fde->dw_fde_begin, fde->dw_fde_end, section);
16515 list_tail =&(*list_tail)->dw_loc_next;
16516 start_label = last_label = fde->dw_fde_second_begin;
16519 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16521 switch (cfi->dw_cfi_opc)
16523 case DW_CFA_set_loc:
16524 case DW_CFA_advance_loc1:
16525 case DW_CFA_advance_loc2:
16526 case DW_CFA_advance_loc4:
16527 if (!cfa_equal_p (&last_cfa, &next_cfa))
16529 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16530 start_label, last_label, section);
16532 list_tail = &(*list_tail)->dw_loc_next;
16533 last_cfa = next_cfa;
16534 start_label = last_label;
16536 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16537 break;
16539 case DW_CFA_advance_loc:
16540 /* The encoding is complex enough that we should never emit this. */
16541 gcc_unreachable ();
16543 default:
16544 lookup_cfa_1 (cfi, &next_cfa, &remember);
16545 break;
16547 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16549 if (!cfa_equal_p (&last_cfa, &next_cfa))
16551 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16552 start_label, last_label, section);
16554 list_tail = &(*list_tail)->dw_loc_next;
16555 last_cfa = next_cfa;
16556 start_label = last_label;
16558 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16559 start_label, fde->dw_fde_end, section);
16560 list_tail = &(*list_tail)->dw_loc_next;
16561 start_label = last_label = fde->dw_fde_second_begin;
16565 if (!cfa_equal_p (&last_cfa, &next_cfa))
16567 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16568 start_label, last_label, section);
16569 list_tail = &(*list_tail)->dw_loc_next;
16570 start_label = last_label;
16573 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16574 start_label,
16575 fde->dw_fde_second_begin
16576 ? fde->dw_fde_second_end : fde->dw_fde_end,
16577 section);
16579 if (list && list->dw_loc_next)
16580 gen_llsym (list);
16582 return list;
16585 /* Compute a displacement from the "steady-state frame pointer" to the
16586 frame base (often the same as the CFA), and store it in
16587 frame_pointer_fb_offset. OFFSET is added to the displacement
16588 before the latter is negated. */
16590 static void
16591 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16593 rtx reg, elim;
16595 #ifdef FRAME_POINTER_CFA_OFFSET
16596 reg = frame_pointer_rtx;
16597 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16598 #else
16599 reg = arg_pointer_rtx;
16600 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16601 #endif
16603 elim = (ira_use_lra_p
16604 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16605 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16606 if (GET_CODE (elim) == PLUS)
16608 offset += INTVAL (XEXP (elim, 1));
16609 elim = XEXP (elim, 0);
16612 frame_pointer_fb_offset = -offset;
16614 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16615 in which to eliminate. This is because it's stack pointer isn't
16616 directly accessible as a register within the ISA. To work around
16617 this, assume that while we cannot provide a proper value for
16618 frame_pointer_fb_offset, we won't need one either. */
16619 frame_pointer_fb_offset_valid
16620 = ((SUPPORTS_STACK_ALIGNMENT
16621 && (elim == hard_frame_pointer_rtx
16622 || elim == stack_pointer_rtx))
16623 || elim == (frame_pointer_needed
16624 ? hard_frame_pointer_rtx
16625 : stack_pointer_rtx));
16628 /* Generate a DW_AT_name attribute given some string value to be included as
16629 the value of the attribute. */
16631 static void
16632 add_name_attribute (dw_die_ref die, const char *name_string)
16634 if (name_string != NULL && *name_string != 0)
16636 if (demangle_name_func)
16637 name_string = (*demangle_name_func) (name_string);
16639 add_AT_string (die, DW_AT_name, name_string);
16643 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16644 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16645 of TYPE accordingly.
16647 ??? This is a temporary measure until after we're able to generate
16648 regular DWARF for the complex Ada type system. */
16650 static void
16651 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16652 dw_die_ref context_die)
16654 tree dtype;
16655 dw_die_ref dtype_die;
16657 if (!lang_hooks.types.descriptive_type)
16658 return;
16660 dtype = lang_hooks.types.descriptive_type (type);
16661 if (!dtype)
16662 return;
16664 dtype_die = lookup_type_die (dtype);
16665 if (!dtype_die)
16667 gen_type_die (dtype, context_die);
16668 dtype_die = lookup_type_die (dtype);
16669 gcc_assert (dtype_die);
16672 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16675 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16677 static const char *
16678 comp_dir_string (void)
16680 const char *wd;
16681 char *wd1;
16682 static const char *cached_wd = NULL;
16684 if (cached_wd != NULL)
16685 return cached_wd;
16687 wd = get_src_pwd ();
16688 if (wd == NULL)
16689 return NULL;
16691 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16693 int wdlen;
16695 wdlen = strlen (wd);
16696 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16697 strcpy (wd1, wd);
16698 wd1 [wdlen] = DIR_SEPARATOR;
16699 wd1 [wdlen + 1] = 0;
16700 wd = wd1;
16703 cached_wd = remap_debug_filename (wd);
16704 return cached_wd;
16707 /* Generate a DW_AT_comp_dir attribute for DIE. */
16709 static void
16710 add_comp_dir_attribute (dw_die_ref die)
16712 const char * wd = comp_dir_string ();
16713 if (wd != NULL)
16714 add_AT_string (die, DW_AT_comp_dir, wd);
16717 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16718 pointer computation, ...), output a representation for that bound according
16719 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16720 loc_list_from_tree for the meaning of CONTEXT. */
16722 static void
16723 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16724 int forms, const struct loc_descr_context *context)
16726 dw_die_ref ctx, decl_die;
16727 dw_loc_list_ref list;
16729 bool strip_conversions = true;
16731 while (strip_conversions)
16732 switch (TREE_CODE (value))
16734 case ERROR_MARK:
16735 case SAVE_EXPR:
16736 return;
16738 CASE_CONVERT:
16739 case VIEW_CONVERT_EXPR:
16740 value = TREE_OPERAND (value, 0);
16741 break;
16743 default:
16744 strip_conversions = false;
16745 break;
16748 /* If possible and permitted, output the attribute as a constant. */
16749 if ((forms & dw_scalar_form_constant) != 0
16750 && TREE_CODE (value) == INTEGER_CST)
16752 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16754 /* If HOST_WIDE_INT is big enough then represent the bound as
16755 a constant value. We need to choose a form based on
16756 whether the type is signed or unsigned. We cannot just
16757 call add_AT_unsigned if the value itself is positive
16758 (add_AT_unsigned might add the unsigned value encoded as
16759 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16760 bounds type and then sign extend any unsigned values found
16761 for signed types. This is needed only for
16762 DW_AT_{lower,upper}_bound, since for most other attributes,
16763 consumers will treat DW_FORM_data[1248] as unsigned values,
16764 regardless of the underlying type. */
16765 if (prec <= HOST_BITS_PER_WIDE_INT
16766 || tree_fits_uhwi_p (value))
16768 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16769 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16770 else
16771 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16773 else
16774 /* Otherwise represent the bound as an unsigned value with
16775 the precision of its type. The precision and signedness
16776 of the type will be necessary to re-interpret it
16777 unambiguously. */
16778 add_AT_wide (die, attr, value);
16779 return;
16782 /* Otherwise, if it's possible and permitted too, output a reference to
16783 another DIE. */
16784 if ((forms & dw_scalar_form_reference) != 0)
16786 tree decl = NULL_TREE;
16788 /* Some type attributes reference an outer type. For instance, the upper
16789 bound of an array may reference an embedding record (this happens in
16790 Ada). */
16791 if (TREE_CODE (value) == COMPONENT_REF
16792 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16793 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16794 decl = TREE_OPERAND (value, 1);
16796 else if (TREE_CODE (value) == VAR_DECL
16797 || TREE_CODE (value) == PARM_DECL
16798 || TREE_CODE (value) == RESULT_DECL)
16799 decl = value;
16801 if (decl != NULL_TREE)
16803 dw_die_ref decl_die = lookup_decl_die (decl);
16805 /* ??? Can this happen, or should the variable have been bound
16806 first? Probably it can, since I imagine that we try to create
16807 the types of parameters in the order in which they exist in
16808 the list, and won't have created a forward reference to a
16809 later parameter. */
16810 if (decl_die != NULL)
16812 add_AT_die_ref (die, attr, decl_die);
16813 return;
16818 /* Last chance: try to create a stack operation procedure to evaluate the
16819 value. Do nothing if even that is not possible or permitted. */
16820 if ((forms & dw_scalar_form_exprloc) == 0)
16821 return;
16823 list = loc_list_from_tree (value, 2, context);
16824 if (list == NULL || single_element_loc_list_p (list))
16826 /* If this attribute is not a reference nor constant, it is
16827 a DWARF expression rather than location description. For that
16828 loc_list_from_tree (value, 0, &context) is needed. */
16829 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16830 if (list2 && single_element_loc_list_p (list2))
16832 add_AT_loc (die, attr, list2->expr);
16833 return;
16837 /* If that failed to give a single element location list, fall back to
16838 outputting this as a reference... still if permitted. */
16839 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16840 return;
16842 if (current_function_decl == 0)
16843 ctx = comp_unit_die ();
16844 else
16845 ctx = lookup_decl_die (current_function_decl);
16847 decl_die = new_die (DW_TAG_variable, ctx, value);
16848 add_AT_flag (decl_die, DW_AT_artificial, 1);
16849 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16850 add_AT_location_description (decl_die, DW_AT_location, list);
16851 add_AT_die_ref (die, attr, decl_die);
16854 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16855 default. */
16857 static int
16858 lower_bound_default (void)
16860 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16862 case DW_LANG_C:
16863 case DW_LANG_C89:
16864 case DW_LANG_C99:
16865 case DW_LANG_C11:
16866 case DW_LANG_C_plus_plus:
16867 case DW_LANG_C_plus_plus_11:
16868 case DW_LANG_C_plus_plus_14:
16869 case DW_LANG_ObjC:
16870 case DW_LANG_ObjC_plus_plus:
16871 case DW_LANG_Java:
16872 return 0;
16873 case DW_LANG_Fortran77:
16874 case DW_LANG_Fortran90:
16875 case DW_LANG_Fortran95:
16876 case DW_LANG_Fortran03:
16877 case DW_LANG_Fortran08:
16878 return 1;
16879 case DW_LANG_UPC:
16880 case DW_LANG_D:
16881 case DW_LANG_Python:
16882 return dwarf_version >= 4 ? 0 : -1;
16883 case DW_LANG_Ada95:
16884 case DW_LANG_Ada83:
16885 case DW_LANG_Cobol74:
16886 case DW_LANG_Cobol85:
16887 case DW_LANG_Pascal83:
16888 case DW_LANG_Modula2:
16889 case DW_LANG_PLI:
16890 return dwarf_version >= 4 ? 1 : -1;
16891 default:
16892 return -1;
16896 /* Given a tree node describing an array bound (either lower or upper) output
16897 a representation for that bound. */
16899 static void
16900 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16901 tree bound, const struct loc_descr_context *context)
16903 int dflt;
16905 while (1)
16906 switch (TREE_CODE (bound))
16908 /* Strip all conversions. */
16909 CASE_CONVERT:
16910 case VIEW_CONVERT_EXPR:
16911 bound = TREE_OPERAND (bound, 0);
16912 break;
16914 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16915 are even omitted when they are the default. */
16916 case INTEGER_CST:
16917 /* If the value for this bound is the default one, we can even omit the
16918 attribute. */
16919 if (bound_attr == DW_AT_lower_bound
16920 && tree_fits_shwi_p (bound)
16921 && (dflt = lower_bound_default ()) != -1
16922 && tree_to_shwi (bound) == dflt)
16923 return;
16925 /* FALLTHRU */
16927 default:
16928 add_scalar_info (subrange_die, bound_attr, bound,
16929 dw_scalar_form_constant
16930 | dw_scalar_form_exprloc
16931 | dw_scalar_form_reference,
16932 context);
16933 return;
16937 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16938 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16939 Note that the block of subscript information for an array type also
16940 includes information about the element type of the given array type.
16942 This function reuses previously set type and bound information if
16943 available. */
16945 static void
16946 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16948 unsigned dimension_number;
16949 tree lower, upper;
16950 dw_die_ref child = type_die->die_child;
16952 for (dimension_number = 0;
16953 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16954 type = TREE_TYPE (type), dimension_number++)
16956 tree domain = TYPE_DOMAIN (type);
16958 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16959 break;
16961 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16962 and (in GNU C only) variable bounds. Handle all three forms
16963 here. */
16965 /* Find and reuse a previously generated DW_TAG_subrange_type if
16966 available.
16968 For multi-dimensional arrays, as we iterate through the
16969 various dimensions in the enclosing for loop above, we also
16970 iterate through the DIE children and pick at each
16971 DW_TAG_subrange_type previously generated (if available).
16972 Each child DW_TAG_subrange_type DIE describes the range of
16973 the current dimension. At this point we should have as many
16974 DW_TAG_subrange_type's as we have dimensions in the
16975 array. */
16976 dw_die_ref subrange_die = NULL;
16977 if (child)
16978 while (1)
16980 child = child->die_sib;
16981 if (child->die_tag == DW_TAG_subrange_type)
16982 subrange_die = child;
16983 if (child == type_die->die_child)
16985 /* If we wrapped around, stop looking next time. */
16986 child = NULL;
16987 break;
16989 if (child->die_tag == DW_TAG_subrange_type)
16990 break;
16992 if (!subrange_die)
16993 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16995 if (domain)
16997 /* We have an array type with specified bounds. */
16998 lower = TYPE_MIN_VALUE (domain);
16999 upper = TYPE_MAX_VALUE (domain);
17001 /* Define the index type. */
17002 if (TREE_TYPE (domain)
17003 && !get_AT (subrange_die, DW_AT_type))
17005 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17006 TREE_TYPE field. We can't emit debug info for this
17007 because it is an unnamed integral type. */
17008 if (TREE_CODE (domain) == INTEGER_TYPE
17009 && TYPE_NAME (domain) == NULL_TREE
17010 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17011 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17013 else
17014 add_type_attribute (subrange_die, TREE_TYPE (domain),
17015 TYPE_UNQUALIFIED, type_die);
17018 /* ??? If upper is NULL, the array has unspecified length,
17019 but it does have a lower bound. This happens with Fortran
17020 dimension arr(N:*)
17021 Since the debugger is definitely going to need to know N
17022 to produce useful results, go ahead and output the lower
17023 bound solo, and hope the debugger can cope. */
17025 if (!get_AT (subrange_die, DW_AT_lower_bound))
17026 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
17027 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
17028 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
17031 /* Otherwise we have an array type with an unspecified length. The
17032 DWARF-2 spec does not say how to handle this; let's just leave out the
17033 bounds. */
17037 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
17039 static void
17040 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17042 dw_die_ref decl_die;
17043 HOST_WIDE_INT size;
17045 switch (TREE_CODE (tree_node))
17047 case ERROR_MARK:
17048 size = 0;
17049 break;
17050 case ENUMERAL_TYPE:
17051 case RECORD_TYPE:
17052 case UNION_TYPE:
17053 case QUAL_UNION_TYPE:
17054 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17055 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17057 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17058 return;
17060 size = int_size_in_bytes (tree_node);
17061 break;
17062 case FIELD_DECL:
17063 /* For a data member of a struct or union, the DW_AT_byte_size is
17064 generally given as the number of bytes normally allocated for an
17065 object of the *declared* type of the member itself. This is true
17066 even for bit-fields. */
17067 size = int_size_in_bytes (field_type (tree_node));
17068 break;
17069 default:
17070 gcc_unreachable ();
17073 /* Note that `size' might be -1 when we get to this point. If it is, that
17074 indicates that the byte size of the entity in question is variable. We
17075 have no good way of expressing this fact in Dwarf at the present time,
17076 when location description was not used by the caller code instead. */
17077 if (size >= 0)
17078 add_AT_unsigned (die, DW_AT_byte_size, size);
17081 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17082 which specifies the distance in bits from the highest order bit of the
17083 "containing object" for the bit-field to the highest order bit of the
17084 bit-field itself.
17086 For any given bit-field, the "containing object" is a hypothetical object
17087 (of some integral or enum type) within which the given bit-field lives. The
17088 type of this hypothetical "containing object" is always the same as the
17089 declared type of the individual bit-field itself. The determination of the
17090 exact location of the "containing object" for a bit-field is rather
17091 complicated. It's handled by the `field_byte_offset' function (above).
17093 Note that it is the size (in bytes) of the hypothetical "containing object"
17094 which will be given in the DW_AT_byte_size attribute for this bit-field.
17095 (See `byte_size_attribute' above). */
17097 static inline void
17098 add_bit_offset_attribute (dw_die_ref die, tree decl)
17100 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17101 tree type = DECL_BIT_FIELD_TYPE (decl);
17102 HOST_WIDE_INT bitpos_int;
17103 HOST_WIDE_INT highest_order_object_bit_offset;
17104 HOST_WIDE_INT highest_order_field_bit_offset;
17105 HOST_WIDE_INT bit_offset;
17107 /* Must be a field and a bit field. */
17108 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17110 /* We can't yet handle bit-fields whose offsets are variable, so if we
17111 encounter such things, just return without generating any attribute
17112 whatsoever. Likewise for variable or too large size. */
17113 if (! tree_fits_shwi_p (bit_position (decl))
17114 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17115 return;
17117 bitpos_int = int_bit_position (decl);
17119 /* Note that the bit offset is always the distance (in bits) from the
17120 highest-order bit of the "containing object" to the highest-order bit of
17121 the bit-field itself. Since the "high-order end" of any object or field
17122 is different on big-endian and little-endian machines, the computation
17123 below must take account of these differences. */
17124 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17125 highest_order_field_bit_offset = bitpos_int;
17127 if (! BYTES_BIG_ENDIAN)
17129 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17130 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17133 bit_offset
17134 = (! BYTES_BIG_ENDIAN
17135 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17136 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17138 if (bit_offset < 0)
17139 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17140 else
17141 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17144 /* For a FIELD_DECL node which represents a bit field, output an attribute
17145 which specifies the length in bits of the given field. */
17147 static inline void
17148 add_bit_size_attribute (dw_die_ref die, tree decl)
17150 /* Must be a field and a bit field. */
17151 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17152 && DECL_BIT_FIELD_TYPE (decl));
17154 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17155 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17158 /* If the compiled language is ANSI C, then add a 'prototyped'
17159 attribute, if arg types are given for the parameters of a function. */
17161 static inline void
17162 add_prototyped_attribute (dw_die_ref die, tree func_type)
17164 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17166 case DW_LANG_C:
17167 case DW_LANG_C89:
17168 case DW_LANG_C99:
17169 case DW_LANG_C11:
17170 case DW_LANG_ObjC:
17171 if (prototype_p (func_type))
17172 add_AT_flag (die, DW_AT_prototyped, 1);
17173 break;
17174 default:
17175 break;
17179 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17180 by looking in either the type declaration or object declaration
17181 equate table. */
17183 static inline dw_die_ref
17184 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17186 dw_die_ref origin_die = NULL;
17188 if (TREE_CODE (origin) != FUNCTION_DECL)
17190 /* We may have gotten separated from the block for the inlined
17191 function, if we're in an exception handler or some such; make
17192 sure that the abstract function has been written out.
17194 Doing this for nested functions is wrong, however; functions are
17195 distinct units, and our context might not even be inline. */
17196 tree fn = origin;
17198 if (TYPE_P (fn))
17199 fn = TYPE_STUB_DECL (fn);
17201 fn = decl_function_context (fn);
17202 if (fn)
17203 dwarf2out_abstract_function (fn);
17206 if (DECL_P (origin))
17207 origin_die = lookup_decl_die (origin);
17208 else if (TYPE_P (origin))
17209 origin_die = lookup_type_die (origin);
17211 /* XXX: Functions that are never lowered don't always have correct block
17212 trees (in the case of java, they simply have no block tree, in some other
17213 languages). For these functions, there is nothing we can really do to
17214 output correct debug info for inlined functions in all cases. Rather
17215 than die, we'll just produce deficient debug info now, in that we will
17216 have variables without a proper abstract origin. In the future, when all
17217 functions are lowered, we should re-add a gcc_assert (origin_die)
17218 here. */
17220 if (origin_die)
17221 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17222 return origin_die;
17225 /* We do not currently support the pure_virtual attribute. */
17227 static inline void
17228 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17230 if (DECL_VINDEX (func_decl))
17232 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17234 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17235 add_AT_loc (die, DW_AT_vtable_elem_location,
17236 new_loc_descr (DW_OP_constu,
17237 tree_to_shwi (DECL_VINDEX (func_decl)),
17238 0));
17240 /* GNU extension: Record what type this method came from originally. */
17241 if (debug_info_level > DINFO_LEVEL_TERSE
17242 && DECL_CONTEXT (func_decl))
17243 add_AT_die_ref (die, DW_AT_containing_type,
17244 lookup_type_die (DECL_CONTEXT (func_decl)));
17248 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17249 given decl. This used to be a vendor extension until after DWARF 4
17250 standardized it. */
17252 static void
17253 add_linkage_attr (dw_die_ref die, tree decl)
17255 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17257 /* Mimic what assemble_name_raw does with a leading '*'. */
17258 if (name[0] == '*')
17259 name = &name[1];
17261 if (dwarf_version >= 4)
17262 add_AT_string (die, DW_AT_linkage_name, name);
17263 else
17264 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17267 /* Add source coordinate attributes for the given decl. */
17269 static void
17270 add_src_coords_attributes (dw_die_ref die, tree decl)
17272 expanded_location s;
17274 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17275 return;
17276 s = expand_location (DECL_SOURCE_LOCATION (decl));
17277 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17278 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17281 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17283 static void
17284 add_linkage_name (dw_die_ref die, tree decl)
17286 if (debug_info_level > DINFO_LEVEL_NONE
17287 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17288 && TREE_PUBLIC (decl)
17289 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17290 && die->die_tag != DW_TAG_member)
17292 /* Defer until we have an assembler name set. */
17293 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17295 limbo_die_node *asm_name;
17297 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17298 asm_name->die = die;
17299 asm_name->created_for = decl;
17300 asm_name->next = deferred_asm_name;
17301 deferred_asm_name = asm_name;
17303 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17304 add_linkage_attr (die, decl);
17308 /* Add a DW_AT_name attribute and source coordinate attribute for the
17309 given decl, but only if it actually has a name. */
17311 static void
17312 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17314 tree decl_name;
17316 decl_name = DECL_NAME (decl);
17317 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17319 const char *name = dwarf2_name (decl, 0);
17320 if (name)
17321 add_name_attribute (die, name);
17322 if (! DECL_ARTIFICIAL (decl))
17323 add_src_coords_attributes (die, decl);
17325 add_linkage_name (die, decl);
17328 #ifdef VMS_DEBUGGING_INFO
17329 /* Get the function's name, as described by its RTL. This may be different
17330 from the DECL_NAME name used in the source file. */
17331 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17333 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17334 XEXP (DECL_RTL (decl), 0), false);
17335 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17337 #endif /* VMS_DEBUGGING_INFO */
17340 #ifdef VMS_DEBUGGING_INFO
17341 /* Output the debug main pointer die for VMS */
17343 void
17344 dwarf2out_vms_debug_main_pointer (void)
17346 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17347 dw_die_ref die;
17349 /* Allocate the VMS debug main subprogram die. */
17350 die = ggc_cleared_alloc<die_node> ();
17351 die->die_tag = DW_TAG_subprogram;
17352 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17353 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17354 current_function_funcdef_no);
17355 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17357 /* Make it the first child of comp_unit_die (). */
17358 die->die_parent = comp_unit_die ();
17359 if (comp_unit_die ()->die_child)
17361 die->die_sib = comp_unit_die ()->die_child->die_sib;
17362 comp_unit_die ()->die_child->die_sib = die;
17364 else
17366 die->die_sib = die;
17367 comp_unit_die ()->die_child = die;
17370 #endif /* VMS_DEBUGGING_INFO */
17372 /* Push a new declaration scope. */
17374 static void
17375 push_decl_scope (tree scope)
17377 vec_safe_push (decl_scope_table, scope);
17380 /* Pop a declaration scope. */
17382 static inline void
17383 pop_decl_scope (void)
17385 decl_scope_table->pop ();
17388 /* walk_tree helper function for uses_local_type, below. */
17390 static tree
17391 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17393 if (!TYPE_P (*tp))
17394 *walk_subtrees = 0;
17395 else
17397 tree name = TYPE_NAME (*tp);
17398 if (name && DECL_P (name) && decl_function_context (name))
17399 return *tp;
17401 return NULL_TREE;
17404 /* If TYPE involves a function-local type (including a local typedef to a
17405 non-local type), returns that type; otherwise returns NULL_TREE. */
17407 static tree
17408 uses_local_type (tree type)
17410 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17411 return used;
17414 /* Return the DIE for the scope that immediately contains this type.
17415 Non-named types that do not involve a function-local type get global
17416 scope. Named types nested in namespaces or other types get their
17417 containing scope. All other types (i.e. function-local named types) get
17418 the current active scope. */
17420 static dw_die_ref
17421 scope_die_for (tree t, dw_die_ref context_die)
17423 dw_die_ref scope_die = NULL;
17424 tree containing_scope;
17426 /* Non-types always go in the current scope. */
17427 gcc_assert (TYPE_P (t));
17429 /* Use the scope of the typedef, rather than the scope of the type
17430 it refers to. */
17431 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17432 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17433 else
17434 containing_scope = TYPE_CONTEXT (t);
17436 /* Use the containing namespace if there is one. */
17437 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17439 if (context_die == lookup_decl_die (containing_scope))
17440 /* OK */;
17441 else if (debug_info_level > DINFO_LEVEL_TERSE)
17442 context_die = get_context_die (containing_scope);
17443 else
17444 containing_scope = NULL_TREE;
17447 /* Ignore function type "scopes" from the C frontend. They mean that
17448 a tagged type is local to a parmlist of a function declarator, but
17449 that isn't useful to DWARF. */
17450 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17451 containing_scope = NULL_TREE;
17453 if (SCOPE_FILE_SCOPE_P (containing_scope))
17455 /* If T uses a local type keep it local as well, to avoid references
17456 to function-local DIEs from outside the function. */
17457 if (current_function_decl && uses_local_type (t))
17458 scope_die = context_die;
17459 else
17460 scope_die = comp_unit_die ();
17462 else if (TYPE_P (containing_scope))
17464 /* For types, we can just look up the appropriate DIE. */
17465 if (debug_info_level > DINFO_LEVEL_TERSE)
17466 scope_die = get_context_die (containing_scope);
17467 else
17469 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17470 if (scope_die == NULL)
17471 scope_die = comp_unit_die ();
17474 else
17475 scope_die = context_die;
17477 return scope_die;
17480 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17482 static inline int
17483 local_scope_p (dw_die_ref context_die)
17485 for (; context_die; context_die = context_die->die_parent)
17486 if (context_die->die_tag == DW_TAG_inlined_subroutine
17487 || context_die->die_tag == DW_TAG_subprogram)
17488 return 1;
17490 return 0;
17493 /* Returns nonzero if CONTEXT_DIE is a class. */
17495 static inline int
17496 class_scope_p (dw_die_ref context_die)
17498 return (context_die
17499 && (context_die->die_tag == DW_TAG_structure_type
17500 || context_die->die_tag == DW_TAG_class_type
17501 || context_die->die_tag == DW_TAG_interface_type
17502 || context_die->die_tag == DW_TAG_union_type));
17505 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17506 whether or not to treat a DIE in this context as a declaration. */
17508 static inline int
17509 class_or_namespace_scope_p (dw_die_ref context_die)
17511 return (class_scope_p (context_die)
17512 || (context_die && context_die->die_tag == DW_TAG_namespace));
17515 /* Many forms of DIEs require a "type description" attribute. This
17516 routine locates the proper "type descriptor" die for the type given
17517 by 'type' plus any additional qualifiers given by 'cv_quals', and
17518 adds a DW_AT_type attribute below the given die. */
17520 static void
17521 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17522 dw_die_ref context_die)
17524 enum tree_code code = TREE_CODE (type);
17525 dw_die_ref type_die = NULL;
17527 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17528 or fixed-point type, use the inner type. This is because we have no
17529 support for unnamed types in base_type_die. This can happen if this is
17530 an Ada subrange type. Correct solution is emit a subrange type die. */
17531 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17532 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17533 type = TREE_TYPE (type), code = TREE_CODE (type);
17535 if (code == ERROR_MARK
17536 /* Handle a special case. For functions whose return type is void, we
17537 generate *no* type attribute. (Note that no object may have type
17538 `void', so this only applies to function return types). */
17539 || code == VOID_TYPE)
17540 return;
17542 type_die = modified_type_die (type,
17543 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17544 context_die);
17546 if (type_die != NULL)
17547 add_AT_die_ref (object_die, DW_AT_type, type_die);
17550 /* Given an object die, add the calling convention attribute for the
17551 function call type. */
17552 static void
17553 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17555 enum dwarf_calling_convention value = DW_CC_normal;
17557 value = ((enum dwarf_calling_convention)
17558 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17560 if (is_fortran ()
17561 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17563 /* DWARF 2 doesn't provide a way to identify a program's source-level
17564 entry point. DW_AT_calling_convention attributes are only meant
17565 to describe functions' calling conventions. However, lacking a
17566 better way to signal the Fortran main program, we used this for
17567 a long time, following existing custom. Now, DWARF 4 has
17568 DW_AT_main_subprogram, which we add below, but some tools still
17569 rely on the old way, which we thus keep. */
17570 value = DW_CC_program;
17572 if (dwarf_version >= 4 || !dwarf_strict)
17573 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17576 /* Only add the attribute if the backend requests it, and
17577 is not DW_CC_normal. */
17578 if (value && (value != DW_CC_normal))
17579 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17582 /* Given a tree pointer to a struct, class, union, or enum type node, return
17583 a pointer to the (string) tag name for the given type, or zero if the type
17584 was declared without a tag. */
17586 static const char *
17587 type_tag (const_tree type)
17589 const char *name = 0;
17591 if (TYPE_NAME (type) != 0)
17593 tree t = 0;
17595 /* Find the IDENTIFIER_NODE for the type name. */
17596 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17597 && !TYPE_NAMELESS (type))
17598 t = TYPE_NAME (type);
17600 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17601 a TYPE_DECL node, regardless of whether or not a `typedef' was
17602 involved. */
17603 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17604 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17606 /* We want to be extra verbose. Don't call dwarf_name if
17607 DECL_NAME isn't set. The default hook for decl_printable_name
17608 doesn't like that, and in this context it's correct to return
17609 0, instead of "<anonymous>" or the like. */
17610 if (DECL_NAME (TYPE_NAME (type))
17611 && !DECL_NAMELESS (TYPE_NAME (type)))
17612 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17615 /* Now get the name as a string, or invent one. */
17616 if (!name && t != 0)
17617 name = IDENTIFIER_POINTER (t);
17620 return (name == 0 || *name == '\0') ? 0 : name;
17623 /* Return the type associated with a data member, make a special check
17624 for bit field types. */
17626 static inline tree
17627 member_declared_type (const_tree member)
17629 return (DECL_BIT_FIELD_TYPE (member)
17630 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17633 /* Get the decl's label, as described by its RTL. This may be different
17634 from the DECL_NAME name used in the source file. */
17636 #if 0
17637 static const char *
17638 decl_start_label (tree decl)
17640 rtx x;
17641 const char *fnname;
17643 x = DECL_RTL (decl);
17644 gcc_assert (MEM_P (x));
17646 x = XEXP (x, 0);
17647 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17649 fnname = XSTR (x, 0);
17650 return fnname;
17652 #endif
17654 /* For variable-length arrays that have been previously generated, but
17655 may be incomplete due to missing subscript info, fill the subscript
17656 info. Return TRUE if this is one of those cases. */
17657 static bool
17658 fill_variable_array_bounds (tree type)
17660 if (TREE_ASM_WRITTEN (type)
17661 && TREE_CODE (type) == ARRAY_TYPE
17662 && variably_modified_type_p (type, NULL))
17664 dw_die_ref array_die = lookup_type_die (type);
17665 if (!array_die)
17666 return false;
17667 add_subscript_info (array_die, type, !is_ada ());
17668 return true;
17670 return false;
17673 /* These routines generate the internal representation of the DIE's for
17674 the compilation unit. Debugging information is collected by walking
17675 the declaration trees passed in from dwarf2out_decl(). */
17677 static void
17678 gen_array_type_die (tree type, dw_die_ref context_die)
17680 dw_die_ref array_die;
17682 /* GNU compilers represent multidimensional array types as sequences of one
17683 dimensional array types whose element types are themselves array types.
17684 We sometimes squish that down to a single array_type DIE with multiple
17685 subscripts in the Dwarf debugging info. The draft Dwarf specification
17686 say that we are allowed to do this kind of compression in C, because
17687 there is no difference between an array of arrays and a multidimensional
17688 array. We don't do this for Ada to remain as close as possible to the
17689 actual representation, which is especially important against the language
17690 flexibilty wrt arrays of variable size. */
17692 bool collapse_nested_arrays = !is_ada ();
17694 if (fill_variable_array_bounds (type))
17695 return;
17697 dw_die_ref scope_die = scope_die_for (type, context_die);
17698 tree element_type;
17700 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17701 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17702 if (TYPE_STRING_FLAG (type)
17703 && TREE_CODE (type) == ARRAY_TYPE
17704 && is_fortran ()
17705 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17707 HOST_WIDE_INT size;
17709 array_die = new_die (DW_TAG_string_type, scope_die, type);
17710 add_name_attribute (array_die, type_tag (type));
17711 equate_type_number_to_die (type, array_die);
17712 size = int_size_in_bytes (type);
17713 if (size >= 0)
17714 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17715 else if (TYPE_DOMAIN (type) != NULL_TREE
17716 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17717 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17719 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17720 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17722 size = int_size_in_bytes (TREE_TYPE (szdecl));
17723 if (loc && size > 0)
17725 add_AT_location_description (array_die, DW_AT_string_length, loc);
17726 if (size != DWARF2_ADDR_SIZE)
17727 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17730 return;
17733 array_die = new_die (DW_TAG_array_type, scope_die, type);
17734 add_name_attribute (array_die, type_tag (type));
17735 equate_type_number_to_die (type, array_die);
17737 if (TREE_CODE (type) == VECTOR_TYPE)
17738 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17740 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17741 if (is_fortran ()
17742 && TREE_CODE (type) == ARRAY_TYPE
17743 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17744 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17745 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17747 #if 0
17748 /* We default the array ordering. SDB will probably do
17749 the right things even if DW_AT_ordering is not present. It's not even
17750 an issue until we start to get into multidimensional arrays anyway. If
17751 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17752 then we'll have to put the DW_AT_ordering attribute back in. (But if
17753 and when we find out that we need to put these in, we will only do so
17754 for multidimensional arrays. */
17755 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17756 #endif
17758 if (TREE_CODE (type) == VECTOR_TYPE)
17760 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17761 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17762 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17763 add_bound_info (subrange_die, DW_AT_upper_bound,
17764 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17766 else
17767 add_subscript_info (array_die, type, collapse_nested_arrays);
17769 /* Add representation of the type of the elements of this array type and
17770 emit the corresponding DIE if we haven't done it already. */
17771 element_type = TREE_TYPE (type);
17772 if (collapse_nested_arrays)
17773 while (TREE_CODE (element_type) == ARRAY_TYPE)
17775 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17776 break;
17777 element_type = TREE_TYPE (element_type);
17780 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17782 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17783 if (TYPE_ARTIFICIAL (type))
17784 add_AT_flag (array_die, DW_AT_artificial, 1);
17786 if (get_AT (array_die, DW_AT_name))
17787 add_pubtype (type, array_die);
17790 /* This routine generates DIE for array with hidden descriptor, details
17791 are filled into *info by a langhook. */
17793 static void
17794 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17795 dw_die_ref context_die)
17797 const dw_die_ref scope_die = scope_die_for (type, context_die);
17798 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17799 const struct loc_descr_context context = { type, info->base_decl };
17800 int dim;
17802 add_name_attribute (array_die, type_tag (type));
17803 equate_type_number_to_die (type, array_die);
17805 if (info->ndimensions > 1)
17806 switch (info->ordering)
17808 case array_descr_ordering_row_major:
17809 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17810 break;
17811 case array_descr_ordering_column_major:
17812 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17813 break;
17814 default:
17815 break;
17818 if (dwarf_version >= 3 || !dwarf_strict)
17820 if (info->data_location)
17821 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17822 dw_scalar_form_exprloc, &context);
17823 if (info->associated)
17824 add_scalar_info (array_die, DW_AT_associated, info->associated,
17825 dw_scalar_form_constant
17826 | dw_scalar_form_exprloc
17827 | dw_scalar_form_reference, &context);
17828 if (info->allocated)
17829 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17830 dw_scalar_form_constant
17831 | dw_scalar_form_exprloc
17832 | dw_scalar_form_reference, &context);
17835 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17837 for (dim = 0; dim < info->ndimensions; dim++)
17839 dw_die_ref subrange_die
17840 = new_die (DW_TAG_subrange_type, array_die, NULL);
17842 if (info->dimen[dim].bounds_type)
17843 add_type_attribute (subrange_die,
17844 info->dimen[dim].bounds_type, 0,
17845 context_die);
17846 if (info->dimen[dim].lower_bound)
17847 add_bound_info (subrange_die, DW_AT_lower_bound,
17848 info->dimen[dim].lower_bound, &context);
17849 if (info->dimen[dim].upper_bound)
17850 add_bound_info (subrange_die, DW_AT_upper_bound,
17851 info->dimen[dim].upper_bound, &context);
17852 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17853 add_scalar_info (subrange_die, DW_AT_byte_stride,
17854 info->dimen[dim].stride,
17855 dw_scalar_form_constant
17856 | dw_scalar_form_exprloc
17857 | dw_scalar_form_reference,
17858 &context);
17861 gen_type_die (info->element_type, context_die);
17862 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17863 context_die);
17865 if (get_AT (array_die, DW_AT_name))
17866 add_pubtype (type, array_die);
17869 #if 0
17870 static void
17871 gen_entry_point_die (tree decl, dw_die_ref context_die)
17873 tree origin = decl_ultimate_origin (decl);
17874 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17876 if (origin != NULL)
17877 add_abstract_origin_attribute (decl_die, origin);
17878 else
17880 add_name_and_src_coords_attributes (decl_die, decl);
17881 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17882 TYPE_UNQUALIFIED, context_die);
17885 if (DECL_ABSTRACT_P (decl))
17886 equate_decl_number_to_die (decl, decl_die);
17887 else
17888 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17890 #endif
17892 /* Walk through the list of incomplete types again, trying once more to
17893 emit full debugging info for them. */
17895 static void
17896 retry_incomplete_types (void)
17898 int i;
17900 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17901 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17902 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17905 /* Determine what tag to use for a record type. */
17907 static enum dwarf_tag
17908 record_type_tag (tree type)
17910 if (! lang_hooks.types.classify_record)
17911 return DW_TAG_structure_type;
17913 switch (lang_hooks.types.classify_record (type))
17915 case RECORD_IS_STRUCT:
17916 return DW_TAG_structure_type;
17918 case RECORD_IS_CLASS:
17919 return DW_TAG_class_type;
17921 case RECORD_IS_INTERFACE:
17922 if (dwarf_version >= 3 || !dwarf_strict)
17923 return DW_TAG_interface_type;
17924 return DW_TAG_structure_type;
17926 default:
17927 gcc_unreachable ();
17931 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17932 include all of the information about the enumeration values also. Each
17933 enumerated type name/value is listed as a child of the enumerated type
17934 DIE. */
17936 static dw_die_ref
17937 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17939 dw_die_ref type_die = lookup_type_die (type);
17941 if (type_die == NULL)
17943 type_die = new_die (DW_TAG_enumeration_type,
17944 scope_die_for (type, context_die), type);
17945 equate_type_number_to_die (type, type_die);
17946 add_name_attribute (type_die, type_tag (type));
17947 if (dwarf_version >= 4 || !dwarf_strict)
17949 if (ENUM_IS_SCOPED (type))
17950 add_AT_flag (type_die, DW_AT_enum_class, 1);
17951 if (ENUM_IS_OPAQUE (type))
17952 add_AT_flag (type_die, DW_AT_declaration, 1);
17955 else if (! TYPE_SIZE (type))
17956 return type_die;
17957 else
17958 remove_AT (type_die, DW_AT_declaration);
17960 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17961 given enum type is incomplete, do not generate the DW_AT_byte_size
17962 attribute or the DW_AT_element_list attribute. */
17963 if (TYPE_SIZE (type))
17965 tree link;
17967 TREE_ASM_WRITTEN (type) = 1;
17968 add_byte_size_attribute (type_die, type);
17969 if (dwarf_version >= 3 || !dwarf_strict)
17971 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17972 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17973 context_die);
17975 if (TYPE_STUB_DECL (type) != NULL_TREE)
17977 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17978 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17981 /* If the first reference to this type was as the return type of an
17982 inline function, then it may not have a parent. Fix this now. */
17983 if (type_die->die_parent == NULL)
17984 add_child_die (scope_die_for (type, context_die), type_die);
17986 for (link = TYPE_VALUES (type);
17987 link != NULL; link = TREE_CHAIN (link))
17989 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17990 tree value = TREE_VALUE (link);
17992 add_name_attribute (enum_die,
17993 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17995 if (TREE_CODE (value) == CONST_DECL)
17996 value = DECL_INITIAL (value);
17998 if (simple_type_size_in_bits (TREE_TYPE (value))
17999 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
18001 /* For constant forms created by add_AT_unsigned DWARF
18002 consumers (GDB, elfutils, etc.) always zero extend
18003 the value. Only when the actual value is negative
18004 do we need to use add_AT_int to generate a constant
18005 form that can represent negative values. */
18006 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
18007 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
18008 add_AT_unsigned (enum_die, DW_AT_const_value,
18009 (unsigned HOST_WIDE_INT) val);
18010 else
18011 add_AT_int (enum_die, DW_AT_const_value, val);
18013 else
18014 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
18015 that here. TODO: This should be re-worked to use correct
18016 signed/unsigned double tags for all cases. */
18017 add_AT_wide (enum_die, DW_AT_const_value, value);
18020 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18021 if (TYPE_ARTIFICIAL (type))
18022 add_AT_flag (type_die, DW_AT_artificial, 1);
18024 else
18025 add_AT_flag (type_die, DW_AT_declaration, 1);
18027 add_pubtype (type, type_die);
18029 return type_die;
18032 /* Generate a DIE to represent either a real live formal parameter decl or to
18033 represent just the type of some formal parameter position in some function
18034 type.
18036 Note that this routine is a bit unusual because its argument may be a
18037 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18038 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18039 node. If it's the former then this function is being called to output a
18040 DIE to represent a formal parameter object (or some inlining thereof). If
18041 it's the latter, then this function is only being called to output a
18042 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18043 argument type of some subprogram type.
18044 If EMIT_NAME_P is true, name and source coordinate attributes
18045 are emitted. */
18047 static dw_die_ref
18048 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18049 dw_die_ref context_die)
18051 tree node_or_origin = node ? node : origin;
18052 tree ultimate_origin;
18053 dw_die_ref parm_die = NULL;
18055 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18057 parm_die = lookup_decl_die (node);
18059 /* If the contexts differ, we may not be talking about the same
18060 thing. */
18061 if (parm_die && parm_die->die_parent != context_die)
18063 if (!DECL_ABSTRACT_P (node))
18065 /* This can happen when creating an inlined instance, in
18066 which case we need to create a new DIE that will get
18067 annotated with DW_AT_abstract_origin. */
18068 parm_die = NULL;
18070 else
18072 /* FIXME: Reuse DIE even with a differing context.
18074 This can happen when calling
18075 dwarf2out_abstract_function to build debug info for
18076 the abstract instance of a function for which we have
18077 already generated a DIE in
18078 dwarf2out_early_global_decl.
18080 Once we remove dwarf2out_abstract_function, we should
18081 have a call to gcc_unreachable here. */
18085 if (parm_die && parm_die->die_parent == NULL)
18087 /* Check that parm_die already has the right attributes that
18088 we would have added below. If any attributes are
18089 missing, fall through to add them. */
18090 if (! DECL_ABSTRACT_P (node_or_origin)
18091 && !get_AT (parm_die, DW_AT_location)
18092 && !get_AT (parm_die, DW_AT_const_value))
18093 /* We are missing location info, and are about to add it. */
18095 else
18097 add_child_die (context_die, parm_die);
18098 return parm_die;
18103 /* If we have a previously generated DIE, use it, unless this is an
18104 concrete instance (origin != NULL), in which case we need a new
18105 DIE with a corresponding DW_AT_abstract_origin. */
18106 bool reusing_die;
18107 if (parm_die && origin == NULL)
18108 reusing_die = true;
18109 else
18111 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18112 reusing_die = false;
18115 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18117 case tcc_declaration:
18118 ultimate_origin = decl_ultimate_origin (node_or_origin);
18119 if (node || ultimate_origin)
18120 origin = ultimate_origin;
18122 if (reusing_die)
18123 goto add_location;
18125 if (origin != NULL)
18126 add_abstract_origin_attribute (parm_die, origin);
18127 else if (emit_name_p)
18128 add_name_and_src_coords_attributes (parm_die, node);
18129 if (origin == NULL
18130 || (! DECL_ABSTRACT_P (node_or_origin)
18131 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18132 decl_function_context
18133 (node_or_origin))))
18135 tree type = TREE_TYPE (node_or_origin);
18136 if (decl_by_reference_p (node_or_origin))
18137 add_type_attribute (parm_die, TREE_TYPE (type),
18138 TYPE_UNQUALIFIED, context_die);
18139 else
18140 add_type_attribute (parm_die, type,
18141 decl_quals (node_or_origin),
18142 context_die);
18144 if (origin == NULL && DECL_ARTIFICIAL (node))
18145 add_AT_flag (parm_die, DW_AT_artificial, 1);
18146 add_location:
18147 if (node && node != origin)
18148 equate_decl_number_to_die (node, parm_die);
18149 if (! DECL_ABSTRACT_P (node_or_origin))
18150 add_location_or_const_value_attribute (parm_die, node_or_origin,
18151 node == NULL);
18153 break;
18155 case tcc_type:
18156 /* We were called with some kind of a ..._TYPE node. */
18157 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18158 context_die);
18159 break;
18161 default:
18162 gcc_unreachable ();
18165 return parm_die;
18168 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18169 children DW_TAG_formal_parameter DIEs representing the arguments of the
18170 parameter pack.
18172 PARM_PACK must be a function parameter pack.
18173 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18174 must point to the subsequent arguments of the function PACK_ARG belongs to.
18175 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18176 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18177 following the last one for which a DIE was generated. */
18179 static dw_die_ref
18180 gen_formal_parameter_pack_die (tree parm_pack,
18181 tree pack_arg,
18182 dw_die_ref subr_die,
18183 tree *next_arg)
18185 tree arg;
18186 dw_die_ref parm_pack_die;
18188 gcc_assert (parm_pack
18189 && lang_hooks.function_parameter_pack_p (parm_pack)
18190 && subr_die);
18192 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18193 add_src_coords_attributes (parm_pack_die, parm_pack);
18195 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18197 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18198 parm_pack))
18199 break;
18200 gen_formal_parameter_die (arg, NULL,
18201 false /* Don't emit name attribute. */,
18202 parm_pack_die);
18204 if (next_arg)
18205 *next_arg = arg;
18206 return parm_pack_die;
18209 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18210 at the end of an (ANSI prototyped) formal parameters list. */
18212 static void
18213 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18215 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18218 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18219 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18220 parameters as specified in some function type specification (except for
18221 those which appear as part of a function *definition*). */
18223 static void
18224 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18226 tree link;
18227 tree formal_type = NULL;
18228 tree first_parm_type;
18229 tree arg;
18231 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18233 arg = DECL_ARGUMENTS (function_or_method_type);
18234 function_or_method_type = TREE_TYPE (function_or_method_type);
18236 else
18237 arg = NULL_TREE;
18239 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18241 /* Make our first pass over the list of formal parameter types and output a
18242 DW_TAG_formal_parameter DIE for each one. */
18243 for (link = first_parm_type; link; )
18245 dw_die_ref parm_die;
18247 formal_type = TREE_VALUE (link);
18248 if (formal_type == void_type_node)
18249 break;
18251 /* Output a (nameless) DIE to represent the formal parameter itself. */
18252 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18254 parm_die = gen_formal_parameter_die (formal_type, NULL,
18255 true /* Emit name attribute. */,
18256 context_die);
18257 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18258 && link == first_parm_type)
18260 add_AT_flag (parm_die, DW_AT_artificial, 1);
18261 if (dwarf_version >= 3 || !dwarf_strict)
18262 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18264 else if (arg && DECL_ARTIFICIAL (arg))
18265 add_AT_flag (parm_die, DW_AT_artificial, 1);
18268 link = TREE_CHAIN (link);
18269 if (arg)
18270 arg = DECL_CHAIN (arg);
18273 /* If this function type has an ellipsis, add a
18274 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18275 if (formal_type != void_type_node)
18276 gen_unspecified_parameters_die (function_or_method_type, context_die);
18278 /* Make our second (and final) pass over the list of formal parameter types
18279 and output DIEs to represent those types (as necessary). */
18280 for (link = TYPE_ARG_TYPES (function_or_method_type);
18281 link && TREE_VALUE (link);
18282 link = TREE_CHAIN (link))
18283 gen_type_die (TREE_VALUE (link), context_die);
18286 /* We want to generate the DIE for TYPE so that we can generate the
18287 die for MEMBER, which has been defined; we will need to refer back
18288 to the member declaration nested within TYPE. If we're trying to
18289 generate minimal debug info for TYPE, processing TYPE won't do the
18290 trick; we need to attach the member declaration by hand. */
18292 static void
18293 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18295 gen_type_die (type, context_die);
18297 /* If we're trying to avoid duplicate debug info, we may not have
18298 emitted the member decl for this function. Emit it now. */
18299 if (TYPE_STUB_DECL (type)
18300 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18301 && ! lookup_decl_die (member))
18303 dw_die_ref type_die;
18304 gcc_assert (!decl_ultimate_origin (member));
18306 push_decl_scope (type);
18307 type_die = lookup_type_die_strip_naming_typedef (type);
18308 if (TREE_CODE (member) == FUNCTION_DECL)
18309 gen_subprogram_die (member, type_die);
18310 else if (TREE_CODE (member) == FIELD_DECL)
18312 /* Ignore the nameless fields that are used to skip bits but handle
18313 C++ anonymous unions and structs. */
18314 if (DECL_NAME (member) != NULL_TREE
18315 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18316 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18318 gen_type_die (member_declared_type (member), type_die);
18319 gen_field_die (member, type_die);
18322 else
18323 gen_variable_die (member, NULL_TREE, type_die);
18325 pop_decl_scope ();
18329 /* Forward declare these functions, because they are mutually recursive
18330 with their set_block_* pairing functions. */
18331 static void set_decl_origin_self (tree);
18332 static void set_decl_abstract_flags (tree, vec<tree> &);
18334 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18335 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18336 that it points to the node itself, thus indicating that the node is its
18337 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18338 the given node is NULL, recursively descend the decl/block tree which
18339 it is the root of, and for each other ..._DECL or BLOCK node contained
18340 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18341 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18342 values to point to themselves. */
18344 static void
18345 set_block_origin_self (tree stmt)
18347 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18349 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18352 tree local_decl;
18354 for (local_decl = BLOCK_VARS (stmt);
18355 local_decl != NULL_TREE;
18356 local_decl = DECL_CHAIN (local_decl))
18357 /* Do not recurse on nested functions since the inlining status
18358 of parent and child can be different as per the DWARF spec. */
18359 if (TREE_CODE (local_decl) != FUNCTION_DECL
18360 && !DECL_EXTERNAL (local_decl))
18361 set_decl_origin_self (local_decl);
18365 tree subblock;
18367 for (subblock = BLOCK_SUBBLOCKS (stmt);
18368 subblock != NULL_TREE;
18369 subblock = BLOCK_CHAIN (subblock))
18370 set_block_origin_self (subblock); /* Recurse. */
18375 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18376 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18377 node to so that it points to the node itself, thus indicating that the
18378 node represents its own (abstract) origin. Additionally, if the
18379 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18380 the decl/block tree of which the given node is the root of, and for
18381 each other ..._DECL or BLOCK node contained therein whose
18382 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18383 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18384 point to themselves. */
18386 static void
18387 set_decl_origin_self (tree decl)
18389 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18391 DECL_ABSTRACT_ORIGIN (decl) = decl;
18392 if (TREE_CODE (decl) == FUNCTION_DECL)
18394 tree arg;
18396 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18397 DECL_ABSTRACT_ORIGIN (arg) = arg;
18398 if (DECL_INITIAL (decl) != NULL_TREE
18399 && DECL_INITIAL (decl) != error_mark_node)
18400 set_block_origin_self (DECL_INITIAL (decl));
18405 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18406 and if it wasn't 1 before, push it to abstract_vec vector.
18407 For all local decls and all local sub-blocks (recursively) do it
18408 too. */
18410 static void
18411 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18413 tree local_decl;
18414 tree subblock;
18415 unsigned int i;
18417 if (!BLOCK_ABSTRACT (stmt))
18419 abstract_vec.safe_push (stmt);
18420 BLOCK_ABSTRACT (stmt) = 1;
18423 for (local_decl = BLOCK_VARS (stmt);
18424 local_decl != NULL_TREE;
18425 local_decl = DECL_CHAIN (local_decl))
18426 if (! DECL_EXTERNAL (local_decl))
18427 set_decl_abstract_flags (local_decl, abstract_vec);
18429 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18431 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18432 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18433 || TREE_CODE (local_decl) == PARM_DECL)
18434 set_decl_abstract_flags (local_decl, abstract_vec);
18437 for (subblock = BLOCK_SUBBLOCKS (stmt);
18438 subblock != NULL_TREE;
18439 subblock = BLOCK_CHAIN (subblock))
18440 set_block_abstract_flags (subblock, abstract_vec);
18443 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18444 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18445 In the case where the decl is a FUNCTION_DECL also set the abstract
18446 flags for all of the parameters, local vars, local
18447 blocks and sub-blocks (recursively). */
18449 static void
18450 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18452 if (!DECL_ABSTRACT_P (decl))
18454 abstract_vec.safe_push (decl);
18455 DECL_ABSTRACT_P (decl) = 1;
18458 if (TREE_CODE (decl) == FUNCTION_DECL)
18460 tree arg;
18462 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18463 if (!DECL_ABSTRACT_P (arg))
18465 abstract_vec.safe_push (arg);
18466 DECL_ABSTRACT_P (arg) = 1;
18468 if (DECL_INITIAL (decl) != NULL_TREE
18469 && DECL_INITIAL (decl) != error_mark_node)
18470 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18474 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18475 may later generate inlined and/or out-of-line instances of.
18477 FIXME: In the early-dwarf world, this function, and most of the
18478 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18479 the abstract instance. All we would need to do is annotate
18480 the early DIE with the appropriate DW_AT_inline in late
18481 dwarf (perhaps in gen_inlined_subroutine_die).
18483 However, we can't do this yet, because LTO streaming of DIEs
18484 has not been implemented yet. */
18486 static void
18487 dwarf2out_abstract_function (tree decl)
18489 dw_die_ref old_die;
18490 tree save_fn;
18491 tree context;
18492 hash_table<decl_loc_hasher> *old_decl_loc_table;
18493 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18494 int old_call_site_count, old_tail_call_site_count;
18495 struct call_arg_loc_node *old_call_arg_locations;
18497 /* Make sure we have the actual abstract inline, not a clone. */
18498 decl = DECL_ORIGIN (decl);
18500 old_die = lookup_decl_die (decl);
18501 if (old_die && get_AT (old_die, DW_AT_inline))
18502 /* We've already generated the abstract instance. */
18503 return;
18505 /* We can be called while recursively when seeing block defining inlined subroutine
18506 DIE. Be sure to not clobber the outer location table nor use it or we would
18507 get locations in abstract instantces. */
18508 old_decl_loc_table = decl_loc_table;
18509 decl_loc_table = NULL;
18510 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18511 cached_dw_loc_list_table = NULL;
18512 old_call_arg_locations = call_arg_locations;
18513 call_arg_locations = NULL;
18514 old_call_site_count = call_site_count;
18515 call_site_count = -1;
18516 old_tail_call_site_count = tail_call_site_count;
18517 tail_call_site_count = -1;
18519 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18520 we don't get confused by DECL_ABSTRACT_P. */
18521 if (debug_info_level > DINFO_LEVEL_TERSE)
18523 context = decl_class_context (decl);
18524 if (context)
18525 gen_type_die_for_member
18526 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18529 /* Pretend we've just finished compiling this function. */
18530 save_fn = current_function_decl;
18531 current_function_decl = decl;
18533 auto_vec<tree, 64> abstract_vec;
18534 set_decl_abstract_flags (decl, abstract_vec);
18535 dwarf2out_decl (decl);
18536 unsigned int i;
18537 tree t;
18538 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18539 if (TREE_CODE (t) == BLOCK)
18540 BLOCK_ABSTRACT (t) = 0;
18541 else
18542 DECL_ABSTRACT_P (t) = 0;
18544 current_function_decl = save_fn;
18545 decl_loc_table = old_decl_loc_table;
18546 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18547 call_arg_locations = old_call_arg_locations;
18548 call_site_count = old_call_site_count;
18549 tail_call_site_count = old_tail_call_site_count;
18552 /* Helper function of premark_used_types() which gets called through
18553 htab_traverse.
18555 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18556 marked as unused by prune_unused_types. */
18558 bool
18559 premark_used_types_helper (tree const &type, void *)
18561 dw_die_ref die;
18563 die = lookup_type_die (type);
18564 if (die != NULL)
18565 die->die_perennial_p = 1;
18566 return true;
18569 /* Helper function of premark_types_used_by_global_vars which gets called
18570 through htab_traverse.
18572 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18573 marked as unused by prune_unused_types. The DIE of the type is marked
18574 only if the global variable using the type will actually be emitted. */
18577 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18578 void *)
18580 struct types_used_by_vars_entry *entry;
18581 dw_die_ref die;
18583 entry = (struct types_used_by_vars_entry *) *slot;
18584 gcc_assert (entry->type != NULL
18585 && entry->var_decl != NULL);
18586 die = lookup_type_die (entry->type);
18587 if (die)
18589 /* Ask cgraph if the global variable really is to be emitted.
18590 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18591 varpool_node *node = varpool_node::get (entry->var_decl);
18592 if (node && node->definition)
18594 die->die_perennial_p = 1;
18595 /* Keep the parent DIEs as well. */
18596 while ((die = die->die_parent) && die->die_perennial_p == 0)
18597 die->die_perennial_p = 1;
18600 return 1;
18603 /* Mark all members of used_types_hash as perennial. */
18605 static void
18606 premark_used_types (struct function *fun)
18608 if (fun && fun->used_types_hash)
18609 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18612 /* Mark all members of types_used_by_vars_entry as perennial. */
18614 static void
18615 premark_types_used_by_global_vars (void)
18617 if (types_used_by_vars_hash)
18618 types_used_by_vars_hash
18619 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18622 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18623 for CA_LOC call arg loc node. */
18625 static dw_die_ref
18626 gen_call_site_die (tree decl, dw_die_ref subr_die,
18627 struct call_arg_loc_node *ca_loc)
18629 dw_die_ref stmt_die = NULL, die;
18630 tree block = ca_loc->block;
18632 while (block
18633 && block != DECL_INITIAL (decl)
18634 && TREE_CODE (block) == BLOCK)
18636 stmt_die = BLOCK_DIE (block);
18637 if (stmt_die)
18638 break;
18639 block = BLOCK_SUPERCONTEXT (block);
18641 if (stmt_die == NULL)
18642 stmt_die = subr_die;
18643 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18644 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18645 if (ca_loc->tail_call_p)
18646 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18647 if (ca_loc->symbol_ref)
18649 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18650 if (tdie)
18651 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18652 else
18653 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18655 return die;
18658 /* Generate a DIE to represent a declared function (either file-scope or
18659 block-local). */
18661 static void
18662 gen_subprogram_die (tree decl, dw_die_ref context_die)
18664 tree origin = decl_ultimate_origin (decl);
18665 dw_die_ref subr_die;
18666 dw_die_ref old_die = lookup_decl_die (decl);
18668 /* This function gets called multiple times for different stages of
18669 the debug process. For example, for func() in this code:
18671 namespace S
18673 void func() { ... }
18676 ...we get called 4 times. Twice in early debug and twice in
18677 late debug:
18679 Early debug
18680 -----------
18682 1. Once while generating func() within the namespace. This is
18683 the declaration. The declaration bit below is set, as the
18684 context is the namespace.
18686 A new DIE will be generated with DW_AT_declaration set.
18688 2. Once for func() itself. This is the specification. The
18689 declaration bit below is clear as the context is the CU.
18691 We will use the cached DIE from (1) to create a new DIE with
18692 DW_AT_specification pointing to the declaration in (1).
18694 Late debug via rest_of_handle_final()
18695 -------------------------------------
18697 3. Once generating func() within the namespace. This is also the
18698 declaration, as in (1), but this time we will early exit below
18699 as we have a cached DIE and a declaration needs no additional
18700 annotations (no locations), as the source declaration line
18701 info is enough.
18703 4. Once for func() itself. As in (2), this is the specification,
18704 but this time we will re-use the cached DIE, and just annotate
18705 it with the location information that should now be available.
18707 For something without namespaces, but with abstract instances, we
18708 are also called a multiple times:
18710 class Base
18712 public:
18713 Base (); // constructor declaration (1)
18716 Base::Base () { } // constructor specification (2)
18718 Early debug
18719 -----------
18721 1. Once for the Base() constructor by virtue of it being a
18722 member of the Base class. This is done via
18723 rest_of_type_compilation.
18725 This is a declaration, so a new DIE will be created with
18726 DW_AT_declaration.
18728 2. Once for the Base() constructor definition, but this time
18729 while generating the abstract instance of the base
18730 constructor (__base_ctor) which is being generated via early
18731 debug of reachable functions.
18733 Even though we have a cached version of the declaration (1),
18734 we will create a DW_AT_specification of the declaration DIE
18735 in (1).
18737 3. Once for the __base_ctor itself, but this time, we generate
18738 an DW_AT_abstract_origin version of the DW_AT_specification in
18739 (2).
18741 Late debug via rest_of_handle_final
18742 -----------------------------------
18744 4. One final time for the __base_ctor (which will have a cached
18745 DIE with DW_AT_abstract_origin created in (3). This time,
18746 we will just annotate the location information now
18747 available.
18749 int declaration = (current_function_decl != decl
18750 || class_or_namespace_scope_p (context_die));
18752 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18754 /* Now that the C++ front end lazily declares artificial member fns, we
18755 might need to retrofit the declaration into its class. */
18756 if (!declaration && !origin && !old_die
18757 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18758 && !class_or_namespace_scope_p (context_die)
18759 && debug_info_level > DINFO_LEVEL_TERSE)
18760 old_die = force_decl_die (decl);
18762 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18763 if (origin != NULL)
18765 gcc_assert (!declaration || local_scope_p (context_die));
18767 /* Fixup die_parent for the abstract instance of a nested
18768 inline function. */
18769 if (old_die && old_die->die_parent == NULL)
18770 add_child_die (context_die, old_die);
18772 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18774 /* If we have a DW_AT_abstract_origin we have a working
18775 cached version. */
18776 subr_die = old_die;
18778 else
18780 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18781 add_abstract_origin_attribute (subr_die, origin);
18782 /* This is where the actual code for a cloned function is.
18783 Let's emit linkage name attribute for it. This helps
18784 debuggers to e.g, set breakpoints into
18785 constructors/destructors when the user asks "break
18786 K::K". */
18787 add_linkage_name (subr_die, decl);
18790 /* A cached copy, possibly from early dwarf generation. Reuse as
18791 much as possible. */
18792 else if (old_die)
18794 /* A declaration that has been previously dumped needs no
18795 additional information. */
18796 if (declaration)
18797 return;
18799 if (!get_AT_flag (old_die, DW_AT_declaration)
18800 /* We can have a normal definition following an inline one in the
18801 case of redefinition of GNU C extern inlines.
18802 It seems reasonable to use AT_specification in this case. */
18803 && !get_AT (old_die, DW_AT_inline))
18805 /* Detect and ignore this case, where we are trying to output
18806 something we have already output. */
18807 if (get_AT (old_die, DW_AT_low_pc)
18808 || get_AT (old_die, DW_AT_ranges))
18809 return;
18811 /* If we have no location information, this must be a
18812 partially generated DIE from early dwarf generation.
18813 Fall through and generate it. */
18816 /* If the definition comes from the same place as the declaration,
18817 maybe use the old DIE. We always want the DIE for this function
18818 that has the *_pc attributes to be under comp_unit_die so the
18819 debugger can find it. We also need to do this for abstract
18820 instances of inlines, since the spec requires the out-of-line copy
18821 to have the same parent. For local class methods, this doesn't
18822 apply; we just use the old DIE. */
18823 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18824 struct dwarf_file_data * file_index = lookup_filename (s.file);
18825 if ((is_cu_die (old_die->die_parent)
18826 /* This condition fixes the inconsistency/ICE with the
18827 following Fortran test (or some derivative thereof) while
18828 building libgfortran:
18830 module some_m
18831 contains
18832 logical function funky (FLAG)
18833 funky = .true.
18834 end function
18835 end module
18837 || (old_die->die_parent
18838 && old_die->die_parent->die_tag == DW_TAG_module)
18839 || context_die == NULL)
18840 && (DECL_ARTIFICIAL (decl)
18841 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18842 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18843 == (unsigned) s.line))))
18845 subr_die = old_die;
18847 /* Clear out the declaration attribute, but leave the
18848 parameters so they can be augmented with location
18849 information later. Unless this was a declaration, in
18850 which case, wipe out the nameless parameters and recreate
18851 them further down. */
18852 if (remove_AT (subr_die, DW_AT_declaration))
18855 remove_AT (subr_die, DW_AT_object_pointer);
18856 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18859 /* Make a specification pointing to the previously built
18860 declaration. */
18861 else
18863 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18864 add_AT_specification (subr_die, old_die);
18865 add_pubname (decl, subr_die);
18866 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18867 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18868 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18869 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18871 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18872 emit the real type on the definition die. */
18873 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18875 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18876 if (die == auto_die || die == decltype_auto_die)
18877 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18878 TYPE_UNQUALIFIED, context_die);
18882 /* Create a fresh DIE for anything else. */
18883 else
18885 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18887 if (TREE_PUBLIC (decl))
18888 add_AT_flag (subr_die, DW_AT_external, 1);
18890 add_name_and_src_coords_attributes (subr_die, decl);
18891 add_pubname (decl, subr_die);
18892 if (debug_info_level > DINFO_LEVEL_TERSE)
18894 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18895 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18896 TYPE_UNQUALIFIED, context_die);
18899 add_pure_or_virtual_attribute (subr_die, decl);
18900 if (DECL_ARTIFICIAL (decl))
18901 add_AT_flag (subr_die, DW_AT_artificial, 1);
18903 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18904 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18906 add_accessibility_attribute (subr_die, decl);
18909 /* Unless we have an existing non-declaration DIE, equate the new
18910 DIE. */
18911 if (!old_die || is_declaration_die (old_die))
18912 equate_decl_number_to_die (decl, subr_die);
18914 if (declaration)
18916 if (!old_die || !get_AT (old_die, DW_AT_inline))
18918 add_AT_flag (subr_die, DW_AT_declaration, 1);
18920 /* If this is an explicit function declaration then generate
18921 a DW_AT_explicit attribute. */
18922 if (lang_hooks.decls.function_decl_explicit_p (decl)
18923 && (dwarf_version >= 3 || !dwarf_strict))
18924 add_AT_flag (subr_die, DW_AT_explicit, 1);
18926 /* If this is a C++11 deleted special function member then generate
18927 a DW_AT_GNU_deleted attribute. */
18928 if (lang_hooks.decls.function_decl_deleted_p (decl)
18929 && (! dwarf_strict))
18930 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18933 /* Tag abstract instances with DW_AT_inline. */
18934 else if (DECL_ABSTRACT_P (decl))
18936 if (DECL_DECLARED_INLINE_P (decl))
18938 if (cgraph_function_possibly_inlined_p (decl))
18939 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18940 else
18941 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18943 else
18945 if (cgraph_function_possibly_inlined_p (decl))
18946 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18947 else
18948 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18951 if (DECL_DECLARED_INLINE_P (decl)
18952 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18953 add_AT_flag (subr_die, DW_AT_artificial, 1);
18955 /* For non DECL_EXTERNALs, if range information is available, fill
18956 the DIE with it. */
18957 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18959 HOST_WIDE_INT cfa_fb_offset;
18961 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18963 if (!flag_reorder_blocks_and_partition)
18965 dw_fde_ref fde = fun->fde;
18966 if (fde->dw_fde_begin)
18968 /* We have already generated the labels. */
18969 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18970 fde->dw_fde_end, false);
18972 else
18974 /* Create start/end labels and add the range. */
18975 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18976 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18977 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18978 current_function_funcdef_no);
18979 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18980 current_function_funcdef_no);
18981 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18982 false);
18985 #if VMS_DEBUGGING_INFO
18986 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18987 Section 2.3 Prologue and Epilogue Attributes:
18988 When a breakpoint is set on entry to a function, it is generally
18989 desirable for execution to be suspended, not on the very first
18990 instruction of the function, but rather at a point after the
18991 function's frame has been set up, after any language defined local
18992 declaration processing has been completed, and before execution of
18993 the first statement of the function begins. Debuggers generally
18994 cannot properly determine where this point is. Similarly for a
18995 breakpoint set on exit from a function. The prologue and epilogue
18996 attributes allow a compiler to communicate the location(s) to use. */
18999 if (fde->dw_fde_vms_end_prologue)
19000 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19001 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19003 if (fde->dw_fde_vms_begin_epilogue)
19004 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19005 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19007 #endif
19010 else
19012 /* Generate pubnames entries for the split function code ranges. */
19013 dw_fde_ref fde = fun->fde;
19015 if (fde->dw_fde_second_begin)
19017 if (dwarf_version >= 3 || !dwarf_strict)
19019 /* We should use ranges for non-contiguous code section
19020 addresses. Use the actual code range for the initial
19021 section, since the HOT/COLD labels might precede an
19022 alignment offset. */
19023 bool range_list_added = false;
19024 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19025 fde->dw_fde_end, &range_list_added,
19026 false);
19027 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19028 fde->dw_fde_second_end,
19029 &range_list_added, false);
19030 if (range_list_added)
19031 add_ranges (NULL);
19033 else
19035 /* There is no real support in DW2 for this .. so we make
19036 a work-around. First, emit the pub name for the segment
19037 containing the function label. Then make and emit a
19038 simplified subprogram DIE for the second segment with the
19039 name pre-fixed by __hot/cold_sect_of_. We use the same
19040 linkage name for the second die so that gdb will find both
19041 sections when given "b foo". */
19042 const char *name = NULL;
19043 tree decl_name = DECL_NAME (decl);
19044 dw_die_ref seg_die;
19046 /* Do the 'primary' section. */
19047 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19048 fde->dw_fde_end, false);
19050 /* Build a minimal DIE for the secondary section. */
19051 seg_die = new_die (DW_TAG_subprogram,
19052 subr_die->die_parent, decl);
19054 if (TREE_PUBLIC (decl))
19055 add_AT_flag (seg_die, DW_AT_external, 1);
19057 if (decl_name != NULL
19058 && IDENTIFIER_POINTER (decl_name) != NULL)
19060 name = dwarf2_name (decl, 1);
19061 if (! DECL_ARTIFICIAL (decl))
19062 add_src_coords_attributes (seg_die, decl);
19064 add_linkage_name (seg_die, decl);
19066 gcc_assert (name != NULL);
19067 add_pure_or_virtual_attribute (seg_die, decl);
19068 if (DECL_ARTIFICIAL (decl))
19069 add_AT_flag (seg_die, DW_AT_artificial, 1);
19071 name = concat ("__second_sect_of_", name, NULL);
19072 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19073 fde->dw_fde_second_end, false);
19074 add_name_attribute (seg_die, name);
19075 if (want_pubnames ())
19076 add_pubname_string (name, seg_die);
19079 else
19080 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19081 false);
19084 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19086 /* We define the "frame base" as the function's CFA. This is more
19087 convenient for several reasons: (1) It's stable across the prologue
19088 and epilogue, which makes it better than just a frame pointer,
19089 (2) With dwarf3, there exists a one-byte encoding that allows us
19090 to reference the .debug_frame data by proxy, but failing that,
19091 (3) We can at least reuse the code inspection and interpretation
19092 code that determines the CFA position at various points in the
19093 function. */
19094 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19096 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19097 add_AT_loc (subr_die, DW_AT_frame_base, op);
19099 else
19101 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19102 if (list->dw_loc_next)
19103 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19104 else
19105 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19108 /* Compute a displacement from the "steady-state frame pointer" to
19109 the CFA. The former is what all stack slots and argument slots
19110 will reference in the rtl; the latter is what we've told the
19111 debugger about. We'll need to adjust all frame_base references
19112 by this displacement. */
19113 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19115 if (fun->static_chain_decl)
19116 add_AT_location_description
19117 (subr_die, DW_AT_static_link,
19118 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19121 /* Generate child dies for template paramaters. */
19122 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19123 gen_generic_params_dies (decl);
19125 /* Now output descriptions of the arguments for this function. This gets
19126 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19127 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19128 `...' at the end of the formal parameter list. In order to find out if
19129 there was a trailing ellipsis or not, we must instead look at the type
19130 associated with the FUNCTION_DECL. This will be a node of type
19131 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19132 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19133 an ellipsis at the end. */
19135 /* In the case where we are describing a mere function declaration, all we
19136 need to do here (and all we *can* do here) is to describe the *types* of
19137 its formal parameters. */
19138 if (debug_info_level <= DINFO_LEVEL_TERSE)
19140 else if (declaration)
19141 gen_formal_types_die (decl, subr_die);
19142 else
19144 /* Generate DIEs to represent all known formal parameters. */
19145 tree parm = DECL_ARGUMENTS (decl);
19146 tree generic_decl = early_dwarf
19147 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19148 tree generic_decl_parm = generic_decl
19149 ? DECL_ARGUMENTS (generic_decl)
19150 : NULL;
19152 /* Now we want to walk the list of parameters of the function and
19153 emit their relevant DIEs.
19155 We consider the case of DECL being an instance of a generic function
19156 as well as it being a normal function.
19158 If DECL is an instance of a generic function we walk the
19159 parameters of the generic function declaration _and_ the parameters of
19160 DECL itself. This is useful because we want to emit specific DIEs for
19161 function parameter packs and those are declared as part of the
19162 generic function declaration. In that particular case,
19163 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19164 That DIE has children DIEs representing the set of arguments
19165 of the pack. Note that the set of pack arguments can be empty.
19166 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19167 children DIE.
19169 Otherwise, we just consider the parameters of DECL. */
19170 while (generic_decl_parm || parm)
19172 if (generic_decl_parm
19173 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19174 gen_formal_parameter_pack_die (generic_decl_parm,
19175 parm, subr_die,
19176 &parm);
19177 else if (parm && !POINTER_BOUNDS_P (parm))
19179 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19181 if (parm == DECL_ARGUMENTS (decl)
19182 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19183 && parm_die
19184 && (dwarf_version >= 3 || !dwarf_strict))
19185 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19187 parm = DECL_CHAIN (parm);
19189 else if (parm)
19190 parm = DECL_CHAIN (parm);
19192 if (generic_decl_parm)
19193 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19196 /* Decide whether we need an unspecified_parameters DIE at the end.
19197 There are 2 more cases to do this for: 1) the ansi ... declaration -
19198 this is detectable when the end of the arg list is not a
19199 void_type_node 2) an unprototyped function declaration (not a
19200 definition). This just means that we have no info about the
19201 parameters at all. */
19202 if (prototype_p (TREE_TYPE (decl)))
19204 /* This is the prototyped case, check for.... */
19205 if (stdarg_p (TREE_TYPE (decl)))
19206 gen_unspecified_parameters_die (decl, subr_die);
19208 else if (DECL_INITIAL (decl) == NULL_TREE)
19209 gen_unspecified_parameters_die (decl, subr_die);
19212 if (subr_die != old_die)
19213 /* Add the calling convention attribute if requested. */
19214 add_calling_convention_attribute (subr_die, decl);
19216 /* Output Dwarf info for all of the stuff within the body of the function
19217 (if it has one - it may be just a declaration).
19219 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19220 a function. This BLOCK actually represents the outermost binding contour
19221 for the function, i.e. the contour in which the function's formal
19222 parameters and labels get declared. Curiously, it appears that the front
19223 end doesn't actually put the PARM_DECL nodes for the current function onto
19224 the BLOCK_VARS list for this outer scope, but are strung off of the
19225 DECL_ARGUMENTS list for the function instead.
19227 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19228 the LABEL_DECL nodes for the function however, and we output DWARF info
19229 for those in decls_for_scope. Just within the `outer_scope' there will be
19230 a BLOCK node representing the function's outermost pair of curly braces,
19231 and any blocks used for the base and member initializers of a C++
19232 constructor function. */
19233 tree outer_scope = DECL_INITIAL (decl);
19234 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19236 int call_site_note_count = 0;
19237 int tail_call_site_note_count = 0;
19239 /* Emit a DW_TAG_variable DIE for a named return value. */
19240 if (DECL_NAME (DECL_RESULT (decl)))
19241 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19243 /* The first time through decls_for_scope we will generate the
19244 DIEs for the locals. The second time, we fill in the
19245 location info. */
19246 decls_for_scope (outer_scope, subr_die);
19248 if (call_arg_locations && !dwarf_strict)
19250 struct call_arg_loc_node *ca_loc;
19251 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19253 dw_die_ref die = NULL;
19254 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19255 rtx arg, next_arg;
19257 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19258 arg; arg = next_arg)
19260 dw_loc_descr_ref reg, val;
19261 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19262 dw_die_ref cdie, tdie = NULL;
19264 next_arg = XEXP (arg, 1);
19265 if (REG_P (XEXP (XEXP (arg, 0), 0))
19266 && next_arg
19267 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19268 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19269 && REGNO (XEXP (XEXP (arg, 0), 0))
19270 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19271 next_arg = XEXP (next_arg, 1);
19272 if (mode == VOIDmode)
19274 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19275 if (mode == VOIDmode)
19276 mode = GET_MODE (XEXP (arg, 0));
19278 if (mode == VOIDmode || mode == BLKmode)
19279 continue;
19280 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19282 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19283 tloc = XEXP (XEXP (arg, 0), 1);
19284 continue;
19286 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19287 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19289 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19290 tlocc = XEXP (XEXP (arg, 0), 1);
19291 continue;
19293 reg = NULL;
19294 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19295 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19296 VAR_INIT_STATUS_INITIALIZED);
19297 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19299 rtx mem = XEXP (XEXP (arg, 0), 0);
19300 reg = mem_loc_descriptor (XEXP (mem, 0),
19301 get_address_mode (mem),
19302 GET_MODE (mem),
19303 VAR_INIT_STATUS_INITIALIZED);
19305 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19306 == DEBUG_PARAMETER_REF)
19308 tree tdecl
19309 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19310 tdie = lookup_decl_die (tdecl);
19311 if (tdie == NULL)
19312 continue;
19314 else
19315 continue;
19316 if (reg == NULL
19317 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19318 != DEBUG_PARAMETER_REF)
19319 continue;
19320 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19321 VOIDmode,
19322 VAR_INIT_STATUS_INITIALIZED);
19323 if (val == NULL)
19324 continue;
19325 if (die == NULL)
19326 die = gen_call_site_die (decl, subr_die, ca_loc);
19327 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19328 NULL_TREE);
19329 if (reg != NULL)
19330 add_AT_loc (cdie, DW_AT_location, reg);
19331 else if (tdie != NULL)
19332 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19333 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19334 if (next_arg != XEXP (arg, 1))
19336 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19337 if (mode == VOIDmode)
19338 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19339 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19340 0), 1),
19341 mode, VOIDmode,
19342 VAR_INIT_STATUS_INITIALIZED);
19343 if (val != NULL)
19344 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19347 if (die == NULL
19348 && (ca_loc->symbol_ref || tloc))
19349 die = gen_call_site_die (decl, subr_die, ca_loc);
19350 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19352 dw_loc_descr_ref tval = NULL;
19354 if (tloc != NULL_RTX)
19355 tval = mem_loc_descriptor (tloc,
19356 GET_MODE (tloc) == VOIDmode
19357 ? Pmode : GET_MODE (tloc),
19358 VOIDmode,
19359 VAR_INIT_STATUS_INITIALIZED);
19360 if (tval)
19361 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19362 else if (tlocc != NULL_RTX)
19364 tval = mem_loc_descriptor (tlocc,
19365 GET_MODE (tlocc) == VOIDmode
19366 ? Pmode : GET_MODE (tlocc),
19367 VOIDmode,
19368 VAR_INIT_STATUS_INITIALIZED);
19369 if (tval)
19370 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19371 tval);
19374 if (die != NULL)
19376 call_site_note_count++;
19377 if (ca_loc->tail_call_p)
19378 tail_call_site_note_count++;
19382 call_arg_locations = NULL;
19383 call_arg_loc_last = NULL;
19384 if (tail_call_site_count >= 0
19385 && tail_call_site_count == tail_call_site_note_count
19386 && !dwarf_strict)
19388 if (call_site_count >= 0
19389 && call_site_count == call_site_note_count)
19390 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19391 else
19392 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19394 call_site_count = -1;
19395 tail_call_site_count = -1;
19399 /* Returns a hash value for X (which really is a die_struct). */
19401 hashval_t
19402 block_die_hasher::hash (die_struct *d)
19404 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19407 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19408 as decl_id and die_parent of die_struct Y. */
19410 bool
19411 block_die_hasher::equal (die_struct *x, die_struct *y)
19413 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19416 /* Return TRUE if DECL, which may have been previously generated as
19417 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19418 true if decl (or its origin) is either an extern declaration or a
19419 class/namespace scoped declaration.
19421 The declare_in_namespace support causes us to get two DIEs for one
19422 variable, both of which are declarations. We want to avoid
19423 considering one to be a specification, so we must test for
19424 DECLARATION and DW_AT_declaration. */
19425 static inline bool
19426 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19428 return (old_die && TREE_STATIC (decl) && !declaration
19429 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19432 /* Return true if DECL is a local static. */
19434 static inline bool
19435 local_function_static (tree decl)
19437 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19438 return TREE_STATIC (decl)
19439 && DECL_CONTEXT (decl)
19440 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19443 /* Generate a DIE to represent a declared data object.
19444 Either DECL or ORIGIN must be non-null. */
19446 static void
19447 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19449 HOST_WIDE_INT off = 0;
19450 tree com_decl;
19451 tree decl_or_origin = decl ? decl : origin;
19452 tree ultimate_origin;
19453 dw_die_ref var_die;
19454 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19455 dw_die_ref origin_die = NULL;
19456 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19457 || class_or_namespace_scope_p (context_die));
19458 bool specialization_p = false;
19460 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19461 if (decl || ultimate_origin)
19462 origin = ultimate_origin;
19463 com_decl = fortran_common (decl_or_origin, &off);
19465 /* Symbol in common gets emitted as a child of the common block, in the form
19466 of a data member. */
19467 if (com_decl)
19469 dw_die_ref com_die;
19470 dw_loc_list_ref loc;
19471 die_node com_die_arg;
19473 var_die = lookup_decl_die (decl_or_origin);
19474 if (var_die)
19476 if (get_AT (var_die, DW_AT_location) == NULL)
19478 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19479 if (loc)
19481 if (off)
19483 /* Optimize the common case. */
19484 if (single_element_loc_list_p (loc)
19485 && loc->expr->dw_loc_opc == DW_OP_addr
19486 && loc->expr->dw_loc_next == NULL
19487 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19488 == SYMBOL_REF)
19490 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19491 loc->expr->dw_loc_oprnd1.v.val_addr
19492 = plus_constant (GET_MODE (x), x , off);
19494 else
19495 loc_list_plus_const (loc, off);
19497 add_AT_location_description (var_die, DW_AT_location, loc);
19498 remove_AT (var_die, DW_AT_declaration);
19501 return;
19504 if (common_block_die_table == NULL)
19505 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19507 com_die_arg.decl_id = DECL_UID (com_decl);
19508 com_die_arg.die_parent = context_die;
19509 com_die = common_block_die_table->find (&com_die_arg);
19510 loc = loc_list_from_tree (com_decl, 2, NULL);
19511 if (com_die == NULL)
19513 const char *cnam
19514 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19515 die_node **slot;
19517 com_die = new_die (DW_TAG_common_block, context_die, decl);
19518 add_name_and_src_coords_attributes (com_die, com_decl);
19519 if (loc)
19521 add_AT_location_description (com_die, DW_AT_location, loc);
19522 /* Avoid sharing the same loc descriptor between
19523 DW_TAG_common_block and DW_TAG_variable. */
19524 loc = loc_list_from_tree (com_decl, 2, NULL);
19526 else if (DECL_EXTERNAL (decl))
19527 add_AT_flag (com_die, DW_AT_declaration, 1);
19528 if (want_pubnames ())
19529 add_pubname_string (cnam, com_die); /* ??? needed? */
19530 com_die->decl_id = DECL_UID (com_decl);
19531 slot = common_block_die_table->find_slot (com_die, INSERT);
19532 *slot = com_die;
19534 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19536 add_AT_location_description (com_die, DW_AT_location, loc);
19537 loc = loc_list_from_tree (com_decl, 2, NULL);
19538 remove_AT (com_die, DW_AT_declaration);
19540 var_die = new_die (DW_TAG_variable, com_die, decl);
19541 add_name_and_src_coords_attributes (var_die, decl);
19542 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19543 context_die);
19544 add_AT_flag (var_die, DW_AT_external, 1);
19545 if (loc)
19547 if (off)
19549 /* Optimize the common case. */
19550 if (single_element_loc_list_p (loc)
19551 && loc->expr->dw_loc_opc == DW_OP_addr
19552 && loc->expr->dw_loc_next == NULL
19553 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19555 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19556 loc->expr->dw_loc_oprnd1.v.val_addr
19557 = plus_constant (GET_MODE (x), x, off);
19559 else
19560 loc_list_plus_const (loc, off);
19562 add_AT_location_description (var_die, DW_AT_location, loc);
19564 else if (DECL_EXTERNAL (decl))
19565 add_AT_flag (var_die, DW_AT_declaration, 1);
19566 equate_decl_number_to_die (decl, var_die);
19567 return;
19570 if (old_die)
19572 if (declaration)
19574 /* A declaration that has been previously dumped, needs no
19575 further annotations, since it doesn't need location on
19576 the second pass. */
19577 return;
19579 else if (decl_will_get_specification_p (old_die, decl, declaration)
19580 && !get_AT (old_die, DW_AT_specification))
19582 /* Fall-thru so we can make a new variable die along with a
19583 DW_AT_specification. */
19585 else if (origin && old_die->die_parent != context_die)
19587 /* If we will be creating an inlined instance, we need a
19588 new DIE that will get annotated with
19589 DW_AT_abstract_origin. Clear things so we can get a
19590 new DIE. */
19591 gcc_assert (!DECL_ABSTRACT_P (decl));
19592 old_die = NULL;
19594 else
19596 /* If a DIE was dumped early, it still needs location info.
19597 Skip to where we fill the location bits. */
19598 var_die = old_die;
19599 goto gen_variable_die_location;
19603 /* For static data members, the declaration in the class is supposed
19604 to have DW_TAG_member tag; the specification should still be
19605 DW_TAG_variable referencing the DW_TAG_member DIE. */
19606 if (declaration && class_scope_p (context_die))
19607 var_die = new_die (DW_TAG_member, context_die, decl);
19608 else
19609 var_die = new_die (DW_TAG_variable, context_die, decl);
19611 if (origin != NULL)
19612 origin_die = add_abstract_origin_attribute (var_die, origin);
19614 /* Loop unrolling can create multiple blocks that refer to the same
19615 static variable, so we must test for the DW_AT_declaration flag.
19617 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19618 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19619 sharing them.
19621 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19622 else if (decl_will_get_specification_p (old_die, decl, declaration))
19624 /* This is a definition of a C++ class level static. */
19625 add_AT_specification (var_die, old_die);
19626 specialization_p = true;
19627 if (DECL_NAME (decl))
19629 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19630 struct dwarf_file_data * file_index = lookup_filename (s.file);
19632 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19633 add_AT_file (var_die, DW_AT_decl_file, file_index);
19635 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19636 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19638 if (old_die->die_tag == DW_TAG_member)
19639 add_linkage_name (var_die, decl);
19642 else
19643 add_name_and_src_coords_attributes (var_die, decl);
19645 if ((origin == NULL && !specialization_p)
19646 || (origin != NULL
19647 && !DECL_ABSTRACT_P (decl_or_origin)
19648 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19649 decl_function_context
19650 (decl_or_origin))))
19652 tree type = TREE_TYPE (decl_or_origin);
19654 if (decl_by_reference_p (decl_or_origin))
19655 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19656 context_die);
19657 else
19658 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19659 context_die);
19662 if (origin == NULL && !specialization_p)
19664 if (TREE_PUBLIC (decl))
19665 add_AT_flag (var_die, DW_AT_external, 1);
19667 if (DECL_ARTIFICIAL (decl))
19668 add_AT_flag (var_die, DW_AT_artificial, 1);
19670 add_accessibility_attribute (var_die, decl);
19673 if (declaration)
19674 add_AT_flag (var_die, DW_AT_declaration, 1);
19676 if (decl && (DECL_ABSTRACT_P (decl)
19677 || !old_die || is_declaration_die (old_die)))
19678 equate_decl_number_to_die (decl, var_die);
19680 gen_variable_die_location:
19681 if (! declaration
19682 && (! DECL_ABSTRACT_P (decl_or_origin)
19683 /* Local static vars are shared between all clones/inlines,
19684 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19685 already set. */
19686 || (TREE_CODE (decl_or_origin) == VAR_DECL
19687 && TREE_STATIC (decl_or_origin)
19688 && DECL_RTL_SET_P (decl_or_origin)))
19689 /* When abstract origin already has DW_AT_location attribute, no need
19690 to add it again. */
19691 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19693 if (early_dwarf)
19694 add_pubname (decl_or_origin, var_die);
19695 else
19696 add_location_or_const_value_attribute (var_die, decl_or_origin,
19697 decl == NULL);
19699 else
19700 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19703 /* Generate a DIE to represent a named constant. */
19705 static void
19706 gen_const_die (tree decl, dw_die_ref context_die)
19708 dw_die_ref const_die;
19709 tree type = TREE_TYPE (decl);
19711 const_die = lookup_decl_die (decl);
19712 if (const_die)
19713 return;
19715 const_die = new_die (DW_TAG_constant, context_die, decl);
19716 equate_decl_number_to_die (decl, const_die);
19717 add_name_and_src_coords_attributes (const_die, decl);
19718 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19719 if (TREE_PUBLIC (decl))
19720 add_AT_flag (const_die, DW_AT_external, 1);
19721 if (DECL_ARTIFICIAL (decl))
19722 add_AT_flag (const_die, DW_AT_artificial, 1);
19723 tree_add_const_value_attribute_for_decl (const_die, decl);
19726 /* Generate a DIE to represent a label identifier. */
19728 static void
19729 gen_label_die (tree decl, dw_die_ref context_die)
19731 tree origin = decl_ultimate_origin (decl);
19732 dw_die_ref lbl_die = lookup_decl_die (decl);
19733 rtx insn;
19734 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19736 if (!lbl_die)
19738 lbl_die = new_die (DW_TAG_label, context_die, decl);
19739 equate_decl_number_to_die (decl, lbl_die);
19741 if (origin != NULL)
19742 add_abstract_origin_attribute (lbl_die, origin);
19743 else
19744 add_name_and_src_coords_attributes (lbl_die, decl);
19747 if (DECL_ABSTRACT_P (decl))
19748 equate_decl_number_to_die (decl, lbl_die);
19749 else
19751 insn = DECL_RTL_IF_SET (decl);
19753 /* Deleted labels are programmer specified labels which have been
19754 eliminated because of various optimizations. We still emit them
19755 here so that it is possible to put breakpoints on them. */
19756 if (insn
19757 && (LABEL_P (insn)
19758 || ((NOTE_P (insn)
19759 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19761 /* When optimization is enabled (via -O) some parts of the compiler
19762 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19763 represent source-level labels which were explicitly declared by
19764 the user. This really shouldn't be happening though, so catch
19765 it if it ever does happen. */
19766 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19768 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19769 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19771 else if (insn
19772 && NOTE_P (insn)
19773 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19774 && CODE_LABEL_NUMBER (insn) != -1)
19776 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19777 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19782 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19783 attributes to the DIE for a block STMT, to describe where the inlined
19784 function was called from. This is similar to add_src_coords_attributes. */
19786 static inline void
19787 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19789 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19791 if (dwarf_version >= 3 || !dwarf_strict)
19793 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19794 add_AT_unsigned (die, DW_AT_call_line, s.line);
19799 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19800 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19802 static inline void
19803 add_high_low_attributes (tree stmt, dw_die_ref die)
19805 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19807 if (BLOCK_FRAGMENT_CHAIN (stmt)
19808 && (dwarf_version >= 3 || !dwarf_strict))
19810 tree chain, superblock = NULL_TREE;
19811 dw_die_ref pdie;
19812 dw_attr_node *attr = NULL;
19814 if (inlined_function_outer_scope_p (stmt))
19816 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19817 BLOCK_NUMBER (stmt));
19818 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19821 /* Optimize duplicate .debug_ranges lists or even tails of
19822 lists. If this BLOCK has same ranges as its supercontext,
19823 lookup DW_AT_ranges attribute in the supercontext (and
19824 recursively so), verify that the ranges_table contains the
19825 right values and use it instead of adding a new .debug_range. */
19826 for (chain = stmt, pdie = die;
19827 BLOCK_SAME_RANGE (chain);
19828 chain = BLOCK_SUPERCONTEXT (chain))
19830 dw_attr_node *new_attr;
19832 pdie = pdie->die_parent;
19833 if (pdie == NULL)
19834 break;
19835 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19836 break;
19837 new_attr = get_AT (pdie, DW_AT_ranges);
19838 if (new_attr == NULL
19839 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19840 break;
19841 attr = new_attr;
19842 superblock = BLOCK_SUPERCONTEXT (chain);
19844 if (attr != NULL
19845 && (ranges_table[attr->dw_attr_val.v.val_offset
19846 / 2 / DWARF2_ADDR_SIZE].num
19847 == BLOCK_NUMBER (superblock))
19848 && BLOCK_FRAGMENT_CHAIN (superblock))
19850 unsigned long off = attr->dw_attr_val.v.val_offset
19851 / 2 / DWARF2_ADDR_SIZE;
19852 unsigned long supercnt = 0, thiscnt = 0;
19853 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19854 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19856 ++supercnt;
19857 gcc_checking_assert (ranges_table[off + supercnt].num
19858 == BLOCK_NUMBER (chain));
19860 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19861 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19862 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19863 ++thiscnt;
19864 gcc_assert (supercnt >= thiscnt);
19865 add_AT_range_list (die, DW_AT_ranges,
19866 ((off + supercnt - thiscnt)
19867 * 2 * DWARF2_ADDR_SIZE),
19868 false);
19869 return;
19872 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19874 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19877 add_ranges (chain);
19878 chain = BLOCK_FRAGMENT_CHAIN (chain);
19880 while (chain);
19881 add_ranges (NULL);
19883 else
19885 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19886 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19887 BLOCK_NUMBER (stmt));
19888 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19889 BLOCK_NUMBER (stmt));
19890 add_AT_low_high_pc (die, label, label_high, false);
19894 /* Generate a DIE for a lexical block. */
19896 static void
19897 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19899 dw_die_ref old_die = BLOCK_DIE (stmt);
19900 dw_die_ref stmt_die = NULL;
19901 if (!old_die)
19903 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19904 BLOCK_DIE (stmt) = stmt_die;
19907 if (BLOCK_ABSTRACT (stmt))
19909 if (old_die)
19911 #ifdef ENABLE_CHECKING
19912 /* This must have been generated early and it won't even
19913 need location information since it's a DW_AT_inline
19914 function. */
19915 for (dw_die_ref c = context_die; c; c = c->die_parent)
19916 if (c->die_tag == DW_TAG_inlined_subroutine
19917 || c->die_tag == DW_TAG_subprogram)
19919 gcc_assert (get_AT (c, DW_AT_inline));
19920 break;
19922 #endif
19923 return;
19926 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19928 /* If this is an inlined instance, create a new lexical die for
19929 anything below to attach DW_AT_abstract_origin to. */
19930 if (old_die)
19932 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19933 BLOCK_DIE (stmt) = stmt_die;
19934 old_die = NULL;
19938 if (old_die)
19939 stmt_die = old_die;
19941 /* A non abstract block whose blocks have already been reordered
19942 should have the instruction range for this block. If so, set the
19943 high/low attributes. */
19944 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19946 gcc_assert (stmt_die);
19947 add_high_low_attributes (stmt, stmt_die);
19950 decls_for_scope (stmt, stmt_die);
19953 /* Generate a DIE for an inlined subprogram. */
19955 static void
19956 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19958 tree decl;
19960 /* The instance of function that is effectively being inlined shall not
19961 be abstract. */
19962 gcc_assert (! BLOCK_ABSTRACT (stmt));
19964 decl = block_ultimate_origin (stmt);
19966 /* Make sure any inlined functions are known to be inlineable. */
19967 gcc_checking_assert (DECL_ABSTRACT_P (decl)
19968 || cgraph_function_possibly_inlined_p (decl));
19970 /* Emit info for the abstract instance first, if we haven't yet. We
19971 must emit this even if the block is abstract, otherwise when we
19972 emit the block below (or elsewhere), we may end up trying to emit
19973 a die whose origin die hasn't been emitted, and crashing. */
19974 dwarf2out_abstract_function (decl);
19976 if (! BLOCK_ABSTRACT (stmt))
19978 dw_die_ref subr_die
19979 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19981 if (call_arg_locations)
19982 BLOCK_DIE (stmt) = subr_die;
19983 add_abstract_origin_attribute (subr_die, decl);
19984 if (TREE_ASM_WRITTEN (stmt))
19985 add_high_low_attributes (stmt, subr_die);
19986 add_call_src_coords_attributes (stmt, subr_die);
19988 decls_for_scope (stmt, subr_die);
19992 /* Generate a DIE for a field in a record, or structure. */
19994 static void
19995 gen_field_die (tree decl, dw_die_ref context_die)
19997 dw_die_ref decl_die;
19999 if (TREE_TYPE (decl) == error_mark_node)
20000 return;
20002 decl_die = new_die (DW_TAG_member, context_die, decl);
20003 add_name_and_src_coords_attributes (decl_die, decl);
20004 add_type_attribute (decl_die, member_declared_type (decl),
20005 decl_quals (decl), context_die);
20007 if (DECL_BIT_FIELD_TYPE (decl))
20009 add_byte_size_attribute (decl_die, decl);
20010 add_bit_size_attribute (decl_die, decl);
20011 add_bit_offset_attribute (decl_die, decl);
20014 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20015 add_data_member_location_attribute (decl_die, decl);
20017 if (DECL_ARTIFICIAL (decl))
20018 add_AT_flag (decl_die, DW_AT_artificial, 1);
20020 add_accessibility_attribute (decl_die, decl);
20022 /* Equate decl number to die, so that we can look up this decl later on. */
20023 equate_decl_number_to_die (decl, decl_die);
20026 #if 0
20027 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20028 Use modified_type_die instead.
20029 We keep this code here just in case these types of DIEs may be needed to
20030 represent certain things in other languages (e.g. Pascal) someday. */
20032 static void
20033 gen_pointer_type_die (tree type, dw_die_ref context_die)
20035 dw_die_ref ptr_die
20036 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20038 equate_type_number_to_die (type, ptr_die);
20039 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20040 context_die);
20041 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20044 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20045 Use modified_type_die instead.
20046 We keep this code here just in case these types of DIEs may be needed to
20047 represent certain things in other languages (e.g. Pascal) someday. */
20049 static void
20050 gen_reference_type_die (tree type, dw_die_ref context_die)
20052 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20054 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20055 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20056 else
20057 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20059 equate_type_number_to_die (type, ref_die);
20060 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20061 context_die);
20062 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20064 #endif
20066 /* Generate a DIE for a pointer to a member type. */
20068 static void
20069 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20071 dw_die_ref ptr_die
20072 = new_die (DW_TAG_ptr_to_member_type,
20073 scope_die_for (type, context_die), type);
20075 equate_type_number_to_die (type, ptr_die);
20076 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20077 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20078 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20079 context_die);
20082 static char *producer_string;
20084 /* Return a heap allocated producer string including command line options
20085 if -grecord-gcc-switches. */
20087 static char *
20088 gen_producer_string (void)
20090 size_t j;
20091 auto_vec<const char *> switches;
20092 const char *language_string = lang_hooks.name;
20093 char *producer, *tail;
20094 const char *p;
20095 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20096 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20098 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20099 switch (save_decoded_options[j].opt_index)
20101 case OPT_o:
20102 case OPT_d:
20103 case OPT_dumpbase:
20104 case OPT_dumpdir:
20105 case OPT_auxbase:
20106 case OPT_auxbase_strip:
20107 case OPT_quiet:
20108 case OPT_version:
20109 case OPT_v:
20110 case OPT_w:
20111 case OPT_L:
20112 case OPT_D:
20113 case OPT_I:
20114 case OPT_U:
20115 case OPT_SPECIAL_unknown:
20116 case OPT_SPECIAL_ignore:
20117 case OPT_SPECIAL_program_name:
20118 case OPT_SPECIAL_input_file:
20119 case OPT_grecord_gcc_switches:
20120 case OPT_gno_record_gcc_switches:
20121 case OPT__output_pch_:
20122 case OPT_fdiagnostics_show_location_:
20123 case OPT_fdiagnostics_show_option:
20124 case OPT_fdiagnostics_show_caret:
20125 case OPT_fdiagnostics_color_:
20126 case OPT_fverbose_asm:
20127 case OPT____:
20128 case OPT__sysroot_:
20129 case OPT_nostdinc:
20130 case OPT_nostdinc__:
20131 case OPT_fpreprocessed:
20132 case OPT_fltrans_output_list_:
20133 case OPT_fresolution_:
20134 /* Ignore these. */
20135 continue;
20136 default:
20137 if (cl_options[save_decoded_options[j].opt_index].flags
20138 & CL_NO_DWARF_RECORD)
20139 continue;
20140 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20141 == '-');
20142 switch (save_decoded_options[j].canonical_option[0][1])
20144 case 'M':
20145 case 'i':
20146 case 'W':
20147 continue;
20148 case 'f':
20149 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20150 "dump", 4) == 0)
20151 continue;
20152 break;
20153 default:
20154 break;
20156 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20157 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20158 break;
20161 producer = XNEWVEC (char, plen + 1 + len + 1);
20162 tail = producer;
20163 sprintf (tail, "%s %s", language_string, version_string);
20164 tail += plen;
20166 FOR_EACH_VEC_ELT (switches, j, p)
20168 len = strlen (p);
20169 *tail = ' ';
20170 memcpy (tail + 1, p, len);
20171 tail += len + 1;
20174 *tail = '\0';
20175 return producer;
20178 /* Given a C and/or C++ language/version string return the "highest".
20179 C++ is assumed to be "higher" than C in this case. Used for merging
20180 LTO translation unit languages. */
20181 static const char *
20182 highest_c_language (const char *lang1, const char *lang2)
20184 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20185 return "GNU C++14";
20186 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20187 return "GNU C++11";
20188 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20189 return "GNU C++98";
20191 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20192 return "GNU C11";
20193 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20194 return "GNU C99";
20195 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20196 return "GNU C89";
20198 gcc_unreachable ();
20202 /* Generate the DIE for the compilation unit. */
20204 static dw_die_ref
20205 gen_compile_unit_die (const char *filename)
20207 dw_die_ref die;
20208 const char *language_string = lang_hooks.name;
20209 int language;
20211 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20213 if (filename)
20215 add_name_attribute (die, filename);
20216 /* Don't add cwd for <built-in>. */
20217 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20218 add_comp_dir_attribute (die);
20221 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20223 /* If our producer is LTO try to figure out a common language to use
20224 from the global list of translation units. */
20225 if (strcmp (language_string, "GNU GIMPLE") == 0)
20227 unsigned i;
20228 tree t;
20229 const char *common_lang = NULL;
20231 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20233 if (!TRANSLATION_UNIT_LANGUAGE (t))
20234 continue;
20235 if (!common_lang)
20236 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20237 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20239 else if (strncmp (common_lang, "GNU C", 5) == 0
20240 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20241 /* Mixing C and C++ is ok, use C++ in that case. */
20242 common_lang = highest_c_language (common_lang,
20243 TRANSLATION_UNIT_LANGUAGE (t));
20244 else
20246 /* Fall back to C. */
20247 common_lang = NULL;
20248 break;
20252 if (common_lang)
20253 language_string = common_lang;
20256 language = DW_LANG_C;
20257 if (strncmp (language_string, "GNU C", 5) == 0
20258 && ISDIGIT (language_string[5]))
20260 language = DW_LANG_C89;
20261 if (dwarf_version >= 3 || !dwarf_strict)
20263 if (strcmp (language_string, "GNU C89") != 0)
20264 language = DW_LANG_C99;
20266 if (dwarf_version >= 5 /* || !dwarf_strict */)
20267 if (strcmp (language_string, "GNU C11") == 0)
20268 language = DW_LANG_C11;
20271 else if (strncmp (language_string, "GNU C++", 7) == 0)
20273 language = DW_LANG_C_plus_plus;
20274 if (dwarf_version >= 5 /* || !dwarf_strict */)
20276 if (strcmp (language_string, "GNU C++11") == 0)
20277 language = DW_LANG_C_plus_plus_11;
20278 else if (strcmp (language_string, "GNU C++14") == 0)
20279 language = DW_LANG_C_plus_plus_14;
20282 else if (strcmp (language_string, "GNU F77") == 0)
20283 language = DW_LANG_Fortran77;
20284 else if (strcmp (language_string, "GNU Pascal") == 0)
20285 language = DW_LANG_Pascal83;
20286 else if (dwarf_version >= 3 || !dwarf_strict)
20288 if (strcmp (language_string, "GNU Ada") == 0)
20289 language = DW_LANG_Ada95;
20290 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20292 language = DW_LANG_Fortran95;
20293 if (dwarf_version >= 5 /* || !dwarf_strict */)
20295 if (strcmp (language_string, "GNU Fortran2003") == 0)
20296 language = DW_LANG_Fortran03;
20297 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20298 language = DW_LANG_Fortran08;
20301 else if (strcmp (language_string, "GNU Java") == 0)
20302 language = DW_LANG_Java;
20303 else if (strcmp (language_string, "GNU Objective-C") == 0)
20304 language = DW_LANG_ObjC;
20305 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20306 language = DW_LANG_ObjC_plus_plus;
20307 else if (dwarf_version >= 5 || !dwarf_strict)
20309 if (strcmp (language_string, "GNU Go") == 0)
20310 language = DW_LANG_Go;
20313 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20314 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20315 language = DW_LANG_Fortran90;
20317 add_AT_unsigned (die, DW_AT_language, language);
20319 switch (language)
20321 case DW_LANG_Fortran77:
20322 case DW_LANG_Fortran90:
20323 case DW_LANG_Fortran95:
20324 case DW_LANG_Fortran03:
20325 case DW_LANG_Fortran08:
20326 /* Fortran has case insensitive identifiers and the front-end
20327 lowercases everything. */
20328 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20329 break;
20330 default:
20331 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20332 break;
20334 return die;
20337 /* Generate the DIE for a base class. */
20339 static void
20340 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20342 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20344 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20345 add_data_member_location_attribute (die, binfo);
20347 if (BINFO_VIRTUAL_P (binfo))
20348 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20350 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20351 children, otherwise the default is DW_ACCESS_public. In DWARF2
20352 the default has always been DW_ACCESS_private. */
20353 if (access == access_public_node)
20355 if (dwarf_version == 2
20356 || context_die->die_tag == DW_TAG_class_type)
20357 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20359 else if (access == access_protected_node)
20360 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20361 else if (dwarf_version > 2
20362 && context_die->die_tag != DW_TAG_class_type)
20363 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20366 /* Generate a DIE for a class member. */
20368 static void
20369 gen_member_die (tree type, dw_die_ref context_die)
20371 tree member;
20372 tree binfo = TYPE_BINFO (type);
20373 dw_die_ref child;
20375 /* If this is not an incomplete type, output descriptions of each of its
20376 members. Note that as we output the DIEs necessary to represent the
20377 members of this record or union type, we will also be trying to output
20378 DIEs to represent the *types* of those members. However the `type'
20379 function (above) will specifically avoid generating type DIEs for member
20380 types *within* the list of member DIEs for this (containing) type except
20381 for those types (of members) which are explicitly marked as also being
20382 members of this (containing) type themselves. The g++ front- end can
20383 force any given type to be treated as a member of some other (containing)
20384 type by setting the TYPE_CONTEXT of the given (member) type to point to
20385 the TREE node representing the appropriate (containing) type. */
20387 /* First output info about the base classes. */
20388 if (binfo)
20390 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20391 int i;
20392 tree base;
20394 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20395 gen_inheritance_die (base,
20396 (accesses ? (*accesses)[i] : access_public_node),
20397 context_die);
20400 /* Now output info about the data members and type members. */
20401 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20403 /* If we thought we were generating minimal debug info for TYPE
20404 and then changed our minds, some of the member declarations
20405 may have already been defined. Don't define them again, but
20406 do put them in the right order. */
20408 child = lookup_decl_die (member);
20409 if (child)
20410 splice_child_die (context_die, child);
20411 else
20412 gen_decl_die (member, NULL, context_die);
20415 /* We do not keep type methods in type variants. */
20416 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20417 /* Now output info about the function members (if any). */
20418 if (TYPE_METHODS (type) != error_mark_node)
20419 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20421 /* Don't include clones in the member list. */
20422 if (DECL_ABSTRACT_ORIGIN (member))
20423 continue;
20424 /* Nor constructors for anonymous classes. */
20425 if (DECL_ARTIFICIAL (member)
20426 && dwarf2_name (member, 0) == NULL)
20427 continue;
20429 child = lookup_decl_die (member);
20430 if (child)
20431 splice_child_die (context_die, child);
20432 else
20433 gen_decl_die (member, NULL, context_die);
20437 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20438 is set, we pretend that the type was never defined, so we only get the
20439 member DIEs needed by later specification DIEs. */
20441 static void
20442 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20443 enum debug_info_usage usage)
20445 if (TREE_ASM_WRITTEN (type))
20447 /* Fill in the bound of variable-length fields in late dwarf if
20448 still incomplete. */
20449 if (!early_dwarf && variably_modified_type_p (type, NULL))
20450 for (tree member = TYPE_FIELDS (type);
20451 member;
20452 member = DECL_CHAIN (member))
20453 fill_variable_array_bounds (TREE_TYPE (member));
20454 return;
20457 dw_die_ref type_die = lookup_type_die (type);
20458 dw_die_ref scope_die = 0;
20459 int nested = 0;
20460 int complete = (TYPE_SIZE (type)
20461 && (! TYPE_STUB_DECL (type)
20462 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20463 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20464 complete = complete && should_emit_struct_debug (type, usage);
20466 if (type_die && ! complete)
20467 return;
20469 if (TYPE_CONTEXT (type) != NULL_TREE
20470 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20471 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20472 nested = 1;
20474 scope_die = scope_die_for (type, context_die);
20476 /* Generate child dies for template paramaters. */
20477 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20478 schedule_generic_params_dies_gen (type);
20480 if (! type_die || (nested && is_cu_die (scope_die)))
20481 /* First occurrence of type or toplevel definition of nested class. */
20483 dw_die_ref old_die = type_die;
20485 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20486 ? record_type_tag (type) : DW_TAG_union_type,
20487 scope_die, type);
20488 equate_type_number_to_die (type, type_die);
20489 if (old_die)
20490 add_AT_specification (type_die, old_die);
20491 else
20492 add_name_attribute (type_die, type_tag (type));
20494 else
20495 remove_AT (type_die, DW_AT_declaration);
20497 /* If this type has been completed, then give it a byte_size attribute and
20498 then give a list of members. */
20499 if (complete && !ns_decl)
20501 /* Prevent infinite recursion in cases where the type of some member of
20502 this type is expressed in terms of this type itself. */
20503 TREE_ASM_WRITTEN (type) = 1;
20504 add_byte_size_attribute (type_die, type);
20505 if (TYPE_STUB_DECL (type) != NULL_TREE)
20507 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20508 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20511 /* If the first reference to this type was as the return type of an
20512 inline function, then it may not have a parent. Fix this now. */
20513 if (type_die->die_parent == NULL)
20514 add_child_die (scope_die, type_die);
20516 push_decl_scope (type);
20517 gen_member_die (type, type_die);
20518 pop_decl_scope ();
20520 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20521 if (TYPE_ARTIFICIAL (type))
20522 add_AT_flag (type_die, DW_AT_artificial, 1);
20524 /* GNU extension: Record what type our vtable lives in. */
20525 if (TYPE_VFIELD (type))
20527 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20529 gen_type_die (vtype, context_die);
20530 add_AT_die_ref (type_die, DW_AT_containing_type,
20531 lookup_type_die (vtype));
20534 else
20536 add_AT_flag (type_die, DW_AT_declaration, 1);
20538 /* We don't need to do this for function-local types. */
20539 if (TYPE_STUB_DECL (type)
20540 && ! decl_function_context (TYPE_STUB_DECL (type)))
20541 vec_safe_push (incomplete_types, type);
20544 if (get_AT (type_die, DW_AT_name))
20545 add_pubtype (type, type_die);
20548 /* Generate a DIE for a subroutine _type_. */
20550 static void
20551 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20553 tree return_type = TREE_TYPE (type);
20554 dw_die_ref subr_die
20555 = new_die (DW_TAG_subroutine_type,
20556 scope_die_for (type, context_die), type);
20558 equate_type_number_to_die (type, subr_die);
20559 add_prototyped_attribute (subr_die, type);
20560 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20561 gen_formal_types_die (type, subr_die);
20563 if (get_AT (subr_die, DW_AT_name))
20564 add_pubtype (type, subr_die);
20567 /* Generate a DIE for a type definition. */
20569 static void
20570 gen_typedef_die (tree decl, dw_die_ref context_die)
20572 dw_die_ref type_die;
20573 tree origin;
20575 if (TREE_ASM_WRITTEN (decl))
20577 if (DECL_ORIGINAL_TYPE (decl))
20578 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20579 return;
20582 TREE_ASM_WRITTEN (decl) = 1;
20583 type_die = new_die (DW_TAG_typedef, context_die, decl);
20584 origin = decl_ultimate_origin (decl);
20585 if (origin != NULL)
20586 add_abstract_origin_attribute (type_die, origin);
20587 else
20589 tree type;
20591 add_name_and_src_coords_attributes (type_die, decl);
20592 if (DECL_ORIGINAL_TYPE (decl))
20594 type = DECL_ORIGINAL_TYPE (decl);
20596 if (type == error_mark_node)
20597 return;
20599 gcc_assert (type != TREE_TYPE (decl));
20600 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20602 else
20604 type = TREE_TYPE (decl);
20606 if (type == error_mark_node)
20607 return;
20609 if (is_naming_typedef_decl (TYPE_NAME (type)))
20611 /* Here, we are in the case of decl being a typedef naming
20612 an anonymous type, e.g:
20613 typedef struct {...} foo;
20614 In that case TREE_TYPE (decl) is not a typedef variant
20615 type and TYPE_NAME of the anonymous type is set to the
20616 TYPE_DECL of the typedef. This construct is emitted by
20617 the C++ FE.
20619 TYPE is the anonymous struct named by the typedef
20620 DECL. As we need the DW_AT_type attribute of the
20621 DW_TAG_typedef to point to the DIE of TYPE, let's
20622 generate that DIE right away. add_type_attribute
20623 called below will then pick (via lookup_type_die) that
20624 anonymous struct DIE. */
20625 if (!TREE_ASM_WRITTEN (type))
20626 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20628 /* This is a GNU Extension. We are adding a
20629 DW_AT_linkage_name attribute to the DIE of the
20630 anonymous struct TYPE. The value of that attribute
20631 is the name of the typedef decl naming the anonymous
20632 struct. This greatly eases the work of consumers of
20633 this debug info. */
20634 add_linkage_attr (lookup_type_die (type), decl);
20638 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20640 if (is_naming_typedef_decl (decl))
20641 /* We want that all subsequent calls to lookup_type_die with
20642 TYPE in argument yield the DW_TAG_typedef we have just
20643 created. */
20644 equate_type_number_to_die (type, type_die);
20646 add_accessibility_attribute (type_die, decl);
20649 if (DECL_ABSTRACT_P (decl))
20650 equate_decl_number_to_die (decl, type_die);
20652 if (get_AT (type_die, DW_AT_name))
20653 add_pubtype (decl, type_die);
20656 /* Generate a DIE for a struct, class, enum or union type. */
20658 static void
20659 gen_tagged_type_die (tree type,
20660 dw_die_ref context_die,
20661 enum debug_info_usage usage)
20663 int need_pop;
20665 if (type == NULL_TREE
20666 || !is_tagged_type (type))
20667 return;
20669 if (TREE_ASM_WRITTEN (type))
20670 need_pop = 0;
20671 /* If this is a nested type whose containing class hasn't been written
20672 out yet, writing it out will cover this one, too. This does not apply
20673 to instantiations of member class templates; they need to be added to
20674 the containing class as they are generated. FIXME: This hurts the
20675 idea of combining type decls from multiple TUs, since we can't predict
20676 what set of template instantiations we'll get. */
20677 else if (TYPE_CONTEXT (type)
20678 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20679 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20681 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20683 if (TREE_ASM_WRITTEN (type))
20684 return;
20686 /* If that failed, attach ourselves to the stub. */
20687 push_decl_scope (TYPE_CONTEXT (type));
20688 context_die = lookup_type_die (TYPE_CONTEXT (type));
20689 need_pop = 1;
20691 else if (TYPE_CONTEXT (type) != NULL_TREE
20692 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20694 /* If this type is local to a function that hasn't been written
20695 out yet, use a NULL context for now; it will be fixed up in
20696 decls_for_scope. */
20697 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20698 /* A declaration DIE doesn't count; nested types need to go in the
20699 specification. */
20700 if (context_die && is_declaration_die (context_die))
20701 context_die = NULL;
20702 need_pop = 0;
20704 else
20706 context_die = declare_in_namespace (type, context_die);
20707 need_pop = 0;
20710 if (TREE_CODE (type) == ENUMERAL_TYPE)
20712 /* This might have been written out by the call to
20713 declare_in_namespace. */
20714 if (!TREE_ASM_WRITTEN (type))
20715 gen_enumeration_type_die (type, context_die);
20717 else
20718 gen_struct_or_union_type_die (type, context_die, usage);
20720 if (need_pop)
20721 pop_decl_scope ();
20723 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20724 it up if it is ever completed. gen_*_type_die will set it for us
20725 when appropriate. */
20728 /* Generate a type description DIE. */
20730 static void
20731 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20732 enum debug_info_usage usage)
20734 struct array_descr_info info;
20736 if (type == NULL_TREE || type == error_mark_node)
20737 return;
20739 #ifdef ENABLE_CHECKING
20740 if (type)
20741 verify_type (type);
20742 #endif
20744 if (TYPE_NAME (type) != NULL_TREE
20745 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20746 && is_redundant_typedef (TYPE_NAME (type))
20747 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20748 /* The DECL of this type is a typedef we don't want to emit debug
20749 info for but we want debug info for its underlying typedef.
20750 This can happen for e.g, the injected-class-name of a C++
20751 type. */
20752 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20754 /* If TYPE is a typedef type variant, let's generate debug info
20755 for the parent typedef which TYPE is a type of. */
20756 if (typedef_variant_p (type))
20758 if (TREE_ASM_WRITTEN (type))
20759 return;
20761 /* Prevent broken recursion; we can't hand off to the same type. */
20762 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20764 /* Give typedefs the right scope. */
20765 context_die = scope_die_for (type, context_die);
20767 TREE_ASM_WRITTEN (type) = 1;
20769 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20770 return;
20773 /* If type is an anonymous tagged type named by a typedef, let's
20774 generate debug info for the typedef. */
20775 if (is_naming_typedef_decl (TYPE_NAME (type)))
20777 /* Use the DIE of the containing namespace as the parent DIE of
20778 the type description DIE we want to generate. */
20779 if (DECL_CONTEXT (TYPE_NAME (type))
20780 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20781 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20783 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20784 return;
20787 /* We are going to output a DIE to represent the unqualified version
20788 of this type (i.e. without any const or volatile qualifiers) so
20789 get the main variant (i.e. the unqualified version) of this type
20790 now. (Vectors are special because the debugging info is in the
20791 cloned type itself). */
20792 if (TREE_CODE (type) != VECTOR_TYPE)
20793 type = type_main_variant (type);
20795 /* If this is an array type with hidden descriptor, handle it first. */
20796 if (!TREE_ASM_WRITTEN (type)
20797 && lang_hooks.types.get_array_descr_info)
20799 memset (&info, 0, sizeof (info));
20800 if (lang_hooks.types.get_array_descr_info (type, &info))
20802 gen_descr_array_type_die (type, &info, context_die);
20803 TREE_ASM_WRITTEN (type) = 1;
20804 return;
20808 if (TREE_ASM_WRITTEN (type))
20810 /* Variable-length types may be incomplete even if
20811 TREE_ASM_WRITTEN. For such types, fall through to
20812 gen_array_type_die() and possibly fill in
20813 DW_AT_{upper,lower}_bound attributes. */
20814 if ((TREE_CODE (type) != ARRAY_TYPE
20815 && TREE_CODE (type) != RECORD_TYPE
20816 && TREE_CODE (type) != UNION_TYPE
20817 && TREE_CODE (type) != QUAL_UNION_TYPE)
20818 || !variably_modified_type_p (type, NULL))
20819 return;
20822 switch (TREE_CODE (type))
20824 case ERROR_MARK:
20825 break;
20827 case POINTER_TYPE:
20828 case REFERENCE_TYPE:
20829 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20830 ensures that the gen_type_die recursion will terminate even if the
20831 type is recursive. Recursive types are possible in Ada. */
20832 /* ??? We could perhaps do this for all types before the switch
20833 statement. */
20834 TREE_ASM_WRITTEN (type) = 1;
20836 /* For these types, all that is required is that we output a DIE (or a
20837 set of DIEs) to represent the "basis" type. */
20838 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20839 DINFO_USAGE_IND_USE);
20840 break;
20842 case OFFSET_TYPE:
20843 /* This code is used for C++ pointer-to-data-member types.
20844 Output a description of the relevant class type. */
20845 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20846 DINFO_USAGE_IND_USE);
20848 /* Output a description of the type of the object pointed to. */
20849 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20850 DINFO_USAGE_IND_USE);
20852 /* Now output a DIE to represent this pointer-to-data-member type
20853 itself. */
20854 gen_ptr_to_mbr_type_die (type, context_die);
20855 break;
20857 case FUNCTION_TYPE:
20858 /* Force out return type (in case it wasn't forced out already). */
20859 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20860 DINFO_USAGE_DIR_USE);
20861 gen_subroutine_type_die (type, context_die);
20862 break;
20864 case METHOD_TYPE:
20865 /* Force out return type (in case it wasn't forced out already). */
20866 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20867 DINFO_USAGE_DIR_USE);
20868 gen_subroutine_type_die (type, context_die);
20869 break;
20871 case ARRAY_TYPE:
20872 case VECTOR_TYPE:
20873 gen_array_type_die (type, context_die);
20874 break;
20876 case ENUMERAL_TYPE:
20877 case RECORD_TYPE:
20878 case UNION_TYPE:
20879 case QUAL_UNION_TYPE:
20880 gen_tagged_type_die (type, context_die, usage);
20881 return;
20883 case VOID_TYPE:
20884 case INTEGER_TYPE:
20885 case REAL_TYPE:
20886 case FIXED_POINT_TYPE:
20887 case COMPLEX_TYPE:
20888 case BOOLEAN_TYPE:
20889 case POINTER_BOUNDS_TYPE:
20890 /* No DIEs needed for fundamental types. */
20891 break;
20893 case NULLPTR_TYPE:
20894 case LANG_TYPE:
20895 /* Just use DW_TAG_unspecified_type. */
20897 dw_die_ref type_die = lookup_type_die (type);
20898 if (type_die == NULL)
20900 tree name = TYPE_IDENTIFIER (type);
20901 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20902 type);
20903 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20904 equate_type_number_to_die (type, type_die);
20907 break;
20909 default:
20910 if (is_cxx_auto (type))
20912 tree name = TYPE_IDENTIFIER (type);
20913 dw_die_ref *die = (name == get_identifier ("auto")
20914 ? &auto_die : &decltype_auto_die);
20915 if (!*die)
20917 *die = new_die (DW_TAG_unspecified_type,
20918 comp_unit_die (), NULL_TREE);
20919 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20921 equate_type_number_to_die (type, *die);
20922 break;
20924 gcc_unreachable ();
20927 TREE_ASM_WRITTEN (type) = 1;
20930 static void
20931 gen_type_die (tree type, dw_die_ref context_die)
20933 if (type != error_mark_node)
20935 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20936 #ifdef ENABLE_CHECKING
20937 dw_die_ref die = lookup_type_die (type);
20938 if (die)
20939 check_die (die);
20940 #endif
20944 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20945 things which are local to the given block. */
20947 static void
20948 gen_block_die (tree stmt, dw_die_ref context_die)
20950 int must_output_die = 0;
20951 bool inlined_func;
20953 /* Ignore blocks that are NULL. */
20954 if (stmt == NULL_TREE)
20955 return;
20957 inlined_func = inlined_function_outer_scope_p (stmt);
20959 /* If the block is one fragment of a non-contiguous block, do not
20960 process the variables, since they will have been done by the
20961 origin block. Do process subblocks. */
20962 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20964 tree sub;
20966 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20967 gen_block_die (sub, context_die);
20969 return;
20972 /* Determine if we need to output any Dwarf DIEs at all to represent this
20973 block. */
20974 if (inlined_func)
20975 /* The outer scopes for inlinings *must* always be represented. We
20976 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20977 must_output_die = 1;
20978 else
20980 /* Determine if this block directly contains any "significant"
20981 local declarations which we will need to output DIEs for. */
20982 if (debug_info_level > DINFO_LEVEL_TERSE)
20983 /* We are not in terse mode so *any* local declaration counts
20984 as being a "significant" one. */
20985 must_output_die = ((BLOCK_VARS (stmt) != NULL
20986 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20987 && (TREE_USED (stmt)
20988 || TREE_ASM_WRITTEN (stmt)
20989 || BLOCK_ABSTRACT (stmt)));
20990 else if ((TREE_USED (stmt)
20991 || TREE_ASM_WRITTEN (stmt)
20992 || BLOCK_ABSTRACT (stmt))
20993 && !dwarf2out_ignore_block (stmt))
20994 must_output_die = 1;
20997 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20998 DIE for any block which contains no significant local declarations at
20999 all. Rather, in such cases we just call `decls_for_scope' so that any
21000 needed Dwarf info for any sub-blocks will get properly generated. Note
21001 that in terse mode, our definition of what constitutes a "significant"
21002 local declaration gets restricted to include only inlined function
21003 instances and local (nested) function definitions. */
21004 if (must_output_die)
21006 if (inlined_func)
21008 /* If STMT block is abstract, that means we have been called
21009 indirectly from dwarf2out_abstract_function.
21010 That function rightfully marks the descendent blocks (of
21011 the abstract function it is dealing with) as being abstract,
21012 precisely to prevent us from emitting any
21013 DW_TAG_inlined_subroutine DIE as a descendent
21014 of an abstract function instance. So in that case, we should
21015 not call gen_inlined_subroutine_die.
21017 Later though, when cgraph asks dwarf2out to emit info
21018 for the concrete instance of the function decl into which
21019 the concrete instance of STMT got inlined, the later will lead
21020 to the generation of a DW_TAG_inlined_subroutine DIE. */
21021 if (! BLOCK_ABSTRACT (stmt))
21022 gen_inlined_subroutine_die (stmt, context_die);
21024 else
21025 gen_lexical_block_die (stmt, context_die);
21027 else
21028 decls_for_scope (stmt, context_die);
21031 /* Process variable DECL (or variable with origin ORIGIN) within
21032 block STMT and add it to CONTEXT_DIE. */
21033 static void
21034 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21036 dw_die_ref die;
21037 tree decl_or_origin = decl ? decl : origin;
21039 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21040 die = lookup_decl_die (decl_or_origin);
21041 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21042 && TYPE_DECL_IS_STUB (decl_or_origin))
21043 die = lookup_type_die (TREE_TYPE (decl_or_origin));
21044 else
21045 die = NULL;
21047 if (die != NULL && die->die_parent == NULL)
21048 add_child_die (context_die, die);
21049 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21051 if (early_dwarf)
21052 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21053 stmt, context_die);
21055 else
21056 gen_decl_die (decl, origin, context_die);
21059 /* Generate all of the decls declared within a given scope and (recursively)
21060 all of its sub-blocks. */
21062 static void
21063 decls_for_scope (tree stmt, dw_die_ref context_die)
21065 tree decl;
21066 unsigned int i;
21067 tree subblocks;
21069 /* Ignore NULL blocks. */
21070 if (stmt == NULL_TREE)
21071 return;
21073 /* Output the DIEs to represent all of the data objects and typedefs
21074 declared directly within this block but not within any nested
21075 sub-blocks. Also, nested function and tag DIEs have been
21076 generated with a parent of NULL; fix that up now. We don't
21077 have to do this if we're at -g1. */
21078 if (debug_info_level > DINFO_LEVEL_TERSE)
21080 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21081 process_scope_var (stmt, decl, NULL_TREE, context_die);
21082 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21083 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21084 context_die);
21087 /* Even if we're at -g1, we need to process the subblocks in order to get
21088 inlined call information. */
21090 /* Output the DIEs to represent all sub-blocks (and the items declared
21091 therein) of this block. */
21092 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21093 subblocks != NULL;
21094 subblocks = BLOCK_CHAIN (subblocks))
21095 gen_block_die (subblocks, context_die);
21098 /* Is this a typedef we can avoid emitting? */
21100 static inline int
21101 is_redundant_typedef (const_tree decl)
21103 if (TYPE_DECL_IS_STUB (decl))
21104 return 1;
21106 if (DECL_ARTIFICIAL (decl)
21107 && DECL_CONTEXT (decl)
21108 && is_tagged_type (DECL_CONTEXT (decl))
21109 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21110 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21111 /* Also ignore the artificial member typedef for the class name. */
21112 return 1;
21114 return 0;
21117 /* Return TRUE if TYPE is a typedef that names a type for linkage
21118 purposes. This kind of typedefs is produced by the C++ FE for
21119 constructs like:
21121 typedef struct {...} foo;
21123 In that case, there is no typedef variant type produced for foo.
21124 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21125 struct type. */
21127 static bool
21128 is_naming_typedef_decl (const_tree decl)
21130 if (decl == NULL_TREE
21131 || TREE_CODE (decl) != TYPE_DECL
21132 || !is_tagged_type (TREE_TYPE (decl))
21133 || DECL_IS_BUILTIN (decl)
21134 || is_redundant_typedef (decl)
21135 /* It looks like Ada produces TYPE_DECLs that are very similar
21136 to C++ naming typedefs but that have different
21137 semantics. Let's be specific to c++ for now. */
21138 || !is_cxx ())
21139 return FALSE;
21141 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21142 && TYPE_NAME (TREE_TYPE (decl)) == decl
21143 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21144 != TYPE_NAME (TREE_TYPE (decl))));
21147 /* Looks up the DIE for a context. */
21149 static inline dw_die_ref
21150 lookup_context_die (tree context)
21152 if (context)
21154 /* Find die that represents this context. */
21155 if (TYPE_P (context))
21157 context = TYPE_MAIN_VARIANT (context);
21158 dw_die_ref ctx = lookup_type_die (context);
21159 if (!ctx)
21160 return NULL;
21161 return strip_naming_typedef (context, ctx);
21163 else
21164 return lookup_decl_die (context);
21166 return comp_unit_die ();
21169 /* Returns the DIE for a context. */
21171 static inline dw_die_ref
21172 get_context_die (tree context)
21174 if (context)
21176 /* Find die that represents this context. */
21177 if (TYPE_P (context))
21179 context = TYPE_MAIN_VARIANT (context);
21180 return strip_naming_typedef (context, force_type_die (context));
21182 else
21183 return force_decl_die (context);
21185 return comp_unit_die ();
21188 /* Returns the DIE for decl. A DIE will always be returned. */
21190 static dw_die_ref
21191 force_decl_die (tree decl)
21193 dw_die_ref decl_die;
21194 unsigned saved_external_flag;
21195 tree save_fn = NULL_TREE;
21196 decl_die = lookup_decl_die (decl);
21197 if (!decl_die)
21199 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21201 decl_die = lookup_decl_die (decl);
21202 if (decl_die)
21203 return decl_die;
21205 switch (TREE_CODE (decl))
21207 case FUNCTION_DECL:
21208 /* Clear current_function_decl, so that gen_subprogram_die thinks
21209 that this is a declaration. At this point, we just want to force
21210 declaration die. */
21211 save_fn = current_function_decl;
21212 current_function_decl = NULL_TREE;
21213 gen_subprogram_die (decl, context_die);
21214 current_function_decl = save_fn;
21215 break;
21217 case VAR_DECL:
21218 /* Set external flag to force declaration die. Restore it after
21219 gen_decl_die() call. */
21220 saved_external_flag = DECL_EXTERNAL (decl);
21221 DECL_EXTERNAL (decl) = 1;
21222 gen_decl_die (decl, NULL, context_die);
21223 DECL_EXTERNAL (decl) = saved_external_flag;
21224 break;
21226 case NAMESPACE_DECL:
21227 if (dwarf_version >= 3 || !dwarf_strict)
21228 dwarf2out_decl (decl);
21229 else
21230 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21231 decl_die = comp_unit_die ();
21232 break;
21234 case TRANSLATION_UNIT_DECL:
21235 decl_die = comp_unit_die ();
21236 break;
21238 default:
21239 gcc_unreachable ();
21242 /* We should be able to find the DIE now. */
21243 if (!decl_die)
21244 decl_die = lookup_decl_die (decl);
21245 gcc_assert (decl_die);
21248 return decl_die;
21251 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21252 always returned. */
21254 static dw_die_ref
21255 force_type_die (tree type)
21257 dw_die_ref type_die;
21259 type_die = lookup_type_die (type);
21260 if (!type_die)
21262 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21264 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21265 context_die);
21266 gcc_assert (type_die);
21268 return type_die;
21271 /* Force out any required namespaces to be able to output DECL,
21272 and return the new context_die for it, if it's changed. */
21274 static dw_die_ref
21275 setup_namespace_context (tree thing, dw_die_ref context_die)
21277 tree context = (DECL_P (thing)
21278 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21279 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21280 /* Force out the namespace. */
21281 context_die = force_decl_die (context);
21283 return context_die;
21286 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21287 type) within its namespace, if appropriate.
21289 For compatibility with older debuggers, namespace DIEs only contain
21290 declarations; all definitions are emitted at CU scope, with
21291 DW_AT_specification pointing to the declaration (like with class
21292 members). */
21294 static dw_die_ref
21295 declare_in_namespace (tree thing, dw_die_ref context_die)
21297 dw_die_ref ns_context;
21299 if (debug_info_level <= DINFO_LEVEL_TERSE)
21300 return context_die;
21302 /* External declarations in the local scope only need to be emitted
21303 once, not once in the namespace and once in the scope.
21305 This avoids declaring the `extern' below in the
21306 namespace DIE as well as in the innermost scope:
21308 namespace S
21310 int i=5;
21311 int foo()
21313 int i=8;
21314 extern int i;
21315 return i;
21319 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21320 return context_die;
21322 /* If this decl is from an inlined function, then don't try to emit it in its
21323 namespace, as we will get confused. It would have already been emitted
21324 when the abstract instance of the inline function was emitted anyways. */
21325 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21326 return context_die;
21328 ns_context = setup_namespace_context (thing, context_die);
21330 if (ns_context != context_die)
21332 if (is_fortran ())
21333 return ns_context;
21334 if (DECL_P (thing))
21335 gen_decl_die (thing, NULL, ns_context);
21336 else
21337 gen_type_die (thing, ns_context);
21339 return context_die;
21342 /* Generate a DIE for a namespace or namespace alias. */
21344 static void
21345 gen_namespace_die (tree decl, dw_die_ref context_die)
21347 dw_die_ref namespace_die;
21349 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21350 they are an alias of. */
21351 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21353 /* Output a real namespace or module. */
21354 context_die = setup_namespace_context (decl, comp_unit_die ());
21355 namespace_die = new_die (is_fortran ()
21356 ? DW_TAG_module : DW_TAG_namespace,
21357 context_die, decl);
21358 /* For Fortran modules defined in different CU don't add src coords. */
21359 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21361 const char *name = dwarf2_name (decl, 0);
21362 if (name)
21363 add_name_attribute (namespace_die, name);
21365 else
21366 add_name_and_src_coords_attributes (namespace_die, decl);
21367 if (DECL_EXTERNAL (decl))
21368 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21369 equate_decl_number_to_die (decl, namespace_die);
21371 else
21373 /* Output a namespace alias. */
21375 /* Force out the namespace we are an alias of, if necessary. */
21376 dw_die_ref origin_die
21377 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21379 if (DECL_FILE_SCOPE_P (decl)
21380 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21381 context_die = setup_namespace_context (decl, comp_unit_die ());
21382 /* Now create the namespace alias DIE. */
21383 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21384 add_name_and_src_coords_attributes (namespace_die, decl);
21385 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21386 equate_decl_number_to_die (decl, namespace_die);
21388 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21389 if (want_pubnames ())
21390 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21393 /* Generate Dwarf debug information for a decl described by DECL.
21394 The return value is currently only meaningful for PARM_DECLs,
21395 for all other decls it returns NULL. */
21397 static dw_die_ref
21398 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21400 tree decl_or_origin = decl ? decl : origin;
21401 tree class_origin = NULL, ultimate_origin;
21403 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21404 return NULL;
21406 /* Ignore pointer bounds decls. */
21407 if (DECL_P (decl_or_origin)
21408 && TREE_TYPE (decl_or_origin)
21409 && POINTER_BOUNDS_P (decl_or_origin))
21410 return NULL;
21412 switch (TREE_CODE (decl_or_origin))
21414 case ERROR_MARK:
21415 break;
21417 case CONST_DECL:
21418 if (!is_fortran () && !is_ada ())
21420 /* The individual enumerators of an enum type get output when we output
21421 the Dwarf representation of the relevant enum type itself. */
21422 break;
21425 /* Emit its type. */
21426 gen_type_die (TREE_TYPE (decl), context_die);
21428 /* And its containing namespace. */
21429 context_die = declare_in_namespace (decl, context_die);
21431 gen_const_die (decl, context_die);
21432 break;
21434 case FUNCTION_DECL:
21435 /* Don't output any DIEs to represent mere function declarations,
21436 unless they are class members or explicit block externs. */
21437 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21438 && DECL_FILE_SCOPE_P (decl_or_origin)
21439 && (current_function_decl == NULL_TREE
21440 || DECL_ARTIFICIAL (decl_or_origin)))
21441 break;
21443 #if 0
21444 /* FIXME */
21445 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21446 on local redeclarations of global functions. That seems broken. */
21447 if (current_function_decl != decl)
21448 /* This is only a declaration. */;
21449 #endif
21451 /* If we're emitting a clone, emit info for the abstract instance. */
21452 if (origin || DECL_ORIGIN (decl) != decl)
21453 dwarf2out_abstract_function (origin
21454 ? DECL_ORIGIN (origin)
21455 : DECL_ABSTRACT_ORIGIN (decl));
21457 /* If we're emitting an out-of-line copy of an inline function,
21458 emit info for the abstract instance and set up to refer to it. */
21459 else if (cgraph_function_possibly_inlined_p (decl)
21460 && ! DECL_ABSTRACT_P (decl)
21461 && ! class_or_namespace_scope_p (context_die)
21462 /* dwarf2out_abstract_function won't emit a die if this is just
21463 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21464 that case, because that works only if we have a die. */
21465 && DECL_INITIAL (decl) != NULL_TREE)
21467 dwarf2out_abstract_function (decl);
21468 set_decl_origin_self (decl);
21471 /* Otherwise we're emitting the primary DIE for this decl. */
21472 else if (debug_info_level > DINFO_LEVEL_TERSE)
21474 /* Before we describe the FUNCTION_DECL itself, make sure that we
21475 have its containing type. */
21476 if (!origin)
21477 origin = decl_class_context (decl);
21478 if (origin != NULL_TREE)
21479 gen_type_die (origin, context_die);
21481 /* And its return type. */
21482 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21484 /* And its virtual context. */
21485 if (DECL_VINDEX (decl) != NULL_TREE)
21486 gen_type_die (DECL_CONTEXT (decl), context_die);
21488 /* Make sure we have a member DIE for decl. */
21489 if (origin != NULL_TREE)
21490 gen_type_die_for_member (origin, decl, context_die);
21492 /* And its containing namespace. */
21493 context_die = declare_in_namespace (decl, context_die);
21496 /* Now output a DIE to represent the function itself. */
21497 if (decl)
21498 gen_subprogram_die (decl, context_die);
21499 break;
21501 case TYPE_DECL:
21502 /* If we are in terse mode, don't generate any DIEs to represent any
21503 actual typedefs. */
21504 if (debug_info_level <= DINFO_LEVEL_TERSE)
21505 break;
21507 /* In the special case of a TYPE_DECL node representing the declaration
21508 of some type tag, if the given TYPE_DECL is marked as having been
21509 instantiated from some other (original) TYPE_DECL node (e.g. one which
21510 was generated within the original definition of an inline function) we
21511 used to generate a special (abbreviated) DW_TAG_structure_type,
21512 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21513 should be actually referencing those DIEs, as variable DIEs with that
21514 type would be emitted already in the abstract origin, so it was always
21515 removed during unused type prunning. Don't add anything in this
21516 case. */
21517 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21518 break;
21520 if (is_redundant_typedef (decl))
21521 gen_type_die (TREE_TYPE (decl), context_die);
21522 else
21523 /* Output a DIE to represent the typedef itself. */
21524 gen_typedef_die (decl, context_die);
21525 break;
21527 case LABEL_DECL:
21528 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21529 gen_label_die (decl, context_die);
21530 break;
21532 case VAR_DECL:
21533 case RESULT_DECL:
21534 /* If we are in terse mode, don't generate any DIEs to represent any
21535 variable declarations or definitions. */
21536 if (debug_info_level <= DINFO_LEVEL_TERSE)
21537 break;
21539 /* Output any DIEs that are needed to specify the type of this data
21540 object. */
21541 if (decl_by_reference_p (decl_or_origin))
21542 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21543 else
21544 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21546 /* And its containing type. */
21547 class_origin = decl_class_context (decl_or_origin);
21548 if (class_origin != NULL_TREE)
21549 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21551 /* And its containing namespace. */
21552 context_die = declare_in_namespace (decl_or_origin, context_die);
21554 /* Now output the DIE to represent the data object itself. This gets
21555 complicated because of the possibility that the VAR_DECL really
21556 represents an inlined instance of a formal parameter for an inline
21557 function. */
21558 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21559 if (ultimate_origin != NULL_TREE
21560 && TREE_CODE (ultimate_origin) == PARM_DECL)
21561 gen_formal_parameter_die (decl, origin,
21562 true /* Emit name attribute. */,
21563 context_die);
21564 else
21565 gen_variable_die (decl, origin, context_die);
21566 break;
21568 case FIELD_DECL:
21569 /* Ignore the nameless fields that are used to skip bits but handle C++
21570 anonymous unions and structs. */
21571 if (DECL_NAME (decl) != NULL_TREE
21572 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21573 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21575 gen_type_die (member_declared_type (decl), context_die);
21576 gen_field_die (decl, context_die);
21578 break;
21580 case PARM_DECL:
21581 if (DECL_BY_REFERENCE (decl_or_origin))
21582 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21583 else
21584 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21585 return gen_formal_parameter_die (decl, origin,
21586 true /* Emit name attribute. */,
21587 context_die);
21589 case NAMESPACE_DECL:
21590 case IMPORTED_DECL:
21591 if (dwarf_version >= 3 || !dwarf_strict)
21592 gen_namespace_die (decl, context_die);
21593 break;
21595 case NAMELIST_DECL:
21596 gen_namelist_decl (DECL_NAME (decl), context_die,
21597 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21598 break;
21600 default:
21601 /* Probably some frontend-internal decl. Assume we don't care. */
21602 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21603 break;
21606 return NULL;
21609 /* Output initial debug information for global DECL. Called at the
21610 end of the parsing process.
21612 This is the initial debug generation process. As such, the DIEs
21613 generated may be incomplete. A later debug generation pass
21614 (dwarf2out_late_global_decl) will augment the information generated
21615 in this pass (e.g., with complete location info). */
21617 static void
21618 dwarf2out_early_global_decl (tree decl)
21620 set_early_dwarf s;
21622 /* gen_decl_die() will set DECL_ABSTRACT because
21623 cgraph_function_possibly_inlined_p() returns true. This is in
21624 turn will cause DW_AT_inline attributes to be set.
21626 This happens because at early dwarf generation, there is no
21627 cgraph information, causing cgraph_function_possibly_inlined_p()
21628 to return true. Trick cgraph_function_possibly_inlined_p()
21629 while we generate dwarf early. */
21630 bool save = symtab->global_info_ready;
21631 symtab->global_info_ready = true;
21633 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21634 other DECLs and they can point to template types or other things
21635 that dwarf2out can't handle when done via dwarf2out_decl. */
21636 if (TREE_CODE (decl) != TYPE_DECL
21637 && TREE_CODE (decl) != PARM_DECL)
21639 tree save_fndecl = current_function_decl;
21640 if (TREE_CODE (decl) == FUNCTION_DECL)
21642 /* No cfun means the symbol has no body, so there's nothing
21643 to emit. */
21644 if (!DECL_STRUCT_FUNCTION (decl))
21645 goto early_decl_exit;
21647 current_function_decl = decl;
21649 dwarf2out_decl (decl);
21650 if (TREE_CODE (decl) == FUNCTION_DECL)
21651 current_function_decl = save_fndecl;
21653 early_decl_exit:
21654 symtab->global_info_ready = save;
21657 /* Output debug information for global decl DECL. Called from
21658 toplev.c after compilation proper has finished. */
21660 static void
21661 dwarf2out_late_global_decl (tree decl)
21663 /* We have to generate early debug late for LTO. */
21664 if (in_lto_p)
21665 dwarf2out_early_global_decl (decl);
21667 /* Fill-in any location information we were unable to determine
21668 on the first pass. */
21669 if (TREE_CODE (decl) == VAR_DECL
21670 && !POINTER_BOUNDS_P (decl))
21672 dw_die_ref die = lookup_decl_die (decl);
21673 if (die)
21674 add_location_or_const_value_attribute (die, decl, false);
21678 /* Output debug information for type decl DECL. Called from toplev.c
21679 and from language front ends (to record built-in types). */
21680 static void
21681 dwarf2out_type_decl (tree decl, int local)
21683 if (!local)
21685 set_early_dwarf s;
21686 dwarf2out_decl (decl);
21690 /* Output debug information for imported module or decl DECL.
21691 NAME is non-NULL name in the lexical block if the decl has been renamed.
21692 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21693 that DECL belongs to.
21694 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21695 static void
21696 dwarf2out_imported_module_or_decl_1 (tree decl,
21697 tree name,
21698 tree lexical_block,
21699 dw_die_ref lexical_block_die)
21701 expanded_location xloc;
21702 dw_die_ref imported_die = NULL;
21703 dw_die_ref at_import_die;
21705 if (TREE_CODE (decl) == IMPORTED_DECL)
21707 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21708 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21709 gcc_assert (decl);
21711 else
21712 xloc = expand_location (input_location);
21714 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21716 at_import_die = force_type_die (TREE_TYPE (decl));
21717 /* For namespace N { typedef void T; } using N::T; base_type_die
21718 returns NULL, but DW_TAG_imported_declaration requires
21719 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21720 if (!at_import_die)
21722 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21723 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21724 at_import_die = lookup_type_die (TREE_TYPE (decl));
21725 gcc_assert (at_import_die);
21728 else
21730 at_import_die = lookup_decl_die (decl);
21731 if (!at_import_die)
21733 /* If we're trying to avoid duplicate debug info, we may not have
21734 emitted the member decl for this field. Emit it now. */
21735 if (TREE_CODE (decl) == FIELD_DECL)
21737 tree type = DECL_CONTEXT (decl);
21739 if (TYPE_CONTEXT (type)
21740 && TYPE_P (TYPE_CONTEXT (type))
21741 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21742 DINFO_USAGE_DIR_USE))
21743 return;
21744 gen_type_die_for_member (type, decl,
21745 get_context_die (TYPE_CONTEXT (type)));
21747 if (TREE_CODE (decl) == NAMELIST_DECL)
21748 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21749 get_context_die (DECL_CONTEXT (decl)),
21750 NULL_TREE);
21751 else
21752 at_import_die = force_decl_die (decl);
21756 if (TREE_CODE (decl) == NAMESPACE_DECL)
21758 if (dwarf_version >= 3 || !dwarf_strict)
21759 imported_die = new_die (DW_TAG_imported_module,
21760 lexical_block_die,
21761 lexical_block);
21762 else
21763 return;
21765 else
21766 imported_die = new_die (DW_TAG_imported_declaration,
21767 lexical_block_die,
21768 lexical_block);
21770 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21771 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21772 if (name)
21773 add_AT_string (imported_die, DW_AT_name,
21774 IDENTIFIER_POINTER (name));
21775 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21778 /* Output debug information for imported module or decl DECL.
21779 NAME is non-NULL name in context if the decl has been renamed.
21780 CHILD is true if decl is one of the renamed decls as part of
21781 importing whole module. */
21783 static void
21784 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21785 bool child)
21787 /* dw_die_ref at_import_die; */
21788 dw_die_ref scope_die;
21790 if (debug_info_level <= DINFO_LEVEL_TERSE)
21791 return;
21793 gcc_assert (decl);
21795 set_early_dwarf s;
21797 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21798 We need decl DIE for reference and scope die. First, get DIE for the decl
21799 itself. */
21801 /* Get the scope die for decl context. Use comp_unit_die for global module
21802 or decl. If die is not found for non globals, force new die. */
21803 if (context
21804 && TYPE_P (context)
21805 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21806 return;
21808 if (!(dwarf_version >= 3 || !dwarf_strict))
21809 return;
21811 scope_die = get_context_die (context);
21813 if (child)
21815 gcc_assert (scope_die->die_child);
21816 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21817 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21818 scope_die = scope_die->die_child;
21821 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21822 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21825 /* Output debug information for namelists. */
21827 static dw_die_ref
21828 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21830 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21831 tree value;
21832 unsigned i;
21834 if (debug_info_level <= DINFO_LEVEL_TERSE)
21835 return NULL;
21837 gcc_assert (scope_die != NULL);
21838 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21839 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21841 /* If there are no item_decls, we have a nondefining namelist, e.g.
21842 with USE association; hence, set DW_AT_declaration. */
21843 if (item_decls == NULL_TREE)
21845 add_AT_flag (nml_die, DW_AT_declaration, 1);
21846 return nml_die;
21849 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21851 nml_item_ref_die = lookup_decl_die (value);
21852 if (!nml_item_ref_die)
21853 nml_item_ref_die = force_decl_die (value);
21855 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21856 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21858 return nml_die;
21862 /* Write the debugging output for DECL and return the DIE. */
21864 static void
21865 dwarf2out_decl (tree decl)
21867 dw_die_ref context_die = comp_unit_die ();
21869 switch (TREE_CODE (decl))
21871 case ERROR_MARK:
21872 return;
21874 case FUNCTION_DECL:
21875 /* What we would really like to do here is to filter out all mere
21876 file-scope declarations of file-scope functions which are never
21877 referenced later within this translation unit (and keep all of ones
21878 that *are* referenced later on) but we aren't clairvoyant, so we have
21879 no idea which functions will be referenced in the future (i.e. later
21880 on within the current translation unit). So here we just ignore all
21881 file-scope function declarations which are not also definitions. If
21882 and when the debugger needs to know something about these functions,
21883 it will have to hunt around and find the DWARF information associated
21884 with the definition of the function.
21886 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21887 nodes represent definitions and which ones represent mere
21888 declarations. We have to check DECL_INITIAL instead. That's because
21889 the C front-end supports some weird semantics for "extern inline"
21890 function definitions. These can get inlined within the current
21891 translation unit (and thus, we need to generate Dwarf info for their
21892 abstract instances so that the Dwarf info for the concrete inlined
21893 instances can have something to refer to) but the compiler never
21894 generates any out-of-lines instances of such things (despite the fact
21895 that they *are* definitions).
21897 The important point is that the C front-end marks these "extern
21898 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21899 them anyway. Note that the C++ front-end also plays some similar games
21900 for inline function definitions appearing within include files which
21901 also contain `#pragma interface' pragmas.
21903 If we are called from dwarf2out_abstract_function output a DIE
21904 anyway. We can end up here this way with early inlining and LTO
21905 where the inlined function is output in a different LTRANS unit
21906 or not at all. */
21907 if (DECL_INITIAL (decl) == NULL_TREE
21908 && ! DECL_ABSTRACT_P (decl))
21909 return;
21911 /* If we're a nested function, initially use a parent of NULL; if we're
21912 a plain function, this will be fixed up in decls_for_scope. If
21913 we're a method, it will be ignored, since we already have a DIE. */
21914 if (decl_function_context (decl)
21915 /* But if we're in terse mode, we don't care about scope. */
21916 && debug_info_level > DINFO_LEVEL_TERSE)
21917 context_die = NULL;
21918 break;
21920 case VAR_DECL:
21921 /* For local statics lookup proper context die. */
21922 if (local_function_static (decl))
21923 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21925 /* If we are in terse mode, don't generate any DIEs to represent any
21926 variable declarations or definitions. */
21927 if (debug_info_level <= DINFO_LEVEL_TERSE)
21928 return;
21929 break;
21931 case CONST_DECL:
21932 if (debug_info_level <= DINFO_LEVEL_TERSE)
21933 return;
21934 if (!is_fortran () && !is_ada ())
21935 return;
21936 if (TREE_STATIC (decl) && decl_function_context (decl))
21937 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21938 break;
21940 case NAMESPACE_DECL:
21941 case IMPORTED_DECL:
21942 if (debug_info_level <= DINFO_LEVEL_TERSE)
21943 return;
21944 if (lookup_decl_die (decl) != NULL)
21945 return;
21946 break;
21948 case TYPE_DECL:
21949 /* Don't emit stubs for types unless they are needed by other DIEs. */
21950 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21951 return;
21953 /* Don't bother trying to generate any DIEs to represent any of the
21954 normal built-in types for the language we are compiling. */
21955 if (DECL_IS_BUILTIN (decl))
21956 return;
21958 /* If we are in terse mode, don't generate any DIEs for types. */
21959 if (debug_info_level <= DINFO_LEVEL_TERSE)
21960 return;
21962 /* If we're a function-scope tag, initially use a parent of NULL;
21963 this will be fixed up in decls_for_scope. */
21964 if (decl_function_context (decl))
21965 context_die = NULL;
21967 break;
21969 case NAMELIST_DECL:
21970 break;
21972 default:
21973 return;
21976 gen_decl_die (decl, NULL, context_die);
21978 #ifdef ENABLE_CHECKING
21979 dw_die_ref die = lookup_decl_die (decl);
21980 if (die)
21981 check_die (die);
21982 #endif
21985 /* Write the debugging output for DECL. */
21987 static void
21988 dwarf2out_function_decl (tree decl)
21990 dwarf2out_decl (decl);
21991 call_arg_locations = NULL;
21992 call_arg_loc_last = NULL;
21993 call_site_count = -1;
21994 tail_call_site_count = -1;
21995 decl_loc_table->empty ();
21996 cached_dw_loc_list_table->empty ();
21999 /* Output a marker (i.e. a label) for the beginning of the generated code for
22000 a lexical block. */
22002 static void
22003 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22004 unsigned int blocknum)
22006 switch_to_section (current_function_section ());
22007 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22010 /* Output a marker (i.e. a label) for the end of the generated code for a
22011 lexical block. */
22013 static void
22014 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22016 switch_to_section (current_function_section ());
22017 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22020 /* Returns nonzero if it is appropriate not to emit any debugging
22021 information for BLOCK, because it doesn't contain any instructions.
22023 Don't allow this for blocks with nested functions or local classes
22024 as we would end up with orphans, and in the presence of scheduling
22025 we may end up calling them anyway. */
22027 static bool
22028 dwarf2out_ignore_block (const_tree block)
22030 tree decl;
22031 unsigned int i;
22033 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22034 if (TREE_CODE (decl) == FUNCTION_DECL
22035 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22036 return 0;
22037 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22039 decl = BLOCK_NONLOCALIZED_VAR (block, i);
22040 if (TREE_CODE (decl) == FUNCTION_DECL
22041 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22042 return 0;
22045 return 1;
22048 /* Hash table routines for file_hash. */
22050 bool
22051 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22053 return filename_cmp (p1->filename, p2) == 0;
22056 hashval_t
22057 dwarf_file_hasher::hash (dwarf_file_data *p)
22059 return htab_hash_string (p->filename);
22062 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22063 dwarf2out.c) and return its "index". The index of each (known) filename is
22064 just a unique number which is associated with only that one filename. We
22065 need such numbers for the sake of generating labels (in the .debug_sfnames
22066 section) and references to those files numbers (in the .debug_srcinfo
22067 and .debug_macinfo sections). If the filename given as an argument is not
22068 found in our current list, add it to the list and assign it the next
22069 available unique index number. */
22071 static struct dwarf_file_data *
22072 lookup_filename (const char *file_name)
22074 struct dwarf_file_data * created;
22076 if (!file_name)
22077 return NULL;
22079 dwarf_file_data **slot
22080 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22081 INSERT);
22082 if (*slot)
22083 return *slot;
22085 created = ggc_alloc<dwarf_file_data> ();
22086 created->filename = file_name;
22087 created->emitted_number = 0;
22088 *slot = created;
22089 return created;
22092 /* If the assembler will construct the file table, then translate the compiler
22093 internal file table number into the assembler file table number, and emit
22094 a .file directive if we haven't already emitted one yet. The file table
22095 numbers are different because we prune debug info for unused variables and
22096 types, which may include filenames. */
22098 static int
22099 maybe_emit_file (struct dwarf_file_data * fd)
22101 if (! fd->emitted_number)
22103 if (last_emitted_file)
22104 fd->emitted_number = last_emitted_file->emitted_number + 1;
22105 else
22106 fd->emitted_number = 1;
22107 last_emitted_file = fd;
22109 if (DWARF2_ASM_LINE_DEBUG_INFO)
22111 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22112 output_quoted_string (asm_out_file,
22113 remap_debug_filename (fd->filename));
22114 fputc ('\n', asm_out_file);
22118 return fd->emitted_number;
22121 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22122 That generation should happen after function debug info has been
22123 generated. The value of the attribute is the constant value of ARG. */
22125 static void
22126 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22128 die_arg_entry entry;
22130 if (!die || !arg)
22131 return;
22133 gcc_assert (early_dwarf);
22135 if (!tmpl_value_parm_die_table)
22136 vec_alloc (tmpl_value_parm_die_table, 32);
22138 entry.die = die;
22139 entry.arg = arg;
22140 vec_safe_push (tmpl_value_parm_die_table, entry);
22143 /* Return TRUE if T is an instance of generic type, FALSE
22144 otherwise. */
22146 static bool
22147 generic_type_p (tree t)
22149 if (t == NULL_TREE || !TYPE_P (t))
22150 return false;
22151 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22154 /* Schedule the generation of the generic parameter dies for the
22155 instance of generic type T. The proper generation itself is later
22156 done by gen_scheduled_generic_parms_dies. */
22158 static void
22159 schedule_generic_params_dies_gen (tree t)
22161 if (!generic_type_p (t))
22162 return;
22164 gcc_assert (early_dwarf);
22166 if (!generic_type_instances)
22167 vec_alloc (generic_type_instances, 256);
22169 vec_safe_push (generic_type_instances, t);
22172 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22173 by append_entry_to_tmpl_value_parm_die_table. This function must
22174 be called after function DIEs have been generated. */
22176 static void
22177 gen_remaining_tmpl_value_param_die_attribute (void)
22179 if (tmpl_value_parm_die_table)
22181 unsigned i, j;
22182 die_arg_entry *e;
22184 /* We do this in two phases - first get the cases we can
22185 handle during early-finish, preserving those we cannot
22186 (containing symbolic constants where we don't yet know
22187 whether we are going to output the referenced symbols).
22188 For those we try again at late-finish. */
22189 j = 0;
22190 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22192 if (!tree_add_const_value_attribute (e->die, e->arg))
22193 (*tmpl_value_parm_die_table)[j++] = *e;
22195 tmpl_value_parm_die_table->truncate (j);
22199 /* Generate generic parameters DIEs for instances of generic types
22200 that have been previously scheduled by
22201 schedule_generic_params_dies_gen. This function must be called
22202 after all the types of the CU have been laid out. */
22204 static void
22205 gen_scheduled_generic_parms_dies (void)
22207 unsigned i;
22208 tree t;
22210 if (!generic_type_instances)
22211 return;
22213 /* We end up "recursing" into schedule_generic_params_dies_gen, so
22214 pretend this generation is part of "early dwarf" as well. */
22215 set_early_dwarf s;
22217 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22218 if (COMPLETE_TYPE_P (t))
22219 gen_generic_params_dies (t);
22221 generic_type_instances = NULL;
22225 /* Replace DW_AT_name for the decl with name. */
22227 static void
22228 dwarf2out_set_name (tree decl, tree name)
22230 dw_die_ref die;
22231 dw_attr_node *attr;
22232 const char *dname;
22234 die = TYPE_SYMTAB_DIE (decl);
22235 if (!die)
22236 return;
22238 dname = dwarf2_name (name, 0);
22239 if (!dname)
22240 return;
22242 attr = get_AT (die, DW_AT_name);
22243 if (attr)
22245 struct indirect_string_node *node;
22247 node = find_AT_string (dname);
22248 /* replace the string. */
22249 attr->dw_attr_val.v.val_str = node;
22252 else
22253 add_name_attribute (die, dname);
22256 /* True if before or during processing of the first function being emitted. */
22257 static bool in_first_function_p = true;
22258 /* True if loc_note during dwarf2out_var_location call might still be
22259 before first real instruction at address equal to .Ltext0. */
22260 static bool maybe_at_text_label_p = true;
22261 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22262 static unsigned int first_loclabel_num_not_at_text_label;
22264 /* Called by the final INSN scan whenever we see a var location. We
22265 use it to drop labels in the right places, and throw the location in
22266 our lookup table. */
22268 static void
22269 dwarf2out_var_location (rtx_insn *loc_note)
22271 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22272 struct var_loc_node *newloc;
22273 rtx_insn *next_real, *next_note;
22274 static const char *last_label;
22275 static const char *last_postcall_label;
22276 static bool last_in_cold_section_p;
22277 static rtx_insn *expected_next_loc_note;
22278 tree decl;
22279 bool var_loc_p;
22281 if (!NOTE_P (loc_note))
22283 if (CALL_P (loc_note))
22285 call_site_count++;
22286 if (SIBLING_CALL_P (loc_note))
22287 tail_call_site_count++;
22289 return;
22292 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22293 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22294 return;
22296 /* Optimize processing a large consecutive sequence of location
22297 notes so we don't spend too much time in next_real_insn. If the
22298 next insn is another location note, remember the next_real_insn
22299 calculation for next time. */
22300 next_real = cached_next_real_insn;
22301 if (next_real)
22303 if (expected_next_loc_note != loc_note)
22304 next_real = NULL;
22307 next_note = NEXT_INSN (loc_note);
22308 if (! next_note
22309 || next_note->deleted ()
22310 || ! NOTE_P (next_note)
22311 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22312 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22313 next_note = NULL;
22315 if (! next_real)
22316 next_real = next_real_insn (loc_note);
22318 if (next_note)
22320 expected_next_loc_note = next_note;
22321 cached_next_real_insn = next_real;
22323 else
22324 cached_next_real_insn = NULL;
22326 /* If there are no instructions which would be affected by this note,
22327 don't do anything. */
22328 if (var_loc_p
22329 && next_real == NULL_RTX
22330 && !NOTE_DURING_CALL_P (loc_note))
22331 return;
22333 if (next_real == NULL_RTX)
22334 next_real = get_last_insn ();
22336 /* If there were any real insns between note we processed last time
22337 and this note (or if it is the first note), clear
22338 last_{,postcall_}label so that they are not reused this time. */
22339 if (last_var_location_insn == NULL_RTX
22340 || last_var_location_insn != next_real
22341 || last_in_cold_section_p != in_cold_section_p)
22343 last_label = NULL;
22344 last_postcall_label = NULL;
22347 if (var_loc_p)
22349 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22350 newloc = add_var_loc_to_decl (decl, loc_note,
22351 NOTE_DURING_CALL_P (loc_note)
22352 ? last_postcall_label : last_label);
22353 if (newloc == NULL)
22354 return;
22356 else
22358 decl = NULL_TREE;
22359 newloc = NULL;
22362 /* If there were no real insns between note we processed last time
22363 and this note, use the label we emitted last time. Otherwise
22364 create a new label and emit it. */
22365 if (last_label == NULL)
22367 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22368 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22369 loclabel_num++;
22370 last_label = ggc_strdup (loclabel);
22371 /* See if loclabel might be equal to .Ltext0. If yes,
22372 bump first_loclabel_num_not_at_text_label. */
22373 if (!have_multiple_function_sections
22374 && in_first_function_p
22375 && maybe_at_text_label_p)
22377 static rtx_insn *last_start;
22378 rtx_insn *insn;
22379 for (insn = loc_note; insn; insn = previous_insn (insn))
22380 if (insn == last_start)
22381 break;
22382 else if (!NONDEBUG_INSN_P (insn))
22383 continue;
22384 else
22386 rtx body = PATTERN (insn);
22387 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22388 continue;
22389 /* Inline asm could occupy zero bytes. */
22390 else if (GET_CODE (body) == ASM_INPUT
22391 || asm_noperands (body) >= 0)
22392 continue;
22393 #ifdef HAVE_attr_length
22394 else if (get_attr_min_length (insn) == 0)
22395 continue;
22396 #endif
22397 else
22399 /* Assume insn has non-zero length. */
22400 maybe_at_text_label_p = false;
22401 break;
22404 if (maybe_at_text_label_p)
22406 last_start = loc_note;
22407 first_loclabel_num_not_at_text_label = loclabel_num;
22412 if (!var_loc_p)
22414 struct call_arg_loc_node *ca_loc
22415 = ggc_cleared_alloc<call_arg_loc_node> ();
22416 rtx_insn *prev = prev_real_insn (loc_note);
22417 rtx x;
22418 ca_loc->call_arg_loc_note = loc_note;
22419 ca_loc->next = NULL;
22420 ca_loc->label = last_label;
22421 gcc_assert (prev
22422 && (CALL_P (prev)
22423 || (NONJUMP_INSN_P (prev)
22424 && GET_CODE (PATTERN (prev)) == SEQUENCE
22425 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22426 if (!CALL_P (prev))
22427 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22428 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22429 x = get_call_rtx_from (PATTERN (prev));
22430 if (x)
22432 x = XEXP (XEXP (x, 0), 0);
22433 if (GET_CODE (x) == SYMBOL_REF
22434 && SYMBOL_REF_DECL (x)
22435 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22436 ca_loc->symbol_ref = x;
22438 ca_loc->block = insn_scope (prev);
22439 if (call_arg_locations)
22440 call_arg_loc_last->next = ca_loc;
22441 else
22442 call_arg_locations = ca_loc;
22443 call_arg_loc_last = ca_loc;
22445 else if (!NOTE_DURING_CALL_P (loc_note))
22446 newloc->label = last_label;
22447 else
22449 if (!last_postcall_label)
22451 sprintf (loclabel, "%s-1", last_label);
22452 last_postcall_label = ggc_strdup (loclabel);
22454 newloc->label = last_postcall_label;
22457 last_var_location_insn = next_real;
22458 last_in_cold_section_p = in_cold_section_p;
22461 /* Note in one location list that text section has changed. */
22464 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22466 var_loc_list *list = *slot;
22467 if (list->first)
22468 list->last_before_switch
22469 = list->last->next ? list->last->next : list->last;
22470 return 1;
22473 /* Note in all location lists that text section has changed. */
22475 static void
22476 var_location_switch_text_section (void)
22478 if (decl_loc_table == NULL)
22479 return;
22481 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22484 /* Create a new line number table. */
22486 static dw_line_info_table *
22487 new_line_info_table (void)
22489 dw_line_info_table *table;
22491 table = ggc_cleared_alloc<dw_line_info_table> ();
22492 table->file_num = 1;
22493 table->line_num = 1;
22494 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22496 return table;
22499 /* Lookup the "current" table into which we emit line info, so
22500 that we don't have to do it for every source line. */
22502 static void
22503 set_cur_line_info_table (section *sec)
22505 dw_line_info_table *table;
22507 if (sec == text_section)
22508 table = text_section_line_info;
22509 else if (sec == cold_text_section)
22511 table = cold_text_section_line_info;
22512 if (!table)
22514 cold_text_section_line_info = table = new_line_info_table ();
22515 table->end_label = cold_end_label;
22518 else
22520 const char *end_label;
22522 if (flag_reorder_blocks_and_partition)
22524 if (in_cold_section_p)
22525 end_label = crtl->subsections.cold_section_end_label;
22526 else
22527 end_label = crtl->subsections.hot_section_end_label;
22529 else
22531 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22532 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22533 current_function_funcdef_no);
22534 end_label = ggc_strdup (label);
22537 table = new_line_info_table ();
22538 table->end_label = end_label;
22540 vec_safe_push (separate_line_info, table);
22543 if (DWARF2_ASM_LINE_DEBUG_INFO)
22544 table->is_stmt = (cur_line_info_table
22545 ? cur_line_info_table->is_stmt
22546 : DWARF_LINE_DEFAULT_IS_STMT_START);
22547 cur_line_info_table = table;
22551 /* We need to reset the locations at the beginning of each
22552 function. We can't do this in the end_function hook, because the
22553 declarations that use the locations won't have been output when
22554 that hook is called. Also compute have_multiple_function_sections here. */
22556 static void
22557 dwarf2out_begin_function (tree fun)
22559 section *sec = function_section (fun);
22561 if (sec != text_section)
22562 have_multiple_function_sections = true;
22564 if (flag_reorder_blocks_and_partition && !cold_text_section)
22566 gcc_assert (current_function_decl == fun);
22567 cold_text_section = unlikely_text_section ();
22568 switch_to_section (cold_text_section);
22569 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22570 switch_to_section (sec);
22573 dwarf2out_note_section_used ();
22574 call_site_count = 0;
22575 tail_call_site_count = 0;
22577 set_cur_line_info_table (sec);
22580 /* Helper function of dwarf2out_end_function, called only after emitting
22581 the very first function into assembly. Check if some .debug_loc range
22582 might end with a .LVL* label that could be equal to .Ltext0.
22583 In that case we must force using absolute addresses in .debug_loc ranges,
22584 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22585 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22586 list terminator.
22587 Set have_multiple_function_sections to true in that case and
22588 terminate htab traversal. */
22591 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22593 var_loc_list *entry = *slot;
22594 struct var_loc_node *node;
22596 node = entry->first;
22597 if (node && node->next && node->next->label)
22599 unsigned int i;
22600 const char *label = node->next->label;
22601 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22603 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22605 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22606 if (strcmp (label, loclabel) == 0)
22608 have_multiple_function_sections = true;
22609 return 0;
22613 return 1;
22616 /* Hook called after emitting a function into assembly.
22617 This does something only for the very first function emitted. */
22619 static void
22620 dwarf2out_end_function (unsigned int)
22622 if (in_first_function_p
22623 && !have_multiple_function_sections
22624 && first_loclabel_num_not_at_text_label
22625 && decl_loc_table)
22626 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22627 in_first_function_p = false;
22628 maybe_at_text_label_p = false;
22631 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22632 front-ends register a translation unit even before dwarf2out_init is
22633 called. */
22634 static tree main_translation_unit = NULL_TREE;
22636 /* Hook called by front-ends after they built their main translation unit.
22637 Associate comp_unit_die to UNIT. */
22639 static void
22640 dwarf2out_register_main_translation_unit (tree unit)
22642 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22643 && main_translation_unit == NULL_TREE);
22644 main_translation_unit = unit;
22645 /* If dwarf2out_init has not been called yet, it will perform the association
22646 itself looking at main_translation_unit. */
22647 if (decl_die_table != NULL)
22648 equate_decl_number_to_die (unit, comp_unit_die ());
22651 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22653 static void
22654 push_dw_line_info_entry (dw_line_info_table *table,
22655 enum dw_line_info_opcode opcode, unsigned int val)
22657 dw_line_info_entry e;
22658 e.opcode = opcode;
22659 e.val = val;
22660 vec_safe_push (table->entries, e);
22663 /* Output a label to mark the beginning of a source code line entry
22664 and record information relating to this source line, in
22665 'line_info_table' for later output of the .debug_line section. */
22666 /* ??? The discriminator parameter ought to be unsigned. */
22668 static void
22669 dwarf2out_source_line (unsigned int line, const char *filename,
22670 int discriminator, bool is_stmt)
22672 unsigned int file_num;
22673 dw_line_info_table *table;
22675 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22676 return;
22678 /* The discriminator column was added in dwarf4. Simplify the below
22679 by simply removing it if we're not supposed to output it. */
22680 if (dwarf_version < 4 && dwarf_strict)
22681 discriminator = 0;
22683 table = cur_line_info_table;
22684 file_num = maybe_emit_file (lookup_filename (filename));
22686 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22687 the debugger has used the second (possibly duplicate) line number
22688 at the beginning of the function to mark the end of the prologue.
22689 We could eliminate any other duplicates within the function. For
22690 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22691 that second line number entry. */
22692 /* Recall that this end-of-prologue indication is *not* the same thing
22693 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22694 to which the hook corresponds, follows the last insn that was
22695 emitted by gen_prologue. What we need is to precede the first insn
22696 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22697 insn that corresponds to something the user wrote. These may be
22698 very different locations once scheduling is enabled. */
22700 if (0 && file_num == table->file_num
22701 && line == table->line_num
22702 && discriminator == table->discrim_num
22703 && is_stmt == table->is_stmt)
22704 return;
22706 switch_to_section (current_function_section ());
22708 /* If requested, emit something human-readable. */
22709 if (flag_debug_asm)
22710 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22712 if (DWARF2_ASM_LINE_DEBUG_INFO)
22714 /* Emit the .loc directive understood by GNU as. */
22715 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22716 file_num, line, is_stmt, discriminator */
22717 fputs ("\t.loc ", asm_out_file);
22718 fprint_ul (asm_out_file, file_num);
22719 putc (' ', asm_out_file);
22720 fprint_ul (asm_out_file, line);
22721 putc (' ', asm_out_file);
22722 putc ('0', asm_out_file);
22724 if (is_stmt != table->is_stmt)
22726 fputs (" is_stmt ", asm_out_file);
22727 putc (is_stmt ? '1' : '0', asm_out_file);
22729 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22731 gcc_assert (discriminator > 0);
22732 fputs (" discriminator ", asm_out_file);
22733 fprint_ul (asm_out_file, (unsigned long) discriminator);
22735 putc ('\n', asm_out_file);
22737 else
22739 unsigned int label_num = ++line_info_label_num;
22741 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22743 push_dw_line_info_entry (table, LI_set_address, label_num);
22744 if (file_num != table->file_num)
22745 push_dw_line_info_entry (table, LI_set_file, file_num);
22746 if (discriminator != table->discrim_num)
22747 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22748 if (is_stmt != table->is_stmt)
22749 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22750 push_dw_line_info_entry (table, LI_set_line, line);
22753 table->file_num = file_num;
22754 table->line_num = line;
22755 table->discrim_num = discriminator;
22756 table->is_stmt = is_stmt;
22757 table->in_use = true;
22760 /* Record the beginning of a new source file. */
22762 static void
22763 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22765 if (flag_eliminate_dwarf2_dups)
22767 /* Record the beginning of the file for break_out_includes. */
22768 dw_die_ref bincl_die;
22770 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22771 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22774 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22776 macinfo_entry e;
22777 e.code = DW_MACINFO_start_file;
22778 e.lineno = lineno;
22779 e.info = ggc_strdup (filename);
22780 vec_safe_push (macinfo_table, e);
22784 /* Record the end of a source file. */
22786 static void
22787 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22789 if (flag_eliminate_dwarf2_dups)
22790 /* Record the end of the file for break_out_includes. */
22791 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22793 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22795 macinfo_entry e;
22796 e.code = DW_MACINFO_end_file;
22797 e.lineno = lineno;
22798 e.info = NULL;
22799 vec_safe_push (macinfo_table, e);
22803 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22804 the tail part of the directive line, i.e. the part which is past the
22805 initial whitespace, #, whitespace, directive-name, whitespace part. */
22807 static void
22808 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22809 const char *buffer ATTRIBUTE_UNUSED)
22811 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22813 macinfo_entry e;
22814 /* Insert a dummy first entry to be able to optimize the whole
22815 predefined macro block using DW_MACRO_GNU_transparent_include. */
22816 if (macinfo_table->is_empty () && lineno <= 1)
22818 e.code = 0;
22819 e.lineno = 0;
22820 e.info = NULL;
22821 vec_safe_push (macinfo_table, e);
22823 e.code = DW_MACINFO_define;
22824 e.lineno = lineno;
22825 e.info = ggc_strdup (buffer);
22826 vec_safe_push (macinfo_table, e);
22830 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22831 the tail part of the directive line, i.e. the part which is past the
22832 initial whitespace, #, whitespace, directive-name, whitespace part. */
22834 static void
22835 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22836 const char *buffer ATTRIBUTE_UNUSED)
22838 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22840 macinfo_entry e;
22841 /* Insert a dummy first entry to be able to optimize the whole
22842 predefined macro block using DW_MACRO_GNU_transparent_include. */
22843 if (macinfo_table->is_empty () && lineno <= 1)
22845 e.code = 0;
22846 e.lineno = 0;
22847 e.info = NULL;
22848 vec_safe_push (macinfo_table, e);
22850 e.code = DW_MACINFO_undef;
22851 e.lineno = lineno;
22852 e.info = ggc_strdup (buffer);
22853 vec_safe_push (macinfo_table, e);
22857 /* Helpers to manipulate hash table of CUs. */
22859 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22861 static inline hashval_t hash (const macinfo_entry *);
22862 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22865 inline hashval_t
22866 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22868 return htab_hash_string (entry->info);
22871 inline bool
22872 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22873 const macinfo_entry *entry2)
22875 return !strcmp (entry1->info, entry2->info);
22878 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22880 /* Output a single .debug_macinfo entry. */
22882 static void
22883 output_macinfo_op (macinfo_entry *ref)
22885 int file_num;
22886 size_t len;
22887 struct indirect_string_node *node;
22888 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22889 struct dwarf_file_data *fd;
22891 switch (ref->code)
22893 case DW_MACINFO_start_file:
22894 fd = lookup_filename (ref->info);
22895 file_num = maybe_emit_file (fd);
22896 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22897 dw2_asm_output_data_uleb128 (ref->lineno,
22898 "Included from line number %lu",
22899 (unsigned long) ref->lineno);
22900 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22901 break;
22902 case DW_MACINFO_end_file:
22903 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22904 break;
22905 case DW_MACINFO_define:
22906 case DW_MACINFO_undef:
22907 len = strlen (ref->info) + 1;
22908 if (!dwarf_strict
22909 && len > DWARF_OFFSET_SIZE
22910 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22911 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22913 ref->code = ref->code == DW_MACINFO_define
22914 ? DW_MACRO_GNU_define_indirect
22915 : DW_MACRO_GNU_undef_indirect;
22916 output_macinfo_op (ref);
22917 return;
22919 dw2_asm_output_data (1, ref->code,
22920 ref->code == DW_MACINFO_define
22921 ? "Define macro" : "Undefine macro");
22922 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22923 (unsigned long) ref->lineno);
22924 dw2_asm_output_nstring (ref->info, -1, "The macro");
22925 break;
22926 case DW_MACRO_GNU_define_indirect:
22927 case DW_MACRO_GNU_undef_indirect:
22928 node = find_AT_string (ref->info);
22929 gcc_assert (node
22930 && ((node->form == DW_FORM_strp)
22931 || (node->form == DW_FORM_GNU_str_index)));
22932 dw2_asm_output_data (1, ref->code,
22933 ref->code == DW_MACRO_GNU_define_indirect
22934 ? "Define macro indirect"
22935 : "Undefine macro indirect");
22936 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22937 (unsigned long) ref->lineno);
22938 if (node->form == DW_FORM_strp)
22939 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22940 debug_str_section, "The macro: \"%s\"",
22941 ref->info);
22942 else
22943 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22944 ref->info);
22945 break;
22946 case DW_MACRO_GNU_transparent_include:
22947 dw2_asm_output_data (1, ref->code, "Transparent include");
22948 ASM_GENERATE_INTERNAL_LABEL (label,
22949 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22950 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22951 break;
22952 default:
22953 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22954 ASM_COMMENT_START, (unsigned long) ref->code);
22955 break;
22959 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22960 other compilation unit .debug_macinfo sections. IDX is the first
22961 index of a define/undef, return the number of ops that should be
22962 emitted in a comdat .debug_macinfo section and emit
22963 a DW_MACRO_GNU_transparent_include entry referencing it.
22964 If the define/undef entry should be emitted normally, return 0. */
22966 static unsigned
22967 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22968 macinfo_hash_type **macinfo_htab)
22970 macinfo_entry *first, *second, *cur, *inc;
22971 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22972 unsigned char checksum[16];
22973 struct md5_ctx ctx;
22974 char *grp_name, *tail;
22975 const char *base;
22976 unsigned int i, count, encoded_filename_len, linebuf_len;
22977 macinfo_entry **slot;
22979 first = &(*macinfo_table)[idx];
22980 second = &(*macinfo_table)[idx + 1];
22982 /* Optimize only if there are at least two consecutive define/undef ops,
22983 and either all of them are before first DW_MACINFO_start_file
22984 with lineno {0,1} (i.e. predefined macro block), or all of them are
22985 in some included header file. */
22986 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22987 return 0;
22988 if (vec_safe_is_empty (files))
22990 if (first->lineno > 1 || second->lineno > 1)
22991 return 0;
22993 else if (first->lineno == 0)
22994 return 0;
22996 /* Find the last define/undef entry that can be grouped together
22997 with first and at the same time compute md5 checksum of their
22998 codes, linenumbers and strings. */
22999 md5_init_ctx (&ctx);
23000 for (i = idx; macinfo_table->iterate (i, &cur); i++)
23001 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
23002 break;
23003 else if (vec_safe_is_empty (files) && cur->lineno > 1)
23004 break;
23005 else
23007 unsigned char code = cur->code;
23008 md5_process_bytes (&code, 1, &ctx);
23009 checksum_uleb128 (cur->lineno, &ctx);
23010 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
23012 md5_finish_ctx (&ctx, checksum);
23013 count = i - idx;
23015 /* From the containing include filename (if any) pick up just
23016 usable characters from its basename. */
23017 if (vec_safe_is_empty (files))
23018 base = "";
23019 else
23020 base = lbasename (files->last ().info);
23021 for (encoded_filename_len = 0, i = 0; base[i]; i++)
23022 if (ISIDNUM (base[i]) || base[i] == '.')
23023 encoded_filename_len++;
23024 /* Count . at the end. */
23025 if (encoded_filename_len)
23026 encoded_filename_len++;
23028 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
23029 linebuf_len = strlen (linebuf);
23031 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
23032 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
23033 + 16 * 2 + 1);
23034 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
23035 tail = grp_name + 4;
23036 if (encoded_filename_len)
23038 for (i = 0; base[i]; i++)
23039 if (ISIDNUM (base[i]) || base[i] == '.')
23040 *tail++ = base[i];
23041 *tail++ = '.';
23043 memcpy (tail, linebuf, linebuf_len);
23044 tail += linebuf_len;
23045 *tail++ = '.';
23046 for (i = 0; i < 16; i++)
23047 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23049 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23050 in the empty vector entry before the first define/undef. */
23051 inc = &(*macinfo_table)[idx - 1];
23052 inc->code = DW_MACRO_GNU_transparent_include;
23053 inc->lineno = 0;
23054 inc->info = ggc_strdup (grp_name);
23055 if (!*macinfo_htab)
23056 *macinfo_htab = new macinfo_hash_type (10);
23057 /* Avoid emitting duplicates. */
23058 slot = (*macinfo_htab)->find_slot (inc, INSERT);
23059 if (*slot != NULL)
23061 inc->code = 0;
23062 inc->info = NULL;
23063 /* If such an entry has been used before, just emit
23064 a DW_MACRO_GNU_transparent_include op. */
23065 inc = *slot;
23066 output_macinfo_op (inc);
23067 /* And clear all macinfo_entry in the range to avoid emitting them
23068 in the second pass. */
23069 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23071 cur->code = 0;
23072 cur->info = NULL;
23075 else
23077 *slot = inc;
23078 inc->lineno = (*macinfo_htab)->elements ();
23079 output_macinfo_op (inc);
23081 return count;
23084 /* Save any strings needed by the macinfo table in the debug str
23085 table. All strings must be collected into the table by the time
23086 index_string is called. */
23088 static void
23089 save_macinfo_strings (void)
23091 unsigned len;
23092 unsigned i;
23093 macinfo_entry *ref;
23095 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23097 switch (ref->code)
23099 /* Match the logic in output_macinfo_op to decide on
23100 indirect strings. */
23101 case DW_MACINFO_define:
23102 case DW_MACINFO_undef:
23103 len = strlen (ref->info) + 1;
23104 if (!dwarf_strict
23105 && len > DWARF_OFFSET_SIZE
23106 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23107 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23108 set_indirect_string (find_AT_string (ref->info));
23109 break;
23110 case DW_MACRO_GNU_define_indirect:
23111 case DW_MACRO_GNU_undef_indirect:
23112 set_indirect_string (find_AT_string (ref->info));
23113 break;
23114 default:
23115 break;
23120 /* Output macinfo section(s). */
23122 static void
23123 output_macinfo (void)
23125 unsigned i;
23126 unsigned long length = vec_safe_length (macinfo_table);
23127 macinfo_entry *ref;
23128 vec<macinfo_entry, va_gc> *files = NULL;
23129 macinfo_hash_type *macinfo_htab = NULL;
23131 if (! length)
23132 return;
23134 /* output_macinfo* uses these interchangeably. */
23135 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23136 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23137 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23138 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23140 /* For .debug_macro emit the section header. */
23141 if (!dwarf_strict)
23143 dw2_asm_output_data (2, 4, "DWARF macro version number");
23144 if (DWARF_OFFSET_SIZE == 8)
23145 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23146 else
23147 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23148 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23149 (!dwarf_split_debug_info ? debug_line_section_label
23150 : debug_skeleton_line_section_label),
23151 debug_line_section, NULL);
23154 /* In the first loop, it emits the primary .debug_macinfo section
23155 and after each emitted op the macinfo_entry is cleared.
23156 If a longer range of define/undef ops can be optimized using
23157 DW_MACRO_GNU_transparent_include, the
23158 DW_MACRO_GNU_transparent_include op is emitted and kept in
23159 the vector before the first define/undef in the range and the
23160 whole range of define/undef ops is not emitted and kept. */
23161 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23163 switch (ref->code)
23165 case DW_MACINFO_start_file:
23166 vec_safe_push (files, *ref);
23167 break;
23168 case DW_MACINFO_end_file:
23169 if (!vec_safe_is_empty (files))
23170 files->pop ();
23171 break;
23172 case DW_MACINFO_define:
23173 case DW_MACINFO_undef:
23174 if (!dwarf_strict
23175 && HAVE_COMDAT_GROUP
23176 && vec_safe_length (files) != 1
23177 && i > 0
23178 && i + 1 < length
23179 && (*macinfo_table)[i - 1].code == 0)
23181 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23182 if (count)
23184 i += count - 1;
23185 continue;
23188 break;
23189 case 0:
23190 /* A dummy entry may be inserted at the beginning to be able
23191 to optimize the whole block of predefined macros. */
23192 if (i == 0)
23193 continue;
23194 default:
23195 break;
23197 output_macinfo_op (ref);
23198 ref->info = NULL;
23199 ref->code = 0;
23202 if (!macinfo_htab)
23203 return;
23205 delete macinfo_htab;
23206 macinfo_htab = NULL;
23208 /* If any DW_MACRO_GNU_transparent_include were used, on those
23209 DW_MACRO_GNU_transparent_include entries terminate the
23210 current chain and switch to a new comdat .debug_macinfo
23211 section and emit the define/undef entries within it. */
23212 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23213 switch (ref->code)
23215 case 0:
23216 continue;
23217 case DW_MACRO_GNU_transparent_include:
23219 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23220 tree comdat_key = get_identifier (ref->info);
23221 /* Terminate the previous .debug_macinfo section. */
23222 dw2_asm_output_data (1, 0, "End compilation unit");
23223 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23224 SECTION_DEBUG
23225 | SECTION_LINKONCE,
23226 comdat_key);
23227 ASM_GENERATE_INTERNAL_LABEL (label,
23228 DEBUG_MACRO_SECTION_LABEL,
23229 ref->lineno);
23230 ASM_OUTPUT_LABEL (asm_out_file, label);
23231 ref->code = 0;
23232 ref->info = NULL;
23233 dw2_asm_output_data (2, 4, "DWARF macro version number");
23234 if (DWARF_OFFSET_SIZE == 8)
23235 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23236 else
23237 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23239 break;
23240 case DW_MACINFO_define:
23241 case DW_MACINFO_undef:
23242 output_macinfo_op (ref);
23243 ref->code = 0;
23244 ref->info = NULL;
23245 break;
23246 default:
23247 gcc_unreachable ();
23251 /* Set up for Dwarf output at the start of compilation. */
23253 static void
23254 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23256 /* This option is currently broken, see (PR53118 and PR46102). */
23257 if (flag_eliminate_dwarf2_dups
23258 && strstr (lang_hooks.name, "C++"))
23260 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23261 flag_eliminate_dwarf2_dups = 0;
23264 /* Allocate the file_table. */
23265 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23267 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23268 /* Allocate the decl_die_table. */
23269 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23271 /* Allocate the decl_loc_table. */
23272 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23274 /* Allocate the cached_dw_loc_list_table. */
23275 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23277 /* Allocate the initial hunk of the decl_scope_table. */
23278 vec_alloc (decl_scope_table, 256);
23280 /* Allocate the initial hunk of the abbrev_die_table. */
23281 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23282 (ABBREV_DIE_TABLE_INCREMENT);
23283 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23284 /* Zero-th entry is allocated, but unused. */
23285 abbrev_die_table_in_use = 1;
23287 /* Allocate the pubtypes and pubnames vectors. */
23288 vec_alloc (pubname_table, 32);
23289 vec_alloc (pubtype_table, 32);
23291 vec_alloc (incomplete_types, 64);
23293 vec_alloc (used_rtx_array, 32);
23295 if (!dwarf_split_debug_info)
23297 debug_info_section = get_section (DEBUG_INFO_SECTION,
23298 SECTION_DEBUG, NULL);
23299 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23300 SECTION_DEBUG, NULL);
23301 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23302 SECTION_DEBUG, NULL);
23304 else
23306 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23307 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23308 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23309 SECTION_DEBUG | SECTION_EXCLUDE,
23310 NULL);
23311 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23312 SECTION_DEBUG, NULL);
23313 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23314 SECTION_DEBUG, NULL);
23315 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23316 SECTION_DEBUG, NULL);
23317 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23318 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23320 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23321 the main .o, but the skeleton_line goes into the split off dwo. */
23322 debug_skeleton_line_section
23323 = get_section (DEBUG_DWO_LINE_SECTION,
23324 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23325 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23326 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23327 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23328 SECTION_DEBUG | SECTION_EXCLUDE,
23329 NULL);
23330 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23331 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23332 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23333 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23334 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23335 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23337 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23338 SECTION_DEBUG, NULL);
23339 debug_macinfo_section = get_section (dwarf_strict
23340 ? DEBUG_MACINFO_SECTION
23341 : DEBUG_MACRO_SECTION,
23342 DEBUG_MACRO_SECTION_FLAGS, NULL);
23343 debug_line_section = get_section (DEBUG_LINE_SECTION,
23344 SECTION_DEBUG, NULL);
23345 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23346 SECTION_DEBUG, NULL);
23347 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23348 SECTION_DEBUG, NULL);
23349 debug_str_section = get_section (DEBUG_STR_SECTION,
23350 DEBUG_STR_SECTION_FLAGS, NULL);
23351 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23352 SECTION_DEBUG, NULL);
23353 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23354 SECTION_DEBUG, NULL);
23356 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23357 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23358 DEBUG_ABBREV_SECTION_LABEL, 0);
23359 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23360 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23361 COLD_TEXT_SECTION_LABEL, 0);
23362 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23364 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23365 DEBUG_INFO_SECTION_LABEL, 0);
23366 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23367 DEBUG_LINE_SECTION_LABEL, 0);
23368 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23369 DEBUG_RANGES_SECTION_LABEL, 0);
23370 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23371 DEBUG_ADDR_SECTION_LABEL, 0);
23372 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23373 dwarf_strict
23374 ? DEBUG_MACINFO_SECTION_LABEL
23375 : DEBUG_MACRO_SECTION_LABEL, 0);
23376 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23378 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23379 vec_alloc (macinfo_table, 64);
23381 switch_to_section (text_section);
23382 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23383 #endif
23385 /* Make sure the line number table for .text always exists. */
23386 text_section_line_info = new_line_info_table ();
23387 text_section_line_info->end_label = text_end_label;
23389 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23390 cur_line_info_table = text_section_line_info;
23391 #endif
23393 /* If front-ends already registered a main translation unit but we were not
23394 ready to perform the association, do this now. */
23395 if (main_translation_unit != NULL_TREE)
23396 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23399 /* Called before compile () starts outputtting functions, variables
23400 and toplevel asms into assembly. */
23402 static void
23403 dwarf2out_assembly_start (void)
23405 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23406 && dwarf2out_do_cfi_asm ()
23407 && (!(flag_unwind_tables || flag_exceptions)
23408 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23409 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23412 /* A helper function for dwarf2out_finish called through
23413 htab_traverse. Assign a string its index. All strings must be
23414 collected into the table by the time index_string is called,
23415 because the indexing code relies on htab_traverse to traverse nodes
23416 in the same order for each run. */
23419 index_string (indirect_string_node **h, unsigned int *index)
23421 indirect_string_node *node = *h;
23423 find_string_form (node);
23424 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23426 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23427 node->index = *index;
23428 *index += 1;
23430 return 1;
23433 /* A helper function for output_indirect_strings called through
23434 htab_traverse. Output the offset to a string and update the
23435 current offset. */
23438 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23440 indirect_string_node *node = *h;
23442 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23444 /* Assert that this node has been assigned an index. */
23445 gcc_assert (node->index != NO_INDEX_ASSIGNED
23446 && node->index != NOT_INDEXED);
23447 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23448 "indexed string 0x%x: %s", node->index, node->str);
23449 *offset += strlen (node->str) + 1;
23451 return 1;
23454 /* A helper function for dwarf2out_finish called through
23455 htab_traverse. Output the indexed string. */
23458 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23460 struct indirect_string_node *node = *h;
23462 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23464 /* Assert that the strings are output in the same order as their
23465 indexes were assigned. */
23466 gcc_assert (*cur_idx == node->index);
23467 assemble_string (node->str, strlen (node->str) + 1);
23468 *cur_idx += 1;
23470 return 1;
23473 /* A helper function for dwarf2out_finish called through
23474 htab_traverse. Emit one queued .debug_str string. */
23477 output_indirect_string (indirect_string_node **h, void *)
23479 struct indirect_string_node *node = *h;
23481 node->form = find_string_form (node);
23482 if (node->form == DW_FORM_strp && node->refcount > 0)
23484 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23485 assemble_string (node->str, strlen (node->str) + 1);
23488 return 1;
23491 /* Output the indexed string table. */
23493 static void
23494 output_indirect_strings (void)
23496 switch_to_section (debug_str_section);
23497 if (!dwarf_split_debug_info)
23498 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23499 else
23501 unsigned int offset = 0;
23502 unsigned int cur_idx = 0;
23504 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23506 switch_to_section (debug_str_offsets_section);
23507 debug_str_hash->traverse_noresize
23508 <unsigned int *, output_index_string_offset> (&offset);
23509 switch_to_section (debug_str_dwo_section);
23510 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23511 (&cur_idx);
23515 /* Callback for htab_traverse to assign an index to an entry in the
23516 table, and to write that entry to the .debug_addr section. */
23519 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23521 addr_table_entry *entry = *slot;
23523 if (entry->refcount == 0)
23525 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23526 || entry->index == NOT_INDEXED);
23527 return 1;
23530 gcc_assert (entry->index == *cur_index);
23531 (*cur_index)++;
23533 switch (entry->kind)
23535 case ate_kind_rtx:
23536 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23537 "0x%x", entry->index);
23538 break;
23539 case ate_kind_rtx_dtprel:
23540 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23541 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23542 DWARF2_ADDR_SIZE,
23543 entry->addr.rtl);
23544 fputc ('\n', asm_out_file);
23545 break;
23546 case ate_kind_label:
23547 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23548 "0x%x", entry->index);
23549 break;
23550 default:
23551 gcc_unreachable ();
23553 return 1;
23556 /* Produce the .debug_addr section. */
23558 static void
23559 output_addr_table (void)
23561 unsigned int index = 0;
23562 if (addr_index_table == NULL || addr_index_table->size () == 0)
23563 return;
23565 switch_to_section (debug_addr_section);
23566 addr_index_table
23567 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23570 #if ENABLE_ASSERT_CHECKING
23571 /* Verify that all marks are clear. */
23573 static void
23574 verify_marks_clear (dw_die_ref die)
23576 dw_die_ref c;
23578 gcc_assert (! die->die_mark);
23579 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23581 #endif /* ENABLE_ASSERT_CHECKING */
23583 /* Clear the marks for a die and its children.
23584 Be cool if the mark isn't set. */
23586 static void
23587 prune_unmark_dies (dw_die_ref die)
23589 dw_die_ref c;
23591 if (die->die_mark)
23592 die->die_mark = 0;
23593 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23596 /* Given DIE that we're marking as used, find any other dies
23597 it references as attributes and mark them as used. */
23599 static void
23600 prune_unused_types_walk_attribs (dw_die_ref die)
23602 dw_attr_node *a;
23603 unsigned ix;
23605 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23607 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23609 /* A reference to another DIE.
23610 Make sure that it will get emitted.
23611 If it was broken out into a comdat group, don't follow it. */
23612 if (! AT_ref (a)->comdat_type_p
23613 || a->dw_attr == DW_AT_specification)
23614 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23616 /* Set the string's refcount to 0 so that prune_unused_types_mark
23617 accounts properly for it. */
23618 if (AT_class (a) == dw_val_class_str)
23619 a->dw_attr_val.v.val_str->refcount = 0;
23623 /* Mark the generic parameters and arguments children DIEs of DIE. */
23625 static void
23626 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23628 dw_die_ref c;
23630 if (die == NULL || die->die_child == NULL)
23631 return;
23632 c = die->die_child;
23635 if (is_template_parameter (c))
23636 prune_unused_types_mark (c, 1);
23637 c = c->die_sib;
23638 } while (c && c != die->die_child);
23641 /* Mark DIE as being used. If DOKIDS is true, then walk down
23642 to DIE's children. */
23644 static void
23645 prune_unused_types_mark (dw_die_ref die, int dokids)
23647 dw_die_ref c;
23649 if (die->die_mark == 0)
23651 /* We haven't done this node yet. Mark it as used. */
23652 die->die_mark = 1;
23653 /* If this is the DIE of a generic type instantiation,
23654 mark the children DIEs that describe its generic parms and
23655 args. */
23656 prune_unused_types_mark_generic_parms_dies (die);
23658 /* We also have to mark its parents as used.
23659 (But we don't want to mark our parent's kids due to this,
23660 unless it is a class.) */
23661 if (die->die_parent)
23662 prune_unused_types_mark (die->die_parent,
23663 class_scope_p (die->die_parent));
23665 /* Mark any referenced nodes. */
23666 prune_unused_types_walk_attribs (die);
23668 /* If this node is a specification,
23669 also mark the definition, if it exists. */
23670 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23671 prune_unused_types_mark (die->die_definition, 1);
23674 if (dokids && die->die_mark != 2)
23676 /* We need to walk the children, but haven't done so yet.
23677 Remember that we've walked the kids. */
23678 die->die_mark = 2;
23680 /* If this is an array type, we need to make sure our
23681 kids get marked, even if they're types. If we're
23682 breaking out types into comdat sections, do this
23683 for all type definitions. */
23684 if (die->die_tag == DW_TAG_array_type
23685 || (use_debug_types
23686 && is_type_die (die) && ! is_declaration_die (die)))
23687 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23688 else
23689 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23693 /* For local classes, look if any static member functions were emitted
23694 and if so, mark them. */
23696 static void
23697 prune_unused_types_walk_local_classes (dw_die_ref die)
23699 dw_die_ref c;
23701 if (die->die_mark == 2)
23702 return;
23704 switch (die->die_tag)
23706 case DW_TAG_structure_type:
23707 case DW_TAG_union_type:
23708 case DW_TAG_class_type:
23709 break;
23711 case DW_TAG_subprogram:
23712 if (!get_AT_flag (die, DW_AT_declaration)
23713 || die->die_definition != NULL)
23714 prune_unused_types_mark (die, 1);
23715 return;
23717 default:
23718 return;
23721 /* Mark children. */
23722 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23725 /* Walk the tree DIE and mark types that we actually use. */
23727 static void
23728 prune_unused_types_walk (dw_die_ref die)
23730 dw_die_ref c;
23732 /* Don't do anything if this node is already marked and
23733 children have been marked as well. */
23734 if (die->die_mark == 2)
23735 return;
23737 switch (die->die_tag)
23739 case DW_TAG_structure_type:
23740 case DW_TAG_union_type:
23741 case DW_TAG_class_type:
23742 if (die->die_perennial_p)
23743 break;
23745 for (c = die->die_parent; c; c = c->die_parent)
23746 if (c->die_tag == DW_TAG_subprogram)
23747 break;
23749 /* Finding used static member functions inside of classes
23750 is needed just for local classes, because for other classes
23751 static member function DIEs with DW_AT_specification
23752 are emitted outside of the DW_TAG_*_type. If we ever change
23753 it, we'd need to call this even for non-local classes. */
23754 if (c)
23755 prune_unused_types_walk_local_classes (die);
23757 /* It's a type node --- don't mark it. */
23758 return;
23760 case DW_TAG_const_type:
23761 case DW_TAG_packed_type:
23762 case DW_TAG_pointer_type:
23763 case DW_TAG_reference_type:
23764 case DW_TAG_rvalue_reference_type:
23765 case DW_TAG_volatile_type:
23766 case DW_TAG_typedef:
23767 case DW_TAG_array_type:
23768 case DW_TAG_interface_type:
23769 case DW_TAG_friend:
23770 case DW_TAG_variant_part:
23771 case DW_TAG_enumeration_type:
23772 case DW_TAG_subroutine_type:
23773 case DW_TAG_string_type:
23774 case DW_TAG_set_type:
23775 case DW_TAG_subrange_type:
23776 case DW_TAG_ptr_to_member_type:
23777 case DW_TAG_file_type:
23778 if (die->die_perennial_p)
23779 break;
23781 /* It's a type node --- don't mark it. */
23782 return;
23784 default:
23785 /* Mark everything else. */
23786 break;
23789 if (die->die_mark == 0)
23791 die->die_mark = 1;
23793 /* Now, mark any dies referenced from here. */
23794 prune_unused_types_walk_attribs (die);
23797 die->die_mark = 2;
23799 /* Mark children. */
23800 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23803 /* Increment the string counts on strings referred to from DIE's
23804 attributes. */
23806 static void
23807 prune_unused_types_update_strings (dw_die_ref die)
23809 dw_attr_node *a;
23810 unsigned ix;
23812 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23813 if (AT_class (a) == dw_val_class_str)
23815 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23816 s->refcount++;
23817 /* Avoid unnecessarily putting strings that are used less than
23818 twice in the hash table. */
23819 if (s->refcount
23820 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23822 indirect_string_node **slot
23823 = debug_str_hash->find_slot_with_hash (s->str,
23824 htab_hash_string (s->str),
23825 INSERT);
23826 gcc_assert (*slot == NULL);
23827 *slot = s;
23832 /* Remove from the tree DIE any dies that aren't marked. */
23834 static void
23835 prune_unused_types_prune (dw_die_ref die)
23837 dw_die_ref c;
23839 gcc_assert (die->die_mark);
23840 prune_unused_types_update_strings (die);
23842 if (! die->die_child)
23843 return;
23845 c = die->die_child;
23846 do {
23847 dw_die_ref prev = c;
23848 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23849 if (c == die->die_child)
23851 /* No marked children between 'prev' and the end of the list. */
23852 if (prev == c)
23853 /* No marked children at all. */
23854 die->die_child = NULL;
23855 else
23857 prev->die_sib = c->die_sib;
23858 die->die_child = prev;
23860 return;
23863 if (c != prev->die_sib)
23864 prev->die_sib = c;
23865 prune_unused_types_prune (c);
23866 } while (c != die->die_child);
23869 /* Remove dies representing declarations that we never use. */
23871 static void
23872 prune_unused_types (void)
23874 unsigned int i;
23875 limbo_die_node *node;
23876 comdat_type_node *ctnode;
23877 pubname_entry *pub;
23878 dw_die_ref base_type;
23880 #if ENABLE_ASSERT_CHECKING
23881 /* All the marks should already be clear. */
23882 verify_marks_clear (comp_unit_die ());
23883 for (node = limbo_die_list; node; node = node->next)
23884 verify_marks_clear (node->die);
23885 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23886 verify_marks_clear (ctnode->root_die);
23887 #endif /* ENABLE_ASSERT_CHECKING */
23889 /* Mark types that are used in global variables. */
23890 premark_types_used_by_global_vars ();
23892 /* Set the mark on nodes that are actually used. */
23893 prune_unused_types_walk (comp_unit_die ());
23894 for (node = limbo_die_list; node; node = node->next)
23895 prune_unused_types_walk (node->die);
23896 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23898 prune_unused_types_walk (ctnode->root_die);
23899 prune_unused_types_mark (ctnode->type_die, 1);
23902 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23903 are unusual in that they are pubnames that are the children of pubtypes.
23904 They should only be marked via their parent DW_TAG_enumeration_type die,
23905 not as roots in themselves. */
23906 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23907 if (pub->die->die_tag != DW_TAG_enumerator)
23908 prune_unused_types_mark (pub->die, 1);
23909 for (i = 0; base_types.iterate (i, &base_type); i++)
23910 prune_unused_types_mark (base_type, 1);
23912 if (debug_str_hash)
23913 debug_str_hash->empty ();
23914 if (skeleton_debug_str_hash)
23915 skeleton_debug_str_hash->empty ();
23916 prune_unused_types_prune (comp_unit_die ());
23917 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23919 node = *pnode;
23920 if (!node->die->die_mark)
23921 *pnode = node->next;
23922 else
23924 prune_unused_types_prune (node->die);
23925 pnode = &node->next;
23928 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23929 prune_unused_types_prune (ctnode->root_die);
23931 /* Leave the marks clear. */
23932 prune_unmark_dies (comp_unit_die ());
23933 for (node = limbo_die_list; node; node = node->next)
23934 prune_unmark_dies (node->die);
23935 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23936 prune_unmark_dies (ctnode->root_die);
23939 /* Set the parameter to true if there are any relative pathnames in
23940 the file table. */
23942 file_table_relative_p (dwarf_file_data **slot, bool *p)
23944 struct dwarf_file_data *d = *slot;
23945 if (!IS_ABSOLUTE_PATH (d->filename))
23947 *p = true;
23948 return 0;
23950 return 1;
23953 /* Helpers to manipulate hash table of comdat type units. */
23955 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23957 static inline hashval_t hash (const comdat_type_node *);
23958 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23961 inline hashval_t
23962 comdat_type_hasher::hash (const comdat_type_node *type_node)
23964 hashval_t h;
23965 memcpy (&h, type_node->signature, sizeof (h));
23966 return h;
23969 inline bool
23970 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23971 const comdat_type_node *type_node_2)
23973 return (! memcmp (type_node_1->signature, type_node_2->signature,
23974 DWARF_TYPE_SIGNATURE_SIZE));
23977 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23978 to the location it would have been added, should we know its
23979 DECL_ASSEMBLER_NAME when we added other attributes. This will
23980 probably improve compactness of debug info, removing equivalent
23981 abbrevs, and hide any differences caused by deferring the
23982 computation of the assembler name, triggered by e.g. PCH. */
23984 static inline void
23985 move_linkage_attr (dw_die_ref die)
23987 unsigned ix = vec_safe_length (die->die_attr);
23988 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23990 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23991 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23993 while (--ix > 0)
23995 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23997 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23998 break;
24001 if (ix != vec_safe_length (die->die_attr) - 1)
24003 die->die_attr->pop ();
24004 die->die_attr->quick_insert (ix, linkage);
24008 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
24009 referenced from typed stack ops and count how often they are used. */
24011 static void
24012 mark_base_types (dw_loc_descr_ref loc)
24014 dw_die_ref base_type = NULL;
24016 for (; loc; loc = loc->dw_loc_next)
24018 switch (loc->dw_loc_opc)
24020 case DW_OP_GNU_regval_type:
24021 case DW_OP_GNU_deref_type:
24022 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24023 break;
24024 case DW_OP_GNU_convert:
24025 case DW_OP_GNU_reinterpret:
24026 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
24027 continue;
24028 /* FALLTHRU */
24029 case DW_OP_GNU_const_type:
24030 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24031 break;
24032 case DW_OP_GNU_entry_value:
24033 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24034 continue;
24035 default:
24036 continue;
24038 gcc_assert (base_type->die_parent == comp_unit_die ());
24039 if (base_type->die_mark)
24040 base_type->die_mark++;
24041 else
24043 base_types.safe_push (base_type);
24044 base_type->die_mark = 1;
24049 /* Comparison function for sorting marked base types. */
24051 static int
24052 base_type_cmp (const void *x, const void *y)
24054 dw_die_ref dx = *(const dw_die_ref *) x;
24055 dw_die_ref dy = *(const dw_die_ref *) y;
24056 unsigned int byte_size1, byte_size2;
24057 unsigned int encoding1, encoding2;
24058 if (dx->die_mark > dy->die_mark)
24059 return -1;
24060 if (dx->die_mark < dy->die_mark)
24061 return 1;
24062 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24063 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24064 if (byte_size1 < byte_size2)
24065 return 1;
24066 if (byte_size1 > byte_size2)
24067 return -1;
24068 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24069 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24070 if (encoding1 < encoding2)
24071 return 1;
24072 if (encoding1 > encoding2)
24073 return -1;
24074 return 0;
24077 /* Move base types marked by mark_base_types as early as possible
24078 in the CU, sorted by decreasing usage count both to make the
24079 uleb128 references as small as possible and to make sure they
24080 will have die_offset already computed by calc_die_sizes when
24081 sizes of typed stack loc ops is computed. */
24083 static void
24084 move_marked_base_types (void)
24086 unsigned int i;
24087 dw_die_ref base_type, die, c;
24089 if (base_types.is_empty ())
24090 return;
24092 /* Sort by decreasing usage count, they will be added again in that
24093 order later on. */
24094 base_types.qsort (base_type_cmp);
24095 die = comp_unit_die ();
24096 c = die->die_child;
24099 dw_die_ref prev = c;
24100 c = c->die_sib;
24101 while (c->die_mark)
24103 remove_child_with_prev (c, prev);
24104 /* As base types got marked, there must be at least
24105 one node other than DW_TAG_base_type. */
24106 gcc_assert (c != c->die_sib);
24107 c = c->die_sib;
24110 while (c != die->die_child);
24111 gcc_assert (die->die_child);
24112 c = die->die_child;
24113 for (i = 0; base_types.iterate (i, &base_type); i++)
24115 base_type->die_mark = 0;
24116 base_type->die_sib = c->die_sib;
24117 c->die_sib = base_type;
24118 c = base_type;
24122 /* Helper function for resolve_addr, attempt to resolve
24123 one CONST_STRING, return true if successful. Similarly verify that
24124 SYMBOL_REFs refer to variables emitted in the current CU. */
24126 static bool
24127 resolve_one_addr (rtx *addr)
24129 rtx rtl = *addr;
24131 if (GET_CODE (rtl) == CONST_STRING)
24133 size_t len = strlen (XSTR (rtl, 0)) + 1;
24134 tree t = build_string (len, XSTR (rtl, 0));
24135 tree tlen = size_int (len - 1);
24136 TREE_TYPE (t)
24137 = build_array_type (char_type_node, build_index_type (tlen));
24138 rtl = lookup_constant_def (t);
24139 if (!rtl || !MEM_P (rtl))
24140 return false;
24141 rtl = XEXP (rtl, 0);
24142 if (GET_CODE (rtl) == SYMBOL_REF
24143 && SYMBOL_REF_DECL (rtl)
24144 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24145 return false;
24146 vec_safe_push (used_rtx_array, rtl);
24147 *addr = rtl;
24148 return true;
24151 if (GET_CODE (rtl) == SYMBOL_REF
24152 && SYMBOL_REF_DECL (rtl))
24154 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24156 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24157 return false;
24159 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24160 return false;
24163 if (GET_CODE (rtl) == CONST)
24165 subrtx_ptr_iterator::array_type array;
24166 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24167 if (!resolve_one_addr (*iter))
24168 return false;
24171 return true;
24174 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24175 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24176 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24178 static rtx
24179 string_cst_pool_decl (tree t)
24181 rtx rtl = output_constant_def (t, 1);
24182 unsigned char *array;
24183 dw_loc_descr_ref l;
24184 tree decl;
24185 size_t len;
24186 dw_die_ref ref;
24188 if (!rtl || !MEM_P (rtl))
24189 return NULL_RTX;
24190 rtl = XEXP (rtl, 0);
24191 if (GET_CODE (rtl) != SYMBOL_REF
24192 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24193 return NULL_RTX;
24195 decl = SYMBOL_REF_DECL (rtl);
24196 if (!lookup_decl_die (decl))
24198 len = TREE_STRING_LENGTH (t);
24199 vec_safe_push (used_rtx_array, rtl);
24200 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24201 array = ggc_vec_alloc<unsigned char> (len);
24202 memcpy (array, TREE_STRING_POINTER (t), len);
24203 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24204 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24205 l->dw_loc_oprnd2.v.val_vec.length = len;
24206 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24207 l->dw_loc_oprnd2.v.val_vec.array = array;
24208 add_AT_loc (ref, DW_AT_location, l);
24209 equate_decl_number_to_die (decl, ref);
24211 return rtl;
24214 /* Helper function of resolve_addr_in_expr. LOC is
24215 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24216 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24217 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24218 with DW_OP_GNU_implicit_pointer if possible
24219 and return true, if unsuccessful, return false. */
24221 static bool
24222 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24224 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24225 HOST_WIDE_INT offset = 0;
24226 dw_die_ref ref = NULL;
24227 tree decl;
24229 if (GET_CODE (rtl) == CONST
24230 && GET_CODE (XEXP (rtl, 0)) == PLUS
24231 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24233 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24234 rtl = XEXP (XEXP (rtl, 0), 0);
24236 if (GET_CODE (rtl) == CONST_STRING)
24238 size_t len = strlen (XSTR (rtl, 0)) + 1;
24239 tree t = build_string (len, XSTR (rtl, 0));
24240 tree tlen = size_int (len - 1);
24242 TREE_TYPE (t)
24243 = build_array_type (char_type_node, build_index_type (tlen));
24244 rtl = string_cst_pool_decl (t);
24245 if (!rtl)
24246 return false;
24248 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24250 decl = SYMBOL_REF_DECL (rtl);
24251 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24253 ref = lookup_decl_die (decl);
24254 if (ref && (get_AT (ref, DW_AT_location)
24255 || get_AT (ref, DW_AT_const_value)))
24257 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24258 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24259 loc->dw_loc_oprnd1.val_entry = NULL;
24260 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24261 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24262 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24263 loc->dw_loc_oprnd2.v.val_int = offset;
24264 return true;
24268 return false;
24271 /* Helper function for resolve_addr, handle one location
24272 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24273 the location list couldn't be resolved. */
24275 static bool
24276 resolve_addr_in_expr (dw_loc_descr_ref loc)
24278 dw_loc_descr_ref keep = NULL;
24279 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24280 switch (loc->dw_loc_opc)
24282 case DW_OP_addr:
24283 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24285 if ((prev == NULL
24286 || prev->dw_loc_opc == DW_OP_piece
24287 || prev->dw_loc_opc == DW_OP_bit_piece)
24288 && loc->dw_loc_next
24289 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24290 && !dwarf_strict
24291 && optimize_one_addr_into_implicit_ptr (loc))
24292 break;
24293 return false;
24295 break;
24296 case DW_OP_GNU_addr_index:
24297 case DW_OP_GNU_const_index:
24298 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24299 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24301 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24302 if (!resolve_one_addr (&rtl))
24303 return false;
24304 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24305 loc->dw_loc_oprnd1.val_entry =
24306 add_addr_table_entry (rtl, ate_kind_rtx);
24308 break;
24309 case DW_OP_const4u:
24310 case DW_OP_const8u:
24311 if (loc->dtprel
24312 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24313 return false;
24314 break;
24315 case DW_OP_plus_uconst:
24316 if (size_of_loc_descr (loc)
24317 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24319 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24321 dw_loc_descr_ref repl
24322 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24323 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24324 add_loc_descr (&repl, loc->dw_loc_next);
24325 *loc = *repl;
24327 break;
24328 case DW_OP_implicit_value:
24329 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24330 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24331 return false;
24332 break;
24333 case DW_OP_GNU_implicit_pointer:
24334 case DW_OP_GNU_parameter_ref:
24335 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24337 dw_die_ref ref
24338 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24339 if (ref == NULL)
24340 return false;
24341 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24342 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24343 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24345 break;
24346 case DW_OP_GNU_const_type:
24347 case DW_OP_GNU_regval_type:
24348 case DW_OP_GNU_deref_type:
24349 case DW_OP_GNU_convert:
24350 case DW_OP_GNU_reinterpret:
24351 while (loc->dw_loc_next
24352 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24354 dw_die_ref base1, base2;
24355 unsigned enc1, enc2, size1, size2;
24356 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24357 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24358 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24359 else if (loc->dw_loc_oprnd1.val_class
24360 == dw_val_class_unsigned_const)
24361 break;
24362 else
24363 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24364 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24365 == dw_val_class_unsigned_const)
24366 break;
24367 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24368 gcc_assert (base1->die_tag == DW_TAG_base_type
24369 && base2->die_tag == DW_TAG_base_type);
24370 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24371 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24372 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24373 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24374 if (size1 == size2
24375 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24376 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24377 && loc != keep)
24378 || enc1 == enc2))
24380 /* Optimize away next DW_OP_GNU_convert after
24381 adjusting LOC's base type die reference. */
24382 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24383 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24384 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24385 else
24386 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24387 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24388 continue;
24390 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24391 point typed stack entry. */
24392 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24393 keep = loc->dw_loc_next;
24394 break;
24396 break;
24397 default:
24398 break;
24400 return true;
24403 /* Helper function of resolve_addr. DIE had DW_AT_location of
24404 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24405 and DW_OP_addr couldn't be resolved. resolve_addr has already
24406 removed the DW_AT_location attribute. This function attempts to
24407 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24408 to it or DW_AT_const_value attribute, if possible. */
24410 static void
24411 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24413 if (TREE_CODE (decl) != VAR_DECL
24414 || lookup_decl_die (decl) != die
24415 || DECL_EXTERNAL (decl)
24416 || !TREE_STATIC (decl)
24417 || DECL_INITIAL (decl) == NULL_TREE
24418 || DECL_P (DECL_INITIAL (decl))
24419 || get_AT (die, DW_AT_const_value))
24420 return;
24422 tree init = DECL_INITIAL (decl);
24423 HOST_WIDE_INT offset = 0;
24424 /* For variables that have been optimized away and thus
24425 don't have a memory location, see if we can emit
24426 DW_AT_const_value instead. */
24427 if (tree_add_const_value_attribute (die, init))
24428 return;
24429 if (dwarf_strict)
24430 return;
24431 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24432 and ADDR_EXPR refers to a decl that has DW_AT_location or
24433 DW_AT_const_value (but isn't addressable, otherwise
24434 resolving the original DW_OP_addr wouldn't fail), see if
24435 we can add DW_OP_GNU_implicit_pointer. */
24436 STRIP_NOPS (init);
24437 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24438 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24440 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24441 init = TREE_OPERAND (init, 0);
24442 STRIP_NOPS (init);
24444 if (TREE_CODE (init) != ADDR_EXPR)
24445 return;
24446 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24447 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24448 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24449 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24450 && TREE_OPERAND (init, 0) != decl))
24452 dw_die_ref ref;
24453 dw_loc_descr_ref l;
24455 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24457 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24458 if (!rtl)
24459 return;
24460 decl = SYMBOL_REF_DECL (rtl);
24462 else
24463 decl = TREE_OPERAND (init, 0);
24464 ref = lookup_decl_die (decl);
24465 if (ref == NULL
24466 || (!get_AT (ref, DW_AT_location)
24467 && !get_AT (ref, DW_AT_const_value)))
24468 return;
24469 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24470 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24471 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24472 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24473 add_AT_loc (die, DW_AT_location, l);
24477 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24478 an address in .rodata section if the string literal is emitted there,
24479 or remove the containing location list or replace DW_AT_const_value
24480 with DW_AT_location and empty location expression, if it isn't found
24481 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24482 to something that has been emitted in the current CU. */
24484 static void
24485 resolve_addr (dw_die_ref die)
24487 dw_die_ref c;
24488 dw_attr_node *a;
24489 dw_loc_list_ref *curr, *start, loc;
24490 unsigned ix;
24492 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24493 switch (AT_class (a))
24495 case dw_val_class_loc_list:
24496 start = curr = AT_loc_list_ptr (a);
24497 loc = *curr;
24498 gcc_assert (loc);
24499 /* The same list can be referenced more than once. See if we have
24500 already recorded the result from a previous pass. */
24501 if (loc->replaced)
24502 *curr = loc->dw_loc_next;
24503 else if (!loc->resolved_addr)
24505 /* As things stand, we do not expect or allow one die to
24506 reference a suffix of another die's location list chain.
24507 References must be identical or completely separate.
24508 There is therefore no need to cache the result of this
24509 pass on any list other than the first; doing so
24510 would lead to unnecessary writes. */
24511 while (*curr)
24513 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24514 if (!resolve_addr_in_expr ((*curr)->expr))
24516 dw_loc_list_ref next = (*curr)->dw_loc_next;
24517 dw_loc_descr_ref l = (*curr)->expr;
24519 if (next && (*curr)->ll_symbol)
24521 gcc_assert (!next->ll_symbol);
24522 next->ll_symbol = (*curr)->ll_symbol;
24524 if (dwarf_split_debug_info)
24525 remove_loc_list_addr_table_entries (l);
24526 *curr = next;
24528 else
24530 mark_base_types ((*curr)->expr);
24531 curr = &(*curr)->dw_loc_next;
24534 if (loc == *start)
24535 loc->resolved_addr = 1;
24536 else
24538 loc->replaced = 1;
24539 loc->dw_loc_next = *start;
24542 if (!*start)
24544 remove_AT (die, a->dw_attr);
24545 ix--;
24547 break;
24548 case dw_val_class_loc:
24550 dw_loc_descr_ref l = AT_loc (a);
24551 /* For -gdwarf-2 don't attempt to optimize
24552 DW_AT_data_member_location containing
24553 DW_OP_plus_uconst - older consumers might
24554 rely on it being that op instead of a more complex,
24555 but shorter, location description. */
24556 if ((dwarf_version > 2
24557 || a->dw_attr != DW_AT_data_member_location
24558 || l == NULL
24559 || l->dw_loc_opc != DW_OP_plus_uconst
24560 || l->dw_loc_next != NULL)
24561 && !resolve_addr_in_expr (l))
24563 if (dwarf_split_debug_info)
24564 remove_loc_list_addr_table_entries (l);
24565 if (l != NULL
24566 && l->dw_loc_next == NULL
24567 && l->dw_loc_opc == DW_OP_addr
24568 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24569 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24570 && a->dw_attr == DW_AT_location)
24572 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24573 remove_AT (die, a->dw_attr);
24574 ix--;
24575 optimize_location_into_implicit_ptr (die, decl);
24576 break;
24578 remove_AT (die, a->dw_attr);
24579 ix--;
24581 else
24582 mark_base_types (l);
24584 break;
24585 case dw_val_class_addr:
24586 if (a->dw_attr == DW_AT_const_value
24587 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24589 if (AT_index (a) != NOT_INDEXED)
24590 remove_addr_table_entry (a->dw_attr_val.val_entry);
24591 remove_AT (die, a->dw_attr);
24592 ix--;
24594 if (die->die_tag == DW_TAG_GNU_call_site
24595 && a->dw_attr == DW_AT_abstract_origin)
24597 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24598 dw_die_ref tdie = lookup_decl_die (tdecl);
24599 dw_die_ref cdie;
24600 if (tdie == NULL
24601 && DECL_EXTERNAL (tdecl)
24602 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24603 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24605 /* Creating a full DIE for tdecl is overly expensive and
24606 at this point even wrong when in the LTO phase
24607 as it can end up generating new type DIEs we didn't
24608 output and thus optimize_external_refs will crash. */
24609 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24610 add_AT_flag (tdie, DW_AT_external, 1);
24611 add_AT_flag (tdie, DW_AT_declaration, 1);
24612 add_linkage_attr (tdie, tdecl);
24613 add_name_and_src_coords_attributes (tdie, tdecl);
24614 equate_decl_number_to_die (tdecl, tdie);
24616 if (tdie)
24618 a->dw_attr_val.val_class = dw_val_class_die_ref;
24619 a->dw_attr_val.v.val_die_ref.die = tdie;
24620 a->dw_attr_val.v.val_die_ref.external = 0;
24622 else
24624 if (AT_index (a) != NOT_INDEXED)
24625 remove_addr_table_entry (a->dw_attr_val.val_entry);
24626 remove_AT (die, a->dw_attr);
24627 ix--;
24630 break;
24631 default:
24632 break;
24635 FOR_EACH_CHILD (die, c, resolve_addr (c));
24638 /* Helper routines for optimize_location_lists.
24639 This pass tries to share identical local lists in .debug_loc
24640 section. */
24642 /* Iteratively hash operands of LOC opcode into HSTATE. */
24644 static void
24645 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24647 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24648 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24650 switch (loc->dw_loc_opc)
24652 case DW_OP_const4u:
24653 case DW_OP_const8u:
24654 if (loc->dtprel)
24655 goto hash_addr;
24656 /* FALLTHRU */
24657 case DW_OP_const1u:
24658 case DW_OP_const1s:
24659 case DW_OP_const2u:
24660 case DW_OP_const2s:
24661 case DW_OP_const4s:
24662 case DW_OP_const8s:
24663 case DW_OP_constu:
24664 case DW_OP_consts:
24665 case DW_OP_pick:
24666 case DW_OP_plus_uconst:
24667 case DW_OP_breg0:
24668 case DW_OP_breg1:
24669 case DW_OP_breg2:
24670 case DW_OP_breg3:
24671 case DW_OP_breg4:
24672 case DW_OP_breg5:
24673 case DW_OP_breg6:
24674 case DW_OP_breg7:
24675 case DW_OP_breg8:
24676 case DW_OP_breg9:
24677 case DW_OP_breg10:
24678 case DW_OP_breg11:
24679 case DW_OP_breg12:
24680 case DW_OP_breg13:
24681 case DW_OP_breg14:
24682 case DW_OP_breg15:
24683 case DW_OP_breg16:
24684 case DW_OP_breg17:
24685 case DW_OP_breg18:
24686 case DW_OP_breg19:
24687 case DW_OP_breg20:
24688 case DW_OP_breg21:
24689 case DW_OP_breg22:
24690 case DW_OP_breg23:
24691 case DW_OP_breg24:
24692 case DW_OP_breg25:
24693 case DW_OP_breg26:
24694 case DW_OP_breg27:
24695 case DW_OP_breg28:
24696 case DW_OP_breg29:
24697 case DW_OP_breg30:
24698 case DW_OP_breg31:
24699 case DW_OP_regx:
24700 case DW_OP_fbreg:
24701 case DW_OP_piece:
24702 case DW_OP_deref_size:
24703 case DW_OP_xderef_size:
24704 hstate.add_object (val1->v.val_int);
24705 break;
24706 case DW_OP_skip:
24707 case DW_OP_bra:
24709 int offset;
24711 gcc_assert (val1->val_class == dw_val_class_loc);
24712 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24713 hstate.add_object (offset);
24715 break;
24716 case DW_OP_implicit_value:
24717 hstate.add_object (val1->v.val_unsigned);
24718 switch (val2->val_class)
24720 case dw_val_class_const:
24721 hstate.add_object (val2->v.val_int);
24722 break;
24723 case dw_val_class_vec:
24725 unsigned int elt_size = val2->v.val_vec.elt_size;
24726 unsigned int len = val2->v.val_vec.length;
24728 hstate.add_int (elt_size);
24729 hstate.add_int (len);
24730 hstate.add (val2->v.val_vec.array, len * elt_size);
24732 break;
24733 case dw_val_class_const_double:
24734 hstate.add_object (val2->v.val_double.low);
24735 hstate.add_object (val2->v.val_double.high);
24736 break;
24737 case dw_val_class_wide_int:
24738 hstate.add (val2->v.val_wide->get_val (),
24739 get_full_len (*val2->v.val_wide)
24740 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24741 break;
24742 case dw_val_class_addr:
24743 inchash::add_rtx (val2->v.val_addr, hstate);
24744 break;
24745 default:
24746 gcc_unreachable ();
24748 break;
24749 case DW_OP_bregx:
24750 case DW_OP_bit_piece:
24751 hstate.add_object (val1->v.val_int);
24752 hstate.add_object (val2->v.val_int);
24753 break;
24754 case DW_OP_addr:
24755 hash_addr:
24756 if (loc->dtprel)
24758 unsigned char dtprel = 0xd1;
24759 hstate.add_object (dtprel);
24761 inchash::add_rtx (val1->v.val_addr, hstate);
24762 break;
24763 case DW_OP_GNU_addr_index:
24764 case DW_OP_GNU_const_index:
24766 if (loc->dtprel)
24768 unsigned char dtprel = 0xd1;
24769 hstate.add_object (dtprel);
24771 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24773 break;
24774 case DW_OP_GNU_implicit_pointer:
24775 hstate.add_int (val2->v.val_int);
24776 break;
24777 case DW_OP_GNU_entry_value:
24778 hstate.add_object (val1->v.val_loc);
24779 break;
24780 case DW_OP_GNU_regval_type:
24781 case DW_OP_GNU_deref_type:
24783 unsigned int byte_size
24784 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24785 unsigned int encoding
24786 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24787 hstate.add_object (val1->v.val_int);
24788 hstate.add_object (byte_size);
24789 hstate.add_object (encoding);
24791 break;
24792 case DW_OP_GNU_convert:
24793 case DW_OP_GNU_reinterpret:
24794 if (val1->val_class == dw_val_class_unsigned_const)
24796 hstate.add_object (val1->v.val_unsigned);
24797 break;
24799 /* FALLTHRU */
24800 case DW_OP_GNU_const_type:
24802 unsigned int byte_size
24803 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24804 unsigned int encoding
24805 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24806 hstate.add_object (byte_size);
24807 hstate.add_object (encoding);
24808 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24809 break;
24810 hstate.add_object (val2->val_class);
24811 switch (val2->val_class)
24813 case dw_val_class_const:
24814 hstate.add_object (val2->v.val_int);
24815 break;
24816 case dw_val_class_vec:
24818 unsigned int elt_size = val2->v.val_vec.elt_size;
24819 unsigned int len = val2->v.val_vec.length;
24821 hstate.add_object (elt_size);
24822 hstate.add_object (len);
24823 hstate.add (val2->v.val_vec.array, len * elt_size);
24825 break;
24826 case dw_val_class_const_double:
24827 hstate.add_object (val2->v.val_double.low);
24828 hstate.add_object (val2->v.val_double.high);
24829 break;
24830 case dw_val_class_wide_int:
24831 hstate.add (val2->v.val_wide->get_val (),
24832 get_full_len (*val2->v.val_wide)
24833 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24834 break;
24835 default:
24836 gcc_unreachable ();
24839 break;
24841 default:
24842 /* Other codes have no operands. */
24843 break;
24847 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24849 static inline void
24850 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24852 dw_loc_descr_ref l;
24853 bool sizes_computed = false;
24854 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24855 size_of_locs (loc);
24857 for (l = loc; l != NULL; l = l->dw_loc_next)
24859 enum dwarf_location_atom opc = l->dw_loc_opc;
24860 hstate.add_object (opc);
24861 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24863 size_of_locs (loc);
24864 sizes_computed = true;
24866 hash_loc_operands (l, hstate);
24870 /* Compute hash of the whole location list LIST_HEAD. */
24872 static inline void
24873 hash_loc_list (dw_loc_list_ref list_head)
24875 dw_loc_list_ref curr = list_head;
24876 inchash::hash hstate;
24878 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24880 hstate.add (curr->begin, strlen (curr->begin) + 1);
24881 hstate.add (curr->end, strlen (curr->end) + 1);
24882 if (curr->section)
24883 hstate.add (curr->section, strlen (curr->section) + 1);
24884 hash_locs (curr->expr, hstate);
24886 list_head->hash = hstate.end ();
24889 /* Return true if X and Y opcodes have the same operands. */
24891 static inline bool
24892 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24894 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24895 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24896 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24897 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24899 switch (x->dw_loc_opc)
24901 case DW_OP_const4u:
24902 case DW_OP_const8u:
24903 if (x->dtprel)
24904 goto hash_addr;
24905 /* FALLTHRU */
24906 case DW_OP_const1u:
24907 case DW_OP_const1s:
24908 case DW_OP_const2u:
24909 case DW_OP_const2s:
24910 case DW_OP_const4s:
24911 case DW_OP_const8s:
24912 case DW_OP_constu:
24913 case DW_OP_consts:
24914 case DW_OP_pick:
24915 case DW_OP_plus_uconst:
24916 case DW_OP_breg0:
24917 case DW_OP_breg1:
24918 case DW_OP_breg2:
24919 case DW_OP_breg3:
24920 case DW_OP_breg4:
24921 case DW_OP_breg5:
24922 case DW_OP_breg6:
24923 case DW_OP_breg7:
24924 case DW_OP_breg8:
24925 case DW_OP_breg9:
24926 case DW_OP_breg10:
24927 case DW_OP_breg11:
24928 case DW_OP_breg12:
24929 case DW_OP_breg13:
24930 case DW_OP_breg14:
24931 case DW_OP_breg15:
24932 case DW_OP_breg16:
24933 case DW_OP_breg17:
24934 case DW_OP_breg18:
24935 case DW_OP_breg19:
24936 case DW_OP_breg20:
24937 case DW_OP_breg21:
24938 case DW_OP_breg22:
24939 case DW_OP_breg23:
24940 case DW_OP_breg24:
24941 case DW_OP_breg25:
24942 case DW_OP_breg26:
24943 case DW_OP_breg27:
24944 case DW_OP_breg28:
24945 case DW_OP_breg29:
24946 case DW_OP_breg30:
24947 case DW_OP_breg31:
24948 case DW_OP_regx:
24949 case DW_OP_fbreg:
24950 case DW_OP_piece:
24951 case DW_OP_deref_size:
24952 case DW_OP_xderef_size:
24953 return valx1->v.val_int == valy1->v.val_int;
24954 case DW_OP_skip:
24955 case DW_OP_bra:
24956 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24957 can cause irrelevant differences in dw_loc_addr. */
24958 gcc_assert (valx1->val_class == dw_val_class_loc
24959 && valy1->val_class == dw_val_class_loc
24960 && (dwarf_split_debug_info
24961 || x->dw_loc_addr == y->dw_loc_addr));
24962 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24963 case DW_OP_implicit_value:
24964 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24965 || valx2->val_class != valy2->val_class)
24966 return false;
24967 switch (valx2->val_class)
24969 case dw_val_class_const:
24970 return valx2->v.val_int == valy2->v.val_int;
24971 case dw_val_class_vec:
24972 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24973 && valx2->v.val_vec.length == valy2->v.val_vec.length
24974 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24975 valx2->v.val_vec.elt_size
24976 * valx2->v.val_vec.length) == 0;
24977 case dw_val_class_const_double:
24978 return valx2->v.val_double.low == valy2->v.val_double.low
24979 && valx2->v.val_double.high == valy2->v.val_double.high;
24980 case dw_val_class_wide_int:
24981 return *valx2->v.val_wide == *valy2->v.val_wide;
24982 case dw_val_class_addr:
24983 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24984 default:
24985 gcc_unreachable ();
24987 case DW_OP_bregx:
24988 case DW_OP_bit_piece:
24989 return valx1->v.val_int == valy1->v.val_int
24990 && valx2->v.val_int == valy2->v.val_int;
24991 case DW_OP_addr:
24992 hash_addr:
24993 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24994 case DW_OP_GNU_addr_index:
24995 case DW_OP_GNU_const_index:
24997 rtx ax1 = valx1->val_entry->addr.rtl;
24998 rtx ay1 = valy1->val_entry->addr.rtl;
24999 return rtx_equal_p (ax1, ay1);
25001 case DW_OP_GNU_implicit_pointer:
25002 return valx1->val_class == dw_val_class_die_ref
25003 && valx1->val_class == valy1->val_class
25004 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
25005 && valx2->v.val_int == valy2->v.val_int;
25006 case DW_OP_GNU_entry_value:
25007 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
25008 case DW_OP_GNU_const_type:
25009 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
25010 || valx2->val_class != valy2->val_class)
25011 return false;
25012 switch (valx2->val_class)
25014 case dw_val_class_const:
25015 return valx2->v.val_int == valy2->v.val_int;
25016 case dw_val_class_vec:
25017 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
25018 && valx2->v.val_vec.length == valy2->v.val_vec.length
25019 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
25020 valx2->v.val_vec.elt_size
25021 * valx2->v.val_vec.length) == 0;
25022 case dw_val_class_const_double:
25023 return valx2->v.val_double.low == valy2->v.val_double.low
25024 && valx2->v.val_double.high == valy2->v.val_double.high;
25025 case dw_val_class_wide_int:
25026 return *valx2->v.val_wide == *valy2->v.val_wide;
25027 default:
25028 gcc_unreachable ();
25030 case DW_OP_GNU_regval_type:
25031 case DW_OP_GNU_deref_type:
25032 return valx1->v.val_int == valy1->v.val_int
25033 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
25034 case DW_OP_GNU_convert:
25035 case DW_OP_GNU_reinterpret:
25036 if (valx1->val_class != valy1->val_class)
25037 return false;
25038 if (valx1->val_class == dw_val_class_unsigned_const)
25039 return valx1->v.val_unsigned == valy1->v.val_unsigned;
25040 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25041 case DW_OP_GNU_parameter_ref:
25042 return valx1->val_class == dw_val_class_die_ref
25043 && valx1->val_class == valy1->val_class
25044 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25045 default:
25046 /* Other codes have no operands. */
25047 return true;
25051 /* Return true if DWARF location expressions X and Y are the same. */
25053 static inline bool
25054 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25056 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25057 if (x->dw_loc_opc != y->dw_loc_opc
25058 || x->dtprel != y->dtprel
25059 || !compare_loc_operands (x, y))
25060 break;
25061 return x == NULL && y == NULL;
25064 /* Hashtable helpers. */
25066 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25068 static inline hashval_t hash (const dw_loc_list_struct *);
25069 static inline bool equal (const dw_loc_list_struct *,
25070 const dw_loc_list_struct *);
25073 /* Return precomputed hash of location list X. */
25075 inline hashval_t
25076 loc_list_hasher::hash (const dw_loc_list_struct *x)
25078 return x->hash;
25081 /* Return true if location lists A and B are the same. */
25083 inline bool
25084 loc_list_hasher::equal (const dw_loc_list_struct *a,
25085 const dw_loc_list_struct *b)
25087 if (a == b)
25088 return 1;
25089 if (a->hash != b->hash)
25090 return 0;
25091 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25092 if (strcmp (a->begin, b->begin) != 0
25093 || strcmp (a->end, b->end) != 0
25094 || (a->section == NULL) != (b->section == NULL)
25095 || (a->section && strcmp (a->section, b->section) != 0)
25096 || !compare_locs (a->expr, b->expr))
25097 break;
25098 return a == NULL && b == NULL;
25101 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25104 /* Recursively optimize location lists referenced from DIE
25105 children and share them whenever possible. */
25107 static void
25108 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25110 dw_die_ref c;
25111 dw_attr_node *a;
25112 unsigned ix;
25113 dw_loc_list_struct **slot;
25115 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25116 if (AT_class (a) == dw_val_class_loc_list)
25118 dw_loc_list_ref list = AT_loc_list (a);
25119 /* TODO: perform some optimizations here, before hashing
25120 it and storing into the hash table. */
25121 hash_loc_list (list);
25122 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25123 if (*slot == NULL)
25124 *slot = list;
25125 else
25126 a->dw_attr_val.v.val_loc_list = *slot;
25129 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25133 /* Recursively assign each location list a unique index into the debug_addr
25134 section. */
25136 static void
25137 index_location_lists (dw_die_ref die)
25139 dw_die_ref c;
25140 dw_attr_node *a;
25141 unsigned ix;
25143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25144 if (AT_class (a) == dw_val_class_loc_list)
25146 dw_loc_list_ref list = AT_loc_list (a);
25147 dw_loc_list_ref curr;
25148 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25150 /* Don't index an entry that has already been indexed
25151 or won't be output. */
25152 if (curr->begin_entry != NULL
25153 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25154 continue;
25156 curr->begin_entry
25157 = add_addr_table_entry (xstrdup (curr->begin),
25158 ate_kind_label);
25162 FOR_EACH_CHILD (die, c, index_location_lists (c));
25165 /* Optimize location lists referenced from DIE
25166 children and share them whenever possible. */
25168 static void
25169 optimize_location_lists (dw_die_ref die)
25171 loc_list_hash_type htab (500);
25172 optimize_location_lists_1 (die, &htab);
25175 /* Traverse the limbo die list, and add parent/child links. The only
25176 dies without parents that should be here are concrete instances of
25177 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25178 For concrete instances, we can get the parent die from the abstract
25179 instance. */
25181 static void
25182 flush_limbo_die_list (void)
25184 limbo_die_node *node, *next_node;
25186 for (node = limbo_die_list; node; node = next_node)
25188 dw_die_ref die = node->die;
25189 next_node = node->next;
25191 if (die->die_parent == NULL)
25193 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25195 if (origin && origin->die_parent)
25196 add_child_die (origin->die_parent, die);
25197 else if (is_cu_die (die))
25199 else if (seen_error ())
25200 /* It's OK to be confused by errors in the input. */
25201 add_child_die (comp_unit_die (), die);
25202 else
25204 /* In certain situations, the lexical block containing a
25205 nested function can be optimized away, which results
25206 in the nested function die being orphaned. Likewise
25207 with the return type of that nested function. Force
25208 this to be a child of the containing function.
25210 It may happen that even the containing function got fully
25211 inlined and optimized out. In that case we are lost and
25212 assign the empty child. This should not be big issue as
25213 the function is likely unreachable too. */
25214 gcc_assert (node->created_for);
25216 if (DECL_P (node->created_for))
25217 origin = get_context_die (DECL_CONTEXT (node->created_for));
25218 else if (TYPE_P (node->created_for))
25219 origin = scope_die_for (node->created_for, comp_unit_die ());
25220 else
25221 origin = comp_unit_die ();
25223 add_child_die (origin, die);
25228 limbo_die_list = NULL;
25231 /* Output stuff that dwarf requires at the end of every file,
25232 and generate the DWARF-2 debugging info. */
25234 static void
25235 dwarf2out_finish (const char *filename)
25237 comdat_type_node *ctnode;
25238 dw_die_ref main_comp_unit_die;
25240 /* Flush out any latecomers to the limbo party. */
25241 flush_limbo_die_list ();
25243 /* We shouldn't have any symbols with delayed asm names for
25244 DIEs generated after early finish. */
25245 gcc_assert (deferred_asm_name == NULL);
25247 /* PCH might result in DW_AT_producer string being restored from the
25248 header compilation, so always fill it with empty string initially
25249 and overwrite only here. */
25250 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
25251 producer_string = gen_producer_string ();
25252 producer->dw_attr_val.v.val_str->refcount--;
25253 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25255 gen_remaining_tmpl_value_param_die_attribute ();
25257 /* Add the name for the main input file now. We delayed this from
25258 dwarf2out_init to avoid complications with PCH.
25259 For LTO produced units use a fixed artificial name to avoid
25260 leaking tempfile names into the dwarf. */
25261 if (!in_lto_p)
25262 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25263 else
25264 add_name_attribute (comp_unit_die (), "<artificial>");
25265 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25266 add_comp_dir_attribute (comp_unit_die ());
25267 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25269 bool p = false;
25270 file_table->traverse<bool *, file_table_relative_p> (&p);
25271 if (p)
25272 add_comp_dir_attribute (comp_unit_die ());
25275 #if ENABLE_ASSERT_CHECKING
25277 dw_die_ref die = comp_unit_die (), c;
25278 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25280 #endif
25281 resolve_addr (comp_unit_die ());
25282 move_marked_base_types ();
25284 /* Walk through the list of incomplete types again, trying once more to
25285 emit full debugging info for them. */
25286 retry_incomplete_types ();
25288 if (flag_eliminate_unused_debug_types)
25289 prune_unused_types ();
25291 /* Generate separate COMDAT sections for type DIEs. */
25292 if (use_debug_types)
25294 break_out_comdat_types (comp_unit_die ());
25296 /* Each new type_unit DIE was added to the limbo die list when created.
25297 Since these have all been added to comdat_type_list, clear the
25298 limbo die list. */
25299 limbo_die_list = NULL;
25301 /* For each new comdat type unit, copy declarations for incomplete
25302 types to make the new unit self-contained (i.e., no direct
25303 references to the main compile unit). */
25304 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25305 copy_decls_for_unworthy_types (ctnode->root_die);
25306 copy_decls_for_unworthy_types (comp_unit_die ());
25308 /* In the process of copying declarations from one unit to another,
25309 we may have left some declarations behind that are no longer
25310 referenced. Prune them. */
25311 prune_unused_types ();
25314 /* Generate separate CUs for each of the include files we've seen.
25315 They will go into limbo_die_list. */
25316 if (flag_eliminate_dwarf2_dups)
25317 break_out_includes (comp_unit_die ());
25319 /* Traverse the DIE's and add sibling attributes to those DIE's that
25320 have children. */
25321 add_sibling_attributes (comp_unit_die ());
25322 limbo_die_node *node;
25323 for (node = limbo_die_list; node; node = node->next)
25324 add_sibling_attributes (node->die);
25325 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25326 add_sibling_attributes (ctnode->root_die);
25328 /* When splitting DWARF info, we put some attributes in the
25329 skeleton compile_unit DIE that remains in the .o, while
25330 most attributes go in the DWO compile_unit_die. */
25331 if (dwarf_split_debug_info)
25332 main_comp_unit_die = gen_compile_unit_die (NULL);
25333 else
25334 main_comp_unit_die = comp_unit_die ();
25336 /* Output a terminator label for the .text section. */
25337 switch_to_section (text_section);
25338 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25339 if (cold_text_section)
25341 switch_to_section (cold_text_section);
25342 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25345 /* We can only use the low/high_pc attributes if all of the code was
25346 in .text. */
25347 if (!have_multiple_function_sections
25348 || (dwarf_version < 3 && dwarf_strict))
25350 /* Don't add if the CU has no associated code. */
25351 if (text_section_used)
25352 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25353 text_end_label, true);
25355 else
25357 unsigned fde_idx;
25358 dw_fde_ref fde;
25359 bool range_list_added = false;
25361 if (text_section_used)
25362 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25363 text_end_label, &range_list_added, true);
25364 if (cold_text_section_used)
25365 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25366 cold_end_label, &range_list_added, true);
25368 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25370 if (DECL_IGNORED_P (fde->decl))
25371 continue;
25372 if (!fde->in_std_section)
25373 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25374 fde->dw_fde_end, &range_list_added,
25375 true);
25376 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25377 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25378 fde->dw_fde_second_end, &range_list_added,
25379 true);
25382 if (range_list_added)
25384 /* We need to give .debug_loc and .debug_ranges an appropriate
25385 "base address". Use zero so that these addresses become
25386 absolute. Historically, we've emitted the unexpected
25387 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25388 Emit both to give time for other tools to adapt. */
25389 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25390 if (! dwarf_strict && dwarf_version < 4)
25391 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25393 add_ranges (NULL);
25397 if (debug_info_level >= DINFO_LEVEL_TERSE)
25398 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25399 debug_line_section_label);
25401 if (have_macinfo)
25402 add_AT_macptr (comp_unit_die (),
25403 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25404 macinfo_section_label);
25406 if (dwarf_split_debug_info)
25408 /* optimize_location_lists calculates the size of the lists,
25409 so index them first, and assign indices to the entries.
25410 Although optimize_location_lists will remove entries from
25411 the table, it only does so for duplicates, and therefore
25412 only reduces ref_counts to 1. */
25413 index_location_lists (comp_unit_die ());
25415 if (addr_index_table != NULL)
25417 unsigned int index = 0;
25418 addr_index_table
25419 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25420 (&index);
25424 if (have_location_lists)
25425 optimize_location_lists (comp_unit_die ());
25427 save_macinfo_strings ();
25429 if (dwarf_split_debug_info)
25431 unsigned int index = 0;
25433 /* Add attributes common to skeleton compile_units and
25434 type_units. Because these attributes include strings, it
25435 must be done before freezing the string table. Top-level
25436 skeleton die attrs are added when the skeleton type unit is
25437 created, so ensure it is created by this point. */
25438 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25439 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25442 /* Output all of the compilation units. We put the main one last so that
25443 the offsets are available to output_pubnames. */
25444 for (node = limbo_die_list; node; node = node->next)
25445 output_comp_unit (node->die, 0);
25447 hash_table<comdat_type_hasher> comdat_type_table (100);
25448 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25450 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25452 /* Don't output duplicate types. */
25453 if (*slot != HTAB_EMPTY_ENTRY)
25454 continue;
25456 /* Add a pointer to the line table for the main compilation unit
25457 so that the debugger can make sense of DW_AT_decl_file
25458 attributes. */
25459 if (debug_info_level >= DINFO_LEVEL_TERSE)
25460 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25461 (!dwarf_split_debug_info
25462 ? debug_line_section_label
25463 : debug_skeleton_line_section_label));
25465 output_comdat_type_unit (ctnode);
25466 *slot = ctnode;
25469 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25470 both the main_cu and all skeleton TUs. Making this call unconditional
25471 would end up either adding a second copy of the AT_pubnames attribute, or
25472 requiring a special case in add_top_level_skeleton_die_attrs. */
25473 if (!dwarf_split_debug_info)
25474 add_AT_pubnames (comp_unit_die ());
25476 if (dwarf_split_debug_info)
25478 int mark;
25479 unsigned char checksum[16];
25480 struct md5_ctx ctx;
25482 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25483 md5_init_ctx (&ctx);
25484 mark = 0;
25485 die_checksum (comp_unit_die (), &ctx, &mark);
25486 unmark_all_dies (comp_unit_die ());
25487 md5_finish_ctx (&ctx, checksum);
25489 /* Use the first 8 bytes of the checksum as the dwo_id,
25490 and add it to both comp-unit DIEs. */
25491 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25492 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25494 /* Add the base offset of the ranges table to the skeleton
25495 comp-unit DIE. */
25496 if (ranges_table_in_use)
25497 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25498 ranges_section_label);
25500 switch_to_section (debug_addr_section);
25501 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25502 output_addr_table ();
25505 /* Output the main compilation unit if non-empty or if .debug_macinfo
25506 or .debug_macro will be emitted. */
25507 output_comp_unit (comp_unit_die (), have_macinfo);
25509 if (dwarf_split_debug_info && info_section_emitted)
25510 output_skeleton_debug_sections (main_comp_unit_die);
25512 /* Output the abbreviation table. */
25513 if (abbrev_die_table_in_use != 1)
25515 switch_to_section (debug_abbrev_section);
25516 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25517 output_abbrev_section ();
25520 /* Output location list section if necessary. */
25521 if (have_location_lists)
25523 /* Output the location lists info. */
25524 switch_to_section (debug_loc_section);
25525 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25526 output_location_lists (comp_unit_die ());
25529 output_pubtables ();
25531 /* Output the address range information if a CU (.debug_info section)
25532 was emitted. We output an empty table even if we had no functions
25533 to put in it. This because the consumer has no way to tell the
25534 difference between an empty table that we omitted and failure to
25535 generate a table that would have contained data. */
25536 if (info_section_emitted)
25538 switch_to_section (debug_aranges_section);
25539 output_aranges ();
25542 /* Output ranges section if necessary. */
25543 if (ranges_table_in_use)
25545 switch_to_section (debug_ranges_section);
25546 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25547 output_ranges ();
25550 /* Have to end the macro section. */
25551 if (have_macinfo)
25553 switch_to_section (debug_macinfo_section);
25554 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25555 output_macinfo ();
25556 dw2_asm_output_data (1, 0, "End compilation unit");
25559 /* Output the source line correspondence table. We must do this
25560 even if there is no line information. Otherwise, on an empty
25561 translation unit, we will generate a present, but empty,
25562 .debug_info section. IRIX 6.5 `nm' will then complain when
25563 examining the file. This is done late so that any filenames
25564 used by the debug_info section are marked as 'used'. */
25565 switch_to_section (debug_line_section);
25566 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25567 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25568 output_line_info (false);
25570 if (dwarf_split_debug_info && info_section_emitted)
25572 switch_to_section (debug_skeleton_line_section);
25573 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25574 output_line_info (true);
25577 /* If we emitted any indirect strings, output the string table too. */
25578 if (debug_str_hash || skeleton_debug_str_hash)
25579 output_indirect_strings ();
25582 /* Perform any cleanups needed after the early debug generation pass
25583 has run. */
25585 static void
25586 dwarf2out_early_finish (void)
25588 limbo_die_node *node;
25590 /* Add DW_AT_linkage_name for all deferred DIEs. */
25591 for (node = deferred_asm_name; node; node = node->next)
25593 tree decl = node->created_for;
25594 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25595 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25596 ended up in deferred_asm_name before we knew it was
25597 constant and never written to disk. */
25598 && DECL_ASSEMBLER_NAME (decl))
25600 add_linkage_attr (node->die, decl);
25601 move_linkage_attr (node->die);
25604 deferred_asm_name = NULL;
25606 /* The point here is to flush out the limbo list so that it is empty
25607 and we don't need to stream it for LTO. */
25608 flush_limbo_die_list ();
25610 gen_scheduled_generic_parms_dies ();
25611 gen_remaining_tmpl_value_param_die_attribute ();
25614 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25615 within the same process. For use by toplev::finalize. */
25617 void
25618 dwarf2out_c_finalize (void)
25620 last_var_location_insn = NULL;
25621 cached_next_real_insn = NULL;
25622 used_rtx_array = NULL;
25623 incomplete_types = NULL;
25624 decl_scope_table = NULL;
25625 debug_info_section = NULL;
25626 debug_skeleton_info_section = NULL;
25627 debug_abbrev_section = NULL;
25628 debug_skeleton_abbrev_section = NULL;
25629 debug_aranges_section = NULL;
25630 debug_addr_section = NULL;
25631 debug_macinfo_section = NULL;
25632 debug_line_section = NULL;
25633 debug_skeleton_line_section = NULL;
25634 debug_loc_section = NULL;
25635 debug_pubnames_section = NULL;
25636 debug_pubtypes_section = NULL;
25637 debug_str_section = NULL;
25638 debug_str_dwo_section = NULL;
25639 debug_str_offsets_section = NULL;
25640 debug_ranges_section = NULL;
25641 debug_frame_section = NULL;
25642 fde_vec = NULL;
25643 debug_str_hash = NULL;
25644 skeleton_debug_str_hash = NULL;
25645 dw2_string_counter = 0;
25646 have_multiple_function_sections = false;
25647 text_section_used = false;
25648 cold_text_section_used = false;
25649 cold_text_section = NULL;
25650 current_unit_personality = NULL;
25652 next_die_offset = 0;
25653 single_comp_unit_die = NULL;
25654 comdat_type_list = NULL;
25655 limbo_die_list = NULL;
25656 file_table = NULL;
25657 decl_die_table = NULL;
25658 common_block_die_table = NULL;
25659 decl_loc_table = NULL;
25660 call_arg_locations = NULL;
25661 call_arg_loc_last = NULL;
25662 call_site_count = -1;
25663 tail_call_site_count = -1;
25664 cached_dw_loc_list_table = NULL;
25665 abbrev_die_table = NULL;
25666 abbrev_die_table_allocated = 0;
25667 abbrev_die_table_in_use = 0;
25668 line_info_label_num = 0;
25669 cur_line_info_table = NULL;
25670 text_section_line_info = NULL;
25671 cold_text_section_line_info = NULL;
25672 separate_line_info = NULL;
25673 info_section_emitted = false;
25674 pubname_table = NULL;
25675 pubtype_table = NULL;
25676 macinfo_table = NULL;
25677 ranges_table = NULL;
25678 ranges_table_allocated = 0;
25679 ranges_table_in_use = 0;
25680 ranges_by_label = 0;
25681 ranges_by_label_allocated = 0;
25682 ranges_by_label_in_use = 0;
25683 have_location_lists = false;
25684 loclabel_num = 0;
25685 poc_label_num = 0;
25686 last_emitted_file = NULL;
25687 label_num = 0;
25688 tmpl_value_parm_die_table = NULL;
25689 generic_type_instances = NULL;
25690 frame_pointer_fb_offset = 0;
25691 frame_pointer_fb_offset_valid = false;
25692 base_types.release ();
25693 XDELETEVEC (producer_string);
25694 producer_string = NULL;
25697 #include "gt-dwarf2out.h"