2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / dwarf2out.c
blob8a36fe8db355b9f151415f7031a53e6af1248b54
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 "input.h"
64 #include "alias.h"
65 #include "symtab.h"
66 #include "tree.h"
67 #include "fold-const.h"
68 #include "stringpool.h"
69 #include "stor-layout.h"
70 #include "varasm.h"
71 #include "hard-reg-set.h"
72 #include "function.h"
73 #include "emit-rtl.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "regs.h"
77 #include "rtlhash.h"
78 #include "insn-config.h"
79 #include "reload.h"
80 #include "output.h"
81 #include "expmed.h"
82 #include "dojump.h"
83 #include "explow.h"
84 #include "calls.h"
85 #include "stmt.h"
86 #include "expr.h"
87 #include "except.h"
88 #include "dwarf2.h"
89 #include "dwarf2out.h"
90 #include "dwarf2asm.h"
91 #include "toplev.h"
92 #include "md5.h"
93 #include "tm_p.h"
94 #include "diagnostic.h"
95 #include "tree-pretty-print.h"
96 #include "debug.h"
97 #include "target.h"
98 #include "common/common-target.h"
99 #include "langhooks.h"
100 #include "is-a.h"
101 #include "plugin-api.h"
102 #include "ipa-ref.h"
103 #include "cgraph.h"
104 #include "ira.h"
105 #include "lra.h"
106 #include "dumpfile.h"
107 #include "opts.h"
108 #include "tree-dfa.h"
109 #include "gdb/gdb-index.h"
110 #include "rtl-iter.h"
112 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
113 static rtx_insn *last_var_location_insn;
114 static rtx_insn *cached_next_real_insn;
115 static void dwarf2out_decl (tree);
117 #ifdef VMS_DEBUGGING_INFO
118 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
120 /* Define this macro to be a nonzero value if the directory specifications
121 which are output in the debug info should end with a separator. */
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
123 /* Define this macro to evaluate to a nonzero value if GCC should refrain
124 from generating indirect strings in DWARF2 debug information, for instance
125 if your target is stuck with an old version of GDB that is unable to
126 process them properly or uses VMS Debug. */
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
128 #else
129 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
130 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
131 #endif
133 /* ??? Poison these here until it can be done generically. They've been
134 totally replaced in this file; make sure it stays that way. */
135 #undef DWARF2_UNWIND_INFO
136 #undef DWARF2_FRAME_INFO
137 #if (GCC_VERSION >= 3000)
138 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
139 #endif
141 /* The size of the target's pointer type. */
142 #ifndef PTR_SIZE
143 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
144 #endif
146 /* Array of RTXes referenced by the debugging information, which therefore
147 must be kept around forever. */
148 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
150 /* A pointer to the base of a list of incomplete types which might be
151 completed at some later time. incomplete_types_list needs to be a
152 vec<tree, va_gc> *because we want to tell the garbage collector about
153 it. */
154 static GTY(()) vec<tree, va_gc> *incomplete_types;
156 /* A pointer to the base of a table of references to declaration
157 scopes. This table is a display which tracks the nesting
158 of declaration scopes at the current scope and containing
159 scopes. This table is used to find the proper place to
160 define type declaration DIE's. */
161 static GTY(()) vec<tree, va_gc> *decl_scope_table;
163 /* Pointers to various DWARF2 sections. */
164 static GTY(()) section *debug_info_section;
165 static GTY(()) section *debug_skeleton_info_section;
166 static GTY(()) section *debug_abbrev_section;
167 static GTY(()) section *debug_skeleton_abbrev_section;
168 static GTY(()) section *debug_aranges_section;
169 static GTY(()) section *debug_addr_section;
170 static GTY(()) section *debug_macinfo_section;
171 static GTY(()) section *debug_line_section;
172 static GTY(()) section *debug_skeleton_line_section;
173 static GTY(()) section *debug_loc_section;
174 static GTY(()) section *debug_pubnames_section;
175 static GTY(()) section *debug_pubtypes_section;
176 static GTY(()) section *debug_str_section;
177 static GTY(()) section *debug_str_dwo_section;
178 static GTY(()) section *debug_str_offsets_section;
179 static GTY(()) section *debug_ranges_section;
180 static GTY(()) section *debug_frame_section;
182 /* Maximum size (in bytes) of an artificially generated label. */
183 #define MAX_ARTIFICIAL_LABEL_BYTES 30
185 /* According to the (draft) DWARF 3 specification, the initial length
186 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
187 bytes are 0xffffffff, followed by the length stored in the next 8
188 bytes.
190 However, the SGI/MIPS ABI uses an initial length which is equal to
191 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
193 #ifndef DWARF_INITIAL_LENGTH_SIZE
194 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
195 #endif
197 /* Round SIZE up to the nearest BOUNDARY. */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201 /* CIE identifier. */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
210 /* A vector for a table that contains frame description
211 information for each routine. */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217 struct GTY((for_user)) indirect_string_node {
218 const char *str;
219 unsigned int refcount;
220 enum dwarf_form form;
221 char *label;
222 unsigned int index;
225 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
227 typedef const char *compare_type;
229 static hashval_t hash (indirect_string_node *);
230 static bool equal (indirect_string_node *, const char *);
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235 /* With split_debug_info, both the comp_dir and dwo_name go in the
236 main object file, rather than the dwo, similar to the force_direct
237 parameter elsewhere but with additional complications:
239 1) The string is needed in both the main object file and the dwo.
240 That is, the comp_dir and dwo_name will appear in both places.
242 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
243 DW_FORM_GNU_str_index.
245 3) GCC chooses the form to use late, depending on the size and
246 reference count.
248 Rather than forcing the all debug string handling functions and
249 callers to deal with these complications, simply use a separate,
250 special-cased string table for any attribute that should go in the
251 main object file. This limits the complexity to just the places
252 that need it. */
254 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
256 static GTY(()) int dw2_string_counter;
258 /* True if the compilation unit places functions in more than one section. */
259 static GTY(()) bool have_multiple_function_sections = false;
261 /* Whether the default text and cold text sections have been used at all. */
263 static GTY(()) bool text_section_used = false;
264 static GTY(()) bool cold_text_section_used = false;
266 /* The default cold text section. */
267 static GTY(()) section *cold_text_section;
269 /* The DIE for C++14 'auto' in a function return type. */
270 static GTY(()) dw_die_ref auto_die;
272 /* The DIE for C++14 'decltype(auto)' in a function return type. */
273 static GTY(()) dw_die_ref decltype_auto_die;
275 /* Forward declarations for functions defined in this file. */
277 static char *stripattributes (const char *);
278 static void output_call_frame_info (int);
279 static void dwarf2out_note_section_used (void);
281 /* Personality decl of current unit. Used only when assembler does not support
282 personality CFI. */
283 static GTY(()) rtx current_unit_personality;
285 /* Data and reference forms for relocatable data. */
286 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
287 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
289 #ifndef DEBUG_FRAME_SECTION
290 #define DEBUG_FRAME_SECTION ".debug_frame"
291 #endif
293 #ifndef FUNC_BEGIN_LABEL
294 #define FUNC_BEGIN_LABEL "LFB"
295 #endif
297 #ifndef FUNC_END_LABEL
298 #define FUNC_END_LABEL "LFE"
299 #endif
301 #ifndef PROLOGUE_END_LABEL
302 #define PROLOGUE_END_LABEL "LPE"
303 #endif
305 #ifndef EPILOGUE_BEGIN_LABEL
306 #define EPILOGUE_BEGIN_LABEL "LEB"
307 #endif
309 #ifndef FRAME_BEGIN_LABEL
310 #define FRAME_BEGIN_LABEL "Lframe"
311 #endif
312 #define CIE_AFTER_SIZE_LABEL "LSCIE"
313 #define CIE_END_LABEL "LECIE"
314 #define FDE_LABEL "LSFDE"
315 #define FDE_AFTER_SIZE_LABEL "LASFDE"
316 #define FDE_END_LABEL "LEFDE"
317 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
318 #define LINE_NUMBER_END_LABEL "LELT"
319 #define LN_PROLOG_AS_LABEL "LASLTP"
320 #define LN_PROLOG_END_LABEL "LELTP"
321 #define DIE_LABEL_PREFIX "DW"
323 /* Match the base name of a file to the base name of a compilation unit. */
325 static int
326 matches_main_base (const char *path)
328 /* Cache the last query. */
329 static const char *last_path = NULL;
330 static int last_match = 0;
331 if (path != last_path)
333 const char *base;
334 int length = base_of_path (path, &base);
335 last_path = path;
336 last_match = (length == main_input_baselength
337 && memcmp (base, main_input_basename, length) == 0);
339 return last_match;
342 #ifdef DEBUG_DEBUG_STRUCT
344 static int
345 dump_struct_debug (tree type, enum debug_info_usage usage,
346 enum debug_struct_file criterion, int generic,
347 int matches, int result)
349 /* Find the type name. */
350 tree type_decl = TYPE_STUB_DECL (type);
351 tree t = type_decl;
352 const char *name = 0;
353 if (TREE_CODE (t) == TYPE_DECL)
354 t = DECL_NAME (t);
355 if (t)
356 name = IDENTIFIER_POINTER (t);
358 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
359 criterion,
360 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
361 matches ? "bas" : "hdr",
362 generic ? "gen" : "ord",
363 usage == DINFO_USAGE_DFN ? ";" :
364 usage == DINFO_USAGE_DIR_USE ? "." : "*",
365 result,
366 (void*) type_decl, name);
367 return result;
369 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
370 dump_struct_debug (type, usage, criterion, generic, matches, result)
372 #else
374 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
375 (result)
377 #endif
379 /* Get the number of HOST_WIDE_INTs needed to represent the precision
380 of the number. */
382 static unsigned int
383 get_full_len (const wide_int &op)
385 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
386 / HOST_BITS_PER_WIDE_INT);
389 static bool
390 should_emit_struct_debug (tree type, enum debug_info_usage usage)
392 enum debug_struct_file criterion;
393 tree type_decl;
394 bool generic = lang_hooks.types.generic_p (type);
396 if (generic)
397 criterion = debug_struct_generic[usage];
398 else
399 criterion = debug_struct_ordinary[usage];
401 if (criterion == DINFO_STRUCT_FILE_NONE)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
403 if (criterion == DINFO_STRUCT_FILE_ANY)
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
408 if (type_decl != NULL)
410 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
413 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
414 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
417 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
420 /* Return a pointer to a copy of the section string name S with all
421 attributes stripped off, and an asterisk prepended (for assemble_name). */
423 static inline char *
424 stripattributes (const char *s)
426 char *stripped = XNEWVEC (char, strlen (s) + 2);
427 char *p = stripped;
429 *p++ = '*';
431 while (*s && *s != ',')
432 *p++ = *s++;
434 *p = '\0';
435 return stripped;
438 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
439 switch to the data section instead, and write out a synthetic start label
440 for collect2 the first time around. */
442 static void
443 switch_to_eh_frame_section (bool back)
445 tree label;
447 #ifdef EH_FRAME_SECTION_NAME
448 if (eh_frame_section == 0)
450 int flags;
452 if (EH_TABLES_CAN_BE_READ_ONLY)
454 int fde_encoding;
455 int per_encoding;
456 int lsda_encoding;
458 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
459 /*global=*/0);
460 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
461 /*global=*/1);
462 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
463 /*global=*/0);
464 flags = ((! flag_pic
465 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
466 && (fde_encoding & 0x70) != DW_EH_PE_aligned
467 && (per_encoding & 0x70) != DW_EH_PE_absptr
468 && (per_encoding & 0x70) != DW_EH_PE_aligned
469 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
470 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
471 ? 0 : SECTION_WRITE);
473 else
474 flags = SECTION_WRITE;
475 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
477 #endif /* EH_FRAME_SECTION_NAME */
479 if (eh_frame_section)
480 switch_to_section (eh_frame_section);
481 else
483 /* We have no special eh_frame section. Put the information in
484 the data section and emit special labels to guide collect2. */
485 switch_to_section (data_section);
487 if (!back)
489 label = get_file_function_name ("F");
490 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
491 targetm.asm_out.globalize_label (asm_out_file,
492 IDENTIFIER_POINTER (label));
493 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
498 /* Switch [BACK] to the eh or debug frame table section, depending on
499 FOR_EH. */
501 static void
502 switch_to_frame_table_section (int for_eh, bool back)
504 if (for_eh)
505 switch_to_eh_frame_section (back);
506 else
508 if (!debug_frame_section)
509 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
510 SECTION_DEBUG, NULL);
511 switch_to_section (debug_frame_section);
515 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
517 enum dw_cfi_oprnd_type
518 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
520 switch (cfi)
522 case DW_CFA_nop:
523 case DW_CFA_GNU_window_save:
524 case DW_CFA_remember_state:
525 case DW_CFA_restore_state:
526 return dw_cfi_oprnd_unused;
528 case DW_CFA_set_loc:
529 case DW_CFA_advance_loc1:
530 case DW_CFA_advance_loc2:
531 case DW_CFA_advance_loc4:
532 case DW_CFA_MIPS_advance_loc8:
533 return dw_cfi_oprnd_addr;
535 case DW_CFA_offset:
536 case DW_CFA_offset_extended:
537 case DW_CFA_def_cfa:
538 case DW_CFA_offset_extended_sf:
539 case DW_CFA_def_cfa_sf:
540 case DW_CFA_restore:
541 case DW_CFA_restore_extended:
542 case DW_CFA_undefined:
543 case DW_CFA_same_value:
544 case DW_CFA_def_cfa_register:
545 case DW_CFA_register:
546 case DW_CFA_expression:
547 return dw_cfi_oprnd_reg_num;
549 case DW_CFA_def_cfa_offset:
550 case DW_CFA_GNU_args_size:
551 case DW_CFA_def_cfa_offset_sf:
552 return dw_cfi_oprnd_offset;
554 case DW_CFA_def_cfa_expression:
555 return dw_cfi_oprnd_loc;
557 default:
558 gcc_unreachable ();
562 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
564 enum dw_cfi_oprnd_type
565 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
567 switch (cfi)
569 case DW_CFA_def_cfa:
570 case DW_CFA_def_cfa_sf:
571 case DW_CFA_offset:
572 case DW_CFA_offset_extended_sf:
573 case DW_CFA_offset_extended:
574 return dw_cfi_oprnd_offset;
576 case DW_CFA_register:
577 return dw_cfi_oprnd_reg_num;
579 case DW_CFA_expression:
580 return dw_cfi_oprnd_loc;
582 default:
583 return dw_cfi_oprnd_unused;
587 /* Output one FDE. */
589 static void
590 output_fde (dw_fde_ref fde, bool for_eh, bool second,
591 char *section_start_label, int fde_encoding, char *augmentation,
592 bool any_lsda_needed, int lsda_encoding)
594 const char *begin, *end;
595 static unsigned int j;
596 char l1[20], l2[20];
598 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
599 /* empty */ 0);
600 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
601 for_eh + j);
602 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
603 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
604 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
605 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
606 " indicating 64-bit DWARF extension");
607 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
608 "FDE Length");
609 ASM_OUTPUT_LABEL (asm_out_file, l1);
611 if (for_eh)
612 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
613 else
614 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
615 debug_frame_section, "FDE CIE offset");
617 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
618 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
620 if (for_eh)
622 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
623 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
624 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
625 "FDE initial location");
626 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
627 end, begin, "FDE address range");
629 else
631 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
632 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
635 if (augmentation[0])
637 if (any_lsda_needed)
639 int size = size_of_encoded_value (lsda_encoding);
641 if (lsda_encoding == DW_EH_PE_aligned)
643 int offset = ( 4 /* Length */
644 + 4 /* CIE offset */
645 + 2 * size_of_encoded_value (fde_encoding)
646 + 1 /* Augmentation size */ );
647 int pad = -offset & (PTR_SIZE - 1);
649 size += pad;
650 gcc_assert (size_of_uleb128 (size) == 1);
653 dw2_asm_output_data_uleb128 (size, "Augmentation size");
655 if (fde->uses_eh_lsda)
657 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
658 fde->funcdef_number);
659 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
660 gen_rtx_SYMBOL_REF (Pmode, l1),
661 false,
662 "Language Specific Data Area");
664 else
666 if (lsda_encoding == DW_EH_PE_aligned)
667 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
668 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
669 "Language Specific Data Area (none)");
672 else
673 dw2_asm_output_data_uleb128 (0, "Augmentation size");
676 /* Loop through the Call Frame Instructions associated with this FDE. */
677 fde->dw_fde_current_label = begin;
679 size_t from, until, i;
681 from = 0;
682 until = vec_safe_length (fde->dw_fde_cfi);
684 if (fde->dw_fde_second_begin == NULL)
686 else if (!second)
687 until = fde->dw_fde_switch_cfi_index;
688 else
689 from = fde->dw_fde_switch_cfi_index;
691 for (i = from; i < until; i++)
692 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
695 /* If we are to emit a ref/link from function bodies to their frame tables,
696 do it now. This is typically performed to make sure that tables
697 associated with functions are dragged with them and not discarded in
698 garbage collecting links. We need to do this on a per function basis to
699 cope with -ffunction-sections. */
701 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
702 /* Switch to the function section, emit the ref to the tables, and
703 switch *back* into the table section. */
704 switch_to_section (function_section (fde->decl));
705 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
706 switch_to_frame_table_section (for_eh, true);
707 #endif
709 /* Pad the FDE out to an address sized boundary. */
710 ASM_OUTPUT_ALIGN (asm_out_file,
711 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
712 ASM_OUTPUT_LABEL (asm_out_file, l2);
714 j += 2;
717 /* Return true if frame description entry FDE is needed for EH. */
719 static bool
720 fde_needed_for_eh_p (dw_fde_ref fde)
722 if (flag_asynchronous_unwind_tables)
723 return true;
725 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
726 return true;
728 if (fde->uses_eh_lsda)
729 return true;
731 /* If exceptions are enabled, we have collected nothrow info. */
732 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
733 return false;
735 return true;
738 /* Output the call frame information used to record information
739 that relates to calculating the frame pointer, and records the
740 location of saved registers. */
742 static void
743 output_call_frame_info (int for_eh)
745 unsigned int i;
746 dw_fde_ref fde;
747 dw_cfi_ref cfi;
748 char l1[20], l2[20], section_start_label[20];
749 bool any_lsda_needed = false;
750 char augmentation[6];
751 int augmentation_size;
752 int fde_encoding = DW_EH_PE_absptr;
753 int per_encoding = DW_EH_PE_absptr;
754 int lsda_encoding = DW_EH_PE_absptr;
755 int return_reg;
756 rtx personality = NULL;
757 int dw_cie_version;
759 /* Don't emit a CIE if there won't be any FDEs. */
760 if (!fde_vec)
761 return;
763 /* Nothing to do if the assembler's doing it all. */
764 if (dwarf2out_do_cfi_asm ())
765 return;
767 /* If we don't have any functions we'll want to unwind out of, don't emit
768 any EH unwind information. If we make FDEs linkonce, we may have to
769 emit an empty label for an FDE that wouldn't otherwise be emitted. We
770 want to avoid having an FDE kept around when the function it refers to
771 is discarded. Example where this matters: a primary function template
772 in C++ requires EH information, an explicit specialization doesn't. */
773 if (for_eh)
775 bool any_eh_needed = false;
777 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
779 if (fde->uses_eh_lsda)
780 any_eh_needed = any_lsda_needed = true;
781 else if (fde_needed_for_eh_p (fde))
782 any_eh_needed = true;
783 else if (TARGET_USES_WEAK_UNWIND_INFO)
784 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
787 if (!any_eh_needed)
788 return;
791 /* We're going to be generating comments, so turn on app. */
792 if (flag_debug_asm)
793 app_enable ();
795 /* Switch to the proper frame section, first time. */
796 switch_to_frame_table_section (for_eh, false);
798 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
799 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
801 /* Output the CIE. */
802 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
803 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
804 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
805 dw2_asm_output_data (4, 0xffffffff,
806 "Initial length escape value indicating 64-bit DWARF extension");
807 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
808 "Length of Common Information Entry");
809 ASM_OUTPUT_LABEL (asm_out_file, l1);
811 /* Now that the CIE pointer is PC-relative for EH,
812 use 0 to identify the CIE. */
813 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
814 (for_eh ? 0 : DWARF_CIE_ID),
815 "CIE Identifier Tag");
817 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
818 use CIE version 1, unless that would produce incorrect results
819 due to overflowing the return register column. */
820 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
821 dw_cie_version = 1;
822 if (return_reg >= 256 || dwarf_version > 2)
823 dw_cie_version = 3;
824 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
826 augmentation[0] = 0;
827 augmentation_size = 0;
829 personality = current_unit_personality;
830 if (for_eh)
832 char *p;
834 /* Augmentation:
835 z Indicates that a uleb128 is present to size the
836 augmentation section.
837 L Indicates the encoding (and thus presence) of
838 an LSDA pointer in the FDE augmentation.
839 R Indicates a non-default pointer encoding for
840 FDE code pointers.
841 P Indicates the presence of an encoding + language
842 personality routine in the CIE augmentation. */
844 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
845 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
846 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
848 p = augmentation + 1;
849 if (personality)
851 *p++ = 'P';
852 augmentation_size += 1 + size_of_encoded_value (per_encoding);
853 assemble_external_libcall (personality);
855 if (any_lsda_needed)
857 *p++ = 'L';
858 augmentation_size += 1;
860 if (fde_encoding != DW_EH_PE_absptr)
862 *p++ = 'R';
863 augmentation_size += 1;
865 if (p > augmentation + 1)
867 augmentation[0] = 'z';
868 *p = '\0';
871 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
872 if (personality && per_encoding == DW_EH_PE_aligned)
874 int offset = ( 4 /* Length */
875 + 4 /* CIE Id */
876 + 1 /* CIE version */
877 + strlen (augmentation) + 1 /* Augmentation */
878 + size_of_uleb128 (1) /* Code alignment */
879 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
880 + 1 /* RA column */
881 + 1 /* Augmentation size */
882 + 1 /* Personality encoding */ );
883 int pad = -offset & (PTR_SIZE - 1);
885 augmentation_size += pad;
887 /* Augmentations should be small, so there's scarce need to
888 iterate for a solution. Die if we exceed one uleb128 byte. */
889 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
893 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
894 if (dw_cie_version >= 4)
896 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
897 dw2_asm_output_data (1, 0, "CIE Segment Size");
899 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
900 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
901 "CIE Data Alignment Factor");
903 if (dw_cie_version == 1)
904 dw2_asm_output_data (1, return_reg, "CIE RA Column");
905 else
906 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
908 if (augmentation[0])
910 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
911 if (personality)
913 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
914 eh_data_format_name (per_encoding));
915 dw2_asm_output_encoded_addr_rtx (per_encoding,
916 personality,
917 true, NULL);
920 if (any_lsda_needed)
921 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
922 eh_data_format_name (lsda_encoding));
924 if (fde_encoding != DW_EH_PE_absptr)
925 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
926 eh_data_format_name (fde_encoding));
929 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
930 output_cfi (cfi, NULL, for_eh);
932 /* Pad the CIE out to an address sized boundary. */
933 ASM_OUTPUT_ALIGN (asm_out_file,
934 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
935 ASM_OUTPUT_LABEL (asm_out_file, l2);
937 /* Loop through all of the FDE's. */
938 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
940 unsigned int k;
942 /* Don't emit EH unwind info for leaf functions that don't need it. */
943 if (for_eh && !fde_needed_for_eh_p (fde))
944 continue;
946 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
947 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
948 augmentation, any_lsda_needed, lsda_encoding);
951 if (for_eh && targetm.terminate_dw2_eh_frame_info)
952 dw2_asm_output_data (4, 0, "End of Table");
954 /* Turn off app to make assembly quicker. */
955 if (flag_debug_asm)
956 app_disable ();
959 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
961 static void
962 dwarf2out_do_cfi_startproc (bool second)
964 int enc;
965 rtx ref;
966 rtx personality = get_personality_function (current_function_decl);
968 fprintf (asm_out_file, "\t.cfi_startproc\n");
970 if (personality)
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
973 ref = personality;
975 /* ??? The GAS support isn't entirely consistent. We have to
976 handle indirect support ourselves, but PC-relative is done
977 in the assembler. Further, the assembler can't handle any
978 of the weirder relocation types. */
979 if (enc & DW_EH_PE_indirect)
980 ref = dw2_force_const_mem (ref, true);
982 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
983 output_addr_const (asm_out_file, ref);
984 fputc ('\n', asm_out_file);
987 if (crtl->uses_eh_lsda)
989 char lab[20];
991 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
992 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
993 current_function_funcdef_no);
994 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
995 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
997 if (enc & DW_EH_PE_indirect)
998 ref = dw2_force_const_mem (ref, true);
1000 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1001 output_addr_const (asm_out_file, ref);
1002 fputc ('\n', asm_out_file);
1006 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1007 this allocation may be done before pass_final. */
1009 dw_fde_ref
1010 dwarf2out_alloc_current_fde (void)
1012 dw_fde_ref fde;
1014 fde = ggc_cleared_alloc<dw_fde_node> ();
1015 fde->decl = current_function_decl;
1016 fde->funcdef_number = current_function_funcdef_no;
1017 fde->fde_index = vec_safe_length (fde_vec);
1018 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1019 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1020 fde->nothrow = crtl->nothrow;
1021 fde->drap_reg = INVALID_REGNUM;
1022 fde->vdrap_reg = INVALID_REGNUM;
1024 /* Record the FDE associated with this function. */
1025 cfun->fde = fde;
1026 vec_safe_push (fde_vec, fde);
1028 return fde;
1031 /* Output a marker (i.e. a label) for the beginning of a function, before
1032 the prologue. */
1034 void
1035 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1036 const char *file ATTRIBUTE_UNUSED)
1038 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1039 char * dup_label;
1040 dw_fde_ref fde;
1041 section *fnsec;
1042 bool do_frame;
1044 current_function_func_begin_label = NULL;
1046 do_frame = dwarf2out_do_frame ();
1048 /* ??? current_function_func_begin_label is also used by except.c for
1049 call-site information. We must emit this label if it might be used. */
1050 if (!do_frame
1051 && (!flag_exceptions
1052 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1053 return;
1055 fnsec = function_section (current_function_decl);
1056 switch_to_section (fnsec);
1057 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1060 current_function_funcdef_no);
1061 dup_label = xstrdup (label);
1062 current_function_func_begin_label = dup_label;
1064 /* We can elide the fde allocation if we're not emitting debug info. */
1065 if (!do_frame)
1066 return;
1068 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1069 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1070 would include pass_dwarf2_frame. If we've not created the FDE yet,
1071 do so now. */
1072 fde = cfun->fde;
1073 if (fde == NULL)
1074 fde = dwarf2out_alloc_current_fde ();
1076 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1077 fde->dw_fde_begin = dup_label;
1078 fde->dw_fde_current_label = dup_label;
1079 fde->in_std_section = (fnsec == text_section
1080 || (cold_text_section && fnsec == cold_text_section));
1082 /* We only want to output line number information for the genuine dwarf2
1083 prologue case, not the eh frame case. */
1084 #ifdef DWARF2_DEBUGGING_INFO
1085 if (file)
1086 dwarf2out_source_line (line, file, 0, true);
1087 #endif
1089 if (dwarf2out_do_cfi_asm ())
1090 dwarf2out_do_cfi_startproc (false);
1091 else
1093 rtx personality = get_personality_function (current_function_decl);
1094 if (!current_unit_personality)
1095 current_unit_personality = personality;
1097 /* We cannot keep a current personality per function as without CFI
1098 asm, at the point where we emit the CFI data, there is no current
1099 function anymore. */
1100 if (personality && current_unit_personality != personality)
1101 sorry ("multiple EH personalities are supported only with assemblers "
1102 "supporting .cfi_personality directive");
1106 /* Output a marker (i.e. a label) for the end of the generated code
1107 for a function prologue. This gets called *after* the prologue code has
1108 been generated. */
1110 void
1111 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1112 const char *file ATTRIBUTE_UNUSED)
1114 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1116 /* Output a label to mark the endpoint of the code generated for this
1117 function. */
1118 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1121 current_function_funcdef_no);
1122 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1125 /* Output a marker (i.e. a label) for the beginning of the generated code
1126 for a function epilogue. This gets called *before* the prologue code has
1127 been generated. */
1129 void
1130 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1131 const char *file ATTRIBUTE_UNUSED)
1133 dw_fde_ref fde = cfun->fde;
1134 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1136 if (fde->dw_fde_vms_begin_epilogue)
1137 return;
1139 /* Output a label to mark the endpoint of the code generated for this
1140 function. */
1141 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1144 current_function_funcdef_no);
1145 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1148 /* Output a marker (i.e. a label) for the absolute end of the generated code
1149 for a function definition. This gets called *after* the epilogue code has
1150 been generated. */
1152 void
1153 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1154 const char *file ATTRIBUTE_UNUSED)
1156 dw_fde_ref fde;
1157 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1159 last_var_location_insn = NULL;
1160 cached_next_real_insn = NULL;
1162 if (dwarf2out_do_cfi_asm ())
1163 fprintf (asm_out_file, "\t.cfi_endproc\n");
1165 /* Output a label to mark the endpoint of the code generated for this
1166 function. */
1167 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1168 current_function_funcdef_no);
1169 ASM_OUTPUT_LABEL (asm_out_file, label);
1170 fde = cfun->fde;
1171 gcc_assert (fde != NULL);
1172 if (fde->dw_fde_second_begin == NULL)
1173 fde->dw_fde_end = xstrdup (label);
1176 void
1177 dwarf2out_frame_finish (void)
1179 /* Output call frame information. */
1180 if (targetm.debug_unwind_info () == UI_DWARF2)
1181 output_call_frame_info (0);
1183 /* Output another copy for the unwinder. */
1184 if ((flag_unwind_tables || flag_exceptions)
1185 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1186 output_call_frame_info (1);
1189 /* Note that the current function section is being used for code. */
1191 static void
1192 dwarf2out_note_section_used (void)
1194 section *sec = current_function_section ();
1195 if (sec == text_section)
1196 text_section_used = true;
1197 else if (sec == cold_text_section)
1198 cold_text_section_used = true;
1201 static void var_location_switch_text_section (void);
1202 static void set_cur_line_info_table (section *);
1204 void
1205 dwarf2out_switch_text_section (void)
1207 section *sect;
1208 dw_fde_ref fde = cfun->fde;
1210 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1212 if (!in_cold_section_p)
1214 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1215 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1216 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1218 else
1220 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1221 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1222 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1224 have_multiple_function_sections = true;
1226 /* There is no need to mark used sections when not debugging. */
1227 if (cold_text_section != NULL)
1228 dwarf2out_note_section_used ();
1230 if (dwarf2out_do_cfi_asm ())
1231 fprintf (asm_out_file, "\t.cfi_endproc\n");
1233 /* Now do the real section switch. */
1234 sect = current_function_section ();
1235 switch_to_section (sect);
1237 fde->second_in_std_section
1238 = (sect == text_section
1239 || (cold_text_section && sect == cold_text_section));
1241 if (dwarf2out_do_cfi_asm ())
1242 dwarf2out_do_cfi_startproc (true);
1244 var_location_switch_text_section ();
1246 if (cold_text_section != NULL)
1247 set_cur_line_info_table (sect);
1250 /* And now, the subset of the debugging information support code necessary
1251 for emitting location expressions. */
1253 /* Data about a single source file. */
1254 struct GTY((for_user)) dwarf_file_data {
1255 const char * filename;
1256 int emitted_number;
1259 /* Describe an entry into the .debug_addr section. */
1261 enum ate_kind {
1262 ate_kind_rtx,
1263 ate_kind_rtx_dtprel,
1264 ate_kind_label
1267 typedef struct GTY((for_user)) addr_table_entry_struct {
1268 enum ate_kind kind;
1269 unsigned int refcount;
1270 unsigned int index;
1271 union addr_table_entry_struct_union
1273 rtx GTY ((tag ("0"))) rtl;
1274 char * GTY ((tag ("1"))) label;
1276 GTY ((desc ("%1.kind"))) addr;
1278 addr_table_entry;
1280 /* Location lists are ranges + location descriptions for that range,
1281 so you can track variables that are in different places over
1282 their entire life. */
1283 typedef struct GTY(()) dw_loc_list_struct {
1284 dw_loc_list_ref dw_loc_next;
1285 const char *begin; /* Label and addr_entry for start of range */
1286 addr_table_entry *begin_entry;
1287 const char *end; /* Label for end of range */
1288 char *ll_symbol; /* Label for beginning of location list.
1289 Only on head of list */
1290 const char *section; /* Section this loclist is relative to */
1291 dw_loc_descr_ref expr;
1292 hashval_t hash;
1293 /* True if all addresses in this and subsequent lists are known to be
1294 resolved. */
1295 bool resolved_addr;
1296 /* True if this list has been replaced by dw_loc_next. */
1297 bool replaced;
1298 bool emitted;
1299 /* True if the range should be emitted even if begin and end
1300 are the same. */
1301 bool force;
1302 } dw_loc_list_node;
1304 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1306 /* Convert a DWARF stack opcode into its string name. */
1308 static const char *
1309 dwarf_stack_op_name (unsigned int op)
1311 const char *name = get_DW_OP_name (op);
1313 if (name != NULL)
1314 return name;
1316 return "OP_<unknown>";
1319 /* Return a pointer to a newly allocated location description. Location
1320 descriptions are simple expression terms that can be strung
1321 together to form more complicated location (address) descriptions. */
1323 static inline dw_loc_descr_ref
1324 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1325 unsigned HOST_WIDE_INT oprnd2)
1327 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1329 descr->dw_loc_opc = op;
1330 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1331 descr->dw_loc_oprnd1.val_entry = NULL;
1332 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1333 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1334 descr->dw_loc_oprnd2.val_entry = NULL;
1335 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1337 return descr;
1340 /* Return a pointer to a newly allocated location description for
1341 REG and OFFSET. */
1343 static inline dw_loc_descr_ref
1344 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1346 if (reg <= 31)
1347 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1348 offset, 0);
1349 else
1350 return new_loc_descr (DW_OP_bregx, reg, offset);
1353 /* Add a location description term to a location description expression. */
1355 static inline void
1356 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1358 dw_loc_descr_ref *d;
1360 /* Find the end of the chain. */
1361 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1364 *d = descr;
1367 /* Compare two location operands for exact equality. */
1369 static bool
1370 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1372 if (a->val_class != b->val_class)
1373 return false;
1374 switch (a->val_class)
1376 case dw_val_class_none:
1377 return true;
1378 case dw_val_class_addr:
1379 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1381 case dw_val_class_offset:
1382 case dw_val_class_unsigned_const:
1383 case dw_val_class_const:
1384 case dw_val_class_range_list:
1385 case dw_val_class_lineptr:
1386 case dw_val_class_macptr:
1387 /* These are all HOST_WIDE_INT, signed or unsigned. */
1388 return a->v.val_unsigned == b->v.val_unsigned;
1390 case dw_val_class_loc:
1391 return a->v.val_loc == b->v.val_loc;
1392 case dw_val_class_loc_list:
1393 return a->v.val_loc_list == b->v.val_loc_list;
1394 case dw_val_class_die_ref:
1395 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1396 case dw_val_class_fde_ref:
1397 return a->v.val_fde_index == b->v.val_fde_index;
1398 case dw_val_class_lbl_id:
1399 case dw_val_class_high_pc:
1400 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1401 case dw_val_class_str:
1402 return a->v.val_str == b->v.val_str;
1403 case dw_val_class_flag:
1404 return a->v.val_flag == b->v.val_flag;
1405 case dw_val_class_file:
1406 return a->v.val_file == b->v.val_file;
1407 case dw_val_class_decl_ref:
1408 return a->v.val_decl_ref == b->v.val_decl_ref;
1410 case dw_val_class_const_double:
1411 return (a->v.val_double.high == b->v.val_double.high
1412 && a->v.val_double.low == b->v.val_double.low);
1414 case dw_val_class_wide_int:
1415 return *a->v.val_wide == *b->v.val_wide;
1417 case dw_val_class_vec:
1419 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1420 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1422 return (a_len == b_len
1423 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1426 case dw_val_class_data8:
1427 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1429 case dw_val_class_vms_delta:
1430 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1431 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1433 gcc_unreachable ();
1436 /* Compare two location atoms for exact equality. */
1438 static bool
1439 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1441 if (a->dw_loc_opc != b->dw_loc_opc)
1442 return false;
1444 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1445 address size, but since we always allocate cleared storage it
1446 should be zero for other types of locations. */
1447 if (a->dtprel != b->dtprel)
1448 return false;
1450 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1451 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1454 /* Compare two complete location expressions for exact equality. */
1456 bool
1457 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1459 while (1)
1461 if (a == b)
1462 return true;
1463 if (a == NULL || b == NULL)
1464 return false;
1465 if (!loc_descr_equal_p_1 (a, b))
1466 return false;
1468 a = a->dw_loc_next;
1469 b = b->dw_loc_next;
1474 /* Add a constant OFFSET to a location expression. */
1476 static void
1477 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1479 dw_loc_descr_ref loc;
1480 HOST_WIDE_INT *p;
1482 gcc_assert (*list_head != NULL);
1484 if (!offset)
1485 return;
1487 /* Find the end of the chain. */
1488 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1491 p = NULL;
1492 if (loc->dw_loc_opc == DW_OP_fbreg
1493 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1494 p = &loc->dw_loc_oprnd1.v.val_int;
1495 else if (loc->dw_loc_opc == DW_OP_bregx)
1496 p = &loc->dw_loc_oprnd2.v.val_int;
1498 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1499 offset. Don't optimize if an signed integer overflow would happen. */
1500 if (p != NULL
1501 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1502 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1503 *p += offset;
1505 else if (offset > 0)
1506 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1508 else
1510 loc->dw_loc_next = int_loc_descriptor (-offset);
1511 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1515 /* Add a constant OFFSET to a location list. */
1517 static void
1518 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1520 dw_loc_list_ref d;
1521 for (d = list_head; d != NULL; d = d->dw_loc_next)
1522 loc_descr_plus_const (&d->expr, offset);
1525 #define DWARF_REF_SIZE \
1526 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1528 static unsigned long int get_base_type_offset (dw_die_ref);
1530 /* Return the size of a location descriptor. */
1532 static unsigned long
1533 size_of_loc_descr (dw_loc_descr_ref loc)
1535 unsigned long size = 1;
1537 switch (loc->dw_loc_opc)
1539 case DW_OP_addr:
1540 size += DWARF2_ADDR_SIZE;
1541 break;
1542 case DW_OP_GNU_addr_index:
1543 case DW_OP_GNU_const_index:
1544 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1545 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1546 break;
1547 case DW_OP_const1u:
1548 case DW_OP_const1s:
1549 size += 1;
1550 break;
1551 case DW_OP_const2u:
1552 case DW_OP_const2s:
1553 size += 2;
1554 break;
1555 case DW_OP_const4u:
1556 case DW_OP_const4s:
1557 size += 4;
1558 break;
1559 case DW_OP_const8u:
1560 case DW_OP_const8s:
1561 size += 8;
1562 break;
1563 case DW_OP_constu:
1564 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565 break;
1566 case DW_OP_consts:
1567 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1568 break;
1569 case DW_OP_pick:
1570 size += 1;
1571 break;
1572 case DW_OP_plus_uconst:
1573 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1574 break;
1575 case DW_OP_skip:
1576 case DW_OP_bra:
1577 size += 2;
1578 break;
1579 case DW_OP_breg0:
1580 case DW_OP_breg1:
1581 case DW_OP_breg2:
1582 case DW_OP_breg3:
1583 case DW_OP_breg4:
1584 case DW_OP_breg5:
1585 case DW_OP_breg6:
1586 case DW_OP_breg7:
1587 case DW_OP_breg8:
1588 case DW_OP_breg9:
1589 case DW_OP_breg10:
1590 case DW_OP_breg11:
1591 case DW_OP_breg12:
1592 case DW_OP_breg13:
1593 case DW_OP_breg14:
1594 case DW_OP_breg15:
1595 case DW_OP_breg16:
1596 case DW_OP_breg17:
1597 case DW_OP_breg18:
1598 case DW_OP_breg19:
1599 case DW_OP_breg20:
1600 case DW_OP_breg21:
1601 case DW_OP_breg22:
1602 case DW_OP_breg23:
1603 case DW_OP_breg24:
1604 case DW_OP_breg25:
1605 case DW_OP_breg26:
1606 case DW_OP_breg27:
1607 case DW_OP_breg28:
1608 case DW_OP_breg29:
1609 case DW_OP_breg30:
1610 case DW_OP_breg31:
1611 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1612 break;
1613 case DW_OP_regx:
1614 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1615 break;
1616 case DW_OP_fbreg:
1617 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1618 break;
1619 case DW_OP_bregx:
1620 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1622 break;
1623 case DW_OP_piece:
1624 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1625 break;
1626 case DW_OP_bit_piece:
1627 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1628 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1629 break;
1630 case DW_OP_deref_size:
1631 case DW_OP_xderef_size:
1632 size += 1;
1633 break;
1634 case DW_OP_call2:
1635 size += 2;
1636 break;
1637 case DW_OP_call4:
1638 size += 4;
1639 break;
1640 case DW_OP_call_ref:
1641 size += DWARF_REF_SIZE;
1642 break;
1643 case DW_OP_implicit_value:
1644 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1645 + loc->dw_loc_oprnd1.v.val_unsigned;
1646 break;
1647 case DW_OP_GNU_implicit_pointer:
1648 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1649 break;
1650 case DW_OP_GNU_entry_value:
1652 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1653 size += size_of_uleb128 (op_size) + op_size;
1654 break;
1656 case DW_OP_GNU_const_type:
1658 unsigned long o
1659 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1660 size += size_of_uleb128 (o) + 1;
1661 switch (loc->dw_loc_oprnd2.val_class)
1663 case dw_val_class_vec:
1664 size += loc->dw_loc_oprnd2.v.val_vec.length
1665 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1666 break;
1667 case dw_val_class_const:
1668 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1669 break;
1670 case dw_val_class_const_double:
1671 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1672 break;
1673 case dw_val_class_wide_int:
1674 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1675 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1676 break;
1677 default:
1678 gcc_unreachable ();
1680 break;
1682 case DW_OP_GNU_regval_type:
1684 unsigned long o
1685 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1686 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1687 + size_of_uleb128 (o);
1689 break;
1690 case DW_OP_GNU_deref_type:
1692 unsigned long o
1693 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694 size += 1 + size_of_uleb128 (o);
1696 break;
1697 case DW_OP_GNU_convert:
1698 case DW_OP_GNU_reinterpret:
1699 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1700 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1701 else
1703 unsigned long o
1704 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1705 size += size_of_uleb128 (o);
1707 break;
1708 case DW_OP_GNU_parameter_ref:
1709 size += 4;
1710 break;
1711 default:
1712 break;
1715 return size;
1718 /* Return the size of a series of location descriptors. */
1720 unsigned long
1721 size_of_locs (dw_loc_descr_ref loc)
1723 dw_loc_descr_ref l;
1724 unsigned long size;
1726 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1727 field, to avoid writing to a PCH file. */
1728 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1730 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1731 break;
1732 size += size_of_loc_descr (l);
1734 if (! l)
1735 return size;
1737 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1739 l->dw_loc_addr = size;
1740 size += size_of_loc_descr (l);
1743 return size;
1746 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1747 static void get_ref_die_offset_label (char *, dw_die_ref);
1748 static unsigned long int get_ref_die_offset (dw_die_ref);
1750 /* Output location description stack opcode's operands (if any).
1751 The for_eh_or_skip parameter controls whether register numbers are
1752 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1753 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1754 info). This should be suppressed for the cases that have not been converted
1755 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1757 static void
1758 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1760 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1761 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1763 switch (loc->dw_loc_opc)
1765 #ifdef DWARF2_DEBUGGING_INFO
1766 case DW_OP_const2u:
1767 case DW_OP_const2s:
1768 dw2_asm_output_data (2, val1->v.val_int, NULL);
1769 break;
1770 case DW_OP_const4u:
1771 if (loc->dtprel)
1773 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1774 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1775 val1->v.val_addr);
1776 fputc ('\n', asm_out_file);
1777 break;
1779 /* FALLTHRU */
1780 case DW_OP_const4s:
1781 dw2_asm_output_data (4, val1->v.val_int, NULL);
1782 break;
1783 case DW_OP_const8u:
1784 if (loc->dtprel)
1786 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1787 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1788 val1->v.val_addr);
1789 fputc ('\n', asm_out_file);
1790 break;
1792 /* FALLTHRU */
1793 case DW_OP_const8s:
1794 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1795 dw2_asm_output_data (8, val1->v.val_int, NULL);
1796 break;
1797 case DW_OP_skip:
1798 case DW_OP_bra:
1800 int offset;
1802 gcc_assert (val1->val_class == dw_val_class_loc);
1803 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1805 dw2_asm_output_data (2, offset, NULL);
1807 break;
1808 case DW_OP_implicit_value:
1809 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1810 switch (val2->val_class)
1812 case dw_val_class_const:
1813 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1814 break;
1815 case dw_val_class_vec:
1817 unsigned int elt_size = val2->v.val_vec.elt_size;
1818 unsigned int len = val2->v.val_vec.length;
1819 unsigned int i;
1820 unsigned char *p;
1822 if (elt_size > sizeof (HOST_WIDE_INT))
1824 elt_size /= 2;
1825 len *= 2;
1827 for (i = 0, p = val2->v.val_vec.array;
1828 i < len;
1829 i++, p += elt_size)
1830 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1831 "fp or vector constant word %u", i);
1833 break;
1834 case dw_val_class_const_double:
1836 unsigned HOST_WIDE_INT first, second;
1838 if (WORDS_BIG_ENDIAN)
1840 first = val2->v.val_double.high;
1841 second = val2->v.val_double.low;
1843 else
1845 first = val2->v.val_double.low;
1846 second = val2->v.val_double.high;
1848 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1849 first, NULL);
1850 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1851 second, NULL);
1853 break;
1854 case dw_val_class_wide_int:
1856 int i;
1857 int len = get_full_len (*val2->v.val_wide);
1858 if (WORDS_BIG_ENDIAN)
1859 for (i = len - 1; i >= 0; --i)
1860 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1861 val2->v.val_wide->elt (i), NULL);
1862 else
1863 for (i = 0; i < len; ++i)
1864 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1865 val2->v.val_wide->elt (i), NULL);
1867 break;
1868 case dw_val_class_addr:
1869 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1870 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1871 break;
1872 default:
1873 gcc_unreachable ();
1875 break;
1876 #else
1877 case DW_OP_const2u:
1878 case DW_OP_const2s:
1879 case DW_OP_const4u:
1880 case DW_OP_const4s:
1881 case DW_OP_const8u:
1882 case DW_OP_const8s:
1883 case DW_OP_skip:
1884 case DW_OP_bra:
1885 case DW_OP_implicit_value:
1886 /* We currently don't make any attempt to make sure these are
1887 aligned properly like we do for the main unwind info, so
1888 don't support emitting things larger than a byte if we're
1889 only doing unwinding. */
1890 gcc_unreachable ();
1891 #endif
1892 case DW_OP_const1u:
1893 case DW_OP_const1s:
1894 dw2_asm_output_data (1, val1->v.val_int, NULL);
1895 break;
1896 case DW_OP_constu:
1897 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1898 break;
1899 case DW_OP_consts:
1900 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1901 break;
1902 case DW_OP_pick:
1903 dw2_asm_output_data (1, val1->v.val_int, NULL);
1904 break;
1905 case DW_OP_plus_uconst:
1906 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1907 break;
1908 case DW_OP_breg0:
1909 case DW_OP_breg1:
1910 case DW_OP_breg2:
1911 case DW_OP_breg3:
1912 case DW_OP_breg4:
1913 case DW_OP_breg5:
1914 case DW_OP_breg6:
1915 case DW_OP_breg7:
1916 case DW_OP_breg8:
1917 case DW_OP_breg9:
1918 case DW_OP_breg10:
1919 case DW_OP_breg11:
1920 case DW_OP_breg12:
1921 case DW_OP_breg13:
1922 case DW_OP_breg14:
1923 case DW_OP_breg15:
1924 case DW_OP_breg16:
1925 case DW_OP_breg17:
1926 case DW_OP_breg18:
1927 case DW_OP_breg19:
1928 case DW_OP_breg20:
1929 case DW_OP_breg21:
1930 case DW_OP_breg22:
1931 case DW_OP_breg23:
1932 case DW_OP_breg24:
1933 case DW_OP_breg25:
1934 case DW_OP_breg26:
1935 case DW_OP_breg27:
1936 case DW_OP_breg28:
1937 case DW_OP_breg29:
1938 case DW_OP_breg30:
1939 case DW_OP_breg31:
1940 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1941 break;
1942 case DW_OP_regx:
1944 unsigned r = val1->v.val_unsigned;
1945 if (for_eh_or_skip >= 0)
1946 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1947 gcc_assert (size_of_uleb128 (r)
1948 == size_of_uleb128 (val1->v.val_unsigned));
1949 dw2_asm_output_data_uleb128 (r, NULL);
1951 break;
1952 case DW_OP_fbreg:
1953 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1954 break;
1955 case DW_OP_bregx:
1957 unsigned r = val1->v.val_unsigned;
1958 if (for_eh_or_skip >= 0)
1959 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1960 gcc_assert (size_of_uleb128 (r)
1961 == size_of_uleb128 (val1->v.val_unsigned));
1962 dw2_asm_output_data_uleb128 (r, NULL);
1963 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1965 break;
1966 case DW_OP_piece:
1967 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1968 break;
1969 case DW_OP_bit_piece:
1970 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1971 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1972 break;
1973 case DW_OP_deref_size:
1974 case DW_OP_xderef_size:
1975 dw2_asm_output_data (1, val1->v.val_int, NULL);
1976 break;
1978 case DW_OP_addr:
1979 if (loc->dtprel)
1981 if (targetm.asm_out.output_dwarf_dtprel)
1983 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1984 DWARF2_ADDR_SIZE,
1985 val1->v.val_addr);
1986 fputc ('\n', asm_out_file);
1988 else
1989 gcc_unreachable ();
1991 else
1993 #ifdef DWARF2_DEBUGGING_INFO
1994 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1995 #else
1996 gcc_unreachable ();
1997 #endif
1999 break;
2001 case DW_OP_GNU_addr_index:
2002 case DW_OP_GNU_const_index:
2003 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2004 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2005 "(index into .debug_addr)");
2006 break;
2008 case DW_OP_GNU_implicit_pointer:
2010 char label[MAX_ARTIFICIAL_LABEL_BYTES
2011 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2012 gcc_assert (val1->val_class == dw_val_class_die_ref);
2013 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2014 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2015 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2017 break;
2019 case DW_OP_GNU_entry_value:
2020 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2021 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2022 break;
2024 case DW_OP_GNU_const_type:
2026 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2027 gcc_assert (o);
2028 dw2_asm_output_data_uleb128 (o, NULL);
2029 switch (val2->val_class)
2031 case dw_val_class_const:
2032 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2033 dw2_asm_output_data (1, l, NULL);
2034 dw2_asm_output_data (l, val2->v.val_int, NULL);
2035 break;
2036 case dw_val_class_vec:
2038 unsigned int elt_size = val2->v.val_vec.elt_size;
2039 unsigned int len = val2->v.val_vec.length;
2040 unsigned int i;
2041 unsigned char *p;
2043 l = len * elt_size;
2044 dw2_asm_output_data (1, l, NULL);
2045 if (elt_size > sizeof (HOST_WIDE_INT))
2047 elt_size /= 2;
2048 len *= 2;
2050 for (i = 0, p = val2->v.val_vec.array;
2051 i < len;
2052 i++, p += elt_size)
2053 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2054 "fp or vector constant word %u", i);
2056 break;
2057 case dw_val_class_const_double:
2059 unsigned HOST_WIDE_INT first, second;
2060 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2062 dw2_asm_output_data (1, 2 * l, NULL);
2063 if (WORDS_BIG_ENDIAN)
2065 first = val2->v.val_double.high;
2066 second = val2->v.val_double.low;
2068 else
2070 first = val2->v.val_double.low;
2071 second = val2->v.val_double.high;
2073 dw2_asm_output_data (l, first, NULL);
2074 dw2_asm_output_data (l, second, NULL);
2076 break;
2077 case dw_val_class_wide_int:
2079 int i;
2080 int len = get_full_len (*val2->v.val_wide);
2081 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2083 dw2_asm_output_data (1, len * l, NULL);
2084 if (WORDS_BIG_ENDIAN)
2085 for (i = len - 1; i >= 0; --i)
2086 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2087 else
2088 for (i = 0; i < len; ++i)
2089 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2091 break;
2092 default:
2093 gcc_unreachable ();
2096 break;
2097 case DW_OP_GNU_regval_type:
2099 unsigned r = val1->v.val_unsigned;
2100 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2101 gcc_assert (o);
2102 if (for_eh_or_skip >= 0)
2104 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2105 gcc_assert (size_of_uleb128 (r)
2106 == size_of_uleb128 (val1->v.val_unsigned));
2108 dw2_asm_output_data_uleb128 (r, NULL);
2109 dw2_asm_output_data_uleb128 (o, NULL);
2111 break;
2112 case DW_OP_GNU_deref_type:
2114 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2115 gcc_assert (o);
2116 dw2_asm_output_data (1, val1->v.val_int, NULL);
2117 dw2_asm_output_data_uleb128 (o, NULL);
2119 break;
2120 case DW_OP_GNU_convert:
2121 case DW_OP_GNU_reinterpret:
2122 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2123 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2124 else
2126 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2127 gcc_assert (o);
2128 dw2_asm_output_data_uleb128 (o, NULL);
2130 break;
2132 case DW_OP_GNU_parameter_ref:
2134 unsigned long o;
2135 gcc_assert (val1->val_class == dw_val_class_die_ref);
2136 o = get_ref_die_offset (val1->v.val_die_ref.die);
2137 dw2_asm_output_data (4, o, NULL);
2139 break;
2141 default:
2142 /* Other codes have no operands. */
2143 break;
2147 /* Output a sequence of location operations.
2148 The for_eh_or_skip parameter controls whether register numbers are
2149 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2150 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2151 info). This should be suppressed for the cases that have not been converted
2152 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2154 void
2155 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2157 for (; loc != NULL; loc = loc->dw_loc_next)
2159 enum dwarf_location_atom opc = loc->dw_loc_opc;
2160 /* Output the opcode. */
2161 if (for_eh_or_skip >= 0
2162 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2164 unsigned r = (opc - DW_OP_breg0);
2165 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2166 gcc_assert (r <= 31);
2167 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2169 else if (for_eh_or_skip >= 0
2170 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2172 unsigned r = (opc - DW_OP_reg0);
2173 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2174 gcc_assert (r <= 31);
2175 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2178 dw2_asm_output_data (1, opc,
2179 "%s", dwarf_stack_op_name (opc));
2181 /* Output the operand(s) (if any). */
2182 output_loc_operands (loc, for_eh_or_skip);
2186 /* Output location description stack opcode's operands (if any).
2187 The output is single bytes on a line, suitable for .cfi_escape. */
2189 static void
2190 output_loc_operands_raw (dw_loc_descr_ref loc)
2192 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2193 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2195 switch (loc->dw_loc_opc)
2197 case DW_OP_addr:
2198 case DW_OP_GNU_addr_index:
2199 case DW_OP_GNU_const_index:
2200 case DW_OP_implicit_value:
2201 /* We cannot output addresses in .cfi_escape, only bytes. */
2202 gcc_unreachable ();
2204 case DW_OP_const1u:
2205 case DW_OP_const1s:
2206 case DW_OP_pick:
2207 case DW_OP_deref_size:
2208 case DW_OP_xderef_size:
2209 fputc (',', asm_out_file);
2210 dw2_asm_output_data_raw (1, val1->v.val_int);
2211 break;
2213 case DW_OP_const2u:
2214 case DW_OP_const2s:
2215 fputc (',', asm_out_file);
2216 dw2_asm_output_data_raw (2, val1->v.val_int);
2217 break;
2219 case DW_OP_const4u:
2220 case DW_OP_const4s:
2221 fputc (',', asm_out_file);
2222 dw2_asm_output_data_raw (4, val1->v.val_int);
2223 break;
2225 case DW_OP_const8u:
2226 case DW_OP_const8s:
2227 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2228 fputc (',', asm_out_file);
2229 dw2_asm_output_data_raw (8, val1->v.val_int);
2230 break;
2232 case DW_OP_skip:
2233 case DW_OP_bra:
2235 int offset;
2237 gcc_assert (val1->val_class == dw_val_class_loc);
2238 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2240 fputc (',', asm_out_file);
2241 dw2_asm_output_data_raw (2, offset);
2243 break;
2245 case DW_OP_regx:
2247 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2248 gcc_assert (size_of_uleb128 (r)
2249 == size_of_uleb128 (val1->v.val_unsigned));
2250 fputc (',', asm_out_file);
2251 dw2_asm_output_data_uleb128_raw (r);
2253 break;
2255 case DW_OP_constu:
2256 case DW_OP_plus_uconst:
2257 case DW_OP_piece:
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2260 break;
2262 case DW_OP_bit_piece:
2263 fputc (',', asm_out_file);
2264 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2265 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2266 break;
2268 case DW_OP_consts:
2269 case DW_OP_breg0:
2270 case DW_OP_breg1:
2271 case DW_OP_breg2:
2272 case DW_OP_breg3:
2273 case DW_OP_breg4:
2274 case DW_OP_breg5:
2275 case DW_OP_breg6:
2276 case DW_OP_breg7:
2277 case DW_OP_breg8:
2278 case DW_OP_breg9:
2279 case DW_OP_breg10:
2280 case DW_OP_breg11:
2281 case DW_OP_breg12:
2282 case DW_OP_breg13:
2283 case DW_OP_breg14:
2284 case DW_OP_breg15:
2285 case DW_OP_breg16:
2286 case DW_OP_breg17:
2287 case DW_OP_breg18:
2288 case DW_OP_breg19:
2289 case DW_OP_breg20:
2290 case DW_OP_breg21:
2291 case DW_OP_breg22:
2292 case DW_OP_breg23:
2293 case DW_OP_breg24:
2294 case DW_OP_breg25:
2295 case DW_OP_breg26:
2296 case DW_OP_breg27:
2297 case DW_OP_breg28:
2298 case DW_OP_breg29:
2299 case DW_OP_breg30:
2300 case DW_OP_breg31:
2301 case DW_OP_fbreg:
2302 fputc (',', asm_out_file);
2303 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2304 break;
2306 case DW_OP_bregx:
2308 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2309 gcc_assert (size_of_uleb128 (r)
2310 == size_of_uleb128 (val1->v.val_unsigned));
2311 fputc (',', asm_out_file);
2312 dw2_asm_output_data_uleb128_raw (r);
2313 fputc (',', asm_out_file);
2314 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2316 break;
2318 case DW_OP_GNU_implicit_pointer:
2319 case DW_OP_GNU_entry_value:
2320 case DW_OP_GNU_const_type:
2321 case DW_OP_GNU_regval_type:
2322 case DW_OP_GNU_deref_type:
2323 case DW_OP_GNU_convert:
2324 case DW_OP_GNU_reinterpret:
2325 case DW_OP_GNU_parameter_ref:
2326 gcc_unreachable ();
2327 break;
2329 default:
2330 /* Other codes have no operands. */
2331 break;
2335 void
2336 output_loc_sequence_raw (dw_loc_descr_ref loc)
2338 while (1)
2340 enum dwarf_location_atom opc = loc->dw_loc_opc;
2341 /* Output the opcode. */
2342 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2344 unsigned r = (opc - DW_OP_breg0);
2345 r = DWARF2_FRAME_REG_OUT (r, 1);
2346 gcc_assert (r <= 31);
2347 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2349 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2351 unsigned r = (opc - DW_OP_reg0);
2352 r = DWARF2_FRAME_REG_OUT (r, 1);
2353 gcc_assert (r <= 31);
2354 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2356 /* Output the opcode. */
2357 fprintf (asm_out_file, "%#x", opc);
2358 output_loc_operands_raw (loc);
2360 if (!loc->dw_loc_next)
2361 break;
2362 loc = loc->dw_loc_next;
2364 fputc (',', asm_out_file);
2368 /* This function builds a dwarf location descriptor sequence from a
2369 dw_cfa_location, adding the given OFFSET to the result of the
2370 expression. */
2372 struct dw_loc_descr_node *
2373 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2375 struct dw_loc_descr_node *head, *tmp;
2377 offset += cfa->offset;
2379 if (cfa->indirect)
2381 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2382 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2383 head->dw_loc_oprnd1.val_entry = NULL;
2384 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2385 add_loc_descr (&head, tmp);
2386 if (offset != 0)
2388 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2389 add_loc_descr (&head, tmp);
2392 else
2393 head = new_reg_loc_descr (cfa->reg, offset);
2395 return head;
2398 /* This function builds a dwarf location descriptor sequence for
2399 the address at OFFSET from the CFA when stack is aligned to
2400 ALIGNMENT byte. */
2402 struct dw_loc_descr_node *
2403 build_cfa_aligned_loc (dw_cfa_location *cfa,
2404 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2406 struct dw_loc_descr_node *head;
2407 unsigned int dwarf_fp
2408 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2410 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2411 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2413 head = new_reg_loc_descr (dwarf_fp, 0);
2414 add_loc_descr (&head, int_loc_descriptor (alignment));
2415 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2416 loc_descr_plus_const (&head, offset);
2418 else
2419 head = new_reg_loc_descr (dwarf_fp, offset);
2420 return head;
2423 /* And now, the support for symbolic debugging information. */
2425 /* .debug_str support. */
2427 static void dwarf2out_init (const char *);
2428 static void dwarf2out_finish (const char *);
2429 static void dwarf2out_early_finish (void);
2430 static void dwarf2out_assembly_start (void);
2431 static void dwarf2out_define (unsigned int, const char *);
2432 static void dwarf2out_undef (unsigned int, const char *);
2433 static void dwarf2out_start_source_file (unsigned, const char *);
2434 static void dwarf2out_end_source_file (unsigned);
2435 static void dwarf2out_function_decl (tree);
2436 static void dwarf2out_begin_block (unsigned, unsigned);
2437 static void dwarf2out_end_block (unsigned, unsigned);
2438 static bool dwarf2out_ignore_block (const_tree);
2439 static void dwarf2out_early_global_decl (tree);
2440 static void dwarf2out_late_global_decl (tree);
2441 static void dwarf2out_type_decl (tree, int);
2442 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2443 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2444 dw_die_ref);
2445 static void dwarf2out_abstract_function (tree);
2446 static void dwarf2out_var_location (rtx_insn *);
2447 static void dwarf2out_begin_function (tree);
2448 static void dwarf2out_end_function (unsigned int);
2449 static void dwarf2out_register_main_translation_unit (tree unit);
2450 static void dwarf2out_set_name (tree, tree);
2452 /* The debug hooks structure. */
2454 const struct gcc_debug_hooks dwarf2_debug_hooks =
2456 dwarf2out_init,
2457 dwarf2out_finish,
2458 dwarf2out_early_finish,
2459 dwarf2out_assembly_start,
2460 dwarf2out_define,
2461 dwarf2out_undef,
2462 dwarf2out_start_source_file,
2463 dwarf2out_end_source_file,
2464 dwarf2out_begin_block,
2465 dwarf2out_end_block,
2466 dwarf2out_ignore_block,
2467 dwarf2out_source_line,
2468 dwarf2out_begin_prologue,
2469 #if VMS_DEBUGGING_INFO
2470 dwarf2out_vms_end_prologue,
2471 dwarf2out_vms_begin_epilogue,
2472 #else
2473 debug_nothing_int_charstar,
2474 debug_nothing_int_charstar,
2475 #endif
2476 dwarf2out_end_epilogue,
2477 dwarf2out_begin_function,
2478 dwarf2out_end_function, /* end_function */
2479 dwarf2out_register_main_translation_unit,
2480 dwarf2out_function_decl, /* function_decl */
2481 dwarf2out_early_global_decl,
2482 dwarf2out_late_global_decl,
2483 dwarf2out_type_decl, /* type_decl */
2484 dwarf2out_imported_module_or_decl,
2485 debug_nothing_tree, /* deferred_inline_function */
2486 /* The DWARF 2 backend tries to reduce debugging bloat by not
2487 emitting the abstract description of inline functions until
2488 something tries to reference them. */
2489 dwarf2out_abstract_function, /* outlining_inline_function */
2490 debug_nothing_rtx_code_label, /* label */
2491 debug_nothing_int, /* handle_pch */
2492 dwarf2out_var_location,
2493 dwarf2out_switch_text_section,
2494 dwarf2out_set_name,
2495 1, /* start_end_main_source_file */
2496 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2499 /* NOTE: In the comments in this file, many references are made to
2500 "Debugging Information Entries". This term is abbreviated as `DIE'
2501 throughout the remainder of this file. */
2503 /* An internal representation of the DWARF output is built, and then
2504 walked to generate the DWARF debugging info. The walk of the internal
2505 representation is done after the entire program has been compiled.
2506 The types below are used to describe the internal representation. */
2508 /* Whether to put type DIEs into their own section .debug_types instead
2509 of making them part of the .debug_info section. Only supported for
2510 Dwarf V4 or higher and the user didn't disable them through
2511 -fno-debug-types-section. It is more efficient to put them in a
2512 separate comdat sections since the linker will then be able to
2513 remove duplicates. But not all tools support .debug_types sections
2514 yet. */
2516 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2518 /* Various DIE's use offsets relative to the beginning of the
2519 .debug_info section to refer to each other. */
2521 typedef long int dw_offset;
2523 /* Define typedefs here to avoid circular dependencies. */
2525 typedef struct dw_attr_struct *dw_attr_ref;
2526 typedef struct dw_line_info_struct *dw_line_info_ref;
2527 typedef struct pubname_struct *pubname_ref;
2528 typedef struct dw_ranges_struct *dw_ranges_ref;
2529 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2530 typedef struct comdat_type_struct *comdat_type_node_ref;
2532 /* The entries in the line_info table more-or-less mirror the opcodes
2533 that are used in the real dwarf line table. Arrays of these entries
2534 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2535 supported. */
2537 enum dw_line_info_opcode {
2538 /* Emit DW_LNE_set_address; the operand is the label index. */
2539 LI_set_address,
2541 /* Emit a row to the matrix with the given line. This may be done
2542 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2543 special opcodes. */
2544 LI_set_line,
2546 /* Emit a DW_LNS_set_file. */
2547 LI_set_file,
2549 /* Emit a DW_LNS_set_column. */
2550 LI_set_column,
2552 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2553 LI_negate_stmt,
2555 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2556 LI_set_prologue_end,
2557 LI_set_epilogue_begin,
2559 /* Emit a DW_LNE_set_discriminator. */
2560 LI_set_discriminator
2563 typedef struct GTY(()) dw_line_info_struct {
2564 enum dw_line_info_opcode opcode;
2565 unsigned int val;
2566 } dw_line_info_entry;
2569 typedef struct GTY(()) dw_line_info_table_struct {
2570 /* The label that marks the end of this section. */
2571 const char *end_label;
2573 /* The values for the last row of the matrix, as collected in the table.
2574 These are used to minimize the changes to the next row. */
2575 unsigned int file_num;
2576 unsigned int line_num;
2577 unsigned int column_num;
2578 int discrim_num;
2579 bool is_stmt;
2580 bool in_use;
2582 vec<dw_line_info_entry, va_gc> *entries;
2583 } dw_line_info_table;
2585 typedef dw_line_info_table *dw_line_info_table_p;
2588 /* Each DIE attribute has a field specifying the attribute kind,
2589 a link to the next attribute in the chain, and an attribute value.
2590 Attributes are typically linked below the DIE they modify. */
2592 typedef struct GTY(()) dw_attr_struct {
2593 enum dwarf_attribute dw_attr;
2594 dw_val_node dw_attr_val;
2596 dw_attr_node;
2599 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2600 The children of each node form a circular list linked by
2601 die_sib. die_child points to the node *before* the "first" child node. */
2603 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2604 union die_symbol_or_type_node
2606 const char * GTY ((tag ("0"))) die_symbol;
2607 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2609 GTY ((desc ("%0.comdat_type_p"))) die_id;
2610 vec<dw_attr_node, va_gc> *die_attr;
2611 dw_die_ref die_parent;
2612 dw_die_ref die_child;
2613 dw_die_ref die_sib;
2614 dw_die_ref die_definition; /* ref from a specification to its definition */
2615 dw_offset die_offset;
2616 unsigned long die_abbrev;
2617 int die_mark;
2618 unsigned int decl_id;
2619 enum dwarf_tag die_tag;
2620 /* Die is used and must not be pruned as unused. */
2621 BOOL_BITFIELD die_perennial_p : 1;
2622 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2623 /* Lots of spare bits. */
2625 die_node;
2627 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2628 static bool early_dwarf;
2629 struct set_early_dwarf {
2630 bool saved;
2631 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2632 ~set_early_dwarf () { early_dwarf = saved; }
2635 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2636 #define FOR_EACH_CHILD(die, c, expr) do { \
2637 c = die->die_child; \
2638 if (c) do { \
2639 c = c->die_sib; \
2640 expr; \
2641 } while (c != die->die_child); \
2642 } while (0)
2644 /* The pubname structure */
2646 typedef struct GTY(()) pubname_struct {
2647 dw_die_ref die;
2648 const char *name;
2650 pubname_entry;
2653 struct GTY(()) dw_ranges_struct {
2654 /* If this is positive, it's a block number, otherwise it's a
2655 bitwise-negated index into dw_ranges_by_label. */
2656 int num;
2659 /* A structure to hold a macinfo entry. */
2661 typedef struct GTY(()) macinfo_struct {
2662 unsigned char code;
2663 unsigned HOST_WIDE_INT lineno;
2664 const char *info;
2666 macinfo_entry;
2669 struct GTY(()) dw_ranges_by_label_struct {
2670 const char *begin;
2671 const char *end;
2674 /* The comdat type node structure. */
2675 typedef struct GTY(()) comdat_type_struct
2677 dw_die_ref root_die;
2678 dw_die_ref type_die;
2679 dw_die_ref skeleton_die;
2680 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2681 struct comdat_type_struct *next;
2683 comdat_type_node;
2685 /* A list of DIEs for which we can't determine ancestry (parent_die
2686 field) just yet. Later in dwarf2out_finish we will fill in the
2687 missing bits. */
2688 typedef struct GTY(()) limbo_die_struct {
2689 dw_die_ref die;
2690 /* The tree for which this DIE was created. We use this to
2691 determine ancestry later. */
2692 tree created_for;
2693 struct limbo_die_struct *next;
2695 limbo_die_node;
2697 typedef struct skeleton_chain_struct
2699 dw_die_ref old_die;
2700 dw_die_ref new_die;
2701 struct skeleton_chain_struct *parent;
2703 skeleton_chain_node;
2705 /* Define a macro which returns nonzero for a TYPE_DECL which was
2706 implicitly generated for a type.
2708 Note that, unlike the C front-end (which generates a NULL named
2709 TYPE_DECL node for each complete tagged type, each array type,
2710 and each function type node created) the C++ front-end generates
2711 a _named_ TYPE_DECL node for each tagged type node created.
2712 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2713 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2714 front-end, but for each type, tagged or not. */
2716 #define TYPE_DECL_IS_STUB(decl) \
2717 (DECL_NAME (decl) == NULL_TREE \
2718 || (DECL_ARTIFICIAL (decl) \
2719 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2720 /* This is necessary for stub decls that \
2721 appear in nested inline functions. */ \
2722 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2723 && (decl_ultimate_origin (decl) \
2724 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2726 /* Information concerning the compilation unit's programming
2727 language, and compiler version. */
2729 /* Fixed size portion of the DWARF compilation unit header. */
2730 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2731 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2733 /* Fixed size portion of the DWARF comdat type unit header. */
2734 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2735 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2736 + DWARF_OFFSET_SIZE)
2738 /* Fixed size portion of public names info. */
2739 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2741 /* Fixed size portion of the address range info. */
2742 #define DWARF_ARANGES_HEADER_SIZE \
2743 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2744 DWARF2_ADDR_SIZE * 2) \
2745 - DWARF_INITIAL_LENGTH_SIZE)
2747 /* Size of padding portion in the address range info. It must be
2748 aligned to twice the pointer size. */
2749 #define DWARF_ARANGES_PAD_SIZE \
2750 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2751 DWARF2_ADDR_SIZE * 2) \
2752 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2754 /* Use assembler line directives if available. */
2755 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2756 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2757 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2758 #else
2759 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2760 #endif
2761 #endif
2763 /* Minimum line offset in a special line info. opcode.
2764 This value was chosen to give a reasonable range of values. */
2765 #define DWARF_LINE_BASE -10
2767 /* First special line opcode - leave room for the standard opcodes. */
2768 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2770 /* Range of line offsets in a special line info. opcode. */
2771 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2773 /* Flag that indicates the initial value of the is_stmt_start flag.
2774 In the present implementation, we do not mark any lines as
2775 the beginning of a source statement, because that information
2776 is not made available by the GCC front-end. */
2777 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2779 /* Maximum number of operations per instruction bundle. */
2780 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2781 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2782 #endif
2784 /* This location is used by calc_die_sizes() to keep track
2785 the offset of each DIE within the .debug_info section. */
2786 static unsigned long next_die_offset;
2788 /* Record the root of the DIE's built for the current compilation unit. */
2789 static GTY(()) dw_die_ref single_comp_unit_die;
2791 /* A list of type DIEs that have been separated into comdat sections. */
2792 static GTY(()) comdat_type_node *comdat_type_list;
2794 /* A list of DIEs with a NULL parent waiting to be relocated. */
2795 static GTY(()) limbo_die_node *limbo_die_list;
2797 /* A list of DIEs for which we may have to generate
2798 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2799 static GTY(()) limbo_die_node *deferred_asm_name;
2801 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2803 typedef const char *compare_type;
2805 static hashval_t hash (dwarf_file_data *);
2806 static bool equal (dwarf_file_data *, const char *);
2809 /* Filenames referenced by this compilation unit. */
2810 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2812 struct decl_die_hasher : ggc_hasher<die_node *>
2814 typedef tree compare_type;
2816 static hashval_t hash (die_node *);
2817 static bool equal (die_node *, tree);
2819 /* A hash table of references to DIE's that describe declarations.
2820 The key is a DECL_UID() which is a unique number identifying each decl. */
2821 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2823 struct block_die_hasher : ggc_hasher<die_struct *>
2825 static hashval_t hash (die_struct *);
2826 static bool equal (die_struct *, die_struct *);
2829 /* A hash table of references to DIE's that describe COMMON blocks.
2830 The key is DECL_UID() ^ die_parent. */
2831 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2833 typedef struct GTY(()) die_arg_entry_struct {
2834 dw_die_ref die;
2835 tree arg;
2836 } die_arg_entry;
2839 /* Node of the variable location list. */
2840 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2841 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2842 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2843 in mode of the EXPR_LIST node and first EXPR_LIST operand
2844 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2845 location or NULL for padding. For larger bitsizes,
2846 mode is 0 and first operand is a CONCAT with bitsize
2847 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2848 NULL as second operand. */
2849 rtx GTY (()) loc;
2850 const char * GTY (()) label;
2851 struct var_loc_node * GTY (()) next;
2854 /* Variable location list. */
2855 struct GTY ((for_user)) var_loc_list_def {
2856 struct var_loc_node * GTY (()) first;
2858 /* Pointer to the last but one or last element of the
2859 chained list. If the list is empty, both first and
2860 last are NULL, if the list contains just one node
2861 or the last node certainly is not redundant, it points
2862 to the last node, otherwise points to the last but one.
2863 Do not mark it for GC because it is marked through the chain. */
2864 struct var_loc_node * GTY ((skip ("%h"))) last;
2866 /* Pointer to the last element before section switch,
2867 if NULL, either sections weren't switched or first
2868 is after section switch. */
2869 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2871 /* DECL_UID of the variable decl. */
2872 unsigned int decl_id;
2874 typedef struct var_loc_list_def var_loc_list;
2876 /* Call argument location list. */
2877 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2878 rtx GTY (()) call_arg_loc_note;
2879 const char * GTY (()) label;
2880 tree GTY (()) block;
2881 bool tail_call_p;
2882 rtx GTY (()) symbol_ref;
2883 struct call_arg_loc_node * GTY (()) next;
2887 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2889 typedef const_tree compare_type;
2891 static hashval_t hash (var_loc_list *);
2892 static bool equal (var_loc_list *, const_tree);
2895 /* Table of decl location linked lists. */
2896 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2898 /* Head and tail of call_arg_loc chain. */
2899 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2900 static struct call_arg_loc_node *call_arg_loc_last;
2902 /* Number of call sites in the current function. */
2903 static int call_site_count = -1;
2904 /* Number of tail call sites in the current function. */
2905 static int tail_call_site_count = -1;
2907 /* A cached location list. */
2908 struct GTY ((for_user)) cached_dw_loc_list_def {
2909 /* The DECL_UID of the decl that this entry describes. */
2910 unsigned int decl_id;
2912 /* The cached location list. */
2913 dw_loc_list_ref loc_list;
2915 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2917 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2920 typedef const_tree compare_type;
2922 static hashval_t hash (cached_dw_loc_list *);
2923 static bool equal (cached_dw_loc_list *, const_tree);
2926 /* Table of cached location lists. */
2927 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2929 /* A pointer to the base of a list of references to DIE's that
2930 are uniquely identified by their tag, presence/absence of
2931 children DIE's, and list of attribute/value pairs. */
2932 static GTY((length ("abbrev_die_table_allocated")))
2933 dw_die_ref *abbrev_die_table;
2935 /* Number of elements currently allocated for abbrev_die_table. */
2936 static GTY(()) unsigned abbrev_die_table_allocated;
2938 /* Number of elements in abbrev_die_table currently in use. */
2939 static GTY(()) unsigned abbrev_die_table_in_use;
2941 /* Size (in elements) of increments by which we may expand the
2942 abbrev_die_table. */
2943 #define ABBREV_DIE_TABLE_INCREMENT 256
2945 /* A global counter for generating labels for line number data. */
2946 static unsigned int line_info_label_num;
2948 /* The current table to which we should emit line number information
2949 for the current function. This will be set up at the beginning of
2950 assembly for the function. */
2951 static dw_line_info_table *cur_line_info_table;
2953 /* The two default tables of line number info. */
2954 static GTY(()) dw_line_info_table *text_section_line_info;
2955 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2957 /* The set of all non-default tables of line number info. */
2958 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2960 /* A flag to tell pubnames/types export if there is an info section to
2961 refer to. */
2962 static bool info_section_emitted;
2964 /* A pointer to the base of a table that contains a list of publicly
2965 accessible names. */
2966 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2968 /* A pointer to the base of a table that contains a list of publicly
2969 accessible types. */
2970 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2972 /* A pointer to the base of a table that contains a list of macro
2973 defines/undefines (and file start/end markers). */
2974 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2976 /* True if .debug_macinfo or .debug_macros section is going to be
2977 emitted. */
2978 #define have_macinfo \
2979 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2980 && !macinfo_table->is_empty ())
2982 /* Array of dies for which we should generate .debug_ranges info. */
2983 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2985 /* Number of elements currently allocated for ranges_table. */
2986 static GTY(()) unsigned ranges_table_allocated;
2988 /* Number of elements in ranges_table currently in use. */
2989 static GTY(()) unsigned ranges_table_in_use;
2991 /* Array of pairs of labels referenced in ranges_table. */
2992 static GTY ((length ("ranges_by_label_allocated")))
2993 dw_ranges_by_label_ref ranges_by_label;
2995 /* Number of elements currently allocated for ranges_by_label. */
2996 static GTY(()) unsigned ranges_by_label_allocated;
2998 /* Number of elements in ranges_by_label currently in use. */
2999 static GTY(()) unsigned ranges_by_label_in_use;
3001 /* Size (in elements) of increments by which we may expand the
3002 ranges_table. */
3003 #define RANGES_TABLE_INCREMENT 64
3005 /* Whether we have location lists that need outputting */
3006 static GTY(()) bool have_location_lists;
3008 /* Unique label counter. */
3009 static GTY(()) unsigned int loclabel_num;
3011 /* Unique label counter for point-of-call tables. */
3012 static GTY(()) unsigned int poc_label_num;
3014 /* The last file entry emitted by maybe_emit_file(). */
3015 static GTY(()) struct dwarf_file_data * last_emitted_file;
3017 /* Number of internal labels generated by gen_internal_sym(). */
3018 static GTY(()) int label_num;
3020 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3022 /* Instances of generic types for which we need to generate debug
3023 info that describe their generic parameters and arguments. That
3024 generation needs to happen once all types are properly laid out so
3025 we do it at the end of compilation. */
3026 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3028 /* Offset from the "steady-state frame pointer" to the frame base,
3029 within the current function. */
3030 static HOST_WIDE_INT frame_pointer_fb_offset;
3031 static bool frame_pointer_fb_offset_valid;
3033 static vec<dw_die_ref> base_types;
3035 /* Flags to represent a set of attribute classes for attributes that represent
3036 a scalar value (bounds, pointers, ...). */
3037 enum dw_scalar_form
3039 dw_scalar_form_constant = 0x01,
3040 dw_scalar_form_exprloc = 0x02,
3041 dw_scalar_form_reference = 0x04
3044 /* Forward declarations for functions defined in this file. */
3046 static int is_pseudo_reg (const_rtx);
3047 static tree type_main_variant (tree);
3048 static int is_tagged_type (const_tree);
3049 static const char *dwarf_tag_name (unsigned);
3050 static const char *dwarf_attr_name (unsigned);
3051 static const char *dwarf_form_name (unsigned);
3052 static tree decl_ultimate_origin (const_tree);
3053 static tree decl_class_context (tree);
3054 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3055 static inline enum dw_val_class AT_class (dw_attr_ref);
3056 static inline unsigned int AT_index (dw_attr_ref);
3057 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3058 static inline unsigned AT_flag (dw_attr_ref);
3059 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3060 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3061 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3062 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3063 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3064 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3065 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3066 unsigned int, unsigned char *);
3067 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3068 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3069 static inline const char *AT_string (dw_attr_ref);
3070 static enum dwarf_form AT_string_form (dw_attr_ref);
3071 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3072 static void add_AT_specification (dw_die_ref, dw_die_ref);
3073 static inline dw_die_ref AT_ref (dw_attr_ref);
3074 static inline int AT_ref_external (dw_attr_ref);
3075 static inline void set_AT_ref_external (dw_attr_ref, int);
3076 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3077 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3078 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3079 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3080 dw_loc_list_ref);
3081 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3082 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3083 static void remove_addr_table_entry (addr_table_entry *);
3084 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3085 static inline rtx AT_addr (dw_attr_ref);
3086 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3087 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3088 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3089 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3090 unsigned HOST_WIDE_INT);
3091 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3092 unsigned long, bool);
3093 static inline const char *AT_lbl (dw_attr_ref);
3094 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3095 static const char *get_AT_low_pc (dw_die_ref);
3096 static const char *get_AT_hi_pc (dw_die_ref);
3097 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3098 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3099 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3100 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3101 static bool is_cxx (void);
3102 static bool is_fortran (void);
3103 static bool is_ada (void);
3104 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3105 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3106 static void add_child_die (dw_die_ref, dw_die_ref);
3107 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3108 static dw_die_ref lookup_type_die (tree);
3109 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3110 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3111 static void equate_type_number_to_die (tree, dw_die_ref);
3112 static dw_die_ref lookup_decl_die (tree);
3113 static var_loc_list *lookup_decl_loc (const_tree);
3114 static void equate_decl_number_to_die (tree, dw_die_ref);
3115 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3116 static void print_spaces (FILE *);
3117 static void print_die (dw_die_ref, FILE *);
3118 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3119 static dw_die_ref pop_compile_unit (dw_die_ref);
3120 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3121 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3122 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3123 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3124 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3125 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3126 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3127 struct md5_ctx *, int *);
3128 struct checksum_attributes;
3129 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3130 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3131 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3132 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3133 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3134 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3135 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3136 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3137 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3138 static void compute_section_prefix (dw_die_ref);
3139 static int is_type_die (dw_die_ref);
3140 static int is_comdat_die (dw_die_ref);
3141 static int is_symbol_die (dw_die_ref);
3142 static inline bool is_template_instantiation (dw_die_ref);
3143 static void assign_symbol_names (dw_die_ref);
3144 static void break_out_includes (dw_die_ref);
3145 static int is_declaration_die (dw_die_ref);
3146 static int should_move_die_to_comdat (dw_die_ref);
3147 static dw_die_ref clone_as_declaration (dw_die_ref);
3148 static dw_die_ref clone_die (dw_die_ref);
3149 static dw_die_ref clone_tree (dw_die_ref);
3150 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3151 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3152 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3153 static dw_die_ref generate_skeleton (dw_die_ref);
3154 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3155 dw_die_ref,
3156 dw_die_ref);
3157 static void break_out_comdat_types (dw_die_ref);
3158 static void copy_decls_for_unworthy_types (dw_die_ref);
3160 static void add_sibling_attributes (dw_die_ref);
3161 static void output_location_lists (dw_die_ref);
3162 static int constant_size (unsigned HOST_WIDE_INT);
3163 static unsigned long size_of_die (dw_die_ref);
3164 static void calc_die_sizes (dw_die_ref);
3165 static void calc_base_type_die_sizes (void);
3166 static void mark_dies (dw_die_ref);
3167 static void unmark_dies (dw_die_ref);
3168 static void unmark_all_dies (dw_die_ref);
3169 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3170 static unsigned long size_of_aranges (void);
3171 static enum dwarf_form value_format (dw_attr_ref);
3172 static void output_value_format (dw_attr_ref);
3173 static void output_abbrev_section (void);
3174 static void output_die_abbrevs (unsigned long, dw_die_ref);
3175 static void output_die_symbol (dw_die_ref);
3176 static void output_die (dw_die_ref);
3177 static void output_compilation_unit_header (void);
3178 static void output_comp_unit (dw_die_ref, int);
3179 static void output_comdat_type_unit (comdat_type_node *);
3180 static const char *dwarf2_name (tree, int);
3181 static void add_pubname (tree, dw_die_ref);
3182 static void add_enumerator_pubname (const char *, dw_die_ref);
3183 static void add_pubname_string (const char *, dw_die_ref);
3184 static void add_pubtype (tree, dw_die_ref);
3185 static void output_pubnames (vec<pubname_entry, va_gc> *);
3186 static void output_aranges (unsigned long);
3187 static unsigned int add_ranges_num (int);
3188 static unsigned int add_ranges (const_tree);
3189 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3190 bool *, bool);
3191 static void output_ranges (void);
3192 static dw_line_info_table *new_line_info_table (void);
3193 static void output_line_info (bool);
3194 static void output_file_names (void);
3195 static dw_die_ref base_type_die (tree);
3196 static int is_base_type (tree);
3197 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3198 static int decl_quals (const_tree);
3199 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3200 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3201 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3202 static int type_is_enum (const_tree);
3203 static unsigned int dbx_reg_number (const_rtx);
3204 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3205 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3206 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3207 enum var_init_status);
3208 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3209 enum var_init_status);
3210 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3211 enum var_init_status);
3212 static int is_based_loc (const_rtx);
3213 static bool resolve_one_addr (rtx *);
3214 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3215 enum var_init_status);
3216 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3217 enum var_init_status);
3218 struct loc_descr_context;
3219 static dw_loc_list_ref loc_list_from_tree (tree, int,
3220 const struct loc_descr_context *);
3221 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3222 const struct loc_descr_context *);
3223 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3224 static tree field_type (const_tree);
3225 static unsigned int simple_type_align_in_bits (const_tree);
3226 static unsigned int simple_decl_align_in_bits (const_tree);
3227 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3228 static HOST_WIDE_INT field_byte_offset (const_tree);
3229 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3230 dw_loc_list_ref);
3231 static void add_data_member_location_attribute (dw_die_ref, tree);
3232 static bool add_const_value_attribute (dw_die_ref, rtx);
3233 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3234 static void insert_wide_int (const wide_int &, unsigned char *, int);
3235 static void insert_float (const_rtx, unsigned char *);
3236 static rtx rtl_for_decl_location (tree);
3237 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3238 enum dwarf_attribute);
3239 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3240 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3241 static void add_name_attribute (dw_die_ref, const char *);
3242 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3243 static void add_comp_dir_attribute (dw_die_ref);
3244 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3245 const struct loc_descr_context *);
3246 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3247 const struct loc_descr_context *);
3248 static void add_subscript_info (dw_die_ref, tree, bool);
3249 static void add_byte_size_attribute (dw_die_ref, tree);
3250 static void add_bit_offset_attribute (dw_die_ref, tree);
3251 static void add_bit_size_attribute (dw_die_ref, tree);
3252 static void add_prototyped_attribute (dw_die_ref, tree);
3253 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3254 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3255 static void add_src_coords_attributes (dw_die_ref, tree);
3256 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3257 static void push_decl_scope (tree);
3258 static void pop_decl_scope (void);
3259 static dw_die_ref scope_die_for (tree, dw_die_ref);
3260 static inline int local_scope_p (dw_die_ref);
3261 static inline int class_scope_p (dw_die_ref);
3262 static inline int class_or_namespace_scope_p (dw_die_ref);
3263 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3264 static void add_calling_convention_attribute (dw_die_ref, tree);
3265 static const char *type_tag (const_tree);
3266 static tree member_declared_type (const_tree);
3267 #if 0
3268 static const char *decl_start_label (tree);
3269 #endif
3270 static void gen_array_type_die (tree, dw_die_ref);
3271 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3272 #if 0
3273 static void gen_entry_point_die (tree, dw_die_ref);
3274 #endif
3275 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3276 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3277 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3278 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3279 static void gen_formal_types_die (tree, dw_die_ref);
3280 static void gen_subprogram_die (tree, dw_die_ref);
3281 static void gen_variable_die (tree, tree, dw_die_ref);
3282 static void gen_const_die (tree, dw_die_ref);
3283 static void gen_label_die (tree, dw_die_ref);
3284 static void gen_lexical_block_die (tree, dw_die_ref);
3285 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3286 static void gen_field_die (tree, dw_die_ref);
3287 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3288 static dw_die_ref gen_compile_unit_die (const char *);
3289 static void gen_inheritance_die (tree, tree, dw_die_ref);
3290 static void gen_member_die (tree, dw_die_ref);
3291 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3292 enum debug_info_usage);
3293 static void gen_subroutine_type_die (tree, dw_die_ref);
3294 static void gen_typedef_die (tree, dw_die_ref);
3295 static void gen_type_die (tree, dw_die_ref);
3296 static void gen_block_die (tree, dw_die_ref);
3297 static void decls_for_scope (tree, dw_die_ref);
3298 static inline int is_redundant_typedef (const_tree);
3299 static bool is_naming_typedef_decl (const_tree);
3300 static inline dw_die_ref get_context_die (tree);
3301 static void gen_namespace_die (tree, dw_die_ref);
3302 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3303 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3304 static dw_die_ref force_decl_die (tree);
3305 static dw_die_ref force_type_die (tree);
3306 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3307 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3308 static struct dwarf_file_data * lookup_filename (const char *);
3309 static void retry_incomplete_types (void);
3310 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3311 static void gen_generic_params_dies (tree);
3312 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3313 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3314 static void splice_child_die (dw_die_ref, dw_die_ref);
3315 static int file_info_cmp (const void *, const void *);
3316 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3317 const char *, const char *);
3318 static void output_loc_list (dw_loc_list_ref);
3319 static char *gen_internal_sym (const char *);
3320 static bool want_pubnames (void);
3322 static void prune_unmark_dies (dw_die_ref);
3323 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3324 static void prune_unused_types_mark (dw_die_ref, int);
3325 static void prune_unused_types_walk (dw_die_ref);
3326 static void prune_unused_types_walk_attribs (dw_die_ref);
3327 static void prune_unused_types_prune (dw_die_ref);
3328 static void prune_unused_types (void);
3329 static int maybe_emit_file (struct dwarf_file_data *fd);
3330 static inline const char *AT_vms_delta1 (dw_attr_ref);
3331 static inline const char *AT_vms_delta2 (dw_attr_ref);
3332 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3333 const char *, const char *);
3334 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3335 static void gen_remaining_tmpl_value_param_die_attribute (void);
3336 static bool generic_type_p (tree);
3337 static void schedule_generic_params_dies_gen (tree t);
3338 static void gen_scheduled_generic_parms_dies (void);
3340 static const char *comp_dir_string (void);
3342 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3344 /* enum for tracking thread-local variables whose address is really an offset
3345 relative to the TLS pointer, which will need link-time relocation, but will
3346 not need relocation by the DWARF consumer. */
3348 enum dtprel_bool
3350 dtprel_false = 0,
3351 dtprel_true = 1
3354 /* Return the operator to use for an address of a variable. For dtprel_true, we
3355 use DW_OP_const*. For regular variables, which need both link-time
3356 relocation and consumer-level relocation (e.g., to account for shared objects
3357 loaded at a random address), we use DW_OP_addr*. */
3359 static inline enum dwarf_location_atom
3360 dw_addr_op (enum dtprel_bool dtprel)
3362 if (dtprel == dtprel_true)
3363 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3364 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3365 else
3366 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3369 /* Return a pointer to a newly allocated address location description. If
3370 dwarf_split_debug_info is true, then record the address with the appropriate
3371 relocation. */
3372 static inline dw_loc_descr_ref
3373 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3375 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3377 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3378 ref->dw_loc_oprnd1.v.val_addr = addr;
3379 ref->dtprel = dtprel;
3380 if (dwarf_split_debug_info)
3381 ref->dw_loc_oprnd1.val_entry
3382 = add_addr_table_entry (addr,
3383 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3384 else
3385 ref->dw_loc_oprnd1.val_entry = NULL;
3387 return ref;
3390 /* Section names used to hold DWARF debugging information. */
3392 #ifndef DEBUG_INFO_SECTION
3393 #define DEBUG_INFO_SECTION ".debug_info"
3394 #endif
3395 #ifndef DEBUG_DWO_INFO_SECTION
3396 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3397 #endif
3398 #ifndef DEBUG_ABBREV_SECTION
3399 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3400 #endif
3401 #ifndef DEBUG_DWO_ABBREV_SECTION
3402 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3403 #endif
3404 #ifndef DEBUG_ARANGES_SECTION
3405 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3406 #endif
3407 #ifndef DEBUG_ADDR_SECTION
3408 #define DEBUG_ADDR_SECTION ".debug_addr"
3409 #endif
3410 #ifndef DEBUG_NORM_MACINFO_SECTION
3411 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3412 #endif
3413 #ifndef DEBUG_DWO_MACINFO_SECTION
3414 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3415 #endif
3416 #ifndef DEBUG_MACINFO_SECTION
3417 #define DEBUG_MACINFO_SECTION \
3418 (!dwarf_split_debug_info \
3419 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3420 #endif
3421 #ifndef DEBUG_NORM_MACRO_SECTION
3422 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3423 #endif
3424 #ifndef DEBUG_DWO_MACRO_SECTION
3425 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3426 #endif
3427 #ifndef DEBUG_MACRO_SECTION
3428 #define DEBUG_MACRO_SECTION \
3429 (!dwarf_split_debug_info \
3430 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3431 #endif
3432 #ifndef DEBUG_LINE_SECTION
3433 #define DEBUG_LINE_SECTION ".debug_line"
3434 #endif
3435 #ifndef DEBUG_DWO_LINE_SECTION
3436 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3437 #endif
3438 #ifndef DEBUG_LOC_SECTION
3439 #define DEBUG_LOC_SECTION ".debug_loc"
3440 #endif
3441 #ifndef DEBUG_DWO_LOC_SECTION
3442 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3443 #endif
3444 #ifndef DEBUG_PUBNAMES_SECTION
3445 #define DEBUG_PUBNAMES_SECTION \
3446 ((debug_generate_pub_sections == 2) \
3447 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3448 #endif
3449 #ifndef DEBUG_PUBTYPES_SECTION
3450 #define DEBUG_PUBTYPES_SECTION \
3451 ((debug_generate_pub_sections == 2) \
3452 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3453 #endif
3454 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3455 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3456 #ifndef DEBUG_STR_OFFSETS_SECTION
3457 #define DEBUG_STR_OFFSETS_SECTION \
3458 (!dwarf_split_debug_info \
3459 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3460 #endif
3461 #ifndef DEBUG_STR_DWO_SECTION
3462 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3463 #endif
3464 #ifndef DEBUG_STR_SECTION
3465 #define DEBUG_STR_SECTION ".debug_str"
3466 #endif
3467 #ifndef DEBUG_RANGES_SECTION
3468 #define DEBUG_RANGES_SECTION ".debug_ranges"
3469 #endif
3471 /* Standard ELF section names for compiled code and data. */
3472 #ifndef TEXT_SECTION_NAME
3473 #define TEXT_SECTION_NAME ".text"
3474 #endif
3476 /* Section flags for .debug_macinfo/.debug_macro section. */
3477 #define DEBUG_MACRO_SECTION_FLAGS \
3478 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3480 /* Section flags for .debug_str section. */
3481 #define DEBUG_STR_SECTION_FLAGS \
3482 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3483 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3484 : SECTION_DEBUG)
3486 /* Section flags for .debug_str.dwo section. */
3487 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3489 /* Labels we insert at beginning sections we can reference instead of
3490 the section names themselves. */
3492 #ifndef TEXT_SECTION_LABEL
3493 #define TEXT_SECTION_LABEL "Ltext"
3494 #endif
3495 #ifndef COLD_TEXT_SECTION_LABEL
3496 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3497 #endif
3498 #ifndef DEBUG_LINE_SECTION_LABEL
3499 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3500 #endif
3501 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3502 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3503 #endif
3504 #ifndef DEBUG_INFO_SECTION_LABEL
3505 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3506 #endif
3507 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3508 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3509 #endif
3510 #ifndef DEBUG_ABBREV_SECTION_LABEL
3511 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3512 #endif
3513 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3514 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3515 #endif
3516 #ifndef DEBUG_ADDR_SECTION_LABEL
3517 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3518 #endif
3519 #ifndef DEBUG_LOC_SECTION_LABEL
3520 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3521 #endif
3522 #ifndef DEBUG_RANGES_SECTION_LABEL
3523 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3524 #endif
3525 #ifndef DEBUG_MACINFO_SECTION_LABEL
3526 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3527 #endif
3528 #ifndef DEBUG_MACRO_SECTION_LABEL
3529 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3530 #endif
3531 #define SKELETON_COMP_DIE_ABBREV 1
3532 #define SKELETON_TYPE_DIE_ABBREV 2
3534 /* Definitions of defaults for formats and names of various special
3535 (artificial) labels which may be generated within this file (when the -g
3536 options is used and DWARF2_DEBUGGING_INFO is in effect.
3537 If necessary, these may be overridden from within the tm.h file, but
3538 typically, overriding these defaults is unnecessary. */
3540 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3541 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3542 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3543 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3544 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3545 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3551 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3555 #ifndef TEXT_END_LABEL
3556 #define TEXT_END_LABEL "Letext"
3557 #endif
3558 #ifndef COLD_END_LABEL
3559 #define COLD_END_LABEL "Letext_cold"
3560 #endif
3561 #ifndef BLOCK_BEGIN_LABEL
3562 #define BLOCK_BEGIN_LABEL "LBB"
3563 #endif
3564 #ifndef BLOCK_END_LABEL
3565 #define BLOCK_END_LABEL "LBE"
3566 #endif
3567 #ifndef LINE_CODE_LABEL
3568 #define LINE_CODE_LABEL "LM"
3569 #endif
3572 /* Return the root of the DIE's built for the current compilation unit. */
3573 static dw_die_ref
3574 comp_unit_die (void)
3576 if (!single_comp_unit_die)
3577 single_comp_unit_die = gen_compile_unit_die (NULL);
3578 return single_comp_unit_die;
3581 /* We allow a language front-end to designate a function that is to be
3582 called to "demangle" any name before it is put into a DIE. */
3584 static const char *(*demangle_name_func) (const char *);
3586 void
3587 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3589 demangle_name_func = func;
3592 /* Test if rtl node points to a pseudo register. */
3594 static inline int
3595 is_pseudo_reg (const_rtx rtl)
3597 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3598 || (GET_CODE (rtl) == SUBREG
3599 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3602 /* Return a reference to a type, with its const and volatile qualifiers
3603 removed. */
3605 static inline tree
3606 type_main_variant (tree type)
3608 type = TYPE_MAIN_VARIANT (type);
3610 /* ??? There really should be only one main variant among any group of
3611 variants of a given type (and all of the MAIN_VARIANT values for all
3612 members of the group should point to that one type) but sometimes the C
3613 front-end messes this up for array types, so we work around that bug
3614 here. */
3615 if (TREE_CODE (type) == ARRAY_TYPE)
3616 while (type != TYPE_MAIN_VARIANT (type))
3617 type = TYPE_MAIN_VARIANT (type);
3619 return type;
3622 /* Return nonzero if the given type node represents a tagged type. */
3624 static inline int
3625 is_tagged_type (const_tree type)
3627 enum tree_code code = TREE_CODE (type);
3629 return (code == RECORD_TYPE || code == UNION_TYPE
3630 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3633 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3635 static void
3636 get_ref_die_offset_label (char *label, dw_die_ref ref)
3638 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3641 /* Return die_offset of a DIE reference to a base type. */
3643 static unsigned long int
3644 get_base_type_offset (dw_die_ref ref)
3646 if (ref->die_offset)
3647 return ref->die_offset;
3648 if (comp_unit_die ()->die_abbrev)
3650 calc_base_type_die_sizes ();
3651 gcc_assert (ref->die_offset);
3653 return ref->die_offset;
3656 /* Return die_offset of a DIE reference other than base type. */
3658 static unsigned long int
3659 get_ref_die_offset (dw_die_ref ref)
3661 gcc_assert (ref->die_offset);
3662 return ref->die_offset;
3665 /* Convert a DIE tag into its string name. */
3667 static const char *
3668 dwarf_tag_name (unsigned int tag)
3670 const char *name = get_DW_TAG_name (tag);
3672 if (name != NULL)
3673 return name;
3675 return "DW_TAG_<unknown>";
3678 /* Convert a DWARF attribute code into its string name. */
3680 static const char *
3681 dwarf_attr_name (unsigned int attr)
3683 const char *name;
3685 switch (attr)
3687 #if VMS_DEBUGGING_INFO
3688 case DW_AT_HP_prologue:
3689 return "DW_AT_HP_prologue";
3690 #else
3691 case DW_AT_MIPS_loop_unroll_factor:
3692 return "DW_AT_MIPS_loop_unroll_factor";
3693 #endif
3695 #if VMS_DEBUGGING_INFO
3696 case DW_AT_HP_epilogue:
3697 return "DW_AT_HP_epilogue";
3698 #else
3699 case DW_AT_MIPS_stride:
3700 return "DW_AT_MIPS_stride";
3701 #endif
3704 name = get_DW_AT_name (attr);
3706 if (name != NULL)
3707 return name;
3709 return "DW_AT_<unknown>";
3712 /* Convert a DWARF value form code into its string name. */
3714 static const char *
3715 dwarf_form_name (unsigned int form)
3717 const char *name = get_DW_FORM_name (form);
3719 if (name != NULL)
3720 return name;
3722 return "DW_FORM_<unknown>";
3725 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3726 instance of an inlined instance of a decl which is local to an inline
3727 function, so we have to trace all of the way back through the origin chain
3728 to find out what sort of node actually served as the original seed for the
3729 given block. */
3731 static tree
3732 decl_ultimate_origin (const_tree decl)
3734 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3735 return NULL_TREE;
3737 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3738 we're trying to output the abstract instance of this function. */
3739 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3740 return NULL_TREE;
3742 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3743 most distant ancestor, this should never happen. */
3744 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3746 return DECL_ABSTRACT_ORIGIN (decl);
3749 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3750 of a virtual function may refer to a base class, so we check the 'this'
3751 parameter. */
3753 static tree
3754 decl_class_context (tree decl)
3756 tree context = NULL_TREE;
3758 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3759 context = DECL_CONTEXT (decl);
3760 else
3761 context = TYPE_MAIN_VARIANT
3762 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3764 if (context && !TYPE_P (context))
3765 context = NULL_TREE;
3767 return context;
3770 /* Add an attribute/value pair to a DIE. */
3772 static inline void
3773 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3775 /* Maybe this should be an assert? */
3776 if (die == NULL)
3777 return;
3779 vec_safe_reserve (die->die_attr, 1);
3780 vec_safe_push (die->die_attr, *attr);
3783 static inline enum dw_val_class
3784 AT_class (dw_attr_ref a)
3786 return a->dw_attr_val.val_class;
3789 /* Return the index for any attribute that will be referenced with a
3790 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3791 are stored in dw_attr_val.v.val_str for reference counting
3792 pruning. */
3794 static inline unsigned int
3795 AT_index (dw_attr_ref a)
3797 if (AT_class (a) == dw_val_class_str)
3798 return a->dw_attr_val.v.val_str->index;
3799 else if (a->dw_attr_val.val_entry != NULL)
3800 return a->dw_attr_val.val_entry->index;
3801 return NOT_INDEXED;
3804 /* Add a flag value attribute to a DIE. */
3806 static inline void
3807 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3809 dw_attr_node attr;
3811 attr.dw_attr = attr_kind;
3812 attr.dw_attr_val.val_class = dw_val_class_flag;
3813 attr.dw_attr_val.val_entry = NULL;
3814 attr.dw_attr_val.v.val_flag = flag;
3815 add_dwarf_attr (die, &attr);
3818 static inline unsigned
3819 AT_flag (dw_attr_ref a)
3821 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3822 return a->dw_attr_val.v.val_flag;
3825 /* Add a signed integer attribute value to a DIE. */
3827 static inline void
3828 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3830 dw_attr_node attr;
3832 attr.dw_attr = attr_kind;
3833 attr.dw_attr_val.val_class = dw_val_class_const;
3834 attr.dw_attr_val.val_entry = NULL;
3835 attr.dw_attr_val.v.val_int = int_val;
3836 add_dwarf_attr (die, &attr);
3839 static inline HOST_WIDE_INT
3840 AT_int (dw_attr_ref a)
3842 gcc_assert (a && AT_class (a) == dw_val_class_const);
3843 return a->dw_attr_val.v.val_int;
3846 /* Add an unsigned integer attribute value to a DIE. */
3848 static inline void
3849 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3850 unsigned HOST_WIDE_INT unsigned_val)
3852 dw_attr_node attr;
3854 attr.dw_attr = attr_kind;
3855 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3856 attr.dw_attr_val.val_entry = NULL;
3857 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3858 add_dwarf_attr (die, &attr);
3861 static inline unsigned HOST_WIDE_INT
3862 AT_unsigned (dw_attr_ref a)
3864 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3865 return a->dw_attr_val.v.val_unsigned;
3868 /* Add an unsigned wide integer attribute value to a DIE. */
3870 static inline void
3871 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3872 const wide_int& w)
3874 dw_attr_node attr;
3876 attr.dw_attr = attr_kind;
3877 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3878 attr.dw_attr_val.val_entry = NULL;
3879 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3880 *attr.dw_attr_val.v.val_wide = w;
3881 add_dwarf_attr (die, &attr);
3884 /* Add an unsigned double integer attribute value to a DIE. */
3886 static inline void
3887 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3888 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3890 dw_attr_node attr;
3892 attr.dw_attr = attr_kind;
3893 attr.dw_attr_val.val_class = dw_val_class_const_double;
3894 attr.dw_attr_val.val_entry = NULL;
3895 attr.dw_attr_val.v.val_double.high = high;
3896 attr.dw_attr_val.v.val_double.low = low;
3897 add_dwarf_attr (die, &attr);
3900 /* Add a floating point attribute value to a DIE and return it. */
3902 static inline void
3903 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3904 unsigned int length, unsigned int elt_size, unsigned char *array)
3906 dw_attr_node attr;
3908 attr.dw_attr = attr_kind;
3909 attr.dw_attr_val.val_class = dw_val_class_vec;
3910 attr.dw_attr_val.val_entry = NULL;
3911 attr.dw_attr_val.v.val_vec.length = length;
3912 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3913 attr.dw_attr_val.v.val_vec.array = array;
3914 add_dwarf_attr (die, &attr);
3917 /* Add an 8-byte data attribute value to a DIE. */
3919 static inline void
3920 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3921 unsigned char data8[8])
3923 dw_attr_node attr;
3925 attr.dw_attr = attr_kind;
3926 attr.dw_attr_val.val_class = dw_val_class_data8;
3927 attr.dw_attr_val.val_entry = NULL;
3928 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3929 add_dwarf_attr (die, &attr);
3932 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3933 dwarf_split_debug_info, address attributes in dies destined for the
3934 final executable have force_direct set to avoid using indexed
3935 references. */
3937 static inline void
3938 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3939 bool force_direct)
3941 dw_attr_node attr;
3942 char * lbl_id;
3944 lbl_id = xstrdup (lbl_low);
3945 attr.dw_attr = DW_AT_low_pc;
3946 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3947 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3948 if (dwarf_split_debug_info && !force_direct)
3949 attr.dw_attr_val.val_entry
3950 = add_addr_table_entry (lbl_id, ate_kind_label);
3951 else
3952 attr.dw_attr_val.val_entry = NULL;
3953 add_dwarf_attr (die, &attr);
3955 attr.dw_attr = DW_AT_high_pc;
3956 if (dwarf_version < 4)
3957 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3958 else
3959 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3960 lbl_id = xstrdup (lbl_high);
3961 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3962 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3963 && dwarf_split_debug_info && !force_direct)
3964 attr.dw_attr_val.val_entry
3965 = add_addr_table_entry (lbl_id, ate_kind_label);
3966 else
3967 attr.dw_attr_val.val_entry = NULL;
3968 add_dwarf_attr (die, &attr);
3971 /* Hash and equality functions for debug_str_hash. */
3973 hashval_t
3974 indirect_string_hasher::hash (indirect_string_node *x)
3976 return htab_hash_string (x->str);
3979 bool
3980 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3982 return strcmp (x1->str, x2) == 0;
3985 /* Add STR to the given string hash table. */
3987 static struct indirect_string_node *
3988 find_AT_string_in_table (const char *str,
3989 hash_table<indirect_string_hasher> *table)
3991 struct indirect_string_node *node;
3993 indirect_string_node **slot
3994 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3995 if (*slot == NULL)
3997 node = ggc_cleared_alloc<indirect_string_node> ();
3998 node->str = ggc_strdup (str);
3999 *slot = node;
4001 else
4002 node = *slot;
4004 node->refcount++;
4005 return node;
4008 /* Add STR to the indirect string hash table. */
4010 static struct indirect_string_node *
4011 find_AT_string (const char *str)
4013 if (! debug_str_hash)
4014 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4016 return find_AT_string_in_table (str, debug_str_hash);
4019 /* Add a string attribute value to a DIE. */
4021 static inline void
4022 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4024 dw_attr_node attr;
4025 struct indirect_string_node *node;
4027 node = find_AT_string (str);
4029 attr.dw_attr = attr_kind;
4030 attr.dw_attr_val.val_class = dw_val_class_str;
4031 attr.dw_attr_val.val_entry = NULL;
4032 attr.dw_attr_val.v.val_str = node;
4033 add_dwarf_attr (die, &attr);
4036 static inline const char *
4037 AT_string (dw_attr_ref a)
4039 gcc_assert (a && AT_class (a) == dw_val_class_str);
4040 return a->dw_attr_val.v.val_str->str;
4043 /* Call this function directly to bypass AT_string_form's logic to put
4044 the string inline in the die. */
4046 static void
4047 set_indirect_string (struct indirect_string_node *node)
4049 char label[32];
4050 /* Already indirect is a no op. */
4051 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4053 gcc_assert (node->label);
4054 return;
4056 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4057 ++dw2_string_counter;
4058 node->label = xstrdup (label);
4060 if (!dwarf_split_debug_info)
4062 node->form = DW_FORM_strp;
4063 node->index = NOT_INDEXED;
4065 else
4067 node->form = DW_FORM_GNU_str_index;
4068 node->index = NO_INDEX_ASSIGNED;
4072 /* Find out whether a string should be output inline in DIE
4073 or out-of-line in .debug_str section. */
4075 static enum dwarf_form
4076 find_string_form (struct indirect_string_node *node)
4078 unsigned int len;
4080 if (node->form)
4081 return node->form;
4083 len = strlen (node->str) + 1;
4085 /* If the string is shorter or equal to the size of the reference, it is
4086 always better to put it inline. */
4087 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4088 return node->form = DW_FORM_string;
4090 /* If we cannot expect the linker to merge strings in .debug_str
4091 section, only put it into .debug_str if it is worth even in this
4092 single module. */
4093 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4094 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4095 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4096 return node->form = DW_FORM_string;
4098 set_indirect_string (node);
4100 return node->form;
4103 /* Find out whether the string referenced from the attribute should be
4104 output inline in DIE or out-of-line in .debug_str section. */
4106 static enum dwarf_form
4107 AT_string_form (dw_attr_ref a)
4109 gcc_assert (a && AT_class (a) == dw_val_class_str);
4110 return find_string_form (a->dw_attr_val.v.val_str);
4113 /* Add a DIE reference attribute value to a DIE. */
4115 static inline void
4116 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4118 dw_attr_node attr;
4120 #ifdef ENABLE_CHECKING
4121 gcc_assert (targ_die != NULL);
4122 #else
4123 /* With LTO we can end up trying to reference something we didn't create
4124 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4125 if (targ_die == NULL)
4126 return;
4127 #endif
4129 attr.dw_attr = attr_kind;
4130 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4131 attr.dw_attr_val.val_entry = NULL;
4132 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4133 attr.dw_attr_val.v.val_die_ref.external = 0;
4134 add_dwarf_attr (die, &attr);
4137 /* Change DIE reference REF to point to NEW_DIE instead. */
4139 static inline void
4140 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4142 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4143 ref->dw_attr_val.v.val_die_ref.die = new_die;
4144 ref->dw_attr_val.v.val_die_ref.external = 0;
4147 /* Add an AT_specification attribute to a DIE, and also make the back
4148 pointer from the specification to the definition. */
4150 static inline void
4151 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4153 add_AT_die_ref (die, DW_AT_specification, targ_die);
4154 gcc_assert (!targ_die->die_definition);
4155 targ_die->die_definition = die;
4158 static inline dw_die_ref
4159 AT_ref (dw_attr_ref a)
4161 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4162 return a->dw_attr_val.v.val_die_ref.die;
4165 static inline int
4166 AT_ref_external (dw_attr_ref a)
4168 if (a && AT_class (a) == dw_val_class_die_ref)
4169 return a->dw_attr_val.v.val_die_ref.external;
4171 return 0;
4174 static inline void
4175 set_AT_ref_external (dw_attr_ref a, int i)
4177 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4178 a->dw_attr_val.v.val_die_ref.external = i;
4181 /* Add an FDE reference attribute value to a DIE. */
4183 static inline void
4184 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4186 dw_attr_node attr;
4188 attr.dw_attr = attr_kind;
4189 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4190 attr.dw_attr_val.val_entry = NULL;
4191 attr.dw_attr_val.v.val_fde_index = targ_fde;
4192 add_dwarf_attr (die, &attr);
4195 /* Add a location description attribute value to a DIE. */
4197 static inline void
4198 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4200 dw_attr_node attr;
4202 attr.dw_attr = attr_kind;
4203 attr.dw_attr_val.val_class = dw_val_class_loc;
4204 attr.dw_attr_val.val_entry = NULL;
4205 attr.dw_attr_val.v.val_loc = loc;
4206 add_dwarf_attr (die, &attr);
4209 static inline dw_loc_descr_ref
4210 AT_loc (dw_attr_ref a)
4212 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4213 return a->dw_attr_val.v.val_loc;
4216 static inline void
4217 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4219 dw_attr_node attr;
4221 attr.dw_attr = attr_kind;
4222 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4223 attr.dw_attr_val.val_entry = NULL;
4224 attr.dw_attr_val.v.val_loc_list = loc_list;
4225 add_dwarf_attr (die, &attr);
4226 have_location_lists = true;
4229 static inline dw_loc_list_ref
4230 AT_loc_list (dw_attr_ref a)
4232 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4233 return a->dw_attr_val.v.val_loc_list;
4236 static inline dw_loc_list_ref *
4237 AT_loc_list_ptr (dw_attr_ref a)
4239 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4240 return &a->dw_attr_val.v.val_loc_list;
4243 struct addr_hasher : ggc_hasher<addr_table_entry *>
4245 static hashval_t hash (addr_table_entry *);
4246 static bool equal (addr_table_entry *, addr_table_entry *);
4249 /* Table of entries into the .debug_addr section. */
4251 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4253 /* Hash an address_table_entry. */
4255 hashval_t
4256 addr_hasher::hash (addr_table_entry *a)
4258 inchash::hash hstate;
4259 switch (a->kind)
4261 case ate_kind_rtx:
4262 hstate.add_int (0);
4263 break;
4264 case ate_kind_rtx_dtprel:
4265 hstate.add_int (1);
4266 break;
4267 case ate_kind_label:
4268 return htab_hash_string (a->addr.label);
4269 default:
4270 gcc_unreachable ();
4272 inchash::add_rtx (a->addr.rtl, hstate);
4273 return hstate.end ();
4276 /* Determine equality for two address_table_entries. */
4278 bool
4279 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4281 if (a1->kind != a2->kind)
4282 return 0;
4283 switch (a1->kind)
4285 case ate_kind_rtx:
4286 case ate_kind_rtx_dtprel:
4287 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4288 case ate_kind_label:
4289 return strcmp (a1->addr.label, a2->addr.label) == 0;
4290 default:
4291 gcc_unreachable ();
4295 /* Initialize an addr_table_entry. */
4297 void
4298 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4300 e->kind = kind;
4301 switch (kind)
4303 case ate_kind_rtx:
4304 case ate_kind_rtx_dtprel:
4305 e->addr.rtl = (rtx) addr;
4306 break;
4307 case ate_kind_label:
4308 e->addr.label = (char *) addr;
4309 break;
4311 e->refcount = 0;
4312 e->index = NO_INDEX_ASSIGNED;
4315 /* Add attr to the address table entry to the table. Defer setting an
4316 index until output time. */
4318 static addr_table_entry *
4319 add_addr_table_entry (void *addr, enum ate_kind kind)
4321 addr_table_entry *node;
4322 addr_table_entry finder;
4324 gcc_assert (dwarf_split_debug_info);
4325 if (! addr_index_table)
4326 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4327 init_addr_table_entry (&finder, kind, addr);
4328 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4330 if (*slot == HTAB_EMPTY_ENTRY)
4332 node = ggc_cleared_alloc<addr_table_entry> ();
4333 init_addr_table_entry (node, kind, addr);
4334 *slot = node;
4336 else
4337 node = *slot;
4339 node->refcount++;
4340 return node;
4343 /* Remove an entry from the addr table by decrementing its refcount.
4344 Strictly, decrementing the refcount would be enough, but the
4345 assertion that the entry is actually in the table has found
4346 bugs. */
4348 static void
4349 remove_addr_table_entry (addr_table_entry *entry)
4351 gcc_assert (dwarf_split_debug_info && addr_index_table);
4352 /* After an index is assigned, the table is frozen. */
4353 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4354 entry->refcount--;
4357 /* Given a location list, remove all addresses it refers to from the
4358 address_table. */
4360 static void
4361 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4363 for (; descr; descr = descr->dw_loc_next)
4364 if (descr->dw_loc_oprnd1.val_entry != NULL)
4366 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4367 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4371 /* A helper function for dwarf2out_finish called through
4372 htab_traverse. Assign an addr_table_entry its index. All entries
4373 must be collected into the table when this function is called,
4374 because the indexing code relies on htab_traverse to traverse nodes
4375 in the same order for each run. */
4378 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4380 addr_table_entry *node = *h;
4382 /* Don't index unreferenced nodes. */
4383 if (node->refcount == 0)
4384 return 1;
4386 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4387 node->index = *index;
4388 *index += 1;
4390 return 1;
4393 /* Add an address constant attribute value to a DIE. When using
4394 dwarf_split_debug_info, address attributes in dies destined for the
4395 final executable should be direct references--setting the parameter
4396 force_direct ensures this behavior. */
4398 static inline void
4399 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4400 bool force_direct)
4402 dw_attr_node attr;
4404 attr.dw_attr = attr_kind;
4405 attr.dw_attr_val.val_class = dw_val_class_addr;
4406 attr.dw_attr_val.v.val_addr = addr;
4407 if (dwarf_split_debug_info && !force_direct)
4408 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4409 else
4410 attr.dw_attr_val.val_entry = NULL;
4411 add_dwarf_attr (die, &attr);
4414 /* Get the RTX from to an address DIE attribute. */
4416 static inline rtx
4417 AT_addr (dw_attr_ref a)
4419 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4420 return a->dw_attr_val.v.val_addr;
4423 /* Add a file attribute value to a DIE. */
4425 static inline void
4426 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4427 struct dwarf_file_data *fd)
4429 dw_attr_node attr;
4431 attr.dw_attr = attr_kind;
4432 attr.dw_attr_val.val_class = dw_val_class_file;
4433 attr.dw_attr_val.val_entry = NULL;
4434 attr.dw_attr_val.v.val_file = fd;
4435 add_dwarf_attr (die, &attr);
4438 /* Get the dwarf_file_data from a file DIE attribute. */
4440 static inline struct dwarf_file_data *
4441 AT_file (dw_attr_ref a)
4443 gcc_assert (a && AT_class (a) == dw_val_class_file);
4444 return a->dw_attr_val.v.val_file;
4447 /* Add a vms delta attribute value to a DIE. */
4449 static inline void
4450 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4451 const char *lbl1, const char *lbl2)
4453 dw_attr_node attr;
4455 attr.dw_attr = attr_kind;
4456 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4457 attr.dw_attr_val.val_entry = NULL;
4458 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4459 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4460 add_dwarf_attr (die, &attr);
4463 /* Add a label identifier attribute value to a DIE. */
4465 static inline void
4466 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4467 const char *lbl_id)
4469 dw_attr_node attr;
4471 attr.dw_attr = attr_kind;
4472 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4473 attr.dw_attr_val.val_entry = NULL;
4474 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4475 if (dwarf_split_debug_info)
4476 attr.dw_attr_val.val_entry
4477 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4478 ate_kind_label);
4479 add_dwarf_attr (die, &attr);
4482 /* Add a section offset attribute value to a DIE, an offset into the
4483 debug_line section. */
4485 static inline void
4486 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4487 const char *label)
4489 dw_attr_node attr;
4491 attr.dw_attr = attr_kind;
4492 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4493 attr.dw_attr_val.val_entry = NULL;
4494 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4495 add_dwarf_attr (die, &attr);
4498 /* Add a section offset attribute value to a DIE, an offset into the
4499 debug_macinfo section. */
4501 static inline void
4502 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4503 const char *label)
4505 dw_attr_node attr;
4507 attr.dw_attr = attr_kind;
4508 attr.dw_attr_val.val_class = dw_val_class_macptr;
4509 attr.dw_attr_val.val_entry = NULL;
4510 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4511 add_dwarf_attr (die, &attr);
4514 /* Add an offset attribute value to a DIE. */
4516 static inline void
4517 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4518 unsigned HOST_WIDE_INT offset)
4520 dw_attr_node attr;
4522 attr.dw_attr = attr_kind;
4523 attr.dw_attr_val.val_class = dw_val_class_offset;
4524 attr.dw_attr_val.val_entry = NULL;
4525 attr.dw_attr_val.v.val_offset = offset;
4526 add_dwarf_attr (die, &attr);
4529 /* Add a range_list attribute value to a DIE. When using
4530 dwarf_split_debug_info, address attributes in dies destined for the
4531 final executable should be direct references--setting the parameter
4532 force_direct ensures this behavior. */
4534 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4535 #define RELOCATED_OFFSET (NULL)
4537 static void
4538 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4539 long unsigned int offset, bool force_direct)
4541 dw_attr_node attr;
4543 attr.dw_attr = attr_kind;
4544 attr.dw_attr_val.val_class = dw_val_class_range_list;
4545 /* For the range_list attribute, use val_entry to store whether the
4546 offset should follow split-debug-info or normal semantics. This
4547 value is read in output_range_list_offset. */
4548 if (dwarf_split_debug_info && !force_direct)
4549 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4550 else
4551 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4552 attr.dw_attr_val.v.val_offset = offset;
4553 add_dwarf_attr (die, &attr);
4556 /* Return the start label of a delta attribute. */
4558 static inline const char *
4559 AT_vms_delta1 (dw_attr_ref a)
4561 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4562 return a->dw_attr_val.v.val_vms_delta.lbl1;
4565 /* Return the end label of a delta attribute. */
4567 static inline const char *
4568 AT_vms_delta2 (dw_attr_ref a)
4570 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4571 return a->dw_attr_val.v.val_vms_delta.lbl2;
4574 static inline const char *
4575 AT_lbl (dw_attr_ref a)
4577 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4578 || AT_class (a) == dw_val_class_lineptr
4579 || AT_class (a) == dw_val_class_macptr
4580 || AT_class (a) == dw_val_class_high_pc));
4581 return a->dw_attr_val.v.val_lbl_id;
4584 /* Get the attribute of type attr_kind. */
4586 static dw_attr_ref
4587 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4589 dw_attr_ref a;
4590 unsigned ix;
4591 dw_die_ref spec = NULL;
4593 if (! die)
4594 return NULL;
4596 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4597 if (a->dw_attr == attr_kind)
4598 return a;
4599 else if (a->dw_attr == DW_AT_specification
4600 || a->dw_attr == DW_AT_abstract_origin)
4601 spec = AT_ref (a);
4603 if (spec)
4604 return get_AT (spec, attr_kind);
4606 return NULL;
4609 /* Returns the parent of the declaration of DIE. */
4611 static dw_die_ref
4612 get_die_parent (dw_die_ref die)
4614 dw_die_ref t;
4616 if (!die)
4617 return NULL;
4619 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4620 || (t = get_AT_ref (die, DW_AT_specification)))
4621 die = t;
4623 return die->die_parent;
4626 /* Return the "low pc" attribute value, typically associated with a subprogram
4627 DIE. Return null if the "low pc" attribute is either not present, or if it
4628 cannot be represented as an assembler label identifier. */
4630 static inline const char *
4631 get_AT_low_pc (dw_die_ref die)
4633 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4635 return a ? AT_lbl (a) : NULL;
4638 /* Return the "high pc" attribute value, typically associated with a subprogram
4639 DIE. Return null if the "high pc" attribute is either not present, or if it
4640 cannot be represented as an assembler label identifier. */
4642 static inline const char *
4643 get_AT_hi_pc (dw_die_ref die)
4645 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4647 return a ? AT_lbl (a) : NULL;
4650 /* Return the value of the string attribute designated by ATTR_KIND, or
4651 NULL if it is not present. */
4653 static inline const char *
4654 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4656 dw_attr_ref a = get_AT (die, attr_kind);
4658 return a ? AT_string (a) : NULL;
4661 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4662 if it is not present. */
4664 static inline int
4665 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4667 dw_attr_ref a = get_AT (die, attr_kind);
4669 return a ? AT_flag (a) : 0;
4672 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4673 if it is not present. */
4675 static inline unsigned
4676 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4678 dw_attr_ref a = get_AT (die, attr_kind);
4680 return a ? AT_unsigned (a) : 0;
4683 static inline dw_die_ref
4684 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4686 dw_attr_ref a = get_AT (die, attr_kind);
4688 return a ? AT_ref (a) : NULL;
4691 static inline struct dwarf_file_data *
4692 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4694 dw_attr_ref a = get_AT (die, attr_kind);
4696 return a ? AT_file (a) : NULL;
4699 /* Return TRUE if the language is C++. */
4701 static inline bool
4702 is_cxx (void)
4704 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4706 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4707 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4710 /* Return TRUE if the language is Java. */
4712 static inline bool
4713 is_java (void)
4715 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4717 return lang == DW_LANG_Java;
4720 /* Return TRUE if the language is Fortran. */
4722 static inline bool
4723 is_fortran (void)
4725 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4727 return (lang == DW_LANG_Fortran77
4728 || lang == DW_LANG_Fortran90
4729 || lang == DW_LANG_Fortran95
4730 || lang == DW_LANG_Fortran03
4731 || lang == DW_LANG_Fortran08);
4734 /* Return TRUE if the language is Ada. */
4736 static inline bool
4737 is_ada (void)
4739 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4741 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4744 /* Remove the specified attribute if present. Return TRUE if removal
4745 was successful. */
4747 static bool
4748 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4750 dw_attr_ref a;
4751 unsigned ix;
4753 if (! die)
4754 return false;
4756 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4757 if (a->dw_attr == attr_kind)
4759 if (AT_class (a) == dw_val_class_str)
4760 if (a->dw_attr_val.v.val_str->refcount)
4761 a->dw_attr_val.v.val_str->refcount--;
4763 /* vec::ordered_remove should help reduce the number of abbrevs
4764 that are needed. */
4765 die->die_attr->ordered_remove (ix);
4766 return true;
4768 return false;
4771 /* Remove CHILD from its parent. PREV must have the property that
4772 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4774 static void
4775 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4777 gcc_assert (child->die_parent == prev->die_parent);
4778 gcc_assert (prev->die_sib == child);
4779 if (prev == child)
4781 gcc_assert (child->die_parent->die_child == child);
4782 prev = NULL;
4784 else
4785 prev->die_sib = child->die_sib;
4786 if (child->die_parent->die_child == child)
4787 child->die_parent->die_child = prev;
4790 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4791 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4793 static void
4794 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4796 dw_die_ref parent = old_child->die_parent;
4798 gcc_assert (parent == prev->die_parent);
4799 gcc_assert (prev->die_sib == old_child);
4801 new_child->die_parent = parent;
4802 if (prev == old_child)
4804 gcc_assert (parent->die_child == old_child);
4805 new_child->die_sib = new_child;
4807 else
4809 prev->die_sib = new_child;
4810 new_child->die_sib = old_child->die_sib;
4812 if (old_child->die_parent->die_child == old_child)
4813 old_child->die_parent->die_child = new_child;
4816 /* Move all children from OLD_PARENT to NEW_PARENT. */
4818 static void
4819 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4821 dw_die_ref c;
4822 new_parent->die_child = old_parent->die_child;
4823 old_parent->die_child = NULL;
4824 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4827 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4828 matches TAG. */
4830 static void
4831 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4833 dw_die_ref c;
4835 c = die->die_child;
4836 if (c) do {
4837 dw_die_ref prev = c;
4838 c = c->die_sib;
4839 while (c->die_tag == tag)
4841 remove_child_with_prev (c, prev);
4842 c->die_parent = NULL;
4843 /* Might have removed every child. */
4844 if (c == c->die_sib)
4845 return;
4846 c = c->die_sib;
4848 } while (c != die->die_child);
4851 /* Add a CHILD_DIE as the last child of DIE. */
4853 static void
4854 add_child_die (dw_die_ref die, dw_die_ref child_die)
4856 /* FIXME this should probably be an assert. */
4857 if (! die || ! child_die)
4858 return;
4859 gcc_assert (die != child_die);
4861 child_die->die_parent = die;
4862 if (die->die_child)
4864 child_die->die_sib = die->die_child->die_sib;
4865 die->die_child->die_sib = child_die;
4867 else
4868 child_die->die_sib = child_die;
4869 die->die_child = child_die;
4872 /* Unassociate CHILD from its parent, and make its parent be
4873 NEW_PARENT. */
4875 static void
4876 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4878 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4879 if (p->die_sib == child)
4881 remove_child_with_prev (child, p);
4882 break;
4884 add_child_die (new_parent, child);
4887 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4888 is the specification, to the end of PARENT's list of children.
4889 This is done by removing and re-adding it. */
4891 static void
4892 splice_child_die (dw_die_ref parent, dw_die_ref child)
4894 /* We want the declaration DIE from inside the class, not the
4895 specification DIE at toplevel. */
4896 if (child->die_parent != parent)
4898 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4900 if (tmp)
4901 child = tmp;
4904 gcc_assert (child->die_parent == parent
4905 || (child->die_parent
4906 == get_AT_ref (parent, DW_AT_specification)));
4908 reparent_child (child, parent);
4911 /* Create and return a new die with a parent of PARENT_DIE. If
4912 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4913 associated tree T must be supplied to determine parenthood
4914 later. */
4916 static inline dw_die_ref
4917 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4919 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4921 die->die_tag = tag_value;
4923 if (parent_die != NULL)
4924 add_child_die (parent_die, die);
4925 else
4927 limbo_die_node *limbo_node;
4929 /* No DIEs created after early dwarf should end up in limbo,
4930 because the limbo list should not persist past LTO
4931 streaming. */
4932 if (tag_value != DW_TAG_compile_unit
4933 /* These are allowed because they're generated while
4934 breaking out COMDAT units late. */
4935 && tag_value != DW_TAG_type_unit
4936 && !early_dwarf
4937 /* Allow nested functions to live in limbo because they will
4938 only temporarily live there, as decls_for_scope will fix
4939 them up. */
4940 && (TREE_CODE (t) != FUNCTION_DECL
4941 || !decl_function_context (t))
4942 /* Same as nested functions above but for types. Types that
4943 are local to a function will be fixed in
4944 decls_for_scope. */
4945 && (!RECORD_OR_UNION_TYPE_P (t)
4946 || !TYPE_CONTEXT (t)
4947 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4948 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4949 especially in the ltrans stage, but once we implement LTO
4950 dwarf streaming, we should remove this exception. */
4951 && !in_lto_p)
4953 fprintf (stderr, "symbol ended up in limbo too late:");
4954 debug_generic_stmt (t);
4955 gcc_unreachable ();
4958 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4959 limbo_node->die = die;
4960 limbo_node->created_for = t;
4961 limbo_node->next = limbo_die_list;
4962 limbo_die_list = limbo_node;
4965 return die;
4968 /* Return the DIE associated with the given type specifier. */
4970 static inline dw_die_ref
4971 lookup_type_die (tree type)
4973 return TYPE_SYMTAB_DIE (type);
4976 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4977 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4978 anonymous type instead the one of the naming typedef. */
4980 static inline dw_die_ref
4981 strip_naming_typedef (tree type, dw_die_ref type_die)
4983 if (type
4984 && TREE_CODE (type) == RECORD_TYPE
4985 && type_die
4986 && type_die->die_tag == DW_TAG_typedef
4987 && is_naming_typedef_decl (TYPE_NAME (type)))
4988 type_die = get_AT_ref (type_die, DW_AT_type);
4989 return type_die;
4992 /* Like lookup_type_die, but if type is an anonymous type named by a
4993 typedef[1], return the DIE of the anonymous type instead the one of
4994 the naming typedef. This is because in gen_typedef_die, we did
4995 equate the anonymous struct named by the typedef with the DIE of
4996 the naming typedef. So by default, lookup_type_die on an anonymous
4997 struct yields the DIE of the naming typedef.
4999 [1]: Read the comment of is_naming_typedef_decl to learn about what
5000 a naming typedef is. */
5002 static inline dw_die_ref
5003 lookup_type_die_strip_naming_typedef (tree type)
5005 dw_die_ref die = lookup_type_die (type);
5006 return strip_naming_typedef (type, die);
5009 /* Equate a DIE to a given type specifier. */
5011 static inline void
5012 equate_type_number_to_die (tree type, dw_die_ref type_die)
5014 TYPE_SYMTAB_DIE (type) = type_die;
5017 /* Returns a hash value for X (which really is a die_struct). */
5019 inline hashval_t
5020 decl_die_hasher::hash (die_node *x)
5022 return (hashval_t) x->decl_id;
5025 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5027 inline bool
5028 decl_die_hasher::equal (die_node *x, tree y)
5030 return (x->decl_id == DECL_UID (y));
5033 /* Return the DIE associated with a given declaration. */
5035 static inline dw_die_ref
5036 lookup_decl_die (tree decl)
5038 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5041 /* Returns a hash value for X (which really is a var_loc_list). */
5043 inline hashval_t
5044 decl_loc_hasher::hash (var_loc_list *x)
5046 return (hashval_t) x->decl_id;
5049 /* Return nonzero if decl_id of var_loc_list X is the same as
5050 UID of decl *Y. */
5052 inline bool
5053 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5055 return (x->decl_id == DECL_UID (y));
5058 /* Return the var_loc list associated with a given declaration. */
5060 static inline var_loc_list *
5061 lookup_decl_loc (const_tree decl)
5063 if (!decl_loc_table)
5064 return NULL;
5065 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5068 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5070 inline hashval_t
5071 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5073 return (hashval_t) x->decl_id;
5076 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5077 UID of decl *Y. */
5079 inline bool
5080 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5082 return (x->decl_id == DECL_UID (y));
5085 /* Equate a DIE to a particular declaration. */
5087 static void
5088 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5090 unsigned int decl_id = DECL_UID (decl);
5092 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5093 decl_die->decl_id = decl_id;
5096 /* Return how many bits covers PIECE EXPR_LIST. */
5098 static HOST_WIDE_INT
5099 decl_piece_bitsize (rtx piece)
5101 int ret = (int) GET_MODE (piece);
5102 if (ret)
5103 return ret;
5104 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5105 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5106 return INTVAL (XEXP (XEXP (piece, 0), 0));
5109 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5111 static rtx *
5112 decl_piece_varloc_ptr (rtx piece)
5114 if ((int) GET_MODE (piece))
5115 return &XEXP (piece, 0);
5116 else
5117 return &XEXP (XEXP (piece, 0), 1);
5120 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5121 Next is the chain of following piece nodes. */
5123 static rtx_expr_list *
5124 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5126 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5127 return alloc_EXPR_LIST (bitsize, loc_note, next);
5128 else
5129 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5130 GEN_INT (bitsize),
5131 loc_note), next);
5134 /* Return rtx that should be stored into loc field for
5135 LOC_NOTE and BITPOS/BITSIZE. */
5137 static rtx
5138 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5139 HOST_WIDE_INT bitsize)
5141 if (bitsize != -1)
5143 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5144 if (bitpos != 0)
5145 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5147 return loc_note;
5150 /* This function either modifies location piece list *DEST in
5151 place (if SRC and INNER is NULL), or copies location piece list
5152 *SRC to *DEST while modifying it. Location BITPOS is modified
5153 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5154 not copied and if needed some padding around it is added.
5155 When modifying in place, DEST should point to EXPR_LIST where
5156 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5157 to the start of the whole list and INNER points to the EXPR_LIST
5158 where earlier pieces cover PIECE_BITPOS bits. */
5160 static void
5161 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5162 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5163 HOST_WIDE_INT bitsize, rtx loc_note)
5165 HOST_WIDE_INT diff;
5166 bool copy = inner != NULL;
5168 if (copy)
5170 /* First copy all nodes preceding the current bitpos. */
5171 while (src != inner)
5173 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5174 decl_piece_bitsize (*src), NULL_RTX);
5175 dest = &XEXP (*dest, 1);
5176 src = &XEXP (*src, 1);
5179 /* Add padding if needed. */
5180 if (bitpos != piece_bitpos)
5182 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5183 copy ? NULL_RTX : *dest);
5184 dest = &XEXP (*dest, 1);
5186 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5188 gcc_assert (!copy);
5189 /* A piece with correct bitpos and bitsize already exist,
5190 just update the location for it and return. */
5191 *decl_piece_varloc_ptr (*dest) = loc_note;
5192 return;
5194 /* Add the piece that changed. */
5195 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5196 dest = &XEXP (*dest, 1);
5197 /* Skip over pieces that overlap it. */
5198 diff = bitpos - piece_bitpos + bitsize;
5199 if (!copy)
5200 src = dest;
5201 while (diff > 0 && *src)
5203 rtx piece = *src;
5204 diff -= decl_piece_bitsize (piece);
5205 if (copy)
5206 src = &XEXP (piece, 1);
5207 else
5209 *src = XEXP (piece, 1);
5210 free_EXPR_LIST_node (piece);
5213 /* Add padding if needed. */
5214 if (diff < 0 && *src)
5216 if (!copy)
5217 dest = src;
5218 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5219 dest = &XEXP (*dest, 1);
5221 if (!copy)
5222 return;
5223 /* Finally copy all nodes following it. */
5224 while (*src)
5226 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5227 decl_piece_bitsize (*src), NULL_RTX);
5228 dest = &XEXP (*dest, 1);
5229 src = &XEXP (*src, 1);
5233 /* Add a variable location node to the linked list for DECL. */
5235 static struct var_loc_node *
5236 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5238 unsigned int decl_id;
5239 var_loc_list *temp;
5240 struct var_loc_node *loc = NULL;
5241 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5243 if (TREE_CODE (decl) == VAR_DECL
5244 && DECL_HAS_DEBUG_EXPR_P (decl))
5246 tree realdecl = DECL_DEBUG_EXPR (decl);
5247 if (handled_component_p (realdecl)
5248 || (TREE_CODE (realdecl) == MEM_REF
5249 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5251 HOST_WIDE_INT maxsize;
5252 tree innerdecl;
5253 innerdecl
5254 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5255 if (!DECL_P (innerdecl)
5256 || DECL_IGNORED_P (innerdecl)
5257 || TREE_STATIC (innerdecl)
5258 || bitsize <= 0
5259 || bitpos + bitsize > 256
5260 || bitsize != maxsize)
5261 return NULL;
5262 decl = innerdecl;
5266 decl_id = DECL_UID (decl);
5267 var_loc_list **slot
5268 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5269 if (*slot == NULL)
5271 temp = ggc_cleared_alloc<var_loc_list> ();
5272 temp->decl_id = decl_id;
5273 *slot = temp;
5275 else
5276 temp = *slot;
5278 /* For PARM_DECLs try to keep around the original incoming value,
5279 even if that means we'll emit a zero-range .debug_loc entry. */
5280 if (temp->last
5281 && temp->first == temp->last
5282 && TREE_CODE (decl) == PARM_DECL
5283 && NOTE_P (temp->first->loc)
5284 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5285 && DECL_INCOMING_RTL (decl)
5286 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5287 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5288 == GET_CODE (DECL_INCOMING_RTL (decl))
5289 && prev_real_insn (temp->first->loc) == NULL_RTX
5290 && (bitsize != -1
5291 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5292 NOTE_VAR_LOCATION_LOC (loc_note))
5293 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5294 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5296 loc = ggc_cleared_alloc<var_loc_node> ();
5297 temp->first->next = loc;
5298 temp->last = loc;
5299 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5301 else if (temp->last)
5303 struct var_loc_node *last = temp->last, *unused = NULL;
5304 rtx *piece_loc = NULL, last_loc_note;
5305 HOST_WIDE_INT piece_bitpos = 0;
5306 if (last->next)
5308 last = last->next;
5309 gcc_assert (last->next == NULL);
5311 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5313 piece_loc = &last->loc;
5316 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5317 if (piece_bitpos + cur_bitsize > bitpos)
5318 break;
5319 piece_bitpos += cur_bitsize;
5320 piece_loc = &XEXP (*piece_loc, 1);
5322 while (*piece_loc);
5324 /* TEMP->LAST here is either pointer to the last but one or
5325 last element in the chained list, LAST is pointer to the
5326 last element. */
5327 if (label && strcmp (last->label, label) == 0)
5329 /* For SRA optimized variables if there weren't any real
5330 insns since last note, just modify the last node. */
5331 if (piece_loc != NULL)
5333 adjust_piece_list (piece_loc, NULL, NULL,
5334 bitpos, piece_bitpos, bitsize, loc_note);
5335 return NULL;
5337 /* If the last note doesn't cover any instructions, remove it. */
5338 if (temp->last != last)
5340 temp->last->next = NULL;
5341 unused = last;
5342 last = temp->last;
5343 gcc_assert (strcmp (last->label, label) != 0);
5345 else
5347 gcc_assert (temp->first == temp->last
5348 || (temp->first->next == temp->last
5349 && TREE_CODE (decl) == PARM_DECL));
5350 memset (temp->last, '\0', sizeof (*temp->last));
5351 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5352 return temp->last;
5355 if (bitsize == -1 && NOTE_P (last->loc))
5356 last_loc_note = last->loc;
5357 else if (piece_loc != NULL
5358 && *piece_loc != NULL_RTX
5359 && piece_bitpos == bitpos
5360 && decl_piece_bitsize (*piece_loc) == bitsize)
5361 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5362 else
5363 last_loc_note = NULL_RTX;
5364 /* If the current location is the same as the end of the list,
5365 and either both or neither of the locations is uninitialized,
5366 we have nothing to do. */
5367 if (last_loc_note == NULL_RTX
5368 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5369 NOTE_VAR_LOCATION_LOC (loc_note)))
5370 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5371 != NOTE_VAR_LOCATION_STATUS (loc_note))
5372 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5373 == VAR_INIT_STATUS_UNINITIALIZED)
5374 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5375 == VAR_INIT_STATUS_UNINITIALIZED))))
5377 /* Add LOC to the end of list and update LAST. If the last
5378 element of the list has been removed above, reuse its
5379 memory for the new node, otherwise allocate a new one. */
5380 if (unused)
5382 loc = unused;
5383 memset (loc, '\0', sizeof (*loc));
5385 else
5386 loc = ggc_cleared_alloc<var_loc_node> ();
5387 if (bitsize == -1 || piece_loc == NULL)
5388 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5389 else
5390 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5391 bitpos, piece_bitpos, bitsize, loc_note);
5392 last->next = loc;
5393 /* Ensure TEMP->LAST will point either to the new last but one
5394 element of the chain, or to the last element in it. */
5395 if (last != temp->last)
5396 temp->last = last;
5398 else if (unused)
5399 ggc_free (unused);
5401 else
5403 loc = ggc_cleared_alloc<var_loc_node> ();
5404 temp->first = loc;
5405 temp->last = loc;
5406 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5408 return loc;
5411 /* Keep track of the number of spaces used to indent the
5412 output of the debugging routines that print the structure of
5413 the DIE internal representation. */
5414 static int print_indent;
5416 /* Indent the line the number of spaces given by print_indent. */
5418 static inline void
5419 print_spaces (FILE *outfile)
5421 fprintf (outfile, "%*s", print_indent, "");
5424 /* Print a type signature in hex. */
5426 static inline void
5427 print_signature (FILE *outfile, char *sig)
5429 int i;
5431 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5432 fprintf (outfile, "%02x", sig[i] & 0xff);
5435 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5437 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5438 RECURSE, output location descriptor operations. */
5440 static void
5441 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5443 switch (val->val_class)
5445 case dw_val_class_addr:
5446 fprintf (outfile, "address");
5447 break;
5448 case dw_val_class_offset:
5449 fprintf (outfile, "offset");
5450 break;
5451 case dw_val_class_loc:
5452 fprintf (outfile, "location descriptor");
5453 if (val->v.val_loc == NULL)
5454 fprintf (outfile, " -> <null>\n");
5455 else if (recurse)
5457 fprintf (outfile, ":\n");
5458 print_indent += 4;
5459 print_loc_descr (val->v.val_loc, outfile);
5460 print_indent -= 4;
5462 else
5463 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5464 break;
5465 case dw_val_class_loc_list:
5466 fprintf (outfile, "location list -> label:%s",
5467 val->v.val_loc_list->ll_symbol);
5468 break;
5469 case dw_val_class_range_list:
5470 fprintf (outfile, "range list");
5471 break;
5472 case dw_val_class_const:
5473 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5474 break;
5475 case dw_val_class_unsigned_const:
5476 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5477 break;
5478 case dw_val_class_const_double:
5479 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5480 HOST_WIDE_INT_PRINT_UNSIGNED")",
5481 val->v.val_double.high,
5482 val->v.val_double.low);
5483 break;
5484 case dw_val_class_wide_int:
5486 int i = val->v.val_wide->get_len ();
5487 fprintf (outfile, "constant (");
5488 gcc_assert (i > 0);
5489 if (val->v.val_wide->elt (i - 1) == 0)
5490 fprintf (outfile, "0x");
5491 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5492 val->v.val_wide->elt (--i));
5493 while (--i >= 0)
5494 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5495 val->v.val_wide->elt (i));
5496 fprintf (outfile, ")");
5497 break;
5499 case dw_val_class_vec:
5500 fprintf (outfile, "floating-point or vector constant");
5501 break;
5502 case dw_val_class_flag:
5503 fprintf (outfile, "%u", val->v.val_flag);
5504 break;
5505 case dw_val_class_die_ref:
5506 if (val->v.val_die_ref.die != NULL)
5508 dw_die_ref die = val->v.val_die_ref.die;
5510 if (die->comdat_type_p)
5512 fprintf (outfile, "die -> signature: ");
5513 print_signature (outfile,
5514 die->die_id.die_type_node->signature);
5516 else if (die->die_id.die_symbol)
5517 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5518 else
5519 fprintf (outfile, "die -> %ld", die->die_offset);
5520 fprintf (outfile, " (%p)", (void *) die);
5522 else
5523 fprintf (outfile, "die -> <null>");
5524 break;
5525 case dw_val_class_vms_delta:
5526 fprintf (outfile, "delta: @slotcount(%s-%s)",
5527 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5528 break;
5529 case dw_val_class_lbl_id:
5530 case dw_val_class_lineptr:
5531 case dw_val_class_macptr:
5532 case dw_val_class_high_pc:
5533 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5534 break;
5535 case dw_val_class_str:
5536 if (val->v.val_str->str != NULL)
5537 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5538 else
5539 fprintf (outfile, "<null>");
5540 break;
5541 case dw_val_class_file:
5542 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5543 val->v.val_file->emitted_number);
5544 break;
5545 case dw_val_class_data8:
5547 int i;
5549 for (i = 0; i < 8; i++)
5550 fprintf (outfile, "%02x", val->v.val_data8[i]);
5551 break;
5553 default:
5554 break;
5558 /* Likewise, for a DIE attribute. */
5560 static void
5561 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5563 print_dw_val (&a->dw_attr_val, recurse, outfile);
5567 /* Print the list of operands in the LOC location description to OUTFILE. This
5568 routine is a debugging aid only. */
5570 static void
5571 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5573 dw_loc_descr_ref l = loc;
5575 if (loc == NULL)
5577 print_spaces (outfile);
5578 fprintf (outfile, "<null>\n");
5579 return;
5582 for (l = loc; l != NULL; l = l->dw_loc_next)
5584 print_spaces (outfile);
5585 fprintf (outfile, "(%p) %s",
5586 (void *) l,
5587 dwarf_stack_op_name (l->dw_loc_opc));
5588 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5590 fprintf (outfile, " ");
5591 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5593 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5595 fprintf (outfile, ", ");
5596 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5598 fprintf (outfile, "\n");
5602 /* Print the information associated with a given DIE, and its children.
5603 This routine is a debugging aid only. */
5605 static void
5606 print_die (dw_die_ref die, FILE *outfile)
5608 dw_attr_ref a;
5609 dw_die_ref c;
5610 unsigned ix;
5612 print_spaces (outfile);
5613 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5614 die->die_offset, dwarf_tag_name (die->die_tag),
5615 (void*) die);
5616 print_spaces (outfile);
5617 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5618 fprintf (outfile, " offset: %ld", die->die_offset);
5619 fprintf (outfile, " mark: %d\n", die->die_mark);
5621 if (die->comdat_type_p)
5623 print_spaces (outfile);
5624 fprintf (outfile, " signature: ");
5625 print_signature (outfile, die->die_id.die_type_node->signature);
5626 fprintf (outfile, "\n");
5629 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5631 print_spaces (outfile);
5632 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5634 print_attribute (a, true, outfile);
5635 fprintf (outfile, "\n");
5638 if (die->die_child != NULL)
5640 print_indent += 4;
5641 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5642 print_indent -= 4;
5644 if (print_indent == 0)
5645 fprintf (outfile, "\n");
5648 /* Print the list of operations in the LOC location description. */
5650 DEBUG_FUNCTION void
5651 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5653 print_loc_descr (loc, stderr);
5656 /* Print the information collected for a given DIE. */
5658 DEBUG_FUNCTION void
5659 debug_dwarf_die (dw_die_ref die)
5661 print_die (die, stderr);
5664 DEBUG_FUNCTION void
5665 debug (die_struct &ref)
5667 print_die (&ref, stderr);
5670 DEBUG_FUNCTION void
5671 debug (die_struct *ptr)
5673 if (ptr)
5674 debug (*ptr);
5675 else
5676 fprintf (stderr, "<nil>\n");
5680 /* Print all DWARF information collected for the compilation unit.
5681 This routine is a debugging aid only. */
5683 DEBUG_FUNCTION void
5684 debug_dwarf (void)
5686 print_indent = 0;
5687 print_die (comp_unit_die (), stderr);
5690 /* Sanity checks on DIEs. */
5692 static void
5693 check_die (dw_die_ref die)
5695 /* A debugging information entry that is a member of an abstract
5696 instance tree [that has DW_AT_inline] should not contain any
5697 attributes which describe aspects of the subroutine which vary
5698 between distinct inlined expansions or distinct out-of-line
5699 expansions. */
5700 unsigned ix;
5701 dw_attr_ref a;
5702 bool inline_found = false;
5703 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5704 if (a->dw_attr == DW_AT_inline && a->dw_attr_val.v.val_unsigned)
5705 inline_found = true;
5706 if (inline_found)
5708 /* Catch the most common mistakes. */
5709 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5710 gcc_assert (a->dw_attr != DW_AT_low_pc
5711 && a->dw_attr != DW_AT_high_pc
5712 && a->dw_attr != DW_AT_location
5713 && a->dw_attr != DW_AT_frame_base
5714 && a->dw_attr != DW_AT_GNU_all_call_sites);
5718 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5719 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5720 DIE that marks the start of the DIEs for this include file. */
5722 static dw_die_ref
5723 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5725 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5726 dw_die_ref new_unit = gen_compile_unit_die (filename);
5728 new_unit->die_sib = old_unit;
5729 return new_unit;
5732 /* Close an include-file CU and reopen the enclosing one. */
5734 static dw_die_ref
5735 pop_compile_unit (dw_die_ref old_unit)
5737 dw_die_ref new_unit = old_unit->die_sib;
5739 old_unit->die_sib = NULL;
5740 return new_unit;
5743 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5744 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5745 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5747 /* Calculate the checksum of a location expression. */
5749 static inline void
5750 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5752 int tem;
5753 inchash::hash hstate;
5754 hashval_t hash;
5756 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5757 CHECKSUM (tem);
5758 hash_loc_operands (loc, hstate);
5759 hash = hstate.end();
5760 CHECKSUM (hash);
5763 /* Calculate the checksum of an attribute. */
5765 static void
5766 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5768 dw_loc_descr_ref loc;
5769 rtx r;
5771 CHECKSUM (at->dw_attr);
5773 /* We don't care that this was compiled with a different compiler
5774 snapshot; if the output is the same, that's what matters. */
5775 if (at->dw_attr == DW_AT_producer)
5776 return;
5778 switch (AT_class (at))
5780 case dw_val_class_const:
5781 CHECKSUM (at->dw_attr_val.v.val_int);
5782 break;
5783 case dw_val_class_unsigned_const:
5784 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5785 break;
5786 case dw_val_class_const_double:
5787 CHECKSUM (at->dw_attr_val.v.val_double);
5788 break;
5789 case dw_val_class_wide_int:
5790 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5791 get_full_len (*at->dw_attr_val.v.val_wide)
5792 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5793 break;
5794 case dw_val_class_vec:
5795 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5796 (at->dw_attr_val.v.val_vec.length
5797 * at->dw_attr_val.v.val_vec.elt_size));
5798 break;
5799 case dw_val_class_flag:
5800 CHECKSUM (at->dw_attr_val.v.val_flag);
5801 break;
5802 case dw_val_class_str:
5803 CHECKSUM_STRING (AT_string (at));
5804 break;
5806 case dw_val_class_addr:
5807 r = AT_addr (at);
5808 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5809 CHECKSUM_STRING (XSTR (r, 0));
5810 break;
5812 case dw_val_class_offset:
5813 CHECKSUM (at->dw_attr_val.v.val_offset);
5814 break;
5816 case dw_val_class_loc:
5817 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5818 loc_checksum (loc, ctx);
5819 break;
5821 case dw_val_class_die_ref:
5822 die_checksum (AT_ref (at), ctx, mark);
5823 break;
5825 case dw_val_class_fde_ref:
5826 case dw_val_class_vms_delta:
5827 case dw_val_class_lbl_id:
5828 case dw_val_class_lineptr:
5829 case dw_val_class_macptr:
5830 case dw_val_class_high_pc:
5831 break;
5833 case dw_val_class_file:
5834 CHECKSUM_STRING (AT_file (at)->filename);
5835 break;
5837 case dw_val_class_data8:
5838 CHECKSUM (at->dw_attr_val.v.val_data8);
5839 break;
5841 default:
5842 break;
5846 /* Calculate the checksum of a DIE. */
5848 static void
5849 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5851 dw_die_ref c;
5852 dw_attr_ref a;
5853 unsigned ix;
5855 /* To avoid infinite recursion. */
5856 if (die->die_mark)
5858 CHECKSUM (die->die_mark);
5859 return;
5861 die->die_mark = ++(*mark);
5863 CHECKSUM (die->die_tag);
5865 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5866 attr_checksum (a, ctx, mark);
5868 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5871 #undef CHECKSUM
5872 #undef CHECKSUM_BLOCK
5873 #undef CHECKSUM_STRING
5875 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5876 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5877 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5878 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5879 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5880 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5881 #define CHECKSUM_ATTR(FOO) \
5882 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5884 /* Calculate the checksum of a number in signed LEB128 format. */
5886 static void
5887 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5889 unsigned char byte;
5890 bool more;
5892 while (1)
5894 byte = (value & 0x7f);
5895 value >>= 7;
5896 more = !((value == 0 && (byte & 0x40) == 0)
5897 || (value == -1 && (byte & 0x40) != 0));
5898 if (more)
5899 byte |= 0x80;
5900 CHECKSUM (byte);
5901 if (!more)
5902 break;
5906 /* Calculate the checksum of a number in unsigned LEB128 format. */
5908 static void
5909 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5911 while (1)
5913 unsigned char byte = (value & 0x7f);
5914 value >>= 7;
5915 if (value != 0)
5916 /* More bytes to follow. */
5917 byte |= 0x80;
5918 CHECKSUM (byte);
5919 if (value == 0)
5920 break;
5924 /* Checksum the context of the DIE. This adds the names of any
5925 surrounding namespaces or structures to the checksum. */
5927 static void
5928 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5930 const char *name;
5931 dw_die_ref spec;
5932 int tag = die->die_tag;
5934 if (tag != DW_TAG_namespace
5935 && tag != DW_TAG_structure_type
5936 && tag != DW_TAG_class_type)
5937 return;
5939 name = get_AT_string (die, DW_AT_name);
5941 spec = get_AT_ref (die, DW_AT_specification);
5942 if (spec != NULL)
5943 die = spec;
5945 if (die->die_parent != NULL)
5946 checksum_die_context (die->die_parent, ctx);
5948 CHECKSUM_ULEB128 ('C');
5949 CHECKSUM_ULEB128 (tag);
5950 if (name != NULL)
5951 CHECKSUM_STRING (name);
5954 /* Calculate the checksum of a location expression. */
5956 static inline void
5957 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5959 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5960 were emitted as a DW_FORM_sdata instead of a location expression. */
5961 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5963 CHECKSUM_ULEB128 (DW_FORM_sdata);
5964 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5965 return;
5968 /* Otherwise, just checksum the raw location expression. */
5969 while (loc != NULL)
5971 inchash::hash hstate;
5972 hashval_t hash;
5974 CHECKSUM_ULEB128 (loc->dtprel);
5975 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5976 hash_loc_operands (loc, hstate);
5977 hash = hstate.end ();
5978 CHECKSUM (hash);
5979 loc = loc->dw_loc_next;
5983 /* Calculate the checksum of an attribute. */
5985 static void
5986 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5987 struct md5_ctx *ctx, int *mark)
5989 dw_loc_descr_ref loc;
5990 rtx r;
5992 if (AT_class (at) == dw_val_class_die_ref)
5994 dw_die_ref target_die = AT_ref (at);
5996 /* For pointer and reference types, we checksum only the (qualified)
5997 name of the target type (if there is a name). For friend entries,
5998 we checksum only the (qualified) name of the target type or function.
5999 This allows the checksum to remain the same whether the target type
6000 is complete or not. */
6001 if ((at->dw_attr == DW_AT_type
6002 && (tag == DW_TAG_pointer_type
6003 || tag == DW_TAG_reference_type
6004 || tag == DW_TAG_rvalue_reference_type
6005 || tag == DW_TAG_ptr_to_member_type))
6006 || (at->dw_attr == DW_AT_friend
6007 && tag == DW_TAG_friend))
6009 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6011 if (name_attr != NULL)
6013 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6015 if (decl == NULL)
6016 decl = target_die;
6017 CHECKSUM_ULEB128 ('N');
6018 CHECKSUM_ULEB128 (at->dw_attr);
6019 if (decl->die_parent != NULL)
6020 checksum_die_context (decl->die_parent, ctx);
6021 CHECKSUM_ULEB128 ('E');
6022 CHECKSUM_STRING (AT_string (name_attr));
6023 return;
6027 /* For all other references to another DIE, we check to see if the
6028 target DIE has already been visited. If it has, we emit a
6029 backward reference; if not, we descend recursively. */
6030 if (target_die->die_mark > 0)
6032 CHECKSUM_ULEB128 ('R');
6033 CHECKSUM_ULEB128 (at->dw_attr);
6034 CHECKSUM_ULEB128 (target_die->die_mark);
6036 else
6038 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6040 if (decl == NULL)
6041 decl = target_die;
6042 target_die->die_mark = ++(*mark);
6043 CHECKSUM_ULEB128 ('T');
6044 CHECKSUM_ULEB128 (at->dw_attr);
6045 if (decl->die_parent != NULL)
6046 checksum_die_context (decl->die_parent, ctx);
6047 die_checksum_ordered (target_die, ctx, mark);
6049 return;
6052 CHECKSUM_ULEB128 ('A');
6053 CHECKSUM_ULEB128 (at->dw_attr);
6055 switch (AT_class (at))
6057 case dw_val_class_const:
6058 CHECKSUM_ULEB128 (DW_FORM_sdata);
6059 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6060 break;
6062 case dw_val_class_unsigned_const:
6063 CHECKSUM_ULEB128 (DW_FORM_sdata);
6064 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6065 break;
6067 case dw_val_class_const_double:
6068 CHECKSUM_ULEB128 (DW_FORM_block);
6069 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6070 CHECKSUM (at->dw_attr_val.v.val_double);
6071 break;
6073 case dw_val_class_wide_int:
6074 CHECKSUM_ULEB128 (DW_FORM_block);
6075 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6076 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6077 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6078 get_full_len (*at->dw_attr_val.v.val_wide)
6079 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6080 break;
6082 case dw_val_class_vec:
6083 CHECKSUM_ULEB128 (DW_FORM_block);
6084 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6085 * at->dw_attr_val.v.val_vec.elt_size);
6086 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6087 (at->dw_attr_val.v.val_vec.length
6088 * at->dw_attr_val.v.val_vec.elt_size));
6089 break;
6091 case dw_val_class_flag:
6092 CHECKSUM_ULEB128 (DW_FORM_flag);
6093 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6094 break;
6096 case dw_val_class_str:
6097 CHECKSUM_ULEB128 (DW_FORM_string);
6098 CHECKSUM_STRING (AT_string (at));
6099 break;
6101 case dw_val_class_addr:
6102 r = AT_addr (at);
6103 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6104 CHECKSUM_ULEB128 (DW_FORM_string);
6105 CHECKSUM_STRING (XSTR (r, 0));
6106 break;
6108 case dw_val_class_offset:
6109 CHECKSUM_ULEB128 (DW_FORM_sdata);
6110 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6111 break;
6113 case dw_val_class_loc:
6114 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6115 loc_checksum_ordered (loc, ctx);
6116 break;
6118 case dw_val_class_fde_ref:
6119 case dw_val_class_lbl_id:
6120 case dw_val_class_lineptr:
6121 case dw_val_class_macptr:
6122 case dw_val_class_high_pc:
6123 break;
6125 case dw_val_class_file:
6126 CHECKSUM_ULEB128 (DW_FORM_string);
6127 CHECKSUM_STRING (AT_file (at)->filename);
6128 break;
6130 case dw_val_class_data8:
6131 CHECKSUM (at->dw_attr_val.v.val_data8);
6132 break;
6134 default:
6135 break;
6139 struct checksum_attributes
6141 dw_attr_ref at_name;
6142 dw_attr_ref at_type;
6143 dw_attr_ref at_friend;
6144 dw_attr_ref at_accessibility;
6145 dw_attr_ref at_address_class;
6146 dw_attr_ref at_allocated;
6147 dw_attr_ref at_artificial;
6148 dw_attr_ref at_associated;
6149 dw_attr_ref at_binary_scale;
6150 dw_attr_ref at_bit_offset;
6151 dw_attr_ref at_bit_size;
6152 dw_attr_ref at_bit_stride;
6153 dw_attr_ref at_byte_size;
6154 dw_attr_ref at_byte_stride;
6155 dw_attr_ref at_const_value;
6156 dw_attr_ref at_containing_type;
6157 dw_attr_ref at_count;
6158 dw_attr_ref at_data_location;
6159 dw_attr_ref at_data_member_location;
6160 dw_attr_ref at_decimal_scale;
6161 dw_attr_ref at_decimal_sign;
6162 dw_attr_ref at_default_value;
6163 dw_attr_ref at_digit_count;
6164 dw_attr_ref at_discr;
6165 dw_attr_ref at_discr_list;
6166 dw_attr_ref at_discr_value;
6167 dw_attr_ref at_encoding;
6168 dw_attr_ref at_endianity;
6169 dw_attr_ref at_explicit;
6170 dw_attr_ref at_is_optional;
6171 dw_attr_ref at_location;
6172 dw_attr_ref at_lower_bound;
6173 dw_attr_ref at_mutable;
6174 dw_attr_ref at_ordering;
6175 dw_attr_ref at_picture_string;
6176 dw_attr_ref at_prototyped;
6177 dw_attr_ref at_small;
6178 dw_attr_ref at_segment;
6179 dw_attr_ref at_string_length;
6180 dw_attr_ref at_threads_scaled;
6181 dw_attr_ref at_upper_bound;
6182 dw_attr_ref at_use_location;
6183 dw_attr_ref at_use_UTF8;
6184 dw_attr_ref at_variable_parameter;
6185 dw_attr_ref at_virtuality;
6186 dw_attr_ref at_visibility;
6187 dw_attr_ref at_vtable_elem_location;
6190 /* Collect the attributes that we will want to use for the checksum. */
6192 static void
6193 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6195 dw_attr_ref a;
6196 unsigned ix;
6198 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6200 switch (a->dw_attr)
6202 case DW_AT_name:
6203 attrs->at_name = a;
6204 break;
6205 case DW_AT_type:
6206 attrs->at_type = a;
6207 break;
6208 case DW_AT_friend:
6209 attrs->at_friend = a;
6210 break;
6211 case DW_AT_accessibility:
6212 attrs->at_accessibility = a;
6213 break;
6214 case DW_AT_address_class:
6215 attrs->at_address_class = a;
6216 break;
6217 case DW_AT_allocated:
6218 attrs->at_allocated = a;
6219 break;
6220 case DW_AT_artificial:
6221 attrs->at_artificial = a;
6222 break;
6223 case DW_AT_associated:
6224 attrs->at_associated = a;
6225 break;
6226 case DW_AT_binary_scale:
6227 attrs->at_binary_scale = a;
6228 break;
6229 case DW_AT_bit_offset:
6230 attrs->at_bit_offset = a;
6231 break;
6232 case DW_AT_bit_size:
6233 attrs->at_bit_size = a;
6234 break;
6235 case DW_AT_bit_stride:
6236 attrs->at_bit_stride = a;
6237 break;
6238 case DW_AT_byte_size:
6239 attrs->at_byte_size = a;
6240 break;
6241 case DW_AT_byte_stride:
6242 attrs->at_byte_stride = a;
6243 break;
6244 case DW_AT_const_value:
6245 attrs->at_const_value = a;
6246 break;
6247 case DW_AT_containing_type:
6248 attrs->at_containing_type = a;
6249 break;
6250 case DW_AT_count:
6251 attrs->at_count = a;
6252 break;
6253 case DW_AT_data_location:
6254 attrs->at_data_location = a;
6255 break;
6256 case DW_AT_data_member_location:
6257 attrs->at_data_member_location = a;
6258 break;
6259 case DW_AT_decimal_scale:
6260 attrs->at_decimal_scale = a;
6261 break;
6262 case DW_AT_decimal_sign:
6263 attrs->at_decimal_sign = a;
6264 break;
6265 case DW_AT_default_value:
6266 attrs->at_default_value = a;
6267 break;
6268 case DW_AT_digit_count:
6269 attrs->at_digit_count = a;
6270 break;
6271 case DW_AT_discr:
6272 attrs->at_discr = a;
6273 break;
6274 case DW_AT_discr_list:
6275 attrs->at_discr_list = a;
6276 break;
6277 case DW_AT_discr_value:
6278 attrs->at_discr_value = a;
6279 break;
6280 case DW_AT_encoding:
6281 attrs->at_encoding = a;
6282 break;
6283 case DW_AT_endianity:
6284 attrs->at_endianity = a;
6285 break;
6286 case DW_AT_explicit:
6287 attrs->at_explicit = a;
6288 break;
6289 case DW_AT_is_optional:
6290 attrs->at_is_optional = a;
6291 break;
6292 case DW_AT_location:
6293 attrs->at_location = a;
6294 break;
6295 case DW_AT_lower_bound:
6296 attrs->at_lower_bound = a;
6297 break;
6298 case DW_AT_mutable:
6299 attrs->at_mutable = a;
6300 break;
6301 case DW_AT_ordering:
6302 attrs->at_ordering = a;
6303 break;
6304 case DW_AT_picture_string:
6305 attrs->at_picture_string = a;
6306 break;
6307 case DW_AT_prototyped:
6308 attrs->at_prototyped = a;
6309 break;
6310 case DW_AT_small:
6311 attrs->at_small = a;
6312 break;
6313 case DW_AT_segment:
6314 attrs->at_segment = a;
6315 break;
6316 case DW_AT_string_length:
6317 attrs->at_string_length = a;
6318 break;
6319 case DW_AT_threads_scaled:
6320 attrs->at_threads_scaled = a;
6321 break;
6322 case DW_AT_upper_bound:
6323 attrs->at_upper_bound = a;
6324 break;
6325 case DW_AT_use_location:
6326 attrs->at_use_location = a;
6327 break;
6328 case DW_AT_use_UTF8:
6329 attrs->at_use_UTF8 = a;
6330 break;
6331 case DW_AT_variable_parameter:
6332 attrs->at_variable_parameter = a;
6333 break;
6334 case DW_AT_virtuality:
6335 attrs->at_virtuality = a;
6336 break;
6337 case DW_AT_visibility:
6338 attrs->at_visibility = a;
6339 break;
6340 case DW_AT_vtable_elem_location:
6341 attrs->at_vtable_elem_location = a;
6342 break;
6343 default:
6344 break;
6349 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6351 static void
6352 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6354 dw_die_ref c;
6355 dw_die_ref decl;
6356 struct checksum_attributes attrs;
6358 CHECKSUM_ULEB128 ('D');
6359 CHECKSUM_ULEB128 (die->die_tag);
6361 memset (&attrs, 0, sizeof (attrs));
6363 decl = get_AT_ref (die, DW_AT_specification);
6364 if (decl != NULL)
6365 collect_checksum_attributes (&attrs, decl);
6366 collect_checksum_attributes (&attrs, die);
6368 CHECKSUM_ATTR (attrs.at_name);
6369 CHECKSUM_ATTR (attrs.at_accessibility);
6370 CHECKSUM_ATTR (attrs.at_address_class);
6371 CHECKSUM_ATTR (attrs.at_allocated);
6372 CHECKSUM_ATTR (attrs.at_artificial);
6373 CHECKSUM_ATTR (attrs.at_associated);
6374 CHECKSUM_ATTR (attrs.at_binary_scale);
6375 CHECKSUM_ATTR (attrs.at_bit_offset);
6376 CHECKSUM_ATTR (attrs.at_bit_size);
6377 CHECKSUM_ATTR (attrs.at_bit_stride);
6378 CHECKSUM_ATTR (attrs.at_byte_size);
6379 CHECKSUM_ATTR (attrs.at_byte_stride);
6380 CHECKSUM_ATTR (attrs.at_const_value);
6381 CHECKSUM_ATTR (attrs.at_containing_type);
6382 CHECKSUM_ATTR (attrs.at_count);
6383 CHECKSUM_ATTR (attrs.at_data_location);
6384 CHECKSUM_ATTR (attrs.at_data_member_location);
6385 CHECKSUM_ATTR (attrs.at_decimal_scale);
6386 CHECKSUM_ATTR (attrs.at_decimal_sign);
6387 CHECKSUM_ATTR (attrs.at_default_value);
6388 CHECKSUM_ATTR (attrs.at_digit_count);
6389 CHECKSUM_ATTR (attrs.at_discr);
6390 CHECKSUM_ATTR (attrs.at_discr_list);
6391 CHECKSUM_ATTR (attrs.at_discr_value);
6392 CHECKSUM_ATTR (attrs.at_encoding);
6393 CHECKSUM_ATTR (attrs.at_endianity);
6394 CHECKSUM_ATTR (attrs.at_explicit);
6395 CHECKSUM_ATTR (attrs.at_is_optional);
6396 CHECKSUM_ATTR (attrs.at_location);
6397 CHECKSUM_ATTR (attrs.at_lower_bound);
6398 CHECKSUM_ATTR (attrs.at_mutable);
6399 CHECKSUM_ATTR (attrs.at_ordering);
6400 CHECKSUM_ATTR (attrs.at_picture_string);
6401 CHECKSUM_ATTR (attrs.at_prototyped);
6402 CHECKSUM_ATTR (attrs.at_small);
6403 CHECKSUM_ATTR (attrs.at_segment);
6404 CHECKSUM_ATTR (attrs.at_string_length);
6405 CHECKSUM_ATTR (attrs.at_threads_scaled);
6406 CHECKSUM_ATTR (attrs.at_upper_bound);
6407 CHECKSUM_ATTR (attrs.at_use_location);
6408 CHECKSUM_ATTR (attrs.at_use_UTF8);
6409 CHECKSUM_ATTR (attrs.at_variable_parameter);
6410 CHECKSUM_ATTR (attrs.at_virtuality);
6411 CHECKSUM_ATTR (attrs.at_visibility);
6412 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6413 CHECKSUM_ATTR (attrs.at_type);
6414 CHECKSUM_ATTR (attrs.at_friend);
6416 /* Checksum the child DIEs. */
6417 c = die->die_child;
6418 if (c) do {
6419 dw_attr_ref name_attr;
6421 c = c->die_sib;
6422 name_attr = get_AT (c, DW_AT_name);
6423 if (is_template_instantiation (c))
6425 /* Ignore instantiations of member type and function templates. */
6427 else if (name_attr != NULL
6428 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6430 /* Use a shallow checksum for named nested types and member
6431 functions. */
6432 CHECKSUM_ULEB128 ('S');
6433 CHECKSUM_ULEB128 (c->die_tag);
6434 CHECKSUM_STRING (AT_string (name_attr));
6436 else
6438 /* Use a deep checksum for other children. */
6439 /* Mark this DIE so it gets processed when unmarking. */
6440 if (c->die_mark == 0)
6441 c->die_mark = -1;
6442 die_checksum_ordered (c, ctx, mark);
6444 } while (c != die->die_child);
6446 CHECKSUM_ULEB128 (0);
6449 /* Add a type name and tag to a hash. */
6450 static void
6451 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6453 CHECKSUM_ULEB128 (tag);
6454 CHECKSUM_STRING (name);
6457 #undef CHECKSUM
6458 #undef CHECKSUM_STRING
6459 #undef CHECKSUM_ATTR
6460 #undef CHECKSUM_LEB128
6461 #undef CHECKSUM_ULEB128
6463 /* Generate the type signature for DIE. This is computed by generating an
6464 MD5 checksum over the DIE's tag, its relevant attributes, and its
6465 children. Attributes that are references to other DIEs are processed
6466 by recursion, using the MARK field to prevent infinite recursion.
6467 If the DIE is nested inside a namespace or another type, we also
6468 need to include that context in the signature. The lower 64 bits
6469 of the resulting MD5 checksum comprise the signature. */
6471 static void
6472 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6474 int mark;
6475 const char *name;
6476 unsigned char checksum[16];
6477 struct md5_ctx ctx;
6478 dw_die_ref decl;
6479 dw_die_ref parent;
6481 name = get_AT_string (die, DW_AT_name);
6482 decl = get_AT_ref (die, DW_AT_specification);
6483 parent = get_die_parent (die);
6485 /* First, compute a signature for just the type name (and its surrounding
6486 context, if any. This is stored in the type unit DIE for link-time
6487 ODR (one-definition rule) checking. */
6489 if (is_cxx () && name != NULL)
6491 md5_init_ctx (&ctx);
6493 /* Checksum the names of surrounding namespaces and structures. */
6494 if (parent != NULL)
6495 checksum_die_context (parent, &ctx);
6497 /* Checksum the current DIE. */
6498 die_odr_checksum (die->die_tag, name, &ctx);
6499 md5_finish_ctx (&ctx, checksum);
6501 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6504 /* Next, compute the complete type signature. */
6506 md5_init_ctx (&ctx);
6507 mark = 1;
6508 die->die_mark = mark;
6510 /* Checksum the names of surrounding namespaces and structures. */
6511 if (parent != NULL)
6512 checksum_die_context (parent, &ctx);
6514 /* Checksum the DIE and its children. */
6515 die_checksum_ordered (die, &ctx, &mark);
6516 unmark_all_dies (die);
6517 md5_finish_ctx (&ctx, checksum);
6519 /* Store the signature in the type node and link the type DIE and the
6520 type node together. */
6521 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6522 DWARF_TYPE_SIGNATURE_SIZE);
6523 die->comdat_type_p = true;
6524 die->die_id.die_type_node = type_node;
6525 type_node->type_die = die;
6527 /* If the DIE is a specification, link its declaration to the type node
6528 as well. */
6529 if (decl != NULL)
6531 decl->comdat_type_p = true;
6532 decl->die_id.die_type_node = type_node;
6536 /* Do the location expressions look same? */
6537 static inline int
6538 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6540 return loc1->dw_loc_opc == loc2->dw_loc_opc
6541 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6542 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6545 /* Do the values look the same? */
6546 static int
6547 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6549 dw_loc_descr_ref loc1, loc2;
6550 rtx r1, r2;
6552 if (v1->val_class != v2->val_class)
6553 return 0;
6555 switch (v1->val_class)
6557 case dw_val_class_const:
6558 return v1->v.val_int == v2->v.val_int;
6559 case dw_val_class_unsigned_const:
6560 return v1->v.val_unsigned == v2->v.val_unsigned;
6561 case dw_val_class_const_double:
6562 return v1->v.val_double.high == v2->v.val_double.high
6563 && v1->v.val_double.low == v2->v.val_double.low;
6564 case dw_val_class_wide_int:
6565 return *v1->v.val_wide == *v2->v.val_wide;
6566 case dw_val_class_vec:
6567 if (v1->v.val_vec.length != v2->v.val_vec.length
6568 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6569 return 0;
6570 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6571 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6572 return 0;
6573 return 1;
6574 case dw_val_class_flag:
6575 return v1->v.val_flag == v2->v.val_flag;
6576 case dw_val_class_str:
6577 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6579 case dw_val_class_addr:
6580 r1 = v1->v.val_addr;
6581 r2 = v2->v.val_addr;
6582 if (GET_CODE (r1) != GET_CODE (r2))
6583 return 0;
6584 return !rtx_equal_p (r1, r2);
6586 case dw_val_class_offset:
6587 return v1->v.val_offset == v2->v.val_offset;
6589 case dw_val_class_loc:
6590 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6591 loc1 && loc2;
6592 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6593 if (!same_loc_p (loc1, loc2, mark))
6594 return 0;
6595 return !loc1 && !loc2;
6597 case dw_val_class_die_ref:
6598 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6600 case dw_val_class_fde_ref:
6601 case dw_val_class_vms_delta:
6602 case dw_val_class_lbl_id:
6603 case dw_val_class_lineptr:
6604 case dw_val_class_macptr:
6605 case dw_val_class_high_pc:
6606 return 1;
6608 case dw_val_class_file:
6609 return v1->v.val_file == v2->v.val_file;
6611 case dw_val_class_data8:
6612 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6614 default:
6615 return 1;
6619 /* Do the attributes look the same? */
6621 static int
6622 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6624 if (at1->dw_attr != at2->dw_attr)
6625 return 0;
6627 /* We don't care that this was compiled with a different compiler
6628 snapshot; if the output is the same, that's what matters. */
6629 if (at1->dw_attr == DW_AT_producer)
6630 return 1;
6632 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6635 /* Do the dies look the same? */
6637 static int
6638 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6640 dw_die_ref c1, c2;
6641 dw_attr_ref a1;
6642 unsigned ix;
6644 /* To avoid infinite recursion. */
6645 if (die1->die_mark)
6646 return die1->die_mark == die2->die_mark;
6647 die1->die_mark = die2->die_mark = ++(*mark);
6649 if (die1->die_tag != die2->die_tag)
6650 return 0;
6652 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6653 return 0;
6655 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6656 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6657 return 0;
6659 c1 = die1->die_child;
6660 c2 = die2->die_child;
6661 if (! c1)
6663 if (c2)
6664 return 0;
6666 else
6667 for (;;)
6669 if (!same_die_p (c1, c2, mark))
6670 return 0;
6671 c1 = c1->die_sib;
6672 c2 = c2->die_sib;
6673 if (c1 == die1->die_child)
6675 if (c2 == die2->die_child)
6676 break;
6677 else
6678 return 0;
6682 return 1;
6685 /* Do the dies look the same? Wrapper around same_die_p. */
6687 static int
6688 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6690 int mark = 0;
6691 int ret = same_die_p (die1, die2, &mark);
6693 unmark_all_dies (die1);
6694 unmark_all_dies (die2);
6696 return ret;
6699 /* The prefix to attach to symbols on DIEs in the current comdat debug
6700 info section. */
6701 static const char *comdat_symbol_id;
6703 /* The index of the current symbol within the current comdat CU. */
6704 static unsigned int comdat_symbol_number;
6706 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6707 children, and set comdat_symbol_id accordingly. */
6709 static void
6710 compute_section_prefix (dw_die_ref unit_die)
6712 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6713 const char *base = die_name ? lbasename (die_name) : "anonymous";
6714 char *name = XALLOCAVEC (char, strlen (base) + 64);
6715 char *p;
6716 int i, mark;
6717 unsigned char checksum[16];
6718 struct md5_ctx ctx;
6720 /* Compute the checksum of the DIE, then append part of it as hex digits to
6721 the name filename of the unit. */
6723 md5_init_ctx (&ctx);
6724 mark = 0;
6725 die_checksum (unit_die, &ctx, &mark);
6726 unmark_all_dies (unit_die);
6727 md5_finish_ctx (&ctx, checksum);
6729 sprintf (name, "%s.", base);
6730 clean_symbol_name (name);
6732 p = name + strlen (name);
6733 for (i = 0; i < 4; i++)
6735 sprintf (p, "%.2x", checksum[i]);
6736 p += 2;
6739 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6740 comdat_symbol_number = 0;
6743 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6745 static int
6746 is_type_die (dw_die_ref die)
6748 switch (die->die_tag)
6750 case DW_TAG_array_type:
6751 case DW_TAG_class_type:
6752 case DW_TAG_interface_type:
6753 case DW_TAG_enumeration_type:
6754 case DW_TAG_pointer_type:
6755 case DW_TAG_reference_type:
6756 case DW_TAG_rvalue_reference_type:
6757 case DW_TAG_string_type:
6758 case DW_TAG_structure_type:
6759 case DW_TAG_subroutine_type:
6760 case DW_TAG_union_type:
6761 case DW_TAG_ptr_to_member_type:
6762 case DW_TAG_set_type:
6763 case DW_TAG_subrange_type:
6764 case DW_TAG_base_type:
6765 case DW_TAG_const_type:
6766 case DW_TAG_file_type:
6767 case DW_TAG_packed_type:
6768 case DW_TAG_volatile_type:
6769 case DW_TAG_typedef:
6770 return 1;
6771 default:
6772 return 0;
6776 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6777 Basically, we want to choose the bits that are likely to be shared between
6778 compilations (types) and leave out the bits that are specific to individual
6779 compilations (functions). */
6781 static int
6782 is_comdat_die (dw_die_ref c)
6784 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6785 we do for stabs. The advantage is a greater likelihood of sharing between
6786 objects that don't include headers in the same order (and therefore would
6787 put the base types in a different comdat). jason 8/28/00 */
6789 if (c->die_tag == DW_TAG_base_type)
6790 return 0;
6792 if (c->die_tag == DW_TAG_pointer_type
6793 || c->die_tag == DW_TAG_reference_type
6794 || c->die_tag == DW_TAG_rvalue_reference_type
6795 || c->die_tag == DW_TAG_const_type
6796 || c->die_tag == DW_TAG_volatile_type)
6798 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6800 return t ? is_comdat_die (t) : 0;
6803 return is_type_die (c);
6806 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6807 compilation unit. */
6809 static int
6810 is_symbol_die (dw_die_ref c)
6812 return (is_type_die (c)
6813 || is_declaration_die (c)
6814 || c->die_tag == DW_TAG_namespace
6815 || c->die_tag == DW_TAG_module);
6818 /* Returns true iff C is a compile-unit DIE. */
6820 static inline bool
6821 is_cu_die (dw_die_ref c)
6823 return c && c->die_tag == DW_TAG_compile_unit;
6826 /* Returns true iff C is a unit DIE of some sort. */
6828 static inline bool
6829 is_unit_die (dw_die_ref c)
6831 return c && (c->die_tag == DW_TAG_compile_unit
6832 || c->die_tag == DW_TAG_partial_unit
6833 || c->die_tag == DW_TAG_type_unit);
6836 /* Returns true iff C is a namespace DIE. */
6838 static inline bool
6839 is_namespace_die (dw_die_ref c)
6841 return c && c->die_tag == DW_TAG_namespace;
6844 /* Returns true iff C is a class or structure DIE. */
6846 static inline bool
6847 is_class_die (dw_die_ref c)
6849 return c && (c->die_tag == DW_TAG_class_type
6850 || c->die_tag == DW_TAG_structure_type);
6853 /* Return non-zero if this DIE is a template parameter. */
6855 static inline bool
6856 is_template_parameter (dw_die_ref die)
6858 switch (die->die_tag)
6860 case DW_TAG_template_type_param:
6861 case DW_TAG_template_value_param:
6862 case DW_TAG_GNU_template_template_param:
6863 case DW_TAG_GNU_template_parameter_pack:
6864 return true;
6865 default:
6866 return false;
6870 /* Return non-zero if this DIE represents a template instantiation. */
6872 static inline bool
6873 is_template_instantiation (dw_die_ref die)
6875 dw_die_ref c;
6877 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6878 return false;
6879 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6880 return false;
6883 static char *
6884 gen_internal_sym (const char *prefix)
6886 char buf[256];
6888 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6889 return xstrdup (buf);
6892 /* Assign symbols to all worthy DIEs under DIE. */
6894 static void
6895 assign_symbol_names (dw_die_ref die)
6897 dw_die_ref c;
6899 if (is_symbol_die (die) && !die->comdat_type_p)
6901 if (comdat_symbol_id)
6903 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6905 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6906 comdat_symbol_id, comdat_symbol_number++);
6907 die->die_id.die_symbol = xstrdup (p);
6909 else
6910 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6913 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6916 struct cu_hash_table_entry
6918 dw_die_ref cu;
6919 unsigned min_comdat_num, max_comdat_num;
6920 struct cu_hash_table_entry *next;
6923 /* Helpers to manipulate hash table of CUs. */
6925 struct cu_hash_table_entry_hasher
6927 typedef cu_hash_table_entry *value_type;
6928 typedef die_struct *compare_type;
6929 static inline hashval_t hash (const cu_hash_table_entry *);
6930 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6931 static inline void remove (cu_hash_table_entry *);
6934 inline hashval_t
6935 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6937 return htab_hash_string (entry->cu->die_id.die_symbol);
6940 inline bool
6941 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6942 const die_struct *entry2)
6944 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6947 inline void
6948 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6950 struct cu_hash_table_entry *next;
6952 while (entry)
6954 next = entry->next;
6955 free (entry);
6956 entry = next;
6960 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6962 /* Check whether we have already seen this CU and set up SYM_NUM
6963 accordingly. */
6964 static int
6965 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6967 struct cu_hash_table_entry dummy;
6968 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6970 dummy.max_comdat_num = 0;
6972 slot = htable->find_slot_with_hash (cu,
6973 htab_hash_string (cu->die_id.die_symbol),
6974 INSERT);
6975 entry = *slot;
6977 for (; entry; last = entry, entry = entry->next)
6979 if (same_die_p_wrap (cu, entry->cu))
6980 break;
6983 if (entry)
6985 *sym_num = entry->min_comdat_num;
6986 return 1;
6989 entry = XCNEW (struct cu_hash_table_entry);
6990 entry->cu = cu;
6991 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6992 entry->next = *slot;
6993 *slot = entry;
6995 return 0;
6998 /* Record SYM_NUM to record of CU in HTABLE. */
6999 static void
7000 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7001 unsigned int sym_num)
7003 struct cu_hash_table_entry **slot, *entry;
7005 slot = htable->find_slot_with_hash (cu,
7006 htab_hash_string (cu->die_id.die_symbol),
7007 NO_INSERT);
7008 entry = *slot;
7010 entry->max_comdat_num = sym_num;
7013 /* Traverse the DIE (which is always comp_unit_die), and set up
7014 additional compilation units for each of the include files we see
7015 bracketed by BINCL/EINCL. */
7017 static void
7018 break_out_includes (dw_die_ref die)
7020 dw_die_ref c;
7021 dw_die_ref unit = NULL;
7022 limbo_die_node *node, **pnode;
7024 c = die->die_child;
7025 if (c) do {
7026 dw_die_ref prev = c;
7027 c = c->die_sib;
7028 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7029 || (unit && is_comdat_die (c)))
7031 dw_die_ref next = c->die_sib;
7033 /* This DIE is for a secondary CU; remove it from the main one. */
7034 remove_child_with_prev (c, prev);
7036 if (c->die_tag == DW_TAG_GNU_BINCL)
7037 unit = push_new_compile_unit (unit, c);
7038 else if (c->die_tag == DW_TAG_GNU_EINCL)
7039 unit = pop_compile_unit (unit);
7040 else
7041 add_child_die (unit, c);
7042 c = next;
7043 if (c == die->die_child)
7044 break;
7046 } while (c != die->die_child);
7048 #if 0
7049 /* We can only use this in debugging, since the frontend doesn't check
7050 to make sure that we leave every include file we enter. */
7051 gcc_assert (!unit);
7052 #endif
7054 assign_symbol_names (die);
7055 cu_hash_type cu_hash_table (10);
7056 for (node = limbo_die_list, pnode = &limbo_die_list;
7057 node;
7058 node = node->next)
7060 int is_dupl;
7062 compute_section_prefix (node->die);
7063 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7064 &comdat_symbol_number);
7065 assign_symbol_names (node->die);
7066 if (is_dupl)
7067 *pnode = node->next;
7068 else
7070 pnode = &node->next;
7071 record_comdat_symbol_number (node->die, &cu_hash_table,
7072 comdat_symbol_number);
7077 /* Return non-zero if this DIE is a declaration. */
7079 static int
7080 is_declaration_die (dw_die_ref die)
7082 dw_attr_ref a;
7083 unsigned ix;
7085 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7086 if (a->dw_attr == DW_AT_declaration)
7087 return 1;
7089 return 0;
7092 /* Return non-zero if this DIE is nested inside a subprogram. */
7094 static int
7095 is_nested_in_subprogram (dw_die_ref die)
7097 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7099 if (decl == NULL)
7100 decl = die;
7101 return local_scope_p (decl);
7104 /* Return non-zero if this DIE contains a defining declaration of a
7105 subprogram. */
7107 static int
7108 contains_subprogram_definition (dw_die_ref die)
7110 dw_die_ref c;
7112 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7113 return 1;
7114 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7115 return 0;
7118 /* Return non-zero if this is a type DIE that should be moved to a
7119 COMDAT .debug_types section. */
7121 static int
7122 should_move_die_to_comdat (dw_die_ref die)
7124 switch (die->die_tag)
7126 case DW_TAG_class_type:
7127 case DW_TAG_structure_type:
7128 case DW_TAG_enumeration_type:
7129 case DW_TAG_union_type:
7130 /* Don't move declarations, inlined instances, types nested in a
7131 subprogram, or types that contain subprogram definitions. */
7132 if (is_declaration_die (die)
7133 || get_AT (die, DW_AT_abstract_origin)
7134 || is_nested_in_subprogram (die)
7135 || contains_subprogram_definition (die))
7136 return 0;
7137 return 1;
7138 case DW_TAG_array_type:
7139 case DW_TAG_interface_type:
7140 case DW_TAG_pointer_type:
7141 case DW_TAG_reference_type:
7142 case DW_TAG_rvalue_reference_type:
7143 case DW_TAG_string_type:
7144 case DW_TAG_subroutine_type:
7145 case DW_TAG_ptr_to_member_type:
7146 case DW_TAG_set_type:
7147 case DW_TAG_subrange_type:
7148 case DW_TAG_base_type:
7149 case DW_TAG_const_type:
7150 case DW_TAG_file_type:
7151 case DW_TAG_packed_type:
7152 case DW_TAG_volatile_type:
7153 case DW_TAG_typedef:
7154 default:
7155 return 0;
7159 /* Make a clone of DIE. */
7161 static dw_die_ref
7162 clone_die (dw_die_ref die)
7164 dw_die_ref clone;
7165 dw_attr_ref a;
7166 unsigned ix;
7168 clone = ggc_cleared_alloc<die_node> ();
7169 clone->die_tag = die->die_tag;
7171 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7172 add_dwarf_attr (clone, a);
7174 return clone;
7177 /* Make a clone of the tree rooted at DIE. */
7179 static dw_die_ref
7180 clone_tree (dw_die_ref die)
7182 dw_die_ref c;
7183 dw_die_ref clone = clone_die (die);
7185 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7187 return clone;
7190 /* Make a clone of DIE as a declaration. */
7192 static dw_die_ref
7193 clone_as_declaration (dw_die_ref die)
7195 dw_die_ref clone;
7196 dw_die_ref decl;
7197 dw_attr_ref a;
7198 unsigned ix;
7200 /* If the DIE is already a declaration, just clone it. */
7201 if (is_declaration_die (die))
7202 return clone_die (die);
7204 /* If the DIE is a specification, just clone its declaration DIE. */
7205 decl = get_AT_ref (die, DW_AT_specification);
7206 if (decl != NULL)
7208 clone = clone_die (decl);
7209 if (die->comdat_type_p)
7210 add_AT_die_ref (clone, DW_AT_signature, die);
7211 return clone;
7214 clone = ggc_cleared_alloc<die_node> ();
7215 clone->die_tag = die->die_tag;
7217 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7219 /* We don't want to copy over all attributes.
7220 For example we don't want DW_AT_byte_size because otherwise we will no
7221 longer have a declaration and GDB will treat it as a definition. */
7223 switch (a->dw_attr)
7225 case DW_AT_abstract_origin:
7226 case DW_AT_artificial:
7227 case DW_AT_containing_type:
7228 case DW_AT_external:
7229 case DW_AT_name:
7230 case DW_AT_type:
7231 case DW_AT_virtuality:
7232 case DW_AT_linkage_name:
7233 case DW_AT_MIPS_linkage_name:
7234 add_dwarf_attr (clone, a);
7235 break;
7236 case DW_AT_byte_size:
7237 default:
7238 break;
7242 if (die->comdat_type_p)
7243 add_AT_die_ref (clone, DW_AT_signature, die);
7245 add_AT_flag (clone, DW_AT_declaration, 1);
7246 return clone;
7250 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7252 struct decl_table_entry
7254 dw_die_ref orig;
7255 dw_die_ref copy;
7258 /* Helpers to manipulate hash table of copied declarations. */
7260 /* Hashtable helpers. */
7262 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7264 typedef decl_table_entry *value_type;
7265 typedef die_struct *compare_type;
7266 static inline hashval_t hash (const decl_table_entry *);
7267 static inline bool equal (const decl_table_entry *, const die_struct *);
7270 inline hashval_t
7271 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7273 return htab_hash_pointer (entry->orig);
7276 inline bool
7277 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7278 const die_struct *entry2)
7280 return entry1->orig == entry2;
7283 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7285 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7286 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7287 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7288 to check if the ancestor has already been copied into UNIT. */
7290 static dw_die_ref
7291 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7292 decl_hash_type *decl_table)
7294 dw_die_ref parent = die->die_parent;
7295 dw_die_ref new_parent = unit;
7296 dw_die_ref copy;
7297 decl_table_entry **slot = NULL;
7298 struct decl_table_entry *entry = NULL;
7300 if (decl_table)
7302 /* Check if the entry has already been copied to UNIT. */
7303 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7304 INSERT);
7305 if (*slot != HTAB_EMPTY_ENTRY)
7307 entry = *slot;
7308 return entry->copy;
7311 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7312 entry = XCNEW (struct decl_table_entry);
7313 entry->orig = die;
7314 entry->copy = NULL;
7315 *slot = entry;
7318 if (parent != NULL)
7320 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7321 if (spec != NULL)
7322 parent = spec;
7323 if (!is_unit_die (parent))
7324 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7327 copy = clone_as_declaration (die);
7328 add_child_die (new_parent, copy);
7330 if (decl_table)
7332 /* Record the pointer to the copy. */
7333 entry->copy = copy;
7336 return copy;
7338 /* Copy the declaration context to the new type unit DIE. This includes
7339 any surrounding namespace or type declarations. If the DIE has an
7340 AT_specification attribute, it also includes attributes and children
7341 attached to the specification, and returns a pointer to the original
7342 parent of the declaration DIE. Returns NULL otherwise. */
7344 static dw_die_ref
7345 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7347 dw_die_ref decl;
7348 dw_die_ref new_decl;
7349 dw_die_ref orig_parent = NULL;
7351 decl = get_AT_ref (die, DW_AT_specification);
7352 if (decl == NULL)
7353 decl = die;
7354 else
7356 unsigned ix;
7357 dw_die_ref c;
7358 dw_attr_ref a;
7360 /* The original DIE will be changed to a declaration, and must
7361 be moved to be a child of the original declaration DIE. */
7362 orig_parent = decl->die_parent;
7364 /* Copy the type node pointer from the new DIE to the original
7365 declaration DIE so we can forward references later. */
7366 decl->comdat_type_p = true;
7367 decl->die_id.die_type_node = die->die_id.die_type_node;
7369 remove_AT (die, DW_AT_specification);
7371 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7373 if (a->dw_attr != DW_AT_name
7374 && a->dw_attr != DW_AT_declaration
7375 && a->dw_attr != DW_AT_external)
7376 add_dwarf_attr (die, a);
7379 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7382 if (decl->die_parent != NULL
7383 && !is_unit_die (decl->die_parent))
7385 new_decl = copy_ancestor_tree (unit, decl, NULL);
7386 if (new_decl != NULL)
7388 remove_AT (new_decl, DW_AT_signature);
7389 add_AT_specification (die, new_decl);
7393 return orig_parent;
7396 /* Generate the skeleton ancestor tree for the given NODE, then clone
7397 the DIE and add the clone into the tree. */
7399 static void
7400 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7402 if (node->new_die != NULL)
7403 return;
7405 node->new_die = clone_as_declaration (node->old_die);
7407 if (node->parent != NULL)
7409 generate_skeleton_ancestor_tree (node->parent);
7410 add_child_die (node->parent->new_die, node->new_die);
7414 /* Generate a skeleton tree of DIEs containing any declarations that are
7415 found in the original tree. We traverse the tree looking for declaration
7416 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7418 static void
7419 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7421 skeleton_chain_node node;
7422 dw_die_ref c;
7423 dw_die_ref first;
7424 dw_die_ref prev = NULL;
7425 dw_die_ref next = NULL;
7427 node.parent = parent;
7429 first = c = parent->old_die->die_child;
7430 if (c)
7431 next = c->die_sib;
7432 if (c) do {
7433 if (prev == NULL || prev->die_sib == c)
7434 prev = c;
7435 c = next;
7436 next = (c == first ? NULL : c->die_sib);
7437 node.old_die = c;
7438 node.new_die = NULL;
7439 if (is_declaration_die (c))
7441 if (is_template_instantiation (c))
7443 /* Instantiated templates do not need to be cloned into the
7444 type unit. Just move the DIE and its children back to
7445 the skeleton tree (in the main CU). */
7446 remove_child_with_prev (c, prev);
7447 add_child_die (parent->new_die, c);
7448 c = prev;
7450 else
7452 /* Clone the existing DIE, move the original to the skeleton
7453 tree (which is in the main CU), and put the clone, with
7454 all the original's children, where the original came from
7455 (which is about to be moved to the type unit). */
7456 dw_die_ref clone = clone_die (c);
7457 move_all_children (c, clone);
7459 /* If the original has a DW_AT_object_pointer attribute,
7460 it would now point to a child DIE just moved to the
7461 cloned tree, so we need to remove that attribute from
7462 the original. */
7463 remove_AT (c, DW_AT_object_pointer);
7465 replace_child (c, clone, prev);
7466 generate_skeleton_ancestor_tree (parent);
7467 add_child_die (parent->new_die, c);
7468 node.new_die = c;
7469 c = clone;
7472 generate_skeleton_bottom_up (&node);
7473 } while (next != NULL);
7476 /* Wrapper function for generate_skeleton_bottom_up. */
7478 static dw_die_ref
7479 generate_skeleton (dw_die_ref die)
7481 skeleton_chain_node node;
7483 node.old_die = die;
7484 node.new_die = NULL;
7485 node.parent = NULL;
7487 /* If this type definition is nested inside another type,
7488 and is not an instantiation of a template, always leave
7489 at least a declaration in its place. */
7490 if (die->die_parent != NULL
7491 && is_type_die (die->die_parent)
7492 && !is_template_instantiation (die))
7493 node.new_die = clone_as_declaration (die);
7495 generate_skeleton_bottom_up (&node);
7496 return node.new_die;
7499 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7500 declaration. The original DIE is moved to a new compile unit so that
7501 existing references to it follow it to the new location. If any of the
7502 original DIE's descendants is a declaration, we need to replace the
7503 original DIE with a skeleton tree and move the declarations back into the
7504 skeleton tree. */
7506 static dw_die_ref
7507 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7508 dw_die_ref prev)
7510 dw_die_ref skeleton, orig_parent;
7512 /* Copy the declaration context to the type unit DIE. If the returned
7513 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7514 that DIE. */
7515 orig_parent = copy_declaration_context (unit, child);
7517 skeleton = generate_skeleton (child);
7518 if (skeleton == NULL)
7519 remove_child_with_prev (child, prev);
7520 else
7522 skeleton->comdat_type_p = true;
7523 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7525 /* If the original DIE was a specification, we need to put
7526 the skeleton under the parent DIE of the declaration.
7527 This leaves the original declaration in the tree, but
7528 it will be pruned later since there are no longer any
7529 references to it. */
7530 if (orig_parent != NULL)
7532 remove_child_with_prev (child, prev);
7533 add_child_die (orig_parent, skeleton);
7535 else
7536 replace_child (child, skeleton, prev);
7539 return skeleton;
7542 /* Traverse the DIE and set up additional .debug_types sections for each
7543 type worthy of being placed in a COMDAT section. */
7545 static void
7546 break_out_comdat_types (dw_die_ref die)
7548 dw_die_ref c;
7549 dw_die_ref first;
7550 dw_die_ref prev = NULL;
7551 dw_die_ref next = NULL;
7552 dw_die_ref unit = NULL;
7554 first = c = die->die_child;
7555 if (c)
7556 next = c->die_sib;
7557 if (c) do {
7558 if (prev == NULL || prev->die_sib == c)
7559 prev = c;
7560 c = next;
7561 next = (c == first ? NULL : c->die_sib);
7562 if (should_move_die_to_comdat (c))
7564 dw_die_ref replacement;
7565 comdat_type_node_ref type_node;
7567 /* Break out nested types into their own type units. */
7568 break_out_comdat_types (c);
7570 /* Create a new type unit DIE as the root for the new tree, and
7571 add it to the list of comdat types. */
7572 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7573 add_AT_unsigned (unit, DW_AT_language,
7574 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7575 type_node = ggc_cleared_alloc<comdat_type_node> ();
7576 type_node->root_die = unit;
7577 type_node->next = comdat_type_list;
7578 comdat_type_list = type_node;
7580 /* Generate the type signature. */
7581 generate_type_signature (c, type_node);
7583 /* Copy the declaration context, attributes, and children of the
7584 declaration into the new type unit DIE, then remove this DIE
7585 from the main CU (or replace it with a skeleton if necessary). */
7586 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7587 type_node->skeleton_die = replacement;
7589 /* Add the DIE to the new compunit. */
7590 add_child_die (unit, c);
7592 if (replacement != NULL)
7593 c = replacement;
7595 else if (c->die_tag == DW_TAG_namespace
7596 || c->die_tag == DW_TAG_class_type
7597 || c->die_tag == DW_TAG_structure_type
7598 || c->die_tag == DW_TAG_union_type)
7600 /* Look for nested types that can be broken out. */
7601 break_out_comdat_types (c);
7603 } while (next != NULL);
7606 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7607 Enter all the cloned children into the hash table decl_table. */
7609 static dw_die_ref
7610 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7612 dw_die_ref c;
7613 dw_die_ref clone;
7614 struct decl_table_entry *entry;
7615 decl_table_entry **slot;
7617 if (die->die_tag == DW_TAG_subprogram)
7618 clone = clone_as_declaration (die);
7619 else
7620 clone = clone_die (die);
7622 slot = decl_table->find_slot_with_hash (die,
7623 htab_hash_pointer (die), INSERT);
7625 /* Assert that DIE isn't in the hash table yet. If it would be there
7626 before, the ancestors would be necessarily there as well, therefore
7627 clone_tree_partial wouldn't be called. */
7628 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7630 entry = XCNEW (struct decl_table_entry);
7631 entry->orig = die;
7632 entry->copy = clone;
7633 *slot = entry;
7635 if (die->die_tag != DW_TAG_subprogram)
7636 FOR_EACH_CHILD (die, c,
7637 add_child_die (clone, clone_tree_partial (c, decl_table)));
7639 return clone;
7642 /* Walk the DIE and its children, looking for references to incomplete
7643 or trivial types that are unmarked (i.e., that are not in the current
7644 type_unit). */
7646 static void
7647 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7649 dw_die_ref c;
7650 dw_attr_ref a;
7651 unsigned ix;
7653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7655 if (AT_class (a) == dw_val_class_die_ref)
7657 dw_die_ref targ = AT_ref (a);
7658 decl_table_entry **slot;
7659 struct decl_table_entry *entry;
7661 if (targ->die_mark != 0 || targ->comdat_type_p)
7662 continue;
7664 slot = decl_table->find_slot_with_hash (targ,
7665 htab_hash_pointer (targ),
7666 INSERT);
7668 if (*slot != HTAB_EMPTY_ENTRY)
7670 /* TARG has already been copied, so we just need to
7671 modify the reference to point to the copy. */
7672 entry = *slot;
7673 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7675 else
7677 dw_die_ref parent = unit;
7678 dw_die_ref copy = clone_die (targ);
7680 /* Record in DECL_TABLE that TARG has been copied.
7681 Need to do this now, before the recursive call,
7682 because DECL_TABLE may be expanded and SLOT
7683 would no longer be a valid pointer. */
7684 entry = XCNEW (struct decl_table_entry);
7685 entry->orig = targ;
7686 entry->copy = copy;
7687 *slot = entry;
7689 /* If TARG is not a declaration DIE, we need to copy its
7690 children. */
7691 if (!is_declaration_die (targ))
7693 FOR_EACH_CHILD (
7694 targ, c,
7695 add_child_die (copy,
7696 clone_tree_partial (c, decl_table)));
7699 /* Make sure the cloned tree is marked as part of the
7700 type unit. */
7701 mark_dies (copy);
7703 /* If TARG has surrounding context, copy its ancestor tree
7704 into the new type unit. */
7705 if (targ->die_parent != NULL
7706 && !is_unit_die (targ->die_parent))
7707 parent = copy_ancestor_tree (unit, targ->die_parent,
7708 decl_table);
7710 add_child_die (parent, copy);
7711 a->dw_attr_val.v.val_die_ref.die = copy;
7713 /* Make sure the newly-copied DIE is walked. If it was
7714 installed in a previously-added context, it won't
7715 get visited otherwise. */
7716 if (parent != unit)
7718 /* Find the highest point of the newly-added tree,
7719 mark each node along the way, and walk from there. */
7720 parent->die_mark = 1;
7721 while (parent->die_parent
7722 && parent->die_parent->die_mark == 0)
7724 parent = parent->die_parent;
7725 parent->die_mark = 1;
7727 copy_decls_walk (unit, parent, decl_table);
7733 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7736 /* Copy declarations for "unworthy" types into the new comdat section.
7737 Incomplete types, modified types, and certain other types aren't broken
7738 out into comdat sections of their own, so they don't have a signature,
7739 and we need to copy the declaration into the same section so that we
7740 don't have an external reference. */
7742 static void
7743 copy_decls_for_unworthy_types (dw_die_ref unit)
7745 mark_dies (unit);
7746 decl_hash_type decl_table (10);
7747 copy_decls_walk (unit, unit, &decl_table);
7748 unmark_dies (unit);
7751 /* Traverse the DIE and add a sibling attribute if it may have the
7752 effect of speeding up access to siblings. To save some space,
7753 avoid generating sibling attributes for DIE's without children. */
7755 static void
7756 add_sibling_attributes (dw_die_ref die)
7758 dw_die_ref c;
7760 if (! die->die_child)
7761 return;
7763 if (die->die_parent && die != die->die_parent->die_child)
7764 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7766 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7769 /* Output all location lists for the DIE and its children. */
7771 static void
7772 output_location_lists (dw_die_ref die)
7774 dw_die_ref c;
7775 dw_attr_ref a;
7776 unsigned ix;
7778 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7779 if (AT_class (a) == dw_val_class_loc_list)
7780 output_loc_list (AT_loc_list (a));
7782 FOR_EACH_CHILD (die, c, output_location_lists (c));
7785 /* We want to limit the number of external references, because they are
7786 larger than local references: a relocation takes multiple words, and
7787 even a sig8 reference is always eight bytes, whereas a local reference
7788 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7789 So if we encounter multiple external references to the same type DIE, we
7790 make a local typedef stub for it and redirect all references there.
7792 This is the element of the hash table for keeping track of these
7793 references. */
7795 struct external_ref
7797 dw_die_ref type;
7798 dw_die_ref stub;
7799 unsigned n_refs;
7802 /* Hashtable helpers. */
7804 struct external_ref_hasher : typed_free_remove <external_ref>
7806 typedef external_ref *value_type;
7807 typedef external_ref *compare_type;
7808 static inline hashval_t hash (const external_ref *);
7809 static inline bool equal (const external_ref *, const external_ref *);
7812 inline hashval_t
7813 external_ref_hasher::hash (const external_ref *r)
7815 dw_die_ref die = r->type;
7816 hashval_t h = 0;
7818 /* We can't use the address of the DIE for hashing, because
7819 that will make the order of the stub DIEs non-deterministic. */
7820 if (! die->comdat_type_p)
7821 /* We have a symbol; use it to compute a hash. */
7822 h = htab_hash_string (die->die_id.die_symbol);
7823 else
7825 /* We have a type signature; use a subset of the bits as the hash.
7826 The 8-byte signature is at least as large as hashval_t. */
7827 comdat_type_node_ref type_node = die->die_id.die_type_node;
7828 memcpy (&h, type_node->signature, sizeof (h));
7830 return h;
7833 inline bool
7834 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7836 return r1->type == r2->type;
7839 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7841 /* Return a pointer to the external_ref for references to DIE. */
7843 static struct external_ref *
7844 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7846 struct external_ref ref, *ref_p;
7847 external_ref **slot;
7849 ref.type = die;
7850 slot = map->find_slot (&ref, INSERT);
7851 if (*slot != HTAB_EMPTY_ENTRY)
7852 return *slot;
7854 ref_p = XCNEW (struct external_ref);
7855 ref_p->type = die;
7856 *slot = ref_p;
7857 return ref_p;
7860 /* Subroutine of optimize_external_refs, below.
7862 If we see a type skeleton, record it as our stub. If we see external
7863 references, remember how many we've seen. */
7865 static void
7866 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7868 dw_die_ref c;
7869 dw_attr_ref a;
7870 unsigned ix;
7871 struct external_ref *ref_p;
7873 if (is_type_die (die)
7874 && (c = get_AT_ref (die, DW_AT_signature)))
7876 /* This is a local skeleton; use it for local references. */
7877 ref_p = lookup_external_ref (map, c);
7878 ref_p->stub = die;
7881 /* Scan the DIE references, and remember any that refer to DIEs from
7882 other CUs (i.e. those which are not marked). */
7883 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7884 if (AT_class (a) == dw_val_class_die_ref
7885 && (c = AT_ref (a))->die_mark == 0
7886 && is_type_die (c))
7888 ref_p = lookup_external_ref (map, c);
7889 ref_p->n_refs++;
7892 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7895 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7896 points to an external_ref, DATA is the CU we're processing. If we don't
7897 already have a local stub, and we have multiple refs, build a stub. */
7900 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7902 struct external_ref *ref_p = *slot;
7904 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7906 /* We have multiple references to this type, so build a small stub.
7907 Both of these forms are a bit dodgy from the perspective of the
7908 DWARF standard, since technically they should have names. */
7909 dw_die_ref cu = data;
7910 dw_die_ref type = ref_p->type;
7911 dw_die_ref stub = NULL;
7913 if (type->comdat_type_p)
7915 /* If we refer to this type via sig8, use AT_signature. */
7916 stub = new_die (type->die_tag, cu, NULL_TREE);
7917 add_AT_die_ref (stub, DW_AT_signature, type);
7919 else
7921 /* Otherwise, use a typedef with no name. */
7922 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7923 add_AT_die_ref (stub, DW_AT_type, type);
7926 stub->die_mark++;
7927 ref_p->stub = stub;
7929 return 1;
7932 /* DIE is a unit; look through all the DIE references to see if there are
7933 any external references to types, and if so, create local stubs for
7934 them which will be applied in build_abbrev_table. This is useful because
7935 references to local DIEs are smaller. */
7937 static external_ref_hash_type *
7938 optimize_external_refs (dw_die_ref die)
7940 external_ref_hash_type *map = new external_ref_hash_type (10);
7941 optimize_external_refs_1 (die, map);
7942 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7943 return map;
7946 /* The format of each DIE (and its attribute value pairs) is encoded in an
7947 abbreviation table. This routine builds the abbreviation table and assigns
7948 a unique abbreviation id for each abbreviation entry. The children of each
7949 die are visited recursively. */
7951 static void
7952 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7954 unsigned long abbrev_id;
7955 unsigned int n_alloc;
7956 dw_die_ref c;
7957 dw_attr_ref a;
7958 unsigned ix;
7960 /* Scan the DIE references, and replace any that refer to
7961 DIEs from other CUs (i.e. those which are not marked) with
7962 the local stubs we built in optimize_external_refs. */
7963 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7964 if (AT_class (a) == dw_val_class_die_ref
7965 && (c = AT_ref (a))->die_mark == 0)
7967 struct external_ref *ref_p;
7968 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7970 ref_p = lookup_external_ref (extern_map, c);
7971 if (ref_p->stub && ref_p->stub != die)
7972 change_AT_die_ref (a, ref_p->stub);
7973 else
7974 /* We aren't changing this reference, so mark it external. */
7975 set_AT_ref_external (a, 1);
7978 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7980 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7981 dw_attr_ref die_a, abbrev_a;
7982 unsigned ix;
7983 bool ok = true;
7985 if (abbrev->die_tag != die->die_tag)
7986 continue;
7987 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7988 continue;
7990 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7991 continue;
7993 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7995 abbrev_a = &(*abbrev->die_attr)[ix];
7996 if ((abbrev_a->dw_attr != die_a->dw_attr)
7997 || (value_format (abbrev_a) != value_format (die_a)))
7999 ok = false;
8000 break;
8003 if (ok)
8004 break;
8007 if (abbrev_id >= abbrev_die_table_in_use)
8009 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8011 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8012 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8013 n_alloc);
8015 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8016 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8017 abbrev_die_table_allocated = n_alloc;
8020 ++abbrev_die_table_in_use;
8021 abbrev_die_table[abbrev_id] = die;
8024 die->die_abbrev = abbrev_id;
8025 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8028 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8030 static int
8031 constant_size (unsigned HOST_WIDE_INT value)
8033 int log;
8035 if (value == 0)
8036 log = 0;
8037 else
8038 log = floor_log2 (value);
8040 log = log / 8;
8041 log = 1 << (floor_log2 (log) + 1);
8043 return log;
8046 /* Return the size of a DIE as it is represented in the
8047 .debug_info section. */
8049 static unsigned long
8050 size_of_die (dw_die_ref die)
8052 unsigned long size = 0;
8053 dw_attr_ref a;
8054 unsigned ix;
8055 enum dwarf_form form;
8057 size += size_of_uleb128 (die->die_abbrev);
8058 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8060 switch (AT_class (a))
8062 case dw_val_class_addr:
8063 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8065 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8066 size += size_of_uleb128 (AT_index (a));
8068 else
8069 size += DWARF2_ADDR_SIZE;
8070 break;
8071 case dw_val_class_offset:
8072 size += DWARF_OFFSET_SIZE;
8073 break;
8074 case dw_val_class_loc:
8076 unsigned long lsize = size_of_locs (AT_loc (a));
8078 /* Block length. */
8079 if (dwarf_version >= 4)
8080 size += size_of_uleb128 (lsize);
8081 else
8082 size += constant_size (lsize);
8083 size += lsize;
8085 break;
8086 case dw_val_class_loc_list:
8087 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8089 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8090 size += size_of_uleb128 (AT_index (a));
8092 else
8093 size += DWARF_OFFSET_SIZE;
8094 break;
8095 case dw_val_class_range_list:
8096 size += DWARF_OFFSET_SIZE;
8097 break;
8098 case dw_val_class_const:
8099 size += size_of_sleb128 (AT_int (a));
8100 break;
8101 case dw_val_class_unsigned_const:
8103 int csize = constant_size (AT_unsigned (a));
8104 if (dwarf_version == 3
8105 && a->dw_attr == DW_AT_data_member_location
8106 && csize >= 4)
8107 size += size_of_uleb128 (AT_unsigned (a));
8108 else
8109 size += csize;
8111 break;
8112 case dw_val_class_const_double:
8113 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8114 if (HOST_BITS_PER_WIDE_INT >= 64)
8115 size++; /* block */
8116 break;
8117 case dw_val_class_wide_int:
8118 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8119 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8120 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8121 > 64)
8122 size++; /* block */
8123 break;
8124 case dw_val_class_vec:
8125 size += constant_size (a->dw_attr_val.v.val_vec.length
8126 * a->dw_attr_val.v.val_vec.elt_size)
8127 + a->dw_attr_val.v.val_vec.length
8128 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8129 break;
8130 case dw_val_class_flag:
8131 if (dwarf_version >= 4)
8132 /* Currently all add_AT_flag calls pass in 1 as last argument,
8133 so DW_FORM_flag_present can be used. If that ever changes,
8134 we'll need to use DW_FORM_flag and have some optimization
8135 in build_abbrev_table that will change those to
8136 DW_FORM_flag_present if it is set to 1 in all DIEs using
8137 the same abbrev entry. */
8138 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8139 else
8140 size += 1;
8141 break;
8142 case dw_val_class_die_ref:
8143 if (AT_ref_external (a))
8145 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8146 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8147 is sized by target address length, whereas in DWARF3
8148 it's always sized as an offset. */
8149 if (use_debug_types)
8150 size += DWARF_TYPE_SIGNATURE_SIZE;
8151 else if (dwarf_version == 2)
8152 size += DWARF2_ADDR_SIZE;
8153 else
8154 size += DWARF_OFFSET_SIZE;
8156 else
8157 size += DWARF_OFFSET_SIZE;
8158 break;
8159 case dw_val_class_fde_ref:
8160 size += DWARF_OFFSET_SIZE;
8161 break;
8162 case dw_val_class_lbl_id:
8163 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8165 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8166 size += size_of_uleb128 (AT_index (a));
8168 else
8169 size += DWARF2_ADDR_SIZE;
8170 break;
8171 case dw_val_class_lineptr:
8172 case dw_val_class_macptr:
8173 size += DWARF_OFFSET_SIZE;
8174 break;
8175 case dw_val_class_str:
8176 form = AT_string_form (a);
8177 if (form == DW_FORM_strp)
8178 size += DWARF_OFFSET_SIZE;
8179 else if (form == DW_FORM_GNU_str_index)
8180 size += size_of_uleb128 (AT_index (a));
8181 else
8182 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8183 break;
8184 case dw_val_class_file:
8185 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8186 break;
8187 case dw_val_class_data8:
8188 size += 8;
8189 break;
8190 case dw_val_class_vms_delta:
8191 size += DWARF_OFFSET_SIZE;
8192 break;
8193 case dw_val_class_high_pc:
8194 size += DWARF2_ADDR_SIZE;
8195 break;
8196 default:
8197 gcc_unreachable ();
8201 return size;
8204 /* Size the debugging information associated with a given DIE. Visits the
8205 DIE's children recursively. Updates the global variable next_die_offset, on
8206 each time through. Uses the current value of next_die_offset to update the
8207 die_offset field in each DIE. */
8209 static void
8210 calc_die_sizes (dw_die_ref die)
8212 dw_die_ref c;
8214 gcc_assert (die->die_offset == 0
8215 || (unsigned long int) die->die_offset == next_die_offset);
8216 die->die_offset = next_die_offset;
8217 next_die_offset += size_of_die (die);
8219 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8221 if (die->die_child != NULL)
8222 /* Count the null byte used to terminate sibling lists. */
8223 next_die_offset += 1;
8226 /* Size just the base type children at the start of the CU.
8227 This is needed because build_abbrev needs to size locs
8228 and sizing of type based stack ops needs to know die_offset
8229 values for the base types. */
8231 static void
8232 calc_base_type_die_sizes (void)
8234 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8235 unsigned int i;
8236 dw_die_ref base_type;
8237 #if ENABLE_ASSERT_CHECKING
8238 dw_die_ref prev = comp_unit_die ()->die_child;
8239 #endif
8241 die_offset += size_of_die (comp_unit_die ());
8242 for (i = 0; base_types.iterate (i, &base_type); i++)
8244 #if ENABLE_ASSERT_CHECKING
8245 gcc_assert (base_type->die_offset == 0
8246 && prev->die_sib == base_type
8247 && base_type->die_child == NULL
8248 && base_type->die_abbrev);
8249 prev = base_type;
8250 #endif
8251 base_type->die_offset = die_offset;
8252 die_offset += size_of_die (base_type);
8256 /* Set the marks for a die and its children. We do this so
8257 that we know whether or not a reference needs to use FORM_ref_addr; only
8258 DIEs in the same CU will be marked. We used to clear out the offset
8259 and use that as the flag, but ran into ordering problems. */
8261 static void
8262 mark_dies (dw_die_ref die)
8264 dw_die_ref c;
8266 gcc_assert (!die->die_mark);
8268 die->die_mark = 1;
8269 FOR_EACH_CHILD (die, c, mark_dies (c));
8272 /* Clear the marks for a die and its children. */
8274 static void
8275 unmark_dies (dw_die_ref die)
8277 dw_die_ref c;
8279 if (! use_debug_types)
8280 gcc_assert (die->die_mark);
8282 die->die_mark = 0;
8283 FOR_EACH_CHILD (die, c, unmark_dies (c));
8286 /* Clear the marks for a die, its children and referred dies. */
8288 static void
8289 unmark_all_dies (dw_die_ref die)
8291 dw_die_ref c;
8292 dw_attr_ref a;
8293 unsigned ix;
8295 if (!die->die_mark)
8296 return;
8297 die->die_mark = 0;
8299 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8301 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8302 if (AT_class (a) == dw_val_class_die_ref)
8303 unmark_all_dies (AT_ref (a));
8306 /* Calculate if the entry should appear in the final output file. It may be
8307 from a pruned a type. */
8309 static bool
8310 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8312 /* By limiting gnu pubnames to definitions only, gold can generate a
8313 gdb index without entries for declarations, which don't include
8314 enough information to be useful. */
8315 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8316 return false;
8318 if (table == pubname_table)
8320 /* Enumerator names are part of the pubname table, but the
8321 parent DW_TAG_enumeration_type die may have been pruned.
8322 Don't output them if that is the case. */
8323 if (p->die->die_tag == DW_TAG_enumerator &&
8324 (p->die->die_parent == NULL
8325 || !p->die->die_parent->die_perennial_p))
8326 return false;
8328 /* Everything else in the pubname table is included. */
8329 return true;
8332 /* The pubtypes table shouldn't include types that have been
8333 pruned. */
8334 return (p->die->die_offset != 0
8335 || !flag_eliminate_unused_debug_types);
8338 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8339 generated for the compilation unit. */
8341 static unsigned long
8342 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8344 unsigned long size;
8345 unsigned i;
8346 pubname_ref p;
8347 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8349 size = DWARF_PUBNAMES_HEADER_SIZE;
8350 FOR_EACH_VEC_ELT (*names, i, p)
8351 if (include_pubname_in_output (names, p))
8352 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8354 size += DWARF_OFFSET_SIZE;
8355 return size;
8358 /* Return the size of the information in the .debug_aranges section. */
8360 static unsigned long
8361 size_of_aranges (void)
8363 unsigned long size;
8365 size = DWARF_ARANGES_HEADER_SIZE;
8367 /* Count the address/length pair for this compilation unit. */
8368 if (text_section_used)
8369 size += 2 * DWARF2_ADDR_SIZE;
8370 if (cold_text_section_used)
8371 size += 2 * DWARF2_ADDR_SIZE;
8372 if (have_multiple_function_sections)
8374 unsigned fde_idx;
8375 dw_fde_ref fde;
8377 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8379 if (DECL_IGNORED_P (fde->decl))
8380 continue;
8381 if (!fde->in_std_section)
8382 size += 2 * DWARF2_ADDR_SIZE;
8383 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8384 size += 2 * DWARF2_ADDR_SIZE;
8388 /* Count the two zero words used to terminated the address range table. */
8389 size += 2 * DWARF2_ADDR_SIZE;
8390 return size;
8393 /* Select the encoding of an attribute value. */
8395 static enum dwarf_form
8396 value_format (dw_attr_ref a)
8398 switch (AT_class (a))
8400 case dw_val_class_addr:
8401 /* Only very few attributes allow DW_FORM_addr. */
8402 switch (a->dw_attr)
8404 case DW_AT_low_pc:
8405 case DW_AT_high_pc:
8406 case DW_AT_entry_pc:
8407 case DW_AT_trampoline:
8408 return (AT_index (a) == NOT_INDEXED
8409 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8410 default:
8411 break;
8413 switch (DWARF2_ADDR_SIZE)
8415 case 1:
8416 return DW_FORM_data1;
8417 case 2:
8418 return DW_FORM_data2;
8419 case 4:
8420 return DW_FORM_data4;
8421 case 8:
8422 return DW_FORM_data8;
8423 default:
8424 gcc_unreachable ();
8426 case dw_val_class_range_list:
8427 case dw_val_class_loc_list:
8428 if (dwarf_version >= 4)
8429 return DW_FORM_sec_offset;
8430 /* FALLTHRU */
8431 case dw_val_class_vms_delta:
8432 case dw_val_class_offset:
8433 switch (DWARF_OFFSET_SIZE)
8435 case 4:
8436 return DW_FORM_data4;
8437 case 8:
8438 return DW_FORM_data8;
8439 default:
8440 gcc_unreachable ();
8442 case dw_val_class_loc:
8443 if (dwarf_version >= 4)
8444 return DW_FORM_exprloc;
8445 switch (constant_size (size_of_locs (AT_loc (a))))
8447 case 1:
8448 return DW_FORM_block1;
8449 case 2:
8450 return DW_FORM_block2;
8451 case 4:
8452 return DW_FORM_block4;
8453 default:
8454 gcc_unreachable ();
8456 case dw_val_class_const:
8457 return DW_FORM_sdata;
8458 case dw_val_class_unsigned_const:
8459 switch (constant_size (AT_unsigned (a)))
8461 case 1:
8462 return DW_FORM_data1;
8463 case 2:
8464 return DW_FORM_data2;
8465 case 4:
8466 /* In DWARF3 DW_AT_data_member_location with
8467 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8468 constant, so we need to use DW_FORM_udata if we need
8469 a large constant. */
8470 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8471 return DW_FORM_udata;
8472 return DW_FORM_data4;
8473 case 8:
8474 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8475 return DW_FORM_udata;
8476 return DW_FORM_data8;
8477 default:
8478 gcc_unreachable ();
8480 case dw_val_class_const_double:
8481 switch (HOST_BITS_PER_WIDE_INT)
8483 case 8:
8484 return DW_FORM_data2;
8485 case 16:
8486 return DW_FORM_data4;
8487 case 32:
8488 return DW_FORM_data8;
8489 case 64:
8490 default:
8491 return DW_FORM_block1;
8493 case dw_val_class_wide_int:
8494 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8496 case 8:
8497 return DW_FORM_data1;
8498 case 16:
8499 return DW_FORM_data2;
8500 case 32:
8501 return DW_FORM_data4;
8502 case 64:
8503 return DW_FORM_data8;
8504 default:
8505 return DW_FORM_block1;
8507 case dw_val_class_vec:
8508 switch (constant_size (a->dw_attr_val.v.val_vec.length
8509 * a->dw_attr_val.v.val_vec.elt_size))
8511 case 1:
8512 return DW_FORM_block1;
8513 case 2:
8514 return DW_FORM_block2;
8515 case 4:
8516 return DW_FORM_block4;
8517 default:
8518 gcc_unreachable ();
8520 case dw_val_class_flag:
8521 if (dwarf_version >= 4)
8523 /* Currently all add_AT_flag calls pass in 1 as last argument,
8524 so DW_FORM_flag_present can be used. If that ever changes,
8525 we'll need to use DW_FORM_flag and have some optimization
8526 in build_abbrev_table that will change those to
8527 DW_FORM_flag_present if it is set to 1 in all DIEs using
8528 the same abbrev entry. */
8529 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8530 return DW_FORM_flag_present;
8532 return DW_FORM_flag;
8533 case dw_val_class_die_ref:
8534 if (AT_ref_external (a))
8535 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8536 else
8537 return DW_FORM_ref;
8538 case dw_val_class_fde_ref:
8539 return DW_FORM_data;
8540 case dw_val_class_lbl_id:
8541 return (AT_index (a) == NOT_INDEXED
8542 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8543 case dw_val_class_lineptr:
8544 case dw_val_class_macptr:
8545 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8546 case dw_val_class_str:
8547 return AT_string_form (a);
8548 case dw_val_class_file:
8549 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8551 case 1:
8552 return DW_FORM_data1;
8553 case 2:
8554 return DW_FORM_data2;
8555 case 4:
8556 return DW_FORM_data4;
8557 default:
8558 gcc_unreachable ();
8561 case dw_val_class_data8:
8562 return DW_FORM_data8;
8564 case dw_val_class_high_pc:
8565 switch (DWARF2_ADDR_SIZE)
8567 case 1:
8568 return DW_FORM_data1;
8569 case 2:
8570 return DW_FORM_data2;
8571 case 4:
8572 return DW_FORM_data4;
8573 case 8:
8574 return DW_FORM_data8;
8575 default:
8576 gcc_unreachable ();
8579 default:
8580 gcc_unreachable ();
8584 /* Output the encoding of an attribute value. */
8586 static void
8587 output_value_format (dw_attr_ref a)
8589 enum dwarf_form form = value_format (a);
8591 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8594 /* Given a die and id, produce the appropriate abbreviations. */
8596 static void
8597 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8599 unsigned ix;
8600 dw_attr_ref a_attr;
8602 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8603 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8604 dwarf_tag_name (abbrev->die_tag));
8606 if (abbrev->die_child != NULL)
8607 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8608 else
8609 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8611 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8613 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8614 dwarf_attr_name (a_attr->dw_attr));
8615 output_value_format (a_attr);
8618 dw2_asm_output_data (1, 0, NULL);
8619 dw2_asm_output_data (1, 0, NULL);
8623 /* Output the .debug_abbrev section which defines the DIE abbreviation
8624 table. */
8626 static void
8627 output_abbrev_section (void)
8629 unsigned long abbrev_id;
8631 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8632 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8634 /* Terminate the table. */
8635 dw2_asm_output_data (1, 0, NULL);
8638 /* Output a symbol we can use to refer to this DIE from another CU. */
8640 static inline void
8641 output_die_symbol (dw_die_ref die)
8643 const char *sym = die->die_id.die_symbol;
8645 gcc_assert (!die->comdat_type_p);
8647 if (sym == 0)
8648 return;
8650 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8651 /* We make these global, not weak; if the target doesn't support
8652 .linkonce, it doesn't support combining the sections, so debugging
8653 will break. */
8654 targetm.asm_out.globalize_label (asm_out_file, sym);
8656 ASM_OUTPUT_LABEL (asm_out_file, sym);
8659 /* Return a new location list, given the begin and end range, and the
8660 expression. */
8662 static inline dw_loc_list_ref
8663 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8664 const char *section)
8666 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8668 retlist->begin = begin;
8669 retlist->begin_entry = NULL;
8670 retlist->end = end;
8671 retlist->expr = expr;
8672 retlist->section = section;
8674 return retlist;
8677 /* Generate a new internal symbol for this location list node, if it
8678 hasn't got one yet. */
8680 static inline void
8681 gen_llsym (dw_loc_list_ref list)
8683 gcc_assert (!list->ll_symbol);
8684 list->ll_symbol = gen_internal_sym ("LLST");
8687 /* Output the location list given to us. */
8689 static void
8690 output_loc_list (dw_loc_list_ref list_head)
8692 dw_loc_list_ref curr = list_head;
8694 if (list_head->emitted)
8695 return;
8696 list_head->emitted = true;
8698 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8700 /* Walk the location list, and output each range + expression. */
8701 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8703 unsigned long size;
8704 /* Don't output an entry that starts and ends at the same address. */
8705 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8706 continue;
8707 size = size_of_locs (curr->expr);
8708 /* If the expression is too large, drop it on the floor. We could
8709 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8710 in the expression, but >= 64KB expressions for a single value
8711 in a single range are unlikely very useful. */
8712 if (size > 0xffff)
8713 continue;
8714 if (dwarf_split_debug_info)
8716 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8717 "Location list start/length entry (%s)",
8718 list_head->ll_symbol);
8719 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8720 "Location list range start index (%s)",
8721 curr->begin);
8722 /* The length field is 4 bytes. If we ever need to support
8723 an 8-byte length, we can add a new DW_LLE code or fall back
8724 to DW_LLE_GNU_start_end_entry. */
8725 dw2_asm_output_delta (4, curr->end, curr->begin,
8726 "Location list range length (%s)",
8727 list_head->ll_symbol);
8729 else if (!have_multiple_function_sections)
8731 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8732 "Location list begin address (%s)",
8733 list_head->ll_symbol);
8734 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8735 "Location list end address (%s)",
8736 list_head->ll_symbol);
8738 else
8740 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8741 "Location list begin address (%s)",
8742 list_head->ll_symbol);
8743 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8744 "Location list end address (%s)",
8745 list_head->ll_symbol);
8748 /* Output the block length for this list of location operations. */
8749 gcc_assert (size <= 0xffff);
8750 dw2_asm_output_data (2, size, "%s", "Location expression size");
8752 output_loc_sequence (curr->expr, -1);
8755 if (dwarf_split_debug_info)
8756 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8757 "Location list terminator (%s)",
8758 list_head->ll_symbol);
8759 else
8761 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8762 "Location list terminator begin (%s)",
8763 list_head->ll_symbol);
8764 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8765 "Location list terminator end (%s)",
8766 list_head->ll_symbol);
8770 /* Output a range_list offset into the debug_range section. Emit a
8771 relocated reference if val_entry is NULL, otherwise, emit an
8772 indirect reference. */
8774 static void
8775 output_range_list_offset (dw_attr_ref a)
8777 const char *name = dwarf_attr_name (a->dw_attr);
8779 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8781 char *p = strchr (ranges_section_label, '\0');
8782 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8783 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8784 debug_ranges_section, "%s", name);
8785 *p = '\0';
8787 else
8788 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8789 "%s (offset from %s)", name, ranges_section_label);
8792 /* Output the offset into the debug_loc section. */
8794 static void
8795 output_loc_list_offset (dw_attr_ref a)
8797 char *sym = AT_loc_list (a)->ll_symbol;
8799 gcc_assert (sym);
8800 if (dwarf_split_debug_info)
8801 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8802 "%s", dwarf_attr_name (a->dw_attr));
8803 else
8804 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8805 "%s", dwarf_attr_name (a->dw_attr));
8808 /* Output an attribute's index or value appropriately. */
8810 static void
8811 output_attr_index_or_value (dw_attr_ref a)
8813 const char *name = dwarf_attr_name (a->dw_attr);
8815 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8817 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8818 return;
8820 switch (AT_class (a))
8822 case dw_val_class_addr:
8823 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8824 break;
8825 case dw_val_class_high_pc:
8826 case dw_val_class_lbl_id:
8827 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8828 break;
8829 case dw_val_class_loc_list:
8830 output_loc_list_offset (a);
8831 break;
8832 default:
8833 gcc_unreachable ();
8837 /* Output a type signature. */
8839 static inline void
8840 output_signature (const char *sig, const char *name)
8842 int i;
8844 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8845 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8848 /* Output the DIE and its attributes. Called recursively to generate
8849 the definitions of each child DIE. */
8851 static void
8852 output_die (dw_die_ref die)
8854 dw_attr_ref a;
8855 dw_die_ref c;
8856 unsigned long size;
8857 unsigned ix;
8859 /* If someone in another CU might refer to us, set up a symbol for
8860 them to point to. */
8861 if (! die->comdat_type_p && die->die_id.die_symbol)
8862 output_die_symbol (die);
8864 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8865 (unsigned long)die->die_offset,
8866 dwarf_tag_name (die->die_tag));
8868 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8870 const char *name = dwarf_attr_name (a->dw_attr);
8872 switch (AT_class (a))
8874 case dw_val_class_addr:
8875 output_attr_index_or_value (a);
8876 break;
8878 case dw_val_class_offset:
8879 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8880 "%s", name);
8881 break;
8883 case dw_val_class_range_list:
8884 output_range_list_offset (a);
8885 break;
8887 case dw_val_class_loc:
8888 size = size_of_locs (AT_loc (a));
8890 /* Output the block length for this list of location operations. */
8891 if (dwarf_version >= 4)
8892 dw2_asm_output_data_uleb128 (size, "%s", name);
8893 else
8894 dw2_asm_output_data (constant_size (size), size, "%s", name);
8896 output_loc_sequence (AT_loc (a), -1);
8897 break;
8899 case dw_val_class_const:
8900 /* ??? It would be slightly more efficient to use a scheme like is
8901 used for unsigned constants below, but gdb 4.x does not sign
8902 extend. Gdb 5.x does sign extend. */
8903 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8904 break;
8906 case dw_val_class_unsigned_const:
8908 int csize = constant_size (AT_unsigned (a));
8909 if (dwarf_version == 3
8910 && a->dw_attr == DW_AT_data_member_location
8911 && csize >= 4)
8912 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8913 else
8914 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8916 break;
8918 case dw_val_class_const_double:
8920 unsigned HOST_WIDE_INT first, second;
8922 if (HOST_BITS_PER_WIDE_INT >= 64)
8923 dw2_asm_output_data (1,
8924 HOST_BITS_PER_DOUBLE_INT
8925 / HOST_BITS_PER_CHAR,
8926 NULL);
8928 if (WORDS_BIG_ENDIAN)
8930 first = a->dw_attr_val.v.val_double.high;
8931 second = a->dw_attr_val.v.val_double.low;
8933 else
8935 first = a->dw_attr_val.v.val_double.low;
8936 second = a->dw_attr_val.v.val_double.high;
8939 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8940 first, "%s", name);
8941 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8942 second, NULL);
8944 break;
8946 case dw_val_class_wide_int:
8948 int i;
8949 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8950 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8951 if (len * HOST_BITS_PER_WIDE_INT > 64)
8952 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8953 NULL);
8955 if (WORDS_BIG_ENDIAN)
8956 for (i = len - 1; i >= 0; --i)
8958 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8959 "%s", name);
8960 name = NULL;
8962 else
8963 for (i = 0; i < len; ++i)
8965 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8966 "%s", name);
8967 name = NULL;
8970 break;
8972 case dw_val_class_vec:
8974 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8975 unsigned int len = a->dw_attr_val.v.val_vec.length;
8976 unsigned int i;
8977 unsigned char *p;
8979 dw2_asm_output_data (constant_size (len * elt_size),
8980 len * elt_size, "%s", name);
8981 if (elt_size > sizeof (HOST_WIDE_INT))
8983 elt_size /= 2;
8984 len *= 2;
8986 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8987 i < len;
8988 i++, p += elt_size)
8989 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8990 "fp or vector constant word %u", i);
8991 break;
8994 case dw_val_class_flag:
8995 if (dwarf_version >= 4)
8997 /* Currently all add_AT_flag calls pass in 1 as last argument,
8998 so DW_FORM_flag_present can be used. If that ever changes,
8999 we'll need to use DW_FORM_flag and have some optimization
9000 in build_abbrev_table that will change those to
9001 DW_FORM_flag_present if it is set to 1 in all DIEs using
9002 the same abbrev entry. */
9003 gcc_assert (AT_flag (a) == 1);
9004 if (flag_debug_asm)
9005 fprintf (asm_out_file, "\t\t\t%s %s\n",
9006 ASM_COMMENT_START, name);
9007 break;
9009 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9010 break;
9012 case dw_val_class_loc_list:
9013 output_attr_index_or_value (a);
9014 break;
9016 case dw_val_class_die_ref:
9017 if (AT_ref_external (a))
9019 if (AT_ref (a)->comdat_type_p)
9021 comdat_type_node_ref type_node =
9022 AT_ref (a)->die_id.die_type_node;
9024 gcc_assert (type_node);
9025 output_signature (type_node->signature, name);
9027 else
9029 const char *sym = AT_ref (a)->die_id.die_symbol;
9030 int size;
9032 gcc_assert (sym);
9033 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9034 length, whereas in DWARF3 it's always sized as an
9035 offset. */
9036 if (dwarf_version == 2)
9037 size = DWARF2_ADDR_SIZE;
9038 else
9039 size = DWARF_OFFSET_SIZE;
9040 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9041 name);
9044 else
9046 gcc_assert (AT_ref (a)->die_offset);
9047 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9048 "%s", name);
9050 break;
9052 case dw_val_class_fde_ref:
9054 char l1[20];
9056 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9057 a->dw_attr_val.v.val_fde_index * 2);
9058 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9059 "%s", name);
9061 break;
9063 case dw_val_class_vms_delta:
9064 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9065 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9066 AT_vms_delta2 (a), AT_vms_delta1 (a),
9067 "%s", name);
9068 #else
9069 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9070 AT_vms_delta2 (a), AT_vms_delta1 (a),
9071 "%s", name);
9072 #endif
9073 break;
9075 case dw_val_class_lbl_id:
9076 output_attr_index_or_value (a);
9077 break;
9079 case dw_val_class_lineptr:
9080 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9081 debug_line_section, "%s", name);
9082 break;
9084 case dw_val_class_macptr:
9085 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9086 debug_macinfo_section, "%s", name);
9087 break;
9089 case dw_val_class_str:
9090 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9091 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9092 a->dw_attr_val.v.val_str->label,
9093 debug_str_section,
9094 "%s: \"%s\"", name, AT_string (a));
9095 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9096 dw2_asm_output_data_uleb128 (AT_index (a),
9097 "%s: \"%s\"", name, AT_string (a));
9098 else
9099 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9100 break;
9102 case dw_val_class_file:
9104 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9106 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9107 a->dw_attr_val.v.val_file->filename);
9108 break;
9111 case dw_val_class_data8:
9113 int i;
9115 for (i = 0; i < 8; i++)
9116 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9117 i == 0 ? "%s" : NULL, name);
9118 break;
9121 case dw_val_class_high_pc:
9122 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9123 get_AT_low_pc (die), "DW_AT_high_pc");
9124 break;
9126 default:
9127 gcc_unreachable ();
9131 FOR_EACH_CHILD (die, c, output_die (c));
9133 /* Add null byte to terminate sibling list. */
9134 if (die->die_child != NULL)
9135 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9136 (unsigned long) die->die_offset);
9139 /* Output the compilation unit that appears at the beginning of the
9140 .debug_info section, and precedes the DIE descriptions. */
9142 static void
9143 output_compilation_unit_header (void)
9145 /* We don't support actual DWARFv5 units yet, we just use some
9146 DWARFv5 draft DIE tags in DWARFv4 format. */
9147 int ver = dwarf_version < 5 ? dwarf_version : 4;
9149 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9150 dw2_asm_output_data (4, 0xffffffff,
9151 "Initial length escape value indicating 64-bit DWARF extension");
9152 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9153 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9154 "Length of Compilation Unit Info");
9155 dw2_asm_output_data (2, ver, "DWARF version number");
9156 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9157 debug_abbrev_section,
9158 "Offset Into Abbrev. Section");
9159 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9162 /* Output the compilation unit DIE and its children. */
9164 static void
9165 output_comp_unit (dw_die_ref die, int output_if_empty)
9167 const char *secname, *oldsym;
9168 char *tmp;
9170 /* Unless we are outputting main CU, we may throw away empty ones. */
9171 if (!output_if_empty && die->die_child == NULL)
9172 return;
9174 /* Even if there are no children of this DIE, we must output the information
9175 about the compilation unit. Otherwise, on an empty translation unit, we
9176 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9177 will then complain when examining the file. First mark all the DIEs in
9178 this CU so we know which get local refs. */
9179 mark_dies (die);
9181 external_ref_hash_type *extern_map = optimize_external_refs (die);
9183 build_abbrev_table (die, extern_map);
9185 delete extern_map;
9187 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9188 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9189 calc_die_sizes (die);
9191 oldsym = die->die_id.die_symbol;
9192 if (oldsym)
9194 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9196 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9197 secname = tmp;
9198 die->die_id.die_symbol = NULL;
9199 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9201 else
9203 switch_to_section (debug_info_section);
9204 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9205 info_section_emitted = true;
9208 /* Output debugging information. */
9209 output_compilation_unit_header ();
9210 output_die (die);
9212 /* Leave the marks on the main CU, so we can check them in
9213 output_pubnames. */
9214 if (oldsym)
9216 unmark_dies (die);
9217 die->die_id.die_symbol = oldsym;
9221 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9222 and .debug_pubtypes. This is configured per-target, but can be
9223 overridden by the -gpubnames or -gno-pubnames options. */
9225 static inline bool
9226 want_pubnames (void)
9228 if (debug_info_level <= DINFO_LEVEL_TERSE)
9229 return false;
9230 if (debug_generate_pub_sections != -1)
9231 return debug_generate_pub_sections;
9232 return targetm.want_debug_pub_sections;
9235 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9237 static void
9238 add_AT_pubnames (dw_die_ref die)
9240 if (want_pubnames ())
9241 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9244 /* Add a string attribute value to a skeleton DIE. */
9246 static inline void
9247 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9248 const char *str)
9250 dw_attr_node attr;
9251 struct indirect_string_node *node;
9253 if (! skeleton_debug_str_hash)
9254 skeleton_debug_str_hash
9255 = hash_table<indirect_string_hasher>::create_ggc (10);
9257 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9258 find_string_form (node);
9259 if (node->form == DW_FORM_GNU_str_index)
9260 node->form = DW_FORM_strp;
9262 attr.dw_attr = attr_kind;
9263 attr.dw_attr_val.val_class = dw_val_class_str;
9264 attr.dw_attr_val.val_entry = NULL;
9265 attr.dw_attr_val.v.val_str = node;
9266 add_dwarf_attr (die, &attr);
9269 /* Helper function to generate top-level dies for skeleton debug_info and
9270 debug_types. */
9272 static void
9273 add_top_level_skeleton_die_attrs (dw_die_ref die)
9275 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9276 const char *comp_dir = comp_dir_string ();
9278 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9279 if (comp_dir != NULL)
9280 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9281 add_AT_pubnames (die);
9282 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9285 /* Output skeleton debug sections that point to the dwo file. */
9287 static void
9288 output_skeleton_debug_sections (dw_die_ref comp_unit)
9290 /* We don't support actual DWARFv5 units yet, we just use some
9291 DWARFv5 draft DIE tags in DWARFv4 format. */
9292 int ver = dwarf_version < 5 ? dwarf_version : 4;
9294 /* These attributes will be found in the full debug_info section. */
9295 remove_AT (comp_unit, DW_AT_producer);
9296 remove_AT (comp_unit, DW_AT_language);
9298 switch_to_section (debug_skeleton_info_section);
9299 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9301 /* Produce the skeleton compilation-unit header. This one differs enough from
9302 a normal CU header that it's better not to call output_compilation_unit
9303 header. */
9304 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9305 dw2_asm_output_data (4, 0xffffffff,
9306 "Initial length escape value indicating 64-bit DWARF extension");
9308 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9309 DWARF_COMPILE_UNIT_HEADER_SIZE
9310 - DWARF_INITIAL_LENGTH_SIZE
9311 + size_of_die (comp_unit),
9312 "Length of Compilation Unit Info");
9313 dw2_asm_output_data (2, ver, "DWARF version number");
9314 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9315 debug_abbrev_section,
9316 "Offset Into Abbrev. Section");
9317 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9319 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9320 output_die (comp_unit);
9322 /* Build the skeleton debug_abbrev section. */
9323 switch_to_section (debug_skeleton_abbrev_section);
9324 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9326 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9328 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9331 /* Output a comdat type unit DIE and its children. */
9333 static void
9334 output_comdat_type_unit (comdat_type_node *node)
9336 const char *secname;
9337 char *tmp;
9338 int i;
9339 #if defined (OBJECT_FORMAT_ELF)
9340 tree comdat_key;
9341 #endif
9343 /* First mark all the DIEs in this CU so we know which get local refs. */
9344 mark_dies (node->root_die);
9346 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9348 build_abbrev_table (node->root_die, extern_map);
9350 delete extern_map;
9351 extern_map = NULL;
9353 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9354 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9355 calc_die_sizes (node->root_die);
9357 #if defined (OBJECT_FORMAT_ELF)
9358 if (!dwarf_split_debug_info)
9359 secname = ".debug_types";
9360 else
9361 secname = ".debug_types.dwo";
9363 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9364 sprintf (tmp, "wt.");
9365 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9366 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9367 comdat_key = get_identifier (tmp);
9368 targetm.asm_out.named_section (secname,
9369 SECTION_DEBUG | SECTION_LINKONCE,
9370 comdat_key);
9371 #else
9372 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9373 sprintf (tmp, ".gnu.linkonce.wt.");
9374 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9375 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9376 secname = tmp;
9377 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9378 #endif
9380 /* Output debugging information. */
9381 output_compilation_unit_header ();
9382 output_signature (node->signature, "Type Signature");
9383 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9384 "Offset to Type DIE");
9385 output_die (node->root_die);
9387 unmark_dies (node->root_die);
9390 /* Return the DWARF2/3 pubname associated with a decl. */
9392 static const char *
9393 dwarf2_name (tree decl, int scope)
9395 if (DECL_NAMELESS (decl))
9396 return NULL;
9397 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9400 /* Add a new entry to .debug_pubnames if appropriate. */
9402 static void
9403 add_pubname_string (const char *str, dw_die_ref die)
9405 pubname_entry e;
9407 e.die = die;
9408 e.name = xstrdup (str);
9409 vec_safe_push (pubname_table, e);
9412 static void
9413 add_pubname (tree decl, dw_die_ref die)
9415 if (!want_pubnames ())
9416 return;
9418 /* Don't add items to the table when we expect that the consumer will have
9419 just read the enclosing die. For example, if the consumer is looking at a
9420 class_member, it will either be inside the class already, or will have just
9421 looked up the class to find the member. Either way, searching the class is
9422 faster than searching the index. */
9423 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9424 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9426 const char *name = dwarf2_name (decl, 1);
9428 if (name)
9429 add_pubname_string (name, die);
9433 /* Add an enumerator to the pubnames section. */
9435 static void
9436 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9438 pubname_entry e;
9440 gcc_assert (scope_name);
9441 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9442 e.die = die;
9443 vec_safe_push (pubname_table, e);
9446 /* Add a new entry to .debug_pubtypes if appropriate. */
9448 static void
9449 add_pubtype (tree decl, dw_die_ref die)
9451 pubname_entry e;
9453 if (!want_pubnames ())
9454 return;
9456 if ((TREE_PUBLIC (decl)
9457 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9458 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9460 tree scope = NULL;
9461 const char *scope_name = "";
9462 const char *sep = is_cxx () ? "::" : ".";
9463 const char *name;
9465 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9466 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9468 scope_name = lang_hooks.dwarf_name (scope, 1);
9469 if (scope_name != NULL && scope_name[0] != '\0')
9470 scope_name = concat (scope_name, sep, NULL);
9471 else
9472 scope_name = "";
9475 if (TYPE_P (decl))
9476 name = type_tag (decl);
9477 else
9478 name = lang_hooks.dwarf_name (decl, 1);
9480 /* If we don't have a name for the type, there's no point in adding
9481 it to the table. */
9482 if (name != NULL && name[0] != '\0')
9484 e.die = die;
9485 e.name = concat (scope_name, name, NULL);
9486 vec_safe_push (pubtype_table, e);
9489 /* Although it might be more consistent to add the pubinfo for the
9490 enumerators as their dies are created, they should only be added if the
9491 enum type meets the criteria above. So rather than re-check the parent
9492 enum type whenever an enumerator die is created, just output them all
9493 here. This isn't protected by the name conditional because anonymous
9494 enums don't have names. */
9495 if (die->die_tag == DW_TAG_enumeration_type)
9497 dw_die_ref c;
9499 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9504 /* Output a single entry in the pubnames table. */
9506 static void
9507 output_pubname (dw_offset die_offset, pubname_entry *entry)
9509 dw_die_ref die = entry->die;
9510 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9512 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9514 if (debug_generate_pub_sections == 2)
9516 /* This logic follows gdb's method for determining the value of the flag
9517 byte. */
9518 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9519 switch (die->die_tag)
9521 case DW_TAG_typedef:
9522 case DW_TAG_base_type:
9523 case DW_TAG_subrange_type:
9524 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9525 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9526 break;
9527 case DW_TAG_enumerator:
9528 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9529 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9530 if (!is_cxx () && !is_java ())
9531 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9532 break;
9533 case DW_TAG_subprogram:
9534 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9535 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9536 if (!is_ada ())
9537 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9538 break;
9539 case DW_TAG_constant:
9540 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9541 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9542 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9543 break;
9544 case DW_TAG_variable:
9545 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9546 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9547 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9548 break;
9549 case DW_TAG_namespace:
9550 case DW_TAG_imported_declaration:
9551 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9552 break;
9553 case DW_TAG_class_type:
9554 case DW_TAG_interface_type:
9555 case DW_TAG_structure_type:
9556 case DW_TAG_union_type:
9557 case DW_TAG_enumeration_type:
9558 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9559 if (!is_cxx () && !is_java ())
9560 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9561 break;
9562 default:
9563 /* An unusual tag. Leave the flag-byte empty. */
9564 break;
9566 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9567 "GDB-index flags");
9570 dw2_asm_output_nstring (entry->name, -1, "external name");
9574 /* Output the public names table used to speed up access to externally
9575 visible names; or the public types table used to find type definitions. */
9577 static void
9578 output_pubnames (vec<pubname_entry, va_gc> *names)
9580 unsigned i;
9581 unsigned long pubnames_length = size_of_pubnames (names);
9582 pubname_ref pub;
9584 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9585 dw2_asm_output_data (4, 0xffffffff,
9586 "Initial length escape value indicating 64-bit DWARF extension");
9587 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9589 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9590 dw2_asm_output_data (2, 2, "DWARF Version");
9592 if (dwarf_split_debug_info)
9593 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9594 debug_skeleton_info_section,
9595 "Offset of Compilation Unit Info");
9596 else
9597 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9598 debug_info_section,
9599 "Offset of Compilation Unit Info");
9600 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9601 "Compilation Unit Length");
9603 FOR_EACH_VEC_ELT (*names, i, pub)
9605 if (include_pubname_in_output (names, pub))
9607 dw_offset die_offset = pub->die->die_offset;
9609 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9610 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9611 gcc_assert (pub->die->die_mark);
9613 /* If we're putting types in their own .debug_types sections,
9614 the .debug_pubtypes table will still point to the compile
9615 unit (not the type unit), so we want to use the offset of
9616 the skeleton DIE (if there is one). */
9617 if (pub->die->comdat_type_p && names == pubtype_table)
9619 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9621 if (type_node != NULL)
9622 die_offset = (type_node->skeleton_die != NULL
9623 ? type_node->skeleton_die->die_offset
9624 : comp_unit_die ()->die_offset);
9627 output_pubname (die_offset, pub);
9631 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9634 /* Output public names and types tables if necessary. */
9636 static void
9637 output_pubtables (void)
9639 if (!want_pubnames () || !info_section_emitted)
9640 return;
9642 switch_to_section (debug_pubnames_section);
9643 output_pubnames (pubname_table);
9644 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9645 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9646 simply won't look for the section. */
9647 switch_to_section (debug_pubtypes_section);
9648 output_pubnames (pubtype_table);
9652 /* Output the information that goes into the .debug_aranges table.
9653 Namely, define the beginning and ending address range of the
9654 text section generated for this compilation unit. */
9656 static void
9657 output_aranges (unsigned long aranges_length)
9659 unsigned i;
9661 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9662 dw2_asm_output_data (4, 0xffffffff,
9663 "Initial length escape value indicating 64-bit DWARF extension");
9664 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9665 "Length of Address Ranges Info");
9666 /* Version number for aranges is still 2, even up to DWARF5. */
9667 dw2_asm_output_data (2, 2, "DWARF Version");
9668 if (dwarf_split_debug_info)
9669 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9670 debug_skeleton_info_section,
9671 "Offset of Compilation Unit Info");
9672 else
9673 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9674 debug_info_section,
9675 "Offset of Compilation Unit Info");
9676 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9677 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9679 /* We need to align to twice the pointer size here. */
9680 if (DWARF_ARANGES_PAD_SIZE)
9682 /* Pad using a 2 byte words so that padding is correct for any
9683 pointer size. */
9684 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9685 2 * DWARF2_ADDR_SIZE);
9686 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9687 dw2_asm_output_data (2, 0, NULL);
9690 /* It is necessary not to output these entries if the sections were
9691 not used; if the sections were not used, the length will be 0 and
9692 the address may end up as 0 if the section is discarded by ld
9693 --gc-sections, leaving an invalid (0, 0) entry that can be
9694 confused with the terminator. */
9695 if (text_section_used)
9697 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9698 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9699 text_section_label, "Length");
9701 if (cold_text_section_used)
9703 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9704 "Address");
9705 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9706 cold_text_section_label, "Length");
9709 if (have_multiple_function_sections)
9711 unsigned fde_idx;
9712 dw_fde_ref fde;
9714 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9716 if (DECL_IGNORED_P (fde->decl))
9717 continue;
9718 if (!fde->in_std_section)
9720 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9721 "Address");
9722 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9723 fde->dw_fde_begin, "Length");
9725 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9727 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9728 "Address");
9729 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9730 fde->dw_fde_second_begin, "Length");
9735 /* Output the terminator words. */
9736 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9737 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9740 /* Add a new entry to .debug_ranges. Return the offset at which it
9741 was placed. */
9743 static unsigned int
9744 add_ranges_num (int num)
9746 unsigned int in_use = ranges_table_in_use;
9748 if (in_use == ranges_table_allocated)
9750 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9751 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9752 ranges_table_allocated);
9753 memset (ranges_table + ranges_table_in_use, 0,
9754 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9757 ranges_table[in_use].num = num;
9758 ranges_table_in_use = in_use + 1;
9760 return in_use * 2 * DWARF2_ADDR_SIZE;
9763 /* Add a new entry to .debug_ranges corresponding to a block, or a
9764 range terminator if BLOCK is NULL. */
9766 static unsigned int
9767 add_ranges (const_tree block)
9769 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9772 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9773 When using dwarf_split_debug_info, address attributes in dies destined
9774 for the final executable should be direct references--setting the
9775 parameter force_direct ensures this behavior. */
9777 static void
9778 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9779 bool *added, bool force_direct)
9781 unsigned int in_use = ranges_by_label_in_use;
9782 unsigned int offset;
9784 if (in_use == ranges_by_label_allocated)
9786 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9787 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9788 ranges_by_label,
9789 ranges_by_label_allocated);
9790 memset (ranges_by_label + ranges_by_label_in_use, 0,
9791 RANGES_TABLE_INCREMENT
9792 * sizeof (struct dw_ranges_by_label_struct));
9795 ranges_by_label[in_use].begin = begin;
9796 ranges_by_label[in_use].end = end;
9797 ranges_by_label_in_use = in_use + 1;
9799 offset = add_ranges_num (-(int)in_use - 1);
9800 if (!*added)
9802 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9803 *added = true;
9807 static void
9808 output_ranges (void)
9810 unsigned i;
9811 static const char *const start_fmt = "Offset %#x";
9812 const char *fmt = start_fmt;
9814 for (i = 0; i < ranges_table_in_use; i++)
9816 int block_num = ranges_table[i].num;
9818 if (block_num > 0)
9820 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9821 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9823 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9824 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9826 /* If all code is in the text section, then the compilation
9827 unit base address defaults to DW_AT_low_pc, which is the
9828 base of the text section. */
9829 if (!have_multiple_function_sections)
9831 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9832 text_section_label,
9833 fmt, i * 2 * DWARF2_ADDR_SIZE);
9834 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9835 text_section_label, NULL);
9838 /* Otherwise, the compilation unit base address is zero,
9839 which allows us to use absolute addresses, and not worry
9840 about whether the target supports cross-section
9841 arithmetic. */
9842 else
9844 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9845 fmt, i * 2 * DWARF2_ADDR_SIZE);
9846 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9849 fmt = NULL;
9852 /* Negative block_num stands for an index into ranges_by_label. */
9853 else if (block_num < 0)
9855 int lab_idx = - block_num - 1;
9857 if (!have_multiple_function_sections)
9859 gcc_unreachable ();
9860 #if 0
9861 /* If we ever use add_ranges_by_labels () for a single
9862 function section, all we have to do is to take out
9863 the #if 0 above. */
9864 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9865 ranges_by_label[lab_idx].begin,
9866 text_section_label,
9867 fmt, i * 2 * DWARF2_ADDR_SIZE);
9868 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9869 ranges_by_label[lab_idx].end,
9870 text_section_label, NULL);
9871 #endif
9873 else
9875 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9876 ranges_by_label[lab_idx].begin,
9877 fmt, i * 2 * DWARF2_ADDR_SIZE);
9878 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9879 ranges_by_label[lab_idx].end,
9880 NULL);
9883 else
9885 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9886 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9887 fmt = start_fmt;
9892 /* Data structure containing information about input files. */
9893 struct file_info
9895 const char *path; /* Complete file name. */
9896 const char *fname; /* File name part. */
9897 int length; /* Length of entire string. */
9898 struct dwarf_file_data * file_idx; /* Index in input file table. */
9899 int dir_idx; /* Index in directory table. */
9902 /* Data structure containing information about directories with source
9903 files. */
9904 struct dir_info
9906 const char *path; /* Path including directory name. */
9907 int length; /* Path length. */
9908 int prefix; /* Index of directory entry which is a prefix. */
9909 int count; /* Number of files in this directory. */
9910 int dir_idx; /* Index of directory used as base. */
9913 /* Callback function for file_info comparison. We sort by looking at
9914 the directories in the path. */
9916 static int
9917 file_info_cmp (const void *p1, const void *p2)
9919 const struct file_info *const s1 = (const struct file_info *) p1;
9920 const struct file_info *const s2 = (const struct file_info *) p2;
9921 const unsigned char *cp1;
9922 const unsigned char *cp2;
9924 /* Take care of file names without directories. We need to make sure that
9925 we return consistent values to qsort since some will get confused if
9926 we return the same value when identical operands are passed in opposite
9927 orders. So if neither has a directory, return 0 and otherwise return
9928 1 or -1 depending on which one has the directory. */
9929 if ((s1->path == s1->fname || s2->path == s2->fname))
9930 return (s2->path == s2->fname) - (s1->path == s1->fname);
9932 cp1 = (const unsigned char *) s1->path;
9933 cp2 = (const unsigned char *) s2->path;
9935 while (1)
9937 ++cp1;
9938 ++cp2;
9939 /* Reached the end of the first path? If so, handle like above. */
9940 if ((cp1 == (const unsigned char *) s1->fname)
9941 || (cp2 == (const unsigned char *) s2->fname))
9942 return ((cp2 == (const unsigned char *) s2->fname)
9943 - (cp1 == (const unsigned char *) s1->fname));
9945 /* Character of current path component the same? */
9946 else if (*cp1 != *cp2)
9947 return *cp1 - *cp2;
9951 struct file_name_acquire_data
9953 struct file_info *files;
9954 int used_files;
9955 int max_files;
9958 /* Traversal function for the hash table. */
9961 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9963 struct dwarf_file_data *d = *slot;
9964 struct file_info *fi;
9965 const char *f;
9967 gcc_assert (fnad->max_files >= d->emitted_number);
9969 if (! d->emitted_number)
9970 return 1;
9972 gcc_assert (fnad->max_files != fnad->used_files);
9974 fi = fnad->files + fnad->used_files++;
9976 /* Skip all leading "./". */
9977 f = d->filename;
9978 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9979 f += 2;
9981 /* Create a new array entry. */
9982 fi->path = f;
9983 fi->length = strlen (f);
9984 fi->file_idx = d;
9986 /* Search for the file name part. */
9987 f = strrchr (f, DIR_SEPARATOR);
9988 #if defined (DIR_SEPARATOR_2)
9990 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9992 if (g != NULL)
9994 if (f == NULL || f < g)
9995 f = g;
9998 #endif
10000 fi->fname = f == NULL ? fi->path : f + 1;
10001 return 1;
10004 /* Output the directory table and the file name table. We try to minimize
10005 the total amount of memory needed. A heuristic is used to avoid large
10006 slowdowns with many input files. */
10008 static void
10009 output_file_names (void)
10011 struct file_name_acquire_data fnad;
10012 int numfiles;
10013 struct file_info *files;
10014 struct dir_info *dirs;
10015 int *saved;
10016 int *savehere;
10017 int *backmap;
10018 int ndirs;
10019 int idx_offset;
10020 int i;
10022 if (!last_emitted_file)
10024 dw2_asm_output_data (1, 0, "End directory table");
10025 dw2_asm_output_data (1, 0, "End file name table");
10026 return;
10029 numfiles = last_emitted_file->emitted_number;
10031 /* Allocate the various arrays we need. */
10032 files = XALLOCAVEC (struct file_info, numfiles);
10033 dirs = XALLOCAVEC (struct dir_info, numfiles);
10035 fnad.files = files;
10036 fnad.used_files = 0;
10037 fnad.max_files = numfiles;
10038 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10039 gcc_assert (fnad.used_files == fnad.max_files);
10041 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10043 /* Find all the different directories used. */
10044 dirs[0].path = files[0].path;
10045 dirs[0].length = files[0].fname - files[0].path;
10046 dirs[0].prefix = -1;
10047 dirs[0].count = 1;
10048 dirs[0].dir_idx = 0;
10049 files[0].dir_idx = 0;
10050 ndirs = 1;
10052 for (i = 1; i < numfiles; i++)
10053 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10054 && memcmp (dirs[ndirs - 1].path, files[i].path,
10055 dirs[ndirs - 1].length) == 0)
10057 /* Same directory as last entry. */
10058 files[i].dir_idx = ndirs - 1;
10059 ++dirs[ndirs - 1].count;
10061 else
10063 int j;
10065 /* This is a new directory. */
10066 dirs[ndirs].path = files[i].path;
10067 dirs[ndirs].length = files[i].fname - files[i].path;
10068 dirs[ndirs].count = 1;
10069 dirs[ndirs].dir_idx = ndirs;
10070 files[i].dir_idx = ndirs;
10072 /* Search for a prefix. */
10073 dirs[ndirs].prefix = -1;
10074 for (j = 0; j < ndirs; j++)
10075 if (dirs[j].length < dirs[ndirs].length
10076 && dirs[j].length > 1
10077 && (dirs[ndirs].prefix == -1
10078 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10079 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10080 dirs[ndirs].prefix = j;
10082 ++ndirs;
10085 /* Now to the actual work. We have to find a subset of the directories which
10086 allow expressing the file name using references to the directory table
10087 with the least amount of characters. We do not do an exhaustive search
10088 where we would have to check out every combination of every single
10089 possible prefix. Instead we use a heuristic which provides nearly optimal
10090 results in most cases and never is much off. */
10091 saved = XALLOCAVEC (int, ndirs);
10092 savehere = XALLOCAVEC (int, ndirs);
10094 memset (saved, '\0', ndirs * sizeof (saved[0]));
10095 for (i = 0; i < ndirs; i++)
10097 int j;
10098 int total;
10100 /* We can always save some space for the current directory. But this
10101 does not mean it will be enough to justify adding the directory. */
10102 savehere[i] = dirs[i].length;
10103 total = (savehere[i] - saved[i]) * dirs[i].count;
10105 for (j = i + 1; j < ndirs; j++)
10107 savehere[j] = 0;
10108 if (saved[j] < dirs[i].length)
10110 /* Determine whether the dirs[i] path is a prefix of the
10111 dirs[j] path. */
10112 int k;
10114 k = dirs[j].prefix;
10115 while (k != -1 && k != (int) i)
10116 k = dirs[k].prefix;
10118 if (k == (int) i)
10120 /* Yes it is. We can possibly save some memory by
10121 writing the filenames in dirs[j] relative to
10122 dirs[i]. */
10123 savehere[j] = dirs[i].length;
10124 total += (savehere[j] - saved[j]) * dirs[j].count;
10129 /* Check whether we can save enough to justify adding the dirs[i]
10130 directory. */
10131 if (total > dirs[i].length + 1)
10133 /* It's worthwhile adding. */
10134 for (j = i; j < ndirs; j++)
10135 if (savehere[j] > 0)
10137 /* Remember how much we saved for this directory so far. */
10138 saved[j] = savehere[j];
10140 /* Remember the prefix directory. */
10141 dirs[j].dir_idx = i;
10146 /* Emit the directory name table. */
10147 idx_offset = dirs[0].length > 0 ? 1 : 0;
10148 for (i = 1 - idx_offset; i < ndirs; i++)
10149 dw2_asm_output_nstring (dirs[i].path,
10150 dirs[i].length
10151 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10152 "Directory Entry: %#x", i + idx_offset);
10154 dw2_asm_output_data (1, 0, "End directory table");
10156 /* We have to emit them in the order of emitted_number since that's
10157 used in the debug info generation. To do this efficiently we
10158 generate a back-mapping of the indices first. */
10159 backmap = XALLOCAVEC (int, numfiles);
10160 for (i = 0; i < numfiles; i++)
10161 backmap[files[i].file_idx->emitted_number - 1] = i;
10163 /* Now write all the file names. */
10164 for (i = 0; i < numfiles; i++)
10166 int file_idx = backmap[i];
10167 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10169 #ifdef VMS_DEBUGGING_INFO
10170 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10172 /* Setting these fields can lead to debugger miscomparisons,
10173 but VMS Debug requires them to be set correctly. */
10175 int ver;
10176 long long cdt;
10177 long siz;
10178 int maxfilelen = strlen (files[file_idx].path)
10179 + dirs[dir_idx].length
10180 + MAX_VMS_VERSION_LEN + 1;
10181 char *filebuf = XALLOCAVEC (char, maxfilelen);
10183 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10184 snprintf (filebuf, maxfilelen, "%s;%d",
10185 files[file_idx].path + dirs[dir_idx].length, ver);
10187 dw2_asm_output_nstring
10188 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10190 /* Include directory index. */
10191 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10193 /* Modification time. */
10194 dw2_asm_output_data_uleb128
10195 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10196 ? cdt : 0,
10197 NULL);
10199 /* File length in bytes. */
10200 dw2_asm_output_data_uleb128
10201 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10202 ? siz : 0,
10203 NULL);
10204 #else
10205 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10206 "File Entry: %#x", (unsigned) i + 1);
10208 /* Include directory index. */
10209 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10211 /* Modification time. */
10212 dw2_asm_output_data_uleb128 (0, NULL);
10214 /* File length in bytes. */
10215 dw2_asm_output_data_uleb128 (0, NULL);
10216 #endif /* VMS_DEBUGGING_INFO */
10219 dw2_asm_output_data (1, 0, "End file name table");
10223 /* Output one line number table into the .debug_line section. */
10225 static void
10226 output_one_line_info_table (dw_line_info_table *table)
10228 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10229 unsigned int current_line = 1;
10230 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10231 dw_line_info_entry *ent;
10232 size_t i;
10234 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10236 switch (ent->opcode)
10238 case LI_set_address:
10239 /* ??? Unfortunately, we have little choice here currently, and
10240 must always use the most general form. GCC does not know the
10241 address delta itself, so we can't use DW_LNS_advance_pc. Many
10242 ports do have length attributes which will give an upper bound
10243 on the address range. We could perhaps use length attributes
10244 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10245 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10247 /* This can handle any delta. This takes
10248 4+DWARF2_ADDR_SIZE bytes. */
10249 dw2_asm_output_data (1, 0, "set address %s", line_label);
10250 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10251 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10252 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10253 break;
10255 case LI_set_line:
10256 if (ent->val == current_line)
10258 /* We still need to start a new row, so output a copy insn. */
10259 dw2_asm_output_data (1, DW_LNS_copy,
10260 "copy line %u", current_line);
10262 else
10264 int line_offset = ent->val - current_line;
10265 int line_delta = line_offset - DWARF_LINE_BASE;
10267 current_line = ent->val;
10268 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10270 /* This can handle deltas from -10 to 234, using the current
10271 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10272 This takes 1 byte. */
10273 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10274 "line %u", current_line);
10276 else
10278 /* This can handle any delta. This takes at least 4 bytes,
10279 depending on the value being encoded. */
10280 dw2_asm_output_data (1, DW_LNS_advance_line,
10281 "advance to line %u", current_line);
10282 dw2_asm_output_data_sleb128 (line_offset, NULL);
10283 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10286 break;
10288 case LI_set_file:
10289 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10290 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10291 break;
10293 case LI_set_column:
10294 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10295 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10296 break;
10298 case LI_negate_stmt:
10299 current_is_stmt = !current_is_stmt;
10300 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10301 "is_stmt %d", current_is_stmt);
10302 break;
10304 case LI_set_prologue_end:
10305 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10306 "set prologue end");
10307 break;
10309 case LI_set_epilogue_begin:
10310 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10311 "set epilogue begin");
10312 break;
10314 case LI_set_discriminator:
10315 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10316 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10317 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10318 dw2_asm_output_data_uleb128 (ent->val, NULL);
10319 break;
10323 /* Emit debug info for the address of the end of the table. */
10324 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10325 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10326 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10329 dw2_asm_output_data (1, 0, "end sequence");
10330 dw2_asm_output_data_uleb128 (1, NULL);
10331 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10334 /* Output the source line number correspondence information. This
10335 information goes into the .debug_line section. */
10337 static void
10338 output_line_info (bool prologue_only)
10340 char l1[20], l2[20], p1[20], p2[20];
10341 /* We don't support DWARFv5 line tables yet. */
10342 int ver = dwarf_version < 5 ? dwarf_version : 4;
10343 bool saw_one = false;
10344 int opc;
10346 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10347 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10348 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10349 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10351 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10352 dw2_asm_output_data (4, 0xffffffff,
10353 "Initial length escape value indicating 64-bit DWARF extension");
10354 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10355 "Length of Source Line Info");
10356 ASM_OUTPUT_LABEL (asm_out_file, l1);
10358 dw2_asm_output_data (2, ver, "DWARF Version");
10359 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10360 ASM_OUTPUT_LABEL (asm_out_file, p1);
10362 /* Define the architecture-dependent minimum instruction length (in bytes).
10363 In this implementation of DWARF, this field is used for information
10364 purposes only. Since GCC generates assembly language, we have no
10365 a priori knowledge of how many instruction bytes are generated for each
10366 source line, and therefore can use only the DW_LNE_set_address and
10367 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10368 this as '1', which is "correct enough" for all architectures,
10369 and don't let the target override. */
10370 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10372 if (ver >= 4)
10373 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10374 "Maximum Operations Per Instruction");
10375 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10376 "Default is_stmt_start flag");
10377 dw2_asm_output_data (1, DWARF_LINE_BASE,
10378 "Line Base Value (Special Opcodes)");
10379 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10380 "Line Range Value (Special Opcodes)");
10381 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10382 "Special Opcode Base");
10384 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10386 int n_op_args;
10387 switch (opc)
10389 case DW_LNS_advance_pc:
10390 case DW_LNS_advance_line:
10391 case DW_LNS_set_file:
10392 case DW_LNS_set_column:
10393 case DW_LNS_fixed_advance_pc:
10394 case DW_LNS_set_isa:
10395 n_op_args = 1;
10396 break;
10397 default:
10398 n_op_args = 0;
10399 break;
10402 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10403 opc, n_op_args);
10406 /* Write out the information about the files we use. */
10407 output_file_names ();
10408 ASM_OUTPUT_LABEL (asm_out_file, p2);
10409 if (prologue_only)
10411 /* Output the marker for the end of the line number info. */
10412 ASM_OUTPUT_LABEL (asm_out_file, l2);
10413 return;
10416 if (separate_line_info)
10418 dw_line_info_table *table;
10419 size_t i;
10421 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10422 if (table->in_use)
10424 output_one_line_info_table (table);
10425 saw_one = true;
10428 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10430 output_one_line_info_table (cold_text_section_line_info);
10431 saw_one = true;
10434 /* ??? Some Darwin linkers crash on a .debug_line section with no
10435 sequences. Further, merely a DW_LNE_end_sequence entry is not
10436 sufficient -- the address column must also be initialized.
10437 Make sure to output at least one set_address/end_sequence pair,
10438 choosing .text since that section is always present. */
10439 if (text_section_line_info->in_use || !saw_one)
10440 output_one_line_info_table (text_section_line_info);
10442 /* Output the marker for the end of the line number info. */
10443 ASM_OUTPUT_LABEL (asm_out_file, l2);
10446 /* Given a pointer to a tree node for some base type, return a pointer to
10447 a DIE that describes the given type.
10449 This routine must only be called for GCC type nodes that correspond to
10450 Dwarf base (fundamental) types. */
10452 static dw_die_ref
10453 base_type_die (tree type)
10455 dw_die_ref base_type_result;
10456 enum dwarf_type encoding;
10458 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10459 return 0;
10461 /* If this is a subtype that should not be emitted as a subrange type,
10462 use the base type. See subrange_type_for_debug_p. */
10463 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10464 type = TREE_TYPE (type);
10466 switch (TREE_CODE (type))
10468 case INTEGER_TYPE:
10469 if ((dwarf_version >= 4 || !dwarf_strict)
10470 && TYPE_NAME (type)
10471 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10472 && DECL_IS_BUILTIN (TYPE_NAME (type))
10473 && DECL_NAME (TYPE_NAME (type)))
10475 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10476 if (strcmp (name, "char16_t") == 0
10477 || strcmp (name, "char32_t") == 0)
10479 encoding = DW_ATE_UTF;
10480 break;
10483 if (TYPE_STRING_FLAG (type))
10485 if (TYPE_UNSIGNED (type))
10486 encoding = DW_ATE_unsigned_char;
10487 else
10488 encoding = DW_ATE_signed_char;
10490 else if (TYPE_UNSIGNED (type))
10491 encoding = DW_ATE_unsigned;
10492 else
10493 encoding = DW_ATE_signed;
10494 break;
10496 case REAL_TYPE:
10497 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10499 if (dwarf_version >= 3 || !dwarf_strict)
10500 encoding = DW_ATE_decimal_float;
10501 else
10502 encoding = DW_ATE_lo_user;
10504 else
10505 encoding = DW_ATE_float;
10506 break;
10508 case FIXED_POINT_TYPE:
10509 if (!(dwarf_version >= 3 || !dwarf_strict))
10510 encoding = DW_ATE_lo_user;
10511 else if (TYPE_UNSIGNED (type))
10512 encoding = DW_ATE_unsigned_fixed;
10513 else
10514 encoding = DW_ATE_signed_fixed;
10515 break;
10517 /* Dwarf2 doesn't know anything about complex ints, so use
10518 a user defined type for it. */
10519 case COMPLEX_TYPE:
10520 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10521 encoding = DW_ATE_complex_float;
10522 else
10523 encoding = DW_ATE_lo_user;
10524 break;
10526 case BOOLEAN_TYPE:
10527 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10528 encoding = DW_ATE_boolean;
10529 break;
10531 default:
10532 /* No other TREE_CODEs are Dwarf fundamental types. */
10533 gcc_unreachable ();
10536 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10538 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10539 int_size_in_bytes (type));
10540 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10541 add_pubtype (type, base_type_result);
10543 return base_type_result;
10546 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10547 named 'auto' in its type: return true for it, false otherwise. */
10549 static inline bool
10550 is_cxx_auto (tree type)
10552 if (is_cxx ())
10554 tree name = TYPE_IDENTIFIER (type);
10555 if (name == get_identifier ("auto")
10556 || name == get_identifier ("decltype(auto)"))
10557 return true;
10559 return false;
10562 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10563 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10565 static inline int
10566 is_base_type (tree type)
10568 switch (TREE_CODE (type))
10570 case ERROR_MARK:
10571 case VOID_TYPE:
10572 case INTEGER_TYPE:
10573 case REAL_TYPE:
10574 case FIXED_POINT_TYPE:
10575 case COMPLEX_TYPE:
10576 case BOOLEAN_TYPE:
10577 case POINTER_BOUNDS_TYPE:
10578 return 1;
10580 case ARRAY_TYPE:
10581 case RECORD_TYPE:
10582 case UNION_TYPE:
10583 case QUAL_UNION_TYPE:
10584 case ENUMERAL_TYPE:
10585 case FUNCTION_TYPE:
10586 case METHOD_TYPE:
10587 case POINTER_TYPE:
10588 case REFERENCE_TYPE:
10589 case NULLPTR_TYPE:
10590 case OFFSET_TYPE:
10591 case LANG_TYPE:
10592 case VECTOR_TYPE:
10593 return 0;
10595 default:
10596 if (is_cxx_auto (type))
10597 return 0;
10598 gcc_unreachable ();
10601 return 0;
10604 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10605 node, return the size in bits for the type if it is a constant, or else
10606 return the alignment for the type if the type's size is not constant, or
10607 else return BITS_PER_WORD if the type actually turns out to be an
10608 ERROR_MARK node. */
10610 static inline unsigned HOST_WIDE_INT
10611 simple_type_size_in_bits (const_tree type)
10613 if (TREE_CODE (type) == ERROR_MARK)
10614 return BITS_PER_WORD;
10615 else if (TYPE_SIZE (type) == NULL_TREE)
10616 return 0;
10617 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10618 return tree_to_uhwi (TYPE_SIZE (type));
10619 else
10620 return TYPE_ALIGN (type);
10623 /* Similarly, but return an offset_int instead of UHWI. */
10625 static inline offset_int
10626 offset_int_type_size_in_bits (const_tree type)
10628 if (TREE_CODE (type) == ERROR_MARK)
10629 return BITS_PER_WORD;
10630 else if (TYPE_SIZE (type) == NULL_TREE)
10631 return 0;
10632 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10633 return wi::to_offset (TYPE_SIZE (type));
10634 else
10635 return TYPE_ALIGN (type);
10638 /* Given a pointer to a tree node for a subrange type, return a pointer
10639 to a DIE that describes the given type. */
10641 static dw_die_ref
10642 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10644 dw_die_ref subrange_die;
10645 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10647 if (context_die == NULL)
10648 context_die = comp_unit_die ();
10650 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10652 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10654 /* The size of the subrange type and its base type do not match,
10655 so we need to generate a size attribute for the subrange type. */
10656 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10659 if (low)
10660 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10661 if (high)
10662 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10664 return subrange_die;
10667 /* Returns the (const and/or volatile) cv_qualifiers associated with
10668 the decl node. This will normally be augmented with the
10669 cv_qualifiers of the underlying type in add_type_attribute. */
10671 static int
10672 decl_quals (const_tree decl)
10674 return ((TREE_READONLY (decl)
10675 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10676 | (TREE_THIS_VOLATILE (decl)
10677 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10680 /* Determine the TYPE whose qualifiers match the largest strict subset
10681 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10682 qualifiers outside QUAL_MASK. */
10684 static int
10685 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10687 tree t;
10688 int best_rank = 0, best_qual = 0, max_rank;
10690 type_quals &= qual_mask;
10691 max_rank = popcount_hwi (type_quals) - 1;
10693 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10694 t = TYPE_NEXT_VARIANT (t))
10696 int q = TYPE_QUALS (t) & qual_mask;
10698 if ((q & type_quals) == q && q != type_quals
10699 && check_base_type (t, type))
10701 int rank = popcount_hwi (q);
10703 if (rank > best_rank)
10705 best_rank = rank;
10706 best_qual = q;
10711 return best_qual;
10714 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10715 entry that chains various modifiers in front of the given type. */
10717 static dw_die_ref
10718 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10720 enum tree_code code = TREE_CODE (type);
10721 dw_die_ref mod_type_die;
10722 dw_die_ref sub_die = NULL;
10723 tree item_type = NULL;
10724 tree qualified_type;
10725 tree name, low, high;
10726 dw_die_ref mod_scope;
10727 /* Only these cv-qualifiers are currently handled. */
10728 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10729 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10731 if (code == ERROR_MARK)
10732 return NULL;
10734 cv_quals &= cv_qual_mask;
10736 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10737 tag modifier (and not an attribute) old consumers won't be able
10738 to handle it. */
10739 if (dwarf_version < 3)
10740 cv_quals &= ~TYPE_QUAL_RESTRICT;
10742 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10743 if (dwarf_version < 5)
10744 cv_quals &= ~TYPE_QUAL_ATOMIC;
10746 /* See if we already have the appropriately qualified variant of
10747 this type. */
10748 qualified_type = get_qualified_type (type, cv_quals);
10750 if (qualified_type == sizetype
10751 && TYPE_NAME (qualified_type)
10752 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10754 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10756 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10757 && TYPE_PRECISION (t)
10758 == TYPE_PRECISION (qualified_type)
10759 && TYPE_UNSIGNED (t)
10760 == TYPE_UNSIGNED (qualified_type));
10761 qualified_type = t;
10764 /* If we do, then we can just use its DIE, if it exists. */
10765 if (qualified_type)
10767 mod_type_die = lookup_type_die (qualified_type);
10768 if (mod_type_die)
10769 return mod_type_die;
10772 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10774 /* Handle C typedef types. */
10775 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10776 && !DECL_ARTIFICIAL (name))
10778 tree dtype = TREE_TYPE (name);
10780 if (qualified_type == dtype)
10782 /* For a named type, use the typedef. */
10783 gen_type_die (qualified_type, context_die);
10784 return lookup_type_die (qualified_type);
10786 else
10788 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10789 dquals &= cv_qual_mask;
10790 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10791 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10792 /* cv-unqualified version of named type. Just use
10793 the unnamed type to which it refers. */
10794 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10795 cv_quals, context_die);
10796 /* Else cv-qualified version of named type; fall through. */
10800 mod_scope = scope_die_for (type, context_die);
10802 if (cv_quals)
10804 struct qual_info { int q; enum dwarf_tag t; };
10805 static const struct qual_info qual_info[] =
10807 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10808 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10809 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10810 { TYPE_QUAL_CONST, DW_TAG_const_type },
10812 int sub_quals;
10813 unsigned i;
10815 /* Determine a lesser qualified type that most closely matches
10816 this one. Then generate DW_TAG_* entries for the remaining
10817 qualifiers. */
10818 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10819 cv_qual_mask);
10820 mod_type_die = modified_type_die (type, sub_quals, context_die);
10822 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10823 if (qual_info[i].q & cv_quals & ~sub_quals)
10825 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10826 if (mod_type_die)
10827 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10828 mod_type_die = d;
10831 else if (code == POINTER_TYPE)
10833 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10834 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10835 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10836 item_type = TREE_TYPE (type);
10837 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10838 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10839 TYPE_ADDR_SPACE (item_type));
10841 else if (code == REFERENCE_TYPE)
10843 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10844 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10845 type);
10846 else
10847 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10848 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10849 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10850 item_type = TREE_TYPE (type);
10851 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10852 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10853 TYPE_ADDR_SPACE (item_type));
10855 else if (code == INTEGER_TYPE
10856 && TREE_TYPE (type) != NULL_TREE
10857 && subrange_type_for_debug_p (type, &low, &high))
10859 mod_type_die = subrange_type_die (type, low, high, context_die);
10860 item_type = TREE_TYPE (type);
10862 else if (is_base_type (type))
10863 mod_type_die = base_type_die (type);
10864 else
10866 gen_type_die (type, context_die);
10868 /* We have to get the type_main_variant here (and pass that to the
10869 `lookup_type_die' routine) because the ..._TYPE node we have
10870 might simply be a *copy* of some original type node (where the
10871 copy was created to help us keep track of typedef names) and
10872 that copy might have a different TYPE_UID from the original
10873 ..._TYPE node. */
10874 if (TREE_CODE (type) != VECTOR_TYPE)
10875 return lookup_type_die (type_main_variant (type));
10876 else
10877 /* Vectors have the debugging information in the type,
10878 not the main variant. */
10879 return lookup_type_die (type);
10882 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10883 don't output a DW_TAG_typedef, since there isn't one in the
10884 user's program; just attach a DW_AT_name to the type.
10885 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10886 if the base type already has the same name. */
10887 if (name
10888 && ((TREE_CODE (name) != TYPE_DECL
10889 && (qualified_type == TYPE_MAIN_VARIANT (type)
10890 || (cv_quals == TYPE_UNQUALIFIED)))
10891 || (TREE_CODE (name) == TYPE_DECL
10892 && TREE_TYPE (name) == qualified_type
10893 && DECL_NAME (name))))
10895 if (TREE_CODE (name) == TYPE_DECL)
10896 /* Could just call add_name_and_src_coords_attributes here,
10897 but since this is a builtin type it doesn't have any
10898 useful source coordinates anyway. */
10899 name = DECL_NAME (name);
10900 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10902 /* This probably indicates a bug. */
10903 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10905 name = TYPE_IDENTIFIER (type);
10906 add_name_attribute (mod_type_die,
10907 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10910 if (qualified_type)
10911 equate_type_number_to_die (qualified_type, mod_type_die);
10913 if (item_type)
10914 /* We must do this after the equate_type_number_to_die call, in case
10915 this is a recursive type. This ensures that the modified_type_die
10916 recursion will terminate even if the type is recursive. Recursive
10917 types are possible in Ada. */
10918 sub_die = modified_type_die (item_type,
10919 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10920 context_die);
10922 if (sub_die != NULL)
10923 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10925 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10926 if (TYPE_ARTIFICIAL (type))
10927 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10929 return mod_type_die;
10932 /* Generate DIEs for the generic parameters of T.
10933 T must be either a generic type or a generic function.
10934 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10936 static void
10937 gen_generic_params_dies (tree t)
10939 tree parms, args;
10940 int parms_num, i;
10941 dw_die_ref die = NULL;
10942 int non_default;
10944 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10945 return;
10947 if (TYPE_P (t))
10948 die = lookup_type_die (t);
10949 else if (DECL_P (t))
10950 die = lookup_decl_die (t);
10952 gcc_assert (die);
10954 parms = lang_hooks.get_innermost_generic_parms (t);
10955 if (!parms)
10956 /* T has no generic parameter. It means T is neither a generic type
10957 or function. End of story. */
10958 return;
10960 parms_num = TREE_VEC_LENGTH (parms);
10961 args = lang_hooks.get_innermost_generic_args (t);
10962 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10963 non_default = int_cst_value (TREE_CHAIN (args));
10964 else
10965 non_default = TREE_VEC_LENGTH (args);
10966 for (i = 0; i < parms_num; i++)
10968 tree parm, arg, arg_pack_elems;
10969 dw_die_ref parm_die;
10971 parm = TREE_VEC_ELT (parms, i);
10972 arg = TREE_VEC_ELT (args, i);
10973 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10974 gcc_assert (parm && TREE_VALUE (parm) && arg);
10976 if (parm && TREE_VALUE (parm) && arg)
10978 /* If PARM represents a template parameter pack,
10979 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10980 by DW_TAG_template_*_parameter DIEs for the argument
10981 pack elements of ARG. Note that ARG would then be
10982 an argument pack. */
10983 if (arg_pack_elems)
10984 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10985 arg_pack_elems,
10986 die);
10987 else
10988 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10989 true /* emit name */, die);
10990 if (i >= non_default)
10991 add_AT_flag (parm_die, DW_AT_default_value, 1);
10996 /* Create and return a DIE for PARM which should be
10997 the representation of a generic type parameter.
10998 For instance, in the C++ front end, PARM would be a template parameter.
10999 ARG is the argument to PARM.
11000 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11001 name of the PARM.
11002 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11003 as a child node. */
11005 static dw_die_ref
11006 generic_parameter_die (tree parm, tree arg,
11007 bool emit_name_p,
11008 dw_die_ref parent_die)
11010 dw_die_ref tmpl_die = NULL;
11011 const char *name = NULL;
11013 if (!parm || !DECL_NAME (parm) || !arg)
11014 return NULL;
11016 /* We support non-type generic parameters and arguments,
11017 type generic parameters and arguments, as well as
11018 generic generic parameters (a.k.a. template template parameters in C++)
11019 and arguments. */
11020 if (TREE_CODE (parm) == PARM_DECL)
11021 /* PARM is a nontype generic parameter */
11022 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11023 else if (TREE_CODE (parm) == TYPE_DECL)
11024 /* PARM is a type generic parameter. */
11025 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11026 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11027 /* PARM is a generic generic parameter.
11028 Its DIE is a GNU extension. It shall have a
11029 DW_AT_name attribute to represent the name of the template template
11030 parameter, and a DW_AT_GNU_template_name attribute to represent the
11031 name of the template template argument. */
11032 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11033 parent_die, parm);
11034 else
11035 gcc_unreachable ();
11037 if (tmpl_die)
11039 tree tmpl_type;
11041 /* If PARM is a generic parameter pack, it means we are
11042 emitting debug info for a template argument pack element.
11043 In other terms, ARG is a template argument pack element.
11044 In that case, we don't emit any DW_AT_name attribute for
11045 the die. */
11046 if (emit_name_p)
11048 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11049 gcc_assert (name);
11050 add_AT_string (tmpl_die, DW_AT_name, name);
11053 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11055 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11056 TMPL_DIE should have a child DW_AT_type attribute that is set
11057 to the type of the argument to PARM, which is ARG.
11058 If PARM is a type generic parameter, TMPL_DIE should have a
11059 child DW_AT_type that is set to ARG. */
11060 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11061 add_type_attribute (tmpl_die, tmpl_type,
11062 (TREE_THIS_VOLATILE (tmpl_type)
11063 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11064 parent_die);
11066 else
11068 /* So TMPL_DIE is a DIE representing a
11069 a generic generic template parameter, a.k.a template template
11070 parameter in C++ and arg is a template. */
11072 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11073 to the name of the argument. */
11074 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11075 if (name)
11076 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11079 if (TREE_CODE (parm) == PARM_DECL)
11080 /* So PARM is a non-type generic parameter.
11081 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11082 attribute of TMPL_DIE which value represents the value
11083 of ARG.
11084 We must be careful here:
11085 The value of ARG might reference some function decls.
11086 We might currently be emitting debug info for a generic
11087 type and types are emitted before function decls, we don't
11088 know if the function decls referenced by ARG will actually be
11089 emitted after cgraph computations.
11090 So must defer the generation of the DW_AT_const_value to
11091 after cgraph is ready. */
11092 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11095 return tmpl_die;
11098 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11099 PARM_PACK must be a template parameter pack. The returned DIE
11100 will be child DIE of PARENT_DIE. */
11102 static dw_die_ref
11103 template_parameter_pack_die (tree parm_pack,
11104 tree parm_pack_args,
11105 dw_die_ref parent_die)
11107 dw_die_ref die;
11108 int j;
11110 gcc_assert (parent_die && parm_pack);
11112 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11113 add_name_and_src_coords_attributes (die, parm_pack);
11114 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11115 generic_parameter_die (parm_pack,
11116 TREE_VEC_ELT (parm_pack_args, j),
11117 false /* Don't emit DW_AT_name */,
11118 die);
11119 return die;
11122 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11123 an enumerated type. */
11125 static inline int
11126 type_is_enum (const_tree type)
11128 return TREE_CODE (type) == ENUMERAL_TYPE;
11131 /* Return the DBX register number described by a given RTL node. */
11133 static unsigned int
11134 dbx_reg_number (const_rtx rtl)
11136 unsigned regno = REGNO (rtl);
11138 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11140 #ifdef LEAF_REG_REMAP
11141 if (crtl->uses_only_leaf_regs)
11143 int leaf_reg = LEAF_REG_REMAP (regno);
11144 if (leaf_reg != -1)
11145 regno = (unsigned) leaf_reg;
11147 #endif
11149 regno = DBX_REGISTER_NUMBER (regno);
11150 gcc_assert (regno != INVALID_REGNUM);
11151 return regno;
11154 /* Optionally add a DW_OP_piece term to a location description expression.
11155 DW_OP_piece is only added if the location description expression already
11156 doesn't end with DW_OP_piece. */
11158 static void
11159 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11161 dw_loc_descr_ref loc;
11163 if (*list_head != NULL)
11165 /* Find the end of the chain. */
11166 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11169 if (loc->dw_loc_opc != DW_OP_piece)
11170 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11174 /* Return a location descriptor that designates a machine register or
11175 zero if there is none. */
11177 static dw_loc_descr_ref
11178 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11180 rtx regs;
11182 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11183 return 0;
11185 /* We only use "frame base" when we're sure we're talking about the
11186 post-prologue local stack frame. We do this by *not* running
11187 register elimination until this point, and recognizing the special
11188 argument pointer and soft frame pointer rtx's.
11189 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11190 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11191 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11193 dw_loc_descr_ref result = NULL;
11195 if (dwarf_version >= 4 || !dwarf_strict)
11197 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11198 initialized);
11199 if (result)
11200 add_loc_descr (&result,
11201 new_loc_descr (DW_OP_stack_value, 0, 0));
11203 return result;
11206 regs = targetm.dwarf_register_span (rtl);
11208 if (REG_NREGS (rtl) > 1 || regs)
11209 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11210 else
11212 unsigned int dbx_regnum = dbx_reg_number (rtl);
11213 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11214 return 0;
11215 return one_reg_loc_descriptor (dbx_regnum, initialized);
11219 /* Return a location descriptor that designates a machine register for
11220 a given hard register number. */
11222 static dw_loc_descr_ref
11223 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11225 dw_loc_descr_ref reg_loc_descr;
11227 if (regno <= 31)
11228 reg_loc_descr
11229 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11230 else
11231 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11233 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11234 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11236 return reg_loc_descr;
11239 /* Given an RTL of a register, return a location descriptor that
11240 designates a value that spans more than one register. */
11242 static dw_loc_descr_ref
11243 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11244 enum var_init_status initialized)
11246 int size, i;
11247 dw_loc_descr_ref loc_result = NULL;
11249 /* Simple, contiguous registers. */
11250 if (regs == NULL_RTX)
11252 unsigned reg = REGNO (rtl);
11253 int nregs;
11255 #ifdef LEAF_REG_REMAP
11256 if (crtl->uses_only_leaf_regs)
11258 int leaf_reg = LEAF_REG_REMAP (reg);
11259 if (leaf_reg != -1)
11260 reg = (unsigned) leaf_reg;
11262 #endif
11264 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11265 nregs = REG_NREGS (rtl);
11267 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11269 loc_result = NULL;
11270 while (nregs--)
11272 dw_loc_descr_ref t;
11274 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11275 VAR_INIT_STATUS_INITIALIZED);
11276 add_loc_descr (&loc_result, t);
11277 add_loc_descr_op_piece (&loc_result, size);
11278 ++reg;
11280 return loc_result;
11283 /* Now onto stupid register sets in non contiguous locations. */
11285 gcc_assert (GET_CODE (regs) == PARALLEL);
11287 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11288 loc_result = NULL;
11290 for (i = 0; i < XVECLEN (regs, 0); ++i)
11292 dw_loc_descr_ref t;
11294 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11295 VAR_INIT_STATUS_INITIALIZED);
11296 add_loc_descr (&loc_result, t);
11297 add_loc_descr_op_piece (&loc_result, size);
11300 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11301 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11302 return loc_result;
11305 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11307 /* Return a location descriptor that designates a constant i,
11308 as a compound operation from constant (i >> shift), constant shift
11309 and DW_OP_shl. */
11311 static dw_loc_descr_ref
11312 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11314 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11315 add_loc_descr (&ret, int_loc_descriptor (shift));
11316 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11317 return ret;
11320 /* Return a location descriptor that designates a constant. */
11322 static dw_loc_descr_ref
11323 int_loc_descriptor (HOST_WIDE_INT i)
11325 enum dwarf_location_atom op;
11327 /* Pick the smallest representation of a constant, rather than just
11328 defaulting to the LEB encoding. */
11329 if (i >= 0)
11331 int clz = clz_hwi (i);
11332 int ctz = ctz_hwi (i);
11333 if (i <= 31)
11334 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11335 else if (i <= 0xff)
11336 op = DW_OP_const1u;
11337 else if (i <= 0xffff)
11338 op = DW_OP_const2u;
11339 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11340 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11341 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11342 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11343 while DW_OP_const4u is 5 bytes. */
11344 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11345 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11346 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11347 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11348 while DW_OP_const4u is 5 bytes. */
11349 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11350 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11351 op = DW_OP_const4u;
11352 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11353 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11354 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11355 while DW_OP_constu of constant >= 0x100000000 takes at least
11356 6 bytes. */
11357 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11358 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11359 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11360 >= HOST_BITS_PER_WIDE_INT)
11361 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11362 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11363 while DW_OP_constu takes in this case at least 6 bytes. */
11364 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11365 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11366 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11367 && size_of_uleb128 (i) > 6)
11368 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11369 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11370 else
11371 op = DW_OP_constu;
11373 else
11375 if (i >= -0x80)
11376 op = DW_OP_const1s;
11377 else if (i >= -0x8000)
11378 op = DW_OP_const2s;
11379 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11381 if (size_of_int_loc_descriptor (i) < 5)
11383 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11384 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11385 return ret;
11387 op = DW_OP_const4s;
11389 else
11391 if (size_of_int_loc_descriptor (i)
11392 < (unsigned long) 1 + size_of_sleb128 (i))
11394 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11395 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11396 return ret;
11398 op = DW_OP_consts;
11402 return new_loc_descr (op, i, 0);
11405 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11406 without actually allocating it. */
11408 static unsigned long
11409 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11411 return size_of_int_loc_descriptor (i >> shift)
11412 + size_of_int_loc_descriptor (shift)
11413 + 1;
11416 /* Return size_of_locs (int_loc_descriptor (i)) without
11417 actually allocating it. */
11419 static unsigned long
11420 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11422 unsigned long s;
11424 if (i >= 0)
11426 int clz, ctz;
11427 if (i <= 31)
11428 return 1;
11429 else if (i <= 0xff)
11430 return 2;
11431 else if (i <= 0xffff)
11432 return 3;
11433 clz = clz_hwi (i);
11434 ctz = ctz_hwi (i);
11435 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11436 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11437 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11438 - clz - 5);
11439 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11440 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11441 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11442 - clz - 8);
11443 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11444 return 5;
11445 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11446 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11447 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11448 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11449 - clz - 8);
11450 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11451 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11452 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11453 - clz - 16);
11454 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11455 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11456 && s > 6)
11457 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11458 - clz - 32);
11459 else
11460 return 1 + s;
11462 else
11464 if (i >= -0x80)
11465 return 2;
11466 else if (i >= -0x8000)
11467 return 3;
11468 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11470 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11472 s = size_of_int_loc_descriptor (-i) + 1;
11473 if (s < 5)
11474 return s;
11476 return 5;
11478 else
11480 unsigned long r = 1 + size_of_sleb128 (i);
11481 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11483 s = size_of_int_loc_descriptor (-i) + 1;
11484 if (s < r)
11485 return s;
11487 return r;
11492 /* Return loc description representing "address" of integer value.
11493 This can appear only as toplevel expression. */
11495 static dw_loc_descr_ref
11496 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11498 int litsize;
11499 dw_loc_descr_ref loc_result = NULL;
11501 if (!(dwarf_version >= 4 || !dwarf_strict))
11502 return NULL;
11504 litsize = size_of_int_loc_descriptor (i);
11505 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11506 is more compact. For DW_OP_stack_value we need:
11507 litsize + 1 (DW_OP_stack_value)
11508 and for DW_OP_implicit_value:
11509 1 (DW_OP_implicit_value) + 1 (length) + size. */
11510 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11512 loc_result = int_loc_descriptor (i);
11513 add_loc_descr (&loc_result,
11514 new_loc_descr (DW_OP_stack_value, 0, 0));
11515 return loc_result;
11518 loc_result = new_loc_descr (DW_OP_implicit_value,
11519 size, 0);
11520 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11521 loc_result->dw_loc_oprnd2.v.val_int = i;
11522 return loc_result;
11525 /* Return a location descriptor that designates a base+offset location. */
11527 static dw_loc_descr_ref
11528 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11529 enum var_init_status initialized)
11531 unsigned int regno;
11532 dw_loc_descr_ref result;
11533 dw_fde_ref fde = cfun->fde;
11535 /* We only use "frame base" when we're sure we're talking about the
11536 post-prologue local stack frame. We do this by *not* running
11537 register elimination until this point, and recognizing the special
11538 argument pointer and soft frame pointer rtx's. */
11539 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11541 rtx elim = (ira_use_lra_p
11542 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11543 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11545 if (elim != reg)
11547 if (GET_CODE (elim) == PLUS)
11549 offset += INTVAL (XEXP (elim, 1));
11550 elim = XEXP (elim, 0);
11552 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11553 && (elim == hard_frame_pointer_rtx
11554 || elim == stack_pointer_rtx))
11555 || elim == (frame_pointer_needed
11556 ? hard_frame_pointer_rtx
11557 : stack_pointer_rtx));
11559 /* If drap register is used to align stack, use frame
11560 pointer + offset to access stack variables. If stack
11561 is aligned without drap, use stack pointer + offset to
11562 access stack variables. */
11563 if (crtl->stack_realign_tried
11564 && reg == frame_pointer_rtx)
11566 int base_reg
11567 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11568 ? HARD_FRAME_POINTER_REGNUM
11569 : REGNO (elim));
11570 return new_reg_loc_descr (base_reg, offset);
11573 gcc_assert (frame_pointer_fb_offset_valid);
11574 offset += frame_pointer_fb_offset;
11575 return new_loc_descr (DW_OP_fbreg, offset, 0);
11579 regno = REGNO (reg);
11580 #ifdef LEAF_REG_REMAP
11581 if (crtl->uses_only_leaf_regs)
11583 int leaf_reg = LEAF_REG_REMAP (regno);
11584 if (leaf_reg != -1)
11585 regno = (unsigned) leaf_reg;
11587 #endif
11588 regno = DWARF_FRAME_REGNUM (regno);
11590 if (!optimize && fde
11591 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11593 /* Use cfa+offset to represent the location of arguments passed
11594 on the stack when drap is used to align stack.
11595 Only do this when not optimizing, for optimized code var-tracking
11596 is supposed to track where the arguments live and the register
11597 used as vdrap or drap in some spot might be used for something
11598 else in other part of the routine. */
11599 return new_loc_descr (DW_OP_fbreg, offset, 0);
11602 if (regno <= 31)
11603 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11604 offset, 0);
11605 else
11606 result = new_loc_descr (DW_OP_bregx, regno, offset);
11608 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11609 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11611 return result;
11614 /* Return true if this RTL expression describes a base+offset calculation. */
11616 static inline int
11617 is_based_loc (const_rtx rtl)
11619 return (GET_CODE (rtl) == PLUS
11620 && ((REG_P (XEXP (rtl, 0))
11621 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11622 && CONST_INT_P (XEXP (rtl, 1)))));
11625 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11626 failed. */
11628 static dw_loc_descr_ref
11629 tls_mem_loc_descriptor (rtx mem)
11631 tree base;
11632 dw_loc_descr_ref loc_result;
11634 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11635 return NULL;
11637 base = get_base_address (MEM_EXPR (mem));
11638 if (base == NULL
11639 || TREE_CODE (base) != VAR_DECL
11640 || !DECL_THREAD_LOCAL_P (base))
11641 return NULL;
11643 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11644 if (loc_result == NULL)
11645 return NULL;
11647 if (MEM_OFFSET (mem))
11648 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11650 return loc_result;
11653 /* Output debug info about reason why we failed to expand expression as dwarf
11654 expression. */
11656 static void
11657 expansion_failed (tree expr, rtx rtl, char const *reason)
11659 if (dump_file && (dump_flags & TDF_DETAILS))
11661 fprintf (dump_file, "Failed to expand as dwarf: ");
11662 if (expr)
11663 print_generic_expr (dump_file, expr, dump_flags);
11664 if (rtl)
11666 fprintf (dump_file, "\n");
11667 print_rtl (dump_file, rtl);
11669 fprintf (dump_file, "\nReason: %s\n", reason);
11673 /* Helper function for const_ok_for_output. */
11675 static bool
11676 const_ok_for_output_1 (rtx rtl)
11678 if (GET_CODE (rtl) == UNSPEC)
11680 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11681 we can't express it in the debug info. */
11682 #ifdef ENABLE_CHECKING
11683 /* Don't complain about TLS UNSPECs, those are just too hard to
11684 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11685 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11686 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11687 if (XVECLEN (rtl, 0) == 0
11688 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11689 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11690 inform (current_function_decl
11691 ? DECL_SOURCE_LOCATION (current_function_decl)
11692 : UNKNOWN_LOCATION,
11693 #if NUM_UNSPEC_VALUES > 0
11694 "non-delegitimized UNSPEC %s (%d) found in variable location",
11695 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11696 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11697 XINT (rtl, 1));
11698 #else
11699 "non-delegitimized UNSPEC %d found in variable location",
11700 XINT (rtl, 1));
11701 #endif
11702 #endif
11703 expansion_failed (NULL_TREE, rtl,
11704 "UNSPEC hasn't been delegitimized.\n");
11705 return false;
11708 if (targetm.const_not_ok_for_debug_p (rtl))
11710 expansion_failed (NULL_TREE, rtl,
11711 "Expression rejected for debug by the backend.\n");
11712 return false;
11715 /* FIXME: Refer to PR60655. It is possible for simplification
11716 of rtl expressions in var tracking to produce such expressions.
11717 We should really identify / validate expressions
11718 enclosed in CONST that can be handled by assemblers on various
11719 targets and only handle legitimate cases here. */
11720 if (GET_CODE (rtl) != SYMBOL_REF)
11722 if (GET_CODE (rtl) == NOT)
11723 return false;
11724 return true;
11727 if (CONSTANT_POOL_ADDRESS_P (rtl))
11729 bool marked;
11730 get_pool_constant_mark (rtl, &marked);
11731 /* If all references to this pool constant were optimized away,
11732 it was not output and thus we can't represent it. */
11733 if (!marked)
11735 expansion_failed (NULL_TREE, rtl,
11736 "Constant was removed from constant pool.\n");
11737 return false;
11741 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11742 return false;
11744 /* Avoid references to external symbols in debug info, on several targets
11745 the linker might even refuse to link when linking a shared library,
11746 and in many other cases the relocations for .debug_info/.debug_loc are
11747 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11748 to be defined within the same shared library or executable are fine. */
11749 if (SYMBOL_REF_EXTERNAL_P (rtl))
11751 tree decl = SYMBOL_REF_DECL (rtl);
11753 if (decl == NULL || !targetm.binds_local_p (decl))
11755 expansion_failed (NULL_TREE, rtl,
11756 "Symbol not defined in current TU.\n");
11757 return false;
11761 return true;
11764 /* Return true if constant RTL can be emitted in DW_OP_addr or
11765 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11766 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11768 static bool
11769 const_ok_for_output (rtx rtl)
11771 if (GET_CODE (rtl) == SYMBOL_REF)
11772 return const_ok_for_output_1 (rtl);
11774 if (GET_CODE (rtl) == CONST)
11776 subrtx_var_iterator::array_type array;
11777 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11778 if (!const_ok_for_output_1 (*iter))
11779 return false;
11780 return true;
11783 return true;
11786 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11787 if possible, NULL otherwise. */
11789 static dw_die_ref
11790 base_type_for_mode (machine_mode mode, bool unsignedp)
11792 dw_die_ref type_die;
11793 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11795 if (type == NULL)
11796 return NULL;
11797 switch (TREE_CODE (type))
11799 case INTEGER_TYPE:
11800 case REAL_TYPE:
11801 break;
11802 default:
11803 return NULL;
11805 type_die = lookup_type_die (type);
11806 if (!type_die)
11807 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11808 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11809 return NULL;
11810 return type_die;
11813 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11814 type matching MODE, or, if MODE is narrower than or as wide as
11815 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11816 possible. */
11818 static dw_loc_descr_ref
11819 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11821 machine_mode outer_mode = mode;
11822 dw_die_ref type_die;
11823 dw_loc_descr_ref cvt;
11825 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11827 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11828 return op;
11830 type_die = base_type_for_mode (outer_mode, 1);
11831 if (type_die == NULL)
11832 return NULL;
11833 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11834 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11835 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11836 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11837 add_loc_descr (&op, cvt);
11838 return op;
11841 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11843 static dw_loc_descr_ref
11844 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11845 dw_loc_descr_ref op1)
11847 dw_loc_descr_ref ret = op0;
11848 add_loc_descr (&ret, op1);
11849 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11850 if (STORE_FLAG_VALUE != 1)
11852 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11853 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11855 return ret;
11858 /* Return location descriptor for signed comparison OP RTL. */
11860 static dw_loc_descr_ref
11861 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11862 machine_mode mem_mode)
11864 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11865 dw_loc_descr_ref op0, op1;
11866 int shift;
11868 if (op_mode == VOIDmode)
11869 op_mode = GET_MODE (XEXP (rtl, 1));
11870 if (op_mode == VOIDmode)
11871 return NULL;
11873 if (dwarf_strict
11874 && (GET_MODE_CLASS (op_mode) != MODE_INT
11875 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11876 return NULL;
11878 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11879 VAR_INIT_STATUS_INITIALIZED);
11880 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11881 VAR_INIT_STATUS_INITIALIZED);
11883 if (op0 == NULL || op1 == NULL)
11884 return NULL;
11886 if (GET_MODE_CLASS (op_mode) != MODE_INT
11887 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11888 return compare_loc_descriptor (op, op0, op1);
11890 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11892 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11893 dw_loc_descr_ref cvt;
11895 if (type_die == NULL)
11896 return NULL;
11897 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11898 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11899 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11900 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11901 add_loc_descr (&op0, cvt);
11902 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11903 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11904 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11905 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11906 add_loc_descr (&op1, cvt);
11907 return compare_loc_descriptor (op, op0, op1);
11910 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11911 /* For eq/ne, if the operands are known to be zero-extended,
11912 there is no need to do the fancy shifting up. */
11913 if (op == DW_OP_eq || op == DW_OP_ne)
11915 dw_loc_descr_ref last0, last1;
11916 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11918 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11920 /* deref_size zero extends, and for constants we can check
11921 whether they are zero extended or not. */
11922 if (((last0->dw_loc_opc == DW_OP_deref_size
11923 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11924 || (CONST_INT_P (XEXP (rtl, 0))
11925 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11926 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11927 && ((last1->dw_loc_opc == DW_OP_deref_size
11928 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11929 || (CONST_INT_P (XEXP (rtl, 1))
11930 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11931 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11932 return compare_loc_descriptor (op, op0, op1);
11934 /* EQ/NE comparison against constant in narrower type than
11935 DWARF2_ADDR_SIZE can be performed either as
11936 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11937 DW_OP_{eq,ne}
11939 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11940 DW_OP_{eq,ne}. Pick whatever is shorter. */
11941 if (CONST_INT_P (XEXP (rtl, 1))
11942 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11943 && (size_of_int_loc_descriptor (shift) + 1
11944 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11945 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11946 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11947 & GET_MODE_MASK (op_mode))))
11949 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11950 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11951 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11952 & GET_MODE_MASK (op_mode));
11953 return compare_loc_descriptor (op, op0, op1);
11956 add_loc_descr (&op0, int_loc_descriptor (shift));
11957 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11958 if (CONST_INT_P (XEXP (rtl, 1)))
11959 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11960 else
11962 add_loc_descr (&op1, int_loc_descriptor (shift));
11963 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11965 return compare_loc_descriptor (op, op0, op1);
11968 /* Return location descriptor for unsigned comparison OP RTL. */
11970 static dw_loc_descr_ref
11971 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11972 machine_mode mem_mode)
11974 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11975 dw_loc_descr_ref op0, op1;
11977 if (op_mode == VOIDmode)
11978 op_mode = GET_MODE (XEXP (rtl, 1));
11979 if (op_mode == VOIDmode)
11980 return NULL;
11981 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11982 return NULL;
11984 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11985 return NULL;
11987 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11988 VAR_INIT_STATUS_INITIALIZED);
11989 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11990 VAR_INIT_STATUS_INITIALIZED);
11992 if (op0 == NULL || op1 == NULL)
11993 return NULL;
11995 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11997 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11998 dw_loc_descr_ref last0, last1;
11999 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12001 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12003 if (CONST_INT_P (XEXP (rtl, 0)))
12004 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12005 /* deref_size zero extends, so no need to mask it again. */
12006 else if (last0->dw_loc_opc != DW_OP_deref_size
12007 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12009 add_loc_descr (&op0, int_loc_descriptor (mask));
12010 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12012 if (CONST_INT_P (XEXP (rtl, 1)))
12013 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12014 /* deref_size zero extends, so no need to mask it again. */
12015 else if (last1->dw_loc_opc != DW_OP_deref_size
12016 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12018 add_loc_descr (&op1, int_loc_descriptor (mask));
12019 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12022 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12024 HOST_WIDE_INT bias = 1;
12025 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12026 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12027 if (CONST_INT_P (XEXP (rtl, 1)))
12028 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12029 + INTVAL (XEXP (rtl, 1)));
12030 else
12031 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12032 bias, 0));
12034 return compare_loc_descriptor (op, op0, op1);
12037 /* Return location descriptor for {U,S}{MIN,MAX}. */
12039 static dw_loc_descr_ref
12040 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12041 machine_mode mem_mode)
12043 enum dwarf_location_atom op;
12044 dw_loc_descr_ref op0, op1, ret;
12045 dw_loc_descr_ref bra_node, drop_node;
12047 if (dwarf_strict
12048 && (GET_MODE_CLASS (mode) != MODE_INT
12049 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12050 return NULL;
12052 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12053 VAR_INIT_STATUS_INITIALIZED);
12054 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12055 VAR_INIT_STATUS_INITIALIZED);
12057 if (op0 == NULL || op1 == NULL)
12058 return NULL;
12060 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12061 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12062 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12063 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12065 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12067 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12068 add_loc_descr (&op0, int_loc_descriptor (mask));
12069 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12070 add_loc_descr (&op1, int_loc_descriptor (mask));
12071 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12073 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12075 HOST_WIDE_INT bias = 1;
12076 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12077 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12078 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12081 else if (GET_MODE_CLASS (mode) == MODE_INT
12082 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12084 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12085 add_loc_descr (&op0, int_loc_descriptor (shift));
12086 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12087 add_loc_descr (&op1, int_loc_descriptor (shift));
12088 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12090 else if (GET_MODE_CLASS (mode) == MODE_INT
12091 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12093 dw_die_ref type_die = base_type_for_mode (mode, 0);
12094 dw_loc_descr_ref cvt;
12095 if (type_die == NULL)
12096 return NULL;
12097 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12098 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12099 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12100 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12101 add_loc_descr (&op0, cvt);
12102 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12103 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12104 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12105 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12106 add_loc_descr (&op1, cvt);
12109 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12110 op = DW_OP_lt;
12111 else
12112 op = DW_OP_gt;
12113 ret = op0;
12114 add_loc_descr (&ret, op1);
12115 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12116 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12117 add_loc_descr (&ret, bra_node);
12118 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12119 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12120 add_loc_descr (&ret, drop_node);
12121 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12122 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12123 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12124 && GET_MODE_CLASS (mode) == MODE_INT
12125 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12126 ret = convert_descriptor_to_mode (mode, ret);
12127 return ret;
12130 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12131 but after converting arguments to type_die, afterwards
12132 convert back to unsigned. */
12134 static dw_loc_descr_ref
12135 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12136 machine_mode mode, machine_mode mem_mode)
12138 dw_loc_descr_ref cvt, op0, op1;
12140 if (type_die == NULL)
12141 return NULL;
12142 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12143 VAR_INIT_STATUS_INITIALIZED);
12144 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12145 VAR_INIT_STATUS_INITIALIZED);
12146 if (op0 == NULL || op1 == NULL)
12147 return NULL;
12148 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12149 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12150 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12151 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12152 add_loc_descr (&op0, cvt);
12153 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12154 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12155 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12156 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12157 add_loc_descr (&op1, cvt);
12158 add_loc_descr (&op0, op1);
12159 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12160 return convert_descriptor_to_mode (mode, op0);
12163 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12164 const0 is DW_OP_lit0 or corresponding typed constant,
12165 const1 is DW_OP_lit1 or corresponding typed constant
12166 and constMSB is constant with just the MSB bit set
12167 for the mode):
12168 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12169 L1: const0 DW_OP_swap
12170 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12171 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12172 L3: DW_OP_drop
12173 L4: DW_OP_nop
12175 CTZ is similar:
12176 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12177 L1: const0 DW_OP_swap
12178 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12179 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12180 L3: DW_OP_drop
12181 L4: DW_OP_nop
12183 FFS is similar:
12184 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12185 L1: const1 DW_OP_swap
12186 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12187 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12188 L3: DW_OP_drop
12189 L4: DW_OP_nop */
12191 static dw_loc_descr_ref
12192 clz_loc_descriptor (rtx rtl, machine_mode mode,
12193 machine_mode mem_mode)
12195 dw_loc_descr_ref op0, ret, tmp;
12196 HOST_WIDE_INT valv;
12197 dw_loc_descr_ref l1jump, l1label;
12198 dw_loc_descr_ref l2jump, l2label;
12199 dw_loc_descr_ref l3jump, l3label;
12200 dw_loc_descr_ref l4jump, l4label;
12201 rtx msb;
12203 if (GET_MODE_CLASS (mode) != MODE_INT
12204 || GET_MODE (XEXP (rtl, 0)) != mode)
12205 return NULL;
12207 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12208 VAR_INIT_STATUS_INITIALIZED);
12209 if (op0 == NULL)
12210 return NULL;
12211 ret = op0;
12212 if (GET_CODE (rtl) == CLZ)
12214 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12215 valv = GET_MODE_BITSIZE (mode);
12217 else if (GET_CODE (rtl) == FFS)
12218 valv = 0;
12219 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12220 valv = GET_MODE_BITSIZE (mode);
12221 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12222 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12223 add_loc_descr (&ret, l1jump);
12224 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12225 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12226 VAR_INIT_STATUS_INITIALIZED);
12227 if (tmp == NULL)
12228 return NULL;
12229 add_loc_descr (&ret, tmp);
12230 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12231 add_loc_descr (&ret, l4jump);
12232 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12233 ? const1_rtx : const0_rtx,
12234 mode, mem_mode,
12235 VAR_INIT_STATUS_INITIALIZED);
12236 if (l1label == NULL)
12237 return NULL;
12238 add_loc_descr (&ret, l1label);
12239 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12240 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12241 add_loc_descr (&ret, l2label);
12242 if (GET_CODE (rtl) != CLZ)
12243 msb = const1_rtx;
12244 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12245 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12246 << (GET_MODE_BITSIZE (mode) - 1));
12247 else
12248 msb = immed_wide_int_const
12249 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12250 GET_MODE_PRECISION (mode)), mode);
12251 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12252 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12253 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12254 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12255 else
12256 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12257 VAR_INIT_STATUS_INITIALIZED);
12258 if (tmp == NULL)
12259 return NULL;
12260 add_loc_descr (&ret, tmp);
12261 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12262 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12263 add_loc_descr (&ret, l3jump);
12264 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12265 VAR_INIT_STATUS_INITIALIZED);
12266 if (tmp == NULL)
12267 return NULL;
12268 add_loc_descr (&ret, tmp);
12269 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12270 ? DW_OP_shl : DW_OP_shr, 0, 0));
12271 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12272 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12273 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12274 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12275 add_loc_descr (&ret, l2jump);
12276 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12277 add_loc_descr (&ret, l3label);
12278 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12279 add_loc_descr (&ret, l4label);
12280 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12281 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12282 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12283 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12284 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12285 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12286 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12287 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12288 return ret;
12291 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12292 const1 is DW_OP_lit1 or corresponding typed constant):
12293 const0 DW_OP_swap
12294 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12295 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12296 L2: DW_OP_drop
12298 PARITY is similar:
12299 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12300 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12301 L2: DW_OP_drop */
12303 static dw_loc_descr_ref
12304 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12305 machine_mode mem_mode)
12307 dw_loc_descr_ref op0, ret, tmp;
12308 dw_loc_descr_ref l1jump, l1label;
12309 dw_loc_descr_ref l2jump, l2label;
12311 if (GET_MODE_CLASS (mode) != MODE_INT
12312 || GET_MODE (XEXP (rtl, 0)) != mode)
12313 return NULL;
12315 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12316 VAR_INIT_STATUS_INITIALIZED);
12317 if (op0 == NULL)
12318 return NULL;
12319 ret = op0;
12320 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12321 VAR_INIT_STATUS_INITIALIZED);
12322 if (tmp == NULL)
12323 return NULL;
12324 add_loc_descr (&ret, tmp);
12325 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12326 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12327 add_loc_descr (&ret, l1label);
12328 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12329 add_loc_descr (&ret, l2jump);
12330 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12331 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12332 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12333 VAR_INIT_STATUS_INITIALIZED);
12334 if (tmp == NULL)
12335 return NULL;
12336 add_loc_descr (&ret, tmp);
12337 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12338 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12339 ? DW_OP_plus : DW_OP_xor, 0, 0));
12340 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12341 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12342 VAR_INIT_STATUS_INITIALIZED);
12343 add_loc_descr (&ret, tmp);
12344 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12345 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12346 add_loc_descr (&ret, l1jump);
12347 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12348 add_loc_descr (&ret, l2label);
12349 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12350 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12351 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12352 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12353 return ret;
12356 /* BSWAP (constS is initial shift count, either 56 or 24):
12357 constS const0
12358 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12359 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12360 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12361 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12362 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12364 static dw_loc_descr_ref
12365 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12366 machine_mode mem_mode)
12368 dw_loc_descr_ref op0, ret, tmp;
12369 dw_loc_descr_ref l1jump, l1label;
12370 dw_loc_descr_ref l2jump, l2label;
12372 if (GET_MODE_CLASS (mode) != MODE_INT
12373 || BITS_PER_UNIT != 8
12374 || (GET_MODE_BITSIZE (mode) != 32
12375 && GET_MODE_BITSIZE (mode) != 64))
12376 return NULL;
12378 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12379 VAR_INIT_STATUS_INITIALIZED);
12380 if (op0 == NULL)
12381 return NULL;
12383 ret = op0;
12384 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12385 mode, mem_mode,
12386 VAR_INIT_STATUS_INITIALIZED);
12387 if (tmp == NULL)
12388 return NULL;
12389 add_loc_descr (&ret, tmp);
12390 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12391 VAR_INIT_STATUS_INITIALIZED);
12392 if (tmp == NULL)
12393 return NULL;
12394 add_loc_descr (&ret, tmp);
12395 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12396 add_loc_descr (&ret, l1label);
12397 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12398 mode, mem_mode,
12399 VAR_INIT_STATUS_INITIALIZED);
12400 add_loc_descr (&ret, tmp);
12401 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12402 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12403 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12404 tmp = mem_loc_descriptor (GEN_INT (255), 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_and, 0, 0));
12410 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12411 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12412 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12413 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12414 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12415 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12416 VAR_INIT_STATUS_INITIALIZED);
12417 add_loc_descr (&ret, tmp);
12418 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12419 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12420 add_loc_descr (&ret, l2jump);
12421 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12422 VAR_INIT_STATUS_INITIALIZED);
12423 add_loc_descr (&ret, tmp);
12424 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12425 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12426 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12427 add_loc_descr (&ret, l1jump);
12428 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12429 add_loc_descr (&ret, l2label);
12430 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12431 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12432 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12433 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12434 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12435 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12436 return ret;
12439 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12440 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12441 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12442 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12444 ROTATERT is similar:
12445 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12446 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12447 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12449 static dw_loc_descr_ref
12450 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12451 machine_mode mem_mode)
12453 rtx rtlop1 = XEXP (rtl, 1);
12454 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12455 int i;
12457 if (GET_MODE_CLASS (mode) != MODE_INT)
12458 return NULL;
12460 if (GET_MODE (rtlop1) != VOIDmode
12461 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12462 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12463 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12464 VAR_INIT_STATUS_INITIALIZED);
12465 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12466 VAR_INIT_STATUS_INITIALIZED);
12467 if (op0 == NULL || op1 == NULL)
12468 return NULL;
12469 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12470 for (i = 0; i < 2; i++)
12472 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12473 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12474 mode, mem_mode,
12475 VAR_INIT_STATUS_INITIALIZED);
12476 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12477 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12478 ? DW_OP_const4u
12479 : HOST_BITS_PER_WIDE_INT == 64
12480 ? DW_OP_const8u : DW_OP_constu,
12481 GET_MODE_MASK (mode), 0);
12482 else
12483 mask[i] = NULL;
12484 if (mask[i] == NULL)
12485 return NULL;
12486 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12488 ret = op0;
12489 add_loc_descr (&ret, op1);
12490 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12491 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12492 if (GET_CODE (rtl) == ROTATERT)
12494 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12495 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12496 GET_MODE_BITSIZE (mode), 0));
12498 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12499 if (mask[0] != NULL)
12500 add_loc_descr (&ret, mask[0]);
12501 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12502 if (mask[1] != NULL)
12504 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12505 add_loc_descr (&ret, mask[1]);
12506 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12508 if (GET_CODE (rtl) == ROTATE)
12510 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12511 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12512 GET_MODE_BITSIZE (mode), 0));
12514 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12515 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12516 return ret;
12519 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12520 for DEBUG_PARAMETER_REF RTL. */
12522 static dw_loc_descr_ref
12523 parameter_ref_descriptor (rtx rtl)
12525 dw_loc_descr_ref ret;
12526 dw_die_ref ref;
12528 if (dwarf_strict)
12529 return NULL;
12530 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12531 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12532 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12533 if (ref)
12535 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12536 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12537 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12539 else
12541 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12542 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12544 return ret;
12547 /* The following routine converts the RTL for a variable or parameter
12548 (resident in memory) into an equivalent Dwarf representation of a
12549 mechanism for getting the address of that same variable onto the top of a
12550 hypothetical "address evaluation" stack.
12552 When creating memory location descriptors, we are effectively transforming
12553 the RTL for a memory-resident object into its Dwarf postfix expression
12554 equivalent. This routine recursively descends an RTL tree, turning
12555 it into Dwarf postfix code as it goes.
12557 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12559 MEM_MODE is the mode of the memory reference, needed to handle some
12560 autoincrement addressing modes.
12562 Return 0 if we can't represent the location. */
12564 dw_loc_descr_ref
12565 mem_loc_descriptor (rtx rtl, machine_mode mode,
12566 machine_mode mem_mode,
12567 enum var_init_status initialized)
12569 dw_loc_descr_ref mem_loc_result = NULL;
12570 enum dwarf_location_atom op;
12571 dw_loc_descr_ref op0, op1;
12572 rtx inner = NULL_RTX;
12574 if (mode == VOIDmode)
12575 mode = GET_MODE (rtl);
12577 /* Note that for a dynamically sized array, the location we will generate a
12578 description of here will be the lowest numbered location which is
12579 actually within the array. That's *not* necessarily the same as the
12580 zeroth element of the array. */
12582 rtl = targetm.delegitimize_address (rtl);
12584 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12585 return NULL;
12587 switch (GET_CODE (rtl))
12589 case POST_INC:
12590 case POST_DEC:
12591 case POST_MODIFY:
12592 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12594 case SUBREG:
12595 /* The case of a subreg may arise when we have a local (register)
12596 variable or a formal (register) parameter which doesn't quite fill
12597 up an entire register. For now, just assume that it is
12598 legitimate to make the Dwarf info refer to the whole register which
12599 contains the given subreg. */
12600 if (!subreg_lowpart_p (rtl))
12601 break;
12602 inner = SUBREG_REG (rtl);
12603 case TRUNCATE:
12604 if (inner == NULL_RTX)
12605 inner = XEXP (rtl, 0);
12606 if (GET_MODE_CLASS (mode) == MODE_INT
12607 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12608 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12609 #ifdef POINTERS_EXTEND_UNSIGNED
12610 || (mode == Pmode && mem_mode != VOIDmode)
12611 #endif
12613 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12615 mem_loc_result = mem_loc_descriptor (inner,
12616 GET_MODE (inner),
12617 mem_mode, initialized);
12618 break;
12620 if (dwarf_strict)
12621 break;
12622 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12623 break;
12624 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12625 && (GET_MODE_CLASS (mode) != MODE_INT
12626 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12627 break;
12628 else
12630 dw_die_ref type_die;
12631 dw_loc_descr_ref cvt;
12633 mem_loc_result = mem_loc_descriptor (inner,
12634 GET_MODE (inner),
12635 mem_mode, initialized);
12636 if (mem_loc_result == NULL)
12637 break;
12638 type_die = base_type_for_mode (mode,
12639 GET_MODE_CLASS (mode) == MODE_INT);
12640 if (type_die == NULL)
12642 mem_loc_result = NULL;
12643 break;
12645 if (GET_MODE_SIZE (mode)
12646 != GET_MODE_SIZE (GET_MODE (inner)))
12647 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12648 else
12649 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12650 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12651 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12652 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12653 add_loc_descr (&mem_loc_result, cvt);
12655 break;
12657 case REG:
12658 if (GET_MODE_CLASS (mode) != MODE_INT
12659 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12660 && rtl != arg_pointer_rtx
12661 && rtl != frame_pointer_rtx
12662 #ifdef POINTERS_EXTEND_UNSIGNED
12663 && (mode != Pmode || mem_mode == VOIDmode)
12664 #endif
12667 dw_die_ref type_die;
12668 unsigned int dbx_regnum;
12670 if (dwarf_strict)
12671 break;
12672 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12673 break;
12674 type_die = base_type_for_mode (mode,
12675 GET_MODE_CLASS (mode) == MODE_INT);
12676 if (type_die == NULL)
12677 break;
12679 dbx_regnum = dbx_reg_number (rtl);
12680 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12681 break;
12682 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12683 dbx_regnum, 0);
12684 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12685 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12686 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12687 break;
12689 /* Whenever a register number forms a part of the description of the
12690 method for calculating the (dynamic) address of a memory resident
12691 object, DWARF rules require the register number be referred to as
12692 a "base register". This distinction is not based in any way upon
12693 what category of register the hardware believes the given register
12694 belongs to. This is strictly DWARF terminology we're dealing with
12695 here. Note that in cases where the location of a memory-resident
12696 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12697 OP_CONST (0)) the actual DWARF location descriptor that we generate
12698 may just be OP_BASEREG (basereg). This may look deceptively like
12699 the object in question was allocated to a register (rather than in
12700 memory) so DWARF consumers need to be aware of the subtle
12701 distinction between OP_REG and OP_BASEREG. */
12702 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12703 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12704 else if (stack_realign_drap
12705 && crtl->drap_reg
12706 && crtl->args.internal_arg_pointer == rtl
12707 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12709 /* If RTL is internal_arg_pointer, which has been optimized
12710 out, use DRAP instead. */
12711 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12712 VAR_INIT_STATUS_INITIALIZED);
12714 break;
12716 case SIGN_EXTEND:
12717 case ZERO_EXTEND:
12718 if (GET_MODE_CLASS (mode) != MODE_INT)
12719 break;
12720 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12721 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12722 if (op0 == 0)
12723 break;
12724 else if (GET_CODE (rtl) == ZERO_EXTEND
12725 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12726 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12727 < HOST_BITS_PER_WIDE_INT
12728 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12729 to expand zero extend as two shifts instead of
12730 masking. */
12731 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12733 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12734 mem_loc_result = op0;
12735 add_loc_descr (&mem_loc_result,
12736 int_loc_descriptor (GET_MODE_MASK (imode)));
12737 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12739 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12741 int shift = DWARF2_ADDR_SIZE
12742 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12743 shift *= BITS_PER_UNIT;
12744 if (GET_CODE (rtl) == SIGN_EXTEND)
12745 op = DW_OP_shra;
12746 else
12747 op = DW_OP_shr;
12748 mem_loc_result = op0;
12749 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12750 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12751 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12752 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12754 else if (!dwarf_strict)
12756 dw_die_ref type_die1, type_die2;
12757 dw_loc_descr_ref cvt;
12759 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12760 GET_CODE (rtl) == ZERO_EXTEND);
12761 if (type_die1 == NULL)
12762 break;
12763 type_die2 = base_type_for_mode (mode, 1);
12764 if (type_die2 == NULL)
12765 break;
12766 mem_loc_result = op0;
12767 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12768 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12769 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12770 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12771 add_loc_descr (&mem_loc_result, cvt);
12772 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12773 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12774 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12775 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12776 add_loc_descr (&mem_loc_result, cvt);
12778 break;
12780 case MEM:
12782 rtx new_rtl = avoid_constant_pool_reference (rtl);
12783 if (new_rtl != rtl)
12785 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12786 initialized);
12787 if (mem_loc_result != NULL)
12788 return mem_loc_result;
12791 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12792 get_address_mode (rtl), mode,
12793 VAR_INIT_STATUS_INITIALIZED);
12794 if (mem_loc_result == NULL)
12795 mem_loc_result = tls_mem_loc_descriptor (rtl);
12796 if (mem_loc_result != NULL)
12798 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12799 || GET_MODE_CLASS (mode) != MODE_INT)
12801 dw_die_ref type_die;
12802 dw_loc_descr_ref deref;
12804 if (dwarf_strict)
12805 return NULL;
12806 type_die
12807 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12808 if (type_die == NULL)
12809 return NULL;
12810 deref = new_loc_descr (DW_OP_GNU_deref_type,
12811 GET_MODE_SIZE (mode), 0);
12812 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12813 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12814 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12815 add_loc_descr (&mem_loc_result, deref);
12817 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12818 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12819 else
12820 add_loc_descr (&mem_loc_result,
12821 new_loc_descr (DW_OP_deref_size,
12822 GET_MODE_SIZE (mode), 0));
12824 break;
12826 case LO_SUM:
12827 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12829 case LABEL_REF:
12830 /* Some ports can transform a symbol ref into a label ref, because
12831 the symbol ref is too far away and has to be dumped into a constant
12832 pool. */
12833 case CONST:
12834 case SYMBOL_REF:
12835 if ((GET_MODE_CLASS (mode) != MODE_INT
12836 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12837 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12838 #ifdef POINTERS_EXTEND_UNSIGNED
12839 && (mode != Pmode || mem_mode == VOIDmode)
12840 #endif
12842 break;
12843 if (GET_CODE (rtl) == SYMBOL_REF
12844 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12846 dw_loc_descr_ref temp;
12848 /* If this is not defined, we have no way to emit the data. */
12849 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12850 break;
12852 temp = new_addr_loc_descr (rtl, dtprel_true);
12854 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12855 add_loc_descr (&mem_loc_result, temp);
12857 break;
12860 if (!const_ok_for_output (rtl))
12862 if (GET_CODE (rtl) == CONST)
12863 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12864 initialized);
12865 break;
12868 symref:
12869 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12870 vec_safe_push (used_rtx_array, rtl);
12871 break;
12873 case CONCAT:
12874 case CONCATN:
12875 case VAR_LOCATION:
12876 case DEBUG_IMPLICIT_PTR:
12877 expansion_failed (NULL_TREE, rtl,
12878 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12879 return 0;
12881 case ENTRY_VALUE:
12882 if (dwarf_strict)
12883 return NULL;
12884 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12886 if (GET_MODE_CLASS (mode) != MODE_INT
12887 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12888 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12889 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12890 else
12892 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12893 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12894 return NULL;
12895 op0 = one_reg_loc_descriptor (dbx_regnum,
12896 VAR_INIT_STATUS_INITIALIZED);
12899 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12900 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12902 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12903 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12904 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12905 return NULL;
12907 else
12908 gcc_unreachable ();
12909 if (op0 == NULL)
12910 return NULL;
12911 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12912 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12913 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12914 break;
12916 case DEBUG_PARAMETER_REF:
12917 mem_loc_result = parameter_ref_descriptor (rtl);
12918 break;
12920 case PRE_MODIFY:
12921 /* Extract the PLUS expression nested inside and fall into
12922 PLUS code below. */
12923 rtl = XEXP (rtl, 1);
12924 goto plus;
12926 case PRE_INC:
12927 case PRE_DEC:
12928 /* Turn these into a PLUS expression and fall into the PLUS code
12929 below. */
12930 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12931 gen_int_mode (GET_CODE (rtl) == PRE_INC
12932 ? GET_MODE_UNIT_SIZE (mem_mode)
12933 : -GET_MODE_UNIT_SIZE (mem_mode),
12934 mode));
12936 /* ... fall through ... */
12938 case PLUS:
12939 plus:
12940 if (is_based_loc (rtl)
12941 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12942 || XEXP (rtl, 0) == arg_pointer_rtx
12943 || XEXP (rtl, 0) == frame_pointer_rtx)
12944 && GET_MODE_CLASS (mode) == MODE_INT)
12945 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12946 INTVAL (XEXP (rtl, 1)),
12947 VAR_INIT_STATUS_INITIALIZED);
12948 else
12950 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12951 VAR_INIT_STATUS_INITIALIZED);
12952 if (mem_loc_result == 0)
12953 break;
12955 if (CONST_INT_P (XEXP (rtl, 1))
12956 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12957 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12958 else
12960 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12961 VAR_INIT_STATUS_INITIALIZED);
12962 if (op1 == 0)
12963 return NULL;
12964 add_loc_descr (&mem_loc_result, op1);
12965 add_loc_descr (&mem_loc_result,
12966 new_loc_descr (DW_OP_plus, 0, 0));
12969 break;
12971 /* If a pseudo-reg is optimized away, it is possible for it to
12972 be replaced with a MEM containing a multiply or shift. */
12973 case MINUS:
12974 op = DW_OP_minus;
12975 goto do_binop;
12977 case MULT:
12978 op = DW_OP_mul;
12979 goto do_binop;
12981 case DIV:
12982 if (!dwarf_strict
12983 && GET_MODE_CLASS (mode) == MODE_INT
12984 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12986 mem_loc_result = typed_binop (DW_OP_div, rtl,
12987 base_type_for_mode (mode, 0),
12988 mode, mem_mode);
12989 break;
12991 op = DW_OP_div;
12992 goto do_binop;
12994 case UMOD:
12995 op = DW_OP_mod;
12996 goto do_binop;
12998 case ASHIFT:
12999 op = DW_OP_shl;
13000 goto do_shift;
13002 case ASHIFTRT:
13003 op = DW_OP_shra;
13004 goto do_shift;
13006 case LSHIFTRT:
13007 op = DW_OP_shr;
13008 goto do_shift;
13010 do_shift:
13011 if (GET_MODE_CLASS (mode) != MODE_INT)
13012 break;
13013 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13014 VAR_INIT_STATUS_INITIALIZED);
13016 rtx rtlop1 = XEXP (rtl, 1);
13017 if (GET_MODE (rtlop1) != VOIDmode
13018 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13019 < GET_MODE_BITSIZE (mode))
13020 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13021 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13022 VAR_INIT_STATUS_INITIALIZED);
13025 if (op0 == 0 || op1 == 0)
13026 break;
13028 mem_loc_result = op0;
13029 add_loc_descr (&mem_loc_result, op1);
13030 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13031 break;
13033 case AND:
13034 op = DW_OP_and;
13035 goto do_binop;
13037 case IOR:
13038 op = DW_OP_or;
13039 goto do_binop;
13041 case XOR:
13042 op = DW_OP_xor;
13043 goto do_binop;
13045 do_binop:
13046 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13047 VAR_INIT_STATUS_INITIALIZED);
13048 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13049 VAR_INIT_STATUS_INITIALIZED);
13051 if (op0 == 0 || op1 == 0)
13052 break;
13054 mem_loc_result = op0;
13055 add_loc_descr (&mem_loc_result, op1);
13056 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13057 break;
13059 case MOD:
13060 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13062 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13063 base_type_for_mode (mode, 0),
13064 mode, mem_mode);
13065 break;
13068 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13069 VAR_INIT_STATUS_INITIALIZED);
13070 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13071 VAR_INIT_STATUS_INITIALIZED);
13073 if (op0 == 0 || op1 == 0)
13074 break;
13076 mem_loc_result = op0;
13077 add_loc_descr (&mem_loc_result, op1);
13078 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13079 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13080 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13081 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13082 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13083 break;
13085 case UDIV:
13086 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13088 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13090 op = DW_OP_div;
13091 goto do_binop;
13093 mem_loc_result = typed_binop (DW_OP_div, rtl,
13094 base_type_for_mode (mode, 1),
13095 mode, mem_mode);
13097 break;
13099 case NOT:
13100 op = DW_OP_not;
13101 goto do_unop;
13103 case ABS:
13104 op = DW_OP_abs;
13105 goto do_unop;
13107 case NEG:
13108 op = DW_OP_neg;
13109 goto do_unop;
13111 do_unop:
13112 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13113 VAR_INIT_STATUS_INITIALIZED);
13115 if (op0 == 0)
13116 break;
13118 mem_loc_result = op0;
13119 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13120 break;
13122 case CONST_INT:
13123 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13124 #ifdef POINTERS_EXTEND_UNSIGNED
13125 || (mode == Pmode
13126 && mem_mode != VOIDmode
13127 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13128 #endif
13131 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13132 break;
13134 if (!dwarf_strict
13135 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13136 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13138 dw_die_ref type_die = base_type_for_mode (mode, 1);
13139 machine_mode amode;
13140 if (type_die == NULL)
13141 return NULL;
13142 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13143 MODE_INT, 0);
13144 if (INTVAL (rtl) >= 0
13145 && amode != BLKmode
13146 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13147 /* const DW_OP_GNU_convert <XXX> vs.
13148 DW_OP_GNU_const_type <XXX, 1, const>. */
13149 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13150 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13152 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13153 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13154 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13155 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13156 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13157 add_loc_descr (&mem_loc_result, op0);
13158 return mem_loc_result;
13160 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13161 INTVAL (rtl));
13162 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13163 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13164 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13165 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13166 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13167 else
13169 mem_loc_result->dw_loc_oprnd2.val_class
13170 = dw_val_class_const_double;
13171 mem_loc_result->dw_loc_oprnd2.v.val_double
13172 = double_int::from_shwi (INTVAL (rtl));
13175 break;
13177 case CONST_DOUBLE:
13178 if (!dwarf_strict)
13180 dw_die_ref type_die;
13182 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13183 CONST_DOUBLE rtx could represent either a large integer
13184 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13185 the value is always a floating point constant.
13187 When it is an integer, a CONST_DOUBLE is used whenever
13188 the constant requires 2 HWIs to be adequately represented.
13189 We output CONST_DOUBLEs as blocks. */
13190 if (mode == VOIDmode
13191 || (GET_MODE (rtl) == VOIDmode
13192 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13193 break;
13194 type_die = base_type_for_mode (mode,
13195 GET_MODE_CLASS (mode) == MODE_INT);
13196 if (type_die == NULL)
13197 return NULL;
13198 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13199 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13200 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13201 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13202 #if TARGET_SUPPORTS_WIDE_INT == 0
13203 if (!SCALAR_FLOAT_MODE_P (mode))
13205 mem_loc_result->dw_loc_oprnd2.val_class
13206 = dw_val_class_const_double;
13207 mem_loc_result->dw_loc_oprnd2.v.val_double
13208 = rtx_to_double_int (rtl);
13210 else
13211 #endif
13213 unsigned int length = GET_MODE_SIZE (mode);
13214 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13216 insert_float (rtl, array);
13217 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13218 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13219 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13220 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13223 break;
13225 case CONST_WIDE_INT:
13226 if (!dwarf_strict)
13228 dw_die_ref type_die;
13230 type_die = base_type_for_mode (mode,
13231 GET_MODE_CLASS (mode) == MODE_INT);
13232 if (type_die == NULL)
13233 return NULL;
13234 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13235 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13236 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13237 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13238 mem_loc_result->dw_loc_oprnd2.val_class
13239 = dw_val_class_wide_int;
13240 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13241 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13243 break;
13245 case EQ:
13246 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13247 break;
13249 case GE:
13250 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13251 break;
13253 case GT:
13254 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13255 break;
13257 case LE:
13258 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13259 break;
13261 case LT:
13262 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13263 break;
13265 case NE:
13266 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13267 break;
13269 case GEU:
13270 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13271 break;
13273 case GTU:
13274 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13275 break;
13277 case LEU:
13278 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13279 break;
13281 case LTU:
13282 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13283 break;
13285 case UMIN:
13286 case UMAX:
13287 if (GET_MODE_CLASS (mode) != MODE_INT)
13288 break;
13289 /* FALLTHRU */
13290 case SMIN:
13291 case SMAX:
13292 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13293 break;
13295 case ZERO_EXTRACT:
13296 case SIGN_EXTRACT:
13297 if (CONST_INT_P (XEXP (rtl, 1))
13298 && CONST_INT_P (XEXP (rtl, 2))
13299 && ((unsigned) INTVAL (XEXP (rtl, 1))
13300 + (unsigned) INTVAL (XEXP (rtl, 2))
13301 <= GET_MODE_BITSIZE (mode))
13302 && GET_MODE_CLASS (mode) == MODE_INT
13303 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13304 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13306 int shift, size;
13307 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13308 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13309 if (op0 == 0)
13310 break;
13311 if (GET_CODE (rtl) == SIGN_EXTRACT)
13312 op = DW_OP_shra;
13313 else
13314 op = DW_OP_shr;
13315 mem_loc_result = op0;
13316 size = INTVAL (XEXP (rtl, 1));
13317 shift = INTVAL (XEXP (rtl, 2));
13318 if (BITS_BIG_ENDIAN)
13319 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13320 - shift - size;
13321 if (shift + size != (int) DWARF2_ADDR_SIZE)
13323 add_loc_descr (&mem_loc_result,
13324 int_loc_descriptor (DWARF2_ADDR_SIZE
13325 - shift - size));
13326 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13328 if (size != (int) DWARF2_ADDR_SIZE)
13330 add_loc_descr (&mem_loc_result,
13331 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13332 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13335 break;
13337 case IF_THEN_ELSE:
13339 dw_loc_descr_ref op2, bra_node, drop_node;
13340 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13341 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13342 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13343 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13344 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13345 VAR_INIT_STATUS_INITIALIZED);
13346 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13347 VAR_INIT_STATUS_INITIALIZED);
13348 if (op0 == NULL || op1 == NULL || op2 == NULL)
13349 break;
13351 mem_loc_result = op1;
13352 add_loc_descr (&mem_loc_result, op2);
13353 add_loc_descr (&mem_loc_result, op0);
13354 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13355 add_loc_descr (&mem_loc_result, bra_node);
13356 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13357 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13358 add_loc_descr (&mem_loc_result, drop_node);
13359 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13360 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13362 break;
13364 case FLOAT_EXTEND:
13365 case FLOAT_TRUNCATE:
13366 case FLOAT:
13367 case UNSIGNED_FLOAT:
13368 case FIX:
13369 case UNSIGNED_FIX:
13370 if (!dwarf_strict)
13372 dw_die_ref type_die;
13373 dw_loc_descr_ref cvt;
13375 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13376 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13377 if (op0 == NULL)
13378 break;
13379 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13380 && (GET_CODE (rtl) == FLOAT
13381 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13382 <= DWARF2_ADDR_SIZE))
13384 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13385 GET_CODE (rtl) == UNSIGNED_FLOAT);
13386 if (type_die == NULL)
13387 break;
13388 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13389 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13390 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13391 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13392 add_loc_descr (&op0, cvt);
13394 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13395 if (type_die == NULL)
13396 break;
13397 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13398 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13399 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13400 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13401 add_loc_descr (&op0, cvt);
13402 if (GET_MODE_CLASS (mode) == MODE_INT
13403 && (GET_CODE (rtl) == FIX
13404 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13406 op0 = convert_descriptor_to_mode (mode, op0);
13407 if (op0 == NULL)
13408 break;
13410 mem_loc_result = op0;
13412 break;
13414 case CLZ:
13415 case CTZ:
13416 case FFS:
13417 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13418 break;
13420 case POPCOUNT:
13421 case PARITY:
13422 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13423 break;
13425 case BSWAP:
13426 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13427 break;
13429 case ROTATE:
13430 case ROTATERT:
13431 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13432 break;
13434 case COMPARE:
13435 /* In theory, we could implement the above. */
13436 /* DWARF cannot represent the unsigned compare operations
13437 natively. */
13438 case SS_MULT:
13439 case US_MULT:
13440 case SS_DIV:
13441 case US_DIV:
13442 case SS_PLUS:
13443 case US_PLUS:
13444 case SS_MINUS:
13445 case US_MINUS:
13446 case SS_NEG:
13447 case US_NEG:
13448 case SS_ABS:
13449 case SS_ASHIFT:
13450 case US_ASHIFT:
13451 case SS_TRUNCATE:
13452 case US_TRUNCATE:
13453 case UNORDERED:
13454 case ORDERED:
13455 case UNEQ:
13456 case UNGE:
13457 case UNGT:
13458 case UNLE:
13459 case UNLT:
13460 case LTGT:
13461 case FRACT_CONVERT:
13462 case UNSIGNED_FRACT_CONVERT:
13463 case SAT_FRACT:
13464 case UNSIGNED_SAT_FRACT:
13465 case SQRT:
13466 case ASM_OPERANDS:
13467 case VEC_MERGE:
13468 case VEC_SELECT:
13469 case VEC_CONCAT:
13470 case VEC_DUPLICATE:
13471 case UNSPEC:
13472 case HIGH:
13473 case FMA:
13474 case STRICT_LOW_PART:
13475 case CONST_VECTOR:
13476 case CONST_FIXED:
13477 case CLRSB:
13478 case CLOBBER:
13479 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13480 can't express it in the debug info. This can happen e.g. with some
13481 TLS UNSPECs. */
13482 break;
13484 case CONST_STRING:
13485 resolve_one_addr (&rtl);
13486 goto symref;
13488 default:
13489 #ifdef ENABLE_CHECKING
13490 print_rtl (stderr, rtl);
13491 gcc_unreachable ();
13492 #else
13493 break;
13494 #endif
13497 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13498 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13500 return mem_loc_result;
13503 /* Return a descriptor that describes the concatenation of two locations.
13504 This is typically a complex variable. */
13506 static dw_loc_descr_ref
13507 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13509 dw_loc_descr_ref cc_loc_result = NULL;
13510 dw_loc_descr_ref x0_ref
13511 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13512 dw_loc_descr_ref x1_ref
13513 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13515 if (x0_ref == 0 || x1_ref == 0)
13516 return 0;
13518 cc_loc_result = x0_ref;
13519 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13521 add_loc_descr (&cc_loc_result, x1_ref);
13522 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13524 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13525 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13527 return cc_loc_result;
13530 /* Return a descriptor that describes the concatenation of N
13531 locations. */
13533 static dw_loc_descr_ref
13534 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13536 unsigned int i;
13537 dw_loc_descr_ref cc_loc_result = NULL;
13538 unsigned int n = XVECLEN (concatn, 0);
13540 for (i = 0; i < n; ++i)
13542 dw_loc_descr_ref ref;
13543 rtx x = XVECEXP (concatn, 0, i);
13545 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13546 if (ref == NULL)
13547 return NULL;
13549 add_loc_descr (&cc_loc_result, ref);
13550 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13553 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13554 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13556 return cc_loc_result;
13559 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13560 for DEBUG_IMPLICIT_PTR RTL. */
13562 static dw_loc_descr_ref
13563 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13565 dw_loc_descr_ref ret;
13566 dw_die_ref ref;
13568 if (dwarf_strict)
13569 return NULL;
13570 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13571 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13572 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13573 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13574 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13575 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13576 if (ref)
13578 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13579 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13580 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13582 else
13584 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13585 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13587 return ret;
13590 /* Output a proper Dwarf location descriptor for a variable or parameter
13591 which is either allocated in a register or in a memory location. For a
13592 register, we just generate an OP_REG and the register number. For a
13593 memory location we provide a Dwarf postfix expression describing how to
13594 generate the (dynamic) address of the object onto the address stack.
13596 MODE is mode of the decl if this loc_descriptor is going to be used in
13597 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13598 allowed, VOIDmode otherwise.
13600 If we don't know how to describe it, return 0. */
13602 static dw_loc_descr_ref
13603 loc_descriptor (rtx rtl, machine_mode mode,
13604 enum var_init_status initialized)
13606 dw_loc_descr_ref loc_result = NULL;
13608 switch (GET_CODE (rtl))
13610 case SUBREG:
13611 /* The case of a subreg may arise when we have a local (register)
13612 variable or a formal (register) parameter which doesn't quite fill
13613 up an entire register. For now, just assume that it is
13614 legitimate to make the Dwarf info refer to the whole register which
13615 contains the given subreg. */
13616 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13617 loc_result = loc_descriptor (SUBREG_REG (rtl),
13618 GET_MODE (SUBREG_REG (rtl)), initialized);
13619 else
13620 goto do_default;
13621 break;
13623 case REG:
13624 loc_result = reg_loc_descriptor (rtl, initialized);
13625 break;
13627 case MEM:
13628 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13629 GET_MODE (rtl), initialized);
13630 if (loc_result == NULL)
13631 loc_result = tls_mem_loc_descriptor (rtl);
13632 if (loc_result == NULL)
13634 rtx new_rtl = avoid_constant_pool_reference (rtl);
13635 if (new_rtl != rtl)
13636 loc_result = loc_descriptor (new_rtl, mode, initialized);
13638 break;
13640 case CONCAT:
13641 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13642 initialized);
13643 break;
13645 case CONCATN:
13646 loc_result = concatn_loc_descriptor (rtl, initialized);
13647 break;
13649 case VAR_LOCATION:
13650 /* Single part. */
13651 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13653 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13654 if (GET_CODE (loc) == EXPR_LIST)
13655 loc = XEXP (loc, 0);
13656 loc_result = loc_descriptor (loc, mode, initialized);
13657 break;
13660 rtl = XEXP (rtl, 1);
13661 /* FALLTHRU */
13663 case PARALLEL:
13665 rtvec par_elems = XVEC (rtl, 0);
13666 int num_elem = GET_NUM_ELEM (par_elems);
13667 machine_mode mode;
13668 int i;
13670 /* Create the first one, so we have something to add to. */
13671 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13672 VOIDmode, initialized);
13673 if (loc_result == NULL)
13674 return NULL;
13675 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13676 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13677 for (i = 1; i < num_elem; i++)
13679 dw_loc_descr_ref temp;
13681 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13682 VOIDmode, initialized);
13683 if (temp == NULL)
13684 return NULL;
13685 add_loc_descr (&loc_result, temp);
13686 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13687 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13690 break;
13692 case CONST_INT:
13693 if (mode != VOIDmode && mode != BLKmode)
13694 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13695 INTVAL (rtl));
13696 break;
13698 case CONST_DOUBLE:
13699 if (mode == VOIDmode)
13700 mode = GET_MODE (rtl);
13702 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13704 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13706 /* Note that a CONST_DOUBLE rtx could represent either an integer
13707 or a floating-point constant. A CONST_DOUBLE is used whenever
13708 the constant requires more than one word in order to be
13709 adequately represented. We output CONST_DOUBLEs as blocks. */
13710 loc_result = new_loc_descr (DW_OP_implicit_value,
13711 GET_MODE_SIZE (mode), 0);
13712 #if TARGET_SUPPORTS_WIDE_INT == 0
13713 if (!SCALAR_FLOAT_MODE_P (mode))
13715 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13716 loc_result->dw_loc_oprnd2.v.val_double
13717 = rtx_to_double_int (rtl);
13719 else
13720 #endif
13722 unsigned int length = GET_MODE_SIZE (mode);
13723 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13725 insert_float (rtl, array);
13726 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13727 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13728 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13729 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13732 break;
13734 case CONST_WIDE_INT:
13735 if (mode == VOIDmode)
13736 mode = GET_MODE (rtl);
13738 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13740 loc_result = new_loc_descr (DW_OP_implicit_value,
13741 GET_MODE_SIZE (mode), 0);
13742 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13743 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13744 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13746 break;
13748 case CONST_VECTOR:
13749 if (mode == VOIDmode)
13750 mode = GET_MODE (rtl);
13752 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13754 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13755 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13756 unsigned char *array
13757 = ggc_vec_alloc<unsigned char> (length * elt_size);
13758 unsigned int i;
13759 unsigned char *p;
13760 machine_mode imode = GET_MODE_INNER (mode);
13762 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13763 switch (GET_MODE_CLASS (mode))
13765 case MODE_VECTOR_INT:
13766 for (i = 0, p = array; i < length; i++, p += elt_size)
13768 rtx elt = CONST_VECTOR_ELT (rtl, i);
13769 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13771 break;
13773 case MODE_VECTOR_FLOAT:
13774 for (i = 0, p = array; i < length; i++, p += elt_size)
13776 rtx elt = CONST_VECTOR_ELT (rtl, i);
13777 insert_float (elt, p);
13779 break;
13781 default:
13782 gcc_unreachable ();
13785 loc_result = new_loc_descr (DW_OP_implicit_value,
13786 length * elt_size, 0);
13787 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13788 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13789 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13790 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13792 break;
13794 case CONST:
13795 if (mode == VOIDmode
13796 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13797 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13798 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13800 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13801 break;
13803 /* FALLTHROUGH */
13804 case SYMBOL_REF:
13805 if (!const_ok_for_output (rtl))
13806 break;
13807 case LABEL_REF:
13808 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13809 && (dwarf_version >= 4 || !dwarf_strict))
13811 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13812 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13813 vec_safe_push (used_rtx_array, rtl);
13815 break;
13817 case DEBUG_IMPLICIT_PTR:
13818 loc_result = implicit_ptr_descriptor (rtl, 0);
13819 break;
13821 case PLUS:
13822 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13823 && CONST_INT_P (XEXP (rtl, 1)))
13825 loc_result
13826 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13827 break;
13829 /* FALLTHRU */
13830 do_default:
13831 default:
13832 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13833 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13834 && dwarf_version >= 4)
13835 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13837 /* Value expression. */
13838 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13839 if (loc_result)
13840 add_loc_descr (&loc_result,
13841 new_loc_descr (DW_OP_stack_value, 0, 0));
13843 break;
13846 return loc_result;
13849 /* We need to figure out what section we should use as the base for the
13850 address ranges where a given location is valid.
13851 1. If this particular DECL has a section associated with it, use that.
13852 2. If this function has a section associated with it, use that.
13853 3. Otherwise, use the text section.
13854 XXX: If you split a variable across multiple sections, we won't notice. */
13856 static const char *
13857 secname_for_decl (const_tree decl)
13859 const char *secname;
13861 if (VAR_OR_FUNCTION_DECL_P (decl)
13862 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13863 && DECL_SECTION_NAME (decl))
13864 secname = DECL_SECTION_NAME (decl);
13865 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13866 secname = DECL_SECTION_NAME (current_function_decl);
13867 else if (cfun && in_cold_section_p)
13868 secname = crtl->subsections.cold_section_label;
13869 else
13870 secname = text_section_label;
13872 return secname;
13875 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13877 static bool
13878 decl_by_reference_p (tree decl)
13880 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13881 || TREE_CODE (decl) == VAR_DECL)
13882 && DECL_BY_REFERENCE (decl));
13885 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13886 for VARLOC. */
13888 static dw_loc_descr_ref
13889 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13890 enum var_init_status initialized)
13892 int have_address = 0;
13893 dw_loc_descr_ref descr;
13894 machine_mode mode;
13896 if (want_address != 2)
13898 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13899 /* Single part. */
13900 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13902 varloc = PAT_VAR_LOCATION_LOC (varloc);
13903 if (GET_CODE (varloc) == EXPR_LIST)
13904 varloc = XEXP (varloc, 0);
13905 mode = GET_MODE (varloc);
13906 if (MEM_P (varloc))
13908 rtx addr = XEXP (varloc, 0);
13909 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13910 mode, initialized);
13911 if (descr)
13912 have_address = 1;
13913 else
13915 rtx x = avoid_constant_pool_reference (varloc);
13916 if (x != varloc)
13917 descr = mem_loc_descriptor (x, mode, VOIDmode,
13918 initialized);
13921 else
13922 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13924 else
13925 return 0;
13927 else
13929 if (GET_CODE (varloc) == VAR_LOCATION)
13930 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13931 else
13932 mode = DECL_MODE (loc);
13933 descr = loc_descriptor (varloc, mode, initialized);
13934 have_address = 1;
13937 if (!descr)
13938 return 0;
13940 if (want_address == 2 && !have_address
13941 && (dwarf_version >= 4 || !dwarf_strict))
13943 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13945 expansion_failed (loc, NULL_RTX,
13946 "DWARF address size mismatch");
13947 return 0;
13949 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13950 have_address = 1;
13952 /* Show if we can't fill the request for an address. */
13953 if (want_address && !have_address)
13955 expansion_failed (loc, NULL_RTX,
13956 "Want address and only have value");
13957 return 0;
13960 /* If we've got an address and don't want one, dereference. */
13961 if (!want_address && have_address)
13963 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13964 enum dwarf_location_atom op;
13966 if (size > DWARF2_ADDR_SIZE || size == -1)
13968 expansion_failed (loc, NULL_RTX,
13969 "DWARF address size mismatch");
13970 return 0;
13972 else if (size == DWARF2_ADDR_SIZE)
13973 op = DW_OP_deref;
13974 else
13975 op = DW_OP_deref_size;
13977 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13980 return descr;
13983 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13984 if it is not possible. */
13986 static dw_loc_descr_ref
13987 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13989 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13990 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13991 else if (dwarf_version >= 3 || !dwarf_strict)
13992 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13993 else
13994 return NULL;
13997 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13998 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14000 static dw_loc_descr_ref
14001 dw_sra_loc_expr (tree decl, rtx loc)
14003 rtx p;
14004 unsigned HOST_WIDE_INT padsize = 0;
14005 dw_loc_descr_ref descr, *descr_tail;
14006 unsigned HOST_WIDE_INT decl_size;
14007 rtx varloc;
14008 enum var_init_status initialized;
14010 if (DECL_SIZE (decl) == NULL
14011 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14012 return NULL;
14014 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14015 descr = NULL;
14016 descr_tail = &descr;
14018 for (p = loc; p; p = XEXP (p, 1))
14020 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14021 rtx loc_note = *decl_piece_varloc_ptr (p);
14022 dw_loc_descr_ref cur_descr;
14023 dw_loc_descr_ref *tail, last = NULL;
14024 unsigned HOST_WIDE_INT opsize = 0;
14026 if (loc_note == NULL_RTX
14027 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14029 padsize += bitsize;
14030 continue;
14032 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14033 varloc = NOTE_VAR_LOCATION (loc_note);
14034 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14035 if (cur_descr == NULL)
14037 padsize += bitsize;
14038 continue;
14041 /* Check that cur_descr either doesn't use
14042 DW_OP_*piece operations, or their sum is equal
14043 to bitsize. Otherwise we can't embed it. */
14044 for (tail = &cur_descr; *tail != NULL;
14045 tail = &(*tail)->dw_loc_next)
14046 if ((*tail)->dw_loc_opc == DW_OP_piece)
14048 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14049 * BITS_PER_UNIT;
14050 last = *tail;
14052 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14054 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14055 last = *tail;
14058 if (last != NULL && opsize != bitsize)
14060 padsize += bitsize;
14061 /* Discard the current piece of the descriptor and release any
14062 addr_table entries it uses. */
14063 remove_loc_list_addr_table_entries (cur_descr);
14064 continue;
14067 /* If there is a hole, add DW_OP_*piece after empty DWARF
14068 expression, which means that those bits are optimized out. */
14069 if (padsize)
14071 if (padsize > decl_size)
14073 remove_loc_list_addr_table_entries (cur_descr);
14074 goto discard_descr;
14076 decl_size -= padsize;
14077 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14078 if (*descr_tail == NULL)
14080 remove_loc_list_addr_table_entries (cur_descr);
14081 goto discard_descr;
14083 descr_tail = &(*descr_tail)->dw_loc_next;
14084 padsize = 0;
14086 *descr_tail = cur_descr;
14087 descr_tail = tail;
14088 if (bitsize > decl_size)
14089 goto discard_descr;
14090 decl_size -= bitsize;
14091 if (last == NULL)
14093 HOST_WIDE_INT offset = 0;
14094 if (GET_CODE (varloc) == VAR_LOCATION
14095 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14097 varloc = PAT_VAR_LOCATION_LOC (varloc);
14098 if (GET_CODE (varloc) == EXPR_LIST)
14099 varloc = XEXP (varloc, 0);
14103 if (GET_CODE (varloc) == CONST
14104 || GET_CODE (varloc) == SIGN_EXTEND
14105 || GET_CODE (varloc) == ZERO_EXTEND)
14106 varloc = XEXP (varloc, 0);
14107 else if (GET_CODE (varloc) == SUBREG)
14108 varloc = SUBREG_REG (varloc);
14109 else
14110 break;
14112 while (1);
14113 /* DW_OP_bit_size offset should be zero for register
14114 or implicit location descriptions and empty location
14115 descriptions, but for memory addresses needs big endian
14116 adjustment. */
14117 if (MEM_P (varloc))
14119 unsigned HOST_WIDE_INT memsize
14120 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14121 if (memsize != bitsize)
14123 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14124 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14125 goto discard_descr;
14126 if (memsize < bitsize)
14127 goto discard_descr;
14128 if (BITS_BIG_ENDIAN)
14129 offset = memsize - bitsize;
14133 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14134 if (*descr_tail == NULL)
14135 goto discard_descr;
14136 descr_tail = &(*descr_tail)->dw_loc_next;
14140 /* If there were any non-empty expressions, add padding till the end of
14141 the decl. */
14142 if (descr != NULL && decl_size != 0)
14144 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14145 if (*descr_tail == NULL)
14146 goto discard_descr;
14148 return descr;
14150 discard_descr:
14151 /* Discard the descriptor and release any addr_table entries it uses. */
14152 remove_loc_list_addr_table_entries (descr);
14153 return NULL;
14156 /* Return the dwarf representation of the location list LOC_LIST of
14157 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14158 function. */
14160 static dw_loc_list_ref
14161 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14163 const char *endname, *secname;
14164 rtx varloc;
14165 enum var_init_status initialized;
14166 struct var_loc_node *node;
14167 dw_loc_descr_ref descr;
14168 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14169 dw_loc_list_ref list = NULL;
14170 dw_loc_list_ref *listp = &list;
14172 /* Now that we know what section we are using for a base,
14173 actually construct the list of locations.
14174 The first location information is what is passed to the
14175 function that creates the location list, and the remaining
14176 locations just get added on to that list.
14177 Note that we only know the start address for a location
14178 (IE location changes), so to build the range, we use
14179 the range [current location start, next location start].
14180 This means we have to special case the last node, and generate
14181 a range of [last location start, end of function label]. */
14183 secname = secname_for_decl (decl);
14185 for (node = loc_list->first; node; node = node->next)
14186 if (GET_CODE (node->loc) == EXPR_LIST
14187 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14189 if (GET_CODE (node->loc) == EXPR_LIST)
14191 /* This requires DW_OP_{,bit_}piece, which is not usable
14192 inside DWARF expressions. */
14193 if (want_address != 2)
14194 continue;
14195 descr = dw_sra_loc_expr (decl, node->loc);
14196 if (descr == NULL)
14197 continue;
14199 else
14201 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14202 varloc = NOTE_VAR_LOCATION (node->loc);
14203 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14205 if (descr)
14207 bool range_across_switch = false;
14208 /* If section switch happens in between node->label
14209 and node->next->label (or end of function) and
14210 we can't emit it as a single entry list,
14211 emit two ranges, first one ending at the end
14212 of first partition and second one starting at the
14213 beginning of second partition. */
14214 if (node == loc_list->last_before_switch
14215 && (node != loc_list->first || loc_list->first->next)
14216 && current_function_decl)
14218 endname = cfun->fde->dw_fde_end;
14219 range_across_switch = true;
14221 /* The variable has a location between NODE->LABEL and
14222 NODE->NEXT->LABEL. */
14223 else if (node->next)
14224 endname = node->next->label;
14225 /* If the variable has a location at the last label
14226 it keeps its location until the end of function. */
14227 else if (!current_function_decl)
14228 endname = text_end_label;
14229 else
14231 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14232 current_function_funcdef_no);
14233 endname = ggc_strdup (label_id);
14236 *listp = new_loc_list (descr, node->label, endname, secname);
14237 if (TREE_CODE (decl) == PARM_DECL
14238 && node == loc_list->first
14239 && NOTE_P (node->loc)
14240 && strcmp (node->label, endname) == 0)
14241 (*listp)->force = true;
14242 listp = &(*listp)->dw_loc_next;
14244 if (range_across_switch)
14246 if (GET_CODE (node->loc) == EXPR_LIST)
14247 descr = dw_sra_loc_expr (decl, node->loc);
14248 else
14250 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14251 varloc = NOTE_VAR_LOCATION (node->loc);
14252 descr = dw_loc_list_1 (decl, varloc, want_address,
14253 initialized);
14255 gcc_assert (descr);
14256 /* The variable has a location between NODE->LABEL and
14257 NODE->NEXT->LABEL. */
14258 if (node->next)
14259 endname = node->next->label;
14260 else
14261 endname = cfun->fde->dw_fde_second_end;
14262 *listp = new_loc_list (descr,
14263 cfun->fde->dw_fde_second_begin,
14264 endname, secname);
14265 listp = &(*listp)->dw_loc_next;
14270 /* Try to avoid the overhead of a location list emitting a location
14271 expression instead, but only if we didn't have more than one
14272 location entry in the first place. If some entries were not
14273 representable, we don't want to pretend a single entry that was
14274 applies to the entire scope in which the variable is
14275 available. */
14276 if (list && loc_list->first->next)
14277 gen_llsym (list);
14279 return list;
14282 /* Return if the loc_list has only single element and thus can be represented
14283 as location description. */
14285 static bool
14286 single_element_loc_list_p (dw_loc_list_ref list)
14288 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14289 return !list->ll_symbol;
14292 /* To each location in list LIST add loc descr REF. */
14294 static void
14295 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14297 dw_loc_descr_ref copy;
14298 add_loc_descr (&list->expr, ref);
14299 list = list->dw_loc_next;
14300 while (list)
14302 copy = ggc_alloc<dw_loc_descr_node> ();
14303 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14304 add_loc_descr (&list->expr, copy);
14305 while (copy->dw_loc_next)
14307 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14308 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14309 copy->dw_loc_next = new_copy;
14310 copy = new_copy;
14312 list = list->dw_loc_next;
14316 /* Given two lists RET and LIST
14317 produce location list that is result of adding expression in LIST
14318 to expression in RET on each position in program.
14319 Might be destructive on both RET and LIST.
14321 TODO: We handle only simple cases of RET or LIST having at most one
14322 element. General case would inolve sorting the lists in program order
14323 and merging them that will need some additional work.
14324 Adding that will improve quality of debug info especially for SRA-ed
14325 structures. */
14327 static void
14328 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14330 if (!list)
14331 return;
14332 if (!*ret)
14334 *ret = list;
14335 return;
14337 if (!list->dw_loc_next)
14339 add_loc_descr_to_each (*ret, list->expr);
14340 return;
14342 if (!(*ret)->dw_loc_next)
14344 add_loc_descr_to_each (list, (*ret)->expr);
14345 *ret = list;
14346 return;
14348 expansion_failed (NULL_TREE, NULL_RTX,
14349 "Don't know how to merge two non-trivial"
14350 " location lists.\n");
14351 *ret = NULL;
14352 return;
14355 /* LOC is constant expression. Try a luck, look it up in constant
14356 pool and return its loc_descr of its address. */
14358 static dw_loc_descr_ref
14359 cst_pool_loc_descr (tree loc)
14361 /* Get an RTL for this, if something has been emitted. */
14362 rtx rtl = lookup_constant_def (loc);
14364 if (!rtl || !MEM_P (rtl))
14366 gcc_assert (!rtl);
14367 return 0;
14369 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14371 /* TODO: We might get more coverage if we was actually delaying expansion
14372 of all expressions till end of compilation when constant pools are fully
14373 populated. */
14374 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14376 expansion_failed (loc, NULL_RTX,
14377 "CST value in contant pool but not marked.");
14378 return 0;
14380 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14381 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14384 /* Return dw_loc_list representing address of addr_expr LOC
14385 by looking for inner INDIRECT_REF expression and turning
14386 it into simple arithmetics.
14388 See loc_list_from_tree for the meaning of CONTEXT. */
14390 static dw_loc_list_ref
14391 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14392 const loc_descr_context *context)
14394 tree obj, offset;
14395 HOST_WIDE_INT bitsize, bitpos, bytepos;
14396 machine_mode mode;
14397 int unsignedp, volatilep = 0;
14398 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14400 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14401 &bitsize, &bitpos, &offset, &mode,
14402 &unsignedp, &volatilep, false);
14403 STRIP_NOPS (obj);
14404 if (bitpos % BITS_PER_UNIT)
14406 expansion_failed (loc, NULL_RTX, "bitfield access");
14407 return 0;
14409 if (!INDIRECT_REF_P (obj))
14411 expansion_failed (obj,
14412 NULL_RTX, "no indirect ref in inner refrence");
14413 return 0;
14415 if (!offset && !bitpos)
14416 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14417 context);
14418 else if (toplev
14419 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14420 && (dwarf_version >= 4 || !dwarf_strict))
14422 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14423 if (!list_ret)
14424 return 0;
14425 if (offset)
14427 /* Variable offset. */
14428 list_ret1 = loc_list_from_tree (offset, 0, context);
14429 if (list_ret1 == 0)
14430 return 0;
14431 add_loc_list (&list_ret, list_ret1);
14432 if (!list_ret)
14433 return 0;
14434 add_loc_descr_to_each (list_ret,
14435 new_loc_descr (DW_OP_plus, 0, 0));
14437 bytepos = bitpos / BITS_PER_UNIT;
14438 if (bytepos > 0)
14439 add_loc_descr_to_each (list_ret,
14440 new_loc_descr (DW_OP_plus_uconst,
14441 bytepos, 0));
14442 else if (bytepos < 0)
14443 loc_list_plus_const (list_ret, bytepos);
14444 add_loc_descr_to_each (list_ret,
14445 new_loc_descr (DW_OP_stack_value, 0, 0));
14447 return list_ret;
14451 /* Helper structure for location descriptions generation. */
14452 struct loc_descr_context
14454 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14455 NULL_TREE if DW_OP_push_object_address in invalid for this location
14456 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14457 tree context_type;
14458 /* The ..._DECL node that should be translated as a
14459 DW_OP_push_object_address operation. */
14460 tree base_decl;
14463 /* Generate Dwarf location list representing LOC.
14464 If WANT_ADDRESS is false, expression computing LOC will be computed
14465 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14466 if WANT_ADDRESS is 2, expression computing address useable in location
14467 will be returned (i.e. DW_OP_reg can be used
14468 to refer to register values).
14470 CONTEXT provides information to customize the location descriptions
14471 generation. Its context_type field specifies what type is implicitly
14472 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14473 will not be generated.
14475 If CONTEXT is NULL, the behavior is the same as if both context_type and
14476 base_decl fields were NULL_TREE. */
14478 static dw_loc_list_ref
14479 loc_list_from_tree (tree loc, int want_address,
14480 const struct loc_descr_context *context)
14482 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14483 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14484 int have_address = 0;
14485 enum dwarf_location_atom op;
14487 /* ??? Most of the time we do not take proper care for sign/zero
14488 extending the values properly. Hopefully this won't be a real
14489 problem... */
14491 if (context != NULL
14492 && context->base_decl == loc
14493 && want_address == 0)
14495 if (dwarf_version >= 3 || !dwarf_strict)
14496 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14497 NULL, NULL, NULL);
14498 else
14499 return NULL;
14502 switch (TREE_CODE (loc))
14504 case ERROR_MARK:
14505 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14506 return 0;
14508 case PLACEHOLDER_EXPR:
14509 /* This case involves extracting fields from an object to determine the
14510 position of other fields. It is supposed to appear only as the first
14511 operand of COMPONENT_REF nodes and to reference precisely the type
14512 that the context allows. */
14513 if (context != NULL
14514 && TREE_TYPE (loc) == context->context_type
14515 && want_address >= 1)
14517 if (dwarf_version >= 3 || !dwarf_strict)
14519 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14520 have_address = 1;
14521 break;
14523 else
14524 return NULL;
14526 else
14527 expansion_failed (loc, NULL_RTX,
14528 "PLACEHOLDER_EXPR for an unexpected type");
14529 break;
14531 case CALL_EXPR:
14532 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14533 /* There are no opcodes for these operations. */
14534 return 0;
14536 case PREINCREMENT_EXPR:
14537 case PREDECREMENT_EXPR:
14538 case POSTINCREMENT_EXPR:
14539 case POSTDECREMENT_EXPR:
14540 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14541 /* There are no opcodes for these operations. */
14542 return 0;
14544 case ADDR_EXPR:
14545 /* If we already want an address, see if there is INDIRECT_REF inside
14546 e.g. for &this->field. */
14547 if (want_address)
14549 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14550 (loc, want_address == 2, context);
14551 if (list_ret)
14552 have_address = 1;
14553 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14554 && (ret = cst_pool_loc_descr (loc)))
14555 have_address = 1;
14557 /* Otherwise, process the argument and look for the address. */
14558 if (!list_ret && !ret)
14559 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14560 else
14562 if (want_address)
14563 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14564 return NULL;
14566 break;
14568 case VAR_DECL:
14569 if (DECL_THREAD_LOCAL_P (loc))
14571 rtx rtl;
14572 enum dwarf_location_atom tls_op;
14573 enum dtprel_bool dtprel = dtprel_false;
14575 if (targetm.have_tls)
14577 /* If this is not defined, we have no way to emit the
14578 data. */
14579 if (!targetm.asm_out.output_dwarf_dtprel)
14580 return 0;
14582 /* The way DW_OP_GNU_push_tls_address is specified, we
14583 can only look up addresses of objects in the current
14584 module. We used DW_OP_addr as first op, but that's
14585 wrong, because DW_OP_addr is relocated by the debug
14586 info consumer, while DW_OP_GNU_push_tls_address
14587 operand shouldn't be. */
14588 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14589 return 0;
14590 dtprel = dtprel_true;
14591 tls_op = DW_OP_GNU_push_tls_address;
14593 else
14595 if (!targetm.emutls.debug_form_tls_address
14596 || !(dwarf_version >= 3 || !dwarf_strict))
14597 return 0;
14598 /* We stuffed the control variable into the DECL_VALUE_EXPR
14599 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14600 no longer appear in gimple code. We used the control
14601 variable in specific so that we could pick it up here. */
14602 loc = DECL_VALUE_EXPR (loc);
14603 tls_op = DW_OP_form_tls_address;
14606 rtl = rtl_for_decl_location (loc);
14607 if (rtl == NULL_RTX)
14608 return 0;
14610 if (!MEM_P (rtl))
14611 return 0;
14612 rtl = XEXP (rtl, 0);
14613 if (! CONSTANT_P (rtl))
14614 return 0;
14616 ret = new_addr_loc_descr (rtl, dtprel);
14617 ret1 = new_loc_descr (tls_op, 0, 0);
14618 add_loc_descr (&ret, ret1);
14620 have_address = 1;
14621 break;
14623 /* FALLTHRU */
14625 case PARM_DECL:
14626 case RESULT_DECL:
14627 if (DECL_HAS_VALUE_EXPR_P (loc))
14628 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14629 want_address, context);
14630 /* FALLTHRU */
14632 case FUNCTION_DECL:
14634 rtx rtl;
14635 var_loc_list *loc_list = lookup_decl_loc (loc);
14637 if (loc_list && loc_list->first)
14639 list_ret = dw_loc_list (loc_list, loc, want_address);
14640 have_address = want_address != 0;
14641 break;
14643 rtl = rtl_for_decl_location (loc);
14644 if (rtl == NULL_RTX)
14646 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14647 return 0;
14649 else if (CONST_INT_P (rtl))
14651 HOST_WIDE_INT val = INTVAL (rtl);
14652 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14653 val &= GET_MODE_MASK (DECL_MODE (loc));
14654 ret = int_loc_descriptor (val);
14656 else if (GET_CODE (rtl) == CONST_STRING)
14658 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14659 return 0;
14661 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14662 ret = new_addr_loc_descr (rtl, dtprel_false);
14663 else
14665 machine_mode mode, mem_mode;
14667 /* Certain constructs can only be represented at top-level. */
14668 if (want_address == 2)
14670 ret = loc_descriptor (rtl, VOIDmode,
14671 VAR_INIT_STATUS_INITIALIZED);
14672 have_address = 1;
14674 else
14676 mode = GET_MODE (rtl);
14677 mem_mode = VOIDmode;
14678 if (MEM_P (rtl))
14680 mem_mode = mode;
14681 mode = get_address_mode (rtl);
14682 rtl = XEXP (rtl, 0);
14683 have_address = 1;
14685 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14686 VAR_INIT_STATUS_INITIALIZED);
14688 if (!ret)
14689 expansion_failed (loc, rtl,
14690 "failed to produce loc descriptor for rtl");
14693 break;
14695 case MEM_REF:
14696 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14698 have_address = 1;
14699 goto do_plus;
14701 /* Fallthru. */
14702 case INDIRECT_REF:
14703 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14704 have_address = 1;
14705 break;
14707 case TARGET_MEM_REF:
14708 case SSA_NAME:
14709 case DEBUG_EXPR_DECL:
14710 return NULL;
14712 case COMPOUND_EXPR:
14713 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14715 CASE_CONVERT:
14716 case VIEW_CONVERT_EXPR:
14717 case SAVE_EXPR:
14718 case MODIFY_EXPR:
14719 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14721 case COMPONENT_REF:
14722 case BIT_FIELD_REF:
14723 case ARRAY_REF:
14724 case ARRAY_RANGE_REF:
14725 case REALPART_EXPR:
14726 case IMAGPART_EXPR:
14728 tree obj, offset;
14729 HOST_WIDE_INT bitsize, bitpos, bytepos;
14730 machine_mode mode;
14731 int unsignedp, volatilep = 0;
14733 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14734 &unsignedp, &volatilep, false);
14736 gcc_assert (obj != loc);
14738 list_ret = loc_list_from_tree (obj,
14739 want_address == 2
14740 && !bitpos && !offset ? 2 : 1,
14741 context);
14742 /* TODO: We can extract value of the small expression via shifting even
14743 for nonzero bitpos. */
14744 if (list_ret == 0)
14745 return 0;
14746 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14748 expansion_failed (loc, NULL_RTX,
14749 "bitfield access");
14750 return 0;
14753 if (offset != NULL_TREE)
14755 /* Variable offset. */
14756 list_ret1 = loc_list_from_tree (offset, 0, context);
14757 if (list_ret1 == 0)
14758 return 0;
14759 add_loc_list (&list_ret, list_ret1);
14760 if (!list_ret)
14761 return 0;
14762 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14765 bytepos = bitpos / BITS_PER_UNIT;
14766 if (bytepos > 0)
14767 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14768 else if (bytepos < 0)
14769 loc_list_plus_const (list_ret, bytepos);
14771 have_address = 1;
14772 break;
14775 case INTEGER_CST:
14776 if ((want_address || !tree_fits_shwi_p (loc))
14777 && (ret = cst_pool_loc_descr (loc)))
14778 have_address = 1;
14779 else if (want_address == 2
14780 && tree_fits_shwi_p (loc)
14781 && (ret = address_of_int_loc_descriptor
14782 (int_size_in_bytes (TREE_TYPE (loc)),
14783 tree_to_shwi (loc))))
14784 have_address = 1;
14785 else if (tree_fits_shwi_p (loc))
14786 ret = int_loc_descriptor (tree_to_shwi (loc));
14787 else
14789 expansion_failed (loc, NULL_RTX,
14790 "Integer operand is not host integer");
14791 return 0;
14793 break;
14795 case CONSTRUCTOR:
14796 case REAL_CST:
14797 case STRING_CST:
14798 case COMPLEX_CST:
14799 if ((ret = cst_pool_loc_descr (loc)))
14800 have_address = 1;
14801 else
14802 /* We can construct small constants here using int_loc_descriptor. */
14803 expansion_failed (loc, NULL_RTX,
14804 "constructor or constant not in constant pool");
14805 break;
14807 case TRUTH_AND_EXPR:
14808 case TRUTH_ANDIF_EXPR:
14809 case BIT_AND_EXPR:
14810 op = DW_OP_and;
14811 goto do_binop;
14813 case TRUTH_XOR_EXPR:
14814 case BIT_XOR_EXPR:
14815 op = DW_OP_xor;
14816 goto do_binop;
14818 case TRUTH_OR_EXPR:
14819 case TRUTH_ORIF_EXPR:
14820 case BIT_IOR_EXPR:
14821 op = DW_OP_or;
14822 goto do_binop;
14824 case FLOOR_DIV_EXPR:
14825 case CEIL_DIV_EXPR:
14826 case ROUND_DIV_EXPR:
14827 case TRUNC_DIV_EXPR:
14828 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14829 return 0;
14830 op = DW_OP_div;
14831 goto do_binop;
14833 case MINUS_EXPR:
14834 op = DW_OP_minus;
14835 goto do_binop;
14837 case FLOOR_MOD_EXPR:
14838 case CEIL_MOD_EXPR:
14839 case ROUND_MOD_EXPR:
14840 case TRUNC_MOD_EXPR:
14841 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14843 op = DW_OP_mod;
14844 goto do_binop;
14846 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14847 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14848 if (list_ret == 0 || list_ret1 == 0)
14849 return 0;
14851 add_loc_list (&list_ret, list_ret1);
14852 if (list_ret == 0)
14853 return 0;
14854 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14855 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14856 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14857 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14858 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14859 break;
14861 case MULT_EXPR:
14862 op = DW_OP_mul;
14863 goto do_binop;
14865 case LSHIFT_EXPR:
14866 op = DW_OP_shl;
14867 goto do_binop;
14869 case RSHIFT_EXPR:
14870 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14871 goto do_binop;
14873 case POINTER_PLUS_EXPR:
14874 case PLUS_EXPR:
14875 do_plus:
14876 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14878 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14879 if (list_ret == 0)
14880 return 0;
14882 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14883 break;
14886 op = DW_OP_plus;
14887 goto do_binop;
14889 case LE_EXPR:
14890 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14891 return 0;
14893 op = DW_OP_le;
14894 goto do_binop;
14896 case GE_EXPR:
14897 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14898 return 0;
14900 op = DW_OP_ge;
14901 goto do_binop;
14903 case LT_EXPR:
14904 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14905 return 0;
14907 op = DW_OP_lt;
14908 goto do_binop;
14910 case GT_EXPR:
14911 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14912 return 0;
14914 op = DW_OP_gt;
14915 goto do_binop;
14917 case EQ_EXPR:
14918 op = DW_OP_eq;
14919 goto do_binop;
14921 case NE_EXPR:
14922 op = DW_OP_ne;
14923 goto do_binop;
14925 do_binop:
14926 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14927 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14928 if (list_ret == 0 || list_ret1 == 0)
14929 return 0;
14931 add_loc_list (&list_ret, list_ret1);
14932 if (list_ret == 0)
14933 return 0;
14934 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14935 break;
14937 case TRUTH_NOT_EXPR:
14938 case BIT_NOT_EXPR:
14939 op = DW_OP_not;
14940 goto do_unop;
14942 case ABS_EXPR:
14943 op = DW_OP_abs;
14944 goto do_unop;
14946 case NEGATE_EXPR:
14947 op = DW_OP_neg;
14948 goto do_unop;
14950 do_unop:
14951 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14952 if (list_ret == 0)
14953 return 0;
14955 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14956 break;
14958 case MIN_EXPR:
14959 case MAX_EXPR:
14961 const enum tree_code code =
14962 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14964 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14965 build2 (code, integer_type_node,
14966 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14967 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14970 /* ... fall through ... */
14972 case COND_EXPR:
14974 dw_loc_descr_ref lhs
14975 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14976 dw_loc_list_ref rhs
14977 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14978 dw_loc_descr_ref bra_node, jump_node, tmp;
14980 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14981 if (list_ret == 0 || lhs == 0 || rhs == 0)
14982 return 0;
14984 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14985 add_loc_descr_to_each (list_ret, bra_node);
14987 add_loc_list (&list_ret, rhs);
14988 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14989 add_loc_descr_to_each (list_ret, jump_node);
14991 add_loc_descr_to_each (list_ret, lhs);
14992 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14993 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14995 /* ??? Need a node to point the skip at. Use a nop. */
14996 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14997 add_loc_descr_to_each (list_ret, tmp);
14998 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14999 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15001 break;
15003 case FIX_TRUNC_EXPR:
15004 return 0;
15006 default:
15007 /* Leave front-end specific codes as simply unknown. This comes
15008 up, for instance, with the C STMT_EXPR. */
15009 if ((unsigned int) TREE_CODE (loc)
15010 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15012 expansion_failed (loc, NULL_RTX,
15013 "language specific tree node");
15014 return 0;
15017 #ifdef ENABLE_CHECKING
15018 /* Otherwise this is a generic code; we should just lists all of
15019 these explicitly. We forgot one. */
15020 gcc_unreachable ();
15021 #else
15022 /* In a release build, we want to degrade gracefully: better to
15023 generate incomplete debugging information than to crash. */
15024 return NULL;
15025 #endif
15028 if (!ret && !list_ret)
15029 return 0;
15031 if (want_address == 2 && !have_address
15032 && (dwarf_version >= 4 || !dwarf_strict))
15034 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15036 expansion_failed (loc, NULL_RTX,
15037 "DWARF address size mismatch");
15038 return 0;
15040 if (ret)
15041 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15042 else
15043 add_loc_descr_to_each (list_ret,
15044 new_loc_descr (DW_OP_stack_value, 0, 0));
15045 have_address = 1;
15047 /* Show if we can't fill the request for an address. */
15048 if (want_address && !have_address)
15050 expansion_failed (loc, NULL_RTX,
15051 "Want address and only have value");
15052 return 0;
15055 gcc_assert (!ret || !list_ret);
15057 /* If we've got an address and don't want one, dereference. */
15058 if (!want_address && have_address)
15060 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15062 if (size > DWARF2_ADDR_SIZE || size == -1)
15064 expansion_failed (loc, NULL_RTX,
15065 "DWARF address size mismatch");
15066 return 0;
15068 else if (size == DWARF2_ADDR_SIZE)
15069 op = DW_OP_deref;
15070 else
15071 op = DW_OP_deref_size;
15073 if (ret)
15074 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15075 else
15076 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15078 if (ret)
15079 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15081 return list_ret;
15084 /* Same as above but return only single location expression. */
15085 static dw_loc_descr_ref
15086 loc_descriptor_from_tree (tree loc, int want_address,
15087 const struct loc_descr_context *context)
15089 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15090 if (!ret)
15091 return NULL;
15092 if (ret->dw_loc_next)
15094 expansion_failed (loc, NULL_RTX,
15095 "Location list where only loc descriptor needed");
15096 return NULL;
15098 return ret->expr;
15101 /* Given a value, round it up to the lowest multiple of `boundary'
15102 which is not less than the value itself. */
15104 static inline HOST_WIDE_INT
15105 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15107 return (((value + boundary - 1) / boundary) * boundary);
15110 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15111 pointer to the declared type for the relevant field variable, or return
15112 `integer_type_node' if the given node turns out to be an
15113 ERROR_MARK node. */
15115 static inline tree
15116 field_type (const_tree decl)
15118 tree type;
15120 if (TREE_CODE (decl) == ERROR_MARK)
15121 return integer_type_node;
15123 type = DECL_BIT_FIELD_TYPE (decl);
15124 if (type == NULL_TREE)
15125 type = TREE_TYPE (decl);
15127 return type;
15130 /* Given a pointer to a tree node, return the alignment in bits for
15131 it, or else return BITS_PER_WORD if the node actually turns out to
15132 be an ERROR_MARK node. */
15134 static inline unsigned
15135 simple_type_align_in_bits (const_tree type)
15137 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15140 static inline unsigned
15141 simple_decl_align_in_bits (const_tree decl)
15143 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15146 /* Return the result of rounding T up to ALIGN. */
15148 static inline offset_int
15149 round_up_to_align (const offset_int &t, unsigned int align)
15151 return wi::udiv_trunc (t + align - 1, align) * align;
15154 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15155 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15156 or return 0 if we are unable to determine what that offset is, either
15157 because the argument turns out to be a pointer to an ERROR_MARK node, or
15158 because the offset is actually variable. (We can't handle the latter case
15159 just yet). */
15161 static HOST_WIDE_INT
15162 field_byte_offset (const_tree decl)
15164 offset_int object_offset_in_bits;
15165 offset_int object_offset_in_bytes;
15166 offset_int bitpos_int;
15168 if (TREE_CODE (decl) == ERROR_MARK)
15169 return 0;
15171 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15173 /* We cannot yet cope with fields whose positions are variable, so
15174 for now, when we see such things, we simply return 0. Someday, we may
15175 be able to handle such cases, but it will be damn difficult. */
15176 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15177 return 0;
15179 bitpos_int = wi::to_offset (bit_position (decl));
15181 if (PCC_BITFIELD_TYPE_MATTERS)
15183 tree type;
15184 tree field_size_tree;
15185 offset_int deepest_bitpos;
15186 offset_int field_size_in_bits;
15187 unsigned int type_align_in_bits;
15188 unsigned int decl_align_in_bits;
15189 offset_int type_size_in_bits;
15191 type = field_type (decl);
15192 type_size_in_bits = offset_int_type_size_in_bits (type);
15193 type_align_in_bits = simple_type_align_in_bits (type);
15195 field_size_tree = DECL_SIZE (decl);
15197 /* The size could be unspecified if there was an error, or for
15198 a flexible array member. */
15199 if (!field_size_tree)
15200 field_size_tree = bitsize_zero_node;
15202 /* If the size of the field is not constant, use the type size. */
15203 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15204 field_size_in_bits = wi::to_offset (field_size_tree);
15205 else
15206 field_size_in_bits = type_size_in_bits;
15208 decl_align_in_bits = simple_decl_align_in_bits (decl);
15210 /* The GCC front-end doesn't make any attempt to keep track of the
15211 starting bit offset (relative to the start of the containing
15212 structure type) of the hypothetical "containing object" for a
15213 bit-field. Thus, when computing the byte offset value for the
15214 start of the "containing object" of a bit-field, we must deduce
15215 this information on our own. This can be rather tricky to do in
15216 some cases. For example, handling the following structure type
15217 definition when compiling for an i386/i486 target (which only
15218 aligns long long's to 32-bit boundaries) can be very tricky:
15220 struct S { int field1; long long field2:31; };
15222 Fortunately, there is a simple rule-of-thumb which can be used
15223 in such cases. When compiling for an i386/i486, GCC will
15224 allocate 8 bytes for the structure shown above. It decides to
15225 do this based upon one simple rule for bit-field allocation.
15226 GCC allocates each "containing object" for each bit-field at
15227 the first (i.e. lowest addressed) legitimate alignment boundary
15228 (based upon the required minimum alignment for the declared
15229 type of the field) which it can possibly use, subject to the
15230 condition that there is still enough available space remaining
15231 in the containing object (when allocated at the selected point)
15232 to fully accommodate all of the bits of the bit-field itself.
15234 This simple rule makes it obvious why GCC allocates 8 bytes for
15235 each object of the structure type shown above. When looking
15236 for a place to allocate the "containing object" for `field2',
15237 the compiler simply tries to allocate a 64-bit "containing
15238 object" at each successive 32-bit boundary (starting at zero)
15239 until it finds a place to allocate that 64- bit field such that
15240 at least 31 contiguous (and previously unallocated) bits remain
15241 within that selected 64 bit field. (As it turns out, for the
15242 example above, the compiler finds it is OK to allocate the
15243 "containing object" 64-bit field at bit-offset zero within the
15244 structure type.)
15246 Here we attempt to work backwards from the limited set of facts
15247 we're given, and we try to deduce from those facts, where GCC
15248 must have believed that the containing object started (within
15249 the structure type). The value we deduce is then used (by the
15250 callers of this routine) to generate DW_AT_location and
15251 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15252 the case of DW_AT_location, regular fields as well). */
15254 /* Figure out the bit-distance from the start of the structure to
15255 the "deepest" bit of the bit-field. */
15256 deepest_bitpos = bitpos_int + field_size_in_bits;
15258 /* This is the tricky part. Use some fancy footwork to deduce
15259 where the lowest addressed bit of the containing object must
15260 be. */
15261 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15263 /* Round up to type_align by default. This works best for
15264 bitfields. */
15265 object_offset_in_bits
15266 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15268 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15270 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15272 /* Round up to decl_align instead. */
15273 object_offset_in_bits
15274 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15277 else
15278 object_offset_in_bits = bitpos_int;
15280 object_offset_in_bytes
15281 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15282 return object_offset_in_bytes.to_shwi ();
15285 /* The following routines define various Dwarf attributes and any data
15286 associated with them. */
15288 /* Add a location description attribute value to a DIE.
15290 This emits location attributes suitable for whole variables and
15291 whole parameters. Note that the location attributes for struct fields are
15292 generated by the routine `data_member_location_attribute' below. */
15294 static inline void
15295 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15296 dw_loc_list_ref descr)
15298 if (descr == 0)
15299 return;
15300 if (single_element_loc_list_p (descr))
15301 add_AT_loc (die, attr_kind, descr->expr);
15302 else
15303 add_AT_loc_list (die, attr_kind, descr);
15306 /* Add DW_AT_accessibility attribute to DIE if needed. */
15308 static void
15309 add_accessibility_attribute (dw_die_ref die, tree decl)
15311 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15312 children, otherwise the default is DW_ACCESS_public. In DWARF2
15313 the default has always been DW_ACCESS_public. */
15314 if (TREE_PROTECTED (decl))
15315 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15316 else if (TREE_PRIVATE (decl))
15318 if (dwarf_version == 2
15319 || die->die_parent == NULL
15320 || die->die_parent->die_tag != DW_TAG_class_type)
15321 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15323 else if (dwarf_version > 2
15324 && die->die_parent
15325 && die->die_parent->die_tag == DW_TAG_class_type)
15326 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15329 /* Attach the specialized form of location attribute used for data members of
15330 struct and union types. In the special case of a FIELD_DECL node which
15331 represents a bit-field, the "offset" part of this special location
15332 descriptor must indicate the distance in bytes from the lowest-addressed
15333 byte of the containing struct or union type to the lowest-addressed byte of
15334 the "containing object" for the bit-field. (See the `field_byte_offset'
15335 function above).
15337 For any given bit-field, the "containing object" is a hypothetical object
15338 (of some integral or enum type) within which the given bit-field lives. The
15339 type of this hypothetical "containing object" is always the same as the
15340 declared type of the individual bit-field itself (for GCC anyway... the
15341 DWARF spec doesn't actually mandate this). Note that it is the size (in
15342 bytes) of the hypothetical "containing object" which will be given in the
15343 DW_AT_byte_size attribute for this bit-field. (See the
15344 `byte_size_attribute' function below.) It is also used when calculating the
15345 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15346 function below.) */
15348 static void
15349 add_data_member_location_attribute (dw_die_ref die, tree decl)
15351 HOST_WIDE_INT offset;
15352 dw_loc_descr_ref loc_descr = 0;
15354 if (TREE_CODE (decl) == TREE_BINFO)
15356 /* We're working on the TAG_inheritance for a base class. */
15357 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15359 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15360 aren't at a fixed offset from all (sub)objects of the same
15361 type. We need to extract the appropriate offset from our
15362 vtable. The following dwarf expression means
15364 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15366 This is specific to the V3 ABI, of course. */
15368 dw_loc_descr_ref tmp;
15370 /* Make a copy of the object address. */
15371 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15372 add_loc_descr (&loc_descr, tmp);
15374 /* Extract the vtable address. */
15375 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15376 add_loc_descr (&loc_descr, tmp);
15378 /* Calculate the address of the offset. */
15379 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15380 gcc_assert (offset < 0);
15382 tmp = int_loc_descriptor (-offset);
15383 add_loc_descr (&loc_descr, tmp);
15384 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15385 add_loc_descr (&loc_descr, tmp);
15387 /* Extract the offset. */
15388 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15389 add_loc_descr (&loc_descr, tmp);
15391 /* Add it to the object address. */
15392 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15393 add_loc_descr (&loc_descr, tmp);
15395 else
15396 offset = tree_to_shwi (BINFO_OFFSET (decl));
15398 else
15399 offset = field_byte_offset (decl);
15401 if (! loc_descr)
15403 if (dwarf_version > 2)
15405 /* Don't need to output a location expression, just the constant. */
15406 if (offset < 0)
15407 add_AT_int (die, DW_AT_data_member_location, offset);
15408 else
15409 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15410 return;
15412 else
15414 enum dwarf_location_atom op;
15416 /* The DWARF2 standard says that we should assume that the structure
15417 address is already on the stack, so we can specify a structure
15418 field address by using DW_OP_plus_uconst. */
15419 op = DW_OP_plus_uconst;
15420 loc_descr = new_loc_descr (op, offset, 0);
15424 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15427 /* Writes integer values to dw_vec_const array. */
15429 static void
15430 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15432 while (size != 0)
15434 *dest++ = val & 0xff;
15435 val >>= 8;
15436 --size;
15440 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15442 static HOST_WIDE_INT
15443 extract_int (const unsigned char *src, unsigned int size)
15445 HOST_WIDE_INT val = 0;
15447 src += size;
15448 while (size != 0)
15450 val <<= 8;
15451 val |= *--src & 0xff;
15452 --size;
15454 return val;
15457 /* Writes wide_int values to dw_vec_const array. */
15459 static void
15460 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15462 int i;
15464 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15466 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15467 return;
15470 /* We'd have to extend this code to support odd sizes. */
15471 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15473 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15475 if (WORDS_BIG_ENDIAN)
15476 for (i = n - 1; i >= 0; i--)
15478 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15479 dest += sizeof (HOST_WIDE_INT);
15481 else
15482 for (i = 0; i < n; i++)
15484 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15485 dest += sizeof (HOST_WIDE_INT);
15489 /* Writes floating point values to dw_vec_const array. */
15491 static void
15492 insert_float (const_rtx rtl, unsigned char *array)
15494 REAL_VALUE_TYPE rv;
15495 long val[4];
15496 int i;
15498 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15499 real_to_target (val, &rv, GET_MODE (rtl));
15501 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15502 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15504 insert_int (val[i], 4, array);
15505 array += 4;
15509 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15510 does not have a "location" either in memory or in a register. These
15511 things can arise in GNU C when a constant is passed as an actual parameter
15512 to an inlined function. They can also arise in C++ where declared
15513 constants do not necessarily get memory "homes". */
15515 static bool
15516 add_const_value_attribute (dw_die_ref die, rtx rtl)
15518 switch (GET_CODE (rtl))
15520 case CONST_INT:
15522 HOST_WIDE_INT val = INTVAL (rtl);
15524 if (val < 0)
15525 add_AT_int (die, DW_AT_const_value, val);
15526 else
15527 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15529 return true;
15531 case CONST_WIDE_INT:
15532 add_AT_wide (die, DW_AT_const_value,
15533 std::make_pair (rtl, GET_MODE (rtl)));
15534 return true;
15536 case CONST_DOUBLE:
15537 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15538 floating-point constant. A CONST_DOUBLE is used whenever the
15539 constant requires more than one word in order to be adequately
15540 represented. */
15542 machine_mode mode = GET_MODE (rtl);
15544 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15545 add_AT_double (die, DW_AT_const_value,
15546 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15547 else
15549 unsigned int length = GET_MODE_SIZE (mode);
15550 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15552 insert_float (rtl, array);
15553 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15556 return true;
15558 case CONST_VECTOR:
15560 machine_mode mode = GET_MODE (rtl);
15561 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15562 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15563 unsigned char *array
15564 = ggc_vec_alloc<unsigned char> (length * elt_size);
15565 unsigned int i;
15566 unsigned char *p;
15567 machine_mode imode = GET_MODE_INNER (mode);
15569 switch (GET_MODE_CLASS (mode))
15571 case MODE_VECTOR_INT:
15572 for (i = 0, p = array; i < length; i++, p += elt_size)
15574 rtx elt = CONST_VECTOR_ELT (rtl, i);
15575 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15577 break;
15579 case MODE_VECTOR_FLOAT:
15580 for (i = 0, p = array; i < length; i++, p += elt_size)
15582 rtx elt = CONST_VECTOR_ELT (rtl, i);
15583 insert_float (elt, p);
15585 break;
15587 default:
15588 gcc_unreachable ();
15591 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15593 return true;
15595 case CONST_STRING:
15596 if (dwarf_version >= 4 || !dwarf_strict)
15598 dw_loc_descr_ref loc_result;
15599 resolve_one_addr (&rtl);
15600 rtl_addr:
15601 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15602 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15603 add_AT_loc (die, DW_AT_location, loc_result);
15604 vec_safe_push (used_rtx_array, rtl);
15605 return true;
15607 return false;
15609 case CONST:
15610 if (CONSTANT_P (XEXP (rtl, 0)))
15611 return add_const_value_attribute (die, XEXP (rtl, 0));
15612 /* FALLTHROUGH */
15613 case SYMBOL_REF:
15614 if (!const_ok_for_output (rtl))
15615 return false;
15616 case LABEL_REF:
15617 if (dwarf_version >= 4 || !dwarf_strict)
15618 goto rtl_addr;
15619 return false;
15621 case PLUS:
15622 /* In cases where an inlined instance of an inline function is passed
15623 the address of an `auto' variable (which is local to the caller) we
15624 can get a situation where the DECL_RTL of the artificial local
15625 variable (for the inlining) which acts as a stand-in for the
15626 corresponding formal parameter (of the inline function) will look
15627 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15628 exactly a compile-time constant expression, but it isn't the address
15629 of the (artificial) local variable either. Rather, it represents the
15630 *value* which the artificial local variable always has during its
15631 lifetime. We currently have no way to represent such quasi-constant
15632 values in Dwarf, so for now we just punt and generate nothing. */
15633 return false;
15635 case HIGH:
15636 case CONST_FIXED:
15637 return false;
15639 case MEM:
15640 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15641 && MEM_READONLY_P (rtl)
15642 && GET_MODE (rtl) == BLKmode)
15644 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15645 return true;
15647 return false;
15649 default:
15650 /* No other kinds of rtx should be possible here. */
15651 gcc_unreachable ();
15653 return false;
15656 /* Determine whether the evaluation of EXPR references any variables
15657 or functions which aren't otherwise used (and therefore may not be
15658 output). */
15659 static tree
15660 reference_to_unused (tree * tp, int * walk_subtrees,
15661 void * data ATTRIBUTE_UNUSED)
15663 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15664 *walk_subtrees = 0;
15666 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15667 && ! TREE_ASM_WRITTEN (*tp))
15668 return *tp;
15669 /* ??? The C++ FE emits debug information for using decls, so
15670 putting gcc_unreachable here falls over. See PR31899. For now
15671 be conservative. */
15672 else if (!symtab->global_info_ready
15673 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15674 return *tp;
15675 else if (TREE_CODE (*tp) == VAR_DECL)
15677 varpool_node *node = varpool_node::get (*tp);
15678 if (!node || !node->definition)
15679 return *tp;
15681 else if (TREE_CODE (*tp) == FUNCTION_DECL
15682 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15684 /* The call graph machinery must have finished analyzing,
15685 optimizing and gimplifying the CU by now.
15686 So if *TP has no call graph node associated
15687 to it, it means *TP will not be emitted. */
15688 if (!cgraph_node::get (*tp))
15689 return *tp;
15691 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15692 return *tp;
15694 return NULL_TREE;
15697 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15698 for use in a later add_const_value_attribute call. */
15700 static rtx
15701 rtl_for_decl_init (tree init, tree type)
15703 rtx rtl = NULL_RTX;
15705 STRIP_NOPS (init);
15707 /* If a variable is initialized with a string constant without embedded
15708 zeros, build CONST_STRING. */
15709 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15711 tree enttype = TREE_TYPE (type);
15712 tree domain = TYPE_DOMAIN (type);
15713 machine_mode mode = TYPE_MODE (enttype);
15715 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15716 && domain
15717 && integer_zerop (TYPE_MIN_VALUE (domain))
15718 && compare_tree_int (TYPE_MAX_VALUE (domain),
15719 TREE_STRING_LENGTH (init) - 1) == 0
15720 && ((size_t) TREE_STRING_LENGTH (init)
15721 == strlen (TREE_STRING_POINTER (init)) + 1))
15723 rtl = gen_rtx_CONST_STRING (VOIDmode,
15724 ggc_strdup (TREE_STRING_POINTER (init)));
15725 rtl = gen_rtx_MEM (BLKmode, rtl);
15726 MEM_READONLY_P (rtl) = 1;
15729 /* Other aggregates, and complex values, could be represented using
15730 CONCAT: FIXME! */
15731 else if (AGGREGATE_TYPE_P (type)
15732 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15733 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15734 || TREE_CODE (type) == COMPLEX_TYPE)
15736 /* Vectors only work if their mode is supported by the target.
15737 FIXME: generic vectors ought to work too. */
15738 else if (TREE_CODE (type) == VECTOR_TYPE
15739 && !VECTOR_MODE_P (TYPE_MODE (type)))
15741 /* If the initializer is something that we know will expand into an
15742 immediate RTL constant, expand it now. We must be careful not to
15743 reference variables which won't be output. */
15744 else if (initializer_constant_valid_p (init, type)
15745 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15747 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15748 possible. */
15749 if (TREE_CODE (type) == VECTOR_TYPE)
15750 switch (TREE_CODE (init))
15752 case VECTOR_CST:
15753 break;
15754 case CONSTRUCTOR:
15755 if (TREE_CONSTANT (init))
15757 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15758 bool constant_p = true;
15759 tree value;
15760 unsigned HOST_WIDE_INT ix;
15762 /* Even when ctor is constant, it might contain non-*_CST
15763 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15764 belong into VECTOR_CST nodes. */
15765 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15766 if (!CONSTANT_CLASS_P (value))
15768 constant_p = false;
15769 break;
15772 if (constant_p)
15774 init = build_vector_from_ctor (type, elts);
15775 break;
15778 /* FALLTHRU */
15780 default:
15781 return NULL;
15784 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15786 /* If expand_expr returns a MEM, it wasn't immediate. */
15787 gcc_assert (!rtl || !MEM_P (rtl));
15790 return rtl;
15793 /* Generate RTL for the variable DECL to represent its location. */
15795 static rtx
15796 rtl_for_decl_location (tree decl)
15798 rtx rtl;
15800 /* Here we have to decide where we are going to say the parameter "lives"
15801 (as far as the debugger is concerned). We only have a couple of
15802 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15804 DECL_RTL normally indicates where the parameter lives during most of the
15805 activation of the function. If optimization is enabled however, this
15806 could be either NULL or else a pseudo-reg. Both of those cases indicate
15807 that the parameter doesn't really live anywhere (as far as the code
15808 generation parts of GCC are concerned) during most of the function's
15809 activation. That will happen (for example) if the parameter is never
15810 referenced within the function.
15812 We could just generate a location descriptor here for all non-NULL
15813 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15814 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15815 where DECL_RTL is NULL or is a pseudo-reg.
15817 Note however that we can only get away with using DECL_INCOMING_RTL as
15818 a backup substitute for DECL_RTL in certain limited cases. In cases
15819 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15820 we can be sure that the parameter was passed using the same type as it is
15821 declared to have within the function, and that its DECL_INCOMING_RTL
15822 points us to a place where a value of that type is passed.
15824 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15825 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15826 because in these cases DECL_INCOMING_RTL points us to a value of some
15827 type which is *different* from the type of the parameter itself. Thus,
15828 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15829 such cases, the debugger would end up (for example) trying to fetch a
15830 `float' from a place which actually contains the first part of a
15831 `double'. That would lead to really incorrect and confusing
15832 output at debug-time.
15834 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15835 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15836 are a couple of exceptions however. On little-endian machines we can
15837 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15838 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15839 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15840 when (on a little-endian machine) a non-prototyped function has a
15841 parameter declared to be of type `short' or `char'. In such cases,
15842 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15843 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15844 passed `int' value. If the debugger then uses that address to fetch
15845 a `short' or a `char' (on a little-endian machine) the result will be
15846 the correct data, so we allow for such exceptional cases below.
15848 Note that our goal here is to describe the place where the given formal
15849 parameter lives during most of the function's activation (i.e. between the
15850 end of the prologue and the start of the epilogue). We'll do that as best
15851 as we can. Note however that if the given formal parameter is modified
15852 sometime during the execution of the function, then a stack backtrace (at
15853 debug-time) will show the function as having been called with the *new*
15854 value rather than the value which was originally passed in. This happens
15855 rarely enough that it is not a major problem, but it *is* a problem, and
15856 I'd like to fix it.
15858 A future version of dwarf2out.c may generate two additional attributes for
15859 any given DW_TAG_formal_parameter DIE which will describe the "passed
15860 type" and the "passed location" for the given formal parameter in addition
15861 to the attributes we now generate to indicate the "declared type" and the
15862 "active location" for each parameter. This additional set of attributes
15863 could be used by debuggers for stack backtraces. Separately, note that
15864 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15865 This happens (for example) for inlined-instances of inline function formal
15866 parameters which are never referenced. This really shouldn't be
15867 happening. All PARM_DECL nodes should get valid non-NULL
15868 DECL_INCOMING_RTL values. FIXME. */
15870 /* Use DECL_RTL as the "location" unless we find something better. */
15871 rtl = DECL_RTL_IF_SET (decl);
15873 /* When generating abstract instances, ignore everything except
15874 constants, symbols living in memory, and symbols living in
15875 fixed registers. */
15876 if (! reload_completed)
15878 if (rtl
15879 && (CONSTANT_P (rtl)
15880 || (MEM_P (rtl)
15881 && CONSTANT_P (XEXP (rtl, 0)))
15882 || (REG_P (rtl)
15883 && TREE_CODE (decl) == VAR_DECL
15884 && TREE_STATIC (decl))))
15886 rtl = targetm.delegitimize_address (rtl);
15887 return rtl;
15889 rtl = NULL_RTX;
15891 else if (TREE_CODE (decl) == PARM_DECL)
15893 if (rtl == NULL_RTX
15894 || is_pseudo_reg (rtl)
15895 || (MEM_P (rtl)
15896 && is_pseudo_reg (XEXP (rtl, 0))
15897 && DECL_INCOMING_RTL (decl)
15898 && MEM_P (DECL_INCOMING_RTL (decl))
15899 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15901 tree declared_type = TREE_TYPE (decl);
15902 tree passed_type = DECL_ARG_TYPE (decl);
15903 machine_mode dmode = TYPE_MODE (declared_type);
15904 machine_mode pmode = TYPE_MODE (passed_type);
15906 /* This decl represents a formal parameter which was optimized out.
15907 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15908 all cases where (rtl == NULL_RTX) just below. */
15909 if (dmode == pmode)
15910 rtl = DECL_INCOMING_RTL (decl);
15911 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15912 && SCALAR_INT_MODE_P (dmode)
15913 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15914 && DECL_INCOMING_RTL (decl))
15916 rtx inc = DECL_INCOMING_RTL (decl);
15917 if (REG_P (inc))
15918 rtl = inc;
15919 else if (MEM_P (inc))
15921 if (BYTES_BIG_ENDIAN)
15922 rtl = adjust_address_nv (inc, dmode,
15923 GET_MODE_SIZE (pmode)
15924 - GET_MODE_SIZE (dmode));
15925 else
15926 rtl = inc;
15931 /* If the parm was passed in registers, but lives on the stack, then
15932 make a big endian correction if the mode of the type of the
15933 parameter is not the same as the mode of the rtl. */
15934 /* ??? This is the same series of checks that are made in dbxout.c before
15935 we reach the big endian correction code there. It isn't clear if all
15936 of these checks are necessary here, but keeping them all is the safe
15937 thing to do. */
15938 else if (MEM_P (rtl)
15939 && XEXP (rtl, 0) != const0_rtx
15940 && ! CONSTANT_P (XEXP (rtl, 0))
15941 /* Not passed in memory. */
15942 && !MEM_P (DECL_INCOMING_RTL (decl))
15943 /* Not passed by invisible reference. */
15944 && (!REG_P (XEXP (rtl, 0))
15945 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15946 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15947 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15948 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15949 #endif
15951 /* Big endian correction check. */
15952 && BYTES_BIG_ENDIAN
15953 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15954 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15955 < UNITS_PER_WORD))
15957 machine_mode addr_mode = get_address_mode (rtl);
15958 int offset = (UNITS_PER_WORD
15959 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15961 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15962 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15965 else if (TREE_CODE (decl) == VAR_DECL
15966 && rtl
15967 && MEM_P (rtl)
15968 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15969 && BYTES_BIG_ENDIAN)
15971 machine_mode addr_mode = get_address_mode (rtl);
15972 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15973 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15975 /* If a variable is declared "register" yet is smaller than
15976 a register, then if we store the variable to memory, it
15977 looks like we're storing a register-sized value, when in
15978 fact we are not. We need to adjust the offset of the
15979 storage location to reflect the actual value's bytes,
15980 else gdb will not be able to display it. */
15981 if (rsize > dsize)
15982 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15983 plus_constant (addr_mode, XEXP (rtl, 0),
15984 rsize - dsize));
15987 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15988 and will have been substituted directly into all expressions that use it.
15989 C does not have such a concept, but C++ and other languages do. */
15990 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15991 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15993 if (rtl)
15994 rtl = targetm.delegitimize_address (rtl);
15996 /* If we don't look past the constant pool, we risk emitting a
15997 reference to a constant pool entry that isn't referenced from
15998 code, and thus is not emitted. */
15999 if (rtl)
16000 rtl = avoid_constant_pool_reference (rtl);
16002 /* Try harder to get a rtl. If this symbol ends up not being emitted
16003 in the current CU, resolve_addr will remove the expression referencing
16004 it. */
16005 if (rtl == NULL_RTX
16006 && TREE_CODE (decl) == VAR_DECL
16007 && !DECL_EXTERNAL (decl)
16008 && TREE_STATIC (decl)
16009 && DECL_NAME (decl)
16010 && !DECL_HARD_REGISTER (decl)
16011 && DECL_MODE (decl) != VOIDmode)
16013 rtl = make_decl_rtl_for_debug (decl);
16014 if (!MEM_P (rtl)
16015 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16016 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16017 rtl = NULL_RTX;
16020 return rtl;
16023 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16024 returned. If so, the decl for the COMMON block is returned, and the
16025 value is the offset into the common block for the symbol. */
16027 static tree
16028 fortran_common (tree decl, HOST_WIDE_INT *value)
16030 tree val_expr, cvar;
16031 machine_mode mode;
16032 HOST_WIDE_INT bitsize, bitpos;
16033 tree offset;
16034 int unsignedp, volatilep = 0;
16036 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16037 it does not have a value (the offset into the common area), or if it
16038 is thread local (as opposed to global) then it isn't common, and shouldn't
16039 be handled as such. */
16040 if (TREE_CODE (decl) != VAR_DECL
16041 || !TREE_STATIC (decl)
16042 || !DECL_HAS_VALUE_EXPR_P (decl)
16043 || !is_fortran ())
16044 return NULL_TREE;
16046 val_expr = DECL_VALUE_EXPR (decl);
16047 if (TREE_CODE (val_expr) != COMPONENT_REF)
16048 return NULL_TREE;
16050 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16051 &mode, &unsignedp, &volatilep, true);
16053 if (cvar == NULL_TREE
16054 || TREE_CODE (cvar) != VAR_DECL
16055 || DECL_ARTIFICIAL (cvar)
16056 || !TREE_PUBLIC (cvar))
16057 return NULL_TREE;
16059 *value = 0;
16060 if (offset != NULL)
16062 if (!tree_fits_shwi_p (offset))
16063 return NULL_TREE;
16064 *value = tree_to_shwi (offset);
16066 if (bitpos != 0)
16067 *value += bitpos / BITS_PER_UNIT;
16069 return cvar;
16072 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16073 data attribute for a variable or a parameter. We generate the
16074 DW_AT_const_value attribute only in those cases where the given variable
16075 or parameter does not have a true "location" either in memory or in a
16076 register. This can happen (for example) when a constant is passed as an
16077 actual argument in a call to an inline function. (It's possible that
16078 these things can crop up in other ways also.) Note that one type of
16079 constant value which can be passed into an inlined function is a constant
16080 pointer. This can happen for example if an actual argument in an inlined
16081 function call evaluates to a compile-time constant address.
16083 CACHE_P is true if it is worth caching the location list for DECL,
16084 so that future calls can reuse it rather than regenerate it from scratch.
16085 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16086 since we will need to refer to them each time the function is inlined. */
16088 static bool
16089 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16090 enum dwarf_attribute attr)
16092 rtx rtl;
16093 dw_loc_list_ref list;
16094 var_loc_list *loc_list;
16095 cached_dw_loc_list *cache;
16097 if (TREE_CODE (decl) == ERROR_MARK)
16098 return false;
16100 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16101 || TREE_CODE (decl) == RESULT_DECL);
16103 /* Try to get some constant RTL for this decl, and use that as the value of
16104 the location. */
16106 rtl = rtl_for_decl_location (decl);
16107 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16108 && add_const_value_attribute (die, rtl))
16109 return true;
16111 /* See if we have single element location list that is equivalent to
16112 a constant value. That way we are better to use add_const_value_attribute
16113 rather than expanding constant value equivalent. */
16114 loc_list = lookup_decl_loc (decl);
16115 if (loc_list
16116 && loc_list->first
16117 && loc_list->first->next == NULL
16118 && NOTE_P (loc_list->first->loc)
16119 && NOTE_VAR_LOCATION (loc_list->first->loc)
16120 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16122 struct var_loc_node *node;
16124 node = loc_list->first;
16125 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16126 if (GET_CODE (rtl) == EXPR_LIST)
16127 rtl = XEXP (rtl, 0);
16128 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16129 && add_const_value_attribute (die, rtl))
16130 return true;
16132 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16133 list several times. See if we've already cached the contents. */
16134 list = NULL;
16135 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16136 cache_p = false;
16137 if (cache_p)
16139 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16140 if (cache)
16141 list = cache->loc_list;
16143 if (list == NULL)
16145 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16146 NULL);
16147 /* It is usually worth caching this result if the decl is from
16148 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16149 if (cache_p && list && list->dw_loc_next)
16151 cached_dw_loc_list **slot
16152 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16153 DECL_UID (decl),
16154 INSERT);
16155 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16156 cache->decl_id = DECL_UID (decl);
16157 cache->loc_list = list;
16158 *slot = cache;
16161 if (list)
16163 add_AT_location_description (die, attr, list);
16164 return true;
16166 /* None of that worked, so it must not really have a location;
16167 try adding a constant value attribute from the DECL_INITIAL. */
16168 return tree_add_const_value_attribute_for_decl (die, decl);
16171 /* Helper function for tree_add_const_value_attribute. Natively encode
16172 initializer INIT into an array. Return true if successful. */
16174 static bool
16175 native_encode_initializer (tree init, unsigned char *array, int size)
16177 tree type;
16179 if (init == NULL_TREE)
16180 return false;
16182 STRIP_NOPS (init);
16183 switch (TREE_CODE (init))
16185 case STRING_CST:
16186 type = TREE_TYPE (init);
16187 if (TREE_CODE (type) == ARRAY_TYPE)
16189 tree enttype = TREE_TYPE (type);
16190 machine_mode mode = TYPE_MODE (enttype);
16192 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16193 return false;
16194 if (int_size_in_bytes (type) != size)
16195 return false;
16196 if (size > TREE_STRING_LENGTH (init))
16198 memcpy (array, TREE_STRING_POINTER (init),
16199 TREE_STRING_LENGTH (init));
16200 memset (array + TREE_STRING_LENGTH (init),
16201 '\0', size - TREE_STRING_LENGTH (init));
16203 else
16204 memcpy (array, TREE_STRING_POINTER (init), size);
16205 return true;
16207 return false;
16208 case CONSTRUCTOR:
16209 type = TREE_TYPE (init);
16210 if (int_size_in_bytes (type) != size)
16211 return false;
16212 if (TREE_CODE (type) == ARRAY_TYPE)
16214 HOST_WIDE_INT min_index;
16215 unsigned HOST_WIDE_INT cnt;
16216 int curpos = 0, fieldsize;
16217 constructor_elt *ce;
16219 if (TYPE_DOMAIN (type) == NULL_TREE
16220 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16221 return false;
16223 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16224 if (fieldsize <= 0)
16225 return false;
16227 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16228 memset (array, '\0', size);
16229 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16231 tree val = ce->value;
16232 tree index = ce->index;
16233 int pos = curpos;
16234 if (index && TREE_CODE (index) == RANGE_EXPR)
16235 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16236 * fieldsize;
16237 else if (index)
16238 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16240 if (val)
16242 STRIP_NOPS (val);
16243 if (!native_encode_initializer (val, array + pos, fieldsize))
16244 return false;
16246 curpos = pos + fieldsize;
16247 if (index && TREE_CODE (index) == RANGE_EXPR)
16249 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16250 - tree_to_shwi (TREE_OPERAND (index, 0));
16251 while (count-- > 0)
16253 if (val)
16254 memcpy (array + curpos, array + pos, fieldsize);
16255 curpos += fieldsize;
16258 gcc_assert (curpos <= size);
16260 return true;
16262 else if (TREE_CODE (type) == RECORD_TYPE
16263 || TREE_CODE (type) == UNION_TYPE)
16265 tree field = NULL_TREE;
16266 unsigned HOST_WIDE_INT cnt;
16267 constructor_elt *ce;
16269 if (int_size_in_bytes (type) != size)
16270 return false;
16272 if (TREE_CODE (type) == RECORD_TYPE)
16273 field = TYPE_FIELDS (type);
16275 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16277 tree val = ce->value;
16278 int pos, fieldsize;
16280 if (ce->index != 0)
16281 field = ce->index;
16283 if (val)
16284 STRIP_NOPS (val);
16286 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16287 return false;
16289 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16290 && TYPE_DOMAIN (TREE_TYPE (field))
16291 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16292 return false;
16293 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16294 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16295 return false;
16296 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16297 pos = int_byte_position (field);
16298 gcc_assert (pos + fieldsize <= size);
16299 if (val
16300 && !native_encode_initializer (val, array + pos, fieldsize))
16301 return false;
16303 return true;
16305 return false;
16306 case VIEW_CONVERT_EXPR:
16307 case NON_LVALUE_EXPR:
16308 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16309 default:
16310 return native_encode_expr (init, array, size) == size;
16314 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16315 attribute is the const value T. */
16317 static bool
16318 tree_add_const_value_attribute (dw_die_ref die, tree t)
16320 tree init;
16321 tree type = TREE_TYPE (t);
16322 rtx rtl;
16324 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16325 return false;
16327 init = t;
16328 gcc_assert (!DECL_P (init));
16330 rtl = rtl_for_decl_init (init, type);
16331 if (rtl)
16332 return add_const_value_attribute (die, rtl);
16333 /* If the host and target are sane, try harder. */
16334 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16335 && initializer_constant_valid_p (init, type))
16337 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16338 if (size > 0 && (int) size == size)
16340 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16342 if (native_encode_initializer (init, array, size))
16344 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16345 return true;
16347 ggc_free (array);
16350 return false;
16353 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16354 attribute is the const value of T, where T is an integral constant
16355 variable with static storage duration
16356 (so it can't be a PARM_DECL or a RESULT_DECL). */
16358 static bool
16359 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16362 if (!decl
16363 || (TREE_CODE (decl) != VAR_DECL
16364 && TREE_CODE (decl) != CONST_DECL)
16365 || (TREE_CODE (decl) == VAR_DECL
16366 && !TREE_STATIC (decl)))
16367 return false;
16369 if (TREE_READONLY (decl)
16370 && ! TREE_THIS_VOLATILE (decl)
16371 && DECL_INITIAL (decl))
16372 /* OK */;
16373 else
16374 return false;
16376 /* Don't add DW_AT_const_value if abstract origin already has one. */
16377 if (get_AT (var_die, DW_AT_const_value))
16378 return false;
16380 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16383 /* Convert the CFI instructions for the current function into a
16384 location list. This is used for DW_AT_frame_base when we targeting
16385 a dwarf2 consumer that does not support the dwarf3
16386 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16387 expressions. */
16389 static dw_loc_list_ref
16390 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16392 int ix;
16393 dw_fde_ref fde;
16394 dw_loc_list_ref list, *list_tail;
16395 dw_cfi_ref cfi;
16396 dw_cfa_location last_cfa, next_cfa;
16397 const char *start_label, *last_label, *section;
16398 dw_cfa_location remember;
16400 fde = cfun->fde;
16401 gcc_assert (fde != NULL);
16403 section = secname_for_decl (current_function_decl);
16404 list_tail = &list;
16405 list = NULL;
16407 memset (&next_cfa, 0, sizeof (next_cfa));
16408 next_cfa.reg = INVALID_REGNUM;
16409 remember = next_cfa;
16411 start_label = fde->dw_fde_begin;
16413 /* ??? Bald assumption that the CIE opcode list does not contain
16414 advance opcodes. */
16415 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16416 lookup_cfa_1 (cfi, &next_cfa, &remember);
16418 last_cfa = next_cfa;
16419 last_label = start_label;
16421 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16423 /* If the first partition contained no CFI adjustments, the
16424 CIE opcodes apply to the whole first partition. */
16425 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16426 fde->dw_fde_begin, fde->dw_fde_end, section);
16427 list_tail =&(*list_tail)->dw_loc_next;
16428 start_label = last_label = fde->dw_fde_second_begin;
16431 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16433 switch (cfi->dw_cfi_opc)
16435 case DW_CFA_set_loc:
16436 case DW_CFA_advance_loc1:
16437 case DW_CFA_advance_loc2:
16438 case DW_CFA_advance_loc4:
16439 if (!cfa_equal_p (&last_cfa, &next_cfa))
16441 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16442 start_label, last_label, section);
16444 list_tail = &(*list_tail)->dw_loc_next;
16445 last_cfa = next_cfa;
16446 start_label = last_label;
16448 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16449 break;
16451 case DW_CFA_advance_loc:
16452 /* The encoding is complex enough that we should never emit this. */
16453 gcc_unreachable ();
16455 default:
16456 lookup_cfa_1 (cfi, &next_cfa, &remember);
16457 break;
16459 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16461 if (!cfa_equal_p (&last_cfa, &next_cfa))
16463 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16464 start_label, last_label, section);
16466 list_tail = &(*list_tail)->dw_loc_next;
16467 last_cfa = next_cfa;
16468 start_label = last_label;
16470 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16471 start_label, fde->dw_fde_end, section);
16472 list_tail = &(*list_tail)->dw_loc_next;
16473 start_label = last_label = fde->dw_fde_second_begin;
16477 if (!cfa_equal_p (&last_cfa, &next_cfa))
16479 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16480 start_label, last_label, section);
16481 list_tail = &(*list_tail)->dw_loc_next;
16482 start_label = last_label;
16485 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16486 start_label,
16487 fde->dw_fde_second_begin
16488 ? fde->dw_fde_second_end : fde->dw_fde_end,
16489 section);
16491 if (list && list->dw_loc_next)
16492 gen_llsym (list);
16494 return list;
16497 /* Compute a displacement from the "steady-state frame pointer" to the
16498 frame base (often the same as the CFA), and store it in
16499 frame_pointer_fb_offset. OFFSET is added to the displacement
16500 before the latter is negated. */
16502 static void
16503 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16505 rtx reg, elim;
16507 #ifdef FRAME_POINTER_CFA_OFFSET
16508 reg = frame_pointer_rtx;
16509 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16510 #else
16511 reg = arg_pointer_rtx;
16512 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16513 #endif
16515 elim = (ira_use_lra_p
16516 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16517 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16518 if (GET_CODE (elim) == PLUS)
16520 offset += INTVAL (XEXP (elim, 1));
16521 elim = XEXP (elim, 0);
16524 frame_pointer_fb_offset = -offset;
16526 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16527 in which to eliminate. This is because it's stack pointer isn't
16528 directly accessible as a register within the ISA. To work around
16529 this, assume that while we cannot provide a proper value for
16530 frame_pointer_fb_offset, we won't need one either. */
16531 frame_pointer_fb_offset_valid
16532 = ((SUPPORTS_STACK_ALIGNMENT
16533 && (elim == hard_frame_pointer_rtx
16534 || elim == stack_pointer_rtx))
16535 || elim == (frame_pointer_needed
16536 ? hard_frame_pointer_rtx
16537 : stack_pointer_rtx));
16540 /* Generate a DW_AT_name attribute given some string value to be included as
16541 the value of the attribute. */
16543 static void
16544 add_name_attribute (dw_die_ref die, const char *name_string)
16546 if (name_string != NULL && *name_string != 0)
16548 if (demangle_name_func)
16549 name_string = (*demangle_name_func) (name_string);
16551 add_AT_string (die, DW_AT_name, name_string);
16555 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16556 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16557 of TYPE accordingly.
16559 ??? This is a temporary measure until after we're able to generate
16560 regular DWARF for the complex Ada type system. */
16562 static void
16563 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16564 dw_die_ref context_die)
16566 tree dtype;
16567 dw_die_ref dtype_die;
16569 if (!lang_hooks.types.descriptive_type)
16570 return;
16572 dtype = lang_hooks.types.descriptive_type (type);
16573 if (!dtype)
16574 return;
16576 dtype_die = lookup_type_die (dtype);
16577 if (!dtype_die)
16579 gen_type_die (dtype, context_die);
16580 dtype_die = lookup_type_die (dtype);
16581 gcc_assert (dtype_die);
16584 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16587 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16589 static const char *
16590 comp_dir_string (void)
16592 const char *wd;
16593 char *wd1;
16594 static const char *cached_wd = NULL;
16596 if (cached_wd != NULL)
16597 return cached_wd;
16599 wd = get_src_pwd ();
16600 if (wd == NULL)
16601 return NULL;
16603 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16605 int wdlen;
16607 wdlen = strlen (wd);
16608 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16609 strcpy (wd1, wd);
16610 wd1 [wdlen] = DIR_SEPARATOR;
16611 wd1 [wdlen + 1] = 0;
16612 wd = wd1;
16615 cached_wd = remap_debug_filename (wd);
16616 return cached_wd;
16619 /* Generate a DW_AT_comp_dir attribute for DIE. */
16621 static void
16622 add_comp_dir_attribute (dw_die_ref die)
16624 const char * wd = comp_dir_string ();
16625 if (wd != NULL)
16626 add_AT_string (die, DW_AT_comp_dir, wd);
16629 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16630 pointer computation, ...), output a representation for that bound according
16631 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16632 loc_list_from_tree for the meaning of CONTEXT. */
16634 static void
16635 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16636 int forms, const struct loc_descr_context *context)
16638 dw_die_ref ctx, decl_die;
16639 dw_loc_list_ref list;
16641 bool strip_conversions = true;
16643 while (strip_conversions)
16644 switch (TREE_CODE (value))
16646 case ERROR_MARK:
16647 case SAVE_EXPR:
16648 return;
16650 CASE_CONVERT:
16651 case VIEW_CONVERT_EXPR:
16652 value = TREE_OPERAND (value, 0);
16653 break;
16655 default:
16656 strip_conversions = false;
16657 break;
16660 /* If possible and permitted, output the attribute as a constant. */
16661 if ((forms & dw_scalar_form_constant) != 0
16662 && TREE_CODE (value) == INTEGER_CST)
16664 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16666 /* If HOST_WIDE_INT is big enough then represent the bound as
16667 a constant value. We need to choose a form based on
16668 whether the type is signed or unsigned. We cannot just
16669 call add_AT_unsigned if the value itself is positive
16670 (add_AT_unsigned might add the unsigned value encoded as
16671 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16672 bounds type and then sign extend any unsigned values found
16673 for signed types. This is needed only for
16674 DW_AT_{lower,upper}_bound, since for most other attributes,
16675 consumers will treat DW_FORM_data[1248] as unsigned values,
16676 regardless of the underlying type. */
16677 if (prec <= HOST_BITS_PER_WIDE_INT
16678 || tree_fits_uhwi_p (value))
16680 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16681 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16682 else
16683 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16685 else
16686 /* Otherwise represent the bound as an unsigned value with
16687 the precision of its type. The precision and signedness
16688 of the type will be necessary to re-interpret it
16689 unambiguously. */
16690 add_AT_wide (die, attr, value);
16691 return;
16694 /* Otherwise, if it's possible and permitted too, output a reference to
16695 another DIE. */
16696 if ((forms & dw_scalar_form_reference) != 0)
16698 tree decl = NULL_TREE;
16700 /* Some type attributes reference an outer type. For instance, the upper
16701 bound of an array may reference an embedding record (this happens in
16702 Ada). */
16703 if (TREE_CODE (value) == COMPONENT_REF
16704 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16705 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16706 decl = TREE_OPERAND (value, 1);
16708 else if (TREE_CODE (value) == VAR_DECL
16709 || TREE_CODE (value) == PARM_DECL
16710 || TREE_CODE (value) == RESULT_DECL)
16711 decl = value;
16713 if (decl != NULL_TREE)
16715 dw_die_ref decl_die = lookup_decl_die (decl);
16717 /* ??? Can this happen, or should the variable have been bound
16718 first? Probably it can, since I imagine that we try to create
16719 the types of parameters in the order in which they exist in
16720 the list, and won't have created a forward reference to a
16721 later parameter. */
16722 if (decl_die != NULL)
16724 add_AT_die_ref (die, attr, decl_die);
16725 return;
16730 /* Last chance: try to create a stack operation procedure to evaluate the
16731 value. Do nothing if even that is not possible or permitted. */
16732 if ((forms & dw_scalar_form_exprloc) == 0)
16733 return;
16735 list = loc_list_from_tree (value, 2, context);
16736 if (list == NULL || single_element_loc_list_p (list))
16738 /* If this attribute is not a reference nor constant, it is
16739 a DWARF expression rather than location description. For that
16740 loc_list_from_tree (value, 0, &context) is needed. */
16741 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16742 if (list2 && single_element_loc_list_p (list2))
16744 add_AT_loc (die, attr, list2->expr);
16745 return;
16749 /* If that failed to give a single element location list, fall back to
16750 outputting this as a reference... still if permitted. */
16751 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16752 return;
16754 if (current_function_decl == 0)
16755 ctx = comp_unit_die ();
16756 else
16757 ctx = lookup_decl_die (current_function_decl);
16759 decl_die = new_die (DW_TAG_variable, ctx, value);
16760 add_AT_flag (decl_die, DW_AT_artificial, 1);
16761 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16762 add_AT_location_description (decl_die, DW_AT_location, list);
16763 add_AT_die_ref (die, attr, decl_die);
16766 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16767 default. */
16769 static int
16770 lower_bound_default (void)
16772 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16774 case DW_LANG_C:
16775 case DW_LANG_C89:
16776 case DW_LANG_C99:
16777 case DW_LANG_C11:
16778 case DW_LANG_C_plus_plus:
16779 case DW_LANG_C_plus_plus_11:
16780 case DW_LANG_C_plus_plus_14:
16781 case DW_LANG_ObjC:
16782 case DW_LANG_ObjC_plus_plus:
16783 case DW_LANG_Java:
16784 return 0;
16785 case DW_LANG_Fortran77:
16786 case DW_LANG_Fortran90:
16787 case DW_LANG_Fortran95:
16788 case DW_LANG_Fortran03:
16789 case DW_LANG_Fortran08:
16790 return 1;
16791 case DW_LANG_UPC:
16792 case DW_LANG_D:
16793 case DW_LANG_Python:
16794 return dwarf_version >= 4 ? 0 : -1;
16795 case DW_LANG_Ada95:
16796 case DW_LANG_Ada83:
16797 case DW_LANG_Cobol74:
16798 case DW_LANG_Cobol85:
16799 case DW_LANG_Pascal83:
16800 case DW_LANG_Modula2:
16801 case DW_LANG_PLI:
16802 return dwarf_version >= 4 ? 1 : -1;
16803 default:
16804 return -1;
16808 /* Given a tree node describing an array bound (either lower or upper) output
16809 a representation for that bound. */
16811 static void
16812 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16813 tree bound, const struct loc_descr_context *context)
16815 int dflt;
16817 while (1)
16818 switch (TREE_CODE (bound))
16820 /* Strip all conversions. */
16821 CASE_CONVERT:
16822 case VIEW_CONVERT_EXPR:
16823 bound = TREE_OPERAND (bound, 0);
16824 break;
16826 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16827 are even omitted when they are the default. */
16828 case INTEGER_CST:
16829 /* If the value for this bound is the default one, we can even omit the
16830 attribute. */
16831 if (bound_attr == DW_AT_lower_bound
16832 && tree_fits_shwi_p (bound)
16833 && (dflt = lower_bound_default ()) != -1
16834 && tree_to_shwi (bound) == dflt)
16835 return;
16837 /* FALLTHRU */
16839 default:
16840 add_scalar_info (subrange_die, bound_attr, bound,
16841 dw_scalar_form_constant
16842 | dw_scalar_form_exprloc
16843 | dw_scalar_form_reference,
16844 context);
16845 return;
16849 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16850 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16851 Note that the block of subscript information for an array type also
16852 includes information about the element type of the given array type.
16854 This function reuses previously set type and bound information if
16855 available. */
16857 static void
16858 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16860 unsigned dimension_number;
16861 tree lower, upper;
16862 dw_die_ref child = type_die->die_child;
16864 for (dimension_number = 0;
16865 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16866 type = TREE_TYPE (type), dimension_number++)
16868 tree domain = TYPE_DOMAIN (type);
16870 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16871 break;
16873 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16874 and (in GNU C only) variable bounds. Handle all three forms
16875 here. */
16877 /* Find and reuse a previously generated DW_TAG_subrange_type if
16878 available.
16880 For multi-dimensional arrays, as we iterate through the
16881 various dimensions in the enclosing for loop above, we also
16882 iterate through the DIE children and pick at each
16883 DW_TAG_subrange_type previously generated (if available).
16884 Each child DW_TAG_subrange_type DIE describes the range of
16885 the current dimension. At this point we should have as many
16886 DW_TAG_subrange_type's as we have dimensions in the
16887 array. */
16888 dw_die_ref subrange_die = NULL;
16889 if (child)
16890 while (1)
16892 child = child->die_sib;
16893 if (child->die_tag == DW_TAG_subrange_type)
16894 subrange_die = child;
16895 if (child == type_die->die_child)
16897 /* If we wrapped around, stop looking next time. */
16898 child = NULL;
16899 break;
16901 if (child->die_tag == DW_TAG_subrange_type)
16902 break;
16904 if (!subrange_die)
16905 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16907 if (domain)
16909 /* We have an array type with specified bounds. */
16910 lower = TYPE_MIN_VALUE (domain);
16911 upper = TYPE_MAX_VALUE (domain);
16913 /* Define the index type. */
16914 if (TREE_TYPE (domain)
16915 && !get_AT (subrange_die, DW_AT_type))
16917 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16918 TREE_TYPE field. We can't emit debug info for this
16919 because it is an unnamed integral type. */
16920 if (TREE_CODE (domain) == INTEGER_TYPE
16921 && TYPE_NAME (domain) == NULL_TREE
16922 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16923 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16925 else
16926 add_type_attribute (subrange_die, TREE_TYPE (domain),
16927 TYPE_UNQUALIFIED, type_die);
16930 /* ??? If upper is NULL, the array has unspecified length,
16931 but it does have a lower bound. This happens with Fortran
16932 dimension arr(N:*)
16933 Since the debugger is definitely going to need to know N
16934 to produce useful results, go ahead and output the lower
16935 bound solo, and hope the debugger can cope. */
16937 if (!get_AT (subrange_die, DW_AT_lower_bound))
16938 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16939 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16940 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16943 /* Otherwise we have an array type with an unspecified length. The
16944 DWARF-2 spec does not say how to handle this; let's just leave out the
16945 bounds. */
16949 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16951 static void
16952 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16954 dw_die_ref decl_die;
16955 HOST_WIDE_INT size;
16957 switch (TREE_CODE (tree_node))
16959 case ERROR_MARK:
16960 size = 0;
16961 break;
16962 case ENUMERAL_TYPE:
16963 case RECORD_TYPE:
16964 case UNION_TYPE:
16965 case QUAL_UNION_TYPE:
16966 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16967 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16969 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16970 return;
16972 size = int_size_in_bytes (tree_node);
16973 break;
16974 case FIELD_DECL:
16975 /* For a data member of a struct or union, the DW_AT_byte_size is
16976 generally given as the number of bytes normally allocated for an
16977 object of the *declared* type of the member itself. This is true
16978 even for bit-fields. */
16979 size = int_size_in_bytes (field_type (tree_node));
16980 break;
16981 default:
16982 gcc_unreachable ();
16985 /* Note that `size' might be -1 when we get to this point. If it is, that
16986 indicates that the byte size of the entity in question is variable. We
16987 have no good way of expressing this fact in Dwarf at the present time,
16988 when location description was not used by the caller code instead. */
16989 if (size >= 0)
16990 add_AT_unsigned (die, DW_AT_byte_size, size);
16993 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16994 which specifies the distance in bits from the highest order bit of the
16995 "containing object" for the bit-field to the highest order bit of the
16996 bit-field itself.
16998 For any given bit-field, the "containing object" is a hypothetical object
16999 (of some integral or enum type) within which the given bit-field lives. The
17000 type of this hypothetical "containing object" is always the same as the
17001 declared type of the individual bit-field itself. The determination of the
17002 exact location of the "containing object" for a bit-field is rather
17003 complicated. It's handled by the `field_byte_offset' function (above).
17005 Note that it is the size (in bytes) of the hypothetical "containing object"
17006 which will be given in the DW_AT_byte_size attribute for this bit-field.
17007 (See `byte_size_attribute' above). */
17009 static inline void
17010 add_bit_offset_attribute (dw_die_ref die, tree decl)
17012 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17013 tree type = DECL_BIT_FIELD_TYPE (decl);
17014 HOST_WIDE_INT bitpos_int;
17015 HOST_WIDE_INT highest_order_object_bit_offset;
17016 HOST_WIDE_INT highest_order_field_bit_offset;
17017 HOST_WIDE_INT bit_offset;
17019 /* Must be a field and a bit field. */
17020 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17022 /* We can't yet handle bit-fields whose offsets are variable, so if we
17023 encounter such things, just return without generating any attribute
17024 whatsoever. Likewise for variable or too large size. */
17025 if (! tree_fits_shwi_p (bit_position (decl))
17026 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17027 return;
17029 bitpos_int = int_bit_position (decl);
17031 /* Note that the bit offset is always the distance (in bits) from the
17032 highest-order bit of the "containing object" to the highest-order bit of
17033 the bit-field itself. Since the "high-order end" of any object or field
17034 is different on big-endian and little-endian machines, the computation
17035 below must take account of these differences. */
17036 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17037 highest_order_field_bit_offset = bitpos_int;
17039 if (! BYTES_BIG_ENDIAN)
17041 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17042 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17045 bit_offset
17046 = (! BYTES_BIG_ENDIAN
17047 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17048 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17050 if (bit_offset < 0)
17051 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17052 else
17053 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17056 /* For a FIELD_DECL node which represents a bit field, output an attribute
17057 which specifies the length in bits of the given field. */
17059 static inline void
17060 add_bit_size_attribute (dw_die_ref die, tree decl)
17062 /* Must be a field and a bit field. */
17063 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17064 && DECL_BIT_FIELD_TYPE (decl));
17066 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17067 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17070 /* If the compiled language is ANSI C, then add a 'prototyped'
17071 attribute, if arg types are given for the parameters of a function. */
17073 static inline void
17074 add_prototyped_attribute (dw_die_ref die, tree func_type)
17076 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17078 case DW_LANG_C:
17079 case DW_LANG_C89:
17080 case DW_LANG_C99:
17081 case DW_LANG_C11:
17082 case DW_LANG_ObjC:
17083 if (prototype_p (func_type))
17084 add_AT_flag (die, DW_AT_prototyped, 1);
17085 break;
17086 default:
17087 break;
17091 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17092 by looking in either the type declaration or object declaration
17093 equate table. */
17095 static inline dw_die_ref
17096 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17098 dw_die_ref origin_die = NULL;
17100 if (TREE_CODE (origin) != FUNCTION_DECL)
17102 /* We may have gotten separated from the block for the inlined
17103 function, if we're in an exception handler or some such; make
17104 sure that the abstract function has been written out.
17106 Doing this for nested functions is wrong, however; functions are
17107 distinct units, and our context might not even be inline. */
17108 tree fn = origin;
17110 if (TYPE_P (fn))
17111 fn = TYPE_STUB_DECL (fn);
17113 fn = decl_function_context (fn);
17114 if (fn)
17115 dwarf2out_abstract_function (fn);
17118 if (DECL_P (origin))
17119 origin_die = lookup_decl_die (origin);
17120 else if (TYPE_P (origin))
17121 origin_die = lookup_type_die (origin);
17123 /* XXX: Functions that are never lowered don't always have correct block
17124 trees (in the case of java, they simply have no block tree, in some other
17125 languages). For these functions, there is nothing we can really do to
17126 output correct debug info for inlined functions in all cases. Rather
17127 than die, we'll just produce deficient debug info now, in that we will
17128 have variables without a proper abstract origin. In the future, when all
17129 functions are lowered, we should re-add a gcc_assert (origin_die)
17130 here. */
17132 if (origin_die)
17133 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17134 return origin_die;
17137 /* We do not currently support the pure_virtual attribute. */
17139 static inline void
17140 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17142 if (DECL_VINDEX (func_decl))
17144 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17146 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17147 add_AT_loc (die, DW_AT_vtable_elem_location,
17148 new_loc_descr (DW_OP_constu,
17149 tree_to_shwi (DECL_VINDEX (func_decl)),
17150 0));
17152 /* GNU extension: Record what type this method came from originally. */
17153 if (debug_info_level > DINFO_LEVEL_TERSE
17154 && DECL_CONTEXT (func_decl))
17155 add_AT_die_ref (die, DW_AT_containing_type,
17156 lookup_type_die (DECL_CONTEXT (func_decl)));
17160 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17161 given decl. This used to be a vendor extension until after DWARF 4
17162 standardized it. */
17164 static void
17165 add_linkage_attr (dw_die_ref die, tree decl)
17167 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17169 /* Mimic what assemble_name_raw does with a leading '*'. */
17170 if (name[0] == '*')
17171 name = &name[1];
17173 if (dwarf_version >= 4)
17174 add_AT_string (die, DW_AT_linkage_name, name);
17175 else
17176 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17179 /* Add source coordinate attributes for the given decl. */
17181 static void
17182 add_src_coords_attributes (dw_die_ref die, tree decl)
17184 expanded_location s;
17186 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17187 return;
17188 s = expand_location (DECL_SOURCE_LOCATION (decl));
17189 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17190 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17193 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17195 static void
17196 add_linkage_name (dw_die_ref die, tree decl)
17198 if (debug_info_level > DINFO_LEVEL_NONE
17199 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17200 && TREE_PUBLIC (decl)
17201 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17202 && die->die_tag != DW_TAG_member)
17204 /* Defer until we have an assembler name set. */
17205 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17207 limbo_die_node *asm_name;
17209 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17210 asm_name->die = die;
17211 asm_name->created_for = decl;
17212 asm_name->next = deferred_asm_name;
17213 deferred_asm_name = asm_name;
17215 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17216 add_linkage_attr (die, decl);
17220 /* Add a DW_AT_name attribute and source coordinate attribute for the
17221 given decl, but only if it actually has a name. */
17223 static void
17224 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17226 tree decl_name;
17228 decl_name = DECL_NAME (decl);
17229 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17231 const char *name = dwarf2_name (decl, 0);
17232 if (name)
17233 add_name_attribute (die, name);
17234 if (! DECL_ARTIFICIAL (decl))
17235 add_src_coords_attributes (die, decl);
17237 add_linkage_name (die, decl);
17240 #ifdef VMS_DEBUGGING_INFO
17241 /* Get the function's name, as described by its RTL. This may be different
17242 from the DECL_NAME name used in the source file. */
17243 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17245 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17246 XEXP (DECL_RTL (decl), 0), false);
17247 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17249 #endif /* VMS_DEBUGGING_INFO */
17252 #ifdef VMS_DEBUGGING_INFO
17253 /* Output the debug main pointer die for VMS */
17255 void
17256 dwarf2out_vms_debug_main_pointer (void)
17258 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17259 dw_die_ref die;
17261 /* Allocate the VMS debug main subprogram die. */
17262 die = ggc_cleared_alloc<die_node> ();
17263 die->die_tag = DW_TAG_subprogram;
17264 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17265 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17266 current_function_funcdef_no);
17267 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17269 /* Make it the first child of comp_unit_die (). */
17270 die->die_parent = comp_unit_die ();
17271 if (comp_unit_die ()->die_child)
17273 die->die_sib = comp_unit_die ()->die_child->die_sib;
17274 comp_unit_die ()->die_child->die_sib = die;
17276 else
17278 die->die_sib = die;
17279 comp_unit_die ()->die_child = die;
17282 #endif /* VMS_DEBUGGING_INFO */
17284 /* Push a new declaration scope. */
17286 static void
17287 push_decl_scope (tree scope)
17289 vec_safe_push (decl_scope_table, scope);
17292 /* Pop a declaration scope. */
17294 static inline void
17295 pop_decl_scope (void)
17297 decl_scope_table->pop ();
17300 /* walk_tree helper function for uses_local_type, below. */
17302 static tree
17303 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17305 if (!TYPE_P (*tp))
17306 *walk_subtrees = 0;
17307 else
17309 tree name = TYPE_NAME (*tp);
17310 if (name && DECL_P (name) && decl_function_context (name))
17311 return *tp;
17313 return NULL_TREE;
17316 /* If TYPE involves a function-local type (including a local typedef to a
17317 non-local type), returns that type; otherwise returns NULL_TREE. */
17319 static tree
17320 uses_local_type (tree type)
17322 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17323 return used;
17326 /* Return the DIE for the scope that immediately contains this type.
17327 Non-named types that do not involve a function-local type get global
17328 scope. Named types nested in namespaces or other types get their
17329 containing scope. All other types (i.e. function-local named types) get
17330 the current active scope. */
17332 static dw_die_ref
17333 scope_die_for (tree t, dw_die_ref context_die)
17335 dw_die_ref scope_die = NULL;
17336 tree containing_scope;
17338 /* Non-types always go in the current scope. */
17339 gcc_assert (TYPE_P (t));
17341 /* Use the scope of the typedef, rather than the scope of the type
17342 it refers to. */
17343 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17344 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17345 else
17346 containing_scope = TYPE_CONTEXT (t);
17348 /* Use the containing namespace if there is one. */
17349 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17351 if (context_die == lookup_decl_die (containing_scope))
17352 /* OK */;
17353 else if (debug_info_level > DINFO_LEVEL_TERSE)
17354 context_die = get_context_die (containing_scope);
17355 else
17356 containing_scope = NULL_TREE;
17359 /* Ignore function type "scopes" from the C frontend. They mean that
17360 a tagged type is local to a parmlist of a function declarator, but
17361 that isn't useful to DWARF. */
17362 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17363 containing_scope = NULL_TREE;
17365 if (SCOPE_FILE_SCOPE_P (containing_scope))
17367 /* If T uses a local type keep it local as well, to avoid references
17368 to function-local DIEs from outside the function. */
17369 if (current_function_decl && uses_local_type (t))
17370 scope_die = context_die;
17371 else
17372 scope_die = comp_unit_die ();
17374 else if (TYPE_P (containing_scope))
17376 /* For types, we can just look up the appropriate DIE. */
17377 if (debug_info_level > DINFO_LEVEL_TERSE)
17378 scope_die = get_context_die (containing_scope);
17379 else
17381 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17382 if (scope_die == NULL)
17383 scope_die = comp_unit_die ();
17386 else
17387 scope_die = context_die;
17389 return scope_die;
17392 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17394 static inline int
17395 local_scope_p (dw_die_ref context_die)
17397 for (; context_die; context_die = context_die->die_parent)
17398 if (context_die->die_tag == DW_TAG_inlined_subroutine
17399 || context_die->die_tag == DW_TAG_subprogram)
17400 return 1;
17402 return 0;
17405 /* Returns nonzero if CONTEXT_DIE is a class. */
17407 static inline int
17408 class_scope_p (dw_die_ref context_die)
17410 return (context_die
17411 && (context_die->die_tag == DW_TAG_structure_type
17412 || context_die->die_tag == DW_TAG_class_type
17413 || context_die->die_tag == DW_TAG_interface_type
17414 || context_die->die_tag == DW_TAG_union_type));
17417 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17418 whether or not to treat a DIE in this context as a declaration. */
17420 static inline int
17421 class_or_namespace_scope_p (dw_die_ref context_die)
17423 return (class_scope_p (context_die)
17424 || (context_die && context_die->die_tag == DW_TAG_namespace));
17427 /* Many forms of DIEs require a "type description" attribute. This
17428 routine locates the proper "type descriptor" die for the type given
17429 by 'type' plus any additional qualifiers given by 'cv_quals', and
17430 adds a DW_AT_type attribute below the given die. */
17432 static void
17433 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17434 dw_die_ref context_die)
17436 enum tree_code code = TREE_CODE (type);
17437 dw_die_ref type_die = NULL;
17439 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17440 or fixed-point type, use the inner type. This is because we have no
17441 support for unnamed types in base_type_die. This can happen if this is
17442 an Ada subrange type. Correct solution is emit a subrange type die. */
17443 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17444 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17445 type = TREE_TYPE (type), code = TREE_CODE (type);
17447 if (code == ERROR_MARK
17448 /* Handle a special case. For functions whose return type is void, we
17449 generate *no* type attribute. (Note that no object may have type
17450 `void', so this only applies to function return types). */
17451 || code == VOID_TYPE)
17452 return;
17454 type_die = modified_type_die (type,
17455 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17456 context_die);
17458 if (type_die != NULL)
17459 add_AT_die_ref (object_die, DW_AT_type, type_die);
17462 /* Given an object die, add the calling convention attribute for the
17463 function call type. */
17464 static void
17465 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17467 enum dwarf_calling_convention value = DW_CC_normal;
17469 value = ((enum dwarf_calling_convention)
17470 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17472 if (is_fortran ()
17473 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17475 /* DWARF 2 doesn't provide a way to identify a program's source-level
17476 entry point. DW_AT_calling_convention attributes are only meant
17477 to describe functions' calling conventions. However, lacking a
17478 better way to signal the Fortran main program, we used this for
17479 a long time, following existing custom. Now, DWARF 4 has
17480 DW_AT_main_subprogram, which we add below, but some tools still
17481 rely on the old way, which we thus keep. */
17482 value = DW_CC_program;
17484 if (dwarf_version >= 4 || !dwarf_strict)
17485 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17488 /* Only add the attribute if the backend requests it, and
17489 is not DW_CC_normal. */
17490 if (value && (value != DW_CC_normal))
17491 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17494 /* Given a tree pointer to a struct, class, union, or enum type node, return
17495 a pointer to the (string) tag name for the given type, or zero if the type
17496 was declared without a tag. */
17498 static const char *
17499 type_tag (const_tree type)
17501 const char *name = 0;
17503 if (TYPE_NAME (type) != 0)
17505 tree t = 0;
17507 /* Find the IDENTIFIER_NODE for the type name. */
17508 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17509 && !TYPE_NAMELESS (type))
17510 t = TYPE_NAME (type);
17512 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17513 a TYPE_DECL node, regardless of whether or not a `typedef' was
17514 involved. */
17515 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17516 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17518 /* We want to be extra verbose. Don't call dwarf_name if
17519 DECL_NAME isn't set. The default hook for decl_printable_name
17520 doesn't like that, and in this context it's correct to return
17521 0, instead of "<anonymous>" or the like. */
17522 if (DECL_NAME (TYPE_NAME (type))
17523 && !DECL_NAMELESS (TYPE_NAME (type)))
17524 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17527 /* Now get the name as a string, or invent one. */
17528 if (!name && t != 0)
17529 name = IDENTIFIER_POINTER (t);
17532 return (name == 0 || *name == '\0') ? 0 : name;
17535 /* Return the type associated with a data member, make a special check
17536 for bit field types. */
17538 static inline tree
17539 member_declared_type (const_tree member)
17541 return (DECL_BIT_FIELD_TYPE (member)
17542 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17545 /* Get the decl's label, as described by its RTL. This may be different
17546 from the DECL_NAME name used in the source file. */
17548 #if 0
17549 static const char *
17550 decl_start_label (tree decl)
17552 rtx x;
17553 const char *fnname;
17555 x = DECL_RTL (decl);
17556 gcc_assert (MEM_P (x));
17558 x = XEXP (x, 0);
17559 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17561 fnname = XSTR (x, 0);
17562 return fnname;
17564 #endif
17566 /* For variable-length arrays that have been previously generated, but
17567 may be incomplete due to missing subscript info, fill the subscript
17568 info. Return TRUE if this is one of those cases. */
17569 static bool
17570 fill_variable_array_bounds (tree type)
17572 if (TREE_ASM_WRITTEN (type)
17573 && TREE_CODE (type) == ARRAY_TYPE
17574 && variably_modified_type_p (type, NULL))
17576 dw_die_ref array_die = lookup_type_die (type);
17577 if (!array_die)
17578 return false;
17579 add_subscript_info (array_die, type, !is_ada ());
17580 return true;
17582 return false;
17585 /* These routines generate the internal representation of the DIE's for
17586 the compilation unit. Debugging information is collected by walking
17587 the declaration trees passed in from dwarf2out_decl(). */
17589 static void
17590 gen_array_type_die (tree type, dw_die_ref context_die)
17592 dw_die_ref array_die;
17594 /* GNU compilers represent multidimensional array types as sequences of one
17595 dimensional array types whose element types are themselves array types.
17596 We sometimes squish that down to a single array_type DIE with multiple
17597 subscripts in the Dwarf debugging info. The draft Dwarf specification
17598 say that we are allowed to do this kind of compression in C, because
17599 there is no difference between an array of arrays and a multidimensional
17600 array. We don't do this for Ada to remain as close as possible to the
17601 actual representation, which is especially important against the language
17602 flexibilty wrt arrays of variable size. */
17604 bool collapse_nested_arrays = !is_ada ();
17606 if (fill_variable_array_bounds (type))
17607 return;
17609 dw_die_ref scope_die = scope_die_for (type, context_die);
17610 tree element_type;
17612 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17613 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17614 if (TYPE_STRING_FLAG (type)
17615 && TREE_CODE (type) == ARRAY_TYPE
17616 && is_fortran ()
17617 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17619 HOST_WIDE_INT size;
17621 array_die = new_die (DW_TAG_string_type, scope_die, type);
17622 add_name_attribute (array_die, type_tag (type));
17623 equate_type_number_to_die (type, array_die);
17624 size = int_size_in_bytes (type);
17625 if (size >= 0)
17626 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17627 else if (TYPE_DOMAIN (type) != NULL_TREE
17628 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17629 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17631 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17632 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17634 size = int_size_in_bytes (TREE_TYPE (szdecl));
17635 if (loc && size > 0)
17637 add_AT_location_description (array_die, DW_AT_string_length, loc);
17638 if (size != DWARF2_ADDR_SIZE)
17639 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17642 return;
17645 array_die = new_die (DW_TAG_array_type, scope_die, type);
17646 add_name_attribute (array_die, type_tag (type));
17647 equate_type_number_to_die (type, array_die);
17649 if (TREE_CODE (type) == VECTOR_TYPE)
17650 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17652 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17653 if (is_fortran ()
17654 && TREE_CODE (type) == ARRAY_TYPE
17655 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17656 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17657 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17659 #if 0
17660 /* We default the array ordering. SDB will probably do
17661 the right things even if DW_AT_ordering is not present. It's not even
17662 an issue until we start to get into multidimensional arrays anyway. If
17663 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17664 then we'll have to put the DW_AT_ordering attribute back in. (But if
17665 and when we find out that we need to put these in, we will only do so
17666 for multidimensional arrays. */
17667 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17668 #endif
17670 if (TREE_CODE (type) == VECTOR_TYPE)
17672 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17673 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17674 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17675 add_bound_info (subrange_die, DW_AT_upper_bound,
17676 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17678 else
17679 add_subscript_info (array_die, type, collapse_nested_arrays);
17681 /* Add representation of the type of the elements of this array type and
17682 emit the corresponding DIE if we haven't done it already. */
17683 element_type = TREE_TYPE (type);
17684 if (collapse_nested_arrays)
17685 while (TREE_CODE (element_type) == ARRAY_TYPE)
17687 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17688 break;
17689 element_type = TREE_TYPE (element_type);
17692 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17694 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17695 if (TYPE_ARTIFICIAL (type))
17696 add_AT_flag (array_die, DW_AT_artificial, 1);
17698 if (get_AT (array_die, DW_AT_name))
17699 add_pubtype (type, array_die);
17702 /* This routine generates DIE for array with hidden descriptor, details
17703 are filled into *info by a langhook. */
17705 static void
17706 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17707 dw_die_ref context_die)
17709 const dw_die_ref scope_die = scope_die_for (type, context_die);
17710 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17711 const struct loc_descr_context context = { type, info->base_decl };
17712 int dim;
17714 add_name_attribute (array_die, type_tag (type));
17715 equate_type_number_to_die (type, array_die);
17717 if (info->ndimensions > 1)
17718 switch (info->ordering)
17720 case array_descr_ordering_row_major:
17721 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17722 break;
17723 case array_descr_ordering_column_major:
17724 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17725 break;
17726 default:
17727 break;
17730 if (dwarf_version >= 3 || !dwarf_strict)
17732 if (info->data_location)
17733 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17734 dw_scalar_form_exprloc, &context);
17735 if (info->associated)
17736 add_scalar_info (array_die, DW_AT_associated, info->associated,
17737 dw_scalar_form_constant
17738 | dw_scalar_form_exprloc
17739 | dw_scalar_form_reference, &context);
17740 if (info->allocated)
17741 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17742 dw_scalar_form_constant
17743 | dw_scalar_form_exprloc
17744 | dw_scalar_form_reference, &context);
17747 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17749 for (dim = 0; dim < info->ndimensions; dim++)
17751 dw_die_ref subrange_die
17752 = new_die (DW_TAG_subrange_type, array_die, NULL);
17754 if (info->dimen[dim].bounds_type)
17755 add_type_attribute (subrange_die,
17756 info->dimen[dim].bounds_type, 0,
17757 context_die);
17758 if (info->dimen[dim].lower_bound)
17759 add_bound_info (subrange_die, DW_AT_lower_bound,
17760 info->dimen[dim].lower_bound, &context);
17761 if (info->dimen[dim].upper_bound)
17762 add_bound_info (subrange_die, DW_AT_upper_bound,
17763 info->dimen[dim].upper_bound, &context);
17764 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17765 add_scalar_info (subrange_die, DW_AT_byte_stride,
17766 info->dimen[dim].stride,
17767 dw_scalar_form_constant
17768 | dw_scalar_form_exprloc
17769 | dw_scalar_form_reference,
17770 &context);
17773 gen_type_die (info->element_type, context_die);
17774 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17775 context_die);
17777 if (get_AT (array_die, DW_AT_name))
17778 add_pubtype (type, array_die);
17781 #if 0
17782 static void
17783 gen_entry_point_die (tree decl, dw_die_ref context_die)
17785 tree origin = decl_ultimate_origin (decl);
17786 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17788 if (origin != NULL)
17789 add_abstract_origin_attribute (decl_die, origin);
17790 else
17792 add_name_and_src_coords_attributes (decl_die, decl);
17793 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17794 TYPE_UNQUALIFIED, context_die);
17797 if (DECL_ABSTRACT_P (decl))
17798 equate_decl_number_to_die (decl, decl_die);
17799 else
17800 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17802 #endif
17804 /* Walk through the list of incomplete types again, trying once more to
17805 emit full debugging info for them. */
17807 static void
17808 retry_incomplete_types (void)
17810 int i;
17812 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17813 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17814 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17817 /* Determine what tag to use for a record type. */
17819 static enum dwarf_tag
17820 record_type_tag (tree type)
17822 if (! lang_hooks.types.classify_record)
17823 return DW_TAG_structure_type;
17825 switch (lang_hooks.types.classify_record (type))
17827 case RECORD_IS_STRUCT:
17828 return DW_TAG_structure_type;
17830 case RECORD_IS_CLASS:
17831 return DW_TAG_class_type;
17833 case RECORD_IS_INTERFACE:
17834 if (dwarf_version >= 3 || !dwarf_strict)
17835 return DW_TAG_interface_type;
17836 return DW_TAG_structure_type;
17838 default:
17839 gcc_unreachable ();
17843 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17844 include all of the information about the enumeration values also. Each
17845 enumerated type name/value is listed as a child of the enumerated type
17846 DIE. */
17848 static dw_die_ref
17849 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17851 dw_die_ref type_die = lookup_type_die (type);
17853 if (type_die == NULL)
17855 type_die = new_die (DW_TAG_enumeration_type,
17856 scope_die_for (type, context_die), type);
17857 equate_type_number_to_die (type, type_die);
17858 add_name_attribute (type_die, type_tag (type));
17859 if (dwarf_version >= 4 || !dwarf_strict)
17861 if (ENUM_IS_SCOPED (type))
17862 add_AT_flag (type_die, DW_AT_enum_class, 1);
17863 if (ENUM_IS_OPAQUE (type))
17864 add_AT_flag (type_die, DW_AT_declaration, 1);
17867 else if (! TYPE_SIZE (type))
17868 return type_die;
17869 else
17870 remove_AT (type_die, DW_AT_declaration);
17872 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17873 given enum type is incomplete, do not generate the DW_AT_byte_size
17874 attribute or the DW_AT_element_list attribute. */
17875 if (TYPE_SIZE (type))
17877 tree link;
17879 TREE_ASM_WRITTEN (type) = 1;
17880 add_byte_size_attribute (type_die, type);
17881 if (dwarf_version >= 3 || !dwarf_strict)
17883 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17884 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17885 context_die);
17887 if (TYPE_STUB_DECL (type) != NULL_TREE)
17889 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17890 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17893 /* If the first reference to this type was as the return type of an
17894 inline function, then it may not have a parent. Fix this now. */
17895 if (type_die->die_parent == NULL)
17896 add_child_die (scope_die_for (type, context_die), type_die);
17898 for (link = TYPE_VALUES (type);
17899 link != NULL; link = TREE_CHAIN (link))
17901 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17902 tree value = TREE_VALUE (link);
17904 add_name_attribute (enum_die,
17905 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17907 if (TREE_CODE (value) == CONST_DECL)
17908 value = DECL_INITIAL (value);
17910 if (simple_type_size_in_bits (TREE_TYPE (value))
17911 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17913 /* For constant forms created by add_AT_unsigned DWARF
17914 consumers (GDB, elfutils, etc.) always zero extend
17915 the value. Only when the actual value is negative
17916 do we need to use add_AT_int to generate a constant
17917 form that can represent negative values. */
17918 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17919 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17920 add_AT_unsigned (enum_die, DW_AT_const_value,
17921 (unsigned HOST_WIDE_INT) val);
17922 else
17923 add_AT_int (enum_die, DW_AT_const_value, val);
17925 else
17926 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17927 that here. TODO: This should be re-worked to use correct
17928 signed/unsigned double tags for all cases. */
17929 add_AT_wide (enum_die, DW_AT_const_value, value);
17932 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17933 if (TYPE_ARTIFICIAL (type))
17934 add_AT_flag (type_die, DW_AT_artificial, 1);
17936 else
17937 add_AT_flag (type_die, DW_AT_declaration, 1);
17939 add_pubtype (type, type_die);
17941 return type_die;
17944 /* Generate a DIE to represent either a real live formal parameter decl or to
17945 represent just the type of some formal parameter position in some function
17946 type.
17948 Note that this routine is a bit unusual because its argument may be a
17949 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17950 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17951 node. If it's the former then this function is being called to output a
17952 DIE to represent a formal parameter object (or some inlining thereof). If
17953 it's the latter, then this function is only being called to output a
17954 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17955 argument type of some subprogram type.
17956 If EMIT_NAME_P is true, name and source coordinate attributes
17957 are emitted. */
17959 static dw_die_ref
17960 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17961 dw_die_ref context_die)
17963 tree node_or_origin = node ? node : origin;
17964 tree ultimate_origin;
17965 dw_die_ref parm_die = NULL;
17967 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
17969 parm_die = lookup_decl_die (node);
17971 /* If the contexts differ, we may not be talking about the same
17972 thing. */
17973 if (parm_die && parm_die->die_parent != context_die)
17975 if (!DECL_ABSTRACT_P (node))
17977 /* This can happen when creating an inlined instance, in
17978 which case we need to create a new DIE that will get
17979 annotated with DW_AT_abstract_origin. */
17980 parm_die = NULL;
17982 else
17984 /* FIXME: Reuse DIE even with a differing context.
17986 This happens when called through
17987 dwarf2out_abstract_function for formal parameter
17988 packs. The issue is that we're calling
17989 dwarf2out_abstract_function to build debug info for
17990 the abstract instance of a function for which we have
17991 already generated a DIE in
17992 dwarf2out_early_global_decl.
17994 Once we remove dwarf2out_abstract_function, this
17995 gcc_assert should be a gcc_unreachable. */
17996 gcc_assert (parm_die->die_parent->die_tag
17997 == DW_TAG_GNU_formal_parameter_pack);
18001 if (parm_die && parm_die->die_parent == NULL)
18003 /* Check that parm_die already has the right attributes that
18004 we would have added below. If any attributes are
18005 missing, fall through to add them. */
18006 if (! DECL_ABSTRACT_P (node_or_origin)
18007 && !get_AT (parm_die, DW_AT_location)
18008 && !get_AT (parm_die, DW_AT_const_value))
18009 /* We are missing location info, and are about to add it. */
18011 else
18013 add_child_die (context_die, parm_die);
18014 return parm_die;
18019 /* If we have a previously generated DIE, use it, unless this is an
18020 concrete instance (origin != NULL), in which case we need a new
18021 DIE with a corresponding DW_AT_abstract_origin. */
18022 bool reusing_die;
18023 if (parm_die && origin == NULL)
18024 reusing_die = true;
18025 else
18027 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18028 reusing_die = false;
18031 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18033 case tcc_declaration:
18034 ultimate_origin = decl_ultimate_origin (node_or_origin);
18035 if (node || ultimate_origin)
18036 origin = ultimate_origin;
18038 if (reusing_die)
18039 goto add_location;
18041 if (origin != NULL)
18042 add_abstract_origin_attribute (parm_die, origin);
18043 else if (emit_name_p)
18044 add_name_and_src_coords_attributes (parm_die, node);
18045 if (origin == NULL
18046 || (! DECL_ABSTRACT_P (node_or_origin)
18047 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18048 decl_function_context
18049 (node_or_origin))))
18051 tree type = TREE_TYPE (node_or_origin);
18052 if (decl_by_reference_p (node_or_origin))
18053 add_type_attribute (parm_die, TREE_TYPE (type),
18054 TYPE_UNQUALIFIED, context_die);
18055 else
18056 add_type_attribute (parm_die, type,
18057 decl_quals (node_or_origin),
18058 context_die);
18060 add_location:
18061 if (origin == NULL && DECL_ARTIFICIAL (node))
18062 add_AT_flag (parm_die, DW_AT_artificial, 1);
18064 if (node && node != origin)
18065 equate_decl_number_to_die (node, parm_die);
18066 if (! DECL_ABSTRACT_P (node_or_origin))
18067 add_location_or_const_value_attribute (parm_die, node_or_origin,
18068 node == NULL, DW_AT_location);
18070 break;
18072 case tcc_type:
18073 /* We were called with some kind of a ..._TYPE node. */
18074 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18075 context_die);
18076 break;
18078 default:
18079 gcc_unreachable ();
18082 return parm_die;
18085 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18086 children DW_TAG_formal_parameter DIEs representing the arguments of the
18087 parameter pack.
18089 PARM_PACK must be a function parameter pack.
18090 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18091 must point to the subsequent arguments of the function PACK_ARG belongs to.
18092 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18093 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18094 following the last one for which a DIE was generated. */
18096 static dw_die_ref
18097 gen_formal_parameter_pack_die (tree parm_pack,
18098 tree pack_arg,
18099 dw_die_ref subr_die,
18100 tree *next_arg)
18102 tree arg;
18103 dw_die_ref parm_pack_die;
18105 gcc_assert (parm_pack
18106 && lang_hooks.function_parameter_pack_p (parm_pack)
18107 && subr_die);
18109 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18110 add_src_coords_attributes (parm_pack_die, parm_pack);
18112 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18114 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18115 parm_pack))
18116 break;
18117 gen_formal_parameter_die (arg, NULL,
18118 false /* Don't emit name attribute. */,
18119 parm_pack_die);
18121 if (next_arg)
18122 *next_arg = arg;
18123 return parm_pack_die;
18126 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18127 at the end of an (ANSI prototyped) formal parameters list. */
18129 static void
18130 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18132 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18135 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18136 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18137 parameters as specified in some function type specification (except for
18138 those which appear as part of a function *definition*). */
18140 static void
18141 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18143 tree link;
18144 tree formal_type = NULL;
18145 tree first_parm_type;
18146 tree arg;
18148 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18150 arg = DECL_ARGUMENTS (function_or_method_type);
18151 function_or_method_type = TREE_TYPE (function_or_method_type);
18153 else
18154 arg = NULL_TREE;
18156 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18158 /* Make our first pass over the list of formal parameter types and output a
18159 DW_TAG_formal_parameter DIE for each one. */
18160 for (link = first_parm_type; link; )
18162 dw_die_ref parm_die;
18164 formal_type = TREE_VALUE (link);
18165 if (formal_type == void_type_node)
18166 break;
18168 /* Output a (nameless) DIE to represent the formal parameter itself. */
18169 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18171 parm_die = gen_formal_parameter_die (formal_type, NULL,
18172 true /* Emit name attribute. */,
18173 context_die);
18174 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18175 && link == first_parm_type)
18177 add_AT_flag (parm_die, DW_AT_artificial, 1);
18178 if (dwarf_version >= 3 || !dwarf_strict)
18179 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18181 else if (arg && DECL_ARTIFICIAL (arg))
18182 add_AT_flag (parm_die, DW_AT_artificial, 1);
18185 link = TREE_CHAIN (link);
18186 if (arg)
18187 arg = DECL_CHAIN (arg);
18190 /* If this function type has an ellipsis, add a
18191 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18192 if (formal_type != void_type_node)
18193 gen_unspecified_parameters_die (function_or_method_type, context_die);
18195 /* Make our second (and final) pass over the list of formal parameter types
18196 and output DIEs to represent those types (as necessary). */
18197 for (link = TYPE_ARG_TYPES (function_or_method_type);
18198 link && TREE_VALUE (link);
18199 link = TREE_CHAIN (link))
18200 gen_type_die (TREE_VALUE (link), context_die);
18203 /* We want to generate the DIE for TYPE so that we can generate the
18204 die for MEMBER, which has been defined; we will need to refer back
18205 to the member declaration nested within TYPE. If we're trying to
18206 generate minimal debug info for TYPE, processing TYPE won't do the
18207 trick; we need to attach the member declaration by hand. */
18209 static void
18210 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18212 gen_type_die (type, context_die);
18214 /* If we're trying to avoid duplicate debug info, we may not have
18215 emitted the member decl for this function. Emit it now. */
18216 if (TYPE_STUB_DECL (type)
18217 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18218 && ! lookup_decl_die (member))
18220 dw_die_ref type_die;
18221 gcc_assert (!decl_ultimate_origin (member));
18223 push_decl_scope (type);
18224 type_die = lookup_type_die_strip_naming_typedef (type);
18225 if (TREE_CODE (member) == FUNCTION_DECL)
18226 gen_subprogram_die (member, type_die);
18227 else if (TREE_CODE (member) == FIELD_DECL)
18229 /* Ignore the nameless fields that are used to skip bits but handle
18230 C++ anonymous unions and structs. */
18231 if (DECL_NAME (member) != NULL_TREE
18232 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18233 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18235 gen_type_die (member_declared_type (member), type_die);
18236 gen_field_die (member, type_die);
18239 else
18240 gen_variable_die (member, NULL_TREE, type_die);
18242 pop_decl_scope ();
18246 /* Forward declare these functions, because they are mutually recursive
18247 with their set_block_* pairing functions. */
18248 static void set_decl_origin_self (tree);
18249 static void set_decl_abstract_flags (tree, vec<tree> &);
18251 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18252 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18253 that it points to the node itself, thus indicating that the node is its
18254 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18255 the given node is NULL, recursively descend the decl/block tree which
18256 it is the root of, and for each other ..._DECL or BLOCK node contained
18257 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18258 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18259 values to point to themselves. */
18261 static void
18262 set_block_origin_self (tree stmt)
18264 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18266 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18269 tree local_decl;
18271 for (local_decl = BLOCK_VARS (stmt);
18272 local_decl != NULL_TREE;
18273 local_decl = DECL_CHAIN (local_decl))
18274 /* Do not recurse on nested functions since the inlining status
18275 of parent and child can be different as per the DWARF spec. */
18276 if (TREE_CODE (local_decl) != FUNCTION_DECL
18277 && !DECL_EXTERNAL (local_decl))
18278 set_decl_origin_self (local_decl);
18282 tree subblock;
18284 for (subblock = BLOCK_SUBBLOCKS (stmt);
18285 subblock != NULL_TREE;
18286 subblock = BLOCK_CHAIN (subblock))
18287 set_block_origin_self (subblock); /* Recurse. */
18292 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18293 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18294 node to so that it points to the node itself, thus indicating that the
18295 node represents its own (abstract) origin. Additionally, if the
18296 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18297 the decl/block tree of which the given node is the root of, and for
18298 each other ..._DECL or BLOCK node contained therein whose
18299 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18300 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18301 point to themselves. */
18303 static void
18304 set_decl_origin_self (tree decl)
18306 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18308 DECL_ABSTRACT_ORIGIN (decl) = decl;
18309 if (TREE_CODE (decl) == FUNCTION_DECL)
18311 tree arg;
18313 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18314 DECL_ABSTRACT_ORIGIN (arg) = arg;
18315 if (DECL_INITIAL (decl) != NULL_TREE
18316 && DECL_INITIAL (decl) != error_mark_node)
18317 set_block_origin_self (DECL_INITIAL (decl));
18322 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18323 and if it wasn't 1 before, push it to abstract_vec vector.
18324 For all local decls and all local sub-blocks (recursively) do it
18325 too. */
18327 static void
18328 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18330 tree local_decl;
18331 tree subblock;
18332 unsigned int i;
18334 if (!BLOCK_ABSTRACT (stmt))
18336 abstract_vec.safe_push (stmt);
18337 BLOCK_ABSTRACT (stmt) = 1;
18340 for (local_decl = BLOCK_VARS (stmt);
18341 local_decl != NULL_TREE;
18342 local_decl = DECL_CHAIN (local_decl))
18343 if (! DECL_EXTERNAL (local_decl))
18344 set_decl_abstract_flags (local_decl, abstract_vec);
18346 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18348 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18349 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18350 || TREE_CODE (local_decl) == PARM_DECL)
18351 set_decl_abstract_flags (local_decl, abstract_vec);
18354 for (subblock = BLOCK_SUBBLOCKS (stmt);
18355 subblock != NULL_TREE;
18356 subblock = BLOCK_CHAIN (subblock))
18357 set_block_abstract_flags (subblock, abstract_vec);
18360 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18361 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18362 In the case where the decl is a FUNCTION_DECL also set the abstract
18363 flags for all of the parameters, local vars, local
18364 blocks and sub-blocks (recursively). */
18366 static void
18367 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18369 if (!DECL_ABSTRACT_P (decl))
18371 abstract_vec.safe_push (decl);
18372 DECL_ABSTRACT_P (decl) = 1;
18375 if (TREE_CODE (decl) == FUNCTION_DECL)
18377 tree arg;
18379 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18380 if (!DECL_ABSTRACT_P (arg))
18382 abstract_vec.safe_push (arg);
18383 DECL_ABSTRACT_P (arg) = 1;
18385 if (DECL_INITIAL (decl) != NULL_TREE
18386 && DECL_INITIAL (decl) != error_mark_node)
18387 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18391 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18392 may later generate inlined and/or out-of-line instances of.
18394 FIXME: In the early-dwarf world, this function, and most of the
18395 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18396 the abstract instance. All we would need to do is annotate
18397 the early DIE with the appropriate DW_AT_inline in late
18398 dwarf (perhaps in gen_inlined_subroutine_die).
18400 However, we can't do this yet, because LTO streaming of DIEs
18401 has not been implemented yet. */
18403 static void
18404 dwarf2out_abstract_function (tree decl)
18406 dw_die_ref old_die;
18407 tree save_fn;
18408 tree context;
18409 hash_table<decl_loc_hasher> *old_decl_loc_table;
18410 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18411 int old_call_site_count, old_tail_call_site_count;
18412 struct call_arg_loc_node *old_call_arg_locations;
18414 /* Make sure we have the actual abstract inline, not a clone. */
18415 decl = DECL_ORIGIN (decl);
18417 old_die = lookup_decl_die (decl);
18418 if (old_die && get_AT (old_die, DW_AT_inline))
18419 /* We've already generated the abstract instance. */
18420 return;
18422 /* We can be called while recursively when seeing block defining inlined subroutine
18423 DIE. Be sure to not clobber the outer location table nor use it or we would
18424 get locations in abstract instantces. */
18425 old_decl_loc_table = decl_loc_table;
18426 decl_loc_table = NULL;
18427 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18428 cached_dw_loc_list_table = NULL;
18429 old_call_arg_locations = call_arg_locations;
18430 call_arg_locations = NULL;
18431 old_call_site_count = call_site_count;
18432 call_site_count = -1;
18433 old_tail_call_site_count = tail_call_site_count;
18434 tail_call_site_count = -1;
18436 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18437 we don't get confused by DECL_ABSTRACT_P. */
18438 if (debug_info_level > DINFO_LEVEL_TERSE)
18440 context = decl_class_context (decl);
18441 if (context)
18442 gen_type_die_for_member
18443 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18446 /* Pretend we've just finished compiling this function. */
18447 save_fn = current_function_decl;
18448 current_function_decl = decl;
18450 auto_vec<tree, 64> abstract_vec;
18451 set_decl_abstract_flags (decl, abstract_vec);
18452 dwarf2out_decl (decl);
18453 unsigned int i;
18454 tree t;
18455 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18456 if (TREE_CODE (t) == BLOCK)
18457 BLOCK_ABSTRACT (t) = 0;
18458 else
18459 DECL_ABSTRACT_P (t) = 0;
18461 current_function_decl = save_fn;
18462 decl_loc_table = old_decl_loc_table;
18463 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18464 call_arg_locations = old_call_arg_locations;
18465 call_site_count = old_call_site_count;
18466 tail_call_site_count = old_tail_call_site_count;
18469 /* Helper function of premark_used_types() which gets called through
18470 htab_traverse.
18472 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18473 marked as unused by prune_unused_types. */
18475 bool
18476 premark_used_types_helper (tree const &type, void *)
18478 dw_die_ref die;
18480 die = lookup_type_die (type);
18481 if (die != NULL)
18482 die->die_perennial_p = 1;
18483 return true;
18486 /* Helper function of premark_types_used_by_global_vars which gets called
18487 through htab_traverse.
18489 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18490 marked as unused by prune_unused_types. The DIE of the type is marked
18491 only if the global variable using the type will actually be emitted. */
18494 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18495 void *)
18497 struct types_used_by_vars_entry *entry;
18498 dw_die_ref die;
18500 entry = (struct types_used_by_vars_entry *) *slot;
18501 gcc_assert (entry->type != NULL
18502 && entry->var_decl != NULL);
18503 die = lookup_type_die (entry->type);
18504 if (die)
18506 /* Ask cgraph if the global variable really is to be emitted.
18507 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18508 varpool_node *node = varpool_node::get (entry->var_decl);
18509 if (node && node->definition)
18511 die->die_perennial_p = 1;
18512 /* Keep the parent DIEs as well. */
18513 while ((die = die->die_parent) && die->die_perennial_p == 0)
18514 die->die_perennial_p = 1;
18517 return 1;
18520 /* Mark all members of used_types_hash as perennial. */
18522 static void
18523 premark_used_types (struct function *fun)
18525 if (fun && fun->used_types_hash)
18526 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18529 /* Mark all members of types_used_by_vars_entry as perennial. */
18531 static void
18532 premark_types_used_by_global_vars (void)
18534 if (types_used_by_vars_hash)
18535 types_used_by_vars_hash
18536 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18539 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18540 for CA_LOC call arg loc node. */
18542 static dw_die_ref
18543 gen_call_site_die (tree decl, dw_die_ref subr_die,
18544 struct call_arg_loc_node *ca_loc)
18546 dw_die_ref stmt_die = NULL, die;
18547 tree block = ca_loc->block;
18549 while (block
18550 && block != DECL_INITIAL (decl)
18551 && TREE_CODE (block) == BLOCK)
18553 stmt_die = BLOCK_DIE (block);
18554 if (stmt_die)
18555 break;
18556 block = BLOCK_SUPERCONTEXT (block);
18558 if (stmt_die == NULL)
18559 stmt_die = subr_die;
18560 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18561 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18562 if (ca_loc->tail_call_p)
18563 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18564 if (ca_loc->symbol_ref)
18566 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18567 if (tdie)
18568 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18569 else
18570 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18572 return die;
18575 /* Generate a DIE to represent a declared function (either file-scope or
18576 block-local). */
18578 static void
18579 gen_subprogram_die (tree decl, dw_die_ref context_die)
18581 tree origin = decl_ultimate_origin (decl);
18582 dw_die_ref subr_die;
18583 dw_die_ref old_die = lookup_decl_die (decl);
18585 /* This function gets called multiple times for different stages of
18586 the debug process. For example, for func() in this code:
18588 namespace S
18590 void func() { ... }
18593 ...we get called 4 times. Twice in early debug and twice in
18594 late debug:
18596 Early debug
18597 -----------
18599 1. Once while generating func() within the namespace. This is
18600 the declaration. The declaration bit below is set, as the
18601 context is the namespace.
18603 A new DIE will be generated with DW_AT_declaration set.
18605 2. Once for func() itself. This is the specification. The
18606 declaration bit below is clear as the context is the CU.
18608 We will use the cached DIE from (1) to create a new DIE with
18609 DW_AT_specification pointing to the declaration in (1).
18611 Late debug via rest_of_handle_final()
18612 -------------------------------------
18614 3. Once generating func() within the namespace. This is also the
18615 declaration, as in (1), but this time we will early exit below
18616 as we have a cached DIE and a declaration needs no additional
18617 annotations (no locations), as the source declaration line
18618 info is enough.
18620 4. Once for func() itself. As in (2), this is the specification,
18621 but this time we will re-use the cached DIE, and just annotate
18622 it with the location information that should now be available.
18624 For something without namespaces, but with abstract instances, we
18625 are also called a multiple times:
18627 class Base
18629 public:
18630 Base (); // constructor declaration (1)
18633 Base::Base () { } // constructor specification (2)
18635 Early debug
18636 -----------
18638 1. Once for the Base() constructor by virtue of it being a
18639 member of the Base class. This is done via
18640 rest_of_type_compilation.
18642 This is a declaration, so a new DIE will be created with
18643 DW_AT_declaration.
18645 2. Once for the Base() constructor definition, but this time
18646 while generating the abstract instance of the base
18647 constructor (__base_ctor) which is being generated via early
18648 debug of reachable functions.
18650 Even though we have a cached version of the declaration (1),
18651 we will create a DW_AT_specification of the declaration DIE
18652 in (1).
18654 3. Once for the __base_ctor itself, but this time, we generate
18655 an DW_AT_abstract_origin version of the DW_AT_specification in
18656 (2).
18658 Late debug via rest_of_handle_final
18659 -----------------------------------
18661 4. One final time for the __base_ctor (which will have a cached
18662 DIE with DW_AT_abstract_origin created in (3). This time,
18663 we will just annotate the location information now
18664 available.
18666 int declaration = (current_function_decl != decl
18667 || class_or_namespace_scope_p (context_die));
18669 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18671 /* Now that the C++ front end lazily declares artificial member fns, we
18672 might need to retrofit the declaration into its class. */
18673 if (!declaration && !origin && !old_die
18674 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18675 && !class_or_namespace_scope_p (context_die)
18676 && debug_info_level > DINFO_LEVEL_TERSE)
18677 old_die = force_decl_die (decl);
18679 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18680 if (origin != NULL)
18682 gcc_assert (!declaration || local_scope_p (context_die));
18684 /* Fixup die_parent for the abstract instance of a nested
18685 inline function. */
18686 if (old_die && old_die->die_parent == NULL)
18687 add_child_die (context_die, old_die);
18689 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18691 /* If we have a DW_AT_abstract_origin we have a working
18692 cached version. */
18693 subr_die = old_die;
18695 else
18697 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18698 add_abstract_origin_attribute (subr_die, origin);
18699 /* This is where the actual code for a cloned function is.
18700 Let's emit linkage name attribute for it. This helps
18701 debuggers to e.g, set breakpoints into
18702 constructors/destructors when the user asks "break
18703 K::K". */
18704 add_linkage_name (subr_die, decl);
18707 /* A cached copy, possibly from early dwarf generation. Reuse as
18708 much as possible. */
18709 else if (old_die)
18711 /* A declaration that has been previously dumped needs no
18712 additional information. */
18713 if (declaration)
18714 return;
18716 if (!get_AT_flag (old_die, DW_AT_declaration)
18717 /* We can have a normal definition following an inline one in the
18718 case of redefinition of GNU C extern inlines.
18719 It seems reasonable to use AT_specification in this case. */
18720 && !get_AT (old_die, DW_AT_inline))
18722 /* Detect and ignore this case, where we are trying to output
18723 something we have already output. */
18724 if (get_AT (old_die, DW_AT_low_pc)
18725 || get_AT (old_die, DW_AT_ranges))
18726 return;
18728 /* If we have no location information, this must be a
18729 partially generated DIE from early dwarf generation.
18730 Fall through and generate it. */
18733 /* If the definition comes from the same place as the declaration,
18734 maybe use the old DIE. We always want the DIE for this function
18735 that has the *_pc attributes to be under comp_unit_die so the
18736 debugger can find it. We also need to do this for abstract
18737 instances of inlines, since the spec requires the out-of-line copy
18738 to have the same parent. For local class methods, this doesn't
18739 apply; we just use the old DIE. */
18740 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18741 struct dwarf_file_data * file_index = lookup_filename (s.file);
18742 if ((is_cu_die (old_die->die_parent)
18743 /* This condition fixes the inconsistency/ICE with the
18744 following Fortran test (or some derivative thereof) while
18745 building libgfortran:
18747 module some_m
18748 contains
18749 logical function funky (FLAG)
18750 funky = .true.
18751 end function
18752 end module
18754 || old_die->die_parent->die_tag == DW_TAG_module
18755 || context_die == NULL)
18756 && (DECL_ARTIFICIAL (decl)
18757 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18758 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18759 == (unsigned) s.line))))
18761 subr_die = old_die;
18763 /* Clear out the declaration attribute, but leave the
18764 parameters so they can be augmented with location
18765 information later. Unless this was a declaration, in
18766 which case, wipe out the nameless parameters and recreate
18767 them further down. */
18768 if (remove_AT (subr_die, DW_AT_declaration))
18771 remove_AT (subr_die, DW_AT_object_pointer);
18772 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18775 /* Make a specification pointing to the previously built
18776 declaration. */
18777 else
18779 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18780 add_AT_specification (subr_die, old_die);
18781 add_pubname (decl, subr_die);
18782 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18783 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18784 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18785 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18787 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18788 emit the real type on the definition die. */
18789 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18791 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18792 if (die == auto_die || die == decltype_auto_die)
18793 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18794 TYPE_UNQUALIFIED, context_die);
18798 /* Create a fresh DIE for anything else. */
18799 else
18801 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18803 if (TREE_PUBLIC (decl))
18804 add_AT_flag (subr_die, DW_AT_external, 1);
18806 add_name_and_src_coords_attributes (subr_die, decl);
18807 add_pubname (decl, subr_die);
18808 if (debug_info_level > DINFO_LEVEL_TERSE)
18810 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18811 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18812 TYPE_UNQUALIFIED, context_die);
18815 add_pure_or_virtual_attribute (subr_die, decl);
18816 if (DECL_ARTIFICIAL (decl))
18817 add_AT_flag (subr_die, DW_AT_artificial, 1);
18819 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18820 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18822 add_accessibility_attribute (subr_die, decl);
18825 /* Unless we have an existing non-declaration DIE, equate the new
18826 DIE. */
18827 if (!old_die || is_declaration_die (old_die))
18828 equate_decl_number_to_die (decl, subr_die);
18830 if (declaration)
18832 if (!old_die || !get_AT (old_die, DW_AT_inline))
18834 add_AT_flag (subr_die, DW_AT_declaration, 1);
18836 /* If this is an explicit function declaration then generate
18837 a DW_AT_explicit attribute. */
18838 if (lang_hooks.decls.function_decl_explicit_p (decl)
18839 && (dwarf_version >= 3 || !dwarf_strict))
18840 add_AT_flag (subr_die, DW_AT_explicit, 1);
18842 /* If this is a C++11 deleted special function member then generate
18843 a DW_AT_GNU_deleted attribute. */
18844 if (lang_hooks.decls.function_decl_deleted_p (decl)
18845 && (! dwarf_strict))
18846 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18849 /* Tag abstract instances with DW_AT_inline. */
18850 else if (DECL_ABSTRACT_P (decl))
18852 if (DECL_DECLARED_INLINE_P (decl))
18854 if (cgraph_function_possibly_inlined_p (decl))
18855 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18856 else
18857 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18859 else
18861 if (cgraph_function_possibly_inlined_p (decl))
18862 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18863 else
18864 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18867 if (DECL_DECLARED_INLINE_P (decl)
18868 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18869 add_AT_flag (subr_die, DW_AT_artificial, 1);
18871 /* For non DECL_EXTERNALs, if range information is available, fill
18872 the DIE with it. */
18873 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18875 HOST_WIDE_INT cfa_fb_offset;
18877 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18879 if (!flag_reorder_blocks_and_partition)
18881 dw_fde_ref fde = fun->fde;
18882 if (fde->dw_fde_begin)
18884 /* We have already generated the labels. */
18885 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18886 fde->dw_fde_end, false);
18888 else
18890 /* Create start/end labels and add the range. */
18891 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18892 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18893 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18894 current_function_funcdef_no);
18895 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18896 current_function_funcdef_no);
18897 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18898 false);
18901 #if VMS_DEBUGGING_INFO
18902 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18903 Section 2.3 Prologue and Epilogue Attributes:
18904 When a breakpoint is set on entry to a function, it is generally
18905 desirable for execution to be suspended, not on the very first
18906 instruction of the function, but rather at a point after the
18907 function's frame has been set up, after any language defined local
18908 declaration processing has been completed, and before execution of
18909 the first statement of the function begins. Debuggers generally
18910 cannot properly determine where this point is. Similarly for a
18911 breakpoint set on exit from a function. The prologue and epilogue
18912 attributes allow a compiler to communicate the location(s) to use. */
18915 if (fde->dw_fde_vms_end_prologue)
18916 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18917 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18919 if (fde->dw_fde_vms_begin_epilogue)
18920 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18921 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18923 #endif
18926 else
18928 /* Generate pubnames entries for the split function code ranges. */
18929 dw_fde_ref fde = fun->fde;
18931 if (fde->dw_fde_second_begin)
18933 if (dwarf_version >= 3 || !dwarf_strict)
18935 /* We should use ranges for non-contiguous code section
18936 addresses. Use the actual code range for the initial
18937 section, since the HOT/COLD labels might precede an
18938 alignment offset. */
18939 bool range_list_added = false;
18940 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18941 fde->dw_fde_end, &range_list_added,
18942 false);
18943 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18944 fde->dw_fde_second_end,
18945 &range_list_added, false);
18946 if (range_list_added)
18947 add_ranges (NULL);
18949 else
18951 /* There is no real support in DW2 for this .. so we make
18952 a work-around. First, emit the pub name for the segment
18953 containing the function label. Then make and emit a
18954 simplified subprogram DIE for the second segment with the
18955 name pre-fixed by __hot/cold_sect_of_. We use the same
18956 linkage name for the second die so that gdb will find both
18957 sections when given "b foo". */
18958 const char *name = NULL;
18959 tree decl_name = DECL_NAME (decl);
18960 dw_die_ref seg_die;
18962 /* Do the 'primary' section. */
18963 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18964 fde->dw_fde_end, false);
18966 /* Build a minimal DIE for the secondary section. */
18967 seg_die = new_die (DW_TAG_subprogram,
18968 subr_die->die_parent, decl);
18970 if (TREE_PUBLIC (decl))
18971 add_AT_flag (seg_die, DW_AT_external, 1);
18973 if (decl_name != NULL
18974 && IDENTIFIER_POINTER (decl_name) != NULL)
18976 name = dwarf2_name (decl, 1);
18977 if (! DECL_ARTIFICIAL (decl))
18978 add_src_coords_attributes (seg_die, decl);
18980 add_linkage_name (seg_die, decl);
18982 gcc_assert (name != NULL);
18983 add_pure_or_virtual_attribute (seg_die, decl);
18984 if (DECL_ARTIFICIAL (decl))
18985 add_AT_flag (seg_die, DW_AT_artificial, 1);
18987 name = concat ("__second_sect_of_", name, NULL);
18988 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18989 fde->dw_fde_second_end, false);
18990 add_name_attribute (seg_die, name);
18991 if (want_pubnames ())
18992 add_pubname_string (name, seg_die);
18995 else
18996 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18997 false);
19000 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19002 /* We define the "frame base" as the function's CFA. This is more
19003 convenient for several reasons: (1) It's stable across the prologue
19004 and epilogue, which makes it better than just a frame pointer,
19005 (2) With dwarf3, there exists a one-byte encoding that allows us
19006 to reference the .debug_frame data by proxy, but failing that,
19007 (3) We can at least reuse the code inspection and interpretation
19008 code that determines the CFA position at various points in the
19009 function. */
19010 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19012 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19013 add_AT_loc (subr_die, DW_AT_frame_base, op);
19015 else
19017 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19018 if (list->dw_loc_next)
19019 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19020 else
19021 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19024 /* Compute a displacement from the "steady-state frame pointer" to
19025 the CFA. The former is what all stack slots and argument slots
19026 will reference in the rtl; the latter is what we've told the
19027 debugger about. We'll need to adjust all frame_base references
19028 by this displacement. */
19029 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19031 if (fun->static_chain_decl)
19032 add_AT_location_description
19033 (subr_die, DW_AT_static_link,
19034 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19037 /* Generate child dies for template paramaters. */
19038 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19039 gen_generic_params_dies (decl);
19041 /* Now output descriptions of the arguments for this function. This gets
19042 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19043 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19044 `...' at the end of the formal parameter list. In order to find out if
19045 there was a trailing ellipsis or not, we must instead look at the type
19046 associated with the FUNCTION_DECL. This will be a node of type
19047 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19048 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19049 an ellipsis at the end. */
19051 /* In the case where we are describing a mere function declaration, all we
19052 need to do here (and all we *can* do here) is to describe the *types* of
19053 its formal parameters. */
19054 if (debug_info_level <= DINFO_LEVEL_TERSE)
19056 else if (declaration)
19057 gen_formal_types_die (decl, subr_die);
19058 else
19060 /* Generate DIEs to represent all known formal parameters. */
19061 tree parm = DECL_ARGUMENTS (decl);
19062 tree generic_decl = early_dwarf
19063 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19064 tree generic_decl_parm = generic_decl
19065 ? DECL_ARGUMENTS (generic_decl)
19066 : NULL;
19068 /* Now we want to walk the list of parameters of the function and
19069 emit their relevant DIEs.
19071 We consider the case of DECL being an instance of a generic function
19072 as well as it being a normal function.
19074 If DECL is an instance of a generic function we walk the
19075 parameters of the generic function declaration _and_ the parameters of
19076 DECL itself. This is useful because we want to emit specific DIEs for
19077 function parameter packs and those are declared as part of the
19078 generic function declaration. In that particular case,
19079 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19080 That DIE has children DIEs representing the set of arguments
19081 of the pack. Note that the set of pack arguments can be empty.
19082 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19083 children DIE.
19085 Otherwise, we just consider the parameters of DECL. */
19086 while (generic_decl_parm || parm)
19088 if (generic_decl_parm
19089 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19090 gen_formal_parameter_pack_die (generic_decl_parm,
19091 parm, subr_die,
19092 &parm);
19093 else if (parm && !POINTER_BOUNDS_P (parm))
19095 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19097 if (parm == DECL_ARGUMENTS (decl)
19098 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19099 && parm_die
19100 && (dwarf_version >= 3 || !dwarf_strict))
19101 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19103 parm = DECL_CHAIN (parm);
19105 else if (parm)
19106 parm = DECL_CHAIN (parm);
19108 if (generic_decl_parm)
19109 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19112 /* Decide whether we need an unspecified_parameters DIE at the end.
19113 There are 2 more cases to do this for: 1) the ansi ... declaration -
19114 this is detectable when the end of the arg list is not a
19115 void_type_node 2) an unprototyped function declaration (not a
19116 definition). This just means that we have no info about the
19117 parameters at all. */
19118 if (prototype_p (TREE_TYPE (decl)))
19120 /* This is the prototyped case, check for.... */
19121 if (stdarg_p (TREE_TYPE (decl)))
19122 gen_unspecified_parameters_die (decl, subr_die);
19124 else if (DECL_INITIAL (decl) == NULL_TREE)
19125 gen_unspecified_parameters_die (decl, subr_die);
19128 if (subr_die != old_die)
19129 /* Add the calling convention attribute if requested. */
19130 add_calling_convention_attribute (subr_die, decl);
19132 /* Output Dwarf info for all of the stuff within the body of the function
19133 (if it has one - it may be just a declaration).
19135 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19136 a function. This BLOCK actually represents the outermost binding contour
19137 for the function, i.e. the contour in which the function's formal
19138 parameters and labels get declared. Curiously, it appears that the front
19139 end doesn't actually put the PARM_DECL nodes for the current function onto
19140 the BLOCK_VARS list for this outer scope, but are strung off of the
19141 DECL_ARGUMENTS list for the function instead.
19143 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19144 the LABEL_DECL nodes for the function however, and we output DWARF info
19145 for those in decls_for_scope. Just within the `outer_scope' there will be
19146 a BLOCK node representing the function's outermost pair of curly braces,
19147 and any blocks used for the base and member initializers of a C++
19148 constructor function. */
19149 tree outer_scope = DECL_INITIAL (decl);
19150 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19152 int call_site_note_count = 0;
19153 int tail_call_site_note_count = 0;
19155 /* Emit a DW_TAG_variable DIE for a named return value. */
19156 if (DECL_NAME (DECL_RESULT (decl)))
19157 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19159 /* The first time through decls_for_scope we will generate the
19160 DIEs for the locals. The second time, we fill in the
19161 location info. */
19162 decls_for_scope (outer_scope, subr_die);
19164 if (call_arg_locations && !dwarf_strict)
19166 struct call_arg_loc_node *ca_loc;
19167 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19169 dw_die_ref die = NULL;
19170 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19171 rtx arg, next_arg;
19173 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19174 arg; arg = next_arg)
19176 dw_loc_descr_ref reg, val;
19177 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19178 dw_die_ref cdie, tdie = NULL;
19180 next_arg = XEXP (arg, 1);
19181 if (REG_P (XEXP (XEXP (arg, 0), 0))
19182 && next_arg
19183 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19184 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19185 && REGNO (XEXP (XEXP (arg, 0), 0))
19186 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19187 next_arg = XEXP (next_arg, 1);
19188 if (mode == VOIDmode)
19190 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19191 if (mode == VOIDmode)
19192 mode = GET_MODE (XEXP (arg, 0));
19194 if (mode == VOIDmode || mode == BLKmode)
19195 continue;
19196 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19198 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19199 tloc = XEXP (XEXP (arg, 0), 1);
19200 continue;
19202 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19203 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19205 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19206 tlocc = XEXP (XEXP (arg, 0), 1);
19207 continue;
19209 reg = NULL;
19210 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19211 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19212 VAR_INIT_STATUS_INITIALIZED);
19213 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19215 rtx mem = XEXP (XEXP (arg, 0), 0);
19216 reg = mem_loc_descriptor (XEXP (mem, 0),
19217 get_address_mode (mem),
19218 GET_MODE (mem),
19219 VAR_INIT_STATUS_INITIALIZED);
19221 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19222 == DEBUG_PARAMETER_REF)
19224 tree tdecl
19225 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19226 tdie = lookup_decl_die (tdecl);
19227 if (tdie == NULL)
19228 continue;
19230 else
19231 continue;
19232 if (reg == NULL
19233 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19234 != DEBUG_PARAMETER_REF)
19235 continue;
19236 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19237 VOIDmode,
19238 VAR_INIT_STATUS_INITIALIZED);
19239 if (val == NULL)
19240 continue;
19241 if (die == NULL)
19242 die = gen_call_site_die (decl, subr_die, ca_loc);
19243 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19244 NULL_TREE);
19245 if (reg != NULL)
19246 add_AT_loc (cdie, DW_AT_location, reg);
19247 else if (tdie != NULL)
19248 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19249 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19250 if (next_arg != XEXP (arg, 1))
19252 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19253 if (mode == VOIDmode)
19254 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19255 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19256 0), 1),
19257 mode, VOIDmode,
19258 VAR_INIT_STATUS_INITIALIZED);
19259 if (val != NULL)
19260 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19263 if (die == NULL
19264 && (ca_loc->symbol_ref || tloc))
19265 die = gen_call_site_die (decl, subr_die, ca_loc);
19266 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19268 dw_loc_descr_ref tval = NULL;
19270 if (tloc != NULL_RTX)
19271 tval = mem_loc_descriptor (tloc,
19272 GET_MODE (tloc) == VOIDmode
19273 ? Pmode : GET_MODE (tloc),
19274 VOIDmode,
19275 VAR_INIT_STATUS_INITIALIZED);
19276 if (tval)
19277 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19278 else if (tlocc != NULL_RTX)
19280 tval = mem_loc_descriptor (tlocc,
19281 GET_MODE (tlocc) == VOIDmode
19282 ? Pmode : GET_MODE (tlocc),
19283 VOIDmode,
19284 VAR_INIT_STATUS_INITIALIZED);
19285 if (tval)
19286 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19287 tval);
19290 if (die != NULL)
19292 call_site_note_count++;
19293 if (ca_loc->tail_call_p)
19294 tail_call_site_note_count++;
19298 call_arg_locations = NULL;
19299 call_arg_loc_last = NULL;
19300 if (tail_call_site_count >= 0
19301 && tail_call_site_count == tail_call_site_note_count
19302 && !dwarf_strict)
19304 if (call_site_count >= 0
19305 && call_site_count == call_site_note_count)
19306 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19307 else
19308 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19310 call_site_count = -1;
19311 tail_call_site_count = -1;
19315 /* Returns a hash value for X (which really is a die_struct). */
19317 hashval_t
19318 block_die_hasher::hash (die_struct *d)
19320 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19323 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19324 as decl_id and die_parent of die_struct Y. */
19326 bool
19327 block_die_hasher::equal (die_struct *x, die_struct *y)
19329 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19332 /* Return TRUE if DECL, which may have been previously generated as
19333 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19334 true if decl (or its origin) is either an extern declaration or a
19335 class/namespace scoped declaration.
19337 The declare_in_namespace support causes us to get two DIEs for one
19338 variable, both of which are declarations. We want to avoid
19339 considering one to be a specification, so we must test for
19340 DECLARATION and DW_AT_declaration. */
19341 static inline bool
19342 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19344 return (old_die && TREE_STATIC (decl) && !declaration
19345 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19348 /* Return true if DECL is a local static. */
19350 static inline bool
19351 local_function_static (tree decl)
19353 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19354 return TREE_STATIC (decl)
19355 && DECL_CONTEXT (decl)
19356 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19359 /* Generate a DIE to represent a declared data object.
19360 Either DECL or ORIGIN must be non-null. */
19362 static void
19363 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19365 HOST_WIDE_INT off = 0;
19366 tree com_decl;
19367 tree decl_or_origin = decl ? decl : origin;
19368 tree ultimate_origin;
19369 dw_die_ref var_die;
19370 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19371 dw_die_ref origin_die = NULL;
19372 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19373 || class_or_namespace_scope_p (context_die));
19374 bool specialization_p = false;
19376 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19377 if (decl || ultimate_origin)
19378 origin = ultimate_origin;
19379 com_decl = fortran_common (decl_or_origin, &off);
19381 /* Symbol in common gets emitted as a child of the common block, in the form
19382 of a data member. */
19383 if (com_decl)
19385 dw_die_ref com_die;
19386 dw_loc_list_ref loc;
19387 die_node com_die_arg;
19389 var_die = lookup_decl_die (decl_or_origin);
19390 if (var_die)
19392 if (get_AT (var_die, DW_AT_location) == NULL)
19394 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19395 if (loc)
19397 if (off)
19399 /* Optimize the common case. */
19400 if (single_element_loc_list_p (loc)
19401 && loc->expr->dw_loc_opc == DW_OP_addr
19402 && loc->expr->dw_loc_next == NULL
19403 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19404 == SYMBOL_REF)
19406 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19407 loc->expr->dw_loc_oprnd1.v.val_addr
19408 = plus_constant (GET_MODE (x), x , off);
19410 else
19411 loc_list_plus_const (loc, off);
19413 add_AT_location_description (var_die, DW_AT_location, loc);
19414 remove_AT (var_die, DW_AT_declaration);
19417 return;
19420 if (common_block_die_table == NULL)
19421 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19423 com_die_arg.decl_id = DECL_UID (com_decl);
19424 com_die_arg.die_parent = context_die;
19425 com_die = common_block_die_table->find (&com_die_arg);
19426 loc = loc_list_from_tree (com_decl, 2, NULL);
19427 if (com_die == NULL)
19429 const char *cnam
19430 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19431 die_node **slot;
19433 com_die = new_die (DW_TAG_common_block, context_die, decl);
19434 add_name_and_src_coords_attributes (com_die, com_decl);
19435 if (loc)
19437 add_AT_location_description (com_die, DW_AT_location, loc);
19438 /* Avoid sharing the same loc descriptor between
19439 DW_TAG_common_block and DW_TAG_variable. */
19440 loc = loc_list_from_tree (com_decl, 2, NULL);
19442 else if (DECL_EXTERNAL (decl))
19443 add_AT_flag (com_die, DW_AT_declaration, 1);
19444 if (want_pubnames ())
19445 add_pubname_string (cnam, com_die); /* ??? needed? */
19446 com_die->decl_id = DECL_UID (com_decl);
19447 slot = common_block_die_table->find_slot (com_die, INSERT);
19448 *slot = com_die;
19450 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19452 add_AT_location_description (com_die, DW_AT_location, loc);
19453 loc = loc_list_from_tree (com_decl, 2, NULL);
19454 remove_AT (com_die, DW_AT_declaration);
19456 var_die = new_die (DW_TAG_variable, com_die, decl);
19457 add_name_and_src_coords_attributes (var_die, decl);
19458 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19459 context_die);
19460 add_AT_flag (var_die, DW_AT_external, 1);
19461 if (loc)
19463 if (off)
19465 /* Optimize the common case. */
19466 if (single_element_loc_list_p (loc)
19467 && loc->expr->dw_loc_opc == DW_OP_addr
19468 && loc->expr->dw_loc_next == NULL
19469 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19471 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19472 loc->expr->dw_loc_oprnd1.v.val_addr
19473 = plus_constant (GET_MODE (x), x, off);
19475 else
19476 loc_list_plus_const (loc, off);
19478 add_AT_location_description (var_die, DW_AT_location, loc);
19480 else if (DECL_EXTERNAL (decl))
19481 add_AT_flag (var_die, DW_AT_declaration, 1);
19482 equate_decl_number_to_die (decl, var_die);
19483 return;
19486 if (old_die)
19488 if (declaration)
19490 /* A declaration that has been previously dumped, needs no
19491 further annotations, since it doesn't need location on
19492 the second pass. */
19493 return;
19495 else if (decl_will_get_specification_p (old_die, decl, declaration)
19496 && !get_AT (old_die, DW_AT_specification))
19498 /* Fall-thru so we can make a new variable die along with a
19499 DW_AT_specification. */
19501 else if (origin && old_die->die_parent != context_die)
19503 /* If we will be creating an inlined instance, we need a
19504 new DIE that will get annotated with
19505 DW_AT_abstract_origin. Clear things so we can get a
19506 new DIE. */
19507 gcc_assert (!DECL_ABSTRACT_P (decl));
19508 old_die = NULL;
19510 else
19512 /* If a DIE was dumped early, it still needs location info.
19513 Skip to where we fill the location bits. */
19514 var_die = old_die;
19515 goto gen_variable_die_location;
19519 /* For static data members, the declaration in the class is supposed
19520 to have DW_TAG_member tag; the specification should still be
19521 DW_TAG_variable referencing the DW_TAG_member DIE. */
19522 if (declaration && class_scope_p (context_die))
19523 var_die = new_die (DW_TAG_member, context_die, decl);
19524 else
19525 var_die = new_die (DW_TAG_variable, context_die, decl);
19527 if (origin != NULL)
19528 origin_die = add_abstract_origin_attribute (var_die, origin);
19530 /* Loop unrolling can create multiple blocks that refer to the same
19531 static variable, so we must test for the DW_AT_declaration flag.
19533 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19534 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19535 sharing them.
19537 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19538 else if (decl_will_get_specification_p (old_die, decl, declaration))
19540 /* This is a definition of a C++ class level static. */
19541 add_AT_specification (var_die, old_die);
19542 specialization_p = true;
19543 if (DECL_NAME (decl))
19545 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19546 struct dwarf_file_data * file_index = lookup_filename (s.file);
19548 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19549 add_AT_file (var_die, DW_AT_decl_file, file_index);
19551 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19552 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19554 if (old_die->die_tag == DW_TAG_member)
19555 add_linkage_name (var_die, decl);
19558 else
19559 add_name_and_src_coords_attributes (var_die, decl);
19561 if ((origin == NULL && !specialization_p)
19562 || (origin != NULL
19563 && !DECL_ABSTRACT_P (decl_or_origin)
19564 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19565 decl_function_context
19566 (decl_or_origin))))
19568 tree type = TREE_TYPE (decl_or_origin);
19570 if (decl_by_reference_p (decl_or_origin))
19571 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19572 context_die);
19573 else
19574 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19575 context_die);
19578 if (origin == NULL && !specialization_p)
19580 if (TREE_PUBLIC (decl))
19581 add_AT_flag (var_die, DW_AT_external, 1);
19583 if (DECL_ARTIFICIAL (decl))
19584 add_AT_flag (var_die, DW_AT_artificial, 1);
19586 add_accessibility_attribute (var_die, decl);
19589 if (declaration)
19590 add_AT_flag (var_die, DW_AT_declaration, 1);
19592 if (decl && (DECL_ABSTRACT_P (decl)
19593 || !old_die || is_declaration_die (old_die)))
19594 equate_decl_number_to_die (decl, var_die);
19596 gen_variable_die_location:
19597 if (! declaration
19598 && (! DECL_ABSTRACT_P (decl_or_origin)
19599 /* Local static vars are shared between all clones/inlines,
19600 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19601 already set. */
19602 || (TREE_CODE (decl_or_origin) == VAR_DECL
19603 && TREE_STATIC (decl_or_origin)
19604 && DECL_RTL_SET_P (decl_or_origin)))
19605 /* When abstract origin already has DW_AT_location attribute, no need
19606 to add it again. */
19607 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19609 if (early_dwarf)
19610 add_pubname (decl_or_origin, var_die);
19611 else
19612 add_location_or_const_value_attribute (var_die, decl_or_origin,
19613 decl == NULL, DW_AT_location);
19615 else
19616 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19619 /* Generate a DIE to represent a named constant. */
19621 static void
19622 gen_const_die (tree decl, dw_die_ref context_die)
19624 dw_die_ref const_die;
19625 tree type = TREE_TYPE (decl);
19627 const_die = lookup_decl_die (decl);
19628 if (const_die)
19629 return;
19631 const_die = new_die (DW_TAG_constant, context_die, decl);
19632 equate_decl_number_to_die (decl, const_die);
19633 add_name_and_src_coords_attributes (const_die, decl);
19634 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19635 if (TREE_PUBLIC (decl))
19636 add_AT_flag (const_die, DW_AT_external, 1);
19637 if (DECL_ARTIFICIAL (decl))
19638 add_AT_flag (const_die, DW_AT_artificial, 1);
19639 tree_add_const_value_attribute_for_decl (const_die, decl);
19642 /* Generate a DIE to represent a label identifier. */
19644 static void
19645 gen_label_die (tree decl, dw_die_ref context_die)
19647 tree origin = decl_ultimate_origin (decl);
19648 dw_die_ref lbl_die = lookup_decl_die (decl);
19649 rtx insn;
19650 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19652 if (!lbl_die)
19654 lbl_die = new_die (DW_TAG_label, context_die, decl);
19655 equate_decl_number_to_die (decl, lbl_die);
19657 if (origin != NULL)
19658 add_abstract_origin_attribute (lbl_die, origin);
19659 else
19660 add_name_and_src_coords_attributes (lbl_die, decl);
19663 if (DECL_ABSTRACT_P (decl))
19664 equate_decl_number_to_die (decl, lbl_die);
19665 else
19667 insn = DECL_RTL_IF_SET (decl);
19669 /* Deleted labels are programmer specified labels which have been
19670 eliminated because of various optimizations. We still emit them
19671 here so that it is possible to put breakpoints on them. */
19672 if (insn
19673 && (LABEL_P (insn)
19674 || ((NOTE_P (insn)
19675 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19677 /* When optimization is enabled (via -O) some parts of the compiler
19678 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19679 represent source-level labels which were explicitly declared by
19680 the user. This really shouldn't be happening though, so catch
19681 it if it ever does happen. */
19682 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19684 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19685 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19687 else if (insn
19688 && NOTE_P (insn)
19689 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19690 && CODE_LABEL_NUMBER (insn) != -1)
19692 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19693 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19698 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19699 attributes to the DIE for a block STMT, to describe where the inlined
19700 function was called from. This is similar to add_src_coords_attributes. */
19702 static inline void
19703 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19705 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19707 if (dwarf_version >= 3 || !dwarf_strict)
19709 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19710 add_AT_unsigned (die, DW_AT_call_line, s.line);
19715 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19716 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19718 static inline void
19719 add_high_low_attributes (tree stmt, dw_die_ref die)
19721 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19723 if (BLOCK_FRAGMENT_CHAIN (stmt)
19724 && (dwarf_version >= 3 || !dwarf_strict))
19726 tree chain, superblock = NULL_TREE;
19727 dw_die_ref pdie;
19728 dw_attr_ref attr = NULL;
19730 if (inlined_function_outer_scope_p (stmt))
19732 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19733 BLOCK_NUMBER (stmt));
19734 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19737 /* Optimize duplicate .debug_ranges lists or even tails of
19738 lists. If this BLOCK has same ranges as its supercontext,
19739 lookup DW_AT_ranges attribute in the supercontext (and
19740 recursively so), verify that the ranges_table contains the
19741 right values and use it instead of adding a new .debug_range. */
19742 for (chain = stmt, pdie = die;
19743 BLOCK_SAME_RANGE (chain);
19744 chain = BLOCK_SUPERCONTEXT (chain))
19746 dw_attr_ref new_attr;
19748 pdie = pdie->die_parent;
19749 if (pdie == NULL)
19750 break;
19751 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19752 break;
19753 new_attr = get_AT (pdie, DW_AT_ranges);
19754 if (new_attr == NULL
19755 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19756 break;
19757 attr = new_attr;
19758 superblock = BLOCK_SUPERCONTEXT (chain);
19760 if (attr != NULL
19761 && (ranges_table[attr->dw_attr_val.v.val_offset
19762 / 2 / DWARF2_ADDR_SIZE].num
19763 == BLOCK_NUMBER (superblock))
19764 && BLOCK_FRAGMENT_CHAIN (superblock))
19766 unsigned long off = attr->dw_attr_val.v.val_offset
19767 / 2 / DWARF2_ADDR_SIZE;
19768 unsigned long supercnt = 0, thiscnt = 0;
19769 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19770 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19772 ++supercnt;
19773 gcc_checking_assert (ranges_table[off + supercnt].num
19774 == BLOCK_NUMBER (chain));
19776 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19777 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19778 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19779 ++thiscnt;
19780 gcc_assert (supercnt >= thiscnt);
19781 add_AT_range_list (die, DW_AT_ranges,
19782 ((off + supercnt - thiscnt)
19783 * 2 * DWARF2_ADDR_SIZE),
19784 false);
19785 return;
19788 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19790 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19793 add_ranges (chain);
19794 chain = BLOCK_FRAGMENT_CHAIN (chain);
19796 while (chain);
19797 add_ranges (NULL);
19799 else
19801 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19802 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19803 BLOCK_NUMBER (stmt));
19804 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19805 BLOCK_NUMBER (stmt));
19806 add_AT_low_high_pc (die, label, label_high, false);
19810 /* Generate a DIE for a lexical block. */
19812 static void
19813 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19815 dw_die_ref old_die = BLOCK_DIE (stmt);
19816 dw_die_ref stmt_die = NULL;
19817 if (!old_die)
19819 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19820 BLOCK_DIE (stmt) = stmt_die;
19823 if (BLOCK_ABSTRACT (stmt))
19825 if (old_die)
19827 #ifdef ENABLE_CHECKING
19828 /* This must have been generated early and it won't even
19829 need location information since it's a DW_AT_inline
19830 function. */
19831 for (dw_die_ref c = context_die; c; c = c->die_parent)
19832 if (c->die_tag == DW_TAG_inlined_subroutine
19833 || c->die_tag == DW_TAG_subprogram)
19835 gcc_assert (get_AT (c, DW_AT_inline));
19836 break;
19838 #endif
19839 return;
19842 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19844 /* If this is an inlined instance, create a new lexical die for
19845 anything below to attach DW_AT_abstract_origin to. */
19846 if (old_die)
19848 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19849 BLOCK_DIE (stmt) = stmt_die;
19850 old_die = NULL;
19854 if (old_die)
19855 stmt_die = old_die;
19857 /* A non abstract block whose blocks have already been reordered
19858 should have the instruction range for this block. If so, set the
19859 high/low attributes. */
19860 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19862 gcc_assert (stmt_die);
19863 add_high_low_attributes (stmt, stmt_die);
19866 decls_for_scope (stmt, stmt_die);
19869 /* Generate a DIE for an inlined subprogram. */
19871 static void
19872 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19874 tree decl;
19876 /* The instance of function that is effectively being inlined shall not
19877 be abstract. */
19878 gcc_assert (! BLOCK_ABSTRACT (stmt));
19880 decl = block_ultimate_origin (stmt);
19882 /* Emit info for the abstract instance first, if we haven't yet. We
19883 must emit this even if the block is abstract, otherwise when we
19884 emit the block below (or elsewhere), we may end up trying to emit
19885 a die whose origin die hasn't been emitted, and crashing. */
19886 dwarf2out_abstract_function (decl);
19888 if (! BLOCK_ABSTRACT (stmt))
19890 dw_die_ref subr_die
19891 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19893 if (call_arg_locations)
19894 BLOCK_DIE (stmt) = subr_die;
19895 add_abstract_origin_attribute (subr_die, decl);
19896 if (TREE_ASM_WRITTEN (stmt))
19897 add_high_low_attributes (stmt, subr_die);
19898 add_call_src_coords_attributes (stmt, subr_die);
19900 decls_for_scope (stmt, subr_die);
19904 /* Generate a DIE for a field in a record, or structure. */
19906 static void
19907 gen_field_die (tree decl, dw_die_ref context_die)
19909 dw_die_ref decl_die;
19911 if (TREE_TYPE (decl) == error_mark_node)
19912 return;
19914 decl_die = new_die (DW_TAG_member, context_die, decl);
19915 add_name_and_src_coords_attributes (decl_die, decl);
19916 add_type_attribute (decl_die, member_declared_type (decl),
19917 decl_quals (decl), context_die);
19919 if (DECL_BIT_FIELD_TYPE (decl))
19921 add_byte_size_attribute (decl_die, decl);
19922 add_bit_size_attribute (decl_die, decl);
19923 add_bit_offset_attribute (decl_die, decl);
19926 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19927 add_data_member_location_attribute (decl_die, decl);
19929 if (DECL_ARTIFICIAL (decl))
19930 add_AT_flag (decl_die, DW_AT_artificial, 1);
19932 add_accessibility_attribute (decl_die, decl);
19934 /* Equate decl number to die, so that we can look up this decl later on. */
19935 equate_decl_number_to_die (decl, decl_die);
19938 #if 0
19939 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19940 Use modified_type_die instead.
19941 We keep this code here just in case these types of DIEs may be needed to
19942 represent certain things in other languages (e.g. Pascal) someday. */
19944 static void
19945 gen_pointer_type_die (tree type, dw_die_ref context_die)
19947 dw_die_ref ptr_die
19948 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19950 equate_type_number_to_die (type, ptr_die);
19951 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19952 context_die);
19953 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19956 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19957 Use modified_type_die instead.
19958 We keep this code here just in case these types of DIEs may be needed to
19959 represent certain things in other languages (e.g. Pascal) someday. */
19961 static void
19962 gen_reference_type_die (tree type, dw_die_ref context_die)
19964 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19966 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19967 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19968 else
19969 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19971 equate_type_number_to_die (type, ref_die);
19972 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19973 context_die);
19974 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19976 #endif
19978 /* Generate a DIE for a pointer to a member type. */
19980 static void
19981 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19983 dw_die_ref ptr_die
19984 = new_die (DW_TAG_ptr_to_member_type,
19985 scope_die_for (type, context_die), type);
19987 equate_type_number_to_die (type, ptr_die);
19988 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19989 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19990 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19991 context_die);
19994 typedef const char *dchar_p; /* For DEF_VEC_P. */
19996 static char *producer_string;
19998 /* Return a heap allocated producer string including command line options
19999 if -grecord-gcc-switches. */
20001 static char *
20002 gen_producer_string (void)
20004 size_t j;
20005 auto_vec<dchar_p> switches;
20006 const char *language_string = lang_hooks.name;
20007 char *producer, *tail;
20008 const char *p;
20009 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20010 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20012 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20013 switch (save_decoded_options[j].opt_index)
20015 case OPT_o:
20016 case OPT_d:
20017 case OPT_dumpbase:
20018 case OPT_dumpdir:
20019 case OPT_auxbase:
20020 case OPT_auxbase_strip:
20021 case OPT_quiet:
20022 case OPT_version:
20023 case OPT_v:
20024 case OPT_w:
20025 case OPT_L:
20026 case OPT_D:
20027 case OPT_I:
20028 case OPT_U:
20029 case OPT_SPECIAL_unknown:
20030 case OPT_SPECIAL_ignore:
20031 case OPT_SPECIAL_program_name:
20032 case OPT_SPECIAL_input_file:
20033 case OPT_grecord_gcc_switches:
20034 case OPT_gno_record_gcc_switches:
20035 case OPT__output_pch_:
20036 case OPT_fdiagnostics_show_location_:
20037 case OPT_fdiagnostics_show_option:
20038 case OPT_fdiagnostics_show_caret:
20039 case OPT_fdiagnostics_color_:
20040 case OPT_fverbose_asm:
20041 case OPT____:
20042 case OPT__sysroot_:
20043 case OPT_nostdinc:
20044 case OPT_nostdinc__:
20045 case OPT_fpreprocessed:
20046 case OPT_fltrans_output_list_:
20047 case OPT_fresolution_:
20048 /* Ignore these. */
20049 continue;
20050 default:
20051 if (cl_options[save_decoded_options[j].opt_index].flags
20052 & CL_NO_DWARF_RECORD)
20053 continue;
20054 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20055 == '-');
20056 switch (save_decoded_options[j].canonical_option[0][1])
20058 case 'M':
20059 case 'i':
20060 case 'W':
20061 continue;
20062 case 'f':
20063 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20064 "dump", 4) == 0)
20065 continue;
20066 break;
20067 default:
20068 break;
20070 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20071 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20072 break;
20075 producer = XNEWVEC (char, plen + 1 + len + 1);
20076 tail = producer;
20077 sprintf (tail, "%s %s", language_string, version_string);
20078 tail += plen;
20080 FOR_EACH_VEC_ELT (switches, j, p)
20082 len = strlen (p);
20083 *tail = ' ';
20084 memcpy (tail + 1, p, len);
20085 tail += len + 1;
20088 *tail = '\0';
20089 return producer;
20092 /* Given a C and/or C++ language/version string return the "highest".
20093 C++ is assumed to be "higher" than C in this case. Used for merging
20094 LTO translation unit languages. */
20095 static const char *
20096 highest_c_language (const char *lang1, const char *lang2)
20098 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20099 return "GNU C++14";
20100 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20101 return "GNU C++11";
20102 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20103 return "GNU C++98";
20105 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20106 return "GNU C11";
20107 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20108 return "GNU C99";
20109 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20110 return "GNU C89";
20112 gcc_unreachable ();
20116 /* Generate the DIE for the compilation unit. */
20118 static dw_die_ref
20119 gen_compile_unit_die (const char *filename)
20121 dw_die_ref die;
20122 const char *language_string = lang_hooks.name;
20123 int language;
20125 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20127 if (filename)
20129 add_name_attribute (die, filename);
20130 /* Don't add cwd for <built-in>. */
20131 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20132 add_comp_dir_attribute (die);
20135 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20137 /* If our producer is LTO try to figure out a common language to use
20138 from the global list of translation units. */
20139 if (strcmp (language_string, "GNU GIMPLE") == 0)
20141 unsigned i;
20142 tree t;
20143 const char *common_lang = NULL;
20145 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20147 if (!TRANSLATION_UNIT_LANGUAGE (t))
20148 continue;
20149 if (!common_lang)
20150 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20151 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20153 else if (strncmp (common_lang, "GNU C", 5) == 0
20154 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20155 /* Mixing C and C++ is ok, use C++ in that case. */
20156 common_lang = highest_c_language (common_lang,
20157 TRANSLATION_UNIT_LANGUAGE (t));
20158 else
20160 /* Fall back to C. */
20161 common_lang = NULL;
20162 break;
20166 if (common_lang)
20167 language_string = common_lang;
20170 language = DW_LANG_C;
20171 if (strncmp (language_string, "GNU C", 5) == 0
20172 && ISDIGIT (language_string[5]))
20174 language = DW_LANG_C89;
20175 if (dwarf_version >= 3 || !dwarf_strict)
20177 if (strcmp (language_string, "GNU C89") != 0)
20178 language = DW_LANG_C99;
20180 if (dwarf_version >= 5 /* || !dwarf_strict */)
20181 if (strcmp (language_string, "GNU C11") == 0)
20182 language = DW_LANG_C11;
20185 else if (strncmp (language_string, "GNU C++", 7) == 0)
20187 language = DW_LANG_C_plus_plus;
20188 if (dwarf_version >= 5 /* || !dwarf_strict */)
20190 if (strcmp (language_string, "GNU C++11") == 0)
20191 language = DW_LANG_C_plus_plus_11;
20192 else if (strcmp (language_string, "GNU C++14") == 0)
20193 language = DW_LANG_C_plus_plus_14;
20196 else if (strcmp (language_string, "GNU F77") == 0)
20197 language = DW_LANG_Fortran77;
20198 else if (strcmp (language_string, "GNU Pascal") == 0)
20199 language = DW_LANG_Pascal83;
20200 else if (dwarf_version >= 3 || !dwarf_strict)
20202 if (strcmp (language_string, "GNU Ada") == 0)
20203 language = DW_LANG_Ada95;
20204 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20206 language = DW_LANG_Fortran95;
20207 if (dwarf_version >= 5 /* || !dwarf_strict */)
20209 if (strcmp (language_string, "GNU Fortran2003") == 0)
20210 language = DW_LANG_Fortran03;
20211 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20212 language = DW_LANG_Fortran08;
20215 else if (strcmp (language_string, "GNU Java") == 0)
20216 language = DW_LANG_Java;
20217 else if (strcmp (language_string, "GNU Objective-C") == 0)
20218 language = DW_LANG_ObjC;
20219 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20220 language = DW_LANG_ObjC_plus_plus;
20221 else if (dwarf_version >= 5 || !dwarf_strict)
20223 if (strcmp (language_string, "GNU Go") == 0)
20224 language = DW_LANG_Go;
20227 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20228 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20229 language = DW_LANG_Fortran90;
20231 add_AT_unsigned (die, DW_AT_language, language);
20233 switch (language)
20235 case DW_LANG_Fortran77:
20236 case DW_LANG_Fortran90:
20237 case DW_LANG_Fortran95:
20238 case DW_LANG_Fortran03:
20239 case DW_LANG_Fortran08:
20240 /* Fortran has case insensitive identifiers and the front-end
20241 lowercases everything. */
20242 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20243 break;
20244 default:
20245 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20246 break;
20248 return die;
20251 /* Generate the DIE for a base class. */
20253 static void
20254 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20256 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20258 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20259 add_data_member_location_attribute (die, binfo);
20261 if (BINFO_VIRTUAL_P (binfo))
20262 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20264 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20265 children, otherwise the default is DW_ACCESS_public. In DWARF2
20266 the default has always been DW_ACCESS_private. */
20267 if (access == access_public_node)
20269 if (dwarf_version == 2
20270 || context_die->die_tag == DW_TAG_class_type)
20271 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20273 else if (access == access_protected_node)
20274 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20275 else if (dwarf_version > 2
20276 && context_die->die_tag != DW_TAG_class_type)
20277 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20280 /* Generate a DIE for a class member. */
20282 static void
20283 gen_member_die (tree type, dw_die_ref context_die)
20285 tree member;
20286 tree binfo = TYPE_BINFO (type);
20287 dw_die_ref child;
20289 /* If this is not an incomplete type, output descriptions of each of its
20290 members. Note that as we output the DIEs necessary to represent the
20291 members of this record or union type, we will also be trying to output
20292 DIEs to represent the *types* of those members. However the `type'
20293 function (above) will specifically avoid generating type DIEs for member
20294 types *within* the list of member DIEs for this (containing) type except
20295 for those types (of members) which are explicitly marked as also being
20296 members of this (containing) type themselves. The g++ front- end can
20297 force any given type to be treated as a member of some other (containing)
20298 type by setting the TYPE_CONTEXT of the given (member) type to point to
20299 the TREE node representing the appropriate (containing) type. */
20301 /* First output info about the base classes. */
20302 if (binfo)
20304 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20305 int i;
20306 tree base;
20308 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20309 gen_inheritance_die (base,
20310 (accesses ? (*accesses)[i] : access_public_node),
20311 context_die);
20314 /* Now output info about the data members and type members. */
20315 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20317 /* If we thought we were generating minimal debug info for TYPE
20318 and then changed our minds, some of the member declarations
20319 may have already been defined. Don't define them again, but
20320 do put them in the right order. */
20322 child = lookup_decl_die (member);
20323 if (child)
20324 splice_child_die (context_die, child);
20325 else
20326 gen_decl_die (member, NULL, context_die);
20329 /* We do not keep type methods in type variants. */
20330 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20331 /* Now output info about the function members (if any). */
20332 if (TYPE_METHODS (type) != error_mark_node)
20333 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20335 /* Don't include clones in the member list. */
20336 if (DECL_ABSTRACT_ORIGIN (member))
20337 continue;
20338 /* Nor constructors for anonymous classes. */
20339 if (DECL_ARTIFICIAL (member)
20340 && dwarf2_name (member, 0) == NULL)
20341 continue;
20343 child = lookup_decl_die (member);
20344 if (child)
20345 splice_child_die (context_die, child);
20346 else
20347 gen_decl_die (member, NULL, context_die);
20351 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20352 is set, we pretend that the type was never defined, so we only get the
20353 member DIEs needed by later specification DIEs. */
20355 static void
20356 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20357 enum debug_info_usage usage)
20359 /* Fill in the bound of variable-length fields in late dwarf if
20360 still incomplete. */
20361 if (TREE_ASM_WRITTEN (type)
20362 && variably_modified_type_p (type, NULL)
20363 && !early_dwarf)
20365 tree member;
20366 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20367 fill_variable_array_bounds (TREE_TYPE (member));
20368 return;
20371 dw_die_ref type_die = lookup_type_die (type);
20372 dw_die_ref scope_die = 0;
20373 int nested = 0;
20374 int complete = (TYPE_SIZE (type)
20375 && (! TYPE_STUB_DECL (type)
20376 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20377 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20378 complete = complete && should_emit_struct_debug (type, usage);
20380 if (type_die && ! complete)
20381 return;
20383 if (TYPE_CONTEXT (type) != NULL_TREE
20384 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20385 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20386 nested = 1;
20388 scope_die = scope_die_for (type, context_die);
20390 /* Generate child dies for template paramaters. */
20391 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20392 schedule_generic_params_dies_gen (type);
20394 if (! type_die || (nested && is_cu_die (scope_die)))
20395 /* First occurrence of type or toplevel definition of nested class. */
20397 dw_die_ref old_die = type_die;
20399 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20400 ? record_type_tag (type) : DW_TAG_union_type,
20401 scope_die, type);
20402 equate_type_number_to_die (type, type_die);
20403 if (old_die)
20404 add_AT_specification (type_die, old_die);
20405 else
20406 add_name_attribute (type_die, type_tag (type));
20408 else
20409 remove_AT (type_die, DW_AT_declaration);
20411 /* If this type has been completed, then give it a byte_size attribute and
20412 then give a list of members. */
20413 if (complete && !ns_decl)
20415 /* Prevent infinite recursion in cases where the type of some member of
20416 this type is expressed in terms of this type itself. */
20417 TREE_ASM_WRITTEN (type) = 1;
20418 add_byte_size_attribute (type_die, type);
20419 if (TYPE_STUB_DECL (type) != NULL_TREE)
20421 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20422 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20425 /* If the first reference to this type was as the return type of an
20426 inline function, then it may not have a parent. Fix this now. */
20427 if (type_die->die_parent == NULL)
20428 add_child_die (scope_die, type_die);
20430 push_decl_scope (type);
20431 gen_member_die (type, type_die);
20432 pop_decl_scope ();
20434 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20435 if (TYPE_ARTIFICIAL (type))
20436 add_AT_flag (type_die, DW_AT_artificial, 1);
20438 /* GNU extension: Record what type our vtable lives in. */
20439 if (TYPE_VFIELD (type))
20441 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20443 gen_type_die (vtype, context_die);
20444 add_AT_die_ref (type_die, DW_AT_containing_type,
20445 lookup_type_die (vtype));
20448 else
20450 add_AT_flag (type_die, DW_AT_declaration, 1);
20452 /* We don't need to do this for function-local types. */
20453 if (TYPE_STUB_DECL (type)
20454 && ! decl_function_context (TYPE_STUB_DECL (type)))
20455 vec_safe_push (incomplete_types, type);
20458 if (get_AT (type_die, DW_AT_name))
20459 add_pubtype (type, type_die);
20462 /* Generate a DIE for a subroutine _type_. */
20464 static void
20465 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20467 tree return_type = TREE_TYPE (type);
20468 dw_die_ref subr_die
20469 = new_die (DW_TAG_subroutine_type,
20470 scope_die_for (type, context_die), type);
20472 equate_type_number_to_die (type, subr_die);
20473 add_prototyped_attribute (subr_die, type);
20474 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20475 gen_formal_types_die (type, subr_die);
20477 if (get_AT (subr_die, DW_AT_name))
20478 add_pubtype (type, subr_die);
20481 /* Generate a DIE for a type definition. */
20483 static void
20484 gen_typedef_die (tree decl, dw_die_ref context_die)
20486 dw_die_ref type_die;
20487 tree origin;
20489 if (TREE_ASM_WRITTEN (decl))
20491 if (DECL_ORIGINAL_TYPE (decl))
20492 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20493 return;
20496 TREE_ASM_WRITTEN (decl) = 1;
20497 type_die = new_die (DW_TAG_typedef, context_die, decl);
20498 origin = decl_ultimate_origin (decl);
20499 if (origin != NULL)
20500 add_abstract_origin_attribute (type_die, origin);
20501 else
20503 tree type;
20505 add_name_and_src_coords_attributes (type_die, decl);
20506 if (DECL_ORIGINAL_TYPE (decl))
20508 type = DECL_ORIGINAL_TYPE (decl);
20510 if (type == error_mark_node)
20511 return;
20513 gcc_assert (type != TREE_TYPE (decl));
20514 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20516 else
20518 type = TREE_TYPE (decl);
20520 if (type == error_mark_node)
20521 return;
20523 if (is_naming_typedef_decl (TYPE_NAME (type)))
20525 /* Here, we are in the case of decl being a typedef naming
20526 an anonymous type, e.g:
20527 typedef struct {...} foo;
20528 In that case TREE_TYPE (decl) is not a typedef variant
20529 type and TYPE_NAME of the anonymous type is set to the
20530 TYPE_DECL of the typedef. This construct is emitted by
20531 the C++ FE.
20533 TYPE is the anonymous struct named by the typedef
20534 DECL. As we need the DW_AT_type attribute of the
20535 DW_TAG_typedef to point to the DIE of TYPE, let's
20536 generate that DIE right away. add_type_attribute
20537 called below will then pick (via lookup_type_die) that
20538 anonymous struct DIE. */
20539 if (!TREE_ASM_WRITTEN (type))
20540 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20542 /* This is a GNU Extension. We are adding a
20543 DW_AT_linkage_name attribute to the DIE of the
20544 anonymous struct TYPE. The value of that attribute
20545 is the name of the typedef decl naming the anonymous
20546 struct. This greatly eases the work of consumers of
20547 this debug info. */
20548 add_linkage_attr (lookup_type_die (type), decl);
20552 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20554 if (is_naming_typedef_decl (decl))
20555 /* We want that all subsequent calls to lookup_type_die with
20556 TYPE in argument yield the DW_TAG_typedef we have just
20557 created. */
20558 equate_type_number_to_die (type, type_die);
20560 add_accessibility_attribute (type_die, decl);
20563 if (DECL_ABSTRACT_P (decl))
20564 equate_decl_number_to_die (decl, type_die);
20566 if (get_AT (type_die, DW_AT_name))
20567 add_pubtype (decl, type_die);
20570 /* Generate a DIE for a struct, class, enum or union type. */
20572 static void
20573 gen_tagged_type_die (tree type,
20574 dw_die_ref context_die,
20575 enum debug_info_usage usage)
20577 int need_pop;
20579 if (type == NULL_TREE
20580 || !is_tagged_type (type))
20581 return;
20583 if (TREE_ASM_WRITTEN (type))
20584 need_pop = 0;
20585 /* If this is a nested type whose containing class hasn't been written
20586 out yet, writing it out will cover this one, too. This does not apply
20587 to instantiations of member class templates; they need to be added to
20588 the containing class as they are generated. FIXME: This hurts the
20589 idea of combining type decls from multiple TUs, since we can't predict
20590 what set of template instantiations we'll get. */
20591 else if (TYPE_CONTEXT (type)
20592 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20593 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20595 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20597 if (TREE_ASM_WRITTEN (type))
20598 return;
20600 /* If that failed, attach ourselves to the stub. */
20601 push_decl_scope (TYPE_CONTEXT (type));
20602 context_die = lookup_type_die (TYPE_CONTEXT (type));
20603 need_pop = 1;
20605 else if (TYPE_CONTEXT (type) != NULL_TREE
20606 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20608 /* If this type is local to a function that hasn't been written
20609 out yet, use a NULL context for now; it will be fixed up in
20610 decls_for_scope. */
20611 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20612 /* A declaration DIE doesn't count; nested types need to go in the
20613 specification. */
20614 if (context_die && is_declaration_die (context_die))
20615 context_die = NULL;
20616 need_pop = 0;
20618 else
20620 context_die = declare_in_namespace (type, context_die);
20621 need_pop = 0;
20624 if (TREE_CODE (type) == ENUMERAL_TYPE)
20626 /* This might have been written out by the call to
20627 declare_in_namespace. */
20628 if (!TREE_ASM_WRITTEN (type))
20629 gen_enumeration_type_die (type, context_die);
20631 else
20632 gen_struct_or_union_type_die (type, context_die, usage);
20634 if (need_pop)
20635 pop_decl_scope ();
20637 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20638 it up if it is ever completed. gen_*_type_die will set it for us
20639 when appropriate. */
20642 /* Generate a type description DIE. */
20644 static void
20645 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20646 enum debug_info_usage usage)
20648 struct array_descr_info info;
20650 if (type == NULL_TREE || type == error_mark_node)
20651 return;
20653 #ifdef ENABLE_CHECKING
20654 if (type)
20655 verify_type (type);
20656 #endif
20658 if (TYPE_NAME (type) != NULL_TREE
20659 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20660 && is_redundant_typedef (TYPE_NAME (type))
20661 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20662 /* The DECL of this type is a typedef we don't want to emit debug
20663 info for but we want debug info for its underlying typedef.
20664 This can happen for e.g, the injected-class-name of a C++
20665 type. */
20666 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20668 /* If TYPE is a typedef type variant, let's generate debug info
20669 for the parent typedef which TYPE is a type of. */
20670 if (typedef_variant_p (type))
20672 if (TREE_ASM_WRITTEN (type))
20673 return;
20675 /* Prevent broken recursion; we can't hand off to the same type. */
20676 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20678 /* Give typedefs the right scope. */
20679 context_die = scope_die_for (type, context_die);
20681 TREE_ASM_WRITTEN (type) = 1;
20683 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20684 return;
20687 /* If type is an anonymous tagged type named by a typedef, let's
20688 generate debug info for the typedef. */
20689 if (is_naming_typedef_decl (TYPE_NAME (type)))
20691 /* Use the DIE of the containing namespace as the parent DIE of
20692 the type description DIE we want to generate. */
20693 if (DECL_CONTEXT (TYPE_NAME (type))
20694 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20695 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20697 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20698 return;
20701 /* We are going to output a DIE to represent the unqualified version
20702 of this type (i.e. without any const or volatile qualifiers) so
20703 get the main variant (i.e. the unqualified version) of this type
20704 now. (Vectors are special because the debugging info is in the
20705 cloned type itself). */
20706 if (TREE_CODE (type) != VECTOR_TYPE)
20707 type = type_main_variant (type);
20709 /* If this is an array type with hidden descriptor, handle it first. */
20710 if (!TREE_ASM_WRITTEN (type)
20711 && lang_hooks.types.get_array_descr_info)
20713 memset (&info, 0, sizeof (info));
20714 if (lang_hooks.types.get_array_descr_info (type, &info))
20716 gen_descr_array_type_die (type, &info, context_die);
20717 TREE_ASM_WRITTEN (type) = 1;
20718 return;
20722 if (TREE_ASM_WRITTEN (type))
20724 /* Variable-length types may be incomplete even if
20725 TREE_ASM_WRITTEN. For such types, fall through to
20726 gen_array_type_die() and possibly fill in
20727 DW_AT_{upper,lower}_bound attributes. */
20728 if ((TREE_CODE (type) != ARRAY_TYPE
20729 && TREE_CODE (type) != RECORD_TYPE
20730 && TREE_CODE (type) != UNION_TYPE
20731 && TREE_CODE (type) != QUAL_UNION_TYPE)
20732 || !variably_modified_type_p (type, NULL))
20733 return;
20736 switch (TREE_CODE (type))
20738 case ERROR_MARK:
20739 break;
20741 case POINTER_TYPE:
20742 case REFERENCE_TYPE:
20743 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20744 ensures that the gen_type_die recursion will terminate even if the
20745 type is recursive. Recursive types are possible in Ada. */
20746 /* ??? We could perhaps do this for all types before the switch
20747 statement. */
20748 TREE_ASM_WRITTEN (type) = 1;
20750 /* For these types, all that is required is that we output a DIE (or a
20751 set of DIEs) to represent the "basis" type. */
20752 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20753 DINFO_USAGE_IND_USE);
20754 break;
20756 case OFFSET_TYPE:
20757 /* This code is used for C++ pointer-to-data-member types.
20758 Output a description of the relevant class type. */
20759 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20760 DINFO_USAGE_IND_USE);
20762 /* Output a description of the type of the object pointed to. */
20763 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20764 DINFO_USAGE_IND_USE);
20766 /* Now output a DIE to represent this pointer-to-data-member type
20767 itself. */
20768 gen_ptr_to_mbr_type_die (type, context_die);
20769 break;
20771 case FUNCTION_TYPE:
20772 /* Force out return type (in case it wasn't forced out already). */
20773 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20774 DINFO_USAGE_DIR_USE);
20775 gen_subroutine_type_die (type, context_die);
20776 break;
20778 case METHOD_TYPE:
20779 /* Force out return type (in case it wasn't forced out already). */
20780 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20781 DINFO_USAGE_DIR_USE);
20782 gen_subroutine_type_die (type, context_die);
20783 break;
20785 case ARRAY_TYPE:
20786 case VECTOR_TYPE:
20787 gen_array_type_die (type, context_die);
20788 break;
20790 case ENUMERAL_TYPE:
20791 case RECORD_TYPE:
20792 case UNION_TYPE:
20793 case QUAL_UNION_TYPE:
20794 gen_tagged_type_die (type, context_die, usage);
20795 return;
20797 case VOID_TYPE:
20798 case INTEGER_TYPE:
20799 case REAL_TYPE:
20800 case FIXED_POINT_TYPE:
20801 case COMPLEX_TYPE:
20802 case BOOLEAN_TYPE:
20803 case POINTER_BOUNDS_TYPE:
20804 /* No DIEs needed for fundamental types. */
20805 break;
20807 case NULLPTR_TYPE:
20808 case LANG_TYPE:
20809 /* Just use DW_TAG_unspecified_type. */
20811 dw_die_ref type_die = lookup_type_die (type);
20812 if (type_die == NULL)
20814 tree name = TYPE_IDENTIFIER (type);
20815 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20816 type);
20817 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20818 equate_type_number_to_die (type, type_die);
20821 break;
20823 default:
20824 if (is_cxx_auto (type))
20826 tree name = TYPE_IDENTIFIER (type);
20827 dw_die_ref *die = (name == get_identifier ("auto")
20828 ? &auto_die : &decltype_auto_die);
20829 if (!*die)
20831 *die = new_die (DW_TAG_unspecified_type,
20832 comp_unit_die (), NULL_TREE);
20833 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20835 equate_type_number_to_die (type, *die);
20836 break;
20838 gcc_unreachable ();
20841 TREE_ASM_WRITTEN (type) = 1;
20844 static void
20845 gen_type_die (tree type, dw_die_ref context_die)
20847 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20850 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20851 things which are local to the given block. */
20853 static void
20854 gen_block_die (tree stmt, dw_die_ref context_die)
20856 int must_output_die = 0;
20857 bool inlined_func;
20859 /* Ignore blocks that are NULL. */
20860 if (stmt == NULL_TREE)
20861 return;
20863 inlined_func = inlined_function_outer_scope_p (stmt);
20865 /* If the block is one fragment of a non-contiguous block, do not
20866 process the variables, since they will have been done by the
20867 origin block. Do process subblocks. */
20868 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20870 tree sub;
20872 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20873 gen_block_die (sub, context_die);
20875 return;
20878 /* Determine if we need to output any Dwarf DIEs at all to represent this
20879 block. */
20880 if (inlined_func)
20881 /* The outer scopes for inlinings *must* always be represented. We
20882 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20883 must_output_die = 1;
20884 else
20886 /* Determine if this block directly contains any "significant"
20887 local declarations which we will need to output DIEs for. */
20888 if (debug_info_level > DINFO_LEVEL_TERSE)
20889 /* We are not in terse mode so *any* local declaration counts
20890 as being a "significant" one. */
20891 must_output_die = ((BLOCK_VARS (stmt) != NULL
20892 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20893 && (TREE_USED (stmt)
20894 || TREE_ASM_WRITTEN (stmt)
20895 || BLOCK_ABSTRACT (stmt)));
20896 else if ((TREE_USED (stmt)
20897 || TREE_ASM_WRITTEN (stmt)
20898 || BLOCK_ABSTRACT (stmt))
20899 && !dwarf2out_ignore_block (stmt))
20900 must_output_die = 1;
20903 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20904 DIE for any block which contains no significant local declarations at
20905 all. Rather, in such cases we just call `decls_for_scope' so that any
20906 needed Dwarf info for any sub-blocks will get properly generated. Note
20907 that in terse mode, our definition of what constitutes a "significant"
20908 local declaration gets restricted to include only inlined function
20909 instances and local (nested) function definitions. */
20910 if (must_output_die)
20912 if (inlined_func)
20914 /* If STMT block is abstract, that means we have been called
20915 indirectly from dwarf2out_abstract_function.
20916 That function rightfully marks the descendent blocks (of
20917 the abstract function it is dealing with) as being abstract,
20918 precisely to prevent us from emitting any
20919 DW_TAG_inlined_subroutine DIE as a descendent
20920 of an abstract function instance. So in that case, we should
20921 not call gen_inlined_subroutine_die.
20923 Later though, when cgraph asks dwarf2out to emit info
20924 for the concrete instance of the function decl into which
20925 the concrete instance of STMT got inlined, the later will lead
20926 to the generation of a DW_TAG_inlined_subroutine DIE. */
20927 if (! BLOCK_ABSTRACT (stmt))
20928 gen_inlined_subroutine_die (stmt, context_die);
20930 else
20931 gen_lexical_block_die (stmt, context_die);
20933 else
20934 decls_for_scope (stmt, context_die);
20937 /* Process variable DECL (or variable with origin ORIGIN) within
20938 block STMT and add it to CONTEXT_DIE. */
20939 static void
20940 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20942 dw_die_ref die;
20943 tree decl_or_origin = decl ? decl : origin;
20945 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20946 die = lookup_decl_die (decl_or_origin);
20947 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20948 && TYPE_DECL_IS_STUB (decl_or_origin))
20949 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20950 else
20951 die = NULL;
20953 if (die != NULL && die->die_parent == NULL)
20954 add_child_die (context_die, die);
20955 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20957 if (early_dwarf)
20958 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20959 stmt, context_die);
20961 else
20962 gen_decl_die (decl, origin, context_die);
20965 /* Generate all of the decls declared within a given scope and (recursively)
20966 all of its sub-blocks. */
20968 static void
20969 decls_for_scope (tree stmt, dw_die_ref context_die)
20971 tree decl;
20972 unsigned int i;
20973 tree subblocks;
20975 /* Ignore NULL blocks. */
20976 if (stmt == NULL_TREE)
20977 return;
20979 /* Output the DIEs to represent all of the data objects and typedefs
20980 declared directly within this block but not within any nested
20981 sub-blocks. Also, nested function and tag DIEs have been
20982 generated with a parent of NULL; fix that up now. We don't
20983 have to do this if we're at -g1. */
20984 if (debug_info_level > DINFO_LEVEL_TERSE)
20986 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20987 process_scope_var (stmt, decl, NULL_TREE, context_die);
20988 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20989 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20990 context_die);
20993 /* Even if we're at -g1, we need to process the subblocks in order to get
20994 inlined call information. */
20996 /* Output the DIEs to represent all sub-blocks (and the items declared
20997 therein) of this block. */
20998 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20999 subblocks != NULL;
21000 subblocks = BLOCK_CHAIN (subblocks))
21001 gen_block_die (subblocks, context_die);
21004 /* Is this a typedef we can avoid emitting? */
21006 static inline int
21007 is_redundant_typedef (const_tree decl)
21009 if (TYPE_DECL_IS_STUB (decl))
21010 return 1;
21012 if (DECL_ARTIFICIAL (decl)
21013 && DECL_CONTEXT (decl)
21014 && is_tagged_type (DECL_CONTEXT (decl))
21015 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21016 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21017 /* Also ignore the artificial member typedef for the class name. */
21018 return 1;
21020 return 0;
21023 /* Return TRUE if TYPE is a typedef that names a type for linkage
21024 purposes. This kind of typedefs is produced by the C++ FE for
21025 constructs like:
21027 typedef struct {...} foo;
21029 In that case, there is no typedef variant type produced for foo.
21030 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21031 struct type. */
21033 static bool
21034 is_naming_typedef_decl (const_tree decl)
21036 if (decl == NULL_TREE
21037 || TREE_CODE (decl) != TYPE_DECL
21038 || !is_tagged_type (TREE_TYPE (decl))
21039 || DECL_IS_BUILTIN (decl)
21040 || is_redundant_typedef (decl)
21041 /* It looks like Ada produces TYPE_DECLs that are very similar
21042 to C++ naming typedefs but that have different
21043 semantics. Let's be specific to c++ for now. */
21044 || !is_cxx ())
21045 return FALSE;
21047 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21048 && TYPE_NAME (TREE_TYPE (decl)) == decl
21049 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21050 != TYPE_NAME (TREE_TYPE (decl))));
21053 /* Looks up the DIE for a context. */
21055 static inline dw_die_ref
21056 lookup_context_die (tree context)
21058 if (context)
21060 /* Find die that represents this context. */
21061 if (TYPE_P (context))
21063 context = TYPE_MAIN_VARIANT (context);
21064 dw_die_ref ctx = lookup_type_die (context);
21065 if (!ctx)
21066 return NULL;
21067 return strip_naming_typedef (context, ctx);
21069 else
21070 return lookup_decl_die (context);
21072 return comp_unit_die ();
21075 /* Returns the DIE for a context. */
21077 static inline dw_die_ref
21078 get_context_die (tree context)
21080 if (context)
21082 /* Find die that represents this context. */
21083 if (TYPE_P (context))
21085 context = TYPE_MAIN_VARIANT (context);
21086 return strip_naming_typedef (context, force_type_die (context));
21088 else
21089 return force_decl_die (context);
21091 return comp_unit_die ();
21094 /* Returns the DIE for decl. A DIE will always be returned. */
21096 static dw_die_ref
21097 force_decl_die (tree decl)
21099 dw_die_ref decl_die;
21100 unsigned saved_external_flag;
21101 tree save_fn = NULL_TREE;
21102 decl_die = lookup_decl_die (decl);
21103 if (!decl_die)
21105 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21107 decl_die = lookup_decl_die (decl);
21108 if (decl_die)
21109 return decl_die;
21111 switch (TREE_CODE (decl))
21113 case FUNCTION_DECL:
21114 /* Clear current_function_decl, so that gen_subprogram_die thinks
21115 that this is a declaration. At this point, we just want to force
21116 declaration die. */
21117 save_fn = current_function_decl;
21118 current_function_decl = NULL_TREE;
21119 gen_subprogram_die (decl, context_die);
21120 current_function_decl = save_fn;
21121 break;
21123 case VAR_DECL:
21124 /* Set external flag to force declaration die. Restore it after
21125 gen_decl_die() call. */
21126 saved_external_flag = DECL_EXTERNAL (decl);
21127 DECL_EXTERNAL (decl) = 1;
21128 gen_decl_die (decl, NULL, context_die);
21129 DECL_EXTERNAL (decl) = saved_external_flag;
21130 break;
21132 case NAMESPACE_DECL:
21133 if (dwarf_version >= 3 || !dwarf_strict)
21134 dwarf2out_decl (decl);
21135 else
21136 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21137 decl_die = comp_unit_die ();
21138 break;
21140 case TRANSLATION_UNIT_DECL:
21141 decl_die = comp_unit_die ();
21142 break;
21144 default:
21145 gcc_unreachable ();
21148 /* We should be able to find the DIE now. */
21149 if (!decl_die)
21150 decl_die = lookup_decl_die (decl);
21151 gcc_assert (decl_die);
21154 return decl_die;
21157 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21158 always returned. */
21160 static dw_die_ref
21161 force_type_die (tree type)
21163 dw_die_ref type_die;
21165 type_die = lookup_type_die (type);
21166 if (!type_die)
21168 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21170 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21171 context_die);
21172 gcc_assert (type_die);
21174 return type_die;
21177 /* Force out any required namespaces to be able to output DECL,
21178 and return the new context_die for it, if it's changed. */
21180 static dw_die_ref
21181 setup_namespace_context (tree thing, dw_die_ref context_die)
21183 tree context = (DECL_P (thing)
21184 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21185 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21186 /* Force out the namespace. */
21187 context_die = force_decl_die (context);
21189 return context_die;
21192 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21193 type) within its namespace, if appropriate.
21195 For compatibility with older debuggers, namespace DIEs only contain
21196 declarations; all definitions are emitted at CU scope, with
21197 DW_AT_specification pointing to the declaration (like with class
21198 members). */
21200 static dw_die_ref
21201 declare_in_namespace (tree thing, dw_die_ref context_die)
21203 dw_die_ref ns_context;
21205 if (debug_info_level <= DINFO_LEVEL_TERSE)
21206 return context_die;
21208 /* External declarations in the local scope only need to be emitted
21209 once, not once in the namespace and once in the scope.
21211 This avoids declaring the `extern' below in the
21212 namespace DIE as well as in the innermost scope:
21214 namespace S
21216 int i=5;
21217 int foo()
21219 int i=8;
21220 extern int i;
21221 return i;
21225 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21226 return context_die;
21228 /* If this decl is from an inlined function, then don't try to emit it in its
21229 namespace, as we will get confused. It would have already been emitted
21230 when the abstract instance of the inline function was emitted anyways. */
21231 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21232 return context_die;
21234 ns_context = setup_namespace_context (thing, context_die);
21236 if (ns_context != context_die)
21238 if (is_fortran ())
21239 return ns_context;
21240 if (DECL_P (thing))
21241 gen_decl_die (thing, NULL, ns_context);
21242 else
21243 gen_type_die (thing, ns_context);
21245 return context_die;
21248 /* Generate a DIE for a namespace or namespace alias. */
21250 static void
21251 gen_namespace_die (tree decl, dw_die_ref context_die)
21253 dw_die_ref namespace_die;
21255 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21256 they are an alias of. */
21257 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21259 /* Output a real namespace or module. */
21260 context_die = setup_namespace_context (decl, comp_unit_die ());
21261 namespace_die = new_die (is_fortran ()
21262 ? DW_TAG_module : DW_TAG_namespace,
21263 context_die, decl);
21264 /* For Fortran modules defined in different CU don't add src coords. */
21265 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21267 const char *name = dwarf2_name (decl, 0);
21268 if (name)
21269 add_name_attribute (namespace_die, name);
21271 else
21272 add_name_and_src_coords_attributes (namespace_die, decl);
21273 if (DECL_EXTERNAL (decl))
21274 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21275 equate_decl_number_to_die (decl, namespace_die);
21277 else
21279 /* Output a namespace alias. */
21281 /* Force out the namespace we are an alias of, if necessary. */
21282 dw_die_ref origin_die
21283 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21285 if (DECL_FILE_SCOPE_P (decl)
21286 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21287 context_die = setup_namespace_context (decl, comp_unit_die ());
21288 /* Now create the namespace alias DIE. */
21289 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21290 add_name_and_src_coords_attributes (namespace_die, decl);
21291 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21292 equate_decl_number_to_die (decl, namespace_die);
21294 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21295 if (want_pubnames ())
21296 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21299 /* Generate Dwarf debug information for a decl described by DECL.
21300 The return value is currently only meaningful for PARM_DECLs,
21301 for all other decls it returns NULL. */
21303 static dw_die_ref
21304 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21306 tree decl_or_origin = decl ? decl : origin;
21307 tree class_origin = NULL, ultimate_origin;
21309 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21310 return NULL;
21312 /* Ignore pointer bounds decls. */
21313 if (DECL_P (decl_or_origin)
21314 && TREE_TYPE (decl_or_origin)
21315 && POINTER_BOUNDS_P (decl_or_origin))
21316 return NULL;
21318 switch (TREE_CODE (decl_or_origin))
21320 case ERROR_MARK:
21321 break;
21323 case CONST_DECL:
21324 if (!is_fortran () && !is_ada ())
21326 /* The individual enumerators of an enum type get output when we output
21327 the Dwarf representation of the relevant enum type itself. */
21328 break;
21331 /* Emit its type. */
21332 gen_type_die (TREE_TYPE (decl), context_die);
21334 /* And its containing namespace. */
21335 context_die = declare_in_namespace (decl, context_die);
21337 gen_const_die (decl, context_die);
21338 break;
21340 case FUNCTION_DECL:
21341 /* Don't output any DIEs to represent mere function declarations,
21342 unless they are class members or explicit block externs. */
21343 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21344 && DECL_FILE_SCOPE_P (decl_or_origin)
21345 && (current_function_decl == NULL_TREE
21346 || DECL_ARTIFICIAL (decl_or_origin)))
21347 break;
21349 #if 0
21350 /* FIXME */
21351 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21352 on local redeclarations of global functions. That seems broken. */
21353 if (current_function_decl != decl)
21354 /* This is only a declaration. */;
21355 #endif
21357 /* If we're emitting a clone, emit info for the abstract instance. */
21358 if (origin || DECL_ORIGIN (decl) != decl)
21359 dwarf2out_abstract_function (origin
21360 ? DECL_ORIGIN (origin)
21361 : DECL_ABSTRACT_ORIGIN (decl));
21363 /* If we're emitting an out-of-line copy of an inline function,
21364 emit info for the abstract instance and set up to refer to it. */
21365 else if (cgraph_function_possibly_inlined_p (decl)
21366 && ! DECL_ABSTRACT_P (decl)
21367 && ! class_or_namespace_scope_p (context_die)
21368 /* dwarf2out_abstract_function won't emit a die if this is just
21369 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21370 that case, because that works only if we have a die. */
21371 && DECL_INITIAL (decl) != NULL_TREE)
21373 dwarf2out_abstract_function (decl);
21374 set_decl_origin_self (decl);
21377 /* Otherwise we're emitting the primary DIE for this decl. */
21378 else if (debug_info_level > DINFO_LEVEL_TERSE)
21380 /* Before we describe the FUNCTION_DECL itself, make sure that we
21381 have its containing type. */
21382 if (!origin)
21383 origin = decl_class_context (decl);
21384 if (origin != NULL_TREE)
21385 gen_type_die (origin, context_die);
21387 /* And its return type. */
21388 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21390 /* And its virtual context. */
21391 if (DECL_VINDEX (decl) != NULL_TREE)
21392 gen_type_die (DECL_CONTEXT (decl), context_die);
21394 /* Make sure we have a member DIE for decl. */
21395 if (origin != NULL_TREE)
21396 gen_type_die_for_member (origin, decl, context_die);
21398 /* And its containing namespace. */
21399 context_die = declare_in_namespace (decl, context_die);
21402 /* Now output a DIE to represent the function itself. */
21403 if (decl)
21404 gen_subprogram_die (decl, context_die);
21405 break;
21407 case TYPE_DECL:
21408 /* If we are in terse mode, don't generate any DIEs to represent any
21409 actual typedefs. */
21410 if (debug_info_level <= DINFO_LEVEL_TERSE)
21411 break;
21413 /* In the special case of a TYPE_DECL node representing the declaration
21414 of some type tag, if the given TYPE_DECL is marked as having been
21415 instantiated from some other (original) TYPE_DECL node (e.g. one which
21416 was generated within the original definition of an inline function) we
21417 used to generate a special (abbreviated) DW_TAG_structure_type,
21418 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21419 should be actually referencing those DIEs, as variable DIEs with that
21420 type would be emitted already in the abstract origin, so it was always
21421 removed during unused type prunning. Don't add anything in this
21422 case. */
21423 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21424 break;
21426 if (is_redundant_typedef (decl))
21427 gen_type_die (TREE_TYPE (decl), context_die);
21428 else
21429 /* Output a DIE to represent the typedef itself. */
21430 gen_typedef_die (decl, context_die);
21431 break;
21433 case LABEL_DECL:
21434 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21435 gen_label_die (decl, context_die);
21436 break;
21438 case VAR_DECL:
21439 case RESULT_DECL:
21440 /* If we are in terse mode, don't generate any DIEs to represent any
21441 variable declarations or definitions. */
21442 if (debug_info_level <= DINFO_LEVEL_TERSE)
21443 break;
21445 /* Output any DIEs that are needed to specify the type of this data
21446 object. */
21447 if (decl_by_reference_p (decl_or_origin))
21448 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21449 else
21450 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21452 /* And its containing type. */
21453 class_origin = decl_class_context (decl_or_origin);
21454 if (class_origin != NULL_TREE)
21455 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21457 /* And its containing namespace. */
21458 context_die = declare_in_namespace (decl_or_origin, context_die);
21460 /* Now output the DIE to represent the data object itself. This gets
21461 complicated because of the possibility that the VAR_DECL really
21462 represents an inlined instance of a formal parameter for an inline
21463 function. */
21464 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21465 if (ultimate_origin != NULL_TREE
21466 && TREE_CODE (ultimate_origin) == PARM_DECL)
21467 gen_formal_parameter_die (decl, origin,
21468 true /* Emit name attribute. */,
21469 context_die);
21470 else
21471 gen_variable_die (decl, origin, context_die);
21472 break;
21474 case FIELD_DECL:
21475 /* Ignore the nameless fields that are used to skip bits but handle C++
21476 anonymous unions and structs. */
21477 if (DECL_NAME (decl) != NULL_TREE
21478 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21479 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21481 gen_type_die (member_declared_type (decl), context_die);
21482 gen_field_die (decl, context_die);
21484 break;
21486 case PARM_DECL:
21487 if (DECL_BY_REFERENCE (decl_or_origin))
21488 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21489 else
21490 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21491 return gen_formal_parameter_die (decl, origin,
21492 true /* Emit name attribute. */,
21493 context_die);
21495 case NAMESPACE_DECL:
21496 case IMPORTED_DECL:
21497 if (dwarf_version >= 3 || !dwarf_strict)
21498 gen_namespace_die (decl, context_die);
21499 break;
21501 case NAMELIST_DECL:
21502 gen_namelist_decl (DECL_NAME (decl), context_die,
21503 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21504 break;
21506 default:
21507 /* Probably some frontend-internal decl. Assume we don't care. */
21508 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21509 break;
21512 return NULL;
21515 /* Output initial debug information for global DECL. Called at the
21516 end of the parsing process.
21518 This is the initial debug generation process. As such, the DIEs
21519 generated may be incomplete. A later debug generation pass
21520 (dwarf2out_late_global_decl) will augment the information generated
21521 in this pass (e.g., with complete location info). */
21523 static void
21524 dwarf2out_early_global_decl (tree decl)
21526 set_early_dwarf s;
21528 /* gen_decl_die() will set DECL_ABSTRACT because
21529 cgraph_function_possibly_inlined_p() returns true. This is in
21530 turn will cause DW_AT_inline attributes to be set.
21532 This happens because at early dwarf generation, there is no
21533 cgraph information, causing cgraph_function_possibly_inlined_p()
21534 to return true. Trick cgraph_function_possibly_inlined_p()
21535 while we generate dwarf early. */
21536 bool save = symtab->global_info_ready;
21537 symtab->global_info_ready = true;
21539 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21540 other DECLs and they can point to template types or other things
21541 that dwarf2out can't handle when done via dwarf2out_decl. */
21542 if (TREE_CODE (decl) != TYPE_DECL
21543 && TREE_CODE (decl) != PARM_DECL)
21545 tree save_fndecl = current_function_decl;
21546 if (TREE_CODE (decl) == FUNCTION_DECL)
21548 /* No cfun means the symbol has no body, so there's nothing
21549 to emit. */
21550 if (!DECL_STRUCT_FUNCTION (decl))
21551 goto early_decl_exit;
21553 current_function_decl = decl;
21555 dwarf2out_decl (decl);
21556 if (TREE_CODE (decl) == FUNCTION_DECL)
21557 current_function_decl = save_fndecl;
21559 early_decl_exit:
21560 symtab->global_info_ready = save;
21563 /* Output debug information for global decl DECL. Called from
21564 toplev.c after compilation proper has finished. */
21566 static void
21567 dwarf2out_late_global_decl (tree decl)
21569 /* Output any global decls we missed or fill-in any location
21570 information we were unable to determine on the first pass.
21572 Skip over functions because they were handled by the
21573 debug_hooks->function_decl() call in rest_of_handle_final. */
21574 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21575 && !POINTER_BOUNDS_P (decl))
21576 dwarf2out_decl (decl);
21579 /* Output debug information for type decl DECL. Called from toplev.c
21580 and from language front ends (to record built-in types). */
21581 static void
21582 dwarf2out_type_decl (tree decl, int local)
21584 if (!local)
21586 set_early_dwarf s;
21587 dwarf2out_decl (decl);
21591 /* Output debug information for imported module or decl DECL.
21592 NAME is non-NULL name in the lexical block if the decl has been renamed.
21593 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21594 that DECL belongs to.
21595 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21596 static void
21597 dwarf2out_imported_module_or_decl_1 (tree decl,
21598 tree name,
21599 tree lexical_block,
21600 dw_die_ref lexical_block_die)
21602 expanded_location xloc;
21603 dw_die_ref imported_die = NULL;
21604 dw_die_ref at_import_die;
21606 if (TREE_CODE (decl) == IMPORTED_DECL)
21608 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21609 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21610 gcc_assert (decl);
21612 else
21613 xloc = expand_location (input_location);
21615 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21617 at_import_die = force_type_die (TREE_TYPE (decl));
21618 /* For namespace N { typedef void T; } using N::T; base_type_die
21619 returns NULL, but DW_TAG_imported_declaration requires
21620 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21621 if (!at_import_die)
21623 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21624 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21625 at_import_die = lookup_type_die (TREE_TYPE (decl));
21626 gcc_assert (at_import_die);
21629 else
21631 at_import_die = lookup_decl_die (decl);
21632 if (!at_import_die)
21634 /* If we're trying to avoid duplicate debug info, we may not have
21635 emitted the member decl for this field. Emit it now. */
21636 if (TREE_CODE (decl) == FIELD_DECL)
21638 tree type = DECL_CONTEXT (decl);
21640 if (TYPE_CONTEXT (type)
21641 && TYPE_P (TYPE_CONTEXT (type))
21642 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21643 DINFO_USAGE_DIR_USE))
21644 return;
21645 gen_type_die_for_member (type, decl,
21646 get_context_die (TYPE_CONTEXT (type)));
21648 if (TREE_CODE (decl) == NAMELIST_DECL)
21649 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21650 get_context_die (DECL_CONTEXT (decl)),
21651 NULL_TREE);
21652 else
21653 at_import_die = force_decl_die (decl);
21657 if (TREE_CODE (decl) == NAMESPACE_DECL)
21659 if (dwarf_version >= 3 || !dwarf_strict)
21660 imported_die = new_die (DW_TAG_imported_module,
21661 lexical_block_die,
21662 lexical_block);
21663 else
21664 return;
21666 else
21667 imported_die = new_die (DW_TAG_imported_declaration,
21668 lexical_block_die,
21669 lexical_block);
21671 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21672 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21673 if (name)
21674 add_AT_string (imported_die, DW_AT_name,
21675 IDENTIFIER_POINTER (name));
21676 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21679 /* Output debug information for imported module or decl DECL.
21680 NAME is non-NULL name in context if the decl has been renamed.
21681 CHILD is true if decl is one of the renamed decls as part of
21682 importing whole module. */
21684 static void
21685 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21686 bool child)
21688 /* dw_die_ref at_import_die; */
21689 dw_die_ref scope_die;
21691 if (debug_info_level <= DINFO_LEVEL_TERSE)
21692 return;
21694 gcc_assert (decl);
21696 set_early_dwarf s;
21698 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21699 We need decl DIE for reference and scope die. First, get DIE for the decl
21700 itself. */
21702 /* Get the scope die for decl context. Use comp_unit_die for global module
21703 or decl. If die is not found for non globals, force new die. */
21704 if (context
21705 && TYPE_P (context)
21706 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21707 return;
21709 if (!(dwarf_version >= 3 || !dwarf_strict))
21710 return;
21712 scope_die = get_context_die (context);
21714 if (child)
21716 gcc_assert (scope_die->die_child);
21717 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21718 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21719 scope_die = scope_die->die_child;
21722 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21723 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21726 /* Output debug information for namelists. */
21728 static dw_die_ref
21729 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21731 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21732 tree value;
21733 unsigned i;
21735 if (debug_info_level <= DINFO_LEVEL_TERSE)
21736 return NULL;
21738 gcc_assert (scope_die != NULL);
21739 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21740 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21742 /* If there are no item_decls, we have a nondefining namelist, e.g.
21743 with USE association; hence, set DW_AT_declaration. */
21744 if (item_decls == NULL_TREE)
21746 add_AT_flag (nml_die, DW_AT_declaration, 1);
21747 return nml_die;
21750 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21752 nml_item_ref_die = lookup_decl_die (value);
21753 if (!nml_item_ref_die)
21754 nml_item_ref_die = force_decl_die (value);
21756 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21757 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21759 return nml_die;
21763 /* Write the debugging output for DECL and return the DIE. */
21765 static void
21766 dwarf2out_decl (tree decl)
21768 dw_die_ref context_die = comp_unit_die ();
21770 switch (TREE_CODE (decl))
21772 case ERROR_MARK:
21773 return;
21775 case FUNCTION_DECL:
21776 /* What we would really like to do here is to filter out all mere
21777 file-scope declarations of file-scope functions which are never
21778 referenced later within this translation unit (and keep all of ones
21779 that *are* referenced later on) but we aren't clairvoyant, so we have
21780 no idea which functions will be referenced in the future (i.e. later
21781 on within the current translation unit). So here we just ignore all
21782 file-scope function declarations which are not also definitions. If
21783 and when the debugger needs to know something about these functions,
21784 it will have to hunt around and find the DWARF information associated
21785 with the definition of the function.
21787 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21788 nodes represent definitions and which ones represent mere
21789 declarations. We have to check DECL_INITIAL instead. That's because
21790 the C front-end supports some weird semantics for "extern inline"
21791 function definitions. These can get inlined within the current
21792 translation unit (and thus, we need to generate Dwarf info for their
21793 abstract instances so that the Dwarf info for the concrete inlined
21794 instances can have something to refer to) but the compiler never
21795 generates any out-of-lines instances of such things (despite the fact
21796 that they *are* definitions).
21798 The important point is that the C front-end marks these "extern
21799 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21800 them anyway. Note that the C++ front-end also plays some similar games
21801 for inline function definitions appearing within include files which
21802 also contain `#pragma interface' pragmas.
21804 If we are called from dwarf2out_abstract_function output a DIE
21805 anyway. We can end up here this way with early inlining and LTO
21806 where the inlined function is output in a different LTRANS unit
21807 or not at all. */
21808 if (DECL_INITIAL (decl) == NULL_TREE
21809 && ! DECL_ABSTRACT_P (decl))
21810 return;
21812 /* If we're a nested function, initially use a parent of NULL; if we're
21813 a plain function, this will be fixed up in decls_for_scope. If
21814 we're a method, it will be ignored, since we already have a DIE. */
21815 if (decl_function_context (decl)
21816 /* But if we're in terse mode, we don't care about scope. */
21817 && debug_info_level > DINFO_LEVEL_TERSE)
21818 context_die = NULL;
21819 break;
21821 case VAR_DECL:
21822 /* For local statics lookup proper context die. */
21823 if (local_function_static (decl))
21824 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21826 /* If we are in terse mode, don't generate any DIEs to represent any
21827 variable declarations or definitions. */
21828 if (debug_info_level <= DINFO_LEVEL_TERSE)
21829 return;
21830 break;
21832 case CONST_DECL:
21833 if (debug_info_level <= DINFO_LEVEL_TERSE)
21834 return;
21835 if (!is_fortran () && !is_ada ())
21836 return;
21837 if (TREE_STATIC (decl) && decl_function_context (decl))
21838 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21839 break;
21841 case NAMESPACE_DECL:
21842 case IMPORTED_DECL:
21843 if (debug_info_level <= DINFO_LEVEL_TERSE)
21844 return;
21845 if (lookup_decl_die (decl) != NULL)
21846 return;
21847 break;
21849 case TYPE_DECL:
21850 /* Don't emit stubs for types unless they are needed by other DIEs. */
21851 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21852 return;
21854 /* Don't bother trying to generate any DIEs to represent any of the
21855 normal built-in types for the language we are compiling. */
21856 if (DECL_IS_BUILTIN (decl))
21857 return;
21859 /* If we are in terse mode, don't generate any DIEs for types. */
21860 if (debug_info_level <= DINFO_LEVEL_TERSE)
21861 return;
21863 /* If we're a function-scope tag, initially use a parent of NULL;
21864 this will be fixed up in decls_for_scope. */
21865 if (decl_function_context (decl))
21866 context_die = NULL;
21868 break;
21870 case NAMELIST_DECL:
21871 break;
21873 default:
21874 return;
21877 gen_decl_die (decl, NULL, context_die);
21879 dw_die_ref die = lookup_decl_die (decl);
21880 if (die)
21881 check_die (die);
21884 /* Write the debugging output for DECL. */
21886 static void
21887 dwarf2out_function_decl (tree decl)
21889 dwarf2out_decl (decl);
21890 call_arg_locations = NULL;
21891 call_arg_loc_last = NULL;
21892 call_site_count = -1;
21893 tail_call_site_count = -1;
21894 decl_loc_table->empty ();
21895 cached_dw_loc_list_table->empty ();
21898 /* Output a marker (i.e. a label) for the beginning of the generated code for
21899 a lexical block. */
21901 static void
21902 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21903 unsigned int blocknum)
21905 switch_to_section (current_function_section ());
21906 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21909 /* Output a marker (i.e. a label) for the end of the generated code for a
21910 lexical block. */
21912 static void
21913 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21915 switch_to_section (current_function_section ());
21916 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21919 /* Returns nonzero if it is appropriate not to emit any debugging
21920 information for BLOCK, because it doesn't contain any instructions.
21922 Don't allow this for blocks with nested functions or local classes
21923 as we would end up with orphans, and in the presence of scheduling
21924 we may end up calling them anyway. */
21926 static bool
21927 dwarf2out_ignore_block (const_tree block)
21929 tree decl;
21930 unsigned int i;
21932 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21933 if (TREE_CODE (decl) == FUNCTION_DECL
21934 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21935 return 0;
21936 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21938 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21939 if (TREE_CODE (decl) == FUNCTION_DECL
21940 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21941 return 0;
21944 return 1;
21947 /* Hash table routines for file_hash. */
21949 bool
21950 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21952 return filename_cmp (p1->filename, p2) == 0;
21955 hashval_t
21956 dwarf_file_hasher::hash (dwarf_file_data *p)
21958 return htab_hash_string (p->filename);
21961 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21962 dwarf2out.c) and return its "index". The index of each (known) filename is
21963 just a unique number which is associated with only that one filename. We
21964 need such numbers for the sake of generating labels (in the .debug_sfnames
21965 section) and references to those files numbers (in the .debug_srcinfo
21966 and.debug_macinfo sections). If the filename given as an argument is not
21967 found in our current list, add it to the list and assign it the next
21968 available unique index number. */
21970 static struct dwarf_file_data *
21971 lookup_filename (const char *file_name)
21973 struct dwarf_file_data * created;
21975 if (!file_name)
21976 return NULL;
21978 dwarf_file_data **slot
21979 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21980 INSERT);
21981 if (*slot)
21982 return *slot;
21984 created = ggc_alloc<dwarf_file_data> ();
21985 created->filename = file_name;
21986 created->emitted_number = 0;
21987 *slot = created;
21988 return created;
21991 /* If the assembler will construct the file table, then translate the compiler
21992 internal file table number into the assembler file table number, and emit
21993 a .file directive if we haven't already emitted one yet. The file table
21994 numbers are different because we prune debug info for unused variables and
21995 types, which may include filenames. */
21997 static int
21998 maybe_emit_file (struct dwarf_file_data * fd)
22000 if (! fd->emitted_number)
22002 if (last_emitted_file)
22003 fd->emitted_number = last_emitted_file->emitted_number + 1;
22004 else
22005 fd->emitted_number = 1;
22006 last_emitted_file = fd;
22008 if (DWARF2_ASM_LINE_DEBUG_INFO)
22010 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22011 output_quoted_string (asm_out_file,
22012 remap_debug_filename (fd->filename));
22013 fputc ('\n', asm_out_file);
22017 return fd->emitted_number;
22020 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22021 That generation should happen after function debug info has been
22022 generated. The value of the attribute is the constant value of ARG. */
22024 static void
22025 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22027 die_arg_entry entry;
22029 if (!die || !arg)
22030 return;
22032 if (!tmpl_value_parm_die_table)
22033 vec_alloc (tmpl_value_parm_die_table, 32);
22035 entry.die = die;
22036 entry.arg = arg;
22037 vec_safe_push (tmpl_value_parm_die_table, entry);
22040 /* Return TRUE if T is an instance of generic type, FALSE
22041 otherwise. */
22043 static bool
22044 generic_type_p (tree t)
22046 if (t == NULL_TREE || !TYPE_P (t))
22047 return false;
22048 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22051 /* Schedule the generation of the generic parameter dies for the
22052 instance of generic type T. The proper generation itself is later
22053 done by gen_scheduled_generic_parms_dies. */
22055 static void
22056 schedule_generic_params_dies_gen (tree t)
22058 if (!generic_type_p (t))
22059 return;
22061 if (!generic_type_instances)
22062 vec_alloc (generic_type_instances, 256);
22064 vec_safe_push (generic_type_instances, t);
22067 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22068 by append_entry_to_tmpl_value_parm_die_table. This function must
22069 be called after function DIEs have been generated. */
22071 static void
22072 gen_remaining_tmpl_value_param_die_attribute (void)
22074 if (tmpl_value_parm_die_table)
22076 unsigned i;
22077 die_arg_entry *e;
22079 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22080 tree_add_const_value_attribute (e->die, e->arg);
22084 /* Generate generic parameters DIEs for instances of generic types
22085 that have been previously scheduled by
22086 schedule_generic_params_dies_gen. This function must be called
22087 after all the types of the CU have been laid out. */
22089 static void
22090 gen_scheduled_generic_parms_dies (void)
22092 unsigned i;
22093 tree t;
22095 if (!generic_type_instances)
22096 return;
22098 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22099 if (COMPLETE_TYPE_P (t))
22100 gen_generic_params_dies (t);
22104 /* Replace DW_AT_name for the decl with name. */
22106 static void
22107 dwarf2out_set_name (tree decl, tree name)
22109 dw_die_ref die;
22110 dw_attr_ref attr;
22111 const char *dname;
22113 die = TYPE_SYMTAB_DIE (decl);
22114 if (!die)
22115 return;
22117 dname = dwarf2_name (name, 0);
22118 if (!dname)
22119 return;
22121 attr = get_AT (die, DW_AT_name);
22122 if (attr)
22124 struct indirect_string_node *node;
22126 node = find_AT_string (dname);
22127 /* replace the string. */
22128 attr->dw_attr_val.v.val_str = node;
22131 else
22132 add_name_attribute (die, dname);
22135 /* True if before or during processing of the first function being emitted. */
22136 static bool in_first_function_p = true;
22137 /* True if loc_note during dwarf2out_var_location call might still be
22138 before first real instruction at address equal to .Ltext0. */
22139 static bool maybe_at_text_label_p = true;
22140 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22141 static unsigned int first_loclabel_num_not_at_text_label;
22143 /* Called by the final INSN scan whenever we see a var location. We
22144 use it to drop labels in the right places, and throw the location in
22145 our lookup table. */
22147 static void
22148 dwarf2out_var_location (rtx_insn *loc_note)
22150 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22151 struct var_loc_node *newloc;
22152 rtx_insn *next_real, *next_note;
22153 static const char *last_label;
22154 static const char *last_postcall_label;
22155 static bool last_in_cold_section_p;
22156 static rtx_insn *expected_next_loc_note;
22157 tree decl;
22158 bool var_loc_p;
22160 if (!NOTE_P (loc_note))
22162 if (CALL_P (loc_note))
22164 call_site_count++;
22165 if (SIBLING_CALL_P (loc_note))
22166 tail_call_site_count++;
22168 return;
22171 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22172 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22173 return;
22175 /* Optimize processing a large consecutive sequence of location
22176 notes so we don't spend too much time in next_real_insn. If the
22177 next insn is another location note, remember the next_real_insn
22178 calculation for next time. */
22179 next_real = cached_next_real_insn;
22180 if (next_real)
22182 if (expected_next_loc_note != loc_note)
22183 next_real = NULL;
22186 next_note = NEXT_INSN (loc_note);
22187 if (! next_note
22188 || next_note->deleted ()
22189 || ! NOTE_P (next_note)
22190 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22191 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22192 next_note = NULL;
22194 if (! next_real)
22195 next_real = next_real_insn (loc_note);
22197 if (next_note)
22199 expected_next_loc_note = next_note;
22200 cached_next_real_insn = next_real;
22202 else
22203 cached_next_real_insn = NULL;
22205 /* If there are no instructions which would be affected by this note,
22206 don't do anything. */
22207 if (var_loc_p
22208 && next_real == NULL_RTX
22209 && !NOTE_DURING_CALL_P (loc_note))
22210 return;
22212 if (next_real == NULL_RTX)
22213 next_real = get_last_insn ();
22215 /* If there were any real insns between note we processed last time
22216 and this note (or if it is the first note), clear
22217 last_{,postcall_}label so that they are not reused this time. */
22218 if (last_var_location_insn == NULL_RTX
22219 || last_var_location_insn != next_real
22220 || last_in_cold_section_p != in_cold_section_p)
22222 last_label = NULL;
22223 last_postcall_label = NULL;
22226 if (var_loc_p)
22228 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22229 newloc = add_var_loc_to_decl (decl, loc_note,
22230 NOTE_DURING_CALL_P (loc_note)
22231 ? last_postcall_label : last_label);
22232 if (newloc == NULL)
22233 return;
22235 else
22237 decl = NULL_TREE;
22238 newloc = NULL;
22241 /* If there were no real insns between note we processed last time
22242 and this note, use the label we emitted last time. Otherwise
22243 create a new label and emit it. */
22244 if (last_label == NULL)
22246 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22247 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22248 loclabel_num++;
22249 last_label = ggc_strdup (loclabel);
22250 /* See if loclabel might be equal to .Ltext0. If yes,
22251 bump first_loclabel_num_not_at_text_label. */
22252 if (!have_multiple_function_sections
22253 && in_first_function_p
22254 && maybe_at_text_label_p)
22256 static rtx_insn *last_start;
22257 rtx_insn *insn;
22258 for (insn = loc_note; insn; insn = previous_insn (insn))
22259 if (insn == last_start)
22260 break;
22261 else if (!NONDEBUG_INSN_P (insn))
22262 continue;
22263 else
22265 rtx body = PATTERN (insn);
22266 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22267 continue;
22268 /* Inline asm could occupy zero bytes. */
22269 else if (GET_CODE (body) == ASM_INPUT
22270 || asm_noperands (body) >= 0)
22271 continue;
22272 #ifdef HAVE_attr_length
22273 else if (get_attr_min_length (insn) == 0)
22274 continue;
22275 #endif
22276 else
22278 /* Assume insn has non-zero length. */
22279 maybe_at_text_label_p = false;
22280 break;
22283 if (maybe_at_text_label_p)
22285 last_start = loc_note;
22286 first_loclabel_num_not_at_text_label = loclabel_num;
22291 if (!var_loc_p)
22293 struct call_arg_loc_node *ca_loc
22294 = ggc_cleared_alloc<call_arg_loc_node> ();
22295 rtx_insn *prev = prev_real_insn (loc_note);
22296 rtx x;
22297 ca_loc->call_arg_loc_note = loc_note;
22298 ca_loc->next = NULL;
22299 ca_loc->label = last_label;
22300 gcc_assert (prev
22301 && (CALL_P (prev)
22302 || (NONJUMP_INSN_P (prev)
22303 && GET_CODE (PATTERN (prev)) == SEQUENCE
22304 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22305 if (!CALL_P (prev))
22306 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22307 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22308 x = get_call_rtx_from (PATTERN (prev));
22309 if (x)
22311 x = XEXP (XEXP (x, 0), 0);
22312 if (GET_CODE (x) == SYMBOL_REF
22313 && SYMBOL_REF_DECL (x)
22314 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22315 ca_loc->symbol_ref = x;
22317 ca_loc->block = insn_scope (prev);
22318 if (call_arg_locations)
22319 call_arg_loc_last->next = ca_loc;
22320 else
22321 call_arg_locations = ca_loc;
22322 call_arg_loc_last = ca_loc;
22324 else if (!NOTE_DURING_CALL_P (loc_note))
22325 newloc->label = last_label;
22326 else
22328 if (!last_postcall_label)
22330 sprintf (loclabel, "%s-1", last_label);
22331 last_postcall_label = ggc_strdup (loclabel);
22333 newloc->label = last_postcall_label;
22336 last_var_location_insn = next_real;
22337 last_in_cold_section_p = in_cold_section_p;
22340 /* Note in one location list that text section has changed. */
22343 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22345 var_loc_list *list = *slot;
22346 if (list->first)
22347 list->last_before_switch
22348 = list->last->next ? list->last->next : list->last;
22349 return 1;
22352 /* Note in all location lists that text section has changed. */
22354 static void
22355 var_location_switch_text_section (void)
22357 if (decl_loc_table == NULL)
22358 return;
22360 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22363 /* Create a new line number table. */
22365 static dw_line_info_table *
22366 new_line_info_table (void)
22368 dw_line_info_table *table;
22370 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22371 table->file_num = 1;
22372 table->line_num = 1;
22373 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22375 return table;
22378 /* Lookup the "current" table into which we emit line info, so
22379 that we don't have to do it for every source line. */
22381 static void
22382 set_cur_line_info_table (section *sec)
22384 dw_line_info_table *table;
22386 if (sec == text_section)
22387 table = text_section_line_info;
22388 else if (sec == cold_text_section)
22390 table = cold_text_section_line_info;
22391 if (!table)
22393 cold_text_section_line_info = table = new_line_info_table ();
22394 table->end_label = cold_end_label;
22397 else
22399 const char *end_label;
22401 if (flag_reorder_blocks_and_partition)
22403 if (in_cold_section_p)
22404 end_label = crtl->subsections.cold_section_end_label;
22405 else
22406 end_label = crtl->subsections.hot_section_end_label;
22408 else
22410 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22411 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22412 current_function_funcdef_no);
22413 end_label = ggc_strdup (label);
22416 table = new_line_info_table ();
22417 table->end_label = end_label;
22419 vec_safe_push (separate_line_info, table);
22422 if (DWARF2_ASM_LINE_DEBUG_INFO)
22423 table->is_stmt = (cur_line_info_table
22424 ? cur_line_info_table->is_stmt
22425 : DWARF_LINE_DEFAULT_IS_STMT_START);
22426 cur_line_info_table = table;
22430 /* We need to reset the locations at the beginning of each
22431 function. We can't do this in the end_function hook, because the
22432 declarations that use the locations won't have been output when
22433 that hook is called. Also compute have_multiple_function_sections here. */
22435 static void
22436 dwarf2out_begin_function (tree fun)
22438 section *sec = function_section (fun);
22440 if (sec != text_section)
22441 have_multiple_function_sections = true;
22443 if (flag_reorder_blocks_and_partition && !cold_text_section)
22445 gcc_assert (current_function_decl == fun);
22446 cold_text_section = unlikely_text_section ();
22447 switch_to_section (cold_text_section);
22448 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22449 switch_to_section (sec);
22452 dwarf2out_note_section_used ();
22453 call_site_count = 0;
22454 tail_call_site_count = 0;
22456 set_cur_line_info_table (sec);
22459 /* Helper function of dwarf2out_end_function, called only after emitting
22460 the very first function into assembly. Check if some .debug_loc range
22461 might end with a .LVL* label that could be equal to .Ltext0.
22462 In that case we must force using absolute addresses in .debug_loc ranges,
22463 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22464 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22465 list terminator.
22466 Set have_multiple_function_sections to true in that case and
22467 terminate htab traversal. */
22470 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22472 var_loc_list *entry = *slot;
22473 struct var_loc_node *node;
22475 node = entry->first;
22476 if (node && node->next && node->next->label)
22478 unsigned int i;
22479 const char *label = node->next->label;
22480 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22482 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22484 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22485 if (strcmp (label, loclabel) == 0)
22487 have_multiple_function_sections = true;
22488 return 0;
22492 return 1;
22495 /* Hook called after emitting a function into assembly.
22496 This does something only for the very first function emitted. */
22498 static void
22499 dwarf2out_end_function (unsigned int)
22501 if (in_first_function_p
22502 && !have_multiple_function_sections
22503 && first_loclabel_num_not_at_text_label
22504 && decl_loc_table)
22505 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22506 in_first_function_p = false;
22507 maybe_at_text_label_p = false;
22510 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22511 front-ends register a translation unit even before dwarf2out_init is
22512 called. */
22513 static tree main_translation_unit = NULL_TREE;
22515 /* Hook called by front-ends after they built their main translation unit.
22516 Associate comp_unit_die to UNIT. */
22518 static void
22519 dwarf2out_register_main_translation_unit (tree unit)
22521 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22522 && main_translation_unit == NULL_TREE);
22523 main_translation_unit = unit;
22524 /* If dwarf2out_init has not been called yet, it will perform the association
22525 itself looking at main_translation_unit. */
22526 if (decl_die_table != NULL)
22527 equate_decl_number_to_die (unit, comp_unit_die ());
22530 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22532 static void
22533 push_dw_line_info_entry (dw_line_info_table *table,
22534 enum dw_line_info_opcode opcode, unsigned int val)
22536 dw_line_info_entry e;
22537 e.opcode = opcode;
22538 e.val = val;
22539 vec_safe_push (table->entries, e);
22542 /* Output a label to mark the beginning of a source code line entry
22543 and record information relating to this source line, in
22544 'line_info_table' for later output of the .debug_line section. */
22545 /* ??? The discriminator parameter ought to be unsigned. */
22547 static void
22548 dwarf2out_source_line (unsigned int line, const char *filename,
22549 int discriminator, bool is_stmt)
22551 unsigned int file_num;
22552 dw_line_info_table *table;
22554 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22555 return;
22557 /* The discriminator column was added in dwarf4. Simplify the below
22558 by simply removing it if we're not supposed to output it. */
22559 if (dwarf_version < 4 && dwarf_strict)
22560 discriminator = 0;
22562 table = cur_line_info_table;
22563 file_num = maybe_emit_file (lookup_filename (filename));
22565 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22566 the debugger has used the second (possibly duplicate) line number
22567 at the beginning of the function to mark the end of the prologue.
22568 We could eliminate any other duplicates within the function. For
22569 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22570 that second line number entry. */
22571 /* Recall that this end-of-prologue indication is *not* the same thing
22572 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22573 to which the hook corresponds, follows the last insn that was
22574 emitted by gen_prologue. What we need is to precede the first insn
22575 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22576 insn that corresponds to something the user wrote. These may be
22577 very different locations once scheduling is enabled. */
22579 if (0 && file_num == table->file_num
22580 && line == table->line_num
22581 && discriminator == table->discrim_num
22582 && is_stmt == table->is_stmt)
22583 return;
22585 switch_to_section (current_function_section ());
22587 /* If requested, emit something human-readable. */
22588 if (flag_debug_asm)
22589 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22591 if (DWARF2_ASM_LINE_DEBUG_INFO)
22593 /* Emit the .loc directive understood by GNU as. */
22594 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22595 file_num, line, is_stmt, discriminator */
22596 fputs ("\t.loc ", asm_out_file);
22597 fprint_ul (asm_out_file, file_num);
22598 putc (' ', asm_out_file);
22599 fprint_ul (asm_out_file, line);
22600 putc (' ', asm_out_file);
22601 putc ('0', asm_out_file);
22603 if (is_stmt != table->is_stmt)
22605 fputs (" is_stmt ", asm_out_file);
22606 putc (is_stmt ? '1' : '0', asm_out_file);
22608 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22610 gcc_assert (discriminator > 0);
22611 fputs (" discriminator ", asm_out_file);
22612 fprint_ul (asm_out_file, (unsigned long) discriminator);
22614 putc ('\n', asm_out_file);
22616 else
22618 unsigned int label_num = ++line_info_label_num;
22620 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22622 push_dw_line_info_entry (table, LI_set_address, label_num);
22623 if (file_num != table->file_num)
22624 push_dw_line_info_entry (table, LI_set_file, file_num);
22625 if (discriminator != table->discrim_num)
22626 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22627 if (is_stmt != table->is_stmt)
22628 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22629 push_dw_line_info_entry (table, LI_set_line, line);
22632 table->file_num = file_num;
22633 table->line_num = line;
22634 table->discrim_num = discriminator;
22635 table->is_stmt = is_stmt;
22636 table->in_use = true;
22639 /* Record the beginning of a new source file. */
22641 static void
22642 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22644 if (flag_eliminate_dwarf2_dups)
22646 /* Record the beginning of the file for break_out_includes. */
22647 dw_die_ref bincl_die;
22649 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22650 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22653 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22655 macinfo_entry e;
22656 e.code = DW_MACINFO_start_file;
22657 e.lineno = lineno;
22658 e.info = ggc_strdup (filename);
22659 vec_safe_push (macinfo_table, e);
22663 /* Record the end of a source file. */
22665 static void
22666 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22668 if (flag_eliminate_dwarf2_dups)
22669 /* Record the end of the file for break_out_includes. */
22670 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22672 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22674 macinfo_entry e;
22675 e.code = DW_MACINFO_end_file;
22676 e.lineno = lineno;
22677 e.info = NULL;
22678 vec_safe_push (macinfo_table, e);
22682 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22683 the tail part of the directive line, i.e. the part which is past the
22684 initial whitespace, #, whitespace, directive-name, whitespace part. */
22686 static void
22687 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22688 const char *buffer ATTRIBUTE_UNUSED)
22690 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22692 macinfo_entry e;
22693 /* Insert a dummy first entry to be able to optimize the whole
22694 predefined macro block using DW_MACRO_GNU_transparent_include. */
22695 if (macinfo_table->is_empty () && lineno <= 1)
22697 e.code = 0;
22698 e.lineno = 0;
22699 e.info = NULL;
22700 vec_safe_push (macinfo_table, e);
22702 e.code = DW_MACINFO_define;
22703 e.lineno = lineno;
22704 e.info = ggc_strdup (buffer);
22705 vec_safe_push (macinfo_table, e);
22709 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22710 the tail part of the directive line, i.e. the part which is past the
22711 initial whitespace, #, whitespace, directive-name, whitespace part. */
22713 static void
22714 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22715 const char *buffer ATTRIBUTE_UNUSED)
22717 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22719 macinfo_entry e;
22720 /* Insert a dummy first entry to be able to optimize the whole
22721 predefined macro block using DW_MACRO_GNU_transparent_include. */
22722 if (macinfo_table->is_empty () && lineno <= 1)
22724 e.code = 0;
22725 e.lineno = 0;
22726 e.info = NULL;
22727 vec_safe_push (macinfo_table, e);
22729 e.code = DW_MACINFO_undef;
22730 e.lineno = lineno;
22731 e.info = ggc_strdup (buffer);
22732 vec_safe_push (macinfo_table, e);
22736 /* Helpers to manipulate hash table of CUs. */
22738 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22740 typedef macinfo_entry *value_type;
22741 typedef macinfo_entry *compare_type;
22742 static inline hashval_t hash (const macinfo_entry *);
22743 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22746 inline hashval_t
22747 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22749 return htab_hash_string (entry->info);
22752 inline bool
22753 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22754 const macinfo_entry *entry2)
22756 return !strcmp (entry1->info, entry2->info);
22759 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22761 /* Output a single .debug_macinfo entry. */
22763 static void
22764 output_macinfo_op (macinfo_entry *ref)
22766 int file_num;
22767 size_t len;
22768 struct indirect_string_node *node;
22769 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22770 struct dwarf_file_data *fd;
22772 switch (ref->code)
22774 case DW_MACINFO_start_file:
22775 fd = lookup_filename (ref->info);
22776 file_num = maybe_emit_file (fd);
22777 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22778 dw2_asm_output_data_uleb128 (ref->lineno,
22779 "Included from line number %lu",
22780 (unsigned long) ref->lineno);
22781 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22782 break;
22783 case DW_MACINFO_end_file:
22784 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22785 break;
22786 case DW_MACINFO_define:
22787 case DW_MACINFO_undef:
22788 len = strlen (ref->info) + 1;
22789 if (!dwarf_strict
22790 && len > DWARF_OFFSET_SIZE
22791 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22792 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22794 ref->code = ref->code == DW_MACINFO_define
22795 ? DW_MACRO_GNU_define_indirect
22796 : DW_MACRO_GNU_undef_indirect;
22797 output_macinfo_op (ref);
22798 return;
22800 dw2_asm_output_data (1, ref->code,
22801 ref->code == DW_MACINFO_define
22802 ? "Define macro" : "Undefine macro");
22803 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22804 (unsigned long) ref->lineno);
22805 dw2_asm_output_nstring (ref->info, -1, "The macro");
22806 break;
22807 case DW_MACRO_GNU_define_indirect:
22808 case DW_MACRO_GNU_undef_indirect:
22809 node = find_AT_string (ref->info);
22810 gcc_assert (node
22811 && ((node->form == DW_FORM_strp)
22812 || (node->form == DW_FORM_GNU_str_index)));
22813 dw2_asm_output_data (1, ref->code,
22814 ref->code == DW_MACRO_GNU_define_indirect
22815 ? "Define macro indirect"
22816 : "Undefine macro indirect");
22817 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22818 (unsigned long) ref->lineno);
22819 if (node->form == DW_FORM_strp)
22820 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22821 debug_str_section, "The macro: \"%s\"",
22822 ref->info);
22823 else
22824 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22825 ref->info);
22826 break;
22827 case DW_MACRO_GNU_transparent_include:
22828 dw2_asm_output_data (1, ref->code, "Transparent include");
22829 ASM_GENERATE_INTERNAL_LABEL (label,
22830 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22831 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22832 break;
22833 default:
22834 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22835 ASM_COMMENT_START, (unsigned long) ref->code);
22836 break;
22840 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22841 other compilation unit .debug_macinfo sections. IDX is the first
22842 index of a define/undef, return the number of ops that should be
22843 emitted in a comdat .debug_macinfo section and emit
22844 a DW_MACRO_GNU_transparent_include entry referencing it.
22845 If the define/undef entry should be emitted normally, return 0. */
22847 static unsigned
22848 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22849 macinfo_hash_type **macinfo_htab)
22851 macinfo_entry *first, *second, *cur, *inc;
22852 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22853 unsigned char checksum[16];
22854 struct md5_ctx ctx;
22855 char *grp_name, *tail;
22856 const char *base;
22857 unsigned int i, count, encoded_filename_len, linebuf_len;
22858 macinfo_entry **slot;
22860 first = &(*macinfo_table)[idx];
22861 second = &(*macinfo_table)[idx + 1];
22863 /* Optimize only if there are at least two consecutive define/undef ops,
22864 and either all of them are before first DW_MACINFO_start_file
22865 with lineno {0,1} (i.e. predefined macro block), or all of them are
22866 in some included header file. */
22867 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22868 return 0;
22869 if (vec_safe_is_empty (files))
22871 if (first->lineno > 1 || second->lineno > 1)
22872 return 0;
22874 else if (first->lineno == 0)
22875 return 0;
22877 /* Find the last define/undef entry that can be grouped together
22878 with first and at the same time compute md5 checksum of their
22879 codes, linenumbers and strings. */
22880 md5_init_ctx (&ctx);
22881 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22882 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22883 break;
22884 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22885 break;
22886 else
22888 unsigned char code = cur->code;
22889 md5_process_bytes (&code, 1, &ctx);
22890 checksum_uleb128 (cur->lineno, &ctx);
22891 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22893 md5_finish_ctx (&ctx, checksum);
22894 count = i - idx;
22896 /* From the containing include filename (if any) pick up just
22897 usable characters from its basename. */
22898 if (vec_safe_is_empty (files))
22899 base = "";
22900 else
22901 base = lbasename (files->last ().info);
22902 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22903 if (ISIDNUM (base[i]) || base[i] == '.')
22904 encoded_filename_len++;
22905 /* Count . at the end. */
22906 if (encoded_filename_len)
22907 encoded_filename_len++;
22909 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22910 linebuf_len = strlen (linebuf);
22912 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22913 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22914 + 16 * 2 + 1);
22915 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22916 tail = grp_name + 4;
22917 if (encoded_filename_len)
22919 for (i = 0; base[i]; i++)
22920 if (ISIDNUM (base[i]) || base[i] == '.')
22921 *tail++ = base[i];
22922 *tail++ = '.';
22924 memcpy (tail, linebuf, linebuf_len);
22925 tail += linebuf_len;
22926 *tail++ = '.';
22927 for (i = 0; i < 16; i++)
22928 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22930 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22931 in the empty vector entry before the first define/undef. */
22932 inc = &(*macinfo_table)[idx - 1];
22933 inc->code = DW_MACRO_GNU_transparent_include;
22934 inc->lineno = 0;
22935 inc->info = ggc_strdup (grp_name);
22936 if (!*macinfo_htab)
22937 *macinfo_htab = new macinfo_hash_type (10);
22938 /* Avoid emitting duplicates. */
22939 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22940 if (*slot != NULL)
22942 inc->code = 0;
22943 inc->info = NULL;
22944 /* If such an entry has been used before, just emit
22945 a DW_MACRO_GNU_transparent_include op. */
22946 inc = *slot;
22947 output_macinfo_op (inc);
22948 /* And clear all macinfo_entry in the range to avoid emitting them
22949 in the second pass. */
22950 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22952 cur->code = 0;
22953 cur->info = NULL;
22956 else
22958 *slot = inc;
22959 inc->lineno = (*macinfo_htab)->elements ();
22960 output_macinfo_op (inc);
22962 return count;
22965 /* Save any strings needed by the macinfo table in the debug str
22966 table. All strings must be collected into the table by the time
22967 index_string is called. */
22969 static void
22970 save_macinfo_strings (void)
22972 unsigned len;
22973 unsigned i;
22974 macinfo_entry *ref;
22976 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22978 switch (ref->code)
22980 /* Match the logic in output_macinfo_op to decide on
22981 indirect strings. */
22982 case DW_MACINFO_define:
22983 case DW_MACINFO_undef:
22984 len = strlen (ref->info) + 1;
22985 if (!dwarf_strict
22986 && len > DWARF_OFFSET_SIZE
22987 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22988 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22989 set_indirect_string (find_AT_string (ref->info));
22990 break;
22991 case DW_MACRO_GNU_define_indirect:
22992 case DW_MACRO_GNU_undef_indirect:
22993 set_indirect_string (find_AT_string (ref->info));
22994 break;
22995 default:
22996 break;
23001 /* Output macinfo section(s). */
23003 static void
23004 output_macinfo (void)
23006 unsigned i;
23007 unsigned long length = vec_safe_length (macinfo_table);
23008 macinfo_entry *ref;
23009 vec<macinfo_entry, va_gc> *files = NULL;
23010 macinfo_hash_type *macinfo_htab = NULL;
23012 if (! length)
23013 return;
23015 /* output_macinfo* uses these interchangeably. */
23016 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23017 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23018 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23019 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23021 /* For .debug_macro emit the section header. */
23022 if (!dwarf_strict)
23024 dw2_asm_output_data (2, 4, "DWARF macro version number");
23025 if (DWARF_OFFSET_SIZE == 8)
23026 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23027 else
23028 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23029 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23030 (!dwarf_split_debug_info ? debug_line_section_label
23031 : debug_skeleton_line_section_label),
23032 debug_line_section, NULL);
23035 /* In the first loop, it emits the primary .debug_macinfo section
23036 and after each emitted op the macinfo_entry is cleared.
23037 If a longer range of define/undef ops can be optimized using
23038 DW_MACRO_GNU_transparent_include, the
23039 DW_MACRO_GNU_transparent_include op is emitted and kept in
23040 the vector before the first define/undef in the range and the
23041 whole range of define/undef ops is not emitted and kept. */
23042 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23044 switch (ref->code)
23046 case DW_MACINFO_start_file:
23047 vec_safe_push (files, *ref);
23048 break;
23049 case DW_MACINFO_end_file:
23050 if (!vec_safe_is_empty (files))
23051 files->pop ();
23052 break;
23053 case DW_MACINFO_define:
23054 case DW_MACINFO_undef:
23055 if (!dwarf_strict
23056 && HAVE_COMDAT_GROUP
23057 && vec_safe_length (files) != 1
23058 && i > 0
23059 && i + 1 < length
23060 && (*macinfo_table)[i - 1].code == 0)
23062 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23063 if (count)
23065 i += count - 1;
23066 continue;
23069 break;
23070 case 0:
23071 /* A dummy entry may be inserted at the beginning to be able
23072 to optimize the whole block of predefined macros. */
23073 if (i == 0)
23074 continue;
23075 default:
23076 break;
23078 output_macinfo_op (ref);
23079 ref->info = NULL;
23080 ref->code = 0;
23083 if (!macinfo_htab)
23084 return;
23086 delete macinfo_htab;
23087 macinfo_htab = NULL;
23089 /* If any DW_MACRO_GNU_transparent_include were used, on those
23090 DW_MACRO_GNU_transparent_include entries terminate the
23091 current chain and switch to a new comdat .debug_macinfo
23092 section and emit the define/undef entries within it. */
23093 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23094 switch (ref->code)
23096 case 0:
23097 continue;
23098 case DW_MACRO_GNU_transparent_include:
23100 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23101 tree comdat_key = get_identifier (ref->info);
23102 /* Terminate the previous .debug_macinfo section. */
23103 dw2_asm_output_data (1, 0, "End compilation unit");
23104 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23105 SECTION_DEBUG
23106 | SECTION_LINKONCE,
23107 comdat_key);
23108 ASM_GENERATE_INTERNAL_LABEL (label,
23109 DEBUG_MACRO_SECTION_LABEL,
23110 ref->lineno);
23111 ASM_OUTPUT_LABEL (asm_out_file, label);
23112 ref->code = 0;
23113 ref->info = NULL;
23114 dw2_asm_output_data (2, 4, "DWARF macro version number");
23115 if (DWARF_OFFSET_SIZE == 8)
23116 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23117 else
23118 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23120 break;
23121 case DW_MACINFO_define:
23122 case DW_MACINFO_undef:
23123 output_macinfo_op (ref);
23124 ref->code = 0;
23125 ref->info = NULL;
23126 break;
23127 default:
23128 gcc_unreachable ();
23132 /* Set up for Dwarf output at the start of compilation. */
23134 static void
23135 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23137 /* This option is currently broken, see (PR53118 and PR46102). */
23138 if (flag_eliminate_dwarf2_dups
23139 && strstr (lang_hooks.name, "C++"))
23141 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23142 flag_eliminate_dwarf2_dups = 0;
23145 /* Allocate the file_table. */
23146 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23148 /* Allocate the decl_die_table. */
23149 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23151 /* Allocate the decl_loc_table. */
23152 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23154 /* Allocate the cached_dw_loc_list_table. */
23155 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23157 /* Allocate the initial hunk of the decl_scope_table. */
23158 vec_alloc (decl_scope_table, 256);
23160 /* Allocate the initial hunk of the abbrev_die_table. */
23161 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23162 (ABBREV_DIE_TABLE_INCREMENT);
23163 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23164 /* Zero-th entry is allocated, but unused. */
23165 abbrev_die_table_in_use = 1;
23167 /* Allocate the pubtypes and pubnames vectors. */
23168 vec_alloc (pubname_table, 32);
23169 vec_alloc (pubtype_table, 32);
23171 vec_alloc (incomplete_types, 64);
23173 vec_alloc (used_rtx_array, 32);
23175 if (!dwarf_split_debug_info)
23177 debug_info_section = get_section (DEBUG_INFO_SECTION,
23178 SECTION_DEBUG, NULL);
23179 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23180 SECTION_DEBUG, NULL);
23181 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23182 SECTION_DEBUG, NULL);
23184 else
23186 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23187 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23188 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23189 SECTION_DEBUG | SECTION_EXCLUDE,
23190 NULL);
23191 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23192 SECTION_DEBUG, NULL);
23193 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23194 SECTION_DEBUG, NULL);
23195 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23196 SECTION_DEBUG, NULL);
23197 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23198 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23200 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23201 the main .o, but the skeleton_line goes into the split off dwo. */
23202 debug_skeleton_line_section
23203 = get_section (DEBUG_DWO_LINE_SECTION,
23204 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23205 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23206 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23207 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23208 SECTION_DEBUG | SECTION_EXCLUDE,
23209 NULL);
23210 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23211 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23212 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23213 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23214 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23215 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23217 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23218 SECTION_DEBUG, NULL);
23219 debug_macinfo_section = get_section (dwarf_strict
23220 ? DEBUG_MACINFO_SECTION
23221 : DEBUG_MACRO_SECTION,
23222 DEBUG_MACRO_SECTION_FLAGS, NULL);
23223 debug_line_section = get_section (DEBUG_LINE_SECTION,
23224 SECTION_DEBUG, NULL);
23225 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23226 SECTION_DEBUG, NULL);
23227 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23228 SECTION_DEBUG, NULL);
23229 debug_str_section = get_section (DEBUG_STR_SECTION,
23230 DEBUG_STR_SECTION_FLAGS, NULL);
23231 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23232 SECTION_DEBUG, NULL);
23233 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23234 SECTION_DEBUG, NULL);
23236 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23237 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23238 DEBUG_ABBREV_SECTION_LABEL, 0);
23239 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23240 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23241 COLD_TEXT_SECTION_LABEL, 0);
23242 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23244 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23245 DEBUG_INFO_SECTION_LABEL, 0);
23246 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23247 DEBUG_LINE_SECTION_LABEL, 0);
23248 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23249 DEBUG_RANGES_SECTION_LABEL, 0);
23250 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23251 DEBUG_ADDR_SECTION_LABEL, 0);
23252 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23253 dwarf_strict
23254 ? DEBUG_MACINFO_SECTION_LABEL
23255 : DEBUG_MACRO_SECTION_LABEL, 0);
23256 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23258 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23259 vec_alloc (macinfo_table, 64);
23261 switch_to_section (text_section);
23262 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23264 /* Make sure the line number table for .text always exists. */
23265 text_section_line_info = new_line_info_table ();
23266 text_section_line_info->end_label = text_end_label;
23268 /* If front-ends already registered a main translation unit but we were not
23269 ready to perform the association, do this now. */
23270 if (main_translation_unit != NULL_TREE)
23271 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23274 /* Called before compile () starts outputtting functions, variables
23275 and toplevel asms into assembly. */
23277 static void
23278 dwarf2out_assembly_start (void)
23280 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23281 && dwarf2out_do_cfi_asm ()
23282 && (!(flag_unwind_tables || flag_exceptions)
23283 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23284 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23287 /* A helper function for dwarf2out_finish called through
23288 htab_traverse. Assign a string its index. All strings must be
23289 collected into the table by the time index_string is called,
23290 because the indexing code relies on htab_traverse to traverse nodes
23291 in the same order for each run. */
23294 index_string (indirect_string_node **h, unsigned int *index)
23296 indirect_string_node *node = *h;
23298 find_string_form (node);
23299 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23301 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23302 node->index = *index;
23303 *index += 1;
23305 return 1;
23308 /* A helper function for output_indirect_strings called through
23309 htab_traverse. Output the offset to a string and update the
23310 current offset. */
23313 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23315 indirect_string_node *node = *h;
23317 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23319 /* Assert that this node has been assigned an index. */
23320 gcc_assert (node->index != NO_INDEX_ASSIGNED
23321 && node->index != NOT_INDEXED);
23322 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23323 "indexed string 0x%x: %s", node->index, node->str);
23324 *offset += strlen (node->str) + 1;
23326 return 1;
23329 /* A helper function for dwarf2out_finish called through
23330 htab_traverse. Output the indexed string. */
23333 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23335 struct indirect_string_node *node = *h;
23337 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23339 /* Assert that the strings are output in the same order as their
23340 indexes were assigned. */
23341 gcc_assert (*cur_idx == node->index);
23342 assemble_string (node->str, strlen (node->str) + 1);
23343 *cur_idx += 1;
23345 return 1;
23348 /* A helper function for dwarf2out_finish called through
23349 htab_traverse. Emit one queued .debug_str string. */
23352 output_indirect_string (indirect_string_node **h, void *)
23354 struct indirect_string_node *node = *h;
23356 node->form = find_string_form (node);
23357 if (node->form == DW_FORM_strp && node->refcount > 0)
23359 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23360 assemble_string (node->str, strlen (node->str) + 1);
23363 return 1;
23366 /* Output the indexed string table. */
23368 static void
23369 output_indirect_strings (void)
23371 switch_to_section (debug_str_section);
23372 if (!dwarf_split_debug_info)
23373 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23374 else
23376 unsigned int offset = 0;
23377 unsigned int cur_idx = 0;
23379 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23381 switch_to_section (debug_str_offsets_section);
23382 debug_str_hash->traverse_noresize
23383 <unsigned int *, output_index_string_offset> (&offset);
23384 switch_to_section (debug_str_dwo_section);
23385 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23386 (&cur_idx);
23390 /* Callback for htab_traverse to assign an index to an entry in the
23391 table, and to write that entry to the .debug_addr section. */
23394 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23396 addr_table_entry *entry = *slot;
23398 if (entry->refcount == 0)
23400 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23401 || entry->index == NOT_INDEXED);
23402 return 1;
23405 gcc_assert (entry->index == *cur_index);
23406 (*cur_index)++;
23408 switch (entry->kind)
23410 case ate_kind_rtx:
23411 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23412 "0x%x", entry->index);
23413 break;
23414 case ate_kind_rtx_dtprel:
23415 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23416 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23417 DWARF2_ADDR_SIZE,
23418 entry->addr.rtl);
23419 fputc ('\n', asm_out_file);
23420 break;
23421 case ate_kind_label:
23422 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23423 "0x%x", entry->index);
23424 break;
23425 default:
23426 gcc_unreachable ();
23428 return 1;
23431 /* Produce the .debug_addr section. */
23433 static void
23434 output_addr_table (void)
23436 unsigned int index = 0;
23437 if (addr_index_table == NULL || addr_index_table->size () == 0)
23438 return;
23440 switch_to_section (debug_addr_section);
23441 addr_index_table
23442 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23445 #if ENABLE_ASSERT_CHECKING
23446 /* Verify that all marks are clear. */
23448 static void
23449 verify_marks_clear (dw_die_ref die)
23451 dw_die_ref c;
23453 gcc_assert (! die->die_mark);
23454 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23456 #endif /* ENABLE_ASSERT_CHECKING */
23458 /* Clear the marks for a die and its children.
23459 Be cool if the mark isn't set. */
23461 static void
23462 prune_unmark_dies (dw_die_ref die)
23464 dw_die_ref c;
23466 if (die->die_mark)
23467 die->die_mark = 0;
23468 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23471 /* Given DIE that we're marking as used, find any other dies
23472 it references as attributes and mark them as used. */
23474 static void
23475 prune_unused_types_walk_attribs (dw_die_ref die)
23477 dw_attr_ref a;
23478 unsigned ix;
23480 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23482 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23484 /* A reference to another DIE.
23485 Make sure that it will get emitted.
23486 If it was broken out into a comdat group, don't follow it. */
23487 if (! AT_ref (a)->comdat_type_p
23488 || a->dw_attr == DW_AT_specification)
23489 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23491 /* Set the string's refcount to 0 so that prune_unused_types_mark
23492 accounts properly for it. */
23493 if (AT_class (a) == dw_val_class_str)
23494 a->dw_attr_val.v.val_str->refcount = 0;
23498 /* Mark the generic parameters and arguments children DIEs of DIE. */
23500 static void
23501 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23503 dw_die_ref c;
23505 if (die == NULL || die->die_child == NULL)
23506 return;
23507 c = die->die_child;
23510 if (is_template_parameter (c))
23511 prune_unused_types_mark (c, 1);
23512 c = c->die_sib;
23513 } while (c && c != die->die_child);
23516 /* Mark DIE as being used. If DOKIDS is true, then walk down
23517 to DIE's children. */
23519 static void
23520 prune_unused_types_mark (dw_die_ref die, int dokids)
23522 dw_die_ref c;
23524 if (die->die_mark == 0)
23526 /* We haven't done this node yet. Mark it as used. */
23527 die->die_mark = 1;
23528 /* If this is the DIE of a generic type instantiation,
23529 mark the children DIEs that describe its generic parms and
23530 args. */
23531 prune_unused_types_mark_generic_parms_dies (die);
23533 /* We also have to mark its parents as used.
23534 (But we don't want to mark our parent's kids due to this,
23535 unless it is a class.) */
23536 if (die->die_parent)
23537 prune_unused_types_mark (die->die_parent,
23538 class_scope_p (die->die_parent));
23540 /* Mark any referenced nodes. */
23541 prune_unused_types_walk_attribs (die);
23543 /* If this node is a specification,
23544 also mark the definition, if it exists. */
23545 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23546 prune_unused_types_mark (die->die_definition, 1);
23549 if (dokids && die->die_mark != 2)
23551 /* We need to walk the children, but haven't done so yet.
23552 Remember that we've walked the kids. */
23553 die->die_mark = 2;
23555 /* If this is an array type, we need to make sure our
23556 kids get marked, even if they're types. If we're
23557 breaking out types into comdat sections, do this
23558 for all type definitions. */
23559 if (die->die_tag == DW_TAG_array_type
23560 || (use_debug_types
23561 && is_type_die (die) && ! is_declaration_die (die)))
23562 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23563 else
23564 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23568 /* For local classes, look if any static member functions were emitted
23569 and if so, mark them. */
23571 static void
23572 prune_unused_types_walk_local_classes (dw_die_ref die)
23574 dw_die_ref c;
23576 if (die->die_mark == 2)
23577 return;
23579 switch (die->die_tag)
23581 case DW_TAG_structure_type:
23582 case DW_TAG_union_type:
23583 case DW_TAG_class_type:
23584 break;
23586 case DW_TAG_subprogram:
23587 if (!get_AT_flag (die, DW_AT_declaration)
23588 || die->die_definition != NULL)
23589 prune_unused_types_mark (die, 1);
23590 return;
23592 default:
23593 return;
23596 /* Mark children. */
23597 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23600 /* Walk the tree DIE and mark types that we actually use. */
23602 static void
23603 prune_unused_types_walk (dw_die_ref die)
23605 dw_die_ref c;
23607 /* Don't do anything if this node is already marked and
23608 children have been marked as well. */
23609 if (die->die_mark == 2)
23610 return;
23612 switch (die->die_tag)
23614 case DW_TAG_structure_type:
23615 case DW_TAG_union_type:
23616 case DW_TAG_class_type:
23617 if (die->die_perennial_p)
23618 break;
23620 for (c = die->die_parent; c; c = c->die_parent)
23621 if (c->die_tag == DW_TAG_subprogram)
23622 break;
23624 /* Finding used static member functions inside of classes
23625 is needed just for local classes, because for other classes
23626 static member function DIEs with DW_AT_specification
23627 are emitted outside of the DW_TAG_*_type. If we ever change
23628 it, we'd need to call this even for non-local classes. */
23629 if (c)
23630 prune_unused_types_walk_local_classes (die);
23632 /* It's a type node --- don't mark it. */
23633 return;
23635 case DW_TAG_const_type:
23636 case DW_TAG_packed_type:
23637 case DW_TAG_pointer_type:
23638 case DW_TAG_reference_type:
23639 case DW_TAG_rvalue_reference_type:
23640 case DW_TAG_volatile_type:
23641 case DW_TAG_typedef:
23642 case DW_TAG_array_type:
23643 case DW_TAG_interface_type:
23644 case DW_TAG_friend:
23645 case DW_TAG_variant_part:
23646 case DW_TAG_enumeration_type:
23647 case DW_TAG_subroutine_type:
23648 case DW_TAG_string_type:
23649 case DW_TAG_set_type:
23650 case DW_TAG_subrange_type:
23651 case DW_TAG_ptr_to_member_type:
23652 case DW_TAG_file_type:
23653 if (die->die_perennial_p)
23654 break;
23656 /* It's a type node --- don't mark it. */
23657 return;
23659 default:
23660 /* Mark everything else. */
23661 break;
23664 if (die->die_mark == 0)
23666 die->die_mark = 1;
23668 /* Now, mark any dies referenced from here. */
23669 prune_unused_types_walk_attribs (die);
23672 die->die_mark = 2;
23674 /* Mark children. */
23675 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23678 /* Increment the string counts on strings referred to from DIE's
23679 attributes. */
23681 static void
23682 prune_unused_types_update_strings (dw_die_ref die)
23684 dw_attr_ref a;
23685 unsigned ix;
23687 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23688 if (AT_class (a) == dw_val_class_str)
23690 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23691 s->refcount++;
23692 /* Avoid unnecessarily putting strings that are used less than
23693 twice in the hash table. */
23694 if (s->refcount
23695 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23697 indirect_string_node **slot
23698 = debug_str_hash->find_slot_with_hash (s->str,
23699 htab_hash_string (s->str),
23700 INSERT);
23701 gcc_assert (*slot == NULL);
23702 *slot = s;
23707 /* Remove from the tree DIE any dies that aren't marked. */
23709 static void
23710 prune_unused_types_prune (dw_die_ref die)
23712 dw_die_ref c;
23714 gcc_assert (die->die_mark);
23715 prune_unused_types_update_strings (die);
23717 if (! die->die_child)
23718 return;
23720 c = die->die_child;
23721 do {
23722 dw_die_ref prev = c;
23723 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23724 if (c == die->die_child)
23726 /* No marked children between 'prev' and the end of the list. */
23727 if (prev == c)
23728 /* No marked children at all. */
23729 die->die_child = NULL;
23730 else
23732 prev->die_sib = c->die_sib;
23733 die->die_child = prev;
23735 return;
23738 if (c != prev->die_sib)
23739 prev->die_sib = c;
23740 prune_unused_types_prune (c);
23741 } while (c != die->die_child);
23744 /* Remove dies representing declarations that we never use. */
23746 static void
23747 prune_unused_types (void)
23749 unsigned int i;
23750 limbo_die_node *node;
23751 comdat_type_node *ctnode;
23752 pubname_ref pub;
23753 dw_die_ref base_type;
23755 #if ENABLE_ASSERT_CHECKING
23756 /* All the marks should already be clear. */
23757 verify_marks_clear (comp_unit_die ());
23758 for (node = limbo_die_list; node; node = node->next)
23759 verify_marks_clear (node->die);
23760 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23761 verify_marks_clear (ctnode->root_die);
23762 #endif /* ENABLE_ASSERT_CHECKING */
23764 /* Mark types that are used in global variables. */
23765 premark_types_used_by_global_vars ();
23767 /* Set the mark on nodes that are actually used. */
23768 prune_unused_types_walk (comp_unit_die ());
23769 for (node = limbo_die_list; node; node = node->next)
23770 prune_unused_types_walk (node->die);
23771 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23773 prune_unused_types_walk (ctnode->root_die);
23774 prune_unused_types_mark (ctnode->type_die, 1);
23777 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23778 are unusual in that they are pubnames that are the children of pubtypes.
23779 They should only be marked via their parent DW_TAG_enumeration_type die,
23780 not as roots in themselves. */
23781 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23782 if (pub->die->die_tag != DW_TAG_enumerator)
23783 prune_unused_types_mark (pub->die, 1);
23784 for (i = 0; base_types.iterate (i, &base_type); i++)
23785 prune_unused_types_mark (base_type, 1);
23787 if (debug_str_hash)
23788 debug_str_hash->empty ();
23789 if (skeleton_debug_str_hash)
23790 skeleton_debug_str_hash->empty ();
23791 prune_unused_types_prune (comp_unit_die ());
23792 for (node = limbo_die_list; node; node = node->next)
23793 prune_unused_types_prune (node->die);
23794 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23795 prune_unused_types_prune (ctnode->root_die);
23797 /* Leave the marks clear. */
23798 prune_unmark_dies (comp_unit_die ());
23799 for (node = limbo_die_list; node; node = node->next)
23800 prune_unmark_dies (node->die);
23801 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23802 prune_unmark_dies (ctnode->root_die);
23805 /* Set the parameter to true if there are any relative pathnames in
23806 the file table. */
23808 file_table_relative_p (dwarf_file_data **slot, bool *p)
23810 struct dwarf_file_data *d = *slot;
23811 if (!IS_ABSOLUTE_PATH (d->filename))
23813 *p = true;
23814 return 0;
23816 return 1;
23819 /* Helpers to manipulate hash table of comdat type units. */
23821 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23823 typedef comdat_type_node *value_type;
23824 typedef comdat_type_node *compare_type;
23825 static inline hashval_t hash (const comdat_type_node *);
23826 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23829 inline hashval_t
23830 comdat_type_hasher::hash (const comdat_type_node *type_node)
23832 hashval_t h;
23833 memcpy (&h, type_node->signature, sizeof (h));
23834 return h;
23837 inline bool
23838 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23839 const comdat_type_node *type_node_2)
23841 return (! memcmp (type_node_1->signature, type_node_2->signature,
23842 DWARF_TYPE_SIGNATURE_SIZE));
23845 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23846 to the location it would have been added, should we know its
23847 DECL_ASSEMBLER_NAME when we added other attributes. This will
23848 probably improve compactness of debug info, removing equivalent
23849 abbrevs, and hide any differences caused by deferring the
23850 computation of the assembler name, triggered by e.g. PCH. */
23852 static inline void
23853 move_linkage_attr (dw_die_ref die)
23855 unsigned ix = vec_safe_length (die->die_attr);
23856 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23858 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23859 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23861 while (--ix > 0)
23863 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23865 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23866 break;
23869 if (ix != vec_safe_length (die->die_attr) - 1)
23871 die->die_attr->pop ();
23872 die->die_attr->quick_insert (ix, linkage);
23876 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23877 referenced from typed stack ops and count how often they are used. */
23879 static void
23880 mark_base_types (dw_loc_descr_ref loc)
23882 dw_die_ref base_type = NULL;
23884 for (; loc; loc = loc->dw_loc_next)
23886 switch (loc->dw_loc_opc)
23888 case DW_OP_GNU_regval_type:
23889 case DW_OP_GNU_deref_type:
23890 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23891 break;
23892 case DW_OP_GNU_convert:
23893 case DW_OP_GNU_reinterpret:
23894 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23895 continue;
23896 /* FALLTHRU */
23897 case DW_OP_GNU_const_type:
23898 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23899 break;
23900 case DW_OP_GNU_entry_value:
23901 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23902 continue;
23903 default:
23904 continue;
23906 gcc_assert (base_type->die_parent == comp_unit_die ());
23907 if (base_type->die_mark)
23908 base_type->die_mark++;
23909 else
23911 base_types.safe_push (base_type);
23912 base_type->die_mark = 1;
23917 /* Comparison function for sorting marked base types. */
23919 static int
23920 base_type_cmp (const void *x, const void *y)
23922 dw_die_ref dx = *(const dw_die_ref *) x;
23923 dw_die_ref dy = *(const dw_die_ref *) y;
23924 unsigned int byte_size1, byte_size2;
23925 unsigned int encoding1, encoding2;
23926 if (dx->die_mark > dy->die_mark)
23927 return -1;
23928 if (dx->die_mark < dy->die_mark)
23929 return 1;
23930 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23931 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23932 if (byte_size1 < byte_size2)
23933 return 1;
23934 if (byte_size1 > byte_size2)
23935 return -1;
23936 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23937 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23938 if (encoding1 < encoding2)
23939 return 1;
23940 if (encoding1 > encoding2)
23941 return -1;
23942 return 0;
23945 /* Move base types marked by mark_base_types as early as possible
23946 in the CU, sorted by decreasing usage count both to make the
23947 uleb128 references as small as possible and to make sure they
23948 will have die_offset already computed by calc_die_sizes when
23949 sizes of typed stack loc ops is computed. */
23951 static void
23952 move_marked_base_types (void)
23954 unsigned int i;
23955 dw_die_ref base_type, die, c;
23957 if (base_types.is_empty ())
23958 return;
23960 /* Sort by decreasing usage count, they will be added again in that
23961 order later on. */
23962 base_types.qsort (base_type_cmp);
23963 die = comp_unit_die ();
23964 c = die->die_child;
23967 dw_die_ref prev = c;
23968 c = c->die_sib;
23969 while (c->die_mark)
23971 remove_child_with_prev (c, prev);
23972 /* As base types got marked, there must be at least
23973 one node other than DW_TAG_base_type. */
23974 gcc_assert (c != c->die_sib);
23975 c = c->die_sib;
23978 while (c != die->die_child);
23979 gcc_assert (die->die_child);
23980 c = die->die_child;
23981 for (i = 0; base_types.iterate (i, &base_type); i++)
23983 base_type->die_mark = 0;
23984 base_type->die_sib = c->die_sib;
23985 c->die_sib = base_type;
23986 c = base_type;
23990 /* Helper function for resolve_addr, attempt to resolve
23991 one CONST_STRING, return true if successful. Similarly verify that
23992 SYMBOL_REFs refer to variables emitted in the current CU. */
23994 static bool
23995 resolve_one_addr (rtx *addr)
23997 rtx rtl = *addr;
23999 if (GET_CODE (rtl) == CONST_STRING)
24001 size_t len = strlen (XSTR (rtl, 0)) + 1;
24002 tree t = build_string (len, XSTR (rtl, 0));
24003 tree tlen = size_int (len - 1);
24004 TREE_TYPE (t)
24005 = build_array_type (char_type_node, build_index_type (tlen));
24006 rtl = lookup_constant_def (t);
24007 if (!rtl || !MEM_P (rtl))
24008 return false;
24009 rtl = XEXP (rtl, 0);
24010 if (GET_CODE (rtl) == SYMBOL_REF
24011 && SYMBOL_REF_DECL (rtl)
24012 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24013 return false;
24014 vec_safe_push (used_rtx_array, rtl);
24015 *addr = rtl;
24016 return true;
24019 if (GET_CODE (rtl) == SYMBOL_REF
24020 && SYMBOL_REF_DECL (rtl))
24022 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24024 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24025 return false;
24027 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24028 return false;
24031 if (GET_CODE (rtl) == CONST)
24033 subrtx_ptr_iterator::array_type array;
24034 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24035 if (!resolve_one_addr (*iter))
24036 return false;
24039 return true;
24042 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24043 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24044 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24046 static rtx
24047 string_cst_pool_decl (tree t)
24049 rtx rtl = output_constant_def (t, 1);
24050 unsigned char *array;
24051 dw_loc_descr_ref l;
24052 tree decl;
24053 size_t len;
24054 dw_die_ref ref;
24056 if (!rtl || !MEM_P (rtl))
24057 return NULL_RTX;
24058 rtl = XEXP (rtl, 0);
24059 if (GET_CODE (rtl) != SYMBOL_REF
24060 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24061 return NULL_RTX;
24063 decl = SYMBOL_REF_DECL (rtl);
24064 if (!lookup_decl_die (decl))
24066 len = TREE_STRING_LENGTH (t);
24067 vec_safe_push (used_rtx_array, rtl);
24068 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24069 array = ggc_vec_alloc<unsigned char> (len);
24070 memcpy (array, TREE_STRING_POINTER (t), len);
24071 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24072 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24073 l->dw_loc_oprnd2.v.val_vec.length = len;
24074 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24075 l->dw_loc_oprnd2.v.val_vec.array = array;
24076 add_AT_loc (ref, DW_AT_location, l);
24077 equate_decl_number_to_die (decl, ref);
24079 return rtl;
24082 /* Helper function of resolve_addr_in_expr. LOC is
24083 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24084 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24085 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24086 with DW_OP_GNU_implicit_pointer if possible
24087 and return true, if unsuccessful, return false. */
24089 static bool
24090 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24092 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24093 HOST_WIDE_INT offset = 0;
24094 dw_die_ref ref = NULL;
24095 tree decl;
24097 if (GET_CODE (rtl) == CONST
24098 && GET_CODE (XEXP (rtl, 0)) == PLUS
24099 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24101 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24102 rtl = XEXP (XEXP (rtl, 0), 0);
24104 if (GET_CODE (rtl) == CONST_STRING)
24106 size_t len = strlen (XSTR (rtl, 0)) + 1;
24107 tree t = build_string (len, XSTR (rtl, 0));
24108 tree tlen = size_int (len - 1);
24110 TREE_TYPE (t)
24111 = build_array_type (char_type_node, build_index_type (tlen));
24112 rtl = string_cst_pool_decl (t);
24113 if (!rtl)
24114 return false;
24116 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24118 decl = SYMBOL_REF_DECL (rtl);
24119 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24121 ref = lookup_decl_die (decl);
24122 if (ref && (get_AT (ref, DW_AT_location)
24123 || get_AT (ref, DW_AT_const_value)))
24125 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24126 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24127 loc->dw_loc_oprnd1.val_entry = NULL;
24128 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24129 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24130 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24131 loc->dw_loc_oprnd2.v.val_int = offset;
24132 return true;
24136 return false;
24139 /* Helper function for resolve_addr, handle one location
24140 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24141 the location list couldn't be resolved. */
24143 static bool
24144 resolve_addr_in_expr (dw_loc_descr_ref loc)
24146 dw_loc_descr_ref keep = NULL;
24147 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24148 switch (loc->dw_loc_opc)
24150 case DW_OP_addr:
24151 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24153 if ((prev == NULL
24154 || prev->dw_loc_opc == DW_OP_piece
24155 || prev->dw_loc_opc == DW_OP_bit_piece)
24156 && loc->dw_loc_next
24157 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24158 && !dwarf_strict
24159 && optimize_one_addr_into_implicit_ptr (loc))
24160 break;
24161 return false;
24163 break;
24164 case DW_OP_GNU_addr_index:
24165 case DW_OP_GNU_const_index:
24166 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24167 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24169 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24170 if (!resolve_one_addr (&rtl))
24171 return false;
24172 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24173 loc->dw_loc_oprnd1.val_entry =
24174 add_addr_table_entry (rtl, ate_kind_rtx);
24176 break;
24177 case DW_OP_const4u:
24178 case DW_OP_const8u:
24179 if (loc->dtprel
24180 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24181 return false;
24182 break;
24183 case DW_OP_plus_uconst:
24184 if (size_of_loc_descr (loc)
24185 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24187 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24189 dw_loc_descr_ref repl
24190 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24191 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24192 add_loc_descr (&repl, loc->dw_loc_next);
24193 *loc = *repl;
24195 break;
24196 case DW_OP_implicit_value:
24197 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24198 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24199 return false;
24200 break;
24201 case DW_OP_GNU_implicit_pointer:
24202 case DW_OP_GNU_parameter_ref:
24203 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24205 dw_die_ref ref
24206 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24207 if (ref == NULL)
24208 return false;
24209 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24210 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24211 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24213 break;
24214 case DW_OP_GNU_const_type:
24215 case DW_OP_GNU_regval_type:
24216 case DW_OP_GNU_deref_type:
24217 case DW_OP_GNU_convert:
24218 case DW_OP_GNU_reinterpret:
24219 while (loc->dw_loc_next
24220 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24222 dw_die_ref base1, base2;
24223 unsigned enc1, enc2, size1, size2;
24224 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24225 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24226 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24227 else if (loc->dw_loc_oprnd1.val_class
24228 == dw_val_class_unsigned_const)
24229 break;
24230 else
24231 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24232 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24233 == dw_val_class_unsigned_const)
24234 break;
24235 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24236 gcc_assert (base1->die_tag == DW_TAG_base_type
24237 && base2->die_tag == DW_TAG_base_type);
24238 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24239 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24240 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24241 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24242 if (size1 == size2
24243 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24244 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24245 && loc != keep)
24246 || enc1 == enc2))
24248 /* Optimize away next DW_OP_GNU_convert after
24249 adjusting LOC's base type die reference. */
24250 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24251 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24252 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24253 else
24254 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24255 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24256 continue;
24258 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24259 point typed stack entry. */
24260 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24261 keep = loc->dw_loc_next;
24262 break;
24264 break;
24265 default:
24266 break;
24268 return true;
24271 /* Helper function of resolve_addr. DIE had DW_AT_location of
24272 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24273 and DW_OP_addr couldn't be resolved. resolve_addr has already
24274 removed the DW_AT_location attribute. This function attempts to
24275 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24276 to it or DW_AT_const_value attribute, if possible. */
24278 static void
24279 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24281 if (TREE_CODE (decl) != VAR_DECL
24282 || lookup_decl_die (decl) != die
24283 || DECL_EXTERNAL (decl)
24284 || !TREE_STATIC (decl)
24285 || DECL_INITIAL (decl) == NULL_TREE
24286 || DECL_P (DECL_INITIAL (decl))
24287 || get_AT (die, DW_AT_const_value))
24288 return;
24290 tree init = DECL_INITIAL (decl);
24291 HOST_WIDE_INT offset = 0;
24292 /* For variables that have been optimized away and thus
24293 don't have a memory location, see if we can emit
24294 DW_AT_const_value instead. */
24295 if (tree_add_const_value_attribute (die, init))
24296 return;
24297 if (dwarf_strict)
24298 return;
24299 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24300 and ADDR_EXPR refers to a decl that has DW_AT_location or
24301 DW_AT_const_value (but isn't addressable, otherwise
24302 resolving the original DW_OP_addr wouldn't fail), see if
24303 we can add DW_OP_GNU_implicit_pointer. */
24304 STRIP_NOPS (init);
24305 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24306 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24308 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24309 init = TREE_OPERAND (init, 0);
24310 STRIP_NOPS (init);
24312 if (TREE_CODE (init) != ADDR_EXPR)
24313 return;
24314 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24315 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24316 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24317 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24318 && TREE_OPERAND (init, 0) != decl))
24320 dw_die_ref ref;
24321 dw_loc_descr_ref l;
24323 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24325 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24326 if (!rtl)
24327 return;
24328 decl = SYMBOL_REF_DECL (rtl);
24330 else
24331 decl = TREE_OPERAND (init, 0);
24332 ref = lookup_decl_die (decl);
24333 if (ref == NULL
24334 || (!get_AT (ref, DW_AT_location)
24335 && !get_AT (ref, DW_AT_const_value)))
24336 return;
24337 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24338 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24339 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24340 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24341 add_AT_loc (die, DW_AT_location, l);
24345 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24346 an address in .rodata section if the string literal is emitted there,
24347 or remove the containing location list or replace DW_AT_const_value
24348 with DW_AT_location and empty location expression, if it isn't found
24349 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24350 to something that has been emitted in the current CU. */
24352 static void
24353 resolve_addr (dw_die_ref die)
24355 dw_die_ref c;
24356 dw_attr_ref a;
24357 dw_loc_list_ref *curr, *start, loc;
24358 unsigned ix;
24360 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24361 switch (AT_class (a))
24363 case dw_val_class_loc_list:
24364 start = curr = AT_loc_list_ptr (a);
24365 loc = *curr;
24366 gcc_assert (loc);
24367 /* The same list can be referenced more than once. See if we have
24368 already recorded the result from a previous pass. */
24369 if (loc->replaced)
24370 *curr = loc->dw_loc_next;
24371 else if (!loc->resolved_addr)
24373 /* As things stand, we do not expect or allow one die to
24374 reference a suffix of another die's location list chain.
24375 References must be identical or completely separate.
24376 There is therefore no need to cache the result of this
24377 pass on any list other than the first; doing so
24378 would lead to unnecessary writes. */
24379 while (*curr)
24381 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24382 if (!resolve_addr_in_expr ((*curr)->expr))
24384 dw_loc_list_ref next = (*curr)->dw_loc_next;
24385 dw_loc_descr_ref l = (*curr)->expr;
24387 if (next && (*curr)->ll_symbol)
24389 gcc_assert (!next->ll_symbol);
24390 next->ll_symbol = (*curr)->ll_symbol;
24392 if (dwarf_split_debug_info)
24393 remove_loc_list_addr_table_entries (l);
24394 *curr = next;
24396 else
24398 mark_base_types ((*curr)->expr);
24399 curr = &(*curr)->dw_loc_next;
24402 if (loc == *start)
24403 loc->resolved_addr = 1;
24404 else
24406 loc->replaced = 1;
24407 loc->dw_loc_next = *start;
24410 if (!*start)
24412 remove_AT (die, a->dw_attr);
24413 ix--;
24415 break;
24416 case dw_val_class_loc:
24418 dw_loc_descr_ref l = AT_loc (a);
24419 /* For -gdwarf-2 don't attempt to optimize
24420 DW_AT_data_member_location containing
24421 DW_OP_plus_uconst - older consumers might
24422 rely on it being that op instead of a more complex,
24423 but shorter, location description. */
24424 if ((dwarf_version > 2
24425 || a->dw_attr != DW_AT_data_member_location
24426 || l == NULL
24427 || l->dw_loc_opc != DW_OP_plus_uconst
24428 || l->dw_loc_next != NULL)
24429 && !resolve_addr_in_expr (l))
24431 if (dwarf_split_debug_info)
24432 remove_loc_list_addr_table_entries (l);
24433 if (l != NULL
24434 && l->dw_loc_next == NULL
24435 && l->dw_loc_opc == DW_OP_addr
24436 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24437 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24438 && a->dw_attr == DW_AT_location)
24440 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24441 remove_AT (die, a->dw_attr);
24442 ix--;
24443 optimize_location_into_implicit_ptr (die, decl);
24444 break;
24446 remove_AT (die, a->dw_attr);
24447 ix--;
24449 else
24450 mark_base_types (l);
24452 break;
24453 case dw_val_class_addr:
24454 if (a->dw_attr == DW_AT_const_value
24455 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24457 if (AT_index (a) != NOT_INDEXED)
24458 remove_addr_table_entry (a->dw_attr_val.val_entry);
24459 remove_AT (die, a->dw_attr);
24460 ix--;
24462 if (die->die_tag == DW_TAG_GNU_call_site
24463 && a->dw_attr == DW_AT_abstract_origin)
24465 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24466 dw_die_ref tdie = lookup_decl_die (tdecl);
24467 dw_die_ref cdie;
24468 if (tdie == NULL
24469 && DECL_EXTERNAL (tdecl)
24470 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24471 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24473 /* Creating a full DIE for tdecl is overly expensive and
24474 at this point even wrong when in the LTO phase
24475 as it can end up generating new type DIEs we didn't
24476 output and thus optimize_external_refs will crash. */
24477 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24478 add_AT_flag (tdie, DW_AT_external, 1);
24479 add_AT_flag (tdie, DW_AT_declaration, 1);
24480 add_linkage_attr (tdie, tdecl);
24481 add_name_and_src_coords_attributes (tdie, tdecl);
24482 equate_decl_number_to_die (tdecl, tdie);
24484 if (tdie)
24486 a->dw_attr_val.val_class = dw_val_class_die_ref;
24487 a->dw_attr_val.v.val_die_ref.die = tdie;
24488 a->dw_attr_val.v.val_die_ref.external = 0;
24490 else
24492 if (AT_index (a) != NOT_INDEXED)
24493 remove_addr_table_entry (a->dw_attr_val.val_entry);
24494 remove_AT (die, a->dw_attr);
24495 ix--;
24498 break;
24499 default:
24500 break;
24503 FOR_EACH_CHILD (die, c, resolve_addr (c));
24506 /* Helper routines for optimize_location_lists.
24507 This pass tries to share identical local lists in .debug_loc
24508 section. */
24510 /* Iteratively hash operands of LOC opcode into HSTATE. */
24512 static void
24513 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24515 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24516 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24518 switch (loc->dw_loc_opc)
24520 case DW_OP_const4u:
24521 case DW_OP_const8u:
24522 if (loc->dtprel)
24523 goto hash_addr;
24524 /* FALLTHRU */
24525 case DW_OP_const1u:
24526 case DW_OP_const1s:
24527 case DW_OP_const2u:
24528 case DW_OP_const2s:
24529 case DW_OP_const4s:
24530 case DW_OP_const8s:
24531 case DW_OP_constu:
24532 case DW_OP_consts:
24533 case DW_OP_pick:
24534 case DW_OP_plus_uconst:
24535 case DW_OP_breg0:
24536 case DW_OP_breg1:
24537 case DW_OP_breg2:
24538 case DW_OP_breg3:
24539 case DW_OP_breg4:
24540 case DW_OP_breg5:
24541 case DW_OP_breg6:
24542 case DW_OP_breg7:
24543 case DW_OP_breg8:
24544 case DW_OP_breg9:
24545 case DW_OP_breg10:
24546 case DW_OP_breg11:
24547 case DW_OP_breg12:
24548 case DW_OP_breg13:
24549 case DW_OP_breg14:
24550 case DW_OP_breg15:
24551 case DW_OP_breg16:
24552 case DW_OP_breg17:
24553 case DW_OP_breg18:
24554 case DW_OP_breg19:
24555 case DW_OP_breg20:
24556 case DW_OP_breg21:
24557 case DW_OP_breg22:
24558 case DW_OP_breg23:
24559 case DW_OP_breg24:
24560 case DW_OP_breg25:
24561 case DW_OP_breg26:
24562 case DW_OP_breg27:
24563 case DW_OP_breg28:
24564 case DW_OP_breg29:
24565 case DW_OP_breg30:
24566 case DW_OP_breg31:
24567 case DW_OP_regx:
24568 case DW_OP_fbreg:
24569 case DW_OP_piece:
24570 case DW_OP_deref_size:
24571 case DW_OP_xderef_size:
24572 hstate.add_object (val1->v.val_int);
24573 break;
24574 case DW_OP_skip:
24575 case DW_OP_bra:
24577 int offset;
24579 gcc_assert (val1->val_class == dw_val_class_loc);
24580 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24581 hstate.add_object (offset);
24583 break;
24584 case DW_OP_implicit_value:
24585 hstate.add_object (val1->v.val_unsigned);
24586 switch (val2->val_class)
24588 case dw_val_class_const:
24589 hstate.add_object (val2->v.val_int);
24590 break;
24591 case dw_val_class_vec:
24593 unsigned int elt_size = val2->v.val_vec.elt_size;
24594 unsigned int len = val2->v.val_vec.length;
24596 hstate.add_int (elt_size);
24597 hstate.add_int (len);
24598 hstate.add (val2->v.val_vec.array, len * elt_size);
24600 break;
24601 case dw_val_class_const_double:
24602 hstate.add_object (val2->v.val_double.low);
24603 hstate.add_object (val2->v.val_double.high);
24604 break;
24605 case dw_val_class_wide_int:
24606 hstate.add (val2->v.val_wide->get_val (),
24607 get_full_len (*val2->v.val_wide)
24608 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24609 break;
24610 case dw_val_class_addr:
24611 inchash::add_rtx (val2->v.val_addr, hstate);
24612 break;
24613 default:
24614 gcc_unreachable ();
24616 break;
24617 case DW_OP_bregx:
24618 case DW_OP_bit_piece:
24619 hstate.add_object (val1->v.val_int);
24620 hstate.add_object (val2->v.val_int);
24621 break;
24622 case DW_OP_addr:
24623 hash_addr:
24624 if (loc->dtprel)
24626 unsigned char dtprel = 0xd1;
24627 hstate.add_object (dtprel);
24629 inchash::add_rtx (val1->v.val_addr, hstate);
24630 break;
24631 case DW_OP_GNU_addr_index:
24632 case DW_OP_GNU_const_index:
24634 if (loc->dtprel)
24636 unsigned char dtprel = 0xd1;
24637 hstate.add_object (dtprel);
24639 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24641 break;
24642 case DW_OP_GNU_implicit_pointer:
24643 hstate.add_int (val2->v.val_int);
24644 break;
24645 case DW_OP_GNU_entry_value:
24646 hstate.add_object (val1->v.val_loc);
24647 break;
24648 case DW_OP_GNU_regval_type:
24649 case DW_OP_GNU_deref_type:
24651 unsigned int byte_size
24652 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24653 unsigned int encoding
24654 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24655 hstate.add_object (val1->v.val_int);
24656 hstate.add_object (byte_size);
24657 hstate.add_object (encoding);
24659 break;
24660 case DW_OP_GNU_convert:
24661 case DW_OP_GNU_reinterpret:
24662 if (val1->val_class == dw_val_class_unsigned_const)
24664 hstate.add_object (val1->v.val_unsigned);
24665 break;
24667 /* FALLTHRU */
24668 case DW_OP_GNU_const_type:
24670 unsigned int byte_size
24671 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24672 unsigned int encoding
24673 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24674 hstate.add_object (byte_size);
24675 hstate.add_object (encoding);
24676 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24677 break;
24678 hstate.add_object (val2->val_class);
24679 switch (val2->val_class)
24681 case dw_val_class_const:
24682 hstate.add_object (val2->v.val_int);
24683 break;
24684 case dw_val_class_vec:
24686 unsigned int elt_size = val2->v.val_vec.elt_size;
24687 unsigned int len = val2->v.val_vec.length;
24689 hstate.add_object (elt_size);
24690 hstate.add_object (len);
24691 hstate.add (val2->v.val_vec.array, len * elt_size);
24693 break;
24694 case dw_val_class_const_double:
24695 hstate.add_object (val2->v.val_double.low);
24696 hstate.add_object (val2->v.val_double.high);
24697 break;
24698 case dw_val_class_wide_int:
24699 hstate.add (val2->v.val_wide->get_val (),
24700 get_full_len (*val2->v.val_wide)
24701 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24702 break;
24703 default:
24704 gcc_unreachable ();
24707 break;
24709 default:
24710 /* Other codes have no operands. */
24711 break;
24715 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24717 static inline void
24718 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24720 dw_loc_descr_ref l;
24721 bool sizes_computed = false;
24722 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24723 size_of_locs (loc);
24725 for (l = loc; l != NULL; l = l->dw_loc_next)
24727 enum dwarf_location_atom opc = l->dw_loc_opc;
24728 hstate.add_object (opc);
24729 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24731 size_of_locs (loc);
24732 sizes_computed = true;
24734 hash_loc_operands (l, hstate);
24738 /* Compute hash of the whole location list LIST_HEAD. */
24740 static inline void
24741 hash_loc_list (dw_loc_list_ref list_head)
24743 dw_loc_list_ref curr = list_head;
24744 inchash::hash hstate;
24746 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24748 hstate.add (curr->begin, strlen (curr->begin) + 1);
24749 hstate.add (curr->end, strlen (curr->end) + 1);
24750 if (curr->section)
24751 hstate.add (curr->section, strlen (curr->section) + 1);
24752 hash_locs (curr->expr, hstate);
24754 list_head->hash = hstate.end ();
24757 /* Return true if X and Y opcodes have the same operands. */
24759 static inline bool
24760 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24762 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24763 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24764 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24765 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24767 switch (x->dw_loc_opc)
24769 case DW_OP_const4u:
24770 case DW_OP_const8u:
24771 if (x->dtprel)
24772 goto hash_addr;
24773 /* FALLTHRU */
24774 case DW_OP_const1u:
24775 case DW_OP_const1s:
24776 case DW_OP_const2u:
24777 case DW_OP_const2s:
24778 case DW_OP_const4s:
24779 case DW_OP_const8s:
24780 case DW_OP_constu:
24781 case DW_OP_consts:
24782 case DW_OP_pick:
24783 case DW_OP_plus_uconst:
24784 case DW_OP_breg0:
24785 case DW_OP_breg1:
24786 case DW_OP_breg2:
24787 case DW_OP_breg3:
24788 case DW_OP_breg4:
24789 case DW_OP_breg5:
24790 case DW_OP_breg6:
24791 case DW_OP_breg7:
24792 case DW_OP_breg8:
24793 case DW_OP_breg9:
24794 case DW_OP_breg10:
24795 case DW_OP_breg11:
24796 case DW_OP_breg12:
24797 case DW_OP_breg13:
24798 case DW_OP_breg14:
24799 case DW_OP_breg15:
24800 case DW_OP_breg16:
24801 case DW_OP_breg17:
24802 case DW_OP_breg18:
24803 case DW_OP_breg19:
24804 case DW_OP_breg20:
24805 case DW_OP_breg21:
24806 case DW_OP_breg22:
24807 case DW_OP_breg23:
24808 case DW_OP_breg24:
24809 case DW_OP_breg25:
24810 case DW_OP_breg26:
24811 case DW_OP_breg27:
24812 case DW_OP_breg28:
24813 case DW_OP_breg29:
24814 case DW_OP_breg30:
24815 case DW_OP_breg31:
24816 case DW_OP_regx:
24817 case DW_OP_fbreg:
24818 case DW_OP_piece:
24819 case DW_OP_deref_size:
24820 case DW_OP_xderef_size:
24821 return valx1->v.val_int == valy1->v.val_int;
24822 case DW_OP_skip:
24823 case DW_OP_bra:
24824 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24825 can cause irrelevant differences in dw_loc_addr. */
24826 gcc_assert (valx1->val_class == dw_val_class_loc
24827 && valy1->val_class == dw_val_class_loc
24828 && (dwarf_split_debug_info
24829 || x->dw_loc_addr == y->dw_loc_addr));
24830 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24831 case DW_OP_implicit_value:
24832 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24833 || valx2->val_class != valy2->val_class)
24834 return false;
24835 switch (valx2->val_class)
24837 case dw_val_class_const:
24838 return valx2->v.val_int == valy2->v.val_int;
24839 case dw_val_class_vec:
24840 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24841 && valx2->v.val_vec.length == valy2->v.val_vec.length
24842 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24843 valx2->v.val_vec.elt_size
24844 * valx2->v.val_vec.length) == 0;
24845 case dw_val_class_const_double:
24846 return valx2->v.val_double.low == valy2->v.val_double.low
24847 && valx2->v.val_double.high == valy2->v.val_double.high;
24848 case dw_val_class_wide_int:
24849 return *valx2->v.val_wide == *valy2->v.val_wide;
24850 case dw_val_class_addr:
24851 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24852 default:
24853 gcc_unreachable ();
24855 case DW_OP_bregx:
24856 case DW_OP_bit_piece:
24857 return valx1->v.val_int == valy1->v.val_int
24858 && valx2->v.val_int == valy2->v.val_int;
24859 case DW_OP_addr:
24860 hash_addr:
24861 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24862 case DW_OP_GNU_addr_index:
24863 case DW_OP_GNU_const_index:
24865 rtx ax1 = valx1->val_entry->addr.rtl;
24866 rtx ay1 = valy1->val_entry->addr.rtl;
24867 return rtx_equal_p (ax1, ay1);
24869 case DW_OP_GNU_implicit_pointer:
24870 return valx1->val_class == dw_val_class_die_ref
24871 && valx1->val_class == valy1->val_class
24872 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24873 && valx2->v.val_int == valy2->v.val_int;
24874 case DW_OP_GNU_entry_value:
24875 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24876 case DW_OP_GNU_const_type:
24877 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24878 || valx2->val_class != valy2->val_class)
24879 return false;
24880 switch (valx2->val_class)
24882 case dw_val_class_const:
24883 return valx2->v.val_int == valy2->v.val_int;
24884 case dw_val_class_vec:
24885 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24886 && valx2->v.val_vec.length == valy2->v.val_vec.length
24887 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24888 valx2->v.val_vec.elt_size
24889 * valx2->v.val_vec.length) == 0;
24890 case dw_val_class_const_double:
24891 return valx2->v.val_double.low == valy2->v.val_double.low
24892 && valx2->v.val_double.high == valy2->v.val_double.high;
24893 case dw_val_class_wide_int:
24894 return *valx2->v.val_wide == *valy2->v.val_wide;
24895 default:
24896 gcc_unreachable ();
24898 case DW_OP_GNU_regval_type:
24899 case DW_OP_GNU_deref_type:
24900 return valx1->v.val_int == valy1->v.val_int
24901 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24902 case DW_OP_GNU_convert:
24903 case DW_OP_GNU_reinterpret:
24904 if (valx1->val_class != valy1->val_class)
24905 return false;
24906 if (valx1->val_class == dw_val_class_unsigned_const)
24907 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24908 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24909 case DW_OP_GNU_parameter_ref:
24910 return valx1->val_class == dw_val_class_die_ref
24911 && valx1->val_class == valy1->val_class
24912 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24913 default:
24914 /* Other codes have no operands. */
24915 return true;
24919 /* Return true if DWARF location expressions X and Y are the same. */
24921 static inline bool
24922 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24924 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24925 if (x->dw_loc_opc != y->dw_loc_opc
24926 || x->dtprel != y->dtprel
24927 || !compare_loc_operands (x, y))
24928 break;
24929 return x == NULL && y == NULL;
24932 /* Hashtable helpers. */
24934 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24936 typedef dw_loc_list_struct *value_type;
24937 typedef dw_loc_list_struct *compare_type;
24938 static inline hashval_t hash (const dw_loc_list_struct *);
24939 static inline bool equal (const dw_loc_list_struct *,
24940 const dw_loc_list_struct *);
24943 /* Return precomputed hash of location list X. */
24945 inline hashval_t
24946 loc_list_hasher::hash (const dw_loc_list_struct *x)
24948 return x->hash;
24951 /* Return true if location lists A and B are the same. */
24953 inline bool
24954 loc_list_hasher::equal (const dw_loc_list_struct *a,
24955 const dw_loc_list_struct *b)
24957 if (a == b)
24958 return 1;
24959 if (a->hash != b->hash)
24960 return 0;
24961 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24962 if (strcmp (a->begin, b->begin) != 0
24963 || strcmp (a->end, b->end) != 0
24964 || (a->section == NULL) != (b->section == NULL)
24965 || (a->section && strcmp (a->section, b->section) != 0)
24966 || !compare_locs (a->expr, b->expr))
24967 break;
24968 return a == NULL && b == NULL;
24971 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24974 /* Recursively optimize location lists referenced from DIE
24975 children and share them whenever possible. */
24977 static void
24978 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24980 dw_die_ref c;
24981 dw_attr_ref a;
24982 unsigned ix;
24983 dw_loc_list_struct **slot;
24985 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24986 if (AT_class (a) == dw_val_class_loc_list)
24988 dw_loc_list_ref list = AT_loc_list (a);
24989 /* TODO: perform some optimizations here, before hashing
24990 it and storing into the hash table. */
24991 hash_loc_list (list);
24992 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24993 if (*slot == NULL)
24994 *slot = list;
24995 else
24996 a->dw_attr_val.v.val_loc_list = *slot;
24999 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25003 /* Recursively assign each location list a unique index into the debug_addr
25004 section. */
25006 static void
25007 index_location_lists (dw_die_ref die)
25009 dw_die_ref c;
25010 dw_attr_ref a;
25011 unsigned ix;
25013 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25014 if (AT_class (a) == dw_val_class_loc_list)
25016 dw_loc_list_ref list = AT_loc_list (a);
25017 dw_loc_list_ref curr;
25018 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25020 /* Don't index an entry that has already been indexed
25021 or won't be output. */
25022 if (curr->begin_entry != NULL
25023 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25024 continue;
25026 curr->begin_entry
25027 = add_addr_table_entry (xstrdup (curr->begin),
25028 ate_kind_label);
25032 FOR_EACH_CHILD (die, c, index_location_lists (c));
25035 /* Optimize location lists referenced from DIE
25036 children and share them whenever possible. */
25038 static void
25039 optimize_location_lists (dw_die_ref die)
25041 loc_list_hash_type htab (500);
25042 optimize_location_lists_1 (die, &htab);
25045 /* Output stuff that dwarf requires at the end of every file,
25046 and generate the DWARF-2 debugging info. */
25048 static void
25049 dwarf2out_finish (const char *filename)
25051 comdat_type_node *ctnode;
25052 dw_die_ref main_comp_unit_die;
25054 /* Flush out any latecomers to the limbo party. */
25055 dwarf2out_early_finish ();
25057 /* PCH might result in DW_AT_producer string being restored from the
25058 header compilation, so always fill it with empty string initially
25059 and overwrite only here. */
25060 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25061 producer_string = gen_producer_string ();
25062 producer->dw_attr_val.v.val_str->refcount--;
25063 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25065 gen_scheduled_generic_parms_dies ();
25066 gen_remaining_tmpl_value_param_die_attribute ();
25068 /* Add the name for the main input file now. We delayed this from
25069 dwarf2out_init to avoid complications with PCH.
25070 For LTO produced units use a fixed artificial name to avoid
25071 leaking tempfile names into the dwarf. */
25072 if (!in_lto_p)
25073 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25074 else
25075 add_name_attribute (comp_unit_die (), "<artificial>");
25076 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25077 add_comp_dir_attribute (comp_unit_die ());
25078 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25080 bool p = false;
25081 file_table->traverse<bool *, file_table_relative_p> (&p);
25082 if (p)
25083 add_comp_dir_attribute (comp_unit_die ());
25086 #if ENABLE_ASSERT_CHECKING
25088 dw_die_ref die = comp_unit_die (), c;
25089 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25091 #endif
25092 resolve_addr (comp_unit_die ());
25093 move_marked_base_types ();
25095 /* Walk through the list of incomplete types again, trying once more to
25096 emit full debugging info for them. */
25097 retry_incomplete_types ();
25099 if (flag_eliminate_unused_debug_types)
25100 prune_unused_types ();
25102 /* Generate separate COMDAT sections for type DIEs. */
25103 if (use_debug_types)
25105 break_out_comdat_types (comp_unit_die ());
25107 /* Each new type_unit DIE was added to the limbo die list when created.
25108 Since these have all been added to comdat_type_list, clear the
25109 limbo die list. */
25110 limbo_die_list = NULL;
25112 /* For each new comdat type unit, copy declarations for incomplete
25113 types to make the new unit self-contained (i.e., no direct
25114 references to the main compile unit). */
25115 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25116 copy_decls_for_unworthy_types (ctnode->root_die);
25117 copy_decls_for_unworthy_types (comp_unit_die ());
25119 /* In the process of copying declarations from one unit to another,
25120 we may have left some declarations behind that are no longer
25121 referenced. Prune them. */
25122 prune_unused_types ();
25125 /* Generate separate CUs for each of the include files we've seen.
25126 They will go into limbo_die_list. */
25127 if (flag_eliminate_dwarf2_dups)
25128 break_out_includes (comp_unit_die ());
25130 /* Traverse the DIE's and add add sibling attributes to those DIE's
25131 that have children. */
25132 add_sibling_attributes (comp_unit_die ());
25133 limbo_die_node *node;
25134 for (node = limbo_die_list; node; node = node->next)
25135 add_sibling_attributes (node->die);
25136 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25137 add_sibling_attributes (ctnode->root_die);
25139 /* When splitting DWARF info, we put some attributes in the
25140 skeleton compile_unit DIE that remains in the .o, while
25141 most attributes go in the DWO compile_unit_die. */
25142 if (dwarf_split_debug_info)
25143 main_comp_unit_die = gen_compile_unit_die (NULL);
25144 else
25145 main_comp_unit_die = comp_unit_die ();
25147 /* Output a terminator label for the .text section. */
25148 switch_to_section (text_section);
25149 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25150 if (cold_text_section)
25152 switch_to_section (cold_text_section);
25153 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25156 /* We can only use the low/high_pc attributes if all of the code was
25157 in .text. */
25158 if (!have_multiple_function_sections
25159 || (dwarf_version < 3 && dwarf_strict))
25161 /* Don't add if the CU has no associated code. */
25162 if (text_section_used)
25163 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25164 text_end_label, true);
25166 else
25168 unsigned fde_idx;
25169 dw_fde_ref fde;
25170 bool range_list_added = false;
25172 if (text_section_used)
25173 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25174 text_end_label, &range_list_added, true);
25175 if (cold_text_section_used)
25176 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25177 cold_end_label, &range_list_added, true);
25179 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25181 if (DECL_IGNORED_P (fde->decl))
25182 continue;
25183 if (!fde->in_std_section)
25184 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25185 fde->dw_fde_end, &range_list_added,
25186 true);
25187 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25188 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25189 fde->dw_fde_second_end, &range_list_added,
25190 true);
25193 if (range_list_added)
25195 /* We need to give .debug_loc and .debug_ranges an appropriate
25196 "base address". Use zero so that these addresses become
25197 absolute. Historically, we've emitted the unexpected
25198 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25199 Emit both to give time for other tools to adapt. */
25200 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25201 if (! dwarf_strict && dwarf_version < 4)
25202 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25204 add_ranges (NULL);
25208 if (debug_info_level >= DINFO_LEVEL_TERSE)
25209 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25210 debug_line_section_label);
25212 if (have_macinfo)
25213 add_AT_macptr (comp_unit_die (),
25214 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25215 macinfo_section_label);
25217 if (dwarf_split_debug_info)
25219 /* optimize_location_lists calculates the size of the lists,
25220 so index them first, and assign indices to the entries.
25221 Although optimize_location_lists will remove entries from
25222 the table, it only does so for duplicates, and therefore
25223 only reduces ref_counts to 1. */
25224 index_location_lists (comp_unit_die ());
25226 if (addr_index_table != NULL)
25228 unsigned int index = 0;
25229 addr_index_table
25230 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25231 (&index);
25235 if (have_location_lists)
25236 optimize_location_lists (comp_unit_die ());
25238 save_macinfo_strings ();
25240 if (dwarf_split_debug_info)
25242 unsigned int index = 0;
25244 /* Add attributes common to skeleton compile_units and
25245 type_units. Because these attributes include strings, it
25246 must be done before freezing the string table. Top-level
25247 skeleton die attrs are added when the skeleton type unit is
25248 created, so ensure it is created by this point. */
25249 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25250 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25253 /* Output all of the compilation units. We put the main one last so that
25254 the offsets are available to output_pubnames. */
25255 for (node = limbo_die_list; node; node = node->next)
25256 output_comp_unit (node->die, 0);
25258 hash_table<comdat_type_hasher> comdat_type_table (100);
25259 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25261 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25263 /* Don't output duplicate types. */
25264 if (*slot != HTAB_EMPTY_ENTRY)
25265 continue;
25267 /* Add a pointer to the line table for the main compilation unit
25268 so that the debugger can make sense of DW_AT_decl_file
25269 attributes. */
25270 if (debug_info_level >= DINFO_LEVEL_TERSE)
25271 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25272 (!dwarf_split_debug_info
25273 ? debug_line_section_label
25274 : debug_skeleton_line_section_label));
25276 output_comdat_type_unit (ctnode);
25277 *slot = ctnode;
25280 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25281 both the main_cu and all skeleton TUs. Making this call unconditional
25282 would end up either adding a second copy of the AT_pubnames attribute, or
25283 requiring a special case in add_top_level_skeleton_die_attrs. */
25284 if (!dwarf_split_debug_info)
25285 add_AT_pubnames (comp_unit_die ());
25287 if (dwarf_split_debug_info)
25289 int mark;
25290 unsigned char checksum[16];
25291 struct md5_ctx ctx;
25293 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25294 md5_init_ctx (&ctx);
25295 mark = 0;
25296 die_checksum (comp_unit_die (), &ctx, &mark);
25297 unmark_all_dies (comp_unit_die ());
25298 md5_finish_ctx (&ctx, checksum);
25300 /* Use the first 8 bytes of the checksum as the dwo_id,
25301 and add it to both comp-unit DIEs. */
25302 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25303 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25305 /* Add the base offset of the ranges table to the skeleton
25306 comp-unit DIE. */
25307 if (ranges_table_in_use)
25308 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25309 ranges_section_label);
25311 switch_to_section (debug_addr_section);
25312 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25313 output_addr_table ();
25316 /* Output the main compilation unit if non-empty or if .debug_macinfo
25317 or .debug_macro will be emitted. */
25318 output_comp_unit (comp_unit_die (), have_macinfo);
25320 if (dwarf_split_debug_info && info_section_emitted)
25321 output_skeleton_debug_sections (main_comp_unit_die);
25323 /* Output the abbreviation table. */
25324 if (abbrev_die_table_in_use != 1)
25326 switch_to_section (debug_abbrev_section);
25327 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25328 output_abbrev_section ();
25331 /* Output location list section if necessary. */
25332 if (have_location_lists)
25334 /* Output the location lists info. */
25335 switch_to_section (debug_loc_section);
25336 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25337 output_location_lists (comp_unit_die ());
25340 output_pubtables ();
25342 /* Output the address range information if a CU (.debug_info section)
25343 was emitted. We output an empty table even if we had no functions
25344 to put in it. This because the consumer has no way to tell the
25345 difference between an empty table that we omitted and failure to
25346 generate a table that would have contained data. */
25347 if (info_section_emitted)
25349 unsigned long aranges_length = size_of_aranges ();
25351 switch_to_section (debug_aranges_section);
25352 output_aranges (aranges_length);
25355 /* Output ranges section if necessary. */
25356 if (ranges_table_in_use)
25358 switch_to_section (debug_ranges_section);
25359 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25360 output_ranges ();
25363 /* Have to end the macro section. */
25364 if (have_macinfo)
25366 switch_to_section (debug_macinfo_section);
25367 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25368 output_macinfo ();
25369 dw2_asm_output_data (1, 0, "End compilation unit");
25372 /* Output the source line correspondence table. We must do this
25373 even if there is no line information. Otherwise, on an empty
25374 translation unit, we will generate a present, but empty,
25375 .debug_info section. IRIX 6.5 `nm' will then complain when
25376 examining the file. This is done late so that any filenames
25377 used by the debug_info section are marked as 'used'. */
25378 switch_to_section (debug_line_section);
25379 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25380 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25381 output_line_info (false);
25383 if (dwarf_split_debug_info && info_section_emitted)
25385 switch_to_section (debug_skeleton_line_section);
25386 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25387 output_line_info (true);
25390 /* If we emitted any indirect strings, output the string table too. */
25391 if (debug_str_hash || skeleton_debug_str_hash)
25392 output_indirect_strings ();
25395 /* Perform any cleanups needed after the early debug generation pass
25396 has run. */
25398 static void
25399 dwarf2out_early_finish (void)
25401 limbo_die_node *node, *next_node;
25403 /* Add DW_AT_linkage_name for all deferred DIEs. */
25404 for (node = deferred_asm_name; node; node = node->next)
25406 tree decl = node->created_for;
25407 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25408 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25409 ended up in in deferred_asm_name before we knew it was
25410 constant and never written to disk. */
25411 && DECL_ASSEMBLER_NAME (decl))
25413 add_linkage_attr (node->die, decl);
25414 move_linkage_attr (node->die);
25417 deferred_asm_name = NULL;
25419 /* Traverse the limbo die list, and add parent/child links. The only
25420 dies without parents that should be here are concrete instances of
25421 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25422 For concrete instances, we can get the parent die from the abstract
25423 instance.
25425 The point here is to flush out the limbo list so that it is empty
25426 and we don't need to stream it for LTO. */
25427 for (node = limbo_die_list; node; node = next_node)
25429 dw_die_ref die = node->die;
25430 next_node = node->next;
25432 if (die->die_parent == NULL)
25434 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25436 if (origin && origin->die_parent)
25437 add_child_die (origin->die_parent, die);
25438 else if (is_cu_die (die))
25440 else if (seen_error ())
25441 /* It's OK to be confused by errors in the input. */
25442 add_child_die (comp_unit_die (), die);
25443 else
25445 /* In certain situations, the lexical block containing a
25446 nested function can be optimized away, which results
25447 in the nested function die being orphaned. Likewise
25448 with the return type of that nested function. Force
25449 this to be a child of the containing function.
25451 It may happen that even the containing function got fully
25452 inlined and optimized out. In that case we are lost and
25453 assign the empty child. This should not be big issue as
25454 the function is likely unreachable too. */
25455 gcc_assert (node->created_for);
25457 if (DECL_P (node->created_for))
25458 origin = get_context_die (DECL_CONTEXT (node->created_for));
25459 else if (TYPE_P (node->created_for))
25460 origin = scope_die_for (node->created_for, comp_unit_die ());
25461 else
25462 origin = comp_unit_die ();
25464 add_child_die (origin, die);
25469 limbo_die_list = NULL;
25472 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25473 within the same process. For use by toplev::finalize. */
25475 void
25476 dwarf2out_c_finalize (void)
25478 last_var_location_insn = NULL;
25479 cached_next_real_insn = NULL;
25480 used_rtx_array = NULL;
25481 incomplete_types = NULL;
25482 decl_scope_table = NULL;
25483 debug_info_section = NULL;
25484 debug_skeleton_info_section = NULL;
25485 debug_abbrev_section = NULL;
25486 debug_skeleton_abbrev_section = NULL;
25487 debug_aranges_section = NULL;
25488 debug_addr_section = NULL;
25489 debug_macinfo_section = NULL;
25490 debug_line_section = NULL;
25491 debug_skeleton_line_section = NULL;
25492 debug_loc_section = NULL;
25493 debug_pubnames_section = NULL;
25494 debug_pubtypes_section = NULL;
25495 debug_str_section = NULL;
25496 debug_str_dwo_section = NULL;
25497 debug_str_offsets_section = NULL;
25498 debug_ranges_section = NULL;
25499 debug_frame_section = NULL;
25500 fde_vec = NULL;
25501 debug_str_hash = NULL;
25502 skeleton_debug_str_hash = NULL;
25503 dw2_string_counter = 0;
25504 have_multiple_function_sections = false;
25505 text_section_used = false;
25506 cold_text_section_used = false;
25507 cold_text_section = NULL;
25508 current_unit_personality = NULL;
25510 next_die_offset = 0;
25511 single_comp_unit_die = NULL;
25512 comdat_type_list = NULL;
25513 limbo_die_list = NULL;
25514 file_table = NULL;
25515 decl_die_table = NULL;
25516 common_block_die_table = NULL;
25517 decl_loc_table = NULL;
25518 call_arg_locations = NULL;
25519 call_arg_loc_last = NULL;
25520 call_site_count = -1;
25521 tail_call_site_count = -1;
25522 cached_dw_loc_list_table = NULL;
25523 abbrev_die_table = NULL;
25524 abbrev_die_table_allocated = 0;
25525 abbrev_die_table_in_use = 0;
25526 line_info_label_num = 0;
25527 cur_line_info_table = NULL;
25528 text_section_line_info = NULL;
25529 cold_text_section_line_info = NULL;
25530 separate_line_info = NULL;
25531 info_section_emitted = false;
25532 pubname_table = NULL;
25533 pubtype_table = NULL;
25534 macinfo_table = NULL;
25535 ranges_table = NULL;
25536 ranges_table_allocated = 0;
25537 ranges_table_in_use = 0;
25538 ranges_by_label = 0;
25539 ranges_by_label_allocated = 0;
25540 ranges_by_label_in_use = 0;
25541 have_location_lists = false;
25542 loclabel_num = 0;
25543 poc_label_num = 0;
25544 last_emitted_file = NULL;
25545 label_num = 0;
25546 tmpl_value_parm_die_table = NULL;
25547 generic_type_instances = NULL;
25548 frame_pointer_fb_offset = 0;
25549 frame_pointer_fb_offset_valid = false;
25550 base_types.release ();
25551 XDELETEVEC (producer_string);
25552 producer_string = NULL;
25555 #include "gt-dwarf2out.h"