* expmed.c (flip_storage_order): Deal with complex modes specially.
[official-gcc.git] / gcc / dwarf2out.c
blob5d615ab7e36abcb98fa5efb1b24e9d4448b93be1
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 bool reverse;
5253 tree innerdecl
5254 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5255 &reverse);
5256 if (!DECL_P (innerdecl)
5257 || DECL_IGNORED_P (innerdecl)
5258 || TREE_STATIC (innerdecl)
5259 || bitsize <= 0
5260 || bitpos + bitsize > 256
5261 || bitsize != maxsize)
5262 return NULL;
5263 decl = innerdecl;
5267 decl_id = DECL_UID (decl);
5268 var_loc_list **slot
5269 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5270 if (*slot == NULL)
5272 temp = ggc_cleared_alloc<var_loc_list> ();
5273 temp->decl_id = decl_id;
5274 *slot = temp;
5276 else
5277 temp = *slot;
5279 /* For PARM_DECLs try to keep around the original incoming value,
5280 even if that means we'll emit a zero-range .debug_loc entry. */
5281 if (temp->last
5282 && temp->first == temp->last
5283 && TREE_CODE (decl) == PARM_DECL
5284 && NOTE_P (temp->first->loc)
5285 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5286 && DECL_INCOMING_RTL (decl)
5287 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5288 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5289 == GET_CODE (DECL_INCOMING_RTL (decl))
5290 && prev_real_insn (temp->first->loc) == NULL_RTX
5291 && (bitsize != -1
5292 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5293 NOTE_VAR_LOCATION_LOC (loc_note))
5294 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5295 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5297 loc = ggc_cleared_alloc<var_loc_node> ();
5298 temp->first->next = loc;
5299 temp->last = loc;
5300 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5302 else if (temp->last)
5304 struct var_loc_node *last = temp->last, *unused = NULL;
5305 rtx *piece_loc = NULL, last_loc_note;
5306 HOST_WIDE_INT piece_bitpos = 0;
5307 if (last->next)
5309 last = last->next;
5310 gcc_assert (last->next == NULL);
5312 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5314 piece_loc = &last->loc;
5317 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5318 if (piece_bitpos + cur_bitsize > bitpos)
5319 break;
5320 piece_bitpos += cur_bitsize;
5321 piece_loc = &XEXP (*piece_loc, 1);
5323 while (*piece_loc);
5325 /* TEMP->LAST here is either pointer to the last but one or
5326 last element in the chained list, LAST is pointer to the
5327 last element. */
5328 if (label && strcmp (last->label, label) == 0)
5330 /* For SRA optimized variables if there weren't any real
5331 insns since last note, just modify the last node. */
5332 if (piece_loc != NULL)
5334 adjust_piece_list (piece_loc, NULL, NULL,
5335 bitpos, piece_bitpos, bitsize, loc_note);
5336 return NULL;
5338 /* If the last note doesn't cover any instructions, remove it. */
5339 if (temp->last != last)
5341 temp->last->next = NULL;
5342 unused = last;
5343 last = temp->last;
5344 gcc_assert (strcmp (last->label, label) != 0);
5346 else
5348 gcc_assert (temp->first == temp->last
5349 || (temp->first->next == temp->last
5350 && TREE_CODE (decl) == PARM_DECL));
5351 memset (temp->last, '\0', sizeof (*temp->last));
5352 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5353 return temp->last;
5356 if (bitsize == -1 && NOTE_P (last->loc))
5357 last_loc_note = last->loc;
5358 else if (piece_loc != NULL
5359 && *piece_loc != NULL_RTX
5360 && piece_bitpos == bitpos
5361 && decl_piece_bitsize (*piece_loc) == bitsize)
5362 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5363 else
5364 last_loc_note = NULL_RTX;
5365 /* If the current location is the same as the end of the list,
5366 and either both or neither of the locations is uninitialized,
5367 we have nothing to do. */
5368 if (last_loc_note == NULL_RTX
5369 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5370 NOTE_VAR_LOCATION_LOC (loc_note)))
5371 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5372 != NOTE_VAR_LOCATION_STATUS (loc_note))
5373 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5374 == VAR_INIT_STATUS_UNINITIALIZED)
5375 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5376 == VAR_INIT_STATUS_UNINITIALIZED))))
5378 /* Add LOC to the end of list and update LAST. If the last
5379 element of the list has been removed above, reuse its
5380 memory for the new node, otherwise allocate a new one. */
5381 if (unused)
5383 loc = unused;
5384 memset (loc, '\0', sizeof (*loc));
5386 else
5387 loc = ggc_cleared_alloc<var_loc_node> ();
5388 if (bitsize == -1 || piece_loc == NULL)
5389 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5390 else
5391 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5392 bitpos, piece_bitpos, bitsize, loc_note);
5393 last->next = loc;
5394 /* Ensure TEMP->LAST will point either to the new last but one
5395 element of the chain, or to the last element in it. */
5396 if (last != temp->last)
5397 temp->last = last;
5399 else if (unused)
5400 ggc_free (unused);
5402 else
5404 loc = ggc_cleared_alloc<var_loc_node> ();
5405 temp->first = loc;
5406 temp->last = loc;
5407 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5409 return loc;
5412 /* Keep track of the number of spaces used to indent the
5413 output of the debugging routines that print the structure of
5414 the DIE internal representation. */
5415 static int print_indent;
5417 /* Indent the line the number of spaces given by print_indent. */
5419 static inline void
5420 print_spaces (FILE *outfile)
5422 fprintf (outfile, "%*s", print_indent, "");
5425 /* Print a type signature in hex. */
5427 static inline void
5428 print_signature (FILE *outfile, char *sig)
5430 int i;
5432 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5433 fprintf (outfile, "%02x", sig[i] & 0xff);
5436 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5438 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5439 RECURSE, output location descriptor operations. */
5441 static void
5442 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5444 switch (val->val_class)
5446 case dw_val_class_addr:
5447 fprintf (outfile, "address");
5448 break;
5449 case dw_val_class_offset:
5450 fprintf (outfile, "offset");
5451 break;
5452 case dw_val_class_loc:
5453 fprintf (outfile, "location descriptor");
5454 if (val->v.val_loc == NULL)
5455 fprintf (outfile, " -> <null>\n");
5456 else if (recurse)
5458 fprintf (outfile, ":\n");
5459 print_indent += 4;
5460 print_loc_descr (val->v.val_loc, outfile);
5461 print_indent -= 4;
5463 else
5464 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5465 break;
5466 case dw_val_class_loc_list:
5467 fprintf (outfile, "location list -> label:%s",
5468 val->v.val_loc_list->ll_symbol);
5469 break;
5470 case dw_val_class_range_list:
5471 fprintf (outfile, "range list");
5472 break;
5473 case dw_val_class_const:
5474 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5475 break;
5476 case dw_val_class_unsigned_const:
5477 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5478 break;
5479 case dw_val_class_const_double:
5480 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5481 HOST_WIDE_INT_PRINT_UNSIGNED")",
5482 val->v.val_double.high,
5483 val->v.val_double.low);
5484 break;
5485 case dw_val_class_wide_int:
5487 int i = val->v.val_wide->get_len ();
5488 fprintf (outfile, "constant (");
5489 gcc_assert (i > 0);
5490 if (val->v.val_wide->elt (i - 1) == 0)
5491 fprintf (outfile, "0x");
5492 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5493 val->v.val_wide->elt (--i));
5494 while (--i >= 0)
5495 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5496 val->v.val_wide->elt (i));
5497 fprintf (outfile, ")");
5498 break;
5500 case dw_val_class_vec:
5501 fprintf (outfile, "floating-point or vector constant");
5502 break;
5503 case dw_val_class_flag:
5504 fprintf (outfile, "%u", val->v.val_flag);
5505 break;
5506 case dw_val_class_die_ref:
5507 if (val->v.val_die_ref.die != NULL)
5509 dw_die_ref die = val->v.val_die_ref.die;
5511 if (die->comdat_type_p)
5513 fprintf (outfile, "die -> signature: ");
5514 print_signature (outfile,
5515 die->die_id.die_type_node->signature);
5517 else if (die->die_id.die_symbol)
5518 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5519 else
5520 fprintf (outfile, "die -> %ld", die->die_offset);
5521 fprintf (outfile, " (%p)", (void *) die);
5523 else
5524 fprintf (outfile, "die -> <null>");
5525 break;
5526 case dw_val_class_vms_delta:
5527 fprintf (outfile, "delta: @slotcount(%s-%s)",
5528 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5529 break;
5530 case dw_val_class_lbl_id:
5531 case dw_val_class_lineptr:
5532 case dw_val_class_macptr:
5533 case dw_val_class_high_pc:
5534 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5535 break;
5536 case dw_val_class_str:
5537 if (val->v.val_str->str != NULL)
5538 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5539 else
5540 fprintf (outfile, "<null>");
5541 break;
5542 case dw_val_class_file:
5543 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5544 val->v.val_file->emitted_number);
5545 break;
5546 case dw_val_class_data8:
5548 int i;
5550 for (i = 0; i < 8; i++)
5551 fprintf (outfile, "%02x", val->v.val_data8[i]);
5552 break;
5554 default:
5555 break;
5559 /* Likewise, for a DIE attribute. */
5561 static void
5562 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5564 print_dw_val (&a->dw_attr_val, recurse, outfile);
5568 /* Print the list of operands in the LOC location description to OUTFILE. This
5569 routine is a debugging aid only. */
5571 static void
5572 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5574 dw_loc_descr_ref l = loc;
5576 if (loc == NULL)
5578 print_spaces (outfile);
5579 fprintf (outfile, "<null>\n");
5580 return;
5583 for (l = loc; l != NULL; l = l->dw_loc_next)
5585 print_spaces (outfile);
5586 fprintf (outfile, "(%p) %s",
5587 (void *) l,
5588 dwarf_stack_op_name (l->dw_loc_opc));
5589 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5591 fprintf (outfile, " ");
5592 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5594 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5596 fprintf (outfile, ", ");
5597 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5599 fprintf (outfile, "\n");
5603 /* Print the information associated with a given DIE, and its children.
5604 This routine is a debugging aid only. */
5606 static void
5607 print_die (dw_die_ref die, FILE *outfile)
5609 dw_attr_ref a;
5610 dw_die_ref c;
5611 unsigned ix;
5613 print_spaces (outfile);
5614 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5615 die->die_offset, dwarf_tag_name (die->die_tag),
5616 (void*) die);
5617 print_spaces (outfile);
5618 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5619 fprintf (outfile, " offset: %ld", die->die_offset);
5620 fprintf (outfile, " mark: %d\n", die->die_mark);
5622 if (die->comdat_type_p)
5624 print_spaces (outfile);
5625 fprintf (outfile, " signature: ");
5626 print_signature (outfile, die->die_id.die_type_node->signature);
5627 fprintf (outfile, "\n");
5630 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5632 print_spaces (outfile);
5633 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5635 print_attribute (a, true, outfile);
5636 fprintf (outfile, "\n");
5639 if (die->die_child != NULL)
5641 print_indent += 4;
5642 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5643 print_indent -= 4;
5645 if (print_indent == 0)
5646 fprintf (outfile, "\n");
5649 /* Print the list of operations in the LOC location description. */
5651 DEBUG_FUNCTION void
5652 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5654 print_loc_descr (loc, stderr);
5657 /* Print the information collected for a given DIE. */
5659 DEBUG_FUNCTION void
5660 debug_dwarf_die (dw_die_ref die)
5662 print_die (die, stderr);
5665 DEBUG_FUNCTION void
5666 debug (die_struct &ref)
5668 print_die (&ref, stderr);
5671 DEBUG_FUNCTION void
5672 debug (die_struct *ptr)
5674 if (ptr)
5675 debug (*ptr);
5676 else
5677 fprintf (stderr, "<nil>\n");
5681 /* Print all DWARF information collected for the compilation unit.
5682 This routine is a debugging aid only. */
5684 DEBUG_FUNCTION void
5685 debug_dwarf (void)
5687 print_indent = 0;
5688 print_die (comp_unit_die (), stderr);
5691 #ifdef ENABLE_CHECKING
5692 /* Sanity checks on DIEs. */
5694 static void
5695 check_die (dw_die_ref die)
5697 unsigned ix;
5698 dw_attr_ref a;
5699 bool inline_found = false;
5700 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5701 int n_decl_line = 0, n_decl_file = 0;
5702 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5704 switch (a->dw_attr)
5706 case DW_AT_inline:
5707 if (a->dw_attr_val.v.val_unsigned)
5708 inline_found = true;
5709 break;
5710 case DW_AT_location:
5711 ++n_location;
5712 break;
5713 case DW_AT_low_pc:
5714 ++n_low_pc;
5715 break;
5716 case DW_AT_high_pc:
5717 ++n_high_pc;
5718 break;
5719 case DW_AT_artificial:
5720 ++n_artificial;
5721 break;
5722 case DW_AT_decl_line:
5723 ++n_decl_line;
5724 break;
5725 case DW_AT_decl_file:
5726 ++n_decl_file;
5727 break;
5728 default:
5729 break;
5732 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5733 || n_decl_line > 1 || n_decl_file > 1)
5735 fprintf (stderr, "Duplicate attributes in DIE:\n");
5736 debug_dwarf_die (die);
5737 gcc_unreachable ();
5739 if (inline_found)
5741 /* A debugging information entry that is a member of an abstract
5742 instance tree [that has DW_AT_inline] should not contain any
5743 attributes which describe aspects of the subroutine which vary
5744 between distinct inlined expansions or distinct out-of-line
5745 expansions. */
5746 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5747 gcc_assert (a->dw_attr != DW_AT_low_pc
5748 && a->dw_attr != DW_AT_high_pc
5749 && a->dw_attr != DW_AT_location
5750 && a->dw_attr != DW_AT_frame_base
5751 && a->dw_attr != DW_AT_GNU_all_call_sites);
5754 #endif
5756 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5757 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5758 DIE that marks the start of the DIEs for this include file. */
5760 static dw_die_ref
5761 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5763 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5764 dw_die_ref new_unit = gen_compile_unit_die (filename);
5766 new_unit->die_sib = old_unit;
5767 return new_unit;
5770 /* Close an include-file CU and reopen the enclosing one. */
5772 static dw_die_ref
5773 pop_compile_unit (dw_die_ref old_unit)
5775 dw_die_ref new_unit = old_unit->die_sib;
5777 old_unit->die_sib = NULL;
5778 return new_unit;
5781 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5782 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5783 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5785 /* Calculate the checksum of a location expression. */
5787 static inline void
5788 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5790 int tem;
5791 inchash::hash hstate;
5792 hashval_t hash;
5794 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5795 CHECKSUM (tem);
5796 hash_loc_operands (loc, hstate);
5797 hash = hstate.end();
5798 CHECKSUM (hash);
5801 /* Calculate the checksum of an attribute. */
5803 static void
5804 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5806 dw_loc_descr_ref loc;
5807 rtx r;
5809 CHECKSUM (at->dw_attr);
5811 /* We don't care that this was compiled with a different compiler
5812 snapshot; if the output is the same, that's what matters. */
5813 if (at->dw_attr == DW_AT_producer)
5814 return;
5816 switch (AT_class (at))
5818 case dw_val_class_const:
5819 CHECKSUM (at->dw_attr_val.v.val_int);
5820 break;
5821 case dw_val_class_unsigned_const:
5822 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5823 break;
5824 case dw_val_class_const_double:
5825 CHECKSUM (at->dw_attr_val.v.val_double);
5826 break;
5827 case dw_val_class_wide_int:
5828 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5829 get_full_len (*at->dw_attr_val.v.val_wide)
5830 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5831 break;
5832 case dw_val_class_vec:
5833 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5834 (at->dw_attr_val.v.val_vec.length
5835 * at->dw_attr_val.v.val_vec.elt_size));
5836 break;
5837 case dw_val_class_flag:
5838 CHECKSUM (at->dw_attr_val.v.val_flag);
5839 break;
5840 case dw_val_class_str:
5841 CHECKSUM_STRING (AT_string (at));
5842 break;
5844 case dw_val_class_addr:
5845 r = AT_addr (at);
5846 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5847 CHECKSUM_STRING (XSTR (r, 0));
5848 break;
5850 case dw_val_class_offset:
5851 CHECKSUM (at->dw_attr_val.v.val_offset);
5852 break;
5854 case dw_val_class_loc:
5855 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5856 loc_checksum (loc, ctx);
5857 break;
5859 case dw_val_class_die_ref:
5860 die_checksum (AT_ref (at), ctx, mark);
5861 break;
5863 case dw_val_class_fde_ref:
5864 case dw_val_class_vms_delta:
5865 case dw_val_class_lbl_id:
5866 case dw_val_class_lineptr:
5867 case dw_val_class_macptr:
5868 case dw_val_class_high_pc:
5869 break;
5871 case dw_val_class_file:
5872 CHECKSUM_STRING (AT_file (at)->filename);
5873 break;
5875 case dw_val_class_data8:
5876 CHECKSUM (at->dw_attr_val.v.val_data8);
5877 break;
5879 default:
5880 break;
5884 /* Calculate the checksum of a DIE. */
5886 static void
5887 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5889 dw_die_ref c;
5890 dw_attr_ref a;
5891 unsigned ix;
5893 /* To avoid infinite recursion. */
5894 if (die->die_mark)
5896 CHECKSUM (die->die_mark);
5897 return;
5899 die->die_mark = ++(*mark);
5901 CHECKSUM (die->die_tag);
5903 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5904 attr_checksum (a, ctx, mark);
5906 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5909 #undef CHECKSUM
5910 #undef CHECKSUM_BLOCK
5911 #undef CHECKSUM_STRING
5913 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5914 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5915 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5916 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5917 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5918 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5919 #define CHECKSUM_ATTR(FOO) \
5920 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5922 /* Calculate the checksum of a number in signed LEB128 format. */
5924 static void
5925 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5927 unsigned char byte;
5928 bool more;
5930 while (1)
5932 byte = (value & 0x7f);
5933 value >>= 7;
5934 more = !((value == 0 && (byte & 0x40) == 0)
5935 || (value == -1 && (byte & 0x40) != 0));
5936 if (more)
5937 byte |= 0x80;
5938 CHECKSUM (byte);
5939 if (!more)
5940 break;
5944 /* Calculate the checksum of a number in unsigned LEB128 format. */
5946 static void
5947 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5949 while (1)
5951 unsigned char byte = (value & 0x7f);
5952 value >>= 7;
5953 if (value != 0)
5954 /* More bytes to follow. */
5955 byte |= 0x80;
5956 CHECKSUM (byte);
5957 if (value == 0)
5958 break;
5962 /* Checksum the context of the DIE. This adds the names of any
5963 surrounding namespaces or structures to the checksum. */
5965 static void
5966 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5968 const char *name;
5969 dw_die_ref spec;
5970 int tag = die->die_tag;
5972 if (tag != DW_TAG_namespace
5973 && tag != DW_TAG_structure_type
5974 && tag != DW_TAG_class_type)
5975 return;
5977 name = get_AT_string (die, DW_AT_name);
5979 spec = get_AT_ref (die, DW_AT_specification);
5980 if (spec != NULL)
5981 die = spec;
5983 if (die->die_parent != NULL)
5984 checksum_die_context (die->die_parent, ctx);
5986 CHECKSUM_ULEB128 ('C');
5987 CHECKSUM_ULEB128 (tag);
5988 if (name != NULL)
5989 CHECKSUM_STRING (name);
5992 /* Calculate the checksum of a location expression. */
5994 static inline void
5995 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5997 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5998 were emitted as a DW_FORM_sdata instead of a location expression. */
5999 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6001 CHECKSUM_ULEB128 (DW_FORM_sdata);
6002 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6003 return;
6006 /* Otherwise, just checksum the raw location expression. */
6007 while (loc != NULL)
6009 inchash::hash hstate;
6010 hashval_t hash;
6012 CHECKSUM_ULEB128 (loc->dtprel);
6013 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6014 hash_loc_operands (loc, hstate);
6015 hash = hstate.end ();
6016 CHECKSUM (hash);
6017 loc = loc->dw_loc_next;
6021 /* Calculate the checksum of an attribute. */
6023 static void
6024 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6025 struct md5_ctx *ctx, int *mark)
6027 dw_loc_descr_ref loc;
6028 rtx r;
6030 if (AT_class (at) == dw_val_class_die_ref)
6032 dw_die_ref target_die = AT_ref (at);
6034 /* For pointer and reference types, we checksum only the (qualified)
6035 name of the target type (if there is a name). For friend entries,
6036 we checksum only the (qualified) name of the target type or function.
6037 This allows the checksum to remain the same whether the target type
6038 is complete or not. */
6039 if ((at->dw_attr == DW_AT_type
6040 && (tag == DW_TAG_pointer_type
6041 || tag == DW_TAG_reference_type
6042 || tag == DW_TAG_rvalue_reference_type
6043 || tag == DW_TAG_ptr_to_member_type))
6044 || (at->dw_attr == DW_AT_friend
6045 && tag == DW_TAG_friend))
6047 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6049 if (name_attr != NULL)
6051 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6053 if (decl == NULL)
6054 decl = target_die;
6055 CHECKSUM_ULEB128 ('N');
6056 CHECKSUM_ULEB128 (at->dw_attr);
6057 if (decl->die_parent != NULL)
6058 checksum_die_context (decl->die_parent, ctx);
6059 CHECKSUM_ULEB128 ('E');
6060 CHECKSUM_STRING (AT_string (name_attr));
6061 return;
6065 /* For all other references to another DIE, we check to see if the
6066 target DIE has already been visited. If it has, we emit a
6067 backward reference; if not, we descend recursively. */
6068 if (target_die->die_mark > 0)
6070 CHECKSUM_ULEB128 ('R');
6071 CHECKSUM_ULEB128 (at->dw_attr);
6072 CHECKSUM_ULEB128 (target_die->die_mark);
6074 else
6076 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6078 if (decl == NULL)
6079 decl = target_die;
6080 target_die->die_mark = ++(*mark);
6081 CHECKSUM_ULEB128 ('T');
6082 CHECKSUM_ULEB128 (at->dw_attr);
6083 if (decl->die_parent != NULL)
6084 checksum_die_context (decl->die_parent, ctx);
6085 die_checksum_ordered (target_die, ctx, mark);
6087 return;
6090 CHECKSUM_ULEB128 ('A');
6091 CHECKSUM_ULEB128 (at->dw_attr);
6093 switch (AT_class (at))
6095 case dw_val_class_const:
6096 CHECKSUM_ULEB128 (DW_FORM_sdata);
6097 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6098 break;
6100 case dw_val_class_unsigned_const:
6101 CHECKSUM_ULEB128 (DW_FORM_sdata);
6102 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6103 break;
6105 case dw_val_class_const_double:
6106 CHECKSUM_ULEB128 (DW_FORM_block);
6107 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6108 CHECKSUM (at->dw_attr_val.v.val_double);
6109 break;
6111 case dw_val_class_wide_int:
6112 CHECKSUM_ULEB128 (DW_FORM_block);
6113 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6114 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6115 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6116 get_full_len (*at->dw_attr_val.v.val_wide)
6117 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6118 break;
6120 case dw_val_class_vec:
6121 CHECKSUM_ULEB128 (DW_FORM_block);
6122 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6123 * at->dw_attr_val.v.val_vec.elt_size);
6124 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6125 (at->dw_attr_val.v.val_vec.length
6126 * at->dw_attr_val.v.val_vec.elt_size));
6127 break;
6129 case dw_val_class_flag:
6130 CHECKSUM_ULEB128 (DW_FORM_flag);
6131 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6132 break;
6134 case dw_val_class_str:
6135 CHECKSUM_ULEB128 (DW_FORM_string);
6136 CHECKSUM_STRING (AT_string (at));
6137 break;
6139 case dw_val_class_addr:
6140 r = AT_addr (at);
6141 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6142 CHECKSUM_ULEB128 (DW_FORM_string);
6143 CHECKSUM_STRING (XSTR (r, 0));
6144 break;
6146 case dw_val_class_offset:
6147 CHECKSUM_ULEB128 (DW_FORM_sdata);
6148 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6149 break;
6151 case dw_val_class_loc:
6152 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6153 loc_checksum_ordered (loc, ctx);
6154 break;
6156 case dw_val_class_fde_ref:
6157 case dw_val_class_lbl_id:
6158 case dw_val_class_lineptr:
6159 case dw_val_class_macptr:
6160 case dw_val_class_high_pc:
6161 break;
6163 case dw_val_class_file:
6164 CHECKSUM_ULEB128 (DW_FORM_string);
6165 CHECKSUM_STRING (AT_file (at)->filename);
6166 break;
6168 case dw_val_class_data8:
6169 CHECKSUM (at->dw_attr_val.v.val_data8);
6170 break;
6172 default:
6173 break;
6177 struct checksum_attributes
6179 dw_attr_ref at_name;
6180 dw_attr_ref at_type;
6181 dw_attr_ref at_friend;
6182 dw_attr_ref at_accessibility;
6183 dw_attr_ref at_address_class;
6184 dw_attr_ref at_allocated;
6185 dw_attr_ref at_artificial;
6186 dw_attr_ref at_associated;
6187 dw_attr_ref at_binary_scale;
6188 dw_attr_ref at_bit_offset;
6189 dw_attr_ref at_bit_size;
6190 dw_attr_ref at_bit_stride;
6191 dw_attr_ref at_byte_size;
6192 dw_attr_ref at_byte_stride;
6193 dw_attr_ref at_const_value;
6194 dw_attr_ref at_containing_type;
6195 dw_attr_ref at_count;
6196 dw_attr_ref at_data_location;
6197 dw_attr_ref at_data_member_location;
6198 dw_attr_ref at_decimal_scale;
6199 dw_attr_ref at_decimal_sign;
6200 dw_attr_ref at_default_value;
6201 dw_attr_ref at_digit_count;
6202 dw_attr_ref at_discr;
6203 dw_attr_ref at_discr_list;
6204 dw_attr_ref at_discr_value;
6205 dw_attr_ref at_encoding;
6206 dw_attr_ref at_endianity;
6207 dw_attr_ref at_explicit;
6208 dw_attr_ref at_is_optional;
6209 dw_attr_ref at_location;
6210 dw_attr_ref at_lower_bound;
6211 dw_attr_ref at_mutable;
6212 dw_attr_ref at_ordering;
6213 dw_attr_ref at_picture_string;
6214 dw_attr_ref at_prototyped;
6215 dw_attr_ref at_small;
6216 dw_attr_ref at_segment;
6217 dw_attr_ref at_string_length;
6218 dw_attr_ref at_threads_scaled;
6219 dw_attr_ref at_upper_bound;
6220 dw_attr_ref at_use_location;
6221 dw_attr_ref at_use_UTF8;
6222 dw_attr_ref at_variable_parameter;
6223 dw_attr_ref at_virtuality;
6224 dw_attr_ref at_visibility;
6225 dw_attr_ref at_vtable_elem_location;
6228 /* Collect the attributes that we will want to use for the checksum. */
6230 static void
6231 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6233 dw_attr_ref a;
6234 unsigned ix;
6236 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6238 switch (a->dw_attr)
6240 case DW_AT_name:
6241 attrs->at_name = a;
6242 break;
6243 case DW_AT_type:
6244 attrs->at_type = a;
6245 break;
6246 case DW_AT_friend:
6247 attrs->at_friend = a;
6248 break;
6249 case DW_AT_accessibility:
6250 attrs->at_accessibility = a;
6251 break;
6252 case DW_AT_address_class:
6253 attrs->at_address_class = a;
6254 break;
6255 case DW_AT_allocated:
6256 attrs->at_allocated = a;
6257 break;
6258 case DW_AT_artificial:
6259 attrs->at_artificial = a;
6260 break;
6261 case DW_AT_associated:
6262 attrs->at_associated = a;
6263 break;
6264 case DW_AT_binary_scale:
6265 attrs->at_binary_scale = a;
6266 break;
6267 case DW_AT_bit_offset:
6268 attrs->at_bit_offset = a;
6269 break;
6270 case DW_AT_bit_size:
6271 attrs->at_bit_size = a;
6272 break;
6273 case DW_AT_bit_stride:
6274 attrs->at_bit_stride = a;
6275 break;
6276 case DW_AT_byte_size:
6277 attrs->at_byte_size = a;
6278 break;
6279 case DW_AT_byte_stride:
6280 attrs->at_byte_stride = a;
6281 break;
6282 case DW_AT_const_value:
6283 attrs->at_const_value = a;
6284 break;
6285 case DW_AT_containing_type:
6286 attrs->at_containing_type = a;
6287 break;
6288 case DW_AT_count:
6289 attrs->at_count = a;
6290 break;
6291 case DW_AT_data_location:
6292 attrs->at_data_location = a;
6293 break;
6294 case DW_AT_data_member_location:
6295 attrs->at_data_member_location = a;
6296 break;
6297 case DW_AT_decimal_scale:
6298 attrs->at_decimal_scale = a;
6299 break;
6300 case DW_AT_decimal_sign:
6301 attrs->at_decimal_sign = a;
6302 break;
6303 case DW_AT_default_value:
6304 attrs->at_default_value = a;
6305 break;
6306 case DW_AT_digit_count:
6307 attrs->at_digit_count = a;
6308 break;
6309 case DW_AT_discr:
6310 attrs->at_discr = a;
6311 break;
6312 case DW_AT_discr_list:
6313 attrs->at_discr_list = a;
6314 break;
6315 case DW_AT_discr_value:
6316 attrs->at_discr_value = a;
6317 break;
6318 case DW_AT_encoding:
6319 attrs->at_encoding = a;
6320 break;
6321 case DW_AT_endianity:
6322 attrs->at_endianity = a;
6323 break;
6324 case DW_AT_explicit:
6325 attrs->at_explicit = a;
6326 break;
6327 case DW_AT_is_optional:
6328 attrs->at_is_optional = a;
6329 break;
6330 case DW_AT_location:
6331 attrs->at_location = a;
6332 break;
6333 case DW_AT_lower_bound:
6334 attrs->at_lower_bound = a;
6335 break;
6336 case DW_AT_mutable:
6337 attrs->at_mutable = a;
6338 break;
6339 case DW_AT_ordering:
6340 attrs->at_ordering = a;
6341 break;
6342 case DW_AT_picture_string:
6343 attrs->at_picture_string = a;
6344 break;
6345 case DW_AT_prototyped:
6346 attrs->at_prototyped = a;
6347 break;
6348 case DW_AT_small:
6349 attrs->at_small = a;
6350 break;
6351 case DW_AT_segment:
6352 attrs->at_segment = a;
6353 break;
6354 case DW_AT_string_length:
6355 attrs->at_string_length = a;
6356 break;
6357 case DW_AT_threads_scaled:
6358 attrs->at_threads_scaled = a;
6359 break;
6360 case DW_AT_upper_bound:
6361 attrs->at_upper_bound = a;
6362 break;
6363 case DW_AT_use_location:
6364 attrs->at_use_location = a;
6365 break;
6366 case DW_AT_use_UTF8:
6367 attrs->at_use_UTF8 = a;
6368 break;
6369 case DW_AT_variable_parameter:
6370 attrs->at_variable_parameter = a;
6371 break;
6372 case DW_AT_virtuality:
6373 attrs->at_virtuality = a;
6374 break;
6375 case DW_AT_visibility:
6376 attrs->at_visibility = a;
6377 break;
6378 case DW_AT_vtable_elem_location:
6379 attrs->at_vtable_elem_location = a;
6380 break;
6381 default:
6382 break;
6387 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6389 static void
6390 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6392 dw_die_ref c;
6393 dw_die_ref decl;
6394 struct checksum_attributes attrs;
6396 CHECKSUM_ULEB128 ('D');
6397 CHECKSUM_ULEB128 (die->die_tag);
6399 memset (&attrs, 0, sizeof (attrs));
6401 decl = get_AT_ref (die, DW_AT_specification);
6402 if (decl != NULL)
6403 collect_checksum_attributes (&attrs, decl);
6404 collect_checksum_attributes (&attrs, die);
6406 CHECKSUM_ATTR (attrs.at_name);
6407 CHECKSUM_ATTR (attrs.at_accessibility);
6408 CHECKSUM_ATTR (attrs.at_address_class);
6409 CHECKSUM_ATTR (attrs.at_allocated);
6410 CHECKSUM_ATTR (attrs.at_artificial);
6411 CHECKSUM_ATTR (attrs.at_associated);
6412 CHECKSUM_ATTR (attrs.at_binary_scale);
6413 CHECKSUM_ATTR (attrs.at_bit_offset);
6414 CHECKSUM_ATTR (attrs.at_bit_size);
6415 CHECKSUM_ATTR (attrs.at_bit_stride);
6416 CHECKSUM_ATTR (attrs.at_byte_size);
6417 CHECKSUM_ATTR (attrs.at_byte_stride);
6418 CHECKSUM_ATTR (attrs.at_const_value);
6419 CHECKSUM_ATTR (attrs.at_containing_type);
6420 CHECKSUM_ATTR (attrs.at_count);
6421 CHECKSUM_ATTR (attrs.at_data_location);
6422 CHECKSUM_ATTR (attrs.at_data_member_location);
6423 CHECKSUM_ATTR (attrs.at_decimal_scale);
6424 CHECKSUM_ATTR (attrs.at_decimal_sign);
6425 CHECKSUM_ATTR (attrs.at_default_value);
6426 CHECKSUM_ATTR (attrs.at_digit_count);
6427 CHECKSUM_ATTR (attrs.at_discr);
6428 CHECKSUM_ATTR (attrs.at_discr_list);
6429 CHECKSUM_ATTR (attrs.at_discr_value);
6430 CHECKSUM_ATTR (attrs.at_encoding);
6431 CHECKSUM_ATTR (attrs.at_endianity);
6432 CHECKSUM_ATTR (attrs.at_explicit);
6433 CHECKSUM_ATTR (attrs.at_is_optional);
6434 CHECKSUM_ATTR (attrs.at_location);
6435 CHECKSUM_ATTR (attrs.at_lower_bound);
6436 CHECKSUM_ATTR (attrs.at_mutable);
6437 CHECKSUM_ATTR (attrs.at_ordering);
6438 CHECKSUM_ATTR (attrs.at_picture_string);
6439 CHECKSUM_ATTR (attrs.at_prototyped);
6440 CHECKSUM_ATTR (attrs.at_small);
6441 CHECKSUM_ATTR (attrs.at_segment);
6442 CHECKSUM_ATTR (attrs.at_string_length);
6443 CHECKSUM_ATTR (attrs.at_threads_scaled);
6444 CHECKSUM_ATTR (attrs.at_upper_bound);
6445 CHECKSUM_ATTR (attrs.at_use_location);
6446 CHECKSUM_ATTR (attrs.at_use_UTF8);
6447 CHECKSUM_ATTR (attrs.at_variable_parameter);
6448 CHECKSUM_ATTR (attrs.at_virtuality);
6449 CHECKSUM_ATTR (attrs.at_visibility);
6450 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6451 CHECKSUM_ATTR (attrs.at_type);
6452 CHECKSUM_ATTR (attrs.at_friend);
6454 /* Checksum the child DIEs. */
6455 c = die->die_child;
6456 if (c) do {
6457 dw_attr_ref name_attr;
6459 c = c->die_sib;
6460 name_attr = get_AT (c, DW_AT_name);
6461 if (is_template_instantiation (c))
6463 /* Ignore instantiations of member type and function templates. */
6465 else if (name_attr != NULL
6466 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6468 /* Use a shallow checksum for named nested types and member
6469 functions. */
6470 CHECKSUM_ULEB128 ('S');
6471 CHECKSUM_ULEB128 (c->die_tag);
6472 CHECKSUM_STRING (AT_string (name_attr));
6474 else
6476 /* Use a deep checksum for other children. */
6477 /* Mark this DIE so it gets processed when unmarking. */
6478 if (c->die_mark == 0)
6479 c->die_mark = -1;
6480 die_checksum_ordered (c, ctx, mark);
6482 } while (c != die->die_child);
6484 CHECKSUM_ULEB128 (0);
6487 /* Add a type name and tag to a hash. */
6488 static void
6489 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6491 CHECKSUM_ULEB128 (tag);
6492 CHECKSUM_STRING (name);
6495 #undef CHECKSUM
6496 #undef CHECKSUM_STRING
6497 #undef CHECKSUM_ATTR
6498 #undef CHECKSUM_LEB128
6499 #undef CHECKSUM_ULEB128
6501 /* Generate the type signature for DIE. This is computed by generating an
6502 MD5 checksum over the DIE's tag, its relevant attributes, and its
6503 children. Attributes that are references to other DIEs are processed
6504 by recursion, using the MARK field to prevent infinite recursion.
6505 If the DIE is nested inside a namespace or another type, we also
6506 need to include that context in the signature. The lower 64 bits
6507 of the resulting MD5 checksum comprise the signature. */
6509 static void
6510 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6512 int mark;
6513 const char *name;
6514 unsigned char checksum[16];
6515 struct md5_ctx ctx;
6516 dw_die_ref decl;
6517 dw_die_ref parent;
6519 name = get_AT_string (die, DW_AT_name);
6520 decl = get_AT_ref (die, DW_AT_specification);
6521 parent = get_die_parent (die);
6523 /* First, compute a signature for just the type name (and its surrounding
6524 context, if any. This is stored in the type unit DIE for link-time
6525 ODR (one-definition rule) checking. */
6527 if (is_cxx () && name != NULL)
6529 md5_init_ctx (&ctx);
6531 /* Checksum the names of surrounding namespaces and structures. */
6532 if (parent != NULL)
6533 checksum_die_context (parent, &ctx);
6535 /* Checksum the current DIE. */
6536 die_odr_checksum (die->die_tag, name, &ctx);
6537 md5_finish_ctx (&ctx, checksum);
6539 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6542 /* Next, compute the complete type signature. */
6544 md5_init_ctx (&ctx);
6545 mark = 1;
6546 die->die_mark = mark;
6548 /* Checksum the names of surrounding namespaces and structures. */
6549 if (parent != NULL)
6550 checksum_die_context (parent, &ctx);
6552 /* Checksum the DIE and its children. */
6553 die_checksum_ordered (die, &ctx, &mark);
6554 unmark_all_dies (die);
6555 md5_finish_ctx (&ctx, checksum);
6557 /* Store the signature in the type node and link the type DIE and the
6558 type node together. */
6559 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6560 DWARF_TYPE_SIGNATURE_SIZE);
6561 die->comdat_type_p = true;
6562 die->die_id.die_type_node = type_node;
6563 type_node->type_die = die;
6565 /* If the DIE is a specification, link its declaration to the type node
6566 as well. */
6567 if (decl != NULL)
6569 decl->comdat_type_p = true;
6570 decl->die_id.die_type_node = type_node;
6574 /* Do the location expressions look same? */
6575 static inline int
6576 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6578 return loc1->dw_loc_opc == loc2->dw_loc_opc
6579 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6580 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6583 /* Do the values look the same? */
6584 static int
6585 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6587 dw_loc_descr_ref loc1, loc2;
6588 rtx r1, r2;
6590 if (v1->val_class != v2->val_class)
6591 return 0;
6593 switch (v1->val_class)
6595 case dw_val_class_const:
6596 return v1->v.val_int == v2->v.val_int;
6597 case dw_val_class_unsigned_const:
6598 return v1->v.val_unsigned == v2->v.val_unsigned;
6599 case dw_val_class_const_double:
6600 return v1->v.val_double.high == v2->v.val_double.high
6601 && v1->v.val_double.low == v2->v.val_double.low;
6602 case dw_val_class_wide_int:
6603 return *v1->v.val_wide == *v2->v.val_wide;
6604 case dw_val_class_vec:
6605 if (v1->v.val_vec.length != v2->v.val_vec.length
6606 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6607 return 0;
6608 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6609 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6610 return 0;
6611 return 1;
6612 case dw_val_class_flag:
6613 return v1->v.val_flag == v2->v.val_flag;
6614 case dw_val_class_str:
6615 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6617 case dw_val_class_addr:
6618 r1 = v1->v.val_addr;
6619 r2 = v2->v.val_addr;
6620 if (GET_CODE (r1) != GET_CODE (r2))
6621 return 0;
6622 return !rtx_equal_p (r1, r2);
6624 case dw_val_class_offset:
6625 return v1->v.val_offset == v2->v.val_offset;
6627 case dw_val_class_loc:
6628 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6629 loc1 && loc2;
6630 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6631 if (!same_loc_p (loc1, loc2, mark))
6632 return 0;
6633 return !loc1 && !loc2;
6635 case dw_val_class_die_ref:
6636 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6638 case dw_val_class_fde_ref:
6639 case dw_val_class_vms_delta:
6640 case dw_val_class_lbl_id:
6641 case dw_val_class_lineptr:
6642 case dw_val_class_macptr:
6643 case dw_val_class_high_pc:
6644 return 1;
6646 case dw_val_class_file:
6647 return v1->v.val_file == v2->v.val_file;
6649 case dw_val_class_data8:
6650 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6652 default:
6653 return 1;
6657 /* Do the attributes look the same? */
6659 static int
6660 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6662 if (at1->dw_attr != at2->dw_attr)
6663 return 0;
6665 /* We don't care that this was compiled with a different compiler
6666 snapshot; if the output is the same, that's what matters. */
6667 if (at1->dw_attr == DW_AT_producer)
6668 return 1;
6670 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6673 /* Do the dies look the same? */
6675 static int
6676 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6678 dw_die_ref c1, c2;
6679 dw_attr_ref a1;
6680 unsigned ix;
6682 /* To avoid infinite recursion. */
6683 if (die1->die_mark)
6684 return die1->die_mark == die2->die_mark;
6685 die1->die_mark = die2->die_mark = ++(*mark);
6687 if (die1->die_tag != die2->die_tag)
6688 return 0;
6690 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6691 return 0;
6693 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6694 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6695 return 0;
6697 c1 = die1->die_child;
6698 c2 = die2->die_child;
6699 if (! c1)
6701 if (c2)
6702 return 0;
6704 else
6705 for (;;)
6707 if (!same_die_p (c1, c2, mark))
6708 return 0;
6709 c1 = c1->die_sib;
6710 c2 = c2->die_sib;
6711 if (c1 == die1->die_child)
6713 if (c2 == die2->die_child)
6714 break;
6715 else
6716 return 0;
6720 return 1;
6723 /* Do the dies look the same? Wrapper around same_die_p. */
6725 static int
6726 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6728 int mark = 0;
6729 int ret = same_die_p (die1, die2, &mark);
6731 unmark_all_dies (die1);
6732 unmark_all_dies (die2);
6734 return ret;
6737 /* The prefix to attach to symbols on DIEs in the current comdat debug
6738 info section. */
6739 static const char *comdat_symbol_id;
6741 /* The index of the current symbol within the current comdat CU. */
6742 static unsigned int comdat_symbol_number;
6744 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6745 children, and set comdat_symbol_id accordingly. */
6747 static void
6748 compute_section_prefix (dw_die_ref unit_die)
6750 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6751 const char *base = die_name ? lbasename (die_name) : "anonymous";
6752 char *name = XALLOCAVEC (char, strlen (base) + 64);
6753 char *p;
6754 int i, mark;
6755 unsigned char checksum[16];
6756 struct md5_ctx ctx;
6758 /* Compute the checksum of the DIE, then append part of it as hex digits to
6759 the name filename of the unit. */
6761 md5_init_ctx (&ctx);
6762 mark = 0;
6763 die_checksum (unit_die, &ctx, &mark);
6764 unmark_all_dies (unit_die);
6765 md5_finish_ctx (&ctx, checksum);
6767 sprintf (name, "%s.", base);
6768 clean_symbol_name (name);
6770 p = name + strlen (name);
6771 for (i = 0; i < 4; i++)
6773 sprintf (p, "%.2x", checksum[i]);
6774 p += 2;
6777 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6778 comdat_symbol_number = 0;
6781 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6783 static int
6784 is_type_die (dw_die_ref die)
6786 switch (die->die_tag)
6788 case DW_TAG_array_type:
6789 case DW_TAG_class_type:
6790 case DW_TAG_interface_type:
6791 case DW_TAG_enumeration_type:
6792 case DW_TAG_pointer_type:
6793 case DW_TAG_reference_type:
6794 case DW_TAG_rvalue_reference_type:
6795 case DW_TAG_string_type:
6796 case DW_TAG_structure_type:
6797 case DW_TAG_subroutine_type:
6798 case DW_TAG_union_type:
6799 case DW_TAG_ptr_to_member_type:
6800 case DW_TAG_set_type:
6801 case DW_TAG_subrange_type:
6802 case DW_TAG_base_type:
6803 case DW_TAG_const_type:
6804 case DW_TAG_file_type:
6805 case DW_TAG_packed_type:
6806 case DW_TAG_volatile_type:
6807 case DW_TAG_typedef:
6808 return 1;
6809 default:
6810 return 0;
6814 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6815 Basically, we want to choose the bits that are likely to be shared between
6816 compilations (types) and leave out the bits that are specific to individual
6817 compilations (functions). */
6819 static int
6820 is_comdat_die (dw_die_ref c)
6822 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6823 we do for stabs. The advantage is a greater likelihood of sharing between
6824 objects that don't include headers in the same order (and therefore would
6825 put the base types in a different comdat). jason 8/28/00 */
6827 if (c->die_tag == DW_TAG_base_type)
6828 return 0;
6830 if (c->die_tag == DW_TAG_pointer_type
6831 || c->die_tag == DW_TAG_reference_type
6832 || c->die_tag == DW_TAG_rvalue_reference_type
6833 || c->die_tag == DW_TAG_const_type
6834 || c->die_tag == DW_TAG_volatile_type)
6836 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6838 return t ? is_comdat_die (t) : 0;
6841 return is_type_die (c);
6844 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6845 compilation unit. */
6847 static int
6848 is_symbol_die (dw_die_ref c)
6850 return (is_type_die (c)
6851 || is_declaration_die (c)
6852 || c->die_tag == DW_TAG_namespace
6853 || c->die_tag == DW_TAG_module);
6856 /* Returns true iff C is a compile-unit DIE. */
6858 static inline bool
6859 is_cu_die (dw_die_ref c)
6861 return c && c->die_tag == DW_TAG_compile_unit;
6864 /* Returns true iff C is a unit DIE of some sort. */
6866 static inline bool
6867 is_unit_die (dw_die_ref c)
6869 return c && (c->die_tag == DW_TAG_compile_unit
6870 || c->die_tag == DW_TAG_partial_unit
6871 || c->die_tag == DW_TAG_type_unit);
6874 /* Returns true iff C is a namespace DIE. */
6876 static inline bool
6877 is_namespace_die (dw_die_ref c)
6879 return c && c->die_tag == DW_TAG_namespace;
6882 /* Returns true iff C is a class or structure DIE. */
6884 static inline bool
6885 is_class_die (dw_die_ref c)
6887 return c && (c->die_tag == DW_TAG_class_type
6888 || c->die_tag == DW_TAG_structure_type);
6891 /* Return non-zero if this DIE is a template parameter. */
6893 static inline bool
6894 is_template_parameter (dw_die_ref die)
6896 switch (die->die_tag)
6898 case DW_TAG_template_type_param:
6899 case DW_TAG_template_value_param:
6900 case DW_TAG_GNU_template_template_param:
6901 case DW_TAG_GNU_template_parameter_pack:
6902 return true;
6903 default:
6904 return false;
6908 /* Return non-zero if this DIE represents a template instantiation. */
6910 static inline bool
6911 is_template_instantiation (dw_die_ref die)
6913 dw_die_ref c;
6915 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6916 return false;
6917 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6918 return false;
6921 static char *
6922 gen_internal_sym (const char *prefix)
6924 char buf[256];
6926 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6927 return xstrdup (buf);
6930 /* Assign symbols to all worthy DIEs under DIE. */
6932 static void
6933 assign_symbol_names (dw_die_ref die)
6935 dw_die_ref c;
6937 if (is_symbol_die (die) && !die->comdat_type_p)
6939 if (comdat_symbol_id)
6941 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6943 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6944 comdat_symbol_id, comdat_symbol_number++);
6945 die->die_id.die_symbol = xstrdup (p);
6947 else
6948 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6951 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6954 struct cu_hash_table_entry
6956 dw_die_ref cu;
6957 unsigned min_comdat_num, max_comdat_num;
6958 struct cu_hash_table_entry *next;
6961 /* Helpers to manipulate hash table of CUs. */
6963 struct cu_hash_table_entry_hasher
6965 typedef cu_hash_table_entry *value_type;
6966 typedef die_struct *compare_type;
6967 static inline hashval_t hash (const cu_hash_table_entry *);
6968 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6969 static inline void remove (cu_hash_table_entry *);
6972 inline hashval_t
6973 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6975 return htab_hash_string (entry->cu->die_id.die_symbol);
6978 inline bool
6979 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6980 const die_struct *entry2)
6982 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6985 inline void
6986 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6988 struct cu_hash_table_entry *next;
6990 while (entry)
6992 next = entry->next;
6993 free (entry);
6994 entry = next;
6998 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7000 /* Check whether we have already seen this CU and set up SYM_NUM
7001 accordingly. */
7002 static int
7003 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7005 struct cu_hash_table_entry dummy;
7006 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7008 dummy.max_comdat_num = 0;
7010 slot = htable->find_slot_with_hash (cu,
7011 htab_hash_string (cu->die_id.die_symbol),
7012 INSERT);
7013 entry = *slot;
7015 for (; entry; last = entry, entry = entry->next)
7017 if (same_die_p_wrap (cu, entry->cu))
7018 break;
7021 if (entry)
7023 *sym_num = entry->min_comdat_num;
7024 return 1;
7027 entry = XCNEW (struct cu_hash_table_entry);
7028 entry->cu = cu;
7029 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7030 entry->next = *slot;
7031 *slot = entry;
7033 return 0;
7036 /* Record SYM_NUM to record of CU in HTABLE. */
7037 static void
7038 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7039 unsigned int sym_num)
7041 struct cu_hash_table_entry **slot, *entry;
7043 slot = htable->find_slot_with_hash (cu,
7044 htab_hash_string (cu->die_id.die_symbol),
7045 NO_INSERT);
7046 entry = *slot;
7048 entry->max_comdat_num = sym_num;
7051 /* Traverse the DIE (which is always comp_unit_die), and set up
7052 additional compilation units for each of the include files we see
7053 bracketed by BINCL/EINCL. */
7055 static void
7056 break_out_includes (dw_die_ref die)
7058 dw_die_ref c;
7059 dw_die_ref unit = NULL;
7060 limbo_die_node *node, **pnode;
7062 c = die->die_child;
7063 if (c) do {
7064 dw_die_ref prev = c;
7065 c = c->die_sib;
7066 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7067 || (unit && is_comdat_die (c)))
7069 dw_die_ref next = c->die_sib;
7071 /* This DIE is for a secondary CU; remove it from the main one. */
7072 remove_child_with_prev (c, prev);
7074 if (c->die_tag == DW_TAG_GNU_BINCL)
7075 unit = push_new_compile_unit (unit, c);
7076 else if (c->die_tag == DW_TAG_GNU_EINCL)
7077 unit = pop_compile_unit (unit);
7078 else
7079 add_child_die (unit, c);
7080 c = next;
7081 if (c == die->die_child)
7082 break;
7084 } while (c != die->die_child);
7086 #if 0
7087 /* We can only use this in debugging, since the frontend doesn't check
7088 to make sure that we leave every include file we enter. */
7089 gcc_assert (!unit);
7090 #endif
7092 assign_symbol_names (die);
7093 cu_hash_type cu_hash_table (10);
7094 for (node = limbo_die_list, pnode = &limbo_die_list;
7095 node;
7096 node = node->next)
7098 int is_dupl;
7100 compute_section_prefix (node->die);
7101 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7102 &comdat_symbol_number);
7103 assign_symbol_names (node->die);
7104 if (is_dupl)
7105 *pnode = node->next;
7106 else
7108 pnode = &node->next;
7109 record_comdat_symbol_number (node->die, &cu_hash_table,
7110 comdat_symbol_number);
7115 /* Return non-zero if this DIE is a declaration. */
7117 static int
7118 is_declaration_die (dw_die_ref die)
7120 dw_attr_ref a;
7121 unsigned ix;
7123 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7124 if (a->dw_attr == DW_AT_declaration)
7125 return 1;
7127 return 0;
7130 /* Return non-zero if this DIE is nested inside a subprogram. */
7132 static int
7133 is_nested_in_subprogram (dw_die_ref die)
7135 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7137 if (decl == NULL)
7138 decl = die;
7139 return local_scope_p (decl);
7142 /* Return non-zero if this DIE contains a defining declaration of a
7143 subprogram. */
7145 static int
7146 contains_subprogram_definition (dw_die_ref die)
7148 dw_die_ref c;
7150 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7151 return 1;
7152 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7153 return 0;
7156 /* Return non-zero if this is a type DIE that should be moved to a
7157 COMDAT .debug_types section. */
7159 static int
7160 should_move_die_to_comdat (dw_die_ref die)
7162 switch (die->die_tag)
7164 case DW_TAG_class_type:
7165 case DW_TAG_structure_type:
7166 case DW_TAG_enumeration_type:
7167 case DW_TAG_union_type:
7168 /* Don't move declarations, inlined instances, types nested in a
7169 subprogram, or types that contain subprogram definitions. */
7170 if (is_declaration_die (die)
7171 || get_AT (die, DW_AT_abstract_origin)
7172 || is_nested_in_subprogram (die)
7173 || contains_subprogram_definition (die))
7174 return 0;
7175 return 1;
7176 case DW_TAG_array_type:
7177 case DW_TAG_interface_type:
7178 case DW_TAG_pointer_type:
7179 case DW_TAG_reference_type:
7180 case DW_TAG_rvalue_reference_type:
7181 case DW_TAG_string_type:
7182 case DW_TAG_subroutine_type:
7183 case DW_TAG_ptr_to_member_type:
7184 case DW_TAG_set_type:
7185 case DW_TAG_subrange_type:
7186 case DW_TAG_base_type:
7187 case DW_TAG_const_type:
7188 case DW_TAG_file_type:
7189 case DW_TAG_packed_type:
7190 case DW_TAG_volatile_type:
7191 case DW_TAG_typedef:
7192 default:
7193 return 0;
7197 /* Make a clone of DIE. */
7199 static dw_die_ref
7200 clone_die (dw_die_ref die)
7202 dw_die_ref clone;
7203 dw_attr_ref a;
7204 unsigned ix;
7206 clone = ggc_cleared_alloc<die_node> ();
7207 clone->die_tag = die->die_tag;
7209 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7210 add_dwarf_attr (clone, a);
7212 return clone;
7215 /* Make a clone of the tree rooted at DIE. */
7217 static dw_die_ref
7218 clone_tree (dw_die_ref die)
7220 dw_die_ref c;
7221 dw_die_ref clone = clone_die (die);
7223 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7225 return clone;
7228 /* Make a clone of DIE as a declaration. */
7230 static dw_die_ref
7231 clone_as_declaration (dw_die_ref die)
7233 dw_die_ref clone;
7234 dw_die_ref decl;
7235 dw_attr_ref a;
7236 unsigned ix;
7238 /* If the DIE is already a declaration, just clone it. */
7239 if (is_declaration_die (die))
7240 return clone_die (die);
7242 /* If the DIE is a specification, just clone its declaration DIE. */
7243 decl = get_AT_ref (die, DW_AT_specification);
7244 if (decl != NULL)
7246 clone = clone_die (decl);
7247 if (die->comdat_type_p)
7248 add_AT_die_ref (clone, DW_AT_signature, die);
7249 return clone;
7252 clone = ggc_cleared_alloc<die_node> ();
7253 clone->die_tag = die->die_tag;
7255 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7257 /* We don't want to copy over all attributes.
7258 For example we don't want DW_AT_byte_size because otherwise we will no
7259 longer have a declaration and GDB will treat it as a definition. */
7261 switch (a->dw_attr)
7263 case DW_AT_abstract_origin:
7264 case DW_AT_artificial:
7265 case DW_AT_containing_type:
7266 case DW_AT_external:
7267 case DW_AT_name:
7268 case DW_AT_type:
7269 case DW_AT_virtuality:
7270 case DW_AT_linkage_name:
7271 case DW_AT_MIPS_linkage_name:
7272 add_dwarf_attr (clone, a);
7273 break;
7274 case DW_AT_byte_size:
7275 default:
7276 break;
7280 if (die->comdat_type_p)
7281 add_AT_die_ref (clone, DW_AT_signature, die);
7283 add_AT_flag (clone, DW_AT_declaration, 1);
7284 return clone;
7288 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7290 struct decl_table_entry
7292 dw_die_ref orig;
7293 dw_die_ref copy;
7296 /* Helpers to manipulate hash table of copied declarations. */
7298 /* Hashtable helpers. */
7300 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7302 typedef decl_table_entry *value_type;
7303 typedef die_struct *compare_type;
7304 static inline hashval_t hash (const decl_table_entry *);
7305 static inline bool equal (const decl_table_entry *, const die_struct *);
7308 inline hashval_t
7309 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7311 return htab_hash_pointer (entry->orig);
7314 inline bool
7315 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7316 const die_struct *entry2)
7318 return entry1->orig == entry2;
7321 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7323 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7324 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7325 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7326 to check if the ancestor has already been copied into UNIT. */
7328 static dw_die_ref
7329 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7330 decl_hash_type *decl_table)
7332 dw_die_ref parent = die->die_parent;
7333 dw_die_ref new_parent = unit;
7334 dw_die_ref copy;
7335 decl_table_entry **slot = NULL;
7336 struct decl_table_entry *entry = NULL;
7338 if (decl_table)
7340 /* Check if the entry has already been copied to UNIT. */
7341 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7342 INSERT);
7343 if (*slot != HTAB_EMPTY_ENTRY)
7345 entry = *slot;
7346 return entry->copy;
7349 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7350 entry = XCNEW (struct decl_table_entry);
7351 entry->orig = die;
7352 entry->copy = NULL;
7353 *slot = entry;
7356 if (parent != NULL)
7358 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7359 if (spec != NULL)
7360 parent = spec;
7361 if (!is_unit_die (parent))
7362 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7365 copy = clone_as_declaration (die);
7366 add_child_die (new_parent, copy);
7368 if (decl_table)
7370 /* Record the pointer to the copy. */
7371 entry->copy = copy;
7374 return copy;
7376 /* Copy the declaration context to the new type unit DIE. This includes
7377 any surrounding namespace or type declarations. If the DIE has an
7378 AT_specification attribute, it also includes attributes and children
7379 attached to the specification, and returns a pointer to the original
7380 parent of the declaration DIE. Returns NULL otherwise. */
7382 static dw_die_ref
7383 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7385 dw_die_ref decl;
7386 dw_die_ref new_decl;
7387 dw_die_ref orig_parent = NULL;
7389 decl = get_AT_ref (die, DW_AT_specification);
7390 if (decl == NULL)
7391 decl = die;
7392 else
7394 unsigned ix;
7395 dw_die_ref c;
7396 dw_attr_ref a;
7398 /* The original DIE will be changed to a declaration, and must
7399 be moved to be a child of the original declaration DIE. */
7400 orig_parent = decl->die_parent;
7402 /* Copy the type node pointer from the new DIE to the original
7403 declaration DIE so we can forward references later. */
7404 decl->comdat_type_p = true;
7405 decl->die_id.die_type_node = die->die_id.die_type_node;
7407 remove_AT (die, DW_AT_specification);
7409 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7411 if (a->dw_attr != DW_AT_name
7412 && a->dw_attr != DW_AT_declaration
7413 && a->dw_attr != DW_AT_external)
7414 add_dwarf_attr (die, a);
7417 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7420 if (decl->die_parent != NULL
7421 && !is_unit_die (decl->die_parent))
7423 new_decl = copy_ancestor_tree (unit, decl, NULL);
7424 if (new_decl != NULL)
7426 remove_AT (new_decl, DW_AT_signature);
7427 add_AT_specification (die, new_decl);
7431 return orig_parent;
7434 /* Generate the skeleton ancestor tree for the given NODE, then clone
7435 the DIE and add the clone into the tree. */
7437 static void
7438 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7440 if (node->new_die != NULL)
7441 return;
7443 node->new_die = clone_as_declaration (node->old_die);
7445 if (node->parent != NULL)
7447 generate_skeleton_ancestor_tree (node->parent);
7448 add_child_die (node->parent->new_die, node->new_die);
7452 /* Generate a skeleton tree of DIEs containing any declarations that are
7453 found in the original tree. We traverse the tree looking for declaration
7454 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7456 static void
7457 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7459 skeleton_chain_node node;
7460 dw_die_ref c;
7461 dw_die_ref first;
7462 dw_die_ref prev = NULL;
7463 dw_die_ref next = NULL;
7465 node.parent = parent;
7467 first = c = parent->old_die->die_child;
7468 if (c)
7469 next = c->die_sib;
7470 if (c) do {
7471 if (prev == NULL || prev->die_sib == c)
7472 prev = c;
7473 c = next;
7474 next = (c == first ? NULL : c->die_sib);
7475 node.old_die = c;
7476 node.new_die = NULL;
7477 if (is_declaration_die (c))
7479 if (is_template_instantiation (c))
7481 /* Instantiated templates do not need to be cloned into the
7482 type unit. Just move the DIE and its children back to
7483 the skeleton tree (in the main CU). */
7484 remove_child_with_prev (c, prev);
7485 add_child_die (parent->new_die, c);
7486 c = prev;
7488 else
7490 /* Clone the existing DIE, move the original to the skeleton
7491 tree (which is in the main CU), and put the clone, with
7492 all the original's children, where the original came from
7493 (which is about to be moved to the type unit). */
7494 dw_die_ref clone = clone_die (c);
7495 move_all_children (c, clone);
7497 /* If the original has a DW_AT_object_pointer attribute,
7498 it would now point to a child DIE just moved to the
7499 cloned tree, so we need to remove that attribute from
7500 the original. */
7501 remove_AT (c, DW_AT_object_pointer);
7503 replace_child (c, clone, prev);
7504 generate_skeleton_ancestor_tree (parent);
7505 add_child_die (parent->new_die, c);
7506 node.new_die = c;
7507 c = clone;
7510 generate_skeleton_bottom_up (&node);
7511 } while (next != NULL);
7514 /* Wrapper function for generate_skeleton_bottom_up. */
7516 static dw_die_ref
7517 generate_skeleton (dw_die_ref die)
7519 skeleton_chain_node node;
7521 node.old_die = die;
7522 node.new_die = NULL;
7523 node.parent = NULL;
7525 /* If this type definition is nested inside another type,
7526 and is not an instantiation of a template, always leave
7527 at least a declaration in its place. */
7528 if (die->die_parent != NULL
7529 && is_type_die (die->die_parent)
7530 && !is_template_instantiation (die))
7531 node.new_die = clone_as_declaration (die);
7533 generate_skeleton_bottom_up (&node);
7534 return node.new_die;
7537 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7538 declaration. The original DIE is moved to a new compile unit so that
7539 existing references to it follow it to the new location. If any of the
7540 original DIE's descendants is a declaration, we need to replace the
7541 original DIE with a skeleton tree and move the declarations back into the
7542 skeleton tree. */
7544 static dw_die_ref
7545 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7546 dw_die_ref prev)
7548 dw_die_ref skeleton, orig_parent;
7550 /* Copy the declaration context to the type unit DIE. If the returned
7551 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7552 that DIE. */
7553 orig_parent = copy_declaration_context (unit, child);
7555 skeleton = generate_skeleton (child);
7556 if (skeleton == NULL)
7557 remove_child_with_prev (child, prev);
7558 else
7560 skeleton->comdat_type_p = true;
7561 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7563 /* If the original DIE was a specification, we need to put
7564 the skeleton under the parent DIE of the declaration.
7565 This leaves the original declaration in the tree, but
7566 it will be pruned later since there are no longer any
7567 references to it. */
7568 if (orig_parent != NULL)
7570 remove_child_with_prev (child, prev);
7571 add_child_die (orig_parent, skeleton);
7573 else
7574 replace_child (child, skeleton, prev);
7577 return skeleton;
7580 /* Traverse the DIE and set up additional .debug_types sections for each
7581 type worthy of being placed in a COMDAT section. */
7583 static void
7584 break_out_comdat_types (dw_die_ref die)
7586 dw_die_ref c;
7587 dw_die_ref first;
7588 dw_die_ref prev = NULL;
7589 dw_die_ref next = NULL;
7590 dw_die_ref unit = NULL;
7592 first = c = die->die_child;
7593 if (c)
7594 next = c->die_sib;
7595 if (c) do {
7596 if (prev == NULL || prev->die_sib == c)
7597 prev = c;
7598 c = next;
7599 next = (c == first ? NULL : c->die_sib);
7600 if (should_move_die_to_comdat (c))
7602 dw_die_ref replacement;
7603 comdat_type_node_ref type_node;
7605 /* Break out nested types into their own type units. */
7606 break_out_comdat_types (c);
7608 /* Create a new type unit DIE as the root for the new tree, and
7609 add it to the list of comdat types. */
7610 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7611 add_AT_unsigned (unit, DW_AT_language,
7612 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7613 type_node = ggc_cleared_alloc<comdat_type_node> ();
7614 type_node->root_die = unit;
7615 type_node->next = comdat_type_list;
7616 comdat_type_list = type_node;
7618 /* Generate the type signature. */
7619 generate_type_signature (c, type_node);
7621 /* Copy the declaration context, attributes, and children of the
7622 declaration into the new type unit DIE, then remove this DIE
7623 from the main CU (or replace it with a skeleton if necessary). */
7624 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7625 type_node->skeleton_die = replacement;
7627 /* Add the DIE to the new compunit. */
7628 add_child_die (unit, c);
7630 if (replacement != NULL)
7631 c = replacement;
7633 else if (c->die_tag == DW_TAG_namespace
7634 || c->die_tag == DW_TAG_class_type
7635 || c->die_tag == DW_TAG_structure_type
7636 || c->die_tag == DW_TAG_union_type)
7638 /* Look for nested types that can be broken out. */
7639 break_out_comdat_types (c);
7641 } while (next != NULL);
7644 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7645 Enter all the cloned children into the hash table decl_table. */
7647 static dw_die_ref
7648 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7650 dw_die_ref c;
7651 dw_die_ref clone;
7652 struct decl_table_entry *entry;
7653 decl_table_entry **slot;
7655 if (die->die_tag == DW_TAG_subprogram)
7656 clone = clone_as_declaration (die);
7657 else
7658 clone = clone_die (die);
7660 slot = decl_table->find_slot_with_hash (die,
7661 htab_hash_pointer (die), INSERT);
7663 /* Assert that DIE isn't in the hash table yet. If it would be there
7664 before, the ancestors would be necessarily there as well, therefore
7665 clone_tree_partial wouldn't be called. */
7666 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7668 entry = XCNEW (struct decl_table_entry);
7669 entry->orig = die;
7670 entry->copy = clone;
7671 *slot = entry;
7673 if (die->die_tag != DW_TAG_subprogram)
7674 FOR_EACH_CHILD (die, c,
7675 add_child_die (clone, clone_tree_partial (c, decl_table)));
7677 return clone;
7680 /* Walk the DIE and its children, looking for references to incomplete
7681 or trivial types that are unmarked (i.e., that are not in the current
7682 type_unit). */
7684 static void
7685 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7687 dw_die_ref c;
7688 dw_attr_ref a;
7689 unsigned ix;
7691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7693 if (AT_class (a) == dw_val_class_die_ref)
7695 dw_die_ref targ = AT_ref (a);
7696 decl_table_entry **slot;
7697 struct decl_table_entry *entry;
7699 if (targ->die_mark != 0 || targ->comdat_type_p)
7700 continue;
7702 slot = decl_table->find_slot_with_hash (targ,
7703 htab_hash_pointer (targ),
7704 INSERT);
7706 if (*slot != HTAB_EMPTY_ENTRY)
7708 /* TARG has already been copied, so we just need to
7709 modify the reference to point to the copy. */
7710 entry = *slot;
7711 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7713 else
7715 dw_die_ref parent = unit;
7716 dw_die_ref copy = clone_die (targ);
7718 /* Record in DECL_TABLE that TARG has been copied.
7719 Need to do this now, before the recursive call,
7720 because DECL_TABLE may be expanded and SLOT
7721 would no longer be a valid pointer. */
7722 entry = XCNEW (struct decl_table_entry);
7723 entry->orig = targ;
7724 entry->copy = copy;
7725 *slot = entry;
7727 /* If TARG is not a declaration DIE, we need to copy its
7728 children. */
7729 if (!is_declaration_die (targ))
7731 FOR_EACH_CHILD (
7732 targ, c,
7733 add_child_die (copy,
7734 clone_tree_partial (c, decl_table)));
7737 /* Make sure the cloned tree is marked as part of the
7738 type unit. */
7739 mark_dies (copy);
7741 /* If TARG has surrounding context, copy its ancestor tree
7742 into the new type unit. */
7743 if (targ->die_parent != NULL
7744 && !is_unit_die (targ->die_parent))
7745 parent = copy_ancestor_tree (unit, targ->die_parent,
7746 decl_table);
7748 add_child_die (parent, copy);
7749 a->dw_attr_val.v.val_die_ref.die = copy;
7751 /* Make sure the newly-copied DIE is walked. If it was
7752 installed in a previously-added context, it won't
7753 get visited otherwise. */
7754 if (parent != unit)
7756 /* Find the highest point of the newly-added tree,
7757 mark each node along the way, and walk from there. */
7758 parent->die_mark = 1;
7759 while (parent->die_parent
7760 && parent->die_parent->die_mark == 0)
7762 parent = parent->die_parent;
7763 parent->die_mark = 1;
7765 copy_decls_walk (unit, parent, decl_table);
7771 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7774 /* Copy declarations for "unworthy" types into the new comdat section.
7775 Incomplete types, modified types, and certain other types aren't broken
7776 out into comdat sections of their own, so they don't have a signature,
7777 and we need to copy the declaration into the same section so that we
7778 don't have an external reference. */
7780 static void
7781 copy_decls_for_unworthy_types (dw_die_ref unit)
7783 mark_dies (unit);
7784 decl_hash_type decl_table (10);
7785 copy_decls_walk (unit, unit, &decl_table);
7786 unmark_dies (unit);
7789 /* Traverse the DIE and add a sibling attribute if it may have the
7790 effect of speeding up access to siblings. To save some space,
7791 avoid generating sibling attributes for DIE's without children. */
7793 static void
7794 add_sibling_attributes (dw_die_ref die)
7796 dw_die_ref c;
7798 if (! die->die_child)
7799 return;
7801 if (die->die_parent && die != die->die_parent->die_child)
7802 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7804 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7807 /* Output all location lists for the DIE and its children. */
7809 static void
7810 output_location_lists (dw_die_ref die)
7812 dw_die_ref c;
7813 dw_attr_ref a;
7814 unsigned ix;
7816 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7817 if (AT_class (a) == dw_val_class_loc_list)
7818 output_loc_list (AT_loc_list (a));
7820 FOR_EACH_CHILD (die, c, output_location_lists (c));
7823 /* We want to limit the number of external references, because they are
7824 larger than local references: a relocation takes multiple words, and
7825 even a sig8 reference is always eight bytes, whereas a local reference
7826 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7827 So if we encounter multiple external references to the same type DIE, we
7828 make a local typedef stub for it and redirect all references there.
7830 This is the element of the hash table for keeping track of these
7831 references. */
7833 struct external_ref
7835 dw_die_ref type;
7836 dw_die_ref stub;
7837 unsigned n_refs;
7840 /* Hashtable helpers. */
7842 struct external_ref_hasher : typed_free_remove <external_ref>
7844 typedef external_ref *value_type;
7845 typedef external_ref *compare_type;
7846 static inline hashval_t hash (const external_ref *);
7847 static inline bool equal (const external_ref *, const external_ref *);
7850 inline hashval_t
7851 external_ref_hasher::hash (const external_ref *r)
7853 dw_die_ref die = r->type;
7854 hashval_t h = 0;
7856 /* We can't use the address of the DIE for hashing, because
7857 that will make the order of the stub DIEs non-deterministic. */
7858 if (! die->comdat_type_p)
7859 /* We have a symbol; use it to compute a hash. */
7860 h = htab_hash_string (die->die_id.die_symbol);
7861 else
7863 /* We have a type signature; use a subset of the bits as the hash.
7864 The 8-byte signature is at least as large as hashval_t. */
7865 comdat_type_node_ref type_node = die->die_id.die_type_node;
7866 memcpy (&h, type_node->signature, sizeof (h));
7868 return h;
7871 inline bool
7872 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7874 return r1->type == r2->type;
7877 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7879 /* Return a pointer to the external_ref for references to DIE. */
7881 static struct external_ref *
7882 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7884 struct external_ref ref, *ref_p;
7885 external_ref **slot;
7887 ref.type = die;
7888 slot = map->find_slot (&ref, INSERT);
7889 if (*slot != HTAB_EMPTY_ENTRY)
7890 return *slot;
7892 ref_p = XCNEW (struct external_ref);
7893 ref_p->type = die;
7894 *slot = ref_p;
7895 return ref_p;
7898 /* Subroutine of optimize_external_refs, below.
7900 If we see a type skeleton, record it as our stub. If we see external
7901 references, remember how many we've seen. */
7903 static void
7904 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7906 dw_die_ref c;
7907 dw_attr_ref a;
7908 unsigned ix;
7909 struct external_ref *ref_p;
7911 if (is_type_die (die)
7912 && (c = get_AT_ref (die, DW_AT_signature)))
7914 /* This is a local skeleton; use it for local references. */
7915 ref_p = lookup_external_ref (map, c);
7916 ref_p->stub = die;
7919 /* Scan the DIE references, and remember any that refer to DIEs from
7920 other CUs (i.e. those which are not marked). */
7921 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7922 if (AT_class (a) == dw_val_class_die_ref
7923 && (c = AT_ref (a))->die_mark == 0
7924 && is_type_die (c))
7926 ref_p = lookup_external_ref (map, c);
7927 ref_p->n_refs++;
7930 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7933 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7934 points to an external_ref, DATA is the CU we're processing. If we don't
7935 already have a local stub, and we have multiple refs, build a stub. */
7938 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7940 struct external_ref *ref_p = *slot;
7942 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7944 /* We have multiple references to this type, so build a small stub.
7945 Both of these forms are a bit dodgy from the perspective of the
7946 DWARF standard, since technically they should have names. */
7947 dw_die_ref cu = data;
7948 dw_die_ref type = ref_p->type;
7949 dw_die_ref stub = NULL;
7951 if (type->comdat_type_p)
7953 /* If we refer to this type via sig8, use AT_signature. */
7954 stub = new_die (type->die_tag, cu, NULL_TREE);
7955 add_AT_die_ref (stub, DW_AT_signature, type);
7957 else
7959 /* Otherwise, use a typedef with no name. */
7960 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7961 add_AT_die_ref (stub, DW_AT_type, type);
7964 stub->die_mark++;
7965 ref_p->stub = stub;
7967 return 1;
7970 /* DIE is a unit; look through all the DIE references to see if there are
7971 any external references to types, and if so, create local stubs for
7972 them which will be applied in build_abbrev_table. This is useful because
7973 references to local DIEs are smaller. */
7975 static external_ref_hash_type *
7976 optimize_external_refs (dw_die_ref die)
7978 external_ref_hash_type *map = new external_ref_hash_type (10);
7979 optimize_external_refs_1 (die, map);
7980 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7981 return map;
7984 /* The format of each DIE (and its attribute value pairs) is encoded in an
7985 abbreviation table. This routine builds the abbreviation table and assigns
7986 a unique abbreviation id for each abbreviation entry. The children of each
7987 die are visited recursively. */
7989 static void
7990 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7992 unsigned long abbrev_id;
7993 unsigned int n_alloc;
7994 dw_die_ref c;
7995 dw_attr_ref a;
7996 unsigned ix;
7998 /* Scan the DIE references, and replace any that refer to
7999 DIEs from other CUs (i.e. those which are not marked) with
8000 the local stubs we built in optimize_external_refs. */
8001 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8002 if (AT_class (a) == dw_val_class_die_ref
8003 && (c = AT_ref (a))->die_mark == 0)
8005 struct external_ref *ref_p;
8006 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8008 ref_p = lookup_external_ref (extern_map, c);
8009 if (ref_p->stub && ref_p->stub != die)
8010 change_AT_die_ref (a, ref_p->stub);
8011 else
8012 /* We aren't changing this reference, so mark it external. */
8013 set_AT_ref_external (a, 1);
8016 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8018 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8019 dw_attr_ref die_a, abbrev_a;
8020 unsigned ix;
8021 bool ok = true;
8023 if (abbrev->die_tag != die->die_tag)
8024 continue;
8025 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8026 continue;
8028 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8029 continue;
8031 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8033 abbrev_a = &(*abbrev->die_attr)[ix];
8034 if ((abbrev_a->dw_attr != die_a->dw_attr)
8035 || (value_format (abbrev_a) != value_format (die_a)))
8037 ok = false;
8038 break;
8041 if (ok)
8042 break;
8045 if (abbrev_id >= abbrev_die_table_in_use)
8047 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8049 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8050 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8051 n_alloc);
8053 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8054 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8055 abbrev_die_table_allocated = n_alloc;
8058 ++abbrev_die_table_in_use;
8059 abbrev_die_table[abbrev_id] = die;
8062 die->die_abbrev = abbrev_id;
8063 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8066 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8068 static int
8069 constant_size (unsigned HOST_WIDE_INT value)
8071 int log;
8073 if (value == 0)
8074 log = 0;
8075 else
8076 log = floor_log2 (value);
8078 log = log / 8;
8079 log = 1 << (floor_log2 (log) + 1);
8081 return log;
8084 /* Return the size of a DIE as it is represented in the
8085 .debug_info section. */
8087 static unsigned long
8088 size_of_die (dw_die_ref die)
8090 unsigned long size = 0;
8091 dw_attr_ref a;
8092 unsigned ix;
8093 enum dwarf_form form;
8095 size += size_of_uleb128 (die->die_abbrev);
8096 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8098 switch (AT_class (a))
8100 case dw_val_class_addr:
8101 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8103 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8104 size += size_of_uleb128 (AT_index (a));
8106 else
8107 size += DWARF2_ADDR_SIZE;
8108 break;
8109 case dw_val_class_offset:
8110 size += DWARF_OFFSET_SIZE;
8111 break;
8112 case dw_val_class_loc:
8114 unsigned long lsize = size_of_locs (AT_loc (a));
8116 /* Block length. */
8117 if (dwarf_version >= 4)
8118 size += size_of_uleb128 (lsize);
8119 else
8120 size += constant_size (lsize);
8121 size += lsize;
8123 break;
8124 case dw_val_class_loc_list:
8125 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8127 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8128 size += size_of_uleb128 (AT_index (a));
8130 else
8131 size += DWARF_OFFSET_SIZE;
8132 break;
8133 case dw_val_class_range_list:
8134 size += DWARF_OFFSET_SIZE;
8135 break;
8136 case dw_val_class_const:
8137 size += size_of_sleb128 (AT_int (a));
8138 break;
8139 case dw_val_class_unsigned_const:
8141 int csize = constant_size (AT_unsigned (a));
8142 if (dwarf_version == 3
8143 && a->dw_attr == DW_AT_data_member_location
8144 && csize >= 4)
8145 size += size_of_uleb128 (AT_unsigned (a));
8146 else
8147 size += csize;
8149 break;
8150 case dw_val_class_const_double:
8151 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8152 if (HOST_BITS_PER_WIDE_INT >= 64)
8153 size++; /* block */
8154 break;
8155 case dw_val_class_wide_int:
8156 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8157 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8158 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8159 > 64)
8160 size++; /* block */
8161 break;
8162 case dw_val_class_vec:
8163 size += constant_size (a->dw_attr_val.v.val_vec.length
8164 * a->dw_attr_val.v.val_vec.elt_size)
8165 + a->dw_attr_val.v.val_vec.length
8166 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8167 break;
8168 case dw_val_class_flag:
8169 if (dwarf_version >= 4)
8170 /* Currently all add_AT_flag calls pass in 1 as last argument,
8171 so DW_FORM_flag_present can be used. If that ever changes,
8172 we'll need to use DW_FORM_flag and have some optimization
8173 in build_abbrev_table that will change those to
8174 DW_FORM_flag_present if it is set to 1 in all DIEs using
8175 the same abbrev entry. */
8176 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8177 else
8178 size += 1;
8179 break;
8180 case dw_val_class_die_ref:
8181 if (AT_ref_external (a))
8183 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8184 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8185 is sized by target address length, whereas in DWARF3
8186 it's always sized as an offset. */
8187 if (use_debug_types)
8188 size += DWARF_TYPE_SIGNATURE_SIZE;
8189 else if (dwarf_version == 2)
8190 size += DWARF2_ADDR_SIZE;
8191 else
8192 size += DWARF_OFFSET_SIZE;
8194 else
8195 size += DWARF_OFFSET_SIZE;
8196 break;
8197 case dw_val_class_fde_ref:
8198 size += DWARF_OFFSET_SIZE;
8199 break;
8200 case dw_val_class_lbl_id:
8201 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8203 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8204 size += size_of_uleb128 (AT_index (a));
8206 else
8207 size += DWARF2_ADDR_SIZE;
8208 break;
8209 case dw_val_class_lineptr:
8210 case dw_val_class_macptr:
8211 size += DWARF_OFFSET_SIZE;
8212 break;
8213 case dw_val_class_str:
8214 form = AT_string_form (a);
8215 if (form == DW_FORM_strp)
8216 size += DWARF_OFFSET_SIZE;
8217 else if (form == DW_FORM_GNU_str_index)
8218 size += size_of_uleb128 (AT_index (a));
8219 else
8220 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8221 break;
8222 case dw_val_class_file:
8223 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8224 break;
8225 case dw_val_class_data8:
8226 size += 8;
8227 break;
8228 case dw_val_class_vms_delta:
8229 size += DWARF_OFFSET_SIZE;
8230 break;
8231 case dw_val_class_high_pc:
8232 size += DWARF2_ADDR_SIZE;
8233 break;
8234 default:
8235 gcc_unreachable ();
8239 return size;
8242 /* Size the debugging information associated with a given DIE. Visits the
8243 DIE's children recursively. Updates the global variable next_die_offset, on
8244 each time through. Uses the current value of next_die_offset to update the
8245 die_offset field in each DIE. */
8247 static void
8248 calc_die_sizes (dw_die_ref die)
8250 dw_die_ref c;
8252 gcc_assert (die->die_offset == 0
8253 || (unsigned long int) die->die_offset == next_die_offset);
8254 die->die_offset = next_die_offset;
8255 next_die_offset += size_of_die (die);
8257 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8259 if (die->die_child != NULL)
8260 /* Count the null byte used to terminate sibling lists. */
8261 next_die_offset += 1;
8264 /* Size just the base type children at the start of the CU.
8265 This is needed because build_abbrev needs to size locs
8266 and sizing of type based stack ops needs to know die_offset
8267 values for the base types. */
8269 static void
8270 calc_base_type_die_sizes (void)
8272 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8273 unsigned int i;
8274 dw_die_ref base_type;
8275 #if ENABLE_ASSERT_CHECKING
8276 dw_die_ref prev = comp_unit_die ()->die_child;
8277 #endif
8279 die_offset += size_of_die (comp_unit_die ());
8280 for (i = 0; base_types.iterate (i, &base_type); i++)
8282 #if ENABLE_ASSERT_CHECKING
8283 gcc_assert (base_type->die_offset == 0
8284 && prev->die_sib == base_type
8285 && base_type->die_child == NULL
8286 && base_type->die_abbrev);
8287 prev = base_type;
8288 #endif
8289 base_type->die_offset = die_offset;
8290 die_offset += size_of_die (base_type);
8294 /* Set the marks for a die and its children. We do this so
8295 that we know whether or not a reference needs to use FORM_ref_addr; only
8296 DIEs in the same CU will be marked. We used to clear out the offset
8297 and use that as the flag, but ran into ordering problems. */
8299 static void
8300 mark_dies (dw_die_ref die)
8302 dw_die_ref c;
8304 gcc_assert (!die->die_mark);
8306 die->die_mark = 1;
8307 FOR_EACH_CHILD (die, c, mark_dies (c));
8310 /* Clear the marks for a die and its children. */
8312 static void
8313 unmark_dies (dw_die_ref die)
8315 dw_die_ref c;
8317 if (! use_debug_types)
8318 gcc_assert (die->die_mark);
8320 die->die_mark = 0;
8321 FOR_EACH_CHILD (die, c, unmark_dies (c));
8324 /* Clear the marks for a die, its children and referred dies. */
8326 static void
8327 unmark_all_dies (dw_die_ref die)
8329 dw_die_ref c;
8330 dw_attr_ref a;
8331 unsigned ix;
8333 if (!die->die_mark)
8334 return;
8335 die->die_mark = 0;
8337 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8339 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8340 if (AT_class (a) == dw_val_class_die_ref)
8341 unmark_all_dies (AT_ref (a));
8344 /* Calculate if the entry should appear in the final output file. It may be
8345 from a pruned a type. */
8347 static bool
8348 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8350 /* By limiting gnu pubnames to definitions only, gold can generate a
8351 gdb index without entries for declarations, which don't include
8352 enough information to be useful. */
8353 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8354 return false;
8356 if (table == pubname_table)
8358 /* Enumerator names are part of the pubname table, but the
8359 parent DW_TAG_enumeration_type die may have been pruned.
8360 Don't output them if that is the case. */
8361 if (p->die->die_tag == DW_TAG_enumerator &&
8362 (p->die->die_parent == NULL
8363 || !p->die->die_parent->die_perennial_p))
8364 return false;
8366 /* Everything else in the pubname table is included. */
8367 return true;
8370 /* The pubtypes table shouldn't include types that have been
8371 pruned. */
8372 return (p->die->die_offset != 0
8373 || !flag_eliminate_unused_debug_types);
8376 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8377 generated for the compilation unit. */
8379 static unsigned long
8380 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8382 unsigned long size;
8383 unsigned i;
8384 pubname_ref p;
8385 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8387 size = DWARF_PUBNAMES_HEADER_SIZE;
8388 FOR_EACH_VEC_ELT (*names, i, p)
8389 if (include_pubname_in_output (names, p))
8390 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8392 size += DWARF_OFFSET_SIZE;
8393 return size;
8396 /* Return the size of the information in the .debug_aranges section. */
8398 static unsigned long
8399 size_of_aranges (void)
8401 unsigned long size;
8403 size = DWARF_ARANGES_HEADER_SIZE;
8405 /* Count the address/length pair for this compilation unit. */
8406 if (text_section_used)
8407 size += 2 * DWARF2_ADDR_SIZE;
8408 if (cold_text_section_used)
8409 size += 2 * DWARF2_ADDR_SIZE;
8410 if (have_multiple_function_sections)
8412 unsigned fde_idx;
8413 dw_fde_ref fde;
8415 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8417 if (DECL_IGNORED_P (fde->decl))
8418 continue;
8419 if (!fde->in_std_section)
8420 size += 2 * DWARF2_ADDR_SIZE;
8421 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8422 size += 2 * DWARF2_ADDR_SIZE;
8426 /* Count the two zero words used to terminated the address range table. */
8427 size += 2 * DWARF2_ADDR_SIZE;
8428 return size;
8431 /* Select the encoding of an attribute value. */
8433 static enum dwarf_form
8434 value_format (dw_attr_ref a)
8436 switch (AT_class (a))
8438 case dw_val_class_addr:
8439 /* Only very few attributes allow DW_FORM_addr. */
8440 switch (a->dw_attr)
8442 case DW_AT_low_pc:
8443 case DW_AT_high_pc:
8444 case DW_AT_entry_pc:
8445 case DW_AT_trampoline:
8446 return (AT_index (a) == NOT_INDEXED
8447 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8448 default:
8449 break;
8451 switch (DWARF2_ADDR_SIZE)
8453 case 1:
8454 return DW_FORM_data1;
8455 case 2:
8456 return DW_FORM_data2;
8457 case 4:
8458 return DW_FORM_data4;
8459 case 8:
8460 return DW_FORM_data8;
8461 default:
8462 gcc_unreachable ();
8464 case dw_val_class_range_list:
8465 case dw_val_class_loc_list:
8466 if (dwarf_version >= 4)
8467 return DW_FORM_sec_offset;
8468 /* FALLTHRU */
8469 case dw_val_class_vms_delta:
8470 case dw_val_class_offset:
8471 switch (DWARF_OFFSET_SIZE)
8473 case 4:
8474 return DW_FORM_data4;
8475 case 8:
8476 return DW_FORM_data8;
8477 default:
8478 gcc_unreachable ();
8480 case dw_val_class_loc:
8481 if (dwarf_version >= 4)
8482 return DW_FORM_exprloc;
8483 switch (constant_size (size_of_locs (AT_loc (a))))
8485 case 1:
8486 return DW_FORM_block1;
8487 case 2:
8488 return DW_FORM_block2;
8489 case 4:
8490 return DW_FORM_block4;
8491 default:
8492 gcc_unreachable ();
8494 case dw_val_class_const:
8495 return DW_FORM_sdata;
8496 case dw_val_class_unsigned_const:
8497 switch (constant_size (AT_unsigned (a)))
8499 case 1:
8500 return DW_FORM_data1;
8501 case 2:
8502 return DW_FORM_data2;
8503 case 4:
8504 /* In DWARF3 DW_AT_data_member_location with
8505 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8506 constant, so we need to use DW_FORM_udata if we need
8507 a large constant. */
8508 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8509 return DW_FORM_udata;
8510 return DW_FORM_data4;
8511 case 8:
8512 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8513 return DW_FORM_udata;
8514 return DW_FORM_data8;
8515 default:
8516 gcc_unreachable ();
8518 case dw_val_class_const_double:
8519 switch (HOST_BITS_PER_WIDE_INT)
8521 case 8:
8522 return DW_FORM_data2;
8523 case 16:
8524 return DW_FORM_data4;
8525 case 32:
8526 return DW_FORM_data8;
8527 case 64:
8528 default:
8529 return DW_FORM_block1;
8531 case dw_val_class_wide_int:
8532 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8534 case 8:
8535 return DW_FORM_data1;
8536 case 16:
8537 return DW_FORM_data2;
8538 case 32:
8539 return DW_FORM_data4;
8540 case 64:
8541 return DW_FORM_data8;
8542 default:
8543 return DW_FORM_block1;
8545 case dw_val_class_vec:
8546 switch (constant_size (a->dw_attr_val.v.val_vec.length
8547 * a->dw_attr_val.v.val_vec.elt_size))
8549 case 1:
8550 return DW_FORM_block1;
8551 case 2:
8552 return DW_FORM_block2;
8553 case 4:
8554 return DW_FORM_block4;
8555 default:
8556 gcc_unreachable ();
8558 case dw_val_class_flag:
8559 if (dwarf_version >= 4)
8561 /* Currently all add_AT_flag calls pass in 1 as last argument,
8562 so DW_FORM_flag_present can be used. If that ever changes,
8563 we'll need to use DW_FORM_flag and have some optimization
8564 in build_abbrev_table that will change those to
8565 DW_FORM_flag_present if it is set to 1 in all DIEs using
8566 the same abbrev entry. */
8567 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8568 return DW_FORM_flag_present;
8570 return DW_FORM_flag;
8571 case dw_val_class_die_ref:
8572 if (AT_ref_external (a))
8573 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8574 else
8575 return DW_FORM_ref;
8576 case dw_val_class_fde_ref:
8577 return DW_FORM_data;
8578 case dw_val_class_lbl_id:
8579 return (AT_index (a) == NOT_INDEXED
8580 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8581 case dw_val_class_lineptr:
8582 case dw_val_class_macptr:
8583 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8584 case dw_val_class_str:
8585 return AT_string_form (a);
8586 case dw_val_class_file:
8587 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8589 case 1:
8590 return DW_FORM_data1;
8591 case 2:
8592 return DW_FORM_data2;
8593 case 4:
8594 return DW_FORM_data4;
8595 default:
8596 gcc_unreachable ();
8599 case dw_val_class_data8:
8600 return DW_FORM_data8;
8602 case dw_val_class_high_pc:
8603 switch (DWARF2_ADDR_SIZE)
8605 case 1:
8606 return DW_FORM_data1;
8607 case 2:
8608 return DW_FORM_data2;
8609 case 4:
8610 return DW_FORM_data4;
8611 case 8:
8612 return DW_FORM_data8;
8613 default:
8614 gcc_unreachable ();
8617 default:
8618 gcc_unreachable ();
8622 /* Output the encoding of an attribute value. */
8624 static void
8625 output_value_format (dw_attr_ref a)
8627 enum dwarf_form form = value_format (a);
8629 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8632 /* Given a die and id, produce the appropriate abbreviations. */
8634 static void
8635 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8637 unsigned ix;
8638 dw_attr_ref a_attr;
8640 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8641 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8642 dwarf_tag_name (abbrev->die_tag));
8644 if (abbrev->die_child != NULL)
8645 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8646 else
8647 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8649 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8651 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8652 dwarf_attr_name (a_attr->dw_attr));
8653 output_value_format (a_attr);
8656 dw2_asm_output_data (1, 0, NULL);
8657 dw2_asm_output_data (1, 0, NULL);
8661 /* Output the .debug_abbrev section which defines the DIE abbreviation
8662 table. */
8664 static void
8665 output_abbrev_section (void)
8667 unsigned long abbrev_id;
8669 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8670 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8672 /* Terminate the table. */
8673 dw2_asm_output_data (1, 0, NULL);
8676 /* Output a symbol we can use to refer to this DIE from another CU. */
8678 static inline void
8679 output_die_symbol (dw_die_ref die)
8681 const char *sym = die->die_id.die_symbol;
8683 gcc_assert (!die->comdat_type_p);
8685 if (sym == 0)
8686 return;
8688 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8689 /* We make these global, not weak; if the target doesn't support
8690 .linkonce, it doesn't support combining the sections, so debugging
8691 will break. */
8692 targetm.asm_out.globalize_label (asm_out_file, sym);
8694 ASM_OUTPUT_LABEL (asm_out_file, sym);
8697 /* Return a new location list, given the begin and end range, and the
8698 expression. */
8700 static inline dw_loc_list_ref
8701 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8702 const char *section)
8704 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8706 retlist->begin = begin;
8707 retlist->begin_entry = NULL;
8708 retlist->end = end;
8709 retlist->expr = expr;
8710 retlist->section = section;
8712 return retlist;
8715 /* Generate a new internal symbol for this location list node, if it
8716 hasn't got one yet. */
8718 static inline void
8719 gen_llsym (dw_loc_list_ref list)
8721 gcc_assert (!list->ll_symbol);
8722 list->ll_symbol = gen_internal_sym ("LLST");
8725 /* Output the location list given to us. */
8727 static void
8728 output_loc_list (dw_loc_list_ref list_head)
8730 dw_loc_list_ref curr = list_head;
8732 if (list_head->emitted)
8733 return;
8734 list_head->emitted = true;
8736 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8738 /* Walk the location list, and output each range + expression. */
8739 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8741 unsigned long size;
8742 /* Don't output an entry that starts and ends at the same address. */
8743 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8744 continue;
8745 size = size_of_locs (curr->expr);
8746 /* If the expression is too large, drop it on the floor. We could
8747 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8748 in the expression, but >= 64KB expressions for a single value
8749 in a single range are unlikely very useful. */
8750 if (size > 0xffff)
8751 continue;
8752 if (dwarf_split_debug_info)
8754 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8755 "Location list start/length entry (%s)",
8756 list_head->ll_symbol);
8757 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8758 "Location list range start index (%s)",
8759 curr->begin);
8760 /* The length field is 4 bytes. If we ever need to support
8761 an 8-byte length, we can add a new DW_LLE code or fall back
8762 to DW_LLE_GNU_start_end_entry. */
8763 dw2_asm_output_delta (4, curr->end, curr->begin,
8764 "Location list range length (%s)",
8765 list_head->ll_symbol);
8767 else if (!have_multiple_function_sections)
8769 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8770 "Location list begin address (%s)",
8771 list_head->ll_symbol);
8772 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8773 "Location list end address (%s)",
8774 list_head->ll_symbol);
8776 else
8778 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8779 "Location list begin address (%s)",
8780 list_head->ll_symbol);
8781 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8782 "Location list end address (%s)",
8783 list_head->ll_symbol);
8786 /* Output the block length for this list of location operations. */
8787 gcc_assert (size <= 0xffff);
8788 dw2_asm_output_data (2, size, "%s", "Location expression size");
8790 output_loc_sequence (curr->expr, -1);
8793 if (dwarf_split_debug_info)
8794 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8795 "Location list terminator (%s)",
8796 list_head->ll_symbol);
8797 else
8799 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8800 "Location list terminator begin (%s)",
8801 list_head->ll_symbol);
8802 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8803 "Location list terminator end (%s)",
8804 list_head->ll_symbol);
8808 /* Output a range_list offset into the debug_range section. Emit a
8809 relocated reference if val_entry is NULL, otherwise, emit an
8810 indirect reference. */
8812 static void
8813 output_range_list_offset (dw_attr_ref a)
8815 const char *name = dwarf_attr_name (a->dw_attr);
8817 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8819 char *p = strchr (ranges_section_label, '\0');
8820 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8821 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8822 debug_ranges_section, "%s", name);
8823 *p = '\0';
8825 else
8826 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8827 "%s (offset from %s)", name, ranges_section_label);
8830 /* Output the offset into the debug_loc section. */
8832 static void
8833 output_loc_list_offset (dw_attr_ref a)
8835 char *sym = AT_loc_list (a)->ll_symbol;
8837 gcc_assert (sym);
8838 if (dwarf_split_debug_info)
8839 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8840 "%s", dwarf_attr_name (a->dw_attr));
8841 else
8842 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8843 "%s", dwarf_attr_name (a->dw_attr));
8846 /* Output an attribute's index or value appropriately. */
8848 static void
8849 output_attr_index_or_value (dw_attr_ref a)
8851 const char *name = dwarf_attr_name (a->dw_attr);
8853 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8855 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8856 return;
8858 switch (AT_class (a))
8860 case dw_val_class_addr:
8861 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8862 break;
8863 case dw_val_class_high_pc:
8864 case dw_val_class_lbl_id:
8865 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8866 break;
8867 case dw_val_class_loc_list:
8868 output_loc_list_offset (a);
8869 break;
8870 default:
8871 gcc_unreachable ();
8875 /* Output a type signature. */
8877 static inline void
8878 output_signature (const char *sig, const char *name)
8880 int i;
8882 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8883 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8886 /* Output the DIE and its attributes. Called recursively to generate
8887 the definitions of each child DIE. */
8889 static void
8890 output_die (dw_die_ref die)
8892 dw_attr_ref a;
8893 dw_die_ref c;
8894 unsigned long size;
8895 unsigned ix;
8897 /* If someone in another CU might refer to us, set up a symbol for
8898 them to point to. */
8899 if (! die->comdat_type_p && die->die_id.die_symbol)
8900 output_die_symbol (die);
8902 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8903 (unsigned long)die->die_offset,
8904 dwarf_tag_name (die->die_tag));
8906 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8908 const char *name = dwarf_attr_name (a->dw_attr);
8910 switch (AT_class (a))
8912 case dw_val_class_addr:
8913 output_attr_index_or_value (a);
8914 break;
8916 case dw_val_class_offset:
8917 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8918 "%s", name);
8919 break;
8921 case dw_val_class_range_list:
8922 output_range_list_offset (a);
8923 break;
8925 case dw_val_class_loc:
8926 size = size_of_locs (AT_loc (a));
8928 /* Output the block length for this list of location operations. */
8929 if (dwarf_version >= 4)
8930 dw2_asm_output_data_uleb128 (size, "%s", name);
8931 else
8932 dw2_asm_output_data (constant_size (size), size, "%s", name);
8934 output_loc_sequence (AT_loc (a), -1);
8935 break;
8937 case dw_val_class_const:
8938 /* ??? It would be slightly more efficient to use a scheme like is
8939 used for unsigned constants below, but gdb 4.x does not sign
8940 extend. Gdb 5.x does sign extend. */
8941 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8942 break;
8944 case dw_val_class_unsigned_const:
8946 int csize = constant_size (AT_unsigned (a));
8947 if (dwarf_version == 3
8948 && a->dw_attr == DW_AT_data_member_location
8949 && csize >= 4)
8950 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8951 else
8952 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8954 break;
8956 case dw_val_class_const_double:
8958 unsigned HOST_WIDE_INT first, second;
8960 if (HOST_BITS_PER_WIDE_INT >= 64)
8961 dw2_asm_output_data (1,
8962 HOST_BITS_PER_DOUBLE_INT
8963 / HOST_BITS_PER_CHAR,
8964 NULL);
8966 if (WORDS_BIG_ENDIAN)
8968 first = a->dw_attr_val.v.val_double.high;
8969 second = a->dw_attr_val.v.val_double.low;
8971 else
8973 first = a->dw_attr_val.v.val_double.low;
8974 second = a->dw_attr_val.v.val_double.high;
8977 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8978 first, "%s", name);
8979 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8980 second, NULL);
8982 break;
8984 case dw_val_class_wide_int:
8986 int i;
8987 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8988 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8989 if (len * HOST_BITS_PER_WIDE_INT > 64)
8990 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8991 NULL);
8993 if (WORDS_BIG_ENDIAN)
8994 for (i = len - 1; i >= 0; --i)
8996 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8997 "%s", name);
8998 name = NULL;
9000 else
9001 for (i = 0; i < len; ++i)
9003 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9004 "%s", name);
9005 name = NULL;
9008 break;
9010 case dw_val_class_vec:
9012 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9013 unsigned int len = a->dw_attr_val.v.val_vec.length;
9014 unsigned int i;
9015 unsigned char *p;
9017 dw2_asm_output_data (constant_size (len * elt_size),
9018 len * elt_size, "%s", name);
9019 if (elt_size > sizeof (HOST_WIDE_INT))
9021 elt_size /= 2;
9022 len *= 2;
9024 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9025 i < len;
9026 i++, p += elt_size)
9027 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9028 "fp or vector constant word %u", i);
9029 break;
9032 case dw_val_class_flag:
9033 if (dwarf_version >= 4)
9035 /* Currently all add_AT_flag calls pass in 1 as last argument,
9036 so DW_FORM_flag_present can be used. If that ever changes,
9037 we'll need to use DW_FORM_flag and have some optimization
9038 in build_abbrev_table that will change those to
9039 DW_FORM_flag_present if it is set to 1 in all DIEs using
9040 the same abbrev entry. */
9041 gcc_assert (AT_flag (a) == 1);
9042 if (flag_debug_asm)
9043 fprintf (asm_out_file, "\t\t\t%s %s\n",
9044 ASM_COMMENT_START, name);
9045 break;
9047 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9048 break;
9050 case dw_val_class_loc_list:
9051 output_attr_index_or_value (a);
9052 break;
9054 case dw_val_class_die_ref:
9055 if (AT_ref_external (a))
9057 if (AT_ref (a)->comdat_type_p)
9059 comdat_type_node_ref type_node =
9060 AT_ref (a)->die_id.die_type_node;
9062 gcc_assert (type_node);
9063 output_signature (type_node->signature, name);
9065 else
9067 const char *sym = AT_ref (a)->die_id.die_symbol;
9068 int size;
9070 gcc_assert (sym);
9071 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9072 length, whereas in DWARF3 it's always sized as an
9073 offset. */
9074 if (dwarf_version == 2)
9075 size = DWARF2_ADDR_SIZE;
9076 else
9077 size = DWARF_OFFSET_SIZE;
9078 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9079 name);
9082 else
9084 gcc_assert (AT_ref (a)->die_offset);
9085 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9086 "%s", name);
9088 break;
9090 case dw_val_class_fde_ref:
9092 char l1[20];
9094 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9095 a->dw_attr_val.v.val_fde_index * 2);
9096 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9097 "%s", name);
9099 break;
9101 case dw_val_class_vms_delta:
9102 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9103 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9104 AT_vms_delta2 (a), AT_vms_delta1 (a),
9105 "%s", name);
9106 #else
9107 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9108 AT_vms_delta2 (a), AT_vms_delta1 (a),
9109 "%s", name);
9110 #endif
9111 break;
9113 case dw_val_class_lbl_id:
9114 output_attr_index_or_value (a);
9115 break;
9117 case dw_val_class_lineptr:
9118 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9119 debug_line_section, "%s", name);
9120 break;
9122 case dw_val_class_macptr:
9123 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9124 debug_macinfo_section, "%s", name);
9125 break;
9127 case dw_val_class_str:
9128 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9129 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9130 a->dw_attr_val.v.val_str->label,
9131 debug_str_section,
9132 "%s: \"%s\"", name, AT_string (a));
9133 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9134 dw2_asm_output_data_uleb128 (AT_index (a),
9135 "%s: \"%s\"", name, AT_string (a));
9136 else
9137 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9138 break;
9140 case dw_val_class_file:
9142 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9144 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9145 a->dw_attr_val.v.val_file->filename);
9146 break;
9149 case dw_val_class_data8:
9151 int i;
9153 for (i = 0; i < 8; i++)
9154 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9155 i == 0 ? "%s" : NULL, name);
9156 break;
9159 case dw_val_class_high_pc:
9160 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9161 get_AT_low_pc (die), "DW_AT_high_pc");
9162 break;
9164 default:
9165 gcc_unreachable ();
9169 FOR_EACH_CHILD (die, c, output_die (c));
9171 /* Add null byte to terminate sibling list. */
9172 if (die->die_child != NULL)
9173 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9174 (unsigned long) die->die_offset);
9177 /* Output the compilation unit that appears at the beginning of the
9178 .debug_info section, and precedes the DIE descriptions. */
9180 static void
9181 output_compilation_unit_header (void)
9183 /* We don't support actual DWARFv5 units yet, we just use some
9184 DWARFv5 draft DIE tags in DWARFv4 format. */
9185 int ver = dwarf_version < 5 ? dwarf_version : 4;
9187 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9188 dw2_asm_output_data (4, 0xffffffff,
9189 "Initial length escape value indicating 64-bit DWARF extension");
9190 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9191 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9192 "Length of Compilation Unit Info");
9193 dw2_asm_output_data (2, ver, "DWARF version number");
9194 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9195 debug_abbrev_section,
9196 "Offset Into Abbrev. Section");
9197 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9200 /* Output the compilation unit DIE and its children. */
9202 static void
9203 output_comp_unit (dw_die_ref die, int output_if_empty)
9205 const char *secname, *oldsym;
9206 char *tmp;
9208 /* Unless we are outputting main CU, we may throw away empty ones. */
9209 if (!output_if_empty && die->die_child == NULL)
9210 return;
9212 /* Even if there are no children of this DIE, we must output the information
9213 about the compilation unit. Otherwise, on an empty translation unit, we
9214 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9215 will then complain when examining the file. First mark all the DIEs in
9216 this CU so we know which get local refs. */
9217 mark_dies (die);
9219 external_ref_hash_type *extern_map = optimize_external_refs (die);
9221 build_abbrev_table (die, extern_map);
9223 delete extern_map;
9225 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9226 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9227 calc_die_sizes (die);
9229 oldsym = die->die_id.die_symbol;
9230 if (oldsym)
9232 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9234 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9235 secname = tmp;
9236 die->die_id.die_symbol = NULL;
9237 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9239 else
9241 switch_to_section (debug_info_section);
9242 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9243 info_section_emitted = true;
9246 /* Output debugging information. */
9247 output_compilation_unit_header ();
9248 output_die (die);
9250 /* Leave the marks on the main CU, so we can check them in
9251 output_pubnames. */
9252 if (oldsym)
9254 unmark_dies (die);
9255 die->die_id.die_symbol = oldsym;
9259 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9260 and .debug_pubtypes. This is configured per-target, but can be
9261 overridden by the -gpubnames or -gno-pubnames options. */
9263 static inline bool
9264 want_pubnames (void)
9266 if (debug_info_level <= DINFO_LEVEL_TERSE)
9267 return false;
9268 if (debug_generate_pub_sections != -1)
9269 return debug_generate_pub_sections;
9270 return targetm.want_debug_pub_sections;
9273 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9275 static void
9276 add_AT_pubnames (dw_die_ref die)
9278 if (want_pubnames ())
9279 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9282 /* Add a string attribute value to a skeleton DIE. */
9284 static inline void
9285 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9286 const char *str)
9288 dw_attr_node attr;
9289 struct indirect_string_node *node;
9291 if (! skeleton_debug_str_hash)
9292 skeleton_debug_str_hash
9293 = hash_table<indirect_string_hasher>::create_ggc (10);
9295 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9296 find_string_form (node);
9297 if (node->form == DW_FORM_GNU_str_index)
9298 node->form = DW_FORM_strp;
9300 attr.dw_attr = attr_kind;
9301 attr.dw_attr_val.val_class = dw_val_class_str;
9302 attr.dw_attr_val.val_entry = NULL;
9303 attr.dw_attr_val.v.val_str = node;
9304 add_dwarf_attr (die, &attr);
9307 /* Helper function to generate top-level dies for skeleton debug_info and
9308 debug_types. */
9310 static void
9311 add_top_level_skeleton_die_attrs (dw_die_ref die)
9313 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9314 const char *comp_dir = comp_dir_string ();
9316 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9317 if (comp_dir != NULL)
9318 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9319 add_AT_pubnames (die);
9320 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9323 /* Output skeleton debug sections that point to the dwo file. */
9325 static void
9326 output_skeleton_debug_sections (dw_die_ref comp_unit)
9328 /* We don't support actual DWARFv5 units yet, we just use some
9329 DWARFv5 draft DIE tags in DWARFv4 format. */
9330 int ver = dwarf_version < 5 ? dwarf_version : 4;
9332 /* These attributes will be found in the full debug_info section. */
9333 remove_AT (comp_unit, DW_AT_producer);
9334 remove_AT (comp_unit, DW_AT_language);
9336 switch_to_section (debug_skeleton_info_section);
9337 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9339 /* Produce the skeleton compilation-unit header. This one differs enough from
9340 a normal CU header that it's better not to call output_compilation_unit
9341 header. */
9342 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9343 dw2_asm_output_data (4, 0xffffffff,
9344 "Initial length escape value indicating 64-bit DWARF extension");
9346 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9347 DWARF_COMPILE_UNIT_HEADER_SIZE
9348 - DWARF_INITIAL_LENGTH_SIZE
9349 + size_of_die (comp_unit),
9350 "Length of Compilation Unit Info");
9351 dw2_asm_output_data (2, ver, "DWARF version number");
9352 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9353 debug_abbrev_section,
9354 "Offset Into Abbrev. Section");
9355 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9357 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9358 output_die (comp_unit);
9360 /* Build the skeleton debug_abbrev section. */
9361 switch_to_section (debug_skeleton_abbrev_section);
9362 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9364 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9366 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9369 /* Output a comdat type unit DIE and its children. */
9371 static void
9372 output_comdat_type_unit (comdat_type_node *node)
9374 const char *secname;
9375 char *tmp;
9376 int i;
9377 #if defined (OBJECT_FORMAT_ELF)
9378 tree comdat_key;
9379 #endif
9381 /* First mark all the DIEs in this CU so we know which get local refs. */
9382 mark_dies (node->root_die);
9384 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9386 build_abbrev_table (node->root_die, extern_map);
9388 delete extern_map;
9389 extern_map = NULL;
9391 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9392 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9393 calc_die_sizes (node->root_die);
9395 #if defined (OBJECT_FORMAT_ELF)
9396 if (!dwarf_split_debug_info)
9397 secname = ".debug_types";
9398 else
9399 secname = ".debug_types.dwo";
9401 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9402 sprintf (tmp, "wt.");
9403 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9404 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9405 comdat_key = get_identifier (tmp);
9406 targetm.asm_out.named_section (secname,
9407 SECTION_DEBUG | SECTION_LINKONCE,
9408 comdat_key);
9409 #else
9410 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9411 sprintf (tmp, ".gnu.linkonce.wt.");
9412 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9413 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9414 secname = tmp;
9415 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9416 #endif
9418 /* Output debugging information. */
9419 output_compilation_unit_header ();
9420 output_signature (node->signature, "Type Signature");
9421 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9422 "Offset to Type DIE");
9423 output_die (node->root_die);
9425 unmark_dies (node->root_die);
9428 /* Return the DWARF2/3 pubname associated with a decl. */
9430 static const char *
9431 dwarf2_name (tree decl, int scope)
9433 if (DECL_NAMELESS (decl))
9434 return NULL;
9435 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9438 /* Add a new entry to .debug_pubnames if appropriate. */
9440 static void
9441 add_pubname_string (const char *str, dw_die_ref die)
9443 pubname_entry e;
9445 e.die = die;
9446 e.name = xstrdup (str);
9447 vec_safe_push (pubname_table, e);
9450 static void
9451 add_pubname (tree decl, dw_die_ref die)
9453 if (!want_pubnames ())
9454 return;
9456 /* Don't add items to the table when we expect that the consumer will have
9457 just read the enclosing die. For example, if the consumer is looking at a
9458 class_member, it will either be inside the class already, or will have just
9459 looked up the class to find the member. Either way, searching the class is
9460 faster than searching the index. */
9461 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9462 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9464 const char *name = dwarf2_name (decl, 1);
9466 if (name)
9467 add_pubname_string (name, die);
9471 /* Add an enumerator to the pubnames section. */
9473 static void
9474 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9476 pubname_entry e;
9478 gcc_assert (scope_name);
9479 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9480 e.die = die;
9481 vec_safe_push (pubname_table, e);
9484 /* Add a new entry to .debug_pubtypes if appropriate. */
9486 static void
9487 add_pubtype (tree decl, dw_die_ref die)
9489 pubname_entry e;
9491 if (!want_pubnames ())
9492 return;
9494 if ((TREE_PUBLIC (decl)
9495 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9496 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9498 tree scope = NULL;
9499 const char *scope_name = "";
9500 const char *sep = is_cxx () ? "::" : ".";
9501 const char *name;
9503 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9504 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9506 scope_name = lang_hooks.dwarf_name (scope, 1);
9507 if (scope_name != NULL && scope_name[0] != '\0')
9508 scope_name = concat (scope_name, sep, NULL);
9509 else
9510 scope_name = "";
9513 if (TYPE_P (decl))
9514 name = type_tag (decl);
9515 else
9516 name = lang_hooks.dwarf_name (decl, 1);
9518 /* If we don't have a name for the type, there's no point in adding
9519 it to the table. */
9520 if (name != NULL && name[0] != '\0')
9522 e.die = die;
9523 e.name = concat (scope_name, name, NULL);
9524 vec_safe_push (pubtype_table, e);
9527 /* Although it might be more consistent to add the pubinfo for the
9528 enumerators as their dies are created, they should only be added if the
9529 enum type meets the criteria above. So rather than re-check the parent
9530 enum type whenever an enumerator die is created, just output them all
9531 here. This isn't protected by the name conditional because anonymous
9532 enums don't have names. */
9533 if (die->die_tag == DW_TAG_enumeration_type)
9535 dw_die_ref c;
9537 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9542 /* Output a single entry in the pubnames table. */
9544 static void
9545 output_pubname (dw_offset die_offset, pubname_entry *entry)
9547 dw_die_ref die = entry->die;
9548 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9550 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9552 if (debug_generate_pub_sections == 2)
9554 /* This logic follows gdb's method for determining the value of the flag
9555 byte. */
9556 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9557 switch (die->die_tag)
9559 case DW_TAG_typedef:
9560 case DW_TAG_base_type:
9561 case DW_TAG_subrange_type:
9562 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9563 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9564 break;
9565 case DW_TAG_enumerator:
9566 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9567 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9568 if (!is_cxx () && !is_java ())
9569 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9570 break;
9571 case DW_TAG_subprogram:
9572 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9573 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9574 if (!is_ada ())
9575 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9576 break;
9577 case DW_TAG_constant:
9578 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9579 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9580 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9581 break;
9582 case DW_TAG_variable:
9583 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9584 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9585 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9586 break;
9587 case DW_TAG_namespace:
9588 case DW_TAG_imported_declaration:
9589 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9590 break;
9591 case DW_TAG_class_type:
9592 case DW_TAG_interface_type:
9593 case DW_TAG_structure_type:
9594 case DW_TAG_union_type:
9595 case DW_TAG_enumeration_type:
9596 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9597 if (!is_cxx () && !is_java ())
9598 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9599 break;
9600 default:
9601 /* An unusual tag. Leave the flag-byte empty. */
9602 break;
9604 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9605 "GDB-index flags");
9608 dw2_asm_output_nstring (entry->name, -1, "external name");
9612 /* Output the public names table used to speed up access to externally
9613 visible names; or the public types table used to find type definitions. */
9615 static void
9616 output_pubnames (vec<pubname_entry, va_gc> *names)
9618 unsigned i;
9619 unsigned long pubnames_length = size_of_pubnames (names);
9620 pubname_ref pub;
9622 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9623 dw2_asm_output_data (4, 0xffffffff,
9624 "Initial length escape value indicating 64-bit DWARF extension");
9625 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9627 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9628 dw2_asm_output_data (2, 2, "DWARF Version");
9630 if (dwarf_split_debug_info)
9631 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9632 debug_skeleton_info_section,
9633 "Offset of Compilation Unit Info");
9634 else
9635 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9636 debug_info_section,
9637 "Offset of Compilation Unit Info");
9638 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9639 "Compilation Unit Length");
9641 FOR_EACH_VEC_ELT (*names, i, pub)
9643 if (include_pubname_in_output (names, pub))
9645 dw_offset die_offset = pub->die->die_offset;
9647 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9648 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9649 gcc_assert (pub->die->die_mark);
9651 /* If we're putting types in their own .debug_types sections,
9652 the .debug_pubtypes table will still point to the compile
9653 unit (not the type unit), so we want to use the offset of
9654 the skeleton DIE (if there is one). */
9655 if (pub->die->comdat_type_p && names == pubtype_table)
9657 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9659 if (type_node != NULL)
9660 die_offset = (type_node->skeleton_die != NULL
9661 ? type_node->skeleton_die->die_offset
9662 : comp_unit_die ()->die_offset);
9665 output_pubname (die_offset, pub);
9669 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9672 /* Output public names and types tables if necessary. */
9674 static void
9675 output_pubtables (void)
9677 if (!want_pubnames () || !info_section_emitted)
9678 return;
9680 switch_to_section (debug_pubnames_section);
9681 output_pubnames (pubname_table);
9682 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9683 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9684 simply won't look for the section. */
9685 switch_to_section (debug_pubtypes_section);
9686 output_pubnames (pubtype_table);
9690 /* Output the information that goes into the .debug_aranges table.
9691 Namely, define the beginning and ending address range of the
9692 text section generated for this compilation unit. */
9694 static void
9695 output_aranges (unsigned long aranges_length)
9697 unsigned i;
9699 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9700 dw2_asm_output_data (4, 0xffffffff,
9701 "Initial length escape value indicating 64-bit DWARF extension");
9702 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9703 "Length of Address Ranges Info");
9704 /* Version number for aranges is still 2, even up to DWARF5. */
9705 dw2_asm_output_data (2, 2, "DWARF Version");
9706 if (dwarf_split_debug_info)
9707 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9708 debug_skeleton_info_section,
9709 "Offset of Compilation Unit Info");
9710 else
9711 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9712 debug_info_section,
9713 "Offset of Compilation Unit Info");
9714 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9715 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9717 /* We need to align to twice the pointer size here. */
9718 if (DWARF_ARANGES_PAD_SIZE)
9720 /* Pad using a 2 byte words so that padding is correct for any
9721 pointer size. */
9722 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9723 2 * DWARF2_ADDR_SIZE);
9724 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9725 dw2_asm_output_data (2, 0, NULL);
9728 /* It is necessary not to output these entries if the sections were
9729 not used; if the sections were not used, the length will be 0 and
9730 the address may end up as 0 if the section is discarded by ld
9731 --gc-sections, leaving an invalid (0, 0) entry that can be
9732 confused with the terminator. */
9733 if (text_section_used)
9735 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9736 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9737 text_section_label, "Length");
9739 if (cold_text_section_used)
9741 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9742 "Address");
9743 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9744 cold_text_section_label, "Length");
9747 if (have_multiple_function_sections)
9749 unsigned fde_idx;
9750 dw_fde_ref fde;
9752 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9754 if (DECL_IGNORED_P (fde->decl))
9755 continue;
9756 if (!fde->in_std_section)
9758 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9759 "Address");
9760 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9761 fde->dw_fde_begin, "Length");
9763 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9765 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9766 "Address");
9767 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9768 fde->dw_fde_second_begin, "Length");
9773 /* Output the terminator words. */
9774 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9775 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9778 /* Add a new entry to .debug_ranges. Return the offset at which it
9779 was placed. */
9781 static unsigned int
9782 add_ranges_num (int num)
9784 unsigned int in_use = ranges_table_in_use;
9786 if (in_use == ranges_table_allocated)
9788 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9789 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9790 ranges_table_allocated);
9791 memset (ranges_table + ranges_table_in_use, 0,
9792 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9795 ranges_table[in_use].num = num;
9796 ranges_table_in_use = in_use + 1;
9798 return in_use * 2 * DWARF2_ADDR_SIZE;
9801 /* Add a new entry to .debug_ranges corresponding to a block, or a
9802 range terminator if BLOCK is NULL. */
9804 static unsigned int
9805 add_ranges (const_tree block)
9807 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9810 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9811 When using dwarf_split_debug_info, address attributes in dies destined
9812 for the final executable should be direct references--setting the
9813 parameter force_direct ensures this behavior. */
9815 static void
9816 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9817 bool *added, bool force_direct)
9819 unsigned int in_use = ranges_by_label_in_use;
9820 unsigned int offset;
9822 if (in_use == ranges_by_label_allocated)
9824 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9825 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9826 ranges_by_label,
9827 ranges_by_label_allocated);
9828 memset (ranges_by_label + ranges_by_label_in_use, 0,
9829 RANGES_TABLE_INCREMENT
9830 * sizeof (struct dw_ranges_by_label_struct));
9833 ranges_by_label[in_use].begin = begin;
9834 ranges_by_label[in_use].end = end;
9835 ranges_by_label_in_use = in_use + 1;
9837 offset = add_ranges_num (-(int)in_use - 1);
9838 if (!*added)
9840 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9841 *added = true;
9845 static void
9846 output_ranges (void)
9848 unsigned i;
9849 static const char *const start_fmt = "Offset %#x";
9850 const char *fmt = start_fmt;
9852 for (i = 0; i < ranges_table_in_use; i++)
9854 int block_num = ranges_table[i].num;
9856 if (block_num > 0)
9858 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9859 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9861 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9862 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9864 /* If all code is in the text section, then the compilation
9865 unit base address defaults to DW_AT_low_pc, which is the
9866 base of the text section. */
9867 if (!have_multiple_function_sections)
9869 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9870 text_section_label,
9871 fmt, i * 2 * DWARF2_ADDR_SIZE);
9872 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9873 text_section_label, NULL);
9876 /* Otherwise, the compilation unit base address is zero,
9877 which allows us to use absolute addresses, and not worry
9878 about whether the target supports cross-section
9879 arithmetic. */
9880 else
9882 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9883 fmt, i * 2 * DWARF2_ADDR_SIZE);
9884 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9887 fmt = NULL;
9890 /* Negative block_num stands for an index into ranges_by_label. */
9891 else if (block_num < 0)
9893 int lab_idx = - block_num - 1;
9895 if (!have_multiple_function_sections)
9897 gcc_unreachable ();
9898 #if 0
9899 /* If we ever use add_ranges_by_labels () for a single
9900 function section, all we have to do is to take out
9901 the #if 0 above. */
9902 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9903 ranges_by_label[lab_idx].begin,
9904 text_section_label,
9905 fmt, i * 2 * DWARF2_ADDR_SIZE);
9906 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9907 ranges_by_label[lab_idx].end,
9908 text_section_label, NULL);
9909 #endif
9911 else
9913 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9914 ranges_by_label[lab_idx].begin,
9915 fmt, i * 2 * DWARF2_ADDR_SIZE);
9916 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9917 ranges_by_label[lab_idx].end,
9918 NULL);
9921 else
9923 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9924 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9925 fmt = start_fmt;
9930 /* Data structure containing information about input files. */
9931 struct file_info
9933 const char *path; /* Complete file name. */
9934 const char *fname; /* File name part. */
9935 int length; /* Length of entire string. */
9936 struct dwarf_file_data * file_idx; /* Index in input file table. */
9937 int dir_idx; /* Index in directory table. */
9940 /* Data structure containing information about directories with source
9941 files. */
9942 struct dir_info
9944 const char *path; /* Path including directory name. */
9945 int length; /* Path length. */
9946 int prefix; /* Index of directory entry which is a prefix. */
9947 int count; /* Number of files in this directory. */
9948 int dir_idx; /* Index of directory used as base. */
9951 /* Callback function for file_info comparison. We sort by looking at
9952 the directories in the path. */
9954 static int
9955 file_info_cmp (const void *p1, const void *p2)
9957 const struct file_info *const s1 = (const struct file_info *) p1;
9958 const struct file_info *const s2 = (const struct file_info *) p2;
9959 const unsigned char *cp1;
9960 const unsigned char *cp2;
9962 /* Take care of file names without directories. We need to make sure that
9963 we return consistent values to qsort since some will get confused if
9964 we return the same value when identical operands are passed in opposite
9965 orders. So if neither has a directory, return 0 and otherwise return
9966 1 or -1 depending on which one has the directory. */
9967 if ((s1->path == s1->fname || s2->path == s2->fname))
9968 return (s2->path == s2->fname) - (s1->path == s1->fname);
9970 cp1 = (const unsigned char *) s1->path;
9971 cp2 = (const unsigned char *) s2->path;
9973 while (1)
9975 ++cp1;
9976 ++cp2;
9977 /* Reached the end of the first path? If so, handle like above. */
9978 if ((cp1 == (const unsigned char *) s1->fname)
9979 || (cp2 == (const unsigned char *) s2->fname))
9980 return ((cp2 == (const unsigned char *) s2->fname)
9981 - (cp1 == (const unsigned char *) s1->fname));
9983 /* Character of current path component the same? */
9984 else if (*cp1 != *cp2)
9985 return *cp1 - *cp2;
9989 struct file_name_acquire_data
9991 struct file_info *files;
9992 int used_files;
9993 int max_files;
9996 /* Traversal function for the hash table. */
9999 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10001 struct dwarf_file_data *d = *slot;
10002 struct file_info *fi;
10003 const char *f;
10005 gcc_assert (fnad->max_files >= d->emitted_number);
10007 if (! d->emitted_number)
10008 return 1;
10010 gcc_assert (fnad->max_files != fnad->used_files);
10012 fi = fnad->files + fnad->used_files++;
10014 /* Skip all leading "./". */
10015 f = d->filename;
10016 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10017 f += 2;
10019 /* Create a new array entry. */
10020 fi->path = f;
10021 fi->length = strlen (f);
10022 fi->file_idx = d;
10024 /* Search for the file name part. */
10025 f = strrchr (f, DIR_SEPARATOR);
10026 #if defined (DIR_SEPARATOR_2)
10028 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10030 if (g != NULL)
10032 if (f == NULL || f < g)
10033 f = g;
10036 #endif
10038 fi->fname = f == NULL ? fi->path : f + 1;
10039 return 1;
10042 /* Output the directory table and the file name table. We try to minimize
10043 the total amount of memory needed. A heuristic is used to avoid large
10044 slowdowns with many input files. */
10046 static void
10047 output_file_names (void)
10049 struct file_name_acquire_data fnad;
10050 int numfiles;
10051 struct file_info *files;
10052 struct dir_info *dirs;
10053 int *saved;
10054 int *savehere;
10055 int *backmap;
10056 int ndirs;
10057 int idx_offset;
10058 int i;
10060 if (!last_emitted_file)
10062 dw2_asm_output_data (1, 0, "End directory table");
10063 dw2_asm_output_data (1, 0, "End file name table");
10064 return;
10067 numfiles = last_emitted_file->emitted_number;
10069 /* Allocate the various arrays we need. */
10070 files = XALLOCAVEC (struct file_info, numfiles);
10071 dirs = XALLOCAVEC (struct dir_info, numfiles);
10073 fnad.files = files;
10074 fnad.used_files = 0;
10075 fnad.max_files = numfiles;
10076 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10077 gcc_assert (fnad.used_files == fnad.max_files);
10079 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10081 /* Find all the different directories used. */
10082 dirs[0].path = files[0].path;
10083 dirs[0].length = files[0].fname - files[0].path;
10084 dirs[0].prefix = -1;
10085 dirs[0].count = 1;
10086 dirs[0].dir_idx = 0;
10087 files[0].dir_idx = 0;
10088 ndirs = 1;
10090 for (i = 1; i < numfiles; i++)
10091 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10092 && memcmp (dirs[ndirs - 1].path, files[i].path,
10093 dirs[ndirs - 1].length) == 0)
10095 /* Same directory as last entry. */
10096 files[i].dir_idx = ndirs - 1;
10097 ++dirs[ndirs - 1].count;
10099 else
10101 int j;
10103 /* This is a new directory. */
10104 dirs[ndirs].path = files[i].path;
10105 dirs[ndirs].length = files[i].fname - files[i].path;
10106 dirs[ndirs].count = 1;
10107 dirs[ndirs].dir_idx = ndirs;
10108 files[i].dir_idx = ndirs;
10110 /* Search for a prefix. */
10111 dirs[ndirs].prefix = -1;
10112 for (j = 0; j < ndirs; j++)
10113 if (dirs[j].length < dirs[ndirs].length
10114 && dirs[j].length > 1
10115 && (dirs[ndirs].prefix == -1
10116 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10117 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10118 dirs[ndirs].prefix = j;
10120 ++ndirs;
10123 /* Now to the actual work. We have to find a subset of the directories which
10124 allow expressing the file name using references to the directory table
10125 with the least amount of characters. We do not do an exhaustive search
10126 where we would have to check out every combination of every single
10127 possible prefix. Instead we use a heuristic which provides nearly optimal
10128 results in most cases and never is much off. */
10129 saved = XALLOCAVEC (int, ndirs);
10130 savehere = XALLOCAVEC (int, ndirs);
10132 memset (saved, '\0', ndirs * sizeof (saved[0]));
10133 for (i = 0; i < ndirs; i++)
10135 int j;
10136 int total;
10138 /* We can always save some space for the current directory. But this
10139 does not mean it will be enough to justify adding the directory. */
10140 savehere[i] = dirs[i].length;
10141 total = (savehere[i] - saved[i]) * dirs[i].count;
10143 for (j = i + 1; j < ndirs; j++)
10145 savehere[j] = 0;
10146 if (saved[j] < dirs[i].length)
10148 /* Determine whether the dirs[i] path is a prefix of the
10149 dirs[j] path. */
10150 int k;
10152 k = dirs[j].prefix;
10153 while (k != -1 && k != (int) i)
10154 k = dirs[k].prefix;
10156 if (k == (int) i)
10158 /* Yes it is. We can possibly save some memory by
10159 writing the filenames in dirs[j] relative to
10160 dirs[i]. */
10161 savehere[j] = dirs[i].length;
10162 total += (savehere[j] - saved[j]) * dirs[j].count;
10167 /* Check whether we can save enough to justify adding the dirs[i]
10168 directory. */
10169 if (total > dirs[i].length + 1)
10171 /* It's worthwhile adding. */
10172 for (j = i; j < ndirs; j++)
10173 if (savehere[j] > 0)
10175 /* Remember how much we saved for this directory so far. */
10176 saved[j] = savehere[j];
10178 /* Remember the prefix directory. */
10179 dirs[j].dir_idx = i;
10184 /* Emit the directory name table. */
10185 idx_offset = dirs[0].length > 0 ? 1 : 0;
10186 for (i = 1 - idx_offset; i < ndirs; i++)
10187 dw2_asm_output_nstring (dirs[i].path,
10188 dirs[i].length
10189 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10190 "Directory Entry: %#x", i + idx_offset);
10192 dw2_asm_output_data (1, 0, "End directory table");
10194 /* We have to emit them in the order of emitted_number since that's
10195 used in the debug info generation. To do this efficiently we
10196 generate a back-mapping of the indices first. */
10197 backmap = XALLOCAVEC (int, numfiles);
10198 for (i = 0; i < numfiles; i++)
10199 backmap[files[i].file_idx->emitted_number - 1] = i;
10201 /* Now write all the file names. */
10202 for (i = 0; i < numfiles; i++)
10204 int file_idx = backmap[i];
10205 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10207 #ifdef VMS_DEBUGGING_INFO
10208 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10210 /* Setting these fields can lead to debugger miscomparisons,
10211 but VMS Debug requires them to be set correctly. */
10213 int ver;
10214 long long cdt;
10215 long siz;
10216 int maxfilelen = strlen (files[file_idx].path)
10217 + dirs[dir_idx].length
10218 + MAX_VMS_VERSION_LEN + 1;
10219 char *filebuf = XALLOCAVEC (char, maxfilelen);
10221 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10222 snprintf (filebuf, maxfilelen, "%s;%d",
10223 files[file_idx].path + dirs[dir_idx].length, ver);
10225 dw2_asm_output_nstring
10226 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10228 /* Include directory index. */
10229 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10231 /* Modification time. */
10232 dw2_asm_output_data_uleb128
10233 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10234 ? cdt : 0,
10235 NULL);
10237 /* File length in bytes. */
10238 dw2_asm_output_data_uleb128
10239 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10240 ? siz : 0,
10241 NULL);
10242 #else
10243 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10244 "File Entry: %#x", (unsigned) i + 1);
10246 /* Include directory index. */
10247 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10249 /* Modification time. */
10250 dw2_asm_output_data_uleb128 (0, NULL);
10252 /* File length in bytes. */
10253 dw2_asm_output_data_uleb128 (0, NULL);
10254 #endif /* VMS_DEBUGGING_INFO */
10257 dw2_asm_output_data (1, 0, "End file name table");
10261 /* Output one line number table into the .debug_line section. */
10263 static void
10264 output_one_line_info_table (dw_line_info_table *table)
10266 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10267 unsigned int current_line = 1;
10268 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10269 dw_line_info_entry *ent;
10270 size_t i;
10272 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10274 switch (ent->opcode)
10276 case LI_set_address:
10277 /* ??? Unfortunately, we have little choice here currently, and
10278 must always use the most general form. GCC does not know the
10279 address delta itself, so we can't use DW_LNS_advance_pc. Many
10280 ports do have length attributes which will give an upper bound
10281 on the address range. We could perhaps use length attributes
10282 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10283 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10285 /* This can handle any delta. This takes
10286 4+DWARF2_ADDR_SIZE bytes. */
10287 dw2_asm_output_data (1, 0, "set address %s", line_label);
10288 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10289 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10290 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10291 break;
10293 case LI_set_line:
10294 if (ent->val == current_line)
10296 /* We still need to start a new row, so output a copy insn. */
10297 dw2_asm_output_data (1, DW_LNS_copy,
10298 "copy line %u", current_line);
10300 else
10302 int line_offset = ent->val - current_line;
10303 int line_delta = line_offset - DWARF_LINE_BASE;
10305 current_line = ent->val;
10306 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10308 /* This can handle deltas from -10 to 234, using the current
10309 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10310 This takes 1 byte. */
10311 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10312 "line %u", current_line);
10314 else
10316 /* This can handle any delta. This takes at least 4 bytes,
10317 depending on the value being encoded. */
10318 dw2_asm_output_data (1, DW_LNS_advance_line,
10319 "advance to line %u", current_line);
10320 dw2_asm_output_data_sleb128 (line_offset, NULL);
10321 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10324 break;
10326 case LI_set_file:
10327 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10328 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10329 break;
10331 case LI_set_column:
10332 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10333 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10334 break;
10336 case LI_negate_stmt:
10337 current_is_stmt = !current_is_stmt;
10338 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10339 "is_stmt %d", current_is_stmt);
10340 break;
10342 case LI_set_prologue_end:
10343 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10344 "set prologue end");
10345 break;
10347 case LI_set_epilogue_begin:
10348 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10349 "set epilogue begin");
10350 break;
10352 case LI_set_discriminator:
10353 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10354 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10355 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10356 dw2_asm_output_data_uleb128 (ent->val, NULL);
10357 break;
10361 /* Emit debug info for the address of the end of the table. */
10362 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10363 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10364 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10365 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10367 dw2_asm_output_data (1, 0, "end sequence");
10368 dw2_asm_output_data_uleb128 (1, NULL);
10369 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10372 /* Output the source line number correspondence information. This
10373 information goes into the .debug_line section. */
10375 static void
10376 output_line_info (bool prologue_only)
10378 char l1[20], l2[20], p1[20], p2[20];
10379 /* We don't support DWARFv5 line tables yet. */
10380 int ver = dwarf_version < 5 ? dwarf_version : 4;
10381 bool saw_one = false;
10382 int opc;
10384 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10385 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10386 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10387 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10389 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10390 dw2_asm_output_data (4, 0xffffffff,
10391 "Initial length escape value indicating 64-bit DWARF extension");
10392 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10393 "Length of Source Line Info");
10394 ASM_OUTPUT_LABEL (asm_out_file, l1);
10396 dw2_asm_output_data (2, ver, "DWARF Version");
10397 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10398 ASM_OUTPUT_LABEL (asm_out_file, p1);
10400 /* Define the architecture-dependent minimum instruction length (in bytes).
10401 In this implementation of DWARF, this field is used for information
10402 purposes only. Since GCC generates assembly language, we have no
10403 a priori knowledge of how many instruction bytes are generated for each
10404 source line, and therefore can use only the DW_LNE_set_address and
10405 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10406 this as '1', which is "correct enough" for all architectures,
10407 and don't let the target override. */
10408 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10410 if (ver >= 4)
10411 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10412 "Maximum Operations Per Instruction");
10413 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10414 "Default is_stmt_start flag");
10415 dw2_asm_output_data (1, DWARF_LINE_BASE,
10416 "Line Base Value (Special Opcodes)");
10417 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10418 "Line Range Value (Special Opcodes)");
10419 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10420 "Special Opcode Base");
10422 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10424 int n_op_args;
10425 switch (opc)
10427 case DW_LNS_advance_pc:
10428 case DW_LNS_advance_line:
10429 case DW_LNS_set_file:
10430 case DW_LNS_set_column:
10431 case DW_LNS_fixed_advance_pc:
10432 case DW_LNS_set_isa:
10433 n_op_args = 1;
10434 break;
10435 default:
10436 n_op_args = 0;
10437 break;
10440 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10441 opc, n_op_args);
10444 /* Write out the information about the files we use. */
10445 output_file_names ();
10446 ASM_OUTPUT_LABEL (asm_out_file, p2);
10447 if (prologue_only)
10449 /* Output the marker for the end of the line number info. */
10450 ASM_OUTPUT_LABEL (asm_out_file, l2);
10451 return;
10454 if (separate_line_info)
10456 dw_line_info_table *table;
10457 size_t i;
10459 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10460 if (table->in_use)
10462 output_one_line_info_table (table);
10463 saw_one = true;
10466 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10468 output_one_line_info_table (cold_text_section_line_info);
10469 saw_one = true;
10472 /* ??? Some Darwin linkers crash on a .debug_line section with no
10473 sequences. Further, merely a DW_LNE_end_sequence entry is not
10474 sufficient -- the address column must also be initialized.
10475 Make sure to output at least one set_address/end_sequence pair,
10476 choosing .text since that section is always present. */
10477 if (text_section_line_info->in_use || !saw_one)
10478 output_one_line_info_table (text_section_line_info);
10480 /* Output the marker for the end of the line number info. */
10481 ASM_OUTPUT_LABEL (asm_out_file, l2);
10484 /* Given a pointer to a tree node for some base type, return a pointer to
10485 a DIE that describes the given type.
10487 This routine must only be called for GCC type nodes that correspond to
10488 Dwarf base (fundamental) types. */
10490 static dw_die_ref
10491 base_type_die (tree type)
10493 dw_die_ref base_type_result;
10494 enum dwarf_type encoding;
10496 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10497 return 0;
10499 /* If this is a subtype that should not be emitted as a subrange type,
10500 use the base type. See subrange_type_for_debug_p. */
10501 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10502 type = TREE_TYPE (type);
10504 switch (TREE_CODE (type))
10506 case INTEGER_TYPE:
10507 if ((dwarf_version >= 4 || !dwarf_strict)
10508 && TYPE_NAME (type)
10509 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10510 && DECL_IS_BUILTIN (TYPE_NAME (type))
10511 && DECL_NAME (TYPE_NAME (type)))
10513 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10514 if (strcmp (name, "char16_t") == 0
10515 || strcmp (name, "char32_t") == 0)
10517 encoding = DW_ATE_UTF;
10518 break;
10521 if (TYPE_STRING_FLAG (type))
10523 if (TYPE_UNSIGNED (type))
10524 encoding = DW_ATE_unsigned_char;
10525 else
10526 encoding = DW_ATE_signed_char;
10528 else if (TYPE_UNSIGNED (type))
10529 encoding = DW_ATE_unsigned;
10530 else
10531 encoding = DW_ATE_signed;
10532 break;
10534 case REAL_TYPE:
10535 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10537 if (dwarf_version >= 3 || !dwarf_strict)
10538 encoding = DW_ATE_decimal_float;
10539 else
10540 encoding = DW_ATE_lo_user;
10542 else
10543 encoding = DW_ATE_float;
10544 break;
10546 case FIXED_POINT_TYPE:
10547 if (!(dwarf_version >= 3 || !dwarf_strict))
10548 encoding = DW_ATE_lo_user;
10549 else if (TYPE_UNSIGNED (type))
10550 encoding = DW_ATE_unsigned_fixed;
10551 else
10552 encoding = DW_ATE_signed_fixed;
10553 break;
10555 /* Dwarf2 doesn't know anything about complex ints, so use
10556 a user defined type for it. */
10557 case COMPLEX_TYPE:
10558 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10559 encoding = DW_ATE_complex_float;
10560 else
10561 encoding = DW_ATE_lo_user;
10562 break;
10564 case BOOLEAN_TYPE:
10565 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10566 encoding = DW_ATE_boolean;
10567 break;
10569 default:
10570 /* No other TREE_CODEs are Dwarf fundamental types. */
10571 gcc_unreachable ();
10574 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10576 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10577 int_size_in_bytes (type));
10578 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10579 add_pubtype (type, base_type_result);
10581 return base_type_result;
10584 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10585 named 'auto' in its type: return true for it, false otherwise. */
10587 static inline bool
10588 is_cxx_auto (tree type)
10590 if (is_cxx ())
10592 tree name = TYPE_IDENTIFIER (type);
10593 if (name == get_identifier ("auto")
10594 || name == get_identifier ("decltype(auto)"))
10595 return true;
10597 return false;
10600 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10601 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10603 static inline int
10604 is_base_type (tree type)
10606 switch (TREE_CODE (type))
10608 case ERROR_MARK:
10609 case VOID_TYPE:
10610 case INTEGER_TYPE:
10611 case REAL_TYPE:
10612 case FIXED_POINT_TYPE:
10613 case COMPLEX_TYPE:
10614 case BOOLEAN_TYPE:
10615 case POINTER_BOUNDS_TYPE:
10616 return 1;
10618 case ARRAY_TYPE:
10619 case RECORD_TYPE:
10620 case UNION_TYPE:
10621 case QUAL_UNION_TYPE:
10622 case ENUMERAL_TYPE:
10623 case FUNCTION_TYPE:
10624 case METHOD_TYPE:
10625 case POINTER_TYPE:
10626 case REFERENCE_TYPE:
10627 case NULLPTR_TYPE:
10628 case OFFSET_TYPE:
10629 case LANG_TYPE:
10630 case VECTOR_TYPE:
10631 return 0;
10633 default:
10634 if (is_cxx_auto (type))
10635 return 0;
10636 gcc_unreachable ();
10639 return 0;
10642 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10643 node, return the size in bits for the type if it is a constant, or else
10644 return the alignment for the type if the type's size is not constant, or
10645 else return BITS_PER_WORD if the type actually turns out to be an
10646 ERROR_MARK node. */
10648 static inline unsigned HOST_WIDE_INT
10649 simple_type_size_in_bits (const_tree type)
10651 if (TREE_CODE (type) == ERROR_MARK)
10652 return BITS_PER_WORD;
10653 else if (TYPE_SIZE (type) == NULL_TREE)
10654 return 0;
10655 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10656 return tree_to_uhwi (TYPE_SIZE (type));
10657 else
10658 return TYPE_ALIGN (type);
10661 /* Similarly, but return an offset_int instead of UHWI. */
10663 static inline offset_int
10664 offset_int_type_size_in_bits (const_tree type)
10666 if (TREE_CODE (type) == ERROR_MARK)
10667 return BITS_PER_WORD;
10668 else if (TYPE_SIZE (type) == NULL_TREE)
10669 return 0;
10670 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10671 return wi::to_offset (TYPE_SIZE (type));
10672 else
10673 return TYPE_ALIGN (type);
10676 /* Given a pointer to a tree node for a subrange type, return a pointer
10677 to a DIE that describes the given type. */
10679 static dw_die_ref
10680 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10682 dw_die_ref subrange_die;
10683 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10685 if (context_die == NULL)
10686 context_die = comp_unit_die ();
10688 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10690 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10692 /* The size of the subrange type and its base type do not match,
10693 so we need to generate a size attribute for the subrange type. */
10694 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10697 if (low)
10698 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10699 if (high)
10700 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10702 return subrange_die;
10705 /* Returns the (const and/or volatile) cv_qualifiers associated with
10706 the decl node. This will normally be augmented with the
10707 cv_qualifiers of the underlying type in add_type_attribute. */
10709 static int
10710 decl_quals (const_tree decl)
10712 return ((TREE_READONLY (decl)
10713 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10714 | (TREE_THIS_VOLATILE (decl)
10715 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10718 /* Determine the TYPE whose qualifiers match the largest strict subset
10719 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10720 qualifiers outside QUAL_MASK. */
10722 static int
10723 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10725 tree t;
10726 int best_rank = 0, best_qual = 0, max_rank;
10728 type_quals &= qual_mask;
10729 max_rank = popcount_hwi (type_quals) - 1;
10731 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10732 t = TYPE_NEXT_VARIANT (t))
10734 int q = TYPE_QUALS (t) & qual_mask;
10736 if ((q & type_quals) == q && q != type_quals
10737 && check_base_type (t, type))
10739 int rank = popcount_hwi (q);
10741 if (rank > best_rank)
10743 best_rank = rank;
10744 best_qual = q;
10749 return best_qual;
10752 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10753 entry that chains various modifiers in front of the given type. */
10755 static dw_die_ref
10756 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10758 enum tree_code code = TREE_CODE (type);
10759 dw_die_ref mod_type_die;
10760 dw_die_ref sub_die = NULL;
10761 tree item_type = NULL;
10762 tree qualified_type;
10763 tree name, low, high;
10764 dw_die_ref mod_scope;
10765 /* Only these cv-qualifiers are currently handled. */
10766 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10767 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10769 if (code == ERROR_MARK)
10770 return NULL;
10772 cv_quals &= cv_qual_mask;
10774 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10775 tag modifier (and not an attribute) old consumers won't be able
10776 to handle it. */
10777 if (dwarf_version < 3)
10778 cv_quals &= ~TYPE_QUAL_RESTRICT;
10780 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10781 if (dwarf_version < 5)
10782 cv_quals &= ~TYPE_QUAL_ATOMIC;
10784 /* See if we already have the appropriately qualified variant of
10785 this type. */
10786 qualified_type = get_qualified_type (type, cv_quals);
10788 if (qualified_type == sizetype
10789 && TYPE_NAME (qualified_type)
10790 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10792 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10794 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10795 && TYPE_PRECISION (t)
10796 == TYPE_PRECISION (qualified_type)
10797 && TYPE_UNSIGNED (t)
10798 == TYPE_UNSIGNED (qualified_type));
10799 qualified_type = t;
10802 /* If we do, then we can just use its DIE, if it exists. */
10803 if (qualified_type)
10805 mod_type_die = lookup_type_die (qualified_type);
10806 if (mod_type_die)
10807 return mod_type_die;
10810 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10812 /* Handle C typedef types. */
10813 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10814 && !DECL_ARTIFICIAL (name))
10816 tree dtype = TREE_TYPE (name);
10818 if (qualified_type == dtype)
10820 /* For a named type, use the typedef. */
10821 gen_type_die (qualified_type, context_die);
10822 return lookup_type_die (qualified_type);
10824 else
10826 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10827 dquals &= cv_qual_mask;
10828 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10829 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10830 /* cv-unqualified version of named type. Just use
10831 the unnamed type to which it refers. */
10832 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10833 cv_quals, context_die);
10834 /* Else cv-qualified version of named type; fall through. */
10838 mod_scope = scope_die_for (type, context_die);
10840 if (cv_quals)
10842 struct qual_info { int q; enum dwarf_tag t; };
10843 static const struct qual_info qual_info[] =
10845 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10846 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10847 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10848 { TYPE_QUAL_CONST, DW_TAG_const_type },
10850 int sub_quals;
10851 unsigned i;
10853 /* Determine a lesser qualified type that most closely matches
10854 this one. Then generate DW_TAG_* entries for the remaining
10855 qualifiers. */
10856 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10857 cv_qual_mask);
10858 mod_type_die = modified_type_die (type, sub_quals, context_die);
10860 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10861 if (qual_info[i].q & cv_quals & ~sub_quals)
10863 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10864 if (mod_type_die)
10865 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10866 mod_type_die = d;
10869 else if (code == POINTER_TYPE)
10871 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10872 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10873 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10874 item_type = TREE_TYPE (type);
10875 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10876 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10877 TYPE_ADDR_SPACE (item_type));
10879 else if (code == REFERENCE_TYPE)
10881 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10882 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10883 type);
10884 else
10885 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10886 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10887 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10888 item_type = TREE_TYPE (type);
10889 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10890 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10891 TYPE_ADDR_SPACE (item_type));
10893 else if (code == INTEGER_TYPE
10894 && TREE_TYPE (type) != NULL_TREE
10895 && subrange_type_for_debug_p (type, &low, &high))
10897 mod_type_die = subrange_type_die (type, low, high, context_die);
10898 item_type = TREE_TYPE (type);
10900 else if (is_base_type (type))
10901 mod_type_die = base_type_die (type);
10902 else
10904 gen_type_die (type, context_die);
10906 /* We have to get the type_main_variant here (and pass that to the
10907 `lookup_type_die' routine) because the ..._TYPE node we have
10908 might simply be a *copy* of some original type node (where the
10909 copy was created to help us keep track of typedef names) and
10910 that copy might have a different TYPE_UID from the original
10911 ..._TYPE node. */
10912 if (TREE_CODE (type) != VECTOR_TYPE)
10913 return lookup_type_die (type_main_variant (type));
10914 else
10915 /* Vectors have the debugging information in the type,
10916 not the main variant. */
10917 return lookup_type_die (type);
10920 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10921 don't output a DW_TAG_typedef, since there isn't one in the
10922 user's program; just attach a DW_AT_name to the type.
10923 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10924 if the base type already has the same name. */
10925 if (name
10926 && ((TREE_CODE (name) != TYPE_DECL
10927 && (qualified_type == TYPE_MAIN_VARIANT (type)
10928 || (cv_quals == TYPE_UNQUALIFIED)))
10929 || (TREE_CODE (name) == TYPE_DECL
10930 && TREE_TYPE (name) == qualified_type
10931 && DECL_NAME (name))))
10933 if (TREE_CODE (name) == TYPE_DECL)
10934 /* Could just call add_name_and_src_coords_attributes here,
10935 but since this is a builtin type it doesn't have any
10936 useful source coordinates anyway. */
10937 name = DECL_NAME (name);
10938 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10940 /* This probably indicates a bug. */
10941 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10943 name = TYPE_IDENTIFIER (type);
10944 add_name_attribute (mod_type_die,
10945 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10948 if (qualified_type)
10949 equate_type_number_to_die (qualified_type, mod_type_die);
10951 if (item_type)
10952 /* We must do this after the equate_type_number_to_die call, in case
10953 this is a recursive type. This ensures that the modified_type_die
10954 recursion will terminate even if the type is recursive. Recursive
10955 types are possible in Ada. */
10956 sub_die = modified_type_die (item_type,
10957 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10958 context_die);
10960 if (sub_die != NULL)
10961 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10963 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10964 if (TYPE_ARTIFICIAL (type))
10965 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10967 return mod_type_die;
10970 /* Generate DIEs for the generic parameters of T.
10971 T must be either a generic type or a generic function.
10972 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10974 static void
10975 gen_generic_params_dies (tree t)
10977 tree parms, args;
10978 int parms_num, i;
10979 dw_die_ref die = NULL;
10980 int non_default;
10982 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10983 return;
10985 if (TYPE_P (t))
10986 die = lookup_type_die (t);
10987 else if (DECL_P (t))
10988 die = lookup_decl_die (t);
10990 gcc_assert (die);
10992 parms = lang_hooks.get_innermost_generic_parms (t);
10993 if (!parms)
10994 /* T has no generic parameter. It means T is neither a generic type
10995 or function. End of story. */
10996 return;
10998 parms_num = TREE_VEC_LENGTH (parms);
10999 args = lang_hooks.get_innermost_generic_args (t);
11000 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11001 non_default = int_cst_value (TREE_CHAIN (args));
11002 else
11003 non_default = TREE_VEC_LENGTH (args);
11004 for (i = 0; i < parms_num; i++)
11006 tree parm, arg, arg_pack_elems;
11007 dw_die_ref parm_die;
11009 parm = TREE_VEC_ELT (parms, i);
11010 arg = TREE_VEC_ELT (args, i);
11011 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11012 gcc_assert (parm && TREE_VALUE (parm) && arg);
11014 if (parm && TREE_VALUE (parm) && arg)
11016 /* If PARM represents a template parameter pack,
11017 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11018 by DW_TAG_template_*_parameter DIEs for the argument
11019 pack elements of ARG. Note that ARG would then be
11020 an argument pack. */
11021 if (arg_pack_elems)
11022 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11023 arg_pack_elems,
11024 die);
11025 else
11026 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11027 true /* emit name */, die);
11028 if (i >= non_default)
11029 add_AT_flag (parm_die, DW_AT_default_value, 1);
11034 /* Create and return a DIE for PARM which should be
11035 the representation of a generic type parameter.
11036 For instance, in the C++ front end, PARM would be a template parameter.
11037 ARG is the argument to PARM.
11038 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11039 name of the PARM.
11040 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11041 as a child node. */
11043 static dw_die_ref
11044 generic_parameter_die (tree parm, tree arg,
11045 bool emit_name_p,
11046 dw_die_ref parent_die)
11048 dw_die_ref tmpl_die = NULL;
11049 const char *name = NULL;
11051 if (!parm || !DECL_NAME (parm) || !arg)
11052 return NULL;
11054 /* We support non-type generic parameters and arguments,
11055 type generic parameters and arguments, as well as
11056 generic generic parameters (a.k.a. template template parameters in C++)
11057 and arguments. */
11058 if (TREE_CODE (parm) == PARM_DECL)
11059 /* PARM is a nontype generic parameter */
11060 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11061 else if (TREE_CODE (parm) == TYPE_DECL)
11062 /* PARM is a type generic parameter. */
11063 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11064 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11065 /* PARM is a generic generic parameter.
11066 Its DIE is a GNU extension. It shall have a
11067 DW_AT_name attribute to represent the name of the template template
11068 parameter, and a DW_AT_GNU_template_name attribute to represent the
11069 name of the template template argument. */
11070 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11071 parent_die, parm);
11072 else
11073 gcc_unreachable ();
11075 if (tmpl_die)
11077 tree tmpl_type;
11079 /* If PARM is a generic parameter pack, it means we are
11080 emitting debug info for a template argument pack element.
11081 In other terms, ARG is a template argument pack element.
11082 In that case, we don't emit any DW_AT_name attribute for
11083 the die. */
11084 if (emit_name_p)
11086 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11087 gcc_assert (name);
11088 add_AT_string (tmpl_die, DW_AT_name, name);
11091 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11093 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11094 TMPL_DIE should have a child DW_AT_type attribute that is set
11095 to the type of the argument to PARM, which is ARG.
11096 If PARM is a type generic parameter, TMPL_DIE should have a
11097 child DW_AT_type that is set to ARG. */
11098 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11099 add_type_attribute (tmpl_die, tmpl_type,
11100 (TREE_THIS_VOLATILE (tmpl_type)
11101 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11102 parent_die);
11104 else
11106 /* So TMPL_DIE is a DIE representing a
11107 a generic generic template parameter, a.k.a template template
11108 parameter in C++ and arg is a template. */
11110 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11111 to the name of the argument. */
11112 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11113 if (name)
11114 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11117 if (TREE_CODE (parm) == PARM_DECL)
11118 /* So PARM is a non-type generic parameter.
11119 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11120 attribute of TMPL_DIE which value represents the value
11121 of ARG.
11122 We must be careful here:
11123 The value of ARG might reference some function decls.
11124 We might currently be emitting debug info for a generic
11125 type and types are emitted before function decls, we don't
11126 know if the function decls referenced by ARG will actually be
11127 emitted after cgraph computations.
11128 So must defer the generation of the DW_AT_const_value to
11129 after cgraph is ready. */
11130 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11133 return tmpl_die;
11136 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11137 PARM_PACK must be a template parameter pack. The returned DIE
11138 will be child DIE of PARENT_DIE. */
11140 static dw_die_ref
11141 template_parameter_pack_die (tree parm_pack,
11142 tree parm_pack_args,
11143 dw_die_ref parent_die)
11145 dw_die_ref die;
11146 int j;
11148 gcc_assert (parent_die && parm_pack);
11150 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11151 add_name_and_src_coords_attributes (die, parm_pack);
11152 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11153 generic_parameter_die (parm_pack,
11154 TREE_VEC_ELT (parm_pack_args, j),
11155 false /* Don't emit DW_AT_name */,
11156 die);
11157 return die;
11160 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11161 an enumerated type. */
11163 static inline int
11164 type_is_enum (const_tree type)
11166 return TREE_CODE (type) == ENUMERAL_TYPE;
11169 /* Return the DBX register number described by a given RTL node. */
11171 static unsigned int
11172 dbx_reg_number (const_rtx rtl)
11174 unsigned regno = REGNO (rtl);
11176 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11178 #ifdef LEAF_REG_REMAP
11179 if (crtl->uses_only_leaf_regs)
11181 int leaf_reg = LEAF_REG_REMAP (regno);
11182 if (leaf_reg != -1)
11183 regno = (unsigned) leaf_reg;
11185 #endif
11187 regno = DBX_REGISTER_NUMBER (regno);
11188 gcc_assert (regno != INVALID_REGNUM);
11189 return regno;
11192 /* Optionally add a DW_OP_piece term to a location description expression.
11193 DW_OP_piece is only added if the location description expression already
11194 doesn't end with DW_OP_piece. */
11196 static void
11197 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11199 dw_loc_descr_ref loc;
11201 if (*list_head != NULL)
11203 /* Find the end of the chain. */
11204 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11207 if (loc->dw_loc_opc != DW_OP_piece)
11208 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11212 /* Return a location descriptor that designates a machine register or
11213 zero if there is none. */
11215 static dw_loc_descr_ref
11216 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11218 rtx regs;
11220 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11221 return 0;
11223 /* We only use "frame base" when we're sure we're talking about the
11224 post-prologue local stack frame. We do this by *not* running
11225 register elimination until this point, and recognizing the special
11226 argument pointer and soft frame pointer rtx's.
11227 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11228 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11229 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11231 dw_loc_descr_ref result = NULL;
11233 if (dwarf_version >= 4 || !dwarf_strict)
11235 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11236 initialized);
11237 if (result)
11238 add_loc_descr (&result,
11239 new_loc_descr (DW_OP_stack_value, 0, 0));
11241 return result;
11244 regs = targetm.dwarf_register_span (rtl);
11246 if (REG_NREGS (rtl) > 1 || regs)
11247 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11248 else
11250 unsigned int dbx_regnum = dbx_reg_number (rtl);
11251 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11252 return 0;
11253 return one_reg_loc_descriptor (dbx_regnum, initialized);
11257 /* Return a location descriptor that designates a machine register for
11258 a given hard register number. */
11260 static dw_loc_descr_ref
11261 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11263 dw_loc_descr_ref reg_loc_descr;
11265 if (regno <= 31)
11266 reg_loc_descr
11267 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11268 else
11269 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11271 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11272 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11274 return reg_loc_descr;
11277 /* Given an RTL of a register, return a location descriptor that
11278 designates a value that spans more than one register. */
11280 static dw_loc_descr_ref
11281 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11282 enum var_init_status initialized)
11284 int size, i;
11285 dw_loc_descr_ref loc_result = NULL;
11287 /* Simple, contiguous registers. */
11288 if (regs == NULL_RTX)
11290 unsigned reg = REGNO (rtl);
11291 int nregs;
11293 #ifdef LEAF_REG_REMAP
11294 if (crtl->uses_only_leaf_regs)
11296 int leaf_reg = LEAF_REG_REMAP (reg);
11297 if (leaf_reg != -1)
11298 reg = (unsigned) leaf_reg;
11300 #endif
11302 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11303 nregs = REG_NREGS (rtl);
11305 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11307 loc_result = NULL;
11308 while (nregs--)
11310 dw_loc_descr_ref t;
11312 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11313 VAR_INIT_STATUS_INITIALIZED);
11314 add_loc_descr (&loc_result, t);
11315 add_loc_descr_op_piece (&loc_result, size);
11316 ++reg;
11318 return loc_result;
11321 /* Now onto stupid register sets in non contiguous locations. */
11323 gcc_assert (GET_CODE (regs) == PARALLEL);
11325 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11326 loc_result = NULL;
11328 for (i = 0; i < XVECLEN (regs, 0); ++i)
11330 dw_loc_descr_ref t;
11332 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11333 VAR_INIT_STATUS_INITIALIZED);
11334 add_loc_descr (&loc_result, t);
11335 add_loc_descr_op_piece (&loc_result, size);
11338 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11339 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11340 return loc_result;
11343 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11345 /* Return a location descriptor that designates a constant i,
11346 as a compound operation from constant (i >> shift), constant shift
11347 and DW_OP_shl. */
11349 static dw_loc_descr_ref
11350 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11352 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11353 add_loc_descr (&ret, int_loc_descriptor (shift));
11354 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11355 return ret;
11358 /* Return a location descriptor that designates a constant. */
11360 static dw_loc_descr_ref
11361 int_loc_descriptor (HOST_WIDE_INT i)
11363 enum dwarf_location_atom op;
11365 /* Pick the smallest representation of a constant, rather than just
11366 defaulting to the LEB encoding. */
11367 if (i >= 0)
11369 int clz = clz_hwi (i);
11370 int ctz = ctz_hwi (i);
11371 if (i <= 31)
11372 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11373 else if (i <= 0xff)
11374 op = DW_OP_const1u;
11375 else if (i <= 0xffff)
11376 op = DW_OP_const2u;
11377 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11378 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11379 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11380 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11381 while DW_OP_const4u is 5 bytes. */
11382 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11383 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11384 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11385 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11386 while DW_OP_const4u is 5 bytes. */
11387 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11388 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11389 op = DW_OP_const4u;
11390 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11391 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11392 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11393 while DW_OP_constu of constant >= 0x100000000 takes at least
11394 6 bytes. */
11395 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11396 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11397 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11398 >= HOST_BITS_PER_WIDE_INT)
11399 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11400 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11401 while DW_OP_constu takes in this case at least 6 bytes. */
11402 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11403 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11404 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11405 && size_of_uleb128 (i) > 6)
11406 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11407 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11408 else
11409 op = DW_OP_constu;
11411 else
11413 if (i >= -0x80)
11414 op = DW_OP_const1s;
11415 else if (i >= -0x8000)
11416 op = DW_OP_const2s;
11417 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11419 if (size_of_int_loc_descriptor (i) < 5)
11421 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11422 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11423 return ret;
11425 op = DW_OP_const4s;
11427 else
11429 if (size_of_int_loc_descriptor (i)
11430 < (unsigned long) 1 + size_of_sleb128 (i))
11432 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11433 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11434 return ret;
11436 op = DW_OP_consts;
11440 return new_loc_descr (op, i, 0);
11443 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11444 without actually allocating it. */
11446 static unsigned long
11447 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11449 return size_of_int_loc_descriptor (i >> shift)
11450 + size_of_int_loc_descriptor (shift)
11451 + 1;
11454 /* Return size_of_locs (int_loc_descriptor (i)) without
11455 actually allocating it. */
11457 static unsigned long
11458 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11460 unsigned long s;
11462 if (i >= 0)
11464 int clz, ctz;
11465 if (i <= 31)
11466 return 1;
11467 else if (i <= 0xff)
11468 return 2;
11469 else if (i <= 0xffff)
11470 return 3;
11471 clz = clz_hwi (i);
11472 ctz = ctz_hwi (i);
11473 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11474 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11475 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11476 - clz - 5);
11477 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11478 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11479 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11480 - clz - 8);
11481 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11482 return 5;
11483 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11484 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11485 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11486 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11487 - clz - 8);
11488 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11489 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11490 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11491 - clz - 16);
11492 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11493 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11494 && s > 6)
11495 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11496 - clz - 32);
11497 else
11498 return 1 + s;
11500 else
11502 if (i >= -0x80)
11503 return 2;
11504 else if (i >= -0x8000)
11505 return 3;
11506 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11508 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11510 s = size_of_int_loc_descriptor (-i) + 1;
11511 if (s < 5)
11512 return s;
11514 return 5;
11516 else
11518 unsigned long r = 1 + size_of_sleb128 (i);
11519 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11521 s = size_of_int_loc_descriptor (-i) + 1;
11522 if (s < r)
11523 return s;
11525 return r;
11530 /* Return loc description representing "address" of integer value.
11531 This can appear only as toplevel expression. */
11533 static dw_loc_descr_ref
11534 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11536 int litsize;
11537 dw_loc_descr_ref loc_result = NULL;
11539 if (!(dwarf_version >= 4 || !dwarf_strict))
11540 return NULL;
11542 litsize = size_of_int_loc_descriptor (i);
11543 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11544 is more compact. For DW_OP_stack_value we need:
11545 litsize + 1 (DW_OP_stack_value)
11546 and for DW_OP_implicit_value:
11547 1 (DW_OP_implicit_value) + 1 (length) + size. */
11548 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11550 loc_result = int_loc_descriptor (i);
11551 add_loc_descr (&loc_result,
11552 new_loc_descr (DW_OP_stack_value, 0, 0));
11553 return loc_result;
11556 loc_result = new_loc_descr (DW_OP_implicit_value,
11557 size, 0);
11558 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11559 loc_result->dw_loc_oprnd2.v.val_int = i;
11560 return loc_result;
11563 /* Return a location descriptor that designates a base+offset location. */
11565 static dw_loc_descr_ref
11566 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11567 enum var_init_status initialized)
11569 unsigned int regno;
11570 dw_loc_descr_ref result;
11571 dw_fde_ref fde = cfun->fde;
11573 /* We only use "frame base" when we're sure we're talking about the
11574 post-prologue local stack frame. We do this by *not* running
11575 register elimination until this point, and recognizing the special
11576 argument pointer and soft frame pointer rtx's. */
11577 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11579 rtx elim = (ira_use_lra_p
11580 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11581 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11583 if (elim != reg)
11585 if (GET_CODE (elim) == PLUS)
11587 offset += INTVAL (XEXP (elim, 1));
11588 elim = XEXP (elim, 0);
11590 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11591 && (elim == hard_frame_pointer_rtx
11592 || elim == stack_pointer_rtx))
11593 || elim == (frame_pointer_needed
11594 ? hard_frame_pointer_rtx
11595 : stack_pointer_rtx));
11597 /* If drap register is used to align stack, use frame
11598 pointer + offset to access stack variables. If stack
11599 is aligned without drap, use stack pointer + offset to
11600 access stack variables. */
11601 if (crtl->stack_realign_tried
11602 && reg == frame_pointer_rtx)
11604 int base_reg
11605 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11606 ? HARD_FRAME_POINTER_REGNUM
11607 : REGNO (elim));
11608 return new_reg_loc_descr (base_reg, offset);
11611 gcc_assert (frame_pointer_fb_offset_valid);
11612 offset += frame_pointer_fb_offset;
11613 return new_loc_descr (DW_OP_fbreg, offset, 0);
11617 regno = REGNO (reg);
11618 #ifdef LEAF_REG_REMAP
11619 if (crtl->uses_only_leaf_regs)
11621 int leaf_reg = LEAF_REG_REMAP (regno);
11622 if (leaf_reg != -1)
11623 regno = (unsigned) leaf_reg;
11625 #endif
11626 regno = DWARF_FRAME_REGNUM (regno);
11628 if (!optimize && fde
11629 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11631 /* Use cfa+offset to represent the location of arguments passed
11632 on the stack when drap is used to align stack.
11633 Only do this when not optimizing, for optimized code var-tracking
11634 is supposed to track where the arguments live and the register
11635 used as vdrap or drap in some spot might be used for something
11636 else in other part of the routine. */
11637 return new_loc_descr (DW_OP_fbreg, offset, 0);
11640 if (regno <= 31)
11641 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11642 offset, 0);
11643 else
11644 result = new_loc_descr (DW_OP_bregx, regno, offset);
11646 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11647 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11649 return result;
11652 /* Return true if this RTL expression describes a base+offset calculation. */
11654 static inline int
11655 is_based_loc (const_rtx rtl)
11657 return (GET_CODE (rtl) == PLUS
11658 && ((REG_P (XEXP (rtl, 0))
11659 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11660 && CONST_INT_P (XEXP (rtl, 1)))));
11663 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11664 failed. */
11666 static dw_loc_descr_ref
11667 tls_mem_loc_descriptor (rtx mem)
11669 tree base;
11670 dw_loc_descr_ref loc_result;
11672 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11673 return NULL;
11675 base = get_base_address (MEM_EXPR (mem));
11676 if (base == NULL
11677 || TREE_CODE (base) != VAR_DECL
11678 || !DECL_THREAD_LOCAL_P (base))
11679 return NULL;
11681 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11682 if (loc_result == NULL)
11683 return NULL;
11685 if (MEM_OFFSET (mem))
11686 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11688 return loc_result;
11691 /* Output debug info about reason why we failed to expand expression as dwarf
11692 expression. */
11694 static void
11695 expansion_failed (tree expr, rtx rtl, char const *reason)
11697 if (dump_file && (dump_flags & TDF_DETAILS))
11699 fprintf (dump_file, "Failed to expand as dwarf: ");
11700 if (expr)
11701 print_generic_expr (dump_file, expr, dump_flags);
11702 if (rtl)
11704 fprintf (dump_file, "\n");
11705 print_rtl (dump_file, rtl);
11707 fprintf (dump_file, "\nReason: %s\n", reason);
11711 /* Helper function for const_ok_for_output. */
11713 static bool
11714 const_ok_for_output_1 (rtx rtl)
11716 if (GET_CODE (rtl) == UNSPEC)
11718 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11719 we can't express it in the debug info. */
11720 #ifdef ENABLE_CHECKING
11721 /* Don't complain about TLS UNSPECs, those are just too hard to
11722 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11723 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11724 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11725 if (XVECLEN (rtl, 0) == 0
11726 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11727 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11728 inform (current_function_decl
11729 ? DECL_SOURCE_LOCATION (current_function_decl)
11730 : UNKNOWN_LOCATION,
11731 #if NUM_UNSPEC_VALUES > 0
11732 "non-delegitimized UNSPEC %s (%d) found in variable location",
11733 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11734 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11735 XINT (rtl, 1));
11736 #else
11737 "non-delegitimized UNSPEC %d found in variable location",
11738 XINT (rtl, 1));
11739 #endif
11740 #endif
11741 expansion_failed (NULL_TREE, rtl,
11742 "UNSPEC hasn't been delegitimized.\n");
11743 return false;
11746 if (targetm.const_not_ok_for_debug_p (rtl))
11748 expansion_failed (NULL_TREE, rtl,
11749 "Expression rejected for debug by the backend.\n");
11750 return false;
11753 /* FIXME: Refer to PR60655. It is possible for simplification
11754 of rtl expressions in var tracking to produce such expressions.
11755 We should really identify / validate expressions
11756 enclosed in CONST that can be handled by assemblers on various
11757 targets and only handle legitimate cases here. */
11758 if (GET_CODE (rtl) != SYMBOL_REF)
11760 if (GET_CODE (rtl) == NOT)
11761 return false;
11762 return true;
11765 if (CONSTANT_POOL_ADDRESS_P (rtl))
11767 bool marked;
11768 get_pool_constant_mark (rtl, &marked);
11769 /* If all references to this pool constant were optimized away,
11770 it was not output and thus we can't represent it. */
11771 if (!marked)
11773 expansion_failed (NULL_TREE, rtl,
11774 "Constant was removed from constant pool.\n");
11775 return false;
11779 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11780 return false;
11782 /* Avoid references to external symbols in debug info, on several targets
11783 the linker might even refuse to link when linking a shared library,
11784 and in many other cases the relocations for .debug_info/.debug_loc are
11785 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11786 to be defined within the same shared library or executable are fine. */
11787 if (SYMBOL_REF_EXTERNAL_P (rtl))
11789 tree decl = SYMBOL_REF_DECL (rtl);
11791 if (decl == NULL || !targetm.binds_local_p (decl))
11793 expansion_failed (NULL_TREE, rtl,
11794 "Symbol not defined in current TU.\n");
11795 return false;
11799 return true;
11802 /* Return true if constant RTL can be emitted in DW_OP_addr or
11803 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11804 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11806 static bool
11807 const_ok_for_output (rtx rtl)
11809 if (GET_CODE (rtl) == SYMBOL_REF)
11810 return const_ok_for_output_1 (rtl);
11812 if (GET_CODE (rtl) == CONST)
11814 subrtx_var_iterator::array_type array;
11815 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11816 if (!const_ok_for_output_1 (*iter))
11817 return false;
11818 return true;
11821 return true;
11824 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11825 if possible, NULL otherwise. */
11827 static dw_die_ref
11828 base_type_for_mode (machine_mode mode, bool unsignedp)
11830 dw_die_ref type_die;
11831 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11833 if (type == NULL)
11834 return NULL;
11835 switch (TREE_CODE (type))
11837 case INTEGER_TYPE:
11838 case REAL_TYPE:
11839 break;
11840 default:
11841 return NULL;
11843 type_die = lookup_type_die (type);
11844 if (!type_die)
11845 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11846 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11847 return NULL;
11848 return type_die;
11851 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11852 type matching MODE, or, if MODE is narrower than or as wide as
11853 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11854 possible. */
11856 static dw_loc_descr_ref
11857 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11859 machine_mode outer_mode = mode;
11860 dw_die_ref type_die;
11861 dw_loc_descr_ref cvt;
11863 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11865 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11866 return op;
11868 type_die = base_type_for_mode (outer_mode, 1);
11869 if (type_die == NULL)
11870 return NULL;
11871 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11872 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11873 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11874 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11875 add_loc_descr (&op, cvt);
11876 return op;
11879 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11881 static dw_loc_descr_ref
11882 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11883 dw_loc_descr_ref op1)
11885 dw_loc_descr_ref ret = op0;
11886 add_loc_descr (&ret, op1);
11887 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11888 if (STORE_FLAG_VALUE != 1)
11890 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11891 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11893 return ret;
11896 /* Return location descriptor for signed comparison OP RTL. */
11898 static dw_loc_descr_ref
11899 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11900 machine_mode mem_mode)
11902 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11903 dw_loc_descr_ref op0, op1;
11904 int shift;
11906 if (op_mode == VOIDmode)
11907 op_mode = GET_MODE (XEXP (rtl, 1));
11908 if (op_mode == VOIDmode)
11909 return NULL;
11911 if (dwarf_strict
11912 && (GET_MODE_CLASS (op_mode) != MODE_INT
11913 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11914 return NULL;
11916 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11917 VAR_INIT_STATUS_INITIALIZED);
11918 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11919 VAR_INIT_STATUS_INITIALIZED);
11921 if (op0 == NULL || op1 == NULL)
11922 return NULL;
11924 if (GET_MODE_CLASS (op_mode) != MODE_INT
11925 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11926 return compare_loc_descriptor (op, op0, op1);
11928 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11930 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11931 dw_loc_descr_ref cvt;
11933 if (type_die == NULL)
11934 return NULL;
11935 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11936 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11937 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11938 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11939 add_loc_descr (&op0, cvt);
11940 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11941 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11942 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11943 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11944 add_loc_descr (&op1, cvt);
11945 return compare_loc_descriptor (op, op0, op1);
11948 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11949 /* For eq/ne, if the operands are known to be zero-extended,
11950 there is no need to do the fancy shifting up. */
11951 if (op == DW_OP_eq || op == DW_OP_ne)
11953 dw_loc_descr_ref last0, last1;
11954 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11956 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11958 /* deref_size zero extends, and for constants we can check
11959 whether they are zero extended or not. */
11960 if (((last0->dw_loc_opc == DW_OP_deref_size
11961 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11962 || (CONST_INT_P (XEXP (rtl, 0))
11963 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11964 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11965 && ((last1->dw_loc_opc == DW_OP_deref_size
11966 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11967 || (CONST_INT_P (XEXP (rtl, 1))
11968 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11969 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11970 return compare_loc_descriptor (op, op0, op1);
11972 /* EQ/NE comparison against constant in narrower type than
11973 DWARF2_ADDR_SIZE can be performed either as
11974 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11975 DW_OP_{eq,ne}
11977 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11978 DW_OP_{eq,ne}. Pick whatever is shorter. */
11979 if (CONST_INT_P (XEXP (rtl, 1))
11980 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11981 && (size_of_int_loc_descriptor (shift) + 1
11982 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11983 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11984 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11985 & GET_MODE_MASK (op_mode))))
11987 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11988 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11989 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11990 & GET_MODE_MASK (op_mode));
11991 return compare_loc_descriptor (op, op0, op1);
11994 add_loc_descr (&op0, int_loc_descriptor (shift));
11995 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11996 if (CONST_INT_P (XEXP (rtl, 1)))
11997 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11998 else
12000 add_loc_descr (&op1, int_loc_descriptor (shift));
12001 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12003 return compare_loc_descriptor (op, op0, op1);
12006 /* Return location descriptor for unsigned comparison OP RTL. */
12008 static dw_loc_descr_ref
12009 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12010 machine_mode mem_mode)
12012 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12013 dw_loc_descr_ref op0, op1;
12015 if (op_mode == VOIDmode)
12016 op_mode = GET_MODE (XEXP (rtl, 1));
12017 if (op_mode == VOIDmode)
12018 return NULL;
12019 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12020 return NULL;
12022 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12023 return NULL;
12025 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12026 VAR_INIT_STATUS_INITIALIZED);
12027 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12028 VAR_INIT_STATUS_INITIALIZED);
12030 if (op0 == NULL || op1 == NULL)
12031 return NULL;
12033 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12035 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12036 dw_loc_descr_ref last0, last1;
12037 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12039 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12041 if (CONST_INT_P (XEXP (rtl, 0)))
12042 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12043 /* deref_size zero extends, so no need to mask it again. */
12044 else if (last0->dw_loc_opc != DW_OP_deref_size
12045 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12047 add_loc_descr (&op0, int_loc_descriptor (mask));
12048 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12050 if (CONST_INT_P (XEXP (rtl, 1)))
12051 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12052 /* deref_size zero extends, so no need to mask it again. */
12053 else if (last1->dw_loc_opc != DW_OP_deref_size
12054 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12056 add_loc_descr (&op1, int_loc_descriptor (mask));
12057 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12060 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12062 HOST_WIDE_INT bias = 1;
12063 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12064 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12065 if (CONST_INT_P (XEXP (rtl, 1)))
12066 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12067 + INTVAL (XEXP (rtl, 1)));
12068 else
12069 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12070 bias, 0));
12072 return compare_loc_descriptor (op, op0, op1);
12075 /* Return location descriptor for {U,S}{MIN,MAX}. */
12077 static dw_loc_descr_ref
12078 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12079 machine_mode mem_mode)
12081 enum dwarf_location_atom op;
12082 dw_loc_descr_ref op0, op1, ret;
12083 dw_loc_descr_ref bra_node, drop_node;
12085 if (dwarf_strict
12086 && (GET_MODE_CLASS (mode) != MODE_INT
12087 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12088 return NULL;
12090 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12091 VAR_INIT_STATUS_INITIALIZED);
12092 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12093 VAR_INIT_STATUS_INITIALIZED);
12095 if (op0 == NULL || op1 == NULL)
12096 return NULL;
12098 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12099 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12100 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12101 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12103 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12105 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12106 add_loc_descr (&op0, int_loc_descriptor (mask));
12107 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12108 add_loc_descr (&op1, int_loc_descriptor (mask));
12109 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12111 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12113 HOST_WIDE_INT bias = 1;
12114 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12115 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12116 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12119 else if (GET_MODE_CLASS (mode) == MODE_INT
12120 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12122 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12123 add_loc_descr (&op0, int_loc_descriptor (shift));
12124 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12125 add_loc_descr (&op1, int_loc_descriptor (shift));
12126 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12128 else if (GET_MODE_CLASS (mode) == MODE_INT
12129 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12131 dw_die_ref type_die = base_type_for_mode (mode, 0);
12132 dw_loc_descr_ref cvt;
12133 if (type_die == NULL)
12134 return NULL;
12135 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12136 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12137 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12138 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12139 add_loc_descr (&op0, cvt);
12140 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12141 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12142 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12143 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12144 add_loc_descr (&op1, cvt);
12147 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12148 op = DW_OP_lt;
12149 else
12150 op = DW_OP_gt;
12151 ret = op0;
12152 add_loc_descr (&ret, op1);
12153 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12154 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12155 add_loc_descr (&ret, bra_node);
12156 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12157 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12158 add_loc_descr (&ret, drop_node);
12159 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12160 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12161 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12162 && GET_MODE_CLASS (mode) == MODE_INT
12163 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12164 ret = convert_descriptor_to_mode (mode, ret);
12165 return ret;
12168 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12169 but after converting arguments to type_die, afterwards
12170 convert back to unsigned. */
12172 static dw_loc_descr_ref
12173 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12174 machine_mode mode, machine_mode mem_mode)
12176 dw_loc_descr_ref cvt, op0, op1;
12178 if (type_die == NULL)
12179 return NULL;
12180 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12181 VAR_INIT_STATUS_INITIALIZED);
12182 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12183 VAR_INIT_STATUS_INITIALIZED);
12184 if (op0 == NULL || op1 == NULL)
12185 return NULL;
12186 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12187 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12188 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12189 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12190 add_loc_descr (&op0, cvt);
12191 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12192 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12193 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12194 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12195 add_loc_descr (&op1, cvt);
12196 add_loc_descr (&op0, op1);
12197 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12198 return convert_descriptor_to_mode (mode, op0);
12201 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12202 const0 is DW_OP_lit0 or corresponding typed constant,
12203 const1 is DW_OP_lit1 or corresponding typed constant
12204 and constMSB is constant with just the MSB bit set
12205 for the mode):
12206 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12207 L1: const0 DW_OP_swap
12208 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12209 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12210 L3: DW_OP_drop
12211 L4: DW_OP_nop
12213 CTZ is similar:
12214 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12215 L1: const0 DW_OP_swap
12216 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12217 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12218 L3: DW_OP_drop
12219 L4: DW_OP_nop
12221 FFS is similar:
12222 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12223 L1: const1 DW_OP_swap
12224 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12225 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12226 L3: DW_OP_drop
12227 L4: DW_OP_nop */
12229 static dw_loc_descr_ref
12230 clz_loc_descriptor (rtx rtl, machine_mode mode,
12231 machine_mode mem_mode)
12233 dw_loc_descr_ref op0, ret, tmp;
12234 HOST_WIDE_INT valv;
12235 dw_loc_descr_ref l1jump, l1label;
12236 dw_loc_descr_ref l2jump, l2label;
12237 dw_loc_descr_ref l3jump, l3label;
12238 dw_loc_descr_ref l4jump, l4label;
12239 rtx msb;
12241 if (GET_MODE_CLASS (mode) != MODE_INT
12242 || GET_MODE (XEXP (rtl, 0)) != mode)
12243 return NULL;
12245 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12246 VAR_INIT_STATUS_INITIALIZED);
12247 if (op0 == NULL)
12248 return NULL;
12249 ret = op0;
12250 if (GET_CODE (rtl) == CLZ)
12252 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12253 valv = GET_MODE_BITSIZE (mode);
12255 else if (GET_CODE (rtl) == FFS)
12256 valv = 0;
12257 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12258 valv = GET_MODE_BITSIZE (mode);
12259 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12260 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12261 add_loc_descr (&ret, l1jump);
12262 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12263 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12264 VAR_INIT_STATUS_INITIALIZED);
12265 if (tmp == NULL)
12266 return NULL;
12267 add_loc_descr (&ret, tmp);
12268 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12269 add_loc_descr (&ret, l4jump);
12270 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12271 ? const1_rtx : const0_rtx,
12272 mode, mem_mode,
12273 VAR_INIT_STATUS_INITIALIZED);
12274 if (l1label == NULL)
12275 return NULL;
12276 add_loc_descr (&ret, l1label);
12277 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12278 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12279 add_loc_descr (&ret, l2label);
12280 if (GET_CODE (rtl) != CLZ)
12281 msb = const1_rtx;
12282 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12283 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12284 << (GET_MODE_BITSIZE (mode) - 1));
12285 else
12286 msb = immed_wide_int_const
12287 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12288 GET_MODE_PRECISION (mode)), mode);
12289 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12290 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12291 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12292 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12293 else
12294 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12295 VAR_INIT_STATUS_INITIALIZED);
12296 if (tmp == NULL)
12297 return NULL;
12298 add_loc_descr (&ret, tmp);
12299 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12300 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12301 add_loc_descr (&ret, l3jump);
12302 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12303 VAR_INIT_STATUS_INITIALIZED);
12304 if (tmp == NULL)
12305 return NULL;
12306 add_loc_descr (&ret, tmp);
12307 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12308 ? DW_OP_shl : DW_OP_shr, 0, 0));
12309 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12310 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12311 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12312 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12313 add_loc_descr (&ret, l2jump);
12314 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12315 add_loc_descr (&ret, l3label);
12316 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12317 add_loc_descr (&ret, l4label);
12318 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12319 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12320 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12321 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12322 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12323 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12324 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12325 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12326 return ret;
12329 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12330 const1 is DW_OP_lit1 or corresponding typed constant):
12331 const0 DW_OP_swap
12332 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12333 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12334 L2: DW_OP_drop
12336 PARITY is similar:
12337 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12338 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12339 L2: DW_OP_drop */
12341 static dw_loc_descr_ref
12342 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12343 machine_mode mem_mode)
12345 dw_loc_descr_ref op0, ret, tmp;
12346 dw_loc_descr_ref l1jump, l1label;
12347 dw_loc_descr_ref l2jump, l2label;
12349 if (GET_MODE_CLASS (mode) != MODE_INT
12350 || GET_MODE (XEXP (rtl, 0)) != mode)
12351 return NULL;
12353 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12354 VAR_INIT_STATUS_INITIALIZED);
12355 if (op0 == NULL)
12356 return NULL;
12357 ret = op0;
12358 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12359 VAR_INIT_STATUS_INITIALIZED);
12360 if (tmp == NULL)
12361 return NULL;
12362 add_loc_descr (&ret, tmp);
12363 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12364 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12365 add_loc_descr (&ret, l1label);
12366 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12367 add_loc_descr (&ret, l2jump);
12368 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12369 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12370 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12371 VAR_INIT_STATUS_INITIALIZED);
12372 if (tmp == NULL)
12373 return NULL;
12374 add_loc_descr (&ret, tmp);
12375 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12376 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12377 ? DW_OP_plus : DW_OP_xor, 0, 0));
12378 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12379 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12380 VAR_INIT_STATUS_INITIALIZED);
12381 add_loc_descr (&ret, tmp);
12382 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12383 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12384 add_loc_descr (&ret, l1jump);
12385 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12386 add_loc_descr (&ret, l2label);
12387 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12388 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12389 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12390 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12391 return ret;
12394 /* BSWAP (constS is initial shift count, either 56 or 24):
12395 constS const0
12396 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12397 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12398 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12399 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12400 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12402 static dw_loc_descr_ref
12403 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12404 machine_mode mem_mode)
12406 dw_loc_descr_ref op0, ret, tmp;
12407 dw_loc_descr_ref l1jump, l1label;
12408 dw_loc_descr_ref l2jump, l2label;
12410 if (GET_MODE_CLASS (mode) != MODE_INT
12411 || BITS_PER_UNIT != 8
12412 || (GET_MODE_BITSIZE (mode) != 32
12413 && GET_MODE_BITSIZE (mode) != 64))
12414 return NULL;
12416 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12417 VAR_INIT_STATUS_INITIALIZED);
12418 if (op0 == NULL)
12419 return NULL;
12421 ret = op0;
12422 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12423 mode, mem_mode,
12424 VAR_INIT_STATUS_INITIALIZED);
12425 if (tmp == NULL)
12426 return NULL;
12427 add_loc_descr (&ret, tmp);
12428 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12429 VAR_INIT_STATUS_INITIALIZED);
12430 if (tmp == NULL)
12431 return NULL;
12432 add_loc_descr (&ret, tmp);
12433 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12434 add_loc_descr (&ret, l1label);
12435 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12436 mode, mem_mode,
12437 VAR_INIT_STATUS_INITIALIZED);
12438 add_loc_descr (&ret, tmp);
12439 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12440 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12441 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12442 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12443 VAR_INIT_STATUS_INITIALIZED);
12444 if (tmp == NULL)
12445 return NULL;
12446 add_loc_descr (&ret, tmp);
12447 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12448 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12449 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12450 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12451 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12452 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12453 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12454 VAR_INIT_STATUS_INITIALIZED);
12455 add_loc_descr (&ret, tmp);
12456 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12457 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12458 add_loc_descr (&ret, l2jump);
12459 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12460 VAR_INIT_STATUS_INITIALIZED);
12461 add_loc_descr (&ret, tmp);
12462 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12463 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12464 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12465 add_loc_descr (&ret, l1jump);
12466 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12467 add_loc_descr (&ret, l2label);
12468 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12469 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12470 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12471 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12472 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12473 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12474 return ret;
12477 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12478 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12479 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12480 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12482 ROTATERT is similar:
12483 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12484 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12485 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12487 static dw_loc_descr_ref
12488 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12489 machine_mode mem_mode)
12491 rtx rtlop1 = XEXP (rtl, 1);
12492 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12493 int i;
12495 if (GET_MODE_CLASS (mode) != MODE_INT)
12496 return NULL;
12498 if (GET_MODE (rtlop1) != VOIDmode
12499 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12500 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12501 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12502 VAR_INIT_STATUS_INITIALIZED);
12503 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12504 VAR_INIT_STATUS_INITIALIZED);
12505 if (op0 == NULL || op1 == NULL)
12506 return NULL;
12507 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12508 for (i = 0; i < 2; i++)
12510 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12511 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12512 mode, mem_mode,
12513 VAR_INIT_STATUS_INITIALIZED);
12514 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12515 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12516 ? DW_OP_const4u
12517 : HOST_BITS_PER_WIDE_INT == 64
12518 ? DW_OP_const8u : DW_OP_constu,
12519 GET_MODE_MASK (mode), 0);
12520 else
12521 mask[i] = NULL;
12522 if (mask[i] == NULL)
12523 return NULL;
12524 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12526 ret = op0;
12527 add_loc_descr (&ret, op1);
12528 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12529 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12530 if (GET_CODE (rtl) == ROTATERT)
12532 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12533 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12534 GET_MODE_BITSIZE (mode), 0));
12536 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12537 if (mask[0] != NULL)
12538 add_loc_descr (&ret, mask[0]);
12539 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12540 if (mask[1] != NULL)
12542 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12543 add_loc_descr (&ret, mask[1]);
12544 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12546 if (GET_CODE (rtl) == ROTATE)
12548 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12549 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12550 GET_MODE_BITSIZE (mode), 0));
12552 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12553 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12554 return ret;
12557 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12558 for DEBUG_PARAMETER_REF RTL. */
12560 static dw_loc_descr_ref
12561 parameter_ref_descriptor (rtx rtl)
12563 dw_loc_descr_ref ret;
12564 dw_die_ref ref;
12566 if (dwarf_strict)
12567 return NULL;
12568 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12569 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12570 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12571 if (ref)
12573 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12574 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12575 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12577 else
12579 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12580 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12582 return ret;
12585 /* The following routine converts the RTL for a variable or parameter
12586 (resident in memory) into an equivalent Dwarf representation of a
12587 mechanism for getting the address of that same variable onto the top of a
12588 hypothetical "address evaluation" stack.
12590 When creating memory location descriptors, we are effectively transforming
12591 the RTL for a memory-resident object into its Dwarf postfix expression
12592 equivalent. This routine recursively descends an RTL tree, turning
12593 it into Dwarf postfix code as it goes.
12595 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12597 MEM_MODE is the mode of the memory reference, needed to handle some
12598 autoincrement addressing modes.
12600 Return 0 if we can't represent the location. */
12602 dw_loc_descr_ref
12603 mem_loc_descriptor (rtx rtl, machine_mode mode,
12604 machine_mode mem_mode,
12605 enum var_init_status initialized)
12607 dw_loc_descr_ref mem_loc_result = NULL;
12608 enum dwarf_location_atom op;
12609 dw_loc_descr_ref op0, op1;
12610 rtx inner = NULL_RTX;
12612 if (mode == VOIDmode)
12613 mode = GET_MODE (rtl);
12615 /* Note that for a dynamically sized array, the location we will generate a
12616 description of here will be the lowest numbered location which is
12617 actually within the array. That's *not* necessarily the same as the
12618 zeroth element of the array. */
12620 rtl = targetm.delegitimize_address (rtl);
12622 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12623 return NULL;
12625 switch (GET_CODE (rtl))
12627 case POST_INC:
12628 case POST_DEC:
12629 case POST_MODIFY:
12630 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12632 case SUBREG:
12633 /* The case of a subreg may arise when we have a local (register)
12634 variable or a formal (register) parameter which doesn't quite fill
12635 up an entire register. For now, just assume that it is
12636 legitimate to make the Dwarf info refer to the whole register which
12637 contains the given subreg. */
12638 if (!subreg_lowpart_p (rtl))
12639 break;
12640 inner = SUBREG_REG (rtl);
12641 case TRUNCATE:
12642 if (inner == NULL_RTX)
12643 inner = XEXP (rtl, 0);
12644 if (GET_MODE_CLASS (mode) == MODE_INT
12645 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12646 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12647 #ifdef POINTERS_EXTEND_UNSIGNED
12648 || (mode == Pmode && mem_mode != VOIDmode)
12649 #endif
12651 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12653 mem_loc_result = mem_loc_descriptor (inner,
12654 GET_MODE (inner),
12655 mem_mode, initialized);
12656 break;
12658 if (dwarf_strict)
12659 break;
12660 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12661 break;
12662 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12663 && (GET_MODE_CLASS (mode) != MODE_INT
12664 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12665 break;
12666 else
12668 dw_die_ref type_die;
12669 dw_loc_descr_ref cvt;
12671 mem_loc_result = mem_loc_descriptor (inner,
12672 GET_MODE (inner),
12673 mem_mode, initialized);
12674 if (mem_loc_result == NULL)
12675 break;
12676 type_die = base_type_for_mode (mode,
12677 GET_MODE_CLASS (mode) == MODE_INT);
12678 if (type_die == NULL)
12680 mem_loc_result = NULL;
12681 break;
12683 if (GET_MODE_SIZE (mode)
12684 != GET_MODE_SIZE (GET_MODE (inner)))
12685 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12686 else
12687 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12688 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12689 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12690 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12691 add_loc_descr (&mem_loc_result, cvt);
12693 break;
12695 case REG:
12696 if (GET_MODE_CLASS (mode) != MODE_INT
12697 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12698 && rtl != arg_pointer_rtx
12699 && rtl != frame_pointer_rtx
12700 #ifdef POINTERS_EXTEND_UNSIGNED
12701 && (mode != Pmode || mem_mode == VOIDmode)
12702 #endif
12705 dw_die_ref type_die;
12706 unsigned int dbx_regnum;
12708 if (dwarf_strict)
12709 break;
12710 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12711 break;
12712 type_die = base_type_for_mode (mode,
12713 GET_MODE_CLASS (mode) == MODE_INT);
12714 if (type_die == NULL)
12715 break;
12717 dbx_regnum = dbx_reg_number (rtl);
12718 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12719 break;
12720 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12721 dbx_regnum, 0);
12722 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12723 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12724 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12725 break;
12727 /* Whenever a register number forms a part of the description of the
12728 method for calculating the (dynamic) address of a memory resident
12729 object, DWARF rules require the register number be referred to as
12730 a "base register". This distinction is not based in any way upon
12731 what category of register the hardware believes the given register
12732 belongs to. This is strictly DWARF terminology we're dealing with
12733 here. Note that in cases where the location of a memory-resident
12734 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12735 OP_CONST (0)) the actual DWARF location descriptor that we generate
12736 may just be OP_BASEREG (basereg). This may look deceptively like
12737 the object in question was allocated to a register (rather than in
12738 memory) so DWARF consumers need to be aware of the subtle
12739 distinction between OP_REG and OP_BASEREG. */
12740 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12741 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12742 else if (stack_realign_drap
12743 && crtl->drap_reg
12744 && crtl->args.internal_arg_pointer == rtl
12745 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12747 /* If RTL is internal_arg_pointer, which has been optimized
12748 out, use DRAP instead. */
12749 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12750 VAR_INIT_STATUS_INITIALIZED);
12752 break;
12754 case SIGN_EXTEND:
12755 case ZERO_EXTEND:
12756 if (GET_MODE_CLASS (mode) != MODE_INT)
12757 break;
12758 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12759 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12760 if (op0 == 0)
12761 break;
12762 else if (GET_CODE (rtl) == ZERO_EXTEND
12763 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12764 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12765 < HOST_BITS_PER_WIDE_INT
12766 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12767 to expand zero extend as two shifts instead of
12768 masking. */
12769 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12771 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12772 mem_loc_result = op0;
12773 add_loc_descr (&mem_loc_result,
12774 int_loc_descriptor (GET_MODE_MASK (imode)));
12775 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12777 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12779 int shift = DWARF2_ADDR_SIZE
12780 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12781 shift *= BITS_PER_UNIT;
12782 if (GET_CODE (rtl) == SIGN_EXTEND)
12783 op = DW_OP_shra;
12784 else
12785 op = DW_OP_shr;
12786 mem_loc_result = op0;
12787 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12788 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12789 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12790 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12792 else if (!dwarf_strict)
12794 dw_die_ref type_die1, type_die2;
12795 dw_loc_descr_ref cvt;
12797 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12798 GET_CODE (rtl) == ZERO_EXTEND);
12799 if (type_die1 == NULL)
12800 break;
12801 type_die2 = base_type_for_mode (mode, 1);
12802 if (type_die2 == NULL)
12803 break;
12804 mem_loc_result = op0;
12805 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12806 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12807 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12808 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12809 add_loc_descr (&mem_loc_result, cvt);
12810 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12811 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12812 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12813 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12814 add_loc_descr (&mem_loc_result, cvt);
12816 break;
12818 case MEM:
12820 rtx new_rtl = avoid_constant_pool_reference (rtl);
12821 if (new_rtl != rtl)
12823 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12824 initialized);
12825 if (mem_loc_result != NULL)
12826 return mem_loc_result;
12829 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12830 get_address_mode (rtl), mode,
12831 VAR_INIT_STATUS_INITIALIZED);
12832 if (mem_loc_result == NULL)
12833 mem_loc_result = tls_mem_loc_descriptor (rtl);
12834 if (mem_loc_result != NULL)
12836 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12837 || GET_MODE_CLASS (mode) != MODE_INT)
12839 dw_die_ref type_die;
12840 dw_loc_descr_ref deref;
12842 if (dwarf_strict)
12843 return NULL;
12844 type_die
12845 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12846 if (type_die == NULL)
12847 return NULL;
12848 deref = new_loc_descr (DW_OP_GNU_deref_type,
12849 GET_MODE_SIZE (mode), 0);
12850 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12851 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12852 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12853 add_loc_descr (&mem_loc_result, deref);
12855 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12856 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12857 else
12858 add_loc_descr (&mem_loc_result,
12859 new_loc_descr (DW_OP_deref_size,
12860 GET_MODE_SIZE (mode), 0));
12862 break;
12864 case LO_SUM:
12865 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12867 case LABEL_REF:
12868 /* Some ports can transform a symbol ref into a label ref, because
12869 the symbol ref is too far away and has to be dumped into a constant
12870 pool. */
12871 case CONST:
12872 case SYMBOL_REF:
12873 if ((GET_MODE_CLASS (mode) != MODE_INT
12874 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12875 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12876 #ifdef POINTERS_EXTEND_UNSIGNED
12877 && (mode != Pmode || mem_mode == VOIDmode)
12878 #endif
12880 break;
12881 if (GET_CODE (rtl) == SYMBOL_REF
12882 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12884 dw_loc_descr_ref temp;
12886 /* If this is not defined, we have no way to emit the data. */
12887 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12888 break;
12890 temp = new_addr_loc_descr (rtl, dtprel_true);
12892 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12893 add_loc_descr (&mem_loc_result, temp);
12895 break;
12898 if (!const_ok_for_output (rtl))
12900 if (GET_CODE (rtl) == CONST)
12901 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12902 initialized);
12903 break;
12906 symref:
12907 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12908 vec_safe_push (used_rtx_array, rtl);
12909 break;
12911 case CONCAT:
12912 case CONCATN:
12913 case VAR_LOCATION:
12914 case DEBUG_IMPLICIT_PTR:
12915 expansion_failed (NULL_TREE, rtl,
12916 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12917 return 0;
12919 case ENTRY_VALUE:
12920 if (dwarf_strict)
12921 return NULL;
12922 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12924 if (GET_MODE_CLASS (mode) != MODE_INT
12925 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12926 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12927 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12928 else
12930 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12931 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12932 return NULL;
12933 op0 = one_reg_loc_descriptor (dbx_regnum,
12934 VAR_INIT_STATUS_INITIALIZED);
12937 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12938 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12940 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12941 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12942 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12943 return NULL;
12945 else
12946 gcc_unreachable ();
12947 if (op0 == NULL)
12948 return NULL;
12949 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12950 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12951 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12952 break;
12954 case DEBUG_PARAMETER_REF:
12955 mem_loc_result = parameter_ref_descriptor (rtl);
12956 break;
12958 case PRE_MODIFY:
12959 /* Extract the PLUS expression nested inside and fall into
12960 PLUS code below. */
12961 rtl = XEXP (rtl, 1);
12962 goto plus;
12964 case PRE_INC:
12965 case PRE_DEC:
12966 /* Turn these into a PLUS expression and fall into the PLUS code
12967 below. */
12968 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12969 gen_int_mode (GET_CODE (rtl) == PRE_INC
12970 ? GET_MODE_UNIT_SIZE (mem_mode)
12971 : -GET_MODE_UNIT_SIZE (mem_mode),
12972 mode));
12974 /* ... fall through ... */
12976 case PLUS:
12977 plus:
12978 if (is_based_loc (rtl)
12979 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12980 || XEXP (rtl, 0) == arg_pointer_rtx
12981 || XEXP (rtl, 0) == frame_pointer_rtx)
12982 && GET_MODE_CLASS (mode) == MODE_INT)
12983 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12984 INTVAL (XEXP (rtl, 1)),
12985 VAR_INIT_STATUS_INITIALIZED);
12986 else
12988 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12989 VAR_INIT_STATUS_INITIALIZED);
12990 if (mem_loc_result == 0)
12991 break;
12993 if (CONST_INT_P (XEXP (rtl, 1))
12994 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12995 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12996 else
12998 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12999 VAR_INIT_STATUS_INITIALIZED);
13000 if (op1 == 0)
13001 return NULL;
13002 add_loc_descr (&mem_loc_result, op1);
13003 add_loc_descr (&mem_loc_result,
13004 new_loc_descr (DW_OP_plus, 0, 0));
13007 break;
13009 /* If a pseudo-reg is optimized away, it is possible for it to
13010 be replaced with a MEM containing a multiply or shift. */
13011 case MINUS:
13012 op = DW_OP_minus;
13013 goto do_binop;
13015 case MULT:
13016 op = DW_OP_mul;
13017 goto do_binop;
13019 case DIV:
13020 if (!dwarf_strict
13021 && GET_MODE_CLASS (mode) == MODE_INT
13022 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13024 mem_loc_result = typed_binop (DW_OP_div, rtl,
13025 base_type_for_mode (mode, 0),
13026 mode, mem_mode);
13027 break;
13029 op = DW_OP_div;
13030 goto do_binop;
13032 case UMOD:
13033 op = DW_OP_mod;
13034 goto do_binop;
13036 case ASHIFT:
13037 op = DW_OP_shl;
13038 goto do_shift;
13040 case ASHIFTRT:
13041 op = DW_OP_shra;
13042 goto do_shift;
13044 case LSHIFTRT:
13045 op = DW_OP_shr;
13046 goto do_shift;
13048 do_shift:
13049 if (GET_MODE_CLASS (mode) != MODE_INT)
13050 break;
13051 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13052 VAR_INIT_STATUS_INITIALIZED);
13054 rtx rtlop1 = XEXP (rtl, 1);
13055 if (GET_MODE (rtlop1) != VOIDmode
13056 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13057 < GET_MODE_BITSIZE (mode))
13058 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13059 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13060 VAR_INIT_STATUS_INITIALIZED);
13063 if (op0 == 0 || op1 == 0)
13064 break;
13066 mem_loc_result = op0;
13067 add_loc_descr (&mem_loc_result, op1);
13068 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13069 break;
13071 case AND:
13072 op = DW_OP_and;
13073 goto do_binop;
13075 case IOR:
13076 op = DW_OP_or;
13077 goto do_binop;
13079 case XOR:
13080 op = DW_OP_xor;
13081 goto do_binop;
13083 do_binop:
13084 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13085 VAR_INIT_STATUS_INITIALIZED);
13086 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13087 VAR_INIT_STATUS_INITIALIZED);
13089 if (op0 == 0 || op1 == 0)
13090 break;
13092 mem_loc_result = op0;
13093 add_loc_descr (&mem_loc_result, op1);
13094 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13095 break;
13097 case MOD:
13098 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13100 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13101 base_type_for_mode (mode, 0),
13102 mode, mem_mode);
13103 break;
13106 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13107 VAR_INIT_STATUS_INITIALIZED);
13108 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13109 VAR_INIT_STATUS_INITIALIZED);
13111 if (op0 == 0 || op1 == 0)
13112 break;
13114 mem_loc_result = op0;
13115 add_loc_descr (&mem_loc_result, op1);
13116 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13117 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13118 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13119 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13120 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13121 break;
13123 case UDIV:
13124 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13126 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13128 op = DW_OP_div;
13129 goto do_binop;
13131 mem_loc_result = typed_binop (DW_OP_div, rtl,
13132 base_type_for_mode (mode, 1),
13133 mode, mem_mode);
13135 break;
13137 case NOT:
13138 op = DW_OP_not;
13139 goto do_unop;
13141 case ABS:
13142 op = DW_OP_abs;
13143 goto do_unop;
13145 case NEG:
13146 op = DW_OP_neg;
13147 goto do_unop;
13149 do_unop:
13150 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13151 VAR_INIT_STATUS_INITIALIZED);
13153 if (op0 == 0)
13154 break;
13156 mem_loc_result = op0;
13157 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13158 break;
13160 case CONST_INT:
13161 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13162 #ifdef POINTERS_EXTEND_UNSIGNED
13163 || (mode == Pmode
13164 && mem_mode != VOIDmode
13165 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13166 #endif
13169 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13170 break;
13172 if (!dwarf_strict
13173 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13174 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13176 dw_die_ref type_die = base_type_for_mode (mode, 1);
13177 machine_mode amode;
13178 if (type_die == NULL)
13179 return NULL;
13180 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13181 MODE_INT, 0);
13182 if (INTVAL (rtl) >= 0
13183 && amode != BLKmode
13184 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13185 /* const DW_OP_GNU_convert <XXX> vs.
13186 DW_OP_GNU_const_type <XXX, 1, const>. */
13187 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13188 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13190 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13191 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13192 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13193 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13194 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13195 add_loc_descr (&mem_loc_result, op0);
13196 return mem_loc_result;
13198 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13199 INTVAL (rtl));
13200 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13201 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13202 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13203 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13204 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13205 else
13207 mem_loc_result->dw_loc_oprnd2.val_class
13208 = dw_val_class_const_double;
13209 mem_loc_result->dw_loc_oprnd2.v.val_double
13210 = double_int::from_shwi (INTVAL (rtl));
13213 break;
13215 case CONST_DOUBLE:
13216 if (!dwarf_strict)
13218 dw_die_ref type_die;
13220 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13221 CONST_DOUBLE rtx could represent either a large integer
13222 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13223 the value is always a floating point constant.
13225 When it is an integer, a CONST_DOUBLE is used whenever
13226 the constant requires 2 HWIs to be adequately represented.
13227 We output CONST_DOUBLEs as blocks. */
13228 if (mode == VOIDmode
13229 || (GET_MODE (rtl) == VOIDmode
13230 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13231 break;
13232 type_die = base_type_for_mode (mode,
13233 GET_MODE_CLASS (mode) == MODE_INT);
13234 if (type_die == NULL)
13235 return NULL;
13236 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13237 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13238 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13239 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13240 #if TARGET_SUPPORTS_WIDE_INT == 0
13241 if (!SCALAR_FLOAT_MODE_P (mode))
13243 mem_loc_result->dw_loc_oprnd2.val_class
13244 = dw_val_class_const_double;
13245 mem_loc_result->dw_loc_oprnd2.v.val_double
13246 = rtx_to_double_int (rtl);
13248 else
13249 #endif
13251 unsigned int length = GET_MODE_SIZE (mode);
13252 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13254 insert_float (rtl, array);
13255 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13256 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13257 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13258 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13261 break;
13263 case CONST_WIDE_INT:
13264 if (!dwarf_strict)
13266 dw_die_ref type_die;
13268 type_die = base_type_for_mode (mode,
13269 GET_MODE_CLASS (mode) == MODE_INT);
13270 if (type_die == NULL)
13271 return NULL;
13272 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13273 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13274 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13275 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13276 mem_loc_result->dw_loc_oprnd2.val_class
13277 = dw_val_class_wide_int;
13278 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13279 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13281 break;
13283 case EQ:
13284 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13285 break;
13287 case GE:
13288 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13289 break;
13291 case GT:
13292 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13293 break;
13295 case LE:
13296 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13297 break;
13299 case LT:
13300 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13301 break;
13303 case NE:
13304 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13305 break;
13307 case GEU:
13308 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13309 break;
13311 case GTU:
13312 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13313 break;
13315 case LEU:
13316 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13317 break;
13319 case LTU:
13320 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13321 break;
13323 case UMIN:
13324 case UMAX:
13325 if (GET_MODE_CLASS (mode) != MODE_INT)
13326 break;
13327 /* FALLTHRU */
13328 case SMIN:
13329 case SMAX:
13330 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13331 break;
13333 case ZERO_EXTRACT:
13334 case SIGN_EXTRACT:
13335 if (CONST_INT_P (XEXP (rtl, 1))
13336 && CONST_INT_P (XEXP (rtl, 2))
13337 && ((unsigned) INTVAL (XEXP (rtl, 1))
13338 + (unsigned) INTVAL (XEXP (rtl, 2))
13339 <= GET_MODE_BITSIZE (mode))
13340 && GET_MODE_CLASS (mode) == MODE_INT
13341 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13342 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13344 int shift, size;
13345 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13346 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13347 if (op0 == 0)
13348 break;
13349 if (GET_CODE (rtl) == SIGN_EXTRACT)
13350 op = DW_OP_shra;
13351 else
13352 op = DW_OP_shr;
13353 mem_loc_result = op0;
13354 size = INTVAL (XEXP (rtl, 1));
13355 shift = INTVAL (XEXP (rtl, 2));
13356 if (BITS_BIG_ENDIAN)
13357 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13358 - shift - size;
13359 if (shift + size != (int) DWARF2_ADDR_SIZE)
13361 add_loc_descr (&mem_loc_result,
13362 int_loc_descriptor (DWARF2_ADDR_SIZE
13363 - shift - size));
13364 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13366 if (size != (int) DWARF2_ADDR_SIZE)
13368 add_loc_descr (&mem_loc_result,
13369 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13370 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13373 break;
13375 case IF_THEN_ELSE:
13377 dw_loc_descr_ref op2, bra_node, drop_node;
13378 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13379 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13380 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13381 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13382 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13383 VAR_INIT_STATUS_INITIALIZED);
13384 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13385 VAR_INIT_STATUS_INITIALIZED);
13386 if (op0 == NULL || op1 == NULL || op2 == NULL)
13387 break;
13389 mem_loc_result = op1;
13390 add_loc_descr (&mem_loc_result, op2);
13391 add_loc_descr (&mem_loc_result, op0);
13392 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13393 add_loc_descr (&mem_loc_result, bra_node);
13394 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13395 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13396 add_loc_descr (&mem_loc_result, drop_node);
13397 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13398 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13400 break;
13402 case FLOAT_EXTEND:
13403 case FLOAT_TRUNCATE:
13404 case FLOAT:
13405 case UNSIGNED_FLOAT:
13406 case FIX:
13407 case UNSIGNED_FIX:
13408 if (!dwarf_strict)
13410 dw_die_ref type_die;
13411 dw_loc_descr_ref cvt;
13413 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13414 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13415 if (op0 == NULL)
13416 break;
13417 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13418 && (GET_CODE (rtl) == FLOAT
13419 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13420 <= DWARF2_ADDR_SIZE))
13422 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13423 GET_CODE (rtl) == UNSIGNED_FLOAT);
13424 if (type_die == NULL)
13425 break;
13426 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13427 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13428 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13429 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13430 add_loc_descr (&op0, cvt);
13432 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13433 if (type_die == NULL)
13434 break;
13435 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13436 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13437 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13438 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13439 add_loc_descr (&op0, cvt);
13440 if (GET_MODE_CLASS (mode) == MODE_INT
13441 && (GET_CODE (rtl) == FIX
13442 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13444 op0 = convert_descriptor_to_mode (mode, op0);
13445 if (op0 == NULL)
13446 break;
13448 mem_loc_result = op0;
13450 break;
13452 case CLZ:
13453 case CTZ:
13454 case FFS:
13455 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13456 break;
13458 case POPCOUNT:
13459 case PARITY:
13460 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13461 break;
13463 case BSWAP:
13464 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13465 break;
13467 case ROTATE:
13468 case ROTATERT:
13469 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13470 break;
13472 case COMPARE:
13473 /* In theory, we could implement the above. */
13474 /* DWARF cannot represent the unsigned compare operations
13475 natively. */
13476 case SS_MULT:
13477 case US_MULT:
13478 case SS_DIV:
13479 case US_DIV:
13480 case SS_PLUS:
13481 case US_PLUS:
13482 case SS_MINUS:
13483 case US_MINUS:
13484 case SS_NEG:
13485 case US_NEG:
13486 case SS_ABS:
13487 case SS_ASHIFT:
13488 case US_ASHIFT:
13489 case SS_TRUNCATE:
13490 case US_TRUNCATE:
13491 case UNORDERED:
13492 case ORDERED:
13493 case UNEQ:
13494 case UNGE:
13495 case UNGT:
13496 case UNLE:
13497 case UNLT:
13498 case LTGT:
13499 case FRACT_CONVERT:
13500 case UNSIGNED_FRACT_CONVERT:
13501 case SAT_FRACT:
13502 case UNSIGNED_SAT_FRACT:
13503 case SQRT:
13504 case ASM_OPERANDS:
13505 case VEC_MERGE:
13506 case VEC_SELECT:
13507 case VEC_CONCAT:
13508 case VEC_DUPLICATE:
13509 case UNSPEC:
13510 case HIGH:
13511 case FMA:
13512 case STRICT_LOW_PART:
13513 case CONST_VECTOR:
13514 case CONST_FIXED:
13515 case CLRSB:
13516 case CLOBBER:
13517 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13518 can't express it in the debug info. This can happen e.g. with some
13519 TLS UNSPECs. */
13520 break;
13522 case CONST_STRING:
13523 resolve_one_addr (&rtl);
13524 goto symref;
13526 default:
13527 #ifdef ENABLE_CHECKING
13528 print_rtl (stderr, rtl);
13529 gcc_unreachable ();
13530 #else
13531 break;
13532 #endif
13535 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13536 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13538 return mem_loc_result;
13541 /* Return a descriptor that describes the concatenation of two locations.
13542 This is typically a complex variable. */
13544 static dw_loc_descr_ref
13545 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13547 dw_loc_descr_ref cc_loc_result = NULL;
13548 dw_loc_descr_ref x0_ref
13549 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13550 dw_loc_descr_ref x1_ref
13551 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13553 if (x0_ref == 0 || x1_ref == 0)
13554 return 0;
13556 cc_loc_result = x0_ref;
13557 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13559 add_loc_descr (&cc_loc_result, x1_ref);
13560 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13562 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13563 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13565 return cc_loc_result;
13568 /* Return a descriptor that describes the concatenation of N
13569 locations. */
13571 static dw_loc_descr_ref
13572 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13574 unsigned int i;
13575 dw_loc_descr_ref cc_loc_result = NULL;
13576 unsigned int n = XVECLEN (concatn, 0);
13578 for (i = 0; i < n; ++i)
13580 dw_loc_descr_ref ref;
13581 rtx x = XVECEXP (concatn, 0, i);
13583 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13584 if (ref == NULL)
13585 return NULL;
13587 add_loc_descr (&cc_loc_result, ref);
13588 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13591 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13592 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13594 return cc_loc_result;
13597 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13598 for DEBUG_IMPLICIT_PTR RTL. */
13600 static dw_loc_descr_ref
13601 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13603 dw_loc_descr_ref ret;
13604 dw_die_ref ref;
13606 if (dwarf_strict)
13607 return NULL;
13608 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13609 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13610 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13611 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13612 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13613 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13614 if (ref)
13616 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13617 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13618 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13620 else
13622 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13623 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13625 return ret;
13628 /* Output a proper Dwarf location descriptor for a variable or parameter
13629 which is either allocated in a register or in a memory location. For a
13630 register, we just generate an OP_REG and the register number. For a
13631 memory location we provide a Dwarf postfix expression describing how to
13632 generate the (dynamic) address of the object onto the address stack.
13634 MODE is mode of the decl if this loc_descriptor is going to be used in
13635 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13636 allowed, VOIDmode otherwise.
13638 If we don't know how to describe it, return 0. */
13640 static dw_loc_descr_ref
13641 loc_descriptor (rtx rtl, machine_mode mode,
13642 enum var_init_status initialized)
13644 dw_loc_descr_ref loc_result = NULL;
13646 switch (GET_CODE (rtl))
13648 case SUBREG:
13649 /* The case of a subreg may arise when we have a local (register)
13650 variable or a formal (register) parameter which doesn't quite fill
13651 up an entire register. For now, just assume that it is
13652 legitimate to make the Dwarf info refer to the whole register which
13653 contains the given subreg. */
13654 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13655 loc_result = loc_descriptor (SUBREG_REG (rtl),
13656 GET_MODE (SUBREG_REG (rtl)), initialized);
13657 else
13658 goto do_default;
13659 break;
13661 case REG:
13662 loc_result = reg_loc_descriptor (rtl, initialized);
13663 break;
13665 case MEM:
13666 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13667 GET_MODE (rtl), initialized);
13668 if (loc_result == NULL)
13669 loc_result = tls_mem_loc_descriptor (rtl);
13670 if (loc_result == NULL)
13672 rtx new_rtl = avoid_constant_pool_reference (rtl);
13673 if (new_rtl != rtl)
13674 loc_result = loc_descriptor (new_rtl, mode, initialized);
13676 break;
13678 case CONCAT:
13679 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13680 initialized);
13681 break;
13683 case CONCATN:
13684 loc_result = concatn_loc_descriptor (rtl, initialized);
13685 break;
13687 case VAR_LOCATION:
13688 /* Single part. */
13689 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13691 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13692 if (GET_CODE (loc) == EXPR_LIST)
13693 loc = XEXP (loc, 0);
13694 loc_result = loc_descriptor (loc, mode, initialized);
13695 break;
13698 rtl = XEXP (rtl, 1);
13699 /* FALLTHRU */
13701 case PARALLEL:
13703 rtvec par_elems = XVEC (rtl, 0);
13704 int num_elem = GET_NUM_ELEM (par_elems);
13705 machine_mode mode;
13706 int i;
13708 /* Create the first one, so we have something to add to. */
13709 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13710 VOIDmode, initialized);
13711 if (loc_result == NULL)
13712 return NULL;
13713 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13714 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13715 for (i = 1; i < num_elem; i++)
13717 dw_loc_descr_ref temp;
13719 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13720 VOIDmode, initialized);
13721 if (temp == NULL)
13722 return NULL;
13723 add_loc_descr (&loc_result, temp);
13724 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13725 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13728 break;
13730 case CONST_INT:
13731 if (mode != VOIDmode && mode != BLKmode)
13732 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13733 INTVAL (rtl));
13734 break;
13736 case CONST_DOUBLE:
13737 if (mode == VOIDmode)
13738 mode = GET_MODE (rtl);
13740 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13742 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13744 /* Note that a CONST_DOUBLE rtx could represent either an integer
13745 or a floating-point constant. A CONST_DOUBLE is used whenever
13746 the constant requires more than one word in order to be
13747 adequately represented. We output CONST_DOUBLEs as blocks. */
13748 loc_result = new_loc_descr (DW_OP_implicit_value,
13749 GET_MODE_SIZE (mode), 0);
13750 #if TARGET_SUPPORTS_WIDE_INT == 0
13751 if (!SCALAR_FLOAT_MODE_P (mode))
13753 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13754 loc_result->dw_loc_oprnd2.v.val_double
13755 = rtx_to_double_int (rtl);
13757 else
13758 #endif
13760 unsigned int length = GET_MODE_SIZE (mode);
13761 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13763 insert_float (rtl, array);
13764 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13765 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13766 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13767 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13770 break;
13772 case CONST_WIDE_INT:
13773 if (mode == VOIDmode)
13774 mode = GET_MODE (rtl);
13776 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13778 loc_result = new_loc_descr (DW_OP_implicit_value,
13779 GET_MODE_SIZE (mode), 0);
13780 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13781 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13782 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13784 break;
13786 case CONST_VECTOR:
13787 if (mode == VOIDmode)
13788 mode = GET_MODE (rtl);
13790 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13792 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13793 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13794 unsigned char *array
13795 = ggc_vec_alloc<unsigned char> (length * elt_size);
13796 unsigned int i;
13797 unsigned char *p;
13798 machine_mode imode = GET_MODE_INNER (mode);
13800 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13801 switch (GET_MODE_CLASS (mode))
13803 case MODE_VECTOR_INT:
13804 for (i = 0, p = array; i < length; i++, p += elt_size)
13806 rtx elt = CONST_VECTOR_ELT (rtl, i);
13807 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13809 break;
13811 case MODE_VECTOR_FLOAT:
13812 for (i = 0, p = array; i < length; i++, p += elt_size)
13814 rtx elt = CONST_VECTOR_ELT (rtl, i);
13815 insert_float (elt, p);
13817 break;
13819 default:
13820 gcc_unreachable ();
13823 loc_result = new_loc_descr (DW_OP_implicit_value,
13824 length * elt_size, 0);
13825 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13826 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13827 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13828 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13830 break;
13832 case CONST:
13833 if (mode == VOIDmode
13834 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13835 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13836 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13838 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13839 break;
13841 /* FALLTHROUGH */
13842 case SYMBOL_REF:
13843 if (!const_ok_for_output (rtl))
13844 break;
13845 case LABEL_REF:
13846 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13847 && (dwarf_version >= 4 || !dwarf_strict))
13849 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13850 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13851 vec_safe_push (used_rtx_array, rtl);
13853 break;
13855 case DEBUG_IMPLICIT_PTR:
13856 loc_result = implicit_ptr_descriptor (rtl, 0);
13857 break;
13859 case PLUS:
13860 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13861 && CONST_INT_P (XEXP (rtl, 1)))
13863 loc_result
13864 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13865 break;
13867 /* FALLTHRU */
13868 do_default:
13869 default:
13870 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13871 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13872 && dwarf_version >= 4)
13873 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13875 /* Value expression. */
13876 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13877 if (loc_result)
13878 add_loc_descr (&loc_result,
13879 new_loc_descr (DW_OP_stack_value, 0, 0));
13881 break;
13884 return loc_result;
13887 /* We need to figure out what section we should use as the base for the
13888 address ranges where a given location is valid.
13889 1. If this particular DECL has a section associated with it, use that.
13890 2. If this function has a section associated with it, use that.
13891 3. Otherwise, use the text section.
13892 XXX: If you split a variable across multiple sections, we won't notice. */
13894 static const char *
13895 secname_for_decl (const_tree decl)
13897 const char *secname;
13899 if (VAR_OR_FUNCTION_DECL_P (decl)
13900 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13901 && DECL_SECTION_NAME (decl))
13902 secname = DECL_SECTION_NAME (decl);
13903 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13904 secname = DECL_SECTION_NAME (current_function_decl);
13905 else if (cfun && in_cold_section_p)
13906 secname = crtl->subsections.cold_section_label;
13907 else
13908 secname = text_section_label;
13910 return secname;
13913 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13915 static bool
13916 decl_by_reference_p (tree decl)
13918 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13919 || TREE_CODE (decl) == VAR_DECL)
13920 && DECL_BY_REFERENCE (decl));
13923 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13924 for VARLOC. */
13926 static dw_loc_descr_ref
13927 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13928 enum var_init_status initialized)
13930 int have_address = 0;
13931 dw_loc_descr_ref descr;
13932 machine_mode mode;
13934 if (want_address != 2)
13936 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13937 /* Single part. */
13938 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13940 varloc = PAT_VAR_LOCATION_LOC (varloc);
13941 if (GET_CODE (varloc) == EXPR_LIST)
13942 varloc = XEXP (varloc, 0);
13943 mode = GET_MODE (varloc);
13944 if (MEM_P (varloc))
13946 rtx addr = XEXP (varloc, 0);
13947 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13948 mode, initialized);
13949 if (descr)
13950 have_address = 1;
13951 else
13953 rtx x = avoid_constant_pool_reference (varloc);
13954 if (x != varloc)
13955 descr = mem_loc_descriptor (x, mode, VOIDmode,
13956 initialized);
13959 else
13960 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13962 else
13963 return 0;
13965 else
13967 if (GET_CODE (varloc) == VAR_LOCATION)
13968 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13969 else
13970 mode = DECL_MODE (loc);
13971 descr = loc_descriptor (varloc, mode, initialized);
13972 have_address = 1;
13975 if (!descr)
13976 return 0;
13978 if (want_address == 2 && !have_address
13979 && (dwarf_version >= 4 || !dwarf_strict))
13981 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13983 expansion_failed (loc, NULL_RTX,
13984 "DWARF address size mismatch");
13985 return 0;
13987 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13988 have_address = 1;
13990 /* Show if we can't fill the request for an address. */
13991 if (want_address && !have_address)
13993 expansion_failed (loc, NULL_RTX,
13994 "Want address and only have value");
13995 return 0;
13998 /* If we've got an address and don't want one, dereference. */
13999 if (!want_address && have_address)
14001 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14002 enum dwarf_location_atom op;
14004 if (size > DWARF2_ADDR_SIZE || size == -1)
14006 expansion_failed (loc, NULL_RTX,
14007 "DWARF address size mismatch");
14008 return 0;
14010 else if (size == DWARF2_ADDR_SIZE)
14011 op = DW_OP_deref;
14012 else
14013 op = DW_OP_deref_size;
14015 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14018 return descr;
14021 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14022 if it is not possible. */
14024 static dw_loc_descr_ref
14025 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14027 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14028 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14029 else if (dwarf_version >= 3 || !dwarf_strict)
14030 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14031 else
14032 return NULL;
14035 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14036 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14038 static dw_loc_descr_ref
14039 dw_sra_loc_expr (tree decl, rtx loc)
14041 rtx p;
14042 unsigned HOST_WIDE_INT padsize = 0;
14043 dw_loc_descr_ref descr, *descr_tail;
14044 unsigned HOST_WIDE_INT decl_size;
14045 rtx varloc;
14046 enum var_init_status initialized;
14048 if (DECL_SIZE (decl) == NULL
14049 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14050 return NULL;
14052 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14053 descr = NULL;
14054 descr_tail = &descr;
14056 for (p = loc; p; p = XEXP (p, 1))
14058 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14059 rtx loc_note = *decl_piece_varloc_ptr (p);
14060 dw_loc_descr_ref cur_descr;
14061 dw_loc_descr_ref *tail, last = NULL;
14062 unsigned HOST_WIDE_INT opsize = 0;
14064 if (loc_note == NULL_RTX
14065 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14067 padsize += bitsize;
14068 continue;
14070 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14071 varloc = NOTE_VAR_LOCATION (loc_note);
14072 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14073 if (cur_descr == NULL)
14075 padsize += bitsize;
14076 continue;
14079 /* Check that cur_descr either doesn't use
14080 DW_OP_*piece operations, or their sum is equal
14081 to bitsize. Otherwise we can't embed it. */
14082 for (tail = &cur_descr; *tail != NULL;
14083 tail = &(*tail)->dw_loc_next)
14084 if ((*tail)->dw_loc_opc == DW_OP_piece)
14086 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14087 * BITS_PER_UNIT;
14088 last = *tail;
14090 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14092 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14093 last = *tail;
14096 if (last != NULL && opsize != bitsize)
14098 padsize += bitsize;
14099 /* Discard the current piece of the descriptor and release any
14100 addr_table entries it uses. */
14101 remove_loc_list_addr_table_entries (cur_descr);
14102 continue;
14105 /* If there is a hole, add DW_OP_*piece after empty DWARF
14106 expression, which means that those bits are optimized out. */
14107 if (padsize)
14109 if (padsize > decl_size)
14111 remove_loc_list_addr_table_entries (cur_descr);
14112 goto discard_descr;
14114 decl_size -= padsize;
14115 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14116 if (*descr_tail == NULL)
14118 remove_loc_list_addr_table_entries (cur_descr);
14119 goto discard_descr;
14121 descr_tail = &(*descr_tail)->dw_loc_next;
14122 padsize = 0;
14124 *descr_tail = cur_descr;
14125 descr_tail = tail;
14126 if (bitsize > decl_size)
14127 goto discard_descr;
14128 decl_size -= bitsize;
14129 if (last == NULL)
14131 HOST_WIDE_INT offset = 0;
14132 if (GET_CODE (varloc) == VAR_LOCATION
14133 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14135 varloc = PAT_VAR_LOCATION_LOC (varloc);
14136 if (GET_CODE (varloc) == EXPR_LIST)
14137 varloc = XEXP (varloc, 0);
14141 if (GET_CODE (varloc) == CONST
14142 || GET_CODE (varloc) == SIGN_EXTEND
14143 || GET_CODE (varloc) == ZERO_EXTEND)
14144 varloc = XEXP (varloc, 0);
14145 else if (GET_CODE (varloc) == SUBREG)
14146 varloc = SUBREG_REG (varloc);
14147 else
14148 break;
14150 while (1);
14151 /* DW_OP_bit_size offset should be zero for register
14152 or implicit location descriptions and empty location
14153 descriptions, but for memory addresses needs big endian
14154 adjustment. */
14155 if (MEM_P (varloc))
14157 unsigned HOST_WIDE_INT memsize
14158 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14159 if (memsize != bitsize)
14161 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14162 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14163 goto discard_descr;
14164 if (memsize < bitsize)
14165 goto discard_descr;
14166 if (BITS_BIG_ENDIAN)
14167 offset = memsize - bitsize;
14171 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14172 if (*descr_tail == NULL)
14173 goto discard_descr;
14174 descr_tail = &(*descr_tail)->dw_loc_next;
14178 /* If there were any non-empty expressions, add padding till the end of
14179 the decl. */
14180 if (descr != NULL && decl_size != 0)
14182 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14183 if (*descr_tail == NULL)
14184 goto discard_descr;
14186 return descr;
14188 discard_descr:
14189 /* Discard the descriptor and release any addr_table entries it uses. */
14190 remove_loc_list_addr_table_entries (descr);
14191 return NULL;
14194 /* Return the dwarf representation of the location list LOC_LIST of
14195 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14196 function. */
14198 static dw_loc_list_ref
14199 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14201 const char *endname, *secname;
14202 rtx varloc;
14203 enum var_init_status initialized;
14204 struct var_loc_node *node;
14205 dw_loc_descr_ref descr;
14206 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14207 dw_loc_list_ref list = NULL;
14208 dw_loc_list_ref *listp = &list;
14210 /* Now that we know what section we are using for a base,
14211 actually construct the list of locations.
14212 The first location information is what is passed to the
14213 function that creates the location list, and the remaining
14214 locations just get added on to that list.
14215 Note that we only know the start address for a location
14216 (IE location changes), so to build the range, we use
14217 the range [current location start, next location start].
14218 This means we have to special case the last node, and generate
14219 a range of [last location start, end of function label]. */
14221 secname = secname_for_decl (decl);
14223 for (node = loc_list->first; node; node = node->next)
14224 if (GET_CODE (node->loc) == EXPR_LIST
14225 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14227 if (GET_CODE (node->loc) == EXPR_LIST)
14229 /* This requires DW_OP_{,bit_}piece, which is not usable
14230 inside DWARF expressions. */
14231 if (want_address != 2)
14232 continue;
14233 descr = dw_sra_loc_expr (decl, node->loc);
14234 if (descr == NULL)
14235 continue;
14237 else
14239 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14240 varloc = NOTE_VAR_LOCATION (node->loc);
14241 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14243 if (descr)
14245 bool range_across_switch = false;
14246 /* If section switch happens in between node->label
14247 and node->next->label (or end of function) and
14248 we can't emit it as a single entry list,
14249 emit two ranges, first one ending at the end
14250 of first partition and second one starting at the
14251 beginning of second partition. */
14252 if (node == loc_list->last_before_switch
14253 && (node != loc_list->first || loc_list->first->next)
14254 && current_function_decl)
14256 endname = cfun->fde->dw_fde_end;
14257 range_across_switch = true;
14259 /* The variable has a location between NODE->LABEL and
14260 NODE->NEXT->LABEL. */
14261 else if (node->next)
14262 endname = node->next->label;
14263 /* If the variable has a location at the last label
14264 it keeps its location until the end of function. */
14265 else if (!current_function_decl)
14266 endname = text_end_label;
14267 else
14269 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14270 current_function_funcdef_no);
14271 endname = ggc_strdup (label_id);
14274 *listp = new_loc_list (descr, node->label, endname, secname);
14275 if (TREE_CODE (decl) == PARM_DECL
14276 && node == loc_list->first
14277 && NOTE_P (node->loc)
14278 && strcmp (node->label, endname) == 0)
14279 (*listp)->force = true;
14280 listp = &(*listp)->dw_loc_next;
14282 if (range_across_switch)
14284 if (GET_CODE (node->loc) == EXPR_LIST)
14285 descr = dw_sra_loc_expr (decl, node->loc);
14286 else
14288 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14289 varloc = NOTE_VAR_LOCATION (node->loc);
14290 descr = dw_loc_list_1 (decl, varloc, want_address,
14291 initialized);
14293 gcc_assert (descr);
14294 /* The variable has a location between NODE->LABEL and
14295 NODE->NEXT->LABEL. */
14296 if (node->next)
14297 endname = node->next->label;
14298 else
14299 endname = cfun->fde->dw_fde_second_end;
14300 *listp = new_loc_list (descr,
14301 cfun->fde->dw_fde_second_begin,
14302 endname, secname);
14303 listp = &(*listp)->dw_loc_next;
14308 /* Try to avoid the overhead of a location list emitting a location
14309 expression instead, but only if we didn't have more than one
14310 location entry in the first place. If some entries were not
14311 representable, we don't want to pretend a single entry that was
14312 applies to the entire scope in which the variable is
14313 available. */
14314 if (list && loc_list->first->next)
14315 gen_llsym (list);
14317 return list;
14320 /* Return if the loc_list has only single element and thus can be represented
14321 as location description. */
14323 static bool
14324 single_element_loc_list_p (dw_loc_list_ref list)
14326 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14327 return !list->ll_symbol;
14330 /* To each location in list LIST add loc descr REF. */
14332 static void
14333 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14335 dw_loc_descr_ref copy;
14336 add_loc_descr (&list->expr, ref);
14337 list = list->dw_loc_next;
14338 while (list)
14340 copy = ggc_alloc<dw_loc_descr_node> ();
14341 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14342 add_loc_descr (&list->expr, copy);
14343 while (copy->dw_loc_next)
14345 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14346 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14347 copy->dw_loc_next = new_copy;
14348 copy = new_copy;
14350 list = list->dw_loc_next;
14354 /* Given two lists RET and LIST
14355 produce location list that is result of adding expression in LIST
14356 to expression in RET on each position in program.
14357 Might be destructive on both RET and LIST.
14359 TODO: We handle only simple cases of RET or LIST having at most one
14360 element. General case would inolve sorting the lists in program order
14361 and merging them that will need some additional work.
14362 Adding that will improve quality of debug info especially for SRA-ed
14363 structures. */
14365 static void
14366 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14368 if (!list)
14369 return;
14370 if (!*ret)
14372 *ret = list;
14373 return;
14375 if (!list->dw_loc_next)
14377 add_loc_descr_to_each (*ret, list->expr);
14378 return;
14380 if (!(*ret)->dw_loc_next)
14382 add_loc_descr_to_each (list, (*ret)->expr);
14383 *ret = list;
14384 return;
14386 expansion_failed (NULL_TREE, NULL_RTX,
14387 "Don't know how to merge two non-trivial"
14388 " location lists.\n");
14389 *ret = NULL;
14390 return;
14393 /* LOC is constant expression. Try a luck, look it up in constant
14394 pool and return its loc_descr of its address. */
14396 static dw_loc_descr_ref
14397 cst_pool_loc_descr (tree loc)
14399 /* Get an RTL for this, if something has been emitted. */
14400 rtx rtl = lookup_constant_def (loc);
14402 if (!rtl || !MEM_P (rtl))
14404 gcc_assert (!rtl);
14405 return 0;
14407 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14409 /* TODO: We might get more coverage if we was actually delaying expansion
14410 of all expressions till end of compilation when constant pools are fully
14411 populated. */
14412 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14414 expansion_failed (loc, NULL_RTX,
14415 "CST value in contant pool but not marked.");
14416 return 0;
14418 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14419 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14422 /* Return dw_loc_list representing address of addr_expr LOC
14423 by looking for inner INDIRECT_REF expression and turning
14424 it into simple arithmetics.
14426 See loc_list_from_tree for the meaning of CONTEXT. */
14428 static dw_loc_list_ref
14429 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14430 const loc_descr_context *context)
14432 tree obj, offset;
14433 HOST_WIDE_INT bitsize, bitpos, bytepos;
14434 machine_mode mode;
14435 int unsignedp, reversep, volatilep = 0;
14436 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14438 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14439 &bitsize, &bitpos, &offset, &mode,
14440 &unsignedp, &reversep, &volatilep, false);
14441 STRIP_NOPS (obj);
14442 if (bitpos % BITS_PER_UNIT)
14444 expansion_failed (loc, NULL_RTX, "bitfield access");
14445 return 0;
14447 if (!INDIRECT_REF_P (obj))
14449 expansion_failed (obj,
14450 NULL_RTX, "no indirect ref in inner refrence");
14451 return 0;
14453 if (!offset && !bitpos)
14454 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14455 context);
14456 else if (toplev
14457 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14458 && (dwarf_version >= 4 || !dwarf_strict))
14460 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14461 if (!list_ret)
14462 return 0;
14463 if (offset)
14465 /* Variable offset. */
14466 list_ret1 = loc_list_from_tree (offset, 0, context);
14467 if (list_ret1 == 0)
14468 return 0;
14469 add_loc_list (&list_ret, list_ret1);
14470 if (!list_ret)
14471 return 0;
14472 add_loc_descr_to_each (list_ret,
14473 new_loc_descr (DW_OP_plus, 0, 0));
14475 bytepos = bitpos / BITS_PER_UNIT;
14476 if (bytepos > 0)
14477 add_loc_descr_to_each (list_ret,
14478 new_loc_descr (DW_OP_plus_uconst,
14479 bytepos, 0));
14480 else if (bytepos < 0)
14481 loc_list_plus_const (list_ret, bytepos);
14482 add_loc_descr_to_each (list_ret,
14483 new_loc_descr (DW_OP_stack_value, 0, 0));
14485 return list_ret;
14489 /* Helper structure for location descriptions generation. */
14490 struct loc_descr_context
14492 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14493 NULL_TREE if DW_OP_push_object_address in invalid for this location
14494 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14495 tree context_type;
14496 /* The ..._DECL node that should be translated as a
14497 DW_OP_push_object_address operation. */
14498 tree base_decl;
14501 /* Generate Dwarf location list representing LOC.
14502 If WANT_ADDRESS is false, expression computing LOC will be computed
14503 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14504 if WANT_ADDRESS is 2, expression computing address useable in location
14505 will be returned (i.e. DW_OP_reg can be used
14506 to refer to register values).
14508 CONTEXT provides information to customize the location descriptions
14509 generation. Its context_type field specifies what type is implicitly
14510 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14511 will not be generated.
14513 If CONTEXT is NULL, the behavior is the same as if both context_type and
14514 base_decl fields were NULL_TREE. */
14516 static dw_loc_list_ref
14517 loc_list_from_tree (tree loc, int want_address,
14518 const struct loc_descr_context *context)
14520 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14521 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14522 int have_address = 0;
14523 enum dwarf_location_atom op;
14525 /* ??? Most of the time we do not take proper care for sign/zero
14526 extending the values properly. Hopefully this won't be a real
14527 problem... */
14529 if (context != NULL
14530 && context->base_decl == loc
14531 && want_address == 0)
14533 if (dwarf_version >= 3 || !dwarf_strict)
14534 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14535 NULL, NULL, NULL);
14536 else
14537 return NULL;
14540 switch (TREE_CODE (loc))
14542 case ERROR_MARK:
14543 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14544 return 0;
14546 case PLACEHOLDER_EXPR:
14547 /* This case involves extracting fields from an object to determine the
14548 position of other fields. It is supposed to appear only as the first
14549 operand of COMPONENT_REF nodes and to reference precisely the type
14550 that the context allows. */
14551 if (context != NULL
14552 && TREE_TYPE (loc) == context->context_type
14553 && want_address >= 1)
14555 if (dwarf_version >= 3 || !dwarf_strict)
14557 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14558 have_address = 1;
14559 break;
14561 else
14562 return NULL;
14564 else
14565 expansion_failed (loc, NULL_RTX,
14566 "PLACEHOLDER_EXPR for an unexpected type");
14567 break;
14569 case CALL_EXPR:
14570 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14571 /* There are no opcodes for these operations. */
14572 return 0;
14574 case PREINCREMENT_EXPR:
14575 case PREDECREMENT_EXPR:
14576 case POSTINCREMENT_EXPR:
14577 case POSTDECREMENT_EXPR:
14578 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14579 /* There are no opcodes for these operations. */
14580 return 0;
14582 case ADDR_EXPR:
14583 /* If we already want an address, see if there is INDIRECT_REF inside
14584 e.g. for &this->field. */
14585 if (want_address)
14587 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14588 (loc, want_address == 2, context);
14589 if (list_ret)
14590 have_address = 1;
14591 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14592 && (ret = cst_pool_loc_descr (loc)))
14593 have_address = 1;
14595 /* Otherwise, process the argument and look for the address. */
14596 if (!list_ret && !ret)
14597 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14598 else
14600 if (want_address)
14601 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14602 return NULL;
14604 break;
14606 case VAR_DECL:
14607 if (DECL_THREAD_LOCAL_P (loc))
14609 rtx rtl;
14610 enum dwarf_location_atom tls_op;
14611 enum dtprel_bool dtprel = dtprel_false;
14613 if (targetm.have_tls)
14615 /* If this is not defined, we have no way to emit the
14616 data. */
14617 if (!targetm.asm_out.output_dwarf_dtprel)
14618 return 0;
14620 /* The way DW_OP_GNU_push_tls_address is specified, we
14621 can only look up addresses of objects in the current
14622 module. We used DW_OP_addr as first op, but that's
14623 wrong, because DW_OP_addr is relocated by the debug
14624 info consumer, while DW_OP_GNU_push_tls_address
14625 operand shouldn't be. */
14626 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14627 return 0;
14628 dtprel = dtprel_true;
14629 tls_op = DW_OP_GNU_push_tls_address;
14631 else
14633 if (!targetm.emutls.debug_form_tls_address
14634 || !(dwarf_version >= 3 || !dwarf_strict))
14635 return 0;
14636 /* We stuffed the control variable into the DECL_VALUE_EXPR
14637 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14638 no longer appear in gimple code. We used the control
14639 variable in specific so that we could pick it up here. */
14640 loc = DECL_VALUE_EXPR (loc);
14641 tls_op = DW_OP_form_tls_address;
14644 rtl = rtl_for_decl_location (loc);
14645 if (rtl == NULL_RTX)
14646 return 0;
14648 if (!MEM_P (rtl))
14649 return 0;
14650 rtl = XEXP (rtl, 0);
14651 if (! CONSTANT_P (rtl))
14652 return 0;
14654 ret = new_addr_loc_descr (rtl, dtprel);
14655 ret1 = new_loc_descr (tls_op, 0, 0);
14656 add_loc_descr (&ret, ret1);
14658 have_address = 1;
14659 break;
14661 /* FALLTHRU */
14663 case PARM_DECL:
14664 case RESULT_DECL:
14665 if (DECL_HAS_VALUE_EXPR_P (loc))
14666 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14667 want_address, context);
14668 /* FALLTHRU */
14670 case FUNCTION_DECL:
14672 rtx rtl;
14673 var_loc_list *loc_list = lookup_decl_loc (loc);
14675 if (loc_list && loc_list->first)
14677 list_ret = dw_loc_list (loc_list, loc, want_address);
14678 have_address = want_address != 0;
14679 break;
14681 rtl = rtl_for_decl_location (loc);
14682 if (rtl == NULL_RTX)
14684 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14685 return 0;
14687 else if (CONST_INT_P (rtl))
14689 HOST_WIDE_INT val = INTVAL (rtl);
14690 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14691 val &= GET_MODE_MASK (DECL_MODE (loc));
14692 ret = int_loc_descriptor (val);
14694 else if (GET_CODE (rtl) == CONST_STRING)
14696 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14697 return 0;
14699 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14700 ret = new_addr_loc_descr (rtl, dtprel_false);
14701 else
14703 machine_mode mode, mem_mode;
14705 /* Certain constructs can only be represented at top-level. */
14706 if (want_address == 2)
14708 ret = loc_descriptor (rtl, VOIDmode,
14709 VAR_INIT_STATUS_INITIALIZED);
14710 have_address = 1;
14712 else
14714 mode = GET_MODE (rtl);
14715 mem_mode = VOIDmode;
14716 if (MEM_P (rtl))
14718 mem_mode = mode;
14719 mode = get_address_mode (rtl);
14720 rtl = XEXP (rtl, 0);
14721 have_address = 1;
14723 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14724 VAR_INIT_STATUS_INITIALIZED);
14726 if (!ret)
14727 expansion_failed (loc, rtl,
14728 "failed to produce loc descriptor for rtl");
14731 break;
14733 case MEM_REF:
14734 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14736 have_address = 1;
14737 goto do_plus;
14739 /* Fallthru. */
14740 case INDIRECT_REF:
14741 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14742 have_address = 1;
14743 break;
14745 case TARGET_MEM_REF:
14746 case SSA_NAME:
14747 case DEBUG_EXPR_DECL:
14748 return NULL;
14750 case COMPOUND_EXPR:
14751 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14753 CASE_CONVERT:
14754 case VIEW_CONVERT_EXPR:
14755 case SAVE_EXPR:
14756 case MODIFY_EXPR:
14757 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14759 case COMPONENT_REF:
14760 case BIT_FIELD_REF:
14761 case ARRAY_REF:
14762 case ARRAY_RANGE_REF:
14763 case REALPART_EXPR:
14764 case IMAGPART_EXPR:
14766 tree obj, offset;
14767 HOST_WIDE_INT bitsize, bitpos, bytepos;
14768 machine_mode mode;
14769 int unsignedp, reversep, volatilep = 0;
14771 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14772 &unsignedp, &reversep, &volatilep, false);
14774 gcc_assert (obj != loc);
14776 list_ret = loc_list_from_tree (obj,
14777 want_address == 2
14778 && !bitpos && !offset ? 2 : 1,
14779 context);
14780 /* TODO: We can extract value of the small expression via shifting even
14781 for nonzero bitpos. */
14782 if (list_ret == 0)
14783 return 0;
14784 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14786 expansion_failed (loc, NULL_RTX,
14787 "bitfield access");
14788 return 0;
14791 if (offset != NULL_TREE)
14793 /* Variable offset. */
14794 list_ret1 = loc_list_from_tree (offset, 0, context);
14795 if (list_ret1 == 0)
14796 return 0;
14797 add_loc_list (&list_ret, list_ret1);
14798 if (!list_ret)
14799 return 0;
14800 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14803 bytepos = bitpos / BITS_PER_UNIT;
14804 if (bytepos > 0)
14805 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14806 else if (bytepos < 0)
14807 loc_list_plus_const (list_ret, bytepos);
14809 have_address = 1;
14810 break;
14813 case INTEGER_CST:
14814 if ((want_address || !tree_fits_shwi_p (loc))
14815 && (ret = cst_pool_loc_descr (loc)))
14816 have_address = 1;
14817 else if (want_address == 2
14818 && tree_fits_shwi_p (loc)
14819 && (ret = address_of_int_loc_descriptor
14820 (int_size_in_bytes (TREE_TYPE (loc)),
14821 tree_to_shwi (loc))))
14822 have_address = 1;
14823 else if (tree_fits_shwi_p (loc))
14824 ret = int_loc_descriptor (tree_to_shwi (loc));
14825 else
14827 expansion_failed (loc, NULL_RTX,
14828 "Integer operand is not host integer");
14829 return 0;
14831 break;
14833 case CONSTRUCTOR:
14834 case REAL_CST:
14835 case STRING_CST:
14836 case COMPLEX_CST:
14837 if ((ret = cst_pool_loc_descr (loc)))
14838 have_address = 1;
14839 else
14840 /* We can construct small constants here using int_loc_descriptor. */
14841 expansion_failed (loc, NULL_RTX,
14842 "constructor or constant not in constant pool");
14843 break;
14845 case TRUTH_AND_EXPR:
14846 case TRUTH_ANDIF_EXPR:
14847 case BIT_AND_EXPR:
14848 op = DW_OP_and;
14849 goto do_binop;
14851 case TRUTH_XOR_EXPR:
14852 case BIT_XOR_EXPR:
14853 op = DW_OP_xor;
14854 goto do_binop;
14856 case TRUTH_OR_EXPR:
14857 case TRUTH_ORIF_EXPR:
14858 case BIT_IOR_EXPR:
14859 op = DW_OP_or;
14860 goto do_binop;
14862 case FLOOR_DIV_EXPR:
14863 case CEIL_DIV_EXPR:
14864 case ROUND_DIV_EXPR:
14865 case TRUNC_DIV_EXPR:
14866 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14867 return 0;
14868 op = DW_OP_div;
14869 goto do_binop;
14871 case MINUS_EXPR:
14872 op = DW_OP_minus;
14873 goto do_binop;
14875 case FLOOR_MOD_EXPR:
14876 case CEIL_MOD_EXPR:
14877 case ROUND_MOD_EXPR:
14878 case TRUNC_MOD_EXPR:
14879 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14881 op = DW_OP_mod;
14882 goto do_binop;
14884 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14885 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14886 if (list_ret == 0 || list_ret1 == 0)
14887 return 0;
14889 add_loc_list (&list_ret, list_ret1);
14890 if (list_ret == 0)
14891 return 0;
14892 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14893 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14894 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14895 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14896 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14897 break;
14899 case MULT_EXPR:
14900 op = DW_OP_mul;
14901 goto do_binop;
14903 case LSHIFT_EXPR:
14904 op = DW_OP_shl;
14905 goto do_binop;
14907 case RSHIFT_EXPR:
14908 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14909 goto do_binop;
14911 case POINTER_PLUS_EXPR:
14912 case PLUS_EXPR:
14913 do_plus:
14914 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14916 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14917 if (list_ret == 0)
14918 return 0;
14920 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14921 break;
14924 op = DW_OP_plus;
14925 goto do_binop;
14927 case LE_EXPR:
14928 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14929 return 0;
14931 op = DW_OP_le;
14932 goto do_binop;
14934 case GE_EXPR:
14935 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14936 return 0;
14938 op = DW_OP_ge;
14939 goto do_binop;
14941 case LT_EXPR:
14942 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14943 return 0;
14945 op = DW_OP_lt;
14946 goto do_binop;
14948 case GT_EXPR:
14949 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14950 return 0;
14952 op = DW_OP_gt;
14953 goto do_binop;
14955 case EQ_EXPR:
14956 op = DW_OP_eq;
14957 goto do_binop;
14959 case NE_EXPR:
14960 op = DW_OP_ne;
14961 goto do_binop;
14963 do_binop:
14964 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14965 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14966 if (list_ret == 0 || list_ret1 == 0)
14967 return 0;
14969 add_loc_list (&list_ret, list_ret1);
14970 if (list_ret == 0)
14971 return 0;
14972 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14973 break;
14975 case TRUTH_NOT_EXPR:
14976 case BIT_NOT_EXPR:
14977 op = DW_OP_not;
14978 goto do_unop;
14980 case ABS_EXPR:
14981 op = DW_OP_abs;
14982 goto do_unop;
14984 case NEGATE_EXPR:
14985 op = DW_OP_neg;
14986 goto do_unop;
14988 do_unop:
14989 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14990 if (list_ret == 0)
14991 return 0;
14993 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14994 break;
14996 case MIN_EXPR:
14997 case MAX_EXPR:
14999 const enum tree_code code =
15000 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15002 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15003 build2 (code, integer_type_node,
15004 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15005 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15008 /* ... fall through ... */
15010 case COND_EXPR:
15012 dw_loc_descr_ref lhs
15013 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15014 dw_loc_list_ref rhs
15015 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15016 dw_loc_descr_ref bra_node, jump_node, tmp;
15018 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15019 if (list_ret == 0 || lhs == 0 || rhs == 0)
15020 return 0;
15022 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15023 add_loc_descr_to_each (list_ret, bra_node);
15025 add_loc_list (&list_ret, rhs);
15026 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15027 add_loc_descr_to_each (list_ret, jump_node);
15029 add_loc_descr_to_each (list_ret, lhs);
15030 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15031 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15033 /* ??? Need a node to point the skip at. Use a nop. */
15034 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15035 add_loc_descr_to_each (list_ret, tmp);
15036 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15037 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15039 break;
15041 case FIX_TRUNC_EXPR:
15042 return 0;
15044 default:
15045 /* Leave front-end specific codes as simply unknown. This comes
15046 up, for instance, with the C STMT_EXPR. */
15047 if ((unsigned int) TREE_CODE (loc)
15048 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15050 expansion_failed (loc, NULL_RTX,
15051 "language specific tree node");
15052 return 0;
15055 #ifdef ENABLE_CHECKING
15056 /* Otherwise this is a generic code; we should just lists all of
15057 these explicitly. We forgot one. */
15058 gcc_unreachable ();
15059 #else
15060 /* In a release build, we want to degrade gracefully: better to
15061 generate incomplete debugging information than to crash. */
15062 return NULL;
15063 #endif
15066 if (!ret && !list_ret)
15067 return 0;
15069 if (want_address == 2 && !have_address
15070 && (dwarf_version >= 4 || !dwarf_strict))
15072 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15074 expansion_failed (loc, NULL_RTX,
15075 "DWARF address size mismatch");
15076 return 0;
15078 if (ret)
15079 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15080 else
15081 add_loc_descr_to_each (list_ret,
15082 new_loc_descr (DW_OP_stack_value, 0, 0));
15083 have_address = 1;
15085 /* Show if we can't fill the request for an address. */
15086 if (want_address && !have_address)
15088 expansion_failed (loc, NULL_RTX,
15089 "Want address and only have value");
15090 return 0;
15093 gcc_assert (!ret || !list_ret);
15095 /* If we've got an address and don't want one, dereference. */
15096 if (!want_address && have_address)
15098 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15100 if (size > DWARF2_ADDR_SIZE || size == -1)
15102 expansion_failed (loc, NULL_RTX,
15103 "DWARF address size mismatch");
15104 return 0;
15106 else if (size == DWARF2_ADDR_SIZE)
15107 op = DW_OP_deref;
15108 else
15109 op = DW_OP_deref_size;
15111 if (ret)
15112 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15113 else
15114 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15116 if (ret)
15117 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15119 return list_ret;
15122 /* Same as above but return only single location expression. */
15123 static dw_loc_descr_ref
15124 loc_descriptor_from_tree (tree loc, int want_address,
15125 const struct loc_descr_context *context)
15127 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15128 if (!ret)
15129 return NULL;
15130 if (ret->dw_loc_next)
15132 expansion_failed (loc, NULL_RTX,
15133 "Location list where only loc descriptor needed");
15134 return NULL;
15136 return ret->expr;
15139 /* Given a value, round it up to the lowest multiple of `boundary'
15140 which is not less than the value itself. */
15142 static inline HOST_WIDE_INT
15143 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15145 return (((value + boundary - 1) / boundary) * boundary);
15148 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15149 pointer to the declared type for the relevant field variable, or return
15150 `integer_type_node' if the given node turns out to be an
15151 ERROR_MARK node. */
15153 static inline tree
15154 field_type (const_tree decl)
15156 tree type;
15158 if (TREE_CODE (decl) == ERROR_MARK)
15159 return integer_type_node;
15161 type = DECL_BIT_FIELD_TYPE (decl);
15162 if (type == NULL_TREE)
15163 type = TREE_TYPE (decl);
15165 return type;
15168 /* Given a pointer to a tree node, return the alignment in bits for
15169 it, or else return BITS_PER_WORD if the node actually turns out to
15170 be an ERROR_MARK node. */
15172 static inline unsigned
15173 simple_type_align_in_bits (const_tree type)
15175 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15178 static inline unsigned
15179 simple_decl_align_in_bits (const_tree decl)
15181 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15184 /* Return the result of rounding T up to ALIGN. */
15186 static inline offset_int
15187 round_up_to_align (const offset_int &t, unsigned int align)
15189 return wi::udiv_trunc (t + align - 1, align) * align;
15192 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15193 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15194 or return 0 if we are unable to determine what that offset is, either
15195 because the argument turns out to be a pointer to an ERROR_MARK node, or
15196 because the offset is actually variable. (We can't handle the latter case
15197 just yet). */
15199 static HOST_WIDE_INT
15200 field_byte_offset (const_tree decl)
15202 offset_int object_offset_in_bits;
15203 offset_int object_offset_in_bytes;
15204 offset_int bitpos_int;
15206 if (TREE_CODE (decl) == ERROR_MARK)
15207 return 0;
15209 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15211 /* We cannot yet cope with fields whose positions are variable, so
15212 for now, when we see such things, we simply return 0. Someday, we may
15213 be able to handle such cases, but it will be damn difficult. */
15214 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15215 return 0;
15217 bitpos_int = wi::to_offset (bit_position (decl));
15219 if (PCC_BITFIELD_TYPE_MATTERS)
15221 tree type;
15222 tree field_size_tree;
15223 offset_int deepest_bitpos;
15224 offset_int field_size_in_bits;
15225 unsigned int type_align_in_bits;
15226 unsigned int decl_align_in_bits;
15227 offset_int type_size_in_bits;
15229 type = field_type (decl);
15230 type_size_in_bits = offset_int_type_size_in_bits (type);
15231 type_align_in_bits = simple_type_align_in_bits (type);
15233 field_size_tree = DECL_SIZE (decl);
15235 /* The size could be unspecified if there was an error, or for
15236 a flexible array member. */
15237 if (!field_size_tree)
15238 field_size_tree = bitsize_zero_node;
15240 /* If the size of the field is not constant, use the type size. */
15241 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15242 field_size_in_bits = wi::to_offset (field_size_tree);
15243 else
15244 field_size_in_bits = type_size_in_bits;
15246 decl_align_in_bits = simple_decl_align_in_bits (decl);
15248 /* The GCC front-end doesn't make any attempt to keep track of the
15249 starting bit offset (relative to the start of the containing
15250 structure type) of the hypothetical "containing object" for a
15251 bit-field. Thus, when computing the byte offset value for the
15252 start of the "containing object" of a bit-field, we must deduce
15253 this information on our own. This can be rather tricky to do in
15254 some cases. For example, handling the following structure type
15255 definition when compiling for an i386/i486 target (which only
15256 aligns long long's to 32-bit boundaries) can be very tricky:
15258 struct S { int field1; long long field2:31; };
15260 Fortunately, there is a simple rule-of-thumb which can be used
15261 in such cases. When compiling for an i386/i486, GCC will
15262 allocate 8 bytes for the structure shown above. It decides to
15263 do this based upon one simple rule for bit-field allocation.
15264 GCC allocates each "containing object" for each bit-field at
15265 the first (i.e. lowest addressed) legitimate alignment boundary
15266 (based upon the required minimum alignment for the declared
15267 type of the field) which it can possibly use, subject to the
15268 condition that there is still enough available space remaining
15269 in the containing object (when allocated at the selected point)
15270 to fully accommodate all of the bits of the bit-field itself.
15272 This simple rule makes it obvious why GCC allocates 8 bytes for
15273 each object of the structure type shown above. When looking
15274 for a place to allocate the "containing object" for `field2',
15275 the compiler simply tries to allocate a 64-bit "containing
15276 object" at each successive 32-bit boundary (starting at zero)
15277 until it finds a place to allocate that 64- bit field such that
15278 at least 31 contiguous (and previously unallocated) bits remain
15279 within that selected 64 bit field. (As it turns out, for the
15280 example above, the compiler finds it is OK to allocate the
15281 "containing object" 64-bit field at bit-offset zero within the
15282 structure type.)
15284 Here we attempt to work backwards from the limited set of facts
15285 we're given, and we try to deduce from those facts, where GCC
15286 must have believed that the containing object started (within
15287 the structure type). The value we deduce is then used (by the
15288 callers of this routine) to generate DW_AT_location and
15289 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15290 the case of DW_AT_location, regular fields as well). */
15292 /* Figure out the bit-distance from the start of the structure to
15293 the "deepest" bit of the bit-field. */
15294 deepest_bitpos = bitpos_int + field_size_in_bits;
15296 /* This is the tricky part. Use some fancy footwork to deduce
15297 where the lowest addressed bit of the containing object must
15298 be. */
15299 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15301 /* Round up to type_align by default. This works best for
15302 bitfields. */
15303 object_offset_in_bits
15304 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15306 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15308 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15310 /* Round up to decl_align instead. */
15311 object_offset_in_bits
15312 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15315 else
15316 object_offset_in_bits = bitpos_int;
15318 object_offset_in_bytes
15319 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15320 return object_offset_in_bytes.to_shwi ();
15323 /* The following routines define various Dwarf attributes and any data
15324 associated with them. */
15326 /* Add a location description attribute value to a DIE.
15328 This emits location attributes suitable for whole variables and
15329 whole parameters. Note that the location attributes for struct fields are
15330 generated by the routine `data_member_location_attribute' below. */
15332 static inline void
15333 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15334 dw_loc_list_ref descr)
15336 if (descr == 0)
15337 return;
15338 if (single_element_loc_list_p (descr))
15339 add_AT_loc (die, attr_kind, descr->expr);
15340 else
15341 add_AT_loc_list (die, attr_kind, descr);
15344 /* Add DW_AT_accessibility attribute to DIE if needed. */
15346 static void
15347 add_accessibility_attribute (dw_die_ref die, tree decl)
15349 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15350 children, otherwise the default is DW_ACCESS_public. In DWARF2
15351 the default has always been DW_ACCESS_public. */
15352 if (TREE_PROTECTED (decl))
15353 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15354 else if (TREE_PRIVATE (decl))
15356 if (dwarf_version == 2
15357 || die->die_parent == NULL
15358 || die->die_parent->die_tag != DW_TAG_class_type)
15359 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15361 else if (dwarf_version > 2
15362 && die->die_parent
15363 && die->die_parent->die_tag == DW_TAG_class_type)
15364 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15367 /* Attach the specialized form of location attribute used for data members of
15368 struct and union types. In the special case of a FIELD_DECL node which
15369 represents a bit-field, the "offset" part of this special location
15370 descriptor must indicate the distance in bytes from the lowest-addressed
15371 byte of the containing struct or union type to the lowest-addressed byte of
15372 the "containing object" for the bit-field. (See the `field_byte_offset'
15373 function above).
15375 For any given bit-field, the "containing object" is a hypothetical object
15376 (of some integral or enum type) within which the given bit-field lives. The
15377 type of this hypothetical "containing object" is always the same as the
15378 declared type of the individual bit-field itself (for GCC anyway... the
15379 DWARF spec doesn't actually mandate this). Note that it is the size (in
15380 bytes) of the hypothetical "containing object" which will be given in the
15381 DW_AT_byte_size attribute for this bit-field. (See the
15382 `byte_size_attribute' function below.) It is also used when calculating the
15383 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15384 function below.) */
15386 static void
15387 add_data_member_location_attribute (dw_die_ref die, tree decl)
15389 HOST_WIDE_INT offset;
15390 dw_loc_descr_ref loc_descr = 0;
15392 if (TREE_CODE (decl) == TREE_BINFO)
15394 /* We're working on the TAG_inheritance for a base class. */
15395 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15397 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15398 aren't at a fixed offset from all (sub)objects of the same
15399 type. We need to extract the appropriate offset from our
15400 vtable. The following dwarf expression means
15402 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15404 This is specific to the V3 ABI, of course. */
15406 dw_loc_descr_ref tmp;
15408 /* Make a copy of the object address. */
15409 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15410 add_loc_descr (&loc_descr, tmp);
15412 /* Extract the vtable address. */
15413 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15414 add_loc_descr (&loc_descr, tmp);
15416 /* Calculate the address of the offset. */
15417 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15418 gcc_assert (offset < 0);
15420 tmp = int_loc_descriptor (-offset);
15421 add_loc_descr (&loc_descr, tmp);
15422 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15423 add_loc_descr (&loc_descr, tmp);
15425 /* Extract the offset. */
15426 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15427 add_loc_descr (&loc_descr, tmp);
15429 /* Add it to the object address. */
15430 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15431 add_loc_descr (&loc_descr, tmp);
15433 else
15434 offset = tree_to_shwi (BINFO_OFFSET (decl));
15436 else
15437 offset = field_byte_offset (decl);
15439 if (! loc_descr)
15441 if (dwarf_version > 2)
15443 /* Don't need to output a location expression, just the constant. */
15444 if (offset < 0)
15445 add_AT_int (die, DW_AT_data_member_location, offset);
15446 else
15447 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15448 return;
15450 else
15452 enum dwarf_location_atom op;
15454 /* The DWARF2 standard says that we should assume that the structure
15455 address is already on the stack, so we can specify a structure
15456 field address by using DW_OP_plus_uconst. */
15457 op = DW_OP_plus_uconst;
15458 loc_descr = new_loc_descr (op, offset, 0);
15462 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15465 /* Writes integer values to dw_vec_const array. */
15467 static void
15468 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15470 while (size != 0)
15472 *dest++ = val & 0xff;
15473 val >>= 8;
15474 --size;
15478 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15480 static HOST_WIDE_INT
15481 extract_int (const unsigned char *src, unsigned int size)
15483 HOST_WIDE_INT val = 0;
15485 src += size;
15486 while (size != 0)
15488 val <<= 8;
15489 val |= *--src & 0xff;
15490 --size;
15492 return val;
15495 /* Writes wide_int values to dw_vec_const array. */
15497 static void
15498 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15500 int i;
15502 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15504 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15505 return;
15508 /* We'd have to extend this code to support odd sizes. */
15509 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15511 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15513 if (WORDS_BIG_ENDIAN)
15514 for (i = n - 1; i >= 0; i--)
15516 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15517 dest += sizeof (HOST_WIDE_INT);
15519 else
15520 for (i = 0; i < n; i++)
15522 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15523 dest += sizeof (HOST_WIDE_INT);
15527 /* Writes floating point values to dw_vec_const array. */
15529 static void
15530 insert_float (const_rtx rtl, unsigned char *array)
15532 REAL_VALUE_TYPE rv;
15533 long val[4];
15534 int i;
15536 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15537 real_to_target (val, &rv, GET_MODE (rtl));
15539 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15540 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15542 insert_int (val[i], 4, array);
15543 array += 4;
15547 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15548 does not have a "location" either in memory or in a register. These
15549 things can arise in GNU C when a constant is passed as an actual parameter
15550 to an inlined function. They can also arise in C++ where declared
15551 constants do not necessarily get memory "homes". */
15553 static bool
15554 add_const_value_attribute (dw_die_ref die, rtx rtl)
15556 switch (GET_CODE (rtl))
15558 case CONST_INT:
15560 HOST_WIDE_INT val = INTVAL (rtl);
15562 if (val < 0)
15563 add_AT_int (die, DW_AT_const_value, val);
15564 else
15565 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15567 return true;
15569 case CONST_WIDE_INT:
15570 add_AT_wide (die, DW_AT_const_value,
15571 std::make_pair (rtl, GET_MODE (rtl)));
15572 return true;
15574 case CONST_DOUBLE:
15575 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15576 floating-point constant. A CONST_DOUBLE is used whenever the
15577 constant requires more than one word in order to be adequately
15578 represented. */
15580 machine_mode mode = GET_MODE (rtl);
15582 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15583 add_AT_double (die, DW_AT_const_value,
15584 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15585 else
15587 unsigned int length = GET_MODE_SIZE (mode);
15588 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15590 insert_float (rtl, array);
15591 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15594 return true;
15596 case CONST_VECTOR:
15598 machine_mode mode = GET_MODE (rtl);
15599 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15600 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15601 unsigned char *array
15602 = ggc_vec_alloc<unsigned char> (length * elt_size);
15603 unsigned int i;
15604 unsigned char *p;
15605 machine_mode imode = GET_MODE_INNER (mode);
15607 switch (GET_MODE_CLASS (mode))
15609 case MODE_VECTOR_INT:
15610 for (i = 0, p = array; i < length; i++, p += elt_size)
15612 rtx elt = CONST_VECTOR_ELT (rtl, i);
15613 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15615 break;
15617 case MODE_VECTOR_FLOAT:
15618 for (i = 0, p = array; i < length; i++, p += elt_size)
15620 rtx elt = CONST_VECTOR_ELT (rtl, i);
15621 insert_float (elt, p);
15623 break;
15625 default:
15626 gcc_unreachable ();
15629 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15631 return true;
15633 case CONST_STRING:
15634 if (dwarf_version >= 4 || !dwarf_strict)
15636 dw_loc_descr_ref loc_result;
15637 resolve_one_addr (&rtl);
15638 rtl_addr:
15639 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15640 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15641 add_AT_loc (die, DW_AT_location, loc_result);
15642 vec_safe_push (used_rtx_array, rtl);
15643 return true;
15645 return false;
15647 case CONST:
15648 if (CONSTANT_P (XEXP (rtl, 0)))
15649 return add_const_value_attribute (die, XEXP (rtl, 0));
15650 /* FALLTHROUGH */
15651 case SYMBOL_REF:
15652 if (!const_ok_for_output (rtl))
15653 return false;
15654 case LABEL_REF:
15655 if (dwarf_version >= 4 || !dwarf_strict)
15656 goto rtl_addr;
15657 return false;
15659 case PLUS:
15660 /* In cases where an inlined instance of an inline function is passed
15661 the address of an `auto' variable (which is local to the caller) we
15662 can get a situation where the DECL_RTL of the artificial local
15663 variable (for the inlining) which acts as a stand-in for the
15664 corresponding formal parameter (of the inline function) will look
15665 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15666 exactly a compile-time constant expression, but it isn't the address
15667 of the (artificial) local variable either. Rather, it represents the
15668 *value* which the artificial local variable always has during its
15669 lifetime. We currently have no way to represent such quasi-constant
15670 values in Dwarf, so for now we just punt and generate nothing. */
15671 return false;
15673 case HIGH:
15674 case CONST_FIXED:
15675 return false;
15677 case MEM:
15678 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15679 && MEM_READONLY_P (rtl)
15680 && GET_MODE (rtl) == BLKmode)
15682 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15683 return true;
15685 return false;
15687 default:
15688 /* No other kinds of rtx should be possible here. */
15689 gcc_unreachable ();
15691 return false;
15694 /* Determine whether the evaluation of EXPR references any variables
15695 or functions which aren't otherwise used (and therefore may not be
15696 output). */
15697 static tree
15698 reference_to_unused (tree * tp, int * walk_subtrees,
15699 void * data ATTRIBUTE_UNUSED)
15701 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15702 *walk_subtrees = 0;
15704 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15705 && ! TREE_ASM_WRITTEN (*tp))
15706 return *tp;
15707 /* ??? The C++ FE emits debug information for using decls, so
15708 putting gcc_unreachable here falls over. See PR31899. For now
15709 be conservative. */
15710 else if (!symtab->global_info_ready
15711 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15712 return *tp;
15713 else if (TREE_CODE (*tp) == VAR_DECL)
15715 varpool_node *node = varpool_node::get (*tp);
15716 if (!node || !node->definition)
15717 return *tp;
15719 else if (TREE_CODE (*tp) == FUNCTION_DECL
15720 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15722 /* The call graph machinery must have finished analyzing,
15723 optimizing and gimplifying the CU by now.
15724 So if *TP has no call graph node associated
15725 to it, it means *TP will not be emitted. */
15726 if (!cgraph_node::get (*tp))
15727 return *tp;
15729 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15730 return *tp;
15732 return NULL_TREE;
15735 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15736 for use in a later add_const_value_attribute call. */
15738 static rtx
15739 rtl_for_decl_init (tree init, tree type)
15741 rtx rtl = NULL_RTX;
15743 STRIP_NOPS (init);
15745 /* If a variable is initialized with a string constant without embedded
15746 zeros, build CONST_STRING. */
15747 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15749 tree enttype = TREE_TYPE (type);
15750 tree domain = TYPE_DOMAIN (type);
15751 machine_mode mode = TYPE_MODE (enttype);
15753 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15754 && domain
15755 && integer_zerop (TYPE_MIN_VALUE (domain))
15756 && compare_tree_int (TYPE_MAX_VALUE (domain),
15757 TREE_STRING_LENGTH (init) - 1) == 0
15758 && ((size_t) TREE_STRING_LENGTH (init)
15759 == strlen (TREE_STRING_POINTER (init)) + 1))
15761 rtl = gen_rtx_CONST_STRING (VOIDmode,
15762 ggc_strdup (TREE_STRING_POINTER (init)));
15763 rtl = gen_rtx_MEM (BLKmode, rtl);
15764 MEM_READONLY_P (rtl) = 1;
15767 /* Other aggregates, and complex values, could be represented using
15768 CONCAT: FIXME! */
15769 else if (AGGREGATE_TYPE_P (type)
15770 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15771 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15772 || TREE_CODE (type) == COMPLEX_TYPE)
15774 /* Vectors only work if their mode is supported by the target.
15775 FIXME: generic vectors ought to work too. */
15776 else if (TREE_CODE (type) == VECTOR_TYPE
15777 && !VECTOR_MODE_P (TYPE_MODE (type)))
15779 /* If the initializer is something that we know will expand into an
15780 immediate RTL constant, expand it now. We must be careful not to
15781 reference variables which won't be output. */
15782 else if (initializer_constant_valid_p (init, type)
15783 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15785 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15786 possible. */
15787 if (TREE_CODE (type) == VECTOR_TYPE)
15788 switch (TREE_CODE (init))
15790 case VECTOR_CST:
15791 break;
15792 case CONSTRUCTOR:
15793 if (TREE_CONSTANT (init))
15795 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15796 bool constant_p = true;
15797 tree value;
15798 unsigned HOST_WIDE_INT ix;
15800 /* Even when ctor is constant, it might contain non-*_CST
15801 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15802 belong into VECTOR_CST nodes. */
15803 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15804 if (!CONSTANT_CLASS_P (value))
15806 constant_p = false;
15807 break;
15810 if (constant_p)
15812 init = build_vector_from_ctor (type, elts);
15813 break;
15816 /* FALLTHRU */
15818 default:
15819 return NULL;
15822 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15824 /* If expand_expr returns a MEM, it wasn't immediate. */
15825 gcc_assert (!rtl || !MEM_P (rtl));
15828 return rtl;
15831 /* Generate RTL for the variable DECL to represent its location. */
15833 static rtx
15834 rtl_for_decl_location (tree decl)
15836 rtx rtl;
15838 /* Here we have to decide where we are going to say the parameter "lives"
15839 (as far as the debugger is concerned). We only have a couple of
15840 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15842 DECL_RTL normally indicates where the parameter lives during most of the
15843 activation of the function. If optimization is enabled however, this
15844 could be either NULL or else a pseudo-reg. Both of those cases indicate
15845 that the parameter doesn't really live anywhere (as far as the code
15846 generation parts of GCC are concerned) during most of the function's
15847 activation. That will happen (for example) if the parameter is never
15848 referenced within the function.
15850 We could just generate a location descriptor here for all non-NULL
15851 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15852 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15853 where DECL_RTL is NULL or is a pseudo-reg.
15855 Note however that we can only get away with using DECL_INCOMING_RTL as
15856 a backup substitute for DECL_RTL in certain limited cases. In cases
15857 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15858 we can be sure that the parameter was passed using the same type as it is
15859 declared to have within the function, and that its DECL_INCOMING_RTL
15860 points us to a place where a value of that type is passed.
15862 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15863 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15864 because in these cases DECL_INCOMING_RTL points us to a value of some
15865 type which is *different* from the type of the parameter itself. Thus,
15866 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15867 such cases, the debugger would end up (for example) trying to fetch a
15868 `float' from a place which actually contains the first part of a
15869 `double'. That would lead to really incorrect and confusing
15870 output at debug-time.
15872 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15873 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15874 are a couple of exceptions however. On little-endian machines we can
15875 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15876 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15877 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15878 when (on a little-endian machine) a non-prototyped function has a
15879 parameter declared to be of type `short' or `char'. In such cases,
15880 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15881 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15882 passed `int' value. If the debugger then uses that address to fetch
15883 a `short' or a `char' (on a little-endian machine) the result will be
15884 the correct data, so we allow for such exceptional cases below.
15886 Note that our goal here is to describe the place where the given formal
15887 parameter lives during most of the function's activation (i.e. between the
15888 end of the prologue and the start of the epilogue). We'll do that as best
15889 as we can. Note however that if the given formal parameter is modified
15890 sometime during the execution of the function, then a stack backtrace (at
15891 debug-time) will show the function as having been called with the *new*
15892 value rather than the value which was originally passed in. This happens
15893 rarely enough that it is not a major problem, but it *is* a problem, and
15894 I'd like to fix it.
15896 A future version of dwarf2out.c may generate two additional attributes for
15897 any given DW_TAG_formal_parameter DIE which will describe the "passed
15898 type" and the "passed location" for the given formal parameter in addition
15899 to the attributes we now generate to indicate the "declared type" and the
15900 "active location" for each parameter. This additional set of attributes
15901 could be used by debuggers for stack backtraces. Separately, note that
15902 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15903 This happens (for example) for inlined-instances of inline function formal
15904 parameters which are never referenced. This really shouldn't be
15905 happening. All PARM_DECL nodes should get valid non-NULL
15906 DECL_INCOMING_RTL values. FIXME. */
15908 /* Use DECL_RTL as the "location" unless we find something better. */
15909 rtl = DECL_RTL_IF_SET (decl);
15911 /* When generating abstract instances, ignore everything except
15912 constants, symbols living in memory, and symbols living in
15913 fixed registers. */
15914 if (! reload_completed)
15916 if (rtl
15917 && (CONSTANT_P (rtl)
15918 || (MEM_P (rtl)
15919 && CONSTANT_P (XEXP (rtl, 0)))
15920 || (REG_P (rtl)
15921 && TREE_CODE (decl) == VAR_DECL
15922 && TREE_STATIC (decl))))
15924 rtl = targetm.delegitimize_address (rtl);
15925 return rtl;
15927 rtl = NULL_RTX;
15929 else if (TREE_CODE (decl) == PARM_DECL)
15931 if (rtl == NULL_RTX
15932 || is_pseudo_reg (rtl)
15933 || (MEM_P (rtl)
15934 && is_pseudo_reg (XEXP (rtl, 0))
15935 && DECL_INCOMING_RTL (decl)
15936 && MEM_P (DECL_INCOMING_RTL (decl))
15937 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15939 tree declared_type = TREE_TYPE (decl);
15940 tree passed_type = DECL_ARG_TYPE (decl);
15941 machine_mode dmode = TYPE_MODE (declared_type);
15942 machine_mode pmode = TYPE_MODE (passed_type);
15944 /* This decl represents a formal parameter which was optimized out.
15945 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15946 all cases where (rtl == NULL_RTX) just below. */
15947 if (dmode == pmode)
15948 rtl = DECL_INCOMING_RTL (decl);
15949 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15950 && SCALAR_INT_MODE_P (dmode)
15951 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15952 && DECL_INCOMING_RTL (decl))
15954 rtx inc = DECL_INCOMING_RTL (decl);
15955 if (REG_P (inc))
15956 rtl = inc;
15957 else if (MEM_P (inc))
15959 if (BYTES_BIG_ENDIAN)
15960 rtl = adjust_address_nv (inc, dmode,
15961 GET_MODE_SIZE (pmode)
15962 - GET_MODE_SIZE (dmode));
15963 else
15964 rtl = inc;
15969 /* If the parm was passed in registers, but lives on the stack, then
15970 make a big endian correction if the mode of the type of the
15971 parameter is not the same as the mode of the rtl. */
15972 /* ??? This is the same series of checks that are made in dbxout.c before
15973 we reach the big endian correction code there. It isn't clear if all
15974 of these checks are necessary here, but keeping them all is the safe
15975 thing to do. */
15976 else if (MEM_P (rtl)
15977 && XEXP (rtl, 0) != const0_rtx
15978 && ! CONSTANT_P (XEXP (rtl, 0))
15979 /* Not passed in memory. */
15980 && !MEM_P (DECL_INCOMING_RTL (decl))
15981 /* Not passed by invisible reference. */
15982 && (!REG_P (XEXP (rtl, 0))
15983 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15984 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15985 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15986 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15987 #endif
15989 /* Big endian correction check. */
15990 && BYTES_BIG_ENDIAN
15991 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15992 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15993 < UNITS_PER_WORD))
15995 machine_mode addr_mode = get_address_mode (rtl);
15996 int offset = (UNITS_PER_WORD
15997 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15999 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16000 plus_constant (addr_mode, XEXP (rtl, 0), offset));
16003 else if (TREE_CODE (decl) == VAR_DECL
16004 && rtl
16005 && MEM_P (rtl)
16006 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16007 && BYTES_BIG_ENDIAN)
16009 machine_mode addr_mode = get_address_mode (rtl);
16010 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16011 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16013 /* If a variable is declared "register" yet is smaller than
16014 a register, then if we store the variable to memory, it
16015 looks like we're storing a register-sized value, when in
16016 fact we are not. We need to adjust the offset of the
16017 storage location to reflect the actual value's bytes,
16018 else gdb will not be able to display it. */
16019 if (rsize > dsize)
16020 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16021 plus_constant (addr_mode, XEXP (rtl, 0),
16022 rsize - dsize));
16025 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16026 and will have been substituted directly into all expressions that use it.
16027 C does not have such a concept, but C++ and other languages do. */
16028 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16029 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16031 if (rtl)
16032 rtl = targetm.delegitimize_address (rtl);
16034 /* If we don't look past the constant pool, we risk emitting a
16035 reference to a constant pool entry that isn't referenced from
16036 code, and thus is not emitted. */
16037 if (rtl)
16038 rtl = avoid_constant_pool_reference (rtl);
16040 /* Try harder to get a rtl. If this symbol ends up not being emitted
16041 in the current CU, resolve_addr will remove the expression referencing
16042 it. */
16043 if (rtl == NULL_RTX
16044 && TREE_CODE (decl) == VAR_DECL
16045 && !DECL_EXTERNAL (decl)
16046 && TREE_STATIC (decl)
16047 && DECL_NAME (decl)
16048 && !DECL_HARD_REGISTER (decl)
16049 && DECL_MODE (decl) != VOIDmode)
16051 rtl = make_decl_rtl_for_debug (decl);
16052 if (!MEM_P (rtl)
16053 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16054 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16055 rtl = NULL_RTX;
16058 return rtl;
16061 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16062 returned. If so, the decl for the COMMON block is returned, and the
16063 value is the offset into the common block for the symbol. */
16065 static tree
16066 fortran_common (tree decl, HOST_WIDE_INT *value)
16068 tree val_expr, cvar;
16069 machine_mode mode;
16070 HOST_WIDE_INT bitsize, bitpos;
16071 tree offset;
16072 int unsignedp, reversep, volatilep = 0;
16074 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16075 it does not have a value (the offset into the common area), or if it
16076 is thread local (as opposed to global) then it isn't common, and shouldn't
16077 be handled as such. */
16078 if (TREE_CODE (decl) != VAR_DECL
16079 || !TREE_STATIC (decl)
16080 || !DECL_HAS_VALUE_EXPR_P (decl)
16081 || !is_fortran ())
16082 return NULL_TREE;
16084 val_expr = DECL_VALUE_EXPR (decl);
16085 if (TREE_CODE (val_expr) != COMPONENT_REF)
16086 return NULL_TREE;
16088 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
16089 &unsignedp, &reversep, &volatilep, true);
16091 if (cvar == NULL_TREE
16092 || TREE_CODE (cvar) != VAR_DECL
16093 || DECL_ARTIFICIAL (cvar)
16094 || !TREE_PUBLIC (cvar))
16095 return NULL_TREE;
16097 *value = 0;
16098 if (offset != NULL)
16100 if (!tree_fits_shwi_p (offset))
16101 return NULL_TREE;
16102 *value = tree_to_shwi (offset);
16104 if (bitpos != 0)
16105 *value += bitpos / BITS_PER_UNIT;
16107 return cvar;
16110 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16111 data attribute for a variable or a parameter. We generate the
16112 DW_AT_const_value attribute only in those cases where the given variable
16113 or parameter does not have a true "location" either in memory or in a
16114 register. This can happen (for example) when a constant is passed as an
16115 actual argument in a call to an inline function. (It's possible that
16116 these things can crop up in other ways also.) Note that one type of
16117 constant value which can be passed into an inlined function is a constant
16118 pointer. This can happen for example if an actual argument in an inlined
16119 function call evaluates to a compile-time constant address.
16121 CACHE_P is true if it is worth caching the location list for DECL,
16122 so that future calls can reuse it rather than regenerate it from scratch.
16123 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16124 since we will need to refer to them each time the function is inlined. */
16126 static bool
16127 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16128 enum dwarf_attribute attr)
16130 rtx rtl;
16131 dw_loc_list_ref list;
16132 var_loc_list *loc_list;
16133 cached_dw_loc_list *cache;
16135 if (TREE_CODE (decl) == ERROR_MARK)
16136 return false;
16138 if (get_AT (die, attr))
16139 return true;
16141 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16142 || TREE_CODE (decl) == RESULT_DECL);
16144 /* Try to get some constant RTL for this decl, and use that as the value of
16145 the location. */
16147 rtl = rtl_for_decl_location (decl);
16148 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16149 && add_const_value_attribute (die, rtl))
16150 return true;
16152 /* See if we have single element location list that is equivalent to
16153 a constant value. That way we are better to use add_const_value_attribute
16154 rather than expanding constant value equivalent. */
16155 loc_list = lookup_decl_loc (decl);
16156 if (loc_list
16157 && loc_list->first
16158 && loc_list->first->next == NULL
16159 && NOTE_P (loc_list->first->loc)
16160 && NOTE_VAR_LOCATION (loc_list->first->loc)
16161 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16163 struct var_loc_node *node;
16165 node = loc_list->first;
16166 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16167 if (GET_CODE (rtl) == EXPR_LIST)
16168 rtl = XEXP (rtl, 0);
16169 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16170 && add_const_value_attribute (die, rtl))
16171 return true;
16173 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16174 list several times. See if we've already cached the contents. */
16175 list = NULL;
16176 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16177 cache_p = false;
16178 if (cache_p)
16180 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16181 if (cache)
16182 list = cache->loc_list;
16184 if (list == NULL)
16186 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16187 NULL);
16188 /* It is usually worth caching this result if the decl is from
16189 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16190 if (cache_p && list && list->dw_loc_next)
16192 cached_dw_loc_list **slot
16193 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16194 DECL_UID (decl),
16195 INSERT);
16196 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16197 cache->decl_id = DECL_UID (decl);
16198 cache->loc_list = list;
16199 *slot = cache;
16202 if (list)
16204 add_AT_location_description (die, attr, list);
16205 return true;
16207 /* None of that worked, so it must not really have a location;
16208 try adding a constant value attribute from the DECL_INITIAL. */
16209 return tree_add_const_value_attribute_for_decl (die, decl);
16212 /* Helper function for tree_add_const_value_attribute. Natively encode
16213 initializer INIT into an array. Return true if successful. */
16215 static bool
16216 native_encode_initializer (tree init, unsigned char *array, int size)
16218 tree type;
16220 if (init == NULL_TREE)
16221 return false;
16223 STRIP_NOPS (init);
16224 switch (TREE_CODE (init))
16226 case STRING_CST:
16227 type = TREE_TYPE (init);
16228 if (TREE_CODE (type) == ARRAY_TYPE)
16230 tree enttype = TREE_TYPE (type);
16231 machine_mode mode = TYPE_MODE (enttype);
16233 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16234 return false;
16235 if (int_size_in_bytes (type) != size)
16236 return false;
16237 if (size > TREE_STRING_LENGTH (init))
16239 memcpy (array, TREE_STRING_POINTER (init),
16240 TREE_STRING_LENGTH (init));
16241 memset (array + TREE_STRING_LENGTH (init),
16242 '\0', size - TREE_STRING_LENGTH (init));
16244 else
16245 memcpy (array, TREE_STRING_POINTER (init), size);
16246 return true;
16248 return false;
16249 case CONSTRUCTOR:
16250 type = TREE_TYPE (init);
16251 if (int_size_in_bytes (type) != size)
16252 return false;
16253 if (TREE_CODE (type) == ARRAY_TYPE)
16255 HOST_WIDE_INT min_index;
16256 unsigned HOST_WIDE_INT cnt;
16257 int curpos = 0, fieldsize;
16258 constructor_elt *ce;
16260 if (TYPE_DOMAIN (type) == NULL_TREE
16261 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16262 return false;
16264 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16265 if (fieldsize <= 0)
16266 return false;
16268 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16269 memset (array, '\0', size);
16270 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16272 tree val = ce->value;
16273 tree index = ce->index;
16274 int pos = curpos;
16275 if (index && TREE_CODE (index) == RANGE_EXPR)
16276 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16277 * fieldsize;
16278 else if (index)
16279 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16281 if (val)
16283 STRIP_NOPS (val);
16284 if (!native_encode_initializer (val, array + pos, fieldsize))
16285 return false;
16287 curpos = pos + fieldsize;
16288 if (index && TREE_CODE (index) == RANGE_EXPR)
16290 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16291 - tree_to_shwi (TREE_OPERAND (index, 0));
16292 while (count-- > 0)
16294 if (val)
16295 memcpy (array + curpos, array + pos, fieldsize);
16296 curpos += fieldsize;
16299 gcc_assert (curpos <= size);
16301 return true;
16303 else if (TREE_CODE (type) == RECORD_TYPE
16304 || TREE_CODE (type) == UNION_TYPE)
16306 tree field = NULL_TREE;
16307 unsigned HOST_WIDE_INT cnt;
16308 constructor_elt *ce;
16310 if (int_size_in_bytes (type) != size)
16311 return false;
16313 if (TREE_CODE (type) == RECORD_TYPE)
16314 field = TYPE_FIELDS (type);
16316 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16318 tree val = ce->value;
16319 int pos, fieldsize;
16321 if (ce->index != 0)
16322 field = ce->index;
16324 if (val)
16325 STRIP_NOPS (val);
16327 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16328 return false;
16330 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16331 && TYPE_DOMAIN (TREE_TYPE (field))
16332 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16333 return false;
16334 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16335 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16336 return false;
16337 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16338 pos = int_byte_position (field);
16339 gcc_assert (pos + fieldsize <= size);
16340 if (val
16341 && !native_encode_initializer (val, array + pos, fieldsize))
16342 return false;
16344 return true;
16346 return false;
16347 case VIEW_CONVERT_EXPR:
16348 case NON_LVALUE_EXPR:
16349 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16350 default:
16351 return native_encode_expr (init, array, size) == size;
16355 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16356 attribute is the const value T. */
16358 static bool
16359 tree_add_const_value_attribute (dw_die_ref die, tree t)
16361 tree init;
16362 tree type = TREE_TYPE (t);
16363 rtx rtl;
16365 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16366 return false;
16368 init = t;
16369 gcc_assert (!DECL_P (init));
16371 rtl = rtl_for_decl_init (init, type);
16372 if (rtl)
16373 return add_const_value_attribute (die, rtl);
16374 /* If the host and target are sane, try harder. */
16375 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16376 && initializer_constant_valid_p (init, type))
16378 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16379 if (size > 0 && (int) size == size)
16381 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16383 if (native_encode_initializer (init, array, size))
16385 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16386 return true;
16388 ggc_free (array);
16391 return false;
16394 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16395 attribute is the const value of T, where T is an integral constant
16396 variable with static storage duration
16397 (so it can't be a PARM_DECL or a RESULT_DECL). */
16399 static bool
16400 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16403 if (!decl
16404 || (TREE_CODE (decl) != VAR_DECL
16405 && TREE_CODE (decl) != CONST_DECL)
16406 || (TREE_CODE (decl) == VAR_DECL
16407 && !TREE_STATIC (decl)))
16408 return false;
16410 if (TREE_READONLY (decl)
16411 && ! TREE_THIS_VOLATILE (decl)
16412 && DECL_INITIAL (decl))
16413 /* OK */;
16414 else
16415 return false;
16417 /* Don't add DW_AT_const_value if abstract origin already has one. */
16418 if (get_AT (var_die, DW_AT_const_value))
16419 return false;
16421 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16424 /* Convert the CFI instructions for the current function into a
16425 location list. This is used for DW_AT_frame_base when we targeting
16426 a dwarf2 consumer that does not support the dwarf3
16427 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16428 expressions. */
16430 static dw_loc_list_ref
16431 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16433 int ix;
16434 dw_fde_ref fde;
16435 dw_loc_list_ref list, *list_tail;
16436 dw_cfi_ref cfi;
16437 dw_cfa_location last_cfa, next_cfa;
16438 const char *start_label, *last_label, *section;
16439 dw_cfa_location remember;
16441 fde = cfun->fde;
16442 gcc_assert (fde != NULL);
16444 section = secname_for_decl (current_function_decl);
16445 list_tail = &list;
16446 list = NULL;
16448 memset (&next_cfa, 0, sizeof (next_cfa));
16449 next_cfa.reg = INVALID_REGNUM;
16450 remember = next_cfa;
16452 start_label = fde->dw_fde_begin;
16454 /* ??? Bald assumption that the CIE opcode list does not contain
16455 advance opcodes. */
16456 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16457 lookup_cfa_1 (cfi, &next_cfa, &remember);
16459 last_cfa = next_cfa;
16460 last_label = start_label;
16462 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16464 /* If the first partition contained no CFI adjustments, the
16465 CIE opcodes apply to the whole first partition. */
16466 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16467 fde->dw_fde_begin, fde->dw_fde_end, section);
16468 list_tail =&(*list_tail)->dw_loc_next;
16469 start_label = last_label = fde->dw_fde_second_begin;
16472 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16474 switch (cfi->dw_cfi_opc)
16476 case DW_CFA_set_loc:
16477 case DW_CFA_advance_loc1:
16478 case DW_CFA_advance_loc2:
16479 case DW_CFA_advance_loc4:
16480 if (!cfa_equal_p (&last_cfa, &next_cfa))
16482 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16483 start_label, last_label, section);
16485 list_tail = &(*list_tail)->dw_loc_next;
16486 last_cfa = next_cfa;
16487 start_label = last_label;
16489 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16490 break;
16492 case DW_CFA_advance_loc:
16493 /* The encoding is complex enough that we should never emit this. */
16494 gcc_unreachable ();
16496 default:
16497 lookup_cfa_1 (cfi, &next_cfa, &remember);
16498 break;
16500 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16502 if (!cfa_equal_p (&last_cfa, &next_cfa))
16504 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16505 start_label, last_label, section);
16507 list_tail = &(*list_tail)->dw_loc_next;
16508 last_cfa = next_cfa;
16509 start_label = last_label;
16511 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16512 start_label, fde->dw_fde_end, section);
16513 list_tail = &(*list_tail)->dw_loc_next;
16514 start_label = last_label = fde->dw_fde_second_begin;
16518 if (!cfa_equal_p (&last_cfa, &next_cfa))
16520 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16521 start_label, last_label, section);
16522 list_tail = &(*list_tail)->dw_loc_next;
16523 start_label = last_label;
16526 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16527 start_label,
16528 fde->dw_fde_second_begin
16529 ? fde->dw_fde_second_end : fde->dw_fde_end,
16530 section);
16532 if (list && list->dw_loc_next)
16533 gen_llsym (list);
16535 return list;
16538 /* Compute a displacement from the "steady-state frame pointer" to the
16539 frame base (often the same as the CFA), and store it in
16540 frame_pointer_fb_offset. OFFSET is added to the displacement
16541 before the latter is negated. */
16543 static void
16544 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16546 rtx reg, elim;
16548 #ifdef FRAME_POINTER_CFA_OFFSET
16549 reg = frame_pointer_rtx;
16550 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16551 #else
16552 reg = arg_pointer_rtx;
16553 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16554 #endif
16556 elim = (ira_use_lra_p
16557 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16558 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16559 if (GET_CODE (elim) == PLUS)
16561 offset += INTVAL (XEXP (elim, 1));
16562 elim = XEXP (elim, 0);
16565 frame_pointer_fb_offset = -offset;
16567 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16568 in which to eliminate. This is because it's stack pointer isn't
16569 directly accessible as a register within the ISA. To work around
16570 this, assume that while we cannot provide a proper value for
16571 frame_pointer_fb_offset, we won't need one either. */
16572 frame_pointer_fb_offset_valid
16573 = ((SUPPORTS_STACK_ALIGNMENT
16574 && (elim == hard_frame_pointer_rtx
16575 || elim == stack_pointer_rtx))
16576 || elim == (frame_pointer_needed
16577 ? hard_frame_pointer_rtx
16578 : stack_pointer_rtx));
16581 /* Generate a DW_AT_name attribute given some string value to be included as
16582 the value of the attribute. */
16584 static void
16585 add_name_attribute (dw_die_ref die, const char *name_string)
16587 if (name_string != NULL && *name_string != 0)
16589 if (demangle_name_func)
16590 name_string = (*demangle_name_func) (name_string);
16592 add_AT_string (die, DW_AT_name, name_string);
16596 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16597 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16598 of TYPE accordingly.
16600 ??? This is a temporary measure until after we're able to generate
16601 regular DWARF for the complex Ada type system. */
16603 static void
16604 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16605 dw_die_ref context_die)
16607 tree dtype;
16608 dw_die_ref dtype_die;
16610 if (!lang_hooks.types.descriptive_type)
16611 return;
16613 dtype = lang_hooks.types.descriptive_type (type);
16614 if (!dtype)
16615 return;
16617 dtype_die = lookup_type_die (dtype);
16618 if (!dtype_die)
16620 gen_type_die (dtype, context_die);
16621 dtype_die = lookup_type_die (dtype);
16622 gcc_assert (dtype_die);
16625 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16628 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16630 static const char *
16631 comp_dir_string (void)
16633 const char *wd;
16634 char *wd1;
16635 static const char *cached_wd = NULL;
16637 if (cached_wd != NULL)
16638 return cached_wd;
16640 wd = get_src_pwd ();
16641 if (wd == NULL)
16642 return NULL;
16644 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16646 int wdlen;
16648 wdlen = strlen (wd);
16649 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16650 strcpy (wd1, wd);
16651 wd1 [wdlen] = DIR_SEPARATOR;
16652 wd1 [wdlen + 1] = 0;
16653 wd = wd1;
16656 cached_wd = remap_debug_filename (wd);
16657 return cached_wd;
16660 /* Generate a DW_AT_comp_dir attribute for DIE. */
16662 static void
16663 add_comp_dir_attribute (dw_die_ref die)
16665 const char * wd = comp_dir_string ();
16666 if (wd != NULL)
16667 add_AT_string (die, DW_AT_comp_dir, wd);
16670 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16671 pointer computation, ...), output a representation for that bound according
16672 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16673 loc_list_from_tree for the meaning of CONTEXT. */
16675 static void
16676 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16677 int forms, const struct loc_descr_context *context)
16679 dw_die_ref ctx, decl_die;
16680 dw_loc_list_ref list;
16682 bool strip_conversions = true;
16684 while (strip_conversions)
16685 switch (TREE_CODE (value))
16687 case ERROR_MARK:
16688 case SAVE_EXPR:
16689 return;
16691 CASE_CONVERT:
16692 case VIEW_CONVERT_EXPR:
16693 value = TREE_OPERAND (value, 0);
16694 break;
16696 default:
16697 strip_conversions = false;
16698 break;
16701 /* If possible and permitted, output the attribute as a constant. */
16702 if ((forms & dw_scalar_form_constant) != 0
16703 && TREE_CODE (value) == INTEGER_CST)
16705 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16707 /* If HOST_WIDE_INT is big enough then represent the bound as
16708 a constant value. We need to choose a form based on
16709 whether the type is signed or unsigned. We cannot just
16710 call add_AT_unsigned if the value itself is positive
16711 (add_AT_unsigned might add the unsigned value encoded as
16712 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16713 bounds type and then sign extend any unsigned values found
16714 for signed types. This is needed only for
16715 DW_AT_{lower,upper}_bound, since for most other attributes,
16716 consumers will treat DW_FORM_data[1248] as unsigned values,
16717 regardless of the underlying type. */
16718 if (prec <= HOST_BITS_PER_WIDE_INT
16719 || tree_fits_uhwi_p (value))
16721 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16722 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16723 else
16724 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16726 else
16727 /* Otherwise represent the bound as an unsigned value with
16728 the precision of its type. The precision and signedness
16729 of the type will be necessary to re-interpret it
16730 unambiguously. */
16731 add_AT_wide (die, attr, value);
16732 return;
16735 /* Otherwise, if it's possible and permitted too, output a reference to
16736 another DIE. */
16737 if ((forms & dw_scalar_form_reference) != 0)
16739 tree decl = NULL_TREE;
16741 /* Some type attributes reference an outer type. For instance, the upper
16742 bound of an array may reference an embedding record (this happens in
16743 Ada). */
16744 if (TREE_CODE (value) == COMPONENT_REF
16745 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16746 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16747 decl = TREE_OPERAND (value, 1);
16749 else if (TREE_CODE (value) == VAR_DECL
16750 || TREE_CODE (value) == PARM_DECL
16751 || TREE_CODE (value) == RESULT_DECL)
16752 decl = value;
16754 if (decl != NULL_TREE)
16756 dw_die_ref decl_die = lookup_decl_die (decl);
16758 /* ??? Can this happen, or should the variable have been bound
16759 first? Probably it can, since I imagine that we try to create
16760 the types of parameters in the order in which they exist in
16761 the list, and won't have created a forward reference to a
16762 later parameter. */
16763 if (decl_die != NULL)
16765 add_AT_die_ref (die, attr, decl_die);
16766 return;
16771 /* Last chance: try to create a stack operation procedure to evaluate the
16772 value. Do nothing if even that is not possible or permitted. */
16773 if ((forms & dw_scalar_form_exprloc) == 0)
16774 return;
16776 list = loc_list_from_tree (value, 2, context);
16777 if (list == NULL || single_element_loc_list_p (list))
16779 /* If this attribute is not a reference nor constant, it is
16780 a DWARF expression rather than location description. For that
16781 loc_list_from_tree (value, 0, &context) is needed. */
16782 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16783 if (list2 && single_element_loc_list_p (list2))
16785 add_AT_loc (die, attr, list2->expr);
16786 return;
16790 /* If that failed to give a single element location list, fall back to
16791 outputting this as a reference... still if permitted. */
16792 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16793 return;
16795 if (current_function_decl == 0)
16796 ctx = comp_unit_die ();
16797 else
16798 ctx = lookup_decl_die (current_function_decl);
16800 decl_die = new_die (DW_TAG_variable, ctx, value);
16801 add_AT_flag (decl_die, DW_AT_artificial, 1);
16802 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16803 add_AT_location_description (decl_die, DW_AT_location, list);
16804 add_AT_die_ref (die, attr, decl_die);
16807 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16808 default. */
16810 static int
16811 lower_bound_default (void)
16813 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16815 case DW_LANG_C:
16816 case DW_LANG_C89:
16817 case DW_LANG_C99:
16818 case DW_LANG_C11:
16819 case DW_LANG_C_plus_plus:
16820 case DW_LANG_C_plus_plus_11:
16821 case DW_LANG_C_plus_plus_14:
16822 case DW_LANG_ObjC:
16823 case DW_LANG_ObjC_plus_plus:
16824 case DW_LANG_Java:
16825 return 0;
16826 case DW_LANG_Fortran77:
16827 case DW_LANG_Fortran90:
16828 case DW_LANG_Fortran95:
16829 case DW_LANG_Fortran03:
16830 case DW_LANG_Fortran08:
16831 return 1;
16832 case DW_LANG_UPC:
16833 case DW_LANG_D:
16834 case DW_LANG_Python:
16835 return dwarf_version >= 4 ? 0 : -1;
16836 case DW_LANG_Ada95:
16837 case DW_LANG_Ada83:
16838 case DW_LANG_Cobol74:
16839 case DW_LANG_Cobol85:
16840 case DW_LANG_Pascal83:
16841 case DW_LANG_Modula2:
16842 case DW_LANG_PLI:
16843 return dwarf_version >= 4 ? 1 : -1;
16844 default:
16845 return -1;
16849 /* Given a tree node describing an array bound (either lower or upper) output
16850 a representation for that bound. */
16852 static void
16853 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16854 tree bound, const struct loc_descr_context *context)
16856 int dflt;
16858 while (1)
16859 switch (TREE_CODE (bound))
16861 /* Strip all conversions. */
16862 CASE_CONVERT:
16863 case VIEW_CONVERT_EXPR:
16864 bound = TREE_OPERAND (bound, 0);
16865 break;
16867 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16868 are even omitted when they are the default. */
16869 case INTEGER_CST:
16870 /* If the value for this bound is the default one, we can even omit the
16871 attribute. */
16872 if (bound_attr == DW_AT_lower_bound
16873 && tree_fits_shwi_p (bound)
16874 && (dflt = lower_bound_default ()) != -1
16875 && tree_to_shwi (bound) == dflt)
16876 return;
16878 /* FALLTHRU */
16880 default:
16881 add_scalar_info (subrange_die, bound_attr, bound,
16882 dw_scalar_form_constant
16883 | dw_scalar_form_exprloc
16884 | dw_scalar_form_reference,
16885 context);
16886 return;
16890 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16891 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16892 Note that the block of subscript information for an array type also
16893 includes information about the element type of the given array type.
16895 This function reuses previously set type and bound information if
16896 available. */
16898 static void
16899 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16901 unsigned dimension_number;
16902 tree lower, upper;
16903 dw_die_ref child = type_die->die_child;
16905 for (dimension_number = 0;
16906 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16907 type = TREE_TYPE (type), dimension_number++)
16909 tree domain = TYPE_DOMAIN (type);
16911 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16912 break;
16914 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16915 and (in GNU C only) variable bounds. Handle all three forms
16916 here. */
16918 /* Find and reuse a previously generated DW_TAG_subrange_type if
16919 available.
16921 For multi-dimensional arrays, as we iterate through the
16922 various dimensions in the enclosing for loop above, we also
16923 iterate through the DIE children and pick at each
16924 DW_TAG_subrange_type previously generated (if available).
16925 Each child DW_TAG_subrange_type DIE describes the range of
16926 the current dimension. At this point we should have as many
16927 DW_TAG_subrange_type's as we have dimensions in the
16928 array. */
16929 dw_die_ref subrange_die = NULL;
16930 if (child)
16931 while (1)
16933 child = child->die_sib;
16934 if (child->die_tag == DW_TAG_subrange_type)
16935 subrange_die = child;
16936 if (child == type_die->die_child)
16938 /* If we wrapped around, stop looking next time. */
16939 child = NULL;
16940 break;
16942 if (child->die_tag == DW_TAG_subrange_type)
16943 break;
16945 if (!subrange_die)
16946 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16948 if (domain)
16950 /* We have an array type with specified bounds. */
16951 lower = TYPE_MIN_VALUE (domain);
16952 upper = TYPE_MAX_VALUE (domain);
16954 /* Define the index type. */
16955 if (TREE_TYPE (domain)
16956 && !get_AT (subrange_die, DW_AT_type))
16958 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16959 TREE_TYPE field. We can't emit debug info for this
16960 because it is an unnamed integral type. */
16961 if (TREE_CODE (domain) == INTEGER_TYPE
16962 && TYPE_NAME (domain) == NULL_TREE
16963 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16964 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16966 else
16967 add_type_attribute (subrange_die, TREE_TYPE (domain),
16968 TYPE_UNQUALIFIED, type_die);
16971 /* ??? If upper is NULL, the array has unspecified length,
16972 but it does have a lower bound. This happens with Fortran
16973 dimension arr(N:*)
16974 Since the debugger is definitely going to need to know N
16975 to produce useful results, go ahead and output the lower
16976 bound solo, and hope the debugger can cope. */
16978 if (!get_AT (subrange_die, DW_AT_lower_bound))
16979 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16980 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16981 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16984 /* Otherwise we have an array type with an unspecified length. The
16985 DWARF-2 spec does not say how to handle this; let's just leave out the
16986 bounds. */
16990 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16992 static void
16993 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16995 dw_die_ref decl_die;
16996 HOST_WIDE_INT size;
16998 switch (TREE_CODE (tree_node))
17000 case ERROR_MARK:
17001 size = 0;
17002 break;
17003 case ENUMERAL_TYPE:
17004 case RECORD_TYPE:
17005 case UNION_TYPE:
17006 case QUAL_UNION_TYPE:
17007 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17008 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17010 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17011 return;
17013 size = int_size_in_bytes (tree_node);
17014 break;
17015 case FIELD_DECL:
17016 /* For a data member of a struct or union, the DW_AT_byte_size is
17017 generally given as the number of bytes normally allocated for an
17018 object of the *declared* type of the member itself. This is true
17019 even for bit-fields. */
17020 size = int_size_in_bytes (field_type (tree_node));
17021 break;
17022 default:
17023 gcc_unreachable ();
17026 /* Note that `size' might be -1 when we get to this point. If it is, that
17027 indicates that the byte size of the entity in question is variable. We
17028 have no good way of expressing this fact in Dwarf at the present time,
17029 when location description was not used by the caller code instead. */
17030 if (size >= 0)
17031 add_AT_unsigned (die, DW_AT_byte_size, size);
17034 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17035 which specifies the distance in bits from the highest order bit of the
17036 "containing object" for the bit-field to the highest order bit of the
17037 bit-field itself.
17039 For any given bit-field, the "containing object" is a hypothetical object
17040 (of some integral or enum type) within which the given bit-field lives. The
17041 type of this hypothetical "containing object" is always the same as the
17042 declared type of the individual bit-field itself. The determination of the
17043 exact location of the "containing object" for a bit-field is rather
17044 complicated. It's handled by the `field_byte_offset' function (above).
17046 Note that it is the size (in bytes) of the hypothetical "containing object"
17047 which will be given in the DW_AT_byte_size attribute for this bit-field.
17048 (See `byte_size_attribute' above). */
17050 static inline void
17051 add_bit_offset_attribute (dw_die_ref die, tree decl)
17053 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17054 tree type = DECL_BIT_FIELD_TYPE (decl);
17055 HOST_WIDE_INT bitpos_int;
17056 HOST_WIDE_INT highest_order_object_bit_offset;
17057 HOST_WIDE_INT highest_order_field_bit_offset;
17058 HOST_WIDE_INT bit_offset;
17060 /* Must be a field and a bit field. */
17061 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17063 /* We can't yet handle bit-fields whose offsets are variable, so if we
17064 encounter such things, just return without generating any attribute
17065 whatsoever. Likewise for variable or too large size. */
17066 if (! tree_fits_shwi_p (bit_position (decl))
17067 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17068 return;
17070 bitpos_int = int_bit_position (decl);
17072 /* Note that the bit offset is always the distance (in bits) from the
17073 highest-order bit of the "containing object" to the highest-order bit of
17074 the bit-field itself. Since the "high-order end" of any object or field
17075 is different on big-endian and little-endian machines, the computation
17076 below must take account of these differences. */
17077 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17078 highest_order_field_bit_offset = bitpos_int;
17080 if (! BYTES_BIG_ENDIAN)
17082 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17083 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17086 bit_offset
17087 = (! BYTES_BIG_ENDIAN
17088 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17089 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17091 if (bit_offset < 0)
17092 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17093 else
17094 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17097 /* For a FIELD_DECL node which represents a bit field, output an attribute
17098 which specifies the length in bits of the given field. */
17100 static inline void
17101 add_bit_size_attribute (dw_die_ref die, tree decl)
17103 /* Must be a field and a bit field. */
17104 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17105 && DECL_BIT_FIELD_TYPE (decl));
17107 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17108 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17111 /* If the compiled language is ANSI C, then add a 'prototyped'
17112 attribute, if arg types are given for the parameters of a function. */
17114 static inline void
17115 add_prototyped_attribute (dw_die_ref die, tree func_type)
17117 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17119 case DW_LANG_C:
17120 case DW_LANG_C89:
17121 case DW_LANG_C99:
17122 case DW_LANG_C11:
17123 case DW_LANG_ObjC:
17124 if (prototype_p (func_type))
17125 add_AT_flag (die, DW_AT_prototyped, 1);
17126 break;
17127 default:
17128 break;
17132 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17133 by looking in either the type declaration or object declaration
17134 equate table. */
17136 static inline dw_die_ref
17137 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17139 dw_die_ref origin_die = NULL;
17141 if (TREE_CODE (origin) != FUNCTION_DECL)
17143 /* We may have gotten separated from the block for the inlined
17144 function, if we're in an exception handler or some such; make
17145 sure that the abstract function has been written out.
17147 Doing this for nested functions is wrong, however; functions are
17148 distinct units, and our context might not even be inline. */
17149 tree fn = origin;
17151 if (TYPE_P (fn))
17152 fn = TYPE_STUB_DECL (fn);
17154 fn = decl_function_context (fn);
17155 if (fn)
17156 dwarf2out_abstract_function (fn);
17159 if (DECL_P (origin))
17160 origin_die = lookup_decl_die (origin);
17161 else if (TYPE_P (origin))
17162 origin_die = lookup_type_die (origin);
17164 /* XXX: Functions that are never lowered don't always have correct block
17165 trees (in the case of java, they simply have no block tree, in some other
17166 languages). For these functions, there is nothing we can really do to
17167 output correct debug info for inlined functions in all cases. Rather
17168 than die, we'll just produce deficient debug info now, in that we will
17169 have variables without a proper abstract origin. In the future, when all
17170 functions are lowered, we should re-add a gcc_assert (origin_die)
17171 here. */
17173 if (origin_die)
17174 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17175 return origin_die;
17178 /* We do not currently support the pure_virtual attribute. */
17180 static inline void
17181 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17183 if (DECL_VINDEX (func_decl))
17185 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17187 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17188 add_AT_loc (die, DW_AT_vtable_elem_location,
17189 new_loc_descr (DW_OP_constu,
17190 tree_to_shwi (DECL_VINDEX (func_decl)),
17191 0));
17193 /* GNU extension: Record what type this method came from originally. */
17194 if (debug_info_level > DINFO_LEVEL_TERSE
17195 && DECL_CONTEXT (func_decl))
17196 add_AT_die_ref (die, DW_AT_containing_type,
17197 lookup_type_die (DECL_CONTEXT (func_decl)));
17201 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17202 given decl. This used to be a vendor extension until after DWARF 4
17203 standardized it. */
17205 static void
17206 add_linkage_attr (dw_die_ref die, tree decl)
17208 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17210 /* Mimic what assemble_name_raw does with a leading '*'. */
17211 if (name[0] == '*')
17212 name = &name[1];
17214 if (dwarf_version >= 4)
17215 add_AT_string (die, DW_AT_linkage_name, name);
17216 else
17217 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17220 /* Add source coordinate attributes for the given decl. */
17222 static void
17223 add_src_coords_attributes (dw_die_ref die, tree decl)
17225 expanded_location s;
17227 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17228 return;
17229 s = expand_location (DECL_SOURCE_LOCATION (decl));
17230 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17231 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17234 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17236 static void
17237 add_linkage_name (dw_die_ref die, tree decl)
17239 if (debug_info_level > DINFO_LEVEL_NONE
17240 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17241 && TREE_PUBLIC (decl)
17242 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17243 && die->die_tag != DW_TAG_member)
17245 /* Defer until we have an assembler name set. */
17246 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17248 limbo_die_node *asm_name;
17250 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17251 asm_name->die = die;
17252 asm_name->created_for = decl;
17253 asm_name->next = deferred_asm_name;
17254 deferred_asm_name = asm_name;
17256 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17257 add_linkage_attr (die, decl);
17261 /* Add a DW_AT_name attribute and source coordinate attribute for the
17262 given decl, but only if it actually has a name. */
17264 static void
17265 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17267 tree decl_name;
17269 decl_name = DECL_NAME (decl);
17270 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17272 const char *name = dwarf2_name (decl, 0);
17273 if (name)
17274 add_name_attribute (die, name);
17275 if (! DECL_ARTIFICIAL (decl))
17276 add_src_coords_attributes (die, decl);
17278 add_linkage_name (die, decl);
17281 #ifdef VMS_DEBUGGING_INFO
17282 /* Get the function's name, as described by its RTL. This may be different
17283 from the DECL_NAME name used in the source file. */
17284 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17286 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17287 XEXP (DECL_RTL (decl), 0), false);
17288 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17290 #endif /* VMS_DEBUGGING_INFO */
17293 #ifdef VMS_DEBUGGING_INFO
17294 /* Output the debug main pointer die for VMS */
17296 void
17297 dwarf2out_vms_debug_main_pointer (void)
17299 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17300 dw_die_ref die;
17302 /* Allocate the VMS debug main subprogram die. */
17303 die = ggc_cleared_alloc<die_node> ();
17304 die->die_tag = DW_TAG_subprogram;
17305 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17306 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17307 current_function_funcdef_no);
17308 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17310 /* Make it the first child of comp_unit_die (). */
17311 die->die_parent = comp_unit_die ();
17312 if (comp_unit_die ()->die_child)
17314 die->die_sib = comp_unit_die ()->die_child->die_sib;
17315 comp_unit_die ()->die_child->die_sib = die;
17317 else
17319 die->die_sib = die;
17320 comp_unit_die ()->die_child = die;
17323 #endif /* VMS_DEBUGGING_INFO */
17325 /* Push a new declaration scope. */
17327 static void
17328 push_decl_scope (tree scope)
17330 vec_safe_push (decl_scope_table, scope);
17333 /* Pop a declaration scope. */
17335 static inline void
17336 pop_decl_scope (void)
17338 decl_scope_table->pop ();
17341 /* walk_tree helper function for uses_local_type, below. */
17343 static tree
17344 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17346 if (!TYPE_P (*tp))
17347 *walk_subtrees = 0;
17348 else
17350 tree name = TYPE_NAME (*tp);
17351 if (name && DECL_P (name) && decl_function_context (name))
17352 return *tp;
17354 return NULL_TREE;
17357 /* If TYPE involves a function-local type (including a local typedef to a
17358 non-local type), returns that type; otherwise returns NULL_TREE. */
17360 static tree
17361 uses_local_type (tree type)
17363 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17364 return used;
17367 /* Return the DIE for the scope that immediately contains this type.
17368 Non-named types that do not involve a function-local type get global
17369 scope. Named types nested in namespaces or other types get their
17370 containing scope. All other types (i.e. function-local named types) get
17371 the current active scope. */
17373 static dw_die_ref
17374 scope_die_for (tree t, dw_die_ref context_die)
17376 dw_die_ref scope_die = NULL;
17377 tree containing_scope;
17379 /* Non-types always go in the current scope. */
17380 gcc_assert (TYPE_P (t));
17382 /* Use the scope of the typedef, rather than the scope of the type
17383 it refers to. */
17384 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17385 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17386 else
17387 containing_scope = TYPE_CONTEXT (t);
17389 /* Use the containing namespace if there is one. */
17390 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17392 if (context_die == lookup_decl_die (containing_scope))
17393 /* OK */;
17394 else if (debug_info_level > DINFO_LEVEL_TERSE)
17395 context_die = get_context_die (containing_scope);
17396 else
17397 containing_scope = NULL_TREE;
17400 /* Ignore function type "scopes" from the C frontend. They mean that
17401 a tagged type is local to a parmlist of a function declarator, but
17402 that isn't useful to DWARF. */
17403 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17404 containing_scope = NULL_TREE;
17406 if (SCOPE_FILE_SCOPE_P (containing_scope))
17408 /* If T uses a local type keep it local as well, to avoid references
17409 to function-local DIEs from outside the function. */
17410 if (current_function_decl && uses_local_type (t))
17411 scope_die = context_die;
17412 else
17413 scope_die = comp_unit_die ();
17415 else if (TYPE_P (containing_scope))
17417 /* For types, we can just look up the appropriate DIE. */
17418 if (debug_info_level > DINFO_LEVEL_TERSE)
17419 scope_die = get_context_die (containing_scope);
17420 else
17422 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17423 if (scope_die == NULL)
17424 scope_die = comp_unit_die ();
17427 else
17428 scope_die = context_die;
17430 return scope_die;
17433 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17435 static inline int
17436 local_scope_p (dw_die_ref context_die)
17438 for (; context_die; context_die = context_die->die_parent)
17439 if (context_die->die_tag == DW_TAG_inlined_subroutine
17440 || context_die->die_tag == DW_TAG_subprogram)
17441 return 1;
17443 return 0;
17446 /* Returns nonzero if CONTEXT_DIE is a class. */
17448 static inline int
17449 class_scope_p (dw_die_ref context_die)
17451 return (context_die
17452 && (context_die->die_tag == DW_TAG_structure_type
17453 || context_die->die_tag == DW_TAG_class_type
17454 || context_die->die_tag == DW_TAG_interface_type
17455 || context_die->die_tag == DW_TAG_union_type));
17458 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17459 whether or not to treat a DIE in this context as a declaration. */
17461 static inline int
17462 class_or_namespace_scope_p (dw_die_ref context_die)
17464 return (class_scope_p (context_die)
17465 || (context_die && context_die->die_tag == DW_TAG_namespace));
17468 /* Many forms of DIEs require a "type description" attribute. This
17469 routine locates the proper "type descriptor" die for the type given
17470 by 'type' plus any additional qualifiers given by 'cv_quals', and
17471 adds a DW_AT_type attribute below the given die. */
17473 static void
17474 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17475 dw_die_ref context_die)
17477 enum tree_code code = TREE_CODE (type);
17478 dw_die_ref type_die = NULL;
17480 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17481 or fixed-point type, use the inner type. This is because we have no
17482 support for unnamed types in base_type_die. This can happen if this is
17483 an Ada subrange type. Correct solution is emit a subrange type die. */
17484 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17485 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17486 type = TREE_TYPE (type), code = TREE_CODE (type);
17488 if (code == ERROR_MARK
17489 /* Handle a special case. For functions whose return type is void, we
17490 generate *no* type attribute. (Note that no object may have type
17491 `void', so this only applies to function return types). */
17492 || code == VOID_TYPE)
17493 return;
17495 type_die = modified_type_die (type,
17496 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17497 context_die);
17499 if (type_die != NULL)
17500 add_AT_die_ref (object_die, DW_AT_type, type_die);
17503 /* Given an object die, add the calling convention attribute for the
17504 function call type. */
17505 static void
17506 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17508 enum dwarf_calling_convention value = DW_CC_normal;
17510 value = ((enum dwarf_calling_convention)
17511 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17513 if (is_fortran ()
17514 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17516 /* DWARF 2 doesn't provide a way to identify a program's source-level
17517 entry point. DW_AT_calling_convention attributes are only meant
17518 to describe functions' calling conventions. However, lacking a
17519 better way to signal the Fortran main program, we used this for
17520 a long time, following existing custom. Now, DWARF 4 has
17521 DW_AT_main_subprogram, which we add below, but some tools still
17522 rely on the old way, which we thus keep. */
17523 value = DW_CC_program;
17525 if (dwarf_version >= 4 || !dwarf_strict)
17526 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17529 /* Only add the attribute if the backend requests it, and
17530 is not DW_CC_normal. */
17531 if (value && (value != DW_CC_normal))
17532 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17535 /* Given a tree pointer to a struct, class, union, or enum type node, return
17536 a pointer to the (string) tag name for the given type, or zero if the type
17537 was declared without a tag. */
17539 static const char *
17540 type_tag (const_tree type)
17542 const char *name = 0;
17544 if (TYPE_NAME (type) != 0)
17546 tree t = 0;
17548 /* Find the IDENTIFIER_NODE for the type name. */
17549 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17550 && !TYPE_NAMELESS (type))
17551 t = TYPE_NAME (type);
17553 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17554 a TYPE_DECL node, regardless of whether or not a `typedef' was
17555 involved. */
17556 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17557 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17559 /* We want to be extra verbose. Don't call dwarf_name if
17560 DECL_NAME isn't set. The default hook for decl_printable_name
17561 doesn't like that, and in this context it's correct to return
17562 0, instead of "<anonymous>" or the like. */
17563 if (DECL_NAME (TYPE_NAME (type))
17564 && !DECL_NAMELESS (TYPE_NAME (type)))
17565 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17568 /* Now get the name as a string, or invent one. */
17569 if (!name && t != 0)
17570 name = IDENTIFIER_POINTER (t);
17573 return (name == 0 || *name == '\0') ? 0 : name;
17576 /* Return the type associated with a data member, make a special check
17577 for bit field types. */
17579 static inline tree
17580 member_declared_type (const_tree member)
17582 return (DECL_BIT_FIELD_TYPE (member)
17583 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17586 /* Get the decl's label, as described by its RTL. This may be different
17587 from the DECL_NAME name used in the source file. */
17589 #if 0
17590 static const char *
17591 decl_start_label (tree decl)
17593 rtx x;
17594 const char *fnname;
17596 x = DECL_RTL (decl);
17597 gcc_assert (MEM_P (x));
17599 x = XEXP (x, 0);
17600 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17602 fnname = XSTR (x, 0);
17603 return fnname;
17605 #endif
17607 /* For variable-length arrays that have been previously generated, but
17608 may be incomplete due to missing subscript info, fill the subscript
17609 info. Return TRUE if this is one of those cases. */
17610 static bool
17611 fill_variable_array_bounds (tree type)
17613 if (TREE_ASM_WRITTEN (type)
17614 && TREE_CODE (type) == ARRAY_TYPE
17615 && variably_modified_type_p (type, NULL))
17617 dw_die_ref array_die = lookup_type_die (type);
17618 if (!array_die)
17619 return false;
17620 add_subscript_info (array_die, type, !is_ada ());
17621 return true;
17623 return false;
17626 /* These routines generate the internal representation of the DIE's for
17627 the compilation unit. Debugging information is collected by walking
17628 the declaration trees passed in from dwarf2out_decl(). */
17630 static void
17631 gen_array_type_die (tree type, dw_die_ref context_die)
17633 dw_die_ref array_die;
17635 /* GNU compilers represent multidimensional array types as sequences of one
17636 dimensional array types whose element types are themselves array types.
17637 We sometimes squish that down to a single array_type DIE with multiple
17638 subscripts in the Dwarf debugging info. The draft Dwarf specification
17639 say that we are allowed to do this kind of compression in C, because
17640 there is no difference between an array of arrays and a multidimensional
17641 array. We don't do this for Ada to remain as close as possible to the
17642 actual representation, which is especially important against the language
17643 flexibilty wrt arrays of variable size. */
17645 bool collapse_nested_arrays = !is_ada ();
17647 if (fill_variable_array_bounds (type))
17648 return;
17650 dw_die_ref scope_die = scope_die_for (type, context_die);
17651 tree element_type;
17653 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17654 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17655 if (TYPE_STRING_FLAG (type)
17656 && TREE_CODE (type) == ARRAY_TYPE
17657 && is_fortran ()
17658 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17660 HOST_WIDE_INT size;
17662 array_die = new_die (DW_TAG_string_type, scope_die, type);
17663 add_name_attribute (array_die, type_tag (type));
17664 equate_type_number_to_die (type, array_die);
17665 size = int_size_in_bytes (type);
17666 if (size >= 0)
17667 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17668 else if (TYPE_DOMAIN (type) != NULL_TREE
17669 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17670 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17672 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17673 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17675 size = int_size_in_bytes (TREE_TYPE (szdecl));
17676 if (loc && size > 0)
17678 add_AT_location_description (array_die, DW_AT_string_length, loc);
17679 if (size != DWARF2_ADDR_SIZE)
17680 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17683 return;
17686 array_die = new_die (DW_TAG_array_type, scope_die, type);
17687 add_name_attribute (array_die, type_tag (type));
17688 equate_type_number_to_die (type, array_die);
17690 if (TREE_CODE (type) == VECTOR_TYPE)
17691 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17693 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17694 if (is_fortran ()
17695 && TREE_CODE (type) == ARRAY_TYPE
17696 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17697 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17698 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17700 #if 0
17701 /* We default the array ordering. SDB will probably do
17702 the right things even if DW_AT_ordering is not present. It's not even
17703 an issue until we start to get into multidimensional arrays anyway. If
17704 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17705 then we'll have to put the DW_AT_ordering attribute back in. (But if
17706 and when we find out that we need to put these in, we will only do so
17707 for multidimensional arrays. */
17708 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17709 #endif
17711 if (TREE_CODE (type) == VECTOR_TYPE)
17713 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17714 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17715 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17716 add_bound_info (subrange_die, DW_AT_upper_bound,
17717 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17719 else
17720 add_subscript_info (array_die, type, collapse_nested_arrays);
17722 /* Add representation of the type of the elements of this array type and
17723 emit the corresponding DIE if we haven't done it already. */
17724 element_type = TREE_TYPE (type);
17725 if (collapse_nested_arrays)
17726 while (TREE_CODE (element_type) == ARRAY_TYPE)
17728 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17729 break;
17730 element_type = TREE_TYPE (element_type);
17733 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17735 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17736 if (TYPE_ARTIFICIAL (type))
17737 add_AT_flag (array_die, DW_AT_artificial, 1);
17739 if (get_AT (array_die, DW_AT_name))
17740 add_pubtype (type, array_die);
17743 /* This routine generates DIE for array with hidden descriptor, details
17744 are filled into *info by a langhook. */
17746 static void
17747 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17748 dw_die_ref context_die)
17750 const dw_die_ref scope_die = scope_die_for (type, context_die);
17751 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17752 const struct loc_descr_context context = { type, info->base_decl };
17753 int dim;
17755 add_name_attribute (array_die, type_tag (type));
17756 equate_type_number_to_die (type, array_die);
17758 if (info->ndimensions > 1)
17759 switch (info->ordering)
17761 case array_descr_ordering_row_major:
17762 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17763 break;
17764 case array_descr_ordering_column_major:
17765 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17766 break;
17767 default:
17768 break;
17771 if (dwarf_version >= 3 || !dwarf_strict)
17773 if (info->data_location)
17774 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17775 dw_scalar_form_exprloc, &context);
17776 if (info->associated)
17777 add_scalar_info (array_die, DW_AT_associated, info->associated,
17778 dw_scalar_form_constant
17779 | dw_scalar_form_exprloc
17780 | dw_scalar_form_reference, &context);
17781 if (info->allocated)
17782 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17783 dw_scalar_form_constant
17784 | dw_scalar_form_exprloc
17785 | dw_scalar_form_reference, &context);
17788 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17790 for (dim = 0; dim < info->ndimensions; dim++)
17792 dw_die_ref subrange_die
17793 = new_die (DW_TAG_subrange_type, array_die, NULL);
17795 if (info->dimen[dim].bounds_type)
17796 add_type_attribute (subrange_die,
17797 info->dimen[dim].bounds_type, 0,
17798 context_die);
17799 if (info->dimen[dim].lower_bound)
17800 add_bound_info (subrange_die, DW_AT_lower_bound,
17801 info->dimen[dim].lower_bound, &context);
17802 if (info->dimen[dim].upper_bound)
17803 add_bound_info (subrange_die, DW_AT_upper_bound,
17804 info->dimen[dim].upper_bound, &context);
17805 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17806 add_scalar_info (subrange_die, DW_AT_byte_stride,
17807 info->dimen[dim].stride,
17808 dw_scalar_form_constant
17809 | dw_scalar_form_exprloc
17810 | dw_scalar_form_reference,
17811 &context);
17814 gen_type_die (info->element_type, context_die);
17815 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17816 context_die);
17818 if (get_AT (array_die, DW_AT_name))
17819 add_pubtype (type, array_die);
17822 #if 0
17823 static void
17824 gen_entry_point_die (tree decl, dw_die_ref context_die)
17826 tree origin = decl_ultimate_origin (decl);
17827 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17829 if (origin != NULL)
17830 add_abstract_origin_attribute (decl_die, origin);
17831 else
17833 add_name_and_src_coords_attributes (decl_die, decl);
17834 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17835 TYPE_UNQUALIFIED, context_die);
17838 if (DECL_ABSTRACT_P (decl))
17839 equate_decl_number_to_die (decl, decl_die);
17840 else
17841 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17843 #endif
17845 /* Walk through the list of incomplete types again, trying once more to
17846 emit full debugging info for them. */
17848 static void
17849 retry_incomplete_types (void)
17851 int i;
17853 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17854 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17855 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17858 /* Determine what tag to use for a record type. */
17860 static enum dwarf_tag
17861 record_type_tag (tree type)
17863 if (! lang_hooks.types.classify_record)
17864 return DW_TAG_structure_type;
17866 switch (lang_hooks.types.classify_record (type))
17868 case RECORD_IS_STRUCT:
17869 return DW_TAG_structure_type;
17871 case RECORD_IS_CLASS:
17872 return DW_TAG_class_type;
17874 case RECORD_IS_INTERFACE:
17875 if (dwarf_version >= 3 || !dwarf_strict)
17876 return DW_TAG_interface_type;
17877 return DW_TAG_structure_type;
17879 default:
17880 gcc_unreachable ();
17884 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17885 include all of the information about the enumeration values also. Each
17886 enumerated type name/value is listed as a child of the enumerated type
17887 DIE. */
17889 static dw_die_ref
17890 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17892 dw_die_ref type_die = lookup_type_die (type);
17894 if (type_die == NULL)
17896 type_die = new_die (DW_TAG_enumeration_type,
17897 scope_die_for (type, context_die), type);
17898 equate_type_number_to_die (type, type_die);
17899 add_name_attribute (type_die, type_tag (type));
17900 if (dwarf_version >= 4 || !dwarf_strict)
17902 if (ENUM_IS_SCOPED (type))
17903 add_AT_flag (type_die, DW_AT_enum_class, 1);
17904 if (ENUM_IS_OPAQUE (type))
17905 add_AT_flag (type_die, DW_AT_declaration, 1);
17908 else if (! TYPE_SIZE (type))
17909 return type_die;
17910 else
17911 remove_AT (type_die, DW_AT_declaration);
17913 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17914 given enum type is incomplete, do not generate the DW_AT_byte_size
17915 attribute or the DW_AT_element_list attribute. */
17916 if (TYPE_SIZE (type))
17918 tree link;
17920 TREE_ASM_WRITTEN (type) = 1;
17921 add_byte_size_attribute (type_die, type);
17922 if (dwarf_version >= 3 || !dwarf_strict)
17924 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17925 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17926 context_die);
17928 if (TYPE_STUB_DECL (type) != NULL_TREE)
17930 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17931 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17934 /* If the first reference to this type was as the return type of an
17935 inline function, then it may not have a parent. Fix this now. */
17936 if (type_die->die_parent == NULL)
17937 add_child_die (scope_die_for (type, context_die), type_die);
17939 for (link = TYPE_VALUES (type);
17940 link != NULL; link = TREE_CHAIN (link))
17942 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17943 tree value = TREE_VALUE (link);
17945 add_name_attribute (enum_die,
17946 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17948 if (TREE_CODE (value) == CONST_DECL)
17949 value = DECL_INITIAL (value);
17951 if (simple_type_size_in_bits (TREE_TYPE (value))
17952 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17954 /* For constant forms created by add_AT_unsigned DWARF
17955 consumers (GDB, elfutils, etc.) always zero extend
17956 the value. Only when the actual value is negative
17957 do we need to use add_AT_int to generate a constant
17958 form that can represent negative values. */
17959 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17960 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17961 add_AT_unsigned (enum_die, DW_AT_const_value,
17962 (unsigned HOST_WIDE_INT) val);
17963 else
17964 add_AT_int (enum_die, DW_AT_const_value, val);
17966 else
17967 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17968 that here. TODO: This should be re-worked to use correct
17969 signed/unsigned double tags for all cases. */
17970 add_AT_wide (enum_die, DW_AT_const_value, value);
17973 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17974 if (TYPE_ARTIFICIAL (type))
17975 add_AT_flag (type_die, DW_AT_artificial, 1);
17977 else
17978 add_AT_flag (type_die, DW_AT_declaration, 1);
17980 add_pubtype (type, type_die);
17982 return type_die;
17985 /* Generate a DIE to represent either a real live formal parameter decl or to
17986 represent just the type of some formal parameter position in some function
17987 type.
17989 Note that this routine is a bit unusual because its argument may be a
17990 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17991 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17992 node. If it's the former then this function is being called to output a
17993 DIE to represent a formal parameter object (or some inlining thereof). If
17994 it's the latter, then this function is only being called to output a
17995 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17996 argument type of some subprogram type.
17997 If EMIT_NAME_P is true, name and source coordinate attributes
17998 are emitted. */
18000 static dw_die_ref
18001 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18002 dw_die_ref context_die)
18004 tree node_or_origin = node ? node : origin;
18005 tree ultimate_origin;
18006 dw_die_ref parm_die = NULL;
18008 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18010 parm_die = lookup_decl_die (node);
18012 /* If the contexts differ, we may not be talking about the same
18013 thing. */
18014 if (parm_die && parm_die->die_parent != context_die)
18016 if (!DECL_ABSTRACT_P (node))
18018 /* This can happen when creating an inlined instance, in
18019 which case we need to create a new DIE that will get
18020 annotated with DW_AT_abstract_origin. */
18021 parm_die = NULL;
18023 else
18025 /* FIXME: Reuse DIE even with a differing context.
18027 This happens when called through
18028 dwarf2out_abstract_function for formal parameter
18029 packs. The issue is that we're calling
18030 dwarf2out_abstract_function to build debug info for
18031 the abstract instance of a function for which we have
18032 already generated a DIE in
18033 dwarf2out_early_global_decl.
18035 Once we remove dwarf2out_abstract_function, this
18036 gcc_assert should be a gcc_unreachable. */
18037 gcc_assert (parm_die->die_parent->die_tag
18038 == DW_TAG_GNU_formal_parameter_pack);
18042 if (parm_die && parm_die->die_parent == NULL)
18044 /* Check that parm_die already has the right attributes that
18045 we would have added below. If any attributes are
18046 missing, fall through to add them. */
18047 if (! DECL_ABSTRACT_P (node_or_origin)
18048 && !get_AT (parm_die, DW_AT_location)
18049 && !get_AT (parm_die, DW_AT_const_value))
18050 /* We are missing location info, and are about to add it. */
18052 else
18054 add_child_die (context_die, parm_die);
18055 return parm_die;
18060 /* If we have a previously generated DIE, use it, unless this is an
18061 concrete instance (origin != NULL), in which case we need a new
18062 DIE with a corresponding DW_AT_abstract_origin. */
18063 bool reusing_die;
18064 if (parm_die && origin == NULL)
18065 reusing_die = true;
18066 else
18068 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18069 reusing_die = false;
18072 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18074 case tcc_declaration:
18075 ultimate_origin = decl_ultimate_origin (node_or_origin);
18076 if (node || ultimate_origin)
18077 origin = ultimate_origin;
18079 if (reusing_die)
18080 goto add_location;
18082 if (origin != NULL)
18083 add_abstract_origin_attribute (parm_die, origin);
18084 else if (emit_name_p)
18085 add_name_and_src_coords_attributes (parm_die, node);
18086 if (origin == NULL
18087 || (! DECL_ABSTRACT_P (node_or_origin)
18088 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18089 decl_function_context
18090 (node_or_origin))))
18092 tree type = TREE_TYPE (node_or_origin);
18093 if (decl_by_reference_p (node_or_origin))
18094 add_type_attribute (parm_die, TREE_TYPE (type),
18095 TYPE_UNQUALIFIED, context_die);
18096 else
18097 add_type_attribute (parm_die, type,
18098 decl_quals (node_or_origin),
18099 context_die);
18101 if (origin == NULL && DECL_ARTIFICIAL (node))
18102 add_AT_flag (parm_die, DW_AT_artificial, 1);
18103 add_location:
18104 if (node && node != origin)
18105 equate_decl_number_to_die (node, parm_die);
18106 if (! DECL_ABSTRACT_P (node_or_origin))
18107 add_location_or_const_value_attribute (parm_die, node_or_origin,
18108 node == NULL, DW_AT_location);
18110 break;
18112 case tcc_type:
18113 /* We were called with some kind of a ..._TYPE node. */
18114 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18115 context_die);
18116 break;
18118 default:
18119 gcc_unreachable ();
18122 return parm_die;
18125 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18126 children DW_TAG_formal_parameter DIEs representing the arguments of the
18127 parameter pack.
18129 PARM_PACK must be a function parameter pack.
18130 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18131 must point to the subsequent arguments of the function PACK_ARG belongs to.
18132 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18133 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18134 following the last one for which a DIE was generated. */
18136 static dw_die_ref
18137 gen_formal_parameter_pack_die (tree parm_pack,
18138 tree pack_arg,
18139 dw_die_ref subr_die,
18140 tree *next_arg)
18142 tree arg;
18143 dw_die_ref parm_pack_die;
18145 gcc_assert (parm_pack
18146 && lang_hooks.function_parameter_pack_p (parm_pack)
18147 && subr_die);
18149 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18150 add_src_coords_attributes (parm_pack_die, parm_pack);
18152 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18154 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18155 parm_pack))
18156 break;
18157 gen_formal_parameter_die (arg, NULL,
18158 false /* Don't emit name attribute. */,
18159 parm_pack_die);
18161 if (next_arg)
18162 *next_arg = arg;
18163 return parm_pack_die;
18166 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18167 at the end of an (ANSI prototyped) formal parameters list. */
18169 static void
18170 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18172 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18175 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18176 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18177 parameters as specified in some function type specification (except for
18178 those which appear as part of a function *definition*). */
18180 static void
18181 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18183 tree link;
18184 tree formal_type = NULL;
18185 tree first_parm_type;
18186 tree arg;
18188 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18190 arg = DECL_ARGUMENTS (function_or_method_type);
18191 function_or_method_type = TREE_TYPE (function_or_method_type);
18193 else
18194 arg = NULL_TREE;
18196 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18198 /* Make our first pass over the list of formal parameter types and output a
18199 DW_TAG_formal_parameter DIE for each one. */
18200 for (link = first_parm_type; link; )
18202 dw_die_ref parm_die;
18204 formal_type = TREE_VALUE (link);
18205 if (formal_type == void_type_node)
18206 break;
18208 /* Output a (nameless) DIE to represent the formal parameter itself. */
18209 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18211 parm_die = gen_formal_parameter_die (formal_type, NULL,
18212 true /* Emit name attribute. */,
18213 context_die);
18214 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18215 && link == first_parm_type)
18217 add_AT_flag (parm_die, DW_AT_artificial, 1);
18218 if (dwarf_version >= 3 || !dwarf_strict)
18219 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18221 else if (arg && DECL_ARTIFICIAL (arg))
18222 add_AT_flag (parm_die, DW_AT_artificial, 1);
18225 link = TREE_CHAIN (link);
18226 if (arg)
18227 arg = DECL_CHAIN (arg);
18230 /* If this function type has an ellipsis, add a
18231 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18232 if (formal_type != void_type_node)
18233 gen_unspecified_parameters_die (function_or_method_type, context_die);
18235 /* Make our second (and final) pass over the list of formal parameter types
18236 and output DIEs to represent those types (as necessary). */
18237 for (link = TYPE_ARG_TYPES (function_or_method_type);
18238 link && TREE_VALUE (link);
18239 link = TREE_CHAIN (link))
18240 gen_type_die (TREE_VALUE (link), context_die);
18243 /* We want to generate the DIE for TYPE so that we can generate the
18244 die for MEMBER, which has been defined; we will need to refer back
18245 to the member declaration nested within TYPE. If we're trying to
18246 generate minimal debug info for TYPE, processing TYPE won't do the
18247 trick; we need to attach the member declaration by hand. */
18249 static void
18250 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18252 gen_type_die (type, context_die);
18254 /* If we're trying to avoid duplicate debug info, we may not have
18255 emitted the member decl for this function. Emit it now. */
18256 if (TYPE_STUB_DECL (type)
18257 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18258 && ! lookup_decl_die (member))
18260 dw_die_ref type_die;
18261 gcc_assert (!decl_ultimate_origin (member));
18263 push_decl_scope (type);
18264 type_die = lookup_type_die_strip_naming_typedef (type);
18265 if (TREE_CODE (member) == FUNCTION_DECL)
18266 gen_subprogram_die (member, type_die);
18267 else if (TREE_CODE (member) == FIELD_DECL)
18269 /* Ignore the nameless fields that are used to skip bits but handle
18270 C++ anonymous unions and structs. */
18271 if (DECL_NAME (member) != NULL_TREE
18272 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18273 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18275 gen_type_die (member_declared_type (member), type_die);
18276 gen_field_die (member, type_die);
18279 else
18280 gen_variable_die (member, NULL_TREE, type_die);
18282 pop_decl_scope ();
18286 /* Forward declare these functions, because they are mutually recursive
18287 with their set_block_* pairing functions. */
18288 static void set_decl_origin_self (tree);
18289 static void set_decl_abstract_flags (tree, vec<tree> &);
18291 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18292 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18293 that it points to the node itself, thus indicating that the node is its
18294 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18295 the given node is NULL, recursively descend the decl/block tree which
18296 it is the root of, and for each other ..._DECL or BLOCK node contained
18297 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18298 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18299 values to point to themselves. */
18301 static void
18302 set_block_origin_self (tree stmt)
18304 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18306 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18309 tree local_decl;
18311 for (local_decl = BLOCK_VARS (stmt);
18312 local_decl != NULL_TREE;
18313 local_decl = DECL_CHAIN (local_decl))
18314 /* Do not recurse on nested functions since the inlining status
18315 of parent and child can be different as per the DWARF spec. */
18316 if (TREE_CODE (local_decl) != FUNCTION_DECL
18317 && !DECL_EXTERNAL (local_decl))
18318 set_decl_origin_self (local_decl);
18322 tree subblock;
18324 for (subblock = BLOCK_SUBBLOCKS (stmt);
18325 subblock != NULL_TREE;
18326 subblock = BLOCK_CHAIN (subblock))
18327 set_block_origin_self (subblock); /* Recurse. */
18332 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18333 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18334 node to so that it points to the node itself, thus indicating that the
18335 node represents its own (abstract) origin. Additionally, if the
18336 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18337 the decl/block tree of which the given node is the root of, and for
18338 each other ..._DECL or BLOCK node contained therein whose
18339 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18340 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18341 point to themselves. */
18343 static void
18344 set_decl_origin_self (tree decl)
18346 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18348 DECL_ABSTRACT_ORIGIN (decl) = decl;
18349 if (TREE_CODE (decl) == FUNCTION_DECL)
18351 tree arg;
18353 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18354 DECL_ABSTRACT_ORIGIN (arg) = arg;
18355 if (DECL_INITIAL (decl) != NULL_TREE
18356 && DECL_INITIAL (decl) != error_mark_node)
18357 set_block_origin_self (DECL_INITIAL (decl));
18362 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18363 and if it wasn't 1 before, push it to abstract_vec vector.
18364 For all local decls and all local sub-blocks (recursively) do it
18365 too. */
18367 static void
18368 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18370 tree local_decl;
18371 tree subblock;
18372 unsigned int i;
18374 if (!BLOCK_ABSTRACT (stmt))
18376 abstract_vec.safe_push (stmt);
18377 BLOCK_ABSTRACT (stmt) = 1;
18380 for (local_decl = BLOCK_VARS (stmt);
18381 local_decl != NULL_TREE;
18382 local_decl = DECL_CHAIN (local_decl))
18383 if (! DECL_EXTERNAL (local_decl))
18384 set_decl_abstract_flags (local_decl, abstract_vec);
18386 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18388 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18389 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18390 || TREE_CODE (local_decl) == PARM_DECL)
18391 set_decl_abstract_flags (local_decl, abstract_vec);
18394 for (subblock = BLOCK_SUBBLOCKS (stmt);
18395 subblock != NULL_TREE;
18396 subblock = BLOCK_CHAIN (subblock))
18397 set_block_abstract_flags (subblock, abstract_vec);
18400 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18401 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18402 In the case where the decl is a FUNCTION_DECL also set the abstract
18403 flags for all of the parameters, local vars, local
18404 blocks and sub-blocks (recursively). */
18406 static void
18407 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18409 if (!DECL_ABSTRACT_P (decl))
18411 abstract_vec.safe_push (decl);
18412 DECL_ABSTRACT_P (decl) = 1;
18415 if (TREE_CODE (decl) == FUNCTION_DECL)
18417 tree arg;
18419 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18420 if (!DECL_ABSTRACT_P (arg))
18422 abstract_vec.safe_push (arg);
18423 DECL_ABSTRACT_P (arg) = 1;
18425 if (DECL_INITIAL (decl) != NULL_TREE
18426 && DECL_INITIAL (decl) != error_mark_node)
18427 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18431 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18432 may later generate inlined and/or out-of-line instances of.
18434 FIXME: In the early-dwarf world, this function, and most of the
18435 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18436 the abstract instance. All we would need to do is annotate
18437 the early DIE with the appropriate DW_AT_inline in late
18438 dwarf (perhaps in gen_inlined_subroutine_die).
18440 However, we can't do this yet, because LTO streaming of DIEs
18441 has not been implemented yet. */
18443 static void
18444 dwarf2out_abstract_function (tree decl)
18446 dw_die_ref old_die;
18447 tree save_fn;
18448 tree context;
18449 hash_table<decl_loc_hasher> *old_decl_loc_table;
18450 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18451 int old_call_site_count, old_tail_call_site_count;
18452 struct call_arg_loc_node *old_call_arg_locations;
18454 /* Make sure we have the actual abstract inline, not a clone. */
18455 decl = DECL_ORIGIN (decl);
18457 old_die = lookup_decl_die (decl);
18458 if (old_die && get_AT (old_die, DW_AT_inline))
18459 /* We've already generated the abstract instance. */
18460 return;
18462 /* We can be called while recursively when seeing block defining inlined subroutine
18463 DIE. Be sure to not clobber the outer location table nor use it or we would
18464 get locations in abstract instantces. */
18465 old_decl_loc_table = decl_loc_table;
18466 decl_loc_table = NULL;
18467 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18468 cached_dw_loc_list_table = NULL;
18469 old_call_arg_locations = call_arg_locations;
18470 call_arg_locations = NULL;
18471 old_call_site_count = call_site_count;
18472 call_site_count = -1;
18473 old_tail_call_site_count = tail_call_site_count;
18474 tail_call_site_count = -1;
18476 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18477 we don't get confused by DECL_ABSTRACT_P. */
18478 if (debug_info_level > DINFO_LEVEL_TERSE)
18480 context = decl_class_context (decl);
18481 if (context)
18482 gen_type_die_for_member
18483 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18486 /* Pretend we've just finished compiling this function. */
18487 save_fn = current_function_decl;
18488 current_function_decl = decl;
18490 auto_vec<tree, 64> abstract_vec;
18491 set_decl_abstract_flags (decl, abstract_vec);
18492 dwarf2out_decl (decl);
18493 unsigned int i;
18494 tree t;
18495 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18496 if (TREE_CODE (t) == BLOCK)
18497 BLOCK_ABSTRACT (t) = 0;
18498 else
18499 DECL_ABSTRACT_P (t) = 0;
18501 current_function_decl = save_fn;
18502 decl_loc_table = old_decl_loc_table;
18503 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18504 call_arg_locations = old_call_arg_locations;
18505 call_site_count = old_call_site_count;
18506 tail_call_site_count = old_tail_call_site_count;
18509 /* Helper function of premark_used_types() which gets called through
18510 htab_traverse.
18512 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18513 marked as unused by prune_unused_types. */
18515 bool
18516 premark_used_types_helper (tree const &type, void *)
18518 dw_die_ref die;
18520 die = lookup_type_die (type);
18521 if (die != NULL)
18522 die->die_perennial_p = 1;
18523 return true;
18526 /* Helper function of premark_types_used_by_global_vars which gets called
18527 through htab_traverse.
18529 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18530 marked as unused by prune_unused_types. The DIE of the type is marked
18531 only if the global variable using the type will actually be emitted. */
18534 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18535 void *)
18537 struct types_used_by_vars_entry *entry;
18538 dw_die_ref die;
18540 entry = (struct types_used_by_vars_entry *) *slot;
18541 gcc_assert (entry->type != NULL
18542 && entry->var_decl != NULL);
18543 die = lookup_type_die (entry->type);
18544 if (die)
18546 /* Ask cgraph if the global variable really is to be emitted.
18547 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18548 varpool_node *node = varpool_node::get (entry->var_decl);
18549 if (node && node->definition)
18551 die->die_perennial_p = 1;
18552 /* Keep the parent DIEs as well. */
18553 while ((die = die->die_parent) && die->die_perennial_p == 0)
18554 die->die_perennial_p = 1;
18557 return 1;
18560 /* Mark all members of used_types_hash as perennial. */
18562 static void
18563 premark_used_types (struct function *fun)
18565 if (fun && fun->used_types_hash)
18566 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18569 /* Mark all members of types_used_by_vars_entry as perennial. */
18571 static void
18572 premark_types_used_by_global_vars (void)
18574 if (types_used_by_vars_hash)
18575 types_used_by_vars_hash
18576 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18579 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18580 for CA_LOC call arg loc node. */
18582 static dw_die_ref
18583 gen_call_site_die (tree decl, dw_die_ref subr_die,
18584 struct call_arg_loc_node *ca_loc)
18586 dw_die_ref stmt_die = NULL, die;
18587 tree block = ca_loc->block;
18589 while (block
18590 && block != DECL_INITIAL (decl)
18591 && TREE_CODE (block) == BLOCK)
18593 stmt_die = BLOCK_DIE (block);
18594 if (stmt_die)
18595 break;
18596 block = BLOCK_SUPERCONTEXT (block);
18598 if (stmt_die == NULL)
18599 stmt_die = subr_die;
18600 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18601 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18602 if (ca_loc->tail_call_p)
18603 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18604 if (ca_loc->symbol_ref)
18606 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18607 if (tdie)
18608 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18609 else
18610 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18612 return die;
18615 /* Generate a DIE to represent a declared function (either file-scope or
18616 block-local). */
18618 static void
18619 gen_subprogram_die (tree decl, dw_die_ref context_die)
18621 tree origin = decl_ultimate_origin (decl);
18622 dw_die_ref subr_die;
18623 dw_die_ref old_die = lookup_decl_die (decl);
18625 /* This function gets called multiple times for different stages of
18626 the debug process. For example, for func() in this code:
18628 namespace S
18630 void func() { ... }
18633 ...we get called 4 times. Twice in early debug and twice in
18634 late debug:
18636 Early debug
18637 -----------
18639 1. Once while generating func() within the namespace. This is
18640 the declaration. The declaration bit below is set, as the
18641 context is the namespace.
18643 A new DIE will be generated with DW_AT_declaration set.
18645 2. Once for func() itself. This is the specification. The
18646 declaration bit below is clear as the context is the CU.
18648 We will use the cached DIE from (1) to create a new DIE with
18649 DW_AT_specification pointing to the declaration in (1).
18651 Late debug via rest_of_handle_final()
18652 -------------------------------------
18654 3. Once generating func() within the namespace. This is also the
18655 declaration, as in (1), but this time we will early exit below
18656 as we have a cached DIE and a declaration needs no additional
18657 annotations (no locations), as the source declaration line
18658 info is enough.
18660 4. Once for func() itself. As in (2), this is the specification,
18661 but this time we will re-use the cached DIE, and just annotate
18662 it with the location information that should now be available.
18664 For something without namespaces, but with abstract instances, we
18665 are also called a multiple times:
18667 class Base
18669 public:
18670 Base (); // constructor declaration (1)
18673 Base::Base () { } // constructor specification (2)
18675 Early debug
18676 -----------
18678 1. Once for the Base() constructor by virtue of it being a
18679 member of the Base class. This is done via
18680 rest_of_type_compilation.
18682 This is a declaration, so a new DIE will be created with
18683 DW_AT_declaration.
18685 2. Once for the Base() constructor definition, but this time
18686 while generating the abstract instance of the base
18687 constructor (__base_ctor) which is being generated via early
18688 debug of reachable functions.
18690 Even though we have a cached version of the declaration (1),
18691 we will create a DW_AT_specification of the declaration DIE
18692 in (1).
18694 3. Once for the __base_ctor itself, but this time, we generate
18695 an DW_AT_abstract_origin version of the DW_AT_specification in
18696 (2).
18698 Late debug via rest_of_handle_final
18699 -----------------------------------
18701 4. One final time for the __base_ctor (which will have a cached
18702 DIE with DW_AT_abstract_origin created in (3). This time,
18703 we will just annotate the location information now
18704 available.
18706 int declaration = (current_function_decl != decl
18707 || class_or_namespace_scope_p (context_die));
18709 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18711 /* Now that the C++ front end lazily declares artificial member fns, we
18712 might need to retrofit the declaration into its class. */
18713 if (!declaration && !origin && !old_die
18714 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18715 && !class_or_namespace_scope_p (context_die)
18716 && debug_info_level > DINFO_LEVEL_TERSE)
18717 old_die = force_decl_die (decl);
18719 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18720 if (origin != NULL)
18722 gcc_assert (!declaration || local_scope_p (context_die));
18724 /* Fixup die_parent for the abstract instance of a nested
18725 inline function. */
18726 if (old_die && old_die->die_parent == NULL)
18727 add_child_die (context_die, old_die);
18729 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18731 /* If we have a DW_AT_abstract_origin we have a working
18732 cached version. */
18733 subr_die = old_die;
18735 else
18737 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18738 add_abstract_origin_attribute (subr_die, origin);
18739 /* This is where the actual code for a cloned function is.
18740 Let's emit linkage name attribute for it. This helps
18741 debuggers to e.g, set breakpoints into
18742 constructors/destructors when the user asks "break
18743 K::K". */
18744 add_linkage_name (subr_die, decl);
18747 /* A cached copy, possibly from early dwarf generation. Reuse as
18748 much as possible. */
18749 else if (old_die)
18751 /* A declaration that has been previously dumped needs no
18752 additional information. */
18753 if (declaration)
18754 return;
18756 if (!get_AT_flag (old_die, DW_AT_declaration)
18757 /* We can have a normal definition following an inline one in the
18758 case of redefinition of GNU C extern inlines.
18759 It seems reasonable to use AT_specification in this case. */
18760 && !get_AT (old_die, DW_AT_inline))
18762 /* Detect and ignore this case, where we are trying to output
18763 something we have already output. */
18764 if (get_AT (old_die, DW_AT_low_pc)
18765 || get_AT (old_die, DW_AT_ranges))
18766 return;
18768 /* If we have no location information, this must be a
18769 partially generated DIE from early dwarf generation.
18770 Fall through and generate it. */
18773 /* If the definition comes from the same place as the declaration,
18774 maybe use the old DIE. We always want the DIE for this function
18775 that has the *_pc attributes to be under comp_unit_die so the
18776 debugger can find it. We also need to do this for abstract
18777 instances of inlines, since the spec requires the out-of-line copy
18778 to have the same parent. For local class methods, this doesn't
18779 apply; we just use the old DIE. */
18780 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18781 struct dwarf_file_data * file_index = lookup_filename (s.file);
18782 if ((is_cu_die (old_die->die_parent)
18783 /* This condition fixes the inconsistency/ICE with the
18784 following Fortran test (or some derivative thereof) while
18785 building libgfortran:
18787 module some_m
18788 contains
18789 logical function funky (FLAG)
18790 funky = .true.
18791 end function
18792 end module
18794 || old_die->die_parent->die_tag == DW_TAG_module
18795 || context_die == NULL)
18796 && (DECL_ARTIFICIAL (decl)
18797 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18798 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18799 == (unsigned) s.line))))
18801 subr_die = old_die;
18803 /* Clear out the declaration attribute, but leave the
18804 parameters so they can be augmented with location
18805 information later. Unless this was a declaration, in
18806 which case, wipe out the nameless parameters and recreate
18807 them further down. */
18808 if (remove_AT (subr_die, DW_AT_declaration))
18811 remove_AT (subr_die, DW_AT_object_pointer);
18812 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18815 /* Make a specification pointing to the previously built
18816 declaration. */
18817 else
18819 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18820 add_AT_specification (subr_die, old_die);
18821 add_pubname (decl, subr_die);
18822 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18823 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18824 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18825 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18827 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18828 emit the real type on the definition die. */
18829 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18831 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18832 if (die == auto_die || die == decltype_auto_die)
18833 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18834 TYPE_UNQUALIFIED, context_die);
18838 /* Create a fresh DIE for anything else. */
18839 else
18841 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18843 if (TREE_PUBLIC (decl))
18844 add_AT_flag (subr_die, DW_AT_external, 1);
18846 add_name_and_src_coords_attributes (subr_die, decl);
18847 add_pubname (decl, subr_die);
18848 if (debug_info_level > DINFO_LEVEL_TERSE)
18850 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18851 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18852 TYPE_UNQUALIFIED, context_die);
18855 add_pure_or_virtual_attribute (subr_die, decl);
18856 if (DECL_ARTIFICIAL (decl))
18857 add_AT_flag (subr_die, DW_AT_artificial, 1);
18859 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18860 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18862 add_accessibility_attribute (subr_die, decl);
18865 /* Unless we have an existing non-declaration DIE, equate the new
18866 DIE. */
18867 if (!old_die || is_declaration_die (old_die))
18868 equate_decl_number_to_die (decl, subr_die);
18870 if (declaration)
18872 if (!old_die || !get_AT (old_die, DW_AT_inline))
18874 add_AT_flag (subr_die, DW_AT_declaration, 1);
18876 /* If this is an explicit function declaration then generate
18877 a DW_AT_explicit attribute. */
18878 if (lang_hooks.decls.function_decl_explicit_p (decl)
18879 && (dwarf_version >= 3 || !dwarf_strict))
18880 add_AT_flag (subr_die, DW_AT_explicit, 1);
18882 /* If this is a C++11 deleted special function member then generate
18883 a DW_AT_GNU_deleted attribute. */
18884 if (lang_hooks.decls.function_decl_deleted_p (decl)
18885 && (! dwarf_strict))
18886 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18889 /* Tag abstract instances with DW_AT_inline. */
18890 else if (DECL_ABSTRACT_P (decl))
18892 if (DECL_DECLARED_INLINE_P (decl))
18894 if (cgraph_function_possibly_inlined_p (decl))
18895 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18896 else
18897 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18899 else
18901 if (cgraph_function_possibly_inlined_p (decl))
18902 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18903 else
18904 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18907 if (DECL_DECLARED_INLINE_P (decl)
18908 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18909 add_AT_flag (subr_die, DW_AT_artificial, 1);
18911 /* For non DECL_EXTERNALs, if range information is available, fill
18912 the DIE with it. */
18913 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18915 HOST_WIDE_INT cfa_fb_offset;
18917 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18919 if (!flag_reorder_blocks_and_partition)
18921 dw_fde_ref fde = fun->fde;
18922 if (fde->dw_fde_begin)
18924 /* We have already generated the labels. */
18925 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18926 fde->dw_fde_end, false);
18928 else
18930 /* Create start/end labels and add the range. */
18931 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18932 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18933 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18934 current_function_funcdef_no);
18935 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18936 current_function_funcdef_no);
18937 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18938 false);
18941 #if VMS_DEBUGGING_INFO
18942 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18943 Section 2.3 Prologue and Epilogue Attributes:
18944 When a breakpoint is set on entry to a function, it is generally
18945 desirable for execution to be suspended, not on the very first
18946 instruction of the function, but rather at a point after the
18947 function's frame has been set up, after any language defined local
18948 declaration processing has been completed, and before execution of
18949 the first statement of the function begins. Debuggers generally
18950 cannot properly determine where this point is. Similarly for a
18951 breakpoint set on exit from a function. The prologue and epilogue
18952 attributes allow a compiler to communicate the location(s) to use. */
18955 if (fde->dw_fde_vms_end_prologue)
18956 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18957 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18959 if (fde->dw_fde_vms_begin_epilogue)
18960 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18961 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18963 #endif
18966 else
18968 /* Generate pubnames entries for the split function code ranges. */
18969 dw_fde_ref fde = fun->fde;
18971 if (fde->dw_fde_second_begin)
18973 if (dwarf_version >= 3 || !dwarf_strict)
18975 /* We should use ranges for non-contiguous code section
18976 addresses. Use the actual code range for the initial
18977 section, since the HOT/COLD labels might precede an
18978 alignment offset. */
18979 bool range_list_added = false;
18980 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18981 fde->dw_fde_end, &range_list_added,
18982 false);
18983 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18984 fde->dw_fde_second_end,
18985 &range_list_added, false);
18986 if (range_list_added)
18987 add_ranges (NULL);
18989 else
18991 /* There is no real support in DW2 for this .. so we make
18992 a work-around. First, emit the pub name for the segment
18993 containing the function label. Then make and emit a
18994 simplified subprogram DIE for the second segment with the
18995 name pre-fixed by __hot/cold_sect_of_. We use the same
18996 linkage name for the second die so that gdb will find both
18997 sections when given "b foo". */
18998 const char *name = NULL;
18999 tree decl_name = DECL_NAME (decl);
19000 dw_die_ref seg_die;
19002 /* Do the 'primary' section. */
19003 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19004 fde->dw_fde_end, false);
19006 /* Build a minimal DIE for the secondary section. */
19007 seg_die = new_die (DW_TAG_subprogram,
19008 subr_die->die_parent, decl);
19010 if (TREE_PUBLIC (decl))
19011 add_AT_flag (seg_die, DW_AT_external, 1);
19013 if (decl_name != NULL
19014 && IDENTIFIER_POINTER (decl_name) != NULL)
19016 name = dwarf2_name (decl, 1);
19017 if (! DECL_ARTIFICIAL (decl))
19018 add_src_coords_attributes (seg_die, decl);
19020 add_linkage_name (seg_die, decl);
19022 gcc_assert (name != NULL);
19023 add_pure_or_virtual_attribute (seg_die, decl);
19024 if (DECL_ARTIFICIAL (decl))
19025 add_AT_flag (seg_die, DW_AT_artificial, 1);
19027 name = concat ("__second_sect_of_", name, NULL);
19028 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19029 fde->dw_fde_second_end, false);
19030 add_name_attribute (seg_die, name);
19031 if (want_pubnames ())
19032 add_pubname_string (name, seg_die);
19035 else
19036 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19037 false);
19040 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19042 /* We define the "frame base" as the function's CFA. This is more
19043 convenient for several reasons: (1) It's stable across the prologue
19044 and epilogue, which makes it better than just a frame pointer,
19045 (2) With dwarf3, there exists a one-byte encoding that allows us
19046 to reference the .debug_frame data by proxy, but failing that,
19047 (3) We can at least reuse the code inspection and interpretation
19048 code that determines the CFA position at various points in the
19049 function. */
19050 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19052 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19053 add_AT_loc (subr_die, DW_AT_frame_base, op);
19055 else
19057 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19058 if (list->dw_loc_next)
19059 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19060 else
19061 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19064 /* Compute a displacement from the "steady-state frame pointer" to
19065 the CFA. The former is what all stack slots and argument slots
19066 will reference in the rtl; the latter is what we've told the
19067 debugger about. We'll need to adjust all frame_base references
19068 by this displacement. */
19069 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19071 if (fun->static_chain_decl)
19072 add_AT_location_description
19073 (subr_die, DW_AT_static_link,
19074 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19077 /* Generate child dies for template paramaters. */
19078 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19079 gen_generic_params_dies (decl);
19081 /* Now output descriptions of the arguments for this function. This gets
19082 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19083 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19084 `...' at the end of the formal parameter list. In order to find out if
19085 there was a trailing ellipsis or not, we must instead look at the type
19086 associated with the FUNCTION_DECL. This will be a node of type
19087 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19088 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19089 an ellipsis at the end. */
19091 /* In the case where we are describing a mere function declaration, all we
19092 need to do here (and all we *can* do here) is to describe the *types* of
19093 its formal parameters. */
19094 if (debug_info_level <= DINFO_LEVEL_TERSE)
19096 else if (declaration)
19097 gen_formal_types_die (decl, subr_die);
19098 else
19100 /* Generate DIEs to represent all known formal parameters. */
19101 tree parm = DECL_ARGUMENTS (decl);
19102 tree generic_decl = early_dwarf
19103 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19104 tree generic_decl_parm = generic_decl
19105 ? DECL_ARGUMENTS (generic_decl)
19106 : NULL;
19108 /* Now we want to walk the list of parameters of the function and
19109 emit their relevant DIEs.
19111 We consider the case of DECL being an instance of a generic function
19112 as well as it being a normal function.
19114 If DECL is an instance of a generic function we walk the
19115 parameters of the generic function declaration _and_ the parameters of
19116 DECL itself. This is useful because we want to emit specific DIEs for
19117 function parameter packs and those are declared as part of the
19118 generic function declaration. In that particular case,
19119 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19120 That DIE has children DIEs representing the set of arguments
19121 of the pack. Note that the set of pack arguments can be empty.
19122 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19123 children DIE.
19125 Otherwise, we just consider the parameters of DECL. */
19126 while (generic_decl_parm || parm)
19128 if (generic_decl_parm
19129 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19130 gen_formal_parameter_pack_die (generic_decl_parm,
19131 parm, subr_die,
19132 &parm);
19133 else if (parm && !POINTER_BOUNDS_P (parm))
19135 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19137 if (parm == DECL_ARGUMENTS (decl)
19138 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19139 && parm_die
19140 && (dwarf_version >= 3 || !dwarf_strict))
19141 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19143 parm = DECL_CHAIN (parm);
19145 else if (parm)
19146 parm = DECL_CHAIN (parm);
19148 if (generic_decl_parm)
19149 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19152 /* Decide whether we need an unspecified_parameters DIE at the end.
19153 There are 2 more cases to do this for: 1) the ansi ... declaration -
19154 this is detectable when the end of the arg list is not a
19155 void_type_node 2) an unprototyped function declaration (not a
19156 definition). This just means that we have no info about the
19157 parameters at all. */
19158 if (prototype_p (TREE_TYPE (decl)))
19160 /* This is the prototyped case, check for.... */
19161 if (stdarg_p (TREE_TYPE (decl)))
19162 gen_unspecified_parameters_die (decl, subr_die);
19164 else if (DECL_INITIAL (decl) == NULL_TREE)
19165 gen_unspecified_parameters_die (decl, subr_die);
19168 if (subr_die != old_die)
19169 /* Add the calling convention attribute if requested. */
19170 add_calling_convention_attribute (subr_die, decl);
19172 /* Output Dwarf info for all of the stuff within the body of the function
19173 (if it has one - it may be just a declaration).
19175 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19176 a function. This BLOCK actually represents the outermost binding contour
19177 for the function, i.e. the contour in which the function's formal
19178 parameters and labels get declared. Curiously, it appears that the front
19179 end doesn't actually put the PARM_DECL nodes for the current function onto
19180 the BLOCK_VARS list for this outer scope, but are strung off of the
19181 DECL_ARGUMENTS list for the function instead.
19183 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19184 the LABEL_DECL nodes for the function however, and we output DWARF info
19185 for those in decls_for_scope. Just within the `outer_scope' there will be
19186 a BLOCK node representing the function's outermost pair of curly braces,
19187 and any blocks used for the base and member initializers of a C++
19188 constructor function. */
19189 tree outer_scope = DECL_INITIAL (decl);
19190 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19192 int call_site_note_count = 0;
19193 int tail_call_site_note_count = 0;
19195 /* Emit a DW_TAG_variable DIE for a named return value. */
19196 if (DECL_NAME (DECL_RESULT (decl)))
19197 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19199 /* The first time through decls_for_scope we will generate the
19200 DIEs for the locals. The second time, we fill in the
19201 location info. */
19202 decls_for_scope (outer_scope, subr_die);
19204 if (call_arg_locations && !dwarf_strict)
19206 struct call_arg_loc_node *ca_loc;
19207 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19209 dw_die_ref die = NULL;
19210 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19211 rtx arg, next_arg;
19213 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19214 arg; arg = next_arg)
19216 dw_loc_descr_ref reg, val;
19217 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19218 dw_die_ref cdie, tdie = NULL;
19220 next_arg = XEXP (arg, 1);
19221 if (REG_P (XEXP (XEXP (arg, 0), 0))
19222 && next_arg
19223 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19224 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19225 && REGNO (XEXP (XEXP (arg, 0), 0))
19226 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19227 next_arg = XEXP (next_arg, 1);
19228 if (mode == VOIDmode)
19230 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19231 if (mode == VOIDmode)
19232 mode = GET_MODE (XEXP (arg, 0));
19234 if (mode == VOIDmode || mode == BLKmode)
19235 continue;
19236 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19238 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19239 tloc = XEXP (XEXP (arg, 0), 1);
19240 continue;
19242 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19243 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19245 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19246 tlocc = XEXP (XEXP (arg, 0), 1);
19247 continue;
19249 reg = NULL;
19250 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19251 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19252 VAR_INIT_STATUS_INITIALIZED);
19253 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19255 rtx mem = XEXP (XEXP (arg, 0), 0);
19256 reg = mem_loc_descriptor (XEXP (mem, 0),
19257 get_address_mode (mem),
19258 GET_MODE (mem),
19259 VAR_INIT_STATUS_INITIALIZED);
19261 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19262 == DEBUG_PARAMETER_REF)
19264 tree tdecl
19265 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19266 tdie = lookup_decl_die (tdecl);
19267 if (tdie == NULL)
19268 continue;
19270 else
19271 continue;
19272 if (reg == NULL
19273 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19274 != DEBUG_PARAMETER_REF)
19275 continue;
19276 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19277 VOIDmode,
19278 VAR_INIT_STATUS_INITIALIZED);
19279 if (val == NULL)
19280 continue;
19281 if (die == NULL)
19282 die = gen_call_site_die (decl, subr_die, ca_loc);
19283 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19284 NULL_TREE);
19285 if (reg != NULL)
19286 add_AT_loc (cdie, DW_AT_location, reg);
19287 else if (tdie != NULL)
19288 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19289 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19290 if (next_arg != XEXP (arg, 1))
19292 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19293 if (mode == VOIDmode)
19294 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19295 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19296 0), 1),
19297 mode, VOIDmode,
19298 VAR_INIT_STATUS_INITIALIZED);
19299 if (val != NULL)
19300 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19303 if (die == NULL
19304 && (ca_loc->symbol_ref || tloc))
19305 die = gen_call_site_die (decl, subr_die, ca_loc);
19306 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19308 dw_loc_descr_ref tval = NULL;
19310 if (tloc != NULL_RTX)
19311 tval = mem_loc_descriptor (tloc,
19312 GET_MODE (tloc) == VOIDmode
19313 ? Pmode : GET_MODE (tloc),
19314 VOIDmode,
19315 VAR_INIT_STATUS_INITIALIZED);
19316 if (tval)
19317 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19318 else if (tlocc != NULL_RTX)
19320 tval = mem_loc_descriptor (tlocc,
19321 GET_MODE (tlocc) == VOIDmode
19322 ? Pmode : GET_MODE (tlocc),
19323 VOIDmode,
19324 VAR_INIT_STATUS_INITIALIZED);
19325 if (tval)
19326 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19327 tval);
19330 if (die != NULL)
19332 call_site_note_count++;
19333 if (ca_loc->tail_call_p)
19334 tail_call_site_note_count++;
19338 call_arg_locations = NULL;
19339 call_arg_loc_last = NULL;
19340 if (tail_call_site_count >= 0
19341 && tail_call_site_count == tail_call_site_note_count
19342 && !dwarf_strict)
19344 if (call_site_count >= 0
19345 && call_site_count == call_site_note_count)
19346 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19347 else
19348 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19350 call_site_count = -1;
19351 tail_call_site_count = -1;
19355 /* Returns a hash value for X (which really is a die_struct). */
19357 hashval_t
19358 block_die_hasher::hash (die_struct *d)
19360 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19363 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19364 as decl_id and die_parent of die_struct Y. */
19366 bool
19367 block_die_hasher::equal (die_struct *x, die_struct *y)
19369 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19372 /* Return TRUE if DECL, which may have been previously generated as
19373 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19374 true if decl (or its origin) is either an extern declaration or a
19375 class/namespace scoped declaration.
19377 The declare_in_namespace support causes us to get two DIEs for one
19378 variable, both of which are declarations. We want to avoid
19379 considering one to be a specification, so we must test for
19380 DECLARATION and DW_AT_declaration. */
19381 static inline bool
19382 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19384 return (old_die && TREE_STATIC (decl) && !declaration
19385 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19388 /* Return true if DECL is a local static. */
19390 static inline bool
19391 local_function_static (tree decl)
19393 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19394 return TREE_STATIC (decl)
19395 && DECL_CONTEXT (decl)
19396 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19399 /* Generate a DIE to represent a declared data object.
19400 Either DECL or ORIGIN must be non-null. */
19402 static void
19403 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19405 HOST_WIDE_INT off = 0;
19406 tree com_decl;
19407 tree decl_or_origin = decl ? decl : origin;
19408 tree ultimate_origin;
19409 dw_die_ref var_die;
19410 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19411 dw_die_ref origin_die = NULL;
19412 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19413 || class_or_namespace_scope_p (context_die));
19414 bool specialization_p = false;
19416 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19417 if (decl || ultimate_origin)
19418 origin = ultimate_origin;
19419 com_decl = fortran_common (decl_or_origin, &off);
19421 /* Symbol in common gets emitted as a child of the common block, in the form
19422 of a data member. */
19423 if (com_decl)
19425 dw_die_ref com_die;
19426 dw_loc_list_ref loc;
19427 die_node com_die_arg;
19429 var_die = lookup_decl_die (decl_or_origin);
19430 if (var_die)
19432 if (get_AT (var_die, DW_AT_location) == NULL)
19434 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19435 if (loc)
19437 if (off)
19439 /* Optimize the common case. */
19440 if (single_element_loc_list_p (loc)
19441 && loc->expr->dw_loc_opc == DW_OP_addr
19442 && loc->expr->dw_loc_next == NULL
19443 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19444 == SYMBOL_REF)
19446 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19447 loc->expr->dw_loc_oprnd1.v.val_addr
19448 = plus_constant (GET_MODE (x), x , off);
19450 else
19451 loc_list_plus_const (loc, off);
19453 add_AT_location_description (var_die, DW_AT_location, loc);
19454 remove_AT (var_die, DW_AT_declaration);
19457 return;
19460 if (common_block_die_table == NULL)
19461 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19463 com_die_arg.decl_id = DECL_UID (com_decl);
19464 com_die_arg.die_parent = context_die;
19465 com_die = common_block_die_table->find (&com_die_arg);
19466 loc = loc_list_from_tree (com_decl, 2, NULL);
19467 if (com_die == NULL)
19469 const char *cnam
19470 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19471 die_node **slot;
19473 com_die = new_die (DW_TAG_common_block, context_die, decl);
19474 add_name_and_src_coords_attributes (com_die, com_decl);
19475 if (loc)
19477 add_AT_location_description (com_die, DW_AT_location, loc);
19478 /* Avoid sharing the same loc descriptor between
19479 DW_TAG_common_block and DW_TAG_variable. */
19480 loc = loc_list_from_tree (com_decl, 2, NULL);
19482 else if (DECL_EXTERNAL (decl))
19483 add_AT_flag (com_die, DW_AT_declaration, 1);
19484 if (want_pubnames ())
19485 add_pubname_string (cnam, com_die); /* ??? needed? */
19486 com_die->decl_id = DECL_UID (com_decl);
19487 slot = common_block_die_table->find_slot (com_die, INSERT);
19488 *slot = com_die;
19490 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19492 add_AT_location_description (com_die, DW_AT_location, loc);
19493 loc = loc_list_from_tree (com_decl, 2, NULL);
19494 remove_AT (com_die, DW_AT_declaration);
19496 var_die = new_die (DW_TAG_variable, com_die, decl);
19497 add_name_and_src_coords_attributes (var_die, decl);
19498 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19499 context_die);
19500 add_AT_flag (var_die, DW_AT_external, 1);
19501 if (loc)
19503 if (off)
19505 /* Optimize the common case. */
19506 if (single_element_loc_list_p (loc)
19507 && loc->expr->dw_loc_opc == DW_OP_addr
19508 && loc->expr->dw_loc_next == NULL
19509 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19511 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19512 loc->expr->dw_loc_oprnd1.v.val_addr
19513 = plus_constant (GET_MODE (x), x, off);
19515 else
19516 loc_list_plus_const (loc, off);
19518 add_AT_location_description (var_die, DW_AT_location, loc);
19520 else if (DECL_EXTERNAL (decl))
19521 add_AT_flag (var_die, DW_AT_declaration, 1);
19522 equate_decl_number_to_die (decl, var_die);
19523 return;
19526 if (old_die)
19528 if (declaration)
19530 /* A declaration that has been previously dumped, needs no
19531 further annotations, since it doesn't need location on
19532 the second pass. */
19533 return;
19535 else if (decl_will_get_specification_p (old_die, decl, declaration)
19536 && !get_AT (old_die, DW_AT_specification))
19538 /* Fall-thru so we can make a new variable die along with a
19539 DW_AT_specification. */
19541 else if (origin && old_die->die_parent != context_die)
19543 /* If we will be creating an inlined instance, we need a
19544 new DIE that will get annotated with
19545 DW_AT_abstract_origin. Clear things so we can get a
19546 new DIE. */
19547 gcc_assert (!DECL_ABSTRACT_P (decl));
19548 old_die = NULL;
19550 else
19552 /* If a DIE was dumped early, it still needs location info.
19553 Skip to where we fill the location bits. */
19554 var_die = old_die;
19555 goto gen_variable_die_location;
19559 /* For static data members, the declaration in the class is supposed
19560 to have DW_TAG_member tag; the specification should still be
19561 DW_TAG_variable referencing the DW_TAG_member DIE. */
19562 if (declaration && class_scope_p (context_die))
19563 var_die = new_die (DW_TAG_member, context_die, decl);
19564 else
19565 var_die = new_die (DW_TAG_variable, context_die, decl);
19567 if (origin != NULL)
19568 origin_die = add_abstract_origin_attribute (var_die, origin);
19570 /* Loop unrolling can create multiple blocks that refer to the same
19571 static variable, so we must test for the DW_AT_declaration flag.
19573 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19574 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19575 sharing them.
19577 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19578 else if (decl_will_get_specification_p (old_die, decl, declaration))
19580 /* This is a definition of a C++ class level static. */
19581 add_AT_specification (var_die, old_die);
19582 specialization_p = true;
19583 if (DECL_NAME (decl))
19585 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19586 struct dwarf_file_data * file_index = lookup_filename (s.file);
19588 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19589 add_AT_file (var_die, DW_AT_decl_file, file_index);
19591 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19592 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19594 if (old_die->die_tag == DW_TAG_member)
19595 add_linkage_name (var_die, decl);
19598 else
19599 add_name_and_src_coords_attributes (var_die, decl);
19601 if ((origin == NULL && !specialization_p)
19602 || (origin != NULL
19603 && !DECL_ABSTRACT_P (decl_or_origin)
19604 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19605 decl_function_context
19606 (decl_or_origin))))
19608 tree type = TREE_TYPE (decl_or_origin);
19610 if (decl_by_reference_p (decl_or_origin))
19611 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19612 context_die);
19613 else
19614 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19615 context_die);
19618 if (origin == NULL && !specialization_p)
19620 if (TREE_PUBLIC (decl))
19621 add_AT_flag (var_die, DW_AT_external, 1);
19623 if (DECL_ARTIFICIAL (decl))
19624 add_AT_flag (var_die, DW_AT_artificial, 1);
19626 add_accessibility_attribute (var_die, decl);
19629 if (declaration)
19630 add_AT_flag (var_die, DW_AT_declaration, 1);
19632 if (decl && (DECL_ABSTRACT_P (decl)
19633 || !old_die || is_declaration_die (old_die)))
19634 equate_decl_number_to_die (decl, var_die);
19636 gen_variable_die_location:
19637 if (! declaration
19638 && (! DECL_ABSTRACT_P (decl_or_origin)
19639 /* Local static vars are shared between all clones/inlines,
19640 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19641 already set. */
19642 || (TREE_CODE (decl_or_origin) == VAR_DECL
19643 && TREE_STATIC (decl_or_origin)
19644 && DECL_RTL_SET_P (decl_or_origin)))
19645 /* When abstract origin already has DW_AT_location attribute, no need
19646 to add it again. */
19647 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19649 if (early_dwarf)
19650 add_pubname (decl_or_origin, var_die);
19651 else
19652 add_location_or_const_value_attribute (var_die, decl_or_origin,
19653 decl == NULL, DW_AT_location);
19655 else
19656 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19659 /* Generate a DIE to represent a named constant. */
19661 static void
19662 gen_const_die (tree decl, dw_die_ref context_die)
19664 dw_die_ref const_die;
19665 tree type = TREE_TYPE (decl);
19667 const_die = lookup_decl_die (decl);
19668 if (const_die)
19669 return;
19671 const_die = new_die (DW_TAG_constant, context_die, decl);
19672 equate_decl_number_to_die (decl, const_die);
19673 add_name_and_src_coords_attributes (const_die, decl);
19674 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19675 if (TREE_PUBLIC (decl))
19676 add_AT_flag (const_die, DW_AT_external, 1);
19677 if (DECL_ARTIFICIAL (decl))
19678 add_AT_flag (const_die, DW_AT_artificial, 1);
19679 tree_add_const_value_attribute_for_decl (const_die, decl);
19682 /* Generate a DIE to represent a label identifier. */
19684 static void
19685 gen_label_die (tree decl, dw_die_ref context_die)
19687 tree origin = decl_ultimate_origin (decl);
19688 dw_die_ref lbl_die = lookup_decl_die (decl);
19689 rtx insn;
19690 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19692 if (!lbl_die)
19694 lbl_die = new_die (DW_TAG_label, context_die, decl);
19695 equate_decl_number_to_die (decl, lbl_die);
19697 if (origin != NULL)
19698 add_abstract_origin_attribute (lbl_die, origin);
19699 else
19700 add_name_and_src_coords_attributes (lbl_die, decl);
19703 if (DECL_ABSTRACT_P (decl))
19704 equate_decl_number_to_die (decl, lbl_die);
19705 else
19707 insn = DECL_RTL_IF_SET (decl);
19709 /* Deleted labels are programmer specified labels which have been
19710 eliminated because of various optimizations. We still emit them
19711 here so that it is possible to put breakpoints on them. */
19712 if (insn
19713 && (LABEL_P (insn)
19714 || ((NOTE_P (insn)
19715 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19717 /* When optimization is enabled (via -O) some parts of the compiler
19718 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19719 represent source-level labels which were explicitly declared by
19720 the user. This really shouldn't be happening though, so catch
19721 it if it ever does happen. */
19722 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19724 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19725 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19727 else if (insn
19728 && NOTE_P (insn)
19729 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19730 && CODE_LABEL_NUMBER (insn) != -1)
19732 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19733 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19738 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19739 attributes to the DIE for a block STMT, to describe where the inlined
19740 function was called from. This is similar to add_src_coords_attributes. */
19742 static inline void
19743 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19745 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19747 if (dwarf_version >= 3 || !dwarf_strict)
19749 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19750 add_AT_unsigned (die, DW_AT_call_line, s.line);
19755 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19756 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19758 static inline void
19759 add_high_low_attributes (tree stmt, dw_die_ref die)
19761 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19763 if (BLOCK_FRAGMENT_CHAIN (stmt)
19764 && (dwarf_version >= 3 || !dwarf_strict))
19766 tree chain, superblock = NULL_TREE;
19767 dw_die_ref pdie;
19768 dw_attr_ref attr = NULL;
19770 if (inlined_function_outer_scope_p (stmt))
19772 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19773 BLOCK_NUMBER (stmt));
19774 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19777 /* Optimize duplicate .debug_ranges lists or even tails of
19778 lists. If this BLOCK has same ranges as its supercontext,
19779 lookup DW_AT_ranges attribute in the supercontext (and
19780 recursively so), verify that the ranges_table contains the
19781 right values and use it instead of adding a new .debug_range. */
19782 for (chain = stmt, pdie = die;
19783 BLOCK_SAME_RANGE (chain);
19784 chain = BLOCK_SUPERCONTEXT (chain))
19786 dw_attr_ref new_attr;
19788 pdie = pdie->die_parent;
19789 if (pdie == NULL)
19790 break;
19791 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19792 break;
19793 new_attr = get_AT (pdie, DW_AT_ranges);
19794 if (new_attr == NULL
19795 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19796 break;
19797 attr = new_attr;
19798 superblock = BLOCK_SUPERCONTEXT (chain);
19800 if (attr != NULL
19801 && (ranges_table[attr->dw_attr_val.v.val_offset
19802 / 2 / DWARF2_ADDR_SIZE].num
19803 == BLOCK_NUMBER (superblock))
19804 && BLOCK_FRAGMENT_CHAIN (superblock))
19806 unsigned long off = attr->dw_attr_val.v.val_offset
19807 / 2 / DWARF2_ADDR_SIZE;
19808 unsigned long supercnt = 0, thiscnt = 0;
19809 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19810 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19812 ++supercnt;
19813 gcc_checking_assert (ranges_table[off + supercnt].num
19814 == BLOCK_NUMBER (chain));
19816 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19817 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19818 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19819 ++thiscnt;
19820 gcc_assert (supercnt >= thiscnt);
19821 add_AT_range_list (die, DW_AT_ranges,
19822 ((off + supercnt - thiscnt)
19823 * 2 * DWARF2_ADDR_SIZE),
19824 false);
19825 return;
19828 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19830 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19833 add_ranges (chain);
19834 chain = BLOCK_FRAGMENT_CHAIN (chain);
19836 while (chain);
19837 add_ranges (NULL);
19839 else
19841 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19842 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19843 BLOCK_NUMBER (stmt));
19844 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19845 BLOCK_NUMBER (stmt));
19846 add_AT_low_high_pc (die, label, label_high, false);
19850 /* Generate a DIE for a lexical block. */
19852 static void
19853 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19855 dw_die_ref old_die = BLOCK_DIE (stmt);
19856 dw_die_ref stmt_die = NULL;
19857 if (!old_die)
19859 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19860 BLOCK_DIE (stmt) = stmt_die;
19863 if (BLOCK_ABSTRACT (stmt))
19865 if (old_die)
19867 #ifdef ENABLE_CHECKING
19868 /* This must have been generated early and it won't even
19869 need location information since it's a DW_AT_inline
19870 function. */
19871 for (dw_die_ref c = context_die; c; c = c->die_parent)
19872 if (c->die_tag == DW_TAG_inlined_subroutine
19873 || c->die_tag == DW_TAG_subprogram)
19875 gcc_assert (get_AT (c, DW_AT_inline));
19876 break;
19878 #endif
19879 return;
19882 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19884 /* If this is an inlined instance, create a new lexical die for
19885 anything below to attach DW_AT_abstract_origin to. */
19886 if (old_die)
19888 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19889 BLOCK_DIE (stmt) = stmt_die;
19890 old_die = NULL;
19894 if (old_die)
19895 stmt_die = old_die;
19897 /* A non abstract block whose blocks have already been reordered
19898 should have the instruction range for this block. If so, set the
19899 high/low attributes. */
19900 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19902 gcc_assert (stmt_die);
19903 add_high_low_attributes (stmt, stmt_die);
19906 decls_for_scope (stmt, stmt_die);
19909 /* Generate a DIE for an inlined subprogram. */
19911 static void
19912 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19914 tree decl;
19916 /* The instance of function that is effectively being inlined shall not
19917 be abstract. */
19918 gcc_assert (! BLOCK_ABSTRACT (stmt));
19920 decl = block_ultimate_origin (stmt);
19922 /* Emit info for the abstract instance first, if we haven't yet. We
19923 must emit this even if the block is abstract, otherwise when we
19924 emit the block below (or elsewhere), we may end up trying to emit
19925 a die whose origin die hasn't been emitted, and crashing. */
19926 dwarf2out_abstract_function (decl);
19928 if (! BLOCK_ABSTRACT (stmt))
19930 dw_die_ref subr_die
19931 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19933 if (call_arg_locations)
19934 BLOCK_DIE (stmt) = subr_die;
19935 add_abstract_origin_attribute (subr_die, decl);
19936 if (TREE_ASM_WRITTEN (stmt))
19937 add_high_low_attributes (stmt, subr_die);
19938 add_call_src_coords_attributes (stmt, subr_die);
19940 decls_for_scope (stmt, subr_die);
19944 /* Generate a DIE for a field in a record, or structure. */
19946 static void
19947 gen_field_die (tree decl, dw_die_ref context_die)
19949 dw_die_ref decl_die;
19951 if (TREE_TYPE (decl) == error_mark_node)
19952 return;
19954 decl_die = new_die (DW_TAG_member, context_die, decl);
19955 add_name_and_src_coords_attributes (decl_die, decl);
19956 add_type_attribute (decl_die, member_declared_type (decl),
19957 decl_quals (decl), context_die);
19959 if (DECL_BIT_FIELD_TYPE (decl))
19961 add_byte_size_attribute (decl_die, decl);
19962 add_bit_size_attribute (decl_die, decl);
19963 add_bit_offset_attribute (decl_die, decl);
19966 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19967 add_data_member_location_attribute (decl_die, decl);
19969 if (DECL_ARTIFICIAL (decl))
19970 add_AT_flag (decl_die, DW_AT_artificial, 1);
19972 add_accessibility_attribute (decl_die, decl);
19974 /* Equate decl number to die, so that we can look up this decl later on. */
19975 equate_decl_number_to_die (decl, decl_die);
19978 #if 0
19979 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19980 Use modified_type_die instead.
19981 We keep this code here just in case these types of DIEs may be needed to
19982 represent certain things in other languages (e.g. Pascal) someday. */
19984 static void
19985 gen_pointer_type_die (tree type, dw_die_ref context_die)
19987 dw_die_ref ptr_die
19988 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19990 equate_type_number_to_die (type, ptr_die);
19991 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19992 context_die);
19993 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19996 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19997 Use modified_type_die instead.
19998 We keep this code here just in case these types of DIEs may be needed to
19999 represent certain things in other languages (e.g. Pascal) someday. */
20001 static void
20002 gen_reference_type_die (tree type, dw_die_ref context_die)
20004 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20006 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20007 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20008 else
20009 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20011 equate_type_number_to_die (type, ref_die);
20012 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20013 context_die);
20014 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20016 #endif
20018 /* Generate a DIE for a pointer to a member type. */
20020 static void
20021 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20023 dw_die_ref ptr_die
20024 = new_die (DW_TAG_ptr_to_member_type,
20025 scope_die_for (type, context_die), type);
20027 equate_type_number_to_die (type, ptr_die);
20028 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20029 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20030 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20031 context_die);
20034 typedef const char *dchar_p; /* For DEF_VEC_P. */
20036 static char *producer_string;
20038 /* Return a heap allocated producer string including command line options
20039 if -grecord-gcc-switches. */
20041 static char *
20042 gen_producer_string (void)
20044 size_t j;
20045 auto_vec<dchar_p> switches;
20046 const char *language_string = lang_hooks.name;
20047 char *producer, *tail;
20048 const char *p;
20049 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20050 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20052 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20053 switch (save_decoded_options[j].opt_index)
20055 case OPT_o:
20056 case OPT_d:
20057 case OPT_dumpbase:
20058 case OPT_dumpdir:
20059 case OPT_auxbase:
20060 case OPT_auxbase_strip:
20061 case OPT_quiet:
20062 case OPT_version:
20063 case OPT_v:
20064 case OPT_w:
20065 case OPT_L:
20066 case OPT_D:
20067 case OPT_I:
20068 case OPT_U:
20069 case OPT_SPECIAL_unknown:
20070 case OPT_SPECIAL_ignore:
20071 case OPT_SPECIAL_program_name:
20072 case OPT_SPECIAL_input_file:
20073 case OPT_grecord_gcc_switches:
20074 case OPT_gno_record_gcc_switches:
20075 case OPT__output_pch_:
20076 case OPT_fdiagnostics_show_location_:
20077 case OPT_fdiagnostics_show_option:
20078 case OPT_fdiagnostics_show_caret:
20079 case OPT_fdiagnostics_color_:
20080 case OPT_fverbose_asm:
20081 case OPT____:
20082 case OPT__sysroot_:
20083 case OPT_nostdinc:
20084 case OPT_nostdinc__:
20085 case OPT_fpreprocessed:
20086 case OPT_fltrans_output_list_:
20087 case OPT_fresolution_:
20088 /* Ignore these. */
20089 continue;
20090 default:
20091 if (cl_options[save_decoded_options[j].opt_index].flags
20092 & CL_NO_DWARF_RECORD)
20093 continue;
20094 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20095 == '-');
20096 switch (save_decoded_options[j].canonical_option[0][1])
20098 case 'M':
20099 case 'i':
20100 case 'W':
20101 continue;
20102 case 'f':
20103 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20104 "dump", 4) == 0)
20105 continue;
20106 break;
20107 default:
20108 break;
20110 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20111 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20112 break;
20115 producer = XNEWVEC (char, plen + 1 + len + 1);
20116 tail = producer;
20117 sprintf (tail, "%s %s", language_string, version_string);
20118 tail += plen;
20120 FOR_EACH_VEC_ELT (switches, j, p)
20122 len = strlen (p);
20123 *tail = ' ';
20124 memcpy (tail + 1, p, len);
20125 tail += len + 1;
20128 *tail = '\0';
20129 return producer;
20132 /* Given a C and/or C++ language/version string return the "highest".
20133 C++ is assumed to be "higher" than C in this case. Used for merging
20134 LTO translation unit languages. */
20135 static const char *
20136 highest_c_language (const char *lang1, const char *lang2)
20138 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20139 return "GNU C++14";
20140 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20141 return "GNU C++11";
20142 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20143 return "GNU C++98";
20145 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20146 return "GNU C11";
20147 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20148 return "GNU C99";
20149 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20150 return "GNU C89";
20152 gcc_unreachable ();
20156 /* Generate the DIE for the compilation unit. */
20158 static dw_die_ref
20159 gen_compile_unit_die (const char *filename)
20161 dw_die_ref die;
20162 const char *language_string = lang_hooks.name;
20163 int language;
20165 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20167 if (filename)
20169 add_name_attribute (die, filename);
20170 /* Don't add cwd for <built-in>. */
20171 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20172 add_comp_dir_attribute (die);
20175 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20177 /* If our producer is LTO try to figure out a common language to use
20178 from the global list of translation units. */
20179 if (strcmp (language_string, "GNU GIMPLE") == 0)
20181 unsigned i;
20182 tree t;
20183 const char *common_lang = NULL;
20185 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20187 if (!TRANSLATION_UNIT_LANGUAGE (t))
20188 continue;
20189 if (!common_lang)
20190 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20191 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20193 else if (strncmp (common_lang, "GNU C", 5) == 0
20194 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20195 /* Mixing C and C++ is ok, use C++ in that case. */
20196 common_lang = highest_c_language (common_lang,
20197 TRANSLATION_UNIT_LANGUAGE (t));
20198 else
20200 /* Fall back to C. */
20201 common_lang = NULL;
20202 break;
20206 if (common_lang)
20207 language_string = common_lang;
20210 language = DW_LANG_C;
20211 if (strncmp (language_string, "GNU C", 5) == 0
20212 && ISDIGIT (language_string[5]))
20214 language = DW_LANG_C89;
20215 if (dwarf_version >= 3 || !dwarf_strict)
20217 if (strcmp (language_string, "GNU C89") != 0)
20218 language = DW_LANG_C99;
20220 if (dwarf_version >= 5 /* || !dwarf_strict */)
20221 if (strcmp (language_string, "GNU C11") == 0)
20222 language = DW_LANG_C11;
20225 else if (strncmp (language_string, "GNU C++", 7) == 0)
20227 language = DW_LANG_C_plus_plus;
20228 if (dwarf_version >= 5 /* || !dwarf_strict */)
20230 if (strcmp (language_string, "GNU C++11") == 0)
20231 language = DW_LANG_C_plus_plus_11;
20232 else if (strcmp (language_string, "GNU C++14") == 0)
20233 language = DW_LANG_C_plus_plus_14;
20236 else if (strcmp (language_string, "GNU F77") == 0)
20237 language = DW_LANG_Fortran77;
20238 else if (strcmp (language_string, "GNU Pascal") == 0)
20239 language = DW_LANG_Pascal83;
20240 else if (dwarf_version >= 3 || !dwarf_strict)
20242 if (strcmp (language_string, "GNU Ada") == 0)
20243 language = DW_LANG_Ada95;
20244 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20246 language = DW_LANG_Fortran95;
20247 if (dwarf_version >= 5 /* || !dwarf_strict */)
20249 if (strcmp (language_string, "GNU Fortran2003") == 0)
20250 language = DW_LANG_Fortran03;
20251 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20252 language = DW_LANG_Fortran08;
20255 else if (strcmp (language_string, "GNU Java") == 0)
20256 language = DW_LANG_Java;
20257 else if (strcmp (language_string, "GNU Objective-C") == 0)
20258 language = DW_LANG_ObjC;
20259 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20260 language = DW_LANG_ObjC_plus_plus;
20261 else if (dwarf_version >= 5 || !dwarf_strict)
20263 if (strcmp (language_string, "GNU Go") == 0)
20264 language = DW_LANG_Go;
20267 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20268 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20269 language = DW_LANG_Fortran90;
20271 add_AT_unsigned (die, DW_AT_language, language);
20273 switch (language)
20275 case DW_LANG_Fortran77:
20276 case DW_LANG_Fortran90:
20277 case DW_LANG_Fortran95:
20278 case DW_LANG_Fortran03:
20279 case DW_LANG_Fortran08:
20280 /* Fortran has case insensitive identifiers and the front-end
20281 lowercases everything. */
20282 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20283 break;
20284 default:
20285 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20286 break;
20288 return die;
20291 /* Generate the DIE for a base class. */
20293 static void
20294 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20296 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20298 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20299 add_data_member_location_attribute (die, binfo);
20301 if (BINFO_VIRTUAL_P (binfo))
20302 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20304 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20305 children, otherwise the default is DW_ACCESS_public. In DWARF2
20306 the default has always been DW_ACCESS_private. */
20307 if (access == access_public_node)
20309 if (dwarf_version == 2
20310 || context_die->die_tag == DW_TAG_class_type)
20311 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20313 else if (access == access_protected_node)
20314 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20315 else if (dwarf_version > 2
20316 && context_die->die_tag != DW_TAG_class_type)
20317 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20320 /* Generate a DIE for a class member. */
20322 static void
20323 gen_member_die (tree type, dw_die_ref context_die)
20325 tree member;
20326 tree binfo = TYPE_BINFO (type);
20327 dw_die_ref child;
20329 /* If this is not an incomplete type, output descriptions of each of its
20330 members. Note that as we output the DIEs necessary to represent the
20331 members of this record or union type, we will also be trying to output
20332 DIEs to represent the *types* of those members. However the `type'
20333 function (above) will specifically avoid generating type DIEs for member
20334 types *within* the list of member DIEs for this (containing) type except
20335 for those types (of members) which are explicitly marked as also being
20336 members of this (containing) type themselves. The g++ front- end can
20337 force any given type to be treated as a member of some other (containing)
20338 type by setting the TYPE_CONTEXT of the given (member) type to point to
20339 the TREE node representing the appropriate (containing) type. */
20341 /* First output info about the base classes. */
20342 if (binfo)
20344 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20345 int i;
20346 tree base;
20348 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20349 gen_inheritance_die (base,
20350 (accesses ? (*accesses)[i] : access_public_node),
20351 context_die);
20354 /* Now output info about the data members and type members. */
20355 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20357 /* If we thought we were generating minimal debug info for TYPE
20358 and then changed our minds, some of the member declarations
20359 may have already been defined. Don't define them again, but
20360 do put them in the right order. */
20362 child = lookup_decl_die (member);
20363 if (child)
20364 splice_child_die (context_die, child);
20365 else
20366 gen_decl_die (member, NULL, context_die);
20369 /* We do not keep type methods in type variants. */
20370 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20371 /* Now output info about the function members (if any). */
20372 if (TYPE_METHODS (type) != error_mark_node)
20373 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20375 /* Don't include clones in the member list. */
20376 if (DECL_ABSTRACT_ORIGIN (member))
20377 continue;
20378 /* Nor constructors for anonymous classes. */
20379 if (DECL_ARTIFICIAL (member)
20380 && dwarf2_name (member, 0) == NULL)
20381 continue;
20383 child = lookup_decl_die (member);
20384 if (child)
20385 splice_child_die (context_die, child);
20386 else
20387 gen_decl_die (member, NULL, context_die);
20391 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20392 is set, we pretend that the type was never defined, so we only get the
20393 member DIEs needed by later specification DIEs. */
20395 static void
20396 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20397 enum debug_info_usage usage)
20399 if (TREE_ASM_WRITTEN (type))
20401 /* Fill in the bound of variable-length fields in late dwarf if
20402 still incomplete. */
20403 if (!early_dwarf && variably_modified_type_p (type, NULL))
20404 for (tree member = TYPE_FIELDS (type);
20405 member;
20406 member = DECL_CHAIN (member))
20407 fill_variable_array_bounds (TREE_TYPE (member));
20408 return;
20411 dw_die_ref type_die = lookup_type_die (type);
20412 dw_die_ref scope_die = 0;
20413 int nested = 0;
20414 int complete = (TYPE_SIZE (type)
20415 && (! TYPE_STUB_DECL (type)
20416 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20417 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20418 complete = complete && should_emit_struct_debug (type, usage);
20420 if (type_die && ! complete)
20421 return;
20423 if (TYPE_CONTEXT (type) != NULL_TREE
20424 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20425 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20426 nested = 1;
20428 scope_die = scope_die_for (type, context_die);
20430 /* Generate child dies for template paramaters. */
20431 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20432 schedule_generic_params_dies_gen (type);
20434 if (! type_die || (nested && is_cu_die (scope_die)))
20435 /* First occurrence of type or toplevel definition of nested class. */
20437 dw_die_ref old_die = type_die;
20439 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20440 ? record_type_tag (type) : DW_TAG_union_type,
20441 scope_die, type);
20442 equate_type_number_to_die (type, type_die);
20443 if (old_die)
20444 add_AT_specification (type_die, old_die);
20445 else
20446 add_name_attribute (type_die, type_tag (type));
20448 else
20449 remove_AT (type_die, DW_AT_declaration);
20451 /* If this type has been completed, then give it a byte_size attribute and
20452 then give a list of members. */
20453 if (complete && !ns_decl)
20455 /* Prevent infinite recursion in cases where the type of some member of
20456 this type is expressed in terms of this type itself. */
20457 TREE_ASM_WRITTEN (type) = 1;
20458 add_byte_size_attribute (type_die, type);
20459 if (TYPE_STUB_DECL (type) != NULL_TREE)
20461 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20462 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20465 /* If the first reference to this type was as the return type of an
20466 inline function, then it may not have a parent. Fix this now. */
20467 if (type_die->die_parent == NULL)
20468 add_child_die (scope_die, type_die);
20470 push_decl_scope (type);
20471 gen_member_die (type, type_die);
20472 pop_decl_scope ();
20474 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20475 if (TYPE_ARTIFICIAL (type))
20476 add_AT_flag (type_die, DW_AT_artificial, 1);
20478 /* GNU extension: Record what type our vtable lives in. */
20479 if (TYPE_VFIELD (type))
20481 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20483 gen_type_die (vtype, context_die);
20484 add_AT_die_ref (type_die, DW_AT_containing_type,
20485 lookup_type_die (vtype));
20488 else
20490 add_AT_flag (type_die, DW_AT_declaration, 1);
20492 /* We don't need to do this for function-local types. */
20493 if (TYPE_STUB_DECL (type)
20494 && ! decl_function_context (TYPE_STUB_DECL (type)))
20495 vec_safe_push (incomplete_types, type);
20498 if (get_AT (type_die, DW_AT_name))
20499 add_pubtype (type, type_die);
20502 /* Generate a DIE for a subroutine _type_. */
20504 static void
20505 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20507 tree return_type = TREE_TYPE (type);
20508 dw_die_ref subr_die
20509 = new_die (DW_TAG_subroutine_type,
20510 scope_die_for (type, context_die), type);
20512 equate_type_number_to_die (type, subr_die);
20513 add_prototyped_attribute (subr_die, type);
20514 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20515 gen_formal_types_die (type, subr_die);
20517 if (get_AT (subr_die, DW_AT_name))
20518 add_pubtype (type, subr_die);
20521 /* Generate a DIE for a type definition. */
20523 static void
20524 gen_typedef_die (tree decl, dw_die_ref context_die)
20526 dw_die_ref type_die;
20527 tree origin;
20529 if (TREE_ASM_WRITTEN (decl))
20531 if (DECL_ORIGINAL_TYPE (decl))
20532 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20533 return;
20536 TREE_ASM_WRITTEN (decl) = 1;
20537 type_die = new_die (DW_TAG_typedef, context_die, decl);
20538 origin = decl_ultimate_origin (decl);
20539 if (origin != NULL)
20540 add_abstract_origin_attribute (type_die, origin);
20541 else
20543 tree type;
20545 add_name_and_src_coords_attributes (type_die, decl);
20546 if (DECL_ORIGINAL_TYPE (decl))
20548 type = DECL_ORIGINAL_TYPE (decl);
20550 if (type == error_mark_node)
20551 return;
20553 gcc_assert (type != TREE_TYPE (decl));
20554 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20556 else
20558 type = TREE_TYPE (decl);
20560 if (type == error_mark_node)
20561 return;
20563 if (is_naming_typedef_decl (TYPE_NAME (type)))
20565 /* Here, we are in the case of decl being a typedef naming
20566 an anonymous type, e.g:
20567 typedef struct {...} foo;
20568 In that case TREE_TYPE (decl) is not a typedef variant
20569 type and TYPE_NAME of the anonymous type is set to the
20570 TYPE_DECL of the typedef. This construct is emitted by
20571 the C++ FE.
20573 TYPE is the anonymous struct named by the typedef
20574 DECL. As we need the DW_AT_type attribute of the
20575 DW_TAG_typedef to point to the DIE of TYPE, let's
20576 generate that DIE right away. add_type_attribute
20577 called below will then pick (via lookup_type_die) that
20578 anonymous struct DIE. */
20579 if (!TREE_ASM_WRITTEN (type))
20580 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20582 /* This is a GNU Extension. We are adding a
20583 DW_AT_linkage_name attribute to the DIE of the
20584 anonymous struct TYPE. The value of that attribute
20585 is the name of the typedef decl naming the anonymous
20586 struct. This greatly eases the work of consumers of
20587 this debug info. */
20588 add_linkage_attr (lookup_type_die (type), decl);
20592 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20594 if (is_naming_typedef_decl (decl))
20595 /* We want that all subsequent calls to lookup_type_die with
20596 TYPE in argument yield the DW_TAG_typedef we have just
20597 created. */
20598 equate_type_number_to_die (type, type_die);
20600 add_accessibility_attribute (type_die, decl);
20603 if (DECL_ABSTRACT_P (decl))
20604 equate_decl_number_to_die (decl, type_die);
20606 if (get_AT (type_die, DW_AT_name))
20607 add_pubtype (decl, type_die);
20610 /* Generate a DIE for a struct, class, enum or union type. */
20612 static void
20613 gen_tagged_type_die (tree type,
20614 dw_die_ref context_die,
20615 enum debug_info_usage usage)
20617 int need_pop;
20619 if (type == NULL_TREE
20620 || !is_tagged_type (type))
20621 return;
20623 if (TREE_ASM_WRITTEN (type))
20624 need_pop = 0;
20625 /* If this is a nested type whose containing class hasn't been written
20626 out yet, writing it out will cover this one, too. This does not apply
20627 to instantiations of member class templates; they need to be added to
20628 the containing class as they are generated. FIXME: This hurts the
20629 idea of combining type decls from multiple TUs, since we can't predict
20630 what set of template instantiations we'll get. */
20631 else if (TYPE_CONTEXT (type)
20632 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20633 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20635 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20637 if (TREE_ASM_WRITTEN (type))
20638 return;
20640 /* If that failed, attach ourselves to the stub. */
20641 push_decl_scope (TYPE_CONTEXT (type));
20642 context_die = lookup_type_die (TYPE_CONTEXT (type));
20643 need_pop = 1;
20645 else if (TYPE_CONTEXT (type) != NULL_TREE
20646 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20648 /* If this type is local to a function that hasn't been written
20649 out yet, use a NULL context for now; it will be fixed up in
20650 decls_for_scope. */
20651 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20652 /* A declaration DIE doesn't count; nested types need to go in the
20653 specification. */
20654 if (context_die && is_declaration_die (context_die))
20655 context_die = NULL;
20656 need_pop = 0;
20658 else
20660 context_die = declare_in_namespace (type, context_die);
20661 need_pop = 0;
20664 if (TREE_CODE (type) == ENUMERAL_TYPE)
20666 /* This might have been written out by the call to
20667 declare_in_namespace. */
20668 if (!TREE_ASM_WRITTEN (type))
20669 gen_enumeration_type_die (type, context_die);
20671 else
20672 gen_struct_or_union_type_die (type, context_die, usage);
20674 if (need_pop)
20675 pop_decl_scope ();
20677 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20678 it up if it is ever completed. gen_*_type_die will set it for us
20679 when appropriate. */
20682 /* Generate a type description DIE. */
20684 static void
20685 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20686 enum debug_info_usage usage)
20688 struct array_descr_info info;
20690 if (type == NULL_TREE || type == error_mark_node)
20691 return;
20693 #ifdef ENABLE_CHECKING
20694 if (type)
20695 verify_type (type);
20696 #endif
20698 if (TYPE_NAME (type) != NULL_TREE
20699 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20700 && is_redundant_typedef (TYPE_NAME (type))
20701 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20702 /* The DECL of this type is a typedef we don't want to emit debug
20703 info for but we want debug info for its underlying typedef.
20704 This can happen for e.g, the injected-class-name of a C++
20705 type. */
20706 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20708 /* If TYPE is a typedef type variant, let's generate debug info
20709 for the parent typedef which TYPE is a type of. */
20710 if (typedef_variant_p (type))
20712 if (TREE_ASM_WRITTEN (type))
20713 return;
20715 /* Prevent broken recursion; we can't hand off to the same type. */
20716 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20718 /* Give typedefs the right scope. */
20719 context_die = scope_die_for (type, context_die);
20721 TREE_ASM_WRITTEN (type) = 1;
20723 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20724 return;
20727 /* If type is an anonymous tagged type named by a typedef, let's
20728 generate debug info for the typedef. */
20729 if (is_naming_typedef_decl (TYPE_NAME (type)))
20731 /* Use the DIE of the containing namespace as the parent DIE of
20732 the type description DIE we want to generate. */
20733 if (DECL_CONTEXT (TYPE_NAME (type))
20734 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20735 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20737 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20738 return;
20741 /* We are going to output a DIE to represent the unqualified version
20742 of this type (i.e. without any const or volatile qualifiers) so
20743 get the main variant (i.e. the unqualified version) of this type
20744 now. (Vectors are special because the debugging info is in the
20745 cloned type itself). */
20746 if (TREE_CODE (type) != VECTOR_TYPE)
20747 type = type_main_variant (type);
20749 /* If this is an array type with hidden descriptor, handle it first. */
20750 if (!TREE_ASM_WRITTEN (type)
20751 && lang_hooks.types.get_array_descr_info)
20753 memset (&info, 0, sizeof (info));
20754 if (lang_hooks.types.get_array_descr_info (type, &info))
20756 gen_descr_array_type_die (type, &info, context_die);
20757 TREE_ASM_WRITTEN (type) = 1;
20758 return;
20762 if (TREE_ASM_WRITTEN (type))
20764 /* Variable-length types may be incomplete even if
20765 TREE_ASM_WRITTEN. For such types, fall through to
20766 gen_array_type_die() and possibly fill in
20767 DW_AT_{upper,lower}_bound attributes. */
20768 if ((TREE_CODE (type) != ARRAY_TYPE
20769 && TREE_CODE (type) != RECORD_TYPE
20770 && TREE_CODE (type) != UNION_TYPE
20771 && TREE_CODE (type) != QUAL_UNION_TYPE)
20772 || !variably_modified_type_p (type, NULL))
20773 return;
20776 switch (TREE_CODE (type))
20778 case ERROR_MARK:
20779 break;
20781 case POINTER_TYPE:
20782 case REFERENCE_TYPE:
20783 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20784 ensures that the gen_type_die recursion will terminate even if the
20785 type is recursive. Recursive types are possible in Ada. */
20786 /* ??? We could perhaps do this for all types before the switch
20787 statement. */
20788 TREE_ASM_WRITTEN (type) = 1;
20790 /* For these types, all that is required is that we output a DIE (or a
20791 set of DIEs) to represent the "basis" type. */
20792 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20793 DINFO_USAGE_IND_USE);
20794 break;
20796 case OFFSET_TYPE:
20797 /* This code is used for C++ pointer-to-data-member types.
20798 Output a description of the relevant class type. */
20799 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20800 DINFO_USAGE_IND_USE);
20802 /* Output a description of the type of the object pointed to. */
20803 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20804 DINFO_USAGE_IND_USE);
20806 /* Now output a DIE to represent this pointer-to-data-member type
20807 itself. */
20808 gen_ptr_to_mbr_type_die (type, context_die);
20809 break;
20811 case FUNCTION_TYPE:
20812 /* Force out return type (in case it wasn't forced out already). */
20813 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20814 DINFO_USAGE_DIR_USE);
20815 gen_subroutine_type_die (type, context_die);
20816 break;
20818 case METHOD_TYPE:
20819 /* Force out return type (in case it wasn't forced out already). */
20820 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20821 DINFO_USAGE_DIR_USE);
20822 gen_subroutine_type_die (type, context_die);
20823 break;
20825 case ARRAY_TYPE:
20826 case VECTOR_TYPE:
20827 gen_array_type_die (type, context_die);
20828 break;
20830 case ENUMERAL_TYPE:
20831 case RECORD_TYPE:
20832 case UNION_TYPE:
20833 case QUAL_UNION_TYPE:
20834 gen_tagged_type_die (type, context_die, usage);
20835 return;
20837 case VOID_TYPE:
20838 case INTEGER_TYPE:
20839 case REAL_TYPE:
20840 case FIXED_POINT_TYPE:
20841 case COMPLEX_TYPE:
20842 case BOOLEAN_TYPE:
20843 case POINTER_BOUNDS_TYPE:
20844 /* No DIEs needed for fundamental types. */
20845 break;
20847 case NULLPTR_TYPE:
20848 case LANG_TYPE:
20849 /* Just use DW_TAG_unspecified_type. */
20851 dw_die_ref type_die = lookup_type_die (type);
20852 if (type_die == NULL)
20854 tree name = TYPE_IDENTIFIER (type);
20855 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20856 type);
20857 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20858 equate_type_number_to_die (type, type_die);
20861 break;
20863 default:
20864 if (is_cxx_auto (type))
20866 tree name = TYPE_IDENTIFIER (type);
20867 dw_die_ref *die = (name == get_identifier ("auto")
20868 ? &auto_die : &decltype_auto_die);
20869 if (!*die)
20871 *die = new_die (DW_TAG_unspecified_type,
20872 comp_unit_die (), NULL_TREE);
20873 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20875 equate_type_number_to_die (type, *die);
20876 break;
20878 gcc_unreachable ();
20881 TREE_ASM_WRITTEN (type) = 1;
20884 static void
20885 gen_type_die (tree type, dw_die_ref context_die)
20887 if (type != error_mark_node)
20889 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20890 #ifdef ENABLE_CHECKING
20891 dw_die_ref die = lookup_type_die (type);
20892 if (die)
20893 check_die (die);
20894 #endif
20898 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20899 things which are local to the given block. */
20901 static void
20902 gen_block_die (tree stmt, dw_die_ref context_die)
20904 int must_output_die = 0;
20905 bool inlined_func;
20907 /* Ignore blocks that are NULL. */
20908 if (stmt == NULL_TREE)
20909 return;
20911 inlined_func = inlined_function_outer_scope_p (stmt);
20913 /* If the block is one fragment of a non-contiguous block, do not
20914 process the variables, since they will have been done by the
20915 origin block. Do process subblocks. */
20916 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20918 tree sub;
20920 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20921 gen_block_die (sub, context_die);
20923 return;
20926 /* Determine if we need to output any Dwarf DIEs at all to represent this
20927 block. */
20928 if (inlined_func)
20929 /* The outer scopes for inlinings *must* always be represented. We
20930 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20931 must_output_die = 1;
20932 else
20934 /* Determine if this block directly contains any "significant"
20935 local declarations which we will need to output DIEs for. */
20936 if (debug_info_level > DINFO_LEVEL_TERSE)
20937 /* We are not in terse mode so *any* local declaration counts
20938 as being a "significant" one. */
20939 must_output_die = ((BLOCK_VARS (stmt) != NULL
20940 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20941 && (TREE_USED (stmt)
20942 || TREE_ASM_WRITTEN (stmt)
20943 || BLOCK_ABSTRACT (stmt)));
20944 else if ((TREE_USED (stmt)
20945 || TREE_ASM_WRITTEN (stmt)
20946 || BLOCK_ABSTRACT (stmt))
20947 && !dwarf2out_ignore_block (stmt))
20948 must_output_die = 1;
20951 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20952 DIE for any block which contains no significant local declarations at
20953 all. Rather, in such cases we just call `decls_for_scope' so that any
20954 needed Dwarf info for any sub-blocks will get properly generated. Note
20955 that in terse mode, our definition of what constitutes a "significant"
20956 local declaration gets restricted to include only inlined function
20957 instances and local (nested) function definitions. */
20958 if (must_output_die)
20960 if (inlined_func)
20962 /* If STMT block is abstract, that means we have been called
20963 indirectly from dwarf2out_abstract_function.
20964 That function rightfully marks the descendent blocks (of
20965 the abstract function it is dealing with) as being abstract,
20966 precisely to prevent us from emitting any
20967 DW_TAG_inlined_subroutine DIE as a descendent
20968 of an abstract function instance. So in that case, we should
20969 not call gen_inlined_subroutine_die.
20971 Later though, when cgraph asks dwarf2out to emit info
20972 for the concrete instance of the function decl into which
20973 the concrete instance of STMT got inlined, the later will lead
20974 to the generation of a DW_TAG_inlined_subroutine DIE. */
20975 if (! BLOCK_ABSTRACT (stmt))
20976 gen_inlined_subroutine_die (stmt, context_die);
20978 else
20979 gen_lexical_block_die (stmt, context_die);
20981 else
20982 decls_for_scope (stmt, context_die);
20985 /* Process variable DECL (or variable with origin ORIGIN) within
20986 block STMT and add it to CONTEXT_DIE. */
20987 static void
20988 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20990 dw_die_ref die;
20991 tree decl_or_origin = decl ? decl : origin;
20993 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20994 die = lookup_decl_die (decl_or_origin);
20995 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20996 && TYPE_DECL_IS_STUB (decl_or_origin))
20997 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20998 else
20999 die = NULL;
21001 if (die != NULL && die->die_parent == NULL)
21002 add_child_die (context_die, die);
21003 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21005 if (early_dwarf)
21006 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21007 stmt, context_die);
21009 else
21010 gen_decl_die (decl, origin, context_die);
21013 /* Generate all of the decls declared within a given scope and (recursively)
21014 all of its sub-blocks. */
21016 static void
21017 decls_for_scope (tree stmt, dw_die_ref context_die)
21019 tree decl;
21020 unsigned int i;
21021 tree subblocks;
21023 /* Ignore NULL blocks. */
21024 if (stmt == NULL_TREE)
21025 return;
21027 /* Output the DIEs to represent all of the data objects and typedefs
21028 declared directly within this block but not within any nested
21029 sub-blocks. Also, nested function and tag DIEs have been
21030 generated with a parent of NULL; fix that up now. We don't
21031 have to do this if we're at -g1. */
21032 if (debug_info_level > DINFO_LEVEL_TERSE)
21034 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21035 process_scope_var (stmt, decl, NULL_TREE, context_die);
21036 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21037 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21038 context_die);
21041 /* Even if we're at -g1, we need to process the subblocks in order to get
21042 inlined call information. */
21044 /* Output the DIEs to represent all sub-blocks (and the items declared
21045 therein) of this block. */
21046 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21047 subblocks != NULL;
21048 subblocks = BLOCK_CHAIN (subblocks))
21049 gen_block_die (subblocks, context_die);
21052 /* Is this a typedef we can avoid emitting? */
21054 static inline int
21055 is_redundant_typedef (const_tree decl)
21057 if (TYPE_DECL_IS_STUB (decl))
21058 return 1;
21060 if (DECL_ARTIFICIAL (decl)
21061 && DECL_CONTEXT (decl)
21062 && is_tagged_type (DECL_CONTEXT (decl))
21063 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21064 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21065 /* Also ignore the artificial member typedef for the class name. */
21066 return 1;
21068 return 0;
21071 /* Return TRUE if TYPE is a typedef that names a type for linkage
21072 purposes. This kind of typedefs is produced by the C++ FE for
21073 constructs like:
21075 typedef struct {...} foo;
21077 In that case, there is no typedef variant type produced for foo.
21078 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21079 struct type. */
21081 static bool
21082 is_naming_typedef_decl (const_tree decl)
21084 if (decl == NULL_TREE
21085 || TREE_CODE (decl) != TYPE_DECL
21086 || !is_tagged_type (TREE_TYPE (decl))
21087 || DECL_IS_BUILTIN (decl)
21088 || is_redundant_typedef (decl)
21089 /* It looks like Ada produces TYPE_DECLs that are very similar
21090 to C++ naming typedefs but that have different
21091 semantics. Let's be specific to c++ for now. */
21092 || !is_cxx ())
21093 return FALSE;
21095 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21096 && TYPE_NAME (TREE_TYPE (decl)) == decl
21097 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21098 != TYPE_NAME (TREE_TYPE (decl))));
21101 /* Looks up the DIE for a context. */
21103 static inline dw_die_ref
21104 lookup_context_die (tree context)
21106 if (context)
21108 /* Find die that represents this context. */
21109 if (TYPE_P (context))
21111 context = TYPE_MAIN_VARIANT (context);
21112 dw_die_ref ctx = lookup_type_die (context);
21113 if (!ctx)
21114 return NULL;
21115 return strip_naming_typedef (context, ctx);
21117 else
21118 return lookup_decl_die (context);
21120 return comp_unit_die ();
21123 /* Returns the DIE for a context. */
21125 static inline dw_die_ref
21126 get_context_die (tree context)
21128 if (context)
21130 /* Find die that represents this context. */
21131 if (TYPE_P (context))
21133 context = TYPE_MAIN_VARIANT (context);
21134 return strip_naming_typedef (context, force_type_die (context));
21136 else
21137 return force_decl_die (context);
21139 return comp_unit_die ();
21142 /* Returns the DIE for decl. A DIE will always be returned. */
21144 static dw_die_ref
21145 force_decl_die (tree decl)
21147 dw_die_ref decl_die;
21148 unsigned saved_external_flag;
21149 tree save_fn = NULL_TREE;
21150 decl_die = lookup_decl_die (decl);
21151 if (!decl_die)
21153 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21155 decl_die = lookup_decl_die (decl);
21156 if (decl_die)
21157 return decl_die;
21159 switch (TREE_CODE (decl))
21161 case FUNCTION_DECL:
21162 /* Clear current_function_decl, so that gen_subprogram_die thinks
21163 that this is a declaration. At this point, we just want to force
21164 declaration die. */
21165 save_fn = current_function_decl;
21166 current_function_decl = NULL_TREE;
21167 gen_subprogram_die (decl, context_die);
21168 current_function_decl = save_fn;
21169 break;
21171 case VAR_DECL:
21172 /* Set external flag to force declaration die. Restore it after
21173 gen_decl_die() call. */
21174 saved_external_flag = DECL_EXTERNAL (decl);
21175 DECL_EXTERNAL (decl) = 1;
21176 gen_decl_die (decl, NULL, context_die);
21177 DECL_EXTERNAL (decl) = saved_external_flag;
21178 break;
21180 case NAMESPACE_DECL:
21181 if (dwarf_version >= 3 || !dwarf_strict)
21182 dwarf2out_decl (decl);
21183 else
21184 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21185 decl_die = comp_unit_die ();
21186 break;
21188 case TRANSLATION_UNIT_DECL:
21189 decl_die = comp_unit_die ();
21190 break;
21192 default:
21193 gcc_unreachable ();
21196 /* We should be able to find the DIE now. */
21197 if (!decl_die)
21198 decl_die = lookup_decl_die (decl);
21199 gcc_assert (decl_die);
21202 return decl_die;
21205 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21206 always returned. */
21208 static dw_die_ref
21209 force_type_die (tree type)
21211 dw_die_ref type_die;
21213 type_die = lookup_type_die (type);
21214 if (!type_die)
21216 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21218 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21219 context_die);
21220 gcc_assert (type_die);
21222 return type_die;
21225 /* Force out any required namespaces to be able to output DECL,
21226 and return the new context_die for it, if it's changed. */
21228 static dw_die_ref
21229 setup_namespace_context (tree thing, dw_die_ref context_die)
21231 tree context = (DECL_P (thing)
21232 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21233 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21234 /* Force out the namespace. */
21235 context_die = force_decl_die (context);
21237 return context_die;
21240 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21241 type) within its namespace, if appropriate.
21243 For compatibility with older debuggers, namespace DIEs only contain
21244 declarations; all definitions are emitted at CU scope, with
21245 DW_AT_specification pointing to the declaration (like with class
21246 members). */
21248 static dw_die_ref
21249 declare_in_namespace (tree thing, dw_die_ref context_die)
21251 dw_die_ref ns_context;
21253 if (debug_info_level <= DINFO_LEVEL_TERSE)
21254 return context_die;
21256 /* External declarations in the local scope only need to be emitted
21257 once, not once in the namespace and once in the scope.
21259 This avoids declaring the `extern' below in the
21260 namespace DIE as well as in the innermost scope:
21262 namespace S
21264 int i=5;
21265 int foo()
21267 int i=8;
21268 extern int i;
21269 return i;
21273 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21274 return context_die;
21276 /* If this decl is from an inlined function, then don't try to emit it in its
21277 namespace, as we will get confused. It would have already been emitted
21278 when the abstract instance of the inline function was emitted anyways. */
21279 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21280 return context_die;
21282 ns_context = setup_namespace_context (thing, context_die);
21284 if (ns_context != context_die)
21286 if (is_fortran ())
21287 return ns_context;
21288 if (DECL_P (thing))
21289 gen_decl_die (thing, NULL, ns_context);
21290 else
21291 gen_type_die (thing, ns_context);
21293 return context_die;
21296 /* Generate a DIE for a namespace or namespace alias. */
21298 static void
21299 gen_namespace_die (tree decl, dw_die_ref context_die)
21301 dw_die_ref namespace_die;
21303 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21304 they are an alias of. */
21305 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21307 /* Output a real namespace or module. */
21308 context_die = setup_namespace_context (decl, comp_unit_die ());
21309 namespace_die = new_die (is_fortran ()
21310 ? DW_TAG_module : DW_TAG_namespace,
21311 context_die, decl);
21312 /* For Fortran modules defined in different CU don't add src coords. */
21313 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21315 const char *name = dwarf2_name (decl, 0);
21316 if (name)
21317 add_name_attribute (namespace_die, name);
21319 else
21320 add_name_and_src_coords_attributes (namespace_die, decl);
21321 if (DECL_EXTERNAL (decl))
21322 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21323 equate_decl_number_to_die (decl, namespace_die);
21325 else
21327 /* Output a namespace alias. */
21329 /* Force out the namespace we are an alias of, if necessary. */
21330 dw_die_ref origin_die
21331 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21333 if (DECL_FILE_SCOPE_P (decl)
21334 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21335 context_die = setup_namespace_context (decl, comp_unit_die ());
21336 /* Now create the namespace alias DIE. */
21337 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21338 add_name_and_src_coords_attributes (namespace_die, decl);
21339 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21340 equate_decl_number_to_die (decl, namespace_die);
21342 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21343 if (want_pubnames ())
21344 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21347 /* Generate Dwarf debug information for a decl described by DECL.
21348 The return value is currently only meaningful for PARM_DECLs,
21349 for all other decls it returns NULL. */
21351 static dw_die_ref
21352 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21354 tree decl_or_origin = decl ? decl : origin;
21355 tree class_origin = NULL, ultimate_origin;
21357 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21358 return NULL;
21360 /* Ignore pointer bounds decls. */
21361 if (DECL_P (decl_or_origin)
21362 && TREE_TYPE (decl_or_origin)
21363 && POINTER_BOUNDS_P (decl_or_origin))
21364 return NULL;
21366 switch (TREE_CODE (decl_or_origin))
21368 case ERROR_MARK:
21369 break;
21371 case CONST_DECL:
21372 if (!is_fortran () && !is_ada ())
21374 /* The individual enumerators of an enum type get output when we output
21375 the Dwarf representation of the relevant enum type itself. */
21376 break;
21379 /* Emit its type. */
21380 gen_type_die (TREE_TYPE (decl), context_die);
21382 /* And its containing namespace. */
21383 context_die = declare_in_namespace (decl, context_die);
21385 gen_const_die (decl, context_die);
21386 break;
21388 case FUNCTION_DECL:
21389 /* Don't output any DIEs to represent mere function declarations,
21390 unless they are class members or explicit block externs. */
21391 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21392 && DECL_FILE_SCOPE_P (decl_or_origin)
21393 && (current_function_decl == NULL_TREE
21394 || DECL_ARTIFICIAL (decl_or_origin)))
21395 break;
21397 #if 0
21398 /* FIXME */
21399 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21400 on local redeclarations of global functions. That seems broken. */
21401 if (current_function_decl != decl)
21402 /* This is only a declaration. */;
21403 #endif
21405 /* If we're emitting a clone, emit info for the abstract instance. */
21406 if (origin || DECL_ORIGIN (decl) != decl)
21407 dwarf2out_abstract_function (origin
21408 ? DECL_ORIGIN (origin)
21409 : DECL_ABSTRACT_ORIGIN (decl));
21411 /* If we're emitting an out-of-line copy of an inline function,
21412 emit info for the abstract instance and set up to refer to it. */
21413 else if (cgraph_function_possibly_inlined_p (decl)
21414 && ! DECL_ABSTRACT_P (decl)
21415 && ! class_or_namespace_scope_p (context_die)
21416 /* dwarf2out_abstract_function won't emit a die if this is just
21417 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21418 that case, because that works only if we have a die. */
21419 && DECL_INITIAL (decl) != NULL_TREE)
21421 dwarf2out_abstract_function (decl);
21422 set_decl_origin_self (decl);
21425 /* Otherwise we're emitting the primary DIE for this decl. */
21426 else if (debug_info_level > DINFO_LEVEL_TERSE)
21428 /* Before we describe the FUNCTION_DECL itself, make sure that we
21429 have its containing type. */
21430 if (!origin)
21431 origin = decl_class_context (decl);
21432 if (origin != NULL_TREE)
21433 gen_type_die (origin, context_die);
21435 /* And its return type. */
21436 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21438 /* And its virtual context. */
21439 if (DECL_VINDEX (decl) != NULL_TREE)
21440 gen_type_die (DECL_CONTEXT (decl), context_die);
21442 /* Make sure we have a member DIE for decl. */
21443 if (origin != NULL_TREE)
21444 gen_type_die_for_member (origin, decl, context_die);
21446 /* And its containing namespace. */
21447 context_die = declare_in_namespace (decl, context_die);
21450 /* Now output a DIE to represent the function itself. */
21451 if (decl)
21452 gen_subprogram_die (decl, context_die);
21453 break;
21455 case TYPE_DECL:
21456 /* If we are in terse mode, don't generate any DIEs to represent any
21457 actual typedefs. */
21458 if (debug_info_level <= DINFO_LEVEL_TERSE)
21459 break;
21461 /* In the special case of a TYPE_DECL node representing the declaration
21462 of some type tag, if the given TYPE_DECL is marked as having been
21463 instantiated from some other (original) TYPE_DECL node (e.g. one which
21464 was generated within the original definition of an inline function) we
21465 used to generate a special (abbreviated) DW_TAG_structure_type,
21466 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21467 should be actually referencing those DIEs, as variable DIEs with that
21468 type would be emitted already in the abstract origin, so it was always
21469 removed during unused type prunning. Don't add anything in this
21470 case. */
21471 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21472 break;
21474 if (is_redundant_typedef (decl))
21475 gen_type_die (TREE_TYPE (decl), context_die);
21476 else
21477 /* Output a DIE to represent the typedef itself. */
21478 gen_typedef_die (decl, context_die);
21479 break;
21481 case LABEL_DECL:
21482 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21483 gen_label_die (decl, context_die);
21484 break;
21486 case VAR_DECL:
21487 case RESULT_DECL:
21488 /* If we are in terse mode, don't generate any DIEs to represent any
21489 variable declarations or definitions. */
21490 if (debug_info_level <= DINFO_LEVEL_TERSE)
21491 break;
21493 /* Output any DIEs that are needed to specify the type of this data
21494 object. */
21495 if (decl_by_reference_p (decl_or_origin))
21496 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21497 else
21498 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21500 /* And its containing type. */
21501 class_origin = decl_class_context (decl_or_origin);
21502 if (class_origin != NULL_TREE)
21503 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21505 /* And its containing namespace. */
21506 context_die = declare_in_namespace (decl_or_origin, context_die);
21508 /* Now output the DIE to represent the data object itself. This gets
21509 complicated because of the possibility that the VAR_DECL really
21510 represents an inlined instance of a formal parameter for an inline
21511 function. */
21512 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21513 if (ultimate_origin != NULL_TREE
21514 && TREE_CODE (ultimate_origin) == PARM_DECL)
21515 gen_formal_parameter_die (decl, origin,
21516 true /* Emit name attribute. */,
21517 context_die);
21518 else
21519 gen_variable_die (decl, origin, context_die);
21520 break;
21522 case FIELD_DECL:
21523 /* Ignore the nameless fields that are used to skip bits but handle C++
21524 anonymous unions and structs. */
21525 if (DECL_NAME (decl) != NULL_TREE
21526 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21527 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21529 gen_type_die (member_declared_type (decl), context_die);
21530 gen_field_die (decl, context_die);
21532 break;
21534 case PARM_DECL:
21535 if (DECL_BY_REFERENCE (decl_or_origin))
21536 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21537 else
21538 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21539 return gen_formal_parameter_die (decl, origin,
21540 true /* Emit name attribute. */,
21541 context_die);
21543 case NAMESPACE_DECL:
21544 case IMPORTED_DECL:
21545 if (dwarf_version >= 3 || !dwarf_strict)
21546 gen_namespace_die (decl, context_die);
21547 break;
21549 case NAMELIST_DECL:
21550 gen_namelist_decl (DECL_NAME (decl), context_die,
21551 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21552 break;
21554 default:
21555 /* Probably some frontend-internal decl. Assume we don't care. */
21556 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21557 break;
21560 return NULL;
21563 /* Output initial debug information for global DECL. Called at the
21564 end of the parsing process.
21566 This is the initial debug generation process. As such, the DIEs
21567 generated may be incomplete. A later debug generation pass
21568 (dwarf2out_late_global_decl) will augment the information generated
21569 in this pass (e.g., with complete location info). */
21571 static void
21572 dwarf2out_early_global_decl (tree decl)
21574 set_early_dwarf s;
21576 /* gen_decl_die() will set DECL_ABSTRACT because
21577 cgraph_function_possibly_inlined_p() returns true. This is in
21578 turn will cause DW_AT_inline attributes to be set.
21580 This happens because at early dwarf generation, there is no
21581 cgraph information, causing cgraph_function_possibly_inlined_p()
21582 to return true. Trick cgraph_function_possibly_inlined_p()
21583 while we generate dwarf early. */
21584 bool save = symtab->global_info_ready;
21585 symtab->global_info_ready = true;
21587 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21588 other DECLs and they can point to template types or other things
21589 that dwarf2out can't handle when done via dwarf2out_decl. */
21590 if (TREE_CODE (decl) != TYPE_DECL
21591 && TREE_CODE (decl) != PARM_DECL)
21593 tree save_fndecl = current_function_decl;
21594 if (TREE_CODE (decl) == FUNCTION_DECL)
21596 /* No cfun means the symbol has no body, so there's nothing
21597 to emit. */
21598 if (!DECL_STRUCT_FUNCTION (decl))
21599 goto early_decl_exit;
21601 current_function_decl = decl;
21603 dwarf2out_decl (decl);
21604 if (TREE_CODE (decl) == FUNCTION_DECL)
21605 current_function_decl = save_fndecl;
21607 early_decl_exit:
21608 symtab->global_info_ready = save;
21611 /* Output debug information for global decl DECL. Called from
21612 toplev.c after compilation proper has finished. */
21614 static void
21615 dwarf2out_late_global_decl (tree decl)
21617 /* Output any global decls we missed or fill-in any location
21618 information we were unable to determine on the first pass.
21620 Skip over functions because they were handled by the
21621 debug_hooks->function_decl() call in rest_of_handle_final. */
21622 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21623 && !POINTER_BOUNDS_P (decl))
21624 dwarf2out_decl (decl);
21627 /* Output debug information for type decl DECL. Called from toplev.c
21628 and from language front ends (to record built-in types). */
21629 static void
21630 dwarf2out_type_decl (tree decl, int local)
21632 if (!local)
21634 set_early_dwarf s;
21635 dwarf2out_decl (decl);
21639 /* Output debug information for imported module or decl DECL.
21640 NAME is non-NULL name in the lexical block if the decl has been renamed.
21641 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21642 that DECL belongs to.
21643 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21644 static void
21645 dwarf2out_imported_module_or_decl_1 (tree decl,
21646 tree name,
21647 tree lexical_block,
21648 dw_die_ref lexical_block_die)
21650 expanded_location xloc;
21651 dw_die_ref imported_die = NULL;
21652 dw_die_ref at_import_die;
21654 if (TREE_CODE (decl) == IMPORTED_DECL)
21656 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21657 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21658 gcc_assert (decl);
21660 else
21661 xloc = expand_location (input_location);
21663 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21665 at_import_die = force_type_die (TREE_TYPE (decl));
21666 /* For namespace N { typedef void T; } using N::T; base_type_die
21667 returns NULL, but DW_TAG_imported_declaration requires
21668 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21669 if (!at_import_die)
21671 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21672 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21673 at_import_die = lookup_type_die (TREE_TYPE (decl));
21674 gcc_assert (at_import_die);
21677 else
21679 at_import_die = lookup_decl_die (decl);
21680 if (!at_import_die)
21682 /* If we're trying to avoid duplicate debug info, we may not have
21683 emitted the member decl for this field. Emit it now. */
21684 if (TREE_CODE (decl) == FIELD_DECL)
21686 tree type = DECL_CONTEXT (decl);
21688 if (TYPE_CONTEXT (type)
21689 && TYPE_P (TYPE_CONTEXT (type))
21690 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21691 DINFO_USAGE_DIR_USE))
21692 return;
21693 gen_type_die_for_member (type, decl,
21694 get_context_die (TYPE_CONTEXT (type)));
21696 if (TREE_CODE (decl) == NAMELIST_DECL)
21697 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21698 get_context_die (DECL_CONTEXT (decl)),
21699 NULL_TREE);
21700 else
21701 at_import_die = force_decl_die (decl);
21705 if (TREE_CODE (decl) == NAMESPACE_DECL)
21707 if (dwarf_version >= 3 || !dwarf_strict)
21708 imported_die = new_die (DW_TAG_imported_module,
21709 lexical_block_die,
21710 lexical_block);
21711 else
21712 return;
21714 else
21715 imported_die = new_die (DW_TAG_imported_declaration,
21716 lexical_block_die,
21717 lexical_block);
21719 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21720 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21721 if (name)
21722 add_AT_string (imported_die, DW_AT_name,
21723 IDENTIFIER_POINTER (name));
21724 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21727 /* Output debug information for imported module or decl DECL.
21728 NAME is non-NULL name in context if the decl has been renamed.
21729 CHILD is true if decl is one of the renamed decls as part of
21730 importing whole module. */
21732 static void
21733 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21734 bool child)
21736 /* dw_die_ref at_import_die; */
21737 dw_die_ref scope_die;
21739 if (debug_info_level <= DINFO_LEVEL_TERSE)
21740 return;
21742 gcc_assert (decl);
21744 set_early_dwarf s;
21746 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21747 We need decl DIE for reference and scope die. First, get DIE for the decl
21748 itself. */
21750 /* Get the scope die for decl context. Use comp_unit_die for global module
21751 or decl. If die is not found for non globals, force new die. */
21752 if (context
21753 && TYPE_P (context)
21754 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21755 return;
21757 if (!(dwarf_version >= 3 || !dwarf_strict))
21758 return;
21760 scope_die = get_context_die (context);
21762 if (child)
21764 gcc_assert (scope_die->die_child);
21765 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21766 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21767 scope_die = scope_die->die_child;
21770 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21771 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21774 /* Output debug information for namelists. */
21776 static dw_die_ref
21777 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21779 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21780 tree value;
21781 unsigned i;
21783 if (debug_info_level <= DINFO_LEVEL_TERSE)
21784 return NULL;
21786 gcc_assert (scope_die != NULL);
21787 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21788 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21790 /* If there are no item_decls, we have a nondefining namelist, e.g.
21791 with USE association; hence, set DW_AT_declaration. */
21792 if (item_decls == NULL_TREE)
21794 add_AT_flag (nml_die, DW_AT_declaration, 1);
21795 return nml_die;
21798 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21800 nml_item_ref_die = lookup_decl_die (value);
21801 if (!nml_item_ref_die)
21802 nml_item_ref_die = force_decl_die (value);
21804 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21805 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21807 return nml_die;
21811 /* Write the debugging output for DECL and return the DIE. */
21813 static void
21814 dwarf2out_decl (tree decl)
21816 dw_die_ref context_die = comp_unit_die ();
21818 switch (TREE_CODE (decl))
21820 case ERROR_MARK:
21821 return;
21823 case FUNCTION_DECL:
21824 /* What we would really like to do here is to filter out all mere
21825 file-scope declarations of file-scope functions which are never
21826 referenced later within this translation unit (and keep all of ones
21827 that *are* referenced later on) but we aren't clairvoyant, so we have
21828 no idea which functions will be referenced in the future (i.e. later
21829 on within the current translation unit). So here we just ignore all
21830 file-scope function declarations which are not also definitions. If
21831 and when the debugger needs to know something about these functions,
21832 it will have to hunt around and find the DWARF information associated
21833 with the definition of the function.
21835 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21836 nodes represent definitions and which ones represent mere
21837 declarations. We have to check DECL_INITIAL instead. That's because
21838 the C front-end supports some weird semantics for "extern inline"
21839 function definitions. These can get inlined within the current
21840 translation unit (and thus, we need to generate Dwarf info for their
21841 abstract instances so that the Dwarf info for the concrete inlined
21842 instances can have something to refer to) but the compiler never
21843 generates any out-of-lines instances of such things (despite the fact
21844 that they *are* definitions).
21846 The important point is that the C front-end marks these "extern
21847 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21848 them anyway. Note that the C++ front-end also plays some similar games
21849 for inline function definitions appearing within include files which
21850 also contain `#pragma interface' pragmas.
21852 If we are called from dwarf2out_abstract_function output a DIE
21853 anyway. We can end up here this way with early inlining and LTO
21854 where the inlined function is output in a different LTRANS unit
21855 or not at all. */
21856 if (DECL_INITIAL (decl) == NULL_TREE
21857 && ! DECL_ABSTRACT_P (decl))
21858 return;
21860 /* If we're a nested function, initially use a parent of NULL; if we're
21861 a plain function, this will be fixed up in decls_for_scope. If
21862 we're a method, it will be ignored, since we already have a DIE. */
21863 if (decl_function_context (decl)
21864 /* But if we're in terse mode, we don't care about scope. */
21865 && debug_info_level > DINFO_LEVEL_TERSE)
21866 context_die = NULL;
21867 break;
21869 case VAR_DECL:
21870 /* For local statics lookup proper context die. */
21871 if (local_function_static (decl))
21872 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21874 /* If we are in terse mode, don't generate any DIEs to represent any
21875 variable declarations or definitions. */
21876 if (debug_info_level <= DINFO_LEVEL_TERSE)
21877 return;
21878 break;
21880 case CONST_DECL:
21881 if (debug_info_level <= DINFO_LEVEL_TERSE)
21882 return;
21883 if (!is_fortran () && !is_ada ())
21884 return;
21885 if (TREE_STATIC (decl) && decl_function_context (decl))
21886 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21887 break;
21889 case NAMESPACE_DECL:
21890 case IMPORTED_DECL:
21891 if (debug_info_level <= DINFO_LEVEL_TERSE)
21892 return;
21893 if (lookup_decl_die (decl) != NULL)
21894 return;
21895 break;
21897 case TYPE_DECL:
21898 /* Don't emit stubs for types unless they are needed by other DIEs. */
21899 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21900 return;
21902 /* Don't bother trying to generate any DIEs to represent any of the
21903 normal built-in types for the language we are compiling. */
21904 if (DECL_IS_BUILTIN (decl))
21905 return;
21907 /* If we are in terse mode, don't generate any DIEs for types. */
21908 if (debug_info_level <= DINFO_LEVEL_TERSE)
21909 return;
21911 /* If we're a function-scope tag, initially use a parent of NULL;
21912 this will be fixed up in decls_for_scope. */
21913 if (decl_function_context (decl))
21914 context_die = NULL;
21916 break;
21918 case NAMELIST_DECL:
21919 break;
21921 default:
21922 return;
21925 gen_decl_die (decl, NULL, context_die);
21927 #ifdef ENABLE_CHECKING
21928 dw_die_ref die = lookup_decl_die (decl);
21929 if (die)
21930 check_die (die);
21931 #endif
21934 /* Write the debugging output for DECL. */
21936 static void
21937 dwarf2out_function_decl (tree decl)
21939 dwarf2out_decl (decl);
21940 call_arg_locations = NULL;
21941 call_arg_loc_last = NULL;
21942 call_site_count = -1;
21943 tail_call_site_count = -1;
21944 decl_loc_table->empty ();
21945 cached_dw_loc_list_table->empty ();
21948 /* Output a marker (i.e. a label) for the beginning of the generated code for
21949 a lexical block. */
21951 static void
21952 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21953 unsigned int blocknum)
21955 switch_to_section (current_function_section ());
21956 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21959 /* Output a marker (i.e. a label) for the end of the generated code for a
21960 lexical block. */
21962 static void
21963 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21965 switch_to_section (current_function_section ());
21966 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21969 /* Returns nonzero if it is appropriate not to emit any debugging
21970 information for BLOCK, because it doesn't contain any instructions.
21972 Don't allow this for blocks with nested functions or local classes
21973 as we would end up with orphans, and in the presence of scheduling
21974 we may end up calling them anyway. */
21976 static bool
21977 dwarf2out_ignore_block (const_tree block)
21979 tree decl;
21980 unsigned int i;
21982 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21983 if (TREE_CODE (decl) == FUNCTION_DECL
21984 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21985 return 0;
21986 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21988 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21989 if (TREE_CODE (decl) == FUNCTION_DECL
21990 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21991 return 0;
21994 return 1;
21997 /* Hash table routines for file_hash. */
21999 bool
22000 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22002 return filename_cmp (p1->filename, p2) == 0;
22005 hashval_t
22006 dwarf_file_hasher::hash (dwarf_file_data *p)
22008 return htab_hash_string (p->filename);
22011 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22012 dwarf2out.c) and return its "index". The index of each (known) filename is
22013 just a unique number which is associated with only that one filename. We
22014 need such numbers for the sake of generating labels (in the .debug_sfnames
22015 section) and references to those files numbers (in the .debug_srcinfo
22016 and.debug_macinfo sections). If the filename given as an argument is not
22017 found in our current list, add it to the list and assign it the next
22018 available unique index number. */
22020 static struct dwarf_file_data *
22021 lookup_filename (const char *file_name)
22023 struct dwarf_file_data * created;
22025 if (!file_name)
22026 return NULL;
22028 dwarf_file_data **slot
22029 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22030 INSERT);
22031 if (*slot)
22032 return *slot;
22034 created = ggc_alloc<dwarf_file_data> ();
22035 created->filename = file_name;
22036 created->emitted_number = 0;
22037 *slot = created;
22038 return created;
22041 /* If the assembler will construct the file table, then translate the compiler
22042 internal file table number into the assembler file table number, and emit
22043 a .file directive if we haven't already emitted one yet. The file table
22044 numbers are different because we prune debug info for unused variables and
22045 types, which may include filenames. */
22047 static int
22048 maybe_emit_file (struct dwarf_file_data * fd)
22050 if (! fd->emitted_number)
22052 if (last_emitted_file)
22053 fd->emitted_number = last_emitted_file->emitted_number + 1;
22054 else
22055 fd->emitted_number = 1;
22056 last_emitted_file = fd;
22058 if (DWARF2_ASM_LINE_DEBUG_INFO)
22060 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22061 output_quoted_string (asm_out_file,
22062 remap_debug_filename (fd->filename));
22063 fputc ('\n', asm_out_file);
22067 return fd->emitted_number;
22070 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22071 That generation should happen after function debug info has been
22072 generated. The value of the attribute is the constant value of ARG. */
22074 static void
22075 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22077 die_arg_entry entry;
22079 if (!die || !arg)
22080 return;
22082 if (!tmpl_value_parm_die_table)
22083 vec_alloc (tmpl_value_parm_die_table, 32);
22085 entry.die = die;
22086 entry.arg = arg;
22087 vec_safe_push (tmpl_value_parm_die_table, entry);
22090 /* Return TRUE if T is an instance of generic type, FALSE
22091 otherwise. */
22093 static bool
22094 generic_type_p (tree t)
22096 if (t == NULL_TREE || !TYPE_P (t))
22097 return false;
22098 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22101 /* Schedule the generation of the generic parameter dies for the
22102 instance of generic type T. The proper generation itself is later
22103 done by gen_scheduled_generic_parms_dies. */
22105 static void
22106 schedule_generic_params_dies_gen (tree t)
22108 if (!generic_type_p (t))
22109 return;
22111 if (!generic_type_instances)
22112 vec_alloc (generic_type_instances, 256);
22114 vec_safe_push (generic_type_instances, t);
22117 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22118 by append_entry_to_tmpl_value_parm_die_table. This function must
22119 be called after function DIEs have been generated. */
22121 static void
22122 gen_remaining_tmpl_value_param_die_attribute (void)
22124 if (tmpl_value_parm_die_table)
22126 unsigned i;
22127 die_arg_entry *e;
22129 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22130 tree_add_const_value_attribute (e->die, e->arg);
22134 /* Generate generic parameters DIEs for instances of generic types
22135 that have been previously scheduled by
22136 schedule_generic_params_dies_gen. This function must be called
22137 after all the types of the CU have been laid out. */
22139 static void
22140 gen_scheduled_generic_parms_dies (void)
22142 unsigned i;
22143 tree t;
22145 if (!generic_type_instances)
22146 return;
22148 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22149 if (COMPLETE_TYPE_P (t))
22150 gen_generic_params_dies (t);
22154 /* Replace DW_AT_name for the decl with name. */
22156 static void
22157 dwarf2out_set_name (tree decl, tree name)
22159 dw_die_ref die;
22160 dw_attr_ref attr;
22161 const char *dname;
22163 die = TYPE_SYMTAB_DIE (decl);
22164 if (!die)
22165 return;
22167 dname = dwarf2_name (name, 0);
22168 if (!dname)
22169 return;
22171 attr = get_AT (die, DW_AT_name);
22172 if (attr)
22174 struct indirect_string_node *node;
22176 node = find_AT_string (dname);
22177 /* replace the string. */
22178 attr->dw_attr_val.v.val_str = node;
22181 else
22182 add_name_attribute (die, dname);
22185 /* True if before or during processing of the first function being emitted. */
22186 static bool in_first_function_p = true;
22187 /* True if loc_note during dwarf2out_var_location call might still be
22188 before first real instruction at address equal to .Ltext0. */
22189 static bool maybe_at_text_label_p = true;
22190 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22191 static unsigned int first_loclabel_num_not_at_text_label;
22193 /* Called by the final INSN scan whenever we see a var location. We
22194 use it to drop labels in the right places, and throw the location in
22195 our lookup table. */
22197 static void
22198 dwarf2out_var_location (rtx_insn *loc_note)
22200 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22201 struct var_loc_node *newloc;
22202 rtx_insn *next_real, *next_note;
22203 static const char *last_label;
22204 static const char *last_postcall_label;
22205 static bool last_in_cold_section_p;
22206 static rtx_insn *expected_next_loc_note;
22207 tree decl;
22208 bool var_loc_p;
22210 if (!NOTE_P (loc_note))
22212 if (CALL_P (loc_note))
22214 call_site_count++;
22215 if (SIBLING_CALL_P (loc_note))
22216 tail_call_site_count++;
22218 return;
22221 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22222 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22223 return;
22225 /* Optimize processing a large consecutive sequence of location
22226 notes so we don't spend too much time in next_real_insn. If the
22227 next insn is another location note, remember the next_real_insn
22228 calculation for next time. */
22229 next_real = cached_next_real_insn;
22230 if (next_real)
22232 if (expected_next_loc_note != loc_note)
22233 next_real = NULL;
22236 next_note = NEXT_INSN (loc_note);
22237 if (! next_note
22238 || next_note->deleted ()
22239 || ! NOTE_P (next_note)
22240 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22241 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22242 next_note = NULL;
22244 if (! next_real)
22245 next_real = next_real_insn (loc_note);
22247 if (next_note)
22249 expected_next_loc_note = next_note;
22250 cached_next_real_insn = next_real;
22252 else
22253 cached_next_real_insn = NULL;
22255 /* If there are no instructions which would be affected by this note,
22256 don't do anything. */
22257 if (var_loc_p
22258 && next_real == NULL_RTX
22259 && !NOTE_DURING_CALL_P (loc_note))
22260 return;
22262 if (next_real == NULL_RTX)
22263 next_real = get_last_insn ();
22265 /* If there were any real insns between note we processed last time
22266 and this note (or if it is the first note), clear
22267 last_{,postcall_}label so that they are not reused this time. */
22268 if (last_var_location_insn == NULL_RTX
22269 || last_var_location_insn != next_real
22270 || last_in_cold_section_p != in_cold_section_p)
22272 last_label = NULL;
22273 last_postcall_label = NULL;
22276 if (var_loc_p)
22278 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22279 newloc = add_var_loc_to_decl (decl, loc_note,
22280 NOTE_DURING_CALL_P (loc_note)
22281 ? last_postcall_label : last_label);
22282 if (newloc == NULL)
22283 return;
22285 else
22287 decl = NULL_TREE;
22288 newloc = NULL;
22291 /* If there were no real insns between note we processed last time
22292 and this note, use the label we emitted last time. Otherwise
22293 create a new label and emit it. */
22294 if (last_label == NULL)
22296 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22297 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22298 loclabel_num++;
22299 last_label = ggc_strdup (loclabel);
22300 /* See if loclabel might be equal to .Ltext0. If yes,
22301 bump first_loclabel_num_not_at_text_label. */
22302 if (!have_multiple_function_sections
22303 && in_first_function_p
22304 && maybe_at_text_label_p)
22306 static rtx_insn *last_start;
22307 rtx_insn *insn;
22308 for (insn = loc_note; insn; insn = previous_insn (insn))
22309 if (insn == last_start)
22310 break;
22311 else if (!NONDEBUG_INSN_P (insn))
22312 continue;
22313 else
22315 rtx body = PATTERN (insn);
22316 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22317 continue;
22318 /* Inline asm could occupy zero bytes. */
22319 else if (GET_CODE (body) == ASM_INPUT
22320 || asm_noperands (body) >= 0)
22321 continue;
22322 #ifdef HAVE_attr_length
22323 else if (get_attr_min_length (insn) == 0)
22324 continue;
22325 #endif
22326 else
22328 /* Assume insn has non-zero length. */
22329 maybe_at_text_label_p = false;
22330 break;
22333 if (maybe_at_text_label_p)
22335 last_start = loc_note;
22336 first_loclabel_num_not_at_text_label = loclabel_num;
22341 if (!var_loc_p)
22343 struct call_arg_loc_node *ca_loc
22344 = ggc_cleared_alloc<call_arg_loc_node> ();
22345 rtx_insn *prev = prev_real_insn (loc_note);
22346 rtx x;
22347 ca_loc->call_arg_loc_note = loc_note;
22348 ca_loc->next = NULL;
22349 ca_loc->label = last_label;
22350 gcc_assert (prev
22351 && (CALL_P (prev)
22352 || (NONJUMP_INSN_P (prev)
22353 && GET_CODE (PATTERN (prev)) == SEQUENCE
22354 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22355 if (!CALL_P (prev))
22356 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22357 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22358 x = get_call_rtx_from (PATTERN (prev));
22359 if (x)
22361 x = XEXP (XEXP (x, 0), 0);
22362 if (GET_CODE (x) == SYMBOL_REF
22363 && SYMBOL_REF_DECL (x)
22364 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22365 ca_loc->symbol_ref = x;
22367 ca_loc->block = insn_scope (prev);
22368 if (call_arg_locations)
22369 call_arg_loc_last->next = ca_loc;
22370 else
22371 call_arg_locations = ca_loc;
22372 call_arg_loc_last = ca_loc;
22374 else if (!NOTE_DURING_CALL_P (loc_note))
22375 newloc->label = last_label;
22376 else
22378 if (!last_postcall_label)
22380 sprintf (loclabel, "%s-1", last_label);
22381 last_postcall_label = ggc_strdup (loclabel);
22383 newloc->label = last_postcall_label;
22386 last_var_location_insn = next_real;
22387 last_in_cold_section_p = in_cold_section_p;
22390 /* Note in one location list that text section has changed. */
22393 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22395 var_loc_list *list = *slot;
22396 if (list->first)
22397 list->last_before_switch
22398 = list->last->next ? list->last->next : list->last;
22399 return 1;
22402 /* Note in all location lists that text section has changed. */
22404 static void
22405 var_location_switch_text_section (void)
22407 if (decl_loc_table == NULL)
22408 return;
22410 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22413 /* Create a new line number table. */
22415 static dw_line_info_table *
22416 new_line_info_table (void)
22418 dw_line_info_table *table;
22420 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22421 table->file_num = 1;
22422 table->line_num = 1;
22423 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22425 return table;
22428 /* Lookup the "current" table into which we emit line info, so
22429 that we don't have to do it for every source line. */
22431 static void
22432 set_cur_line_info_table (section *sec)
22434 dw_line_info_table *table;
22436 if (sec == text_section)
22437 table = text_section_line_info;
22438 else if (sec == cold_text_section)
22440 table = cold_text_section_line_info;
22441 if (!table)
22443 cold_text_section_line_info = table = new_line_info_table ();
22444 table->end_label = cold_end_label;
22447 else
22449 const char *end_label;
22451 if (flag_reorder_blocks_and_partition)
22453 if (in_cold_section_p)
22454 end_label = crtl->subsections.cold_section_end_label;
22455 else
22456 end_label = crtl->subsections.hot_section_end_label;
22458 else
22460 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22461 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22462 current_function_funcdef_no);
22463 end_label = ggc_strdup (label);
22466 table = new_line_info_table ();
22467 table->end_label = end_label;
22469 vec_safe_push (separate_line_info, table);
22472 if (DWARF2_ASM_LINE_DEBUG_INFO)
22473 table->is_stmt = (cur_line_info_table
22474 ? cur_line_info_table->is_stmt
22475 : DWARF_LINE_DEFAULT_IS_STMT_START);
22476 cur_line_info_table = table;
22480 /* We need to reset the locations at the beginning of each
22481 function. We can't do this in the end_function hook, because the
22482 declarations that use the locations won't have been output when
22483 that hook is called. Also compute have_multiple_function_sections here. */
22485 static void
22486 dwarf2out_begin_function (tree fun)
22488 section *sec = function_section (fun);
22490 if (sec != text_section)
22491 have_multiple_function_sections = true;
22493 if (flag_reorder_blocks_and_partition && !cold_text_section)
22495 gcc_assert (current_function_decl == fun);
22496 cold_text_section = unlikely_text_section ();
22497 switch_to_section (cold_text_section);
22498 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22499 switch_to_section (sec);
22502 dwarf2out_note_section_used ();
22503 call_site_count = 0;
22504 tail_call_site_count = 0;
22506 set_cur_line_info_table (sec);
22509 /* Helper function of dwarf2out_end_function, called only after emitting
22510 the very first function into assembly. Check if some .debug_loc range
22511 might end with a .LVL* label that could be equal to .Ltext0.
22512 In that case we must force using absolute addresses in .debug_loc ranges,
22513 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22514 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22515 list terminator.
22516 Set have_multiple_function_sections to true in that case and
22517 terminate htab traversal. */
22520 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22522 var_loc_list *entry = *slot;
22523 struct var_loc_node *node;
22525 node = entry->first;
22526 if (node && node->next && node->next->label)
22528 unsigned int i;
22529 const char *label = node->next->label;
22530 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22532 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22534 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22535 if (strcmp (label, loclabel) == 0)
22537 have_multiple_function_sections = true;
22538 return 0;
22542 return 1;
22545 /* Hook called after emitting a function into assembly.
22546 This does something only for the very first function emitted. */
22548 static void
22549 dwarf2out_end_function (unsigned int)
22551 if (in_first_function_p
22552 && !have_multiple_function_sections
22553 && first_loclabel_num_not_at_text_label
22554 && decl_loc_table)
22555 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22556 in_first_function_p = false;
22557 maybe_at_text_label_p = false;
22560 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22561 front-ends register a translation unit even before dwarf2out_init is
22562 called. */
22563 static tree main_translation_unit = NULL_TREE;
22565 /* Hook called by front-ends after they built their main translation unit.
22566 Associate comp_unit_die to UNIT. */
22568 static void
22569 dwarf2out_register_main_translation_unit (tree unit)
22571 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22572 && main_translation_unit == NULL_TREE);
22573 main_translation_unit = unit;
22574 /* If dwarf2out_init has not been called yet, it will perform the association
22575 itself looking at main_translation_unit. */
22576 if (decl_die_table != NULL)
22577 equate_decl_number_to_die (unit, comp_unit_die ());
22580 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22582 static void
22583 push_dw_line_info_entry (dw_line_info_table *table,
22584 enum dw_line_info_opcode opcode, unsigned int val)
22586 dw_line_info_entry e;
22587 e.opcode = opcode;
22588 e.val = val;
22589 vec_safe_push (table->entries, e);
22592 /* Output a label to mark the beginning of a source code line entry
22593 and record information relating to this source line, in
22594 'line_info_table' for later output of the .debug_line section. */
22595 /* ??? The discriminator parameter ought to be unsigned. */
22597 static void
22598 dwarf2out_source_line (unsigned int line, const char *filename,
22599 int discriminator, bool is_stmt)
22601 unsigned int file_num;
22602 dw_line_info_table *table;
22604 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22605 return;
22607 /* The discriminator column was added in dwarf4. Simplify the below
22608 by simply removing it if we're not supposed to output it. */
22609 if (dwarf_version < 4 && dwarf_strict)
22610 discriminator = 0;
22612 table = cur_line_info_table;
22613 file_num = maybe_emit_file (lookup_filename (filename));
22615 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22616 the debugger has used the second (possibly duplicate) line number
22617 at the beginning of the function to mark the end of the prologue.
22618 We could eliminate any other duplicates within the function. For
22619 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22620 that second line number entry. */
22621 /* Recall that this end-of-prologue indication is *not* the same thing
22622 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22623 to which the hook corresponds, follows the last insn that was
22624 emitted by gen_prologue. What we need is to precede the first insn
22625 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22626 insn that corresponds to something the user wrote. These may be
22627 very different locations once scheduling is enabled. */
22629 if (0 && file_num == table->file_num
22630 && line == table->line_num
22631 && discriminator == table->discrim_num
22632 && is_stmt == table->is_stmt)
22633 return;
22635 switch_to_section (current_function_section ());
22637 /* If requested, emit something human-readable. */
22638 if (flag_debug_asm)
22639 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22641 if (DWARF2_ASM_LINE_DEBUG_INFO)
22643 /* Emit the .loc directive understood by GNU as. */
22644 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22645 file_num, line, is_stmt, discriminator */
22646 fputs ("\t.loc ", asm_out_file);
22647 fprint_ul (asm_out_file, file_num);
22648 putc (' ', asm_out_file);
22649 fprint_ul (asm_out_file, line);
22650 putc (' ', asm_out_file);
22651 putc ('0', asm_out_file);
22653 if (is_stmt != table->is_stmt)
22655 fputs (" is_stmt ", asm_out_file);
22656 putc (is_stmt ? '1' : '0', asm_out_file);
22658 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22660 gcc_assert (discriminator > 0);
22661 fputs (" discriminator ", asm_out_file);
22662 fprint_ul (asm_out_file, (unsigned long) discriminator);
22664 putc ('\n', asm_out_file);
22666 else
22668 unsigned int label_num = ++line_info_label_num;
22670 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22672 push_dw_line_info_entry (table, LI_set_address, label_num);
22673 if (file_num != table->file_num)
22674 push_dw_line_info_entry (table, LI_set_file, file_num);
22675 if (discriminator != table->discrim_num)
22676 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22677 if (is_stmt != table->is_stmt)
22678 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22679 push_dw_line_info_entry (table, LI_set_line, line);
22682 table->file_num = file_num;
22683 table->line_num = line;
22684 table->discrim_num = discriminator;
22685 table->is_stmt = is_stmt;
22686 table->in_use = true;
22689 /* Record the beginning of a new source file. */
22691 static void
22692 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22694 if (flag_eliminate_dwarf2_dups)
22696 /* Record the beginning of the file for break_out_includes. */
22697 dw_die_ref bincl_die;
22699 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22700 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22703 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22705 macinfo_entry e;
22706 e.code = DW_MACINFO_start_file;
22707 e.lineno = lineno;
22708 e.info = ggc_strdup (filename);
22709 vec_safe_push (macinfo_table, e);
22713 /* Record the end of a source file. */
22715 static void
22716 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22718 if (flag_eliminate_dwarf2_dups)
22719 /* Record the end of the file for break_out_includes. */
22720 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22722 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22724 macinfo_entry e;
22725 e.code = DW_MACINFO_end_file;
22726 e.lineno = lineno;
22727 e.info = NULL;
22728 vec_safe_push (macinfo_table, e);
22732 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22733 the tail part of the directive line, i.e. the part which is past the
22734 initial whitespace, #, whitespace, directive-name, whitespace part. */
22736 static void
22737 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22738 const char *buffer ATTRIBUTE_UNUSED)
22740 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22742 macinfo_entry e;
22743 /* Insert a dummy first entry to be able to optimize the whole
22744 predefined macro block using DW_MACRO_GNU_transparent_include. */
22745 if (macinfo_table->is_empty () && lineno <= 1)
22747 e.code = 0;
22748 e.lineno = 0;
22749 e.info = NULL;
22750 vec_safe_push (macinfo_table, e);
22752 e.code = DW_MACINFO_define;
22753 e.lineno = lineno;
22754 e.info = ggc_strdup (buffer);
22755 vec_safe_push (macinfo_table, e);
22759 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22760 the tail part of the directive line, i.e. the part which is past the
22761 initial whitespace, #, whitespace, directive-name, whitespace part. */
22763 static void
22764 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22765 const char *buffer ATTRIBUTE_UNUSED)
22767 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22769 macinfo_entry e;
22770 /* Insert a dummy first entry to be able to optimize the whole
22771 predefined macro block using DW_MACRO_GNU_transparent_include. */
22772 if (macinfo_table->is_empty () && lineno <= 1)
22774 e.code = 0;
22775 e.lineno = 0;
22776 e.info = NULL;
22777 vec_safe_push (macinfo_table, e);
22779 e.code = DW_MACINFO_undef;
22780 e.lineno = lineno;
22781 e.info = ggc_strdup (buffer);
22782 vec_safe_push (macinfo_table, e);
22786 /* Helpers to manipulate hash table of CUs. */
22788 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22790 typedef macinfo_entry *value_type;
22791 typedef macinfo_entry *compare_type;
22792 static inline hashval_t hash (const macinfo_entry *);
22793 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22796 inline hashval_t
22797 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22799 return htab_hash_string (entry->info);
22802 inline bool
22803 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22804 const macinfo_entry *entry2)
22806 return !strcmp (entry1->info, entry2->info);
22809 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22811 /* Output a single .debug_macinfo entry. */
22813 static void
22814 output_macinfo_op (macinfo_entry *ref)
22816 int file_num;
22817 size_t len;
22818 struct indirect_string_node *node;
22819 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22820 struct dwarf_file_data *fd;
22822 switch (ref->code)
22824 case DW_MACINFO_start_file:
22825 fd = lookup_filename (ref->info);
22826 file_num = maybe_emit_file (fd);
22827 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22828 dw2_asm_output_data_uleb128 (ref->lineno,
22829 "Included from line number %lu",
22830 (unsigned long) ref->lineno);
22831 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22832 break;
22833 case DW_MACINFO_end_file:
22834 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22835 break;
22836 case DW_MACINFO_define:
22837 case DW_MACINFO_undef:
22838 len = strlen (ref->info) + 1;
22839 if (!dwarf_strict
22840 && len > DWARF_OFFSET_SIZE
22841 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22842 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22844 ref->code = ref->code == DW_MACINFO_define
22845 ? DW_MACRO_GNU_define_indirect
22846 : DW_MACRO_GNU_undef_indirect;
22847 output_macinfo_op (ref);
22848 return;
22850 dw2_asm_output_data (1, ref->code,
22851 ref->code == DW_MACINFO_define
22852 ? "Define macro" : "Undefine macro");
22853 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22854 (unsigned long) ref->lineno);
22855 dw2_asm_output_nstring (ref->info, -1, "The macro");
22856 break;
22857 case DW_MACRO_GNU_define_indirect:
22858 case DW_MACRO_GNU_undef_indirect:
22859 node = find_AT_string (ref->info);
22860 gcc_assert (node
22861 && ((node->form == DW_FORM_strp)
22862 || (node->form == DW_FORM_GNU_str_index)));
22863 dw2_asm_output_data (1, ref->code,
22864 ref->code == DW_MACRO_GNU_define_indirect
22865 ? "Define macro indirect"
22866 : "Undefine macro indirect");
22867 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22868 (unsigned long) ref->lineno);
22869 if (node->form == DW_FORM_strp)
22870 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22871 debug_str_section, "The macro: \"%s\"",
22872 ref->info);
22873 else
22874 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22875 ref->info);
22876 break;
22877 case DW_MACRO_GNU_transparent_include:
22878 dw2_asm_output_data (1, ref->code, "Transparent include");
22879 ASM_GENERATE_INTERNAL_LABEL (label,
22880 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22881 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22882 break;
22883 default:
22884 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22885 ASM_COMMENT_START, (unsigned long) ref->code);
22886 break;
22890 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22891 other compilation unit .debug_macinfo sections. IDX is the first
22892 index of a define/undef, return the number of ops that should be
22893 emitted in a comdat .debug_macinfo section and emit
22894 a DW_MACRO_GNU_transparent_include entry referencing it.
22895 If the define/undef entry should be emitted normally, return 0. */
22897 static unsigned
22898 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22899 macinfo_hash_type **macinfo_htab)
22901 macinfo_entry *first, *second, *cur, *inc;
22902 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22903 unsigned char checksum[16];
22904 struct md5_ctx ctx;
22905 char *grp_name, *tail;
22906 const char *base;
22907 unsigned int i, count, encoded_filename_len, linebuf_len;
22908 macinfo_entry **slot;
22910 first = &(*macinfo_table)[idx];
22911 second = &(*macinfo_table)[idx + 1];
22913 /* Optimize only if there are at least two consecutive define/undef ops,
22914 and either all of them are before first DW_MACINFO_start_file
22915 with lineno {0,1} (i.e. predefined macro block), or all of them are
22916 in some included header file. */
22917 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22918 return 0;
22919 if (vec_safe_is_empty (files))
22921 if (first->lineno > 1 || second->lineno > 1)
22922 return 0;
22924 else if (first->lineno == 0)
22925 return 0;
22927 /* Find the last define/undef entry that can be grouped together
22928 with first and at the same time compute md5 checksum of their
22929 codes, linenumbers and strings. */
22930 md5_init_ctx (&ctx);
22931 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22932 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22933 break;
22934 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22935 break;
22936 else
22938 unsigned char code = cur->code;
22939 md5_process_bytes (&code, 1, &ctx);
22940 checksum_uleb128 (cur->lineno, &ctx);
22941 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22943 md5_finish_ctx (&ctx, checksum);
22944 count = i - idx;
22946 /* From the containing include filename (if any) pick up just
22947 usable characters from its basename. */
22948 if (vec_safe_is_empty (files))
22949 base = "";
22950 else
22951 base = lbasename (files->last ().info);
22952 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22953 if (ISIDNUM (base[i]) || base[i] == '.')
22954 encoded_filename_len++;
22955 /* Count . at the end. */
22956 if (encoded_filename_len)
22957 encoded_filename_len++;
22959 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22960 linebuf_len = strlen (linebuf);
22962 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22963 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22964 + 16 * 2 + 1);
22965 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22966 tail = grp_name + 4;
22967 if (encoded_filename_len)
22969 for (i = 0; base[i]; i++)
22970 if (ISIDNUM (base[i]) || base[i] == '.')
22971 *tail++ = base[i];
22972 *tail++ = '.';
22974 memcpy (tail, linebuf, linebuf_len);
22975 tail += linebuf_len;
22976 *tail++ = '.';
22977 for (i = 0; i < 16; i++)
22978 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22980 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22981 in the empty vector entry before the first define/undef. */
22982 inc = &(*macinfo_table)[idx - 1];
22983 inc->code = DW_MACRO_GNU_transparent_include;
22984 inc->lineno = 0;
22985 inc->info = ggc_strdup (grp_name);
22986 if (!*macinfo_htab)
22987 *macinfo_htab = new macinfo_hash_type (10);
22988 /* Avoid emitting duplicates. */
22989 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22990 if (*slot != NULL)
22992 inc->code = 0;
22993 inc->info = NULL;
22994 /* If such an entry has been used before, just emit
22995 a DW_MACRO_GNU_transparent_include op. */
22996 inc = *slot;
22997 output_macinfo_op (inc);
22998 /* And clear all macinfo_entry in the range to avoid emitting them
22999 in the second pass. */
23000 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23002 cur->code = 0;
23003 cur->info = NULL;
23006 else
23008 *slot = inc;
23009 inc->lineno = (*macinfo_htab)->elements ();
23010 output_macinfo_op (inc);
23012 return count;
23015 /* Save any strings needed by the macinfo table in the debug str
23016 table. All strings must be collected into the table by the time
23017 index_string is called. */
23019 static void
23020 save_macinfo_strings (void)
23022 unsigned len;
23023 unsigned i;
23024 macinfo_entry *ref;
23026 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23028 switch (ref->code)
23030 /* Match the logic in output_macinfo_op to decide on
23031 indirect strings. */
23032 case DW_MACINFO_define:
23033 case DW_MACINFO_undef:
23034 len = strlen (ref->info) + 1;
23035 if (!dwarf_strict
23036 && len > DWARF_OFFSET_SIZE
23037 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23038 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23039 set_indirect_string (find_AT_string (ref->info));
23040 break;
23041 case DW_MACRO_GNU_define_indirect:
23042 case DW_MACRO_GNU_undef_indirect:
23043 set_indirect_string (find_AT_string (ref->info));
23044 break;
23045 default:
23046 break;
23051 /* Output macinfo section(s). */
23053 static void
23054 output_macinfo (void)
23056 unsigned i;
23057 unsigned long length = vec_safe_length (macinfo_table);
23058 macinfo_entry *ref;
23059 vec<macinfo_entry, va_gc> *files = NULL;
23060 macinfo_hash_type *macinfo_htab = NULL;
23062 if (! length)
23063 return;
23065 /* output_macinfo* uses these interchangeably. */
23066 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23067 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23068 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23069 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23071 /* For .debug_macro emit the section header. */
23072 if (!dwarf_strict)
23074 dw2_asm_output_data (2, 4, "DWARF macro version number");
23075 if (DWARF_OFFSET_SIZE == 8)
23076 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23077 else
23078 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23079 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23080 (!dwarf_split_debug_info ? debug_line_section_label
23081 : debug_skeleton_line_section_label),
23082 debug_line_section, NULL);
23085 /* In the first loop, it emits the primary .debug_macinfo section
23086 and after each emitted op the macinfo_entry is cleared.
23087 If a longer range of define/undef ops can be optimized using
23088 DW_MACRO_GNU_transparent_include, the
23089 DW_MACRO_GNU_transparent_include op is emitted and kept in
23090 the vector before the first define/undef in the range and the
23091 whole range of define/undef ops is not emitted and kept. */
23092 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23094 switch (ref->code)
23096 case DW_MACINFO_start_file:
23097 vec_safe_push (files, *ref);
23098 break;
23099 case DW_MACINFO_end_file:
23100 if (!vec_safe_is_empty (files))
23101 files->pop ();
23102 break;
23103 case DW_MACINFO_define:
23104 case DW_MACINFO_undef:
23105 if (!dwarf_strict
23106 && HAVE_COMDAT_GROUP
23107 && vec_safe_length (files) != 1
23108 && i > 0
23109 && i + 1 < length
23110 && (*macinfo_table)[i - 1].code == 0)
23112 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23113 if (count)
23115 i += count - 1;
23116 continue;
23119 break;
23120 case 0:
23121 /* A dummy entry may be inserted at the beginning to be able
23122 to optimize the whole block of predefined macros. */
23123 if (i == 0)
23124 continue;
23125 default:
23126 break;
23128 output_macinfo_op (ref);
23129 ref->info = NULL;
23130 ref->code = 0;
23133 if (!macinfo_htab)
23134 return;
23136 delete macinfo_htab;
23137 macinfo_htab = NULL;
23139 /* If any DW_MACRO_GNU_transparent_include were used, on those
23140 DW_MACRO_GNU_transparent_include entries terminate the
23141 current chain and switch to a new comdat .debug_macinfo
23142 section and emit the define/undef entries within it. */
23143 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23144 switch (ref->code)
23146 case 0:
23147 continue;
23148 case DW_MACRO_GNU_transparent_include:
23150 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23151 tree comdat_key = get_identifier (ref->info);
23152 /* Terminate the previous .debug_macinfo section. */
23153 dw2_asm_output_data (1, 0, "End compilation unit");
23154 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23155 SECTION_DEBUG
23156 | SECTION_LINKONCE,
23157 comdat_key);
23158 ASM_GENERATE_INTERNAL_LABEL (label,
23159 DEBUG_MACRO_SECTION_LABEL,
23160 ref->lineno);
23161 ASM_OUTPUT_LABEL (asm_out_file, label);
23162 ref->code = 0;
23163 ref->info = NULL;
23164 dw2_asm_output_data (2, 4, "DWARF macro version number");
23165 if (DWARF_OFFSET_SIZE == 8)
23166 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23167 else
23168 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23170 break;
23171 case DW_MACINFO_define:
23172 case DW_MACINFO_undef:
23173 output_macinfo_op (ref);
23174 ref->code = 0;
23175 ref->info = NULL;
23176 break;
23177 default:
23178 gcc_unreachable ();
23182 /* Set up for Dwarf output at the start of compilation. */
23184 static void
23185 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23187 /* This option is currently broken, see (PR53118 and PR46102). */
23188 if (flag_eliminate_dwarf2_dups
23189 && strstr (lang_hooks.name, "C++"))
23191 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23192 flag_eliminate_dwarf2_dups = 0;
23195 /* Allocate the file_table. */
23196 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23198 /* Allocate the decl_die_table. */
23199 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23201 /* Allocate the decl_loc_table. */
23202 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23204 /* Allocate the cached_dw_loc_list_table. */
23205 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23207 /* Allocate the initial hunk of the decl_scope_table. */
23208 vec_alloc (decl_scope_table, 256);
23210 /* Allocate the initial hunk of the abbrev_die_table. */
23211 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23212 (ABBREV_DIE_TABLE_INCREMENT);
23213 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23214 /* Zero-th entry is allocated, but unused. */
23215 abbrev_die_table_in_use = 1;
23217 /* Allocate the pubtypes and pubnames vectors. */
23218 vec_alloc (pubname_table, 32);
23219 vec_alloc (pubtype_table, 32);
23221 vec_alloc (incomplete_types, 64);
23223 vec_alloc (used_rtx_array, 32);
23225 if (!dwarf_split_debug_info)
23227 debug_info_section = get_section (DEBUG_INFO_SECTION,
23228 SECTION_DEBUG, NULL);
23229 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23230 SECTION_DEBUG, NULL);
23231 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23232 SECTION_DEBUG, NULL);
23234 else
23236 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23237 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23238 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23239 SECTION_DEBUG | SECTION_EXCLUDE,
23240 NULL);
23241 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23242 SECTION_DEBUG, NULL);
23243 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23244 SECTION_DEBUG, NULL);
23245 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23246 SECTION_DEBUG, NULL);
23247 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23248 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23250 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23251 the main .o, but the skeleton_line goes into the split off dwo. */
23252 debug_skeleton_line_section
23253 = get_section (DEBUG_DWO_LINE_SECTION,
23254 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23255 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23256 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23257 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23258 SECTION_DEBUG | SECTION_EXCLUDE,
23259 NULL);
23260 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23261 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23262 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23263 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23264 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23265 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23267 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23268 SECTION_DEBUG, NULL);
23269 debug_macinfo_section = get_section (dwarf_strict
23270 ? DEBUG_MACINFO_SECTION
23271 : DEBUG_MACRO_SECTION,
23272 DEBUG_MACRO_SECTION_FLAGS, NULL);
23273 debug_line_section = get_section (DEBUG_LINE_SECTION,
23274 SECTION_DEBUG, NULL);
23275 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23276 SECTION_DEBUG, NULL);
23277 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23278 SECTION_DEBUG, NULL);
23279 debug_str_section = get_section (DEBUG_STR_SECTION,
23280 DEBUG_STR_SECTION_FLAGS, NULL);
23281 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23282 SECTION_DEBUG, NULL);
23283 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23284 SECTION_DEBUG, NULL);
23286 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23287 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23288 DEBUG_ABBREV_SECTION_LABEL, 0);
23289 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23290 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23291 COLD_TEXT_SECTION_LABEL, 0);
23292 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23294 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23295 DEBUG_INFO_SECTION_LABEL, 0);
23296 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23297 DEBUG_LINE_SECTION_LABEL, 0);
23298 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23299 DEBUG_RANGES_SECTION_LABEL, 0);
23300 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23301 DEBUG_ADDR_SECTION_LABEL, 0);
23302 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23303 dwarf_strict
23304 ? DEBUG_MACINFO_SECTION_LABEL
23305 : DEBUG_MACRO_SECTION_LABEL, 0);
23306 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23308 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23309 vec_alloc (macinfo_table, 64);
23311 switch_to_section (text_section);
23312 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23314 /* Make sure the line number table for .text always exists. */
23315 text_section_line_info = new_line_info_table ();
23316 text_section_line_info->end_label = text_end_label;
23318 /* If front-ends already registered a main translation unit but we were not
23319 ready to perform the association, do this now. */
23320 if (main_translation_unit != NULL_TREE)
23321 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23324 /* Called before compile () starts outputtting functions, variables
23325 and toplevel asms into assembly. */
23327 static void
23328 dwarf2out_assembly_start (void)
23330 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23331 && dwarf2out_do_cfi_asm ()
23332 && (!(flag_unwind_tables || flag_exceptions)
23333 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23334 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23337 /* A helper function for dwarf2out_finish called through
23338 htab_traverse. Assign a string its index. All strings must be
23339 collected into the table by the time index_string is called,
23340 because the indexing code relies on htab_traverse to traverse nodes
23341 in the same order for each run. */
23344 index_string (indirect_string_node **h, unsigned int *index)
23346 indirect_string_node *node = *h;
23348 find_string_form (node);
23349 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23351 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23352 node->index = *index;
23353 *index += 1;
23355 return 1;
23358 /* A helper function for output_indirect_strings called through
23359 htab_traverse. Output the offset to a string and update the
23360 current offset. */
23363 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23365 indirect_string_node *node = *h;
23367 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23369 /* Assert that this node has been assigned an index. */
23370 gcc_assert (node->index != NO_INDEX_ASSIGNED
23371 && node->index != NOT_INDEXED);
23372 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23373 "indexed string 0x%x: %s", node->index, node->str);
23374 *offset += strlen (node->str) + 1;
23376 return 1;
23379 /* A helper function for dwarf2out_finish called through
23380 htab_traverse. Output the indexed string. */
23383 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23385 struct indirect_string_node *node = *h;
23387 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23389 /* Assert that the strings are output in the same order as their
23390 indexes were assigned. */
23391 gcc_assert (*cur_idx == node->index);
23392 assemble_string (node->str, strlen (node->str) + 1);
23393 *cur_idx += 1;
23395 return 1;
23398 /* A helper function for dwarf2out_finish called through
23399 htab_traverse. Emit one queued .debug_str string. */
23402 output_indirect_string (indirect_string_node **h, void *)
23404 struct indirect_string_node *node = *h;
23406 node->form = find_string_form (node);
23407 if (node->form == DW_FORM_strp && node->refcount > 0)
23409 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23410 assemble_string (node->str, strlen (node->str) + 1);
23413 return 1;
23416 /* Output the indexed string table. */
23418 static void
23419 output_indirect_strings (void)
23421 switch_to_section (debug_str_section);
23422 if (!dwarf_split_debug_info)
23423 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23424 else
23426 unsigned int offset = 0;
23427 unsigned int cur_idx = 0;
23429 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23431 switch_to_section (debug_str_offsets_section);
23432 debug_str_hash->traverse_noresize
23433 <unsigned int *, output_index_string_offset> (&offset);
23434 switch_to_section (debug_str_dwo_section);
23435 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23436 (&cur_idx);
23440 /* Callback for htab_traverse to assign an index to an entry in the
23441 table, and to write that entry to the .debug_addr section. */
23444 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23446 addr_table_entry *entry = *slot;
23448 if (entry->refcount == 0)
23450 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23451 || entry->index == NOT_INDEXED);
23452 return 1;
23455 gcc_assert (entry->index == *cur_index);
23456 (*cur_index)++;
23458 switch (entry->kind)
23460 case ate_kind_rtx:
23461 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23462 "0x%x", entry->index);
23463 break;
23464 case ate_kind_rtx_dtprel:
23465 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23466 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23467 DWARF2_ADDR_SIZE,
23468 entry->addr.rtl);
23469 fputc ('\n', asm_out_file);
23470 break;
23471 case ate_kind_label:
23472 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23473 "0x%x", entry->index);
23474 break;
23475 default:
23476 gcc_unreachable ();
23478 return 1;
23481 /* Produce the .debug_addr section. */
23483 static void
23484 output_addr_table (void)
23486 unsigned int index = 0;
23487 if (addr_index_table == NULL || addr_index_table->size () == 0)
23488 return;
23490 switch_to_section (debug_addr_section);
23491 addr_index_table
23492 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23495 #if ENABLE_ASSERT_CHECKING
23496 /* Verify that all marks are clear. */
23498 static void
23499 verify_marks_clear (dw_die_ref die)
23501 dw_die_ref c;
23503 gcc_assert (! die->die_mark);
23504 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23506 #endif /* ENABLE_ASSERT_CHECKING */
23508 /* Clear the marks for a die and its children.
23509 Be cool if the mark isn't set. */
23511 static void
23512 prune_unmark_dies (dw_die_ref die)
23514 dw_die_ref c;
23516 if (die->die_mark)
23517 die->die_mark = 0;
23518 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23521 /* Given DIE that we're marking as used, find any other dies
23522 it references as attributes and mark them as used. */
23524 static void
23525 prune_unused_types_walk_attribs (dw_die_ref die)
23527 dw_attr_ref a;
23528 unsigned ix;
23530 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23532 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23534 /* A reference to another DIE.
23535 Make sure that it will get emitted.
23536 If it was broken out into a comdat group, don't follow it. */
23537 if (! AT_ref (a)->comdat_type_p
23538 || a->dw_attr == DW_AT_specification)
23539 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23541 /* Set the string's refcount to 0 so that prune_unused_types_mark
23542 accounts properly for it. */
23543 if (AT_class (a) == dw_val_class_str)
23544 a->dw_attr_val.v.val_str->refcount = 0;
23548 /* Mark the generic parameters and arguments children DIEs of DIE. */
23550 static void
23551 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23553 dw_die_ref c;
23555 if (die == NULL || die->die_child == NULL)
23556 return;
23557 c = die->die_child;
23560 if (is_template_parameter (c))
23561 prune_unused_types_mark (c, 1);
23562 c = c->die_sib;
23563 } while (c && c != die->die_child);
23566 /* Mark DIE as being used. If DOKIDS is true, then walk down
23567 to DIE's children. */
23569 static void
23570 prune_unused_types_mark (dw_die_ref die, int dokids)
23572 dw_die_ref c;
23574 if (die->die_mark == 0)
23576 /* We haven't done this node yet. Mark it as used. */
23577 die->die_mark = 1;
23578 /* If this is the DIE of a generic type instantiation,
23579 mark the children DIEs that describe its generic parms and
23580 args. */
23581 prune_unused_types_mark_generic_parms_dies (die);
23583 /* We also have to mark its parents as used.
23584 (But we don't want to mark our parent's kids due to this,
23585 unless it is a class.) */
23586 if (die->die_parent)
23587 prune_unused_types_mark (die->die_parent,
23588 class_scope_p (die->die_parent));
23590 /* Mark any referenced nodes. */
23591 prune_unused_types_walk_attribs (die);
23593 /* If this node is a specification,
23594 also mark the definition, if it exists. */
23595 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23596 prune_unused_types_mark (die->die_definition, 1);
23599 if (dokids && die->die_mark != 2)
23601 /* We need to walk the children, but haven't done so yet.
23602 Remember that we've walked the kids. */
23603 die->die_mark = 2;
23605 /* If this is an array type, we need to make sure our
23606 kids get marked, even if they're types. If we're
23607 breaking out types into comdat sections, do this
23608 for all type definitions. */
23609 if (die->die_tag == DW_TAG_array_type
23610 || (use_debug_types
23611 && is_type_die (die) && ! is_declaration_die (die)))
23612 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23613 else
23614 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23618 /* For local classes, look if any static member functions were emitted
23619 and if so, mark them. */
23621 static void
23622 prune_unused_types_walk_local_classes (dw_die_ref die)
23624 dw_die_ref c;
23626 if (die->die_mark == 2)
23627 return;
23629 switch (die->die_tag)
23631 case DW_TAG_structure_type:
23632 case DW_TAG_union_type:
23633 case DW_TAG_class_type:
23634 break;
23636 case DW_TAG_subprogram:
23637 if (!get_AT_flag (die, DW_AT_declaration)
23638 || die->die_definition != NULL)
23639 prune_unused_types_mark (die, 1);
23640 return;
23642 default:
23643 return;
23646 /* Mark children. */
23647 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23650 /* Walk the tree DIE and mark types that we actually use. */
23652 static void
23653 prune_unused_types_walk (dw_die_ref die)
23655 dw_die_ref c;
23657 /* Don't do anything if this node is already marked and
23658 children have been marked as well. */
23659 if (die->die_mark == 2)
23660 return;
23662 switch (die->die_tag)
23664 case DW_TAG_structure_type:
23665 case DW_TAG_union_type:
23666 case DW_TAG_class_type:
23667 if (die->die_perennial_p)
23668 break;
23670 for (c = die->die_parent; c; c = c->die_parent)
23671 if (c->die_tag == DW_TAG_subprogram)
23672 break;
23674 /* Finding used static member functions inside of classes
23675 is needed just for local classes, because for other classes
23676 static member function DIEs with DW_AT_specification
23677 are emitted outside of the DW_TAG_*_type. If we ever change
23678 it, we'd need to call this even for non-local classes. */
23679 if (c)
23680 prune_unused_types_walk_local_classes (die);
23682 /* It's a type node --- don't mark it. */
23683 return;
23685 case DW_TAG_const_type:
23686 case DW_TAG_packed_type:
23687 case DW_TAG_pointer_type:
23688 case DW_TAG_reference_type:
23689 case DW_TAG_rvalue_reference_type:
23690 case DW_TAG_volatile_type:
23691 case DW_TAG_typedef:
23692 case DW_TAG_array_type:
23693 case DW_TAG_interface_type:
23694 case DW_TAG_friend:
23695 case DW_TAG_variant_part:
23696 case DW_TAG_enumeration_type:
23697 case DW_TAG_subroutine_type:
23698 case DW_TAG_string_type:
23699 case DW_TAG_set_type:
23700 case DW_TAG_subrange_type:
23701 case DW_TAG_ptr_to_member_type:
23702 case DW_TAG_file_type:
23703 if (die->die_perennial_p)
23704 break;
23706 /* It's a type node --- don't mark it. */
23707 return;
23709 default:
23710 /* Mark everything else. */
23711 break;
23714 if (die->die_mark == 0)
23716 die->die_mark = 1;
23718 /* Now, mark any dies referenced from here. */
23719 prune_unused_types_walk_attribs (die);
23722 die->die_mark = 2;
23724 /* Mark children. */
23725 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23728 /* Increment the string counts on strings referred to from DIE's
23729 attributes. */
23731 static void
23732 prune_unused_types_update_strings (dw_die_ref die)
23734 dw_attr_ref a;
23735 unsigned ix;
23737 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23738 if (AT_class (a) == dw_val_class_str)
23740 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23741 s->refcount++;
23742 /* Avoid unnecessarily putting strings that are used less than
23743 twice in the hash table. */
23744 if (s->refcount
23745 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23747 indirect_string_node **slot
23748 = debug_str_hash->find_slot_with_hash (s->str,
23749 htab_hash_string (s->str),
23750 INSERT);
23751 gcc_assert (*slot == NULL);
23752 *slot = s;
23757 /* Remove from the tree DIE any dies that aren't marked. */
23759 static void
23760 prune_unused_types_prune (dw_die_ref die)
23762 dw_die_ref c;
23764 gcc_assert (die->die_mark);
23765 prune_unused_types_update_strings (die);
23767 if (! die->die_child)
23768 return;
23770 c = die->die_child;
23771 do {
23772 dw_die_ref prev = c;
23773 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23774 if (c == die->die_child)
23776 /* No marked children between 'prev' and the end of the list. */
23777 if (prev == c)
23778 /* No marked children at all. */
23779 die->die_child = NULL;
23780 else
23782 prev->die_sib = c->die_sib;
23783 die->die_child = prev;
23785 return;
23788 if (c != prev->die_sib)
23789 prev->die_sib = c;
23790 prune_unused_types_prune (c);
23791 } while (c != die->die_child);
23794 /* Remove dies representing declarations that we never use. */
23796 static void
23797 prune_unused_types (void)
23799 unsigned int i;
23800 limbo_die_node *node;
23801 comdat_type_node *ctnode;
23802 pubname_ref pub;
23803 dw_die_ref base_type;
23805 #if ENABLE_ASSERT_CHECKING
23806 /* All the marks should already be clear. */
23807 verify_marks_clear (comp_unit_die ());
23808 for (node = limbo_die_list; node; node = node->next)
23809 verify_marks_clear (node->die);
23810 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23811 verify_marks_clear (ctnode->root_die);
23812 #endif /* ENABLE_ASSERT_CHECKING */
23814 /* Mark types that are used in global variables. */
23815 premark_types_used_by_global_vars ();
23817 /* Set the mark on nodes that are actually used. */
23818 prune_unused_types_walk (comp_unit_die ());
23819 for (node = limbo_die_list; node; node = node->next)
23820 prune_unused_types_walk (node->die);
23821 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23823 prune_unused_types_walk (ctnode->root_die);
23824 prune_unused_types_mark (ctnode->type_die, 1);
23827 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23828 are unusual in that they are pubnames that are the children of pubtypes.
23829 They should only be marked via their parent DW_TAG_enumeration_type die,
23830 not as roots in themselves. */
23831 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23832 if (pub->die->die_tag != DW_TAG_enumerator)
23833 prune_unused_types_mark (pub->die, 1);
23834 for (i = 0; base_types.iterate (i, &base_type); i++)
23835 prune_unused_types_mark (base_type, 1);
23837 if (debug_str_hash)
23838 debug_str_hash->empty ();
23839 if (skeleton_debug_str_hash)
23840 skeleton_debug_str_hash->empty ();
23841 prune_unused_types_prune (comp_unit_die ());
23842 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23844 node = *pnode;
23845 if (!node->die->die_mark)
23846 *pnode = node->next;
23847 else
23849 prune_unused_types_prune (node->die);
23850 pnode = &node->next;
23853 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23854 prune_unused_types_prune (ctnode->root_die);
23856 /* Leave the marks clear. */
23857 prune_unmark_dies (comp_unit_die ());
23858 for (node = limbo_die_list; node; node = node->next)
23859 prune_unmark_dies (node->die);
23860 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23861 prune_unmark_dies (ctnode->root_die);
23864 /* Set the parameter to true if there are any relative pathnames in
23865 the file table. */
23867 file_table_relative_p (dwarf_file_data **slot, bool *p)
23869 struct dwarf_file_data *d = *slot;
23870 if (!IS_ABSOLUTE_PATH (d->filename))
23872 *p = true;
23873 return 0;
23875 return 1;
23878 /* Helpers to manipulate hash table of comdat type units. */
23880 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23882 typedef comdat_type_node *value_type;
23883 typedef comdat_type_node *compare_type;
23884 static inline hashval_t hash (const comdat_type_node *);
23885 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23888 inline hashval_t
23889 comdat_type_hasher::hash (const comdat_type_node *type_node)
23891 hashval_t h;
23892 memcpy (&h, type_node->signature, sizeof (h));
23893 return h;
23896 inline bool
23897 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23898 const comdat_type_node *type_node_2)
23900 return (! memcmp (type_node_1->signature, type_node_2->signature,
23901 DWARF_TYPE_SIGNATURE_SIZE));
23904 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23905 to the location it would have been added, should we know its
23906 DECL_ASSEMBLER_NAME when we added other attributes. This will
23907 probably improve compactness of debug info, removing equivalent
23908 abbrevs, and hide any differences caused by deferring the
23909 computation of the assembler name, triggered by e.g. PCH. */
23911 static inline void
23912 move_linkage_attr (dw_die_ref die)
23914 unsigned ix = vec_safe_length (die->die_attr);
23915 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23917 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23918 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23920 while (--ix > 0)
23922 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23924 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23925 break;
23928 if (ix != vec_safe_length (die->die_attr) - 1)
23930 die->die_attr->pop ();
23931 die->die_attr->quick_insert (ix, linkage);
23935 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23936 referenced from typed stack ops and count how often they are used. */
23938 static void
23939 mark_base_types (dw_loc_descr_ref loc)
23941 dw_die_ref base_type = NULL;
23943 for (; loc; loc = loc->dw_loc_next)
23945 switch (loc->dw_loc_opc)
23947 case DW_OP_GNU_regval_type:
23948 case DW_OP_GNU_deref_type:
23949 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23950 break;
23951 case DW_OP_GNU_convert:
23952 case DW_OP_GNU_reinterpret:
23953 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23954 continue;
23955 /* FALLTHRU */
23956 case DW_OP_GNU_const_type:
23957 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23958 break;
23959 case DW_OP_GNU_entry_value:
23960 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23961 continue;
23962 default:
23963 continue;
23965 gcc_assert (base_type->die_parent == comp_unit_die ());
23966 if (base_type->die_mark)
23967 base_type->die_mark++;
23968 else
23970 base_types.safe_push (base_type);
23971 base_type->die_mark = 1;
23976 /* Comparison function for sorting marked base types. */
23978 static int
23979 base_type_cmp (const void *x, const void *y)
23981 dw_die_ref dx = *(const dw_die_ref *) x;
23982 dw_die_ref dy = *(const dw_die_ref *) y;
23983 unsigned int byte_size1, byte_size2;
23984 unsigned int encoding1, encoding2;
23985 if (dx->die_mark > dy->die_mark)
23986 return -1;
23987 if (dx->die_mark < dy->die_mark)
23988 return 1;
23989 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23990 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23991 if (byte_size1 < byte_size2)
23992 return 1;
23993 if (byte_size1 > byte_size2)
23994 return -1;
23995 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23996 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23997 if (encoding1 < encoding2)
23998 return 1;
23999 if (encoding1 > encoding2)
24000 return -1;
24001 return 0;
24004 /* Move base types marked by mark_base_types as early as possible
24005 in the CU, sorted by decreasing usage count both to make the
24006 uleb128 references as small as possible and to make sure they
24007 will have die_offset already computed by calc_die_sizes when
24008 sizes of typed stack loc ops is computed. */
24010 static void
24011 move_marked_base_types (void)
24013 unsigned int i;
24014 dw_die_ref base_type, die, c;
24016 if (base_types.is_empty ())
24017 return;
24019 /* Sort by decreasing usage count, they will be added again in that
24020 order later on. */
24021 base_types.qsort (base_type_cmp);
24022 die = comp_unit_die ();
24023 c = die->die_child;
24026 dw_die_ref prev = c;
24027 c = c->die_sib;
24028 while (c->die_mark)
24030 remove_child_with_prev (c, prev);
24031 /* As base types got marked, there must be at least
24032 one node other than DW_TAG_base_type. */
24033 gcc_assert (c != c->die_sib);
24034 c = c->die_sib;
24037 while (c != die->die_child);
24038 gcc_assert (die->die_child);
24039 c = die->die_child;
24040 for (i = 0; base_types.iterate (i, &base_type); i++)
24042 base_type->die_mark = 0;
24043 base_type->die_sib = c->die_sib;
24044 c->die_sib = base_type;
24045 c = base_type;
24049 /* Helper function for resolve_addr, attempt to resolve
24050 one CONST_STRING, return true if successful. Similarly verify that
24051 SYMBOL_REFs refer to variables emitted in the current CU. */
24053 static bool
24054 resolve_one_addr (rtx *addr)
24056 rtx rtl = *addr;
24058 if (GET_CODE (rtl) == CONST_STRING)
24060 size_t len = strlen (XSTR (rtl, 0)) + 1;
24061 tree t = build_string (len, XSTR (rtl, 0));
24062 tree tlen = size_int (len - 1);
24063 TREE_TYPE (t)
24064 = build_array_type (char_type_node, build_index_type (tlen));
24065 rtl = lookup_constant_def (t);
24066 if (!rtl || !MEM_P (rtl))
24067 return false;
24068 rtl = XEXP (rtl, 0);
24069 if (GET_CODE (rtl) == SYMBOL_REF
24070 && SYMBOL_REF_DECL (rtl)
24071 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24072 return false;
24073 vec_safe_push (used_rtx_array, rtl);
24074 *addr = rtl;
24075 return true;
24078 if (GET_CODE (rtl) == SYMBOL_REF
24079 && SYMBOL_REF_DECL (rtl))
24081 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24083 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24084 return false;
24086 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24087 return false;
24090 if (GET_CODE (rtl) == CONST)
24092 subrtx_ptr_iterator::array_type array;
24093 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24094 if (!resolve_one_addr (*iter))
24095 return false;
24098 return true;
24101 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24102 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24103 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24105 static rtx
24106 string_cst_pool_decl (tree t)
24108 rtx rtl = output_constant_def (t, 1);
24109 unsigned char *array;
24110 dw_loc_descr_ref l;
24111 tree decl;
24112 size_t len;
24113 dw_die_ref ref;
24115 if (!rtl || !MEM_P (rtl))
24116 return NULL_RTX;
24117 rtl = XEXP (rtl, 0);
24118 if (GET_CODE (rtl) != SYMBOL_REF
24119 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24120 return NULL_RTX;
24122 decl = SYMBOL_REF_DECL (rtl);
24123 if (!lookup_decl_die (decl))
24125 len = TREE_STRING_LENGTH (t);
24126 vec_safe_push (used_rtx_array, rtl);
24127 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24128 array = ggc_vec_alloc<unsigned char> (len);
24129 memcpy (array, TREE_STRING_POINTER (t), len);
24130 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24131 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24132 l->dw_loc_oprnd2.v.val_vec.length = len;
24133 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24134 l->dw_loc_oprnd2.v.val_vec.array = array;
24135 add_AT_loc (ref, DW_AT_location, l);
24136 equate_decl_number_to_die (decl, ref);
24138 return rtl;
24141 /* Helper function of resolve_addr_in_expr. LOC is
24142 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24143 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24144 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24145 with DW_OP_GNU_implicit_pointer if possible
24146 and return true, if unsuccessful, return false. */
24148 static bool
24149 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24151 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24152 HOST_WIDE_INT offset = 0;
24153 dw_die_ref ref = NULL;
24154 tree decl;
24156 if (GET_CODE (rtl) == CONST
24157 && GET_CODE (XEXP (rtl, 0)) == PLUS
24158 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24160 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24161 rtl = XEXP (XEXP (rtl, 0), 0);
24163 if (GET_CODE (rtl) == CONST_STRING)
24165 size_t len = strlen (XSTR (rtl, 0)) + 1;
24166 tree t = build_string (len, XSTR (rtl, 0));
24167 tree tlen = size_int (len - 1);
24169 TREE_TYPE (t)
24170 = build_array_type (char_type_node, build_index_type (tlen));
24171 rtl = string_cst_pool_decl (t);
24172 if (!rtl)
24173 return false;
24175 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24177 decl = SYMBOL_REF_DECL (rtl);
24178 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24180 ref = lookup_decl_die (decl);
24181 if (ref && (get_AT (ref, DW_AT_location)
24182 || get_AT (ref, DW_AT_const_value)))
24184 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24185 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24186 loc->dw_loc_oprnd1.val_entry = NULL;
24187 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24188 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24189 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24190 loc->dw_loc_oprnd2.v.val_int = offset;
24191 return true;
24195 return false;
24198 /* Helper function for resolve_addr, handle one location
24199 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24200 the location list couldn't be resolved. */
24202 static bool
24203 resolve_addr_in_expr (dw_loc_descr_ref loc)
24205 dw_loc_descr_ref keep = NULL;
24206 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24207 switch (loc->dw_loc_opc)
24209 case DW_OP_addr:
24210 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24212 if ((prev == NULL
24213 || prev->dw_loc_opc == DW_OP_piece
24214 || prev->dw_loc_opc == DW_OP_bit_piece)
24215 && loc->dw_loc_next
24216 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24217 && !dwarf_strict
24218 && optimize_one_addr_into_implicit_ptr (loc))
24219 break;
24220 return false;
24222 break;
24223 case DW_OP_GNU_addr_index:
24224 case DW_OP_GNU_const_index:
24225 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24226 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24228 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24229 if (!resolve_one_addr (&rtl))
24230 return false;
24231 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24232 loc->dw_loc_oprnd1.val_entry =
24233 add_addr_table_entry (rtl, ate_kind_rtx);
24235 break;
24236 case DW_OP_const4u:
24237 case DW_OP_const8u:
24238 if (loc->dtprel
24239 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24240 return false;
24241 break;
24242 case DW_OP_plus_uconst:
24243 if (size_of_loc_descr (loc)
24244 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24246 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24248 dw_loc_descr_ref repl
24249 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24250 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24251 add_loc_descr (&repl, loc->dw_loc_next);
24252 *loc = *repl;
24254 break;
24255 case DW_OP_implicit_value:
24256 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24257 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24258 return false;
24259 break;
24260 case DW_OP_GNU_implicit_pointer:
24261 case DW_OP_GNU_parameter_ref:
24262 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24264 dw_die_ref ref
24265 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24266 if (ref == NULL)
24267 return false;
24268 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24269 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24270 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24272 break;
24273 case DW_OP_GNU_const_type:
24274 case DW_OP_GNU_regval_type:
24275 case DW_OP_GNU_deref_type:
24276 case DW_OP_GNU_convert:
24277 case DW_OP_GNU_reinterpret:
24278 while (loc->dw_loc_next
24279 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24281 dw_die_ref base1, base2;
24282 unsigned enc1, enc2, size1, size2;
24283 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24284 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24285 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24286 else if (loc->dw_loc_oprnd1.val_class
24287 == dw_val_class_unsigned_const)
24288 break;
24289 else
24290 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24291 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24292 == dw_val_class_unsigned_const)
24293 break;
24294 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24295 gcc_assert (base1->die_tag == DW_TAG_base_type
24296 && base2->die_tag == DW_TAG_base_type);
24297 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24298 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24299 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24300 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24301 if (size1 == size2
24302 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24303 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24304 && loc != keep)
24305 || enc1 == enc2))
24307 /* Optimize away next DW_OP_GNU_convert after
24308 adjusting LOC's base type die reference. */
24309 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24310 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24311 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24312 else
24313 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24314 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24315 continue;
24317 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24318 point typed stack entry. */
24319 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24320 keep = loc->dw_loc_next;
24321 break;
24323 break;
24324 default:
24325 break;
24327 return true;
24330 /* Helper function of resolve_addr. DIE had DW_AT_location of
24331 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24332 and DW_OP_addr couldn't be resolved. resolve_addr has already
24333 removed the DW_AT_location attribute. This function attempts to
24334 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24335 to it or DW_AT_const_value attribute, if possible. */
24337 static void
24338 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24340 if (TREE_CODE (decl) != VAR_DECL
24341 || lookup_decl_die (decl) != die
24342 || DECL_EXTERNAL (decl)
24343 || !TREE_STATIC (decl)
24344 || DECL_INITIAL (decl) == NULL_TREE
24345 || DECL_P (DECL_INITIAL (decl))
24346 || get_AT (die, DW_AT_const_value))
24347 return;
24349 tree init = DECL_INITIAL (decl);
24350 HOST_WIDE_INT offset = 0;
24351 /* For variables that have been optimized away and thus
24352 don't have a memory location, see if we can emit
24353 DW_AT_const_value instead. */
24354 if (tree_add_const_value_attribute (die, init))
24355 return;
24356 if (dwarf_strict)
24357 return;
24358 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24359 and ADDR_EXPR refers to a decl that has DW_AT_location or
24360 DW_AT_const_value (but isn't addressable, otherwise
24361 resolving the original DW_OP_addr wouldn't fail), see if
24362 we can add DW_OP_GNU_implicit_pointer. */
24363 STRIP_NOPS (init);
24364 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24365 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24367 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24368 init = TREE_OPERAND (init, 0);
24369 STRIP_NOPS (init);
24371 if (TREE_CODE (init) != ADDR_EXPR)
24372 return;
24373 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24374 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24375 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24376 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24377 && TREE_OPERAND (init, 0) != decl))
24379 dw_die_ref ref;
24380 dw_loc_descr_ref l;
24382 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24384 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24385 if (!rtl)
24386 return;
24387 decl = SYMBOL_REF_DECL (rtl);
24389 else
24390 decl = TREE_OPERAND (init, 0);
24391 ref = lookup_decl_die (decl);
24392 if (ref == NULL
24393 || (!get_AT (ref, DW_AT_location)
24394 && !get_AT (ref, DW_AT_const_value)))
24395 return;
24396 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24397 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24398 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24399 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24400 add_AT_loc (die, DW_AT_location, l);
24404 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24405 an address in .rodata section if the string literal is emitted there,
24406 or remove the containing location list or replace DW_AT_const_value
24407 with DW_AT_location and empty location expression, if it isn't found
24408 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24409 to something that has been emitted in the current CU. */
24411 static void
24412 resolve_addr (dw_die_ref die)
24414 dw_die_ref c;
24415 dw_attr_ref a;
24416 dw_loc_list_ref *curr, *start, loc;
24417 unsigned ix;
24419 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24420 switch (AT_class (a))
24422 case dw_val_class_loc_list:
24423 start = curr = AT_loc_list_ptr (a);
24424 loc = *curr;
24425 gcc_assert (loc);
24426 /* The same list can be referenced more than once. See if we have
24427 already recorded the result from a previous pass. */
24428 if (loc->replaced)
24429 *curr = loc->dw_loc_next;
24430 else if (!loc->resolved_addr)
24432 /* As things stand, we do not expect or allow one die to
24433 reference a suffix of another die's location list chain.
24434 References must be identical or completely separate.
24435 There is therefore no need to cache the result of this
24436 pass on any list other than the first; doing so
24437 would lead to unnecessary writes. */
24438 while (*curr)
24440 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24441 if (!resolve_addr_in_expr ((*curr)->expr))
24443 dw_loc_list_ref next = (*curr)->dw_loc_next;
24444 dw_loc_descr_ref l = (*curr)->expr;
24446 if (next && (*curr)->ll_symbol)
24448 gcc_assert (!next->ll_symbol);
24449 next->ll_symbol = (*curr)->ll_symbol;
24451 if (dwarf_split_debug_info)
24452 remove_loc_list_addr_table_entries (l);
24453 *curr = next;
24455 else
24457 mark_base_types ((*curr)->expr);
24458 curr = &(*curr)->dw_loc_next;
24461 if (loc == *start)
24462 loc->resolved_addr = 1;
24463 else
24465 loc->replaced = 1;
24466 loc->dw_loc_next = *start;
24469 if (!*start)
24471 remove_AT (die, a->dw_attr);
24472 ix--;
24474 break;
24475 case dw_val_class_loc:
24477 dw_loc_descr_ref l = AT_loc (a);
24478 /* For -gdwarf-2 don't attempt to optimize
24479 DW_AT_data_member_location containing
24480 DW_OP_plus_uconst - older consumers might
24481 rely on it being that op instead of a more complex,
24482 but shorter, location description. */
24483 if ((dwarf_version > 2
24484 || a->dw_attr != DW_AT_data_member_location
24485 || l == NULL
24486 || l->dw_loc_opc != DW_OP_plus_uconst
24487 || l->dw_loc_next != NULL)
24488 && !resolve_addr_in_expr (l))
24490 if (dwarf_split_debug_info)
24491 remove_loc_list_addr_table_entries (l);
24492 if (l != NULL
24493 && l->dw_loc_next == NULL
24494 && l->dw_loc_opc == DW_OP_addr
24495 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24496 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24497 && a->dw_attr == DW_AT_location)
24499 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24500 remove_AT (die, a->dw_attr);
24501 ix--;
24502 optimize_location_into_implicit_ptr (die, decl);
24503 break;
24505 remove_AT (die, a->dw_attr);
24506 ix--;
24508 else
24509 mark_base_types (l);
24511 break;
24512 case dw_val_class_addr:
24513 if (a->dw_attr == DW_AT_const_value
24514 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24516 if (AT_index (a) != NOT_INDEXED)
24517 remove_addr_table_entry (a->dw_attr_val.val_entry);
24518 remove_AT (die, a->dw_attr);
24519 ix--;
24521 if (die->die_tag == DW_TAG_GNU_call_site
24522 && a->dw_attr == DW_AT_abstract_origin)
24524 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24525 dw_die_ref tdie = lookup_decl_die (tdecl);
24526 dw_die_ref cdie;
24527 if (tdie == NULL
24528 && DECL_EXTERNAL (tdecl)
24529 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24530 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24532 /* Creating a full DIE for tdecl is overly expensive and
24533 at this point even wrong when in the LTO phase
24534 as it can end up generating new type DIEs we didn't
24535 output and thus optimize_external_refs will crash. */
24536 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24537 add_AT_flag (tdie, DW_AT_external, 1);
24538 add_AT_flag (tdie, DW_AT_declaration, 1);
24539 add_linkage_attr (tdie, tdecl);
24540 add_name_and_src_coords_attributes (tdie, tdecl);
24541 equate_decl_number_to_die (tdecl, tdie);
24543 if (tdie)
24545 a->dw_attr_val.val_class = dw_val_class_die_ref;
24546 a->dw_attr_val.v.val_die_ref.die = tdie;
24547 a->dw_attr_val.v.val_die_ref.external = 0;
24549 else
24551 if (AT_index (a) != NOT_INDEXED)
24552 remove_addr_table_entry (a->dw_attr_val.val_entry);
24553 remove_AT (die, a->dw_attr);
24554 ix--;
24557 break;
24558 default:
24559 break;
24562 FOR_EACH_CHILD (die, c, resolve_addr (c));
24565 /* Helper routines for optimize_location_lists.
24566 This pass tries to share identical local lists in .debug_loc
24567 section. */
24569 /* Iteratively hash operands of LOC opcode into HSTATE. */
24571 static void
24572 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24574 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24575 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24577 switch (loc->dw_loc_opc)
24579 case DW_OP_const4u:
24580 case DW_OP_const8u:
24581 if (loc->dtprel)
24582 goto hash_addr;
24583 /* FALLTHRU */
24584 case DW_OP_const1u:
24585 case DW_OP_const1s:
24586 case DW_OP_const2u:
24587 case DW_OP_const2s:
24588 case DW_OP_const4s:
24589 case DW_OP_const8s:
24590 case DW_OP_constu:
24591 case DW_OP_consts:
24592 case DW_OP_pick:
24593 case DW_OP_plus_uconst:
24594 case DW_OP_breg0:
24595 case DW_OP_breg1:
24596 case DW_OP_breg2:
24597 case DW_OP_breg3:
24598 case DW_OP_breg4:
24599 case DW_OP_breg5:
24600 case DW_OP_breg6:
24601 case DW_OP_breg7:
24602 case DW_OP_breg8:
24603 case DW_OP_breg9:
24604 case DW_OP_breg10:
24605 case DW_OP_breg11:
24606 case DW_OP_breg12:
24607 case DW_OP_breg13:
24608 case DW_OP_breg14:
24609 case DW_OP_breg15:
24610 case DW_OP_breg16:
24611 case DW_OP_breg17:
24612 case DW_OP_breg18:
24613 case DW_OP_breg19:
24614 case DW_OP_breg20:
24615 case DW_OP_breg21:
24616 case DW_OP_breg22:
24617 case DW_OP_breg23:
24618 case DW_OP_breg24:
24619 case DW_OP_breg25:
24620 case DW_OP_breg26:
24621 case DW_OP_breg27:
24622 case DW_OP_breg28:
24623 case DW_OP_breg29:
24624 case DW_OP_breg30:
24625 case DW_OP_breg31:
24626 case DW_OP_regx:
24627 case DW_OP_fbreg:
24628 case DW_OP_piece:
24629 case DW_OP_deref_size:
24630 case DW_OP_xderef_size:
24631 hstate.add_object (val1->v.val_int);
24632 break;
24633 case DW_OP_skip:
24634 case DW_OP_bra:
24636 int offset;
24638 gcc_assert (val1->val_class == dw_val_class_loc);
24639 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24640 hstate.add_object (offset);
24642 break;
24643 case DW_OP_implicit_value:
24644 hstate.add_object (val1->v.val_unsigned);
24645 switch (val2->val_class)
24647 case dw_val_class_const:
24648 hstate.add_object (val2->v.val_int);
24649 break;
24650 case dw_val_class_vec:
24652 unsigned int elt_size = val2->v.val_vec.elt_size;
24653 unsigned int len = val2->v.val_vec.length;
24655 hstate.add_int (elt_size);
24656 hstate.add_int (len);
24657 hstate.add (val2->v.val_vec.array, len * elt_size);
24659 break;
24660 case dw_val_class_const_double:
24661 hstate.add_object (val2->v.val_double.low);
24662 hstate.add_object (val2->v.val_double.high);
24663 break;
24664 case dw_val_class_wide_int:
24665 hstate.add (val2->v.val_wide->get_val (),
24666 get_full_len (*val2->v.val_wide)
24667 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24668 break;
24669 case dw_val_class_addr:
24670 inchash::add_rtx (val2->v.val_addr, hstate);
24671 break;
24672 default:
24673 gcc_unreachable ();
24675 break;
24676 case DW_OP_bregx:
24677 case DW_OP_bit_piece:
24678 hstate.add_object (val1->v.val_int);
24679 hstate.add_object (val2->v.val_int);
24680 break;
24681 case DW_OP_addr:
24682 hash_addr:
24683 if (loc->dtprel)
24685 unsigned char dtprel = 0xd1;
24686 hstate.add_object (dtprel);
24688 inchash::add_rtx (val1->v.val_addr, hstate);
24689 break;
24690 case DW_OP_GNU_addr_index:
24691 case DW_OP_GNU_const_index:
24693 if (loc->dtprel)
24695 unsigned char dtprel = 0xd1;
24696 hstate.add_object (dtprel);
24698 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24700 break;
24701 case DW_OP_GNU_implicit_pointer:
24702 hstate.add_int (val2->v.val_int);
24703 break;
24704 case DW_OP_GNU_entry_value:
24705 hstate.add_object (val1->v.val_loc);
24706 break;
24707 case DW_OP_GNU_regval_type:
24708 case DW_OP_GNU_deref_type:
24710 unsigned int byte_size
24711 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24712 unsigned int encoding
24713 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24714 hstate.add_object (val1->v.val_int);
24715 hstate.add_object (byte_size);
24716 hstate.add_object (encoding);
24718 break;
24719 case DW_OP_GNU_convert:
24720 case DW_OP_GNU_reinterpret:
24721 if (val1->val_class == dw_val_class_unsigned_const)
24723 hstate.add_object (val1->v.val_unsigned);
24724 break;
24726 /* FALLTHRU */
24727 case DW_OP_GNU_const_type:
24729 unsigned int byte_size
24730 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24731 unsigned int encoding
24732 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24733 hstate.add_object (byte_size);
24734 hstate.add_object (encoding);
24735 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24736 break;
24737 hstate.add_object (val2->val_class);
24738 switch (val2->val_class)
24740 case dw_val_class_const:
24741 hstate.add_object (val2->v.val_int);
24742 break;
24743 case dw_val_class_vec:
24745 unsigned int elt_size = val2->v.val_vec.elt_size;
24746 unsigned int len = val2->v.val_vec.length;
24748 hstate.add_object (elt_size);
24749 hstate.add_object (len);
24750 hstate.add (val2->v.val_vec.array, len * elt_size);
24752 break;
24753 case dw_val_class_const_double:
24754 hstate.add_object (val2->v.val_double.low);
24755 hstate.add_object (val2->v.val_double.high);
24756 break;
24757 case dw_val_class_wide_int:
24758 hstate.add (val2->v.val_wide->get_val (),
24759 get_full_len (*val2->v.val_wide)
24760 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24761 break;
24762 default:
24763 gcc_unreachable ();
24766 break;
24768 default:
24769 /* Other codes have no operands. */
24770 break;
24774 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24776 static inline void
24777 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24779 dw_loc_descr_ref l;
24780 bool sizes_computed = false;
24781 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24782 size_of_locs (loc);
24784 for (l = loc; l != NULL; l = l->dw_loc_next)
24786 enum dwarf_location_atom opc = l->dw_loc_opc;
24787 hstate.add_object (opc);
24788 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24790 size_of_locs (loc);
24791 sizes_computed = true;
24793 hash_loc_operands (l, hstate);
24797 /* Compute hash of the whole location list LIST_HEAD. */
24799 static inline void
24800 hash_loc_list (dw_loc_list_ref list_head)
24802 dw_loc_list_ref curr = list_head;
24803 inchash::hash hstate;
24805 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24807 hstate.add (curr->begin, strlen (curr->begin) + 1);
24808 hstate.add (curr->end, strlen (curr->end) + 1);
24809 if (curr->section)
24810 hstate.add (curr->section, strlen (curr->section) + 1);
24811 hash_locs (curr->expr, hstate);
24813 list_head->hash = hstate.end ();
24816 /* Return true if X and Y opcodes have the same operands. */
24818 static inline bool
24819 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24821 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24822 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24823 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24824 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24826 switch (x->dw_loc_opc)
24828 case DW_OP_const4u:
24829 case DW_OP_const8u:
24830 if (x->dtprel)
24831 goto hash_addr;
24832 /* FALLTHRU */
24833 case DW_OP_const1u:
24834 case DW_OP_const1s:
24835 case DW_OP_const2u:
24836 case DW_OP_const2s:
24837 case DW_OP_const4s:
24838 case DW_OP_const8s:
24839 case DW_OP_constu:
24840 case DW_OP_consts:
24841 case DW_OP_pick:
24842 case DW_OP_plus_uconst:
24843 case DW_OP_breg0:
24844 case DW_OP_breg1:
24845 case DW_OP_breg2:
24846 case DW_OP_breg3:
24847 case DW_OP_breg4:
24848 case DW_OP_breg5:
24849 case DW_OP_breg6:
24850 case DW_OP_breg7:
24851 case DW_OP_breg8:
24852 case DW_OP_breg9:
24853 case DW_OP_breg10:
24854 case DW_OP_breg11:
24855 case DW_OP_breg12:
24856 case DW_OP_breg13:
24857 case DW_OP_breg14:
24858 case DW_OP_breg15:
24859 case DW_OP_breg16:
24860 case DW_OP_breg17:
24861 case DW_OP_breg18:
24862 case DW_OP_breg19:
24863 case DW_OP_breg20:
24864 case DW_OP_breg21:
24865 case DW_OP_breg22:
24866 case DW_OP_breg23:
24867 case DW_OP_breg24:
24868 case DW_OP_breg25:
24869 case DW_OP_breg26:
24870 case DW_OP_breg27:
24871 case DW_OP_breg28:
24872 case DW_OP_breg29:
24873 case DW_OP_breg30:
24874 case DW_OP_breg31:
24875 case DW_OP_regx:
24876 case DW_OP_fbreg:
24877 case DW_OP_piece:
24878 case DW_OP_deref_size:
24879 case DW_OP_xderef_size:
24880 return valx1->v.val_int == valy1->v.val_int;
24881 case DW_OP_skip:
24882 case DW_OP_bra:
24883 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24884 can cause irrelevant differences in dw_loc_addr. */
24885 gcc_assert (valx1->val_class == dw_val_class_loc
24886 && valy1->val_class == dw_val_class_loc
24887 && (dwarf_split_debug_info
24888 || x->dw_loc_addr == y->dw_loc_addr));
24889 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24890 case DW_OP_implicit_value:
24891 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24892 || valx2->val_class != valy2->val_class)
24893 return false;
24894 switch (valx2->val_class)
24896 case dw_val_class_const:
24897 return valx2->v.val_int == valy2->v.val_int;
24898 case dw_val_class_vec:
24899 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24900 && valx2->v.val_vec.length == valy2->v.val_vec.length
24901 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24902 valx2->v.val_vec.elt_size
24903 * valx2->v.val_vec.length) == 0;
24904 case dw_val_class_const_double:
24905 return valx2->v.val_double.low == valy2->v.val_double.low
24906 && valx2->v.val_double.high == valy2->v.val_double.high;
24907 case dw_val_class_wide_int:
24908 return *valx2->v.val_wide == *valy2->v.val_wide;
24909 case dw_val_class_addr:
24910 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24911 default:
24912 gcc_unreachable ();
24914 case DW_OP_bregx:
24915 case DW_OP_bit_piece:
24916 return valx1->v.val_int == valy1->v.val_int
24917 && valx2->v.val_int == valy2->v.val_int;
24918 case DW_OP_addr:
24919 hash_addr:
24920 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24921 case DW_OP_GNU_addr_index:
24922 case DW_OP_GNU_const_index:
24924 rtx ax1 = valx1->val_entry->addr.rtl;
24925 rtx ay1 = valy1->val_entry->addr.rtl;
24926 return rtx_equal_p (ax1, ay1);
24928 case DW_OP_GNU_implicit_pointer:
24929 return valx1->val_class == dw_val_class_die_ref
24930 && valx1->val_class == valy1->val_class
24931 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24932 && valx2->v.val_int == valy2->v.val_int;
24933 case DW_OP_GNU_entry_value:
24934 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24935 case DW_OP_GNU_const_type:
24936 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24937 || valx2->val_class != valy2->val_class)
24938 return false;
24939 switch (valx2->val_class)
24941 case dw_val_class_const:
24942 return valx2->v.val_int == valy2->v.val_int;
24943 case dw_val_class_vec:
24944 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24945 && valx2->v.val_vec.length == valy2->v.val_vec.length
24946 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24947 valx2->v.val_vec.elt_size
24948 * valx2->v.val_vec.length) == 0;
24949 case dw_val_class_const_double:
24950 return valx2->v.val_double.low == valy2->v.val_double.low
24951 && valx2->v.val_double.high == valy2->v.val_double.high;
24952 case dw_val_class_wide_int:
24953 return *valx2->v.val_wide == *valy2->v.val_wide;
24954 default:
24955 gcc_unreachable ();
24957 case DW_OP_GNU_regval_type:
24958 case DW_OP_GNU_deref_type:
24959 return valx1->v.val_int == valy1->v.val_int
24960 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24961 case DW_OP_GNU_convert:
24962 case DW_OP_GNU_reinterpret:
24963 if (valx1->val_class != valy1->val_class)
24964 return false;
24965 if (valx1->val_class == dw_val_class_unsigned_const)
24966 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24967 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24968 case DW_OP_GNU_parameter_ref:
24969 return valx1->val_class == dw_val_class_die_ref
24970 && valx1->val_class == valy1->val_class
24971 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24972 default:
24973 /* Other codes have no operands. */
24974 return true;
24978 /* Return true if DWARF location expressions X and Y are the same. */
24980 static inline bool
24981 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24983 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24984 if (x->dw_loc_opc != y->dw_loc_opc
24985 || x->dtprel != y->dtprel
24986 || !compare_loc_operands (x, y))
24987 break;
24988 return x == NULL && y == NULL;
24991 /* Hashtable helpers. */
24993 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24995 typedef dw_loc_list_struct *value_type;
24996 typedef dw_loc_list_struct *compare_type;
24997 static inline hashval_t hash (const dw_loc_list_struct *);
24998 static inline bool equal (const dw_loc_list_struct *,
24999 const dw_loc_list_struct *);
25002 /* Return precomputed hash of location list X. */
25004 inline hashval_t
25005 loc_list_hasher::hash (const dw_loc_list_struct *x)
25007 return x->hash;
25010 /* Return true if location lists A and B are the same. */
25012 inline bool
25013 loc_list_hasher::equal (const dw_loc_list_struct *a,
25014 const dw_loc_list_struct *b)
25016 if (a == b)
25017 return 1;
25018 if (a->hash != b->hash)
25019 return 0;
25020 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25021 if (strcmp (a->begin, b->begin) != 0
25022 || strcmp (a->end, b->end) != 0
25023 || (a->section == NULL) != (b->section == NULL)
25024 || (a->section && strcmp (a->section, b->section) != 0)
25025 || !compare_locs (a->expr, b->expr))
25026 break;
25027 return a == NULL && b == NULL;
25030 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25033 /* Recursively optimize location lists referenced from DIE
25034 children and share them whenever possible. */
25036 static void
25037 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25039 dw_die_ref c;
25040 dw_attr_ref a;
25041 unsigned ix;
25042 dw_loc_list_struct **slot;
25044 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25045 if (AT_class (a) == dw_val_class_loc_list)
25047 dw_loc_list_ref list = AT_loc_list (a);
25048 /* TODO: perform some optimizations here, before hashing
25049 it and storing into the hash table. */
25050 hash_loc_list (list);
25051 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25052 if (*slot == NULL)
25053 *slot = list;
25054 else
25055 a->dw_attr_val.v.val_loc_list = *slot;
25058 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25062 /* Recursively assign each location list a unique index into the debug_addr
25063 section. */
25065 static void
25066 index_location_lists (dw_die_ref die)
25068 dw_die_ref c;
25069 dw_attr_ref a;
25070 unsigned ix;
25072 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25073 if (AT_class (a) == dw_val_class_loc_list)
25075 dw_loc_list_ref list = AT_loc_list (a);
25076 dw_loc_list_ref curr;
25077 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25079 /* Don't index an entry that has already been indexed
25080 or won't be output. */
25081 if (curr->begin_entry != NULL
25082 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25083 continue;
25085 curr->begin_entry
25086 = add_addr_table_entry (xstrdup (curr->begin),
25087 ate_kind_label);
25091 FOR_EACH_CHILD (die, c, index_location_lists (c));
25094 /* Optimize location lists referenced from DIE
25095 children and share them whenever possible. */
25097 static void
25098 optimize_location_lists (dw_die_ref die)
25100 loc_list_hash_type htab (500);
25101 optimize_location_lists_1 (die, &htab);
25104 /* Output stuff that dwarf requires at the end of every file,
25105 and generate the DWARF-2 debugging info. */
25107 static void
25108 dwarf2out_finish (const char *filename)
25110 comdat_type_node *ctnode;
25111 dw_die_ref main_comp_unit_die;
25113 /* Flush out any latecomers to the limbo party. */
25114 dwarf2out_early_finish ();
25116 /* PCH might result in DW_AT_producer string being restored from the
25117 header compilation, so always fill it with empty string initially
25118 and overwrite only here. */
25119 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25120 producer_string = gen_producer_string ();
25121 producer->dw_attr_val.v.val_str->refcount--;
25122 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25124 gen_scheduled_generic_parms_dies ();
25125 gen_remaining_tmpl_value_param_die_attribute ();
25127 /* Add the name for the main input file now. We delayed this from
25128 dwarf2out_init to avoid complications with PCH.
25129 For LTO produced units use a fixed artificial name to avoid
25130 leaking tempfile names into the dwarf. */
25131 if (!in_lto_p)
25132 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25133 else
25134 add_name_attribute (comp_unit_die (), "<artificial>");
25135 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25136 add_comp_dir_attribute (comp_unit_die ());
25137 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25139 bool p = false;
25140 file_table->traverse<bool *, file_table_relative_p> (&p);
25141 if (p)
25142 add_comp_dir_attribute (comp_unit_die ());
25145 #if ENABLE_ASSERT_CHECKING
25147 dw_die_ref die = comp_unit_die (), c;
25148 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25150 #endif
25151 resolve_addr (comp_unit_die ());
25152 move_marked_base_types ();
25154 /* Walk through the list of incomplete types again, trying once more to
25155 emit full debugging info for them. */
25156 retry_incomplete_types ();
25158 if (flag_eliminate_unused_debug_types)
25159 prune_unused_types ();
25161 /* Generate separate COMDAT sections for type DIEs. */
25162 if (use_debug_types)
25164 break_out_comdat_types (comp_unit_die ());
25166 /* Each new type_unit DIE was added to the limbo die list when created.
25167 Since these have all been added to comdat_type_list, clear the
25168 limbo die list. */
25169 limbo_die_list = NULL;
25171 /* For each new comdat type unit, copy declarations for incomplete
25172 types to make the new unit self-contained (i.e., no direct
25173 references to the main compile unit). */
25174 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25175 copy_decls_for_unworthy_types (ctnode->root_die);
25176 copy_decls_for_unworthy_types (comp_unit_die ());
25178 /* In the process of copying declarations from one unit to another,
25179 we may have left some declarations behind that are no longer
25180 referenced. Prune them. */
25181 prune_unused_types ();
25184 /* Generate separate CUs for each of the include files we've seen.
25185 They will go into limbo_die_list. */
25186 if (flag_eliminate_dwarf2_dups)
25187 break_out_includes (comp_unit_die ());
25189 /* Traverse the DIE's and add add sibling attributes to those DIE's
25190 that have children. */
25191 add_sibling_attributes (comp_unit_die ());
25192 limbo_die_node *node;
25193 for (node = limbo_die_list; node; node = node->next)
25194 add_sibling_attributes (node->die);
25195 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25196 add_sibling_attributes (ctnode->root_die);
25198 /* When splitting DWARF info, we put some attributes in the
25199 skeleton compile_unit DIE that remains in the .o, while
25200 most attributes go in the DWO compile_unit_die. */
25201 if (dwarf_split_debug_info)
25202 main_comp_unit_die = gen_compile_unit_die (NULL);
25203 else
25204 main_comp_unit_die = comp_unit_die ();
25206 /* Output a terminator label for the .text section. */
25207 switch_to_section (text_section);
25208 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25209 if (cold_text_section)
25211 switch_to_section (cold_text_section);
25212 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25215 /* We can only use the low/high_pc attributes if all of the code was
25216 in .text. */
25217 if (!have_multiple_function_sections
25218 || (dwarf_version < 3 && dwarf_strict))
25220 /* Don't add if the CU has no associated code. */
25221 if (text_section_used)
25222 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25223 text_end_label, true);
25225 else
25227 unsigned fde_idx;
25228 dw_fde_ref fde;
25229 bool range_list_added = false;
25231 if (text_section_used)
25232 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25233 text_end_label, &range_list_added, true);
25234 if (cold_text_section_used)
25235 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25236 cold_end_label, &range_list_added, true);
25238 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25240 if (DECL_IGNORED_P (fde->decl))
25241 continue;
25242 if (!fde->in_std_section)
25243 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25244 fde->dw_fde_end, &range_list_added,
25245 true);
25246 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25247 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25248 fde->dw_fde_second_end, &range_list_added,
25249 true);
25252 if (range_list_added)
25254 /* We need to give .debug_loc and .debug_ranges an appropriate
25255 "base address". Use zero so that these addresses become
25256 absolute. Historically, we've emitted the unexpected
25257 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25258 Emit both to give time for other tools to adapt. */
25259 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25260 if (! dwarf_strict && dwarf_version < 4)
25261 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25263 add_ranges (NULL);
25267 if (debug_info_level >= DINFO_LEVEL_TERSE)
25268 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25269 debug_line_section_label);
25271 if (have_macinfo)
25272 add_AT_macptr (comp_unit_die (),
25273 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25274 macinfo_section_label);
25276 if (dwarf_split_debug_info)
25278 /* optimize_location_lists calculates the size of the lists,
25279 so index them first, and assign indices to the entries.
25280 Although optimize_location_lists will remove entries from
25281 the table, it only does so for duplicates, and therefore
25282 only reduces ref_counts to 1. */
25283 index_location_lists (comp_unit_die ());
25285 if (addr_index_table != NULL)
25287 unsigned int index = 0;
25288 addr_index_table
25289 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25290 (&index);
25294 if (have_location_lists)
25295 optimize_location_lists (comp_unit_die ());
25297 save_macinfo_strings ();
25299 if (dwarf_split_debug_info)
25301 unsigned int index = 0;
25303 /* Add attributes common to skeleton compile_units and
25304 type_units. Because these attributes include strings, it
25305 must be done before freezing the string table. Top-level
25306 skeleton die attrs are added when the skeleton type unit is
25307 created, so ensure it is created by this point. */
25308 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25309 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25312 /* Output all of the compilation units. We put the main one last so that
25313 the offsets are available to output_pubnames. */
25314 for (node = limbo_die_list; node; node = node->next)
25315 output_comp_unit (node->die, 0);
25317 hash_table<comdat_type_hasher> comdat_type_table (100);
25318 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25320 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25322 /* Don't output duplicate types. */
25323 if (*slot != HTAB_EMPTY_ENTRY)
25324 continue;
25326 /* Add a pointer to the line table for the main compilation unit
25327 so that the debugger can make sense of DW_AT_decl_file
25328 attributes. */
25329 if (debug_info_level >= DINFO_LEVEL_TERSE)
25330 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25331 (!dwarf_split_debug_info
25332 ? debug_line_section_label
25333 : debug_skeleton_line_section_label));
25335 output_comdat_type_unit (ctnode);
25336 *slot = ctnode;
25339 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25340 both the main_cu and all skeleton TUs. Making this call unconditional
25341 would end up either adding a second copy of the AT_pubnames attribute, or
25342 requiring a special case in add_top_level_skeleton_die_attrs. */
25343 if (!dwarf_split_debug_info)
25344 add_AT_pubnames (comp_unit_die ());
25346 if (dwarf_split_debug_info)
25348 int mark;
25349 unsigned char checksum[16];
25350 struct md5_ctx ctx;
25352 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25353 md5_init_ctx (&ctx);
25354 mark = 0;
25355 die_checksum (comp_unit_die (), &ctx, &mark);
25356 unmark_all_dies (comp_unit_die ());
25357 md5_finish_ctx (&ctx, checksum);
25359 /* Use the first 8 bytes of the checksum as the dwo_id,
25360 and add it to both comp-unit DIEs. */
25361 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25362 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25364 /* Add the base offset of the ranges table to the skeleton
25365 comp-unit DIE. */
25366 if (ranges_table_in_use)
25367 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25368 ranges_section_label);
25370 switch_to_section (debug_addr_section);
25371 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25372 output_addr_table ();
25375 /* Output the main compilation unit if non-empty or if .debug_macinfo
25376 or .debug_macro will be emitted. */
25377 output_comp_unit (comp_unit_die (), have_macinfo);
25379 if (dwarf_split_debug_info && info_section_emitted)
25380 output_skeleton_debug_sections (main_comp_unit_die);
25382 /* Output the abbreviation table. */
25383 if (abbrev_die_table_in_use != 1)
25385 switch_to_section (debug_abbrev_section);
25386 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25387 output_abbrev_section ();
25390 /* Output location list section if necessary. */
25391 if (have_location_lists)
25393 /* Output the location lists info. */
25394 switch_to_section (debug_loc_section);
25395 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25396 output_location_lists (comp_unit_die ());
25399 output_pubtables ();
25401 /* Output the address range information if a CU (.debug_info section)
25402 was emitted. We output an empty table even if we had no functions
25403 to put in it. This because the consumer has no way to tell the
25404 difference between an empty table that we omitted and failure to
25405 generate a table that would have contained data. */
25406 if (info_section_emitted)
25408 unsigned long aranges_length = size_of_aranges ();
25410 switch_to_section (debug_aranges_section);
25411 output_aranges (aranges_length);
25414 /* Output ranges section if necessary. */
25415 if (ranges_table_in_use)
25417 switch_to_section (debug_ranges_section);
25418 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25419 output_ranges ();
25422 /* Have to end the macro section. */
25423 if (have_macinfo)
25425 switch_to_section (debug_macinfo_section);
25426 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25427 output_macinfo ();
25428 dw2_asm_output_data (1, 0, "End compilation unit");
25431 /* Output the source line correspondence table. We must do this
25432 even if there is no line information. Otherwise, on an empty
25433 translation unit, we will generate a present, but empty,
25434 .debug_info section. IRIX 6.5 `nm' will then complain when
25435 examining the file. This is done late so that any filenames
25436 used by the debug_info section are marked as 'used'. */
25437 switch_to_section (debug_line_section);
25438 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25439 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25440 output_line_info (false);
25442 if (dwarf_split_debug_info && info_section_emitted)
25444 switch_to_section (debug_skeleton_line_section);
25445 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25446 output_line_info (true);
25449 /* If we emitted any indirect strings, output the string table too. */
25450 if (debug_str_hash || skeleton_debug_str_hash)
25451 output_indirect_strings ();
25454 /* Perform any cleanups needed after the early debug generation pass
25455 has run. */
25457 static void
25458 dwarf2out_early_finish (void)
25460 limbo_die_node *node, *next_node;
25462 /* Add DW_AT_linkage_name for all deferred DIEs. */
25463 for (node = deferred_asm_name; node; node = node->next)
25465 tree decl = node->created_for;
25466 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25467 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25468 ended up in in deferred_asm_name before we knew it was
25469 constant and never written to disk. */
25470 && DECL_ASSEMBLER_NAME (decl))
25472 add_linkage_attr (node->die, decl);
25473 move_linkage_attr (node->die);
25476 deferred_asm_name = NULL;
25478 /* Traverse the limbo die list, and add parent/child links. The only
25479 dies without parents that should be here are concrete instances of
25480 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25481 For concrete instances, we can get the parent die from the abstract
25482 instance.
25484 The point here is to flush out the limbo list so that it is empty
25485 and we don't need to stream it for LTO. */
25486 for (node = limbo_die_list; node; node = next_node)
25488 dw_die_ref die = node->die;
25489 next_node = node->next;
25491 if (die->die_parent == NULL)
25493 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25495 if (origin && origin->die_parent)
25496 add_child_die (origin->die_parent, die);
25497 else if (is_cu_die (die))
25499 else if (seen_error ())
25500 /* It's OK to be confused by errors in the input. */
25501 add_child_die (comp_unit_die (), die);
25502 else
25504 /* In certain situations, the lexical block containing a
25505 nested function can be optimized away, which results
25506 in the nested function die being orphaned. Likewise
25507 with the return type of that nested function. Force
25508 this to be a child of the containing function.
25510 It may happen that even the containing function got fully
25511 inlined and optimized out. In that case we are lost and
25512 assign the empty child. This should not be big issue as
25513 the function is likely unreachable too. */
25514 gcc_assert (node->created_for);
25516 if (DECL_P (node->created_for))
25517 origin = get_context_die (DECL_CONTEXT (node->created_for));
25518 else if (TYPE_P (node->created_for))
25519 origin = scope_die_for (node->created_for, comp_unit_die ());
25520 else
25521 origin = comp_unit_die ();
25523 add_child_die (origin, die);
25528 limbo_die_list = NULL;
25531 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25532 within the same process. For use by toplev::finalize. */
25534 void
25535 dwarf2out_c_finalize (void)
25537 last_var_location_insn = NULL;
25538 cached_next_real_insn = NULL;
25539 used_rtx_array = NULL;
25540 incomplete_types = NULL;
25541 decl_scope_table = NULL;
25542 debug_info_section = NULL;
25543 debug_skeleton_info_section = NULL;
25544 debug_abbrev_section = NULL;
25545 debug_skeleton_abbrev_section = NULL;
25546 debug_aranges_section = NULL;
25547 debug_addr_section = NULL;
25548 debug_macinfo_section = NULL;
25549 debug_line_section = NULL;
25550 debug_skeleton_line_section = NULL;
25551 debug_loc_section = NULL;
25552 debug_pubnames_section = NULL;
25553 debug_pubtypes_section = NULL;
25554 debug_str_section = NULL;
25555 debug_str_dwo_section = NULL;
25556 debug_str_offsets_section = NULL;
25557 debug_ranges_section = NULL;
25558 debug_frame_section = NULL;
25559 fde_vec = NULL;
25560 debug_str_hash = NULL;
25561 skeleton_debug_str_hash = NULL;
25562 dw2_string_counter = 0;
25563 have_multiple_function_sections = false;
25564 text_section_used = false;
25565 cold_text_section_used = false;
25566 cold_text_section = NULL;
25567 current_unit_personality = NULL;
25569 next_die_offset = 0;
25570 single_comp_unit_die = NULL;
25571 comdat_type_list = NULL;
25572 limbo_die_list = NULL;
25573 file_table = NULL;
25574 decl_die_table = NULL;
25575 common_block_die_table = NULL;
25576 decl_loc_table = NULL;
25577 call_arg_locations = NULL;
25578 call_arg_loc_last = NULL;
25579 call_site_count = -1;
25580 tail_call_site_count = -1;
25581 cached_dw_loc_list_table = NULL;
25582 abbrev_die_table = NULL;
25583 abbrev_die_table_allocated = 0;
25584 abbrev_die_table_in_use = 0;
25585 line_info_label_num = 0;
25586 cur_line_info_table = NULL;
25587 text_section_line_info = NULL;
25588 cold_text_section_line_info = NULL;
25589 separate_line_info = NULL;
25590 info_section_emitted = false;
25591 pubname_table = NULL;
25592 pubtype_table = NULL;
25593 macinfo_table = NULL;
25594 ranges_table = NULL;
25595 ranges_table_allocated = 0;
25596 ranges_table_in_use = 0;
25597 ranges_by_label = 0;
25598 ranges_by_label_allocated = 0;
25599 ranges_by_label_in_use = 0;
25600 have_location_lists = false;
25601 loclabel_num = 0;
25602 poc_label_num = 0;
25603 last_emitted_file = NULL;
25604 label_num = 0;
25605 tmpl_value_parm_die_table = NULL;
25606 generic_type_instances = NULL;
25607 frame_pointer_fb_offset = 0;
25608 frame_pointer_fb_offset_valid = false;
25609 base_types.release ();
25610 XDELETEVEC (producer_string);
25611 producer_string = NULL;
25614 #include "gt-dwarf2out.h"