Fix cygwin performance loss on linpack.
[official-gcc.git] / gcc / dwarf2out.c
blobf184750091051d970818fc64615d8913be418227
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 "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
173 /* Maximum size (in bytes) of an artificially generated label. */
174 #define MAX_ARTIFICIAL_LABEL_BYTES 30
176 /* According to the (draft) DWARF 3 specification, the initial length
177 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
178 bytes are 0xffffffff, followed by the length stored in the next 8
179 bytes.
181 However, the SGI/MIPS ABI uses an initial length which is equal to
182 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
188 /* Round SIZE up to the nearest BOUNDARY. */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
192 /* CIE identifier. */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
201 /* A vector for a table that contains frame description
202 information for each routine. */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
208 struct GTY((for_user)) indirect_string_node {
209 const char *str;
210 unsigned int refcount;
211 enum dwarf_form form;
212 char *label;
213 unsigned int index;
216 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
218 typedef const char *compare_type;
220 static hashval_t hash (indirect_string_node *);
221 static bool equal (indirect_string_node *, const char *);
224 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
226 /* With split_debug_info, both the comp_dir and dwo_name go in the
227 main object file, rather than the dwo, similar to the force_direct
228 parameter elsewhere but with additional complications:
230 1) The string is needed in both the main object file and the dwo.
231 That is, the comp_dir and dwo_name will appear in both places.
233 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
234 DW_FORM_GNU_str_index.
236 3) GCC chooses the form to use late, depending on the size and
237 reference count.
239 Rather than forcing the all debug string handling functions and
240 callers to deal with these complications, simply use a separate,
241 special-cased string table for any attribute that should go in the
242 main object file. This limits the complexity to just the places
243 that need it. */
245 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
247 static GTY(()) int dw2_string_counter;
249 /* True if the compilation unit places functions in more than one section. */
250 static GTY(()) bool have_multiple_function_sections = false;
252 /* Whether the default text and cold text sections have been used at all. */
254 static GTY(()) bool text_section_used = false;
255 static GTY(()) bool cold_text_section_used = false;
257 /* The default cold text section. */
258 static GTY(()) section *cold_text_section;
260 /* The DIE for C++14 'auto' in a function return type. */
261 static GTY(()) dw_die_ref auto_die;
263 /* The DIE for C++14 'decltype(auto)' in a function return type. */
264 static GTY(()) dw_die_ref decltype_auto_die;
266 /* Forward declarations for functions defined in this file. */
268 static char *stripattributes (const char *);
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
272 /* Personality decl of current unit. Used only when assembler does not support
273 personality CFI. */
274 static GTY(()) rtx current_unit_personality;
276 /* Data and reference forms for relocatable data. */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION ".debug_frame"
282 #endif
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL "LFB"
286 #endif
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL "LFE"
290 #endif
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL "LPE"
294 #endif
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL "LEB"
298 #endif
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL "LSCIE"
304 #define CIE_END_LABEL "LECIE"
305 #define FDE_LABEL "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL "LASFDE"
307 #define FDE_END_LABEL "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL "LELT"
310 #define LN_PROLOG_AS_LABEL "LASLTP"
311 #define LN_PROLOG_END_LABEL "LELTP"
312 #define DIE_LABEL_PREFIX "DW"
314 /* Match the base name of a file to the base name of a compilation unit. */
316 static int
317 matches_main_base (const char *path)
319 /* Cache the last query. */
320 static const char *last_path = NULL;
321 static int last_match = 0;
322 if (path != last_path)
324 const char *base;
325 int length = base_of_path (path, &base);
326 last_path = path;
327 last_match = (length == main_input_baselength
328 && memcmp (base, main_input_basename, length) == 0);
330 return last_match;
333 #ifdef DEBUG_DEBUG_STRUCT
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337 enum debug_struct_file criterion, int generic,
338 int matches, int result)
340 /* Find the type name. */
341 tree type_decl = TYPE_STUB_DECL (type);
342 tree t = type_decl;
343 const char *name = 0;
344 if (TREE_CODE (t) == TYPE_DECL)
345 t = DECL_NAME (t);
346 if (t)
347 name = IDENTIFIER_POINTER (t);
349 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
350 criterion,
351 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352 matches ? "bas" : "hdr",
353 generic ? "gen" : "ord",
354 usage == DINFO_USAGE_DFN ? ";" :
355 usage == DINFO_USAGE_DIR_USE ? "." : "*",
356 result,
357 (void*) type_decl, name);
358 return result;
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361 dump_struct_debug (type, usage, criterion, generic, matches, result)
363 #else
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366 (result)
368 #endif
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371 of the number. Some constants have a large uniform precision, so
372 we get the precision needed for the actual value of the number. */
374 static unsigned int
375 get_full_len (const wide_int &op)
377 int prec = wi::min_precision (op, UNSIGNED);
378 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379 / HOST_BITS_PER_WIDE_INT);
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
385 enum debug_struct_file criterion;
386 tree type_decl;
387 bool generic = lang_hooks.types.generic_p (type);
389 if (generic)
390 criterion = debug_struct_generic[usage];
391 else
392 criterion = debug_struct_ordinary[usage];
394 if (criterion == DINFO_STRUCT_FILE_NONE)
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396 if (criterion == DINFO_STRUCT_FILE_ANY)
397 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
399 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
401 if (type_decl != NULL)
403 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 /* Return a pointer to a copy of the section string name S with all
414 attributes stripped off, and an asterisk prepended (for assemble_name). */
416 static inline char *
417 stripattributes (const char *s)
419 char *stripped = XNEWVEC (char, strlen (s) + 2);
420 char *p = stripped;
422 *p++ = '*';
424 while (*s && *s != ',')
425 *p++ = *s++;
427 *p = '\0';
428 return stripped;
431 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
432 switch to the data section instead, and write out a synthetic start label
433 for collect2 the first time around. */
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
438 if (eh_frame_section == 0)
440 int flags;
442 if (EH_TABLES_CAN_BE_READ_ONLY)
444 int fde_encoding;
445 int per_encoding;
446 int lsda_encoding;
448 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449 /*global=*/0);
450 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451 /*global=*/1);
452 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453 /*global=*/0);
454 flags = ((! flag_pic
455 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456 && (fde_encoding & 0x70) != DW_EH_PE_aligned
457 && (per_encoding & 0x70) != DW_EH_PE_absptr
458 && (per_encoding & 0x70) != DW_EH_PE_aligned
459 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461 ? 0 : SECTION_WRITE);
463 else
464 flags = SECTION_WRITE;
466 #ifdef EH_FRAME_SECTION_NAME
467 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469 eh_frame_section = ((flags == SECTION_WRITE)
470 ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
474 switch_to_section (eh_frame_section);
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477 /* We have no special eh_frame section. Emit special labels to guide
478 collect2. */
479 if (!back)
481 tree label = get_file_function_name ("F");
482 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483 targetm.asm_out.globalize_label (asm_out_file,
484 IDENTIFIER_POINTER (label));
485 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
487 #endif
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491 FOR_EH. */
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
496 if (for_eh)
497 switch_to_eh_frame_section (back);
498 else
500 if (!debug_frame_section)
501 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502 SECTION_DEBUG, NULL);
503 switch_to_section (debug_frame_section);
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
512 switch (cfi)
514 case DW_CFA_nop:
515 case DW_CFA_GNU_window_save:
516 case DW_CFA_remember_state:
517 case DW_CFA_restore_state:
518 return dw_cfi_oprnd_unused;
520 case DW_CFA_set_loc:
521 case DW_CFA_advance_loc1:
522 case DW_CFA_advance_loc2:
523 case DW_CFA_advance_loc4:
524 case DW_CFA_MIPS_advance_loc8:
525 return dw_cfi_oprnd_addr;
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended:
529 case DW_CFA_def_cfa:
530 case DW_CFA_offset_extended_sf:
531 case DW_CFA_def_cfa_sf:
532 case DW_CFA_restore:
533 case DW_CFA_restore_extended:
534 case DW_CFA_undefined:
535 case DW_CFA_same_value:
536 case DW_CFA_def_cfa_register:
537 case DW_CFA_register:
538 case DW_CFA_expression:
539 return dw_cfi_oprnd_reg_num;
541 case DW_CFA_def_cfa_offset:
542 case DW_CFA_GNU_args_size:
543 case DW_CFA_def_cfa_offset_sf:
544 return dw_cfi_oprnd_offset;
546 case DW_CFA_def_cfa_expression:
547 return dw_cfi_oprnd_loc;
549 default:
550 gcc_unreachable ();
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
559 switch (cfi)
561 case DW_CFA_def_cfa:
562 case DW_CFA_def_cfa_sf:
563 case DW_CFA_offset:
564 case DW_CFA_offset_extended_sf:
565 case DW_CFA_offset_extended:
566 return dw_cfi_oprnd_offset;
568 case DW_CFA_register:
569 return dw_cfi_oprnd_reg_num;
571 case DW_CFA_expression:
572 return dw_cfi_oprnd_loc;
574 default:
575 return dw_cfi_oprnd_unused;
579 /* Output one FDE. */
581 static void
582 output_fde (dw_fde_ref fde, bool for_eh, bool second,
583 char *section_start_label, int fde_encoding, char *augmentation,
584 bool any_lsda_needed, int lsda_encoding)
586 const char *begin, *end;
587 static unsigned int j;
588 char l1[20], l2[20];
590 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
591 /* empty */ 0);
592 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
593 for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
595 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
596 if (!XCOFF_DEBUGGING_INFO || for_eh)
598 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600 " indicating 64-bit DWARF extension");
601 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602 "FDE Length");
604 ASM_OUTPUT_LABEL (asm_out_file, l1);
606 if (for_eh)
607 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
608 else
609 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
610 debug_frame_section, "FDE CIE offset");
612 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
613 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
615 if (for_eh)
617 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
618 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
619 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
620 "FDE initial location");
621 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
622 end, begin, "FDE address range");
624 else
626 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
627 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
630 if (augmentation[0])
632 if (any_lsda_needed)
634 int size = size_of_encoded_value (lsda_encoding);
636 if (lsda_encoding == DW_EH_PE_aligned)
638 int offset = ( 4 /* Length */
639 + 4 /* CIE offset */
640 + 2 * size_of_encoded_value (fde_encoding)
641 + 1 /* Augmentation size */ );
642 int pad = -offset & (PTR_SIZE - 1);
644 size += pad;
645 gcc_assert (size_of_uleb128 (size) == 1);
648 dw2_asm_output_data_uleb128 (size, "Augmentation size");
650 if (fde->uses_eh_lsda)
652 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
653 fde->funcdef_number);
654 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
655 gen_rtx_SYMBOL_REF (Pmode, l1),
656 false,
657 "Language Specific Data Area");
659 else
661 if (lsda_encoding == DW_EH_PE_aligned)
662 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
663 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
664 "Language Specific Data Area (none)");
667 else
668 dw2_asm_output_data_uleb128 (0, "Augmentation size");
671 /* Loop through the Call Frame Instructions associated with this FDE. */
672 fde->dw_fde_current_label = begin;
674 size_t from, until, i;
676 from = 0;
677 until = vec_safe_length (fde->dw_fde_cfi);
679 if (fde->dw_fde_second_begin == NULL)
681 else if (!second)
682 until = fde->dw_fde_switch_cfi_index;
683 else
684 from = fde->dw_fde_switch_cfi_index;
686 for (i = from; i < until; i++)
687 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
690 /* If we are to emit a ref/link from function bodies to their frame tables,
691 do it now. This is typically performed to make sure that tables
692 associated with functions are dragged with them and not discarded in
693 garbage collecting links. We need to do this on a per function basis to
694 cope with -ffunction-sections. */
696 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
697 /* Switch to the function section, emit the ref to the tables, and
698 switch *back* into the table section. */
699 switch_to_section (function_section (fde->decl));
700 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
701 switch_to_frame_table_section (for_eh, true);
702 #endif
704 /* Pad the FDE out to an address sized boundary. */
705 ASM_OUTPUT_ALIGN (asm_out_file,
706 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
707 ASM_OUTPUT_LABEL (asm_out_file, l2);
709 j += 2;
712 /* Return true if frame description entry FDE is needed for EH. */
714 static bool
715 fde_needed_for_eh_p (dw_fde_ref fde)
717 if (flag_asynchronous_unwind_tables)
718 return true;
720 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
721 return true;
723 if (fde->uses_eh_lsda)
724 return true;
726 /* If exceptions are enabled, we have collected nothrow info. */
727 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
728 return false;
730 return true;
733 /* Output the call frame information used to record information
734 that relates to calculating the frame pointer, and records the
735 location of saved registers. */
737 static void
738 output_call_frame_info (int for_eh)
740 unsigned int i;
741 dw_fde_ref fde;
742 dw_cfi_ref cfi;
743 char l1[20], l2[20], section_start_label[20];
744 bool any_lsda_needed = false;
745 char augmentation[6];
746 int augmentation_size;
747 int fde_encoding = DW_EH_PE_absptr;
748 int per_encoding = DW_EH_PE_absptr;
749 int lsda_encoding = DW_EH_PE_absptr;
750 int return_reg;
751 rtx personality = NULL;
752 int dw_cie_version;
754 /* Don't emit a CIE if there won't be any FDEs. */
755 if (!fde_vec)
756 return;
758 /* Nothing to do if the assembler's doing it all. */
759 if (dwarf2out_do_cfi_asm ())
760 return;
762 /* If we don't have any functions we'll want to unwind out of, don't emit
763 any EH unwind information. If we make FDEs linkonce, we may have to
764 emit an empty label for an FDE that wouldn't otherwise be emitted. We
765 want to avoid having an FDE kept around when the function it refers to
766 is discarded. Example where this matters: a primary function template
767 in C++ requires EH information, an explicit specialization doesn't. */
768 if (for_eh)
770 bool any_eh_needed = false;
772 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 if (fde->uses_eh_lsda)
775 any_eh_needed = any_lsda_needed = true;
776 else if (fde_needed_for_eh_p (fde))
777 any_eh_needed = true;
778 else if (TARGET_USES_WEAK_UNWIND_INFO)
779 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 if (!any_eh_needed)
783 return;
786 /* We're going to be generating comments, so turn on app. */
787 if (flag_debug_asm)
788 app_enable ();
790 /* Switch to the proper frame section, first time. */
791 switch_to_frame_table_section (for_eh, false);
793 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796 /* Output the CIE. */
797 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802 dw2_asm_output_data (4, 0xffffffff,
803 "Initial length escape value indicating 64-bit DWARF extension");
804 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[20];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 /* Describe an entry into the .debug_addr section. */
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1265 struct GTY((for_user)) addr_table_entry {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1274 GTY ((desc ("%1.kind"))) addr;
1277 /* Location lists are ranges + location descriptions for that range,
1278 so you can track variables that are in different places over
1279 their entire life. */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281 dw_loc_list_ref dw_loc_next;
1282 const char *begin; /* Label and addr_entry for start of range */
1283 addr_table_entry *begin_entry;
1284 const char *end; /* Label for end of range */
1285 char *ll_symbol; /* Label for beginning of location list.
1286 Only on head of list */
1287 const char *section; /* Section this loclist is relative to */
1288 dw_loc_descr_ref expr;
1289 hashval_t hash;
1290 /* True if all addresses in this and subsequent lists are known to be
1291 resolved. */
1292 bool resolved_addr;
1293 /* True if this list has been replaced by dw_loc_next. */
1294 bool replaced;
1295 bool emitted;
1296 /* True if the range should be emitted even if begin and end
1297 are the same. */
1298 bool force;
1299 } dw_loc_list_node;
1301 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1303 /* Convert a DWARF stack opcode into its string name. */
1305 static const char *
1306 dwarf_stack_op_name (unsigned int op)
1308 const char *name = get_DW_OP_name (op);
1310 if (name != NULL)
1311 return name;
1313 return "OP_<unknown>";
1316 /* Return a pointer to a newly allocated location description. Location
1317 descriptions are simple expression terms that can be strung
1318 together to form more complicated location (address) descriptions. */
1320 static inline dw_loc_descr_ref
1321 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1322 unsigned HOST_WIDE_INT oprnd2)
1324 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1326 descr->dw_loc_opc = op;
1327 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1328 descr->dw_loc_oprnd1.val_entry = NULL;
1329 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1330 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1331 descr->dw_loc_oprnd2.val_entry = NULL;
1332 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1334 return descr;
1337 /* Return a pointer to a newly allocated location description for
1338 REG and OFFSET. */
1340 static inline dw_loc_descr_ref
1341 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1343 if (reg <= 31)
1344 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1345 offset, 0);
1346 else
1347 return new_loc_descr (DW_OP_bregx, reg, offset);
1350 /* Add a location description term to a location description expression. */
1352 static inline void
1353 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1355 dw_loc_descr_ref *d;
1357 /* Find the end of the chain. */
1358 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1361 *d = descr;
1364 /* Compare two location operands for exact equality. */
1366 static bool
1367 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1369 if (a->val_class != b->val_class)
1370 return false;
1371 switch (a->val_class)
1373 case dw_val_class_none:
1374 return true;
1375 case dw_val_class_addr:
1376 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1378 case dw_val_class_offset:
1379 case dw_val_class_unsigned_const:
1380 case dw_val_class_const:
1381 case dw_val_class_range_list:
1382 case dw_val_class_lineptr:
1383 case dw_val_class_macptr:
1384 /* These are all HOST_WIDE_INT, signed or unsigned. */
1385 return a->v.val_unsigned == b->v.val_unsigned;
1387 case dw_val_class_loc:
1388 return a->v.val_loc == b->v.val_loc;
1389 case dw_val_class_loc_list:
1390 return a->v.val_loc_list == b->v.val_loc_list;
1391 case dw_val_class_die_ref:
1392 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1393 case dw_val_class_fde_ref:
1394 return a->v.val_fde_index == b->v.val_fde_index;
1395 case dw_val_class_lbl_id:
1396 case dw_val_class_high_pc:
1397 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1398 case dw_val_class_str:
1399 return a->v.val_str == b->v.val_str;
1400 case dw_val_class_flag:
1401 return a->v.val_flag == b->v.val_flag;
1402 case dw_val_class_file:
1403 return a->v.val_file == b->v.val_file;
1404 case dw_val_class_decl_ref:
1405 return a->v.val_decl_ref == b->v.val_decl_ref;
1407 case dw_val_class_const_double:
1408 return (a->v.val_double.high == b->v.val_double.high
1409 && a->v.val_double.low == b->v.val_double.low);
1411 case dw_val_class_wide_int:
1412 return *a->v.val_wide == *b->v.val_wide;
1414 case dw_val_class_vec:
1416 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1417 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1419 return (a_len == b_len
1420 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1423 case dw_val_class_data8:
1424 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1426 case dw_val_class_vms_delta:
1427 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1428 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1430 gcc_unreachable ();
1433 /* Compare two location atoms for exact equality. */
1435 static bool
1436 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1438 if (a->dw_loc_opc != b->dw_loc_opc)
1439 return false;
1441 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1442 address size, but since we always allocate cleared storage it
1443 should be zero for other types of locations. */
1444 if (a->dtprel != b->dtprel)
1445 return false;
1447 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1448 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1451 /* Compare two complete location expressions for exact equality. */
1453 bool
1454 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1456 while (1)
1458 if (a == b)
1459 return true;
1460 if (a == NULL || b == NULL)
1461 return false;
1462 if (!loc_descr_equal_p_1 (a, b))
1463 return false;
1465 a = a->dw_loc_next;
1466 b = b->dw_loc_next;
1471 /* Add a constant OFFSET to a location expression. */
1473 static void
1474 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1476 dw_loc_descr_ref loc;
1477 HOST_WIDE_INT *p;
1479 gcc_assert (*list_head != NULL);
1481 if (!offset)
1482 return;
1484 /* Find the end of the chain. */
1485 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1488 p = NULL;
1489 if (loc->dw_loc_opc == DW_OP_fbreg
1490 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1491 p = &loc->dw_loc_oprnd1.v.val_int;
1492 else if (loc->dw_loc_opc == DW_OP_bregx)
1493 p = &loc->dw_loc_oprnd2.v.val_int;
1495 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1496 offset. Don't optimize if an signed integer overflow would happen. */
1497 if (p != NULL
1498 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1499 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1500 *p += offset;
1502 else if (offset > 0)
1503 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1505 else
1507 loc->dw_loc_next = int_loc_descriptor (-offset);
1508 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1512 /* Add a constant OFFSET to a location list. */
1514 static void
1515 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1517 dw_loc_list_ref d;
1518 for (d = list_head; d != NULL; d = d->dw_loc_next)
1519 loc_descr_plus_const (&d->expr, offset);
1522 #define DWARF_REF_SIZE \
1523 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1525 static unsigned long int get_base_type_offset (dw_die_ref);
1527 /* Return the size of a location descriptor. */
1529 static unsigned long
1530 size_of_loc_descr (dw_loc_descr_ref loc)
1532 unsigned long size = 1;
1534 switch (loc->dw_loc_opc)
1536 case DW_OP_addr:
1537 size += DWARF2_ADDR_SIZE;
1538 break;
1539 case DW_OP_GNU_addr_index:
1540 case DW_OP_GNU_const_index:
1541 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1542 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1543 break;
1544 case DW_OP_const1u:
1545 case DW_OP_const1s:
1546 size += 1;
1547 break;
1548 case DW_OP_const2u:
1549 case DW_OP_const2s:
1550 size += 2;
1551 break;
1552 case DW_OP_const4u:
1553 case DW_OP_const4s:
1554 size += 4;
1555 break;
1556 case DW_OP_const8u:
1557 case DW_OP_const8s:
1558 size += 8;
1559 break;
1560 case DW_OP_constu:
1561 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1562 break;
1563 case DW_OP_consts:
1564 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1565 break;
1566 case DW_OP_pick:
1567 size += 1;
1568 break;
1569 case DW_OP_plus_uconst:
1570 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1571 break;
1572 case DW_OP_skip:
1573 case DW_OP_bra:
1574 size += 2;
1575 break;
1576 case DW_OP_breg0:
1577 case DW_OP_breg1:
1578 case DW_OP_breg2:
1579 case DW_OP_breg3:
1580 case DW_OP_breg4:
1581 case DW_OP_breg5:
1582 case DW_OP_breg6:
1583 case DW_OP_breg7:
1584 case DW_OP_breg8:
1585 case DW_OP_breg9:
1586 case DW_OP_breg10:
1587 case DW_OP_breg11:
1588 case DW_OP_breg12:
1589 case DW_OP_breg13:
1590 case DW_OP_breg14:
1591 case DW_OP_breg15:
1592 case DW_OP_breg16:
1593 case DW_OP_breg17:
1594 case DW_OP_breg18:
1595 case DW_OP_breg19:
1596 case DW_OP_breg20:
1597 case DW_OP_breg21:
1598 case DW_OP_breg22:
1599 case DW_OP_breg23:
1600 case DW_OP_breg24:
1601 case DW_OP_breg25:
1602 case DW_OP_breg26:
1603 case DW_OP_breg27:
1604 case DW_OP_breg28:
1605 case DW_OP_breg29:
1606 case DW_OP_breg30:
1607 case DW_OP_breg31:
1608 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1609 break;
1610 case DW_OP_regx:
1611 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1612 break;
1613 case DW_OP_fbreg:
1614 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1615 break;
1616 case DW_OP_bregx:
1617 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1618 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1619 break;
1620 case DW_OP_piece:
1621 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1622 break;
1623 case DW_OP_bit_piece:
1624 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1625 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1626 break;
1627 case DW_OP_deref_size:
1628 case DW_OP_xderef_size:
1629 size += 1;
1630 break;
1631 case DW_OP_call2:
1632 size += 2;
1633 break;
1634 case DW_OP_call4:
1635 size += 4;
1636 break;
1637 case DW_OP_call_ref:
1638 size += DWARF_REF_SIZE;
1639 break;
1640 case DW_OP_implicit_value:
1641 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1642 + loc->dw_loc_oprnd1.v.val_unsigned;
1643 break;
1644 case DW_OP_GNU_implicit_pointer:
1645 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1646 break;
1647 case DW_OP_GNU_entry_value:
1649 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1650 size += size_of_uleb128 (op_size) + op_size;
1651 break;
1653 case DW_OP_GNU_const_type:
1655 unsigned long o
1656 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1657 size += size_of_uleb128 (o) + 1;
1658 switch (loc->dw_loc_oprnd2.val_class)
1660 case dw_val_class_vec:
1661 size += loc->dw_loc_oprnd2.v.val_vec.length
1662 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1663 break;
1664 case dw_val_class_const:
1665 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1666 break;
1667 case dw_val_class_const_double:
1668 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1669 break;
1670 case dw_val_class_wide_int:
1671 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1672 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1673 break;
1674 default:
1675 gcc_unreachable ();
1677 break;
1679 case DW_OP_GNU_regval_type:
1681 unsigned long o
1682 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1683 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1684 + size_of_uleb128 (o);
1686 break;
1687 case DW_OP_GNU_deref_type:
1689 unsigned long o
1690 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1691 size += 1 + size_of_uleb128 (o);
1693 break;
1694 case DW_OP_GNU_convert:
1695 case DW_OP_GNU_reinterpret:
1696 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1697 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1698 else
1700 unsigned long o
1701 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1702 size += size_of_uleb128 (o);
1704 break;
1705 case DW_OP_GNU_parameter_ref:
1706 size += 4;
1707 break;
1708 default:
1709 break;
1712 return size;
1715 /* Return the size of a series of location descriptors. */
1717 unsigned long
1718 size_of_locs (dw_loc_descr_ref loc)
1720 dw_loc_descr_ref l;
1721 unsigned long size;
1723 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1724 field, to avoid writing to a PCH file. */
1725 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1727 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1728 break;
1729 size += size_of_loc_descr (l);
1731 if (! l)
1732 return size;
1734 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1736 l->dw_loc_addr = size;
1737 size += size_of_loc_descr (l);
1740 return size;
1743 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1744 static void get_ref_die_offset_label (char *, dw_die_ref);
1745 static unsigned long int get_ref_die_offset (dw_die_ref);
1747 /* Output location description stack opcode's operands (if any).
1748 The for_eh_or_skip parameter controls whether register numbers are
1749 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1750 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1751 info). This should be suppressed for the cases that have not been converted
1752 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1754 static void
1755 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1757 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1758 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1760 switch (loc->dw_loc_opc)
1762 #ifdef DWARF2_DEBUGGING_INFO
1763 case DW_OP_const2u:
1764 case DW_OP_const2s:
1765 dw2_asm_output_data (2, val1->v.val_int, NULL);
1766 break;
1767 case DW_OP_const4u:
1768 if (loc->dtprel)
1770 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1771 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1772 val1->v.val_addr);
1773 fputc ('\n', asm_out_file);
1774 break;
1776 /* FALLTHRU */
1777 case DW_OP_const4s:
1778 dw2_asm_output_data (4, val1->v.val_int, NULL);
1779 break;
1780 case DW_OP_const8u:
1781 if (loc->dtprel)
1783 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1784 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1785 val1->v.val_addr);
1786 fputc ('\n', asm_out_file);
1787 break;
1789 /* FALLTHRU */
1790 case DW_OP_const8s:
1791 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1792 dw2_asm_output_data (8, val1->v.val_int, NULL);
1793 break;
1794 case DW_OP_skip:
1795 case DW_OP_bra:
1797 int offset;
1799 gcc_assert (val1->val_class == dw_val_class_loc);
1800 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1802 dw2_asm_output_data (2, offset, NULL);
1804 break;
1805 case DW_OP_implicit_value:
1806 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1807 switch (val2->val_class)
1809 case dw_val_class_const:
1810 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1811 break;
1812 case dw_val_class_vec:
1814 unsigned int elt_size = val2->v.val_vec.elt_size;
1815 unsigned int len = val2->v.val_vec.length;
1816 unsigned int i;
1817 unsigned char *p;
1819 if (elt_size > sizeof (HOST_WIDE_INT))
1821 elt_size /= 2;
1822 len *= 2;
1824 for (i = 0, p = val2->v.val_vec.array;
1825 i < len;
1826 i++, p += elt_size)
1827 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1828 "fp or vector constant word %u", i);
1830 break;
1831 case dw_val_class_const_double:
1833 unsigned HOST_WIDE_INT first, second;
1835 if (WORDS_BIG_ENDIAN)
1837 first = val2->v.val_double.high;
1838 second = val2->v.val_double.low;
1840 else
1842 first = val2->v.val_double.low;
1843 second = val2->v.val_double.high;
1845 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1846 first, NULL);
1847 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1848 second, NULL);
1850 break;
1851 case dw_val_class_wide_int:
1853 int i;
1854 int len = get_full_len (*val2->v.val_wide);
1855 if (WORDS_BIG_ENDIAN)
1856 for (i = len - 1; i >= 0; --i)
1857 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1858 val2->v.val_wide->elt (i), NULL);
1859 else
1860 for (i = 0; i < len; ++i)
1861 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1862 val2->v.val_wide->elt (i), NULL);
1864 break;
1865 case dw_val_class_addr:
1866 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1867 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1868 break;
1869 default:
1870 gcc_unreachable ();
1872 break;
1873 #else
1874 case DW_OP_const2u:
1875 case DW_OP_const2s:
1876 case DW_OP_const4u:
1877 case DW_OP_const4s:
1878 case DW_OP_const8u:
1879 case DW_OP_const8s:
1880 case DW_OP_skip:
1881 case DW_OP_bra:
1882 case DW_OP_implicit_value:
1883 /* We currently don't make any attempt to make sure these are
1884 aligned properly like we do for the main unwind info, so
1885 don't support emitting things larger than a byte if we're
1886 only doing unwinding. */
1887 gcc_unreachable ();
1888 #endif
1889 case DW_OP_const1u:
1890 case DW_OP_const1s:
1891 dw2_asm_output_data (1, val1->v.val_int, NULL);
1892 break;
1893 case DW_OP_constu:
1894 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1895 break;
1896 case DW_OP_consts:
1897 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1898 break;
1899 case DW_OP_pick:
1900 dw2_asm_output_data (1, val1->v.val_int, NULL);
1901 break;
1902 case DW_OP_plus_uconst:
1903 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1904 break;
1905 case DW_OP_breg0:
1906 case DW_OP_breg1:
1907 case DW_OP_breg2:
1908 case DW_OP_breg3:
1909 case DW_OP_breg4:
1910 case DW_OP_breg5:
1911 case DW_OP_breg6:
1912 case DW_OP_breg7:
1913 case DW_OP_breg8:
1914 case DW_OP_breg9:
1915 case DW_OP_breg10:
1916 case DW_OP_breg11:
1917 case DW_OP_breg12:
1918 case DW_OP_breg13:
1919 case DW_OP_breg14:
1920 case DW_OP_breg15:
1921 case DW_OP_breg16:
1922 case DW_OP_breg17:
1923 case DW_OP_breg18:
1924 case DW_OP_breg19:
1925 case DW_OP_breg20:
1926 case DW_OP_breg21:
1927 case DW_OP_breg22:
1928 case DW_OP_breg23:
1929 case DW_OP_breg24:
1930 case DW_OP_breg25:
1931 case DW_OP_breg26:
1932 case DW_OP_breg27:
1933 case DW_OP_breg28:
1934 case DW_OP_breg29:
1935 case DW_OP_breg30:
1936 case DW_OP_breg31:
1937 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1938 break;
1939 case DW_OP_regx:
1941 unsigned r = val1->v.val_unsigned;
1942 if (for_eh_or_skip >= 0)
1943 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1944 gcc_assert (size_of_uleb128 (r)
1945 == size_of_uleb128 (val1->v.val_unsigned));
1946 dw2_asm_output_data_uleb128 (r, NULL);
1948 break;
1949 case DW_OP_fbreg:
1950 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1951 break;
1952 case DW_OP_bregx:
1954 unsigned r = val1->v.val_unsigned;
1955 if (for_eh_or_skip >= 0)
1956 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1957 gcc_assert (size_of_uleb128 (r)
1958 == size_of_uleb128 (val1->v.val_unsigned));
1959 dw2_asm_output_data_uleb128 (r, NULL);
1960 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1962 break;
1963 case DW_OP_piece:
1964 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1965 break;
1966 case DW_OP_bit_piece:
1967 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1968 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1969 break;
1970 case DW_OP_deref_size:
1971 case DW_OP_xderef_size:
1972 dw2_asm_output_data (1, val1->v.val_int, NULL);
1973 break;
1975 case DW_OP_addr:
1976 if (loc->dtprel)
1978 if (targetm.asm_out.output_dwarf_dtprel)
1980 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1981 DWARF2_ADDR_SIZE,
1982 val1->v.val_addr);
1983 fputc ('\n', asm_out_file);
1985 else
1986 gcc_unreachable ();
1988 else
1990 #ifdef DWARF2_DEBUGGING_INFO
1991 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1992 #else
1993 gcc_unreachable ();
1994 #endif
1996 break;
1998 case DW_OP_GNU_addr_index:
1999 case DW_OP_GNU_const_index:
2000 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2001 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2002 "(index into .debug_addr)");
2003 break;
2005 case DW_OP_GNU_implicit_pointer:
2007 char label[MAX_ARTIFICIAL_LABEL_BYTES
2008 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2009 gcc_assert (val1->val_class == dw_val_class_die_ref);
2010 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2011 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2012 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2014 break;
2016 case DW_OP_GNU_entry_value:
2017 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2018 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2019 break;
2021 case DW_OP_GNU_const_type:
2023 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2024 gcc_assert (o);
2025 dw2_asm_output_data_uleb128 (o, NULL);
2026 switch (val2->val_class)
2028 case dw_val_class_const:
2029 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2030 dw2_asm_output_data (1, l, NULL);
2031 dw2_asm_output_data (l, val2->v.val_int, NULL);
2032 break;
2033 case dw_val_class_vec:
2035 unsigned int elt_size = val2->v.val_vec.elt_size;
2036 unsigned int len = val2->v.val_vec.length;
2037 unsigned int i;
2038 unsigned char *p;
2040 l = len * elt_size;
2041 dw2_asm_output_data (1, l, NULL);
2042 if (elt_size > sizeof (HOST_WIDE_INT))
2044 elt_size /= 2;
2045 len *= 2;
2047 for (i = 0, p = val2->v.val_vec.array;
2048 i < len;
2049 i++, p += elt_size)
2050 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2051 "fp or vector constant word %u", i);
2053 break;
2054 case dw_val_class_const_double:
2056 unsigned HOST_WIDE_INT first, second;
2057 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2059 dw2_asm_output_data (1, 2 * l, NULL);
2060 if (WORDS_BIG_ENDIAN)
2062 first = val2->v.val_double.high;
2063 second = val2->v.val_double.low;
2065 else
2067 first = val2->v.val_double.low;
2068 second = val2->v.val_double.high;
2070 dw2_asm_output_data (l, first, NULL);
2071 dw2_asm_output_data (l, second, NULL);
2073 break;
2074 case dw_val_class_wide_int:
2076 int i;
2077 int len = get_full_len (*val2->v.val_wide);
2078 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2080 dw2_asm_output_data (1, len * l, NULL);
2081 if (WORDS_BIG_ENDIAN)
2082 for (i = len - 1; i >= 0; --i)
2083 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2084 else
2085 for (i = 0; i < len; ++i)
2086 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2088 break;
2089 default:
2090 gcc_unreachable ();
2093 break;
2094 case DW_OP_GNU_regval_type:
2096 unsigned r = val1->v.val_unsigned;
2097 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2098 gcc_assert (o);
2099 if (for_eh_or_skip >= 0)
2101 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2102 gcc_assert (size_of_uleb128 (r)
2103 == size_of_uleb128 (val1->v.val_unsigned));
2105 dw2_asm_output_data_uleb128 (r, NULL);
2106 dw2_asm_output_data_uleb128 (o, NULL);
2108 break;
2109 case DW_OP_GNU_deref_type:
2111 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2112 gcc_assert (o);
2113 dw2_asm_output_data (1, val1->v.val_int, NULL);
2114 dw2_asm_output_data_uleb128 (o, NULL);
2116 break;
2117 case DW_OP_GNU_convert:
2118 case DW_OP_GNU_reinterpret:
2119 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2120 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2121 else
2123 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2124 gcc_assert (o);
2125 dw2_asm_output_data_uleb128 (o, NULL);
2127 break;
2129 case DW_OP_GNU_parameter_ref:
2131 unsigned long o;
2132 gcc_assert (val1->val_class == dw_val_class_die_ref);
2133 o = get_ref_die_offset (val1->v.val_die_ref.die);
2134 dw2_asm_output_data (4, o, NULL);
2136 break;
2138 default:
2139 /* Other codes have no operands. */
2140 break;
2144 /* Output a sequence of location operations.
2145 The for_eh_or_skip parameter controls whether register numbers are
2146 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2147 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2148 info). This should be suppressed for the cases that have not been converted
2149 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2151 void
2152 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2154 for (; loc != NULL; loc = loc->dw_loc_next)
2156 enum dwarf_location_atom opc = loc->dw_loc_opc;
2157 /* Output the opcode. */
2158 if (for_eh_or_skip >= 0
2159 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2161 unsigned r = (opc - DW_OP_breg0);
2162 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2163 gcc_assert (r <= 31);
2164 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2166 else if (for_eh_or_skip >= 0
2167 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2169 unsigned r = (opc - DW_OP_reg0);
2170 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2171 gcc_assert (r <= 31);
2172 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2175 dw2_asm_output_data (1, opc,
2176 "%s", dwarf_stack_op_name (opc));
2178 /* Output the operand(s) (if any). */
2179 output_loc_operands (loc, for_eh_or_skip);
2183 /* Output location description stack opcode's operands (if any).
2184 The output is single bytes on a line, suitable for .cfi_escape. */
2186 static void
2187 output_loc_operands_raw (dw_loc_descr_ref loc)
2189 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2190 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2192 switch (loc->dw_loc_opc)
2194 case DW_OP_addr:
2195 case DW_OP_GNU_addr_index:
2196 case DW_OP_GNU_const_index:
2197 case DW_OP_implicit_value:
2198 /* We cannot output addresses in .cfi_escape, only bytes. */
2199 gcc_unreachable ();
2201 case DW_OP_const1u:
2202 case DW_OP_const1s:
2203 case DW_OP_pick:
2204 case DW_OP_deref_size:
2205 case DW_OP_xderef_size:
2206 fputc (',', asm_out_file);
2207 dw2_asm_output_data_raw (1, val1->v.val_int);
2208 break;
2210 case DW_OP_const2u:
2211 case DW_OP_const2s:
2212 fputc (',', asm_out_file);
2213 dw2_asm_output_data_raw (2, val1->v.val_int);
2214 break;
2216 case DW_OP_const4u:
2217 case DW_OP_const4s:
2218 fputc (',', asm_out_file);
2219 dw2_asm_output_data_raw (4, val1->v.val_int);
2220 break;
2222 case DW_OP_const8u:
2223 case DW_OP_const8s:
2224 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2225 fputc (',', asm_out_file);
2226 dw2_asm_output_data_raw (8, val1->v.val_int);
2227 break;
2229 case DW_OP_skip:
2230 case DW_OP_bra:
2232 int offset;
2234 gcc_assert (val1->val_class == dw_val_class_loc);
2235 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2237 fputc (',', asm_out_file);
2238 dw2_asm_output_data_raw (2, offset);
2240 break;
2242 case DW_OP_regx:
2244 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2245 gcc_assert (size_of_uleb128 (r)
2246 == size_of_uleb128 (val1->v.val_unsigned));
2247 fputc (',', asm_out_file);
2248 dw2_asm_output_data_uleb128_raw (r);
2250 break;
2252 case DW_OP_constu:
2253 case DW_OP_plus_uconst:
2254 case DW_OP_piece:
2255 fputc (',', asm_out_file);
2256 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2257 break;
2259 case DW_OP_bit_piece:
2260 fputc (',', asm_out_file);
2261 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2262 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2263 break;
2265 case DW_OP_consts:
2266 case DW_OP_breg0:
2267 case DW_OP_breg1:
2268 case DW_OP_breg2:
2269 case DW_OP_breg3:
2270 case DW_OP_breg4:
2271 case DW_OP_breg5:
2272 case DW_OP_breg6:
2273 case DW_OP_breg7:
2274 case DW_OP_breg8:
2275 case DW_OP_breg9:
2276 case DW_OP_breg10:
2277 case DW_OP_breg11:
2278 case DW_OP_breg12:
2279 case DW_OP_breg13:
2280 case DW_OP_breg14:
2281 case DW_OP_breg15:
2282 case DW_OP_breg16:
2283 case DW_OP_breg17:
2284 case DW_OP_breg18:
2285 case DW_OP_breg19:
2286 case DW_OP_breg20:
2287 case DW_OP_breg21:
2288 case DW_OP_breg22:
2289 case DW_OP_breg23:
2290 case DW_OP_breg24:
2291 case DW_OP_breg25:
2292 case DW_OP_breg26:
2293 case DW_OP_breg27:
2294 case DW_OP_breg28:
2295 case DW_OP_breg29:
2296 case DW_OP_breg30:
2297 case DW_OP_breg31:
2298 case DW_OP_fbreg:
2299 fputc (',', asm_out_file);
2300 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2301 break;
2303 case DW_OP_bregx:
2305 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2306 gcc_assert (size_of_uleb128 (r)
2307 == size_of_uleb128 (val1->v.val_unsigned));
2308 fputc (',', asm_out_file);
2309 dw2_asm_output_data_uleb128_raw (r);
2310 fputc (',', asm_out_file);
2311 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2313 break;
2315 case DW_OP_GNU_implicit_pointer:
2316 case DW_OP_GNU_entry_value:
2317 case DW_OP_GNU_const_type:
2318 case DW_OP_GNU_regval_type:
2319 case DW_OP_GNU_deref_type:
2320 case DW_OP_GNU_convert:
2321 case DW_OP_GNU_reinterpret:
2322 case DW_OP_GNU_parameter_ref:
2323 gcc_unreachable ();
2324 break;
2326 default:
2327 /* Other codes have no operands. */
2328 break;
2332 void
2333 output_loc_sequence_raw (dw_loc_descr_ref loc)
2335 while (1)
2337 enum dwarf_location_atom opc = loc->dw_loc_opc;
2338 /* Output the opcode. */
2339 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2341 unsigned r = (opc - DW_OP_breg0);
2342 r = DWARF2_FRAME_REG_OUT (r, 1);
2343 gcc_assert (r <= 31);
2344 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2346 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2348 unsigned r = (opc - DW_OP_reg0);
2349 r = DWARF2_FRAME_REG_OUT (r, 1);
2350 gcc_assert (r <= 31);
2351 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2353 /* Output the opcode. */
2354 fprintf (asm_out_file, "%#x", opc);
2355 output_loc_operands_raw (loc);
2357 if (!loc->dw_loc_next)
2358 break;
2359 loc = loc->dw_loc_next;
2361 fputc (',', asm_out_file);
2365 /* This function builds a dwarf location descriptor sequence from a
2366 dw_cfa_location, adding the given OFFSET to the result of the
2367 expression. */
2369 struct dw_loc_descr_node *
2370 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2372 struct dw_loc_descr_node *head, *tmp;
2374 offset += cfa->offset;
2376 if (cfa->indirect)
2378 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2379 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2380 head->dw_loc_oprnd1.val_entry = NULL;
2381 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2382 add_loc_descr (&head, tmp);
2383 if (offset != 0)
2385 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2386 add_loc_descr (&head, tmp);
2389 else
2390 head = new_reg_loc_descr (cfa->reg, offset);
2392 return head;
2395 /* This function builds a dwarf location descriptor sequence for
2396 the address at OFFSET from the CFA when stack is aligned to
2397 ALIGNMENT byte. */
2399 struct dw_loc_descr_node *
2400 build_cfa_aligned_loc (dw_cfa_location *cfa,
2401 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2403 struct dw_loc_descr_node *head;
2404 unsigned int dwarf_fp
2405 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2407 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2408 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2410 head = new_reg_loc_descr (dwarf_fp, 0);
2411 add_loc_descr (&head, int_loc_descriptor (alignment));
2412 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2413 loc_descr_plus_const (&head, offset);
2415 else
2416 head = new_reg_loc_descr (dwarf_fp, offset);
2417 return head;
2420 /* And now, the support for symbolic debugging information. */
2422 /* .debug_str support. */
2424 static void dwarf2out_init (const char *);
2425 static void dwarf2out_finish (const char *);
2426 static void dwarf2out_early_finish (void);
2427 static void dwarf2out_assembly_start (void);
2428 static void dwarf2out_define (unsigned int, const char *);
2429 static void dwarf2out_undef (unsigned int, const char *);
2430 static void dwarf2out_start_source_file (unsigned, const char *);
2431 static void dwarf2out_end_source_file (unsigned);
2432 static void dwarf2out_function_decl (tree);
2433 static void dwarf2out_begin_block (unsigned, unsigned);
2434 static void dwarf2out_end_block (unsigned, unsigned);
2435 static bool dwarf2out_ignore_block (const_tree);
2436 static void dwarf2out_early_global_decl (tree);
2437 static void dwarf2out_late_global_decl (tree);
2438 static void dwarf2out_type_decl (tree, int);
2439 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2440 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2441 dw_die_ref);
2442 static void dwarf2out_abstract_function (tree);
2443 static void dwarf2out_var_location (rtx_insn *);
2444 static void dwarf2out_begin_function (tree);
2445 static void dwarf2out_end_function (unsigned int);
2446 static void dwarf2out_register_main_translation_unit (tree unit);
2447 static void dwarf2out_set_name (tree, tree);
2449 /* The debug hooks structure. */
2451 const struct gcc_debug_hooks dwarf2_debug_hooks =
2453 dwarf2out_init,
2454 dwarf2out_finish,
2455 dwarf2out_early_finish,
2456 dwarf2out_assembly_start,
2457 dwarf2out_define,
2458 dwarf2out_undef,
2459 dwarf2out_start_source_file,
2460 dwarf2out_end_source_file,
2461 dwarf2out_begin_block,
2462 dwarf2out_end_block,
2463 dwarf2out_ignore_block,
2464 dwarf2out_source_line,
2465 dwarf2out_begin_prologue,
2466 #if VMS_DEBUGGING_INFO
2467 dwarf2out_vms_end_prologue,
2468 dwarf2out_vms_begin_epilogue,
2469 #else
2470 debug_nothing_int_charstar,
2471 debug_nothing_int_charstar,
2472 #endif
2473 dwarf2out_end_epilogue,
2474 dwarf2out_begin_function,
2475 dwarf2out_end_function, /* end_function */
2476 dwarf2out_register_main_translation_unit,
2477 dwarf2out_function_decl, /* function_decl */
2478 dwarf2out_early_global_decl,
2479 dwarf2out_late_global_decl,
2480 dwarf2out_type_decl, /* type_decl */
2481 dwarf2out_imported_module_or_decl,
2482 debug_nothing_tree, /* deferred_inline_function */
2483 /* The DWARF 2 backend tries to reduce debugging bloat by not
2484 emitting the abstract description of inline functions until
2485 something tries to reference them. */
2486 dwarf2out_abstract_function, /* outlining_inline_function */
2487 debug_nothing_rtx_code_label, /* label */
2488 debug_nothing_int, /* handle_pch */
2489 dwarf2out_var_location,
2490 dwarf2out_switch_text_section,
2491 dwarf2out_set_name,
2492 1, /* start_end_main_source_file */
2493 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2496 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2498 dwarf2out_init,
2499 debug_nothing_charstar,
2500 debug_nothing_void,
2501 debug_nothing_void,
2502 debug_nothing_int_charstar,
2503 debug_nothing_int_charstar,
2504 debug_nothing_int_charstar,
2505 debug_nothing_int,
2506 debug_nothing_int_int, /* begin_block */
2507 debug_nothing_int_int, /* end_block */
2508 debug_true_const_tree, /* ignore_block */
2509 dwarf2out_source_line, /* source_line */
2510 debug_nothing_int_charstar, /* begin_prologue */
2511 debug_nothing_int_charstar, /* end_prologue */
2512 debug_nothing_int_charstar, /* begin_epilogue */
2513 debug_nothing_int_charstar, /* end_epilogue */
2514 debug_nothing_tree, /* begin_function */
2515 debug_nothing_int, /* end_function */
2516 debug_nothing_tree, /* register_main_translation_unit */
2517 debug_nothing_tree, /* function_decl */
2518 debug_nothing_tree, /* early_global_decl */
2519 debug_nothing_tree, /* late_global_decl */
2520 debug_nothing_tree_int, /* type_decl */
2521 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2522 debug_nothing_tree, /* deferred_inline_function */
2523 debug_nothing_tree, /* outlining_inline_function */
2524 debug_nothing_rtx_code_label, /* label */
2525 debug_nothing_int, /* handle_pch */
2526 debug_nothing_rtx_insn, /* var_location */
2527 debug_nothing_void, /* switch_text_section */
2528 debug_nothing_tree_tree, /* set_name */
2529 0, /* start_end_main_source_file */
2530 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2533 /* NOTE: In the comments in this file, many references are made to
2534 "Debugging Information Entries". This term is abbreviated as `DIE'
2535 throughout the remainder of this file. */
2537 /* An internal representation of the DWARF output is built, and then
2538 walked to generate the DWARF debugging info. The walk of the internal
2539 representation is done after the entire program has been compiled.
2540 The types below are used to describe the internal representation. */
2542 /* Whether to put type DIEs into their own section .debug_types instead
2543 of making them part of the .debug_info section. Only supported for
2544 Dwarf V4 or higher and the user didn't disable them through
2545 -fno-debug-types-section. It is more efficient to put them in a
2546 separate comdat sections since the linker will then be able to
2547 remove duplicates. But not all tools support .debug_types sections
2548 yet. */
2550 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2552 /* Various DIE's use offsets relative to the beginning of the
2553 .debug_info section to refer to each other. */
2555 typedef long int dw_offset;
2557 struct comdat_type_node;
2559 /* The entries in the line_info table more-or-less mirror the opcodes
2560 that are used in the real dwarf line table. Arrays of these entries
2561 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2562 supported. */
2564 enum dw_line_info_opcode {
2565 /* Emit DW_LNE_set_address; the operand is the label index. */
2566 LI_set_address,
2568 /* Emit a row to the matrix with the given line. This may be done
2569 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2570 special opcodes. */
2571 LI_set_line,
2573 /* Emit a DW_LNS_set_file. */
2574 LI_set_file,
2576 /* Emit a DW_LNS_set_column. */
2577 LI_set_column,
2579 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2580 LI_negate_stmt,
2582 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2583 LI_set_prologue_end,
2584 LI_set_epilogue_begin,
2586 /* Emit a DW_LNE_set_discriminator. */
2587 LI_set_discriminator
2590 typedef struct GTY(()) dw_line_info_struct {
2591 enum dw_line_info_opcode opcode;
2592 unsigned int val;
2593 } dw_line_info_entry;
2596 struct GTY(()) dw_line_info_table {
2597 /* The label that marks the end of this section. */
2598 const char *end_label;
2600 /* The values for the last row of the matrix, as collected in the table.
2601 These are used to minimize the changes to the next row. */
2602 unsigned int file_num;
2603 unsigned int line_num;
2604 unsigned int column_num;
2605 int discrim_num;
2606 bool is_stmt;
2607 bool in_use;
2609 vec<dw_line_info_entry, va_gc> *entries;
2613 /* Each DIE attribute has a field specifying the attribute kind,
2614 a link to the next attribute in the chain, and an attribute value.
2615 Attributes are typically linked below the DIE they modify. */
2617 typedef struct GTY(()) dw_attr_struct {
2618 enum dwarf_attribute dw_attr;
2619 dw_val_node dw_attr_val;
2621 dw_attr_node;
2624 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2625 The children of each node form a circular list linked by
2626 die_sib. die_child points to the node *before* the "first" child node. */
2628 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2629 union die_symbol_or_type_node
2631 const char * GTY ((tag ("0"))) die_symbol;
2632 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2634 GTY ((desc ("%0.comdat_type_p"))) die_id;
2635 vec<dw_attr_node, va_gc> *die_attr;
2636 dw_die_ref die_parent;
2637 dw_die_ref die_child;
2638 dw_die_ref die_sib;
2639 dw_die_ref die_definition; /* ref from a specification to its definition */
2640 dw_offset die_offset;
2641 unsigned long die_abbrev;
2642 int die_mark;
2643 unsigned int decl_id;
2644 enum dwarf_tag die_tag;
2645 /* Die is used and must not be pruned as unused. */
2646 BOOL_BITFIELD die_perennial_p : 1;
2647 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2648 /* Lots of spare bits. */
2650 die_node;
2652 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2653 static bool early_dwarf;
2654 struct set_early_dwarf {
2655 bool saved;
2656 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2657 ~set_early_dwarf () { early_dwarf = saved; }
2660 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2661 #define FOR_EACH_CHILD(die, c, expr) do { \
2662 c = die->die_child; \
2663 if (c) do { \
2664 c = c->die_sib; \
2665 expr; \
2666 } while (c != die->die_child); \
2667 } while (0)
2669 /* The pubname structure */
2671 typedef struct GTY(()) pubname_struct {
2672 dw_die_ref die;
2673 const char *name;
2675 pubname_entry;
2678 struct GTY(()) dw_ranges {
2679 /* If this is positive, it's a block number, otherwise it's a
2680 bitwise-negated index into dw_ranges_by_label. */
2681 int num;
2684 /* A structure to hold a macinfo entry. */
2686 typedef struct GTY(()) macinfo_struct {
2687 unsigned char code;
2688 unsigned HOST_WIDE_INT lineno;
2689 const char *info;
2691 macinfo_entry;
2694 struct GTY(()) dw_ranges_by_label {
2695 const char *begin;
2696 const char *end;
2699 /* The comdat type node structure. */
2700 struct GTY(()) comdat_type_node
2702 dw_die_ref root_die;
2703 dw_die_ref type_die;
2704 dw_die_ref skeleton_die;
2705 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2706 comdat_type_node *next;
2709 /* A list of DIEs for which we can't determine ancestry (parent_die
2710 field) just yet. Later in dwarf2out_finish we will fill in the
2711 missing bits. */
2712 typedef struct GTY(()) limbo_die_struct {
2713 dw_die_ref die;
2714 /* The tree for which this DIE was created. We use this to
2715 determine ancestry later. */
2716 tree created_for;
2717 struct limbo_die_struct *next;
2719 limbo_die_node;
2721 typedef struct skeleton_chain_struct
2723 dw_die_ref old_die;
2724 dw_die_ref new_die;
2725 struct skeleton_chain_struct *parent;
2727 skeleton_chain_node;
2729 /* Define a macro which returns nonzero for a TYPE_DECL which was
2730 implicitly generated for a type.
2732 Note that, unlike the C front-end (which generates a NULL named
2733 TYPE_DECL node for each complete tagged type, each array type,
2734 and each function type node created) the C++ front-end generates
2735 a _named_ TYPE_DECL node for each tagged type node created.
2736 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2737 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2738 front-end, but for each type, tagged or not. */
2740 #define TYPE_DECL_IS_STUB(decl) \
2741 (DECL_NAME (decl) == NULL_TREE \
2742 || (DECL_ARTIFICIAL (decl) \
2743 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2744 /* This is necessary for stub decls that \
2745 appear in nested inline functions. */ \
2746 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2747 && (decl_ultimate_origin (decl) \
2748 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2750 /* Information concerning the compilation unit's programming
2751 language, and compiler version. */
2753 /* Fixed size portion of the DWARF compilation unit header. */
2754 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2755 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2757 /* Fixed size portion of the DWARF comdat type unit header. */
2758 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2759 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2760 + DWARF_OFFSET_SIZE)
2762 /* Fixed size portion of public names info. */
2763 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2765 /* Fixed size portion of the address range info. */
2766 #define DWARF_ARANGES_HEADER_SIZE \
2767 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2768 DWARF2_ADDR_SIZE * 2) \
2769 - DWARF_INITIAL_LENGTH_SIZE)
2771 /* Size of padding portion in the address range info. It must be
2772 aligned to twice the pointer size. */
2773 #define DWARF_ARANGES_PAD_SIZE \
2774 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2775 DWARF2_ADDR_SIZE * 2) \
2776 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2778 /* Use assembler line directives if available. */
2779 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2780 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2781 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2782 #else
2783 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2784 #endif
2785 #endif
2787 /* Minimum line offset in a special line info. opcode.
2788 This value was chosen to give a reasonable range of values. */
2789 #define DWARF_LINE_BASE -10
2791 /* First special line opcode - leave room for the standard opcodes. */
2792 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2794 /* Range of line offsets in a special line info. opcode. */
2795 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2797 /* Flag that indicates the initial value of the is_stmt_start flag.
2798 In the present implementation, we do not mark any lines as
2799 the beginning of a source statement, because that information
2800 is not made available by the GCC front-end. */
2801 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2803 /* Maximum number of operations per instruction bundle. */
2804 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2805 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2806 #endif
2808 /* This location is used by calc_die_sizes() to keep track
2809 the offset of each DIE within the .debug_info section. */
2810 static unsigned long next_die_offset;
2812 /* Record the root of the DIE's built for the current compilation unit. */
2813 static GTY(()) dw_die_ref single_comp_unit_die;
2815 /* A list of type DIEs that have been separated into comdat sections. */
2816 static GTY(()) comdat_type_node *comdat_type_list;
2818 /* A list of DIEs with a NULL parent waiting to be relocated. */
2819 static GTY(()) limbo_die_node *limbo_die_list;
2821 /* A list of DIEs for which we may have to generate
2822 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2823 static GTY(()) limbo_die_node *deferred_asm_name;
2825 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2827 typedef const char *compare_type;
2829 static hashval_t hash (dwarf_file_data *);
2830 static bool equal (dwarf_file_data *, const char *);
2833 /* Filenames referenced by this compilation unit. */
2834 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2836 struct decl_die_hasher : ggc_ptr_hash<die_node>
2838 typedef tree compare_type;
2840 static hashval_t hash (die_node *);
2841 static bool equal (die_node *, tree);
2843 /* A hash table of references to DIE's that describe declarations.
2844 The key is a DECL_UID() which is a unique number identifying each decl. */
2845 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2847 struct block_die_hasher : ggc_ptr_hash<die_struct>
2849 static hashval_t hash (die_struct *);
2850 static bool equal (die_struct *, die_struct *);
2853 /* A hash table of references to DIE's that describe COMMON blocks.
2854 The key is DECL_UID() ^ die_parent. */
2855 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2857 typedef struct GTY(()) die_arg_entry_struct {
2858 dw_die_ref die;
2859 tree arg;
2860 } die_arg_entry;
2863 /* Node of the variable location list. */
2864 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2865 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2866 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2867 in mode of the EXPR_LIST node and first EXPR_LIST operand
2868 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2869 location or NULL for padding. For larger bitsizes,
2870 mode is 0 and first operand is a CONCAT with bitsize
2871 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2872 NULL as second operand. */
2873 rtx GTY (()) loc;
2874 const char * GTY (()) label;
2875 struct var_loc_node * GTY (()) next;
2878 /* Variable location list. */
2879 struct GTY ((for_user)) var_loc_list_def {
2880 struct var_loc_node * GTY (()) first;
2882 /* Pointer to the last but one or last element of the
2883 chained list. If the list is empty, both first and
2884 last are NULL, if the list contains just one node
2885 or the last node certainly is not redundant, it points
2886 to the last node, otherwise points to the last but one.
2887 Do not mark it for GC because it is marked through the chain. */
2888 struct var_loc_node * GTY ((skip ("%h"))) last;
2890 /* Pointer to the last element before section switch,
2891 if NULL, either sections weren't switched or first
2892 is after section switch. */
2893 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2895 /* DECL_UID of the variable decl. */
2896 unsigned int decl_id;
2898 typedef struct var_loc_list_def var_loc_list;
2900 /* Call argument location list. */
2901 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2902 rtx GTY (()) call_arg_loc_note;
2903 const char * GTY (()) label;
2904 tree GTY (()) block;
2905 bool tail_call_p;
2906 rtx GTY (()) symbol_ref;
2907 struct call_arg_loc_node * GTY (()) next;
2911 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2913 typedef const_tree compare_type;
2915 static hashval_t hash (var_loc_list *);
2916 static bool equal (var_loc_list *, const_tree);
2919 /* Table of decl location linked lists. */
2920 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2922 /* Head and tail of call_arg_loc chain. */
2923 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2924 static struct call_arg_loc_node *call_arg_loc_last;
2926 /* Number of call sites in the current function. */
2927 static int call_site_count = -1;
2928 /* Number of tail call sites in the current function. */
2929 static int tail_call_site_count = -1;
2931 /* A cached location list. */
2932 struct GTY ((for_user)) cached_dw_loc_list_def {
2933 /* The DECL_UID of the decl that this entry describes. */
2934 unsigned int decl_id;
2936 /* The cached location list. */
2937 dw_loc_list_ref loc_list;
2939 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2941 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2944 typedef const_tree compare_type;
2946 static hashval_t hash (cached_dw_loc_list *);
2947 static bool equal (cached_dw_loc_list *, const_tree);
2950 /* Table of cached location lists. */
2951 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2953 /* A pointer to the base of a list of references to DIE's that
2954 are uniquely identified by their tag, presence/absence of
2955 children DIE's, and list of attribute/value pairs. */
2956 static GTY((length ("abbrev_die_table_allocated")))
2957 dw_die_ref *abbrev_die_table;
2959 /* Number of elements currently allocated for abbrev_die_table. */
2960 static GTY(()) unsigned abbrev_die_table_allocated;
2962 /* Number of elements in abbrev_die_table currently in use. */
2963 static GTY(()) unsigned abbrev_die_table_in_use;
2965 /* Size (in elements) of increments by which we may expand the
2966 abbrev_die_table. */
2967 #define ABBREV_DIE_TABLE_INCREMENT 256
2969 /* A global counter for generating labels for line number data. */
2970 static unsigned int line_info_label_num;
2972 /* The current table to which we should emit line number information
2973 for the current function. This will be set up at the beginning of
2974 assembly for the function. */
2975 static dw_line_info_table *cur_line_info_table;
2977 /* The two default tables of line number info. */
2978 static GTY(()) dw_line_info_table *text_section_line_info;
2979 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2981 /* The set of all non-default tables of line number info. */
2982 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
2984 /* A flag to tell pubnames/types export if there is an info section to
2985 refer to. */
2986 static bool info_section_emitted;
2988 /* A pointer to the base of a table that contains a list of publicly
2989 accessible names. */
2990 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2992 /* A pointer to the base of a table that contains a list of publicly
2993 accessible types. */
2994 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2996 /* A pointer to the base of a table that contains a list of macro
2997 defines/undefines (and file start/end markers). */
2998 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3000 /* True if .debug_macinfo or .debug_macros section is going to be
3001 emitted. */
3002 #define have_macinfo \
3003 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3004 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3005 && !macinfo_table->is_empty ())
3007 /* Array of dies for which we should generate .debug_ranges info. */
3008 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3010 /* Number of elements currently allocated for ranges_table. */
3011 static GTY(()) unsigned ranges_table_allocated;
3013 /* Number of elements in ranges_table currently in use. */
3014 static GTY(()) unsigned ranges_table_in_use;
3016 /* Array of pairs of labels referenced in ranges_table. */
3017 static GTY ((length ("ranges_by_label_allocated")))
3018 dw_ranges_by_label *ranges_by_label;
3020 /* Number of elements currently allocated for ranges_by_label. */
3021 static GTY(()) unsigned ranges_by_label_allocated;
3023 /* Number of elements in ranges_by_label currently in use. */
3024 static GTY(()) unsigned ranges_by_label_in_use;
3026 /* Size (in elements) of increments by which we may expand the
3027 ranges_table. */
3028 #define RANGES_TABLE_INCREMENT 64
3030 /* Whether we have location lists that need outputting */
3031 static GTY(()) bool have_location_lists;
3033 /* Unique label counter. */
3034 static GTY(()) unsigned int loclabel_num;
3036 /* Unique label counter for point-of-call tables. */
3037 static GTY(()) unsigned int poc_label_num;
3039 /* The last file entry emitted by maybe_emit_file(). */
3040 static GTY(()) struct dwarf_file_data * last_emitted_file;
3042 /* Number of internal labels generated by gen_internal_sym(). */
3043 static GTY(()) int label_num;
3045 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3047 /* Instances of generic types for which we need to generate debug
3048 info that describe their generic parameters and arguments. That
3049 generation needs to happen once all types are properly laid out so
3050 we do it at the end of compilation. */
3051 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3053 /* Offset from the "steady-state frame pointer" to the frame base,
3054 within the current function. */
3055 static HOST_WIDE_INT frame_pointer_fb_offset;
3056 static bool frame_pointer_fb_offset_valid;
3058 static vec<dw_die_ref> base_types;
3060 /* Flags to represent a set of attribute classes for attributes that represent
3061 a scalar value (bounds, pointers, ...). */
3062 enum dw_scalar_form
3064 dw_scalar_form_constant = 0x01,
3065 dw_scalar_form_exprloc = 0x02,
3066 dw_scalar_form_reference = 0x04
3069 /* Forward declarations for functions defined in this file. */
3071 static int is_pseudo_reg (const_rtx);
3072 static tree type_main_variant (tree);
3073 static int is_tagged_type (const_tree);
3074 static const char *dwarf_tag_name (unsigned);
3075 static const char *dwarf_attr_name (unsigned);
3076 static const char *dwarf_form_name (unsigned);
3077 static tree decl_ultimate_origin (const_tree);
3078 static tree decl_class_context (tree);
3079 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3080 static inline enum dw_val_class AT_class (dw_attr_node *);
3081 static inline unsigned int AT_index (dw_attr_node *);
3082 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3083 static inline unsigned AT_flag (dw_attr_node *);
3084 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3085 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3086 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3087 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3088 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3089 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3090 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3091 unsigned int, unsigned char *);
3092 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3093 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3094 static inline const char *AT_string (dw_attr_node *);
3095 static enum dwarf_form AT_string_form (dw_attr_node *);
3096 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3097 static void add_AT_specification (dw_die_ref, dw_die_ref);
3098 static inline dw_die_ref AT_ref (dw_attr_node *);
3099 static inline int AT_ref_external (dw_attr_node *);
3100 static inline void set_AT_ref_external (dw_attr_node *, int);
3101 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3102 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3103 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3104 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3105 dw_loc_list_ref);
3106 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3107 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3108 static void remove_addr_table_entry (addr_table_entry *);
3109 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3110 static inline rtx AT_addr (dw_attr_node *);
3111 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3112 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3113 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3114 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3115 unsigned HOST_WIDE_INT);
3116 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3117 unsigned long, bool);
3118 static inline const char *AT_lbl (dw_attr_node *);
3119 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3120 static const char *get_AT_low_pc (dw_die_ref);
3121 static const char *get_AT_hi_pc (dw_die_ref);
3122 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3123 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3124 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3125 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3126 static bool is_cxx (void);
3127 static bool is_fortran (void);
3128 static bool is_ada (void);
3129 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3130 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3131 static void add_child_die (dw_die_ref, dw_die_ref);
3132 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3133 static dw_die_ref lookup_type_die (tree);
3134 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3135 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3136 static void equate_type_number_to_die (tree, dw_die_ref);
3137 static dw_die_ref lookup_decl_die (tree);
3138 static var_loc_list *lookup_decl_loc (const_tree);
3139 static void equate_decl_number_to_die (tree, dw_die_ref);
3140 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3141 static void print_spaces (FILE *);
3142 static void print_die (dw_die_ref, FILE *);
3143 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3144 static dw_die_ref pop_compile_unit (dw_die_ref);
3145 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3146 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3147 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3148 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3149 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3150 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3151 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3152 struct md5_ctx *, int *);
3153 struct checksum_attributes;
3154 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3155 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3156 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3157 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3158 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3159 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3160 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3161 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3162 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3163 static void compute_section_prefix (dw_die_ref);
3164 static int is_type_die (dw_die_ref);
3165 static int is_comdat_die (dw_die_ref);
3166 static int is_symbol_die (dw_die_ref);
3167 static inline bool is_template_instantiation (dw_die_ref);
3168 static void assign_symbol_names (dw_die_ref);
3169 static void break_out_includes (dw_die_ref);
3170 static int is_declaration_die (dw_die_ref);
3171 static int should_move_die_to_comdat (dw_die_ref);
3172 static dw_die_ref clone_as_declaration (dw_die_ref);
3173 static dw_die_ref clone_die (dw_die_ref);
3174 static dw_die_ref clone_tree (dw_die_ref);
3175 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3176 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3177 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3178 static dw_die_ref generate_skeleton (dw_die_ref);
3179 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3180 dw_die_ref,
3181 dw_die_ref);
3182 static void break_out_comdat_types (dw_die_ref);
3183 static void copy_decls_for_unworthy_types (dw_die_ref);
3185 static void add_sibling_attributes (dw_die_ref);
3186 static void output_location_lists (dw_die_ref);
3187 static int constant_size (unsigned HOST_WIDE_INT);
3188 static unsigned long size_of_die (dw_die_ref);
3189 static void calc_die_sizes (dw_die_ref);
3190 static void calc_base_type_die_sizes (void);
3191 static void mark_dies (dw_die_ref);
3192 static void unmark_dies (dw_die_ref);
3193 static void unmark_all_dies (dw_die_ref);
3194 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3195 static unsigned long size_of_aranges (void);
3196 static enum dwarf_form value_format (dw_attr_node *);
3197 static void output_value_format (dw_attr_node *);
3198 static void output_abbrev_section (void);
3199 static void output_die_abbrevs (unsigned long, dw_die_ref);
3200 static void output_die_symbol (dw_die_ref);
3201 static void output_die (dw_die_ref);
3202 static void output_compilation_unit_header (void);
3203 static void output_comp_unit (dw_die_ref, int);
3204 static void output_comdat_type_unit (comdat_type_node *);
3205 static const char *dwarf2_name (tree, int);
3206 static void add_pubname (tree, dw_die_ref);
3207 static void add_enumerator_pubname (const char *, dw_die_ref);
3208 static void add_pubname_string (const char *, dw_die_ref);
3209 static void add_pubtype (tree, dw_die_ref);
3210 static void output_pubnames (vec<pubname_entry, va_gc> *);
3211 static void output_aranges (void);
3212 static unsigned int add_ranges_num (int);
3213 static unsigned int add_ranges (const_tree);
3214 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3215 bool *, bool);
3216 static void output_ranges (void);
3217 static dw_line_info_table *new_line_info_table (void);
3218 static void output_line_info (bool);
3219 static void output_file_names (void);
3220 static dw_die_ref base_type_die (tree);
3221 static int is_base_type (tree);
3222 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3223 static int decl_quals (const_tree);
3224 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3225 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3226 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3227 static int type_is_enum (const_tree);
3228 static unsigned int dbx_reg_number (const_rtx);
3229 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3230 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3231 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3232 enum var_init_status);
3233 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3234 enum var_init_status);
3235 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3236 enum var_init_status);
3237 static int is_based_loc (const_rtx);
3238 static bool resolve_one_addr (rtx *);
3239 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3240 enum var_init_status);
3241 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3242 enum var_init_status);
3243 struct loc_descr_context;
3244 static dw_loc_list_ref loc_list_from_tree (tree, int,
3245 const struct loc_descr_context *);
3246 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3247 const struct loc_descr_context *);
3248 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3249 static tree field_type (const_tree);
3250 static unsigned int simple_type_align_in_bits (const_tree);
3251 static unsigned int simple_decl_align_in_bits (const_tree);
3252 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3253 static HOST_WIDE_INT field_byte_offset (const_tree);
3254 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3255 dw_loc_list_ref);
3256 static void add_data_member_location_attribute (dw_die_ref, tree);
3257 static bool add_const_value_attribute (dw_die_ref, rtx);
3258 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3259 static void insert_wide_int (const wide_int &, unsigned char *, int);
3260 static void insert_float (const_rtx, unsigned char *);
3261 static rtx rtl_for_decl_location (tree);
3262 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3263 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3264 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3265 static void add_name_attribute (dw_die_ref, const char *);
3266 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3267 static void add_comp_dir_attribute (dw_die_ref);
3268 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3269 const struct loc_descr_context *);
3270 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3271 const struct loc_descr_context *);
3272 static void add_subscript_info (dw_die_ref, tree, bool);
3273 static void add_byte_size_attribute (dw_die_ref, tree);
3274 static void add_bit_offset_attribute (dw_die_ref, tree);
3275 static void add_bit_size_attribute (dw_die_ref, tree);
3276 static void add_prototyped_attribute (dw_die_ref, tree);
3277 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3278 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3279 static void add_src_coords_attributes (dw_die_ref, tree);
3280 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3281 static void push_decl_scope (tree);
3282 static void pop_decl_scope (void);
3283 static dw_die_ref scope_die_for (tree, dw_die_ref);
3284 static inline int local_scope_p (dw_die_ref);
3285 static inline int class_scope_p (dw_die_ref);
3286 static inline int class_or_namespace_scope_p (dw_die_ref);
3287 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3288 static void add_calling_convention_attribute (dw_die_ref, tree);
3289 static const char *type_tag (const_tree);
3290 static tree member_declared_type (const_tree);
3291 #if 0
3292 static const char *decl_start_label (tree);
3293 #endif
3294 static void gen_array_type_die (tree, dw_die_ref);
3295 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3296 #if 0
3297 static void gen_entry_point_die (tree, dw_die_ref);
3298 #endif
3299 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3300 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3301 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3302 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3303 static void gen_formal_types_die (tree, dw_die_ref);
3304 static void gen_subprogram_die (tree, dw_die_ref);
3305 static void gen_variable_die (tree, tree, dw_die_ref);
3306 static void gen_const_die (tree, dw_die_ref);
3307 static void gen_label_die (tree, dw_die_ref);
3308 static void gen_lexical_block_die (tree, dw_die_ref);
3309 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3310 static void gen_field_die (tree, dw_die_ref);
3311 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3312 static dw_die_ref gen_compile_unit_die (const char *);
3313 static void gen_inheritance_die (tree, tree, dw_die_ref);
3314 static void gen_member_die (tree, dw_die_ref);
3315 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3316 enum debug_info_usage);
3317 static void gen_subroutine_type_die (tree, dw_die_ref);
3318 static void gen_typedef_die (tree, dw_die_ref);
3319 static void gen_type_die (tree, dw_die_ref);
3320 static void gen_block_die (tree, dw_die_ref);
3321 static void decls_for_scope (tree, dw_die_ref);
3322 static inline int is_redundant_typedef (const_tree);
3323 static bool is_naming_typedef_decl (const_tree);
3324 static inline dw_die_ref get_context_die (tree);
3325 static void gen_namespace_die (tree, dw_die_ref);
3326 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3327 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3328 static dw_die_ref force_decl_die (tree);
3329 static dw_die_ref force_type_die (tree);
3330 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3331 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3332 static struct dwarf_file_data * lookup_filename (const char *);
3333 static void retry_incomplete_types (void);
3334 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3335 static void gen_generic_params_dies (tree);
3336 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3337 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3338 static void splice_child_die (dw_die_ref, dw_die_ref);
3339 static int file_info_cmp (const void *, const void *);
3340 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3341 const char *, const char *);
3342 static void output_loc_list (dw_loc_list_ref);
3343 static char *gen_internal_sym (const char *);
3344 static bool want_pubnames (void);
3346 static void prune_unmark_dies (dw_die_ref);
3347 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3348 static void prune_unused_types_mark (dw_die_ref, int);
3349 static void prune_unused_types_walk (dw_die_ref);
3350 static void prune_unused_types_walk_attribs (dw_die_ref);
3351 static void prune_unused_types_prune (dw_die_ref);
3352 static void prune_unused_types (void);
3353 static int maybe_emit_file (struct dwarf_file_data *fd);
3354 static inline const char *AT_vms_delta1 (dw_attr_node *);
3355 static inline const char *AT_vms_delta2 (dw_attr_node *);
3356 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3357 const char *, const char *);
3358 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3359 static void gen_remaining_tmpl_value_param_die_attribute (void);
3360 static bool generic_type_p (tree);
3361 static void schedule_generic_params_dies_gen (tree t);
3362 static void gen_scheduled_generic_parms_dies (void);
3364 static const char *comp_dir_string (void);
3366 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3368 /* enum for tracking thread-local variables whose address is really an offset
3369 relative to the TLS pointer, which will need link-time relocation, but will
3370 not need relocation by the DWARF consumer. */
3372 enum dtprel_bool
3374 dtprel_false = 0,
3375 dtprel_true = 1
3378 /* Return the operator to use for an address of a variable. For dtprel_true, we
3379 use DW_OP_const*. For regular variables, which need both link-time
3380 relocation and consumer-level relocation (e.g., to account for shared objects
3381 loaded at a random address), we use DW_OP_addr*. */
3383 static inline enum dwarf_location_atom
3384 dw_addr_op (enum dtprel_bool dtprel)
3386 if (dtprel == dtprel_true)
3387 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3388 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3389 else
3390 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3393 /* Return a pointer to a newly allocated address location description. If
3394 dwarf_split_debug_info is true, then record the address with the appropriate
3395 relocation. */
3396 static inline dw_loc_descr_ref
3397 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3399 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3401 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3402 ref->dw_loc_oprnd1.v.val_addr = addr;
3403 ref->dtprel = dtprel;
3404 if (dwarf_split_debug_info)
3405 ref->dw_loc_oprnd1.val_entry
3406 = add_addr_table_entry (addr,
3407 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3408 else
3409 ref->dw_loc_oprnd1.val_entry = NULL;
3411 return ref;
3414 /* Section names used to hold DWARF debugging information. */
3416 #ifndef DEBUG_INFO_SECTION
3417 #define DEBUG_INFO_SECTION ".debug_info"
3418 #endif
3419 #ifndef DEBUG_DWO_INFO_SECTION
3420 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3421 #endif
3422 #ifndef DEBUG_ABBREV_SECTION
3423 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3424 #endif
3425 #ifndef DEBUG_DWO_ABBREV_SECTION
3426 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3427 #endif
3428 #ifndef DEBUG_ARANGES_SECTION
3429 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3430 #endif
3431 #ifndef DEBUG_ADDR_SECTION
3432 #define DEBUG_ADDR_SECTION ".debug_addr"
3433 #endif
3434 #ifndef DEBUG_NORM_MACINFO_SECTION
3435 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3436 #endif
3437 #ifndef DEBUG_DWO_MACINFO_SECTION
3438 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3439 #endif
3440 #ifndef DEBUG_MACINFO_SECTION
3441 #define DEBUG_MACINFO_SECTION \
3442 (!dwarf_split_debug_info \
3443 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3444 #endif
3445 #ifndef DEBUG_NORM_MACRO_SECTION
3446 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3447 #endif
3448 #ifndef DEBUG_DWO_MACRO_SECTION
3449 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3450 #endif
3451 #ifndef DEBUG_MACRO_SECTION
3452 #define DEBUG_MACRO_SECTION \
3453 (!dwarf_split_debug_info \
3454 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3455 #endif
3456 #ifndef DEBUG_LINE_SECTION
3457 #define DEBUG_LINE_SECTION ".debug_line"
3458 #endif
3459 #ifndef DEBUG_DWO_LINE_SECTION
3460 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3461 #endif
3462 #ifndef DEBUG_LOC_SECTION
3463 #define DEBUG_LOC_SECTION ".debug_loc"
3464 #endif
3465 #ifndef DEBUG_DWO_LOC_SECTION
3466 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3467 #endif
3468 #ifndef DEBUG_PUBNAMES_SECTION
3469 #define DEBUG_PUBNAMES_SECTION \
3470 ((debug_generate_pub_sections == 2) \
3471 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3472 #endif
3473 #ifndef DEBUG_PUBTYPES_SECTION
3474 #define DEBUG_PUBTYPES_SECTION \
3475 ((debug_generate_pub_sections == 2) \
3476 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3477 #endif
3478 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3479 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3480 #ifndef DEBUG_STR_OFFSETS_SECTION
3481 #define DEBUG_STR_OFFSETS_SECTION \
3482 (!dwarf_split_debug_info \
3483 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3484 #endif
3485 #ifndef DEBUG_STR_DWO_SECTION
3486 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3487 #endif
3488 #ifndef DEBUG_STR_SECTION
3489 #define DEBUG_STR_SECTION ".debug_str"
3490 #endif
3491 #ifndef DEBUG_RANGES_SECTION
3492 #define DEBUG_RANGES_SECTION ".debug_ranges"
3493 #endif
3495 /* Standard ELF section names for compiled code and data. */
3496 #ifndef TEXT_SECTION_NAME
3497 #define TEXT_SECTION_NAME ".text"
3498 #endif
3500 /* Section flags for .debug_macinfo/.debug_macro section. */
3501 #define DEBUG_MACRO_SECTION_FLAGS \
3502 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3504 /* Section flags for .debug_str section. */
3505 #define DEBUG_STR_SECTION_FLAGS \
3506 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3507 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3508 : SECTION_DEBUG)
3510 /* Section flags for .debug_str.dwo section. */
3511 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3513 /* Labels we insert at beginning sections we can reference instead of
3514 the section names themselves. */
3516 #ifndef TEXT_SECTION_LABEL
3517 #define TEXT_SECTION_LABEL "Ltext"
3518 #endif
3519 #ifndef COLD_TEXT_SECTION_LABEL
3520 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3521 #endif
3522 #ifndef DEBUG_LINE_SECTION_LABEL
3523 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3524 #endif
3525 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3526 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3527 #endif
3528 #ifndef DEBUG_INFO_SECTION_LABEL
3529 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3530 #endif
3531 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3532 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3533 #endif
3534 #ifndef DEBUG_ABBREV_SECTION_LABEL
3535 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3536 #endif
3537 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3538 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3539 #endif
3540 #ifndef DEBUG_ADDR_SECTION_LABEL
3541 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3542 #endif
3543 #ifndef DEBUG_LOC_SECTION_LABEL
3544 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3545 #endif
3546 #ifndef DEBUG_RANGES_SECTION_LABEL
3547 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3548 #endif
3549 #ifndef DEBUG_MACINFO_SECTION_LABEL
3550 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3551 #endif
3552 #ifndef DEBUG_MACRO_SECTION_LABEL
3553 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3554 #endif
3555 #define SKELETON_COMP_DIE_ABBREV 1
3556 #define SKELETON_TYPE_DIE_ABBREV 2
3558 /* Definitions of defaults for formats and names of various special
3559 (artificial) labels which may be generated within this file (when the -g
3560 options is used and DWARF2_DEBUGGING_INFO is in effect.
3561 If necessary, these may be overridden from within the tm.h file, but
3562 typically, overriding these defaults is unnecessary. */
3564 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3565 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3566 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3567 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3568 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3569 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3570 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3571 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3572 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3573 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3574 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3575 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3579 #ifndef TEXT_END_LABEL
3580 #define TEXT_END_LABEL "Letext"
3581 #endif
3582 #ifndef COLD_END_LABEL
3583 #define COLD_END_LABEL "Letext_cold"
3584 #endif
3585 #ifndef BLOCK_BEGIN_LABEL
3586 #define BLOCK_BEGIN_LABEL "LBB"
3587 #endif
3588 #ifndef BLOCK_END_LABEL
3589 #define BLOCK_END_LABEL "LBE"
3590 #endif
3591 #ifndef LINE_CODE_LABEL
3592 #define LINE_CODE_LABEL "LM"
3593 #endif
3596 /* Return the root of the DIE's built for the current compilation unit. */
3597 static dw_die_ref
3598 comp_unit_die (void)
3600 if (!single_comp_unit_die)
3601 single_comp_unit_die = gen_compile_unit_die (NULL);
3602 return single_comp_unit_die;
3605 /* We allow a language front-end to designate a function that is to be
3606 called to "demangle" any name before it is put into a DIE. */
3608 static const char *(*demangle_name_func) (const char *);
3610 void
3611 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3613 demangle_name_func = func;
3616 /* Test if rtl node points to a pseudo register. */
3618 static inline int
3619 is_pseudo_reg (const_rtx rtl)
3621 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3622 || (GET_CODE (rtl) == SUBREG
3623 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3626 /* Return a reference to a type, with its const and volatile qualifiers
3627 removed. */
3629 static inline tree
3630 type_main_variant (tree type)
3632 type = TYPE_MAIN_VARIANT (type);
3634 /* ??? There really should be only one main variant among any group of
3635 variants of a given type (and all of the MAIN_VARIANT values for all
3636 members of the group should point to that one type) but sometimes the C
3637 front-end messes this up for array types, so we work around that bug
3638 here. */
3639 if (TREE_CODE (type) == ARRAY_TYPE)
3640 while (type != TYPE_MAIN_VARIANT (type))
3641 type = TYPE_MAIN_VARIANT (type);
3643 return type;
3646 /* Return nonzero if the given type node represents a tagged type. */
3648 static inline int
3649 is_tagged_type (const_tree type)
3651 enum tree_code code = TREE_CODE (type);
3653 return (code == RECORD_TYPE || code == UNION_TYPE
3654 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3657 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3659 static void
3660 get_ref_die_offset_label (char *label, dw_die_ref ref)
3662 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3665 /* Return die_offset of a DIE reference to a base type. */
3667 static unsigned long int
3668 get_base_type_offset (dw_die_ref ref)
3670 if (ref->die_offset)
3671 return ref->die_offset;
3672 if (comp_unit_die ()->die_abbrev)
3674 calc_base_type_die_sizes ();
3675 gcc_assert (ref->die_offset);
3677 return ref->die_offset;
3680 /* Return die_offset of a DIE reference other than base type. */
3682 static unsigned long int
3683 get_ref_die_offset (dw_die_ref ref)
3685 gcc_assert (ref->die_offset);
3686 return ref->die_offset;
3689 /* Convert a DIE tag into its string name. */
3691 static const char *
3692 dwarf_tag_name (unsigned int tag)
3694 const char *name = get_DW_TAG_name (tag);
3696 if (name != NULL)
3697 return name;
3699 return "DW_TAG_<unknown>";
3702 /* Convert a DWARF attribute code into its string name. */
3704 static const char *
3705 dwarf_attr_name (unsigned int attr)
3707 const char *name;
3709 switch (attr)
3711 #if VMS_DEBUGGING_INFO
3712 case DW_AT_HP_prologue:
3713 return "DW_AT_HP_prologue";
3714 #else
3715 case DW_AT_MIPS_loop_unroll_factor:
3716 return "DW_AT_MIPS_loop_unroll_factor";
3717 #endif
3719 #if VMS_DEBUGGING_INFO
3720 case DW_AT_HP_epilogue:
3721 return "DW_AT_HP_epilogue";
3722 #else
3723 case DW_AT_MIPS_stride:
3724 return "DW_AT_MIPS_stride";
3725 #endif
3728 name = get_DW_AT_name (attr);
3730 if (name != NULL)
3731 return name;
3733 return "DW_AT_<unknown>";
3736 /* Convert a DWARF value form code into its string name. */
3738 static const char *
3739 dwarf_form_name (unsigned int form)
3741 const char *name = get_DW_FORM_name (form);
3743 if (name != NULL)
3744 return name;
3746 return "DW_FORM_<unknown>";
3749 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3750 instance of an inlined instance of a decl which is local to an inline
3751 function, so we have to trace all of the way back through the origin chain
3752 to find out what sort of node actually served as the original seed for the
3753 given block. */
3755 static tree
3756 decl_ultimate_origin (const_tree decl)
3758 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3759 return NULL_TREE;
3761 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3762 we're trying to output the abstract instance of this function. */
3763 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3764 return NULL_TREE;
3766 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3767 most distant ancestor, this should never happen. */
3768 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3770 return DECL_ABSTRACT_ORIGIN (decl);
3773 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3774 of a virtual function may refer to a base class, so we check the 'this'
3775 parameter. */
3777 static tree
3778 decl_class_context (tree decl)
3780 tree context = NULL_TREE;
3782 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3783 context = DECL_CONTEXT (decl);
3784 else
3785 context = TYPE_MAIN_VARIANT
3786 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3788 if (context && !TYPE_P (context))
3789 context = NULL_TREE;
3791 return context;
3794 /* Add an attribute/value pair to a DIE. */
3796 static inline void
3797 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3799 /* Maybe this should be an assert? */
3800 if (die == NULL)
3801 return;
3803 vec_safe_reserve (die->die_attr, 1);
3804 vec_safe_push (die->die_attr, *attr);
3807 static inline enum dw_val_class
3808 AT_class (dw_attr_node *a)
3810 return a->dw_attr_val.val_class;
3813 /* Return the index for any attribute that will be referenced with a
3814 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3815 are stored in dw_attr_val.v.val_str for reference counting
3816 pruning. */
3818 static inline unsigned int
3819 AT_index (dw_attr_node *a)
3821 if (AT_class (a) == dw_val_class_str)
3822 return a->dw_attr_val.v.val_str->index;
3823 else if (a->dw_attr_val.val_entry != NULL)
3824 return a->dw_attr_val.val_entry->index;
3825 return NOT_INDEXED;
3828 /* Add a flag value attribute to a DIE. */
3830 static inline void
3831 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3833 dw_attr_node attr;
3835 attr.dw_attr = attr_kind;
3836 attr.dw_attr_val.val_class = dw_val_class_flag;
3837 attr.dw_attr_val.val_entry = NULL;
3838 attr.dw_attr_val.v.val_flag = flag;
3839 add_dwarf_attr (die, &attr);
3842 static inline unsigned
3843 AT_flag (dw_attr_node *a)
3845 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3846 return a->dw_attr_val.v.val_flag;
3849 /* Add a signed integer attribute value to a DIE. */
3851 static inline void
3852 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3854 dw_attr_node attr;
3856 attr.dw_attr = attr_kind;
3857 attr.dw_attr_val.val_class = dw_val_class_const;
3858 attr.dw_attr_val.val_entry = NULL;
3859 attr.dw_attr_val.v.val_int = int_val;
3860 add_dwarf_attr (die, &attr);
3863 static inline HOST_WIDE_INT
3864 AT_int (dw_attr_node *a)
3866 gcc_assert (a && AT_class (a) == dw_val_class_const);
3867 return a->dw_attr_val.v.val_int;
3870 /* Add an unsigned integer attribute value to a DIE. */
3872 static inline void
3873 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3874 unsigned HOST_WIDE_INT unsigned_val)
3876 dw_attr_node attr;
3878 attr.dw_attr = attr_kind;
3879 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3880 attr.dw_attr_val.val_entry = NULL;
3881 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3882 add_dwarf_attr (die, &attr);
3885 static inline unsigned HOST_WIDE_INT
3886 AT_unsigned (dw_attr_node *a)
3888 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3889 return a->dw_attr_val.v.val_unsigned;
3892 /* Add an unsigned wide integer attribute value to a DIE. */
3894 static inline void
3895 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3896 const wide_int& w)
3898 dw_attr_node attr;
3900 attr.dw_attr = attr_kind;
3901 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3902 attr.dw_attr_val.val_entry = NULL;
3903 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3904 *attr.dw_attr_val.v.val_wide = w;
3905 add_dwarf_attr (die, &attr);
3908 /* Add an unsigned double integer attribute value to a DIE. */
3910 static inline void
3911 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3912 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3914 dw_attr_node attr;
3916 attr.dw_attr = attr_kind;
3917 attr.dw_attr_val.val_class = dw_val_class_const_double;
3918 attr.dw_attr_val.val_entry = NULL;
3919 attr.dw_attr_val.v.val_double.high = high;
3920 attr.dw_attr_val.v.val_double.low = low;
3921 add_dwarf_attr (die, &attr);
3924 /* Add a floating point attribute value to a DIE and return it. */
3926 static inline void
3927 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3928 unsigned int length, unsigned int elt_size, unsigned char *array)
3930 dw_attr_node attr;
3932 attr.dw_attr = attr_kind;
3933 attr.dw_attr_val.val_class = dw_val_class_vec;
3934 attr.dw_attr_val.val_entry = NULL;
3935 attr.dw_attr_val.v.val_vec.length = length;
3936 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3937 attr.dw_attr_val.v.val_vec.array = array;
3938 add_dwarf_attr (die, &attr);
3941 /* Add an 8-byte data attribute value to a DIE. */
3943 static inline void
3944 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3945 unsigned char data8[8])
3947 dw_attr_node attr;
3949 attr.dw_attr = attr_kind;
3950 attr.dw_attr_val.val_class = dw_val_class_data8;
3951 attr.dw_attr_val.val_entry = NULL;
3952 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3953 add_dwarf_attr (die, &attr);
3956 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3957 dwarf_split_debug_info, address attributes in dies destined for the
3958 final executable have force_direct set to avoid using indexed
3959 references. */
3961 static inline void
3962 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3963 bool force_direct)
3965 dw_attr_node attr;
3966 char * lbl_id;
3968 lbl_id = xstrdup (lbl_low);
3969 attr.dw_attr = DW_AT_low_pc;
3970 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3971 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3972 if (dwarf_split_debug_info && !force_direct)
3973 attr.dw_attr_val.val_entry
3974 = add_addr_table_entry (lbl_id, ate_kind_label);
3975 else
3976 attr.dw_attr_val.val_entry = NULL;
3977 add_dwarf_attr (die, &attr);
3979 attr.dw_attr = DW_AT_high_pc;
3980 if (dwarf_version < 4)
3981 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3982 else
3983 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3984 lbl_id = xstrdup (lbl_high);
3985 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3986 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3987 && dwarf_split_debug_info && !force_direct)
3988 attr.dw_attr_val.val_entry
3989 = add_addr_table_entry (lbl_id, ate_kind_label);
3990 else
3991 attr.dw_attr_val.val_entry = NULL;
3992 add_dwarf_attr (die, &attr);
3995 /* Hash and equality functions for debug_str_hash. */
3997 hashval_t
3998 indirect_string_hasher::hash (indirect_string_node *x)
4000 return htab_hash_string (x->str);
4003 bool
4004 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4006 return strcmp (x1->str, x2) == 0;
4009 /* Add STR to the given string hash table. */
4011 static struct indirect_string_node *
4012 find_AT_string_in_table (const char *str,
4013 hash_table<indirect_string_hasher> *table)
4015 struct indirect_string_node *node;
4017 indirect_string_node **slot
4018 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4019 if (*slot == NULL)
4021 node = ggc_cleared_alloc<indirect_string_node> ();
4022 node->str = ggc_strdup (str);
4023 *slot = node;
4025 else
4026 node = *slot;
4028 node->refcount++;
4029 return node;
4032 /* Add STR to the indirect string hash table. */
4034 static struct indirect_string_node *
4035 find_AT_string (const char *str)
4037 if (! debug_str_hash)
4038 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4040 return find_AT_string_in_table (str, debug_str_hash);
4043 /* Add a string attribute value to a DIE. */
4045 static inline void
4046 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4048 dw_attr_node attr;
4049 struct indirect_string_node *node;
4051 node = find_AT_string (str);
4053 attr.dw_attr = attr_kind;
4054 attr.dw_attr_val.val_class = dw_val_class_str;
4055 attr.dw_attr_val.val_entry = NULL;
4056 attr.dw_attr_val.v.val_str = node;
4057 add_dwarf_attr (die, &attr);
4060 static inline const char *
4061 AT_string (dw_attr_node *a)
4063 gcc_assert (a && AT_class (a) == dw_val_class_str);
4064 return a->dw_attr_val.v.val_str->str;
4067 /* Call this function directly to bypass AT_string_form's logic to put
4068 the string inline in the die. */
4070 static void
4071 set_indirect_string (struct indirect_string_node *node)
4073 char label[32];
4074 /* Already indirect is a no op. */
4075 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4077 gcc_assert (node->label);
4078 return;
4080 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4081 ++dw2_string_counter;
4082 node->label = xstrdup (label);
4084 if (!dwarf_split_debug_info)
4086 node->form = DW_FORM_strp;
4087 node->index = NOT_INDEXED;
4089 else
4091 node->form = DW_FORM_GNU_str_index;
4092 node->index = NO_INDEX_ASSIGNED;
4096 /* Find out whether a string should be output inline in DIE
4097 or out-of-line in .debug_str section. */
4099 static enum dwarf_form
4100 find_string_form (struct indirect_string_node *node)
4102 unsigned int len;
4104 if (node->form)
4105 return node->form;
4107 len = strlen (node->str) + 1;
4109 /* If the string is shorter or equal to the size of the reference, it is
4110 always better to put it inline. */
4111 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4112 return node->form = DW_FORM_string;
4114 /* If we cannot expect the linker to merge strings in .debug_str
4115 section, only put it into .debug_str if it is worth even in this
4116 single module. */
4117 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4118 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4119 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4120 return node->form = DW_FORM_string;
4122 set_indirect_string (node);
4124 return node->form;
4127 /* Find out whether the string referenced from the attribute should be
4128 output inline in DIE or out-of-line in .debug_str section. */
4130 static enum dwarf_form
4131 AT_string_form (dw_attr_node *a)
4133 gcc_assert (a && AT_class (a) == dw_val_class_str);
4134 return find_string_form (a->dw_attr_val.v.val_str);
4137 /* Add a DIE reference attribute value to a DIE. */
4139 static inline void
4140 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4142 dw_attr_node attr;
4143 gcc_checking_assert (targ_die != NULL);
4145 /* With LTO we can end up trying to reference something we didn't create
4146 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4147 if (targ_die == NULL)
4148 return;
4150 attr.dw_attr = attr_kind;
4151 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4152 attr.dw_attr_val.val_entry = NULL;
4153 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4154 attr.dw_attr_val.v.val_die_ref.external = 0;
4155 add_dwarf_attr (die, &attr);
4158 /* Change DIE reference REF to point to NEW_DIE instead. */
4160 static inline void
4161 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4163 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4164 ref->dw_attr_val.v.val_die_ref.die = new_die;
4165 ref->dw_attr_val.v.val_die_ref.external = 0;
4168 /* Add an AT_specification attribute to a DIE, and also make the back
4169 pointer from the specification to the definition. */
4171 static inline void
4172 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4174 add_AT_die_ref (die, DW_AT_specification, targ_die);
4175 gcc_assert (!targ_die->die_definition);
4176 targ_die->die_definition = die;
4179 static inline dw_die_ref
4180 AT_ref (dw_attr_node *a)
4182 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4183 return a->dw_attr_val.v.val_die_ref.die;
4186 static inline int
4187 AT_ref_external (dw_attr_node *a)
4189 if (a && AT_class (a) == dw_val_class_die_ref)
4190 return a->dw_attr_val.v.val_die_ref.external;
4192 return 0;
4195 static inline void
4196 set_AT_ref_external (dw_attr_node *a, int i)
4198 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4199 a->dw_attr_val.v.val_die_ref.external = i;
4202 /* Add an FDE reference attribute value to a DIE. */
4204 static inline void
4205 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4207 dw_attr_node attr;
4209 attr.dw_attr = attr_kind;
4210 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4211 attr.dw_attr_val.val_entry = NULL;
4212 attr.dw_attr_val.v.val_fde_index = targ_fde;
4213 add_dwarf_attr (die, &attr);
4216 /* Add a location description attribute value to a DIE. */
4218 static inline void
4219 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4221 dw_attr_node attr;
4223 attr.dw_attr = attr_kind;
4224 attr.dw_attr_val.val_class = dw_val_class_loc;
4225 attr.dw_attr_val.val_entry = NULL;
4226 attr.dw_attr_val.v.val_loc = loc;
4227 add_dwarf_attr (die, &attr);
4230 static inline dw_loc_descr_ref
4231 AT_loc (dw_attr_node *a)
4233 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4234 return a->dw_attr_val.v.val_loc;
4237 static inline void
4238 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4240 dw_attr_node attr;
4242 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4243 return;
4245 attr.dw_attr = attr_kind;
4246 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4247 attr.dw_attr_val.val_entry = NULL;
4248 attr.dw_attr_val.v.val_loc_list = loc_list;
4249 add_dwarf_attr (die, &attr);
4250 have_location_lists = true;
4253 static inline dw_loc_list_ref
4254 AT_loc_list (dw_attr_node *a)
4256 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4257 return a->dw_attr_val.v.val_loc_list;
4260 static inline dw_loc_list_ref *
4261 AT_loc_list_ptr (dw_attr_node *a)
4263 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4264 return &a->dw_attr_val.v.val_loc_list;
4267 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4269 static hashval_t hash (addr_table_entry *);
4270 static bool equal (addr_table_entry *, addr_table_entry *);
4273 /* Table of entries into the .debug_addr section. */
4275 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4277 /* Hash an address_table_entry. */
4279 hashval_t
4280 addr_hasher::hash (addr_table_entry *a)
4282 inchash::hash hstate;
4283 switch (a->kind)
4285 case ate_kind_rtx:
4286 hstate.add_int (0);
4287 break;
4288 case ate_kind_rtx_dtprel:
4289 hstate.add_int (1);
4290 break;
4291 case ate_kind_label:
4292 return htab_hash_string (a->addr.label);
4293 default:
4294 gcc_unreachable ();
4296 inchash::add_rtx (a->addr.rtl, hstate);
4297 return hstate.end ();
4300 /* Determine equality for two address_table_entries. */
4302 bool
4303 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4305 if (a1->kind != a2->kind)
4306 return 0;
4307 switch (a1->kind)
4309 case ate_kind_rtx:
4310 case ate_kind_rtx_dtprel:
4311 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4312 case ate_kind_label:
4313 return strcmp (a1->addr.label, a2->addr.label) == 0;
4314 default:
4315 gcc_unreachable ();
4319 /* Initialize an addr_table_entry. */
4321 void
4322 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4324 e->kind = kind;
4325 switch (kind)
4327 case ate_kind_rtx:
4328 case ate_kind_rtx_dtprel:
4329 e->addr.rtl = (rtx) addr;
4330 break;
4331 case ate_kind_label:
4332 e->addr.label = (char *) addr;
4333 break;
4335 e->refcount = 0;
4336 e->index = NO_INDEX_ASSIGNED;
4339 /* Add attr to the address table entry to the table. Defer setting an
4340 index until output time. */
4342 static addr_table_entry *
4343 add_addr_table_entry (void *addr, enum ate_kind kind)
4345 addr_table_entry *node;
4346 addr_table_entry finder;
4348 gcc_assert (dwarf_split_debug_info);
4349 if (! addr_index_table)
4350 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4351 init_addr_table_entry (&finder, kind, addr);
4352 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4354 if (*slot == HTAB_EMPTY_ENTRY)
4356 node = ggc_cleared_alloc<addr_table_entry> ();
4357 init_addr_table_entry (node, kind, addr);
4358 *slot = node;
4360 else
4361 node = *slot;
4363 node->refcount++;
4364 return node;
4367 /* Remove an entry from the addr table by decrementing its refcount.
4368 Strictly, decrementing the refcount would be enough, but the
4369 assertion that the entry is actually in the table has found
4370 bugs. */
4372 static void
4373 remove_addr_table_entry (addr_table_entry *entry)
4375 gcc_assert (dwarf_split_debug_info && addr_index_table);
4376 /* After an index is assigned, the table is frozen. */
4377 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4378 entry->refcount--;
4381 /* Given a location list, remove all addresses it refers to from the
4382 address_table. */
4384 static void
4385 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4387 for (; descr; descr = descr->dw_loc_next)
4388 if (descr->dw_loc_oprnd1.val_entry != NULL)
4390 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4391 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4395 /* A helper function for dwarf2out_finish called through
4396 htab_traverse. Assign an addr_table_entry its index. All entries
4397 must be collected into the table when this function is called,
4398 because the indexing code relies on htab_traverse to traverse nodes
4399 in the same order for each run. */
4402 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4404 addr_table_entry *node = *h;
4406 /* Don't index unreferenced nodes. */
4407 if (node->refcount == 0)
4408 return 1;
4410 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4411 node->index = *index;
4412 *index += 1;
4414 return 1;
4417 /* Add an address constant attribute value to a DIE. When using
4418 dwarf_split_debug_info, address attributes in dies destined for the
4419 final executable should be direct references--setting the parameter
4420 force_direct ensures this behavior. */
4422 static inline void
4423 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4424 bool force_direct)
4426 dw_attr_node attr;
4428 attr.dw_attr = attr_kind;
4429 attr.dw_attr_val.val_class = dw_val_class_addr;
4430 attr.dw_attr_val.v.val_addr = addr;
4431 if (dwarf_split_debug_info && !force_direct)
4432 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4433 else
4434 attr.dw_attr_val.val_entry = NULL;
4435 add_dwarf_attr (die, &attr);
4438 /* Get the RTX from to an address DIE attribute. */
4440 static inline rtx
4441 AT_addr (dw_attr_node *a)
4443 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4444 return a->dw_attr_val.v.val_addr;
4447 /* Add a file attribute value to a DIE. */
4449 static inline void
4450 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4451 struct dwarf_file_data *fd)
4453 dw_attr_node attr;
4455 attr.dw_attr = attr_kind;
4456 attr.dw_attr_val.val_class = dw_val_class_file;
4457 attr.dw_attr_val.val_entry = NULL;
4458 attr.dw_attr_val.v.val_file = fd;
4459 add_dwarf_attr (die, &attr);
4462 /* Get the dwarf_file_data from a file DIE attribute. */
4464 static inline struct dwarf_file_data *
4465 AT_file (dw_attr_node *a)
4467 gcc_assert (a && AT_class (a) == dw_val_class_file);
4468 return a->dw_attr_val.v.val_file;
4471 /* Add a vms delta attribute value to a DIE. */
4473 static inline void
4474 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4475 const char *lbl1, const char *lbl2)
4477 dw_attr_node attr;
4479 attr.dw_attr = attr_kind;
4480 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4481 attr.dw_attr_val.val_entry = NULL;
4482 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4483 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4484 add_dwarf_attr (die, &attr);
4487 /* Add a label identifier attribute value to a DIE. */
4489 static inline void
4490 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4491 const char *lbl_id)
4493 dw_attr_node attr;
4495 attr.dw_attr = attr_kind;
4496 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4497 attr.dw_attr_val.val_entry = NULL;
4498 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4499 if (dwarf_split_debug_info)
4500 attr.dw_attr_val.val_entry
4501 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4502 ate_kind_label);
4503 add_dwarf_attr (die, &attr);
4506 /* Add a section offset attribute value to a DIE, an offset into the
4507 debug_line section. */
4509 static inline void
4510 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4511 const char *label)
4513 dw_attr_node attr;
4515 attr.dw_attr = attr_kind;
4516 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4517 attr.dw_attr_val.val_entry = NULL;
4518 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4519 add_dwarf_attr (die, &attr);
4522 /* Add a section offset attribute value to a DIE, an offset into the
4523 debug_macinfo section. */
4525 static inline void
4526 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4527 const char *label)
4529 dw_attr_node attr;
4531 attr.dw_attr = attr_kind;
4532 attr.dw_attr_val.val_class = dw_val_class_macptr;
4533 attr.dw_attr_val.val_entry = NULL;
4534 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4535 add_dwarf_attr (die, &attr);
4538 /* Add an offset attribute value to a DIE. */
4540 static inline void
4541 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4542 unsigned HOST_WIDE_INT offset)
4544 dw_attr_node attr;
4546 attr.dw_attr = attr_kind;
4547 attr.dw_attr_val.val_class = dw_val_class_offset;
4548 attr.dw_attr_val.val_entry = NULL;
4549 attr.dw_attr_val.v.val_offset = offset;
4550 add_dwarf_attr (die, &attr);
4553 /* Add a range_list attribute value to a DIE. When using
4554 dwarf_split_debug_info, address attributes in dies destined for the
4555 final executable should be direct references--setting the parameter
4556 force_direct ensures this behavior. */
4558 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4559 #define RELOCATED_OFFSET (NULL)
4561 static void
4562 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4563 long unsigned int offset, bool force_direct)
4565 dw_attr_node attr;
4567 attr.dw_attr = attr_kind;
4568 attr.dw_attr_val.val_class = dw_val_class_range_list;
4569 /* For the range_list attribute, use val_entry to store whether the
4570 offset should follow split-debug-info or normal semantics. This
4571 value is read in output_range_list_offset. */
4572 if (dwarf_split_debug_info && !force_direct)
4573 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4574 else
4575 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4576 attr.dw_attr_val.v.val_offset = offset;
4577 add_dwarf_attr (die, &attr);
4580 /* Return the start label of a delta attribute. */
4582 static inline const char *
4583 AT_vms_delta1 (dw_attr_node *a)
4585 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4586 return a->dw_attr_val.v.val_vms_delta.lbl1;
4589 /* Return the end label of a delta attribute. */
4591 static inline const char *
4592 AT_vms_delta2 (dw_attr_node *a)
4594 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4595 return a->dw_attr_val.v.val_vms_delta.lbl2;
4598 static inline const char *
4599 AT_lbl (dw_attr_node *a)
4601 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4602 || AT_class (a) == dw_val_class_lineptr
4603 || AT_class (a) == dw_val_class_macptr
4604 || AT_class (a) == dw_val_class_high_pc));
4605 return a->dw_attr_val.v.val_lbl_id;
4608 /* Get the attribute of type attr_kind. */
4610 static dw_attr_node *
4611 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4613 dw_attr_node *a;
4614 unsigned ix;
4615 dw_die_ref spec = NULL;
4617 if (! die)
4618 return NULL;
4620 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4621 if (a->dw_attr == attr_kind)
4622 return a;
4623 else if (a->dw_attr == DW_AT_specification
4624 || a->dw_attr == DW_AT_abstract_origin)
4625 spec = AT_ref (a);
4627 if (spec)
4628 return get_AT (spec, attr_kind);
4630 return NULL;
4633 /* Returns the parent of the declaration of DIE. */
4635 static dw_die_ref
4636 get_die_parent (dw_die_ref die)
4638 dw_die_ref t;
4640 if (!die)
4641 return NULL;
4643 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4644 || (t = get_AT_ref (die, DW_AT_specification)))
4645 die = t;
4647 return die->die_parent;
4650 /* Return the "low pc" attribute value, typically associated with a subprogram
4651 DIE. Return null if the "low pc" attribute is either not present, or if it
4652 cannot be represented as an assembler label identifier. */
4654 static inline const char *
4655 get_AT_low_pc (dw_die_ref die)
4657 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4659 return a ? AT_lbl (a) : NULL;
4662 /* Return the "high pc" attribute value, typically associated with a subprogram
4663 DIE. Return null if the "high pc" attribute is either not present, or if it
4664 cannot be represented as an assembler label identifier. */
4666 static inline const char *
4667 get_AT_hi_pc (dw_die_ref die)
4669 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4671 return a ? AT_lbl (a) : NULL;
4674 /* Return the value of the string attribute designated by ATTR_KIND, or
4675 NULL if it is not present. */
4677 static inline const char *
4678 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4680 dw_attr_node *a = get_AT (die, attr_kind);
4682 return a ? AT_string (a) : NULL;
4685 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4686 if it is not present. */
4688 static inline int
4689 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4691 dw_attr_node *a = get_AT (die, attr_kind);
4693 return a ? AT_flag (a) : 0;
4696 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4697 if it is not present. */
4699 static inline unsigned
4700 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4702 dw_attr_node *a = get_AT (die, attr_kind);
4704 return a ? AT_unsigned (a) : 0;
4707 static inline dw_die_ref
4708 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4710 dw_attr_node *a = get_AT (die, attr_kind);
4712 return a ? AT_ref (a) : NULL;
4715 static inline struct dwarf_file_data *
4716 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4718 dw_attr_node *a = get_AT (die, attr_kind);
4720 return a ? AT_file (a) : NULL;
4723 /* Return TRUE if the language is C++. */
4725 static inline bool
4726 is_cxx (void)
4728 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4730 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4731 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4734 /* Return TRUE if the language is Java. */
4736 static inline bool
4737 is_java (void)
4739 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4741 return lang == DW_LANG_Java;
4744 /* Return TRUE if the language is Fortran. */
4746 static inline bool
4747 is_fortran (void)
4749 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4751 return (lang == DW_LANG_Fortran77
4752 || lang == DW_LANG_Fortran90
4753 || lang == DW_LANG_Fortran95
4754 || lang == DW_LANG_Fortran03
4755 || lang == DW_LANG_Fortran08);
4758 /* Return TRUE if the language is Ada. */
4760 static inline bool
4761 is_ada (void)
4763 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4765 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4768 /* Remove the specified attribute if present. Return TRUE if removal
4769 was successful. */
4771 static bool
4772 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4774 dw_attr_node *a;
4775 unsigned ix;
4777 if (! die)
4778 return false;
4780 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4781 if (a->dw_attr == attr_kind)
4783 if (AT_class (a) == dw_val_class_str)
4784 if (a->dw_attr_val.v.val_str->refcount)
4785 a->dw_attr_val.v.val_str->refcount--;
4787 /* vec::ordered_remove should help reduce the number of abbrevs
4788 that are needed. */
4789 die->die_attr->ordered_remove (ix);
4790 return true;
4792 return false;
4795 /* Remove CHILD from its parent. PREV must have the property that
4796 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4798 static void
4799 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4801 gcc_assert (child->die_parent == prev->die_parent);
4802 gcc_assert (prev->die_sib == child);
4803 if (prev == child)
4805 gcc_assert (child->die_parent->die_child == child);
4806 prev = NULL;
4808 else
4809 prev->die_sib = child->die_sib;
4810 if (child->die_parent->die_child == child)
4811 child->die_parent->die_child = prev;
4814 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4815 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4817 static void
4818 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4820 dw_die_ref parent = old_child->die_parent;
4822 gcc_assert (parent == prev->die_parent);
4823 gcc_assert (prev->die_sib == old_child);
4825 new_child->die_parent = parent;
4826 if (prev == old_child)
4828 gcc_assert (parent->die_child == old_child);
4829 new_child->die_sib = new_child;
4831 else
4833 prev->die_sib = new_child;
4834 new_child->die_sib = old_child->die_sib;
4836 if (old_child->die_parent->die_child == old_child)
4837 old_child->die_parent->die_child = new_child;
4840 /* Move all children from OLD_PARENT to NEW_PARENT. */
4842 static void
4843 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4845 dw_die_ref c;
4846 new_parent->die_child = old_parent->die_child;
4847 old_parent->die_child = NULL;
4848 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4851 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4852 matches TAG. */
4854 static void
4855 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4857 dw_die_ref c;
4859 c = die->die_child;
4860 if (c) do {
4861 dw_die_ref prev = c;
4862 c = c->die_sib;
4863 while (c->die_tag == tag)
4865 remove_child_with_prev (c, prev);
4866 c->die_parent = NULL;
4867 /* Might have removed every child. */
4868 if (c == c->die_sib)
4869 return;
4870 c = c->die_sib;
4872 } while (c != die->die_child);
4875 /* Add a CHILD_DIE as the last child of DIE. */
4877 static void
4878 add_child_die (dw_die_ref die, dw_die_ref child_die)
4880 /* FIXME this should probably be an assert. */
4881 if (! die || ! child_die)
4882 return;
4883 gcc_assert (die != child_die);
4885 child_die->die_parent = die;
4886 if (die->die_child)
4888 child_die->die_sib = die->die_child->die_sib;
4889 die->die_child->die_sib = child_die;
4891 else
4892 child_die->die_sib = child_die;
4893 die->die_child = child_die;
4896 /* Unassociate CHILD from its parent, and make its parent be
4897 NEW_PARENT. */
4899 static void
4900 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4902 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4903 if (p->die_sib == child)
4905 remove_child_with_prev (child, p);
4906 break;
4908 add_child_die (new_parent, child);
4911 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4912 is the specification, to the end of PARENT's list of children.
4913 This is done by removing and re-adding it. */
4915 static void
4916 splice_child_die (dw_die_ref parent, dw_die_ref child)
4918 /* We want the declaration DIE from inside the class, not the
4919 specification DIE at toplevel. */
4920 if (child->die_parent != parent)
4922 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4924 if (tmp)
4925 child = tmp;
4928 gcc_assert (child->die_parent == parent
4929 || (child->die_parent
4930 == get_AT_ref (parent, DW_AT_specification)));
4932 reparent_child (child, parent);
4935 /* Create and return a new die with a parent of PARENT_DIE. If
4936 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4937 associated tree T must be supplied to determine parenthood
4938 later. */
4940 static inline dw_die_ref
4941 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4943 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4945 die->die_tag = tag_value;
4947 if (parent_die != NULL)
4948 add_child_die (parent_die, die);
4949 else
4951 limbo_die_node *limbo_node;
4953 /* No DIEs created after early dwarf should end up in limbo,
4954 because the limbo list should not persist past LTO
4955 streaming. */
4956 if (tag_value != DW_TAG_compile_unit
4957 /* These are allowed because they're generated while
4958 breaking out COMDAT units late. */
4959 && tag_value != DW_TAG_type_unit
4960 && !early_dwarf
4961 /* Allow nested functions to live in limbo because they will
4962 only temporarily live there, as decls_for_scope will fix
4963 them up. */
4964 && (TREE_CODE (t) != FUNCTION_DECL
4965 || !decl_function_context (t))
4966 /* Same as nested functions above but for types. Types that
4967 are local to a function will be fixed in
4968 decls_for_scope. */
4969 && (!RECORD_OR_UNION_TYPE_P (t)
4970 || !TYPE_CONTEXT (t)
4971 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4972 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4973 especially in the ltrans stage, but once we implement LTO
4974 dwarf streaming, we should remove this exception. */
4975 && !in_lto_p)
4977 fprintf (stderr, "symbol ended up in limbo too late:");
4978 debug_generic_stmt (t);
4979 gcc_unreachable ();
4982 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4983 limbo_node->die = die;
4984 limbo_node->created_for = t;
4985 limbo_node->next = limbo_die_list;
4986 limbo_die_list = limbo_node;
4989 return die;
4992 /* Return the DIE associated with the given type specifier. */
4994 static inline dw_die_ref
4995 lookup_type_die (tree type)
4997 return TYPE_SYMTAB_DIE (type);
5000 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5001 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5002 anonymous type instead the one of the naming typedef. */
5004 static inline dw_die_ref
5005 strip_naming_typedef (tree type, dw_die_ref type_die)
5007 if (type
5008 && TREE_CODE (type) == RECORD_TYPE
5009 && type_die
5010 && type_die->die_tag == DW_TAG_typedef
5011 && is_naming_typedef_decl (TYPE_NAME (type)))
5012 type_die = get_AT_ref (type_die, DW_AT_type);
5013 return type_die;
5016 /* Like lookup_type_die, but if type is an anonymous type named by a
5017 typedef[1], return the DIE of the anonymous type instead the one of
5018 the naming typedef. This is because in gen_typedef_die, we did
5019 equate the anonymous struct named by the typedef with the DIE of
5020 the naming typedef. So by default, lookup_type_die on an anonymous
5021 struct yields the DIE of the naming typedef.
5023 [1]: Read the comment of is_naming_typedef_decl to learn about what
5024 a naming typedef is. */
5026 static inline dw_die_ref
5027 lookup_type_die_strip_naming_typedef (tree type)
5029 dw_die_ref die = lookup_type_die (type);
5030 return strip_naming_typedef (type, die);
5033 /* Equate a DIE to a given type specifier. */
5035 static inline void
5036 equate_type_number_to_die (tree type, dw_die_ref type_die)
5038 TYPE_SYMTAB_DIE (type) = type_die;
5041 /* Returns a hash value for X (which really is a die_struct). */
5043 inline hashval_t
5044 decl_die_hasher::hash (die_node *x)
5046 return (hashval_t) x->decl_id;
5049 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5051 inline bool
5052 decl_die_hasher::equal (die_node *x, tree y)
5054 return (x->decl_id == DECL_UID (y));
5057 /* Return the DIE associated with a given declaration. */
5059 static inline dw_die_ref
5060 lookup_decl_die (tree decl)
5062 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5065 /* Returns a hash value for X (which really is a var_loc_list). */
5067 inline hashval_t
5068 decl_loc_hasher::hash (var_loc_list *x)
5070 return (hashval_t) x->decl_id;
5073 /* Return nonzero if decl_id of var_loc_list X is the same as
5074 UID of decl *Y. */
5076 inline bool
5077 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5079 return (x->decl_id == DECL_UID (y));
5082 /* Return the var_loc list associated with a given declaration. */
5084 static inline var_loc_list *
5085 lookup_decl_loc (const_tree decl)
5087 if (!decl_loc_table)
5088 return NULL;
5089 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5092 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5094 inline hashval_t
5095 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5097 return (hashval_t) x->decl_id;
5100 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5101 UID of decl *Y. */
5103 inline bool
5104 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5106 return (x->decl_id == DECL_UID (y));
5109 /* Equate a DIE to a particular declaration. */
5111 static void
5112 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5114 unsigned int decl_id = DECL_UID (decl);
5116 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5117 decl_die->decl_id = decl_id;
5120 /* Return how many bits covers PIECE EXPR_LIST. */
5122 static HOST_WIDE_INT
5123 decl_piece_bitsize (rtx piece)
5125 int ret = (int) GET_MODE (piece);
5126 if (ret)
5127 return ret;
5128 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5129 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5130 return INTVAL (XEXP (XEXP (piece, 0), 0));
5133 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5135 static rtx *
5136 decl_piece_varloc_ptr (rtx piece)
5138 if ((int) GET_MODE (piece))
5139 return &XEXP (piece, 0);
5140 else
5141 return &XEXP (XEXP (piece, 0), 1);
5144 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5145 Next is the chain of following piece nodes. */
5147 static rtx_expr_list *
5148 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5150 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5151 return alloc_EXPR_LIST (bitsize, loc_note, next);
5152 else
5153 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5154 GEN_INT (bitsize),
5155 loc_note), next);
5158 /* Return rtx that should be stored into loc field for
5159 LOC_NOTE and BITPOS/BITSIZE. */
5161 static rtx
5162 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5163 HOST_WIDE_INT bitsize)
5165 if (bitsize != -1)
5167 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5168 if (bitpos != 0)
5169 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5171 return loc_note;
5174 /* This function either modifies location piece list *DEST in
5175 place (if SRC and INNER is NULL), or copies location piece list
5176 *SRC to *DEST while modifying it. Location BITPOS is modified
5177 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5178 not copied and if needed some padding around it is added.
5179 When modifying in place, DEST should point to EXPR_LIST where
5180 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5181 to the start of the whole list and INNER points to the EXPR_LIST
5182 where earlier pieces cover PIECE_BITPOS bits. */
5184 static void
5185 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5186 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5187 HOST_WIDE_INT bitsize, rtx loc_note)
5189 HOST_WIDE_INT diff;
5190 bool copy = inner != NULL;
5192 if (copy)
5194 /* First copy all nodes preceding the current bitpos. */
5195 while (src != inner)
5197 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5198 decl_piece_bitsize (*src), NULL_RTX);
5199 dest = &XEXP (*dest, 1);
5200 src = &XEXP (*src, 1);
5203 /* Add padding if needed. */
5204 if (bitpos != piece_bitpos)
5206 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5207 copy ? NULL_RTX : *dest);
5208 dest = &XEXP (*dest, 1);
5210 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5212 gcc_assert (!copy);
5213 /* A piece with correct bitpos and bitsize already exist,
5214 just update the location for it and return. */
5215 *decl_piece_varloc_ptr (*dest) = loc_note;
5216 return;
5218 /* Add the piece that changed. */
5219 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5220 dest = &XEXP (*dest, 1);
5221 /* Skip over pieces that overlap it. */
5222 diff = bitpos - piece_bitpos + bitsize;
5223 if (!copy)
5224 src = dest;
5225 while (diff > 0 && *src)
5227 rtx piece = *src;
5228 diff -= decl_piece_bitsize (piece);
5229 if (copy)
5230 src = &XEXP (piece, 1);
5231 else
5233 *src = XEXP (piece, 1);
5234 free_EXPR_LIST_node (piece);
5237 /* Add padding if needed. */
5238 if (diff < 0 && *src)
5240 if (!copy)
5241 dest = src;
5242 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5243 dest = &XEXP (*dest, 1);
5245 if (!copy)
5246 return;
5247 /* Finally copy all nodes following it. */
5248 while (*src)
5250 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5251 decl_piece_bitsize (*src), NULL_RTX);
5252 dest = &XEXP (*dest, 1);
5253 src = &XEXP (*src, 1);
5257 /* Add a variable location node to the linked list for DECL. */
5259 static struct var_loc_node *
5260 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5262 unsigned int decl_id;
5263 var_loc_list *temp;
5264 struct var_loc_node *loc = NULL;
5265 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5267 if (TREE_CODE (decl) == VAR_DECL
5268 && DECL_HAS_DEBUG_EXPR_P (decl))
5270 tree realdecl = DECL_DEBUG_EXPR (decl);
5271 if (handled_component_p (realdecl)
5272 || (TREE_CODE (realdecl) == MEM_REF
5273 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5275 HOST_WIDE_INT maxsize;
5276 bool reverse;
5277 tree innerdecl
5278 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5279 &reverse);
5280 if (!DECL_P (innerdecl)
5281 || DECL_IGNORED_P (innerdecl)
5282 || TREE_STATIC (innerdecl)
5283 || bitsize <= 0
5284 || bitpos + bitsize > 256
5285 || bitsize != maxsize)
5286 return NULL;
5287 decl = innerdecl;
5291 decl_id = DECL_UID (decl);
5292 var_loc_list **slot
5293 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5294 if (*slot == NULL)
5296 temp = ggc_cleared_alloc<var_loc_list> ();
5297 temp->decl_id = decl_id;
5298 *slot = temp;
5300 else
5301 temp = *slot;
5303 /* For PARM_DECLs try to keep around the original incoming value,
5304 even if that means we'll emit a zero-range .debug_loc entry. */
5305 if (temp->last
5306 && temp->first == temp->last
5307 && TREE_CODE (decl) == PARM_DECL
5308 && NOTE_P (temp->first->loc)
5309 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5310 && DECL_INCOMING_RTL (decl)
5311 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5312 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5313 == GET_CODE (DECL_INCOMING_RTL (decl))
5314 && prev_real_insn (temp->first->loc) == NULL_RTX
5315 && (bitsize != -1
5316 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5317 NOTE_VAR_LOCATION_LOC (loc_note))
5318 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5319 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5321 loc = ggc_cleared_alloc<var_loc_node> ();
5322 temp->first->next = loc;
5323 temp->last = loc;
5324 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5326 else if (temp->last)
5328 struct var_loc_node *last = temp->last, *unused = NULL;
5329 rtx *piece_loc = NULL, last_loc_note;
5330 HOST_WIDE_INT piece_bitpos = 0;
5331 if (last->next)
5333 last = last->next;
5334 gcc_assert (last->next == NULL);
5336 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5338 piece_loc = &last->loc;
5341 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5342 if (piece_bitpos + cur_bitsize > bitpos)
5343 break;
5344 piece_bitpos += cur_bitsize;
5345 piece_loc = &XEXP (*piece_loc, 1);
5347 while (*piece_loc);
5349 /* TEMP->LAST here is either pointer to the last but one or
5350 last element in the chained list, LAST is pointer to the
5351 last element. */
5352 if (label && strcmp (last->label, label) == 0)
5354 /* For SRA optimized variables if there weren't any real
5355 insns since last note, just modify the last node. */
5356 if (piece_loc != NULL)
5358 adjust_piece_list (piece_loc, NULL, NULL,
5359 bitpos, piece_bitpos, bitsize, loc_note);
5360 return NULL;
5362 /* If the last note doesn't cover any instructions, remove it. */
5363 if (temp->last != last)
5365 temp->last->next = NULL;
5366 unused = last;
5367 last = temp->last;
5368 gcc_assert (strcmp (last->label, label) != 0);
5370 else
5372 gcc_assert (temp->first == temp->last
5373 || (temp->first->next == temp->last
5374 && TREE_CODE (decl) == PARM_DECL));
5375 memset (temp->last, '\0', sizeof (*temp->last));
5376 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5377 return temp->last;
5380 if (bitsize == -1 && NOTE_P (last->loc))
5381 last_loc_note = last->loc;
5382 else if (piece_loc != NULL
5383 && *piece_loc != NULL_RTX
5384 && piece_bitpos == bitpos
5385 && decl_piece_bitsize (*piece_loc) == bitsize)
5386 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5387 else
5388 last_loc_note = NULL_RTX;
5389 /* If the current location is the same as the end of the list,
5390 and either both or neither of the locations is uninitialized,
5391 we have nothing to do. */
5392 if (last_loc_note == NULL_RTX
5393 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5394 NOTE_VAR_LOCATION_LOC (loc_note)))
5395 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5396 != NOTE_VAR_LOCATION_STATUS (loc_note))
5397 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5398 == VAR_INIT_STATUS_UNINITIALIZED)
5399 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5400 == VAR_INIT_STATUS_UNINITIALIZED))))
5402 /* Add LOC to the end of list and update LAST. If the last
5403 element of the list has been removed above, reuse its
5404 memory for the new node, otherwise allocate a new one. */
5405 if (unused)
5407 loc = unused;
5408 memset (loc, '\0', sizeof (*loc));
5410 else
5411 loc = ggc_cleared_alloc<var_loc_node> ();
5412 if (bitsize == -1 || piece_loc == NULL)
5413 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5414 else
5415 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5416 bitpos, piece_bitpos, bitsize, loc_note);
5417 last->next = loc;
5418 /* Ensure TEMP->LAST will point either to the new last but one
5419 element of the chain, or to the last element in it. */
5420 if (last != temp->last)
5421 temp->last = last;
5423 else if (unused)
5424 ggc_free (unused);
5426 else
5428 loc = ggc_cleared_alloc<var_loc_node> ();
5429 temp->first = loc;
5430 temp->last = loc;
5431 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5433 return loc;
5436 /* Keep track of the number of spaces used to indent the
5437 output of the debugging routines that print the structure of
5438 the DIE internal representation. */
5439 static int print_indent;
5441 /* Indent the line the number of spaces given by print_indent. */
5443 static inline void
5444 print_spaces (FILE *outfile)
5446 fprintf (outfile, "%*s", print_indent, "");
5449 /* Print a type signature in hex. */
5451 static inline void
5452 print_signature (FILE *outfile, char *sig)
5454 int i;
5456 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5457 fprintf (outfile, "%02x", sig[i] & 0xff);
5460 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5462 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5463 RECURSE, output location descriptor operations. */
5465 static void
5466 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5468 switch (val->val_class)
5470 case dw_val_class_addr:
5471 fprintf (outfile, "address");
5472 break;
5473 case dw_val_class_offset:
5474 fprintf (outfile, "offset");
5475 break;
5476 case dw_val_class_loc:
5477 fprintf (outfile, "location descriptor");
5478 if (val->v.val_loc == NULL)
5479 fprintf (outfile, " -> <null>\n");
5480 else if (recurse)
5482 fprintf (outfile, ":\n");
5483 print_indent += 4;
5484 print_loc_descr (val->v.val_loc, outfile);
5485 print_indent -= 4;
5487 else
5488 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5489 break;
5490 case dw_val_class_loc_list:
5491 fprintf (outfile, "location list -> label:%s",
5492 val->v.val_loc_list->ll_symbol);
5493 break;
5494 case dw_val_class_range_list:
5495 fprintf (outfile, "range list");
5496 break;
5497 case dw_val_class_const:
5498 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5499 break;
5500 case dw_val_class_unsigned_const:
5501 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5502 break;
5503 case dw_val_class_const_double:
5504 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5505 HOST_WIDE_INT_PRINT_UNSIGNED")",
5506 val->v.val_double.high,
5507 val->v.val_double.low);
5508 break;
5509 case dw_val_class_wide_int:
5511 int i = val->v.val_wide->get_len ();
5512 fprintf (outfile, "constant (");
5513 gcc_assert (i > 0);
5514 if (val->v.val_wide->elt (i - 1) == 0)
5515 fprintf (outfile, "0x");
5516 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5517 val->v.val_wide->elt (--i));
5518 while (--i >= 0)
5519 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5520 val->v.val_wide->elt (i));
5521 fprintf (outfile, ")");
5522 break;
5524 case dw_val_class_vec:
5525 fprintf (outfile, "floating-point or vector constant");
5526 break;
5527 case dw_val_class_flag:
5528 fprintf (outfile, "%u", val->v.val_flag);
5529 break;
5530 case dw_val_class_die_ref:
5531 if (val->v.val_die_ref.die != NULL)
5533 dw_die_ref die = val->v.val_die_ref.die;
5535 if (die->comdat_type_p)
5537 fprintf (outfile, "die -> signature: ");
5538 print_signature (outfile,
5539 die->die_id.die_type_node->signature);
5541 else if (die->die_id.die_symbol)
5542 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5543 else
5544 fprintf (outfile, "die -> %ld", die->die_offset);
5545 fprintf (outfile, " (%p)", (void *) die);
5547 else
5548 fprintf (outfile, "die -> <null>");
5549 break;
5550 case dw_val_class_vms_delta:
5551 fprintf (outfile, "delta: @slotcount(%s-%s)",
5552 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5553 break;
5554 case dw_val_class_lbl_id:
5555 case dw_val_class_lineptr:
5556 case dw_val_class_macptr:
5557 case dw_val_class_high_pc:
5558 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5559 break;
5560 case dw_val_class_str:
5561 if (val->v.val_str->str != NULL)
5562 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5563 else
5564 fprintf (outfile, "<null>");
5565 break;
5566 case dw_val_class_file:
5567 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5568 val->v.val_file->emitted_number);
5569 break;
5570 case dw_val_class_data8:
5572 int i;
5574 for (i = 0; i < 8; i++)
5575 fprintf (outfile, "%02x", val->v.val_data8[i]);
5576 break;
5578 default:
5579 break;
5583 /* Likewise, for a DIE attribute. */
5585 static void
5586 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5588 print_dw_val (&a->dw_attr_val, recurse, outfile);
5592 /* Print the list of operands in the LOC location description to OUTFILE. This
5593 routine is a debugging aid only. */
5595 static void
5596 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5598 dw_loc_descr_ref l = loc;
5600 if (loc == NULL)
5602 print_spaces (outfile);
5603 fprintf (outfile, "<null>\n");
5604 return;
5607 for (l = loc; l != NULL; l = l->dw_loc_next)
5609 print_spaces (outfile);
5610 fprintf (outfile, "(%p) %s",
5611 (void *) l,
5612 dwarf_stack_op_name (l->dw_loc_opc));
5613 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5615 fprintf (outfile, " ");
5616 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5618 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5620 fprintf (outfile, ", ");
5621 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5623 fprintf (outfile, "\n");
5627 /* Print the information associated with a given DIE, and its children.
5628 This routine is a debugging aid only. */
5630 static void
5631 print_die (dw_die_ref die, FILE *outfile)
5633 dw_attr_node *a;
5634 dw_die_ref c;
5635 unsigned ix;
5637 print_spaces (outfile);
5638 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5639 die->die_offset, dwarf_tag_name (die->die_tag),
5640 (void*) die);
5641 print_spaces (outfile);
5642 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5643 fprintf (outfile, " offset: %ld", die->die_offset);
5644 fprintf (outfile, " mark: %d\n", die->die_mark);
5646 if (die->comdat_type_p)
5648 print_spaces (outfile);
5649 fprintf (outfile, " signature: ");
5650 print_signature (outfile, die->die_id.die_type_node->signature);
5651 fprintf (outfile, "\n");
5654 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5656 print_spaces (outfile);
5657 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5659 print_attribute (a, true, outfile);
5660 fprintf (outfile, "\n");
5663 if (die->die_child != NULL)
5665 print_indent += 4;
5666 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5667 print_indent -= 4;
5669 if (print_indent == 0)
5670 fprintf (outfile, "\n");
5673 /* Print the list of operations in the LOC location description. */
5675 DEBUG_FUNCTION void
5676 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5678 print_loc_descr (loc, stderr);
5681 /* Print the information collected for a given DIE. */
5683 DEBUG_FUNCTION void
5684 debug_dwarf_die (dw_die_ref die)
5686 print_die (die, stderr);
5689 DEBUG_FUNCTION void
5690 debug (die_struct &ref)
5692 print_die (&ref, stderr);
5695 DEBUG_FUNCTION void
5696 debug (die_struct *ptr)
5698 if (ptr)
5699 debug (*ptr);
5700 else
5701 fprintf (stderr, "<nil>\n");
5705 /* Print all DWARF information collected for the compilation unit.
5706 This routine is a debugging aid only. */
5708 DEBUG_FUNCTION void
5709 debug_dwarf (void)
5711 print_indent = 0;
5712 print_die (comp_unit_die (), stderr);
5715 /* Sanity checks on DIEs. */
5717 static void
5718 check_die (dw_die_ref die)
5720 unsigned ix;
5721 dw_attr_node *a;
5722 bool inline_found = false;
5723 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5724 int n_decl_line = 0, n_decl_file = 0;
5725 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5727 switch (a->dw_attr)
5729 case DW_AT_inline:
5730 if (a->dw_attr_val.v.val_unsigned)
5731 inline_found = true;
5732 break;
5733 case DW_AT_location:
5734 ++n_location;
5735 break;
5736 case DW_AT_low_pc:
5737 ++n_low_pc;
5738 break;
5739 case DW_AT_high_pc:
5740 ++n_high_pc;
5741 break;
5742 case DW_AT_artificial:
5743 ++n_artificial;
5744 break;
5745 case DW_AT_decl_line:
5746 ++n_decl_line;
5747 break;
5748 case DW_AT_decl_file:
5749 ++n_decl_file;
5750 break;
5751 default:
5752 break;
5755 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5756 || n_decl_line > 1 || n_decl_file > 1)
5758 fprintf (stderr, "Duplicate attributes in DIE:\n");
5759 debug_dwarf_die (die);
5760 gcc_unreachable ();
5762 if (inline_found)
5764 /* A debugging information entry that is a member of an abstract
5765 instance tree [that has DW_AT_inline] should not contain any
5766 attributes which describe aspects of the subroutine which vary
5767 between distinct inlined expansions or distinct out-of-line
5768 expansions. */
5769 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5770 gcc_assert (a->dw_attr != DW_AT_low_pc
5771 && a->dw_attr != DW_AT_high_pc
5772 && a->dw_attr != DW_AT_location
5773 && a->dw_attr != DW_AT_frame_base
5774 && a->dw_attr != DW_AT_GNU_all_call_sites);
5778 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5779 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5780 DIE that marks the start of the DIEs for this include file. */
5782 static dw_die_ref
5783 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5785 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5786 dw_die_ref new_unit = gen_compile_unit_die (filename);
5788 new_unit->die_sib = old_unit;
5789 return new_unit;
5792 /* Close an include-file CU and reopen the enclosing one. */
5794 static dw_die_ref
5795 pop_compile_unit (dw_die_ref old_unit)
5797 dw_die_ref new_unit = old_unit->die_sib;
5799 old_unit->die_sib = NULL;
5800 return new_unit;
5803 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5804 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5805 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5807 /* Calculate the checksum of a location expression. */
5809 static inline void
5810 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5812 int tem;
5813 inchash::hash hstate;
5814 hashval_t hash;
5816 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5817 CHECKSUM (tem);
5818 hash_loc_operands (loc, hstate);
5819 hash = hstate.end();
5820 CHECKSUM (hash);
5823 /* Calculate the checksum of an attribute. */
5825 static void
5826 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5828 dw_loc_descr_ref loc;
5829 rtx r;
5831 CHECKSUM (at->dw_attr);
5833 /* We don't care that this was compiled with a different compiler
5834 snapshot; if the output is the same, that's what matters. */
5835 if (at->dw_attr == DW_AT_producer)
5836 return;
5838 switch (AT_class (at))
5840 case dw_val_class_const:
5841 CHECKSUM (at->dw_attr_val.v.val_int);
5842 break;
5843 case dw_val_class_unsigned_const:
5844 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5845 break;
5846 case dw_val_class_const_double:
5847 CHECKSUM (at->dw_attr_val.v.val_double);
5848 break;
5849 case dw_val_class_wide_int:
5850 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5851 get_full_len (*at->dw_attr_val.v.val_wide)
5852 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5853 break;
5854 case dw_val_class_vec:
5855 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5856 (at->dw_attr_val.v.val_vec.length
5857 * at->dw_attr_val.v.val_vec.elt_size));
5858 break;
5859 case dw_val_class_flag:
5860 CHECKSUM (at->dw_attr_val.v.val_flag);
5861 break;
5862 case dw_val_class_str:
5863 CHECKSUM_STRING (AT_string (at));
5864 break;
5866 case dw_val_class_addr:
5867 r = AT_addr (at);
5868 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5869 CHECKSUM_STRING (XSTR (r, 0));
5870 break;
5872 case dw_val_class_offset:
5873 CHECKSUM (at->dw_attr_val.v.val_offset);
5874 break;
5876 case dw_val_class_loc:
5877 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5878 loc_checksum (loc, ctx);
5879 break;
5881 case dw_val_class_die_ref:
5882 die_checksum (AT_ref (at), ctx, mark);
5883 break;
5885 case dw_val_class_fde_ref:
5886 case dw_val_class_vms_delta:
5887 case dw_val_class_lbl_id:
5888 case dw_val_class_lineptr:
5889 case dw_val_class_macptr:
5890 case dw_val_class_high_pc:
5891 break;
5893 case dw_val_class_file:
5894 CHECKSUM_STRING (AT_file (at)->filename);
5895 break;
5897 case dw_val_class_data8:
5898 CHECKSUM (at->dw_attr_val.v.val_data8);
5899 break;
5901 default:
5902 break;
5906 /* Calculate the checksum of a DIE. */
5908 static void
5909 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5911 dw_die_ref c;
5912 dw_attr_node *a;
5913 unsigned ix;
5915 /* To avoid infinite recursion. */
5916 if (die->die_mark)
5918 CHECKSUM (die->die_mark);
5919 return;
5921 die->die_mark = ++(*mark);
5923 CHECKSUM (die->die_tag);
5925 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5926 attr_checksum (a, ctx, mark);
5928 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5931 #undef CHECKSUM
5932 #undef CHECKSUM_BLOCK
5933 #undef CHECKSUM_STRING
5935 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5936 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5937 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5938 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5939 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5940 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5941 #define CHECKSUM_ATTR(FOO) \
5942 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5944 /* Calculate the checksum of a number in signed LEB128 format. */
5946 static void
5947 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5949 unsigned char byte;
5950 bool more;
5952 while (1)
5954 byte = (value & 0x7f);
5955 value >>= 7;
5956 more = !((value == 0 && (byte & 0x40) == 0)
5957 || (value == -1 && (byte & 0x40) != 0));
5958 if (more)
5959 byte |= 0x80;
5960 CHECKSUM (byte);
5961 if (!more)
5962 break;
5966 /* Calculate the checksum of a number in unsigned LEB128 format. */
5968 static void
5969 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5971 while (1)
5973 unsigned char byte = (value & 0x7f);
5974 value >>= 7;
5975 if (value != 0)
5976 /* More bytes to follow. */
5977 byte |= 0x80;
5978 CHECKSUM (byte);
5979 if (value == 0)
5980 break;
5984 /* Checksum the context of the DIE. This adds the names of any
5985 surrounding namespaces or structures to the checksum. */
5987 static void
5988 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5990 const char *name;
5991 dw_die_ref spec;
5992 int tag = die->die_tag;
5994 if (tag != DW_TAG_namespace
5995 && tag != DW_TAG_structure_type
5996 && tag != DW_TAG_class_type)
5997 return;
5999 name = get_AT_string (die, DW_AT_name);
6001 spec = get_AT_ref (die, DW_AT_specification);
6002 if (spec != NULL)
6003 die = spec;
6005 if (die->die_parent != NULL)
6006 checksum_die_context (die->die_parent, ctx);
6008 CHECKSUM_ULEB128 ('C');
6009 CHECKSUM_ULEB128 (tag);
6010 if (name != NULL)
6011 CHECKSUM_STRING (name);
6014 /* Calculate the checksum of a location expression. */
6016 static inline void
6017 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6019 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6020 were emitted as a DW_FORM_sdata instead of a location expression. */
6021 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6023 CHECKSUM_ULEB128 (DW_FORM_sdata);
6024 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6025 return;
6028 /* Otherwise, just checksum the raw location expression. */
6029 while (loc != NULL)
6031 inchash::hash hstate;
6032 hashval_t hash;
6034 CHECKSUM_ULEB128 (loc->dtprel);
6035 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6036 hash_loc_operands (loc, hstate);
6037 hash = hstate.end ();
6038 CHECKSUM (hash);
6039 loc = loc->dw_loc_next;
6043 /* Calculate the checksum of an attribute. */
6045 static void
6046 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6047 struct md5_ctx *ctx, int *mark)
6049 dw_loc_descr_ref loc;
6050 rtx r;
6052 if (AT_class (at) == dw_val_class_die_ref)
6054 dw_die_ref target_die = AT_ref (at);
6056 /* For pointer and reference types, we checksum only the (qualified)
6057 name of the target type (if there is a name). For friend entries,
6058 we checksum only the (qualified) name of the target type or function.
6059 This allows the checksum to remain the same whether the target type
6060 is complete or not. */
6061 if ((at->dw_attr == DW_AT_type
6062 && (tag == DW_TAG_pointer_type
6063 || tag == DW_TAG_reference_type
6064 || tag == DW_TAG_rvalue_reference_type
6065 || tag == DW_TAG_ptr_to_member_type))
6066 || (at->dw_attr == DW_AT_friend
6067 && tag == DW_TAG_friend))
6069 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6071 if (name_attr != NULL)
6073 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6075 if (decl == NULL)
6076 decl = target_die;
6077 CHECKSUM_ULEB128 ('N');
6078 CHECKSUM_ULEB128 (at->dw_attr);
6079 if (decl->die_parent != NULL)
6080 checksum_die_context (decl->die_parent, ctx);
6081 CHECKSUM_ULEB128 ('E');
6082 CHECKSUM_STRING (AT_string (name_attr));
6083 return;
6087 /* For all other references to another DIE, we check to see if the
6088 target DIE has already been visited. If it has, we emit a
6089 backward reference; if not, we descend recursively. */
6090 if (target_die->die_mark > 0)
6092 CHECKSUM_ULEB128 ('R');
6093 CHECKSUM_ULEB128 (at->dw_attr);
6094 CHECKSUM_ULEB128 (target_die->die_mark);
6096 else
6098 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6100 if (decl == NULL)
6101 decl = target_die;
6102 target_die->die_mark = ++(*mark);
6103 CHECKSUM_ULEB128 ('T');
6104 CHECKSUM_ULEB128 (at->dw_attr);
6105 if (decl->die_parent != NULL)
6106 checksum_die_context (decl->die_parent, ctx);
6107 die_checksum_ordered (target_die, ctx, mark);
6109 return;
6112 CHECKSUM_ULEB128 ('A');
6113 CHECKSUM_ULEB128 (at->dw_attr);
6115 switch (AT_class (at))
6117 case dw_val_class_const:
6118 CHECKSUM_ULEB128 (DW_FORM_sdata);
6119 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6120 break;
6122 case dw_val_class_unsigned_const:
6123 CHECKSUM_ULEB128 (DW_FORM_sdata);
6124 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6125 break;
6127 case dw_val_class_const_double:
6128 CHECKSUM_ULEB128 (DW_FORM_block);
6129 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6130 CHECKSUM (at->dw_attr_val.v.val_double);
6131 break;
6133 case dw_val_class_wide_int:
6134 CHECKSUM_ULEB128 (DW_FORM_block);
6135 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6136 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6137 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6138 get_full_len (*at->dw_attr_val.v.val_wide)
6139 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6140 break;
6142 case dw_val_class_vec:
6143 CHECKSUM_ULEB128 (DW_FORM_block);
6144 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6145 * at->dw_attr_val.v.val_vec.elt_size);
6146 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6147 (at->dw_attr_val.v.val_vec.length
6148 * at->dw_attr_val.v.val_vec.elt_size));
6149 break;
6151 case dw_val_class_flag:
6152 CHECKSUM_ULEB128 (DW_FORM_flag);
6153 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6154 break;
6156 case dw_val_class_str:
6157 CHECKSUM_ULEB128 (DW_FORM_string);
6158 CHECKSUM_STRING (AT_string (at));
6159 break;
6161 case dw_val_class_addr:
6162 r = AT_addr (at);
6163 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6164 CHECKSUM_ULEB128 (DW_FORM_string);
6165 CHECKSUM_STRING (XSTR (r, 0));
6166 break;
6168 case dw_val_class_offset:
6169 CHECKSUM_ULEB128 (DW_FORM_sdata);
6170 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6171 break;
6173 case dw_val_class_loc:
6174 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6175 loc_checksum_ordered (loc, ctx);
6176 break;
6178 case dw_val_class_fde_ref:
6179 case dw_val_class_lbl_id:
6180 case dw_val_class_lineptr:
6181 case dw_val_class_macptr:
6182 case dw_val_class_high_pc:
6183 break;
6185 case dw_val_class_file:
6186 CHECKSUM_ULEB128 (DW_FORM_string);
6187 CHECKSUM_STRING (AT_file (at)->filename);
6188 break;
6190 case dw_val_class_data8:
6191 CHECKSUM (at->dw_attr_val.v.val_data8);
6192 break;
6194 default:
6195 break;
6199 struct checksum_attributes
6201 dw_attr_node *at_name;
6202 dw_attr_node *at_type;
6203 dw_attr_node *at_friend;
6204 dw_attr_node *at_accessibility;
6205 dw_attr_node *at_address_class;
6206 dw_attr_node *at_allocated;
6207 dw_attr_node *at_artificial;
6208 dw_attr_node *at_associated;
6209 dw_attr_node *at_binary_scale;
6210 dw_attr_node *at_bit_offset;
6211 dw_attr_node *at_bit_size;
6212 dw_attr_node *at_bit_stride;
6213 dw_attr_node *at_byte_size;
6214 dw_attr_node *at_byte_stride;
6215 dw_attr_node *at_const_value;
6216 dw_attr_node *at_containing_type;
6217 dw_attr_node *at_count;
6218 dw_attr_node *at_data_location;
6219 dw_attr_node *at_data_member_location;
6220 dw_attr_node *at_decimal_scale;
6221 dw_attr_node *at_decimal_sign;
6222 dw_attr_node *at_default_value;
6223 dw_attr_node *at_digit_count;
6224 dw_attr_node *at_discr;
6225 dw_attr_node *at_discr_list;
6226 dw_attr_node *at_discr_value;
6227 dw_attr_node *at_encoding;
6228 dw_attr_node *at_endianity;
6229 dw_attr_node *at_explicit;
6230 dw_attr_node *at_is_optional;
6231 dw_attr_node *at_location;
6232 dw_attr_node *at_lower_bound;
6233 dw_attr_node *at_mutable;
6234 dw_attr_node *at_ordering;
6235 dw_attr_node *at_picture_string;
6236 dw_attr_node *at_prototyped;
6237 dw_attr_node *at_small;
6238 dw_attr_node *at_segment;
6239 dw_attr_node *at_string_length;
6240 dw_attr_node *at_threads_scaled;
6241 dw_attr_node *at_upper_bound;
6242 dw_attr_node *at_use_location;
6243 dw_attr_node *at_use_UTF8;
6244 dw_attr_node *at_variable_parameter;
6245 dw_attr_node *at_virtuality;
6246 dw_attr_node *at_visibility;
6247 dw_attr_node *at_vtable_elem_location;
6250 /* Collect the attributes that we will want to use for the checksum. */
6252 static void
6253 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6255 dw_attr_node *a;
6256 unsigned ix;
6258 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6260 switch (a->dw_attr)
6262 case DW_AT_name:
6263 attrs->at_name = a;
6264 break;
6265 case DW_AT_type:
6266 attrs->at_type = a;
6267 break;
6268 case DW_AT_friend:
6269 attrs->at_friend = a;
6270 break;
6271 case DW_AT_accessibility:
6272 attrs->at_accessibility = a;
6273 break;
6274 case DW_AT_address_class:
6275 attrs->at_address_class = a;
6276 break;
6277 case DW_AT_allocated:
6278 attrs->at_allocated = a;
6279 break;
6280 case DW_AT_artificial:
6281 attrs->at_artificial = a;
6282 break;
6283 case DW_AT_associated:
6284 attrs->at_associated = a;
6285 break;
6286 case DW_AT_binary_scale:
6287 attrs->at_binary_scale = a;
6288 break;
6289 case DW_AT_bit_offset:
6290 attrs->at_bit_offset = a;
6291 break;
6292 case DW_AT_bit_size:
6293 attrs->at_bit_size = a;
6294 break;
6295 case DW_AT_bit_stride:
6296 attrs->at_bit_stride = a;
6297 break;
6298 case DW_AT_byte_size:
6299 attrs->at_byte_size = a;
6300 break;
6301 case DW_AT_byte_stride:
6302 attrs->at_byte_stride = a;
6303 break;
6304 case DW_AT_const_value:
6305 attrs->at_const_value = a;
6306 break;
6307 case DW_AT_containing_type:
6308 attrs->at_containing_type = a;
6309 break;
6310 case DW_AT_count:
6311 attrs->at_count = a;
6312 break;
6313 case DW_AT_data_location:
6314 attrs->at_data_location = a;
6315 break;
6316 case DW_AT_data_member_location:
6317 attrs->at_data_member_location = a;
6318 break;
6319 case DW_AT_decimal_scale:
6320 attrs->at_decimal_scale = a;
6321 break;
6322 case DW_AT_decimal_sign:
6323 attrs->at_decimal_sign = a;
6324 break;
6325 case DW_AT_default_value:
6326 attrs->at_default_value = a;
6327 break;
6328 case DW_AT_digit_count:
6329 attrs->at_digit_count = a;
6330 break;
6331 case DW_AT_discr:
6332 attrs->at_discr = a;
6333 break;
6334 case DW_AT_discr_list:
6335 attrs->at_discr_list = a;
6336 break;
6337 case DW_AT_discr_value:
6338 attrs->at_discr_value = a;
6339 break;
6340 case DW_AT_encoding:
6341 attrs->at_encoding = a;
6342 break;
6343 case DW_AT_endianity:
6344 attrs->at_endianity = a;
6345 break;
6346 case DW_AT_explicit:
6347 attrs->at_explicit = a;
6348 break;
6349 case DW_AT_is_optional:
6350 attrs->at_is_optional = a;
6351 break;
6352 case DW_AT_location:
6353 attrs->at_location = a;
6354 break;
6355 case DW_AT_lower_bound:
6356 attrs->at_lower_bound = a;
6357 break;
6358 case DW_AT_mutable:
6359 attrs->at_mutable = a;
6360 break;
6361 case DW_AT_ordering:
6362 attrs->at_ordering = a;
6363 break;
6364 case DW_AT_picture_string:
6365 attrs->at_picture_string = a;
6366 break;
6367 case DW_AT_prototyped:
6368 attrs->at_prototyped = a;
6369 break;
6370 case DW_AT_small:
6371 attrs->at_small = a;
6372 break;
6373 case DW_AT_segment:
6374 attrs->at_segment = a;
6375 break;
6376 case DW_AT_string_length:
6377 attrs->at_string_length = a;
6378 break;
6379 case DW_AT_threads_scaled:
6380 attrs->at_threads_scaled = a;
6381 break;
6382 case DW_AT_upper_bound:
6383 attrs->at_upper_bound = a;
6384 break;
6385 case DW_AT_use_location:
6386 attrs->at_use_location = a;
6387 break;
6388 case DW_AT_use_UTF8:
6389 attrs->at_use_UTF8 = a;
6390 break;
6391 case DW_AT_variable_parameter:
6392 attrs->at_variable_parameter = a;
6393 break;
6394 case DW_AT_virtuality:
6395 attrs->at_virtuality = a;
6396 break;
6397 case DW_AT_visibility:
6398 attrs->at_visibility = a;
6399 break;
6400 case DW_AT_vtable_elem_location:
6401 attrs->at_vtable_elem_location = a;
6402 break;
6403 default:
6404 break;
6409 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6411 static void
6412 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6414 dw_die_ref c;
6415 dw_die_ref decl;
6416 struct checksum_attributes attrs;
6418 CHECKSUM_ULEB128 ('D');
6419 CHECKSUM_ULEB128 (die->die_tag);
6421 memset (&attrs, 0, sizeof (attrs));
6423 decl = get_AT_ref (die, DW_AT_specification);
6424 if (decl != NULL)
6425 collect_checksum_attributes (&attrs, decl);
6426 collect_checksum_attributes (&attrs, die);
6428 CHECKSUM_ATTR (attrs.at_name);
6429 CHECKSUM_ATTR (attrs.at_accessibility);
6430 CHECKSUM_ATTR (attrs.at_address_class);
6431 CHECKSUM_ATTR (attrs.at_allocated);
6432 CHECKSUM_ATTR (attrs.at_artificial);
6433 CHECKSUM_ATTR (attrs.at_associated);
6434 CHECKSUM_ATTR (attrs.at_binary_scale);
6435 CHECKSUM_ATTR (attrs.at_bit_offset);
6436 CHECKSUM_ATTR (attrs.at_bit_size);
6437 CHECKSUM_ATTR (attrs.at_bit_stride);
6438 CHECKSUM_ATTR (attrs.at_byte_size);
6439 CHECKSUM_ATTR (attrs.at_byte_stride);
6440 CHECKSUM_ATTR (attrs.at_const_value);
6441 CHECKSUM_ATTR (attrs.at_containing_type);
6442 CHECKSUM_ATTR (attrs.at_count);
6443 CHECKSUM_ATTR (attrs.at_data_location);
6444 CHECKSUM_ATTR (attrs.at_data_member_location);
6445 CHECKSUM_ATTR (attrs.at_decimal_scale);
6446 CHECKSUM_ATTR (attrs.at_decimal_sign);
6447 CHECKSUM_ATTR (attrs.at_default_value);
6448 CHECKSUM_ATTR (attrs.at_digit_count);
6449 CHECKSUM_ATTR (attrs.at_discr);
6450 CHECKSUM_ATTR (attrs.at_discr_list);
6451 CHECKSUM_ATTR (attrs.at_discr_value);
6452 CHECKSUM_ATTR (attrs.at_encoding);
6453 CHECKSUM_ATTR (attrs.at_endianity);
6454 CHECKSUM_ATTR (attrs.at_explicit);
6455 CHECKSUM_ATTR (attrs.at_is_optional);
6456 CHECKSUM_ATTR (attrs.at_location);
6457 CHECKSUM_ATTR (attrs.at_lower_bound);
6458 CHECKSUM_ATTR (attrs.at_mutable);
6459 CHECKSUM_ATTR (attrs.at_ordering);
6460 CHECKSUM_ATTR (attrs.at_picture_string);
6461 CHECKSUM_ATTR (attrs.at_prototyped);
6462 CHECKSUM_ATTR (attrs.at_small);
6463 CHECKSUM_ATTR (attrs.at_segment);
6464 CHECKSUM_ATTR (attrs.at_string_length);
6465 CHECKSUM_ATTR (attrs.at_threads_scaled);
6466 CHECKSUM_ATTR (attrs.at_upper_bound);
6467 CHECKSUM_ATTR (attrs.at_use_location);
6468 CHECKSUM_ATTR (attrs.at_use_UTF8);
6469 CHECKSUM_ATTR (attrs.at_variable_parameter);
6470 CHECKSUM_ATTR (attrs.at_virtuality);
6471 CHECKSUM_ATTR (attrs.at_visibility);
6472 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6473 CHECKSUM_ATTR (attrs.at_type);
6474 CHECKSUM_ATTR (attrs.at_friend);
6476 /* Checksum the child DIEs. */
6477 c = die->die_child;
6478 if (c) do {
6479 dw_attr_node *name_attr;
6481 c = c->die_sib;
6482 name_attr = get_AT (c, DW_AT_name);
6483 if (is_template_instantiation (c))
6485 /* Ignore instantiations of member type and function templates. */
6487 else if (name_attr != NULL
6488 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6490 /* Use a shallow checksum for named nested types and member
6491 functions. */
6492 CHECKSUM_ULEB128 ('S');
6493 CHECKSUM_ULEB128 (c->die_tag);
6494 CHECKSUM_STRING (AT_string (name_attr));
6496 else
6498 /* Use a deep checksum for other children. */
6499 /* Mark this DIE so it gets processed when unmarking. */
6500 if (c->die_mark == 0)
6501 c->die_mark = -1;
6502 die_checksum_ordered (c, ctx, mark);
6504 } while (c != die->die_child);
6506 CHECKSUM_ULEB128 (0);
6509 /* Add a type name and tag to a hash. */
6510 static void
6511 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6513 CHECKSUM_ULEB128 (tag);
6514 CHECKSUM_STRING (name);
6517 #undef CHECKSUM
6518 #undef CHECKSUM_STRING
6519 #undef CHECKSUM_ATTR
6520 #undef CHECKSUM_LEB128
6521 #undef CHECKSUM_ULEB128
6523 /* Generate the type signature for DIE. This is computed by generating an
6524 MD5 checksum over the DIE's tag, its relevant attributes, and its
6525 children. Attributes that are references to other DIEs are processed
6526 by recursion, using the MARK field to prevent infinite recursion.
6527 If the DIE is nested inside a namespace or another type, we also
6528 need to include that context in the signature. The lower 64 bits
6529 of the resulting MD5 checksum comprise the signature. */
6531 static void
6532 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6534 int mark;
6535 const char *name;
6536 unsigned char checksum[16];
6537 struct md5_ctx ctx;
6538 dw_die_ref decl;
6539 dw_die_ref parent;
6541 name = get_AT_string (die, DW_AT_name);
6542 decl = get_AT_ref (die, DW_AT_specification);
6543 parent = get_die_parent (die);
6545 /* First, compute a signature for just the type name (and its surrounding
6546 context, if any. This is stored in the type unit DIE for link-time
6547 ODR (one-definition rule) checking. */
6549 if (is_cxx () && name != NULL)
6551 md5_init_ctx (&ctx);
6553 /* Checksum the names of surrounding namespaces and structures. */
6554 if (parent != NULL)
6555 checksum_die_context (parent, &ctx);
6557 /* Checksum the current DIE. */
6558 die_odr_checksum (die->die_tag, name, &ctx);
6559 md5_finish_ctx (&ctx, checksum);
6561 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6564 /* Next, compute the complete type signature. */
6566 md5_init_ctx (&ctx);
6567 mark = 1;
6568 die->die_mark = mark;
6570 /* Checksum the names of surrounding namespaces and structures. */
6571 if (parent != NULL)
6572 checksum_die_context (parent, &ctx);
6574 /* Checksum the DIE and its children. */
6575 die_checksum_ordered (die, &ctx, &mark);
6576 unmark_all_dies (die);
6577 md5_finish_ctx (&ctx, checksum);
6579 /* Store the signature in the type node and link the type DIE and the
6580 type node together. */
6581 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6582 DWARF_TYPE_SIGNATURE_SIZE);
6583 die->comdat_type_p = true;
6584 die->die_id.die_type_node = type_node;
6585 type_node->type_die = die;
6587 /* If the DIE is a specification, link its declaration to the type node
6588 as well. */
6589 if (decl != NULL)
6591 decl->comdat_type_p = true;
6592 decl->die_id.die_type_node = type_node;
6596 /* Do the location expressions look same? */
6597 static inline int
6598 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6600 return loc1->dw_loc_opc == loc2->dw_loc_opc
6601 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6602 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6605 /* Do the values look the same? */
6606 static int
6607 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6609 dw_loc_descr_ref loc1, loc2;
6610 rtx r1, r2;
6612 if (v1->val_class != v2->val_class)
6613 return 0;
6615 switch (v1->val_class)
6617 case dw_val_class_const:
6618 return v1->v.val_int == v2->v.val_int;
6619 case dw_val_class_unsigned_const:
6620 return v1->v.val_unsigned == v2->v.val_unsigned;
6621 case dw_val_class_const_double:
6622 return v1->v.val_double.high == v2->v.val_double.high
6623 && v1->v.val_double.low == v2->v.val_double.low;
6624 case dw_val_class_wide_int:
6625 return *v1->v.val_wide == *v2->v.val_wide;
6626 case dw_val_class_vec:
6627 if (v1->v.val_vec.length != v2->v.val_vec.length
6628 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6629 return 0;
6630 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6631 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6632 return 0;
6633 return 1;
6634 case dw_val_class_flag:
6635 return v1->v.val_flag == v2->v.val_flag;
6636 case dw_val_class_str:
6637 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6639 case dw_val_class_addr:
6640 r1 = v1->v.val_addr;
6641 r2 = v2->v.val_addr;
6642 if (GET_CODE (r1) != GET_CODE (r2))
6643 return 0;
6644 return !rtx_equal_p (r1, r2);
6646 case dw_val_class_offset:
6647 return v1->v.val_offset == v2->v.val_offset;
6649 case dw_val_class_loc:
6650 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6651 loc1 && loc2;
6652 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6653 if (!same_loc_p (loc1, loc2, mark))
6654 return 0;
6655 return !loc1 && !loc2;
6657 case dw_val_class_die_ref:
6658 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6660 case dw_val_class_fde_ref:
6661 case dw_val_class_vms_delta:
6662 case dw_val_class_lbl_id:
6663 case dw_val_class_lineptr:
6664 case dw_val_class_macptr:
6665 case dw_val_class_high_pc:
6666 return 1;
6668 case dw_val_class_file:
6669 return v1->v.val_file == v2->v.val_file;
6671 case dw_val_class_data8:
6672 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6674 default:
6675 return 1;
6679 /* Do the attributes look the same? */
6681 static int
6682 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6684 if (at1->dw_attr != at2->dw_attr)
6685 return 0;
6687 /* We don't care that this was compiled with a different compiler
6688 snapshot; if the output is the same, that's what matters. */
6689 if (at1->dw_attr == DW_AT_producer)
6690 return 1;
6692 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6695 /* Do the dies look the same? */
6697 static int
6698 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6700 dw_die_ref c1, c2;
6701 dw_attr_node *a1;
6702 unsigned ix;
6704 /* To avoid infinite recursion. */
6705 if (die1->die_mark)
6706 return die1->die_mark == die2->die_mark;
6707 die1->die_mark = die2->die_mark = ++(*mark);
6709 if (die1->die_tag != die2->die_tag)
6710 return 0;
6712 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6713 return 0;
6715 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6716 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6717 return 0;
6719 c1 = die1->die_child;
6720 c2 = die2->die_child;
6721 if (! c1)
6723 if (c2)
6724 return 0;
6726 else
6727 for (;;)
6729 if (!same_die_p (c1, c2, mark))
6730 return 0;
6731 c1 = c1->die_sib;
6732 c2 = c2->die_sib;
6733 if (c1 == die1->die_child)
6735 if (c2 == die2->die_child)
6736 break;
6737 else
6738 return 0;
6742 return 1;
6745 /* Do the dies look the same? Wrapper around same_die_p. */
6747 static int
6748 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6750 int mark = 0;
6751 int ret = same_die_p (die1, die2, &mark);
6753 unmark_all_dies (die1);
6754 unmark_all_dies (die2);
6756 return ret;
6759 /* The prefix to attach to symbols on DIEs in the current comdat debug
6760 info section. */
6761 static const char *comdat_symbol_id;
6763 /* The index of the current symbol within the current comdat CU. */
6764 static unsigned int comdat_symbol_number;
6766 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6767 children, and set comdat_symbol_id accordingly. */
6769 static void
6770 compute_section_prefix (dw_die_ref unit_die)
6772 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6773 const char *base = die_name ? lbasename (die_name) : "anonymous";
6774 char *name = XALLOCAVEC (char, strlen (base) + 64);
6775 char *p;
6776 int i, mark;
6777 unsigned char checksum[16];
6778 struct md5_ctx ctx;
6780 /* Compute the checksum of the DIE, then append part of it as hex digits to
6781 the name filename of the unit. */
6783 md5_init_ctx (&ctx);
6784 mark = 0;
6785 die_checksum (unit_die, &ctx, &mark);
6786 unmark_all_dies (unit_die);
6787 md5_finish_ctx (&ctx, checksum);
6789 sprintf (name, "%s.", base);
6790 clean_symbol_name (name);
6792 p = name + strlen (name);
6793 for (i = 0; i < 4; i++)
6795 sprintf (p, "%.2x", checksum[i]);
6796 p += 2;
6799 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6800 comdat_symbol_number = 0;
6803 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6805 static int
6806 is_type_die (dw_die_ref die)
6808 switch (die->die_tag)
6810 case DW_TAG_array_type:
6811 case DW_TAG_class_type:
6812 case DW_TAG_interface_type:
6813 case DW_TAG_enumeration_type:
6814 case DW_TAG_pointer_type:
6815 case DW_TAG_reference_type:
6816 case DW_TAG_rvalue_reference_type:
6817 case DW_TAG_string_type:
6818 case DW_TAG_structure_type:
6819 case DW_TAG_subroutine_type:
6820 case DW_TAG_union_type:
6821 case DW_TAG_ptr_to_member_type:
6822 case DW_TAG_set_type:
6823 case DW_TAG_subrange_type:
6824 case DW_TAG_base_type:
6825 case DW_TAG_const_type:
6826 case DW_TAG_file_type:
6827 case DW_TAG_packed_type:
6828 case DW_TAG_volatile_type:
6829 case DW_TAG_typedef:
6830 return 1;
6831 default:
6832 return 0;
6836 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6837 Basically, we want to choose the bits that are likely to be shared between
6838 compilations (types) and leave out the bits that are specific to individual
6839 compilations (functions). */
6841 static int
6842 is_comdat_die (dw_die_ref c)
6844 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6845 we do for stabs. The advantage is a greater likelihood of sharing between
6846 objects that don't include headers in the same order (and therefore would
6847 put the base types in a different comdat). jason 8/28/00 */
6849 if (c->die_tag == DW_TAG_base_type)
6850 return 0;
6852 if (c->die_tag == DW_TAG_pointer_type
6853 || c->die_tag == DW_TAG_reference_type
6854 || c->die_tag == DW_TAG_rvalue_reference_type
6855 || c->die_tag == DW_TAG_const_type
6856 || c->die_tag == DW_TAG_volatile_type)
6858 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6860 return t ? is_comdat_die (t) : 0;
6863 return is_type_die (c);
6866 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6867 compilation unit. */
6869 static int
6870 is_symbol_die (dw_die_ref c)
6872 return (is_type_die (c)
6873 || is_declaration_die (c)
6874 || c->die_tag == DW_TAG_namespace
6875 || c->die_tag == DW_TAG_module);
6878 /* Returns true iff C is a compile-unit DIE. */
6880 static inline bool
6881 is_cu_die (dw_die_ref c)
6883 return c && c->die_tag == DW_TAG_compile_unit;
6886 /* Returns true iff C is a unit DIE of some sort. */
6888 static inline bool
6889 is_unit_die (dw_die_ref c)
6891 return c && (c->die_tag == DW_TAG_compile_unit
6892 || c->die_tag == DW_TAG_partial_unit
6893 || c->die_tag == DW_TAG_type_unit);
6896 /* Returns true iff C is a namespace DIE. */
6898 static inline bool
6899 is_namespace_die (dw_die_ref c)
6901 return c && c->die_tag == DW_TAG_namespace;
6904 /* Returns true iff C is a class or structure DIE. */
6906 static inline bool
6907 is_class_die (dw_die_ref c)
6909 return c && (c->die_tag == DW_TAG_class_type
6910 || c->die_tag == DW_TAG_structure_type);
6913 /* Return non-zero if this DIE is a template parameter. */
6915 static inline bool
6916 is_template_parameter (dw_die_ref die)
6918 switch (die->die_tag)
6920 case DW_TAG_template_type_param:
6921 case DW_TAG_template_value_param:
6922 case DW_TAG_GNU_template_template_param:
6923 case DW_TAG_GNU_template_parameter_pack:
6924 return true;
6925 default:
6926 return false;
6930 /* Return non-zero if this DIE represents a template instantiation. */
6932 static inline bool
6933 is_template_instantiation (dw_die_ref die)
6935 dw_die_ref c;
6937 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6938 return false;
6939 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6940 return false;
6943 static char *
6944 gen_internal_sym (const char *prefix)
6946 char buf[256];
6948 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6949 return xstrdup (buf);
6952 /* Assign symbols to all worthy DIEs under DIE. */
6954 static void
6955 assign_symbol_names (dw_die_ref die)
6957 dw_die_ref c;
6959 if (is_symbol_die (die) && !die->comdat_type_p)
6961 if (comdat_symbol_id)
6963 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6965 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6966 comdat_symbol_id, comdat_symbol_number++);
6967 die->die_id.die_symbol = xstrdup (p);
6969 else
6970 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6973 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6976 struct cu_hash_table_entry
6978 dw_die_ref cu;
6979 unsigned min_comdat_num, max_comdat_num;
6980 struct cu_hash_table_entry *next;
6983 /* Helpers to manipulate hash table of CUs. */
6985 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6987 typedef die_struct *compare_type;
6988 static inline hashval_t hash (const cu_hash_table_entry *);
6989 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6990 static inline void remove (cu_hash_table_entry *);
6993 inline hashval_t
6994 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6996 return htab_hash_string (entry->cu->die_id.die_symbol);
6999 inline bool
7000 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7001 const die_struct *entry2)
7003 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7006 inline void
7007 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7009 struct cu_hash_table_entry *next;
7011 while (entry)
7013 next = entry->next;
7014 free (entry);
7015 entry = next;
7019 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7021 /* Check whether we have already seen this CU and set up SYM_NUM
7022 accordingly. */
7023 static int
7024 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7026 struct cu_hash_table_entry dummy;
7027 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7029 dummy.max_comdat_num = 0;
7031 slot = htable->find_slot_with_hash (cu,
7032 htab_hash_string (cu->die_id.die_symbol),
7033 INSERT);
7034 entry = *slot;
7036 for (; entry; last = entry, entry = entry->next)
7038 if (same_die_p_wrap (cu, entry->cu))
7039 break;
7042 if (entry)
7044 *sym_num = entry->min_comdat_num;
7045 return 1;
7048 entry = XCNEW (struct cu_hash_table_entry);
7049 entry->cu = cu;
7050 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7051 entry->next = *slot;
7052 *slot = entry;
7054 return 0;
7057 /* Record SYM_NUM to record of CU in HTABLE. */
7058 static void
7059 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7060 unsigned int sym_num)
7062 struct cu_hash_table_entry **slot, *entry;
7064 slot = htable->find_slot_with_hash (cu,
7065 htab_hash_string (cu->die_id.die_symbol),
7066 NO_INSERT);
7067 entry = *slot;
7069 entry->max_comdat_num = sym_num;
7072 /* Traverse the DIE (which is always comp_unit_die), and set up
7073 additional compilation units for each of the include files we see
7074 bracketed by BINCL/EINCL. */
7076 static void
7077 break_out_includes (dw_die_ref die)
7079 dw_die_ref c;
7080 dw_die_ref unit = NULL;
7081 limbo_die_node *node, **pnode;
7083 c = die->die_child;
7084 if (c) do {
7085 dw_die_ref prev = c;
7086 c = c->die_sib;
7087 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7088 || (unit && is_comdat_die (c)))
7090 dw_die_ref next = c->die_sib;
7092 /* This DIE is for a secondary CU; remove it from the main one. */
7093 remove_child_with_prev (c, prev);
7095 if (c->die_tag == DW_TAG_GNU_BINCL)
7096 unit = push_new_compile_unit (unit, c);
7097 else if (c->die_tag == DW_TAG_GNU_EINCL)
7098 unit = pop_compile_unit (unit);
7099 else
7100 add_child_die (unit, c);
7101 c = next;
7102 if (c == die->die_child)
7103 break;
7105 } while (c != die->die_child);
7107 #if 0
7108 /* We can only use this in debugging, since the frontend doesn't check
7109 to make sure that we leave every include file we enter. */
7110 gcc_assert (!unit);
7111 #endif
7113 assign_symbol_names (die);
7114 cu_hash_type cu_hash_table (10);
7115 for (node = limbo_die_list, pnode = &limbo_die_list;
7116 node;
7117 node = node->next)
7119 int is_dupl;
7121 compute_section_prefix (node->die);
7122 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7123 &comdat_symbol_number);
7124 assign_symbol_names (node->die);
7125 if (is_dupl)
7126 *pnode = node->next;
7127 else
7129 pnode = &node->next;
7130 record_comdat_symbol_number (node->die, &cu_hash_table,
7131 comdat_symbol_number);
7136 /* Return non-zero if this DIE is a declaration. */
7138 static int
7139 is_declaration_die (dw_die_ref die)
7141 dw_attr_node *a;
7142 unsigned ix;
7144 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7145 if (a->dw_attr == DW_AT_declaration)
7146 return 1;
7148 return 0;
7151 /* Return non-zero if this DIE is nested inside a subprogram. */
7153 static int
7154 is_nested_in_subprogram (dw_die_ref die)
7156 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7158 if (decl == NULL)
7159 decl = die;
7160 return local_scope_p (decl);
7163 /* Return non-zero if this DIE contains a defining declaration of a
7164 subprogram. */
7166 static int
7167 contains_subprogram_definition (dw_die_ref die)
7169 dw_die_ref c;
7171 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7172 return 1;
7173 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7174 return 0;
7177 /* Return non-zero if this is a type DIE that should be moved to a
7178 COMDAT .debug_types section. */
7180 static int
7181 should_move_die_to_comdat (dw_die_ref die)
7183 switch (die->die_tag)
7185 case DW_TAG_class_type:
7186 case DW_TAG_structure_type:
7187 case DW_TAG_enumeration_type:
7188 case DW_TAG_union_type:
7189 /* Don't move declarations, inlined instances, types nested in a
7190 subprogram, or types that contain subprogram definitions. */
7191 if (is_declaration_die (die)
7192 || get_AT (die, DW_AT_abstract_origin)
7193 || is_nested_in_subprogram (die)
7194 || contains_subprogram_definition (die))
7195 return 0;
7196 return 1;
7197 case DW_TAG_array_type:
7198 case DW_TAG_interface_type:
7199 case DW_TAG_pointer_type:
7200 case DW_TAG_reference_type:
7201 case DW_TAG_rvalue_reference_type:
7202 case DW_TAG_string_type:
7203 case DW_TAG_subroutine_type:
7204 case DW_TAG_ptr_to_member_type:
7205 case DW_TAG_set_type:
7206 case DW_TAG_subrange_type:
7207 case DW_TAG_base_type:
7208 case DW_TAG_const_type:
7209 case DW_TAG_file_type:
7210 case DW_TAG_packed_type:
7211 case DW_TAG_volatile_type:
7212 case DW_TAG_typedef:
7213 default:
7214 return 0;
7218 /* Make a clone of DIE. */
7220 static dw_die_ref
7221 clone_die (dw_die_ref die)
7223 dw_die_ref clone;
7224 dw_attr_node *a;
7225 unsigned ix;
7227 clone = ggc_cleared_alloc<die_node> ();
7228 clone->die_tag = die->die_tag;
7230 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7231 add_dwarf_attr (clone, a);
7233 return clone;
7236 /* Make a clone of the tree rooted at DIE. */
7238 static dw_die_ref
7239 clone_tree (dw_die_ref die)
7241 dw_die_ref c;
7242 dw_die_ref clone = clone_die (die);
7244 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7246 return clone;
7249 /* Make a clone of DIE as a declaration. */
7251 static dw_die_ref
7252 clone_as_declaration (dw_die_ref die)
7254 dw_die_ref clone;
7255 dw_die_ref decl;
7256 dw_attr_node *a;
7257 unsigned ix;
7259 /* If the DIE is already a declaration, just clone it. */
7260 if (is_declaration_die (die))
7261 return clone_die (die);
7263 /* If the DIE is a specification, just clone its declaration DIE. */
7264 decl = get_AT_ref (die, DW_AT_specification);
7265 if (decl != NULL)
7267 clone = clone_die (decl);
7268 if (die->comdat_type_p)
7269 add_AT_die_ref (clone, DW_AT_signature, die);
7270 return clone;
7273 clone = ggc_cleared_alloc<die_node> ();
7274 clone->die_tag = die->die_tag;
7276 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7278 /* We don't want to copy over all attributes.
7279 For example we don't want DW_AT_byte_size because otherwise we will no
7280 longer have a declaration and GDB will treat it as a definition. */
7282 switch (a->dw_attr)
7284 case DW_AT_abstract_origin:
7285 case DW_AT_artificial:
7286 case DW_AT_containing_type:
7287 case DW_AT_external:
7288 case DW_AT_name:
7289 case DW_AT_type:
7290 case DW_AT_virtuality:
7291 case DW_AT_linkage_name:
7292 case DW_AT_MIPS_linkage_name:
7293 add_dwarf_attr (clone, a);
7294 break;
7295 case DW_AT_byte_size:
7296 default:
7297 break;
7301 if (die->comdat_type_p)
7302 add_AT_die_ref (clone, DW_AT_signature, die);
7304 add_AT_flag (clone, DW_AT_declaration, 1);
7305 return clone;
7309 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7311 struct decl_table_entry
7313 dw_die_ref orig;
7314 dw_die_ref copy;
7317 /* Helpers to manipulate hash table of copied declarations. */
7319 /* Hashtable helpers. */
7321 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7323 typedef die_struct *compare_type;
7324 static inline hashval_t hash (const decl_table_entry *);
7325 static inline bool equal (const decl_table_entry *, const die_struct *);
7328 inline hashval_t
7329 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7331 return htab_hash_pointer (entry->orig);
7334 inline bool
7335 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7336 const die_struct *entry2)
7338 return entry1->orig == entry2;
7341 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7343 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7344 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7345 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7346 to check if the ancestor has already been copied into UNIT. */
7348 static dw_die_ref
7349 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7350 decl_hash_type *decl_table)
7352 dw_die_ref parent = die->die_parent;
7353 dw_die_ref new_parent = unit;
7354 dw_die_ref copy;
7355 decl_table_entry **slot = NULL;
7356 struct decl_table_entry *entry = NULL;
7358 if (decl_table)
7360 /* Check if the entry has already been copied to UNIT. */
7361 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7362 INSERT);
7363 if (*slot != HTAB_EMPTY_ENTRY)
7365 entry = *slot;
7366 return entry->copy;
7369 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7370 entry = XCNEW (struct decl_table_entry);
7371 entry->orig = die;
7372 entry->copy = NULL;
7373 *slot = entry;
7376 if (parent != NULL)
7378 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7379 if (spec != NULL)
7380 parent = spec;
7381 if (!is_unit_die (parent))
7382 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7385 copy = clone_as_declaration (die);
7386 add_child_die (new_parent, copy);
7388 if (decl_table)
7390 /* Record the pointer to the copy. */
7391 entry->copy = copy;
7394 return copy;
7396 /* Copy the declaration context to the new type unit DIE. This includes
7397 any surrounding namespace or type declarations. If the DIE has an
7398 AT_specification attribute, it also includes attributes and children
7399 attached to the specification, and returns a pointer to the original
7400 parent of the declaration DIE. Returns NULL otherwise. */
7402 static dw_die_ref
7403 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7405 dw_die_ref decl;
7406 dw_die_ref new_decl;
7407 dw_die_ref orig_parent = NULL;
7409 decl = get_AT_ref (die, DW_AT_specification);
7410 if (decl == NULL)
7411 decl = die;
7412 else
7414 unsigned ix;
7415 dw_die_ref c;
7416 dw_attr_node *a;
7418 /* The original DIE will be changed to a declaration, and must
7419 be moved to be a child of the original declaration DIE. */
7420 orig_parent = decl->die_parent;
7422 /* Copy the type node pointer from the new DIE to the original
7423 declaration DIE so we can forward references later. */
7424 decl->comdat_type_p = true;
7425 decl->die_id.die_type_node = die->die_id.die_type_node;
7427 remove_AT (die, DW_AT_specification);
7429 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7431 if (a->dw_attr != DW_AT_name
7432 && a->dw_attr != DW_AT_declaration
7433 && a->dw_attr != DW_AT_external)
7434 add_dwarf_attr (die, a);
7437 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7440 if (decl->die_parent != NULL
7441 && !is_unit_die (decl->die_parent))
7443 new_decl = copy_ancestor_tree (unit, decl, NULL);
7444 if (new_decl != NULL)
7446 remove_AT (new_decl, DW_AT_signature);
7447 add_AT_specification (die, new_decl);
7451 return orig_parent;
7454 /* Generate the skeleton ancestor tree for the given NODE, then clone
7455 the DIE and add the clone into the tree. */
7457 static void
7458 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7460 if (node->new_die != NULL)
7461 return;
7463 node->new_die = clone_as_declaration (node->old_die);
7465 if (node->parent != NULL)
7467 generate_skeleton_ancestor_tree (node->parent);
7468 add_child_die (node->parent->new_die, node->new_die);
7472 /* Generate a skeleton tree of DIEs containing any declarations that are
7473 found in the original tree. We traverse the tree looking for declaration
7474 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7476 static void
7477 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7479 skeleton_chain_node node;
7480 dw_die_ref c;
7481 dw_die_ref first;
7482 dw_die_ref prev = NULL;
7483 dw_die_ref next = NULL;
7485 node.parent = parent;
7487 first = c = parent->old_die->die_child;
7488 if (c)
7489 next = c->die_sib;
7490 if (c) do {
7491 if (prev == NULL || prev->die_sib == c)
7492 prev = c;
7493 c = next;
7494 next = (c == first ? NULL : c->die_sib);
7495 node.old_die = c;
7496 node.new_die = NULL;
7497 if (is_declaration_die (c))
7499 if (is_template_instantiation (c))
7501 /* Instantiated templates do not need to be cloned into the
7502 type unit. Just move the DIE and its children back to
7503 the skeleton tree (in the main CU). */
7504 remove_child_with_prev (c, prev);
7505 add_child_die (parent->new_die, c);
7506 c = prev;
7508 else
7510 /* Clone the existing DIE, move the original to the skeleton
7511 tree (which is in the main CU), and put the clone, with
7512 all the original's children, where the original came from
7513 (which is about to be moved to the type unit). */
7514 dw_die_ref clone = clone_die (c);
7515 move_all_children (c, clone);
7517 /* If the original has a DW_AT_object_pointer attribute,
7518 it would now point to a child DIE just moved to the
7519 cloned tree, so we need to remove that attribute from
7520 the original. */
7521 remove_AT (c, DW_AT_object_pointer);
7523 replace_child (c, clone, prev);
7524 generate_skeleton_ancestor_tree (parent);
7525 add_child_die (parent->new_die, c);
7526 node.new_die = c;
7527 c = clone;
7530 generate_skeleton_bottom_up (&node);
7531 } while (next != NULL);
7534 /* Wrapper function for generate_skeleton_bottom_up. */
7536 static dw_die_ref
7537 generate_skeleton (dw_die_ref die)
7539 skeleton_chain_node node;
7541 node.old_die = die;
7542 node.new_die = NULL;
7543 node.parent = NULL;
7545 /* If this type definition is nested inside another type,
7546 and is not an instantiation of a template, always leave
7547 at least a declaration in its place. */
7548 if (die->die_parent != NULL
7549 && is_type_die (die->die_parent)
7550 && !is_template_instantiation (die))
7551 node.new_die = clone_as_declaration (die);
7553 generate_skeleton_bottom_up (&node);
7554 return node.new_die;
7557 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7558 declaration. The original DIE is moved to a new compile unit so that
7559 existing references to it follow it to the new location. If any of the
7560 original DIE's descendants is a declaration, we need to replace the
7561 original DIE with a skeleton tree and move the declarations back into the
7562 skeleton tree. */
7564 static dw_die_ref
7565 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7566 dw_die_ref prev)
7568 dw_die_ref skeleton, orig_parent;
7570 /* Copy the declaration context to the type unit DIE. If the returned
7571 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7572 that DIE. */
7573 orig_parent = copy_declaration_context (unit, child);
7575 skeleton = generate_skeleton (child);
7576 if (skeleton == NULL)
7577 remove_child_with_prev (child, prev);
7578 else
7580 skeleton->comdat_type_p = true;
7581 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7583 /* If the original DIE was a specification, we need to put
7584 the skeleton under the parent DIE of the declaration.
7585 This leaves the original declaration in the tree, but
7586 it will be pruned later since there are no longer any
7587 references to it. */
7588 if (orig_parent != NULL)
7590 remove_child_with_prev (child, prev);
7591 add_child_die (orig_parent, skeleton);
7593 else
7594 replace_child (child, skeleton, prev);
7597 return skeleton;
7600 /* Traverse the DIE and set up additional .debug_types sections for each
7601 type worthy of being placed in a COMDAT section. */
7603 static void
7604 break_out_comdat_types (dw_die_ref die)
7606 dw_die_ref c;
7607 dw_die_ref first;
7608 dw_die_ref prev = NULL;
7609 dw_die_ref next = NULL;
7610 dw_die_ref unit = NULL;
7612 first = c = die->die_child;
7613 if (c)
7614 next = c->die_sib;
7615 if (c) do {
7616 if (prev == NULL || prev->die_sib == c)
7617 prev = c;
7618 c = next;
7619 next = (c == first ? NULL : c->die_sib);
7620 if (should_move_die_to_comdat (c))
7622 dw_die_ref replacement;
7623 comdat_type_node *type_node;
7625 /* Break out nested types into their own type units. */
7626 break_out_comdat_types (c);
7628 /* Create a new type unit DIE as the root for the new tree, and
7629 add it to the list of comdat types. */
7630 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7631 add_AT_unsigned (unit, DW_AT_language,
7632 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7633 type_node = ggc_cleared_alloc<comdat_type_node> ();
7634 type_node->root_die = unit;
7635 type_node->next = comdat_type_list;
7636 comdat_type_list = type_node;
7638 /* Generate the type signature. */
7639 generate_type_signature (c, type_node);
7641 /* Copy the declaration context, attributes, and children of the
7642 declaration into the new type unit DIE, then remove this DIE
7643 from the main CU (or replace it with a skeleton if necessary). */
7644 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7645 type_node->skeleton_die = replacement;
7647 /* Add the DIE to the new compunit. */
7648 add_child_die (unit, c);
7650 if (replacement != NULL)
7651 c = replacement;
7653 else if (c->die_tag == DW_TAG_namespace
7654 || c->die_tag == DW_TAG_class_type
7655 || c->die_tag == DW_TAG_structure_type
7656 || c->die_tag == DW_TAG_union_type)
7658 /* Look for nested types that can be broken out. */
7659 break_out_comdat_types (c);
7661 } while (next != NULL);
7664 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7665 Enter all the cloned children into the hash table decl_table. */
7667 static dw_die_ref
7668 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7670 dw_die_ref c;
7671 dw_die_ref clone;
7672 struct decl_table_entry *entry;
7673 decl_table_entry **slot;
7675 if (die->die_tag == DW_TAG_subprogram)
7676 clone = clone_as_declaration (die);
7677 else
7678 clone = clone_die (die);
7680 slot = decl_table->find_slot_with_hash (die,
7681 htab_hash_pointer (die), INSERT);
7683 /* Assert that DIE isn't in the hash table yet. If it would be there
7684 before, the ancestors would be necessarily there as well, therefore
7685 clone_tree_partial wouldn't be called. */
7686 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7688 entry = XCNEW (struct decl_table_entry);
7689 entry->orig = die;
7690 entry->copy = clone;
7691 *slot = entry;
7693 if (die->die_tag != DW_TAG_subprogram)
7694 FOR_EACH_CHILD (die, c,
7695 add_child_die (clone, clone_tree_partial (c, decl_table)));
7697 return clone;
7700 /* Walk the DIE and its children, looking for references to incomplete
7701 or trivial types that are unmarked (i.e., that are not in the current
7702 type_unit). */
7704 static void
7705 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7707 dw_die_ref c;
7708 dw_attr_node *a;
7709 unsigned ix;
7711 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7713 if (AT_class (a) == dw_val_class_die_ref)
7715 dw_die_ref targ = AT_ref (a);
7716 decl_table_entry **slot;
7717 struct decl_table_entry *entry;
7719 if (targ->die_mark != 0 || targ->comdat_type_p)
7720 continue;
7722 slot = decl_table->find_slot_with_hash (targ,
7723 htab_hash_pointer (targ),
7724 INSERT);
7726 if (*slot != HTAB_EMPTY_ENTRY)
7728 /* TARG has already been copied, so we just need to
7729 modify the reference to point to the copy. */
7730 entry = *slot;
7731 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7733 else
7735 dw_die_ref parent = unit;
7736 dw_die_ref copy = clone_die (targ);
7738 /* Record in DECL_TABLE that TARG has been copied.
7739 Need to do this now, before the recursive call,
7740 because DECL_TABLE may be expanded and SLOT
7741 would no longer be a valid pointer. */
7742 entry = XCNEW (struct decl_table_entry);
7743 entry->orig = targ;
7744 entry->copy = copy;
7745 *slot = entry;
7747 /* If TARG is not a declaration DIE, we need to copy its
7748 children. */
7749 if (!is_declaration_die (targ))
7751 FOR_EACH_CHILD (
7752 targ, c,
7753 add_child_die (copy,
7754 clone_tree_partial (c, decl_table)));
7757 /* Make sure the cloned tree is marked as part of the
7758 type unit. */
7759 mark_dies (copy);
7761 /* If TARG has surrounding context, copy its ancestor tree
7762 into the new type unit. */
7763 if (targ->die_parent != NULL
7764 && !is_unit_die (targ->die_parent))
7765 parent = copy_ancestor_tree (unit, targ->die_parent,
7766 decl_table);
7768 add_child_die (parent, copy);
7769 a->dw_attr_val.v.val_die_ref.die = copy;
7771 /* Make sure the newly-copied DIE is walked. If it was
7772 installed in a previously-added context, it won't
7773 get visited otherwise. */
7774 if (parent != unit)
7776 /* Find the highest point of the newly-added tree,
7777 mark each node along the way, and walk from there. */
7778 parent->die_mark = 1;
7779 while (parent->die_parent
7780 && parent->die_parent->die_mark == 0)
7782 parent = parent->die_parent;
7783 parent->die_mark = 1;
7785 copy_decls_walk (unit, parent, decl_table);
7791 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7794 /* Copy declarations for "unworthy" types into the new comdat section.
7795 Incomplete types, modified types, and certain other types aren't broken
7796 out into comdat sections of their own, so they don't have a signature,
7797 and we need to copy the declaration into the same section so that we
7798 don't have an external reference. */
7800 static void
7801 copy_decls_for_unworthy_types (dw_die_ref unit)
7803 mark_dies (unit);
7804 decl_hash_type decl_table (10);
7805 copy_decls_walk (unit, unit, &decl_table);
7806 unmark_dies (unit);
7809 /* Traverse the DIE and add a sibling attribute if it may have the
7810 effect of speeding up access to siblings. To save some space,
7811 avoid generating sibling attributes for DIE's without children. */
7813 static void
7814 add_sibling_attributes (dw_die_ref die)
7816 dw_die_ref c;
7818 if (! die->die_child)
7819 return;
7821 if (die->die_parent && die != die->die_parent->die_child)
7822 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7824 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7827 /* Output all location lists for the DIE and its children. */
7829 static void
7830 output_location_lists (dw_die_ref die)
7832 dw_die_ref c;
7833 dw_attr_node *a;
7834 unsigned ix;
7836 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7837 if (AT_class (a) == dw_val_class_loc_list)
7838 output_loc_list (AT_loc_list (a));
7840 FOR_EACH_CHILD (die, c, output_location_lists (c));
7843 /* We want to limit the number of external references, because they are
7844 larger than local references: a relocation takes multiple words, and
7845 even a sig8 reference is always eight bytes, whereas a local reference
7846 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7847 So if we encounter multiple external references to the same type DIE, we
7848 make a local typedef stub for it and redirect all references there.
7850 This is the element of the hash table for keeping track of these
7851 references. */
7853 struct external_ref
7855 dw_die_ref type;
7856 dw_die_ref stub;
7857 unsigned n_refs;
7860 /* Hashtable helpers. */
7862 struct external_ref_hasher : free_ptr_hash <external_ref>
7864 static inline hashval_t hash (const external_ref *);
7865 static inline bool equal (const external_ref *, const external_ref *);
7868 inline hashval_t
7869 external_ref_hasher::hash (const external_ref *r)
7871 dw_die_ref die = r->type;
7872 hashval_t h = 0;
7874 /* We can't use the address of the DIE for hashing, because
7875 that will make the order of the stub DIEs non-deterministic. */
7876 if (! die->comdat_type_p)
7877 /* We have a symbol; use it to compute a hash. */
7878 h = htab_hash_string (die->die_id.die_symbol);
7879 else
7881 /* We have a type signature; use a subset of the bits as the hash.
7882 The 8-byte signature is at least as large as hashval_t. */
7883 comdat_type_node *type_node = die->die_id.die_type_node;
7884 memcpy (&h, type_node->signature, sizeof (h));
7886 return h;
7889 inline bool
7890 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7892 return r1->type == r2->type;
7895 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7897 /* Return a pointer to the external_ref for references to DIE. */
7899 static struct external_ref *
7900 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7902 struct external_ref ref, *ref_p;
7903 external_ref **slot;
7905 ref.type = die;
7906 slot = map->find_slot (&ref, INSERT);
7907 if (*slot != HTAB_EMPTY_ENTRY)
7908 return *slot;
7910 ref_p = XCNEW (struct external_ref);
7911 ref_p->type = die;
7912 *slot = ref_p;
7913 return ref_p;
7916 /* Subroutine of optimize_external_refs, below.
7918 If we see a type skeleton, record it as our stub. If we see external
7919 references, remember how many we've seen. */
7921 static void
7922 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7924 dw_die_ref c;
7925 dw_attr_node *a;
7926 unsigned ix;
7927 struct external_ref *ref_p;
7929 if (is_type_die (die)
7930 && (c = get_AT_ref (die, DW_AT_signature)))
7932 /* This is a local skeleton; use it for local references. */
7933 ref_p = lookup_external_ref (map, c);
7934 ref_p->stub = die;
7937 /* Scan the DIE references, and remember any that refer to DIEs from
7938 other CUs (i.e. those which are not marked). */
7939 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7940 if (AT_class (a) == dw_val_class_die_ref
7941 && (c = AT_ref (a))->die_mark == 0
7942 && is_type_die (c))
7944 ref_p = lookup_external_ref (map, c);
7945 ref_p->n_refs++;
7948 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7951 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7952 points to an external_ref, DATA is the CU we're processing. If we don't
7953 already have a local stub, and we have multiple refs, build a stub. */
7956 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7958 struct external_ref *ref_p = *slot;
7960 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7962 /* We have multiple references to this type, so build a small stub.
7963 Both of these forms are a bit dodgy from the perspective of the
7964 DWARF standard, since technically they should have names. */
7965 dw_die_ref cu = data;
7966 dw_die_ref type = ref_p->type;
7967 dw_die_ref stub = NULL;
7969 if (type->comdat_type_p)
7971 /* If we refer to this type via sig8, use AT_signature. */
7972 stub = new_die (type->die_tag, cu, NULL_TREE);
7973 add_AT_die_ref (stub, DW_AT_signature, type);
7975 else
7977 /* Otherwise, use a typedef with no name. */
7978 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7979 add_AT_die_ref (stub, DW_AT_type, type);
7982 stub->die_mark++;
7983 ref_p->stub = stub;
7985 return 1;
7988 /* DIE is a unit; look through all the DIE references to see if there are
7989 any external references to types, and if so, create local stubs for
7990 them which will be applied in build_abbrev_table. This is useful because
7991 references to local DIEs are smaller. */
7993 static external_ref_hash_type *
7994 optimize_external_refs (dw_die_ref die)
7996 external_ref_hash_type *map = new external_ref_hash_type (10);
7997 optimize_external_refs_1 (die, map);
7998 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7999 return map;
8002 /* The format of each DIE (and its attribute value pairs) is encoded in an
8003 abbreviation table. This routine builds the abbreviation table and assigns
8004 a unique abbreviation id for each abbreviation entry. The children of each
8005 die are visited recursively. */
8007 static void
8008 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8010 unsigned long abbrev_id;
8011 unsigned int n_alloc;
8012 dw_die_ref c;
8013 dw_attr_node *a;
8014 unsigned ix;
8016 /* Scan the DIE references, and replace any that refer to
8017 DIEs from other CUs (i.e. those which are not marked) with
8018 the local stubs we built in optimize_external_refs. */
8019 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8020 if (AT_class (a) == dw_val_class_die_ref
8021 && (c = AT_ref (a))->die_mark == 0)
8023 struct external_ref *ref_p;
8024 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8026 ref_p = lookup_external_ref (extern_map, c);
8027 if (ref_p->stub && ref_p->stub != die)
8028 change_AT_die_ref (a, ref_p->stub);
8029 else
8030 /* We aren't changing this reference, so mark it external. */
8031 set_AT_ref_external (a, 1);
8034 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8036 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8037 dw_attr_node *die_a, *abbrev_a;
8038 unsigned ix;
8039 bool ok = true;
8041 if (abbrev->die_tag != die->die_tag)
8042 continue;
8043 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8044 continue;
8046 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8047 continue;
8049 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8051 abbrev_a = &(*abbrev->die_attr)[ix];
8052 if ((abbrev_a->dw_attr != die_a->dw_attr)
8053 || (value_format (abbrev_a) != value_format (die_a)))
8055 ok = false;
8056 break;
8059 if (ok)
8060 break;
8063 if (abbrev_id >= abbrev_die_table_in_use)
8065 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8067 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8068 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8069 n_alloc);
8071 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8072 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8073 abbrev_die_table_allocated = n_alloc;
8076 ++abbrev_die_table_in_use;
8077 abbrev_die_table[abbrev_id] = die;
8080 die->die_abbrev = abbrev_id;
8081 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8084 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8086 static int
8087 constant_size (unsigned HOST_WIDE_INT value)
8089 int log;
8091 if (value == 0)
8092 log = 0;
8093 else
8094 log = floor_log2 (value);
8096 log = log / 8;
8097 log = 1 << (floor_log2 (log) + 1);
8099 return log;
8102 /* Return the size of a DIE as it is represented in the
8103 .debug_info section. */
8105 static unsigned long
8106 size_of_die (dw_die_ref die)
8108 unsigned long size = 0;
8109 dw_attr_node *a;
8110 unsigned ix;
8111 enum dwarf_form form;
8113 size += size_of_uleb128 (die->die_abbrev);
8114 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8116 switch (AT_class (a))
8118 case dw_val_class_addr:
8119 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8121 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8122 size += size_of_uleb128 (AT_index (a));
8124 else
8125 size += DWARF2_ADDR_SIZE;
8126 break;
8127 case dw_val_class_offset:
8128 size += DWARF_OFFSET_SIZE;
8129 break;
8130 case dw_val_class_loc:
8132 unsigned long lsize = size_of_locs (AT_loc (a));
8134 /* Block length. */
8135 if (dwarf_version >= 4)
8136 size += size_of_uleb128 (lsize);
8137 else
8138 size += constant_size (lsize);
8139 size += lsize;
8141 break;
8142 case dw_val_class_loc_list:
8143 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8145 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8146 size += size_of_uleb128 (AT_index (a));
8148 else
8149 size += DWARF_OFFSET_SIZE;
8150 break;
8151 case dw_val_class_range_list:
8152 size += DWARF_OFFSET_SIZE;
8153 break;
8154 case dw_val_class_const:
8155 size += size_of_sleb128 (AT_int (a));
8156 break;
8157 case dw_val_class_unsigned_const:
8159 int csize = constant_size (AT_unsigned (a));
8160 if (dwarf_version == 3
8161 && a->dw_attr == DW_AT_data_member_location
8162 && csize >= 4)
8163 size += size_of_uleb128 (AT_unsigned (a));
8164 else
8165 size += csize;
8167 break;
8168 case dw_val_class_const_double:
8169 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8170 if (HOST_BITS_PER_WIDE_INT >= 64)
8171 size++; /* block */
8172 break;
8173 case dw_val_class_wide_int:
8174 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8175 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8176 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8177 > 64)
8178 size++; /* block */
8179 break;
8180 case dw_val_class_vec:
8181 size += constant_size (a->dw_attr_val.v.val_vec.length
8182 * a->dw_attr_val.v.val_vec.elt_size)
8183 + a->dw_attr_val.v.val_vec.length
8184 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8185 break;
8186 case dw_val_class_flag:
8187 if (dwarf_version >= 4)
8188 /* Currently all add_AT_flag calls pass in 1 as last argument,
8189 so DW_FORM_flag_present can be used. If that ever changes,
8190 we'll need to use DW_FORM_flag and have some optimization
8191 in build_abbrev_table that will change those to
8192 DW_FORM_flag_present if it is set to 1 in all DIEs using
8193 the same abbrev entry. */
8194 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8195 else
8196 size += 1;
8197 break;
8198 case dw_val_class_die_ref:
8199 if (AT_ref_external (a))
8201 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8202 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8203 is sized by target address length, whereas in DWARF3
8204 it's always sized as an offset. */
8205 if (use_debug_types)
8206 size += DWARF_TYPE_SIGNATURE_SIZE;
8207 else if (dwarf_version == 2)
8208 size += DWARF2_ADDR_SIZE;
8209 else
8210 size += DWARF_OFFSET_SIZE;
8212 else
8213 size += DWARF_OFFSET_SIZE;
8214 break;
8215 case dw_val_class_fde_ref:
8216 size += DWARF_OFFSET_SIZE;
8217 break;
8218 case dw_val_class_lbl_id:
8219 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8221 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8222 size += size_of_uleb128 (AT_index (a));
8224 else
8225 size += DWARF2_ADDR_SIZE;
8226 break;
8227 case dw_val_class_lineptr:
8228 case dw_val_class_macptr:
8229 size += DWARF_OFFSET_SIZE;
8230 break;
8231 case dw_val_class_str:
8232 form = AT_string_form (a);
8233 if (form == DW_FORM_strp)
8234 size += DWARF_OFFSET_SIZE;
8235 else if (form == DW_FORM_GNU_str_index)
8236 size += size_of_uleb128 (AT_index (a));
8237 else
8238 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8239 break;
8240 case dw_val_class_file:
8241 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8242 break;
8243 case dw_val_class_data8:
8244 size += 8;
8245 break;
8246 case dw_val_class_vms_delta:
8247 size += DWARF_OFFSET_SIZE;
8248 break;
8249 case dw_val_class_high_pc:
8250 size += DWARF2_ADDR_SIZE;
8251 break;
8252 default:
8253 gcc_unreachable ();
8257 return size;
8260 /* Size the debugging information associated with a given DIE. Visits the
8261 DIE's children recursively. Updates the global variable next_die_offset, on
8262 each time through. Uses the current value of next_die_offset to update the
8263 die_offset field in each DIE. */
8265 static void
8266 calc_die_sizes (dw_die_ref die)
8268 dw_die_ref c;
8270 gcc_assert (die->die_offset == 0
8271 || (unsigned long int) die->die_offset == next_die_offset);
8272 die->die_offset = next_die_offset;
8273 next_die_offset += size_of_die (die);
8275 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8277 if (die->die_child != NULL)
8278 /* Count the null byte used to terminate sibling lists. */
8279 next_die_offset += 1;
8282 /* Size just the base type children at the start of the CU.
8283 This is needed because build_abbrev needs to size locs
8284 and sizing of type based stack ops needs to know die_offset
8285 values for the base types. */
8287 static void
8288 calc_base_type_die_sizes (void)
8290 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8291 unsigned int i;
8292 dw_die_ref base_type;
8293 #if ENABLE_ASSERT_CHECKING
8294 dw_die_ref prev = comp_unit_die ()->die_child;
8295 #endif
8297 die_offset += size_of_die (comp_unit_die ());
8298 for (i = 0; base_types.iterate (i, &base_type); i++)
8300 #if ENABLE_ASSERT_CHECKING
8301 gcc_assert (base_type->die_offset == 0
8302 && prev->die_sib == base_type
8303 && base_type->die_child == NULL
8304 && base_type->die_abbrev);
8305 prev = base_type;
8306 #endif
8307 base_type->die_offset = die_offset;
8308 die_offset += size_of_die (base_type);
8312 /* Set the marks for a die and its children. We do this so
8313 that we know whether or not a reference needs to use FORM_ref_addr; only
8314 DIEs in the same CU will be marked. We used to clear out the offset
8315 and use that as the flag, but ran into ordering problems. */
8317 static void
8318 mark_dies (dw_die_ref die)
8320 dw_die_ref c;
8322 gcc_assert (!die->die_mark);
8324 die->die_mark = 1;
8325 FOR_EACH_CHILD (die, c, mark_dies (c));
8328 /* Clear the marks for a die and its children. */
8330 static void
8331 unmark_dies (dw_die_ref die)
8333 dw_die_ref c;
8335 if (! use_debug_types)
8336 gcc_assert (die->die_mark);
8338 die->die_mark = 0;
8339 FOR_EACH_CHILD (die, c, unmark_dies (c));
8342 /* Clear the marks for a die, its children and referred dies. */
8344 static void
8345 unmark_all_dies (dw_die_ref die)
8347 dw_die_ref c;
8348 dw_attr_node *a;
8349 unsigned ix;
8351 if (!die->die_mark)
8352 return;
8353 die->die_mark = 0;
8355 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8357 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8358 if (AT_class (a) == dw_val_class_die_ref)
8359 unmark_all_dies (AT_ref (a));
8362 /* Calculate if the entry should appear in the final output file. It may be
8363 from a pruned a type. */
8365 static bool
8366 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8368 /* By limiting gnu pubnames to definitions only, gold can generate a
8369 gdb index without entries for declarations, which don't include
8370 enough information to be useful. */
8371 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8372 return false;
8374 if (table == pubname_table)
8376 /* Enumerator names are part of the pubname table, but the
8377 parent DW_TAG_enumeration_type die may have been pruned.
8378 Don't output them if that is the case. */
8379 if (p->die->die_tag == DW_TAG_enumerator &&
8380 (p->die->die_parent == NULL
8381 || !p->die->die_parent->die_perennial_p))
8382 return false;
8384 /* Everything else in the pubname table is included. */
8385 return true;
8388 /* The pubtypes table shouldn't include types that have been
8389 pruned. */
8390 return (p->die->die_offset != 0
8391 || !flag_eliminate_unused_debug_types);
8394 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8395 generated for the compilation unit. */
8397 static unsigned long
8398 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8400 unsigned long size;
8401 unsigned i;
8402 pubname_entry *p;
8403 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8405 size = DWARF_PUBNAMES_HEADER_SIZE;
8406 FOR_EACH_VEC_ELT (*names, i, p)
8407 if (include_pubname_in_output (names, p))
8408 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8410 size += DWARF_OFFSET_SIZE;
8411 return size;
8414 /* Return the size of the information in the .debug_aranges section. */
8416 static unsigned long
8417 size_of_aranges (void)
8419 unsigned long size;
8421 size = DWARF_ARANGES_HEADER_SIZE;
8423 /* Count the address/length pair for this compilation unit. */
8424 if (text_section_used)
8425 size += 2 * DWARF2_ADDR_SIZE;
8426 if (cold_text_section_used)
8427 size += 2 * DWARF2_ADDR_SIZE;
8428 if (have_multiple_function_sections)
8430 unsigned fde_idx;
8431 dw_fde_ref fde;
8433 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8435 if (DECL_IGNORED_P (fde->decl))
8436 continue;
8437 if (!fde->in_std_section)
8438 size += 2 * DWARF2_ADDR_SIZE;
8439 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8440 size += 2 * DWARF2_ADDR_SIZE;
8444 /* Count the two zero words used to terminated the address range table. */
8445 size += 2 * DWARF2_ADDR_SIZE;
8446 return size;
8449 /* Select the encoding of an attribute value. */
8451 static enum dwarf_form
8452 value_format (dw_attr_node *a)
8454 switch (AT_class (a))
8456 case dw_val_class_addr:
8457 /* Only very few attributes allow DW_FORM_addr. */
8458 switch (a->dw_attr)
8460 case DW_AT_low_pc:
8461 case DW_AT_high_pc:
8462 case DW_AT_entry_pc:
8463 case DW_AT_trampoline:
8464 return (AT_index (a) == NOT_INDEXED
8465 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8466 default:
8467 break;
8469 switch (DWARF2_ADDR_SIZE)
8471 case 1:
8472 return DW_FORM_data1;
8473 case 2:
8474 return DW_FORM_data2;
8475 case 4:
8476 return DW_FORM_data4;
8477 case 8:
8478 return DW_FORM_data8;
8479 default:
8480 gcc_unreachable ();
8482 case dw_val_class_range_list:
8483 case dw_val_class_loc_list:
8484 if (dwarf_version >= 4)
8485 return DW_FORM_sec_offset;
8486 /* FALLTHRU */
8487 case dw_val_class_vms_delta:
8488 case dw_val_class_offset:
8489 switch (DWARF_OFFSET_SIZE)
8491 case 4:
8492 return DW_FORM_data4;
8493 case 8:
8494 return DW_FORM_data8;
8495 default:
8496 gcc_unreachable ();
8498 case dw_val_class_loc:
8499 if (dwarf_version >= 4)
8500 return DW_FORM_exprloc;
8501 switch (constant_size (size_of_locs (AT_loc (a))))
8503 case 1:
8504 return DW_FORM_block1;
8505 case 2:
8506 return DW_FORM_block2;
8507 case 4:
8508 return DW_FORM_block4;
8509 default:
8510 gcc_unreachable ();
8512 case dw_val_class_const:
8513 return DW_FORM_sdata;
8514 case dw_val_class_unsigned_const:
8515 switch (constant_size (AT_unsigned (a)))
8517 case 1:
8518 return DW_FORM_data1;
8519 case 2:
8520 return DW_FORM_data2;
8521 case 4:
8522 /* In DWARF3 DW_AT_data_member_location with
8523 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8524 constant, so we need to use DW_FORM_udata if we need
8525 a large constant. */
8526 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8527 return DW_FORM_udata;
8528 return DW_FORM_data4;
8529 case 8:
8530 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8531 return DW_FORM_udata;
8532 return DW_FORM_data8;
8533 default:
8534 gcc_unreachable ();
8536 case dw_val_class_const_double:
8537 switch (HOST_BITS_PER_WIDE_INT)
8539 case 8:
8540 return DW_FORM_data2;
8541 case 16:
8542 return DW_FORM_data4;
8543 case 32:
8544 return DW_FORM_data8;
8545 case 64:
8546 default:
8547 return DW_FORM_block1;
8549 case dw_val_class_wide_int:
8550 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8552 case 8:
8553 return DW_FORM_data1;
8554 case 16:
8555 return DW_FORM_data2;
8556 case 32:
8557 return DW_FORM_data4;
8558 case 64:
8559 return DW_FORM_data8;
8560 default:
8561 return DW_FORM_block1;
8563 case dw_val_class_vec:
8564 switch (constant_size (a->dw_attr_val.v.val_vec.length
8565 * a->dw_attr_val.v.val_vec.elt_size))
8567 case 1:
8568 return DW_FORM_block1;
8569 case 2:
8570 return DW_FORM_block2;
8571 case 4:
8572 return DW_FORM_block4;
8573 default:
8574 gcc_unreachable ();
8576 case dw_val_class_flag:
8577 if (dwarf_version >= 4)
8579 /* Currently all add_AT_flag calls pass in 1 as last argument,
8580 so DW_FORM_flag_present can be used. If that ever changes,
8581 we'll need to use DW_FORM_flag and have some optimization
8582 in build_abbrev_table that will change those to
8583 DW_FORM_flag_present if it is set to 1 in all DIEs using
8584 the same abbrev entry. */
8585 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8586 return DW_FORM_flag_present;
8588 return DW_FORM_flag;
8589 case dw_val_class_die_ref:
8590 if (AT_ref_external (a))
8591 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8592 else
8593 return DW_FORM_ref;
8594 case dw_val_class_fde_ref:
8595 return DW_FORM_data;
8596 case dw_val_class_lbl_id:
8597 return (AT_index (a) == NOT_INDEXED
8598 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8599 case dw_val_class_lineptr:
8600 case dw_val_class_macptr:
8601 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8602 case dw_val_class_str:
8603 return AT_string_form (a);
8604 case dw_val_class_file:
8605 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8607 case 1:
8608 return DW_FORM_data1;
8609 case 2:
8610 return DW_FORM_data2;
8611 case 4:
8612 return DW_FORM_data4;
8613 default:
8614 gcc_unreachable ();
8617 case dw_val_class_data8:
8618 return DW_FORM_data8;
8620 case dw_val_class_high_pc:
8621 switch (DWARF2_ADDR_SIZE)
8623 case 1:
8624 return DW_FORM_data1;
8625 case 2:
8626 return DW_FORM_data2;
8627 case 4:
8628 return DW_FORM_data4;
8629 case 8:
8630 return DW_FORM_data8;
8631 default:
8632 gcc_unreachable ();
8635 default:
8636 gcc_unreachable ();
8640 /* Output the encoding of an attribute value. */
8642 static void
8643 output_value_format (dw_attr_node *a)
8645 enum dwarf_form form = value_format (a);
8647 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8650 /* Given a die and id, produce the appropriate abbreviations. */
8652 static void
8653 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8655 unsigned ix;
8656 dw_attr_node *a_attr;
8658 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8659 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8660 dwarf_tag_name (abbrev->die_tag));
8662 if (abbrev->die_child != NULL)
8663 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8664 else
8665 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8667 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8669 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8670 dwarf_attr_name (a_attr->dw_attr));
8671 output_value_format (a_attr);
8674 dw2_asm_output_data (1, 0, NULL);
8675 dw2_asm_output_data (1, 0, NULL);
8679 /* Output the .debug_abbrev section which defines the DIE abbreviation
8680 table. */
8682 static void
8683 output_abbrev_section (void)
8685 unsigned long abbrev_id;
8687 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8688 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8690 /* Terminate the table. */
8691 dw2_asm_output_data (1, 0, NULL);
8694 /* Output a symbol we can use to refer to this DIE from another CU. */
8696 static inline void
8697 output_die_symbol (dw_die_ref die)
8699 const char *sym = die->die_id.die_symbol;
8701 gcc_assert (!die->comdat_type_p);
8703 if (sym == 0)
8704 return;
8706 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8707 /* We make these global, not weak; if the target doesn't support
8708 .linkonce, it doesn't support combining the sections, so debugging
8709 will break. */
8710 targetm.asm_out.globalize_label (asm_out_file, sym);
8712 ASM_OUTPUT_LABEL (asm_out_file, sym);
8715 /* Return a new location list, given the begin and end range, and the
8716 expression. */
8718 static inline dw_loc_list_ref
8719 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8720 const char *section)
8722 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8724 retlist->begin = begin;
8725 retlist->begin_entry = NULL;
8726 retlist->end = end;
8727 retlist->expr = expr;
8728 retlist->section = section;
8730 return retlist;
8733 /* Generate a new internal symbol for this location list node, if it
8734 hasn't got one yet. */
8736 static inline void
8737 gen_llsym (dw_loc_list_ref list)
8739 gcc_assert (!list->ll_symbol);
8740 list->ll_symbol = gen_internal_sym ("LLST");
8743 /* Output the location list given to us. */
8745 static void
8746 output_loc_list (dw_loc_list_ref list_head)
8748 dw_loc_list_ref curr = list_head;
8750 if (list_head->emitted)
8751 return;
8752 list_head->emitted = true;
8754 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8756 /* Walk the location list, and output each range + expression. */
8757 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8759 unsigned long size;
8760 /* Don't output an entry that starts and ends at the same address. */
8761 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8762 continue;
8763 size = size_of_locs (curr->expr);
8764 /* If the expression is too large, drop it on the floor. We could
8765 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8766 in the expression, but >= 64KB expressions for a single value
8767 in a single range are unlikely very useful. */
8768 if (size > 0xffff)
8769 continue;
8770 if (dwarf_split_debug_info)
8772 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8773 "Location list start/length entry (%s)",
8774 list_head->ll_symbol);
8775 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8776 "Location list range start index (%s)",
8777 curr->begin);
8778 /* The length field is 4 bytes. If we ever need to support
8779 an 8-byte length, we can add a new DW_LLE code or fall back
8780 to DW_LLE_GNU_start_end_entry. */
8781 dw2_asm_output_delta (4, curr->end, curr->begin,
8782 "Location list range length (%s)",
8783 list_head->ll_symbol);
8785 else if (!have_multiple_function_sections)
8787 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8788 "Location list begin address (%s)",
8789 list_head->ll_symbol);
8790 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8791 "Location list end address (%s)",
8792 list_head->ll_symbol);
8794 else
8796 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8797 "Location list begin address (%s)",
8798 list_head->ll_symbol);
8799 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8800 "Location list end address (%s)",
8801 list_head->ll_symbol);
8804 /* Output the block length for this list of location operations. */
8805 gcc_assert (size <= 0xffff);
8806 dw2_asm_output_data (2, size, "%s", "Location expression size");
8808 output_loc_sequence (curr->expr, -1);
8811 if (dwarf_split_debug_info)
8812 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8813 "Location list terminator (%s)",
8814 list_head->ll_symbol);
8815 else
8817 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8818 "Location list terminator begin (%s)",
8819 list_head->ll_symbol);
8820 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8821 "Location list terminator end (%s)",
8822 list_head->ll_symbol);
8826 /* Output a range_list offset into the debug_range section. Emit a
8827 relocated reference if val_entry is NULL, otherwise, emit an
8828 indirect reference. */
8830 static void
8831 output_range_list_offset (dw_attr_node *a)
8833 const char *name = dwarf_attr_name (a->dw_attr);
8835 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8837 char *p = strchr (ranges_section_label, '\0');
8838 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8839 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8840 debug_ranges_section, "%s", name);
8841 *p = '\0';
8843 else
8844 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8845 "%s (offset from %s)", name, ranges_section_label);
8848 /* Output the offset into the debug_loc section. */
8850 static void
8851 output_loc_list_offset (dw_attr_node *a)
8853 char *sym = AT_loc_list (a)->ll_symbol;
8855 gcc_assert (sym);
8856 if (dwarf_split_debug_info)
8857 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8858 "%s", dwarf_attr_name (a->dw_attr));
8859 else
8860 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8861 "%s", dwarf_attr_name (a->dw_attr));
8864 /* Output an attribute's index or value appropriately. */
8866 static void
8867 output_attr_index_or_value (dw_attr_node *a)
8869 const char *name = dwarf_attr_name (a->dw_attr);
8871 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8873 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8874 return;
8876 switch (AT_class (a))
8878 case dw_val_class_addr:
8879 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8880 break;
8881 case dw_val_class_high_pc:
8882 case dw_val_class_lbl_id:
8883 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8884 break;
8885 case dw_val_class_loc_list:
8886 output_loc_list_offset (a);
8887 break;
8888 default:
8889 gcc_unreachable ();
8893 /* Output a type signature. */
8895 static inline void
8896 output_signature (const char *sig, const char *name)
8898 int i;
8900 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8901 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8904 /* Output the DIE and its attributes. Called recursively to generate
8905 the definitions of each child DIE. */
8907 static void
8908 output_die (dw_die_ref die)
8910 dw_attr_node *a;
8911 dw_die_ref c;
8912 unsigned long size;
8913 unsigned ix;
8915 /* If someone in another CU might refer to us, set up a symbol for
8916 them to point to. */
8917 if (! die->comdat_type_p && die->die_id.die_symbol)
8918 output_die_symbol (die);
8920 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8921 (unsigned long)die->die_offset,
8922 dwarf_tag_name (die->die_tag));
8924 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8926 const char *name = dwarf_attr_name (a->dw_attr);
8928 switch (AT_class (a))
8930 case dw_val_class_addr:
8931 output_attr_index_or_value (a);
8932 break;
8934 case dw_val_class_offset:
8935 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8936 "%s", name);
8937 break;
8939 case dw_val_class_range_list:
8940 output_range_list_offset (a);
8941 break;
8943 case dw_val_class_loc:
8944 size = size_of_locs (AT_loc (a));
8946 /* Output the block length for this list of location operations. */
8947 if (dwarf_version >= 4)
8948 dw2_asm_output_data_uleb128 (size, "%s", name);
8949 else
8950 dw2_asm_output_data (constant_size (size), size, "%s", name);
8952 output_loc_sequence (AT_loc (a), -1);
8953 break;
8955 case dw_val_class_const:
8956 /* ??? It would be slightly more efficient to use a scheme like is
8957 used for unsigned constants below, but gdb 4.x does not sign
8958 extend. Gdb 5.x does sign extend. */
8959 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8960 break;
8962 case dw_val_class_unsigned_const:
8964 int csize = constant_size (AT_unsigned (a));
8965 if (dwarf_version == 3
8966 && a->dw_attr == DW_AT_data_member_location
8967 && csize >= 4)
8968 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8969 else
8970 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8972 break;
8974 case dw_val_class_const_double:
8976 unsigned HOST_WIDE_INT first, second;
8978 if (HOST_BITS_PER_WIDE_INT >= 64)
8979 dw2_asm_output_data (1,
8980 HOST_BITS_PER_DOUBLE_INT
8981 / HOST_BITS_PER_CHAR,
8982 NULL);
8984 if (WORDS_BIG_ENDIAN)
8986 first = a->dw_attr_val.v.val_double.high;
8987 second = a->dw_attr_val.v.val_double.low;
8989 else
8991 first = a->dw_attr_val.v.val_double.low;
8992 second = a->dw_attr_val.v.val_double.high;
8995 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8996 first, "%s", name);
8997 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8998 second, NULL);
9000 break;
9002 case dw_val_class_wide_int:
9004 int i;
9005 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9006 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9007 if (len * HOST_BITS_PER_WIDE_INT > 64)
9008 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9009 NULL);
9011 if (WORDS_BIG_ENDIAN)
9012 for (i = len - 1; i >= 0; --i)
9014 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9015 "%s", name);
9016 name = "";
9018 else
9019 for (i = 0; i < len; ++i)
9021 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9022 "%s", name);
9023 name = "";
9026 break;
9028 case dw_val_class_vec:
9030 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9031 unsigned int len = a->dw_attr_val.v.val_vec.length;
9032 unsigned int i;
9033 unsigned char *p;
9035 dw2_asm_output_data (constant_size (len * elt_size),
9036 len * elt_size, "%s", name);
9037 if (elt_size > sizeof (HOST_WIDE_INT))
9039 elt_size /= 2;
9040 len *= 2;
9042 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9043 i < len;
9044 i++, p += elt_size)
9045 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9046 "fp or vector constant word %u", i);
9047 break;
9050 case dw_val_class_flag:
9051 if (dwarf_version >= 4)
9053 /* Currently all add_AT_flag calls pass in 1 as last argument,
9054 so DW_FORM_flag_present can be used. If that ever changes,
9055 we'll need to use DW_FORM_flag and have some optimization
9056 in build_abbrev_table that will change those to
9057 DW_FORM_flag_present if it is set to 1 in all DIEs using
9058 the same abbrev entry. */
9059 gcc_assert (AT_flag (a) == 1);
9060 if (flag_debug_asm)
9061 fprintf (asm_out_file, "\t\t\t%s %s\n",
9062 ASM_COMMENT_START, name);
9063 break;
9065 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9066 break;
9068 case dw_val_class_loc_list:
9069 output_attr_index_or_value (a);
9070 break;
9072 case dw_val_class_die_ref:
9073 if (AT_ref_external (a))
9075 if (AT_ref (a)->comdat_type_p)
9077 comdat_type_node *type_node =
9078 AT_ref (a)->die_id.die_type_node;
9080 gcc_assert (type_node);
9081 output_signature (type_node->signature, name);
9083 else
9085 const char *sym = AT_ref (a)->die_id.die_symbol;
9086 int size;
9088 gcc_assert (sym);
9089 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9090 length, whereas in DWARF3 it's always sized as an
9091 offset. */
9092 if (dwarf_version == 2)
9093 size = DWARF2_ADDR_SIZE;
9094 else
9095 size = DWARF_OFFSET_SIZE;
9096 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9097 name);
9100 else
9102 gcc_assert (AT_ref (a)->die_offset);
9103 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9104 "%s", name);
9106 break;
9108 case dw_val_class_fde_ref:
9110 char l1[20];
9112 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9113 a->dw_attr_val.v.val_fde_index * 2);
9114 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9115 "%s", name);
9117 break;
9119 case dw_val_class_vms_delta:
9120 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9121 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9122 AT_vms_delta2 (a), AT_vms_delta1 (a),
9123 "%s", name);
9124 #else
9125 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9126 AT_vms_delta2 (a), AT_vms_delta1 (a),
9127 "%s", name);
9128 #endif
9129 break;
9131 case dw_val_class_lbl_id:
9132 output_attr_index_or_value (a);
9133 break;
9135 case dw_val_class_lineptr:
9136 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9137 debug_line_section, "%s", name);
9138 break;
9140 case dw_val_class_macptr:
9141 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9142 debug_macinfo_section, "%s", name);
9143 break;
9145 case dw_val_class_str:
9146 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9147 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9148 a->dw_attr_val.v.val_str->label,
9149 debug_str_section,
9150 "%s: \"%s\"", name, AT_string (a));
9151 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9152 dw2_asm_output_data_uleb128 (AT_index (a),
9153 "%s: \"%s\"", name, AT_string (a));
9154 else
9155 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9156 break;
9158 case dw_val_class_file:
9160 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9162 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9163 a->dw_attr_val.v.val_file->filename);
9164 break;
9167 case dw_val_class_data8:
9169 int i;
9171 for (i = 0; i < 8; i++)
9172 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9173 i == 0 ? "%s" : NULL, name);
9174 break;
9177 case dw_val_class_high_pc:
9178 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9179 get_AT_low_pc (die), "DW_AT_high_pc");
9180 break;
9182 default:
9183 gcc_unreachable ();
9187 FOR_EACH_CHILD (die, c, output_die (c));
9189 /* Add null byte to terminate sibling list. */
9190 if (die->die_child != NULL)
9191 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9192 (unsigned long) die->die_offset);
9195 /* Output the compilation unit that appears at the beginning of the
9196 .debug_info section, and precedes the DIE descriptions. */
9198 static void
9199 output_compilation_unit_header (void)
9201 /* We don't support actual DWARFv5 units yet, we just use some
9202 DWARFv5 draft DIE tags in DWARFv4 format. */
9203 int ver = dwarf_version < 5 ? dwarf_version : 4;
9205 if (!XCOFF_DEBUGGING_INFO)
9207 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9208 dw2_asm_output_data (4, 0xffffffff,
9209 "Initial length escape value indicating 64-bit DWARF extension");
9210 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9211 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9212 "Length of Compilation Unit Info");
9215 dw2_asm_output_data (2, ver, "DWARF version number");
9216 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9217 debug_abbrev_section,
9218 "Offset Into Abbrev. Section");
9219 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9222 /* Output the compilation unit DIE and its children. */
9224 static void
9225 output_comp_unit (dw_die_ref die, int output_if_empty)
9227 const char *secname, *oldsym;
9228 char *tmp;
9230 /* Unless we are outputting main CU, we may throw away empty ones. */
9231 if (!output_if_empty && die->die_child == NULL)
9232 return;
9234 /* Even if there are no children of this DIE, we must output the information
9235 about the compilation unit. Otherwise, on an empty translation unit, we
9236 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9237 will then complain when examining the file. First mark all the DIEs in
9238 this CU so we know which get local refs. */
9239 mark_dies (die);
9241 external_ref_hash_type *extern_map = optimize_external_refs (die);
9243 build_abbrev_table (die, extern_map);
9245 delete extern_map;
9247 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9248 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9249 calc_die_sizes (die);
9251 oldsym = die->die_id.die_symbol;
9252 if (oldsym)
9254 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9256 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9257 secname = tmp;
9258 die->die_id.die_symbol = NULL;
9259 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9261 else
9263 switch_to_section (debug_info_section);
9264 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9265 info_section_emitted = true;
9268 /* Output debugging information. */
9269 output_compilation_unit_header ();
9270 output_die (die);
9272 /* Leave the marks on the main CU, so we can check them in
9273 output_pubnames. */
9274 if (oldsym)
9276 unmark_dies (die);
9277 die->die_id.die_symbol = oldsym;
9281 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9282 and .debug_pubtypes. This is configured per-target, but can be
9283 overridden by the -gpubnames or -gno-pubnames options. */
9285 static inline bool
9286 want_pubnames (void)
9288 if (debug_info_level <= DINFO_LEVEL_TERSE)
9289 return false;
9290 if (debug_generate_pub_sections != -1)
9291 return debug_generate_pub_sections;
9292 return targetm.want_debug_pub_sections;
9295 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9297 static void
9298 add_AT_pubnames (dw_die_ref die)
9300 if (want_pubnames ())
9301 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9304 /* Add a string attribute value to a skeleton DIE. */
9306 static inline void
9307 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9308 const char *str)
9310 dw_attr_node attr;
9311 struct indirect_string_node *node;
9313 if (! skeleton_debug_str_hash)
9314 skeleton_debug_str_hash
9315 = hash_table<indirect_string_hasher>::create_ggc (10);
9317 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9318 find_string_form (node);
9319 if (node->form == DW_FORM_GNU_str_index)
9320 node->form = DW_FORM_strp;
9322 attr.dw_attr = attr_kind;
9323 attr.dw_attr_val.val_class = dw_val_class_str;
9324 attr.dw_attr_val.val_entry = NULL;
9325 attr.dw_attr_val.v.val_str = node;
9326 add_dwarf_attr (die, &attr);
9329 /* Helper function to generate top-level dies for skeleton debug_info and
9330 debug_types. */
9332 static void
9333 add_top_level_skeleton_die_attrs (dw_die_ref die)
9335 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9336 const char *comp_dir = comp_dir_string ();
9338 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9339 if (comp_dir != NULL)
9340 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9341 add_AT_pubnames (die);
9342 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9345 /* Output skeleton debug sections that point to the dwo file. */
9347 static void
9348 output_skeleton_debug_sections (dw_die_ref comp_unit)
9350 /* We don't support actual DWARFv5 units yet, we just use some
9351 DWARFv5 draft DIE tags in DWARFv4 format. */
9352 int ver = dwarf_version < 5 ? dwarf_version : 4;
9354 /* These attributes will be found in the full debug_info section. */
9355 remove_AT (comp_unit, DW_AT_producer);
9356 remove_AT (comp_unit, DW_AT_language);
9358 switch_to_section (debug_skeleton_info_section);
9359 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9361 /* Produce the skeleton compilation-unit header. This one differs enough from
9362 a normal CU header that it's better not to call output_compilation_unit
9363 header. */
9364 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9365 dw2_asm_output_data (4, 0xffffffff,
9366 "Initial length escape value indicating 64-bit DWARF extension");
9368 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9369 DWARF_COMPILE_UNIT_HEADER_SIZE
9370 - DWARF_INITIAL_LENGTH_SIZE
9371 + size_of_die (comp_unit),
9372 "Length of Compilation Unit Info");
9373 dw2_asm_output_data (2, ver, "DWARF version number");
9374 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9375 debug_abbrev_section,
9376 "Offset Into Abbrev. Section");
9377 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9379 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9380 output_die (comp_unit);
9382 /* Build the skeleton debug_abbrev section. */
9383 switch_to_section (debug_skeleton_abbrev_section);
9384 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9386 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9388 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9391 /* Output a comdat type unit DIE and its children. */
9393 static void
9394 output_comdat_type_unit (comdat_type_node *node)
9396 const char *secname;
9397 char *tmp;
9398 int i;
9399 #if defined (OBJECT_FORMAT_ELF)
9400 tree comdat_key;
9401 #endif
9403 /* First mark all the DIEs in this CU so we know which get local refs. */
9404 mark_dies (node->root_die);
9406 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9408 build_abbrev_table (node->root_die, extern_map);
9410 delete extern_map;
9411 extern_map = NULL;
9413 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9414 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9415 calc_die_sizes (node->root_die);
9417 #if defined (OBJECT_FORMAT_ELF)
9418 if (!dwarf_split_debug_info)
9419 secname = ".debug_types";
9420 else
9421 secname = ".debug_types.dwo";
9423 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9424 sprintf (tmp, "wt.");
9425 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9426 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9427 comdat_key = get_identifier (tmp);
9428 targetm.asm_out.named_section (secname,
9429 SECTION_DEBUG | SECTION_LINKONCE,
9430 comdat_key);
9431 #else
9432 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9433 sprintf (tmp, ".gnu.linkonce.wt.");
9434 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9435 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9436 secname = tmp;
9437 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9438 #endif
9440 /* Output debugging information. */
9441 output_compilation_unit_header ();
9442 output_signature (node->signature, "Type Signature");
9443 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9444 "Offset to Type DIE");
9445 output_die (node->root_die);
9447 unmark_dies (node->root_die);
9450 /* Return the DWARF2/3 pubname associated with a decl. */
9452 static const char *
9453 dwarf2_name (tree decl, int scope)
9455 if (DECL_NAMELESS (decl))
9456 return NULL;
9457 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9460 /* Add a new entry to .debug_pubnames if appropriate. */
9462 static void
9463 add_pubname_string (const char *str, dw_die_ref die)
9465 pubname_entry e;
9467 e.die = die;
9468 e.name = xstrdup (str);
9469 vec_safe_push (pubname_table, e);
9472 static void
9473 add_pubname (tree decl, dw_die_ref die)
9475 if (!want_pubnames ())
9476 return;
9478 /* Don't add items to the table when we expect that the consumer will have
9479 just read the enclosing die. For example, if the consumer is looking at a
9480 class_member, it will either be inside the class already, or will have just
9481 looked up the class to find the member. Either way, searching the class is
9482 faster than searching the index. */
9483 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9484 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9486 const char *name = dwarf2_name (decl, 1);
9488 if (name)
9489 add_pubname_string (name, die);
9493 /* Add an enumerator to the pubnames section. */
9495 static void
9496 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9498 pubname_entry e;
9500 gcc_assert (scope_name);
9501 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9502 e.die = die;
9503 vec_safe_push (pubname_table, e);
9506 /* Add a new entry to .debug_pubtypes if appropriate. */
9508 static void
9509 add_pubtype (tree decl, dw_die_ref die)
9511 pubname_entry e;
9513 if (!want_pubnames ())
9514 return;
9516 if ((TREE_PUBLIC (decl)
9517 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9518 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9520 tree scope = NULL;
9521 const char *scope_name = "";
9522 const char *sep = is_cxx () ? "::" : ".";
9523 const char *name;
9525 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9526 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9528 scope_name = lang_hooks.dwarf_name (scope, 1);
9529 if (scope_name != NULL && scope_name[0] != '\0')
9530 scope_name = concat (scope_name, sep, NULL);
9531 else
9532 scope_name = "";
9535 if (TYPE_P (decl))
9536 name = type_tag (decl);
9537 else
9538 name = lang_hooks.dwarf_name (decl, 1);
9540 /* If we don't have a name for the type, there's no point in adding
9541 it to the table. */
9542 if (name != NULL && name[0] != '\0')
9544 e.die = die;
9545 e.name = concat (scope_name, name, NULL);
9546 vec_safe_push (pubtype_table, e);
9549 /* Although it might be more consistent to add the pubinfo for the
9550 enumerators as their dies are created, they should only be added if the
9551 enum type meets the criteria above. So rather than re-check the parent
9552 enum type whenever an enumerator die is created, just output them all
9553 here. This isn't protected by the name conditional because anonymous
9554 enums don't have names. */
9555 if (die->die_tag == DW_TAG_enumeration_type)
9557 dw_die_ref c;
9559 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9564 /* Output a single entry in the pubnames table. */
9566 static void
9567 output_pubname (dw_offset die_offset, pubname_entry *entry)
9569 dw_die_ref die = entry->die;
9570 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9572 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9574 if (debug_generate_pub_sections == 2)
9576 /* This logic follows gdb's method for determining the value of the flag
9577 byte. */
9578 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9579 switch (die->die_tag)
9581 case DW_TAG_typedef:
9582 case DW_TAG_base_type:
9583 case DW_TAG_subrange_type:
9584 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9585 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9586 break;
9587 case DW_TAG_enumerator:
9588 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9589 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9590 if (!is_cxx () && !is_java ())
9591 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9592 break;
9593 case DW_TAG_subprogram:
9594 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9595 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9596 if (!is_ada ())
9597 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9598 break;
9599 case DW_TAG_constant:
9600 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9601 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9602 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9603 break;
9604 case DW_TAG_variable:
9605 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9606 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9607 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9608 break;
9609 case DW_TAG_namespace:
9610 case DW_TAG_imported_declaration:
9611 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9612 break;
9613 case DW_TAG_class_type:
9614 case DW_TAG_interface_type:
9615 case DW_TAG_structure_type:
9616 case DW_TAG_union_type:
9617 case DW_TAG_enumeration_type:
9618 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9619 if (!is_cxx () && !is_java ())
9620 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9621 break;
9622 default:
9623 /* An unusual tag. Leave the flag-byte empty. */
9624 break;
9626 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9627 "GDB-index flags");
9630 dw2_asm_output_nstring (entry->name, -1, "external name");
9634 /* Output the public names table used to speed up access to externally
9635 visible names; or the public types table used to find type definitions. */
9637 static void
9638 output_pubnames (vec<pubname_entry, va_gc> *names)
9640 unsigned i;
9641 unsigned long pubnames_length = size_of_pubnames (names);
9642 pubname_entry *pub;
9644 if (!XCOFF_DEBUGGING_INFO)
9646 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9647 dw2_asm_output_data (4, 0xffffffff,
9648 "Initial length escape value indicating 64-bit DWARF extension");
9649 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9650 "Pub Info Length");
9653 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9654 dw2_asm_output_data (2, 2, "DWARF Version");
9656 if (dwarf_split_debug_info)
9657 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9658 debug_skeleton_info_section,
9659 "Offset of Compilation Unit Info");
9660 else
9661 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9662 debug_info_section,
9663 "Offset of Compilation Unit Info");
9664 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9665 "Compilation Unit Length");
9667 FOR_EACH_VEC_ELT (*names, i, pub)
9669 if (include_pubname_in_output (names, pub))
9671 dw_offset die_offset = pub->die->die_offset;
9673 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9674 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9675 gcc_assert (pub->die->die_mark);
9677 /* If we're putting types in their own .debug_types sections,
9678 the .debug_pubtypes table will still point to the compile
9679 unit (not the type unit), so we want to use the offset of
9680 the skeleton DIE (if there is one). */
9681 if (pub->die->comdat_type_p && names == pubtype_table)
9683 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9685 if (type_node != NULL)
9686 die_offset = (type_node->skeleton_die != NULL
9687 ? type_node->skeleton_die->die_offset
9688 : comp_unit_die ()->die_offset);
9691 output_pubname (die_offset, pub);
9695 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9698 /* Output public names and types tables if necessary. */
9700 static void
9701 output_pubtables (void)
9703 if (!want_pubnames () || !info_section_emitted)
9704 return;
9706 switch_to_section (debug_pubnames_section);
9707 output_pubnames (pubname_table);
9708 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9709 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9710 simply won't look for the section. */
9711 switch_to_section (debug_pubtypes_section);
9712 output_pubnames (pubtype_table);
9716 /* Output the information that goes into the .debug_aranges table.
9717 Namely, define the beginning and ending address range of the
9718 text section generated for this compilation unit. */
9720 static void
9721 output_aranges (void)
9723 unsigned i;
9724 unsigned long aranges_length = size_of_aranges ();
9726 if (!XCOFF_DEBUGGING_INFO)
9728 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9729 dw2_asm_output_data (4, 0xffffffff,
9730 "Initial length escape value indicating 64-bit DWARF extension");
9731 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9732 "Length of Address Ranges Info");
9735 /* Version number for aranges is still 2, even up to DWARF5. */
9736 dw2_asm_output_data (2, 2, "DWARF Version");
9737 if (dwarf_split_debug_info)
9738 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9739 debug_skeleton_info_section,
9740 "Offset of Compilation Unit Info");
9741 else
9742 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9743 debug_info_section,
9744 "Offset of Compilation Unit Info");
9745 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9746 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9748 /* We need to align to twice the pointer size here. */
9749 if (DWARF_ARANGES_PAD_SIZE)
9751 /* Pad using a 2 byte words so that padding is correct for any
9752 pointer size. */
9753 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9754 2 * DWARF2_ADDR_SIZE);
9755 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9756 dw2_asm_output_data (2, 0, NULL);
9759 /* It is necessary not to output these entries if the sections were
9760 not used; if the sections were not used, the length will be 0 and
9761 the address may end up as 0 if the section is discarded by ld
9762 --gc-sections, leaving an invalid (0, 0) entry that can be
9763 confused with the terminator. */
9764 if (text_section_used)
9766 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9767 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9768 text_section_label, "Length");
9770 if (cold_text_section_used)
9772 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9773 "Address");
9774 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9775 cold_text_section_label, "Length");
9778 if (have_multiple_function_sections)
9780 unsigned fde_idx;
9781 dw_fde_ref fde;
9783 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9785 if (DECL_IGNORED_P (fde->decl))
9786 continue;
9787 if (!fde->in_std_section)
9789 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9790 "Address");
9791 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9792 fde->dw_fde_begin, "Length");
9794 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9796 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9797 "Address");
9798 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9799 fde->dw_fde_second_begin, "Length");
9804 /* Output the terminator words. */
9805 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9806 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9809 /* Add a new entry to .debug_ranges. Return the offset at which it
9810 was placed. */
9812 static unsigned int
9813 add_ranges_num (int num)
9815 unsigned int in_use = ranges_table_in_use;
9817 if (in_use == ranges_table_allocated)
9819 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9820 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
9821 ranges_table_allocated);
9822 memset (ranges_table + ranges_table_in_use, 0,
9823 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
9826 ranges_table[in_use].num = num;
9827 ranges_table_in_use = in_use + 1;
9829 return in_use * 2 * DWARF2_ADDR_SIZE;
9832 /* Add a new entry to .debug_ranges corresponding to a block, or a
9833 range terminator if BLOCK is NULL. */
9835 static unsigned int
9836 add_ranges (const_tree block)
9838 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9841 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9842 When using dwarf_split_debug_info, address attributes in dies destined
9843 for the final executable should be direct references--setting the
9844 parameter force_direct ensures this behavior. */
9846 static void
9847 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9848 bool *added, bool force_direct)
9850 unsigned int in_use = ranges_by_label_in_use;
9851 unsigned int offset;
9853 if (in_use == ranges_by_label_allocated)
9855 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9856 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
9857 ranges_by_label_allocated);
9858 memset (ranges_by_label + ranges_by_label_in_use, 0,
9859 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
9862 ranges_by_label[in_use].begin = begin;
9863 ranges_by_label[in_use].end = end;
9864 ranges_by_label_in_use = in_use + 1;
9866 offset = add_ranges_num (-(int)in_use - 1);
9867 if (!*added)
9869 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9870 *added = true;
9874 static void
9875 output_ranges (void)
9877 unsigned i;
9878 static const char *const start_fmt = "Offset %#x";
9879 const char *fmt = start_fmt;
9881 for (i = 0; i < ranges_table_in_use; i++)
9883 int block_num = ranges_table[i].num;
9885 if (block_num > 0)
9887 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9888 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9890 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9891 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9893 /* If all code is in the text section, then the compilation
9894 unit base address defaults to DW_AT_low_pc, which is the
9895 base of the text section. */
9896 if (!have_multiple_function_sections)
9898 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9899 text_section_label,
9900 fmt, i * 2 * DWARF2_ADDR_SIZE);
9901 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9902 text_section_label, NULL);
9905 /* Otherwise, the compilation unit base address is zero,
9906 which allows us to use absolute addresses, and not worry
9907 about whether the target supports cross-section
9908 arithmetic. */
9909 else
9911 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9912 fmt, i * 2 * DWARF2_ADDR_SIZE);
9913 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9916 fmt = NULL;
9919 /* Negative block_num stands for an index into ranges_by_label. */
9920 else if (block_num < 0)
9922 int lab_idx = - block_num - 1;
9924 if (!have_multiple_function_sections)
9926 gcc_unreachable ();
9927 #if 0
9928 /* If we ever use add_ranges_by_labels () for a single
9929 function section, all we have to do is to take out
9930 the #if 0 above. */
9931 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9932 ranges_by_label[lab_idx].begin,
9933 text_section_label,
9934 fmt, i * 2 * DWARF2_ADDR_SIZE);
9935 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9936 ranges_by_label[lab_idx].end,
9937 text_section_label, NULL);
9938 #endif
9940 else
9942 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9943 ranges_by_label[lab_idx].begin,
9944 fmt, i * 2 * DWARF2_ADDR_SIZE);
9945 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9946 ranges_by_label[lab_idx].end,
9947 NULL);
9950 else
9952 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9953 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9954 fmt = start_fmt;
9959 /* Data structure containing information about input files. */
9960 struct file_info
9962 const char *path; /* Complete file name. */
9963 const char *fname; /* File name part. */
9964 int length; /* Length of entire string. */
9965 struct dwarf_file_data * file_idx; /* Index in input file table. */
9966 int dir_idx; /* Index in directory table. */
9969 /* Data structure containing information about directories with source
9970 files. */
9971 struct dir_info
9973 const char *path; /* Path including directory name. */
9974 int length; /* Path length. */
9975 int prefix; /* Index of directory entry which is a prefix. */
9976 int count; /* Number of files in this directory. */
9977 int dir_idx; /* Index of directory used as base. */
9980 /* Callback function for file_info comparison. We sort by looking at
9981 the directories in the path. */
9983 static int
9984 file_info_cmp (const void *p1, const void *p2)
9986 const struct file_info *const s1 = (const struct file_info *) p1;
9987 const struct file_info *const s2 = (const struct file_info *) p2;
9988 const unsigned char *cp1;
9989 const unsigned char *cp2;
9991 /* Take care of file names without directories. We need to make sure that
9992 we return consistent values to qsort since some will get confused if
9993 we return the same value when identical operands are passed in opposite
9994 orders. So if neither has a directory, return 0 and otherwise return
9995 1 or -1 depending on which one has the directory. */
9996 if ((s1->path == s1->fname || s2->path == s2->fname))
9997 return (s2->path == s2->fname) - (s1->path == s1->fname);
9999 cp1 = (const unsigned char *) s1->path;
10000 cp2 = (const unsigned char *) s2->path;
10002 while (1)
10004 ++cp1;
10005 ++cp2;
10006 /* Reached the end of the first path? If so, handle like above. */
10007 if ((cp1 == (const unsigned char *) s1->fname)
10008 || (cp2 == (const unsigned char *) s2->fname))
10009 return ((cp2 == (const unsigned char *) s2->fname)
10010 - (cp1 == (const unsigned char *) s1->fname));
10012 /* Character of current path component the same? */
10013 else if (*cp1 != *cp2)
10014 return *cp1 - *cp2;
10018 struct file_name_acquire_data
10020 struct file_info *files;
10021 int used_files;
10022 int max_files;
10025 /* Traversal function for the hash table. */
10028 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10030 struct dwarf_file_data *d = *slot;
10031 struct file_info *fi;
10032 const char *f;
10034 gcc_assert (fnad->max_files >= d->emitted_number);
10036 if (! d->emitted_number)
10037 return 1;
10039 gcc_assert (fnad->max_files != fnad->used_files);
10041 fi = fnad->files + fnad->used_files++;
10043 /* Skip all leading "./". */
10044 f = d->filename;
10045 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10046 f += 2;
10048 /* Create a new array entry. */
10049 fi->path = f;
10050 fi->length = strlen (f);
10051 fi->file_idx = d;
10053 /* Search for the file name part. */
10054 f = strrchr (f, DIR_SEPARATOR);
10055 #if defined (DIR_SEPARATOR_2)
10057 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10059 if (g != NULL)
10061 if (f == NULL || f < g)
10062 f = g;
10065 #endif
10067 fi->fname = f == NULL ? fi->path : f + 1;
10068 return 1;
10071 /* Output the directory table and the file name table. We try to minimize
10072 the total amount of memory needed. A heuristic is used to avoid large
10073 slowdowns with many input files. */
10075 static void
10076 output_file_names (void)
10078 struct file_name_acquire_data fnad;
10079 int numfiles;
10080 struct file_info *files;
10081 struct dir_info *dirs;
10082 int *saved;
10083 int *savehere;
10084 int *backmap;
10085 int ndirs;
10086 int idx_offset;
10087 int i;
10089 if (!last_emitted_file)
10091 dw2_asm_output_data (1, 0, "End directory table");
10092 dw2_asm_output_data (1, 0, "End file name table");
10093 return;
10096 numfiles = last_emitted_file->emitted_number;
10098 /* Allocate the various arrays we need. */
10099 files = XALLOCAVEC (struct file_info, numfiles);
10100 dirs = XALLOCAVEC (struct dir_info, numfiles);
10102 fnad.files = files;
10103 fnad.used_files = 0;
10104 fnad.max_files = numfiles;
10105 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10106 gcc_assert (fnad.used_files == fnad.max_files);
10108 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10110 /* Find all the different directories used. */
10111 dirs[0].path = files[0].path;
10112 dirs[0].length = files[0].fname - files[0].path;
10113 dirs[0].prefix = -1;
10114 dirs[0].count = 1;
10115 dirs[0].dir_idx = 0;
10116 files[0].dir_idx = 0;
10117 ndirs = 1;
10119 for (i = 1; i < numfiles; i++)
10120 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10121 && memcmp (dirs[ndirs - 1].path, files[i].path,
10122 dirs[ndirs - 1].length) == 0)
10124 /* Same directory as last entry. */
10125 files[i].dir_idx = ndirs - 1;
10126 ++dirs[ndirs - 1].count;
10128 else
10130 int j;
10132 /* This is a new directory. */
10133 dirs[ndirs].path = files[i].path;
10134 dirs[ndirs].length = files[i].fname - files[i].path;
10135 dirs[ndirs].count = 1;
10136 dirs[ndirs].dir_idx = ndirs;
10137 files[i].dir_idx = ndirs;
10139 /* Search for a prefix. */
10140 dirs[ndirs].prefix = -1;
10141 for (j = 0; j < ndirs; j++)
10142 if (dirs[j].length < dirs[ndirs].length
10143 && dirs[j].length > 1
10144 && (dirs[ndirs].prefix == -1
10145 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10146 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10147 dirs[ndirs].prefix = j;
10149 ++ndirs;
10152 /* Now to the actual work. We have to find a subset of the directories which
10153 allow expressing the file name using references to the directory table
10154 with the least amount of characters. We do not do an exhaustive search
10155 where we would have to check out every combination of every single
10156 possible prefix. Instead we use a heuristic which provides nearly optimal
10157 results in most cases and never is much off. */
10158 saved = XALLOCAVEC (int, ndirs);
10159 savehere = XALLOCAVEC (int, ndirs);
10161 memset (saved, '\0', ndirs * sizeof (saved[0]));
10162 for (i = 0; i < ndirs; i++)
10164 int j;
10165 int total;
10167 /* We can always save some space for the current directory. But this
10168 does not mean it will be enough to justify adding the directory. */
10169 savehere[i] = dirs[i].length;
10170 total = (savehere[i] - saved[i]) * dirs[i].count;
10172 for (j = i + 1; j < ndirs; j++)
10174 savehere[j] = 0;
10175 if (saved[j] < dirs[i].length)
10177 /* Determine whether the dirs[i] path is a prefix of the
10178 dirs[j] path. */
10179 int k;
10181 k = dirs[j].prefix;
10182 while (k != -1 && k != (int) i)
10183 k = dirs[k].prefix;
10185 if (k == (int) i)
10187 /* Yes it is. We can possibly save some memory by
10188 writing the filenames in dirs[j] relative to
10189 dirs[i]. */
10190 savehere[j] = dirs[i].length;
10191 total += (savehere[j] - saved[j]) * dirs[j].count;
10196 /* Check whether we can save enough to justify adding the dirs[i]
10197 directory. */
10198 if (total > dirs[i].length + 1)
10200 /* It's worthwhile adding. */
10201 for (j = i; j < ndirs; j++)
10202 if (savehere[j] > 0)
10204 /* Remember how much we saved for this directory so far. */
10205 saved[j] = savehere[j];
10207 /* Remember the prefix directory. */
10208 dirs[j].dir_idx = i;
10213 /* Emit the directory name table. */
10214 idx_offset = dirs[0].length > 0 ? 1 : 0;
10215 for (i = 1 - idx_offset; i < ndirs; i++)
10216 dw2_asm_output_nstring (dirs[i].path,
10217 dirs[i].length
10218 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10219 "Directory Entry: %#x", i + idx_offset);
10221 dw2_asm_output_data (1, 0, "End directory table");
10223 /* We have to emit them in the order of emitted_number since that's
10224 used in the debug info generation. To do this efficiently we
10225 generate a back-mapping of the indices first. */
10226 backmap = XALLOCAVEC (int, numfiles);
10227 for (i = 0; i < numfiles; i++)
10228 backmap[files[i].file_idx->emitted_number - 1] = i;
10230 /* Now write all the file names. */
10231 for (i = 0; i < numfiles; i++)
10233 int file_idx = backmap[i];
10234 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10236 #ifdef VMS_DEBUGGING_INFO
10237 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10239 /* Setting these fields can lead to debugger miscomparisons,
10240 but VMS Debug requires them to be set correctly. */
10242 int ver;
10243 long long cdt;
10244 long siz;
10245 int maxfilelen = strlen (files[file_idx].path)
10246 + dirs[dir_idx].length
10247 + MAX_VMS_VERSION_LEN + 1;
10248 char *filebuf = XALLOCAVEC (char, maxfilelen);
10250 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10251 snprintf (filebuf, maxfilelen, "%s;%d",
10252 files[file_idx].path + dirs[dir_idx].length, ver);
10254 dw2_asm_output_nstring
10255 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10257 /* Include directory index. */
10258 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10260 /* Modification time. */
10261 dw2_asm_output_data_uleb128
10262 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10263 ? cdt : 0,
10264 NULL);
10266 /* File length in bytes. */
10267 dw2_asm_output_data_uleb128
10268 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10269 ? siz : 0,
10270 NULL);
10271 #else
10272 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10273 "File Entry: %#x", (unsigned) i + 1);
10275 /* Include directory index. */
10276 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10278 /* Modification time. */
10279 dw2_asm_output_data_uleb128 (0, NULL);
10281 /* File length in bytes. */
10282 dw2_asm_output_data_uleb128 (0, NULL);
10283 #endif /* VMS_DEBUGGING_INFO */
10286 dw2_asm_output_data (1, 0, "End file name table");
10290 /* Output one line number table into the .debug_line section. */
10292 static void
10293 output_one_line_info_table (dw_line_info_table *table)
10295 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10296 unsigned int current_line = 1;
10297 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10298 dw_line_info_entry *ent;
10299 size_t i;
10301 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10303 switch (ent->opcode)
10305 case LI_set_address:
10306 /* ??? Unfortunately, we have little choice here currently, and
10307 must always use the most general form. GCC does not know the
10308 address delta itself, so we can't use DW_LNS_advance_pc. Many
10309 ports do have length attributes which will give an upper bound
10310 on the address range. We could perhaps use length attributes
10311 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10312 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10314 /* This can handle any delta. This takes
10315 4+DWARF2_ADDR_SIZE bytes. */
10316 dw2_asm_output_data (1, 0, "set address %s", line_label);
10317 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10318 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10319 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10320 break;
10322 case LI_set_line:
10323 if (ent->val == current_line)
10325 /* We still need to start a new row, so output a copy insn. */
10326 dw2_asm_output_data (1, DW_LNS_copy,
10327 "copy line %u", current_line);
10329 else
10331 int line_offset = ent->val - current_line;
10332 int line_delta = line_offset - DWARF_LINE_BASE;
10334 current_line = ent->val;
10335 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10337 /* This can handle deltas from -10 to 234, using the current
10338 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10339 This takes 1 byte. */
10340 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10341 "line %u", current_line);
10343 else
10345 /* This can handle any delta. This takes at least 4 bytes,
10346 depending on the value being encoded. */
10347 dw2_asm_output_data (1, DW_LNS_advance_line,
10348 "advance to line %u", current_line);
10349 dw2_asm_output_data_sleb128 (line_offset, NULL);
10350 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10353 break;
10355 case LI_set_file:
10356 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10357 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10358 break;
10360 case LI_set_column:
10361 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10362 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10363 break;
10365 case LI_negate_stmt:
10366 current_is_stmt = !current_is_stmt;
10367 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10368 "is_stmt %d", current_is_stmt);
10369 break;
10371 case LI_set_prologue_end:
10372 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10373 "set prologue end");
10374 break;
10376 case LI_set_epilogue_begin:
10377 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10378 "set epilogue begin");
10379 break;
10381 case LI_set_discriminator:
10382 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10383 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10384 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10385 dw2_asm_output_data_uleb128 (ent->val, NULL);
10386 break;
10390 /* Emit debug info for the address of the end of the table. */
10391 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10392 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10393 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10396 dw2_asm_output_data (1, 0, "end sequence");
10397 dw2_asm_output_data_uleb128 (1, NULL);
10398 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10401 /* Output the source line number correspondence information. This
10402 information goes into the .debug_line section. */
10404 static void
10405 output_line_info (bool prologue_only)
10407 char l1[20], l2[20], p1[20], p2[20];
10408 /* We don't support DWARFv5 line tables yet. */
10409 int ver = dwarf_version < 5 ? dwarf_version : 4;
10410 bool saw_one = false;
10411 int opc;
10413 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10414 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10415 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10416 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10418 if (!XCOFF_DEBUGGING_INFO)
10420 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10421 dw2_asm_output_data (4, 0xffffffff,
10422 "Initial length escape value indicating 64-bit DWARF extension");
10423 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10424 "Length of Source Line Info");
10427 ASM_OUTPUT_LABEL (asm_out_file, l1);
10429 dw2_asm_output_data (2, ver, "DWARF Version");
10430 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10431 ASM_OUTPUT_LABEL (asm_out_file, p1);
10433 /* Define the architecture-dependent minimum instruction length (in bytes).
10434 In this implementation of DWARF, this field is used for information
10435 purposes only. Since GCC generates assembly language, we have no
10436 a priori knowledge of how many instruction bytes are generated for each
10437 source line, and therefore can use only the DW_LNE_set_address and
10438 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10439 this as '1', which is "correct enough" for all architectures,
10440 and don't let the target override. */
10441 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10443 if (ver >= 4)
10444 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10445 "Maximum Operations Per Instruction");
10446 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10447 "Default is_stmt_start flag");
10448 dw2_asm_output_data (1, DWARF_LINE_BASE,
10449 "Line Base Value (Special Opcodes)");
10450 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10451 "Line Range Value (Special Opcodes)");
10452 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10453 "Special Opcode Base");
10455 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10457 int n_op_args;
10458 switch (opc)
10460 case DW_LNS_advance_pc:
10461 case DW_LNS_advance_line:
10462 case DW_LNS_set_file:
10463 case DW_LNS_set_column:
10464 case DW_LNS_fixed_advance_pc:
10465 case DW_LNS_set_isa:
10466 n_op_args = 1;
10467 break;
10468 default:
10469 n_op_args = 0;
10470 break;
10473 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10474 opc, n_op_args);
10477 /* Write out the information about the files we use. */
10478 output_file_names ();
10479 ASM_OUTPUT_LABEL (asm_out_file, p2);
10480 if (prologue_only)
10482 /* Output the marker for the end of the line number info. */
10483 ASM_OUTPUT_LABEL (asm_out_file, l2);
10484 return;
10487 if (separate_line_info)
10489 dw_line_info_table *table;
10490 size_t i;
10492 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10493 if (table->in_use)
10495 output_one_line_info_table (table);
10496 saw_one = true;
10499 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10501 output_one_line_info_table (cold_text_section_line_info);
10502 saw_one = true;
10505 /* ??? Some Darwin linkers crash on a .debug_line section with no
10506 sequences. Further, merely a DW_LNE_end_sequence entry is not
10507 sufficient -- the address column must also be initialized.
10508 Make sure to output at least one set_address/end_sequence pair,
10509 choosing .text since that section is always present. */
10510 if (text_section_line_info->in_use || !saw_one)
10511 output_one_line_info_table (text_section_line_info);
10513 /* Output the marker for the end of the line number info. */
10514 ASM_OUTPUT_LABEL (asm_out_file, l2);
10517 /* Given a pointer to a tree node for some base type, return a pointer to
10518 a DIE that describes the given type.
10520 This routine must only be called for GCC type nodes that correspond to
10521 Dwarf base (fundamental) types. */
10523 static dw_die_ref
10524 base_type_die (tree type)
10526 dw_die_ref base_type_result;
10527 enum dwarf_type encoding;
10529 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10530 return 0;
10532 /* If this is a subtype that should not be emitted as a subrange type,
10533 use the base type. See subrange_type_for_debug_p. */
10534 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10535 type = TREE_TYPE (type);
10537 switch (TREE_CODE (type))
10539 case INTEGER_TYPE:
10540 if ((dwarf_version >= 4 || !dwarf_strict)
10541 && TYPE_NAME (type)
10542 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10543 && DECL_IS_BUILTIN (TYPE_NAME (type))
10544 && DECL_NAME (TYPE_NAME (type)))
10546 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10547 if (strcmp (name, "char16_t") == 0
10548 || strcmp (name, "char32_t") == 0)
10550 encoding = DW_ATE_UTF;
10551 break;
10554 if (TYPE_STRING_FLAG (type))
10556 if (TYPE_UNSIGNED (type))
10557 encoding = DW_ATE_unsigned_char;
10558 else
10559 encoding = DW_ATE_signed_char;
10561 else if (TYPE_UNSIGNED (type))
10562 encoding = DW_ATE_unsigned;
10563 else
10564 encoding = DW_ATE_signed;
10565 break;
10567 case REAL_TYPE:
10568 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10570 if (dwarf_version >= 3 || !dwarf_strict)
10571 encoding = DW_ATE_decimal_float;
10572 else
10573 encoding = DW_ATE_lo_user;
10575 else
10576 encoding = DW_ATE_float;
10577 break;
10579 case FIXED_POINT_TYPE:
10580 if (!(dwarf_version >= 3 || !dwarf_strict))
10581 encoding = DW_ATE_lo_user;
10582 else if (TYPE_UNSIGNED (type))
10583 encoding = DW_ATE_unsigned_fixed;
10584 else
10585 encoding = DW_ATE_signed_fixed;
10586 break;
10588 /* Dwarf2 doesn't know anything about complex ints, so use
10589 a user defined type for it. */
10590 case COMPLEX_TYPE:
10591 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10592 encoding = DW_ATE_complex_float;
10593 else
10594 encoding = DW_ATE_lo_user;
10595 break;
10597 case BOOLEAN_TYPE:
10598 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10599 encoding = DW_ATE_boolean;
10600 break;
10602 default:
10603 /* No other TREE_CODEs are Dwarf fundamental types. */
10604 gcc_unreachable ();
10607 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10609 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10610 int_size_in_bytes (type));
10611 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10612 add_pubtype (type, base_type_result);
10614 return base_type_result;
10617 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10618 named 'auto' in its type: return true for it, false otherwise. */
10620 static inline bool
10621 is_cxx_auto (tree type)
10623 if (is_cxx ())
10625 tree name = TYPE_IDENTIFIER (type);
10626 if (name == get_identifier ("auto")
10627 || name == get_identifier ("decltype(auto)"))
10628 return true;
10630 return false;
10633 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10634 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10636 static inline int
10637 is_base_type (tree type)
10639 switch (TREE_CODE (type))
10641 case ERROR_MARK:
10642 case VOID_TYPE:
10643 case INTEGER_TYPE:
10644 case REAL_TYPE:
10645 case FIXED_POINT_TYPE:
10646 case COMPLEX_TYPE:
10647 case BOOLEAN_TYPE:
10648 case POINTER_BOUNDS_TYPE:
10649 return 1;
10651 case ARRAY_TYPE:
10652 case RECORD_TYPE:
10653 case UNION_TYPE:
10654 case QUAL_UNION_TYPE:
10655 case ENUMERAL_TYPE:
10656 case FUNCTION_TYPE:
10657 case METHOD_TYPE:
10658 case POINTER_TYPE:
10659 case REFERENCE_TYPE:
10660 case NULLPTR_TYPE:
10661 case OFFSET_TYPE:
10662 case LANG_TYPE:
10663 case VECTOR_TYPE:
10664 return 0;
10666 default:
10667 if (is_cxx_auto (type))
10668 return 0;
10669 gcc_unreachable ();
10672 return 0;
10675 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10676 node, return the size in bits for the type if it is a constant, or else
10677 return the alignment for the type if the type's size is not constant, or
10678 else return BITS_PER_WORD if the type actually turns out to be an
10679 ERROR_MARK node. */
10681 static inline unsigned HOST_WIDE_INT
10682 simple_type_size_in_bits (const_tree type)
10684 if (TREE_CODE (type) == ERROR_MARK)
10685 return BITS_PER_WORD;
10686 else if (TYPE_SIZE (type) == NULL_TREE)
10687 return 0;
10688 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10689 return tree_to_uhwi (TYPE_SIZE (type));
10690 else
10691 return TYPE_ALIGN (type);
10694 /* Similarly, but return an offset_int instead of UHWI. */
10696 static inline offset_int
10697 offset_int_type_size_in_bits (const_tree type)
10699 if (TREE_CODE (type) == ERROR_MARK)
10700 return BITS_PER_WORD;
10701 else if (TYPE_SIZE (type) == NULL_TREE)
10702 return 0;
10703 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10704 return wi::to_offset (TYPE_SIZE (type));
10705 else
10706 return TYPE_ALIGN (type);
10709 /* Given a pointer to a tree node for a subrange type, return a pointer
10710 to a DIE that describes the given type. */
10712 static dw_die_ref
10713 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10715 dw_die_ref subrange_die;
10716 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10718 if (context_die == NULL)
10719 context_die = comp_unit_die ();
10721 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10723 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10725 /* The size of the subrange type and its base type do not match,
10726 so we need to generate a size attribute for the subrange type. */
10727 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10730 if (low)
10731 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10732 if (high)
10733 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10735 return subrange_die;
10738 /* Returns the (const and/or volatile) cv_qualifiers associated with
10739 the decl node. This will normally be augmented with the
10740 cv_qualifiers of the underlying type in add_type_attribute. */
10742 static int
10743 decl_quals (const_tree decl)
10745 return ((TREE_READONLY (decl)
10746 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10747 | (TREE_THIS_VOLATILE (decl)
10748 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10751 /* Determine the TYPE whose qualifiers match the largest strict subset
10752 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10753 qualifiers outside QUAL_MASK. */
10755 static int
10756 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10758 tree t;
10759 int best_rank = 0, best_qual = 0, max_rank;
10761 type_quals &= qual_mask;
10762 max_rank = popcount_hwi (type_quals) - 1;
10764 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10765 t = TYPE_NEXT_VARIANT (t))
10767 int q = TYPE_QUALS (t) & qual_mask;
10769 if ((q & type_quals) == q && q != type_quals
10770 && check_base_type (t, type))
10772 int rank = popcount_hwi (q);
10774 if (rank > best_rank)
10776 best_rank = rank;
10777 best_qual = q;
10782 return best_qual;
10785 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10786 entry that chains various modifiers in front of the given type. */
10788 static dw_die_ref
10789 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10791 enum tree_code code = TREE_CODE (type);
10792 dw_die_ref mod_type_die;
10793 dw_die_ref sub_die = NULL;
10794 tree item_type = NULL;
10795 tree qualified_type;
10796 tree name, low, high;
10797 dw_die_ref mod_scope;
10798 /* Only these cv-qualifiers are currently handled. */
10799 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10800 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10802 if (code == ERROR_MARK)
10803 return NULL;
10805 cv_quals &= cv_qual_mask;
10807 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10808 tag modifier (and not an attribute) old consumers won't be able
10809 to handle it. */
10810 if (dwarf_version < 3)
10811 cv_quals &= ~TYPE_QUAL_RESTRICT;
10813 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10814 if (dwarf_version < 5)
10815 cv_quals &= ~TYPE_QUAL_ATOMIC;
10817 /* See if we already have the appropriately qualified variant of
10818 this type. */
10819 qualified_type = get_qualified_type (type, cv_quals);
10821 if (qualified_type == sizetype
10822 && TYPE_NAME (qualified_type)
10823 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10825 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10827 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10828 && TYPE_PRECISION (t)
10829 == TYPE_PRECISION (qualified_type)
10830 && TYPE_UNSIGNED (t)
10831 == TYPE_UNSIGNED (qualified_type));
10832 qualified_type = t;
10835 /* If we do, then we can just use its DIE, if it exists. */
10836 if (qualified_type)
10838 mod_type_die = lookup_type_die (qualified_type);
10839 if (mod_type_die)
10840 return mod_type_die;
10843 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10845 /* Handle C typedef types. */
10846 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10847 && !DECL_ARTIFICIAL (name))
10849 tree dtype = TREE_TYPE (name);
10851 if (qualified_type == dtype)
10853 /* For a named type, use the typedef. */
10854 gen_type_die (qualified_type, context_die);
10855 return lookup_type_die (qualified_type);
10857 else
10859 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10860 dquals &= cv_qual_mask;
10861 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10862 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10863 /* cv-unqualified version of named type. Just use
10864 the unnamed type to which it refers. */
10865 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10866 cv_quals, context_die);
10867 /* Else cv-qualified version of named type; fall through. */
10871 mod_scope = scope_die_for (type, context_die);
10873 if (cv_quals)
10875 struct qual_info { int q; enum dwarf_tag t; };
10876 static const struct qual_info qual_info[] =
10878 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10879 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10880 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10881 { TYPE_QUAL_CONST, DW_TAG_const_type },
10883 int sub_quals;
10884 unsigned i;
10886 /* Determine a lesser qualified type that most closely matches
10887 this one. Then generate DW_TAG_* entries for the remaining
10888 qualifiers. */
10889 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10890 cv_qual_mask);
10891 mod_type_die = modified_type_die (type, sub_quals, context_die);
10893 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10894 if (qual_info[i].q & cv_quals & ~sub_quals)
10896 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10897 if (mod_type_die)
10898 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10899 mod_type_die = d;
10902 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
10904 dwarf_tag tag = DW_TAG_pointer_type;
10905 if (code == REFERENCE_TYPE)
10907 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10908 tag = DW_TAG_rvalue_reference_type;
10909 else
10910 tag = DW_TAG_reference_type;
10912 mod_type_die = new_die (tag, mod_scope, type);
10914 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10915 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10916 item_type = TREE_TYPE (type);
10918 addr_space_t as = TYPE_ADDR_SPACE (item_type);
10919 if (!ADDR_SPACE_GENERIC_P (as))
10921 int action = targetm.addr_space.debug (as);
10922 if (action >= 0)
10924 /* Positive values indicate an address_class. */
10925 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
10927 else
10929 /* Negative values indicate an (inverted) segment base reg. */
10930 dw_loc_descr_ref d
10931 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
10932 add_AT_loc (mod_type_die, DW_AT_segment, d);
10936 else if (code == INTEGER_TYPE
10937 && TREE_TYPE (type) != NULL_TREE
10938 && subrange_type_for_debug_p (type, &low, &high))
10940 mod_type_die = subrange_type_die (type, low, high, context_die);
10941 item_type = TREE_TYPE (type);
10943 else if (is_base_type (type))
10944 mod_type_die = base_type_die (type);
10945 else
10947 gen_type_die (type, context_die);
10949 /* We have to get the type_main_variant here (and pass that to the
10950 `lookup_type_die' routine) because the ..._TYPE node we have
10951 might simply be a *copy* of some original type node (where the
10952 copy was created to help us keep track of typedef names) and
10953 that copy might have a different TYPE_UID from the original
10954 ..._TYPE node. */
10955 if (TREE_CODE (type) != VECTOR_TYPE)
10956 return lookup_type_die (type_main_variant (type));
10957 else
10958 /* Vectors have the debugging information in the type,
10959 not the main variant. */
10960 return lookup_type_die (type);
10963 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10964 don't output a DW_TAG_typedef, since there isn't one in the
10965 user's program; just attach a DW_AT_name to the type.
10966 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10967 if the base type already has the same name. */
10968 if (name
10969 && ((TREE_CODE (name) != TYPE_DECL
10970 && (qualified_type == TYPE_MAIN_VARIANT (type)
10971 || (cv_quals == TYPE_UNQUALIFIED)))
10972 || (TREE_CODE (name) == TYPE_DECL
10973 && TREE_TYPE (name) == qualified_type
10974 && DECL_NAME (name))))
10976 if (TREE_CODE (name) == TYPE_DECL)
10977 /* Could just call add_name_and_src_coords_attributes here,
10978 but since this is a builtin type it doesn't have any
10979 useful source coordinates anyway. */
10980 name = DECL_NAME (name);
10981 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10983 /* This probably indicates a bug. */
10984 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10986 name = TYPE_IDENTIFIER (type);
10987 add_name_attribute (mod_type_die,
10988 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10991 if (qualified_type)
10992 equate_type_number_to_die (qualified_type, mod_type_die);
10994 if (item_type)
10995 /* We must do this after the equate_type_number_to_die call, in case
10996 this is a recursive type. This ensures that the modified_type_die
10997 recursion will terminate even if the type is recursive. Recursive
10998 types are possible in Ada. */
10999 sub_die = modified_type_die (item_type,
11000 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11001 context_die);
11003 if (sub_die != NULL)
11004 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11006 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11007 if (TYPE_ARTIFICIAL (type))
11008 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11010 return mod_type_die;
11013 /* Generate DIEs for the generic parameters of T.
11014 T must be either a generic type or a generic function.
11015 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11017 static void
11018 gen_generic_params_dies (tree t)
11020 tree parms, args;
11021 int parms_num, i;
11022 dw_die_ref die = NULL;
11023 int non_default;
11025 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11026 return;
11028 if (TYPE_P (t))
11029 die = lookup_type_die (t);
11030 else if (DECL_P (t))
11031 die = lookup_decl_die (t);
11033 gcc_assert (die);
11035 parms = lang_hooks.get_innermost_generic_parms (t);
11036 if (!parms)
11037 /* T has no generic parameter. It means T is neither a generic type
11038 or function. End of story. */
11039 return;
11041 parms_num = TREE_VEC_LENGTH (parms);
11042 args = lang_hooks.get_innermost_generic_args (t);
11043 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11044 non_default = int_cst_value (TREE_CHAIN (args));
11045 else
11046 non_default = TREE_VEC_LENGTH (args);
11047 for (i = 0; i < parms_num; i++)
11049 tree parm, arg, arg_pack_elems;
11050 dw_die_ref parm_die;
11052 parm = TREE_VEC_ELT (parms, i);
11053 arg = TREE_VEC_ELT (args, i);
11054 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11055 gcc_assert (parm && TREE_VALUE (parm) && arg);
11057 if (parm && TREE_VALUE (parm) && arg)
11059 /* If PARM represents a template parameter pack,
11060 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11061 by DW_TAG_template_*_parameter DIEs for the argument
11062 pack elements of ARG. Note that ARG would then be
11063 an argument pack. */
11064 if (arg_pack_elems)
11065 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11066 arg_pack_elems,
11067 die);
11068 else
11069 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11070 true /* emit name */, die);
11071 if (i >= non_default)
11072 add_AT_flag (parm_die, DW_AT_default_value, 1);
11077 /* Create and return a DIE for PARM which should be
11078 the representation of a generic type parameter.
11079 For instance, in the C++ front end, PARM would be a template parameter.
11080 ARG is the argument to PARM.
11081 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11082 name of the PARM.
11083 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11084 as a child node. */
11086 static dw_die_ref
11087 generic_parameter_die (tree parm, tree arg,
11088 bool emit_name_p,
11089 dw_die_ref parent_die)
11091 dw_die_ref tmpl_die = NULL;
11092 const char *name = NULL;
11094 if (!parm || !DECL_NAME (parm) || !arg)
11095 return NULL;
11097 /* We support non-type generic parameters and arguments,
11098 type generic parameters and arguments, as well as
11099 generic generic parameters (a.k.a. template template parameters in C++)
11100 and arguments. */
11101 if (TREE_CODE (parm) == PARM_DECL)
11102 /* PARM is a nontype generic parameter */
11103 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11104 else if (TREE_CODE (parm) == TYPE_DECL)
11105 /* PARM is a type generic parameter. */
11106 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11107 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11108 /* PARM is a generic generic parameter.
11109 Its DIE is a GNU extension. It shall have a
11110 DW_AT_name attribute to represent the name of the template template
11111 parameter, and a DW_AT_GNU_template_name attribute to represent the
11112 name of the template template argument. */
11113 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11114 parent_die, parm);
11115 else
11116 gcc_unreachable ();
11118 if (tmpl_die)
11120 tree tmpl_type;
11122 /* If PARM is a generic parameter pack, it means we are
11123 emitting debug info for a template argument pack element.
11124 In other terms, ARG is a template argument pack element.
11125 In that case, we don't emit any DW_AT_name attribute for
11126 the die. */
11127 if (emit_name_p)
11129 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11130 gcc_assert (name);
11131 add_AT_string (tmpl_die, DW_AT_name, name);
11134 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11136 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11137 TMPL_DIE should have a child DW_AT_type attribute that is set
11138 to the type of the argument to PARM, which is ARG.
11139 If PARM is a type generic parameter, TMPL_DIE should have a
11140 child DW_AT_type that is set to ARG. */
11141 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11142 add_type_attribute (tmpl_die, tmpl_type,
11143 (TREE_THIS_VOLATILE (tmpl_type)
11144 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11145 parent_die);
11147 else
11149 /* So TMPL_DIE is a DIE representing a
11150 a generic generic template parameter, a.k.a template template
11151 parameter in C++ and arg is a template. */
11153 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11154 to the name of the argument. */
11155 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11156 if (name)
11157 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11160 if (TREE_CODE (parm) == PARM_DECL)
11161 /* So PARM is a non-type generic parameter.
11162 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11163 attribute of TMPL_DIE which value represents the value
11164 of ARG.
11165 We must be careful here:
11166 The value of ARG might reference some function decls.
11167 We might currently be emitting debug info for a generic
11168 type and types are emitted before function decls, we don't
11169 know if the function decls referenced by ARG will actually be
11170 emitted after cgraph computations.
11171 So must defer the generation of the DW_AT_const_value to
11172 after cgraph is ready. */
11173 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11176 return tmpl_die;
11179 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11180 PARM_PACK must be a template parameter pack. The returned DIE
11181 will be child DIE of PARENT_DIE. */
11183 static dw_die_ref
11184 template_parameter_pack_die (tree parm_pack,
11185 tree parm_pack_args,
11186 dw_die_ref parent_die)
11188 dw_die_ref die;
11189 int j;
11191 gcc_assert (parent_die && parm_pack);
11193 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11194 add_name_and_src_coords_attributes (die, parm_pack);
11195 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11196 generic_parameter_die (parm_pack,
11197 TREE_VEC_ELT (parm_pack_args, j),
11198 false /* Don't emit DW_AT_name */,
11199 die);
11200 return die;
11203 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11204 an enumerated type. */
11206 static inline int
11207 type_is_enum (const_tree type)
11209 return TREE_CODE (type) == ENUMERAL_TYPE;
11212 /* Return the DBX register number described by a given RTL node. */
11214 static unsigned int
11215 dbx_reg_number (const_rtx rtl)
11217 unsigned regno = REGNO (rtl);
11219 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11221 #ifdef LEAF_REG_REMAP
11222 if (crtl->uses_only_leaf_regs)
11224 int leaf_reg = LEAF_REG_REMAP (regno);
11225 if (leaf_reg != -1)
11226 regno = (unsigned) leaf_reg;
11228 #endif
11230 regno = DBX_REGISTER_NUMBER (regno);
11231 gcc_assert (regno != INVALID_REGNUM);
11232 return regno;
11235 /* Optionally add a DW_OP_piece term to a location description expression.
11236 DW_OP_piece is only added if the location description expression already
11237 doesn't end with DW_OP_piece. */
11239 static void
11240 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11242 dw_loc_descr_ref loc;
11244 if (*list_head != NULL)
11246 /* Find the end of the chain. */
11247 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11250 if (loc->dw_loc_opc != DW_OP_piece)
11251 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11255 /* Return a location descriptor that designates a machine register or
11256 zero if there is none. */
11258 static dw_loc_descr_ref
11259 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11261 rtx regs;
11263 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11264 return 0;
11266 /* We only use "frame base" when we're sure we're talking about the
11267 post-prologue local stack frame. We do this by *not* running
11268 register elimination until this point, and recognizing the special
11269 argument pointer and soft frame pointer rtx's.
11270 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11271 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11272 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11274 dw_loc_descr_ref result = NULL;
11276 if (dwarf_version >= 4 || !dwarf_strict)
11278 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11279 initialized);
11280 if (result)
11281 add_loc_descr (&result,
11282 new_loc_descr (DW_OP_stack_value, 0, 0));
11284 return result;
11287 regs = targetm.dwarf_register_span (rtl);
11289 if (REG_NREGS (rtl) > 1 || regs)
11290 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11291 else
11293 unsigned int dbx_regnum = dbx_reg_number (rtl);
11294 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11295 return 0;
11296 return one_reg_loc_descriptor (dbx_regnum, initialized);
11300 /* Return a location descriptor that designates a machine register for
11301 a given hard register number. */
11303 static dw_loc_descr_ref
11304 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11306 dw_loc_descr_ref reg_loc_descr;
11308 if (regno <= 31)
11309 reg_loc_descr
11310 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11311 else
11312 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11314 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11315 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11317 return reg_loc_descr;
11320 /* Given an RTL of a register, return a location descriptor that
11321 designates a value that spans more than one register. */
11323 static dw_loc_descr_ref
11324 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11325 enum var_init_status initialized)
11327 int size, i;
11328 dw_loc_descr_ref loc_result = NULL;
11330 /* Simple, contiguous registers. */
11331 if (regs == NULL_RTX)
11333 unsigned reg = REGNO (rtl);
11334 int nregs;
11336 #ifdef LEAF_REG_REMAP
11337 if (crtl->uses_only_leaf_regs)
11339 int leaf_reg = LEAF_REG_REMAP (reg);
11340 if (leaf_reg != -1)
11341 reg = (unsigned) leaf_reg;
11343 #endif
11345 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11346 nregs = REG_NREGS (rtl);
11348 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11350 loc_result = NULL;
11351 while (nregs--)
11353 dw_loc_descr_ref t;
11355 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11356 VAR_INIT_STATUS_INITIALIZED);
11357 add_loc_descr (&loc_result, t);
11358 add_loc_descr_op_piece (&loc_result, size);
11359 ++reg;
11361 return loc_result;
11364 /* Now onto stupid register sets in non contiguous locations. */
11366 gcc_assert (GET_CODE (regs) == PARALLEL);
11368 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11369 loc_result = NULL;
11371 for (i = 0; i < XVECLEN (regs, 0); ++i)
11373 dw_loc_descr_ref t;
11375 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11376 VAR_INIT_STATUS_INITIALIZED);
11377 add_loc_descr (&loc_result, t);
11378 add_loc_descr_op_piece (&loc_result, size);
11381 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11382 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11383 return loc_result;
11386 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11388 /* Return a location descriptor that designates a constant i,
11389 as a compound operation from constant (i >> shift), constant shift
11390 and DW_OP_shl. */
11392 static dw_loc_descr_ref
11393 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11395 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11396 add_loc_descr (&ret, int_loc_descriptor (shift));
11397 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11398 return ret;
11401 /* Return a location descriptor that designates a constant. */
11403 static dw_loc_descr_ref
11404 int_loc_descriptor (HOST_WIDE_INT i)
11406 enum dwarf_location_atom op;
11408 /* Pick the smallest representation of a constant, rather than just
11409 defaulting to the LEB encoding. */
11410 if (i >= 0)
11412 int clz = clz_hwi (i);
11413 int ctz = ctz_hwi (i);
11414 if (i <= 31)
11415 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11416 else if (i <= 0xff)
11417 op = DW_OP_const1u;
11418 else if (i <= 0xffff)
11419 op = DW_OP_const2u;
11420 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11421 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11422 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11423 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11424 while DW_OP_const4u is 5 bytes. */
11425 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11426 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11427 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11428 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11429 while DW_OP_const4u is 5 bytes. */
11430 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11431 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11432 op = DW_OP_const4u;
11433 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11434 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11435 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11436 while DW_OP_constu of constant >= 0x100000000 takes at least
11437 6 bytes. */
11438 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11439 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11440 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11441 >= HOST_BITS_PER_WIDE_INT)
11442 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11443 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11444 while DW_OP_constu takes in this case at least 6 bytes. */
11445 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11446 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11447 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11448 && size_of_uleb128 (i) > 6)
11449 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11450 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11451 else
11452 op = DW_OP_constu;
11454 else
11456 if (i >= -0x80)
11457 op = DW_OP_const1s;
11458 else if (i >= -0x8000)
11459 op = DW_OP_const2s;
11460 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11462 if (size_of_int_loc_descriptor (i) < 5)
11464 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11465 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11466 return ret;
11468 op = DW_OP_const4s;
11470 else
11472 if (size_of_int_loc_descriptor (i)
11473 < (unsigned long) 1 + size_of_sleb128 (i))
11475 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11476 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11477 return ret;
11479 op = DW_OP_consts;
11483 return new_loc_descr (op, i, 0);
11486 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11487 without actually allocating it. */
11489 static unsigned long
11490 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11492 return size_of_int_loc_descriptor (i >> shift)
11493 + size_of_int_loc_descriptor (shift)
11494 + 1;
11497 /* Return size_of_locs (int_loc_descriptor (i)) without
11498 actually allocating it. */
11500 static unsigned long
11501 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11503 unsigned long s;
11505 if (i >= 0)
11507 int clz, ctz;
11508 if (i <= 31)
11509 return 1;
11510 else if (i <= 0xff)
11511 return 2;
11512 else if (i <= 0xffff)
11513 return 3;
11514 clz = clz_hwi (i);
11515 ctz = ctz_hwi (i);
11516 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11517 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11518 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11519 - clz - 5);
11520 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11521 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11522 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11523 - clz - 8);
11524 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11525 return 5;
11526 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11527 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11528 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11529 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11530 - clz - 8);
11531 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11532 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11533 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11534 - clz - 16);
11535 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11536 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11537 && s > 6)
11538 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11539 - clz - 32);
11540 else
11541 return 1 + s;
11543 else
11545 if (i >= -0x80)
11546 return 2;
11547 else if (i >= -0x8000)
11548 return 3;
11549 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11551 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11553 s = size_of_int_loc_descriptor (-i) + 1;
11554 if (s < 5)
11555 return s;
11557 return 5;
11559 else
11561 unsigned long r = 1 + size_of_sleb128 (i);
11562 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11564 s = size_of_int_loc_descriptor (-i) + 1;
11565 if (s < r)
11566 return s;
11568 return r;
11573 /* Return loc description representing "address" of integer value.
11574 This can appear only as toplevel expression. */
11576 static dw_loc_descr_ref
11577 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11579 int litsize;
11580 dw_loc_descr_ref loc_result = NULL;
11582 if (!(dwarf_version >= 4 || !dwarf_strict))
11583 return NULL;
11585 litsize = size_of_int_loc_descriptor (i);
11586 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11587 is more compact. For DW_OP_stack_value we need:
11588 litsize + 1 (DW_OP_stack_value)
11589 and for DW_OP_implicit_value:
11590 1 (DW_OP_implicit_value) + 1 (length) + size. */
11591 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11593 loc_result = int_loc_descriptor (i);
11594 add_loc_descr (&loc_result,
11595 new_loc_descr (DW_OP_stack_value, 0, 0));
11596 return loc_result;
11599 loc_result = new_loc_descr (DW_OP_implicit_value,
11600 size, 0);
11601 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11602 loc_result->dw_loc_oprnd2.v.val_int = i;
11603 return loc_result;
11606 /* Return a location descriptor that designates a base+offset location. */
11608 static dw_loc_descr_ref
11609 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11610 enum var_init_status initialized)
11612 unsigned int regno;
11613 dw_loc_descr_ref result;
11614 dw_fde_ref fde = cfun->fde;
11616 /* We only use "frame base" when we're sure we're talking about the
11617 post-prologue local stack frame. We do this by *not* running
11618 register elimination until this point, and recognizing the special
11619 argument pointer and soft frame pointer rtx's. */
11620 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11622 rtx elim = (ira_use_lra_p
11623 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11624 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11626 if (elim != reg)
11628 if (GET_CODE (elim) == PLUS)
11630 offset += INTVAL (XEXP (elim, 1));
11631 elim = XEXP (elim, 0);
11633 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11634 && (elim == hard_frame_pointer_rtx
11635 || elim == stack_pointer_rtx))
11636 || elim == (frame_pointer_needed
11637 ? hard_frame_pointer_rtx
11638 : stack_pointer_rtx));
11640 /* If drap register is used to align stack, use frame
11641 pointer + offset to access stack variables. If stack
11642 is aligned without drap, use stack pointer + offset to
11643 access stack variables. */
11644 if (crtl->stack_realign_tried
11645 && reg == frame_pointer_rtx)
11647 int base_reg
11648 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11649 ? HARD_FRAME_POINTER_REGNUM
11650 : REGNO (elim));
11651 return new_reg_loc_descr (base_reg, offset);
11654 gcc_assert (frame_pointer_fb_offset_valid);
11655 offset += frame_pointer_fb_offset;
11656 return new_loc_descr (DW_OP_fbreg, offset, 0);
11660 regno = REGNO (reg);
11661 #ifdef LEAF_REG_REMAP
11662 if (crtl->uses_only_leaf_regs)
11664 int leaf_reg = LEAF_REG_REMAP (regno);
11665 if (leaf_reg != -1)
11666 regno = (unsigned) leaf_reg;
11668 #endif
11669 regno = DWARF_FRAME_REGNUM (regno);
11671 if (!optimize && fde
11672 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11674 /* Use cfa+offset to represent the location of arguments passed
11675 on the stack when drap is used to align stack.
11676 Only do this when not optimizing, for optimized code var-tracking
11677 is supposed to track where the arguments live and the register
11678 used as vdrap or drap in some spot might be used for something
11679 else in other part of the routine. */
11680 return new_loc_descr (DW_OP_fbreg, offset, 0);
11683 if (regno <= 31)
11684 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11685 offset, 0);
11686 else
11687 result = new_loc_descr (DW_OP_bregx, regno, offset);
11689 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11690 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11692 return result;
11695 /* Return true if this RTL expression describes a base+offset calculation. */
11697 static inline int
11698 is_based_loc (const_rtx rtl)
11700 return (GET_CODE (rtl) == PLUS
11701 && ((REG_P (XEXP (rtl, 0))
11702 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11703 && CONST_INT_P (XEXP (rtl, 1)))));
11706 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11707 failed. */
11709 static dw_loc_descr_ref
11710 tls_mem_loc_descriptor (rtx mem)
11712 tree base;
11713 dw_loc_descr_ref loc_result;
11715 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11716 return NULL;
11718 base = get_base_address (MEM_EXPR (mem));
11719 if (base == NULL
11720 || TREE_CODE (base) != VAR_DECL
11721 || !DECL_THREAD_LOCAL_P (base))
11722 return NULL;
11724 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11725 if (loc_result == NULL)
11726 return NULL;
11728 if (MEM_OFFSET (mem))
11729 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11731 return loc_result;
11734 /* Output debug info about reason why we failed to expand expression as dwarf
11735 expression. */
11737 static void
11738 expansion_failed (tree expr, rtx rtl, char const *reason)
11740 if (dump_file && (dump_flags & TDF_DETAILS))
11742 fprintf (dump_file, "Failed to expand as dwarf: ");
11743 if (expr)
11744 print_generic_expr (dump_file, expr, dump_flags);
11745 if (rtl)
11747 fprintf (dump_file, "\n");
11748 print_rtl (dump_file, rtl);
11750 fprintf (dump_file, "\nReason: %s\n", reason);
11754 /* Helper function for const_ok_for_output. */
11756 static bool
11757 const_ok_for_output_1 (rtx rtl)
11759 if (GET_CODE (rtl) == UNSPEC)
11761 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11762 we can't express it in the debug info. */
11763 /* Don't complain about TLS UNSPECs, those are just too hard to
11764 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11765 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11766 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11767 if (flag_checking
11768 && (XVECLEN (rtl, 0) == 0
11769 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11770 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
11771 inform (current_function_decl
11772 ? DECL_SOURCE_LOCATION (current_function_decl)
11773 : UNKNOWN_LOCATION,
11774 #if NUM_UNSPEC_VALUES > 0
11775 "non-delegitimized UNSPEC %s (%d) found in variable location",
11776 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11777 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11778 XINT (rtl, 1));
11779 #else
11780 "non-delegitimized UNSPEC %d found in variable location",
11781 XINT (rtl, 1));
11782 #endif
11783 expansion_failed (NULL_TREE, rtl,
11784 "UNSPEC hasn't been delegitimized.\n");
11785 return false;
11788 if (targetm.const_not_ok_for_debug_p (rtl))
11790 expansion_failed (NULL_TREE, rtl,
11791 "Expression rejected for debug by the backend.\n");
11792 return false;
11795 /* FIXME: Refer to PR60655. It is possible for simplification
11796 of rtl expressions in var tracking to produce such expressions.
11797 We should really identify / validate expressions
11798 enclosed in CONST that can be handled by assemblers on various
11799 targets and only handle legitimate cases here. */
11800 if (GET_CODE (rtl) != SYMBOL_REF)
11802 if (GET_CODE (rtl) == NOT)
11803 return false;
11804 return true;
11807 if (CONSTANT_POOL_ADDRESS_P (rtl))
11809 bool marked;
11810 get_pool_constant_mark (rtl, &marked);
11811 /* If all references to this pool constant were optimized away,
11812 it was not output and thus we can't represent it. */
11813 if (!marked)
11815 expansion_failed (NULL_TREE, rtl,
11816 "Constant was removed from constant pool.\n");
11817 return false;
11821 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11822 return false;
11824 /* Avoid references to external symbols in debug info, on several targets
11825 the linker might even refuse to link when linking a shared library,
11826 and in many other cases the relocations for .debug_info/.debug_loc are
11827 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11828 to be defined within the same shared library or executable are fine. */
11829 if (SYMBOL_REF_EXTERNAL_P (rtl))
11831 tree decl = SYMBOL_REF_DECL (rtl);
11833 if (decl == NULL || !targetm.binds_local_p (decl))
11835 expansion_failed (NULL_TREE, rtl,
11836 "Symbol not defined in current TU.\n");
11837 return false;
11841 return true;
11844 /* Return true if constant RTL can be emitted in DW_OP_addr or
11845 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11846 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11848 static bool
11849 const_ok_for_output (rtx rtl)
11851 if (GET_CODE (rtl) == SYMBOL_REF)
11852 return const_ok_for_output_1 (rtl);
11854 if (GET_CODE (rtl) == CONST)
11856 subrtx_var_iterator::array_type array;
11857 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11858 if (!const_ok_for_output_1 (*iter))
11859 return false;
11860 return true;
11863 return true;
11866 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11867 if possible, NULL otherwise. */
11869 static dw_die_ref
11870 base_type_for_mode (machine_mode mode, bool unsignedp)
11872 dw_die_ref type_die;
11873 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11875 if (type == NULL)
11876 return NULL;
11877 switch (TREE_CODE (type))
11879 case INTEGER_TYPE:
11880 case REAL_TYPE:
11881 break;
11882 default:
11883 return NULL;
11885 type_die = lookup_type_die (type);
11886 if (!type_die)
11887 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11888 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11889 return NULL;
11890 return type_die;
11893 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11894 type matching MODE, or, if MODE is narrower than or as wide as
11895 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11896 possible. */
11898 static dw_loc_descr_ref
11899 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11901 machine_mode outer_mode = mode;
11902 dw_die_ref type_die;
11903 dw_loc_descr_ref cvt;
11905 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11907 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11908 return op;
11910 type_die = base_type_for_mode (outer_mode, 1);
11911 if (type_die == NULL)
11912 return NULL;
11913 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11914 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11915 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11916 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11917 add_loc_descr (&op, cvt);
11918 return op;
11921 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11923 static dw_loc_descr_ref
11924 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11925 dw_loc_descr_ref op1)
11927 dw_loc_descr_ref ret = op0;
11928 add_loc_descr (&ret, op1);
11929 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11930 if (STORE_FLAG_VALUE != 1)
11932 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11933 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11935 return ret;
11938 /* Return location descriptor for signed comparison OP RTL. */
11940 static dw_loc_descr_ref
11941 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11942 machine_mode mem_mode)
11944 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11945 dw_loc_descr_ref op0, op1;
11946 int shift;
11948 if (op_mode == VOIDmode)
11949 op_mode = GET_MODE (XEXP (rtl, 1));
11950 if (op_mode == VOIDmode)
11951 return NULL;
11953 if (dwarf_strict
11954 && (GET_MODE_CLASS (op_mode) != MODE_INT
11955 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11956 return NULL;
11958 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11959 VAR_INIT_STATUS_INITIALIZED);
11960 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11961 VAR_INIT_STATUS_INITIALIZED);
11963 if (op0 == NULL || op1 == NULL)
11964 return NULL;
11966 if (GET_MODE_CLASS (op_mode) != MODE_INT
11967 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11968 return compare_loc_descriptor (op, op0, op1);
11970 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11972 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11973 dw_loc_descr_ref cvt;
11975 if (type_die == NULL)
11976 return NULL;
11977 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11978 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11979 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11980 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11981 add_loc_descr (&op0, cvt);
11982 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11983 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11984 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11985 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11986 add_loc_descr (&op1, cvt);
11987 return compare_loc_descriptor (op, op0, op1);
11990 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11991 /* For eq/ne, if the operands are known to be zero-extended,
11992 there is no need to do the fancy shifting up. */
11993 if (op == DW_OP_eq || op == DW_OP_ne)
11995 dw_loc_descr_ref last0, last1;
11996 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11998 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12000 /* deref_size zero extends, and for constants we can check
12001 whether they are zero extended or not. */
12002 if (((last0->dw_loc_opc == DW_OP_deref_size
12003 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12004 || (CONST_INT_P (XEXP (rtl, 0))
12005 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12006 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12007 && ((last1->dw_loc_opc == DW_OP_deref_size
12008 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12009 || (CONST_INT_P (XEXP (rtl, 1))
12010 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12011 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12012 return compare_loc_descriptor (op, op0, op1);
12014 /* EQ/NE comparison against constant in narrower type than
12015 DWARF2_ADDR_SIZE can be performed either as
12016 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12017 DW_OP_{eq,ne}
12019 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12020 DW_OP_{eq,ne}. Pick whatever is shorter. */
12021 if (CONST_INT_P (XEXP (rtl, 1))
12022 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12023 && (size_of_int_loc_descriptor (shift) + 1
12024 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12025 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12026 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12027 & GET_MODE_MASK (op_mode))))
12029 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12030 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12031 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12032 & GET_MODE_MASK (op_mode));
12033 return compare_loc_descriptor (op, op0, op1);
12036 add_loc_descr (&op0, int_loc_descriptor (shift));
12037 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12038 if (CONST_INT_P (XEXP (rtl, 1)))
12039 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12040 else
12042 add_loc_descr (&op1, int_loc_descriptor (shift));
12043 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12045 return compare_loc_descriptor (op, op0, op1);
12048 /* Return location descriptor for unsigned comparison OP RTL. */
12050 static dw_loc_descr_ref
12051 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12052 machine_mode mem_mode)
12054 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12055 dw_loc_descr_ref op0, op1;
12057 if (op_mode == VOIDmode)
12058 op_mode = GET_MODE (XEXP (rtl, 1));
12059 if (op_mode == VOIDmode)
12060 return NULL;
12061 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12062 return NULL;
12064 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12065 return NULL;
12067 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12068 VAR_INIT_STATUS_INITIALIZED);
12069 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12070 VAR_INIT_STATUS_INITIALIZED);
12072 if (op0 == NULL || op1 == NULL)
12073 return NULL;
12075 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12077 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12078 dw_loc_descr_ref last0, last1;
12079 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12081 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12083 if (CONST_INT_P (XEXP (rtl, 0)))
12084 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12085 /* deref_size zero extends, so no need to mask it again. */
12086 else if (last0->dw_loc_opc != DW_OP_deref_size
12087 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12089 add_loc_descr (&op0, int_loc_descriptor (mask));
12090 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12092 if (CONST_INT_P (XEXP (rtl, 1)))
12093 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12094 /* deref_size zero extends, so no need to mask it again. */
12095 else if (last1->dw_loc_opc != DW_OP_deref_size
12096 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12098 add_loc_descr (&op1, int_loc_descriptor (mask));
12099 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12102 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12104 HOST_WIDE_INT bias = 1;
12105 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12106 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12107 if (CONST_INT_P (XEXP (rtl, 1)))
12108 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12109 + INTVAL (XEXP (rtl, 1)));
12110 else
12111 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12112 bias, 0));
12114 return compare_loc_descriptor (op, op0, op1);
12117 /* Return location descriptor for {U,S}{MIN,MAX}. */
12119 static dw_loc_descr_ref
12120 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12121 machine_mode mem_mode)
12123 enum dwarf_location_atom op;
12124 dw_loc_descr_ref op0, op1, ret;
12125 dw_loc_descr_ref bra_node, drop_node;
12127 if (dwarf_strict
12128 && (GET_MODE_CLASS (mode) != MODE_INT
12129 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12130 return NULL;
12132 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12133 VAR_INIT_STATUS_INITIALIZED);
12134 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12135 VAR_INIT_STATUS_INITIALIZED);
12137 if (op0 == NULL || op1 == NULL)
12138 return NULL;
12140 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12141 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12142 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12143 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12145 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12147 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12148 add_loc_descr (&op0, int_loc_descriptor (mask));
12149 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12150 add_loc_descr (&op1, int_loc_descriptor (mask));
12151 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12153 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12155 HOST_WIDE_INT bias = 1;
12156 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12157 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12158 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12161 else if (GET_MODE_CLASS (mode) == MODE_INT
12162 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12164 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12165 add_loc_descr (&op0, int_loc_descriptor (shift));
12166 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12167 add_loc_descr (&op1, int_loc_descriptor (shift));
12168 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12170 else if (GET_MODE_CLASS (mode) == MODE_INT
12171 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12173 dw_die_ref type_die = base_type_for_mode (mode, 0);
12174 dw_loc_descr_ref cvt;
12175 if (type_die == NULL)
12176 return NULL;
12177 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12178 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12179 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12180 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12181 add_loc_descr (&op0, cvt);
12182 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12183 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12184 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12185 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12186 add_loc_descr (&op1, cvt);
12189 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12190 op = DW_OP_lt;
12191 else
12192 op = DW_OP_gt;
12193 ret = op0;
12194 add_loc_descr (&ret, op1);
12195 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12196 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12197 add_loc_descr (&ret, bra_node);
12198 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12199 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12200 add_loc_descr (&ret, drop_node);
12201 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12202 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12203 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12204 && GET_MODE_CLASS (mode) == MODE_INT
12205 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12206 ret = convert_descriptor_to_mode (mode, ret);
12207 return ret;
12210 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12211 but after converting arguments to type_die, afterwards
12212 convert back to unsigned. */
12214 static dw_loc_descr_ref
12215 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12216 machine_mode mode, machine_mode mem_mode)
12218 dw_loc_descr_ref cvt, op0, op1;
12220 if (type_die == NULL)
12221 return NULL;
12222 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12223 VAR_INIT_STATUS_INITIALIZED);
12224 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12225 VAR_INIT_STATUS_INITIALIZED);
12226 if (op0 == NULL || op1 == NULL)
12227 return NULL;
12228 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12229 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12230 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12231 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12232 add_loc_descr (&op0, cvt);
12233 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12234 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12235 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12236 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12237 add_loc_descr (&op1, cvt);
12238 add_loc_descr (&op0, op1);
12239 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12240 return convert_descriptor_to_mode (mode, op0);
12243 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12244 const0 is DW_OP_lit0 or corresponding typed constant,
12245 const1 is DW_OP_lit1 or corresponding typed constant
12246 and constMSB is constant with just the MSB bit set
12247 for the mode):
12248 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12249 L1: const0 DW_OP_swap
12250 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12251 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12252 L3: DW_OP_drop
12253 L4: DW_OP_nop
12255 CTZ is similar:
12256 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12257 L1: const0 DW_OP_swap
12258 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12259 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12260 L3: DW_OP_drop
12261 L4: DW_OP_nop
12263 FFS is similar:
12264 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12265 L1: const1 DW_OP_swap
12266 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12267 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12268 L3: DW_OP_drop
12269 L4: DW_OP_nop */
12271 static dw_loc_descr_ref
12272 clz_loc_descriptor (rtx rtl, machine_mode mode,
12273 machine_mode mem_mode)
12275 dw_loc_descr_ref op0, ret, tmp;
12276 HOST_WIDE_INT valv;
12277 dw_loc_descr_ref l1jump, l1label;
12278 dw_loc_descr_ref l2jump, l2label;
12279 dw_loc_descr_ref l3jump, l3label;
12280 dw_loc_descr_ref l4jump, l4label;
12281 rtx msb;
12283 if (GET_MODE_CLASS (mode) != MODE_INT
12284 || GET_MODE (XEXP (rtl, 0)) != mode)
12285 return NULL;
12287 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12288 VAR_INIT_STATUS_INITIALIZED);
12289 if (op0 == NULL)
12290 return NULL;
12291 ret = op0;
12292 if (GET_CODE (rtl) == CLZ)
12294 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12295 valv = GET_MODE_BITSIZE (mode);
12297 else if (GET_CODE (rtl) == FFS)
12298 valv = 0;
12299 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12300 valv = GET_MODE_BITSIZE (mode);
12301 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12302 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12303 add_loc_descr (&ret, l1jump);
12304 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12305 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12306 VAR_INIT_STATUS_INITIALIZED);
12307 if (tmp == NULL)
12308 return NULL;
12309 add_loc_descr (&ret, tmp);
12310 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12311 add_loc_descr (&ret, l4jump);
12312 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12313 ? const1_rtx : const0_rtx,
12314 mode, mem_mode,
12315 VAR_INIT_STATUS_INITIALIZED);
12316 if (l1label == NULL)
12317 return NULL;
12318 add_loc_descr (&ret, l1label);
12319 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12320 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12321 add_loc_descr (&ret, l2label);
12322 if (GET_CODE (rtl) != CLZ)
12323 msb = const1_rtx;
12324 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12325 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12326 << (GET_MODE_BITSIZE (mode) - 1));
12327 else
12328 msb = immed_wide_int_const
12329 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12330 GET_MODE_PRECISION (mode)), mode);
12331 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12332 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12333 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12334 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12335 else
12336 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12337 VAR_INIT_STATUS_INITIALIZED);
12338 if (tmp == NULL)
12339 return NULL;
12340 add_loc_descr (&ret, tmp);
12341 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12342 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12343 add_loc_descr (&ret, l3jump);
12344 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12345 VAR_INIT_STATUS_INITIALIZED);
12346 if (tmp == NULL)
12347 return NULL;
12348 add_loc_descr (&ret, tmp);
12349 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12350 ? DW_OP_shl : DW_OP_shr, 0, 0));
12351 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12352 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12353 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12354 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12355 add_loc_descr (&ret, l2jump);
12356 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12357 add_loc_descr (&ret, l3label);
12358 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12359 add_loc_descr (&ret, l4label);
12360 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12361 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12362 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12363 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12364 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12365 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12366 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12367 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12368 return ret;
12371 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12372 const1 is DW_OP_lit1 or corresponding typed constant):
12373 const0 DW_OP_swap
12374 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12375 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12376 L2: DW_OP_drop
12378 PARITY is similar:
12379 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12380 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12381 L2: DW_OP_drop */
12383 static dw_loc_descr_ref
12384 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12385 machine_mode mem_mode)
12387 dw_loc_descr_ref op0, ret, tmp;
12388 dw_loc_descr_ref l1jump, l1label;
12389 dw_loc_descr_ref l2jump, l2label;
12391 if (GET_MODE_CLASS (mode) != MODE_INT
12392 || GET_MODE (XEXP (rtl, 0)) != mode)
12393 return NULL;
12395 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12396 VAR_INIT_STATUS_INITIALIZED);
12397 if (op0 == NULL)
12398 return NULL;
12399 ret = op0;
12400 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12401 VAR_INIT_STATUS_INITIALIZED);
12402 if (tmp == NULL)
12403 return NULL;
12404 add_loc_descr (&ret, tmp);
12405 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12406 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12407 add_loc_descr (&ret, l1label);
12408 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12409 add_loc_descr (&ret, l2jump);
12410 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12411 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12412 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12413 VAR_INIT_STATUS_INITIALIZED);
12414 if (tmp == NULL)
12415 return NULL;
12416 add_loc_descr (&ret, tmp);
12417 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12418 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12419 ? DW_OP_plus : DW_OP_xor, 0, 0));
12420 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12421 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12422 VAR_INIT_STATUS_INITIALIZED);
12423 add_loc_descr (&ret, tmp);
12424 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12425 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12426 add_loc_descr (&ret, l1jump);
12427 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12428 add_loc_descr (&ret, l2label);
12429 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12430 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12431 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12432 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12433 return ret;
12436 /* BSWAP (constS is initial shift count, either 56 or 24):
12437 constS const0
12438 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12439 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12440 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12441 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12442 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12444 static dw_loc_descr_ref
12445 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12446 machine_mode mem_mode)
12448 dw_loc_descr_ref op0, ret, tmp;
12449 dw_loc_descr_ref l1jump, l1label;
12450 dw_loc_descr_ref l2jump, l2label;
12452 if (GET_MODE_CLASS (mode) != MODE_INT
12453 || BITS_PER_UNIT != 8
12454 || (GET_MODE_BITSIZE (mode) != 32
12455 && GET_MODE_BITSIZE (mode) != 64))
12456 return NULL;
12458 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12459 VAR_INIT_STATUS_INITIALIZED);
12460 if (op0 == NULL)
12461 return NULL;
12463 ret = op0;
12464 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12465 mode, mem_mode,
12466 VAR_INIT_STATUS_INITIALIZED);
12467 if (tmp == NULL)
12468 return NULL;
12469 add_loc_descr (&ret, tmp);
12470 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12471 VAR_INIT_STATUS_INITIALIZED);
12472 if (tmp == NULL)
12473 return NULL;
12474 add_loc_descr (&ret, tmp);
12475 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12476 add_loc_descr (&ret, l1label);
12477 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12478 mode, mem_mode,
12479 VAR_INIT_STATUS_INITIALIZED);
12480 add_loc_descr (&ret, tmp);
12481 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12482 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12483 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12484 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12485 VAR_INIT_STATUS_INITIALIZED);
12486 if (tmp == NULL)
12487 return NULL;
12488 add_loc_descr (&ret, tmp);
12489 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12490 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12491 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12492 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12493 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12494 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12495 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12496 VAR_INIT_STATUS_INITIALIZED);
12497 add_loc_descr (&ret, tmp);
12498 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12499 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12500 add_loc_descr (&ret, l2jump);
12501 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12502 VAR_INIT_STATUS_INITIALIZED);
12503 add_loc_descr (&ret, tmp);
12504 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12505 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12506 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12507 add_loc_descr (&ret, l1jump);
12508 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12509 add_loc_descr (&ret, l2label);
12510 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12511 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12512 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12513 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12514 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12515 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12516 return ret;
12519 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12520 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12521 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12522 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12524 ROTATERT is similar:
12525 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12526 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12527 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12529 static dw_loc_descr_ref
12530 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12531 machine_mode mem_mode)
12533 rtx rtlop1 = XEXP (rtl, 1);
12534 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12535 int i;
12537 if (GET_MODE_CLASS (mode) != MODE_INT)
12538 return NULL;
12540 if (GET_MODE (rtlop1) != VOIDmode
12541 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12542 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12543 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12544 VAR_INIT_STATUS_INITIALIZED);
12545 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12546 VAR_INIT_STATUS_INITIALIZED);
12547 if (op0 == NULL || op1 == NULL)
12548 return NULL;
12549 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12550 for (i = 0; i < 2; i++)
12552 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12553 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12554 mode, mem_mode,
12555 VAR_INIT_STATUS_INITIALIZED);
12556 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12557 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12558 ? DW_OP_const4u
12559 : HOST_BITS_PER_WIDE_INT == 64
12560 ? DW_OP_const8u : DW_OP_constu,
12561 GET_MODE_MASK (mode), 0);
12562 else
12563 mask[i] = NULL;
12564 if (mask[i] == NULL)
12565 return NULL;
12566 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12568 ret = op0;
12569 add_loc_descr (&ret, op1);
12570 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12571 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12572 if (GET_CODE (rtl) == ROTATERT)
12574 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12575 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12576 GET_MODE_BITSIZE (mode), 0));
12578 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12579 if (mask[0] != NULL)
12580 add_loc_descr (&ret, mask[0]);
12581 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12582 if (mask[1] != NULL)
12584 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12585 add_loc_descr (&ret, mask[1]);
12586 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12588 if (GET_CODE (rtl) == ROTATE)
12590 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12591 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12592 GET_MODE_BITSIZE (mode), 0));
12594 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12595 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12596 return ret;
12599 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12600 for DEBUG_PARAMETER_REF RTL. */
12602 static dw_loc_descr_ref
12603 parameter_ref_descriptor (rtx rtl)
12605 dw_loc_descr_ref ret;
12606 dw_die_ref ref;
12608 if (dwarf_strict)
12609 return NULL;
12610 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12611 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12612 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12613 if (ref)
12615 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12616 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12617 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12619 else
12621 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12622 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12624 return ret;
12627 /* The following routine converts the RTL for a variable or parameter
12628 (resident in memory) into an equivalent Dwarf representation of a
12629 mechanism for getting the address of that same variable onto the top of a
12630 hypothetical "address evaluation" stack.
12632 When creating memory location descriptors, we are effectively transforming
12633 the RTL for a memory-resident object into its Dwarf postfix expression
12634 equivalent. This routine recursively descends an RTL tree, turning
12635 it into Dwarf postfix code as it goes.
12637 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12639 MEM_MODE is the mode of the memory reference, needed to handle some
12640 autoincrement addressing modes.
12642 Return 0 if we can't represent the location. */
12644 dw_loc_descr_ref
12645 mem_loc_descriptor (rtx rtl, machine_mode mode,
12646 machine_mode mem_mode,
12647 enum var_init_status initialized)
12649 dw_loc_descr_ref mem_loc_result = NULL;
12650 enum dwarf_location_atom op;
12651 dw_loc_descr_ref op0, op1;
12652 rtx inner = NULL_RTX;
12654 if (mode == VOIDmode)
12655 mode = GET_MODE (rtl);
12657 /* Note that for a dynamically sized array, the location we will generate a
12658 description of here will be the lowest numbered location which is
12659 actually within the array. That's *not* necessarily the same as the
12660 zeroth element of the array. */
12662 rtl = targetm.delegitimize_address (rtl);
12664 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12665 return NULL;
12667 switch (GET_CODE (rtl))
12669 case POST_INC:
12670 case POST_DEC:
12671 case POST_MODIFY:
12672 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12674 case SUBREG:
12675 /* The case of a subreg may arise when we have a local (register)
12676 variable or a formal (register) parameter which doesn't quite fill
12677 up an entire register. For now, just assume that it is
12678 legitimate to make the Dwarf info refer to the whole register which
12679 contains the given subreg. */
12680 if (!subreg_lowpart_p (rtl))
12681 break;
12682 inner = SUBREG_REG (rtl);
12683 case TRUNCATE:
12684 if (inner == NULL_RTX)
12685 inner = XEXP (rtl, 0);
12686 if (GET_MODE_CLASS (mode) == MODE_INT
12687 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12688 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12689 #ifdef POINTERS_EXTEND_UNSIGNED
12690 || (mode == Pmode && mem_mode != VOIDmode)
12691 #endif
12693 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12695 mem_loc_result = mem_loc_descriptor (inner,
12696 GET_MODE (inner),
12697 mem_mode, initialized);
12698 break;
12700 if (dwarf_strict)
12701 break;
12702 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12703 break;
12704 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12705 && (GET_MODE_CLASS (mode) != MODE_INT
12706 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12707 break;
12708 else
12710 dw_die_ref type_die;
12711 dw_loc_descr_ref cvt;
12713 mem_loc_result = mem_loc_descriptor (inner,
12714 GET_MODE (inner),
12715 mem_mode, initialized);
12716 if (mem_loc_result == NULL)
12717 break;
12718 type_die = base_type_for_mode (mode,
12719 GET_MODE_CLASS (mode) == MODE_INT);
12720 if (type_die == NULL)
12722 mem_loc_result = NULL;
12723 break;
12725 if (GET_MODE_SIZE (mode)
12726 != GET_MODE_SIZE (GET_MODE (inner)))
12727 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12728 else
12729 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12730 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12731 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12732 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12733 add_loc_descr (&mem_loc_result, cvt);
12735 break;
12737 case REG:
12738 if (GET_MODE_CLASS (mode) != MODE_INT
12739 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12740 && rtl != arg_pointer_rtx
12741 && rtl != frame_pointer_rtx
12742 #ifdef POINTERS_EXTEND_UNSIGNED
12743 && (mode != Pmode || mem_mode == VOIDmode)
12744 #endif
12747 dw_die_ref type_die;
12748 unsigned int dbx_regnum;
12750 if (dwarf_strict)
12751 break;
12752 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12753 break;
12754 type_die = base_type_for_mode (mode,
12755 GET_MODE_CLASS (mode) == MODE_INT);
12756 if (type_die == NULL)
12757 break;
12759 dbx_regnum = dbx_reg_number (rtl);
12760 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12761 break;
12762 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12763 dbx_regnum, 0);
12764 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12765 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12766 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12767 break;
12769 /* Whenever a register number forms a part of the description of the
12770 method for calculating the (dynamic) address of a memory resident
12771 object, DWARF rules require the register number be referred to as
12772 a "base register". This distinction is not based in any way upon
12773 what category of register the hardware believes the given register
12774 belongs to. This is strictly DWARF terminology we're dealing with
12775 here. Note that in cases where the location of a memory-resident
12776 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12777 OP_CONST (0)) the actual DWARF location descriptor that we generate
12778 may just be OP_BASEREG (basereg). This may look deceptively like
12779 the object in question was allocated to a register (rather than in
12780 memory) so DWARF consumers need to be aware of the subtle
12781 distinction between OP_REG and OP_BASEREG. */
12782 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12783 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12784 else if (stack_realign_drap
12785 && crtl->drap_reg
12786 && crtl->args.internal_arg_pointer == rtl
12787 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12789 /* If RTL is internal_arg_pointer, which has been optimized
12790 out, use DRAP instead. */
12791 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12792 VAR_INIT_STATUS_INITIALIZED);
12794 break;
12796 case SIGN_EXTEND:
12797 case ZERO_EXTEND:
12798 if (GET_MODE_CLASS (mode) != MODE_INT)
12799 break;
12800 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12801 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12802 if (op0 == 0)
12803 break;
12804 else if (GET_CODE (rtl) == ZERO_EXTEND
12805 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12806 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12807 < HOST_BITS_PER_WIDE_INT
12808 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12809 to expand zero extend as two shifts instead of
12810 masking. */
12811 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12813 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12814 mem_loc_result = op0;
12815 add_loc_descr (&mem_loc_result,
12816 int_loc_descriptor (GET_MODE_MASK (imode)));
12817 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12819 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12821 int shift = DWARF2_ADDR_SIZE
12822 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12823 shift *= BITS_PER_UNIT;
12824 if (GET_CODE (rtl) == SIGN_EXTEND)
12825 op = DW_OP_shra;
12826 else
12827 op = DW_OP_shr;
12828 mem_loc_result = op0;
12829 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12830 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12831 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12832 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12834 else if (!dwarf_strict)
12836 dw_die_ref type_die1, type_die2;
12837 dw_loc_descr_ref cvt;
12839 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12840 GET_CODE (rtl) == ZERO_EXTEND);
12841 if (type_die1 == NULL)
12842 break;
12843 type_die2 = base_type_for_mode (mode, 1);
12844 if (type_die2 == NULL)
12845 break;
12846 mem_loc_result = op0;
12847 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12848 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12849 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12850 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12851 add_loc_descr (&mem_loc_result, cvt);
12852 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12853 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12854 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12855 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12856 add_loc_descr (&mem_loc_result, cvt);
12858 break;
12860 case MEM:
12862 rtx new_rtl = avoid_constant_pool_reference (rtl);
12863 if (new_rtl != rtl)
12865 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12866 initialized);
12867 if (mem_loc_result != NULL)
12868 return mem_loc_result;
12871 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12872 get_address_mode (rtl), mode,
12873 VAR_INIT_STATUS_INITIALIZED);
12874 if (mem_loc_result == NULL)
12875 mem_loc_result = tls_mem_loc_descriptor (rtl);
12876 if (mem_loc_result != NULL)
12878 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12879 || GET_MODE_CLASS (mode) != MODE_INT)
12881 dw_die_ref type_die;
12882 dw_loc_descr_ref deref;
12884 if (dwarf_strict)
12885 return NULL;
12886 type_die
12887 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12888 if (type_die == NULL)
12889 return NULL;
12890 deref = new_loc_descr (DW_OP_GNU_deref_type,
12891 GET_MODE_SIZE (mode), 0);
12892 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12893 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12894 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12895 add_loc_descr (&mem_loc_result, deref);
12897 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12898 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12899 else
12900 add_loc_descr (&mem_loc_result,
12901 new_loc_descr (DW_OP_deref_size,
12902 GET_MODE_SIZE (mode), 0));
12904 break;
12906 case LO_SUM:
12907 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12909 case LABEL_REF:
12910 /* Some ports can transform a symbol ref into a label ref, because
12911 the symbol ref is too far away and has to be dumped into a constant
12912 pool. */
12913 case CONST:
12914 case SYMBOL_REF:
12915 if ((GET_MODE_CLASS (mode) != MODE_INT
12916 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12917 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12918 #ifdef POINTERS_EXTEND_UNSIGNED
12919 && (mode != Pmode || mem_mode == VOIDmode)
12920 #endif
12922 break;
12923 if (GET_CODE (rtl) == SYMBOL_REF
12924 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12926 dw_loc_descr_ref temp;
12928 /* If this is not defined, we have no way to emit the data. */
12929 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12930 break;
12932 temp = new_addr_loc_descr (rtl, dtprel_true);
12934 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12935 add_loc_descr (&mem_loc_result, temp);
12937 break;
12940 if (!const_ok_for_output (rtl))
12942 if (GET_CODE (rtl) == CONST)
12943 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12944 initialized);
12945 break;
12948 symref:
12949 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12950 vec_safe_push (used_rtx_array, rtl);
12951 break;
12953 case CONCAT:
12954 case CONCATN:
12955 case VAR_LOCATION:
12956 case DEBUG_IMPLICIT_PTR:
12957 expansion_failed (NULL_TREE, rtl,
12958 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12959 return 0;
12961 case ENTRY_VALUE:
12962 if (dwarf_strict)
12963 return NULL;
12964 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12966 if (GET_MODE_CLASS (mode) != MODE_INT
12967 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12968 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12969 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12970 else
12972 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12973 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12974 return NULL;
12975 op0 = one_reg_loc_descriptor (dbx_regnum,
12976 VAR_INIT_STATUS_INITIALIZED);
12979 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12980 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12982 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12983 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12984 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12985 return NULL;
12987 else
12988 gcc_unreachable ();
12989 if (op0 == NULL)
12990 return NULL;
12991 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12992 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12993 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12994 break;
12996 case DEBUG_PARAMETER_REF:
12997 mem_loc_result = parameter_ref_descriptor (rtl);
12998 break;
13000 case PRE_MODIFY:
13001 /* Extract the PLUS expression nested inside and fall into
13002 PLUS code below. */
13003 rtl = XEXP (rtl, 1);
13004 goto plus;
13006 case PRE_INC:
13007 case PRE_DEC:
13008 /* Turn these into a PLUS expression and fall into the PLUS code
13009 below. */
13010 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13011 gen_int_mode (GET_CODE (rtl) == PRE_INC
13012 ? GET_MODE_UNIT_SIZE (mem_mode)
13013 : -GET_MODE_UNIT_SIZE (mem_mode),
13014 mode));
13016 /* ... fall through ... */
13018 case PLUS:
13019 plus:
13020 if (is_based_loc (rtl)
13021 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13022 || XEXP (rtl, 0) == arg_pointer_rtx
13023 || XEXP (rtl, 0) == frame_pointer_rtx)
13024 && GET_MODE_CLASS (mode) == MODE_INT)
13025 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13026 INTVAL (XEXP (rtl, 1)),
13027 VAR_INIT_STATUS_INITIALIZED);
13028 else
13030 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13031 VAR_INIT_STATUS_INITIALIZED);
13032 if (mem_loc_result == 0)
13033 break;
13035 if (CONST_INT_P (XEXP (rtl, 1))
13036 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13037 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13038 else
13040 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13041 VAR_INIT_STATUS_INITIALIZED);
13042 if (op1 == 0)
13043 return NULL;
13044 add_loc_descr (&mem_loc_result, op1);
13045 add_loc_descr (&mem_loc_result,
13046 new_loc_descr (DW_OP_plus, 0, 0));
13049 break;
13051 /* If a pseudo-reg is optimized away, it is possible for it to
13052 be replaced with a MEM containing a multiply or shift. */
13053 case MINUS:
13054 op = DW_OP_minus;
13055 goto do_binop;
13057 case MULT:
13058 op = DW_OP_mul;
13059 goto do_binop;
13061 case DIV:
13062 if (!dwarf_strict
13063 && GET_MODE_CLASS (mode) == MODE_INT
13064 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13066 mem_loc_result = typed_binop (DW_OP_div, rtl,
13067 base_type_for_mode (mode, 0),
13068 mode, mem_mode);
13069 break;
13071 op = DW_OP_div;
13072 goto do_binop;
13074 case UMOD:
13075 op = DW_OP_mod;
13076 goto do_binop;
13078 case ASHIFT:
13079 op = DW_OP_shl;
13080 goto do_shift;
13082 case ASHIFTRT:
13083 op = DW_OP_shra;
13084 goto do_shift;
13086 case LSHIFTRT:
13087 op = DW_OP_shr;
13088 goto do_shift;
13090 do_shift:
13091 if (GET_MODE_CLASS (mode) != MODE_INT)
13092 break;
13093 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13094 VAR_INIT_STATUS_INITIALIZED);
13096 rtx rtlop1 = XEXP (rtl, 1);
13097 if (GET_MODE (rtlop1) != VOIDmode
13098 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13099 < GET_MODE_BITSIZE (mode))
13100 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13101 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13102 VAR_INIT_STATUS_INITIALIZED);
13105 if (op0 == 0 || op1 == 0)
13106 break;
13108 mem_loc_result = op0;
13109 add_loc_descr (&mem_loc_result, op1);
13110 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13111 break;
13113 case AND:
13114 op = DW_OP_and;
13115 goto do_binop;
13117 case IOR:
13118 op = DW_OP_or;
13119 goto do_binop;
13121 case XOR:
13122 op = DW_OP_xor;
13123 goto do_binop;
13125 do_binop:
13126 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13127 VAR_INIT_STATUS_INITIALIZED);
13128 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13129 VAR_INIT_STATUS_INITIALIZED);
13131 if (op0 == 0 || op1 == 0)
13132 break;
13134 mem_loc_result = op0;
13135 add_loc_descr (&mem_loc_result, op1);
13136 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13137 break;
13139 case MOD:
13140 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13142 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13143 base_type_for_mode (mode, 0),
13144 mode, mem_mode);
13145 break;
13148 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13149 VAR_INIT_STATUS_INITIALIZED);
13150 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13151 VAR_INIT_STATUS_INITIALIZED);
13153 if (op0 == 0 || op1 == 0)
13154 break;
13156 mem_loc_result = op0;
13157 add_loc_descr (&mem_loc_result, op1);
13158 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13159 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13160 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13161 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13162 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13163 break;
13165 case UDIV:
13166 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13168 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13170 op = DW_OP_div;
13171 goto do_binop;
13173 mem_loc_result = typed_binop (DW_OP_div, rtl,
13174 base_type_for_mode (mode, 1),
13175 mode, mem_mode);
13177 break;
13179 case NOT:
13180 op = DW_OP_not;
13181 goto do_unop;
13183 case ABS:
13184 op = DW_OP_abs;
13185 goto do_unop;
13187 case NEG:
13188 op = DW_OP_neg;
13189 goto do_unop;
13191 do_unop:
13192 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13193 VAR_INIT_STATUS_INITIALIZED);
13195 if (op0 == 0)
13196 break;
13198 mem_loc_result = op0;
13199 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13200 break;
13202 case CONST_INT:
13203 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13204 #ifdef POINTERS_EXTEND_UNSIGNED
13205 || (mode == Pmode
13206 && mem_mode != VOIDmode
13207 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13208 #endif
13211 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13212 break;
13214 if (!dwarf_strict
13215 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13216 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13218 dw_die_ref type_die = base_type_for_mode (mode, 1);
13219 machine_mode amode;
13220 if (type_die == NULL)
13221 return NULL;
13222 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13223 MODE_INT, 0);
13224 if (INTVAL (rtl) >= 0
13225 && amode != BLKmode
13226 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13227 /* const DW_OP_GNU_convert <XXX> vs.
13228 DW_OP_GNU_const_type <XXX, 1, const>. */
13229 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13230 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13232 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13233 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13234 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13235 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13236 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13237 add_loc_descr (&mem_loc_result, op0);
13238 return mem_loc_result;
13240 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13241 INTVAL (rtl));
13242 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13243 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13244 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13245 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13246 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13247 else
13249 mem_loc_result->dw_loc_oprnd2.val_class
13250 = dw_val_class_const_double;
13251 mem_loc_result->dw_loc_oprnd2.v.val_double
13252 = double_int::from_shwi (INTVAL (rtl));
13255 break;
13257 case CONST_DOUBLE:
13258 if (!dwarf_strict)
13260 dw_die_ref type_die;
13262 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13263 CONST_DOUBLE rtx could represent either a large integer
13264 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13265 the value is always a floating point constant.
13267 When it is an integer, a CONST_DOUBLE is used whenever
13268 the constant requires 2 HWIs to be adequately represented.
13269 We output CONST_DOUBLEs as blocks. */
13270 if (mode == VOIDmode
13271 || (GET_MODE (rtl) == VOIDmode
13272 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13273 break;
13274 type_die = base_type_for_mode (mode,
13275 GET_MODE_CLASS (mode) == MODE_INT);
13276 if (type_die == NULL)
13277 return NULL;
13278 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13279 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13280 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13281 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13282 #if TARGET_SUPPORTS_WIDE_INT == 0
13283 if (!SCALAR_FLOAT_MODE_P (mode))
13285 mem_loc_result->dw_loc_oprnd2.val_class
13286 = dw_val_class_const_double;
13287 mem_loc_result->dw_loc_oprnd2.v.val_double
13288 = rtx_to_double_int (rtl);
13290 else
13291 #endif
13293 unsigned int length = GET_MODE_SIZE (mode);
13294 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13296 insert_float (rtl, array);
13297 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13298 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13299 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13300 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13303 break;
13305 case CONST_WIDE_INT:
13306 if (!dwarf_strict)
13308 dw_die_ref type_die;
13310 type_die = base_type_for_mode (mode,
13311 GET_MODE_CLASS (mode) == MODE_INT);
13312 if (type_die == NULL)
13313 return NULL;
13314 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13315 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13316 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13317 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13318 mem_loc_result->dw_loc_oprnd2.val_class
13319 = dw_val_class_wide_int;
13320 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13321 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13323 break;
13325 case EQ:
13326 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13327 break;
13329 case GE:
13330 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13331 break;
13333 case GT:
13334 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13335 break;
13337 case LE:
13338 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13339 break;
13341 case LT:
13342 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13343 break;
13345 case NE:
13346 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13347 break;
13349 case GEU:
13350 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13351 break;
13353 case GTU:
13354 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13355 break;
13357 case LEU:
13358 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13359 break;
13361 case LTU:
13362 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13363 break;
13365 case UMIN:
13366 case UMAX:
13367 if (GET_MODE_CLASS (mode) != MODE_INT)
13368 break;
13369 /* FALLTHRU */
13370 case SMIN:
13371 case SMAX:
13372 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13373 break;
13375 case ZERO_EXTRACT:
13376 case SIGN_EXTRACT:
13377 if (CONST_INT_P (XEXP (rtl, 1))
13378 && CONST_INT_P (XEXP (rtl, 2))
13379 && ((unsigned) INTVAL (XEXP (rtl, 1))
13380 + (unsigned) INTVAL (XEXP (rtl, 2))
13381 <= GET_MODE_BITSIZE (mode))
13382 && GET_MODE_CLASS (mode) == MODE_INT
13383 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13384 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13386 int shift, size;
13387 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13388 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13389 if (op0 == 0)
13390 break;
13391 if (GET_CODE (rtl) == SIGN_EXTRACT)
13392 op = DW_OP_shra;
13393 else
13394 op = DW_OP_shr;
13395 mem_loc_result = op0;
13396 size = INTVAL (XEXP (rtl, 1));
13397 shift = INTVAL (XEXP (rtl, 2));
13398 if (BITS_BIG_ENDIAN)
13399 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13400 - shift - size;
13401 if (shift + size != (int) DWARF2_ADDR_SIZE)
13403 add_loc_descr (&mem_loc_result,
13404 int_loc_descriptor (DWARF2_ADDR_SIZE
13405 - shift - size));
13406 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13408 if (size != (int) DWARF2_ADDR_SIZE)
13410 add_loc_descr (&mem_loc_result,
13411 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13412 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13415 break;
13417 case IF_THEN_ELSE:
13419 dw_loc_descr_ref op2, bra_node, drop_node;
13420 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13421 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13422 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13423 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13424 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13425 VAR_INIT_STATUS_INITIALIZED);
13426 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13427 VAR_INIT_STATUS_INITIALIZED);
13428 if (op0 == NULL || op1 == NULL || op2 == NULL)
13429 break;
13431 mem_loc_result = op1;
13432 add_loc_descr (&mem_loc_result, op2);
13433 add_loc_descr (&mem_loc_result, op0);
13434 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13435 add_loc_descr (&mem_loc_result, bra_node);
13436 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13437 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13438 add_loc_descr (&mem_loc_result, drop_node);
13439 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13440 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13442 break;
13444 case FLOAT_EXTEND:
13445 case FLOAT_TRUNCATE:
13446 case FLOAT:
13447 case UNSIGNED_FLOAT:
13448 case FIX:
13449 case UNSIGNED_FIX:
13450 if (!dwarf_strict)
13452 dw_die_ref type_die;
13453 dw_loc_descr_ref cvt;
13455 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13456 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13457 if (op0 == NULL)
13458 break;
13459 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13460 && (GET_CODE (rtl) == FLOAT
13461 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13462 <= DWARF2_ADDR_SIZE))
13464 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13465 GET_CODE (rtl) == UNSIGNED_FLOAT);
13466 if (type_die == NULL)
13467 break;
13468 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13469 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13470 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13471 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13472 add_loc_descr (&op0, cvt);
13474 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13475 if (type_die == NULL)
13476 break;
13477 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13478 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13479 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13480 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13481 add_loc_descr (&op0, cvt);
13482 if (GET_MODE_CLASS (mode) == MODE_INT
13483 && (GET_CODE (rtl) == FIX
13484 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13486 op0 = convert_descriptor_to_mode (mode, op0);
13487 if (op0 == NULL)
13488 break;
13490 mem_loc_result = op0;
13492 break;
13494 case CLZ:
13495 case CTZ:
13496 case FFS:
13497 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13498 break;
13500 case POPCOUNT:
13501 case PARITY:
13502 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13503 break;
13505 case BSWAP:
13506 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13507 break;
13509 case ROTATE:
13510 case ROTATERT:
13511 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13512 break;
13514 case COMPARE:
13515 /* In theory, we could implement the above. */
13516 /* DWARF cannot represent the unsigned compare operations
13517 natively. */
13518 case SS_MULT:
13519 case US_MULT:
13520 case SS_DIV:
13521 case US_DIV:
13522 case SS_PLUS:
13523 case US_PLUS:
13524 case SS_MINUS:
13525 case US_MINUS:
13526 case SS_NEG:
13527 case US_NEG:
13528 case SS_ABS:
13529 case SS_ASHIFT:
13530 case US_ASHIFT:
13531 case SS_TRUNCATE:
13532 case US_TRUNCATE:
13533 case UNORDERED:
13534 case ORDERED:
13535 case UNEQ:
13536 case UNGE:
13537 case UNGT:
13538 case UNLE:
13539 case UNLT:
13540 case LTGT:
13541 case FRACT_CONVERT:
13542 case UNSIGNED_FRACT_CONVERT:
13543 case SAT_FRACT:
13544 case UNSIGNED_SAT_FRACT:
13545 case SQRT:
13546 case ASM_OPERANDS:
13547 case VEC_MERGE:
13548 case VEC_SELECT:
13549 case VEC_CONCAT:
13550 case VEC_DUPLICATE:
13551 case UNSPEC:
13552 case HIGH:
13553 case FMA:
13554 case STRICT_LOW_PART:
13555 case CONST_VECTOR:
13556 case CONST_FIXED:
13557 case CLRSB:
13558 case CLOBBER:
13559 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13560 can't express it in the debug info. This can happen e.g. with some
13561 TLS UNSPECs. */
13562 break;
13564 case CONST_STRING:
13565 resolve_one_addr (&rtl);
13566 goto symref;
13568 default:
13569 if (flag_checking)
13571 print_rtl (stderr, rtl);
13572 gcc_unreachable ();
13574 break;
13577 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13578 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13580 return mem_loc_result;
13583 /* Return a descriptor that describes the concatenation of two locations.
13584 This is typically a complex variable. */
13586 static dw_loc_descr_ref
13587 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13589 dw_loc_descr_ref cc_loc_result = NULL;
13590 dw_loc_descr_ref x0_ref
13591 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13592 dw_loc_descr_ref x1_ref
13593 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13595 if (x0_ref == 0 || x1_ref == 0)
13596 return 0;
13598 cc_loc_result = x0_ref;
13599 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13601 add_loc_descr (&cc_loc_result, x1_ref);
13602 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13604 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13605 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13607 return cc_loc_result;
13610 /* Return a descriptor that describes the concatenation of N
13611 locations. */
13613 static dw_loc_descr_ref
13614 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13616 unsigned int i;
13617 dw_loc_descr_ref cc_loc_result = NULL;
13618 unsigned int n = XVECLEN (concatn, 0);
13620 for (i = 0; i < n; ++i)
13622 dw_loc_descr_ref ref;
13623 rtx x = XVECEXP (concatn, 0, i);
13625 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13626 if (ref == NULL)
13627 return NULL;
13629 add_loc_descr (&cc_loc_result, ref);
13630 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13633 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13634 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13636 return cc_loc_result;
13639 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13640 for DEBUG_IMPLICIT_PTR RTL. */
13642 static dw_loc_descr_ref
13643 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13645 dw_loc_descr_ref ret;
13646 dw_die_ref ref;
13648 if (dwarf_strict)
13649 return NULL;
13650 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13651 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13652 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13653 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13654 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13655 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13656 if (ref)
13658 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13659 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13660 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13662 else
13664 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13665 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13667 return ret;
13670 /* Output a proper Dwarf location descriptor for a variable or parameter
13671 which is either allocated in a register or in a memory location. For a
13672 register, we just generate an OP_REG and the register number. For a
13673 memory location we provide a Dwarf postfix expression describing how to
13674 generate the (dynamic) address of the object onto the address stack.
13676 MODE is mode of the decl if this loc_descriptor is going to be used in
13677 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13678 allowed, VOIDmode otherwise.
13680 If we don't know how to describe it, return 0. */
13682 static dw_loc_descr_ref
13683 loc_descriptor (rtx rtl, machine_mode mode,
13684 enum var_init_status initialized)
13686 dw_loc_descr_ref loc_result = NULL;
13688 switch (GET_CODE (rtl))
13690 case SUBREG:
13691 /* The case of a subreg may arise when we have a local (register)
13692 variable or a formal (register) parameter which doesn't quite fill
13693 up an entire register. For now, just assume that it is
13694 legitimate to make the Dwarf info refer to the whole register which
13695 contains the given subreg. */
13696 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13697 loc_result = loc_descriptor (SUBREG_REG (rtl),
13698 GET_MODE (SUBREG_REG (rtl)), initialized);
13699 else
13700 goto do_default;
13701 break;
13703 case REG:
13704 loc_result = reg_loc_descriptor (rtl, initialized);
13705 break;
13707 case MEM:
13708 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13709 GET_MODE (rtl), initialized);
13710 if (loc_result == NULL)
13711 loc_result = tls_mem_loc_descriptor (rtl);
13712 if (loc_result == NULL)
13714 rtx new_rtl = avoid_constant_pool_reference (rtl);
13715 if (new_rtl != rtl)
13716 loc_result = loc_descriptor (new_rtl, mode, initialized);
13718 break;
13720 case CONCAT:
13721 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13722 initialized);
13723 break;
13725 case CONCATN:
13726 loc_result = concatn_loc_descriptor (rtl, initialized);
13727 break;
13729 case VAR_LOCATION:
13730 /* Single part. */
13731 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13733 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13734 if (GET_CODE (loc) == EXPR_LIST)
13735 loc = XEXP (loc, 0);
13736 loc_result = loc_descriptor (loc, mode, initialized);
13737 break;
13740 rtl = XEXP (rtl, 1);
13741 /* FALLTHRU */
13743 case PARALLEL:
13745 rtvec par_elems = XVEC (rtl, 0);
13746 int num_elem = GET_NUM_ELEM (par_elems);
13747 machine_mode mode;
13748 int i;
13750 /* Create the first one, so we have something to add to. */
13751 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13752 VOIDmode, initialized);
13753 if (loc_result == NULL)
13754 return NULL;
13755 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13756 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13757 for (i = 1; i < num_elem; i++)
13759 dw_loc_descr_ref temp;
13761 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13762 VOIDmode, initialized);
13763 if (temp == NULL)
13764 return NULL;
13765 add_loc_descr (&loc_result, temp);
13766 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13767 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13770 break;
13772 case CONST_INT:
13773 if (mode != VOIDmode && mode != BLKmode)
13774 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13775 INTVAL (rtl));
13776 break;
13778 case CONST_DOUBLE:
13779 if (mode == VOIDmode)
13780 mode = GET_MODE (rtl);
13782 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13784 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13786 /* Note that a CONST_DOUBLE rtx could represent either an integer
13787 or a floating-point constant. A CONST_DOUBLE is used whenever
13788 the constant requires more than one word in order to be
13789 adequately represented. We output CONST_DOUBLEs as blocks. */
13790 loc_result = new_loc_descr (DW_OP_implicit_value,
13791 GET_MODE_SIZE (mode), 0);
13792 #if TARGET_SUPPORTS_WIDE_INT == 0
13793 if (!SCALAR_FLOAT_MODE_P (mode))
13795 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13796 loc_result->dw_loc_oprnd2.v.val_double
13797 = rtx_to_double_int (rtl);
13799 else
13800 #endif
13802 unsigned int length = GET_MODE_SIZE (mode);
13803 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13805 insert_float (rtl, array);
13806 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13807 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13808 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13809 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13812 break;
13814 case CONST_WIDE_INT:
13815 if (mode == VOIDmode)
13816 mode = GET_MODE (rtl);
13818 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13820 loc_result = new_loc_descr (DW_OP_implicit_value,
13821 GET_MODE_SIZE (mode), 0);
13822 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13823 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13824 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13826 break;
13828 case CONST_VECTOR:
13829 if (mode == VOIDmode)
13830 mode = GET_MODE (rtl);
13832 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13834 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13835 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13836 unsigned char *array
13837 = ggc_vec_alloc<unsigned char> (length * elt_size);
13838 unsigned int i;
13839 unsigned char *p;
13840 machine_mode imode = GET_MODE_INNER (mode);
13842 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13843 switch (GET_MODE_CLASS (mode))
13845 case MODE_VECTOR_INT:
13846 for (i = 0, p = array; i < length; i++, p += elt_size)
13848 rtx elt = CONST_VECTOR_ELT (rtl, i);
13849 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13851 break;
13853 case MODE_VECTOR_FLOAT:
13854 for (i = 0, p = array; i < length; i++, p += elt_size)
13856 rtx elt = CONST_VECTOR_ELT (rtl, i);
13857 insert_float (elt, p);
13859 break;
13861 default:
13862 gcc_unreachable ();
13865 loc_result = new_loc_descr (DW_OP_implicit_value,
13866 length * elt_size, 0);
13867 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13868 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13869 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13870 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13872 break;
13874 case CONST:
13875 if (mode == VOIDmode
13876 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13877 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13878 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13880 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13881 break;
13883 /* FALLTHROUGH */
13884 case SYMBOL_REF:
13885 if (!const_ok_for_output (rtl))
13886 break;
13887 case LABEL_REF:
13888 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13889 && (dwarf_version >= 4 || !dwarf_strict))
13891 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13892 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13893 vec_safe_push (used_rtx_array, rtl);
13895 break;
13897 case DEBUG_IMPLICIT_PTR:
13898 loc_result = implicit_ptr_descriptor (rtl, 0);
13899 break;
13901 case PLUS:
13902 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13903 && CONST_INT_P (XEXP (rtl, 1)))
13905 loc_result
13906 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13907 break;
13909 /* FALLTHRU */
13910 do_default:
13911 default:
13912 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13913 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13914 && dwarf_version >= 4)
13915 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13917 /* Value expression. */
13918 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13919 if (loc_result)
13920 add_loc_descr (&loc_result,
13921 new_loc_descr (DW_OP_stack_value, 0, 0));
13923 break;
13926 return loc_result;
13929 /* We need to figure out what section we should use as the base for the
13930 address ranges where a given location is valid.
13931 1. If this particular DECL has a section associated with it, use that.
13932 2. If this function has a section associated with it, use that.
13933 3. Otherwise, use the text section.
13934 XXX: If you split a variable across multiple sections, we won't notice. */
13936 static const char *
13937 secname_for_decl (const_tree decl)
13939 const char *secname;
13941 if (VAR_OR_FUNCTION_DECL_P (decl)
13942 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13943 && DECL_SECTION_NAME (decl))
13944 secname = DECL_SECTION_NAME (decl);
13945 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13946 secname = DECL_SECTION_NAME (current_function_decl);
13947 else if (cfun && in_cold_section_p)
13948 secname = crtl->subsections.cold_section_label;
13949 else
13950 secname = text_section_label;
13952 return secname;
13955 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13957 static bool
13958 decl_by_reference_p (tree decl)
13960 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13961 || TREE_CODE (decl) == VAR_DECL)
13962 && DECL_BY_REFERENCE (decl));
13965 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13966 for VARLOC. */
13968 static dw_loc_descr_ref
13969 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13970 enum var_init_status initialized)
13972 int have_address = 0;
13973 dw_loc_descr_ref descr;
13974 machine_mode mode;
13976 if (want_address != 2)
13978 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13979 /* Single part. */
13980 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13982 varloc = PAT_VAR_LOCATION_LOC (varloc);
13983 if (GET_CODE (varloc) == EXPR_LIST)
13984 varloc = XEXP (varloc, 0);
13985 mode = GET_MODE (varloc);
13986 if (MEM_P (varloc))
13988 rtx addr = XEXP (varloc, 0);
13989 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13990 mode, initialized);
13991 if (descr)
13992 have_address = 1;
13993 else
13995 rtx x = avoid_constant_pool_reference (varloc);
13996 if (x != varloc)
13997 descr = mem_loc_descriptor (x, mode, VOIDmode,
13998 initialized);
14001 else
14002 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14004 else
14005 return 0;
14007 else
14009 if (GET_CODE (varloc) == VAR_LOCATION)
14010 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14011 else
14012 mode = DECL_MODE (loc);
14013 descr = loc_descriptor (varloc, mode, initialized);
14014 have_address = 1;
14017 if (!descr)
14018 return 0;
14020 if (want_address == 2 && !have_address
14021 && (dwarf_version >= 4 || !dwarf_strict))
14023 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14025 expansion_failed (loc, NULL_RTX,
14026 "DWARF address size mismatch");
14027 return 0;
14029 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14030 have_address = 1;
14032 /* Show if we can't fill the request for an address. */
14033 if (want_address && !have_address)
14035 expansion_failed (loc, NULL_RTX,
14036 "Want address and only have value");
14037 return 0;
14040 /* If we've got an address and don't want one, dereference. */
14041 if (!want_address && have_address)
14043 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14044 enum dwarf_location_atom op;
14046 if (size > DWARF2_ADDR_SIZE || size == -1)
14048 expansion_failed (loc, NULL_RTX,
14049 "DWARF address size mismatch");
14050 return 0;
14052 else if (size == DWARF2_ADDR_SIZE)
14053 op = DW_OP_deref;
14054 else
14055 op = DW_OP_deref_size;
14057 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14060 return descr;
14063 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14064 if it is not possible. */
14066 static dw_loc_descr_ref
14067 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14069 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14070 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14071 else if (dwarf_version >= 3 || !dwarf_strict)
14072 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14073 else
14074 return NULL;
14077 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14078 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14080 static dw_loc_descr_ref
14081 dw_sra_loc_expr (tree decl, rtx loc)
14083 rtx p;
14084 unsigned HOST_WIDE_INT padsize = 0;
14085 dw_loc_descr_ref descr, *descr_tail;
14086 unsigned HOST_WIDE_INT decl_size;
14087 rtx varloc;
14088 enum var_init_status initialized;
14090 if (DECL_SIZE (decl) == NULL
14091 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14092 return NULL;
14094 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14095 descr = NULL;
14096 descr_tail = &descr;
14098 for (p = loc; p; p = XEXP (p, 1))
14100 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14101 rtx loc_note = *decl_piece_varloc_ptr (p);
14102 dw_loc_descr_ref cur_descr;
14103 dw_loc_descr_ref *tail, last = NULL;
14104 unsigned HOST_WIDE_INT opsize = 0;
14106 if (loc_note == NULL_RTX
14107 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14109 padsize += bitsize;
14110 continue;
14112 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14113 varloc = NOTE_VAR_LOCATION (loc_note);
14114 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14115 if (cur_descr == NULL)
14117 padsize += bitsize;
14118 continue;
14121 /* Check that cur_descr either doesn't use
14122 DW_OP_*piece operations, or their sum is equal
14123 to bitsize. Otherwise we can't embed it. */
14124 for (tail = &cur_descr; *tail != NULL;
14125 tail = &(*tail)->dw_loc_next)
14126 if ((*tail)->dw_loc_opc == DW_OP_piece)
14128 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14129 * BITS_PER_UNIT;
14130 last = *tail;
14132 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14134 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14135 last = *tail;
14138 if (last != NULL && opsize != bitsize)
14140 padsize += bitsize;
14141 /* Discard the current piece of the descriptor and release any
14142 addr_table entries it uses. */
14143 remove_loc_list_addr_table_entries (cur_descr);
14144 continue;
14147 /* If there is a hole, add DW_OP_*piece after empty DWARF
14148 expression, which means that those bits are optimized out. */
14149 if (padsize)
14151 if (padsize > decl_size)
14153 remove_loc_list_addr_table_entries (cur_descr);
14154 goto discard_descr;
14156 decl_size -= padsize;
14157 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14158 if (*descr_tail == NULL)
14160 remove_loc_list_addr_table_entries (cur_descr);
14161 goto discard_descr;
14163 descr_tail = &(*descr_tail)->dw_loc_next;
14164 padsize = 0;
14166 *descr_tail = cur_descr;
14167 descr_tail = tail;
14168 if (bitsize > decl_size)
14169 goto discard_descr;
14170 decl_size -= bitsize;
14171 if (last == NULL)
14173 HOST_WIDE_INT offset = 0;
14174 if (GET_CODE (varloc) == VAR_LOCATION
14175 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14177 varloc = PAT_VAR_LOCATION_LOC (varloc);
14178 if (GET_CODE (varloc) == EXPR_LIST)
14179 varloc = XEXP (varloc, 0);
14183 if (GET_CODE (varloc) == CONST
14184 || GET_CODE (varloc) == SIGN_EXTEND
14185 || GET_CODE (varloc) == ZERO_EXTEND)
14186 varloc = XEXP (varloc, 0);
14187 else if (GET_CODE (varloc) == SUBREG)
14188 varloc = SUBREG_REG (varloc);
14189 else
14190 break;
14192 while (1);
14193 /* DW_OP_bit_size offset should be zero for register
14194 or implicit location descriptions and empty location
14195 descriptions, but for memory addresses needs big endian
14196 adjustment. */
14197 if (MEM_P (varloc))
14199 unsigned HOST_WIDE_INT memsize
14200 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14201 if (memsize != bitsize)
14203 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14204 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14205 goto discard_descr;
14206 if (memsize < bitsize)
14207 goto discard_descr;
14208 if (BITS_BIG_ENDIAN)
14209 offset = memsize - bitsize;
14213 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14214 if (*descr_tail == NULL)
14215 goto discard_descr;
14216 descr_tail = &(*descr_tail)->dw_loc_next;
14220 /* If there were any non-empty expressions, add padding till the end of
14221 the decl. */
14222 if (descr != NULL && decl_size != 0)
14224 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14225 if (*descr_tail == NULL)
14226 goto discard_descr;
14228 return descr;
14230 discard_descr:
14231 /* Discard the descriptor and release any addr_table entries it uses. */
14232 remove_loc_list_addr_table_entries (descr);
14233 return NULL;
14236 /* Return the dwarf representation of the location list LOC_LIST of
14237 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14238 function. */
14240 static dw_loc_list_ref
14241 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14243 const char *endname, *secname;
14244 rtx varloc;
14245 enum var_init_status initialized;
14246 struct var_loc_node *node;
14247 dw_loc_descr_ref descr;
14248 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14249 dw_loc_list_ref list = NULL;
14250 dw_loc_list_ref *listp = &list;
14252 /* Now that we know what section we are using for a base,
14253 actually construct the list of locations.
14254 The first location information is what is passed to the
14255 function that creates the location list, and the remaining
14256 locations just get added on to that list.
14257 Note that we only know the start address for a location
14258 (IE location changes), so to build the range, we use
14259 the range [current location start, next location start].
14260 This means we have to special case the last node, and generate
14261 a range of [last location start, end of function label]. */
14263 secname = secname_for_decl (decl);
14265 for (node = loc_list->first; node; node = node->next)
14266 if (GET_CODE (node->loc) == EXPR_LIST
14267 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14269 if (GET_CODE (node->loc) == EXPR_LIST)
14271 /* This requires DW_OP_{,bit_}piece, which is not usable
14272 inside DWARF expressions. */
14273 if (want_address != 2)
14274 continue;
14275 descr = dw_sra_loc_expr (decl, node->loc);
14276 if (descr == NULL)
14277 continue;
14279 else
14281 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14282 varloc = NOTE_VAR_LOCATION (node->loc);
14283 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14285 if (descr)
14287 bool range_across_switch = false;
14288 /* If section switch happens in between node->label
14289 and node->next->label (or end of function) and
14290 we can't emit it as a single entry list,
14291 emit two ranges, first one ending at the end
14292 of first partition and second one starting at the
14293 beginning of second partition. */
14294 if (node == loc_list->last_before_switch
14295 && (node != loc_list->first || loc_list->first->next)
14296 && current_function_decl)
14298 endname = cfun->fde->dw_fde_end;
14299 range_across_switch = true;
14301 /* The variable has a location between NODE->LABEL and
14302 NODE->NEXT->LABEL. */
14303 else if (node->next)
14304 endname = node->next->label;
14305 /* If the variable has a location at the last label
14306 it keeps its location until the end of function. */
14307 else if (!current_function_decl)
14308 endname = text_end_label;
14309 else
14311 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14312 current_function_funcdef_no);
14313 endname = ggc_strdup (label_id);
14316 *listp = new_loc_list (descr, node->label, endname, secname);
14317 if (TREE_CODE (decl) == PARM_DECL
14318 && node == loc_list->first
14319 && NOTE_P (node->loc)
14320 && strcmp (node->label, endname) == 0)
14321 (*listp)->force = true;
14322 listp = &(*listp)->dw_loc_next;
14324 if (range_across_switch)
14326 if (GET_CODE (node->loc) == EXPR_LIST)
14327 descr = dw_sra_loc_expr (decl, node->loc);
14328 else
14330 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14331 varloc = NOTE_VAR_LOCATION (node->loc);
14332 descr = dw_loc_list_1 (decl, varloc, want_address,
14333 initialized);
14335 gcc_assert (descr);
14336 /* The variable has a location between NODE->LABEL and
14337 NODE->NEXT->LABEL. */
14338 if (node->next)
14339 endname = node->next->label;
14340 else
14341 endname = cfun->fde->dw_fde_second_end;
14342 *listp = new_loc_list (descr,
14343 cfun->fde->dw_fde_second_begin,
14344 endname, secname);
14345 listp = &(*listp)->dw_loc_next;
14350 /* Try to avoid the overhead of a location list emitting a location
14351 expression instead, but only if we didn't have more than one
14352 location entry in the first place. If some entries were not
14353 representable, we don't want to pretend a single entry that was
14354 applies to the entire scope in which the variable is
14355 available. */
14356 if (list && loc_list->first->next)
14357 gen_llsym (list);
14359 return list;
14362 /* Return if the loc_list has only single element and thus can be represented
14363 as location description. */
14365 static bool
14366 single_element_loc_list_p (dw_loc_list_ref list)
14368 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14369 return !list->ll_symbol;
14372 /* To each location in list LIST add loc descr REF. */
14374 static void
14375 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14377 dw_loc_descr_ref copy;
14378 add_loc_descr (&list->expr, ref);
14379 list = list->dw_loc_next;
14380 while (list)
14382 copy = ggc_alloc<dw_loc_descr_node> ();
14383 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14384 add_loc_descr (&list->expr, copy);
14385 while (copy->dw_loc_next)
14387 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14388 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14389 copy->dw_loc_next = new_copy;
14390 copy = new_copy;
14392 list = list->dw_loc_next;
14396 /* Given two lists RET and LIST
14397 produce location list that is result of adding expression in LIST
14398 to expression in RET on each position in program.
14399 Might be destructive on both RET and LIST.
14401 TODO: We handle only simple cases of RET or LIST having at most one
14402 element. General case would inolve sorting the lists in program order
14403 and merging them that will need some additional work.
14404 Adding that will improve quality of debug info especially for SRA-ed
14405 structures. */
14407 static void
14408 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14410 if (!list)
14411 return;
14412 if (!*ret)
14414 *ret = list;
14415 return;
14417 if (!list->dw_loc_next)
14419 add_loc_descr_to_each (*ret, list->expr);
14420 return;
14422 if (!(*ret)->dw_loc_next)
14424 add_loc_descr_to_each (list, (*ret)->expr);
14425 *ret = list;
14426 return;
14428 expansion_failed (NULL_TREE, NULL_RTX,
14429 "Don't know how to merge two non-trivial"
14430 " location lists.\n");
14431 *ret = NULL;
14432 return;
14435 /* LOC is constant expression. Try a luck, look it up in constant
14436 pool and return its loc_descr of its address. */
14438 static dw_loc_descr_ref
14439 cst_pool_loc_descr (tree loc)
14441 /* Get an RTL for this, if something has been emitted. */
14442 rtx rtl = lookup_constant_def (loc);
14444 if (!rtl || !MEM_P (rtl))
14446 gcc_assert (!rtl);
14447 return 0;
14449 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14451 /* TODO: We might get more coverage if we was actually delaying expansion
14452 of all expressions till end of compilation when constant pools are fully
14453 populated. */
14454 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14456 expansion_failed (loc, NULL_RTX,
14457 "CST value in contant pool but not marked.");
14458 return 0;
14460 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14461 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14464 /* Return dw_loc_list representing address of addr_expr LOC
14465 by looking for inner INDIRECT_REF expression and turning
14466 it into simple arithmetics.
14468 See loc_list_from_tree for the meaning of CONTEXT. */
14470 static dw_loc_list_ref
14471 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14472 const loc_descr_context *context)
14474 tree obj, offset;
14475 HOST_WIDE_INT bitsize, bitpos, bytepos;
14476 machine_mode mode;
14477 int unsignedp, reversep, volatilep = 0;
14478 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14480 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14481 &bitsize, &bitpos, &offset, &mode,
14482 &unsignedp, &reversep, &volatilep, false);
14483 STRIP_NOPS (obj);
14484 if (bitpos % BITS_PER_UNIT)
14486 expansion_failed (loc, NULL_RTX, "bitfield access");
14487 return 0;
14489 if (!INDIRECT_REF_P (obj))
14491 expansion_failed (obj,
14492 NULL_RTX, "no indirect ref in inner refrence");
14493 return 0;
14495 if (!offset && !bitpos)
14496 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14497 context);
14498 else if (toplev
14499 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14500 && (dwarf_version >= 4 || !dwarf_strict))
14502 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14503 if (!list_ret)
14504 return 0;
14505 if (offset)
14507 /* Variable offset. */
14508 list_ret1 = loc_list_from_tree (offset, 0, context);
14509 if (list_ret1 == 0)
14510 return 0;
14511 add_loc_list (&list_ret, list_ret1);
14512 if (!list_ret)
14513 return 0;
14514 add_loc_descr_to_each (list_ret,
14515 new_loc_descr (DW_OP_plus, 0, 0));
14517 bytepos = bitpos / BITS_PER_UNIT;
14518 if (bytepos > 0)
14519 add_loc_descr_to_each (list_ret,
14520 new_loc_descr (DW_OP_plus_uconst,
14521 bytepos, 0));
14522 else if (bytepos < 0)
14523 loc_list_plus_const (list_ret, bytepos);
14524 add_loc_descr_to_each (list_ret,
14525 new_loc_descr (DW_OP_stack_value, 0, 0));
14527 return list_ret;
14531 /* Helper structure for location descriptions generation. */
14532 struct loc_descr_context
14534 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14535 NULL_TREE if DW_OP_push_object_address in invalid for this location
14536 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14537 tree context_type;
14538 /* The ..._DECL node that should be translated as a
14539 DW_OP_push_object_address operation. */
14540 tree base_decl;
14543 /* Generate Dwarf location list representing LOC.
14544 If WANT_ADDRESS is false, expression computing LOC will be computed
14545 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14546 if WANT_ADDRESS is 2, expression computing address useable in location
14547 will be returned (i.e. DW_OP_reg can be used
14548 to refer to register values).
14550 CONTEXT provides information to customize the location descriptions
14551 generation. Its context_type field specifies what type is implicitly
14552 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14553 will not be generated.
14555 If CONTEXT is NULL, the behavior is the same as if both context_type and
14556 base_decl fields were NULL_TREE. */
14558 static dw_loc_list_ref
14559 loc_list_from_tree (tree loc, int want_address,
14560 const struct loc_descr_context *context)
14562 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14563 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14564 int have_address = 0;
14565 enum dwarf_location_atom op;
14567 /* ??? Most of the time we do not take proper care for sign/zero
14568 extending the values properly. Hopefully this won't be a real
14569 problem... */
14571 if (context != NULL
14572 && context->base_decl == loc
14573 && want_address == 0)
14575 if (dwarf_version >= 3 || !dwarf_strict)
14576 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14577 NULL, NULL, NULL);
14578 else
14579 return NULL;
14582 switch (TREE_CODE (loc))
14584 case ERROR_MARK:
14585 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14586 return 0;
14588 case PLACEHOLDER_EXPR:
14589 /* This case involves extracting fields from an object to determine the
14590 position of other fields. It is supposed to appear only as the first
14591 operand of COMPONENT_REF nodes and to reference precisely the type
14592 that the context allows. */
14593 if (context != NULL
14594 && TREE_TYPE (loc) == context->context_type
14595 && want_address >= 1)
14597 if (dwarf_version >= 3 || !dwarf_strict)
14599 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14600 have_address = 1;
14601 break;
14603 else
14604 return NULL;
14606 else
14607 expansion_failed (loc, NULL_RTX,
14608 "PLACEHOLDER_EXPR for an unexpected type");
14609 break;
14611 case CALL_EXPR:
14612 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14613 /* There are no opcodes for these operations. */
14614 return 0;
14616 case PREINCREMENT_EXPR:
14617 case PREDECREMENT_EXPR:
14618 case POSTINCREMENT_EXPR:
14619 case POSTDECREMENT_EXPR:
14620 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14621 /* There are no opcodes for these operations. */
14622 return 0;
14624 case ADDR_EXPR:
14625 /* If we already want an address, see if there is INDIRECT_REF inside
14626 e.g. for &this->field. */
14627 if (want_address)
14629 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14630 (loc, want_address == 2, context);
14631 if (list_ret)
14632 have_address = 1;
14633 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14634 && (ret = cst_pool_loc_descr (loc)))
14635 have_address = 1;
14637 /* Otherwise, process the argument and look for the address. */
14638 if (!list_ret && !ret)
14639 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14640 else
14642 if (want_address)
14643 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14644 return NULL;
14646 break;
14648 case VAR_DECL:
14649 if (DECL_THREAD_LOCAL_P (loc))
14651 rtx rtl;
14652 enum dwarf_location_atom tls_op;
14653 enum dtprel_bool dtprel = dtprel_false;
14655 if (targetm.have_tls)
14657 /* If this is not defined, we have no way to emit the
14658 data. */
14659 if (!targetm.asm_out.output_dwarf_dtprel)
14660 return 0;
14662 /* The way DW_OP_GNU_push_tls_address is specified, we
14663 can only look up addresses of objects in the current
14664 module. We used DW_OP_addr as first op, but that's
14665 wrong, because DW_OP_addr is relocated by the debug
14666 info consumer, while DW_OP_GNU_push_tls_address
14667 operand shouldn't be. */
14668 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14669 return 0;
14670 dtprel = dtprel_true;
14671 tls_op = DW_OP_GNU_push_tls_address;
14673 else
14675 if (!targetm.emutls.debug_form_tls_address
14676 || !(dwarf_version >= 3 || !dwarf_strict))
14677 return 0;
14678 /* We stuffed the control variable into the DECL_VALUE_EXPR
14679 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14680 no longer appear in gimple code. We used the control
14681 variable in specific so that we could pick it up here. */
14682 loc = DECL_VALUE_EXPR (loc);
14683 tls_op = DW_OP_form_tls_address;
14686 rtl = rtl_for_decl_location (loc);
14687 if (rtl == NULL_RTX)
14688 return 0;
14690 if (!MEM_P (rtl))
14691 return 0;
14692 rtl = XEXP (rtl, 0);
14693 if (! CONSTANT_P (rtl))
14694 return 0;
14696 ret = new_addr_loc_descr (rtl, dtprel);
14697 ret1 = new_loc_descr (tls_op, 0, 0);
14698 add_loc_descr (&ret, ret1);
14700 have_address = 1;
14701 break;
14703 /* FALLTHRU */
14705 case PARM_DECL:
14706 case RESULT_DECL:
14707 if (DECL_HAS_VALUE_EXPR_P (loc))
14708 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14709 want_address, context);
14710 /* FALLTHRU */
14712 case FUNCTION_DECL:
14714 rtx rtl;
14715 var_loc_list *loc_list = lookup_decl_loc (loc);
14717 if (loc_list && loc_list->first)
14719 list_ret = dw_loc_list (loc_list, loc, want_address);
14720 have_address = want_address != 0;
14721 break;
14723 rtl = rtl_for_decl_location (loc);
14724 if (rtl == NULL_RTX)
14726 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14727 return 0;
14729 else if (CONST_INT_P (rtl))
14731 HOST_WIDE_INT val = INTVAL (rtl);
14732 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14733 val &= GET_MODE_MASK (DECL_MODE (loc));
14734 ret = int_loc_descriptor (val);
14736 else if (GET_CODE (rtl) == CONST_STRING)
14738 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14739 return 0;
14741 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14742 ret = new_addr_loc_descr (rtl, dtprel_false);
14743 else
14745 machine_mode mode, mem_mode;
14747 /* Certain constructs can only be represented at top-level. */
14748 if (want_address == 2)
14750 ret = loc_descriptor (rtl, VOIDmode,
14751 VAR_INIT_STATUS_INITIALIZED);
14752 have_address = 1;
14754 else
14756 mode = GET_MODE (rtl);
14757 mem_mode = VOIDmode;
14758 if (MEM_P (rtl))
14760 mem_mode = mode;
14761 mode = get_address_mode (rtl);
14762 rtl = XEXP (rtl, 0);
14763 have_address = 1;
14765 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14766 VAR_INIT_STATUS_INITIALIZED);
14768 if (!ret)
14769 expansion_failed (loc, rtl,
14770 "failed to produce loc descriptor for rtl");
14773 break;
14775 case MEM_REF:
14776 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14778 have_address = 1;
14779 goto do_plus;
14781 /* Fallthru. */
14782 case INDIRECT_REF:
14783 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14784 have_address = 1;
14785 break;
14787 case TARGET_MEM_REF:
14788 case SSA_NAME:
14789 case DEBUG_EXPR_DECL:
14790 return NULL;
14792 case COMPOUND_EXPR:
14793 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14795 CASE_CONVERT:
14796 case VIEW_CONVERT_EXPR:
14797 case SAVE_EXPR:
14798 case MODIFY_EXPR:
14799 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14801 case COMPONENT_REF:
14802 case BIT_FIELD_REF:
14803 case ARRAY_REF:
14804 case ARRAY_RANGE_REF:
14805 case REALPART_EXPR:
14806 case IMAGPART_EXPR:
14808 tree obj, offset;
14809 HOST_WIDE_INT bitsize, bitpos, bytepos;
14810 machine_mode mode;
14811 int unsignedp, reversep, volatilep = 0;
14813 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14814 &unsignedp, &reversep, &volatilep, false);
14816 gcc_assert (obj != loc);
14818 list_ret = loc_list_from_tree (obj,
14819 want_address == 2
14820 && !bitpos && !offset ? 2 : 1,
14821 context);
14822 /* TODO: We can extract value of the small expression via shifting even
14823 for nonzero bitpos. */
14824 if (list_ret == 0)
14825 return 0;
14826 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14828 expansion_failed (loc, NULL_RTX,
14829 "bitfield access");
14830 return 0;
14833 if (offset != NULL_TREE)
14835 /* Variable offset. */
14836 list_ret1 = loc_list_from_tree (offset, 0, context);
14837 if (list_ret1 == 0)
14838 return 0;
14839 add_loc_list (&list_ret, list_ret1);
14840 if (!list_ret)
14841 return 0;
14842 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14845 bytepos = bitpos / BITS_PER_UNIT;
14846 if (bytepos > 0)
14847 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14848 else if (bytepos < 0)
14849 loc_list_plus_const (list_ret, bytepos);
14851 have_address = 1;
14852 break;
14855 case INTEGER_CST:
14856 if ((want_address || !tree_fits_shwi_p (loc))
14857 && (ret = cst_pool_loc_descr (loc)))
14858 have_address = 1;
14859 else if (want_address == 2
14860 && tree_fits_shwi_p (loc)
14861 && (ret = address_of_int_loc_descriptor
14862 (int_size_in_bytes (TREE_TYPE (loc)),
14863 tree_to_shwi (loc))))
14864 have_address = 1;
14865 else if (tree_fits_shwi_p (loc))
14866 ret = int_loc_descriptor (tree_to_shwi (loc));
14867 else
14869 expansion_failed (loc, NULL_RTX,
14870 "Integer operand is not host integer");
14871 return 0;
14873 break;
14875 case CONSTRUCTOR:
14876 case REAL_CST:
14877 case STRING_CST:
14878 case COMPLEX_CST:
14879 if ((ret = cst_pool_loc_descr (loc)))
14880 have_address = 1;
14881 else
14882 /* We can construct small constants here using int_loc_descriptor. */
14883 expansion_failed (loc, NULL_RTX,
14884 "constructor or constant not in constant pool");
14885 break;
14887 case TRUTH_AND_EXPR:
14888 case TRUTH_ANDIF_EXPR:
14889 case BIT_AND_EXPR:
14890 op = DW_OP_and;
14891 goto do_binop;
14893 case TRUTH_XOR_EXPR:
14894 case BIT_XOR_EXPR:
14895 op = DW_OP_xor;
14896 goto do_binop;
14898 case TRUTH_OR_EXPR:
14899 case TRUTH_ORIF_EXPR:
14900 case BIT_IOR_EXPR:
14901 op = DW_OP_or;
14902 goto do_binop;
14904 case FLOOR_DIV_EXPR:
14905 case CEIL_DIV_EXPR:
14906 case ROUND_DIV_EXPR:
14907 case TRUNC_DIV_EXPR:
14908 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14909 return 0;
14910 op = DW_OP_div;
14911 goto do_binop;
14913 case MINUS_EXPR:
14914 op = DW_OP_minus;
14915 goto do_binop;
14917 case FLOOR_MOD_EXPR:
14918 case CEIL_MOD_EXPR:
14919 case ROUND_MOD_EXPR:
14920 case TRUNC_MOD_EXPR:
14921 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14923 op = DW_OP_mod;
14924 goto do_binop;
14926 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14927 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14928 if (list_ret == 0 || list_ret1 == 0)
14929 return 0;
14931 add_loc_list (&list_ret, list_ret1);
14932 if (list_ret == 0)
14933 return 0;
14934 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14935 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14936 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14937 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14938 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14939 break;
14941 case MULT_EXPR:
14942 op = DW_OP_mul;
14943 goto do_binop;
14945 case LSHIFT_EXPR:
14946 op = DW_OP_shl;
14947 goto do_binop;
14949 case RSHIFT_EXPR:
14950 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14951 goto do_binop;
14953 case POINTER_PLUS_EXPR:
14954 case PLUS_EXPR:
14955 do_plus:
14956 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14958 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14959 if (list_ret == 0)
14960 return 0;
14962 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14963 break;
14966 op = DW_OP_plus;
14967 goto do_binop;
14969 case LE_EXPR:
14970 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14971 return 0;
14973 op = DW_OP_le;
14974 goto do_binop;
14976 case GE_EXPR:
14977 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14978 return 0;
14980 op = DW_OP_ge;
14981 goto do_binop;
14983 case LT_EXPR:
14984 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14985 return 0;
14987 op = DW_OP_lt;
14988 goto do_binop;
14990 case GT_EXPR:
14991 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14992 return 0;
14994 op = DW_OP_gt;
14995 goto do_binop;
14997 case EQ_EXPR:
14998 op = DW_OP_eq;
14999 goto do_binop;
15001 case NE_EXPR:
15002 op = DW_OP_ne;
15003 goto do_binop;
15005 do_binop:
15006 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15007 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
15008 if (list_ret == 0 || list_ret1 == 0)
15009 return 0;
15011 add_loc_list (&list_ret, list_ret1);
15012 if (list_ret == 0)
15013 return 0;
15014 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15015 break;
15017 case TRUTH_NOT_EXPR:
15018 case BIT_NOT_EXPR:
15019 op = DW_OP_not;
15020 goto do_unop;
15022 case ABS_EXPR:
15023 op = DW_OP_abs;
15024 goto do_unop;
15026 case NEGATE_EXPR:
15027 op = DW_OP_neg;
15028 goto do_unop;
15030 do_unop:
15031 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15032 if (list_ret == 0)
15033 return 0;
15035 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15036 break;
15038 case MIN_EXPR:
15039 case MAX_EXPR:
15041 const enum tree_code code =
15042 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15044 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15045 build2 (code, integer_type_node,
15046 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15047 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15050 /* ... fall through ... */
15052 case COND_EXPR:
15054 dw_loc_descr_ref lhs
15055 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15056 dw_loc_list_ref rhs
15057 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15058 dw_loc_descr_ref bra_node, jump_node, tmp;
15060 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15061 if (list_ret == 0 || lhs == 0 || rhs == 0)
15062 return 0;
15064 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15065 add_loc_descr_to_each (list_ret, bra_node);
15067 add_loc_list (&list_ret, rhs);
15068 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15069 add_loc_descr_to_each (list_ret, jump_node);
15071 add_loc_descr_to_each (list_ret, lhs);
15072 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15073 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15075 /* ??? Need a node to point the skip at. Use a nop. */
15076 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15077 add_loc_descr_to_each (list_ret, tmp);
15078 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15079 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15081 break;
15083 case FIX_TRUNC_EXPR:
15084 return 0;
15086 default:
15087 /* Leave front-end specific codes as simply unknown. This comes
15088 up, for instance, with the C STMT_EXPR. */
15089 if ((unsigned int) TREE_CODE (loc)
15090 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15092 expansion_failed (loc, NULL_RTX,
15093 "language specific tree node");
15094 return 0;
15097 /* Otherwise this is a generic code; we should just lists all of
15098 these explicitly. We forgot one. */
15099 if (flag_checking)
15100 gcc_unreachable ();
15102 /* In a release build, we want to degrade gracefully: better to
15103 generate incomplete debugging information than to crash. */
15104 return NULL;
15107 if (!ret && !list_ret)
15108 return 0;
15110 if (want_address == 2 && !have_address
15111 && (dwarf_version >= 4 || !dwarf_strict))
15113 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15115 expansion_failed (loc, NULL_RTX,
15116 "DWARF address size mismatch");
15117 return 0;
15119 if (ret)
15120 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15121 else
15122 add_loc_descr_to_each (list_ret,
15123 new_loc_descr (DW_OP_stack_value, 0, 0));
15124 have_address = 1;
15126 /* Show if we can't fill the request for an address. */
15127 if (want_address && !have_address)
15129 expansion_failed (loc, NULL_RTX,
15130 "Want address and only have value");
15131 return 0;
15134 gcc_assert (!ret || !list_ret);
15136 /* If we've got an address and don't want one, dereference. */
15137 if (!want_address && have_address)
15139 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15141 if (size > DWARF2_ADDR_SIZE || size == -1)
15143 expansion_failed (loc, NULL_RTX,
15144 "DWARF address size mismatch");
15145 return 0;
15147 else if (size == DWARF2_ADDR_SIZE)
15148 op = DW_OP_deref;
15149 else
15150 op = DW_OP_deref_size;
15152 if (ret)
15153 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15154 else
15155 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15157 if (ret)
15158 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15160 return list_ret;
15163 /* Same as above but return only single location expression. */
15164 static dw_loc_descr_ref
15165 loc_descriptor_from_tree (tree loc, int want_address,
15166 const struct loc_descr_context *context)
15168 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15169 if (!ret)
15170 return NULL;
15171 if (ret->dw_loc_next)
15173 expansion_failed (loc, NULL_RTX,
15174 "Location list where only loc descriptor needed");
15175 return NULL;
15177 return ret->expr;
15180 /* Given a value, round it up to the lowest multiple of `boundary'
15181 which is not less than the value itself. */
15183 static inline HOST_WIDE_INT
15184 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15186 return (((value + boundary - 1) / boundary) * boundary);
15189 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15190 pointer to the declared type for the relevant field variable, or return
15191 `integer_type_node' if the given node turns out to be an
15192 ERROR_MARK node. */
15194 static inline tree
15195 field_type (const_tree decl)
15197 tree type;
15199 if (TREE_CODE (decl) == ERROR_MARK)
15200 return integer_type_node;
15202 type = DECL_BIT_FIELD_TYPE (decl);
15203 if (type == NULL_TREE)
15204 type = TREE_TYPE (decl);
15206 return type;
15209 /* Given a pointer to a tree node, return the alignment in bits for
15210 it, or else return BITS_PER_WORD if the node actually turns out to
15211 be an ERROR_MARK node. */
15213 static inline unsigned
15214 simple_type_align_in_bits (const_tree type)
15216 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15219 static inline unsigned
15220 simple_decl_align_in_bits (const_tree decl)
15222 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15225 /* Return the result of rounding T up to ALIGN. */
15227 static inline offset_int
15228 round_up_to_align (const offset_int &t, unsigned int align)
15230 return wi::udiv_trunc (t + align - 1, align) * align;
15233 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15234 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15235 or return 0 if we are unable to determine what that offset is, either
15236 because the argument turns out to be a pointer to an ERROR_MARK node, or
15237 because the offset is actually variable. (We can't handle the latter case
15238 just yet). */
15240 static HOST_WIDE_INT
15241 field_byte_offset (const_tree decl)
15243 offset_int object_offset_in_bits;
15244 offset_int object_offset_in_bytes;
15245 offset_int bitpos_int;
15247 if (TREE_CODE (decl) == ERROR_MARK)
15248 return 0;
15250 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15252 /* We cannot yet cope with fields whose positions are variable, so
15253 for now, when we see such things, we simply return 0. Someday, we may
15254 be able to handle such cases, but it will be damn difficult. */
15255 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15256 return 0;
15258 bitpos_int = wi::to_offset (bit_position (decl));
15260 if (PCC_BITFIELD_TYPE_MATTERS)
15262 tree type;
15263 tree field_size_tree;
15264 offset_int deepest_bitpos;
15265 offset_int field_size_in_bits;
15266 unsigned int type_align_in_bits;
15267 unsigned int decl_align_in_bits;
15268 offset_int type_size_in_bits;
15270 type = field_type (decl);
15271 type_size_in_bits = offset_int_type_size_in_bits (type);
15272 type_align_in_bits = simple_type_align_in_bits (type);
15274 field_size_tree = DECL_SIZE (decl);
15276 /* The size could be unspecified if there was an error, or for
15277 a flexible array member. */
15278 if (!field_size_tree)
15279 field_size_tree = bitsize_zero_node;
15281 /* If the size of the field is not constant, use the type size. */
15282 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15283 field_size_in_bits = wi::to_offset (field_size_tree);
15284 else
15285 field_size_in_bits = type_size_in_bits;
15287 decl_align_in_bits = simple_decl_align_in_bits (decl);
15289 /* The GCC front-end doesn't make any attempt to keep track of the
15290 starting bit offset (relative to the start of the containing
15291 structure type) of the hypothetical "containing object" for a
15292 bit-field. Thus, when computing the byte offset value for the
15293 start of the "containing object" of a bit-field, we must deduce
15294 this information on our own. This can be rather tricky to do in
15295 some cases. For example, handling the following structure type
15296 definition when compiling for an i386/i486 target (which only
15297 aligns long long's to 32-bit boundaries) can be very tricky:
15299 struct S { int field1; long long field2:31; };
15301 Fortunately, there is a simple rule-of-thumb which can be used
15302 in such cases. When compiling for an i386/i486, GCC will
15303 allocate 8 bytes for the structure shown above. It decides to
15304 do this based upon one simple rule for bit-field allocation.
15305 GCC allocates each "containing object" for each bit-field at
15306 the first (i.e. lowest addressed) legitimate alignment boundary
15307 (based upon the required minimum alignment for the declared
15308 type of the field) which it can possibly use, subject to the
15309 condition that there is still enough available space remaining
15310 in the containing object (when allocated at the selected point)
15311 to fully accommodate all of the bits of the bit-field itself.
15313 This simple rule makes it obvious why GCC allocates 8 bytes for
15314 each object of the structure type shown above. When looking
15315 for a place to allocate the "containing object" for `field2',
15316 the compiler simply tries to allocate a 64-bit "containing
15317 object" at each successive 32-bit boundary (starting at zero)
15318 until it finds a place to allocate that 64- bit field such that
15319 at least 31 contiguous (and previously unallocated) bits remain
15320 within that selected 64 bit field. (As it turns out, for the
15321 example above, the compiler finds it is OK to allocate the
15322 "containing object" 64-bit field at bit-offset zero within the
15323 structure type.)
15325 Here we attempt to work backwards from the limited set of facts
15326 we're given, and we try to deduce from those facts, where GCC
15327 must have believed that the containing object started (within
15328 the structure type). The value we deduce is then used (by the
15329 callers of this routine) to generate DW_AT_location and
15330 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15331 the case of DW_AT_location, regular fields as well). */
15333 /* Figure out the bit-distance from the start of the structure to
15334 the "deepest" bit of the bit-field. */
15335 deepest_bitpos = bitpos_int + field_size_in_bits;
15337 /* This is the tricky part. Use some fancy footwork to deduce
15338 where the lowest addressed bit of the containing object must
15339 be. */
15340 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15342 /* Round up to type_align by default. This works best for
15343 bitfields. */
15344 object_offset_in_bits
15345 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15347 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15349 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15351 /* Round up to decl_align instead. */
15352 object_offset_in_bits
15353 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15356 else
15357 object_offset_in_bits = bitpos_int;
15359 object_offset_in_bytes
15360 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15361 return object_offset_in_bytes.to_shwi ();
15364 /* The following routines define various Dwarf attributes and any data
15365 associated with them. */
15367 /* Add a location description attribute value to a DIE.
15369 This emits location attributes suitable for whole variables and
15370 whole parameters. Note that the location attributes for struct fields are
15371 generated by the routine `data_member_location_attribute' below. */
15373 static inline void
15374 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15375 dw_loc_list_ref descr)
15377 if (descr == 0)
15378 return;
15379 if (single_element_loc_list_p (descr))
15380 add_AT_loc (die, attr_kind, descr->expr);
15381 else
15382 add_AT_loc_list (die, attr_kind, descr);
15385 /* Add DW_AT_accessibility attribute to DIE if needed. */
15387 static void
15388 add_accessibility_attribute (dw_die_ref die, tree decl)
15390 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15391 children, otherwise the default is DW_ACCESS_public. In DWARF2
15392 the default has always been DW_ACCESS_public. */
15393 if (TREE_PROTECTED (decl))
15394 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15395 else if (TREE_PRIVATE (decl))
15397 if (dwarf_version == 2
15398 || die->die_parent == NULL
15399 || die->die_parent->die_tag != DW_TAG_class_type)
15400 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15402 else if (dwarf_version > 2
15403 && die->die_parent
15404 && die->die_parent->die_tag == DW_TAG_class_type)
15405 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15408 /* Attach the specialized form of location attribute used for data members of
15409 struct and union types. In the special case of a FIELD_DECL node which
15410 represents a bit-field, the "offset" part of this special location
15411 descriptor must indicate the distance in bytes from the lowest-addressed
15412 byte of the containing struct or union type to the lowest-addressed byte of
15413 the "containing object" for the bit-field. (See the `field_byte_offset'
15414 function above).
15416 For any given bit-field, the "containing object" is a hypothetical object
15417 (of some integral or enum type) within which the given bit-field lives. The
15418 type of this hypothetical "containing object" is always the same as the
15419 declared type of the individual bit-field itself (for GCC anyway... the
15420 DWARF spec doesn't actually mandate this). Note that it is the size (in
15421 bytes) of the hypothetical "containing object" which will be given in the
15422 DW_AT_byte_size attribute for this bit-field. (See the
15423 `byte_size_attribute' function below.) It is also used when calculating the
15424 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15425 function below.) */
15427 static void
15428 add_data_member_location_attribute (dw_die_ref die, tree decl)
15430 HOST_WIDE_INT offset;
15431 dw_loc_descr_ref loc_descr = 0;
15433 if (TREE_CODE (decl) == TREE_BINFO)
15435 /* We're working on the TAG_inheritance for a base class. */
15436 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15438 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15439 aren't at a fixed offset from all (sub)objects of the same
15440 type. We need to extract the appropriate offset from our
15441 vtable. The following dwarf expression means
15443 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15445 This is specific to the V3 ABI, of course. */
15447 dw_loc_descr_ref tmp;
15449 /* Make a copy of the object address. */
15450 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15451 add_loc_descr (&loc_descr, tmp);
15453 /* Extract the vtable address. */
15454 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15455 add_loc_descr (&loc_descr, tmp);
15457 /* Calculate the address of the offset. */
15458 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15459 gcc_assert (offset < 0);
15461 tmp = int_loc_descriptor (-offset);
15462 add_loc_descr (&loc_descr, tmp);
15463 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15464 add_loc_descr (&loc_descr, tmp);
15466 /* Extract the offset. */
15467 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15468 add_loc_descr (&loc_descr, tmp);
15470 /* Add it to the object address. */
15471 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15472 add_loc_descr (&loc_descr, tmp);
15474 else
15475 offset = tree_to_shwi (BINFO_OFFSET (decl));
15477 else
15478 offset = field_byte_offset (decl);
15480 if (! loc_descr)
15482 if (dwarf_version > 2)
15484 /* Don't need to output a location expression, just the constant. */
15485 if (offset < 0)
15486 add_AT_int (die, DW_AT_data_member_location, offset);
15487 else
15488 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15489 return;
15491 else
15493 enum dwarf_location_atom op;
15495 /* The DWARF2 standard says that we should assume that the structure
15496 address is already on the stack, so we can specify a structure
15497 field address by using DW_OP_plus_uconst. */
15498 op = DW_OP_plus_uconst;
15499 loc_descr = new_loc_descr (op, offset, 0);
15503 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15506 /* Writes integer values to dw_vec_const array. */
15508 static void
15509 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15511 while (size != 0)
15513 *dest++ = val & 0xff;
15514 val >>= 8;
15515 --size;
15519 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15521 static HOST_WIDE_INT
15522 extract_int (const unsigned char *src, unsigned int size)
15524 HOST_WIDE_INT val = 0;
15526 src += size;
15527 while (size != 0)
15529 val <<= 8;
15530 val |= *--src & 0xff;
15531 --size;
15533 return val;
15536 /* Writes wide_int values to dw_vec_const array. */
15538 static void
15539 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15541 int i;
15543 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15545 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15546 return;
15549 /* We'd have to extend this code to support odd sizes. */
15550 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15552 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15554 if (WORDS_BIG_ENDIAN)
15555 for (i = n - 1; i >= 0; i--)
15557 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15558 dest += sizeof (HOST_WIDE_INT);
15560 else
15561 for (i = 0; i < n; i++)
15563 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15564 dest += sizeof (HOST_WIDE_INT);
15568 /* Writes floating point values to dw_vec_const array. */
15570 static void
15571 insert_float (const_rtx rtl, unsigned char *array)
15573 long val[4];
15574 int i;
15576 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
15578 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15579 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15581 insert_int (val[i], 4, array);
15582 array += 4;
15586 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15587 does not have a "location" either in memory or in a register. These
15588 things can arise in GNU C when a constant is passed as an actual parameter
15589 to an inlined function. They can also arise in C++ where declared
15590 constants do not necessarily get memory "homes". */
15592 static bool
15593 add_const_value_attribute (dw_die_ref die, rtx rtl)
15595 switch (GET_CODE (rtl))
15597 case CONST_INT:
15599 HOST_WIDE_INT val = INTVAL (rtl);
15601 if (val < 0)
15602 add_AT_int (die, DW_AT_const_value, val);
15603 else
15604 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15606 return true;
15608 case CONST_WIDE_INT:
15610 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
15611 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
15612 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
15613 wide_int w = wi::zext (w1, prec);
15614 add_AT_wide (die, DW_AT_const_value, w);
15616 return true;
15618 case CONST_DOUBLE:
15619 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15620 floating-point constant. A CONST_DOUBLE is used whenever the
15621 constant requires more than one word in order to be adequately
15622 represented. */
15624 machine_mode mode = GET_MODE (rtl);
15626 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15627 add_AT_double (die, DW_AT_const_value,
15628 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15629 else
15631 unsigned int length = GET_MODE_SIZE (mode);
15632 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15634 insert_float (rtl, array);
15635 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15638 return true;
15640 case CONST_VECTOR:
15642 machine_mode mode = GET_MODE (rtl);
15643 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15644 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15645 unsigned char *array
15646 = ggc_vec_alloc<unsigned char> (length * elt_size);
15647 unsigned int i;
15648 unsigned char *p;
15649 machine_mode imode = GET_MODE_INNER (mode);
15651 switch (GET_MODE_CLASS (mode))
15653 case MODE_VECTOR_INT:
15654 for (i = 0, p = array; i < length; i++, p += elt_size)
15656 rtx elt = CONST_VECTOR_ELT (rtl, i);
15657 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15659 break;
15661 case MODE_VECTOR_FLOAT:
15662 for (i = 0, p = array; i < length; i++, p += elt_size)
15664 rtx elt = CONST_VECTOR_ELT (rtl, i);
15665 insert_float (elt, p);
15667 break;
15669 default:
15670 gcc_unreachable ();
15673 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15675 return true;
15677 case CONST_STRING:
15678 if (dwarf_version >= 4 || !dwarf_strict)
15680 dw_loc_descr_ref loc_result;
15681 resolve_one_addr (&rtl);
15682 rtl_addr:
15683 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15684 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15685 add_AT_loc (die, DW_AT_location, loc_result);
15686 vec_safe_push (used_rtx_array, rtl);
15687 return true;
15689 return false;
15691 case CONST:
15692 if (CONSTANT_P (XEXP (rtl, 0)))
15693 return add_const_value_attribute (die, XEXP (rtl, 0));
15694 /* FALLTHROUGH */
15695 case SYMBOL_REF:
15696 if (!const_ok_for_output (rtl))
15697 return false;
15698 case LABEL_REF:
15699 if (dwarf_version >= 4 || !dwarf_strict)
15700 goto rtl_addr;
15701 return false;
15703 case PLUS:
15704 /* In cases where an inlined instance of an inline function is passed
15705 the address of an `auto' variable (which is local to the caller) we
15706 can get a situation where the DECL_RTL of the artificial local
15707 variable (for the inlining) which acts as a stand-in for the
15708 corresponding formal parameter (of the inline function) will look
15709 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15710 exactly a compile-time constant expression, but it isn't the address
15711 of the (artificial) local variable either. Rather, it represents the
15712 *value* which the artificial local variable always has during its
15713 lifetime. We currently have no way to represent such quasi-constant
15714 values in Dwarf, so for now we just punt and generate nothing. */
15715 return false;
15717 case HIGH:
15718 case CONST_FIXED:
15719 return false;
15721 case MEM:
15722 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15723 && MEM_READONLY_P (rtl)
15724 && GET_MODE (rtl) == BLKmode)
15726 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15727 return true;
15729 return false;
15731 default:
15732 /* No other kinds of rtx should be possible here. */
15733 gcc_unreachable ();
15735 return false;
15738 /* Determine whether the evaluation of EXPR references any variables
15739 or functions which aren't otherwise used (and therefore may not be
15740 output). */
15741 static tree
15742 reference_to_unused (tree * tp, int * walk_subtrees,
15743 void * data ATTRIBUTE_UNUSED)
15745 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15746 *walk_subtrees = 0;
15748 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15749 && ! TREE_ASM_WRITTEN (*tp))
15750 return *tp;
15751 /* ??? The C++ FE emits debug information for using decls, so
15752 putting gcc_unreachable here falls over. See PR31899. For now
15753 be conservative. */
15754 else if (!symtab->global_info_ready
15755 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15756 return *tp;
15757 else if (TREE_CODE (*tp) == VAR_DECL)
15759 varpool_node *node = varpool_node::get (*tp);
15760 if (!node || !node->definition)
15761 return *tp;
15763 else if (TREE_CODE (*tp) == FUNCTION_DECL
15764 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15766 /* The call graph machinery must have finished analyzing,
15767 optimizing and gimplifying the CU by now.
15768 So if *TP has no call graph node associated
15769 to it, it means *TP will not be emitted. */
15770 if (!cgraph_node::get (*tp))
15771 return *tp;
15773 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15774 return *tp;
15776 return NULL_TREE;
15779 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15780 for use in a later add_const_value_attribute call. */
15782 static rtx
15783 rtl_for_decl_init (tree init, tree type)
15785 rtx rtl = NULL_RTX;
15787 STRIP_NOPS (init);
15789 /* If a variable is initialized with a string constant without embedded
15790 zeros, build CONST_STRING. */
15791 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15793 tree enttype = TREE_TYPE (type);
15794 tree domain = TYPE_DOMAIN (type);
15795 machine_mode mode = TYPE_MODE (enttype);
15797 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15798 && domain
15799 && integer_zerop (TYPE_MIN_VALUE (domain))
15800 && compare_tree_int (TYPE_MAX_VALUE (domain),
15801 TREE_STRING_LENGTH (init) - 1) == 0
15802 && ((size_t) TREE_STRING_LENGTH (init)
15803 == strlen (TREE_STRING_POINTER (init)) + 1))
15805 rtl = gen_rtx_CONST_STRING (VOIDmode,
15806 ggc_strdup (TREE_STRING_POINTER (init)));
15807 rtl = gen_rtx_MEM (BLKmode, rtl);
15808 MEM_READONLY_P (rtl) = 1;
15811 /* Other aggregates, and complex values, could be represented using
15812 CONCAT: FIXME! */
15813 else if (AGGREGATE_TYPE_P (type)
15814 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15815 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15816 || TREE_CODE (type) == COMPLEX_TYPE)
15818 /* Vectors only work if their mode is supported by the target.
15819 FIXME: generic vectors ought to work too. */
15820 else if (TREE_CODE (type) == VECTOR_TYPE
15821 && !VECTOR_MODE_P (TYPE_MODE (type)))
15823 /* If the initializer is something that we know will expand into an
15824 immediate RTL constant, expand it now. We must be careful not to
15825 reference variables which won't be output. */
15826 else if (initializer_constant_valid_p (init, type)
15827 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15829 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15830 possible. */
15831 if (TREE_CODE (type) == VECTOR_TYPE)
15832 switch (TREE_CODE (init))
15834 case VECTOR_CST:
15835 break;
15836 case CONSTRUCTOR:
15837 if (TREE_CONSTANT (init))
15839 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15840 bool constant_p = true;
15841 tree value;
15842 unsigned HOST_WIDE_INT ix;
15844 /* Even when ctor is constant, it might contain non-*_CST
15845 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15846 belong into VECTOR_CST nodes. */
15847 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15848 if (!CONSTANT_CLASS_P (value))
15850 constant_p = false;
15851 break;
15854 if (constant_p)
15856 init = build_vector_from_ctor (type, elts);
15857 break;
15860 /* FALLTHRU */
15862 default:
15863 return NULL;
15866 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15868 /* If expand_expr returns a MEM, it wasn't immediate. */
15869 gcc_assert (!rtl || !MEM_P (rtl));
15872 return rtl;
15875 /* Generate RTL for the variable DECL to represent its location. */
15877 static rtx
15878 rtl_for_decl_location (tree decl)
15880 rtx rtl;
15882 /* Here we have to decide where we are going to say the parameter "lives"
15883 (as far as the debugger is concerned). We only have a couple of
15884 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15886 DECL_RTL normally indicates where the parameter lives during most of the
15887 activation of the function. If optimization is enabled however, this
15888 could be either NULL or else a pseudo-reg. Both of those cases indicate
15889 that the parameter doesn't really live anywhere (as far as the code
15890 generation parts of GCC are concerned) during most of the function's
15891 activation. That will happen (for example) if the parameter is never
15892 referenced within the function.
15894 We could just generate a location descriptor here for all non-NULL
15895 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15896 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15897 where DECL_RTL is NULL or is a pseudo-reg.
15899 Note however that we can only get away with using DECL_INCOMING_RTL as
15900 a backup substitute for DECL_RTL in certain limited cases. In cases
15901 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15902 we can be sure that the parameter was passed using the same type as it is
15903 declared to have within the function, and that its DECL_INCOMING_RTL
15904 points us to a place where a value of that type is passed.
15906 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15907 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15908 because in these cases DECL_INCOMING_RTL points us to a value of some
15909 type which is *different* from the type of the parameter itself. Thus,
15910 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15911 such cases, the debugger would end up (for example) trying to fetch a
15912 `float' from a place which actually contains the first part of a
15913 `double'. That would lead to really incorrect and confusing
15914 output at debug-time.
15916 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15917 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15918 are a couple of exceptions however. On little-endian machines we can
15919 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15920 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15921 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15922 when (on a little-endian machine) a non-prototyped function has a
15923 parameter declared to be of type `short' or `char'. In such cases,
15924 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15925 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15926 passed `int' value. If the debugger then uses that address to fetch
15927 a `short' or a `char' (on a little-endian machine) the result will be
15928 the correct data, so we allow for such exceptional cases below.
15930 Note that our goal here is to describe the place where the given formal
15931 parameter lives during most of the function's activation (i.e. between the
15932 end of the prologue and the start of the epilogue). We'll do that as best
15933 as we can. Note however that if the given formal parameter is modified
15934 sometime during the execution of the function, then a stack backtrace (at
15935 debug-time) will show the function as having been called with the *new*
15936 value rather than the value which was originally passed in. This happens
15937 rarely enough that it is not a major problem, but it *is* a problem, and
15938 I'd like to fix it.
15940 A future version of dwarf2out.c may generate two additional attributes for
15941 any given DW_TAG_formal_parameter DIE which will describe the "passed
15942 type" and the "passed location" for the given formal parameter in addition
15943 to the attributes we now generate to indicate the "declared type" and the
15944 "active location" for each parameter. This additional set of attributes
15945 could be used by debuggers for stack backtraces. Separately, note that
15946 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15947 This happens (for example) for inlined-instances of inline function formal
15948 parameters which are never referenced. This really shouldn't be
15949 happening. All PARM_DECL nodes should get valid non-NULL
15950 DECL_INCOMING_RTL values. FIXME. */
15952 /* Use DECL_RTL as the "location" unless we find something better. */
15953 rtl = DECL_RTL_IF_SET (decl);
15955 /* When generating abstract instances, ignore everything except
15956 constants, symbols living in memory, and symbols living in
15957 fixed registers. */
15958 if (! reload_completed)
15960 if (rtl
15961 && (CONSTANT_P (rtl)
15962 || (MEM_P (rtl)
15963 && CONSTANT_P (XEXP (rtl, 0)))
15964 || (REG_P (rtl)
15965 && TREE_CODE (decl) == VAR_DECL
15966 && TREE_STATIC (decl))))
15968 rtl = targetm.delegitimize_address (rtl);
15969 return rtl;
15971 rtl = NULL_RTX;
15973 else if (TREE_CODE (decl) == PARM_DECL)
15975 if (rtl == NULL_RTX
15976 || is_pseudo_reg (rtl)
15977 || (MEM_P (rtl)
15978 && is_pseudo_reg (XEXP (rtl, 0))
15979 && DECL_INCOMING_RTL (decl)
15980 && MEM_P (DECL_INCOMING_RTL (decl))
15981 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15983 tree declared_type = TREE_TYPE (decl);
15984 tree passed_type = DECL_ARG_TYPE (decl);
15985 machine_mode dmode = TYPE_MODE (declared_type);
15986 machine_mode pmode = TYPE_MODE (passed_type);
15988 /* This decl represents a formal parameter which was optimized out.
15989 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15990 all cases where (rtl == NULL_RTX) just below. */
15991 if (dmode == pmode)
15992 rtl = DECL_INCOMING_RTL (decl);
15993 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15994 && SCALAR_INT_MODE_P (dmode)
15995 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15996 && DECL_INCOMING_RTL (decl))
15998 rtx inc = DECL_INCOMING_RTL (decl);
15999 if (REG_P (inc))
16000 rtl = inc;
16001 else if (MEM_P (inc))
16003 if (BYTES_BIG_ENDIAN)
16004 rtl = adjust_address_nv (inc, dmode,
16005 GET_MODE_SIZE (pmode)
16006 - GET_MODE_SIZE (dmode));
16007 else
16008 rtl = inc;
16013 /* If the parm was passed in registers, but lives on the stack, then
16014 make a big endian correction if the mode of the type of the
16015 parameter is not the same as the mode of the rtl. */
16016 /* ??? This is the same series of checks that are made in dbxout.c before
16017 we reach the big endian correction code there. It isn't clear if all
16018 of these checks are necessary here, but keeping them all is the safe
16019 thing to do. */
16020 else if (MEM_P (rtl)
16021 && XEXP (rtl, 0) != const0_rtx
16022 && ! CONSTANT_P (XEXP (rtl, 0))
16023 /* Not passed in memory. */
16024 && !MEM_P (DECL_INCOMING_RTL (decl))
16025 /* Not passed by invisible reference. */
16026 && (!REG_P (XEXP (rtl, 0))
16027 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16028 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16029 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16030 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16031 #endif
16033 /* Big endian correction check. */
16034 && BYTES_BIG_ENDIAN
16035 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16036 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16037 < UNITS_PER_WORD))
16039 machine_mode addr_mode = get_address_mode (rtl);
16040 int offset = (UNITS_PER_WORD
16041 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16043 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16044 plus_constant (addr_mode, XEXP (rtl, 0), offset));
16047 else if (TREE_CODE (decl) == VAR_DECL
16048 && rtl
16049 && MEM_P (rtl)
16050 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16051 && BYTES_BIG_ENDIAN)
16053 machine_mode addr_mode = get_address_mode (rtl);
16054 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16055 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16057 /* If a variable is declared "register" yet is smaller than
16058 a register, then if we store the variable to memory, it
16059 looks like we're storing a register-sized value, when in
16060 fact we are not. We need to adjust the offset of the
16061 storage location to reflect the actual value's bytes,
16062 else gdb will not be able to display it. */
16063 if (rsize > dsize)
16064 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16065 plus_constant (addr_mode, XEXP (rtl, 0),
16066 rsize - dsize));
16069 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16070 and will have been substituted directly into all expressions that use it.
16071 C does not have such a concept, but C++ and other languages do. */
16072 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16073 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16075 if (rtl)
16076 rtl = targetm.delegitimize_address (rtl);
16078 /* If we don't look past the constant pool, we risk emitting a
16079 reference to a constant pool entry that isn't referenced from
16080 code, and thus is not emitted. */
16081 if (rtl)
16082 rtl = avoid_constant_pool_reference (rtl);
16084 /* Try harder to get a rtl. If this symbol ends up not being emitted
16085 in the current CU, resolve_addr will remove the expression referencing
16086 it. */
16087 if (rtl == NULL_RTX
16088 && TREE_CODE (decl) == VAR_DECL
16089 && !DECL_EXTERNAL (decl)
16090 && TREE_STATIC (decl)
16091 && DECL_NAME (decl)
16092 && !DECL_HARD_REGISTER (decl)
16093 && DECL_MODE (decl) != VOIDmode)
16095 rtl = make_decl_rtl_for_debug (decl);
16096 if (!MEM_P (rtl)
16097 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16098 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16099 rtl = NULL_RTX;
16102 return rtl;
16105 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16106 returned. If so, the decl for the COMMON block is returned, and the
16107 value is the offset into the common block for the symbol. */
16109 static tree
16110 fortran_common (tree decl, HOST_WIDE_INT *value)
16112 tree val_expr, cvar;
16113 machine_mode mode;
16114 HOST_WIDE_INT bitsize, bitpos;
16115 tree offset;
16116 int unsignedp, reversep, volatilep = 0;
16118 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16119 it does not have a value (the offset into the common area), or if it
16120 is thread local (as opposed to global) then it isn't common, and shouldn't
16121 be handled as such. */
16122 if (TREE_CODE (decl) != VAR_DECL
16123 || !TREE_STATIC (decl)
16124 || !DECL_HAS_VALUE_EXPR_P (decl)
16125 || !is_fortran ())
16126 return NULL_TREE;
16128 val_expr = DECL_VALUE_EXPR (decl);
16129 if (TREE_CODE (val_expr) != COMPONENT_REF)
16130 return NULL_TREE;
16132 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
16133 &unsignedp, &reversep, &volatilep, true);
16135 if (cvar == NULL_TREE
16136 || TREE_CODE (cvar) != VAR_DECL
16137 || DECL_ARTIFICIAL (cvar)
16138 || !TREE_PUBLIC (cvar))
16139 return NULL_TREE;
16141 *value = 0;
16142 if (offset != NULL)
16144 if (!tree_fits_shwi_p (offset))
16145 return NULL_TREE;
16146 *value = tree_to_shwi (offset);
16148 if (bitpos != 0)
16149 *value += bitpos / BITS_PER_UNIT;
16151 return cvar;
16154 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16155 data attribute for a variable or a parameter. We generate the
16156 DW_AT_const_value attribute only in those cases where the given variable
16157 or parameter does not have a true "location" either in memory or in a
16158 register. This can happen (for example) when a constant is passed as an
16159 actual argument in a call to an inline function. (It's possible that
16160 these things can crop up in other ways also.) Note that one type of
16161 constant value which can be passed into an inlined function is a constant
16162 pointer. This can happen for example if an actual argument in an inlined
16163 function call evaluates to a compile-time constant address.
16165 CACHE_P is true if it is worth caching the location list for DECL,
16166 so that future calls can reuse it rather than regenerate it from scratch.
16167 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16168 since we will need to refer to them each time the function is inlined. */
16170 static bool
16171 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
16173 rtx rtl;
16174 dw_loc_list_ref list;
16175 var_loc_list *loc_list;
16176 cached_dw_loc_list *cache;
16178 if (early_dwarf)
16179 return false;
16181 if (TREE_CODE (decl) == ERROR_MARK)
16182 return false;
16184 if (get_AT (die, DW_AT_location)
16185 || get_AT (die, DW_AT_const_value))
16186 return true;
16188 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16189 || TREE_CODE (decl) == RESULT_DECL);
16191 /* Try to get some constant RTL for this decl, and use that as the value of
16192 the location. */
16194 rtl = rtl_for_decl_location (decl);
16195 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16196 && add_const_value_attribute (die, rtl))
16197 return true;
16199 /* See if we have single element location list that is equivalent to
16200 a constant value. That way we are better to use add_const_value_attribute
16201 rather than expanding constant value equivalent. */
16202 loc_list = lookup_decl_loc (decl);
16203 if (loc_list
16204 && loc_list->first
16205 && loc_list->first->next == NULL
16206 && NOTE_P (loc_list->first->loc)
16207 && NOTE_VAR_LOCATION (loc_list->first->loc)
16208 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16210 struct var_loc_node *node;
16212 node = loc_list->first;
16213 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16214 if (GET_CODE (rtl) == EXPR_LIST)
16215 rtl = XEXP (rtl, 0);
16216 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16217 && add_const_value_attribute (die, rtl))
16218 return true;
16220 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16221 list several times. See if we've already cached the contents. */
16222 list = NULL;
16223 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16224 cache_p = false;
16225 if (cache_p)
16227 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16228 if (cache)
16229 list = cache->loc_list;
16231 if (list == NULL)
16233 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16234 NULL);
16235 /* It is usually worth caching this result if the decl is from
16236 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16237 if (cache_p && list && list->dw_loc_next)
16239 cached_dw_loc_list **slot
16240 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16241 DECL_UID (decl),
16242 INSERT);
16243 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16244 cache->decl_id = DECL_UID (decl);
16245 cache->loc_list = list;
16246 *slot = cache;
16249 if (list)
16251 add_AT_location_description (die, DW_AT_location, list);
16252 return true;
16254 /* None of that worked, so it must not really have a location;
16255 try adding a constant value attribute from the DECL_INITIAL. */
16256 return tree_add_const_value_attribute_for_decl (die, decl);
16259 /* Helper function for tree_add_const_value_attribute. Natively encode
16260 initializer INIT into an array. Return true if successful. */
16262 static bool
16263 native_encode_initializer (tree init, unsigned char *array, int size)
16265 tree type;
16267 if (init == NULL_TREE)
16268 return false;
16270 STRIP_NOPS (init);
16271 switch (TREE_CODE (init))
16273 case STRING_CST:
16274 type = TREE_TYPE (init);
16275 if (TREE_CODE (type) == ARRAY_TYPE)
16277 tree enttype = TREE_TYPE (type);
16278 machine_mode mode = TYPE_MODE (enttype);
16280 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16281 return false;
16282 if (int_size_in_bytes (type) != size)
16283 return false;
16284 if (size > TREE_STRING_LENGTH (init))
16286 memcpy (array, TREE_STRING_POINTER (init),
16287 TREE_STRING_LENGTH (init));
16288 memset (array + TREE_STRING_LENGTH (init),
16289 '\0', size - TREE_STRING_LENGTH (init));
16291 else
16292 memcpy (array, TREE_STRING_POINTER (init), size);
16293 return true;
16295 return false;
16296 case CONSTRUCTOR:
16297 type = TREE_TYPE (init);
16298 if (int_size_in_bytes (type) != size)
16299 return false;
16300 if (TREE_CODE (type) == ARRAY_TYPE)
16302 HOST_WIDE_INT min_index;
16303 unsigned HOST_WIDE_INT cnt;
16304 int curpos = 0, fieldsize;
16305 constructor_elt *ce;
16307 if (TYPE_DOMAIN (type) == NULL_TREE
16308 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16309 return false;
16311 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16312 if (fieldsize <= 0)
16313 return false;
16315 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16316 memset (array, '\0', size);
16317 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16319 tree val = ce->value;
16320 tree index = ce->index;
16321 int pos = curpos;
16322 if (index && TREE_CODE (index) == RANGE_EXPR)
16323 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16324 * fieldsize;
16325 else if (index)
16326 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16328 if (val)
16330 STRIP_NOPS (val);
16331 if (!native_encode_initializer (val, array + pos, fieldsize))
16332 return false;
16334 curpos = pos + fieldsize;
16335 if (index && TREE_CODE (index) == RANGE_EXPR)
16337 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16338 - tree_to_shwi (TREE_OPERAND (index, 0));
16339 while (count-- > 0)
16341 if (val)
16342 memcpy (array + curpos, array + pos, fieldsize);
16343 curpos += fieldsize;
16346 gcc_assert (curpos <= size);
16348 return true;
16350 else if (TREE_CODE (type) == RECORD_TYPE
16351 || TREE_CODE (type) == UNION_TYPE)
16353 tree field = NULL_TREE;
16354 unsigned HOST_WIDE_INT cnt;
16355 constructor_elt *ce;
16357 if (int_size_in_bytes (type) != size)
16358 return false;
16360 if (TREE_CODE (type) == RECORD_TYPE)
16361 field = TYPE_FIELDS (type);
16363 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16365 tree val = ce->value;
16366 int pos, fieldsize;
16368 if (ce->index != 0)
16369 field = ce->index;
16371 if (val)
16372 STRIP_NOPS (val);
16374 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16375 return false;
16377 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16378 && TYPE_DOMAIN (TREE_TYPE (field))
16379 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16380 return false;
16381 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16382 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16383 return false;
16384 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16385 pos = int_byte_position (field);
16386 gcc_assert (pos + fieldsize <= size);
16387 if (val
16388 && !native_encode_initializer (val, array + pos, fieldsize))
16389 return false;
16391 return true;
16393 return false;
16394 case VIEW_CONVERT_EXPR:
16395 case NON_LVALUE_EXPR:
16396 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16397 default:
16398 return native_encode_expr (init, array, size) == size;
16402 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16403 attribute is the const value T. */
16405 static bool
16406 tree_add_const_value_attribute (dw_die_ref die, tree t)
16408 tree init;
16409 tree type = TREE_TYPE (t);
16410 rtx rtl;
16412 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16413 return false;
16415 init = t;
16416 gcc_assert (!DECL_P (init));
16418 rtl = rtl_for_decl_init (init, type);
16419 if (rtl)
16420 return add_const_value_attribute (die, rtl);
16421 /* If the host and target are sane, try harder. */
16422 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16423 && initializer_constant_valid_p (init, type))
16425 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16426 if (size > 0 && (int) size == size)
16428 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16430 if (native_encode_initializer (init, array, size))
16432 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16433 return true;
16435 ggc_free (array);
16438 return false;
16441 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16442 attribute is the const value of T, where T is an integral constant
16443 variable with static storage duration
16444 (so it can't be a PARM_DECL or a RESULT_DECL). */
16446 static bool
16447 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16450 if (!decl
16451 || (TREE_CODE (decl) != VAR_DECL
16452 && TREE_CODE (decl) != CONST_DECL)
16453 || (TREE_CODE (decl) == VAR_DECL
16454 && !TREE_STATIC (decl)))
16455 return false;
16457 if (TREE_READONLY (decl)
16458 && ! TREE_THIS_VOLATILE (decl)
16459 && DECL_INITIAL (decl))
16460 /* OK */;
16461 else
16462 return false;
16464 /* Don't add DW_AT_const_value if abstract origin already has one. */
16465 if (get_AT (var_die, DW_AT_const_value))
16466 return false;
16468 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16471 /* Convert the CFI instructions for the current function into a
16472 location list. This is used for DW_AT_frame_base when we targeting
16473 a dwarf2 consumer that does not support the dwarf3
16474 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16475 expressions. */
16477 static dw_loc_list_ref
16478 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16480 int ix;
16481 dw_fde_ref fde;
16482 dw_loc_list_ref list, *list_tail;
16483 dw_cfi_ref cfi;
16484 dw_cfa_location last_cfa, next_cfa;
16485 const char *start_label, *last_label, *section;
16486 dw_cfa_location remember;
16488 fde = cfun->fde;
16489 gcc_assert (fde != NULL);
16491 section = secname_for_decl (current_function_decl);
16492 list_tail = &list;
16493 list = NULL;
16495 memset (&next_cfa, 0, sizeof (next_cfa));
16496 next_cfa.reg = INVALID_REGNUM;
16497 remember = next_cfa;
16499 start_label = fde->dw_fde_begin;
16501 /* ??? Bald assumption that the CIE opcode list does not contain
16502 advance opcodes. */
16503 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16504 lookup_cfa_1 (cfi, &next_cfa, &remember);
16506 last_cfa = next_cfa;
16507 last_label = start_label;
16509 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16511 /* If the first partition contained no CFI adjustments, the
16512 CIE opcodes apply to the whole first partition. */
16513 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16514 fde->dw_fde_begin, fde->dw_fde_end, section);
16515 list_tail =&(*list_tail)->dw_loc_next;
16516 start_label = last_label = fde->dw_fde_second_begin;
16519 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16521 switch (cfi->dw_cfi_opc)
16523 case DW_CFA_set_loc:
16524 case DW_CFA_advance_loc1:
16525 case DW_CFA_advance_loc2:
16526 case DW_CFA_advance_loc4:
16527 if (!cfa_equal_p (&last_cfa, &next_cfa))
16529 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16530 start_label, last_label, section);
16532 list_tail = &(*list_tail)->dw_loc_next;
16533 last_cfa = next_cfa;
16534 start_label = last_label;
16536 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16537 break;
16539 case DW_CFA_advance_loc:
16540 /* The encoding is complex enough that we should never emit this. */
16541 gcc_unreachable ();
16543 default:
16544 lookup_cfa_1 (cfi, &next_cfa, &remember);
16545 break;
16547 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16549 if (!cfa_equal_p (&last_cfa, &next_cfa))
16551 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16552 start_label, last_label, section);
16554 list_tail = &(*list_tail)->dw_loc_next;
16555 last_cfa = next_cfa;
16556 start_label = last_label;
16558 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16559 start_label, fde->dw_fde_end, section);
16560 list_tail = &(*list_tail)->dw_loc_next;
16561 start_label = last_label = fde->dw_fde_second_begin;
16565 if (!cfa_equal_p (&last_cfa, &next_cfa))
16567 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16568 start_label, last_label, section);
16569 list_tail = &(*list_tail)->dw_loc_next;
16570 start_label = last_label;
16573 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16574 start_label,
16575 fde->dw_fde_second_begin
16576 ? fde->dw_fde_second_end : fde->dw_fde_end,
16577 section);
16579 if (list && list->dw_loc_next)
16580 gen_llsym (list);
16582 return list;
16585 /* Compute a displacement from the "steady-state frame pointer" to the
16586 frame base (often the same as the CFA), and store it in
16587 frame_pointer_fb_offset. OFFSET is added to the displacement
16588 before the latter is negated. */
16590 static void
16591 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16593 rtx reg, elim;
16595 #ifdef FRAME_POINTER_CFA_OFFSET
16596 reg = frame_pointer_rtx;
16597 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16598 #else
16599 reg = arg_pointer_rtx;
16600 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16601 #endif
16603 elim = (ira_use_lra_p
16604 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16605 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16606 if (GET_CODE (elim) == PLUS)
16608 offset += INTVAL (XEXP (elim, 1));
16609 elim = XEXP (elim, 0);
16612 frame_pointer_fb_offset = -offset;
16614 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16615 in which to eliminate. This is because it's stack pointer isn't
16616 directly accessible as a register within the ISA. To work around
16617 this, assume that while we cannot provide a proper value for
16618 frame_pointer_fb_offset, we won't need one either. */
16619 frame_pointer_fb_offset_valid
16620 = ((SUPPORTS_STACK_ALIGNMENT
16621 && (elim == hard_frame_pointer_rtx
16622 || elim == stack_pointer_rtx))
16623 || elim == (frame_pointer_needed
16624 ? hard_frame_pointer_rtx
16625 : stack_pointer_rtx));
16628 /* Generate a DW_AT_name attribute given some string value to be included as
16629 the value of the attribute. */
16631 static void
16632 add_name_attribute (dw_die_ref die, const char *name_string)
16634 if (name_string != NULL && *name_string != 0)
16636 if (demangle_name_func)
16637 name_string = (*demangle_name_func) (name_string);
16639 add_AT_string (die, DW_AT_name, name_string);
16643 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16644 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16645 of TYPE accordingly.
16647 ??? This is a temporary measure until after we're able to generate
16648 regular DWARF for the complex Ada type system. */
16650 static void
16651 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16652 dw_die_ref context_die)
16654 tree dtype;
16655 dw_die_ref dtype_die;
16657 if (!lang_hooks.types.descriptive_type)
16658 return;
16660 dtype = lang_hooks.types.descriptive_type (type);
16661 if (!dtype)
16662 return;
16664 dtype_die = lookup_type_die (dtype);
16665 if (!dtype_die)
16667 gen_type_die (dtype, context_die);
16668 dtype_die = lookup_type_die (dtype);
16669 gcc_assert (dtype_die);
16672 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16675 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16677 static const char *
16678 comp_dir_string (void)
16680 const char *wd;
16681 char *wd1;
16682 static const char *cached_wd = NULL;
16684 if (cached_wd != NULL)
16685 return cached_wd;
16687 wd = get_src_pwd ();
16688 if (wd == NULL)
16689 return NULL;
16691 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16693 int wdlen;
16695 wdlen = strlen (wd);
16696 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16697 strcpy (wd1, wd);
16698 wd1 [wdlen] = DIR_SEPARATOR;
16699 wd1 [wdlen + 1] = 0;
16700 wd = wd1;
16703 cached_wd = remap_debug_filename (wd);
16704 return cached_wd;
16707 /* Generate a DW_AT_comp_dir attribute for DIE. */
16709 static void
16710 add_comp_dir_attribute (dw_die_ref die)
16712 const char * wd = comp_dir_string ();
16713 if (wd != NULL)
16714 add_AT_string (die, DW_AT_comp_dir, wd);
16717 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16718 pointer computation, ...), output a representation for that bound according
16719 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16720 loc_list_from_tree for the meaning of CONTEXT. */
16722 static void
16723 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16724 int forms, const struct loc_descr_context *context)
16726 dw_die_ref ctx, decl_die;
16727 dw_loc_list_ref list;
16729 bool strip_conversions = true;
16731 while (strip_conversions)
16732 switch (TREE_CODE (value))
16734 case ERROR_MARK:
16735 case SAVE_EXPR:
16736 return;
16738 CASE_CONVERT:
16739 case VIEW_CONVERT_EXPR:
16740 value = TREE_OPERAND (value, 0);
16741 break;
16743 default:
16744 strip_conversions = false;
16745 break;
16748 /* If possible and permitted, output the attribute as a constant. */
16749 if ((forms & dw_scalar_form_constant) != 0
16750 && TREE_CODE (value) == INTEGER_CST)
16752 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16754 /* If HOST_WIDE_INT is big enough then represent the bound as
16755 a constant value. We need to choose a form based on
16756 whether the type is signed or unsigned. We cannot just
16757 call add_AT_unsigned if the value itself is positive
16758 (add_AT_unsigned might add the unsigned value encoded as
16759 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16760 bounds type and then sign extend any unsigned values found
16761 for signed types. This is needed only for
16762 DW_AT_{lower,upper}_bound, since for most other attributes,
16763 consumers will treat DW_FORM_data[1248] as unsigned values,
16764 regardless of the underlying type. */
16765 if (prec <= HOST_BITS_PER_WIDE_INT
16766 || tree_fits_uhwi_p (value))
16768 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16769 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16770 else
16771 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16773 else
16774 /* Otherwise represent the bound as an unsigned value with
16775 the precision of its type. The precision and signedness
16776 of the type will be necessary to re-interpret it
16777 unambiguously. */
16778 add_AT_wide (die, attr, value);
16779 return;
16782 /* Otherwise, if it's possible and permitted too, output a reference to
16783 another DIE. */
16784 if ((forms & dw_scalar_form_reference) != 0)
16786 tree decl = NULL_TREE;
16788 /* Some type attributes reference an outer type. For instance, the upper
16789 bound of an array may reference an embedding record (this happens in
16790 Ada). */
16791 if (TREE_CODE (value) == COMPONENT_REF
16792 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16793 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16794 decl = TREE_OPERAND (value, 1);
16796 else if (TREE_CODE (value) == VAR_DECL
16797 || TREE_CODE (value) == PARM_DECL
16798 || TREE_CODE (value) == RESULT_DECL)
16799 decl = value;
16801 if (decl != NULL_TREE)
16803 dw_die_ref decl_die = lookup_decl_die (decl);
16805 /* ??? Can this happen, or should the variable have been bound
16806 first? Probably it can, since I imagine that we try to create
16807 the types of parameters in the order in which they exist in
16808 the list, and won't have created a forward reference to a
16809 later parameter. */
16810 if (decl_die != NULL)
16812 add_AT_die_ref (die, attr, decl_die);
16813 return;
16818 /* Last chance: try to create a stack operation procedure to evaluate the
16819 value. Do nothing if even that is not possible or permitted. */
16820 if ((forms & dw_scalar_form_exprloc) == 0)
16821 return;
16823 list = loc_list_from_tree (value, 2, context);
16824 if (list == NULL || single_element_loc_list_p (list))
16826 /* If this attribute is not a reference nor constant, it is
16827 a DWARF expression rather than location description. For that
16828 loc_list_from_tree (value, 0, &context) is needed. */
16829 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16830 if (list2 && single_element_loc_list_p (list2))
16832 add_AT_loc (die, attr, list2->expr);
16833 return;
16837 /* If that failed to give a single element location list, fall back to
16838 outputting this as a reference... still if permitted. */
16839 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16840 return;
16842 if (current_function_decl == 0)
16843 ctx = comp_unit_die ();
16844 else
16845 ctx = lookup_decl_die (current_function_decl);
16847 decl_die = new_die (DW_TAG_variable, ctx, value);
16848 add_AT_flag (decl_die, DW_AT_artificial, 1);
16849 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16850 add_AT_location_description (decl_die, DW_AT_location, list);
16851 add_AT_die_ref (die, attr, decl_die);
16854 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16855 default. */
16857 static int
16858 lower_bound_default (void)
16860 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16862 case DW_LANG_C:
16863 case DW_LANG_C89:
16864 case DW_LANG_C99:
16865 case DW_LANG_C11:
16866 case DW_LANG_C_plus_plus:
16867 case DW_LANG_C_plus_plus_11:
16868 case DW_LANG_C_plus_plus_14:
16869 case DW_LANG_ObjC:
16870 case DW_LANG_ObjC_plus_plus:
16871 case DW_LANG_Java:
16872 return 0;
16873 case DW_LANG_Fortran77:
16874 case DW_LANG_Fortran90:
16875 case DW_LANG_Fortran95:
16876 case DW_LANG_Fortran03:
16877 case DW_LANG_Fortran08:
16878 return 1;
16879 case DW_LANG_UPC:
16880 case DW_LANG_D:
16881 case DW_LANG_Python:
16882 return dwarf_version >= 4 ? 0 : -1;
16883 case DW_LANG_Ada95:
16884 case DW_LANG_Ada83:
16885 case DW_LANG_Cobol74:
16886 case DW_LANG_Cobol85:
16887 case DW_LANG_Pascal83:
16888 case DW_LANG_Modula2:
16889 case DW_LANG_PLI:
16890 return dwarf_version >= 4 ? 1 : -1;
16891 default:
16892 return -1;
16896 /* Given a tree node describing an array bound (either lower or upper) output
16897 a representation for that bound. */
16899 static void
16900 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16901 tree bound, const struct loc_descr_context *context)
16903 int dflt;
16905 while (1)
16906 switch (TREE_CODE (bound))
16908 /* Strip all conversions. */
16909 CASE_CONVERT:
16910 case VIEW_CONVERT_EXPR:
16911 bound = TREE_OPERAND (bound, 0);
16912 break;
16914 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16915 are even omitted when they are the default. */
16916 case INTEGER_CST:
16917 /* If the value for this bound is the default one, we can even omit the
16918 attribute. */
16919 if (bound_attr == DW_AT_lower_bound
16920 && tree_fits_shwi_p (bound)
16921 && (dflt = lower_bound_default ()) != -1
16922 && tree_to_shwi (bound) == dflt)
16923 return;
16925 /* FALLTHRU */
16927 default:
16928 add_scalar_info (subrange_die, bound_attr, bound,
16929 dw_scalar_form_constant
16930 | dw_scalar_form_exprloc
16931 | dw_scalar_form_reference,
16932 context);
16933 return;
16937 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16938 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16939 Note that the block of subscript information for an array type also
16940 includes information about the element type of the given array type.
16942 This function reuses previously set type and bound information if
16943 available. */
16945 static void
16946 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16948 unsigned dimension_number;
16949 tree lower, upper;
16950 dw_die_ref child = type_die->die_child;
16952 for (dimension_number = 0;
16953 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16954 type = TREE_TYPE (type), dimension_number++)
16956 tree domain = TYPE_DOMAIN (type);
16958 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16959 break;
16961 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16962 and (in GNU C only) variable bounds. Handle all three forms
16963 here. */
16965 /* Find and reuse a previously generated DW_TAG_subrange_type if
16966 available.
16968 For multi-dimensional arrays, as we iterate through the
16969 various dimensions in the enclosing for loop above, we also
16970 iterate through the DIE children and pick at each
16971 DW_TAG_subrange_type previously generated (if available).
16972 Each child DW_TAG_subrange_type DIE describes the range of
16973 the current dimension. At this point we should have as many
16974 DW_TAG_subrange_type's as we have dimensions in the
16975 array. */
16976 dw_die_ref subrange_die = NULL;
16977 if (child)
16978 while (1)
16980 child = child->die_sib;
16981 if (child->die_tag == DW_TAG_subrange_type)
16982 subrange_die = child;
16983 if (child == type_die->die_child)
16985 /* If we wrapped around, stop looking next time. */
16986 child = NULL;
16987 break;
16989 if (child->die_tag == DW_TAG_subrange_type)
16990 break;
16992 if (!subrange_die)
16993 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16995 if (domain)
16997 /* We have an array type with specified bounds. */
16998 lower = TYPE_MIN_VALUE (domain);
16999 upper = TYPE_MAX_VALUE (domain);
17001 /* Define the index type. */
17002 if (TREE_TYPE (domain)
17003 && !get_AT (subrange_die, DW_AT_type))
17005 /* ??? This is probably an Ada unnamed subrange type. Ignore the
17006 TREE_TYPE field. We can't emit debug info for this
17007 because it is an unnamed integral type. */
17008 if (TREE_CODE (domain) == INTEGER_TYPE
17009 && TYPE_NAME (domain) == NULL_TREE
17010 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17011 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17013 else
17014 add_type_attribute (subrange_die, TREE_TYPE (domain),
17015 TYPE_UNQUALIFIED, type_die);
17018 /* ??? If upper is NULL, the array has unspecified length,
17019 but it does have a lower bound. This happens with Fortran
17020 dimension arr(N:*)
17021 Since the debugger is definitely going to need to know N
17022 to produce useful results, go ahead and output the lower
17023 bound solo, and hope the debugger can cope. */
17025 if (!get_AT (subrange_die, DW_AT_lower_bound))
17026 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
17027 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
17028 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
17031 /* Otherwise we have an array type with an unspecified length. The
17032 DWARF-2 spec does not say how to handle this; let's just leave out the
17033 bounds. */
17037 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
17039 static void
17040 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17042 dw_die_ref decl_die;
17043 HOST_WIDE_INT size;
17045 switch (TREE_CODE (tree_node))
17047 case ERROR_MARK:
17048 size = 0;
17049 break;
17050 case ENUMERAL_TYPE:
17051 case RECORD_TYPE:
17052 case UNION_TYPE:
17053 case QUAL_UNION_TYPE:
17054 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17055 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17057 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17058 return;
17060 size = int_size_in_bytes (tree_node);
17061 break;
17062 case FIELD_DECL:
17063 /* For a data member of a struct or union, the DW_AT_byte_size is
17064 generally given as the number of bytes normally allocated for an
17065 object of the *declared* type of the member itself. This is true
17066 even for bit-fields. */
17067 size = int_size_in_bytes (field_type (tree_node));
17068 break;
17069 default:
17070 gcc_unreachable ();
17073 /* Note that `size' might be -1 when we get to this point. If it is, that
17074 indicates that the byte size of the entity in question is variable. We
17075 have no good way of expressing this fact in Dwarf at the present time,
17076 when location description was not used by the caller code instead. */
17077 if (size >= 0)
17078 add_AT_unsigned (die, DW_AT_byte_size, size);
17081 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17082 which specifies the distance in bits from the highest order bit of the
17083 "containing object" for the bit-field to the highest order bit of the
17084 bit-field itself.
17086 For any given bit-field, the "containing object" is a hypothetical object
17087 (of some integral or enum type) within which the given bit-field lives. The
17088 type of this hypothetical "containing object" is always the same as the
17089 declared type of the individual bit-field itself. The determination of the
17090 exact location of the "containing object" for a bit-field is rather
17091 complicated. It's handled by the `field_byte_offset' function (above).
17093 Note that it is the size (in bytes) of the hypothetical "containing object"
17094 which will be given in the DW_AT_byte_size attribute for this bit-field.
17095 (See `byte_size_attribute' above). */
17097 static inline void
17098 add_bit_offset_attribute (dw_die_ref die, tree decl)
17100 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17101 tree type = DECL_BIT_FIELD_TYPE (decl);
17102 HOST_WIDE_INT bitpos_int;
17103 HOST_WIDE_INT highest_order_object_bit_offset;
17104 HOST_WIDE_INT highest_order_field_bit_offset;
17105 HOST_WIDE_INT bit_offset;
17107 /* Must be a field and a bit field. */
17108 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17110 /* We can't yet handle bit-fields whose offsets are variable, so if we
17111 encounter such things, just return without generating any attribute
17112 whatsoever. Likewise for variable or too large size. */
17113 if (! tree_fits_shwi_p (bit_position (decl))
17114 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17115 return;
17117 bitpos_int = int_bit_position (decl);
17119 /* Note that the bit offset is always the distance (in bits) from the
17120 highest-order bit of the "containing object" to the highest-order bit of
17121 the bit-field itself. Since the "high-order end" of any object or field
17122 is different on big-endian and little-endian machines, the computation
17123 below must take account of these differences. */
17124 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17125 highest_order_field_bit_offset = bitpos_int;
17127 if (! BYTES_BIG_ENDIAN)
17129 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17130 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17133 bit_offset
17134 = (! BYTES_BIG_ENDIAN
17135 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17136 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17138 if (bit_offset < 0)
17139 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17140 else
17141 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17144 /* For a FIELD_DECL node which represents a bit field, output an attribute
17145 which specifies the length in bits of the given field. */
17147 static inline void
17148 add_bit_size_attribute (dw_die_ref die, tree decl)
17150 /* Must be a field and a bit field. */
17151 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17152 && DECL_BIT_FIELD_TYPE (decl));
17154 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17155 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17158 /* If the compiled language is ANSI C, then add a 'prototyped'
17159 attribute, if arg types are given for the parameters of a function. */
17161 static inline void
17162 add_prototyped_attribute (dw_die_ref die, tree func_type)
17164 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17166 case DW_LANG_C:
17167 case DW_LANG_C89:
17168 case DW_LANG_C99:
17169 case DW_LANG_C11:
17170 case DW_LANG_ObjC:
17171 if (prototype_p (func_type))
17172 add_AT_flag (die, DW_AT_prototyped, 1);
17173 break;
17174 default:
17175 break;
17179 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17180 by looking in either the type declaration or object declaration
17181 equate table. */
17183 static inline dw_die_ref
17184 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17186 dw_die_ref origin_die = NULL;
17188 if (TREE_CODE (origin) != FUNCTION_DECL)
17190 /* We may have gotten separated from the block for the inlined
17191 function, if we're in an exception handler or some such; make
17192 sure that the abstract function has been written out.
17194 Doing this for nested functions is wrong, however; functions are
17195 distinct units, and our context might not even be inline. */
17196 tree fn = origin;
17198 if (TYPE_P (fn))
17199 fn = TYPE_STUB_DECL (fn);
17201 fn = decl_function_context (fn);
17202 if (fn)
17203 dwarf2out_abstract_function (fn);
17206 if (DECL_P (origin))
17207 origin_die = lookup_decl_die (origin);
17208 else if (TYPE_P (origin))
17209 origin_die = lookup_type_die (origin);
17211 /* XXX: Functions that are never lowered don't always have correct block
17212 trees (in the case of java, they simply have no block tree, in some other
17213 languages). For these functions, there is nothing we can really do to
17214 output correct debug info for inlined functions in all cases. Rather
17215 than die, we'll just produce deficient debug info now, in that we will
17216 have variables without a proper abstract origin. In the future, when all
17217 functions are lowered, we should re-add a gcc_assert (origin_die)
17218 here. */
17220 if (origin_die)
17221 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17222 return origin_die;
17225 /* We do not currently support the pure_virtual attribute. */
17227 static inline void
17228 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17230 if (DECL_VINDEX (func_decl))
17232 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17234 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17235 add_AT_loc (die, DW_AT_vtable_elem_location,
17236 new_loc_descr (DW_OP_constu,
17237 tree_to_shwi (DECL_VINDEX (func_decl)),
17238 0));
17240 /* GNU extension: Record what type this method came from originally. */
17241 if (debug_info_level > DINFO_LEVEL_TERSE
17242 && DECL_CONTEXT (func_decl))
17243 add_AT_die_ref (die, DW_AT_containing_type,
17244 lookup_type_die (DECL_CONTEXT (func_decl)));
17248 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17249 given decl. This used to be a vendor extension until after DWARF 4
17250 standardized it. */
17252 static void
17253 add_linkage_attr (dw_die_ref die, tree decl)
17255 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17257 /* Mimic what assemble_name_raw does with a leading '*'. */
17258 if (name[0] == '*')
17259 name = &name[1];
17261 if (dwarf_version >= 4)
17262 add_AT_string (die, DW_AT_linkage_name, name);
17263 else
17264 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17267 /* Add source coordinate attributes for the given decl. */
17269 static void
17270 add_src_coords_attributes (dw_die_ref die, tree decl)
17272 expanded_location s;
17274 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17275 return;
17276 s = expand_location (DECL_SOURCE_LOCATION (decl));
17277 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17278 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17281 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17283 static void
17284 add_linkage_name (dw_die_ref die, tree decl)
17286 if (debug_info_level > DINFO_LEVEL_NONE
17287 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17288 && TREE_PUBLIC (decl)
17289 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17290 && die->die_tag != DW_TAG_member)
17292 /* Defer until we have an assembler name set. */
17293 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17295 limbo_die_node *asm_name;
17297 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17298 asm_name->die = die;
17299 asm_name->created_for = decl;
17300 asm_name->next = deferred_asm_name;
17301 deferred_asm_name = asm_name;
17303 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17304 add_linkage_attr (die, decl);
17308 /* Add a DW_AT_name attribute and source coordinate attribute for the
17309 given decl, but only if it actually has a name. */
17311 static void
17312 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17314 tree decl_name;
17316 decl_name = DECL_NAME (decl);
17317 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17319 const char *name = dwarf2_name (decl, 0);
17320 if (name)
17321 add_name_attribute (die, name);
17322 if (! DECL_ARTIFICIAL (decl))
17323 add_src_coords_attributes (die, decl);
17325 add_linkage_name (die, decl);
17328 #ifdef VMS_DEBUGGING_INFO
17329 /* Get the function's name, as described by its RTL. This may be different
17330 from the DECL_NAME name used in the source file. */
17331 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17333 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17334 XEXP (DECL_RTL (decl), 0), false);
17335 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17337 #endif /* VMS_DEBUGGING_INFO */
17340 #ifdef VMS_DEBUGGING_INFO
17341 /* Output the debug main pointer die for VMS */
17343 void
17344 dwarf2out_vms_debug_main_pointer (void)
17346 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17347 dw_die_ref die;
17349 /* Allocate the VMS debug main subprogram die. */
17350 die = ggc_cleared_alloc<die_node> ();
17351 die->die_tag = DW_TAG_subprogram;
17352 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17353 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17354 current_function_funcdef_no);
17355 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17357 /* Make it the first child of comp_unit_die (). */
17358 die->die_parent = comp_unit_die ();
17359 if (comp_unit_die ()->die_child)
17361 die->die_sib = comp_unit_die ()->die_child->die_sib;
17362 comp_unit_die ()->die_child->die_sib = die;
17364 else
17366 die->die_sib = die;
17367 comp_unit_die ()->die_child = die;
17370 #endif /* VMS_DEBUGGING_INFO */
17372 /* Push a new declaration scope. */
17374 static void
17375 push_decl_scope (tree scope)
17377 vec_safe_push (decl_scope_table, scope);
17380 /* Pop a declaration scope. */
17382 static inline void
17383 pop_decl_scope (void)
17385 decl_scope_table->pop ();
17388 /* walk_tree helper function for uses_local_type, below. */
17390 static tree
17391 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17393 if (!TYPE_P (*tp))
17394 *walk_subtrees = 0;
17395 else
17397 tree name = TYPE_NAME (*tp);
17398 if (name && DECL_P (name) && decl_function_context (name))
17399 return *tp;
17401 return NULL_TREE;
17404 /* If TYPE involves a function-local type (including a local typedef to a
17405 non-local type), returns that type; otherwise returns NULL_TREE. */
17407 static tree
17408 uses_local_type (tree type)
17410 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17411 return used;
17414 /* Return the DIE for the scope that immediately contains this type.
17415 Non-named types that do not involve a function-local type get global
17416 scope. Named types nested in namespaces or other types get their
17417 containing scope. All other types (i.e. function-local named types) get
17418 the current active scope. */
17420 static dw_die_ref
17421 scope_die_for (tree t, dw_die_ref context_die)
17423 dw_die_ref scope_die = NULL;
17424 tree containing_scope;
17426 /* Non-types always go in the current scope. */
17427 gcc_assert (TYPE_P (t));
17429 /* Use the scope of the typedef, rather than the scope of the type
17430 it refers to. */
17431 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17432 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17433 else
17434 containing_scope = TYPE_CONTEXT (t);
17436 /* Use the containing namespace if there is one. */
17437 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17439 if (context_die == lookup_decl_die (containing_scope))
17440 /* OK */;
17441 else if (debug_info_level > DINFO_LEVEL_TERSE)
17442 context_die = get_context_die (containing_scope);
17443 else
17444 containing_scope = NULL_TREE;
17447 /* Ignore function type "scopes" from the C frontend. They mean that
17448 a tagged type is local to a parmlist of a function declarator, but
17449 that isn't useful to DWARF. */
17450 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17451 containing_scope = NULL_TREE;
17453 if (SCOPE_FILE_SCOPE_P (containing_scope))
17455 /* If T uses a local type keep it local as well, to avoid references
17456 to function-local DIEs from outside the function. */
17457 if (current_function_decl && uses_local_type (t))
17458 scope_die = context_die;
17459 else
17460 scope_die = comp_unit_die ();
17462 else if (TYPE_P (containing_scope))
17464 /* For types, we can just look up the appropriate DIE. */
17465 if (debug_info_level > DINFO_LEVEL_TERSE)
17466 scope_die = get_context_die (containing_scope);
17467 else
17469 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17470 if (scope_die == NULL)
17471 scope_die = comp_unit_die ();
17474 else
17475 scope_die = context_die;
17477 return scope_die;
17480 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17482 static inline int
17483 local_scope_p (dw_die_ref context_die)
17485 for (; context_die; context_die = context_die->die_parent)
17486 if (context_die->die_tag == DW_TAG_inlined_subroutine
17487 || context_die->die_tag == DW_TAG_subprogram)
17488 return 1;
17490 return 0;
17493 /* Returns nonzero if CONTEXT_DIE is a class. */
17495 static inline int
17496 class_scope_p (dw_die_ref context_die)
17498 return (context_die
17499 && (context_die->die_tag == DW_TAG_structure_type
17500 || context_die->die_tag == DW_TAG_class_type
17501 || context_die->die_tag == DW_TAG_interface_type
17502 || context_die->die_tag == DW_TAG_union_type));
17505 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17506 whether or not to treat a DIE in this context as a declaration. */
17508 static inline int
17509 class_or_namespace_scope_p (dw_die_ref context_die)
17511 return (class_scope_p (context_die)
17512 || (context_die && context_die->die_tag == DW_TAG_namespace));
17515 /* Many forms of DIEs require a "type description" attribute. This
17516 routine locates the proper "type descriptor" die for the type given
17517 by 'type' plus any additional qualifiers given by 'cv_quals', and
17518 adds a DW_AT_type attribute below the given die. */
17520 static void
17521 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17522 dw_die_ref context_die)
17524 enum tree_code code = TREE_CODE (type);
17525 dw_die_ref type_die = NULL;
17527 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17528 or fixed-point type, use the inner type. This is because we have no
17529 support for unnamed types in base_type_die. This can happen if this is
17530 an Ada subrange type. Correct solution is emit a subrange type die. */
17531 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17532 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17533 type = TREE_TYPE (type), code = TREE_CODE (type);
17535 if (code == ERROR_MARK
17536 /* Handle a special case. For functions whose return type is void, we
17537 generate *no* type attribute. (Note that no object may have type
17538 `void', so this only applies to function return types). */
17539 || code == VOID_TYPE)
17540 return;
17542 type_die = modified_type_die (type,
17543 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17544 context_die);
17546 if (type_die != NULL)
17547 add_AT_die_ref (object_die, DW_AT_type, type_die);
17550 /* Given an object die, add the calling convention attribute for the
17551 function call type. */
17552 static void
17553 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17555 enum dwarf_calling_convention value = DW_CC_normal;
17557 value = ((enum dwarf_calling_convention)
17558 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17560 if (is_fortran ()
17561 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17563 /* DWARF 2 doesn't provide a way to identify a program's source-level
17564 entry point. DW_AT_calling_convention attributes are only meant
17565 to describe functions' calling conventions. However, lacking a
17566 better way to signal the Fortran main program, we used this for
17567 a long time, following existing custom. Now, DWARF 4 has
17568 DW_AT_main_subprogram, which we add below, but some tools still
17569 rely on the old way, which we thus keep. */
17570 value = DW_CC_program;
17572 if (dwarf_version >= 4 || !dwarf_strict)
17573 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17576 /* Only add the attribute if the backend requests it, and
17577 is not DW_CC_normal. */
17578 if (value && (value != DW_CC_normal))
17579 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17582 /* Given a tree pointer to a struct, class, union, or enum type node, return
17583 a pointer to the (string) tag name for the given type, or zero if the type
17584 was declared without a tag. */
17586 static const char *
17587 type_tag (const_tree type)
17589 const char *name = 0;
17591 if (TYPE_NAME (type) != 0)
17593 tree t = 0;
17595 /* Find the IDENTIFIER_NODE for the type name. */
17596 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17597 && !TYPE_NAMELESS (type))
17598 t = TYPE_NAME (type);
17600 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17601 a TYPE_DECL node, regardless of whether or not a `typedef' was
17602 involved. */
17603 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17604 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17606 /* We want to be extra verbose. Don't call dwarf_name if
17607 DECL_NAME isn't set. The default hook for decl_printable_name
17608 doesn't like that, and in this context it's correct to return
17609 0, instead of "<anonymous>" or the like. */
17610 if (DECL_NAME (TYPE_NAME (type))
17611 && !DECL_NAMELESS (TYPE_NAME (type)))
17612 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17615 /* Now get the name as a string, or invent one. */
17616 if (!name && t != 0)
17617 name = IDENTIFIER_POINTER (t);
17620 return (name == 0 || *name == '\0') ? 0 : name;
17623 /* Return the type associated with a data member, make a special check
17624 for bit field types. */
17626 static inline tree
17627 member_declared_type (const_tree member)
17629 return (DECL_BIT_FIELD_TYPE (member)
17630 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17633 /* Get the decl's label, as described by its RTL. This may be different
17634 from the DECL_NAME name used in the source file. */
17636 #if 0
17637 static const char *
17638 decl_start_label (tree decl)
17640 rtx x;
17641 const char *fnname;
17643 x = DECL_RTL (decl);
17644 gcc_assert (MEM_P (x));
17646 x = XEXP (x, 0);
17647 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17649 fnname = XSTR (x, 0);
17650 return fnname;
17652 #endif
17654 /* For variable-length arrays that have been previously generated, but
17655 may be incomplete due to missing subscript info, fill the subscript
17656 info. Return TRUE if this is one of those cases. */
17657 static bool
17658 fill_variable_array_bounds (tree type)
17660 if (TREE_ASM_WRITTEN (type)
17661 && TREE_CODE (type) == ARRAY_TYPE
17662 && variably_modified_type_p (type, NULL))
17664 dw_die_ref array_die = lookup_type_die (type);
17665 if (!array_die)
17666 return false;
17667 add_subscript_info (array_die, type, !is_ada ());
17668 return true;
17670 return false;
17673 /* These routines generate the internal representation of the DIE's for
17674 the compilation unit. Debugging information is collected by walking
17675 the declaration trees passed in from dwarf2out_decl(). */
17677 static void
17678 gen_array_type_die (tree type, dw_die_ref context_die)
17680 dw_die_ref array_die;
17682 /* GNU compilers represent multidimensional array types as sequences of one
17683 dimensional array types whose element types are themselves array types.
17684 We sometimes squish that down to a single array_type DIE with multiple
17685 subscripts in the Dwarf debugging info. The draft Dwarf specification
17686 say that we are allowed to do this kind of compression in C, because
17687 there is no difference between an array of arrays and a multidimensional
17688 array. We don't do this for Ada to remain as close as possible to the
17689 actual representation, which is especially important against the language
17690 flexibilty wrt arrays of variable size. */
17692 bool collapse_nested_arrays = !is_ada ();
17694 if (fill_variable_array_bounds (type))
17695 return;
17697 dw_die_ref scope_die = scope_die_for (type, context_die);
17698 tree element_type;
17700 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17701 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17702 if (TYPE_STRING_FLAG (type)
17703 && TREE_CODE (type) == ARRAY_TYPE
17704 && is_fortran ()
17705 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17707 HOST_WIDE_INT size;
17709 array_die = new_die (DW_TAG_string_type, scope_die, type);
17710 add_name_attribute (array_die, type_tag (type));
17711 equate_type_number_to_die (type, array_die);
17712 size = int_size_in_bytes (type);
17713 if (size >= 0)
17714 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17715 else if (TYPE_DOMAIN (type) != NULL_TREE
17716 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17717 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17719 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17720 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17722 size = int_size_in_bytes (TREE_TYPE (szdecl));
17723 if (loc && size > 0)
17725 add_AT_location_description (array_die, DW_AT_string_length, loc);
17726 if (size != DWARF2_ADDR_SIZE)
17727 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17730 return;
17733 array_die = new_die (DW_TAG_array_type, scope_die, type);
17734 add_name_attribute (array_die, type_tag (type));
17735 equate_type_number_to_die (type, array_die);
17737 if (TREE_CODE (type) == VECTOR_TYPE)
17738 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17740 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17741 if (is_fortran ()
17742 && TREE_CODE (type) == ARRAY_TYPE
17743 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17744 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17745 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17747 #if 0
17748 /* We default the array ordering. SDB will probably do
17749 the right things even if DW_AT_ordering is not present. It's not even
17750 an issue until we start to get into multidimensional arrays anyway. If
17751 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17752 then we'll have to put the DW_AT_ordering attribute back in. (But if
17753 and when we find out that we need to put these in, we will only do so
17754 for multidimensional arrays. */
17755 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17756 #endif
17758 if (TREE_CODE (type) == VECTOR_TYPE)
17760 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17761 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17762 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17763 add_bound_info (subrange_die, DW_AT_upper_bound,
17764 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17766 else
17767 add_subscript_info (array_die, type, collapse_nested_arrays);
17769 /* Add representation of the type of the elements of this array type and
17770 emit the corresponding DIE if we haven't done it already. */
17771 element_type = TREE_TYPE (type);
17772 if (collapse_nested_arrays)
17773 while (TREE_CODE (element_type) == ARRAY_TYPE)
17775 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17776 break;
17777 element_type = TREE_TYPE (element_type);
17780 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17782 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17783 if (TYPE_ARTIFICIAL (type))
17784 add_AT_flag (array_die, DW_AT_artificial, 1);
17786 if (get_AT (array_die, DW_AT_name))
17787 add_pubtype (type, array_die);
17790 /* This routine generates DIE for array with hidden descriptor, details
17791 are filled into *info by a langhook. */
17793 static void
17794 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17795 dw_die_ref context_die)
17797 const dw_die_ref scope_die = scope_die_for (type, context_die);
17798 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17799 const struct loc_descr_context context = { type, info->base_decl };
17800 int dim;
17802 add_name_attribute (array_die, type_tag (type));
17803 equate_type_number_to_die (type, array_die);
17805 if (info->ndimensions > 1)
17806 switch (info->ordering)
17808 case array_descr_ordering_row_major:
17809 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17810 break;
17811 case array_descr_ordering_column_major:
17812 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17813 break;
17814 default:
17815 break;
17818 if (dwarf_version >= 3 || !dwarf_strict)
17820 if (info->data_location)
17821 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17822 dw_scalar_form_exprloc, &context);
17823 if (info->associated)
17824 add_scalar_info (array_die, DW_AT_associated, info->associated,
17825 dw_scalar_form_constant
17826 | dw_scalar_form_exprloc
17827 | dw_scalar_form_reference, &context);
17828 if (info->allocated)
17829 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17830 dw_scalar_form_constant
17831 | dw_scalar_form_exprloc
17832 | dw_scalar_form_reference, &context);
17835 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17837 for (dim = 0; dim < info->ndimensions; dim++)
17839 dw_die_ref subrange_die
17840 = new_die (DW_TAG_subrange_type, array_die, NULL);
17842 if (info->dimen[dim].bounds_type)
17843 add_type_attribute (subrange_die,
17844 info->dimen[dim].bounds_type, 0,
17845 context_die);
17846 if (info->dimen[dim].lower_bound)
17847 add_bound_info (subrange_die, DW_AT_lower_bound,
17848 info->dimen[dim].lower_bound, &context);
17849 if (info->dimen[dim].upper_bound)
17850 add_bound_info (subrange_die, DW_AT_upper_bound,
17851 info->dimen[dim].upper_bound, &context);
17852 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17853 add_scalar_info (subrange_die, DW_AT_byte_stride,
17854 info->dimen[dim].stride,
17855 dw_scalar_form_constant
17856 | dw_scalar_form_exprloc
17857 | dw_scalar_form_reference,
17858 &context);
17861 gen_type_die (info->element_type, context_die);
17862 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17863 context_die);
17865 if (get_AT (array_die, DW_AT_name))
17866 add_pubtype (type, array_die);
17869 #if 0
17870 static void
17871 gen_entry_point_die (tree decl, dw_die_ref context_die)
17873 tree origin = decl_ultimate_origin (decl);
17874 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17876 if (origin != NULL)
17877 add_abstract_origin_attribute (decl_die, origin);
17878 else
17880 add_name_and_src_coords_attributes (decl_die, decl);
17881 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17882 TYPE_UNQUALIFIED, context_die);
17885 if (DECL_ABSTRACT_P (decl))
17886 equate_decl_number_to_die (decl, decl_die);
17887 else
17888 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17890 #endif
17892 /* Walk through the list of incomplete types again, trying once more to
17893 emit full debugging info for them. */
17895 static void
17896 retry_incomplete_types (void)
17898 int i;
17900 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17901 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17902 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17905 /* Determine what tag to use for a record type. */
17907 static enum dwarf_tag
17908 record_type_tag (tree type)
17910 if (! lang_hooks.types.classify_record)
17911 return DW_TAG_structure_type;
17913 switch (lang_hooks.types.classify_record (type))
17915 case RECORD_IS_STRUCT:
17916 return DW_TAG_structure_type;
17918 case RECORD_IS_CLASS:
17919 return DW_TAG_class_type;
17921 case RECORD_IS_INTERFACE:
17922 if (dwarf_version >= 3 || !dwarf_strict)
17923 return DW_TAG_interface_type;
17924 return DW_TAG_structure_type;
17926 default:
17927 gcc_unreachable ();
17931 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17932 include all of the information about the enumeration values also. Each
17933 enumerated type name/value is listed as a child of the enumerated type
17934 DIE. */
17936 static dw_die_ref
17937 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17939 dw_die_ref type_die = lookup_type_die (type);
17941 if (type_die == NULL)
17943 type_die = new_die (DW_TAG_enumeration_type,
17944 scope_die_for (type, context_die), type);
17945 equate_type_number_to_die (type, type_die);
17946 add_name_attribute (type_die, type_tag (type));
17947 if (dwarf_version >= 4 || !dwarf_strict)
17949 if (ENUM_IS_SCOPED (type))
17950 add_AT_flag (type_die, DW_AT_enum_class, 1);
17951 if (ENUM_IS_OPAQUE (type))
17952 add_AT_flag (type_die, DW_AT_declaration, 1);
17955 else if (! TYPE_SIZE (type))
17956 return type_die;
17957 else
17958 remove_AT (type_die, DW_AT_declaration);
17960 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17961 given enum type is incomplete, do not generate the DW_AT_byte_size
17962 attribute or the DW_AT_element_list attribute. */
17963 if (TYPE_SIZE (type))
17965 tree link;
17967 TREE_ASM_WRITTEN (type) = 1;
17968 add_byte_size_attribute (type_die, type);
17969 if (dwarf_version >= 3 || !dwarf_strict)
17971 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17972 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17973 context_die);
17975 if (TYPE_STUB_DECL (type) != NULL_TREE)
17977 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17978 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17981 /* If the first reference to this type was as the return type of an
17982 inline function, then it may not have a parent. Fix this now. */
17983 if (type_die->die_parent == NULL)
17984 add_child_die (scope_die_for (type, context_die), type_die);
17986 for (link = TYPE_VALUES (type);
17987 link != NULL; link = TREE_CHAIN (link))
17989 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17990 tree value = TREE_VALUE (link);
17992 add_name_attribute (enum_die,
17993 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17995 if (TREE_CODE (value) == CONST_DECL)
17996 value = DECL_INITIAL (value);
17998 if (simple_type_size_in_bits (TREE_TYPE (value))
17999 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
18001 /* For constant forms created by add_AT_unsigned DWARF
18002 consumers (GDB, elfutils, etc.) always zero extend
18003 the value. Only when the actual value is negative
18004 do we need to use add_AT_int to generate a constant
18005 form that can represent negative values. */
18006 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
18007 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
18008 add_AT_unsigned (enum_die, DW_AT_const_value,
18009 (unsigned HOST_WIDE_INT) val);
18010 else
18011 add_AT_int (enum_die, DW_AT_const_value, val);
18013 else
18014 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
18015 that here. TODO: This should be re-worked to use correct
18016 signed/unsigned double tags for all cases. */
18017 add_AT_wide (enum_die, DW_AT_const_value, value);
18020 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18021 if (TYPE_ARTIFICIAL (type))
18022 add_AT_flag (type_die, DW_AT_artificial, 1);
18024 else
18025 add_AT_flag (type_die, DW_AT_declaration, 1);
18027 add_pubtype (type, type_die);
18029 return type_die;
18032 /* Generate a DIE to represent either a real live formal parameter decl or to
18033 represent just the type of some formal parameter position in some function
18034 type.
18036 Note that this routine is a bit unusual because its argument may be a
18037 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18038 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18039 node. If it's the former then this function is being called to output a
18040 DIE to represent a formal parameter object (or some inlining thereof). If
18041 it's the latter, then this function is only being called to output a
18042 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18043 argument type of some subprogram type.
18044 If EMIT_NAME_P is true, name and source coordinate attributes
18045 are emitted. */
18047 static dw_die_ref
18048 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18049 dw_die_ref context_die)
18051 tree node_or_origin = node ? node : origin;
18052 tree ultimate_origin;
18053 dw_die_ref parm_die = NULL;
18055 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18057 parm_die = lookup_decl_die (node);
18059 /* If the contexts differ, we may not be talking about the same
18060 thing. */
18061 if (parm_die && parm_die->die_parent != context_die)
18063 if (!DECL_ABSTRACT_P (node))
18065 /* This can happen when creating an inlined instance, in
18066 which case we need to create a new DIE that will get
18067 annotated with DW_AT_abstract_origin. */
18068 parm_die = NULL;
18070 else
18072 /* FIXME: Reuse DIE even with a differing context.
18074 This can happen when calling
18075 dwarf2out_abstract_function to build debug info for
18076 the abstract instance of a function for which we have
18077 already generated a DIE in
18078 dwarf2out_early_global_decl.
18080 Once we remove dwarf2out_abstract_function, we should
18081 have a call to gcc_unreachable here. */
18085 if (parm_die && parm_die->die_parent == NULL)
18087 /* Check that parm_die already has the right attributes that
18088 we would have added below. If any attributes are
18089 missing, fall through to add them. */
18090 if (! DECL_ABSTRACT_P (node_or_origin)
18091 && !get_AT (parm_die, DW_AT_location)
18092 && !get_AT (parm_die, DW_AT_const_value))
18093 /* We are missing location info, and are about to add it. */
18095 else
18097 add_child_die (context_die, parm_die);
18098 return parm_die;
18103 /* If we have a previously generated DIE, use it, unless this is an
18104 concrete instance (origin != NULL), in which case we need a new
18105 DIE with a corresponding DW_AT_abstract_origin. */
18106 bool reusing_die;
18107 if (parm_die && origin == NULL)
18108 reusing_die = true;
18109 else
18111 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18112 reusing_die = false;
18115 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18117 case tcc_declaration:
18118 ultimate_origin = decl_ultimate_origin (node_or_origin);
18119 if (node || ultimate_origin)
18120 origin = ultimate_origin;
18122 if (reusing_die)
18123 goto add_location;
18125 if (origin != NULL)
18126 add_abstract_origin_attribute (parm_die, origin);
18127 else if (emit_name_p)
18128 add_name_and_src_coords_attributes (parm_die, node);
18129 if (origin == NULL
18130 || (! DECL_ABSTRACT_P (node_or_origin)
18131 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18132 decl_function_context
18133 (node_or_origin))))
18135 tree type = TREE_TYPE (node_or_origin);
18136 if (decl_by_reference_p (node_or_origin))
18137 add_type_attribute (parm_die, TREE_TYPE (type),
18138 TYPE_UNQUALIFIED, context_die);
18139 else
18140 add_type_attribute (parm_die, type,
18141 decl_quals (node_or_origin),
18142 context_die);
18144 if (origin == NULL && DECL_ARTIFICIAL (node))
18145 add_AT_flag (parm_die, DW_AT_artificial, 1);
18146 add_location:
18147 if (node && node != origin)
18148 equate_decl_number_to_die (node, parm_die);
18149 if (! DECL_ABSTRACT_P (node_or_origin))
18150 add_location_or_const_value_attribute (parm_die, node_or_origin,
18151 node == NULL);
18153 break;
18155 case tcc_type:
18156 /* We were called with some kind of a ..._TYPE node. */
18157 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18158 context_die);
18159 break;
18161 default:
18162 gcc_unreachable ();
18165 return parm_die;
18168 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18169 children DW_TAG_formal_parameter DIEs representing the arguments of the
18170 parameter pack.
18172 PARM_PACK must be a function parameter pack.
18173 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18174 must point to the subsequent arguments of the function PACK_ARG belongs to.
18175 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18176 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18177 following the last one for which a DIE was generated. */
18179 static dw_die_ref
18180 gen_formal_parameter_pack_die (tree parm_pack,
18181 tree pack_arg,
18182 dw_die_ref subr_die,
18183 tree *next_arg)
18185 tree arg;
18186 dw_die_ref parm_pack_die;
18188 gcc_assert (parm_pack
18189 && lang_hooks.function_parameter_pack_p (parm_pack)
18190 && subr_die);
18192 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18193 add_src_coords_attributes (parm_pack_die, parm_pack);
18195 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18197 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18198 parm_pack))
18199 break;
18200 gen_formal_parameter_die (arg, NULL,
18201 false /* Don't emit name attribute. */,
18202 parm_pack_die);
18204 if (next_arg)
18205 *next_arg = arg;
18206 return parm_pack_die;
18209 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18210 at the end of an (ANSI prototyped) formal parameters list. */
18212 static void
18213 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18215 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18218 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18219 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18220 parameters as specified in some function type specification (except for
18221 those which appear as part of a function *definition*). */
18223 static void
18224 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18226 tree link;
18227 tree formal_type = NULL;
18228 tree first_parm_type;
18229 tree arg;
18231 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18233 arg = DECL_ARGUMENTS (function_or_method_type);
18234 function_or_method_type = TREE_TYPE (function_or_method_type);
18236 else
18237 arg = NULL_TREE;
18239 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18241 /* Make our first pass over the list of formal parameter types and output a
18242 DW_TAG_formal_parameter DIE for each one. */
18243 for (link = first_parm_type; link; )
18245 dw_die_ref parm_die;
18247 formal_type = TREE_VALUE (link);
18248 if (formal_type == void_type_node)
18249 break;
18251 /* Output a (nameless) DIE to represent the formal parameter itself. */
18252 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18254 parm_die = gen_formal_parameter_die (formal_type, NULL,
18255 true /* Emit name attribute. */,
18256 context_die);
18257 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18258 && link == first_parm_type)
18260 add_AT_flag (parm_die, DW_AT_artificial, 1);
18261 if (dwarf_version >= 3 || !dwarf_strict)
18262 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18264 else if (arg && DECL_ARTIFICIAL (arg))
18265 add_AT_flag (parm_die, DW_AT_artificial, 1);
18268 link = TREE_CHAIN (link);
18269 if (arg)
18270 arg = DECL_CHAIN (arg);
18273 /* If this function type has an ellipsis, add a
18274 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18275 if (formal_type != void_type_node)
18276 gen_unspecified_parameters_die (function_or_method_type, context_die);
18278 /* Make our second (and final) pass over the list of formal parameter types
18279 and output DIEs to represent those types (as necessary). */
18280 for (link = TYPE_ARG_TYPES (function_or_method_type);
18281 link && TREE_VALUE (link);
18282 link = TREE_CHAIN (link))
18283 gen_type_die (TREE_VALUE (link), context_die);
18286 /* We want to generate the DIE for TYPE so that we can generate the
18287 die for MEMBER, which has been defined; we will need to refer back
18288 to the member declaration nested within TYPE. If we're trying to
18289 generate minimal debug info for TYPE, processing TYPE won't do the
18290 trick; we need to attach the member declaration by hand. */
18292 static void
18293 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18295 gen_type_die (type, context_die);
18297 /* If we're trying to avoid duplicate debug info, we may not have
18298 emitted the member decl for this function. Emit it now. */
18299 if (TYPE_STUB_DECL (type)
18300 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18301 && ! lookup_decl_die (member))
18303 dw_die_ref type_die;
18304 gcc_assert (!decl_ultimate_origin (member));
18306 push_decl_scope (type);
18307 type_die = lookup_type_die_strip_naming_typedef (type);
18308 if (TREE_CODE (member) == FUNCTION_DECL)
18309 gen_subprogram_die (member, type_die);
18310 else if (TREE_CODE (member) == FIELD_DECL)
18312 /* Ignore the nameless fields that are used to skip bits but handle
18313 C++ anonymous unions and structs. */
18314 if (DECL_NAME (member) != NULL_TREE
18315 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18316 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18318 gen_type_die (member_declared_type (member), type_die);
18319 gen_field_die (member, type_die);
18322 else
18323 gen_variable_die (member, NULL_TREE, type_die);
18325 pop_decl_scope ();
18329 /* Forward declare these functions, because they are mutually recursive
18330 with their set_block_* pairing functions. */
18331 static void set_decl_origin_self (tree);
18332 static void set_decl_abstract_flags (tree, vec<tree> &);
18334 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18335 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18336 that it points to the node itself, thus indicating that the node is its
18337 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18338 the given node is NULL, recursively descend the decl/block tree which
18339 it is the root of, and for each other ..._DECL or BLOCK node contained
18340 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18341 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18342 values to point to themselves. */
18344 static void
18345 set_block_origin_self (tree stmt)
18347 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18349 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18352 tree local_decl;
18354 for (local_decl = BLOCK_VARS (stmt);
18355 local_decl != NULL_TREE;
18356 local_decl = DECL_CHAIN (local_decl))
18357 /* Do not recurse on nested functions since the inlining status
18358 of parent and child can be different as per the DWARF spec. */
18359 if (TREE_CODE (local_decl) != FUNCTION_DECL
18360 && !DECL_EXTERNAL (local_decl))
18361 set_decl_origin_self (local_decl);
18365 tree subblock;
18367 for (subblock = BLOCK_SUBBLOCKS (stmt);
18368 subblock != NULL_TREE;
18369 subblock = BLOCK_CHAIN (subblock))
18370 set_block_origin_self (subblock); /* Recurse. */
18375 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18376 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18377 node to so that it points to the node itself, thus indicating that the
18378 node represents its own (abstract) origin. Additionally, if the
18379 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18380 the decl/block tree of which the given node is the root of, and for
18381 each other ..._DECL or BLOCK node contained therein whose
18382 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18383 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18384 point to themselves. */
18386 static void
18387 set_decl_origin_self (tree decl)
18389 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18391 DECL_ABSTRACT_ORIGIN (decl) = decl;
18392 if (TREE_CODE (decl) == FUNCTION_DECL)
18394 tree arg;
18396 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18397 DECL_ABSTRACT_ORIGIN (arg) = arg;
18398 if (DECL_INITIAL (decl) != NULL_TREE
18399 && DECL_INITIAL (decl) != error_mark_node)
18400 set_block_origin_self (DECL_INITIAL (decl));
18405 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18406 and if it wasn't 1 before, push it to abstract_vec vector.
18407 For all local decls and all local sub-blocks (recursively) do it
18408 too. */
18410 static void
18411 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18413 tree local_decl;
18414 tree subblock;
18415 unsigned int i;
18417 if (!BLOCK_ABSTRACT (stmt))
18419 abstract_vec.safe_push (stmt);
18420 BLOCK_ABSTRACT (stmt) = 1;
18423 for (local_decl = BLOCK_VARS (stmt);
18424 local_decl != NULL_TREE;
18425 local_decl = DECL_CHAIN (local_decl))
18426 if (! DECL_EXTERNAL (local_decl))
18427 set_decl_abstract_flags (local_decl, abstract_vec);
18429 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18431 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18432 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18433 || TREE_CODE (local_decl) == PARM_DECL)
18434 set_decl_abstract_flags (local_decl, abstract_vec);
18437 for (subblock = BLOCK_SUBBLOCKS (stmt);
18438 subblock != NULL_TREE;
18439 subblock = BLOCK_CHAIN (subblock))
18440 set_block_abstract_flags (subblock, abstract_vec);
18443 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18444 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18445 In the case where the decl is a FUNCTION_DECL also set the abstract
18446 flags for all of the parameters, local vars, local
18447 blocks and sub-blocks (recursively). */
18449 static void
18450 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18452 if (!DECL_ABSTRACT_P (decl))
18454 abstract_vec.safe_push (decl);
18455 DECL_ABSTRACT_P (decl) = 1;
18458 if (TREE_CODE (decl) == FUNCTION_DECL)
18460 tree arg;
18462 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18463 if (!DECL_ABSTRACT_P (arg))
18465 abstract_vec.safe_push (arg);
18466 DECL_ABSTRACT_P (arg) = 1;
18468 if (DECL_INITIAL (decl) != NULL_TREE
18469 && DECL_INITIAL (decl) != error_mark_node)
18470 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18474 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18475 may later generate inlined and/or out-of-line instances of.
18477 FIXME: In the early-dwarf world, this function, and most of the
18478 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18479 the abstract instance. All we would need to do is annotate
18480 the early DIE with the appropriate DW_AT_inline in late
18481 dwarf (perhaps in gen_inlined_subroutine_die).
18483 However, we can't do this yet, because LTO streaming of DIEs
18484 has not been implemented yet. */
18486 static void
18487 dwarf2out_abstract_function (tree decl)
18489 dw_die_ref old_die;
18490 tree save_fn;
18491 tree context;
18492 hash_table<decl_loc_hasher> *old_decl_loc_table;
18493 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18494 int old_call_site_count, old_tail_call_site_count;
18495 struct call_arg_loc_node *old_call_arg_locations;
18497 /* Make sure we have the actual abstract inline, not a clone. */
18498 decl = DECL_ORIGIN (decl);
18500 old_die = lookup_decl_die (decl);
18501 if (old_die && get_AT (old_die, DW_AT_inline))
18502 /* We've already generated the abstract instance. */
18503 return;
18505 /* We can be called while recursively when seeing block defining inlined subroutine
18506 DIE. Be sure to not clobber the outer location table nor use it or we would
18507 get locations in abstract instantces. */
18508 old_decl_loc_table = decl_loc_table;
18509 decl_loc_table = NULL;
18510 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18511 cached_dw_loc_list_table = NULL;
18512 old_call_arg_locations = call_arg_locations;
18513 call_arg_locations = NULL;
18514 old_call_site_count = call_site_count;
18515 call_site_count = -1;
18516 old_tail_call_site_count = tail_call_site_count;
18517 tail_call_site_count = -1;
18519 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18520 we don't get confused by DECL_ABSTRACT_P. */
18521 if (debug_info_level > DINFO_LEVEL_TERSE)
18523 context = decl_class_context (decl);
18524 if (context)
18525 gen_type_die_for_member
18526 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18529 /* Pretend we've just finished compiling this function. */
18530 save_fn = current_function_decl;
18531 current_function_decl = decl;
18533 auto_vec<tree, 64> abstract_vec;
18534 set_decl_abstract_flags (decl, abstract_vec);
18535 dwarf2out_decl (decl);
18536 unsigned int i;
18537 tree t;
18538 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18539 if (TREE_CODE (t) == BLOCK)
18540 BLOCK_ABSTRACT (t) = 0;
18541 else
18542 DECL_ABSTRACT_P (t) = 0;
18544 current_function_decl = save_fn;
18545 decl_loc_table = old_decl_loc_table;
18546 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18547 call_arg_locations = old_call_arg_locations;
18548 call_site_count = old_call_site_count;
18549 tail_call_site_count = old_tail_call_site_count;
18552 /* Helper function of premark_used_types() which gets called through
18553 htab_traverse.
18555 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18556 marked as unused by prune_unused_types. */
18558 bool
18559 premark_used_types_helper (tree const &type, void *)
18561 dw_die_ref die;
18563 die = lookup_type_die (type);
18564 if (die != NULL)
18565 die->die_perennial_p = 1;
18566 return true;
18569 /* Helper function of premark_types_used_by_global_vars which gets called
18570 through htab_traverse.
18572 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18573 marked as unused by prune_unused_types. The DIE of the type is marked
18574 only if the global variable using the type will actually be emitted. */
18577 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18578 void *)
18580 struct types_used_by_vars_entry *entry;
18581 dw_die_ref die;
18583 entry = (struct types_used_by_vars_entry *) *slot;
18584 gcc_assert (entry->type != NULL
18585 && entry->var_decl != NULL);
18586 die = lookup_type_die (entry->type);
18587 if (die)
18589 /* Ask cgraph if the global variable really is to be emitted.
18590 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18591 varpool_node *node = varpool_node::get (entry->var_decl);
18592 if (node && node->definition)
18594 die->die_perennial_p = 1;
18595 /* Keep the parent DIEs as well. */
18596 while ((die = die->die_parent) && die->die_perennial_p == 0)
18597 die->die_perennial_p = 1;
18600 return 1;
18603 /* Mark all members of used_types_hash as perennial. */
18605 static void
18606 premark_used_types (struct function *fun)
18608 if (fun && fun->used_types_hash)
18609 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18612 /* Mark all members of types_used_by_vars_entry as perennial. */
18614 static void
18615 premark_types_used_by_global_vars (void)
18617 if (types_used_by_vars_hash)
18618 types_used_by_vars_hash
18619 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18622 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18623 for CA_LOC call arg loc node. */
18625 static dw_die_ref
18626 gen_call_site_die (tree decl, dw_die_ref subr_die,
18627 struct call_arg_loc_node *ca_loc)
18629 dw_die_ref stmt_die = NULL, die;
18630 tree block = ca_loc->block;
18632 while (block
18633 && block != DECL_INITIAL (decl)
18634 && TREE_CODE (block) == BLOCK)
18636 stmt_die = BLOCK_DIE (block);
18637 if (stmt_die)
18638 break;
18639 block = BLOCK_SUPERCONTEXT (block);
18641 if (stmt_die == NULL)
18642 stmt_die = subr_die;
18643 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18644 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18645 if (ca_loc->tail_call_p)
18646 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18647 if (ca_loc->symbol_ref)
18649 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18650 if (tdie)
18651 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18652 else
18653 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18655 return die;
18658 /* Generate a DIE to represent a declared function (either file-scope or
18659 block-local). */
18661 static void
18662 gen_subprogram_die (tree decl, dw_die_ref context_die)
18664 tree origin = decl_ultimate_origin (decl);
18665 dw_die_ref subr_die;
18666 dw_die_ref old_die = lookup_decl_die (decl);
18668 /* This function gets called multiple times for different stages of
18669 the debug process. For example, for func() in this code:
18671 namespace S
18673 void func() { ... }
18676 ...we get called 4 times. Twice in early debug and twice in
18677 late debug:
18679 Early debug
18680 -----------
18682 1. Once while generating func() within the namespace. This is
18683 the declaration. The declaration bit below is set, as the
18684 context is the namespace.
18686 A new DIE will be generated with DW_AT_declaration set.
18688 2. Once for func() itself. This is the specification. The
18689 declaration bit below is clear as the context is the CU.
18691 We will use the cached DIE from (1) to create a new DIE with
18692 DW_AT_specification pointing to the declaration in (1).
18694 Late debug via rest_of_handle_final()
18695 -------------------------------------
18697 3. Once generating func() within the namespace. This is also the
18698 declaration, as in (1), but this time we will early exit below
18699 as we have a cached DIE and a declaration needs no additional
18700 annotations (no locations), as the source declaration line
18701 info is enough.
18703 4. Once for func() itself. As in (2), this is the specification,
18704 but this time we will re-use the cached DIE, and just annotate
18705 it with the location information that should now be available.
18707 For something without namespaces, but with abstract instances, we
18708 are also called a multiple times:
18710 class Base
18712 public:
18713 Base (); // constructor declaration (1)
18716 Base::Base () { } // constructor specification (2)
18718 Early debug
18719 -----------
18721 1. Once for the Base() constructor by virtue of it being a
18722 member of the Base class. This is done via
18723 rest_of_type_compilation.
18725 This is a declaration, so a new DIE will be created with
18726 DW_AT_declaration.
18728 2. Once for the Base() constructor definition, but this time
18729 while generating the abstract instance of the base
18730 constructor (__base_ctor) which is being generated via early
18731 debug of reachable functions.
18733 Even though we have a cached version of the declaration (1),
18734 we will create a DW_AT_specification of the declaration DIE
18735 in (1).
18737 3. Once for the __base_ctor itself, but this time, we generate
18738 an DW_AT_abstract_origin version of the DW_AT_specification in
18739 (2).
18741 Late debug via rest_of_handle_final
18742 -----------------------------------
18744 4. One final time for the __base_ctor (which will have a cached
18745 DIE with DW_AT_abstract_origin created in (3). This time,
18746 we will just annotate the location information now
18747 available.
18749 int declaration = (current_function_decl != decl
18750 || class_or_namespace_scope_p (context_die));
18752 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18754 /* Now that the C++ front end lazily declares artificial member fns, we
18755 might need to retrofit the declaration into its class. */
18756 if (!declaration && !origin && !old_die
18757 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18758 && !class_or_namespace_scope_p (context_die)
18759 && debug_info_level > DINFO_LEVEL_TERSE)
18760 old_die = force_decl_die (decl);
18762 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18763 if (origin != NULL)
18765 gcc_assert (!declaration || local_scope_p (context_die));
18767 /* Fixup die_parent for the abstract instance of a nested
18768 inline function. */
18769 if (old_die && old_die->die_parent == NULL)
18770 add_child_die (context_die, old_die);
18772 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18774 /* If we have a DW_AT_abstract_origin we have a working
18775 cached version. */
18776 subr_die = old_die;
18778 else
18780 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18781 add_abstract_origin_attribute (subr_die, origin);
18782 /* This is where the actual code for a cloned function is.
18783 Let's emit linkage name attribute for it. This helps
18784 debuggers to e.g, set breakpoints into
18785 constructors/destructors when the user asks "break
18786 K::K". */
18787 add_linkage_name (subr_die, decl);
18790 /* A cached copy, possibly from early dwarf generation. Reuse as
18791 much as possible. */
18792 else if (old_die)
18794 /* A declaration that has been previously dumped needs no
18795 additional information. */
18796 if (declaration)
18797 return;
18799 if (!get_AT_flag (old_die, DW_AT_declaration)
18800 /* We can have a normal definition following an inline one in the
18801 case of redefinition of GNU C extern inlines.
18802 It seems reasonable to use AT_specification in this case. */
18803 && !get_AT (old_die, DW_AT_inline))
18805 /* Detect and ignore this case, where we are trying to output
18806 something we have already output. */
18807 if (get_AT (old_die, DW_AT_low_pc)
18808 || get_AT (old_die, DW_AT_ranges))
18809 return;
18811 /* If we have no location information, this must be a
18812 partially generated DIE from early dwarf generation.
18813 Fall through and generate it. */
18816 /* If the definition comes from the same place as the declaration,
18817 maybe use the old DIE. We always want the DIE for this function
18818 that has the *_pc attributes to be under comp_unit_die so the
18819 debugger can find it. We also need to do this for abstract
18820 instances of inlines, since the spec requires the out-of-line copy
18821 to have the same parent. For local class methods, this doesn't
18822 apply; we just use the old DIE. */
18823 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18824 struct dwarf_file_data * file_index = lookup_filename (s.file);
18825 if ((is_cu_die (old_die->die_parent)
18826 /* This condition fixes the inconsistency/ICE with the
18827 following Fortran test (or some derivative thereof) while
18828 building libgfortran:
18830 module some_m
18831 contains
18832 logical function funky (FLAG)
18833 funky = .true.
18834 end function
18835 end module
18837 || (old_die->die_parent
18838 && old_die->die_parent->die_tag == DW_TAG_module)
18839 || context_die == NULL)
18840 && (DECL_ARTIFICIAL (decl)
18841 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18842 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18843 == (unsigned) s.line))))
18845 subr_die = old_die;
18847 /* Clear out the declaration attribute, but leave the
18848 parameters so they can be augmented with location
18849 information later. Unless this was a declaration, in
18850 which case, wipe out the nameless parameters and recreate
18851 them further down. */
18852 if (remove_AT (subr_die, DW_AT_declaration))
18855 remove_AT (subr_die, DW_AT_object_pointer);
18856 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18859 /* Make a specification pointing to the previously built
18860 declaration. */
18861 else
18863 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18864 add_AT_specification (subr_die, old_die);
18865 add_pubname (decl, subr_die);
18866 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18867 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18868 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18869 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18871 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18872 emit the real type on the definition die. */
18873 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18875 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18876 if (die == auto_die || die == decltype_auto_die)
18877 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18878 TYPE_UNQUALIFIED, context_die);
18882 /* Create a fresh DIE for anything else. */
18883 else
18885 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18887 if (TREE_PUBLIC (decl))
18888 add_AT_flag (subr_die, DW_AT_external, 1);
18890 add_name_and_src_coords_attributes (subr_die, decl);
18891 add_pubname (decl, subr_die);
18892 if (debug_info_level > DINFO_LEVEL_TERSE)
18894 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18895 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18896 TYPE_UNQUALIFIED, context_die);
18899 add_pure_or_virtual_attribute (subr_die, decl);
18900 if (DECL_ARTIFICIAL (decl))
18901 add_AT_flag (subr_die, DW_AT_artificial, 1);
18903 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18904 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18906 add_accessibility_attribute (subr_die, decl);
18909 /* Unless we have an existing non-declaration DIE, equate the new
18910 DIE. */
18911 if (!old_die || is_declaration_die (old_die))
18912 equate_decl_number_to_die (decl, subr_die);
18914 if (declaration)
18916 if (!old_die || !get_AT (old_die, DW_AT_inline))
18918 add_AT_flag (subr_die, DW_AT_declaration, 1);
18920 /* If this is an explicit function declaration then generate
18921 a DW_AT_explicit attribute. */
18922 if (lang_hooks.decls.function_decl_explicit_p (decl)
18923 && (dwarf_version >= 3 || !dwarf_strict))
18924 add_AT_flag (subr_die, DW_AT_explicit, 1);
18926 /* If this is a C++11 deleted special function member then generate
18927 a DW_AT_GNU_deleted attribute. */
18928 if (lang_hooks.decls.function_decl_deleted_p (decl)
18929 && (! dwarf_strict))
18930 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18933 /* Tag abstract instances with DW_AT_inline. */
18934 else if (DECL_ABSTRACT_P (decl))
18936 if (DECL_DECLARED_INLINE_P (decl))
18938 if (cgraph_function_possibly_inlined_p (decl))
18939 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18940 else
18941 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18943 else
18945 if (cgraph_function_possibly_inlined_p (decl))
18946 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18947 else
18948 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18951 if (DECL_DECLARED_INLINE_P (decl)
18952 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18953 add_AT_flag (subr_die, DW_AT_artificial, 1);
18955 /* For non DECL_EXTERNALs, if range information is available, fill
18956 the DIE with it. */
18957 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18959 HOST_WIDE_INT cfa_fb_offset;
18961 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18963 if (!flag_reorder_blocks_and_partition)
18965 dw_fde_ref fde = fun->fde;
18966 if (fde->dw_fde_begin)
18968 /* We have already generated the labels. */
18969 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18970 fde->dw_fde_end, false);
18972 else
18974 /* Create start/end labels and add the range. */
18975 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18976 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18977 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18978 current_function_funcdef_no);
18979 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18980 current_function_funcdef_no);
18981 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18982 false);
18985 #if VMS_DEBUGGING_INFO
18986 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18987 Section 2.3 Prologue and Epilogue Attributes:
18988 When a breakpoint is set on entry to a function, it is generally
18989 desirable for execution to be suspended, not on the very first
18990 instruction of the function, but rather at a point after the
18991 function's frame has been set up, after any language defined local
18992 declaration processing has been completed, and before execution of
18993 the first statement of the function begins. Debuggers generally
18994 cannot properly determine where this point is. Similarly for a
18995 breakpoint set on exit from a function. The prologue and epilogue
18996 attributes allow a compiler to communicate the location(s) to use. */
18999 if (fde->dw_fde_vms_end_prologue)
19000 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19001 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19003 if (fde->dw_fde_vms_begin_epilogue)
19004 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19005 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19007 #endif
19010 else
19012 /* Generate pubnames entries for the split function code ranges. */
19013 dw_fde_ref fde = fun->fde;
19015 if (fde->dw_fde_second_begin)
19017 if (dwarf_version >= 3 || !dwarf_strict)
19019 /* We should use ranges for non-contiguous code section
19020 addresses. Use the actual code range for the initial
19021 section, since the HOT/COLD labels might precede an
19022 alignment offset. */
19023 bool range_list_added = false;
19024 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19025 fde->dw_fde_end, &range_list_added,
19026 false);
19027 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19028 fde->dw_fde_second_end,
19029 &range_list_added, false);
19030 if (range_list_added)
19031 add_ranges (NULL);
19033 else
19035 /* There is no real support in DW2 for this .. so we make
19036 a work-around. First, emit the pub name for the segment
19037 containing the function label. Then make and emit a
19038 simplified subprogram DIE for the second segment with the
19039 name pre-fixed by __hot/cold_sect_of_. We use the same
19040 linkage name for the second die so that gdb will find both
19041 sections when given "b foo". */
19042 const char *name = NULL;
19043 tree decl_name = DECL_NAME (decl);
19044 dw_die_ref seg_die;
19046 /* Do the 'primary' section. */
19047 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19048 fde->dw_fde_end, false);
19050 /* Build a minimal DIE for the secondary section. */
19051 seg_die = new_die (DW_TAG_subprogram,
19052 subr_die->die_parent, decl);
19054 if (TREE_PUBLIC (decl))
19055 add_AT_flag (seg_die, DW_AT_external, 1);
19057 if (decl_name != NULL
19058 && IDENTIFIER_POINTER (decl_name) != NULL)
19060 name = dwarf2_name (decl, 1);
19061 if (! DECL_ARTIFICIAL (decl))
19062 add_src_coords_attributes (seg_die, decl);
19064 add_linkage_name (seg_die, decl);
19066 gcc_assert (name != NULL);
19067 add_pure_or_virtual_attribute (seg_die, decl);
19068 if (DECL_ARTIFICIAL (decl))
19069 add_AT_flag (seg_die, DW_AT_artificial, 1);
19071 name = concat ("__second_sect_of_", name, NULL);
19072 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19073 fde->dw_fde_second_end, false);
19074 add_name_attribute (seg_die, name);
19075 if (want_pubnames ())
19076 add_pubname_string (name, seg_die);
19079 else
19080 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19081 false);
19084 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19086 /* We define the "frame base" as the function's CFA. This is more
19087 convenient for several reasons: (1) It's stable across the prologue
19088 and epilogue, which makes it better than just a frame pointer,
19089 (2) With dwarf3, there exists a one-byte encoding that allows us
19090 to reference the .debug_frame data by proxy, but failing that,
19091 (3) We can at least reuse the code inspection and interpretation
19092 code that determines the CFA position at various points in the
19093 function. */
19094 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19096 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19097 add_AT_loc (subr_die, DW_AT_frame_base, op);
19099 else
19101 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19102 if (list->dw_loc_next)
19103 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19104 else
19105 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19108 /* Compute a displacement from the "steady-state frame pointer" to
19109 the CFA. The former is what all stack slots and argument slots
19110 will reference in the rtl; the latter is what we've told the
19111 debugger about. We'll need to adjust all frame_base references
19112 by this displacement. */
19113 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19115 if (fun->static_chain_decl)
19116 add_AT_location_description
19117 (subr_die, DW_AT_static_link,
19118 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19121 /* Generate child dies for template paramaters. */
19122 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19123 gen_generic_params_dies (decl);
19125 /* Now output descriptions of the arguments for this function. This gets
19126 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19127 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19128 `...' at the end of the formal parameter list. In order to find out if
19129 there was a trailing ellipsis or not, we must instead look at the type
19130 associated with the FUNCTION_DECL. This will be a node of type
19131 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19132 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19133 an ellipsis at the end. */
19135 /* In the case where we are describing a mere function declaration, all we
19136 need to do here (and all we *can* do here) is to describe the *types* of
19137 its formal parameters. */
19138 if (debug_info_level <= DINFO_LEVEL_TERSE)
19140 else if (declaration)
19141 gen_formal_types_die (decl, subr_die);
19142 else
19144 /* Generate DIEs to represent all known formal parameters. */
19145 tree parm = DECL_ARGUMENTS (decl);
19146 tree generic_decl = early_dwarf
19147 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19148 tree generic_decl_parm = generic_decl
19149 ? DECL_ARGUMENTS (generic_decl)
19150 : NULL;
19152 /* Now we want to walk the list of parameters of the function and
19153 emit their relevant DIEs.
19155 We consider the case of DECL being an instance of a generic function
19156 as well as it being a normal function.
19158 If DECL is an instance of a generic function we walk the
19159 parameters of the generic function declaration _and_ the parameters of
19160 DECL itself. This is useful because we want to emit specific DIEs for
19161 function parameter packs and those are declared as part of the
19162 generic function declaration. In that particular case,
19163 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19164 That DIE has children DIEs representing the set of arguments
19165 of the pack. Note that the set of pack arguments can be empty.
19166 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19167 children DIE.
19169 Otherwise, we just consider the parameters of DECL. */
19170 while (generic_decl_parm || parm)
19172 if (generic_decl_parm
19173 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19174 gen_formal_parameter_pack_die (generic_decl_parm,
19175 parm, subr_die,
19176 &parm);
19177 else if (parm && !POINTER_BOUNDS_P (parm))
19179 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19181 if (parm == DECL_ARGUMENTS (decl)
19182 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19183 && parm_die
19184 && (dwarf_version >= 3 || !dwarf_strict))
19185 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19187 parm = DECL_CHAIN (parm);
19189 else if (parm)
19190 parm = DECL_CHAIN (parm);
19192 if (generic_decl_parm)
19193 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19196 /* Decide whether we need an unspecified_parameters DIE at the end.
19197 There are 2 more cases to do this for: 1) the ansi ... declaration -
19198 this is detectable when the end of the arg list is not a
19199 void_type_node 2) an unprototyped function declaration (not a
19200 definition). This just means that we have no info about the
19201 parameters at all. */
19202 if (prototype_p (TREE_TYPE (decl)))
19204 /* This is the prototyped case, check for.... */
19205 if (stdarg_p (TREE_TYPE (decl)))
19206 gen_unspecified_parameters_die (decl, subr_die);
19208 else if (DECL_INITIAL (decl) == NULL_TREE)
19209 gen_unspecified_parameters_die (decl, subr_die);
19212 if (subr_die != old_die)
19213 /* Add the calling convention attribute if requested. */
19214 add_calling_convention_attribute (subr_die, decl);
19216 /* Output Dwarf info for all of the stuff within the body of the function
19217 (if it has one - it may be just a declaration).
19219 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19220 a function. This BLOCK actually represents the outermost binding contour
19221 for the function, i.e. the contour in which the function's formal
19222 parameters and labels get declared. Curiously, it appears that the front
19223 end doesn't actually put the PARM_DECL nodes for the current function onto
19224 the BLOCK_VARS list for this outer scope, but are strung off of the
19225 DECL_ARGUMENTS list for the function instead.
19227 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19228 the LABEL_DECL nodes for the function however, and we output DWARF info
19229 for those in decls_for_scope. Just within the `outer_scope' there will be
19230 a BLOCK node representing the function's outermost pair of curly braces,
19231 and any blocks used for the base and member initializers of a C++
19232 constructor function. */
19233 tree outer_scope = DECL_INITIAL (decl);
19234 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19236 int call_site_note_count = 0;
19237 int tail_call_site_note_count = 0;
19239 /* Emit a DW_TAG_variable DIE for a named return value. */
19240 if (DECL_NAME (DECL_RESULT (decl)))
19241 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19243 /* The first time through decls_for_scope we will generate the
19244 DIEs for the locals. The second time, we fill in the
19245 location info. */
19246 decls_for_scope (outer_scope, subr_die);
19248 if (call_arg_locations && !dwarf_strict)
19250 struct call_arg_loc_node *ca_loc;
19251 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19253 dw_die_ref die = NULL;
19254 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19255 rtx arg, next_arg;
19257 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19258 arg; arg = next_arg)
19260 dw_loc_descr_ref reg, val;
19261 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19262 dw_die_ref cdie, tdie = NULL;
19264 next_arg = XEXP (arg, 1);
19265 if (REG_P (XEXP (XEXP (arg, 0), 0))
19266 && next_arg
19267 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19268 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19269 && REGNO (XEXP (XEXP (arg, 0), 0))
19270 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19271 next_arg = XEXP (next_arg, 1);
19272 if (mode == VOIDmode)
19274 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19275 if (mode == VOIDmode)
19276 mode = GET_MODE (XEXP (arg, 0));
19278 if (mode == VOIDmode || mode == BLKmode)
19279 continue;
19280 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19282 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19283 tloc = XEXP (XEXP (arg, 0), 1);
19284 continue;
19286 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19287 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19289 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19290 tlocc = XEXP (XEXP (arg, 0), 1);
19291 continue;
19293 reg = NULL;
19294 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19295 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19296 VAR_INIT_STATUS_INITIALIZED);
19297 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19299 rtx mem = XEXP (XEXP (arg, 0), 0);
19300 reg = mem_loc_descriptor (XEXP (mem, 0),
19301 get_address_mode (mem),
19302 GET_MODE (mem),
19303 VAR_INIT_STATUS_INITIALIZED);
19305 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19306 == DEBUG_PARAMETER_REF)
19308 tree tdecl
19309 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19310 tdie = lookup_decl_die (tdecl);
19311 if (tdie == NULL)
19312 continue;
19314 else
19315 continue;
19316 if (reg == NULL
19317 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19318 != DEBUG_PARAMETER_REF)
19319 continue;
19320 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19321 VOIDmode,
19322 VAR_INIT_STATUS_INITIALIZED);
19323 if (val == NULL)
19324 continue;
19325 if (die == NULL)
19326 die = gen_call_site_die (decl, subr_die, ca_loc);
19327 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19328 NULL_TREE);
19329 if (reg != NULL)
19330 add_AT_loc (cdie, DW_AT_location, reg);
19331 else if (tdie != NULL)
19332 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19333 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19334 if (next_arg != XEXP (arg, 1))
19336 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19337 if (mode == VOIDmode)
19338 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19339 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19340 0), 1),
19341 mode, VOIDmode,
19342 VAR_INIT_STATUS_INITIALIZED);
19343 if (val != NULL)
19344 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19347 if (die == NULL
19348 && (ca_loc->symbol_ref || tloc))
19349 die = gen_call_site_die (decl, subr_die, ca_loc);
19350 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19352 dw_loc_descr_ref tval = NULL;
19354 if (tloc != NULL_RTX)
19355 tval = mem_loc_descriptor (tloc,
19356 GET_MODE (tloc) == VOIDmode
19357 ? Pmode : GET_MODE (tloc),
19358 VOIDmode,
19359 VAR_INIT_STATUS_INITIALIZED);
19360 if (tval)
19361 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19362 else if (tlocc != NULL_RTX)
19364 tval = mem_loc_descriptor (tlocc,
19365 GET_MODE (tlocc) == VOIDmode
19366 ? Pmode : GET_MODE (tlocc),
19367 VOIDmode,
19368 VAR_INIT_STATUS_INITIALIZED);
19369 if (tval)
19370 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19371 tval);
19374 if (die != NULL)
19376 call_site_note_count++;
19377 if (ca_loc->tail_call_p)
19378 tail_call_site_note_count++;
19382 call_arg_locations = NULL;
19383 call_arg_loc_last = NULL;
19384 if (tail_call_site_count >= 0
19385 && tail_call_site_count == tail_call_site_note_count
19386 && !dwarf_strict)
19388 if (call_site_count >= 0
19389 && call_site_count == call_site_note_count)
19390 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19391 else
19392 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19394 call_site_count = -1;
19395 tail_call_site_count = -1;
19399 /* Returns a hash value for X (which really is a die_struct). */
19401 hashval_t
19402 block_die_hasher::hash (die_struct *d)
19404 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19407 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19408 as decl_id and die_parent of die_struct Y. */
19410 bool
19411 block_die_hasher::equal (die_struct *x, die_struct *y)
19413 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19416 /* Return TRUE if DECL, which may have been previously generated as
19417 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19418 true if decl (or its origin) is either an extern declaration or a
19419 class/namespace scoped declaration.
19421 The declare_in_namespace support causes us to get two DIEs for one
19422 variable, both of which are declarations. We want to avoid
19423 considering one to be a specification, so we must test for
19424 DECLARATION and DW_AT_declaration. */
19425 static inline bool
19426 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19428 return (old_die && TREE_STATIC (decl) && !declaration
19429 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19432 /* Return true if DECL is a local static. */
19434 static inline bool
19435 local_function_static (tree decl)
19437 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19438 return TREE_STATIC (decl)
19439 && DECL_CONTEXT (decl)
19440 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19443 /* Generate a DIE to represent a declared data object.
19444 Either DECL or ORIGIN must be non-null. */
19446 static void
19447 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19449 HOST_WIDE_INT off = 0;
19450 tree com_decl;
19451 tree decl_or_origin = decl ? decl : origin;
19452 tree ultimate_origin;
19453 dw_die_ref var_die;
19454 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19455 dw_die_ref origin_die = NULL;
19456 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19457 || class_or_namespace_scope_p (context_die));
19458 bool specialization_p = false;
19460 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19461 if (decl || ultimate_origin)
19462 origin = ultimate_origin;
19463 com_decl = fortran_common (decl_or_origin, &off);
19465 /* Symbol in common gets emitted as a child of the common block, in the form
19466 of a data member. */
19467 if (com_decl)
19469 dw_die_ref com_die;
19470 dw_loc_list_ref loc;
19471 die_node com_die_arg;
19473 var_die = lookup_decl_die (decl_or_origin);
19474 if (var_die)
19476 if (get_AT (var_die, DW_AT_location) == NULL)
19478 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19479 if (loc)
19481 if (off)
19483 /* Optimize the common case. */
19484 if (single_element_loc_list_p (loc)
19485 && loc->expr->dw_loc_opc == DW_OP_addr
19486 && loc->expr->dw_loc_next == NULL
19487 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19488 == SYMBOL_REF)
19490 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19491 loc->expr->dw_loc_oprnd1.v.val_addr
19492 = plus_constant (GET_MODE (x), x , off);
19494 else
19495 loc_list_plus_const (loc, off);
19497 add_AT_location_description (var_die, DW_AT_location, loc);
19498 remove_AT (var_die, DW_AT_declaration);
19501 return;
19504 if (common_block_die_table == NULL)
19505 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19507 com_die_arg.decl_id = DECL_UID (com_decl);
19508 com_die_arg.die_parent = context_die;
19509 com_die = common_block_die_table->find (&com_die_arg);
19510 loc = loc_list_from_tree (com_decl, 2, NULL);
19511 if (com_die == NULL)
19513 const char *cnam
19514 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19515 die_node **slot;
19517 com_die = new_die (DW_TAG_common_block, context_die, decl);
19518 add_name_and_src_coords_attributes (com_die, com_decl);
19519 if (loc)
19521 add_AT_location_description (com_die, DW_AT_location, loc);
19522 /* Avoid sharing the same loc descriptor between
19523 DW_TAG_common_block and DW_TAG_variable. */
19524 loc = loc_list_from_tree (com_decl, 2, NULL);
19526 else if (DECL_EXTERNAL (decl))
19527 add_AT_flag (com_die, DW_AT_declaration, 1);
19528 if (want_pubnames ())
19529 add_pubname_string (cnam, com_die); /* ??? needed? */
19530 com_die->decl_id = DECL_UID (com_decl);
19531 slot = common_block_die_table->find_slot (com_die, INSERT);
19532 *slot = com_die;
19534 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19536 add_AT_location_description (com_die, DW_AT_location, loc);
19537 loc = loc_list_from_tree (com_decl, 2, NULL);
19538 remove_AT (com_die, DW_AT_declaration);
19540 var_die = new_die (DW_TAG_variable, com_die, decl);
19541 add_name_and_src_coords_attributes (var_die, decl);
19542 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19543 context_die);
19544 add_AT_flag (var_die, DW_AT_external, 1);
19545 if (loc)
19547 if (off)
19549 /* Optimize the common case. */
19550 if (single_element_loc_list_p (loc)
19551 && loc->expr->dw_loc_opc == DW_OP_addr
19552 && loc->expr->dw_loc_next == NULL
19553 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19555 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19556 loc->expr->dw_loc_oprnd1.v.val_addr
19557 = plus_constant (GET_MODE (x), x, off);
19559 else
19560 loc_list_plus_const (loc, off);
19562 add_AT_location_description (var_die, DW_AT_location, loc);
19564 else if (DECL_EXTERNAL (decl))
19565 add_AT_flag (var_die, DW_AT_declaration, 1);
19566 equate_decl_number_to_die (decl, var_die);
19567 return;
19570 if (old_die)
19572 if (declaration)
19574 /* A declaration that has been previously dumped, needs no
19575 further annotations, since it doesn't need location on
19576 the second pass. */
19577 return;
19579 else if (decl_will_get_specification_p (old_die, decl, declaration)
19580 && !get_AT (old_die, DW_AT_specification))
19582 /* Fall-thru so we can make a new variable die along with a
19583 DW_AT_specification. */
19585 else if (origin && old_die->die_parent != context_die)
19587 /* If we will be creating an inlined instance, we need a
19588 new DIE that will get annotated with
19589 DW_AT_abstract_origin. Clear things so we can get a
19590 new DIE. */
19591 gcc_assert (!DECL_ABSTRACT_P (decl));
19592 old_die = NULL;
19594 else
19596 /* If a DIE was dumped early, it still needs location info.
19597 Skip to where we fill the location bits. */
19598 var_die = old_die;
19599 goto gen_variable_die_location;
19603 /* For static data members, the declaration in the class is supposed
19604 to have DW_TAG_member tag; the specification should still be
19605 DW_TAG_variable referencing the DW_TAG_member DIE. */
19606 if (declaration && class_scope_p (context_die))
19607 var_die = new_die (DW_TAG_member, context_die, decl);
19608 else
19609 var_die = new_die (DW_TAG_variable, context_die, decl);
19611 if (origin != NULL)
19612 origin_die = add_abstract_origin_attribute (var_die, origin);
19614 /* Loop unrolling can create multiple blocks that refer to the same
19615 static variable, so we must test for the DW_AT_declaration flag.
19617 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19618 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19619 sharing them.
19621 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19622 else if (decl_will_get_specification_p (old_die, decl, declaration))
19624 /* This is a definition of a C++ class level static. */
19625 add_AT_specification (var_die, old_die);
19626 specialization_p = true;
19627 if (DECL_NAME (decl))
19629 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19630 struct dwarf_file_data * file_index = lookup_filename (s.file);
19632 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19633 add_AT_file (var_die, DW_AT_decl_file, file_index);
19635 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19636 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19638 if (old_die->die_tag == DW_TAG_member)
19639 add_linkage_name (var_die, decl);
19642 else
19643 add_name_and_src_coords_attributes (var_die, decl);
19645 if ((origin == NULL && !specialization_p)
19646 || (origin != NULL
19647 && !DECL_ABSTRACT_P (decl_or_origin)
19648 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19649 decl_function_context
19650 (decl_or_origin))))
19652 tree type = TREE_TYPE (decl_or_origin);
19654 if (decl_by_reference_p (decl_or_origin))
19655 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19656 context_die);
19657 else
19658 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19659 context_die);
19662 if (origin == NULL && !specialization_p)
19664 if (TREE_PUBLIC (decl))
19665 add_AT_flag (var_die, DW_AT_external, 1);
19667 if (DECL_ARTIFICIAL (decl))
19668 add_AT_flag (var_die, DW_AT_artificial, 1);
19670 add_accessibility_attribute (var_die, decl);
19673 if (declaration)
19674 add_AT_flag (var_die, DW_AT_declaration, 1);
19676 if (decl && (DECL_ABSTRACT_P (decl)
19677 || !old_die || is_declaration_die (old_die)))
19678 equate_decl_number_to_die (decl, var_die);
19680 gen_variable_die_location:
19681 if (! declaration
19682 && (! DECL_ABSTRACT_P (decl_or_origin)
19683 /* Local static vars are shared between all clones/inlines,
19684 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19685 already set. */
19686 || (TREE_CODE (decl_or_origin) == VAR_DECL
19687 && TREE_STATIC (decl_or_origin)
19688 && DECL_RTL_SET_P (decl_or_origin)))
19689 /* When abstract origin already has DW_AT_location attribute, no need
19690 to add it again. */
19691 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19693 if (early_dwarf)
19694 add_pubname (decl_or_origin, var_die);
19695 else
19696 add_location_or_const_value_attribute (var_die, decl_or_origin,
19697 decl == NULL);
19699 else
19700 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19703 /* Generate a DIE to represent a named constant. */
19705 static void
19706 gen_const_die (tree decl, dw_die_ref context_die)
19708 dw_die_ref const_die;
19709 tree type = TREE_TYPE (decl);
19711 const_die = lookup_decl_die (decl);
19712 if (const_die)
19713 return;
19715 const_die = new_die (DW_TAG_constant, context_die, decl);
19716 equate_decl_number_to_die (decl, const_die);
19717 add_name_and_src_coords_attributes (const_die, decl);
19718 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19719 if (TREE_PUBLIC (decl))
19720 add_AT_flag (const_die, DW_AT_external, 1);
19721 if (DECL_ARTIFICIAL (decl))
19722 add_AT_flag (const_die, DW_AT_artificial, 1);
19723 tree_add_const_value_attribute_for_decl (const_die, decl);
19726 /* Generate a DIE to represent a label identifier. */
19728 static void
19729 gen_label_die (tree decl, dw_die_ref context_die)
19731 tree origin = decl_ultimate_origin (decl);
19732 dw_die_ref lbl_die = lookup_decl_die (decl);
19733 rtx insn;
19734 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19736 if (!lbl_die)
19738 lbl_die = new_die (DW_TAG_label, context_die, decl);
19739 equate_decl_number_to_die (decl, lbl_die);
19741 if (origin != NULL)
19742 add_abstract_origin_attribute (lbl_die, origin);
19743 else
19744 add_name_and_src_coords_attributes (lbl_die, decl);
19747 if (DECL_ABSTRACT_P (decl))
19748 equate_decl_number_to_die (decl, lbl_die);
19749 else
19751 insn = DECL_RTL_IF_SET (decl);
19753 /* Deleted labels are programmer specified labels which have been
19754 eliminated because of various optimizations. We still emit them
19755 here so that it is possible to put breakpoints on them. */
19756 if (insn
19757 && (LABEL_P (insn)
19758 || ((NOTE_P (insn)
19759 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19761 /* When optimization is enabled (via -O) some parts of the compiler
19762 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19763 represent source-level labels which were explicitly declared by
19764 the user. This really shouldn't be happening though, so catch
19765 it if it ever does happen. */
19766 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19768 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19769 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19771 else if (insn
19772 && NOTE_P (insn)
19773 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19774 && CODE_LABEL_NUMBER (insn) != -1)
19776 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19777 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19782 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19783 attributes to the DIE for a block STMT, to describe where the inlined
19784 function was called from. This is similar to add_src_coords_attributes. */
19786 static inline void
19787 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19789 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19791 if (dwarf_version >= 3 || !dwarf_strict)
19793 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19794 add_AT_unsigned (die, DW_AT_call_line, s.line);
19799 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19800 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19802 static inline void
19803 add_high_low_attributes (tree stmt, dw_die_ref die)
19805 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19807 if (BLOCK_FRAGMENT_CHAIN (stmt)
19808 && (dwarf_version >= 3 || !dwarf_strict))
19810 tree chain, superblock = NULL_TREE;
19811 dw_die_ref pdie;
19812 dw_attr_node *attr = NULL;
19814 if (inlined_function_outer_scope_p (stmt))
19816 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19817 BLOCK_NUMBER (stmt));
19818 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19821 /* Optimize duplicate .debug_ranges lists or even tails of
19822 lists. If this BLOCK has same ranges as its supercontext,
19823 lookup DW_AT_ranges attribute in the supercontext (and
19824 recursively so), verify that the ranges_table contains the
19825 right values and use it instead of adding a new .debug_range. */
19826 for (chain = stmt, pdie = die;
19827 BLOCK_SAME_RANGE (chain);
19828 chain = BLOCK_SUPERCONTEXT (chain))
19830 dw_attr_node *new_attr;
19832 pdie = pdie->die_parent;
19833 if (pdie == NULL)
19834 break;
19835 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19836 break;
19837 new_attr = get_AT (pdie, DW_AT_ranges);
19838 if (new_attr == NULL
19839 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19840 break;
19841 attr = new_attr;
19842 superblock = BLOCK_SUPERCONTEXT (chain);
19844 if (attr != NULL
19845 && (ranges_table[attr->dw_attr_val.v.val_offset
19846 / 2 / DWARF2_ADDR_SIZE].num
19847 == BLOCK_NUMBER (superblock))
19848 && BLOCK_FRAGMENT_CHAIN (superblock))
19850 unsigned long off = attr->dw_attr_val.v.val_offset
19851 / 2 / DWARF2_ADDR_SIZE;
19852 unsigned long supercnt = 0, thiscnt = 0;
19853 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19854 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19856 ++supercnt;
19857 gcc_checking_assert (ranges_table[off + supercnt].num
19858 == BLOCK_NUMBER (chain));
19860 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19861 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19862 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19863 ++thiscnt;
19864 gcc_assert (supercnt >= thiscnt);
19865 add_AT_range_list (die, DW_AT_ranges,
19866 ((off + supercnt - thiscnt)
19867 * 2 * DWARF2_ADDR_SIZE),
19868 false);
19869 return;
19872 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19874 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19877 add_ranges (chain);
19878 chain = BLOCK_FRAGMENT_CHAIN (chain);
19880 while (chain);
19881 add_ranges (NULL);
19883 else
19885 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19886 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19887 BLOCK_NUMBER (stmt));
19888 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19889 BLOCK_NUMBER (stmt));
19890 add_AT_low_high_pc (die, label, label_high, false);
19894 /* Generate a DIE for a lexical block. */
19896 static void
19897 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19899 dw_die_ref old_die = BLOCK_DIE (stmt);
19900 dw_die_ref stmt_die = NULL;
19901 if (!old_die)
19903 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19904 BLOCK_DIE (stmt) = stmt_die;
19907 if (BLOCK_ABSTRACT (stmt))
19909 if (old_die)
19911 /* This must have been generated early and it won't even
19912 need location information since it's a DW_AT_inline
19913 function. */
19914 if (flag_checking)
19915 for (dw_die_ref c = context_die; c; c = c->die_parent)
19916 if (c->die_tag == DW_TAG_inlined_subroutine
19917 || c->die_tag == DW_TAG_subprogram)
19919 gcc_assert (get_AT (c, DW_AT_inline));
19920 break;
19922 return;
19925 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19927 /* If this is an inlined instance, create a new lexical die for
19928 anything below to attach DW_AT_abstract_origin to. */
19929 if (old_die)
19931 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19932 BLOCK_DIE (stmt) = stmt_die;
19933 old_die = NULL;
19937 if (old_die)
19938 stmt_die = old_die;
19940 /* A non abstract block whose blocks have already been reordered
19941 should have the instruction range for this block. If so, set the
19942 high/low attributes. */
19943 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19945 gcc_assert (stmt_die);
19946 add_high_low_attributes (stmt, stmt_die);
19949 decls_for_scope (stmt, stmt_die);
19952 /* Generate a DIE for an inlined subprogram. */
19954 static void
19955 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19957 tree decl;
19959 /* The instance of function that is effectively being inlined shall not
19960 be abstract. */
19961 gcc_assert (! BLOCK_ABSTRACT (stmt));
19963 decl = block_ultimate_origin (stmt);
19965 /* Make sure any inlined functions are known to be inlineable. */
19966 gcc_checking_assert (DECL_ABSTRACT_P (decl)
19967 || cgraph_function_possibly_inlined_p (decl));
19969 /* Emit info for the abstract instance first, if we haven't yet. We
19970 must emit this even if the block is abstract, otherwise when we
19971 emit the block below (or elsewhere), we may end up trying to emit
19972 a die whose origin die hasn't been emitted, and crashing. */
19973 dwarf2out_abstract_function (decl);
19975 if (! BLOCK_ABSTRACT (stmt))
19977 dw_die_ref subr_die
19978 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19980 if (call_arg_locations)
19981 BLOCK_DIE (stmt) = subr_die;
19982 add_abstract_origin_attribute (subr_die, decl);
19983 if (TREE_ASM_WRITTEN (stmt))
19984 add_high_low_attributes (stmt, subr_die);
19985 add_call_src_coords_attributes (stmt, subr_die);
19987 decls_for_scope (stmt, subr_die);
19991 /* Generate a DIE for a field in a record, or structure. */
19993 static void
19994 gen_field_die (tree decl, dw_die_ref context_die)
19996 dw_die_ref decl_die;
19998 if (TREE_TYPE (decl) == error_mark_node)
19999 return;
20001 decl_die = new_die (DW_TAG_member, context_die, decl);
20002 add_name_and_src_coords_attributes (decl_die, decl);
20003 add_type_attribute (decl_die, member_declared_type (decl),
20004 decl_quals (decl), context_die);
20006 if (DECL_BIT_FIELD_TYPE (decl))
20008 add_byte_size_attribute (decl_die, decl);
20009 add_bit_size_attribute (decl_die, decl);
20010 add_bit_offset_attribute (decl_die, decl);
20013 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20014 add_data_member_location_attribute (decl_die, decl);
20016 if (DECL_ARTIFICIAL (decl))
20017 add_AT_flag (decl_die, DW_AT_artificial, 1);
20019 add_accessibility_attribute (decl_die, decl);
20021 /* Equate decl number to die, so that we can look up this decl later on. */
20022 equate_decl_number_to_die (decl, decl_die);
20025 #if 0
20026 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20027 Use modified_type_die instead.
20028 We keep this code here just in case these types of DIEs may be needed to
20029 represent certain things in other languages (e.g. Pascal) someday. */
20031 static void
20032 gen_pointer_type_die (tree type, dw_die_ref context_die)
20034 dw_die_ref ptr_die
20035 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20037 equate_type_number_to_die (type, ptr_die);
20038 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20039 context_die);
20040 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20043 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20044 Use modified_type_die instead.
20045 We keep this code here just in case these types of DIEs may be needed to
20046 represent certain things in other languages (e.g. Pascal) someday. */
20048 static void
20049 gen_reference_type_die (tree type, dw_die_ref context_die)
20051 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20053 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20054 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20055 else
20056 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20058 equate_type_number_to_die (type, ref_die);
20059 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20060 context_die);
20061 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20063 #endif
20065 /* Generate a DIE for a pointer to a member type. */
20067 static void
20068 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20070 dw_die_ref ptr_die
20071 = new_die (DW_TAG_ptr_to_member_type,
20072 scope_die_for (type, context_die), type);
20074 equate_type_number_to_die (type, ptr_die);
20075 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20076 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20077 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20078 context_die);
20081 static char *producer_string;
20083 /* Return a heap allocated producer string including command line options
20084 if -grecord-gcc-switches. */
20086 static char *
20087 gen_producer_string (void)
20089 size_t j;
20090 auto_vec<const char *> switches;
20091 const char *language_string = lang_hooks.name;
20092 char *producer, *tail;
20093 const char *p;
20094 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20095 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20097 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20098 switch (save_decoded_options[j].opt_index)
20100 case OPT_o:
20101 case OPT_d:
20102 case OPT_dumpbase:
20103 case OPT_dumpdir:
20104 case OPT_auxbase:
20105 case OPT_auxbase_strip:
20106 case OPT_quiet:
20107 case OPT_version:
20108 case OPT_v:
20109 case OPT_w:
20110 case OPT_L:
20111 case OPT_D:
20112 case OPT_I:
20113 case OPT_U:
20114 case OPT_SPECIAL_unknown:
20115 case OPT_SPECIAL_ignore:
20116 case OPT_SPECIAL_program_name:
20117 case OPT_SPECIAL_input_file:
20118 case OPT_grecord_gcc_switches:
20119 case OPT_gno_record_gcc_switches:
20120 case OPT__output_pch_:
20121 case OPT_fdiagnostics_show_location_:
20122 case OPT_fdiagnostics_show_option:
20123 case OPT_fdiagnostics_show_caret:
20124 case OPT_fdiagnostics_color_:
20125 case OPT_fverbose_asm:
20126 case OPT____:
20127 case OPT__sysroot_:
20128 case OPT_nostdinc:
20129 case OPT_nostdinc__:
20130 case OPT_fpreprocessed:
20131 case OPT_fltrans_output_list_:
20132 case OPT_fresolution_:
20133 /* Ignore these. */
20134 continue;
20135 default:
20136 if (cl_options[save_decoded_options[j].opt_index].flags
20137 & CL_NO_DWARF_RECORD)
20138 continue;
20139 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20140 == '-');
20141 switch (save_decoded_options[j].canonical_option[0][1])
20143 case 'M':
20144 case 'i':
20145 case 'W':
20146 continue;
20147 case 'f':
20148 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20149 "dump", 4) == 0)
20150 continue;
20151 break;
20152 default:
20153 break;
20155 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20156 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20157 break;
20160 producer = XNEWVEC (char, plen + 1 + len + 1);
20161 tail = producer;
20162 sprintf (tail, "%s %s", language_string, version_string);
20163 tail += plen;
20165 FOR_EACH_VEC_ELT (switches, j, p)
20167 len = strlen (p);
20168 *tail = ' ';
20169 memcpy (tail + 1, p, len);
20170 tail += len + 1;
20173 *tail = '\0';
20174 return producer;
20177 /* Given a C and/or C++ language/version string return the "highest".
20178 C++ is assumed to be "higher" than C in this case. Used for merging
20179 LTO translation unit languages. */
20180 static const char *
20181 highest_c_language (const char *lang1, const char *lang2)
20183 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20184 return "GNU C++14";
20185 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20186 return "GNU C++11";
20187 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20188 return "GNU C++98";
20190 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20191 return "GNU C11";
20192 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20193 return "GNU C99";
20194 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20195 return "GNU C89";
20197 gcc_unreachable ();
20201 /* Generate the DIE for the compilation unit. */
20203 static dw_die_ref
20204 gen_compile_unit_die (const char *filename)
20206 dw_die_ref die;
20207 const char *language_string = lang_hooks.name;
20208 int language;
20210 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20212 if (filename)
20214 add_name_attribute (die, filename);
20215 /* Don't add cwd for <built-in>. */
20216 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20217 add_comp_dir_attribute (die);
20220 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20222 /* If our producer is LTO try to figure out a common language to use
20223 from the global list of translation units. */
20224 if (strcmp (language_string, "GNU GIMPLE") == 0)
20226 unsigned i;
20227 tree t;
20228 const char *common_lang = NULL;
20230 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20232 if (!TRANSLATION_UNIT_LANGUAGE (t))
20233 continue;
20234 if (!common_lang)
20235 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20236 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20238 else if (strncmp (common_lang, "GNU C", 5) == 0
20239 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20240 /* Mixing C and C++ is ok, use C++ in that case. */
20241 common_lang = highest_c_language (common_lang,
20242 TRANSLATION_UNIT_LANGUAGE (t));
20243 else
20245 /* Fall back to C. */
20246 common_lang = NULL;
20247 break;
20251 if (common_lang)
20252 language_string = common_lang;
20255 language = DW_LANG_C;
20256 if (strncmp (language_string, "GNU C", 5) == 0
20257 && ISDIGIT (language_string[5]))
20259 language = DW_LANG_C89;
20260 if (dwarf_version >= 3 || !dwarf_strict)
20262 if (strcmp (language_string, "GNU C89") != 0)
20263 language = DW_LANG_C99;
20265 if (dwarf_version >= 5 /* || !dwarf_strict */)
20266 if (strcmp (language_string, "GNU C11") == 0)
20267 language = DW_LANG_C11;
20270 else if (strncmp (language_string, "GNU C++", 7) == 0)
20272 language = DW_LANG_C_plus_plus;
20273 if (dwarf_version >= 5 /* || !dwarf_strict */)
20275 if (strcmp (language_string, "GNU C++11") == 0)
20276 language = DW_LANG_C_plus_plus_11;
20277 else if (strcmp (language_string, "GNU C++14") == 0)
20278 language = DW_LANG_C_plus_plus_14;
20281 else if (strcmp (language_string, "GNU F77") == 0)
20282 language = DW_LANG_Fortran77;
20283 else if (strcmp (language_string, "GNU Pascal") == 0)
20284 language = DW_LANG_Pascal83;
20285 else if (dwarf_version >= 3 || !dwarf_strict)
20287 if (strcmp (language_string, "GNU Ada") == 0)
20288 language = DW_LANG_Ada95;
20289 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20291 language = DW_LANG_Fortran95;
20292 if (dwarf_version >= 5 /* || !dwarf_strict */)
20294 if (strcmp (language_string, "GNU Fortran2003") == 0)
20295 language = DW_LANG_Fortran03;
20296 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20297 language = DW_LANG_Fortran08;
20300 else if (strcmp (language_string, "GNU Java") == 0)
20301 language = DW_LANG_Java;
20302 else if (strcmp (language_string, "GNU Objective-C") == 0)
20303 language = DW_LANG_ObjC;
20304 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20305 language = DW_LANG_ObjC_plus_plus;
20306 else if (dwarf_version >= 5 || !dwarf_strict)
20308 if (strcmp (language_string, "GNU Go") == 0)
20309 language = DW_LANG_Go;
20312 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20313 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20314 language = DW_LANG_Fortran90;
20316 add_AT_unsigned (die, DW_AT_language, language);
20318 switch (language)
20320 case DW_LANG_Fortran77:
20321 case DW_LANG_Fortran90:
20322 case DW_LANG_Fortran95:
20323 case DW_LANG_Fortran03:
20324 case DW_LANG_Fortran08:
20325 /* Fortran has case insensitive identifiers and the front-end
20326 lowercases everything. */
20327 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20328 break;
20329 default:
20330 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20331 break;
20333 return die;
20336 /* Generate the DIE for a base class. */
20338 static void
20339 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20341 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20343 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20344 add_data_member_location_attribute (die, binfo);
20346 if (BINFO_VIRTUAL_P (binfo))
20347 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20349 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20350 children, otherwise the default is DW_ACCESS_public. In DWARF2
20351 the default has always been DW_ACCESS_private. */
20352 if (access == access_public_node)
20354 if (dwarf_version == 2
20355 || context_die->die_tag == DW_TAG_class_type)
20356 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20358 else if (access == access_protected_node)
20359 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20360 else if (dwarf_version > 2
20361 && context_die->die_tag != DW_TAG_class_type)
20362 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20365 /* Generate a DIE for a class member. */
20367 static void
20368 gen_member_die (tree type, dw_die_ref context_die)
20370 tree member;
20371 tree binfo = TYPE_BINFO (type);
20372 dw_die_ref child;
20374 /* If this is not an incomplete type, output descriptions of each of its
20375 members. Note that as we output the DIEs necessary to represent the
20376 members of this record or union type, we will also be trying to output
20377 DIEs to represent the *types* of those members. However the `type'
20378 function (above) will specifically avoid generating type DIEs for member
20379 types *within* the list of member DIEs for this (containing) type except
20380 for those types (of members) which are explicitly marked as also being
20381 members of this (containing) type themselves. The g++ front- end can
20382 force any given type to be treated as a member of some other (containing)
20383 type by setting the TYPE_CONTEXT of the given (member) type to point to
20384 the TREE node representing the appropriate (containing) type. */
20386 /* First output info about the base classes. */
20387 if (binfo)
20389 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20390 int i;
20391 tree base;
20393 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20394 gen_inheritance_die (base,
20395 (accesses ? (*accesses)[i] : access_public_node),
20396 context_die);
20399 /* Now output info about the data members and type members. */
20400 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20402 /* If we thought we were generating minimal debug info for TYPE
20403 and then changed our minds, some of the member declarations
20404 may have already been defined. Don't define them again, but
20405 do put them in the right order. */
20407 child = lookup_decl_die (member);
20408 if (child)
20409 splice_child_die (context_die, child);
20410 else
20411 gen_decl_die (member, NULL, context_die);
20414 /* We do not keep type methods in type variants. */
20415 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20416 /* Now output info about the function members (if any). */
20417 if (TYPE_METHODS (type) != error_mark_node)
20418 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20420 /* Don't include clones in the member list. */
20421 if (DECL_ABSTRACT_ORIGIN (member))
20422 continue;
20423 /* Nor constructors for anonymous classes. */
20424 if (DECL_ARTIFICIAL (member)
20425 && dwarf2_name (member, 0) == NULL)
20426 continue;
20428 child = lookup_decl_die (member);
20429 if (child)
20430 splice_child_die (context_die, child);
20431 else
20432 gen_decl_die (member, NULL, context_die);
20436 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20437 is set, we pretend that the type was never defined, so we only get the
20438 member DIEs needed by later specification DIEs. */
20440 static void
20441 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20442 enum debug_info_usage usage)
20444 if (TREE_ASM_WRITTEN (type))
20446 /* Fill in the bound of variable-length fields in late dwarf if
20447 still incomplete. */
20448 if (!early_dwarf && variably_modified_type_p (type, NULL))
20449 for (tree member = TYPE_FIELDS (type);
20450 member;
20451 member = DECL_CHAIN (member))
20452 fill_variable_array_bounds (TREE_TYPE (member));
20453 return;
20456 dw_die_ref type_die = lookup_type_die (type);
20457 dw_die_ref scope_die = 0;
20458 int nested = 0;
20459 int complete = (TYPE_SIZE (type)
20460 && (! TYPE_STUB_DECL (type)
20461 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20462 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20463 complete = complete && should_emit_struct_debug (type, usage);
20465 if (type_die && ! complete)
20466 return;
20468 if (TYPE_CONTEXT (type) != NULL_TREE
20469 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20470 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20471 nested = 1;
20473 scope_die = scope_die_for (type, context_die);
20475 /* Generate child dies for template paramaters. */
20476 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20477 schedule_generic_params_dies_gen (type);
20479 if (! type_die || (nested && is_cu_die (scope_die)))
20480 /* First occurrence of type or toplevel definition of nested class. */
20482 dw_die_ref old_die = type_die;
20484 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20485 ? record_type_tag (type) : DW_TAG_union_type,
20486 scope_die, type);
20487 equate_type_number_to_die (type, type_die);
20488 if (old_die)
20489 add_AT_specification (type_die, old_die);
20490 else
20491 add_name_attribute (type_die, type_tag (type));
20493 else
20494 remove_AT (type_die, DW_AT_declaration);
20496 /* If this type has been completed, then give it a byte_size attribute and
20497 then give a list of members. */
20498 if (complete && !ns_decl)
20500 /* Prevent infinite recursion in cases where the type of some member of
20501 this type is expressed in terms of this type itself. */
20502 TREE_ASM_WRITTEN (type) = 1;
20503 add_byte_size_attribute (type_die, type);
20504 if (TYPE_STUB_DECL (type) != NULL_TREE)
20506 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20507 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20510 /* If the first reference to this type was as the return type of an
20511 inline function, then it may not have a parent. Fix this now. */
20512 if (type_die->die_parent == NULL)
20513 add_child_die (scope_die, type_die);
20515 push_decl_scope (type);
20516 gen_member_die (type, type_die);
20517 pop_decl_scope ();
20519 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20520 if (TYPE_ARTIFICIAL (type))
20521 add_AT_flag (type_die, DW_AT_artificial, 1);
20523 /* GNU extension: Record what type our vtable lives in. */
20524 if (TYPE_VFIELD (type))
20526 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20528 gen_type_die (vtype, context_die);
20529 add_AT_die_ref (type_die, DW_AT_containing_type,
20530 lookup_type_die (vtype));
20533 else
20535 add_AT_flag (type_die, DW_AT_declaration, 1);
20537 /* We don't need to do this for function-local types. */
20538 if (TYPE_STUB_DECL (type)
20539 && ! decl_function_context (TYPE_STUB_DECL (type)))
20540 vec_safe_push (incomplete_types, type);
20543 if (get_AT (type_die, DW_AT_name))
20544 add_pubtype (type, type_die);
20547 /* Generate a DIE for a subroutine _type_. */
20549 static void
20550 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20552 tree return_type = TREE_TYPE (type);
20553 dw_die_ref subr_die
20554 = new_die (DW_TAG_subroutine_type,
20555 scope_die_for (type, context_die), type);
20557 equate_type_number_to_die (type, subr_die);
20558 add_prototyped_attribute (subr_die, type);
20559 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20560 gen_formal_types_die (type, subr_die);
20562 if (get_AT (subr_die, DW_AT_name))
20563 add_pubtype (type, subr_die);
20566 /* Generate a DIE for a type definition. */
20568 static void
20569 gen_typedef_die (tree decl, dw_die_ref context_die)
20571 dw_die_ref type_die;
20572 tree origin;
20574 if (TREE_ASM_WRITTEN (decl))
20576 if (DECL_ORIGINAL_TYPE (decl))
20577 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20578 return;
20581 TREE_ASM_WRITTEN (decl) = 1;
20582 type_die = new_die (DW_TAG_typedef, context_die, decl);
20583 origin = decl_ultimate_origin (decl);
20584 if (origin != NULL)
20585 add_abstract_origin_attribute (type_die, origin);
20586 else
20588 tree type;
20590 add_name_and_src_coords_attributes (type_die, decl);
20591 if (DECL_ORIGINAL_TYPE (decl))
20593 type = DECL_ORIGINAL_TYPE (decl);
20595 if (type == error_mark_node)
20596 return;
20598 gcc_assert (type != TREE_TYPE (decl));
20599 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20601 else
20603 type = TREE_TYPE (decl);
20605 if (type == error_mark_node)
20606 return;
20608 if (is_naming_typedef_decl (TYPE_NAME (type)))
20610 /* Here, we are in the case of decl being a typedef naming
20611 an anonymous type, e.g:
20612 typedef struct {...} foo;
20613 In that case TREE_TYPE (decl) is not a typedef variant
20614 type and TYPE_NAME of the anonymous type is set to the
20615 TYPE_DECL of the typedef. This construct is emitted by
20616 the C++ FE.
20618 TYPE is the anonymous struct named by the typedef
20619 DECL. As we need the DW_AT_type attribute of the
20620 DW_TAG_typedef to point to the DIE of TYPE, let's
20621 generate that DIE right away. add_type_attribute
20622 called below will then pick (via lookup_type_die) that
20623 anonymous struct DIE. */
20624 if (!TREE_ASM_WRITTEN (type))
20625 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20627 /* This is a GNU Extension. We are adding a
20628 DW_AT_linkage_name attribute to the DIE of the
20629 anonymous struct TYPE. The value of that attribute
20630 is the name of the typedef decl naming the anonymous
20631 struct. This greatly eases the work of consumers of
20632 this debug info. */
20633 add_linkage_attr (lookup_type_die (type), decl);
20637 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20639 if (is_naming_typedef_decl (decl))
20640 /* We want that all subsequent calls to lookup_type_die with
20641 TYPE in argument yield the DW_TAG_typedef we have just
20642 created. */
20643 equate_type_number_to_die (type, type_die);
20645 add_accessibility_attribute (type_die, decl);
20648 if (DECL_ABSTRACT_P (decl))
20649 equate_decl_number_to_die (decl, type_die);
20651 if (get_AT (type_die, DW_AT_name))
20652 add_pubtype (decl, type_die);
20655 /* Generate a DIE for a struct, class, enum or union type. */
20657 static void
20658 gen_tagged_type_die (tree type,
20659 dw_die_ref context_die,
20660 enum debug_info_usage usage)
20662 int need_pop;
20664 if (type == NULL_TREE
20665 || !is_tagged_type (type))
20666 return;
20668 if (TREE_ASM_WRITTEN (type))
20669 need_pop = 0;
20670 /* If this is a nested type whose containing class hasn't been written
20671 out yet, writing it out will cover this one, too. This does not apply
20672 to instantiations of member class templates; they need to be added to
20673 the containing class as they are generated. FIXME: This hurts the
20674 idea of combining type decls from multiple TUs, since we can't predict
20675 what set of template instantiations we'll get. */
20676 else if (TYPE_CONTEXT (type)
20677 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20678 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20680 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20682 if (TREE_ASM_WRITTEN (type))
20683 return;
20685 /* If that failed, attach ourselves to the stub. */
20686 push_decl_scope (TYPE_CONTEXT (type));
20687 context_die = lookup_type_die (TYPE_CONTEXT (type));
20688 need_pop = 1;
20690 else if (TYPE_CONTEXT (type) != NULL_TREE
20691 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20693 /* If this type is local to a function that hasn't been written
20694 out yet, use a NULL context for now; it will be fixed up in
20695 decls_for_scope. */
20696 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20697 /* A declaration DIE doesn't count; nested types need to go in the
20698 specification. */
20699 if (context_die && is_declaration_die (context_die))
20700 context_die = NULL;
20701 need_pop = 0;
20703 else
20705 context_die = declare_in_namespace (type, context_die);
20706 need_pop = 0;
20709 if (TREE_CODE (type) == ENUMERAL_TYPE)
20711 /* This might have been written out by the call to
20712 declare_in_namespace. */
20713 if (!TREE_ASM_WRITTEN (type))
20714 gen_enumeration_type_die (type, context_die);
20716 else
20717 gen_struct_or_union_type_die (type, context_die, usage);
20719 if (need_pop)
20720 pop_decl_scope ();
20722 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20723 it up if it is ever completed. gen_*_type_die will set it for us
20724 when appropriate. */
20727 /* Generate a type description DIE. */
20729 static void
20730 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20731 enum debug_info_usage usage)
20733 struct array_descr_info info;
20735 if (type == NULL_TREE || type == error_mark_node)
20736 return;
20738 if (flag_checking && type)
20739 verify_type (type);
20741 if (TYPE_NAME (type) != NULL_TREE
20742 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20743 && is_redundant_typedef (TYPE_NAME (type))
20744 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20745 /* The DECL of this type is a typedef we don't want to emit debug
20746 info for but we want debug info for its underlying typedef.
20747 This can happen for e.g, the injected-class-name of a C++
20748 type. */
20749 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20751 /* If TYPE is a typedef type variant, let's generate debug info
20752 for the parent typedef which TYPE is a type of. */
20753 if (typedef_variant_p (type))
20755 if (TREE_ASM_WRITTEN (type))
20756 return;
20758 /* Prevent broken recursion; we can't hand off to the same type. */
20759 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20761 /* Give typedefs the right scope. */
20762 context_die = scope_die_for (type, context_die);
20764 TREE_ASM_WRITTEN (type) = 1;
20766 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20767 return;
20770 /* If type is an anonymous tagged type named by a typedef, let's
20771 generate debug info for the typedef. */
20772 if (is_naming_typedef_decl (TYPE_NAME (type)))
20774 /* Use the DIE of the containing namespace as the parent DIE of
20775 the type description DIE we want to generate. */
20776 if (DECL_CONTEXT (TYPE_NAME (type))
20777 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20778 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20780 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20781 return;
20784 /* We are going to output a DIE to represent the unqualified version
20785 of this type (i.e. without any const or volatile qualifiers) so
20786 get the main variant (i.e. the unqualified version) of this type
20787 now. (Vectors are special because the debugging info is in the
20788 cloned type itself). */
20789 if (TREE_CODE (type) != VECTOR_TYPE)
20790 type = type_main_variant (type);
20792 /* If this is an array type with hidden descriptor, handle it first. */
20793 if (!TREE_ASM_WRITTEN (type)
20794 && lang_hooks.types.get_array_descr_info)
20796 memset (&info, 0, sizeof (info));
20797 if (lang_hooks.types.get_array_descr_info (type, &info))
20799 gen_descr_array_type_die (type, &info, context_die);
20800 TREE_ASM_WRITTEN (type) = 1;
20801 return;
20805 if (TREE_ASM_WRITTEN (type))
20807 /* Variable-length types may be incomplete even if
20808 TREE_ASM_WRITTEN. For such types, fall through to
20809 gen_array_type_die() and possibly fill in
20810 DW_AT_{upper,lower}_bound attributes. */
20811 if ((TREE_CODE (type) != ARRAY_TYPE
20812 && TREE_CODE (type) != RECORD_TYPE
20813 && TREE_CODE (type) != UNION_TYPE
20814 && TREE_CODE (type) != QUAL_UNION_TYPE)
20815 || !variably_modified_type_p (type, NULL))
20816 return;
20819 switch (TREE_CODE (type))
20821 case ERROR_MARK:
20822 break;
20824 case POINTER_TYPE:
20825 case REFERENCE_TYPE:
20826 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20827 ensures that the gen_type_die recursion will terminate even if the
20828 type is recursive. Recursive types are possible in Ada. */
20829 /* ??? We could perhaps do this for all types before the switch
20830 statement. */
20831 TREE_ASM_WRITTEN (type) = 1;
20833 /* For these types, all that is required is that we output a DIE (or a
20834 set of DIEs) to represent the "basis" type. */
20835 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20836 DINFO_USAGE_IND_USE);
20837 break;
20839 case OFFSET_TYPE:
20840 /* This code is used for C++ pointer-to-data-member types.
20841 Output a description of the relevant class type. */
20842 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20843 DINFO_USAGE_IND_USE);
20845 /* Output a description of the type of the object pointed to. */
20846 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20847 DINFO_USAGE_IND_USE);
20849 /* Now output a DIE to represent this pointer-to-data-member type
20850 itself. */
20851 gen_ptr_to_mbr_type_die (type, context_die);
20852 break;
20854 case FUNCTION_TYPE:
20855 /* Force out return type (in case it wasn't forced out already). */
20856 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20857 DINFO_USAGE_DIR_USE);
20858 gen_subroutine_type_die (type, context_die);
20859 break;
20861 case METHOD_TYPE:
20862 /* Force out return type (in case it wasn't forced out already). */
20863 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20864 DINFO_USAGE_DIR_USE);
20865 gen_subroutine_type_die (type, context_die);
20866 break;
20868 case ARRAY_TYPE:
20869 case VECTOR_TYPE:
20870 gen_array_type_die (type, context_die);
20871 break;
20873 case ENUMERAL_TYPE:
20874 case RECORD_TYPE:
20875 case UNION_TYPE:
20876 case QUAL_UNION_TYPE:
20877 gen_tagged_type_die (type, context_die, usage);
20878 return;
20880 case VOID_TYPE:
20881 case INTEGER_TYPE:
20882 case REAL_TYPE:
20883 case FIXED_POINT_TYPE:
20884 case COMPLEX_TYPE:
20885 case BOOLEAN_TYPE:
20886 case POINTER_BOUNDS_TYPE:
20887 /* No DIEs needed for fundamental types. */
20888 break;
20890 case NULLPTR_TYPE:
20891 case LANG_TYPE:
20892 /* Just use DW_TAG_unspecified_type. */
20894 dw_die_ref type_die = lookup_type_die (type);
20895 if (type_die == NULL)
20897 tree name = TYPE_IDENTIFIER (type);
20898 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20899 type);
20900 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20901 equate_type_number_to_die (type, type_die);
20904 break;
20906 default:
20907 if (is_cxx_auto (type))
20909 tree name = TYPE_IDENTIFIER (type);
20910 dw_die_ref *die = (name == get_identifier ("auto")
20911 ? &auto_die : &decltype_auto_die);
20912 if (!*die)
20914 *die = new_die (DW_TAG_unspecified_type,
20915 comp_unit_die (), NULL_TREE);
20916 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20918 equate_type_number_to_die (type, *die);
20919 break;
20921 gcc_unreachable ();
20924 TREE_ASM_WRITTEN (type) = 1;
20927 static void
20928 gen_type_die (tree type, dw_die_ref context_die)
20930 if (type != error_mark_node)
20932 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20933 if (flag_checking)
20935 dw_die_ref die = lookup_type_die (type);
20936 if (die)
20937 check_die (die);
20942 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20943 things which are local to the given block. */
20945 static void
20946 gen_block_die (tree stmt, dw_die_ref context_die)
20948 int must_output_die = 0;
20949 bool inlined_func;
20951 /* Ignore blocks that are NULL. */
20952 if (stmt == NULL_TREE)
20953 return;
20955 inlined_func = inlined_function_outer_scope_p (stmt);
20957 /* If the block is one fragment of a non-contiguous block, do not
20958 process the variables, since they will have been done by the
20959 origin block. Do process subblocks. */
20960 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20962 tree sub;
20964 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20965 gen_block_die (sub, context_die);
20967 return;
20970 /* Determine if we need to output any Dwarf DIEs at all to represent this
20971 block. */
20972 if (inlined_func)
20973 /* The outer scopes for inlinings *must* always be represented. We
20974 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20975 must_output_die = 1;
20976 else
20978 /* Determine if this block directly contains any "significant"
20979 local declarations which we will need to output DIEs for. */
20980 if (debug_info_level > DINFO_LEVEL_TERSE)
20981 /* We are not in terse mode so *any* local declaration counts
20982 as being a "significant" one. */
20983 must_output_die = ((BLOCK_VARS (stmt) != NULL
20984 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20985 && (TREE_USED (stmt)
20986 || TREE_ASM_WRITTEN (stmt)
20987 || BLOCK_ABSTRACT (stmt)));
20988 else if ((TREE_USED (stmt)
20989 || TREE_ASM_WRITTEN (stmt)
20990 || BLOCK_ABSTRACT (stmt))
20991 && !dwarf2out_ignore_block (stmt))
20992 must_output_die = 1;
20995 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20996 DIE for any block which contains no significant local declarations at
20997 all. Rather, in such cases we just call `decls_for_scope' so that any
20998 needed Dwarf info for any sub-blocks will get properly generated. Note
20999 that in terse mode, our definition of what constitutes a "significant"
21000 local declaration gets restricted to include only inlined function
21001 instances and local (nested) function definitions. */
21002 if (must_output_die)
21004 if (inlined_func)
21006 /* If STMT block is abstract, that means we have been called
21007 indirectly from dwarf2out_abstract_function.
21008 That function rightfully marks the descendent blocks (of
21009 the abstract function it is dealing with) as being abstract,
21010 precisely to prevent us from emitting any
21011 DW_TAG_inlined_subroutine DIE as a descendent
21012 of an abstract function instance. So in that case, we should
21013 not call gen_inlined_subroutine_die.
21015 Later though, when cgraph asks dwarf2out to emit info
21016 for the concrete instance of the function decl into which
21017 the concrete instance of STMT got inlined, the later will lead
21018 to the generation of a DW_TAG_inlined_subroutine DIE. */
21019 if (! BLOCK_ABSTRACT (stmt))
21020 gen_inlined_subroutine_die (stmt, context_die);
21022 else
21023 gen_lexical_block_die (stmt, context_die);
21025 else
21026 decls_for_scope (stmt, context_die);
21029 /* Process variable DECL (or variable with origin ORIGIN) within
21030 block STMT and add it to CONTEXT_DIE. */
21031 static void
21032 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21034 dw_die_ref die;
21035 tree decl_or_origin = decl ? decl : origin;
21037 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21038 die = lookup_decl_die (decl_or_origin);
21039 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21040 && TYPE_DECL_IS_STUB (decl_or_origin))
21041 die = lookup_type_die (TREE_TYPE (decl_or_origin));
21042 else
21043 die = NULL;
21045 if (die != NULL && die->die_parent == NULL)
21046 add_child_die (context_die, die);
21047 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21049 if (early_dwarf)
21050 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21051 stmt, context_die);
21053 else
21054 gen_decl_die (decl, origin, context_die);
21057 /* Generate all of the decls declared within a given scope and (recursively)
21058 all of its sub-blocks. */
21060 static void
21061 decls_for_scope (tree stmt, dw_die_ref context_die)
21063 tree decl;
21064 unsigned int i;
21065 tree subblocks;
21067 /* Ignore NULL blocks. */
21068 if (stmt == NULL_TREE)
21069 return;
21071 /* Output the DIEs to represent all of the data objects and typedefs
21072 declared directly within this block but not within any nested
21073 sub-blocks. Also, nested function and tag DIEs have been
21074 generated with a parent of NULL; fix that up now. We don't
21075 have to do this if we're at -g1. */
21076 if (debug_info_level > DINFO_LEVEL_TERSE)
21078 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21079 process_scope_var (stmt, decl, NULL_TREE, context_die);
21080 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21081 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21082 context_die);
21085 /* Even if we're at -g1, we need to process the subblocks in order to get
21086 inlined call information. */
21088 /* Output the DIEs to represent all sub-blocks (and the items declared
21089 therein) of this block. */
21090 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21091 subblocks != NULL;
21092 subblocks = BLOCK_CHAIN (subblocks))
21093 gen_block_die (subblocks, context_die);
21096 /* Is this a typedef we can avoid emitting? */
21098 static inline int
21099 is_redundant_typedef (const_tree decl)
21101 if (TYPE_DECL_IS_STUB (decl))
21102 return 1;
21104 if (DECL_ARTIFICIAL (decl)
21105 && DECL_CONTEXT (decl)
21106 && is_tagged_type (DECL_CONTEXT (decl))
21107 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21108 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21109 /* Also ignore the artificial member typedef for the class name. */
21110 return 1;
21112 return 0;
21115 /* Return TRUE if TYPE is a typedef that names a type for linkage
21116 purposes. This kind of typedefs is produced by the C++ FE for
21117 constructs like:
21119 typedef struct {...} foo;
21121 In that case, there is no typedef variant type produced for foo.
21122 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21123 struct type. */
21125 static bool
21126 is_naming_typedef_decl (const_tree decl)
21128 if (decl == NULL_TREE
21129 || TREE_CODE (decl) != TYPE_DECL
21130 || !is_tagged_type (TREE_TYPE (decl))
21131 || DECL_IS_BUILTIN (decl)
21132 || is_redundant_typedef (decl)
21133 /* It looks like Ada produces TYPE_DECLs that are very similar
21134 to C++ naming typedefs but that have different
21135 semantics. Let's be specific to c++ for now. */
21136 || !is_cxx ())
21137 return FALSE;
21139 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21140 && TYPE_NAME (TREE_TYPE (decl)) == decl
21141 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21142 != TYPE_NAME (TREE_TYPE (decl))));
21145 /* Looks up the DIE for a context. */
21147 static inline dw_die_ref
21148 lookup_context_die (tree context)
21150 if (context)
21152 /* Find die that represents this context. */
21153 if (TYPE_P (context))
21155 context = TYPE_MAIN_VARIANT (context);
21156 dw_die_ref ctx = lookup_type_die (context);
21157 if (!ctx)
21158 return NULL;
21159 return strip_naming_typedef (context, ctx);
21161 else
21162 return lookup_decl_die (context);
21164 return comp_unit_die ();
21167 /* Returns the DIE for a context. */
21169 static inline dw_die_ref
21170 get_context_die (tree context)
21172 if (context)
21174 /* Find die that represents this context. */
21175 if (TYPE_P (context))
21177 context = TYPE_MAIN_VARIANT (context);
21178 return strip_naming_typedef (context, force_type_die (context));
21180 else
21181 return force_decl_die (context);
21183 return comp_unit_die ();
21186 /* Returns the DIE for decl. A DIE will always be returned. */
21188 static dw_die_ref
21189 force_decl_die (tree decl)
21191 dw_die_ref decl_die;
21192 unsigned saved_external_flag;
21193 tree save_fn = NULL_TREE;
21194 decl_die = lookup_decl_die (decl);
21195 if (!decl_die)
21197 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21199 decl_die = lookup_decl_die (decl);
21200 if (decl_die)
21201 return decl_die;
21203 switch (TREE_CODE (decl))
21205 case FUNCTION_DECL:
21206 /* Clear current_function_decl, so that gen_subprogram_die thinks
21207 that this is a declaration. At this point, we just want to force
21208 declaration die. */
21209 save_fn = current_function_decl;
21210 current_function_decl = NULL_TREE;
21211 gen_subprogram_die (decl, context_die);
21212 current_function_decl = save_fn;
21213 break;
21215 case VAR_DECL:
21216 /* Set external flag to force declaration die. Restore it after
21217 gen_decl_die() call. */
21218 saved_external_flag = DECL_EXTERNAL (decl);
21219 DECL_EXTERNAL (decl) = 1;
21220 gen_decl_die (decl, NULL, context_die);
21221 DECL_EXTERNAL (decl) = saved_external_flag;
21222 break;
21224 case NAMESPACE_DECL:
21225 if (dwarf_version >= 3 || !dwarf_strict)
21226 dwarf2out_decl (decl);
21227 else
21228 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21229 decl_die = comp_unit_die ();
21230 break;
21232 case TRANSLATION_UNIT_DECL:
21233 decl_die = comp_unit_die ();
21234 break;
21236 default:
21237 gcc_unreachable ();
21240 /* We should be able to find the DIE now. */
21241 if (!decl_die)
21242 decl_die = lookup_decl_die (decl);
21243 gcc_assert (decl_die);
21246 return decl_die;
21249 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21250 always returned. */
21252 static dw_die_ref
21253 force_type_die (tree type)
21255 dw_die_ref type_die;
21257 type_die = lookup_type_die (type);
21258 if (!type_die)
21260 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21262 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21263 context_die);
21264 gcc_assert (type_die);
21266 return type_die;
21269 /* Force out any required namespaces to be able to output DECL,
21270 and return the new context_die for it, if it's changed. */
21272 static dw_die_ref
21273 setup_namespace_context (tree thing, dw_die_ref context_die)
21275 tree context = (DECL_P (thing)
21276 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21277 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21278 /* Force out the namespace. */
21279 context_die = force_decl_die (context);
21281 return context_die;
21284 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21285 type) within its namespace, if appropriate.
21287 For compatibility with older debuggers, namespace DIEs only contain
21288 declarations; all definitions are emitted at CU scope, with
21289 DW_AT_specification pointing to the declaration (like with class
21290 members). */
21292 static dw_die_ref
21293 declare_in_namespace (tree thing, dw_die_ref context_die)
21295 dw_die_ref ns_context;
21297 if (debug_info_level <= DINFO_LEVEL_TERSE)
21298 return context_die;
21300 /* External declarations in the local scope only need to be emitted
21301 once, not once in the namespace and once in the scope.
21303 This avoids declaring the `extern' below in the
21304 namespace DIE as well as in the innermost scope:
21306 namespace S
21308 int i=5;
21309 int foo()
21311 int i=8;
21312 extern int i;
21313 return i;
21317 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21318 return context_die;
21320 /* If this decl is from an inlined function, then don't try to emit it in its
21321 namespace, as we will get confused. It would have already been emitted
21322 when the abstract instance of the inline function was emitted anyways. */
21323 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21324 return context_die;
21326 ns_context = setup_namespace_context (thing, context_die);
21328 if (ns_context != context_die)
21330 if (is_fortran ())
21331 return ns_context;
21332 if (DECL_P (thing))
21333 gen_decl_die (thing, NULL, ns_context);
21334 else
21335 gen_type_die (thing, ns_context);
21337 return context_die;
21340 /* Generate a DIE for a namespace or namespace alias. */
21342 static void
21343 gen_namespace_die (tree decl, dw_die_ref context_die)
21345 dw_die_ref namespace_die;
21347 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21348 they are an alias of. */
21349 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21351 /* Output a real namespace or module. */
21352 context_die = setup_namespace_context (decl, comp_unit_die ());
21353 namespace_die = new_die (is_fortran ()
21354 ? DW_TAG_module : DW_TAG_namespace,
21355 context_die, decl);
21356 /* For Fortran modules defined in different CU don't add src coords. */
21357 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21359 const char *name = dwarf2_name (decl, 0);
21360 if (name)
21361 add_name_attribute (namespace_die, name);
21363 else
21364 add_name_and_src_coords_attributes (namespace_die, decl);
21365 if (DECL_EXTERNAL (decl))
21366 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21367 equate_decl_number_to_die (decl, namespace_die);
21369 else
21371 /* Output a namespace alias. */
21373 /* Force out the namespace we are an alias of, if necessary. */
21374 dw_die_ref origin_die
21375 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21377 if (DECL_FILE_SCOPE_P (decl)
21378 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21379 context_die = setup_namespace_context (decl, comp_unit_die ());
21380 /* Now create the namespace alias DIE. */
21381 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21382 add_name_and_src_coords_attributes (namespace_die, decl);
21383 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21384 equate_decl_number_to_die (decl, namespace_die);
21386 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21387 if (want_pubnames ())
21388 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21391 /* Generate Dwarf debug information for a decl described by DECL.
21392 The return value is currently only meaningful for PARM_DECLs,
21393 for all other decls it returns NULL. */
21395 static dw_die_ref
21396 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21398 tree decl_or_origin = decl ? decl : origin;
21399 tree class_origin = NULL, ultimate_origin;
21401 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21402 return NULL;
21404 /* Ignore pointer bounds decls. */
21405 if (DECL_P (decl_or_origin)
21406 && TREE_TYPE (decl_or_origin)
21407 && POINTER_BOUNDS_P (decl_or_origin))
21408 return NULL;
21410 switch (TREE_CODE (decl_or_origin))
21412 case ERROR_MARK:
21413 break;
21415 case CONST_DECL:
21416 if (!is_fortran () && !is_ada ())
21418 /* The individual enumerators of an enum type get output when we output
21419 the Dwarf representation of the relevant enum type itself. */
21420 break;
21423 /* Emit its type. */
21424 gen_type_die (TREE_TYPE (decl), context_die);
21426 /* And its containing namespace. */
21427 context_die = declare_in_namespace (decl, context_die);
21429 gen_const_die (decl, context_die);
21430 break;
21432 case FUNCTION_DECL:
21433 /* Don't output any DIEs to represent mere function declarations,
21434 unless they are class members or explicit block externs. */
21435 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21436 && DECL_FILE_SCOPE_P (decl_or_origin)
21437 && (current_function_decl == NULL_TREE
21438 || DECL_ARTIFICIAL (decl_or_origin)))
21439 break;
21441 #if 0
21442 /* FIXME */
21443 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21444 on local redeclarations of global functions. That seems broken. */
21445 if (current_function_decl != decl)
21446 /* This is only a declaration. */;
21447 #endif
21449 /* If we're emitting a clone, emit info for the abstract instance. */
21450 if (origin || DECL_ORIGIN (decl) != decl)
21451 dwarf2out_abstract_function (origin
21452 ? DECL_ORIGIN (origin)
21453 : DECL_ABSTRACT_ORIGIN (decl));
21455 /* If we're emitting an out-of-line copy of an inline function,
21456 emit info for the abstract instance and set up to refer to it. */
21457 else if (cgraph_function_possibly_inlined_p (decl)
21458 && ! DECL_ABSTRACT_P (decl)
21459 && ! class_or_namespace_scope_p (context_die)
21460 /* dwarf2out_abstract_function won't emit a die if this is just
21461 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21462 that case, because that works only if we have a die. */
21463 && DECL_INITIAL (decl) != NULL_TREE)
21465 dwarf2out_abstract_function (decl);
21466 set_decl_origin_self (decl);
21469 /* Otherwise we're emitting the primary DIE for this decl. */
21470 else if (debug_info_level > DINFO_LEVEL_TERSE)
21472 /* Before we describe the FUNCTION_DECL itself, make sure that we
21473 have its containing type. */
21474 if (!origin)
21475 origin = decl_class_context (decl);
21476 if (origin != NULL_TREE)
21477 gen_type_die (origin, context_die);
21479 /* And its return type. */
21480 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21482 /* And its virtual context. */
21483 if (DECL_VINDEX (decl) != NULL_TREE)
21484 gen_type_die (DECL_CONTEXT (decl), context_die);
21486 /* Make sure we have a member DIE for decl. */
21487 if (origin != NULL_TREE)
21488 gen_type_die_for_member (origin, decl, context_die);
21490 /* And its containing namespace. */
21491 context_die = declare_in_namespace (decl, context_die);
21494 /* Now output a DIE to represent the function itself. */
21495 if (decl)
21496 gen_subprogram_die (decl, context_die);
21497 break;
21499 case TYPE_DECL:
21500 /* If we are in terse mode, don't generate any DIEs to represent any
21501 actual typedefs. */
21502 if (debug_info_level <= DINFO_LEVEL_TERSE)
21503 break;
21505 /* In the special case of a TYPE_DECL node representing the declaration
21506 of some type tag, if the given TYPE_DECL is marked as having been
21507 instantiated from some other (original) TYPE_DECL node (e.g. one which
21508 was generated within the original definition of an inline function) we
21509 used to generate a special (abbreviated) DW_TAG_structure_type,
21510 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21511 should be actually referencing those DIEs, as variable DIEs with that
21512 type would be emitted already in the abstract origin, so it was always
21513 removed during unused type prunning. Don't add anything in this
21514 case. */
21515 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21516 break;
21518 if (is_redundant_typedef (decl))
21519 gen_type_die (TREE_TYPE (decl), context_die);
21520 else
21521 /* Output a DIE to represent the typedef itself. */
21522 gen_typedef_die (decl, context_die);
21523 break;
21525 case LABEL_DECL:
21526 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21527 gen_label_die (decl, context_die);
21528 break;
21530 case VAR_DECL:
21531 case RESULT_DECL:
21532 /* If we are in terse mode, don't generate any DIEs to represent any
21533 variable declarations or definitions. */
21534 if (debug_info_level <= DINFO_LEVEL_TERSE)
21535 break;
21537 /* Output any DIEs that are needed to specify the type of this data
21538 object. */
21539 if (decl_by_reference_p (decl_or_origin))
21540 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21541 else
21542 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21544 /* And its containing type. */
21545 class_origin = decl_class_context (decl_or_origin);
21546 if (class_origin != NULL_TREE)
21547 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21549 /* And its containing namespace. */
21550 context_die = declare_in_namespace (decl_or_origin, context_die);
21552 /* Now output the DIE to represent the data object itself. This gets
21553 complicated because of the possibility that the VAR_DECL really
21554 represents an inlined instance of a formal parameter for an inline
21555 function. */
21556 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21557 if (ultimate_origin != NULL_TREE
21558 && TREE_CODE (ultimate_origin) == PARM_DECL)
21559 gen_formal_parameter_die (decl, origin,
21560 true /* Emit name attribute. */,
21561 context_die);
21562 else
21563 gen_variable_die (decl, origin, context_die);
21564 break;
21566 case FIELD_DECL:
21567 /* Ignore the nameless fields that are used to skip bits but handle C++
21568 anonymous unions and structs. */
21569 if (DECL_NAME (decl) != NULL_TREE
21570 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21571 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21573 gen_type_die (member_declared_type (decl), context_die);
21574 gen_field_die (decl, context_die);
21576 break;
21578 case PARM_DECL:
21579 if (DECL_BY_REFERENCE (decl_or_origin))
21580 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21581 else
21582 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21583 return gen_formal_parameter_die (decl, origin,
21584 true /* Emit name attribute. */,
21585 context_die);
21587 case NAMESPACE_DECL:
21588 case IMPORTED_DECL:
21589 if (dwarf_version >= 3 || !dwarf_strict)
21590 gen_namespace_die (decl, context_die);
21591 break;
21593 case NAMELIST_DECL:
21594 gen_namelist_decl (DECL_NAME (decl), context_die,
21595 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21596 break;
21598 default:
21599 /* Probably some frontend-internal decl. Assume we don't care. */
21600 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21601 break;
21604 return NULL;
21607 /* Output initial debug information for global DECL. Called at the
21608 end of the parsing process.
21610 This is the initial debug generation process. As such, the DIEs
21611 generated may be incomplete. A later debug generation pass
21612 (dwarf2out_late_global_decl) will augment the information generated
21613 in this pass (e.g., with complete location info). */
21615 static void
21616 dwarf2out_early_global_decl (tree decl)
21618 set_early_dwarf s;
21620 /* gen_decl_die() will set DECL_ABSTRACT because
21621 cgraph_function_possibly_inlined_p() returns true. This is in
21622 turn will cause DW_AT_inline attributes to be set.
21624 This happens because at early dwarf generation, there is no
21625 cgraph information, causing cgraph_function_possibly_inlined_p()
21626 to return true. Trick cgraph_function_possibly_inlined_p()
21627 while we generate dwarf early. */
21628 bool save = symtab->global_info_ready;
21629 symtab->global_info_ready = true;
21631 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21632 other DECLs and they can point to template types or other things
21633 that dwarf2out can't handle when done via dwarf2out_decl. */
21634 if (TREE_CODE (decl) != TYPE_DECL
21635 && TREE_CODE (decl) != PARM_DECL)
21637 tree save_fndecl = current_function_decl;
21638 if (TREE_CODE (decl) == FUNCTION_DECL)
21640 /* No cfun means the symbol has no body, so there's nothing
21641 to emit. */
21642 if (!DECL_STRUCT_FUNCTION (decl))
21643 goto early_decl_exit;
21645 current_function_decl = decl;
21647 dwarf2out_decl (decl);
21648 if (TREE_CODE (decl) == FUNCTION_DECL)
21649 current_function_decl = save_fndecl;
21651 early_decl_exit:
21652 symtab->global_info_ready = save;
21655 /* Output debug information for global decl DECL. Called from
21656 toplev.c after compilation proper has finished. */
21658 static void
21659 dwarf2out_late_global_decl (tree decl)
21661 /* We have to generate early debug late for LTO. */
21662 if (in_lto_p)
21663 dwarf2out_early_global_decl (decl);
21665 /* Fill-in any location information we were unable to determine
21666 on the first pass. */
21667 if (TREE_CODE (decl) == VAR_DECL
21668 && !POINTER_BOUNDS_P (decl))
21670 dw_die_ref die = lookup_decl_die (decl);
21671 if (die)
21672 add_location_or_const_value_attribute (die, decl, false);
21676 /* Output debug information for type decl DECL. Called from toplev.c
21677 and from language front ends (to record built-in types). */
21678 static void
21679 dwarf2out_type_decl (tree decl, int local)
21681 if (!local)
21683 set_early_dwarf s;
21684 dwarf2out_decl (decl);
21688 /* Output debug information for imported module or decl DECL.
21689 NAME is non-NULL name in the lexical block if the decl has been renamed.
21690 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21691 that DECL belongs to.
21692 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21693 static void
21694 dwarf2out_imported_module_or_decl_1 (tree decl,
21695 tree name,
21696 tree lexical_block,
21697 dw_die_ref lexical_block_die)
21699 expanded_location xloc;
21700 dw_die_ref imported_die = NULL;
21701 dw_die_ref at_import_die;
21703 if (TREE_CODE (decl) == IMPORTED_DECL)
21705 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21706 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21707 gcc_assert (decl);
21709 else
21710 xloc = expand_location (input_location);
21712 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21714 at_import_die = force_type_die (TREE_TYPE (decl));
21715 /* For namespace N { typedef void T; } using N::T; base_type_die
21716 returns NULL, but DW_TAG_imported_declaration requires
21717 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21718 if (!at_import_die)
21720 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21721 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21722 at_import_die = lookup_type_die (TREE_TYPE (decl));
21723 gcc_assert (at_import_die);
21726 else
21728 at_import_die = lookup_decl_die (decl);
21729 if (!at_import_die)
21731 /* If we're trying to avoid duplicate debug info, we may not have
21732 emitted the member decl for this field. Emit it now. */
21733 if (TREE_CODE (decl) == FIELD_DECL)
21735 tree type = DECL_CONTEXT (decl);
21737 if (TYPE_CONTEXT (type)
21738 && TYPE_P (TYPE_CONTEXT (type))
21739 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21740 DINFO_USAGE_DIR_USE))
21741 return;
21742 gen_type_die_for_member (type, decl,
21743 get_context_die (TYPE_CONTEXT (type)));
21745 if (TREE_CODE (decl) == NAMELIST_DECL)
21746 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21747 get_context_die (DECL_CONTEXT (decl)),
21748 NULL_TREE);
21749 else
21750 at_import_die = force_decl_die (decl);
21754 if (TREE_CODE (decl) == NAMESPACE_DECL)
21756 if (dwarf_version >= 3 || !dwarf_strict)
21757 imported_die = new_die (DW_TAG_imported_module,
21758 lexical_block_die,
21759 lexical_block);
21760 else
21761 return;
21763 else
21764 imported_die = new_die (DW_TAG_imported_declaration,
21765 lexical_block_die,
21766 lexical_block);
21768 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21769 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21770 if (name)
21771 add_AT_string (imported_die, DW_AT_name,
21772 IDENTIFIER_POINTER (name));
21773 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21776 /* Output debug information for imported module or decl DECL.
21777 NAME is non-NULL name in context if the decl has been renamed.
21778 CHILD is true if decl is one of the renamed decls as part of
21779 importing whole module. */
21781 static void
21782 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21783 bool child)
21785 /* dw_die_ref at_import_die; */
21786 dw_die_ref scope_die;
21788 if (debug_info_level <= DINFO_LEVEL_TERSE)
21789 return;
21791 gcc_assert (decl);
21793 set_early_dwarf s;
21795 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21796 We need decl DIE for reference and scope die. First, get DIE for the decl
21797 itself. */
21799 /* Get the scope die for decl context. Use comp_unit_die for global module
21800 or decl. If die is not found for non globals, force new die. */
21801 if (context
21802 && TYPE_P (context)
21803 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21804 return;
21806 if (!(dwarf_version >= 3 || !dwarf_strict))
21807 return;
21809 scope_die = get_context_die (context);
21811 if (child)
21813 gcc_assert (scope_die->die_child);
21814 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21815 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21816 scope_die = scope_die->die_child;
21819 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21820 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21823 /* Output debug information for namelists. */
21825 static dw_die_ref
21826 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21828 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21829 tree value;
21830 unsigned i;
21832 if (debug_info_level <= DINFO_LEVEL_TERSE)
21833 return NULL;
21835 gcc_assert (scope_die != NULL);
21836 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21837 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21839 /* If there are no item_decls, we have a nondefining namelist, e.g.
21840 with USE association; hence, set DW_AT_declaration. */
21841 if (item_decls == NULL_TREE)
21843 add_AT_flag (nml_die, DW_AT_declaration, 1);
21844 return nml_die;
21847 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21849 nml_item_ref_die = lookup_decl_die (value);
21850 if (!nml_item_ref_die)
21851 nml_item_ref_die = force_decl_die (value);
21853 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21854 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21856 return nml_die;
21860 /* Write the debugging output for DECL and return the DIE. */
21862 static void
21863 dwarf2out_decl (tree decl)
21865 dw_die_ref context_die = comp_unit_die ();
21867 switch (TREE_CODE (decl))
21869 case ERROR_MARK:
21870 return;
21872 case FUNCTION_DECL:
21873 /* What we would really like to do here is to filter out all mere
21874 file-scope declarations of file-scope functions which are never
21875 referenced later within this translation unit (and keep all of ones
21876 that *are* referenced later on) but we aren't clairvoyant, so we have
21877 no idea which functions will be referenced in the future (i.e. later
21878 on within the current translation unit). So here we just ignore all
21879 file-scope function declarations which are not also definitions. If
21880 and when the debugger needs to know something about these functions,
21881 it will have to hunt around and find the DWARF information associated
21882 with the definition of the function.
21884 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21885 nodes represent definitions and which ones represent mere
21886 declarations. We have to check DECL_INITIAL instead. That's because
21887 the C front-end supports some weird semantics for "extern inline"
21888 function definitions. These can get inlined within the current
21889 translation unit (and thus, we need to generate Dwarf info for their
21890 abstract instances so that the Dwarf info for the concrete inlined
21891 instances can have something to refer to) but the compiler never
21892 generates any out-of-lines instances of such things (despite the fact
21893 that they *are* definitions).
21895 The important point is that the C front-end marks these "extern
21896 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21897 them anyway. Note that the C++ front-end also plays some similar games
21898 for inline function definitions appearing within include files which
21899 also contain `#pragma interface' pragmas.
21901 If we are called from dwarf2out_abstract_function output a DIE
21902 anyway. We can end up here this way with early inlining and LTO
21903 where the inlined function is output in a different LTRANS unit
21904 or not at all. */
21905 if (DECL_INITIAL (decl) == NULL_TREE
21906 && ! DECL_ABSTRACT_P (decl))
21907 return;
21909 /* If we're a nested function, initially use a parent of NULL; if we're
21910 a plain function, this will be fixed up in decls_for_scope. If
21911 we're a method, it will be ignored, since we already have a DIE. */
21912 if (decl_function_context (decl)
21913 /* But if we're in terse mode, we don't care about scope. */
21914 && debug_info_level > DINFO_LEVEL_TERSE)
21915 context_die = NULL;
21916 break;
21918 case VAR_DECL:
21919 /* For local statics lookup proper context die. */
21920 if (local_function_static (decl))
21921 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21923 /* If we are in terse mode, don't generate any DIEs to represent any
21924 variable declarations or definitions. */
21925 if (debug_info_level <= DINFO_LEVEL_TERSE)
21926 return;
21927 break;
21929 case CONST_DECL:
21930 if (debug_info_level <= DINFO_LEVEL_TERSE)
21931 return;
21932 if (!is_fortran () && !is_ada ())
21933 return;
21934 if (TREE_STATIC (decl) && decl_function_context (decl))
21935 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21936 break;
21938 case NAMESPACE_DECL:
21939 case IMPORTED_DECL:
21940 if (debug_info_level <= DINFO_LEVEL_TERSE)
21941 return;
21942 if (lookup_decl_die (decl) != NULL)
21943 return;
21944 break;
21946 case TYPE_DECL:
21947 /* Don't emit stubs for types unless they are needed by other DIEs. */
21948 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21949 return;
21951 /* Don't bother trying to generate any DIEs to represent any of the
21952 normal built-in types for the language we are compiling. */
21953 if (DECL_IS_BUILTIN (decl))
21954 return;
21956 /* If we are in terse mode, don't generate any DIEs for types. */
21957 if (debug_info_level <= DINFO_LEVEL_TERSE)
21958 return;
21960 /* If we're a function-scope tag, initially use a parent of NULL;
21961 this will be fixed up in decls_for_scope. */
21962 if (decl_function_context (decl))
21963 context_die = NULL;
21965 break;
21967 case NAMELIST_DECL:
21968 break;
21970 default:
21971 return;
21974 gen_decl_die (decl, NULL, context_die);
21976 if (flag_checking)
21978 dw_die_ref die = lookup_decl_die (decl);
21979 if (die)
21980 check_die (die);
21984 /* Write the debugging output for DECL. */
21986 static void
21987 dwarf2out_function_decl (tree decl)
21989 dwarf2out_decl (decl);
21990 call_arg_locations = NULL;
21991 call_arg_loc_last = NULL;
21992 call_site_count = -1;
21993 tail_call_site_count = -1;
21994 decl_loc_table->empty ();
21995 cached_dw_loc_list_table->empty ();
21998 /* Output a marker (i.e. a label) for the beginning of the generated code for
21999 a lexical block. */
22001 static void
22002 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22003 unsigned int blocknum)
22005 switch_to_section (current_function_section ());
22006 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22009 /* Output a marker (i.e. a label) for the end of the generated code for a
22010 lexical block. */
22012 static void
22013 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22015 switch_to_section (current_function_section ());
22016 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22019 /* Returns nonzero if it is appropriate not to emit any debugging
22020 information for BLOCK, because it doesn't contain any instructions.
22022 Don't allow this for blocks with nested functions or local classes
22023 as we would end up with orphans, and in the presence of scheduling
22024 we may end up calling them anyway. */
22026 static bool
22027 dwarf2out_ignore_block (const_tree block)
22029 tree decl;
22030 unsigned int i;
22032 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22033 if (TREE_CODE (decl) == FUNCTION_DECL
22034 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22035 return 0;
22036 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22038 decl = BLOCK_NONLOCALIZED_VAR (block, i);
22039 if (TREE_CODE (decl) == FUNCTION_DECL
22040 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22041 return 0;
22044 return 1;
22047 /* Hash table routines for file_hash. */
22049 bool
22050 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22052 return filename_cmp (p1->filename, p2) == 0;
22055 hashval_t
22056 dwarf_file_hasher::hash (dwarf_file_data *p)
22058 return htab_hash_string (p->filename);
22061 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22062 dwarf2out.c) and return its "index". The index of each (known) filename is
22063 just a unique number which is associated with only that one filename. We
22064 need such numbers for the sake of generating labels (in the .debug_sfnames
22065 section) and references to those files numbers (in the .debug_srcinfo
22066 and .debug_macinfo sections). If the filename given as an argument is not
22067 found in our current list, add it to the list and assign it the next
22068 available unique index number. */
22070 static struct dwarf_file_data *
22071 lookup_filename (const char *file_name)
22073 struct dwarf_file_data * created;
22075 if (!file_name)
22076 return NULL;
22078 dwarf_file_data **slot
22079 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22080 INSERT);
22081 if (*slot)
22082 return *slot;
22084 created = ggc_alloc<dwarf_file_data> ();
22085 created->filename = file_name;
22086 created->emitted_number = 0;
22087 *slot = created;
22088 return created;
22091 /* If the assembler will construct the file table, then translate the compiler
22092 internal file table number into the assembler file table number, and emit
22093 a .file directive if we haven't already emitted one yet. The file table
22094 numbers are different because we prune debug info for unused variables and
22095 types, which may include filenames. */
22097 static int
22098 maybe_emit_file (struct dwarf_file_data * fd)
22100 if (! fd->emitted_number)
22102 if (last_emitted_file)
22103 fd->emitted_number = last_emitted_file->emitted_number + 1;
22104 else
22105 fd->emitted_number = 1;
22106 last_emitted_file = fd;
22108 if (DWARF2_ASM_LINE_DEBUG_INFO)
22110 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22111 output_quoted_string (asm_out_file,
22112 remap_debug_filename (fd->filename));
22113 fputc ('\n', asm_out_file);
22117 return fd->emitted_number;
22120 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22121 That generation should happen after function debug info has been
22122 generated. The value of the attribute is the constant value of ARG. */
22124 static void
22125 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22127 die_arg_entry entry;
22129 if (!die || !arg)
22130 return;
22132 gcc_assert (early_dwarf);
22134 if (!tmpl_value_parm_die_table)
22135 vec_alloc (tmpl_value_parm_die_table, 32);
22137 entry.die = die;
22138 entry.arg = arg;
22139 vec_safe_push (tmpl_value_parm_die_table, entry);
22142 /* Return TRUE if T is an instance of generic type, FALSE
22143 otherwise. */
22145 static bool
22146 generic_type_p (tree t)
22148 if (t == NULL_TREE || !TYPE_P (t))
22149 return false;
22150 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22153 /* Schedule the generation of the generic parameter dies for the
22154 instance of generic type T. The proper generation itself is later
22155 done by gen_scheduled_generic_parms_dies. */
22157 static void
22158 schedule_generic_params_dies_gen (tree t)
22160 if (!generic_type_p (t))
22161 return;
22163 gcc_assert (early_dwarf);
22165 if (!generic_type_instances)
22166 vec_alloc (generic_type_instances, 256);
22168 vec_safe_push (generic_type_instances, t);
22171 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22172 by append_entry_to_tmpl_value_parm_die_table. This function must
22173 be called after function DIEs have been generated. */
22175 static void
22176 gen_remaining_tmpl_value_param_die_attribute (void)
22178 if (tmpl_value_parm_die_table)
22180 unsigned i, j;
22181 die_arg_entry *e;
22183 /* We do this in two phases - first get the cases we can
22184 handle during early-finish, preserving those we cannot
22185 (containing symbolic constants where we don't yet know
22186 whether we are going to output the referenced symbols).
22187 For those we try again at late-finish. */
22188 j = 0;
22189 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22191 if (!tree_add_const_value_attribute (e->die, e->arg))
22192 (*tmpl_value_parm_die_table)[j++] = *e;
22194 tmpl_value_parm_die_table->truncate (j);
22198 /* Generate generic parameters DIEs for instances of generic types
22199 that have been previously scheduled by
22200 schedule_generic_params_dies_gen. This function must be called
22201 after all the types of the CU have been laid out. */
22203 static void
22204 gen_scheduled_generic_parms_dies (void)
22206 unsigned i;
22207 tree t;
22209 if (!generic_type_instances)
22210 return;
22212 /* We end up "recursing" into schedule_generic_params_dies_gen, so
22213 pretend this generation is part of "early dwarf" as well. */
22214 set_early_dwarf s;
22216 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22217 if (COMPLETE_TYPE_P (t))
22218 gen_generic_params_dies (t);
22220 generic_type_instances = NULL;
22224 /* Replace DW_AT_name for the decl with name. */
22226 static void
22227 dwarf2out_set_name (tree decl, tree name)
22229 dw_die_ref die;
22230 dw_attr_node *attr;
22231 const char *dname;
22233 die = TYPE_SYMTAB_DIE (decl);
22234 if (!die)
22235 return;
22237 dname = dwarf2_name (name, 0);
22238 if (!dname)
22239 return;
22241 attr = get_AT (die, DW_AT_name);
22242 if (attr)
22244 struct indirect_string_node *node;
22246 node = find_AT_string (dname);
22247 /* replace the string. */
22248 attr->dw_attr_val.v.val_str = node;
22251 else
22252 add_name_attribute (die, dname);
22255 /* True if before or during processing of the first function being emitted. */
22256 static bool in_first_function_p = true;
22257 /* True if loc_note during dwarf2out_var_location call might still be
22258 before first real instruction at address equal to .Ltext0. */
22259 static bool maybe_at_text_label_p = true;
22260 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22261 static unsigned int first_loclabel_num_not_at_text_label;
22263 /* Called by the final INSN scan whenever we see a var location. We
22264 use it to drop labels in the right places, and throw the location in
22265 our lookup table. */
22267 static void
22268 dwarf2out_var_location (rtx_insn *loc_note)
22270 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22271 struct var_loc_node *newloc;
22272 rtx_insn *next_real, *next_note;
22273 static const char *last_label;
22274 static const char *last_postcall_label;
22275 static bool last_in_cold_section_p;
22276 static rtx_insn *expected_next_loc_note;
22277 tree decl;
22278 bool var_loc_p;
22280 if (!NOTE_P (loc_note))
22282 if (CALL_P (loc_note))
22284 call_site_count++;
22285 if (SIBLING_CALL_P (loc_note))
22286 tail_call_site_count++;
22288 return;
22291 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22292 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22293 return;
22295 /* Optimize processing a large consecutive sequence of location
22296 notes so we don't spend too much time in next_real_insn. If the
22297 next insn is another location note, remember the next_real_insn
22298 calculation for next time. */
22299 next_real = cached_next_real_insn;
22300 if (next_real)
22302 if (expected_next_loc_note != loc_note)
22303 next_real = NULL;
22306 next_note = NEXT_INSN (loc_note);
22307 if (! next_note
22308 || next_note->deleted ()
22309 || ! NOTE_P (next_note)
22310 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22311 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22312 next_note = NULL;
22314 if (! next_real)
22315 next_real = next_real_insn (loc_note);
22317 if (next_note)
22319 expected_next_loc_note = next_note;
22320 cached_next_real_insn = next_real;
22322 else
22323 cached_next_real_insn = NULL;
22325 /* If there are no instructions which would be affected by this note,
22326 don't do anything. */
22327 if (var_loc_p
22328 && next_real == NULL_RTX
22329 && !NOTE_DURING_CALL_P (loc_note))
22330 return;
22332 if (next_real == NULL_RTX)
22333 next_real = get_last_insn ();
22335 /* If there were any real insns between note we processed last time
22336 and this note (or if it is the first note), clear
22337 last_{,postcall_}label so that they are not reused this time. */
22338 if (last_var_location_insn == NULL_RTX
22339 || last_var_location_insn != next_real
22340 || last_in_cold_section_p != in_cold_section_p)
22342 last_label = NULL;
22343 last_postcall_label = NULL;
22346 if (var_loc_p)
22348 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22349 newloc = add_var_loc_to_decl (decl, loc_note,
22350 NOTE_DURING_CALL_P (loc_note)
22351 ? last_postcall_label : last_label);
22352 if (newloc == NULL)
22353 return;
22355 else
22357 decl = NULL_TREE;
22358 newloc = NULL;
22361 /* If there were no real insns between note we processed last time
22362 and this note, use the label we emitted last time. Otherwise
22363 create a new label and emit it. */
22364 if (last_label == NULL)
22366 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22367 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22368 loclabel_num++;
22369 last_label = ggc_strdup (loclabel);
22370 /* See if loclabel might be equal to .Ltext0. If yes,
22371 bump first_loclabel_num_not_at_text_label. */
22372 if (!have_multiple_function_sections
22373 && in_first_function_p
22374 && maybe_at_text_label_p)
22376 static rtx_insn *last_start;
22377 rtx_insn *insn;
22378 for (insn = loc_note; insn; insn = previous_insn (insn))
22379 if (insn == last_start)
22380 break;
22381 else if (!NONDEBUG_INSN_P (insn))
22382 continue;
22383 else
22385 rtx body = PATTERN (insn);
22386 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22387 continue;
22388 /* Inline asm could occupy zero bytes. */
22389 else if (GET_CODE (body) == ASM_INPUT
22390 || asm_noperands (body) >= 0)
22391 continue;
22392 #ifdef HAVE_attr_length
22393 else if (get_attr_min_length (insn) == 0)
22394 continue;
22395 #endif
22396 else
22398 /* Assume insn has non-zero length. */
22399 maybe_at_text_label_p = false;
22400 break;
22403 if (maybe_at_text_label_p)
22405 last_start = loc_note;
22406 first_loclabel_num_not_at_text_label = loclabel_num;
22411 if (!var_loc_p)
22413 struct call_arg_loc_node *ca_loc
22414 = ggc_cleared_alloc<call_arg_loc_node> ();
22415 rtx_insn *prev = prev_real_insn (loc_note);
22416 rtx x;
22417 ca_loc->call_arg_loc_note = loc_note;
22418 ca_loc->next = NULL;
22419 ca_loc->label = last_label;
22420 gcc_assert (prev
22421 && (CALL_P (prev)
22422 || (NONJUMP_INSN_P (prev)
22423 && GET_CODE (PATTERN (prev)) == SEQUENCE
22424 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22425 if (!CALL_P (prev))
22426 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22427 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22428 x = get_call_rtx_from (PATTERN (prev));
22429 if (x)
22431 x = XEXP (XEXP (x, 0), 0);
22432 if (GET_CODE (x) == SYMBOL_REF
22433 && SYMBOL_REF_DECL (x)
22434 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22435 ca_loc->symbol_ref = x;
22437 ca_loc->block = insn_scope (prev);
22438 if (call_arg_locations)
22439 call_arg_loc_last->next = ca_loc;
22440 else
22441 call_arg_locations = ca_loc;
22442 call_arg_loc_last = ca_loc;
22444 else if (!NOTE_DURING_CALL_P (loc_note))
22445 newloc->label = last_label;
22446 else
22448 if (!last_postcall_label)
22450 sprintf (loclabel, "%s-1", last_label);
22451 last_postcall_label = ggc_strdup (loclabel);
22453 newloc->label = last_postcall_label;
22456 last_var_location_insn = next_real;
22457 last_in_cold_section_p = in_cold_section_p;
22460 /* Note in one location list that text section has changed. */
22463 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22465 var_loc_list *list = *slot;
22466 if (list->first)
22467 list->last_before_switch
22468 = list->last->next ? list->last->next : list->last;
22469 return 1;
22472 /* Note in all location lists that text section has changed. */
22474 static void
22475 var_location_switch_text_section (void)
22477 if (decl_loc_table == NULL)
22478 return;
22480 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22483 /* Create a new line number table. */
22485 static dw_line_info_table *
22486 new_line_info_table (void)
22488 dw_line_info_table *table;
22490 table = ggc_cleared_alloc<dw_line_info_table> ();
22491 table->file_num = 1;
22492 table->line_num = 1;
22493 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22495 return table;
22498 /* Lookup the "current" table into which we emit line info, so
22499 that we don't have to do it for every source line. */
22501 static void
22502 set_cur_line_info_table (section *sec)
22504 dw_line_info_table *table;
22506 if (sec == text_section)
22507 table = text_section_line_info;
22508 else if (sec == cold_text_section)
22510 table = cold_text_section_line_info;
22511 if (!table)
22513 cold_text_section_line_info = table = new_line_info_table ();
22514 table->end_label = cold_end_label;
22517 else
22519 const char *end_label;
22521 if (flag_reorder_blocks_and_partition)
22523 if (in_cold_section_p)
22524 end_label = crtl->subsections.cold_section_end_label;
22525 else
22526 end_label = crtl->subsections.hot_section_end_label;
22528 else
22530 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22531 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22532 current_function_funcdef_no);
22533 end_label = ggc_strdup (label);
22536 table = new_line_info_table ();
22537 table->end_label = end_label;
22539 vec_safe_push (separate_line_info, table);
22542 if (DWARF2_ASM_LINE_DEBUG_INFO)
22543 table->is_stmt = (cur_line_info_table
22544 ? cur_line_info_table->is_stmt
22545 : DWARF_LINE_DEFAULT_IS_STMT_START);
22546 cur_line_info_table = table;
22550 /* We need to reset the locations at the beginning of each
22551 function. We can't do this in the end_function hook, because the
22552 declarations that use the locations won't have been output when
22553 that hook is called. Also compute have_multiple_function_sections here. */
22555 static void
22556 dwarf2out_begin_function (tree fun)
22558 section *sec = function_section (fun);
22560 if (sec != text_section)
22561 have_multiple_function_sections = true;
22563 if (flag_reorder_blocks_and_partition && !cold_text_section)
22565 gcc_assert (current_function_decl == fun);
22566 cold_text_section = unlikely_text_section ();
22567 switch_to_section (cold_text_section);
22568 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22569 switch_to_section (sec);
22572 dwarf2out_note_section_used ();
22573 call_site_count = 0;
22574 tail_call_site_count = 0;
22576 set_cur_line_info_table (sec);
22579 /* Helper function of dwarf2out_end_function, called only after emitting
22580 the very first function into assembly. Check if some .debug_loc range
22581 might end with a .LVL* label that could be equal to .Ltext0.
22582 In that case we must force using absolute addresses in .debug_loc ranges,
22583 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22584 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22585 list terminator.
22586 Set have_multiple_function_sections to true in that case and
22587 terminate htab traversal. */
22590 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22592 var_loc_list *entry = *slot;
22593 struct var_loc_node *node;
22595 node = entry->first;
22596 if (node && node->next && node->next->label)
22598 unsigned int i;
22599 const char *label = node->next->label;
22600 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22602 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22604 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22605 if (strcmp (label, loclabel) == 0)
22607 have_multiple_function_sections = true;
22608 return 0;
22612 return 1;
22615 /* Hook called after emitting a function into assembly.
22616 This does something only for the very first function emitted. */
22618 static void
22619 dwarf2out_end_function (unsigned int)
22621 if (in_first_function_p
22622 && !have_multiple_function_sections
22623 && first_loclabel_num_not_at_text_label
22624 && decl_loc_table)
22625 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22626 in_first_function_p = false;
22627 maybe_at_text_label_p = false;
22630 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22631 front-ends register a translation unit even before dwarf2out_init is
22632 called. */
22633 static tree main_translation_unit = NULL_TREE;
22635 /* Hook called by front-ends after they built their main translation unit.
22636 Associate comp_unit_die to UNIT. */
22638 static void
22639 dwarf2out_register_main_translation_unit (tree unit)
22641 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22642 && main_translation_unit == NULL_TREE);
22643 main_translation_unit = unit;
22644 /* If dwarf2out_init has not been called yet, it will perform the association
22645 itself looking at main_translation_unit. */
22646 if (decl_die_table != NULL)
22647 equate_decl_number_to_die (unit, comp_unit_die ());
22650 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22652 static void
22653 push_dw_line_info_entry (dw_line_info_table *table,
22654 enum dw_line_info_opcode opcode, unsigned int val)
22656 dw_line_info_entry e;
22657 e.opcode = opcode;
22658 e.val = val;
22659 vec_safe_push (table->entries, e);
22662 /* Output a label to mark the beginning of a source code line entry
22663 and record information relating to this source line, in
22664 'line_info_table' for later output of the .debug_line section. */
22665 /* ??? The discriminator parameter ought to be unsigned. */
22667 static void
22668 dwarf2out_source_line (unsigned int line, const char *filename,
22669 int discriminator, bool is_stmt)
22671 unsigned int file_num;
22672 dw_line_info_table *table;
22674 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22675 return;
22677 /* The discriminator column was added in dwarf4. Simplify the below
22678 by simply removing it if we're not supposed to output it. */
22679 if (dwarf_version < 4 && dwarf_strict)
22680 discriminator = 0;
22682 table = cur_line_info_table;
22683 file_num = maybe_emit_file (lookup_filename (filename));
22685 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22686 the debugger has used the second (possibly duplicate) line number
22687 at the beginning of the function to mark the end of the prologue.
22688 We could eliminate any other duplicates within the function. For
22689 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22690 that second line number entry. */
22691 /* Recall that this end-of-prologue indication is *not* the same thing
22692 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22693 to which the hook corresponds, follows the last insn that was
22694 emitted by gen_prologue. What we need is to precede the first insn
22695 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22696 insn that corresponds to something the user wrote. These may be
22697 very different locations once scheduling is enabled. */
22699 if (0 && file_num == table->file_num
22700 && line == table->line_num
22701 && discriminator == table->discrim_num
22702 && is_stmt == table->is_stmt)
22703 return;
22705 switch_to_section (current_function_section ());
22707 /* If requested, emit something human-readable. */
22708 if (flag_debug_asm)
22709 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22711 if (DWARF2_ASM_LINE_DEBUG_INFO)
22713 /* Emit the .loc directive understood by GNU as. */
22714 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22715 file_num, line, is_stmt, discriminator */
22716 fputs ("\t.loc ", asm_out_file);
22717 fprint_ul (asm_out_file, file_num);
22718 putc (' ', asm_out_file);
22719 fprint_ul (asm_out_file, line);
22720 putc (' ', asm_out_file);
22721 putc ('0', asm_out_file);
22723 if (is_stmt != table->is_stmt)
22725 fputs (" is_stmt ", asm_out_file);
22726 putc (is_stmt ? '1' : '0', asm_out_file);
22728 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22730 gcc_assert (discriminator > 0);
22731 fputs (" discriminator ", asm_out_file);
22732 fprint_ul (asm_out_file, (unsigned long) discriminator);
22734 putc ('\n', asm_out_file);
22736 else
22738 unsigned int label_num = ++line_info_label_num;
22740 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22742 push_dw_line_info_entry (table, LI_set_address, label_num);
22743 if (file_num != table->file_num)
22744 push_dw_line_info_entry (table, LI_set_file, file_num);
22745 if (discriminator != table->discrim_num)
22746 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22747 if (is_stmt != table->is_stmt)
22748 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22749 push_dw_line_info_entry (table, LI_set_line, line);
22752 table->file_num = file_num;
22753 table->line_num = line;
22754 table->discrim_num = discriminator;
22755 table->is_stmt = is_stmt;
22756 table->in_use = true;
22759 /* Record the beginning of a new source file. */
22761 static void
22762 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22764 if (flag_eliminate_dwarf2_dups)
22766 /* Record the beginning of the file for break_out_includes. */
22767 dw_die_ref bincl_die;
22769 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22770 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22773 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22775 macinfo_entry e;
22776 e.code = DW_MACINFO_start_file;
22777 e.lineno = lineno;
22778 e.info = ggc_strdup (filename);
22779 vec_safe_push (macinfo_table, e);
22783 /* Record the end of a source file. */
22785 static void
22786 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22788 if (flag_eliminate_dwarf2_dups)
22789 /* Record the end of the file for break_out_includes. */
22790 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22792 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22794 macinfo_entry e;
22795 e.code = DW_MACINFO_end_file;
22796 e.lineno = lineno;
22797 e.info = NULL;
22798 vec_safe_push (macinfo_table, e);
22802 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22803 the tail part of the directive line, i.e. the part which is past the
22804 initial whitespace, #, whitespace, directive-name, whitespace part. */
22806 static void
22807 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22808 const char *buffer ATTRIBUTE_UNUSED)
22810 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22812 macinfo_entry e;
22813 /* Insert a dummy first entry to be able to optimize the whole
22814 predefined macro block using DW_MACRO_GNU_transparent_include. */
22815 if (macinfo_table->is_empty () && lineno <= 1)
22817 e.code = 0;
22818 e.lineno = 0;
22819 e.info = NULL;
22820 vec_safe_push (macinfo_table, e);
22822 e.code = DW_MACINFO_define;
22823 e.lineno = lineno;
22824 e.info = ggc_strdup (buffer);
22825 vec_safe_push (macinfo_table, e);
22829 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22830 the tail part of the directive line, i.e. the part which is past the
22831 initial whitespace, #, whitespace, directive-name, whitespace part. */
22833 static void
22834 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22835 const char *buffer ATTRIBUTE_UNUSED)
22837 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22839 macinfo_entry e;
22840 /* Insert a dummy first entry to be able to optimize the whole
22841 predefined macro block using DW_MACRO_GNU_transparent_include. */
22842 if (macinfo_table->is_empty () && lineno <= 1)
22844 e.code = 0;
22845 e.lineno = 0;
22846 e.info = NULL;
22847 vec_safe_push (macinfo_table, e);
22849 e.code = DW_MACINFO_undef;
22850 e.lineno = lineno;
22851 e.info = ggc_strdup (buffer);
22852 vec_safe_push (macinfo_table, e);
22856 /* Helpers to manipulate hash table of CUs. */
22858 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22860 static inline hashval_t hash (const macinfo_entry *);
22861 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22864 inline hashval_t
22865 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22867 return htab_hash_string (entry->info);
22870 inline bool
22871 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22872 const macinfo_entry *entry2)
22874 return !strcmp (entry1->info, entry2->info);
22877 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22879 /* Output a single .debug_macinfo entry. */
22881 static void
22882 output_macinfo_op (macinfo_entry *ref)
22884 int file_num;
22885 size_t len;
22886 struct indirect_string_node *node;
22887 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22888 struct dwarf_file_data *fd;
22890 switch (ref->code)
22892 case DW_MACINFO_start_file:
22893 fd = lookup_filename (ref->info);
22894 file_num = maybe_emit_file (fd);
22895 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22896 dw2_asm_output_data_uleb128 (ref->lineno,
22897 "Included from line number %lu",
22898 (unsigned long) ref->lineno);
22899 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22900 break;
22901 case DW_MACINFO_end_file:
22902 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22903 break;
22904 case DW_MACINFO_define:
22905 case DW_MACINFO_undef:
22906 len = strlen (ref->info) + 1;
22907 if (!dwarf_strict
22908 && len > DWARF_OFFSET_SIZE
22909 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22910 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22912 ref->code = ref->code == DW_MACINFO_define
22913 ? DW_MACRO_GNU_define_indirect
22914 : DW_MACRO_GNU_undef_indirect;
22915 output_macinfo_op (ref);
22916 return;
22918 dw2_asm_output_data (1, ref->code,
22919 ref->code == DW_MACINFO_define
22920 ? "Define macro" : "Undefine macro");
22921 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22922 (unsigned long) ref->lineno);
22923 dw2_asm_output_nstring (ref->info, -1, "The macro");
22924 break;
22925 case DW_MACRO_GNU_define_indirect:
22926 case DW_MACRO_GNU_undef_indirect:
22927 node = find_AT_string (ref->info);
22928 gcc_assert (node
22929 && ((node->form == DW_FORM_strp)
22930 || (node->form == DW_FORM_GNU_str_index)));
22931 dw2_asm_output_data (1, ref->code,
22932 ref->code == DW_MACRO_GNU_define_indirect
22933 ? "Define macro indirect"
22934 : "Undefine macro indirect");
22935 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22936 (unsigned long) ref->lineno);
22937 if (node->form == DW_FORM_strp)
22938 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22939 debug_str_section, "The macro: \"%s\"",
22940 ref->info);
22941 else
22942 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22943 ref->info);
22944 break;
22945 case DW_MACRO_GNU_transparent_include:
22946 dw2_asm_output_data (1, ref->code, "Transparent include");
22947 ASM_GENERATE_INTERNAL_LABEL (label,
22948 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22949 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22950 break;
22951 default:
22952 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22953 ASM_COMMENT_START, (unsigned long) ref->code);
22954 break;
22958 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22959 other compilation unit .debug_macinfo sections. IDX is the first
22960 index of a define/undef, return the number of ops that should be
22961 emitted in a comdat .debug_macinfo section and emit
22962 a DW_MACRO_GNU_transparent_include entry referencing it.
22963 If the define/undef entry should be emitted normally, return 0. */
22965 static unsigned
22966 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22967 macinfo_hash_type **macinfo_htab)
22969 macinfo_entry *first, *second, *cur, *inc;
22970 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22971 unsigned char checksum[16];
22972 struct md5_ctx ctx;
22973 char *grp_name, *tail;
22974 const char *base;
22975 unsigned int i, count, encoded_filename_len, linebuf_len;
22976 macinfo_entry **slot;
22978 first = &(*macinfo_table)[idx];
22979 second = &(*macinfo_table)[idx + 1];
22981 /* Optimize only if there are at least two consecutive define/undef ops,
22982 and either all of them are before first DW_MACINFO_start_file
22983 with lineno {0,1} (i.e. predefined macro block), or all of them are
22984 in some included header file. */
22985 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22986 return 0;
22987 if (vec_safe_is_empty (files))
22989 if (first->lineno > 1 || second->lineno > 1)
22990 return 0;
22992 else if (first->lineno == 0)
22993 return 0;
22995 /* Find the last define/undef entry that can be grouped together
22996 with first and at the same time compute md5 checksum of their
22997 codes, linenumbers and strings. */
22998 md5_init_ctx (&ctx);
22999 for (i = idx; macinfo_table->iterate (i, &cur); i++)
23000 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
23001 break;
23002 else if (vec_safe_is_empty (files) && cur->lineno > 1)
23003 break;
23004 else
23006 unsigned char code = cur->code;
23007 md5_process_bytes (&code, 1, &ctx);
23008 checksum_uleb128 (cur->lineno, &ctx);
23009 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
23011 md5_finish_ctx (&ctx, checksum);
23012 count = i - idx;
23014 /* From the containing include filename (if any) pick up just
23015 usable characters from its basename. */
23016 if (vec_safe_is_empty (files))
23017 base = "";
23018 else
23019 base = lbasename (files->last ().info);
23020 for (encoded_filename_len = 0, i = 0; base[i]; i++)
23021 if (ISIDNUM (base[i]) || base[i] == '.')
23022 encoded_filename_len++;
23023 /* Count . at the end. */
23024 if (encoded_filename_len)
23025 encoded_filename_len++;
23027 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
23028 linebuf_len = strlen (linebuf);
23030 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
23031 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
23032 + 16 * 2 + 1);
23033 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
23034 tail = grp_name + 4;
23035 if (encoded_filename_len)
23037 for (i = 0; base[i]; i++)
23038 if (ISIDNUM (base[i]) || base[i] == '.')
23039 *tail++ = base[i];
23040 *tail++ = '.';
23042 memcpy (tail, linebuf, linebuf_len);
23043 tail += linebuf_len;
23044 *tail++ = '.';
23045 for (i = 0; i < 16; i++)
23046 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23048 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23049 in the empty vector entry before the first define/undef. */
23050 inc = &(*macinfo_table)[idx - 1];
23051 inc->code = DW_MACRO_GNU_transparent_include;
23052 inc->lineno = 0;
23053 inc->info = ggc_strdup (grp_name);
23054 if (!*macinfo_htab)
23055 *macinfo_htab = new macinfo_hash_type (10);
23056 /* Avoid emitting duplicates. */
23057 slot = (*macinfo_htab)->find_slot (inc, INSERT);
23058 if (*slot != NULL)
23060 inc->code = 0;
23061 inc->info = NULL;
23062 /* If such an entry has been used before, just emit
23063 a DW_MACRO_GNU_transparent_include op. */
23064 inc = *slot;
23065 output_macinfo_op (inc);
23066 /* And clear all macinfo_entry in the range to avoid emitting them
23067 in the second pass. */
23068 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23070 cur->code = 0;
23071 cur->info = NULL;
23074 else
23076 *slot = inc;
23077 inc->lineno = (*macinfo_htab)->elements ();
23078 output_macinfo_op (inc);
23080 return count;
23083 /* Save any strings needed by the macinfo table in the debug str
23084 table. All strings must be collected into the table by the time
23085 index_string is called. */
23087 static void
23088 save_macinfo_strings (void)
23090 unsigned len;
23091 unsigned i;
23092 macinfo_entry *ref;
23094 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23096 switch (ref->code)
23098 /* Match the logic in output_macinfo_op to decide on
23099 indirect strings. */
23100 case DW_MACINFO_define:
23101 case DW_MACINFO_undef:
23102 len = strlen (ref->info) + 1;
23103 if (!dwarf_strict
23104 && len > DWARF_OFFSET_SIZE
23105 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23106 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23107 set_indirect_string (find_AT_string (ref->info));
23108 break;
23109 case DW_MACRO_GNU_define_indirect:
23110 case DW_MACRO_GNU_undef_indirect:
23111 set_indirect_string (find_AT_string (ref->info));
23112 break;
23113 default:
23114 break;
23119 /* Output macinfo section(s). */
23121 static void
23122 output_macinfo (void)
23124 unsigned i;
23125 unsigned long length = vec_safe_length (macinfo_table);
23126 macinfo_entry *ref;
23127 vec<macinfo_entry, va_gc> *files = NULL;
23128 macinfo_hash_type *macinfo_htab = NULL;
23130 if (! length)
23131 return;
23133 /* output_macinfo* uses these interchangeably. */
23134 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23135 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23136 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23137 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23139 /* For .debug_macro emit the section header. */
23140 if (!dwarf_strict)
23142 dw2_asm_output_data (2, 4, "DWARF macro version number");
23143 if (DWARF_OFFSET_SIZE == 8)
23144 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23145 else
23146 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23147 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23148 (!dwarf_split_debug_info ? debug_line_section_label
23149 : debug_skeleton_line_section_label),
23150 debug_line_section, NULL);
23153 /* In the first loop, it emits the primary .debug_macinfo section
23154 and after each emitted op the macinfo_entry is cleared.
23155 If a longer range of define/undef ops can be optimized using
23156 DW_MACRO_GNU_transparent_include, the
23157 DW_MACRO_GNU_transparent_include op is emitted and kept in
23158 the vector before the first define/undef in the range and the
23159 whole range of define/undef ops is not emitted and kept. */
23160 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23162 switch (ref->code)
23164 case DW_MACINFO_start_file:
23165 vec_safe_push (files, *ref);
23166 break;
23167 case DW_MACINFO_end_file:
23168 if (!vec_safe_is_empty (files))
23169 files->pop ();
23170 break;
23171 case DW_MACINFO_define:
23172 case DW_MACINFO_undef:
23173 if (!dwarf_strict
23174 && HAVE_COMDAT_GROUP
23175 && vec_safe_length (files) != 1
23176 && i > 0
23177 && i + 1 < length
23178 && (*macinfo_table)[i - 1].code == 0)
23180 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23181 if (count)
23183 i += count - 1;
23184 continue;
23187 break;
23188 case 0:
23189 /* A dummy entry may be inserted at the beginning to be able
23190 to optimize the whole block of predefined macros. */
23191 if (i == 0)
23192 continue;
23193 default:
23194 break;
23196 output_macinfo_op (ref);
23197 ref->info = NULL;
23198 ref->code = 0;
23201 if (!macinfo_htab)
23202 return;
23204 delete macinfo_htab;
23205 macinfo_htab = NULL;
23207 /* If any DW_MACRO_GNU_transparent_include were used, on those
23208 DW_MACRO_GNU_transparent_include entries terminate the
23209 current chain and switch to a new comdat .debug_macinfo
23210 section and emit the define/undef entries within it. */
23211 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23212 switch (ref->code)
23214 case 0:
23215 continue;
23216 case DW_MACRO_GNU_transparent_include:
23218 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23219 tree comdat_key = get_identifier (ref->info);
23220 /* Terminate the previous .debug_macinfo section. */
23221 dw2_asm_output_data (1, 0, "End compilation unit");
23222 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23223 SECTION_DEBUG
23224 | SECTION_LINKONCE,
23225 comdat_key);
23226 ASM_GENERATE_INTERNAL_LABEL (label,
23227 DEBUG_MACRO_SECTION_LABEL,
23228 ref->lineno);
23229 ASM_OUTPUT_LABEL (asm_out_file, label);
23230 ref->code = 0;
23231 ref->info = NULL;
23232 dw2_asm_output_data (2, 4, "DWARF macro version number");
23233 if (DWARF_OFFSET_SIZE == 8)
23234 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23235 else
23236 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23238 break;
23239 case DW_MACINFO_define:
23240 case DW_MACINFO_undef:
23241 output_macinfo_op (ref);
23242 ref->code = 0;
23243 ref->info = NULL;
23244 break;
23245 default:
23246 gcc_unreachable ();
23250 /* Set up for Dwarf output at the start of compilation. */
23252 static void
23253 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23255 /* This option is currently broken, see (PR53118 and PR46102). */
23256 if (flag_eliminate_dwarf2_dups
23257 && strstr (lang_hooks.name, "C++"))
23259 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23260 flag_eliminate_dwarf2_dups = 0;
23263 /* Allocate the file_table. */
23264 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23266 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23267 /* Allocate the decl_die_table. */
23268 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23270 /* Allocate the decl_loc_table. */
23271 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23273 /* Allocate the cached_dw_loc_list_table. */
23274 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23276 /* Allocate the initial hunk of the decl_scope_table. */
23277 vec_alloc (decl_scope_table, 256);
23279 /* Allocate the initial hunk of the abbrev_die_table. */
23280 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23281 (ABBREV_DIE_TABLE_INCREMENT);
23282 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23283 /* Zero-th entry is allocated, but unused. */
23284 abbrev_die_table_in_use = 1;
23286 /* Allocate the pubtypes and pubnames vectors. */
23287 vec_alloc (pubname_table, 32);
23288 vec_alloc (pubtype_table, 32);
23290 vec_alloc (incomplete_types, 64);
23292 vec_alloc (used_rtx_array, 32);
23294 if (!dwarf_split_debug_info)
23296 debug_info_section = get_section (DEBUG_INFO_SECTION,
23297 SECTION_DEBUG, NULL);
23298 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23299 SECTION_DEBUG, NULL);
23300 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23301 SECTION_DEBUG, NULL);
23303 else
23305 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23306 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23307 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23308 SECTION_DEBUG | SECTION_EXCLUDE,
23309 NULL);
23310 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23311 SECTION_DEBUG, NULL);
23312 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23313 SECTION_DEBUG, NULL);
23314 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23315 SECTION_DEBUG, NULL);
23316 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23317 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23319 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23320 the main .o, but the skeleton_line goes into the split off dwo. */
23321 debug_skeleton_line_section
23322 = get_section (DEBUG_DWO_LINE_SECTION,
23323 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23324 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23325 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23326 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23327 SECTION_DEBUG | SECTION_EXCLUDE,
23328 NULL);
23329 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23330 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23331 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23332 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23333 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23334 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23336 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23337 SECTION_DEBUG, NULL);
23338 debug_macinfo_section = get_section (dwarf_strict
23339 ? DEBUG_MACINFO_SECTION
23340 : DEBUG_MACRO_SECTION,
23341 DEBUG_MACRO_SECTION_FLAGS, NULL);
23342 debug_line_section = get_section (DEBUG_LINE_SECTION,
23343 SECTION_DEBUG, NULL);
23344 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23345 SECTION_DEBUG, NULL);
23346 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23347 SECTION_DEBUG, NULL);
23348 debug_str_section = get_section (DEBUG_STR_SECTION,
23349 DEBUG_STR_SECTION_FLAGS, NULL);
23350 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23351 SECTION_DEBUG, NULL);
23352 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23353 SECTION_DEBUG, NULL);
23355 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23356 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23357 DEBUG_ABBREV_SECTION_LABEL, 0);
23358 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23359 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23360 COLD_TEXT_SECTION_LABEL, 0);
23361 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23363 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23364 DEBUG_INFO_SECTION_LABEL, 0);
23365 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23366 DEBUG_LINE_SECTION_LABEL, 0);
23367 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23368 DEBUG_RANGES_SECTION_LABEL, 0);
23369 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23370 DEBUG_ADDR_SECTION_LABEL, 0);
23371 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23372 dwarf_strict
23373 ? DEBUG_MACINFO_SECTION_LABEL
23374 : DEBUG_MACRO_SECTION_LABEL, 0);
23375 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23377 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23378 vec_alloc (macinfo_table, 64);
23380 switch_to_section (text_section);
23381 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23382 #endif
23384 /* Make sure the line number table for .text always exists. */
23385 text_section_line_info = new_line_info_table ();
23386 text_section_line_info->end_label = text_end_label;
23388 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23389 cur_line_info_table = text_section_line_info;
23390 #endif
23392 /* If front-ends already registered a main translation unit but we were not
23393 ready to perform the association, do this now. */
23394 if (main_translation_unit != NULL_TREE)
23395 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23398 /* Called before compile () starts outputtting functions, variables
23399 and toplevel asms into assembly. */
23401 static void
23402 dwarf2out_assembly_start (void)
23404 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23405 && dwarf2out_do_cfi_asm ()
23406 && (!(flag_unwind_tables || flag_exceptions)
23407 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23408 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23411 /* A helper function for dwarf2out_finish called through
23412 htab_traverse. Assign a string its index. All strings must be
23413 collected into the table by the time index_string is called,
23414 because the indexing code relies on htab_traverse to traverse nodes
23415 in the same order for each run. */
23418 index_string (indirect_string_node **h, unsigned int *index)
23420 indirect_string_node *node = *h;
23422 find_string_form (node);
23423 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23425 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23426 node->index = *index;
23427 *index += 1;
23429 return 1;
23432 /* A helper function for output_indirect_strings called through
23433 htab_traverse. Output the offset to a string and update the
23434 current offset. */
23437 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23439 indirect_string_node *node = *h;
23441 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23443 /* Assert that this node has been assigned an index. */
23444 gcc_assert (node->index != NO_INDEX_ASSIGNED
23445 && node->index != NOT_INDEXED);
23446 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23447 "indexed string 0x%x: %s", node->index, node->str);
23448 *offset += strlen (node->str) + 1;
23450 return 1;
23453 /* A helper function for dwarf2out_finish called through
23454 htab_traverse. Output the indexed string. */
23457 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23459 struct indirect_string_node *node = *h;
23461 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23463 /* Assert that the strings are output in the same order as their
23464 indexes were assigned. */
23465 gcc_assert (*cur_idx == node->index);
23466 assemble_string (node->str, strlen (node->str) + 1);
23467 *cur_idx += 1;
23469 return 1;
23472 /* A helper function for dwarf2out_finish called through
23473 htab_traverse. Emit one queued .debug_str string. */
23476 output_indirect_string (indirect_string_node **h, void *)
23478 struct indirect_string_node *node = *h;
23480 node->form = find_string_form (node);
23481 if (node->form == DW_FORM_strp && node->refcount > 0)
23483 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23484 assemble_string (node->str, strlen (node->str) + 1);
23487 return 1;
23490 /* Output the indexed string table. */
23492 static void
23493 output_indirect_strings (void)
23495 switch_to_section (debug_str_section);
23496 if (!dwarf_split_debug_info)
23497 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23498 else
23500 unsigned int offset = 0;
23501 unsigned int cur_idx = 0;
23503 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23505 switch_to_section (debug_str_offsets_section);
23506 debug_str_hash->traverse_noresize
23507 <unsigned int *, output_index_string_offset> (&offset);
23508 switch_to_section (debug_str_dwo_section);
23509 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23510 (&cur_idx);
23514 /* Callback for htab_traverse to assign an index to an entry in the
23515 table, and to write that entry to the .debug_addr section. */
23518 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23520 addr_table_entry *entry = *slot;
23522 if (entry->refcount == 0)
23524 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23525 || entry->index == NOT_INDEXED);
23526 return 1;
23529 gcc_assert (entry->index == *cur_index);
23530 (*cur_index)++;
23532 switch (entry->kind)
23534 case ate_kind_rtx:
23535 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23536 "0x%x", entry->index);
23537 break;
23538 case ate_kind_rtx_dtprel:
23539 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23540 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23541 DWARF2_ADDR_SIZE,
23542 entry->addr.rtl);
23543 fputc ('\n', asm_out_file);
23544 break;
23545 case ate_kind_label:
23546 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23547 "0x%x", entry->index);
23548 break;
23549 default:
23550 gcc_unreachable ();
23552 return 1;
23555 /* Produce the .debug_addr section. */
23557 static void
23558 output_addr_table (void)
23560 unsigned int index = 0;
23561 if (addr_index_table == NULL || addr_index_table->size () == 0)
23562 return;
23564 switch_to_section (debug_addr_section);
23565 addr_index_table
23566 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23569 #if ENABLE_ASSERT_CHECKING
23570 /* Verify that all marks are clear. */
23572 static void
23573 verify_marks_clear (dw_die_ref die)
23575 dw_die_ref c;
23577 gcc_assert (! die->die_mark);
23578 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23580 #endif /* ENABLE_ASSERT_CHECKING */
23582 /* Clear the marks for a die and its children.
23583 Be cool if the mark isn't set. */
23585 static void
23586 prune_unmark_dies (dw_die_ref die)
23588 dw_die_ref c;
23590 if (die->die_mark)
23591 die->die_mark = 0;
23592 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23595 /* Given DIE that we're marking as used, find any other dies
23596 it references as attributes and mark them as used. */
23598 static void
23599 prune_unused_types_walk_attribs (dw_die_ref die)
23601 dw_attr_node *a;
23602 unsigned ix;
23604 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23606 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23608 /* A reference to another DIE.
23609 Make sure that it will get emitted.
23610 If it was broken out into a comdat group, don't follow it. */
23611 if (! AT_ref (a)->comdat_type_p
23612 || a->dw_attr == DW_AT_specification)
23613 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23615 /* Set the string's refcount to 0 so that prune_unused_types_mark
23616 accounts properly for it. */
23617 if (AT_class (a) == dw_val_class_str)
23618 a->dw_attr_val.v.val_str->refcount = 0;
23622 /* Mark the generic parameters and arguments children DIEs of DIE. */
23624 static void
23625 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23627 dw_die_ref c;
23629 if (die == NULL || die->die_child == NULL)
23630 return;
23631 c = die->die_child;
23634 if (is_template_parameter (c))
23635 prune_unused_types_mark (c, 1);
23636 c = c->die_sib;
23637 } while (c && c != die->die_child);
23640 /* Mark DIE as being used. If DOKIDS is true, then walk down
23641 to DIE's children. */
23643 static void
23644 prune_unused_types_mark (dw_die_ref die, int dokids)
23646 dw_die_ref c;
23648 if (die->die_mark == 0)
23650 /* We haven't done this node yet. Mark it as used. */
23651 die->die_mark = 1;
23652 /* If this is the DIE of a generic type instantiation,
23653 mark the children DIEs that describe its generic parms and
23654 args. */
23655 prune_unused_types_mark_generic_parms_dies (die);
23657 /* We also have to mark its parents as used.
23658 (But we don't want to mark our parent's kids due to this,
23659 unless it is a class.) */
23660 if (die->die_parent)
23661 prune_unused_types_mark (die->die_parent,
23662 class_scope_p (die->die_parent));
23664 /* Mark any referenced nodes. */
23665 prune_unused_types_walk_attribs (die);
23667 /* If this node is a specification,
23668 also mark the definition, if it exists. */
23669 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23670 prune_unused_types_mark (die->die_definition, 1);
23673 if (dokids && die->die_mark != 2)
23675 /* We need to walk the children, but haven't done so yet.
23676 Remember that we've walked the kids. */
23677 die->die_mark = 2;
23679 /* If this is an array type, we need to make sure our
23680 kids get marked, even if they're types. If we're
23681 breaking out types into comdat sections, do this
23682 for all type definitions. */
23683 if (die->die_tag == DW_TAG_array_type
23684 || (use_debug_types
23685 && is_type_die (die) && ! is_declaration_die (die)))
23686 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23687 else
23688 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23692 /* For local classes, look if any static member functions were emitted
23693 and if so, mark them. */
23695 static void
23696 prune_unused_types_walk_local_classes (dw_die_ref die)
23698 dw_die_ref c;
23700 if (die->die_mark == 2)
23701 return;
23703 switch (die->die_tag)
23705 case DW_TAG_structure_type:
23706 case DW_TAG_union_type:
23707 case DW_TAG_class_type:
23708 break;
23710 case DW_TAG_subprogram:
23711 if (!get_AT_flag (die, DW_AT_declaration)
23712 || die->die_definition != NULL)
23713 prune_unused_types_mark (die, 1);
23714 return;
23716 default:
23717 return;
23720 /* Mark children. */
23721 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23724 /* Walk the tree DIE and mark types that we actually use. */
23726 static void
23727 prune_unused_types_walk (dw_die_ref die)
23729 dw_die_ref c;
23731 /* Don't do anything if this node is already marked and
23732 children have been marked as well. */
23733 if (die->die_mark == 2)
23734 return;
23736 switch (die->die_tag)
23738 case DW_TAG_structure_type:
23739 case DW_TAG_union_type:
23740 case DW_TAG_class_type:
23741 if (die->die_perennial_p)
23742 break;
23744 for (c = die->die_parent; c; c = c->die_parent)
23745 if (c->die_tag == DW_TAG_subprogram)
23746 break;
23748 /* Finding used static member functions inside of classes
23749 is needed just for local classes, because for other classes
23750 static member function DIEs with DW_AT_specification
23751 are emitted outside of the DW_TAG_*_type. If we ever change
23752 it, we'd need to call this even for non-local classes. */
23753 if (c)
23754 prune_unused_types_walk_local_classes (die);
23756 /* It's a type node --- don't mark it. */
23757 return;
23759 case DW_TAG_const_type:
23760 case DW_TAG_packed_type:
23761 case DW_TAG_pointer_type:
23762 case DW_TAG_reference_type:
23763 case DW_TAG_rvalue_reference_type:
23764 case DW_TAG_volatile_type:
23765 case DW_TAG_typedef:
23766 case DW_TAG_array_type:
23767 case DW_TAG_interface_type:
23768 case DW_TAG_friend:
23769 case DW_TAG_variant_part:
23770 case DW_TAG_enumeration_type:
23771 case DW_TAG_subroutine_type:
23772 case DW_TAG_string_type:
23773 case DW_TAG_set_type:
23774 case DW_TAG_subrange_type:
23775 case DW_TAG_ptr_to_member_type:
23776 case DW_TAG_file_type:
23777 if (die->die_perennial_p)
23778 break;
23780 /* It's a type node --- don't mark it. */
23781 return;
23783 default:
23784 /* Mark everything else. */
23785 break;
23788 if (die->die_mark == 0)
23790 die->die_mark = 1;
23792 /* Now, mark any dies referenced from here. */
23793 prune_unused_types_walk_attribs (die);
23796 die->die_mark = 2;
23798 /* Mark children. */
23799 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23802 /* Increment the string counts on strings referred to from DIE's
23803 attributes. */
23805 static void
23806 prune_unused_types_update_strings (dw_die_ref die)
23808 dw_attr_node *a;
23809 unsigned ix;
23811 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23812 if (AT_class (a) == dw_val_class_str)
23814 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23815 s->refcount++;
23816 /* Avoid unnecessarily putting strings that are used less than
23817 twice in the hash table. */
23818 if (s->refcount
23819 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23821 indirect_string_node **slot
23822 = debug_str_hash->find_slot_with_hash (s->str,
23823 htab_hash_string (s->str),
23824 INSERT);
23825 gcc_assert (*slot == NULL);
23826 *slot = s;
23831 /* Remove from the tree DIE any dies that aren't marked. */
23833 static void
23834 prune_unused_types_prune (dw_die_ref die)
23836 dw_die_ref c;
23838 gcc_assert (die->die_mark);
23839 prune_unused_types_update_strings (die);
23841 if (! die->die_child)
23842 return;
23844 c = die->die_child;
23845 do {
23846 dw_die_ref prev = c;
23847 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23848 if (c == die->die_child)
23850 /* No marked children between 'prev' and the end of the list. */
23851 if (prev == c)
23852 /* No marked children at all. */
23853 die->die_child = NULL;
23854 else
23856 prev->die_sib = c->die_sib;
23857 die->die_child = prev;
23859 return;
23862 if (c != prev->die_sib)
23863 prev->die_sib = c;
23864 prune_unused_types_prune (c);
23865 } while (c != die->die_child);
23868 /* Remove dies representing declarations that we never use. */
23870 static void
23871 prune_unused_types (void)
23873 unsigned int i;
23874 limbo_die_node *node;
23875 comdat_type_node *ctnode;
23876 pubname_entry *pub;
23877 dw_die_ref base_type;
23879 #if ENABLE_ASSERT_CHECKING
23880 /* All the marks should already be clear. */
23881 verify_marks_clear (comp_unit_die ());
23882 for (node = limbo_die_list; node; node = node->next)
23883 verify_marks_clear (node->die);
23884 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23885 verify_marks_clear (ctnode->root_die);
23886 #endif /* ENABLE_ASSERT_CHECKING */
23888 /* Mark types that are used in global variables. */
23889 premark_types_used_by_global_vars ();
23891 /* Set the mark on nodes that are actually used. */
23892 prune_unused_types_walk (comp_unit_die ());
23893 for (node = limbo_die_list; node; node = node->next)
23894 prune_unused_types_walk (node->die);
23895 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23897 prune_unused_types_walk (ctnode->root_die);
23898 prune_unused_types_mark (ctnode->type_die, 1);
23901 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23902 are unusual in that they are pubnames that are the children of pubtypes.
23903 They should only be marked via their parent DW_TAG_enumeration_type die,
23904 not as roots in themselves. */
23905 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23906 if (pub->die->die_tag != DW_TAG_enumerator)
23907 prune_unused_types_mark (pub->die, 1);
23908 for (i = 0; base_types.iterate (i, &base_type); i++)
23909 prune_unused_types_mark (base_type, 1);
23911 if (debug_str_hash)
23912 debug_str_hash->empty ();
23913 if (skeleton_debug_str_hash)
23914 skeleton_debug_str_hash->empty ();
23915 prune_unused_types_prune (comp_unit_die ());
23916 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23918 node = *pnode;
23919 if (!node->die->die_mark)
23920 *pnode = node->next;
23921 else
23923 prune_unused_types_prune (node->die);
23924 pnode = &node->next;
23927 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23928 prune_unused_types_prune (ctnode->root_die);
23930 /* Leave the marks clear. */
23931 prune_unmark_dies (comp_unit_die ());
23932 for (node = limbo_die_list; node; node = node->next)
23933 prune_unmark_dies (node->die);
23934 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23935 prune_unmark_dies (ctnode->root_die);
23938 /* Set the parameter to true if there are any relative pathnames in
23939 the file table. */
23941 file_table_relative_p (dwarf_file_data **slot, bool *p)
23943 struct dwarf_file_data *d = *slot;
23944 if (!IS_ABSOLUTE_PATH (d->filename))
23946 *p = true;
23947 return 0;
23949 return 1;
23952 /* Helpers to manipulate hash table of comdat type units. */
23954 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23956 static inline hashval_t hash (const comdat_type_node *);
23957 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23960 inline hashval_t
23961 comdat_type_hasher::hash (const comdat_type_node *type_node)
23963 hashval_t h;
23964 memcpy (&h, type_node->signature, sizeof (h));
23965 return h;
23968 inline bool
23969 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23970 const comdat_type_node *type_node_2)
23972 return (! memcmp (type_node_1->signature, type_node_2->signature,
23973 DWARF_TYPE_SIGNATURE_SIZE));
23976 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23977 to the location it would have been added, should we know its
23978 DECL_ASSEMBLER_NAME when we added other attributes. This will
23979 probably improve compactness of debug info, removing equivalent
23980 abbrevs, and hide any differences caused by deferring the
23981 computation of the assembler name, triggered by e.g. PCH. */
23983 static inline void
23984 move_linkage_attr (dw_die_ref die)
23986 unsigned ix = vec_safe_length (die->die_attr);
23987 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23989 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23990 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23992 while (--ix > 0)
23994 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23996 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23997 break;
24000 if (ix != vec_safe_length (die->die_attr) - 1)
24002 die->die_attr->pop ();
24003 die->die_attr->quick_insert (ix, linkage);
24007 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
24008 referenced from typed stack ops and count how often they are used. */
24010 static void
24011 mark_base_types (dw_loc_descr_ref loc)
24013 dw_die_ref base_type = NULL;
24015 for (; loc; loc = loc->dw_loc_next)
24017 switch (loc->dw_loc_opc)
24019 case DW_OP_GNU_regval_type:
24020 case DW_OP_GNU_deref_type:
24021 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24022 break;
24023 case DW_OP_GNU_convert:
24024 case DW_OP_GNU_reinterpret:
24025 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
24026 continue;
24027 /* FALLTHRU */
24028 case DW_OP_GNU_const_type:
24029 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24030 break;
24031 case DW_OP_GNU_entry_value:
24032 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24033 continue;
24034 default:
24035 continue;
24037 gcc_assert (base_type->die_parent == comp_unit_die ());
24038 if (base_type->die_mark)
24039 base_type->die_mark++;
24040 else
24042 base_types.safe_push (base_type);
24043 base_type->die_mark = 1;
24048 /* Comparison function for sorting marked base types. */
24050 static int
24051 base_type_cmp (const void *x, const void *y)
24053 dw_die_ref dx = *(const dw_die_ref *) x;
24054 dw_die_ref dy = *(const dw_die_ref *) y;
24055 unsigned int byte_size1, byte_size2;
24056 unsigned int encoding1, encoding2;
24057 if (dx->die_mark > dy->die_mark)
24058 return -1;
24059 if (dx->die_mark < dy->die_mark)
24060 return 1;
24061 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24062 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24063 if (byte_size1 < byte_size2)
24064 return 1;
24065 if (byte_size1 > byte_size2)
24066 return -1;
24067 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24068 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24069 if (encoding1 < encoding2)
24070 return 1;
24071 if (encoding1 > encoding2)
24072 return -1;
24073 return 0;
24076 /* Move base types marked by mark_base_types as early as possible
24077 in the CU, sorted by decreasing usage count both to make the
24078 uleb128 references as small as possible and to make sure they
24079 will have die_offset already computed by calc_die_sizes when
24080 sizes of typed stack loc ops is computed. */
24082 static void
24083 move_marked_base_types (void)
24085 unsigned int i;
24086 dw_die_ref base_type, die, c;
24088 if (base_types.is_empty ())
24089 return;
24091 /* Sort by decreasing usage count, they will be added again in that
24092 order later on. */
24093 base_types.qsort (base_type_cmp);
24094 die = comp_unit_die ();
24095 c = die->die_child;
24098 dw_die_ref prev = c;
24099 c = c->die_sib;
24100 while (c->die_mark)
24102 remove_child_with_prev (c, prev);
24103 /* As base types got marked, there must be at least
24104 one node other than DW_TAG_base_type. */
24105 gcc_assert (c != c->die_sib);
24106 c = c->die_sib;
24109 while (c != die->die_child);
24110 gcc_assert (die->die_child);
24111 c = die->die_child;
24112 for (i = 0; base_types.iterate (i, &base_type); i++)
24114 base_type->die_mark = 0;
24115 base_type->die_sib = c->die_sib;
24116 c->die_sib = base_type;
24117 c = base_type;
24121 /* Helper function for resolve_addr, attempt to resolve
24122 one CONST_STRING, return true if successful. Similarly verify that
24123 SYMBOL_REFs refer to variables emitted in the current CU. */
24125 static bool
24126 resolve_one_addr (rtx *addr)
24128 rtx rtl = *addr;
24130 if (GET_CODE (rtl) == CONST_STRING)
24132 size_t len = strlen (XSTR (rtl, 0)) + 1;
24133 tree t = build_string (len, XSTR (rtl, 0));
24134 tree tlen = size_int (len - 1);
24135 TREE_TYPE (t)
24136 = build_array_type (char_type_node, build_index_type (tlen));
24137 rtl = lookup_constant_def (t);
24138 if (!rtl || !MEM_P (rtl))
24139 return false;
24140 rtl = XEXP (rtl, 0);
24141 if (GET_CODE (rtl) == SYMBOL_REF
24142 && SYMBOL_REF_DECL (rtl)
24143 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24144 return false;
24145 vec_safe_push (used_rtx_array, rtl);
24146 *addr = rtl;
24147 return true;
24150 if (GET_CODE (rtl) == SYMBOL_REF
24151 && SYMBOL_REF_DECL (rtl))
24153 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24155 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24156 return false;
24158 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24159 return false;
24162 if (GET_CODE (rtl) == CONST)
24164 subrtx_ptr_iterator::array_type array;
24165 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24166 if (!resolve_one_addr (*iter))
24167 return false;
24170 return true;
24173 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24174 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24175 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24177 static rtx
24178 string_cst_pool_decl (tree t)
24180 rtx rtl = output_constant_def (t, 1);
24181 unsigned char *array;
24182 dw_loc_descr_ref l;
24183 tree decl;
24184 size_t len;
24185 dw_die_ref ref;
24187 if (!rtl || !MEM_P (rtl))
24188 return NULL_RTX;
24189 rtl = XEXP (rtl, 0);
24190 if (GET_CODE (rtl) != SYMBOL_REF
24191 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24192 return NULL_RTX;
24194 decl = SYMBOL_REF_DECL (rtl);
24195 if (!lookup_decl_die (decl))
24197 len = TREE_STRING_LENGTH (t);
24198 vec_safe_push (used_rtx_array, rtl);
24199 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24200 array = ggc_vec_alloc<unsigned char> (len);
24201 memcpy (array, TREE_STRING_POINTER (t), len);
24202 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24203 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24204 l->dw_loc_oprnd2.v.val_vec.length = len;
24205 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24206 l->dw_loc_oprnd2.v.val_vec.array = array;
24207 add_AT_loc (ref, DW_AT_location, l);
24208 equate_decl_number_to_die (decl, ref);
24210 return rtl;
24213 /* Helper function of resolve_addr_in_expr. LOC is
24214 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24215 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24216 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24217 with DW_OP_GNU_implicit_pointer if possible
24218 and return true, if unsuccessful, return false. */
24220 static bool
24221 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24223 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24224 HOST_WIDE_INT offset = 0;
24225 dw_die_ref ref = NULL;
24226 tree decl;
24228 if (GET_CODE (rtl) == CONST
24229 && GET_CODE (XEXP (rtl, 0)) == PLUS
24230 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24232 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24233 rtl = XEXP (XEXP (rtl, 0), 0);
24235 if (GET_CODE (rtl) == CONST_STRING)
24237 size_t len = strlen (XSTR (rtl, 0)) + 1;
24238 tree t = build_string (len, XSTR (rtl, 0));
24239 tree tlen = size_int (len - 1);
24241 TREE_TYPE (t)
24242 = build_array_type (char_type_node, build_index_type (tlen));
24243 rtl = string_cst_pool_decl (t);
24244 if (!rtl)
24245 return false;
24247 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24249 decl = SYMBOL_REF_DECL (rtl);
24250 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24252 ref = lookup_decl_die (decl);
24253 if (ref && (get_AT (ref, DW_AT_location)
24254 || get_AT (ref, DW_AT_const_value)))
24256 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24257 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24258 loc->dw_loc_oprnd1.val_entry = NULL;
24259 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24260 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24261 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24262 loc->dw_loc_oprnd2.v.val_int = offset;
24263 return true;
24267 return false;
24270 /* Helper function for resolve_addr, handle one location
24271 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24272 the location list couldn't be resolved. */
24274 static bool
24275 resolve_addr_in_expr (dw_loc_descr_ref loc)
24277 dw_loc_descr_ref keep = NULL;
24278 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24279 switch (loc->dw_loc_opc)
24281 case DW_OP_addr:
24282 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24284 if ((prev == NULL
24285 || prev->dw_loc_opc == DW_OP_piece
24286 || prev->dw_loc_opc == DW_OP_bit_piece)
24287 && loc->dw_loc_next
24288 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24289 && !dwarf_strict
24290 && optimize_one_addr_into_implicit_ptr (loc))
24291 break;
24292 return false;
24294 break;
24295 case DW_OP_GNU_addr_index:
24296 case DW_OP_GNU_const_index:
24297 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24298 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24300 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24301 if (!resolve_one_addr (&rtl))
24302 return false;
24303 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24304 loc->dw_loc_oprnd1.val_entry =
24305 add_addr_table_entry (rtl, ate_kind_rtx);
24307 break;
24308 case DW_OP_const4u:
24309 case DW_OP_const8u:
24310 if (loc->dtprel
24311 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24312 return false;
24313 break;
24314 case DW_OP_plus_uconst:
24315 if (size_of_loc_descr (loc)
24316 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24318 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24320 dw_loc_descr_ref repl
24321 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24322 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24323 add_loc_descr (&repl, loc->dw_loc_next);
24324 *loc = *repl;
24326 break;
24327 case DW_OP_implicit_value:
24328 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24329 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24330 return false;
24331 break;
24332 case DW_OP_GNU_implicit_pointer:
24333 case DW_OP_GNU_parameter_ref:
24334 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24336 dw_die_ref ref
24337 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24338 if (ref == NULL)
24339 return false;
24340 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24341 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24342 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24344 break;
24345 case DW_OP_GNU_const_type:
24346 case DW_OP_GNU_regval_type:
24347 case DW_OP_GNU_deref_type:
24348 case DW_OP_GNU_convert:
24349 case DW_OP_GNU_reinterpret:
24350 while (loc->dw_loc_next
24351 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24353 dw_die_ref base1, base2;
24354 unsigned enc1, enc2, size1, size2;
24355 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24356 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24357 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24358 else if (loc->dw_loc_oprnd1.val_class
24359 == dw_val_class_unsigned_const)
24360 break;
24361 else
24362 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24363 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24364 == dw_val_class_unsigned_const)
24365 break;
24366 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24367 gcc_assert (base1->die_tag == DW_TAG_base_type
24368 && base2->die_tag == DW_TAG_base_type);
24369 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24370 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24371 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24372 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24373 if (size1 == size2
24374 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24375 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24376 && loc != keep)
24377 || enc1 == enc2))
24379 /* Optimize away next DW_OP_GNU_convert after
24380 adjusting LOC's base type die reference. */
24381 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24382 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24383 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24384 else
24385 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24386 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24387 continue;
24389 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24390 point typed stack entry. */
24391 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24392 keep = loc->dw_loc_next;
24393 break;
24395 break;
24396 default:
24397 break;
24399 return true;
24402 /* Helper function of resolve_addr. DIE had DW_AT_location of
24403 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24404 and DW_OP_addr couldn't be resolved. resolve_addr has already
24405 removed the DW_AT_location attribute. This function attempts to
24406 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24407 to it or DW_AT_const_value attribute, if possible. */
24409 static void
24410 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24412 if (TREE_CODE (decl) != VAR_DECL
24413 || lookup_decl_die (decl) != die
24414 || DECL_EXTERNAL (decl)
24415 || !TREE_STATIC (decl)
24416 || DECL_INITIAL (decl) == NULL_TREE
24417 || DECL_P (DECL_INITIAL (decl))
24418 || get_AT (die, DW_AT_const_value))
24419 return;
24421 tree init = DECL_INITIAL (decl);
24422 HOST_WIDE_INT offset = 0;
24423 /* For variables that have been optimized away and thus
24424 don't have a memory location, see if we can emit
24425 DW_AT_const_value instead. */
24426 if (tree_add_const_value_attribute (die, init))
24427 return;
24428 if (dwarf_strict)
24429 return;
24430 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24431 and ADDR_EXPR refers to a decl that has DW_AT_location or
24432 DW_AT_const_value (but isn't addressable, otherwise
24433 resolving the original DW_OP_addr wouldn't fail), see if
24434 we can add DW_OP_GNU_implicit_pointer. */
24435 STRIP_NOPS (init);
24436 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24437 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24439 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24440 init = TREE_OPERAND (init, 0);
24441 STRIP_NOPS (init);
24443 if (TREE_CODE (init) != ADDR_EXPR)
24444 return;
24445 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24446 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24447 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24448 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24449 && TREE_OPERAND (init, 0) != decl))
24451 dw_die_ref ref;
24452 dw_loc_descr_ref l;
24454 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24456 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24457 if (!rtl)
24458 return;
24459 decl = SYMBOL_REF_DECL (rtl);
24461 else
24462 decl = TREE_OPERAND (init, 0);
24463 ref = lookup_decl_die (decl);
24464 if (ref == NULL
24465 || (!get_AT (ref, DW_AT_location)
24466 && !get_AT (ref, DW_AT_const_value)))
24467 return;
24468 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24469 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24470 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24471 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24472 add_AT_loc (die, DW_AT_location, l);
24476 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24477 an address in .rodata section if the string literal is emitted there,
24478 or remove the containing location list or replace DW_AT_const_value
24479 with DW_AT_location and empty location expression, if it isn't found
24480 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24481 to something that has been emitted in the current CU. */
24483 static void
24484 resolve_addr (dw_die_ref die)
24486 dw_die_ref c;
24487 dw_attr_node *a;
24488 dw_loc_list_ref *curr, *start, loc;
24489 unsigned ix;
24491 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24492 switch (AT_class (a))
24494 case dw_val_class_loc_list:
24495 start = curr = AT_loc_list_ptr (a);
24496 loc = *curr;
24497 gcc_assert (loc);
24498 /* The same list can be referenced more than once. See if we have
24499 already recorded the result from a previous pass. */
24500 if (loc->replaced)
24501 *curr = loc->dw_loc_next;
24502 else if (!loc->resolved_addr)
24504 /* As things stand, we do not expect or allow one die to
24505 reference a suffix of another die's location list chain.
24506 References must be identical or completely separate.
24507 There is therefore no need to cache the result of this
24508 pass on any list other than the first; doing so
24509 would lead to unnecessary writes. */
24510 while (*curr)
24512 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24513 if (!resolve_addr_in_expr ((*curr)->expr))
24515 dw_loc_list_ref next = (*curr)->dw_loc_next;
24516 dw_loc_descr_ref l = (*curr)->expr;
24518 if (next && (*curr)->ll_symbol)
24520 gcc_assert (!next->ll_symbol);
24521 next->ll_symbol = (*curr)->ll_symbol;
24523 if (dwarf_split_debug_info)
24524 remove_loc_list_addr_table_entries (l);
24525 *curr = next;
24527 else
24529 mark_base_types ((*curr)->expr);
24530 curr = &(*curr)->dw_loc_next;
24533 if (loc == *start)
24534 loc->resolved_addr = 1;
24535 else
24537 loc->replaced = 1;
24538 loc->dw_loc_next = *start;
24541 if (!*start)
24543 remove_AT (die, a->dw_attr);
24544 ix--;
24546 break;
24547 case dw_val_class_loc:
24549 dw_loc_descr_ref l = AT_loc (a);
24550 /* For -gdwarf-2 don't attempt to optimize
24551 DW_AT_data_member_location containing
24552 DW_OP_plus_uconst - older consumers might
24553 rely on it being that op instead of a more complex,
24554 but shorter, location description. */
24555 if ((dwarf_version > 2
24556 || a->dw_attr != DW_AT_data_member_location
24557 || l == NULL
24558 || l->dw_loc_opc != DW_OP_plus_uconst
24559 || l->dw_loc_next != NULL)
24560 && !resolve_addr_in_expr (l))
24562 if (dwarf_split_debug_info)
24563 remove_loc_list_addr_table_entries (l);
24564 if (l != NULL
24565 && l->dw_loc_next == NULL
24566 && l->dw_loc_opc == DW_OP_addr
24567 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24568 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24569 && a->dw_attr == DW_AT_location)
24571 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24572 remove_AT (die, a->dw_attr);
24573 ix--;
24574 optimize_location_into_implicit_ptr (die, decl);
24575 break;
24577 remove_AT (die, a->dw_attr);
24578 ix--;
24580 else
24581 mark_base_types (l);
24583 break;
24584 case dw_val_class_addr:
24585 if (a->dw_attr == DW_AT_const_value
24586 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24588 if (AT_index (a) != NOT_INDEXED)
24589 remove_addr_table_entry (a->dw_attr_val.val_entry);
24590 remove_AT (die, a->dw_attr);
24591 ix--;
24593 if (die->die_tag == DW_TAG_GNU_call_site
24594 && a->dw_attr == DW_AT_abstract_origin)
24596 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24597 dw_die_ref tdie = lookup_decl_die (tdecl);
24598 dw_die_ref cdie;
24599 if (tdie == NULL
24600 && DECL_EXTERNAL (tdecl)
24601 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24602 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24604 /* Creating a full DIE for tdecl is overly expensive and
24605 at this point even wrong when in the LTO phase
24606 as it can end up generating new type DIEs we didn't
24607 output and thus optimize_external_refs will crash. */
24608 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24609 add_AT_flag (tdie, DW_AT_external, 1);
24610 add_AT_flag (tdie, DW_AT_declaration, 1);
24611 add_linkage_attr (tdie, tdecl);
24612 add_name_and_src_coords_attributes (tdie, tdecl);
24613 equate_decl_number_to_die (tdecl, tdie);
24615 if (tdie)
24617 a->dw_attr_val.val_class = dw_val_class_die_ref;
24618 a->dw_attr_val.v.val_die_ref.die = tdie;
24619 a->dw_attr_val.v.val_die_ref.external = 0;
24621 else
24623 if (AT_index (a) != NOT_INDEXED)
24624 remove_addr_table_entry (a->dw_attr_val.val_entry);
24625 remove_AT (die, a->dw_attr);
24626 ix--;
24629 break;
24630 default:
24631 break;
24634 FOR_EACH_CHILD (die, c, resolve_addr (c));
24637 /* Helper routines for optimize_location_lists.
24638 This pass tries to share identical local lists in .debug_loc
24639 section. */
24641 /* Iteratively hash operands of LOC opcode into HSTATE. */
24643 static void
24644 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24646 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24647 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24649 switch (loc->dw_loc_opc)
24651 case DW_OP_const4u:
24652 case DW_OP_const8u:
24653 if (loc->dtprel)
24654 goto hash_addr;
24655 /* FALLTHRU */
24656 case DW_OP_const1u:
24657 case DW_OP_const1s:
24658 case DW_OP_const2u:
24659 case DW_OP_const2s:
24660 case DW_OP_const4s:
24661 case DW_OP_const8s:
24662 case DW_OP_constu:
24663 case DW_OP_consts:
24664 case DW_OP_pick:
24665 case DW_OP_plus_uconst:
24666 case DW_OP_breg0:
24667 case DW_OP_breg1:
24668 case DW_OP_breg2:
24669 case DW_OP_breg3:
24670 case DW_OP_breg4:
24671 case DW_OP_breg5:
24672 case DW_OP_breg6:
24673 case DW_OP_breg7:
24674 case DW_OP_breg8:
24675 case DW_OP_breg9:
24676 case DW_OP_breg10:
24677 case DW_OP_breg11:
24678 case DW_OP_breg12:
24679 case DW_OP_breg13:
24680 case DW_OP_breg14:
24681 case DW_OP_breg15:
24682 case DW_OP_breg16:
24683 case DW_OP_breg17:
24684 case DW_OP_breg18:
24685 case DW_OP_breg19:
24686 case DW_OP_breg20:
24687 case DW_OP_breg21:
24688 case DW_OP_breg22:
24689 case DW_OP_breg23:
24690 case DW_OP_breg24:
24691 case DW_OP_breg25:
24692 case DW_OP_breg26:
24693 case DW_OP_breg27:
24694 case DW_OP_breg28:
24695 case DW_OP_breg29:
24696 case DW_OP_breg30:
24697 case DW_OP_breg31:
24698 case DW_OP_regx:
24699 case DW_OP_fbreg:
24700 case DW_OP_piece:
24701 case DW_OP_deref_size:
24702 case DW_OP_xderef_size:
24703 hstate.add_object (val1->v.val_int);
24704 break;
24705 case DW_OP_skip:
24706 case DW_OP_bra:
24708 int offset;
24710 gcc_assert (val1->val_class == dw_val_class_loc);
24711 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24712 hstate.add_object (offset);
24714 break;
24715 case DW_OP_implicit_value:
24716 hstate.add_object (val1->v.val_unsigned);
24717 switch (val2->val_class)
24719 case dw_val_class_const:
24720 hstate.add_object (val2->v.val_int);
24721 break;
24722 case dw_val_class_vec:
24724 unsigned int elt_size = val2->v.val_vec.elt_size;
24725 unsigned int len = val2->v.val_vec.length;
24727 hstate.add_int (elt_size);
24728 hstate.add_int (len);
24729 hstate.add (val2->v.val_vec.array, len * elt_size);
24731 break;
24732 case dw_val_class_const_double:
24733 hstate.add_object (val2->v.val_double.low);
24734 hstate.add_object (val2->v.val_double.high);
24735 break;
24736 case dw_val_class_wide_int:
24737 hstate.add (val2->v.val_wide->get_val (),
24738 get_full_len (*val2->v.val_wide)
24739 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24740 break;
24741 case dw_val_class_addr:
24742 inchash::add_rtx (val2->v.val_addr, hstate);
24743 break;
24744 default:
24745 gcc_unreachable ();
24747 break;
24748 case DW_OP_bregx:
24749 case DW_OP_bit_piece:
24750 hstate.add_object (val1->v.val_int);
24751 hstate.add_object (val2->v.val_int);
24752 break;
24753 case DW_OP_addr:
24754 hash_addr:
24755 if (loc->dtprel)
24757 unsigned char dtprel = 0xd1;
24758 hstate.add_object (dtprel);
24760 inchash::add_rtx (val1->v.val_addr, hstate);
24761 break;
24762 case DW_OP_GNU_addr_index:
24763 case DW_OP_GNU_const_index:
24765 if (loc->dtprel)
24767 unsigned char dtprel = 0xd1;
24768 hstate.add_object (dtprel);
24770 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24772 break;
24773 case DW_OP_GNU_implicit_pointer:
24774 hstate.add_int (val2->v.val_int);
24775 break;
24776 case DW_OP_GNU_entry_value:
24777 hstate.add_object (val1->v.val_loc);
24778 break;
24779 case DW_OP_GNU_regval_type:
24780 case DW_OP_GNU_deref_type:
24782 unsigned int byte_size
24783 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24784 unsigned int encoding
24785 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24786 hstate.add_object (val1->v.val_int);
24787 hstate.add_object (byte_size);
24788 hstate.add_object (encoding);
24790 break;
24791 case DW_OP_GNU_convert:
24792 case DW_OP_GNU_reinterpret:
24793 if (val1->val_class == dw_val_class_unsigned_const)
24795 hstate.add_object (val1->v.val_unsigned);
24796 break;
24798 /* FALLTHRU */
24799 case DW_OP_GNU_const_type:
24801 unsigned int byte_size
24802 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24803 unsigned int encoding
24804 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24805 hstate.add_object (byte_size);
24806 hstate.add_object (encoding);
24807 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24808 break;
24809 hstate.add_object (val2->val_class);
24810 switch (val2->val_class)
24812 case dw_val_class_const:
24813 hstate.add_object (val2->v.val_int);
24814 break;
24815 case dw_val_class_vec:
24817 unsigned int elt_size = val2->v.val_vec.elt_size;
24818 unsigned int len = val2->v.val_vec.length;
24820 hstate.add_object (elt_size);
24821 hstate.add_object (len);
24822 hstate.add (val2->v.val_vec.array, len * elt_size);
24824 break;
24825 case dw_val_class_const_double:
24826 hstate.add_object (val2->v.val_double.low);
24827 hstate.add_object (val2->v.val_double.high);
24828 break;
24829 case dw_val_class_wide_int:
24830 hstate.add (val2->v.val_wide->get_val (),
24831 get_full_len (*val2->v.val_wide)
24832 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24833 break;
24834 default:
24835 gcc_unreachable ();
24838 break;
24840 default:
24841 /* Other codes have no operands. */
24842 break;
24846 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24848 static inline void
24849 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24851 dw_loc_descr_ref l;
24852 bool sizes_computed = false;
24853 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24854 size_of_locs (loc);
24856 for (l = loc; l != NULL; l = l->dw_loc_next)
24858 enum dwarf_location_atom opc = l->dw_loc_opc;
24859 hstate.add_object (opc);
24860 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24862 size_of_locs (loc);
24863 sizes_computed = true;
24865 hash_loc_operands (l, hstate);
24869 /* Compute hash of the whole location list LIST_HEAD. */
24871 static inline void
24872 hash_loc_list (dw_loc_list_ref list_head)
24874 dw_loc_list_ref curr = list_head;
24875 inchash::hash hstate;
24877 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24879 hstate.add (curr->begin, strlen (curr->begin) + 1);
24880 hstate.add (curr->end, strlen (curr->end) + 1);
24881 if (curr->section)
24882 hstate.add (curr->section, strlen (curr->section) + 1);
24883 hash_locs (curr->expr, hstate);
24885 list_head->hash = hstate.end ();
24888 /* Return true if X and Y opcodes have the same operands. */
24890 static inline bool
24891 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24893 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24894 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24895 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24896 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24898 switch (x->dw_loc_opc)
24900 case DW_OP_const4u:
24901 case DW_OP_const8u:
24902 if (x->dtprel)
24903 goto hash_addr;
24904 /* FALLTHRU */
24905 case DW_OP_const1u:
24906 case DW_OP_const1s:
24907 case DW_OP_const2u:
24908 case DW_OP_const2s:
24909 case DW_OP_const4s:
24910 case DW_OP_const8s:
24911 case DW_OP_constu:
24912 case DW_OP_consts:
24913 case DW_OP_pick:
24914 case DW_OP_plus_uconst:
24915 case DW_OP_breg0:
24916 case DW_OP_breg1:
24917 case DW_OP_breg2:
24918 case DW_OP_breg3:
24919 case DW_OP_breg4:
24920 case DW_OP_breg5:
24921 case DW_OP_breg6:
24922 case DW_OP_breg7:
24923 case DW_OP_breg8:
24924 case DW_OP_breg9:
24925 case DW_OP_breg10:
24926 case DW_OP_breg11:
24927 case DW_OP_breg12:
24928 case DW_OP_breg13:
24929 case DW_OP_breg14:
24930 case DW_OP_breg15:
24931 case DW_OP_breg16:
24932 case DW_OP_breg17:
24933 case DW_OP_breg18:
24934 case DW_OP_breg19:
24935 case DW_OP_breg20:
24936 case DW_OP_breg21:
24937 case DW_OP_breg22:
24938 case DW_OP_breg23:
24939 case DW_OP_breg24:
24940 case DW_OP_breg25:
24941 case DW_OP_breg26:
24942 case DW_OP_breg27:
24943 case DW_OP_breg28:
24944 case DW_OP_breg29:
24945 case DW_OP_breg30:
24946 case DW_OP_breg31:
24947 case DW_OP_regx:
24948 case DW_OP_fbreg:
24949 case DW_OP_piece:
24950 case DW_OP_deref_size:
24951 case DW_OP_xderef_size:
24952 return valx1->v.val_int == valy1->v.val_int;
24953 case DW_OP_skip:
24954 case DW_OP_bra:
24955 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24956 can cause irrelevant differences in dw_loc_addr. */
24957 gcc_assert (valx1->val_class == dw_val_class_loc
24958 && valy1->val_class == dw_val_class_loc
24959 && (dwarf_split_debug_info
24960 || x->dw_loc_addr == y->dw_loc_addr));
24961 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24962 case DW_OP_implicit_value:
24963 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24964 || valx2->val_class != valy2->val_class)
24965 return false;
24966 switch (valx2->val_class)
24968 case dw_val_class_const:
24969 return valx2->v.val_int == valy2->v.val_int;
24970 case dw_val_class_vec:
24971 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24972 && valx2->v.val_vec.length == valy2->v.val_vec.length
24973 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24974 valx2->v.val_vec.elt_size
24975 * valx2->v.val_vec.length) == 0;
24976 case dw_val_class_const_double:
24977 return valx2->v.val_double.low == valy2->v.val_double.low
24978 && valx2->v.val_double.high == valy2->v.val_double.high;
24979 case dw_val_class_wide_int:
24980 return *valx2->v.val_wide == *valy2->v.val_wide;
24981 case dw_val_class_addr:
24982 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24983 default:
24984 gcc_unreachable ();
24986 case DW_OP_bregx:
24987 case DW_OP_bit_piece:
24988 return valx1->v.val_int == valy1->v.val_int
24989 && valx2->v.val_int == valy2->v.val_int;
24990 case DW_OP_addr:
24991 hash_addr:
24992 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24993 case DW_OP_GNU_addr_index:
24994 case DW_OP_GNU_const_index:
24996 rtx ax1 = valx1->val_entry->addr.rtl;
24997 rtx ay1 = valy1->val_entry->addr.rtl;
24998 return rtx_equal_p (ax1, ay1);
25000 case DW_OP_GNU_implicit_pointer:
25001 return valx1->val_class == dw_val_class_die_ref
25002 && valx1->val_class == valy1->val_class
25003 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
25004 && valx2->v.val_int == valy2->v.val_int;
25005 case DW_OP_GNU_entry_value:
25006 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
25007 case DW_OP_GNU_const_type:
25008 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
25009 || valx2->val_class != valy2->val_class)
25010 return false;
25011 switch (valx2->val_class)
25013 case dw_val_class_const:
25014 return valx2->v.val_int == valy2->v.val_int;
25015 case dw_val_class_vec:
25016 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
25017 && valx2->v.val_vec.length == valy2->v.val_vec.length
25018 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
25019 valx2->v.val_vec.elt_size
25020 * valx2->v.val_vec.length) == 0;
25021 case dw_val_class_const_double:
25022 return valx2->v.val_double.low == valy2->v.val_double.low
25023 && valx2->v.val_double.high == valy2->v.val_double.high;
25024 case dw_val_class_wide_int:
25025 return *valx2->v.val_wide == *valy2->v.val_wide;
25026 default:
25027 gcc_unreachable ();
25029 case DW_OP_GNU_regval_type:
25030 case DW_OP_GNU_deref_type:
25031 return valx1->v.val_int == valy1->v.val_int
25032 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
25033 case DW_OP_GNU_convert:
25034 case DW_OP_GNU_reinterpret:
25035 if (valx1->val_class != valy1->val_class)
25036 return false;
25037 if (valx1->val_class == dw_val_class_unsigned_const)
25038 return valx1->v.val_unsigned == valy1->v.val_unsigned;
25039 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25040 case DW_OP_GNU_parameter_ref:
25041 return valx1->val_class == dw_val_class_die_ref
25042 && valx1->val_class == valy1->val_class
25043 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25044 default:
25045 /* Other codes have no operands. */
25046 return true;
25050 /* Return true if DWARF location expressions X and Y are the same. */
25052 static inline bool
25053 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25055 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25056 if (x->dw_loc_opc != y->dw_loc_opc
25057 || x->dtprel != y->dtprel
25058 || !compare_loc_operands (x, y))
25059 break;
25060 return x == NULL && y == NULL;
25063 /* Hashtable helpers. */
25065 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25067 static inline hashval_t hash (const dw_loc_list_struct *);
25068 static inline bool equal (const dw_loc_list_struct *,
25069 const dw_loc_list_struct *);
25072 /* Return precomputed hash of location list X. */
25074 inline hashval_t
25075 loc_list_hasher::hash (const dw_loc_list_struct *x)
25077 return x->hash;
25080 /* Return true if location lists A and B are the same. */
25082 inline bool
25083 loc_list_hasher::equal (const dw_loc_list_struct *a,
25084 const dw_loc_list_struct *b)
25086 if (a == b)
25087 return 1;
25088 if (a->hash != b->hash)
25089 return 0;
25090 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25091 if (strcmp (a->begin, b->begin) != 0
25092 || strcmp (a->end, b->end) != 0
25093 || (a->section == NULL) != (b->section == NULL)
25094 || (a->section && strcmp (a->section, b->section) != 0)
25095 || !compare_locs (a->expr, b->expr))
25096 break;
25097 return a == NULL && b == NULL;
25100 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25103 /* Recursively optimize location lists referenced from DIE
25104 children and share them whenever possible. */
25106 static void
25107 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25109 dw_die_ref c;
25110 dw_attr_node *a;
25111 unsigned ix;
25112 dw_loc_list_struct **slot;
25114 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25115 if (AT_class (a) == dw_val_class_loc_list)
25117 dw_loc_list_ref list = AT_loc_list (a);
25118 /* TODO: perform some optimizations here, before hashing
25119 it and storing into the hash table. */
25120 hash_loc_list (list);
25121 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25122 if (*slot == NULL)
25123 *slot = list;
25124 else
25125 a->dw_attr_val.v.val_loc_list = *slot;
25128 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25132 /* Recursively assign each location list a unique index into the debug_addr
25133 section. */
25135 static void
25136 index_location_lists (dw_die_ref die)
25138 dw_die_ref c;
25139 dw_attr_node *a;
25140 unsigned ix;
25142 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25143 if (AT_class (a) == dw_val_class_loc_list)
25145 dw_loc_list_ref list = AT_loc_list (a);
25146 dw_loc_list_ref curr;
25147 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25149 /* Don't index an entry that has already been indexed
25150 or won't be output. */
25151 if (curr->begin_entry != NULL
25152 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25153 continue;
25155 curr->begin_entry
25156 = add_addr_table_entry (xstrdup (curr->begin),
25157 ate_kind_label);
25161 FOR_EACH_CHILD (die, c, index_location_lists (c));
25164 /* Optimize location lists referenced from DIE
25165 children and share them whenever possible. */
25167 static void
25168 optimize_location_lists (dw_die_ref die)
25170 loc_list_hash_type htab (500);
25171 optimize_location_lists_1 (die, &htab);
25174 /* Traverse the limbo die list, and add parent/child links. The only
25175 dies without parents that should be here are concrete instances of
25176 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25177 For concrete instances, we can get the parent die from the abstract
25178 instance. */
25180 static void
25181 flush_limbo_die_list (void)
25183 limbo_die_node *node, *next_node;
25185 for (node = limbo_die_list; node; node = next_node)
25187 dw_die_ref die = node->die;
25188 next_node = node->next;
25190 if (die->die_parent == NULL)
25192 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25194 if (origin && origin->die_parent)
25195 add_child_die (origin->die_parent, die);
25196 else if (is_cu_die (die))
25198 else if (seen_error ())
25199 /* It's OK to be confused by errors in the input. */
25200 add_child_die (comp_unit_die (), die);
25201 else
25203 /* In certain situations, the lexical block containing a
25204 nested function can be optimized away, which results
25205 in the nested function die being orphaned. Likewise
25206 with the return type of that nested function. Force
25207 this to be a child of the containing function.
25209 It may happen that even the containing function got fully
25210 inlined and optimized out. In that case we are lost and
25211 assign the empty child. This should not be big issue as
25212 the function is likely unreachable too. */
25213 gcc_assert (node->created_for);
25215 if (DECL_P (node->created_for))
25216 origin = get_context_die (DECL_CONTEXT (node->created_for));
25217 else if (TYPE_P (node->created_for))
25218 origin = scope_die_for (node->created_for, comp_unit_die ());
25219 else
25220 origin = comp_unit_die ();
25222 add_child_die (origin, die);
25227 limbo_die_list = NULL;
25230 /* Output stuff that dwarf requires at the end of every file,
25231 and generate the DWARF-2 debugging info. */
25233 static void
25234 dwarf2out_finish (const char *filename)
25236 comdat_type_node *ctnode;
25237 dw_die_ref main_comp_unit_die;
25239 /* Flush out any latecomers to the limbo party. */
25240 flush_limbo_die_list ();
25242 /* We shouldn't have any symbols with delayed asm names for
25243 DIEs generated after early finish. */
25244 gcc_assert (deferred_asm_name == NULL);
25246 /* PCH might result in DW_AT_producer string being restored from the
25247 header compilation, so always fill it with empty string initially
25248 and overwrite only here. */
25249 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
25250 producer_string = gen_producer_string ();
25251 producer->dw_attr_val.v.val_str->refcount--;
25252 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25254 gen_remaining_tmpl_value_param_die_attribute ();
25256 /* Add the name for the main input file now. We delayed this from
25257 dwarf2out_init to avoid complications with PCH.
25258 For LTO produced units use a fixed artificial name to avoid
25259 leaking tempfile names into the dwarf. */
25260 if (!in_lto_p)
25261 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25262 else
25263 add_name_attribute (comp_unit_die (), "<artificial>");
25264 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25265 add_comp_dir_attribute (comp_unit_die ());
25266 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25268 bool p = false;
25269 file_table->traverse<bool *, file_table_relative_p> (&p);
25270 if (p)
25271 add_comp_dir_attribute (comp_unit_die ());
25274 #if ENABLE_ASSERT_CHECKING
25276 dw_die_ref die = comp_unit_die (), c;
25277 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25279 #endif
25280 resolve_addr (comp_unit_die ());
25281 move_marked_base_types ();
25283 /* Walk through the list of incomplete types again, trying once more to
25284 emit full debugging info for them. */
25285 retry_incomplete_types ();
25287 if (flag_eliminate_unused_debug_types)
25288 prune_unused_types ();
25290 /* Generate separate COMDAT sections for type DIEs. */
25291 if (use_debug_types)
25293 break_out_comdat_types (comp_unit_die ());
25295 /* Each new type_unit DIE was added to the limbo die list when created.
25296 Since these have all been added to comdat_type_list, clear the
25297 limbo die list. */
25298 limbo_die_list = NULL;
25300 /* For each new comdat type unit, copy declarations for incomplete
25301 types to make the new unit self-contained (i.e., no direct
25302 references to the main compile unit). */
25303 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25304 copy_decls_for_unworthy_types (ctnode->root_die);
25305 copy_decls_for_unworthy_types (comp_unit_die ());
25307 /* In the process of copying declarations from one unit to another,
25308 we may have left some declarations behind that are no longer
25309 referenced. Prune them. */
25310 prune_unused_types ();
25313 /* Generate separate CUs for each of the include files we've seen.
25314 They will go into limbo_die_list. */
25315 if (flag_eliminate_dwarf2_dups)
25316 break_out_includes (comp_unit_die ());
25318 /* Traverse the DIE's and add sibling attributes to those DIE's that
25319 have children. */
25320 add_sibling_attributes (comp_unit_die ());
25321 limbo_die_node *node;
25322 for (node = limbo_die_list; node; node = node->next)
25323 add_sibling_attributes (node->die);
25324 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25325 add_sibling_attributes (ctnode->root_die);
25327 /* When splitting DWARF info, we put some attributes in the
25328 skeleton compile_unit DIE that remains in the .o, while
25329 most attributes go in the DWO compile_unit_die. */
25330 if (dwarf_split_debug_info)
25331 main_comp_unit_die = gen_compile_unit_die (NULL);
25332 else
25333 main_comp_unit_die = comp_unit_die ();
25335 /* Output a terminator label for the .text section. */
25336 switch_to_section (text_section);
25337 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25338 if (cold_text_section)
25340 switch_to_section (cold_text_section);
25341 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25344 /* We can only use the low/high_pc attributes if all of the code was
25345 in .text. */
25346 if (!have_multiple_function_sections
25347 || (dwarf_version < 3 && dwarf_strict))
25349 /* Don't add if the CU has no associated code. */
25350 if (text_section_used)
25351 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25352 text_end_label, true);
25354 else
25356 unsigned fde_idx;
25357 dw_fde_ref fde;
25358 bool range_list_added = false;
25360 if (text_section_used)
25361 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25362 text_end_label, &range_list_added, true);
25363 if (cold_text_section_used)
25364 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25365 cold_end_label, &range_list_added, true);
25367 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25369 if (DECL_IGNORED_P (fde->decl))
25370 continue;
25371 if (!fde->in_std_section)
25372 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25373 fde->dw_fde_end, &range_list_added,
25374 true);
25375 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25376 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25377 fde->dw_fde_second_end, &range_list_added,
25378 true);
25381 if (range_list_added)
25383 /* We need to give .debug_loc and .debug_ranges an appropriate
25384 "base address". Use zero so that these addresses become
25385 absolute. Historically, we've emitted the unexpected
25386 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25387 Emit both to give time for other tools to adapt. */
25388 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25389 if (! dwarf_strict && dwarf_version < 4)
25390 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25392 add_ranges (NULL);
25396 if (debug_info_level >= DINFO_LEVEL_TERSE)
25397 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25398 debug_line_section_label);
25400 if (have_macinfo)
25401 add_AT_macptr (comp_unit_die (),
25402 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25403 macinfo_section_label);
25405 if (dwarf_split_debug_info)
25407 /* optimize_location_lists calculates the size of the lists,
25408 so index them first, and assign indices to the entries.
25409 Although optimize_location_lists will remove entries from
25410 the table, it only does so for duplicates, and therefore
25411 only reduces ref_counts to 1. */
25412 index_location_lists (comp_unit_die ());
25414 if (addr_index_table != NULL)
25416 unsigned int index = 0;
25417 addr_index_table
25418 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25419 (&index);
25423 if (have_location_lists)
25424 optimize_location_lists (comp_unit_die ());
25426 save_macinfo_strings ();
25428 if (dwarf_split_debug_info)
25430 unsigned int index = 0;
25432 /* Add attributes common to skeleton compile_units and
25433 type_units. Because these attributes include strings, it
25434 must be done before freezing the string table. Top-level
25435 skeleton die attrs are added when the skeleton type unit is
25436 created, so ensure it is created by this point. */
25437 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25438 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25441 /* Output all of the compilation units. We put the main one last so that
25442 the offsets are available to output_pubnames. */
25443 for (node = limbo_die_list; node; node = node->next)
25444 output_comp_unit (node->die, 0);
25446 hash_table<comdat_type_hasher> comdat_type_table (100);
25447 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25449 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25451 /* Don't output duplicate types. */
25452 if (*slot != HTAB_EMPTY_ENTRY)
25453 continue;
25455 /* Add a pointer to the line table for the main compilation unit
25456 so that the debugger can make sense of DW_AT_decl_file
25457 attributes. */
25458 if (debug_info_level >= DINFO_LEVEL_TERSE)
25459 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25460 (!dwarf_split_debug_info
25461 ? debug_line_section_label
25462 : debug_skeleton_line_section_label));
25464 output_comdat_type_unit (ctnode);
25465 *slot = ctnode;
25468 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25469 both the main_cu and all skeleton TUs. Making this call unconditional
25470 would end up either adding a second copy of the AT_pubnames attribute, or
25471 requiring a special case in add_top_level_skeleton_die_attrs. */
25472 if (!dwarf_split_debug_info)
25473 add_AT_pubnames (comp_unit_die ());
25475 if (dwarf_split_debug_info)
25477 int mark;
25478 unsigned char checksum[16];
25479 struct md5_ctx ctx;
25481 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25482 md5_init_ctx (&ctx);
25483 mark = 0;
25484 die_checksum (comp_unit_die (), &ctx, &mark);
25485 unmark_all_dies (comp_unit_die ());
25486 md5_finish_ctx (&ctx, checksum);
25488 /* Use the first 8 bytes of the checksum as the dwo_id,
25489 and add it to both comp-unit DIEs. */
25490 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25491 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25493 /* Add the base offset of the ranges table to the skeleton
25494 comp-unit DIE. */
25495 if (ranges_table_in_use)
25496 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25497 ranges_section_label);
25499 switch_to_section (debug_addr_section);
25500 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25501 output_addr_table ();
25504 /* Output the main compilation unit if non-empty or if .debug_macinfo
25505 or .debug_macro will be emitted. */
25506 output_comp_unit (comp_unit_die (), have_macinfo);
25508 if (dwarf_split_debug_info && info_section_emitted)
25509 output_skeleton_debug_sections (main_comp_unit_die);
25511 /* Output the abbreviation table. */
25512 if (abbrev_die_table_in_use != 1)
25514 switch_to_section (debug_abbrev_section);
25515 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25516 output_abbrev_section ();
25519 /* Output location list section if necessary. */
25520 if (have_location_lists)
25522 /* Output the location lists info. */
25523 switch_to_section (debug_loc_section);
25524 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25525 output_location_lists (comp_unit_die ());
25528 output_pubtables ();
25530 /* Output the address range information if a CU (.debug_info section)
25531 was emitted. We output an empty table even if we had no functions
25532 to put in it. This because the consumer has no way to tell the
25533 difference between an empty table that we omitted and failure to
25534 generate a table that would have contained data. */
25535 if (info_section_emitted)
25537 switch_to_section (debug_aranges_section);
25538 output_aranges ();
25541 /* Output ranges section if necessary. */
25542 if (ranges_table_in_use)
25544 switch_to_section (debug_ranges_section);
25545 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25546 output_ranges ();
25549 /* Have to end the macro section. */
25550 if (have_macinfo)
25552 switch_to_section (debug_macinfo_section);
25553 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25554 output_macinfo ();
25555 dw2_asm_output_data (1, 0, "End compilation unit");
25558 /* Output the source line correspondence table. We must do this
25559 even if there is no line information. Otherwise, on an empty
25560 translation unit, we will generate a present, but empty,
25561 .debug_info section. IRIX 6.5 `nm' will then complain when
25562 examining the file. This is done late so that any filenames
25563 used by the debug_info section are marked as 'used'. */
25564 switch_to_section (debug_line_section);
25565 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25566 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25567 output_line_info (false);
25569 if (dwarf_split_debug_info && info_section_emitted)
25571 switch_to_section (debug_skeleton_line_section);
25572 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25573 output_line_info (true);
25576 /* If we emitted any indirect strings, output the string table too. */
25577 if (debug_str_hash || skeleton_debug_str_hash)
25578 output_indirect_strings ();
25581 /* Perform any cleanups needed after the early debug generation pass
25582 has run. */
25584 static void
25585 dwarf2out_early_finish (void)
25587 limbo_die_node *node;
25589 /* Add DW_AT_linkage_name for all deferred DIEs. */
25590 for (node = deferred_asm_name; node; node = node->next)
25592 tree decl = node->created_for;
25593 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25594 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25595 ended up in deferred_asm_name before we knew it was
25596 constant and never written to disk. */
25597 && DECL_ASSEMBLER_NAME (decl))
25599 add_linkage_attr (node->die, decl);
25600 move_linkage_attr (node->die);
25603 deferred_asm_name = NULL;
25605 /* The point here is to flush out the limbo list so that it is empty
25606 and we don't need to stream it for LTO. */
25607 flush_limbo_die_list ();
25609 gen_scheduled_generic_parms_dies ();
25610 gen_remaining_tmpl_value_param_die_attribute ();
25613 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25614 within the same process. For use by toplev::finalize. */
25616 void
25617 dwarf2out_c_finalize (void)
25619 last_var_location_insn = NULL;
25620 cached_next_real_insn = NULL;
25621 used_rtx_array = NULL;
25622 incomplete_types = NULL;
25623 decl_scope_table = NULL;
25624 debug_info_section = NULL;
25625 debug_skeleton_info_section = NULL;
25626 debug_abbrev_section = NULL;
25627 debug_skeleton_abbrev_section = NULL;
25628 debug_aranges_section = NULL;
25629 debug_addr_section = NULL;
25630 debug_macinfo_section = NULL;
25631 debug_line_section = NULL;
25632 debug_skeleton_line_section = NULL;
25633 debug_loc_section = NULL;
25634 debug_pubnames_section = NULL;
25635 debug_pubtypes_section = NULL;
25636 debug_str_section = NULL;
25637 debug_str_dwo_section = NULL;
25638 debug_str_offsets_section = NULL;
25639 debug_ranges_section = NULL;
25640 debug_frame_section = NULL;
25641 fde_vec = NULL;
25642 debug_str_hash = NULL;
25643 skeleton_debug_str_hash = NULL;
25644 dw2_string_counter = 0;
25645 have_multiple_function_sections = false;
25646 text_section_used = false;
25647 cold_text_section_used = false;
25648 cold_text_section = NULL;
25649 current_unit_personality = NULL;
25651 next_die_offset = 0;
25652 single_comp_unit_die = NULL;
25653 comdat_type_list = NULL;
25654 limbo_die_list = NULL;
25655 file_table = NULL;
25656 decl_die_table = NULL;
25657 common_block_die_table = NULL;
25658 decl_loc_table = NULL;
25659 call_arg_locations = NULL;
25660 call_arg_loc_last = NULL;
25661 call_site_count = -1;
25662 tail_call_site_count = -1;
25663 cached_dw_loc_list_table = NULL;
25664 abbrev_die_table = NULL;
25665 abbrev_die_table_allocated = 0;
25666 abbrev_die_table_in_use = 0;
25667 line_info_label_num = 0;
25668 cur_line_info_table = NULL;
25669 text_section_line_info = NULL;
25670 cold_text_section_line_info = NULL;
25671 separate_line_info = NULL;
25672 info_section_emitted = false;
25673 pubname_table = NULL;
25674 pubtype_table = NULL;
25675 macinfo_table = NULL;
25676 ranges_table = NULL;
25677 ranges_table_allocated = 0;
25678 ranges_table_in_use = 0;
25679 ranges_by_label = 0;
25680 ranges_by_label_allocated = 0;
25681 ranges_by_label_in_use = 0;
25682 have_location_lists = false;
25683 loclabel_num = 0;
25684 poc_label_num = 0;
25685 last_emitted_file = NULL;
25686 label_num = 0;
25687 tmpl_value_parm_die_table = NULL;
25688 generic_type_instances = NULL;
25689 frame_pointer_fb_offset = 0;
25690 frame_pointer_fb_offset_valid = false;
25691 base_types.release ();
25692 XDELETEVEC (producer_string);
25693 producer_string = NULL;
25696 #include "gt-dwarf2out.h"